* gcc.dg/compat/struct-layout-1_generate.c (dg_options): New. Moved
[official-gcc.git] / gcc / fold-const.c
blob0ce122a15545b3f57362ce781703d55bd6099460
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, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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_double.
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_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
44 sets TREE_OVERFLOW.
46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
70 /* Nonzero if we are folding constants inside an initializer; zero
71 otherwise. */
72 int folding_initializer = 0;
74 /* The following constants represent a bit based encoding of GCC's
75 comparison operators. This encoding simplifies transformations
76 on relational comparison operators, such as AND and OR. */
77 enum comparison_code {
78 COMPCODE_FALSE = 0,
79 COMPCODE_LT = 1,
80 COMPCODE_EQ = 2,
81 COMPCODE_LE = 3,
82 COMPCODE_GT = 4,
83 COMPCODE_LTGT = 5,
84 COMPCODE_GE = 6,
85 COMPCODE_ORD = 7,
86 COMPCODE_UNORD = 8,
87 COMPCODE_UNLT = 9,
88 COMPCODE_UNEQ = 10,
89 COMPCODE_UNLE = 11,
90 COMPCODE_UNGT = 12,
91 COMPCODE_NE = 13,
92 COMPCODE_UNGE = 14,
93 COMPCODE_TRUE = 15
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static tree combine_comparisons (enum tree_code, enum tree_code,
107 enum tree_code, tree, tree, tree);
108 static int truth_value_p (enum tree_code);
109 static int operand_equal_for_comparison_p (tree, tree, tree);
110 static int twoval_comparison_p (tree, tree *, tree *, int *);
111 static tree eval_subst (tree, tree, tree, tree, tree);
112 static tree pedantic_omit_one_operand (tree, tree, tree);
113 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
114 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
115 enum machine_mode *, int *, int *,
116 tree *, tree *);
117 static tree sign_bit_p (tree, const_tree);
118 static int simple_operand_p (const_tree);
119 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
120 static tree range_predecessor (tree);
121 static tree range_successor (tree);
122 static tree make_range (tree, int *, tree *, tree *, bool *);
123 static tree build_range_check (tree, tree, int, tree, tree);
124 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
125 tree);
126 static tree fold_range_test (enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
137 tree, tree, tree);
138 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
139 static tree fold_div_compare (enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
148 and SUM1. Then this yields nonzero if overflow occurred during the
149 addition.
151 Overflow occurs if A and B have the same sign, but A and SUM differ in
152 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
153 sign. */
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
156 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
157 We do that by representing the two-word integer in 4 words, with only
158 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
159 number. The value of the word is LOWPART + HIGHPART * BASE. */
161 #define LOWPART(x) \
162 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
163 #define HIGHPART(x) \
164 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
165 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
167 /* Unpack a two-word integer into 4 words.
168 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
169 WORDS points to the array of HOST_WIDE_INTs. */
171 static void
172 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
174 words[0] = LOWPART (low);
175 words[1] = HIGHPART (low);
176 words[2] = LOWPART (hi);
177 words[3] = HIGHPART (hi);
180 /* Pack an array of 4 words into a two-word integer.
181 WORDS points to the array of words.
182 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
184 static void
185 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
186 HOST_WIDE_INT *hi)
188 *low = words[0] + words[1] * BASE;
189 *hi = words[2] + words[3] * BASE;
192 /* Force the double-word integer L1, H1 to be within the range of the
193 integer type TYPE. Stores the properly truncated and sign-extended
194 double-word integer in *LV, *HV. Returns true if the operation
195 overflows, that is, argument and result are different. */
198 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
199 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
201 unsigned HOST_WIDE_INT low0 = l1;
202 HOST_WIDE_INT high0 = h1;
203 unsigned int prec;
204 int sign_extended_type;
206 if (POINTER_TYPE_P (type)
207 || TREE_CODE (type) == OFFSET_TYPE)
208 prec = POINTER_SIZE;
209 else
210 prec = TYPE_PRECISION (type);
212 /* Size types *are* sign extended. */
213 sign_extended_type = (!TYPE_UNSIGNED (type)
214 || (TREE_CODE (type) == INTEGER_TYPE
215 && TYPE_IS_SIZETYPE (type)));
217 /* First clear all bits that are beyond the type's precision. */
218 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
220 else if (prec > HOST_BITS_PER_WIDE_INT)
221 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
222 else
224 h1 = 0;
225 if (prec < HOST_BITS_PER_WIDE_INT)
226 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
229 /* Then do sign extension if necessary. */
230 if (!sign_extended_type)
231 /* No sign extension */;
232 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233 /* Correct width already. */;
234 else if (prec > HOST_BITS_PER_WIDE_INT)
236 /* Sign extend top half? */
237 if (h1 & ((unsigned HOST_WIDE_INT)1
238 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
241 else if (prec == HOST_BITS_PER_WIDE_INT)
243 if ((HOST_WIDE_INT)l1 < 0)
244 h1 = -1;
246 else
248 /* Sign extend bottom half? */
249 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
251 h1 = -1;
252 l1 |= (HOST_WIDE_INT)(-1) << prec;
256 *lv = l1;
257 *hv = h1;
259 /* If the value didn't fit, signal overflow. */
260 return l1 != low0 || h1 != high0;
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264 sign or zero extending it.
265 OVERFLOWABLE indicates if we are interested
266 in overflow of the value, when >0 we are only interested in signed
267 overflow, for <0 we are interested in any overflow. OVERFLOWED
268 indicates whether overflow has already occurred. CONST_OVERFLOWED
269 indicates whether constant overflow has already occurred. We force
270 T's value to be within range of T's type (by setting to 0 or 1 all
271 the bits outside the type's range). We set TREE_OVERFLOWED if,
272 OVERFLOWED is nonzero,
273 or OVERFLOWABLE is >0 and signed overflow occurs
274 or OVERFLOWABLE is <0 and any overflow occurs
275 We return a new tree node for the extended double-int. The node
276 is shared if no overflow flags are set. */
278 tree
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280 HOST_WIDE_INT high, int overflowable,
281 bool overflowed)
283 int sign_extended_type;
284 bool overflow;
286 /* Size types *are* sign extended. */
287 sign_extended_type = (!TYPE_UNSIGNED (type)
288 || (TREE_CODE (type) == INTEGER_TYPE
289 && TYPE_IS_SIZETYPE (type)));
291 overflow = fit_double_type (low, high, &low, &high, type);
293 /* If we need to set overflow flags, return a new unshared node. */
294 if (overflowed || overflow)
296 if (overflowed
297 || overflowable < 0
298 || (overflowable > 0 && sign_extended_type))
300 tree t = make_node (INTEGER_CST);
301 TREE_INT_CST_LOW (t) = low;
302 TREE_INT_CST_HIGH (t) = high;
303 TREE_TYPE (t) = type;
304 TREE_OVERFLOW (t) = 1;
305 return t;
309 /* Else build a shared node. */
310 return build_int_cst_wide (type, low, high);
313 /* Add two doubleword integers with doubleword result.
314 Return nonzero if the operation overflows according to UNSIGNED_P.
315 Each argument is given as two `HOST_WIDE_INT' pieces.
316 One argument is L1 and H1; the other, L2 and H2.
317 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
323 bool unsigned_p)
325 unsigned HOST_WIDE_INT l;
326 HOST_WIDE_INT h;
328 l = l1 + l2;
329 h = h1 + h2 + (l < l1);
331 *lv = l;
332 *hv = h;
334 if (unsigned_p)
335 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
336 else
337 return OVERFLOW_SUM_SIGN (h1, h2, h);
340 /* Negate a doubleword integer with doubleword result.
341 Return nonzero if the operation overflows, assuming it's signed.
342 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
343 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
346 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
347 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
349 if (l1 == 0)
351 *lv = 0;
352 *hv = - h1;
353 return (*hv & h1) < 0;
355 else
357 *lv = -l1;
358 *hv = ~h1;
359 return 0;
363 /* Multiply two doubleword integers with doubleword result.
364 Return nonzero if the operation overflows according to UNSIGNED_P.
365 Each argument is given as two `HOST_WIDE_INT' pieces.
366 One argument is L1 and H1; the other, L2 and H2.
367 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
370 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
371 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
372 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
373 bool unsigned_p)
375 HOST_WIDE_INT arg1[4];
376 HOST_WIDE_INT arg2[4];
377 HOST_WIDE_INT prod[4 * 2];
378 unsigned HOST_WIDE_INT carry;
379 int i, j, k;
380 unsigned HOST_WIDE_INT toplow, neglow;
381 HOST_WIDE_INT tophigh, neghigh;
383 encode (arg1, l1, h1);
384 encode (arg2, l2, h2);
386 memset (prod, 0, sizeof prod);
388 for (i = 0; i < 4; i++)
390 carry = 0;
391 for (j = 0; j < 4; j++)
393 k = i + j;
394 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
395 carry += arg1[i] * arg2[j];
396 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
397 carry += prod[k];
398 prod[k] = LOWPART (carry);
399 carry = HIGHPART (carry);
401 prod[i + 4] = carry;
404 decode (prod, lv, hv);
405 decode (prod + 4, &toplow, &tophigh);
407 /* Unsigned overflow is immediate. */
408 if (unsigned_p)
409 return (toplow | tophigh) != 0;
411 /* Check for signed overflow by calculating the signed representation of the
412 top half of the result; it should agree with the low half's sign bit. */
413 if (h1 < 0)
415 neg_double (l2, h2, &neglow, &neghigh);
416 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
418 if (h2 < 0)
420 neg_double (l1, h1, &neglow, &neghigh);
421 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
426 /* Shift the doubleword integer in L1, H1 left by COUNT places
427 keeping only PREC bits of result.
428 Shift right if COUNT is negative.
429 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
430 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
432 void
433 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
434 HOST_WIDE_INT count, unsigned int prec,
435 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
437 unsigned HOST_WIDE_INT signmask;
439 if (count < 0)
441 rshift_double (l1, h1, -count, prec, lv, hv, arith);
442 return;
445 if (SHIFT_COUNT_TRUNCATED)
446 count %= prec;
448 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
450 /* Shifting by the host word size is undefined according to the
451 ANSI standard, so we must handle this as a special case. */
452 *hv = 0;
453 *lv = 0;
455 else if (count >= HOST_BITS_PER_WIDE_INT)
457 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
458 *lv = 0;
460 else
462 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
463 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
464 *lv = l1 << count;
467 /* Sign extend all bits that are beyond the precision. */
469 signmask = -((prec > HOST_BITS_PER_WIDE_INT
470 ? ((unsigned HOST_WIDE_INT) *hv
471 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
472 : (*lv >> (prec - 1))) & 1);
474 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
476 else if (prec >= HOST_BITS_PER_WIDE_INT)
478 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
479 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
481 else
483 *hv = signmask;
484 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
485 *lv |= signmask << prec;
489 /* Shift the doubleword integer in L1, H1 right by COUNT places
490 keeping only PREC bits of result. COUNT must be positive.
491 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
492 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
494 void
495 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
496 HOST_WIDE_INT count, unsigned int prec,
497 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
498 int arith)
500 unsigned HOST_WIDE_INT signmask;
502 signmask = (arith
503 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
504 : 0);
506 if (SHIFT_COUNT_TRUNCATED)
507 count %= prec;
509 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
511 /* Shifting by the host word size is undefined according to the
512 ANSI standard, so we must handle this as a special case. */
513 *hv = 0;
514 *lv = 0;
516 else if (count >= HOST_BITS_PER_WIDE_INT)
518 *hv = 0;
519 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
521 else
523 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
524 *lv = ((l1 >> count)
525 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
528 /* Zero / sign extend all bits that are beyond the precision. */
530 if (count >= (HOST_WIDE_INT)prec)
532 *hv = signmask;
533 *lv = signmask;
535 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
537 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
539 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
540 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
542 else
544 *hv = signmask;
545 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
546 *lv |= signmask << (prec - count);
550 /* Rotate the doubleword integer in L1, H1 left by COUNT places
551 keeping only PREC bits of result.
552 Rotate right if COUNT is negative.
553 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
555 void
556 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
557 HOST_WIDE_INT count, unsigned int prec,
558 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
560 unsigned HOST_WIDE_INT s1l, s2l;
561 HOST_WIDE_INT s1h, s2h;
563 count %= prec;
564 if (count < 0)
565 count += prec;
567 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
568 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
569 *lv = s1l | s2l;
570 *hv = s1h | s2h;
573 /* Rotate the doubleword integer in L1, H1 left by COUNT places
574 keeping only PREC bits of result. COUNT must be positive.
575 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
577 void
578 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
579 HOST_WIDE_INT count, unsigned int prec,
580 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
582 unsigned HOST_WIDE_INT s1l, s2l;
583 HOST_WIDE_INT s1h, s2h;
585 count %= prec;
586 if (count < 0)
587 count += prec;
589 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
590 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
591 *lv = s1l | s2l;
592 *hv = s1h | s2h;
595 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
596 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
597 CODE is a tree code for a kind of division, one of
598 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
599 or EXACT_DIV_EXPR
600 It controls how the quotient is rounded to an integer.
601 Return nonzero if the operation overflows.
602 UNS nonzero says do unsigned division. */
605 div_and_round_double (enum tree_code code, int uns,
606 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
607 HOST_WIDE_INT hnum_orig,
608 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
609 HOST_WIDE_INT hden_orig,
610 unsigned HOST_WIDE_INT *lquo,
611 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
612 HOST_WIDE_INT *hrem)
614 int quo_neg = 0;
615 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
616 HOST_WIDE_INT den[4], quo[4];
617 int i, j;
618 unsigned HOST_WIDE_INT work;
619 unsigned HOST_WIDE_INT carry = 0;
620 unsigned HOST_WIDE_INT lnum = lnum_orig;
621 HOST_WIDE_INT hnum = hnum_orig;
622 unsigned HOST_WIDE_INT lden = lden_orig;
623 HOST_WIDE_INT hden = hden_orig;
624 int overflow = 0;
626 if (hden == 0 && lden == 0)
627 overflow = 1, lden = 1;
629 /* Calculate quotient sign and convert operands to unsigned. */
630 if (!uns)
632 if (hnum < 0)
634 quo_neg = ~ quo_neg;
635 /* (minimum integer) / (-1) is the only overflow case. */
636 if (neg_double (lnum, hnum, &lnum, &hnum)
637 && ((HOST_WIDE_INT) lden & hden) == -1)
638 overflow = 1;
640 if (hden < 0)
642 quo_neg = ~ quo_neg;
643 neg_double (lden, hden, &lden, &hden);
647 if (hnum == 0 && hden == 0)
648 { /* single precision */
649 *hquo = *hrem = 0;
650 /* This unsigned division rounds toward zero. */
651 *lquo = lnum / lden;
652 goto finish_up;
655 if (hnum == 0)
656 { /* trivial case: dividend < divisor */
657 /* hden != 0 already checked. */
658 *hquo = *lquo = 0;
659 *hrem = hnum;
660 *lrem = lnum;
661 goto finish_up;
664 memset (quo, 0, sizeof quo);
666 memset (num, 0, sizeof num); /* to zero 9th element */
667 memset (den, 0, sizeof den);
669 encode (num, lnum, hnum);
670 encode (den, lden, hden);
672 /* Special code for when the divisor < BASE. */
673 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
675 /* hnum != 0 already checked. */
676 for (i = 4 - 1; i >= 0; i--)
678 work = num[i] + carry * BASE;
679 quo[i] = work / lden;
680 carry = work % lden;
683 else
685 /* Full double precision division,
686 with thanks to Don Knuth's "Seminumerical Algorithms". */
687 int num_hi_sig, den_hi_sig;
688 unsigned HOST_WIDE_INT quo_est, scale;
690 /* Find the highest nonzero divisor digit. */
691 for (i = 4 - 1;; i--)
692 if (den[i] != 0)
694 den_hi_sig = i;
695 break;
698 /* Insure that the first digit of the divisor is at least BASE/2.
699 This is required by the quotient digit estimation algorithm. */
701 scale = BASE / (den[den_hi_sig] + 1);
702 if (scale > 1)
703 { /* scale divisor and dividend */
704 carry = 0;
705 for (i = 0; i <= 4 - 1; i++)
707 work = (num[i] * scale) + carry;
708 num[i] = LOWPART (work);
709 carry = HIGHPART (work);
712 num[4] = carry;
713 carry = 0;
714 for (i = 0; i <= 4 - 1; i++)
716 work = (den[i] * scale) + carry;
717 den[i] = LOWPART (work);
718 carry = HIGHPART (work);
719 if (den[i] != 0) den_hi_sig = i;
723 num_hi_sig = 4;
725 /* Main loop */
726 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
728 /* Guess the next quotient digit, quo_est, by dividing the first
729 two remaining dividend digits by the high order quotient digit.
730 quo_est is never low and is at most 2 high. */
731 unsigned HOST_WIDE_INT tmp;
733 num_hi_sig = i + den_hi_sig + 1;
734 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
735 if (num[num_hi_sig] != den[den_hi_sig])
736 quo_est = work / den[den_hi_sig];
737 else
738 quo_est = BASE - 1;
740 /* Refine quo_est so it's usually correct, and at most one high. */
741 tmp = work - quo_est * den[den_hi_sig];
742 if (tmp < BASE
743 && (den[den_hi_sig - 1] * quo_est
744 > (tmp * BASE + num[num_hi_sig - 2])))
745 quo_est--;
747 /* Try QUO_EST as the quotient digit, by multiplying the
748 divisor by QUO_EST and subtracting from the remaining dividend.
749 Keep in mind that QUO_EST is the I - 1st digit. */
751 carry = 0;
752 for (j = 0; j <= den_hi_sig; j++)
754 work = quo_est * den[j] + carry;
755 carry = HIGHPART (work);
756 work = num[i + j] - LOWPART (work);
757 num[i + j] = LOWPART (work);
758 carry += HIGHPART (work) != 0;
761 /* If quo_est was high by one, then num[i] went negative and
762 we need to correct things. */
763 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
765 quo_est--;
766 carry = 0; /* add divisor back in */
767 for (j = 0; j <= den_hi_sig; j++)
769 work = num[i + j] + den[j] + carry;
770 carry = HIGHPART (work);
771 num[i + j] = LOWPART (work);
774 num [num_hi_sig] += carry;
777 /* Store the quotient digit. */
778 quo[i] = quo_est;
782 decode (quo, lquo, hquo);
784 finish_up:
785 /* If result is negative, make it so. */
786 if (quo_neg)
787 neg_double (*lquo, *hquo, lquo, hquo);
789 /* Compute trial remainder: rem = num - (quo * den) */
790 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
791 neg_double (*lrem, *hrem, lrem, hrem);
792 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
794 switch (code)
796 case TRUNC_DIV_EXPR:
797 case TRUNC_MOD_EXPR: /* round toward zero */
798 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
799 return overflow;
801 case FLOOR_DIV_EXPR:
802 case FLOOR_MOD_EXPR: /* round toward negative infinity */
803 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
805 /* quo = quo - 1; */
806 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
807 lquo, hquo);
809 else
810 return overflow;
811 break;
813 case CEIL_DIV_EXPR:
814 case CEIL_MOD_EXPR: /* round toward positive infinity */
815 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
817 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
818 lquo, hquo);
820 else
821 return overflow;
822 break;
824 case ROUND_DIV_EXPR:
825 case ROUND_MOD_EXPR: /* round to closest integer */
827 unsigned HOST_WIDE_INT labs_rem = *lrem;
828 HOST_WIDE_INT habs_rem = *hrem;
829 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
830 HOST_WIDE_INT habs_den = hden, htwice;
832 /* Get absolute values. */
833 if (*hrem < 0)
834 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
835 if (hden < 0)
836 neg_double (lden, hden, &labs_den, &habs_den);
838 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
839 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
840 labs_rem, habs_rem, &ltwice, &htwice);
842 if (((unsigned HOST_WIDE_INT) habs_den
843 < (unsigned HOST_WIDE_INT) htwice)
844 || (((unsigned HOST_WIDE_INT) habs_den
845 == (unsigned HOST_WIDE_INT) htwice)
846 && (labs_den <= ltwice)))
848 if (*hquo < 0)
849 /* quo = quo - 1; */
850 add_double (*lquo, *hquo,
851 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
852 else
853 /* quo = quo + 1; */
854 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
855 lquo, hquo);
857 else
858 return overflow;
860 break;
862 default:
863 gcc_unreachable ();
866 /* Compute true remainder: rem = num - (quo * den) */
867 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
868 neg_double (*lrem, *hrem, lrem, hrem);
869 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
870 return overflow;
873 /* If ARG2 divides ARG1 with zero remainder, carries out the division
874 of type CODE and returns the quotient.
875 Otherwise returns NULL_TREE. */
877 static tree
878 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
880 unsigned HOST_WIDE_INT int1l, int2l;
881 HOST_WIDE_INT int1h, int2h;
882 unsigned HOST_WIDE_INT quol, reml;
883 HOST_WIDE_INT quoh, remh;
884 tree type = TREE_TYPE (arg1);
885 int uns = TYPE_UNSIGNED (type);
887 int1l = TREE_INT_CST_LOW (arg1);
888 int1h = TREE_INT_CST_HIGH (arg1);
889 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
890 &obj[some_exotic_number]. */
891 if (POINTER_TYPE_P (type))
893 uns = false;
894 type = signed_type_for (type);
895 fit_double_type (int1l, int1h, &int1l, &int1h,
896 type);
898 else
899 fit_double_type (int1l, int1h, &int1l, &int1h, type);
900 int2l = TREE_INT_CST_LOW (arg2);
901 int2h = TREE_INT_CST_HIGH (arg2);
903 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
904 &quol, &quoh, &reml, &remh);
905 if (remh != 0 || reml != 0)
906 return NULL_TREE;
908 return build_int_cst_wide (type, quol, quoh);
911 /* This is nonzero if we should defer warnings about undefined
912 overflow. This facility exists because these warnings are a
913 special case. The code to estimate loop iterations does not want
914 to issue any warnings, since it works with expressions which do not
915 occur in user code. Various bits of cleanup code call fold(), but
916 only use the result if it has certain characteristics (e.g., is a
917 constant); that code only wants to issue a warning if the result is
918 used. */
920 static int fold_deferring_overflow_warnings;
922 /* If a warning about undefined overflow is deferred, this is the
923 warning. Note that this may cause us to turn two warnings into
924 one, but that is fine since it is sufficient to only give one
925 warning per expression. */
927 static const char* fold_deferred_overflow_warning;
929 /* If a warning about undefined overflow is deferred, this is the
930 level at which the warning should be emitted. */
932 static enum warn_strict_overflow_code fold_deferred_overflow_code;
934 /* Start deferring overflow warnings. We could use a stack here to
935 permit nested calls, but at present it is not necessary. */
937 void
938 fold_defer_overflow_warnings (void)
940 ++fold_deferring_overflow_warnings;
943 /* Stop deferring overflow warnings. If there is a pending warning,
944 and ISSUE is true, then issue the warning if appropriate. STMT is
945 the statement with which the warning should be associated (used for
946 location information); STMT may be NULL. CODE is the level of the
947 warning--a warn_strict_overflow_code value. This function will use
948 the smaller of CODE and the deferred code when deciding whether to
949 issue the warning. CODE may be zero to mean to always use the
950 deferred code. */
952 void
953 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
955 const char *warnmsg;
956 location_t locus;
958 gcc_assert (fold_deferring_overflow_warnings > 0);
959 --fold_deferring_overflow_warnings;
960 if (fold_deferring_overflow_warnings > 0)
962 if (fold_deferred_overflow_warning != NULL
963 && code != 0
964 && code < (int) fold_deferred_overflow_code)
965 fold_deferred_overflow_code = code;
966 return;
969 warnmsg = fold_deferred_overflow_warning;
970 fold_deferred_overflow_warning = NULL;
972 if (!issue || warnmsg == NULL)
973 return;
975 if (gimple_no_warning_p (stmt))
976 return;
978 /* Use the smallest code level when deciding to issue the
979 warning. */
980 if (code == 0 || code > (int) fold_deferred_overflow_code)
981 code = fold_deferred_overflow_code;
983 if (!issue_strict_overflow_warning (code))
984 return;
986 if (stmt == NULL)
987 locus = input_location;
988 else
989 locus = gimple_location (stmt);
990 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
993 /* Stop deferring overflow warnings, ignoring any deferred
994 warnings. */
996 void
997 fold_undefer_and_ignore_overflow_warnings (void)
999 fold_undefer_overflow_warnings (false, NULL, 0);
1002 /* Whether we are deferring overflow warnings. */
1004 bool
1005 fold_deferring_overflow_warnings_p (void)
1007 return fold_deferring_overflow_warnings > 0;
1010 /* This is called when we fold something based on the fact that signed
1011 overflow is undefined. */
1013 static void
1014 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016 if (fold_deferring_overflow_warnings > 0)
1018 if (fold_deferred_overflow_warning == NULL
1019 || wc < fold_deferred_overflow_code)
1021 fold_deferred_overflow_warning = gmsgid;
1022 fold_deferred_overflow_code = wc;
1025 else if (issue_strict_overflow_warning (wc))
1026 warning (OPT_Wstrict_overflow, gmsgid);
1029 /* Return true if the built-in mathematical function specified by CODE
1030 is odd, i.e. -f(x) == f(-x). */
1032 static bool
1033 negate_mathfn_p (enum built_in_function code)
1035 switch (code)
1037 CASE_FLT_FN (BUILT_IN_ASIN):
1038 CASE_FLT_FN (BUILT_IN_ASINH):
1039 CASE_FLT_FN (BUILT_IN_ATAN):
1040 CASE_FLT_FN (BUILT_IN_ATANH):
1041 CASE_FLT_FN (BUILT_IN_CASIN):
1042 CASE_FLT_FN (BUILT_IN_CASINH):
1043 CASE_FLT_FN (BUILT_IN_CATAN):
1044 CASE_FLT_FN (BUILT_IN_CATANH):
1045 CASE_FLT_FN (BUILT_IN_CBRT):
1046 CASE_FLT_FN (BUILT_IN_CPROJ):
1047 CASE_FLT_FN (BUILT_IN_CSIN):
1048 CASE_FLT_FN (BUILT_IN_CSINH):
1049 CASE_FLT_FN (BUILT_IN_CTAN):
1050 CASE_FLT_FN (BUILT_IN_CTANH):
1051 CASE_FLT_FN (BUILT_IN_ERF):
1052 CASE_FLT_FN (BUILT_IN_LLROUND):
1053 CASE_FLT_FN (BUILT_IN_LROUND):
1054 CASE_FLT_FN (BUILT_IN_ROUND):
1055 CASE_FLT_FN (BUILT_IN_SIN):
1056 CASE_FLT_FN (BUILT_IN_SINH):
1057 CASE_FLT_FN (BUILT_IN_TAN):
1058 CASE_FLT_FN (BUILT_IN_TANH):
1059 CASE_FLT_FN (BUILT_IN_TRUNC):
1060 return true;
1062 CASE_FLT_FN (BUILT_IN_LLRINT):
1063 CASE_FLT_FN (BUILT_IN_LRINT):
1064 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065 CASE_FLT_FN (BUILT_IN_RINT):
1066 return !flag_rounding_math;
1068 default:
1069 break;
1071 return false;
1074 /* Check whether we may negate an integer constant T without causing
1075 overflow. */
1077 bool
1078 may_negate_without_overflow_p (const_tree t)
1080 unsigned HOST_WIDE_INT val;
1081 unsigned int prec;
1082 tree type;
1084 gcc_assert (TREE_CODE (t) == INTEGER_CST);
1086 type = TREE_TYPE (t);
1087 if (TYPE_UNSIGNED (type))
1088 return false;
1090 prec = TYPE_PRECISION (type);
1091 if (prec > HOST_BITS_PER_WIDE_INT)
1093 if (TREE_INT_CST_LOW (t) != 0)
1094 return true;
1095 prec -= HOST_BITS_PER_WIDE_INT;
1096 val = TREE_INT_CST_HIGH (t);
1098 else
1099 val = TREE_INT_CST_LOW (t);
1100 if (prec < HOST_BITS_PER_WIDE_INT)
1101 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1105 /* Determine whether an expression T can be cheaply negated using
1106 the function negate_expr without introducing undefined overflow. */
1108 static bool
1109 negate_expr_p (tree t)
1111 tree type;
1113 if (t == 0)
1114 return false;
1116 type = TREE_TYPE (t);
1118 STRIP_SIGN_NOPS (t);
1119 switch (TREE_CODE (t))
1121 case INTEGER_CST:
1122 if (TYPE_OVERFLOW_WRAPS (type))
1123 return true;
1125 /* Check that -CST will not overflow type. */
1126 return may_negate_without_overflow_p (t);
1127 case BIT_NOT_EXPR:
1128 return (INTEGRAL_TYPE_P (type)
1129 && TYPE_OVERFLOW_WRAPS (type));
1131 case FIXED_CST:
1132 case REAL_CST:
1133 case NEGATE_EXPR:
1134 return true;
1136 case COMPLEX_CST:
1137 return negate_expr_p (TREE_REALPART (t))
1138 && negate_expr_p (TREE_IMAGPART (t));
1140 case COMPLEX_EXPR:
1141 return negate_expr_p (TREE_OPERAND (t, 0))
1142 && negate_expr_p (TREE_OPERAND (t, 1));
1144 case CONJ_EXPR:
1145 return negate_expr_p (TREE_OPERAND (t, 0));
1147 case PLUS_EXPR:
1148 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1150 return false;
1151 /* -(A + B) -> (-B) - A. */
1152 if (negate_expr_p (TREE_OPERAND (t, 1))
1153 && reorder_operands_p (TREE_OPERAND (t, 0),
1154 TREE_OPERAND (t, 1)))
1155 return true;
1156 /* -(A + B) -> (-A) - B. */
1157 return negate_expr_p (TREE_OPERAND (t, 0));
1159 case MINUS_EXPR:
1160 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1161 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163 && reorder_operands_p (TREE_OPERAND (t, 0),
1164 TREE_OPERAND (t, 1));
1166 case MULT_EXPR:
1167 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1168 break;
1170 /* Fall through. */
1172 case RDIV_EXPR:
1173 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174 return negate_expr_p (TREE_OPERAND (t, 1))
1175 || negate_expr_p (TREE_OPERAND (t, 0));
1176 break;
1178 case TRUNC_DIV_EXPR:
1179 case ROUND_DIV_EXPR:
1180 case FLOOR_DIV_EXPR:
1181 case CEIL_DIV_EXPR:
1182 case EXACT_DIV_EXPR:
1183 /* In general we can't negate A / B, because if A is INT_MIN and
1184 B is 1, we may turn this into INT_MIN / -1 which is undefined
1185 and actually traps on some architectures. But if overflow is
1186 undefined, we can negate, because - (INT_MIN / 1) is an
1187 overflow. */
1188 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1190 break;
1191 return negate_expr_p (TREE_OPERAND (t, 1))
1192 || negate_expr_p (TREE_OPERAND (t, 0));
1194 case NOP_EXPR:
1195 /* Negate -((double)float) as (double)(-float). */
1196 if (TREE_CODE (type) == REAL_TYPE)
1198 tree tem = strip_float_extensions (t);
1199 if (tem != t)
1200 return negate_expr_p (tem);
1202 break;
1204 case CALL_EXPR:
1205 /* Negate -f(x) as f(-x). */
1206 if (negate_mathfn_p (builtin_mathfn_code (t)))
1207 return negate_expr_p (CALL_EXPR_ARG (t, 0));
1208 break;
1210 case RSHIFT_EXPR:
1211 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1212 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214 tree op1 = TREE_OPERAND (t, 1);
1215 if (TREE_INT_CST_HIGH (op1) == 0
1216 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217 == TREE_INT_CST_LOW (op1))
1218 return true;
1220 break;
1222 default:
1223 break;
1225 return false;
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229 simplification is possible.
1230 If negate_expr_p would return true for T, NULL_TREE will never be
1231 returned. */
1233 static tree
1234 fold_negate_expr (tree t)
1236 tree type = TREE_TYPE (t);
1237 tree tem;
1239 switch (TREE_CODE (t))
1241 /* Convert - (~A) to A + 1. */
1242 case BIT_NOT_EXPR:
1243 if (INTEGRAL_TYPE_P (type))
1244 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245 build_int_cst (type, 1));
1246 break;
1248 case INTEGER_CST:
1249 tem = fold_negate_const (t, type);
1250 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251 || !TYPE_OVERFLOW_TRAPS (type))
1252 return tem;
1253 break;
1255 case REAL_CST:
1256 tem = fold_negate_const (t, type);
1257 /* Two's complement FP formats, such as c4x, may overflow. */
1258 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1259 return tem;
1260 break;
1262 case FIXED_CST:
1263 tem = fold_negate_const (t, type);
1264 return tem;
1266 case COMPLEX_CST:
1268 tree rpart = negate_expr (TREE_REALPART (t));
1269 tree ipart = negate_expr (TREE_IMAGPART (t));
1271 if ((TREE_CODE (rpart) == REAL_CST
1272 && TREE_CODE (ipart) == REAL_CST)
1273 || (TREE_CODE (rpart) == INTEGER_CST
1274 && TREE_CODE (ipart) == INTEGER_CST))
1275 return build_complex (type, rpart, ipart);
1277 break;
1279 case COMPLEX_EXPR:
1280 if (negate_expr_p (t))
1281 return fold_build2 (COMPLEX_EXPR, type,
1282 fold_negate_expr (TREE_OPERAND (t, 0)),
1283 fold_negate_expr (TREE_OPERAND (t, 1)));
1284 break;
1286 case CONJ_EXPR:
1287 if (negate_expr_p (t))
1288 return fold_build1 (CONJ_EXPR, type,
1289 fold_negate_expr (TREE_OPERAND (t, 0)));
1290 break;
1292 case NEGATE_EXPR:
1293 return TREE_OPERAND (t, 0);
1295 case PLUS_EXPR:
1296 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1299 /* -(A + B) -> (-B) - A. */
1300 if (negate_expr_p (TREE_OPERAND (t, 1))
1301 && reorder_operands_p (TREE_OPERAND (t, 0),
1302 TREE_OPERAND (t, 1)))
1304 tem = negate_expr (TREE_OPERAND (t, 1));
1305 return fold_build2 (MINUS_EXPR, type,
1306 tem, TREE_OPERAND (t, 0));
1309 /* -(A + B) -> (-A) - B. */
1310 if (negate_expr_p (TREE_OPERAND (t, 0)))
1312 tem = negate_expr (TREE_OPERAND (t, 0));
1313 return fold_build2 (MINUS_EXPR, type,
1314 tem, TREE_OPERAND (t, 1));
1317 break;
1319 case MINUS_EXPR:
1320 /* - (A - B) -> B - A */
1321 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324 return fold_build2 (MINUS_EXPR, type,
1325 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1326 break;
1328 case MULT_EXPR:
1329 if (TYPE_UNSIGNED (type))
1330 break;
1332 /* Fall through. */
1334 case RDIV_EXPR:
1335 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1337 tem = TREE_OPERAND (t, 1);
1338 if (negate_expr_p (tem))
1339 return fold_build2 (TREE_CODE (t), type,
1340 TREE_OPERAND (t, 0), negate_expr (tem));
1341 tem = TREE_OPERAND (t, 0);
1342 if (negate_expr_p (tem))
1343 return fold_build2 (TREE_CODE (t), type,
1344 negate_expr (tem), TREE_OPERAND (t, 1));
1346 break;
1348 case TRUNC_DIV_EXPR:
1349 case ROUND_DIV_EXPR:
1350 case FLOOR_DIV_EXPR:
1351 case CEIL_DIV_EXPR:
1352 case EXACT_DIV_EXPR:
1353 /* In general we can't negate A / B, because if A is INT_MIN and
1354 B is 1, we may turn this into INT_MIN / -1 which is undefined
1355 and actually traps on some architectures. But if overflow is
1356 undefined, we can negate, because - (INT_MIN / 1) is an
1357 overflow. */
1358 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1360 const char * const warnmsg = G_("assuming signed overflow does not "
1361 "occur when negating a division");
1362 tem = TREE_OPERAND (t, 1);
1363 if (negate_expr_p (tem))
1365 if (INTEGRAL_TYPE_P (type)
1366 && (TREE_CODE (tem) != INTEGER_CST
1367 || integer_onep (tem)))
1368 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369 return fold_build2 (TREE_CODE (t), type,
1370 TREE_OPERAND (t, 0), negate_expr (tem));
1372 tem = TREE_OPERAND (t, 0);
1373 if (negate_expr_p (tem))
1375 if (INTEGRAL_TYPE_P (type)
1376 && (TREE_CODE (tem) != INTEGER_CST
1377 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379 return fold_build2 (TREE_CODE (t), type,
1380 negate_expr (tem), TREE_OPERAND (t, 1));
1383 break;
1385 case NOP_EXPR:
1386 /* Convert -((double)float) into (double)(-float). */
1387 if (TREE_CODE (type) == REAL_TYPE)
1389 tem = strip_float_extensions (t);
1390 if (tem != t && negate_expr_p (tem))
1391 return fold_convert (type, negate_expr (tem));
1393 break;
1395 case CALL_EXPR:
1396 /* Negate -f(x) as f(-x). */
1397 if (negate_mathfn_p (builtin_mathfn_code (t))
1398 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1400 tree fndecl, arg;
1402 fndecl = get_callee_fndecl (t);
1403 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404 return build_call_expr (fndecl, 1, arg);
1406 break;
1408 case RSHIFT_EXPR:
1409 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1410 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1412 tree op1 = TREE_OPERAND (t, 1);
1413 if (TREE_INT_CST_HIGH (op1) == 0
1414 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415 == TREE_INT_CST_LOW (op1))
1417 tree ntype = TYPE_UNSIGNED (type)
1418 ? signed_type_for (type)
1419 : unsigned_type_for (type);
1420 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1421 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1422 return fold_convert (type, temp);
1425 break;
1427 default:
1428 break;
1431 return NULL_TREE;
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1436 return NULL_TREE. */
1438 static tree
1439 negate_expr (tree t)
1441 tree type, tem;
1443 if (t == NULL_TREE)
1444 return NULL_TREE;
1446 type = TREE_TYPE (t);
1447 STRIP_SIGN_NOPS (t);
1449 tem = fold_negate_expr (t);
1450 if (!tem)
1451 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1452 return fold_convert (type, tem);
1455 /* Split a tree IN into a constant, literal and variable parts that could be
1456 combined with CODE to make IN. "constant" means an expression with
1457 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1458 commutative arithmetic operation. Store the constant part into *CONP,
1459 the literal in *LITP and return the variable part. If a part isn't
1460 present, set it to null. If the tree does not decompose in this way,
1461 return the entire tree as the variable part and the other parts as null.
1463 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1464 case, we negate an operand that was subtracted. Except if it is a
1465 literal for which we use *MINUS_LITP instead.
1467 If NEGATE_P is true, we are negating all of IN, again except a literal
1468 for which we use *MINUS_LITP instead.
1470 If IN is itself a literal or constant, return it as appropriate.
1472 Note that we do not guarantee that any of the three values will be the
1473 same type as IN, but they will have the same signedness and mode. */
1475 static tree
1476 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1477 tree *minus_litp, int negate_p)
1479 tree var = 0;
1481 *conp = 0;
1482 *litp = 0;
1483 *minus_litp = 0;
1485 /* Strip any conversions that don't change the machine mode or signedness. */
1486 STRIP_SIGN_NOPS (in);
1488 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1489 || TREE_CODE (in) == FIXED_CST)
1490 *litp = in;
1491 else if (TREE_CODE (in) == code
1492 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1493 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1494 /* We can associate addition and subtraction together (even
1495 though the C standard doesn't say so) for integers because
1496 the value is not affected. For reals, the value might be
1497 affected, so we can't. */
1498 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1499 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501 tree op0 = TREE_OPERAND (in, 0);
1502 tree op1 = TREE_OPERAND (in, 1);
1503 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1504 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506 /* First see if either of the operands is a literal, then a constant. */
1507 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1508 || TREE_CODE (op0) == FIXED_CST)
1509 *litp = op0, op0 = 0;
1510 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1511 || TREE_CODE (op1) == FIXED_CST)
1512 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514 if (op0 != 0 && TREE_CONSTANT (op0))
1515 *conp = op0, op0 = 0;
1516 else if (op1 != 0 && TREE_CONSTANT (op1))
1517 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519 /* If we haven't dealt with either operand, this is not a case we can
1520 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1521 if (op0 != 0 && op1 != 0)
1522 var = in;
1523 else if (op0 != 0)
1524 var = op0;
1525 else
1526 var = op1, neg_var_p = neg1_p;
1528 /* Now do any needed negations. */
1529 if (neg_litp_p)
1530 *minus_litp = *litp, *litp = 0;
1531 if (neg_conp_p)
1532 *conp = negate_expr (*conp);
1533 if (neg_var_p)
1534 var = negate_expr (var);
1536 else if (TREE_CONSTANT (in))
1537 *conp = in;
1538 else
1539 var = in;
1541 if (negate_p)
1543 if (*litp)
1544 *minus_litp = *litp, *litp = 0;
1545 else if (*minus_litp)
1546 *litp = *minus_litp, *minus_litp = 0;
1547 *conp = negate_expr (*conp);
1548 var = negate_expr (var);
1551 return var;
1554 /* Re-associate trees split by the above function. T1 and T2 are either
1555 expressions to associate or null. Return the new expression, if any. If
1556 we build an operation, do it in TYPE and with CODE. */
1558 static tree
1559 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1561 if (t1 == 0)
1562 return t2;
1563 else if (t2 == 0)
1564 return t1;
1566 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1567 try to fold this since we will have infinite recursion. But do
1568 deal with any NEGATE_EXPRs. */
1569 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1570 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1572 if (code == PLUS_EXPR)
1574 if (TREE_CODE (t1) == NEGATE_EXPR)
1575 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1576 fold_convert (type, TREE_OPERAND (t1, 0)));
1577 else if (TREE_CODE (t2) == NEGATE_EXPR)
1578 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1579 fold_convert (type, TREE_OPERAND (t2, 0)));
1580 else if (integer_zerop (t2))
1581 return fold_convert (type, t1);
1583 else if (code == MINUS_EXPR)
1585 if (integer_zerop (t2))
1586 return fold_convert (type, t1);
1589 return build2 (code, type, fold_convert (type, t1),
1590 fold_convert (type, t2));
1593 return fold_build2 (code, type, fold_convert (type, t1),
1594 fold_convert (type, t2));
1597 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1598 for use in int_const_binop, size_binop and size_diffop. */
1600 static bool
1601 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1603 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1604 return false;
1605 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1606 return false;
1608 switch (code)
1610 case LSHIFT_EXPR:
1611 case RSHIFT_EXPR:
1612 case LROTATE_EXPR:
1613 case RROTATE_EXPR:
1614 return true;
1616 default:
1617 break;
1620 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1621 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1622 && TYPE_MODE (type1) == TYPE_MODE (type2);
1626 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1627 to produce a new constant. Return NULL_TREE if we don't know how
1628 to evaluate CODE at compile-time.
1630 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1632 tree
1633 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1635 unsigned HOST_WIDE_INT int1l, int2l;
1636 HOST_WIDE_INT int1h, int2h;
1637 unsigned HOST_WIDE_INT low;
1638 HOST_WIDE_INT hi;
1639 unsigned HOST_WIDE_INT garbagel;
1640 HOST_WIDE_INT garbageh;
1641 tree t;
1642 tree type = TREE_TYPE (arg1);
1643 int uns = TYPE_UNSIGNED (type);
1644 int is_sizetype
1645 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1646 int overflow = 0;
1648 int1l = TREE_INT_CST_LOW (arg1);
1649 int1h = TREE_INT_CST_HIGH (arg1);
1650 int2l = TREE_INT_CST_LOW (arg2);
1651 int2h = TREE_INT_CST_HIGH (arg2);
1653 switch (code)
1655 case BIT_IOR_EXPR:
1656 low = int1l | int2l, hi = int1h | int2h;
1657 break;
1659 case BIT_XOR_EXPR:
1660 low = int1l ^ int2l, hi = int1h ^ int2h;
1661 break;
1663 case BIT_AND_EXPR:
1664 low = int1l & int2l, hi = int1h & int2h;
1665 break;
1667 case RSHIFT_EXPR:
1668 int2l = -int2l;
1669 case LSHIFT_EXPR:
1670 /* It's unclear from the C standard whether shifts can overflow.
1671 The following code ignores overflow; perhaps a C standard
1672 interpretation ruling is needed. */
1673 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1674 &low, &hi, !uns);
1675 break;
1677 case RROTATE_EXPR:
1678 int2l = - int2l;
1679 case LROTATE_EXPR:
1680 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1681 &low, &hi);
1682 break;
1684 case PLUS_EXPR:
1685 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1686 break;
1688 case MINUS_EXPR:
1689 neg_double (int2l, int2h, &low, &hi);
1690 add_double (int1l, int1h, low, hi, &low, &hi);
1691 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1692 break;
1694 case MULT_EXPR:
1695 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1696 break;
1698 case TRUNC_DIV_EXPR:
1699 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1700 case EXACT_DIV_EXPR:
1701 /* This is a shortcut for a common special case. */
1702 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1703 && !TREE_OVERFLOW (arg1)
1704 && !TREE_OVERFLOW (arg2)
1705 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1707 if (code == CEIL_DIV_EXPR)
1708 int1l += int2l - 1;
1710 low = int1l / int2l, hi = 0;
1711 break;
1714 /* ... fall through ... */
1716 case ROUND_DIV_EXPR:
1717 if (int2h == 0 && int2l == 0)
1718 return NULL_TREE;
1719 if (int2h == 0 && int2l == 1)
1721 low = int1l, hi = int1h;
1722 break;
1724 if (int1l == int2l && int1h == int2h
1725 && ! (int1l == 0 && int1h == 0))
1727 low = 1, hi = 0;
1728 break;
1730 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1731 &low, &hi, &garbagel, &garbageh);
1732 break;
1734 case TRUNC_MOD_EXPR:
1735 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1736 /* This is a shortcut for a common special case. */
1737 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1738 && !TREE_OVERFLOW (arg1)
1739 && !TREE_OVERFLOW (arg2)
1740 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1742 if (code == CEIL_MOD_EXPR)
1743 int1l += int2l - 1;
1744 low = int1l % int2l, hi = 0;
1745 break;
1748 /* ... fall through ... */
1750 case ROUND_MOD_EXPR:
1751 if (int2h == 0 && int2l == 0)
1752 return NULL_TREE;
1753 overflow = div_and_round_double (code, uns,
1754 int1l, int1h, int2l, int2h,
1755 &garbagel, &garbageh, &low, &hi);
1756 break;
1758 case MIN_EXPR:
1759 case MAX_EXPR:
1760 if (uns)
1761 low = (((unsigned HOST_WIDE_INT) int1h
1762 < (unsigned HOST_WIDE_INT) int2h)
1763 || (((unsigned HOST_WIDE_INT) int1h
1764 == (unsigned HOST_WIDE_INT) int2h)
1765 && int1l < int2l));
1766 else
1767 low = (int1h < int2h
1768 || (int1h == int2h && int1l < int2l));
1770 if (low == (code == MIN_EXPR))
1771 low = int1l, hi = int1h;
1772 else
1773 low = int2l, hi = int2h;
1774 break;
1776 default:
1777 return NULL_TREE;
1780 if (notrunc)
1782 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1784 /* Propagate overflow flags ourselves. */
1785 if (((!uns || is_sizetype) && overflow)
1786 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1788 t = copy_node (t);
1789 TREE_OVERFLOW (t) = 1;
1792 else
1793 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1794 ((!uns || is_sizetype) && overflow)
1795 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1797 return t;
1800 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1801 constant. We assume ARG1 and ARG2 have the same data type, or at least
1802 are the same kind of constant and the same machine mode. Return zero if
1803 combining the constants is not allowed in the current operating mode.
1805 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1807 static tree
1808 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1810 /* Sanity check for the recursive cases. */
1811 if (!arg1 || !arg2)
1812 return NULL_TREE;
1814 STRIP_NOPS (arg1);
1815 STRIP_NOPS (arg2);
1817 if (TREE_CODE (arg1) == INTEGER_CST)
1818 return int_const_binop (code, arg1, arg2, notrunc);
1820 if (TREE_CODE (arg1) == REAL_CST)
1822 enum machine_mode mode;
1823 REAL_VALUE_TYPE d1;
1824 REAL_VALUE_TYPE d2;
1825 REAL_VALUE_TYPE value;
1826 REAL_VALUE_TYPE result;
1827 bool inexact;
1828 tree t, type;
1830 /* The following codes are handled by real_arithmetic. */
1831 switch (code)
1833 case PLUS_EXPR:
1834 case MINUS_EXPR:
1835 case MULT_EXPR:
1836 case RDIV_EXPR:
1837 case MIN_EXPR:
1838 case MAX_EXPR:
1839 break;
1841 default:
1842 return NULL_TREE;
1845 d1 = TREE_REAL_CST (arg1);
1846 d2 = TREE_REAL_CST (arg2);
1848 type = TREE_TYPE (arg1);
1849 mode = TYPE_MODE (type);
1851 /* Don't perform operation if we honor signaling NaNs and
1852 either operand is a NaN. */
1853 if (HONOR_SNANS (mode)
1854 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1855 return NULL_TREE;
1857 /* Don't perform operation if it would raise a division
1858 by zero exception. */
1859 if (code == RDIV_EXPR
1860 && REAL_VALUES_EQUAL (d2, dconst0)
1861 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1862 return NULL_TREE;
1864 /* If either operand is a NaN, just return it. Otherwise, set up
1865 for floating-point trap; we return an overflow. */
1866 if (REAL_VALUE_ISNAN (d1))
1867 return arg1;
1868 else if (REAL_VALUE_ISNAN (d2))
1869 return arg2;
1871 inexact = real_arithmetic (&value, code, &d1, &d2);
1872 real_convert (&result, mode, &value);
1874 /* Don't constant fold this floating point operation if
1875 the result has overflowed and flag_trapping_math. */
1876 if (flag_trapping_math
1877 && MODE_HAS_INFINITIES (mode)
1878 && REAL_VALUE_ISINF (result)
1879 && !REAL_VALUE_ISINF (d1)
1880 && !REAL_VALUE_ISINF (d2))
1881 return NULL_TREE;
1883 /* Don't constant fold this floating point operation if the
1884 result may dependent upon the run-time rounding mode and
1885 flag_rounding_math is set, or if GCC's software emulation
1886 is unable to accurately represent the result. */
1887 if ((flag_rounding_math
1888 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1889 && (inexact || !real_identical (&result, &value)))
1890 return NULL_TREE;
1892 t = build_real (type, result);
1894 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1895 return t;
1898 if (TREE_CODE (arg1) == FIXED_CST)
1900 FIXED_VALUE_TYPE f1;
1901 FIXED_VALUE_TYPE f2;
1902 FIXED_VALUE_TYPE result;
1903 tree t, type;
1904 int sat_p;
1905 bool overflow_p;
1907 /* The following codes are handled by fixed_arithmetic. */
1908 switch (code)
1910 case PLUS_EXPR:
1911 case MINUS_EXPR:
1912 case MULT_EXPR:
1913 case TRUNC_DIV_EXPR:
1914 f2 = TREE_FIXED_CST (arg2);
1915 break;
1917 case LSHIFT_EXPR:
1918 case RSHIFT_EXPR:
1919 f2.data.high = TREE_INT_CST_HIGH (arg2);
1920 f2.data.low = TREE_INT_CST_LOW (arg2);
1921 f2.mode = SImode;
1922 break;
1924 default:
1925 return NULL_TREE;
1928 f1 = TREE_FIXED_CST (arg1);
1929 type = TREE_TYPE (arg1);
1930 sat_p = TYPE_SATURATING (type);
1931 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1932 t = build_fixed (type, result);
1933 /* Propagate overflow flags. */
1934 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1936 TREE_OVERFLOW (t) = 1;
1937 TREE_CONSTANT_OVERFLOW (t) = 1;
1939 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1940 TREE_CONSTANT_OVERFLOW (t) = 1;
1941 return t;
1944 if (TREE_CODE (arg1) == COMPLEX_CST)
1946 tree type = TREE_TYPE (arg1);
1947 tree r1 = TREE_REALPART (arg1);
1948 tree i1 = TREE_IMAGPART (arg1);
1949 tree r2 = TREE_REALPART (arg2);
1950 tree i2 = TREE_IMAGPART (arg2);
1951 tree real, imag;
1953 switch (code)
1955 case PLUS_EXPR:
1956 case MINUS_EXPR:
1957 real = const_binop (code, r1, r2, notrunc);
1958 imag = const_binop (code, i1, i2, notrunc);
1959 break;
1961 case MULT_EXPR:
1962 real = const_binop (MINUS_EXPR,
1963 const_binop (MULT_EXPR, r1, r2, notrunc),
1964 const_binop (MULT_EXPR, i1, i2, notrunc),
1965 notrunc);
1966 imag = const_binop (PLUS_EXPR,
1967 const_binop (MULT_EXPR, r1, i2, notrunc),
1968 const_binop (MULT_EXPR, i1, r2, notrunc),
1969 notrunc);
1970 break;
1972 case RDIV_EXPR:
1974 tree magsquared
1975 = const_binop (PLUS_EXPR,
1976 const_binop (MULT_EXPR, r2, r2, notrunc),
1977 const_binop (MULT_EXPR, i2, i2, notrunc),
1978 notrunc);
1979 tree t1
1980 = const_binop (PLUS_EXPR,
1981 const_binop (MULT_EXPR, r1, r2, notrunc),
1982 const_binop (MULT_EXPR, i1, i2, notrunc),
1983 notrunc);
1984 tree t2
1985 = const_binop (MINUS_EXPR,
1986 const_binop (MULT_EXPR, i1, r2, notrunc),
1987 const_binop (MULT_EXPR, r1, i2, notrunc),
1988 notrunc);
1990 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1991 code = TRUNC_DIV_EXPR;
1993 real = const_binop (code, t1, magsquared, notrunc);
1994 imag = const_binop (code, t2, magsquared, notrunc);
1996 break;
1998 default:
1999 return NULL_TREE;
2002 if (real && imag)
2003 return build_complex (type, real, imag);
2006 return NULL_TREE;
2009 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2010 indicates which particular sizetype to create. */
2012 tree
2013 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2015 return build_int_cst (sizetype_tab[(int) kind], number);
2018 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2019 is a tree code. The type of the result is taken from the operands.
2020 Both must be equivalent integer types, ala int_binop_types_match_p.
2021 If the operands are constant, so is the result. */
2023 tree
2024 size_binop (enum tree_code code, tree arg0, tree arg1)
2026 tree type = TREE_TYPE (arg0);
2028 if (arg0 == error_mark_node || arg1 == error_mark_node)
2029 return error_mark_node;
2031 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2032 TREE_TYPE (arg1)));
2034 /* Handle the special case of two integer constants faster. */
2035 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2037 /* And some specific cases even faster than that. */
2038 if (code == PLUS_EXPR)
2040 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2041 return arg1;
2042 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2043 return arg0;
2045 else if (code == MINUS_EXPR)
2047 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2048 return arg0;
2050 else if (code == MULT_EXPR)
2052 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2053 return arg1;
2056 /* Handle general case of two integer constants. */
2057 return int_const_binop (code, arg0, arg1, 0);
2060 return fold_build2 (code, type, arg0, arg1);
2063 /* Given two values, either both of sizetype or both of bitsizetype,
2064 compute the difference between the two values. Return the value
2065 in signed type corresponding to the type of the operands. */
2067 tree
2068 size_diffop (tree arg0, tree arg1)
2070 tree type = TREE_TYPE (arg0);
2071 tree ctype;
2073 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2074 TREE_TYPE (arg1)));
2076 /* If the type is already signed, just do the simple thing. */
2077 if (!TYPE_UNSIGNED (type))
2078 return size_binop (MINUS_EXPR, arg0, arg1);
2080 if (type == sizetype)
2081 ctype = ssizetype;
2082 else if (type == bitsizetype)
2083 ctype = sbitsizetype;
2084 else
2085 ctype = signed_type_for (type);
2087 /* If either operand is not a constant, do the conversions to the signed
2088 type and subtract. The hardware will do the right thing with any
2089 overflow in the subtraction. */
2090 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2092 fold_convert (ctype, arg1));
2094 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2095 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2096 overflow) and negate (which can't either). Special-case a result
2097 of zero while we're here. */
2098 if (tree_int_cst_equal (arg0, arg1))
2099 return build_int_cst (ctype, 0);
2100 else if (tree_int_cst_lt (arg1, arg0))
2101 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2102 else
2103 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2104 fold_convert (ctype, size_binop (MINUS_EXPR,
2105 arg1, arg0)));
2108 /* A subroutine of fold_convert_const handling conversions of an
2109 INTEGER_CST to another integer type. */
2111 static tree
2112 fold_convert_const_int_from_int (tree type, const_tree arg1)
2114 tree t;
2116 /* Given an integer constant, make new constant with new type,
2117 appropriately sign-extended or truncated. */
2118 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2119 TREE_INT_CST_HIGH (arg1),
2120 /* Don't set the overflow when
2121 converting from a pointer, */
2122 !POINTER_TYPE_P (TREE_TYPE (arg1))
2123 /* or to a sizetype with same signedness
2124 and the precision is unchanged.
2125 ??? sizetype is always sign-extended,
2126 but its signedness depends on the
2127 frontend. Thus we see spurious overflows
2128 here if we do not check this. */
2129 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2130 == TYPE_PRECISION (type))
2131 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2132 == TYPE_UNSIGNED (type))
2133 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2134 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2135 || (TREE_CODE (type) == INTEGER_TYPE
2136 && TYPE_IS_SIZETYPE (type)))),
2137 (TREE_INT_CST_HIGH (arg1) < 0
2138 && (TYPE_UNSIGNED (type)
2139 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2140 | TREE_OVERFLOW (arg1));
2142 return t;
2145 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2146 to an integer type. */
2148 static tree
2149 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2151 int overflow = 0;
2152 tree t;
2154 /* The following code implements the floating point to integer
2155 conversion rules required by the Java Language Specification,
2156 that IEEE NaNs are mapped to zero and values that overflow
2157 the target precision saturate, i.e. values greater than
2158 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2159 are mapped to INT_MIN. These semantics are allowed by the
2160 C and C++ standards that simply state that the behavior of
2161 FP-to-integer conversion is unspecified upon overflow. */
2163 HOST_WIDE_INT high, low;
2164 REAL_VALUE_TYPE r;
2165 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2167 switch (code)
2169 case FIX_TRUNC_EXPR:
2170 real_trunc (&r, VOIDmode, &x);
2171 break;
2173 default:
2174 gcc_unreachable ();
2177 /* If R is NaN, return zero and show we have an overflow. */
2178 if (REAL_VALUE_ISNAN (r))
2180 overflow = 1;
2181 high = 0;
2182 low = 0;
2185 /* See if R is less than the lower bound or greater than the
2186 upper bound. */
2188 if (! overflow)
2190 tree lt = TYPE_MIN_VALUE (type);
2191 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2192 if (REAL_VALUES_LESS (r, l))
2194 overflow = 1;
2195 high = TREE_INT_CST_HIGH (lt);
2196 low = TREE_INT_CST_LOW (lt);
2200 if (! overflow)
2202 tree ut = TYPE_MAX_VALUE (type);
2203 if (ut)
2205 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2206 if (REAL_VALUES_LESS (u, r))
2208 overflow = 1;
2209 high = TREE_INT_CST_HIGH (ut);
2210 low = TREE_INT_CST_LOW (ut);
2215 if (! overflow)
2216 REAL_VALUE_TO_INT (&low, &high, r);
2218 t = force_fit_type_double (type, low, high, -1,
2219 overflow | TREE_OVERFLOW (arg1));
2220 return t;
2223 /* A subroutine of fold_convert_const handling conversions of a
2224 FIXED_CST to an integer type. */
2226 static tree
2227 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2229 tree t;
2230 double_int temp, temp_trunc;
2231 unsigned int mode;
2233 /* Right shift FIXED_CST to temp by fbit. */
2234 temp = TREE_FIXED_CST (arg1).data;
2235 mode = TREE_FIXED_CST (arg1).mode;
2236 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2238 lshift_double (temp.low, temp.high,
2239 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2242 /* Left shift temp to temp_trunc by fbit. */
2243 lshift_double (temp.low, temp.high,
2244 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2245 &temp_trunc.low, &temp_trunc.high,
2246 SIGNED_FIXED_POINT_MODE_P (mode));
2248 else
2250 temp.low = 0;
2251 temp.high = 0;
2252 temp_trunc.low = 0;
2253 temp_trunc.high = 0;
2256 /* If FIXED_CST is negative, we need to round the value toward 0.
2257 By checking if the fractional bits are not zero to add 1 to temp. */
2258 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2259 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2261 double_int one;
2262 one.low = 1;
2263 one.high = 0;
2264 temp = double_int_add (temp, one);
2267 /* Given a fixed-point constant, make new constant with new type,
2268 appropriately sign-extended or truncated. */
2269 t = force_fit_type_double (type, temp.low, temp.high, -1,
2270 (temp.high < 0
2271 && (TYPE_UNSIGNED (type)
2272 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2273 | TREE_OVERFLOW (arg1));
2275 return t;
2278 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2279 to another floating point type. */
2281 static tree
2282 fold_convert_const_real_from_real (tree type, const_tree arg1)
2284 REAL_VALUE_TYPE value;
2285 tree t;
2287 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2288 t = build_real (type, value);
2290 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2291 return t;
2294 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2295 to a floating point type. */
2297 static tree
2298 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2300 REAL_VALUE_TYPE value;
2301 tree t;
2303 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2304 t = build_real (type, value);
2306 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2307 TREE_CONSTANT_OVERFLOW (t)
2308 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2309 return t;
2312 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2313 to another fixed-point type. */
2315 static tree
2316 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2318 FIXED_VALUE_TYPE value;
2319 tree t;
2320 bool overflow_p;
2322 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2323 TYPE_SATURATING (type));
2324 t = build_fixed (type, value);
2326 /* Propagate overflow flags. */
2327 if (overflow_p | TREE_OVERFLOW (arg1))
2329 TREE_OVERFLOW (t) = 1;
2330 TREE_CONSTANT_OVERFLOW (t) = 1;
2332 else if (TREE_CONSTANT_OVERFLOW (arg1))
2333 TREE_CONSTANT_OVERFLOW (t) = 1;
2334 return t;
2337 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2338 to a fixed-point type. */
2340 static tree
2341 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2343 FIXED_VALUE_TYPE value;
2344 tree t;
2345 bool overflow_p;
2347 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2348 TREE_INT_CST (arg1),
2349 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2350 TYPE_SATURATING (type));
2351 t = build_fixed (type, value);
2353 /* Propagate overflow flags. */
2354 if (overflow_p | TREE_OVERFLOW (arg1))
2356 TREE_OVERFLOW (t) = 1;
2357 TREE_CONSTANT_OVERFLOW (t) = 1;
2359 else if (TREE_CONSTANT_OVERFLOW (arg1))
2360 TREE_CONSTANT_OVERFLOW (t) = 1;
2361 return t;
2364 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2365 to a fixed-point type. */
2367 static tree
2368 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2370 FIXED_VALUE_TYPE value;
2371 tree t;
2372 bool overflow_p;
2374 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2375 &TREE_REAL_CST (arg1),
2376 TYPE_SATURATING (type));
2377 t = build_fixed (type, value);
2379 /* Propagate overflow flags. */
2380 if (overflow_p | TREE_OVERFLOW (arg1))
2382 TREE_OVERFLOW (t) = 1;
2383 TREE_CONSTANT_OVERFLOW (t) = 1;
2385 else if (TREE_CONSTANT_OVERFLOW (arg1))
2386 TREE_CONSTANT_OVERFLOW (t) = 1;
2387 return t;
2390 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2391 type TYPE. If no simplification can be done return NULL_TREE. */
2393 static tree
2394 fold_convert_const (enum tree_code code, tree type, tree arg1)
2396 if (TREE_TYPE (arg1) == type)
2397 return arg1;
2399 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2400 || TREE_CODE (type) == OFFSET_TYPE)
2402 if (TREE_CODE (arg1) == INTEGER_CST)
2403 return fold_convert_const_int_from_int (type, arg1);
2404 else if (TREE_CODE (arg1) == REAL_CST)
2405 return fold_convert_const_int_from_real (code, type, arg1);
2406 else if (TREE_CODE (arg1) == FIXED_CST)
2407 return fold_convert_const_int_from_fixed (type, arg1);
2409 else if (TREE_CODE (type) == REAL_TYPE)
2411 if (TREE_CODE (arg1) == INTEGER_CST)
2412 return build_real_from_int_cst (type, arg1);
2413 else if (TREE_CODE (arg1) == REAL_CST)
2414 return fold_convert_const_real_from_real (type, arg1);
2415 else if (TREE_CODE (arg1) == FIXED_CST)
2416 return fold_convert_const_real_from_fixed (type, arg1);
2418 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2420 if (TREE_CODE (arg1) == FIXED_CST)
2421 return fold_convert_const_fixed_from_fixed (type, arg1);
2422 else if (TREE_CODE (arg1) == INTEGER_CST)
2423 return fold_convert_const_fixed_from_int (type, arg1);
2424 else if (TREE_CODE (arg1) == REAL_CST)
2425 return fold_convert_const_fixed_from_real (type, arg1);
2427 return NULL_TREE;
2430 /* Construct a vector of zero elements of vector type TYPE. */
2432 static tree
2433 build_zero_vector (tree type)
2435 tree elem, list;
2436 int i, units;
2438 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2439 units = TYPE_VECTOR_SUBPARTS (type);
2441 list = NULL_TREE;
2442 for (i = 0; i < units; i++)
2443 list = tree_cons (NULL_TREE, elem, list);
2444 return build_vector (type, list);
2447 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2449 bool
2450 fold_convertible_p (const_tree type, const_tree arg)
2452 tree orig = TREE_TYPE (arg);
2454 if (type == orig)
2455 return true;
2457 if (TREE_CODE (arg) == ERROR_MARK
2458 || TREE_CODE (type) == ERROR_MARK
2459 || TREE_CODE (orig) == ERROR_MARK)
2460 return false;
2462 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2463 return true;
2465 switch (TREE_CODE (type))
2467 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2468 case POINTER_TYPE: case REFERENCE_TYPE:
2469 case OFFSET_TYPE:
2470 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2471 || TREE_CODE (orig) == OFFSET_TYPE)
2472 return true;
2473 return (TREE_CODE (orig) == VECTOR_TYPE
2474 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2476 case REAL_TYPE:
2477 case FIXED_POINT_TYPE:
2478 case COMPLEX_TYPE:
2479 case VECTOR_TYPE:
2480 case VOID_TYPE:
2481 return TREE_CODE (type) == TREE_CODE (orig);
2483 default:
2484 return false;
2488 /* Convert expression ARG to type TYPE. Used by the middle-end for
2489 simple conversions in preference to calling the front-end's convert. */
2491 tree
2492 fold_convert (tree type, tree arg)
2494 tree orig = TREE_TYPE (arg);
2495 tree tem;
2497 if (type == orig)
2498 return arg;
2500 if (TREE_CODE (arg) == ERROR_MARK
2501 || TREE_CODE (type) == ERROR_MARK
2502 || TREE_CODE (orig) == ERROR_MARK)
2503 return error_mark_node;
2505 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2506 return fold_build1 (NOP_EXPR, type, arg);
2508 switch (TREE_CODE (type))
2510 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2511 case POINTER_TYPE: case REFERENCE_TYPE:
2512 case OFFSET_TYPE:
2513 if (TREE_CODE (arg) == INTEGER_CST)
2515 tem = fold_convert_const (NOP_EXPR, type, arg);
2516 if (tem != NULL_TREE)
2517 return tem;
2519 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2520 || TREE_CODE (orig) == OFFSET_TYPE)
2521 return fold_build1 (NOP_EXPR, type, arg);
2522 if (TREE_CODE (orig) == COMPLEX_TYPE)
2524 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2525 return fold_convert (type, tem);
2527 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2528 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2529 return fold_build1 (NOP_EXPR, type, arg);
2531 case REAL_TYPE:
2532 if (TREE_CODE (arg) == INTEGER_CST)
2534 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2535 if (tem != NULL_TREE)
2536 return tem;
2538 else if (TREE_CODE (arg) == REAL_CST)
2540 tem = fold_convert_const (NOP_EXPR, type, arg);
2541 if (tem != NULL_TREE)
2542 return tem;
2544 else if (TREE_CODE (arg) == FIXED_CST)
2546 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2547 if (tem != NULL_TREE)
2548 return tem;
2551 switch (TREE_CODE (orig))
2553 case INTEGER_TYPE:
2554 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2555 case POINTER_TYPE: case REFERENCE_TYPE:
2556 return fold_build1 (FLOAT_EXPR, type, arg);
2558 case REAL_TYPE:
2559 return fold_build1 (NOP_EXPR, type, arg);
2561 case FIXED_POINT_TYPE:
2562 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2564 case COMPLEX_TYPE:
2565 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2566 return fold_convert (type, tem);
2568 default:
2569 gcc_unreachable ();
2572 case FIXED_POINT_TYPE:
2573 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2574 || TREE_CODE (arg) == REAL_CST)
2576 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2577 if (tem != NULL_TREE)
2578 return tem;
2581 switch (TREE_CODE (orig))
2583 case FIXED_POINT_TYPE:
2584 case INTEGER_TYPE:
2585 case ENUMERAL_TYPE:
2586 case BOOLEAN_TYPE:
2587 case REAL_TYPE:
2588 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2590 case COMPLEX_TYPE:
2591 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2592 return fold_convert (type, tem);
2594 default:
2595 gcc_unreachable ();
2598 case COMPLEX_TYPE:
2599 switch (TREE_CODE (orig))
2601 case INTEGER_TYPE:
2602 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2603 case POINTER_TYPE: case REFERENCE_TYPE:
2604 case REAL_TYPE:
2605 case FIXED_POINT_TYPE:
2606 return build2 (COMPLEX_EXPR, type,
2607 fold_convert (TREE_TYPE (type), arg),
2608 fold_convert (TREE_TYPE (type), integer_zero_node));
2609 case COMPLEX_TYPE:
2611 tree rpart, ipart;
2613 if (TREE_CODE (arg) == COMPLEX_EXPR)
2615 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2616 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2617 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2620 arg = save_expr (arg);
2621 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2622 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2623 rpart = fold_convert (TREE_TYPE (type), rpart);
2624 ipart = fold_convert (TREE_TYPE (type), ipart);
2625 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2628 default:
2629 gcc_unreachable ();
2632 case VECTOR_TYPE:
2633 if (integer_zerop (arg))
2634 return build_zero_vector (type);
2635 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2636 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2637 || TREE_CODE (orig) == VECTOR_TYPE);
2638 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2640 case VOID_TYPE:
2641 tem = fold_ignored_result (arg);
2642 if (TREE_CODE (tem) == MODIFY_EXPR)
2643 return tem;
2644 return fold_build1 (NOP_EXPR, type, tem);
2646 default:
2647 gcc_unreachable ();
2651 /* Return false if expr can be assumed not to be an lvalue, true
2652 otherwise. */
2654 static bool
2655 maybe_lvalue_p (const_tree x)
2657 /* We only need to wrap lvalue tree codes. */
2658 switch (TREE_CODE (x))
2660 case VAR_DECL:
2661 case PARM_DECL:
2662 case RESULT_DECL:
2663 case LABEL_DECL:
2664 case FUNCTION_DECL:
2665 case SSA_NAME:
2667 case COMPONENT_REF:
2668 case INDIRECT_REF:
2669 case ALIGN_INDIRECT_REF:
2670 case MISALIGNED_INDIRECT_REF:
2671 case ARRAY_REF:
2672 case ARRAY_RANGE_REF:
2673 case BIT_FIELD_REF:
2674 case OBJ_TYPE_REF:
2676 case REALPART_EXPR:
2677 case IMAGPART_EXPR:
2678 case PREINCREMENT_EXPR:
2679 case PREDECREMENT_EXPR:
2680 case SAVE_EXPR:
2681 case TRY_CATCH_EXPR:
2682 case WITH_CLEANUP_EXPR:
2683 case COMPOUND_EXPR:
2684 case MODIFY_EXPR:
2685 case TARGET_EXPR:
2686 case COND_EXPR:
2687 case BIND_EXPR:
2688 case MIN_EXPR:
2689 case MAX_EXPR:
2690 break;
2692 default:
2693 /* Assume the worst for front-end tree codes. */
2694 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2695 break;
2696 return false;
2699 return true;
2702 /* Return an expr equal to X but certainly not valid as an lvalue. */
2704 tree
2705 non_lvalue (tree x)
2707 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2708 us. */
2709 if (in_gimple_form)
2710 return x;
2712 if (! maybe_lvalue_p (x))
2713 return x;
2714 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2717 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2718 Zero means allow extended lvalues. */
2720 int pedantic_lvalues;
2722 /* When pedantic, return an expr equal to X but certainly not valid as a
2723 pedantic lvalue. Otherwise, return X. */
2725 static tree
2726 pedantic_non_lvalue (tree x)
2728 if (pedantic_lvalues)
2729 return non_lvalue (x);
2730 else
2731 return x;
2734 /* Given a tree comparison code, return the code that is the logical inverse
2735 of the given code. It is not safe to do this for floating-point
2736 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2737 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2739 enum tree_code
2740 invert_tree_comparison (enum tree_code code, bool honor_nans)
2742 if (honor_nans && flag_trapping_math)
2743 return ERROR_MARK;
2745 switch (code)
2747 case EQ_EXPR:
2748 return NE_EXPR;
2749 case NE_EXPR:
2750 return EQ_EXPR;
2751 case GT_EXPR:
2752 return honor_nans ? UNLE_EXPR : LE_EXPR;
2753 case GE_EXPR:
2754 return honor_nans ? UNLT_EXPR : LT_EXPR;
2755 case LT_EXPR:
2756 return honor_nans ? UNGE_EXPR : GE_EXPR;
2757 case LE_EXPR:
2758 return honor_nans ? UNGT_EXPR : GT_EXPR;
2759 case LTGT_EXPR:
2760 return UNEQ_EXPR;
2761 case UNEQ_EXPR:
2762 return LTGT_EXPR;
2763 case UNGT_EXPR:
2764 return LE_EXPR;
2765 case UNGE_EXPR:
2766 return LT_EXPR;
2767 case UNLT_EXPR:
2768 return GE_EXPR;
2769 case UNLE_EXPR:
2770 return GT_EXPR;
2771 case ORDERED_EXPR:
2772 return UNORDERED_EXPR;
2773 case UNORDERED_EXPR:
2774 return ORDERED_EXPR;
2775 default:
2776 gcc_unreachable ();
2780 /* Similar, but return the comparison that results if the operands are
2781 swapped. This is safe for floating-point. */
2783 enum tree_code
2784 swap_tree_comparison (enum tree_code code)
2786 switch (code)
2788 case EQ_EXPR:
2789 case NE_EXPR:
2790 case ORDERED_EXPR:
2791 case UNORDERED_EXPR:
2792 case LTGT_EXPR:
2793 case UNEQ_EXPR:
2794 return code;
2795 case GT_EXPR:
2796 return LT_EXPR;
2797 case GE_EXPR:
2798 return LE_EXPR;
2799 case LT_EXPR:
2800 return GT_EXPR;
2801 case LE_EXPR:
2802 return GE_EXPR;
2803 case UNGT_EXPR:
2804 return UNLT_EXPR;
2805 case UNGE_EXPR:
2806 return UNLE_EXPR;
2807 case UNLT_EXPR:
2808 return UNGT_EXPR;
2809 case UNLE_EXPR:
2810 return UNGE_EXPR;
2811 default:
2812 gcc_unreachable ();
2817 /* Convert a comparison tree code from an enum tree_code representation
2818 into a compcode bit-based encoding. This function is the inverse of
2819 compcode_to_comparison. */
2821 static enum comparison_code
2822 comparison_to_compcode (enum tree_code code)
2824 switch (code)
2826 case LT_EXPR:
2827 return COMPCODE_LT;
2828 case EQ_EXPR:
2829 return COMPCODE_EQ;
2830 case LE_EXPR:
2831 return COMPCODE_LE;
2832 case GT_EXPR:
2833 return COMPCODE_GT;
2834 case NE_EXPR:
2835 return COMPCODE_NE;
2836 case GE_EXPR:
2837 return COMPCODE_GE;
2838 case ORDERED_EXPR:
2839 return COMPCODE_ORD;
2840 case UNORDERED_EXPR:
2841 return COMPCODE_UNORD;
2842 case UNLT_EXPR:
2843 return COMPCODE_UNLT;
2844 case UNEQ_EXPR:
2845 return COMPCODE_UNEQ;
2846 case UNLE_EXPR:
2847 return COMPCODE_UNLE;
2848 case UNGT_EXPR:
2849 return COMPCODE_UNGT;
2850 case LTGT_EXPR:
2851 return COMPCODE_LTGT;
2852 case UNGE_EXPR:
2853 return COMPCODE_UNGE;
2854 default:
2855 gcc_unreachable ();
2859 /* Convert a compcode bit-based encoding of a comparison operator back
2860 to GCC's enum tree_code representation. This function is the
2861 inverse of comparison_to_compcode. */
2863 static enum tree_code
2864 compcode_to_comparison (enum comparison_code code)
2866 switch (code)
2868 case COMPCODE_LT:
2869 return LT_EXPR;
2870 case COMPCODE_EQ:
2871 return EQ_EXPR;
2872 case COMPCODE_LE:
2873 return LE_EXPR;
2874 case COMPCODE_GT:
2875 return GT_EXPR;
2876 case COMPCODE_NE:
2877 return NE_EXPR;
2878 case COMPCODE_GE:
2879 return GE_EXPR;
2880 case COMPCODE_ORD:
2881 return ORDERED_EXPR;
2882 case COMPCODE_UNORD:
2883 return UNORDERED_EXPR;
2884 case COMPCODE_UNLT:
2885 return UNLT_EXPR;
2886 case COMPCODE_UNEQ:
2887 return UNEQ_EXPR;
2888 case COMPCODE_UNLE:
2889 return UNLE_EXPR;
2890 case COMPCODE_UNGT:
2891 return UNGT_EXPR;
2892 case COMPCODE_LTGT:
2893 return LTGT_EXPR;
2894 case COMPCODE_UNGE:
2895 return UNGE_EXPR;
2896 default:
2897 gcc_unreachable ();
2901 /* Return a tree for the comparison which is the combination of
2902 doing the AND or OR (depending on CODE) of the two operations LCODE
2903 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2904 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2905 if this makes the transformation invalid. */
2907 tree
2908 combine_comparisons (enum tree_code code, enum tree_code lcode,
2909 enum tree_code rcode, tree truth_type,
2910 tree ll_arg, tree lr_arg)
2912 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2913 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2914 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2915 enum comparison_code compcode;
2917 switch (code)
2919 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2920 compcode = lcompcode & rcompcode;
2921 break;
2923 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2924 compcode = lcompcode | rcompcode;
2925 break;
2927 default:
2928 return NULL_TREE;
2931 if (!honor_nans)
2933 /* Eliminate unordered comparisons, as well as LTGT and ORD
2934 which are not used unless the mode has NaNs. */
2935 compcode &= ~COMPCODE_UNORD;
2936 if (compcode == COMPCODE_LTGT)
2937 compcode = COMPCODE_NE;
2938 else if (compcode == COMPCODE_ORD)
2939 compcode = COMPCODE_TRUE;
2941 else if (flag_trapping_math)
2943 /* Check that the original operation and the optimized ones will trap
2944 under the same condition. */
2945 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2946 && (lcompcode != COMPCODE_EQ)
2947 && (lcompcode != COMPCODE_ORD);
2948 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2949 && (rcompcode != COMPCODE_EQ)
2950 && (rcompcode != COMPCODE_ORD);
2951 bool trap = (compcode & COMPCODE_UNORD) == 0
2952 && (compcode != COMPCODE_EQ)
2953 && (compcode != COMPCODE_ORD);
2955 /* In a short-circuited boolean expression the LHS might be
2956 such that the RHS, if evaluated, will never trap. For
2957 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2958 if neither x nor y is NaN. (This is a mixed blessing: for
2959 example, the expression above will never trap, hence
2960 optimizing it to x < y would be invalid). */
2961 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2962 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2963 rtrap = false;
2965 /* If the comparison was short-circuited, and only the RHS
2966 trapped, we may now generate a spurious trap. */
2967 if (rtrap && !ltrap
2968 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2969 return NULL_TREE;
2971 /* If we changed the conditions that cause a trap, we lose. */
2972 if ((ltrap || rtrap) != trap)
2973 return NULL_TREE;
2976 if (compcode == COMPCODE_TRUE)
2977 return constant_boolean_node (true, truth_type);
2978 else if (compcode == COMPCODE_FALSE)
2979 return constant_boolean_node (false, truth_type);
2980 else
2981 return fold_build2 (compcode_to_comparison (compcode),
2982 truth_type, ll_arg, lr_arg);
2985 /* Return nonzero if CODE is a tree code that represents a truth value. */
2987 static int
2988 truth_value_p (enum tree_code code)
2990 return (TREE_CODE_CLASS (code) == tcc_comparison
2991 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2992 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2993 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2996 /* Return nonzero if two operands (typically of the same tree node)
2997 are necessarily equal. If either argument has side-effects this
2998 function returns zero. FLAGS modifies behavior as follows:
3000 If OEP_ONLY_CONST is set, only return nonzero for constants.
3001 This function tests whether the operands are indistinguishable;
3002 it does not test whether they are equal using C's == operation.
3003 The distinction is important for IEEE floating point, because
3004 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3005 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3007 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3008 even though it may hold multiple values during a function.
3009 This is because a GCC tree node guarantees that nothing else is
3010 executed between the evaluation of its "operands" (which may often
3011 be evaluated in arbitrary order). Hence if the operands themselves
3012 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3013 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3014 unset means assuming isochronic (or instantaneous) tree equivalence.
3015 Unless comparing arbitrary expression trees, such as from different
3016 statements, this flag can usually be left unset.
3018 If OEP_PURE_SAME is set, then pure functions with identical arguments
3019 are considered the same. It is used when the caller has other ways
3020 to ensure that global memory is unchanged in between. */
3023 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3025 /* If either is ERROR_MARK, they aren't equal. */
3026 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3027 return 0;
3029 /* Check equality of integer constants before bailing out due to
3030 precision differences. */
3031 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3032 return tree_int_cst_equal (arg0, arg1);
3034 /* If both types don't have the same signedness, then we can't consider
3035 them equal. We must check this before the STRIP_NOPS calls
3036 because they may change the signedness of the arguments. As pointers
3037 strictly don't have a signedness, require either two pointers or
3038 two non-pointers as well. */
3039 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3040 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3041 return 0;
3043 /* If both types don't have the same precision, then it is not safe
3044 to strip NOPs. */
3045 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3046 return 0;
3048 STRIP_NOPS (arg0);
3049 STRIP_NOPS (arg1);
3051 /* In case both args are comparisons but with different comparison
3052 code, try to swap the comparison operands of one arg to produce
3053 a match and compare that variant. */
3054 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3055 && COMPARISON_CLASS_P (arg0)
3056 && COMPARISON_CLASS_P (arg1))
3058 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3060 if (TREE_CODE (arg0) == swap_code)
3061 return operand_equal_p (TREE_OPERAND (arg0, 0),
3062 TREE_OPERAND (arg1, 1), flags)
3063 && operand_equal_p (TREE_OPERAND (arg0, 1),
3064 TREE_OPERAND (arg1, 0), flags);
3067 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3068 /* This is needed for conversions and for COMPONENT_REF.
3069 Might as well play it safe and always test this. */
3070 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3071 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3072 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3073 return 0;
3075 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3076 We don't care about side effects in that case because the SAVE_EXPR
3077 takes care of that for us. In all other cases, two expressions are
3078 equal if they have no side effects. If we have two identical
3079 expressions with side effects that should be treated the same due
3080 to the only side effects being identical SAVE_EXPR's, that will
3081 be detected in the recursive calls below. */
3082 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3083 && (TREE_CODE (arg0) == SAVE_EXPR
3084 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3085 return 1;
3087 /* Next handle constant cases, those for which we can return 1 even
3088 if ONLY_CONST is set. */
3089 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3090 switch (TREE_CODE (arg0))
3092 case INTEGER_CST:
3093 return tree_int_cst_equal (arg0, arg1);
3095 case FIXED_CST:
3096 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3097 TREE_FIXED_CST (arg1));
3099 case REAL_CST:
3100 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3101 TREE_REAL_CST (arg1)))
3102 return 1;
3105 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3107 /* If we do not distinguish between signed and unsigned zero,
3108 consider them equal. */
3109 if (real_zerop (arg0) && real_zerop (arg1))
3110 return 1;
3112 return 0;
3114 case VECTOR_CST:
3116 tree v1, v2;
3118 v1 = TREE_VECTOR_CST_ELTS (arg0);
3119 v2 = TREE_VECTOR_CST_ELTS (arg1);
3120 while (v1 && v2)
3122 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3123 flags))
3124 return 0;
3125 v1 = TREE_CHAIN (v1);
3126 v2 = TREE_CHAIN (v2);
3129 return v1 == v2;
3132 case COMPLEX_CST:
3133 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3134 flags)
3135 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3136 flags));
3138 case STRING_CST:
3139 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3140 && ! memcmp (TREE_STRING_POINTER (arg0),
3141 TREE_STRING_POINTER (arg1),
3142 TREE_STRING_LENGTH (arg0)));
3144 case ADDR_EXPR:
3145 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3147 default:
3148 break;
3151 if (flags & OEP_ONLY_CONST)
3152 return 0;
3154 /* Define macros to test an operand from arg0 and arg1 for equality and a
3155 variant that allows null and views null as being different from any
3156 non-null value. In the latter case, if either is null, the both
3157 must be; otherwise, do the normal comparison. */
3158 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3159 TREE_OPERAND (arg1, N), flags)
3161 #define OP_SAME_WITH_NULL(N) \
3162 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3163 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3165 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3167 case tcc_unary:
3168 /* Two conversions are equal only if signedness and modes match. */
3169 switch (TREE_CODE (arg0))
3171 CASE_CONVERT:
3172 case FIX_TRUNC_EXPR:
3173 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3174 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3175 return 0;
3176 break;
3177 default:
3178 break;
3181 return OP_SAME (0);
3184 case tcc_comparison:
3185 case tcc_binary:
3186 if (OP_SAME (0) && OP_SAME (1))
3187 return 1;
3189 /* For commutative ops, allow the other order. */
3190 return (commutative_tree_code (TREE_CODE (arg0))
3191 && operand_equal_p (TREE_OPERAND (arg0, 0),
3192 TREE_OPERAND (arg1, 1), flags)
3193 && operand_equal_p (TREE_OPERAND (arg0, 1),
3194 TREE_OPERAND (arg1, 0), flags));
3196 case tcc_reference:
3197 /* If either of the pointer (or reference) expressions we are
3198 dereferencing contain a side effect, these cannot be equal. */
3199 if (TREE_SIDE_EFFECTS (arg0)
3200 || TREE_SIDE_EFFECTS (arg1))
3201 return 0;
3203 switch (TREE_CODE (arg0))
3205 case INDIRECT_REF:
3206 case ALIGN_INDIRECT_REF:
3207 case MISALIGNED_INDIRECT_REF:
3208 case REALPART_EXPR:
3209 case IMAGPART_EXPR:
3210 return OP_SAME (0);
3212 case ARRAY_REF:
3213 case ARRAY_RANGE_REF:
3214 /* Operands 2 and 3 may be null.
3215 Compare the array index by value if it is constant first as we
3216 may have different types but same value here. */
3217 return (OP_SAME (0)
3218 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3219 TREE_OPERAND (arg1, 1))
3220 || OP_SAME (1))
3221 && OP_SAME_WITH_NULL (2)
3222 && OP_SAME_WITH_NULL (3));
3224 case COMPONENT_REF:
3225 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3226 may be NULL when we're called to compare MEM_EXPRs. */
3227 return OP_SAME_WITH_NULL (0)
3228 && OP_SAME (1)
3229 && OP_SAME_WITH_NULL (2);
3231 case BIT_FIELD_REF:
3232 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3234 default:
3235 return 0;
3238 case tcc_expression:
3239 switch (TREE_CODE (arg0))
3241 case ADDR_EXPR:
3242 case TRUTH_NOT_EXPR:
3243 return OP_SAME (0);
3245 case TRUTH_ANDIF_EXPR:
3246 case TRUTH_ORIF_EXPR:
3247 return OP_SAME (0) && OP_SAME (1);
3249 case TRUTH_AND_EXPR:
3250 case TRUTH_OR_EXPR:
3251 case TRUTH_XOR_EXPR:
3252 if (OP_SAME (0) && OP_SAME (1))
3253 return 1;
3255 /* Otherwise take into account this is a commutative operation. */
3256 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3257 TREE_OPERAND (arg1, 1), flags)
3258 && operand_equal_p (TREE_OPERAND (arg0, 1),
3259 TREE_OPERAND (arg1, 0), flags));
3261 case COND_EXPR:
3262 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3264 default:
3265 return 0;
3268 case tcc_vl_exp:
3269 switch (TREE_CODE (arg0))
3271 case CALL_EXPR:
3272 /* If the CALL_EXPRs call different functions, then they
3273 clearly can not be equal. */
3274 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3275 flags))
3276 return 0;
3279 unsigned int cef = call_expr_flags (arg0);
3280 if (flags & OEP_PURE_SAME)
3281 cef &= ECF_CONST | ECF_PURE;
3282 else
3283 cef &= ECF_CONST;
3284 if (!cef)
3285 return 0;
3288 /* Now see if all the arguments are the same. */
3290 const_call_expr_arg_iterator iter0, iter1;
3291 const_tree a0, a1;
3292 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3293 a1 = first_const_call_expr_arg (arg1, &iter1);
3294 a0 && a1;
3295 a0 = next_const_call_expr_arg (&iter0),
3296 a1 = next_const_call_expr_arg (&iter1))
3297 if (! operand_equal_p (a0, a1, flags))
3298 return 0;
3300 /* If we get here and both argument lists are exhausted
3301 then the CALL_EXPRs are equal. */
3302 return ! (a0 || a1);
3304 default:
3305 return 0;
3308 case tcc_declaration:
3309 /* Consider __builtin_sqrt equal to sqrt. */
3310 return (TREE_CODE (arg0) == FUNCTION_DECL
3311 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3312 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3313 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3315 default:
3316 return 0;
3319 #undef OP_SAME
3320 #undef OP_SAME_WITH_NULL
3323 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3324 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3326 When in doubt, return 0. */
3328 static int
3329 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3331 int unsignedp1, unsignedpo;
3332 tree primarg0, primarg1, primother;
3333 unsigned int correct_width;
3335 if (operand_equal_p (arg0, arg1, 0))
3336 return 1;
3338 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3339 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3340 return 0;
3342 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3343 and see if the inner values are the same. This removes any
3344 signedness comparison, which doesn't matter here. */
3345 primarg0 = arg0, primarg1 = arg1;
3346 STRIP_NOPS (primarg0);
3347 STRIP_NOPS (primarg1);
3348 if (operand_equal_p (primarg0, primarg1, 0))
3349 return 1;
3351 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3352 actual comparison operand, ARG0.
3354 First throw away any conversions to wider types
3355 already present in the operands. */
3357 primarg1 = get_narrower (arg1, &unsignedp1);
3358 primother = get_narrower (other, &unsignedpo);
3360 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3361 if (unsignedp1 == unsignedpo
3362 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3363 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3365 tree type = TREE_TYPE (arg0);
3367 /* Make sure shorter operand is extended the right way
3368 to match the longer operand. */
3369 primarg1 = fold_convert (signed_or_unsigned_type_for
3370 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3372 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3373 return 1;
3376 return 0;
3379 /* See if ARG is an expression that is either a comparison or is performing
3380 arithmetic on comparisons. The comparisons must only be comparing
3381 two different values, which will be stored in *CVAL1 and *CVAL2; if
3382 they are nonzero it means that some operands have already been found.
3383 No variables may be used anywhere else in the expression except in the
3384 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3385 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3387 If this is true, return 1. Otherwise, return zero. */
3389 static int
3390 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3392 enum tree_code code = TREE_CODE (arg);
3393 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3395 /* We can handle some of the tcc_expression cases here. */
3396 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3397 tclass = tcc_unary;
3398 else if (tclass == tcc_expression
3399 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3400 || code == COMPOUND_EXPR))
3401 tclass = tcc_binary;
3403 else if (tclass == tcc_expression && code == SAVE_EXPR
3404 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3406 /* If we've already found a CVAL1 or CVAL2, this expression is
3407 two complex to handle. */
3408 if (*cval1 || *cval2)
3409 return 0;
3411 tclass = tcc_unary;
3412 *save_p = 1;
3415 switch (tclass)
3417 case tcc_unary:
3418 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3420 case tcc_binary:
3421 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3422 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3423 cval1, cval2, save_p));
3425 case tcc_constant:
3426 return 1;
3428 case tcc_expression:
3429 if (code == COND_EXPR)
3430 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3431 cval1, cval2, save_p)
3432 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3433 cval1, cval2, save_p)
3434 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3435 cval1, cval2, save_p));
3436 return 0;
3438 case tcc_comparison:
3439 /* First see if we can handle the first operand, then the second. For
3440 the second operand, we know *CVAL1 can't be zero. It must be that
3441 one side of the comparison is each of the values; test for the
3442 case where this isn't true by failing if the two operands
3443 are the same. */
3445 if (operand_equal_p (TREE_OPERAND (arg, 0),
3446 TREE_OPERAND (arg, 1), 0))
3447 return 0;
3449 if (*cval1 == 0)
3450 *cval1 = TREE_OPERAND (arg, 0);
3451 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3453 else if (*cval2 == 0)
3454 *cval2 = TREE_OPERAND (arg, 0);
3455 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3457 else
3458 return 0;
3460 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3462 else if (*cval2 == 0)
3463 *cval2 = TREE_OPERAND (arg, 1);
3464 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3466 else
3467 return 0;
3469 return 1;
3471 default:
3472 return 0;
3476 /* ARG is a tree that is known to contain just arithmetic operations and
3477 comparisons. Evaluate the operations in the tree substituting NEW0 for
3478 any occurrence of OLD0 as an operand of a comparison and likewise for
3479 NEW1 and OLD1. */
3481 static tree
3482 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3484 tree type = TREE_TYPE (arg);
3485 enum tree_code code = TREE_CODE (arg);
3486 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3488 /* We can handle some of the tcc_expression cases here. */
3489 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3490 tclass = tcc_unary;
3491 else if (tclass == tcc_expression
3492 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3493 tclass = tcc_binary;
3495 switch (tclass)
3497 case tcc_unary:
3498 return fold_build1 (code, type,
3499 eval_subst (TREE_OPERAND (arg, 0),
3500 old0, new0, old1, new1));
3502 case tcc_binary:
3503 return fold_build2 (code, type,
3504 eval_subst (TREE_OPERAND (arg, 0),
3505 old0, new0, old1, new1),
3506 eval_subst (TREE_OPERAND (arg, 1),
3507 old0, new0, old1, new1));
3509 case tcc_expression:
3510 switch (code)
3512 case SAVE_EXPR:
3513 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3515 case COMPOUND_EXPR:
3516 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3518 case COND_EXPR:
3519 return fold_build3 (code, type,
3520 eval_subst (TREE_OPERAND (arg, 0),
3521 old0, new0, old1, new1),
3522 eval_subst (TREE_OPERAND (arg, 1),
3523 old0, new0, old1, new1),
3524 eval_subst (TREE_OPERAND (arg, 2),
3525 old0, new0, old1, new1));
3526 default:
3527 break;
3529 /* Fall through - ??? */
3531 case tcc_comparison:
3533 tree arg0 = TREE_OPERAND (arg, 0);
3534 tree arg1 = TREE_OPERAND (arg, 1);
3536 /* We need to check both for exact equality and tree equality. The
3537 former will be true if the operand has a side-effect. In that
3538 case, we know the operand occurred exactly once. */
3540 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3541 arg0 = new0;
3542 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3543 arg0 = new1;
3545 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3546 arg1 = new0;
3547 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3548 arg1 = new1;
3550 return fold_build2 (code, type, arg0, arg1);
3553 default:
3554 return arg;
3558 /* Return a tree for the case when the result of an expression is RESULT
3559 converted to TYPE and OMITTED was previously an operand of the expression
3560 but is now not needed (e.g., we folded OMITTED * 0).
3562 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3563 the conversion of RESULT to TYPE. */
3565 tree
3566 omit_one_operand (tree type, tree result, tree omitted)
3568 tree t = fold_convert (type, result);
3570 /* If the resulting operand is an empty statement, just return the omitted
3571 statement casted to void. */
3572 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3573 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3575 if (TREE_SIDE_EFFECTS (omitted))
3576 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3578 return non_lvalue (t);
3581 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3583 static tree
3584 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3586 tree t = fold_convert (type, result);
3588 /* If the resulting operand is an empty statement, just return the omitted
3589 statement casted to void. */
3590 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3591 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3593 if (TREE_SIDE_EFFECTS (omitted))
3594 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3596 return pedantic_non_lvalue (t);
3599 /* Return a tree for the case when the result of an expression is RESULT
3600 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3601 of the expression but are now not needed.
3603 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3604 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3605 evaluated before OMITTED2. Otherwise, if neither has side effects,
3606 just do the conversion of RESULT to TYPE. */
3608 tree
3609 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3611 tree t = fold_convert (type, result);
3613 if (TREE_SIDE_EFFECTS (omitted2))
3614 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3615 if (TREE_SIDE_EFFECTS (omitted1))
3616 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3618 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3622 /* Return a simplified tree node for the truth-negation of ARG. This
3623 never alters ARG itself. We assume that ARG is an operation that
3624 returns a truth value (0 or 1).
3626 FIXME: one would think we would fold the result, but it causes
3627 problems with the dominator optimizer. */
3629 tree
3630 fold_truth_not_expr (tree arg)
3632 tree type = TREE_TYPE (arg);
3633 enum tree_code code = TREE_CODE (arg);
3635 /* If this is a comparison, we can simply invert it, except for
3636 floating-point non-equality comparisons, in which case we just
3637 enclose a TRUTH_NOT_EXPR around what we have. */
3639 if (TREE_CODE_CLASS (code) == tcc_comparison)
3641 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3642 if (FLOAT_TYPE_P (op_type)
3643 && flag_trapping_math
3644 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3645 && code != NE_EXPR && code != EQ_EXPR)
3646 return NULL_TREE;
3647 else
3649 code = invert_tree_comparison (code,
3650 HONOR_NANS (TYPE_MODE (op_type)));
3651 if (code == ERROR_MARK)
3652 return NULL_TREE;
3653 else
3654 return build2 (code, type,
3655 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3659 switch (code)
3661 case INTEGER_CST:
3662 return constant_boolean_node (integer_zerop (arg), type);
3664 case TRUTH_AND_EXPR:
3665 return build2 (TRUTH_OR_EXPR, type,
3666 invert_truthvalue (TREE_OPERAND (arg, 0)),
3667 invert_truthvalue (TREE_OPERAND (arg, 1)));
3669 case TRUTH_OR_EXPR:
3670 return build2 (TRUTH_AND_EXPR, type,
3671 invert_truthvalue (TREE_OPERAND (arg, 0)),
3672 invert_truthvalue (TREE_OPERAND (arg, 1)));
3674 case TRUTH_XOR_EXPR:
3675 /* Here we can invert either operand. We invert the first operand
3676 unless the second operand is a TRUTH_NOT_EXPR in which case our
3677 result is the XOR of the first operand with the inside of the
3678 negation of the second operand. */
3680 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3681 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3682 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3683 else
3684 return build2 (TRUTH_XOR_EXPR, type,
3685 invert_truthvalue (TREE_OPERAND (arg, 0)),
3686 TREE_OPERAND (arg, 1));
3688 case TRUTH_ANDIF_EXPR:
3689 return build2 (TRUTH_ORIF_EXPR, type,
3690 invert_truthvalue (TREE_OPERAND (arg, 0)),
3691 invert_truthvalue (TREE_OPERAND (arg, 1)));
3693 case TRUTH_ORIF_EXPR:
3694 return build2 (TRUTH_ANDIF_EXPR, type,
3695 invert_truthvalue (TREE_OPERAND (arg, 0)),
3696 invert_truthvalue (TREE_OPERAND (arg, 1)));
3698 case TRUTH_NOT_EXPR:
3699 return TREE_OPERAND (arg, 0);
3701 case COND_EXPR:
3703 tree arg1 = TREE_OPERAND (arg, 1);
3704 tree arg2 = TREE_OPERAND (arg, 2);
3705 /* A COND_EXPR may have a throw as one operand, which
3706 then has void type. Just leave void operands
3707 as they are. */
3708 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3709 VOID_TYPE_P (TREE_TYPE (arg1))
3710 ? arg1 : invert_truthvalue (arg1),
3711 VOID_TYPE_P (TREE_TYPE (arg2))
3712 ? arg2 : invert_truthvalue (arg2));
3715 case COMPOUND_EXPR:
3716 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3717 invert_truthvalue (TREE_OPERAND (arg, 1)));
3719 case NON_LVALUE_EXPR:
3720 return invert_truthvalue (TREE_OPERAND (arg, 0));
3722 case NOP_EXPR:
3723 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3724 return build1 (TRUTH_NOT_EXPR, type, arg);
3726 case CONVERT_EXPR:
3727 case FLOAT_EXPR:
3728 return build1 (TREE_CODE (arg), type,
3729 invert_truthvalue (TREE_OPERAND (arg, 0)));
3731 case BIT_AND_EXPR:
3732 if (!integer_onep (TREE_OPERAND (arg, 1)))
3733 break;
3734 return build2 (EQ_EXPR, type, arg,
3735 build_int_cst (type, 0));
3737 case SAVE_EXPR:
3738 return build1 (TRUTH_NOT_EXPR, type, arg);
3740 case CLEANUP_POINT_EXPR:
3741 return build1 (CLEANUP_POINT_EXPR, type,
3742 invert_truthvalue (TREE_OPERAND (arg, 0)));
3744 default:
3745 break;
3748 return NULL_TREE;
3751 /* Return a simplified tree node for the truth-negation of ARG. This
3752 never alters ARG itself. We assume that ARG is an operation that
3753 returns a truth value (0 or 1).
3755 FIXME: one would think we would fold the result, but it causes
3756 problems with the dominator optimizer. */
3758 tree
3759 invert_truthvalue (tree arg)
3761 tree tem;
3763 if (TREE_CODE (arg) == ERROR_MARK)
3764 return arg;
3766 tem = fold_truth_not_expr (arg);
3767 if (!tem)
3768 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3770 return tem;
3773 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3774 operands are another bit-wise operation with a common input. If so,
3775 distribute the bit operations to save an operation and possibly two if
3776 constants are involved. For example, convert
3777 (A | B) & (A | C) into A | (B & C)
3778 Further simplification will occur if B and C are constants.
3780 If this optimization cannot be done, 0 will be returned. */
3782 static tree
3783 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3785 tree common;
3786 tree left, right;
3788 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3789 || TREE_CODE (arg0) == code
3790 || (TREE_CODE (arg0) != BIT_AND_EXPR
3791 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3792 return 0;
3794 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3796 common = TREE_OPERAND (arg0, 0);
3797 left = TREE_OPERAND (arg0, 1);
3798 right = TREE_OPERAND (arg1, 1);
3800 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3802 common = TREE_OPERAND (arg0, 0);
3803 left = TREE_OPERAND (arg0, 1);
3804 right = TREE_OPERAND (arg1, 0);
3806 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3808 common = TREE_OPERAND (arg0, 1);
3809 left = TREE_OPERAND (arg0, 0);
3810 right = TREE_OPERAND (arg1, 1);
3812 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3814 common = TREE_OPERAND (arg0, 1);
3815 left = TREE_OPERAND (arg0, 0);
3816 right = TREE_OPERAND (arg1, 0);
3818 else
3819 return 0;
3821 return fold_build2 (TREE_CODE (arg0), type, common,
3822 fold_build2 (code, type, left, right));
3825 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3826 with code CODE. This optimization is unsafe. */
3827 static tree
3828 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3830 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3831 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3833 /* (A / C) +- (B / C) -> (A +- B) / C. */
3834 if (mul0 == mul1
3835 && operand_equal_p (TREE_OPERAND (arg0, 1),
3836 TREE_OPERAND (arg1, 1), 0))
3837 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3838 fold_build2 (code, type,
3839 TREE_OPERAND (arg0, 0),
3840 TREE_OPERAND (arg1, 0)),
3841 TREE_OPERAND (arg0, 1));
3843 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3844 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3845 TREE_OPERAND (arg1, 0), 0)
3846 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3847 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3849 REAL_VALUE_TYPE r0, r1;
3850 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3851 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3852 if (!mul0)
3853 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3854 if (!mul1)
3855 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3856 real_arithmetic (&r0, code, &r0, &r1);
3857 return fold_build2 (MULT_EXPR, type,
3858 TREE_OPERAND (arg0, 0),
3859 build_real (type, r0));
3862 return NULL_TREE;
3865 /* Subroutine for fold_truthop: decode a field reference.
3867 If EXP is a comparison reference, we return the innermost reference.
3869 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3870 set to the starting bit number.
3872 If the innermost field can be completely contained in a mode-sized
3873 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3875 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3876 otherwise it is not changed.
3878 *PUNSIGNEDP is set to the signedness of the field.
3880 *PMASK is set to the mask used. This is either contained in a
3881 BIT_AND_EXPR or derived from the width of the field.
3883 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3885 Return 0 if this is not a component reference or is one that we can't
3886 do anything with. */
3888 static tree
3889 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3890 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3891 int *punsignedp, int *pvolatilep,
3892 tree *pmask, tree *pand_mask)
3894 tree outer_type = 0;
3895 tree and_mask = 0;
3896 tree mask, inner, offset;
3897 tree unsigned_type;
3898 unsigned int precision;
3900 /* All the optimizations using this function assume integer fields.
3901 There are problems with FP fields since the type_for_size call
3902 below can fail for, e.g., XFmode. */
3903 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3904 return 0;
3906 /* We are interested in the bare arrangement of bits, so strip everything
3907 that doesn't affect the machine mode. However, record the type of the
3908 outermost expression if it may matter below. */
3909 if (CONVERT_EXPR_P (exp)
3910 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3911 outer_type = TREE_TYPE (exp);
3912 STRIP_NOPS (exp);
3914 if (TREE_CODE (exp) == BIT_AND_EXPR)
3916 and_mask = TREE_OPERAND (exp, 1);
3917 exp = TREE_OPERAND (exp, 0);
3918 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3919 if (TREE_CODE (and_mask) != INTEGER_CST)
3920 return 0;
3923 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3924 punsignedp, pvolatilep, false);
3925 if ((inner == exp && and_mask == 0)
3926 || *pbitsize < 0 || offset != 0
3927 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3928 return 0;
3930 /* If the number of bits in the reference is the same as the bitsize of
3931 the outer type, then the outer type gives the signedness. Otherwise
3932 (in case of a small bitfield) the signedness is unchanged. */
3933 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3934 *punsignedp = TYPE_UNSIGNED (outer_type);
3936 /* Compute the mask to access the bitfield. */
3937 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3938 precision = TYPE_PRECISION (unsigned_type);
3940 mask = build_int_cst_type (unsigned_type, -1);
3942 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3943 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3945 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3946 if (and_mask != 0)
3947 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3948 fold_convert (unsigned_type, and_mask), mask);
3950 *pmask = mask;
3951 *pand_mask = and_mask;
3952 return inner;
3955 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3956 represents the sign bit of EXP's type. If EXP represents a sign
3957 or zero extension, also test VAL against the unextended type.
3958 The return value is the (sub)expression whose sign bit is VAL,
3959 or NULL_TREE otherwise. */
3961 static tree
3962 sign_bit_p (tree exp, const_tree val)
3964 unsigned HOST_WIDE_INT mask_lo, lo;
3965 HOST_WIDE_INT mask_hi, hi;
3966 int width;
3967 tree t;
3969 /* Tree EXP must have an integral type. */
3970 t = TREE_TYPE (exp);
3971 if (! INTEGRAL_TYPE_P (t))
3972 return NULL_TREE;
3974 /* Tree VAL must be an integer constant. */
3975 if (TREE_CODE (val) != INTEGER_CST
3976 || TREE_OVERFLOW (val))
3977 return NULL_TREE;
3979 width = TYPE_PRECISION (t);
3980 if (width > HOST_BITS_PER_WIDE_INT)
3982 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3983 lo = 0;
3985 mask_hi = ((unsigned HOST_WIDE_INT) -1
3986 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3987 mask_lo = -1;
3989 else
3991 hi = 0;
3992 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3994 mask_hi = 0;
3995 mask_lo = ((unsigned HOST_WIDE_INT) -1
3996 >> (HOST_BITS_PER_WIDE_INT - width));
3999 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4000 treat VAL as if it were unsigned. */
4001 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4002 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4003 return exp;
4005 /* Handle extension from a narrower type. */
4006 if (TREE_CODE (exp) == NOP_EXPR
4007 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4008 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4010 return NULL_TREE;
4013 /* Subroutine for fold_truthop: determine if an operand is simple enough
4014 to be evaluated unconditionally. */
4016 static int
4017 simple_operand_p (const_tree exp)
4019 /* Strip any conversions that don't change the machine mode. */
4020 STRIP_NOPS (exp);
4022 return (CONSTANT_CLASS_P (exp)
4023 || TREE_CODE (exp) == SSA_NAME
4024 || (DECL_P (exp)
4025 && ! TREE_ADDRESSABLE (exp)
4026 && ! TREE_THIS_VOLATILE (exp)
4027 && ! DECL_NONLOCAL (exp)
4028 /* Don't regard global variables as simple. They may be
4029 allocated in ways unknown to the compiler (shared memory,
4030 #pragma weak, etc). */
4031 && ! TREE_PUBLIC (exp)
4032 && ! DECL_EXTERNAL (exp)
4033 /* Loading a static variable is unduly expensive, but global
4034 registers aren't expensive. */
4035 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4038 /* The following functions are subroutines to fold_range_test and allow it to
4039 try to change a logical combination of comparisons into a range test.
4041 For example, both
4042 X == 2 || X == 3 || X == 4 || X == 5
4044 X >= 2 && X <= 5
4045 are converted to
4046 (unsigned) (X - 2) <= 3
4048 We describe each set of comparisons as being either inside or outside
4049 a range, using a variable named like IN_P, and then describe the
4050 range with a lower and upper bound. If one of the bounds is omitted,
4051 it represents either the highest or lowest value of the type.
4053 In the comments below, we represent a range by two numbers in brackets
4054 preceded by a "+" to designate being inside that range, or a "-" to
4055 designate being outside that range, so the condition can be inverted by
4056 flipping the prefix. An omitted bound is represented by a "-". For
4057 example, "- [-, 10]" means being outside the range starting at the lowest
4058 possible value and ending at 10, in other words, being greater than 10.
4059 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4060 always false.
4062 We set up things so that the missing bounds are handled in a consistent
4063 manner so neither a missing bound nor "true" and "false" need to be
4064 handled using a special case. */
4066 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4067 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4068 and UPPER1_P are nonzero if the respective argument is an upper bound
4069 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4070 must be specified for a comparison. ARG1 will be converted to ARG0's
4071 type if both are specified. */
4073 static tree
4074 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4075 tree arg1, int upper1_p)
4077 tree tem;
4078 int result;
4079 int sgn0, sgn1;
4081 /* If neither arg represents infinity, do the normal operation.
4082 Else, if not a comparison, return infinity. Else handle the special
4083 comparison rules. Note that most of the cases below won't occur, but
4084 are handled for consistency. */
4086 if (arg0 != 0 && arg1 != 0)
4088 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4089 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4090 STRIP_NOPS (tem);
4091 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4094 if (TREE_CODE_CLASS (code) != tcc_comparison)
4095 return 0;
4097 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4098 for neither. In real maths, we cannot assume open ended ranges are
4099 the same. But, this is computer arithmetic, where numbers are finite.
4100 We can therefore make the transformation of any unbounded range with
4101 the value Z, Z being greater than any representable number. This permits
4102 us to treat unbounded ranges as equal. */
4103 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4104 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4105 switch (code)
4107 case EQ_EXPR:
4108 result = sgn0 == sgn1;
4109 break;
4110 case NE_EXPR:
4111 result = sgn0 != sgn1;
4112 break;
4113 case LT_EXPR:
4114 result = sgn0 < sgn1;
4115 break;
4116 case LE_EXPR:
4117 result = sgn0 <= sgn1;
4118 break;
4119 case GT_EXPR:
4120 result = sgn0 > sgn1;
4121 break;
4122 case GE_EXPR:
4123 result = sgn0 >= sgn1;
4124 break;
4125 default:
4126 gcc_unreachable ();
4129 return constant_boolean_node (result, type);
4132 /* Given EXP, a logical expression, set the range it is testing into
4133 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4134 actually being tested. *PLOW and *PHIGH will be made of the same
4135 type as the returned expression. If EXP is not a comparison, we
4136 will most likely not be returning a useful value and range. Set
4137 *STRICT_OVERFLOW_P to true if the return value is only valid
4138 because signed overflow is undefined; otherwise, do not change
4139 *STRICT_OVERFLOW_P. */
4141 static tree
4142 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4143 bool *strict_overflow_p)
4145 enum tree_code code;
4146 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4147 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4148 int in_p, n_in_p;
4149 tree low, high, n_low, n_high;
4151 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4152 and see if we can refine the range. Some of the cases below may not
4153 happen, but it doesn't seem worth worrying about this. We "continue"
4154 the outer loop when we've changed something; otherwise we "break"
4155 the switch, which will "break" the while. */
4157 in_p = 0;
4158 low = high = build_int_cst (TREE_TYPE (exp), 0);
4160 while (1)
4162 code = TREE_CODE (exp);
4163 exp_type = TREE_TYPE (exp);
4165 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4167 if (TREE_OPERAND_LENGTH (exp) > 0)
4168 arg0 = TREE_OPERAND (exp, 0);
4169 if (TREE_CODE_CLASS (code) == tcc_comparison
4170 || TREE_CODE_CLASS (code) == tcc_unary
4171 || TREE_CODE_CLASS (code) == tcc_binary)
4172 arg0_type = TREE_TYPE (arg0);
4173 if (TREE_CODE_CLASS (code) == tcc_binary
4174 || TREE_CODE_CLASS (code) == tcc_comparison
4175 || (TREE_CODE_CLASS (code) == tcc_expression
4176 && TREE_OPERAND_LENGTH (exp) > 1))
4177 arg1 = TREE_OPERAND (exp, 1);
4180 switch (code)
4182 case TRUTH_NOT_EXPR:
4183 in_p = ! in_p, exp = arg0;
4184 continue;
4186 case EQ_EXPR: case NE_EXPR:
4187 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4188 /* We can only do something if the range is testing for zero
4189 and if the second operand is an integer constant. Note that
4190 saying something is "in" the range we make is done by
4191 complementing IN_P since it will set in the initial case of
4192 being not equal to zero; "out" is leaving it alone. */
4193 if (low == 0 || high == 0
4194 || ! integer_zerop (low) || ! integer_zerop (high)
4195 || TREE_CODE (arg1) != INTEGER_CST)
4196 break;
4198 switch (code)
4200 case NE_EXPR: /* - [c, c] */
4201 low = high = arg1;
4202 break;
4203 case EQ_EXPR: /* + [c, c] */
4204 in_p = ! in_p, low = high = arg1;
4205 break;
4206 case GT_EXPR: /* - [-, c] */
4207 low = 0, high = arg1;
4208 break;
4209 case GE_EXPR: /* + [c, -] */
4210 in_p = ! in_p, low = arg1, high = 0;
4211 break;
4212 case LT_EXPR: /* - [c, -] */
4213 low = arg1, high = 0;
4214 break;
4215 case LE_EXPR: /* + [-, c] */
4216 in_p = ! in_p, low = 0, high = arg1;
4217 break;
4218 default:
4219 gcc_unreachable ();
4222 /* If this is an unsigned comparison, we also know that EXP is
4223 greater than or equal to zero. We base the range tests we make
4224 on that fact, so we record it here so we can parse existing
4225 range tests. We test arg0_type since often the return type
4226 of, e.g. EQ_EXPR, is boolean. */
4227 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4229 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4230 in_p, low, high, 1,
4231 build_int_cst (arg0_type, 0),
4232 NULL_TREE))
4233 break;
4235 in_p = n_in_p, low = n_low, high = n_high;
4237 /* If the high bound is missing, but we have a nonzero low
4238 bound, reverse the range so it goes from zero to the low bound
4239 minus 1. */
4240 if (high == 0 && low && ! integer_zerop (low))
4242 in_p = ! in_p;
4243 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4244 integer_one_node, 0);
4245 low = build_int_cst (arg0_type, 0);
4249 exp = arg0;
4250 continue;
4252 case NEGATE_EXPR:
4253 /* (-x) IN [a,b] -> x in [-b, -a] */
4254 n_low = range_binop (MINUS_EXPR, exp_type,
4255 build_int_cst (exp_type, 0),
4256 0, high, 1);
4257 n_high = range_binop (MINUS_EXPR, exp_type,
4258 build_int_cst (exp_type, 0),
4259 0, low, 0);
4260 low = n_low, high = n_high;
4261 exp = arg0;
4262 continue;
4264 case BIT_NOT_EXPR:
4265 /* ~ X -> -X - 1 */
4266 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4267 build_int_cst (exp_type, 1));
4268 continue;
4270 case PLUS_EXPR: case MINUS_EXPR:
4271 if (TREE_CODE (arg1) != INTEGER_CST)
4272 break;
4274 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4275 move a constant to the other side. */
4276 if (!TYPE_UNSIGNED (arg0_type)
4277 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4278 break;
4280 /* If EXP is signed, any overflow in the computation is undefined,
4281 so we don't worry about it so long as our computations on
4282 the bounds don't overflow. For unsigned, overflow is defined
4283 and this is exactly the right thing. */
4284 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4285 arg0_type, low, 0, arg1, 0);
4286 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4287 arg0_type, high, 1, arg1, 0);
4288 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4289 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4290 break;
4292 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4293 *strict_overflow_p = true;
4295 /* Check for an unsigned range which has wrapped around the maximum
4296 value thus making n_high < n_low, and normalize it. */
4297 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4299 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4300 integer_one_node, 0);
4301 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4302 integer_one_node, 0);
4304 /* If the range is of the form +/- [ x+1, x ], we won't
4305 be able to normalize it. But then, it represents the
4306 whole range or the empty set, so make it
4307 +/- [ -, - ]. */
4308 if (tree_int_cst_equal (n_low, low)
4309 && tree_int_cst_equal (n_high, high))
4310 low = high = 0;
4311 else
4312 in_p = ! in_p;
4314 else
4315 low = n_low, high = n_high;
4317 exp = arg0;
4318 continue;
4320 CASE_CONVERT: case NON_LVALUE_EXPR:
4321 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4322 break;
4324 if (! INTEGRAL_TYPE_P (arg0_type)
4325 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4326 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4327 break;
4329 n_low = low, n_high = high;
4331 if (n_low != 0)
4332 n_low = fold_convert (arg0_type, n_low);
4334 if (n_high != 0)
4335 n_high = fold_convert (arg0_type, n_high);
4338 /* If we're converting arg0 from an unsigned type, to exp,
4339 a signed type, we will be doing the comparison as unsigned.
4340 The tests above have already verified that LOW and HIGH
4341 are both positive.
4343 So we have to ensure that we will handle large unsigned
4344 values the same way that the current signed bounds treat
4345 negative values. */
4347 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4349 tree high_positive;
4350 tree equiv_type;
4351 /* For fixed-point modes, we need to pass the saturating flag
4352 as the 2nd parameter. */
4353 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4354 equiv_type = lang_hooks.types.type_for_mode
4355 (TYPE_MODE (arg0_type),
4356 TYPE_SATURATING (arg0_type));
4357 else
4358 equiv_type = lang_hooks.types.type_for_mode
4359 (TYPE_MODE (arg0_type), 1);
4361 /* A range without an upper bound is, naturally, unbounded.
4362 Since convert would have cropped a very large value, use
4363 the max value for the destination type. */
4364 high_positive
4365 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4366 : TYPE_MAX_VALUE (arg0_type);
4368 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4369 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4370 fold_convert (arg0_type,
4371 high_positive),
4372 build_int_cst (arg0_type, 1));
4374 /* If the low bound is specified, "and" the range with the
4375 range for which the original unsigned value will be
4376 positive. */
4377 if (low != 0)
4379 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4380 1, n_low, n_high, 1,
4381 fold_convert (arg0_type,
4382 integer_zero_node),
4383 high_positive))
4384 break;
4386 in_p = (n_in_p == in_p);
4388 else
4390 /* Otherwise, "or" the range with the range of the input
4391 that will be interpreted as negative. */
4392 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4393 0, n_low, n_high, 1,
4394 fold_convert (arg0_type,
4395 integer_zero_node),
4396 high_positive))
4397 break;
4399 in_p = (in_p != n_in_p);
4403 exp = arg0;
4404 low = n_low, high = n_high;
4405 continue;
4407 default:
4408 break;
4411 break;
4414 /* If EXP is a constant, we can evaluate whether this is true or false. */
4415 if (TREE_CODE (exp) == INTEGER_CST)
4417 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4418 exp, 0, low, 0))
4419 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4420 exp, 1, high, 1)));
4421 low = high = 0;
4422 exp = 0;
4425 *pin_p = in_p, *plow = low, *phigh = high;
4426 return exp;
4429 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4430 type, TYPE, return an expression to test if EXP is in (or out of, depending
4431 on IN_P) the range. Return 0 if the test couldn't be created. */
4433 static tree
4434 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4436 tree etype = TREE_TYPE (exp);
4437 tree value;
4439 #ifdef HAVE_canonicalize_funcptr_for_compare
4440 /* Disable this optimization for function pointer expressions
4441 on targets that require function pointer canonicalization. */
4442 if (HAVE_canonicalize_funcptr_for_compare
4443 && TREE_CODE (etype) == POINTER_TYPE
4444 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4445 return NULL_TREE;
4446 #endif
4448 if (! in_p)
4450 value = build_range_check (type, exp, 1, low, high);
4451 if (value != 0)
4452 return invert_truthvalue (value);
4454 return 0;
4457 if (low == 0 && high == 0)
4458 return build_int_cst (type, 1);
4460 if (low == 0)
4461 return fold_build2 (LE_EXPR, type, exp,
4462 fold_convert (etype, high));
4464 if (high == 0)
4465 return fold_build2 (GE_EXPR, type, exp,
4466 fold_convert (etype, low));
4468 if (operand_equal_p (low, high, 0))
4469 return fold_build2 (EQ_EXPR, type, exp,
4470 fold_convert (etype, low));
4472 if (integer_zerop (low))
4474 if (! TYPE_UNSIGNED (etype))
4476 etype = unsigned_type_for (etype);
4477 high = fold_convert (etype, high);
4478 exp = fold_convert (etype, exp);
4480 return build_range_check (type, exp, 1, 0, high);
4483 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4484 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4486 unsigned HOST_WIDE_INT lo;
4487 HOST_WIDE_INT hi;
4488 int prec;
4490 prec = TYPE_PRECISION (etype);
4491 if (prec <= HOST_BITS_PER_WIDE_INT)
4493 hi = 0;
4494 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4496 else
4498 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4499 lo = (unsigned HOST_WIDE_INT) -1;
4502 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4504 if (TYPE_UNSIGNED (etype))
4506 etype = signed_type_for (etype);
4507 exp = fold_convert (etype, exp);
4509 return fold_build2 (GT_EXPR, type, exp,
4510 build_int_cst (etype, 0));
4514 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4515 This requires wrap-around arithmetics for the type of the expression. */
4516 switch (TREE_CODE (etype))
4518 case INTEGER_TYPE:
4519 /* There is no requirement that LOW be within the range of ETYPE
4520 if the latter is a subtype. It must, however, be within the base
4521 type of ETYPE. So be sure we do the subtraction in that type. */
4522 if (TREE_TYPE (etype))
4523 etype = TREE_TYPE (etype);
4524 break;
4526 case ENUMERAL_TYPE:
4527 case BOOLEAN_TYPE:
4528 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4529 TYPE_UNSIGNED (etype));
4530 break;
4532 default:
4533 break;
4536 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4537 if (TREE_CODE (etype) == INTEGER_TYPE
4538 && !TYPE_OVERFLOW_WRAPS (etype))
4540 tree utype, minv, maxv;
4542 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4543 for the type in question, as we rely on this here. */
4544 utype = unsigned_type_for (etype);
4545 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4546 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4547 integer_one_node, 1);
4548 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4550 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4551 minv, 1, maxv, 1)))
4552 etype = utype;
4553 else
4554 return 0;
4557 high = fold_convert (etype, high);
4558 low = fold_convert (etype, low);
4559 exp = fold_convert (etype, exp);
4561 value = const_binop (MINUS_EXPR, high, low, 0);
4564 if (POINTER_TYPE_P (etype))
4566 if (value != 0 && !TREE_OVERFLOW (value))
4568 low = fold_convert (sizetype, low);
4569 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4570 return build_range_check (type,
4571 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4572 1, build_int_cst (etype, 0), value);
4574 return 0;
4577 if (value != 0 && !TREE_OVERFLOW (value))
4578 return build_range_check (type,
4579 fold_build2 (MINUS_EXPR, etype, exp, low),
4580 1, build_int_cst (etype, 0), value);
4582 return 0;
4585 /* Return the predecessor of VAL in its type, handling the infinite case. */
4587 static tree
4588 range_predecessor (tree val)
4590 tree type = TREE_TYPE (val);
4592 if (INTEGRAL_TYPE_P (type)
4593 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4594 return 0;
4595 else
4596 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4599 /* Return the successor of VAL in its type, handling the infinite case. */
4601 static tree
4602 range_successor (tree val)
4604 tree type = TREE_TYPE (val);
4606 if (INTEGRAL_TYPE_P (type)
4607 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4608 return 0;
4609 else
4610 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4613 /* Given two ranges, see if we can merge them into one. Return 1 if we
4614 can, 0 if we can't. Set the output range into the specified parameters. */
4616 static int
4617 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4618 tree high0, int in1_p, tree low1, tree high1)
4620 int no_overlap;
4621 int subset;
4622 int temp;
4623 tree tem;
4624 int in_p;
4625 tree low, high;
4626 int lowequal = ((low0 == 0 && low1 == 0)
4627 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4628 low0, 0, low1, 0)));
4629 int highequal = ((high0 == 0 && high1 == 0)
4630 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4631 high0, 1, high1, 1)));
4633 /* Make range 0 be the range that starts first, or ends last if they
4634 start at the same value. Swap them if it isn't. */
4635 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4636 low0, 0, low1, 0))
4637 || (lowequal
4638 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4639 high1, 1, high0, 1))))
4641 temp = in0_p, in0_p = in1_p, in1_p = temp;
4642 tem = low0, low0 = low1, low1 = tem;
4643 tem = high0, high0 = high1, high1 = tem;
4646 /* Now flag two cases, whether the ranges are disjoint or whether the
4647 second range is totally subsumed in the first. Note that the tests
4648 below are simplified by the ones above. */
4649 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4650 high0, 1, low1, 0));
4651 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4652 high1, 1, high0, 1));
4654 /* We now have four cases, depending on whether we are including or
4655 excluding the two ranges. */
4656 if (in0_p && in1_p)
4658 /* If they don't overlap, the result is false. If the second range
4659 is a subset it is the result. Otherwise, the range is from the start
4660 of the second to the end of the first. */
4661 if (no_overlap)
4662 in_p = 0, low = high = 0;
4663 else if (subset)
4664 in_p = 1, low = low1, high = high1;
4665 else
4666 in_p = 1, low = low1, high = high0;
4669 else if (in0_p && ! in1_p)
4671 /* If they don't overlap, the result is the first range. If they are
4672 equal, the result is false. If the second range is a subset of the
4673 first, and the ranges begin at the same place, we go from just after
4674 the end of the second range to the end of the first. If the second
4675 range is not a subset of the first, or if it is a subset and both
4676 ranges end at the same place, the range starts at the start of the
4677 first range and ends just before the second range.
4678 Otherwise, we can't describe this as a single range. */
4679 if (no_overlap)
4680 in_p = 1, low = low0, high = high0;
4681 else if (lowequal && highequal)
4682 in_p = 0, low = high = 0;
4683 else if (subset && lowequal)
4685 low = range_successor (high1);
4686 high = high0;
4687 in_p = 1;
4688 if (low == 0)
4690 /* We are in the weird situation where high0 > high1 but
4691 high1 has no successor. Punt. */
4692 return 0;
4695 else if (! subset || highequal)
4697 low = low0;
4698 high = range_predecessor (low1);
4699 in_p = 1;
4700 if (high == 0)
4702 /* low0 < low1 but low1 has no predecessor. Punt. */
4703 return 0;
4706 else
4707 return 0;
4710 else if (! in0_p && in1_p)
4712 /* If they don't overlap, the result is the second range. If the second
4713 is a subset of the first, the result is false. Otherwise,
4714 the range starts just after the first range and ends at the
4715 end of the second. */
4716 if (no_overlap)
4717 in_p = 1, low = low1, high = high1;
4718 else if (subset || highequal)
4719 in_p = 0, low = high = 0;
4720 else
4722 low = range_successor (high0);
4723 high = high1;
4724 in_p = 1;
4725 if (low == 0)
4727 /* high1 > high0 but high0 has no successor. Punt. */
4728 return 0;
4733 else
4735 /* The case where we are excluding both ranges. Here the complex case
4736 is if they don't overlap. In that case, the only time we have a
4737 range is if they are adjacent. If the second is a subset of the
4738 first, the result is the first. Otherwise, the range to exclude
4739 starts at the beginning of the first range and ends at the end of the
4740 second. */
4741 if (no_overlap)
4743 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4744 range_successor (high0),
4745 1, low1, 0)))
4746 in_p = 0, low = low0, high = high1;
4747 else
4749 /* Canonicalize - [min, x] into - [-, x]. */
4750 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4751 switch (TREE_CODE (TREE_TYPE (low0)))
4753 case ENUMERAL_TYPE:
4754 if (TYPE_PRECISION (TREE_TYPE (low0))
4755 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4756 break;
4757 /* FALLTHROUGH */
4758 case INTEGER_TYPE:
4759 if (tree_int_cst_equal (low0,
4760 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4761 low0 = 0;
4762 break;
4763 case POINTER_TYPE:
4764 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4765 && integer_zerop (low0))
4766 low0 = 0;
4767 break;
4768 default:
4769 break;
4772 /* Canonicalize - [x, max] into - [x, -]. */
4773 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4774 switch (TREE_CODE (TREE_TYPE (high1)))
4776 case ENUMERAL_TYPE:
4777 if (TYPE_PRECISION (TREE_TYPE (high1))
4778 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4779 break;
4780 /* FALLTHROUGH */
4781 case INTEGER_TYPE:
4782 if (tree_int_cst_equal (high1,
4783 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4784 high1 = 0;
4785 break;
4786 case POINTER_TYPE:
4787 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4788 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4789 high1, 1,
4790 integer_one_node, 1)))
4791 high1 = 0;
4792 break;
4793 default:
4794 break;
4797 /* The ranges might be also adjacent between the maximum and
4798 minimum values of the given type. For
4799 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4800 return + [x + 1, y - 1]. */
4801 if (low0 == 0 && high1 == 0)
4803 low = range_successor (high0);
4804 high = range_predecessor (low1);
4805 if (low == 0 || high == 0)
4806 return 0;
4808 in_p = 1;
4810 else
4811 return 0;
4814 else if (subset)
4815 in_p = 0, low = low0, high = high0;
4816 else
4817 in_p = 0, low = low0, high = high1;
4820 *pin_p = in_p, *plow = low, *phigh = high;
4821 return 1;
4825 /* Subroutine of fold, looking inside expressions of the form
4826 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4827 of the COND_EXPR. This function is being used also to optimize
4828 A op B ? C : A, by reversing the comparison first.
4830 Return a folded expression whose code is not a COND_EXPR
4831 anymore, or NULL_TREE if no folding opportunity is found. */
4833 static tree
4834 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4836 enum tree_code comp_code = TREE_CODE (arg0);
4837 tree arg00 = TREE_OPERAND (arg0, 0);
4838 tree arg01 = TREE_OPERAND (arg0, 1);
4839 tree arg1_type = TREE_TYPE (arg1);
4840 tree tem;
4842 STRIP_NOPS (arg1);
4843 STRIP_NOPS (arg2);
4845 /* If we have A op 0 ? A : -A, consider applying the following
4846 transformations:
4848 A == 0? A : -A same as -A
4849 A != 0? A : -A same as A
4850 A >= 0? A : -A same as abs (A)
4851 A > 0? A : -A same as abs (A)
4852 A <= 0? A : -A same as -abs (A)
4853 A < 0? A : -A same as -abs (A)
4855 None of these transformations work for modes with signed
4856 zeros. If A is +/-0, the first two transformations will
4857 change the sign of the result (from +0 to -0, or vice
4858 versa). The last four will fix the sign of the result,
4859 even though the original expressions could be positive or
4860 negative, depending on the sign of A.
4862 Note that all these transformations are correct if A is
4863 NaN, since the two alternatives (A and -A) are also NaNs. */
4864 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4865 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4866 ? real_zerop (arg01)
4867 : integer_zerop (arg01))
4868 && ((TREE_CODE (arg2) == NEGATE_EXPR
4869 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4870 /* In the case that A is of the form X-Y, '-A' (arg2) may
4871 have already been folded to Y-X, check for that. */
4872 || (TREE_CODE (arg1) == MINUS_EXPR
4873 && TREE_CODE (arg2) == MINUS_EXPR
4874 && operand_equal_p (TREE_OPERAND (arg1, 0),
4875 TREE_OPERAND (arg2, 1), 0)
4876 && operand_equal_p (TREE_OPERAND (arg1, 1),
4877 TREE_OPERAND (arg2, 0), 0))))
4878 switch (comp_code)
4880 case EQ_EXPR:
4881 case UNEQ_EXPR:
4882 tem = fold_convert (arg1_type, arg1);
4883 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4884 case NE_EXPR:
4885 case LTGT_EXPR:
4886 return pedantic_non_lvalue (fold_convert (type, arg1));
4887 case UNGE_EXPR:
4888 case UNGT_EXPR:
4889 if (flag_trapping_math)
4890 break;
4891 /* Fall through. */
4892 case GE_EXPR:
4893 case GT_EXPR:
4894 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4895 arg1 = fold_convert (signed_type_for
4896 (TREE_TYPE (arg1)), arg1);
4897 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4898 return pedantic_non_lvalue (fold_convert (type, tem));
4899 case UNLE_EXPR:
4900 case UNLT_EXPR:
4901 if (flag_trapping_math)
4902 break;
4903 case LE_EXPR:
4904 case LT_EXPR:
4905 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4906 arg1 = fold_convert (signed_type_for
4907 (TREE_TYPE (arg1)), arg1);
4908 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4909 return negate_expr (fold_convert (type, tem));
4910 default:
4911 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4912 break;
4915 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4916 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4917 both transformations are correct when A is NaN: A != 0
4918 is then true, and A == 0 is false. */
4920 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4921 && integer_zerop (arg01) && integer_zerop (arg2))
4923 if (comp_code == NE_EXPR)
4924 return pedantic_non_lvalue (fold_convert (type, arg1));
4925 else if (comp_code == EQ_EXPR)
4926 return build_int_cst (type, 0);
4929 /* Try some transformations of A op B ? A : B.
4931 A == B? A : B same as B
4932 A != B? A : B same as A
4933 A >= B? A : B same as max (A, B)
4934 A > B? A : B same as max (B, A)
4935 A <= B? A : B same as min (A, B)
4936 A < B? A : B same as min (B, A)
4938 As above, these transformations don't work in the presence
4939 of signed zeros. For example, if A and B are zeros of
4940 opposite sign, the first two transformations will change
4941 the sign of the result. In the last four, the original
4942 expressions give different results for (A=+0, B=-0) and
4943 (A=-0, B=+0), but the transformed expressions do not.
4945 The first two transformations are correct if either A or B
4946 is a NaN. In the first transformation, the condition will
4947 be false, and B will indeed be chosen. In the case of the
4948 second transformation, the condition A != B will be true,
4949 and A will be chosen.
4951 The conversions to max() and min() are not correct if B is
4952 a number and A is not. The conditions in the original
4953 expressions will be false, so all four give B. The min()
4954 and max() versions would give a NaN instead. */
4955 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4956 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4957 /* Avoid these transformations if the COND_EXPR may be used
4958 as an lvalue in the C++ front-end. PR c++/19199. */
4959 && (in_gimple_form
4960 || (strcmp (lang_hooks.name, "GNU C++") != 0
4961 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4962 || ! maybe_lvalue_p (arg1)
4963 || ! maybe_lvalue_p (arg2)))
4965 tree comp_op0 = arg00;
4966 tree comp_op1 = arg01;
4967 tree comp_type = TREE_TYPE (comp_op0);
4969 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4970 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4972 comp_type = type;
4973 comp_op0 = arg1;
4974 comp_op1 = arg2;
4977 switch (comp_code)
4979 case EQ_EXPR:
4980 return pedantic_non_lvalue (fold_convert (type, arg2));
4981 case NE_EXPR:
4982 return pedantic_non_lvalue (fold_convert (type, arg1));
4983 case LE_EXPR:
4984 case LT_EXPR:
4985 case UNLE_EXPR:
4986 case UNLT_EXPR:
4987 /* In C++ a ?: expression can be an lvalue, so put the
4988 operand which will be used if they are equal first
4989 so that we can convert this back to the
4990 corresponding COND_EXPR. */
4991 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4993 comp_op0 = fold_convert (comp_type, comp_op0);
4994 comp_op1 = fold_convert (comp_type, comp_op1);
4995 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4996 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4997 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4998 return pedantic_non_lvalue (fold_convert (type, tem));
5000 break;
5001 case GE_EXPR:
5002 case GT_EXPR:
5003 case UNGE_EXPR:
5004 case UNGT_EXPR:
5005 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5007 comp_op0 = fold_convert (comp_type, comp_op0);
5008 comp_op1 = fold_convert (comp_type, comp_op1);
5009 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5010 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5011 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5012 return pedantic_non_lvalue (fold_convert (type, tem));
5014 break;
5015 case UNEQ_EXPR:
5016 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5017 return pedantic_non_lvalue (fold_convert (type, arg2));
5018 break;
5019 case LTGT_EXPR:
5020 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5021 return pedantic_non_lvalue (fold_convert (type, arg1));
5022 break;
5023 default:
5024 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5025 break;
5029 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5030 we might still be able to simplify this. For example,
5031 if C1 is one less or one more than C2, this might have started
5032 out as a MIN or MAX and been transformed by this function.
5033 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5035 if (INTEGRAL_TYPE_P (type)
5036 && TREE_CODE (arg01) == INTEGER_CST
5037 && TREE_CODE (arg2) == INTEGER_CST)
5038 switch (comp_code)
5040 case EQ_EXPR:
5041 /* We can replace A with C1 in this case. */
5042 arg1 = fold_convert (type, arg01);
5043 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5045 case LT_EXPR:
5046 /* If C1 is C2 + 1, this is min(A, C2). */
5047 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5048 OEP_ONLY_CONST)
5049 && operand_equal_p (arg01,
5050 const_binop (PLUS_EXPR, arg2,
5051 build_int_cst (type, 1), 0),
5052 OEP_ONLY_CONST))
5053 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5054 type,
5055 fold_convert (type, arg1),
5056 arg2));
5057 break;
5059 case LE_EXPR:
5060 /* If C1 is C2 - 1, this is min(A, C2). */
5061 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5062 OEP_ONLY_CONST)
5063 && operand_equal_p (arg01,
5064 const_binop (MINUS_EXPR, arg2,
5065 build_int_cst (type, 1), 0),
5066 OEP_ONLY_CONST))
5067 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5068 type,
5069 fold_convert (type, arg1),
5070 arg2));
5071 break;
5073 case GT_EXPR:
5074 /* If C1 is C2 - 1, this is max(A, C2). */
5075 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5076 OEP_ONLY_CONST)
5077 && operand_equal_p (arg01,
5078 const_binop (MINUS_EXPR, arg2,
5079 build_int_cst (type, 1), 0),
5080 OEP_ONLY_CONST))
5081 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5082 type,
5083 fold_convert (type, arg1),
5084 arg2));
5085 break;
5087 case GE_EXPR:
5088 /* If C1 is C2 + 1, this is max(A, C2). */
5089 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5090 OEP_ONLY_CONST)
5091 && operand_equal_p (arg01,
5092 const_binop (PLUS_EXPR, arg2,
5093 build_int_cst (type, 1), 0),
5094 OEP_ONLY_CONST))
5095 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5096 type,
5097 fold_convert (type, arg1),
5098 arg2));
5099 break;
5100 case NE_EXPR:
5101 break;
5102 default:
5103 gcc_unreachable ();
5106 return NULL_TREE;
5111 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5112 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5113 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5114 false) >= 2)
5115 #endif
5117 /* EXP is some logical combination of boolean tests. See if we can
5118 merge it into some range test. Return the new tree if so. */
5120 static tree
5121 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5123 int or_op = (code == TRUTH_ORIF_EXPR
5124 || code == TRUTH_OR_EXPR);
5125 int in0_p, in1_p, in_p;
5126 tree low0, low1, low, high0, high1, high;
5127 bool strict_overflow_p = false;
5128 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5129 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5130 tree tem;
5131 const char * const warnmsg = G_("assuming signed overflow does not occur "
5132 "when simplifying range test");
5134 /* If this is an OR operation, invert both sides; we will invert
5135 again at the end. */
5136 if (or_op)
5137 in0_p = ! in0_p, in1_p = ! in1_p;
5139 /* If both expressions are the same, if we can merge the ranges, and we
5140 can build the range test, return it or it inverted. If one of the
5141 ranges is always true or always false, consider it to be the same
5142 expression as the other. */
5143 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5144 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5145 in1_p, low1, high1)
5146 && 0 != (tem = (build_range_check (type,
5147 lhs != 0 ? lhs
5148 : rhs != 0 ? rhs : integer_zero_node,
5149 in_p, low, high))))
5151 if (strict_overflow_p)
5152 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5153 return or_op ? invert_truthvalue (tem) : tem;
5156 /* On machines where the branch cost is expensive, if this is a
5157 short-circuited branch and the underlying object on both sides
5158 is the same, make a non-short-circuit operation. */
5159 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5160 && lhs != 0 && rhs != 0
5161 && (code == TRUTH_ANDIF_EXPR
5162 || code == TRUTH_ORIF_EXPR)
5163 && operand_equal_p (lhs, rhs, 0))
5165 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5166 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5167 which cases we can't do this. */
5168 if (simple_operand_p (lhs))
5169 return build2 (code == TRUTH_ANDIF_EXPR
5170 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5171 type, op0, op1);
5173 else if (lang_hooks.decls.global_bindings_p () == 0
5174 && ! CONTAINS_PLACEHOLDER_P (lhs))
5176 tree common = save_expr (lhs);
5178 if (0 != (lhs = build_range_check (type, common,
5179 or_op ? ! in0_p : in0_p,
5180 low0, high0))
5181 && (0 != (rhs = build_range_check (type, common,
5182 or_op ? ! in1_p : in1_p,
5183 low1, high1))))
5185 if (strict_overflow_p)
5186 fold_overflow_warning (warnmsg,
5187 WARN_STRICT_OVERFLOW_COMPARISON);
5188 return build2 (code == TRUTH_ANDIF_EXPR
5189 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5190 type, lhs, rhs);
5195 return 0;
5198 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5199 bit value. Arrange things so the extra bits will be set to zero if and
5200 only if C is signed-extended to its full width. If MASK is nonzero,
5201 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5203 static tree
5204 unextend (tree c, int p, int unsignedp, tree mask)
5206 tree type = TREE_TYPE (c);
5207 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5208 tree temp;
5210 if (p == modesize || unsignedp)
5211 return c;
5213 /* We work by getting just the sign bit into the low-order bit, then
5214 into the high-order bit, then sign-extend. We then XOR that value
5215 with C. */
5216 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5217 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5219 /* We must use a signed type in order to get an arithmetic right shift.
5220 However, we must also avoid introducing accidental overflows, so that
5221 a subsequent call to integer_zerop will work. Hence we must
5222 do the type conversion here. At this point, the constant is either
5223 zero or one, and the conversion to a signed type can never overflow.
5224 We could get an overflow if this conversion is done anywhere else. */
5225 if (TYPE_UNSIGNED (type))
5226 temp = fold_convert (signed_type_for (type), temp);
5228 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5229 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5230 if (mask != 0)
5231 temp = const_binop (BIT_AND_EXPR, temp,
5232 fold_convert (TREE_TYPE (c), mask), 0);
5233 /* If necessary, convert the type back to match the type of C. */
5234 if (TYPE_UNSIGNED (type))
5235 temp = fold_convert (type, temp);
5237 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5240 /* Find ways of folding logical expressions of LHS and RHS:
5241 Try to merge two comparisons to the same innermost item.
5242 Look for range tests like "ch >= '0' && ch <= '9'".
5243 Look for combinations of simple terms on machines with expensive branches
5244 and evaluate the RHS unconditionally.
5246 For example, if we have p->a == 2 && p->b == 4 and we can make an
5247 object large enough to span both A and B, we can do this with a comparison
5248 against the object ANDed with the a mask.
5250 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5251 operations to do this with one comparison.
5253 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5254 function and the one above.
5256 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5257 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5259 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5260 two operands.
5262 We return the simplified tree or 0 if no optimization is possible. */
5264 static tree
5265 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5267 /* If this is the "or" of two comparisons, we can do something if
5268 the comparisons are NE_EXPR. If this is the "and", we can do something
5269 if the comparisons are EQ_EXPR. I.e.,
5270 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5272 WANTED_CODE is this operation code. For single bit fields, we can
5273 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5274 comparison for one-bit fields. */
5276 enum tree_code wanted_code;
5277 enum tree_code lcode, rcode;
5278 tree ll_arg, lr_arg, rl_arg, rr_arg;
5279 tree ll_inner, lr_inner, rl_inner, rr_inner;
5280 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5281 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5282 HOST_WIDE_INT xll_bitpos, xrl_bitpos;
5283 HOST_WIDE_INT lnbitsize, lnbitpos;
5284 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5285 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5286 enum machine_mode lnmode;
5287 tree ll_mask, lr_mask, rl_mask, rr_mask;
5288 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5289 tree l_const, r_const;
5290 tree lntype, result;
5291 int first_bit, end_bit;
5292 int volatilep;
5293 tree orig_lhs = lhs, orig_rhs = rhs;
5294 enum tree_code orig_code = code;
5296 /* Start by getting the comparison codes. Fail if anything is volatile.
5297 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5298 it were surrounded with a NE_EXPR. */
5300 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5301 return 0;
5303 lcode = TREE_CODE (lhs);
5304 rcode = TREE_CODE (rhs);
5306 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5308 lhs = build2 (NE_EXPR, truth_type, lhs,
5309 build_int_cst (TREE_TYPE (lhs), 0));
5310 lcode = NE_EXPR;
5313 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5315 rhs = build2 (NE_EXPR, truth_type, rhs,
5316 build_int_cst (TREE_TYPE (rhs), 0));
5317 rcode = NE_EXPR;
5320 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5321 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5322 return 0;
5324 ll_arg = TREE_OPERAND (lhs, 0);
5325 lr_arg = TREE_OPERAND (lhs, 1);
5326 rl_arg = TREE_OPERAND (rhs, 0);
5327 rr_arg = TREE_OPERAND (rhs, 1);
5329 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5330 if (simple_operand_p (ll_arg)
5331 && simple_operand_p (lr_arg))
5333 tree result;
5334 if (operand_equal_p (ll_arg, rl_arg, 0)
5335 && operand_equal_p (lr_arg, rr_arg, 0))
5337 result = combine_comparisons (code, lcode, rcode,
5338 truth_type, ll_arg, lr_arg);
5339 if (result)
5340 return result;
5342 else if (operand_equal_p (ll_arg, rr_arg, 0)
5343 && operand_equal_p (lr_arg, rl_arg, 0))
5345 result = combine_comparisons (code, lcode,
5346 swap_tree_comparison (rcode),
5347 truth_type, ll_arg, lr_arg);
5348 if (result)
5349 return result;
5353 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5354 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5356 /* If the RHS can be evaluated unconditionally and its operands are
5357 simple, it wins to evaluate the RHS unconditionally on machines
5358 with expensive branches. In this case, this isn't a comparison
5359 that can be merged. Avoid doing this if the RHS is a floating-point
5360 comparison since those can trap. */
5362 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5363 false) >= 2
5364 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5365 && simple_operand_p (rl_arg)
5366 && simple_operand_p (rr_arg))
5368 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5369 if (code == TRUTH_OR_EXPR
5370 && lcode == NE_EXPR && integer_zerop (lr_arg)
5371 && rcode == NE_EXPR && integer_zerop (rr_arg)
5372 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5373 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5374 return build2 (NE_EXPR, truth_type,
5375 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5376 ll_arg, rl_arg),
5377 build_int_cst (TREE_TYPE (ll_arg), 0));
5379 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5380 if (code == TRUTH_AND_EXPR
5381 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5382 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5383 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5384 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5385 return build2 (EQ_EXPR, truth_type,
5386 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5387 ll_arg, rl_arg),
5388 build_int_cst (TREE_TYPE (ll_arg), 0));
5390 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5392 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5393 return build2 (code, truth_type, lhs, rhs);
5394 return NULL_TREE;
5398 /* See if the comparisons can be merged. Then get all the parameters for
5399 each side. */
5401 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5402 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5403 return 0;
5405 volatilep = 0;
5406 ll_inner = decode_field_reference (ll_arg,
5407 &ll_bitsize, &ll_bitpos, &ll_mode,
5408 &ll_unsignedp, &volatilep, &ll_mask,
5409 &ll_and_mask);
5410 lr_inner = decode_field_reference (lr_arg,
5411 &lr_bitsize, &lr_bitpos, &lr_mode,
5412 &lr_unsignedp, &volatilep, &lr_mask,
5413 &lr_and_mask);
5414 rl_inner = decode_field_reference (rl_arg,
5415 &rl_bitsize, &rl_bitpos, &rl_mode,
5416 &rl_unsignedp, &volatilep, &rl_mask,
5417 &rl_and_mask);
5418 rr_inner = decode_field_reference (rr_arg,
5419 &rr_bitsize, &rr_bitpos, &rr_mode,
5420 &rr_unsignedp, &volatilep, &rr_mask,
5421 &rr_and_mask);
5423 /* It must be true that the inner operation on the lhs of each
5424 comparison must be the same if we are to be able to do anything.
5425 Then see if we have constants. If not, the same must be true for
5426 the rhs's. */
5427 if (volatilep || ll_inner == 0 || rl_inner == 0
5428 || ! operand_equal_p (ll_inner, rl_inner, 0))
5429 return 0;
5431 if (TREE_CODE (lr_arg) == INTEGER_CST
5432 && TREE_CODE (rr_arg) == INTEGER_CST)
5433 l_const = lr_arg, r_const = rr_arg;
5434 else if (lr_inner == 0 || rr_inner == 0
5435 || ! operand_equal_p (lr_inner, rr_inner, 0))
5436 return 0;
5437 else
5438 l_const = r_const = 0;
5440 /* If either comparison code is not correct for our logical operation,
5441 fail. However, we can convert a one-bit comparison against zero into
5442 the opposite comparison against that bit being set in the field. */
5444 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5445 if (lcode != wanted_code)
5447 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5449 /* Make the left operand unsigned, since we are only interested
5450 in the value of one bit. Otherwise we are doing the wrong
5451 thing below. */
5452 ll_unsignedp = 1;
5453 l_const = ll_mask;
5455 else
5456 return 0;
5459 /* This is analogous to the code for l_const above. */
5460 if (rcode != wanted_code)
5462 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5464 rl_unsignedp = 1;
5465 r_const = rl_mask;
5467 else
5468 return 0;
5471 /* See if we can find a mode that contains both fields being compared on
5472 the left. If we can't, fail. Otherwise, update all constants and masks
5473 to be relative to a field of that size. */
5474 first_bit = MIN (ll_bitpos, rl_bitpos);
5475 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5476 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5477 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5478 volatilep);
5479 if (lnmode == VOIDmode)
5480 return 0;
5482 lnbitsize = GET_MODE_BITSIZE (lnmode);
5483 lnbitpos = first_bit & ~ (lnbitsize - 1);
5484 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5485 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5487 if (BYTES_BIG_ENDIAN)
5489 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5490 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5493 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5494 size_int (xll_bitpos), 0);
5495 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5496 size_int (xrl_bitpos), 0);
5498 if (l_const)
5500 l_const = fold_convert (lntype, l_const);
5501 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5502 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5503 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5504 fold_build1 (BIT_NOT_EXPR,
5505 lntype, ll_mask),
5506 0)))
5508 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5510 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5513 if (r_const)
5515 r_const = fold_convert (lntype, r_const);
5516 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5517 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5518 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5519 fold_build1 (BIT_NOT_EXPR,
5520 lntype, rl_mask),
5521 0)))
5523 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5525 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5529 /* Handle the case of comparisons with constants. If there is something in
5530 common between the masks, those bits of the constants must be the same.
5531 If not, the condition is always false. Test for this to avoid generating
5532 incorrect code below. */
5533 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5534 if (! integer_zerop (result)
5535 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5536 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5538 if (wanted_code == NE_EXPR)
5540 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5541 return constant_boolean_node (true, truth_type);
5543 else
5545 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5546 return constant_boolean_node (false, truth_type);
5550 return NULL_TREE;
5553 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5554 constant. */
5556 static tree
5557 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5559 tree arg0 = op0;
5560 enum tree_code op_code;
5561 tree comp_const;
5562 tree minmax_const;
5563 int consts_equal, consts_lt;
5564 tree inner;
5566 STRIP_SIGN_NOPS (arg0);
5568 op_code = TREE_CODE (arg0);
5569 minmax_const = TREE_OPERAND (arg0, 1);
5570 comp_const = fold_convert (TREE_TYPE (arg0), op1);
5571 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5572 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5573 inner = TREE_OPERAND (arg0, 0);
5575 /* If something does not permit us to optimize, return the original tree. */
5576 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5577 || TREE_CODE (comp_const) != INTEGER_CST
5578 || TREE_OVERFLOW (comp_const)
5579 || TREE_CODE (minmax_const) != INTEGER_CST
5580 || TREE_OVERFLOW (minmax_const))
5581 return NULL_TREE;
5583 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5584 and GT_EXPR, doing the rest with recursive calls using logical
5585 simplifications. */
5586 switch (code)
5588 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5590 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5591 type, op0, op1);
5592 if (tem)
5593 return invert_truthvalue (tem);
5594 return NULL_TREE;
5597 case GE_EXPR:
5598 return
5599 fold_build2 (TRUTH_ORIF_EXPR, type,
5600 optimize_minmax_comparison
5601 (EQ_EXPR, type, arg0, comp_const),
5602 optimize_minmax_comparison
5603 (GT_EXPR, type, arg0, comp_const));
5605 case EQ_EXPR:
5606 if (op_code == MAX_EXPR && consts_equal)
5607 /* MAX (X, 0) == 0 -> X <= 0 */
5608 return fold_build2 (LE_EXPR, type, inner, comp_const);
5610 else if (op_code == MAX_EXPR && consts_lt)
5611 /* MAX (X, 0) == 5 -> X == 5 */
5612 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5614 else if (op_code == MAX_EXPR)
5615 /* MAX (X, 0) == -1 -> false */
5616 return omit_one_operand (type, integer_zero_node, inner);
5618 else if (consts_equal)
5619 /* MIN (X, 0) == 0 -> X >= 0 */
5620 return fold_build2 (GE_EXPR, type, inner, comp_const);
5622 else if (consts_lt)
5623 /* MIN (X, 0) == 5 -> false */
5624 return omit_one_operand (type, integer_zero_node, inner);
5626 else
5627 /* MIN (X, 0) == -1 -> X == -1 */
5628 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5630 case GT_EXPR:
5631 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5632 /* MAX (X, 0) > 0 -> X > 0
5633 MAX (X, 0) > 5 -> X > 5 */
5634 return fold_build2 (GT_EXPR, type, inner, comp_const);
5636 else if (op_code == MAX_EXPR)
5637 /* MAX (X, 0) > -1 -> true */
5638 return omit_one_operand (type, integer_one_node, inner);
5640 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5641 /* MIN (X, 0) > 0 -> false
5642 MIN (X, 0) > 5 -> false */
5643 return omit_one_operand (type, integer_zero_node, inner);
5645 else
5646 /* MIN (X, 0) > -1 -> X > -1 */
5647 return fold_build2 (GT_EXPR, type, inner, comp_const);
5649 default:
5650 return NULL_TREE;
5654 /* T is an integer expression that is being multiplied, divided, or taken a
5655 modulus (CODE says which and what kind of divide or modulus) by a
5656 constant C. See if we can eliminate that operation by folding it with
5657 other operations already in T. WIDE_TYPE, if non-null, is a type that
5658 should be used for the computation if wider than our type.
5660 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5661 (X * 2) + (Y * 4). We must, however, be assured that either the original
5662 expression would not overflow or that overflow is undefined for the type
5663 in the language in question.
5665 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5666 the machine has a multiply-accumulate insn or that this is part of an
5667 addressing calculation.
5669 If we return a non-null expression, it is an equivalent form of the
5670 original computation, but need not be in the original type.
5672 We set *STRICT_OVERFLOW_P to true if the return values depends on
5673 signed overflow being undefined. Otherwise we do not change
5674 *STRICT_OVERFLOW_P. */
5676 static tree
5677 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5678 bool *strict_overflow_p)
5680 /* To avoid exponential search depth, refuse to allow recursion past
5681 three levels. Beyond that (1) it's highly unlikely that we'll find
5682 something interesting and (2) we've probably processed it before
5683 when we built the inner expression. */
5685 static int depth;
5686 tree ret;
5688 if (depth > 3)
5689 return NULL;
5691 depth++;
5692 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5693 depth--;
5695 return ret;
5698 static tree
5699 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5700 bool *strict_overflow_p)
5702 tree type = TREE_TYPE (t);
5703 enum tree_code tcode = TREE_CODE (t);
5704 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5705 > GET_MODE_SIZE (TYPE_MODE (type)))
5706 ? wide_type : type);
5707 tree t1, t2;
5708 int same_p = tcode == code;
5709 tree op0 = NULL_TREE, op1 = NULL_TREE;
5710 bool sub_strict_overflow_p;
5712 /* Don't deal with constants of zero here; they confuse the code below. */
5713 if (integer_zerop (c))
5714 return NULL_TREE;
5716 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5717 op0 = TREE_OPERAND (t, 0);
5719 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5720 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5722 /* Note that we need not handle conditional operations here since fold
5723 already handles those cases. So just do arithmetic here. */
5724 switch (tcode)
5726 case INTEGER_CST:
5727 /* For a constant, we can always simplify if we are a multiply
5728 or (for divide and modulus) if it is a multiple of our constant. */
5729 if (code == MULT_EXPR
5730 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5731 return const_binop (code, fold_convert (ctype, t),
5732 fold_convert (ctype, c), 0);
5733 break;
5735 CASE_CONVERT: case NON_LVALUE_EXPR:
5736 /* If op0 is an expression ... */
5737 if ((COMPARISON_CLASS_P (op0)
5738 || UNARY_CLASS_P (op0)
5739 || BINARY_CLASS_P (op0)
5740 || VL_EXP_CLASS_P (op0)
5741 || EXPRESSION_CLASS_P (op0))
5742 /* ... and has wrapping overflow, and its type is smaller
5743 than ctype, then we cannot pass through as widening. */
5744 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5745 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5746 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5747 && (TYPE_PRECISION (ctype)
5748 > TYPE_PRECISION (TREE_TYPE (op0))))
5749 /* ... or this is a truncation (t is narrower than op0),
5750 then we cannot pass through this narrowing. */
5751 || (TYPE_PRECISION (type)
5752 < TYPE_PRECISION (TREE_TYPE (op0)))
5753 /* ... or signedness changes for division or modulus,
5754 then we cannot pass through this conversion. */
5755 || (code != MULT_EXPR
5756 && (TYPE_UNSIGNED (ctype)
5757 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5758 /* ... or has undefined overflow while the converted to
5759 type has not, we cannot do the operation in the inner type
5760 as that would introduce undefined overflow. */
5761 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5762 && !TYPE_OVERFLOW_UNDEFINED (type))))
5763 break;
5765 /* Pass the constant down and see if we can make a simplification. If
5766 we can, replace this expression with the inner simplification for
5767 possible later conversion to our or some other type. */
5768 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5769 && TREE_CODE (t2) == INTEGER_CST
5770 && !TREE_OVERFLOW (t2)
5771 && (0 != (t1 = extract_muldiv (op0, t2, code,
5772 code == MULT_EXPR
5773 ? ctype : NULL_TREE,
5774 strict_overflow_p))))
5775 return t1;
5776 break;
5778 case ABS_EXPR:
5779 /* If widening the type changes it from signed to unsigned, then we
5780 must avoid building ABS_EXPR itself as unsigned. */
5781 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5783 tree cstype = (*signed_type_for) (ctype);
5784 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5785 != 0)
5787 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5788 return fold_convert (ctype, t1);
5790 break;
5792 /* If the constant is negative, we cannot simplify this. */
5793 if (tree_int_cst_sgn (c) == -1)
5794 break;
5795 /* FALLTHROUGH */
5796 case NEGATE_EXPR:
5797 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5798 != 0)
5799 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5800 break;
5802 case MIN_EXPR: case MAX_EXPR:
5803 /* If widening the type changes the signedness, then we can't perform
5804 this optimization as that changes the result. */
5805 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5806 break;
5808 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5809 sub_strict_overflow_p = false;
5810 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5811 &sub_strict_overflow_p)) != 0
5812 && (t2 = extract_muldiv (op1, c, code, wide_type,
5813 &sub_strict_overflow_p)) != 0)
5815 if (tree_int_cst_sgn (c) < 0)
5816 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5817 if (sub_strict_overflow_p)
5818 *strict_overflow_p = true;
5819 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5820 fold_convert (ctype, t2));
5822 break;
5824 case LSHIFT_EXPR: case RSHIFT_EXPR:
5825 /* If the second operand is constant, this is a multiplication
5826 or floor division, by a power of two, so we can treat it that
5827 way unless the multiplier or divisor overflows. Signed
5828 left-shift overflow is implementation-defined rather than
5829 undefined in C90, so do not convert signed left shift into
5830 multiplication. */
5831 if (TREE_CODE (op1) == INTEGER_CST
5832 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5833 /* const_binop may not detect overflow correctly,
5834 so check for it explicitly here. */
5835 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5836 && TREE_INT_CST_HIGH (op1) == 0
5837 && 0 != (t1 = fold_convert (ctype,
5838 const_binop (LSHIFT_EXPR,
5839 size_one_node,
5840 op1, 0)))
5841 && !TREE_OVERFLOW (t1))
5842 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5843 ? MULT_EXPR : FLOOR_DIV_EXPR,
5844 ctype, fold_convert (ctype, op0), t1),
5845 c, code, wide_type, strict_overflow_p);
5846 break;
5848 case PLUS_EXPR: case MINUS_EXPR:
5849 /* See if we can eliminate the operation on both sides. If we can, we
5850 can return a new PLUS or MINUS. If we can't, the only remaining
5851 cases where we can do anything are if the second operand is a
5852 constant. */
5853 sub_strict_overflow_p = false;
5854 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5855 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5856 if (t1 != 0 && t2 != 0
5857 && (code == MULT_EXPR
5858 /* If not multiplication, we can only do this if both operands
5859 are divisible by c. */
5860 || (multiple_of_p (ctype, op0, c)
5861 && multiple_of_p (ctype, op1, c))))
5863 if (sub_strict_overflow_p)
5864 *strict_overflow_p = true;
5865 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5866 fold_convert (ctype, t2));
5869 /* If this was a subtraction, negate OP1 and set it to be an addition.
5870 This simplifies the logic below. */
5871 if (tcode == MINUS_EXPR)
5872 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5874 if (TREE_CODE (op1) != INTEGER_CST)
5875 break;
5877 /* If either OP1 or C are negative, this optimization is not safe for
5878 some of the division and remainder types while for others we need
5879 to change the code. */
5880 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5882 if (code == CEIL_DIV_EXPR)
5883 code = FLOOR_DIV_EXPR;
5884 else if (code == FLOOR_DIV_EXPR)
5885 code = CEIL_DIV_EXPR;
5886 else if (code != MULT_EXPR
5887 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5888 break;
5891 /* If it's a multiply or a division/modulus operation of a multiple
5892 of our constant, do the operation and verify it doesn't overflow. */
5893 if (code == MULT_EXPR
5894 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5896 op1 = const_binop (code, fold_convert (ctype, op1),
5897 fold_convert (ctype, c), 0);
5898 /* We allow the constant to overflow with wrapping semantics. */
5899 if (op1 == 0
5900 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5901 break;
5903 else
5904 break;
5906 /* If we have an unsigned type is not a sizetype, we cannot widen
5907 the operation since it will change the result if the original
5908 computation overflowed. */
5909 if (TYPE_UNSIGNED (ctype)
5910 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5911 && ctype != type)
5912 break;
5914 /* If we were able to eliminate our operation from the first side,
5915 apply our operation to the second side and reform the PLUS. */
5916 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5917 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5919 /* The last case is if we are a multiply. In that case, we can
5920 apply the distributive law to commute the multiply and addition
5921 if the multiplication of the constants doesn't overflow. */
5922 if (code == MULT_EXPR)
5923 return fold_build2 (tcode, ctype,
5924 fold_build2 (code, ctype,
5925 fold_convert (ctype, op0),
5926 fold_convert (ctype, c)),
5927 op1);
5929 break;
5931 case MULT_EXPR:
5932 /* We have a special case here if we are doing something like
5933 (C * 8) % 4 since we know that's zero. */
5934 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5935 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5936 /* If the multiplication can overflow we cannot optimize this.
5937 ??? Until we can properly mark individual operations as
5938 not overflowing we need to treat sizetype special here as
5939 stor-layout relies on this opimization to make
5940 DECL_FIELD_BIT_OFFSET always a constant. */
5941 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5942 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5943 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5944 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5945 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5947 *strict_overflow_p = true;
5948 return omit_one_operand (type, integer_zero_node, op0);
5951 /* ... fall through ... */
5953 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5954 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5955 /* If we can extract our operation from the LHS, do so and return a
5956 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5957 do something only if the second operand is a constant. */
5958 if (same_p
5959 && (t1 = extract_muldiv (op0, c, code, wide_type,
5960 strict_overflow_p)) != 0)
5961 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5962 fold_convert (ctype, op1));
5963 else if (tcode == MULT_EXPR && code == MULT_EXPR
5964 && (t1 = extract_muldiv (op1, c, code, wide_type,
5965 strict_overflow_p)) != 0)
5966 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5967 fold_convert (ctype, t1));
5968 else if (TREE_CODE (op1) != INTEGER_CST)
5969 return 0;
5971 /* If these are the same operation types, we can associate them
5972 assuming no overflow. */
5973 if (tcode == code
5974 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
5975 fold_convert (ctype, c), 1))
5976 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
5977 TREE_INT_CST_HIGH (t1),
5978 (TYPE_UNSIGNED (ctype)
5979 && tcode != MULT_EXPR) ? -1 : 1,
5980 TREE_OVERFLOW (t1)))
5981 && !TREE_OVERFLOW (t1))
5982 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5984 /* If these operations "cancel" each other, we have the main
5985 optimizations of this pass, which occur when either constant is a
5986 multiple of the other, in which case we replace this with either an
5987 operation or CODE or TCODE.
5989 If we have an unsigned type that is not a sizetype, we cannot do
5990 this since it will change the result if the original computation
5991 overflowed. */
5992 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5993 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5994 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5995 || (tcode == MULT_EXPR
5996 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5997 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5998 && code != MULT_EXPR)))
6000 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6002 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6003 *strict_overflow_p = true;
6004 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6005 fold_convert (ctype,
6006 const_binop (TRUNC_DIV_EXPR,
6007 op1, c, 0)));
6009 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6011 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6012 *strict_overflow_p = true;
6013 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6014 fold_convert (ctype,
6015 const_binop (TRUNC_DIV_EXPR,
6016 c, op1, 0)));
6019 break;
6021 default:
6022 break;
6025 return 0;
6028 /* Return a node which has the indicated constant VALUE (either 0 or
6029 1), and is of the indicated TYPE. */
6031 tree
6032 constant_boolean_node (int value, tree type)
6034 if (type == integer_type_node)
6035 return value ? integer_one_node : integer_zero_node;
6036 else if (type == boolean_type_node)
6037 return value ? boolean_true_node : boolean_false_node;
6038 else
6039 return build_int_cst (type, value);
6043 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6044 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6045 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6046 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6047 COND is the first argument to CODE; otherwise (as in the example
6048 given here), it is the second argument. TYPE is the type of the
6049 original expression. Return NULL_TREE if no simplification is
6050 possible. */
6052 static tree
6053 fold_binary_op_with_conditional_arg (enum tree_code code,
6054 tree type, tree op0, tree op1,
6055 tree cond, tree arg, int cond_first_p)
6057 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6058 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6059 tree test, true_value, false_value;
6060 tree lhs = NULL_TREE;
6061 tree rhs = NULL_TREE;
6063 /* This transformation is only worthwhile if we don't have to wrap
6064 arg in a SAVE_EXPR, and the operation can be simplified on at least
6065 one of the branches once its pushed inside the COND_EXPR. */
6066 if (!TREE_CONSTANT (arg))
6067 return NULL_TREE;
6069 if (TREE_CODE (cond) == COND_EXPR)
6071 test = TREE_OPERAND (cond, 0);
6072 true_value = TREE_OPERAND (cond, 1);
6073 false_value = TREE_OPERAND (cond, 2);
6074 /* If this operand throws an expression, then it does not make
6075 sense to try to perform a logical or arithmetic operation
6076 involving it. */
6077 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6078 lhs = true_value;
6079 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6080 rhs = false_value;
6082 else
6084 tree testtype = TREE_TYPE (cond);
6085 test = cond;
6086 true_value = constant_boolean_node (true, testtype);
6087 false_value = constant_boolean_node (false, testtype);
6090 arg = fold_convert (arg_type, arg);
6091 if (lhs == 0)
6093 true_value = fold_convert (cond_type, true_value);
6094 if (cond_first_p)
6095 lhs = fold_build2 (code, type, true_value, arg);
6096 else
6097 lhs = fold_build2 (code, type, arg, true_value);
6099 if (rhs == 0)
6101 false_value = fold_convert (cond_type, false_value);
6102 if (cond_first_p)
6103 rhs = fold_build2 (code, type, false_value, arg);
6104 else
6105 rhs = fold_build2 (code, type, arg, false_value);
6108 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6109 return fold_convert (type, test);
6113 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6115 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6116 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6117 ADDEND is the same as X.
6119 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6120 and finite. The problematic cases are when X is zero, and its mode
6121 has signed zeros. In the case of rounding towards -infinity,
6122 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6123 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6125 bool
6126 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6128 if (!real_zerop (addend))
6129 return false;
6131 /* Don't allow the fold with -fsignaling-nans. */
6132 if (HONOR_SNANS (TYPE_MODE (type)))
6133 return false;
6135 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6136 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6137 return true;
6139 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6140 if (TREE_CODE (addend) == REAL_CST
6141 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6142 negate = !negate;
6144 /* The mode has signed zeros, and we have to honor their sign.
6145 In this situation, there is only one case we can return true for.
6146 X - 0 is the same as X unless rounding towards -infinity is
6147 supported. */
6148 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6151 /* Subroutine of fold() that checks comparisons of built-in math
6152 functions against real constants.
6154 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6155 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6156 is the type of the result and ARG0 and ARG1 are the operands of the
6157 comparison. ARG1 must be a TREE_REAL_CST.
6159 The function returns the constant folded tree if a simplification
6160 can be made, and NULL_TREE otherwise. */
6162 static tree
6163 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6164 tree type, tree arg0, tree arg1)
6166 REAL_VALUE_TYPE c;
6168 if (BUILTIN_SQRT_P (fcode))
6170 tree arg = CALL_EXPR_ARG (arg0, 0);
6171 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6173 c = TREE_REAL_CST (arg1);
6174 if (REAL_VALUE_NEGATIVE (c))
6176 /* sqrt(x) < y is always false, if y is negative. */
6177 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6178 return omit_one_operand (type, integer_zero_node, arg);
6180 /* sqrt(x) > y is always true, if y is negative and we
6181 don't care about NaNs, i.e. negative values of x. */
6182 if (code == NE_EXPR || !HONOR_NANS (mode))
6183 return omit_one_operand (type, integer_one_node, arg);
6185 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6186 return fold_build2 (GE_EXPR, type, arg,
6187 build_real (TREE_TYPE (arg), dconst0));
6189 else if (code == GT_EXPR || code == GE_EXPR)
6191 REAL_VALUE_TYPE c2;
6193 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6194 real_convert (&c2, mode, &c2);
6196 if (REAL_VALUE_ISINF (c2))
6198 /* sqrt(x) > y is x == +Inf, when y is very large. */
6199 if (HONOR_INFINITIES (mode))
6200 return fold_build2 (EQ_EXPR, type, arg,
6201 build_real (TREE_TYPE (arg), c2));
6203 /* sqrt(x) > y is always false, when y is very large
6204 and we don't care about infinities. */
6205 return omit_one_operand (type, integer_zero_node, arg);
6208 /* sqrt(x) > c is the same as x > c*c. */
6209 return fold_build2 (code, type, arg,
6210 build_real (TREE_TYPE (arg), c2));
6212 else if (code == LT_EXPR || code == LE_EXPR)
6214 REAL_VALUE_TYPE c2;
6216 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6217 real_convert (&c2, mode, &c2);
6219 if (REAL_VALUE_ISINF (c2))
6221 /* sqrt(x) < y is always true, when y is a very large
6222 value and we don't care about NaNs or Infinities. */
6223 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6224 return omit_one_operand (type, integer_one_node, arg);
6226 /* sqrt(x) < y is x != +Inf when y is very large and we
6227 don't care about NaNs. */
6228 if (! HONOR_NANS (mode))
6229 return fold_build2 (NE_EXPR, type, arg,
6230 build_real (TREE_TYPE (arg), c2));
6232 /* sqrt(x) < y is x >= 0 when y is very large and we
6233 don't care about Infinities. */
6234 if (! HONOR_INFINITIES (mode))
6235 return fold_build2 (GE_EXPR, type, arg,
6236 build_real (TREE_TYPE (arg), dconst0));
6238 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6239 if (lang_hooks.decls.global_bindings_p () != 0
6240 || CONTAINS_PLACEHOLDER_P (arg))
6241 return NULL_TREE;
6243 arg = save_expr (arg);
6244 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6245 fold_build2 (GE_EXPR, type, arg,
6246 build_real (TREE_TYPE (arg),
6247 dconst0)),
6248 fold_build2 (NE_EXPR, type, arg,
6249 build_real (TREE_TYPE (arg),
6250 c2)));
6253 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6254 if (! HONOR_NANS (mode))
6255 return fold_build2 (code, type, arg,
6256 build_real (TREE_TYPE (arg), c2));
6258 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6259 if (lang_hooks.decls.global_bindings_p () == 0
6260 && ! CONTAINS_PLACEHOLDER_P (arg))
6262 arg = save_expr (arg);
6263 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6264 fold_build2 (GE_EXPR, type, arg,
6265 build_real (TREE_TYPE (arg),
6266 dconst0)),
6267 fold_build2 (code, type, arg,
6268 build_real (TREE_TYPE (arg),
6269 c2)));
6274 return NULL_TREE;
6277 /* Subroutine of fold() that optimizes comparisons against Infinities,
6278 either +Inf or -Inf.
6280 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6281 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6282 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6284 The function returns the constant folded tree if a simplification
6285 can be made, and NULL_TREE otherwise. */
6287 static tree
6288 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6290 enum machine_mode mode;
6291 REAL_VALUE_TYPE max;
6292 tree temp;
6293 bool neg;
6295 mode = TYPE_MODE (TREE_TYPE (arg0));
6297 /* For negative infinity swap the sense of the comparison. */
6298 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6299 if (neg)
6300 code = swap_tree_comparison (code);
6302 switch (code)
6304 case GT_EXPR:
6305 /* x > +Inf is always false, if with ignore sNANs. */
6306 if (HONOR_SNANS (mode))
6307 return NULL_TREE;
6308 return omit_one_operand (type, integer_zero_node, arg0);
6310 case LE_EXPR:
6311 /* x <= +Inf is always true, if we don't case about NaNs. */
6312 if (! HONOR_NANS (mode))
6313 return omit_one_operand (type, integer_one_node, arg0);
6315 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6316 if (lang_hooks.decls.global_bindings_p () == 0
6317 && ! CONTAINS_PLACEHOLDER_P (arg0))
6319 arg0 = save_expr (arg0);
6320 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6322 break;
6324 case EQ_EXPR:
6325 case GE_EXPR:
6326 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6327 real_maxval (&max, neg, mode);
6328 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6329 arg0, build_real (TREE_TYPE (arg0), max));
6331 case LT_EXPR:
6332 /* x < +Inf is always equal to x <= DBL_MAX. */
6333 real_maxval (&max, neg, mode);
6334 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6335 arg0, build_real (TREE_TYPE (arg0), max));
6337 case NE_EXPR:
6338 /* x != +Inf is always equal to !(x > DBL_MAX). */
6339 real_maxval (&max, neg, mode);
6340 if (! HONOR_NANS (mode))
6341 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6342 arg0, build_real (TREE_TYPE (arg0), max));
6344 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6345 arg0, build_real (TREE_TYPE (arg0), max));
6346 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6348 default:
6349 break;
6352 return NULL_TREE;
6355 /* Subroutine of fold() that optimizes comparisons of a division by
6356 a nonzero integer constant against an integer constant, i.e.
6357 X/C1 op C2.
6359 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6360 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6361 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6363 The function returns the constant folded tree if a simplification
6364 can be made, and NULL_TREE otherwise. */
6366 static tree
6367 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6369 tree prod, tmp, hi, lo;
6370 tree arg00 = TREE_OPERAND (arg0, 0);
6371 tree arg01 = TREE_OPERAND (arg0, 1);
6372 unsigned HOST_WIDE_INT lpart;
6373 HOST_WIDE_INT hpart;
6374 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6375 bool neg_overflow;
6376 int overflow;
6378 /* We have to do this the hard way to detect unsigned overflow.
6379 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6380 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6381 TREE_INT_CST_HIGH (arg01),
6382 TREE_INT_CST_LOW (arg1),
6383 TREE_INT_CST_HIGH (arg1),
6384 &lpart, &hpart, unsigned_p);
6385 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6386 -1, overflow);
6387 neg_overflow = false;
6389 if (unsigned_p)
6391 tmp = int_const_binop (MINUS_EXPR, arg01,
6392 build_int_cst (TREE_TYPE (arg01), 1), 0);
6393 lo = prod;
6395 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6396 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6397 TREE_INT_CST_HIGH (prod),
6398 TREE_INT_CST_LOW (tmp),
6399 TREE_INT_CST_HIGH (tmp),
6400 &lpart, &hpart, unsigned_p);
6401 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6402 -1, overflow | TREE_OVERFLOW (prod));
6404 else if (tree_int_cst_sgn (arg01) >= 0)
6406 tmp = int_const_binop (MINUS_EXPR, arg01,
6407 build_int_cst (TREE_TYPE (arg01), 1), 0);
6408 switch (tree_int_cst_sgn (arg1))
6410 case -1:
6411 neg_overflow = true;
6412 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6413 hi = prod;
6414 break;
6416 case 0:
6417 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6418 hi = tmp;
6419 break;
6421 case 1:
6422 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6423 lo = prod;
6424 break;
6426 default:
6427 gcc_unreachable ();
6430 else
6432 /* A negative divisor reverses the relational operators. */
6433 code = swap_tree_comparison (code);
6435 tmp = int_const_binop (PLUS_EXPR, arg01,
6436 build_int_cst (TREE_TYPE (arg01), 1), 0);
6437 switch (tree_int_cst_sgn (arg1))
6439 case -1:
6440 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6441 lo = prod;
6442 break;
6444 case 0:
6445 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6446 lo = tmp;
6447 break;
6449 case 1:
6450 neg_overflow = true;
6451 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6452 hi = prod;
6453 break;
6455 default:
6456 gcc_unreachable ();
6460 switch (code)
6462 case EQ_EXPR:
6463 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6464 return omit_one_operand (type, integer_zero_node, arg00);
6465 if (TREE_OVERFLOW (hi))
6466 return fold_build2 (GE_EXPR, type, arg00, lo);
6467 if (TREE_OVERFLOW (lo))
6468 return fold_build2 (LE_EXPR, type, arg00, hi);
6469 return build_range_check (type, arg00, 1, lo, hi);
6471 case NE_EXPR:
6472 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6473 return omit_one_operand (type, integer_one_node, arg00);
6474 if (TREE_OVERFLOW (hi))
6475 return fold_build2 (LT_EXPR, type, arg00, lo);
6476 if (TREE_OVERFLOW (lo))
6477 return fold_build2 (GT_EXPR, type, arg00, hi);
6478 return build_range_check (type, arg00, 0, lo, hi);
6480 case LT_EXPR:
6481 if (TREE_OVERFLOW (lo))
6483 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6484 return omit_one_operand (type, tmp, arg00);
6486 return fold_build2 (LT_EXPR, type, arg00, lo);
6488 case LE_EXPR:
6489 if (TREE_OVERFLOW (hi))
6491 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6492 return omit_one_operand (type, tmp, arg00);
6494 return fold_build2 (LE_EXPR, type, arg00, hi);
6496 case GT_EXPR:
6497 if (TREE_OVERFLOW (hi))
6499 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6500 return omit_one_operand (type, tmp, arg00);
6502 return fold_build2 (GT_EXPR, type, arg00, hi);
6504 case GE_EXPR:
6505 if (TREE_OVERFLOW (lo))
6507 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6508 return omit_one_operand (type, tmp, arg00);
6510 return fold_build2 (GE_EXPR, type, arg00, lo);
6512 default:
6513 break;
6516 return NULL_TREE;
6520 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6521 equality/inequality test, then return a simplified form of the test
6522 using a sign testing. Otherwise return NULL. TYPE is the desired
6523 result type. */
6525 static tree
6526 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6527 tree result_type)
6529 /* If this is testing a single bit, we can optimize the test. */
6530 if ((code == NE_EXPR || code == EQ_EXPR)
6531 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6532 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6534 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6535 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6536 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6538 if (arg00 != NULL_TREE
6539 /* This is only a win if casting to a signed type is cheap,
6540 i.e. when arg00's type is not a partial mode. */
6541 && TYPE_PRECISION (TREE_TYPE (arg00))
6542 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6544 tree stype = signed_type_for (TREE_TYPE (arg00));
6545 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6546 result_type, fold_convert (stype, arg00),
6547 build_int_cst (stype, 0));
6551 return NULL_TREE;
6554 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6555 equality/inequality test, then return a simplified form of
6556 the test using shifts and logical operations. Otherwise return
6557 NULL. TYPE is the desired result type. */
6559 tree
6560 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6561 tree result_type)
6563 /* If this is testing a single bit, we can optimize the test. */
6564 if ((code == NE_EXPR || code == EQ_EXPR)
6565 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6566 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6568 tree inner = TREE_OPERAND (arg0, 0);
6569 tree type = TREE_TYPE (arg0);
6570 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6571 enum machine_mode operand_mode = TYPE_MODE (type);
6572 int ops_unsigned;
6573 tree signed_type, unsigned_type, intermediate_type;
6574 tree tem, one;
6576 /* First, see if we can fold the single bit test into a sign-bit
6577 test. */
6578 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6579 result_type);
6580 if (tem)
6581 return tem;
6583 /* Otherwise we have (A & C) != 0 where C is a single bit,
6584 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6585 Similarly for (A & C) == 0. */
6587 /* If INNER is a right shift of a constant and it plus BITNUM does
6588 not overflow, adjust BITNUM and INNER. */
6589 if (TREE_CODE (inner) == RSHIFT_EXPR
6590 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6591 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6592 && bitnum < TYPE_PRECISION (type)
6593 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6594 bitnum - TYPE_PRECISION (type)))
6596 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6597 inner = TREE_OPERAND (inner, 0);
6600 /* If we are going to be able to omit the AND below, we must do our
6601 operations as unsigned. If we must use the AND, we have a choice.
6602 Normally unsigned is faster, but for some machines signed is. */
6603 #ifdef LOAD_EXTEND_OP
6604 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6605 && !flag_syntax_only) ? 0 : 1;
6606 #else
6607 ops_unsigned = 1;
6608 #endif
6610 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6611 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6612 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6613 inner = fold_convert (intermediate_type, inner);
6615 if (bitnum != 0)
6616 inner = build2 (RSHIFT_EXPR, intermediate_type,
6617 inner, size_int (bitnum));
6619 one = build_int_cst (intermediate_type, 1);
6621 if (code == EQ_EXPR)
6622 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6624 /* Put the AND last so it can combine with more things. */
6625 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6627 /* Make sure to return the proper type. */
6628 inner = fold_convert (result_type, inner);
6630 return inner;
6632 return NULL_TREE;
6635 /* Check whether we are allowed to reorder operands arg0 and arg1,
6636 such that the evaluation of arg1 occurs before arg0. */
6638 static bool
6639 reorder_operands_p (const_tree arg0, const_tree arg1)
6641 if (! flag_evaluation_order)
6642 return true;
6643 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6644 return true;
6645 return ! TREE_SIDE_EFFECTS (arg0)
6646 && ! TREE_SIDE_EFFECTS (arg1);
6649 /* Test whether it is preferable two swap two operands, ARG0 and
6650 ARG1, for example because ARG0 is an integer constant and ARG1
6651 isn't. If REORDER is true, only recommend swapping if we can
6652 evaluate the operands in reverse order. */
6654 bool
6655 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6657 STRIP_SIGN_NOPS (arg0);
6658 STRIP_SIGN_NOPS (arg1);
6660 if (TREE_CODE (arg1) == INTEGER_CST)
6661 return 0;
6662 if (TREE_CODE (arg0) == INTEGER_CST)
6663 return 1;
6665 if (TREE_CODE (arg1) == REAL_CST)
6666 return 0;
6667 if (TREE_CODE (arg0) == REAL_CST)
6668 return 1;
6670 if (TREE_CODE (arg1) == FIXED_CST)
6671 return 0;
6672 if (TREE_CODE (arg0) == FIXED_CST)
6673 return 1;
6675 if (TREE_CODE (arg1) == COMPLEX_CST)
6676 return 0;
6677 if (TREE_CODE (arg0) == COMPLEX_CST)
6678 return 1;
6680 if (TREE_CONSTANT (arg1))
6681 return 0;
6682 if (TREE_CONSTANT (arg0))
6683 return 1;
6685 if (optimize_function_for_size_p (cfun))
6686 return 0;
6688 if (reorder && flag_evaluation_order
6689 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6690 return 0;
6692 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6693 for commutative and comparison operators. Ensuring a canonical
6694 form allows the optimizers to find additional redundancies without
6695 having to explicitly check for both orderings. */
6696 if (TREE_CODE (arg0) == SSA_NAME
6697 && TREE_CODE (arg1) == SSA_NAME
6698 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6699 return 1;
6701 /* Put SSA_NAMEs last. */
6702 if (TREE_CODE (arg1) == SSA_NAME)
6703 return 0;
6704 if (TREE_CODE (arg0) == SSA_NAME)
6705 return 1;
6707 /* Put variables last. */
6708 if (DECL_P (arg1))
6709 return 0;
6710 if (DECL_P (arg0))
6711 return 1;
6713 return 0;
6716 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6717 ARG0 is extended to a wider type. */
6719 static tree
6720 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6722 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6723 tree arg1_unw;
6724 tree shorter_type, outer_type;
6725 tree min, max;
6726 bool above, below;
6728 if (arg0_unw == arg0)
6729 return NULL_TREE;
6730 shorter_type = TREE_TYPE (arg0_unw);
6732 #ifdef HAVE_canonicalize_funcptr_for_compare
6733 /* Disable this optimization if we're casting a function pointer
6734 type on targets that require function pointer canonicalization. */
6735 if (HAVE_canonicalize_funcptr_for_compare
6736 && TREE_CODE (shorter_type) == POINTER_TYPE
6737 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6738 return NULL_TREE;
6739 #endif
6741 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6742 return NULL_TREE;
6744 arg1_unw = get_unwidened (arg1, NULL_TREE);
6746 /* If possible, express the comparison in the shorter mode. */
6747 if ((code == EQ_EXPR || code == NE_EXPR
6748 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6749 && (TREE_TYPE (arg1_unw) == shorter_type
6750 || ((TYPE_PRECISION (shorter_type)
6751 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6752 && (TYPE_UNSIGNED (shorter_type)
6753 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6754 || (TREE_CODE (arg1_unw) == INTEGER_CST
6755 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6756 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6757 && int_fits_type_p (arg1_unw, shorter_type))))
6758 return fold_build2 (code, type, arg0_unw,
6759 fold_convert (shorter_type, arg1_unw));
6761 if (TREE_CODE (arg1_unw) != INTEGER_CST
6762 || TREE_CODE (shorter_type) != INTEGER_TYPE
6763 || !int_fits_type_p (arg1_unw, shorter_type))
6764 return NULL_TREE;
6766 /* If we are comparing with the integer that does not fit into the range
6767 of the shorter type, the result is known. */
6768 outer_type = TREE_TYPE (arg1_unw);
6769 min = lower_bound_in_type (outer_type, shorter_type);
6770 max = upper_bound_in_type (outer_type, shorter_type);
6772 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6773 max, arg1_unw));
6774 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6775 arg1_unw, min));
6777 switch (code)
6779 case EQ_EXPR:
6780 if (above || below)
6781 return omit_one_operand (type, integer_zero_node, arg0);
6782 break;
6784 case NE_EXPR:
6785 if (above || below)
6786 return omit_one_operand (type, integer_one_node, arg0);
6787 break;
6789 case LT_EXPR:
6790 case LE_EXPR:
6791 if (above)
6792 return omit_one_operand (type, integer_one_node, arg0);
6793 else if (below)
6794 return omit_one_operand (type, integer_zero_node, arg0);
6796 case GT_EXPR:
6797 case GE_EXPR:
6798 if (above)
6799 return omit_one_operand (type, integer_zero_node, arg0);
6800 else if (below)
6801 return omit_one_operand (type, integer_one_node, arg0);
6803 default:
6804 break;
6807 return NULL_TREE;
6810 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6811 ARG0 just the signedness is changed. */
6813 static tree
6814 fold_sign_changed_comparison (enum tree_code code, tree type,
6815 tree arg0, tree arg1)
6817 tree arg0_inner;
6818 tree inner_type, outer_type;
6820 if (!CONVERT_EXPR_P (arg0))
6821 return NULL_TREE;
6823 outer_type = TREE_TYPE (arg0);
6824 arg0_inner = TREE_OPERAND (arg0, 0);
6825 inner_type = TREE_TYPE (arg0_inner);
6827 #ifdef HAVE_canonicalize_funcptr_for_compare
6828 /* Disable this optimization if we're casting a function pointer
6829 type on targets that require function pointer canonicalization. */
6830 if (HAVE_canonicalize_funcptr_for_compare
6831 && TREE_CODE (inner_type) == POINTER_TYPE
6832 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6833 return NULL_TREE;
6834 #endif
6836 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6837 return NULL_TREE;
6839 /* If the conversion is from an integral subtype to its basetype
6840 leave it alone. */
6841 if (TREE_TYPE (inner_type) == outer_type)
6842 return NULL_TREE;
6844 if (TREE_CODE (arg1) != INTEGER_CST
6845 && !(CONVERT_EXPR_P (arg1)
6846 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6847 return NULL_TREE;
6849 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6850 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6851 && code != NE_EXPR
6852 && code != EQ_EXPR)
6853 return NULL_TREE;
6855 if (TREE_CODE (arg1) == INTEGER_CST)
6856 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6857 TREE_INT_CST_HIGH (arg1), 0,
6858 TREE_OVERFLOW (arg1));
6859 else
6860 arg1 = fold_convert (inner_type, arg1);
6862 return fold_build2 (code, type, arg0_inner, arg1);
6865 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6866 step of the array. Reconstructs s and delta in the case of s * delta
6867 being an integer constant (and thus already folded).
6868 ADDR is the address. MULT is the multiplicative expression.
6869 If the function succeeds, the new address expression is returned. Otherwise
6870 NULL_TREE is returned. */
6872 static tree
6873 try_move_mult_to_index (tree addr, tree op1)
6875 tree s, delta, step;
6876 tree ref = TREE_OPERAND (addr, 0), pref;
6877 tree ret, pos;
6878 tree itype;
6879 bool mdim = false;
6881 /* Strip the nops that might be added when converting op1 to sizetype. */
6882 STRIP_NOPS (op1);
6884 /* Canonicalize op1 into a possibly non-constant delta
6885 and an INTEGER_CST s. */
6886 if (TREE_CODE (op1) == MULT_EXPR)
6888 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6890 STRIP_NOPS (arg0);
6891 STRIP_NOPS (arg1);
6893 if (TREE_CODE (arg0) == INTEGER_CST)
6895 s = arg0;
6896 delta = arg1;
6898 else if (TREE_CODE (arg1) == INTEGER_CST)
6900 s = arg1;
6901 delta = arg0;
6903 else
6904 return NULL_TREE;
6906 else if (TREE_CODE (op1) == INTEGER_CST)
6908 delta = op1;
6909 s = NULL_TREE;
6911 else
6913 /* Simulate we are delta * 1. */
6914 delta = op1;
6915 s = integer_one_node;
6918 for (;; ref = TREE_OPERAND (ref, 0))
6920 if (TREE_CODE (ref) == ARRAY_REF)
6922 /* Remember if this was a multi-dimensional array. */
6923 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6924 mdim = true;
6926 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6927 if (! itype)
6928 continue;
6930 step = array_ref_element_size (ref);
6931 if (TREE_CODE (step) != INTEGER_CST)
6932 continue;
6934 if (s)
6936 if (! tree_int_cst_equal (step, s))
6937 continue;
6939 else
6941 /* Try if delta is a multiple of step. */
6942 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6943 if (! tmp)
6944 continue;
6945 delta = tmp;
6948 /* Only fold here if we can verify we do not overflow one
6949 dimension of a multi-dimensional array. */
6950 if (mdim)
6952 tree tmp;
6954 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6955 || !INTEGRAL_TYPE_P (itype)
6956 || !TYPE_MAX_VALUE (itype)
6957 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
6958 continue;
6960 tmp = fold_binary (PLUS_EXPR, itype,
6961 fold_convert (itype,
6962 TREE_OPERAND (ref, 1)),
6963 fold_convert (itype, delta));
6964 if (!tmp
6965 || TREE_CODE (tmp) != INTEGER_CST
6966 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
6967 continue;
6970 break;
6972 else
6973 mdim = false;
6975 if (!handled_component_p (ref))
6976 return NULL_TREE;
6979 /* We found the suitable array reference. So copy everything up to it,
6980 and replace the index. */
6982 pref = TREE_OPERAND (addr, 0);
6983 ret = copy_node (pref);
6984 pos = ret;
6986 while (pref != ref)
6988 pref = TREE_OPERAND (pref, 0);
6989 TREE_OPERAND (pos, 0) = copy_node (pref);
6990 pos = TREE_OPERAND (pos, 0);
6993 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
6994 fold_convert (itype,
6995 TREE_OPERAND (pos, 1)),
6996 fold_convert (itype, delta));
6998 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7002 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7003 means A >= Y && A != MAX, but in this case we know that
7004 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7006 static tree
7007 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7009 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7011 if (TREE_CODE (bound) == LT_EXPR)
7012 a = TREE_OPERAND (bound, 0);
7013 else if (TREE_CODE (bound) == GT_EXPR)
7014 a = TREE_OPERAND (bound, 1);
7015 else
7016 return NULL_TREE;
7018 typea = TREE_TYPE (a);
7019 if (!INTEGRAL_TYPE_P (typea)
7020 && !POINTER_TYPE_P (typea))
7021 return NULL_TREE;
7023 if (TREE_CODE (ineq) == LT_EXPR)
7025 a1 = TREE_OPERAND (ineq, 1);
7026 y = TREE_OPERAND (ineq, 0);
7028 else if (TREE_CODE (ineq) == GT_EXPR)
7030 a1 = TREE_OPERAND (ineq, 0);
7031 y = TREE_OPERAND (ineq, 1);
7033 else
7034 return NULL_TREE;
7036 if (TREE_TYPE (a1) != typea)
7037 return NULL_TREE;
7039 if (POINTER_TYPE_P (typea))
7041 /* Convert the pointer types into integer before taking the difference. */
7042 tree ta = fold_convert (ssizetype, a);
7043 tree ta1 = fold_convert (ssizetype, a1);
7044 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7046 else
7047 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7049 if (!diff || !integer_onep (diff))
7050 return NULL_TREE;
7052 return fold_build2 (GE_EXPR, type, a, y);
7055 /* Fold a sum or difference of at least one multiplication.
7056 Returns the folded tree or NULL if no simplification could be made. */
7058 static tree
7059 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7061 tree arg00, arg01, arg10, arg11;
7062 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7064 /* (A * C) +- (B * C) -> (A+-B) * C.
7065 (A * C) +- A -> A * (C+-1).
7066 We are most concerned about the case where C is a constant,
7067 but other combinations show up during loop reduction. Since
7068 it is not difficult, try all four possibilities. */
7070 if (TREE_CODE (arg0) == MULT_EXPR)
7072 arg00 = TREE_OPERAND (arg0, 0);
7073 arg01 = TREE_OPERAND (arg0, 1);
7075 else if (TREE_CODE (arg0) == INTEGER_CST)
7077 arg00 = build_one_cst (type);
7078 arg01 = arg0;
7080 else
7082 /* We cannot generate constant 1 for fract. */
7083 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7084 return NULL_TREE;
7085 arg00 = arg0;
7086 arg01 = build_one_cst (type);
7088 if (TREE_CODE (arg1) == MULT_EXPR)
7090 arg10 = TREE_OPERAND (arg1, 0);
7091 arg11 = TREE_OPERAND (arg1, 1);
7093 else if (TREE_CODE (arg1) == INTEGER_CST)
7095 arg10 = build_one_cst (type);
7096 arg11 = arg1;
7098 else
7100 /* We cannot generate constant 1 for fract. */
7101 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7102 return NULL_TREE;
7103 arg10 = arg1;
7104 arg11 = build_one_cst (type);
7106 same = NULL_TREE;
7108 if (operand_equal_p (arg01, arg11, 0))
7109 same = arg01, alt0 = arg00, alt1 = arg10;
7110 else if (operand_equal_p (arg00, arg10, 0))
7111 same = arg00, alt0 = arg01, alt1 = arg11;
7112 else if (operand_equal_p (arg00, arg11, 0))
7113 same = arg00, alt0 = arg01, alt1 = arg10;
7114 else if (operand_equal_p (arg01, arg10, 0))
7115 same = arg01, alt0 = arg00, alt1 = arg11;
7117 /* No identical multiplicands; see if we can find a common
7118 power-of-two factor in non-power-of-two multiplies. This
7119 can help in multi-dimensional array access. */
7120 else if (host_integerp (arg01, 0)
7121 && host_integerp (arg11, 0))
7123 HOST_WIDE_INT int01, int11, tmp;
7124 bool swap = false;
7125 tree maybe_same;
7126 int01 = TREE_INT_CST_LOW (arg01);
7127 int11 = TREE_INT_CST_LOW (arg11);
7129 /* Move min of absolute values to int11. */
7130 if ((int01 >= 0 ? int01 : -int01)
7131 < (int11 >= 0 ? int11 : -int11))
7133 tmp = int01, int01 = int11, int11 = tmp;
7134 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7135 maybe_same = arg01;
7136 swap = true;
7138 else
7139 maybe_same = arg11;
7141 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7143 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7144 build_int_cst (TREE_TYPE (arg00),
7145 int01 / int11));
7146 alt1 = arg10;
7147 same = maybe_same;
7148 if (swap)
7149 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7153 if (same)
7154 return fold_build2 (MULT_EXPR, type,
7155 fold_build2 (code, type,
7156 fold_convert (type, alt0),
7157 fold_convert (type, alt1)),
7158 fold_convert (type, same));
7160 return NULL_TREE;
7163 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7164 specified by EXPR into the buffer PTR of length LEN bytes.
7165 Return the number of bytes placed in the buffer, or zero
7166 upon failure. */
7168 static int
7169 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7171 tree type = TREE_TYPE (expr);
7172 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7173 int byte, offset, word, words;
7174 unsigned char value;
7176 if (total_bytes > len)
7177 return 0;
7178 words = total_bytes / UNITS_PER_WORD;
7180 for (byte = 0; byte < total_bytes; byte++)
7182 int bitpos = byte * BITS_PER_UNIT;
7183 if (bitpos < HOST_BITS_PER_WIDE_INT)
7184 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7185 else
7186 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7187 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7189 if (total_bytes > UNITS_PER_WORD)
7191 word = byte / UNITS_PER_WORD;
7192 if (WORDS_BIG_ENDIAN)
7193 word = (words - 1) - word;
7194 offset = word * UNITS_PER_WORD;
7195 if (BYTES_BIG_ENDIAN)
7196 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7197 else
7198 offset += byte % UNITS_PER_WORD;
7200 else
7201 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7202 ptr[offset] = value;
7204 return total_bytes;
7208 /* Subroutine of native_encode_expr. Encode the REAL_CST
7209 specified by EXPR into the buffer PTR of length LEN bytes.
7210 Return the number of bytes placed in the buffer, or zero
7211 upon failure. */
7213 static int
7214 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7216 tree type = TREE_TYPE (expr);
7217 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7218 int byte, offset, word, words, bitpos;
7219 unsigned char value;
7221 /* There are always 32 bits in each long, no matter the size of
7222 the hosts long. We handle floating point representations with
7223 up to 192 bits. */
7224 long tmp[6];
7226 if (total_bytes > len)
7227 return 0;
7228 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7230 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7232 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7233 bitpos += BITS_PER_UNIT)
7235 byte = (bitpos / BITS_PER_UNIT) & 3;
7236 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7238 if (UNITS_PER_WORD < 4)
7240 word = byte / UNITS_PER_WORD;
7241 if (WORDS_BIG_ENDIAN)
7242 word = (words - 1) - word;
7243 offset = word * UNITS_PER_WORD;
7244 if (BYTES_BIG_ENDIAN)
7245 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7246 else
7247 offset += byte % UNITS_PER_WORD;
7249 else
7250 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7251 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7253 return total_bytes;
7256 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7257 specified by EXPR into the buffer PTR of length LEN bytes.
7258 Return the number of bytes placed in the buffer, or zero
7259 upon failure. */
7261 static int
7262 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7264 int rsize, isize;
7265 tree part;
7267 part = TREE_REALPART (expr);
7268 rsize = native_encode_expr (part, ptr, len);
7269 if (rsize == 0)
7270 return 0;
7271 part = TREE_IMAGPART (expr);
7272 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7273 if (isize != rsize)
7274 return 0;
7275 return rsize + isize;
7279 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7280 specified by EXPR into the buffer PTR of length LEN bytes.
7281 Return the number of bytes placed in the buffer, or zero
7282 upon failure. */
7284 static int
7285 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7287 int i, size, offset, count;
7288 tree itype, elem, elements;
7290 offset = 0;
7291 elements = TREE_VECTOR_CST_ELTS (expr);
7292 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7293 itype = TREE_TYPE (TREE_TYPE (expr));
7294 size = GET_MODE_SIZE (TYPE_MODE (itype));
7295 for (i = 0; i < count; i++)
7297 if (elements)
7299 elem = TREE_VALUE (elements);
7300 elements = TREE_CHAIN (elements);
7302 else
7303 elem = NULL_TREE;
7305 if (elem)
7307 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7308 return 0;
7310 else
7312 if (offset + size > len)
7313 return 0;
7314 memset (ptr+offset, 0, size);
7316 offset += size;
7318 return offset;
7322 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7323 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7324 buffer PTR of length LEN bytes. Return the number of bytes
7325 placed in the buffer, or zero upon failure. */
7328 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7330 switch (TREE_CODE (expr))
7332 case INTEGER_CST:
7333 return native_encode_int (expr, ptr, len);
7335 case REAL_CST:
7336 return native_encode_real (expr, ptr, len);
7338 case COMPLEX_CST:
7339 return native_encode_complex (expr, ptr, len);
7341 case VECTOR_CST:
7342 return native_encode_vector (expr, ptr, len);
7344 default:
7345 return 0;
7350 /* Subroutine of native_interpret_expr. Interpret the contents of
7351 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7352 If the buffer cannot be interpreted, return NULL_TREE. */
7354 static tree
7355 native_interpret_int (tree type, const unsigned char *ptr, int len)
7357 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7358 int byte, offset, word, words;
7359 unsigned char value;
7360 unsigned int HOST_WIDE_INT lo = 0;
7361 HOST_WIDE_INT hi = 0;
7363 if (total_bytes > len)
7364 return NULL_TREE;
7365 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7366 return NULL_TREE;
7367 words = total_bytes / UNITS_PER_WORD;
7369 for (byte = 0; byte < total_bytes; byte++)
7371 int bitpos = byte * BITS_PER_UNIT;
7372 if (total_bytes > UNITS_PER_WORD)
7374 word = byte / UNITS_PER_WORD;
7375 if (WORDS_BIG_ENDIAN)
7376 word = (words - 1) - word;
7377 offset = word * UNITS_PER_WORD;
7378 if (BYTES_BIG_ENDIAN)
7379 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7380 else
7381 offset += byte % UNITS_PER_WORD;
7383 else
7384 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7385 value = ptr[offset];
7387 if (bitpos < HOST_BITS_PER_WIDE_INT)
7388 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7389 else
7390 hi |= (unsigned HOST_WIDE_INT) value
7391 << (bitpos - HOST_BITS_PER_WIDE_INT);
7394 return build_int_cst_wide_type (type, lo, hi);
7398 /* Subroutine of native_interpret_expr. Interpret the contents of
7399 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7400 If the buffer cannot be interpreted, return NULL_TREE. */
7402 static tree
7403 native_interpret_real (tree type, const unsigned char *ptr, int len)
7405 enum machine_mode mode = TYPE_MODE (type);
7406 int total_bytes = GET_MODE_SIZE (mode);
7407 int byte, offset, word, words, bitpos;
7408 unsigned char value;
7409 /* There are always 32 bits in each long, no matter the size of
7410 the hosts long. We handle floating point representations with
7411 up to 192 bits. */
7412 REAL_VALUE_TYPE r;
7413 long tmp[6];
7415 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7416 if (total_bytes > len || total_bytes > 24)
7417 return NULL_TREE;
7418 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7420 memset (tmp, 0, sizeof (tmp));
7421 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7422 bitpos += BITS_PER_UNIT)
7424 byte = (bitpos / BITS_PER_UNIT) & 3;
7425 if (UNITS_PER_WORD < 4)
7427 word = byte / UNITS_PER_WORD;
7428 if (WORDS_BIG_ENDIAN)
7429 word = (words - 1) - word;
7430 offset = word * UNITS_PER_WORD;
7431 if (BYTES_BIG_ENDIAN)
7432 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7433 else
7434 offset += byte % UNITS_PER_WORD;
7436 else
7437 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7438 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7440 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7443 real_from_target (&r, tmp, mode);
7444 return build_real (type, r);
7448 /* Subroutine of native_interpret_expr. Interpret the contents of
7449 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7450 If the buffer cannot be interpreted, return NULL_TREE. */
7452 static tree
7453 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7455 tree etype, rpart, ipart;
7456 int size;
7458 etype = TREE_TYPE (type);
7459 size = GET_MODE_SIZE (TYPE_MODE (etype));
7460 if (size * 2 > len)
7461 return NULL_TREE;
7462 rpart = native_interpret_expr (etype, ptr, size);
7463 if (!rpart)
7464 return NULL_TREE;
7465 ipart = native_interpret_expr (etype, ptr+size, size);
7466 if (!ipart)
7467 return NULL_TREE;
7468 return build_complex (type, rpart, ipart);
7472 /* Subroutine of native_interpret_expr. Interpret the contents of
7473 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7474 If the buffer cannot be interpreted, return NULL_TREE. */
7476 static tree
7477 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7479 tree etype, elem, elements;
7480 int i, size, count;
7482 etype = TREE_TYPE (type);
7483 size = GET_MODE_SIZE (TYPE_MODE (etype));
7484 count = TYPE_VECTOR_SUBPARTS (type);
7485 if (size * count > len)
7486 return NULL_TREE;
7488 elements = NULL_TREE;
7489 for (i = count - 1; i >= 0; i--)
7491 elem = native_interpret_expr (etype, ptr+(i*size), size);
7492 if (!elem)
7493 return NULL_TREE;
7494 elements = tree_cons (NULL_TREE, elem, elements);
7496 return build_vector (type, elements);
7500 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7501 the buffer PTR of length LEN as a constant of type TYPE. For
7502 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7503 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7504 return NULL_TREE. */
7506 tree
7507 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7509 switch (TREE_CODE (type))
7511 case INTEGER_TYPE:
7512 case ENUMERAL_TYPE:
7513 case BOOLEAN_TYPE:
7514 return native_interpret_int (type, ptr, len);
7516 case REAL_TYPE:
7517 return native_interpret_real (type, ptr, len);
7519 case COMPLEX_TYPE:
7520 return native_interpret_complex (type, ptr, len);
7522 case VECTOR_TYPE:
7523 return native_interpret_vector (type, ptr, len);
7525 default:
7526 return NULL_TREE;
7531 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7532 TYPE at compile-time. If we're unable to perform the conversion
7533 return NULL_TREE. */
7535 static tree
7536 fold_view_convert_expr (tree type, tree expr)
7538 /* We support up to 512-bit values (for V8DFmode). */
7539 unsigned char buffer[64];
7540 int len;
7542 /* Check that the host and target are sane. */
7543 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7544 return NULL_TREE;
7546 len = native_encode_expr (expr, buffer, sizeof (buffer));
7547 if (len == 0)
7548 return NULL_TREE;
7550 return native_interpret_expr (type, buffer, len);
7553 /* Build an expression for the address of T. Folds away INDIRECT_REF
7554 to avoid confusing the gimplify process. When IN_FOLD is true
7555 avoid modifications of T. */
7557 static tree
7558 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7560 /* The size of the object is not relevant when talking about its address. */
7561 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7562 t = TREE_OPERAND (t, 0);
7564 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7565 if (TREE_CODE (t) == INDIRECT_REF
7566 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7568 t = TREE_OPERAND (t, 0);
7570 if (TREE_TYPE (t) != ptrtype)
7571 t = build1 (NOP_EXPR, ptrtype, t);
7573 else if (!in_fold)
7575 tree base = t;
7577 while (handled_component_p (base))
7578 base = TREE_OPERAND (base, 0);
7580 if (DECL_P (base))
7581 TREE_ADDRESSABLE (base) = 1;
7583 t = build1 (ADDR_EXPR, ptrtype, t);
7585 else
7586 t = build1 (ADDR_EXPR, ptrtype, t);
7588 return t;
7591 /* Build an expression for the address of T with type PTRTYPE. This
7592 function modifies the input parameter 'T' by sometimes setting the
7593 TREE_ADDRESSABLE flag. */
7595 tree
7596 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7598 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7601 /* Build an expression for the address of T. This function modifies
7602 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7603 flag. When called from fold functions, use fold_addr_expr instead. */
7605 tree
7606 build_fold_addr_expr (tree t)
7608 return build_fold_addr_expr_with_type_1 (t,
7609 build_pointer_type (TREE_TYPE (t)),
7610 false);
7613 /* Same as build_fold_addr_expr, builds an expression for the address
7614 of T, but avoids touching the input node 't'. Fold functions
7615 should use this version. */
7617 static tree
7618 fold_addr_expr (tree t)
7620 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7622 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7625 /* Fold a unary expression of code CODE and type TYPE with operand
7626 OP0. Return the folded expression if folding is successful.
7627 Otherwise, return NULL_TREE. */
7629 tree
7630 fold_unary (enum tree_code code, tree type, tree op0)
7632 tree tem;
7633 tree arg0;
7634 enum tree_code_class kind = TREE_CODE_CLASS (code);
7636 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7637 && TREE_CODE_LENGTH (code) == 1);
7639 arg0 = op0;
7640 if (arg0)
7642 if (CONVERT_EXPR_CODE_P (code)
7643 || code == FLOAT_EXPR || code == ABS_EXPR)
7645 /* Don't use STRIP_NOPS, because signedness of argument type
7646 matters. */
7647 STRIP_SIGN_NOPS (arg0);
7649 else
7651 /* Strip any conversions that don't change the mode. This
7652 is safe for every expression, except for a comparison
7653 expression because its signedness is derived from its
7654 operands.
7656 Note that this is done as an internal manipulation within
7657 the constant folder, in order to find the simplest
7658 representation of the arguments so that their form can be
7659 studied. In any cases, the appropriate type conversions
7660 should be put back in the tree that will get out of the
7661 constant folder. */
7662 STRIP_NOPS (arg0);
7666 if (TREE_CODE_CLASS (code) == tcc_unary)
7668 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7669 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7670 fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7671 else if (TREE_CODE (arg0) == COND_EXPR)
7673 tree arg01 = TREE_OPERAND (arg0, 1);
7674 tree arg02 = TREE_OPERAND (arg0, 2);
7675 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7676 arg01 = fold_build1 (code, type, arg01);
7677 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7678 arg02 = fold_build1 (code, type, arg02);
7679 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7680 arg01, arg02);
7682 /* If this was a conversion, and all we did was to move into
7683 inside the COND_EXPR, bring it back out. But leave it if
7684 it is a conversion from integer to integer and the
7685 result precision is no wider than a word since such a
7686 conversion is cheap and may be optimized away by combine,
7687 while it couldn't if it were outside the COND_EXPR. Then return
7688 so we don't get into an infinite recursion loop taking the
7689 conversion out and then back in. */
7691 if ((CONVERT_EXPR_CODE_P (code)
7692 || code == NON_LVALUE_EXPR)
7693 && TREE_CODE (tem) == COND_EXPR
7694 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7695 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7696 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7697 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7698 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7699 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7700 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7701 && (INTEGRAL_TYPE_P
7702 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7703 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7704 || flag_syntax_only))
7705 tem = build1 (code, type,
7706 build3 (COND_EXPR,
7707 TREE_TYPE (TREE_OPERAND
7708 (TREE_OPERAND (tem, 1), 0)),
7709 TREE_OPERAND (tem, 0),
7710 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7711 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7712 return tem;
7714 else if (COMPARISON_CLASS_P (arg0))
7716 if (TREE_CODE (type) == BOOLEAN_TYPE)
7718 arg0 = copy_node (arg0);
7719 TREE_TYPE (arg0) = type;
7720 return arg0;
7722 else if (TREE_CODE (type) != INTEGER_TYPE)
7723 return fold_build3 (COND_EXPR, type, arg0,
7724 fold_build1 (code, type,
7725 integer_one_node),
7726 fold_build1 (code, type,
7727 integer_zero_node));
7731 switch (code)
7733 case PAREN_EXPR:
7734 /* Re-association barriers around constants and other re-association
7735 barriers can be removed. */
7736 if (CONSTANT_CLASS_P (op0)
7737 || TREE_CODE (op0) == PAREN_EXPR)
7738 return fold_convert (type, op0);
7739 return NULL_TREE;
7741 CASE_CONVERT:
7742 case FLOAT_EXPR:
7743 case FIX_TRUNC_EXPR:
7744 if (TREE_TYPE (op0) == type)
7745 return op0;
7747 /* If we have (type) (a CMP b) and type is an integral type, return
7748 new expression involving the new type. */
7749 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7750 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7751 TREE_OPERAND (op0, 1));
7753 /* Handle cases of two conversions in a row. */
7754 if (CONVERT_EXPR_P (op0))
7756 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7757 tree inter_type = TREE_TYPE (op0);
7758 int inside_int = INTEGRAL_TYPE_P (inside_type);
7759 int inside_ptr = POINTER_TYPE_P (inside_type);
7760 int inside_float = FLOAT_TYPE_P (inside_type);
7761 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7762 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7763 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7764 int inter_int = INTEGRAL_TYPE_P (inter_type);
7765 int inter_ptr = POINTER_TYPE_P (inter_type);
7766 int inter_float = FLOAT_TYPE_P (inter_type);
7767 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7768 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7769 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7770 int final_int = INTEGRAL_TYPE_P (type);
7771 int final_ptr = POINTER_TYPE_P (type);
7772 int final_float = FLOAT_TYPE_P (type);
7773 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7774 unsigned int final_prec = TYPE_PRECISION (type);
7775 int final_unsignedp = TYPE_UNSIGNED (type);
7777 /* In addition to the cases of two conversions in a row
7778 handled below, if we are converting something to its own
7779 type via an object of identical or wider precision, neither
7780 conversion is needed. */
7781 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7782 && (((inter_int || inter_ptr) && final_int)
7783 || (inter_float && final_float))
7784 && inter_prec >= final_prec)
7785 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7787 /* Likewise, if the intermediate and final types are either both
7788 float or both integer, we don't need the middle conversion if
7789 it is wider than the final type and doesn't change the signedness
7790 (for integers). Avoid this if the final type is a pointer
7791 since then we sometimes need the inner conversion. Likewise if
7792 the outer has a precision not equal to the size of its mode. */
7793 if (((inter_int && inside_int)
7794 || (inter_float && inside_float)
7795 || (inter_vec && inside_vec))
7796 && inter_prec >= inside_prec
7797 && (inter_float || inter_vec
7798 || inter_unsignedp == inside_unsignedp)
7799 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7800 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7801 && ! final_ptr
7802 && (! final_vec || inter_prec == inside_prec))
7803 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7805 /* If we have a sign-extension of a zero-extended value, we can
7806 replace that by a single zero-extension. */
7807 if (inside_int && inter_int && final_int
7808 && inside_prec < inter_prec && inter_prec < final_prec
7809 && inside_unsignedp && !inter_unsignedp)
7810 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7812 /* Two conversions in a row are not needed unless:
7813 - some conversion is floating-point (overstrict for now), or
7814 - some conversion is a vector (overstrict for now), or
7815 - the intermediate type is narrower than both initial and
7816 final, or
7817 - the intermediate type and innermost type differ in signedness,
7818 and the outermost type is wider than the intermediate, or
7819 - the initial type is a pointer type and the precisions of the
7820 intermediate and final types differ, or
7821 - the final type is a pointer type and the precisions of the
7822 initial and intermediate types differ. */
7823 if (! inside_float && ! inter_float && ! final_float
7824 && ! inside_vec && ! inter_vec && ! final_vec
7825 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7826 && ! (inside_int && inter_int
7827 && inter_unsignedp != inside_unsignedp
7828 && inter_prec < final_prec)
7829 && ((inter_unsignedp && inter_prec > inside_prec)
7830 == (final_unsignedp && final_prec > inter_prec))
7831 && ! (inside_ptr && inter_prec != final_prec)
7832 && ! (final_ptr && inside_prec != inter_prec)
7833 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7834 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7835 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7838 /* Handle (T *)&A.B.C for A being of type T and B and C
7839 living at offset zero. This occurs frequently in
7840 C++ upcasting and then accessing the base. */
7841 if (TREE_CODE (op0) == ADDR_EXPR
7842 && POINTER_TYPE_P (type)
7843 && handled_component_p (TREE_OPERAND (op0, 0)))
7845 HOST_WIDE_INT bitsize, bitpos;
7846 tree offset;
7847 enum machine_mode mode;
7848 int unsignedp, volatilep;
7849 tree base = TREE_OPERAND (op0, 0);
7850 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7851 &mode, &unsignedp, &volatilep, false);
7852 /* If the reference was to a (constant) zero offset, we can use
7853 the address of the base if it has the same base type
7854 as the result type. */
7855 if (! offset && bitpos == 0
7856 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7857 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7858 return fold_convert (type, fold_addr_expr (base));
7861 if (TREE_CODE (op0) == MODIFY_EXPR
7862 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7863 /* Detect assigning a bitfield. */
7864 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7865 && DECL_BIT_FIELD
7866 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7868 /* Don't leave an assignment inside a conversion
7869 unless assigning a bitfield. */
7870 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
7871 /* First do the assignment, then return converted constant. */
7872 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7873 TREE_NO_WARNING (tem) = 1;
7874 TREE_USED (tem) = 1;
7875 return tem;
7878 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7879 constants (if x has signed type, the sign bit cannot be set
7880 in c). This folds extension into the BIT_AND_EXPR.
7881 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7882 very likely don't have maximal range for their precision and this
7883 transformation effectively doesn't preserve non-maximal ranges. */
7884 if (TREE_CODE (type) == INTEGER_TYPE
7885 && TREE_CODE (op0) == BIT_AND_EXPR
7886 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
7887 /* Not if the conversion is to the sub-type. */
7888 && TREE_TYPE (type) != TREE_TYPE (op0))
7890 tree and = op0;
7891 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7892 int change = 0;
7894 if (TYPE_UNSIGNED (TREE_TYPE (and))
7895 || (TYPE_PRECISION (type)
7896 <= TYPE_PRECISION (TREE_TYPE (and))))
7897 change = 1;
7898 else if (TYPE_PRECISION (TREE_TYPE (and1))
7899 <= HOST_BITS_PER_WIDE_INT
7900 && host_integerp (and1, 1))
7902 unsigned HOST_WIDE_INT cst;
7904 cst = tree_low_cst (and1, 1);
7905 cst &= (HOST_WIDE_INT) -1
7906 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7907 change = (cst == 0);
7908 #ifdef LOAD_EXTEND_OP
7909 if (change
7910 && !flag_syntax_only
7911 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7912 == ZERO_EXTEND))
7914 tree uns = unsigned_type_for (TREE_TYPE (and0));
7915 and0 = fold_convert (uns, and0);
7916 and1 = fold_convert (uns, and1);
7918 #endif
7920 if (change)
7922 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7923 TREE_INT_CST_HIGH (and1), 0,
7924 TREE_OVERFLOW (and1));
7925 return fold_build2 (BIT_AND_EXPR, type,
7926 fold_convert (type, and0), tem);
7930 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7931 when one of the new casts will fold away. Conservatively we assume
7932 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7933 if (POINTER_TYPE_P (type)
7934 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7935 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7936 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7937 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7939 tree arg00 = TREE_OPERAND (arg0, 0);
7940 tree arg01 = TREE_OPERAND (arg0, 1);
7942 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
7943 fold_convert (sizetype, arg01));
7946 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7947 of the same precision, and X is an integer type not narrower than
7948 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7949 if (INTEGRAL_TYPE_P (type)
7950 && TREE_CODE (op0) == BIT_NOT_EXPR
7951 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7952 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7953 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7955 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7956 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7957 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7958 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7961 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7962 type of X and Y (integer types only). */
7963 if (INTEGRAL_TYPE_P (type)
7964 && TREE_CODE (op0) == MULT_EXPR
7965 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7966 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7968 /* Be careful not to introduce new overflows. */
7969 tree mult_type;
7970 if (TYPE_OVERFLOW_WRAPS (type))
7971 mult_type = type;
7972 else
7973 mult_type = unsigned_type_for (type);
7975 tem = fold_build2 (MULT_EXPR, mult_type,
7976 fold_convert (mult_type, TREE_OPERAND (op0, 0)),
7977 fold_convert (mult_type, TREE_OPERAND (op0, 1)));
7978 return fold_convert (type, tem);
7981 tem = fold_convert_const (code, type, op0);
7982 return tem ? tem : NULL_TREE;
7984 case FIXED_CONVERT_EXPR:
7985 tem = fold_convert_const (code, type, arg0);
7986 return tem ? tem : NULL_TREE;
7988 case VIEW_CONVERT_EXPR:
7989 if (TREE_TYPE (op0) == type)
7990 return op0;
7991 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7992 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7994 /* For integral conversions with the same precision or pointer
7995 conversions use a NOP_EXPR instead. */
7996 if ((INTEGRAL_TYPE_P (type)
7997 || POINTER_TYPE_P (type))
7998 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7999 || POINTER_TYPE_P (TREE_TYPE (op0)))
8000 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8001 /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8002 a sub-type to its base type as generated by the Ada FE. */
8003 && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8004 && TREE_TYPE (TREE_TYPE (op0))))
8005 return fold_convert (type, op0);
8007 /* Strip inner integral conversions that do not change the precision. */
8008 if (CONVERT_EXPR_P (op0)
8009 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8010 || POINTER_TYPE_P (TREE_TYPE (op0)))
8011 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8012 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8013 && (TYPE_PRECISION (TREE_TYPE (op0))
8014 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8015 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8017 return fold_view_convert_expr (type, op0);
8019 case NEGATE_EXPR:
8020 tem = fold_negate_expr (arg0);
8021 if (tem)
8022 return fold_convert (type, tem);
8023 return NULL_TREE;
8025 case ABS_EXPR:
8026 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8027 return fold_abs_const (arg0, type);
8028 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8029 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8030 /* Convert fabs((double)float) into (double)fabsf(float). */
8031 else if (TREE_CODE (arg0) == NOP_EXPR
8032 && TREE_CODE (type) == REAL_TYPE)
8034 tree targ0 = strip_float_extensions (arg0);
8035 if (targ0 != arg0)
8036 return fold_convert (type, fold_build1 (ABS_EXPR,
8037 TREE_TYPE (targ0),
8038 targ0));
8040 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8041 else if (TREE_CODE (arg0) == ABS_EXPR)
8042 return arg0;
8043 else if (tree_expr_nonnegative_p (arg0))
8044 return arg0;
8046 /* Strip sign ops from argument. */
8047 if (TREE_CODE (type) == REAL_TYPE)
8049 tem = fold_strip_sign_ops (arg0);
8050 if (tem)
8051 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8053 return NULL_TREE;
8055 case CONJ_EXPR:
8056 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8057 return fold_convert (type, arg0);
8058 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8060 tree itype = TREE_TYPE (type);
8061 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8062 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8063 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8065 if (TREE_CODE (arg0) == COMPLEX_CST)
8067 tree itype = TREE_TYPE (type);
8068 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8069 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8070 return build_complex (type, rpart, negate_expr (ipart));
8072 if (TREE_CODE (arg0) == CONJ_EXPR)
8073 return fold_convert (type, TREE_OPERAND (arg0, 0));
8074 return NULL_TREE;
8076 case BIT_NOT_EXPR:
8077 if (TREE_CODE (arg0) == INTEGER_CST)
8078 return fold_not_const (arg0, type);
8079 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8080 return fold_convert (type, TREE_OPERAND (arg0, 0));
8081 /* Convert ~ (-A) to A - 1. */
8082 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8083 return fold_build2 (MINUS_EXPR, type,
8084 fold_convert (type, TREE_OPERAND (arg0, 0)),
8085 build_int_cst (type, 1));
8086 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8087 else if (INTEGRAL_TYPE_P (type)
8088 && ((TREE_CODE (arg0) == MINUS_EXPR
8089 && integer_onep (TREE_OPERAND (arg0, 1)))
8090 || (TREE_CODE (arg0) == PLUS_EXPR
8091 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8092 return fold_build1 (NEGATE_EXPR, type,
8093 fold_convert (type, TREE_OPERAND (arg0, 0)));
8094 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8095 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8096 && (tem = fold_unary (BIT_NOT_EXPR, type,
8097 fold_convert (type,
8098 TREE_OPERAND (arg0, 0)))))
8099 return fold_build2 (BIT_XOR_EXPR, type, tem,
8100 fold_convert (type, TREE_OPERAND (arg0, 1)));
8101 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8102 && (tem = fold_unary (BIT_NOT_EXPR, type,
8103 fold_convert (type,
8104 TREE_OPERAND (arg0, 1)))))
8105 return fold_build2 (BIT_XOR_EXPR, type,
8106 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8107 /* Perform BIT_NOT_EXPR on each element individually. */
8108 else if (TREE_CODE (arg0) == VECTOR_CST)
8110 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8111 int count = TYPE_VECTOR_SUBPARTS (type), i;
8113 for (i = 0; i < count; i++)
8115 if (elements)
8117 elem = TREE_VALUE (elements);
8118 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8119 if (elem == NULL_TREE)
8120 break;
8121 elements = TREE_CHAIN (elements);
8123 else
8124 elem = build_int_cst (TREE_TYPE (type), -1);
8125 list = tree_cons (NULL_TREE, elem, list);
8127 if (i == count)
8128 return build_vector (type, nreverse (list));
8131 return NULL_TREE;
8133 case TRUTH_NOT_EXPR:
8134 /* The argument to invert_truthvalue must have Boolean type. */
8135 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8136 arg0 = fold_convert (boolean_type_node, arg0);
8138 /* Note that the operand of this must be an int
8139 and its values must be 0 or 1.
8140 ("true" is a fixed value perhaps depending on the language,
8141 but we don't handle values other than 1 correctly yet.) */
8142 tem = fold_truth_not_expr (arg0);
8143 if (!tem)
8144 return NULL_TREE;
8145 return fold_convert (type, tem);
8147 case REALPART_EXPR:
8148 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8149 return fold_convert (type, arg0);
8150 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8151 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8152 TREE_OPERAND (arg0, 1));
8153 if (TREE_CODE (arg0) == COMPLEX_CST)
8154 return fold_convert (type, TREE_REALPART (arg0));
8155 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8157 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8158 tem = fold_build2 (TREE_CODE (arg0), itype,
8159 fold_build1 (REALPART_EXPR, itype,
8160 TREE_OPERAND (arg0, 0)),
8161 fold_build1 (REALPART_EXPR, itype,
8162 TREE_OPERAND (arg0, 1)));
8163 return fold_convert (type, tem);
8165 if (TREE_CODE (arg0) == CONJ_EXPR)
8167 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8168 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8169 return fold_convert (type, tem);
8171 if (TREE_CODE (arg0) == CALL_EXPR)
8173 tree fn = get_callee_fndecl (arg0);
8174 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8175 switch (DECL_FUNCTION_CODE (fn))
8177 CASE_FLT_FN (BUILT_IN_CEXPI):
8178 fn = mathfn_built_in (type, BUILT_IN_COS);
8179 if (fn)
8180 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8181 break;
8183 default:
8184 break;
8187 return NULL_TREE;
8189 case IMAGPART_EXPR:
8190 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8191 return fold_convert (type, integer_zero_node);
8192 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8193 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8194 TREE_OPERAND (arg0, 0));
8195 if (TREE_CODE (arg0) == COMPLEX_CST)
8196 return fold_convert (type, TREE_IMAGPART (arg0));
8197 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8199 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8200 tem = fold_build2 (TREE_CODE (arg0), itype,
8201 fold_build1 (IMAGPART_EXPR, itype,
8202 TREE_OPERAND (arg0, 0)),
8203 fold_build1 (IMAGPART_EXPR, itype,
8204 TREE_OPERAND (arg0, 1)));
8205 return fold_convert (type, tem);
8207 if (TREE_CODE (arg0) == CONJ_EXPR)
8209 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8210 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8211 return fold_convert (type, negate_expr (tem));
8213 if (TREE_CODE (arg0) == CALL_EXPR)
8215 tree fn = get_callee_fndecl (arg0);
8216 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8217 switch (DECL_FUNCTION_CODE (fn))
8219 CASE_FLT_FN (BUILT_IN_CEXPI):
8220 fn = mathfn_built_in (type, BUILT_IN_SIN);
8221 if (fn)
8222 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8223 break;
8225 default:
8226 break;
8229 return NULL_TREE;
8231 default:
8232 return NULL_TREE;
8233 } /* switch (code) */
8236 /* Fold a binary expression of code CODE and type TYPE with operands
8237 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8238 Return the folded expression if folding is successful. Otherwise,
8239 return NULL_TREE. */
8241 static tree
8242 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8244 enum tree_code compl_code;
8246 if (code == MIN_EXPR)
8247 compl_code = MAX_EXPR;
8248 else if (code == MAX_EXPR)
8249 compl_code = MIN_EXPR;
8250 else
8251 gcc_unreachable ();
8253 /* MIN (MAX (a, b), b) == b. */
8254 if (TREE_CODE (op0) == compl_code
8255 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8256 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8258 /* MIN (MAX (b, a), b) == b. */
8259 if (TREE_CODE (op0) == compl_code
8260 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8261 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8262 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8264 /* MIN (a, MAX (a, b)) == a. */
8265 if (TREE_CODE (op1) == compl_code
8266 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8267 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8268 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8270 /* MIN (a, MAX (b, a)) == a. */
8271 if (TREE_CODE (op1) == compl_code
8272 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8273 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8274 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8276 return NULL_TREE;
8279 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8280 by changing CODE to reduce the magnitude of constants involved in
8281 ARG0 of the comparison.
8282 Returns a canonicalized comparison tree if a simplification was
8283 possible, otherwise returns NULL_TREE.
8284 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8285 valid if signed overflow is undefined. */
8287 static tree
8288 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8289 tree arg0, tree arg1,
8290 bool *strict_overflow_p)
8292 enum tree_code code0 = TREE_CODE (arg0);
8293 tree t, cst0 = NULL_TREE;
8294 int sgn0;
8295 bool swap = false;
8297 /* Match A +- CST code arg1 and CST code arg1. We can change the
8298 first form only if overflow is undefined. */
8299 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8300 /* In principle pointers also have undefined overflow behavior,
8301 but that causes problems elsewhere. */
8302 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8303 && (code0 == MINUS_EXPR
8304 || code0 == PLUS_EXPR)
8305 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8306 || code0 == INTEGER_CST))
8307 return NULL_TREE;
8309 /* Identify the constant in arg0 and its sign. */
8310 if (code0 == INTEGER_CST)
8311 cst0 = arg0;
8312 else
8313 cst0 = TREE_OPERAND (arg0, 1);
8314 sgn0 = tree_int_cst_sgn (cst0);
8316 /* Overflowed constants and zero will cause problems. */
8317 if (integer_zerop (cst0)
8318 || TREE_OVERFLOW (cst0))
8319 return NULL_TREE;
8321 /* See if we can reduce the magnitude of the constant in
8322 arg0 by changing the comparison code. */
8323 if (code0 == INTEGER_CST)
8325 /* CST <= arg1 -> CST-1 < arg1. */
8326 if (code == LE_EXPR && sgn0 == 1)
8327 code = LT_EXPR;
8328 /* -CST < arg1 -> -CST-1 <= arg1. */
8329 else if (code == LT_EXPR && sgn0 == -1)
8330 code = LE_EXPR;
8331 /* CST > arg1 -> CST-1 >= arg1. */
8332 else if (code == GT_EXPR && sgn0 == 1)
8333 code = GE_EXPR;
8334 /* -CST >= arg1 -> -CST-1 > arg1. */
8335 else if (code == GE_EXPR && sgn0 == -1)
8336 code = GT_EXPR;
8337 else
8338 return NULL_TREE;
8339 /* arg1 code' CST' might be more canonical. */
8340 swap = true;
8342 else
8344 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8345 if (code == LT_EXPR
8346 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8347 code = LE_EXPR;
8348 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8349 else if (code == GT_EXPR
8350 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8351 code = GE_EXPR;
8352 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8353 else if (code == LE_EXPR
8354 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8355 code = LT_EXPR;
8356 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8357 else if (code == GE_EXPR
8358 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8359 code = GT_EXPR;
8360 else
8361 return NULL_TREE;
8362 *strict_overflow_p = true;
8365 /* Now build the constant reduced in magnitude. But not if that
8366 would produce one outside of its types range. */
8367 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8368 && ((sgn0 == 1
8369 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8370 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8371 || (sgn0 == -1
8372 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8373 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8374 /* We cannot swap the comparison here as that would cause us to
8375 endlessly recurse. */
8376 return NULL_TREE;
8378 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8379 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8380 if (code0 != INTEGER_CST)
8381 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8383 /* If swapping might yield to a more canonical form, do so. */
8384 if (swap)
8385 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8386 else
8387 return fold_build2 (code, type, t, arg1);
8390 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8391 overflow further. Try to decrease the magnitude of constants involved
8392 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8393 and put sole constants at the second argument position.
8394 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8396 static tree
8397 maybe_canonicalize_comparison (enum tree_code code, tree type,
8398 tree arg0, tree arg1)
8400 tree t;
8401 bool strict_overflow_p;
8402 const char * const warnmsg = G_("assuming signed overflow does not occur "
8403 "when reducing constant in comparison");
8405 /* Try canonicalization by simplifying arg0. */
8406 strict_overflow_p = false;
8407 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8408 &strict_overflow_p);
8409 if (t)
8411 if (strict_overflow_p)
8412 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8413 return t;
8416 /* Try canonicalization by simplifying arg1 using the swapped
8417 comparison. */
8418 code = swap_tree_comparison (code);
8419 strict_overflow_p = false;
8420 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8421 &strict_overflow_p);
8422 if (t && strict_overflow_p)
8423 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8424 return t;
8427 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8428 space. This is used to avoid issuing overflow warnings for
8429 expressions like &p->x which can not wrap. */
8431 static bool
8432 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8434 unsigned HOST_WIDE_INT offset_low, total_low;
8435 HOST_WIDE_INT size, offset_high, total_high;
8437 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8438 return true;
8440 if (bitpos < 0)
8441 return true;
8443 if (offset == NULL_TREE)
8445 offset_low = 0;
8446 offset_high = 0;
8448 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8449 return true;
8450 else
8452 offset_low = TREE_INT_CST_LOW (offset);
8453 offset_high = TREE_INT_CST_HIGH (offset);
8456 if (add_double_with_sign (offset_low, offset_high,
8457 bitpos / BITS_PER_UNIT, 0,
8458 &total_low, &total_high,
8459 true))
8460 return true;
8462 if (total_high != 0)
8463 return true;
8465 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8466 if (size <= 0)
8467 return true;
8469 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8470 array. */
8471 if (TREE_CODE (base) == ADDR_EXPR)
8473 HOST_WIDE_INT base_size;
8475 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8476 if (base_size > 0 && size < base_size)
8477 size = base_size;
8480 return total_low > (unsigned HOST_WIDE_INT) size;
8483 /* Subroutine of fold_binary. This routine performs all of the
8484 transformations that are common to the equality/inequality
8485 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8486 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8487 fold_binary should call fold_binary. Fold a comparison with
8488 tree code CODE and type TYPE with operands OP0 and OP1. Return
8489 the folded comparison or NULL_TREE. */
8491 static tree
8492 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8494 tree arg0, arg1, tem;
8496 arg0 = op0;
8497 arg1 = op1;
8499 STRIP_SIGN_NOPS (arg0);
8500 STRIP_SIGN_NOPS (arg1);
8502 tem = fold_relational_const (code, type, arg0, arg1);
8503 if (tem != NULL_TREE)
8504 return tem;
8506 /* If one arg is a real or integer constant, put it last. */
8507 if (tree_swap_operands_p (arg0, arg1, true))
8508 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8510 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8511 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8512 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8513 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8514 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8515 && (TREE_CODE (arg1) == INTEGER_CST
8516 && !TREE_OVERFLOW (arg1)))
8518 tree const1 = TREE_OPERAND (arg0, 1);
8519 tree const2 = arg1;
8520 tree variable = TREE_OPERAND (arg0, 0);
8521 tree lhs;
8522 int lhs_add;
8523 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8525 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8526 TREE_TYPE (arg1), const2, const1);
8528 /* If the constant operation overflowed this can be
8529 simplified as a comparison against INT_MAX/INT_MIN. */
8530 if (TREE_CODE (lhs) == INTEGER_CST
8531 && TREE_OVERFLOW (lhs))
8533 int const1_sgn = tree_int_cst_sgn (const1);
8534 enum tree_code code2 = code;
8536 /* Get the sign of the constant on the lhs if the
8537 operation were VARIABLE + CONST1. */
8538 if (TREE_CODE (arg0) == MINUS_EXPR)
8539 const1_sgn = -const1_sgn;
8541 /* The sign of the constant determines if we overflowed
8542 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8543 Canonicalize to the INT_MIN overflow by swapping the comparison
8544 if necessary. */
8545 if (const1_sgn == -1)
8546 code2 = swap_tree_comparison (code);
8548 /* We now can look at the canonicalized case
8549 VARIABLE + 1 CODE2 INT_MIN
8550 and decide on the result. */
8551 if (code2 == LT_EXPR
8552 || code2 == LE_EXPR
8553 || code2 == EQ_EXPR)
8554 return omit_one_operand (type, boolean_false_node, variable);
8555 else if (code2 == NE_EXPR
8556 || code2 == GE_EXPR
8557 || code2 == GT_EXPR)
8558 return omit_one_operand (type, boolean_true_node, variable);
8561 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8562 && (TREE_CODE (lhs) != INTEGER_CST
8563 || !TREE_OVERFLOW (lhs)))
8565 fold_overflow_warning (("assuming signed overflow does not occur "
8566 "when changing X +- C1 cmp C2 to "
8567 "X cmp C1 +- C2"),
8568 WARN_STRICT_OVERFLOW_COMPARISON);
8569 return fold_build2 (code, type, variable, lhs);
8573 /* For comparisons of pointers we can decompose it to a compile time
8574 comparison of the base objects and the offsets into the object.
8575 This requires at least one operand being an ADDR_EXPR or a
8576 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8577 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8578 && (TREE_CODE (arg0) == ADDR_EXPR
8579 || TREE_CODE (arg1) == ADDR_EXPR
8580 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8581 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8583 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8584 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8585 enum machine_mode mode;
8586 int volatilep, unsignedp;
8587 bool indirect_base0 = false, indirect_base1 = false;
8589 /* Get base and offset for the access. Strip ADDR_EXPR for
8590 get_inner_reference, but put it back by stripping INDIRECT_REF
8591 off the base object if possible. indirect_baseN will be true
8592 if baseN is not an address but refers to the object itself. */
8593 base0 = arg0;
8594 if (TREE_CODE (arg0) == ADDR_EXPR)
8596 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8597 &bitsize, &bitpos0, &offset0, &mode,
8598 &unsignedp, &volatilep, false);
8599 if (TREE_CODE (base0) == INDIRECT_REF)
8600 base0 = TREE_OPERAND (base0, 0);
8601 else
8602 indirect_base0 = true;
8604 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8606 base0 = TREE_OPERAND (arg0, 0);
8607 offset0 = TREE_OPERAND (arg0, 1);
8610 base1 = arg1;
8611 if (TREE_CODE (arg1) == ADDR_EXPR)
8613 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8614 &bitsize, &bitpos1, &offset1, &mode,
8615 &unsignedp, &volatilep, false);
8616 if (TREE_CODE (base1) == INDIRECT_REF)
8617 base1 = TREE_OPERAND (base1, 0);
8618 else
8619 indirect_base1 = true;
8621 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8623 base1 = TREE_OPERAND (arg1, 0);
8624 offset1 = TREE_OPERAND (arg1, 1);
8627 /* If we have equivalent bases we might be able to simplify. */
8628 if (indirect_base0 == indirect_base1
8629 && operand_equal_p (base0, base1, 0))
8631 /* We can fold this expression to a constant if the non-constant
8632 offset parts are equal. */
8633 if ((offset0 == offset1
8634 || (offset0 && offset1
8635 && operand_equal_p (offset0, offset1, 0)))
8636 && (code == EQ_EXPR
8637 || code == NE_EXPR
8638 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8641 if (code != EQ_EXPR
8642 && code != NE_EXPR
8643 && bitpos0 != bitpos1
8644 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8645 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8646 fold_overflow_warning (("assuming pointer wraparound does not "
8647 "occur when comparing P +- C1 with "
8648 "P +- C2"),
8649 WARN_STRICT_OVERFLOW_CONDITIONAL);
8651 switch (code)
8653 case EQ_EXPR:
8654 return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8655 case NE_EXPR:
8656 return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8657 case LT_EXPR:
8658 return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8659 case LE_EXPR:
8660 return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8661 case GE_EXPR:
8662 return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8663 case GT_EXPR:
8664 return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8665 default:;
8668 /* We can simplify the comparison to a comparison of the variable
8669 offset parts if the constant offset parts are equal.
8670 Be careful to use signed size type here because otherwise we
8671 mess with array offsets in the wrong way. This is possible
8672 because pointer arithmetic is restricted to retain within an
8673 object and overflow on pointer differences is undefined as of
8674 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8675 else if (bitpos0 == bitpos1
8676 && ((code == EQ_EXPR || code == NE_EXPR)
8677 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8679 tree signed_size_type_node;
8680 signed_size_type_node = signed_type_for (size_type_node);
8682 /* By converting to signed size type we cover middle-end pointer
8683 arithmetic which operates on unsigned pointer types of size
8684 type size and ARRAY_REF offsets which are properly sign or
8685 zero extended from their type in case it is narrower than
8686 size type. */
8687 if (offset0 == NULL_TREE)
8688 offset0 = build_int_cst (signed_size_type_node, 0);
8689 else
8690 offset0 = fold_convert (signed_size_type_node, offset0);
8691 if (offset1 == NULL_TREE)
8692 offset1 = build_int_cst (signed_size_type_node, 0);
8693 else
8694 offset1 = fold_convert (signed_size_type_node, offset1);
8696 if (code != EQ_EXPR
8697 && code != NE_EXPR
8698 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8699 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8700 fold_overflow_warning (("assuming pointer wraparound does not "
8701 "occur when comparing P +- C1 with "
8702 "P +- C2"),
8703 WARN_STRICT_OVERFLOW_COMPARISON);
8705 return fold_build2 (code, type, offset0, offset1);
8708 /* For non-equal bases we can simplify if they are addresses
8709 of local binding decls or constants. */
8710 else if (indirect_base0 && indirect_base1
8711 /* We know that !operand_equal_p (base0, base1, 0)
8712 because the if condition was false. But make
8713 sure two decls are not the same. */
8714 && base0 != base1
8715 && TREE_CODE (arg0) == ADDR_EXPR
8716 && TREE_CODE (arg1) == ADDR_EXPR
8717 && (((TREE_CODE (base0) == VAR_DECL
8718 || TREE_CODE (base0) == PARM_DECL)
8719 && (targetm.binds_local_p (base0)
8720 || CONSTANT_CLASS_P (base1)))
8721 || CONSTANT_CLASS_P (base0))
8722 && (((TREE_CODE (base1) == VAR_DECL
8723 || TREE_CODE (base1) == PARM_DECL)
8724 && (targetm.binds_local_p (base1)
8725 || CONSTANT_CLASS_P (base0)))
8726 || CONSTANT_CLASS_P (base1)))
8728 if (code == EQ_EXPR)
8729 return omit_two_operands (type, boolean_false_node, arg0, arg1);
8730 else if (code == NE_EXPR)
8731 return omit_two_operands (type, boolean_true_node, arg0, arg1);
8733 /* For equal offsets we can simplify to a comparison of the
8734 base addresses. */
8735 else if (bitpos0 == bitpos1
8736 && (indirect_base0
8737 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8738 && (indirect_base1
8739 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8740 && ((offset0 == offset1)
8741 || (offset0 && offset1
8742 && operand_equal_p (offset0, offset1, 0))))
8744 if (indirect_base0)
8745 base0 = fold_addr_expr (base0);
8746 if (indirect_base1)
8747 base1 = fold_addr_expr (base1);
8748 return fold_build2 (code, type, base0, base1);
8752 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8753 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8754 the resulting offset is smaller in absolute value than the
8755 original one. */
8756 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8757 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8758 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8759 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8760 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8761 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8762 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8764 tree const1 = TREE_OPERAND (arg0, 1);
8765 tree const2 = TREE_OPERAND (arg1, 1);
8766 tree variable1 = TREE_OPERAND (arg0, 0);
8767 tree variable2 = TREE_OPERAND (arg1, 0);
8768 tree cst;
8769 const char * const warnmsg = G_("assuming signed overflow does not "
8770 "occur when combining constants around "
8771 "a comparison");
8773 /* Put the constant on the side where it doesn't overflow and is
8774 of lower absolute value than before. */
8775 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8776 ? MINUS_EXPR : PLUS_EXPR,
8777 const2, const1, 0);
8778 if (!TREE_OVERFLOW (cst)
8779 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8781 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8782 return fold_build2 (code, type,
8783 variable1,
8784 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8785 variable2, cst));
8788 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8789 ? MINUS_EXPR : PLUS_EXPR,
8790 const1, const2, 0);
8791 if (!TREE_OVERFLOW (cst)
8792 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8794 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8795 return fold_build2 (code, type,
8796 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8797 variable1, cst),
8798 variable2);
8802 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8803 signed arithmetic case. That form is created by the compiler
8804 often enough for folding it to be of value. One example is in
8805 computing loop trip counts after Operator Strength Reduction. */
8806 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8807 && TREE_CODE (arg0) == MULT_EXPR
8808 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8809 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8810 && integer_zerop (arg1))
8812 tree const1 = TREE_OPERAND (arg0, 1);
8813 tree const2 = arg1; /* zero */
8814 tree variable1 = TREE_OPERAND (arg0, 0);
8815 enum tree_code cmp_code = code;
8817 gcc_assert (!integer_zerop (const1));
8819 fold_overflow_warning (("assuming signed overflow does not occur when "
8820 "eliminating multiplication in comparison "
8821 "with zero"),
8822 WARN_STRICT_OVERFLOW_COMPARISON);
8824 /* If const1 is negative we swap the sense of the comparison. */
8825 if (tree_int_cst_sgn (const1) < 0)
8826 cmp_code = swap_tree_comparison (cmp_code);
8828 return fold_build2 (cmp_code, type, variable1, const2);
8831 tem = maybe_canonicalize_comparison (code, type, op0, op1);
8832 if (tem)
8833 return tem;
8835 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8837 tree targ0 = strip_float_extensions (arg0);
8838 tree targ1 = strip_float_extensions (arg1);
8839 tree newtype = TREE_TYPE (targ0);
8841 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8842 newtype = TREE_TYPE (targ1);
8844 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8845 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8846 return fold_build2 (code, type, fold_convert (newtype, targ0),
8847 fold_convert (newtype, targ1));
8849 /* (-a) CMP (-b) -> b CMP a */
8850 if (TREE_CODE (arg0) == NEGATE_EXPR
8851 && TREE_CODE (arg1) == NEGATE_EXPR)
8852 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8853 TREE_OPERAND (arg0, 0));
8855 if (TREE_CODE (arg1) == REAL_CST)
8857 REAL_VALUE_TYPE cst;
8858 cst = TREE_REAL_CST (arg1);
8860 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8861 if (TREE_CODE (arg0) == NEGATE_EXPR)
8862 return fold_build2 (swap_tree_comparison (code), type,
8863 TREE_OPERAND (arg0, 0),
8864 build_real (TREE_TYPE (arg1),
8865 REAL_VALUE_NEGATE (cst)));
8867 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8868 /* a CMP (-0) -> a CMP 0 */
8869 if (REAL_VALUE_MINUS_ZERO (cst))
8870 return fold_build2 (code, type, arg0,
8871 build_real (TREE_TYPE (arg1), dconst0));
8873 /* x != NaN is always true, other ops are always false. */
8874 if (REAL_VALUE_ISNAN (cst)
8875 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8877 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8878 return omit_one_operand (type, tem, arg0);
8881 /* Fold comparisons against infinity. */
8882 if (REAL_VALUE_ISINF (cst))
8884 tem = fold_inf_compare (code, type, arg0, arg1);
8885 if (tem != NULL_TREE)
8886 return tem;
8890 /* If this is a comparison of a real constant with a PLUS_EXPR
8891 or a MINUS_EXPR of a real constant, we can convert it into a
8892 comparison with a revised real constant as long as no overflow
8893 occurs when unsafe_math_optimizations are enabled. */
8894 if (flag_unsafe_math_optimizations
8895 && TREE_CODE (arg1) == REAL_CST
8896 && (TREE_CODE (arg0) == PLUS_EXPR
8897 || TREE_CODE (arg0) == MINUS_EXPR)
8898 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8899 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8900 ? MINUS_EXPR : PLUS_EXPR,
8901 arg1, TREE_OPERAND (arg0, 1), 0))
8902 && !TREE_OVERFLOW (tem))
8903 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8905 /* Likewise, we can simplify a comparison of a real constant with
8906 a MINUS_EXPR whose first operand is also a real constant, i.e.
8907 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
8908 floating-point types only if -fassociative-math is set. */
8909 if (flag_associative_math
8910 && TREE_CODE (arg1) == REAL_CST
8911 && TREE_CODE (arg0) == MINUS_EXPR
8912 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8913 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8914 arg1, 0))
8915 && !TREE_OVERFLOW (tem))
8916 return fold_build2 (swap_tree_comparison (code), type,
8917 TREE_OPERAND (arg0, 1), tem);
8919 /* Fold comparisons against built-in math functions. */
8920 if (TREE_CODE (arg1) == REAL_CST
8921 && flag_unsafe_math_optimizations
8922 && ! flag_errno_math)
8924 enum built_in_function fcode = builtin_mathfn_code (arg0);
8926 if (fcode != END_BUILTINS)
8928 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8929 if (tem != NULL_TREE)
8930 return tem;
8935 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8936 && CONVERT_EXPR_P (arg0))
8938 /* If we are widening one operand of an integer comparison,
8939 see if the other operand is similarly being widened. Perhaps we
8940 can do the comparison in the narrower type. */
8941 tem = fold_widened_comparison (code, type, arg0, arg1);
8942 if (tem)
8943 return tem;
8945 /* Or if we are changing signedness. */
8946 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8947 if (tem)
8948 return tem;
8951 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8952 constant, we can simplify it. */
8953 if (TREE_CODE (arg1) == INTEGER_CST
8954 && (TREE_CODE (arg0) == MIN_EXPR
8955 || TREE_CODE (arg0) == MAX_EXPR)
8956 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8958 tem = optimize_minmax_comparison (code, type, op0, op1);
8959 if (tem)
8960 return tem;
8963 /* Simplify comparison of something with itself. (For IEEE
8964 floating-point, we can only do some of these simplifications.) */
8965 if (operand_equal_p (arg0, arg1, 0))
8967 switch (code)
8969 case EQ_EXPR:
8970 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8971 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8972 return constant_boolean_node (1, type);
8973 break;
8975 case GE_EXPR:
8976 case LE_EXPR:
8977 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8978 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8979 return constant_boolean_node (1, type);
8980 return fold_build2 (EQ_EXPR, type, arg0, arg1);
8982 case NE_EXPR:
8983 /* For NE, we can only do this simplification if integer
8984 or we don't honor IEEE floating point NaNs. */
8985 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8986 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8987 break;
8988 /* ... fall through ... */
8989 case GT_EXPR:
8990 case LT_EXPR:
8991 return constant_boolean_node (0, type);
8992 default:
8993 gcc_unreachable ();
8997 /* If we are comparing an expression that just has comparisons
8998 of two integer values, arithmetic expressions of those comparisons,
8999 and constants, we can simplify it. There are only three cases
9000 to check: the two values can either be equal, the first can be
9001 greater, or the second can be greater. Fold the expression for
9002 those three values. Since each value must be 0 or 1, we have
9003 eight possibilities, each of which corresponds to the constant 0
9004 or 1 or one of the six possible comparisons.
9006 This handles common cases like (a > b) == 0 but also handles
9007 expressions like ((x > y) - (y > x)) > 0, which supposedly
9008 occur in macroized code. */
9010 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9012 tree cval1 = 0, cval2 = 0;
9013 int save_p = 0;
9015 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9016 /* Don't handle degenerate cases here; they should already
9017 have been handled anyway. */
9018 && cval1 != 0 && cval2 != 0
9019 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9020 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9021 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9022 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9023 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9024 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9025 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9027 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9028 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9030 /* We can't just pass T to eval_subst in case cval1 or cval2
9031 was the same as ARG1. */
9033 tree high_result
9034 = fold_build2 (code, type,
9035 eval_subst (arg0, cval1, maxval,
9036 cval2, minval),
9037 arg1);
9038 tree equal_result
9039 = fold_build2 (code, type,
9040 eval_subst (arg0, cval1, maxval,
9041 cval2, maxval),
9042 arg1);
9043 tree low_result
9044 = fold_build2 (code, type,
9045 eval_subst (arg0, cval1, minval,
9046 cval2, maxval),
9047 arg1);
9049 /* All three of these results should be 0 or 1. Confirm they are.
9050 Then use those values to select the proper code to use. */
9052 if (TREE_CODE (high_result) == INTEGER_CST
9053 && TREE_CODE (equal_result) == INTEGER_CST
9054 && TREE_CODE (low_result) == INTEGER_CST)
9056 /* Make a 3-bit mask with the high-order bit being the
9057 value for `>', the next for '=', and the low for '<'. */
9058 switch ((integer_onep (high_result) * 4)
9059 + (integer_onep (equal_result) * 2)
9060 + integer_onep (low_result))
9062 case 0:
9063 /* Always false. */
9064 return omit_one_operand (type, integer_zero_node, arg0);
9065 case 1:
9066 code = LT_EXPR;
9067 break;
9068 case 2:
9069 code = EQ_EXPR;
9070 break;
9071 case 3:
9072 code = LE_EXPR;
9073 break;
9074 case 4:
9075 code = GT_EXPR;
9076 break;
9077 case 5:
9078 code = NE_EXPR;
9079 break;
9080 case 6:
9081 code = GE_EXPR;
9082 break;
9083 case 7:
9084 /* Always true. */
9085 return omit_one_operand (type, integer_one_node, arg0);
9088 if (save_p)
9089 return save_expr (build2 (code, type, cval1, cval2));
9090 return fold_build2 (code, type, cval1, cval2);
9095 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9096 into a single range test. */
9097 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9098 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9099 && TREE_CODE (arg1) == INTEGER_CST
9100 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9101 && !integer_zerop (TREE_OPERAND (arg0, 1))
9102 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9103 && !TREE_OVERFLOW (arg1))
9105 tem = fold_div_compare (code, type, arg0, arg1);
9106 if (tem != NULL_TREE)
9107 return tem;
9110 /* Fold ~X op ~Y as Y op X. */
9111 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9112 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9114 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9115 return fold_build2 (code, type,
9116 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9117 TREE_OPERAND (arg0, 0));
9120 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9121 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9122 && TREE_CODE (arg1) == INTEGER_CST)
9124 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9125 return fold_build2 (swap_tree_comparison (code), type,
9126 TREE_OPERAND (arg0, 0),
9127 fold_build1 (BIT_NOT_EXPR, cmp_type,
9128 fold_convert (cmp_type, arg1)));
9131 return NULL_TREE;
9135 /* Subroutine of fold_binary. Optimize complex multiplications of the
9136 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9137 argument EXPR represents the expression "z" of type TYPE. */
9139 static tree
9140 fold_mult_zconjz (tree type, tree expr)
9142 tree itype = TREE_TYPE (type);
9143 tree rpart, ipart, tem;
9145 if (TREE_CODE (expr) == COMPLEX_EXPR)
9147 rpart = TREE_OPERAND (expr, 0);
9148 ipart = TREE_OPERAND (expr, 1);
9150 else if (TREE_CODE (expr) == COMPLEX_CST)
9152 rpart = TREE_REALPART (expr);
9153 ipart = TREE_IMAGPART (expr);
9155 else
9157 expr = save_expr (expr);
9158 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9159 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9162 rpart = save_expr (rpart);
9163 ipart = save_expr (ipart);
9164 tem = fold_build2 (PLUS_EXPR, itype,
9165 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9166 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9167 return fold_build2 (COMPLEX_EXPR, type, tem,
9168 fold_convert (itype, integer_zero_node));
9172 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9173 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9174 guarantees that P and N have the same least significant log2(M) bits.
9175 N is not otherwise constrained. In particular, N is not normalized to
9176 0 <= N < M as is common. In general, the precise value of P is unknown.
9177 M is chosen as large as possible such that constant N can be determined.
9179 Returns M and sets *RESIDUE to N. */
9181 static unsigned HOST_WIDE_INT
9182 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9184 enum tree_code code;
9186 *residue = 0;
9188 code = TREE_CODE (expr);
9189 if (code == ADDR_EXPR)
9191 expr = TREE_OPERAND (expr, 0);
9192 if (handled_component_p (expr))
9194 HOST_WIDE_INT bitsize, bitpos;
9195 tree offset;
9196 enum machine_mode mode;
9197 int unsignedp, volatilep;
9199 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9200 &mode, &unsignedp, &volatilep, false);
9201 *residue = bitpos / BITS_PER_UNIT;
9202 if (offset)
9204 if (TREE_CODE (offset) == INTEGER_CST)
9205 *residue += TREE_INT_CST_LOW (offset);
9206 else
9207 /* We don't handle more complicated offset expressions. */
9208 return 1;
9212 if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9213 return DECL_ALIGN_UNIT (expr);
9215 else if (code == POINTER_PLUS_EXPR)
9217 tree op0, op1;
9218 unsigned HOST_WIDE_INT modulus;
9219 enum tree_code inner_code;
9221 op0 = TREE_OPERAND (expr, 0);
9222 STRIP_NOPS (op0);
9223 modulus = get_pointer_modulus_and_residue (op0, residue);
9225 op1 = TREE_OPERAND (expr, 1);
9226 STRIP_NOPS (op1);
9227 inner_code = TREE_CODE (op1);
9228 if (inner_code == INTEGER_CST)
9230 *residue += TREE_INT_CST_LOW (op1);
9231 return modulus;
9233 else if (inner_code == MULT_EXPR)
9235 op1 = TREE_OPERAND (op1, 1);
9236 if (TREE_CODE (op1) == INTEGER_CST)
9238 unsigned HOST_WIDE_INT align;
9240 /* Compute the greatest power-of-2 divisor of op1. */
9241 align = TREE_INT_CST_LOW (op1);
9242 align &= -align;
9244 /* If align is non-zero and less than *modulus, replace
9245 *modulus with align., If align is 0, then either op1 is 0
9246 or the greatest power-of-2 divisor of op1 doesn't fit in an
9247 unsigned HOST_WIDE_INT. In either case, no additional
9248 constraint is imposed. */
9249 if (align)
9250 modulus = MIN (modulus, align);
9252 return modulus;
9257 /* If we get here, we were unable to determine anything useful about the
9258 expression. */
9259 return 1;
9263 /* Fold a binary expression of code CODE and type TYPE with operands
9264 OP0 and OP1. Return the folded expression if folding is
9265 successful. Otherwise, return NULL_TREE. */
9267 tree
9268 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9270 enum tree_code_class kind = TREE_CODE_CLASS (code);
9271 tree arg0, arg1, tem;
9272 tree t1 = NULL_TREE;
9273 bool strict_overflow_p;
9275 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9276 && TREE_CODE_LENGTH (code) == 2
9277 && op0 != NULL_TREE
9278 && op1 != NULL_TREE);
9280 arg0 = op0;
9281 arg1 = op1;
9283 /* Strip any conversions that don't change the mode. This is
9284 safe for every expression, except for a comparison expression
9285 because its signedness is derived from its operands. So, in
9286 the latter case, only strip conversions that don't change the
9287 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9288 preserved.
9290 Note that this is done as an internal manipulation within the
9291 constant folder, in order to find the simplest representation
9292 of the arguments so that their form can be studied. In any
9293 cases, the appropriate type conversions should be put back in
9294 the tree that will get out of the constant folder. */
9296 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9298 STRIP_SIGN_NOPS (arg0);
9299 STRIP_SIGN_NOPS (arg1);
9301 else
9303 STRIP_NOPS (arg0);
9304 STRIP_NOPS (arg1);
9307 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9308 constant but we can't do arithmetic on them. */
9309 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9310 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9311 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9312 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9313 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9314 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9316 if (kind == tcc_binary)
9318 /* Make sure type and arg0 have the same saturating flag. */
9319 gcc_assert (TYPE_SATURATING (type)
9320 == TYPE_SATURATING (TREE_TYPE (arg0)));
9321 tem = const_binop (code, arg0, arg1, 0);
9323 else if (kind == tcc_comparison)
9324 tem = fold_relational_const (code, type, arg0, arg1);
9325 else
9326 tem = NULL_TREE;
9328 if (tem != NULL_TREE)
9330 if (TREE_TYPE (tem) != type)
9331 tem = fold_convert (type, tem);
9332 return tem;
9336 /* If this is a commutative operation, and ARG0 is a constant, move it
9337 to ARG1 to reduce the number of tests below. */
9338 if (commutative_tree_code (code)
9339 && tree_swap_operands_p (arg0, arg1, true))
9340 return fold_build2 (code, type, op1, op0);
9342 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9344 First check for cases where an arithmetic operation is applied to a
9345 compound, conditional, or comparison operation. Push the arithmetic
9346 operation inside the compound or conditional to see if any folding
9347 can then be done. Convert comparison to conditional for this purpose.
9348 The also optimizes non-constant cases that used to be done in
9349 expand_expr.
9351 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9352 one of the operands is a comparison and the other is a comparison, a
9353 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9354 code below would make the expression more complex. Change it to a
9355 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9356 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9358 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9359 || code == EQ_EXPR || code == NE_EXPR)
9360 && ((truth_value_p (TREE_CODE (arg0))
9361 && (truth_value_p (TREE_CODE (arg1))
9362 || (TREE_CODE (arg1) == BIT_AND_EXPR
9363 && integer_onep (TREE_OPERAND (arg1, 1)))))
9364 || (truth_value_p (TREE_CODE (arg1))
9365 && (truth_value_p (TREE_CODE (arg0))
9366 || (TREE_CODE (arg0) == BIT_AND_EXPR
9367 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9369 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9370 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9371 : TRUTH_XOR_EXPR,
9372 boolean_type_node,
9373 fold_convert (boolean_type_node, arg0),
9374 fold_convert (boolean_type_node, arg1));
9376 if (code == EQ_EXPR)
9377 tem = invert_truthvalue (tem);
9379 return fold_convert (type, tem);
9382 if (TREE_CODE_CLASS (code) == tcc_binary
9383 || TREE_CODE_CLASS (code) == tcc_comparison)
9385 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9386 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9387 fold_build2 (code, type,
9388 fold_convert (TREE_TYPE (op0),
9389 TREE_OPERAND (arg0, 1)),
9390 op1));
9391 if (TREE_CODE (arg1) == COMPOUND_EXPR
9392 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9393 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9394 fold_build2 (code, type, op0,
9395 fold_convert (TREE_TYPE (op1),
9396 TREE_OPERAND (arg1, 1))));
9398 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9400 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9401 arg0, arg1,
9402 /*cond_first_p=*/1);
9403 if (tem != NULL_TREE)
9404 return tem;
9407 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9409 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9410 arg1, arg0,
9411 /*cond_first_p=*/0);
9412 if (tem != NULL_TREE)
9413 return tem;
9417 switch (code)
9419 case POINTER_PLUS_EXPR:
9420 /* 0 +p index -> (type)index */
9421 if (integer_zerop (arg0))
9422 return non_lvalue (fold_convert (type, arg1));
9424 /* PTR +p 0 -> PTR */
9425 if (integer_zerop (arg1))
9426 return non_lvalue (fold_convert (type, arg0));
9428 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9429 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9430 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9431 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9432 fold_convert (sizetype, arg1),
9433 fold_convert (sizetype, arg0)));
9435 /* index +p PTR -> PTR +p index */
9436 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9437 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9438 return fold_build2 (POINTER_PLUS_EXPR, type,
9439 fold_convert (type, arg1),
9440 fold_convert (sizetype, arg0));
9442 /* (PTR +p B) +p A -> PTR +p (B + A) */
9443 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9445 tree inner;
9446 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9447 tree arg00 = TREE_OPERAND (arg0, 0);
9448 inner = fold_build2 (PLUS_EXPR, sizetype,
9449 arg01, fold_convert (sizetype, arg1));
9450 return fold_convert (type,
9451 fold_build2 (POINTER_PLUS_EXPR,
9452 TREE_TYPE (arg00), arg00, inner));
9455 /* PTR_CST +p CST -> CST1 */
9456 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9457 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9459 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9460 of the array. Loop optimizer sometimes produce this type of
9461 expressions. */
9462 if (TREE_CODE (arg0) == ADDR_EXPR)
9464 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9465 if (tem)
9466 return fold_convert (type, tem);
9469 return NULL_TREE;
9471 case PLUS_EXPR:
9472 /* PTR + INT -> (INT)(PTR p+ INT) */
9473 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9474 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9475 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9476 TREE_TYPE (arg0),
9477 arg0,
9478 fold_convert (sizetype, arg1)));
9479 /* INT + PTR -> (INT)(PTR p+ INT) */
9480 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9481 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9482 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9483 TREE_TYPE (arg1),
9484 arg1,
9485 fold_convert (sizetype, arg0)));
9486 /* A + (-B) -> A - B */
9487 if (TREE_CODE (arg1) == NEGATE_EXPR)
9488 return fold_build2 (MINUS_EXPR, type,
9489 fold_convert (type, arg0),
9490 fold_convert (type, TREE_OPERAND (arg1, 0)));
9491 /* (-A) + B -> B - A */
9492 if (TREE_CODE (arg0) == NEGATE_EXPR
9493 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9494 return fold_build2 (MINUS_EXPR, type,
9495 fold_convert (type, arg1),
9496 fold_convert (type, TREE_OPERAND (arg0, 0)));
9498 if (INTEGRAL_TYPE_P (type))
9500 /* Convert ~A + 1 to -A. */
9501 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9502 && integer_onep (arg1))
9503 return fold_build1 (NEGATE_EXPR, type,
9504 fold_convert (type, TREE_OPERAND (arg0, 0)));
9506 /* ~X + X is -1. */
9507 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9508 && !TYPE_OVERFLOW_TRAPS (type))
9510 tree tem = TREE_OPERAND (arg0, 0);
9512 STRIP_NOPS (tem);
9513 if (operand_equal_p (tem, arg1, 0))
9515 t1 = build_int_cst_type (type, -1);
9516 return omit_one_operand (type, t1, arg1);
9520 /* X + ~X is -1. */
9521 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9522 && !TYPE_OVERFLOW_TRAPS (type))
9524 tree tem = TREE_OPERAND (arg1, 0);
9526 STRIP_NOPS (tem);
9527 if (operand_equal_p (arg0, tem, 0))
9529 t1 = build_int_cst_type (type, -1);
9530 return omit_one_operand (type, t1, arg0);
9534 /* X + (X / CST) * -CST is X % CST. */
9535 if (TREE_CODE (arg1) == MULT_EXPR
9536 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9537 && operand_equal_p (arg0,
9538 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9540 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9541 tree cst1 = TREE_OPERAND (arg1, 1);
9542 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9543 if (sum && integer_zerop (sum))
9544 return fold_convert (type,
9545 fold_build2 (TRUNC_MOD_EXPR,
9546 TREE_TYPE (arg0), arg0, cst0));
9550 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9551 same or one. Make sure type is not saturating.
9552 fold_plusminus_mult_expr will re-associate. */
9553 if ((TREE_CODE (arg0) == MULT_EXPR
9554 || TREE_CODE (arg1) == MULT_EXPR)
9555 && !TYPE_SATURATING (type)
9556 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9558 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9559 if (tem)
9560 return tem;
9563 if (! FLOAT_TYPE_P (type))
9565 if (integer_zerop (arg1))
9566 return non_lvalue (fold_convert (type, arg0));
9568 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9569 with a constant, and the two constants have no bits in common,
9570 we should treat this as a BIT_IOR_EXPR since this may produce more
9571 simplifications. */
9572 if (TREE_CODE (arg0) == BIT_AND_EXPR
9573 && TREE_CODE (arg1) == BIT_AND_EXPR
9574 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9575 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9576 && integer_zerop (const_binop (BIT_AND_EXPR,
9577 TREE_OPERAND (arg0, 1),
9578 TREE_OPERAND (arg1, 1), 0)))
9580 code = BIT_IOR_EXPR;
9581 goto bit_ior;
9584 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9585 (plus (plus (mult) (mult)) (foo)) so that we can
9586 take advantage of the factoring cases below. */
9587 if (((TREE_CODE (arg0) == PLUS_EXPR
9588 || TREE_CODE (arg0) == MINUS_EXPR)
9589 && TREE_CODE (arg1) == MULT_EXPR)
9590 || ((TREE_CODE (arg1) == PLUS_EXPR
9591 || TREE_CODE (arg1) == MINUS_EXPR)
9592 && TREE_CODE (arg0) == MULT_EXPR))
9594 tree parg0, parg1, parg, marg;
9595 enum tree_code pcode;
9597 if (TREE_CODE (arg1) == MULT_EXPR)
9598 parg = arg0, marg = arg1;
9599 else
9600 parg = arg1, marg = arg0;
9601 pcode = TREE_CODE (parg);
9602 parg0 = TREE_OPERAND (parg, 0);
9603 parg1 = TREE_OPERAND (parg, 1);
9604 STRIP_NOPS (parg0);
9605 STRIP_NOPS (parg1);
9607 if (TREE_CODE (parg0) == MULT_EXPR
9608 && TREE_CODE (parg1) != MULT_EXPR)
9609 return fold_build2 (pcode, type,
9610 fold_build2 (PLUS_EXPR, type,
9611 fold_convert (type, parg0),
9612 fold_convert (type, marg)),
9613 fold_convert (type, parg1));
9614 if (TREE_CODE (parg0) != MULT_EXPR
9615 && TREE_CODE (parg1) == MULT_EXPR)
9616 return fold_build2 (PLUS_EXPR, type,
9617 fold_convert (type, parg0),
9618 fold_build2 (pcode, type,
9619 fold_convert (type, marg),
9620 fold_convert (type,
9621 parg1)));
9624 else
9626 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9627 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9628 return non_lvalue (fold_convert (type, arg0));
9630 /* Likewise if the operands are reversed. */
9631 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9632 return non_lvalue (fold_convert (type, arg1));
9634 /* Convert X + -C into X - C. */
9635 if (TREE_CODE (arg1) == REAL_CST
9636 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9638 tem = fold_negate_const (arg1, type);
9639 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9640 return fold_build2 (MINUS_EXPR, type,
9641 fold_convert (type, arg0),
9642 fold_convert (type, tem));
9645 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9646 to __complex__ ( x, y ). This is not the same for SNaNs or
9647 if signed zeros are involved. */
9648 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9649 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9650 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9652 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9653 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9654 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9655 bool arg0rz = false, arg0iz = false;
9656 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9657 || (arg0i && (arg0iz = real_zerop (arg0i))))
9659 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9660 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9661 if (arg0rz && arg1i && real_zerop (arg1i))
9663 tree rp = arg1r ? arg1r
9664 : build1 (REALPART_EXPR, rtype, arg1);
9665 tree ip = arg0i ? arg0i
9666 : build1 (IMAGPART_EXPR, rtype, arg0);
9667 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9669 else if (arg0iz && arg1r && real_zerop (arg1r))
9671 tree rp = arg0r ? arg0r
9672 : build1 (REALPART_EXPR, rtype, arg0);
9673 tree ip = arg1i ? arg1i
9674 : build1 (IMAGPART_EXPR, rtype, arg1);
9675 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9680 if (flag_unsafe_math_optimizations
9681 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9682 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9683 && (tem = distribute_real_division (code, type, arg0, arg1)))
9684 return tem;
9686 /* Convert x+x into x*2.0. */
9687 if (operand_equal_p (arg0, arg1, 0)
9688 && SCALAR_FLOAT_TYPE_P (type))
9689 return fold_build2 (MULT_EXPR, type, arg0,
9690 build_real (type, dconst2));
9692 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9693 We associate floats only if the user has specified
9694 -fassociative-math. */
9695 if (flag_associative_math
9696 && TREE_CODE (arg1) == PLUS_EXPR
9697 && TREE_CODE (arg0) != MULT_EXPR)
9699 tree tree10 = TREE_OPERAND (arg1, 0);
9700 tree tree11 = TREE_OPERAND (arg1, 1);
9701 if (TREE_CODE (tree11) == MULT_EXPR
9702 && TREE_CODE (tree10) == MULT_EXPR)
9704 tree tree0;
9705 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9706 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9709 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9710 We associate floats only if the user has specified
9711 -fassociative-math. */
9712 if (flag_associative_math
9713 && TREE_CODE (arg0) == PLUS_EXPR
9714 && TREE_CODE (arg1) != MULT_EXPR)
9716 tree tree00 = TREE_OPERAND (arg0, 0);
9717 tree tree01 = TREE_OPERAND (arg0, 1);
9718 if (TREE_CODE (tree01) == MULT_EXPR
9719 && TREE_CODE (tree00) == MULT_EXPR)
9721 tree tree0;
9722 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9723 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9728 bit_rotate:
9729 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9730 is a rotate of A by C1 bits. */
9731 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9732 is a rotate of A by B bits. */
9734 enum tree_code code0, code1;
9735 tree rtype;
9736 code0 = TREE_CODE (arg0);
9737 code1 = TREE_CODE (arg1);
9738 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9739 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9740 && operand_equal_p (TREE_OPERAND (arg0, 0),
9741 TREE_OPERAND (arg1, 0), 0)
9742 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9743 TYPE_UNSIGNED (rtype))
9744 /* Only create rotates in complete modes. Other cases are not
9745 expanded properly. */
9746 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9748 tree tree01, tree11;
9749 enum tree_code code01, code11;
9751 tree01 = TREE_OPERAND (arg0, 1);
9752 tree11 = TREE_OPERAND (arg1, 1);
9753 STRIP_NOPS (tree01);
9754 STRIP_NOPS (tree11);
9755 code01 = TREE_CODE (tree01);
9756 code11 = TREE_CODE (tree11);
9757 if (code01 == INTEGER_CST
9758 && code11 == INTEGER_CST
9759 && TREE_INT_CST_HIGH (tree01) == 0
9760 && TREE_INT_CST_HIGH (tree11) == 0
9761 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9762 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9763 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9764 code0 == LSHIFT_EXPR ? tree01 : tree11);
9765 else if (code11 == MINUS_EXPR)
9767 tree tree110, tree111;
9768 tree110 = TREE_OPERAND (tree11, 0);
9769 tree111 = TREE_OPERAND (tree11, 1);
9770 STRIP_NOPS (tree110);
9771 STRIP_NOPS (tree111);
9772 if (TREE_CODE (tree110) == INTEGER_CST
9773 && 0 == compare_tree_int (tree110,
9774 TYPE_PRECISION
9775 (TREE_TYPE (TREE_OPERAND
9776 (arg0, 0))))
9777 && operand_equal_p (tree01, tree111, 0))
9778 return build2 ((code0 == LSHIFT_EXPR
9779 ? LROTATE_EXPR
9780 : RROTATE_EXPR),
9781 type, TREE_OPERAND (arg0, 0), tree01);
9783 else if (code01 == MINUS_EXPR)
9785 tree tree010, tree011;
9786 tree010 = TREE_OPERAND (tree01, 0);
9787 tree011 = TREE_OPERAND (tree01, 1);
9788 STRIP_NOPS (tree010);
9789 STRIP_NOPS (tree011);
9790 if (TREE_CODE (tree010) == INTEGER_CST
9791 && 0 == compare_tree_int (tree010,
9792 TYPE_PRECISION
9793 (TREE_TYPE (TREE_OPERAND
9794 (arg0, 0))))
9795 && operand_equal_p (tree11, tree011, 0))
9796 return build2 ((code0 != LSHIFT_EXPR
9797 ? LROTATE_EXPR
9798 : RROTATE_EXPR),
9799 type, TREE_OPERAND (arg0, 0), tree11);
9804 associate:
9805 /* In most languages, can't associate operations on floats through
9806 parentheses. Rather than remember where the parentheses were, we
9807 don't associate floats at all, unless the user has specified
9808 -fassociative-math.
9809 And, we need to make sure type is not saturating. */
9811 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9812 && !TYPE_SATURATING (type))
9814 tree var0, con0, lit0, minus_lit0;
9815 tree var1, con1, lit1, minus_lit1;
9816 bool ok = true;
9818 /* Split both trees into variables, constants, and literals. Then
9819 associate each group together, the constants with literals,
9820 then the result with variables. This increases the chances of
9821 literals being recombined later and of generating relocatable
9822 expressions for the sum of a constant and literal. */
9823 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9824 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9825 code == MINUS_EXPR);
9827 /* With undefined overflow we can only associate constants
9828 with one variable. */
9829 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9830 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9831 && var0 && var1)
9833 tree tmp0 = var0;
9834 tree tmp1 = var1;
9836 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9837 tmp0 = TREE_OPERAND (tmp0, 0);
9838 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9839 tmp1 = TREE_OPERAND (tmp1, 0);
9840 /* The only case we can still associate with two variables
9841 is if they are the same, modulo negation. */
9842 if (!operand_equal_p (tmp0, tmp1, 0))
9843 ok = false;
9846 /* Only do something if we found more than two objects. Otherwise,
9847 nothing has changed and we risk infinite recursion. */
9848 if (ok
9849 && (2 < ((var0 != 0) + (var1 != 0)
9850 + (con0 != 0) + (con1 != 0)
9851 + (lit0 != 0) + (lit1 != 0)
9852 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9854 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9855 if (code == MINUS_EXPR)
9856 code = PLUS_EXPR;
9858 var0 = associate_trees (var0, var1, code, type);
9859 con0 = associate_trees (con0, con1, code, type);
9860 lit0 = associate_trees (lit0, lit1, code, type);
9861 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9863 /* Preserve the MINUS_EXPR if the negative part of the literal is
9864 greater than the positive part. Otherwise, the multiplicative
9865 folding code (i.e extract_muldiv) may be fooled in case
9866 unsigned constants are subtracted, like in the following
9867 example: ((X*2 + 4) - 8U)/2. */
9868 if (minus_lit0 && lit0)
9870 if (TREE_CODE (lit0) == INTEGER_CST
9871 && TREE_CODE (minus_lit0) == INTEGER_CST
9872 && tree_int_cst_lt (lit0, minus_lit0))
9874 minus_lit0 = associate_trees (minus_lit0, lit0,
9875 MINUS_EXPR, type);
9876 lit0 = 0;
9878 else
9880 lit0 = associate_trees (lit0, minus_lit0,
9881 MINUS_EXPR, type);
9882 minus_lit0 = 0;
9885 if (minus_lit0)
9887 if (con0 == 0)
9888 return fold_convert (type,
9889 associate_trees (var0, minus_lit0,
9890 MINUS_EXPR, type));
9891 else
9893 con0 = associate_trees (con0, minus_lit0,
9894 MINUS_EXPR, type);
9895 return fold_convert (type,
9896 associate_trees (var0, con0,
9897 PLUS_EXPR, type));
9901 con0 = associate_trees (con0, lit0, code, type);
9902 return fold_convert (type, associate_trees (var0, con0,
9903 code, type));
9907 return NULL_TREE;
9909 case MINUS_EXPR:
9910 /* Pointer simplifications for subtraction, simple reassociations. */
9911 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9913 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9914 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9915 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9917 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9918 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9919 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9920 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9921 return fold_build2 (PLUS_EXPR, type,
9922 fold_build2 (MINUS_EXPR, type, arg00, arg10),
9923 fold_build2 (MINUS_EXPR, type, arg01, arg11));
9925 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9926 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9928 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9929 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9930 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9931 if (tmp)
9932 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9935 /* A - (-B) -> A + B */
9936 if (TREE_CODE (arg1) == NEGATE_EXPR)
9937 return fold_build2 (PLUS_EXPR, type, op0,
9938 fold_convert (type, TREE_OPERAND (arg1, 0)));
9939 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9940 if (TREE_CODE (arg0) == NEGATE_EXPR
9941 && (FLOAT_TYPE_P (type)
9942 || INTEGRAL_TYPE_P (type))
9943 && negate_expr_p (arg1)
9944 && reorder_operands_p (arg0, arg1))
9945 return fold_build2 (MINUS_EXPR, type,
9946 fold_convert (type, negate_expr (arg1)),
9947 fold_convert (type, TREE_OPERAND (arg0, 0)));
9948 /* Convert -A - 1 to ~A. */
9949 if (INTEGRAL_TYPE_P (type)
9950 && TREE_CODE (arg0) == NEGATE_EXPR
9951 && integer_onep (arg1)
9952 && !TYPE_OVERFLOW_TRAPS (type))
9953 return fold_build1 (BIT_NOT_EXPR, type,
9954 fold_convert (type, TREE_OPERAND (arg0, 0)));
9956 /* Convert -1 - A to ~A. */
9957 if (INTEGRAL_TYPE_P (type)
9958 && integer_all_onesp (arg0))
9959 return fold_build1 (BIT_NOT_EXPR, type, op1);
9962 /* X - (X / CST) * CST is X % CST. */
9963 if (INTEGRAL_TYPE_P (type)
9964 && TREE_CODE (arg1) == MULT_EXPR
9965 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9966 && operand_equal_p (arg0,
9967 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
9968 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
9969 TREE_OPERAND (arg1, 1), 0))
9970 return fold_convert (type,
9971 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
9972 arg0, TREE_OPERAND (arg1, 1)));
9974 if (! FLOAT_TYPE_P (type))
9976 if (integer_zerop (arg0))
9977 return negate_expr (fold_convert (type, arg1));
9978 if (integer_zerop (arg1))
9979 return non_lvalue (fold_convert (type, arg0));
9981 /* Fold A - (A & B) into ~B & A. */
9982 if (!TREE_SIDE_EFFECTS (arg0)
9983 && TREE_CODE (arg1) == BIT_AND_EXPR)
9985 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9987 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9988 return fold_build2 (BIT_AND_EXPR, type,
9989 fold_build1 (BIT_NOT_EXPR, type, arg10),
9990 fold_convert (type, arg0));
9992 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9994 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9995 return fold_build2 (BIT_AND_EXPR, type,
9996 fold_build1 (BIT_NOT_EXPR, type, arg11),
9997 fold_convert (type, arg0));
10001 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10002 any power of 2 minus 1. */
10003 if (TREE_CODE (arg0) == BIT_AND_EXPR
10004 && TREE_CODE (arg1) == BIT_AND_EXPR
10005 && operand_equal_p (TREE_OPERAND (arg0, 0),
10006 TREE_OPERAND (arg1, 0), 0))
10008 tree mask0 = TREE_OPERAND (arg0, 1);
10009 tree mask1 = TREE_OPERAND (arg1, 1);
10010 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10012 if (operand_equal_p (tem, mask1, 0))
10014 tem = fold_build2 (BIT_XOR_EXPR, type,
10015 TREE_OPERAND (arg0, 0), mask1);
10016 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10021 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10022 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10023 return non_lvalue (fold_convert (type, arg0));
10025 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10026 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10027 (-ARG1 + ARG0) reduces to -ARG1. */
10028 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10029 return negate_expr (fold_convert (type, arg1));
10031 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10032 __complex__ ( x, -y ). This is not the same for SNaNs or if
10033 signed zeros are involved. */
10034 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10035 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10036 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10038 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10039 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10040 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10041 bool arg0rz = false, arg0iz = false;
10042 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10043 || (arg0i && (arg0iz = real_zerop (arg0i))))
10045 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10046 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10047 if (arg0rz && arg1i && real_zerop (arg1i))
10049 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10050 arg1r ? arg1r
10051 : build1 (REALPART_EXPR, rtype, arg1));
10052 tree ip = arg0i ? arg0i
10053 : build1 (IMAGPART_EXPR, rtype, arg0);
10054 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10056 else if (arg0iz && arg1r && real_zerop (arg1r))
10058 tree rp = arg0r ? arg0r
10059 : build1 (REALPART_EXPR, rtype, arg0);
10060 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10061 arg1i ? arg1i
10062 : build1 (IMAGPART_EXPR, rtype, arg1));
10063 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10068 /* Fold &x - &x. This can happen from &x.foo - &x.
10069 This is unsafe for certain floats even in non-IEEE formats.
10070 In IEEE, it is unsafe because it does wrong for NaNs.
10071 Also note that operand_equal_p is always false if an operand
10072 is volatile. */
10074 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10075 && operand_equal_p (arg0, arg1, 0))
10076 return fold_convert (type, integer_zero_node);
10078 /* A - B -> A + (-B) if B is easily negatable. */
10079 if (negate_expr_p (arg1)
10080 && ((FLOAT_TYPE_P (type)
10081 /* Avoid this transformation if B is a positive REAL_CST. */
10082 && (TREE_CODE (arg1) != REAL_CST
10083 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10084 || INTEGRAL_TYPE_P (type)))
10085 return fold_build2 (PLUS_EXPR, type,
10086 fold_convert (type, arg0),
10087 fold_convert (type, negate_expr (arg1)));
10089 /* Try folding difference of addresses. */
10091 HOST_WIDE_INT diff;
10093 if ((TREE_CODE (arg0) == ADDR_EXPR
10094 || TREE_CODE (arg1) == ADDR_EXPR)
10095 && ptr_difference_const (arg0, arg1, &diff))
10096 return build_int_cst_type (type, diff);
10099 /* Fold &a[i] - &a[j] to i-j. */
10100 if (TREE_CODE (arg0) == ADDR_EXPR
10101 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10102 && TREE_CODE (arg1) == ADDR_EXPR
10103 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10105 tree aref0 = TREE_OPERAND (arg0, 0);
10106 tree aref1 = TREE_OPERAND (arg1, 0);
10107 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10108 TREE_OPERAND (aref1, 0), 0))
10110 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10111 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10112 tree esz = array_ref_element_size (aref0);
10113 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10114 return fold_build2 (MULT_EXPR, type, diff,
10115 fold_convert (type, esz));
10120 if (flag_unsafe_math_optimizations
10121 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10122 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10123 && (tem = distribute_real_division (code, type, arg0, arg1)))
10124 return tem;
10126 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10127 same or one. Make sure type is not saturating.
10128 fold_plusminus_mult_expr will re-associate. */
10129 if ((TREE_CODE (arg0) == MULT_EXPR
10130 || TREE_CODE (arg1) == MULT_EXPR)
10131 && !TYPE_SATURATING (type)
10132 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10134 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10135 if (tem)
10136 return tem;
10139 goto associate;
10141 case MULT_EXPR:
10142 /* (-A) * (-B) -> A * B */
10143 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10144 return fold_build2 (MULT_EXPR, type,
10145 fold_convert (type, TREE_OPERAND (arg0, 0)),
10146 fold_convert (type, negate_expr (arg1)));
10147 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10148 return fold_build2 (MULT_EXPR, type,
10149 fold_convert (type, negate_expr (arg0)),
10150 fold_convert (type, TREE_OPERAND (arg1, 0)));
10152 if (! FLOAT_TYPE_P (type))
10154 if (integer_zerop (arg1))
10155 return omit_one_operand (type, arg1, arg0);
10156 if (integer_onep (arg1))
10157 return non_lvalue (fold_convert (type, arg0));
10158 /* Transform x * -1 into -x. Make sure to do the negation
10159 on the original operand with conversions not stripped
10160 because we can only strip non-sign-changing conversions. */
10161 if (integer_all_onesp (arg1))
10162 return fold_convert (type, negate_expr (op0));
10163 /* Transform x * -C into -x * C if x is easily negatable. */
10164 if (TREE_CODE (arg1) == INTEGER_CST
10165 && tree_int_cst_sgn (arg1) == -1
10166 && negate_expr_p (arg0)
10167 && (tem = negate_expr (arg1)) != arg1
10168 && !TREE_OVERFLOW (tem))
10169 return fold_build2 (MULT_EXPR, type,
10170 fold_convert (type, negate_expr (arg0)), tem);
10172 /* (a * (1 << b)) is (a << b) */
10173 if (TREE_CODE (arg1) == LSHIFT_EXPR
10174 && integer_onep (TREE_OPERAND (arg1, 0)))
10175 return fold_build2 (LSHIFT_EXPR, type, op0,
10176 TREE_OPERAND (arg1, 1));
10177 if (TREE_CODE (arg0) == LSHIFT_EXPR
10178 && integer_onep (TREE_OPERAND (arg0, 0)))
10179 return fold_build2 (LSHIFT_EXPR, type, op1,
10180 TREE_OPERAND (arg0, 1));
10182 /* (A + A) * C -> A * 2 * C */
10183 if (TREE_CODE (arg0) == PLUS_EXPR
10184 && TREE_CODE (arg1) == INTEGER_CST
10185 && operand_equal_p (TREE_OPERAND (arg0, 0),
10186 TREE_OPERAND (arg0, 1), 0))
10187 return fold_build2 (MULT_EXPR, type,
10188 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10189 TREE_OPERAND (arg0, 1)),
10190 fold_build2 (MULT_EXPR, type,
10191 build_int_cst (type, 2) , arg1));
10193 strict_overflow_p = false;
10194 if (TREE_CODE (arg1) == INTEGER_CST
10195 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10196 &strict_overflow_p)))
10198 if (strict_overflow_p)
10199 fold_overflow_warning (("assuming signed overflow does not "
10200 "occur when simplifying "
10201 "multiplication"),
10202 WARN_STRICT_OVERFLOW_MISC);
10203 return fold_convert (type, tem);
10206 /* Optimize z * conj(z) for integer complex numbers. */
10207 if (TREE_CODE (arg0) == CONJ_EXPR
10208 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10209 return fold_mult_zconjz (type, arg1);
10210 if (TREE_CODE (arg1) == CONJ_EXPR
10211 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10212 return fold_mult_zconjz (type, arg0);
10214 else
10216 /* Maybe fold x * 0 to 0. The expressions aren't the same
10217 when x is NaN, since x * 0 is also NaN. Nor are they the
10218 same in modes with signed zeros, since multiplying a
10219 negative value by 0 gives -0, not +0. */
10220 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10221 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10222 && real_zerop (arg1))
10223 return omit_one_operand (type, arg1, arg0);
10224 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10225 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10226 && real_onep (arg1))
10227 return non_lvalue (fold_convert (type, arg0));
10229 /* Transform x * -1.0 into -x. */
10230 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10231 && real_minus_onep (arg1))
10232 return fold_convert (type, negate_expr (arg0));
10234 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10235 the result for floating point types due to rounding so it is applied
10236 only if -fassociative-math was specify. */
10237 if (flag_associative_math
10238 && TREE_CODE (arg0) == RDIV_EXPR
10239 && TREE_CODE (arg1) == REAL_CST
10240 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10242 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10243 arg1, 0);
10244 if (tem)
10245 return fold_build2 (RDIV_EXPR, type, tem,
10246 TREE_OPERAND (arg0, 1));
10249 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10250 if (operand_equal_p (arg0, arg1, 0))
10252 tree tem = fold_strip_sign_ops (arg0);
10253 if (tem != NULL_TREE)
10255 tem = fold_convert (type, tem);
10256 return fold_build2 (MULT_EXPR, type, tem, tem);
10260 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10261 This is not the same for NaNs or if signed zeros are
10262 involved. */
10263 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10264 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10265 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10266 && TREE_CODE (arg1) == COMPLEX_CST
10267 && real_zerop (TREE_REALPART (arg1)))
10269 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10270 if (real_onep (TREE_IMAGPART (arg1)))
10271 return fold_build2 (COMPLEX_EXPR, type,
10272 negate_expr (fold_build1 (IMAGPART_EXPR,
10273 rtype, arg0)),
10274 fold_build1 (REALPART_EXPR, rtype, arg0));
10275 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10276 return fold_build2 (COMPLEX_EXPR, type,
10277 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10278 negate_expr (fold_build1 (REALPART_EXPR,
10279 rtype, arg0)));
10282 /* Optimize z * conj(z) for floating point complex numbers.
10283 Guarded by flag_unsafe_math_optimizations as non-finite
10284 imaginary components don't produce scalar results. */
10285 if (flag_unsafe_math_optimizations
10286 && TREE_CODE (arg0) == CONJ_EXPR
10287 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10288 return fold_mult_zconjz (type, arg1);
10289 if (flag_unsafe_math_optimizations
10290 && TREE_CODE (arg1) == CONJ_EXPR
10291 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10292 return fold_mult_zconjz (type, arg0);
10294 if (flag_unsafe_math_optimizations)
10296 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10297 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10299 /* Optimizations of root(...)*root(...). */
10300 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10302 tree rootfn, arg;
10303 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10304 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10306 /* Optimize sqrt(x)*sqrt(x) as x. */
10307 if (BUILTIN_SQRT_P (fcode0)
10308 && operand_equal_p (arg00, arg10, 0)
10309 && ! HONOR_SNANS (TYPE_MODE (type)))
10310 return arg00;
10312 /* Optimize root(x)*root(y) as root(x*y). */
10313 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10314 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10315 return build_call_expr (rootfn, 1, arg);
10318 /* Optimize expN(x)*expN(y) as expN(x+y). */
10319 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10321 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10322 tree arg = fold_build2 (PLUS_EXPR, type,
10323 CALL_EXPR_ARG (arg0, 0),
10324 CALL_EXPR_ARG (arg1, 0));
10325 return build_call_expr (expfn, 1, arg);
10328 /* Optimizations of pow(...)*pow(...). */
10329 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10330 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10331 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10333 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10334 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10335 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10336 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10338 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10339 if (operand_equal_p (arg01, arg11, 0))
10341 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10342 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10343 return build_call_expr (powfn, 2, arg, arg01);
10346 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10347 if (operand_equal_p (arg00, arg10, 0))
10349 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10350 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10351 return build_call_expr (powfn, 2, arg00, arg);
10355 /* Optimize tan(x)*cos(x) as sin(x). */
10356 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10357 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10358 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10359 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10360 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10361 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10362 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10363 CALL_EXPR_ARG (arg1, 0), 0))
10365 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10367 if (sinfn != NULL_TREE)
10368 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10371 /* Optimize x*pow(x,c) as pow(x,c+1). */
10372 if (fcode1 == BUILT_IN_POW
10373 || fcode1 == BUILT_IN_POWF
10374 || fcode1 == BUILT_IN_POWL)
10376 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10377 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10378 if (TREE_CODE (arg11) == REAL_CST
10379 && !TREE_OVERFLOW (arg11)
10380 && operand_equal_p (arg0, arg10, 0))
10382 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10383 REAL_VALUE_TYPE c;
10384 tree arg;
10386 c = TREE_REAL_CST (arg11);
10387 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10388 arg = build_real (type, c);
10389 return build_call_expr (powfn, 2, arg0, arg);
10393 /* Optimize pow(x,c)*x as pow(x,c+1). */
10394 if (fcode0 == BUILT_IN_POW
10395 || fcode0 == BUILT_IN_POWF
10396 || fcode0 == BUILT_IN_POWL)
10398 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10399 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10400 if (TREE_CODE (arg01) == REAL_CST
10401 && !TREE_OVERFLOW (arg01)
10402 && operand_equal_p (arg1, arg00, 0))
10404 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10405 REAL_VALUE_TYPE c;
10406 tree arg;
10408 c = TREE_REAL_CST (arg01);
10409 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10410 arg = build_real (type, c);
10411 return build_call_expr (powfn, 2, arg1, arg);
10415 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10416 if (optimize_function_for_speed_p (cfun)
10417 && operand_equal_p (arg0, arg1, 0))
10419 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10421 if (powfn)
10423 tree arg = build_real (type, dconst2);
10424 return build_call_expr (powfn, 2, arg0, arg);
10429 goto associate;
10431 case BIT_IOR_EXPR:
10432 bit_ior:
10433 if (integer_all_onesp (arg1))
10434 return omit_one_operand (type, arg1, arg0);
10435 if (integer_zerop (arg1))
10436 return non_lvalue (fold_convert (type, arg0));
10437 if (operand_equal_p (arg0, arg1, 0))
10438 return non_lvalue (fold_convert (type, arg0));
10440 /* ~X | X is -1. */
10441 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10442 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10444 t1 = fold_convert (type, integer_zero_node);
10445 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10446 return omit_one_operand (type, t1, arg1);
10449 /* X | ~X is -1. */
10450 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10451 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10453 t1 = fold_convert (type, integer_zero_node);
10454 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10455 return omit_one_operand (type, t1, arg0);
10458 /* Canonicalize (X & C1) | C2. */
10459 if (TREE_CODE (arg0) == BIT_AND_EXPR
10460 && TREE_CODE (arg1) == INTEGER_CST
10461 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10463 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10464 int width = TYPE_PRECISION (type), w;
10465 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10466 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10467 hi2 = TREE_INT_CST_HIGH (arg1);
10468 lo2 = TREE_INT_CST_LOW (arg1);
10470 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10471 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10472 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10474 if (width > HOST_BITS_PER_WIDE_INT)
10476 mhi = (unsigned HOST_WIDE_INT) -1
10477 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10478 mlo = -1;
10480 else
10482 mhi = 0;
10483 mlo = (unsigned HOST_WIDE_INT) -1
10484 >> (HOST_BITS_PER_WIDE_INT - width);
10487 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10488 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10489 return fold_build2 (BIT_IOR_EXPR, type,
10490 TREE_OPERAND (arg0, 0), arg1);
10492 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10493 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10494 mode which allows further optimizations. */
10495 hi1 &= mhi;
10496 lo1 &= mlo;
10497 hi2 &= mhi;
10498 lo2 &= mlo;
10499 hi3 = hi1 & ~hi2;
10500 lo3 = lo1 & ~lo2;
10501 for (w = BITS_PER_UNIT;
10502 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10503 w <<= 1)
10505 unsigned HOST_WIDE_INT mask
10506 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10507 if (((lo1 | lo2) & mask) == mask
10508 && (lo1 & ~mask) == 0 && hi1 == 0)
10510 hi3 = 0;
10511 lo3 = mask;
10512 break;
10515 if (hi3 != hi1 || lo3 != lo1)
10516 return fold_build2 (BIT_IOR_EXPR, type,
10517 fold_build2 (BIT_AND_EXPR, type,
10518 TREE_OPERAND (arg0, 0),
10519 build_int_cst_wide (type,
10520 lo3, hi3)),
10521 arg1);
10524 /* (X & Y) | Y is (X, Y). */
10525 if (TREE_CODE (arg0) == BIT_AND_EXPR
10526 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10527 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10528 /* (X & Y) | X is (Y, X). */
10529 if (TREE_CODE (arg0) == BIT_AND_EXPR
10530 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10531 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10532 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10533 /* X | (X & Y) is (Y, X). */
10534 if (TREE_CODE (arg1) == BIT_AND_EXPR
10535 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10536 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10537 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10538 /* X | (Y & X) is (Y, X). */
10539 if (TREE_CODE (arg1) == BIT_AND_EXPR
10540 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10541 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10542 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10544 t1 = distribute_bit_expr (code, type, arg0, arg1);
10545 if (t1 != NULL_TREE)
10546 return t1;
10548 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10550 This results in more efficient code for machines without a NAND
10551 instruction. Combine will canonicalize to the first form
10552 which will allow use of NAND instructions provided by the
10553 backend if they exist. */
10554 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10555 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10557 return fold_build1 (BIT_NOT_EXPR, type,
10558 build2 (BIT_AND_EXPR, type,
10559 fold_convert (type,
10560 TREE_OPERAND (arg0, 0)),
10561 fold_convert (type,
10562 TREE_OPERAND (arg1, 0))));
10565 /* See if this can be simplified into a rotate first. If that
10566 is unsuccessful continue in the association code. */
10567 goto bit_rotate;
10569 case BIT_XOR_EXPR:
10570 if (integer_zerop (arg1))
10571 return non_lvalue (fold_convert (type, arg0));
10572 if (integer_all_onesp (arg1))
10573 return fold_build1 (BIT_NOT_EXPR, type, op0);
10574 if (operand_equal_p (arg0, arg1, 0))
10575 return omit_one_operand (type, integer_zero_node, arg0);
10577 /* ~X ^ X is -1. */
10578 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10579 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10581 t1 = fold_convert (type, integer_zero_node);
10582 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10583 return omit_one_operand (type, t1, arg1);
10586 /* X ^ ~X is -1. */
10587 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10588 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10590 t1 = fold_convert (type, integer_zero_node);
10591 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10592 return omit_one_operand (type, t1, arg0);
10595 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10596 with a constant, and the two constants have no bits in common,
10597 we should treat this as a BIT_IOR_EXPR since this may produce more
10598 simplifications. */
10599 if (TREE_CODE (arg0) == BIT_AND_EXPR
10600 && TREE_CODE (arg1) == BIT_AND_EXPR
10601 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10602 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10603 && integer_zerop (const_binop (BIT_AND_EXPR,
10604 TREE_OPERAND (arg0, 1),
10605 TREE_OPERAND (arg1, 1), 0)))
10607 code = BIT_IOR_EXPR;
10608 goto bit_ior;
10611 /* (X | Y) ^ X -> Y & ~ X*/
10612 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10613 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10615 tree t2 = TREE_OPERAND (arg0, 1);
10616 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10617 arg1);
10618 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10619 fold_convert (type, t1));
10620 return t1;
10623 /* (Y | X) ^ X -> Y & ~ X*/
10624 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10625 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10627 tree t2 = TREE_OPERAND (arg0, 0);
10628 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10629 arg1);
10630 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10631 fold_convert (type, t1));
10632 return t1;
10635 /* X ^ (X | Y) -> Y & ~ X*/
10636 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10637 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10639 tree t2 = TREE_OPERAND (arg1, 1);
10640 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10641 arg0);
10642 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10643 fold_convert (type, t1));
10644 return t1;
10647 /* X ^ (Y | X) -> Y & ~ X*/
10648 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10649 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10651 tree t2 = TREE_OPERAND (arg1, 0);
10652 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10653 arg0);
10654 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10655 fold_convert (type, t1));
10656 return t1;
10659 /* Convert ~X ^ ~Y to X ^ Y. */
10660 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10661 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10662 return fold_build2 (code, type,
10663 fold_convert (type, TREE_OPERAND (arg0, 0)),
10664 fold_convert (type, TREE_OPERAND (arg1, 0)));
10666 /* Convert ~X ^ C to X ^ ~C. */
10667 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10668 && TREE_CODE (arg1) == INTEGER_CST)
10669 return fold_build2 (code, type,
10670 fold_convert (type, TREE_OPERAND (arg0, 0)),
10671 fold_build1 (BIT_NOT_EXPR, type, arg1));
10673 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10674 if (TREE_CODE (arg0) == BIT_AND_EXPR
10675 && integer_onep (TREE_OPERAND (arg0, 1))
10676 && integer_onep (arg1))
10677 return fold_build2 (EQ_EXPR, type, arg0,
10678 build_int_cst (TREE_TYPE (arg0), 0));
10680 /* Fold (X & Y) ^ Y as ~X & Y. */
10681 if (TREE_CODE (arg0) == BIT_AND_EXPR
10682 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10684 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10685 return fold_build2 (BIT_AND_EXPR, type,
10686 fold_build1 (BIT_NOT_EXPR, type, tem),
10687 fold_convert (type, arg1));
10689 /* Fold (X & Y) ^ X as ~Y & X. */
10690 if (TREE_CODE (arg0) == BIT_AND_EXPR
10691 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10692 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10694 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10695 return fold_build2 (BIT_AND_EXPR, type,
10696 fold_build1 (BIT_NOT_EXPR, type, tem),
10697 fold_convert (type, arg1));
10699 /* Fold X ^ (X & Y) as X & ~Y. */
10700 if (TREE_CODE (arg1) == BIT_AND_EXPR
10701 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10703 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10704 return fold_build2 (BIT_AND_EXPR, type,
10705 fold_convert (type, arg0),
10706 fold_build1 (BIT_NOT_EXPR, type, tem));
10708 /* Fold X ^ (Y & X) as ~Y & X. */
10709 if (TREE_CODE (arg1) == BIT_AND_EXPR
10710 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10711 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10713 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10714 return fold_build2 (BIT_AND_EXPR, type,
10715 fold_build1 (BIT_NOT_EXPR, type, tem),
10716 fold_convert (type, arg0));
10719 /* See if this can be simplified into a rotate first. If that
10720 is unsuccessful continue in the association code. */
10721 goto bit_rotate;
10723 case BIT_AND_EXPR:
10724 if (integer_all_onesp (arg1))
10725 return non_lvalue (fold_convert (type, arg0));
10726 if (integer_zerop (arg1))
10727 return omit_one_operand (type, arg1, arg0);
10728 if (operand_equal_p (arg0, arg1, 0))
10729 return non_lvalue (fold_convert (type, arg0));
10731 /* ~X & X is always zero. */
10732 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10733 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10734 return omit_one_operand (type, integer_zero_node, arg1);
10736 /* X & ~X is always zero. */
10737 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10738 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10739 return omit_one_operand (type, integer_zero_node, arg0);
10741 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10742 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10743 && TREE_CODE (arg1) == INTEGER_CST
10744 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10746 tree tmp1 = fold_convert (type, arg1);
10747 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
10748 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
10749 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
10750 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
10751 return fold_convert (type,
10752 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
10755 /* (X | Y) & Y is (X, Y). */
10756 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10757 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10758 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10759 /* (X | Y) & X is (Y, X). */
10760 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10761 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10762 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10763 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10764 /* X & (X | Y) is (Y, X). */
10765 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10766 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10767 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10768 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10769 /* X & (Y | X) is (Y, X). */
10770 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10771 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10772 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10773 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10775 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10776 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10777 && integer_onep (TREE_OPERAND (arg0, 1))
10778 && integer_onep (arg1))
10780 tem = TREE_OPERAND (arg0, 0);
10781 return fold_build2 (EQ_EXPR, type,
10782 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10783 build_int_cst (TREE_TYPE (tem), 1)),
10784 build_int_cst (TREE_TYPE (tem), 0));
10786 /* Fold ~X & 1 as (X & 1) == 0. */
10787 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10788 && integer_onep (arg1))
10790 tem = TREE_OPERAND (arg0, 0);
10791 return fold_build2 (EQ_EXPR, type,
10792 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10793 build_int_cst (TREE_TYPE (tem), 1)),
10794 build_int_cst (TREE_TYPE (tem), 0));
10797 /* Fold (X ^ Y) & Y as ~X & Y. */
10798 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10799 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10801 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10802 return fold_build2 (BIT_AND_EXPR, type,
10803 fold_build1 (BIT_NOT_EXPR, type, tem),
10804 fold_convert (type, arg1));
10806 /* Fold (X ^ Y) & X as ~Y & X. */
10807 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10808 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10809 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10811 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10812 return fold_build2 (BIT_AND_EXPR, type,
10813 fold_build1 (BIT_NOT_EXPR, type, tem),
10814 fold_convert (type, arg1));
10816 /* Fold X & (X ^ Y) as X & ~Y. */
10817 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10818 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10820 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10821 return fold_build2 (BIT_AND_EXPR, type,
10822 fold_convert (type, arg0),
10823 fold_build1 (BIT_NOT_EXPR, type, tem));
10825 /* Fold X & (Y ^ X) as ~Y & X. */
10826 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10827 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10828 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10830 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10831 return fold_build2 (BIT_AND_EXPR, type,
10832 fold_build1 (BIT_NOT_EXPR, type, tem),
10833 fold_convert (type, arg0));
10836 t1 = distribute_bit_expr (code, type, arg0, arg1);
10837 if (t1 != NULL_TREE)
10838 return t1;
10839 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10840 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10841 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10843 unsigned int prec
10844 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10846 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10847 && (~TREE_INT_CST_LOW (arg1)
10848 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10849 return fold_convert (type, TREE_OPERAND (arg0, 0));
10852 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10854 This results in more efficient code for machines without a NOR
10855 instruction. Combine will canonicalize to the first form
10856 which will allow use of NOR instructions provided by the
10857 backend if they exist. */
10858 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10859 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10861 return fold_build1 (BIT_NOT_EXPR, type,
10862 build2 (BIT_IOR_EXPR, type,
10863 fold_convert (type,
10864 TREE_OPERAND (arg0, 0)),
10865 fold_convert (type,
10866 TREE_OPERAND (arg1, 0))));
10869 /* If arg0 is derived from the address of an object or function, we may
10870 be able to fold this expression using the object or function's
10871 alignment. */
10872 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10874 unsigned HOST_WIDE_INT modulus, residue;
10875 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10877 modulus = get_pointer_modulus_and_residue (arg0, &residue);
10879 /* This works because modulus is a power of 2. If this weren't the
10880 case, we'd have to replace it by its greatest power-of-2
10881 divisor: modulus & -modulus. */
10882 if (low < modulus)
10883 return build_int_cst (type, residue & low);
10886 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
10887 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
10888 if the new mask might be further optimized. */
10889 if ((TREE_CODE (arg0) == LSHIFT_EXPR
10890 || TREE_CODE (arg0) == RSHIFT_EXPR)
10891 && host_integerp (TREE_OPERAND (arg0, 1), 1)
10892 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
10893 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
10894 < TYPE_PRECISION (TREE_TYPE (arg0))
10895 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
10896 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
10898 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
10899 unsigned HOST_WIDE_INT mask
10900 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
10901 unsigned HOST_WIDE_INT newmask, zerobits = 0;
10902 tree shift_type = TREE_TYPE (arg0);
10904 if (TREE_CODE (arg0) == LSHIFT_EXPR)
10905 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
10906 else if (TREE_CODE (arg0) == RSHIFT_EXPR
10907 && TYPE_PRECISION (TREE_TYPE (arg0))
10908 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
10910 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
10911 tree arg00 = TREE_OPERAND (arg0, 0);
10912 /* See if more bits can be proven as zero because of
10913 zero extension. */
10914 if (TREE_CODE (arg00) == NOP_EXPR
10915 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
10917 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
10918 if (TYPE_PRECISION (inner_type)
10919 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
10920 && TYPE_PRECISION (inner_type) < prec)
10922 prec = TYPE_PRECISION (inner_type);
10923 /* See if we can shorten the right shift. */
10924 if (shiftc < prec)
10925 shift_type = inner_type;
10928 zerobits = ~(unsigned HOST_WIDE_INT) 0;
10929 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
10930 zerobits <<= prec - shiftc;
10931 /* For arithmetic shift if sign bit could be set, zerobits
10932 can contain actually sign bits, so no transformation is
10933 possible, unless MASK masks them all away. In that
10934 case the shift needs to be converted into logical shift. */
10935 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
10936 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
10938 if ((mask & zerobits) == 0)
10939 shift_type = unsigned_type_for (TREE_TYPE (arg0));
10940 else
10941 zerobits = 0;
10945 /* ((X << 16) & 0xff00) is (X, 0). */
10946 if ((mask & zerobits) == mask)
10947 return omit_one_operand (type, build_int_cst (type, 0), arg0);
10949 newmask = mask | zerobits;
10950 if (newmask != mask && (newmask & (newmask + 1)) == 0)
10952 unsigned int prec;
10954 /* Only do the transformation if NEWMASK is some integer
10955 mode's mask. */
10956 for (prec = BITS_PER_UNIT;
10957 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
10958 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
10959 break;
10960 if (prec < HOST_BITS_PER_WIDE_INT
10961 || newmask == ~(unsigned HOST_WIDE_INT) 0)
10963 if (shift_type != TREE_TYPE (arg0))
10965 tem = fold_build2 (TREE_CODE (arg0), shift_type,
10966 fold_convert (shift_type,
10967 TREE_OPERAND (arg0, 0)),
10968 TREE_OPERAND (arg0, 1));
10969 tem = fold_convert (type, tem);
10971 else
10972 tem = op0;
10973 return fold_build2 (BIT_AND_EXPR, type, tem,
10974 build_int_cst_type (TREE_TYPE (op1),
10975 newmask));
10980 goto associate;
10982 case RDIV_EXPR:
10983 /* Don't touch a floating-point divide by zero unless the mode
10984 of the constant can represent infinity. */
10985 if (TREE_CODE (arg1) == REAL_CST
10986 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10987 && real_zerop (arg1))
10988 return NULL_TREE;
10990 /* Optimize A / A to 1.0 if we don't care about
10991 NaNs or Infinities. Skip the transformation
10992 for non-real operands. */
10993 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10994 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10995 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10996 && operand_equal_p (arg0, arg1, 0))
10998 tree r = build_real (TREE_TYPE (arg0), dconst1);
11000 return omit_two_operands (type, r, arg0, arg1);
11003 /* The complex version of the above A / A optimization. */
11004 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11005 && operand_equal_p (arg0, arg1, 0))
11007 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11008 if (! HONOR_NANS (TYPE_MODE (elem_type))
11009 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11011 tree r = build_real (elem_type, dconst1);
11012 /* omit_two_operands will call fold_convert for us. */
11013 return omit_two_operands (type, r, arg0, arg1);
11017 /* (-A) / (-B) -> A / B */
11018 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11019 return fold_build2 (RDIV_EXPR, type,
11020 TREE_OPERAND (arg0, 0),
11021 negate_expr (arg1));
11022 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11023 return fold_build2 (RDIV_EXPR, type,
11024 negate_expr (arg0),
11025 TREE_OPERAND (arg1, 0));
11027 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11028 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11029 && real_onep (arg1))
11030 return non_lvalue (fold_convert (type, arg0));
11032 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11033 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11034 && real_minus_onep (arg1))
11035 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11037 /* If ARG1 is a constant, we can convert this to a multiply by the
11038 reciprocal. This does not have the same rounding properties,
11039 so only do this if -freciprocal-math. We can actually
11040 always safely do it if ARG1 is a power of two, but it's hard to
11041 tell if it is or not in a portable manner. */
11042 if (TREE_CODE (arg1) == REAL_CST)
11044 if (flag_reciprocal_math
11045 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11046 arg1, 0)))
11047 return fold_build2 (MULT_EXPR, type, arg0, tem);
11048 /* Find the reciprocal if optimizing and the result is exact. */
11049 if (optimize)
11051 REAL_VALUE_TYPE r;
11052 r = TREE_REAL_CST (arg1);
11053 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11055 tem = build_real (type, r);
11056 return fold_build2 (MULT_EXPR, type,
11057 fold_convert (type, arg0), tem);
11061 /* Convert A/B/C to A/(B*C). */
11062 if (flag_reciprocal_math
11063 && TREE_CODE (arg0) == RDIV_EXPR)
11064 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11065 fold_build2 (MULT_EXPR, type,
11066 TREE_OPERAND (arg0, 1), arg1));
11068 /* Convert A/(B/C) to (A/B)*C. */
11069 if (flag_reciprocal_math
11070 && TREE_CODE (arg1) == RDIV_EXPR)
11071 return fold_build2 (MULT_EXPR, type,
11072 fold_build2 (RDIV_EXPR, type, arg0,
11073 TREE_OPERAND (arg1, 0)),
11074 TREE_OPERAND (arg1, 1));
11076 /* Convert C1/(X*C2) into (C1/C2)/X. */
11077 if (flag_reciprocal_math
11078 && TREE_CODE (arg1) == MULT_EXPR
11079 && TREE_CODE (arg0) == REAL_CST
11080 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11082 tree tem = const_binop (RDIV_EXPR, arg0,
11083 TREE_OPERAND (arg1, 1), 0);
11084 if (tem)
11085 return fold_build2 (RDIV_EXPR, type, tem,
11086 TREE_OPERAND (arg1, 0));
11089 if (flag_unsafe_math_optimizations)
11091 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11092 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11094 /* Optimize sin(x)/cos(x) as tan(x). */
11095 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11096 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11097 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11098 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11099 CALL_EXPR_ARG (arg1, 0), 0))
11101 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11103 if (tanfn != NULL_TREE)
11104 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11107 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11108 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11109 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11110 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11111 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11112 CALL_EXPR_ARG (arg1, 0), 0))
11114 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11116 if (tanfn != NULL_TREE)
11118 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11119 return fold_build2 (RDIV_EXPR, type,
11120 build_real (type, dconst1), tmp);
11124 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11125 NaNs or Infinities. */
11126 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11127 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11128 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11130 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11131 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11133 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11134 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11135 && operand_equal_p (arg00, arg01, 0))
11137 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11139 if (cosfn != NULL_TREE)
11140 return build_call_expr (cosfn, 1, arg00);
11144 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11145 NaNs or Infinities. */
11146 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11147 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11148 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11150 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11151 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11153 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11154 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11155 && operand_equal_p (arg00, arg01, 0))
11157 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11159 if (cosfn != NULL_TREE)
11161 tree tmp = build_call_expr (cosfn, 1, arg00);
11162 return fold_build2 (RDIV_EXPR, type,
11163 build_real (type, dconst1),
11164 tmp);
11169 /* Optimize pow(x,c)/x as pow(x,c-1). */
11170 if (fcode0 == BUILT_IN_POW
11171 || fcode0 == BUILT_IN_POWF
11172 || fcode0 == BUILT_IN_POWL)
11174 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11175 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11176 if (TREE_CODE (arg01) == REAL_CST
11177 && !TREE_OVERFLOW (arg01)
11178 && operand_equal_p (arg1, arg00, 0))
11180 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11181 REAL_VALUE_TYPE c;
11182 tree arg;
11184 c = TREE_REAL_CST (arg01);
11185 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11186 arg = build_real (type, c);
11187 return build_call_expr (powfn, 2, arg1, arg);
11191 /* Optimize a/root(b/c) into a*root(c/b). */
11192 if (BUILTIN_ROOT_P (fcode1))
11194 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11196 if (TREE_CODE (rootarg) == RDIV_EXPR)
11198 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11199 tree b = TREE_OPERAND (rootarg, 0);
11200 tree c = TREE_OPERAND (rootarg, 1);
11202 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11204 tmp = build_call_expr (rootfn, 1, tmp);
11205 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11209 /* Optimize x/expN(y) into x*expN(-y). */
11210 if (BUILTIN_EXPONENT_P (fcode1))
11212 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11213 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11214 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11215 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11218 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11219 if (fcode1 == BUILT_IN_POW
11220 || fcode1 == BUILT_IN_POWF
11221 || fcode1 == BUILT_IN_POWL)
11223 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11224 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11225 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11226 tree neg11 = fold_convert (type, negate_expr (arg11));
11227 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11228 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11231 return NULL_TREE;
11233 case TRUNC_DIV_EXPR:
11234 case FLOOR_DIV_EXPR:
11235 /* Simplify A / (B << N) where A and B are positive and B is
11236 a power of 2, to A >> (N + log2(B)). */
11237 strict_overflow_p = false;
11238 if (TREE_CODE (arg1) == LSHIFT_EXPR
11239 && (TYPE_UNSIGNED (type)
11240 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11242 tree sval = TREE_OPERAND (arg1, 0);
11243 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11245 tree sh_cnt = TREE_OPERAND (arg1, 1);
11246 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11248 if (strict_overflow_p)
11249 fold_overflow_warning (("assuming signed overflow does not "
11250 "occur when simplifying A / (B << N)"),
11251 WARN_STRICT_OVERFLOW_MISC);
11253 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11254 sh_cnt, build_int_cst (NULL_TREE, pow2));
11255 return fold_build2 (RSHIFT_EXPR, type,
11256 fold_convert (type, arg0), sh_cnt);
11260 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11261 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11262 if (INTEGRAL_TYPE_P (type)
11263 && TYPE_UNSIGNED (type)
11264 && code == FLOOR_DIV_EXPR)
11265 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11267 /* Fall thru */
11269 case ROUND_DIV_EXPR:
11270 case CEIL_DIV_EXPR:
11271 case EXACT_DIV_EXPR:
11272 if (integer_onep (arg1))
11273 return non_lvalue (fold_convert (type, arg0));
11274 if (integer_zerop (arg1))
11275 return NULL_TREE;
11276 /* X / -1 is -X. */
11277 if (!TYPE_UNSIGNED (type)
11278 && TREE_CODE (arg1) == INTEGER_CST
11279 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11280 && TREE_INT_CST_HIGH (arg1) == -1)
11281 return fold_convert (type, negate_expr (arg0));
11283 /* Convert -A / -B to A / B when the type is signed and overflow is
11284 undefined. */
11285 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11286 && TREE_CODE (arg0) == NEGATE_EXPR
11287 && negate_expr_p (arg1))
11289 if (INTEGRAL_TYPE_P (type))
11290 fold_overflow_warning (("assuming signed overflow does not occur "
11291 "when distributing negation across "
11292 "division"),
11293 WARN_STRICT_OVERFLOW_MISC);
11294 return fold_build2 (code, type,
11295 fold_convert (type, TREE_OPERAND (arg0, 0)),
11296 negate_expr (arg1));
11298 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11299 && TREE_CODE (arg1) == NEGATE_EXPR
11300 && negate_expr_p (arg0))
11302 if (INTEGRAL_TYPE_P (type))
11303 fold_overflow_warning (("assuming signed overflow does not occur "
11304 "when distributing negation across "
11305 "division"),
11306 WARN_STRICT_OVERFLOW_MISC);
11307 return fold_build2 (code, type, negate_expr (arg0),
11308 TREE_OPERAND (arg1, 0));
11311 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11312 operation, EXACT_DIV_EXPR.
11314 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11315 At one time others generated faster code, it's not clear if they do
11316 after the last round to changes to the DIV code in expmed.c. */
11317 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11318 && multiple_of_p (type, arg0, arg1))
11319 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11321 strict_overflow_p = false;
11322 if (TREE_CODE (arg1) == INTEGER_CST
11323 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11324 &strict_overflow_p)))
11326 if (strict_overflow_p)
11327 fold_overflow_warning (("assuming signed overflow does not occur "
11328 "when simplifying division"),
11329 WARN_STRICT_OVERFLOW_MISC);
11330 return fold_convert (type, tem);
11333 return NULL_TREE;
11335 case CEIL_MOD_EXPR:
11336 case FLOOR_MOD_EXPR:
11337 case ROUND_MOD_EXPR:
11338 case TRUNC_MOD_EXPR:
11339 /* X % 1 is always zero, but be sure to preserve any side
11340 effects in X. */
11341 if (integer_onep (arg1))
11342 return omit_one_operand (type, integer_zero_node, arg0);
11344 /* X % 0, return X % 0 unchanged so that we can get the
11345 proper warnings and errors. */
11346 if (integer_zerop (arg1))
11347 return NULL_TREE;
11349 /* 0 % X is always zero, but be sure to preserve any side
11350 effects in X. Place this after checking for X == 0. */
11351 if (integer_zerop (arg0))
11352 return omit_one_operand (type, integer_zero_node, arg1);
11354 /* X % -1 is zero. */
11355 if (!TYPE_UNSIGNED (type)
11356 && TREE_CODE (arg1) == INTEGER_CST
11357 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11358 && TREE_INT_CST_HIGH (arg1) == -1)
11359 return omit_one_operand (type, integer_zero_node, arg0);
11361 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11362 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11363 strict_overflow_p = false;
11364 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11365 && (TYPE_UNSIGNED (type)
11366 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11368 tree c = arg1;
11369 /* Also optimize A % (C << N) where C is a power of 2,
11370 to A & ((C << N) - 1). */
11371 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11372 c = TREE_OPERAND (arg1, 0);
11374 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11376 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11377 build_int_cst (TREE_TYPE (arg1), 1));
11378 if (strict_overflow_p)
11379 fold_overflow_warning (("assuming signed overflow does not "
11380 "occur when simplifying "
11381 "X % (power of two)"),
11382 WARN_STRICT_OVERFLOW_MISC);
11383 return fold_build2 (BIT_AND_EXPR, type,
11384 fold_convert (type, arg0),
11385 fold_convert (type, mask));
11389 /* X % -C is the same as X % C. */
11390 if (code == TRUNC_MOD_EXPR
11391 && !TYPE_UNSIGNED (type)
11392 && TREE_CODE (arg1) == INTEGER_CST
11393 && !TREE_OVERFLOW (arg1)
11394 && TREE_INT_CST_HIGH (arg1) < 0
11395 && !TYPE_OVERFLOW_TRAPS (type)
11396 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11397 && !sign_bit_p (arg1, arg1))
11398 return fold_build2 (code, type, fold_convert (type, arg0),
11399 fold_convert (type, negate_expr (arg1)));
11401 /* X % -Y is the same as X % Y. */
11402 if (code == TRUNC_MOD_EXPR
11403 && !TYPE_UNSIGNED (type)
11404 && TREE_CODE (arg1) == NEGATE_EXPR
11405 && !TYPE_OVERFLOW_TRAPS (type))
11406 return fold_build2 (code, type, fold_convert (type, arg0),
11407 fold_convert (type, TREE_OPERAND (arg1, 0)));
11409 if (TREE_CODE (arg1) == INTEGER_CST
11410 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11411 &strict_overflow_p)))
11413 if (strict_overflow_p)
11414 fold_overflow_warning (("assuming signed overflow does not occur "
11415 "when simplifying modulus"),
11416 WARN_STRICT_OVERFLOW_MISC);
11417 return fold_convert (type, tem);
11420 return NULL_TREE;
11422 case LROTATE_EXPR:
11423 case RROTATE_EXPR:
11424 if (integer_all_onesp (arg0))
11425 return omit_one_operand (type, arg0, arg1);
11426 goto shift;
11428 case RSHIFT_EXPR:
11429 /* Optimize -1 >> x for arithmetic right shifts. */
11430 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11431 return omit_one_operand (type, arg0, arg1);
11432 /* ... fall through ... */
11434 case LSHIFT_EXPR:
11435 shift:
11436 if (integer_zerop (arg1))
11437 return non_lvalue (fold_convert (type, arg0));
11438 if (integer_zerop (arg0))
11439 return omit_one_operand (type, arg0, arg1);
11441 /* Since negative shift count is not well-defined,
11442 don't try to compute it in the compiler. */
11443 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11444 return NULL_TREE;
11446 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11447 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11448 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11449 && host_integerp (TREE_OPERAND (arg0, 1), false)
11450 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11452 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11453 + TREE_INT_CST_LOW (arg1));
11455 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11456 being well defined. */
11457 if (low >= TYPE_PRECISION (type))
11459 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11460 low = low % TYPE_PRECISION (type);
11461 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11462 return build_int_cst (type, 0);
11463 else
11464 low = TYPE_PRECISION (type) - 1;
11467 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11468 build_int_cst (type, low));
11471 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11472 into x & ((unsigned)-1 >> c) for unsigned types. */
11473 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11474 || (TYPE_UNSIGNED (type)
11475 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11476 && host_integerp (arg1, false)
11477 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11478 && host_integerp (TREE_OPERAND (arg0, 1), false)
11479 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11481 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11482 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11483 tree lshift;
11484 tree arg00;
11486 if (low0 == low1)
11488 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11490 lshift = build_int_cst (type, -1);
11491 lshift = int_const_binop (code, lshift, arg1, 0);
11493 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11497 /* Rewrite an LROTATE_EXPR by a constant into an
11498 RROTATE_EXPR by a new constant. */
11499 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11501 tree tem = build_int_cst (TREE_TYPE (arg1),
11502 TYPE_PRECISION (type));
11503 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11504 return fold_build2 (RROTATE_EXPR, type, op0, tem);
11507 /* If we have a rotate of a bit operation with the rotate count and
11508 the second operand of the bit operation both constant,
11509 permute the two operations. */
11510 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11511 && (TREE_CODE (arg0) == BIT_AND_EXPR
11512 || TREE_CODE (arg0) == BIT_IOR_EXPR
11513 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11514 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11515 return fold_build2 (TREE_CODE (arg0), type,
11516 fold_build2 (code, type,
11517 TREE_OPERAND (arg0, 0), arg1),
11518 fold_build2 (code, type,
11519 TREE_OPERAND (arg0, 1), arg1));
11521 /* Two consecutive rotates adding up to the precision of the
11522 type can be ignored. */
11523 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11524 && TREE_CODE (arg0) == RROTATE_EXPR
11525 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11526 && TREE_INT_CST_HIGH (arg1) == 0
11527 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11528 && ((TREE_INT_CST_LOW (arg1)
11529 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11530 == (unsigned int) TYPE_PRECISION (type)))
11531 return TREE_OPERAND (arg0, 0);
11533 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11534 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11535 if the latter can be further optimized. */
11536 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11537 && TREE_CODE (arg0) == BIT_AND_EXPR
11538 && TREE_CODE (arg1) == INTEGER_CST
11539 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11541 tree mask = fold_build2 (code, type,
11542 fold_convert (type, TREE_OPERAND (arg0, 1)),
11543 arg1);
11544 tree shift = fold_build2 (code, type,
11545 fold_convert (type, TREE_OPERAND (arg0, 0)),
11546 arg1);
11547 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11548 if (tem)
11549 return tem;
11552 return NULL_TREE;
11554 case MIN_EXPR:
11555 if (operand_equal_p (arg0, arg1, 0))
11556 return omit_one_operand (type, arg0, arg1);
11557 if (INTEGRAL_TYPE_P (type)
11558 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11559 return omit_one_operand (type, arg1, arg0);
11560 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11561 if (tem)
11562 return tem;
11563 goto associate;
11565 case MAX_EXPR:
11566 if (operand_equal_p (arg0, arg1, 0))
11567 return omit_one_operand (type, arg0, arg1);
11568 if (INTEGRAL_TYPE_P (type)
11569 && TYPE_MAX_VALUE (type)
11570 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11571 return omit_one_operand (type, arg1, arg0);
11572 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11573 if (tem)
11574 return tem;
11575 goto associate;
11577 case TRUTH_ANDIF_EXPR:
11578 /* Note that the operands of this must be ints
11579 and their values must be 0 or 1.
11580 ("true" is a fixed value perhaps depending on the language.) */
11581 /* If first arg is constant zero, return it. */
11582 if (integer_zerop (arg0))
11583 return fold_convert (type, arg0);
11584 case TRUTH_AND_EXPR:
11585 /* If either arg is constant true, drop it. */
11586 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11587 return non_lvalue (fold_convert (type, arg1));
11588 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11589 /* Preserve sequence points. */
11590 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11591 return non_lvalue (fold_convert (type, arg0));
11592 /* If second arg is constant zero, result is zero, but first arg
11593 must be evaluated. */
11594 if (integer_zerop (arg1))
11595 return omit_one_operand (type, arg1, arg0);
11596 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11597 case will be handled here. */
11598 if (integer_zerop (arg0))
11599 return omit_one_operand (type, arg0, arg1);
11601 /* !X && X is always false. */
11602 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11603 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11604 return omit_one_operand (type, integer_zero_node, arg1);
11605 /* X && !X is always false. */
11606 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11607 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11608 return omit_one_operand (type, integer_zero_node, arg0);
11610 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11611 means A >= Y && A != MAX, but in this case we know that
11612 A < X <= MAX. */
11614 if (!TREE_SIDE_EFFECTS (arg0)
11615 && !TREE_SIDE_EFFECTS (arg1))
11617 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11618 if (tem && !operand_equal_p (tem, arg0, 0))
11619 return fold_build2 (code, type, tem, arg1);
11621 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11622 if (tem && !operand_equal_p (tem, arg1, 0))
11623 return fold_build2 (code, type, arg0, tem);
11626 truth_andor:
11627 /* We only do these simplifications if we are optimizing. */
11628 if (!optimize)
11629 return NULL_TREE;
11631 /* Check for things like (A || B) && (A || C). We can convert this
11632 to A || (B && C). Note that either operator can be any of the four
11633 truth and/or operations and the transformation will still be
11634 valid. Also note that we only care about order for the
11635 ANDIF and ORIF operators. If B contains side effects, this
11636 might change the truth-value of A. */
11637 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11638 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11639 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11640 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11641 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11642 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11644 tree a00 = TREE_OPERAND (arg0, 0);
11645 tree a01 = TREE_OPERAND (arg0, 1);
11646 tree a10 = TREE_OPERAND (arg1, 0);
11647 tree a11 = TREE_OPERAND (arg1, 1);
11648 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11649 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11650 && (code == TRUTH_AND_EXPR
11651 || code == TRUTH_OR_EXPR));
11653 if (operand_equal_p (a00, a10, 0))
11654 return fold_build2 (TREE_CODE (arg0), type, a00,
11655 fold_build2 (code, type, a01, a11));
11656 else if (commutative && operand_equal_p (a00, a11, 0))
11657 return fold_build2 (TREE_CODE (arg0), type, a00,
11658 fold_build2 (code, type, a01, a10));
11659 else if (commutative && operand_equal_p (a01, a10, 0))
11660 return fold_build2 (TREE_CODE (arg0), type, a01,
11661 fold_build2 (code, type, a00, a11));
11663 /* This case if tricky because we must either have commutative
11664 operators or else A10 must not have side-effects. */
11666 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11667 && operand_equal_p (a01, a11, 0))
11668 return fold_build2 (TREE_CODE (arg0), type,
11669 fold_build2 (code, type, a00, a10),
11670 a01);
11673 /* See if we can build a range comparison. */
11674 if (0 != (tem = fold_range_test (code, type, op0, op1)))
11675 return tem;
11677 /* Check for the possibility of merging component references. If our
11678 lhs is another similar operation, try to merge its rhs with our
11679 rhs. Then try to merge our lhs and rhs. */
11680 if (TREE_CODE (arg0) == code
11681 && 0 != (tem = fold_truthop (code, type,
11682 TREE_OPERAND (arg0, 1), arg1)))
11683 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11685 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11686 return tem;
11688 return NULL_TREE;
11690 case TRUTH_ORIF_EXPR:
11691 /* Note that the operands of this must be ints
11692 and their values must be 0 or true.
11693 ("true" is a fixed value perhaps depending on the language.) */
11694 /* If first arg is constant true, return it. */
11695 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11696 return fold_convert (type, arg0);
11697 case TRUTH_OR_EXPR:
11698 /* If either arg is constant zero, drop it. */
11699 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11700 return non_lvalue (fold_convert (type, arg1));
11701 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11702 /* Preserve sequence points. */
11703 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11704 return non_lvalue (fold_convert (type, arg0));
11705 /* If second arg is constant true, result is true, but we must
11706 evaluate first arg. */
11707 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11708 return omit_one_operand (type, arg1, arg0);
11709 /* Likewise for first arg, but note this only occurs here for
11710 TRUTH_OR_EXPR. */
11711 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11712 return omit_one_operand (type, arg0, arg1);
11714 /* !X || X is always true. */
11715 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11716 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11717 return omit_one_operand (type, integer_one_node, arg1);
11718 /* X || !X is always true. */
11719 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11720 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11721 return omit_one_operand (type, integer_one_node, arg0);
11723 goto truth_andor;
11725 case TRUTH_XOR_EXPR:
11726 /* If the second arg is constant zero, drop it. */
11727 if (integer_zerop (arg1))
11728 return non_lvalue (fold_convert (type, arg0));
11729 /* If the second arg is constant true, this is a logical inversion. */
11730 if (integer_onep (arg1))
11732 /* Only call invert_truthvalue if operand is a truth value. */
11733 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11734 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11735 else
11736 tem = invert_truthvalue (arg0);
11737 return non_lvalue (fold_convert (type, tem));
11739 /* Identical arguments cancel to zero. */
11740 if (operand_equal_p (arg0, arg1, 0))
11741 return omit_one_operand (type, integer_zero_node, arg0);
11743 /* !X ^ X is always true. */
11744 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11745 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11746 return omit_one_operand (type, integer_one_node, arg1);
11748 /* X ^ !X is always true. */
11749 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11750 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11751 return omit_one_operand (type, integer_one_node, arg0);
11753 return NULL_TREE;
11755 case EQ_EXPR:
11756 case NE_EXPR:
11757 tem = fold_comparison (code, type, op0, op1);
11758 if (tem != NULL_TREE)
11759 return tem;
11761 /* bool_var != 0 becomes bool_var. */
11762 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11763 && code == NE_EXPR)
11764 return non_lvalue (fold_convert (type, arg0));
11766 /* bool_var == 1 becomes bool_var. */
11767 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11768 && code == EQ_EXPR)
11769 return non_lvalue (fold_convert (type, arg0));
11771 /* bool_var != 1 becomes !bool_var. */
11772 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11773 && code == NE_EXPR)
11774 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11776 /* bool_var == 0 becomes !bool_var. */
11777 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11778 && code == EQ_EXPR)
11779 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11781 /* If this is an equality comparison of the address of two non-weak,
11782 unaliased symbols neither of which are extern (since we do not
11783 have access to attributes for externs), then we know the result. */
11784 if (TREE_CODE (arg0) == ADDR_EXPR
11785 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11786 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11787 && ! lookup_attribute ("alias",
11788 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11789 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11790 && TREE_CODE (arg1) == ADDR_EXPR
11791 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11792 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11793 && ! lookup_attribute ("alias",
11794 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11795 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11797 /* We know that we're looking at the address of two
11798 non-weak, unaliased, static _DECL nodes.
11800 It is both wasteful and incorrect to call operand_equal_p
11801 to compare the two ADDR_EXPR nodes. It is wasteful in that
11802 all we need to do is test pointer equality for the arguments
11803 to the two ADDR_EXPR nodes. It is incorrect to use
11804 operand_equal_p as that function is NOT equivalent to a
11805 C equality test. It can in fact return false for two
11806 objects which would test as equal using the C equality
11807 operator. */
11808 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11809 return constant_boolean_node (equal
11810 ? code == EQ_EXPR : code != EQ_EXPR,
11811 type);
11814 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11815 a MINUS_EXPR of a constant, we can convert it into a comparison with
11816 a revised constant as long as no overflow occurs. */
11817 if (TREE_CODE (arg1) == INTEGER_CST
11818 && (TREE_CODE (arg0) == PLUS_EXPR
11819 || TREE_CODE (arg0) == MINUS_EXPR)
11820 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11821 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11822 ? MINUS_EXPR : PLUS_EXPR,
11823 fold_convert (TREE_TYPE (arg0), arg1),
11824 TREE_OPERAND (arg0, 1), 0))
11825 && !TREE_OVERFLOW (tem))
11826 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11828 /* Similarly for a NEGATE_EXPR. */
11829 if (TREE_CODE (arg0) == NEGATE_EXPR
11830 && TREE_CODE (arg1) == INTEGER_CST
11831 && 0 != (tem = negate_expr (arg1))
11832 && TREE_CODE (tem) == INTEGER_CST
11833 && !TREE_OVERFLOW (tem))
11834 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11836 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11837 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11838 && TREE_CODE (arg1) == INTEGER_CST
11839 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11840 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11841 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11842 fold_convert (TREE_TYPE (arg0), arg1),
11843 TREE_OPERAND (arg0, 1)));
11845 /* Transform comparisons of the form X +- C CMP X. */
11846 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11847 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11848 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11849 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11850 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11852 tree cst = TREE_OPERAND (arg0, 1);
11854 if (code == EQ_EXPR
11855 && !integer_zerop (cst))
11856 return omit_two_operands (type, boolean_false_node,
11857 TREE_OPERAND (arg0, 0), arg1);
11858 else
11859 return omit_two_operands (type, boolean_true_node,
11860 TREE_OPERAND (arg0, 0), arg1);
11863 /* If we have X - Y == 0, we can convert that to X == Y and similarly
11864 for !=. Don't do this for ordered comparisons due to overflow. */
11865 if (TREE_CODE (arg0) == MINUS_EXPR
11866 && integer_zerop (arg1))
11867 return fold_build2 (code, type,
11868 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11870 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11871 if (TREE_CODE (arg0) == ABS_EXPR
11872 && (integer_zerop (arg1) || real_zerop (arg1)))
11873 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11875 /* If this is an EQ or NE comparison with zero and ARG0 is
11876 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11877 two operations, but the latter can be done in one less insn
11878 on machines that have only two-operand insns or on which a
11879 constant cannot be the first operand. */
11880 if (TREE_CODE (arg0) == BIT_AND_EXPR
11881 && integer_zerop (arg1))
11883 tree arg00 = TREE_OPERAND (arg0, 0);
11884 tree arg01 = TREE_OPERAND (arg0, 1);
11885 if (TREE_CODE (arg00) == LSHIFT_EXPR
11886 && integer_onep (TREE_OPERAND (arg00, 0)))
11888 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11889 arg01, TREE_OPERAND (arg00, 1));
11890 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11891 build_int_cst (TREE_TYPE (arg0), 1));
11892 return fold_build2 (code, type,
11893 fold_convert (TREE_TYPE (arg1), tem), arg1);
11895 else if (TREE_CODE (arg01) == LSHIFT_EXPR
11896 && integer_onep (TREE_OPERAND (arg01, 0)))
11898 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11899 arg00, TREE_OPERAND (arg01, 1));
11900 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11901 build_int_cst (TREE_TYPE (arg0), 1));
11902 return fold_build2 (code, type,
11903 fold_convert (TREE_TYPE (arg1), tem), arg1);
11907 /* If this is an NE or EQ comparison of zero against the result of a
11908 signed MOD operation whose second operand is a power of 2, make
11909 the MOD operation unsigned since it is simpler and equivalent. */
11910 if (integer_zerop (arg1)
11911 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11912 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11913 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11914 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11915 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11916 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11918 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11919 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11920 fold_convert (newtype,
11921 TREE_OPERAND (arg0, 0)),
11922 fold_convert (newtype,
11923 TREE_OPERAND (arg0, 1)));
11925 return fold_build2 (code, type, newmod,
11926 fold_convert (newtype, arg1));
11929 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11930 C1 is a valid shift constant, and C2 is a power of two, i.e.
11931 a single bit. */
11932 if (TREE_CODE (arg0) == BIT_AND_EXPR
11933 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11934 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11935 == INTEGER_CST
11936 && integer_pow2p (TREE_OPERAND (arg0, 1))
11937 && integer_zerop (arg1))
11939 tree itype = TREE_TYPE (arg0);
11940 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11941 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11943 /* Check for a valid shift count. */
11944 if (TREE_INT_CST_HIGH (arg001) == 0
11945 && TREE_INT_CST_LOW (arg001) < prec)
11947 tree arg01 = TREE_OPERAND (arg0, 1);
11948 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11949 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11950 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11951 can be rewritten as (X & (C2 << C1)) != 0. */
11952 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11954 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11955 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11956 return fold_build2 (code, type, tem, arg1);
11958 /* Otherwise, for signed (arithmetic) shifts,
11959 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11960 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11961 else if (!TYPE_UNSIGNED (itype))
11962 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11963 arg000, build_int_cst (itype, 0));
11964 /* Otherwise, of unsigned (logical) shifts,
11965 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11966 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11967 else
11968 return omit_one_operand (type,
11969 code == EQ_EXPR ? integer_one_node
11970 : integer_zero_node,
11971 arg000);
11975 /* If this is an NE comparison of zero with an AND of one, remove the
11976 comparison since the AND will give the correct value. */
11977 if (code == NE_EXPR
11978 && integer_zerop (arg1)
11979 && TREE_CODE (arg0) == BIT_AND_EXPR
11980 && integer_onep (TREE_OPERAND (arg0, 1)))
11981 return fold_convert (type, arg0);
11983 /* If we have (A & C) == C where C is a power of 2, convert this into
11984 (A & C) != 0. Similarly for NE_EXPR. */
11985 if (TREE_CODE (arg0) == BIT_AND_EXPR
11986 && integer_pow2p (TREE_OPERAND (arg0, 1))
11987 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11988 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11989 arg0, fold_convert (TREE_TYPE (arg0),
11990 integer_zero_node));
11992 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11993 bit, then fold the expression into A < 0 or A >= 0. */
11994 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11995 if (tem)
11996 return tem;
11998 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11999 Similarly for NE_EXPR. */
12000 if (TREE_CODE (arg0) == BIT_AND_EXPR
12001 && TREE_CODE (arg1) == INTEGER_CST
12002 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12004 tree notc = fold_build1 (BIT_NOT_EXPR,
12005 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12006 TREE_OPERAND (arg0, 1));
12007 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12008 arg1, notc);
12009 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12010 if (integer_nonzerop (dandnotc))
12011 return omit_one_operand (type, rslt, arg0);
12014 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12015 Similarly for NE_EXPR. */
12016 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12017 && TREE_CODE (arg1) == INTEGER_CST
12018 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12020 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12021 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12022 TREE_OPERAND (arg0, 1), notd);
12023 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12024 if (integer_nonzerop (candnotd))
12025 return omit_one_operand (type, rslt, arg0);
12028 /* Optimize comparisons of strlen vs zero to a compare of the
12029 first character of the string vs zero. To wit,
12030 strlen(ptr) == 0 => *ptr == 0
12031 strlen(ptr) != 0 => *ptr != 0
12032 Other cases should reduce to one of these two (or a constant)
12033 due to the return value of strlen being unsigned. */
12034 if (TREE_CODE (arg0) == CALL_EXPR
12035 && integer_zerop (arg1))
12037 tree fndecl = get_callee_fndecl (arg0);
12039 if (fndecl
12040 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12041 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12042 && call_expr_nargs (arg0) == 1
12043 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12045 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12046 return fold_build2 (code, type, iref,
12047 build_int_cst (TREE_TYPE (iref), 0));
12051 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12052 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12053 if (TREE_CODE (arg0) == RSHIFT_EXPR
12054 && integer_zerop (arg1)
12055 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12057 tree arg00 = TREE_OPERAND (arg0, 0);
12058 tree arg01 = TREE_OPERAND (arg0, 1);
12059 tree itype = TREE_TYPE (arg00);
12060 if (TREE_INT_CST_HIGH (arg01) == 0
12061 && TREE_INT_CST_LOW (arg01)
12062 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12064 if (TYPE_UNSIGNED (itype))
12066 itype = signed_type_for (itype);
12067 arg00 = fold_convert (itype, arg00);
12069 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12070 type, arg00, build_int_cst (itype, 0));
12074 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12075 if (integer_zerop (arg1)
12076 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12077 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12078 TREE_OPERAND (arg0, 1));
12080 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12081 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12082 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12083 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12084 build_int_cst (TREE_TYPE (arg1), 0));
12085 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12086 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12087 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12088 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12089 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12090 build_int_cst (TREE_TYPE (arg1), 0));
12092 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12093 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12094 && TREE_CODE (arg1) == INTEGER_CST
12095 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12096 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12097 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12098 TREE_OPERAND (arg0, 1), arg1));
12100 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12101 (X & C) == 0 when C is a single bit. */
12102 if (TREE_CODE (arg0) == BIT_AND_EXPR
12103 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12104 && integer_zerop (arg1)
12105 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12107 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12108 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12109 TREE_OPERAND (arg0, 1));
12110 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12111 type, tem, arg1);
12114 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12115 constant C is a power of two, i.e. a single bit. */
12116 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12117 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12118 && integer_zerop (arg1)
12119 && integer_pow2p (TREE_OPERAND (arg0, 1))
12120 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12121 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12123 tree arg00 = TREE_OPERAND (arg0, 0);
12124 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12125 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12128 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12129 when is C is a power of two, i.e. a single bit. */
12130 if (TREE_CODE (arg0) == BIT_AND_EXPR
12131 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12132 && integer_zerop (arg1)
12133 && integer_pow2p (TREE_OPERAND (arg0, 1))
12134 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12135 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12137 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12138 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12139 arg000, TREE_OPERAND (arg0, 1));
12140 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12141 tem, build_int_cst (TREE_TYPE (tem), 0));
12144 if (integer_zerop (arg1)
12145 && tree_expr_nonzero_p (arg0))
12147 tree res = constant_boolean_node (code==NE_EXPR, type);
12148 return omit_one_operand (type, res, arg0);
12151 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12152 if (TREE_CODE (arg0) == NEGATE_EXPR
12153 && TREE_CODE (arg1) == NEGATE_EXPR)
12154 return fold_build2 (code, type,
12155 TREE_OPERAND (arg0, 0),
12156 TREE_OPERAND (arg1, 0));
12158 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12159 if (TREE_CODE (arg0) == BIT_AND_EXPR
12160 && TREE_CODE (arg1) == BIT_AND_EXPR)
12162 tree arg00 = TREE_OPERAND (arg0, 0);
12163 tree arg01 = TREE_OPERAND (arg0, 1);
12164 tree arg10 = TREE_OPERAND (arg1, 0);
12165 tree arg11 = TREE_OPERAND (arg1, 1);
12166 tree itype = TREE_TYPE (arg0);
12168 if (operand_equal_p (arg01, arg11, 0))
12169 return fold_build2 (code, type,
12170 fold_build2 (BIT_AND_EXPR, itype,
12171 fold_build2 (BIT_XOR_EXPR, itype,
12172 arg00, arg10),
12173 arg01),
12174 build_int_cst (itype, 0));
12176 if (operand_equal_p (arg01, arg10, 0))
12177 return fold_build2 (code, type,
12178 fold_build2 (BIT_AND_EXPR, itype,
12179 fold_build2 (BIT_XOR_EXPR, itype,
12180 arg00, arg11),
12181 arg01),
12182 build_int_cst (itype, 0));
12184 if (operand_equal_p (arg00, arg11, 0))
12185 return fold_build2 (code, type,
12186 fold_build2 (BIT_AND_EXPR, itype,
12187 fold_build2 (BIT_XOR_EXPR, itype,
12188 arg01, arg10),
12189 arg00),
12190 build_int_cst (itype, 0));
12192 if (operand_equal_p (arg00, arg10, 0))
12193 return fold_build2 (code, type,
12194 fold_build2 (BIT_AND_EXPR, itype,
12195 fold_build2 (BIT_XOR_EXPR, itype,
12196 arg01, arg11),
12197 arg00),
12198 build_int_cst (itype, 0));
12201 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12202 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12204 tree arg00 = TREE_OPERAND (arg0, 0);
12205 tree arg01 = TREE_OPERAND (arg0, 1);
12206 tree arg10 = TREE_OPERAND (arg1, 0);
12207 tree arg11 = TREE_OPERAND (arg1, 1);
12208 tree itype = TREE_TYPE (arg0);
12210 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12211 operand_equal_p guarantees no side-effects so we don't need
12212 to use omit_one_operand on Z. */
12213 if (operand_equal_p (arg01, arg11, 0))
12214 return fold_build2 (code, type, arg00, arg10);
12215 if (operand_equal_p (arg01, arg10, 0))
12216 return fold_build2 (code, type, arg00, arg11);
12217 if (operand_equal_p (arg00, arg11, 0))
12218 return fold_build2 (code, type, arg01, arg10);
12219 if (operand_equal_p (arg00, arg10, 0))
12220 return fold_build2 (code, type, arg01, arg11);
12222 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12223 if (TREE_CODE (arg01) == INTEGER_CST
12224 && TREE_CODE (arg11) == INTEGER_CST)
12225 return fold_build2 (code, type,
12226 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12227 fold_build2 (BIT_XOR_EXPR, itype,
12228 arg01, arg11)),
12229 arg10);
12232 /* Attempt to simplify equality/inequality comparisons of complex
12233 values. Only lower the comparison if the result is known or
12234 can be simplified to a single scalar comparison. */
12235 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12236 || TREE_CODE (arg0) == COMPLEX_CST)
12237 && (TREE_CODE (arg1) == COMPLEX_EXPR
12238 || TREE_CODE (arg1) == COMPLEX_CST))
12240 tree real0, imag0, real1, imag1;
12241 tree rcond, icond;
12243 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12245 real0 = TREE_OPERAND (arg0, 0);
12246 imag0 = TREE_OPERAND (arg0, 1);
12248 else
12250 real0 = TREE_REALPART (arg0);
12251 imag0 = TREE_IMAGPART (arg0);
12254 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12256 real1 = TREE_OPERAND (arg1, 0);
12257 imag1 = TREE_OPERAND (arg1, 1);
12259 else
12261 real1 = TREE_REALPART (arg1);
12262 imag1 = TREE_IMAGPART (arg1);
12265 rcond = fold_binary (code, type, real0, real1);
12266 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12268 if (integer_zerop (rcond))
12270 if (code == EQ_EXPR)
12271 return omit_two_operands (type, boolean_false_node,
12272 imag0, imag1);
12273 return fold_build2 (NE_EXPR, type, imag0, imag1);
12275 else
12277 if (code == NE_EXPR)
12278 return omit_two_operands (type, boolean_true_node,
12279 imag0, imag1);
12280 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12284 icond = fold_binary (code, type, imag0, imag1);
12285 if (icond && TREE_CODE (icond) == INTEGER_CST)
12287 if (integer_zerop (icond))
12289 if (code == EQ_EXPR)
12290 return omit_two_operands (type, boolean_false_node,
12291 real0, real1);
12292 return fold_build2 (NE_EXPR, type, real0, real1);
12294 else
12296 if (code == NE_EXPR)
12297 return omit_two_operands (type, boolean_true_node,
12298 real0, real1);
12299 return fold_build2 (EQ_EXPR, type, real0, real1);
12304 return NULL_TREE;
12306 case LT_EXPR:
12307 case GT_EXPR:
12308 case LE_EXPR:
12309 case GE_EXPR:
12310 tem = fold_comparison (code, type, op0, op1);
12311 if (tem != NULL_TREE)
12312 return tem;
12314 /* Transform comparisons of the form X +- C CMP X. */
12315 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12316 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12317 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12318 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12319 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12320 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12322 tree arg01 = TREE_OPERAND (arg0, 1);
12323 enum tree_code code0 = TREE_CODE (arg0);
12324 int is_positive;
12326 if (TREE_CODE (arg01) == REAL_CST)
12327 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12328 else
12329 is_positive = tree_int_cst_sgn (arg01);
12331 /* (X - c) > X becomes false. */
12332 if (code == GT_EXPR
12333 && ((code0 == MINUS_EXPR && is_positive >= 0)
12334 || (code0 == PLUS_EXPR && is_positive <= 0)))
12336 if (TREE_CODE (arg01) == INTEGER_CST
12337 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12338 fold_overflow_warning (("assuming signed overflow does not "
12339 "occur when assuming that (X - c) > X "
12340 "is always false"),
12341 WARN_STRICT_OVERFLOW_ALL);
12342 return constant_boolean_node (0, type);
12345 /* Likewise (X + c) < X becomes false. */
12346 if (code == LT_EXPR
12347 && ((code0 == PLUS_EXPR && is_positive >= 0)
12348 || (code0 == MINUS_EXPR && is_positive <= 0)))
12350 if (TREE_CODE (arg01) == INTEGER_CST
12351 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12352 fold_overflow_warning (("assuming signed overflow does not "
12353 "occur when assuming that "
12354 "(X + c) < X is always false"),
12355 WARN_STRICT_OVERFLOW_ALL);
12356 return constant_boolean_node (0, type);
12359 /* Convert (X - c) <= X to true. */
12360 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12361 && code == LE_EXPR
12362 && ((code0 == MINUS_EXPR && is_positive >= 0)
12363 || (code0 == PLUS_EXPR && is_positive <= 0)))
12365 if (TREE_CODE (arg01) == INTEGER_CST
12366 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12367 fold_overflow_warning (("assuming signed overflow does not "
12368 "occur when assuming that "
12369 "(X - c) <= X is always true"),
12370 WARN_STRICT_OVERFLOW_ALL);
12371 return constant_boolean_node (1, type);
12374 /* Convert (X + c) >= X to true. */
12375 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12376 && code == GE_EXPR
12377 && ((code0 == PLUS_EXPR && is_positive >= 0)
12378 || (code0 == MINUS_EXPR && is_positive <= 0)))
12380 if (TREE_CODE (arg01) == INTEGER_CST
12381 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12382 fold_overflow_warning (("assuming signed overflow does not "
12383 "occur when assuming that "
12384 "(X + c) >= X is always true"),
12385 WARN_STRICT_OVERFLOW_ALL);
12386 return constant_boolean_node (1, type);
12389 if (TREE_CODE (arg01) == INTEGER_CST)
12391 /* Convert X + c > X and X - c < X to true for integers. */
12392 if (code == GT_EXPR
12393 && ((code0 == PLUS_EXPR && is_positive > 0)
12394 || (code0 == MINUS_EXPR && is_positive < 0)))
12396 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12397 fold_overflow_warning (("assuming signed overflow does "
12398 "not occur when assuming that "
12399 "(X + c) > X is always true"),
12400 WARN_STRICT_OVERFLOW_ALL);
12401 return constant_boolean_node (1, type);
12404 if (code == LT_EXPR
12405 && ((code0 == MINUS_EXPR && is_positive > 0)
12406 || (code0 == PLUS_EXPR && is_positive < 0)))
12408 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12409 fold_overflow_warning (("assuming signed overflow does "
12410 "not occur when assuming that "
12411 "(X - c) < X is always true"),
12412 WARN_STRICT_OVERFLOW_ALL);
12413 return constant_boolean_node (1, type);
12416 /* Convert X + c <= X and X - c >= X to false for integers. */
12417 if (code == LE_EXPR
12418 && ((code0 == PLUS_EXPR && is_positive > 0)
12419 || (code0 == MINUS_EXPR && is_positive < 0)))
12421 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12422 fold_overflow_warning (("assuming signed overflow does "
12423 "not occur when assuming that "
12424 "(X + c) <= X is always false"),
12425 WARN_STRICT_OVERFLOW_ALL);
12426 return constant_boolean_node (0, type);
12429 if (code == GE_EXPR
12430 && ((code0 == MINUS_EXPR && is_positive > 0)
12431 || (code0 == PLUS_EXPR && is_positive < 0)))
12433 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12434 fold_overflow_warning (("assuming signed overflow does "
12435 "not occur when assuming that "
12436 "(X - c) >= X is always false"),
12437 WARN_STRICT_OVERFLOW_ALL);
12438 return constant_boolean_node (0, type);
12443 /* Comparisons with the highest or lowest possible integer of
12444 the specified precision will have known values. */
12446 tree arg1_type = TREE_TYPE (arg1);
12447 unsigned int width = TYPE_PRECISION (arg1_type);
12449 if (TREE_CODE (arg1) == INTEGER_CST
12450 && !TREE_OVERFLOW (arg1)
12451 && width <= 2 * HOST_BITS_PER_WIDE_INT
12452 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12454 HOST_WIDE_INT signed_max_hi;
12455 unsigned HOST_WIDE_INT signed_max_lo;
12456 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12458 if (width <= HOST_BITS_PER_WIDE_INT)
12460 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12461 - 1;
12462 signed_max_hi = 0;
12463 max_hi = 0;
12465 if (TYPE_UNSIGNED (arg1_type))
12467 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12468 min_lo = 0;
12469 min_hi = 0;
12471 else
12473 max_lo = signed_max_lo;
12474 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12475 min_hi = -1;
12478 else
12480 width -= HOST_BITS_PER_WIDE_INT;
12481 signed_max_lo = -1;
12482 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12483 - 1;
12484 max_lo = -1;
12485 min_lo = 0;
12487 if (TYPE_UNSIGNED (arg1_type))
12489 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12490 min_hi = 0;
12492 else
12494 max_hi = signed_max_hi;
12495 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12499 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12500 && TREE_INT_CST_LOW (arg1) == max_lo)
12501 switch (code)
12503 case GT_EXPR:
12504 return omit_one_operand (type, integer_zero_node, arg0);
12506 case GE_EXPR:
12507 return fold_build2 (EQ_EXPR, type, op0, op1);
12509 case LE_EXPR:
12510 return omit_one_operand (type, integer_one_node, arg0);
12512 case LT_EXPR:
12513 return fold_build2 (NE_EXPR, type, op0, op1);
12515 /* The GE_EXPR and LT_EXPR cases above are not normally
12516 reached because of previous transformations. */
12518 default:
12519 break;
12521 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12522 == max_hi
12523 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12524 switch (code)
12526 case GT_EXPR:
12527 arg1 = const_binop (PLUS_EXPR, arg1,
12528 build_int_cst (TREE_TYPE (arg1), 1), 0);
12529 return fold_build2 (EQ_EXPR, type,
12530 fold_convert (TREE_TYPE (arg1), arg0),
12531 arg1);
12532 case LE_EXPR:
12533 arg1 = const_binop (PLUS_EXPR, arg1,
12534 build_int_cst (TREE_TYPE (arg1), 1), 0);
12535 return fold_build2 (NE_EXPR, type,
12536 fold_convert (TREE_TYPE (arg1), arg0),
12537 arg1);
12538 default:
12539 break;
12541 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12542 == min_hi
12543 && TREE_INT_CST_LOW (arg1) == min_lo)
12544 switch (code)
12546 case LT_EXPR:
12547 return omit_one_operand (type, integer_zero_node, arg0);
12549 case LE_EXPR:
12550 return fold_build2 (EQ_EXPR, type, op0, op1);
12552 case GE_EXPR:
12553 return omit_one_operand (type, integer_one_node, arg0);
12555 case GT_EXPR:
12556 return fold_build2 (NE_EXPR, type, op0, op1);
12558 default:
12559 break;
12561 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12562 == min_hi
12563 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12564 switch (code)
12566 case GE_EXPR:
12567 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12568 return fold_build2 (NE_EXPR, type,
12569 fold_convert (TREE_TYPE (arg1), arg0),
12570 arg1);
12571 case LT_EXPR:
12572 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12573 return fold_build2 (EQ_EXPR, type,
12574 fold_convert (TREE_TYPE (arg1), arg0),
12575 arg1);
12576 default:
12577 break;
12580 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12581 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12582 && TYPE_UNSIGNED (arg1_type)
12583 /* We will flip the signedness of the comparison operator
12584 associated with the mode of arg1, so the sign bit is
12585 specified by this mode. Check that arg1 is the signed
12586 max associated with this sign bit. */
12587 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12588 /* signed_type does not work on pointer types. */
12589 && INTEGRAL_TYPE_P (arg1_type))
12591 /* The following case also applies to X < signed_max+1
12592 and X >= signed_max+1 because previous transformations. */
12593 if (code == LE_EXPR || code == GT_EXPR)
12595 tree st;
12596 st = signed_type_for (TREE_TYPE (arg1));
12597 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12598 type, fold_convert (st, arg0),
12599 build_int_cst (st, 0));
12605 /* If we are comparing an ABS_EXPR with a constant, we can
12606 convert all the cases into explicit comparisons, but they may
12607 well not be faster than doing the ABS and one comparison.
12608 But ABS (X) <= C is a range comparison, which becomes a subtraction
12609 and a comparison, and is probably faster. */
12610 if (code == LE_EXPR
12611 && TREE_CODE (arg1) == INTEGER_CST
12612 && TREE_CODE (arg0) == ABS_EXPR
12613 && ! TREE_SIDE_EFFECTS (arg0)
12614 && (0 != (tem = negate_expr (arg1)))
12615 && TREE_CODE (tem) == INTEGER_CST
12616 && !TREE_OVERFLOW (tem))
12617 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12618 build2 (GE_EXPR, type,
12619 TREE_OPERAND (arg0, 0), tem),
12620 build2 (LE_EXPR, type,
12621 TREE_OPERAND (arg0, 0), arg1));
12623 /* Convert ABS_EXPR<x> >= 0 to true. */
12624 strict_overflow_p = false;
12625 if (code == GE_EXPR
12626 && (integer_zerop (arg1)
12627 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12628 && real_zerop (arg1)))
12629 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12631 if (strict_overflow_p)
12632 fold_overflow_warning (("assuming signed overflow does not occur "
12633 "when simplifying comparison of "
12634 "absolute value and zero"),
12635 WARN_STRICT_OVERFLOW_CONDITIONAL);
12636 return omit_one_operand (type, integer_one_node, arg0);
12639 /* Convert ABS_EXPR<x> < 0 to false. */
12640 strict_overflow_p = false;
12641 if (code == LT_EXPR
12642 && (integer_zerop (arg1) || real_zerop (arg1))
12643 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12645 if (strict_overflow_p)
12646 fold_overflow_warning (("assuming signed overflow does not occur "
12647 "when simplifying comparison of "
12648 "absolute value and zero"),
12649 WARN_STRICT_OVERFLOW_CONDITIONAL);
12650 return omit_one_operand (type, integer_zero_node, arg0);
12653 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12654 and similarly for >= into !=. */
12655 if ((code == LT_EXPR || code == GE_EXPR)
12656 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12657 && TREE_CODE (arg1) == LSHIFT_EXPR
12658 && integer_onep (TREE_OPERAND (arg1, 0)))
12659 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12660 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12661 TREE_OPERAND (arg1, 1)),
12662 build_int_cst (TREE_TYPE (arg0), 0));
12664 if ((code == LT_EXPR || code == GE_EXPR)
12665 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12666 && CONVERT_EXPR_P (arg1)
12667 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12668 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12669 return
12670 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12671 fold_convert (TREE_TYPE (arg0),
12672 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12673 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12674 1))),
12675 build_int_cst (TREE_TYPE (arg0), 0));
12677 return NULL_TREE;
12679 case UNORDERED_EXPR:
12680 case ORDERED_EXPR:
12681 case UNLT_EXPR:
12682 case UNLE_EXPR:
12683 case UNGT_EXPR:
12684 case UNGE_EXPR:
12685 case UNEQ_EXPR:
12686 case LTGT_EXPR:
12687 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12689 t1 = fold_relational_const (code, type, arg0, arg1);
12690 if (t1 != NULL_TREE)
12691 return t1;
12694 /* If the first operand is NaN, the result is constant. */
12695 if (TREE_CODE (arg0) == REAL_CST
12696 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12697 && (code != LTGT_EXPR || ! flag_trapping_math))
12699 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12700 ? integer_zero_node
12701 : integer_one_node;
12702 return omit_one_operand (type, t1, arg1);
12705 /* If the second operand is NaN, the result is constant. */
12706 if (TREE_CODE (arg1) == REAL_CST
12707 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12708 && (code != LTGT_EXPR || ! flag_trapping_math))
12710 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12711 ? integer_zero_node
12712 : integer_one_node;
12713 return omit_one_operand (type, t1, arg0);
12716 /* Simplify unordered comparison of something with itself. */
12717 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12718 && operand_equal_p (arg0, arg1, 0))
12719 return constant_boolean_node (1, type);
12721 if (code == LTGT_EXPR
12722 && !flag_trapping_math
12723 && operand_equal_p (arg0, arg1, 0))
12724 return constant_boolean_node (0, type);
12726 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12728 tree targ0 = strip_float_extensions (arg0);
12729 tree targ1 = strip_float_extensions (arg1);
12730 tree newtype = TREE_TYPE (targ0);
12732 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12733 newtype = TREE_TYPE (targ1);
12735 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12736 return fold_build2 (code, type, fold_convert (newtype, targ0),
12737 fold_convert (newtype, targ1));
12740 return NULL_TREE;
12742 case COMPOUND_EXPR:
12743 /* When pedantic, a compound expression can be neither an lvalue
12744 nor an integer constant expression. */
12745 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12746 return NULL_TREE;
12747 /* Don't let (0, 0) be null pointer constant. */
12748 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12749 : fold_convert (type, arg1);
12750 return pedantic_non_lvalue (tem);
12752 case COMPLEX_EXPR:
12753 if ((TREE_CODE (arg0) == REAL_CST
12754 && TREE_CODE (arg1) == REAL_CST)
12755 || (TREE_CODE (arg0) == INTEGER_CST
12756 && TREE_CODE (arg1) == INTEGER_CST))
12757 return build_complex (type, arg0, arg1);
12758 return NULL_TREE;
12760 case ASSERT_EXPR:
12761 /* An ASSERT_EXPR should never be passed to fold_binary. */
12762 gcc_unreachable ();
12764 default:
12765 return NULL_TREE;
12766 } /* switch (code) */
12769 /* Callback for walk_tree, looking for LABEL_EXPR.
12770 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12771 Do not check the sub-tree of GOTO_EXPR. */
12773 static tree
12774 contains_label_1 (tree *tp,
12775 int *walk_subtrees,
12776 void *data ATTRIBUTE_UNUSED)
12778 switch (TREE_CODE (*tp))
12780 case LABEL_EXPR:
12781 return *tp;
12782 case GOTO_EXPR:
12783 *walk_subtrees = 0;
12784 /* no break */
12785 default:
12786 return NULL_TREE;
12790 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12791 accessible from outside the sub-tree. Returns NULL_TREE if no
12792 addressable label is found. */
12794 static bool
12795 contains_label_p (tree st)
12797 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12800 /* Fold a ternary expression of code CODE and type TYPE with operands
12801 OP0, OP1, and OP2. Return the folded expression if folding is
12802 successful. Otherwise, return NULL_TREE. */
12804 tree
12805 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12807 tree tem;
12808 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12809 enum tree_code_class kind = TREE_CODE_CLASS (code);
12811 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12812 && TREE_CODE_LENGTH (code) == 3);
12814 /* Strip any conversions that don't change the mode. This is safe
12815 for every expression, except for a comparison expression because
12816 its signedness is derived from its operands. So, in the latter
12817 case, only strip conversions that don't change the signedness.
12819 Note that this is done as an internal manipulation within the
12820 constant folder, in order to find the simplest representation of
12821 the arguments so that their form can be studied. In any cases,
12822 the appropriate type conversions should be put back in the tree
12823 that will get out of the constant folder. */
12824 if (op0)
12826 arg0 = op0;
12827 STRIP_NOPS (arg0);
12830 if (op1)
12832 arg1 = op1;
12833 STRIP_NOPS (arg1);
12836 switch (code)
12838 case COMPONENT_REF:
12839 if (TREE_CODE (arg0) == CONSTRUCTOR
12840 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12842 unsigned HOST_WIDE_INT idx;
12843 tree field, value;
12844 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12845 if (field == arg1)
12846 return value;
12848 return NULL_TREE;
12850 case COND_EXPR:
12851 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12852 so all simple results must be passed through pedantic_non_lvalue. */
12853 if (TREE_CODE (arg0) == INTEGER_CST)
12855 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12856 tem = integer_zerop (arg0) ? op2 : op1;
12857 /* Only optimize constant conditions when the selected branch
12858 has the same type as the COND_EXPR. This avoids optimizing
12859 away "c ? x : throw", where the throw has a void type.
12860 Avoid throwing away that operand which contains label. */
12861 if ((!TREE_SIDE_EFFECTS (unused_op)
12862 || !contains_label_p (unused_op))
12863 && (! VOID_TYPE_P (TREE_TYPE (tem))
12864 || VOID_TYPE_P (type)))
12865 return pedantic_non_lvalue (tem);
12866 return NULL_TREE;
12868 if (operand_equal_p (arg1, op2, 0))
12869 return pedantic_omit_one_operand (type, arg1, arg0);
12871 /* If we have A op B ? A : C, we may be able to convert this to a
12872 simpler expression, depending on the operation and the values
12873 of B and C. Signed zeros prevent all of these transformations,
12874 for reasons given above each one.
12876 Also try swapping the arguments and inverting the conditional. */
12877 if (COMPARISON_CLASS_P (arg0)
12878 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12879 arg1, TREE_OPERAND (arg0, 1))
12880 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12882 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12883 if (tem)
12884 return tem;
12887 if (COMPARISON_CLASS_P (arg0)
12888 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12889 op2,
12890 TREE_OPERAND (arg0, 1))
12891 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12893 tem = fold_truth_not_expr (arg0);
12894 if (tem && COMPARISON_CLASS_P (tem))
12896 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12897 if (tem)
12898 return tem;
12902 /* If the second operand is simpler than the third, swap them
12903 since that produces better jump optimization results. */
12904 if (truth_value_p (TREE_CODE (arg0))
12905 && tree_swap_operands_p (op1, op2, false))
12907 /* See if this can be inverted. If it can't, possibly because
12908 it was a floating-point inequality comparison, don't do
12909 anything. */
12910 tem = fold_truth_not_expr (arg0);
12911 if (tem)
12912 return fold_build3 (code, type, tem, op2, op1);
12915 /* Convert A ? 1 : 0 to simply A. */
12916 if (integer_onep (op1)
12917 && integer_zerop (op2)
12918 /* If we try to convert OP0 to our type, the
12919 call to fold will try to move the conversion inside
12920 a COND, which will recurse. In that case, the COND_EXPR
12921 is probably the best choice, so leave it alone. */
12922 && type == TREE_TYPE (arg0))
12923 return pedantic_non_lvalue (arg0);
12925 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12926 over COND_EXPR in cases such as floating point comparisons. */
12927 if (integer_zerop (op1)
12928 && integer_onep (op2)
12929 && truth_value_p (TREE_CODE (arg0)))
12930 return pedantic_non_lvalue (fold_convert (type,
12931 invert_truthvalue (arg0)));
12933 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12934 if (TREE_CODE (arg0) == LT_EXPR
12935 && integer_zerop (TREE_OPERAND (arg0, 1))
12936 && integer_zerop (op2)
12937 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12939 /* sign_bit_p only checks ARG1 bits within A's precision.
12940 If <sign bit of A> has wider type than A, bits outside
12941 of A's precision in <sign bit of A> need to be checked.
12942 If they are all 0, this optimization needs to be done
12943 in unsigned A's type, if they are all 1 in signed A's type,
12944 otherwise this can't be done. */
12945 if (TYPE_PRECISION (TREE_TYPE (tem))
12946 < TYPE_PRECISION (TREE_TYPE (arg1))
12947 && TYPE_PRECISION (TREE_TYPE (tem))
12948 < TYPE_PRECISION (type))
12950 unsigned HOST_WIDE_INT mask_lo;
12951 HOST_WIDE_INT mask_hi;
12952 int inner_width, outer_width;
12953 tree tem_type;
12955 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12956 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12957 if (outer_width > TYPE_PRECISION (type))
12958 outer_width = TYPE_PRECISION (type);
12960 if (outer_width > HOST_BITS_PER_WIDE_INT)
12962 mask_hi = ((unsigned HOST_WIDE_INT) -1
12963 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12964 mask_lo = -1;
12966 else
12968 mask_hi = 0;
12969 mask_lo = ((unsigned HOST_WIDE_INT) -1
12970 >> (HOST_BITS_PER_WIDE_INT - outer_width));
12972 if (inner_width > HOST_BITS_PER_WIDE_INT)
12974 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12975 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12976 mask_lo = 0;
12978 else
12979 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12980 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12982 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12983 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12985 tem_type = signed_type_for (TREE_TYPE (tem));
12986 tem = fold_convert (tem_type, tem);
12988 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12989 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12991 tem_type = unsigned_type_for (TREE_TYPE (tem));
12992 tem = fold_convert (tem_type, tem);
12994 else
12995 tem = NULL;
12998 if (tem)
12999 return fold_convert (type,
13000 fold_build2 (BIT_AND_EXPR,
13001 TREE_TYPE (tem), tem,
13002 fold_convert (TREE_TYPE (tem),
13003 arg1)));
13006 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13007 already handled above. */
13008 if (TREE_CODE (arg0) == BIT_AND_EXPR
13009 && integer_onep (TREE_OPERAND (arg0, 1))
13010 && integer_zerop (op2)
13011 && integer_pow2p (arg1))
13013 tree tem = TREE_OPERAND (arg0, 0);
13014 STRIP_NOPS (tem);
13015 if (TREE_CODE (tem) == RSHIFT_EXPR
13016 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13017 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13018 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13019 return fold_build2 (BIT_AND_EXPR, type,
13020 TREE_OPERAND (tem, 0), arg1);
13023 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13024 is probably obsolete because the first operand should be a
13025 truth value (that's why we have the two cases above), but let's
13026 leave it in until we can confirm this for all front-ends. */
13027 if (integer_zerop (op2)
13028 && TREE_CODE (arg0) == NE_EXPR
13029 && integer_zerop (TREE_OPERAND (arg0, 1))
13030 && integer_pow2p (arg1)
13031 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13032 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13033 arg1, OEP_ONLY_CONST))
13034 return pedantic_non_lvalue (fold_convert (type,
13035 TREE_OPERAND (arg0, 0)));
13037 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13038 if (integer_zerop (op2)
13039 && truth_value_p (TREE_CODE (arg0))
13040 && truth_value_p (TREE_CODE (arg1)))
13041 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13042 fold_convert (type, arg0),
13043 arg1);
13045 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13046 if (integer_onep (op2)
13047 && truth_value_p (TREE_CODE (arg0))
13048 && truth_value_p (TREE_CODE (arg1)))
13050 /* Only perform transformation if ARG0 is easily inverted. */
13051 tem = fold_truth_not_expr (arg0);
13052 if (tem)
13053 return fold_build2 (TRUTH_ORIF_EXPR, type,
13054 fold_convert (type, tem),
13055 arg1);
13058 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13059 if (integer_zerop (arg1)
13060 && truth_value_p (TREE_CODE (arg0))
13061 && truth_value_p (TREE_CODE (op2)))
13063 /* Only perform transformation if ARG0 is easily inverted. */
13064 tem = fold_truth_not_expr (arg0);
13065 if (tem)
13066 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13067 fold_convert (type, tem),
13068 op2);
13071 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13072 if (integer_onep (arg1)
13073 && truth_value_p (TREE_CODE (arg0))
13074 && truth_value_p (TREE_CODE (op2)))
13075 return fold_build2 (TRUTH_ORIF_EXPR, type,
13076 fold_convert (type, arg0),
13077 op2);
13079 return NULL_TREE;
13081 case CALL_EXPR:
13082 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13083 of fold_ternary on them. */
13084 gcc_unreachable ();
13086 case BIT_FIELD_REF:
13087 if ((TREE_CODE (arg0) == VECTOR_CST
13088 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13089 && type == TREE_TYPE (TREE_TYPE (arg0)))
13091 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13092 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13094 if (width != 0
13095 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13096 && (idx % width) == 0
13097 && (idx = idx / width)
13098 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13100 tree elements = NULL_TREE;
13102 if (TREE_CODE (arg0) == VECTOR_CST)
13103 elements = TREE_VECTOR_CST_ELTS (arg0);
13104 else
13106 unsigned HOST_WIDE_INT idx;
13107 tree value;
13109 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13110 elements = tree_cons (NULL_TREE, value, elements);
13112 while (idx-- > 0 && elements)
13113 elements = TREE_CHAIN (elements);
13114 if (elements)
13115 return TREE_VALUE (elements);
13116 else
13117 return fold_convert (type, integer_zero_node);
13121 /* A bit-field-ref that referenced the full argument can be stripped. */
13122 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13123 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13124 && integer_zerop (op2))
13125 return fold_convert (type, arg0);
13127 return NULL_TREE;
13129 default:
13130 return NULL_TREE;
13131 } /* switch (code) */
13134 /* Perform constant folding and related simplification of EXPR.
13135 The related simplifications include x*1 => x, x*0 => 0, etc.,
13136 and application of the associative law.
13137 NOP_EXPR conversions may be removed freely (as long as we
13138 are careful not to change the type of the overall expression).
13139 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13140 but we can constant-fold them if they have constant operands. */
13142 #ifdef ENABLE_FOLD_CHECKING
13143 # define fold(x) fold_1 (x)
13144 static tree fold_1 (tree);
13145 static
13146 #endif
13147 tree
13148 fold (tree expr)
13150 const tree t = expr;
13151 enum tree_code code = TREE_CODE (t);
13152 enum tree_code_class kind = TREE_CODE_CLASS (code);
13153 tree tem;
13155 /* Return right away if a constant. */
13156 if (kind == tcc_constant)
13157 return t;
13159 /* CALL_EXPR-like objects with variable numbers of operands are
13160 treated specially. */
13161 if (kind == tcc_vl_exp)
13163 if (code == CALL_EXPR)
13165 tem = fold_call_expr (expr, false);
13166 return tem ? tem : expr;
13168 return expr;
13171 if (IS_EXPR_CODE_CLASS (kind))
13173 tree type = TREE_TYPE (t);
13174 tree op0, op1, op2;
13176 switch (TREE_CODE_LENGTH (code))
13178 case 1:
13179 op0 = TREE_OPERAND (t, 0);
13180 tem = fold_unary (code, type, op0);
13181 return tem ? tem : expr;
13182 case 2:
13183 op0 = TREE_OPERAND (t, 0);
13184 op1 = TREE_OPERAND (t, 1);
13185 tem = fold_binary (code, type, op0, op1);
13186 return tem ? tem : expr;
13187 case 3:
13188 op0 = TREE_OPERAND (t, 0);
13189 op1 = TREE_OPERAND (t, 1);
13190 op2 = TREE_OPERAND (t, 2);
13191 tem = fold_ternary (code, type, op0, op1, op2);
13192 return tem ? tem : expr;
13193 default:
13194 break;
13198 switch (code)
13200 case ARRAY_REF:
13202 tree op0 = TREE_OPERAND (t, 0);
13203 tree op1 = TREE_OPERAND (t, 1);
13205 if (TREE_CODE (op1) == INTEGER_CST
13206 && TREE_CODE (op0) == CONSTRUCTOR
13207 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13209 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13210 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13211 unsigned HOST_WIDE_INT begin = 0;
13213 /* Find a matching index by means of a binary search. */
13214 while (begin != end)
13216 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13217 tree index = VEC_index (constructor_elt, elts, middle)->index;
13219 if (TREE_CODE (index) == INTEGER_CST
13220 && tree_int_cst_lt (index, op1))
13221 begin = middle + 1;
13222 else if (TREE_CODE (index) == INTEGER_CST
13223 && tree_int_cst_lt (op1, index))
13224 end = middle;
13225 else if (TREE_CODE (index) == RANGE_EXPR
13226 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13227 begin = middle + 1;
13228 else if (TREE_CODE (index) == RANGE_EXPR
13229 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13230 end = middle;
13231 else
13232 return VEC_index (constructor_elt, elts, middle)->value;
13236 return t;
13239 case CONST_DECL:
13240 return fold (DECL_INITIAL (t));
13242 default:
13243 return t;
13244 } /* switch (code) */
13247 #ifdef ENABLE_FOLD_CHECKING
13248 #undef fold
13250 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13251 static void fold_check_failed (const_tree, const_tree);
13252 void print_fold_checksum (const_tree);
13254 /* When --enable-checking=fold, compute a digest of expr before
13255 and after actual fold call to see if fold did not accidentally
13256 change original expr. */
13258 tree
13259 fold (tree expr)
13261 tree ret;
13262 struct md5_ctx ctx;
13263 unsigned char checksum_before[16], checksum_after[16];
13264 htab_t ht;
13266 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13267 md5_init_ctx (&ctx);
13268 fold_checksum_tree (expr, &ctx, ht);
13269 md5_finish_ctx (&ctx, checksum_before);
13270 htab_empty (ht);
13272 ret = fold_1 (expr);
13274 md5_init_ctx (&ctx);
13275 fold_checksum_tree (expr, &ctx, ht);
13276 md5_finish_ctx (&ctx, checksum_after);
13277 htab_delete (ht);
13279 if (memcmp (checksum_before, checksum_after, 16))
13280 fold_check_failed (expr, ret);
13282 return ret;
13285 void
13286 print_fold_checksum (const_tree expr)
13288 struct md5_ctx ctx;
13289 unsigned char checksum[16], cnt;
13290 htab_t ht;
13292 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13293 md5_init_ctx (&ctx);
13294 fold_checksum_tree (expr, &ctx, ht);
13295 md5_finish_ctx (&ctx, checksum);
13296 htab_delete (ht);
13297 for (cnt = 0; cnt < 16; ++cnt)
13298 fprintf (stderr, "%02x", checksum[cnt]);
13299 putc ('\n', stderr);
13302 static void
13303 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13305 internal_error ("fold check: original tree changed by fold");
13308 static void
13309 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13311 const void **slot;
13312 enum tree_code code;
13313 struct tree_function_decl buf;
13314 int i, len;
13316 recursive_label:
13318 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13319 <= sizeof (struct tree_function_decl))
13320 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13321 if (expr == NULL)
13322 return;
13323 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13324 if (*slot != NULL)
13325 return;
13326 *slot = expr;
13327 code = TREE_CODE (expr);
13328 if (TREE_CODE_CLASS (code) == tcc_declaration
13329 && DECL_ASSEMBLER_NAME_SET_P (expr))
13331 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13332 memcpy ((char *) &buf, expr, tree_size (expr));
13333 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13334 expr = (tree) &buf;
13336 else if (TREE_CODE_CLASS (code) == tcc_type
13337 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13338 || TYPE_CACHED_VALUES_P (expr)
13339 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13341 /* Allow these fields to be modified. */
13342 tree tmp;
13343 memcpy ((char *) &buf, expr, tree_size (expr));
13344 expr = tmp = (tree) &buf;
13345 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13346 TYPE_POINTER_TO (tmp) = NULL;
13347 TYPE_REFERENCE_TO (tmp) = NULL;
13348 if (TYPE_CACHED_VALUES_P (tmp))
13350 TYPE_CACHED_VALUES_P (tmp) = 0;
13351 TYPE_CACHED_VALUES (tmp) = NULL;
13354 md5_process_bytes (expr, tree_size (expr), ctx);
13355 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13356 if (TREE_CODE_CLASS (code) != tcc_type
13357 && TREE_CODE_CLASS (code) != tcc_declaration
13358 && code != TREE_LIST
13359 && code != SSA_NAME)
13360 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13361 switch (TREE_CODE_CLASS (code))
13363 case tcc_constant:
13364 switch (code)
13366 case STRING_CST:
13367 md5_process_bytes (TREE_STRING_POINTER (expr),
13368 TREE_STRING_LENGTH (expr), ctx);
13369 break;
13370 case COMPLEX_CST:
13371 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13372 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13373 break;
13374 case VECTOR_CST:
13375 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13376 break;
13377 default:
13378 break;
13380 break;
13381 case tcc_exceptional:
13382 switch (code)
13384 case TREE_LIST:
13385 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13386 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13387 expr = TREE_CHAIN (expr);
13388 goto recursive_label;
13389 break;
13390 case TREE_VEC:
13391 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13392 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13393 break;
13394 default:
13395 break;
13397 break;
13398 case tcc_expression:
13399 case tcc_reference:
13400 case tcc_comparison:
13401 case tcc_unary:
13402 case tcc_binary:
13403 case tcc_statement:
13404 case tcc_vl_exp:
13405 len = TREE_OPERAND_LENGTH (expr);
13406 for (i = 0; i < len; ++i)
13407 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13408 break;
13409 case tcc_declaration:
13410 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13411 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13412 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13414 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13415 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13416 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13417 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13418 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13420 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13421 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13423 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13425 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13426 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13427 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13429 break;
13430 case tcc_type:
13431 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13432 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13433 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13434 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13435 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13436 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13437 if (INTEGRAL_TYPE_P (expr)
13438 || SCALAR_FLOAT_TYPE_P (expr))
13440 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13441 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13443 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13444 if (TREE_CODE (expr) == RECORD_TYPE
13445 || TREE_CODE (expr) == UNION_TYPE
13446 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13447 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13448 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13449 break;
13450 default:
13451 break;
13455 /* Helper function for outputting the checksum of a tree T. When
13456 debugging with gdb, you can "define mynext" to be "next" followed
13457 by "call debug_fold_checksum (op0)", then just trace down till the
13458 outputs differ. */
13460 void
13461 debug_fold_checksum (const_tree t)
13463 int i;
13464 unsigned char checksum[16];
13465 struct md5_ctx ctx;
13466 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13468 md5_init_ctx (&ctx);
13469 fold_checksum_tree (t, &ctx, ht);
13470 md5_finish_ctx (&ctx, checksum);
13471 htab_empty (ht);
13473 for (i = 0; i < 16; i++)
13474 fprintf (stderr, "%d ", checksum[i]);
13476 fprintf (stderr, "\n");
13479 #endif
13481 /* Fold a unary tree expression with code CODE of type TYPE with an
13482 operand OP0. Return a folded expression if successful. Otherwise,
13483 return a tree expression with code CODE of type TYPE with an
13484 operand OP0. */
13486 tree
13487 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13489 tree tem;
13490 #ifdef ENABLE_FOLD_CHECKING
13491 unsigned char checksum_before[16], checksum_after[16];
13492 struct md5_ctx ctx;
13493 htab_t ht;
13495 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13496 md5_init_ctx (&ctx);
13497 fold_checksum_tree (op0, &ctx, ht);
13498 md5_finish_ctx (&ctx, checksum_before);
13499 htab_empty (ht);
13500 #endif
13502 tem = fold_unary (code, type, op0);
13503 if (!tem)
13504 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13506 #ifdef ENABLE_FOLD_CHECKING
13507 md5_init_ctx (&ctx);
13508 fold_checksum_tree (op0, &ctx, ht);
13509 md5_finish_ctx (&ctx, checksum_after);
13510 htab_delete (ht);
13512 if (memcmp (checksum_before, checksum_after, 16))
13513 fold_check_failed (op0, tem);
13514 #endif
13515 return tem;
13518 /* Fold a binary tree expression with code CODE of type TYPE with
13519 operands OP0 and OP1. Return a folded expression if successful.
13520 Otherwise, return a tree expression with code CODE of type TYPE
13521 with operands OP0 and OP1. */
13523 tree
13524 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13525 MEM_STAT_DECL)
13527 tree tem;
13528 #ifdef ENABLE_FOLD_CHECKING
13529 unsigned char checksum_before_op0[16],
13530 checksum_before_op1[16],
13531 checksum_after_op0[16],
13532 checksum_after_op1[16];
13533 struct md5_ctx ctx;
13534 htab_t ht;
13536 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13537 md5_init_ctx (&ctx);
13538 fold_checksum_tree (op0, &ctx, ht);
13539 md5_finish_ctx (&ctx, checksum_before_op0);
13540 htab_empty (ht);
13542 md5_init_ctx (&ctx);
13543 fold_checksum_tree (op1, &ctx, ht);
13544 md5_finish_ctx (&ctx, checksum_before_op1);
13545 htab_empty (ht);
13546 #endif
13548 tem = fold_binary (code, type, op0, op1);
13549 if (!tem)
13550 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13552 #ifdef ENABLE_FOLD_CHECKING
13553 md5_init_ctx (&ctx);
13554 fold_checksum_tree (op0, &ctx, ht);
13555 md5_finish_ctx (&ctx, checksum_after_op0);
13556 htab_empty (ht);
13558 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13559 fold_check_failed (op0, tem);
13561 md5_init_ctx (&ctx);
13562 fold_checksum_tree (op1, &ctx, ht);
13563 md5_finish_ctx (&ctx, checksum_after_op1);
13564 htab_delete (ht);
13566 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13567 fold_check_failed (op1, tem);
13568 #endif
13569 return tem;
13572 /* Fold a ternary tree expression with code CODE of type TYPE with
13573 operands OP0, OP1, and OP2. Return a folded expression if
13574 successful. Otherwise, return a tree expression with code CODE of
13575 type TYPE with operands OP0, OP1, and OP2. */
13577 tree
13578 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13579 MEM_STAT_DECL)
13581 tree tem;
13582 #ifdef ENABLE_FOLD_CHECKING
13583 unsigned char checksum_before_op0[16],
13584 checksum_before_op1[16],
13585 checksum_before_op2[16],
13586 checksum_after_op0[16],
13587 checksum_after_op1[16],
13588 checksum_after_op2[16];
13589 struct md5_ctx ctx;
13590 htab_t ht;
13592 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13593 md5_init_ctx (&ctx);
13594 fold_checksum_tree (op0, &ctx, ht);
13595 md5_finish_ctx (&ctx, checksum_before_op0);
13596 htab_empty (ht);
13598 md5_init_ctx (&ctx);
13599 fold_checksum_tree (op1, &ctx, ht);
13600 md5_finish_ctx (&ctx, checksum_before_op1);
13601 htab_empty (ht);
13603 md5_init_ctx (&ctx);
13604 fold_checksum_tree (op2, &ctx, ht);
13605 md5_finish_ctx (&ctx, checksum_before_op2);
13606 htab_empty (ht);
13607 #endif
13609 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13610 tem = fold_ternary (code, type, op0, op1, op2);
13611 if (!tem)
13612 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13614 #ifdef ENABLE_FOLD_CHECKING
13615 md5_init_ctx (&ctx);
13616 fold_checksum_tree (op0, &ctx, ht);
13617 md5_finish_ctx (&ctx, checksum_after_op0);
13618 htab_empty (ht);
13620 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13621 fold_check_failed (op0, tem);
13623 md5_init_ctx (&ctx);
13624 fold_checksum_tree (op1, &ctx, ht);
13625 md5_finish_ctx (&ctx, checksum_after_op1);
13626 htab_empty (ht);
13628 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13629 fold_check_failed (op1, tem);
13631 md5_init_ctx (&ctx);
13632 fold_checksum_tree (op2, &ctx, ht);
13633 md5_finish_ctx (&ctx, checksum_after_op2);
13634 htab_delete (ht);
13636 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13637 fold_check_failed (op2, tem);
13638 #endif
13639 return tem;
13642 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13643 arguments in ARGARRAY, and a null static chain.
13644 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13645 of type TYPE from the given operands as constructed by build_call_array. */
13647 tree
13648 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13650 tree tem;
13651 #ifdef ENABLE_FOLD_CHECKING
13652 unsigned char checksum_before_fn[16],
13653 checksum_before_arglist[16],
13654 checksum_after_fn[16],
13655 checksum_after_arglist[16];
13656 struct md5_ctx ctx;
13657 htab_t ht;
13658 int i;
13660 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13661 md5_init_ctx (&ctx);
13662 fold_checksum_tree (fn, &ctx, ht);
13663 md5_finish_ctx (&ctx, checksum_before_fn);
13664 htab_empty (ht);
13666 md5_init_ctx (&ctx);
13667 for (i = 0; i < nargs; i++)
13668 fold_checksum_tree (argarray[i], &ctx, ht);
13669 md5_finish_ctx (&ctx, checksum_before_arglist);
13670 htab_empty (ht);
13671 #endif
13673 tem = fold_builtin_call_array (type, fn, nargs, argarray);
13675 #ifdef ENABLE_FOLD_CHECKING
13676 md5_init_ctx (&ctx);
13677 fold_checksum_tree (fn, &ctx, ht);
13678 md5_finish_ctx (&ctx, checksum_after_fn);
13679 htab_empty (ht);
13681 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13682 fold_check_failed (fn, tem);
13684 md5_init_ctx (&ctx);
13685 for (i = 0; i < nargs; i++)
13686 fold_checksum_tree (argarray[i], &ctx, ht);
13687 md5_finish_ctx (&ctx, checksum_after_arglist);
13688 htab_delete (ht);
13690 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13691 fold_check_failed (NULL_TREE, tem);
13692 #endif
13693 return tem;
13696 /* Perform constant folding and related simplification of initializer
13697 expression EXPR. These behave identically to "fold_buildN" but ignore
13698 potential run-time traps and exceptions that fold must preserve. */
13700 #define START_FOLD_INIT \
13701 int saved_signaling_nans = flag_signaling_nans;\
13702 int saved_trapping_math = flag_trapping_math;\
13703 int saved_rounding_math = flag_rounding_math;\
13704 int saved_trapv = flag_trapv;\
13705 int saved_folding_initializer = folding_initializer;\
13706 flag_signaling_nans = 0;\
13707 flag_trapping_math = 0;\
13708 flag_rounding_math = 0;\
13709 flag_trapv = 0;\
13710 folding_initializer = 1;
13712 #define END_FOLD_INIT \
13713 flag_signaling_nans = saved_signaling_nans;\
13714 flag_trapping_math = saved_trapping_math;\
13715 flag_rounding_math = saved_rounding_math;\
13716 flag_trapv = saved_trapv;\
13717 folding_initializer = saved_folding_initializer;
13719 tree
13720 fold_build1_initializer (enum tree_code code, tree type, tree op)
13722 tree result;
13723 START_FOLD_INIT;
13725 result = fold_build1 (code, type, op);
13727 END_FOLD_INIT;
13728 return result;
13731 tree
13732 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13734 tree result;
13735 START_FOLD_INIT;
13737 result = fold_build2 (code, type, op0, op1);
13739 END_FOLD_INIT;
13740 return result;
13743 tree
13744 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13745 tree op2)
13747 tree result;
13748 START_FOLD_INIT;
13750 result = fold_build3 (code, type, op0, op1, op2);
13752 END_FOLD_INIT;
13753 return result;
13756 tree
13757 fold_build_call_array_initializer (tree type, tree fn,
13758 int nargs, tree *argarray)
13760 tree result;
13761 START_FOLD_INIT;
13763 result = fold_build_call_array (type, fn, nargs, argarray);
13765 END_FOLD_INIT;
13766 return result;
13769 #undef START_FOLD_INIT
13770 #undef END_FOLD_INIT
13772 /* Determine if first argument is a multiple of second argument. Return 0 if
13773 it is not, or we cannot easily determined it to be.
13775 An example of the sort of thing we care about (at this point; this routine
13776 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13777 fold cases do now) is discovering that
13779 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13781 is a multiple of
13783 SAVE_EXPR (J * 8)
13785 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13787 This code also handles discovering that
13789 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13791 is a multiple of 8 so we don't have to worry about dealing with a
13792 possible remainder.
13794 Note that we *look* inside a SAVE_EXPR only to determine how it was
13795 calculated; it is not safe for fold to do much of anything else with the
13796 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13797 at run time. For example, the latter example above *cannot* be implemented
13798 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13799 evaluation time of the original SAVE_EXPR is not necessarily the same at
13800 the time the new expression is evaluated. The only optimization of this
13801 sort that would be valid is changing
13803 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13805 divided by 8 to
13807 SAVE_EXPR (I) * SAVE_EXPR (J)
13809 (where the same SAVE_EXPR (J) is used in the original and the
13810 transformed version). */
13813 multiple_of_p (tree type, const_tree top, const_tree bottom)
13815 if (operand_equal_p (top, bottom, 0))
13816 return 1;
13818 if (TREE_CODE (type) != INTEGER_TYPE)
13819 return 0;
13821 switch (TREE_CODE (top))
13823 case BIT_AND_EXPR:
13824 /* Bitwise and provides a power of two multiple. If the mask is
13825 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13826 if (!integer_pow2p (bottom))
13827 return 0;
13828 /* FALLTHRU */
13830 case MULT_EXPR:
13831 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13832 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13834 case PLUS_EXPR:
13835 case MINUS_EXPR:
13836 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13837 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13839 case LSHIFT_EXPR:
13840 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13842 tree op1, t1;
13844 op1 = TREE_OPERAND (top, 1);
13845 /* const_binop may not detect overflow correctly,
13846 so check for it explicitly here. */
13847 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13848 > TREE_INT_CST_LOW (op1)
13849 && TREE_INT_CST_HIGH (op1) == 0
13850 && 0 != (t1 = fold_convert (type,
13851 const_binop (LSHIFT_EXPR,
13852 size_one_node,
13853 op1, 0)))
13854 && !TREE_OVERFLOW (t1))
13855 return multiple_of_p (type, t1, bottom);
13857 return 0;
13859 case NOP_EXPR:
13860 /* Can't handle conversions from non-integral or wider integral type. */
13861 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13862 || (TYPE_PRECISION (type)
13863 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13864 return 0;
13866 /* .. fall through ... */
13868 case SAVE_EXPR:
13869 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13871 case INTEGER_CST:
13872 if (TREE_CODE (bottom) != INTEGER_CST
13873 || integer_zerop (bottom)
13874 || (TYPE_UNSIGNED (type)
13875 && (tree_int_cst_sgn (top) < 0
13876 || tree_int_cst_sgn (bottom) < 0)))
13877 return 0;
13878 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13879 top, bottom, 0));
13881 default:
13882 return 0;
13886 /* Return true if CODE or TYPE is known to be non-negative. */
13888 static bool
13889 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13891 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13892 && truth_value_p (code))
13893 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13894 have a signed:1 type (where the value is -1 and 0). */
13895 return true;
13896 return false;
13899 /* Return true if (CODE OP0) is known to be non-negative. If the return
13900 value is based on the assumption that signed overflow is undefined,
13901 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13902 *STRICT_OVERFLOW_P. */
13904 bool
13905 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13906 bool *strict_overflow_p)
13908 if (TYPE_UNSIGNED (type))
13909 return true;
13911 switch (code)
13913 case ABS_EXPR:
13914 /* We can't return 1 if flag_wrapv is set because
13915 ABS_EXPR<INT_MIN> = INT_MIN. */
13916 if (!INTEGRAL_TYPE_P (type))
13917 return true;
13918 if (TYPE_OVERFLOW_UNDEFINED (type))
13920 *strict_overflow_p = true;
13921 return true;
13923 break;
13925 case NON_LVALUE_EXPR:
13926 case FLOAT_EXPR:
13927 case FIX_TRUNC_EXPR:
13928 return tree_expr_nonnegative_warnv_p (op0,
13929 strict_overflow_p);
13931 case NOP_EXPR:
13933 tree inner_type = TREE_TYPE (op0);
13934 tree outer_type = type;
13936 if (TREE_CODE (outer_type) == REAL_TYPE)
13938 if (TREE_CODE (inner_type) == REAL_TYPE)
13939 return tree_expr_nonnegative_warnv_p (op0,
13940 strict_overflow_p);
13941 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13943 if (TYPE_UNSIGNED (inner_type))
13944 return true;
13945 return tree_expr_nonnegative_warnv_p (op0,
13946 strict_overflow_p);
13949 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13951 if (TREE_CODE (inner_type) == REAL_TYPE)
13952 return tree_expr_nonnegative_warnv_p (op0,
13953 strict_overflow_p);
13954 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13955 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13956 && TYPE_UNSIGNED (inner_type);
13959 break;
13961 default:
13962 return tree_simple_nonnegative_warnv_p (code, type);
13965 /* We don't know sign of `t', so be conservative and return false. */
13966 return false;
13969 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
13970 value is based on the assumption that signed overflow is undefined,
13971 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13972 *STRICT_OVERFLOW_P. */
13974 bool
13975 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13976 tree op1, bool *strict_overflow_p)
13978 if (TYPE_UNSIGNED (type))
13979 return true;
13981 switch (code)
13983 case POINTER_PLUS_EXPR:
13984 case PLUS_EXPR:
13985 if (FLOAT_TYPE_P (type))
13986 return (tree_expr_nonnegative_warnv_p (op0,
13987 strict_overflow_p)
13988 && tree_expr_nonnegative_warnv_p (op1,
13989 strict_overflow_p));
13991 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13992 both unsigned and at least 2 bits shorter than the result. */
13993 if (TREE_CODE (type) == INTEGER_TYPE
13994 && TREE_CODE (op0) == NOP_EXPR
13995 && TREE_CODE (op1) == NOP_EXPR)
13997 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13998 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13999 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14000 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14002 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14003 TYPE_PRECISION (inner2)) + 1;
14004 return prec < TYPE_PRECISION (type);
14007 break;
14009 case MULT_EXPR:
14010 if (FLOAT_TYPE_P (type))
14012 /* x * x for floating point x is always non-negative. */
14013 if (operand_equal_p (op0, op1, 0))
14014 return true;
14015 return (tree_expr_nonnegative_warnv_p (op0,
14016 strict_overflow_p)
14017 && tree_expr_nonnegative_warnv_p (op1,
14018 strict_overflow_p));
14021 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14022 both unsigned and their total bits is shorter than the result. */
14023 if (TREE_CODE (type) == INTEGER_TYPE
14024 && TREE_CODE (op0) == NOP_EXPR
14025 && TREE_CODE (op1) == NOP_EXPR)
14027 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14028 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14029 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14030 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14031 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
14032 < TYPE_PRECISION (type);
14034 return false;
14036 case BIT_AND_EXPR:
14037 case MAX_EXPR:
14038 return (tree_expr_nonnegative_warnv_p (op0,
14039 strict_overflow_p)
14040 || tree_expr_nonnegative_warnv_p (op1,
14041 strict_overflow_p));
14043 case BIT_IOR_EXPR:
14044 case BIT_XOR_EXPR:
14045 case MIN_EXPR:
14046 case RDIV_EXPR:
14047 case TRUNC_DIV_EXPR:
14048 case CEIL_DIV_EXPR:
14049 case FLOOR_DIV_EXPR:
14050 case ROUND_DIV_EXPR:
14051 return (tree_expr_nonnegative_warnv_p (op0,
14052 strict_overflow_p)
14053 && tree_expr_nonnegative_warnv_p (op1,
14054 strict_overflow_p));
14056 case TRUNC_MOD_EXPR:
14057 case CEIL_MOD_EXPR:
14058 case FLOOR_MOD_EXPR:
14059 case ROUND_MOD_EXPR:
14060 return tree_expr_nonnegative_warnv_p (op0,
14061 strict_overflow_p);
14062 default:
14063 return tree_simple_nonnegative_warnv_p (code, type);
14066 /* We don't know sign of `t', so be conservative and return false. */
14067 return false;
14070 /* Return true if T is known to be non-negative. If the return
14071 value is based on the assumption that signed overflow is undefined,
14072 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14073 *STRICT_OVERFLOW_P. */
14075 bool
14076 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14078 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14079 return true;
14081 switch (TREE_CODE (t))
14083 case INTEGER_CST:
14084 return tree_int_cst_sgn (t) >= 0;
14086 case REAL_CST:
14087 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14089 case FIXED_CST:
14090 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14092 case COND_EXPR:
14093 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14094 strict_overflow_p)
14095 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14096 strict_overflow_p));
14097 default:
14098 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14099 TREE_TYPE (t));
14101 /* We don't know sign of `t', so be conservative and return false. */
14102 return false;
14105 /* Return true if T is known to be non-negative. If the return
14106 value is based on the assumption that signed overflow is undefined,
14107 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14108 *STRICT_OVERFLOW_P. */
14110 bool
14111 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14112 tree arg0, tree arg1, bool *strict_overflow_p)
14114 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14115 switch (DECL_FUNCTION_CODE (fndecl))
14117 CASE_FLT_FN (BUILT_IN_ACOS):
14118 CASE_FLT_FN (BUILT_IN_ACOSH):
14119 CASE_FLT_FN (BUILT_IN_CABS):
14120 CASE_FLT_FN (BUILT_IN_COSH):
14121 CASE_FLT_FN (BUILT_IN_ERFC):
14122 CASE_FLT_FN (BUILT_IN_EXP):
14123 CASE_FLT_FN (BUILT_IN_EXP10):
14124 CASE_FLT_FN (BUILT_IN_EXP2):
14125 CASE_FLT_FN (BUILT_IN_FABS):
14126 CASE_FLT_FN (BUILT_IN_FDIM):
14127 CASE_FLT_FN (BUILT_IN_HYPOT):
14128 CASE_FLT_FN (BUILT_IN_POW10):
14129 CASE_INT_FN (BUILT_IN_FFS):
14130 CASE_INT_FN (BUILT_IN_PARITY):
14131 CASE_INT_FN (BUILT_IN_POPCOUNT):
14132 case BUILT_IN_BSWAP32:
14133 case BUILT_IN_BSWAP64:
14134 /* Always true. */
14135 return true;
14137 CASE_FLT_FN (BUILT_IN_SQRT):
14138 /* sqrt(-0.0) is -0.0. */
14139 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14140 return true;
14141 return tree_expr_nonnegative_warnv_p (arg0,
14142 strict_overflow_p);
14144 CASE_FLT_FN (BUILT_IN_ASINH):
14145 CASE_FLT_FN (BUILT_IN_ATAN):
14146 CASE_FLT_FN (BUILT_IN_ATANH):
14147 CASE_FLT_FN (BUILT_IN_CBRT):
14148 CASE_FLT_FN (BUILT_IN_CEIL):
14149 CASE_FLT_FN (BUILT_IN_ERF):
14150 CASE_FLT_FN (BUILT_IN_EXPM1):
14151 CASE_FLT_FN (BUILT_IN_FLOOR):
14152 CASE_FLT_FN (BUILT_IN_FMOD):
14153 CASE_FLT_FN (BUILT_IN_FREXP):
14154 CASE_FLT_FN (BUILT_IN_LCEIL):
14155 CASE_FLT_FN (BUILT_IN_LDEXP):
14156 CASE_FLT_FN (BUILT_IN_LFLOOR):
14157 CASE_FLT_FN (BUILT_IN_LLCEIL):
14158 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14159 CASE_FLT_FN (BUILT_IN_LLRINT):
14160 CASE_FLT_FN (BUILT_IN_LLROUND):
14161 CASE_FLT_FN (BUILT_IN_LRINT):
14162 CASE_FLT_FN (BUILT_IN_LROUND):
14163 CASE_FLT_FN (BUILT_IN_MODF):
14164 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14165 CASE_FLT_FN (BUILT_IN_RINT):
14166 CASE_FLT_FN (BUILT_IN_ROUND):
14167 CASE_FLT_FN (BUILT_IN_SCALB):
14168 CASE_FLT_FN (BUILT_IN_SCALBLN):
14169 CASE_FLT_FN (BUILT_IN_SCALBN):
14170 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14171 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14172 CASE_FLT_FN (BUILT_IN_SINH):
14173 CASE_FLT_FN (BUILT_IN_TANH):
14174 CASE_FLT_FN (BUILT_IN_TRUNC):
14175 /* True if the 1st argument is nonnegative. */
14176 return tree_expr_nonnegative_warnv_p (arg0,
14177 strict_overflow_p);
14179 CASE_FLT_FN (BUILT_IN_FMAX):
14180 /* True if the 1st OR 2nd arguments are nonnegative. */
14181 return (tree_expr_nonnegative_warnv_p (arg0,
14182 strict_overflow_p)
14183 || (tree_expr_nonnegative_warnv_p (arg1,
14184 strict_overflow_p)));
14186 CASE_FLT_FN (BUILT_IN_FMIN):
14187 /* True if the 1st AND 2nd arguments are nonnegative. */
14188 return (tree_expr_nonnegative_warnv_p (arg0,
14189 strict_overflow_p)
14190 && (tree_expr_nonnegative_warnv_p (arg1,
14191 strict_overflow_p)));
14193 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14194 /* True if the 2nd argument is nonnegative. */
14195 return tree_expr_nonnegative_warnv_p (arg1,
14196 strict_overflow_p);
14198 CASE_FLT_FN (BUILT_IN_POWI):
14199 /* True if the 1st argument is nonnegative or the second
14200 argument is an even integer. */
14201 if (TREE_CODE (arg1) == INTEGER_CST
14202 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14203 return true;
14204 return tree_expr_nonnegative_warnv_p (arg0,
14205 strict_overflow_p);
14207 CASE_FLT_FN (BUILT_IN_POW):
14208 /* True if the 1st argument is nonnegative or the second
14209 argument is an even integer valued real. */
14210 if (TREE_CODE (arg1) == REAL_CST)
14212 REAL_VALUE_TYPE c;
14213 HOST_WIDE_INT n;
14215 c = TREE_REAL_CST (arg1);
14216 n = real_to_integer (&c);
14217 if ((n & 1) == 0)
14219 REAL_VALUE_TYPE cint;
14220 real_from_integer (&cint, VOIDmode, n,
14221 n < 0 ? -1 : 0, 0);
14222 if (real_identical (&c, &cint))
14223 return true;
14226 return tree_expr_nonnegative_warnv_p (arg0,
14227 strict_overflow_p);
14229 default:
14230 break;
14232 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14233 type);
14236 /* Return true if T is known to be non-negative. If the return
14237 value is based on the assumption that signed overflow is undefined,
14238 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14239 *STRICT_OVERFLOW_P. */
14241 bool
14242 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14244 enum tree_code code = TREE_CODE (t);
14245 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14246 return true;
14248 switch (code)
14250 case TARGET_EXPR:
14252 tree temp = TARGET_EXPR_SLOT (t);
14253 t = TARGET_EXPR_INITIAL (t);
14255 /* If the initializer is non-void, then it's a normal expression
14256 that will be assigned to the slot. */
14257 if (!VOID_TYPE_P (t))
14258 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14260 /* Otherwise, the initializer sets the slot in some way. One common
14261 way is an assignment statement at the end of the initializer. */
14262 while (1)
14264 if (TREE_CODE (t) == BIND_EXPR)
14265 t = expr_last (BIND_EXPR_BODY (t));
14266 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14267 || TREE_CODE (t) == TRY_CATCH_EXPR)
14268 t = expr_last (TREE_OPERAND (t, 0));
14269 else if (TREE_CODE (t) == STATEMENT_LIST)
14270 t = expr_last (t);
14271 else
14272 break;
14274 if (TREE_CODE (t) == MODIFY_EXPR
14275 && TREE_OPERAND (t, 0) == temp)
14276 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14277 strict_overflow_p);
14279 return false;
14282 case CALL_EXPR:
14284 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14285 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14287 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14288 get_callee_fndecl (t),
14289 arg0,
14290 arg1,
14291 strict_overflow_p);
14293 case COMPOUND_EXPR:
14294 case MODIFY_EXPR:
14295 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14296 strict_overflow_p);
14297 case BIND_EXPR:
14298 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14299 strict_overflow_p);
14300 case SAVE_EXPR:
14301 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14302 strict_overflow_p);
14304 default:
14305 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14306 TREE_TYPE (t));
14309 /* We don't know sign of `t', so be conservative and return false. */
14310 return false;
14313 /* Return true if T is known to be non-negative. If the return
14314 value is based on the assumption that signed overflow is undefined,
14315 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14316 *STRICT_OVERFLOW_P. */
14318 bool
14319 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14321 enum tree_code code;
14322 if (t == error_mark_node)
14323 return false;
14325 code = TREE_CODE (t);
14326 switch (TREE_CODE_CLASS (code))
14328 case tcc_binary:
14329 case tcc_comparison:
14330 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14331 TREE_TYPE (t),
14332 TREE_OPERAND (t, 0),
14333 TREE_OPERAND (t, 1),
14334 strict_overflow_p);
14336 case tcc_unary:
14337 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14338 TREE_TYPE (t),
14339 TREE_OPERAND (t, 0),
14340 strict_overflow_p);
14342 case tcc_constant:
14343 case tcc_declaration:
14344 case tcc_reference:
14345 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14347 default:
14348 break;
14351 switch (code)
14353 case TRUTH_AND_EXPR:
14354 case TRUTH_OR_EXPR:
14355 case TRUTH_XOR_EXPR:
14356 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14357 TREE_TYPE (t),
14358 TREE_OPERAND (t, 0),
14359 TREE_OPERAND (t, 1),
14360 strict_overflow_p);
14361 case TRUTH_NOT_EXPR:
14362 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14363 TREE_TYPE (t),
14364 TREE_OPERAND (t, 0),
14365 strict_overflow_p);
14367 case COND_EXPR:
14368 case CONSTRUCTOR:
14369 case OBJ_TYPE_REF:
14370 case ASSERT_EXPR:
14371 case ADDR_EXPR:
14372 case WITH_SIZE_EXPR:
14373 case EXC_PTR_EXPR:
14374 case SSA_NAME:
14375 case FILTER_EXPR:
14376 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14378 default:
14379 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14383 /* Return true if `t' is known to be non-negative. Handle warnings
14384 about undefined signed overflow. */
14386 bool
14387 tree_expr_nonnegative_p (tree t)
14389 bool ret, strict_overflow_p;
14391 strict_overflow_p = false;
14392 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14393 if (strict_overflow_p)
14394 fold_overflow_warning (("assuming signed overflow does not occur when "
14395 "determining that expression is always "
14396 "non-negative"),
14397 WARN_STRICT_OVERFLOW_MISC);
14398 return ret;
14402 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14403 For floating point we further ensure that T is not denormal.
14404 Similar logic is present in nonzero_address in rtlanal.h.
14406 If the return value is based on the assumption that signed overflow
14407 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14408 change *STRICT_OVERFLOW_P. */
14410 bool
14411 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14412 bool *strict_overflow_p)
14414 switch (code)
14416 case ABS_EXPR:
14417 return tree_expr_nonzero_warnv_p (op0,
14418 strict_overflow_p);
14420 case NOP_EXPR:
14422 tree inner_type = TREE_TYPE (op0);
14423 tree outer_type = type;
14425 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14426 && tree_expr_nonzero_warnv_p (op0,
14427 strict_overflow_p));
14429 break;
14431 case NON_LVALUE_EXPR:
14432 return tree_expr_nonzero_warnv_p (op0,
14433 strict_overflow_p);
14435 default:
14436 break;
14439 return false;
14442 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14443 For floating point we further ensure that T is not denormal.
14444 Similar logic is present in nonzero_address in rtlanal.h.
14446 If the return value is based on the assumption that signed overflow
14447 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14448 change *STRICT_OVERFLOW_P. */
14450 bool
14451 tree_binary_nonzero_warnv_p (enum tree_code code,
14452 tree type,
14453 tree op0,
14454 tree op1, bool *strict_overflow_p)
14456 bool sub_strict_overflow_p;
14457 switch (code)
14459 case POINTER_PLUS_EXPR:
14460 case PLUS_EXPR:
14461 if (TYPE_OVERFLOW_UNDEFINED (type))
14463 /* With the presence of negative values it is hard
14464 to say something. */
14465 sub_strict_overflow_p = false;
14466 if (!tree_expr_nonnegative_warnv_p (op0,
14467 &sub_strict_overflow_p)
14468 || !tree_expr_nonnegative_warnv_p (op1,
14469 &sub_strict_overflow_p))
14470 return false;
14471 /* One of operands must be positive and the other non-negative. */
14472 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14473 overflows, on a twos-complement machine the sum of two
14474 nonnegative numbers can never be zero. */
14475 return (tree_expr_nonzero_warnv_p (op0,
14476 strict_overflow_p)
14477 || tree_expr_nonzero_warnv_p (op1,
14478 strict_overflow_p));
14480 break;
14482 case MULT_EXPR:
14483 if (TYPE_OVERFLOW_UNDEFINED (type))
14485 if (tree_expr_nonzero_warnv_p (op0,
14486 strict_overflow_p)
14487 && tree_expr_nonzero_warnv_p (op1,
14488 strict_overflow_p))
14490 *strict_overflow_p = true;
14491 return true;
14494 break;
14496 case MIN_EXPR:
14497 sub_strict_overflow_p = false;
14498 if (tree_expr_nonzero_warnv_p (op0,
14499 &sub_strict_overflow_p)
14500 && tree_expr_nonzero_warnv_p (op1,
14501 &sub_strict_overflow_p))
14503 if (sub_strict_overflow_p)
14504 *strict_overflow_p = true;
14506 break;
14508 case MAX_EXPR:
14509 sub_strict_overflow_p = false;
14510 if (tree_expr_nonzero_warnv_p (op0,
14511 &sub_strict_overflow_p))
14513 if (sub_strict_overflow_p)
14514 *strict_overflow_p = true;
14516 /* When both operands are nonzero, then MAX must be too. */
14517 if (tree_expr_nonzero_warnv_p (op1,
14518 strict_overflow_p))
14519 return true;
14521 /* MAX where operand 0 is positive is positive. */
14522 return tree_expr_nonnegative_warnv_p (op0,
14523 strict_overflow_p);
14525 /* MAX where operand 1 is positive is positive. */
14526 else if (tree_expr_nonzero_warnv_p (op1,
14527 &sub_strict_overflow_p)
14528 && tree_expr_nonnegative_warnv_p (op1,
14529 &sub_strict_overflow_p))
14531 if (sub_strict_overflow_p)
14532 *strict_overflow_p = true;
14533 return true;
14535 break;
14537 case BIT_IOR_EXPR:
14538 return (tree_expr_nonzero_warnv_p (op1,
14539 strict_overflow_p)
14540 || tree_expr_nonzero_warnv_p (op0,
14541 strict_overflow_p));
14543 default:
14544 break;
14547 return false;
14550 /* Return true when T is an address and is known to be nonzero.
14551 For floating point we further ensure that T is not denormal.
14552 Similar logic is present in nonzero_address in rtlanal.h.
14554 If the return value is based on the assumption that signed overflow
14555 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14556 change *STRICT_OVERFLOW_P. */
14558 bool
14559 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14561 bool sub_strict_overflow_p;
14562 switch (TREE_CODE (t))
14564 case INTEGER_CST:
14565 return !integer_zerop (t);
14567 case ADDR_EXPR:
14569 tree base = get_base_address (TREE_OPERAND (t, 0));
14571 if (!base)
14572 return false;
14574 /* Weak declarations may link to NULL. */
14575 if (VAR_OR_FUNCTION_DECL_P (base))
14576 return !DECL_WEAK (base);
14578 /* Constants are never weak. */
14579 if (CONSTANT_CLASS_P (base))
14580 return true;
14582 return false;
14585 case COND_EXPR:
14586 sub_strict_overflow_p = false;
14587 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14588 &sub_strict_overflow_p)
14589 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14590 &sub_strict_overflow_p))
14592 if (sub_strict_overflow_p)
14593 *strict_overflow_p = true;
14594 return true;
14596 break;
14598 default:
14599 break;
14601 return false;
14604 /* Return true when T is an address and is known to be nonzero.
14605 For floating point we further ensure that T is not denormal.
14606 Similar logic is present in nonzero_address in rtlanal.h.
14608 If the return value is based on the assumption that signed overflow
14609 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14610 change *STRICT_OVERFLOW_P. */
14612 bool
14613 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14615 tree type = TREE_TYPE (t);
14616 enum tree_code code;
14618 /* Doing something useful for floating point would need more work. */
14619 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14620 return false;
14622 code = TREE_CODE (t);
14623 switch (TREE_CODE_CLASS (code))
14625 case tcc_unary:
14626 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14627 strict_overflow_p);
14628 case tcc_binary:
14629 case tcc_comparison:
14630 return tree_binary_nonzero_warnv_p (code, type,
14631 TREE_OPERAND (t, 0),
14632 TREE_OPERAND (t, 1),
14633 strict_overflow_p);
14634 case tcc_constant:
14635 case tcc_declaration:
14636 case tcc_reference:
14637 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14639 default:
14640 break;
14643 switch (code)
14645 case TRUTH_NOT_EXPR:
14646 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14647 strict_overflow_p);
14649 case TRUTH_AND_EXPR:
14650 case TRUTH_OR_EXPR:
14651 case TRUTH_XOR_EXPR:
14652 return tree_binary_nonzero_warnv_p (code, type,
14653 TREE_OPERAND (t, 0),
14654 TREE_OPERAND (t, 1),
14655 strict_overflow_p);
14657 case COND_EXPR:
14658 case CONSTRUCTOR:
14659 case OBJ_TYPE_REF:
14660 case ASSERT_EXPR:
14661 case ADDR_EXPR:
14662 case WITH_SIZE_EXPR:
14663 case EXC_PTR_EXPR:
14664 case SSA_NAME:
14665 case FILTER_EXPR:
14666 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14668 case COMPOUND_EXPR:
14669 case MODIFY_EXPR:
14670 case BIND_EXPR:
14671 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14672 strict_overflow_p);
14674 case SAVE_EXPR:
14675 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14676 strict_overflow_p);
14678 case CALL_EXPR:
14679 return alloca_call_p (t);
14681 default:
14682 break;
14684 return false;
14687 /* Return true when T is an address and is known to be nonzero.
14688 Handle warnings about undefined signed overflow. */
14690 bool
14691 tree_expr_nonzero_p (tree t)
14693 bool ret, strict_overflow_p;
14695 strict_overflow_p = false;
14696 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14697 if (strict_overflow_p)
14698 fold_overflow_warning (("assuming signed overflow does not occur when "
14699 "determining that expression is always "
14700 "non-zero"),
14701 WARN_STRICT_OVERFLOW_MISC);
14702 return ret;
14705 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14706 attempt to fold the expression to a constant without modifying TYPE,
14707 OP0 or OP1.
14709 If the expression could be simplified to a constant, then return
14710 the constant. If the expression would not be simplified to a
14711 constant, then return NULL_TREE. */
14713 tree
14714 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14716 tree tem = fold_binary (code, type, op0, op1);
14717 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14720 /* Given the components of a unary expression CODE, TYPE and OP0,
14721 attempt to fold the expression to a constant without modifying
14722 TYPE or OP0.
14724 If the expression could be simplified to a constant, then return
14725 the constant. If the expression would not be simplified to a
14726 constant, then return NULL_TREE. */
14728 tree
14729 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14731 tree tem = fold_unary (code, type, op0);
14732 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14735 /* If EXP represents referencing an element in a constant string
14736 (either via pointer arithmetic or array indexing), return the
14737 tree representing the value accessed, otherwise return NULL. */
14739 tree
14740 fold_read_from_constant_string (tree exp)
14742 if ((TREE_CODE (exp) == INDIRECT_REF
14743 || TREE_CODE (exp) == ARRAY_REF)
14744 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14746 tree exp1 = TREE_OPERAND (exp, 0);
14747 tree index;
14748 tree string;
14750 if (TREE_CODE (exp) == INDIRECT_REF)
14751 string = string_constant (exp1, &index);
14752 else
14754 tree low_bound = array_ref_low_bound (exp);
14755 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14757 /* Optimize the special-case of a zero lower bound.
14759 We convert the low_bound to sizetype to avoid some problems
14760 with constant folding. (E.g. suppose the lower bound is 1,
14761 and its mode is QI. Without the conversion,l (ARRAY
14762 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14763 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
14764 if (! integer_zerop (low_bound))
14765 index = size_diffop (index, fold_convert (sizetype, low_bound));
14767 string = exp1;
14770 if (string
14771 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14772 && TREE_CODE (string) == STRING_CST
14773 && TREE_CODE (index) == INTEGER_CST
14774 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14775 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14776 == MODE_INT)
14777 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14778 return build_int_cst_type (TREE_TYPE (exp),
14779 (TREE_STRING_POINTER (string)
14780 [TREE_INT_CST_LOW (index)]));
14782 return NULL;
14785 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14786 an integer constant, real, or fixed-point constant.
14788 TYPE is the type of the result. */
14790 static tree
14791 fold_negate_const (tree arg0, tree type)
14793 tree t = NULL_TREE;
14795 switch (TREE_CODE (arg0))
14797 case INTEGER_CST:
14799 unsigned HOST_WIDE_INT low;
14800 HOST_WIDE_INT high;
14801 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14802 TREE_INT_CST_HIGH (arg0),
14803 &low, &high);
14804 t = force_fit_type_double (type, low, high, 1,
14805 (overflow | TREE_OVERFLOW (arg0))
14806 && !TYPE_UNSIGNED (type));
14807 break;
14810 case REAL_CST:
14811 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14812 break;
14814 case FIXED_CST:
14816 FIXED_VALUE_TYPE f;
14817 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14818 &(TREE_FIXED_CST (arg0)), NULL,
14819 TYPE_SATURATING (type));
14820 t = build_fixed (type, f);
14821 /* Propagate overflow flags. */
14822 if (overflow_p | TREE_OVERFLOW (arg0))
14824 TREE_OVERFLOW (t) = 1;
14825 TREE_CONSTANT_OVERFLOW (t) = 1;
14827 else if (TREE_CONSTANT_OVERFLOW (arg0))
14828 TREE_CONSTANT_OVERFLOW (t) = 1;
14829 break;
14832 default:
14833 gcc_unreachable ();
14836 return t;
14839 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14840 an integer constant or real constant.
14842 TYPE is the type of the result. */
14844 tree
14845 fold_abs_const (tree arg0, tree type)
14847 tree t = NULL_TREE;
14849 switch (TREE_CODE (arg0))
14851 case INTEGER_CST:
14852 /* If the value is unsigned, then the absolute value is
14853 the same as the ordinary value. */
14854 if (TYPE_UNSIGNED (type))
14855 t = arg0;
14856 /* Similarly, if the value is non-negative. */
14857 else if (INT_CST_LT (integer_minus_one_node, arg0))
14858 t = arg0;
14859 /* If the value is negative, then the absolute value is
14860 its negation. */
14861 else
14863 unsigned HOST_WIDE_INT low;
14864 HOST_WIDE_INT high;
14865 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14866 TREE_INT_CST_HIGH (arg0),
14867 &low, &high);
14868 t = force_fit_type_double (type, low, high, -1,
14869 overflow | TREE_OVERFLOW (arg0));
14871 break;
14873 case REAL_CST:
14874 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14875 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14876 else
14877 t = arg0;
14878 break;
14880 default:
14881 gcc_unreachable ();
14884 return t;
14887 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14888 constant. TYPE is the type of the result. */
14890 static tree
14891 fold_not_const (tree arg0, tree type)
14893 tree t = NULL_TREE;
14895 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14897 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14898 ~TREE_INT_CST_HIGH (arg0), 0,
14899 TREE_OVERFLOW (arg0));
14901 return t;
14904 /* Given CODE, a relational operator, the target type, TYPE and two
14905 constant operands OP0 and OP1, return the result of the
14906 relational operation. If the result is not a compile time
14907 constant, then return NULL_TREE. */
14909 static tree
14910 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14912 int result, invert;
14914 /* From here on, the only cases we handle are when the result is
14915 known to be a constant. */
14917 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14919 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14920 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14922 /* Handle the cases where either operand is a NaN. */
14923 if (real_isnan (c0) || real_isnan (c1))
14925 switch (code)
14927 case EQ_EXPR:
14928 case ORDERED_EXPR:
14929 result = 0;
14930 break;
14932 case NE_EXPR:
14933 case UNORDERED_EXPR:
14934 case UNLT_EXPR:
14935 case UNLE_EXPR:
14936 case UNGT_EXPR:
14937 case UNGE_EXPR:
14938 case UNEQ_EXPR:
14939 result = 1;
14940 break;
14942 case LT_EXPR:
14943 case LE_EXPR:
14944 case GT_EXPR:
14945 case GE_EXPR:
14946 case LTGT_EXPR:
14947 if (flag_trapping_math)
14948 return NULL_TREE;
14949 result = 0;
14950 break;
14952 default:
14953 gcc_unreachable ();
14956 return constant_boolean_node (result, type);
14959 return constant_boolean_node (real_compare (code, c0, c1), type);
14962 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14964 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14965 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14966 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14969 /* Handle equality/inequality of complex constants. */
14970 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14972 tree rcond = fold_relational_const (code, type,
14973 TREE_REALPART (op0),
14974 TREE_REALPART (op1));
14975 tree icond = fold_relational_const (code, type,
14976 TREE_IMAGPART (op0),
14977 TREE_IMAGPART (op1));
14978 if (code == EQ_EXPR)
14979 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14980 else if (code == NE_EXPR)
14981 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14982 else
14983 return NULL_TREE;
14986 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14988 To compute GT, swap the arguments and do LT.
14989 To compute GE, do LT and invert the result.
14990 To compute LE, swap the arguments, do LT and invert the result.
14991 To compute NE, do EQ and invert the result.
14993 Therefore, the code below must handle only EQ and LT. */
14995 if (code == LE_EXPR || code == GT_EXPR)
14997 tree tem = op0;
14998 op0 = op1;
14999 op1 = tem;
15000 code = swap_tree_comparison (code);
15003 /* Note that it is safe to invert for real values here because we
15004 have already handled the one case that it matters. */
15006 invert = 0;
15007 if (code == NE_EXPR || code == GE_EXPR)
15009 invert = 1;
15010 code = invert_tree_comparison (code, false);
15013 /* Compute a result for LT or EQ if args permit;
15014 Otherwise return T. */
15015 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15017 if (code == EQ_EXPR)
15018 result = tree_int_cst_equal (op0, op1);
15019 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15020 result = INT_CST_LT_UNSIGNED (op0, op1);
15021 else
15022 result = INT_CST_LT (op0, op1);
15024 else
15025 return NULL_TREE;
15027 if (invert)
15028 result ^= 1;
15029 return constant_boolean_node (result, type);
15032 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15033 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15034 itself. */
15036 tree
15037 fold_build_cleanup_point_expr (tree type, tree expr)
15039 /* If the expression does not have side effects then we don't have to wrap
15040 it with a cleanup point expression. */
15041 if (!TREE_SIDE_EFFECTS (expr))
15042 return expr;
15044 /* If the expression is a return, check to see if the expression inside the
15045 return has no side effects or the right hand side of the modify expression
15046 inside the return. If either don't have side effects set we don't need to
15047 wrap the expression in a cleanup point expression. Note we don't check the
15048 left hand side of the modify because it should always be a return decl. */
15049 if (TREE_CODE (expr) == RETURN_EXPR)
15051 tree op = TREE_OPERAND (expr, 0);
15052 if (!op || !TREE_SIDE_EFFECTS (op))
15053 return expr;
15054 op = TREE_OPERAND (op, 1);
15055 if (!TREE_SIDE_EFFECTS (op))
15056 return expr;
15059 return build1 (CLEANUP_POINT_EXPR, type, expr);
15062 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15063 of an indirection through OP0, or NULL_TREE if no simplification is
15064 possible. */
15066 tree
15067 fold_indirect_ref_1 (tree type, tree op0)
15069 tree sub = op0;
15070 tree subtype;
15072 STRIP_NOPS (sub);
15073 subtype = TREE_TYPE (sub);
15074 if (!POINTER_TYPE_P (subtype))
15075 return NULL_TREE;
15077 if (TREE_CODE (sub) == ADDR_EXPR)
15079 tree op = TREE_OPERAND (sub, 0);
15080 tree optype = TREE_TYPE (op);
15081 /* *&CONST_DECL -> to the value of the const decl. */
15082 if (TREE_CODE (op) == CONST_DECL)
15083 return DECL_INITIAL (op);
15084 /* *&p => p; make sure to handle *&"str"[cst] here. */
15085 if (type == optype)
15087 tree fop = fold_read_from_constant_string (op);
15088 if (fop)
15089 return fop;
15090 else
15091 return op;
15093 /* *(foo *)&fooarray => fooarray[0] */
15094 else if (TREE_CODE (optype) == ARRAY_TYPE
15095 && type == TREE_TYPE (optype))
15097 tree type_domain = TYPE_DOMAIN (optype);
15098 tree min_val = size_zero_node;
15099 if (type_domain && TYPE_MIN_VALUE (type_domain))
15100 min_val = TYPE_MIN_VALUE (type_domain);
15101 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15103 /* *(foo *)&complexfoo => __real__ complexfoo */
15104 else if (TREE_CODE (optype) == COMPLEX_TYPE
15105 && type == TREE_TYPE (optype))
15106 return fold_build1 (REALPART_EXPR, type, op);
15107 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15108 else if (TREE_CODE (optype) == VECTOR_TYPE
15109 && type == TREE_TYPE (optype))
15111 tree part_width = TYPE_SIZE (type);
15112 tree index = bitsize_int (0);
15113 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15117 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15118 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15119 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15121 tree op00 = TREE_OPERAND (sub, 0);
15122 tree op01 = TREE_OPERAND (sub, 1);
15123 tree op00type;
15125 STRIP_NOPS (op00);
15126 op00type = TREE_TYPE (op00);
15127 if (TREE_CODE (op00) == ADDR_EXPR
15128 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15129 && type == TREE_TYPE (TREE_TYPE (op00type)))
15131 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15132 tree part_width = TYPE_SIZE (type);
15133 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15134 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15135 tree index = bitsize_int (indexi);
15137 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15138 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15139 part_width, index);
15145 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15146 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15147 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15149 tree op00 = TREE_OPERAND (sub, 0);
15150 tree op01 = TREE_OPERAND (sub, 1);
15151 tree op00type;
15153 STRIP_NOPS (op00);
15154 op00type = TREE_TYPE (op00);
15155 if (TREE_CODE (op00) == ADDR_EXPR
15156 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15157 && type == TREE_TYPE (TREE_TYPE (op00type)))
15159 tree size = TYPE_SIZE_UNIT (type);
15160 if (tree_int_cst_equal (size, op01))
15161 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15165 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15166 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15167 && type == TREE_TYPE (TREE_TYPE (subtype)))
15169 tree type_domain;
15170 tree min_val = size_zero_node;
15171 sub = build_fold_indirect_ref (sub);
15172 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15173 if (type_domain && TYPE_MIN_VALUE (type_domain))
15174 min_val = TYPE_MIN_VALUE (type_domain);
15175 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15178 return NULL_TREE;
15181 /* Builds an expression for an indirection through T, simplifying some
15182 cases. */
15184 tree
15185 build_fold_indirect_ref (tree t)
15187 tree type = TREE_TYPE (TREE_TYPE (t));
15188 tree sub = fold_indirect_ref_1 (type, t);
15190 if (sub)
15191 return sub;
15192 else
15193 return build1 (INDIRECT_REF, type, t);
15196 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15198 tree
15199 fold_indirect_ref (tree t)
15201 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15203 if (sub)
15204 return sub;
15205 else
15206 return t;
15209 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15210 whose result is ignored. The type of the returned tree need not be
15211 the same as the original expression. */
15213 tree
15214 fold_ignored_result (tree t)
15216 if (!TREE_SIDE_EFFECTS (t))
15217 return integer_zero_node;
15219 for (;;)
15220 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15222 case tcc_unary:
15223 t = TREE_OPERAND (t, 0);
15224 break;
15226 case tcc_binary:
15227 case tcc_comparison:
15228 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15229 t = TREE_OPERAND (t, 0);
15230 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15231 t = TREE_OPERAND (t, 1);
15232 else
15233 return t;
15234 break;
15236 case tcc_expression:
15237 switch (TREE_CODE (t))
15239 case COMPOUND_EXPR:
15240 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15241 return t;
15242 t = TREE_OPERAND (t, 0);
15243 break;
15245 case COND_EXPR:
15246 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15247 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15248 return t;
15249 t = TREE_OPERAND (t, 0);
15250 break;
15252 default:
15253 return t;
15255 break;
15257 default:
15258 return t;
15262 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15263 This can only be applied to objects of a sizetype. */
15265 tree
15266 round_up (tree value, int divisor)
15268 tree div = NULL_TREE;
15270 gcc_assert (divisor > 0);
15271 if (divisor == 1)
15272 return value;
15274 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15275 have to do anything. Only do this when we are not given a const,
15276 because in that case, this check is more expensive than just
15277 doing it. */
15278 if (TREE_CODE (value) != INTEGER_CST)
15280 div = build_int_cst (TREE_TYPE (value), divisor);
15282 if (multiple_of_p (TREE_TYPE (value), value, div))
15283 return value;
15286 /* If divisor is a power of two, simplify this to bit manipulation. */
15287 if (divisor == (divisor & -divisor))
15289 if (TREE_CODE (value) == INTEGER_CST)
15291 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15292 unsigned HOST_WIDE_INT high;
15293 bool overflow_p;
15295 if ((low & (divisor - 1)) == 0)
15296 return value;
15298 overflow_p = TREE_OVERFLOW (value);
15299 high = TREE_INT_CST_HIGH (value);
15300 low &= ~(divisor - 1);
15301 low += divisor;
15302 if (low == 0)
15304 high++;
15305 if (high == 0)
15306 overflow_p = true;
15309 return force_fit_type_double (TREE_TYPE (value), low, high,
15310 -1, overflow_p);
15312 else
15314 tree t;
15316 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15317 value = size_binop (PLUS_EXPR, value, t);
15318 t = build_int_cst (TREE_TYPE (value), -divisor);
15319 value = size_binop (BIT_AND_EXPR, value, t);
15322 else
15324 if (!div)
15325 div = build_int_cst (TREE_TYPE (value), divisor);
15326 value = size_binop (CEIL_DIV_EXPR, value, div);
15327 value = size_binop (MULT_EXPR, value, div);
15330 return value;
15333 /* Likewise, but round down. */
15335 tree
15336 round_down (tree value, int divisor)
15338 tree div = NULL_TREE;
15340 gcc_assert (divisor > 0);
15341 if (divisor == 1)
15342 return value;
15344 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15345 have to do anything. Only do this when we are not given a const,
15346 because in that case, this check is more expensive than just
15347 doing it. */
15348 if (TREE_CODE (value) != INTEGER_CST)
15350 div = build_int_cst (TREE_TYPE (value), divisor);
15352 if (multiple_of_p (TREE_TYPE (value), value, div))
15353 return value;
15356 /* If divisor is a power of two, simplify this to bit manipulation. */
15357 if (divisor == (divisor & -divisor))
15359 tree t;
15361 t = build_int_cst (TREE_TYPE (value), -divisor);
15362 value = size_binop (BIT_AND_EXPR, value, t);
15364 else
15366 if (!div)
15367 div = build_int_cst (TREE_TYPE (value), divisor);
15368 value = size_binop (FLOOR_DIV_EXPR, value, div);
15369 value = size_binop (MULT_EXPR, value, div);
15372 return value;
15375 /* Returns the pointer to the base of the object addressed by EXP and
15376 extracts the information about the offset of the access, storing it
15377 to PBITPOS and POFFSET. */
15379 static tree
15380 split_address_to_core_and_offset (tree exp,
15381 HOST_WIDE_INT *pbitpos, tree *poffset)
15383 tree core;
15384 enum machine_mode mode;
15385 int unsignedp, volatilep;
15386 HOST_WIDE_INT bitsize;
15388 if (TREE_CODE (exp) == ADDR_EXPR)
15390 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15391 poffset, &mode, &unsignedp, &volatilep,
15392 false);
15393 core = fold_addr_expr (core);
15395 else
15397 core = exp;
15398 *pbitpos = 0;
15399 *poffset = NULL_TREE;
15402 return core;
15405 /* Returns true if addresses of E1 and E2 differ by a constant, false
15406 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15408 bool
15409 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15411 tree core1, core2;
15412 HOST_WIDE_INT bitpos1, bitpos2;
15413 tree toffset1, toffset2, tdiff, type;
15415 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15416 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15418 if (bitpos1 % BITS_PER_UNIT != 0
15419 || bitpos2 % BITS_PER_UNIT != 0
15420 || !operand_equal_p (core1, core2, 0))
15421 return false;
15423 if (toffset1 && toffset2)
15425 type = TREE_TYPE (toffset1);
15426 if (type != TREE_TYPE (toffset2))
15427 toffset2 = fold_convert (type, toffset2);
15429 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15430 if (!cst_and_fits_in_hwi (tdiff))
15431 return false;
15433 *diff = int_cst_value (tdiff);
15435 else if (toffset1 || toffset2)
15437 /* If only one of the offsets is non-constant, the difference cannot
15438 be a constant. */
15439 return false;
15441 else
15442 *diff = 0;
15444 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15445 return true;
15448 /* Simplify the floating point expression EXP when the sign of the
15449 result is not significant. Return NULL_TREE if no simplification
15450 is possible. */
15452 tree
15453 fold_strip_sign_ops (tree exp)
15455 tree arg0, arg1;
15457 switch (TREE_CODE (exp))
15459 case ABS_EXPR:
15460 case NEGATE_EXPR:
15461 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15462 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15464 case MULT_EXPR:
15465 case RDIV_EXPR:
15466 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15467 return NULL_TREE;
15468 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15469 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15470 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15471 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15472 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15473 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15474 break;
15476 case COMPOUND_EXPR:
15477 arg0 = TREE_OPERAND (exp, 0);
15478 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15479 if (arg1)
15480 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15481 break;
15483 case COND_EXPR:
15484 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15485 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15486 if (arg0 || arg1)
15487 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15488 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15489 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15490 break;
15492 case CALL_EXPR:
15494 const enum built_in_function fcode = builtin_mathfn_code (exp);
15495 switch (fcode)
15497 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15498 /* Strip copysign function call, return the 1st argument. */
15499 arg0 = CALL_EXPR_ARG (exp, 0);
15500 arg1 = CALL_EXPR_ARG (exp, 1);
15501 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15503 default:
15504 /* Strip sign ops from the argument of "odd" math functions. */
15505 if (negate_mathfn_p (fcode))
15507 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15508 if (arg0)
15509 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15511 break;
15514 break;
15516 default:
15517 break;
15519 return NULL_TREE;