acinclude.m4: Restore the situation that we don't build modules on darwin.
[official-gcc.git] / gcc / fold-const.c
blobaa710b126c8a3e9202f71b5ed6e6bdd84c87d7ab
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 /* If this is a comparison of two exprs that look like an
7309 ARRAY_REF of the same object, then we can fold this to a
7310 comparison of the two offsets. */
7312 tree base0, offset0, base1, offset1;
7314 if (extract_array_ref (arg0, &base0, &offset0)
7315 && extract_array_ref (arg1, &base1, &offset1)
7316 && operand_equal_p (base0, base1, 0))
7318 /* Handle no offsets on both sides specially. */
7319 if (offset0 == NULL_TREE && offset1 == NULL_TREE)
7320 return fold_build2 (code, type, integer_zero_node,
7321 integer_zero_node);
7323 if (!offset0 || !offset1
7324 || TREE_TYPE (offset0) == TREE_TYPE (offset1))
7326 if (offset0 == NULL_TREE)
7327 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
7328 if (offset1 == NULL_TREE)
7329 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
7330 return fold_build2 (code, type, offset0, offset1);
7335 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
7336 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7337 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7338 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7339 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
7340 && !(flag_wrapv || flag_trapv))
7341 && (TREE_CODE (arg1) == INTEGER_CST
7342 && !TREE_OVERFLOW (arg1)))
7344 tree const1 = TREE_OPERAND (arg0, 1);
7345 tree const2 = arg1;
7346 tree variable = TREE_OPERAND (arg0, 0);
7347 tree lhs;
7348 int lhs_add;
7349 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
7351 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
7352 TREE_TYPE (arg1), const2, const1);
7353 if (TREE_CODE (lhs) == TREE_CODE (arg1)
7354 && (TREE_CODE (lhs) != INTEGER_CST
7355 || !TREE_OVERFLOW (lhs)))
7356 return fold_build2 (code, type, variable, lhs);
7359 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7361 tree targ0 = strip_float_extensions (arg0);
7362 tree targ1 = strip_float_extensions (arg1);
7363 tree newtype = TREE_TYPE (targ0);
7365 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7366 newtype = TREE_TYPE (targ1);
7368 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7369 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7370 return fold_build2 (code, type, fold_convert (newtype, targ0),
7371 fold_convert (newtype, targ1));
7373 /* (-a) CMP (-b) -> b CMP a */
7374 if (TREE_CODE (arg0) == NEGATE_EXPR
7375 && TREE_CODE (arg1) == NEGATE_EXPR)
7376 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
7377 TREE_OPERAND (arg0, 0));
7379 if (TREE_CODE (arg1) == REAL_CST)
7381 REAL_VALUE_TYPE cst;
7382 cst = TREE_REAL_CST (arg1);
7384 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7385 if (TREE_CODE (arg0) == NEGATE_EXPR)
7386 return fold_build2 (swap_tree_comparison (code), type,
7387 TREE_OPERAND (arg0, 0),
7388 build_real (TREE_TYPE (arg1),
7389 REAL_VALUE_NEGATE (cst)));
7391 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7392 /* a CMP (-0) -> a CMP 0 */
7393 if (REAL_VALUE_MINUS_ZERO (cst))
7394 return fold_build2 (code, type, arg0,
7395 build_real (TREE_TYPE (arg1), dconst0));
7397 /* x != NaN is always true, other ops are always false. */
7398 if (REAL_VALUE_ISNAN (cst)
7399 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7401 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7402 return omit_one_operand (type, tem, arg0);
7405 /* Fold comparisons against infinity. */
7406 if (REAL_VALUE_ISINF (cst))
7408 tem = fold_inf_compare (code, type, arg0, arg1);
7409 if (tem != NULL_TREE)
7410 return tem;
7414 /* If this is a comparison of a real constant with a PLUS_EXPR
7415 or a MINUS_EXPR of a real constant, we can convert it into a
7416 comparison with a revised real constant as long as no overflow
7417 occurs when unsafe_math_optimizations are enabled. */
7418 if (flag_unsafe_math_optimizations
7419 && TREE_CODE (arg1) == REAL_CST
7420 && (TREE_CODE (arg0) == PLUS_EXPR
7421 || TREE_CODE (arg0) == MINUS_EXPR)
7422 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7423 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7424 ? MINUS_EXPR : PLUS_EXPR,
7425 arg1, TREE_OPERAND (arg0, 1), 0))
7426 && ! TREE_CONSTANT_OVERFLOW (tem))
7427 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
7429 /* Likewise, we can simplify a comparison of a real constant with
7430 a MINUS_EXPR whose first operand is also a real constant, i.e.
7431 (c1 - x) < c2 becomes x > c1-c2. */
7432 if (flag_unsafe_math_optimizations
7433 && TREE_CODE (arg1) == REAL_CST
7434 && TREE_CODE (arg0) == MINUS_EXPR
7435 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7436 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7437 arg1, 0))
7438 && ! TREE_CONSTANT_OVERFLOW (tem))
7439 return fold_build2 (swap_tree_comparison (code), type,
7440 TREE_OPERAND (arg0, 1), tem);
7442 /* Fold comparisons against built-in math functions. */
7443 if (TREE_CODE (arg1) == REAL_CST
7444 && flag_unsafe_math_optimizations
7445 && ! flag_errno_math)
7447 enum built_in_function fcode = builtin_mathfn_code (arg0);
7449 if (fcode != END_BUILTINS)
7451 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7452 if (tem != NULL_TREE)
7453 return tem;
7458 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7459 if (TREE_CONSTANT (arg1)
7460 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7461 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7462 /* This optimization is invalid for ordered comparisons
7463 if CONST+INCR overflows or if foo+incr might overflow.
7464 This optimization is invalid for floating point due to rounding.
7465 For pointer types we assume overflow doesn't happen. */
7466 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7467 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7468 && (code == EQ_EXPR || code == NE_EXPR))))
7470 tree varop, newconst;
7472 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7474 newconst = fold_build2 (PLUS_EXPR, TREE_TYPE (arg0),
7475 arg1, TREE_OPERAND (arg0, 1));
7476 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7477 TREE_OPERAND (arg0, 0),
7478 TREE_OPERAND (arg0, 1));
7480 else
7482 newconst = fold_build2 (MINUS_EXPR, TREE_TYPE (arg0),
7483 arg1, TREE_OPERAND (arg0, 1));
7484 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7485 TREE_OPERAND (arg0, 0),
7486 TREE_OPERAND (arg0, 1));
7490 /* If VAROP is a reference to a bitfield, we must mask
7491 the constant by the width of the field. */
7492 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7493 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
7494 && host_integerp (DECL_SIZE (TREE_OPERAND
7495 (TREE_OPERAND (varop, 0), 1)), 1))
7497 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7498 HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
7499 tree folded_compare, shift;
7501 /* First check whether the comparison would come out
7502 always the same. If we don't do that we would
7503 change the meaning with the masking. */
7504 folded_compare = fold_build2 (code, type,
7505 TREE_OPERAND (varop, 0), arg1);
7506 if (TREE_CODE (folded_compare) == INTEGER_CST)
7507 return omit_one_operand (type, folded_compare, varop);
7509 shift = build_int_cst (NULL_TREE,
7510 TYPE_PRECISION (TREE_TYPE (varop)) - size);
7511 shift = fold_convert (TREE_TYPE (varop), shift);
7512 newconst = fold_build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7513 newconst, shift);
7514 newconst = fold_build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7515 newconst, shift);
7518 return fold_build2 (code, type, varop, newconst);
7521 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7522 && (TREE_CODE (arg0) == NOP_EXPR
7523 || TREE_CODE (arg0) == CONVERT_EXPR))
7525 /* If we are widening one operand of an integer comparison,
7526 see if the other operand is similarly being widened. Perhaps we
7527 can do the comparison in the narrower type. */
7528 tem = fold_widened_comparison (code, type, arg0, arg1);
7529 if (tem)
7530 return tem;
7532 /* Or if we are changing signedness. */
7533 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
7534 if (tem)
7535 return tem;
7538 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7539 constant, we can simplify it. */
7540 if (TREE_CODE (arg1) == INTEGER_CST
7541 && (TREE_CODE (arg0) == MIN_EXPR
7542 || TREE_CODE (arg0) == MAX_EXPR)
7543 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7545 tem = optimize_minmax_comparison (code, type, op0, op1);
7546 if (tem)
7547 return tem;
7550 /* Simplify comparison of something with itself. (For IEEE
7551 floating-point, we can only do some of these simplifications.) */
7552 if (operand_equal_p (arg0, arg1, 0))
7554 switch (code)
7556 case EQ_EXPR:
7557 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7558 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7559 return constant_boolean_node (1, type);
7560 break;
7562 case GE_EXPR:
7563 case LE_EXPR:
7564 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7565 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7566 return constant_boolean_node (1, type);
7567 return fold_build2 (EQ_EXPR, type, arg0, arg1);
7569 case NE_EXPR:
7570 /* For NE, we can only do this simplification if integer
7571 or we don't honor IEEE floating point NaNs. */
7572 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7573 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7574 break;
7575 /* ... fall through ... */
7576 case GT_EXPR:
7577 case LT_EXPR:
7578 return constant_boolean_node (0, type);
7579 default:
7580 gcc_unreachable ();
7584 /* If we are comparing an expression that just has comparisons
7585 of two integer values, arithmetic expressions of those comparisons,
7586 and constants, we can simplify it. There are only three cases
7587 to check: the two values can either be equal, the first can be
7588 greater, or the second can be greater. Fold the expression for
7589 those three values. Since each value must be 0 or 1, we have
7590 eight possibilities, each of which corresponds to the constant 0
7591 or 1 or one of the six possible comparisons.
7593 This handles common cases like (a > b) == 0 but also handles
7594 expressions like ((x > y) - (y > x)) > 0, which supposedly
7595 occur in macroized code. */
7597 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7599 tree cval1 = 0, cval2 = 0;
7600 int save_p = 0;
7602 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7603 /* Don't handle degenerate cases here; they should already
7604 have been handled anyway. */
7605 && cval1 != 0 && cval2 != 0
7606 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7607 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7608 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7609 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7610 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7611 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7612 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7614 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7615 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7617 /* We can't just pass T to eval_subst in case cval1 or cval2
7618 was the same as ARG1. */
7620 tree high_result
7621 = fold_build2 (code, type,
7622 eval_subst (arg0, cval1, maxval,
7623 cval2, minval),
7624 arg1);
7625 tree equal_result
7626 = fold_build2 (code, type,
7627 eval_subst (arg0, cval1, maxval,
7628 cval2, maxval),
7629 arg1);
7630 tree low_result
7631 = fold_build2 (code, type,
7632 eval_subst (arg0, cval1, minval,
7633 cval2, maxval),
7634 arg1);
7636 /* All three of these results should be 0 or 1. Confirm they are.
7637 Then use those values to select the proper code to use. */
7639 if (TREE_CODE (high_result) == INTEGER_CST
7640 && TREE_CODE (equal_result) == INTEGER_CST
7641 && TREE_CODE (low_result) == INTEGER_CST)
7643 /* Make a 3-bit mask with the high-order bit being the
7644 value for `>', the next for '=', and the low for '<'. */
7645 switch ((integer_onep (high_result) * 4)
7646 + (integer_onep (equal_result) * 2)
7647 + integer_onep (low_result))
7649 case 0:
7650 /* Always false. */
7651 return omit_one_operand (type, integer_zero_node, arg0);
7652 case 1:
7653 code = LT_EXPR;
7654 break;
7655 case 2:
7656 code = EQ_EXPR;
7657 break;
7658 case 3:
7659 code = LE_EXPR;
7660 break;
7661 case 4:
7662 code = GT_EXPR;
7663 break;
7664 case 5:
7665 code = NE_EXPR;
7666 break;
7667 case 6:
7668 code = GE_EXPR;
7669 break;
7670 case 7:
7671 /* Always true. */
7672 return omit_one_operand (type, integer_one_node, arg0);
7675 if (save_p)
7676 return save_expr (build2 (code, type, cval1, cval2));
7677 return fold_build2 (code, type, cval1, cval2);
7682 /* Fold a comparison of the address of COMPONENT_REFs with the same
7683 type and component to a comparison of the address of the base
7684 object. In short, &x->a OP &y->a to x OP y and
7685 &x->a OP &y.a to x OP &y */
7686 if (TREE_CODE (arg0) == ADDR_EXPR
7687 && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
7688 && TREE_CODE (arg1) == ADDR_EXPR
7689 && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
7691 tree cref0 = TREE_OPERAND (arg0, 0);
7692 tree cref1 = TREE_OPERAND (arg1, 0);
7693 if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
7695 tree op0 = TREE_OPERAND (cref0, 0);
7696 tree op1 = TREE_OPERAND (cref1, 0);
7697 return fold_build2 (code, type,
7698 build_fold_addr_expr (op0),
7699 build_fold_addr_expr (op1));
7703 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
7704 into a single range test. */
7705 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
7706 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
7707 && TREE_CODE (arg1) == INTEGER_CST
7708 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7709 && !integer_zerop (TREE_OPERAND (arg0, 1))
7710 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7711 && !TREE_OVERFLOW (arg1))
7713 tem = fold_div_compare (code, type, arg0, arg1);
7714 if (tem != NULL_TREE)
7715 return tem;
7718 return NULL_TREE;
7721 /* Fold a binary expression of code CODE and type TYPE with operands
7722 OP0 and OP1. Return the folded expression if folding is
7723 successful. Otherwise, return NULL_TREE. */
7725 tree
7726 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
7728 enum tree_code_class kind = TREE_CODE_CLASS (code);
7729 tree arg0, arg1, tem;
7730 tree t1 = NULL_TREE;
7732 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7733 && TREE_CODE_LENGTH (code) == 2
7734 && op0 != NULL_TREE
7735 && op1 != NULL_TREE);
7737 arg0 = op0;
7738 arg1 = op1;
7740 /* Strip any conversions that don't change the mode. This is
7741 safe for every expression, except for a comparison expression
7742 because its signedness is derived from its operands. So, in
7743 the latter case, only strip conversions that don't change the
7744 signedness.
7746 Note that this is done as an internal manipulation within the
7747 constant folder, in order to find the simplest representation
7748 of the arguments so that their form can be studied. In any
7749 cases, the appropriate type conversions should be put back in
7750 the tree that will get out of the constant folder. */
7752 if (kind == tcc_comparison)
7754 STRIP_SIGN_NOPS (arg0);
7755 STRIP_SIGN_NOPS (arg1);
7757 else
7759 STRIP_NOPS (arg0);
7760 STRIP_NOPS (arg1);
7763 /* Note that TREE_CONSTANT isn't enough: static var addresses are
7764 constant but we can't do arithmetic on them. */
7765 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7766 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7767 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
7768 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
7770 if (kind == tcc_binary)
7771 tem = const_binop (code, arg0, arg1, 0);
7772 else if (kind == tcc_comparison)
7773 tem = fold_relational_const (code, type, arg0, arg1);
7774 else
7775 tem = NULL_TREE;
7777 if (tem != NULL_TREE)
7779 if (TREE_TYPE (tem) != type)
7780 tem = fold_convert (type, tem);
7781 return tem;
7785 /* If this is a commutative operation, and ARG0 is a constant, move it
7786 to ARG1 to reduce the number of tests below. */
7787 if (commutative_tree_code (code)
7788 && tree_swap_operands_p (arg0, arg1, true))
7789 return fold_build2 (code, type, op1, op0);
7791 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
7793 First check for cases where an arithmetic operation is applied to a
7794 compound, conditional, or comparison operation. Push the arithmetic
7795 operation inside the compound or conditional to see if any folding
7796 can then be done. Convert comparison to conditional for this purpose.
7797 The also optimizes non-constant cases that used to be done in
7798 expand_expr.
7800 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
7801 one of the operands is a comparison and the other is a comparison, a
7802 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
7803 code below would make the expression more complex. Change it to a
7804 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
7805 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
7807 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
7808 || code == EQ_EXPR || code == NE_EXPR)
7809 && ((truth_value_p (TREE_CODE (arg0))
7810 && (truth_value_p (TREE_CODE (arg1))
7811 || (TREE_CODE (arg1) == BIT_AND_EXPR
7812 && integer_onep (TREE_OPERAND (arg1, 1)))))
7813 || (truth_value_p (TREE_CODE (arg1))
7814 && (truth_value_p (TREE_CODE (arg0))
7815 || (TREE_CODE (arg0) == BIT_AND_EXPR
7816 && integer_onep (TREE_OPERAND (arg0, 1)))))))
7818 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
7819 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
7820 : TRUTH_XOR_EXPR,
7821 boolean_type_node,
7822 fold_convert (boolean_type_node, arg0),
7823 fold_convert (boolean_type_node, arg1));
7825 if (code == EQ_EXPR)
7826 tem = invert_truthvalue (tem);
7828 return fold_convert (type, tem);
7831 if (TREE_CODE_CLASS (code) == tcc_binary
7832 || TREE_CODE_CLASS (code) == tcc_comparison)
7834 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7835 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7836 fold_build2 (code, type,
7837 TREE_OPERAND (arg0, 1), op1));
7838 if (TREE_CODE (arg1) == COMPOUND_EXPR
7839 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
7840 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7841 fold_build2 (code, type,
7842 op0, TREE_OPERAND (arg1, 1)));
7844 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
7846 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7847 arg0, arg1,
7848 /*cond_first_p=*/1);
7849 if (tem != NULL_TREE)
7850 return tem;
7853 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
7855 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7856 arg1, arg0,
7857 /*cond_first_p=*/0);
7858 if (tem != NULL_TREE)
7859 return tem;
7863 switch (code)
7865 case PLUS_EXPR:
7866 /* A + (-B) -> A - B */
7867 if (TREE_CODE (arg1) == NEGATE_EXPR)
7868 return fold_build2 (MINUS_EXPR, type,
7869 fold_convert (type, arg0),
7870 fold_convert (type, TREE_OPERAND (arg1, 0)));
7871 /* (-A) + B -> B - A */
7872 if (TREE_CODE (arg0) == NEGATE_EXPR
7873 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7874 return fold_build2 (MINUS_EXPR, type,
7875 fold_convert (type, arg1),
7876 fold_convert (type, TREE_OPERAND (arg0, 0)));
7877 /* Convert ~A + 1 to -A. */
7878 if (INTEGRAL_TYPE_P (type)
7879 && TREE_CODE (arg0) == BIT_NOT_EXPR
7880 && integer_onep (arg1))
7881 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7883 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
7884 same or one. */
7885 if ((TREE_CODE (arg0) == MULT_EXPR
7886 || TREE_CODE (arg1) == MULT_EXPR)
7887 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7889 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
7890 if (tem)
7891 return tem;
7894 if (! FLOAT_TYPE_P (type))
7896 if (integer_zerop (arg1))
7897 return non_lvalue (fold_convert (type, arg0));
7899 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7900 with a constant, and the two constants have no bits in common,
7901 we should treat this as a BIT_IOR_EXPR since this may produce more
7902 simplifications. */
7903 if (TREE_CODE (arg0) == BIT_AND_EXPR
7904 && TREE_CODE (arg1) == BIT_AND_EXPR
7905 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7906 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7907 && integer_zerop (const_binop (BIT_AND_EXPR,
7908 TREE_OPERAND (arg0, 1),
7909 TREE_OPERAND (arg1, 1), 0)))
7911 code = BIT_IOR_EXPR;
7912 goto bit_ior;
7915 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7916 (plus (plus (mult) (mult)) (foo)) so that we can
7917 take advantage of the factoring cases below. */
7918 if (((TREE_CODE (arg0) == PLUS_EXPR
7919 || TREE_CODE (arg0) == MINUS_EXPR)
7920 && TREE_CODE (arg1) == MULT_EXPR)
7921 || ((TREE_CODE (arg1) == PLUS_EXPR
7922 || TREE_CODE (arg1) == MINUS_EXPR)
7923 && TREE_CODE (arg0) == MULT_EXPR))
7925 tree parg0, parg1, parg, marg;
7926 enum tree_code pcode;
7928 if (TREE_CODE (arg1) == MULT_EXPR)
7929 parg = arg0, marg = arg1;
7930 else
7931 parg = arg1, marg = arg0;
7932 pcode = TREE_CODE (parg);
7933 parg0 = TREE_OPERAND (parg, 0);
7934 parg1 = TREE_OPERAND (parg, 1);
7935 STRIP_NOPS (parg0);
7936 STRIP_NOPS (parg1);
7938 if (TREE_CODE (parg0) == MULT_EXPR
7939 && TREE_CODE (parg1) != MULT_EXPR)
7940 return fold_build2 (pcode, type,
7941 fold_build2 (PLUS_EXPR, type,
7942 fold_convert (type, parg0),
7943 fold_convert (type, marg)),
7944 fold_convert (type, parg1));
7945 if (TREE_CODE (parg0) != MULT_EXPR
7946 && TREE_CODE (parg1) == MULT_EXPR)
7947 return fold_build2 (PLUS_EXPR, type,
7948 fold_convert (type, parg0),
7949 fold_build2 (pcode, type,
7950 fold_convert (type, marg),
7951 fold_convert (type,
7952 parg1)));
7955 /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7956 of the array. Loop optimizer sometimes produce this type of
7957 expressions. */
7958 if (TREE_CODE (arg0) == ADDR_EXPR)
7960 tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7961 if (tem)
7962 return fold_convert (type, tem);
7964 else if (TREE_CODE (arg1) == ADDR_EXPR)
7966 tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7967 if (tem)
7968 return fold_convert (type, tem);
7971 else
7973 /* See if ARG1 is zero and X + ARG1 reduces to X. */
7974 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7975 return non_lvalue (fold_convert (type, arg0));
7977 /* Likewise if the operands are reversed. */
7978 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7979 return non_lvalue (fold_convert (type, arg1));
7981 /* Convert X + -C into X - C. */
7982 if (TREE_CODE (arg1) == REAL_CST
7983 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7985 tem = fold_negate_const (arg1, type);
7986 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7987 return fold_build2 (MINUS_EXPR, type,
7988 fold_convert (type, arg0),
7989 fold_convert (type, tem));
7992 if (flag_unsafe_math_optimizations
7993 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
7994 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
7995 && (tem = distribute_real_division (code, type, arg0, arg1)))
7996 return tem;
7998 /* Convert x+x into x*2.0. */
7999 if (operand_equal_p (arg0, arg1, 0)
8000 && SCALAR_FLOAT_TYPE_P (type))
8001 return fold_build2 (MULT_EXPR, type, arg0,
8002 build_real (type, dconst2));
8004 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
8005 if (flag_unsafe_math_optimizations
8006 && TREE_CODE (arg1) == PLUS_EXPR
8007 && TREE_CODE (arg0) != MULT_EXPR)
8009 tree tree10 = TREE_OPERAND (arg1, 0);
8010 tree tree11 = TREE_OPERAND (arg1, 1);
8011 if (TREE_CODE (tree11) == MULT_EXPR
8012 && TREE_CODE (tree10) == MULT_EXPR)
8014 tree tree0;
8015 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
8016 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
8019 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
8020 if (flag_unsafe_math_optimizations
8021 && TREE_CODE (arg0) == PLUS_EXPR
8022 && TREE_CODE (arg1) != MULT_EXPR)
8024 tree tree00 = TREE_OPERAND (arg0, 0);
8025 tree tree01 = TREE_OPERAND (arg0, 1);
8026 if (TREE_CODE (tree01) == MULT_EXPR
8027 && TREE_CODE (tree00) == MULT_EXPR)
8029 tree tree0;
8030 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
8031 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
8036 bit_rotate:
8037 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
8038 is a rotate of A by C1 bits. */
8039 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
8040 is a rotate of A by B bits. */
8042 enum tree_code code0, code1;
8043 code0 = TREE_CODE (arg0);
8044 code1 = TREE_CODE (arg1);
8045 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
8046 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
8047 && operand_equal_p (TREE_OPERAND (arg0, 0),
8048 TREE_OPERAND (arg1, 0), 0)
8049 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8051 tree tree01, tree11;
8052 enum tree_code code01, code11;
8054 tree01 = TREE_OPERAND (arg0, 1);
8055 tree11 = TREE_OPERAND (arg1, 1);
8056 STRIP_NOPS (tree01);
8057 STRIP_NOPS (tree11);
8058 code01 = TREE_CODE (tree01);
8059 code11 = TREE_CODE (tree11);
8060 if (code01 == INTEGER_CST
8061 && code11 == INTEGER_CST
8062 && TREE_INT_CST_HIGH (tree01) == 0
8063 && TREE_INT_CST_HIGH (tree11) == 0
8064 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
8065 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
8066 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
8067 code0 == LSHIFT_EXPR ? tree01 : tree11);
8068 else if (code11 == MINUS_EXPR)
8070 tree tree110, tree111;
8071 tree110 = TREE_OPERAND (tree11, 0);
8072 tree111 = TREE_OPERAND (tree11, 1);
8073 STRIP_NOPS (tree110);
8074 STRIP_NOPS (tree111);
8075 if (TREE_CODE (tree110) == INTEGER_CST
8076 && 0 == compare_tree_int (tree110,
8077 TYPE_PRECISION
8078 (TREE_TYPE (TREE_OPERAND
8079 (arg0, 0))))
8080 && operand_equal_p (tree01, tree111, 0))
8081 return build2 ((code0 == LSHIFT_EXPR
8082 ? LROTATE_EXPR
8083 : RROTATE_EXPR),
8084 type, TREE_OPERAND (arg0, 0), tree01);
8086 else if (code01 == MINUS_EXPR)
8088 tree tree010, tree011;
8089 tree010 = TREE_OPERAND (tree01, 0);
8090 tree011 = TREE_OPERAND (tree01, 1);
8091 STRIP_NOPS (tree010);
8092 STRIP_NOPS (tree011);
8093 if (TREE_CODE (tree010) == INTEGER_CST
8094 && 0 == compare_tree_int (tree010,
8095 TYPE_PRECISION
8096 (TREE_TYPE (TREE_OPERAND
8097 (arg0, 0))))
8098 && operand_equal_p (tree11, tree011, 0))
8099 return build2 ((code0 != LSHIFT_EXPR
8100 ? LROTATE_EXPR
8101 : RROTATE_EXPR),
8102 type, TREE_OPERAND (arg0, 0), tree11);
8107 associate:
8108 /* In most languages, can't associate operations on floats through
8109 parentheses. Rather than remember where the parentheses were, we
8110 don't associate floats at all, unless the user has specified
8111 -funsafe-math-optimizations. */
8113 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8115 tree var0, con0, lit0, minus_lit0;
8116 tree var1, con1, lit1, minus_lit1;
8118 /* Split both trees into variables, constants, and literals. Then
8119 associate each group together, the constants with literals,
8120 then the result with variables. This increases the chances of
8121 literals being recombined later and of generating relocatable
8122 expressions for the sum of a constant and literal. */
8123 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
8124 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
8125 code == MINUS_EXPR);
8127 /* Only do something if we found more than two objects. Otherwise,
8128 nothing has changed and we risk infinite recursion. */
8129 if (2 < ((var0 != 0) + (var1 != 0)
8130 + (con0 != 0) + (con1 != 0)
8131 + (lit0 != 0) + (lit1 != 0)
8132 + (minus_lit0 != 0) + (minus_lit1 != 0)))
8134 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
8135 if (code == MINUS_EXPR)
8136 code = PLUS_EXPR;
8138 var0 = associate_trees (var0, var1, code, type);
8139 con0 = associate_trees (con0, con1, code, type);
8140 lit0 = associate_trees (lit0, lit1, code, type);
8141 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
8143 /* Preserve the MINUS_EXPR if the negative part of the literal is
8144 greater than the positive part. Otherwise, the multiplicative
8145 folding code (i.e extract_muldiv) may be fooled in case
8146 unsigned constants are subtracted, like in the following
8147 example: ((X*2 + 4) - 8U)/2. */
8148 if (minus_lit0 && lit0)
8150 if (TREE_CODE (lit0) == INTEGER_CST
8151 && TREE_CODE (minus_lit0) == INTEGER_CST
8152 && tree_int_cst_lt (lit0, minus_lit0))
8154 minus_lit0 = associate_trees (minus_lit0, lit0,
8155 MINUS_EXPR, type);
8156 lit0 = 0;
8158 else
8160 lit0 = associate_trees (lit0, minus_lit0,
8161 MINUS_EXPR, type);
8162 minus_lit0 = 0;
8165 if (minus_lit0)
8167 if (con0 == 0)
8168 return fold_convert (type,
8169 associate_trees (var0, minus_lit0,
8170 MINUS_EXPR, type));
8171 else
8173 con0 = associate_trees (con0, minus_lit0,
8174 MINUS_EXPR, type);
8175 return fold_convert (type,
8176 associate_trees (var0, con0,
8177 PLUS_EXPR, type));
8181 con0 = associate_trees (con0, lit0, code, type);
8182 return fold_convert (type, associate_trees (var0, con0,
8183 code, type));
8187 return NULL_TREE;
8189 case MINUS_EXPR:
8190 /* A - (-B) -> A + B */
8191 if (TREE_CODE (arg1) == NEGATE_EXPR)
8192 return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
8193 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
8194 if (TREE_CODE (arg0) == NEGATE_EXPR
8195 && (FLOAT_TYPE_P (type)
8196 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
8197 && negate_expr_p (arg1)
8198 && reorder_operands_p (arg0, arg1))
8199 return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
8200 TREE_OPERAND (arg0, 0));
8201 /* Convert -A - 1 to ~A. */
8202 if (INTEGRAL_TYPE_P (type)
8203 && TREE_CODE (arg0) == NEGATE_EXPR
8204 && integer_onep (arg1))
8205 return fold_build1 (BIT_NOT_EXPR, type,
8206 fold_convert (type, TREE_OPERAND (arg0, 0)));
8208 /* Convert -1 - A to ~A. */
8209 if (INTEGRAL_TYPE_P (type)
8210 && integer_all_onesp (arg0))
8211 return fold_build1 (BIT_NOT_EXPR, type, arg1);
8213 if (! FLOAT_TYPE_P (type))
8215 if (integer_zerop (arg0))
8216 return negate_expr (fold_convert (type, arg1));
8217 if (integer_zerop (arg1))
8218 return non_lvalue (fold_convert (type, arg0));
8220 /* Fold A - (A & B) into ~B & A. */
8221 if (!TREE_SIDE_EFFECTS (arg0)
8222 && TREE_CODE (arg1) == BIT_AND_EXPR)
8224 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
8225 return fold_build2 (BIT_AND_EXPR, type,
8226 fold_build1 (BIT_NOT_EXPR, type,
8227 TREE_OPERAND (arg1, 0)),
8228 arg0);
8229 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8230 return fold_build2 (BIT_AND_EXPR, type,
8231 fold_build1 (BIT_NOT_EXPR, type,
8232 TREE_OPERAND (arg1, 1)),
8233 arg0);
8236 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
8237 any power of 2 minus 1. */
8238 if (TREE_CODE (arg0) == BIT_AND_EXPR
8239 && TREE_CODE (arg1) == BIT_AND_EXPR
8240 && operand_equal_p (TREE_OPERAND (arg0, 0),
8241 TREE_OPERAND (arg1, 0), 0))
8243 tree mask0 = TREE_OPERAND (arg0, 1);
8244 tree mask1 = TREE_OPERAND (arg1, 1);
8245 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
8247 if (operand_equal_p (tem, mask1, 0))
8249 tem = fold_build2 (BIT_XOR_EXPR, type,
8250 TREE_OPERAND (arg0, 0), mask1);
8251 return fold_build2 (MINUS_EXPR, type, tem, mask1);
8256 /* See if ARG1 is zero and X - ARG1 reduces to X. */
8257 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
8258 return non_lvalue (fold_convert (type, arg0));
8260 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
8261 ARG0 is zero and X + ARG0 reduces to X, since that would mean
8262 (-ARG1 + ARG0) reduces to -ARG1. */
8263 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
8264 return negate_expr (fold_convert (type, arg1));
8266 /* Fold &x - &x. This can happen from &x.foo - &x.
8267 This is unsafe for certain floats even in non-IEEE formats.
8268 In IEEE, it is unsafe because it does wrong for NaNs.
8269 Also note that operand_equal_p is always false if an operand
8270 is volatile. */
8272 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8273 && operand_equal_p (arg0, arg1, 0))
8274 return fold_convert (type, integer_zero_node);
8276 /* A - B -> A + (-B) if B is easily negatable. */
8277 if (negate_expr_p (arg1)
8278 && ((FLOAT_TYPE_P (type)
8279 /* Avoid this transformation if B is a positive REAL_CST. */
8280 && (TREE_CODE (arg1) != REAL_CST
8281 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
8282 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
8283 return fold_build2 (PLUS_EXPR, type,
8284 fold_convert (type, arg0),
8285 fold_convert (type, negate_expr (arg1)));
8287 /* Try folding difference of addresses. */
8289 HOST_WIDE_INT diff;
8291 if ((TREE_CODE (arg0) == ADDR_EXPR
8292 || TREE_CODE (arg1) == ADDR_EXPR)
8293 && ptr_difference_const (arg0, arg1, &diff))
8294 return build_int_cst_type (type, diff);
8297 /* Fold &a[i] - &a[j] to i-j. */
8298 if (TREE_CODE (arg0) == ADDR_EXPR
8299 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
8300 && TREE_CODE (arg1) == ADDR_EXPR
8301 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
8303 tree aref0 = TREE_OPERAND (arg0, 0);
8304 tree aref1 = TREE_OPERAND (arg1, 0);
8305 if (operand_equal_p (TREE_OPERAND (aref0, 0),
8306 TREE_OPERAND (aref1, 0), 0))
8308 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
8309 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
8310 tree esz = array_ref_element_size (aref0);
8311 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8312 return fold_build2 (MULT_EXPR, type, diff,
8313 fold_convert (type, esz));
8318 /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
8319 of the array. Loop optimizer sometimes produce this type of
8320 expressions. */
8321 if (TREE_CODE (arg0) == ADDR_EXPR)
8323 tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
8324 if (tem)
8325 return fold_convert (type, tem);
8328 if (flag_unsafe_math_optimizations
8329 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
8330 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
8331 && (tem = distribute_real_division (code, type, arg0, arg1)))
8332 return tem;
8334 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
8335 same or one. */
8336 if ((TREE_CODE (arg0) == MULT_EXPR
8337 || TREE_CODE (arg1) == MULT_EXPR)
8338 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
8340 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
8341 if (tem)
8342 return tem;
8345 goto associate;
8347 case MULT_EXPR:
8348 /* (-A) * (-B) -> A * B */
8349 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8350 return fold_build2 (MULT_EXPR, type,
8351 TREE_OPERAND (arg0, 0),
8352 negate_expr (arg1));
8353 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8354 return fold_build2 (MULT_EXPR, type,
8355 negate_expr (arg0),
8356 TREE_OPERAND (arg1, 0));
8358 if (! FLOAT_TYPE_P (type))
8360 if (integer_zerop (arg1))
8361 return omit_one_operand (type, arg1, arg0);
8362 if (integer_onep (arg1))
8363 return non_lvalue (fold_convert (type, arg0));
8364 /* Transform x * -1 into -x. */
8365 if (integer_all_onesp (arg1))
8366 return fold_convert (type, negate_expr (arg0));
8368 /* (a * (1 << b)) is (a << b) */
8369 if (TREE_CODE (arg1) == LSHIFT_EXPR
8370 && integer_onep (TREE_OPERAND (arg1, 0)))
8371 return fold_build2 (LSHIFT_EXPR, type, arg0,
8372 TREE_OPERAND (arg1, 1));
8373 if (TREE_CODE (arg0) == LSHIFT_EXPR
8374 && integer_onep (TREE_OPERAND (arg0, 0)))
8375 return fold_build2 (LSHIFT_EXPR, type, arg1,
8376 TREE_OPERAND (arg0, 1));
8378 if (TREE_CODE (arg1) == INTEGER_CST
8379 && 0 != (tem = extract_muldiv (op0,
8380 fold_convert (type, arg1),
8381 code, NULL_TREE)))
8382 return fold_convert (type, tem);
8385 else
8387 /* Maybe fold x * 0 to 0. The expressions aren't the same
8388 when x is NaN, since x * 0 is also NaN. Nor are they the
8389 same in modes with signed zeros, since multiplying a
8390 negative value by 0 gives -0, not +0. */
8391 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8392 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
8393 && real_zerop (arg1))
8394 return omit_one_operand (type, arg1, arg0);
8395 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
8396 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8397 && real_onep (arg1))
8398 return non_lvalue (fold_convert (type, arg0));
8400 /* Transform x * -1.0 into -x. */
8401 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8402 && real_minus_onep (arg1))
8403 return fold_convert (type, negate_expr (arg0));
8405 /* Convert (C1/X)*C2 into (C1*C2)/X. */
8406 if (flag_unsafe_math_optimizations
8407 && TREE_CODE (arg0) == RDIV_EXPR
8408 && TREE_CODE (arg1) == REAL_CST
8409 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
8411 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
8412 arg1, 0);
8413 if (tem)
8414 return fold_build2 (RDIV_EXPR, type, tem,
8415 TREE_OPERAND (arg0, 1));
8418 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
8419 if (operand_equal_p (arg0, arg1, 0))
8421 tree tem = fold_strip_sign_ops (arg0);
8422 if (tem != NULL_TREE)
8424 tem = fold_convert (type, tem);
8425 return fold_build2 (MULT_EXPR, type, tem, tem);
8429 if (flag_unsafe_math_optimizations)
8431 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8432 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8434 /* Optimizations of root(...)*root(...). */
8435 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
8437 tree rootfn, arg, arglist;
8438 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8439 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8441 /* Optimize sqrt(x)*sqrt(x) as x. */
8442 if (BUILTIN_SQRT_P (fcode0)
8443 && operand_equal_p (arg00, arg10, 0)
8444 && ! HONOR_SNANS (TYPE_MODE (type)))
8445 return arg00;
8447 /* Optimize root(x)*root(y) as root(x*y). */
8448 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8449 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8450 arglist = build_tree_list (NULL_TREE, arg);
8451 return build_function_call_expr (rootfn, arglist);
8454 /* Optimize expN(x)*expN(y) as expN(x+y). */
8455 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
8457 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8458 tree arg = fold_build2 (PLUS_EXPR, type,
8459 TREE_VALUE (TREE_OPERAND (arg0, 1)),
8460 TREE_VALUE (TREE_OPERAND (arg1, 1)));
8461 tree arglist = build_tree_list (NULL_TREE, arg);
8462 return build_function_call_expr (expfn, arglist);
8465 /* Optimizations of pow(...)*pow(...). */
8466 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
8467 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
8468 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
8470 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8471 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8472 1)));
8473 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8474 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8475 1)));
8477 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
8478 if (operand_equal_p (arg01, arg11, 0))
8480 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8481 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8482 tree arglist = tree_cons (NULL_TREE, arg,
8483 build_tree_list (NULL_TREE,
8484 arg01));
8485 return build_function_call_expr (powfn, arglist);
8488 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
8489 if (operand_equal_p (arg00, arg10, 0))
8491 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8492 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
8493 tree arglist = tree_cons (NULL_TREE, arg00,
8494 build_tree_list (NULL_TREE,
8495 arg));
8496 return build_function_call_expr (powfn, arglist);
8500 /* Optimize tan(x)*cos(x) as sin(x). */
8501 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
8502 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
8503 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
8504 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
8505 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
8506 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
8507 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8508 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8510 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
8512 if (sinfn != NULL_TREE)
8513 return build_function_call_expr (sinfn,
8514 TREE_OPERAND (arg0, 1));
8517 /* Optimize x*pow(x,c) as pow(x,c+1). */
8518 if (fcode1 == BUILT_IN_POW
8519 || fcode1 == BUILT_IN_POWF
8520 || fcode1 == BUILT_IN_POWL)
8522 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8523 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8524 1)));
8525 if (TREE_CODE (arg11) == REAL_CST
8526 && ! TREE_CONSTANT_OVERFLOW (arg11)
8527 && operand_equal_p (arg0, arg10, 0))
8529 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8530 REAL_VALUE_TYPE c;
8531 tree arg, arglist;
8533 c = TREE_REAL_CST (arg11);
8534 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8535 arg = build_real (type, c);
8536 arglist = build_tree_list (NULL_TREE, arg);
8537 arglist = tree_cons (NULL_TREE, arg0, arglist);
8538 return build_function_call_expr (powfn, arglist);
8542 /* Optimize pow(x,c)*x as pow(x,c+1). */
8543 if (fcode0 == BUILT_IN_POW
8544 || fcode0 == BUILT_IN_POWF
8545 || fcode0 == BUILT_IN_POWL)
8547 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8548 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8549 1)));
8550 if (TREE_CODE (arg01) == REAL_CST
8551 && ! TREE_CONSTANT_OVERFLOW (arg01)
8552 && operand_equal_p (arg1, arg00, 0))
8554 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8555 REAL_VALUE_TYPE c;
8556 tree arg, arglist;
8558 c = TREE_REAL_CST (arg01);
8559 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8560 arg = build_real (type, c);
8561 arglist = build_tree_list (NULL_TREE, arg);
8562 arglist = tree_cons (NULL_TREE, arg1, arglist);
8563 return build_function_call_expr (powfn, arglist);
8567 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
8568 if (! optimize_size
8569 && operand_equal_p (arg0, arg1, 0))
8571 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
8573 if (powfn)
8575 tree arg = build_real (type, dconst2);
8576 tree arglist = build_tree_list (NULL_TREE, arg);
8577 arglist = tree_cons (NULL_TREE, arg0, arglist);
8578 return build_function_call_expr (powfn, arglist);
8583 goto associate;
8585 case BIT_IOR_EXPR:
8586 bit_ior:
8587 if (integer_all_onesp (arg1))
8588 return omit_one_operand (type, arg1, arg0);
8589 if (integer_zerop (arg1))
8590 return non_lvalue (fold_convert (type, arg0));
8591 if (operand_equal_p (arg0, arg1, 0))
8592 return non_lvalue (fold_convert (type, arg0));
8594 /* ~X | X is -1. */
8595 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8596 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8598 t1 = build_int_cst (type, -1);
8599 t1 = force_fit_type (t1, 0, false, false);
8600 return omit_one_operand (type, t1, arg1);
8603 /* X | ~X is -1. */
8604 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8605 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8607 t1 = build_int_cst (type, -1);
8608 t1 = force_fit_type (t1, 0, false, false);
8609 return omit_one_operand (type, t1, arg0);
8612 /* Canonicalize (X & C1) | C2. */
8613 if (TREE_CODE (arg0) == BIT_AND_EXPR
8614 && TREE_CODE (arg1) == INTEGER_CST
8615 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8617 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
8618 int width = TYPE_PRECISION (type);
8619 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
8620 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
8621 hi2 = TREE_INT_CST_HIGH (arg1);
8622 lo2 = TREE_INT_CST_LOW (arg1);
8624 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
8625 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
8626 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8628 if (width > HOST_BITS_PER_WIDE_INT)
8630 mhi = (unsigned HOST_WIDE_INT) -1
8631 >> (2 * HOST_BITS_PER_WIDE_INT - width);
8632 mlo = -1;
8634 else
8636 mhi = 0;
8637 mlo = (unsigned HOST_WIDE_INT) -1
8638 >> (HOST_BITS_PER_WIDE_INT - width);
8641 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
8642 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
8643 return fold_build2 (BIT_IOR_EXPR, type,
8644 TREE_OPERAND (arg0, 0), arg1);
8646 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
8647 hi1 &= mhi;
8648 lo1 &= mlo;
8649 if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
8650 return fold_build2 (BIT_IOR_EXPR, type,
8651 fold_build2 (BIT_AND_EXPR, type,
8652 TREE_OPERAND (arg0, 0),
8653 build_int_cst_wide (type,
8654 lo1 & ~lo2,
8655 hi1 & ~hi2)),
8656 arg1);
8659 /* (X & Y) | Y is (X, Y). */
8660 if (TREE_CODE (arg0) == BIT_AND_EXPR
8661 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8662 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8663 /* (X & Y) | X is (Y, X). */
8664 if (TREE_CODE (arg0) == BIT_AND_EXPR
8665 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8666 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8667 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8668 /* X | (X & Y) is (Y, X). */
8669 if (TREE_CODE (arg1) == BIT_AND_EXPR
8670 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8671 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8672 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8673 /* X | (Y & X) is (Y, X). */
8674 if (TREE_CODE (arg1) == BIT_AND_EXPR
8675 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8676 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8677 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8679 t1 = distribute_bit_expr (code, type, arg0, arg1);
8680 if (t1 != NULL_TREE)
8681 return t1;
8683 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
8685 This results in more efficient code for machines without a NAND
8686 instruction. Combine will canonicalize to the first form
8687 which will allow use of NAND instructions provided by the
8688 backend if they exist. */
8689 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8690 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8692 return fold_build1 (BIT_NOT_EXPR, type,
8693 build2 (BIT_AND_EXPR, type,
8694 TREE_OPERAND (arg0, 0),
8695 TREE_OPERAND (arg1, 0)));
8698 /* See if this can be simplified into a rotate first. If that
8699 is unsuccessful continue in the association code. */
8700 goto bit_rotate;
8702 case BIT_XOR_EXPR:
8703 if (integer_zerop (arg1))
8704 return non_lvalue (fold_convert (type, arg0));
8705 if (integer_all_onesp (arg1))
8706 return fold_build1 (BIT_NOT_EXPR, type, arg0);
8707 if (operand_equal_p (arg0, arg1, 0))
8708 return omit_one_operand (type, integer_zero_node, arg0);
8710 /* ~X ^ X is -1. */
8711 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8712 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8714 t1 = build_int_cst (type, -1);
8715 t1 = force_fit_type (t1, 0, false, false);
8716 return omit_one_operand (type, t1, arg1);
8719 /* X ^ ~X is -1. */
8720 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8721 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8723 t1 = build_int_cst (type, -1);
8724 t1 = force_fit_type (t1, 0, false, false);
8725 return omit_one_operand (type, t1, arg0);
8728 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
8729 with a constant, and the two constants have no bits in common,
8730 we should treat this as a BIT_IOR_EXPR since this may produce more
8731 simplifications. */
8732 if (TREE_CODE (arg0) == BIT_AND_EXPR
8733 && TREE_CODE (arg1) == BIT_AND_EXPR
8734 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8735 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8736 && integer_zerop (const_binop (BIT_AND_EXPR,
8737 TREE_OPERAND (arg0, 1),
8738 TREE_OPERAND (arg1, 1), 0)))
8740 code = BIT_IOR_EXPR;
8741 goto bit_ior;
8744 /* (X | Y) ^ X -> Y & ~ X*/
8745 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8746 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8748 tree t2 = TREE_OPERAND (arg0, 1);
8749 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8750 arg1);
8751 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8752 fold_convert (type, t1));
8753 return t1;
8756 /* (Y | X) ^ X -> Y & ~ X*/
8757 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8758 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8760 tree t2 = TREE_OPERAND (arg0, 0);
8761 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8762 arg1);
8763 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8764 fold_convert (type, t1));
8765 return t1;
8768 /* X ^ (X | Y) -> Y & ~ X*/
8769 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8770 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
8772 tree t2 = TREE_OPERAND (arg1, 1);
8773 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8774 arg0);
8775 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8776 fold_convert (type, t1));
8777 return t1;
8780 /* X ^ (Y | X) -> Y & ~ X*/
8781 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8782 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
8784 tree t2 = TREE_OPERAND (arg1, 0);
8785 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8786 arg0);
8787 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8788 fold_convert (type, t1));
8789 return t1;
8792 /* Convert ~X ^ ~Y to X ^ Y. */
8793 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8794 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8795 return fold_build2 (code, type,
8796 fold_convert (type, TREE_OPERAND (arg0, 0)),
8797 fold_convert (type, TREE_OPERAND (arg1, 0)));
8799 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
8800 if (TREE_CODE (arg0) == BIT_AND_EXPR
8801 && integer_onep (TREE_OPERAND (arg0, 1))
8802 && integer_onep (arg1))
8803 return fold_build2 (EQ_EXPR, type, arg0,
8804 build_int_cst (TREE_TYPE (arg0), 0));
8806 /* Fold (X & Y) ^ Y as ~X & Y. */
8807 if (TREE_CODE (arg0) == BIT_AND_EXPR
8808 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8810 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
8811 return fold_build2 (BIT_AND_EXPR, type,
8812 fold_build1 (BIT_NOT_EXPR, type, tem),
8813 fold_convert (type, arg1));
8815 /* Fold (X & Y) ^ X as ~Y & X. */
8816 if (TREE_CODE (arg0) == BIT_AND_EXPR
8817 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8818 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8820 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
8821 return fold_build2 (BIT_AND_EXPR, type,
8822 fold_build1 (BIT_NOT_EXPR, type, tem),
8823 fold_convert (type, arg1));
8825 /* Fold X ^ (X & Y) as X & ~Y. */
8826 if (TREE_CODE (arg1) == BIT_AND_EXPR
8827 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8829 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
8830 return fold_build2 (BIT_AND_EXPR, type,
8831 fold_convert (type, arg0),
8832 fold_build1 (BIT_NOT_EXPR, type, tem));
8834 /* Fold X ^ (Y & X) as ~Y & X. */
8835 if (TREE_CODE (arg1) == BIT_AND_EXPR
8836 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8837 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8839 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
8840 return fold_build2 (BIT_AND_EXPR, type,
8841 fold_build1 (BIT_NOT_EXPR, type, tem),
8842 fold_convert (type, arg0));
8845 /* See if this can be simplified into a rotate first. If that
8846 is unsuccessful continue in the association code. */
8847 goto bit_rotate;
8849 case BIT_AND_EXPR:
8850 if (integer_all_onesp (arg1))
8851 return non_lvalue (fold_convert (type, arg0));
8852 if (integer_zerop (arg1))
8853 return omit_one_operand (type, arg1, arg0);
8854 if (operand_equal_p (arg0, arg1, 0))
8855 return non_lvalue (fold_convert (type, arg0));
8857 /* ~X & X is always zero. */
8858 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8859 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8860 return omit_one_operand (type, integer_zero_node, arg1);
8862 /* X & ~X is always zero. */
8863 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8864 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8865 return omit_one_operand (type, integer_zero_node, arg0);
8867 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
8868 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8869 && TREE_CODE (arg1) == INTEGER_CST
8870 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8871 return fold_build2 (BIT_IOR_EXPR, type,
8872 fold_build2 (BIT_AND_EXPR, type,
8873 TREE_OPERAND (arg0, 0), arg1),
8874 fold_build2 (BIT_AND_EXPR, type,
8875 TREE_OPERAND (arg0, 1), arg1));
8877 /* (X | Y) & Y is (X, Y). */
8878 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8879 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8880 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8881 /* (X | Y) & X is (Y, X). */
8882 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8883 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8884 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8885 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8886 /* X & (X | Y) is (Y, X). */
8887 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8888 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8889 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8890 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8891 /* X & (Y | X) is (Y, X). */
8892 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8893 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8894 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8895 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8897 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
8898 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8899 && integer_onep (TREE_OPERAND (arg0, 1))
8900 && integer_onep (arg1))
8902 tem = TREE_OPERAND (arg0, 0);
8903 return fold_build2 (EQ_EXPR, type,
8904 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8905 build_int_cst (TREE_TYPE (tem), 1)),
8906 build_int_cst (TREE_TYPE (tem), 0));
8908 /* Fold ~X & 1 as (X & 1) == 0. */
8909 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8910 && integer_onep (arg1))
8912 tem = TREE_OPERAND (arg0, 0);
8913 return fold_build2 (EQ_EXPR, type,
8914 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8915 build_int_cst (TREE_TYPE (tem), 1)),
8916 build_int_cst (TREE_TYPE (tem), 0));
8919 /* Fold (X ^ Y) & Y as ~X & Y. */
8920 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8921 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8923 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
8924 return fold_build2 (BIT_AND_EXPR, type,
8925 fold_build1 (BIT_NOT_EXPR, type, tem),
8926 fold_convert (type, arg1));
8928 /* Fold (X ^ Y) & X as ~Y & X. */
8929 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8930 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8931 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8933 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
8934 return fold_build2 (BIT_AND_EXPR, type,
8935 fold_build1 (BIT_NOT_EXPR, type, tem),
8936 fold_convert (type, arg1));
8938 /* Fold X & (X ^ Y) as X & ~Y. */
8939 if (TREE_CODE (arg1) == BIT_XOR_EXPR
8940 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8942 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
8943 return fold_build2 (BIT_AND_EXPR, type,
8944 fold_convert (type, arg0),
8945 fold_build1 (BIT_NOT_EXPR, type, tem));
8947 /* Fold X & (Y ^ X) as ~Y & X. */
8948 if (TREE_CODE (arg1) == BIT_XOR_EXPR
8949 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8950 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8952 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
8953 return fold_build2 (BIT_AND_EXPR, type,
8954 fold_build1 (BIT_NOT_EXPR, type, tem),
8955 fold_convert (type, arg0));
8958 t1 = distribute_bit_expr (code, type, arg0, arg1);
8959 if (t1 != NULL_TREE)
8960 return t1;
8961 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
8962 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
8963 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8965 unsigned int prec
8966 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
8968 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
8969 && (~TREE_INT_CST_LOW (arg1)
8970 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
8971 return fold_convert (type, TREE_OPERAND (arg0, 0));
8974 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
8976 This results in more efficient code for machines without a NOR
8977 instruction. Combine will canonicalize to the first form
8978 which will allow use of NOR instructions provided by the
8979 backend if they exist. */
8980 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8981 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8983 return fold_build1 (BIT_NOT_EXPR, type,
8984 build2 (BIT_IOR_EXPR, type,
8985 TREE_OPERAND (arg0, 0),
8986 TREE_OPERAND (arg1, 0)));
8989 goto associate;
8991 case RDIV_EXPR:
8992 /* Don't touch a floating-point divide by zero unless the mode
8993 of the constant can represent infinity. */
8994 if (TREE_CODE (arg1) == REAL_CST
8995 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
8996 && real_zerop (arg1))
8997 return NULL_TREE;
8999 /* Optimize A / A to 1.0 if we don't care about
9000 NaNs or Infinities. Skip the transformation
9001 for non-real operands. */
9002 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
9003 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9004 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
9005 && operand_equal_p (arg0, arg1, 0))
9007 tree r = build_real (TREE_TYPE (arg0), dconst1);
9009 return omit_two_operands (type, r, arg0, arg1);
9012 /* The complex version of the above A / A optimization. */
9013 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9014 && operand_equal_p (arg0, arg1, 0))
9016 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
9017 if (! HONOR_NANS (TYPE_MODE (elem_type))
9018 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
9020 tree r = build_real (elem_type, dconst1);
9021 /* omit_two_operands will call fold_convert for us. */
9022 return omit_two_operands (type, r, arg0, arg1);
9026 /* (-A) / (-B) -> A / B */
9027 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9028 return fold_build2 (RDIV_EXPR, type,
9029 TREE_OPERAND (arg0, 0),
9030 negate_expr (arg1));
9031 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9032 return fold_build2 (RDIV_EXPR, type,
9033 negate_expr (arg0),
9034 TREE_OPERAND (arg1, 0));
9036 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
9037 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9038 && real_onep (arg1))
9039 return non_lvalue (fold_convert (type, arg0));
9041 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
9042 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9043 && real_minus_onep (arg1))
9044 return non_lvalue (fold_convert (type, negate_expr (arg0)));
9046 /* If ARG1 is a constant, we can convert this to a multiply by the
9047 reciprocal. This does not have the same rounding properties,
9048 so only do this if -funsafe-math-optimizations. We can actually
9049 always safely do it if ARG1 is a power of two, but it's hard to
9050 tell if it is or not in a portable manner. */
9051 if (TREE_CODE (arg1) == REAL_CST)
9053 if (flag_unsafe_math_optimizations
9054 && 0 != (tem = const_binop (code, build_real (type, dconst1),
9055 arg1, 0)))
9056 return fold_build2 (MULT_EXPR, type, arg0, tem);
9057 /* Find the reciprocal if optimizing and the result is exact. */
9058 if (optimize)
9060 REAL_VALUE_TYPE r;
9061 r = TREE_REAL_CST (arg1);
9062 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
9064 tem = build_real (type, r);
9065 return fold_build2 (MULT_EXPR, type,
9066 fold_convert (type, arg0), tem);
9070 /* Convert A/B/C to A/(B*C). */
9071 if (flag_unsafe_math_optimizations
9072 && TREE_CODE (arg0) == RDIV_EXPR)
9073 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
9074 fold_build2 (MULT_EXPR, type,
9075 TREE_OPERAND (arg0, 1), arg1));
9077 /* Convert A/(B/C) to (A/B)*C. */
9078 if (flag_unsafe_math_optimizations
9079 && TREE_CODE (arg1) == RDIV_EXPR)
9080 return fold_build2 (MULT_EXPR, type,
9081 fold_build2 (RDIV_EXPR, type, arg0,
9082 TREE_OPERAND (arg1, 0)),
9083 TREE_OPERAND (arg1, 1));
9085 /* Convert C1/(X*C2) into (C1/C2)/X. */
9086 if (flag_unsafe_math_optimizations
9087 && TREE_CODE (arg1) == MULT_EXPR
9088 && TREE_CODE (arg0) == REAL_CST
9089 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
9091 tree tem = const_binop (RDIV_EXPR, arg0,
9092 TREE_OPERAND (arg1, 1), 0);
9093 if (tem)
9094 return fold_build2 (RDIV_EXPR, type, tem,
9095 TREE_OPERAND (arg1, 0));
9098 if (flag_unsafe_math_optimizations)
9100 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
9101 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
9103 /* Optimize sin(x)/cos(x) as tan(x). */
9104 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
9105 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
9106 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
9107 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9108 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9110 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9112 if (tanfn != NULL_TREE)
9113 return build_function_call_expr (tanfn,
9114 TREE_OPERAND (arg0, 1));
9117 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
9118 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
9119 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
9120 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
9121 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9122 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9124 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9126 if (tanfn != NULL_TREE)
9128 tree tmp = TREE_OPERAND (arg0, 1);
9129 tmp = build_function_call_expr (tanfn, tmp);
9130 return fold_build2 (RDIV_EXPR, type,
9131 build_real (type, dconst1), tmp);
9135 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
9136 NaNs or Infinities. */
9137 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
9138 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
9139 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
9141 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9142 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9144 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9145 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9146 && operand_equal_p (arg00, arg01, 0))
9148 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9150 if (cosfn != NULL_TREE)
9151 return build_function_call_expr (cosfn,
9152 TREE_OPERAND (arg0, 1));
9156 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
9157 NaNs or Infinities. */
9158 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
9159 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
9160 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
9162 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9163 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9165 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9166 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9167 && operand_equal_p (arg00, arg01, 0))
9169 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9171 if (cosfn != NULL_TREE)
9173 tree tmp = TREE_OPERAND (arg0, 1);
9174 tmp = build_function_call_expr (cosfn, tmp);
9175 return fold_build2 (RDIV_EXPR, type,
9176 build_real (type, dconst1),
9177 tmp);
9182 /* Optimize pow(x,c)/x as pow(x,c-1). */
9183 if (fcode0 == BUILT_IN_POW
9184 || fcode0 == BUILT_IN_POWF
9185 || fcode0 == BUILT_IN_POWL)
9187 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9188 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
9189 if (TREE_CODE (arg01) == REAL_CST
9190 && ! TREE_CONSTANT_OVERFLOW (arg01)
9191 && operand_equal_p (arg1, arg00, 0))
9193 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9194 REAL_VALUE_TYPE c;
9195 tree arg, arglist;
9197 c = TREE_REAL_CST (arg01);
9198 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
9199 arg = build_real (type, c);
9200 arglist = build_tree_list (NULL_TREE, arg);
9201 arglist = tree_cons (NULL_TREE, arg1, arglist);
9202 return build_function_call_expr (powfn, arglist);
9206 /* Optimize x/expN(y) into x*expN(-y). */
9207 if (BUILTIN_EXPONENT_P (fcode1))
9209 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9210 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
9211 tree arglist = build_tree_list (NULL_TREE,
9212 fold_convert (type, arg));
9213 arg1 = build_function_call_expr (expfn, arglist);
9214 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9217 /* Optimize x/pow(y,z) into x*pow(y,-z). */
9218 if (fcode1 == BUILT_IN_POW
9219 || fcode1 == BUILT_IN_POWF
9220 || fcode1 == BUILT_IN_POWL)
9222 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9223 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9224 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
9225 tree neg11 = fold_convert (type, negate_expr (arg11));
9226 tree arglist = tree_cons(NULL_TREE, arg10,
9227 build_tree_list (NULL_TREE, neg11));
9228 arg1 = build_function_call_expr (powfn, arglist);
9229 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9232 return NULL_TREE;
9234 case TRUNC_DIV_EXPR:
9235 case ROUND_DIV_EXPR:
9236 case FLOOR_DIV_EXPR:
9237 case CEIL_DIV_EXPR:
9238 case EXACT_DIV_EXPR:
9239 if (integer_onep (arg1))
9240 return non_lvalue (fold_convert (type, arg0));
9241 if (integer_zerop (arg1))
9242 return NULL_TREE;
9243 /* X / -1 is -X. */
9244 if (!TYPE_UNSIGNED (type)
9245 && TREE_CODE (arg1) == INTEGER_CST
9246 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9247 && TREE_INT_CST_HIGH (arg1) == -1)
9248 return fold_convert (type, negate_expr (arg0));
9250 /* Convert -A / -B to A / B when the type is signed and overflow is
9251 undefined. */
9252 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9253 && TREE_CODE (arg0) == NEGATE_EXPR
9254 && negate_expr_p (arg1))
9255 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9256 negate_expr (arg1));
9257 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9258 && TREE_CODE (arg1) == NEGATE_EXPR
9259 && negate_expr_p (arg0))
9260 return fold_build2 (code, type, negate_expr (arg0),
9261 TREE_OPERAND (arg1, 0));
9263 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
9264 operation, EXACT_DIV_EXPR.
9266 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
9267 At one time others generated faster code, it's not clear if they do
9268 after the last round to changes to the DIV code in expmed.c. */
9269 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
9270 && multiple_of_p (type, arg0, arg1))
9271 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
9273 if (TREE_CODE (arg1) == INTEGER_CST
9274 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9275 return fold_convert (type, tem);
9277 return NULL_TREE;
9279 case CEIL_MOD_EXPR:
9280 case FLOOR_MOD_EXPR:
9281 case ROUND_MOD_EXPR:
9282 case TRUNC_MOD_EXPR:
9283 /* X % 1 is always zero, but be sure to preserve any side
9284 effects in X. */
9285 if (integer_onep (arg1))
9286 return omit_one_operand (type, integer_zero_node, arg0);
9288 /* X % 0, return X % 0 unchanged so that we can get the
9289 proper warnings and errors. */
9290 if (integer_zerop (arg1))
9291 return NULL_TREE;
9293 /* 0 % X is always zero, but be sure to preserve any side
9294 effects in X. Place this after checking for X == 0. */
9295 if (integer_zerop (arg0))
9296 return omit_one_operand (type, integer_zero_node, arg1);
9298 /* X % -1 is zero. */
9299 if (!TYPE_UNSIGNED (type)
9300 && TREE_CODE (arg1) == INTEGER_CST
9301 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9302 && TREE_INT_CST_HIGH (arg1) == -1)
9303 return omit_one_operand (type, integer_zero_node, arg0);
9305 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
9306 i.e. "X % C" into "X & C2", if X and C are positive. */
9307 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
9308 && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))
9309 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) >= 0)
9311 unsigned HOST_WIDE_INT high, low;
9312 tree mask;
9313 int l;
9315 l = tree_log2 (arg1);
9316 if (l >= HOST_BITS_PER_WIDE_INT)
9318 high = ((unsigned HOST_WIDE_INT) 1
9319 << (l - HOST_BITS_PER_WIDE_INT)) - 1;
9320 low = -1;
9322 else
9324 high = 0;
9325 low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
9328 mask = build_int_cst_wide (type, low, high);
9329 return fold_build2 (BIT_AND_EXPR, type,
9330 fold_convert (type, arg0), mask);
9333 /* X % -C is the same as X % C. */
9334 if (code == TRUNC_MOD_EXPR
9335 && !TYPE_UNSIGNED (type)
9336 && TREE_CODE (arg1) == INTEGER_CST
9337 && !TREE_CONSTANT_OVERFLOW (arg1)
9338 && TREE_INT_CST_HIGH (arg1) < 0
9339 && !flag_trapv
9340 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
9341 && !sign_bit_p (arg1, arg1))
9342 return fold_build2 (code, type, fold_convert (type, arg0),
9343 fold_convert (type, negate_expr (arg1)));
9345 /* X % -Y is the same as X % Y. */
9346 if (code == TRUNC_MOD_EXPR
9347 && !TYPE_UNSIGNED (type)
9348 && TREE_CODE (arg1) == NEGATE_EXPR
9349 && !flag_trapv)
9350 return fold_build2 (code, type, fold_convert (type, arg0),
9351 fold_convert (type, TREE_OPERAND (arg1, 0)));
9353 if (TREE_CODE (arg1) == INTEGER_CST
9354 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9355 return fold_convert (type, tem);
9357 return NULL_TREE;
9359 case LROTATE_EXPR:
9360 case RROTATE_EXPR:
9361 if (integer_all_onesp (arg0))
9362 return omit_one_operand (type, arg0, arg1);
9363 goto shift;
9365 case RSHIFT_EXPR:
9366 /* Optimize -1 >> x for arithmetic right shifts. */
9367 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
9368 return omit_one_operand (type, arg0, arg1);
9369 /* ... fall through ... */
9371 case LSHIFT_EXPR:
9372 shift:
9373 if (integer_zerop (arg1))
9374 return non_lvalue (fold_convert (type, arg0));
9375 if (integer_zerop (arg0))
9376 return omit_one_operand (type, arg0, arg1);
9378 /* Since negative shift count is not well-defined,
9379 don't try to compute it in the compiler. */
9380 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
9381 return NULL_TREE;
9383 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
9384 if (TREE_CODE (arg0) == code && host_integerp (arg1, false)
9385 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9386 && host_integerp (TREE_OPERAND (arg0, 1), false)
9387 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9389 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
9390 + TREE_INT_CST_LOW (arg1));
9392 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
9393 being well defined. */
9394 if (low >= TYPE_PRECISION (type))
9396 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
9397 low = low % TYPE_PRECISION (type);
9398 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
9399 return build_int_cst (type, 0);
9400 else
9401 low = TYPE_PRECISION (type) - 1;
9404 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9405 build_int_cst (type, low));
9408 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
9409 into x & ((unsigned)-1 >> c) for unsigned types. */
9410 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
9411 || (TYPE_UNSIGNED (type)
9412 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
9413 && host_integerp (arg1, false)
9414 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9415 && host_integerp (TREE_OPERAND (arg0, 1), false)
9416 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9418 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
9419 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
9420 tree lshift;
9421 tree arg00;
9423 if (low0 == low1)
9425 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9427 lshift = build_int_cst (type, -1);
9428 lshift = int_const_binop (code, lshift, arg1, 0);
9430 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
9434 /* Rewrite an LROTATE_EXPR by a constant into an
9435 RROTATE_EXPR by a new constant. */
9436 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
9438 tree tem = build_int_cst (NULL_TREE,
9439 GET_MODE_BITSIZE (TYPE_MODE (type)));
9440 tem = fold_convert (TREE_TYPE (arg1), tem);
9441 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
9442 return fold_build2 (RROTATE_EXPR, type, arg0, tem);
9445 /* If we have a rotate of a bit operation with the rotate count and
9446 the second operand of the bit operation both constant,
9447 permute the two operations. */
9448 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9449 && (TREE_CODE (arg0) == BIT_AND_EXPR
9450 || TREE_CODE (arg0) == BIT_IOR_EXPR
9451 || TREE_CODE (arg0) == BIT_XOR_EXPR)
9452 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9453 return fold_build2 (TREE_CODE (arg0), type,
9454 fold_build2 (code, type,
9455 TREE_OPERAND (arg0, 0), arg1),
9456 fold_build2 (code, type,
9457 TREE_OPERAND (arg0, 1), arg1));
9459 /* Two consecutive rotates adding up to the width of the mode can
9460 be ignored. */
9461 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9462 && TREE_CODE (arg0) == RROTATE_EXPR
9463 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9464 && TREE_INT_CST_HIGH (arg1) == 0
9465 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
9466 && ((TREE_INT_CST_LOW (arg1)
9467 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
9468 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
9469 return TREE_OPERAND (arg0, 0);
9471 return NULL_TREE;
9473 case MIN_EXPR:
9474 if (operand_equal_p (arg0, arg1, 0))
9475 return omit_one_operand (type, arg0, arg1);
9476 if (INTEGRAL_TYPE_P (type)
9477 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9478 return omit_one_operand (type, arg1, arg0);
9479 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
9480 if (tem)
9481 return tem;
9482 goto associate;
9484 case MAX_EXPR:
9485 if (operand_equal_p (arg0, arg1, 0))
9486 return omit_one_operand (type, arg0, arg1);
9487 if (INTEGRAL_TYPE_P (type)
9488 && TYPE_MAX_VALUE (type)
9489 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9490 return omit_one_operand (type, arg1, arg0);
9491 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
9492 if (tem)
9493 return tem;
9494 goto associate;
9496 case TRUTH_ANDIF_EXPR:
9497 /* Note that the operands of this must be ints
9498 and their values must be 0 or 1.
9499 ("true" is a fixed value perhaps depending on the language.) */
9500 /* If first arg is constant zero, return it. */
9501 if (integer_zerop (arg0))
9502 return fold_convert (type, arg0);
9503 case TRUTH_AND_EXPR:
9504 /* If either arg is constant true, drop it. */
9505 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9506 return non_lvalue (fold_convert (type, arg1));
9507 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
9508 /* Preserve sequence points. */
9509 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9510 return non_lvalue (fold_convert (type, arg0));
9511 /* If second arg is constant zero, result is zero, but first arg
9512 must be evaluated. */
9513 if (integer_zerop (arg1))
9514 return omit_one_operand (type, arg1, arg0);
9515 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9516 case will be handled here. */
9517 if (integer_zerop (arg0))
9518 return omit_one_operand (type, arg0, arg1);
9520 /* !X && X is always false. */
9521 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9522 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9523 return omit_one_operand (type, integer_zero_node, arg1);
9524 /* X && !X is always false. */
9525 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9526 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9527 return omit_one_operand (type, integer_zero_node, arg0);
9529 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
9530 means A >= Y && A != MAX, but in this case we know that
9531 A < X <= MAX. */
9533 if (!TREE_SIDE_EFFECTS (arg0)
9534 && !TREE_SIDE_EFFECTS (arg1))
9536 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
9537 if (tem && !operand_equal_p (tem, arg0, 0))
9538 return fold_build2 (code, type, tem, arg1);
9540 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
9541 if (tem && !operand_equal_p (tem, arg1, 0))
9542 return fold_build2 (code, type, arg0, tem);
9545 truth_andor:
9546 /* We only do these simplifications if we are optimizing. */
9547 if (!optimize)
9548 return NULL_TREE;
9550 /* Check for things like (A || B) && (A || C). We can convert this
9551 to A || (B && C). Note that either operator can be any of the four
9552 truth and/or operations and the transformation will still be
9553 valid. Also note that we only care about order for the
9554 ANDIF and ORIF operators. If B contains side effects, this
9555 might change the truth-value of A. */
9556 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9557 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9558 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9559 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9560 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9561 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9563 tree a00 = TREE_OPERAND (arg0, 0);
9564 tree a01 = TREE_OPERAND (arg0, 1);
9565 tree a10 = TREE_OPERAND (arg1, 0);
9566 tree a11 = TREE_OPERAND (arg1, 1);
9567 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9568 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9569 && (code == TRUTH_AND_EXPR
9570 || code == TRUTH_OR_EXPR));
9572 if (operand_equal_p (a00, a10, 0))
9573 return fold_build2 (TREE_CODE (arg0), type, a00,
9574 fold_build2 (code, type, a01, a11));
9575 else if (commutative && operand_equal_p (a00, a11, 0))
9576 return fold_build2 (TREE_CODE (arg0), type, a00,
9577 fold_build2 (code, type, a01, a10));
9578 else if (commutative && operand_equal_p (a01, a10, 0))
9579 return fold_build2 (TREE_CODE (arg0), type, a01,
9580 fold_build2 (code, type, a00, a11));
9582 /* This case if tricky because we must either have commutative
9583 operators or else A10 must not have side-effects. */
9585 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9586 && operand_equal_p (a01, a11, 0))
9587 return fold_build2 (TREE_CODE (arg0), type,
9588 fold_build2 (code, type, a00, a10),
9589 a01);
9592 /* See if we can build a range comparison. */
9593 if (0 != (tem = fold_range_test (code, type, op0, op1)))
9594 return tem;
9596 /* Check for the possibility of merging component references. If our
9597 lhs is another similar operation, try to merge its rhs with our
9598 rhs. Then try to merge our lhs and rhs. */
9599 if (TREE_CODE (arg0) == code
9600 && 0 != (tem = fold_truthop (code, type,
9601 TREE_OPERAND (arg0, 1), arg1)))
9602 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9604 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
9605 return tem;
9607 return NULL_TREE;
9609 case TRUTH_ORIF_EXPR:
9610 /* Note that the operands of this must be ints
9611 and their values must be 0 or true.
9612 ("true" is a fixed value perhaps depending on the language.) */
9613 /* If first arg is constant true, return it. */
9614 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9615 return fold_convert (type, arg0);
9616 case TRUTH_OR_EXPR:
9617 /* If either arg is constant zero, drop it. */
9618 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
9619 return non_lvalue (fold_convert (type, arg1));
9620 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
9621 /* Preserve sequence points. */
9622 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9623 return non_lvalue (fold_convert (type, arg0));
9624 /* If second arg is constant true, result is true, but we must
9625 evaluate first arg. */
9626 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
9627 return omit_one_operand (type, arg1, arg0);
9628 /* Likewise for first arg, but note this only occurs here for
9629 TRUTH_OR_EXPR. */
9630 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9631 return omit_one_operand (type, arg0, arg1);
9633 /* !X || X is always true. */
9634 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9635 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9636 return omit_one_operand (type, integer_one_node, arg1);
9637 /* X || !X is always true. */
9638 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9639 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9640 return omit_one_operand (type, integer_one_node, arg0);
9642 goto truth_andor;
9644 case TRUTH_XOR_EXPR:
9645 /* If the second arg is constant zero, drop it. */
9646 if (integer_zerop (arg1))
9647 return non_lvalue (fold_convert (type, arg0));
9648 /* If the second arg is constant true, this is a logical inversion. */
9649 if (integer_onep (arg1))
9651 /* Only call invert_truthvalue if operand is a truth value. */
9652 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
9653 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
9654 else
9655 tem = invert_truthvalue (arg0);
9656 return non_lvalue (fold_convert (type, tem));
9658 /* Identical arguments cancel to zero. */
9659 if (operand_equal_p (arg0, arg1, 0))
9660 return omit_one_operand (type, integer_zero_node, arg0);
9662 /* !X ^ X is always true. */
9663 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9664 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9665 return omit_one_operand (type, integer_one_node, arg1);
9667 /* X ^ !X is always true. */
9668 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9669 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9670 return omit_one_operand (type, integer_one_node, arg0);
9672 return NULL_TREE;
9674 case EQ_EXPR:
9675 case NE_EXPR:
9676 tem = fold_comparison (code, type, op0, op1);
9677 if (tem != NULL_TREE)
9678 return tem;
9680 /* bool_var != 0 becomes bool_var. */
9681 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9682 && code == NE_EXPR)
9683 return non_lvalue (fold_convert (type, arg0));
9685 /* bool_var == 1 becomes bool_var. */
9686 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9687 && code == EQ_EXPR)
9688 return non_lvalue (fold_convert (type, arg0));
9690 /* bool_var != 1 becomes !bool_var. */
9691 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9692 && code == NE_EXPR)
9693 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9695 /* bool_var == 0 becomes !bool_var. */
9696 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9697 && code == EQ_EXPR)
9698 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9700 /* ~a != C becomes a != ~C where C is a constant. Likewise for ==. */
9701 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9702 && TREE_CODE (arg1) == INTEGER_CST)
9703 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9704 fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
9705 arg1));
9707 /* If this is an equality comparison of the address of a non-weak
9708 object against zero, then we know the result. */
9709 if (TREE_CODE (arg0) == ADDR_EXPR
9710 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9711 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9712 && integer_zerop (arg1))
9713 return constant_boolean_node (code != EQ_EXPR, type);
9715 /* If this is an equality comparison of the address of two non-weak,
9716 unaliased symbols neither of which are extern (since we do not
9717 have access to attributes for externs), then we know the result. */
9718 if (TREE_CODE (arg0) == ADDR_EXPR
9719 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9720 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9721 && ! lookup_attribute ("alias",
9722 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
9723 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
9724 && TREE_CODE (arg1) == ADDR_EXPR
9725 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
9726 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
9727 && ! lookup_attribute ("alias",
9728 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
9729 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
9731 /* We know that we're looking at the address of two
9732 non-weak, unaliased, static _DECL nodes.
9734 It is both wasteful and incorrect to call operand_equal_p
9735 to compare the two ADDR_EXPR nodes. It is wasteful in that
9736 all we need to do is test pointer equality for the arguments
9737 to the two ADDR_EXPR nodes. It is incorrect to use
9738 operand_equal_p as that function is NOT equivalent to a
9739 C equality test. It can in fact return false for two
9740 objects which would test as equal using the C equality
9741 operator. */
9742 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
9743 return constant_boolean_node (equal
9744 ? code == EQ_EXPR : code != EQ_EXPR,
9745 type);
9748 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
9749 a MINUS_EXPR of a constant, we can convert it into a comparison with
9750 a revised constant as long as no overflow occurs. */
9751 if (TREE_CODE (arg1) == INTEGER_CST
9752 && (TREE_CODE (arg0) == PLUS_EXPR
9753 || TREE_CODE (arg0) == MINUS_EXPR)
9754 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9755 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9756 ? MINUS_EXPR : PLUS_EXPR,
9757 arg1, TREE_OPERAND (arg0, 1), 0))
9758 && ! TREE_CONSTANT_OVERFLOW (tem))
9759 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9761 /* Similarly for a NEGATE_EXPR. */
9762 if (TREE_CODE (arg0) == NEGATE_EXPR
9763 && TREE_CODE (arg1) == INTEGER_CST
9764 && 0 != (tem = negate_expr (arg1))
9765 && TREE_CODE (tem) == INTEGER_CST
9766 && ! TREE_CONSTANT_OVERFLOW (tem))
9767 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9769 /* If we have X - Y == 0, we can convert that to X == Y and similarly
9770 for !=. Don't do this for ordered comparisons due to overflow. */
9771 if (TREE_CODE (arg0) == MINUS_EXPR
9772 && integer_zerop (arg1))
9773 return fold_build2 (code, type,
9774 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
9776 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
9777 if (TREE_CODE (arg0) == ABS_EXPR
9778 && (integer_zerop (arg1) || real_zerop (arg1)))
9779 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
9781 /* If this is an EQ or NE comparison with zero and ARG0 is
9782 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
9783 two operations, but the latter can be done in one less insn
9784 on machines that have only two-operand insns or on which a
9785 constant cannot be the first operand. */
9786 if (TREE_CODE (arg0) == BIT_AND_EXPR
9787 && integer_zerop (arg1))
9789 tree arg00 = TREE_OPERAND (arg0, 0);
9790 tree arg01 = TREE_OPERAND (arg0, 1);
9791 if (TREE_CODE (arg00) == LSHIFT_EXPR
9792 && integer_onep (TREE_OPERAND (arg00, 0)))
9793 return
9794 fold_build2 (code, type,
9795 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9796 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
9797 arg01, TREE_OPERAND (arg00, 1)),
9798 fold_convert (TREE_TYPE (arg0),
9799 integer_one_node)),
9800 arg1);
9801 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
9802 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
9803 return
9804 fold_build2 (code, type,
9805 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9806 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9807 arg00, TREE_OPERAND (arg01, 1)),
9808 fold_convert (TREE_TYPE (arg0),
9809 integer_one_node)),
9810 arg1);
9813 /* If this is an NE or EQ comparison of zero against the result of a
9814 signed MOD operation whose second operand is a power of 2, make
9815 the MOD operation unsigned since it is simpler and equivalent. */
9816 if (integer_zerop (arg1)
9817 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9818 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9819 || TREE_CODE (arg0) == CEIL_MOD_EXPR
9820 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9821 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9822 && integer_pow2p (TREE_OPERAND (arg0, 1)))
9824 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9825 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
9826 fold_convert (newtype,
9827 TREE_OPERAND (arg0, 0)),
9828 fold_convert (newtype,
9829 TREE_OPERAND (arg0, 1)));
9831 return fold_build2 (code, type, newmod,
9832 fold_convert (newtype, arg1));
9835 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
9836 C1 is a valid shift constant, and C2 is a power of two, i.e.
9837 a single bit. */
9838 if (TREE_CODE (arg0) == BIT_AND_EXPR
9839 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
9840 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
9841 == INTEGER_CST
9842 && integer_pow2p (TREE_OPERAND (arg0, 1))
9843 && integer_zerop (arg1))
9845 tree itype = TREE_TYPE (arg0);
9846 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
9847 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
9849 /* Check for a valid shift count. */
9850 if (TREE_INT_CST_HIGH (arg001) == 0
9851 && TREE_INT_CST_LOW (arg001) < prec)
9853 tree arg01 = TREE_OPERAND (arg0, 1);
9854 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9855 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
9856 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
9857 can be rewritten as (X & (C2 << C1)) != 0. */
9858 if ((log2 + TREE_INT_CST_LOW (arg01)) < prec)
9860 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
9861 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
9862 return fold_build2 (code, type, tem, arg1);
9864 /* Otherwise, for signed (arithmetic) shifts,
9865 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
9866 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
9867 else if (!TYPE_UNSIGNED (itype))
9868 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
9869 arg000, build_int_cst (itype, 0));
9870 /* Otherwise, of unsigned (logical) shifts,
9871 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
9872 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
9873 else
9874 return omit_one_operand (type,
9875 code == EQ_EXPR ? integer_one_node
9876 : integer_zero_node,
9877 arg000);
9881 /* If this is an NE comparison of zero with an AND of one, remove the
9882 comparison since the AND will give the correct value. */
9883 if (code == NE_EXPR
9884 && integer_zerop (arg1)
9885 && TREE_CODE (arg0) == BIT_AND_EXPR
9886 && integer_onep (TREE_OPERAND (arg0, 1)))
9887 return fold_convert (type, arg0);
9889 /* If we have (A & C) == C where C is a power of 2, convert this into
9890 (A & C) != 0. Similarly for NE_EXPR. */
9891 if (TREE_CODE (arg0) == BIT_AND_EXPR
9892 && integer_pow2p (TREE_OPERAND (arg0, 1))
9893 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9894 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9895 arg0, fold_convert (TREE_TYPE (arg0),
9896 integer_zero_node));
9898 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
9899 bit, then fold the expression into A < 0 or A >= 0. */
9900 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
9901 if (tem)
9902 return tem;
9904 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9905 Similarly for NE_EXPR. */
9906 if (TREE_CODE (arg0) == BIT_AND_EXPR
9907 && TREE_CODE (arg1) == INTEGER_CST
9908 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9910 tree notc = fold_build1 (BIT_NOT_EXPR,
9911 TREE_TYPE (TREE_OPERAND (arg0, 1)),
9912 TREE_OPERAND (arg0, 1));
9913 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9914 arg1, notc);
9915 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9916 if (integer_nonzerop (dandnotc))
9917 return omit_one_operand (type, rslt, arg0);
9920 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9921 Similarly for NE_EXPR. */
9922 if (TREE_CODE (arg0) == BIT_IOR_EXPR
9923 && TREE_CODE (arg1) == INTEGER_CST
9924 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9926 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
9927 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9928 TREE_OPERAND (arg0, 1), notd);
9929 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9930 if (integer_nonzerop (candnotd))
9931 return omit_one_operand (type, rslt, arg0);
9934 /* If this is a comparison of a field, we may be able to simplify it. */
9935 if (((TREE_CODE (arg0) == COMPONENT_REF
9936 && lang_hooks.can_use_bit_fields_p ())
9937 || TREE_CODE (arg0) == BIT_FIELD_REF)
9938 /* Handle the constant case even without -O
9939 to make sure the warnings are given. */
9940 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9942 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9943 if (t1)
9944 return t1;
9947 /* Optimize comparisons of strlen vs zero to a compare of the
9948 first character of the string vs zero. To wit,
9949 strlen(ptr) == 0 => *ptr == 0
9950 strlen(ptr) != 0 => *ptr != 0
9951 Other cases should reduce to one of these two (or a constant)
9952 due to the return value of strlen being unsigned. */
9953 if (TREE_CODE (arg0) == CALL_EXPR
9954 && integer_zerop (arg1))
9956 tree fndecl = get_callee_fndecl (arg0);
9957 tree arglist;
9959 if (fndecl
9960 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9961 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9962 && (arglist = TREE_OPERAND (arg0, 1))
9963 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9964 && ! TREE_CHAIN (arglist))
9966 tree iref = build_fold_indirect_ref (TREE_VALUE (arglist));
9967 return fold_build2 (code, type, iref,
9968 build_int_cst (TREE_TYPE (iref), 0));
9972 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
9973 of X. Similarly fold (X >> C) == 0 into X >= 0. */
9974 if (TREE_CODE (arg0) == RSHIFT_EXPR
9975 && integer_zerop (arg1)
9976 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9978 tree arg00 = TREE_OPERAND (arg0, 0);
9979 tree arg01 = TREE_OPERAND (arg0, 1);
9980 tree itype = TREE_TYPE (arg00);
9981 if (TREE_INT_CST_HIGH (arg01) == 0
9982 && TREE_INT_CST_LOW (arg01)
9983 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
9985 if (TYPE_UNSIGNED (itype))
9987 itype = lang_hooks.types.signed_type (itype);
9988 arg00 = fold_convert (itype, arg00);
9990 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
9991 type, arg00, build_int_cst (itype, 0));
9995 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
9996 if (integer_zerop (arg1)
9997 && TREE_CODE (arg0) == BIT_XOR_EXPR)
9998 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9999 TREE_OPERAND (arg0, 1));
10001 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
10002 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10003 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10004 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10005 build_int_cst (TREE_TYPE (arg1), 0));
10006 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
10007 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10008 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10009 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10010 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
10011 build_int_cst (TREE_TYPE (arg1), 0));
10013 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
10014 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10015 && TREE_CODE (arg1) == INTEGER_CST
10016 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10017 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10018 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
10019 TREE_OPERAND (arg0, 1), arg1));
10021 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10022 (X & C) == 0 when C is a single bit. */
10023 if (TREE_CODE (arg0) == BIT_AND_EXPR
10024 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10025 && integer_zerop (arg1)
10026 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10028 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10029 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10030 TREE_OPERAND (arg0, 1));
10031 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10032 type, tem, arg1);
10035 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10036 constant C is a power of two, i.e. a single bit. */
10037 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10038 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10039 && integer_zerop (arg1)
10040 && integer_pow2p (TREE_OPERAND (arg0, 1))
10041 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10042 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10044 tree arg00 = TREE_OPERAND (arg0, 0);
10045 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10046 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10049 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10050 when is C is a power of two, i.e. a single bit. */
10051 if (TREE_CODE (arg0) == BIT_AND_EXPR
10052 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10053 && integer_zerop (arg1)
10054 && integer_pow2p (TREE_OPERAND (arg0, 1))
10055 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10056 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10058 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10059 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
10060 arg000, TREE_OPERAND (arg0, 1));
10061 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10062 tem, build_int_cst (TREE_TYPE (tem), 0));
10065 if (integer_zerop (arg1)
10066 && tree_expr_nonzero_p (arg0))
10068 tree res = constant_boolean_node (code==NE_EXPR, type);
10069 return omit_one_operand (type, res, arg0);
10071 return NULL_TREE;
10073 case LT_EXPR:
10074 case GT_EXPR:
10075 case LE_EXPR:
10076 case GE_EXPR:
10077 tem = fold_comparison (code, type, op0, op1);
10078 if (tem != NULL_TREE)
10079 return tem;
10081 /* Transform comparisons of the form X +- C CMP X. */
10082 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10083 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10084 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10085 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
10086 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10087 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
10088 && !(flag_wrapv || flag_trapv))))
10090 tree arg01 = TREE_OPERAND (arg0, 1);
10091 enum tree_code code0 = TREE_CODE (arg0);
10092 int is_positive;
10094 if (TREE_CODE (arg01) == REAL_CST)
10095 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10096 else
10097 is_positive = tree_int_cst_sgn (arg01);
10099 /* (X - c) > X becomes false. */
10100 if (code == GT_EXPR
10101 && ((code0 == MINUS_EXPR && is_positive >= 0)
10102 || (code0 == PLUS_EXPR && is_positive <= 0)))
10103 return constant_boolean_node (0, type);
10105 /* Likewise (X + c) < X becomes false. */
10106 if (code == LT_EXPR
10107 && ((code0 == PLUS_EXPR && is_positive >= 0)
10108 || (code0 == MINUS_EXPR && is_positive <= 0)))
10109 return constant_boolean_node (0, type);
10111 /* Convert (X - c) <= X to true. */
10112 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10113 && code == LE_EXPR
10114 && ((code0 == MINUS_EXPR && is_positive >= 0)
10115 || (code0 == PLUS_EXPR && is_positive <= 0)))
10116 return constant_boolean_node (1, type);
10118 /* Convert (X + c) >= X to true. */
10119 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10120 && code == GE_EXPR
10121 && ((code0 == PLUS_EXPR && is_positive >= 0)
10122 || (code0 == MINUS_EXPR && is_positive <= 0)))
10123 return constant_boolean_node (1, type);
10125 if (TREE_CODE (arg01) == INTEGER_CST)
10127 /* Convert X + c > X and X - c < X to true for integers. */
10128 if (code == GT_EXPR
10129 && ((code0 == PLUS_EXPR && is_positive > 0)
10130 || (code0 == MINUS_EXPR && is_positive < 0)))
10131 return constant_boolean_node (1, type);
10133 if (code == LT_EXPR
10134 && ((code0 == MINUS_EXPR && is_positive > 0)
10135 || (code0 == PLUS_EXPR && is_positive < 0)))
10136 return constant_boolean_node (1, type);
10138 /* Convert X + c <= X and X - c >= X to false for integers. */
10139 if (code == LE_EXPR
10140 && ((code0 == PLUS_EXPR && is_positive > 0)
10141 || (code0 == MINUS_EXPR && is_positive < 0)))
10142 return constant_boolean_node (0, type);
10144 if (code == GE_EXPR
10145 && ((code0 == MINUS_EXPR && is_positive > 0)
10146 || (code0 == PLUS_EXPR && is_positive < 0)))
10147 return constant_boolean_node (0, type);
10151 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
10152 This transformation affects the cases which are handled in later
10153 optimizations involving comparisons with non-negative constants. */
10154 if (TREE_CODE (arg1) == INTEGER_CST
10155 && TREE_CODE (arg0) != INTEGER_CST
10156 && tree_int_cst_sgn (arg1) > 0)
10158 if (code == GE_EXPR)
10160 arg1 = const_binop (MINUS_EXPR, arg1,
10161 build_int_cst (TREE_TYPE (arg1), 1), 0);
10162 return fold_build2 (GT_EXPR, type, arg0,
10163 fold_convert (TREE_TYPE (arg0), arg1));
10165 if (code == LT_EXPR)
10167 arg1 = const_binop (MINUS_EXPR, arg1,
10168 build_int_cst (TREE_TYPE (arg1), 1), 0);
10169 return fold_build2 (LE_EXPR, type, arg0,
10170 fold_convert (TREE_TYPE (arg0), arg1));
10174 /* Comparisons with the highest or lowest possible integer of
10175 the specified size will have known values. */
10177 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
10179 if (TREE_CODE (arg1) == INTEGER_CST
10180 && ! TREE_CONSTANT_OVERFLOW (arg1)
10181 && width <= 2 * HOST_BITS_PER_WIDE_INT
10182 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10183 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10185 HOST_WIDE_INT signed_max_hi;
10186 unsigned HOST_WIDE_INT signed_max_lo;
10187 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
10189 if (width <= HOST_BITS_PER_WIDE_INT)
10191 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10192 - 1;
10193 signed_max_hi = 0;
10194 max_hi = 0;
10196 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10198 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10199 min_lo = 0;
10200 min_hi = 0;
10202 else
10204 max_lo = signed_max_lo;
10205 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10206 min_hi = -1;
10209 else
10211 width -= HOST_BITS_PER_WIDE_INT;
10212 signed_max_lo = -1;
10213 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10214 - 1;
10215 max_lo = -1;
10216 min_lo = 0;
10218 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10220 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10221 min_hi = 0;
10223 else
10225 max_hi = signed_max_hi;
10226 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10230 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
10231 && TREE_INT_CST_LOW (arg1) == max_lo)
10232 switch (code)
10234 case GT_EXPR:
10235 return omit_one_operand (type, integer_zero_node, arg0);
10237 case GE_EXPR:
10238 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10240 case LE_EXPR:
10241 return omit_one_operand (type, integer_one_node, arg0);
10243 case LT_EXPR:
10244 return fold_build2 (NE_EXPR, type, arg0, arg1);
10246 /* The GE_EXPR and LT_EXPR cases above are not normally
10247 reached because of previous transformations. */
10249 default:
10250 break;
10252 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10253 == max_hi
10254 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
10255 switch (code)
10257 case GT_EXPR:
10258 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10259 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10260 case LE_EXPR:
10261 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10262 return fold_build2 (NE_EXPR, type, arg0, arg1);
10263 default:
10264 break;
10266 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10267 == min_hi
10268 && TREE_INT_CST_LOW (arg1) == min_lo)
10269 switch (code)
10271 case LT_EXPR:
10272 return omit_one_operand (type, integer_zero_node, arg0);
10274 case LE_EXPR:
10275 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10277 case GE_EXPR:
10278 return omit_one_operand (type, integer_one_node, arg0);
10280 case GT_EXPR:
10281 return fold_build2 (NE_EXPR, type, op0, op1);
10283 default:
10284 break;
10286 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10287 == min_hi
10288 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
10289 switch (code)
10291 case GE_EXPR:
10292 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10293 return fold_build2 (NE_EXPR, type, arg0, arg1);
10294 case LT_EXPR:
10295 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10296 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10297 default:
10298 break;
10301 else if (!in_gimple_form
10302 && TREE_INT_CST_HIGH (arg1) == signed_max_hi
10303 && TREE_INT_CST_LOW (arg1) == signed_max_lo
10304 && TYPE_UNSIGNED (TREE_TYPE (arg1))
10305 /* signed_type does not work on pointer types. */
10306 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
10308 /* The following case also applies to X < signed_max+1
10309 and X >= signed_max+1 because previous transformations. */
10310 if (code == LE_EXPR || code == GT_EXPR)
10312 tree st0, st1;
10313 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
10314 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
10315 return fold_build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
10316 type, fold_convert (st0, arg0),
10317 build_int_cst (st1, 0));
10323 /* If we are comparing an ABS_EXPR with a constant, we can
10324 convert all the cases into explicit comparisons, but they may
10325 well not be faster than doing the ABS and one comparison.
10326 But ABS (X) <= C is a range comparison, which becomes a subtraction
10327 and a comparison, and is probably faster. */
10328 if (code == LE_EXPR
10329 && TREE_CODE (arg1) == INTEGER_CST
10330 && TREE_CODE (arg0) == ABS_EXPR
10331 && ! TREE_SIDE_EFFECTS (arg0)
10332 && (0 != (tem = negate_expr (arg1)))
10333 && TREE_CODE (tem) == INTEGER_CST
10334 && ! TREE_CONSTANT_OVERFLOW (tem))
10335 return fold_build2 (TRUTH_ANDIF_EXPR, type,
10336 build2 (GE_EXPR, type,
10337 TREE_OPERAND (arg0, 0), tem),
10338 build2 (LE_EXPR, type,
10339 TREE_OPERAND (arg0, 0), arg1));
10341 /* Convert ABS_EXPR<x> >= 0 to true. */
10342 if (code == GE_EXPR
10343 && tree_expr_nonnegative_p (arg0)
10344 && (integer_zerop (arg1)
10345 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10346 && real_zerop (arg1))))
10347 return omit_one_operand (type, integer_one_node, arg0);
10349 /* Convert ABS_EXPR<x> < 0 to false. */
10350 if (code == LT_EXPR
10351 && tree_expr_nonnegative_p (arg0)
10352 && (integer_zerop (arg1) || real_zerop (arg1)))
10353 return omit_one_operand (type, integer_zero_node, arg0);
10355 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
10356 and similarly for >= into !=. */
10357 if ((code == LT_EXPR || code == GE_EXPR)
10358 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10359 && TREE_CODE (arg1) == LSHIFT_EXPR
10360 && integer_onep (TREE_OPERAND (arg1, 0)))
10361 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10362 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10363 TREE_OPERAND (arg1, 1)),
10364 build_int_cst (TREE_TYPE (arg0), 0));
10366 if ((code == LT_EXPR || code == GE_EXPR)
10367 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10368 && (TREE_CODE (arg1) == NOP_EXPR
10369 || TREE_CODE (arg1) == CONVERT_EXPR)
10370 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
10371 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
10372 return
10373 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10374 fold_convert (TREE_TYPE (arg0),
10375 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10376 TREE_OPERAND (TREE_OPERAND (arg1, 0),
10377 1))),
10378 build_int_cst (TREE_TYPE (arg0), 0));
10380 return NULL_TREE;
10382 case UNORDERED_EXPR:
10383 case ORDERED_EXPR:
10384 case UNLT_EXPR:
10385 case UNLE_EXPR:
10386 case UNGT_EXPR:
10387 case UNGE_EXPR:
10388 case UNEQ_EXPR:
10389 case LTGT_EXPR:
10390 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10392 t1 = fold_relational_const (code, type, arg0, arg1);
10393 if (t1 != NULL_TREE)
10394 return t1;
10397 /* If the first operand is NaN, the result is constant. */
10398 if (TREE_CODE (arg0) == REAL_CST
10399 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
10400 && (code != LTGT_EXPR || ! flag_trapping_math))
10402 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10403 ? integer_zero_node
10404 : integer_one_node;
10405 return omit_one_operand (type, t1, arg1);
10408 /* If the second operand is NaN, the result is constant. */
10409 if (TREE_CODE (arg1) == REAL_CST
10410 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
10411 && (code != LTGT_EXPR || ! flag_trapping_math))
10413 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10414 ? integer_zero_node
10415 : integer_one_node;
10416 return omit_one_operand (type, t1, arg0);
10419 /* Simplify unordered comparison of something with itself. */
10420 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
10421 && operand_equal_p (arg0, arg1, 0))
10422 return constant_boolean_node (1, type);
10424 if (code == LTGT_EXPR
10425 && !flag_trapping_math
10426 && operand_equal_p (arg0, arg1, 0))
10427 return constant_boolean_node (0, type);
10429 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
10431 tree targ0 = strip_float_extensions (arg0);
10432 tree targ1 = strip_float_extensions (arg1);
10433 tree newtype = TREE_TYPE (targ0);
10435 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
10436 newtype = TREE_TYPE (targ1);
10438 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
10439 return fold_build2 (code, type, fold_convert (newtype, targ0),
10440 fold_convert (newtype, targ1));
10443 return NULL_TREE;
10445 case COMPOUND_EXPR:
10446 /* When pedantic, a compound expression can be neither an lvalue
10447 nor an integer constant expression. */
10448 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
10449 return NULL_TREE;
10450 /* Don't let (0, 0) be null pointer constant. */
10451 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
10452 : fold_convert (type, arg1);
10453 return pedantic_non_lvalue (tem);
10455 case COMPLEX_EXPR:
10456 if ((TREE_CODE (arg0) == REAL_CST
10457 && TREE_CODE (arg1) == REAL_CST)
10458 || (TREE_CODE (arg0) == INTEGER_CST
10459 && TREE_CODE (arg1) == INTEGER_CST))
10460 return build_complex (type, arg0, arg1);
10461 return NULL_TREE;
10463 case ASSERT_EXPR:
10464 /* An ASSERT_EXPR should never be passed to fold_binary. */
10465 gcc_unreachable ();
10467 default:
10468 return NULL_TREE;
10469 } /* switch (code) */
10472 /* Callback for walk_tree, looking for LABEL_EXPR.
10473 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
10474 Do not check the sub-tree of GOTO_EXPR. */
10476 static tree
10477 contains_label_1 (tree *tp,
10478 int *walk_subtrees,
10479 void *data ATTRIBUTE_UNUSED)
10481 switch (TREE_CODE (*tp))
10483 case LABEL_EXPR:
10484 return *tp;
10485 case GOTO_EXPR:
10486 *walk_subtrees = 0;
10487 /* no break */
10488 default:
10489 return NULL_TREE;
10493 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
10494 accessible from outside the sub-tree. Returns NULL_TREE if no
10495 addressable label is found. */
10497 static bool
10498 contains_label_p (tree st)
10500 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
10503 /* Fold a ternary expression of code CODE and type TYPE with operands
10504 OP0, OP1, and OP2. Return the folded expression if folding is
10505 successful. Otherwise, return NULL_TREE. */
10507 tree
10508 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
10510 tree tem;
10511 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
10512 enum tree_code_class kind = TREE_CODE_CLASS (code);
10514 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10515 && TREE_CODE_LENGTH (code) == 3);
10517 /* Strip any conversions that don't change the mode. This is safe
10518 for every expression, except for a comparison expression because
10519 its signedness is derived from its operands. So, in the latter
10520 case, only strip conversions that don't change the signedness.
10522 Note that this is done as an internal manipulation within the
10523 constant folder, in order to find the simplest representation of
10524 the arguments so that their form can be studied. In any cases,
10525 the appropriate type conversions should be put back in the tree
10526 that will get out of the constant folder. */
10527 if (op0)
10529 arg0 = op0;
10530 STRIP_NOPS (arg0);
10533 if (op1)
10535 arg1 = op1;
10536 STRIP_NOPS (arg1);
10539 switch (code)
10541 case COMPONENT_REF:
10542 if (TREE_CODE (arg0) == CONSTRUCTOR
10543 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
10545 unsigned HOST_WIDE_INT idx;
10546 tree field, value;
10547 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
10548 if (field == arg1)
10549 return value;
10551 return NULL_TREE;
10553 case COND_EXPR:
10554 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
10555 so all simple results must be passed through pedantic_non_lvalue. */
10556 if (TREE_CODE (arg0) == INTEGER_CST)
10558 tree unused_op = integer_zerop (arg0) ? op1 : op2;
10559 tem = integer_zerop (arg0) ? op2 : op1;
10560 /* Only optimize constant conditions when the selected branch
10561 has the same type as the COND_EXPR. This avoids optimizing
10562 away "c ? x : throw", where the throw has a void type.
10563 Avoid throwing away that operand which contains label. */
10564 if ((!TREE_SIDE_EFFECTS (unused_op)
10565 || !contains_label_p (unused_op))
10566 && (! VOID_TYPE_P (TREE_TYPE (tem))
10567 || VOID_TYPE_P (type)))
10568 return pedantic_non_lvalue (tem);
10569 return NULL_TREE;
10571 if (operand_equal_p (arg1, op2, 0))
10572 return pedantic_omit_one_operand (type, arg1, arg0);
10574 /* If we have A op B ? A : C, we may be able to convert this to a
10575 simpler expression, depending on the operation and the values
10576 of B and C. Signed zeros prevent all of these transformations,
10577 for reasons given above each one.
10579 Also try swapping the arguments and inverting the conditional. */
10580 if (COMPARISON_CLASS_P (arg0)
10581 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10582 arg1, TREE_OPERAND (arg0, 1))
10583 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
10585 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
10586 if (tem)
10587 return tem;
10590 if (COMPARISON_CLASS_P (arg0)
10591 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10592 op2,
10593 TREE_OPERAND (arg0, 1))
10594 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
10596 tem = invert_truthvalue (arg0);
10597 if (COMPARISON_CLASS_P (tem))
10599 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
10600 if (tem)
10601 return tem;
10605 /* If the second operand is simpler than the third, swap them
10606 since that produces better jump optimization results. */
10607 if (truth_value_p (TREE_CODE (arg0))
10608 && tree_swap_operands_p (op1, op2, false))
10610 /* See if this can be inverted. If it can't, possibly because
10611 it was a floating-point inequality comparison, don't do
10612 anything. */
10613 tem = invert_truthvalue (arg0);
10615 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10616 return fold_build3 (code, type, tem, op2, op1);
10619 /* Convert A ? 1 : 0 to simply A. */
10620 if (integer_onep (op1)
10621 && integer_zerop (op2)
10622 /* If we try to convert OP0 to our type, the
10623 call to fold will try to move the conversion inside
10624 a COND, which will recurse. In that case, the COND_EXPR
10625 is probably the best choice, so leave it alone. */
10626 && type == TREE_TYPE (arg0))
10627 return pedantic_non_lvalue (arg0);
10629 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
10630 over COND_EXPR in cases such as floating point comparisons. */
10631 if (integer_zerop (op1)
10632 && integer_onep (op2)
10633 && truth_value_p (TREE_CODE (arg0)))
10634 return pedantic_non_lvalue (fold_convert (type,
10635 invert_truthvalue (arg0)));
10637 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
10638 if (TREE_CODE (arg0) == LT_EXPR
10639 && integer_zerop (TREE_OPERAND (arg0, 1))
10640 && integer_zerop (op2)
10641 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
10642 return fold_convert (type, fold_build2 (BIT_AND_EXPR,
10643 TREE_TYPE (tem), tem, arg1));
10645 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
10646 already handled above. */
10647 if (TREE_CODE (arg0) == BIT_AND_EXPR
10648 && integer_onep (TREE_OPERAND (arg0, 1))
10649 && integer_zerop (op2)
10650 && integer_pow2p (arg1))
10652 tree tem = TREE_OPERAND (arg0, 0);
10653 STRIP_NOPS (tem);
10654 if (TREE_CODE (tem) == RSHIFT_EXPR
10655 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
10656 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
10657 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
10658 return fold_build2 (BIT_AND_EXPR, type,
10659 TREE_OPERAND (tem, 0), arg1);
10662 /* A & N ? N : 0 is simply A & N if N is a power of two. This
10663 is probably obsolete because the first operand should be a
10664 truth value (that's why we have the two cases above), but let's
10665 leave it in until we can confirm this for all front-ends. */
10666 if (integer_zerop (op2)
10667 && TREE_CODE (arg0) == NE_EXPR
10668 && integer_zerop (TREE_OPERAND (arg0, 1))
10669 && integer_pow2p (arg1)
10670 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10671 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10672 arg1, OEP_ONLY_CONST))
10673 return pedantic_non_lvalue (fold_convert (type,
10674 TREE_OPERAND (arg0, 0)));
10676 /* Convert A ? B : 0 into A && B if A and B are truth values. */
10677 if (integer_zerop (op2)
10678 && truth_value_p (TREE_CODE (arg0))
10679 && truth_value_p (TREE_CODE (arg1)))
10680 return fold_build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1);
10682 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
10683 if (integer_onep (op2)
10684 && truth_value_p (TREE_CODE (arg0))
10685 && truth_value_p (TREE_CODE (arg1)))
10687 /* Only perform transformation if ARG0 is easily inverted. */
10688 tem = invert_truthvalue (arg0);
10689 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10690 return fold_build2 (TRUTH_ORIF_EXPR, type, tem, arg1);
10693 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
10694 if (integer_zerop (arg1)
10695 && truth_value_p (TREE_CODE (arg0))
10696 && truth_value_p (TREE_CODE (op2)))
10698 /* Only perform transformation if ARG0 is easily inverted. */
10699 tem = invert_truthvalue (arg0);
10700 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10701 return fold_build2 (TRUTH_ANDIF_EXPR, type, tem, op2);
10704 /* Convert A ? 1 : B into A || B if A and B are truth values. */
10705 if (integer_onep (arg1)
10706 && truth_value_p (TREE_CODE (arg0))
10707 && truth_value_p (TREE_CODE (op2)))
10708 return fold_build2 (TRUTH_ORIF_EXPR, type, arg0, op2);
10710 return NULL_TREE;
10712 case CALL_EXPR:
10713 /* Check for a built-in function. */
10714 if (TREE_CODE (op0) == ADDR_EXPR
10715 && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
10716 && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
10717 return fold_builtin (TREE_OPERAND (op0, 0), op1, false);
10718 return NULL_TREE;
10720 case BIT_FIELD_REF:
10721 if (TREE_CODE (arg0) == VECTOR_CST
10722 && type == TREE_TYPE (TREE_TYPE (arg0))
10723 && host_integerp (arg1, 1)
10724 && host_integerp (op2, 1))
10726 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
10727 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
10729 if (width != 0
10730 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
10731 && (idx % width) == 0
10732 && (idx = idx / width)
10733 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
10735 tree elements = TREE_VECTOR_CST_ELTS (arg0);
10736 while (idx-- > 0 && elements)
10737 elements = TREE_CHAIN (elements);
10738 if (elements)
10739 return TREE_VALUE (elements);
10740 else
10741 return fold_convert (type, integer_zero_node);
10744 return NULL_TREE;
10746 default:
10747 return NULL_TREE;
10748 } /* switch (code) */
10751 /* Perform constant folding and related simplification of EXPR.
10752 The related simplifications include x*1 => x, x*0 => 0, etc.,
10753 and application of the associative law.
10754 NOP_EXPR conversions may be removed freely (as long as we
10755 are careful not to change the type of the overall expression).
10756 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
10757 but we can constant-fold them if they have constant operands. */
10759 #ifdef ENABLE_FOLD_CHECKING
10760 # define fold(x) fold_1 (x)
10761 static tree fold_1 (tree);
10762 static
10763 #endif
10764 tree
10765 fold (tree expr)
10767 const tree t = expr;
10768 enum tree_code code = TREE_CODE (t);
10769 enum tree_code_class kind = TREE_CODE_CLASS (code);
10770 tree tem;
10772 /* Return right away if a constant. */
10773 if (kind == tcc_constant)
10774 return t;
10776 if (IS_EXPR_CODE_CLASS (kind))
10778 tree type = TREE_TYPE (t);
10779 tree op0, op1, op2;
10781 switch (TREE_CODE_LENGTH (code))
10783 case 1:
10784 op0 = TREE_OPERAND (t, 0);
10785 tem = fold_unary (code, type, op0);
10786 return tem ? tem : expr;
10787 case 2:
10788 op0 = TREE_OPERAND (t, 0);
10789 op1 = TREE_OPERAND (t, 1);
10790 tem = fold_binary (code, type, op0, op1);
10791 return tem ? tem : expr;
10792 case 3:
10793 op0 = TREE_OPERAND (t, 0);
10794 op1 = TREE_OPERAND (t, 1);
10795 op2 = TREE_OPERAND (t, 2);
10796 tem = fold_ternary (code, type, op0, op1, op2);
10797 return tem ? tem : expr;
10798 default:
10799 break;
10803 switch (code)
10805 case CONST_DECL:
10806 return fold (DECL_INITIAL (t));
10808 default:
10809 return t;
10810 } /* switch (code) */
10813 #ifdef ENABLE_FOLD_CHECKING
10814 #undef fold
10816 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
10817 static void fold_check_failed (tree, tree);
10818 void print_fold_checksum (tree);
10820 /* When --enable-checking=fold, compute a digest of expr before
10821 and after actual fold call to see if fold did not accidentally
10822 change original expr. */
10824 tree
10825 fold (tree expr)
10827 tree ret;
10828 struct md5_ctx ctx;
10829 unsigned char checksum_before[16], checksum_after[16];
10830 htab_t ht;
10832 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10833 md5_init_ctx (&ctx);
10834 fold_checksum_tree (expr, &ctx, ht);
10835 md5_finish_ctx (&ctx, checksum_before);
10836 htab_empty (ht);
10838 ret = fold_1 (expr);
10840 md5_init_ctx (&ctx);
10841 fold_checksum_tree (expr, &ctx, ht);
10842 md5_finish_ctx (&ctx, checksum_after);
10843 htab_delete (ht);
10845 if (memcmp (checksum_before, checksum_after, 16))
10846 fold_check_failed (expr, ret);
10848 return ret;
10851 void
10852 print_fold_checksum (tree expr)
10854 struct md5_ctx ctx;
10855 unsigned char checksum[16], cnt;
10856 htab_t ht;
10858 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10859 md5_init_ctx (&ctx);
10860 fold_checksum_tree (expr, &ctx, ht);
10861 md5_finish_ctx (&ctx, checksum);
10862 htab_delete (ht);
10863 for (cnt = 0; cnt < 16; ++cnt)
10864 fprintf (stderr, "%02x", checksum[cnt]);
10865 putc ('\n', stderr);
10868 static void
10869 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
10871 internal_error ("fold check: original tree changed by fold");
10874 static void
10875 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
10877 void **slot;
10878 enum tree_code code;
10879 struct tree_function_decl buf;
10880 int i, len;
10882 recursive_label:
10884 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
10885 <= sizeof (struct tree_function_decl))
10886 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
10887 if (expr == NULL)
10888 return;
10889 slot = htab_find_slot (ht, expr, INSERT);
10890 if (*slot != NULL)
10891 return;
10892 *slot = expr;
10893 code = TREE_CODE (expr);
10894 if (TREE_CODE_CLASS (code) == tcc_declaration
10895 && DECL_ASSEMBLER_NAME_SET_P (expr))
10897 /* Allow DECL_ASSEMBLER_NAME to be modified. */
10898 memcpy ((char *) &buf, expr, tree_size (expr));
10899 expr = (tree) &buf;
10900 SET_DECL_ASSEMBLER_NAME (expr, NULL);
10902 else if (TREE_CODE_CLASS (code) == tcc_type
10903 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
10904 || TYPE_CACHED_VALUES_P (expr)
10905 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
10907 /* Allow these fields to be modified. */
10908 memcpy ((char *) &buf, expr, tree_size (expr));
10909 expr = (tree) &buf;
10910 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
10911 TYPE_POINTER_TO (expr) = NULL;
10912 TYPE_REFERENCE_TO (expr) = NULL;
10913 if (TYPE_CACHED_VALUES_P (expr))
10915 TYPE_CACHED_VALUES_P (expr) = 0;
10916 TYPE_CACHED_VALUES (expr) = NULL;
10919 md5_process_bytes (expr, tree_size (expr), ctx);
10920 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
10921 if (TREE_CODE_CLASS (code) != tcc_type
10922 && TREE_CODE_CLASS (code) != tcc_declaration
10923 && code != TREE_LIST)
10924 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
10925 switch (TREE_CODE_CLASS (code))
10927 case tcc_constant:
10928 switch (code)
10930 case STRING_CST:
10931 md5_process_bytes (TREE_STRING_POINTER (expr),
10932 TREE_STRING_LENGTH (expr), ctx);
10933 break;
10934 case COMPLEX_CST:
10935 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
10936 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
10937 break;
10938 case VECTOR_CST:
10939 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
10940 break;
10941 default:
10942 break;
10944 break;
10945 case tcc_exceptional:
10946 switch (code)
10948 case TREE_LIST:
10949 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
10950 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
10951 expr = TREE_CHAIN (expr);
10952 goto recursive_label;
10953 break;
10954 case TREE_VEC:
10955 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
10956 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
10957 break;
10958 default:
10959 break;
10961 break;
10962 case tcc_expression:
10963 case tcc_reference:
10964 case tcc_comparison:
10965 case tcc_unary:
10966 case tcc_binary:
10967 case tcc_statement:
10968 len = TREE_CODE_LENGTH (code);
10969 for (i = 0; i < len; ++i)
10970 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
10971 break;
10972 case tcc_declaration:
10973 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
10974 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
10975 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
10977 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
10978 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
10979 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
10980 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
10981 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
10983 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
10984 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
10986 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
10988 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
10989 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
10990 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
10992 break;
10993 case tcc_type:
10994 if (TREE_CODE (expr) == ENUMERAL_TYPE)
10995 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
10996 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
10997 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
10998 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
10999 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
11000 if (INTEGRAL_TYPE_P (expr)
11001 || SCALAR_FLOAT_TYPE_P (expr))
11003 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
11004 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
11006 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
11007 if (TREE_CODE (expr) == RECORD_TYPE
11008 || TREE_CODE (expr) == UNION_TYPE
11009 || TREE_CODE (expr) == QUAL_UNION_TYPE)
11010 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
11011 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
11012 break;
11013 default:
11014 break;
11018 #endif
11020 /* Fold a unary tree expression with code CODE of type TYPE with an
11021 operand OP0. Return a folded expression if successful. Otherwise,
11022 return a tree expression with code CODE of type TYPE with an
11023 operand OP0. */
11025 tree
11026 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
11028 tree tem;
11029 #ifdef ENABLE_FOLD_CHECKING
11030 unsigned char checksum_before[16], checksum_after[16];
11031 struct md5_ctx ctx;
11032 htab_t ht;
11034 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11035 md5_init_ctx (&ctx);
11036 fold_checksum_tree (op0, &ctx, ht);
11037 md5_finish_ctx (&ctx, checksum_before);
11038 htab_empty (ht);
11039 #endif
11041 tem = fold_unary (code, type, op0);
11042 if (!tem)
11043 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
11045 #ifdef ENABLE_FOLD_CHECKING
11046 md5_init_ctx (&ctx);
11047 fold_checksum_tree (op0, &ctx, ht);
11048 md5_finish_ctx (&ctx, checksum_after);
11049 htab_delete (ht);
11051 if (memcmp (checksum_before, checksum_after, 16))
11052 fold_check_failed (op0, tem);
11053 #endif
11054 return tem;
11057 /* Fold a binary tree expression with code CODE of type TYPE with
11058 operands OP0 and OP1. Return a folded expression if successful.
11059 Otherwise, return a tree expression with code CODE of type TYPE
11060 with operands OP0 and OP1. */
11062 tree
11063 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
11064 MEM_STAT_DECL)
11066 tree tem;
11067 #ifdef ENABLE_FOLD_CHECKING
11068 unsigned char checksum_before_op0[16],
11069 checksum_before_op1[16],
11070 checksum_after_op0[16],
11071 checksum_after_op1[16];
11072 struct md5_ctx ctx;
11073 htab_t ht;
11075 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11076 md5_init_ctx (&ctx);
11077 fold_checksum_tree (op0, &ctx, ht);
11078 md5_finish_ctx (&ctx, checksum_before_op0);
11079 htab_empty (ht);
11081 md5_init_ctx (&ctx);
11082 fold_checksum_tree (op1, &ctx, ht);
11083 md5_finish_ctx (&ctx, checksum_before_op1);
11084 htab_empty (ht);
11085 #endif
11087 tem = fold_binary (code, type, op0, op1);
11088 if (!tem)
11089 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
11091 #ifdef ENABLE_FOLD_CHECKING
11092 md5_init_ctx (&ctx);
11093 fold_checksum_tree (op0, &ctx, ht);
11094 md5_finish_ctx (&ctx, checksum_after_op0);
11095 htab_empty (ht);
11097 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
11098 fold_check_failed (op0, tem);
11100 md5_init_ctx (&ctx);
11101 fold_checksum_tree (op1, &ctx, ht);
11102 md5_finish_ctx (&ctx, checksum_after_op1);
11103 htab_delete (ht);
11105 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
11106 fold_check_failed (op1, tem);
11107 #endif
11108 return tem;
11111 /* Fold a ternary tree expression with code CODE of type TYPE with
11112 operands OP0, OP1, and OP2. Return a folded expression if
11113 successful. Otherwise, return a tree expression with code CODE of
11114 type TYPE with operands OP0, OP1, and OP2. */
11116 tree
11117 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
11118 MEM_STAT_DECL)
11120 tree tem;
11121 #ifdef ENABLE_FOLD_CHECKING
11122 unsigned char checksum_before_op0[16],
11123 checksum_before_op1[16],
11124 checksum_before_op2[16],
11125 checksum_after_op0[16],
11126 checksum_after_op1[16],
11127 checksum_after_op2[16];
11128 struct md5_ctx ctx;
11129 htab_t ht;
11131 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11132 md5_init_ctx (&ctx);
11133 fold_checksum_tree (op0, &ctx, ht);
11134 md5_finish_ctx (&ctx, checksum_before_op0);
11135 htab_empty (ht);
11137 md5_init_ctx (&ctx);
11138 fold_checksum_tree (op1, &ctx, ht);
11139 md5_finish_ctx (&ctx, checksum_before_op1);
11140 htab_empty (ht);
11142 md5_init_ctx (&ctx);
11143 fold_checksum_tree (op2, &ctx, ht);
11144 md5_finish_ctx (&ctx, checksum_before_op2);
11145 htab_empty (ht);
11146 #endif
11148 tem = fold_ternary (code, type, op0, op1, op2);
11149 if (!tem)
11150 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
11152 #ifdef ENABLE_FOLD_CHECKING
11153 md5_init_ctx (&ctx);
11154 fold_checksum_tree (op0, &ctx, ht);
11155 md5_finish_ctx (&ctx, checksum_after_op0);
11156 htab_empty (ht);
11158 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
11159 fold_check_failed (op0, tem);
11161 md5_init_ctx (&ctx);
11162 fold_checksum_tree (op1, &ctx, ht);
11163 md5_finish_ctx (&ctx, checksum_after_op1);
11164 htab_empty (ht);
11166 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
11167 fold_check_failed (op1, tem);
11169 md5_init_ctx (&ctx);
11170 fold_checksum_tree (op2, &ctx, ht);
11171 md5_finish_ctx (&ctx, checksum_after_op2);
11172 htab_delete (ht);
11174 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
11175 fold_check_failed (op2, tem);
11176 #endif
11177 return tem;
11180 /* Perform constant folding and related simplification of initializer
11181 expression EXPR. These behave identically to "fold_buildN" but ignore
11182 potential run-time traps and exceptions that fold must preserve. */
11184 #define START_FOLD_INIT \
11185 int saved_signaling_nans = flag_signaling_nans;\
11186 int saved_trapping_math = flag_trapping_math;\
11187 int saved_rounding_math = flag_rounding_math;\
11188 int saved_trapv = flag_trapv;\
11189 flag_signaling_nans = 0;\
11190 flag_trapping_math = 0;\
11191 flag_rounding_math = 0;\
11192 flag_trapv = 0
11194 #define END_FOLD_INIT \
11195 flag_signaling_nans = saved_signaling_nans;\
11196 flag_trapping_math = saved_trapping_math;\
11197 flag_rounding_math = saved_rounding_math;\
11198 flag_trapv = saved_trapv
11200 tree
11201 fold_build1_initializer (enum tree_code code, tree type, tree op)
11203 tree result;
11204 START_FOLD_INIT;
11206 result = fold_build1 (code, type, op);
11208 END_FOLD_INIT;
11209 return result;
11212 tree
11213 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
11215 tree result;
11216 START_FOLD_INIT;
11218 result = fold_build2 (code, type, op0, op1);
11220 END_FOLD_INIT;
11221 return result;
11224 tree
11225 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
11226 tree op2)
11228 tree result;
11229 START_FOLD_INIT;
11231 result = fold_build3 (code, type, op0, op1, op2);
11233 END_FOLD_INIT;
11234 return result;
11237 #undef START_FOLD_INIT
11238 #undef END_FOLD_INIT
11240 /* Determine if first argument is a multiple of second argument. Return 0 if
11241 it is not, or we cannot easily determined it to be.
11243 An example of the sort of thing we care about (at this point; this routine
11244 could surely be made more general, and expanded to do what the *_DIV_EXPR's
11245 fold cases do now) is discovering that
11247 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11249 is a multiple of
11251 SAVE_EXPR (J * 8)
11253 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
11255 This code also handles discovering that
11257 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11259 is a multiple of 8 so we don't have to worry about dealing with a
11260 possible remainder.
11262 Note that we *look* inside a SAVE_EXPR only to determine how it was
11263 calculated; it is not safe for fold to do much of anything else with the
11264 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
11265 at run time. For example, the latter example above *cannot* be implemented
11266 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
11267 evaluation time of the original SAVE_EXPR is not necessarily the same at
11268 the time the new expression is evaluated. The only optimization of this
11269 sort that would be valid is changing
11271 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
11273 divided by 8 to
11275 SAVE_EXPR (I) * SAVE_EXPR (J)
11277 (where the same SAVE_EXPR (J) is used in the original and the
11278 transformed version). */
11280 static int
11281 multiple_of_p (tree type, tree top, tree bottom)
11283 if (operand_equal_p (top, bottom, 0))
11284 return 1;
11286 if (TREE_CODE (type) != INTEGER_TYPE)
11287 return 0;
11289 switch (TREE_CODE (top))
11291 case BIT_AND_EXPR:
11292 /* Bitwise and provides a power of two multiple. If the mask is
11293 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
11294 if (!integer_pow2p (bottom))
11295 return 0;
11296 /* FALLTHRU */
11298 case MULT_EXPR:
11299 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11300 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11302 case PLUS_EXPR:
11303 case MINUS_EXPR:
11304 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11305 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11307 case LSHIFT_EXPR:
11308 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
11310 tree op1, t1;
11312 op1 = TREE_OPERAND (top, 1);
11313 /* const_binop may not detect overflow correctly,
11314 so check for it explicitly here. */
11315 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
11316 > TREE_INT_CST_LOW (op1)
11317 && TREE_INT_CST_HIGH (op1) == 0
11318 && 0 != (t1 = fold_convert (type,
11319 const_binop (LSHIFT_EXPR,
11320 size_one_node,
11321 op1, 0)))
11322 && ! TREE_OVERFLOW (t1))
11323 return multiple_of_p (type, t1, bottom);
11325 return 0;
11327 case NOP_EXPR:
11328 /* Can't handle conversions from non-integral or wider integral type. */
11329 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
11330 || (TYPE_PRECISION (type)
11331 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
11332 return 0;
11334 /* .. fall through ... */
11336 case SAVE_EXPR:
11337 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
11339 case INTEGER_CST:
11340 if (TREE_CODE (bottom) != INTEGER_CST
11341 || (TYPE_UNSIGNED (type)
11342 && (tree_int_cst_sgn (top) < 0
11343 || tree_int_cst_sgn (bottom) < 0)))
11344 return 0;
11345 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
11346 top, bottom, 0));
11348 default:
11349 return 0;
11353 /* Return true if `t' is known to be non-negative. */
11356 tree_expr_nonnegative_p (tree t)
11358 if (TYPE_UNSIGNED (TREE_TYPE (t)))
11359 return 1;
11361 switch (TREE_CODE (t))
11363 case SSA_NAME:
11364 /* Query VRP to see if it has recorded any information about
11365 the range of this object. */
11366 return ssa_name_nonnegative_p (t);
11368 case ABS_EXPR:
11369 /* We can't return 1 if flag_wrapv is set because
11370 ABS_EXPR<INT_MIN> = INT_MIN. */
11371 if (!(flag_wrapv && INTEGRAL_TYPE_P (TREE_TYPE (t))))
11372 return 1;
11373 break;
11375 case INTEGER_CST:
11376 return tree_int_cst_sgn (t) >= 0;
11378 case REAL_CST:
11379 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
11381 case PLUS_EXPR:
11382 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11383 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11384 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11386 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
11387 both unsigned and at least 2 bits shorter than the result. */
11388 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11389 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11390 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11392 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11393 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11394 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11395 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11397 unsigned int prec = MAX (TYPE_PRECISION (inner1),
11398 TYPE_PRECISION (inner2)) + 1;
11399 return prec < TYPE_PRECISION (TREE_TYPE (t));
11402 break;
11404 case MULT_EXPR:
11405 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11407 /* x * x for floating point x is always non-negative. */
11408 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
11409 return 1;
11410 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11411 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11414 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
11415 both unsigned and their total bits is shorter than the result. */
11416 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11417 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11418 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11420 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11421 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11422 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11423 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11424 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
11425 < TYPE_PRECISION (TREE_TYPE (t));
11427 return 0;
11429 case BIT_AND_EXPR:
11430 case MAX_EXPR:
11431 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11432 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11434 case BIT_IOR_EXPR:
11435 case BIT_XOR_EXPR:
11436 case MIN_EXPR:
11437 case RDIV_EXPR:
11438 case TRUNC_DIV_EXPR:
11439 case CEIL_DIV_EXPR:
11440 case FLOOR_DIV_EXPR:
11441 case ROUND_DIV_EXPR:
11442 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11443 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11445 case TRUNC_MOD_EXPR:
11446 case CEIL_MOD_EXPR:
11447 case FLOOR_MOD_EXPR:
11448 case ROUND_MOD_EXPR:
11449 case SAVE_EXPR:
11450 case NON_LVALUE_EXPR:
11451 case FLOAT_EXPR:
11452 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11454 case COMPOUND_EXPR:
11455 case MODIFY_EXPR:
11456 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11458 case BIND_EXPR:
11459 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
11461 case COND_EXPR:
11462 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
11463 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
11465 case NOP_EXPR:
11467 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11468 tree outer_type = TREE_TYPE (t);
11470 if (TREE_CODE (outer_type) == REAL_TYPE)
11472 if (TREE_CODE (inner_type) == REAL_TYPE)
11473 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11474 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11476 if (TYPE_UNSIGNED (inner_type))
11477 return 1;
11478 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11481 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
11483 if (TREE_CODE (inner_type) == REAL_TYPE)
11484 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
11485 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11486 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
11487 && TYPE_UNSIGNED (inner_type);
11490 break;
11492 case TARGET_EXPR:
11494 tree temp = TARGET_EXPR_SLOT (t);
11495 t = TARGET_EXPR_INITIAL (t);
11497 /* If the initializer is non-void, then it's a normal expression
11498 that will be assigned to the slot. */
11499 if (!VOID_TYPE_P (t))
11500 return tree_expr_nonnegative_p (t);
11502 /* Otherwise, the initializer sets the slot in some way. One common
11503 way is an assignment statement at the end of the initializer. */
11504 while (1)
11506 if (TREE_CODE (t) == BIND_EXPR)
11507 t = expr_last (BIND_EXPR_BODY (t));
11508 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
11509 || TREE_CODE (t) == TRY_CATCH_EXPR)
11510 t = expr_last (TREE_OPERAND (t, 0));
11511 else if (TREE_CODE (t) == STATEMENT_LIST)
11512 t = expr_last (t);
11513 else
11514 break;
11516 if (TREE_CODE (t) == MODIFY_EXPR
11517 && TREE_OPERAND (t, 0) == temp)
11518 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11520 return 0;
11523 case CALL_EXPR:
11525 tree fndecl = get_callee_fndecl (t);
11526 tree arglist = TREE_OPERAND (t, 1);
11527 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
11528 switch (DECL_FUNCTION_CODE (fndecl))
11530 CASE_FLT_FN (BUILT_IN_ACOS):
11531 CASE_FLT_FN (BUILT_IN_ACOSH):
11532 CASE_FLT_FN (BUILT_IN_CABS):
11533 CASE_FLT_FN (BUILT_IN_COSH):
11534 CASE_FLT_FN (BUILT_IN_ERFC):
11535 CASE_FLT_FN (BUILT_IN_EXP):
11536 CASE_FLT_FN (BUILT_IN_EXP10):
11537 CASE_FLT_FN (BUILT_IN_EXP2):
11538 CASE_FLT_FN (BUILT_IN_FABS):
11539 CASE_FLT_FN (BUILT_IN_FDIM):
11540 CASE_FLT_FN (BUILT_IN_HYPOT):
11541 CASE_FLT_FN (BUILT_IN_POW10):
11542 CASE_INT_FN (BUILT_IN_FFS):
11543 CASE_INT_FN (BUILT_IN_PARITY):
11544 CASE_INT_FN (BUILT_IN_POPCOUNT):
11545 /* Always true. */
11546 return 1;
11548 CASE_FLT_FN (BUILT_IN_SQRT):
11549 /* sqrt(-0.0) is -0.0. */
11550 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
11551 return 1;
11552 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11554 CASE_FLT_FN (BUILT_IN_ASINH):
11555 CASE_FLT_FN (BUILT_IN_ATAN):
11556 CASE_FLT_FN (BUILT_IN_ATANH):
11557 CASE_FLT_FN (BUILT_IN_CBRT):
11558 CASE_FLT_FN (BUILT_IN_CEIL):
11559 CASE_FLT_FN (BUILT_IN_ERF):
11560 CASE_FLT_FN (BUILT_IN_EXPM1):
11561 CASE_FLT_FN (BUILT_IN_FLOOR):
11562 CASE_FLT_FN (BUILT_IN_FMOD):
11563 CASE_FLT_FN (BUILT_IN_FREXP):
11564 CASE_FLT_FN (BUILT_IN_LCEIL):
11565 CASE_FLT_FN (BUILT_IN_LDEXP):
11566 CASE_FLT_FN (BUILT_IN_LFLOOR):
11567 CASE_FLT_FN (BUILT_IN_LLCEIL):
11568 CASE_FLT_FN (BUILT_IN_LLFLOOR):
11569 CASE_FLT_FN (BUILT_IN_LLRINT):
11570 CASE_FLT_FN (BUILT_IN_LLROUND):
11571 CASE_FLT_FN (BUILT_IN_LRINT):
11572 CASE_FLT_FN (BUILT_IN_LROUND):
11573 CASE_FLT_FN (BUILT_IN_MODF):
11574 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11575 CASE_FLT_FN (BUILT_IN_POW):
11576 CASE_FLT_FN (BUILT_IN_RINT):
11577 CASE_FLT_FN (BUILT_IN_ROUND):
11578 CASE_FLT_FN (BUILT_IN_SIGNBIT):
11579 CASE_FLT_FN (BUILT_IN_SINH):
11580 CASE_FLT_FN (BUILT_IN_TANH):
11581 CASE_FLT_FN (BUILT_IN_TRUNC):
11582 /* True if the 1st argument is nonnegative. */
11583 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11585 CASE_FLT_FN (BUILT_IN_FMAX):
11586 /* True if the 1st OR 2nd arguments are nonnegative. */
11587 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11588 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11590 CASE_FLT_FN (BUILT_IN_FMIN):
11591 /* True if the 1st AND 2nd arguments are nonnegative. */
11592 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11593 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11595 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11596 /* True if the 2nd argument is nonnegative. */
11597 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11599 default:
11600 break;
11604 /* ... fall through ... */
11606 default:
11607 if (truth_value_p (TREE_CODE (t)))
11608 /* Truth values evaluate to 0 or 1, which is nonnegative. */
11609 return 1;
11612 /* We don't know sign of `t', so be conservative and return false. */
11613 return 0;
11616 /* Return true when T is an address and is known to be nonzero.
11617 For floating point we further ensure that T is not denormal.
11618 Similar logic is present in nonzero_address in rtlanal.h. */
11620 bool
11621 tree_expr_nonzero_p (tree t)
11623 tree type = TREE_TYPE (t);
11625 /* Doing something useful for floating point would need more work. */
11626 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11627 return false;
11629 switch (TREE_CODE (t))
11631 case SSA_NAME:
11632 /* Query VRP to see if it has recorded any information about
11633 the range of this object. */
11634 return ssa_name_nonzero_p (t);
11636 case ABS_EXPR:
11637 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11639 case INTEGER_CST:
11640 /* We used to test for !integer_zerop here. This does not work correctly
11641 if TREE_CONSTANT_OVERFLOW (t). */
11642 return (TREE_INT_CST_LOW (t) != 0
11643 || TREE_INT_CST_HIGH (t) != 0);
11645 case PLUS_EXPR:
11646 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11648 /* With the presence of negative values it is hard
11649 to say something. */
11650 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11651 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11652 return false;
11653 /* One of operands must be positive and the other non-negative. */
11654 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11655 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11657 break;
11659 case MULT_EXPR:
11660 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11662 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11663 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11665 break;
11667 case NOP_EXPR:
11669 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11670 tree outer_type = TREE_TYPE (t);
11672 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
11673 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
11675 break;
11677 case ADDR_EXPR:
11679 tree base = get_base_address (TREE_OPERAND (t, 0));
11681 if (!base)
11682 return false;
11684 /* Weak declarations may link to NULL. */
11685 if (VAR_OR_FUNCTION_DECL_P (base))
11686 return !DECL_WEAK (base);
11688 /* Constants are never weak. */
11689 if (CONSTANT_CLASS_P (base))
11690 return true;
11692 return false;
11695 case COND_EXPR:
11696 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11697 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
11699 case MIN_EXPR:
11700 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11701 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11703 case MAX_EXPR:
11704 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
11706 /* When both operands are nonzero, then MAX must be too. */
11707 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
11708 return true;
11710 /* MAX where operand 0 is positive is positive. */
11711 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11713 /* MAX where operand 1 is positive is positive. */
11714 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11715 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11716 return true;
11717 break;
11719 case COMPOUND_EXPR:
11720 case MODIFY_EXPR:
11721 case BIND_EXPR:
11722 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
11724 case SAVE_EXPR:
11725 case NON_LVALUE_EXPR:
11726 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11728 case BIT_IOR_EXPR:
11729 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11730 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11732 case CALL_EXPR:
11733 return alloca_call_p (t);
11735 default:
11736 break;
11738 return false;
11741 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
11742 attempt to fold the expression to a constant without modifying TYPE,
11743 OP0 or OP1.
11745 If the expression could be simplified to a constant, then return
11746 the constant. If the expression would not be simplified to a
11747 constant, then return NULL_TREE. */
11749 tree
11750 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
11752 tree tem = fold_binary (code, type, op0, op1);
11753 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11756 /* Given the components of a unary expression CODE, TYPE and OP0,
11757 attempt to fold the expression to a constant without modifying
11758 TYPE or OP0.
11760 If the expression could be simplified to a constant, then return
11761 the constant. If the expression would not be simplified to a
11762 constant, then return NULL_TREE. */
11764 tree
11765 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
11767 tree tem = fold_unary (code, type, op0);
11768 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11771 /* If EXP represents referencing an element in a constant string
11772 (either via pointer arithmetic or array indexing), return the
11773 tree representing the value accessed, otherwise return NULL. */
11775 tree
11776 fold_read_from_constant_string (tree exp)
11778 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
11780 tree exp1 = TREE_OPERAND (exp, 0);
11781 tree index;
11782 tree string;
11784 if (TREE_CODE (exp) == INDIRECT_REF)
11785 string = string_constant (exp1, &index);
11786 else
11788 tree low_bound = array_ref_low_bound (exp);
11789 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
11791 /* Optimize the special-case of a zero lower bound.
11793 We convert the low_bound to sizetype to avoid some problems
11794 with constant folding. (E.g. suppose the lower bound is 1,
11795 and its mode is QI. Without the conversion,l (ARRAY
11796 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
11797 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
11798 if (! integer_zerop (low_bound))
11799 index = size_diffop (index, fold_convert (sizetype, low_bound));
11801 string = exp1;
11804 if (string
11805 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
11806 && TREE_CODE (string) == STRING_CST
11807 && TREE_CODE (index) == INTEGER_CST
11808 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
11809 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
11810 == MODE_INT)
11811 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
11812 return fold_convert (TREE_TYPE (exp),
11813 build_int_cst (NULL_TREE,
11814 (TREE_STRING_POINTER (string)
11815 [TREE_INT_CST_LOW (index)])));
11817 return NULL;
11820 /* Return the tree for neg (ARG0) when ARG0 is known to be either
11821 an integer constant or real constant.
11823 TYPE is the type of the result. */
11825 static tree
11826 fold_negate_const (tree arg0, tree type)
11828 tree t = NULL_TREE;
11830 switch (TREE_CODE (arg0))
11832 case INTEGER_CST:
11834 unsigned HOST_WIDE_INT low;
11835 HOST_WIDE_INT high;
11836 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11837 TREE_INT_CST_HIGH (arg0),
11838 &low, &high);
11839 t = build_int_cst_wide (type, low, high);
11840 t = force_fit_type (t, 1,
11841 (overflow | TREE_OVERFLOW (arg0))
11842 && !TYPE_UNSIGNED (type),
11843 TREE_CONSTANT_OVERFLOW (arg0));
11844 break;
11847 case REAL_CST:
11848 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11849 break;
11851 default:
11852 gcc_unreachable ();
11855 return t;
11858 /* Return the tree for abs (ARG0) when ARG0 is known to be either
11859 an integer constant or real constant.
11861 TYPE is the type of the result. */
11863 tree
11864 fold_abs_const (tree arg0, tree type)
11866 tree t = NULL_TREE;
11868 switch (TREE_CODE (arg0))
11870 case INTEGER_CST:
11871 /* If the value is unsigned, then the absolute value is
11872 the same as the ordinary value. */
11873 if (TYPE_UNSIGNED (type))
11874 t = arg0;
11875 /* Similarly, if the value is non-negative. */
11876 else if (INT_CST_LT (integer_minus_one_node, arg0))
11877 t = arg0;
11878 /* If the value is negative, then the absolute value is
11879 its negation. */
11880 else
11882 unsigned HOST_WIDE_INT low;
11883 HOST_WIDE_INT high;
11884 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11885 TREE_INT_CST_HIGH (arg0),
11886 &low, &high);
11887 t = build_int_cst_wide (type, low, high);
11888 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
11889 TREE_CONSTANT_OVERFLOW (arg0));
11891 break;
11893 case REAL_CST:
11894 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
11895 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11896 else
11897 t = arg0;
11898 break;
11900 default:
11901 gcc_unreachable ();
11904 return t;
11907 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11908 constant. TYPE is the type of the result. */
11910 static tree
11911 fold_not_const (tree arg0, tree type)
11913 tree t = NULL_TREE;
11915 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11917 t = build_int_cst_wide (type,
11918 ~ TREE_INT_CST_LOW (arg0),
11919 ~ TREE_INT_CST_HIGH (arg0));
11920 t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11921 TREE_CONSTANT_OVERFLOW (arg0));
11923 return t;
11926 /* Given CODE, a relational operator, the target type, TYPE and two
11927 constant operands OP0 and OP1, return the result of the
11928 relational operation. If the result is not a compile time
11929 constant, then return NULL_TREE. */
11931 static tree
11932 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11934 int result, invert;
11936 /* From here on, the only cases we handle are when the result is
11937 known to be a constant. */
11939 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11941 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11942 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11944 /* Handle the cases where either operand is a NaN. */
11945 if (real_isnan (c0) || real_isnan (c1))
11947 switch (code)
11949 case EQ_EXPR:
11950 case ORDERED_EXPR:
11951 result = 0;
11952 break;
11954 case NE_EXPR:
11955 case UNORDERED_EXPR:
11956 case UNLT_EXPR:
11957 case UNLE_EXPR:
11958 case UNGT_EXPR:
11959 case UNGE_EXPR:
11960 case UNEQ_EXPR:
11961 result = 1;
11962 break;
11964 case LT_EXPR:
11965 case LE_EXPR:
11966 case GT_EXPR:
11967 case GE_EXPR:
11968 case LTGT_EXPR:
11969 if (flag_trapping_math)
11970 return NULL_TREE;
11971 result = 0;
11972 break;
11974 default:
11975 gcc_unreachable ();
11978 return constant_boolean_node (result, type);
11981 return constant_boolean_node (real_compare (code, c0, c1), type);
11984 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11986 To compute GT, swap the arguments and do LT.
11987 To compute GE, do LT and invert the result.
11988 To compute LE, swap the arguments, do LT and invert the result.
11989 To compute NE, do EQ and invert the result.
11991 Therefore, the code below must handle only EQ and LT. */
11993 if (code == LE_EXPR || code == GT_EXPR)
11995 tree tem = op0;
11996 op0 = op1;
11997 op1 = tem;
11998 code = swap_tree_comparison (code);
12001 /* Note that it is safe to invert for real values here because we
12002 have already handled the one case that it matters. */
12004 invert = 0;
12005 if (code == NE_EXPR || code == GE_EXPR)
12007 invert = 1;
12008 code = invert_tree_comparison (code, false);
12011 /* Compute a result for LT or EQ if args permit;
12012 Otherwise return T. */
12013 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
12015 if (code == EQ_EXPR)
12016 result = tree_int_cst_equal (op0, op1);
12017 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
12018 result = INT_CST_LT_UNSIGNED (op0, op1);
12019 else
12020 result = INT_CST_LT (op0, op1);
12022 else
12023 return NULL_TREE;
12025 if (invert)
12026 result ^= 1;
12027 return constant_boolean_node (result, type);
12030 /* Build an expression for the a clean point containing EXPR with type TYPE.
12031 Don't build a cleanup point expression for EXPR which don't have side
12032 effects. */
12034 tree
12035 fold_build_cleanup_point_expr (tree type, tree expr)
12037 /* If the expression does not have side effects then we don't have to wrap
12038 it with a cleanup point expression. */
12039 if (!TREE_SIDE_EFFECTS (expr))
12040 return expr;
12042 /* If the expression is a return, check to see if the expression inside the
12043 return has no side effects or the right hand side of the modify expression
12044 inside the return. If either don't have side effects set we don't need to
12045 wrap the expression in a cleanup point expression. Note we don't check the
12046 left hand side of the modify because it should always be a return decl. */
12047 if (TREE_CODE (expr) == RETURN_EXPR)
12049 tree op = TREE_OPERAND (expr, 0);
12050 if (!op || !TREE_SIDE_EFFECTS (op))
12051 return expr;
12052 op = TREE_OPERAND (op, 1);
12053 if (!TREE_SIDE_EFFECTS (op))
12054 return expr;
12057 return build1 (CLEANUP_POINT_EXPR, type, expr);
12060 /* Build an expression for the address of T. Folds away INDIRECT_REF to
12061 avoid confusing the gimplify process. */
12063 tree
12064 build_fold_addr_expr_with_type (tree t, tree ptrtype)
12066 /* The size of the object is not relevant when talking about its address. */
12067 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12068 t = TREE_OPERAND (t, 0);
12070 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
12071 if (TREE_CODE (t) == INDIRECT_REF
12072 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
12074 t = TREE_OPERAND (t, 0);
12075 if (TREE_TYPE (t) != ptrtype)
12076 t = build1 (NOP_EXPR, ptrtype, t);
12078 else
12080 tree base = t;
12082 while (handled_component_p (base))
12083 base = TREE_OPERAND (base, 0);
12084 if (DECL_P (base))
12085 TREE_ADDRESSABLE (base) = 1;
12087 t = build1 (ADDR_EXPR, ptrtype, t);
12090 return t;
12093 tree
12094 build_fold_addr_expr (tree t)
12096 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
12099 /* Given a pointer value OP0 and a type TYPE, return a simplified version
12100 of an indirection through OP0, or NULL_TREE if no simplification is
12101 possible. */
12103 tree
12104 fold_indirect_ref_1 (tree type, tree op0)
12106 tree sub = op0;
12107 tree subtype;
12109 STRIP_NOPS (sub);
12110 subtype = TREE_TYPE (sub);
12111 if (!POINTER_TYPE_P (subtype))
12112 return NULL_TREE;
12114 if (TREE_CODE (sub) == ADDR_EXPR)
12116 tree op = TREE_OPERAND (sub, 0);
12117 tree optype = TREE_TYPE (op);
12118 /* *&p => p; make sure to handle *&"str"[cst] here. */
12119 if (type == optype)
12121 tree fop = fold_read_from_constant_string (op);
12122 if (fop)
12123 return fop;
12124 else
12125 return op;
12127 /* *(foo *)&fooarray => fooarray[0] */
12128 else if (TREE_CODE (optype) == ARRAY_TYPE
12129 && type == TREE_TYPE (optype))
12131 tree type_domain = TYPE_DOMAIN (optype);
12132 tree min_val = size_zero_node;
12133 if (type_domain && TYPE_MIN_VALUE (type_domain))
12134 min_val = TYPE_MIN_VALUE (type_domain);
12135 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
12137 /* *(foo *)&complexfoo => __real__ complexfoo */
12138 else if (TREE_CODE (optype) == COMPLEX_TYPE
12139 && type == TREE_TYPE (optype))
12140 return fold_build1 (REALPART_EXPR, type, op);
12143 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
12144 if (TREE_CODE (sub) == PLUS_EXPR
12145 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
12147 tree op00 = TREE_OPERAND (sub, 0);
12148 tree op01 = TREE_OPERAND (sub, 1);
12149 tree op00type;
12151 STRIP_NOPS (op00);
12152 op00type = TREE_TYPE (op00);
12153 if (TREE_CODE (op00) == ADDR_EXPR
12154 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
12155 && type == TREE_TYPE (TREE_TYPE (op00type)))
12157 tree size = TYPE_SIZE_UNIT (type);
12158 if (tree_int_cst_equal (size, op01))
12159 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
12163 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
12164 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
12165 && type == TREE_TYPE (TREE_TYPE (subtype)))
12167 tree type_domain;
12168 tree min_val = size_zero_node;
12169 sub = build_fold_indirect_ref (sub);
12170 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
12171 if (type_domain && TYPE_MIN_VALUE (type_domain))
12172 min_val = TYPE_MIN_VALUE (type_domain);
12173 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
12176 return NULL_TREE;
12179 /* Builds an expression for an indirection through T, simplifying some
12180 cases. */
12182 tree
12183 build_fold_indirect_ref (tree t)
12185 tree type = TREE_TYPE (TREE_TYPE (t));
12186 tree sub = fold_indirect_ref_1 (type, t);
12188 if (sub)
12189 return sub;
12190 else
12191 return build1 (INDIRECT_REF, type, t);
12194 /* Given an INDIRECT_REF T, return either T or a simplified version. */
12196 tree
12197 fold_indirect_ref (tree t)
12199 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
12201 if (sub)
12202 return sub;
12203 else
12204 return t;
12207 /* Strip non-trapping, non-side-effecting tree nodes from an expression
12208 whose result is ignored. The type of the returned tree need not be
12209 the same as the original expression. */
12211 tree
12212 fold_ignored_result (tree t)
12214 if (!TREE_SIDE_EFFECTS (t))
12215 return integer_zero_node;
12217 for (;;)
12218 switch (TREE_CODE_CLASS (TREE_CODE (t)))
12220 case tcc_unary:
12221 t = TREE_OPERAND (t, 0);
12222 break;
12224 case tcc_binary:
12225 case tcc_comparison:
12226 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12227 t = TREE_OPERAND (t, 0);
12228 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
12229 t = TREE_OPERAND (t, 1);
12230 else
12231 return t;
12232 break;
12234 case tcc_expression:
12235 switch (TREE_CODE (t))
12237 case COMPOUND_EXPR:
12238 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12239 return t;
12240 t = TREE_OPERAND (t, 0);
12241 break;
12243 case COND_EXPR:
12244 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
12245 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
12246 return t;
12247 t = TREE_OPERAND (t, 0);
12248 break;
12250 default:
12251 return t;
12253 break;
12255 default:
12256 return t;
12260 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
12261 This can only be applied to objects of a sizetype. */
12263 tree
12264 round_up (tree value, int divisor)
12266 tree div = NULL_TREE;
12268 gcc_assert (divisor > 0);
12269 if (divisor == 1)
12270 return value;
12272 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12273 have to do anything. Only do this when we are not given a const,
12274 because in that case, this check is more expensive than just
12275 doing it. */
12276 if (TREE_CODE (value) != INTEGER_CST)
12278 div = build_int_cst (TREE_TYPE (value), divisor);
12280 if (multiple_of_p (TREE_TYPE (value), value, div))
12281 return value;
12284 /* If divisor is a power of two, simplify this to bit manipulation. */
12285 if (divisor == (divisor & -divisor))
12287 tree t;
12289 t = build_int_cst (TREE_TYPE (value), divisor - 1);
12290 value = size_binop (PLUS_EXPR, value, t);
12291 t = build_int_cst (TREE_TYPE (value), -divisor);
12292 value = size_binop (BIT_AND_EXPR, value, t);
12294 else
12296 if (!div)
12297 div = build_int_cst (TREE_TYPE (value), divisor);
12298 value = size_binop (CEIL_DIV_EXPR, value, div);
12299 value = size_binop (MULT_EXPR, value, div);
12302 return value;
12305 /* Likewise, but round down. */
12307 tree
12308 round_down (tree value, int divisor)
12310 tree div = NULL_TREE;
12312 gcc_assert (divisor > 0);
12313 if (divisor == 1)
12314 return value;
12316 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12317 have to do anything. Only do this when we are not given a const,
12318 because in that case, this check is more expensive than just
12319 doing it. */
12320 if (TREE_CODE (value) != INTEGER_CST)
12322 div = build_int_cst (TREE_TYPE (value), divisor);
12324 if (multiple_of_p (TREE_TYPE (value), value, div))
12325 return value;
12328 /* If divisor is a power of two, simplify this to bit manipulation. */
12329 if (divisor == (divisor & -divisor))
12331 tree t;
12333 t = build_int_cst (TREE_TYPE (value), -divisor);
12334 value = size_binop (BIT_AND_EXPR, value, t);
12336 else
12338 if (!div)
12339 div = build_int_cst (TREE_TYPE (value), divisor);
12340 value = size_binop (FLOOR_DIV_EXPR, value, div);
12341 value = size_binop (MULT_EXPR, value, div);
12344 return value;
12347 /* Returns the pointer to the base of the object addressed by EXP and
12348 extracts the information about the offset of the access, storing it
12349 to PBITPOS and POFFSET. */
12351 static tree
12352 split_address_to_core_and_offset (tree exp,
12353 HOST_WIDE_INT *pbitpos, tree *poffset)
12355 tree core;
12356 enum machine_mode mode;
12357 int unsignedp, volatilep;
12358 HOST_WIDE_INT bitsize;
12360 if (TREE_CODE (exp) == ADDR_EXPR)
12362 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
12363 poffset, &mode, &unsignedp, &volatilep,
12364 false);
12365 core = build_fold_addr_expr (core);
12367 else
12369 core = exp;
12370 *pbitpos = 0;
12371 *poffset = NULL_TREE;
12374 return core;
12377 /* Returns true if addresses of E1 and E2 differ by a constant, false
12378 otherwise. If they do, E1 - E2 is stored in *DIFF. */
12380 bool
12381 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
12383 tree core1, core2;
12384 HOST_WIDE_INT bitpos1, bitpos2;
12385 tree toffset1, toffset2, tdiff, type;
12387 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
12388 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
12390 if (bitpos1 % BITS_PER_UNIT != 0
12391 || bitpos2 % BITS_PER_UNIT != 0
12392 || !operand_equal_p (core1, core2, 0))
12393 return false;
12395 if (toffset1 && toffset2)
12397 type = TREE_TYPE (toffset1);
12398 if (type != TREE_TYPE (toffset2))
12399 toffset2 = fold_convert (type, toffset2);
12401 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
12402 if (!cst_and_fits_in_hwi (tdiff))
12403 return false;
12405 *diff = int_cst_value (tdiff);
12407 else if (toffset1 || toffset2)
12409 /* If only one of the offsets is non-constant, the difference cannot
12410 be a constant. */
12411 return false;
12413 else
12414 *diff = 0;
12416 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
12417 return true;
12420 /* Simplify the floating point expression EXP when the sign of the
12421 result is not significant. Return NULL_TREE if no simplification
12422 is possible. */
12424 tree
12425 fold_strip_sign_ops (tree exp)
12427 tree arg0, arg1;
12429 switch (TREE_CODE (exp))
12431 case ABS_EXPR:
12432 case NEGATE_EXPR:
12433 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12434 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
12436 case MULT_EXPR:
12437 case RDIV_EXPR:
12438 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
12439 return NULL_TREE;
12440 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12441 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
12442 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
12443 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
12444 arg0 ? arg0 : TREE_OPERAND (exp, 0),
12445 arg1 ? arg1 : TREE_OPERAND (exp, 1));
12446 break;
12448 default:
12449 break;
12451 return NULL_TREE;