2006-03-15 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / fold-const.c
blob4e05368fe2b7605623fcdc065fa892813aa2b1ff
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 tem = fold_convert_const (code, type, arg0);
7078 return tem ? tem : NULL_TREE;
7080 case VIEW_CONVERT_EXPR:
7081 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7082 return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7083 return NULL_TREE;
7085 case NEGATE_EXPR:
7086 if (negate_expr_p (arg0))
7087 return fold_convert (type, negate_expr (arg0));
7088 return NULL_TREE;
7090 case ABS_EXPR:
7091 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7092 return fold_abs_const (arg0, type);
7093 else if (TREE_CODE (arg0) == NEGATE_EXPR)
7094 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7095 /* Convert fabs((double)float) into (double)fabsf(float). */
7096 else if (TREE_CODE (arg0) == NOP_EXPR
7097 && TREE_CODE (type) == REAL_TYPE)
7099 tree targ0 = strip_float_extensions (arg0);
7100 if (targ0 != arg0)
7101 return fold_convert (type, fold_build1 (ABS_EXPR,
7102 TREE_TYPE (targ0),
7103 targ0));
7105 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
7106 else if (tree_expr_nonnegative_p (arg0) || TREE_CODE (arg0) == ABS_EXPR)
7107 return arg0;
7109 /* Strip sign ops from argument. */
7110 if (TREE_CODE (type) == REAL_TYPE)
7112 tem = fold_strip_sign_ops (arg0);
7113 if (tem)
7114 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
7116 return NULL_TREE;
7118 case CONJ_EXPR:
7119 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7120 return fold_convert (type, arg0);
7121 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7122 return build2 (COMPLEX_EXPR, type,
7123 TREE_OPERAND (arg0, 0),
7124 negate_expr (TREE_OPERAND (arg0, 1)));
7125 else if (TREE_CODE (arg0) == COMPLEX_CST)
7126 return build_complex (type, TREE_REALPART (arg0),
7127 negate_expr (TREE_IMAGPART (arg0)));
7128 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7129 return fold_build2 (TREE_CODE (arg0), type,
7130 fold_build1 (CONJ_EXPR, type,
7131 TREE_OPERAND (arg0, 0)),
7132 fold_build1 (CONJ_EXPR, type,
7133 TREE_OPERAND (arg0, 1)));
7134 else if (TREE_CODE (arg0) == CONJ_EXPR)
7135 return TREE_OPERAND (arg0, 0);
7136 return NULL_TREE;
7138 case BIT_NOT_EXPR:
7139 if (TREE_CODE (arg0) == INTEGER_CST)
7140 return fold_not_const (arg0, type);
7141 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
7142 return TREE_OPERAND (arg0, 0);
7143 /* Convert ~ (-A) to A - 1. */
7144 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
7145 return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
7146 build_int_cst (type, 1));
7147 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
7148 else if (INTEGRAL_TYPE_P (type)
7149 && ((TREE_CODE (arg0) == MINUS_EXPR
7150 && integer_onep (TREE_OPERAND (arg0, 1)))
7151 || (TREE_CODE (arg0) == PLUS_EXPR
7152 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
7153 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7154 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7155 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7156 && (tem = fold_unary (BIT_NOT_EXPR, type,
7157 fold_convert (type,
7158 TREE_OPERAND (arg0, 0)))))
7159 return fold_build2 (BIT_XOR_EXPR, type, tem,
7160 fold_convert (type, TREE_OPERAND (arg0, 1)));
7161 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7162 && (tem = fold_unary (BIT_NOT_EXPR, type,
7163 fold_convert (type,
7164 TREE_OPERAND (arg0, 1)))))
7165 return fold_build2 (BIT_XOR_EXPR, type,
7166 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
7168 return NULL_TREE;
7170 case TRUTH_NOT_EXPR:
7171 /* The argument to invert_truthvalue must have Boolean type. */
7172 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7173 arg0 = fold_convert (boolean_type_node, arg0);
7175 /* Note that the operand of this must be an int
7176 and its values must be 0 or 1.
7177 ("true" is a fixed value perhaps depending on the language,
7178 but we don't handle values other than 1 correctly yet.) */
7179 tem = invert_truthvalue (arg0);
7180 /* Avoid infinite recursion. */
7181 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7182 return NULL_TREE;
7183 return fold_convert (type, tem);
7185 case REALPART_EXPR:
7186 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7187 return NULL_TREE;
7188 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7189 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7190 TREE_OPERAND (arg0, 1));
7191 else if (TREE_CODE (arg0) == COMPLEX_CST)
7192 return TREE_REALPART (arg0);
7193 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7194 return fold_build2 (TREE_CODE (arg0), type,
7195 fold_build1 (REALPART_EXPR, type,
7196 TREE_OPERAND (arg0, 0)),
7197 fold_build1 (REALPART_EXPR, type,
7198 TREE_OPERAND (arg0, 1)));
7199 return NULL_TREE;
7201 case IMAGPART_EXPR:
7202 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7203 return fold_convert (type, integer_zero_node);
7204 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7205 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7206 TREE_OPERAND (arg0, 0));
7207 else if (TREE_CODE (arg0) == COMPLEX_CST)
7208 return TREE_IMAGPART (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 (IMAGPART_EXPR, type,
7212 TREE_OPERAND (arg0, 0)),
7213 fold_build1 (IMAGPART_EXPR, type,
7214 TREE_OPERAND (arg0, 1)));
7215 return NULL_TREE;
7217 default:
7218 return NULL_TREE;
7219 } /* switch (code) */
7222 /* Fold a binary expression of code CODE and type TYPE with operands
7223 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
7224 Return the folded expression if folding is successful. Otherwise,
7225 return NULL_TREE. */
7227 static tree
7228 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
7230 enum tree_code compl_code;
7232 if (code == MIN_EXPR)
7233 compl_code = MAX_EXPR;
7234 else if (code == MAX_EXPR)
7235 compl_code = MIN_EXPR;
7236 else
7237 gcc_unreachable ();
7239 /* MIN (MAX (a, b), b) == b.  */
7240 if (TREE_CODE (op0) == compl_code
7241 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
7242 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
7244 /* MIN (MAX (b, a), b) == b.  */
7245 if (TREE_CODE (op0) == compl_code
7246 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
7247 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
7248 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
7250 /* MIN (a, MAX (a, b)) == a.  */
7251 if (TREE_CODE (op1) == compl_code
7252 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
7253 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
7254 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
7256 /* MIN (a, MAX (b, a)) == a.  */
7257 if (TREE_CODE (op1) == compl_code
7258 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
7259 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
7260 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
7262 return NULL_TREE;
7265 /* Subroutine of fold_binary. This routine performs all of the
7266 transformations that are common to the equality/inequality
7267 operators (EQ_EXPR and NE_EXPR) and the ordering operators
7268 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
7269 fold_binary should call fold_binary. Fold a comparison with
7270 tree code CODE and type TYPE with operands OP0 and OP1. Return
7271 the folded comparison or NULL_TREE. */
7273 static tree
7274 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
7276 tree arg0, arg1, tem;
7278 arg0 = op0;
7279 arg1 = op1;
7281 STRIP_SIGN_NOPS (arg0);
7282 STRIP_SIGN_NOPS (arg1);
7284 tem = fold_relational_const (code, type, arg0, arg1);
7285 if (tem != NULL_TREE)
7286 return tem;
7288 /* If one arg is a real or integer constant, put it last. */
7289 if (tree_swap_operands_p (arg0, arg1, true))
7290 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
7292 /* If this is a comparison of two exprs that look like an
7293 ARRAY_REF of the same object, then we can fold this to a
7294 comparison of the two offsets. */
7296 tree base0, offset0, base1, offset1;
7298 if (extract_array_ref (arg0, &base0, &offset0)
7299 && extract_array_ref (arg1, &base1, &offset1)
7300 && operand_equal_p (base0, base1, 0))
7302 /* Handle no offsets on both sides specially. */
7303 if (offset0 == NULL_TREE && offset1 == NULL_TREE)
7304 return fold_build2 (code, type, integer_zero_node,
7305 integer_zero_node);
7307 if (!offset0 || !offset1
7308 || TREE_TYPE (offset0) == TREE_TYPE (offset1))
7310 if (offset0 == NULL_TREE)
7311 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
7312 if (offset1 == NULL_TREE)
7313 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
7314 return fold_build2 (code, type, offset0, offset1);
7319 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
7320 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7321 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7322 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7323 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
7324 && !(flag_wrapv || flag_trapv))
7325 && (TREE_CODE (arg1) == INTEGER_CST
7326 && !TREE_OVERFLOW (arg1)))
7328 tree const1 = TREE_OPERAND (arg0, 1);
7329 tree const2 = arg1;
7330 tree variable = TREE_OPERAND (arg0, 0);
7331 tree lhs;
7332 int lhs_add;
7333 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
7335 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
7336 TREE_TYPE (arg1), const2, const1);
7337 if (TREE_CODE (lhs) == TREE_CODE (arg1)
7338 && (TREE_CODE (lhs) != INTEGER_CST
7339 || !TREE_OVERFLOW (lhs)))
7340 return fold_build2 (code, type, variable, lhs);
7343 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7345 tree targ0 = strip_float_extensions (arg0);
7346 tree targ1 = strip_float_extensions (arg1);
7347 tree newtype = TREE_TYPE (targ0);
7349 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7350 newtype = TREE_TYPE (targ1);
7352 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7353 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7354 return fold_build2 (code, type, fold_convert (newtype, targ0),
7355 fold_convert (newtype, targ1));
7357 /* (-a) CMP (-b) -> b CMP a */
7358 if (TREE_CODE (arg0) == NEGATE_EXPR
7359 && TREE_CODE (arg1) == NEGATE_EXPR)
7360 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
7361 TREE_OPERAND (arg0, 0));
7363 if (TREE_CODE (arg1) == REAL_CST)
7365 REAL_VALUE_TYPE cst;
7366 cst = TREE_REAL_CST (arg1);
7368 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7369 if (TREE_CODE (arg0) == NEGATE_EXPR)
7370 return fold_build2 (swap_tree_comparison (code), type,
7371 TREE_OPERAND (arg0, 0),
7372 build_real (TREE_TYPE (arg1),
7373 REAL_VALUE_NEGATE (cst)));
7375 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7376 /* a CMP (-0) -> a CMP 0 */
7377 if (REAL_VALUE_MINUS_ZERO (cst))
7378 return fold_build2 (code, type, arg0,
7379 build_real (TREE_TYPE (arg1), dconst0));
7381 /* x != NaN is always true, other ops are always false. */
7382 if (REAL_VALUE_ISNAN (cst)
7383 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7385 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7386 return omit_one_operand (type, tem, arg0);
7389 /* Fold comparisons against infinity. */
7390 if (REAL_VALUE_ISINF (cst))
7392 tem = fold_inf_compare (code, type, arg0, arg1);
7393 if (tem != NULL_TREE)
7394 return tem;
7398 /* If this is a comparison of a real constant with a PLUS_EXPR
7399 or a MINUS_EXPR of a real constant, we can convert it into a
7400 comparison with a revised real constant as long as no overflow
7401 occurs when unsafe_math_optimizations are enabled. */
7402 if (flag_unsafe_math_optimizations
7403 && TREE_CODE (arg1) == REAL_CST
7404 && (TREE_CODE (arg0) == PLUS_EXPR
7405 || TREE_CODE (arg0) == MINUS_EXPR)
7406 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7407 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7408 ? MINUS_EXPR : PLUS_EXPR,
7409 arg1, TREE_OPERAND (arg0, 1), 0))
7410 && ! TREE_CONSTANT_OVERFLOW (tem))
7411 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
7413 /* Likewise, we can simplify a comparison of a real constant with
7414 a MINUS_EXPR whose first operand is also a real constant, i.e.
7415 (c1 - x) < c2 becomes x > c1-c2. */
7416 if (flag_unsafe_math_optimizations
7417 && TREE_CODE (arg1) == REAL_CST
7418 && TREE_CODE (arg0) == MINUS_EXPR
7419 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7420 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7421 arg1, 0))
7422 && ! TREE_CONSTANT_OVERFLOW (tem))
7423 return fold_build2 (swap_tree_comparison (code), type,
7424 TREE_OPERAND (arg0, 1), tem);
7426 /* Fold comparisons against built-in math functions. */
7427 if (TREE_CODE (arg1) == REAL_CST
7428 && flag_unsafe_math_optimizations
7429 && ! flag_errno_math)
7431 enum built_in_function fcode = builtin_mathfn_code (arg0);
7433 if (fcode != END_BUILTINS)
7435 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7436 if (tem != NULL_TREE)
7437 return tem;
7442 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7443 if (TREE_CONSTANT (arg1)
7444 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7445 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7446 /* This optimization is invalid for ordered comparisons
7447 if CONST+INCR overflows or if foo+incr might overflow.
7448 This optimization is invalid for floating point due to rounding.
7449 For pointer types we assume overflow doesn't happen. */
7450 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7451 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7452 && (code == EQ_EXPR || code == NE_EXPR))))
7454 tree varop, newconst;
7456 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7458 newconst = fold_build2 (PLUS_EXPR, TREE_TYPE (arg0),
7459 arg1, TREE_OPERAND (arg0, 1));
7460 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7461 TREE_OPERAND (arg0, 0),
7462 TREE_OPERAND (arg0, 1));
7464 else
7466 newconst = fold_build2 (MINUS_EXPR, TREE_TYPE (arg0),
7467 arg1, TREE_OPERAND (arg0, 1));
7468 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7469 TREE_OPERAND (arg0, 0),
7470 TREE_OPERAND (arg0, 1));
7474 /* If VAROP is a reference to a bitfield, we must mask
7475 the constant by the width of the field. */
7476 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7477 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
7478 && host_integerp (DECL_SIZE (TREE_OPERAND
7479 (TREE_OPERAND (varop, 0), 1)), 1))
7481 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7482 HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
7483 tree folded_compare, shift;
7485 /* First check whether the comparison would come out
7486 always the same. If we don't do that we would
7487 change the meaning with the masking. */
7488 folded_compare = fold_build2 (code, type,
7489 TREE_OPERAND (varop, 0), arg1);
7490 if (TREE_CODE (folded_compare) == INTEGER_CST)
7491 return omit_one_operand (type, folded_compare, varop);
7493 shift = build_int_cst (NULL_TREE,
7494 TYPE_PRECISION (TREE_TYPE (varop)) - size);
7495 shift = fold_convert (TREE_TYPE (varop), shift);
7496 newconst = fold_build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7497 newconst, shift);
7498 newconst = fold_build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7499 newconst, shift);
7502 return fold_build2 (code, type, varop, newconst);
7505 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7506 && (TREE_CODE (arg0) == NOP_EXPR
7507 || TREE_CODE (arg0) == CONVERT_EXPR))
7509 /* If we are widening one operand of an integer comparison,
7510 see if the other operand is similarly being widened. Perhaps we
7511 can do the comparison in the narrower type. */
7512 tem = fold_widened_comparison (code, type, arg0, arg1);
7513 if (tem)
7514 return tem;
7516 /* Or if we are changing signedness. */
7517 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
7518 if (tem)
7519 return tem;
7522 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7523 constant, we can simplify it. */
7524 if (TREE_CODE (arg1) == INTEGER_CST
7525 && (TREE_CODE (arg0) == MIN_EXPR
7526 || TREE_CODE (arg0) == MAX_EXPR)
7527 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7529 tem = optimize_minmax_comparison (code, type, op0, op1);
7530 if (tem)
7531 return tem;
7534 /* Simplify comparison of something with itself. (For IEEE
7535 floating-point, we can only do some of these simplifications.) */
7536 if (operand_equal_p (arg0, arg1, 0))
7538 switch (code)
7540 case EQ_EXPR:
7541 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7542 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7543 return constant_boolean_node (1, type);
7544 break;
7546 case GE_EXPR:
7547 case LE_EXPR:
7548 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7549 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7550 return constant_boolean_node (1, type);
7551 return fold_build2 (EQ_EXPR, type, arg0, arg1);
7553 case NE_EXPR:
7554 /* For NE, we can only do this simplification if integer
7555 or we don't honor IEEE floating point NaNs. */
7556 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7557 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7558 break;
7559 /* ... fall through ... */
7560 case GT_EXPR:
7561 case LT_EXPR:
7562 return constant_boolean_node (0, type);
7563 default:
7564 gcc_unreachable ();
7568 /* If we are comparing an expression that just has comparisons
7569 of two integer values, arithmetic expressions of those comparisons,
7570 and constants, we can simplify it. There are only three cases
7571 to check: the two values can either be equal, the first can be
7572 greater, or the second can be greater. Fold the expression for
7573 those three values. Since each value must be 0 or 1, we have
7574 eight possibilities, each of which corresponds to the constant 0
7575 or 1 or one of the six possible comparisons.
7577 This handles common cases like (a > b) == 0 but also handles
7578 expressions like ((x > y) - (y > x)) > 0, which supposedly
7579 occur in macroized code. */
7581 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7583 tree cval1 = 0, cval2 = 0;
7584 int save_p = 0;
7586 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7587 /* Don't handle degenerate cases here; they should already
7588 have been handled anyway. */
7589 && cval1 != 0 && cval2 != 0
7590 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7591 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7592 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7593 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7594 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7595 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7596 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7598 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7599 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7601 /* We can't just pass T to eval_subst in case cval1 or cval2
7602 was the same as ARG1. */
7604 tree high_result
7605 = fold_build2 (code, type,
7606 eval_subst (arg0, cval1, maxval,
7607 cval2, minval),
7608 arg1);
7609 tree equal_result
7610 = fold_build2 (code, type,
7611 eval_subst (arg0, cval1, maxval,
7612 cval2, maxval),
7613 arg1);
7614 tree low_result
7615 = fold_build2 (code, type,
7616 eval_subst (arg0, cval1, minval,
7617 cval2, maxval),
7618 arg1);
7620 /* All three of these results should be 0 or 1. Confirm they are.
7621 Then use those values to select the proper code to use. */
7623 if (TREE_CODE (high_result) == INTEGER_CST
7624 && TREE_CODE (equal_result) == INTEGER_CST
7625 && TREE_CODE (low_result) == INTEGER_CST)
7627 /* Make a 3-bit mask with the high-order bit being the
7628 value for `>', the next for '=', and the low for '<'. */
7629 switch ((integer_onep (high_result) * 4)
7630 + (integer_onep (equal_result) * 2)
7631 + integer_onep (low_result))
7633 case 0:
7634 /* Always false. */
7635 return omit_one_operand (type, integer_zero_node, arg0);
7636 case 1:
7637 code = LT_EXPR;
7638 break;
7639 case 2:
7640 code = EQ_EXPR;
7641 break;
7642 case 3:
7643 code = LE_EXPR;
7644 break;
7645 case 4:
7646 code = GT_EXPR;
7647 break;
7648 case 5:
7649 code = NE_EXPR;
7650 break;
7651 case 6:
7652 code = GE_EXPR;
7653 break;
7654 case 7:
7655 /* Always true. */
7656 return omit_one_operand (type, integer_one_node, arg0);
7659 if (save_p)
7660 return save_expr (build2 (code, type, cval1, cval2));
7661 return fold_build2 (code, type, cval1, cval2);
7666 /* Fold a comparison of the address of COMPONENT_REFs with the same
7667 type and component to a comparison of the address of the base
7668 object. In short, &x->a OP &y->a to x OP y and
7669 &x->a OP &y.a to x OP &y */
7670 if (TREE_CODE (arg0) == ADDR_EXPR
7671 && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
7672 && TREE_CODE (arg1) == ADDR_EXPR
7673 && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
7675 tree cref0 = TREE_OPERAND (arg0, 0);
7676 tree cref1 = TREE_OPERAND (arg1, 0);
7677 if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
7679 tree op0 = TREE_OPERAND (cref0, 0);
7680 tree op1 = TREE_OPERAND (cref1, 0);
7681 return fold_build2 (code, type,
7682 build_fold_addr_expr (op0),
7683 build_fold_addr_expr (op1));
7687 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
7688 into a single range test. */
7689 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
7690 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
7691 && TREE_CODE (arg1) == INTEGER_CST
7692 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7693 && !integer_zerop (TREE_OPERAND (arg0, 1))
7694 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7695 && !TREE_OVERFLOW (arg1))
7697 tem = fold_div_compare (code, type, arg0, arg1);
7698 if (tem != NULL_TREE)
7699 return tem;
7702 return NULL_TREE;
7705 /* Fold a binary expression of code CODE and type TYPE with operands
7706 OP0 and OP1. Return the folded expression if folding is
7707 successful. Otherwise, return NULL_TREE. */
7709 tree
7710 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
7712 enum tree_code_class kind = TREE_CODE_CLASS (code);
7713 tree arg0, arg1, tem;
7714 tree t1 = NULL_TREE;
7716 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7717 && TREE_CODE_LENGTH (code) == 2
7718 && op0 != NULL_TREE
7719 && op1 != NULL_TREE);
7721 arg0 = op0;
7722 arg1 = op1;
7724 /* Strip any conversions that don't change the mode. This is
7725 safe for every expression, except for a comparison expression
7726 because its signedness is derived from its operands. So, in
7727 the latter case, only strip conversions that don't change the
7728 signedness.
7730 Note that this is done as an internal manipulation within the
7731 constant folder, in order to find the simplest representation
7732 of the arguments so that their form can be studied. In any
7733 cases, the appropriate type conversions should be put back in
7734 the tree that will get out of the constant folder. */
7736 if (kind == tcc_comparison)
7738 STRIP_SIGN_NOPS (arg0);
7739 STRIP_SIGN_NOPS (arg1);
7741 else
7743 STRIP_NOPS (arg0);
7744 STRIP_NOPS (arg1);
7747 /* Note that TREE_CONSTANT isn't enough: static var addresses are
7748 constant but we can't do arithmetic on them. */
7749 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7750 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7751 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
7752 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
7754 if (kind == tcc_binary)
7755 tem = const_binop (code, arg0, arg1, 0);
7756 else if (kind == tcc_comparison)
7757 tem = fold_relational_const (code, type, arg0, arg1);
7758 else
7759 tem = NULL_TREE;
7761 if (tem != NULL_TREE)
7763 if (TREE_TYPE (tem) != type)
7764 tem = fold_convert (type, tem);
7765 return tem;
7769 /* If this is a commutative operation, and ARG0 is a constant, move it
7770 to ARG1 to reduce the number of tests below. */
7771 if (commutative_tree_code (code)
7772 && tree_swap_operands_p (arg0, arg1, true))
7773 return fold_build2 (code, type, op1, op0);
7775 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
7777 First check for cases where an arithmetic operation is applied to a
7778 compound, conditional, or comparison operation. Push the arithmetic
7779 operation inside the compound or conditional to see if any folding
7780 can then be done. Convert comparison to conditional for this purpose.
7781 The also optimizes non-constant cases that used to be done in
7782 expand_expr.
7784 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
7785 one of the operands is a comparison and the other is a comparison, a
7786 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
7787 code below would make the expression more complex. Change it to a
7788 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
7789 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
7791 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
7792 || code == EQ_EXPR || code == NE_EXPR)
7793 && ((truth_value_p (TREE_CODE (arg0))
7794 && (truth_value_p (TREE_CODE (arg1))
7795 || (TREE_CODE (arg1) == BIT_AND_EXPR
7796 && integer_onep (TREE_OPERAND (arg1, 1)))))
7797 || (truth_value_p (TREE_CODE (arg1))
7798 && (truth_value_p (TREE_CODE (arg0))
7799 || (TREE_CODE (arg0) == BIT_AND_EXPR
7800 && integer_onep (TREE_OPERAND (arg0, 1)))))))
7802 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
7803 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
7804 : TRUTH_XOR_EXPR,
7805 boolean_type_node,
7806 fold_convert (boolean_type_node, arg0),
7807 fold_convert (boolean_type_node, arg1));
7809 if (code == EQ_EXPR)
7810 tem = invert_truthvalue (tem);
7812 return fold_convert (type, tem);
7815 if (TREE_CODE_CLASS (code) == tcc_binary
7816 || TREE_CODE_CLASS (code) == tcc_comparison)
7818 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7819 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7820 fold_build2 (code, type,
7821 TREE_OPERAND (arg0, 1), op1));
7822 if (TREE_CODE (arg1) == COMPOUND_EXPR
7823 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
7824 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7825 fold_build2 (code, type,
7826 op0, TREE_OPERAND (arg1, 1)));
7828 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
7830 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7831 arg0, arg1,
7832 /*cond_first_p=*/1);
7833 if (tem != NULL_TREE)
7834 return tem;
7837 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
7839 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7840 arg1, arg0,
7841 /*cond_first_p=*/0);
7842 if (tem != NULL_TREE)
7843 return tem;
7847 switch (code)
7849 case PLUS_EXPR:
7850 /* A + (-B) -> A - B */
7851 if (TREE_CODE (arg1) == NEGATE_EXPR)
7852 return fold_build2 (MINUS_EXPR, type,
7853 fold_convert (type, arg0),
7854 fold_convert (type, TREE_OPERAND (arg1, 0)));
7855 /* (-A) + B -> B - A */
7856 if (TREE_CODE (arg0) == NEGATE_EXPR
7857 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7858 return fold_build2 (MINUS_EXPR, type,
7859 fold_convert (type, arg1),
7860 fold_convert (type, TREE_OPERAND (arg0, 0)));
7861 /* Convert ~A + 1 to -A. */
7862 if (INTEGRAL_TYPE_P (type)
7863 && TREE_CODE (arg0) == BIT_NOT_EXPR
7864 && integer_onep (arg1))
7865 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7867 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
7868 same or one. */
7869 if ((TREE_CODE (arg0) == MULT_EXPR
7870 || TREE_CODE (arg1) == MULT_EXPR)
7871 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7873 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
7874 if (tem)
7875 return tem;
7878 if (! FLOAT_TYPE_P (type))
7880 if (integer_zerop (arg1))
7881 return non_lvalue (fold_convert (type, arg0));
7883 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7884 with a constant, and the two constants have no bits in common,
7885 we should treat this as a BIT_IOR_EXPR since this may produce more
7886 simplifications. */
7887 if (TREE_CODE (arg0) == BIT_AND_EXPR
7888 && TREE_CODE (arg1) == BIT_AND_EXPR
7889 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7890 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7891 && integer_zerop (const_binop (BIT_AND_EXPR,
7892 TREE_OPERAND (arg0, 1),
7893 TREE_OPERAND (arg1, 1), 0)))
7895 code = BIT_IOR_EXPR;
7896 goto bit_ior;
7899 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7900 (plus (plus (mult) (mult)) (foo)) so that we can
7901 take advantage of the factoring cases below. */
7902 if (((TREE_CODE (arg0) == PLUS_EXPR
7903 || TREE_CODE (arg0) == MINUS_EXPR)
7904 && TREE_CODE (arg1) == MULT_EXPR)
7905 || ((TREE_CODE (arg1) == PLUS_EXPR
7906 || TREE_CODE (arg1) == MINUS_EXPR)
7907 && TREE_CODE (arg0) == MULT_EXPR))
7909 tree parg0, parg1, parg, marg;
7910 enum tree_code pcode;
7912 if (TREE_CODE (arg1) == MULT_EXPR)
7913 parg = arg0, marg = arg1;
7914 else
7915 parg = arg1, marg = arg0;
7916 pcode = TREE_CODE (parg);
7917 parg0 = TREE_OPERAND (parg, 0);
7918 parg1 = TREE_OPERAND (parg, 1);
7919 STRIP_NOPS (parg0);
7920 STRIP_NOPS (parg1);
7922 if (TREE_CODE (parg0) == MULT_EXPR
7923 && TREE_CODE (parg1) != MULT_EXPR)
7924 return fold_build2 (pcode, type,
7925 fold_build2 (PLUS_EXPR, type,
7926 fold_convert (type, parg0),
7927 fold_convert (type, marg)),
7928 fold_convert (type, parg1));
7929 if (TREE_CODE (parg0) != MULT_EXPR
7930 && TREE_CODE (parg1) == MULT_EXPR)
7931 return fold_build2 (PLUS_EXPR, type,
7932 fold_convert (type, parg0),
7933 fold_build2 (pcode, type,
7934 fold_convert (type, marg),
7935 fold_convert (type,
7936 parg1)));
7939 /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7940 of the array. Loop optimizer sometimes produce this type of
7941 expressions. */
7942 if (TREE_CODE (arg0) == ADDR_EXPR)
7944 tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7945 if (tem)
7946 return fold_convert (type, tem);
7948 else if (TREE_CODE (arg1) == ADDR_EXPR)
7950 tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7951 if (tem)
7952 return fold_convert (type, tem);
7955 else
7957 /* See if ARG1 is zero and X + ARG1 reduces to X. */
7958 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7959 return non_lvalue (fold_convert (type, arg0));
7961 /* Likewise if the operands are reversed. */
7962 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7963 return non_lvalue (fold_convert (type, arg1));
7965 /* Convert X + -C into X - C. */
7966 if (TREE_CODE (arg1) == REAL_CST
7967 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7969 tem = fold_negate_const (arg1, type);
7970 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7971 return fold_build2 (MINUS_EXPR, type,
7972 fold_convert (type, arg0),
7973 fold_convert (type, tem));
7976 if (flag_unsafe_math_optimizations
7977 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
7978 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
7979 && (tem = distribute_real_division (code, type, arg0, arg1)))
7980 return tem;
7982 /* Convert x+x into x*2.0. */
7983 if (operand_equal_p (arg0, arg1, 0)
7984 && SCALAR_FLOAT_TYPE_P (type))
7985 return fold_build2 (MULT_EXPR, type, arg0,
7986 build_real (type, dconst2));
7988 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
7989 if (flag_unsafe_math_optimizations
7990 && TREE_CODE (arg1) == PLUS_EXPR
7991 && TREE_CODE (arg0) != MULT_EXPR)
7993 tree tree10 = TREE_OPERAND (arg1, 0);
7994 tree tree11 = TREE_OPERAND (arg1, 1);
7995 if (TREE_CODE (tree11) == MULT_EXPR
7996 && TREE_CODE (tree10) == MULT_EXPR)
7998 tree tree0;
7999 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
8000 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
8003 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
8004 if (flag_unsafe_math_optimizations
8005 && TREE_CODE (arg0) == PLUS_EXPR
8006 && TREE_CODE (arg1) != MULT_EXPR)
8008 tree tree00 = TREE_OPERAND (arg0, 0);
8009 tree tree01 = TREE_OPERAND (arg0, 1);
8010 if (TREE_CODE (tree01) == MULT_EXPR
8011 && TREE_CODE (tree00) == MULT_EXPR)
8013 tree tree0;
8014 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
8015 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
8020 bit_rotate:
8021 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
8022 is a rotate of A by C1 bits. */
8023 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
8024 is a rotate of A by B bits. */
8026 enum tree_code code0, code1;
8027 code0 = TREE_CODE (arg0);
8028 code1 = TREE_CODE (arg1);
8029 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
8030 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
8031 && operand_equal_p (TREE_OPERAND (arg0, 0),
8032 TREE_OPERAND (arg1, 0), 0)
8033 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8035 tree tree01, tree11;
8036 enum tree_code code01, code11;
8038 tree01 = TREE_OPERAND (arg0, 1);
8039 tree11 = TREE_OPERAND (arg1, 1);
8040 STRIP_NOPS (tree01);
8041 STRIP_NOPS (tree11);
8042 code01 = TREE_CODE (tree01);
8043 code11 = TREE_CODE (tree11);
8044 if (code01 == INTEGER_CST
8045 && code11 == INTEGER_CST
8046 && TREE_INT_CST_HIGH (tree01) == 0
8047 && TREE_INT_CST_HIGH (tree11) == 0
8048 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
8049 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
8050 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
8051 code0 == LSHIFT_EXPR ? tree01 : tree11);
8052 else if (code11 == MINUS_EXPR)
8054 tree tree110, tree111;
8055 tree110 = TREE_OPERAND (tree11, 0);
8056 tree111 = TREE_OPERAND (tree11, 1);
8057 STRIP_NOPS (tree110);
8058 STRIP_NOPS (tree111);
8059 if (TREE_CODE (tree110) == INTEGER_CST
8060 && 0 == compare_tree_int (tree110,
8061 TYPE_PRECISION
8062 (TREE_TYPE (TREE_OPERAND
8063 (arg0, 0))))
8064 && operand_equal_p (tree01, tree111, 0))
8065 return build2 ((code0 == LSHIFT_EXPR
8066 ? LROTATE_EXPR
8067 : RROTATE_EXPR),
8068 type, TREE_OPERAND (arg0, 0), tree01);
8070 else if (code01 == MINUS_EXPR)
8072 tree tree010, tree011;
8073 tree010 = TREE_OPERAND (tree01, 0);
8074 tree011 = TREE_OPERAND (tree01, 1);
8075 STRIP_NOPS (tree010);
8076 STRIP_NOPS (tree011);
8077 if (TREE_CODE (tree010) == INTEGER_CST
8078 && 0 == compare_tree_int (tree010,
8079 TYPE_PRECISION
8080 (TREE_TYPE (TREE_OPERAND
8081 (arg0, 0))))
8082 && operand_equal_p (tree11, tree011, 0))
8083 return build2 ((code0 != LSHIFT_EXPR
8084 ? LROTATE_EXPR
8085 : RROTATE_EXPR),
8086 type, TREE_OPERAND (arg0, 0), tree11);
8091 associate:
8092 /* In most languages, can't associate operations on floats through
8093 parentheses. Rather than remember where the parentheses were, we
8094 don't associate floats at all, unless the user has specified
8095 -funsafe-math-optimizations. */
8097 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8099 tree var0, con0, lit0, minus_lit0;
8100 tree var1, con1, lit1, minus_lit1;
8102 /* Split both trees into variables, constants, and literals. Then
8103 associate each group together, the constants with literals,
8104 then the result with variables. This increases the chances of
8105 literals being recombined later and of generating relocatable
8106 expressions for the sum of a constant and literal. */
8107 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
8108 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
8109 code == MINUS_EXPR);
8111 /* Only do something if we found more than two objects. Otherwise,
8112 nothing has changed and we risk infinite recursion. */
8113 if (2 < ((var0 != 0) + (var1 != 0)
8114 + (con0 != 0) + (con1 != 0)
8115 + (lit0 != 0) + (lit1 != 0)
8116 + (minus_lit0 != 0) + (minus_lit1 != 0)))
8118 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
8119 if (code == MINUS_EXPR)
8120 code = PLUS_EXPR;
8122 var0 = associate_trees (var0, var1, code, type);
8123 con0 = associate_trees (con0, con1, code, type);
8124 lit0 = associate_trees (lit0, lit1, code, type);
8125 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
8127 /* Preserve the MINUS_EXPR if the negative part of the literal is
8128 greater than the positive part. Otherwise, the multiplicative
8129 folding code (i.e extract_muldiv) may be fooled in case
8130 unsigned constants are subtracted, like in the following
8131 example: ((X*2 + 4) - 8U)/2. */
8132 if (minus_lit0 && lit0)
8134 if (TREE_CODE (lit0) == INTEGER_CST
8135 && TREE_CODE (minus_lit0) == INTEGER_CST
8136 && tree_int_cst_lt (lit0, minus_lit0))
8138 minus_lit0 = associate_trees (minus_lit0, lit0,
8139 MINUS_EXPR, type);
8140 lit0 = 0;
8142 else
8144 lit0 = associate_trees (lit0, minus_lit0,
8145 MINUS_EXPR, type);
8146 minus_lit0 = 0;
8149 if (minus_lit0)
8151 if (con0 == 0)
8152 return fold_convert (type,
8153 associate_trees (var0, minus_lit0,
8154 MINUS_EXPR, type));
8155 else
8157 con0 = associate_trees (con0, minus_lit0,
8158 MINUS_EXPR, type);
8159 return fold_convert (type,
8160 associate_trees (var0, con0,
8161 PLUS_EXPR, type));
8165 con0 = associate_trees (con0, lit0, code, type);
8166 return fold_convert (type, associate_trees (var0, con0,
8167 code, type));
8171 return NULL_TREE;
8173 case MINUS_EXPR:
8174 /* A - (-B) -> A + B */
8175 if (TREE_CODE (arg1) == NEGATE_EXPR)
8176 return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
8177 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
8178 if (TREE_CODE (arg0) == NEGATE_EXPR
8179 && (FLOAT_TYPE_P (type)
8180 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
8181 && negate_expr_p (arg1)
8182 && reorder_operands_p (arg0, arg1))
8183 return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
8184 TREE_OPERAND (arg0, 0));
8185 /* Convert -A - 1 to ~A. */
8186 if (INTEGRAL_TYPE_P (type)
8187 && TREE_CODE (arg0) == NEGATE_EXPR
8188 && integer_onep (arg1))
8189 return fold_build1 (BIT_NOT_EXPR, type, TREE_OPERAND (arg0, 0));
8191 /* Convert -1 - A to ~A. */
8192 if (INTEGRAL_TYPE_P (type)
8193 && integer_all_onesp (arg0))
8194 return fold_build1 (BIT_NOT_EXPR, type, arg1);
8196 if (! FLOAT_TYPE_P (type))
8198 if (integer_zerop (arg0))
8199 return negate_expr (fold_convert (type, arg1));
8200 if (integer_zerop (arg1))
8201 return non_lvalue (fold_convert (type, arg0));
8203 /* Fold A - (A & B) into ~B & A. */
8204 if (!TREE_SIDE_EFFECTS (arg0)
8205 && TREE_CODE (arg1) == BIT_AND_EXPR)
8207 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
8208 return fold_build2 (BIT_AND_EXPR, type,
8209 fold_build1 (BIT_NOT_EXPR, type,
8210 TREE_OPERAND (arg1, 0)),
8211 arg0);
8212 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8213 return fold_build2 (BIT_AND_EXPR, type,
8214 fold_build1 (BIT_NOT_EXPR, type,
8215 TREE_OPERAND (arg1, 1)),
8216 arg0);
8219 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
8220 any power of 2 minus 1. */
8221 if (TREE_CODE (arg0) == BIT_AND_EXPR
8222 && TREE_CODE (arg1) == BIT_AND_EXPR
8223 && operand_equal_p (TREE_OPERAND (arg0, 0),
8224 TREE_OPERAND (arg1, 0), 0))
8226 tree mask0 = TREE_OPERAND (arg0, 1);
8227 tree mask1 = TREE_OPERAND (arg1, 1);
8228 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
8230 if (operand_equal_p (tem, mask1, 0))
8232 tem = fold_build2 (BIT_XOR_EXPR, type,
8233 TREE_OPERAND (arg0, 0), mask1);
8234 return fold_build2 (MINUS_EXPR, type, tem, mask1);
8239 /* See if ARG1 is zero and X - ARG1 reduces to X. */
8240 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
8241 return non_lvalue (fold_convert (type, arg0));
8243 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
8244 ARG0 is zero and X + ARG0 reduces to X, since that would mean
8245 (-ARG1 + ARG0) reduces to -ARG1. */
8246 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
8247 return negate_expr (fold_convert (type, arg1));
8249 /* Fold &x - &x. This can happen from &x.foo - &x.
8250 This is unsafe for certain floats even in non-IEEE formats.
8251 In IEEE, it is unsafe because it does wrong for NaNs.
8252 Also note that operand_equal_p is always false if an operand
8253 is volatile. */
8255 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
8256 && operand_equal_p (arg0, arg1, 0))
8257 return fold_convert (type, integer_zero_node);
8259 /* A - B -> A + (-B) if B is easily negatable. */
8260 if (negate_expr_p (arg1)
8261 && ((FLOAT_TYPE_P (type)
8262 /* Avoid this transformation if B is a positive REAL_CST. */
8263 && (TREE_CODE (arg1) != REAL_CST
8264 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
8265 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
8266 return fold_build2 (PLUS_EXPR, type,
8267 fold_convert (type, arg0),
8268 fold_convert (type, negate_expr (arg1)));
8270 /* Try folding difference of addresses. */
8272 HOST_WIDE_INT diff;
8274 if ((TREE_CODE (arg0) == ADDR_EXPR
8275 || TREE_CODE (arg1) == ADDR_EXPR)
8276 && ptr_difference_const (arg0, arg1, &diff))
8277 return build_int_cst_type (type, diff);
8280 /* Fold &a[i] - &a[j] to i-j. */
8281 if (TREE_CODE (arg0) == ADDR_EXPR
8282 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
8283 && TREE_CODE (arg1) == ADDR_EXPR
8284 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
8286 tree aref0 = TREE_OPERAND (arg0, 0);
8287 tree aref1 = TREE_OPERAND (arg1, 0);
8288 if (operand_equal_p (TREE_OPERAND (aref0, 0),
8289 TREE_OPERAND (aref1, 0), 0))
8291 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
8292 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
8293 tree esz = array_ref_element_size (aref0);
8294 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8295 return fold_build2 (MULT_EXPR, type, diff,
8296 fold_convert (type, esz));
8301 /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
8302 of the array. Loop optimizer sometimes produce this type of
8303 expressions. */
8304 if (TREE_CODE (arg0) == ADDR_EXPR)
8306 tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
8307 if (tem)
8308 return fold_convert (type, tem);
8311 if (flag_unsafe_math_optimizations
8312 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
8313 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
8314 && (tem = distribute_real_division (code, type, arg0, arg1)))
8315 return tem;
8317 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
8318 same or one. */
8319 if ((TREE_CODE (arg0) == MULT_EXPR
8320 || TREE_CODE (arg1) == MULT_EXPR)
8321 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
8323 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
8324 if (tem)
8325 return tem;
8328 goto associate;
8330 case MULT_EXPR:
8331 /* (-A) * (-B) -> A * B */
8332 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8333 return fold_build2 (MULT_EXPR, type,
8334 TREE_OPERAND (arg0, 0),
8335 negate_expr (arg1));
8336 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8337 return fold_build2 (MULT_EXPR, type,
8338 negate_expr (arg0),
8339 TREE_OPERAND (arg1, 0));
8341 if (! FLOAT_TYPE_P (type))
8343 if (integer_zerop (arg1))
8344 return omit_one_operand (type, arg1, arg0);
8345 if (integer_onep (arg1))
8346 return non_lvalue (fold_convert (type, arg0));
8347 /* Transform x * -1 into -x. */
8348 if (integer_all_onesp (arg1))
8349 return fold_convert (type, negate_expr (arg0));
8351 /* (a * (1 << b)) is (a << b) */
8352 if (TREE_CODE (arg1) == LSHIFT_EXPR
8353 && integer_onep (TREE_OPERAND (arg1, 0)))
8354 return fold_build2 (LSHIFT_EXPR, type, arg0,
8355 TREE_OPERAND (arg1, 1));
8356 if (TREE_CODE (arg0) == LSHIFT_EXPR
8357 && integer_onep (TREE_OPERAND (arg0, 0)))
8358 return fold_build2 (LSHIFT_EXPR, type, arg1,
8359 TREE_OPERAND (arg0, 1));
8361 if (TREE_CODE (arg1) == INTEGER_CST
8362 && 0 != (tem = extract_muldiv (op0,
8363 fold_convert (type, arg1),
8364 code, NULL_TREE)))
8365 return fold_convert (type, tem);
8368 else
8370 /* Maybe fold x * 0 to 0. The expressions aren't the same
8371 when x is NaN, since x * 0 is also NaN. Nor are they the
8372 same in modes with signed zeros, since multiplying a
8373 negative value by 0 gives -0, not +0. */
8374 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8375 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
8376 && real_zerop (arg1))
8377 return omit_one_operand (type, arg1, arg0);
8378 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
8379 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8380 && real_onep (arg1))
8381 return non_lvalue (fold_convert (type, arg0));
8383 /* Transform x * -1.0 into -x. */
8384 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8385 && real_minus_onep (arg1))
8386 return fold_convert (type, negate_expr (arg0));
8388 /* Convert (C1/X)*C2 into (C1*C2)/X. */
8389 if (flag_unsafe_math_optimizations
8390 && TREE_CODE (arg0) == RDIV_EXPR
8391 && TREE_CODE (arg1) == REAL_CST
8392 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
8394 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
8395 arg1, 0);
8396 if (tem)
8397 return fold_build2 (RDIV_EXPR, type, tem,
8398 TREE_OPERAND (arg0, 1));
8401 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
8402 if (operand_equal_p (arg0, arg1, 0))
8404 tree tem = fold_strip_sign_ops (arg0);
8405 if (tem != NULL_TREE)
8407 tem = fold_convert (type, tem);
8408 return fold_build2 (MULT_EXPR, type, tem, tem);
8412 if (flag_unsafe_math_optimizations)
8414 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8415 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8417 /* Optimizations of root(...)*root(...). */
8418 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
8420 tree rootfn, arg, arglist;
8421 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8422 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8424 /* Optimize sqrt(x)*sqrt(x) as x. */
8425 if (BUILTIN_SQRT_P (fcode0)
8426 && operand_equal_p (arg00, arg10, 0)
8427 && ! HONOR_SNANS (TYPE_MODE (type)))
8428 return arg00;
8430 /* Optimize root(x)*root(y) as root(x*y). */
8431 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8432 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8433 arglist = build_tree_list (NULL_TREE, arg);
8434 return build_function_call_expr (rootfn, arglist);
8437 /* Optimize expN(x)*expN(y) as expN(x+y). */
8438 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
8440 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8441 tree arg = fold_build2 (PLUS_EXPR, type,
8442 TREE_VALUE (TREE_OPERAND (arg0, 1)),
8443 TREE_VALUE (TREE_OPERAND (arg1, 1)));
8444 tree arglist = build_tree_list (NULL_TREE, arg);
8445 return build_function_call_expr (expfn, arglist);
8448 /* Optimizations of pow(...)*pow(...). */
8449 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
8450 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
8451 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
8453 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8454 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8455 1)));
8456 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8457 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8458 1)));
8460 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
8461 if (operand_equal_p (arg01, arg11, 0))
8463 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8464 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8465 tree arglist = tree_cons (NULL_TREE, arg,
8466 build_tree_list (NULL_TREE,
8467 arg01));
8468 return build_function_call_expr (powfn, arglist);
8471 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
8472 if (operand_equal_p (arg00, arg10, 0))
8474 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8475 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
8476 tree arglist = tree_cons (NULL_TREE, arg00,
8477 build_tree_list (NULL_TREE,
8478 arg));
8479 return build_function_call_expr (powfn, arglist);
8483 /* Optimize tan(x)*cos(x) as sin(x). */
8484 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
8485 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
8486 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
8487 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
8488 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
8489 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
8490 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8491 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8493 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
8495 if (sinfn != NULL_TREE)
8496 return build_function_call_expr (sinfn,
8497 TREE_OPERAND (arg0, 1));
8500 /* Optimize x*pow(x,c) as pow(x,c+1). */
8501 if (fcode1 == BUILT_IN_POW
8502 || fcode1 == BUILT_IN_POWF
8503 || fcode1 == BUILT_IN_POWL)
8505 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8506 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8507 1)));
8508 if (TREE_CODE (arg11) == REAL_CST
8509 && ! TREE_CONSTANT_OVERFLOW (arg11)
8510 && operand_equal_p (arg0, arg10, 0))
8512 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8513 REAL_VALUE_TYPE c;
8514 tree arg, arglist;
8516 c = TREE_REAL_CST (arg11);
8517 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8518 arg = build_real (type, c);
8519 arglist = build_tree_list (NULL_TREE, arg);
8520 arglist = tree_cons (NULL_TREE, arg0, arglist);
8521 return build_function_call_expr (powfn, arglist);
8525 /* Optimize pow(x,c)*x as pow(x,c+1). */
8526 if (fcode0 == BUILT_IN_POW
8527 || fcode0 == BUILT_IN_POWF
8528 || fcode0 == BUILT_IN_POWL)
8530 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8531 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8532 1)));
8533 if (TREE_CODE (arg01) == REAL_CST
8534 && ! TREE_CONSTANT_OVERFLOW (arg01)
8535 && operand_equal_p (arg1, arg00, 0))
8537 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8538 REAL_VALUE_TYPE c;
8539 tree arg, arglist;
8541 c = TREE_REAL_CST (arg01);
8542 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8543 arg = build_real (type, c);
8544 arglist = build_tree_list (NULL_TREE, arg);
8545 arglist = tree_cons (NULL_TREE, arg1, arglist);
8546 return build_function_call_expr (powfn, arglist);
8550 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
8551 if (! optimize_size
8552 && operand_equal_p (arg0, arg1, 0))
8554 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
8556 if (powfn)
8558 tree arg = build_real (type, dconst2);
8559 tree arglist = build_tree_list (NULL_TREE, arg);
8560 arglist = tree_cons (NULL_TREE, arg0, arglist);
8561 return build_function_call_expr (powfn, arglist);
8566 goto associate;
8568 case BIT_IOR_EXPR:
8569 bit_ior:
8570 if (integer_all_onesp (arg1))
8571 return omit_one_operand (type, arg1, arg0);
8572 if (integer_zerop (arg1))
8573 return non_lvalue (fold_convert (type, arg0));
8574 if (operand_equal_p (arg0, arg1, 0))
8575 return non_lvalue (fold_convert (type, arg0));
8577 /* ~X | X is -1. */
8578 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8579 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8581 t1 = build_int_cst (type, -1);
8582 t1 = force_fit_type (t1, 0, false, false);
8583 return omit_one_operand (type, t1, arg1);
8586 /* X | ~X is -1. */
8587 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8588 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8590 t1 = build_int_cst (type, -1);
8591 t1 = force_fit_type (t1, 0, false, false);
8592 return omit_one_operand (type, t1, arg0);
8595 /* Canonicalize (X & C1) | C2. */
8596 if (TREE_CODE (arg0) == BIT_AND_EXPR
8597 && TREE_CODE (arg1) == INTEGER_CST
8598 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8600 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
8601 int width = TYPE_PRECISION (type);
8602 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
8603 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
8604 hi2 = TREE_INT_CST_HIGH (arg1);
8605 lo2 = TREE_INT_CST_LOW (arg1);
8607 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
8608 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
8609 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8611 if (width > HOST_BITS_PER_WIDE_INT)
8613 mhi = (unsigned HOST_WIDE_INT) -1
8614 >> (2 * HOST_BITS_PER_WIDE_INT - width);
8615 mlo = -1;
8617 else
8619 mhi = 0;
8620 mlo = (unsigned HOST_WIDE_INT) -1
8621 >> (HOST_BITS_PER_WIDE_INT - width);
8624 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
8625 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
8626 return fold_build2 (BIT_IOR_EXPR, type,
8627 TREE_OPERAND (arg0, 0), arg1);
8629 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
8630 hi1 &= mhi;
8631 lo1 &= mlo;
8632 if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
8633 return fold_build2 (BIT_IOR_EXPR, type,
8634 fold_build2 (BIT_AND_EXPR, type,
8635 TREE_OPERAND (arg0, 0),
8636 build_int_cst_wide (type,
8637 lo1 & ~lo2,
8638 hi1 & ~hi2)),
8639 arg1);
8642 /* (X & Y) | Y is (X, Y). */
8643 if (TREE_CODE (arg0) == BIT_AND_EXPR
8644 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8645 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8646 /* (X & Y) | X is (Y, X). */
8647 if (TREE_CODE (arg0) == BIT_AND_EXPR
8648 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8649 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8650 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8651 /* X | (X & Y) is (Y, X). */
8652 if (TREE_CODE (arg1) == BIT_AND_EXPR
8653 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8654 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8655 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8656 /* X | (Y & X) is (Y, X). */
8657 if (TREE_CODE (arg1) == BIT_AND_EXPR
8658 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8659 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8660 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8662 t1 = distribute_bit_expr (code, type, arg0, arg1);
8663 if (t1 != NULL_TREE)
8664 return t1;
8666 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
8668 This results in more efficient code for machines without a NAND
8669 instruction. Combine will canonicalize to the first form
8670 which will allow use of NAND instructions provided by the
8671 backend if they exist. */
8672 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8673 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8675 return fold_build1 (BIT_NOT_EXPR, type,
8676 build2 (BIT_AND_EXPR, type,
8677 TREE_OPERAND (arg0, 0),
8678 TREE_OPERAND (arg1, 0)));
8681 /* See if this can be simplified into a rotate first. If that
8682 is unsuccessful continue in the association code. */
8683 goto bit_rotate;
8685 case BIT_XOR_EXPR:
8686 if (integer_zerop (arg1))
8687 return non_lvalue (fold_convert (type, arg0));
8688 if (integer_all_onesp (arg1))
8689 return fold_build1 (BIT_NOT_EXPR, type, arg0);
8690 if (operand_equal_p (arg0, arg1, 0))
8691 return omit_one_operand (type, integer_zero_node, arg0);
8693 /* ~X ^ X is -1. */
8694 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8695 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8697 t1 = build_int_cst (type, -1);
8698 t1 = force_fit_type (t1, 0, false, false);
8699 return omit_one_operand (type, t1, arg1);
8702 /* X ^ ~X is -1. */
8703 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8704 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8706 t1 = build_int_cst (type, -1);
8707 t1 = force_fit_type (t1, 0, false, false);
8708 return omit_one_operand (type, t1, arg0);
8711 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
8712 with a constant, and the two constants have no bits in common,
8713 we should treat this as a BIT_IOR_EXPR since this may produce more
8714 simplifications. */
8715 if (TREE_CODE (arg0) == BIT_AND_EXPR
8716 && TREE_CODE (arg1) == BIT_AND_EXPR
8717 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8718 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8719 && integer_zerop (const_binop (BIT_AND_EXPR,
8720 TREE_OPERAND (arg0, 1),
8721 TREE_OPERAND (arg1, 1), 0)))
8723 code = BIT_IOR_EXPR;
8724 goto bit_ior;
8727 /* (X | Y) ^ X -> Y & ~ X*/
8728 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8729 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8731 tree t2 = TREE_OPERAND (arg0, 1);
8732 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8733 arg1);
8734 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8735 fold_convert (type, t1));
8736 return t1;
8739 /* (Y | X) ^ X -> Y & ~ X*/
8740 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8741 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8743 tree t2 = TREE_OPERAND (arg0, 0);
8744 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
8745 arg1);
8746 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8747 fold_convert (type, t1));
8748 return t1;
8751 /* X ^ (X | Y) -> Y & ~ X*/
8752 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8753 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
8755 tree t2 = TREE_OPERAND (arg1, 1);
8756 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8757 arg0);
8758 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8759 fold_convert (type, t1));
8760 return t1;
8763 /* X ^ (Y | X) -> Y & ~ X*/
8764 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8765 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
8767 tree t2 = TREE_OPERAND (arg1, 0);
8768 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
8769 arg0);
8770 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
8771 fold_convert (type, t1));
8772 return t1;
8775 /* Convert ~X ^ ~Y to X ^ Y. */
8776 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8777 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8778 return fold_build2 (code, type,
8779 fold_convert (type, TREE_OPERAND (arg0, 0)),
8780 fold_convert (type, TREE_OPERAND (arg1, 0)));
8782 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
8783 if (TREE_CODE (arg0) == BIT_AND_EXPR
8784 && integer_onep (TREE_OPERAND (arg0, 1))
8785 && integer_onep (arg1))
8786 return fold_build2 (EQ_EXPR, type, arg0,
8787 build_int_cst (TREE_TYPE (arg0), 0));
8789 /* See if this can be simplified into a rotate first. If that
8790 is unsuccessful continue in the association code. */
8791 goto bit_rotate;
8793 case BIT_AND_EXPR:
8794 if (integer_all_onesp (arg1))
8795 return non_lvalue (fold_convert (type, arg0));
8796 if (integer_zerop (arg1))
8797 return omit_one_operand (type, arg1, arg0);
8798 if (operand_equal_p (arg0, arg1, 0))
8799 return non_lvalue (fold_convert (type, arg0));
8801 /* ~X & X is always zero. */
8802 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8803 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8804 return omit_one_operand (type, integer_zero_node, arg1);
8806 /* X & ~X is always zero. */
8807 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8808 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8809 return omit_one_operand (type, integer_zero_node, arg0);
8811 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
8812 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8813 && TREE_CODE (arg1) == INTEGER_CST
8814 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8815 return fold_build2 (BIT_IOR_EXPR, type,
8816 fold_build2 (BIT_AND_EXPR, type,
8817 TREE_OPERAND (arg0, 0), arg1),
8818 fold_build2 (BIT_AND_EXPR, type,
8819 TREE_OPERAND (arg0, 1), arg1));
8821 /* (X | Y) & Y is (X, Y). */
8822 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8823 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8824 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
8825 /* (X | Y) & X is (Y, X). */
8826 if (TREE_CODE (arg0) == BIT_IOR_EXPR
8827 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8828 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
8829 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
8830 /* X & (X | Y) is (Y, X). */
8831 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8832 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8833 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
8834 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
8835 /* X & (Y | X) is (Y, X). */
8836 if (TREE_CODE (arg1) == BIT_IOR_EXPR
8837 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
8838 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8839 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
8841 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
8842 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8843 && integer_onep (TREE_OPERAND (arg0, 1))
8844 && integer_onep (arg1))
8846 tem = TREE_OPERAND (arg0, 0);
8847 return fold_build2 (EQ_EXPR, type,
8848 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8849 build_int_cst (TREE_TYPE (tem), 1)),
8850 build_int_cst (TREE_TYPE (tem), 0));
8852 /* Fold ~X & 1 as (X & 1) == 0. */
8853 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8854 && integer_onep (arg1))
8856 tem = TREE_OPERAND (arg0, 0);
8857 return fold_build2 (EQ_EXPR, type,
8858 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
8859 build_int_cst (TREE_TYPE (tem), 1)),
8860 build_int_cst (TREE_TYPE (tem), 0));
8863 t1 = distribute_bit_expr (code, type, arg0, arg1);
8864 if (t1 != NULL_TREE)
8865 return t1;
8866 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
8867 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
8868 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8870 unsigned int prec
8871 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
8873 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
8874 && (~TREE_INT_CST_LOW (arg1)
8875 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
8876 return fold_convert (type, TREE_OPERAND (arg0, 0));
8879 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
8881 This results in more efficient code for machines without a NOR
8882 instruction. Combine will canonicalize to the first form
8883 which will allow use of NOR instructions provided by the
8884 backend if they exist. */
8885 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8886 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8888 return fold_build1 (BIT_NOT_EXPR, type,
8889 build2 (BIT_IOR_EXPR, type,
8890 TREE_OPERAND (arg0, 0),
8891 TREE_OPERAND (arg1, 0)));
8894 goto associate;
8896 case RDIV_EXPR:
8897 /* Don't touch a floating-point divide by zero unless the mode
8898 of the constant can represent infinity. */
8899 if (TREE_CODE (arg1) == REAL_CST
8900 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
8901 && real_zerop (arg1))
8902 return NULL_TREE;
8904 /* Optimize A / A to 1.0 if we don't care about
8905 NaNs or Infinities. */
8906 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8907 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
8908 && operand_equal_p (arg0, arg1, 0))
8910 tree r = build_real (TREE_TYPE (arg0), dconst1);
8912 return omit_two_operands (type, r, arg0, arg1);
8915 /* (-A) / (-B) -> A / B */
8916 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8917 return fold_build2 (RDIV_EXPR, type,
8918 TREE_OPERAND (arg0, 0),
8919 negate_expr (arg1));
8920 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8921 return fold_build2 (RDIV_EXPR, type,
8922 negate_expr (arg0),
8923 TREE_OPERAND (arg1, 0));
8925 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
8926 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8927 && real_onep (arg1))
8928 return non_lvalue (fold_convert (type, arg0));
8930 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
8931 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8932 && real_minus_onep (arg1))
8933 return non_lvalue (fold_convert (type, negate_expr (arg0)));
8935 /* If ARG1 is a constant, we can convert this to a multiply by the
8936 reciprocal. This does not have the same rounding properties,
8937 so only do this if -funsafe-math-optimizations. We can actually
8938 always safely do it if ARG1 is a power of two, but it's hard to
8939 tell if it is or not in a portable manner. */
8940 if (TREE_CODE (arg1) == REAL_CST)
8942 if (flag_unsafe_math_optimizations
8943 && 0 != (tem = const_binop (code, build_real (type, dconst1),
8944 arg1, 0)))
8945 return fold_build2 (MULT_EXPR, type, arg0, tem);
8946 /* Find the reciprocal if optimizing and the result is exact. */
8947 if (optimize)
8949 REAL_VALUE_TYPE r;
8950 r = TREE_REAL_CST (arg1);
8951 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
8953 tem = build_real (type, r);
8954 return fold_build2 (MULT_EXPR, type,
8955 fold_convert (type, arg0), tem);
8959 /* Convert A/B/C to A/(B*C). */
8960 if (flag_unsafe_math_optimizations
8961 && TREE_CODE (arg0) == RDIV_EXPR)
8962 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
8963 fold_build2 (MULT_EXPR, type,
8964 TREE_OPERAND (arg0, 1), arg1));
8966 /* Convert A/(B/C) to (A/B)*C. */
8967 if (flag_unsafe_math_optimizations
8968 && TREE_CODE (arg1) == RDIV_EXPR)
8969 return fold_build2 (MULT_EXPR, type,
8970 fold_build2 (RDIV_EXPR, type, arg0,
8971 TREE_OPERAND (arg1, 0)),
8972 TREE_OPERAND (arg1, 1));
8974 /* Convert C1/(X*C2) into (C1/C2)/X. */
8975 if (flag_unsafe_math_optimizations
8976 && TREE_CODE (arg1) == MULT_EXPR
8977 && TREE_CODE (arg0) == REAL_CST
8978 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
8980 tree tem = const_binop (RDIV_EXPR, arg0,
8981 TREE_OPERAND (arg1, 1), 0);
8982 if (tem)
8983 return fold_build2 (RDIV_EXPR, type, tem,
8984 TREE_OPERAND (arg1, 0));
8987 if (flag_unsafe_math_optimizations)
8989 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8990 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8992 /* Optimize sin(x)/cos(x) as tan(x). */
8993 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
8994 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
8995 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
8996 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8997 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8999 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9001 if (tanfn != NULL_TREE)
9002 return build_function_call_expr (tanfn,
9003 TREE_OPERAND (arg0, 1));
9006 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
9007 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
9008 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
9009 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
9010 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9011 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9013 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
9015 if (tanfn != NULL_TREE)
9017 tree tmp = TREE_OPERAND (arg0, 1);
9018 tmp = build_function_call_expr (tanfn, tmp);
9019 return fold_build2 (RDIV_EXPR, type,
9020 build_real (type, dconst1), tmp);
9024 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
9025 NaNs or Infinities. */
9026 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
9027 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
9028 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
9030 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9031 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9033 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9034 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9035 && operand_equal_p (arg00, arg01, 0))
9037 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9039 if (cosfn != NULL_TREE)
9040 return build_function_call_expr (cosfn,
9041 TREE_OPERAND (arg0, 1));
9045 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
9046 NaNs or Infinities. */
9047 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
9048 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
9049 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
9051 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9052 tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9054 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
9055 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
9056 && operand_equal_p (arg00, arg01, 0))
9058 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
9060 if (cosfn != NULL_TREE)
9062 tree tmp = TREE_OPERAND (arg0, 1);
9063 tmp = build_function_call_expr (cosfn, tmp);
9064 return fold_build2 (RDIV_EXPR, type,
9065 build_real (type, dconst1),
9066 tmp);
9071 /* Optimize pow(x,c)/x as pow(x,c-1). */
9072 if (fcode0 == BUILT_IN_POW
9073 || fcode0 == BUILT_IN_POWF
9074 || fcode0 == BUILT_IN_POWL)
9076 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9077 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
9078 if (TREE_CODE (arg01) == REAL_CST
9079 && ! TREE_CONSTANT_OVERFLOW (arg01)
9080 && operand_equal_p (arg1, arg00, 0))
9082 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9083 REAL_VALUE_TYPE c;
9084 tree arg, arglist;
9086 c = TREE_REAL_CST (arg01);
9087 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
9088 arg = build_real (type, c);
9089 arglist = build_tree_list (NULL_TREE, arg);
9090 arglist = tree_cons (NULL_TREE, arg1, arglist);
9091 return build_function_call_expr (powfn, arglist);
9095 /* Optimize x/expN(y) into x*expN(-y). */
9096 if (BUILTIN_EXPONENT_P (fcode1))
9098 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9099 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
9100 tree arglist = build_tree_list (NULL_TREE,
9101 fold_convert (type, arg));
9102 arg1 = build_function_call_expr (expfn, arglist);
9103 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9106 /* Optimize x/pow(y,z) into x*pow(y,-z). */
9107 if (fcode1 == BUILT_IN_POW
9108 || fcode1 == BUILT_IN_POWF
9109 || fcode1 == BUILT_IN_POWL)
9111 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9112 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9113 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
9114 tree neg11 = fold_convert (type, negate_expr (arg11));
9115 tree arglist = tree_cons(NULL_TREE, arg10,
9116 build_tree_list (NULL_TREE, neg11));
9117 arg1 = build_function_call_expr (powfn, arglist);
9118 return fold_build2 (MULT_EXPR, type, arg0, arg1);
9121 return NULL_TREE;
9123 case TRUNC_DIV_EXPR:
9124 case ROUND_DIV_EXPR:
9125 case FLOOR_DIV_EXPR:
9126 case CEIL_DIV_EXPR:
9127 case EXACT_DIV_EXPR:
9128 if (integer_onep (arg1))
9129 return non_lvalue (fold_convert (type, arg0));
9130 if (integer_zerop (arg1))
9131 return NULL_TREE;
9132 /* X / -1 is -X. */
9133 if (!TYPE_UNSIGNED (type)
9134 && TREE_CODE (arg1) == INTEGER_CST
9135 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9136 && TREE_INT_CST_HIGH (arg1) == -1)
9137 return fold_convert (type, negate_expr (arg0));
9139 /* Convert -A / -B to A / B when the type is signed and overflow is
9140 undefined. */
9141 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9142 && TREE_CODE (arg0) == NEGATE_EXPR
9143 && negate_expr_p (arg1))
9144 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9145 negate_expr (arg1));
9146 if (!TYPE_UNSIGNED (type) && !flag_wrapv
9147 && TREE_CODE (arg1) == NEGATE_EXPR
9148 && negate_expr_p (arg0))
9149 return fold_build2 (code, type, negate_expr (arg0),
9150 TREE_OPERAND (arg1, 0));
9152 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
9153 operation, EXACT_DIV_EXPR.
9155 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
9156 At one time others generated faster code, it's not clear if they do
9157 after the last round to changes to the DIV code in expmed.c. */
9158 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
9159 && multiple_of_p (type, arg0, arg1))
9160 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
9162 if (TREE_CODE (arg1) == INTEGER_CST
9163 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9164 return fold_convert (type, tem);
9166 return NULL_TREE;
9168 case CEIL_MOD_EXPR:
9169 case FLOOR_MOD_EXPR:
9170 case ROUND_MOD_EXPR:
9171 case TRUNC_MOD_EXPR:
9172 /* X % 1 is always zero, but be sure to preserve any side
9173 effects in X. */
9174 if (integer_onep (arg1))
9175 return omit_one_operand (type, integer_zero_node, arg0);
9177 /* X % 0, return X % 0 unchanged so that we can get the
9178 proper warnings and errors. */
9179 if (integer_zerop (arg1))
9180 return NULL_TREE;
9182 /* 0 % X is always zero, but be sure to preserve any side
9183 effects in X. Place this after checking for X == 0. */
9184 if (integer_zerop (arg0))
9185 return omit_one_operand (type, integer_zero_node, arg1);
9187 /* X % -1 is zero. */
9188 if (!TYPE_UNSIGNED (type)
9189 && TREE_CODE (arg1) == INTEGER_CST
9190 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
9191 && TREE_INT_CST_HIGH (arg1) == -1)
9192 return omit_one_operand (type, integer_zero_node, arg0);
9194 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
9195 i.e. "X % C" into "X & C2", if X and C are positive. */
9196 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
9197 && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))
9198 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) >= 0)
9200 unsigned HOST_WIDE_INT high, low;
9201 tree mask;
9202 int l;
9204 l = tree_log2 (arg1);
9205 if (l >= HOST_BITS_PER_WIDE_INT)
9207 high = ((unsigned HOST_WIDE_INT) 1
9208 << (l - HOST_BITS_PER_WIDE_INT)) - 1;
9209 low = -1;
9211 else
9213 high = 0;
9214 low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
9217 mask = build_int_cst_wide (type, low, high);
9218 return fold_build2 (BIT_AND_EXPR, type,
9219 fold_convert (type, arg0), mask);
9222 /* X % -C is the same as X % C. */
9223 if (code == TRUNC_MOD_EXPR
9224 && !TYPE_UNSIGNED (type)
9225 && TREE_CODE (arg1) == INTEGER_CST
9226 && !TREE_CONSTANT_OVERFLOW (arg1)
9227 && TREE_INT_CST_HIGH (arg1) < 0
9228 && !flag_trapv
9229 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
9230 && !sign_bit_p (arg1, arg1))
9231 return fold_build2 (code, type, fold_convert (type, arg0),
9232 fold_convert (type, negate_expr (arg1)));
9234 /* X % -Y is the same as X % Y. */
9235 if (code == TRUNC_MOD_EXPR
9236 && !TYPE_UNSIGNED (type)
9237 && TREE_CODE (arg1) == NEGATE_EXPR
9238 && !flag_trapv)
9239 return fold_build2 (code, type, fold_convert (type, arg0),
9240 fold_convert (type, TREE_OPERAND (arg1, 0)));
9242 if (TREE_CODE (arg1) == INTEGER_CST
9243 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
9244 return fold_convert (type, tem);
9246 return NULL_TREE;
9248 case LROTATE_EXPR:
9249 case RROTATE_EXPR:
9250 if (integer_all_onesp (arg0))
9251 return omit_one_operand (type, arg0, arg1);
9252 goto shift;
9254 case RSHIFT_EXPR:
9255 /* Optimize -1 >> x for arithmetic right shifts. */
9256 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
9257 return omit_one_operand (type, arg0, arg1);
9258 /* ... fall through ... */
9260 case LSHIFT_EXPR:
9261 shift:
9262 if (integer_zerop (arg1))
9263 return non_lvalue (fold_convert (type, arg0));
9264 if (integer_zerop (arg0))
9265 return omit_one_operand (type, arg0, arg1);
9267 /* Since negative shift count is not well-defined,
9268 don't try to compute it in the compiler. */
9269 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
9270 return NULL_TREE;
9272 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
9273 if (TREE_CODE (arg0) == code && host_integerp (arg1, false)
9274 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9275 && host_integerp (TREE_OPERAND (arg0, 1), false)
9276 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9278 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
9279 + TREE_INT_CST_LOW (arg1));
9281 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
9282 being well defined. */
9283 if (low >= TYPE_PRECISION (type))
9285 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
9286 low = low % TYPE_PRECISION (type);
9287 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
9288 return build_int_cst (type, 0);
9289 else
9290 low = TYPE_PRECISION (type) - 1;
9293 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9294 build_int_cst (type, low));
9297 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
9298 into x & ((unsigned)-1 >> c) for unsigned types. */
9299 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
9300 || (TYPE_UNSIGNED (type)
9301 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
9302 && host_integerp (arg1, false)
9303 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
9304 && host_integerp (TREE_OPERAND (arg0, 1), false)
9305 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
9307 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
9308 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
9309 tree lshift;
9310 tree arg00;
9312 if (low0 == low1)
9314 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9316 lshift = build_int_cst (type, -1);
9317 lshift = int_const_binop (code, lshift, arg1, 0);
9319 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
9323 /* Rewrite an LROTATE_EXPR by a constant into an
9324 RROTATE_EXPR by a new constant. */
9325 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
9327 tree tem = build_int_cst (NULL_TREE,
9328 GET_MODE_BITSIZE (TYPE_MODE (type)));
9329 tem = fold_convert (TREE_TYPE (arg1), tem);
9330 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
9331 return fold_build2 (RROTATE_EXPR, type, arg0, tem);
9334 /* If we have a rotate of a bit operation with the rotate count and
9335 the second operand of the bit operation both constant,
9336 permute the two operations. */
9337 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9338 && (TREE_CODE (arg0) == BIT_AND_EXPR
9339 || TREE_CODE (arg0) == BIT_IOR_EXPR
9340 || TREE_CODE (arg0) == BIT_XOR_EXPR)
9341 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9342 return fold_build2 (TREE_CODE (arg0), type,
9343 fold_build2 (code, type,
9344 TREE_OPERAND (arg0, 0), arg1),
9345 fold_build2 (code, type,
9346 TREE_OPERAND (arg0, 1), arg1));
9348 /* Two consecutive rotates adding up to the width of the mode can
9349 be ignored. */
9350 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9351 && TREE_CODE (arg0) == RROTATE_EXPR
9352 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9353 && TREE_INT_CST_HIGH (arg1) == 0
9354 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
9355 && ((TREE_INT_CST_LOW (arg1)
9356 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
9357 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
9358 return TREE_OPERAND (arg0, 0);
9360 return NULL_TREE;
9362 case MIN_EXPR:
9363 if (operand_equal_p (arg0, arg1, 0))
9364 return omit_one_operand (type, arg0, arg1);
9365 if (INTEGRAL_TYPE_P (type)
9366 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9367 return omit_one_operand (type, arg1, arg0);
9368 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
9369 if (tem)
9370 return tem;
9371 goto associate;
9373 case MAX_EXPR:
9374 if (operand_equal_p (arg0, arg1, 0))
9375 return omit_one_operand (type, arg0, arg1);
9376 if (INTEGRAL_TYPE_P (type)
9377 && TYPE_MAX_VALUE (type)
9378 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9379 return omit_one_operand (type, arg1, arg0);
9380 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
9381 if (tem)
9382 return tem;
9383 goto associate;
9385 case TRUTH_ANDIF_EXPR:
9386 /* Note that the operands of this must be ints
9387 and their values must be 0 or 1.
9388 ("true" is a fixed value perhaps depending on the language.) */
9389 /* If first arg is constant zero, return it. */
9390 if (integer_zerop (arg0))
9391 return fold_convert (type, arg0);
9392 case TRUTH_AND_EXPR:
9393 /* If either arg is constant true, drop it. */
9394 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9395 return non_lvalue (fold_convert (type, arg1));
9396 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
9397 /* Preserve sequence points. */
9398 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9399 return non_lvalue (fold_convert (type, arg0));
9400 /* If second arg is constant zero, result is zero, but first arg
9401 must be evaluated. */
9402 if (integer_zerop (arg1))
9403 return omit_one_operand (type, arg1, arg0);
9404 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9405 case will be handled here. */
9406 if (integer_zerop (arg0))
9407 return omit_one_operand (type, arg0, arg1);
9409 /* !X && X is always false. */
9410 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9411 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9412 return omit_one_operand (type, integer_zero_node, arg1);
9413 /* X && !X is always false. */
9414 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9415 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9416 return omit_one_operand (type, integer_zero_node, arg0);
9418 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
9419 means A >= Y && A != MAX, but in this case we know that
9420 A < X <= MAX. */
9422 if (!TREE_SIDE_EFFECTS (arg0)
9423 && !TREE_SIDE_EFFECTS (arg1))
9425 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
9426 if (tem && !operand_equal_p (tem, arg0, 0))
9427 return fold_build2 (code, type, tem, arg1);
9429 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
9430 if (tem && !operand_equal_p (tem, arg1, 0))
9431 return fold_build2 (code, type, arg0, tem);
9434 truth_andor:
9435 /* We only do these simplifications if we are optimizing. */
9436 if (!optimize)
9437 return NULL_TREE;
9439 /* Check for things like (A || B) && (A || C). We can convert this
9440 to A || (B && C). Note that either operator can be any of the four
9441 truth and/or operations and the transformation will still be
9442 valid. Also note that we only care about order for the
9443 ANDIF and ORIF operators. If B contains side effects, this
9444 might change the truth-value of A. */
9445 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9446 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9447 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9448 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9449 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9450 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9452 tree a00 = TREE_OPERAND (arg0, 0);
9453 tree a01 = TREE_OPERAND (arg0, 1);
9454 tree a10 = TREE_OPERAND (arg1, 0);
9455 tree a11 = TREE_OPERAND (arg1, 1);
9456 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9457 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9458 && (code == TRUTH_AND_EXPR
9459 || code == TRUTH_OR_EXPR));
9461 if (operand_equal_p (a00, a10, 0))
9462 return fold_build2 (TREE_CODE (arg0), type, a00,
9463 fold_build2 (code, type, a01, a11));
9464 else if (commutative && operand_equal_p (a00, a11, 0))
9465 return fold_build2 (TREE_CODE (arg0), type, a00,
9466 fold_build2 (code, type, a01, a10));
9467 else if (commutative && operand_equal_p (a01, a10, 0))
9468 return fold_build2 (TREE_CODE (arg0), type, a01,
9469 fold_build2 (code, type, a00, a11));
9471 /* This case if tricky because we must either have commutative
9472 operators or else A10 must not have side-effects. */
9474 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9475 && operand_equal_p (a01, a11, 0))
9476 return fold_build2 (TREE_CODE (arg0), type,
9477 fold_build2 (code, type, a00, a10),
9478 a01);
9481 /* See if we can build a range comparison. */
9482 if (0 != (tem = fold_range_test (code, type, op0, op1)))
9483 return tem;
9485 /* Check for the possibility of merging component references. If our
9486 lhs is another similar operation, try to merge its rhs with our
9487 rhs. Then try to merge our lhs and rhs. */
9488 if (TREE_CODE (arg0) == code
9489 && 0 != (tem = fold_truthop (code, type,
9490 TREE_OPERAND (arg0, 1), arg1)))
9491 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9493 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
9494 return tem;
9496 return NULL_TREE;
9498 case TRUTH_ORIF_EXPR:
9499 /* Note that the operands of this must be ints
9500 and their values must be 0 or true.
9501 ("true" is a fixed value perhaps depending on the language.) */
9502 /* If first arg is constant true, return it. */
9503 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9504 return fold_convert (type, arg0);
9505 case TRUTH_OR_EXPR:
9506 /* If either arg is constant zero, drop it. */
9507 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
9508 return non_lvalue (fold_convert (type, arg1));
9509 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
9510 /* Preserve sequence points. */
9511 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
9512 return non_lvalue (fold_convert (type, arg0));
9513 /* If second arg is constant true, result is true, but we must
9514 evaluate first arg. */
9515 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
9516 return omit_one_operand (type, arg1, arg0);
9517 /* Likewise for first arg, but note this only occurs here for
9518 TRUTH_OR_EXPR. */
9519 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
9520 return omit_one_operand (type, arg0, arg1);
9522 /* !X || X is always true. */
9523 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9524 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9525 return omit_one_operand (type, integer_one_node, arg1);
9526 /* X || !X is always true. */
9527 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9528 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9529 return omit_one_operand (type, integer_one_node, arg0);
9531 goto truth_andor;
9533 case TRUTH_XOR_EXPR:
9534 /* If the second arg is constant zero, drop it. */
9535 if (integer_zerop (arg1))
9536 return non_lvalue (fold_convert (type, arg0));
9537 /* If the second arg is constant true, this is a logical inversion. */
9538 if (integer_onep (arg1))
9540 /* Only call invert_truthvalue if operand is a truth value. */
9541 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
9542 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
9543 else
9544 tem = invert_truthvalue (arg0);
9545 return non_lvalue (fold_convert (type, tem));
9547 /* Identical arguments cancel to zero. */
9548 if (operand_equal_p (arg0, arg1, 0))
9549 return omit_one_operand (type, integer_zero_node, arg0);
9551 /* !X ^ X is always true. */
9552 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
9553 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9554 return omit_one_operand (type, integer_one_node, arg1);
9556 /* X ^ !X is always true. */
9557 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
9558 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9559 return omit_one_operand (type, integer_one_node, arg0);
9561 return NULL_TREE;
9563 case EQ_EXPR:
9564 case NE_EXPR:
9565 tem = fold_comparison (code, type, op0, op1);
9566 if (tem != NULL_TREE)
9567 return tem;
9569 /* bool_var != 0 becomes bool_var. */
9570 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9571 && code == NE_EXPR)
9572 return non_lvalue (fold_convert (type, arg0));
9574 /* bool_var == 1 becomes bool_var. */
9575 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9576 && code == EQ_EXPR)
9577 return non_lvalue (fold_convert (type, arg0));
9579 /* bool_var != 1 becomes !bool_var. */
9580 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
9581 && code == NE_EXPR)
9582 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9584 /* bool_var == 0 becomes !bool_var. */
9585 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
9586 && code == EQ_EXPR)
9587 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
9589 /* ~a != C becomes a != ~C where C is a constant. Likewise for ==. */
9590 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9591 && TREE_CODE (arg1) == INTEGER_CST)
9592 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9593 fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
9594 arg1));
9596 /* If this is an equality comparison of the address of a non-weak
9597 object against zero, then we know the result. */
9598 if (TREE_CODE (arg0) == ADDR_EXPR
9599 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9600 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9601 && integer_zerop (arg1))
9602 return constant_boolean_node (code != EQ_EXPR, type);
9604 /* If this is an equality comparison of the address of two non-weak,
9605 unaliased symbols neither of which are extern (since we do not
9606 have access to attributes for externs), then we know the result. */
9607 if (TREE_CODE (arg0) == ADDR_EXPR
9608 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
9609 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
9610 && ! lookup_attribute ("alias",
9611 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
9612 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
9613 && TREE_CODE (arg1) == ADDR_EXPR
9614 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
9615 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
9616 && ! lookup_attribute ("alias",
9617 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
9618 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
9620 /* We know that we're looking at the address of two
9621 non-weak, unaliased, static _DECL nodes.
9623 It is both wasteful and incorrect to call operand_equal_p
9624 to compare the two ADDR_EXPR nodes. It is wasteful in that
9625 all we need to do is test pointer equality for the arguments
9626 to the two ADDR_EXPR nodes. It is incorrect to use
9627 operand_equal_p as that function is NOT equivalent to a
9628 C equality test. It can in fact return false for two
9629 objects which would test as equal using the C equality
9630 operator. */
9631 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
9632 return constant_boolean_node (equal
9633 ? code == EQ_EXPR : code != EQ_EXPR,
9634 type);
9637 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
9638 a MINUS_EXPR of a constant, we can convert it into a comparison with
9639 a revised constant as long as no overflow occurs. */
9640 if (TREE_CODE (arg1) == INTEGER_CST
9641 && (TREE_CODE (arg0) == PLUS_EXPR
9642 || TREE_CODE (arg0) == MINUS_EXPR)
9643 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9644 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9645 ? MINUS_EXPR : PLUS_EXPR,
9646 arg1, TREE_OPERAND (arg0, 1), 0))
9647 && ! TREE_CONSTANT_OVERFLOW (tem))
9648 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9650 /* Similarly for a NEGATE_EXPR. */
9651 if (TREE_CODE (arg0) == NEGATE_EXPR
9652 && TREE_CODE (arg1) == INTEGER_CST
9653 && 0 != (tem = negate_expr (arg1))
9654 && TREE_CODE (tem) == INTEGER_CST
9655 && ! TREE_CONSTANT_OVERFLOW (tem))
9656 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9658 /* If we have X - Y == 0, we can convert that to X == Y and similarly
9659 for !=. Don't do this for ordered comparisons due to overflow. */
9660 if (TREE_CODE (arg0) == MINUS_EXPR
9661 && integer_zerop (arg1))
9662 return fold_build2 (code, type,
9663 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
9665 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
9666 if (TREE_CODE (arg0) == ABS_EXPR
9667 && (integer_zerop (arg1) || real_zerop (arg1)))
9668 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
9670 /* If this is an EQ or NE comparison with zero and ARG0 is
9671 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
9672 two operations, but the latter can be done in one less insn
9673 on machines that have only two-operand insns or on which a
9674 constant cannot be the first operand. */
9675 if (TREE_CODE (arg0) == BIT_AND_EXPR
9676 && integer_zerop (arg1))
9678 tree arg00 = TREE_OPERAND (arg0, 0);
9679 tree arg01 = TREE_OPERAND (arg0, 1);
9680 if (TREE_CODE (arg00) == LSHIFT_EXPR
9681 && integer_onep (TREE_OPERAND (arg00, 0)))
9682 return
9683 fold_build2 (code, type,
9684 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9685 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
9686 arg01, TREE_OPERAND (arg00, 1)),
9687 fold_convert (TREE_TYPE (arg0),
9688 integer_one_node)),
9689 arg1);
9690 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
9691 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
9692 return
9693 fold_build2 (code, type,
9694 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9695 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9696 arg00, TREE_OPERAND (arg01, 1)),
9697 fold_convert (TREE_TYPE (arg0),
9698 integer_one_node)),
9699 arg1);
9702 /* If this is an NE or EQ comparison of zero against the result of a
9703 signed MOD operation whose second operand is a power of 2, make
9704 the MOD operation unsigned since it is simpler and equivalent. */
9705 if (integer_zerop (arg1)
9706 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9707 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9708 || TREE_CODE (arg0) == CEIL_MOD_EXPR
9709 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9710 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9711 && integer_pow2p (TREE_OPERAND (arg0, 1)))
9713 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9714 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
9715 fold_convert (newtype,
9716 TREE_OPERAND (arg0, 0)),
9717 fold_convert (newtype,
9718 TREE_OPERAND (arg0, 1)));
9720 return fold_build2 (code, type, newmod,
9721 fold_convert (newtype, arg1));
9724 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
9725 C1 is a valid shift constant, and C2 is a power of two, i.e.
9726 a single bit. */
9727 if (TREE_CODE (arg0) == BIT_AND_EXPR
9728 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
9729 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
9730 == INTEGER_CST
9731 && integer_pow2p (TREE_OPERAND (arg0, 1))
9732 && integer_zerop (arg1))
9734 tree itype = TREE_TYPE (arg0);
9735 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
9736 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
9738 /* Check for a valid shift count. */
9739 if (TREE_INT_CST_HIGH (arg001) == 0
9740 && TREE_INT_CST_LOW (arg001) < prec)
9742 tree arg01 = TREE_OPERAND (arg0, 1);
9743 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9744 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
9745 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
9746 can be rewritten as (X & (C2 << C1)) != 0. */
9747 if ((log2 + TREE_INT_CST_LOW (arg01)) < prec)
9749 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
9750 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
9751 return fold_build2 (code, type, tem, arg1);
9753 /* Otherwise, for signed (arithmetic) shifts,
9754 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
9755 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
9756 else if (!TYPE_UNSIGNED (itype))
9757 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
9758 arg000, build_int_cst (itype, 0));
9759 /* Otherwise, of unsigned (logical) shifts,
9760 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
9761 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
9762 else
9763 return omit_one_operand (type,
9764 code == EQ_EXPR ? integer_one_node
9765 : integer_zero_node,
9766 arg000);
9770 /* If this is an NE comparison of zero with an AND of one, remove the
9771 comparison since the AND will give the correct value. */
9772 if (code == NE_EXPR
9773 && integer_zerop (arg1)
9774 && TREE_CODE (arg0) == BIT_AND_EXPR
9775 && integer_onep (TREE_OPERAND (arg0, 1)))
9776 return fold_convert (type, arg0);
9778 /* If we have (A & C) == C where C is a power of 2, convert this into
9779 (A & C) != 0. Similarly for NE_EXPR. */
9780 if (TREE_CODE (arg0) == BIT_AND_EXPR
9781 && integer_pow2p (TREE_OPERAND (arg0, 1))
9782 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9783 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9784 arg0, fold_convert (TREE_TYPE (arg0),
9785 integer_zero_node));
9787 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
9788 bit, then fold the expression into A < 0 or A >= 0. */
9789 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
9790 if (tem)
9791 return tem;
9793 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9794 Similarly for NE_EXPR. */
9795 if (TREE_CODE (arg0) == BIT_AND_EXPR
9796 && TREE_CODE (arg1) == INTEGER_CST
9797 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9799 tree notc = fold_build1 (BIT_NOT_EXPR,
9800 TREE_TYPE (TREE_OPERAND (arg0, 1)),
9801 TREE_OPERAND (arg0, 1));
9802 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9803 arg1, notc);
9804 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9805 if (integer_nonzerop (dandnotc))
9806 return omit_one_operand (type, rslt, arg0);
9809 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9810 Similarly for NE_EXPR. */
9811 if (TREE_CODE (arg0) == BIT_IOR_EXPR
9812 && TREE_CODE (arg1) == INTEGER_CST
9813 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9815 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
9816 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9817 TREE_OPERAND (arg0, 1), notd);
9818 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9819 if (integer_nonzerop (candnotd))
9820 return omit_one_operand (type, rslt, arg0);
9823 /* If this is a comparison of a field, we may be able to simplify it. */
9824 if (((TREE_CODE (arg0) == COMPONENT_REF
9825 && lang_hooks.can_use_bit_fields_p ())
9826 || TREE_CODE (arg0) == BIT_FIELD_REF)
9827 /* Handle the constant case even without -O
9828 to make sure the warnings are given. */
9829 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9831 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9832 if (t1)
9833 return t1;
9836 /* Optimize comparisons of strlen vs zero to a compare of the
9837 first character of the string vs zero. To wit,
9838 strlen(ptr) == 0 => *ptr == 0
9839 strlen(ptr) != 0 => *ptr != 0
9840 Other cases should reduce to one of these two (or a constant)
9841 due to the return value of strlen being unsigned. */
9842 if (TREE_CODE (arg0) == CALL_EXPR
9843 && integer_zerop (arg1))
9845 tree fndecl = get_callee_fndecl (arg0);
9846 tree arglist;
9848 if (fndecl
9849 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9850 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9851 && (arglist = TREE_OPERAND (arg0, 1))
9852 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9853 && ! TREE_CHAIN (arglist))
9855 tree iref = build_fold_indirect_ref (TREE_VALUE (arglist));
9856 return fold_build2 (code, type, iref,
9857 build_int_cst (TREE_TYPE (iref), 0));
9861 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
9862 of X. Similarly fold (X >> C) == 0 into X >= 0. */
9863 if (TREE_CODE (arg0) == RSHIFT_EXPR
9864 && integer_zerop (arg1)
9865 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9867 tree arg00 = TREE_OPERAND (arg0, 0);
9868 tree arg01 = TREE_OPERAND (arg0, 1);
9869 tree itype = TREE_TYPE (arg00);
9870 if (TREE_INT_CST_HIGH (arg01) == 0
9871 && TREE_INT_CST_LOW (arg01)
9872 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
9874 if (TYPE_UNSIGNED (itype))
9876 itype = lang_hooks.types.signed_type (itype);
9877 arg00 = fold_convert (itype, arg00);
9879 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
9880 type, arg00, build_int_cst (itype, 0));
9884 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
9885 if (integer_zerop (arg1)
9886 && TREE_CODE (arg0) == BIT_XOR_EXPR)
9887 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9888 TREE_OPERAND (arg0, 1));
9890 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
9891 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9892 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9893 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9894 build_int_cst (TREE_TYPE (arg1), 0));
9895 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
9896 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9897 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9898 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9899 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
9900 build_int_cst (TREE_TYPE (arg1), 0));
9902 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
9903 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9904 && TREE_CODE (arg1) == INTEGER_CST
9905 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9906 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
9907 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
9908 TREE_OPERAND (arg0, 1), arg1));
9910 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
9911 (X & C) == 0 when C is a single bit. */
9912 if (TREE_CODE (arg0) == BIT_AND_EXPR
9913 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
9914 && integer_zerop (arg1)
9915 && integer_pow2p (TREE_OPERAND (arg0, 1)))
9917 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9918 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
9919 TREE_OPERAND (arg0, 1));
9920 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
9921 type, tem, arg1);
9924 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
9925 constant C is a power of two, i.e. a single bit. */
9926 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9927 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
9928 && integer_zerop (arg1)
9929 && integer_pow2p (TREE_OPERAND (arg0, 1))
9930 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
9931 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
9933 tree arg00 = TREE_OPERAND (arg0, 0);
9934 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9935 arg00, build_int_cst (TREE_TYPE (arg00), 0));
9938 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
9939 when is C is a power of two, i.e. a single bit. */
9940 if (TREE_CODE (arg0) == BIT_AND_EXPR
9941 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
9942 && integer_zerop (arg1)
9943 && integer_pow2p (TREE_OPERAND (arg0, 1))
9944 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
9945 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
9947 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9948 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
9949 arg000, TREE_OPERAND (arg0, 1));
9950 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9951 tem, build_int_cst (TREE_TYPE (tem), 0));
9954 if (integer_zerop (arg1)
9955 && tree_expr_nonzero_p (arg0))
9957 tree res = constant_boolean_node (code==NE_EXPR, type);
9958 return omit_one_operand (type, res, arg0);
9960 return NULL_TREE;
9962 case LT_EXPR:
9963 case GT_EXPR:
9964 case LE_EXPR:
9965 case GE_EXPR:
9966 tem = fold_comparison (code, type, op0, op1);
9967 if (tem != NULL_TREE)
9968 return tem;
9970 /* Transform comparisons of the form X +- C CMP X. */
9971 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9972 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9973 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9974 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
9975 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9976 && !TYPE_UNSIGNED (TREE_TYPE (arg1))
9977 && !(flag_wrapv || flag_trapv))))
9979 tree arg01 = TREE_OPERAND (arg0, 1);
9980 enum tree_code code0 = TREE_CODE (arg0);
9981 int is_positive;
9983 if (TREE_CODE (arg01) == REAL_CST)
9984 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
9985 else
9986 is_positive = tree_int_cst_sgn (arg01);
9988 /* (X - c) > X becomes false. */
9989 if (code == GT_EXPR
9990 && ((code0 == MINUS_EXPR && is_positive >= 0)
9991 || (code0 == PLUS_EXPR && is_positive <= 0)))
9992 return constant_boolean_node (0, type);
9994 /* Likewise (X + c) < X becomes false. */
9995 if (code == LT_EXPR
9996 && ((code0 == PLUS_EXPR && is_positive >= 0)
9997 || (code0 == MINUS_EXPR && is_positive <= 0)))
9998 return constant_boolean_node (0, type);
10000 /* Convert (X - c) <= X to true. */
10001 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10002 && code == LE_EXPR
10003 && ((code0 == MINUS_EXPR && is_positive >= 0)
10004 || (code0 == PLUS_EXPR && is_positive <= 0)))
10005 return constant_boolean_node (1, type);
10007 /* Convert (X + c) >= X to true. */
10008 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
10009 && code == GE_EXPR
10010 && ((code0 == PLUS_EXPR && is_positive >= 0)
10011 || (code0 == MINUS_EXPR && is_positive <= 0)))
10012 return constant_boolean_node (1, type);
10014 if (TREE_CODE (arg01) == INTEGER_CST)
10016 /* Convert X + c > X and X - c < X to true for integers. */
10017 if (code == GT_EXPR
10018 && ((code0 == PLUS_EXPR && is_positive > 0)
10019 || (code0 == MINUS_EXPR && is_positive < 0)))
10020 return constant_boolean_node (1, type);
10022 if (code == LT_EXPR
10023 && ((code0 == MINUS_EXPR && is_positive > 0)
10024 || (code0 == PLUS_EXPR && is_positive < 0)))
10025 return constant_boolean_node (1, type);
10027 /* Convert X + c <= X and X - c >= X to false for integers. */
10028 if (code == LE_EXPR
10029 && ((code0 == PLUS_EXPR && is_positive > 0)
10030 || (code0 == MINUS_EXPR && is_positive < 0)))
10031 return constant_boolean_node (0, type);
10033 if (code == GE_EXPR
10034 && ((code0 == MINUS_EXPR && is_positive > 0)
10035 || (code0 == PLUS_EXPR && is_positive < 0)))
10036 return constant_boolean_node (0, type);
10040 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
10041 This transformation affects the cases which are handled in later
10042 optimizations involving comparisons with non-negative constants. */
10043 if (TREE_CODE (arg1) == INTEGER_CST
10044 && TREE_CODE (arg0) != INTEGER_CST
10045 && tree_int_cst_sgn (arg1) > 0)
10047 if (code == GE_EXPR)
10049 arg1 = const_binop (MINUS_EXPR, arg1,
10050 build_int_cst (TREE_TYPE (arg1), 1), 0);
10051 return fold_build2 (GT_EXPR, type, arg0,
10052 fold_convert (TREE_TYPE (arg0), arg1));
10054 if (code == LT_EXPR)
10056 arg1 = const_binop (MINUS_EXPR, arg1,
10057 build_int_cst (TREE_TYPE (arg1), 1), 0);
10058 return fold_build2 (LE_EXPR, type, arg0,
10059 fold_convert (TREE_TYPE (arg0), arg1));
10063 /* Comparisons with the highest or lowest possible integer of
10064 the specified size will have known values. */
10066 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
10068 if (TREE_CODE (arg1) == INTEGER_CST
10069 && ! TREE_CONSTANT_OVERFLOW (arg1)
10070 && width <= 2 * HOST_BITS_PER_WIDE_INT
10071 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10072 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10074 HOST_WIDE_INT signed_max_hi;
10075 unsigned HOST_WIDE_INT signed_max_lo;
10076 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
10078 if (width <= HOST_BITS_PER_WIDE_INT)
10080 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10081 - 1;
10082 signed_max_hi = 0;
10083 max_hi = 0;
10085 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10087 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10088 min_lo = 0;
10089 min_hi = 0;
10091 else
10093 max_lo = signed_max_lo;
10094 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10095 min_hi = -1;
10098 else
10100 width -= HOST_BITS_PER_WIDE_INT;
10101 signed_max_lo = -1;
10102 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
10103 - 1;
10104 max_lo = -1;
10105 min_lo = 0;
10107 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
10109 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10110 min_hi = 0;
10112 else
10114 max_hi = signed_max_hi;
10115 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10119 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
10120 && TREE_INT_CST_LOW (arg1) == max_lo)
10121 switch (code)
10123 case GT_EXPR:
10124 return omit_one_operand (type, integer_zero_node, arg0);
10126 case GE_EXPR:
10127 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10129 case LE_EXPR:
10130 return omit_one_operand (type, integer_one_node, arg0);
10132 case LT_EXPR:
10133 return fold_build2 (NE_EXPR, type, arg0, arg1);
10135 /* The GE_EXPR and LT_EXPR cases above are not normally
10136 reached because of previous transformations. */
10138 default:
10139 break;
10141 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10142 == max_hi
10143 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
10144 switch (code)
10146 case GT_EXPR:
10147 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10148 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10149 case LE_EXPR:
10150 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
10151 return fold_build2 (NE_EXPR, type, arg0, arg1);
10152 default:
10153 break;
10155 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10156 == min_hi
10157 && TREE_INT_CST_LOW (arg1) == min_lo)
10158 switch (code)
10160 case LT_EXPR:
10161 return omit_one_operand (type, integer_zero_node, arg0);
10163 case LE_EXPR:
10164 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10166 case GE_EXPR:
10167 return omit_one_operand (type, integer_one_node, arg0);
10169 case GT_EXPR:
10170 return fold_build2 (NE_EXPR, type, op0, op1);
10172 default:
10173 break;
10175 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
10176 == min_hi
10177 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
10178 switch (code)
10180 case GE_EXPR:
10181 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10182 return fold_build2 (NE_EXPR, type, arg0, arg1);
10183 case LT_EXPR:
10184 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
10185 return fold_build2 (EQ_EXPR, type, arg0, arg1);
10186 default:
10187 break;
10190 else if (!in_gimple_form
10191 && TREE_INT_CST_HIGH (arg1) == signed_max_hi
10192 && TREE_INT_CST_LOW (arg1) == signed_max_lo
10193 && TYPE_UNSIGNED (TREE_TYPE (arg1))
10194 /* signed_type does not work on pointer types. */
10195 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
10197 /* The following case also applies to X < signed_max+1
10198 and X >= signed_max+1 because previous transformations. */
10199 if (code == LE_EXPR || code == GT_EXPR)
10201 tree st0, st1;
10202 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
10203 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
10204 return fold_build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
10205 type, fold_convert (st0, arg0),
10206 build_int_cst (st1, 0));
10212 /* If we are comparing an ABS_EXPR with a constant, we can
10213 convert all the cases into explicit comparisons, but they may
10214 well not be faster than doing the ABS and one comparison.
10215 But ABS (X) <= C is a range comparison, which becomes a subtraction
10216 and a comparison, and is probably faster. */
10217 if (code == LE_EXPR
10218 && TREE_CODE (arg1) == INTEGER_CST
10219 && TREE_CODE (arg0) == ABS_EXPR
10220 && ! TREE_SIDE_EFFECTS (arg0)
10221 && (0 != (tem = negate_expr (arg1)))
10222 && TREE_CODE (tem) == INTEGER_CST
10223 && ! TREE_CONSTANT_OVERFLOW (tem))
10224 return fold_build2 (TRUTH_ANDIF_EXPR, type,
10225 build2 (GE_EXPR, type,
10226 TREE_OPERAND (arg0, 0), tem),
10227 build2 (LE_EXPR, type,
10228 TREE_OPERAND (arg0, 0), arg1));
10230 /* Convert ABS_EXPR<x> >= 0 to true. */
10231 if (code == GE_EXPR
10232 && tree_expr_nonnegative_p (arg0)
10233 && (integer_zerop (arg1)
10234 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10235 && real_zerop (arg1))))
10236 return omit_one_operand (type, integer_one_node, arg0);
10238 /* Convert ABS_EXPR<x> < 0 to false. */
10239 if (code == LT_EXPR
10240 && tree_expr_nonnegative_p (arg0)
10241 && (integer_zerop (arg1) || real_zerop (arg1)))
10242 return omit_one_operand (type, integer_zero_node, arg0);
10244 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
10245 and similarly for >= into !=. */
10246 if ((code == LT_EXPR || code == GE_EXPR)
10247 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10248 && TREE_CODE (arg1) == LSHIFT_EXPR
10249 && integer_onep (TREE_OPERAND (arg1, 0)))
10250 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10251 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10252 TREE_OPERAND (arg1, 1)),
10253 build_int_cst (TREE_TYPE (arg0), 0));
10255 if ((code == LT_EXPR || code == GE_EXPR)
10256 && TYPE_UNSIGNED (TREE_TYPE (arg0))
10257 && (TREE_CODE (arg1) == NOP_EXPR
10258 || TREE_CODE (arg1) == CONVERT_EXPR)
10259 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
10260 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
10261 return
10262 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
10263 fold_convert (TREE_TYPE (arg0),
10264 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
10265 TREE_OPERAND (TREE_OPERAND (arg1, 0),
10266 1))),
10267 build_int_cst (TREE_TYPE (arg0), 0));
10269 return NULL_TREE;
10271 case UNORDERED_EXPR:
10272 case ORDERED_EXPR:
10273 case UNLT_EXPR:
10274 case UNLE_EXPR:
10275 case UNGT_EXPR:
10276 case UNGE_EXPR:
10277 case UNEQ_EXPR:
10278 case LTGT_EXPR:
10279 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10281 t1 = fold_relational_const (code, type, arg0, arg1);
10282 if (t1 != NULL_TREE)
10283 return t1;
10286 /* If the first operand is NaN, the result is constant. */
10287 if (TREE_CODE (arg0) == REAL_CST
10288 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
10289 && (code != LTGT_EXPR || ! flag_trapping_math))
10291 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10292 ? integer_zero_node
10293 : integer_one_node;
10294 return omit_one_operand (type, t1, arg1);
10297 /* If the second operand is NaN, the result is constant. */
10298 if (TREE_CODE (arg1) == REAL_CST
10299 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
10300 && (code != LTGT_EXPR || ! flag_trapping_math))
10302 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
10303 ? integer_zero_node
10304 : integer_one_node;
10305 return omit_one_operand (type, t1, arg0);
10308 /* Simplify unordered comparison of something with itself. */
10309 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
10310 && operand_equal_p (arg0, arg1, 0))
10311 return constant_boolean_node (1, type);
10313 if (code == LTGT_EXPR
10314 && !flag_trapping_math
10315 && operand_equal_p (arg0, arg1, 0))
10316 return constant_boolean_node (0, type);
10318 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
10320 tree targ0 = strip_float_extensions (arg0);
10321 tree targ1 = strip_float_extensions (arg1);
10322 tree newtype = TREE_TYPE (targ0);
10324 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
10325 newtype = TREE_TYPE (targ1);
10327 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
10328 return fold_build2 (code, type, fold_convert (newtype, targ0),
10329 fold_convert (newtype, targ1));
10332 return NULL_TREE;
10334 case COMPOUND_EXPR:
10335 /* When pedantic, a compound expression can be neither an lvalue
10336 nor an integer constant expression. */
10337 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
10338 return NULL_TREE;
10339 /* Don't let (0, 0) be null pointer constant. */
10340 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
10341 : fold_convert (type, arg1);
10342 return pedantic_non_lvalue (tem);
10344 case COMPLEX_EXPR:
10345 if ((TREE_CODE (arg0) == REAL_CST
10346 && TREE_CODE (arg1) == REAL_CST)
10347 || (TREE_CODE (arg0) == INTEGER_CST
10348 && TREE_CODE (arg1) == INTEGER_CST))
10349 return build_complex (type, arg0, arg1);
10350 return NULL_TREE;
10352 case ASSERT_EXPR:
10353 /* An ASSERT_EXPR should never be passed to fold_binary. */
10354 gcc_unreachable ();
10356 default:
10357 return NULL_TREE;
10358 } /* switch (code) */
10361 /* Callback for walk_tree, looking for LABEL_EXPR.
10362 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
10363 Do not check the sub-tree of GOTO_EXPR. */
10365 static tree
10366 contains_label_1 (tree *tp,
10367 int *walk_subtrees,
10368 void *data ATTRIBUTE_UNUSED)
10370 switch (TREE_CODE (*tp))
10372 case LABEL_EXPR:
10373 return *tp;
10374 case GOTO_EXPR:
10375 *walk_subtrees = 0;
10376 /* no break */
10377 default:
10378 return NULL_TREE;
10382 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
10383 accessible from outside the sub-tree. Returns NULL_TREE if no
10384 addressable label is found. */
10386 static bool
10387 contains_label_p (tree st)
10389 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
10392 /* Fold a ternary expression of code CODE and type TYPE with operands
10393 OP0, OP1, and OP2. Return the folded expression if folding is
10394 successful. Otherwise, return NULL_TREE. */
10396 tree
10397 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
10399 tree tem;
10400 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
10401 enum tree_code_class kind = TREE_CODE_CLASS (code);
10403 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10404 && TREE_CODE_LENGTH (code) == 3);
10406 /* Strip any conversions that don't change the mode. This is safe
10407 for every expression, except for a comparison expression because
10408 its signedness is derived from its operands. So, in the latter
10409 case, only strip conversions that don't change the signedness.
10411 Note that this is done as an internal manipulation within the
10412 constant folder, in order to find the simplest representation of
10413 the arguments so that their form can be studied. In any cases,
10414 the appropriate type conversions should be put back in the tree
10415 that will get out of the constant folder. */
10416 if (op0)
10418 arg0 = op0;
10419 STRIP_NOPS (arg0);
10422 if (op1)
10424 arg1 = op1;
10425 STRIP_NOPS (arg1);
10428 switch (code)
10430 case COMPONENT_REF:
10431 if (TREE_CODE (arg0) == CONSTRUCTOR
10432 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
10434 unsigned HOST_WIDE_INT idx;
10435 tree field, value;
10436 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
10437 if (field == arg1)
10438 return value;
10440 return NULL_TREE;
10442 case COND_EXPR:
10443 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
10444 so all simple results must be passed through pedantic_non_lvalue. */
10445 if (TREE_CODE (arg0) == INTEGER_CST)
10447 tree unused_op = integer_zerop (arg0) ? op1 : op2;
10448 tem = integer_zerop (arg0) ? op2 : op1;
10449 /* Only optimize constant conditions when the selected branch
10450 has the same type as the COND_EXPR. This avoids optimizing
10451 away "c ? x : throw", where the throw has a void type.
10452 Avoid throwing away that operand which contains label. */
10453 if ((!TREE_SIDE_EFFECTS (unused_op)
10454 || !contains_label_p (unused_op))
10455 && (! VOID_TYPE_P (TREE_TYPE (tem))
10456 || VOID_TYPE_P (type)))
10457 return pedantic_non_lvalue (tem);
10458 return NULL_TREE;
10460 if (operand_equal_p (arg1, op2, 0))
10461 return pedantic_omit_one_operand (type, arg1, arg0);
10463 /* If we have A op B ? A : C, we may be able to convert this to a
10464 simpler expression, depending on the operation and the values
10465 of B and C. Signed zeros prevent all of these transformations,
10466 for reasons given above each one.
10468 Also try swapping the arguments and inverting the conditional. */
10469 if (COMPARISON_CLASS_P (arg0)
10470 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10471 arg1, TREE_OPERAND (arg0, 1))
10472 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
10474 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
10475 if (tem)
10476 return tem;
10479 if (COMPARISON_CLASS_P (arg0)
10480 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10481 op2,
10482 TREE_OPERAND (arg0, 1))
10483 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
10485 tem = invert_truthvalue (arg0);
10486 if (COMPARISON_CLASS_P (tem))
10488 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
10489 if (tem)
10490 return tem;
10494 /* If the second operand is simpler than the third, swap them
10495 since that produces better jump optimization results. */
10496 if (truth_value_p (TREE_CODE (arg0))
10497 && tree_swap_operands_p (op1, op2, false))
10499 /* See if this can be inverted. If it can't, possibly because
10500 it was a floating-point inequality comparison, don't do
10501 anything. */
10502 tem = invert_truthvalue (arg0);
10504 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10505 return fold_build3 (code, type, tem, op2, op1);
10508 /* Convert A ? 1 : 0 to simply A. */
10509 if (integer_onep (op1)
10510 && integer_zerop (op2)
10511 /* If we try to convert OP0 to our type, the
10512 call to fold will try to move the conversion inside
10513 a COND, which will recurse. In that case, the COND_EXPR
10514 is probably the best choice, so leave it alone. */
10515 && type == TREE_TYPE (arg0))
10516 return pedantic_non_lvalue (arg0);
10518 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
10519 over COND_EXPR in cases such as floating point comparisons. */
10520 if (integer_zerop (op1)
10521 && integer_onep (op2)
10522 && truth_value_p (TREE_CODE (arg0)))
10523 return pedantic_non_lvalue (fold_convert (type,
10524 invert_truthvalue (arg0)));
10526 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
10527 if (TREE_CODE (arg0) == LT_EXPR
10528 && integer_zerop (TREE_OPERAND (arg0, 1))
10529 && integer_zerop (op2)
10530 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
10531 return fold_convert (type, fold_build2 (BIT_AND_EXPR,
10532 TREE_TYPE (tem), tem, arg1));
10534 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
10535 already handled above. */
10536 if (TREE_CODE (arg0) == BIT_AND_EXPR
10537 && integer_onep (TREE_OPERAND (arg0, 1))
10538 && integer_zerop (op2)
10539 && integer_pow2p (arg1))
10541 tree tem = TREE_OPERAND (arg0, 0);
10542 STRIP_NOPS (tem);
10543 if (TREE_CODE (tem) == RSHIFT_EXPR
10544 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
10545 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
10546 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
10547 return fold_build2 (BIT_AND_EXPR, type,
10548 TREE_OPERAND (tem, 0), arg1);
10551 /* A & N ? N : 0 is simply A & N if N is a power of two. This
10552 is probably obsolete because the first operand should be a
10553 truth value (that's why we have the two cases above), but let's
10554 leave it in until we can confirm this for all front-ends. */
10555 if (integer_zerop (op2)
10556 && TREE_CODE (arg0) == NE_EXPR
10557 && integer_zerop (TREE_OPERAND (arg0, 1))
10558 && integer_pow2p (arg1)
10559 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10560 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10561 arg1, OEP_ONLY_CONST))
10562 return pedantic_non_lvalue (fold_convert (type,
10563 TREE_OPERAND (arg0, 0)));
10565 /* Convert A ? B : 0 into A && B if A and B are truth values. */
10566 if (integer_zerop (op2)
10567 && truth_value_p (TREE_CODE (arg0))
10568 && truth_value_p (TREE_CODE (arg1)))
10569 return fold_build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1);
10571 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
10572 if (integer_onep (op2)
10573 && truth_value_p (TREE_CODE (arg0))
10574 && truth_value_p (TREE_CODE (arg1)))
10576 /* Only perform transformation if ARG0 is easily inverted. */
10577 tem = invert_truthvalue (arg0);
10578 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10579 return fold_build2 (TRUTH_ORIF_EXPR, type, tem, arg1);
10582 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
10583 if (integer_zerop (arg1)
10584 && truth_value_p (TREE_CODE (arg0))
10585 && truth_value_p (TREE_CODE (op2)))
10587 /* Only perform transformation if ARG0 is easily inverted. */
10588 tem = invert_truthvalue (arg0);
10589 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10590 return fold_build2 (TRUTH_ANDIF_EXPR, type, tem, op2);
10593 /* Convert A ? 1 : B into A || B if A and B are truth values. */
10594 if (integer_onep (arg1)
10595 && truth_value_p (TREE_CODE (arg0))
10596 && truth_value_p (TREE_CODE (op2)))
10597 return fold_build2 (TRUTH_ORIF_EXPR, type, arg0, op2);
10599 return NULL_TREE;
10601 case CALL_EXPR:
10602 /* Check for a built-in function. */
10603 if (TREE_CODE (op0) == ADDR_EXPR
10604 && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
10605 && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
10606 return fold_builtin (TREE_OPERAND (op0, 0), op1, false);
10607 return NULL_TREE;
10609 case BIT_FIELD_REF:
10610 if (TREE_CODE (arg0) == VECTOR_CST
10611 && type == TREE_TYPE (TREE_TYPE (arg0))
10612 && host_integerp (arg1, 1)
10613 && host_integerp (op2, 1))
10615 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
10616 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
10618 if (width != 0
10619 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
10620 && (idx % width) == 0
10621 && (idx = idx / width)
10622 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
10624 tree elements = TREE_VECTOR_CST_ELTS (arg0);
10625 while (idx-- > 0 && elements)
10626 elements = TREE_CHAIN (elements);
10627 if (elements)
10628 return TREE_VALUE (elements);
10629 else
10630 return fold_convert (type, integer_zero_node);
10633 return NULL_TREE;
10635 default:
10636 return NULL_TREE;
10637 } /* switch (code) */
10640 /* Perform constant folding and related simplification of EXPR.
10641 The related simplifications include x*1 => x, x*0 => 0, etc.,
10642 and application of the associative law.
10643 NOP_EXPR conversions may be removed freely (as long as we
10644 are careful not to change the type of the overall expression).
10645 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
10646 but we can constant-fold them if they have constant operands. */
10648 #ifdef ENABLE_FOLD_CHECKING
10649 # define fold(x) fold_1 (x)
10650 static tree fold_1 (tree);
10651 static
10652 #endif
10653 tree
10654 fold (tree expr)
10656 const tree t = expr;
10657 enum tree_code code = TREE_CODE (t);
10658 enum tree_code_class kind = TREE_CODE_CLASS (code);
10659 tree tem;
10661 /* Return right away if a constant. */
10662 if (kind == tcc_constant)
10663 return t;
10665 if (IS_EXPR_CODE_CLASS (kind))
10667 tree type = TREE_TYPE (t);
10668 tree op0, op1, op2;
10670 switch (TREE_CODE_LENGTH (code))
10672 case 1:
10673 op0 = TREE_OPERAND (t, 0);
10674 tem = fold_unary (code, type, op0);
10675 return tem ? tem : expr;
10676 case 2:
10677 op0 = TREE_OPERAND (t, 0);
10678 op1 = TREE_OPERAND (t, 1);
10679 tem = fold_binary (code, type, op0, op1);
10680 return tem ? tem : expr;
10681 case 3:
10682 op0 = TREE_OPERAND (t, 0);
10683 op1 = TREE_OPERAND (t, 1);
10684 op2 = TREE_OPERAND (t, 2);
10685 tem = fold_ternary (code, type, op0, op1, op2);
10686 return tem ? tem : expr;
10687 default:
10688 break;
10692 switch (code)
10694 case CONST_DECL:
10695 return fold (DECL_INITIAL (t));
10697 default:
10698 return t;
10699 } /* switch (code) */
10702 #ifdef ENABLE_FOLD_CHECKING
10703 #undef fold
10705 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
10706 static void fold_check_failed (tree, tree);
10707 void print_fold_checksum (tree);
10709 /* When --enable-checking=fold, compute a digest of expr before
10710 and after actual fold call to see if fold did not accidentally
10711 change original expr. */
10713 tree
10714 fold (tree expr)
10716 tree ret;
10717 struct md5_ctx ctx;
10718 unsigned char checksum_before[16], checksum_after[16];
10719 htab_t ht;
10721 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10722 md5_init_ctx (&ctx);
10723 fold_checksum_tree (expr, &ctx, ht);
10724 md5_finish_ctx (&ctx, checksum_before);
10725 htab_empty (ht);
10727 ret = fold_1 (expr);
10729 md5_init_ctx (&ctx);
10730 fold_checksum_tree (expr, &ctx, ht);
10731 md5_finish_ctx (&ctx, checksum_after);
10732 htab_delete (ht);
10734 if (memcmp (checksum_before, checksum_after, 16))
10735 fold_check_failed (expr, ret);
10737 return ret;
10740 void
10741 print_fold_checksum (tree expr)
10743 struct md5_ctx ctx;
10744 unsigned char checksum[16], cnt;
10745 htab_t ht;
10747 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10748 md5_init_ctx (&ctx);
10749 fold_checksum_tree (expr, &ctx, ht);
10750 md5_finish_ctx (&ctx, checksum);
10751 htab_delete (ht);
10752 for (cnt = 0; cnt < 16; ++cnt)
10753 fprintf (stderr, "%02x", checksum[cnt]);
10754 putc ('\n', stderr);
10757 static void
10758 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
10760 internal_error ("fold check: original tree changed by fold");
10763 static void
10764 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
10766 void **slot;
10767 enum tree_code code;
10768 struct tree_function_decl buf;
10769 int i, len;
10771 recursive_label:
10773 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
10774 <= sizeof (struct tree_function_decl))
10775 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
10776 if (expr == NULL)
10777 return;
10778 slot = htab_find_slot (ht, expr, INSERT);
10779 if (*slot != NULL)
10780 return;
10781 *slot = expr;
10782 code = TREE_CODE (expr);
10783 if (TREE_CODE_CLASS (code) == tcc_declaration
10784 && DECL_ASSEMBLER_NAME_SET_P (expr))
10786 /* Allow DECL_ASSEMBLER_NAME to be modified. */
10787 memcpy ((char *) &buf, expr, tree_size (expr));
10788 expr = (tree) &buf;
10789 SET_DECL_ASSEMBLER_NAME (expr, NULL);
10791 else if (TREE_CODE_CLASS (code) == tcc_type
10792 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
10793 || TYPE_CACHED_VALUES_P (expr)
10794 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
10796 /* Allow these fields to be modified. */
10797 memcpy ((char *) &buf, expr, tree_size (expr));
10798 expr = (tree) &buf;
10799 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
10800 TYPE_POINTER_TO (expr) = NULL;
10801 TYPE_REFERENCE_TO (expr) = NULL;
10802 if (TYPE_CACHED_VALUES_P (expr))
10804 TYPE_CACHED_VALUES_P (expr) = 0;
10805 TYPE_CACHED_VALUES (expr) = NULL;
10808 md5_process_bytes (expr, tree_size (expr), ctx);
10809 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
10810 if (TREE_CODE_CLASS (code) != tcc_type
10811 && TREE_CODE_CLASS (code) != tcc_declaration
10812 && code != TREE_LIST)
10813 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
10814 switch (TREE_CODE_CLASS (code))
10816 case tcc_constant:
10817 switch (code)
10819 case STRING_CST:
10820 md5_process_bytes (TREE_STRING_POINTER (expr),
10821 TREE_STRING_LENGTH (expr), ctx);
10822 break;
10823 case COMPLEX_CST:
10824 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
10825 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
10826 break;
10827 case VECTOR_CST:
10828 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
10829 break;
10830 default:
10831 break;
10833 break;
10834 case tcc_exceptional:
10835 switch (code)
10837 case TREE_LIST:
10838 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
10839 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
10840 expr = TREE_CHAIN (expr);
10841 goto recursive_label;
10842 break;
10843 case TREE_VEC:
10844 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
10845 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
10846 break;
10847 default:
10848 break;
10850 break;
10851 case tcc_expression:
10852 case tcc_reference:
10853 case tcc_comparison:
10854 case tcc_unary:
10855 case tcc_binary:
10856 case tcc_statement:
10857 len = TREE_CODE_LENGTH (code);
10858 for (i = 0; i < len; ++i)
10859 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
10860 break;
10861 case tcc_declaration:
10862 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
10863 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
10864 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
10866 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
10867 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
10868 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
10869 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
10870 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
10872 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
10873 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
10875 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
10877 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
10878 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
10879 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
10881 break;
10882 case tcc_type:
10883 if (TREE_CODE (expr) == ENUMERAL_TYPE)
10884 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
10885 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
10886 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
10887 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
10888 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
10889 if (INTEGRAL_TYPE_P (expr)
10890 || SCALAR_FLOAT_TYPE_P (expr))
10892 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
10893 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
10895 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
10896 if (TREE_CODE (expr) == RECORD_TYPE
10897 || TREE_CODE (expr) == UNION_TYPE
10898 || TREE_CODE (expr) == QUAL_UNION_TYPE)
10899 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
10900 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
10901 break;
10902 default:
10903 break;
10907 #endif
10909 /* Fold a unary tree expression with code CODE of type TYPE with an
10910 operand OP0. Return a folded expression if successful. Otherwise,
10911 return a tree expression with code CODE of type TYPE with an
10912 operand OP0. */
10914 tree
10915 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
10917 tree tem;
10918 #ifdef ENABLE_FOLD_CHECKING
10919 unsigned char checksum_before[16], checksum_after[16];
10920 struct md5_ctx ctx;
10921 htab_t ht;
10923 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10924 md5_init_ctx (&ctx);
10925 fold_checksum_tree (op0, &ctx, ht);
10926 md5_finish_ctx (&ctx, checksum_before);
10927 htab_empty (ht);
10928 #endif
10930 tem = fold_unary (code, type, op0);
10931 if (!tem)
10932 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
10934 #ifdef ENABLE_FOLD_CHECKING
10935 md5_init_ctx (&ctx);
10936 fold_checksum_tree (op0, &ctx, ht);
10937 md5_finish_ctx (&ctx, checksum_after);
10938 htab_delete (ht);
10940 if (memcmp (checksum_before, checksum_after, 16))
10941 fold_check_failed (op0, tem);
10942 #endif
10943 return tem;
10946 /* Fold a binary tree expression with code CODE of type TYPE with
10947 operands OP0 and OP1. Return a folded expression if successful.
10948 Otherwise, return a tree expression with code CODE of type TYPE
10949 with operands OP0 and OP1. */
10951 tree
10952 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
10953 MEM_STAT_DECL)
10955 tree tem;
10956 #ifdef ENABLE_FOLD_CHECKING
10957 unsigned char checksum_before_op0[16],
10958 checksum_before_op1[16],
10959 checksum_after_op0[16],
10960 checksum_after_op1[16];
10961 struct md5_ctx ctx;
10962 htab_t ht;
10964 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10965 md5_init_ctx (&ctx);
10966 fold_checksum_tree (op0, &ctx, ht);
10967 md5_finish_ctx (&ctx, checksum_before_op0);
10968 htab_empty (ht);
10970 md5_init_ctx (&ctx);
10971 fold_checksum_tree (op1, &ctx, ht);
10972 md5_finish_ctx (&ctx, checksum_before_op1);
10973 htab_empty (ht);
10974 #endif
10976 tem = fold_binary (code, type, op0, op1);
10977 if (!tem)
10978 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
10980 #ifdef ENABLE_FOLD_CHECKING
10981 md5_init_ctx (&ctx);
10982 fold_checksum_tree (op0, &ctx, ht);
10983 md5_finish_ctx (&ctx, checksum_after_op0);
10984 htab_empty (ht);
10986 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
10987 fold_check_failed (op0, tem);
10989 md5_init_ctx (&ctx);
10990 fold_checksum_tree (op1, &ctx, ht);
10991 md5_finish_ctx (&ctx, checksum_after_op1);
10992 htab_delete (ht);
10994 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
10995 fold_check_failed (op1, tem);
10996 #endif
10997 return tem;
11000 /* Fold a ternary tree expression with code CODE of type TYPE with
11001 operands OP0, OP1, and OP2. Return a folded expression if
11002 successful. Otherwise, return a tree expression with code CODE of
11003 type TYPE with operands OP0, OP1, and OP2. */
11005 tree
11006 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
11007 MEM_STAT_DECL)
11009 tree tem;
11010 #ifdef ENABLE_FOLD_CHECKING
11011 unsigned char checksum_before_op0[16],
11012 checksum_before_op1[16],
11013 checksum_before_op2[16],
11014 checksum_after_op0[16],
11015 checksum_after_op1[16],
11016 checksum_after_op2[16];
11017 struct md5_ctx ctx;
11018 htab_t ht;
11020 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
11021 md5_init_ctx (&ctx);
11022 fold_checksum_tree (op0, &ctx, ht);
11023 md5_finish_ctx (&ctx, checksum_before_op0);
11024 htab_empty (ht);
11026 md5_init_ctx (&ctx);
11027 fold_checksum_tree (op1, &ctx, ht);
11028 md5_finish_ctx (&ctx, checksum_before_op1);
11029 htab_empty (ht);
11031 md5_init_ctx (&ctx);
11032 fold_checksum_tree (op2, &ctx, ht);
11033 md5_finish_ctx (&ctx, checksum_before_op2);
11034 htab_empty (ht);
11035 #endif
11037 tem = fold_ternary (code, type, op0, op1, op2);
11038 if (!tem)
11039 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
11041 #ifdef ENABLE_FOLD_CHECKING
11042 md5_init_ctx (&ctx);
11043 fold_checksum_tree (op0, &ctx, ht);
11044 md5_finish_ctx (&ctx, checksum_after_op0);
11045 htab_empty (ht);
11047 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
11048 fold_check_failed (op0, tem);
11050 md5_init_ctx (&ctx);
11051 fold_checksum_tree (op1, &ctx, ht);
11052 md5_finish_ctx (&ctx, checksum_after_op1);
11053 htab_empty (ht);
11055 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
11056 fold_check_failed (op1, tem);
11058 md5_init_ctx (&ctx);
11059 fold_checksum_tree (op2, &ctx, ht);
11060 md5_finish_ctx (&ctx, checksum_after_op2);
11061 htab_delete (ht);
11063 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
11064 fold_check_failed (op2, tem);
11065 #endif
11066 return tem;
11069 /* Perform constant folding and related simplification of initializer
11070 expression EXPR. These behave identically to "fold_buildN" but ignore
11071 potential run-time traps and exceptions that fold must preserve. */
11073 #define START_FOLD_INIT \
11074 int saved_signaling_nans = flag_signaling_nans;\
11075 int saved_trapping_math = flag_trapping_math;\
11076 int saved_rounding_math = flag_rounding_math;\
11077 int saved_trapv = flag_trapv;\
11078 flag_signaling_nans = 0;\
11079 flag_trapping_math = 0;\
11080 flag_rounding_math = 0;\
11081 flag_trapv = 0
11083 #define END_FOLD_INIT \
11084 flag_signaling_nans = saved_signaling_nans;\
11085 flag_trapping_math = saved_trapping_math;\
11086 flag_rounding_math = saved_rounding_math;\
11087 flag_trapv = saved_trapv
11089 tree
11090 fold_build1_initializer (enum tree_code code, tree type, tree op)
11092 tree result;
11093 START_FOLD_INIT;
11095 result = fold_build1 (code, type, op);
11097 END_FOLD_INIT;
11098 return result;
11101 tree
11102 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
11104 tree result;
11105 START_FOLD_INIT;
11107 result = fold_build2 (code, type, op0, op1);
11109 END_FOLD_INIT;
11110 return result;
11113 tree
11114 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
11115 tree op2)
11117 tree result;
11118 START_FOLD_INIT;
11120 result = fold_build3 (code, type, op0, op1, op2);
11122 END_FOLD_INIT;
11123 return result;
11126 #undef START_FOLD_INIT
11127 #undef END_FOLD_INIT
11129 /* Determine if first argument is a multiple of second argument. Return 0 if
11130 it is not, or we cannot easily determined it to be.
11132 An example of the sort of thing we care about (at this point; this routine
11133 could surely be made more general, and expanded to do what the *_DIV_EXPR's
11134 fold cases do now) is discovering that
11136 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11138 is a multiple of
11140 SAVE_EXPR (J * 8)
11142 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
11144 This code also handles discovering that
11146 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
11148 is a multiple of 8 so we don't have to worry about dealing with a
11149 possible remainder.
11151 Note that we *look* inside a SAVE_EXPR only to determine how it was
11152 calculated; it is not safe for fold to do much of anything else with the
11153 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
11154 at run time. For example, the latter example above *cannot* be implemented
11155 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
11156 evaluation time of the original SAVE_EXPR is not necessarily the same at
11157 the time the new expression is evaluated. The only optimization of this
11158 sort that would be valid is changing
11160 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
11162 divided by 8 to
11164 SAVE_EXPR (I) * SAVE_EXPR (J)
11166 (where the same SAVE_EXPR (J) is used in the original and the
11167 transformed version). */
11169 static int
11170 multiple_of_p (tree type, tree top, tree bottom)
11172 if (operand_equal_p (top, bottom, 0))
11173 return 1;
11175 if (TREE_CODE (type) != INTEGER_TYPE)
11176 return 0;
11178 switch (TREE_CODE (top))
11180 case BIT_AND_EXPR:
11181 /* Bitwise and provides a power of two multiple. If the mask is
11182 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
11183 if (!integer_pow2p (bottom))
11184 return 0;
11185 /* FALLTHRU */
11187 case MULT_EXPR:
11188 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11189 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11191 case PLUS_EXPR:
11192 case MINUS_EXPR:
11193 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
11194 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
11196 case LSHIFT_EXPR:
11197 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
11199 tree op1, t1;
11201 op1 = TREE_OPERAND (top, 1);
11202 /* const_binop may not detect overflow correctly,
11203 so check for it explicitly here. */
11204 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
11205 > TREE_INT_CST_LOW (op1)
11206 && TREE_INT_CST_HIGH (op1) == 0
11207 && 0 != (t1 = fold_convert (type,
11208 const_binop (LSHIFT_EXPR,
11209 size_one_node,
11210 op1, 0)))
11211 && ! TREE_OVERFLOW (t1))
11212 return multiple_of_p (type, t1, bottom);
11214 return 0;
11216 case NOP_EXPR:
11217 /* Can't handle conversions from non-integral or wider integral type. */
11218 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
11219 || (TYPE_PRECISION (type)
11220 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
11221 return 0;
11223 /* .. fall through ... */
11225 case SAVE_EXPR:
11226 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
11228 case INTEGER_CST:
11229 if (TREE_CODE (bottom) != INTEGER_CST
11230 || (TYPE_UNSIGNED (type)
11231 && (tree_int_cst_sgn (top) < 0
11232 || tree_int_cst_sgn (bottom) < 0)))
11233 return 0;
11234 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
11235 top, bottom, 0));
11237 default:
11238 return 0;
11242 /* Return true if `t' is known to be non-negative. */
11245 tree_expr_nonnegative_p (tree t)
11247 if (TYPE_UNSIGNED (TREE_TYPE (t)))
11248 return 1;
11250 switch (TREE_CODE (t))
11252 case SSA_NAME:
11253 /* Query VRP to see if it has recorded any information about
11254 the range of this object. */
11255 return ssa_name_nonnegative_p (t);
11257 case ABS_EXPR:
11258 /* We can't return 1 if flag_wrapv is set because
11259 ABS_EXPR<INT_MIN> = INT_MIN. */
11260 if (!(flag_wrapv && INTEGRAL_TYPE_P (TREE_TYPE (t))))
11261 return 1;
11262 break;
11264 case INTEGER_CST:
11265 return tree_int_cst_sgn (t) >= 0;
11267 case REAL_CST:
11268 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
11270 case PLUS_EXPR:
11271 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11272 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11273 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11275 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
11276 both unsigned and at least 2 bits shorter than the result. */
11277 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11278 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11279 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11281 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11282 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11283 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11284 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11286 unsigned int prec = MAX (TYPE_PRECISION (inner1),
11287 TYPE_PRECISION (inner2)) + 1;
11288 return prec < TYPE_PRECISION (TREE_TYPE (t));
11291 break;
11293 case MULT_EXPR:
11294 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11296 /* x * x for floating point x is always non-negative. */
11297 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
11298 return 1;
11299 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11300 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11303 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
11304 both unsigned and their total bits is shorter than the result. */
11305 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
11306 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
11307 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
11309 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
11310 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
11311 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
11312 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
11313 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
11314 < TYPE_PRECISION (TREE_TYPE (t));
11316 return 0;
11318 case BIT_AND_EXPR:
11319 case MAX_EXPR:
11320 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11321 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11323 case BIT_IOR_EXPR:
11324 case BIT_XOR_EXPR:
11325 case MIN_EXPR:
11326 case RDIV_EXPR:
11327 case TRUNC_DIV_EXPR:
11328 case CEIL_DIV_EXPR:
11329 case FLOOR_DIV_EXPR:
11330 case ROUND_DIV_EXPR:
11331 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11332 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11334 case TRUNC_MOD_EXPR:
11335 case CEIL_MOD_EXPR:
11336 case FLOOR_MOD_EXPR:
11337 case ROUND_MOD_EXPR:
11338 case SAVE_EXPR:
11339 case NON_LVALUE_EXPR:
11340 case FLOAT_EXPR:
11341 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11343 case COMPOUND_EXPR:
11344 case MODIFY_EXPR:
11345 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11347 case BIND_EXPR:
11348 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
11350 case COND_EXPR:
11351 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
11352 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
11354 case NOP_EXPR:
11356 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11357 tree outer_type = TREE_TYPE (t);
11359 if (TREE_CODE (outer_type) == REAL_TYPE)
11361 if (TREE_CODE (inner_type) == REAL_TYPE)
11362 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11363 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11365 if (TYPE_UNSIGNED (inner_type))
11366 return 1;
11367 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11370 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
11372 if (TREE_CODE (inner_type) == REAL_TYPE)
11373 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
11374 if (TREE_CODE (inner_type) == INTEGER_TYPE)
11375 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
11376 && TYPE_UNSIGNED (inner_type);
11379 break;
11381 case TARGET_EXPR:
11383 tree temp = TARGET_EXPR_SLOT (t);
11384 t = TARGET_EXPR_INITIAL (t);
11386 /* If the initializer is non-void, then it's a normal expression
11387 that will be assigned to the slot. */
11388 if (!VOID_TYPE_P (t))
11389 return tree_expr_nonnegative_p (t);
11391 /* Otherwise, the initializer sets the slot in some way. One common
11392 way is an assignment statement at the end of the initializer. */
11393 while (1)
11395 if (TREE_CODE (t) == BIND_EXPR)
11396 t = expr_last (BIND_EXPR_BODY (t));
11397 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
11398 || TREE_CODE (t) == TRY_CATCH_EXPR)
11399 t = expr_last (TREE_OPERAND (t, 0));
11400 else if (TREE_CODE (t) == STATEMENT_LIST)
11401 t = expr_last (t);
11402 else
11403 break;
11405 if (TREE_CODE (t) == MODIFY_EXPR
11406 && TREE_OPERAND (t, 0) == temp)
11407 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
11409 return 0;
11412 case CALL_EXPR:
11414 tree fndecl = get_callee_fndecl (t);
11415 tree arglist = TREE_OPERAND (t, 1);
11416 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
11417 switch (DECL_FUNCTION_CODE (fndecl))
11419 CASE_FLT_FN (BUILT_IN_ACOS):
11420 CASE_FLT_FN (BUILT_IN_ACOSH):
11421 CASE_FLT_FN (BUILT_IN_CABS):
11422 CASE_FLT_FN (BUILT_IN_COSH):
11423 CASE_FLT_FN (BUILT_IN_ERFC):
11424 CASE_FLT_FN (BUILT_IN_EXP):
11425 CASE_FLT_FN (BUILT_IN_EXP10):
11426 CASE_FLT_FN (BUILT_IN_EXP2):
11427 CASE_FLT_FN (BUILT_IN_FABS):
11428 CASE_FLT_FN (BUILT_IN_FDIM):
11429 CASE_FLT_FN (BUILT_IN_HYPOT):
11430 CASE_FLT_FN (BUILT_IN_POW10):
11431 CASE_INT_FN (BUILT_IN_FFS):
11432 CASE_INT_FN (BUILT_IN_PARITY):
11433 CASE_INT_FN (BUILT_IN_POPCOUNT):
11434 /* Always true. */
11435 return 1;
11437 CASE_FLT_FN (BUILT_IN_SQRT):
11438 /* sqrt(-0.0) is -0.0. */
11439 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
11440 return 1;
11441 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11443 CASE_FLT_FN (BUILT_IN_ASINH):
11444 CASE_FLT_FN (BUILT_IN_ATAN):
11445 CASE_FLT_FN (BUILT_IN_ATANH):
11446 CASE_FLT_FN (BUILT_IN_CBRT):
11447 CASE_FLT_FN (BUILT_IN_CEIL):
11448 CASE_FLT_FN (BUILT_IN_ERF):
11449 CASE_FLT_FN (BUILT_IN_EXPM1):
11450 CASE_FLT_FN (BUILT_IN_FLOOR):
11451 CASE_FLT_FN (BUILT_IN_FMOD):
11452 CASE_FLT_FN (BUILT_IN_FREXP):
11453 CASE_FLT_FN (BUILT_IN_LCEIL):
11454 CASE_FLT_FN (BUILT_IN_LDEXP):
11455 CASE_FLT_FN (BUILT_IN_LFLOOR):
11456 CASE_FLT_FN (BUILT_IN_LLCEIL):
11457 CASE_FLT_FN (BUILT_IN_LLFLOOR):
11458 CASE_FLT_FN (BUILT_IN_LLRINT):
11459 CASE_FLT_FN (BUILT_IN_LLROUND):
11460 CASE_FLT_FN (BUILT_IN_LRINT):
11461 CASE_FLT_FN (BUILT_IN_LROUND):
11462 CASE_FLT_FN (BUILT_IN_MODF):
11463 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11464 CASE_FLT_FN (BUILT_IN_POW):
11465 CASE_FLT_FN (BUILT_IN_RINT):
11466 CASE_FLT_FN (BUILT_IN_ROUND):
11467 CASE_FLT_FN (BUILT_IN_SIGNBIT):
11468 CASE_FLT_FN (BUILT_IN_SINH):
11469 CASE_FLT_FN (BUILT_IN_TANH):
11470 CASE_FLT_FN (BUILT_IN_TRUNC):
11471 /* True if the 1st argument is nonnegative. */
11472 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
11474 CASE_FLT_FN (BUILT_IN_FMAX):
11475 /* True if the 1st OR 2nd arguments are nonnegative. */
11476 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11477 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11479 CASE_FLT_FN (BUILT_IN_FMIN):
11480 /* True if the 1st AND 2nd arguments are nonnegative. */
11481 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
11482 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11484 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11485 /* True if the 2nd argument is nonnegative. */
11486 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
11488 default:
11489 break;
11493 /* ... fall through ... */
11495 default:
11496 if (truth_value_p (TREE_CODE (t)))
11497 /* Truth values evaluate to 0 or 1, which is nonnegative. */
11498 return 1;
11501 /* We don't know sign of `t', so be conservative and return false. */
11502 return 0;
11505 /* Return true when T is an address and is known to be nonzero.
11506 For floating point we further ensure that T is not denormal.
11507 Similar logic is present in nonzero_address in rtlanal.h. */
11509 bool
11510 tree_expr_nonzero_p (tree t)
11512 tree type = TREE_TYPE (t);
11514 /* Doing something useful for floating point would need more work. */
11515 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11516 return false;
11518 switch (TREE_CODE (t))
11520 case SSA_NAME:
11521 /* Query VRP to see if it has recorded any information about
11522 the range of this object. */
11523 return ssa_name_nonzero_p (t);
11525 case ABS_EXPR:
11526 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11528 case INTEGER_CST:
11529 /* We used to test for !integer_zerop here. This does not work correctly
11530 if TREE_CONSTANT_OVERFLOW (t). */
11531 return (TREE_INT_CST_LOW (t) != 0
11532 || TREE_INT_CST_HIGH (t) != 0);
11534 case PLUS_EXPR:
11535 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11537 /* With the presence of negative values it is hard
11538 to say something. */
11539 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11540 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11541 return false;
11542 /* One of operands must be positive and the other non-negative. */
11543 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11544 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11546 break;
11548 case MULT_EXPR:
11549 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11551 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11552 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11554 break;
11556 case NOP_EXPR:
11558 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11559 tree outer_type = TREE_TYPE (t);
11561 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
11562 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
11564 break;
11566 case ADDR_EXPR:
11568 tree base = get_base_address (TREE_OPERAND (t, 0));
11570 if (!base)
11571 return false;
11573 /* Weak declarations may link to NULL. */
11574 if (VAR_OR_FUNCTION_DECL_P (base))
11575 return !DECL_WEAK (base);
11577 /* Constants are never weak. */
11578 if (CONSTANT_CLASS_P (base))
11579 return true;
11581 return false;
11584 case COND_EXPR:
11585 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11586 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
11588 case MIN_EXPR:
11589 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11590 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11592 case MAX_EXPR:
11593 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
11595 /* When both operands are nonzero, then MAX must be too. */
11596 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
11597 return true;
11599 /* MAX where operand 0 is positive is positive. */
11600 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11602 /* MAX where operand 1 is positive is positive. */
11603 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11604 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11605 return true;
11606 break;
11608 case COMPOUND_EXPR:
11609 case MODIFY_EXPR:
11610 case BIND_EXPR:
11611 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
11613 case SAVE_EXPR:
11614 case NON_LVALUE_EXPR:
11615 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11617 case BIT_IOR_EXPR:
11618 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11619 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11621 case CALL_EXPR:
11622 return alloca_call_p (t);
11624 default:
11625 break;
11627 return false;
11630 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
11631 attempt to fold the expression to a constant without modifying TYPE,
11632 OP0 or OP1.
11634 If the expression could be simplified to a constant, then return
11635 the constant. If the expression would not be simplified to a
11636 constant, then return NULL_TREE. */
11638 tree
11639 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
11641 tree tem = fold_binary (code, type, op0, op1);
11642 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11645 /* Given the components of a unary expression CODE, TYPE and OP0,
11646 attempt to fold the expression to a constant without modifying
11647 TYPE or OP0.
11649 If the expression could be simplified to a constant, then return
11650 the constant. If the expression would not be simplified to a
11651 constant, then return NULL_TREE. */
11653 tree
11654 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
11656 tree tem = fold_unary (code, type, op0);
11657 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11660 /* If EXP represents referencing an element in a constant string
11661 (either via pointer arithmetic or array indexing), return the
11662 tree representing the value accessed, otherwise return NULL. */
11664 tree
11665 fold_read_from_constant_string (tree exp)
11667 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
11669 tree exp1 = TREE_OPERAND (exp, 0);
11670 tree index;
11671 tree string;
11673 if (TREE_CODE (exp) == INDIRECT_REF)
11674 string = string_constant (exp1, &index);
11675 else
11677 tree low_bound = array_ref_low_bound (exp);
11678 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
11680 /* Optimize the special-case of a zero lower bound.
11682 We convert the low_bound to sizetype to avoid some problems
11683 with constant folding. (E.g. suppose the lower bound is 1,
11684 and its mode is QI. Without the conversion,l (ARRAY
11685 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
11686 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
11687 if (! integer_zerop (low_bound))
11688 index = size_diffop (index, fold_convert (sizetype, low_bound));
11690 string = exp1;
11693 if (string
11694 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
11695 && TREE_CODE (string) == STRING_CST
11696 && TREE_CODE (index) == INTEGER_CST
11697 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
11698 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
11699 == MODE_INT)
11700 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
11701 return fold_convert (TREE_TYPE (exp),
11702 build_int_cst (NULL_TREE,
11703 (TREE_STRING_POINTER (string)
11704 [TREE_INT_CST_LOW (index)])));
11706 return NULL;
11709 /* Return the tree for neg (ARG0) when ARG0 is known to be either
11710 an integer constant or real constant.
11712 TYPE is the type of the result. */
11714 static tree
11715 fold_negate_const (tree arg0, tree type)
11717 tree t = NULL_TREE;
11719 switch (TREE_CODE (arg0))
11721 case INTEGER_CST:
11723 unsigned HOST_WIDE_INT low;
11724 HOST_WIDE_INT high;
11725 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11726 TREE_INT_CST_HIGH (arg0),
11727 &low, &high);
11728 t = build_int_cst_wide (type, low, high);
11729 t = force_fit_type (t, 1,
11730 (overflow | TREE_OVERFLOW (arg0))
11731 && !TYPE_UNSIGNED (type),
11732 TREE_CONSTANT_OVERFLOW (arg0));
11733 break;
11736 case REAL_CST:
11737 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11738 break;
11740 default:
11741 gcc_unreachable ();
11744 return t;
11747 /* Return the tree for abs (ARG0) when ARG0 is known to be either
11748 an integer constant or real constant.
11750 TYPE is the type of the result. */
11752 tree
11753 fold_abs_const (tree arg0, tree type)
11755 tree t = NULL_TREE;
11757 switch (TREE_CODE (arg0))
11759 case INTEGER_CST:
11760 /* If the value is unsigned, then the absolute value is
11761 the same as the ordinary value. */
11762 if (TYPE_UNSIGNED (type))
11763 t = arg0;
11764 /* Similarly, if the value is non-negative. */
11765 else if (INT_CST_LT (integer_minus_one_node, arg0))
11766 t = arg0;
11767 /* If the value is negative, then the absolute value is
11768 its negation. */
11769 else
11771 unsigned HOST_WIDE_INT low;
11772 HOST_WIDE_INT high;
11773 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11774 TREE_INT_CST_HIGH (arg0),
11775 &low, &high);
11776 t = build_int_cst_wide (type, low, high);
11777 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
11778 TREE_CONSTANT_OVERFLOW (arg0));
11780 break;
11782 case REAL_CST:
11783 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
11784 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11785 else
11786 t = arg0;
11787 break;
11789 default:
11790 gcc_unreachable ();
11793 return t;
11796 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11797 constant. TYPE is the type of the result. */
11799 static tree
11800 fold_not_const (tree arg0, tree type)
11802 tree t = NULL_TREE;
11804 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11806 t = build_int_cst_wide (type,
11807 ~ TREE_INT_CST_LOW (arg0),
11808 ~ TREE_INT_CST_HIGH (arg0));
11809 t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11810 TREE_CONSTANT_OVERFLOW (arg0));
11812 return t;
11815 /* Given CODE, a relational operator, the target type, TYPE and two
11816 constant operands OP0 and OP1, return the result of the
11817 relational operation. If the result is not a compile time
11818 constant, then return NULL_TREE. */
11820 static tree
11821 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11823 int result, invert;
11825 /* From here on, the only cases we handle are when the result is
11826 known to be a constant. */
11828 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11830 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11831 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11833 /* Handle the cases where either operand is a NaN. */
11834 if (real_isnan (c0) || real_isnan (c1))
11836 switch (code)
11838 case EQ_EXPR:
11839 case ORDERED_EXPR:
11840 result = 0;
11841 break;
11843 case NE_EXPR:
11844 case UNORDERED_EXPR:
11845 case UNLT_EXPR:
11846 case UNLE_EXPR:
11847 case UNGT_EXPR:
11848 case UNGE_EXPR:
11849 case UNEQ_EXPR:
11850 result = 1;
11851 break;
11853 case LT_EXPR:
11854 case LE_EXPR:
11855 case GT_EXPR:
11856 case GE_EXPR:
11857 case LTGT_EXPR:
11858 if (flag_trapping_math)
11859 return NULL_TREE;
11860 result = 0;
11861 break;
11863 default:
11864 gcc_unreachable ();
11867 return constant_boolean_node (result, type);
11870 return constant_boolean_node (real_compare (code, c0, c1), type);
11873 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11875 To compute GT, swap the arguments and do LT.
11876 To compute GE, do LT and invert the result.
11877 To compute LE, swap the arguments, do LT and invert the result.
11878 To compute NE, do EQ and invert the result.
11880 Therefore, the code below must handle only EQ and LT. */
11882 if (code == LE_EXPR || code == GT_EXPR)
11884 tree tem = op0;
11885 op0 = op1;
11886 op1 = tem;
11887 code = swap_tree_comparison (code);
11890 /* Note that it is safe to invert for real values here because we
11891 have already handled the one case that it matters. */
11893 invert = 0;
11894 if (code == NE_EXPR || code == GE_EXPR)
11896 invert = 1;
11897 code = invert_tree_comparison (code, false);
11900 /* Compute a result for LT or EQ if args permit;
11901 Otherwise return T. */
11902 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11904 if (code == EQ_EXPR)
11905 result = tree_int_cst_equal (op0, op1);
11906 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
11907 result = INT_CST_LT_UNSIGNED (op0, op1);
11908 else
11909 result = INT_CST_LT (op0, op1);
11911 else
11912 return NULL_TREE;
11914 if (invert)
11915 result ^= 1;
11916 return constant_boolean_node (result, type);
11919 /* Build an expression for the a clean point containing EXPR with type TYPE.
11920 Don't build a cleanup point expression for EXPR which don't have side
11921 effects. */
11923 tree
11924 fold_build_cleanup_point_expr (tree type, tree expr)
11926 /* If the expression does not have side effects then we don't have to wrap
11927 it with a cleanup point expression. */
11928 if (!TREE_SIDE_EFFECTS (expr))
11929 return expr;
11931 /* If the expression is a return, check to see if the expression inside the
11932 return has no side effects or the right hand side of the modify expression
11933 inside the return. If either don't have side effects set we don't need to
11934 wrap the expression in a cleanup point expression. Note we don't check the
11935 left hand side of the modify because it should always be a return decl. */
11936 if (TREE_CODE (expr) == RETURN_EXPR)
11938 tree op = TREE_OPERAND (expr, 0);
11939 if (!op || !TREE_SIDE_EFFECTS (op))
11940 return expr;
11941 op = TREE_OPERAND (op, 1);
11942 if (!TREE_SIDE_EFFECTS (op))
11943 return expr;
11946 return build1 (CLEANUP_POINT_EXPR, type, expr);
11949 /* Build an expression for the address of T. Folds away INDIRECT_REF to
11950 avoid confusing the gimplify process. */
11952 tree
11953 build_fold_addr_expr_with_type (tree t, tree ptrtype)
11955 /* The size of the object is not relevant when talking about its address. */
11956 if (TREE_CODE (t) == WITH_SIZE_EXPR)
11957 t = TREE_OPERAND (t, 0);
11959 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
11960 if (TREE_CODE (t) == INDIRECT_REF
11961 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
11963 t = TREE_OPERAND (t, 0);
11964 if (TREE_TYPE (t) != ptrtype)
11965 t = build1 (NOP_EXPR, ptrtype, t);
11967 else
11969 tree base = t;
11971 while (handled_component_p (base))
11972 base = TREE_OPERAND (base, 0);
11973 if (DECL_P (base))
11974 TREE_ADDRESSABLE (base) = 1;
11976 t = build1 (ADDR_EXPR, ptrtype, t);
11979 return t;
11982 tree
11983 build_fold_addr_expr (tree t)
11985 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
11988 /* Given a pointer value OP0 and a type TYPE, return a simplified version
11989 of an indirection through OP0, or NULL_TREE if no simplification is
11990 possible. */
11992 tree
11993 fold_indirect_ref_1 (tree type, tree op0)
11995 tree sub = op0;
11996 tree subtype;
11998 STRIP_NOPS (sub);
11999 subtype = TREE_TYPE (sub);
12000 if (!POINTER_TYPE_P (subtype))
12001 return NULL_TREE;
12003 if (TREE_CODE (sub) == ADDR_EXPR)
12005 tree op = TREE_OPERAND (sub, 0);
12006 tree optype = TREE_TYPE (op);
12007 /* *&p => p; make sure to handle *&"str"[cst] here. */
12008 if (type == optype)
12010 tree fop = fold_read_from_constant_string (op);
12011 if (fop)
12012 return fop;
12013 else
12014 return op;
12016 /* *(foo *)&fooarray => fooarray[0] */
12017 else if (TREE_CODE (optype) == ARRAY_TYPE
12018 && type == TREE_TYPE (optype))
12020 tree type_domain = TYPE_DOMAIN (optype);
12021 tree min_val = size_zero_node;
12022 if (type_domain && TYPE_MIN_VALUE (type_domain))
12023 min_val = TYPE_MIN_VALUE (type_domain);
12024 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
12026 /* *(foo *)&complexfoo => __real__ complexfoo */
12027 else if (TREE_CODE (optype) == COMPLEX_TYPE
12028 && type == TREE_TYPE (optype))
12029 return fold_build1 (REALPART_EXPR, type, op);
12032 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
12033 if (TREE_CODE (sub) == PLUS_EXPR
12034 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
12036 tree op00 = TREE_OPERAND (sub, 0);
12037 tree op01 = TREE_OPERAND (sub, 1);
12038 tree op00type;
12040 STRIP_NOPS (op00);
12041 op00type = TREE_TYPE (op00);
12042 if (TREE_CODE (op00) == ADDR_EXPR
12043 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
12044 && type == TREE_TYPE (TREE_TYPE (op00type)))
12046 tree size = TYPE_SIZE_UNIT (type);
12047 if (tree_int_cst_equal (size, op01))
12048 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
12052 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
12053 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
12054 && type == TREE_TYPE (TREE_TYPE (subtype)))
12056 tree type_domain;
12057 tree min_val = size_zero_node;
12058 sub = build_fold_indirect_ref (sub);
12059 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
12060 if (type_domain && TYPE_MIN_VALUE (type_domain))
12061 min_val = TYPE_MIN_VALUE (type_domain);
12062 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
12065 return NULL_TREE;
12068 /* Builds an expression for an indirection through T, simplifying some
12069 cases. */
12071 tree
12072 build_fold_indirect_ref (tree t)
12074 tree type = TREE_TYPE (TREE_TYPE (t));
12075 tree sub = fold_indirect_ref_1 (type, t);
12077 if (sub)
12078 return sub;
12079 else
12080 return build1 (INDIRECT_REF, type, t);
12083 /* Given an INDIRECT_REF T, return either T or a simplified version. */
12085 tree
12086 fold_indirect_ref (tree t)
12088 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
12090 if (sub)
12091 return sub;
12092 else
12093 return t;
12096 /* Strip non-trapping, non-side-effecting tree nodes from an expression
12097 whose result is ignored. The type of the returned tree need not be
12098 the same as the original expression. */
12100 tree
12101 fold_ignored_result (tree t)
12103 if (!TREE_SIDE_EFFECTS (t))
12104 return integer_zero_node;
12106 for (;;)
12107 switch (TREE_CODE_CLASS (TREE_CODE (t)))
12109 case tcc_unary:
12110 t = TREE_OPERAND (t, 0);
12111 break;
12113 case tcc_binary:
12114 case tcc_comparison:
12115 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12116 t = TREE_OPERAND (t, 0);
12117 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
12118 t = TREE_OPERAND (t, 1);
12119 else
12120 return t;
12121 break;
12123 case tcc_expression:
12124 switch (TREE_CODE (t))
12126 case COMPOUND_EXPR:
12127 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
12128 return t;
12129 t = TREE_OPERAND (t, 0);
12130 break;
12132 case COND_EXPR:
12133 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
12134 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
12135 return t;
12136 t = TREE_OPERAND (t, 0);
12137 break;
12139 default:
12140 return t;
12142 break;
12144 default:
12145 return t;
12149 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
12150 This can only be applied to objects of a sizetype. */
12152 tree
12153 round_up (tree value, int divisor)
12155 tree div = NULL_TREE;
12157 gcc_assert (divisor > 0);
12158 if (divisor == 1)
12159 return value;
12161 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12162 have to do anything. Only do this when we are not given a const,
12163 because in that case, this check is more expensive than just
12164 doing it. */
12165 if (TREE_CODE (value) != INTEGER_CST)
12167 div = build_int_cst (TREE_TYPE (value), divisor);
12169 if (multiple_of_p (TREE_TYPE (value), value, div))
12170 return value;
12173 /* If divisor is a power of two, simplify this to bit manipulation. */
12174 if (divisor == (divisor & -divisor))
12176 tree t;
12178 t = build_int_cst (TREE_TYPE (value), divisor - 1);
12179 value = size_binop (PLUS_EXPR, value, t);
12180 t = build_int_cst (TREE_TYPE (value), -divisor);
12181 value = size_binop (BIT_AND_EXPR, value, t);
12183 else
12185 if (!div)
12186 div = build_int_cst (TREE_TYPE (value), divisor);
12187 value = size_binop (CEIL_DIV_EXPR, value, div);
12188 value = size_binop (MULT_EXPR, value, div);
12191 return value;
12194 /* Likewise, but round down. */
12196 tree
12197 round_down (tree value, int divisor)
12199 tree div = NULL_TREE;
12201 gcc_assert (divisor > 0);
12202 if (divisor == 1)
12203 return value;
12205 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
12206 have to do anything. Only do this when we are not given a const,
12207 because in that case, this check is more expensive than just
12208 doing it. */
12209 if (TREE_CODE (value) != INTEGER_CST)
12211 div = build_int_cst (TREE_TYPE (value), divisor);
12213 if (multiple_of_p (TREE_TYPE (value), value, div))
12214 return value;
12217 /* If divisor is a power of two, simplify this to bit manipulation. */
12218 if (divisor == (divisor & -divisor))
12220 tree t;
12222 t = build_int_cst (TREE_TYPE (value), -divisor);
12223 value = size_binop (BIT_AND_EXPR, value, t);
12225 else
12227 if (!div)
12228 div = build_int_cst (TREE_TYPE (value), divisor);
12229 value = size_binop (FLOOR_DIV_EXPR, value, div);
12230 value = size_binop (MULT_EXPR, value, div);
12233 return value;
12236 /* Returns the pointer to the base of the object addressed by EXP and
12237 extracts the information about the offset of the access, storing it
12238 to PBITPOS and POFFSET. */
12240 static tree
12241 split_address_to_core_and_offset (tree exp,
12242 HOST_WIDE_INT *pbitpos, tree *poffset)
12244 tree core;
12245 enum machine_mode mode;
12246 int unsignedp, volatilep;
12247 HOST_WIDE_INT bitsize;
12249 if (TREE_CODE (exp) == ADDR_EXPR)
12251 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
12252 poffset, &mode, &unsignedp, &volatilep,
12253 false);
12254 core = build_fold_addr_expr (core);
12256 else
12258 core = exp;
12259 *pbitpos = 0;
12260 *poffset = NULL_TREE;
12263 return core;
12266 /* Returns true if addresses of E1 and E2 differ by a constant, false
12267 otherwise. If they do, E1 - E2 is stored in *DIFF. */
12269 bool
12270 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
12272 tree core1, core2;
12273 HOST_WIDE_INT bitpos1, bitpos2;
12274 tree toffset1, toffset2, tdiff, type;
12276 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
12277 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
12279 if (bitpos1 % BITS_PER_UNIT != 0
12280 || bitpos2 % BITS_PER_UNIT != 0
12281 || !operand_equal_p (core1, core2, 0))
12282 return false;
12284 if (toffset1 && toffset2)
12286 type = TREE_TYPE (toffset1);
12287 if (type != TREE_TYPE (toffset2))
12288 toffset2 = fold_convert (type, toffset2);
12290 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
12291 if (!cst_and_fits_in_hwi (tdiff))
12292 return false;
12294 *diff = int_cst_value (tdiff);
12296 else if (toffset1 || toffset2)
12298 /* If only one of the offsets is non-constant, the difference cannot
12299 be a constant. */
12300 return false;
12302 else
12303 *diff = 0;
12305 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
12306 return true;
12309 /* Simplify the floating point expression EXP when the sign of the
12310 result is not significant. Return NULL_TREE if no simplification
12311 is possible. */
12313 tree
12314 fold_strip_sign_ops (tree exp)
12316 tree arg0, arg1;
12318 switch (TREE_CODE (exp))
12320 case ABS_EXPR:
12321 case NEGATE_EXPR:
12322 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12323 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
12325 case MULT_EXPR:
12326 case RDIV_EXPR:
12327 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
12328 return NULL_TREE;
12329 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
12330 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
12331 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
12332 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
12333 arg0 ? arg0 : TREE_OPERAND (exp, 0),
12334 arg1 ? arg1 : TREE_OPERAND (exp, 1));
12335 break;
12337 default:
12338 break;
12340 return NULL_TREE;