* Merge from mainline
[official-gcc.git] / gcc / fold-const.c
blobd8f7efc62e5c89b9e8bf5130639a890db88772ce
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, 2005, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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, an overflowable flag and prior
43 overflow indicators. It forces the value to fit the type and sets
44 TREE_OVERFLOW and TREE_CONSTANT_OVERFLOW as appropriate. */
46 #include "config.h"
47 #include "system.h"
48 #include "coretypes.h"
49 #include "tm.h"
50 #include "flags.h"
51 #include "tree.h"
52 #include "real.h"
53 #include "rtl.h"
54 #include "expr.h"
55 #include "tm_p.h"
56 #include "toplev.h"
57 #include "ggc.h"
58 #include "hashtab.h"
59 #include "langhooks.h"
60 #include "md5.h"
62 /* The following constants represent a bit based encoding of GCC's
63 comparison operators. This encoding simplifies transformations
64 on relational comparison operators, such as AND and OR. */
65 enum comparison_code {
66 COMPCODE_FALSE = 0,
67 COMPCODE_LT = 1,
68 COMPCODE_EQ = 2,
69 COMPCODE_LE = 3,
70 COMPCODE_GT = 4,
71 COMPCODE_LTGT = 5,
72 COMPCODE_GE = 6,
73 COMPCODE_ORD = 7,
74 COMPCODE_UNORD = 8,
75 COMPCODE_UNLT = 9,
76 COMPCODE_UNEQ = 10,
77 COMPCODE_UNLE = 11,
78 COMPCODE_UNGT = 12,
79 COMPCODE_NE = 13,
80 COMPCODE_UNGE = 14,
81 COMPCODE_TRUE = 15
84 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
85 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
86 static bool negate_mathfn_p (enum built_in_function);
87 static bool negate_expr_p (tree);
88 static tree negate_expr (tree);
89 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
90 static tree associate_trees (tree, tree, enum tree_code, tree);
91 static tree const_binop (enum tree_code, tree, tree, int);
92 static enum comparison_code comparison_to_compcode (enum tree_code);
93 static enum tree_code compcode_to_comparison (enum comparison_code);
94 static tree combine_comparisons (enum tree_code, enum tree_code,
95 enum tree_code, tree, tree, tree);
96 static int truth_value_p (enum tree_code);
97 static int operand_equal_for_comparison_p (tree, tree, tree);
98 static int twoval_comparison_p (tree, tree *, tree *, int *);
99 static tree eval_subst (tree, tree, tree, tree, tree);
100 static tree pedantic_omit_one_operand (tree, tree, tree);
101 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
102 static tree make_bit_field_ref (tree, tree, int, int, int);
103 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
104 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
105 enum machine_mode *, int *, int *,
106 tree *, tree *);
107 static int all_ones_mask_p (tree, int);
108 static tree sign_bit_p (tree, tree);
109 static int simple_operand_p (tree);
110 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
111 static tree range_predecessor (tree);
112 static tree range_successor (tree);
113 static tree make_range (tree, int *, tree *, tree *);
114 static tree build_range_check (tree, tree, int, tree, tree);
115 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
116 tree);
117 static tree fold_range_test (enum tree_code, tree, tree, tree);
118 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
119 static tree unextend (tree, int, int, tree);
120 static tree fold_truthop (enum tree_code, tree, tree, tree);
121 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
122 static tree extract_muldiv (tree, tree, enum tree_code, tree);
123 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
124 static int multiple_of_p (tree, tree, tree);
125 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
126 tree, tree,
127 tree, tree, int);
128 static bool fold_real_zero_addition_p (tree, tree, int);
129 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
130 tree, tree, tree);
131 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
132 static tree fold_div_compare (enum tree_code, tree, tree, tree);
133 static bool reorder_operands_p (tree, tree);
134 static tree fold_negate_const (tree, tree);
135 static tree fold_not_const (tree, tree);
136 static tree fold_relational_const (enum tree_code, tree, tree, tree);
138 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
139 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
140 and SUM1. Then this yields nonzero if overflow occurred during the
141 addition.
143 Overflow occurs if A and B have the same sign, but A and SUM differ in
144 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
145 sign. */
146 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
148 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
149 We do that by representing the two-word integer in 4 words, with only
150 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
151 number. The value of the word is LOWPART + HIGHPART * BASE. */
153 #define LOWPART(x) \
154 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
155 #define HIGHPART(x) \
156 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
157 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
159 /* Unpack a two-word integer into 4 words.
160 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
161 WORDS points to the array of HOST_WIDE_INTs. */
163 static void
164 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
166 words[0] = LOWPART (low);
167 words[1] = HIGHPART (low);
168 words[2] = LOWPART (hi);
169 words[3] = HIGHPART (hi);
172 /* Pack an array of 4 words into a two-word integer.
173 WORDS points to the array of words.
174 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
176 static void
177 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
178 HOST_WIDE_INT *hi)
180 *low = words[0] + words[1] * BASE;
181 *hi = words[2] + words[3] * BASE;
184 /* T is an INT_CST node. OVERFLOWABLE indicates if we are interested
185 in overflow of the value, when >0 we are only interested in signed
186 overflow, for <0 we are interested in any overflow. OVERFLOWED
187 indicates whether overflow has already occurred. CONST_OVERFLOWED
188 indicates whether constant overflow has already occurred. We force
189 T's value to be within range of T's type (by setting to 0 or 1 all
190 the bits outside the type's range). We set TREE_OVERFLOWED if,
191 OVERFLOWED is nonzero,
192 or OVERFLOWABLE is >0 and signed overflow occurs
193 or OVERFLOWABLE is <0 and any overflow occurs
194 We set TREE_CONSTANT_OVERFLOWED if,
195 CONST_OVERFLOWED is nonzero
196 or we set TREE_OVERFLOWED.
197 We return either the original T, or a copy. */
199 tree
200 force_fit_type (tree t, int overflowable,
201 bool overflowed, bool overflowed_const)
203 unsigned HOST_WIDE_INT low;
204 HOST_WIDE_INT high;
205 unsigned int prec;
206 int sign_extended_type;
208 gcc_assert (TREE_CODE (t) == INTEGER_CST);
210 low = TREE_INT_CST_LOW (t);
211 high = TREE_INT_CST_HIGH (t);
213 if (POINTER_TYPE_P (TREE_TYPE (t))
214 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
215 prec = POINTER_SIZE;
216 else
217 prec = TYPE_PRECISION (TREE_TYPE (t));
218 /* Size types *are* sign extended. */
219 sign_extended_type = (!TYPE_UNSIGNED (TREE_TYPE (t))
220 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
221 && TYPE_IS_SIZETYPE (TREE_TYPE (t))));
223 /* First clear all bits that are beyond the type's precision. */
225 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
227 else if (prec > HOST_BITS_PER_WIDE_INT)
228 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
229 else
231 high = 0;
232 if (prec < HOST_BITS_PER_WIDE_INT)
233 low &= ~((HOST_WIDE_INT) (-1) << prec);
236 if (!sign_extended_type)
237 /* No sign extension */;
238 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
239 /* Correct width already. */;
240 else if (prec > HOST_BITS_PER_WIDE_INT)
242 /* Sign extend top half? */
243 if (high & ((unsigned HOST_WIDE_INT)1
244 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
245 high |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
247 else if (prec == HOST_BITS_PER_WIDE_INT)
249 if ((HOST_WIDE_INT)low < 0)
250 high = -1;
252 else
254 /* Sign extend bottom half? */
255 if (low & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
257 high = -1;
258 low |= (HOST_WIDE_INT)(-1) << prec;
262 /* If the value changed, return a new node. */
263 if (overflowed || overflowed_const
264 || low != TREE_INT_CST_LOW (t) || high != TREE_INT_CST_HIGH (t))
266 t = build_int_cst_wide (TREE_TYPE (t), low, high);
268 if (overflowed
269 || overflowable < 0
270 || (overflowable > 0 && sign_extended_type))
272 t = copy_node (t);
273 TREE_OVERFLOW (t) = 1;
274 TREE_CONSTANT_OVERFLOW (t) = 1;
276 else if (overflowed_const)
278 t = copy_node (t);
279 TREE_CONSTANT_OVERFLOW (t) = 1;
283 return t;
286 /* Add two doubleword integers with doubleword result.
287 Each argument is given as two `HOST_WIDE_INT' pieces.
288 One argument is L1 and H1; the other, L2 and H2.
289 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
292 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
293 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
294 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
296 unsigned HOST_WIDE_INT l;
297 HOST_WIDE_INT h;
299 l = l1 + l2;
300 h = h1 + h2 + (l < l1);
302 *lv = l;
303 *hv = h;
304 return OVERFLOW_SUM_SIGN (h1, h2, h);
307 /* Negate a doubleword integer with doubleword result.
308 Return nonzero if the operation overflows, assuming it's signed.
309 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
310 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
313 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
314 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
316 if (l1 == 0)
318 *lv = 0;
319 *hv = - h1;
320 return (*hv & h1) < 0;
322 else
324 *lv = -l1;
325 *hv = ~h1;
326 return 0;
330 /* Multiply two doubleword integers with doubleword result.
331 Return nonzero if the operation overflows, assuming it's signed.
332 Each argument is given as two `HOST_WIDE_INT' pieces.
333 One argument is L1 and H1; the other, L2 and H2.
334 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
337 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
338 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
339 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
341 HOST_WIDE_INT arg1[4];
342 HOST_WIDE_INT arg2[4];
343 HOST_WIDE_INT prod[4 * 2];
344 unsigned HOST_WIDE_INT carry;
345 int i, j, k;
346 unsigned HOST_WIDE_INT toplow, neglow;
347 HOST_WIDE_INT tophigh, neghigh;
349 encode (arg1, l1, h1);
350 encode (arg2, l2, h2);
352 memset (prod, 0, sizeof prod);
354 for (i = 0; i < 4; i++)
356 carry = 0;
357 for (j = 0; j < 4; j++)
359 k = i + j;
360 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
361 carry += arg1[i] * arg2[j];
362 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
363 carry += prod[k];
364 prod[k] = LOWPART (carry);
365 carry = HIGHPART (carry);
367 prod[i + 4] = carry;
370 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
372 /* Check for overflow by calculating the top half of the answer in full;
373 it should agree with the low half's sign bit. */
374 decode (prod + 4, &toplow, &tophigh);
375 if (h1 < 0)
377 neg_double (l2, h2, &neglow, &neghigh);
378 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
380 if (h2 < 0)
382 neg_double (l1, h1, &neglow, &neghigh);
383 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
385 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
388 /* Shift the doubleword integer in L1, H1 left by COUNT places
389 keeping only PREC bits of result.
390 Shift right if COUNT is negative.
391 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
392 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
394 void
395 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
396 HOST_WIDE_INT count, unsigned int prec,
397 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
399 unsigned HOST_WIDE_INT signmask;
401 if (count < 0)
403 rshift_double (l1, h1, -count, prec, lv, hv, arith);
404 return;
407 if (SHIFT_COUNT_TRUNCATED)
408 count %= prec;
410 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
412 /* Shifting by the host word size is undefined according to the
413 ANSI standard, so we must handle this as a special case. */
414 *hv = 0;
415 *lv = 0;
417 else if (count >= HOST_BITS_PER_WIDE_INT)
419 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
420 *lv = 0;
422 else
424 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
425 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
426 *lv = l1 << count;
429 /* Sign extend all bits that are beyond the precision. */
431 signmask = -((prec > HOST_BITS_PER_WIDE_INT
432 ? ((unsigned HOST_WIDE_INT) *hv
433 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
434 : (*lv >> (prec - 1))) & 1);
436 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
438 else if (prec >= HOST_BITS_PER_WIDE_INT)
440 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
441 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
443 else
445 *hv = signmask;
446 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
447 *lv |= signmask << prec;
451 /* Shift the doubleword integer in L1, H1 right by COUNT places
452 keeping only PREC bits of result. COUNT must be positive.
453 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
454 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
456 void
457 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
458 HOST_WIDE_INT count, unsigned int prec,
459 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
460 int arith)
462 unsigned HOST_WIDE_INT signmask;
464 signmask = (arith
465 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
466 : 0);
468 if (SHIFT_COUNT_TRUNCATED)
469 count %= prec;
471 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
473 /* Shifting by the host word size is undefined according to the
474 ANSI standard, so we must handle this as a special case. */
475 *hv = 0;
476 *lv = 0;
478 else if (count >= HOST_BITS_PER_WIDE_INT)
480 *hv = 0;
481 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
483 else
485 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
486 *lv = ((l1 >> count)
487 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
490 /* Zero / sign extend all bits that are beyond the precision. */
492 if (count >= (HOST_WIDE_INT)prec)
494 *hv = signmask;
495 *lv = signmask;
497 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
499 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
501 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
502 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
504 else
506 *hv = signmask;
507 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
508 *lv |= signmask << (prec - count);
512 /* Rotate the doubleword integer in L1, H1 left by COUNT places
513 keeping only PREC bits of result.
514 Rotate right if COUNT is negative.
515 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
517 void
518 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
519 HOST_WIDE_INT count, unsigned int prec,
520 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
522 unsigned HOST_WIDE_INT s1l, s2l;
523 HOST_WIDE_INT s1h, s2h;
525 count %= prec;
526 if (count < 0)
527 count += prec;
529 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
530 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
531 *lv = s1l | s2l;
532 *hv = s1h | s2h;
535 /* Rotate the doubleword integer in L1, H1 left by COUNT places
536 keeping only PREC bits of result. COUNT must be positive.
537 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
539 void
540 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
541 HOST_WIDE_INT count, unsigned int prec,
542 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
544 unsigned HOST_WIDE_INT s1l, s2l;
545 HOST_WIDE_INT s1h, s2h;
547 count %= prec;
548 if (count < 0)
549 count += prec;
551 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
552 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
553 *lv = s1l | s2l;
554 *hv = s1h | s2h;
557 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
558 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
559 CODE is a tree code for a kind of division, one of
560 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
561 or EXACT_DIV_EXPR
562 It controls how the quotient is rounded to an integer.
563 Return nonzero if the operation overflows.
564 UNS nonzero says do unsigned division. */
567 div_and_round_double (enum tree_code code, int uns,
568 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
569 HOST_WIDE_INT hnum_orig,
570 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
571 HOST_WIDE_INT hden_orig,
572 unsigned HOST_WIDE_INT *lquo,
573 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
574 HOST_WIDE_INT *hrem)
576 int quo_neg = 0;
577 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
578 HOST_WIDE_INT den[4], quo[4];
579 int i, j;
580 unsigned HOST_WIDE_INT work;
581 unsigned HOST_WIDE_INT carry = 0;
582 unsigned HOST_WIDE_INT lnum = lnum_orig;
583 HOST_WIDE_INT hnum = hnum_orig;
584 unsigned HOST_WIDE_INT lden = lden_orig;
585 HOST_WIDE_INT hden = hden_orig;
586 int overflow = 0;
588 if (hden == 0 && lden == 0)
589 overflow = 1, lden = 1;
591 /* Calculate quotient sign and convert operands to unsigned. */
592 if (!uns)
594 if (hnum < 0)
596 quo_neg = ~ quo_neg;
597 /* (minimum integer) / (-1) is the only overflow case. */
598 if (neg_double (lnum, hnum, &lnum, &hnum)
599 && ((HOST_WIDE_INT) lden & hden) == -1)
600 overflow = 1;
602 if (hden < 0)
604 quo_neg = ~ quo_neg;
605 neg_double (lden, hden, &lden, &hden);
609 if (hnum == 0 && hden == 0)
610 { /* single precision */
611 *hquo = *hrem = 0;
612 /* This unsigned division rounds toward zero. */
613 *lquo = lnum / lden;
614 goto finish_up;
617 if (hnum == 0)
618 { /* trivial case: dividend < divisor */
619 /* hden != 0 already checked. */
620 *hquo = *lquo = 0;
621 *hrem = hnum;
622 *lrem = lnum;
623 goto finish_up;
626 memset (quo, 0, sizeof quo);
628 memset (num, 0, sizeof num); /* to zero 9th element */
629 memset (den, 0, sizeof den);
631 encode (num, lnum, hnum);
632 encode (den, lden, hden);
634 /* Special code for when the divisor < BASE. */
635 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
637 /* hnum != 0 already checked. */
638 for (i = 4 - 1; i >= 0; i--)
640 work = num[i] + carry * BASE;
641 quo[i] = work / lden;
642 carry = work % lden;
645 else
647 /* Full double precision division,
648 with thanks to Don Knuth's "Seminumerical Algorithms". */
649 int num_hi_sig, den_hi_sig;
650 unsigned HOST_WIDE_INT quo_est, scale;
652 /* Find the highest nonzero divisor digit. */
653 for (i = 4 - 1;; i--)
654 if (den[i] != 0)
656 den_hi_sig = i;
657 break;
660 /* Insure that the first digit of the divisor is at least BASE/2.
661 This is required by the quotient digit estimation algorithm. */
663 scale = BASE / (den[den_hi_sig] + 1);
664 if (scale > 1)
665 { /* scale divisor and dividend */
666 carry = 0;
667 for (i = 0; i <= 4 - 1; i++)
669 work = (num[i] * scale) + carry;
670 num[i] = LOWPART (work);
671 carry = HIGHPART (work);
674 num[4] = carry;
675 carry = 0;
676 for (i = 0; i <= 4 - 1; i++)
678 work = (den[i] * scale) + carry;
679 den[i] = LOWPART (work);
680 carry = HIGHPART (work);
681 if (den[i] != 0) den_hi_sig = i;
685 num_hi_sig = 4;
687 /* Main loop */
688 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
690 /* Guess the next quotient digit, quo_est, by dividing the first
691 two remaining dividend digits by the high order quotient digit.
692 quo_est is never low and is at most 2 high. */
693 unsigned HOST_WIDE_INT tmp;
695 num_hi_sig = i + den_hi_sig + 1;
696 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
697 if (num[num_hi_sig] != den[den_hi_sig])
698 quo_est = work / den[den_hi_sig];
699 else
700 quo_est = BASE - 1;
702 /* Refine quo_est so it's usually correct, and at most one high. */
703 tmp = work - quo_est * den[den_hi_sig];
704 if (tmp < BASE
705 && (den[den_hi_sig - 1] * quo_est
706 > (tmp * BASE + num[num_hi_sig - 2])))
707 quo_est--;
709 /* Try QUO_EST as the quotient digit, by multiplying the
710 divisor by QUO_EST and subtracting from the remaining dividend.
711 Keep in mind that QUO_EST is the I - 1st digit. */
713 carry = 0;
714 for (j = 0; j <= den_hi_sig; j++)
716 work = quo_est * den[j] + carry;
717 carry = HIGHPART (work);
718 work = num[i + j] - LOWPART (work);
719 num[i + j] = LOWPART (work);
720 carry += HIGHPART (work) != 0;
723 /* If quo_est was high by one, then num[i] went negative and
724 we need to correct things. */
725 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
727 quo_est--;
728 carry = 0; /* add divisor back in */
729 for (j = 0; j <= den_hi_sig; j++)
731 work = num[i + j] + den[j] + carry;
732 carry = HIGHPART (work);
733 num[i + j] = LOWPART (work);
736 num [num_hi_sig] += carry;
739 /* Store the quotient digit. */
740 quo[i] = quo_est;
744 decode (quo, lquo, hquo);
746 finish_up:
747 /* If result is negative, make it so. */
748 if (quo_neg)
749 neg_double (*lquo, *hquo, lquo, hquo);
751 /* Compute trial remainder: rem = num - (quo * den) */
752 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
753 neg_double (*lrem, *hrem, lrem, hrem);
754 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
756 switch (code)
758 case TRUNC_DIV_EXPR:
759 case TRUNC_MOD_EXPR: /* round toward zero */
760 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
761 return overflow;
763 case FLOOR_DIV_EXPR:
764 case FLOOR_MOD_EXPR: /* round toward negative infinity */
765 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
767 /* quo = quo - 1; */
768 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
769 lquo, hquo);
771 else
772 return overflow;
773 break;
775 case CEIL_DIV_EXPR:
776 case CEIL_MOD_EXPR: /* round toward positive infinity */
777 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
779 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
780 lquo, hquo);
782 else
783 return overflow;
784 break;
786 case ROUND_DIV_EXPR:
787 case ROUND_MOD_EXPR: /* round to closest integer */
789 unsigned HOST_WIDE_INT labs_rem = *lrem;
790 HOST_WIDE_INT habs_rem = *hrem;
791 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
792 HOST_WIDE_INT habs_den = hden, htwice;
794 /* Get absolute values. */
795 if (*hrem < 0)
796 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
797 if (hden < 0)
798 neg_double (lden, hden, &labs_den, &habs_den);
800 /* If (2 * abs (lrem) >= abs (lden)) */
801 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
802 labs_rem, habs_rem, &ltwice, &htwice);
804 if (((unsigned HOST_WIDE_INT) habs_den
805 < (unsigned HOST_WIDE_INT) htwice)
806 || (((unsigned HOST_WIDE_INT) habs_den
807 == (unsigned HOST_WIDE_INT) htwice)
808 && (labs_den < ltwice)))
810 if (*hquo < 0)
811 /* quo = quo - 1; */
812 add_double (*lquo, *hquo,
813 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
814 else
815 /* quo = quo + 1; */
816 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
817 lquo, hquo);
819 else
820 return overflow;
822 break;
824 default:
825 gcc_unreachable ();
828 /* Compute true remainder: rem = num - (quo * den) */
829 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
830 neg_double (*lrem, *hrem, lrem, hrem);
831 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
832 return overflow;
835 /* If ARG2 divides ARG1 with zero remainder, carries out the division
836 of type CODE and returns the quotient.
837 Otherwise returns NULL_TREE. */
839 static tree
840 div_if_zero_remainder (enum tree_code code, tree arg1, tree arg2)
842 unsigned HOST_WIDE_INT int1l, int2l;
843 HOST_WIDE_INT int1h, int2h;
844 unsigned HOST_WIDE_INT quol, reml;
845 HOST_WIDE_INT quoh, remh;
846 tree type = TREE_TYPE (arg1);
847 int uns = TYPE_UNSIGNED (type);
849 int1l = TREE_INT_CST_LOW (arg1);
850 int1h = TREE_INT_CST_HIGH (arg1);
851 int2l = TREE_INT_CST_LOW (arg2);
852 int2h = TREE_INT_CST_HIGH (arg2);
854 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
855 &quol, &quoh, &reml, &remh);
856 if (remh != 0 || reml != 0)
857 return NULL_TREE;
859 return build_int_cst_wide (type, quol, quoh);
862 /* Return true if the built-in mathematical function specified by CODE
863 is odd, i.e. -f(x) == f(-x). */
865 static bool
866 negate_mathfn_p (enum built_in_function code)
868 switch (code)
870 CASE_FLT_FN (BUILT_IN_ASIN):
871 CASE_FLT_FN (BUILT_IN_ASINH):
872 CASE_FLT_FN (BUILT_IN_ATAN):
873 CASE_FLT_FN (BUILT_IN_ATANH):
874 CASE_FLT_FN (BUILT_IN_CBRT):
875 CASE_FLT_FN (BUILT_IN_SIN):
876 CASE_FLT_FN (BUILT_IN_SINH):
877 CASE_FLT_FN (BUILT_IN_TAN):
878 CASE_FLT_FN (BUILT_IN_TANH):
879 return true;
881 default:
882 break;
884 return false;
887 /* Check whether we may negate an integer constant T without causing
888 overflow. */
890 bool
891 may_negate_without_overflow_p (tree t)
893 unsigned HOST_WIDE_INT val;
894 unsigned int prec;
895 tree type;
897 gcc_assert (TREE_CODE (t) == INTEGER_CST);
899 type = TREE_TYPE (t);
900 if (TYPE_UNSIGNED (type))
901 return false;
903 prec = TYPE_PRECISION (type);
904 if (prec > HOST_BITS_PER_WIDE_INT)
906 if (TREE_INT_CST_LOW (t) != 0)
907 return true;
908 prec -= HOST_BITS_PER_WIDE_INT;
909 val = TREE_INT_CST_HIGH (t);
911 else
912 val = TREE_INT_CST_LOW (t);
913 if (prec < HOST_BITS_PER_WIDE_INT)
914 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
915 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
918 /* Determine whether an expression T can be cheaply negated using
919 the function negate_expr. */
921 static bool
922 negate_expr_p (tree t)
924 tree type;
926 if (t == 0)
927 return false;
929 type = TREE_TYPE (t);
931 STRIP_SIGN_NOPS (t);
932 switch (TREE_CODE (t))
934 case INTEGER_CST:
935 if (TYPE_UNSIGNED (type) || ! flag_trapv)
936 return true;
938 /* Check that -CST will not overflow type. */
939 return may_negate_without_overflow_p (t);
940 case BIT_NOT_EXPR:
941 return INTEGRAL_TYPE_P (type);
943 case REAL_CST:
944 case NEGATE_EXPR:
945 return true;
947 case COMPLEX_CST:
948 return negate_expr_p (TREE_REALPART (t))
949 && negate_expr_p (TREE_IMAGPART (t));
951 case PLUS_EXPR:
952 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
953 return false;
954 /* -(A + B) -> (-B) - A. */
955 if (negate_expr_p (TREE_OPERAND (t, 1))
956 && reorder_operands_p (TREE_OPERAND (t, 0),
957 TREE_OPERAND (t, 1)))
958 return true;
959 /* -(A + B) -> (-A) - B. */
960 return negate_expr_p (TREE_OPERAND (t, 0));
962 case MINUS_EXPR:
963 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
964 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
965 && reorder_operands_p (TREE_OPERAND (t, 0),
966 TREE_OPERAND (t, 1));
968 case MULT_EXPR:
969 if (TYPE_UNSIGNED (TREE_TYPE (t)))
970 break;
972 /* Fall through. */
974 case RDIV_EXPR:
975 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
976 return negate_expr_p (TREE_OPERAND (t, 1))
977 || negate_expr_p (TREE_OPERAND (t, 0));
978 break;
980 case TRUNC_DIV_EXPR:
981 case ROUND_DIV_EXPR:
982 case FLOOR_DIV_EXPR:
983 case CEIL_DIV_EXPR:
984 case EXACT_DIV_EXPR:
985 if (TYPE_UNSIGNED (TREE_TYPE (t)) || flag_wrapv)
986 break;
987 return negate_expr_p (TREE_OPERAND (t, 1))
988 || negate_expr_p (TREE_OPERAND (t, 0));
990 case NOP_EXPR:
991 /* Negate -((double)float) as (double)(-float). */
992 if (TREE_CODE (type) == REAL_TYPE)
994 tree tem = strip_float_extensions (t);
995 if (tem != t)
996 return negate_expr_p (tem);
998 break;
1000 case CALL_EXPR:
1001 /* Negate -f(x) as f(-x). */
1002 if (negate_mathfn_p (builtin_mathfn_code (t)))
1003 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
1004 break;
1006 case RSHIFT_EXPR:
1007 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1008 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1010 tree op1 = TREE_OPERAND (t, 1);
1011 if (TREE_INT_CST_HIGH (op1) == 0
1012 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1013 == TREE_INT_CST_LOW (op1))
1014 return true;
1016 break;
1018 default:
1019 break;
1021 return false;
1024 /* Given T, an expression, return the negation of T. Allow for T to be
1025 null, in which case return null. */
1027 static tree
1028 negate_expr (tree t)
1030 tree type;
1031 tree tem;
1033 if (t == 0)
1034 return 0;
1036 type = TREE_TYPE (t);
1037 STRIP_SIGN_NOPS (t);
1039 switch (TREE_CODE (t))
1041 /* Convert - (~A) to A + 1. */
1042 case BIT_NOT_EXPR:
1043 if (INTEGRAL_TYPE_P (type))
1044 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1045 build_int_cst (type, 1));
1046 break;
1048 case INTEGER_CST:
1049 tem = fold_negate_const (t, type);
1050 if (! TREE_OVERFLOW (tem)
1051 || TYPE_UNSIGNED (type)
1052 || ! flag_trapv)
1053 return tem;
1054 break;
1056 case REAL_CST:
1057 tem = fold_negate_const (t, type);
1058 /* Two's complement FP formats, such as c4x, may overflow. */
1059 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
1060 return fold_convert (type, tem);
1061 break;
1063 case COMPLEX_CST:
1065 tree rpart = negate_expr (TREE_REALPART (t));
1066 tree ipart = negate_expr (TREE_IMAGPART (t));
1068 if ((TREE_CODE (rpart) == REAL_CST
1069 && TREE_CODE (ipart) == REAL_CST)
1070 || (TREE_CODE (rpart) == INTEGER_CST
1071 && TREE_CODE (ipart) == INTEGER_CST))
1072 return build_complex (type, rpart, ipart);
1074 break;
1076 case NEGATE_EXPR:
1077 return fold_convert (type, TREE_OPERAND (t, 0));
1079 case PLUS_EXPR:
1080 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1082 /* -(A + B) -> (-B) - A. */
1083 if (negate_expr_p (TREE_OPERAND (t, 1))
1084 && reorder_operands_p (TREE_OPERAND (t, 0),
1085 TREE_OPERAND (t, 1)))
1087 tem = negate_expr (TREE_OPERAND (t, 1));
1088 tem = fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1089 tem, TREE_OPERAND (t, 0));
1090 return fold_convert (type, tem);
1093 /* -(A + B) -> (-A) - B. */
1094 if (negate_expr_p (TREE_OPERAND (t, 0)))
1096 tem = negate_expr (TREE_OPERAND (t, 0));
1097 tem = fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1098 tem, TREE_OPERAND (t, 1));
1099 return fold_convert (type, tem);
1102 break;
1104 case MINUS_EXPR:
1105 /* - (A - B) -> B - A */
1106 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1107 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1108 return fold_convert (type,
1109 fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1110 TREE_OPERAND (t, 1),
1111 TREE_OPERAND (t, 0)));
1112 break;
1114 case MULT_EXPR:
1115 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1116 break;
1118 /* Fall through. */
1120 case RDIV_EXPR:
1121 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1123 tem = TREE_OPERAND (t, 1);
1124 if (negate_expr_p (tem))
1125 return fold_convert (type,
1126 fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1127 TREE_OPERAND (t, 0),
1128 negate_expr (tem)));
1129 tem = TREE_OPERAND (t, 0);
1130 if (negate_expr_p (tem))
1131 return fold_convert (type,
1132 fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1133 negate_expr (tem),
1134 TREE_OPERAND (t, 1)));
1136 break;
1138 case TRUNC_DIV_EXPR:
1139 case ROUND_DIV_EXPR:
1140 case FLOOR_DIV_EXPR:
1141 case CEIL_DIV_EXPR:
1142 case EXACT_DIV_EXPR:
1143 if (!TYPE_UNSIGNED (TREE_TYPE (t)) && !flag_wrapv)
1145 tem = TREE_OPERAND (t, 1);
1146 if (negate_expr_p (tem))
1147 return fold_convert (type,
1148 fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1149 TREE_OPERAND (t, 0),
1150 negate_expr (tem)));
1151 tem = TREE_OPERAND (t, 0);
1152 if (negate_expr_p (tem))
1153 return fold_convert (type,
1154 fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1155 negate_expr (tem),
1156 TREE_OPERAND (t, 1)));
1158 break;
1160 case NOP_EXPR:
1161 /* Convert -((double)float) into (double)(-float). */
1162 if (TREE_CODE (type) == REAL_TYPE)
1164 tem = strip_float_extensions (t);
1165 if (tem != t && negate_expr_p (tem))
1166 return fold_convert (type, negate_expr (tem));
1168 break;
1170 case CALL_EXPR:
1171 /* Negate -f(x) as f(-x). */
1172 if (negate_mathfn_p (builtin_mathfn_code (t))
1173 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1175 tree fndecl, arg, arglist;
1177 fndecl = get_callee_fndecl (t);
1178 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1179 arglist = build_tree_list (NULL_TREE, arg);
1180 return build_function_call_expr (fndecl, arglist);
1182 break;
1184 case RSHIFT_EXPR:
1185 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1186 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1188 tree op1 = TREE_OPERAND (t, 1);
1189 if (TREE_INT_CST_HIGH (op1) == 0
1190 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1191 == TREE_INT_CST_LOW (op1))
1193 tree ntype = TYPE_UNSIGNED (type)
1194 ? lang_hooks.types.signed_type (type)
1195 : lang_hooks.types.unsigned_type (type);
1196 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1197 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1198 return fold_convert (type, temp);
1201 break;
1203 default:
1204 break;
1207 tem = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1208 return fold_convert (type, tem);
1211 /* Split a tree IN into a constant, literal and variable parts that could be
1212 combined with CODE to make IN. "constant" means an expression with
1213 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1214 commutative arithmetic operation. Store the constant part into *CONP,
1215 the literal in *LITP and return the variable part. If a part isn't
1216 present, set it to null. If the tree does not decompose in this way,
1217 return the entire tree as the variable part and the other parts as null.
1219 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1220 case, we negate an operand that was subtracted. Except if it is a
1221 literal for which we use *MINUS_LITP instead.
1223 If NEGATE_P is true, we are negating all of IN, again except a literal
1224 for which we use *MINUS_LITP instead.
1226 If IN is itself a literal or constant, return it as appropriate.
1228 Note that we do not guarantee that any of the three values will be the
1229 same type as IN, but they will have the same signedness and mode. */
1231 static tree
1232 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1233 tree *minus_litp, int negate_p)
1235 tree var = 0;
1237 *conp = 0;
1238 *litp = 0;
1239 *minus_litp = 0;
1241 /* Strip any conversions that don't change the machine mode or signedness. */
1242 STRIP_SIGN_NOPS (in);
1244 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1245 *litp = in;
1246 else if (TREE_CODE (in) == code
1247 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1248 /* We can associate addition and subtraction together (even
1249 though the C standard doesn't say so) for integers because
1250 the value is not affected. For reals, the value might be
1251 affected, so we can't. */
1252 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1253 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1255 tree op0 = TREE_OPERAND (in, 0);
1256 tree op1 = TREE_OPERAND (in, 1);
1257 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1258 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1260 /* First see if either of the operands is a literal, then a constant. */
1261 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1262 *litp = op0, op0 = 0;
1263 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1264 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1266 if (op0 != 0 && TREE_CONSTANT (op0))
1267 *conp = op0, op0 = 0;
1268 else if (op1 != 0 && TREE_CONSTANT (op1))
1269 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1271 /* If we haven't dealt with either operand, this is not a case we can
1272 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1273 if (op0 != 0 && op1 != 0)
1274 var = in;
1275 else if (op0 != 0)
1276 var = op0;
1277 else
1278 var = op1, neg_var_p = neg1_p;
1280 /* Now do any needed negations. */
1281 if (neg_litp_p)
1282 *minus_litp = *litp, *litp = 0;
1283 if (neg_conp_p)
1284 *conp = negate_expr (*conp);
1285 if (neg_var_p)
1286 var = negate_expr (var);
1288 else if (TREE_CONSTANT (in))
1289 *conp = in;
1290 else
1291 var = in;
1293 if (negate_p)
1295 if (*litp)
1296 *minus_litp = *litp, *litp = 0;
1297 else if (*minus_litp)
1298 *litp = *minus_litp, *minus_litp = 0;
1299 *conp = negate_expr (*conp);
1300 var = negate_expr (var);
1303 return var;
1306 /* Re-associate trees split by the above function. T1 and T2 are either
1307 expressions to associate or null. Return the new expression, if any. If
1308 we build an operation, do it in TYPE and with CODE. */
1310 static tree
1311 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1313 if (t1 == 0)
1314 return t2;
1315 else if (t2 == 0)
1316 return t1;
1318 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1319 try to fold this since we will have infinite recursion. But do
1320 deal with any NEGATE_EXPRs. */
1321 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1322 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1324 if (code == PLUS_EXPR)
1326 if (TREE_CODE (t1) == NEGATE_EXPR)
1327 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1328 fold_convert (type, TREE_OPERAND (t1, 0)));
1329 else if (TREE_CODE (t2) == NEGATE_EXPR)
1330 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1331 fold_convert (type, TREE_OPERAND (t2, 0)));
1332 else if (integer_zerop (t2))
1333 return fold_convert (type, t1);
1335 else if (code == MINUS_EXPR)
1337 if (integer_zerop (t2))
1338 return fold_convert (type, t1);
1341 return build2 (code, type, fold_convert (type, t1),
1342 fold_convert (type, t2));
1345 return fold_build2 (code, type, fold_convert (type, t1),
1346 fold_convert (type, t2));
1349 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1350 to produce a new constant. Return NULL_TREE if we don't know how
1351 to evaluate CODE at compile-time.
1353 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1355 tree
1356 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1358 unsigned HOST_WIDE_INT int1l, int2l;
1359 HOST_WIDE_INT int1h, int2h;
1360 unsigned HOST_WIDE_INT low;
1361 HOST_WIDE_INT hi;
1362 unsigned HOST_WIDE_INT garbagel;
1363 HOST_WIDE_INT garbageh;
1364 tree t;
1365 tree type = TREE_TYPE (arg1);
1366 int uns = TYPE_UNSIGNED (type);
1367 int is_sizetype
1368 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1369 int overflow = 0;
1371 int1l = TREE_INT_CST_LOW (arg1);
1372 int1h = TREE_INT_CST_HIGH (arg1);
1373 int2l = TREE_INT_CST_LOW (arg2);
1374 int2h = TREE_INT_CST_HIGH (arg2);
1376 switch (code)
1378 case BIT_IOR_EXPR:
1379 low = int1l | int2l, hi = int1h | int2h;
1380 break;
1382 case BIT_XOR_EXPR:
1383 low = int1l ^ int2l, hi = int1h ^ int2h;
1384 break;
1386 case BIT_AND_EXPR:
1387 low = int1l & int2l, hi = int1h & int2h;
1388 break;
1390 case RSHIFT_EXPR:
1391 int2l = -int2l;
1392 case LSHIFT_EXPR:
1393 /* It's unclear from the C standard whether shifts can overflow.
1394 The following code ignores overflow; perhaps a C standard
1395 interpretation ruling is needed. */
1396 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1397 &low, &hi, !uns);
1398 break;
1400 case RROTATE_EXPR:
1401 int2l = - int2l;
1402 case LROTATE_EXPR:
1403 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1404 &low, &hi);
1405 break;
1407 case PLUS_EXPR:
1408 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1409 break;
1411 case MINUS_EXPR:
1412 neg_double (int2l, int2h, &low, &hi);
1413 add_double (int1l, int1h, low, hi, &low, &hi);
1414 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1415 break;
1417 case MULT_EXPR:
1418 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1419 break;
1421 case TRUNC_DIV_EXPR:
1422 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1423 case EXACT_DIV_EXPR:
1424 /* This is a shortcut for a common special case. */
1425 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1426 && ! TREE_CONSTANT_OVERFLOW (arg1)
1427 && ! TREE_CONSTANT_OVERFLOW (arg2)
1428 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1430 if (code == CEIL_DIV_EXPR)
1431 int1l += int2l - 1;
1433 low = int1l / int2l, hi = 0;
1434 break;
1437 /* ... fall through ... */
1439 case ROUND_DIV_EXPR:
1440 if (int2h == 0 && int2l == 0)
1441 return NULL_TREE;
1442 if (int2h == 0 && int2l == 1)
1444 low = int1l, hi = int1h;
1445 break;
1447 if (int1l == int2l && int1h == int2h
1448 && ! (int1l == 0 && int1h == 0))
1450 low = 1, hi = 0;
1451 break;
1453 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1454 &low, &hi, &garbagel, &garbageh);
1455 break;
1457 case TRUNC_MOD_EXPR:
1458 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1459 /* This is a shortcut for a common special case. */
1460 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1461 && ! TREE_CONSTANT_OVERFLOW (arg1)
1462 && ! TREE_CONSTANT_OVERFLOW (arg2)
1463 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1465 if (code == CEIL_MOD_EXPR)
1466 int1l += int2l - 1;
1467 low = int1l % int2l, hi = 0;
1468 break;
1471 /* ... fall through ... */
1473 case ROUND_MOD_EXPR:
1474 if (int2h == 0 && int2l == 0)
1475 return NULL_TREE;
1476 overflow = div_and_round_double (code, uns,
1477 int1l, int1h, int2l, int2h,
1478 &garbagel, &garbageh, &low, &hi);
1479 break;
1481 case MIN_EXPR:
1482 case MAX_EXPR:
1483 if (uns)
1484 low = (((unsigned HOST_WIDE_INT) int1h
1485 < (unsigned HOST_WIDE_INT) int2h)
1486 || (((unsigned HOST_WIDE_INT) int1h
1487 == (unsigned HOST_WIDE_INT) int2h)
1488 && int1l < int2l));
1489 else
1490 low = (int1h < int2h
1491 || (int1h == int2h && int1l < int2l));
1493 if (low == (code == MIN_EXPR))
1494 low = int1l, hi = int1h;
1495 else
1496 low = int2l, hi = int2h;
1497 break;
1499 default:
1500 return NULL_TREE;
1503 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1505 if (notrunc)
1507 /* Propagate overflow flags ourselves. */
1508 if (((!uns || is_sizetype) && overflow)
1509 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1511 t = copy_node (t);
1512 TREE_OVERFLOW (t) = 1;
1513 TREE_CONSTANT_OVERFLOW (t) = 1;
1515 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1517 t = copy_node (t);
1518 TREE_CONSTANT_OVERFLOW (t) = 1;
1521 else
1522 t = force_fit_type (t, 1,
1523 ((!uns || is_sizetype) && overflow)
1524 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2),
1525 TREE_CONSTANT_OVERFLOW (arg1)
1526 | TREE_CONSTANT_OVERFLOW (arg2));
1528 return t;
1531 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1532 constant. We assume ARG1 and ARG2 have the same data type, or at least
1533 are the same kind of constant and the same machine mode.
1535 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1537 static tree
1538 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1540 STRIP_NOPS (arg1);
1541 STRIP_NOPS (arg2);
1543 if (TREE_CODE (arg1) == INTEGER_CST)
1544 return int_const_binop (code, arg1, arg2, notrunc);
1546 if (TREE_CODE (arg1) == REAL_CST)
1548 enum machine_mode mode;
1549 REAL_VALUE_TYPE d1;
1550 REAL_VALUE_TYPE d2;
1551 REAL_VALUE_TYPE value;
1552 REAL_VALUE_TYPE result;
1553 bool inexact;
1554 tree t, type;
1556 /* The following codes are handled by real_arithmetic. */
1557 switch (code)
1559 case PLUS_EXPR:
1560 case MINUS_EXPR:
1561 case MULT_EXPR:
1562 case RDIV_EXPR:
1563 case MIN_EXPR:
1564 case MAX_EXPR:
1565 break;
1567 default:
1568 return NULL_TREE;
1571 d1 = TREE_REAL_CST (arg1);
1572 d2 = TREE_REAL_CST (arg2);
1574 type = TREE_TYPE (arg1);
1575 mode = TYPE_MODE (type);
1577 /* Don't perform operation if we honor signaling NaNs and
1578 either operand is a NaN. */
1579 if (HONOR_SNANS (mode)
1580 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1581 return NULL_TREE;
1583 /* Don't perform operation if it would raise a division
1584 by zero exception. */
1585 if (code == RDIV_EXPR
1586 && REAL_VALUES_EQUAL (d2, dconst0)
1587 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1588 return NULL_TREE;
1590 /* If either operand is a NaN, just return it. Otherwise, set up
1591 for floating-point trap; we return an overflow. */
1592 if (REAL_VALUE_ISNAN (d1))
1593 return arg1;
1594 else if (REAL_VALUE_ISNAN (d2))
1595 return arg2;
1597 inexact = real_arithmetic (&value, code, &d1, &d2);
1598 real_convert (&result, mode, &value);
1600 /* Don't constant fold this floating point operation if
1601 the result has overflowed and flag_trapping_math. */
1603 if (flag_trapping_math
1604 && MODE_HAS_INFINITIES (mode)
1605 && REAL_VALUE_ISINF (result)
1606 && !REAL_VALUE_ISINF (d1)
1607 && !REAL_VALUE_ISINF (d2))
1608 return NULL_TREE;
1610 /* Don't constant fold this floating point operation if the
1611 result may dependent upon the run-time rounding mode and
1612 flag_rounding_math is set, or if GCC's software emulation
1613 is unable to accurately represent the result. */
1615 if ((flag_rounding_math
1616 || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1617 && !flag_unsafe_math_optimizations))
1618 && (inexact || !real_identical (&result, &value)))
1619 return NULL_TREE;
1621 t = build_real (type, result);
1623 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1624 TREE_CONSTANT_OVERFLOW (t)
1625 = TREE_OVERFLOW (t)
1626 | TREE_CONSTANT_OVERFLOW (arg1)
1627 | TREE_CONSTANT_OVERFLOW (arg2);
1628 return t;
1631 if (TREE_CODE (arg1) == COMPLEX_CST)
1633 tree type = TREE_TYPE (arg1);
1634 tree r1 = TREE_REALPART (arg1);
1635 tree i1 = TREE_IMAGPART (arg1);
1636 tree r2 = TREE_REALPART (arg2);
1637 tree i2 = TREE_IMAGPART (arg2);
1638 tree t;
1640 switch (code)
1642 case PLUS_EXPR:
1643 t = build_complex (type,
1644 const_binop (PLUS_EXPR, r1, r2, notrunc),
1645 const_binop (PLUS_EXPR, i1, i2, notrunc));
1646 break;
1648 case MINUS_EXPR:
1649 t = build_complex (type,
1650 const_binop (MINUS_EXPR, r1, r2, notrunc),
1651 const_binop (MINUS_EXPR, i1, i2, notrunc));
1652 break;
1654 case MULT_EXPR:
1655 t = build_complex (type,
1656 const_binop (MINUS_EXPR,
1657 const_binop (MULT_EXPR,
1658 r1, r2, notrunc),
1659 const_binop (MULT_EXPR,
1660 i1, i2, notrunc),
1661 notrunc),
1662 const_binop (PLUS_EXPR,
1663 const_binop (MULT_EXPR,
1664 r1, i2, notrunc),
1665 const_binop (MULT_EXPR,
1666 i1, r2, notrunc),
1667 notrunc));
1668 break;
1670 case RDIV_EXPR:
1672 tree t1, t2, real, imag;
1673 tree magsquared
1674 = const_binop (PLUS_EXPR,
1675 const_binop (MULT_EXPR, r2, r2, notrunc),
1676 const_binop (MULT_EXPR, i2, i2, notrunc),
1677 notrunc);
1679 t1 = const_binop (PLUS_EXPR,
1680 const_binop (MULT_EXPR, r1, r2, notrunc),
1681 const_binop (MULT_EXPR, i1, i2, notrunc),
1682 notrunc);
1683 t2 = const_binop (MINUS_EXPR,
1684 const_binop (MULT_EXPR, i1, r2, notrunc),
1685 const_binop (MULT_EXPR, r1, i2, notrunc),
1686 notrunc);
1688 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1690 real = const_binop (TRUNC_DIV_EXPR, t1, magsquared, notrunc);
1691 imag = const_binop (TRUNC_DIV_EXPR, t2, magsquared, notrunc);
1693 else
1695 real = const_binop (RDIV_EXPR, t1, magsquared, notrunc);
1696 imag = const_binop (RDIV_EXPR, t2, magsquared, notrunc);
1697 if (!real || !imag)
1698 return NULL_TREE;
1701 t = build_complex (type, real, imag);
1703 break;
1705 default:
1706 return NULL_TREE;
1708 return t;
1710 return NULL_TREE;
1713 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1714 indicates which particular sizetype to create. */
1716 tree
1717 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1719 return build_int_cst (sizetype_tab[(int) kind], number);
1722 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1723 is a tree code. The type of the result is taken from the operands.
1724 Both must be the same type integer type and it must be a size type.
1725 If the operands are constant, so is the result. */
1727 tree
1728 size_binop (enum tree_code code, tree arg0, tree arg1)
1730 tree type = TREE_TYPE (arg0);
1732 gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1733 && type == TREE_TYPE (arg1));
1735 /* Handle the special case of two integer constants faster. */
1736 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1738 /* And some specific cases even faster than that. */
1739 if (code == PLUS_EXPR && integer_zerop (arg0))
1740 return arg1;
1741 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1742 && integer_zerop (arg1))
1743 return arg0;
1744 else if (code == MULT_EXPR && integer_onep (arg0))
1745 return arg1;
1747 /* Handle general case of two integer constants. */
1748 return int_const_binop (code, arg0, arg1, 0);
1751 if (arg0 == error_mark_node || arg1 == error_mark_node)
1752 return error_mark_node;
1754 return fold_build2 (code, type, arg0, arg1);
1757 /* Given two values, either both of sizetype or both of bitsizetype,
1758 compute the difference between the two values. Return the value
1759 in signed type corresponding to the type of the operands. */
1761 tree
1762 size_diffop (tree arg0, tree arg1)
1764 tree type = TREE_TYPE (arg0);
1765 tree ctype;
1767 gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1768 && type == TREE_TYPE (arg1));
1770 /* If the type is already signed, just do the simple thing. */
1771 if (!TYPE_UNSIGNED (type))
1772 return size_binop (MINUS_EXPR, arg0, arg1);
1774 ctype = type == bitsizetype ? sbitsizetype : ssizetype;
1776 /* If either operand is not a constant, do the conversions to the signed
1777 type and subtract. The hardware will do the right thing with any
1778 overflow in the subtraction. */
1779 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1780 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1781 fold_convert (ctype, arg1));
1783 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1784 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1785 overflow) and negate (which can't either). Special-case a result
1786 of zero while we're here. */
1787 if (tree_int_cst_equal (arg0, arg1))
1788 return build_int_cst (ctype, 0);
1789 else if (tree_int_cst_lt (arg1, arg0))
1790 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1791 else
1792 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
1793 fold_convert (ctype, size_binop (MINUS_EXPR,
1794 arg1, arg0)));
1797 /* A subroutine of fold_convert_const handling conversions of an
1798 INTEGER_CST to another integer type. */
1800 static tree
1801 fold_convert_const_int_from_int (tree type, tree arg1)
1803 tree t;
1805 /* Given an integer constant, make new constant with new type,
1806 appropriately sign-extended or truncated. */
1807 t = build_int_cst_wide (type, TREE_INT_CST_LOW (arg1),
1808 TREE_INT_CST_HIGH (arg1));
1810 t = force_fit_type (t,
1811 /* Don't set the overflow when
1812 converting a pointer */
1813 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1814 (TREE_INT_CST_HIGH (arg1) < 0
1815 && (TYPE_UNSIGNED (type)
1816 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1817 | TREE_OVERFLOW (arg1),
1818 TREE_CONSTANT_OVERFLOW (arg1));
1820 return t;
1823 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1824 to an integer type. */
1826 static tree
1827 fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
1829 int overflow = 0;
1830 tree t;
1832 /* The following code implements the floating point to integer
1833 conversion rules required by the Java Language Specification,
1834 that IEEE NaNs are mapped to zero and values that overflow
1835 the target precision saturate, i.e. values greater than
1836 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1837 are mapped to INT_MIN. These semantics are allowed by the
1838 C and C++ standards that simply state that the behavior of
1839 FP-to-integer conversion is unspecified upon overflow. */
1841 HOST_WIDE_INT high, low;
1842 REAL_VALUE_TYPE r;
1843 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1845 switch (code)
1847 case FIX_TRUNC_EXPR:
1848 real_trunc (&r, VOIDmode, &x);
1849 break;
1851 case FIX_CEIL_EXPR:
1852 real_ceil (&r, VOIDmode, &x);
1853 break;
1855 case FIX_FLOOR_EXPR:
1856 real_floor (&r, VOIDmode, &x);
1857 break;
1859 case FIX_ROUND_EXPR:
1860 real_round (&r, VOIDmode, &x);
1861 break;
1863 default:
1864 gcc_unreachable ();
1867 /* If R is NaN, return zero and show we have an overflow. */
1868 if (REAL_VALUE_ISNAN (r))
1870 overflow = 1;
1871 high = 0;
1872 low = 0;
1875 /* See if R is less than the lower bound or greater than the
1876 upper bound. */
1878 if (! overflow)
1880 tree lt = TYPE_MIN_VALUE (type);
1881 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1882 if (REAL_VALUES_LESS (r, l))
1884 overflow = 1;
1885 high = TREE_INT_CST_HIGH (lt);
1886 low = TREE_INT_CST_LOW (lt);
1890 if (! overflow)
1892 tree ut = TYPE_MAX_VALUE (type);
1893 if (ut)
1895 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1896 if (REAL_VALUES_LESS (u, r))
1898 overflow = 1;
1899 high = TREE_INT_CST_HIGH (ut);
1900 low = TREE_INT_CST_LOW (ut);
1905 if (! overflow)
1906 REAL_VALUE_TO_INT (&low, &high, r);
1908 t = build_int_cst_wide (type, low, high);
1910 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg1),
1911 TREE_CONSTANT_OVERFLOW (arg1));
1912 return t;
1915 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1916 to another floating point type. */
1918 static tree
1919 fold_convert_const_real_from_real (tree type, tree arg1)
1921 REAL_VALUE_TYPE value;
1922 tree t;
1924 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1925 t = build_real (type, value);
1927 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1928 TREE_CONSTANT_OVERFLOW (t)
1929 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1930 return t;
1933 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1934 type TYPE. If no simplification can be done return NULL_TREE. */
1936 static tree
1937 fold_convert_const (enum tree_code code, tree type, tree arg1)
1939 if (TREE_TYPE (arg1) == type)
1940 return arg1;
1942 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1944 if (TREE_CODE (arg1) == INTEGER_CST)
1945 return fold_convert_const_int_from_int (type, arg1);
1946 else if (TREE_CODE (arg1) == REAL_CST)
1947 return fold_convert_const_int_from_real (code, type, arg1);
1949 else if (TREE_CODE (type) == REAL_TYPE)
1951 if (TREE_CODE (arg1) == INTEGER_CST)
1952 return build_real_from_int_cst (type, arg1);
1953 if (TREE_CODE (arg1) == REAL_CST)
1954 return fold_convert_const_real_from_real (type, arg1);
1956 return NULL_TREE;
1959 /* Construct a vector of zero elements of vector type TYPE. */
1961 static tree
1962 build_zero_vector (tree type)
1964 tree elem, list;
1965 int i, units;
1967 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1968 units = TYPE_VECTOR_SUBPARTS (type);
1970 list = NULL_TREE;
1971 for (i = 0; i < units; i++)
1972 list = tree_cons (NULL_TREE, elem, list);
1973 return build_vector (type, list);
1976 /* Convert expression ARG to type TYPE. Used by the middle-end for
1977 simple conversions in preference to calling the front-end's convert. */
1979 tree
1980 fold_convert (tree type, tree arg)
1982 tree orig = TREE_TYPE (arg);
1983 tree tem;
1985 if (type == orig)
1986 return arg;
1988 if (TREE_CODE (arg) == ERROR_MARK
1989 || TREE_CODE (type) == ERROR_MARK
1990 || TREE_CODE (orig) == ERROR_MARK)
1991 return error_mark_node;
1993 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
1994 || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
1995 TYPE_MAIN_VARIANT (orig)))
1996 return fold_build1 (NOP_EXPR, type, arg);
1998 switch (TREE_CODE (type))
2000 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2001 case POINTER_TYPE: case REFERENCE_TYPE:
2002 case OFFSET_TYPE:
2003 if (TREE_CODE (arg) == INTEGER_CST)
2005 tem = fold_convert_const (NOP_EXPR, type, arg);
2006 if (tem != NULL_TREE)
2007 return tem;
2009 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2010 || TREE_CODE (orig) == OFFSET_TYPE)
2011 return fold_build1 (NOP_EXPR, type, arg);
2012 if (TREE_CODE (orig) == COMPLEX_TYPE)
2014 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2015 return fold_convert (type, tem);
2017 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2018 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2019 return fold_build1 (NOP_EXPR, type, arg);
2021 case REAL_TYPE:
2022 if (TREE_CODE (arg) == INTEGER_CST)
2024 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2025 if (tem != NULL_TREE)
2026 return tem;
2028 else if (TREE_CODE (arg) == REAL_CST)
2030 tem = fold_convert_const (NOP_EXPR, type, arg);
2031 if (tem != NULL_TREE)
2032 return tem;
2035 switch (TREE_CODE (orig))
2037 case INTEGER_TYPE:
2038 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2039 case POINTER_TYPE: case REFERENCE_TYPE:
2040 return fold_build1 (FLOAT_EXPR, type, arg);
2042 case REAL_TYPE:
2043 return fold_build1 (NOP_EXPR, type, arg);
2045 case COMPLEX_TYPE:
2046 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2047 return fold_convert (type, tem);
2049 default:
2050 gcc_unreachable ();
2053 case COMPLEX_TYPE:
2054 switch (TREE_CODE (orig))
2056 case INTEGER_TYPE:
2057 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2058 case POINTER_TYPE: case REFERENCE_TYPE:
2059 case REAL_TYPE:
2060 return build2 (COMPLEX_EXPR, type,
2061 fold_convert (TREE_TYPE (type), arg),
2062 fold_convert (TREE_TYPE (type), integer_zero_node));
2063 case COMPLEX_TYPE:
2065 tree rpart, ipart;
2067 if (TREE_CODE (arg) == COMPLEX_EXPR)
2069 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2070 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2071 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2074 arg = save_expr (arg);
2075 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2076 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2077 rpart = fold_convert (TREE_TYPE (type), rpart);
2078 ipart = fold_convert (TREE_TYPE (type), ipart);
2079 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2082 default:
2083 gcc_unreachable ();
2086 case VECTOR_TYPE:
2087 if (integer_zerop (arg))
2088 return build_zero_vector (type);
2089 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2090 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2091 || TREE_CODE (orig) == VECTOR_TYPE);
2092 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2094 case VOID_TYPE:
2095 return fold_build1 (NOP_EXPR, type, fold_ignored_result (arg));
2097 default:
2098 gcc_unreachable ();
2102 /* Return false if expr can be assumed not to be an lvalue, true
2103 otherwise. */
2105 static bool
2106 maybe_lvalue_p (tree x)
2108 /* We only need to wrap lvalue tree codes. */
2109 switch (TREE_CODE (x))
2111 case VAR_DECL:
2112 case PARM_DECL:
2113 case RESULT_DECL:
2114 case LABEL_DECL:
2115 case FUNCTION_DECL:
2116 case SSA_NAME:
2118 case COMPONENT_REF:
2119 case INDIRECT_REF:
2120 case ALIGN_INDIRECT_REF:
2121 case MISALIGNED_INDIRECT_REF:
2122 case ARRAY_REF:
2123 case ARRAY_RANGE_REF:
2124 case BIT_FIELD_REF:
2125 case OBJ_TYPE_REF:
2127 case REALPART_EXPR:
2128 case IMAGPART_EXPR:
2129 case PREINCREMENT_EXPR:
2130 case PREDECREMENT_EXPR:
2131 case SAVE_EXPR:
2132 case TRY_CATCH_EXPR:
2133 case WITH_CLEANUP_EXPR:
2134 case COMPOUND_EXPR:
2135 case MODIFY_EXPR:
2136 case TARGET_EXPR:
2137 case COND_EXPR:
2138 case BIND_EXPR:
2139 case MIN_EXPR:
2140 case MAX_EXPR:
2141 break;
2143 default:
2144 /* Assume the worst for front-end tree codes. */
2145 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2146 break;
2147 return false;
2150 return true;
2153 /* Return an expr equal to X but certainly not valid as an lvalue. */
2155 tree
2156 non_lvalue (tree x)
2158 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2159 us. */
2160 if (in_gimple_form)
2161 return x;
2163 if (! maybe_lvalue_p (x))
2164 return x;
2165 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2168 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2169 Zero means allow extended lvalues. */
2171 int pedantic_lvalues;
2173 /* When pedantic, return an expr equal to X but certainly not valid as a
2174 pedantic lvalue. Otherwise, return X. */
2176 static tree
2177 pedantic_non_lvalue (tree x)
2179 if (pedantic_lvalues)
2180 return non_lvalue (x);
2181 else
2182 return x;
2185 /* Given a tree comparison code, return the code that is the logical inverse
2186 of the given code. It is not safe to do this for floating-point
2187 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2188 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2190 enum tree_code
2191 invert_tree_comparison (enum tree_code code, bool honor_nans)
2193 if (honor_nans && flag_trapping_math)
2194 return ERROR_MARK;
2196 switch (code)
2198 case EQ_EXPR:
2199 return NE_EXPR;
2200 case NE_EXPR:
2201 return EQ_EXPR;
2202 case GT_EXPR:
2203 return honor_nans ? UNLE_EXPR : LE_EXPR;
2204 case GE_EXPR:
2205 return honor_nans ? UNLT_EXPR : LT_EXPR;
2206 case LT_EXPR:
2207 return honor_nans ? UNGE_EXPR : GE_EXPR;
2208 case LE_EXPR:
2209 return honor_nans ? UNGT_EXPR : GT_EXPR;
2210 case LTGT_EXPR:
2211 return UNEQ_EXPR;
2212 case UNEQ_EXPR:
2213 return LTGT_EXPR;
2214 case UNGT_EXPR:
2215 return LE_EXPR;
2216 case UNGE_EXPR:
2217 return LT_EXPR;
2218 case UNLT_EXPR:
2219 return GE_EXPR;
2220 case UNLE_EXPR:
2221 return GT_EXPR;
2222 case ORDERED_EXPR:
2223 return UNORDERED_EXPR;
2224 case UNORDERED_EXPR:
2225 return ORDERED_EXPR;
2226 default:
2227 gcc_unreachable ();
2231 /* Similar, but return the comparison that results if the operands are
2232 swapped. This is safe for floating-point. */
2234 enum tree_code
2235 swap_tree_comparison (enum tree_code code)
2237 switch (code)
2239 case EQ_EXPR:
2240 case NE_EXPR:
2241 case ORDERED_EXPR:
2242 case UNORDERED_EXPR:
2243 case LTGT_EXPR:
2244 case UNEQ_EXPR:
2245 return code;
2246 case GT_EXPR:
2247 return LT_EXPR;
2248 case GE_EXPR:
2249 return LE_EXPR;
2250 case LT_EXPR:
2251 return GT_EXPR;
2252 case LE_EXPR:
2253 return GE_EXPR;
2254 case UNGT_EXPR:
2255 return UNLT_EXPR;
2256 case UNGE_EXPR:
2257 return UNLE_EXPR;
2258 case UNLT_EXPR:
2259 return UNGT_EXPR;
2260 case UNLE_EXPR:
2261 return UNGE_EXPR;
2262 default:
2263 gcc_unreachable ();
2268 /* Convert a comparison tree code from an enum tree_code representation
2269 into a compcode bit-based encoding. This function is the inverse of
2270 compcode_to_comparison. */
2272 static enum comparison_code
2273 comparison_to_compcode (enum tree_code code)
2275 switch (code)
2277 case LT_EXPR:
2278 return COMPCODE_LT;
2279 case EQ_EXPR:
2280 return COMPCODE_EQ;
2281 case LE_EXPR:
2282 return COMPCODE_LE;
2283 case GT_EXPR:
2284 return COMPCODE_GT;
2285 case NE_EXPR:
2286 return COMPCODE_NE;
2287 case GE_EXPR:
2288 return COMPCODE_GE;
2289 case ORDERED_EXPR:
2290 return COMPCODE_ORD;
2291 case UNORDERED_EXPR:
2292 return COMPCODE_UNORD;
2293 case UNLT_EXPR:
2294 return COMPCODE_UNLT;
2295 case UNEQ_EXPR:
2296 return COMPCODE_UNEQ;
2297 case UNLE_EXPR:
2298 return COMPCODE_UNLE;
2299 case UNGT_EXPR:
2300 return COMPCODE_UNGT;
2301 case LTGT_EXPR:
2302 return COMPCODE_LTGT;
2303 case UNGE_EXPR:
2304 return COMPCODE_UNGE;
2305 default:
2306 gcc_unreachable ();
2310 /* Convert a compcode bit-based encoding of a comparison operator back
2311 to GCC's enum tree_code representation. This function is the
2312 inverse of comparison_to_compcode. */
2314 static enum tree_code
2315 compcode_to_comparison (enum comparison_code code)
2317 switch (code)
2319 case COMPCODE_LT:
2320 return LT_EXPR;
2321 case COMPCODE_EQ:
2322 return EQ_EXPR;
2323 case COMPCODE_LE:
2324 return LE_EXPR;
2325 case COMPCODE_GT:
2326 return GT_EXPR;
2327 case COMPCODE_NE:
2328 return NE_EXPR;
2329 case COMPCODE_GE:
2330 return GE_EXPR;
2331 case COMPCODE_ORD:
2332 return ORDERED_EXPR;
2333 case COMPCODE_UNORD:
2334 return UNORDERED_EXPR;
2335 case COMPCODE_UNLT:
2336 return UNLT_EXPR;
2337 case COMPCODE_UNEQ:
2338 return UNEQ_EXPR;
2339 case COMPCODE_UNLE:
2340 return UNLE_EXPR;
2341 case COMPCODE_UNGT:
2342 return UNGT_EXPR;
2343 case COMPCODE_LTGT:
2344 return LTGT_EXPR;
2345 case COMPCODE_UNGE:
2346 return UNGE_EXPR;
2347 default:
2348 gcc_unreachable ();
2352 /* Return a tree for the comparison which is the combination of
2353 doing the AND or OR (depending on CODE) of the two operations LCODE
2354 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2355 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2356 if this makes the transformation invalid. */
2358 tree
2359 combine_comparisons (enum tree_code code, enum tree_code lcode,
2360 enum tree_code rcode, tree truth_type,
2361 tree ll_arg, tree lr_arg)
2363 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2364 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2365 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2366 enum comparison_code compcode;
2368 switch (code)
2370 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2371 compcode = lcompcode & rcompcode;
2372 break;
2374 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2375 compcode = lcompcode | rcompcode;
2376 break;
2378 default:
2379 return NULL_TREE;
2382 if (!honor_nans)
2384 /* Eliminate unordered comparisons, as well as LTGT and ORD
2385 which are not used unless the mode has NaNs. */
2386 compcode &= ~COMPCODE_UNORD;
2387 if (compcode == COMPCODE_LTGT)
2388 compcode = COMPCODE_NE;
2389 else if (compcode == COMPCODE_ORD)
2390 compcode = COMPCODE_TRUE;
2392 else if (flag_trapping_math)
2394 /* Check that the original operation and the optimized ones will trap
2395 under the same condition. */
2396 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2397 && (lcompcode != COMPCODE_EQ)
2398 && (lcompcode != COMPCODE_ORD);
2399 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2400 && (rcompcode != COMPCODE_EQ)
2401 && (rcompcode != COMPCODE_ORD);
2402 bool trap = (compcode & COMPCODE_UNORD) == 0
2403 && (compcode != COMPCODE_EQ)
2404 && (compcode != COMPCODE_ORD);
2406 /* In a short-circuited boolean expression the LHS might be
2407 such that the RHS, if evaluated, will never trap. For
2408 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2409 if neither x nor y is NaN. (This is a mixed blessing: for
2410 example, the expression above will never trap, hence
2411 optimizing it to x < y would be invalid). */
2412 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2413 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2414 rtrap = false;
2416 /* If the comparison was short-circuited, and only the RHS
2417 trapped, we may now generate a spurious trap. */
2418 if (rtrap && !ltrap
2419 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2420 return NULL_TREE;
2422 /* If we changed the conditions that cause a trap, we lose. */
2423 if ((ltrap || rtrap) != trap)
2424 return NULL_TREE;
2427 if (compcode == COMPCODE_TRUE)
2428 return constant_boolean_node (true, truth_type);
2429 else if (compcode == COMPCODE_FALSE)
2430 return constant_boolean_node (false, truth_type);
2431 else
2432 return fold_build2 (compcode_to_comparison (compcode),
2433 truth_type, ll_arg, lr_arg);
2436 /* Return nonzero if CODE is a tree code that represents a truth value. */
2438 static int
2439 truth_value_p (enum tree_code code)
2441 return (TREE_CODE_CLASS (code) == tcc_comparison
2442 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2443 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2444 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2447 /* Return nonzero if two operands (typically of the same tree node)
2448 are necessarily equal. If either argument has side-effects this
2449 function returns zero. FLAGS modifies behavior as follows:
2451 If OEP_ONLY_CONST is set, only return nonzero for constants.
2452 This function tests whether the operands are indistinguishable;
2453 it does not test whether they are equal using C's == operation.
2454 The distinction is important for IEEE floating point, because
2455 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2456 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2458 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2459 even though it may hold multiple values during a function.
2460 This is because a GCC tree node guarantees that nothing else is
2461 executed between the evaluation of its "operands" (which may often
2462 be evaluated in arbitrary order). Hence if the operands themselves
2463 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2464 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2465 unset means assuming isochronic (or instantaneous) tree equivalence.
2466 Unless comparing arbitrary expression trees, such as from different
2467 statements, this flag can usually be left unset.
2469 If OEP_PURE_SAME is set, then pure functions with identical arguments
2470 are considered the same. It is used when the caller has other ways
2471 to ensure that global memory is unchanged in between. */
2474 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2476 /* If either is ERROR_MARK, they aren't equal. */
2477 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2478 return 0;
2480 /* If both types don't have the same signedness, then we can't consider
2481 them equal. We must check this before the STRIP_NOPS calls
2482 because they may change the signedness of the arguments. */
2483 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2484 return 0;
2486 STRIP_NOPS (arg0);
2487 STRIP_NOPS (arg1);
2489 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2490 /* This is needed for conversions and for COMPONENT_REF.
2491 Might as well play it safe and always test this. */
2492 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2493 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2494 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2495 return 0;
2497 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2498 We don't care about side effects in that case because the SAVE_EXPR
2499 takes care of that for us. In all other cases, two expressions are
2500 equal if they have no side effects. If we have two identical
2501 expressions with side effects that should be treated the same due
2502 to the only side effects being identical SAVE_EXPR's, that will
2503 be detected in the recursive calls below. */
2504 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2505 && (TREE_CODE (arg0) == SAVE_EXPR
2506 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2507 return 1;
2509 /* Next handle constant cases, those for which we can return 1 even
2510 if ONLY_CONST is set. */
2511 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2512 switch (TREE_CODE (arg0))
2514 case INTEGER_CST:
2515 return (! TREE_CONSTANT_OVERFLOW (arg0)
2516 && ! TREE_CONSTANT_OVERFLOW (arg1)
2517 && tree_int_cst_equal (arg0, arg1));
2519 case REAL_CST:
2520 return (! TREE_CONSTANT_OVERFLOW (arg0)
2521 && ! TREE_CONSTANT_OVERFLOW (arg1)
2522 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2523 TREE_REAL_CST (arg1)));
2525 case VECTOR_CST:
2527 tree v1, v2;
2529 if (TREE_CONSTANT_OVERFLOW (arg0)
2530 || TREE_CONSTANT_OVERFLOW (arg1))
2531 return 0;
2533 v1 = TREE_VECTOR_CST_ELTS (arg0);
2534 v2 = TREE_VECTOR_CST_ELTS (arg1);
2535 while (v1 && v2)
2537 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2538 flags))
2539 return 0;
2540 v1 = TREE_CHAIN (v1);
2541 v2 = TREE_CHAIN (v2);
2544 return v1 == v2;
2547 case COMPLEX_CST:
2548 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2549 flags)
2550 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2551 flags));
2553 case STRING_CST:
2554 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2555 && ! memcmp (TREE_STRING_POINTER (arg0),
2556 TREE_STRING_POINTER (arg1),
2557 TREE_STRING_LENGTH (arg0)));
2559 case ADDR_EXPR:
2560 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2562 default:
2563 break;
2566 if (flags & OEP_ONLY_CONST)
2567 return 0;
2569 /* Define macros to test an operand from arg0 and arg1 for equality and a
2570 variant that allows null and views null as being different from any
2571 non-null value. In the latter case, if either is null, the both
2572 must be; otherwise, do the normal comparison. */
2573 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2574 TREE_OPERAND (arg1, N), flags)
2576 #define OP_SAME_WITH_NULL(N) \
2577 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2578 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2580 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2582 case tcc_unary:
2583 /* Two conversions are equal only if signedness and modes match. */
2584 switch (TREE_CODE (arg0))
2586 case NOP_EXPR:
2587 case CONVERT_EXPR:
2588 case FIX_CEIL_EXPR:
2589 case FIX_TRUNC_EXPR:
2590 case FIX_FLOOR_EXPR:
2591 case FIX_ROUND_EXPR:
2592 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2593 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2594 return 0;
2595 break;
2596 default:
2597 break;
2600 return OP_SAME (0);
2603 case tcc_comparison:
2604 case tcc_binary:
2605 if (OP_SAME (0) && OP_SAME (1))
2606 return 1;
2608 /* For commutative ops, allow the other order. */
2609 return (commutative_tree_code (TREE_CODE (arg0))
2610 && operand_equal_p (TREE_OPERAND (arg0, 0),
2611 TREE_OPERAND (arg1, 1), flags)
2612 && operand_equal_p (TREE_OPERAND (arg0, 1),
2613 TREE_OPERAND (arg1, 0), flags));
2615 case tcc_reference:
2616 /* If either of the pointer (or reference) expressions we are
2617 dereferencing contain a side effect, these cannot be equal. */
2618 if (TREE_SIDE_EFFECTS (arg0)
2619 || TREE_SIDE_EFFECTS (arg1))
2620 return 0;
2622 switch (TREE_CODE (arg0))
2624 case INDIRECT_REF:
2625 case ALIGN_INDIRECT_REF:
2626 case MISALIGNED_INDIRECT_REF:
2627 case REALPART_EXPR:
2628 case IMAGPART_EXPR:
2629 return OP_SAME (0);
2631 case ARRAY_REF:
2632 case ARRAY_RANGE_REF:
2633 /* Operands 2 and 3 may be null. */
2634 return (OP_SAME (0)
2635 && OP_SAME (1)
2636 && OP_SAME_WITH_NULL (2)
2637 && OP_SAME_WITH_NULL (3));
2639 case COMPONENT_REF:
2640 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2641 may be NULL when we're called to compare MEM_EXPRs. */
2642 return OP_SAME_WITH_NULL (0)
2643 && OP_SAME (1)
2644 && OP_SAME_WITH_NULL (2);
2646 case BIT_FIELD_REF:
2647 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2649 default:
2650 return 0;
2653 case tcc_expression:
2654 switch (TREE_CODE (arg0))
2656 case ADDR_EXPR:
2657 case TRUTH_NOT_EXPR:
2658 return OP_SAME (0);
2660 case TRUTH_ANDIF_EXPR:
2661 case TRUTH_ORIF_EXPR:
2662 return OP_SAME (0) && OP_SAME (1);
2664 case TRUTH_AND_EXPR:
2665 case TRUTH_OR_EXPR:
2666 case TRUTH_XOR_EXPR:
2667 if (OP_SAME (0) && OP_SAME (1))
2668 return 1;
2670 /* Otherwise take into account this is a commutative operation. */
2671 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2672 TREE_OPERAND (arg1, 1), flags)
2673 && operand_equal_p (TREE_OPERAND (arg0, 1),
2674 TREE_OPERAND (arg1, 0), flags));
2676 case CALL_EXPR:
2677 /* If the CALL_EXPRs call different functions, then they
2678 clearly can not be equal. */
2679 if (!OP_SAME (0))
2680 return 0;
2683 unsigned int cef = call_expr_flags (arg0);
2684 if (flags & OEP_PURE_SAME)
2685 cef &= ECF_CONST | ECF_PURE;
2686 else
2687 cef &= ECF_CONST;
2688 if (!cef)
2689 return 0;
2692 /* Now see if all the arguments are the same. operand_equal_p
2693 does not handle TREE_LIST, so we walk the operands here
2694 feeding them to operand_equal_p. */
2695 arg0 = TREE_OPERAND (arg0, 1);
2696 arg1 = TREE_OPERAND (arg1, 1);
2697 while (arg0 && arg1)
2699 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2700 flags))
2701 return 0;
2703 arg0 = TREE_CHAIN (arg0);
2704 arg1 = TREE_CHAIN (arg1);
2707 /* If we get here and both argument lists are exhausted
2708 then the CALL_EXPRs are equal. */
2709 return ! (arg0 || arg1);
2711 default:
2712 return 0;
2715 case tcc_declaration:
2716 /* Consider __builtin_sqrt equal to sqrt. */
2717 return (TREE_CODE (arg0) == FUNCTION_DECL
2718 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2719 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2720 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2722 default:
2723 return 0;
2726 #undef OP_SAME
2727 #undef OP_SAME_WITH_NULL
2730 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2731 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2733 When in doubt, return 0. */
2735 static int
2736 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2738 int unsignedp1, unsignedpo;
2739 tree primarg0, primarg1, primother;
2740 unsigned int correct_width;
2742 if (operand_equal_p (arg0, arg1, 0))
2743 return 1;
2745 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2746 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2747 return 0;
2749 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2750 and see if the inner values are the same. This removes any
2751 signedness comparison, which doesn't matter here. */
2752 primarg0 = arg0, primarg1 = arg1;
2753 STRIP_NOPS (primarg0);
2754 STRIP_NOPS (primarg1);
2755 if (operand_equal_p (primarg0, primarg1, 0))
2756 return 1;
2758 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2759 actual comparison operand, ARG0.
2761 First throw away any conversions to wider types
2762 already present in the operands. */
2764 primarg1 = get_narrower (arg1, &unsignedp1);
2765 primother = get_narrower (other, &unsignedpo);
2767 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2768 if (unsignedp1 == unsignedpo
2769 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2770 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2772 tree type = TREE_TYPE (arg0);
2774 /* Make sure shorter operand is extended the right way
2775 to match the longer operand. */
2776 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2777 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2779 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2780 return 1;
2783 return 0;
2786 /* See if ARG is an expression that is either a comparison or is performing
2787 arithmetic on comparisons. The comparisons must only be comparing
2788 two different values, which will be stored in *CVAL1 and *CVAL2; if
2789 they are nonzero it means that some operands have already been found.
2790 No variables may be used anywhere else in the expression except in the
2791 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2792 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2794 If this is true, return 1. Otherwise, return zero. */
2796 static int
2797 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2799 enum tree_code code = TREE_CODE (arg);
2800 enum tree_code_class class = TREE_CODE_CLASS (code);
2802 /* We can handle some of the tcc_expression cases here. */
2803 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2804 class = tcc_unary;
2805 else if (class == tcc_expression
2806 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2807 || code == COMPOUND_EXPR))
2808 class = tcc_binary;
2810 else if (class == tcc_expression && code == SAVE_EXPR
2811 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2813 /* If we've already found a CVAL1 or CVAL2, this expression is
2814 two complex to handle. */
2815 if (*cval1 || *cval2)
2816 return 0;
2818 class = tcc_unary;
2819 *save_p = 1;
2822 switch (class)
2824 case tcc_unary:
2825 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2827 case tcc_binary:
2828 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2829 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2830 cval1, cval2, save_p));
2832 case tcc_constant:
2833 return 1;
2835 case tcc_expression:
2836 if (code == COND_EXPR)
2837 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2838 cval1, cval2, save_p)
2839 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2840 cval1, cval2, save_p)
2841 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2842 cval1, cval2, save_p));
2843 return 0;
2845 case tcc_comparison:
2846 /* First see if we can handle the first operand, then the second. For
2847 the second operand, we know *CVAL1 can't be zero. It must be that
2848 one side of the comparison is each of the values; test for the
2849 case where this isn't true by failing if the two operands
2850 are the same. */
2852 if (operand_equal_p (TREE_OPERAND (arg, 0),
2853 TREE_OPERAND (arg, 1), 0))
2854 return 0;
2856 if (*cval1 == 0)
2857 *cval1 = TREE_OPERAND (arg, 0);
2858 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2860 else if (*cval2 == 0)
2861 *cval2 = TREE_OPERAND (arg, 0);
2862 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2864 else
2865 return 0;
2867 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2869 else if (*cval2 == 0)
2870 *cval2 = TREE_OPERAND (arg, 1);
2871 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2873 else
2874 return 0;
2876 return 1;
2878 default:
2879 return 0;
2883 /* ARG is a tree that is known to contain just arithmetic operations and
2884 comparisons. Evaluate the operations in the tree substituting NEW0 for
2885 any occurrence of OLD0 as an operand of a comparison and likewise for
2886 NEW1 and OLD1. */
2888 static tree
2889 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2891 tree type = TREE_TYPE (arg);
2892 enum tree_code code = TREE_CODE (arg);
2893 enum tree_code_class class = TREE_CODE_CLASS (code);
2895 /* We can handle some of the tcc_expression cases here. */
2896 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2897 class = tcc_unary;
2898 else if (class == tcc_expression
2899 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2900 class = tcc_binary;
2902 switch (class)
2904 case tcc_unary:
2905 return fold_build1 (code, type,
2906 eval_subst (TREE_OPERAND (arg, 0),
2907 old0, new0, old1, new1));
2909 case tcc_binary:
2910 return fold_build2 (code, type,
2911 eval_subst (TREE_OPERAND (arg, 0),
2912 old0, new0, old1, new1),
2913 eval_subst (TREE_OPERAND (arg, 1),
2914 old0, new0, old1, new1));
2916 case tcc_expression:
2917 switch (code)
2919 case SAVE_EXPR:
2920 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2922 case COMPOUND_EXPR:
2923 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2925 case COND_EXPR:
2926 return fold_build3 (code, type,
2927 eval_subst (TREE_OPERAND (arg, 0),
2928 old0, new0, old1, new1),
2929 eval_subst (TREE_OPERAND (arg, 1),
2930 old0, new0, old1, new1),
2931 eval_subst (TREE_OPERAND (arg, 2),
2932 old0, new0, old1, new1));
2933 default:
2934 break;
2936 /* Fall through - ??? */
2938 case tcc_comparison:
2940 tree arg0 = TREE_OPERAND (arg, 0);
2941 tree arg1 = TREE_OPERAND (arg, 1);
2943 /* We need to check both for exact equality and tree equality. The
2944 former will be true if the operand has a side-effect. In that
2945 case, we know the operand occurred exactly once. */
2947 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2948 arg0 = new0;
2949 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2950 arg0 = new1;
2952 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2953 arg1 = new0;
2954 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2955 arg1 = new1;
2957 return fold_build2 (code, type, arg0, arg1);
2960 default:
2961 return arg;
2965 /* Return a tree for the case when the result of an expression is RESULT
2966 converted to TYPE and OMITTED was previously an operand of the expression
2967 but is now not needed (e.g., we folded OMITTED * 0).
2969 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2970 the conversion of RESULT to TYPE. */
2972 tree
2973 omit_one_operand (tree type, tree result, tree omitted)
2975 tree t = fold_convert (type, result);
2977 if (TREE_SIDE_EFFECTS (omitted))
2978 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2980 return non_lvalue (t);
2983 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2985 static tree
2986 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2988 tree t = fold_convert (type, result);
2990 if (TREE_SIDE_EFFECTS (omitted))
2991 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2993 return pedantic_non_lvalue (t);
2996 /* Return a tree for the case when the result of an expression is RESULT
2997 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
2998 of the expression but are now not needed.
3000 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3001 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3002 evaluated before OMITTED2. Otherwise, if neither has side effects,
3003 just do the conversion of RESULT to TYPE. */
3005 tree
3006 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3008 tree t = fold_convert (type, result);
3010 if (TREE_SIDE_EFFECTS (omitted2))
3011 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3012 if (TREE_SIDE_EFFECTS (omitted1))
3013 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3015 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3019 /* Return a simplified tree node for the truth-negation of ARG. This
3020 never alters ARG itself. We assume that ARG is an operation that
3021 returns a truth value (0 or 1).
3023 FIXME: one would think we would fold the result, but it causes
3024 problems with the dominator optimizer. */
3025 tree
3026 invert_truthvalue (tree arg)
3028 tree type = TREE_TYPE (arg);
3029 enum tree_code code = TREE_CODE (arg);
3031 if (code == ERROR_MARK)
3032 return arg;
3034 /* If this is a comparison, we can simply invert it, except for
3035 floating-point non-equality comparisons, in which case we just
3036 enclose a TRUTH_NOT_EXPR around what we have. */
3038 if (TREE_CODE_CLASS (code) == tcc_comparison)
3040 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3041 if (FLOAT_TYPE_P (op_type)
3042 && flag_trapping_math
3043 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3044 && code != NE_EXPR && code != EQ_EXPR)
3045 return build1 (TRUTH_NOT_EXPR, type, arg);
3046 else
3048 code = invert_tree_comparison (code,
3049 HONOR_NANS (TYPE_MODE (op_type)));
3050 if (code == ERROR_MARK)
3051 return build1 (TRUTH_NOT_EXPR, type, arg);
3052 else
3053 return build2 (code, type,
3054 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3058 switch (code)
3060 case INTEGER_CST:
3061 return constant_boolean_node (integer_zerop (arg), type);
3063 case TRUTH_AND_EXPR:
3064 return build2 (TRUTH_OR_EXPR, type,
3065 invert_truthvalue (TREE_OPERAND (arg, 0)),
3066 invert_truthvalue (TREE_OPERAND (arg, 1)));
3068 case TRUTH_OR_EXPR:
3069 return build2 (TRUTH_AND_EXPR, type,
3070 invert_truthvalue (TREE_OPERAND (arg, 0)),
3071 invert_truthvalue (TREE_OPERAND (arg, 1)));
3073 case TRUTH_XOR_EXPR:
3074 /* Here we can invert either operand. We invert the first operand
3075 unless the second operand is a TRUTH_NOT_EXPR in which case our
3076 result is the XOR of the first operand with the inside of the
3077 negation of the second operand. */
3079 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3080 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3081 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3082 else
3083 return build2 (TRUTH_XOR_EXPR, type,
3084 invert_truthvalue (TREE_OPERAND (arg, 0)),
3085 TREE_OPERAND (arg, 1));
3087 case TRUTH_ANDIF_EXPR:
3088 return build2 (TRUTH_ORIF_EXPR, type,
3089 invert_truthvalue (TREE_OPERAND (arg, 0)),
3090 invert_truthvalue (TREE_OPERAND (arg, 1)));
3092 case TRUTH_ORIF_EXPR:
3093 return build2 (TRUTH_ANDIF_EXPR, type,
3094 invert_truthvalue (TREE_OPERAND (arg, 0)),
3095 invert_truthvalue (TREE_OPERAND (arg, 1)));
3097 case TRUTH_NOT_EXPR:
3098 return TREE_OPERAND (arg, 0);
3100 case COND_EXPR:
3102 tree arg1 = TREE_OPERAND (arg, 1);
3103 tree arg2 = TREE_OPERAND (arg, 2);
3104 /* A COND_EXPR may have a throw as one operand, which
3105 then has void type. Just leave void operands
3106 as they are. */
3107 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3108 VOID_TYPE_P (TREE_TYPE (arg1))
3109 ? arg1 : invert_truthvalue (arg1),
3110 VOID_TYPE_P (TREE_TYPE (arg2))
3111 ? arg2 : invert_truthvalue (arg2));
3114 case COMPOUND_EXPR:
3115 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3116 invert_truthvalue (TREE_OPERAND (arg, 1)));
3118 case NON_LVALUE_EXPR:
3119 return invert_truthvalue (TREE_OPERAND (arg, 0));
3121 case NOP_EXPR:
3122 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3123 break;
3125 case CONVERT_EXPR:
3126 case FLOAT_EXPR:
3127 return build1 (TREE_CODE (arg), type,
3128 invert_truthvalue (TREE_OPERAND (arg, 0)));
3130 case BIT_AND_EXPR:
3131 if (!integer_onep (TREE_OPERAND (arg, 1)))
3132 break;
3133 return build2 (EQ_EXPR, type, arg,
3134 build_int_cst (type, 0));
3136 case SAVE_EXPR:
3137 return build1 (TRUTH_NOT_EXPR, type, arg);
3139 case CLEANUP_POINT_EXPR:
3140 return build1 (CLEANUP_POINT_EXPR, type,
3141 invert_truthvalue (TREE_OPERAND (arg, 0)));
3143 default:
3144 break;
3146 gcc_assert (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE);
3147 return build1 (TRUTH_NOT_EXPR, type, arg);
3150 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3151 operands are another bit-wise operation with a common input. If so,
3152 distribute the bit operations to save an operation and possibly two if
3153 constants are involved. For example, convert
3154 (A | B) & (A | C) into A | (B & C)
3155 Further simplification will occur if B and C are constants.
3157 If this optimization cannot be done, 0 will be returned. */
3159 static tree
3160 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3162 tree common;
3163 tree left, right;
3165 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3166 || TREE_CODE (arg0) == code
3167 || (TREE_CODE (arg0) != BIT_AND_EXPR
3168 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3169 return 0;
3171 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3173 common = TREE_OPERAND (arg0, 0);
3174 left = TREE_OPERAND (arg0, 1);
3175 right = TREE_OPERAND (arg1, 1);
3177 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3179 common = TREE_OPERAND (arg0, 0);
3180 left = TREE_OPERAND (arg0, 1);
3181 right = TREE_OPERAND (arg1, 0);
3183 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3185 common = TREE_OPERAND (arg0, 1);
3186 left = TREE_OPERAND (arg0, 0);
3187 right = TREE_OPERAND (arg1, 1);
3189 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3191 common = TREE_OPERAND (arg0, 1);
3192 left = TREE_OPERAND (arg0, 0);
3193 right = TREE_OPERAND (arg1, 0);
3195 else
3196 return 0;
3198 return fold_build2 (TREE_CODE (arg0), type, common,
3199 fold_build2 (code, type, left, right));
3202 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3203 with code CODE. This optimization is unsafe. */
3204 static tree
3205 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3207 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3208 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3210 /* (A / C) +- (B / C) -> (A +- B) / C. */
3211 if (mul0 == mul1
3212 && operand_equal_p (TREE_OPERAND (arg0, 1),
3213 TREE_OPERAND (arg1, 1), 0))
3214 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3215 fold_build2 (code, type,
3216 TREE_OPERAND (arg0, 0),
3217 TREE_OPERAND (arg1, 0)),
3218 TREE_OPERAND (arg0, 1));
3220 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3221 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3222 TREE_OPERAND (arg1, 0), 0)
3223 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3224 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3226 REAL_VALUE_TYPE r0, r1;
3227 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3228 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3229 if (!mul0)
3230 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3231 if (!mul1)
3232 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3233 real_arithmetic (&r0, code, &r0, &r1);
3234 return fold_build2 (MULT_EXPR, type,
3235 TREE_OPERAND (arg0, 0),
3236 build_real (type, r0));
3239 return NULL_TREE;
3242 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3243 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3245 static tree
3246 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3247 int unsignedp)
3249 tree result;
3251 if (bitpos == 0)
3253 tree size = TYPE_SIZE (TREE_TYPE (inner));
3254 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3255 || POINTER_TYPE_P (TREE_TYPE (inner)))
3256 && host_integerp (size, 0)
3257 && tree_low_cst (size, 0) == bitsize)
3258 return fold_convert (type, inner);
3261 result = build3 (BIT_FIELD_REF, type, inner,
3262 size_int (bitsize), bitsize_int (bitpos));
3264 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3266 return result;
3269 /* Optimize a bit-field compare.
3271 There are two cases: First is a compare against a constant and the
3272 second is a comparison of two items where the fields are at the same
3273 bit position relative to the start of a chunk (byte, halfword, word)
3274 large enough to contain it. In these cases we can avoid the shift
3275 implicit in bitfield extractions.
3277 For constants, we emit a compare of the shifted constant with the
3278 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3279 compared. For two fields at the same position, we do the ANDs with the
3280 similar mask and compare the result of the ANDs.
3282 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3283 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3284 are the left and right operands of the comparison, respectively.
3286 If the optimization described above can be done, we return the resulting
3287 tree. Otherwise we return zero. */
3289 static tree
3290 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3291 tree lhs, tree rhs)
3293 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3294 tree type = TREE_TYPE (lhs);
3295 tree signed_type, unsigned_type;
3296 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3297 enum machine_mode lmode, rmode, nmode;
3298 int lunsignedp, runsignedp;
3299 int lvolatilep = 0, rvolatilep = 0;
3300 tree linner, rinner = NULL_TREE;
3301 tree mask;
3302 tree offset;
3304 /* Get all the information about the extractions being done. If the bit size
3305 if the same as the size of the underlying object, we aren't doing an
3306 extraction at all and so can do nothing. We also don't want to
3307 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3308 then will no longer be able to replace it. */
3309 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3310 &lunsignedp, &lvolatilep, false);
3311 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3312 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3313 return 0;
3315 if (!const_p)
3317 /* If this is not a constant, we can only do something if bit positions,
3318 sizes, and signedness are the same. */
3319 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3320 &runsignedp, &rvolatilep, false);
3322 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3323 || lunsignedp != runsignedp || offset != 0
3324 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3325 return 0;
3328 /* See if we can find a mode to refer to this field. We should be able to,
3329 but fail if we can't. */
3330 nmode = get_best_mode (lbitsize, lbitpos,
3331 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3332 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3333 TYPE_ALIGN (TREE_TYPE (rinner))),
3334 word_mode, lvolatilep || rvolatilep);
3335 if (nmode == VOIDmode)
3336 return 0;
3338 /* Set signed and unsigned types of the precision of this mode for the
3339 shifts below. */
3340 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3341 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3343 /* Compute the bit position and size for the new reference and our offset
3344 within it. If the new reference is the same size as the original, we
3345 won't optimize anything, so return zero. */
3346 nbitsize = GET_MODE_BITSIZE (nmode);
3347 nbitpos = lbitpos & ~ (nbitsize - 1);
3348 lbitpos -= nbitpos;
3349 if (nbitsize == lbitsize)
3350 return 0;
3352 if (BYTES_BIG_ENDIAN)
3353 lbitpos = nbitsize - lbitsize - lbitpos;
3355 /* Make the mask to be used against the extracted field. */
3356 mask = build_int_cst (unsigned_type, -1);
3357 mask = force_fit_type (mask, 0, false, false);
3358 mask = fold_convert (unsigned_type, mask);
3359 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3360 mask = const_binop (RSHIFT_EXPR, mask,
3361 size_int (nbitsize - lbitsize - lbitpos), 0);
3363 if (! const_p)
3364 /* If not comparing with constant, just rework the comparison
3365 and return. */
3366 return build2 (code, compare_type,
3367 build2 (BIT_AND_EXPR, unsigned_type,
3368 make_bit_field_ref (linner, unsigned_type,
3369 nbitsize, nbitpos, 1),
3370 mask),
3371 build2 (BIT_AND_EXPR, unsigned_type,
3372 make_bit_field_ref (rinner, unsigned_type,
3373 nbitsize, nbitpos, 1),
3374 mask));
3376 /* Otherwise, we are handling the constant case. See if the constant is too
3377 big for the field. Warn and return a tree of for 0 (false) if so. We do
3378 this not only for its own sake, but to avoid having to test for this
3379 error case below. If we didn't, we might generate wrong code.
3381 For unsigned fields, the constant shifted right by the field length should
3382 be all zero. For signed fields, the high-order bits should agree with
3383 the sign bit. */
3385 if (lunsignedp)
3387 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3388 fold_convert (unsigned_type, rhs),
3389 size_int (lbitsize), 0)))
3391 warning (0, "comparison is always %d due to width of bit-field",
3392 code == NE_EXPR);
3393 return constant_boolean_node (code == NE_EXPR, compare_type);
3396 else
3398 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3399 size_int (lbitsize - 1), 0);
3400 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3402 warning (0, "comparison is always %d due to width of bit-field",
3403 code == NE_EXPR);
3404 return constant_boolean_node (code == NE_EXPR, compare_type);
3408 /* Single-bit compares should always be against zero. */
3409 if (lbitsize == 1 && ! integer_zerop (rhs))
3411 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3412 rhs = build_int_cst (type, 0);
3415 /* Make a new bitfield reference, shift the constant over the
3416 appropriate number of bits and mask it with the computed mask
3417 (in case this was a signed field). If we changed it, make a new one. */
3418 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3419 if (lvolatilep)
3421 TREE_SIDE_EFFECTS (lhs) = 1;
3422 TREE_THIS_VOLATILE (lhs) = 1;
3425 rhs = const_binop (BIT_AND_EXPR,
3426 const_binop (LSHIFT_EXPR,
3427 fold_convert (unsigned_type, rhs),
3428 size_int (lbitpos), 0),
3429 mask, 0);
3431 return build2 (code, compare_type,
3432 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3433 rhs);
3436 /* Subroutine for fold_truthop: decode a field reference.
3438 If EXP is a comparison reference, we return the innermost reference.
3440 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3441 set to the starting bit number.
3443 If the innermost field can be completely contained in a mode-sized
3444 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3446 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3447 otherwise it is not changed.
3449 *PUNSIGNEDP is set to the signedness of the field.
3451 *PMASK is set to the mask used. This is either contained in a
3452 BIT_AND_EXPR or derived from the width of the field.
3454 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3456 Return 0 if this is not a component reference or is one that we can't
3457 do anything with. */
3459 static tree
3460 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3461 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3462 int *punsignedp, int *pvolatilep,
3463 tree *pmask, tree *pand_mask)
3465 tree outer_type = 0;
3466 tree and_mask = 0;
3467 tree mask, inner, offset;
3468 tree unsigned_type;
3469 unsigned int precision;
3471 /* All the optimizations using this function assume integer fields.
3472 There are problems with FP fields since the type_for_size call
3473 below can fail for, e.g., XFmode. */
3474 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3475 return 0;
3477 /* We are interested in the bare arrangement of bits, so strip everything
3478 that doesn't affect the machine mode. However, record the type of the
3479 outermost expression if it may matter below. */
3480 if (TREE_CODE (exp) == NOP_EXPR
3481 || TREE_CODE (exp) == CONVERT_EXPR
3482 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3483 outer_type = TREE_TYPE (exp);
3484 STRIP_NOPS (exp);
3486 if (TREE_CODE (exp) == BIT_AND_EXPR)
3488 and_mask = TREE_OPERAND (exp, 1);
3489 exp = TREE_OPERAND (exp, 0);
3490 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3491 if (TREE_CODE (and_mask) != INTEGER_CST)
3492 return 0;
3495 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3496 punsignedp, pvolatilep, false);
3497 if ((inner == exp && and_mask == 0)
3498 || *pbitsize < 0 || offset != 0
3499 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3500 return 0;
3502 /* If the number of bits in the reference is the same as the bitsize of
3503 the outer type, then the outer type gives the signedness. Otherwise
3504 (in case of a small bitfield) the signedness is unchanged. */
3505 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3506 *punsignedp = TYPE_UNSIGNED (outer_type);
3508 /* Compute the mask to access the bitfield. */
3509 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3510 precision = TYPE_PRECISION (unsigned_type);
3512 mask = build_int_cst (unsigned_type, -1);
3513 mask = force_fit_type (mask, 0, false, false);
3515 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3516 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3518 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3519 if (and_mask != 0)
3520 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3521 fold_convert (unsigned_type, and_mask), mask);
3523 *pmask = mask;
3524 *pand_mask = and_mask;
3525 return inner;
3528 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3529 bit positions. */
3531 static int
3532 all_ones_mask_p (tree mask, int size)
3534 tree type = TREE_TYPE (mask);
3535 unsigned int precision = TYPE_PRECISION (type);
3536 tree tmask;
3538 tmask = build_int_cst (lang_hooks.types.signed_type (type), -1);
3539 tmask = force_fit_type (tmask, 0, false, false);
3541 return
3542 tree_int_cst_equal (mask,
3543 const_binop (RSHIFT_EXPR,
3544 const_binop (LSHIFT_EXPR, tmask,
3545 size_int (precision - size),
3547 size_int (precision - size), 0));
3550 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3551 represents the sign bit of EXP's type. If EXP represents a sign
3552 or zero extension, also test VAL against the unextended type.
3553 The return value is the (sub)expression whose sign bit is VAL,
3554 or NULL_TREE otherwise. */
3556 static tree
3557 sign_bit_p (tree exp, tree val)
3559 unsigned HOST_WIDE_INT mask_lo, lo;
3560 HOST_WIDE_INT mask_hi, hi;
3561 int width;
3562 tree t;
3564 /* Tree EXP must have an integral type. */
3565 t = TREE_TYPE (exp);
3566 if (! INTEGRAL_TYPE_P (t))
3567 return NULL_TREE;
3569 /* Tree VAL must be an integer constant. */
3570 if (TREE_CODE (val) != INTEGER_CST
3571 || TREE_CONSTANT_OVERFLOW (val))
3572 return NULL_TREE;
3574 width = TYPE_PRECISION (t);
3575 if (width > HOST_BITS_PER_WIDE_INT)
3577 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3578 lo = 0;
3580 mask_hi = ((unsigned HOST_WIDE_INT) -1
3581 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3582 mask_lo = -1;
3584 else
3586 hi = 0;
3587 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3589 mask_hi = 0;
3590 mask_lo = ((unsigned HOST_WIDE_INT) -1
3591 >> (HOST_BITS_PER_WIDE_INT - width));
3594 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3595 treat VAL as if it were unsigned. */
3596 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3597 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3598 return exp;
3600 /* Handle extension from a narrower type. */
3601 if (TREE_CODE (exp) == NOP_EXPR
3602 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3603 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3605 return NULL_TREE;
3608 /* Subroutine for fold_truthop: determine if an operand is simple enough
3609 to be evaluated unconditionally. */
3611 static int
3612 simple_operand_p (tree exp)
3614 /* Strip any conversions that don't change the machine mode. */
3615 STRIP_NOPS (exp);
3617 return (CONSTANT_CLASS_P (exp)
3618 || TREE_CODE (exp) == SSA_NAME
3619 || (DECL_P (exp)
3620 && ! TREE_ADDRESSABLE (exp)
3621 && ! TREE_THIS_VOLATILE (exp)
3622 && ! DECL_NONLOCAL (exp)
3623 /* Don't regard global variables as simple. They may be
3624 allocated in ways unknown to the compiler (shared memory,
3625 #pragma weak, etc). */
3626 && ! TREE_PUBLIC (exp)
3627 && ! DECL_EXTERNAL (exp)
3628 /* Loading a static variable is unduly expensive, but global
3629 registers aren't expensive. */
3630 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3633 /* The following functions are subroutines to fold_range_test and allow it to
3634 try to change a logical combination of comparisons into a range test.
3636 For example, both
3637 X == 2 || X == 3 || X == 4 || X == 5
3639 X >= 2 && X <= 5
3640 are converted to
3641 (unsigned) (X - 2) <= 3
3643 We describe each set of comparisons as being either inside or outside
3644 a range, using a variable named like IN_P, and then describe the
3645 range with a lower and upper bound. If one of the bounds is omitted,
3646 it represents either the highest or lowest value of the type.
3648 In the comments below, we represent a range by two numbers in brackets
3649 preceded by a "+" to designate being inside that range, or a "-" to
3650 designate being outside that range, so the condition can be inverted by
3651 flipping the prefix. An omitted bound is represented by a "-". For
3652 example, "- [-, 10]" means being outside the range starting at the lowest
3653 possible value and ending at 10, in other words, being greater than 10.
3654 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3655 always false.
3657 We set up things so that the missing bounds are handled in a consistent
3658 manner so neither a missing bound nor "true" and "false" need to be
3659 handled using a special case. */
3661 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3662 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3663 and UPPER1_P are nonzero if the respective argument is an upper bound
3664 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3665 must be specified for a comparison. ARG1 will be converted to ARG0's
3666 type if both are specified. */
3668 static tree
3669 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3670 tree arg1, int upper1_p)
3672 tree tem;
3673 int result;
3674 int sgn0, sgn1;
3676 /* If neither arg represents infinity, do the normal operation.
3677 Else, if not a comparison, return infinity. Else handle the special
3678 comparison rules. Note that most of the cases below won't occur, but
3679 are handled for consistency. */
3681 if (arg0 != 0 && arg1 != 0)
3683 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3684 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3685 STRIP_NOPS (tem);
3686 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3689 if (TREE_CODE_CLASS (code) != tcc_comparison)
3690 return 0;
3692 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3693 for neither. In real maths, we cannot assume open ended ranges are
3694 the same. But, this is computer arithmetic, where numbers are finite.
3695 We can therefore make the transformation of any unbounded range with
3696 the value Z, Z being greater than any representable number. This permits
3697 us to treat unbounded ranges as equal. */
3698 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3699 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3700 switch (code)
3702 case EQ_EXPR:
3703 result = sgn0 == sgn1;
3704 break;
3705 case NE_EXPR:
3706 result = sgn0 != sgn1;
3707 break;
3708 case LT_EXPR:
3709 result = sgn0 < sgn1;
3710 break;
3711 case LE_EXPR:
3712 result = sgn0 <= sgn1;
3713 break;
3714 case GT_EXPR:
3715 result = sgn0 > sgn1;
3716 break;
3717 case GE_EXPR:
3718 result = sgn0 >= sgn1;
3719 break;
3720 default:
3721 gcc_unreachable ();
3724 return constant_boolean_node (result, type);
3727 /* Given EXP, a logical expression, set the range it is testing into
3728 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3729 actually being tested. *PLOW and *PHIGH will be made of the same type
3730 as the returned expression. If EXP is not a comparison, we will most
3731 likely not be returning a useful value and range. */
3733 static tree
3734 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3736 enum tree_code code;
3737 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3738 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3739 int in_p, n_in_p;
3740 tree low, high, n_low, n_high;
3742 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3743 and see if we can refine the range. Some of the cases below may not
3744 happen, but it doesn't seem worth worrying about this. We "continue"
3745 the outer loop when we've changed something; otherwise we "break"
3746 the switch, which will "break" the while. */
3748 in_p = 0;
3749 low = high = build_int_cst (TREE_TYPE (exp), 0);
3751 while (1)
3753 code = TREE_CODE (exp);
3754 exp_type = TREE_TYPE (exp);
3756 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3758 if (TREE_CODE_LENGTH (code) > 0)
3759 arg0 = TREE_OPERAND (exp, 0);
3760 if (TREE_CODE_CLASS (code) == tcc_comparison
3761 || TREE_CODE_CLASS (code) == tcc_unary
3762 || TREE_CODE_CLASS (code) == tcc_binary)
3763 arg0_type = TREE_TYPE (arg0);
3764 if (TREE_CODE_CLASS (code) == tcc_binary
3765 || TREE_CODE_CLASS (code) == tcc_comparison
3766 || (TREE_CODE_CLASS (code) == tcc_expression
3767 && TREE_CODE_LENGTH (code) > 1))
3768 arg1 = TREE_OPERAND (exp, 1);
3771 switch (code)
3773 case TRUTH_NOT_EXPR:
3774 in_p = ! in_p, exp = arg0;
3775 continue;
3777 case EQ_EXPR: case NE_EXPR:
3778 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3779 /* We can only do something if the range is testing for zero
3780 and if the second operand is an integer constant. Note that
3781 saying something is "in" the range we make is done by
3782 complementing IN_P since it will set in the initial case of
3783 being not equal to zero; "out" is leaving it alone. */
3784 if (low == 0 || high == 0
3785 || ! integer_zerop (low) || ! integer_zerop (high)
3786 || TREE_CODE (arg1) != INTEGER_CST)
3787 break;
3789 switch (code)
3791 case NE_EXPR: /* - [c, c] */
3792 low = high = arg1;
3793 break;
3794 case EQ_EXPR: /* + [c, c] */
3795 in_p = ! in_p, low = high = arg1;
3796 break;
3797 case GT_EXPR: /* - [-, c] */
3798 low = 0, high = arg1;
3799 break;
3800 case GE_EXPR: /* + [c, -] */
3801 in_p = ! in_p, low = arg1, high = 0;
3802 break;
3803 case LT_EXPR: /* - [c, -] */
3804 low = arg1, high = 0;
3805 break;
3806 case LE_EXPR: /* + [-, c] */
3807 in_p = ! in_p, low = 0, high = arg1;
3808 break;
3809 default:
3810 gcc_unreachable ();
3813 /* If this is an unsigned comparison, we also know that EXP is
3814 greater than or equal to zero. We base the range tests we make
3815 on that fact, so we record it here so we can parse existing
3816 range tests. We test arg0_type since often the return type
3817 of, e.g. EQ_EXPR, is boolean. */
3818 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3820 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3821 in_p, low, high, 1,
3822 build_int_cst (arg0_type, 0),
3823 NULL_TREE))
3824 break;
3826 in_p = n_in_p, low = n_low, high = n_high;
3828 /* If the high bound is missing, but we have a nonzero low
3829 bound, reverse the range so it goes from zero to the low bound
3830 minus 1. */
3831 if (high == 0 && low && ! integer_zerop (low))
3833 in_p = ! in_p;
3834 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3835 integer_one_node, 0);
3836 low = build_int_cst (arg0_type, 0);
3840 exp = arg0;
3841 continue;
3843 case NEGATE_EXPR:
3844 /* (-x) IN [a,b] -> x in [-b, -a] */
3845 n_low = range_binop (MINUS_EXPR, exp_type,
3846 build_int_cst (exp_type, 0),
3847 0, high, 1);
3848 n_high = range_binop (MINUS_EXPR, exp_type,
3849 build_int_cst (exp_type, 0),
3850 0, low, 0);
3851 low = n_low, high = n_high;
3852 exp = arg0;
3853 continue;
3855 case BIT_NOT_EXPR:
3856 /* ~ X -> -X - 1 */
3857 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3858 build_int_cst (exp_type, 1));
3859 continue;
3861 case PLUS_EXPR: case MINUS_EXPR:
3862 if (TREE_CODE (arg1) != INTEGER_CST)
3863 break;
3865 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3866 move a constant to the other side. */
3867 if (flag_wrapv && !TYPE_UNSIGNED (arg0_type))
3868 break;
3870 /* If EXP is signed, any overflow in the computation is undefined,
3871 so we don't worry about it so long as our computations on
3872 the bounds don't overflow. For unsigned, overflow is defined
3873 and this is exactly the right thing. */
3874 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3875 arg0_type, low, 0, arg1, 0);
3876 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3877 arg0_type, high, 1, arg1, 0);
3878 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3879 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3880 break;
3882 /* Check for an unsigned range which has wrapped around the maximum
3883 value thus making n_high < n_low, and normalize it. */
3884 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3886 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3887 integer_one_node, 0);
3888 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3889 integer_one_node, 0);
3891 /* If the range is of the form +/- [ x+1, x ], we won't
3892 be able to normalize it. But then, it represents the
3893 whole range or the empty set, so make it
3894 +/- [ -, - ]. */
3895 if (tree_int_cst_equal (n_low, low)
3896 && tree_int_cst_equal (n_high, high))
3897 low = high = 0;
3898 else
3899 in_p = ! in_p;
3901 else
3902 low = n_low, high = n_high;
3904 exp = arg0;
3905 continue;
3907 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3908 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3909 break;
3911 if (! INTEGRAL_TYPE_P (arg0_type)
3912 || (low != 0 && ! int_fits_type_p (low, arg0_type))
3913 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3914 break;
3916 n_low = low, n_high = high;
3918 if (n_low != 0)
3919 n_low = fold_convert (arg0_type, n_low);
3921 if (n_high != 0)
3922 n_high = fold_convert (arg0_type, n_high);
3925 /* If we're converting arg0 from an unsigned type, to exp,
3926 a signed type, we will be doing the comparison as unsigned.
3927 The tests above have already verified that LOW and HIGH
3928 are both positive.
3930 So we have to ensure that we will handle large unsigned
3931 values the same way that the current signed bounds treat
3932 negative values. */
3934 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
3936 tree high_positive;
3937 tree equiv_type = lang_hooks.types.type_for_mode
3938 (TYPE_MODE (arg0_type), 1);
3940 /* A range without an upper bound is, naturally, unbounded.
3941 Since convert would have cropped a very large value, use
3942 the max value for the destination type. */
3943 high_positive
3944 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3945 : TYPE_MAX_VALUE (arg0_type);
3947 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
3948 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
3949 fold_convert (arg0_type,
3950 high_positive),
3951 fold_convert (arg0_type,
3952 integer_one_node));
3954 /* If the low bound is specified, "and" the range with the
3955 range for which the original unsigned value will be
3956 positive. */
3957 if (low != 0)
3959 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3960 1, n_low, n_high, 1,
3961 fold_convert (arg0_type,
3962 integer_zero_node),
3963 high_positive))
3964 break;
3966 in_p = (n_in_p == in_p);
3968 else
3970 /* Otherwise, "or" the range with the range of the input
3971 that will be interpreted as negative. */
3972 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3973 0, n_low, n_high, 1,
3974 fold_convert (arg0_type,
3975 integer_zero_node),
3976 high_positive))
3977 break;
3979 in_p = (in_p != n_in_p);
3983 exp = arg0;
3984 low = n_low, high = n_high;
3985 continue;
3987 default:
3988 break;
3991 break;
3994 /* If EXP is a constant, we can evaluate whether this is true or false. */
3995 if (TREE_CODE (exp) == INTEGER_CST)
3997 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3998 exp, 0, low, 0))
3999 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4000 exp, 1, high, 1)));
4001 low = high = 0;
4002 exp = 0;
4005 *pin_p = in_p, *plow = low, *phigh = high;
4006 return exp;
4009 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4010 type, TYPE, return an expression to test if EXP is in (or out of, depending
4011 on IN_P) the range. Return 0 if the test couldn't be created. */
4013 static tree
4014 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4016 tree etype = TREE_TYPE (exp);
4017 tree value;
4019 #ifdef HAVE_canonicalize_funcptr_for_compare
4020 /* Disable this optimization for function pointer expressions
4021 on targets that require function pointer canonicalization. */
4022 if (HAVE_canonicalize_funcptr_for_compare
4023 && TREE_CODE (etype) == POINTER_TYPE
4024 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4025 return NULL_TREE;
4026 #endif
4028 if (! in_p)
4030 value = build_range_check (type, exp, 1, low, high);
4031 if (value != 0)
4032 return invert_truthvalue (value);
4034 return 0;
4037 if (low == 0 && high == 0)
4038 return build_int_cst (type, 1);
4040 if (low == 0)
4041 return fold_build2 (LE_EXPR, type, exp,
4042 fold_convert (etype, high));
4044 if (high == 0)
4045 return fold_build2 (GE_EXPR, type, exp,
4046 fold_convert (etype, low));
4048 if (operand_equal_p (low, high, 0))
4049 return fold_build2 (EQ_EXPR, type, exp,
4050 fold_convert (etype, low));
4052 if (integer_zerop (low))
4054 if (! TYPE_UNSIGNED (etype))
4056 etype = lang_hooks.types.unsigned_type (etype);
4057 high = fold_convert (etype, high);
4058 exp = fold_convert (etype, exp);
4060 return build_range_check (type, exp, 1, 0, high);
4063 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4064 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4066 unsigned HOST_WIDE_INT lo;
4067 HOST_WIDE_INT hi;
4068 int prec;
4070 prec = TYPE_PRECISION (etype);
4071 if (prec <= HOST_BITS_PER_WIDE_INT)
4073 hi = 0;
4074 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4076 else
4078 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4079 lo = (unsigned HOST_WIDE_INT) -1;
4082 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4084 if (TYPE_UNSIGNED (etype))
4086 etype = lang_hooks.types.signed_type (etype);
4087 exp = fold_convert (etype, exp);
4089 return fold_build2 (GT_EXPR, type, exp,
4090 build_int_cst (etype, 0));
4094 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4095 This requires wrap-around arithmetics for the type of the expression. */
4096 switch (TREE_CODE (etype))
4098 case INTEGER_TYPE:
4099 /* There is no requirement that LOW be within the range of ETYPE
4100 if the latter is a subtype. It must, however, be within the base
4101 type of ETYPE. So be sure we do the subtraction in that type. */
4102 if (TREE_TYPE (etype))
4103 etype = TREE_TYPE (etype);
4104 break;
4106 case ENUMERAL_TYPE:
4107 case BOOLEAN_TYPE:
4108 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4109 TYPE_UNSIGNED (etype));
4110 break;
4112 default:
4113 break;
4116 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4117 if (TREE_CODE (etype) == INTEGER_TYPE
4118 && !TYPE_UNSIGNED (etype) && !flag_wrapv)
4120 tree utype, minv, maxv;
4122 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4123 for the type in question, as we rely on this here. */
4124 utype = lang_hooks.types.unsigned_type (etype);
4125 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4126 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4127 integer_one_node, 1);
4128 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4130 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4131 minv, 1, maxv, 1)))
4132 etype = utype;
4133 else
4134 return 0;
4137 high = fold_convert (etype, high);
4138 low = fold_convert (etype, low);
4139 exp = fold_convert (etype, exp);
4141 value = const_binop (MINUS_EXPR, high, low, 0);
4143 if (value != 0 && !TREE_OVERFLOW (value))
4144 return build_range_check (type,
4145 fold_build2 (MINUS_EXPR, etype, exp, low),
4146 1, build_int_cst (etype, 0), value);
4148 return 0;
4151 /* Return the predecessor of VAL in its type, handling the infinite case. */
4153 static tree
4154 range_predecessor (tree val)
4156 tree type = TREE_TYPE (val);
4158 if (INTEGRAL_TYPE_P (type) && val == TYPE_MIN_VALUE (type))
4159 return 0;
4160 else
4161 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4164 /* Return the successor of VAL in its type, handling the infinite case. */
4166 static tree
4167 range_successor (tree val)
4169 tree type = TREE_TYPE (val);
4171 if (INTEGRAL_TYPE_P (type) && val == TYPE_MAX_VALUE (type))
4172 return 0;
4173 else
4174 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4177 /* Given two ranges, see if we can merge them into one. Return 1 if we
4178 can, 0 if we can't. Set the output range into the specified parameters. */
4180 static int
4181 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4182 tree high0, int in1_p, tree low1, tree high1)
4184 int no_overlap;
4185 int subset;
4186 int temp;
4187 tree tem;
4188 int in_p;
4189 tree low, high;
4190 int lowequal = ((low0 == 0 && low1 == 0)
4191 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4192 low0, 0, low1, 0)));
4193 int highequal = ((high0 == 0 && high1 == 0)
4194 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4195 high0, 1, high1, 1)));
4197 /* Make range 0 be the range that starts first, or ends last if they
4198 start at the same value. Swap them if it isn't. */
4199 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4200 low0, 0, low1, 0))
4201 || (lowequal
4202 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4203 high1, 1, high0, 1))))
4205 temp = in0_p, in0_p = in1_p, in1_p = temp;
4206 tem = low0, low0 = low1, low1 = tem;
4207 tem = high0, high0 = high1, high1 = tem;
4210 /* Now flag two cases, whether the ranges are disjoint or whether the
4211 second range is totally subsumed in the first. Note that the tests
4212 below are simplified by the ones above. */
4213 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4214 high0, 1, low1, 0));
4215 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4216 high1, 1, high0, 1));
4218 /* We now have four cases, depending on whether we are including or
4219 excluding the two ranges. */
4220 if (in0_p && in1_p)
4222 /* If they don't overlap, the result is false. If the second range
4223 is a subset it is the result. Otherwise, the range is from the start
4224 of the second to the end of the first. */
4225 if (no_overlap)
4226 in_p = 0, low = high = 0;
4227 else if (subset)
4228 in_p = 1, low = low1, high = high1;
4229 else
4230 in_p = 1, low = low1, high = high0;
4233 else if (in0_p && ! in1_p)
4235 /* If they don't overlap, the result is the first range. If they are
4236 equal, the result is false. If the second range is a subset of the
4237 first, and the ranges begin at the same place, we go from just after
4238 the end of the second range to the end of the first. If the second
4239 range is not a subset of the first, or if it is a subset and both
4240 ranges end at the same place, the range starts at the start of the
4241 first range and ends just before the second range.
4242 Otherwise, we can't describe this as a single range. */
4243 if (no_overlap)
4244 in_p = 1, low = low0, high = high0;
4245 else if (lowequal && highequal)
4246 in_p = 0, low = high = 0;
4247 else if (subset && lowequal)
4249 low = range_successor (high1);
4250 high = high0;
4251 in_p = (low != 0);
4253 else if (! subset || highequal)
4255 low = low0;
4256 high = range_predecessor (low1);
4257 in_p = (high != 0);
4259 else
4260 return 0;
4263 else if (! in0_p && in1_p)
4265 /* If they don't overlap, the result is the second range. If the second
4266 is a subset of the first, the result is false. Otherwise,
4267 the range starts just after the first range and ends at the
4268 end of the second. */
4269 if (no_overlap)
4270 in_p = 1, low = low1, high = high1;
4271 else if (subset || highequal)
4272 in_p = 0, low = high = 0;
4273 else
4275 low = range_successor (high0);
4276 high = high1;
4277 in_p = (low != 0);
4281 else
4283 /* The case where we are excluding both ranges. Here the complex case
4284 is if they don't overlap. In that case, the only time we have a
4285 range is if they are adjacent. If the second is a subset of the
4286 first, the result is the first. Otherwise, the range to exclude
4287 starts at the beginning of the first range and ends at the end of the
4288 second. */
4289 if (no_overlap)
4291 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4292 range_successor (high0),
4293 1, low1, 0)))
4294 in_p = 0, low = low0, high = high1;
4295 else
4297 /* Canonicalize - [min, x] into - [-, x]. */
4298 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4299 switch (TREE_CODE (TREE_TYPE (low0)))
4301 case ENUMERAL_TYPE:
4302 if (TYPE_PRECISION (TREE_TYPE (low0))
4303 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4304 break;
4305 /* FALLTHROUGH */
4306 case INTEGER_TYPE:
4307 if (tree_int_cst_equal (low0,
4308 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4309 low0 = 0;
4310 break;
4311 case POINTER_TYPE:
4312 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4313 && integer_zerop (low0))
4314 low0 = 0;
4315 break;
4316 default:
4317 break;
4320 /* Canonicalize - [x, max] into - [x, -]. */
4321 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4322 switch (TREE_CODE (TREE_TYPE (high1)))
4324 case ENUMERAL_TYPE:
4325 if (TYPE_PRECISION (TREE_TYPE (high1))
4326 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4327 break;
4328 /* FALLTHROUGH */
4329 case INTEGER_TYPE:
4330 if (tree_int_cst_equal (high1,
4331 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4332 high1 = 0;
4333 break;
4334 case POINTER_TYPE:
4335 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4336 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4337 high1, 1,
4338 integer_one_node, 1)))
4339 high1 = 0;
4340 break;
4341 default:
4342 break;
4345 /* The ranges might be also adjacent between the maximum and
4346 minimum values of the given type. For
4347 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4348 return + [x + 1, y - 1]. */
4349 if (low0 == 0 && high1 == 0)
4351 low = range_successor (high0);
4352 high = range_predecessor (low1);
4353 if (low == 0 || high == 0)
4354 return 0;
4356 in_p = 1;
4358 else
4359 return 0;
4362 else if (subset)
4363 in_p = 0, low = low0, high = high0;
4364 else
4365 in_p = 0, low = low0, high = high1;
4368 *pin_p = in_p, *plow = low, *phigh = high;
4369 return 1;
4373 /* Subroutine of fold, looking inside expressions of the form
4374 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4375 of the COND_EXPR. This function is being used also to optimize
4376 A op B ? C : A, by reversing the comparison first.
4378 Return a folded expression whose code is not a COND_EXPR
4379 anymore, or NULL_TREE if no folding opportunity is found. */
4381 static tree
4382 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4384 enum tree_code comp_code = TREE_CODE (arg0);
4385 tree arg00 = TREE_OPERAND (arg0, 0);
4386 tree arg01 = TREE_OPERAND (arg0, 1);
4387 tree arg1_type = TREE_TYPE (arg1);
4388 tree tem;
4390 STRIP_NOPS (arg1);
4391 STRIP_NOPS (arg2);
4393 /* If we have A op 0 ? A : -A, consider applying the following
4394 transformations:
4396 A == 0? A : -A same as -A
4397 A != 0? A : -A same as A
4398 A >= 0? A : -A same as abs (A)
4399 A > 0? A : -A same as abs (A)
4400 A <= 0? A : -A same as -abs (A)
4401 A < 0? A : -A same as -abs (A)
4403 None of these transformations work for modes with signed
4404 zeros. If A is +/-0, the first two transformations will
4405 change the sign of the result (from +0 to -0, or vice
4406 versa). The last four will fix the sign of the result,
4407 even though the original expressions could be positive or
4408 negative, depending on the sign of A.
4410 Note that all these transformations are correct if A is
4411 NaN, since the two alternatives (A and -A) are also NaNs. */
4412 if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4413 ? real_zerop (arg01)
4414 : integer_zerop (arg01))
4415 && ((TREE_CODE (arg2) == NEGATE_EXPR
4416 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4417 /* In the case that A is of the form X-Y, '-A' (arg2) may
4418 have already been folded to Y-X, check for that. */
4419 || (TREE_CODE (arg1) == MINUS_EXPR
4420 && TREE_CODE (arg2) == MINUS_EXPR
4421 && operand_equal_p (TREE_OPERAND (arg1, 0),
4422 TREE_OPERAND (arg2, 1), 0)
4423 && operand_equal_p (TREE_OPERAND (arg1, 1),
4424 TREE_OPERAND (arg2, 0), 0))))
4425 switch (comp_code)
4427 case EQ_EXPR:
4428 case UNEQ_EXPR:
4429 tem = fold_convert (arg1_type, arg1);
4430 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4431 case NE_EXPR:
4432 case LTGT_EXPR:
4433 return pedantic_non_lvalue (fold_convert (type, arg1));
4434 case UNGE_EXPR:
4435 case UNGT_EXPR:
4436 if (flag_trapping_math)
4437 break;
4438 /* Fall through. */
4439 case GE_EXPR:
4440 case GT_EXPR:
4441 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4442 arg1 = fold_convert (lang_hooks.types.signed_type
4443 (TREE_TYPE (arg1)), arg1);
4444 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4445 return pedantic_non_lvalue (fold_convert (type, tem));
4446 case UNLE_EXPR:
4447 case UNLT_EXPR:
4448 if (flag_trapping_math)
4449 break;
4450 case LE_EXPR:
4451 case LT_EXPR:
4452 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4453 arg1 = fold_convert (lang_hooks.types.signed_type
4454 (TREE_TYPE (arg1)), arg1);
4455 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4456 return negate_expr (fold_convert (type, tem));
4457 default:
4458 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4459 break;
4462 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4463 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4464 both transformations are correct when A is NaN: A != 0
4465 is then true, and A == 0 is false. */
4467 if (integer_zerop (arg01) && integer_zerop (arg2))
4469 if (comp_code == NE_EXPR)
4470 return pedantic_non_lvalue (fold_convert (type, arg1));
4471 else if (comp_code == EQ_EXPR)
4472 return build_int_cst (type, 0);
4475 /* Try some transformations of A op B ? A : B.
4477 A == B? A : B same as B
4478 A != B? A : B same as A
4479 A >= B? A : B same as max (A, B)
4480 A > B? A : B same as max (B, A)
4481 A <= B? A : B same as min (A, B)
4482 A < B? A : B same as min (B, A)
4484 As above, these transformations don't work in the presence
4485 of signed zeros. For example, if A and B are zeros of
4486 opposite sign, the first two transformations will change
4487 the sign of the result. In the last four, the original
4488 expressions give different results for (A=+0, B=-0) and
4489 (A=-0, B=+0), but the transformed expressions do not.
4491 The first two transformations are correct if either A or B
4492 is a NaN. In the first transformation, the condition will
4493 be false, and B will indeed be chosen. In the case of the
4494 second transformation, the condition A != B will be true,
4495 and A will be chosen.
4497 The conversions to max() and min() are not correct if B is
4498 a number and A is not. The conditions in the original
4499 expressions will be false, so all four give B. The min()
4500 and max() versions would give a NaN instead. */
4501 if (operand_equal_for_comparison_p (arg01, arg2, arg00)
4502 /* Avoid these transformations if the COND_EXPR may be used
4503 as an lvalue in the C++ front-end. PR c++/19199. */
4504 && (in_gimple_form
4505 || strcmp (lang_hooks.name, "GNU C++") != 0
4506 || ! maybe_lvalue_p (arg1)
4507 || ! maybe_lvalue_p (arg2)))
4509 tree comp_op0 = arg00;
4510 tree comp_op1 = arg01;
4511 tree comp_type = TREE_TYPE (comp_op0);
4513 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4514 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4516 comp_type = type;
4517 comp_op0 = arg1;
4518 comp_op1 = arg2;
4521 switch (comp_code)
4523 case EQ_EXPR:
4524 return pedantic_non_lvalue (fold_convert (type, arg2));
4525 case NE_EXPR:
4526 return pedantic_non_lvalue (fold_convert (type, arg1));
4527 case LE_EXPR:
4528 case LT_EXPR:
4529 case UNLE_EXPR:
4530 case UNLT_EXPR:
4531 /* In C++ a ?: expression can be an lvalue, so put the
4532 operand which will be used if they are equal first
4533 so that we can convert this back to the
4534 corresponding COND_EXPR. */
4535 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4537 comp_op0 = fold_convert (comp_type, comp_op0);
4538 comp_op1 = fold_convert (comp_type, comp_op1);
4539 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4540 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4541 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4542 return pedantic_non_lvalue (fold_convert (type, tem));
4544 break;
4545 case GE_EXPR:
4546 case GT_EXPR:
4547 case UNGE_EXPR:
4548 case UNGT_EXPR:
4549 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4551 comp_op0 = fold_convert (comp_type, comp_op0);
4552 comp_op1 = fold_convert (comp_type, comp_op1);
4553 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4554 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
4555 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
4556 return pedantic_non_lvalue (fold_convert (type, tem));
4558 break;
4559 case UNEQ_EXPR:
4560 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4561 return pedantic_non_lvalue (fold_convert (type, arg2));
4562 break;
4563 case LTGT_EXPR:
4564 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4565 return pedantic_non_lvalue (fold_convert (type, arg1));
4566 break;
4567 default:
4568 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4569 break;
4573 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4574 we might still be able to simplify this. For example,
4575 if C1 is one less or one more than C2, this might have started
4576 out as a MIN or MAX and been transformed by this function.
4577 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4579 if (INTEGRAL_TYPE_P (type)
4580 && TREE_CODE (arg01) == INTEGER_CST
4581 && TREE_CODE (arg2) == INTEGER_CST)
4582 switch (comp_code)
4584 case EQ_EXPR:
4585 /* We can replace A with C1 in this case. */
4586 arg1 = fold_convert (type, arg01);
4587 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
4589 case LT_EXPR:
4590 /* If C1 is C2 + 1, this is min(A, C2). */
4591 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4592 OEP_ONLY_CONST)
4593 && operand_equal_p (arg01,
4594 const_binop (PLUS_EXPR, arg2,
4595 integer_one_node, 0),
4596 OEP_ONLY_CONST))
4597 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4598 type, arg1, arg2));
4599 break;
4601 case LE_EXPR:
4602 /* If C1 is C2 - 1, this is min(A, C2). */
4603 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4604 OEP_ONLY_CONST)
4605 && operand_equal_p (arg01,
4606 const_binop (MINUS_EXPR, arg2,
4607 integer_one_node, 0),
4608 OEP_ONLY_CONST))
4609 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4610 type, arg1, arg2));
4611 break;
4613 case GT_EXPR:
4614 /* If C1 is C2 - 1, this is max(A, C2). */
4615 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4616 OEP_ONLY_CONST)
4617 && operand_equal_p (arg01,
4618 const_binop (MINUS_EXPR, arg2,
4619 integer_one_node, 0),
4620 OEP_ONLY_CONST))
4621 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4622 type, arg1, arg2));
4623 break;
4625 case GE_EXPR:
4626 /* If C1 is C2 + 1, this is max(A, C2). */
4627 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4628 OEP_ONLY_CONST)
4629 && operand_equal_p (arg01,
4630 const_binop (PLUS_EXPR, arg2,
4631 integer_one_node, 0),
4632 OEP_ONLY_CONST))
4633 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4634 type, arg1, arg2));
4635 break;
4636 case NE_EXPR:
4637 break;
4638 default:
4639 gcc_unreachable ();
4642 return NULL_TREE;
4647 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4648 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4649 #endif
4651 /* EXP is some logical combination of boolean tests. See if we can
4652 merge it into some range test. Return the new tree if so. */
4654 static tree
4655 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
4657 int or_op = (code == TRUTH_ORIF_EXPR
4658 || code == TRUTH_OR_EXPR);
4659 int in0_p, in1_p, in_p;
4660 tree low0, low1, low, high0, high1, high;
4661 tree lhs = make_range (op0, &in0_p, &low0, &high0);
4662 tree rhs = make_range (op1, &in1_p, &low1, &high1);
4663 tree tem;
4665 /* If this is an OR operation, invert both sides; we will invert
4666 again at the end. */
4667 if (or_op)
4668 in0_p = ! in0_p, in1_p = ! in1_p;
4670 /* If both expressions are the same, if we can merge the ranges, and we
4671 can build the range test, return it or it inverted. If one of the
4672 ranges is always true or always false, consider it to be the same
4673 expression as the other. */
4674 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4675 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4676 in1_p, low1, high1)
4677 && 0 != (tem = (build_range_check (type,
4678 lhs != 0 ? lhs
4679 : rhs != 0 ? rhs : integer_zero_node,
4680 in_p, low, high))))
4681 return or_op ? invert_truthvalue (tem) : tem;
4683 /* On machines where the branch cost is expensive, if this is a
4684 short-circuited branch and the underlying object on both sides
4685 is the same, make a non-short-circuit operation. */
4686 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4687 && lhs != 0 && rhs != 0
4688 && (code == TRUTH_ANDIF_EXPR
4689 || code == TRUTH_ORIF_EXPR)
4690 && operand_equal_p (lhs, rhs, 0))
4692 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4693 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4694 which cases we can't do this. */
4695 if (simple_operand_p (lhs))
4696 return build2 (code == TRUTH_ANDIF_EXPR
4697 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4698 type, op0, op1);
4700 else if (lang_hooks.decls.global_bindings_p () == 0
4701 && ! CONTAINS_PLACEHOLDER_P (lhs))
4703 tree common = save_expr (lhs);
4705 if (0 != (lhs = build_range_check (type, common,
4706 or_op ? ! in0_p : in0_p,
4707 low0, high0))
4708 && (0 != (rhs = build_range_check (type, common,
4709 or_op ? ! in1_p : in1_p,
4710 low1, high1))))
4711 return build2 (code == TRUTH_ANDIF_EXPR
4712 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4713 type, lhs, rhs);
4717 return 0;
4720 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4721 bit value. Arrange things so the extra bits will be set to zero if and
4722 only if C is signed-extended to its full width. If MASK is nonzero,
4723 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4725 static tree
4726 unextend (tree c, int p, int unsignedp, tree mask)
4728 tree type = TREE_TYPE (c);
4729 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4730 tree temp;
4732 if (p == modesize || unsignedp)
4733 return c;
4735 /* We work by getting just the sign bit into the low-order bit, then
4736 into the high-order bit, then sign-extend. We then XOR that value
4737 with C. */
4738 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4739 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4741 /* We must use a signed type in order to get an arithmetic right shift.
4742 However, we must also avoid introducing accidental overflows, so that
4743 a subsequent call to integer_zerop will work. Hence we must
4744 do the type conversion here. At this point, the constant is either
4745 zero or one, and the conversion to a signed type can never overflow.
4746 We could get an overflow if this conversion is done anywhere else. */
4747 if (TYPE_UNSIGNED (type))
4748 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4750 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4751 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4752 if (mask != 0)
4753 temp = const_binop (BIT_AND_EXPR, temp,
4754 fold_convert (TREE_TYPE (c), mask), 0);
4755 /* If necessary, convert the type back to match the type of C. */
4756 if (TYPE_UNSIGNED (type))
4757 temp = fold_convert (type, temp);
4759 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4762 /* Find ways of folding logical expressions of LHS and RHS:
4763 Try to merge two comparisons to the same innermost item.
4764 Look for range tests like "ch >= '0' && ch <= '9'".
4765 Look for combinations of simple terms on machines with expensive branches
4766 and evaluate the RHS unconditionally.
4768 For example, if we have p->a == 2 && p->b == 4 and we can make an
4769 object large enough to span both A and B, we can do this with a comparison
4770 against the object ANDed with the a mask.
4772 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4773 operations to do this with one comparison.
4775 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4776 function and the one above.
4778 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4779 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4781 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4782 two operands.
4784 We return the simplified tree or 0 if no optimization is possible. */
4786 static tree
4787 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4789 /* If this is the "or" of two comparisons, we can do something if
4790 the comparisons are NE_EXPR. If this is the "and", we can do something
4791 if the comparisons are EQ_EXPR. I.e.,
4792 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4794 WANTED_CODE is this operation code. For single bit fields, we can
4795 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4796 comparison for one-bit fields. */
4798 enum tree_code wanted_code;
4799 enum tree_code lcode, rcode;
4800 tree ll_arg, lr_arg, rl_arg, rr_arg;
4801 tree ll_inner, lr_inner, rl_inner, rr_inner;
4802 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4803 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4804 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4805 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4806 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4807 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4808 enum machine_mode lnmode, rnmode;
4809 tree ll_mask, lr_mask, rl_mask, rr_mask;
4810 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4811 tree l_const, r_const;
4812 tree lntype, rntype, result;
4813 int first_bit, end_bit;
4814 int volatilep;
4816 /* Start by getting the comparison codes. Fail if anything is volatile.
4817 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4818 it were surrounded with a NE_EXPR. */
4820 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4821 return 0;
4823 lcode = TREE_CODE (lhs);
4824 rcode = TREE_CODE (rhs);
4826 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4828 lhs = build2 (NE_EXPR, truth_type, lhs,
4829 build_int_cst (TREE_TYPE (lhs), 0));
4830 lcode = NE_EXPR;
4833 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4835 rhs = build2 (NE_EXPR, truth_type, rhs,
4836 build_int_cst (TREE_TYPE (rhs), 0));
4837 rcode = NE_EXPR;
4840 if (TREE_CODE_CLASS (lcode) != tcc_comparison
4841 || TREE_CODE_CLASS (rcode) != tcc_comparison)
4842 return 0;
4844 ll_arg = TREE_OPERAND (lhs, 0);
4845 lr_arg = TREE_OPERAND (lhs, 1);
4846 rl_arg = TREE_OPERAND (rhs, 0);
4847 rr_arg = TREE_OPERAND (rhs, 1);
4849 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
4850 if (simple_operand_p (ll_arg)
4851 && simple_operand_p (lr_arg))
4853 tree result;
4854 if (operand_equal_p (ll_arg, rl_arg, 0)
4855 && operand_equal_p (lr_arg, rr_arg, 0))
4857 result = combine_comparisons (code, lcode, rcode,
4858 truth_type, ll_arg, lr_arg);
4859 if (result)
4860 return result;
4862 else if (operand_equal_p (ll_arg, rr_arg, 0)
4863 && operand_equal_p (lr_arg, rl_arg, 0))
4865 result = combine_comparisons (code, lcode,
4866 swap_tree_comparison (rcode),
4867 truth_type, ll_arg, lr_arg);
4868 if (result)
4869 return result;
4873 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4874 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4876 /* If the RHS can be evaluated unconditionally and its operands are
4877 simple, it wins to evaluate the RHS unconditionally on machines
4878 with expensive branches. In this case, this isn't a comparison
4879 that can be merged. Avoid doing this if the RHS is a floating-point
4880 comparison since those can trap. */
4882 if (BRANCH_COST >= 2
4883 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4884 && simple_operand_p (rl_arg)
4885 && simple_operand_p (rr_arg))
4887 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4888 if (code == TRUTH_OR_EXPR
4889 && lcode == NE_EXPR && integer_zerop (lr_arg)
4890 && rcode == NE_EXPR && integer_zerop (rr_arg)
4891 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4892 return build2 (NE_EXPR, truth_type,
4893 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4894 ll_arg, rl_arg),
4895 build_int_cst (TREE_TYPE (ll_arg), 0));
4897 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4898 if (code == TRUTH_AND_EXPR
4899 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4900 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4901 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4902 return build2 (EQ_EXPR, truth_type,
4903 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4904 ll_arg, rl_arg),
4905 build_int_cst (TREE_TYPE (ll_arg), 0));
4907 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
4908 return build2 (code, truth_type, lhs, rhs);
4911 /* See if the comparisons can be merged. Then get all the parameters for
4912 each side. */
4914 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4915 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4916 return 0;
4918 volatilep = 0;
4919 ll_inner = decode_field_reference (ll_arg,
4920 &ll_bitsize, &ll_bitpos, &ll_mode,
4921 &ll_unsignedp, &volatilep, &ll_mask,
4922 &ll_and_mask);
4923 lr_inner = decode_field_reference (lr_arg,
4924 &lr_bitsize, &lr_bitpos, &lr_mode,
4925 &lr_unsignedp, &volatilep, &lr_mask,
4926 &lr_and_mask);
4927 rl_inner = decode_field_reference (rl_arg,
4928 &rl_bitsize, &rl_bitpos, &rl_mode,
4929 &rl_unsignedp, &volatilep, &rl_mask,
4930 &rl_and_mask);
4931 rr_inner = decode_field_reference (rr_arg,
4932 &rr_bitsize, &rr_bitpos, &rr_mode,
4933 &rr_unsignedp, &volatilep, &rr_mask,
4934 &rr_and_mask);
4936 /* It must be true that the inner operation on the lhs of each
4937 comparison must be the same if we are to be able to do anything.
4938 Then see if we have constants. If not, the same must be true for
4939 the rhs's. */
4940 if (volatilep || ll_inner == 0 || rl_inner == 0
4941 || ! operand_equal_p (ll_inner, rl_inner, 0))
4942 return 0;
4944 if (TREE_CODE (lr_arg) == INTEGER_CST
4945 && TREE_CODE (rr_arg) == INTEGER_CST)
4946 l_const = lr_arg, r_const = rr_arg;
4947 else if (lr_inner == 0 || rr_inner == 0
4948 || ! operand_equal_p (lr_inner, rr_inner, 0))
4949 return 0;
4950 else
4951 l_const = r_const = 0;
4953 /* If either comparison code is not correct for our logical operation,
4954 fail. However, we can convert a one-bit comparison against zero into
4955 the opposite comparison against that bit being set in the field. */
4957 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4958 if (lcode != wanted_code)
4960 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4962 /* Make the left operand unsigned, since we are only interested
4963 in the value of one bit. Otherwise we are doing the wrong
4964 thing below. */
4965 ll_unsignedp = 1;
4966 l_const = ll_mask;
4968 else
4969 return 0;
4972 /* This is analogous to the code for l_const above. */
4973 if (rcode != wanted_code)
4975 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4977 rl_unsignedp = 1;
4978 r_const = rl_mask;
4980 else
4981 return 0;
4984 /* After this point all optimizations will generate bit-field
4985 references, which we might not want. */
4986 if (! lang_hooks.can_use_bit_fields_p ())
4987 return 0;
4989 /* See if we can find a mode that contains both fields being compared on
4990 the left. If we can't, fail. Otherwise, update all constants and masks
4991 to be relative to a field of that size. */
4992 first_bit = MIN (ll_bitpos, rl_bitpos);
4993 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4994 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4995 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4996 volatilep);
4997 if (lnmode == VOIDmode)
4998 return 0;
5000 lnbitsize = GET_MODE_BITSIZE (lnmode);
5001 lnbitpos = first_bit & ~ (lnbitsize - 1);
5002 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5003 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5005 if (BYTES_BIG_ENDIAN)
5007 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5008 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5011 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5012 size_int (xll_bitpos), 0);
5013 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5014 size_int (xrl_bitpos), 0);
5016 if (l_const)
5018 l_const = fold_convert (lntype, l_const);
5019 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5020 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5021 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5022 fold_build1 (BIT_NOT_EXPR,
5023 lntype, ll_mask),
5024 0)))
5026 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5028 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5031 if (r_const)
5033 r_const = fold_convert (lntype, r_const);
5034 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5035 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5036 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5037 fold_build1 (BIT_NOT_EXPR,
5038 lntype, rl_mask),
5039 0)))
5041 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5043 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5047 /* If the right sides are not constant, do the same for it. Also,
5048 disallow this optimization if a size or signedness mismatch occurs
5049 between the left and right sides. */
5050 if (l_const == 0)
5052 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5053 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5054 /* Make sure the two fields on the right
5055 correspond to the left without being swapped. */
5056 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5057 return 0;
5059 first_bit = MIN (lr_bitpos, rr_bitpos);
5060 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5061 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5062 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5063 volatilep);
5064 if (rnmode == VOIDmode)
5065 return 0;
5067 rnbitsize = GET_MODE_BITSIZE (rnmode);
5068 rnbitpos = first_bit & ~ (rnbitsize - 1);
5069 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5070 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5072 if (BYTES_BIG_ENDIAN)
5074 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5075 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5078 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5079 size_int (xlr_bitpos), 0);
5080 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5081 size_int (xrr_bitpos), 0);
5083 /* Make a mask that corresponds to both fields being compared.
5084 Do this for both items being compared. If the operands are the
5085 same size and the bits being compared are in the same position
5086 then we can do this by masking both and comparing the masked
5087 results. */
5088 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5089 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5090 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5092 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5093 ll_unsignedp || rl_unsignedp);
5094 if (! all_ones_mask_p (ll_mask, lnbitsize))
5095 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5097 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5098 lr_unsignedp || rr_unsignedp);
5099 if (! all_ones_mask_p (lr_mask, rnbitsize))
5100 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5102 return build2 (wanted_code, truth_type, lhs, rhs);
5105 /* There is still another way we can do something: If both pairs of
5106 fields being compared are adjacent, we may be able to make a wider
5107 field containing them both.
5109 Note that we still must mask the lhs/rhs expressions. Furthermore,
5110 the mask must be shifted to account for the shift done by
5111 make_bit_field_ref. */
5112 if ((ll_bitsize + ll_bitpos == rl_bitpos
5113 && lr_bitsize + lr_bitpos == rr_bitpos)
5114 || (ll_bitpos == rl_bitpos + rl_bitsize
5115 && lr_bitpos == rr_bitpos + rr_bitsize))
5117 tree type;
5119 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5120 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5121 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5122 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5124 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5125 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5126 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5127 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5129 /* Convert to the smaller type before masking out unwanted bits. */
5130 type = lntype;
5131 if (lntype != rntype)
5133 if (lnbitsize > rnbitsize)
5135 lhs = fold_convert (rntype, lhs);
5136 ll_mask = fold_convert (rntype, ll_mask);
5137 type = rntype;
5139 else if (lnbitsize < rnbitsize)
5141 rhs = fold_convert (lntype, rhs);
5142 lr_mask = fold_convert (lntype, lr_mask);
5143 type = lntype;
5147 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5148 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5150 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5151 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5153 return build2 (wanted_code, truth_type, lhs, rhs);
5156 return 0;
5159 /* Handle the case of comparisons with constants. If there is something in
5160 common between the masks, those bits of the constants must be the same.
5161 If not, the condition is always false. Test for this to avoid generating
5162 incorrect code below. */
5163 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5164 if (! integer_zerop (result)
5165 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5166 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5168 if (wanted_code == NE_EXPR)
5170 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5171 return constant_boolean_node (true, truth_type);
5173 else
5175 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5176 return constant_boolean_node (false, truth_type);
5180 /* Construct the expression we will return. First get the component
5181 reference we will make. Unless the mask is all ones the width of
5182 that field, perform the mask operation. Then compare with the
5183 merged constant. */
5184 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5185 ll_unsignedp || rl_unsignedp);
5187 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5188 if (! all_ones_mask_p (ll_mask, lnbitsize))
5189 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5191 return build2 (wanted_code, truth_type, result,
5192 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5195 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5196 constant. */
5198 static tree
5199 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5201 tree arg0 = op0;
5202 enum tree_code op_code;
5203 tree comp_const = op1;
5204 tree minmax_const;
5205 int consts_equal, consts_lt;
5206 tree inner;
5208 STRIP_SIGN_NOPS (arg0);
5210 op_code = TREE_CODE (arg0);
5211 minmax_const = TREE_OPERAND (arg0, 1);
5212 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5213 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5214 inner = TREE_OPERAND (arg0, 0);
5216 /* If something does not permit us to optimize, return the original tree. */
5217 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5218 || TREE_CODE (comp_const) != INTEGER_CST
5219 || TREE_CONSTANT_OVERFLOW (comp_const)
5220 || TREE_CODE (minmax_const) != INTEGER_CST
5221 || TREE_CONSTANT_OVERFLOW (minmax_const))
5222 return NULL_TREE;
5224 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5225 and GT_EXPR, doing the rest with recursive calls using logical
5226 simplifications. */
5227 switch (code)
5229 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5231 /* FIXME: We should be able to invert code without building a
5232 scratch tree node, but doing so would require us to
5233 duplicate a part of invert_truthvalue here. */
5234 tree tem = invert_truthvalue (build2 (code, type, op0, op1));
5235 tem = optimize_minmax_comparison (TREE_CODE (tem),
5236 TREE_TYPE (tem),
5237 TREE_OPERAND (tem, 0),
5238 TREE_OPERAND (tem, 1));
5239 return invert_truthvalue (tem);
5242 case GE_EXPR:
5243 return
5244 fold_build2 (TRUTH_ORIF_EXPR, type,
5245 optimize_minmax_comparison
5246 (EQ_EXPR, type, arg0, comp_const),
5247 optimize_minmax_comparison
5248 (GT_EXPR, type, arg0, comp_const));
5250 case EQ_EXPR:
5251 if (op_code == MAX_EXPR && consts_equal)
5252 /* MAX (X, 0) == 0 -> X <= 0 */
5253 return fold_build2 (LE_EXPR, type, inner, comp_const);
5255 else if (op_code == MAX_EXPR && consts_lt)
5256 /* MAX (X, 0) == 5 -> X == 5 */
5257 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5259 else if (op_code == MAX_EXPR)
5260 /* MAX (X, 0) == -1 -> false */
5261 return omit_one_operand (type, integer_zero_node, inner);
5263 else if (consts_equal)
5264 /* MIN (X, 0) == 0 -> X >= 0 */
5265 return fold_build2 (GE_EXPR, type, inner, comp_const);
5267 else if (consts_lt)
5268 /* MIN (X, 0) == 5 -> false */
5269 return omit_one_operand (type, integer_zero_node, inner);
5271 else
5272 /* MIN (X, 0) == -1 -> X == -1 */
5273 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5275 case GT_EXPR:
5276 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5277 /* MAX (X, 0) > 0 -> X > 0
5278 MAX (X, 0) > 5 -> X > 5 */
5279 return fold_build2 (GT_EXPR, type, inner, comp_const);
5281 else if (op_code == MAX_EXPR)
5282 /* MAX (X, 0) > -1 -> true */
5283 return omit_one_operand (type, integer_one_node, inner);
5285 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5286 /* MIN (X, 0) > 0 -> false
5287 MIN (X, 0) > 5 -> false */
5288 return omit_one_operand (type, integer_zero_node, inner);
5290 else
5291 /* MIN (X, 0) > -1 -> X > -1 */
5292 return fold_build2 (GT_EXPR, type, inner, comp_const);
5294 default:
5295 return NULL_TREE;
5299 /* T is an integer expression that is being multiplied, divided, or taken a
5300 modulus (CODE says which and what kind of divide or modulus) by a
5301 constant C. See if we can eliminate that operation by folding it with
5302 other operations already in T. WIDE_TYPE, if non-null, is a type that
5303 should be used for the computation if wider than our type.
5305 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5306 (X * 2) + (Y * 4). We must, however, be assured that either the original
5307 expression would not overflow or that overflow is undefined for the type
5308 in the language in question.
5310 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5311 the machine has a multiply-accumulate insn or that this is part of an
5312 addressing calculation.
5314 If we return a non-null expression, it is an equivalent form of the
5315 original computation, but need not be in the original type. */
5317 static tree
5318 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5320 /* To avoid exponential search depth, refuse to allow recursion past
5321 three levels. Beyond that (1) it's highly unlikely that we'll find
5322 something interesting and (2) we've probably processed it before
5323 when we built the inner expression. */
5325 static int depth;
5326 tree ret;
5328 if (depth > 3)
5329 return NULL;
5331 depth++;
5332 ret = extract_muldiv_1 (t, c, code, wide_type);
5333 depth--;
5335 return ret;
5338 static tree
5339 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5341 tree type = TREE_TYPE (t);
5342 enum tree_code tcode = TREE_CODE (t);
5343 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5344 > GET_MODE_SIZE (TYPE_MODE (type)))
5345 ? wide_type : type);
5346 tree t1, t2;
5347 int same_p = tcode == code;
5348 tree op0 = NULL_TREE, op1 = NULL_TREE;
5350 /* Don't deal with constants of zero here; they confuse the code below. */
5351 if (integer_zerop (c))
5352 return NULL_TREE;
5354 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5355 op0 = TREE_OPERAND (t, 0);
5357 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5358 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5360 /* Note that we need not handle conditional operations here since fold
5361 already handles those cases. So just do arithmetic here. */
5362 switch (tcode)
5364 case INTEGER_CST:
5365 /* For a constant, we can always simplify if we are a multiply
5366 or (for divide and modulus) if it is a multiple of our constant. */
5367 if (code == MULT_EXPR
5368 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5369 return const_binop (code, fold_convert (ctype, t),
5370 fold_convert (ctype, c), 0);
5371 break;
5373 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
5374 /* If op0 is an expression ... */
5375 if ((COMPARISON_CLASS_P (op0)
5376 || UNARY_CLASS_P (op0)
5377 || BINARY_CLASS_P (op0)
5378 || EXPRESSION_CLASS_P (op0))
5379 /* ... and is unsigned, and its type is smaller than ctype,
5380 then we cannot pass through as widening. */
5381 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5382 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5383 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5384 && (GET_MODE_SIZE (TYPE_MODE (ctype))
5385 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5386 /* ... or this is a truncation (t is narrower than op0),
5387 then we cannot pass through this narrowing. */
5388 || (GET_MODE_SIZE (TYPE_MODE (type))
5389 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5390 /* ... or signedness changes for division or modulus,
5391 then we cannot pass through this conversion. */
5392 || (code != MULT_EXPR
5393 && (TYPE_UNSIGNED (ctype)
5394 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5395 break;
5397 /* Pass the constant down and see if we can make a simplification. If
5398 we can, replace this expression with the inner simplification for
5399 possible later conversion to our or some other type. */
5400 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5401 && TREE_CODE (t2) == INTEGER_CST
5402 && ! TREE_CONSTANT_OVERFLOW (t2)
5403 && (0 != (t1 = extract_muldiv (op0, t2, code,
5404 code == MULT_EXPR
5405 ? ctype : NULL_TREE))))
5406 return t1;
5407 break;
5409 case ABS_EXPR:
5410 /* If widening the type changes it from signed to unsigned, then we
5411 must avoid building ABS_EXPR itself as unsigned. */
5412 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5414 tree cstype = (*lang_hooks.types.signed_type) (ctype);
5415 if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
5417 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5418 return fold_convert (ctype, t1);
5420 break;
5422 /* FALLTHROUGH */
5423 case NEGATE_EXPR:
5424 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5425 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5426 break;
5428 case MIN_EXPR: case MAX_EXPR:
5429 /* If widening the type changes the signedness, then we can't perform
5430 this optimization as that changes the result. */
5431 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5432 break;
5434 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5435 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5436 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5438 if (tree_int_cst_sgn (c) < 0)
5439 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5441 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5442 fold_convert (ctype, t2));
5444 break;
5446 case LSHIFT_EXPR: case RSHIFT_EXPR:
5447 /* If the second operand is constant, this is a multiplication
5448 or floor division, by a power of two, so we can treat it that
5449 way unless the multiplier or divisor overflows. Signed
5450 left-shift overflow is implementation-defined rather than
5451 undefined in C90, so do not convert signed left shift into
5452 multiplication. */
5453 if (TREE_CODE (op1) == INTEGER_CST
5454 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5455 /* const_binop may not detect overflow correctly,
5456 so check for it explicitly here. */
5457 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5458 && TREE_INT_CST_HIGH (op1) == 0
5459 && 0 != (t1 = fold_convert (ctype,
5460 const_binop (LSHIFT_EXPR,
5461 size_one_node,
5462 op1, 0)))
5463 && ! TREE_OVERFLOW (t1))
5464 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5465 ? MULT_EXPR : FLOOR_DIV_EXPR,
5466 ctype, fold_convert (ctype, op0), t1),
5467 c, code, wide_type);
5468 break;
5470 case PLUS_EXPR: case MINUS_EXPR:
5471 /* See if we can eliminate the operation on both sides. If we can, we
5472 can return a new PLUS or MINUS. If we can't, the only remaining
5473 cases where we can do anything are if the second operand is a
5474 constant. */
5475 t1 = extract_muldiv (op0, c, code, wide_type);
5476 t2 = extract_muldiv (op1, c, code, wide_type);
5477 if (t1 != 0 && t2 != 0
5478 && (code == MULT_EXPR
5479 /* If not multiplication, we can only do this if both operands
5480 are divisible by c. */
5481 || (multiple_of_p (ctype, op0, c)
5482 && multiple_of_p (ctype, op1, c))))
5483 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5484 fold_convert (ctype, t2));
5486 /* If this was a subtraction, negate OP1 and set it to be an addition.
5487 This simplifies the logic below. */
5488 if (tcode == MINUS_EXPR)
5489 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5491 if (TREE_CODE (op1) != INTEGER_CST)
5492 break;
5494 /* If either OP1 or C are negative, this optimization is not safe for
5495 some of the division and remainder types while for others we need
5496 to change the code. */
5497 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5499 if (code == CEIL_DIV_EXPR)
5500 code = FLOOR_DIV_EXPR;
5501 else if (code == FLOOR_DIV_EXPR)
5502 code = CEIL_DIV_EXPR;
5503 else if (code != MULT_EXPR
5504 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5505 break;
5508 /* If it's a multiply or a division/modulus operation of a multiple
5509 of our constant, do the operation and verify it doesn't overflow. */
5510 if (code == MULT_EXPR
5511 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5513 op1 = const_binop (code, fold_convert (ctype, op1),
5514 fold_convert (ctype, c), 0);
5515 /* We allow the constant to overflow with wrapping semantics. */
5516 if (op1 == 0
5517 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5518 break;
5520 else
5521 break;
5523 /* If we have an unsigned type is not a sizetype, we cannot widen
5524 the operation since it will change the result if the original
5525 computation overflowed. */
5526 if (TYPE_UNSIGNED (ctype)
5527 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5528 && ctype != type)
5529 break;
5531 /* If we were able to eliminate our operation from the first side,
5532 apply our operation to the second side and reform the PLUS. */
5533 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5534 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5536 /* The last case is if we are a multiply. In that case, we can
5537 apply the distributive law to commute the multiply and addition
5538 if the multiplication of the constants doesn't overflow. */
5539 if (code == MULT_EXPR)
5540 return fold_build2 (tcode, ctype,
5541 fold_build2 (code, ctype,
5542 fold_convert (ctype, op0),
5543 fold_convert (ctype, c)),
5544 op1);
5546 break;
5548 case MULT_EXPR:
5549 /* We have a special case here if we are doing something like
5550 (C * 8) % 4 since we know that's zero. */
5551 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5552 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5553 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5554 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5555 return omit_one_operand (type, integer_zero_node, op0);
5557 /* ... fall through ... */
5559 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5560 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5561 /* If we can extract our operation from the LHS, do so and return a
5562 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5563 do something only if the second operand is a constant. */
5564 if (same_p
5565 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5566 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5567 fold_convert (ctype, op1));
5568 else if (tcode == MULT_EXPR && code == MULT_EXPR
5569 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5570 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5571 fold_convert (ctype, t1));
5572 else if (TREE_CODE (op1) != INTEGER_CST)
5573 return 0;
5575 /* If these are the same operation types, we can associate them
5576 assuming no overflow. */
5577 if (tcode == code
5578 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5579 fold_convert (ctype, c), 0))
5580 && ! TREE_OVERFLOW (t1))
5581 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5583 /* If these operations "cancel" each other, we have the main
5584 optimizations of this pass, which occur when either constant is a
5585 multiple of the other, in which case we replace this with either an
5586 operation or CODE or TCODE.
5588 If we have an unsigned type that is not a sizetype, we cannot do
5589 this since it will change the result if the original computation
5590 overflowed. */
5591 if ((! TYPE_UNSIGNED (ctype)
5592 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5593 && ! flag_wrapv
5594 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5595 || (tcode == MULT_EXPR
5596 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5597 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5599 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5600 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5601 fold_convert (ctype,
5602 const_binop (TRUNC_DIV_EXPR,
5603 op1, c, 0)));
5604 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5605 return fold_build2 (code, ctype, fold_convert (ctype, op0),
5606 fold_convert (ctype,
5607 const_binop (TRUNC_DIV_EXPR,
5608 c, op1, 0)));
5610 break;
5612 default:
5613 break;
5616 return 0;
5619 /* Return a node which has the indicated constant VALUE (either 0 or
5620 1), and is of the indicated TYPE. */
5622 tree
5623 constant_boolean_node (int value, tree type)
5625 if (type == integer_type_node)
5626 return value ? integer_one_node : integer_zero_node;
5627 else if (type == boolean_type_node)
5628 return value ? boolean_true_node : boolean_false_node;
5629 else
5630 return build_int_cst (type, value);
5634 /* Return true if expr looks like an ARRAY_REF and set base and
5635 offset to the appropriate trees. If there is no offset,
5636 offset is set to NULL_TREE. Base will be canonicalized to
5637 something you can get the element type from using
5638 TREE_TYPE (TREE_TYPE (base)). Offset will be the offset
5639 in bytes to the base. */
5641 static bool
5642 extract_array_ref (tree expr, tree *base, tree *offset)
5644 /* One canonical form is a PLUS_EXPR with the first
5645 argument being an ADDR_EXPR with a possible NOP_EXPR
5646 attached. */
5647 if (TREE_CODE (expr) == PLUS_EXPR)
5649 tree op0 = TREE_OPERAND (expr, 0);
5650 tree inner_base, dummy1;
5651 /* Strip NOP_EXPRs here because the C frontends and/or
5652 folders present us (int *)&x.a + 4B possibly. */
5653 STRIP_NOPS (op0);
5654 if (extract_array_ref (op0, &inner_base, &dummy1))
5656 *base = inner_base;
5657 if (dummy1 == NULL_TREE)
5658 *offset = TREE_OPERAND (expr, 1);
5659 else
5660 *offset = fold_build2 (PLUS_EXPR, TREE_TYPE (expr),
5661 dummy1, TREE_OPERAND (expr, 1));
5662 return true;
5665 /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
5666 which we transform into an ADDR_EXPR with appropriate
5667 offset. For other arguments to the ADDR_EXPR we assume
5668 zero offset and as such do not care about the ADDR_EXPR
5669 type and strip possible nops from it. */
5670 else if (TREE_CODE (expr) == ADDR_EXPR)
5672 tree op0 = TREE_OPERAND (expr, 0);
5673 if (TREE_CODE (op0) == ARRAY_REF)
5675 tree idx = TREE_OPERAND (op0, 1);
5676 *base = TREE_OPERAND (op0, 0);
5677 *offset = fold_build2 (MULT_EXPR, TREE_TYPE (idx), idx,
5678 array_ref_element_size (op0));
5680 else
5682 /* Handle array-to-pointer decay as &a. */
5683 if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE)
5684 *base = TREE_OPERAND (expr, 0);
5685 else
5686 *base = expr;
5687 *offset = NULL_TREE;
5689 return true;
5691 /* The next canonical form is a VAR_DECL with POINTER_TYPE. */
5692 else if (SSA_VAR_P (expr)
5693 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5695 *base = expr;
5696 *offset = NULL_TREE;
5697 return true;
5700 return false;
5704 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5705 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5706 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5707 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5708 COND is the first argument to CODE; otherwise (as in the example
5709 given here), it is the second argument. TYPE is the type of the
5710 original expression. Return NULL_TREE if no simplification is
5711 possible. */
5713 static tree
5714 fold_binary_op_with_conditional_arg (enum tree_code code,
5715 tree type, tree op0, tree op1,
5716 tree cond, tree arg, int cond_first_p)
5718 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5719 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5720 tree test, true_value, false_value;
5721 tree lhs = NULL_TREE;
5722 tree rhs = NULL_TREE;
5724 /* This transformation is only worthwhile if we don't have to wrap
5725 arg in a SAVE_EXPR, and the operation can be simplified on at least
5726 one of the branches once its pushed inside the COND_EXPR. */
5727 if (!TREE_CONSTANT (arg))
5728 return NULL_TREE;
5730 if (TREE_CODE (cond) == COND_EXPR)
5732 test = TREE_OPERAND (cond, 0);
5733 true_value = TREE_OPERAND (cond, 1);
5734 false_value = TREE_OPERAND (cond, 2);
5735 /* If this operand throws an expression, then it does not make
5736 sense to try to perform a logical or arithmetic operation
5737 involving it. */
5738 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5739 lhs = true_value;
5740 if (VOID_TYPE_P (TREE_TYPE (false_value)))
5741 rhs = false_value;
5743 else
5745 tree testtype = TREE_TYPE (cond);
5746 test = cond;
5747 true_value = constant_boolean_node (true, testtype);
5748 false_value = constant_boolean_node (false, testtype);
5751 arg = fold_convert (arg_type, arg);
5752 if (lhs == 0)
5754 true_value = fold_convert (cond_type, true_value);
5755 if (cond_first_p)
5756 lhs = fold_build2 (code, type, true_value, arg);
5757 else
5758 lhs = fold_build2 (code, type, arg, true_value);
5760 if (rhs == 0)
5762 false_value = fold_convert (cond_type, false_value);
5763 if (cond_first_p)
5764 rhs = fold_build2 (code, type, false_value, arg);
5765 else
5766 rhs = fold_build2 (code, type, arg, false_value);
5769 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
5770 return fold_convert (type, test);
5774 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5776 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5777 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
5778 ADDEND is the same as X.
5780 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5781 and finite. The problematic cases are when X is zero, and its mode
5782 has signed zeros. In the case of rounding towards -infinity,
5783 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
5784 modes, X + 0 is not the same as X because -0 + 0 is 0. */
5786 static bool
5787 fold_real_zero_addition_p (tree type, tree addend, int negate)
5789 if (!real_zerop (addend))
5790 return false;
5792 /* Don't allow the fold with -fsignaling-nans. */
5793 if (HONOR_SNANS (TYPE_MODE (type)))
5794 return false;
5796 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
5797 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5798 return true;
5800 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
5801 if (TREE_CODE (addend) == REAL_CST
5802 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5803 negate = !negate;
5805 /* The mode has signed zeros, and we have to honor their sign.
5806 In this situation, there is only one case we can return true for.
5807 X - 0 is the same as X unless rounding towards -infinity is
5808 supported. */
5809 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5812 /* Subroutine of fold() that checks comparisons of built-in math
5813 functions against real constants.
5815 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5816 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5817 is the type of the result and ARG0 and ARG1 are the operands of the
5818 comparison. ARG1 must be a TREE_REAL_CST.
5820 The function returns the constant folded tree if a simplification
5821 can be made, and NULL_TREE otherwise. */
5823 static tree
5824 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5825 tree type, tree arg0, tree arg1)
5827 REAL_VALUE_TYPE c;
5829 if (BUILTIN_SQRT_P (fcode))
5831 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5832 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5834 c = TREE_REAL_CST (arg1);
5835 if (REAL_VALUE_NEGATIVE (c))
5837 /* sqrt(x) < y is always false, if y is negative. */
5838 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5839 return omit_one_operand (type, integer_zero_node, arg);
5841 /* sqrt(x) > y is always true, if y is negative and we
5842 don't care about NaNs, i.e. negative values of x. */
5843 if (code == NE_EXPR || !HONOR_NANS (mode))
5844 return omit_one_operand (type, integer_one_node, arg);
5846 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5847 return fold_build2 (GE_EXPR, type, arg,
5848 build_real (TREE_TYPE (arg), dconst0));
5850 else if (code == GT_EXPR || code == GE_EXPR)
5852 REAL_VALUE_TYPE c2;
5854 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5855 real_convert (&c2, mode, &c2);
5857 if (REAL_VALUE_ISINF (c2))
5859 /* sqrt(x) > y is x == +Inf, when y is very large. */
5860 if (HONOR_INFINITIES (mode))
5861 return fold_build2 (EQ_EXPR, type, arg,
5862 build_real (TREE_TYPE (arg), c2));
5864 /* sqrt(x) > y is always false, when y is very large
5865 and we don't care about infinities. */
5866 return omit_one_operand (type, integer_zero_node, arg);
5869 /* sqrt(x) > c is the same as x > c*c. */
5870 return fold_build2 (code, type, arg,
5871 build_real (TREE_TYPE (arg), c2));
5873 else if (code == LT_EXPR || code == LE_EXPR)
5875 REAL_VALUE_TYPE c2;
5877 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5878 real_convert (&c2, mode, &c2);
5880 if (REAL_VALUE_ISINF (c2))
5882 /* sqrt(x) < y is always true, when y is a very large
5883 value and we don't care about NaNs or Infinities. */
5884 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5885 return omit_one_operand (type, integer_one_node, arg);
5887 /* sqrt(x) < y is x != +Inf when y is very large and we
5888 don't care about NaNs. */
5889 if (! HONOR_NANS (mode))
5890 return fold_build2 (NE_EXPR, type, arg,
5891 build_real (TREE_TYPE (arg), c2));
5893 /* sqrt(x) < y is x >= 0 when y is very large and we
5894 don't care about Infinities. */
5895 if (! HONOR_INFINITIES (mode))
5896 return fold_build2 (GE_EXPR, type, arg,
5897 build_real (TREE_TYPE (arg), dconst0));
5899 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5900 if (lang_hooks.decls.global_bindings_p () != 0
5901 || CONTAINS_PLACEHOLDER_P (arg))
5902 return NULL_TREE;
5904 arg = save_expr (arg);
5905 return fold_build2 (TRUTH_ANDIF_EXPR, type,
5906 fold_build2 (GE_EXPR, type, arg,
5907 build_real (TREE_TYPE (arg),
5908 dconst0)),
5909 fold_build2 (NE_EXPR, type, arg,
5910 build_real (TREE_TYPE (arg),
5911 c2)));
5914 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5915 if (! HONOR_NANS (mode))
5916 return fold_build2 (code, type, arg,
5917 build_real (TREE_TYPE (arg), c2));
5919 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5920 if (lang_hooks.decls.global_bindings_p () == 0
5921 && ! CONTAINS_PLACEHOLDER_P (arg))
5923 arg = save_expr (arg);
5924 return fold_build2 (TRUTH_ANDIF_EXPR, type,
5925 fold_build2 (GE_EXPR, type, arg,
5926 build_real (TREE_TYPE (arg),
5927 dconst0)),
5928 fold_build2 (code, type, arg,
5929 build_real (TREE_TYPE (arg),
5930 c2)));
5935 return NULL_TREE;
5938 /* Subroutine of fold() that optimizes comparisons against Infinities,
5939 either +Inf or -Inf.
5941 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5942 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5943 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5945 The function returns the constant folded tree if a simplification
5946 can be made, and NULL_TREE otherwise. */
5948 static tree
5949 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5951 enum machine_mode mode;
5952 REAL_VALUE_TYPE max;
5953 tree temp;
5954 bool neg;
5956 mode = TYPE_MODE (TREE_TYPE (arg0));
5958 /* For negative infinity swap the sense of the comparison. */
5959 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5960 if (neg)
5961 code = swap_tree_comparison (code);
5963 switch (code)
5965 case GT_EXPR:
5966 /* x > +Inf is always false, if with ignore sNANs. */
5967 if (HONOR_SNANS (mode))
5968 return NULL_TREE;
5969 return omit_one_operand (type, integer_zero_node, arg0);
5971 case LE_EXPR:
5972 /* x <= +Inf is always true, if we don't case about NaNs. */
5973 if (! HONOR_NANS (mode))
5974 return omit_one_operand (type, integer_one_node, arg0);
5976 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5977 if (lang_hooks.decls.global_bindings_p () == 0
5978 && ! CONTAINS_PLACEHOLDER_P (arg0))
5980 arg0 = save_expr (arg0);
5981 return fold_build2 (EQ_EXPR, type, arg0, arg0);
5983 break;
5985 case EQ_EXPR:
5986 case GE_EXPR:
5987 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5988 real_maxval (&max, neg, mode);
5989 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
5990 arg0, build_real (TREE_TYPE (arg0), max));
5992 case LT_EXPR:
5993 /* x < +Inf is always equal to x <= DBL_MAX. */
5994 real_maxval (&max, neg, mode);
5995 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
5996 arg0, build_real (TREE_TYPE (arg0), max));
5998 case NE_EXPR:
5999 /* x != +Inf is always equal to !(x > DBL_MAX). */
6000 real_maxval (&max, neg, mode);
6001 if (! HONOR_NANS (mode))
6002 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6003 arg0, build_real (TREE_TYPE (arg0), max));
6005 /* The transformation below creates non-gimple code and thus is
6006 not appropriate if we are in gimple form. */
6007 if (in_gimple_form)
6008 return NULL_TREE;
6010 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6011 arg0, build_real (TREE_TYPE (arg0), max));
6012 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6014 default:
6015 break;
6018 return NULL_TREE;
6021 /* Subroutine of fold() that optimizes comparisons of a division by
6022 a nonzero integer constant against an integer constant, i.e.
6023 X/C1 op C2.
6025 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6026 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6027 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6029 The function returns the constant folded tree if a simplification
6030 can be made, and NULL_TREE otherwise. */
6032 static tree
6033 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6035 tree prod, tmp, hi, lo;
6036 tree arg00 = TREE_OPERAND (arg0, 0);
6037 tree arg01 = TREE_OPERAND (arg0, 1);
6038 unsigned HOST_WIDE_INT lpart;
6039 HOST_WIDE_INT hpart;
6040 bool neg_overflow;
6041 int overflow;
6043 /* We have to do this the hard way to detect unsigned overflow.
6044 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6045 overflow = mul_double (TREE_INT_CST_LOW (arg01),
6046 TREE_INT_CST_HIGH (arg01),
6047 TREE_INT_CST_LOW (arg1),
6048 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
6049 prod = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
6050 prod = force_fit_type (prod, -1, overflow, false);
6051 neg_overflow = false;
6053 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
6055 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
6056 lo = prod;
6058 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6059 overflow = add_double (TREE_INT_CST_LOW (prod),
6060 TREE_INT_CST_HIGH (prod),
6061 TREE_INT_CST_LOW (tmp),
6062 TREE_INT_CST_HIGH (tmp),
6063 &lpart, &hpart);
6064 hi = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
6065 hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
6066 TREE_CONSTANT_OVERFLOW (prod));
6068 else if (tree_int_cst_sgn (arg01) >= 0)
6070 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
6071 switch (tree_int_cst_sgn (arg1))
6073 case -1:
6074 neg_overflow = true;
6075 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6076 hi = prod;
6077 break;
6079 case 0:
6080 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6081 hi = tmp;
6082 break;
6084 case 1:
6085 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6086 lo = prod;
6087 break;
6089 default:
6090 gcc_unreachable ();
6093 else
6095 /* A negative divisor reverses the relational operators. */
6096 code = swap_tree_comparison (code);
6098 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
6099 switch (tree_int_cst_sgn (arg1))
6101 case -1:
6102 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6103 lo = prod;
6104 break;
6106 case 0:
6107 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6108 lo = tmp;
6109 break;
6111 case 1:
6112 neg_overflow = true;
6113 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6114 hi = prod;
6115 break;
6117 default:
6118 gcc_unreachable ();
6122 switch (code)
6124 case EQ_EXPR:
6125 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6126 return omit_one_operand (type, integer_zero_node, arg00);
6127 if (TREE_OVERFLOW (hi))
6128 return fold_build2 (GE_EXPR, type, arg00, lo);
6129 if (TREE_OVERFLOW (lo))
6130 return fold_build2 (LE_EXPR, type, arg00, hi);
6131 return build_range_check (type, arg00, 1, lo, hi);
6133 case NE_EXPR:
6134 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6135 return omit_one_operand (type, integer_one_node, arg00);
6136 if (TREE_OVERFLOW (hi))
6137 return fold_build2 (LT_EXPR, type, arg00, lo);
6138 if (TREE_OVERFLOW (lo))
6139 return fold_build2 (GT_EXPR, type, arg00, hi);
6140 return build_range_check (type, arg00, 0, lo, hi);
6142 case LT_EXPR:
6143 if (TREE_OVERFLOW (lo))
6145 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6146 return omit_one_operand (type, tmp, arg00);
6148 return fold_build2 (LT_EXPR, type, arg00, lo);
6150 case LE_EXPR:
6151 if (TREE_OVERFLOW (hi))
6153 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6154 return omit_one_operand (type, tmp, arg00);
6156 return fold_build2 (LE_EXPR, type, arg00, hi);
6158 case GT_EXPR:
6159 if (TREE_OVERFLOW (hi))
6161 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6162 return omit_one_operand (type, tmp, arg00);
6164 return fold_build2 (GT_EXPR, type, arg00, hi);
6166 case GE_EXPR:
6167 if (TREE_OVERFLOW (lo))
6169 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6170 return omit_one_operand (type, tmp, arg00);
6172 return fold_build2 (GE_EXPR, type, arg00, lo);
6174 default:
6175 break;
6178 return NULL_TREE;
6182 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6183 equality/inequality test, then return a simplified form of the test
6184 using a sign testing. Otherwise return NULL. TYPE is the desired
6185 result type. */
6187 static tree
6188 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6189 tree result_type)
6191 /* If this is testing a single bit, we can optimize the test. */
6192 if ((code == NE_EXPR || code == EQ_EXPR)
6193 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6194 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6196 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6197 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6198 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6200 if (arg00 != NULL_TREE
6201 /* This is only a win if casting to a signed type is cheap,
6202 i.e. when arg00's type is not a partial mode. */
6203 && TYPE_PRECISION (TREE_TYPE (arg00))
6204 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6206 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
6207 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6208 result_type, fold_convert (stype, arg00),
6209 build_int_cst (stype, 0));
6213 return NULL_TREE;
6216 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6217 equality/inequality test, then return a simplified form of
6218 the test using shifts and logical operations. Otherwise return
6219 NULL. TYPE is the desired result type. */
6221 tree
6222 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6223 tree result_type)
6225 /* If this is testing a single bit, we can optimize the test. */
6226 if ((code == NE_EXPR || code == EQ_EXPR)
6227 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6228 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6230 tree inner = TREE_OPERAND (arg0, 0);
6231 tree type = TREE_TYPE (arg0);
6232 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6233 enum machine_mode operand_mode = TYPE_MODE (type);
6234 int ops_unsigned;
6235 tree signed_type, unsigned_type, intermediate_type;
6236 tree tem;
6238 /* First, see if we can fold the single bit test into a sign-bit
6239 test. */
6240 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6241 result_type);
6242 if (tem)
6243 return tem;
6245 /* Otherwise we have (A & C) != 0 where C is a single bit,
6246 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6247 Similarly for (A & C) == 0. */
6249 /* If INNER is a right shift of a constant and it plus BITNUM does
6250 not overflow, adjust BITNUM and INNER. */
6251 if (TREE_CODE (inner) == RSHIFT_EXPR
6252 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6253 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6254 && bitnum < TYPE_PRECISION (type)
6255 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6256 bitnum - TYPE_PRECISION (type)))
6258 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6259 inner = TREE_OPERAND (inner, 0);
6262 /* If we are going to be able to omit the AND below, we must do our
6263 operations as unsigned. If we must use the AND, we have a choice.
6264 Normally unsigned is faster, but for some machines signed is. */
6265 #ifdef LOAD_EXTEND_OP
6266 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6267 && !flag_syntax_only) ? 0 : 1;
6268 #else
6269 ops_unsigned = 1;
6270 #endif
6272 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6273 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6274 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6275 inner = fold_convert (intermediate_type, inner);
6277 if (bitnum != 0)
6278 inner = build2 (RSHIFT_EXPR, intermediate_type,
6279 inner, size_int (bitnum));
6281 if (code == EQ_EXPR)
6282 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type,
6283 inner, integer_one_node);
6285 /* Put the AND last so it can combine with more things. */
6286 inner = build2 (BIT_AND_EXPR, intermediate_type,
6287 inner, integer_one_node);
6289 /* Make sure to return the proper type. */
6290 inner = fold_convert (result_type, inner);
6292 return inner;
6294 return NULL_TREE;
6297 /* Check whether we are allowed to reorder operands arg0 and arg1,
6298 such that the evaluation of arg1 occurs before arg0. */
6300 static bool
6301 reorder_operands_p (tree arg0, tree arg1)
6303 if (! flag_evaluation_order)
6304 return true;
6305 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6306 return true;
6307 return ! TREE_SIDE_EFFECTS (arg0)
6308 && ! TREE_SIDE_EFFECTS (arg1);
6311 /* Test whether it is preferable two swap two operands, ARG0 and
6312 ARG1, for example because ARG0 is an integer constant and ARG1
6313 isn't. If REORDER is true, only recommend swapping if we can
6314 evaluate the operands in reverse order. */
6316 bool
6317 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
6319 STRIP_SIGN_NOPS (arg0);
6320 STRIP_SIGN_NOPS (arg1);
6322 if (TREE_CODE (arg1) == INTEGER_CST)
6323 return 0;
6324 if (TREE_CODE (arg0) == INTEGER_CST)
6325 return 1;
6327 if (TREE_CODE (arg1) == REAL_CST)
6328 return 0;
6329 if (TREE_CODE (arg0) == REAL_CST)
6330 return 1;
6332 if (TREE_CODE (arg1) == COMPLEX_CST)
6333 return 0;
6334 if (TREE_CODE (arg0) == COMPLEX_CST)
6335 return 1;
6337 if (TREE_CONSTANT (arg1))
6338 return 0;
6339 if (TREE_CONSTANT (arg0))
6340 return 1;
6342 if (optimize_size)
6343 return 0;
6345 if (reorder && flag_evaluation_order
6346 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6347 return 0;
6349 if (DECL_P (arg1))
6350 return 0;
6351 if (DECL_P (arg0))
6352 return 1;
6354 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6355 for commutative and comparison operators. Ensuring a canonical
6356 form allows the optimizers to find additional redundancies without
6357 having to explicitly check for both orderings. */
6358 if (TREE_CODE (arg0) == SSA_NAME
6359 && TREE_CODE (arg1) == SSA_NAME
6360 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6361 return 1;
6363 return 0;
6366 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6367 ARG0 is extended to a wider type. */
6369 static tree
6370 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6372 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6373 tree arg1_unw;
6374 tree shorter_type, outer_type;
6375 tree min, max;
6376 bool above, below;
6378 if (arg0_unw == arg0)
6379 return NULL_TREE;
6380 shorter_type = TREE_TYPE (arg0_unw);
6382 #ifdef HAVE_canonicalize_funcptr_for_compare
6383 /* Disable this optimization if we're casting a function pointer
6384 type on targets that require function pointer canonicalization. */
6385 if (HAVE_canonicalize_funcptr_for_compare
6386 && TREE_CODE (shorter_type) == POINTER_TYPE
6387 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6388 return NULL_TREE;
6389 #endif
6391 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6392 return NULL_TREE;
6394 arg1_unw = get_unwidened (arg1, shorter_type);
6396 /* If possible, express the comparison in the shorter mode. */
6397 if ((code == EQ_EXPR || code == NE_EXPR
6398 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6399 && (TREE_TYPE (arg1_unw) == shorter_type
6400 || (TREE_CODE (arg1_unw) == INTEGER_CST
6401 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6402 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6403 && int_fits_type_p (arg1_unw, shorter_type))))
6404 return fold_build2 (code, type, arg0_unw,
6405 fold_convert (shorter_type, arg1_unw));
6407 if (TREE_CODE (arg1_unw) != INTEGER_CST
6408 || TREE_CODE (shorter_type) != INTEGER_TYPE
6409 || !int_fits_type_p (arg1_unw, shorter_type))
6410 return NULL_TREE;
6412 /* If we are comparing with the integer that does not fit into the range
6413 of the shorter type, the result is known. */
6414 outer_type = TREE_TYPE (arg1_unw);
6415 min = lower_bound_in_type (outer_type, shorter_type);
6416 max = upper_bound_in_type (outer_type, shorter_type);
6418 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6419 max, arg1_unw));
6420 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6421 arg1_unw, min));
6423 switch (code)
6425 case EQ_EXPR:
6426 if (above || below)
6427 return omit_one_operand (type, integer_zero_node, arg0);
6428 break;
6430 case NE_EXPR:
6431 if (above || below)
6432 return omit_one_operand (type, integer_one_node, arg0);
6433 break;
6435 case LT_EXPR:
6436 case LE_EXPR:
6437 if (above)
6438 return omit_one_operand (type, integer_one_node, arg0);
6439 else if (below)
6440 return omit_one_operand (type, integer_zero_node, arg0);
6442 case GT_EXPR:
6443 case GE_EXPR:
6444 if (above)
6445 return omit_one_operand (type, integer_zero_node, arg0);
6446 else if (below)
6447 return omit_one_operand (type, integer_one_node, arg0);
6449 default:
6450 break;
6453 return NULL_TREE;
6456 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6457 ARG0 just the signedness is changed. */
6459 static tree
6460 fold_sign_changed_comparison (enum tree_code code, tree type,
6461 tree arg0, tree arg1)
6463 tree arg0_inner, tmp;
6464 tree inner_type, outer_type;
6466 if (TREE_CODE (arg0) != NOP_EXPR
6467 && TREE_CODE (arg0) != CONVERT_EXPR)
6468 return NULL_TREE;
6470 outer_type = TREE_TYPE (arg0);
6471 arg0_inner = TREE_OPERAND (arg0, 0);
6472 inner_type = TREE_TYPE (arg0_inner);
6474 #ifdef HAVE_canonicalize_funcptr_for_compare
6475 /* Disable this optimization if we're casting a function pointer
6476 type on targets that require function pointer canonicalization. */
6477 if (HAVE_canonicalize_funcptr_for_compare
6478 && TREE_CODE (inner_type) == POINTER_TYPE
6479 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6480 return NULL_TREE;
6481 #endif
6483 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6484 return NULL_TREE;
6486 if (TREE_CODE (arg1) != INTEGER_CST
6487 && !((TREE_CODE (arg1) == NOP_EXPR
6488 || TREE_CODE (arg1) == CONVERT_EXPR)
6489 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6490 return NULL_TREE;
6492 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6493 && code != NE_EXPR
6494 && code != EQ_EXPR)
6495 return NULL_TREE;
6497 if (TREE_CODE (arg1) == INTEGER_CST)
6499 tmp = build_int_cst_wide (inner_type,
6500 TREE_INT_CST_LOW (arg1),
6501 TREE_INT_CST_HIGH (arg1));
6502 arg1 = force_fit_type (tmp, 0,
6503 TREE_OVERFLOW (arg1),
6504 TREE_CONSTANT_OVERFLOW (arg1));
6506 else
6507 arg1 = fold_convert (inner_type, arg1);
6509 return fold_build2 (code, type, arg0_inner, arg1);
6512 /* Tries to replace &a[idx] CODE s * delta with &a[idx CODE delta], if s is
6513 step of the array. Reconstructs s and delta in the case of s * delta
6514 being an integer constant (and thus already folded).
6515 ADDR is the address. MULT is the multiplicative expression.
6516 If the function succeeds, the new address expression is returned. Otherwise
6517 NULL_TREE is returned. */
6519 static tree
6520 try_move_mult_to_index (enum tree_code code, tree addr, tree op1)
6522 tree s, delta, step;
6523 tree ref = TREE_OPERAND (addr, 0), pref;
6524 tree ret, pos;
6525 tree itype;
6527 /* Canonicalize op1 into a possibly non-constant delta
6528 and an INTEGER_CST s. */
6529 if (TREE_CODE (op1) == MULT_EXPR)
6531 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6533 STRIP_NOPS (arg0);
6534 STRIP_NOPS (arg1);
6536 if (TREE_CODE (arg0) == INTEGER_CST)
6538 s = arg0;
6539 delta = arg1;
6541 else if (TREE_CODE (arg1) == INTEGER_CST)
6543 s = arg1;
6544 delta = arg0;
6546 else
6547 return NULL_TREE;
6549 else if (TREE_CODE (op1) == INTEGER_CST)
6551 delta = op1;
6552 s = NULL_TREE;
6554 else
6556 /* Simulate we are delta * 1. */
6557 delta = op1;
6558 s = integer_one_node;
6561 for (;; ref = TREE_OPERAND (ref, 0))
6563 if (TREE_CODE (ref) == ARRAY_REF)
6565 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6566 if (! itype)
6567 continue;
6569 step = array_ref_element_size (ref);
6570 if (TREE_CODE (step) != INTEGER_CST)
6571 continue;
6573 if (s)
6575 if (! tree_int_cst_equal (step, s))
6576 continue;
6578 else
6580 /* Try if delta is a multiple of step. */
6581 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6582 if (! tmp)
6583 continue;
6584 delta = tmp;
6587 break;
6590 if (!handled_component_p (ref))
6591 return NULL_TREE;
6594 /* We found the suitable array reference. So copy everything up to it,
6595 and replace the index. */
6597 pref = TREE_OPERAND (addr, 0);
6598 ret = copy_node (pref);
6599 pos = ret;
6601 while (pref != ref)
6603 pref = TREE_OPERAND (pref, 0);
6604 TREE_OPERAND (pos, 0) = copy_node (pref);
6605 pos = TREE_OPERAND (pos, 0);
6608 TREE_OPERAND (pos, 1) = fold_build2 (code, itype,
6609 fold_convert (itype,
6610 TREE_OPERAND (pos, 1)),
6611 fold_convert (itype, delta));
6613 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6617 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6618 means A >= Y && A != MAX, but in this case we know that
6619 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6621 static tree
6622 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6624 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6626 if (TREE_CODE (bound) == LT_EXPR)
6627 a = TREE_OPERAND (bound, 0);
6628 else if (TREE_CODE (bound) == GT_EXPR)
6629 a = TREE_OPERAND (bound, 1);
6630 else
6631 return NULL_TREE;
6633 typea = TREE_TYPE (a);
6634 if (!INTEGRAL_TYPE_P (typea)
6635 && !POINTER_TYPE_P (typea))
6636 return NULL_TREE;
6638 if (TREE_CODE (ineq) == LT_EXPR)
6640 a1 = TREE_OPERAND (ineq, 1);
6641 y = TREE_OPERAND (ineq, 0);
6643 else if (TREE_CODE (ineq) == GT_EXPR)
6645 a1 = TREE_OPERAND (ineq, 0);
6646 y = TREE_OPERAND (ineq, 1);
6648 else
6649 return NULL_TREE;
6651 if (TREE_TYPE (a1) != typea)
6652 return NULL_TREE;
6654 diff = fold_build2 (MINUS_EXPR, typea, a1, a);
6655 if (!integer_onep (diff))
6656 return NULL_TREE;
6658 return fold_build2 (GE_EXPR, type, a, y);
6661 /* Fold a sum or difference of at least one multiplication.
6662 Returns the folded tree or NULL if no simplification could be made. */
6664 static tree
6665 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
6667 tree arg00, arg01, arg10, arg11;
6668 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6670 /* (A * C) +- (B * C) -> (A+-B) * C.
6671 (A * C) +- A -> A * (C+-1).
6672 We are most concerned about the case where C is a constant,
6673 but other combinations show up during loop reduction. Since
6674 it is not difficult, try all four possibilities. */
6676 if (TREE_CODE (arg0) == MULT_EXPR)
6678 arg00 = TREE_OPERAND (arg0, 0);
6679 arg01 = TREE_OPERAND (arg0, 1);
6681 else
6683 arg00 = arg0;
6684 if (!FLOAT_TYPE_P (type))
6685 arg01 = build_int_cst (type, 1);
6686 else
6687 arg01 = build_real (type, dconst1);
6689 if (TREE_CODE (arg1) == MULT_EXPR)
6691 arg10 = TREE_OPERAND (arg1, 0);
6692 arg11 = TREE_OPERAND (arg1, 1);
6694 else
6696 arg10 = arg1;
6697 if (!FLOAT_TYPE_P (type))
6698 arg11 = build_int_cst (type, 1);
6699 else
6700 arg11 = build_real (type, dconst1);
6702 same = NULL_TREE;
6704 if (operand_equal_p (arg01, arg11, 0))
6705 same = arg01, alt0 = arg00, alt1 = arg10;
6706 else if (operand_equal_p (arg00, arg10, 0))
6707 same = arg00, alt0 = arg01, alt1 = arg11;
6708 else if (operand_equal_p (arg00, arg11, 0))
6709 same = arg00, alt0 = arg01, alt1 = arg10;
6710 else if (operand_equal_p (arg01, arg10, 0))
6711 same = arg01, alt0 = arg00, alt1 = arg11;
6713 /* No identical multiplicands; see if we can find a common
6714 power-of-two factor in non-power-of-two multiplies. This
6715 can help in multi-dimensional array access. */
6716 else if (host_integerp (arg01, 0)
6717 && host_integerp (arg11, 0))
6719 HOST_WIDE_INT int01, int11, tmp;
6720 bool swap = false;
6721 tree maybe_same;
6722 int01 = TREE_INT_CST_LOW (arg01);
6723 int11 = TREE_INT_CST_LOW (arg11);
6725 /* Move min of absolute values to int11. */
6726 if ((int01 >= 0 ? int01 : -int01)
6727 < (int11 >= 0 ? int11 : -int11))
6729 tmp = int01, int01 = int11, int11 = tmp;
6730 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6731 maybe_same = arg01;
6732 swap = true;
6734 else
6735 maybe_same = arg11;
6737 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6739 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
6740 build_int_cst (TREE_TYPE (arg00),
6741 int01 / int11));
6742 alt1 = arg10;
6743 same = maybe_same;
6744 if (swap)
6745 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6749 if (same)
6750 return fold_build2 (MULT_EXPR, type,
6751 fold_build2 (code, type,
6752 fold_convert (type, alt0),
6753 fold_convert (type, alt1)),
6754 fold_convert (type, same));
6756 return NULL_TREE;
6759 /* Fold a unary expression of code CODE and type TYPE with operand
6760 OP0. Return the folded expression if folding is successful.
6761 Otherwise, return NULL_TREE. */
6763 tree
6764 fold_unary (enum tree_code code, tree type, tree op0)
6766 tree tem;
6767 tree arg0;
6768 enum tree_code_class kind = TREE_CODE_CLASS (code);
6770 gcc_assert (IS_EXPR_CODE_CLASS (kind)
6771 && TREE_CODE_LENGTH (code) == 1);
6773 arg0 = op0;
6774 if (arg0)
6776 if (code == NOP_EXPR || code == CONVERT_EXPR
6777 || code == FLOAT_EXPR || code == ABS_EXPR)
6779 /* Don't use STRIP_NOPS, because signedness of argument type
6780 matters. */
6781 STRIP_SIGN_NOPS (arg0);
6783 else
6785 /* Strip any conversions that don't change the mode. This
6786 is safe for every expression, except for a comparison
6787 expression because its signedness is derived from its
6788 operands.
6790 Note that this is done as an internal manipulation within
6791 the constant folder, in order to find the simplest
6792 representation of the arguments so that their form can be
6793 studied. In any cases, the appropriate type conversions
6794 should be put back in the tree that will get out of the
6795 constant folder. */
6796 STRIP_NOPS (arg0);
6800 if (TREE_CODE_CLASS (code) == tcc_unary)
6802 if (TREE_CODE (arg0) == COMPOUND_EXPR)
6803 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6804 fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
6805 else if (TREE_CODE (arg0) == COND_EXPR)
6807 tree arg01 = TREE_OPERAND (arg0, 1);
6808 tree arg02 = TREE_OPERAND (arg0, 2);
6809 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
6810 arg01 = fold_build1 (code, type, arg01);
6811 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
6812 arg02 = fold_build1 (code, type, arg02);
6813 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
6814 arg01, arg02);
6816 /* If this was a conversion, and all we did was to move into
6817 inside the COND_EXPR, bring it back out. But leave it if
6818 it is a conversion from integer to integer and the
6819 result precision is no wider than a word since such a
6820 conversion is cheap and may be optimized away by combine,
6821 while it couldn't if it were outside the COND_EXPR. Then return
6822 so we don't get into an infinite recursion loop taking the
6823 conversion out and then back in. */
6825 if ((code == NOP_EXPR || code == CONVERT_EXPR
6826 || code == NON_LVALUE_EXPR)
6827 && TREE_CODE (tem) == COND_EXPR
6828 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
6829 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
6830 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
6831 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
6832 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
6833 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
6834 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
6835 && (INTEGRAL_TYPE_P
6836 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
6837 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
6838 || flag_syntax_only))
6839 tem = build1 (code, type,
6840 build3 (COND_EXPR,
6841 TREE_TYPE (TREE_OPERAND
6842 (TREE_OPERAND (tem, 1), 0)),
6843 TREE_OPERAND (tem, 0),
6844 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
6845 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
6846 return tem;
6848 else if (COMPARISON_CLASS_P (arg0))
6850 if (TREE_CODE (type) == BOOLEAN_TYPE)
6852 arg0 = copy_node (arg0);
6853 TREE_TYPE (arg0) = type;
6854 return arg0;
6856 else if (TREE_CODE (type) != INTEGER_TYPE)
6857 return fold_build3 (COND_EXPR, type, arg0,
6858 fold_build1 (code, type,
6859 integer_one_node),
6860 fold_build1 (code, type,
6861 integer_zero_node));
6865 switch (code)
6867 case NOP_EXPR:
6868 case FLOAT_EXPR:
6869 case CONVERT_EXPR:
6870 case FIX_TRUNC_EXPR:
6871 case FIX_CEIL_EXPR:
6872 case FIX_FLOOR_EXPR:
6873 case FIX_ROUND_EXPR:
6874 if (TREE_TYPE (op0) == type)
6875 return op0;
6877 /* If we have (type) (a CMP b) and type is an integral type, return
6878 new expression involving the new type. */
6879 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
6880 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
6881 TREE_OPERAND (op0, 1));
6883 /* Handle cases of two conversions in a row. */
6884 if (TREE_CODE (op0) == NOP_EXPR
6885 || TREE_CODE (op0) == CONVERT_EXPR)
6887 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
6888 tree inter_type = TREE_TYPE (op0);
6889 int inside_int = INTEGRAL_TYPE_P (inside_type);
6890 int inside_ptr = POINTER_TYPE_P (inside_type);
6891 int inside_float = FLOAT_TYPE_P (inside_type);
6892 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
6893 unsigned int inside_prec = TYPE_PRECISION (inside_type);
6894 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
6895 int inter_int = INTEGRAL_TYPE_P (inter_type);
6896 int inter_ptr = POINTER_TYPE_P (inter_type);
6897 int inter_float = FLOAT_TYPE_P (inter_type);
6898 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
6899 unsigned int inter_prec = TYPE_PRECISION (inter_type);
6900 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
6901 int final_int = INTEGRAL_TYPE_P (type);
6902 int final_ptr = POINTER_TYPE_P (type);
6903 int final_float = FLOAT_TYPE_P (type);
6904 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
6905 unsigned int final_prec = TYPE_PRECISION (type);
6906 int final_unsignedp = TYPE_UNSIGNED (type);
6908 /* In addition to the cases of two conversions in a row
6909 handled below, if we are converting something to its own
6910 type via an object of identical or wider precision, neither
6911 conversion is needed. */
6912 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
6913 && ((inter_int && final_int) || (inter_float && final_float))
6914 && inter_prec >= final_prec)
6915 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6917 /* Likewise, if the intermediate and final types are either both
6918 float or both integer, we don't need the middle conversion if
6919 it is wider than the final type and doesn't change the signedness
6920 (for integers). Avoid this if the final type is a pointer
6921 since then we sometimes need the inner conversion. Likewise if
6922 the outer has a precision not equal to the size of its mode. */
6923 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
6924 || (inter_float && inside_float)
6925 || (inter_vec && inside_vec))
6926 && inter_prec >= inside_prec
6927 && (inter_float || inter_vec
6928 || inter_unsignedp == inside_unsignedp)
6929 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6930 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6931 && ! final_ptr
6932 && (! final_vec || inter_prec == inside_prec))
6933 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6935 /* If we have a sign-extension of a zero-extended value, we can
6936 replace that by a single zero-extension. */
6937 if (inside_int && inter_int && final_int
6938 && inside_prec < inter_prec && inter_prec < final_prec
6939 && inside_unsignedp && !inter_unsignedp)
6940 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6942 /* Two conversions in a row are not needed unless:
6943 - some conversion is floating-point (overstrict for now), or
6944 - some conversion is a vector (overstrict for now), or
6945 - the intermediate type is narrower than both initial and
6946 final, or
6947 - the intermediate type and innermost type differ in signedness,
6948 and the outermost type is wider than the intermediate, or
6949 - the initial type is a pointer type and the precisions of the
6950 intermediate and final types differ, or
6951 - the final type is a pointer type and the precisions of the
6952 initial and intermediate types differ. */
6953 if (! inside_float && ! inter_float && ! final_float
6954 && ! inside_vec && ! inter_vec && ! final_vec
6955 && (inter_prec > inside_prec || inter_prec > final_prec)
6956 && ! (inside_int && inter_int
6957 && inter_unsignedp != inside_unsignedp
6958 && inter_prec < final_prec)
6959 && ((inter_unsignedp && inter_prec > inside_prec)
6960 == (final_unsignedp && final_prec > inter_prec))
6961 && ! (inside_ptr && inter_prec != final_prec)
6962 && ! (final_ptr && inside_prec != inter_prec)
6963 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6964 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6965 && ! final_ptr)
6966 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6969 /* Handle (T *)&A.B.C for A being of type T and B and C
6970 living at offset zero. This occurs frequently in
6971 C++ upcasting and then accessing the base. */
6972 if (TREE_CODE (op0) == ADDR_EXPR
6973 && POINTER_TYPE_P (type)
6974 && handled_component_p (TREE_OPERAND (op0, 0)))
6976 HOST_WIDE_INT bitsize, bitpos;
6977 tree offset;
6978 enum machine_mode mode;
6979 int unsignedp, volatilep;
6980 tree base = TREE_OPERAND (op0, 0);
6981 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
6982 &mode, &unsignedp, &volatilep, false);
6983 /* If the reference was to a (constant) zero offset, we can use
6984 the address of the base if it has the same base type
6985 as the result type. */
6986 if (! offset && bitpos == 0
6987 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
6988 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
6989 return fold_convert (type, build_fold_addr_expr (base));
6992 if (TREE_CODE (op0) == MODIFY_EXPR
6993 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
6994 /* Detect assigning a bitfield. */
6995 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
6996 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
6998 /* Don't leave an assignment inside a conversion
6999 unless assigning a bitfield. */
7000 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
7001 /* First do the assignment, then return converted constant. */
7002 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7003 TREE_NO_WARNING (tem) = 1;
7004 TREE_USED (tem) = 1;
7005 return tem;
7008 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7009 constants (if x has signed type, the sign bit cannot be set
7010 in c). This folds extension into the BIT_AND_EXPR. */
7011 if (INTEGRAL_TYPE_P (type)
7012 && TREE_CODE (type) != BOOLEAN_TYPE
7013 && TREE_CODE (op0) == BIT_AND_EXPR
7014 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7016 tree and = op0;
7017 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7018 int change = 0;
7020 if (TYPE_UNSIGNED (TREE_TYPE (and))
7021 || (TYPE_PRECISION (type)
7022 <= TYPE_PRECISION (TREE_TYPE (and))))
7023 change = 1;
7024 else if (TYPE_PRECISION (TREE_TYPE (and1))
7025 <= HOST_BITS_PER_WIDE_INT
7026 && host_integerp (and1, 1))
7028 unsigned HOST_WIDE_INT cst;
7030 cst = tree_low_cst (and1, 1);
7031 cst &= (HOST_WIDE_INT) -1
7032 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7033 change = (cst == 0);
7034 #ifdef LOAD_EXTEND_OP
7035 if (change
7036 && !flag_syntax_only
7037 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7038 == ZERO_EXTEND))
7040 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
7041 and0 = fold_convert (uns, and0);
7042 and1 = fold_convert (uns, and1);
7044 #endif
7046 if (change)
7048 tem = build_int_cst_wide (type, TREE_INT_CST_LOW (and1),
7049 TREE_INT_CST_HIGH (and1));
7050 tem = force_fit_type (tem, 0, TREE_OVERFLOW (and1),
7051 TREE_CONSTANT_OVERFLOW (and1));
7052 return fold_build2 (BIT_AND_EXPR, type,
7053 fold_convert (type, and0), tem);
7057 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
7058 T2 being pointers to types of the same size. */
7059 if (POINTER_TYPE_P (type)
7060 && BINARY_CLASS_P (arg0)
7061 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7062 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7064 tree arg00 = TREE_OPERAND (arg0, 0);
7065 tree t0 = type;
7066 tree t1 = TREE_TYPE (arg00);
7067 tree tt0 = TREE_TYPE (t0);
7068 tree tt1 = TREE_TYPE (t1);
7069 tree s0 = TYPE_SIZE (tt0);
7070 tree s1 = TYPE_SIZE (tt1);
7072 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
7073 return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
7074 TREE_OPERAND (arg0, 1));
7077 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7078 of the same precision, and X is a integer type not narrower than
7079 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7080 if (INTEGRAL_TYPE_P (type)
7081 && TREE_CODE (op0) == BIT_NOT_EXPR
7082 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7083 && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7084 || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7085 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7087 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7088 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7089 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7090 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7093 tem = fold_convert_const (code, type, arg0);
7094 return tem ? tem : NULL_TREE;
7096 case VIEW_CONVERT_EXPR:
7097 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7098 return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7099 return NULL_TREE;
7101 case NEGATE_EXPR:
7102 if (negate_expr_p (arg0))
7103 return fold_convert (type, negate_expr (arg0));
7104 return NULL_TREE;
7106 case ABS_EXPR:
7107 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7108 return fold_abs_const (arg0, type);
7109 else if (TREE_CODE (arg0) == NEGATE_EXPR)
7110 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7111 /* Convert fabs((double)float) into (double)fabsf(float). */
7112 else if (TREE_CODE (arg0) == NOP_EXPR
7113 && TREE_CODE (type) == REAL_TYPE)
7115 tree targ0 = strip_float_extensions (arg0);
7116 if (targ0 != arg0)
7117 return fold_convert (type, fold_build1 (ABS_EXPR,
7118 TREE_TYPE (targ0),
7119 targ0));
7121 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
7122 else if (tree_expr_nonnegative_p (arg0) || TREE_CODE (arg0) == ABS_EXPR)
7123 return arg0;
7125 /* Strip sign ops from argument. */
7126 if (TREE_CODE (type) == REAL_TYPE)
7128 tem = fold_strip_sign_ops (arg0);
7129 if (tem)
7130 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
7132 return NULL_TREE;
7134 case CONJ_EXPR:
7135 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7136 return fold_convert (type, arg0);
7137 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7138 return build2 (COMPLEX_EXPR, type,
7139 TREE_OPERAND (arg0, 0),
7140 negate_expr (TREE_OPERAND (arg0, 1)));
7141 else if (TREE_CODE (arg0) == COMPLEX_CST)
7142 return build_complex (type, TREE_REALPART (arg0),
7143 negate_expr (TREE_IMAGPART (arg0)));
7144 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7145 return fold_build2 (TREE_CODE (arg0), type,
7146 fold_build1 (CONJ_EXPR, type,
7147 TREE_OPERAND (arg0, 0)),
7148 fold_build1 (CONJ_EXPR, type,
7149 TREE_OPERAND (arg0, 1)));
7150 else if (TREE_CODE (arg0) == CONJ_EXPR)
7151 return TREE_OPERAND (arg0, 0);
7152 return NULL_TREE;
7154 case BIT_NOT_EXPR:
7155 if (TREE_CODE (arg0) == INTEGER_CST)
7156 return fold_not_const (arg0, type);
7157 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
7158 return TREE_OPERAND (arg0, 0);
7159 /* Convert ~ (-A) to A - 1. */
7160 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
7161 return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
7162 build_int_cst (type, 1));
7163 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
7164 else if (INTEGRAL_TYPE_P (type)
7165 && ((TREE_CODE (arg0) == MINUS_EXPR
7166 && integer_onep (TREE_OPERAND (arg0, 1)))
7167 || (TREE_CODE (arg0) == PLUS_EXPR
7168 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
7169 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7170 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7171 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7172 && (tem = fold_unary (BIT_NOT_EXPR, type,
7173 fold_convert (type,
7174 TREE_OPERAND (arg0, 0)))))
7175 return fold_build2 (BIT_XOR_EXPR, type, tem,
7176 fold_convert (type, TREE_OPERAND (arg0, 1)));
7177 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7178 && (tem = fold_unary (BIT_NOT_EXPR, type,
7179 fold_convert (type,
7180 TREE_OPERAND (arg0, 1)))))
7181 return fold_build2 (BIT_XOR_EXPR, type,
7182 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
7184 return NULL_TREE;
7186 case TRUTH_NOT_EXPR:
7187 /* The argument to invert_truthvalue must have Boolean type. */
7188 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7189 arg0 = fold_convert (boolean_type_node, arg0);
7191 /* Note that the operand of this must be an int
7192 and its values must be 0 or 1.
7193 ("true" is a fixed value perhaps depending on the language,
7194 but we don't handle values other than 1 correctly yet.) */
7195 tem = invert_truthvalue (arg0);
7196 /* Avoid infinite recursion. */
7197 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7198 return NULL_TREE;
7199 return fold_convert (type, tem);
7201 case REALPART_EXPR:
7202 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7203 return NULL_TREE;
7204 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7205 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7206 TREE_OPERAND (arg0, 1));
7207 else if (TREE_CODE (arg0) == COMPLEX_CST)
7208 return TREE_REALPART (arg0);
7209 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7210 return fold_build2 (TREE_CODE (arg0), type,
7211 fold_build1 (REALPART_EXPR, type,
7212 TREE_OPERAND (arg0, 0)),
7213 fold_build1 (REALPART_EXPR, type,
7214 TREE_OPERAND (arg0, 1)));
7215 return NULL_TREE;
7217 case IMAGPART_EXPR:
7218 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7219 return fold_convert (type, integer_zero_node);
7220 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7221 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7222 TREE_OPERAND (arg0, 0));
7223 else if (TREE_CODE (arg0) == COMPLEX_CST)
7224 return TREE_IMAGPART (arg0);
7225 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7226 return fold_build2 (TREE_CODE (arg0), type,
7227 fold_build1 (IMAGPART_EXPR, type,
7228 TREE_OPERAND (arg0, 0)),
7229 fold_build1 (IMAGPART_EXPR, type,
7230 TREE_OPERAND (arg0, 1)));
7231 return NULL_TREE;
7233 default:
7234 return NULL_TREE;
7235 } /* switch (code) */
7238 /* Fold a binary expression of code CODE and type TYPE with operands
7239 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
7240 Return the folded expression if folding is successful. Otherwise,
7241 return NULL_TREE. */
7243 static tree
7244 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
7246 enum tree_code compl_code;
7248 if (code == MIN_EXPR)
7249 compl_code = MAX_EXPR;
7250 else if (code == MAX_EXPR)
7251 compl_code = MIN_EXPR;
7252 else
7253 gcc_unreachable ();
7255 /* MIN (MAX (a, b), b) == b.  */
7256 if (TREE_CODE (op0) == compl_code
7257 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
7258 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
7260 /* MIN (MAX (b, a), b) == b.  */
7261 if (TREE_CODE (op0) == compl_code
7262 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
7263 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
7264 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
7266 /* MIN (a, MAX (a, b)) == a.  */
7267 if (TREE_CODE (op1) == compl_code
7268 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
7269 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
7270 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
7272 /* MIN (a, MAX (b, a)) == a.  */
7273 if (TREE_CODE (op1) == compl_code
7274 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
7275 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
7276 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
7278 return NULL_TREE;
7281 /* Subroutine of fold_binary. This routine performs all of the
7282 transformations that are common to the equality/inequality
7283 operators (EQ_EXPR and NE_EXPR) and the ordering operators
7284 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
7285 fold_binary should call fold_binary. Fold a comparison with
7286 tree code CODE and type TYPE with operands OP0 and OP1. Return
7287 the folded comparison or NULL_TREE. */
7289 static tree
7290 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
7292 tree arg0, arg1, tem;
7294 arg0 = op0;
7295 arg1 = op1;
7297 STRIP_SIGN_NOPS (arg0);
7298 STRIP_SIGN_NOPS (arg1);
7300 tem = fold_relational_const (code, type, arg0, arg1);
7301 if (tem != NULL_TREE)
7302 return tem;
7304 /* If one arg is a real or integer constant, put it last. */
7305 if (tree_swap_operands_p (arg0, arg1, true))
7306 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
7308 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
7309 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7310 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7311 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7312 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
7313 && !(flag_wrapv || flag_trapv))
7314 && (TREE_CODE (arg1) == INTEGER_CST
7315 && !TREE_OVERFLOW (arg1)))
7317 tree const1 = TREE_OPERAND (arg0, 1);
7318 tree const2 = arg1;
7319 tree variable = TREE_OPERAND (arg0, 0);
7320 tree lhs;
7321 int lhs_add;
7322 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
7324 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
7325 TREE_TYPE (arg1), const2, const1);
7326 if (TREE_CODE (lhs) == TREE_CODE (arg1)
7327 && (TREE_CODE (lhs) != INTEGER_CST
7328 || !TREE_OVERFLOW (lhs)))
7329 return fold_build2 (code, type, variable, lhs);
7332 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7334 tree targ0 = strip_float_extensions (arg0);
7335 tree targ1 = strip_float_extensions (arg1);
7336 tree newtype = TREE_TYPE (targ0);
7338 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7339 newtype = TREE_TYPE (targ1);
7341 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7342 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7343 return fold_build2 (code, type, fold_convert (newtype, targ0),
7344 fold_convert (newtype, targ1));
7346 /* (-a) CMP (-b) -> b CMP a */
7347 if (TREE_CODE (arg0) == NEGATE_EXPR
7348 && TREE_CODE (arg1) == NEGATE_EXPR)
7349 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
7350 TREE_OPERAND (arg0, 0));
7352 if (TREE_CODE (arg1) == REAL_CST)
7354 REAL_VALUE_TYPE cst;
7355 cst = TREE_REAL_CST (arg1);
7357 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7358 if (TREE_CODE (arg0) == NEGATE_EXPR)
7359 return fold_build2 (swap_tree_comparison (code), type,
7360 TREE_OPERAND (arg0, 0),
7361 build_real (TREE_TYPE (arg1),
7362 REAL_VALUE_NEGATE (cst)));
7364 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7365 /* a CMP (-0) -> a CMP 0 */
7366 if (REAL_VALUE_MINUS_ZERO (cst))
7367 return fold_build2 (code, type, arg0,
7368 build_real (TREE_TYPE (arg1), dconst0));
7370 /* x != NaN is always true, other ops are always false. */
7371 if (REAL_VALUE_ISNAN (cst)
7372 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7374 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7375 return omit_one_operand (type, tem, arg0);
7378 /* Fold comparisons against infinity. */
7379 if (REAL_VALUE_ISINF (cst))
7381 tem = fold_inf_compare (code, type, arg0, arg1);
7382 if (tem != NULL_TREE)
7383 return tem;
7387 /* If this is a comparison of a real constant with a PLUS_EXPR
7388 or a MINUS_EXPR of a real constant, we can convert it into a
7389 comparison with a revised real constant as long as no overflow
7390 occurs when unsafe_math_optimizations are enabled. */
7391 if (flag_unsafe_math_optimizations
7392 && TREE_CODE (arg1) == REAL_CST
7393 && (TREE_CODE (arg0) == PLUS_EXPR
7394 || TREE_CODE (arg0) == MINUS_EXPR)
7395 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7396 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7397 ? MINUS_EXPR : PLUS_EXPR,
7398 arg1, TREE_OPERAND (arg0, 1), 0))
7399 && ! TREE_CONSTANT_OVERFLOW (tem))
7400 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
7402 /* Likewise, we can simplify a comparison of a real constant with
7403 a MINUS_EXPR whose first operand is also a real constant, i.e.
7404 (c1 - x) < c2 becomes x > c1-c2. */
7405 if (flag_unsafe_math_optimizations
7406 && TREE_CODE (arg1) == REAL_CST
7407 && TREE_CODE (arg0) == MINUS_EXPR
7408 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7409 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7410 arg1, 0))
7411 && ! TREE_CONSTANT_OVERFLOW (tem))
7412 return fold_build2 (swap_tree_comparison (code), type,
7413 TREE_OPERAND (arg0, 1), tem);
7415 /* Fold comparisons against built-in math functions. */
7416 if (TREE_CODE (arg1) == REAL_CST
7417 && flag_unsafe_math_optimizations
7418 && ! flag_errno_math)
7420 enum built_in_function fcode = builtin_mathfn_code (arg0);
7422 if (fcode != END_BUILTINS)
7424 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7425 if (tem != NULL_TREE)
7426 return tem;
7431 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7432 if (TREE_CONSTANT (arg1)
7433 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7434 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7435 /* This optimization is invalid for ordered comparisons
7436 if CONST+INCR overflows or if foo+incr might overflow.
7437 This optimization is invalid for floating point due to rounding.
7438 For pointer types we assume overflow doesn't happen. */
7439 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7440 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7441 && (code == EQ_EXPR || code == NE_EXPR))))
7443 tree varop, newconst;
7445 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7447 newconst = fold_build2 (PLUS_EXPR, TREE_TYPE (arg0),
7448 arg1, TREE_OPERAND (arg0, 1));
7449 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7450 TREE_OPERAND (arg0, 0),
7451 TREE_OPERAND (arg0, 1));
7453 else
7455 newconst = fold_build2 (MINUS_EXPR, TREE_TYPE (arg0),
7456 arg1, TREE_OPERAND (arg0, 1));
7457 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7458 TREE_OPERAND (arg0, 0),
7459 TREE_OPERAND (arg0, 1));
7463 /* If VAROP is a reference to a bitfield, we must mask
7464 the constant by the width of the field. */
7465 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7466 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
7467 && host_integerp (DECL_SIZE (TREE_OPERAND
7468 (TREE_OPERAND (varop, 0), 1)), 1))
7470 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7471 HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
7472 tree folded_compare, shift;
7474 /* First check whether the comparison would come out
7475 always the same. If we don't do that we would
7476 change the meaning with the masking. */
7477 folded_compare = fold_build2 (code, type,
7478 TREE_OPERAND (varop, 0), arg1);
7479 if (TREE_CODE (folded_compare) == INTEGER_CST)
7480 return omit_one_operand (type, folded_compare, varop);
7482 shift = build_int_cst (NULL_TREE,
7483 TYPE_PRECISION (TREE_TYPE (varop)) - size);
7484 shift = fold_convert (TREE_TYPE (varop), shift);
7485 newconst = fold_build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7486 newconst, shift);
7487 newconst = fold_build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7488 newconst, shift);
7491 return fold_build2 (code, type, varop, newconst);
7494 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7495 && (TREE_CODE (arg0) == NOP_EXPR
7496 || TREE_CODE (arg0) == CONVERT_EXPR))
7498 /* If we are widening one operand of an integer comparison,
7499 see if the other operand is similarly being widened. Perhaps we
7500 can do the comparison in the narrower type. */
7501 tem = fold_widened_comparison (code, type, arg0, arg1);
7502 if (tem)
7503 return tem;
7505 /* Or if we are changing signedness. */
7506 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
7507 if (tem)
7508 return tem;
7511 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7512 constant, we can simplify it. */
7513 if (TREE_CODE (arg1) == INTEGER_CST
7514 && (TREE_CODE (arg0) == MIN_EXPR
7515 || TREE_CODE (arg0) == MAX_EXPR)
7516 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7518 tem = optimize_minmax_comparison (code, type, op0, op1);
7519 if (tem)
7520 return tem;
7523 /* Simplify comparison of something with itself. (For IEEE
7524 floating-point, we can only do some of these simplifications.) */
7525 if (operand_equal_p (arg0, arg1, 0))
7527 switch (code)
7529 case EQ_EXPR:
7530 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7531 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7532 return constant_boolean_node (1, type);
7533 break;
7535 case GE_EXPR:
7536 case LE_EXPR:
7537 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7538 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7539 return constant_boolean_node (1, type);
7540 return fold_build2 (EQ_EXPR, type, arg0, arg1);
7542 case NE_EXPR:
7543 /* For NE, we can only do this simplification if integer
7544 or we don't honor IEEE floating point NaNs. */
7545 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7546 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7547 break;
7548 /* ... fall through ... */
7549 case GT_EXPR:
7550 case LT_EXPR:
7551 return constant_boolean_node (0, type);
7552 default:
7553 gcc_unreachable ();
7557 /* If we are comparing an expression that just has comparisons
7558 of two integer values, arithmetic expressions of those comparisons,
7559 and constants, we can simplify it. There are only three cases
7560 to check: the two values can either be equal, the first can be
7561 greater, or the second can be greater. Fold the expression for
7562 those three values. Since each value must be 0 or 1, we have
7563 eight possibilities, each of which corresponds to the constant 0
7564 or 1 or one of the six possible comparisons.
7566 This handles common cases like (a > b) == 0 but also handles
7567 expressions like ((x > y) - (y > x)) > 0, which supposedly
7568 occur in macroized code. */
7570 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7572 tree cval1 = 0, cval2 = 0;
7573 int save_p = 0;
7575 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7576 /* Don't handle degenerate cases here; they should already
7577 have been handled anyway. */
7578 && cval1 != 0 && cval2 != 0
7579 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7580 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7581 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7582 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7583 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7584 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7585 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7587 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7588 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7590 /* We can't just pass T to eval_subst in case cval1 or cval2
7591 was the same as ARG1. */
7593 tree high_result
7594 = fold_build2 (code, type,
7595 eval_subst (arg0, cval1, maxval,
7596 cval2, minval),
7597 arg1);
7598 tree equal_result
7599 = fold_build2 (code, type,
7600 eval_subst (arg0, cval1, maxval,
7601 cval2, maxval),
7602 arg1);
7603 tree low_result
7604 = fold_build2 (code, type,
7605 eval_subst (arg0, cval1, minval,
7606 cval2, maxval),
7607 arg1);
7609 /* All three of these results should be 0 or 1. Confirm they are.
7610 Then use those values to select the proper code to use. */
7612 if (TREE_CODE (high_result) == INTEGER_CST
7613 && TREE_CODE (equal_result) == INTEGER_CST
7614 && TREE_CODE (low_result) == INTEGER_CST)
7616 /* Make a 3-bit mask with the high-order bit being the
7617 value for `>', the next for '=', and the low for '<'. */
7618 switch ((integer_onep (high_result) * 4)
7619 + (integer_onep (equal_result) * 2)
7620 + integer_onep (low_result))
7622 case 0:
7623 /* Always false. */
7624 return omit_one_operand (type, integer_zero_node, arg0);
7625 case 1:
7626 code = LT_EXPR;
7627 break;
7628 case 2:
7629 code = EQ_EXPR;
7630 break;
7631 case 3:
7632 code = LE_EXPR;
7633 break;
7634 case 4:
7635 code = GT_EXPR;
7636 break;
7637 case 5:
7638 code = NE_EXPR;
7639 break;
7640 case 6:
7641 code = GE_EXPR;
7642 break;
7643 case 7:
7644 /* Always true. */
7645 return omit_one_operand (type, integer_one_node, arg0);
7648 if (save_p)
7649 return save_expr (build2 (code, type, cval1, cval2));
7650 return fold_build2 (code, type, cval1, cval2);
7655 /* Fold a comparison of the address of COMPONENT_REFs with the same
7656 type and component to a comparison of the address of the base
7657 object. In short, &x->a OP &y->a to x OP y and
7658 &x->a OP &y.a to x OP &y */
7659 if (TREE_CODE (arg0) == ADDR_EXPR
7660 && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
7661 && TREE_CODE (arg1) == ADDR_EXPR
7662 && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
7664 tree cref0 = TREE_OPERAND (arg0, 0);
7665 tree cref1 = TREE_OPERAND (arg1, 0);
7666 if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
7668 tree op0 = TREE_OPERAND (cref0, 0);
7669 tree op1 = TREE_OPERAND (cref1, 0);
7670 return fold_build2 (code, type,
7671 build_fold_addr_expr (op0),
7672 build_fold_addr_expr (op1));
7676 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
7677 into a single range test. */
7678 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
7679 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
7680 && TREE_CODE (arg1) == INTEGER_CST
7681 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7682 && !integer_zerop (TREE_OPERAND (arg0, 1))
7683 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7684 && !TREE_OVERFLOW (arg1))
7686 tem = fold_div_compare (code, type, arg0, arg1);
7687 if (tem != NULL_TREE)
7688 return tem;
7691 return NULL_TREE;
7694 /* Fold a binary expression of code CODE and type TYPE with operands
7695 OP0 and OP1. Return the folded expression if folding is
7696 successful. Otherwise, return NULL_TREE. */
7698 tree
7699 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
7701 enum tree_code_class kind = TREE_CODE_CLASS (code);
7702 tree arg0, arg1, tem;
7703 tree t1 = NULL_TREE;
7705 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7706 && TREE_CODE_LENGTH (code) == 2
7707 && op0 != NULL_TREE
7708 && op1 != NULL_TREE);
7710 arg0 = op0;
7711 arg1 = op1;
7713 /* Strip any conversions that don't change the mode. This is
7714 safe for every expression, except for a comparison expression
7715 because its signedness is derived from its operands. So, in
7716 the latter case, only strip conversions that don't change the
7717 signedness.
7719 Note that this is done as an internal manipulation within the
7720 constant folder, in order to find the simplest representation
7721 of the arguments so that their form can be studied. In any
7722 cases, the appropriate type conversions should be put back in
7723 the tree that will get out of the constant folder. */
7725 if (kind == tcc_comparison)
7727 STRIP_SIGN_NOPS (arg0);
7728 STRIP_SIGN_NOPS (arg1);
7730 else
7732 STRIP_NOPS (arg0);
7733 STRIP_NOPS (arg1);
7736 /* Note that TREE_CONSTANT isn't enough: static var addresses are
7737 constant but we can't do arithmetic on them. */
7738 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7739 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7740 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
7741 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
7743 if (kind == tcc_binary)
7744 tem = const_binop (code, arg0, arg1, 0);
7745 else if (kind == tcc_comparison)
7746 tem = fold_relational_const (code, type, arg0, arg1);
7747 else
7748 tem = NULL_TREE;
7750 if (tem != NULL_TREE)
7752 if (TREE_TYPE (tem) != type)
7753 tem = fold_convert (type, tem);
7754 return tem;
7758 /* If this is a commutative operation, and ARG0 is a constant, move it
7759 to ARG1 to reduce the number of tests below. */
7760 if (commutative_tree_code (code)
7761 && tree_swap_operands_p (arg0, arg1, true))
7762 return fold_build2 (code, type, op1, op0);
7764 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
7766 First check for cases where an arithmetic operation is applied to a
7767 compound, conditional, or comparison operation. Push the arithmetic
7768 operation inside the compound or conditional to see if any folding
7769 can then be done. Convert comparison to conditional for this purpose.
7770 The also optimizes non-constant cases that used to be done in
7771 expand_expr.
7773 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
7774 one of the operands is a comparison and the other is a comparison, a
7775 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
7776 code below would make the expression more complex. Change it to a
7777 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
7778 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
7780 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
7781 || code == EQ_EXPR || code == NE_EXPR)
7782 && ((truth_value_p (TREE_CODE (arg0))
7783 && (truth_value_p (TREE_CODE (arg1))
7784 || (TREE_CODE (arg1) == BIT_AND_EXPR
7785 && integer_onep (TREE_OPERAND (arg1, 1)))))
7786 || (truth_value_p (TREE_CODE (arg1))
7787 && (truth_value_p (TREE_CODE (arg0))
7788 || (TREE_CODE (arg0) == BIT_AND_EXPR
7789 && integer_onep (TREE_OPERAND (arg0, 1)))))))
7791 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
7792 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
7793 : TRUTH_XOR_EXPR,
7794 boolean_type_node,
7795 fold_convert (boolean_type_node, arg0),
7796 fold_convert (boolean_type_node, arg1));
7798 if (code == EQ_EXPR)
7799 tem = invert_truthvalue (tem);
7801 return fold_convert (type, tem);
7804 if (TREE_CODE_CLASS (code) == tcc_binary
7805 || TREE_CODE_CLASS (code) == tcc_comparison)
7807 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7808 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7809 fold_build2 (code, type,
7810 TREE_OPERAND (arg0, 1), op1));
7811 if (TREE_CODE (arg1) == COMPOUND_EXPR
7812 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
7813 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7814 fold_build2 (code, type,
7815 op0, TREE_OPERAND (arg1, 1)));
7817 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
7819 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7820 arg0, arg1,
7821 /*cond_first_p=*/1);
7822 if (tem != NULL_TREE)
7823 return tem;
7826 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
7828 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7829 arg1, arg0,
7830 /*cond_first_p=*/0);
7831 if (tem != NULL_TREE)
7832 return tem;
7836 switch (code)
7838 case PLUS_EXPR:
7839 /* A + (-B) -> A - B */
7840 if (TREE_CODE (arg1) == NEGATE_EXPR)
7841 return fold_build2 (MINUS_EXPR, type,
7842 fold_convert (type, arg0),
7843 fold_convert (type, TREE_OPERAND (arg1, 0)));
7844 /* (-A) + B -> B - A */
7845 if (TREE_CODE (arg0) == NEGATE_EXPR
7846 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7847 return fold_build2 (MINUS_EXPR, type,
7848 fold_convert (type, arg1),
7849 fold_convert (type, TREE_OPERAND (arg0, 0)));
7850 /* Convert ~A + 1 to -A. */
7851 if (INTEGRAL_TYPE_P (type)
7852 && TREE_CODE (arg0) == BIT_NOT_EXPR
7853 && integer_onep (arg1))
7854 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7856 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
7857 same or one. */
7858 if ((TREE_CODE (arg0) == MULT_EXPR
7859 || TREE_CODE (arg1) == MULT_EXPR)
7860 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7862 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
7863 if (tem)
7864 return tem;
7867 if (! FLOAT_TYPE_P (type))
7869 if (integer_zerop (arg1))
7870 return non_lvalue (fold_convert (type, arg0));
7872 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7873 with a constant, and the two constants have no bits in common,
7874 we should treat this as a BIT_IOR_EXPR since this may produce more
7875 simplifications. */
7876 if (TREE_CODE (arg0) == BIT_AND_EXPR
7877 && TREE_CODE (arg1) == BIT_AND_EXPR
7878 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7879 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7880 && integer_zerop (const_binop (BIT_AND_EXPR,
7881 TREE_OPERAND (arg0, 1),
7882 TREE_OPERAND (arg1, 1), 0)))
7884 code = BIT_IOR_EXPR;
7885 goto bit_ior;
7888 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7889 (plus (plus (mult) (mult)) (foo)) so that we can
7890 take advantage of the factoring cases below. */
7891 if (((TREE_CODE (arg0) == PLUS_EXPR
7892 || TREE_CODE (arg0) == MINUS_EXPR)
7893 && TREE_CODE (arg1) == MULT_EXPR)
7894 || ((TREE_CODE (arg1) == PLUS_EXPR
7895 || TREE_CODE (arg1) == MINUS_EXPR)
7896 && TREE_CODE (arg0) == MULT_EXPR))
7898 tree parg0, parg1, parg, marg;
7899 enum tree_code pcode;
7901 if (TREE_CODE (arg1) == MULT_EXPR)
7902 parg = arg0, marg = arg1;
7903 else
7904 parg = arg1, marg = arg0;
7905 pcode = TREE_CODE (parg);
7906 parg0 = TREE_OPERAND (parg, 0);
7907 parg1 = TREE_OPERAND (parg, 1);
7908 STRIP_NOPS (parg0);
7909 STRIP_NOPS (parg1);
7911 if (TREE_CODE (parg0) == MULT_EXPR
7912 && TREE_CODE (parg1) != MULT_EXPR)
7913 return fold_build2 (pcode, type,
7914 fold_build2 (PLUS_EXPR, type,
7915 fold_convert (type, parg0),
7916 fold_convert (type, marg)),
7917 fold_convert (type, parg1));
7918 if (TREE_CODE (parg0) != MULT_EXPR
7919 && TREE_CODE (parg1) == MULT_EXPR)
7920 return fold_build2 (PLUS_EXPR, type,
7921 fold_convert (type, parg0),
7922 fold_build2 (pcode, type,
7923 fold_convert (type, marg),
7924 fold_convert (type,
7925 parg1)));
7928 /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7929 of the array. Loop optimizer sometimes produce this type of
7930 expressions. */
7931 if (TREE_CODE (arg0) == ADDR_EXPR)
7933 tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7934 if (tem)
7935 return fold_convert (type, tem);
7937 else if (TREE_CODE (arg1) == ADDR_EXPR)
7939 tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7940 if (tem)
7941 return fold_convert (type, tem);
7944 else
7946 /* See if ARG1 is zero and X + ARG1 reduces to X. */
7947 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7948 return non_lvalue (fold_convert (type, arg0));
7950 /* Likewise if the operands are reversed. */
7951 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7952 return non_lvalue (fold_convert (type, arg1));
7954 /* Convert X + -C into X - C. */
7955 if (TREE_CODE (arg1) == REAL_CST
7956 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7958 tem = fold_negate_const (arg1, type);
7959 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7960 return fold_build2 (MINUS_EXPR, type,
7961 fold_convert (type, arg0),
7962 fold_convert (type, tem));
7965 if (flag_unsafe_math_optimizations
7966 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
7967 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
7968 && (tem = distribute_real_division (code, type, arg0, arg1)))
7969 return tem;
7971 /* Convert x+x into x*2.0. */
7972 if (operand_equal_p (arg0, arg1, 0)
7973 && SCALAR_FLOAT_TYPE_P (type))
7974 return fold_build2 (MULT_EXPR, type, arg0,
7975 build_real (type, dconst2));
7977 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
7978 if (flag_unsafe_math_optimizations
7979 && TREE_CODE (arg1) == PLUS_EXPR
7980 && TREE_CODE (arg0) != MULT_EXPR)
7982 tree tree10 = TREE_OPERAND (arg1, 0);
7983 tree tree11 = TREE_OPERAND (arg1, 1);
7984 if (TREE_CODE (tree11) == MULT_EXPR
7985 && TREE_CODE (tree10) == MULT_EXPR)
7987 tree tree0;
7988 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
7989 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
7992 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
7993 if (flag_unsafe_math_optimizations
7994 && TREE_CODE (arg0) == PLUS_EXPR
7995 && TREE_CODE (arg1) != MULT_EXPR)
7997 tree tree00 = TREE_OPERAND (arg0, 0);
7998 tree tree01 = TREE_OPERAND (arg0, 1);
7999 if (TREE_CODE (tree01) == MULT_EXPR
8000 && TREE_CODE (tree00) == MULT_EXPR)
8002 tree tree0;
8003 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
8004 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
8009 bit_rotate:
8010 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
8011 is a rotate of A by C1 bits. */
8012 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
8013 is a rotate of A by B bits. */
8015 enum tree_code code0, code1;
8016 code0 = TREE_CODE (arg0);
8017 code1 = TREE_CODE (arg1);
8018 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
8019 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
8020 && operand_equal_p (TREE_OPERAND (arg0, 0),
8021 TREE_OPERAND (arg1, 0), 0)
8022 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8024 tree tree01, tree11;
8025 enum tree_code code01, code11;
8027 tree01 = TREE_OPERAND (arg0, 1);
8028 tree11 = TREE_OPERAND (arg1, 1);
8029 STRIP_NOPS (tree01);
8030 STRIP_NOPS (tree11);
8031 code01 = TREE_CODE (tree01);
8032 code11 = TREE_CODE (tree11);
8033 if (code01 == INTEGER_CST
8034 && code11 == INTEGER_CST
8035 && TREE_INT_CST_HIGH (tree01) == 0
8036 && TREE_INT_CST_HIGH (tree11) == 0
8037 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
8038 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
8039 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
8040 code0 == LSHIFT_EXPR ? tree01 : tree11);
8041 else if (code11 == MINUS_EXPR)
8043 tree tree110, tree111;
8044 tree110 = TREE_OPERAND (tree11, 0);
8045 tree111 = TREE_OPERAND (tree11, 1);
8046 STRIP_NOPS (tree110);
8047 STRIP_NOPS (tree111);
8048 if (TREE_CODE (tree110) == INTEGER_CST
8049 && 0 == compare_tree_int (tree110,
8050 TYPE_PRECISION
8051 (TREE_TYPE (TREE_OPERAND
8052 (arg0, 0))))
8053 && operand_equal_p (tree01, tree111, 0))
8054 return build2 ((code0 == LSHIFT_EXPR
8055 ? LROTATE_EXPR
8056 : RROTATE_EXPR),
8057 type, TREE_OPERAND (arg0, 0), tree01);
8059 else if (code01 == MINUS_EXPR)
8061 tree tree010, tree011;
8062 tree010 = TREE_OPERAND (tree01, 0);
8063 tree011 = TREE_OPERAND (tree01, 1);
8064 STRIP_NOPS (tree010);
8065 STRIP_NOPS (tree011);
8066 if (TREE_CODE (tree010) == INTEGER_CST
8067 && 0 == compare_tree_int (tree010,
8068 TYPE_PRECISION
8069 (TREE_TYPE (TREE_OPERAND
8070 (arg0, 0))))
8071 && operand_equal_p (tree11, tree011, 0))
8072 return build2 ((code0 != LSHIFT_EXPR
8073 ? LROTATE_EXPR
8074 : RROTATE_EXPR),
8075 type, TREE_OPERAND (arg0, 0), tree11);
8080 associate:
8081 /* In most languages, can't associate operations on floats through
8082 parentheses. Rather than remember where the parentheses were, we
8083 don't associate floats at all, unless the user has specified
8084 -funsafe-math-optimizations. */
8086 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8088 tree var0, con0, lit0, minus_lit0;
8089 tree var1, con1, lit1, minus_lit1;
8091 /* Split both trees into variables, constants, and literals. Then
8092 associate each group together, the constants with literals,
8093 then the result with variables. This increases the chances of
8094 literals being recombined later and of generating relocatable
8095 expressions for the sum of a constant and literal. */
8096 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
8097 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
8098 code == MINUS_EXPR);
8100 /* Only do something if we found more than two objects. Otherwise,
8101 nothing has changed and we risk infinite recursion. */
8102 if (2 < ((var0 != 0) + (var1 != 0)
8103 + (con0 != 0) + (con1 != 0)
8104 + (lit0 != 0) + (lit1 != 0)
8105 + (minus_lit0 != 0) + (minus_lit1 != 0)))
8107 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
8108 if (code == MINUS_EXPR)
8109 code = PLUS_EXPR;
8111 var0 = associate_trees (var0, var1, code, type);
8112 con0 = associate_trees (con0, con1, code, type);
8113 lit0 = associate_trees (lit0, lit1, code, type);
8114 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
8116 /* Preserve the MINUS_EXPR if the negative part of the literal is
8117 greater than the positive part. Otherwise, the multiplicative
8118 folding code (i.e extract_muldiv) may be fooled in case
8119 unsigned constants are subtracted, like in the following
8120 example: ((X*2 + 4) - 8U)/2. */
8121 if (minus_lit0 && lit0)
8123 if (TREE_CODE (lit0) == INTEGER_CST
8124 && TREE_CODE (minus_lit0) == INTEGER_CST
8125 && tree_int_cst_lt (lit0, minus_lit0))
8127 minus_lit0 = associate_trees (minus_lit0, lit0,
8128 MINUS_EXPR, type);
8129 lit0 = 0;
8131 else
8133 lit0 = associate_trees (lit0, minus_lit0,
8134 MINUS_EXPR, type);
8135 minus_lit0 = 0;
8138 if (minus_lit0)
8140 if (con0 == 0)
8141 return fold_convert (type,
8142 associate_trees (var0, minus_lit0,
8143 MINUS_EXPR, type));
8144 else
8146 con0 = associate_trees (con0, minus_lit0,
8147 MINUS_EXPR, type);
8148 return fold_convert (type,
8149 associate_trees (var0, con0,
8150 PLUS_EXPR, type));
8154 con0 = associate_trees (con0, lit0, code, type);
8155 return fold_convert (type, associate_trees (var0, con0,
8156 code, type));
8160 return NULL_TREE;
8162 case MINUS_EXPR:
8163 /* A - (-B) -> A + B */
8164 if (TREE_CODE (arg1) == NEGATE_EXPR)
8165 return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
8166 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
8167 if (TREE_CODE (arg0) == NEGATE_EXPR
8168 && (FLOAT_TYPE_P (type)
8169 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
8170 && negate_expr_p (arg1)
8171 && reorder_operands_p (arg0, arg1))
8172 return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
8173 TREE_OPERAND (arg0, 0));
8174 /* Convert -A - 1 to ~A. */
8175 if (INTEGRAL_TYPE_P (type)
8176 && TREE_CODE (arg0) == NEGATE_EXPR
8177 && integer_onep (arg1))
8178 return fold_build1 (BIT_NOT_EXPR, type,
8179 fold_convert (type, TREE_OPERAND (arg0, 0)));
8181 /* Convert -1 - A to ~A. */
8182 if (INTEGRAL_TYPE_P (type)
8183 && integer_all_onesp (arg0))
8184 return fold_build1 (BIT_NOT_EXPR, type, arg1);
8186 if (! FLOAT_TYPE_P (type))
8188 if (integer_zerop (arg0))
8189 return negate_expr (fold_convert (type, arg1));
8190 if (integer_zerop (arg1))
8191 return non_lvalue (fold_convert (type, arg0));
8193 /* Fold A - (A & B) into ~B & A. */
8194 if (!TREE_SIDE_EFFECTS (arg0)
8195 && TREE_CODE (arg1) == BIT_AND_EXPR)
8197 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
8198 return fold_build2 (BIT_AND_EXPR, type,
8199 fold_build1 (BIT_NOT_EXPR, type,
8200 TREE_OPERAND (arg1, 0)),
8201 arg0);
8202 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8203 return fold_build2 (BIT_AND_EXPR, type,
8204 fold_build1 (BIT_NOT_EXPR, type,
8205 TREE_OPERAND (arg1, 1)),
8206 arg0);
8209 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
8210 any power of 2 minus 1. */
8211 if (TREE_CODE (arg0) == BIT_AND_EXPR
8212 && TREE_CODE (arg1) == BIT_AND_EXPR
8213 && operand_equal_p (TREE_OPERAND (arg0, 0),
8214 TREE_OPERAND (arg1, 0), 0))
8216 tree mask0 = TREE_OPERAND (arg0, 1);
8217 tree mask1 = TREE_OPERAND (arg1, 1);
8218 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
8220 if (operand_equal_p (tem, mask1, 0))
8222 tem = fold_build2 (BIT_XOR_EXPR, type,
8223 TREE_OPERAND (arg0, 0), mask1);
8224 return fold_build2 (MINUS_EXPR, type, tem, mask1);
8229 /* See if ARG1 is zero and X - ARG1 reduces to X. */
8230 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
8231 return non_lvalue (fold_convert (type, arg0));
8233 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
8234 ARG0 is zero and X + ARG0 reduces to X, since that would mean
8235 (-ARG1 + ARG0) reduces to -ARG1. */
8236 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
8237 return negate_expr (fold_convert (type, arg1));
8239 /* Fold &x - &x. This can happen from &x.foo - &x.
8240 This is unsafe for certain floats even in non-IEEE formats.
8241 In IEEE, it is unsafe because it does wrong for NaNs.
8242 Also note that operand_equal_p is always false if an operand
8243 is volatile. */
8245 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8246 && operand_equal_p (arg0, arg1, 0))
8247 return fold_convert (type, integer_zero_node);
8249 /* A - B -> A + (-B) if B is easily negatable. */
8250 if (negate_expr_p (arg1)
8251 && ((FLOAT_TYPE_P (type)
8252 /* Avoid this transformation if B is a positive REAL_CST. */
8253 && (TREE_CODE (arg1) != REAL_CST
8254 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
8255 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
8256 return fold_build2 (PLUS_EXPR, type,
8257 fold_convert (type, arg0),
8258 fold_convert (type, negate_expr (arg1)));
8260 /* Try folding difference of addresses. */
8262 HOST_WIDE_INT diff;
8264 if ((TREE_CODE (arg0) == ADDR_EXPR
8265 || TREE_CODE (arg1) == ADDR_EXPR)
8266 && ptr_difference_const (arg0, arg1, &diff))
8267 return build_int_cst_type (type, diff);
8270 /* Fold &a[i] - &a[j] to i-j. */
8271 if (TREE_CODE (arg0) == ADDR_EXPR
8272 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
8273 && TREE_CODE (arg1) == ADDR_EXPR
8274 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
8276 tree aref0 = TREE_OPERAND (arg0, 0);
8277 tree aref1 = TREE_OPERAND (arg1, 0);
8278 if (operand_equal_p (TREE_OPERAND (aref0, 0),
8279 TREE_OPERAND (aref1, 0), 0))
8281 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
8282 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
8283 tree esz = array_ref_element_size (aref0);
8284 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8285 return fold_build2 (MULT_EXPR, type, diff,
8286 fold_convert (type, esz));
8291 /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
8292 of the array. Loop optimizer sometimes produce this type of
8293 expressions. */
8294 if (TREE_CODE (arg0) == ADDR_EXPR)
8296 tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
8297 if (tem)
8298 return fold_convert (type, tem);
8301 if (flag_unsafe_math_optimizations
8302 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
8303 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
8304 && (tem = distribute_real_division (code, type, arg0, arg1)))
8305 return tem;
8307 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
8308 same or one. */
8309 if ((TREE_CODE (arg0) == MULT_EXPR
8310 || TREE_CODE (arg1) == MULT_EXPR)
8311 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
8313 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
8314 if (tem)
8315 return tem;
8318 goto associate;
8320 case MULT_EXPR:
8321 /* (-A) * (-B) -> A * B */
8322 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8323 return fold_build2 (MULT_EXPR, type,
8324 TREE_OPERAND (arg0, 0),
8325 negate_expr (arg1));
8326 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8327 return fold_build2 (MULT_EXPR, type,
8328 negate_expr (arg0),
8329 TREE_OPERAND (arg1, 0));
8331 if (! FLOAT_TYPE_P (type))
8333 if (integer_zerop (arg1))
8334 return omit_one_operand (type, arg1, arg0);
8335 if (integer_onep (arg1))
8336 return non_lvalue (fold_convert (type, arg0));
8337 /* Transform x * -1 into -x. */
8338 if (integer_all_onesp (arg1))
8339 return fold_convert (type, negate_expr (arg0));
8341 /* (a * (1 << b)) is (a << b) */
8342 if (TREE_CODE (arg1) == LSHIFT_EXPR
8343 && integer_onep (TREE_OPERAND (arg1, 0)))
8344 return fold_build2 (LSHIFT_EXPR, type, arg0,
8345 TREE_OPERAND (arg1, 1));
8346 if (TREE_CODE (arg0) == LSHIFT_EXPR
8347 && integer_onep (TREE_OPERAND (arg0, 0)))
8348 return fold_build2 (LSHIFT_EXPR, type, arg1,
8349 TREE_OPERAND (arg0, 1));
8351 if (TREE_CODE (arg1) == INTEGER_CST
8352 && 0 != (tem = extract_muldiv (op0,
8353 fold_convert (type, arg1),
8354 code, NULL_TREE)))
8355 return fold_convert (type, tem);
8358 else
8360 /* Maybe fold x * 0 to 0. The expressions aren't the same
8361 when x is NaN, since x * 0 is also NaN. Nor are they the
8362 same in modes with signed zeros, since multiplying a
8363 negative value by 0 gives -0, not +0. */
8364 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8365 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
8366 && real_zerop (arg1))
8367 return omit_one_operand (type, arg1, arg0);
8368 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
8369 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8370 && real_onep (arg1))
8371 return non_lvalue (fold_convert (type, arg0));
8373 /* Transform x * -1.0 into -x. */
8374 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8375 && real_minus_onep (arg1))
8376 return fold_convert (type, negate_expr (arg0));
8378 /* Convert (C1/X)*C2 into (C1*C2)/X. */
8379 if (flag_unsafe_math_optimizations
8380 && TREE_CODE (arg0) == RDIV_EXPR
8381 && TREE_CODE (arg1) == REAL_CST
8382 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
8384 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
8385 arg1, 0);
8386 if (tem)
8387 return fold_build2 (RDIV_EXPR, type, tem,
8388 TREE_OPERAND (arg0, 1));
8391 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
8392 if (operand_equal_p (arg0, arg1, 0))
8394 tree tem = fold_strip_sign_ops (arg0);
8395 if (tem != NULL_TREE)
8397 tem = fold_convert (type, tem);
8398 return fold_build2 (MULT_EXPR, type, tem, tem);
8402 if (flag_unsafe_math_optimizations)
8404 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8405 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8407 /* Optimizations of root(...)*root(...). */
8408 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
8410 tree rootfn, arg, arglist;
8411 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8412 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8414 /* Optimize sqrt(x)*sqrt(x) as x. */
8415 if (BUILTIN_SQRT_P (fcode0)
8416 && operand_equal_p (arg00, arg10, 0)
8417 && ! HONOR_SNANS (TYPE_MODE (type)))
8418 return arg00;
8420 /* Optimize root(x)*root(y) as root(x*y). */
8421 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8422 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8423 arglist = build_tree_list (NULL_TREE, arg);
8424 return build_function_call_expr (rootfn, arglist);
8427 /* Optimize expN(x)*expN(y) as expN(x+y). */
8428 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
8430 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8431 tree arg = fold_build2 (PLUS_EXPR, type,
8432 TREE_VALUE (TREE_OPERAND (arg0, 1)),
8433 TREE_VALUE (TREE_OPERAND (arg1, 1)));
8434 tree arglist = build_tree_list (NULL_TREE, arg);
8435 return build_function_call_expr (expfn, arglist);
8438 /* Optimizations of pow(...)*pow(...). */
8439 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
8440 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
8441 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
8443 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8444 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8445 1)));
8446 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8447 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8448 1)));
8450 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
8451 if (operand_equal_p (arg01, arg11, 0))
8453 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8454 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8455 tree arglist = tree_cons (NULL_TREE, arg,
8456 build_tree_list (NULL_TREE,
8457 arg01));
8458 return build_function_call_expr (powfn, arglist);
8461 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
8462 if (operand_equal_p (arg00, arg10, 0))
8464 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8465 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
8466 tree arglist = tree_cons (NULL_TREE, arg00,
8467 build_tree_list (NULL_TREE,
8468 arg));
8469 return build_function_call_expr (powfn, arglist);
8473 /* Optimize tan(x)*cos(x) as sin(x). */
8474 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
8475 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
8476 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
8477 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
8478 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
8479 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
8480 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8481 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8483 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
8485 if (sinfn != NULL_TREE)
8486 return build_function_call_expr (sinfn,
8487 TREE_OPERAND (arg0, 1));
8490 /* Optimize x*pow(x,c) as pow(x,c+1). */
8491 if (fcode1 == BUILT_IN_POW
8492 || fcode1 == BUILT_IN_POWF
8493 || fcode1 == BUILT_IN_POWL)
8495 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8496 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8497 1)));
8498 if (TREE_CODE (arg11) == REAL_CST
8499 && ! TREE_CONSTANT_OVERFLOW (arg11)
8500 && operand_equal_p (arg0, arg10, 0))
8502 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8503 REAL_VALUE_TYPE c;
8504 tree arg, arglist;
8506 c = TREE_REAL_CST (arg11);
8507 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8508 arg = build_real (type, c);
8509 arglist = build_tree_list (NULL_TREE, arg);
8510 arglist = tree_cons (NULL_TREE, arg0, arglist);
8511 return build_function_call_expr (powfn, arglist);
8515 /* Optimize pow(x,c)*x as pow(x,c+1). */
8516 if (fcode0 == BUILT_IN_POW
8517 || fcode0 == BUILT_IN_POWF
8518 || fcode0 == BUILT_IN_POWL)
8520 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8521 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8522 1)));
8523 if (TREE_CODE (arg01) == REAL_CST
8524 && ! TREE_CONSTANT_OVERFLOW (arg01)
8525 && operand_equal_p (arg1, arg00, 0))
8527 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8528 REAL_VALUE_TYPE c;
8529 tree arg, arglist;
8531 c = TREE_REAL_CST (arg01);
8532 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8533 arg = build_real (type, c);
8534 arglist = build_tree_list (NULL_TREE, arg);
8535 arglist = tree_cons (NULL_TREE, arg1, arglist);
8536 return build_function_call_expr (powfn, arglist);
8540 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
8541 if (! optimize_size
8542 && operand_equal_p (arg0, arg1, 0))
8544 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
8546 if (powfn)
8548 tree arg = build_real (type, dconst2);
8549 tree arglist = build_tree_list (NULL_TREE, arg);
8550 arglist = tree_cons (NULL_TREE, arg0, arglist);
8551 return build_function_call_expr (powfn, arglist);
8556 goto associate;
8558 case BIT_IOR_EXPR:
8559 bit_ior:
8560 if (integer_all_onesp (arg1))
8561 return omit_one_operand (type, arg1, arg0);
8562 if (integer_zerop (arg1))
8563 return non_lvalue (fold_convert (type, arg0));
8564 if (operand_equal_p (arg0, arg1, 0))
8565 return non_lvalue (fold_convert (type, arg0));
8567 /* ~X | X is -1. */
8568 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8569 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8571 t1 = build_int_cst (type, -1);
8572 t1 = force_fit_type (t1, 0, false, false);
8573 return omit_one_operand (type, t1, arg1);
8576 /* X | ~X is -1. */
8577 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8578 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8580 t1 = build_int_cst (type, -1);
8581 t1 = force_fit_type (t1, 0, false, false);
8582 return omit_one_operand (type, t1, arg0);
8585 /* Canonicalize (X & C1) | C2. */
8586 if (TREE_CODE (arg0) == BIT_AND_EXPR
8587 && TREE_CODE (arg1) == INTEGER_CST
8588 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8590 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
8591 int width = TYPE_PRECISION (type);
8592 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
8593 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
8594 hi2 = TREE_INT_CST_HIGH (arg1);
8595 lo2 = TREE_INT_CST_LOW (arg1);
8597 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
8598 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
8599 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8601 if (width > HOST_BITS_PER_WIDE_INT)
8603 mhi = (unsigned HOST_WIDE_INT) -1
8604 >> (2 * HOST_BITS_PER_WIDE_INT - width);
8605 mlo = -1;
8607 else
8609 mhi = 0;
8610 mlo = (unsigned HOST_WIDE_INT) -1
8611 >> (HOST_BITS_PER_WIDE_INT - width);
8614 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
8615 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
8616 return fold_build2 (BIT_IOR_EXPR, type,
8617 TREE_OPERAND (arg0, 0), arg1);
8619 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
8620 hi1 &= mhi;
8621 lo1 &= mlo;
8622 if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
8623 return fold_build2 (BIT_IOR_EXPR, type,
8624 fold_build2 (BIT_AND_EXPR, type,
8625 TREE_OPERAND (arg0, 0),
8626 build_int_cst_wide (type,
8627 lo1 & ~lo2,
8628 hi1 & ~hi2)),
8629 arg1);
8632 /* (X & Y) | Y is (X, Y). */
8633 if (TREE_CODE (arg0) == BIT_AND_EXPR
8634 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8635 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8636 /* (X & Y) | X is (Y, X). */
8637 if (TREE_CODE (arg0) == BIT_AND_EXPR
8638 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8639 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8640 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8641 /* X | (X & Y) is (Y, X). */
8642 if (TREE_CODE (arg1) == BIT_AND_EXPR
8643 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8644 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8645 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8646 /* X | (Y & X) is (Y, X). */
8647 if (TREE_CODE (arg1) == BIT_AND_EXPR
8648 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8649 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8650 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8652 t1 = distribute_bit_expr (code, type, arg0, arg1);
8653 if (t1 != NULL_TREE)
8654 return t1;
8656 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
8658 This results in more efficient code for machines without a NAND
8659 instruction. Combine will canonicalize to the first form
8660 which will allow use of NAND instructions provided by the
8661 backend if they exist. */
8662 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8663 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8665 return fold_build1 (BIT_NOT_EXPR, type,
8666 build2 (BIT_AND_EXPR, type,
8667 TREE_OPERAND (arg0, 0),
8668 TREE_OPERAND (arg1, 0)));
8671 /* See if this can be simplified into a rotate first. If that
8672 is unsuccessful continue in the association code. */
8673 goto bit_rotate;
8675 case BIT_XOR_EXPR:
8676 if (integer_zerop (arg1))
8677 return non_lvalue (fold_convert (type, arg0));
8678 if (integer_all_onesp (arg1))
8679 return fold_build1 (BIT_NOT_EXPR, type, arg0);
8680 if (operand_equal_p (arg0, arg1, 0))
8681 return omit_one_operand (type, integer_zero_node, arg0);
8683 /* ~X ^ X is -1. */
8684 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8685 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8687 t1 = build_int_cst (type, -1);
8688 t1 = force_fit_type (t1, 0, false, false);
8689 return omit_one_operand (type, t1, arg1);
8692 /* X ^ ~X is -1. */
8693 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8694 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8696 t1 = build_int_cst (type, -1);
8697 t1 = force_fit_type (t1, 0, false, false);
8698 return omit_one_operand (type, t1, arg0);
8701 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
8702 with a constant, and the two constants have no bits in common,
8703 we should treat this as a BIT_IOR_EXPR since this may produce more
8704 simplifications. */
8705 if (TREE_CODE (arg0) == BIT_AND_EXPR
8706 && TREE_CODE (arg1) == BIT_AND_EXPR
8707 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8708 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8709 && integer_zerop (const_binop (BIT_AND_EXPR,
8710 TREE_OPERAND (arg0, 1),
8711 TREE_OPERAND (arg1, 1), 0)))
8713 code = BIT_IOR_EXPR;
8714 goto bit_ior;
8717 /* (X | Y) ^ X -> Y & ~ X*/
8718 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8719 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8721 tree t2 = TREE_OPERAND (arg0, 1);
8722 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8723 arg1);
8724 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8725 fold_convert (type, t1));
8726 return t1;
8729 /* (Y | X) ^ X -> Y & ~ X*/
8730 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8731 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8733 tree t2 = TREE_OPERAND (arg0, 0);
8734 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8735 arg1);
8736 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8737 fold_convert (type, t1));
8738 return t1;
8741 /* X ^ (X | Y) -> Y & ~ X*/
8742 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8743 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
8745 tree t2 = TREE_OPERAND (arg1, 1);
8746 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8747 arg0);
8748 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8749 fold_convert (type, t1));
8750 return t1;
8753 /* X ^ (Y | X) -> Y & ~ X*/
8754 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8755 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
8757 tree t2 = TREE_OPERAND (arg1, 0);
8758 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8759 arg0);
8760 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8761 fold_convert (type, t1));
8762 return t1;
8765 /* Convert ~X ^ ~Y to X ^ Y. */
8766 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8767 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8768 return fold_build2 (code, type,
8769 fold_convert (type, TREE_OPERAND (arg0, 0)),
8770 fold_convert (type, TREE_OPERAND (arg1, 0)));
8772 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
8773 if (TREE_CODE (arg0) == BIT_AND_EXPR
8774 && integer_onep (TREE_OPERAND (arg0, 1))
8775 && integer_onep (arg1))
8776 return fold_build2 (EQ_EXPR, type, arg0,
8777 build_int_cst (TREE_TYPE (arg0), 0));
8779 /* Fold (X & Y) ^ Y as ~X & Y. */
8780 if (TREE_CODE (arg0) == BIT_AND_EXPR
8781 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8783 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
8784 return fold_build2 (BIT_AND_EXPR, type,
8785 fold_build1 (BIT_NOT_EXPR, type, tem),
8786 fold_convert (type, arg1));
8788 /* Fold (X & Y) ^ X as ~Y & X. */
8789 if (TREE_CODE (arg0) == BIT_AND_EXPR
8790 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8791 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8793 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
8794 return fold_build2 (BIT_AND_EXPR, type,
8795 fold_build1 (BIT_NOT_EXPR, type, tem),
8796 fold_convert (type, arg1));
8798 /* Fold X ^ (X & Y) as X & ~Y. */
8799 if (TREE_CODE (arg1) == BIT_AND_EXPR
8800 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8802 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
8803 return fold_build2 (BIT_AND_EXPR, type,
8804 fold_convert (type, arg0),
8805 fold_build1 (BIT_NOT_EXPR, type, tem));
8807 /* Fold X ^ (Y & X) as ~Y & X. */
8808 if (TREE_CODE (arg1) == BIT_AND_EXPR
8809 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8810 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8812 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
8813 return fold_build2 (BIT_AND_EXPR, type,
8814 fold_build1 (BIT_NOT_EXPR, type, tem),
8815 fold_convert (type, arg0));
8818 /* See if this can be simplified into a rotate first. If that
8819 is unsuccessful continue in the association code. */
8820 goto bit_rotate;
8822 case BIT_AND_EXPR:
8823 if (integer_all_onesp (arg1))
8824 return non_lvalue (fold_convert (type, arg0));
8825 if (integer_zerop (arg1))
8826 return omit_one_operand (type, arg1, arg0);
8827 if (operand_equal_p (arg0, arg1, 0))
8828 return non_lvalue (fold_convert (type, arg0));
8830 /* ~X & X is always zero. */
8831 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8832 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8833 return omit_one_operand (type, integer_zero_node, arg1);
8835 /* X & ~X is always zero. */
8836 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8837 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8838 return omit_one_operand (type, integer_zero_node, arg0);
8840 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
8841 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8842 && TREE_CODE (arg1) == INTEGER_CST
8843 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8844 return fold_build2 (BIT_IOR_EXPR, type,
8845 fold_build2 (BIT_AND_EXPR, type,
8846 TREE_OPERAND (arg0, 0), arg1),
8847 fold_build2 (BIT_AND_EXPR, type,
8848 TREE_OPERAND (arg0, 1), arg1));
8850 /* (X | Y) & Y is (X, Y). */
8851 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8852 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8853 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8854 /* (X | Y) & X is (Y, X). */
8855 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8856 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8857 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8858 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8859 /* X & (X | Y) is (Y, X). */
8860 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8861 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8862 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8863 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8864 /* X & (Y | X) is (Y, X). */
8865 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8866 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8867 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8868 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8870 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
8871 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8872 && integer_onep (TREE_OPERAND (arg0, 1))
8873 && integer_onep (arg1))
8875 tem = TREE_OPERAND (arg0, 0);
8876 return fold_build2 (EQ_EXPR, type,
8877 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8878 build_int_cst (TREE_TYPE (tem), 1)),
8879 build_int_cst (TREE_TYPE (tem), 0));
8881 /* Fold ~X & 1 as (X & 1) == 0. */
8882 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8883 && integer_onep (arg1))
8885 tem = TREE_OPERAND (arg0, 0);
8886 return fold_build2 (EQ_EXPR, type,
8887 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8888 build_int_cst (TREE_TYPE (tem), 1)),
8889 build_int_cst (TREE_TYPE (tem), 0));
8892 /* Fold (X ^ Y) & Y as ~X & Y. */
8893 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8894 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8896 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
8897 return fold_build2 (BIT_AND_EXPR, type,
8898 fold_build1 (BIT_NOT_EXPR, type, tem),
8899 fold_convert (type, arg1));
8901 /* Fold (X ^ Y) & X as ~Y & X. */
8902 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8903 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8904 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8906 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
8907 return fold_build2 (BIT_AND_EXPR, type,
8908 fold_build1 (BIT_NOT_EXPR, type, tem),
8909 fold_convert (type, arg1));
8911 /* Fold X & (X ^ Y) as X & ~Y. */
8912 if (TREE_CODE (arg1) == BIT_XOR_EXPR
8913 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8915 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
8916 return fold_build2 (BIT_AND_EXPR, type,
8917 fold_convert (type, arg0),
8918 fold_build1 (BIT_NOT_EXPR, type, tem));
8920 /* Fold X & (Y ^ X) as ~Y & X. */
8921 if (TREE_CODE (arg1) == BIT_XOR_EXPR
8922 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8923 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8925 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
8926 return fold_build2 (BIT_AND_EXPR, type,
8927 fold_build1 (BIT_NOT_EXPR, type, tem),
8928 fold_convert (type, arg0));
8931 t1 = distribute_bit_expr (code, type, arg0, arg1);
8932 if (t1 != NULL_TREE)
8933 return t1;
8934 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
8935 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
8936 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8938 unsigned int prec
8939 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
8941 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
8942 && (~TREE_INT_CST_LOW (arg1)
8943 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
8944 return fold_convert (type, TREE_OPERAND (arg0, 0));
8947 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
8949 This results in more efficient code for machines without a NOR
8950 instruction. Combine will canonicalize to the first form
8951 which will allow use of NOR instructions provided by the
8952 backend if they exist. */
8953 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8954 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8956 return fold_build1 (BIT_NOT_EXPR, type,
8957 build2 (BIT_IOR_EXPR, type,
8958 TREE_OPERAND (arg0, 0),
8959 TREE_OPERAND (arg1, 0)));
8962 goto associate;
8964 case RDIV_EXPR:
8965 /* Don't touch a floating-point divide by zero unless the mode
8966 of the constant can represent infinity. */
8967 if (TREE_CODE (arg1) == REAL_CST
8968 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
8969 && real_zerop (arg1))
8970 return NULL_TREE;
8972 /* Optimize A / A to 1.0 if we don't care about
8973 NaNs or Infinities. Skip the transformation
8974 for non-real operands. */
8975 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
8976 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8977 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
8978 && operand_equal_p (arg0, arg1, 0))
8980 tree r = build_real (TREE_TYPE (arg0), dconst1);
8982 return omit_two_operands (type, r, arg0, arg1);
8985 /* The complex version of the above A / A optimization. */
8986 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
8987 && operand_equal_p (arg0, arg1, 0))
8989 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
8990 if (! HONOR_NANS (TYPE_MODE (elem_type))
8991 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
8993 tree r = build_real (elem_type, dconst1);
8994 /* omit_two_operands will call fold_convert for us. */
8995 return omit_two_operands (type, r, arg0, arg1);
8999 /* (-A) / (-B) -> A / B */
9000 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9001 return fold_build2 (RDIV_EXPR, type,
9002 TREE_OPERAND (arg0, 0),
9003 negate_expr (arg1));
9004 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9005 return fold_build2 (RDIV_EXPR, type,
9006 negate_expr (arg0),
9007 TREE_OPERAND (arg1, 0));
9009 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
9010 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9011 && real_onep (arg1))
9012 return non_lvalue (fold_convert (type, arg0));
9014 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
9015 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9016 && real_minus_onep (arg1))
9017 return non_lvalue (fold_convert (type, negate_expr (arg0)));
9019 /* If ARG1 is a constant, we can convert this to a multiply by the
9020 reciprocal. This does not have the same rounding properties,
9021 so only do this if -funsafe-math-optimizations. We can actually
9022 always safely do it if ARG1 is a power of two, but it's hard to
9023 tell if it is or not in a portable manner. */
9024 if (TREE_CODE (arg1) == REAL_CST)
9026 if (flag_unsafe_math_optimizations
9027 && 0 != (tem = const_binop (code, build_real (type, dconst1),
9028 arg1, 0)))
9029 return fold_build2 (MULT_EXPR, type, arg0, tem);
9030 /* Find the reciprocal if optimizing and the result is exact. */
9031 if (optimize)
9033 REAL_VALUE_TYPE r;
9034 r = TREE_REAL_CST (arg1);
9035 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
9037 tem = build_real (type, r);
9038 return fold_build2 (MULT_EXPR, type,
9039 fold_convert (type, arg0), tem);
9043 /* Convert A/B/C to A/(B*C). */
9044 if (flag_unsafe_math_optimizations
9045 && TREE_CODE (arg0) == RDIV_EXPR)
9046 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
9047 fold_build2 (MULT_EXPR, type,
9048 TREE_OPERAND (arg0, 1), arg1));
9050 /* Convert A/(B/C) to (A/B)*C. */
9051 if (flag_unsafe_math_optimizations
9052 && TREE_CODE (arg1) == RDIV_EXPR)
9053 return fold_build2 (MULT_EXPR, type,
9054 fold_build2 (RDIV_EXPR, type, arg0,
9055 TREE_OPERAND (arg1, 0)),
9056 TREE_OPERAND (arg1, 1));
9058 /* Convert C1/(X*C2) into (C1/C2)/X. */
9059 if (flag_unsafe_math_optimizations
9060 && TREE_CODE (arg1) == MULT_EXPR
9061 && TREE_CODE (arg0) == REAL_CST
9062 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
9064 tree tem = const_binop (RDIV_EXPR, arg0,
9065 TREE_OPERAND (arg1, 1), 0);
9066 if (tem)
9067 return fold_build2 (RDIV_EXPR, type, tem,
9068 TREE_OPERAND (arg1, 0));
9071 if (flag_unsafe_math_optimizations)
9073 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
9074 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
9076 /* Optimize sin(x)/cos(x) as tan(x). */
9077 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
9078 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
9079 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
9080 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9081 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9083 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9085 if (tanfn != NULL_TREE)
9086 return build_function_call_expr (tanfn,
9087 TREE_OPERAND (arg0, 1));
9090 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
9091 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
9092 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
9093 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
9094 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9095 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9097 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9099 if (tanfn != NULL_TREE)
9101 tree tmp = TREE_OPERAND (arg0, 1);
9102 tmp = build_function_call_expr (tanfn, tmp);
9103 return fold_build2 (RDIV_EXPR, type,
9104 build_real (type, dconst1), tmp);
9108 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
9109 NaNs or Infinities. */
9110 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
9111 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
9112 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
9114 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9115 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9117 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9118 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9119 && operand_equal_p (arg00, arg01, 0))
9121 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9123 if (cosfn != NULL_TREE)
9124 return build_function_call_expr (cosfn,
9125 TREE_OPERAND (arg0, 1));
9129 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
9130 NaNs or Infinities. */
9131 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
9132 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
9133 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
9135 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9136 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9138 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9139 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9140 && operand_equal_p (arg00, arg01, 0))
9142 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9144 if (cosfn != NULL_TREE)
9146 tree tmp = TREE_OPERAND (arg0, 1);
9147 tmp = build_function_call_expr (cosfn, tmp);
9148 return fold_build2 (RDIV_EXPR, type,
9149 build_real (type, dconst1),
9150 tmp);
9155 /* Optimize pow(x,c)/x as pow(x,c-1). */
9156 if (fcode0 == BUILT_IN_POW
9157 || fcode0 == BUILT_IN_POWF
9158 || fcode0 == BUILT_IN_POWL)
9160 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9161 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
9162 if (TREE_CODE (arg01) == REAL_CST
9163 && ! TREE_CONSTANT_OVERFLOW (arg01)
9164 && operand_equal_p (arg1, arg00, 0))
9166 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9167 REAL_VALUE_TYPE c;
9168 tree arg, arglist;
9170 c = TREE_REAL_CST (arg01);
9171 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
9172 arg = build_real (type, c);
9173 arglist = build_tree_list (NULL_TREE, arg);
9174 arglist = tree_cons (NULL_TREE, arg1, arglist);
9175 return build_function_call_expr (powfn, arglist);
9179 /* Optimize x/expN(y) into x*expN(-y). */
9180 if (BUILTIN_EXPONENT_P (fcode1))
9182 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9183 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
9184 tree arglist = build_tree_list (NULL_TREE,
9185 fold_convert (type, arg));
9186 arg1 = build_function_call_expr (expfn, arglist);
9187 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9190 /* Optimize x/pow(y,z) into x*pow(y,-z). */
9191 if (fcode1 == BUILT_IN_POW
9192 || fcode1 == BUILT_IN_POWF
9193 || fcode1 == BUILT_IN_POWL)
9195 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9196 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9197 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
9198 tree neg11 = fold_convert (type, negate_expr (arg11));
9199 tree arglist = tree_cons(NULL_TREE, arg10,
9200 build_tree_list (NULL_TREE, neg11));
9201 arg1 = build_function_call_expr (powfn, arglist);
9202 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9205 return NULL_TREE;
9207 case TRUNC_DIV_EXPR:
9208 case ROUND_DIV_EXPR:
9209 case FLOOR_DIV_EXPR:
9210 case CEIL_DIV_EXPR:
9211 case EXACT_DIV_EXPR:
9212 if (integer_onep (arg1))
9213 return non_lvalue (fold_convert (type, arg0));
9214 if (integer_zerop (arg1))
9215 return NULL_TREE;
9216 /* X / -1 is -X. */
9217 if (!TYPE_UNSIGNED (type)
9218 && TREE_CODE (arg1) == INTEGER_CST
9219 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9220 && TREE_INT_CST_HIGH (arg1) == -1)
9221 return fold_convert (type, negate_expr (arg0));
9223 /* Convert -A / -B to A / B when the type is signed and overflow is
9224 undefined. */
9225 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9226 && TREE_CODE (arg0) == NEGATE_EXPR
9227 && negate_expr_p (arg1))
9228 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9229 negate_expr (arg1));
9230 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9231 && TREE_CODE (arg1) == NEGATE_EXPR
9232 && negate_expr_p (arg0))
9233 return fold_build2 (code, type, negate_expr (arg0),
9234 TREE_OPERAND (arg1, 0));
9236 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
9237 operation, EXACT_DIV_EXPR.
9239 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
9240 At one time others generated faster code, it's not clear if they do
9241 after the last round to changes to the DIV code in expmed.c. */
9242 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
9243 && multiple_of_p (type, arg0, arg1))
9244 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
9246 if (TREE_CODE (arg1) == INTEGER_CST
9247 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9248 return fold_convert (type, tem);
9250 return NULL_TREE;
9252 case CEIL_MOD_EXPR:
9253 case FLOOR_MOD_EXPR:
9254 case ROUND_MOD_EXPR:
9255 case TRUNC_MOD_EXPR:
9256 /* X % 1 is always zero, but be sure to preserve any side
9257 effects in X. */
9258 if (integer_onep (arg1))
9259 return omit_one_operand (type, integer_zero_node, arg0);
9261 /* X % 0, return X % 0 unchanged so that we can get the
9262 proper warnings and errors. */
9263 if (integer_zerop (arg1))
9264 return NULL_TREE;
9266 /* 0 % X is always zero, but be sure to preserve any side
9267 effects in X. Place this after checking for X == 0. */
9268 if (integer_zerop (arg0))
9269 return omit_one_operand (type, integer_zero_node, arg1);
9271 /* X % -1 is zero. */
9272 if (!TYPE_UNSIGNED (type)
9273 && TREE_CODE (arg1) == INTEGER_CST
9274 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9275 && TREE_INT_CST_HIGH (arg1) == -1)
9276 return omit_one_operand (type, integer_zero_node, arg0);
9278 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
9279 i.e. "X % C" into "X & C2", if X and C are positive. */
9280 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
9281 && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))
9282 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) >= 0)
9284 unsigned HOST_WIDE_INT high, low;
9285 tree mask;
9286 int l;
9288 l = tree_log2 (arg1);
9289 if (l >= HOST_BITS_PER_WIDE_INT)
9291 high = ((unsigned HOST_WIDE_INT) 1
9292 << (l - HOST_BITS_PER_WIDE_INT)) - 1;
9293 low = -1;
9295 else
9297 high = 0;
9298 low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
9301 mask = build_int_cst_wide (type, low, high);
9302 return fold_build2 (BIT_AND_EXPR, type,
9303 fold_convert (type, arg0), mask);
9306 /* X % -C is the same as X % C. */
9307 if (code == TRUNC_MOD_EXPR
9308 && !TYPE_UNSIGNED (type)
9309 && TREE_CODE (arg1) == INTEGER_CST
9310 && !TREE_CONSTANT_OVERFLOW (arg1)
9311 && TREE_INT_CST_HIGH (arg1) < 0
9312 && !flag_trapv
9313 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
9314 && !sign_bit_p (arg1, arg1))
9315 return fold_build2 (code, type, fold_convert (type, arg0),
9316 fold_convert (type, negate_expr (arg1)));
9318 /* X % -Y is the same as X % Y. */
9319 if (code == TRUNC_MOD_EXPR
9320 && !TYPE_UNSIGNED (type)
9321 && TREE_CODE (arg1) == NEGATE_EXPR
9322 && !flag_trapv)
9323 return fold_build2 (code, type, fold_convert (type, arg0),
9324 fold_convert (type, TREE_OPERAND (arg1, 0)));
9326 if (TREE_CODE (arg1) == INTEGER_CST
9327 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9328 return fold_convert (type, tem);
9330 return NULL_TREE;
9332 case LROTATE_EXPR:
9333 case RROTATE_EXPR:
9334 if (integer_all_onesp (arg0))
9335 return omit_one_operand (type, arg0, arg1);
9336 goto shift;
9338 case RSHIFT_EXPR:
9339 /* Optimize -1 >> x for arithmetic right shifts. */
9340 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
9341 return omit_one_operand (type, arg0, arg1);
9342 /* ... fall through ... */
9344 case LSHIFT_EXPR:
9345 shift:
9346 if (integer_zerop (arg1))
9347 return non_lvalue (fold_convert (type, arg0));
9348 if (integer_zerop (arg0))
9349 return omit_one_operand (type, arg0, arg1);
9351 /* Since negative shift count is not well-defined,
9352 don't try to compute it in the compiler. */
9353 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
9354 return NULL_TREE;
9356 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
9357 if (TREE_CODE (arg0) == code && host_integerp (arg1, false)
9358 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9359 && host_integerp (TREE_OPERAND (arg0, 1), false)
9360 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9362 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
9363 + TREE_INT_CST_LOW (arg1));
9365 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
9366 being well defined. */
9367 if (low >= TYPE_PRECISION (type))
9369 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
9370 low = low % TYPE_PRECISION (type);
9371 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
9372 return build_int_cst (type, 0);
9373 else
9374 low = TYPE_PRECISION (type) - 1;
9377 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9378 build_int_cst (type, low));
9381 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
9382 into x & ((unsigned)-1 >> c) for unsigned types. */
9383 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
9384 || (TYPE_UNSIGNED (type)
9385 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
9386 && host_integerp (arg1, false)
9387 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9388 && host_integerp (TREE_OPERAND (arg0, 1), false)
9389 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9391 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
9392 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
9393 tree lshift;
9394 tree arg00;
9396 if (low0 == low1)
9398 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9400 lshift = build_int_cst (type, -1);
9401 lshift = int_const_binop (code, lshift, arg1, 0);
9403 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
9407 /* Rewrite an LROTATE_EXPR by a constant into an
9408 RROTATE_EXPR by a new constant. */
9409 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
9411 tree tem = build_int_cst (NULL_TREE,
9412 GET_MODE_BITSIZE (TYPE_MODE (type)));
9413 tem = fold_convert (TREE_TYPE (arg1), tem);
9414 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
9415 return fold_build2 (RROTATE_EXPR, type, arg0, tem);
9418 /* If we have a rotate of a bit operation with the rotate count and
9419 the second operand of the bit operation both constant,
9420 permute the two operations. */
9421 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9422 && (TREE_CODE (arg0) == BIT_AND_EXPR
9423 || TREE_CODE (arg0) == BIT_IOR_EXPR
9424 || TREE_CODE (arg0) == BIT_XOR_EXPR)
9425 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9426 return fold_build2 (TREE_CODE (arg0), type,
9427 fold_build2 (code, type,
9428 TREE_OPERAND (arg0, 0), arg1),
9429 fold_build2 (code, type,
9430 TREE_OPERAND (arg0, 1), arg1));
9432 /* Two consecutive rotates adding up to the width of the mode can
9433 be ignored. */
9434 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9435 && TREE_CODE (arg0) == RROTATE_EXPR
9436 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9437 && TREE_INT_CST_HIGH (arg1) == 0
9438 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
9439 && ((TREE_INT_CST_LOW (arg1)
9440 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
9441 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
9442 return TREE_OPERAND (arg0, 0);
9444 return NULL_TREE;
9446 case MIN_EXPR:
9447 if (operand_equal_p (arg0, arg1, 0))
9448 return omit_one_operand (type, arg0, arg1);
9449 if (INTEGRAL_TYPE_P (type)
9450 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9451 return omit_one_operand (type, arg1, arg0);
9452 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
9453 if (tem)
9454 return tem;
9455 goto associate;
9457 case MAX_EXPR:
9458 if (operand_equal_p (arg0, arg1, 0))
9459 return omit_one_operand (type, arg0, arg1);
9460 if (INTEGRAL_TYPE_P (type)
9461 && TYPE_MAX_VALUE (type)
9462 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9463 return omit_one_operand (type, arg1, arg0);
9464 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
9465 if (tem)
9466 return tem;
9467 goto associate;
9469 case TRUTH_ANDIF_EXPR:
9470 /* Note that the operands of this must be ints
9471 and their values must be 0 or 1.
9472 ("true" is a fixed value perhaps depending on the language.) */
9473 /* If first arg is constant zero, return it. */
9474 if (integer_zerop (arg0))
9475 return fold_convert (type, arg0);
9476 case TRUTH_AND_EXPR:
9477 /* If either arg is constant true, drop it. */
9478 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9479 return non_lvalue (fold_convert (type, arg1));
9480 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
9481 /* Preserve sequence points. */
9482 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9483 return non_lvalue (fold_convert (type, arg0));
9484 /* If second arg is constant zero, result is zero, but first arg
9485 must be evaluated. */
9486 if (integer_zerop (arg1))
9487 return omit_one_operand (type, arg1, arg0);
9488 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9489 case will be handled here. */
9490 if (integer_zerop (arg0))
9491 return omit_one_operand (type, arg0, arg1);
9493 /* !X && X is always false. */
9494 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9495 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9496 return omit_one_operand (type, integer_zero_node, arg1);
9497 /* X && !X is always false. */
9498 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9499 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9500 return omit_one_operand (type, integer_zero_node, arg0);
9502 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
9503 means A >= Y && A != MAX, but in this case we know that
9504 A < X <= MAX. */
9506 if (!TREE_SIDE_EFFECTS (arg0)
9507 && !TREE_SIDE_EFFECTS (arg1))
9509 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
9510 if (tem && !operand_equal_p (tem, arg0, 0))
9511 return fold_build2 (code, type, tem, arg1);
9513 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
9514 if (tem && !operand_equal_p (tem, arg1, 0))
9515 return fold_build2 (code, type, arg0, tem);
9518 truth_andor:
9519 /* We only do these simplifications if we are optimizing. */
9520 if (!optimize)
9521 return NULL_TREE;
9523 /* Check for things like (A || B) && (A || C). We can convert this
9524 to A || (B && C). Note that either operator can be any of the four
9525 truth and/or operations and the transformation will still be
9526 valid. Also note that we only care about order for the
9527 ANDIF and ORIF operators. If B contains side effects, this
9528 might change the truth-value of A. */
9529 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9530 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9531 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9532 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9533 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9534 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9536 tree a00 = TREE_OPERAND (arg0, 0);
9537 tree a01 = TREE_OPERAND (arg0, 1);
9538 tree a10 = TREE_OPERAND (arg1, 0);
9539 tree a11 = TREE_OPERAND (arg1, 1);
9540 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9541 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9542 && (code == TRUTH_AND_EXPR
9543 || code == TRUTH_OR_EXPR));
9545 if (operand_equal_p (a00, a10, 0))
9546 return fold_build2 (TREE_CODE (arg0), type, a00,
9547 fold_build2 (code, type, a01, a11));
9548 else if (commutative && operand_equal_p (a00, a11, 0))
9549 return fold_build2 (TREE_CODE (arg0), type, a00,
9550 fold_build2 (code, type, a01, a10));
9551 else if (commutative && operand_equal_p (a01, a10, 0))
9552 return fold_build2 (TREE_CODE (arg0), type, a01,
9553 fold_build2 (code, type, a00, a11));
9555 /* This case if tricky because we must either have commutative
9556 operators or else A10 must not have side-effects. */
9558 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9559 && operand_equal_p (a01, a11, 0))
9560 return fold_build2 (TREE_CODE (arg0), type,
9561 fold_build2 (code, type, a00, a10),
9562 a01);
9565 /* See if we can build a range comparison. */
9566 if (0 != (tem = fold_range_test (code, type, op0, op1)))
9567 return tem;
9569 /* Check for the possibility of merging component references. If our
9570 lhs is another similar operation, try to merge its rhs with our
9571 rhs. Then try to merge our lhs and rhs. */
9572 if (TREE_CODE (arg0) == code
9573 && 0 != (tem = fold_truthop (code, type,
9574 TREE_OPERAND (arg0, 1), arg1)))
9575 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9577 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
9578 return tem;
9580 return NULL_TREE;
9582 case TRUTH_ORIF_EXPR:
9583 /* Note that the operands of this must be ints
9584 and their values must be 0 or true.
9585 ("true" is a fixed value perhaps depending on the language.) */
9586 /* If first arg is constant true, return it. */
9587 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9588 return fold_convert (type, arg0);
9589 case TRUTH_OR_EXPR:
9590 /* If either arg is constant zero, drop it. */
9591 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
9592 return non_lvalue (fold_convert (type, arg1));
9593 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
9594 /* Preserve sequence points. */
9595 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9596 return non_lvalue (fold_convert (type, arg0));
9597 /* If second arg is constant true, result is true, but we must
9598 evaluate first arg. */
9599 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
9600 return omit_one_operand (type, arg1, arg0);
9601 /* Likewise for first arg, but note this only occurs here for
9602 TRUTH_OR_EXPR. */
9603 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9604 return omit_one_operand (type, arg0, arg1);
9606 /* !X || X is always true. */
9607 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9608 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9609 return omit_one_operand (type, integer_one_node, arg1);
9610 /* X || !X is always true. */
9611 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9612 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9613 return omit_one_operand (type, integer_one_node, arg0);
9615 goto truth_andor;
9617 case TRUTH_XOR_EXPR:
9618 /* If the second arg is constant zero, drop it. */
9619 if (integer_zerop (arg1))
9620 return non_lvalue (fold_convert (type, arg0));
9621 /* If the second arg is constant true, this is a logical inversion. */
9622 if (integer_onep (arg1))
9624 /* Only call invert_truthvalue if operand is a truth value. */
9625 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
9626 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
9627 else
9628 tem = invert_truthvalue (arg0);
9629 return non_lvalue (fold_convert (type, tem));
9631 /* Identical arguments cancel to zero. */
9632 if (operand_equal_p (arg0, arg1, 0))
9633 return omit_one_operand (type, integer_zero_node, arg0);
9635 /* !X ^ X is always true. */
9636 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9637 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9638 return omit_one_operand (type, integer_one_node, arg1);
9640 /* X ^ !X is always true. */
9641 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9642 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9643 return omit_one_operand (type, integer_one_node, arg0);
9645 return NULL_TREE;
9647 case EQ_EXPR:
9648 case NE_EXPR:
9649 tem = fold_comparison (code, type, op0, op1);
9650 if (tem != NULL_TREE)
9651 return tem;
9653 /* bool_var != 0 becomes bool_var. */
9654 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9655 && code == NE_EXPR)
9656 return non_lvalue (fold_convert (type, arg0));
9658 /* bool_var == 1 becomes bool_var. */
9659 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9660 && code == EQ_EXPR)
9661 return non_lvalue (fold_convert (type, arg0));
9663 /* bool_var != 1 becomes !bool_var. */
9664 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9665 && code == NE_EXPR)
9666 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9668 /* bool_var == 0 becomes !bool_var. */
9669 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9670 && code == EQ_EXPR)
9671 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9673 /* ~a != C becomes a != ~C where C is a constant. Likewise for ==. */
9674 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9675 && TREE_CODE (arg1) == INTEGER_CST)
9676 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9677 fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
9678 arg1));
9680 /* If this is an equality comparison of the address of a non-weak
9681 object against zero, then we know the result. */
9682 if (TREE_CODE (arg0) == ADDR_EXPR
9683 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9684 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9685 && integer_zerop (arg1))
9686 return constant_boolean_node (code != EQ_EXPR, type);
9688 /* If this is an equality comparison of the address of two non-weak,
9689 unaliased symbols neither of which are extern (since we do not
9690 have access to attributes for externs), then we know the result. */
9691 if (TREE_CODE (arg0) == ADDR_EXPR
9692 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9693 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9694 && ! lookup_attribute ("alias",
9695 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
9696 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
9697 && TREE_CODE (arg1) == ADDR_EXPR
9698 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
9699 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
9700 && ! lookup_attribute ("alias",
9701 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
9702 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
9704 /* We know that we're looking at the address of two
9705 non-weak, unaliased, static _DECL nodes.
9707 It is both wasteful and incorrect to call operand_equal_p
9708 to compare the two ADDR_EXPR nodes. It is wasteful in that
9709 all we need to do is test pointer equality for the arguments
9710 to the two ADDR_EXPR nodes. It is incorrect to use
9711 operand_equal_p as that function is NOT equivalent to a
9712 C equality test. It can in fact return false for two
9713 objects which would test as equal using the C equality
9714 operator. */
9715 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
9716 return constant_boolean_node (equal
9717 ? code == EQ_EXPR : code != EQ_EXPR,
9718 type);
9721 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
9722 a MINUS_EXPR of a constant, we can convert it into a comparison with
9723 a revised constant as long as no overflow occurs. */
9724 if (TREE_CODE (arg1) == INTEGER_CST
9725 && (TREE_CODE (arg0) == PLUS_EXPR
9726 || TREE_CODE (arg0) == MINUS_EXPR)
9727 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9728 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9729 ? MINUS_EXPR : PLUS_EXPR,
9730 arg1, TREE_OPERAND (arg0, 1), 0))
9731 && ! TREE_CONSTANT_OVERFLOW (tem))
9732 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9734 /* Similarly for a NEGATE_EXPR. */
9735 if (TREE_CODE (arg0) == NEGATE_EXPR
9736 && TREE_CODE (arg1) == INTEGER_CST
9737 && 0 != (tem = negate_expr (arg1))
9738 && TREE_CODE (tem) == INTEGER_CST
9739 && ! TREE_CONSTANT_OVERFLOW (tem))
9740 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9742 /* If we have X - Y == 0, we can convert that to X == Y and similarly
9743 for !=. Don't do this for ordered comparisons due to overflow. */
9744 if (TREE_CODE (arg0) == MINUS_EXPR
9745 && integer_zerop (arg1))
9746 return fold_build2 (code, type,
9747 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
9749 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
9750 if (TREE_CODE (arg0) == ABS_EXPR
9751 && (integer_zerop (arg1) || real_zerop (arg1)))
9752 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
9754 /* If this is an EQ or NE comparison with zero and ARG0 is
9755 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
9756 two operations, but the latter can be done in one less insn
9757 on machines that have only two-operand insns or on which a
9758 constant cannot be the first operand. */
9759 if (TREE_CODE (arg0) == BIT_AND_EXPR
9760 && integer_zerop (arg1))
9762 tree arg00 = TREE_OPERAND (arg0, 0);
9763 tree arg01 = TREE_OPERAND (arg0, 1);
9764 if (TREE_CODE (arg00) == LSHIFT_EXPR
9765 && integer_onep (TREE_OPERAND (arg00, 0)))
9766 return
9767 fold_build2 (code, type,
9768 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9769 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
9770 arg01, TREE_OPERAND (arg00, 1)),
9771 fold_convert (TREE_TYPE (arg0),
9772 integer_one_node)),
9773 arg1);
9774 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
9775 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
9776 return
9777 fold_build2 (code, type,
9778 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9779 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9780 arg00, TREE_OPERAND (arg01, 1)),
9781 fold_convert (TREE_TYPE (arg0),
9782 integer_one_node)),
9783 arg1);
9786 /* If this is an NE or EQ comparison of zero against the result of a
9787 signed MOD operation whose second operand is a power of 2, make
9788 the MOD operation unsigned since it is simpler and equivalent. */
9789 if (integer_zerop (arg1)
9790 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9791 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9792 || TREE_CODE (arg0) == CEIL_MOD_EXPR
9793 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9794 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9795 && integer_pow2p (TREE_OPERAND (arg0, 1)))
9797 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9798 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
9799 fold_convert (newtype,
9800 TREE_OPERAND (arg0, 0)),
9801 fold_convert (newtype,
9802 TREE_OPERAND (arg0, 1)));
9804 return fold_build2 (code, type, newmod,
9805 fold_convert (newtype, arg1));
9808 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
9809 C1 is a valid shift constant, and C2 is a power of two, i.e.
9810 a single bit. */
9811 if (TREE_CODE (arg0) == BIT_AND_EXPR
9812 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
9813 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
9814 == INTEGER_CST
9815 && integer_pow2p (TREE_OPERAND (arg0, 1))
9816 && integer_zerop (arg1))
9818 tree itype = TREE_TYPE (arg0);
9819 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
9820 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
9822 /* Check for a valid shift count. */
9823 if (TREE_INT_CST_HIGH (arg001) == 0
9824 && TREE_INT_CST_LOW (arg001) < prec)
9826 tree arg01 = TREE_OPERAND (arg0, 1);
9827 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9828 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
9829 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
9830 can be rewritten as (X & (C2 << C1)) != 0. */
9831 if ((log2 + TREE_INT_CST_LOW (arg01)) < prec)
9833 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
9834 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
9835 return fold_build2 (code, type, tem, arg1);
9837 /* Otherwise, for signed (arithmetic) shifts,
9838 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
9839 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
9840 else if (!TYPE_UNSIGNED (itype))
9841 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
9842 arg000, build_int_cst (itype, 0));
9843 /* Otherwise, of unsigned (logical) shifts,
9844 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
9845 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
9846 else
9847 return omit_one_operand (type,
9848 code == EQ_EXPR ? integer_one_node
9849 : integer_zero_node,
9850 arg000);
9854 /* If this is an NE comparison of zero with an AND of one, remove the
9855 comparison since the AND will give the correct value. */
9856 if (code == NE_EXPR
9857 && integer_zerop (arg1)
9858 && TREE_CODE (arg0) == BIT_AND_EXPR
9859 && integer_onep (TREE_OPERAND (arg0, 1)))
9860 return fold_convert (type, arg0);
9862 /* If we have (A & C) == C where C is a power of 2, convert this into
9863 (A & C) != 0. Similarly for NE_EXPR. */
9864 if (TREE_CODE (arg0) == BIT_AND_EXPR
9865 && integer_pow2p (TREE_OPERAND (arg0, 1))
9866 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9867 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9868 arg0, fold_convert (TREE_TYPE (arg0),
9869 integer_zero_node));
9871 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
9872 bit, then fold the expression into A < 0 or A >= 0. */
9873 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
9874 if (tem)
9875 return tem;
9877 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9878 Similarly for NE_EXPR. */
9879 if (TREE_CODE (arg0) == BIT_AND_EXPR
9880 && TREE_CODE (arg1) == INTEGER_CST
9881 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9883 tree notc = fold_build1 (BIT_NOT_EXPR,
9884 TREE_TYPE (TREE_OPERAND (arg0, 1)),
9885 TREE_OPERAND (arg0, 1));
9886 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9887 arg1, notc);
9888 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9889 if (integer_nonzerop (dandnotc))
9890 return omit_one_operand (type, rslt, arg0);
9893 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9894 Similarly for NE_EXPR. */
9895 if (TREE_CODE (arg0) == BIT_IOR_EXPR
9896 && TREE_CODE (arg1) == INTEGER_CST
9897 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9899 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
9900 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9901 TREE_OPERAND (arg0, 1), notd);
9902 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9903 if (integer_nonzerop (candnotd))
9904 return omit_one_operand (type, rslt, arg0);
9907 /* If this is a comparison of a field, we may be able to simplify it. */
9908 if (((TREE_CODE (arg0) == COMPONENT_REF
9909 && lang_hooks.can_use_bit_fields_p ())
9910 || TREE_CODE (arg0) == BIT_FIELD_REF)
9911 /* Handle the constant case even without -O
9912 to make sure the warnings are given. */
9913 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9915 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9916 if (t1)
9917 return t1;
9920 /* Optimize comparisons of strlen vs zero to a compare of the
9921 first character of the string vs zero. To wit,
9922 strlen(ptr) == 0 => *ptr == 0
9923 strlen(ptr) != 0 => *ptr != 0
9924 Other cases should reduce to one of these two (or a constant)
9925 due to the return value of strlen being unsigned. */
9926 if (TREE_CODE (arg0) == CALL_EXPR
9927 && integer_zerop (arg1))
9929 tree fndecl = get_callee_fndecl (arg0);
9930 tree arglist;
9932 if (fndecl
9933 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9934 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9935 && (arglist = TREE_OPERAND (arg0, 1))
9936 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9937 && ! TREE_CHAIN (arglist))
9939 tree iref = build_fold_indirect_ref (TREE_VALUE (arglist));
9940 return fold_build2 (code, type, iref,
9941 build_int_cst (TREE_TYPE (iref), 0));
9945 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
9946 of X. Similarly fold (X >> C) == 0 into X >= 0. */
9947 if (TREE_CODE (arg0) == RSHIFT_EXPR
9948 && integer_zerop (arg1)
9949 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9951 tree arg00 = TREE_OPERAND (arg0, 0);
9952 tree arg01 = TREE_OPERAND (arg0, 1);
9953 tree itype = TREE_TYPE (arg00);
9954 if (TREE_INT_CST_HIGH (arg01) == 0
9955 && TREE_INT_CST_LOW (arg01)
9956 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
9958 if (TYPE_UNSIGNED (itype))
9960 itype = lang_hooks.types.signed_type (itype);
9961 arg00 = fold_convert (itype, arg00);
9963 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
9964 type, arg00, build_int_cst (itype, 0));
9968 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
9969 if (integer_zerop (arg1)
9970 && TREE_CODE (arg0) == BIT_XOR_EXPR)
9971 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9972 TREE_OPERAND (arg0, 1));
9974 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
9975 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9976 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9977 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9978 build_int_cst (TREE_TYPE (arg1), 0));
9979 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
9980 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9981 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9982 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9983 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
9984 build_int_cst (TREE_TYPE (arg1), 0));
9986 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
9987 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9988 && TREE_CODE (arg1) == INTEGER_CST
9989 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9990 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9991 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
9992 TREE_OPERAND (arg0, 1), arg1));
9994 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
9995 (X & C) == 0 when C is a single bit. */
9996 if (TREE_CODE (arg0) == BIT_AND_EXPR
9997 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
9998 && integer_zerop (arg1)
9999 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10001 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10002 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10003 TREE_OPERAND (arg0, 1));
10004 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10005 type, tem, arg1);
10008 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10009 constant C is a power of two, i.e. a single bit. */
10010 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10011 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10012 && integer_zerop (arg1)
10013 && integer_pow2p (TREE_OPERAND (arg0, 1))
10014 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10015 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10017 tree arg00 = TREE_OPERAND (arg0, 0);
10018 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10019 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10022 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10023 when is C is a power of two, i.e. a single bit. */
10024 if (TREE_CODE (arg0) == BIT_AND_EXPR
10025 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10026 && integer_zerop (arg1)
10027 && integer_pow2p (TREE_OPERAND (arg0, 1))
10028 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10029 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10031 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10032 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
10033 arg000, TREE_OPERAND (arg0, 1));
10034 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10035 tem, build_int_cst (TREE_TYPE (tem), 0));
10038 /* If this is a comparison of two exprs that look like an
10039 ARRAY_REF of the same object, then we can fold this to a
10040 comparison of the two offsets. This is only safe for
10041 EQ_EXPR and NE_EXPR because of overflow issues. */
10043 tree base0, offset0, base1, offset1;
10045 if (extract_array_ref (arg0, &base0, &offset0)
10046 && extract_array_ref (arg1, &base1, &offset1)
10047 && operand_equal_p (base0, base1, 0))
10049 /* Handle no offsets on both sides specially. */
10050 if (offset0 == NULL_TREE && offset1 == NULL_TREE)
10051 return fold_build2 (code, type, integer_zero_node,
10052 integer_zero_node);
10054 if (!offset0 || !offset1
10055 || TREE_TYPE (offset0) == TREE_TYPE (offset1))
10057 if (offset0 == NULL_TREE)
10058 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
10059 if (offset1 == NULL_TREE)
10060 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
10061 return fold_build2 (code, type, offset0, offset1);
10066 if (integer_zerop (arg1)
10067 && tree_expr_nonzero_p (arg0))
10069 tree res = constant_boolean_node (code==NE_EXPR, type);
10070 return omit_one_operand (type, res, arg0);
10072 return NULL_TREE;
10074 case LT_EXPR:
10075 case GT_EXPR:
10076 case LE_EXPR:
10077 case GE_EXPR:
10078 tem = fold_comparison (code, type, op0, op1);
10079 if (tem != NULL_TREE)
10080 return tem;
10082 /* Transform comparisons of the form X +- C CMP X. */
10083 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10084 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10085 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10086 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
10087 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10088 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
10089 && !(flag_wrapv || flag_trapv))))
10091 tree arg01 = TREE_OPERAND (arg0, 1);
10092 enum tree_code code0 = TREE_CODE (arg0);
10093 int is_positive;
10095 if (TREE_CODE (arg01) == REAL_CST)
10096 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10097 else
10098 is_positive = tree_int_cst_sgn (arg01);
10100 /* (X - c) > X becomes false. */
10101 if (code == GT_EXPR
10102 && ((code0 == MINUS_EXPR && is_positive >= 0)
10103 || (code0 == PLUS_EXPR && is_positive <= 0)))
10104 return constant_boolean_node (0, type);
10106 /* Likewise (X + c) < X becomes false. */
10107 if (code == LT_EXPR
10108 && ((code0 == PLUS_EXPR && is_positive >= 0)
10109 || (code0 == MINUS_EXPR && is_positive <= 0)))
10110 return constant_boolean_node (0, type);
10112 /* Convert (X - c) <= X to true. */
10113 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10114 && code == LE_EXPR
10115 && ((code0 == MINUS_EXPR && is_positive >= 0)
10116 || (code0 == PLUS_EXPR && is_positive <= 0)))
10117 return constant_boolean_node (1, type);
10119 /* Convert (X + c) >= X to true. */
10120 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10121 && code == GE_EXPR
10122 && ((code0 == PLUS_EXPR && is_positive >= 0)
10123 || (code0 == MINUS_EXPR && is_positive <= 0)))
10124 return constant_boolean_node (1, type);
10126 if (TREE_CODE (arg01) == INTEGER_CST)
10128 /* Convert X + c > X and X - c < X to true for integers. */
10129 if (code == GT_EXPR
10130 && ((code0 == PLUS_EXPR && is_positive > 0)
10131 || (code0 == MINUS_EXPR && is_positive < 0)))
10132 return constant_boolean_node (1, type);
10134 if (code == LT_EXPR
10135 && ((code0 == MINUS_EXPR && is_positive > 0)
10136 || (code0 == PLUS_EXPR && is_positive < 0)))
10137 return constant_boolean_node (1, type);
10139 /* Convert X + c <= X and X - c >= X to false for integers. */
10140 if (code == LE_EXPR
10141 && ((code0 == PLUS_EXPR && is_positive > 0)
10142 || (code0 == MINUS_EXPR && is_positive < 0)))
10143 return constant_boolean_node (0, type);
10145 if (code == GE_EXPR
10146 && ((code0 == MINUS_EXPR && is_positive > 0)
10147 || (code0 == PLUS_EXPR && is_positive < 0)))
10148 return constant_boolean_node (0, type);
10152 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
10153 This transformation affects the cases which are handled in later
10154 optimizations involving comparisons with non-negative constants. */
10155 if (TREE_CODE (arg1) == INTEGER_CST
10156 && TREE_CODE (arg0) != INTEGER_CST
10157 && tree_int_cst_sgn (arg1) > 0)
10159 if (code == GE_EXPR)
10161 arg1 = const_binop (MINUS_EXPR, arg1,
10162 build_int_cst (TREE_TYPE (arg1), 1), 0);
10163 return fold_build2 (GT_EXPR, type, arg0,
10164 fold_convert (TREE_TYPE (arg0), arg1));
10166 if (code == LT_EXPR)
10168 arg1 = const_binop (MINUS_EXPR, arg1,
10169 build_int_cst (TREE_TYPE (arg1), 1), 0);
10170 return fold_build2 (LE_EXPR, type, arg0,
10171 fold_convert (TREE_TYPE (arg0), arg1));
10175 /* Comparisons with the highest or lowest possible integer of
10176 the specified size will have known values. */
10178 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
10180 if (TREE_CODE (arg1) == INTEGER_CST
10181 && ! TREE_CONSTANT_OVERFLOW (arg1)
10182 && width <= 2 * HOST_BITS_PER_WIDE_INT
10183 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10184 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10186 HOST_WIDE_INT signed_max_hi;
10187 unsigned HOST_WIDE_INT signed_max_lo;
10188 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
10190 if (width <= HOST_BITS_PER_WIDE_INT)
10192 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10193 - 1;
10194 signed_max_hi = 0;
10195 max_hi = 0;
10197 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10199 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10200 min_lo = 0;
10201 min_hi = 0;
10203 else
10205 max_lo = signed_max_lo;
10206 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10207 min_hi = -1;
10210 else
10212 width -= HOST_BITS_PER_WIDE_INT;
10213 signed_max_lo = -1;
10214 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10215 - 1;
10216 max_lo = -1;
10217 min_lo = 0;
10219 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10221 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10222 min_hi = 0;
10224 else
10226 max_hi = signed_max_hi;
10227 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10231 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
10232 && TREE_INT_CST_LOW (arg1) == max_lo)
10233 switch (code)
10235 case GT_EXPR:
10236 return omit_one_operand (type, integer_zero_node, arg0);
10238 case GE_EXPR:
10239 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10241 case LE_EXPR:
10242 return omit_one_operand (type, integer_one_node, arg0);
10244 case LT_EXPR:
10245 return fold_build2 (NE_EXPR, type, arg0, arg1);
10247 /* The GE_EXPR and LT_EXPR cases above are not normally
10248 reached because of previous transformations. */
10250 default:
10251 break;
10253 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10254 == max_hi
10255 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
10256 switch (code)
10258 case GT_EXPR:
10259 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10260 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10261 case LE_EXPR:
10262 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10263 return fold_build2 (NE_EXPR, type, arg0, arg1);
10264 default:
10265 break;
10267 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10268 == min_hi
10269 && TREE_INT_CST_LOW (arg1) == min_lo)
10270 switch (code)
10272 case LT_EXPR:
10273 return omit_one_operand (type, integer_zero_node, arg0);
10275 case LE_EXPR:
10276 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10278 case GE_EXPR:
10279 return omit_one_operand (type, integer_one_node, arg0);
10281 case GT_EXPR:
10282 return fold_build2 (NE_EXPR, type, op0, op1);
10284 default:
10285 break;
10287 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10288 == min_hi
10289 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
10290 switch (code)
10292 case GE_EXPR:
10293 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10294 return fold_build2 (NE_EXPR, type, arg0, arg1);
10295 case LT_EXPR:
10296 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10297 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10298 default:
10299 break;
10302 else if (!in_gimple_form
10303 && TREE_INT_CST_HIGH (arg1) == signed_max_hi
10304 && TREE_INT_CST_LOW (arg1) == signed_max_lo
10305 && TYPE_UNSIGNED (TREE_TYPE (arg1))
10306 /* signed_type does not work on pointer types. */
10307 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
10309 /* The following case also applies to X < signed_max+1
10310 and X >= signed_max+1 because previous transformations. */
10311 if (code == LE_EXPR || code == GT_EXPR)
10313 tree st0, st1;
10314 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
10315 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
10316 return fold_build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
10317 type, fold_convert (st0, arg0),
10318 build_int_cst (st1, 0));
10324 /* If we are comparing an ABS_EXPR with a constant, we can
10325 convert all the cases into explicit comparisons, but they may
10326 well not be faster than doing the ABS and one comparison.
10327 But ABS (X) <= C is a range comparison, which becomes a subtraction
10328 and a comparison, and is probably faster. */
10329 if (code == LE_EXPR
10330 && TREE_CODE (arg1) == INTEGER_CST
10331 && TREE_CODE (arg0) == ABS_EXPR
10332 && ! TREE_SIDE_EFFECTS (arg0)
10333 && (0 != (tem = negate_expr (arg1)))
10334 && TREE_CODE (tem) == INTEGER_CST
10335 && ! TREE_CONSTANT_OVERFLOW (tem))
10336 return fold_build2 (TRUTH_ANDIF_EXPR, type,
10337 build2 (GE_EXPR, type,
10338 TREE_OPERAND (arg0, 0), tem),
10339 build2 (LE_EXPR, type,
10340 TREE_OPERAND (arg0, 0), arg1));
10342 /* Convert ABS_EXPR<x> >= 0 to true. */
10343 if (code == GE_EXPR
10344 && tree_expr_nonnegative_p (arg0)
10345 && (integer_zerop (arg1)
10346 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10347 && real_zerop (arg1))))
10348 return omit_one_operand (type, integer_one_node, arg0);
10350 /* Convert ABS_EXPR<x> < 0 to false. */
10351 if (code == LT_EXPR
10352 && tree_expr_nonnegative_p (arg0)
10353 && (integer_zerop (arg1) || real_zerop (arg1)))
10354 return omit_one_operand (type, integer_zero_node, arg0);
10356 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
10357 and similarly for >= into !=. */
10358 if ((code == LT_EXPR || code == GE_EXPR)
10359 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10360 && TREE_CODE (arg1) == LSHIFT_EXPR
10361 && integer_onep (TREE_OPERAND (arg1, 0)))
10362 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10363 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10364 TREE_OPERAND (arg1, 1)),
10365 build_int_cst (TREE_TYPE (arg0), 0));
10367 if ((code == LT_EXPR || code == GE_EXPR)
10368 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10369 && (TREE_CODE (arg1) == NOP_EXPR
10370 || TREE_CODE (arg1) == CONVERT_EXPR)
10371 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
10372 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
10373 return
10374 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10375 fold_convert (TREE_TYPE (arg0),
10376 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10377 TREE_OPERAND (TREE_OPERAND (arg1, 0),
10378 1))),
10379 build_int_cst (TREE_TYPE (arg0), 0));
10381 return NULL_TREE;
10383 case UNORDERED_EXPR:
10384 case ORDERED_EXPR:
10385 case UNLT_EXPR:
10386 case UNLE_EXPR:
10387 case UNGT_EXPR:
10388 case UNGE_EXPR:
10389 case UNEQ_EXPR:
10390 case LTGT_EXPR:
10391 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10393 t1 = fold_relational_const (code, type, arg0, arg1);
10394 if (t1 != NULL_TREE)
10395 return t1;
10398 /* If the first operand is NaN, the result is constant. */
10399 if (TREE_CODE (arg0) == REAL_CST
10400 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
10401 && (code != LTGT_EXPR || ! flag_trapping_math))
10403 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10404 ? integer_zero_node
10405 : integer_one_node;
10406 return omit_one_operand (type, t1, arg1);
10409 /* If the second operand is NaN, the result is constant. */
10410 if (TREE_CODE (arg1) == REAL_CST
10411 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
10412 && (code != LTGT_EXPR || ! flag_trapping_math))
10414 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10415 ? integer_zero_node
10416 : integer_one_node;
10417 return omit_one_operand (type, t1, arg0);
10420 /* Simplify unordered comparison of something with itself. */
10421 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
10422 && operand_equal_p (arg0, arg1, 0))
10423 return constant_boolean_node (1, type);
10425 if (code == LTGT_EXPR
10426 && !flag_trapping_math
10427 && operand_equal_p (arg0, arg1, 0))
10428 return constant_boolean_node (0, type);
10430 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
10432 tree targ0 = strip_float_extensions (arg0);
10433 tree targ1 = strip_float_extensions (arg1);
10434 tree newtype = TREE_TYPE (targ0);
10436 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
10437 newtype = TREE_TYPE (targ1);
10439 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
10440 return fold_build2 (code, type, fold_convert (newtype, targ0),
10441 fold_convert (newtype, targ1));
10444 return NULL_TREE;
10446 case COMPOUND_EXPR:
10447 /* When pedantic, a compound expression can be neither an lvalue
10448 nor an integer constant expression. */
10449 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
10450 return NULL_TREE;
10451 /* Don't let (0, 0) be null pointer constant. */
10452 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
10453 : fold_convert (type, arg1);
10454 return pedantic_non_lvalue (tem);
10456 case COMPLEX_EXPR:
10457 if ((TREE_CODE (arg0) == REAL_CST
10458 && TREE_CODE (arg1) == REAL_CST)
10459 || (TREE_CODE (arg0) == INTEGER_CST
10460 && TREE_CODE (arg1) == INTEGER_CST))
10461 return build_complex (type, arg0, arg1);
10462 return NULL_TREE;
10464 case ASSERT_EXPR:
10465 /* An ASSERT_EXPR should never be passed to fold_binary. */
10466 gcc_unreachable ();
10468 default:
10469 return NULL_TREE;
10470 } /* switch (code) */
10473 /* Callback for walk_tree, looking for LABEL_EXPR.
10474 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
10475 Do not check the sub-tree of GOTO_EXPR. */
10477 static tree
10478 contains_label_1 (tree *tp,
10479 int *walk_subtrees,
10480 void *data ATTRIBUTE_UNUSED)
10482 switch (TREE_CODE (*tp))
10484 case LABEL_EXPR:
10485 return *tp;
10486 case GOTO_EXPR:
10487 *walk_subtrees = 0;
10488 /* no break */
10489 default:
10490 return NULL_TREE;
10494 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
10495 accessible from outside the sub-tree. Returns NULL_TREE if no
10496 addressable label is found. */
10498 static bool
10499 contains_label_p (tree st)
10501 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
10504 /* Fold a ternary expression of code CODE and type TYPE with operands
10505 OP0, OP1, and OP2. Return the folded expression if folding is
10506 successful. Otherwise, return NULL_TREE. */
10508 tree
10509 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
10511 tree tem;
10512 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
10513 enum tree_code_class kind = TREE_CODE_CLASS (code);
10515 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10516 && TREE_CODE_LENGTH (code) == 3);
10518 /* Strip any conversions that don't change the mode. This is safe
10519 for every expression, except for a comparison expression because
10520 its signedness is derived from its operands. So, in the latter
10521 case, only strip conversions that don't change the signedness.
10523 Note that this is done as an internal manipulation within the
10524 constant folder, in order to find the simplest representation of
10525 the arguments so that their form can be studied. In any cases,
10526 the appropriate type conversions should be put back in the tree
10527 that will get out of the constant folder. */
10528 if (op0)
10530 arg0 = op0;
10531 STRIP_NOPS (arg0);
10534 if (op1)
10536 arg1 = op1;
10537 STRIP_NOPS (arg1);
10540 switch (code)
10542 case COMPONENT_REF:
10543 if (TREE_CODE (arg0) == CONSTRUCTOR
10544 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
10546 unsigned HOST_WIDE_INT idx;
10547 tree field, value;
10548 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
10549 if (field == arg1)
10550 return value;
10552 return NULL_TREE;
10554 case COND_EXPR:
10555 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
10556 so all simple results must be passed through pedantic_non_lvalue. */
10557 if (TREE_CODE (arg0) == INTEGER_CST)
10559 tree unused_op = integer_zerop (arg0) ? op1 : op2;
10560 tem = integer_zerop (arg0) ? op2 : op1;
10561 /* Only optimize constant conditions when the selected branch
10562 has the same type as the COND_EXPR. This avoids optimizing
10563 away "c ? x : throw", where the throw has a void type.
10564 Avoid throwing away that operand which contains label. */
10565 if ((!TREE_SIDE_EFFECTS (unused_op)
10566 || !contains_label_p (unused_op))
10567 && (! VOID_TYPE_P (TREE_TYPE (tem))
10568 || VOID_TYPE_P (type)))
10569 return pedantic_non_lvalue (tem);
10570 return NULL_TREE;
10572 if (operand_equal_p (arg1, op2, 0))
10573 return pedantic_omit_one_operand (type, arg1, arg0);
10575 /* If we have A op B ? A : C, we may be able to convert this to a
10576 simpler expression, depending on the operation and the values
10577 of B and C. Signed zeros prevent all of these transformations,
10578 for reasons given above each one.
10580 Also try swapping the arguments and inverting the conditional. */
10581 if (COMPARISON_CLASS_P (arg0)
10582 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10583 arg1, TREE_OPERAND (arg0, 1))
10584 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
10586 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
10587 if (tem)
10588 return tem;
10591 if (COMPARISON_CLASS_P (arg0)
10592 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10593 op2,
10594 TREE_OPERAND (arg0, 1))
10595 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
10597 tem = invert_truthvalue (arg0);
10598 if (COMPARISON_CLASS_P (tem))
10600 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
10601 if (tem)
10602 return tem;
10606 /* If the second operand is simpler than the third, swap them
10607 since that produces better jump optimization results. */
10608 if (truth_value_p (TREE_CODE (arg0))
10609 && tree_swap_operands_p (op1, op2, false))
10611 /* See if this can be inverted. If it can't, possibly because
10612 it was a floating-point inequality comparison, don't do
10613 anything. */
10614 tem = invert_truthvalue (arg0);
10616 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10617 return fold_build3 (code, type, tem, op2, op1);
10620 /* Convert A ? 1 : 0 to simply A. */
10621 if (integer_onep (op1)
10622 && integer_zerop (op2)
10623 /* If we try to convert OP0 to our type, the
10624 call to fold will try to move the conversion inside
10625 a COND, which will recurse. In that case, the COND_EXPR
10626 is probably the best choice, so leave it alone. */
10627 && type == TREE_TYPE (arg0))
10628 return pedantic_non_lvalue (arg0);
10630 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
10631 over COND_EXPR in cases such as floating point comparisons. */
10632 if (integer_zerop (op1)
10633 && integer_onep (op2)
10634 && truth_value_p (TREE_CODE (arg0)))
10635 return pedantic_non_lvalue (fold_convert (type,
10636 invert_truthvalue (arg0)));
10638 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
10639 if (TREE_CODE (arg0) == LT_EXPR
10640 && integer_zerop (TREE_OPERAND (arg0, 1))
10641 && integer_zerop (op2)
10642 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
10643 return fold_convert (type, fold_build2 (BIT_AND_EXPR,
10644 TREE_TYPE (tem), tem, arg1));
10646 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
10647 already handled above. */
10648 if (TREE_CODE (arg0) == BIT_AND_EXPR
10649 && integer_onep (TREE_OPERAND (arg0, 1))
10650 && integer_zerop (op2)
10651 && integer_pow2p (arg1))
10653 tree tem = TREE_OPERAND (arg0, 0);
10654 STRIP_NOPS (tem);
10655 if (TREE_CODE (tem) == RSHIFT_EXPR
10656 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
10657 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
10658 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
10659 return fold_build2 (BIT_AND_EXPR, type,
10660 TREE_OPERAND (tem, 0), arg1);
10663 /* A & N ? N : 0 is simply A & N if N is a power of two. This
10664 is probably obsolete because the first operand should be a
10665 truth value (that's why we have the two cases above), but let's
10666 leave it in until we can confirm this for all front-ends. */
10667 if (integer_zerop (op2)
10668 && TREE_CODE (arg0) == NE_EXPR
10669 && integer_zerop (TREE_OPERAND (arg0, 1))
10670 && integer_pow2p (arg1)
10671 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10672 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10673 arg1, OEP_ONLY_CONST))
10674 return pedantic_non_lvalue (fold_convert (type,
10675 TREE_OPERAND (arg0, 0)));
10677 /* Convert A ? B : 0 into A && B if A and B are truth values. */
10678 if (integer_zerop (op2)
10679 && truth_value_p (TREE_CODE (arg0))
10680 && truth_value_p (TREE_CODE (arg1)))
10681 return fold_build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1);
10683 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
10684 if (integer_onep (op2)
10685 && truth_value_p (TREE_CODE (arg0))
10686 && truth_value_p (TREE_CODE (arg1)))
10688 /* Only perform transformation if ARG0 is easily inverted. */
10689 tem = invert_truthvalue (arg0);
10690 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10691 return fold_build2 (TRUTH_ORIF_EXPR, type, tem, arg1);
10694 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
10695 if (integer_zerop (arg1)
10696 && truth_value_p (TREE_CODE (arg0))
10697 && truth_value_p (TREE_CODE (op2)))
10699 /* Only perform transformation if ARG0 is easily inverted. */
10700 tem = invert_truthvalue (arg0);
10701 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10702 return fold_build2 (TRUTH_ANDIF_EXPR, type, tem, op2);
10705 /* Convert A ? 1 : B into A || B if A and B are truth values. */
10706 if (integer_onep (arg1)
10707 && truth_value_p (TREE_CODE (arg0))
10708 && truth_value_p (TREE_CODE (op2)))
10709 return fold_build2 (TRUTH_ORIF_EXPR, type, arg0, op2);
10711 return NULL_TREE;
10713 case CALL_EXPR:
10714 /* Check for a built-in function. */
10715 if (TREE_CODE (op0) == ADDR_EXPR
10716 && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
10717 && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
10718 return fold_builtin (TREE_OPERAND (op0, 0), op1, false);
10719 return NULL_TREE;
10721 case BIT_FIELD_REF:
10722 if (TREE_CODE (arg0) == VECTOR_CST
10723 && type == TREE_TYPE (TREE_TYPE (arg0))
10724 && host_integerp (arg1, 1)
10725 && host_integerp (op2, 1))
10727 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
10728 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
10730 if (width != 0
10731 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
10732 && (idx % width) == 0
10733 && (idx = idx / width)
10734 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
10736 tree elements = TREE_VECTOR_CST_ELTS (arg0);
10737 while (idx-- > 0 && elements)
10738 elements = TREE_CHAIN (elements);
10739 if (elements)
10740 return TREE_VALUE (elements);
10741 else
10742 return fold_convert (type, integer_zero_node);
10745 return NULL_TREE;
10747 default:
10748 return NULL_TREE;
10749 } /* switch (code) */
10752 /* Perform constant folding and related simplification of EXPR.
10753 The related simplifications include x*1 => x, x*0 => 0, etc.,
10754 and application of the associative law.
10755 NOP_EXPR conversions may be removed freely (as long as we
10756 are careful not to change the type of the overall expression).
10757 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
10758 but we can constant-fold them if they have constant operands. */
10760 #ifdef ENABLE_FOLD_CHECKING
10761 # define fold(x) fold_1 (x)
10762 static tree fold_1 (tree);
10763 static
10764 #endif
10765 tree
10766 fold (tree expr)
10768 const tree t = expr;
10769 enum tree_code code = TREE_CODE (t);
10770 enum tree_code_class kind = TREE_CODE_CLASS (code);
10771 tree tem;
10773 /* Return right away if a constant. */
10774 if (kind == tcc_constant)
10775 return t;
10777 if (IS_EXPR_CODE_CLASS (kind))
10779 tree type = TREE_TYPE (t);
10780 tree op0, op1, op2;
10782 switch (TREE_CODE_LENGTH (code))
10784 case 1:
10785 op0 = TREE_OPERAND (t, 0);
10786 tem = fold_unary (code, type, op0);
10787 return tem ? tem : expr;
10788 case 2:
10789 op0 = TREE_OPERAND (t, 0);
10790 op1 = TREE_OPERAND (t, 1);
10791 tem = fold_binary (code, type, op0, op1);
10792 return tem ? tem : expr;
10793 case 3:
10794 op0 = TREE_OPERAND (t, 0);
10795 op1 = TREE_OPERAND (t, 1);
10796 op2 = TREE_OPERAND (t, 2);
10797 tem = fold_ternary (code, type, op0, op1, op2);
10798 return tem ? tem : expr;
10799 default:
10800 break;
10804 switch (code)
10806 case CONST_DECL:
10807 return fold (DECL_INITIAL (t));
10809 default:
10810 return t;
10811 } /* switch (code) */
10814 #ifdef ENABLE_FOLD_CHECKING
10815 #undef fold
10817 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
10818 static void fold_check_failed (tree, tree);
10819 void print_fold_checksum (tree);
10821 /* When --enable-checking=fold, compute a digest of expr before
10822 and after actual fold call to see if fold did not accidentally
10823 change original expr. */
10825 tree
10826 fold (tree expr)
10828 tree ret;
10829 struct md5_ctx ctx;
10830 unsigned char checksum_before[16], checksum_after[16];
10831 htab_t ht;
10833 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10834 md5_init_ctx (&ctx);
10835 fold_checksum_tree (expr, &ctx, ht);
10836 md5_finish_ctx (&ctx, checksum_before);
10837 htab_empty (ht);
10839 ret = fold_1 (expr);
10841 md5_init_ctx (&ctx);
10842 fold_checksum_tree (expr, &ctx, ht);
10843 md5_finish_ctx (&ctx, checksum_after);
10844 htab_delete (ht);
10846 if (memcmp (checksum_before, checksum_after, 16))
10847 fold_check_failed (expr, ret);
10849 return ret;
10852 void
10853 print_fold_checksum (tree expr)
10855 struct md5_ctx ctx;
10856 unsigned char checksum[16], cnt;
10857 htab_t ht;
10859 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10860 md5_init_ctx (&ctx);
10861 fold_checksum_tree (expr, &ctx, ht);
10862 md5_finish_ctx (&ctx, checksum);
10863 htab_delete (ht);
10864 for (cnt = 0; cnt < 16; ++cnt)
10865 fprintf (stderr, "%02x", checksum[cnt]);
10866 putc ('\n', stderr);
10869 static void
10870 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
10872 internal_error ("fold check: original tree changed by fold");
10875 static void
10876 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
10878 void **slot;
10879 enum tree_code code;
10880 struct tree_function_decl buf;
10881 int i, len;
10883 recursive_label:
10885 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
10886 <= sizeof (struct tree_function_decl))
10887 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
10888 if (expr == NULL)
10889 return;
10890 slot = htab_find_slot (ht, expr, INSERT);
10891 if (*slot != NULL)
10892 return;
10893 *slot = expr;
10894 code = TREE_CODE (expr);
10895 if (TREE_CODE_CLASS (code) == tcc_declaration
10896 && DECL_ASSEMBLER_NAME_SET_P (expr))
10898 /* Allow DECL_ASSEMBLER_NAME to be modified. */
10899 memcpy ((char *) &buf, expr, tree_size (expr));
10900 expr = (tree) &buf;
10901 SET_DECL_ASSEMBLER_NAME (expr, NULL);
10903 else if (TREE_CODE_CLASS (code) == tcc_type
10904 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
10905 || TYPE_CACHED_VALUES_P (expr)
10906 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
10908 /* Allow these fields to be modified. */
10909 memcpy ((char *) &buf, expr, tree_size (expr));
10910 expr = (tree) &buf;
10911 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
10912 TYPE_POINTER_TO (expr) = NULL;
10913 TYPE_REFERENCE_TO (expr) = NULL;
10914 if (TYPE_CACHED_VALUES_P (expr))
10916 TYPE_CACHED_VALUES_P (expr) = 0;
10917 TYPE_CACHED_VALUES (expr) = NULL;
10920 md5_process_bytes (expr, tree_size (expr), ctx);
10921 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
10922 if (TREE_CODE_CLASS (code) != tcc_type
10923 && TREE_CODE_CLASS (code) != tcc_declaration
10924 && code != TREE_LIST)
10925 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
10926 switch (TREE_CODE_CLASS (code))
10928 case tcc_constant:
10929 switch (code)
10931 case STRING_CST:
10932 md5_process_bytes (TREE_STRING_POINTER (expr),
10933 TREE_STRING_LENGTH (expr), ctx);
10934 break;
10935 case COMPLEX_CST:
10936 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
10937 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
10938 break;
10939 case VECTOR_CST:
10940 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
10941 break;
10942 default:
10943 break;
10945 break;
10946 case tcc_exceptional:
10947 switch (code)
10949 case TREE_LIST:
10950 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
10951 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
10952 expr = TREE_CHAIN (expr);
10953 goto recursive_label;
10954 break;
10955 case TREE_VEC:
10956 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
10957 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
10958 break;
10959 default:
10960 break;
10962 break;
10963 case tcc_expression:
10964 case tcc_reference:
10965 case tcc_comparison:
10966 case tcc_unary:
10967 case tcc_binary:
10968 case tcc_statement:
10969 len = TREE_CODE_LENGTH (code);
10970 for (i = 0; i < len; ++i)
10971 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
10972 break;
10973 case tcc_declaration:
10974 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
10975 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
10976 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
10978 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
10979 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
10980 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
10981 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
10982 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
10984 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
10985 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
10987 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
10989 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
10990 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
10991 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
10993 break;
10994 case tcc_type:
10995 if (TREE_CODE (expr) == ENUMERAL_TYPE)
10996 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
10997 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
10998 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
10999 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
11000 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
11001 if (INTEGRAL_TYPE_P (expr)
11002 || SCALAR_FLOAT_TYPE_P (expr))
11004 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
11005 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
11007 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
11008 if (TREE_CODE (expr) == RECORD_TYPE
11009 || TREE_CODE (expr) == UNION_TYPE
11010 || TREE_CODE (expr) == QUAL_UNION_TYPE)
11011 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
11012 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
11013 break;
11014 default:
11015 break;
11019 #endif
11021 /* Fold a unary tree expression with code CODE of type TYPE with an
11022 operand OP0. Return a folded expression if successful. Otherwise,
11023 return a tree expression with code CODE of type TYPE with an
11024 operand OP0. */
11026 tree
11027 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
11029 tree tem;
11030 #ifdef ENABLE_FOLD_CHECKING
11031 unsigned char checksum_before[16], checksum_after[16];
11032 struct md5_ctx ctx;
11033 htab_t ht;
11035 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11036 md5_init_ctx (&ctx);
11037 fold_checksum_tree (op0, &ctx, ht);
11038 md5_finish_ctx (&ctx, checksum_before);
11039 htab_empty (ht);
11040 #endif
11042 tem = fold_unary (code, type, op0);
11043 if (!tem)
11044 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
11046 #ifdef ENABLE_FOLD_CHECKING
11047 md5_init_ctx (&ctx);
11048 fold_checksum_tree (op0, &ctx, ht);
11049 md5_finish_ctx (&ctx, checksum_after);
11050 htab_delete (ht);
11052 if (memcmp (checksum_before, checksum_after, 16))
11053 fold_check_failed (op0, tem);
11054 #endif
11055 return tem;
11058 /* Fold a binary tree expression with code CODE of type TYPE with
11059 operands OP0 and OP1. Return a folded expression if successful.
11060 Otherwise, return a tree expression with code CODE of type TYPE
11061 with operands OP0 and OP1. */
11063 tree
11064 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
11065 MEM_STAT_DECL)
11067 tree tem;
11068 #ifdef ENABLE_FOLD_CHECKING
11069 unsigned char checksum_before_op0[16],
11070 checksum_before_op1[16],
11071 checksum_after_op0[16],
11072 checksum_after_op1[16];
11073 struct md5_ctx ctx;
11074 htab_t ht;
11076 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11077 md5_init_ctx (&ctx);
11078 fold_checksum_tree (op0, &ctx, ht);
11079 md5_finish_ctx (&ctx, checksum_before_op0);
11080 htab_empty (ht);
11082 md5_init_ctx (&ctx);
11083 fold_checksum_tree (op1, &ctx, ht);
11084 md5_finish_ctx (&ctx, checksum_before_op1);
11085 htab_empty (ht);
11086 #endif
11088 tem = fold_binary (code, type, op0, op1);
11089 if (!tem)
11090 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
11092 #ifdef ENABLE_FOLD_CHECKING
11093 md5_init_ctx (&ctx);
11094 fold_checksum_tree (op0, &ctx, ht);
11095 md5_finish_ctx (&ctx, checksum_after_op0);
11096 htab_empty (ht);
11098 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
11099 fold_check_failed (op0, tem);
11101 md5_init_ctx (&ctx);
11102 fold_checksum_tree (op1, &ctx, ht);
11103 md5_finish_ctx (&ctx, checksum_after_op1);
11104 htab_delete (ht);
11106 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
11107 fold_check_failed (op1, tem);
11108 #endif
11109 return tem;
11112 /* Fold a ternary tree expression with code CODE of type TYPE with
11113 operands OP0, OP1, and OP2. Return a folded expression if
11114 successful. Otherwise, return a tree expression with code CODE of
11115 type TYPE with operands OP0, OP1, and OP2. */
11117 tree
11118 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
11119 MEM_STAT_DECL)
11121 tree tem;
11122 #ifdef ENABLE_FOLD_CHECKING
11123 unsigned char checksum_before_op0[16],
11124 checksum_before_op1[16],
11125 checksum_before_op2[16],
11126 checksum_after_op0[16],
11127 checksum_after_op1[16],
11128 checksum_after_op2[16];
11129 struct md5_ctx ctx;
11130 htab_t ht;
11132 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11133 md5_init_ctx (&ctx);
11134 fold_checksum_tree (op0, &ctx, ht);
11135 md5_finish_ctx (&ctx, checksum_before_op0);
11136 htab_empty (ht);
11138 md5_init_ctx (&ctx);
11139 fold_checksum_tree (op1, &ctx, ht);
11140 md5_finish_ctx (&ctx, checksum_before_op1);
11141 htab_empty (ht);
11143 md5_init_ctx (&ctx);
11144 fold_checksum_tree (op2, &ctx, ht);
11145 md5_finish_ctx (&ctx, checksum_before_op2);
11146 htab_empty (ht);
11147 #endif
11149 tem = fold_ternary (code, type, op0, op1, op2);
11150 if (!tem)
11151 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
11153 #ifdef ENABLE_FOLD_CHECKING
11154 md5_init_ctx (&ctx);
11155 fold_checksum_tree (op0, &ctx, ht);
11156 md5_finish_ctx (&ctx, checksum_after_op0);
11157 htab_empty (ht);
11159 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
11160 fold_check_failed (op0, tem);
11162 md5_init_ctx (&ctx);
11163 fold_checksum_tree (op1, &ctx, ht);
11164 md5_finish_ctx (&ctx, checksum_after_op1);
11165 htab_empty (ht);
11167 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
11168 fold_check_failed (op1, tem);
11170 md5_init_ctx (&ctx);
11171 fold_checksum_tree (op2, &ctx, ht);
11172 md5_finish_ctx (&ctx, checksum_after_op2);
11173 htab_delete (ht);
11175 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
11176 fold_check_failed (op2, tem);
11177 #endif
11178 return tem;
11181 /* Perform constant folding and related simplification of initializer
11182 expression EXPR. These behave identically to "fold_buildN" but ignore
11183 potential run-time traps and exceptions that fold must preserve. */
11185 #define START_FOLD_INIT \
11186 int saved_signaling_nans = flag_signaling_nans;\
11187 int saved_trapping_math = flag_trapping_math;\
11188 int saved_rounding_math = flag_rounding_math;\
11189 int saved_trapv = flag_trapv;\
11190 flag_signaling_nans = 0;\
11191 flag_trapping_math = 0;\
11192 flag_rounding_math = 0;\
11193 flag_trapv = 0
11195 #define END_FOLD_INIT \
11196 flag_signaling_nans = saved_signaling_nans;\
11197 flag_trapping_math = saved_trapping_math;\
11198 flag_rounding_math = saved_rounding_math;\
11199 flag_trapv = saved_trapv
11201 tree
11202 fold_build1_initializer (enum tree_code code, tree type, tree op)
11204 tree result;
11205 START_FOLD_INIT;
11207 result = fold_build1 (code, type, op);
11209 END_FOLD_INIT;
11210 return result;
11213 tree
11214 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
11216 tree result;
11217 START_FOLD_INIT;
11219 result = fold_build2 (code, type, op0, op1);
11221 END_FOLD_INIT;
11222 return result;
11225 tree
11226 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
11227 tree op2)
11229 tree result;
11230 START_FOLD_INIT;
11232 result = fold_build3 (code, type, op0, op1, op2);
11234 END_FOLD_INIT;
11235 return result;
11238 #undef START_FOLD_INIT
11239 #undef END_FOLD_INIT
11241 /* Determine if first argument is a multiple of second argument. Return 0 if
11242 it is not, or we cannot easily determined it to be.
11244 An example of the sort of thing we care about (at this point; this routine
11245 could surely be made more general, and expanded to do what the *_DIV_EXPR's
11246 fold cases do now) is discovering that
11248 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11250 is a multiple of
11252 SAVE_EXPR (J * 8)
11254 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
11256 This code also handles discovering that
11258 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11260 is a multiple of 8 so we don't have to worry about dealing with a
11261 possible remainder.
11263 Note that we *look* inside a SAVE_EXPR only to determine how it was
11264 calculated; it is not safe for fold to do much of anything else with the
11265 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
11266 at run time. For example, the latter example above *cannot* be implemented
11267 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
11268 evaluation time of the original SAVE_EXPR is not necessarily the same at
11269 the time the new expression is evaluated. The only optimization of this
11270 sort that would be valid is changing
11272 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
11274 divided by 8 to
11276 SAVE_EXPR (I) * SAVE_EXPR (J)
11278 (where the same SAVE_EXPR (J) is used in the original and the
11279 transformed version). */
11281 static int
11282 multiple_of_p (tree type, tree top, tree bottom)
11284 if (operand_equal_p (top, bottom, 0))
11285 return 1;
11287 if (TREE_CODE (type) != INTEGER_TYPE)
11288 return 0;
11290 switch (TREE_CODE (top))
11292 case BIT_AND_EXPR:
11293 /* Bitwise and provides a power of two multiple. If the mask is
11294 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
11295 if (!integer_pow2p (bottom))
11296 return 0;
11297 /* FALLTHRU */
11299 case MULT_EXPR:
11300 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11301 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11303 case PLUS_EXPR:
11304 case MINUS_EXPR:
11305 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11306 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11308 case LSHIFT_EXPR:
11309 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
11311 tree op1, t1;
11313 op1 = TREE_OPERAND (top, 1);
11314 /* const_binop may not detect overflow correctly,
11315 so check for it explicitly here. */
11316 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
11317 > TREE_INT_CST_LOW (op1)
11318 && TREE_INT_CST_HIGH (op1) == 0
11319 && 0 != (t1 = fold_convert (type,
11320 const_binop (LSHIFT_EXPR,
11321 size_one_node,
11322 op1, 0)))
11323 && ! TREE_OVERFLOW (t1))
11324 return multiple_of_p (type, t1, bottom);
11326 return 0;
11328 case NOP_EXPR:
11329 /* Can't handle conversions from non-integral or wider integral type. */
11330 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
11331 || (TYPE_PRECISION (type)
11332 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
11333 return 0;
11335 /* .. fall through ... */
11337 case SAVE_EXPR:
11338 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
11340 case INTEGER_CST:
11341 if (TREE_CODE (bottom) != INTEGER_CST
11342 || (TYPE_UNSIGNED (type)
11343 && (tree_int_cst_sgn (top) < 0
11344 || tree_int_cst_sgn (bottom) < 0)))
11345 return 0;
11346 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
11347 top, bottom, 0));
11349 default:
11350 return 0;
11354 /* Return true if `t' is known to be non-negative. */
11357 tree_expr_nonnegative_p (tree t)
11359 if (TYPE_UNSIGNED (TREE_TYPE (t)))
11360 return 1;
11362 switch (TREE_CODE (t))
11364 case SSA_NAME:
11365 /* Query VRP to see if it has recorded any information about
11366 the range of this object. */
11367 return ssa_name_nonnegative_p (t);
11369 case ABS_EXPR:
11370 /* We can't return 1 if flag_wrapv is set because
11371 ABS_EXPR<INT_MIN> = INT_MIN. */
11372 if (!(flag_wrapv && INTEGRAL_TYPE_P (TREE_TYPE (t))))
11373 return 1;
11374 break;
11376 case INTEGER_CST:
11377 return tree_int_cst_sgn (t) >= 0;
11379 case REAL_CST:
11380 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
11382 case PLUS_EXPR:
11383 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11384 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11385 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11387 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
11388 both unsigned and at least 2 bits shorter than the result. */
11389 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11390 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11391 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11393 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11394 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11395 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11396 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11398 unsigned int prec = MAX (TYPE_PRECISION (inner1),
11399 TYPE_PRECISION (inner2)) + 1;
11400 return prec < TYPE_PRECISION (TREE_TYPE (t));
11403 break;
11405 case MULT_EXPR:
11406 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11408 /* x * x for floating point x is always non-negative. */
11409 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
11410 return 1;
11411 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11412 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11415 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
11416 both unsigned and their total bits is shorter than the result. */
11417 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11418 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11419 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11421 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11422 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11423 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11424 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11425 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
11426 < TYPE_PRECISION (TREE_TYPE (t));
11428 return 0;
11430 case BIT_AND_EXPR:
11431 case MAX_EXPR:
11432 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11433 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11435 case BIT_IOR_EXPR:
11436 case BIT_XOR_EXPR:
11437 case MIN_EXPR:
11438 case RDIV_EXPR:
11439 case TRUNC_DIV_EXPR:
11440 case CEIL_DIV_EXPR:
11441 case FLOOR_DIV_EXPR:
11442 case ROUND_DIV_EXPR:
11443 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11444 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11446 case TRUNC_MOD_EXPR:
11447 case CEIL_MOD_EXPR:
11448 case FLOOR_MOD_EXPR:
11449 case ROUND_MOD_EXPR:
11450 case SAVE_EXPR:
11451 case NON_LVALUE_EXPR:
11452 case FLOAT_EXPR:
11453 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11455 case COMPOUND_EXPR:
11456 case MODIFY_EXPR:
11457 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11459 case BIND_EXPR:
11460 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
11462 case COND_EXPR:
11463 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
11464 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
11466 case NOP_EXPR:
11468 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11469 tree outer_type = TREE_TYPE (t);
11471 if (TREE_CODE (outer_type) == REAL_TYPE)
11473 if (TREE_CODE (inner_type) == REAL_TYPE)
11474 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11475 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11477 if (TYPE_UNSIGNED (inner_type))
11478 return 1;
11479 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11482 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
11484 if (TREE_CODE (inner_type) == REAL_TYPE)
11485 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
11486 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11487 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
11488 && TYPE_UNSIGNED (inner_type);
11491 break;
11493 case TARGET_EXPR:
11495 tree temp = TARGET_EXPR_SLOT (t);
11496 t = TARGET_EXPR_INITIAL (t);
11498 /* If the initializer is non-void, then it's a normal expression
11499 that will be assigned to the slot. */
11500 if (!VOID_TYPE_P (t))
11501 return tree_expr_nonnegative_p (t);
11503 /* Otherwise, the initializer sets the slot in some way. One common
11504 way is an assignment statement at the end of the initializer. */
11505 while (1)
11507 if (TREE_CODE (t) == BIND_EXPR)
11508 t = expr_last (BIND_EXPR_BODY (t));
11509 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
11510 || TREE_CODE (t) == TRY_CATCH_EXPR)
11511 t = expr_last (TREE_OPERAND (t, 0));
11512 else if (TREE_CODE (t) == STATEMENT_LIST)
11513 t = expr_last (t);
11514 else
11515 break;
11517 if (TREE_CODE (t) == MODIFY_EXPR
11518 && TREE_OPERAND (t, 0) == temp)
11519 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11521 return 0;
11524 case CALL_EXPR:
11526 tree fndecl = get_callee_fndecl (t);
11527 tree arglist = TREE_OPERAND (t, 1);
11528 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
11529 switch (DECL_FUNCTION_CODE (fndecl))
11531 CASE_FLT_FN (BUILT_IN_ACOS):
11532 CASE_FLT_FN (BUILT_IN_ACOSH):
11533 CASE_FLT_FN (BUILT_IN_CABS):
11534 CASE_FLT_FN (BUILT_IN_COSH):
11535 CASE_FLT_FN (BUILT_IN_ERFC):
11536 CASE_FLT_FN (BUILT_IN_EXP):
11537 CASE_FLT_FN (BUILT_IN_EXP10):
11538 CASE_FLT_FN (BUILT_IN_EXP2):
11539 CASE_FLT_FN (BUILT_IN_FABS):
11540 CASE_FLT_FN (BUILT_IN_FDIM):
11541 CASE_FLT_FN (BUILT_IN_HYPOT):
11542 CASE_FLT_FN (BUILT_IN_POW10):
11543 CASE_INT_FN (BUILT_IN_FFS):
11544 CASE_INT_FN (BUILT_IN_PARITY):
11545 CASE_INT_FN (BUILT_IN_POPCOUNT):
11546 /* Always true. */
11547 return 1;
11549 CASE_FLT_FN (BUILT_IN_SQRT):
11550 /* sqrt(-0.0) is -0.0. */
11551 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
11552 return 1;
11553 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11555 CASE_FLT_FN (BUILT_IN_ASINH):
11556 CASE_FLT_FN (BUILT_IN_ATAN):
11557 CASE_FLT_FN (BUILT_IN_ATANH):
11558 CASE_FLT_FN (BUILT_IN_CBRT):
11559 CASE_FLT_FN (BUILT_IN_CEIL):
11560 CASE_FLT_FN (BUILT_IN_ERF):
11561 CASE_FLT_FN (BUILT_IN_EXPM1):
11562 CASE_FLT_FN (BUILT_IN_FLOOR):
11563 CASE_FLT_FN (BUILT_IN_FMOD):
11564 CASE_FLT_FN (BUILT_IN_FREXP):
11565 CASE_FLT_FN (BUILT_IN_LCEIL):
11566 CASE_FLT_FN (BUILT_IN_LDEXP):
11567 CASE_FLT_FN (BUILT_IN_LFLOOR):
11568 CASE_FLT_FN (BUILT_IN_LLCEIL):
11569 CASE_FLT_FN (BUILT_IN_LLFLOOR):
11570 CASE_FLT_FN (BUILT_IN_LLRINT):
11571 CASE_FLT_FN (BUILT_IN_LLROUND):
11572 CASE_FLT_FN (BUILT_IN_LRINT):
11573 CASE_FLT_FN (BUILT_IN_LROUND):
11574 CASE_FLT_FN (BUILT_IN_MODF):
11575 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11576 CASE_FLT_FN (BUILT_IN_POW):
11577 CASE_FLT_FN (BUILT_IN_RINT):
11578 CASE_FLT_FN (BUILT_IN_ROUND):
11579 CASE_FLT_FN (BUILT_IN_SIGNBIT):
11580 CASE_FLT_FN (BUILT_IN_SINH):
11581 CASE_FLT_FN (BUILT_IN_TANH):
11582 CASE_FLT_FN (BUILT_IN_TRUNC):
11583 /* True if the 1st argument is nonnegative. */
11584 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11586 CASE_FLT_FN (BUILT_IN_FMAX):
11587 /* True if the 1st OR 2nd arguments are nonnegative. */
11588 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11589 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11591 CASE_FLT_FN (BUILT_IN_FMIN):
11592 /* True if the 1st AND 2nd arguments are nonnegative. */
11593 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11594 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11596 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11597 /* True if the 2nd argument is nonnegative. */
11598 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11600 default:
11601 break;
11605 /* ... fall through ... */
11607 default:
11608 if (truth_value_p (TREE_CODE (t)))
11609 /* Truth values evaluate to 0 or 1, which is nonnegative. */
11610 return 1;
11613 /* We don't know sign of `t', so be conservative and return false. */
11614 return 0;
11617 /* Return true when T is an address and is known to be nonzero.
11618 For floating point we further ensure that T is not denormal.
11619 Similar logic is present in nonzero_address in rtlanal.h. */
11621 bool
11622 tree_expr_nonzero_p (tree t)
11624 tree type = TREE_TYPE (t);
11626 /* Doing something useful for floating point would need more work. */
11627 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11628 return false;
11630 switch (TREE_CODE (t))
11632 case SSA_NAME:
11633 /* Query VRP to see if it has recorded any information about
11634 the range of this object. */
11635 return ssa_name_nonzero_p (t);
11637 case ABS_EXPR:
11638 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11640 case INTEGER_CST:
11641 /* We used to test for !integer_zerop here. This does not work correctly
11642 if TREE_CONSTANT_OVERFLOW (t). */
11643 return (TREE_INT_CST_LOW (t) != 0
11644 || TREE_INT_CST_HIGH (t) != 0);
11646 case PLUS_EXPR:
11647 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11649 /* With the presence of negative values it is hard
11650 to say something. */
11651 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11652 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11653 return false;
11654 /* One of operands must be positive and the other non-negative. */
11655 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11656 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11658 break;
11660 case MULT_EXPR:
11661 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11663 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11664 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11666 break;
11668 case NOP_EXPR:
11670 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11671 tree outer_type = TREE_TYPE (t);
11673 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
11674 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
11676 break;
11678 case ADDR_EXPR:
11680 tree base = get_base_address (TREE_OPERAND (t, 0));
11682 if (!base)
11683 return false;
11685 /* Weak declarations may link to NULL. */
11686 if (VAR_OR_FUNCTION_DECL_P (base))
11687 return !DECL_WEAK (base);
11689 /* Constants are never weak. */
11690 if (CONSTANT_CLASS_P (base))
11691 return true;
11693 return false;
11696 case COND_EXPR:
11697 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11698 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
11700 case MIN_EXPR:
11701 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11702 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11704 case MAX_EXPR:
11705 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
11707 /* When both operands are nonzero, then MAX must be too. */
11708 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
11709 return true;
11711 /* MAX where operand 0 is positive is positive. */
11712 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11714 /* MAX where operand 1 is positive is positive. */
11715 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11716 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11717 return true;
11718 break;
11720 case COMPOUND_EXPR:
11721 case MODIFY_EXPR:
11722 case BIND_EXPR:
11723 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
11725 case SAVE_EXPR:
11726 case NON_LVALUE_EXPR:
11727 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11729 case BIT_IOR_EXPR:
11730 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11731 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11733 case CALL_EXPR:
11734 return alloca_call_p (t);
11736 default:
11737 break;
11739 return false;
11742 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
11743 attempt to fold the expression to a constant without modifying TYPE,
11744 OP0 or OP1.
11746 If the expression could be simplified to a constant, then return
11747 the constant. If the expression would not be simplified to a
11748 constant, then return NULL_TREE. */
11750 tree
11751 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
11753 tree tem = fold_binary (code, type, op0, op1);
11754 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11757 /* Given the components of a unary expression CODE, TYPE and OP0,
11758 attempt to fold the expression to a constant without modifying
11759 TYPE or OP0.
11761 If the expression could be simplified to a constant, then return
11762 the constant. If the expression would not be simplified to a
11763 constant, then return NULL_TREE. */
11765 tree
11766 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
11768 tree tem = fold_unary (code, type, op0);
11769 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11772 /* If EXP represents referencing an element in a constant string
11773 (either via pointer arithmetic or array indexing), return the
11774 tree representing the value accessed, otherwise return NULL. */
11776 tree
11777 fold_read_from_constant_string (tree exp)
11779 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
11781 tree exp1 = TREE_OPERAND (exp, 0);
11782 tree index;
11783 tree string;
11785 if (TREE_CODE (exp) == INDIRECT_REF)
11786 string = string_constant (exp1, &index);
11787 else
11789 tree low_bound = array_ref_low_bound (exp);
11790 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
11792 /* Optimize the special-case of a zero lower bound.
11794 We convert the low_bound to sizetype to avoid some problems
11795 with constant folding. (E.g. suppose the lower bound is 1,
11796 and its mode is QI. Without the conversion,l (ARRAY
11797 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
11798 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
11799 if (! integer_zerop (low_bound))
11800 index = size_diffop (index, fold_convert (sizetype, low_bound));
11802 string = exp1;
11805 if (string
11806 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
11807 && TREE_CODE (string) == STRING_CST
11808 && TREE_CODE (index) == INTEGER_CST
11809 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
11810 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
11811 == MODE_INT)
11812 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
11813 return fold_convert (TREE_TYPE (exp),
11814 build_int_cst (NULL_TREE,
11815 (TREE_STRING_POINTER (string)
11816 [TREE_INT_CST_LOW (index)])));
11818 return NULL;
11821 /* Return the tree for neg (ARG0) when ARG0 is known to be either
11822 an integer constant or real constant.
11824 TYPE is the type of the result. */
11826 static tree
11827 fold_negate_const (tree arg0, tree type)
11829 tree t = NULL_TREE;
11831 switch (TREE_CODE (arg0))
11833 case INTEGER_CST:
11835 unsigned HOST_WIDE_INT low;
11836 HOST_WIDE_INT high;
11837 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11838 TREE_INT_CST_HIGH (arg0),
11839 &low, &high);
11840 t = build_int_cst_wide (type, low, high);
11841 t = force_fit_type (t, 1,
11842 (overflow | TREE_OVERFLOW (arg0))
11843 && !TYPE_UNSIGNED (type),
11844 TREE_CONSTANT_OVERFLOW (arg0));
11845 break;
11848 case REAL_CST:
11849 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11850 break;
11852 default:
11853 gcc_unreachable ();
11856 return t;
11859 /* Return the tree for abs (ARG0) when ARG0 is known to be either
11860 an integer constant or real constant.
11862 TYPE is the type of the result. */
11864 tree
11865 fold_abs_const (tree arg0, tree type)
11867 tree t = NULL_TREE;
11869 switch (TREE_CODE (arg0))
11871 case INTEGER_CST:
11872 /* If the value is unsigned, then the absolute value is
11873 the same as the ordinary value. */
11874 if (TYPE_UNSIGNED (type))
11875 t = arg0;
11876 /* Similarly, if the value is non-negative. */
11877 else if (INT_CST_LT (integer_minus_one_node, arg0))
11878 t = arg0;
11879 /* If the value is negative, then the absolute value is
11880 its negation. */
11881 else
11883 unsigned HOST_WIDE_INT low;
11884 HOST_WIDE_INT high;
11885 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11886 TREE_INT_CST_HIGH (arg0),
11887 &low, &high);
11888 t = build_int_cst_wide (type, low, high);
11889 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
11890 TREE_CONSTANT_OVERFLOW (arg0));
11892 break;
11894 case REAL_CST:
11895 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
11896 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11897 else
11898 t = arg0;
11899 break;
11901 default:
11902 gcc_unreachable ();
11905 return t;
11908 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11909 constant. TYPE is the type of the result. */
11911 static tree
11912 fold_not_const (tree arg0, tree type)
11914 tree t = NULL_TREE;
11916 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11918 t = build_int_cst_wide (type,
11919 ~ TREE_INT_CST_LOW (arg0),
11920 ~ TREE_INT_CST_HIGH (arg0));
11921 t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11922 TREE_CONSTANT_OVERFLOW (arg0));
11924 return t;
11927 /* Given CODE, a relational operator, the target type, TYPE and two
11928 constant operands OP0 and OP1, return the result of the
11929 relational operation. If the result is not a compile time
11930 constant, then return NULL_TREE. */
11932 static tree
11933 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11935 int result, invert;
11937 /* From here on, the only cases we handle are when the result is
11938 known to be a constant. */
11940 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11942 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11943 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11945 /* Handle the cases where either operand is a NaN. */
11946 if (real_isnan (c0) || real_isnan (c1))
11948 switch (code)
11950 case EQ_EXPR:
11951 case ORDERED_EXPR:
11952 result = 0;
11953 break;
11955 case NE_EXPR:
11956 case UNORDERED_EXPR:
11957 case UNLT_EXPR:
11958 case UNLE_EXPR:
11959 case UNGT_EXPR:
11960 case UNGE_EXPR:
11961 case UNEQ_EXPR:
11962 result = 1;
11963 break;
11965 case LT_EXPR:
11966 case LE_EXPR:
11967 case GT_EXPR:
11968 case GE_EXPR:
11969 case LTGT_EXPR:
11970 if (flag_trapping_math)
11971 return NULL_TREE;
11972 result = 0;
11973 break;
11975 default:
11976 gcc_unreachable ();
11979 return constant_boolean_node (result, type);
11982 return constant_boolean_node (real_compare (code, c0, c1), type);
11985 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11987 To compute GT, swap the arguments and do LT.
11988 To compute GE, do LT and invert the result.
11989 To compute LE, swap the arguments, do LT and invert the result.
11990 To compute NE, do EQ and invert the result.
11992 Therefore, the code below must handle only EQ and LT. */
11994 if (code == LE_EXPR || code == GT_EXPR)
11996 tree tem = op0;
11997 op0 = op1;
11998 op1 = tem;
11999 code = swap_tree_comparison (code);
12002 /* Note that it is safe to invert for real values here because we
12003 have already handled the one case that it matters. */
12005 invert = 0;
12006 if (code == NE_EXPR || code == GE_EXPR)
12008 invert = 1;
12009 code = invert_tree_comparison (code, false);
12012 /* Compute a result for LT or EQ if args permit;
12013 Otherwise return T. */
12014 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
12016 if (code == EQ_EXPR)
12017 result = tree_int_cst_equal (op0, op1);
12018 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
12019 result = INT_CST_LT_UNSIGNED (op0, op1);
12020 else
12021 result = INT_CST_LT (op0, op1);
12023 else
12024 return NULL_TREE;
12026 if (invert)
12027 result ^= 1;
12028 return constant_boolean_node (result, type);
12031 /* Build an expression for the a clean point containing EXPR with type TYPE.
12032 Don't build a cleanup point expression for EXPR which don't have side
12033 effects. */
12035 tree
12036 fold_build_cleanup_point_expr (tree type, tree expr)
12038 /* If the expression does not have side effects then we don't have to wrap
12039 it with a cleanup point expression. */
12040 if (!TREE_SIDE_EFFECTS (expr))
12041 return expr;
12043 /* If the expression is a return, check to see if the expression inside the
12044 return has no side effects or the right hand side of the modify expression
12045 inside the return. If either don't have side effects set we don't need to
12046 wrap the expression in a cleanup point expression. Note we don't check the
12047 left hand side of the modify because it should always be a return decl. */
12048 if (TREE_CODE (expr) == RETURN_EXPR)
12050 tree op = TREE_OPERAND (expr, 0);
12051 if (!op || !TREE_SIDE_EFFECTS (op))
12052 return expr;
12053 op = TREE_OPERAND (op, 1);
12054 if (!TREE_SIDE_EFFECTS (op))
12055 return expr;
12058 return build1 (CLEANUP_POINT_EXPR, type, expr);
12061 /* Build an expression for the address of T. Folds away INDIRECT_REF to
12062 avoid confusing the gimplify process. */
12064 tree
12065 build_fold_addr_expr_with_type (tree t, tree ptrtype)
12067 /* The size of the object is not relevant when talking about its address. */
12068 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12069 t = TREE_OPERAND (t, 0);
12071 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
12072 if (TREE_CODE (t) == INDIRECT_REF
12073 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
12075 t = TREE_OPERAND (t, 0);
12076 if (TREE_TYPE (t) != ptrtype)
12077 t = build1 (NOP_EXPR, ptrtype, t);
12079 else
12081 tree base = t;
12083 while (handled_component_p (base))
12084 base = TREE_OPERAND (base, 0);
12085 if (DECL_P (base))
12086 TREE_ADDRESSABLE (base) = 1;
12088 t = build1 (ADDR_EXPR, ptrtype, t);
12091 return t;
12094 tree
12095 build_fold_addr_expr (tree t)
12097 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
12100 /* Given a pointer value OP0 and a type TYPE, return a simplified version
12101 of an indirection through OP0, or NULL_TREE if no simplification is
12102 possible. */
12104 tree
12105 fold_indirect_ref_1 (tree type, tree op0)
12107 tree sub = op0;
12108 tree subtype;
12110 STRIP_NOPS (sub);
12111 subtype = TREE_TYPE (sub);
12112 if (!POINTER_TYPE_P (subtype))
12113 return NULL_TREE;
12115 if (TREE_CODE (sub) == ADDR_EXPR)
12117 tree op = TREE_OPERAND (sub, 0);
12118 tree optype = TREE_TYPE (op);
12119 /* *&p => p; make sure to handle *&"str"[cst] here. */
12120 if (type == optype)
12122 tree fop = fold_read_from_constant_string (op);
12123 if (fop)
12124 return fop;
12125 else
12126 return op;
12128 /* *(foo *)&fooarray => fooarray[0] */
12129 else if (TREE_CODE (optype) == ARRAY_TYPE
12130 && type == TREE_TYPE (optype))
12132 tree type_domain = TYPE_DOMAIN (optype);
12133 tree min_val = size_zero_node;
12134 if (type_domain && TYPE_MIN_VALUE (type_domain))
12135 min_val = TYPE_MIN_VALUE (type_domain);
12136 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
12138 /* *(foo *)&complexfoo => __real__ complexfoo */
12139 else if (TREE_CODE (optype) == COMPLEX_TYPE
12140 && type == TREE_TYPE (optype))
12141 return fold_build1 (REALPART_EXPR, type, op);
12144 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
12145 if (TREE_CODE (sub) == PLUS_EXPR
12146 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
12148 tree op00 = TREE_OPERAND (sub, 0);
12149 tree op01 = TREE_OPERAND (sub, 1);
12150 tree op00type;
12152 STRIP_NOPS (op00);
12153 op00type = TREE_TYPE (op00);
12154 if (TREE_CODE (op00) == ADDR_EXPR
12155 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
12156 && type == TREE_TYPE (TREE_TYPE (op00type)))
12158 tree size = TYPE_SIZE_UNIT (type);
12159 if (tree_int_cst_equal (size, op01))
12160 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
12164 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
12165 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
12166 && type == TREE_TYPE (TREE_TYPE (subtype)))
12168 tree type_domain;
12169 tree min_val = size_zero_node;
12170 sub = build_fold_indirect_ref (sub);
12171 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
12172 if (type_domain && TYPE_MIN_VALUE (type_domain))
12173 min_val = TYPE_MIN_VALUE (type_domain);
12174 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
12177 return NULL_TREE;
12180 /* Builds an expression for an indirection through T, simplifying some
12181 cases. */
12183 tree
12184 build_fold_indirect_ref (tree t)
12186 tree type = TREE_TYPE (TREE_TYPE (t));
12187 tree sub = fold_indirect_ref_1 (type, t);
12189 if (sub)
12190 return sub;
12191 else
12192 return build1 (INDIRECT_REF, type, t);
12195 /* Given an INDIRECT_REF T, return either T or a simplified version. */
12197 tree
12198 fold_indirect_ref (tree t)
12200 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
12202 if (sub)
12203 return sub;
12204 else
12205 return t;
12208 /* Strip non-trapping, non-side-effecting tree nodes from an expression
12209 whose result is ignored. The type of the returned tree need not be
12210 the same as the original expression. */
12212 tree
12213 fold_ignored_result (tree t)
12215 if (!TREE_SIDE_EFFECTS (t))
12216 return integer_zero_node;
12218 for (;;)
12219 switch (TREE_CODE_CLASS (TREE_CODE (t)))
12221 case tcc_unary:
12222 t = TREE_OPERAND (t, 0);
12223 break;
12225 case tcc_binary:
12226 case tcc_comparison:
12227 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12228 t = TREE_OPERAND (t, 0);
12229 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
12230 t = TREE_OPERAND (t, 1);
12231 else
12232 return t;
12233 break;
12235 case tcc_expression:
12236 switch (TREE_CODE (t))
12238 case COMPOUND_EXPR:
12239 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12240 return t;
12241 t = TREE_OPERAND (t, 0);
12242 break;
12244 case COND_EXPR:
12245 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
12246 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
12247 return t;
12248 t = TREE_OPERAND (t, 0);
12249 break;
12251 default:
12252 return t;
12254 break;
12256 default:
12257 return t;
12261 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
12262 This can only be applied to objects of a sizetype. */
12264 tree
12265 round_up (tree value, int divisor)
12267 tree div = NULL_TREE;
12269 gcc_assert (divisor > 0);
12270 if (divisor == 1)
12271 return value;
12273 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12274 have to do anything. Only do this when we are not given a const,
12275 because in that case, this check is more expensive than just
12276 doing it. */
12277 if (TREE_CODE (value) != INTEGER_CST)
12279 div = build_int_cst (TREE_TYPE (value), divisor);
12281 if (multiple_of_p (TREE_TYPE (value), value, div))
12282 return value;
12285 /* If divisor is a power of two, simplify this to bit manipulation. */
12286 if (divisor == (divisor & -divisor))
12288 tree t;
12290 t = build_int_cst (TREE_TYPE (value), divisor - 1);
12291 value = size_binop (PLUS_EXPR, value, t);
12292 t = build_int_cst (TREE_TYPE (value), -divisor);
12293 value = size_binop (BIT_AND_EXPR, value, t);
12295 else
12297 if (!div)
12298 div = build_int_cst (TREE_TYPE (value), divisor);
12299 value = size_binop (CEIL_DIV_EXPR, value, div);
12300 value = size_binop (MULT_EXPR, value, div);
12303 return value;
12306 /* Likewise, but round down. */
12308 tree
12309 round_down (tree value, int divisor)
12311 tree div = NULL_TREE;
12313 gcc_assert (divisor > 0);
12314 if (divisor == 1)
12315 return value;
12317 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12318 have to do anything. Only do this when we are not given a const,
12319 because in that case, this check is more expensive than just
12320 doing it. */
12321 if (TREE_CODE (value) != INTEGER_CST)
12323 div = build_int_cst (TREE_TYPE (value), divisor);
12325 if (multiple_of_p (TREE_TYPE (value), value, div))
12326 return value;
12329 /* If divisor is a power of two, simplify this to bit manipulation. */
12330 if (divisor == (divisor & -divisor))
12332 tree t;
12334 t = build_int_cst (TREE_TYPE (value), -divisor);
12335 value = size_binop (BIT_AND_EXPR, value, t);
12337 else
12339 if (!div)
12340 div = build_int_cst (TREE_TYPE (value), divisor);
12341 value = size_binop (FLOOR_DIV_EXPR, value, div);
12342 value = size_binop (MULT_EXPR, value, div);
12345 return value;
12348 /* Returns the pointer to the base of the object addressed by EXP and
12349 extracts the information about the offset of the access, storing it
12350 to PBITPOS and POFFSET. */
12352 static tree
12353 split_address_to_core_and_offset (tree exp,
12354 HOST_WIDE_INT *pbitpos, tree *poffset)
12356 tree core;
12357 enum machine_mode mode;
12358 int unsignedp, volatilep;
12359 HOST_WIDE_INT bitsize;
12361 if (TREE_CODE (exp) == ADDR_EXPR)
12363 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
12364 poffset, &mode, &unsignedp, &volatilep,
12365 false);
12366 core = build_fold_addr_expr (core);
12368 else
12370 core = exp;
12371 *pbitpos = 0;
12372 *poffset = NULL_TREE;
12375 return core;
12378 /* Returns true if addresses of E1 and E2 differ by a constant, false
12379 otherwise. If they do, E1 - E2 is stored in *DIFF. */
12381 bool
12382 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
12384 tree core1, core2;
12385 HOST_WIDE_INT bitpos1, bitpos2;
12386 tree toffset1, toffset2, tdiff, type;
12388 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
12389 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
12391 if (bitpos1 % BITS_PER_UNIT != 0
12392 || bitpos2 % BITS_PER_UNIT != 0
12393 || !operand_equal_p (core1, core2, 0))
12394 return false;
12396 if (toffset1 && toffset2)
12398 type = TREE_TYPE (toffset1);
12399 if (type != TREE_TYPE (toffset2))
12400 toffset2 = fold_convert (type, toffset2);
12402 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
12403 if (!cst_and_fits_in_hwi (tdiff))
12404 return false;
12406 *diff = int_cst_value (tdiff);
12408 else if (toffset1 || toffset2)
12410 /* If only one of the offsets is non-constant, the difference cannot
12411 be a constant. */
12412 return false;
12414 else
12415 *diff = 0;
12417 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
12418 return true;
12421 /* Simplify the floating point expression EXP when the sign of the
12422 result is not significant. Return NULL_TREE if no simplification
12423 is possible. */
12425 tree
12426 fold_strip_sign_ops (tree exp)
12428 tree arg0, arg1;
12430 switch (TREE_CODE (exp))
12432 case ABS_EXPR:
12433 case NEGATE_EXPR:
12434 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12435 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
12437 case MULT_EXPR:
12438 case RDIV_EXPR:
12439 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
12440 return NULL_TREE;
12441 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12442 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
12443 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
12444 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
12445 arg0 ? arg0 : TREE_OPERAND (exp, 0),
12446 arg1 ? arg1 : TREE_OPERAND (exp, 1));
12447 break;
12449 default:
12450 break;
12452 return NULL_TREE;