Rename serialize_sleep() to zsleep()
[dragonfly.git] / contrib / gcc-4.4 / gcc / fold-const.c
blob67c52b4a836905851bc5ac087ecb5c912f1363d8
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, 2009
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 operand_equal_for_comparison_p (tree, tree, tree);
109 static int twoval_comparison_p (tree, tree *, tree *, int *);
110 static tree eval_subst (tree, tree, tree, tree, tree);
111 static tree pedantic_omit_one_operand (tree, tree, tree);
112 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
113 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
114 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
115 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
116 enum machine_mode *, int *, int *,
117 tree *, tree *);
118 static int all_ones_mask_p (const_tree, int);
119 static tree sign_bit_p (tree, const_tree);
120 static int simple_operand_p (const_tree);
121 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
122 static tree range_predecessor (tree);
123 static tree range_successor (tree);
124 static tree make_range (tree, int *, tree *, tree *, bool *);
125 static tree build_range_check (tree, tree, int, tree, tree);
126 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
127 tree);
128 static tree fold_range_test (enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
136 tree, tree,
137 tree, tree, int);
138 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
139 tree, tree, tree);
140 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (const_tree, const_tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
148 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
150 and SUM1. Then this yields nonzero if overflow occurred during the
151 addition.
153 Overflow occurs if A and B have the same sign, but A and SUM differ in
154 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
155 sign. */
156 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
158 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
159 We do that by representing the two-word integer in 4 words, with only
160 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161 number. The value of the word is LOWPART + HIGHPART * BASE. */
163 #define LOWPART(x) \
164 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
165 #define HIGHPART(x) \
166 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
169 /* Unpack a two-word integer into 4 words.
170 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
171 WORDS points to the array of HOST_WIDE_INTs. */
173 static void
174 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
176 words[0] = LOWPART (low);
177 words[1] = HIGHPART (low);
178 words[2] = LOWPART (hi);
179 words[3] = HIGHPART (hi);
182 /* Pack an array of 4 words into a two-word integer.
183 WORDS points to the array of words.
184 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
186 static void
187 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
188 HOST_WIDE_INT *hi)
190 *low = words[0] + words[1] * BASE;
191 *hi = words[2] + words[3] * BASE;
194 /* Force the double-word integer L1, H1 to be within the range of the
195 integer type TYPE. Stores the properly truncated and sign-extended
196 double-word integer in *LV, *HV. Returns true if the operation
197 overflows, that is, argument and result are different. */
200 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
203 unsigned HOST_WIDE_INT low0 = l1;
204 HOST_WIDE_INT high0 = h1;
205 unsigned int prec;
206 int sign_extended_type;
208 if (POINTER_TYPE_P (type)
209 || TREE_CODE (type) == OFFSET_TYPE)
210 prec = POINTER_SIZE;
211 else
212 prec = TYPE_PRECISION (type);
214 /* Size types *are* sign extended. */
215 sign_extended_type = (!TYPE_UNSIGNED (type)
216 || (TREE_CODE (type) == INTEGER_TYPE
217 && TYPE_IS_SIZETYPE (type)));
219 /* First clear all bits that are beyond the type's precision. */
220 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
222 else if (prec > HOST_BITS_PER_WIDE_INT)
223 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
224 else
226 h1 = 0;
227 if (prec < HOST_BITS_PER_WIDE_INT)
228 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
231 /* Then do sign extension if necessary. */
232 if (!sign_extended_type)
233 /* No sign extension */;
234 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
235 /* Correct width already. */;
236 else if (prec > HOST_BITS_PER_WIDE_INT)
238 /* Sign extend top half? */
239 if (h1 & ((unsigned HOST_WIDE_INT)1
240 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
243 else if (prec == HOST_BITS_PER_WIDE_INT)
245 if ((HOST_WIDE_INT)l1 < 0)
246 h1 = -1;
248 else
250 /* Sign extend bottom half? */
251 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
253 h1 = -1;
254 l1 |= (HOST_WIDE_INT)(-1) << prec;
258 *lv = l1;
259 *hv = h1;
261 /* If the value didn't fit, signal overflow. */
262 return l1 != low0 || h1 != high0;
265 /* We force the double-int HIGH:LOW to the range of the type TYPE by
266 sign or zero extending it.
267 OVERFLOWABLE indicates if we are interested
268 in overflow of the value, when >0 we are only interested in signed
269 overflow, for <0 we are interested in any overflow. OVERFLOWED
270 indicates whether overflow has already occurred. CONST_OVERFLOWED
271 indicates whether constant overflow has already occurred. We force
272 T's value to be within range of T's type (by setting to 0 or 1 all
273 the bits outside the type's range). We set TREE_OVERFLOWED if,
274 OVERFLOWED is nonzero,
275 or OVERFLOWABLE is >0 and signed overflow occurs
276 or OVERFLOWABLE is <0 and any overflow occurs
277 We return a new tree node for the extended double-int. The node
278 is shared if no overflow flags are set. */
280 tree
281 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282 HOST_WIDE_INT high, int overflowable,
283 bool overflowed)
285 int sign_extended_type;
286 bool overflow;
288 /* Size types *are* sign extended. */
289 sign_extended_type = (!TYPE_UNSIGNED (type)
290 || (TREE_CODE (type) == INTEGER_TYPE
291 && TYPE_IS_SIZETYPE (type)));
293 overflow = fit_double_type (low, high, &low, &high, type);
295 /* If we need to set overflow flags, return a new unshared node. */
296 if (overflowed || overflow)
298 if (overflowed
299 || overflowable < 0
300 || (overflowable > 0 && sign_extended_type))
302 tree t = make_node (INTEGER_CST);
303 TREE_INT_CST_LOW (t) = low;
304 TREE_INT_CST_HIGH (t) = high;
305 TREE_TYPE (t) = type;
306 TREE_OVERFLOW (t) = 1;
307 return t;
311 /* Else build a shared node. */
312 return build_int_cst_wide (type, low, high);
315 /* Add two doubleword integers with doubleword result.
316 Return nonzero if the operation overflows according to UNSIGNED_P.
317 Each argument is given as two `HOST_WIDE_INT' pieces.
318 One argument is L1 and H1; the other, L2 and H2.
319 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
322 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325 bool unsigned_p)
327 unsigned HOST_WIDE_INT l;
328 HOST_WIDE_INT h;
330 l = l1 + l2;
331 h = h1 + h2 + (l < l1);
333 *lv = l;
334 *hv = h;
336 if (unsigned_p)
337 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
338 else
339 return OVERFLOW_SUM_SIGN (h1, h2, h);
342 /* Negate a doubleword integer with doubleword result.
343 Return nonzero if the operation overflows, assuming it's signed.
344 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
345 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
348 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
349 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
351 if (l1 == 0)
353 *lv = 0;
354 *hv = - h1;
355 return (*hv & h1) < 0;
357 else
359 *lv = -l1;
360 *hv = ~h1;
361 return 0;
365 /* Multiply two doubleword integers with doubleword result.
366 Return nonzero if the operation overflows according to UNSIGNED_P.
367 Each argument is given as two `HOST_WIDE_INT' pieces.
368 One argument is L1 and H1; the other, L2 and H2.
369 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
372 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
373 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
374 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
375 bool unsigned_p)
377 HOST_WIDE_INT arg1[4];
378 HOST_WIDE_INT arg2[4];
379 HOST_WIDE_INT prod[4 * 2];
380 unsigned HOST_WIDE_INT carry;
381 int i, j, k;
382 unsigned HOST_WIDE_INT toplow, neglow;
383 HOST_WIDE_INT tophigh, neghigh;
385 encode (arg1, l1, h1);
386 encode (arg2, l2, h2);
388 memset (prod, 0, sizeof prod);
390 for (i = 0; i < 4; i++)
392 carry = 0;
393 for (j = 0; j < 4; j++)
395 k = i + j;
396 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
397 carry += arg1[i] * arg2[j];
398 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
399 carry += prod[k];
400 prod[k] = LOWPART (carry);
401 carry = HIGHPART (carry);
403 prod[i + 4] = carry;
406 decode (prod, lv, hv);
407 decode (prod + 4, &toplow, &tophigh);
409 /* Unsigned overflow is immediate. */
410 if (unsigned_p)
411 return (toplow | tophigh) != 0;
413 /* Check for signed overflow by calculating the signed representation of the
414 top half of the result; it should agree with the low half's sign bit. */
415 if (h1 < 0)
417 neg_double (l2, h2, &neglow, &neghigh);
418 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
420 if (h2 < 0)
422 neg_double (l1, h1, &neglow, &neghigh);
423 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
425 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
428 /* Shift the doubleword integer in L1, H1 left by COUNT places
429 keeping only PREC bits of result.
430 Shift right if COUNT is negative.
431 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
432 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
434 void
435 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
436 HOST_WIDE_INT count, unsigned int prec,
437 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
439 unsigned HOST_WIDE_INT signmask;
441 if (count < 0)
443 rshift_double (l1, h1, -count, prec, lv, hv, arith);
444 return;
447 if (SHIFT_COUNT_TRUNCATED)
448 count %= prec;
450 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
452 /* Shifting by the host word size is undefined according to the
453 ANSI standard, so we must handle this as a special case. */
454 *hv = 0;
455 *lv = 0;
457 else if (count >= HOST_BITS_PER_WIDE_INT)
459 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
460 *lv = 0;
462 else
464 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
465 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
466 *lv = l1 << count;
469 /* Sign extend all bits that are beyond the precision. */
471 signmask = -((prec > HOST_BITS_PER_WIDE_INT
472 ? ((unsigned HOST_WIDE_INT) *hv
473 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
474 : (*lv >> (prec - 1))) & 1);
476 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
478 else if (prec >= HOST_BITS_PER_WIDE_INT)
480 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
481 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
483 else
485 *hv = signmask;
486 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
487 *lv |= signmask << prec;
491 /* Shift the doubleword integer in L1, H1 right by COUNT places
492 keeping only PREC bits of result. COUNT must be positive.
493 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
494 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
496 void
497 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
498 HOST_WIDE_INT count, unsigned int prec,
499 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
500 int arith)
502 unsigned HOST_WIDE_INT signmask;
504 signmask = (arith
505 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
506 : 0);
508 if (SHIFT_COUNT_TRUNCATED)
509 count %= prec;
511 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
513 /* Shifting by the host word size is undefined according to the
514 ANSI standard, so we must handle this as a special case. */
515 *hv = 0;
516 *lv = 0;
518 else if (count >= HOST_BITS_PER_WIDE_INT)
520 *hv = 0;
521 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
523 else
525 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
526 *lv = ((l1 >> count)
527 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
530 /* Zero / sign extend all bits that are beyond the precision. */
532 if (count >= (HOST_WIDE_INT)prec)
534 *hv = signmask;
535 *lv = signmask;
537 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
539 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
541 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
542 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
544 else
546 *hv = signmask;
547 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
548 *lv |= signmask << (prec - count);
552 /* Rotate the doubleword integer in L1, H1 left by COUNT places
553 keeping only PREC bits of result.
554 Rotate right if COUNT is negative.
555 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
557 void
558 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
559 HOST_WIDE_INT count, unsigned int prec,
560 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
562 unsigned HOST_WIDE_INT s1l, s2l;
563 HOST_WIDE_INT s1h, s2h;
565 count %= prec;
566 if (count < 0)
567 count += prec;
569 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
570 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
571 *lv = s1l | s2l;
572 *hv = s1h | s2h;
575 /* Rotate the doubleword integer in L1, H1 left by COUNT places
576 keeping only PREC bits of result. COUNT must be positive.
577 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
579 void
580 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
581 HOST_WIDE_INT count, unsigned int prec,
582 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
584 unsigned HOST_WIDE_INT s1l, s2l;
585 HOST_WIDE_INT s1h, s2h;
587 count %= prec;
588 if (count < 0)
589 count += prec;
591 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
592 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
593 *lv = s1l | s2l;
594 *hv = s1h | s2h;
597 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
598 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
599 CODE is a tree code for a kind of division, one of
600 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
601 or EXACT_DIV_EXPR
602 It controls how the quotient is rounded to an integer.
603 Return nonzero if the operation overflows.
604 UNS nonzero says do unsigned division. */
607 div_and_round_double (enum tree_code code, int uns,
608 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
609 HOST_WIDE_INT hnum_orig,
610 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
611 HOST_WIDE_INT hden_orig,
612 unsigned HOST_WIDE_INT *lquo,
613 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
614 HOST_WIDE_INT *hrem)
616 int quo_neg = 0;
617 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
618 HOST_WIDE_INT den[4], quo[4];
619 int i, j;
620 unsigned HOST_WIDE_INT work;
621 unsigned HOST_WIDE_INT carry = 0;
622 unsigned HOST_WIDE_INT lnum = lnum_orig;
623 HOST_WIDE_INT hnum = hnum_orig;
624 unsigned HOST_WIDE_INT lden = lden_orig;
625 HOST_WIDE_INT hden = hden_orig;
626 int overflow = 0;
628 if (hden == 0 && lden == 0)
629 overflow = 1, lden = 1;
631 /* Calculate quotient sign and convert operands to unsigned. */
632 if (!uns)
634 if (hnum < 0)
636 quo_neg = ~ quo_neg;
637 /* (minimum integer) / (-1) is the only overflow case. */
638 if (neg_double (lnum, hnum, &lnum, &hnum)
639 && ((HOST_WIDE_INT) lden & hden) == -1)
640 overflow = 1;
642 if (hden < 0)
644 quo_neg = ~ quo_neg;
645 neg_double (lden, hden, &lden, &hden);
649 if (hnum == 0 && hden == 0)
650 { /* single precision */
651 *hquo = *hrem = 0;
652 /* This unsigned division rounds toward zero. */
653 *lquo = lnum / lden;
654 goto finish_up;
657 if (hnum == 0)
658 { /* trivial case: dividend < divisor */
659 /* hden != 0 already checked. */
660 *hquo = *lquo = 0;
661 *hrem = hnum;
662 *lrem = lnum;
663 goto finish_up;
666 memset (quo, 0, sizeof quo);
668 memset (num, 0, sizeof num); /* to zero 9th element */
669 memset (den, 0, sizeof den);
671 encode (num, lnum, hnum);
672 encode (den, lden, hden);
674 /* Special code for when the divisor < BASE. */
675 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
677 /* hnum != 0 already checked. */
678 for (i = 4 - 1; i >= 0; i--)
680 work = num[i] + carry * BASE;
681 quo[i] = work / lden;
682 carry = work % lden;
685 else
687 /* Full double precision division,
688 with thanks to Don Knuth's "Seminumerical Algorithms". */
689 int num_hi_sig, den_hi_sig;
690 unsigned HOST_WIDE_INT quo_est, scale;
692 /* Find the highest nonzero divisor digit. */
693 for (i = 4 - 1;; i--)
694 if (den[i] != 0)
696 den_hi_sig = i;
697 break;
700 /* Insure that the first digit of the divisor is at least BASE/2.
701 This is required by the quotient digit estimation algorithm. */
703 scale = BASE / (den[den_hi_sig] + 1);
704 if (scale > 1)
705 { /* scale divisor and dividend */
706 carry = 0;
707 for (i = 0; i <= 4 - 1; i++)
709 work = (num[i] * scale) + carry;
710 num[i] = LOWPART (work);
711 carry = HIGHPART (work);
714 num[4] = carry;
715 carry = 0;
716 for (i = 0; i <= 4 - 1; i++)
718 work = (den[i] * scale) + carry;
719 den[i] = LOWPART (work);
720 carry = HIGHPART (work);
721 if (den[i] != 0) den_hi_sig = i;
725 num_hi_sig = 4;
727 /* Main loop */
728 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
730 /* Guess the next quotient digit, quo_est, by dividing the first
731 two remaining dividend digits by the high order quotient digit.
732 quo_est is never low and is at most 2 high. */
733 unsigned HOST_WIDE_INT tmp;
735 num_hi_sig = i + den_hi_sig + 1;
736 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
737 if (num[num_hi_sig] != den[den_hi_sig])
738 quo_est = work / den[den_hi_sig];
739 else
740 quo_est = BASE - 1;
742 /* Refine quo_est so it's usually correct, and at most one high. */
743 tmp = work - quo_est * den[den_hi_sig];
744 if (tmp < BASE
745 && (den[den_hi_sig - 1] * quo_est
746 > (tmp * BASE + num[num_hi_sig - 2])))
747 quo_est--;
749 /* Try QUO_EST as the quotient digit, by multiplying the
750 divisor by QUO_EST and subtracting from the remaining dividend.
751 Keep in mind that QUO_EST is the I - 1st digit. */
753 carry = 0;
754 for (j = 0; j <= den_hi_sig; j++)
756 work = quo_est * den[j] + carry;
757 carry = HIGHPART (work);
758 work = num[i + j] - LOWPART (work);
759 num[i + j] = LOWPART (work);
760 carry += HIGHPART (work) != 0;
763 /* If quo_est was high by one, then num[i] went negative and
764 we need to correct things. */
765 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
767 quo_est--;
768 carry = 0; /* add divisor back in */
769 for (j = 0; j <= den_hi_sig; j++)
771 work = num[i + j] + den[j] + carry;
772 carry = HIGHPART (work);
773 num[i + j] = LOWPART (work);
776 num [num_hi_sig] += carry;
779 /* Store the quotient digit. */
780 quo[i] = quo_est;
784 decode (quo, lquo, hquo);
786 finish_up:
787 /* If result is negative, make it so. */
788 if (quo_neg)
789 neg_double (*lquo, *hquo, lquo, hquo);
791 /* Compute trial remainder: rem = num - (quo * den) */
792 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
793 neg_double (*lrem, *hrem, lrem, hrem);
794 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
796 switch (code)
798 case TRUNC_DIV_EXPR:
799 case TRUNC_MOD_EXPR: /* round toward zero */
800 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
801 return overflow;
803 case FLOOR_DIV_EXPR:
804 case FLOOR_MOD_EXPR: /* round toward negative infinity */
805 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
807 /* quo = quo - 1; */
808 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
809 lquo, hquo);
811 else
812 return overflow;
813 break;
815 case CEIL_DIV_EXPR:
816 case CEIL_MOD_EXPR: /* round toward positive infinity */
817 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
819 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
820 lquo, hquo);
822 else
823 return overflow;
824 break;
826 case ROUND_DIV_EXPR:
827 case ROUND_MOD_EXPR: /* round to closest integer */
829 unsigned HOST_WIDE_INT labs_rem = *lrem;
830 HOST_WIDE_INT habs_rem = *hrem;
831 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
832 HOST_WIDE_INT habs_den = hden, htwice;
834 /* Get absolute values. */
835 if (*hrem < 0)
836 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
837 if (hden < 0)
838 neg_double (lden, hden, &labs_den, &habs_den);
840 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
841 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
842 labs_rem, habs_rem, &ltwice, &htwice);
844 if (((unsigned HOST_WIDE_INT) habs_den
845 < (unsigned HOST_WIDE_INT) htwice)
846 || (((unsigned HOST_WIDE_INT) habs_den
847 == (unsigned HOST_WIDE_INT) htwice)
848 && (labs_den <= ltwice)))
850 if (*hquo < 0)
851 /* quo = quo - 1; */
852 add_double (*lquo, *hquo,
853 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
854 else
855 /* quo = quo + 1; */
856 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
857 lquo, hquo);
859 else
860 return overflow;
862 break;
864 default:
865 gcc_unreachable ();
868 /* Compute true remainder: rem = num - (quo * den) */
869 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
870 neg_double (*lrem, *hrem, lrem, hrem);
871 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
872 return overflow;
875 /* If ARG2 divides ARG1 with zero remainder, carries out the division
876 of type CODE and returns the quotient.
877 Otherwise returns NULL_TREE. */
879 static tree
880 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
882 unsigned HOST_WIDE_INT int1l, int2l;
883 HOST_WIDE_INT int1h, int2h;
884 unsigned HOST_WIDE_INT quol, reml;
885 HOST_WIDE_INT quoh, remh;
886 tree type = TREE_TYPE (arg1);
887 int uns = TYPE_UNSIGNED (type);
889 int1l = TREE_INT_CST_LOW (arg1);
890 int1h = TREE_INT_CST_HIGH (arg1);
891 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
892 &obj[some_exotic_number]. */
893 if (POINTER_TYPE_P (type))
895 uns = false;
896 type = signed_type_for (type);
897 fit_double_type (int1l, int1h, &int1l, &int1h,
898 type);
900 else
901 fit_double_type (int1l, int1h, &int1l, &int1h, type);
902 int2l = TREE_INT_CST_LOW (arg2);
903 int2h = TREE_INT_CST_HIGH (arg2);
905 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
906 &quol, &quoh, &reml, &remh);
907 if (remh != 0 || reml != 0)
908 return NULL_TREE;
910 return build_int_cst_wide (type, quol, quoh);
913 /* This is nonzero if we should defer warnings about undefined
914 overflow. This facility exists because these warnings are a
915 special case. The code to estimate loop iterations does not want
916 to issue any warnings, since it works with expressions which do not
917 occur in user code. Various bits of cleanup code call fold(), but
918 only use the result if it has certain characteristics (e.g., is a
919 constant); that code only wants to issue a warning if the result is
920 used. */
922 static int fold_deferring_overflow_warnings;
924 /* If a warning about undefined overflow is deferred, this is the
925 warning. Note that this may cause us to turn two warnings into
926 one, but that is fine since it is sufficient to only give one
927 warning per expression. */
929 static const char* fold_deferred_overflow_warning;
931 /* If a warning about undefined overflow is deferred, this is the
932 level at which the warning should be emitted. */
934 static enum warn_strict_overflow_code fold_deferred_overflow_code;
936 /* Start deferring overflow warnings. We could use a stack here to
937 permit nested calls, but at present it is not necessary. */
939 void
940 fold_defer_overflow_warnings (void)
942 ++fold_deferring_overflow_warnings;
945 /* Stop deferring overflow warnings. If there is a pending warning,
946 and ISSUE is true, then issue the warning if appropriate. STMT is
947 the statement with which the warning should be associated (used for
948 location information); STMT may be NULL. CODE is the level of the
949 warning--a warn_strict_overflow_code value. This function will use
950 the smaller of CODE and the deferred code when deciding whether to
951 issue the warning. CODE may be zero to mean to always use the
952 deferred code. */
954 void
955 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
957 const char *warnmsg;
958 location_t locus;
960 gcc_assert (fold_deferring_overflow_warnings > 0);
961 --fold_deferring_overflow_warnings;
962 if (fold_deferring_overflow_warnings > 0)
964 if (fold_deferred_overflow_warning != NULL
965 && code != 0
966 && code < (int) fold_deferred_overflow_code)
967 fold_deferred_overflow_code = code;
968 return;
971 warnmsg = fold_deferred_overflow_warning;
972 fold_deferred_overflow_warning = NULL;
974 if (!issue || warnmsg == NULL)
975 return;
977 if (gimple_no_warning_p (stmt))
978 return;
980 /* Use the smallest code level when deciding to issue the
981 warning. */
982 if (code == 0 || code > (int) fold_deferred_overflow_code)
983 code = fold_deferred_overflow_code;
985 if (!issue_strict_overflow_warning (code))
986 return;
988 if (stmt == NULL)
989 locus = input_location;
990 else
991 locus = gimple_location (stmt);
992 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
995 /* Stop deferring overflow warnings, ignoring any deferred
996 warnings. */
998 void
999 fold_undefer_and_ignore_overflow_warnings (void)
1001 fold_undefer_overflow_warnings (false, NULL, 0);
1004 /* Whether we are deferring overflow warnings. */
1006 bool
1007 fold_deferring_overflow_warnings_p (void)
1009 return fold_deferring_overflow_warnings > 0;
1012 /* This is called when we fold something based on the fact that signed
1013 overflow is undefined. */
1015 static void
1016 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1018 if (fold_deferring_overflow_warnings > 0)
1020 if (fold_deferred_overflow_warning == NULL
1021 || wc < fold_deferred_overflow_code)
1023 fold_deferred_overflow_warning = gmsgid;
1024 fold_deferred_overflow_code = wc;
1027 else if (issue_strict_overflow_warning (wc))
1028 warning (OPT_Wstrict_overflow, gmsgid);
1031 /* Return true if the built-in mathematical function specified by CODE
1032 is odd, i.e. -f(x) == f(-x). */
1034 static bool
1035 negate_mathfn_p (enum built_in_function code)
1037 switch (code)
1039 CASE_FLT_FN (BUILT_IN_ASIN):
1040 CASE_FLT_FN (BUILT_IN_ASINH):
1041 CASE_FLT_FN (BUILT_IN_ATAN):
1042 CASE_FLT_FN (BUILT_IN_ATANH):
1043 CASE_FLT_FN (BUILT_IN_CASIN):
1044 CASE_FLT_FN (BUILT_IN_CASINH):
1045 CASE_FLT_FN (BUILT_IN_CATAN):
1046 CASE_FLT_FN (BUILT_IN_CATANH):
1047 CASE_FLT_FN (BUILT_IN_CBRT):
1048 CASE_FLT_FN (BUILT_IN_CPROJ):
1049 CASE_FLT_FN (BUILT_IN_CSIN):
1050 CASE_FLT_FN (BUILT_IN_CSINH):
1051 CASE_FLT_FN (BUILT_IN_CTAN):
1052 CASE_FLT_FN (BUILT_IN_CTANH):
1053 CASE_FLT_FN (BUILT_IN_ERF):
1054 CASE_FLT_FN (BUILT_IN_LLROUND):
1055 CASE_FLT_FN (BUILT_IN_LROUND):
1056 CASE_FLT_FN (BUILT_IN_ROUND):
1057 CASE_FLT_FN (BUILT_IN_SIN):
1058 CASE_FLT_FN (BUILT_IN_SINH):
1059 CASE_FLT_FN (BUILT_IN_TAN):
1060 CASE_FLT_FN (BUILT_IN_TANH):
1061 CASE_FLT_FN (BUILT_IN_TRUNC):
1062 return true;
1064 CASE_FLT_FN (BUILT_IN_LLRINT):
1065 CASE_FLT_FN (BUILT_IN_LRINT):
1066 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1067 CASE_FLT_FN (BUILT_IN_RINT):
1068 return !flag_rounding_math;
1070 default:
1071 break;
1073 return false;
1076 /* Check whether we may negate an integer constant T without causing
1077 overflow. */
1079 bool
1080 may_negate_without_overflow_p (const_tree t)
1082 unsigned HOST_WIDE_INT val;
1083 unsigned int prec;
1084 tree type;
1086 gcc_assert (TREE_CODE (t) == INTEGER_CST);
1088 type = TREE_TYPE (t);
1089 if (TYPE_UNSIGNED (type))
1090 return false;
1092 prec = TYPE_PRECISION (type);
1093 if (prec > HOST_BITS_PER_WIDE_INT)
1095 if (TREE_INT_CST_LOW (t) != 0)
1096 return true;
1097 prec -= HOST_BITS_PER_WIDE_INT;
1098 val = TREE_INT_CST_HIGH (t);
1100 else
1101 val = TREE_INT_CST_LOW (t);
1102 if (prec < HOST_BITS_PER_WIDE_INT)
1103 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1104 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1107 /* Determine whether an expression T can be cheaply negated using
1108 the function negate_expr without introducing undefined overflow. */
1110 static bool
1111 negate_expr_p (tree t)
1113 tree type;
1115 if (t == 0)
1116 return false;
1118 type = TREE_TYPE (t);
1120 STRIP_SIGN_NOPS (t);
1121 switch (TREE_CODE (t))
1123 case INTEGER_CST:
1124 if (TYPE_OVERFLOW_WRAPS (type))
1125 return true;
1127 /* Check that -CST will not overflow type. */
1128 return may_negate_without_overflow_p (t);
1129 case BIT_NOT_EXPR:
1130 return (INTEGRAL_TYPE_P (type)
1131 && TYPE_OVERFLOW_WRAPS (type));
1133 case FIXED_CST:
1134 case REAL_CST:
1135 case NEGATE_EXPR:
1136 return true;
1138 case COMPLEX_CST:
1139 return negate_expr_p (TREE_REALPART (t))
1140 && negate_expr_p (TREE_IMAGPART (t));
1142 case COMPLEX_EXPR:
1143 return negate_expr_p (TREE_OPERAND (t, 0))
1144 && negate_expr_p (TREE_OPERAND (t, 1));
1146 case CONJ_EXPR:
1147 return negate_expr_p (TREE_OPERAND (t, 0));
1149 case PLUS_EXPR:
1150 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1151 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1152 return false;
1153 /* -(A + B) -> (-B) - A. */
1154 if (negate_expr_p (TREE_OPERAND (t, 1))
1155 && reorder_operands_p (TREE_OPERAND (t, 0),
1156 TREE_OPERAND (t, 1)))
1157 return true;
1158 /* -(A + B) -> (-A) - B. */
1159 return negate_expr_p (TREE_OPERAND (t, 0));
1161 case MINUS_EXPR:
1162 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1163 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1164 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1165 && reorder_operands_p (TREE_OPERAND (t, 0),
1166 TREE_OPERAND (t, 1));
1168 case MULT_EXPR:
1169 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1170 break;
1172 /* Fall through. */
1174 case RDIV_EXPR:
1175 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1176 return negate_expr_p (TREE_OPERAND (t, 1))
1177 || negate_expr_p (TREE_OPERAND (t, 0));
1178 break;
1180 case TRUNC_DIV_EXPR:
1181 case ROUND_DIV_EXPR:
1182 case FLOOR_DIV_EXPR:
1183 case CEIL_DIV_EXPR:
1184 case EXACT_DIV_EXPR:
1185 /* In general we can't negate A / B, because if A is INT_MIN and
1186 B is 1, we may turn this into INT_MIN / -1 which is undefined
1187 and actually traps on some architectures. But if overflow is
1188 undefined, we can negate, because - (INT_MIN / 1) is an
1189 overflow. */
1190 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1191 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1192 break;
1193 return negate_expr_p (TREE_OPERAND (t, 1))
1194 || negate_expr_p (TREE_OPERAND (t, 0));
1196 case NOP_EXPR:
1197 /* Negate -((double)float) as (double)(-float). */
1198 if (TREE_CODE (type) == REAL_TYPE)
1200 tree tem = strip_float_extensions (t);
1201 if (tem != t)
1202 return negate_expr_p (tem);
1204 break;
1206 case CALL_EXPR:
1207 /* Negate -f(x) as f(-x). */
1208 if (negate_mathfn_p (builtin_mathfn_code (t)))
1209 return negate_expr_p (CALL_EXPR_ARG (t, 0));
1210 break;
1212 case RSHIFT_EXPR:
1213 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1214 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1216 tree op1 = TREE_OPERAND (t, 1);
1217 if (TREE_INT_CST_HIGH (op1) == 0
1218 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1219 == TREE_INT_CST_LOW (op1))
1220 return true;
1222 break;
1224 default:
1225 break;
1227 return false;
1230 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1231 simplification is possible.
1232 If negate_expr_p would return true for T, NULL_TREE will never be
1233 returned. */
1235 static tree
1236 fold_negate_expr (tree t)
1238 tree type = TREE_TYPE (t);
1239 tree tem;
1241 switch (TREE_CODE (t))
1243 /* Convert - (~A) to A + 1. */
1244 case BIT_NOT_EXPR:
1245 if (INTEGRAL_TYPE_P (type))
1246 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1247 build_int_cst (type, 1));
1248 break;
1250 case INTEGER_CST:
1251 tem = fold_negate_const (t, type);
1252 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1253 || !TYPE_OVERFLOW_TRAPS (type))
1254 return tem;
1255 break;
1257 case REAL_CST:
1258 tem = fold_negate_const (t, type);
1259 /* Two's complement FP formats, such as c4x, may overflow. */
1260 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1261 return tem;
1262 break;
1264 case FIXED_CST:
1265 tem = fold_negate_const (t, type);
1266 return tem;
1268 case COMPLEX_CST:
1270 tree rpart = negate_expr (TREE_REALPART (t));
1271 tree ipart = negate_expr (TREE_IMAGPART (t));
1273 if ((TREE_CODE (rpart) == REAL_CST
1274 && TREE_CODE (ipart) == REAL_CST)
1275 || (TREE_CODE (rpart) == INTEGER_CST
1276 && TREE_CODE (ipart) == INTEGER_CST))
1277 return build_complex (type, rpart, ipart);
1279 break;
1281 case COMPLEX_EXPR:
1282 if (negate_expr_p (t))
1283 return fold_build2 (COMPLEX_EXPR, type,
1284 fold_negate_expr (TREE_OPERAND (t, 0)),
1285 fold_negate_expr (TREE_OPERAND (t, 1)));
1286 break;
1288 case CONJ_EXPR:
1289 if (negate_expr_p (t))
1290 return fold_build1 (CONJ_EXPR, type,
1291 fold_negate_expr (TREE_OPERAND (t, 0)));
1292 break;
1294 case NEGATE_EXPR:
1295 return TREE_OPERAND (t, 0);
1297 case PLUS_EXPR:
1298 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1299 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1301 /* -(A + B) -> (-B) - A. */
1302 if (negate_expr_p (TREE_OPERAND (t, 1))
1303 && reorder_operands_p (TREE_OPERAND (t, 0),
1304 TREE_OPERAND (t, 1)))
1306 tem = negate_expr (TREE_OPERAND (t, 1));
1307 return fold_build2 (MINUS_EXPR, type,
1308 tem, TREE_OPERAND (t, 0));
1311 /* -(A + B) -> (-A) - B. */
1312 if (negate_expr_p (TREE_OPERAND (t, 0)))
1314 tem = negate_expr (TREE_OPERAND (t, 0));
1315 return fold_build2 (MINUS_EXPR, type,
1316 tem, TREE_OPERAND (t, 1));
1319 break;
1321 case MINUS_EXPR:
1322 /* - (A - B) -> B - A */
1323 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1324 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1325 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1326 return fold_build2 (MINUS_EXPR, type,
1327 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1328 break;
1330 case MULT_EXPR:
1331 if (TYPE_UNSIGNED (type))
1332 break;
1334 /* Fall through. */
1336 case RDIV_EXPR:
1337 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1339 tem = TREE_OPERAND (t, 1);
1340 if (negate_expr_p (tem))
1341 return fold_build2 (TREE_CODE (t), type,
1342 TREE_OPERAND (t, 0), negate_expr (tem));
1343 tem = TREE_OPERAND (t, 0);
1344 if (negate_expr_p (tem))
1345 return fold_build2 (TREE_CODE (t), type,
1346 negate_expr (tem), TREE_OPERAND (t, 1));
1348 break;
1350 case TRUNC_DIV_EXPR:
1351 case ROUND_DIV_EXPR:
1352 case FLOOR_DIV_EXPR:
1353 case CEIL_DIV_EXPR:
1354 case EXACT_DIV_EXPR:
1355 /* In general we can't negate A / B, because if A is INT_MIN and
1356 B is 1, we may turn this into INT_MIN / -1 which is undefined
1357 and actually traps on some architectures. But if overflow is
1358 undefined, we can negate, because - (INT_MIN / 1) is an
1359 overflow. */
1360 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1362 const char * const warnmsg = G_("assuming signed overflow does not "
1363 "occur when negating a division");
1364 tem = TREE_OPERAND (t, 1);
1365 if (negate_expr_p (tem))
1367 if (INTEGRAL_TYPE_P (type)
1368 && (TREE_CODE (tem) != INTEGER_CST
1369 || integer_onep (tem)))
1370 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1371 return fold_build2 (TREE_CODE (t), type,
1372 TREE_OPERAND (t, 0), negate_expr (tem));
1374 tem = TREE_OPERAND (t, 0);
1375 if (negate_expr_p (tem))
1377 if (INTEGRAL_TYPE_P (type)
1378 && (TREE_CODE (tem) != INTEGER_CST
1379 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1380 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1381 return fold_build2 (TREE_CODE (t), type,
1382 negate_expr (tem), TREE_OPERAND (t, 1));
1385 break;
1387 case NOP_EXPR:
1388 /* Convert -((double)float) into (double)(-float). */
1389 if (TREE_CODE (type) == REAL_TYPE)
1391 tem = strip_float_extensions (t);
1392 if (tem != t && negate_expr_p (tem))
1393 return fold_convert (type, negate_expr (tem));
1395 break;
1397 case CALL_EXPR:
1398 /* Negate -f(x) as f(-x). */
1399 if (negate_mathfn_p (builtin_mathfn_code (t))
1400 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1402 tree fndecl, arg;
1404 fndecl = get_callee_fndecl (t);
1405 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1406 return build_call_expr (fndecl, 1, arg);
1408 break;
1410 case RSHIFT_EXPR:
1411 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1412 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1414 tree op1 = TREE_OPERAND (t, 1);
1415 if (TREE_INT_CST_HIGH (op1) == 0
1416 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1417 == TREE_INT_CST_LOW (op1))
1419 tree ntype = TYPE_UNSIGNED (type)
1420 ? signed_type_for (type)
1421 : unsigned_type_for (type);
1422 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1423 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1424 return fold_convert (type, temp);
1427 break;
1429 default:
1430 break;
1433 return NULL_TREE;
1436 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1437 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1438 return NULL_TREE. */
1440 static tree
1441 negate_expr (tree t)
1443 tree type, tem;
1445 if (t == NULL_TREE)
1446 return NULL_TREE;
1448 type = TREE_TYPE (t);
1449 STRIP_SIGN_NOPS (t);
1451 tem = fold_negate_expr (t);
1452 if (!tem)
1453 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1454 return fold_convert (type, tem);
1457 /* Split a tree IN into a constant, literal and variable parts that could be
1458 combined with CODE to make IN. "constant" means an expression with
1459 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1460 commutative arithmetic operation. Store the constant part into *CONP,
1461 the literal in *LITP and return the variable part. If a part isn't
1462 present, set it to null. If the tree does not decompose in this way,
1463 return the entire tree as the variable part and the other parts as null.
1465 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1466 case, we negate an operand that was subtracted. Except if it is a
1467 literal for which we use *MINUS_LITP instead.
1469 If NEGATE_P is true, we are negating all of IN, again except a literal
1470 for which we use *MINUS_LITP instead.
1472 If IN is itself a literal or constant, return it as appropriate.
1474 Note that we do not guarantee that any of the three values will be the
1475 same type as IN, but they will have the same signedness and mode. */
1477 static tree
1478 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1479 tree *minus_litp, int negate_p)
1481 tree var = 0;
1483 *conp = 0;
1484 *litp = 0;
1485 *minus_litp = 0;
1487 /* Strip any conversions that don't change the machine mode or signedness. */
1488 STRIP_SIGN_NOPS (in);
1490 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1491 || TREE_CODE (in) == FIXED_CST)
1492 *litp = in;
1493 else if (TREE_CODE (in) == code
1494 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1495 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1496 /* We can associate addition and subtraction together (even
1497 though the C standard doesn't say so) for integers because
1498 the value is not affected. For reals, the value might be
1499 affected, so we can't. */
1500 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1501 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1503 tree op0 = TREE_OPERAND (in, 0);
1504 tree op1 = TREE_OPERAND (in, 1);
1505 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1506 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1508 /* First see if either of the operands is a literal, then a constant. */
1509 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1510 || TREE_CODE (op0) == FIXED_CST)
1511 *litp = op0, op0 = 0;
1512 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1513 || TREE_CODE (op1) == FIXED_CST)
1514 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1516 if (op0 != 0 && TREE_CONSTANT (op0))
1517 *conp = op0, op0 = 0;
1518 else if (op1 != 0 && TREE_CONSTANT (op1))
1519 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1521 /* If we haven't dealt with either operand, this is not a case we can
1522 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1523 if (op0 != 0 && op1 != 0)
1524 var = in;
1525 else if (op0 != 0)
1526 var = op0;
1527 else
1528 var = op1, neg_var_p = neg1_p;
1530 /* Now do any needed negations. */
1531 if (neg_litp_p)
1532 *minus_litp = *litp, *litp = 0;
1533 if (neg_conp_p)
1534 *conp = negate_expr (*conp);
1535 if (neg_var_p)
1536 var = negate_expr (var);
1538 else if (TREE_CONSTANT (in))
1539 *conp = in;
1540 else
1541 var = in;
1543 if (negate_p)
1545 if (*litp)
1546 *minus_litp = *litp, *litp = 0;
1547 else if (*minus_litp)
1548 *litp = *minus_litp, *minus_litp = 0;
1549 *conp = negate_expr (*conp);
1550 var = negate_expr (var);
1553 return var;
1556 /* Re-associate trees split by the above function. T1 and T2 are either
1557 expressions to associate or null. Return the new expression, if any. If
1558 we build an operation, do it in TYPE and with CODE. */
1560 static tree
1561 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1563 if (t1 == 0)
1564 return t2;
1565 else if (t2 == 0)
1566 return t1;
1568 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1569 try to fold this since we will have infinite recursion. But do
1570 deal with any NEGATE_EXPRs. */
1571 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1572 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1574 if (code == PLUS_EXPR)
1576 if (TREE_CODE (t1) == NEGATE_EXPR)
1577 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1578 fold_convert (type, TREE_OPERAND (t1, 0)));
1579 else if (TREE_CODE (t2) == NEGATE_EXPR)
1580 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1581 fold_convert (type, TREE_OPERAND (t2, 0)));
1582 else if (integer_zerop (t2))
1583 return fold_convert (type, t1);
1585 else if (code == MINUS_EXPR)
1587 if (integer_zerop (t2))
1588 return fold_convert (type, t1);
1591 return build2 (code, type, fold_convert (type, t1),
1592 fold_convert (type, t2));
1595 return fold_build2 (code, type, fold_convert (type, t1),
1596 fold_convert (type, t2));
1599 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1600 for use in int_const_binop, size_binop and size_diffop. */
1602 static bool
1603 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1605 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1606 return false;
1607 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1608 return false;
1610 switch (code)
1612 case LSHIFT_EXPR:
1613 case RSHIFT_EXPR:
1614 case LROTATE_EXPR:
1615 case RROTATE_EXPR:
1616 return true;
1618 default:
1619 break;
1622 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1623 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1624 && TYPE_MODE (type1) == TYPE_MODE (type2);
1628 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1629 to produce a new constant. Return NULL_TREE if we don't know how
1630 to evaluate CODE at compile-time.
1632 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1634 tree
1635 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1637 unsigned HOST_WIDE_INT int1l, int2l;
1638 HOST_WIDE_INT int1h, int2h;
1639 unsigned HOST_WIDE_INT low;
1640 HOST_WIDE_INT hi;
1641 unsigned HOST_WIDE_INT garbagel;
1642 HOST_WIDE_INT garbageh;
1643 tree t;
1644 tree type = TREE_TYPE (arg1);
1645 int uns = TYPE_UNSIGNED (type);
1646 int is_sizetype
1647 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1648 int overflow = 0;
1650 int1l = TREE_INT_CST_LOW (arg1);
1651 int1h = TREE_INT_CST_HIGH (arg1);
1652 int2l = TREE_INT_CST_LOW (arg2);
1653 int2h = TREE_INT_CST_HIGH (arg2);
1655 switch (code)
1657 case BIT_IOR_EXPR:
1658 low = int1l | int2l, hi = int1h | int2h;
1659 break;
1661 case BIT_XOR_EXPR:
1662 low = int1l ^ int2l, hi = int1h ^ int2h;
1663 break;
1665 case BIT_AND_EXPR:
1666 low = int1l & int2l, hi = int1h & int2h;
1667 break;
1669 case RSHIFT_EXPR:
1670 int2l = -int2l;
1671 case LSHIFT_EXPR:
1672 /* It's unclear from the C standard whether shifts can overflow.
1673 The following code ignores overflow; perhaps a C standard
1674 interpretation ruling is needed. */
1675 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1676 &low, &hi, !uns);
1677 break;
1679 case RROTATE_EXPR:
1680 int2l = - int2l;
1681 case LROTATE_EXPR:
1682 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1683 &low, &hi);
1684 break;
1686 case PLUS_EXPR:
1687 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1688 break;
1690 case MINUS_EXPR:
1691 neg_double (int2l, int2h, &low, &hi);
1692 add_double (int1l, int1h, low, hi, &low, &hi);
1693 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1694 break;
1696 case MULT_EXPR:
1697 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1698 break;
1700 case TRUNC_DIV_EXPR:
1701 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1702 case EXACT_DIV_EXPR:
1703 /* This is a shortcut for a common special case. */
1704 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1705 && !TREE_OVERFLOW (arg1)
1706 && !TREE_OVERFLOW (arg2)
1707 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1709 if (code == CEIL_DIV_EXPR)
1710 int1l += int2l - 1;
1712 low = int1l / int2l, hi = 0;
1713 break;
1716 /* ... fall through ... */
1718 case ROUND_DIV_EXPR:
1719 if (int2h == 0 && int2l == 0)
1720 return NULL_TREE;
1721 if (int2h == 0 && int2l == 1)
1723 low = int1l, hi = int1h;
1724 break;
1726 if (int1l == int2l && int1h == int2h
1727 && ! (int1l == 0 && int1h == 0))
1729 low = 1, hi = 0;
1730 break;
1732 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1733 &low, &hi, &garbagel, &garbageh);
1734 break;
1736 case TRUNC_MOD_EXPR:
1737 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1738 /* This is a shortcut for a common special case. */
1739 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1740 && !TREE_OVERFLOW (arg1)
1741 && !TREE_OVERFLOW (arg2)
1742 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1744 if (code == CEIL_MOD_EXPR)
1745 int1l += int2l - 1;
1746 low = int1l % int2l, hi = 0;
1747 break;
1750 /* ... fall through ... */
1752 case ROUND_MOD_EXPR:
1753 if (int2h == 0 && int2l == 0)
1754 return NULL_TREE;
1755 overflow = div_and_round_double (code, uns,
1756 int1l, int1h, int2l, int2h,
1757 &garbagel, &garbageh, &low, &hi);
1758 break;
1760 case MIN_EXPR:
1761 case MAX_EXPR:
1762 if (uns)
1763 low = (((unsigned HOST_WIDE_INT) int1h
1764 < (unsigned HOST_WIDE_INT) int2h)
1765 || (((unsigned HOST_WIDE_INT) int1h
1766 == (unsigned HOST_WIDE_INT) int2h)
1767 && int1l < int2l));
1768 else
1769 low = (int1h < int2h
1770 || (int1h == int2h && int1l < int2l));
1772 if (low == (code == MIN_EXPR))
1773 low = int1l, hi = int1h;
1774 else
1775 low = int2l, hi = int2h;
1776 break;
1778 default:
1779 return NULL_TREE;
1782 if (notrunc)
1784 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1786 /* Propagate overflow flags ourselves. */
1787 if (((!uns || is_sizetype) && overflow)
1788 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1790 t = copy_node (t);
1791 TREE_OVERFLOW (t) = 1;
1794 else
1795 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1796 ((!uns || is_sizetype) && overflow)
1797 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1799 return t;
1802 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1803 constant. We assume ARG1 and ARG2 have the same data type, or at least
1804 are the same kind of constant and the same machine mode. Return zero if
1805 combining the constants is not allowed in the current operating mode.
1807 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1809 static tree
1810 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1812 /* Sanity check for the recursive cases. */
1813 if (!arg1 || !arg2)
1814 return NULL_TREE;
1816 STRIP_NOPS (arg1);
1817 STRIP_NOPS (arg2);
1819 if (TREE_CODE (arg1) == INTEGER_CST)
1820 return int_const_binop (code, arg1, arg2, notrunc);
1822 if (TREE_CODE (arg1) == REAL_CST)
1824 enum machine_mode mode;
1825 REAL_VALUE_TYPE d1;
1826 REAL_VALUE_TYPE d2;
1827 REAL_VALUE_TYPE value;
1828 REAL_VALUE_TYPE result;
1829 bool inexact;
1830 tree t, type;
1832 /* The following codes are handled by real_arithmetic. */
1833 switch (code)
1835 case PLUS_EXPR:
1836 case MINUS_EXPR:
1837 case MULT_EXPR:
1838 case RDIV_EXPR:
1839 case MIN_EXPR:
1840 case MAX_EXPR:
1841 break;
1843 default:
1844 return NULL_TREE;
1847 d1 = TREE_REAL_CST (arg1);
1848 d2 = TREE_REAL_CST (arg2);
1850 type = TREE_TYPE (arg1);
1851 mode = TYPE_MODE (type);
1853 /* Don't perform operation if we honor signaling NaNs and
1854 either operand is a NaN. */
1855 if (HONOR_SNANS (mode)
1856 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1857 return NULL_TREE;
1859 /* Don't perform operation if it would raise a division
1860 by zero exception. */
1861 if (code == RDIV_EXPR
1862 && REAL_VALUES_EQUAL (d2, dconst0)
1863 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1864 return NULL_TREE;
1866 /* If either operand is a NaN, just return it. Otherwise, set up
1867 for floating-point trap; we return an overflow. */
1868 if (REAL_VALUE_ISNAN (d1))
1869 return arg1;
1870 else if (REAL_VALUE_ISNAN (d2))
1871 return arg2;
1873 inexact = real_arithmetic (&value, code, &d1, &d2);
1874 real_convert (&result, mode, &value);
1876 /* Don't constant fold this floating point operation if
1877 the result has overflowed and flag_trapping_math. */
1878 if (flag_trapping_math
1879 && MODE_HAS_INFINITIES (mode)
1880 && REAL_VALUE_ISINF (result)
1881 && !REAL_VALUE_ISINF (d1)
1882 && !REAL_VALUE_ISINF (d2))
1883 return NULL_TREE;
1885 /* Don't constant fold this floating point operation if the
1886 result may dependent upon the run-time rounding mode and
1887 flag_rounding_math is set, or if GCC's software emulation
1888 is unable to accurately represent the result. */
1889 if ((flag_rounding_math
1890 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1891 && (inexact || !real_identical (&result, &value)))
1892 return NULL_TREE;
1894 t = build_real (type, result);
1896 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1897 return t;
1900 if (TREE_CODE (arg1) == FIXED_CST)
1902 FIXED_VALUE_TYPE f1;
1903 FIXED_VALUE_TYPE f2;
1904 FIXED_VALUE_TYPE result;
1905 tree t, type;
1906 int sat_p;
1907 bool overflow_p;
1909 /* The following codes are handled by fixed_arithmetic. */
1910 switch (code)
1912 case PLUS_EXPR:
1913 case MINUS_EXPR:
1914 case MULT_EXPR:
1915 case TRUNC_DIV_EXPR:
1916 f2 = TREE_FIXED_CST (arg2);
1917 break;
1919 case LSHIFT_EXPR:
1920 case RSHIFT_EXPR:
1921 f2.data.high = TREE_INT_CST_HIGH (arg2);
1922 f2.data.low = TREE_INT_CST_LOW (arg2);
1923 f2.mode = SImode;
1924 break;
1926 default:
1927 return NULL_TREE;
1930 f1 = TREE_FIXED_CST (arg1);
1931 type = TREE_TYPE (arg1);
1932 sat_p = TYPE_SATURATING (type);
1933 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1934 t = build_fixed (type, result);
1935 /* Propagate overflow flags. */
1936 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1938 TREE_OVERFLOW (t) = 1;
1939 TREE_CONSTANT_OVERFLOW (t) = 1;
1941 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1942 TREE_CONSTANT_OVERFLOW (t) = 1;
1943 return t;
1946 if (TREE_CODE (arg1) == COMPLEX_CST)
1948 tree type = TREE_TYPE (arg1);
1949 tree r1 = TREE_REALPART (arg1);
1950 tree i1 = TREE_IMAGPART (arg1);
1951 tree r2 = TREE_REALPART (arg2);
1952 tree i2 = TREE_IMAGPART (arg2);
1953 tree real, imag;
1955 switch (code)
1957 case PLUS_EXPR:
1958 case MINUS_EXPR:
1959 real = const_binop (code, r1, r2, notrunc);
1960 imag = const_binop (code, i1, i2, notrunc);
1961 break;
1963 case MULT_EXPR:
1964 real = const_binop (MINUS_EXPR,
1965 const_binop (MULT_EXPR, r1, r2, notrunc),
1966 const_binop (MULT_EXPR, i1, i2, notrunc),
1967 notrunc);
1968 imag = const_binop (PLUS_EXPR,
1969 const_binop (MULT_EXPR, r1, i2, notrunc),
1970 const_binop (MULT_EXPR, i1, r2, notrunc),
1971 notrunc);
1972 break;
1974 case RDIV_EXPR:
1976 tree magsquared
1977 = const_binop (PLUS_EXPR,
1978 const_binop (MULT_EXPR, r2, r2, notrunc),
1979 const_binop (MULT_EXPR, i2, i2, notrunc),
1980 notrunc);
1981 tree t1
1982 = const_binop (PLUS_EXPR,
1983 const_binop (MULT_EXPR, r1, r2, notrunc),
1984 const_binop (MULT_EXPR, i1, i2, notrunc),
1985 notrunc);
1986 tree t2
1987 = const_binop (MINUS_EXPR,
1988 const_binop (MULT_EXPR, i1, r2, notrunc),
1989 const_binop (MULT_EXPR, r1, i2, notrunc),
1990 notrunc);
1992 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1993 code = TRUNC_DIV_EXPR;
1995 real = const_binop (code, t1, magsquared, notrunc);
1996 imag = const_binop (code, t2, magsquared, notrunc);
1998 break;
2000 default:
2001 return NULL_TREE;
2004 if (real && imag)
2005 return build_complex (type, real, imag);
2008 return NULL_TREE;
2011 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2012 indicates which particular sizetype to create. */
2014 tree
2015 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2017 return build_int_cst (sizetype_tab[(int) kind], number);
2020 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2021 is a tree code. The type of the result is taken from the operands.
2022 Both must be equivalent integer types, ala int_binop_types_match_p.
2023 If the operands are constant, so is the result. */
2025 tree
2026 size_binop (enum tree_code code, tree arg0, tree arg1)
2028 tree type = TREE_TYPE (arg0);
2030 if (arg0 == error_mark_node || arg1 == error_mark_node)
2031 return error_mark_node;
2033 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2034 TREE_TYPE (arg1)));
2036 /* Handle the special case of two integer constants faster. */
2037 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2039 /* And some specific cases even faster than that. */
2040 if (code == PLUS_EXPR)
2042 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2043 return arg1;
2044 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2045 return arg0;
2047 else if (code == MINUS_EXPR)
2049 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2050 return arg0;
2052 else if (code == MULT_EXPR)
2054 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2055 return arg1;
2058 /* Handle general case of two integer constants. */
2059 return int_const_binop (code, arg0, arg1, 0);
2062 return fold_build2 (code, type, arg0, arg1);
2065 /* Given two values, either both of sizetype or both of bitsizetype,
2066 compute the difference between the two values. Return the value
2067 in signed type corresponding to the type of the operands. */
2069 tree
2070 size_diffop (tree arg0, tree arg1)
2072 tree type = TREE_TYPE (arg0);
2073 tree ctype;
2075 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2076 TREE_TYPE (arg1)));
2078 /* If the type is already signed, just do the simple thing. */
2079 if (!TYPE_UNSIGNED (type))
2080 return size_binop (MINUS_EXPR, arg0, arg1);
2082 if (type == sizetype)
2083 ctype = ssizetype;
2084 else if (type == bitsizetype)
2085 ctype = sbitsizetype;
2086 else
2087 ctype = signed_type_for (type);
2089 /* If either operand is not a constant, do the conversions to the signed
2090 type and subtract. The hardware will do the right thing with any
2091 overflow in the subtraction. */
2092 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2093 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2094 fold_convert (ctype, arg1));
2096 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2097 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2098 overflow) and negate (which can't either). Special-case a result
2099 of zero while we're here. */
2100 if (tree_int_cst_equal (arg0, arg1))
2101 return build_int_cst (ctype, 0);
2102 else if (tree_int_cst_lt (arg1, arg0))
2103 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2104 else
2105 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2106 fold_convert (ctype, size_binop (MINUS_EXPR,
2107 arg1, arg0)));
2110 /* A subroutine of fold_convert_const handling conversions of an
2111 INTEGER_CST to another integer type. */
2113 static tree
2114 fold_convert_const_int_from_int (tree type, const_tree arg1)
2116 tree t;
2118 /* Given an integer constant, make new constant with new type,
2119 appropriately sign-extended or truncated. */
2120 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2121 TREE_INT_CST_HIGH (arg1),
2122 /* Don't set the overflow when
2123 converting from a pointer, */
2124 !POINTER_TYPE_P (TREE_TYPE (arg1))
2125 /* or to a sizetype with same signedness
2126 and the precision is unchanged.
2127 ??? sizetype is always sign-extended,
2128 but its signedness depends on the
2129 frontend. Thus we see spurious overflows
2130 here if we do not check this. */
2131 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2132 == TYPE_PRECISION (type))
2133 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2134 == TYPE_UNSIGNED (type))
2135 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2136 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2137 || (TREE_CODE (type) == INTEGER_TYPE
2138 && TYPE_IS_SIZETYPE (type)))),
2139 (TREE_INT_CST_HIGH (arg1) < 0
2140 && (TYPE_UNSIGNED (type)
2141 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2142 | TREE_OVERFLOW (arg1));
2144 return t;
2147 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2148 to an integer type. */
2150 static tree
2151 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2153 int overflow = 0;
2154 tree t;
2156 /* The following code implements the floating point to integer
2157 conversion rules required by the Java Language Specification,
2158 that IEEE NaNs are mapped to zero and values that overflow
2159 the target precision saturate, i.e. values greater than
2160 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2161 are mapped to INT_MIN. These semantics are allowed by the
2162 C and C++ standards that simply state that the behavior of
2163 FP-to-integer conversion is unspecified upon overflow. */
2165 HOST_WIDE_INT high, low;
2166 REAL_VALUE_TYPE r;
2167 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2169 switch (code)
2171 case FIX_TRUNC_EXPR:
2172 real_trunc (&r, VOIDmode, &x);
2173 break;
2175 default:
2176 gcc_unreachable ();
2179 /* If R is NaN, return zero and show we have an overflow. */
2180 if (REAL_VALUE_ISNAN (r))
2182 overflow = 1;
2183 high = 0;
2184 low = 0;
2187 /* See if R is less than the lower bound or greater than the
2188 upper bound. */
2190 if (! overflow)
2192 tree lt = TYPE_MIN_VALUE (type);
2193 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2194 if (REAL_VALUES_LESS (r, l))
2196 overflow = 1;
2197 high = TREE_INT_CST_HIGH (lt);
2198 low = TREE_INT_CST_LOW (lt);
2202 if (! overflow)
2204 tree ut = TYPE_MAX_VALUE (type);
2205 if (ut)
2207 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2208 if (REAL_VALUES_LESS (u, r))
2210 overflow = 1;
2211 high = TREE_INT_CST_HIGH (ut);
2212 low = TREE_INT_CST_LOW (ut);
2217 if (! overflow)
2218 REAL_VALUE_TO_INT (&low, &high, r);
2220 t = force_fit_type_double (type, low, high, -1,
2221 overflow | TREE_OVERFLOW (arg1));
2222 return t;
2225 /* A subroutine of fold_convert_const handling conversions of a
2226 FIXED_CST to an integer type. */
2228 static tree
2229 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2231 tree t;
2232 double_int temp, temp_trunc;
2233 unsigned int mode;
2235 /* Right shift FIXED_CST to temp by fbit. */
2236 temp = TREE_FIXED_CST (arg1).data;
2237 mode = TREE_FIXED_CST (arg1).mode;
2238 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2240 lshift_double (temp.low, temp.high,
2241 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2242 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2244 /* Left shift temp to temp_trunc by fbit. */
2245 lshift_double (temp.low, temp.high,
2246 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2247 &temp_trunc.low, &temp_trunc.high,
2248 SIGNED_FIXED_POINT_MODE_P (mode));
2250 else
2252 temp.low = 0;
2253 temp.high = 0;
2254 temp_trunc.low = 0;
2255 temp_trunc.high = 0;
2258 /* If FIXED_CST is negative, we need to round the value toward 0.
2259 By checking if the fractional bits are not zero to add 1 to temp. */
2260 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2261 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2263 double_int one;
2264 one.low = 1;
2265 one.high = 0;
2266 temp = double_int_add (temp, one);
2269 /* Given a fixed-point constant, make new constant with new type,
2270 appropriately sign-extended or truncated. */
2271 t = force_fit_type_double (type, temp.low, temp.high, -1,
2272 (temp.high < 0
2273 && (TYPE_UNSIGNED (type)
2274 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2275 | TREE_OVERFLOW (arg1));
2277 return t;
2280 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2281 to another floating point type. */
2283 static tree
2284 fold_convert_const_real_from_real (tree type, const_tree arg1)
2286 REAL_VALUE_TYPE value;
2287 tree t;
2289 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2290 t = build_real (type, value);
2292 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2293 return t;
2296 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2297 to a floating point type. */
2299 static tree
2300 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2302 REAL_VALUE_TYPE value;
2303 tree t;
2305 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2306 t = build_real (type, value);
2308 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2309 TREE_CONSTANT_OVERFLOW (t)
2310 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2311 return t;
2314 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2315 to another fixed-point type. */
2317 static tree
2318 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2320 FIXED_VALUE_TYPE value;
2321 tree t;
2322 bool overflow_p;
2324 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2325 TYPE_SATURATING (type));
2326 t = build_fixed (type, value);
2328 /* Propagate overflow flags. */
2329 if (overflow_p | TREE_OVERFLOW (arg1))
2331 TREE_OVERFLOW (t) = 1;
2332 TREE_CONSTANT_OVERFLOW (t) = 1;
2334 else if (TREE_CONSTANT_OVERFLOW (arg1))
2335 TREE_CONSTANT_OVERFLOW (t) = 1;
2336 return t;
2339 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2340 to a fixed-point type. */
2342 static tree
2343 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2345 FIXED_VALUE_TYPE value;
2346 tree t;
2347 bool overflow_p;
2349 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2350 TREE_INT_CST (arg1),
2351 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2352 TYPE_SATURATING (type));
2353 t = build_fixed (type, value);
2355 /* Propagate overflow flags. */
2356 if (overflow_p | TREE_OVERFLOW (arg1))
2358 TREE_OVERFLOW (t) = 1;
2359 TREE_CONSTANT_OVERFLOW (t) = 1;
2361 else if (TREE_CONSTANT_OVERFLOW (arg1))
2362 TREE_CONSTANT_OVERFLOW (t) = 1;
2363 return t;
2366 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2367 to a fixed-point type. */
2369 static tree
2370 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2372 FIXED_VALUE_TYPE value;
2373 tree t;
2374 bool overflow_p;
2376 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2377 &TREE_REAL_CST (arg1),
2378 TYPE_SATURATING (type));
2379 t = build_fixed (type, value);
2381 /* Propagate overflow flags. */
2382 if (overflow_p | TREE_OVERFLOW (arg1))
2384 TREE_OVERFLOW (t) = 1;
2385 TREE_CONSTANT_OVERFLOW (t) = 1;
2387 else if (TREE_CONSTANT_OVERFLOW (arg1))
2388 TREE_CONSTANT_OVERFLOW (t) = 1;
2389 return t;
2392 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2393 type TYPE. If no simplification can be done return NULL_TREE. */
2395 static tree
2396 fold_convert_const (enum tree_code code, tree type, tree arg1)
2398 if (TREE_TYPE (arg1) == type)
2399 return arg1;
2401 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2402 || TREE_CODE (type) == OFFSET_TYPE)
2404 if (TREE_CODE (arg1) == INTEGER_CST)
2405 return fold_convert_const_int_from_int (type, arg1);
2406 else if (TREE_CODE (arg1) == REAL_CST)
2407 return fold_convert_const_int_from_real (code, type, arg1);
2408 else if (TREE_CODE (arg1) == FIXED_CST)
2409 return fold_convert_const_int_from_fixed (type, arg1);
2411 else if (TREE_CODE (type) == REAL_TYPE)
2413 if (TREE_CODE (arg1) == INTEGER_CST)
2414 return build_real_from_int_cst (type, arg1);
2415 else if (TREE_CODE (arg1) == REAL_CST)
2416 return fold_convert_const_real_from_real (type, arg1);
2417 else if (TREE_CODE (arg1) == FIXED_CST)
2418 return fold_convert_const_real_from_fixed (type, arg1);
2420 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2422 if (TREE_CODE (arg1) == FIXED_CST)
2423 return fold_convert_const_fixed_from_fixed (type, arg1);
2424 else if (TREE_CODE (arg1) == INTEGER_CST)
2425 return fold_convert_const_fixed_from_int (type, arg1);
2426 else if (TREE_CODE (arg1) == REAL_CST)
2427 return fold_convert_const_fixed_from_real (type, arg1);
2429 return NULL_TREE;
2432 /* Construct a vector of zero elements of vector type TYPE. */
2434 static tree
2435 build_zero_vector (tree type)
2437 tree elem, list;
2438 int i, units;
2440 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2441 units = TYPE_VECTOR_SUBPARTS (type);
2443 list = NULL_TREE;
2444 for (i = 0; i < units; i++)
2445 list = tree_cons (NULL_TREE, elem, list);
2446 return build_vector (type, list);
2449 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2451 bool
2452 fold_convertible_p (const_tree type, const_tree arg)
2454 tree orig = TREE_TYPE (arg);
2456 if (type == orig)
2457 return true;
2459 if (TREE_CODE (arg) == ERROR_MARK
2460 || TREE_CODE (type) == ERROR_MARK
2461 || TREE_CODE (orig) == ERROR_MARK)
2462 return false;
2464 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2465 return true;
2467 switch (TREE_CODE (type))
2469 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2470 case POINTER_TYPE: case REFERENCE_TYPE:
2471 case OFFSET_TYPE:
2472 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2473 || TREE_CODE (orig) == OFFSET_TYPE)
2474 return true;
2475 return (TREE_CODE (orig) == VECTOR_TYPE
2476 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2478 case REAL_TYPE:
2479 case FIXED_POINT_TYPE:
2480 case COMPLEX_TYPE:
2481 case VECTOR_TYPE:
2482 case VOID_TYPE:
2483 return TREE_CODE (type) == TREE_CODE (orig);
2485 default:
2486 return false;
2490 /* Convert expression ARG to type TYPE. Used by the middle-end for
2491 simple conversions in preference to calling the front-end's convert. */
2493 tree
2494 fold_convert (tree type, tree arg)
2496 tree orig = TREE_TYPE (arg);
2497 tree tem;
2499 if (type == orig)
2500 return arg;
2502 if (TREE_CODE (arg) == ERROR_MARK
2503 || TREE_CODE (type) == ERROR_MARK
2504 || TREE_CODE (orig) == ERROR_MARK)
2505 return error_mark_node;
2507 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2508 return fold_build1 (NOP_EXPR, type, arg);
2510 switch (TREE_CODE (type))
2512 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2513 case POINTER_TYPE: case REFERENCE_TYPE:
2514 case OFFSET_TYPE:
2515 if (TREE_CODE (arg) == INTEGER_CST)
2517 tem = fold_convert_const (NOP_EXPR, type, arg);
2518 if (tem != NULL_TREE)
2519 return tem;
2521 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2522 || TREE_CODE (orig) == OFFSET_TYPE)
2523 return fold_build1 (NOP_EXPR, type, arg);
2524 if (TREE_CODE (orig) == COMPLEX_TYPE)
2526 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2527 return fold_convert (type, tem);
2529 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2530 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2531 return fold_build1 (NOP_EXPR, type, arg);
2533 case REAL_TYPE:
2534 if (TREE_CODE (arg) == INTEGER_CST)
2536 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2537 if (tem != NULL_TREE)
2538 return tem;
2540 else if (TREE_CODE (arg) == REAL_CST)
2542 tem = fold_convert_const (NOP_EXPR, type, arg);
2543 if (tem != NULL_TREE)
2544 return tem;
2546 else if (TREE_CODE (arg) == FIXED_CST)
2548 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2549 if (tem != NULL_TREE)
2550 return tem;
2553 switch (TREE_CODE (orig))
2555 case INTEGER_TYPE:
2556 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2557 case POINTER_TYPE: case REFERENCE_TYPE:
2558 return fold_build1 (FLOAT_EXPR, type, arg);
2560 case REAL_TYPE:
2561 return fold_build1 (NOP_EXPR, type, arg);
2563 case FIXED_POINT_TYPE:
2564 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2566 case COMPLEX_TYPE:
2567 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2568 return fold_convert (type, tem);
2570 default:
2571 gcc_unreachable ();
2574 case FIXED_POINT_TYPE:
2575 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2576 || TREE_CODE (arg) == REAL_CST)
2578 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2579 if (tem != NULL_TREE)
2580 return tem;
2583 switch (TREE_CODE (orig))
2585 case FIXED_POINT_TYPE:
2586 case INTEGER_TYPE:
2587 case ENUMERAL_TYPE:
2588 case BOOLEAN_TYPE:
2589 case REAL_TYPE:
2590 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2592 case COMPLEX_TYPE:
2593 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2594 return fold_convert (type, tem);
2596 default:
2597 gcc_unreachable ();
2600 case COMPLEX_TYPE:
2601 switch (TREE_CODE (orig))
2603 case INTEGER_TYPE:
2604 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2605 case POINTER_TYPE: case REFERENCE_TYPE:
2606 case REAL_TYPE:
2607 case FIXED_POINT_TYPE:
2608 return fold_build2 (COMPLEX_EXPR, type,
2609 fold_convert (TREE_TYPE (type), arg),
2610 fold_convert (TREE_TYPE (type),
2611 integer_zero_node));
2612 case COMPLEX_TYPE:
2614 tree rpart, ipart;
2616 if (TREE_CODE (arg) == COMPLEX_EXPR)
2618 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2619 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2620 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2623 arg = save_expr (arg);
2624 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2625 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2626 rpart = fold_convert (TREE_TYPE (type), rpart);
2627 ipart = fold_convert (TREE_TYPE (type), ipart);
2628 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2631 default:
2632 gcc_unreachable ();
2635 case VECTOR_TYPE:
2636 if (integer_zerop (arg))
2637 return build_zero_vector (type);
2638 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2639 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2640 || TREE_CODE (orig) == VECTOR_TYPE);
2641 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2643 case VOID_TYPE:
2644 tem = fold_ignored_result (arg);
2645 if (TREE_CODE (tem) == MODIFY_EXPR)
2646 return tem;
2647 return fold_build1 (NOP_EXPR, type, tem);
2649 default:
2650 gcc_unreachable ();
2654 /* Return false if expr can be assumed not to be an lvalue, true
2655 otherwise. */
2657 static bool
2658 maybe_lvalue_p (const_tree x)
2660 /* We only need to wrap lvalue tree codes. */
2661 switch (TREE_CODE (x))
2663 case VAR_DECL:
2664 case PARM_DECL:
2665 case RESULT_DECL:
2666 case LABEL_DECL:
2667 case FUNCTION_DECL:
2668 case SSA_NAME:
2670 case COMPONENT_REF:
2671 case INDIRECT_REF:
2672 case ALIGN_INDIRECT_REF:
2673 case MISALIGNED_INDIRECT_REF:
2674 case ARRAY_REF:
2675 case ARRAY_RANGE_REF:
2676 case BIT_FIELD_REF:
2677 case OBJ_TYPE_REF:
2679 case REALPART_EXPR:
2680 case IMAGPART_EXPR:
2681 case PREINCREMENT_EXPR:
2682 case PREDECREMENT_EXPR:
2683 case SAVE_EXPR:
2684 case TRY_CATCH_EXPR:
2685 case WITH_CLEANUP_EXPR:
2686 case COMPOUND_EXPR:
2687 case MODIFY_EXPR:
2688 case TARGET_EXPR:
2689 case COND_EXPR:
2690 case BIND_EXPR:
2691 case MIN_EXPR:
2692 case MAX_EXPR:
2693 break;
2695 default:
2696 /* Assume the worst for front-end tree codes. */
2697 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2698 break;
2699 return false;
2702 return true;
2705 /* Return an expr equal to X but certainly not valid as an lvalue. */
2707 tree
2708 non_lvalue (tree x)
2710 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2711 us. */
2712 if (in_gimple_form)
2713 return x;
2715 if (! maybe_lvalue_p (x))
2716 return x;
2717 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2720 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2721 Zero means allow extended lvalues. */
2723 int pedantic_lvalues;
2725 /* When pedantic, return an expr equal to X but certainly not valid as a
2726 pedantic lvalue. Otherwise, return X. */
2728 static tree
2729 pedantic_non_lvalue (tree x)
2731 if (pedantic_lvalues)
2732 return non_lvalue (x);
2733 else
2734 return x;
2737 /* Given a tree comparison code, return the code that is the logical inverse
2738 of the given code. It is not safe to do this for floating-point
2739 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2740 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2742 enum tree_code
2743 invert_tree_comparison (enum tree_code code, bool honor_nans)
2745 if (honor_nans && flag_trapping_math)
2746 return ERROR_MARK;
2748 switch (code)
2750 case EQ_EXPR:
2751 return NE_EXPR;
2752 case NE_EXPR:
2753 return EQ_EXPR;
2754 case GT_EXPR:
2755 return honor_nans ? UNLE_EXPR : LE_EXPR;
2756 case GE_EXPR:
2757 return honor_nans ? UNLT_EXPR : LT_EXPR;
2758 case LT_EXPR:
2759 return honor_nans ? UNGE_EXPR : GE_EXPR;
2760 case LE_EXPR:
2761 return honor_nans ? UNGT_EXPR : GT_EXPR;
2762 case LTGT_EXPR:
2763 return UNEQ_EXPR;
2764 case UNEQ_EXPR:
2765 return LTGT_EXPR;
2766 case UNGT_EXPR:
2767 return LE_EXPR;
2768 case UNGE_EXPR:
2769 return LT_EXPR;
2770 case UNLT_EXPR:
2771 return GE_EXPR;
2772 case UNLE_EXPR:
2773 return GT_EXPR;
2774 case ORDERED_EXPR:
2775 return UNORDERED_EXPR;
2776 case UNORDERED_EXPR:
2777 return ORDERED_EXPR;
2778 default:
2779 gcc_unreachable ();
2783 /* Similar, but return the comparison that results if the operands are
2784 swapped. This is safe for floating-point. */
2786 enum tree_code
2787 swap_tree_comparison (enum tree_code code)
2789 switch (code)
2791 case EQ_EXPR:
2792 case NE_EXPR:
2793 case ORDERED_EXPR:
2794 case UNORDERED_EXPR:
2795 case LTGT_EXPR:
2796 case UNEQ_EXPR:
2797 return code;
2798 case GT_EXPR:
2799 return LT_EXPR;
2800 case GE_EXPR:
2801 return LE_EXPR;
2802 case LT_EXPR:
2803 return GT_EXPR;
2804 case LE_EXPR:
2805 return GE_EXPR;
2806 case UNGT_EXPR:
2807 return UNLT_EXPR;
2808 case UNGE_EXPR:
2809 return UNLE_EXPR;
2810 case UNLT_EXPR:
2811 return UNGT_EXPR;
2812 case UNLE_EXPR:
2813 return UNGE_EXPR;
2814 default:
2815 gcc_unreachable ();
2820 /* Convert a comparison tree code from an enum tree_code representation
2821 into a compcode bit-based encoding. This function is the inverse of
2822 compcode_to_comparison. */
2824 static enum comparison_code
2825 comparison_to_compcode (enum tree_code code)
2827 switch (code)
2829 case LT_EXPR:
2830 return COMPCODE_LT;
2831 case EQ_EXPR:
2832 return COMPCODE_EQ;
2833 case LE_EXPR:
2834 return COMPCODE_LE;
2835 case GT_EXPR:
2836 return COMPCODE_GT;
2837 case NE_EXPR:
2838 return COMPCODE_NE;
2839 case GE_EXPR:
2840 return COMPCODE_GE;
2841 case ORDERED_EXPR:
2842 return COMPCODE_ORD;
2843 case UNORDERED_EXPR:
2844 return COMPCODE_UNORD;
2845 case UNLT_EXPR:
2846 return COMPCODE_UNLT;
2847 case UNEQ_EXPR:
2848 return COMPCODE_UNEQ;
2849 case UNLE_EXPR:
2850 return COMPCODE_UNLE;
2851 case UNGT_EXPR:
2852 return COMPCODE_UNGT;
2853 case LTGT_EXPR:
2854 return COMPCODE_LTGT;
2855 case UNGE_EXPR:
2856 return COMPCODE_UNGE;
2857 default:
2858 gcc_unreachable ();
2862 /* Convert a compcode bit-based encoding of a comparison operator back
2863 to GCC's enum tree_code representation. This function is the
2864 inverse of comparison_to_compcode. */
2866 static enum tree_code
2867 compcode_to_comparison (enum comparison_code code)
2869 switch (code)
2871 case COMPCODE_LT:
2872 return LT_EXPR;
2873 case COMPCODE_EQ:
2874 return EQ_EXPR;
2875 case COMPCODE_LE:
2876 return LE_EXPR;
2877 case COMPCODE_GT:
2878 return GT_EXPR;
2879 case COMPCODE_NE:
2880 return NE_EXPR;
2881 case COMPCODE_GE:
2882 return GE_EXPR;
2883 case COMPCODE_ORD:
2884 return ORDERED_EXPR;
2885 case COMPCODE_UNORD:
2886 return UNORDERED_EXPR;
2887 case COMPCODE_UNLT:
2888 return UNLT_EXPR;
2889 case COMPCODE_UNEQ:
2890 return UNEQ_EXPR;
2891 case COMPCODE_UNLE:
2892 return UNLE_EXPR;
2893 case COMPCODE_UNGT:
2894 return UNGT_EXPR;
2895 case COMPCODE_LTGT:
2896 return LTGT_EXPR;
2897 case COMPCODE_UNGE:
2898 return UNGE_EXPR;
2899 default:
2900 gcc_unreachable ();
2904 /* Return a tree for the comparison which is the combination of
2905 doing the AND or OR (depending on CODE) of the two operations LCODE
2906 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2907 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2908 if this makes the transformation invalid. */
2910 tree
2911 combine_comparisons (enum tree_code code, enum tree_code lcode,
2912 enum tree_code rcode, tree truth_type,
2913 tree ll_arg, tree lr_arg)
2915 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2916 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2917 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2918 enum comparison_code compcode;
2920 switch (code)
2922 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2923 compcode = lcompcode & rcompcode;
2924 break;
2926 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2927 compcode = lcompcode | rcompcode;
2928 break;
2930 default:
2931 return NULL_TREE;
2934 if (!honor_nans)
2936 /* Eliminate unordered comparisons, as well as LTGT and ORD
2937 which are not used unless the mode has NaNs. */
2938 compcode &= ~COMPCODE_UNORD;
2939 if (compcode == COMPCODE_LTGT)
2940 compcode = COMPCODE_NE;
2941 else if (compcode == COMPCODE_ORD)
2942 compcode = COMPCODE_TRUE;
2944 else if (flag_trapping_math)
2946 /* Check that the original operation and the optimized ones will trap
2947 under the same condition. */
2948 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2949 && (lcompcode != COMPCODE_EQ)
2950 && (lcompcode != COMPCODE_ORD);
2951 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2952 && (rcompcode != COMPCODE_EQ)
2953 && (rcompcode != COMPCODE_ORD);
2954 bool trap = (compcode & COMPCODE_UNORD) == 0
2955 && (compcode != COMPCODE_EQ)
2956 && (compcode != COMPCODE_ORD);
2958 /* In a short-circuited boolean expression the LHS might be
2959 such that the RHS, if evaluated, will never trap. For
2960 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2961 if neither x nor y is NaN. (This is a mixed blessing: for
2962 example, the expression above will never trap, hence
2963 optimizing it to x < y would be invalid). */
2964 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2965 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2966 rtrap = false;
2968 /* If the comparison was short-circuited, and only the RHS
2969 trapped, we may now generate a spurious trap. */
2970 if (rtrap && !ltrap
2971 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2972 return NULL_TREE;
2974 /* If we changed the conditions that cause a trap, we lose. */
2975 if ((ltrap || rtrap) != trap)
2976 return NULL_TREE;
2979 if (compcode == COMPCODE_TRUE)
2980 return constant_boolean_node (true, truth_type);
2981 else if (compcode == COMPCODE_FALSE)
2982 return constant_boolean_node (false, truth_type);
2983 else
2984 return fold_build2 (compcode_to_comparison (compcode),
2985 truth_type, ll_arg, lr_arg);
2988 /* Return nonzero if two operands (typically of the same tree node)
2989 are necessarily equal. If either argument has side-effects this
2990 function returns zero. FLAGS modifies behavior as follows:
2992 If OEP_ONLY_CONST is set, only return nonzero for constants.
2993 This function tests whether the operands are indistinguishable;
2994 it does not test whether they are equal using C's == operation.
2995 The distinction is important for IEEE floating point, because
2996 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2997 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2999 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3000 even though it may hold multiple values during a function.
3001 This is because a GCC tree node guarantees that nothing else is
3002 executed between the evaluation of its "operands" (which may often
3003 be evaluated in arbitrary order). Hence if the operands themselves
3004 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3005 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3006 unset means assuming isochronic (or instantaneous) tree equivalence.
3007 Unless comparing arbitrary expression trees, such as from different
3008 statements, this flag can usually be left unset.
3010 If OEP_PURE_SAME is set, then pure functions with identical arguments
3011 are considered the same. It is used when the caller has other ways
3012 to ensure that global memory is unchanged in between. */
3015 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3017 /* If either is ERROR_MARK, they aren't equal. */
3018 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3019 return 0;
3021 /* Check equality of integer constants before bailing out due to
3022 precision differences. */
3023 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3024 return tree_int_cst_equal (arg0, arg1);
3026 /* If both types don't have the same signedness, then we can't consider
3027 them equal. We must check this before the STRIP_NOPS calls
3028 because they may change the signedness of the arguments. As pointers
3029 strictly don't have a signedness, require either two pointers or
3030 two non-pointers as well. */
3031 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3032 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3033 return 0;
3035 /* If both types don't have the same precision, then it is not safe
3036 to strip NOPs. */
3037 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3038 return 0;
3040 STRIP_NOPS (arg0);
3041 STRIP_NOPS (arg1);
3043 /* In case both args are comparisons but with different comparison
3044 code, try to swap the comparison operands of one arg to produce
3045 a match and compare that variant. */
3046 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3047 && COMPARISON_CLASS_P (arg0)
3048 && COMPARISON_CLASS_P (arg1))
3050 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3052 if (TREE_CODE (arg0) == swap_code)
3053 return operand_equal_p (TREE_OPERAND (arg0, 0),
3054 TREE_OPERAND (arg1, 1), flags)
3055 && operand_equal_p (TREE_OPERAND (arg0, 1),
3056 TREE_OPERAND (arg1, 0), flags);
3059 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3060 /* This is needed for conversions and for COMPONENT_REF.
3061 Might as well play it safe and always test this. */
3062 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3063 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3064 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3065 return 0;
3067 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3068 We don't care about side effects in that case because the SAVE_EXPR
3069 takes care of that for us. In all other cases, two expressions are
3070 equal if they have no side effects. If we have two identical
3071 expressions with side effects that should be treated the same due
3072 to the only side effects being identical SAVE_EXPR's, that will
3073 be detected in the recursive calls below. */
3074 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3075 && (TREE_CODE (arg0) == SAVE_EXPR
3076 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3077 return 1;
3079 /* Next handle constant cases, those for which we can return 1 even
3080 if ONLY_CONST is set. */
3081 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3082 switch (TREE_CODE (arg0))
3084 case INTEGER_CST:
3085 return tree_int_cst_equal (arg0, arg1);
3087 case FIXED_CST:
3088 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3089 TREE_FIXED_CST (arg1));
3091 case REAL_CST:
3092 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3093 TREE_REAL_CST (arg1)))
3094 return 1;
3097 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3099 /* If we do not distinguish between signed and unsigned zero,
3100 consider them equal. */
3101 if (real_zerop (arg0) && real_zerop (arg1))
3102 return 1;
3104 return 0;
3106 case VECTOR_CST:
3108 tree v1, v2;
3110 v1 = TREE_VECTOR_CST_ELTS (arg0);
3111 v2 = TREE_VECTOR_CST_ELTS (arg1);
3112 while (v1 && v2)
3114 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3115 flags))
3116 return 0;
3117 v1 = TREE_CHAIN (v1);
3118 v2 = TREE_CHAIN (v2);
3121 return v1 == v2;
3124 case COMPLEX_CST:
3125 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3126 flags)
3127 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3128 flags));
3130 case STRING_CST:
3131 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3132 && ! memcmp (TREE_STRING_POINTER (arg0),
3133 TREE_STRING_POINTER (arg1),
3134 TREE_STRING_LENGTH (arg0)));
3136 case ADDR_EXPR:
3137 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3139 default:
3140 break;
3143 if (flags & OEP_ONLY_CONST)
3144 return 0;
3146 /* Define macros to test an operand from arg0 and arg1 for equality and a
3147 variant that allows null and views null as being different from any
3148 non-null value. In the latter case, if either is null, the both
3149 must be; otherwise, do the normal comparison. */
3150 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3151 TREE_OPERAND (arg1, N), flags)
3153 #define OP_SAME_WITH_NULL(N) \
3154 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3155 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3157 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3159 case tcc_unary:
3160 /* Two conversions are equal only if signedness and modes match. */
3161 switch (TREE_CODE (arg0))
3163 CASE_CONVERT:
3164 case FIX_TRUNC_EXPR:
3165 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3166 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3167 return 0;
3168 break;
3169 default:
3170 break;
3173 return OP_SAME (0);
3176 case tcc_comparison:
3177 case tcc_binary:
3178 if (OP_SAME (0) && OP_SAME (1))
3179 return 1;
3181 /* For commutative ops, allow the other order. */
3182 return (commutative_tree_code (TREE_CODE (arg0))
3183 && operand_equal_p (TREE_OPERAND (arg0, 0),
3184 TREE_OPERAND (arg1, 1), flags)
3185 && operand_equal_p (TREE_OPERAND (arg0, 1),
3186 TREE_OPERAND (arg1, 0), flags));
3188 case tcc_reference:
3189 /* If either of the pointer (or reference) expressions we are
3190 dereferencing contain a side effect, these cannot be equal. */
3191 if (TREE_SIDE_EFFECTS (arg0)
3192 || TREE_SIDE_EFFECTS (arg1))
3193 return 0;
3195 switch (TREE_CODE (arg0))
3197 case INDIRECT_REF:
3198 case ALIGN_INDIRECT_REF:
3199 case MISALIGNED_INDIRECT_REF:
3200 case REALPART_EXPR:
3201 case IMAGPART_EXPR:
3202 return OP_SAME (0);
3204 case ARRAY_REF:
3205 case ARRAY_RANGE_REF:
3206 /* Operands 2 and 3 may be null.
3207 Compare the array index by value if it is constant first as we
3208 may have different types but same value here. */
3209 return (OP_SAME (0)
3210 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3211 TREE_OPERAND (arg1, 1))
3212 || OP_SAME (1))
3213 && OP_SAME_WITH_NULL (2)
3214 && OP_SAME_WITH_NULL (3));
3216 case COMPONENT_REF:
3217 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3218 may be NULL when we're called to compare MEM_EXPRs. */
3219 return OP_SAME_WITH_NULL (0)
3220 && OP_SAME (1)
3221 && OP_SAME_WITH_NULL (2);
3223 case BIT_FIELD_REF:
3224 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3226 default:
3227 return 0;
3230 case tcc_expression:
3231 switch (TREE_CODE (arg0))
3233 case ADDR_EXPR:
3234 case TRUTH_NOT_EXPR:
3235 return OP_SAME (0);
3237 case TRUTH_ANDIF_EXPR:
3238 case TRUTH_ORIF_EXPR:
3239 return OP_SAME (0) && OP_SAME (1);
3241 case TRUTH_AND_EXPR:
3242 case TRUTH_OR_EXPR:
3243 case TRUTH_XOR_EXPR:
3244 if (OP_SAME (0) && OP_SAME (1))
3245 return 1;
3247 /* Otherwise take into account this is a commutative operation. */
3248 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3249 TREE_OPERAND (arg1, 1), flags)
3250 && operand_equal_p (TREE_OPERAND (arg0, 1),
3251 TREE_OPERAND (arg1, 0), flags));
3253 case COND_EXPR:
3254 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3256 default:
3257 return 0;
3260 case tcc_vl_exp:
3261 switch (TREE_CODE (arg0))
3263 case CALL_EXPR:
3264 /* If the CALL_EXPRs call different functions, then they
3265 clearly can not be equal. */
3266 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3267 flags))
3268 return 0;
3271 unsigned int cef = call_expr_flags (arg0);
3272 if (flags & OEP_PURE_SAME)
3273 cef &= ECF_CONST | ECF_PURE;
3274 else
3275 cef &= ECF_CONST;
3276 if (!cef)
3277 return 0;
3280 /* Now see if all the arguments are the same. */
3282 const_call_expr_arg_iterator iter0, iter1;
3283 const_tree a0, a1;
3284 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3285 a1 = first_const_call_expr_arg (arg1, &iter1);
3286 a0 && a1;
3287 a0 = next_const_call_expr_arg (&iter0),
3288 a1 = next_const_call_expr_arg (&iter1))
3289 if (! operand_equal_p (a0, a1, flags))
3290 return 0;
3292 /* If we get here and both argument lists are exhausted
3293 then the CALL_EXPRs are equal. */
3294 return ! (a0 || a1);
3296 default:
3297 return 0;
3300 case tcc_declaration:
3301 /* Consider __builtin_sqrt equal to sqrt. */
3302 return (TREE_CODE (arg0) == FUNCTION_DECL
3303 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3304 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3305 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3307 default:
3308 return 0;
3311 #undef OP_SAME
3312 #undef OP_SAME_WITH_NULL
3315 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3316 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3318 When in doubt, return 0. */
3320 static int
3321 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3323 int unsignedp1, unsignedpo;
3324 tree primarg0, primarg1, primother;
3325 unsigned int correct_width;
3327 if (operand_equal_p (arg0, arg1, 0))
3328 return 1;
3330 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3331 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3332 return 0;
3334 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3335 and see if the inner values are the same. This removes any
3336 signedness comparison, which doesn't matter here. */
3337 primarg0 = arg0, primarg1 = arg1;
3338 STRIP_NOPS (primarg0);
3339 STRIP_NOPS (primarg1);
3340 if (operand_equal_p (primarg0, primarg1, 0))
3341 return 1;
3343 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3344 actual comparison operand, ARG0.
3346 First throw away any conversions to wider types
3347 already present in the operands. */
3349 primarg1 = get_narrower (arg1, &unsignedp1);
3350 primother = get_narrower (other, &unsignedpo);
3352 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3353 if (unsignedp1 == unsignedpo
3354 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3355 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3357 tree type = TREE_TYPE (arg0);
3359 /* Make sure shorter operand is extended the right way
3360 to match the longer operand. */
3361 primarg1 = fold_convert (signed_or_unsigned_type_for
3362 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3364 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3365 return 1;
3368 return 0;
3371 /* See if ARG is an expression that is either a comparison or is performing
3372 arithmetic on comparisons. The comparisons must only be comparing
3373 two different values, which will be stored in *CVAL1 and *CVAL2; if
3374 they are nonzero it means that some operands have already been found.
3375 No variables may be used anywhere else in the expression except in the
3376 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3377 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3379 If this is true, return 1. Otherwise, return zero. */
3381 static int
3382 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3384 enum tree_code code = TREE_CODE (arg);
3385 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3387 /* We can handle some of the tcc_expression cases here. */
3388 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3389 tclass = tcc_unary;
3390 else if (tclass == tcc_expression
3391 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3392 || code == COMPOUND_EXPR))
3393 tclass = tcc_binary;
3395 else if (tclass == tcc_expression && code == SAVE_EXPR
3396 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3398 /* If we've already found a CVAL1 or CVAL2, this expression is
3399 two complex to handle. */
3400 if (*cval1 || *cval2)
3401 return 0;
3403 tclass = tcc_unary;
3404 *save_p = 1;
3407 switch (tclass)
3409 case tcc_unary:
3410 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3412 case tcc_binary:
3413 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3414 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3415 cval1, cval2, save_p));
3417 case tcc_constant:
3418 return 1;
3420 case tcc_expression:
3421 if (code == COND_EXPR)
3422 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3423 cval1, cval2, save_p)
3424 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3425 cval1, cval2, save_p)
3426 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3427 cval1, cval2, save_p));
3428 return 0;
3430 case tcc_comparison:
3431 /* First see if we can handle the first operand, then the second. For
3432 the second operand, we know *CVAL1 can't be zero. It must be that
3433 one side of the comparison is each of the values; test for the
3434 case where this isn't true by failing if the two operands
3435 are the same. */
3437 if (operand_equal_p (TREE_OPERAND (arg, 0),
3438 TREE_OPERAND (arg, 1), 0))
3439 return 0;
3441 if (*cval1 == 0)
3442 *cval1 = TREE_OPERAND (arg, 0);
3443 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3445 else if (*cval2 == 0)
3446 *cval2 = TREE_OPERAND (arg, 0);
3447 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3449 else
3450 return 0;
3452 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3454 else if (*cval2 == 0)
3455 *cval2 = TREE_OPERAND (arg, 1);
3456 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3458 else
3459 return 0;
3461 return 1;
3463 default:
3464 return 0;
3468 /* ARG is a tree that is known to contain just arithmetic operations and
3469 comparisons. Evaluate the operations in the tree substituting NEW0 for
3470 any occurrence of OLD0 as an operand of a comparison and likewise for
3471 NEW1 and OLD1. */
3473 static tree
3474 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3476 tree type = TREE_TYPE (arg);
3477 enum tree_code code = TREE_CODE (arg);
3478 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3480 /* We can handle some of the tcc_expression cases here. */
3481 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3482 tclass = tcc_unary;
3483 else if (tclass == tcc_expression
3484 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3485 tclass = tcc_binary;
3487 switch (tclass)
3489 case tcc_unary:
3490 return fold_build1 (code, type,
3491 eval_subst (TREE_OPERAND (arg, 0),
3492 old0, new0, old1, new1));
3494 case tcc_binary:
3495 return fold_build2 (code, type,
3496 eval_subst (TREE_OPERAND (arg, 0),
3497 old0, new0, old1, new1),
3498 eval_subst (TREE_OPERAND (arg, 1),
3499 old0, new0, old1, new1));
3501 case tcc_expression:
3502 switch (code)
3504 case SAVE_EXPR:
3505 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3507 case COMPOUND_EXPR:
3508 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3510 case COND_EXPR:
3511 return fold_build3 (code, type,
3512 eval_subst (TREE_OPERAND (arg, 0),
3513 old0, new0, old1, new1),
3514 eval_subst (TREE_OPERAND (arg, 1),
3515 old0, new0, old1, new1),
3516 eval_subst (TREE_OPERAND (arg, 2),
3517 old0, new0, old1, new1));
3518 default:
3519 break;
3521 /* Fall through - ??? */
3523 case tcc_comparison:
3525 tree arg0 = TREE_OPERAND (arg, 0);
3526 tree arg1 = TREE_OPERAND (arg, 1);
3528 /* We need to check both for exact equality and tree equality. The
3529 former will be true if the operand has a side-effect. In that
3530 case, we know the operand occurred exactly once. */
3532 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3533 arg0 = new0;
3534 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3535 arg0 = new1;
3537 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3538 arg1 = new0;
3539 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3540 arg1 = new1;
3542 return fold_build2 (code, type, arg0, arg1);
3545 default:
3546 return arg;
3550 /* Return a tree for the case when the result of an expression is RESULT
3551 converted to TYPE and OMITTED was previously an operand of the expression
3552 but is now not needed (e.g., we folded OMITTED * 0).
3554 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3555 the conversion of RESULT to TYPE. */
3557 tree
3558 omit_one_operand (tree type, tree result, tree omitted)
3560 tree t = fold_convert (type, result);
3562 /* If the resulting operand is an empty statement, just return the omitted
3563 statement casted to void. */
3564 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3565 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3567 if (TREE_SIDE_EFFECTS (omitted))
3568 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3570 return non_lvalue (t);
3573 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3575 static tree
3576 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3578 tree t = fold_convert (type, result);
3580 /* If the resulting operand is an empty statement, just return the omitted
3581 statement casted to void. */
3582 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3583 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3585 if (TREE_SIDE_EFFECTS (omitted))
3586 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3588 return pedantic_non_lvalue (t);
3591 /* Return a tree for the case when the result of an expression is RESULT
3592 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3593 of the expression but are now not needed.
3595 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3596 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3597 evaluated before OMITTED2. Otherwise, if neither has side effects,
3598 just do the conversion of RESULT to TYPE. */
3600 tree
3601 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3603 tree t = fold_convert (type, result);
3605 if (TREE_SIDE_EFFECTS (omitted2))
3606 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3607 if (TREE_SIDE_EFFECTS (omitted1))
3608 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3610 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3614 /* Return a simplified tree node for the truth-negation of ARG. This
3615 never alters ARG itself. We assume that ARG is an operation that
3616 returns a truth value (0 or 1).
3618 FIXME: one would think we would fold the result, but it causes
3619 problems with the dominator optimizer. */
3621 tree
3622 fold_truth_not_expr (tree arg)
3624 tree type = TREE_TYPE (arg);
3625 enum tree_code code = TREE_CODE (arg);
3627 /* If this is a comparison, we can simply invert it, except for
3628 floating-point non-equality comparisons, in which case we just
3629 enclose a TRUTH_NOT_EXPR around what we have. */
3631 if (TREE_CODE_CLASS (code) == tcc_comparison)
3633 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3634 if (FLOAT_TYPE_P (op_type)
3635 && flag_trapping_math
3636 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3637 && code != NE_EXPR && code != EQ_EXPR)
3638 return NULL_TREE;
3639 else
3641 code = invert_tree_comparison (code,
3642 HONOR_NANS (TYPE_MODE (op_type)));
3643 if (code == ERROR_MARK)
3644 return NULL_TREE;
3645 else
3646 return build2 (code, type,
3647 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3651 switch (code)
3653 case INTEGER_CST:
3654 return constant_boolean_node (integer_zerop (arg), type);
3656 case TRUTH_AND_EXPR:
3657 return build2 (TRUTH_OR_EXPR, type,
3658 invert_truthvalue (TREE_OPERAND (arg, 0)),
3659 invert_truthvalue (TREE_OPERAND (arg, 1)));
3661 case TRUTH_OR_EXPR:
3662 return build2 (TRUTH_AND_EXPR, type,
3663 invert_truthvalue (TREE_OPERAND (arg, 0)),
3664 invert_truthvalue (TREE_OPERAND (arg, 1)));
3666 case TRUTH_XOR_EXPR:
3667 /* Here we can invert either operand. We invert the first operand
3668 unless the second operand is a TRUTH_NOT_EXPR in which case our
3669 result is the XOR of the first operand with the inside of the
3670 negation of the second operand. */
3672 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3673 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3674 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3675 else
3676 return build2 (TRUTH_XOR_EXPR, type,
3677 invert_truthvalue (TREE_OPERAND (arg, 0)),
3678 TREE_OPERAND (arg, 1));
3680 case TRUTH_ANDIF_EXPR:
3681 return build2 (TRUTH_ORIF_EXPR, type,
3682 invert_truthvalue (TREE_OPERAND (arg, 0)),
3683 invert_truthvalue (TREE_OPERAND (arg, 1)));
3685 case TRUTH_ORIF_EXPR:
3686 return build2 (TRUTH_ANDIF_EXPR, type,
3687 invert_truthvalue (TREE_OPERAND (arg, 0)),
3688 invert_truthvalue (TREE_OPERAND (arg, 1)));
3690 case TRUTH_NOT_EXPR:
3691 return TREE_OPERAND (arg, 0);
3693 case COND_EXPR:
3695 tree arg1 = TREE_OPERAND (arg, 1);
3696 tree arg2 = TREE_OPERAND (arg, 2);
3697 /* A COND_EXPR may have a throw as one operand, which
3698 then has void type. Just leave void operands
3699 as they are. */
3700 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3701 VOID_TYPE_P (TREE_TYPE (arg1))
3702 ? arg1 : invert_truthvalue (arg1),
3703 VOID_TYPE_P (TREE_TYPE (arg2))
3704 ? arg2 : invert_truthvalue (arg2));
3707 case COMPOUND_EXPR:
3708 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3709 invert_truthvalue (TREE_OPERAND (arg, 1)));
3711 case NON_LVALUE_EXPR:
3712 return invert_truthvalue (TREE_OPERAND (arg, 0));
3714 case NOP_EXPR:
3715 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3716 return build1 (TRUTH_NOT_EXPR, type, arg);
3718 case CONVERT_EXPR:
3719 case FLOAT_EXPR:
3720 return build1 (TREE_CODE (arg), type,
3721 invert_truthvalue (TREE_OPERAND (arg, 0)));
3723 case BIT_AND_EXPR:
3724 if (!integer_onep (TREE_OPERAND (arg, 1)))
3725 break;
3726 return build2 (EQ_EXPR, type, arg,
3727 build_int_cst (type, 0));
3729 case SAVE_EXPR:
3730 return build1 (TRUTH_NOT_EXPR, type, arg);
3732 case CLEANUP_POINT_EXPR:
3733 return build1 (CLEANUP_POINT_EXPR, type,
3734 invert_truthvalue (TREE_OPERAND (arg, 0)));
3736 default:
3737 break;
3740 return NULL_TREE;
3743 /* Return a simplified tree node for the truth-negation of ARG. This
3744 never alters ARG itself. We assume that ARG is an operation that
3745 returns a truth value (0 or 1).
3747 FIXME: one would think we would fold the result, but it causes
3748 problems with the dominator optimizer. */
3750 tree
3751 invert_truthvalue (tree arg)
3753 tree tem;
3755 if (TREE_CODE (arg) == ERROR_MARK)
3756 return arg;
3758 tem = fold_truth_not_expr (arg);
3759 if (!tem)
3760 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3762 return tem;
3765 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3766 operands are another bit-wise operation with a common input. If so,
3767 distribute the bit operations to save an operation and possibly two if
3768 constants are involved. For example, convert
3769 (A | B) & (A | C) into A | (B & C)
3770 Further simplification will occur if B and C are constants.
3772 If this optimization cannot be done, 0 will be returned. */
3774 static tree
3775 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3777 tree common;
3778 tree left, right;
3780 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3781 || TREE_CODE (arg0) == code
3782 || (TREE_CODE (arg0) != BIT_AND_EXPR
3783 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3784 return 0;
3786 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3788 common = TREE_OPERAND (arg0, 0);
3789 left = TREE_OPERAND (arg0, 1);
3790 right = TREE_OPERAND (arg1, 1);
3792 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3794 common = TREE_OPERAND (arg0, 0);
3795 left = TREE_OPERAND (arg0, 1);
3796 right = TREE_OPERAND (arg1, 0);
3798 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3800 common = TREE_OPERAND (arg0, 1);
3801 left = TREE_OPERAND (arg0, 0);
3802 right = TREE_OPERAND (arg1, 1);
3804 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3806 common = TREE_OPERAND (arg0, 1);
3807 left = TREE_OPERAND (arg0, 0);
3808 right = TREE_OPERAND (arg1, 0);
3810 else
3811 return 0;
3813 common = fold_convert (type, common);
3814 left = fold_convert (type, left);
3815 right = fold_convert (type, right);
3816 return fold_build2 (TREE_CODE (arg0), type, common,
3817 fold_build2 (code, type, left, right));
3820 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3821 with code CODE. This optimization is unsafe. */
3822 static tree
3823 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3825 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3826 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3828 /* (A / C) +- (B / C) -> (A +- B) / C. */
3829 if (mul0 == mul1
3830 && operand_equal_p (TREE_OPERAND (arg0, 1),
3831 TREE_OPERAND (arg1, 1), 0))
3832 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3833 fold_build2 (code, type,
3834 TREE_OPERAND (arg0, 0),
3835 TREE_OPERAND (arg1, 0)),
3836 TREE_OPERAND (arg0, 1));
3838 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3839 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3840 TREE_OPERAND (arg1, 0), 0)
3841 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3842 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3844 REAL_VALUE_TYPE r0, r1;
3845 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3846 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3847 if (!mul0)
3848 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3849 if (!mul1)
3850 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3851 real_arithmetic (&r0, code, &r0, &r1);
3852 return fold_build2 (MULT_EXPR, type,
3853 TREE_OPERAND (arg0, 0),
3854 build_real (type, r0));
3857 return NULL_TREE;
3860 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3861 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3863 static tree
3864 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3865 HOST_WIDE_INT bitpos, int unsignedp)
3867 tree result, bftype;
3869 if (bitpos == 0)
3871 tree size = TYPE_SIZE (TREE_TYPE (inner));
3872 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3873 || POINTER_TYPE_P (TREE_TYPE (inner)))
3874 && host_integerp (size, 0)
3875 && tree_low_cst (size, 0) == bitsize)
3876 return fold_convert (type, inner);
3879 bftype = type;
3880 if (TYPE_PRECISION (bftype) != bitsize
3881 || TYPE_UNSIGNED (bftype) == !unsignedp)
3882 bftype = build_nonstandard_integer_type (bitsize, 0);
3884 result = build3 (BIT_FIELD_REF, bftype, inner,
3885 size_int (bitsize), bitsize_int (bitpos));
3887 if (bftype != type)
3888 result = fold_convert (type, result);
3890 return result;
3893 /* Optimize a bit-field compare.
3895 There are two cases: First is a compare against a constant and the
3896 second is a comparison of two items where the fields are at the same
3897 bit position relative to the start of a chunk (byte, halfword, word)
3898 large enough to contain it. In these cases we can avoid the shift
3899 implicit in bitfield extractions.
3901 For constants, we emit a compare of the shifted constant with the
3902 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3903 compared. For two fields at the same position, we do the ANDs with the
3904 similar mask and compare the result of the ANDs.
3906 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3907 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3908 are the left and right operands of the comparison, respectively.
3910 If the optimization described above can be done, we return the resulting
3911 tree. Otherwise we return zero. */
3913 static tree
3914 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3915 tree lhs, tree rhs)
3917 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3918 tree type = TREE_TYPE (lhs);
3919 tree signed_type, unsigned_type;
3920 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3921 enum machine_mode lmode, rmode, nmode;
3922 int lunsignedp, runsignedp;
3923 int lvolatilep = 0, rvolatilep = 0;
3924 tree linner, rinner = NULL_TREE;
3925 tree mask;
3926 tree offset;
3928 /* Get all the information about the extractions being done. If the bit size
3929 if the same as the size of the underlying object, we aren't doing an
3930 extraction at all and so can do nothing. We also don't want to
3931 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3932 then will no longer be able to replace it. */
3933 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3934 &lunsignedp, &lvolatilep, false);
3935 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3936 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3937 return 0;
3939 if (!const_p)
3941 /* If this is not a constant, we can only do something if bit positions,
3942 sizes, and signedness are the same. */
3943 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3944 &runsignedp, &rvolatilep, false);
3946 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3947 || lunsignedp != runsignedp || offset != 0
3948 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3949 return 0;
3952 /* See if we can find a mode to refer to this field. We should be able to,
3953 but fail if we can't. */
3954 nmode = get_best_mode (lbitsize, lbitpos,
3955 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3956 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3957 TYPE_ALIGN (TREE_TYPE (rinner))),
3958 word_mode, lvolatilep || rvolatilep);
3959 if (nmode == VOIDmode)
3960 return 0;
3962 /* Set signed and unsigned types of the precision of this mode for the
3963 shifts below. */
3964 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3965 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3967 /* Compute the bit position and size for the new reference and our offset
3968 within it. If the new reference is the same size as the original, we
3969 won't optimize anything, so return zero. */
3970 nbitsize = GET_MODE_BITSIZE (nmode);
3971 nbitpos = lbitpos & ~ (nbitsize - 1);
3972 lbitpos -= nbitpos;
3973 if (nbitsize == lbitsize)
3974 return 0;
3976 if (BYTES_BIG_ENDIAN)
3977 lbitpos = nbitsize - lbitsize - lbitpos;
3979 /* Make the mask to be used against the extracted field. */
3980 mask = build_int_cst_type (unsigned_type, -1);
3981 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3982 mask = const_binop (RSHIFT_EXPR, mask,
3983 size_int (nbitsize - lbitsize - lbitpos), 0);
3985 if (! const_p)
3986 /* If not comparing with constant, just rework the comparison
3987 and return. */
3988 return fold_build2 (code, compare_type,
3989 fold_build2 (BIT_AND_EXPR, unsigned_type,
3990 make_bit_field_ref (linner,
3991 unsigned_type,
3992 nbitsize, nbitpos,
3994 mask),
3995 fold_build2 (BIT_AND_EXPR, unsigned_type,
3996 make_bit_field_ref (rinner,
3997 unsigned_type,
3998 nbitsize, nbitpos,
4000 mask));
4002 /* Otherwise, we are handling the constant case. See if the constant is too
4003 big for the field. Warn and return a tree of for 0 (false) if so. We do
4004 this not only for its own sake, but to avoid having to test for this
4005 error case below. If we didn't, we might generate wrong code.
4007 For unsigned fields, the constant shifted right by the field length should
4008 be all zero. For signed fields, the high-order bits should agree with
4009 the sign bit. */
4011 if (lunsignedp)
4013 if (! integer_zerop (const_binop (RSHIFT_EXPR,
4014 fold_convert (unsigned_type, rhs),
4015 size_int (lbitsize), 0)))
4017 warning (0, "comparison is always %d due to width of bit-field",
4018 code == NE_EXPR);
4019 return constant_boolean_node (code == NE_EXPR, compare_type);
4022 else
4024 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4025 size_int (lbitsize - 1), 0);
4026 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4028 warning (0, "comparison is always %d due to width of bit-field",
4029 code == NE_EXPR);
4030 return constant_boolean_node (code == NE_EXPR, compare_type);
4034 /* Single-bit compares should always be against zero. */
4035 if (lbitsize == 1 && ! integer_zerop (rhs))
4037 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4038 rhs = build_int_cst (type, 0);
4041 /* Make a new bitfield reference, shift the constant over the
4042 appropriate number of bits and mask it with the computed mask
4043 (in case this was a signed field). If we changed it, make a new one. */
4044 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4045 if (lvolatilep)
4047 TREE_SIDE_EFFECTS (lhs) = 1;
4048 TREE_THIS_VOLATILE (lhs) = 1;
4051 rhs = const_binop (BIT_AND_EXPR,
4052 const_binop (LSHIFT_EXPR,
4053 fold_convert (unsigned_type, rhs),
4054 size_int (lbitpos), 0),
4055 mask, 0);
4057 return build2 (code, compare_type,
4058 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4059 rhs);
4062 /* Subroutine for fold_truthop: decode a field reference.
4064 If EXP is a comparison reference, we return the innermost reference.
4066 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4067 set to the starting bit number.
4069 If the innermost field can be completely contained in a mode-sized
4070 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4072 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4073 otherwise it is not changed.
4075 *PUNSIGNEDP is set to the signedness of the field.
4077 *PMASK is set to the mask used. This is either contained in a
4078 BIT_AND_EXPR or derived from the width of the field.
4080 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4082 Return 0 if this is not a component reference or is one that we can't
4083 do anything with. */
4085 static tree
4086 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4087 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4088 int *punsignedp, int *pvolatilep,
4089 tree *pmask, tree *pand_mask)
4091 tree outer_type = 0;
4092 tree and_mask = 0;
4093 tree mask, inner, offset;
4094 tree unsigned_type;
4095 unsigned int precision;
4097 /* All the optimizations using this function assume integer fields.
4098 There are problems with FP fields since the type_for_size call
4099 below can fail for, e.g., XFmode. */
4100 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4101 return 0;
4103 /* We are interested in the bare arrangement of bits, so strip everything
4104 that doesn't affect the machine mode. However, record the type of the
4105 outermost expression if it may matter below. */
4106 if (CONVERT_EXPR_P (exp)
4107 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4108 outer_type = TREE_TYPE (exp);
4109 STRIP_NOPS (exp);
4111 if (TREE_CODE (exp) == BIT_AND_EXPR)
4113 and_mask = TREE_OPERAND (exp, 1);
4114 exp = TREE_OPERAND (exp, 0);
4115 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4116 if (TREE_CODE (and_mask) != INTEGER_CST)
4117 return 0;
4120 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4121 punsignedp, pvolatilep, false);
4122 if ((inner == exp && and_mask == 0)
4123 || *pbitsize < 0 || offset != 0
4124 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4125 return 0;
4127 /* If the number of bits in the reference is the same as the bitsize of
4128 the outer type, then the outer type gives the signedness. Otherwise
4129 (in case of a small bitfield) the signedness is unchanged. */
4130 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4131 *punsignedp = TYPE_UNSIGNED (outer_type);
4133 /* Compute the mask to access the bitfield. */
4134 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4135 precision = TYPE_PRECISION (unsigned_type);
4137 mask = build_int_cst_type (unsigned_type, -1);
4139 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4140 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4142 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4143 if (and_mask != 0)
4144 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4145 fold_convert (unsigned_type, and_mask), mask);
4147 *pmask = mask;
4148 *pand_mask = and_mask;
4149 return inner;
4152 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4153 bit positions. */
4155 static int
4156 all_ones_mask_p (const_tree mask, int size)
4158 tree type = TREE_TYPE (mask);
4159 unsigned int precision = TYPE_PRECISION (type);
4160 tree tmask;
4162 tmask = build_int_cst_type (signed_type_for (type), -1);
4164 return
4165 tree_int_cst_equal (mask,
4166 const_binop (RSHIFT_EXPR,
4167 const_binop (LSHIFT_EXPR, tmask,
4168 size_int (precision - size),
4170 size_int (precision - size), 0));
4173 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4174 represents the sign bit of EXP's type. If EXP represents a sign
4175 or zero extension, also test VAL against the unextended type.
4176 The return value is the (sub)expression whose sign bit is VAL,
4177 or NULL_TREE otherwise. */
4179 static tree
4180 sign_bit_p (tree exp, const_tree val)
4182 unsigned HOST_WIDE_INT mask_lo, lo;
4183 HOST_WIDE_INT mask_hi, hi;
4184 int width;
4185 tree t;
4187 /* Tree EXP must have an integral type. */
4188 t = TREE_TYPE (exp);
4189 if (! INTEGRAL_TYPE_P (t))
4190 return NULL_TREE;
4192 /* Tree VAL must be an integer constant. */
4193 if (TREE_CODE (val) != INTEGER_CST
4194 || TREE_OVERFLOW (val))
4195 return NULL_TREE;
4197 width = TYPE_PRECISION (t);
4198 if (width > HOST_BITS_PER_WIDE_INT)
4200 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4201 lo = 0;
4203 mask_hi = ((unsigned HOST_WIDE_INT) -1
4204 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4205 mask_lo = -1;
4207 else
4209 hi = 0;
4210 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4212 mask_hi = 0;
4213 mask_lo = ((unsigned HOST_WIDE_INT) -1
4214 >> (HOST_BITS_PER_WIDE_INT - width));
4217 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4218 treat VAL as if it were unsigned. */
4219 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4220 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4221 return exp;
4223 /* Handle extension from a narrower type. */
4224 if (TREE_CODE (exp) == NOP_EXPR
4225 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4226 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4228 return NULL_TREE;
4231 /* Subroutine for fold_truthop: determine if an operand is simple enough
4232 to be evaluated unconditionally. */
4234 static int
4235 simple_operand_p (const_tree exp)
4237 /* Strip any conversions that don't change the machine mode. */
4238 STRIP_NOPS (exp);
4240 return (CONSTANT_CLASS_P (exp)
4241 || TREE_CODE (exp) == SSA_NAME
4242 || (DECL_P (exp)
4243 && ! TREE_ADDRESSABLE (exp)
4244 && ! TREE_THIS_VOLATILE (exp)
4245 && ! DECL_NONLOCAL (exp)
4246 /* Don't regard global variables as simple. They may be
4247 allocated in ways unknown to the compiler (shared memory,
4248 #pragma weak, etc). */
4249 && ! TREE_PUBLIC (exp)
4250 && ! DECL_EXTERNAL (exp)
4251 /* Loading a static variable is unduly expensive, but global
4252 registers aren't expensive. */
4253 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4256 /* The following functions are subroutines to fold_range_test and allow it to
4257 try to change a logical combination of comparisons into a range test.
4259 For example, both
4260 X == 2 || X == 3 || X == 4 || X == 5
4262 X >= 2 && X <= 5
4263 are converted to
4264 (unsigned) (X - 2) <= 3
4266 We describe each set of comparisons as being either inside or outside
4267 a range, using a variable named like IN_P, and then describe the
4268 range with a lower and upper bound. If one of the bounds is omitted,
4269 it represents either the highest or lowest value of the type.
4271 In the comments below, we represent a range by two numbers in brackets
4272 preceded by a "+" to designate being inside that range, or a "-" to
4273 designate being outside that range, so the condition can be inverted by
4274 flipping the prefix. An omitted bound is represented by a "-". For
4275 example, "- [-, 10]" means being outside the range starting at the lowest
4276 possible value and ending at 10, in other words, being greater than 10.
4277 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4278 always false.
4280 We set up things so that the missing bounds are handled in a consistent
4281 manner so neither a missing bound nor "true" and "false" need to be
4282 handled using a special case. */
4284 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4285 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4286 and UPPER1_P are nonzero if the respective argument is an upper bound
4287 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4288 must be specified for a comparison. ARG1 will be converted to ARG0's
4289 type if both are specified. */
4291 static tree
4292 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4293 tree arg1, int upper1_p)
4295 tree tem;
4296 int result;
4297 int sgn0, sgn1;
4299 /* If neither arg represents infinity, do the normal operation.
4300 Else, if not a comparison, return infinity. Else handle the special
4301 comparison rules. Note that most of the cases below won't occur, but
4302 are handled for consistency. */
4304 if (arg0 != 0 && arg1 != 0)
4306 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4307 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4308 STRIP_NOPS (tem);
4309 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4312 if (TREE_CODE_CLASS (code) != tcc_comparison)
4313 return 0;
4315 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4316 for neither. In real maths, we cannot assume open ended ranges are
4317 the same. But, this is computer arithmetic, where numbers are finite.
4318 We can therefore make the transformation of any unbounded range with
4319 the value Z, Z being greater than any representable number. This permits
4320 us to treat unbounded ranges as equal. */
4321 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4322 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4323 switch (code)
4325 case EQ_EXPR:
4326 result = sgn0 == sgn1;
4327 break;
4328 case NE_EXPR:
4329 result = sgn0 != sgn1;
4330 break;
4331 case LT_EXPR:
4332 result = sgn0 < sgn1;
4333 break;
4334 case LE_EXPR:
4335 result = sgn0 <= sgn1;
4336 break;
4337 case GT_EXPR:
4338 result = sgn0 > sgn1;
4339 break;
4340 case GE_EXPR:
4341 result = sgn0 >= sgn1;
4342 break;
4343 default:
4344 gcc_unreachable ();
4347 return constant_boolean_node (result, type);
4350 /* Given EXP, a logical expression, set the range it is testing into
4351 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4352 actually being tested. *PLOW and *PHIGH will be made of the same
4353 type as the returned expression. If EXP is not a comparison, we
4354 will most likely not be returning a useful value and range. Set
4355 *STRICT_OVERFLOW_P to true if the return value is only valid
4356 because signed overflow is undefined; otherwise, do not change
4357 *STRICT_OVERFLOW_P. */
4359 static tree
4360 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4361 bool *strict_overflow_p)
4363 enum tree_code code;
4364 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4365 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4366 int in_p, n_in_p;
4367 tree low, high, n_low, n_high;
4369 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4370 and see if we can refine the range. Some of the cases below may not
4371 happen, but it doesn't seem worth worrying about this. We "continue"
4372 the outer loop when we've changed something; otherwise we "break"
4373 the switch, which will "break" the while. */
4375 in_p = 0;
4376 low = high = build_int_cst (TREE_TYPE (exp), 0);
4378 while (1)
4380 code = TREE_CODE (exp);
4381 exp_type = TREE_TYPE (exp);
4383 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4385 if (TREE_OPERAND_LENGTH (exp) > 0)
4386 arg0 = TREE_OPERAND (exp, 0);
4387 if (TREE_CODE_CLASS (code) == tcc_comparison
4388 || TREE_CODE_CLASS (code) == tcc_unary
4389 || TREE_CODE_CLASS (code) == tcc_binary)
4390 arg0_type = TREE_TYPE (arg0);
4391 if (TREE_CODE_CLASS (code) == tcc_binary
4392 || TREE_CODE_CLASS (code) == tcc_comparison
4393 || (TREE_CODE_CLASS (code) == tcc_expression
4394 && TREE_OPERAND_LENGTH (exp) > 1))
4395 arg1 = TREE_OPERAND (exp, 1);
4398 switch (code)
4400 case TRUTH_NOT_EXPR:
4401 in_p = ! in_p, exp = arg0;
4402 continue;
4404 case EQ_EXPR: case NE_EXPR:
4405 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4406 /* We can only do something if the range is testing for zero
4407 and if the second operand is an integer constant. Note that
4408 saying something is "in" the range we make is done by
4409 complementing IN_P since it will set in the initial case of
4410 being not equal to zero; "out" is leaving it alone. */
4411 if (low == 0 || high == 0
4412 || ! integer_zerop (low) || ! integer_zerop (high)
4413 || TREE_CODE (arg1) != INTEGER_CST)
4414 break;
4416 switch (code)
4418 case NE_EXPR: /* - [c, c] */
4419 low = high = arg1;
4420 break;
4421 case EQ_EXPR: /* + [c, c] */
4422 in_p = ! in_p, low = high = arg1;
4423 break;
4424 case GT_EXPR: /* - [-, c] */
4425 low = 0, high = arg1;
4426 break;
4427 case GE_EXPR: /* + [c, -] */
4428 in_p = ! in_p, low = arg1, high = 0;
4429 break;
4430 case LT_EXPR: /* - [c, -] */
4431 low = arg1, high = 0;
4432 break;
4433 case LE_EXPR: /* + [-, c] */
4434 in_p = ! in_p, low = 0, high = arg1;
4435 break;
4436 default:
4437 gcc_unreachable ();
4440 /* If this is an unsigned comparison, we also know that EXP is
4441 greater than or equal to zero. We base the range tests we make
4442 on that fact, so we record it here so we can parse existing
4443 range tests. We test arg0_type since often the return type
4444 of, e.g. EQ_EXPR, is boolean. */
4445 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4447 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4448 in_p, low, high, 1,
4449 build_int_cst (arg0_type, 0),
4450 NULL_TREE))
4451 break;
4453 in_p = n_in_p, low = n_low, high = n_high;
4455 /* If the high bound is missing, but we have a nonzero low
4456 bound, reverse the range so it goes from zero to the low bound
4457 minus 1. */
4458 if (high == 0 && low && ! integer_zerop (low))
4460 in_p = ! in_p;
4461 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4462 integer_one_node, 0);
4463 low = build_int_cst (arg0_type, 0);
4467 exp = arg0;
4468 continue;
4470 case NEGATE_EXPR:
4471 /* (-x) IN [a,b] -> x in [-b, -a] */
4472 n_low = range_binop (MINUS_EXPR, exp_type,
4473 build_int_cst (exp_type, 0),
4474 0, high, 1);
4475 n_high = range_binop (MINUS_EXPR, exp_type,
4476 build_int_cst (exp_type, 0),
4477 0, low, 0);
4478 low = n_low, high = n_high;
4479 exp = arg0;
4480 continue;
4482 case BIT_NOT_EXPR:
4483 /* ~ X -> -X - 1 */
4484 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4485 build_int_cst (exp_type, 1));
4486 continue;
4488 case PLUS_EXPR: case MINUS_EXPR:
4489 if (TREE_CODE (arg1) != INTEGER_CST)
4490 break;
4492 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4493 move a constant to the other side. */
4494 if (!TYPE_UNSIGNED (arg0_type)
4495 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4496 break;
4498 /* If EXP is signed, any overflow in the computation is undefined,
4499 so we don't worry about it so long as our computations on
4500 the bounds don't overflow. For unsigned, overflow is defined
4501 and this is exactly the right thing. */
4502 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4503 arg0_type, low, 0, arg1, 0);
4504 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4505 arg0_type, high, 1, arg1, 0);
4506 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4507 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4508 break;
4510 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4511 *strict_overflow_p = true;
4513 /* Check for an unsigned range which has wrapped around the maximum
4514 value thus making n_high < n_low, and normalize it. */
4515 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4517 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4518 integer_one_node, 0);
4519 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4520 integer_one_node, 0);
4522 /* If the range is of the form +/- [ x+1, x ], we won't
4523 be able to normalize it. But then, it represents the
4524 whole range or the empty set, so make it
4525 +/- [ -, - ]. */
4526 if (tree_int_cst_equal (n_low, low)
4527 && tree_int_cst_equal (n_high, high))
4528 low = high = 0;
4529 else
4530 in_p = ! in_p;
4532 else
4533 low = n_low, high = n_high;
4535 exp = arg0;
4536 continue;
4538 CASE_CONVERT: case NON_LVALUE_EXPR:
4539 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4540 break;
4542 if (! INTEGRAL_TYPE_P (arg0_type)
4543 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4544 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4545 break;
4547 n_low = low, n_high = high;
4549 if (n_low != 0)
4550 n_low = fold_convert (arg0_type, n_low);
4552 if (n_high != 0)
4553 n_high = fold_convert (arg0_type, n_high);
4556 /* If we're converting arg0 from an unsigned type, to exp,
4557 a signed type, we will be doing the comparison as unsigned.
4558 The tests above have already verified that LOW and HIGH
4559 are both positive.
4561 So we have to ensure that we will handle large unsigned
4562 values the same way that the current signed bounds treat
4563 negative values. */
4565 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4567 tree high_positive;
4568 tree equiv_type;
4569 /* For fixed-point modes, we need to pass the saturating flag
4570 as the 2nd parameter. */
4571 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4572 equiv_type = lang_hooks.types.type_for_mode
4573 (TYPE_MODE (arg0_type),
4574 TYPE_SATURATING (arg0_type));
4575 else
4576 equiv_type = lang_hooks.types.type_for_mode
4577 (TYPE_MODE (arg0_type), 1);
4579 /* A range without an upper bound is, naturally, unbounded.
4580 Since convert would have cropped a very large value, use
4581 the max value for the destination type. */
4582 high_positive
4583 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4584 : TYPE_MAX_VALUE (arg0_type);
4586 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4587 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4588 fold_convert (arg0_type,
4589 high_positive),
4590 build_int_cst (arg0_type, 1));
4592 /* If the low bound is specified, "and" the range with the
4593 range for which the original unsigned value will be
4594 positive. */
4595 if (low != 0)
4597 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4598 1, n_low, n_high, 1,
4599 fold_convert (arg0_type,
4600 integer_zero_node),
4601 high_positive))
4602 break;
4604 in_p = (n_in_p == in_p);
4606 else
4608 /* Otherwise, "or" the range with the range of the input
4609 that will be interpreted as negative. */
4610 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4611 0, n_low, n_high, 1,
4612 fold_convert (arg0_type,
4613 integer_zero_node),
4614 high_positive))
4615 break;
4617 in_p = (in_p != n_in_p);
4621 exp = arg0;
4622 low = n_low, high = n_high;
4623 continue;
4625 default:
4626 break;
4629 break;
4632 /* If EXP is a constant, we can evaluate whether this is true or false. */
4633 if (TREE_CODE (exp) == INTEGER_CST)
4635 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4636 exp, 0, low, 0))
4637 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4638 exp, 1, high, 1)));
4639 low = high = 0;
4640 exp = 0;
4643 *pin_p = in_p, *plow = low, *phigh = high;
4644 return exp;
4647 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4648 type, TYPE, return an expression to test if EXP is in (or out of, depending
4649 on IN_P) the range. Return 0 if the test couldn't be created. */
4651 static tree
4652 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4654 tree etype = TREE_TYPE (exp);
4655 tree value;
4657 #ifdef HAVE_canonicalize_funcptr_for_compare
4658 /* Disable this optimization for function pointer expressions
4659 on targets that require function pointer canonicalization. */
4660 if (HAVE_canonicalize_funcptr_for_compare
4661 && TREE_CODE (etype) == POINTER_TYPE
4662 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4663 return NULL_TREE;
4664 #endif
4666 if (! in_p)
4668 value = build_range_check (type, exp, 1, low, high);
4669 if (value != 0)
4670 return invert_truthvalue (value);
4672 return 0;
4675 if (low == 0 && high == 0)
4676 return build_int_cst (type, 1);
4678 if (low == 0)
4679 return fold_build2 (LE_EXPR, type, exp,
4680 fold_convert (etype, high));
4682 if (high == 0)
4683 return fold_build2 (GE_EXPR, type, exp,
4684 fold_convert (etype, low));
4686 if (operand_equal_p (low, high, 0))
4687 return fold_build2 (EQ_EXPR, type, exp,
4688 fold_convert (etype, low));
4690 if (integer_zerop (low))
4692 if (! TYPE_UNSIGNED (etype))
4694 etype = unsigned_type_for (etype);
4695 high = fold_convert (etype, high);
4696 exp = fold_convert (etype, exp);
4698 return build_range_check (type, exp, 1, 0, high);
4701 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4702 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4704 unsigned HOST_WIDE_INT lo;
4705 HOST_WIDE_INT hi;
4706 int prec;
4708 prec = TYPE_PRECISION (etype);
4709 if (prec <= HOST_BITS_PER_WIDE_INT)
4711 hi = 0;
4712 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4714 else
4716 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4717 lo = (unsigned HOST_WIDE_INT) -1;
4720 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4722 if (TYPE_UNSIGNED (etype))
4724 tree signed_etype = signed_type_for (etype);
4725 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4726 etype
4727 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4728 else
4729 etype = signed_etype;
4730 exp = fold_convert (etype, exp);
4732 return fold_build2 (GT_EXPR, type, exp,
4733 build_int_cst (etype, 0));
4737 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4738 This requires wrap-around arithmetics for the type of the expression. */
4739 switch (TREE_CODE (etype))
4741 case INTEGER_TYPE:
4742 /* There is no requirement that LOW be within the range of ETYPE
4743 if the latter is a subtype. It must, however, be within the base
4744 type of ETYPE. So be sure we do the subtraction in that type. */
4745 if (TREE_TYPE (etype))
4746 etype = TREE_TYPE (etype);
4747 break;
4749 case ENUMERAL_TYPE:
4750 case BOOLEAN_TYPE:
4751 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4752 TYPE_UNSIGNED (etype));
4753 break;
4755 default:
4756 break;
4759 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4760 if (TREE_CODE (etype) == INTEGER_TYPE
4761 && !TYPE_OVERFLOW_WRAPS (etype))
4763 tree utype, minv, maxv;
4765 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4766 for the type in question, as we rely on this here. */
4767 utype = unsigned_type_for (etype);
4768 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4769 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4770 integer_one_node, 1);
4771 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4773 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4774 minv, 1, maxv, 1)))
4775 etype = utype;
4776 else
4777 return 0;
4780 high = fold_convert (etype, high);
4781 low = fold_convert (etype, low);
4782 exp = fold_convert (etype, exp);
4784 value = const_binop (MINUS_EXPR, high, low, 0);
4787 if (POINTER_TYPE_P (etype))
4789 if (value != 0 && !TREE_OVERFLOW (value))
4791 low = fold_convert (sizetype, low);
4792 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4793 return build_range_check (type,
4794 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4795 1, build_int_cst (etype, 0), value);
4797 return 0;
4800 if (value != 0 && !TREE_OVERFLOW (value))
4801 return build_range_check (type,
4802 fold_build2 (MINUS_EXPR, etype, exp, low),
4803 1, build_int_cst (etype, 0), value);
4805 return 0;
4808 /* Return the predecessor of VAL in its type, handling the infinite case. */
4810 static tree
4811 range_predecessor (tree val)
4813 tree type = TREE_TYPE (val);
4815 if (INTEGRAL_TYPE_P (type)
4816 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4817 return 0;
4818 else
4819 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4822 /* Return the successor of VAL in its type, handling the infinite case. */
4824 static tree
4825 range_successor (tree val)
4827 tree type = TREE_TYPE (val);
4829 if (INTEGRAL_TYPE_P (type)
4830 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4831 return 0;
4832 else
4833 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4836 /* Given two ranges, see if we can merge them into one. Return 1 if we
4837 can, 0 if we can't. Set the output range into the specified parameters. */
4839 static int
4840 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4841 tree high0, int in1_p, tree low1, tree high1)
4843 int no_overlap;
4844 int subset;
4845 int temp;
4846 tree tem;
4847 int in_p;
4848 tree low, high;
4849 int lowequal = ((low0 == 0 && low1 == 0)
4850 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4851 low0, 0, low1, 0)));
4852 int highequal = ((high0 == 0 && high1 == 0)
4853 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4854 high0, 1, high1, 1)));
4856 /* Make range 0 be the range that starts first, or ends last if they
4857 start at the same value. Swap them if it isn't. */
4858 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4859 low0, 0, low1, 0))
4860 || (lowequal
4861 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4862 high1, 1, high0, 1))))
4864 temp = in0_p, in0_p = in1_p, in1_p = temp;
4865 tem = low0, low0 = low1, low1 = tem;
4866 tem = high0, high0 = high1, high1 = tem;
4869 /* Now flag two cases, whether the ranges are disjoint or whether the
4870 second range is totally subsumed in the first. Note that the tests
4871 below are simplified by the ones above. */
4872 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4873 high0, 1, low1, 0));
4874 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4875 high1, 1, high0, 1));
4877 /* We now have four cases, depending on whether we are including or
4878 excluding the two ranges. */
4879 if (in0_p && in1_p)
4881 /* If they don't overlap, the result is false. If the second range
4882 is a subset it is the result. Otherwise, the range is from the start
4883 of the second to the end of the first. */
4884 if (no_overlap)
4885 in_p = 0, low = high = 0;
4886 else if (subset)
4887 in_p = 1, low = low1, high = high1;
4888 else
4889 in_p = 1, low = low1, high = high0;
4892 else if (in0_p && ! in1_p)
4894 /* If they don't overlap, the result is the first range. If they are
4895 equal, the result is false. If the second range is a subset of the
4896 first, and the ranges begin at the same place, we go from just after
4897 the end of the second range to the end of the first. If the second
4898 range is not a subset of the first, or if it is a subset and both
4899 ranges end at the same place, the range starts at the start of the
4900 first range and ends just before the second range.
4901 Otherwise, we can't describe this as a single range. */
4902 if (no_overlap)
4903 in_p = 1, low = low0, high = high0;
4904 else if (lowequal && highequal)
4905 in_p = 0, low = high = 0;
4906 else if (subset && lowequal)
4908 low = range_successor (high1);
4909 high = high0;
4910 in_p = 1;
4911 if (low == 0)
4913 /* We are in the weird situation where high0 > high1 but
4914 high1 has no successor. Punt. */
4915 return 0;
4918 else if (! subset || highequal)
4920 low = low0;
4921 high = range_predecessor (low1);
4922 in_p = 1;
4923 if (high == 0)
4925 /* low0 < low1 but low1 has no predecessor. Punt. */
4926 return 0;
4929 else
4930 return 0;
4933 else if (! in0_p && in1_p)
4935 /* If they don't overlap, the result is the second range. If the second
4936 is a subset of the first, the result is false. Otherwise,
4937 the range starts just after the first range and ends at the
4938 end of the second. */
4939 if (no_overlap)
4940 in_p = 1, low = low1, high = high1;
4941 else if (subset || highequal)
4942 in_p = 0, low = high = 0;
4943 else
4945 low = range_successor (high0);
4946 high = high1;
4947 in_p = 1;
4948 if (low == 0)
4950 /* high1 > high0 but high0 has no successor. Punt. */
4951 return 0;
4956 else
4958 /* The case where we are excluding both ranges. Here the complex case
4959 is if they don't overlap. In that case, the only time we have a
4960 range is if they are adjacent. If the second is a subset of the
4961 first, the result is the first. Otherwise, the range to exclude
4962 starts at the beginning of the first range and ends at the end of the
4963 second. */
4964 if (no_overlap)
4966 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4967 range_successor (high0),
4968 1, low1, 0)))
4969 in_p = 0, low = low0, high = high1;
4970 else
4972 /* Canonicalize - [min, x] into - [-, x]. */
4973 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4974 switch (TREE_CODE (TREE_TYPE (low0)))
4976 case ENUMERAL_TYPE:
4977 if (TYPE_PRECISION (TREE_TYPE (low0))
4978 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4979 break;
4980 /* FALLTHROUGH */
4981 case INTEGER_TYPE:
4982 if (tree_int_cst_equal (low0,
4983 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4984 low0 = 0;
4985 break;
4986 case POINTER_TYPE:
4987 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4988 && integer_zerop (low0))
4989 low0 = 0;
4990 break;
4991 default:
4992 break;
4995 /* Canonicalize - [x, max] into - [x, -]. */
4996 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4997 switch (TREE_CODE (TREE_TYPE (high1)))
4999 case ENUMERAL_TYPE:
5000 if (TYPE_PRECISION (TREE_TYPE (high1))
5001 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5002 break;
5003 /* FALLTHROUGH */
5004 case INTEGER_TYPE:
5005 if (tree_int_cst_equal (high1,
5006 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5007 high1 = 0;
5008 break;
5009 case POINTER_TYPE:
5010 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5011 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5012 high1, 1,
5013 integer_one_node, 1)))
5014 high1 = 0;
5015 break;
5016 default:
5017 break;
5020 /* The ranges might be also adjacent between the maximum and
5021 minimum values of the given type. For
5022 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5023 return + [x + 1, y - 1]. */
5024 if (low0 == 0 && high1 == 0)
5026 low = range_successor (high0);
5027 high = range_predecessor (low1);
5028 if (low == 0 || high == 0)
5029 return 0;
5031 in_p = 1;
5033 else
5034 return 0;
5037 else if (subset)
5038 in_p = 0, low = low0, high = high0;
5039 else
5040 in_p = 0, low = low0, high = high1;
5043 *pin_p = in_p, *plow = low, *phigh = high;
5044 return 1;
5048 /* Subroutine of fold, looking inside expressions of the form
5049 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5050 of the COND_EXPR. This function is being used also to optimize
5051 A op B ? C : A, by reversing the comparison first.
5053 Return a folded expression whose code is not a COND_EXPR
5054 anymore, or NULL_TREE if no folding opportunity is found. */
5056 static tree
5057 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5059 enum tree_code comp_code = TREE_CODE (arg0);
5060 tree arg00 = TREE_OPERAND (arg0, 0);
5061 tree arg01 = TREE_OPERAND (arg0, 1);
5062 tree arg1_type = TREE_TYPE (arg1);
5063 tree tem;
5065 STRIP_NOPS (arg1);
5066 STRIP_NOPS (arg2);
5068 /* If we have A op 0 ? A : -A, consider applying the following
5069 transformations:
5071 A == 0? A : -A same as -A
5072 A != 0? A : -A same as A
5073 A >= 0? A : -A same as abs (A)
5074 A > 0? A : -A same as abs (A)
5075 A <= 0? A : -A same as -abs (A)
5076 A < 0? A : -A same as -abs (A)
5078 None of these transformations work for modes with signed
5079 zeros. If A is +/-0, the first two transformations will
5080 change the sign of the result (from +0 to -0, or vice
5081 versa). The last four will fix the sign of the result,
5082 even though the original expressions could be positive or
5083 negative, depending on the sign of A.
5085 Note that all these transformations are correct if A is
5086 NaN, since the two alternatives (A and -A) are also NaNs. */
5087 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5088 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5089 ? real_zerop (arg01)
5090 : integer_zerop (arg01))
5091 && ((TREE_CODE (arg2) == NEGATE_EXPR
5092 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5093 /* In the case that A is of the form X-Y, '-A' (arg2) may
5094 have already been folded to Y-X, check for that. */
5095 || (TREE_CODE (arg1) == MINUS_EXPR
5096 && TREE_CODE (arg2) == MINUS_EXPR
5097 && operand_equal_p (TREE_OPERAND (arg1, 0),
5098 TREE_OPERAND (arg2, 1), 0)
5099 && operand_equal_p (TREE_OPERAND (arg1, 1),
5100 TREE_OPERAND (arg2, 0), 0))))
5101 switch (comp_code)
5103 case EQ_EXPR:
5104 case UNEQ_EXPR:
5105 tem = fold_convert (arg1_type, arg1);
5106 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5107 case NE_EXPR:
5108 case LTGT_EXPR:
5109 return pedantic_non_lvalue (fold_convert (type, arg1));
5110 case UNGE_EXPR:
5111 case UNGT_EXPR:
5112 if (flag_trapping_math)
5113 break;
5114 /* Fall through. */
5115 case GE_EXPR:
5116 case GT_EXPR:
5117 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5118 arg1 = fold_convert (signed_type_for
5119 (TREE_TYPE (arg1)), arg1);
5120 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5121 return pedantic_non_lvalue (fold_convert (type, tem));
5122 case UNLE_EXPR:
5123 case UNLT_EXPR:
5124 if (flag_trapping_math)
5125 break;
5126 case LE_EXPR:
5127 case LT_EXPR:
5128 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5129 arg1 = fold_convert (signed_type_for
5130 (TREE_TYPE (arg1)), arg1);
5131 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5132 return negate_expr (fold_convert (type, tem));
5133 default:
5134 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5135 break;
5138 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5139 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5140 both transformations are correct when A is NaN: A != 0
5141 is then true, and A == 0 is false. */
5143 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5144 && integer_zerop (arg01) && integer_zerop (arg2))
5146 if (comp_code == NE_EXPR)
5147 return pedantic_non_lvalue (fold_convert (type, arg1));
5148 else if (comp_code == EQ_EXPR)
5149 return build_int_cst (type, 0);
5152 /* Try some transformations of A op B ? A : B.
5154 A == B? A : B same as B
5155 A != B? A : B same as A
5156 A >= B? A : B same as max (A, B)
5157 A > B? A : B same as max (B, A)
5158 A <= B? A : B same as min (A, B)
5159 A < B? A : B same as min (B, A)
5161 As above, these transformations don't work in the presence
5162 of signed zeros. For example, if A and B are zeros of
5163 opposite sign, the first two transformations will change
5164 the sign of the result. In the last four, the original
5165 expressions give different results for (A=+0, B=-0) and
5166 (A=-0, B=+0), but the transformed expressions do not.
5168 The first two transformations are correct if either A or B
5169 is a NaN. In the first transformation, the condition will
5170 be false, and B will indeed be chosen. In the case of the
5171 second transformation, the condition A != B will be true,
5172 and A will be chosen.
5174 The conversions to max() and min() are not correct if B is
5175 a number and A is not. The conditions in the original
5176 expressions will be false, so all four give B. The min()
5177 and max() versions would give a NaN instead. */
5178 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5179 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5180 /* Avoid these transformations if the COND_EXPR may be used
5181 as an lvalue in the C++ front-end. PR c++/19199. */
5182 && (in_gimple_form
5183 || (strcmp (lang_hooks.name, "GNU C++") != 0
5184 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5185 || ! maybe_lvalue_p (arg1)
5186 || ! maybe_lvalue_p (arg2)))
5188 tree comp_op0 = arg00;
5189 tree comp_op1 = arg01;
5190 tree comp_type = TREE_TYPE (comp_op0);
5192 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5193 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5195 comp_type = type;
5196 comp_op0 = arg1;
5197 comp_op1 = arg2;
5200 switch (comp_code)
5202 case EQ_EXPR:
5203 return pedantic_non_lvalue (fold_convert (type, arg2));
5204 case NE_EXPR:
5205 return pedantic_non_lvalue (fold_convert (type, arg1));
5206 case LE_EXPR:
5207 case LT_EXPR:
5208 case UNLE_EXPR:
5209 case UNLT_EXPR:
5210 /* In C++ a ?: expression can be an lvalue, so put the
5211 operand which will be used if they are equal first
5212 so that we can convert this back to the
5213 corresponding COND_EXPR. */
5214 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5216 comp_op0 = fold_convert (comp_type, comp_op0);
5217 comp_op1 = fold_convert (comp_type, comp_op1);
5218 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5219 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5220 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5221 return pedantic_non_lvalue (fold_convert (type, tem));
5223 break;
5224 case GE_EXPR:
5225 case GT_EXPR:
5226 case UNGE_EXPR:
5227 case UNGT_EXPR:
5228 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5230 comp_op0 = fold_convert (comp_type, comp_op0);
5231 comp_op1 = fold_convert (comp_type, comp_op1);
5232 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5233 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5234 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5235 return pedantic_non_lvalue (fold_convert (type, tem));
5237 break;
5238 case UNEQ_EXPR:
5239 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5240 return pedantic_non_lvalue (fold_convert (type, arg2));
5241 break;
5242 case LTGT_EXPR:
5243 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5244 return pedantic_non_lvalue (fold_convert (type, arg1));
5245 break;
5246 default:
5247 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5248 break;
5252 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5253 we might still be able to simplify this. For example,
5254 if C1 is one less or one more than C2, this might have started
5255 out as a MIN or MAX and been transformed by this function.
5256 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5258 if (INTEGRAL_TYPE_P (type)
5259 && TREE_CODE (arg01) == INTEGER_CST
5260 && TREE_CODE (arg2) == INTEGER_CST)
5261 switch (comp_code)
5263 case EQ_EXPR:
5264 /* We can replace A with C1 in this case. */
5265 arg1 = fold_convert (type, arg01);
5266 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5268 case LT_EXPR:
5269 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5270 MIN_EXPR, to preserve the signedness of the comparison. */
5271 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5272 OEP_ONLY_CONST)
5273 && operand_equal_p (arg01,
5274 const_binop (PLUS_EXPR, arg2,
5275 build_int_cst (type, 1), 0),
5276 OEP_ONLY_CONST))
5278 tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
5279 fold_convert (TREE_TYPE (arg00), arg2));
5280 return pedantic_non_lvalue (fold_convert (type, tem));
5282 break;
5284 case LE_EXPR:
5285 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5286 as above. */
5287 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5288 OEP_ONLY_CONST)
5289 && operand_equal_p (arg01,
5290 const_binop (MINUS_EXPR, arg2,
5291 build_int_cst (type, 1), 0),
5292 OEP_ONLY_CONST))
5294 tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
5295 fold_convert (TREE_TYPE (arg00), arg2));
5296 return pedantic_non_lvalue (fold_convert (type, tem));
5298 break;
5300 case GT_EXPR:
5301 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5302 MAX_EXPR, to preserve the signedness of the comparison. */
5303 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5304 OEP_ONLY_CONST)
5305 && operand_equal_p (arg01,
5306 const_binop (MINUS_EXPR, arg2,
5307 build_int_cst (type, 1), 0),
5308 OEP_ONLY_CONST))
5310 tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
5311 fold_convert (TREE_TYPE (arg00), arg2));
5312 return pedantic_non_lvalue (fold_convert (type, tem));
5314 break;
5316 case GE_EXPR:
5317 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5318 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5319 OEP_ONLY_CONST)
5320 && operand_equal_p (arg01,
5321 const_binop (PLUS_EXPR, arg2,
5322 build_int_cst (type, 1), 0),
5323 OEP_ONLY_CONST))
5325 tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
5326 fold_convert (TREE_TYPE (arg00), arg2));
5327 return pedantic_non_lvalue (fold_convert (type, tem));
5329 break;
5330 case NE_EXPR:
5331 break;
5332 default:
5333 gcc_unreachable ();
5336 return NULL_TREE;
5341 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5342 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5343 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5344 false) >= 2)
5345 #endif
5347 /* EXP is some logical combination of boolean tests. See if we can
5348 merge it into some range test. Return the new tree if so. */
5350 static tree
5351 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5353 int or_op = (code == TRUTH_ORIF_EXPR
5354 || code == TRUTH_OR_EXPR);
5355 int in0_p, in1_p, in_p;
5356 tree low0, low1, low, high0, high1, high;
5357 bool strict_overflow_p = false;
5358 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5359 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5360 tree tem;
5361 const char * const warnmsg = G_("assuming signed overflow does not occur "
5362 "when simplifying range test");
5364 /* If this is an OR operation, invert both sides; we will invert
5365 again at the end. */
5366 if (or_op)
5367 in0_p = ! in0_p, in1_p = ! in1_p;
5369 /* If both expressions are the same, if we can merge the ranges, and we
5370 can build the range test, return it or it inverted. If one of the
5371 ranges is always true or always false, consider it to be the same
5372 expression as the other. */
5373 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5374 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5375 in1_p, low1, high1)
5376 && 0 != (tem = (build_range_check (type,
5377 lhs != 0 ? lhs
5378 : rhs != 0 ? rhs : integer_zero_node,
5379 in_p, low, high))))
5381 if (strict_overflow_p)
5382 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5383 return or_op ? invert_truthvalue (tem) : tem;
5386 /* On machines where the branch cost is expensive, if this is a
5387 short-circuited branch and the underlying object on both sides
5388 is the same, make a non-short-circuit operation. */
5389 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5390 && lhs != 0 && rhs != 0
5391 && (code == TRUTH_ANDIF_EXPR
5392 || code == TRUTH_ORIF_EXPR)
5393 && operand_equal_p (lhs, rhs, 0))
5395 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5396 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5397 which cases we can't do this. */
5398 if (simple_operand_p (lhs))
5399 return build2 (code == TRUTH_ANDIF_EXPR
5400 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5401 type, op0, op1);
5403 else if (lang_hooks.decls.global_bindings_p () == 0
5404 && ! CONTAINS_PLACEHOLDER_P (lhs))
5406 tree common = save_expr (lhs);
5408 if (0 != (lhs = build_range_check (type, common,
5409 or_op ? ! in0_p : in0_p,
5410 low0, high0))
5411 && (0 != (rhs = build_range_check (type, common,
5412 or_op ? ! in1_p : in1_p,
5413 low1, high1))))
5415 if (strict_overflow_p)
5416 fold_overflow_warning (warnmsg,
5417 WARN_STRICT_OVERFLOW_COMPARISON);
5418 return build2 (code == TRUTH_ANDIF_EXPR
5419 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5420 type, lhs, rhs);
5425 return 0;
5428 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5429 bit value. Arrange things so the extra bits will be set to zero if and
5430 only if C is signed-extended to its full width. If MASK is nonzero,
5431 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5433 static tree
5434 unextend (tree c, int p, int unsignedp, tree mask)
5436 tree type = TREE_TYPE (c);
5437 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5438 tree temp;
5440 if (p == modesize || unsignedp)
5441 return c;
5443 /* We work by getting just the sign bit into the low-order bit, then
5444 into the high-order bit, then sign-extend. We then XOR that value
5445 with C. */
5446 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5447 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5449 /* We must use a signed type in order to get an arithmetic right shift.
5450 However, we must also avoid introducing accidental overflows, so that
5451 a subsequent call to integer_zerop will work. Hence we must
5452 do the type conversion here. At this point, the constant is either
5453 zero or one, and the conversion to a signed type can never overflow.
5454 We could get an overflow if this conversion is done anywhere else. */
5455 if (TYPE_UNSIGNED (type))
5456 temp = fold_convert (signed_type_for (type), temp);
5458 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5459 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5460 if (mask != 0)
5461 temp = const_binop (BIT_AND_EXPR, temp,
5462 fold_convert (TREE_TYPE (c), mask), 0);
5463 /* If necessary, convert the type back to match the type of C. */
5464 if (TYPE_UNSIGNED (type))
5465 temp = fold_convert (type, temp);
5467 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5470 /* Find ways of folding logical expressions of LHS and RHS:
5471 Try to merge two comparisons to the same innermost item.
5472 Look for range tests like "ch >= '0' && ch <= '9'".
5473 Look for combinations of simple terms on machines with expensive branches
5474 and evaluate the RHS unconditionally.
5476 For example, if we have p->a == 2 && p->b == 4 and we can make an
5477 object large enough to span both A and B, we can do this with a comparison
5478 against the object ANDed with the a mask.
5480 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5481 operations to do this with one comparison.
5483 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5484 function and the one above.
5486 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5487 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5489 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5490 two operands.
5492 We return the simplified tree or 0 if no optimization is possible. */
5494 static tree
5495 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5497 /* If this is the "or" of two comparisons, we can do something if
5498 the comparisons are NE_EXPR. If this is the "and", we can do something
5499 if the comparisons are EQ_EXPR. I.e.,
5500 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5502 WANTED_CODE is this operation code. For single bit fields, we can
5503 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5504 comparison for one-bit fields. */
5506 enum tree_code wanted_code;
5507 enum tree_code lcode, rcode;
5508 tree ll_arg, lr_arg, rl_arg, rr_arg;
5509 tree ll_inner, lr_inner, rl_inner, rr_inner;
5510 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5511 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5512 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5513 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5514 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5515 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5516 enum machine_mode lnmode, rnmode;
5517 tree ll_mask, lr_mask, rl_mask, rr_mask;
5518 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5519 tree l_const, r_const;
5520 tree lntype, rntype, result;
5521 HOST_WIDE_INT first_bit, end_bit;
5522 int volatilep;
5523 tree orig_lhs = lhs, orig_rhs = rhs;
5524 enum tree_code orig_code = code;
5526 /* Start by getting the comparison codes. Fail if anything is volatile.
5527 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5528 it were surrounded with a NE_EXPR. */
5530 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5531 return 0;
5533 lcode = TREE_CODE (lhs);
5534 rcode = TREE_CODE (rhs);
5536 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5538 lhs = build2 (NE_EXPR, truth_type, lhs,
5539 build_int_cst (TREE_TYPE (lhs), 0));
5540 lcode = NE_EXPR;
5543 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5545 rhs = build2 (NE_EXPR, truth_type, rhs,
5546 build_int_cst (TREE_TYPE (rhs), 0));
5547 rcode = NE_EXPR;
5550 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5551 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5552 return 0;
5554 ll_arg = TREE_OPERAND (lhs, 0);
5555 lr_arg = TREE_OPERAND (lhs, 1);
5556 rl_arg = TREE_OPERAND (rhs, 0);
5557 rr_arg = TREE_OPERAND (rhs, 1);
5559 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5560 if (simple_operand_p (ll_arg)
5561 && simple_operand_p (lr_arg))
5563 tree result;
5564 if (operand_equal_p (ll_arg, rl_arg, 0)
5565 && operand_equal_p (lr_arg, rr_arg, 0))
5567 result = combine_comparisons (code, lcode, rcode,
5568 truth_type, ll_arg, lr_arg);
5569 if (result)
5570 return result;
5572 else if (operand_equal_p (ll_arg, rr_arg, 0)
5573 && operand_equal_p (lr_arg, rl_arg, 0))
5575 result = combine_comparisons (code, lcode,
5576 swap_tree_comparison (rcode),
5577 truth_type, ll_arg, lr_arg);
5578 if (result)
5579 return result;
5583 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5584 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5586 /* If the RHS can be evaluated unconditionally and its operands are
5587 simple, it wins to evaluate the RHS unconditionally on machines
5588 with expensive branches. In this case, this isn't a comparison
5589 that can be merged. Avoid doing this if the RHS is a floating-point
5590 comparison since those can trap. */
5592 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5593 false) >= 2
5594 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5595 && simple_operand_p (rl_arg)
5596 && simple_operand_p (rr_arg))
5598 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5599 if (code == TRUTH_OR_EXPR
5600 && lcode == NE_EXPR && integer_zerop (lr_arg)
5601 && rcode == NE_EXPR && integer_zerop (rr_arg)
5602 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5603 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5604 return build2 (NE_EXPR, truth_type,
5605 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5606 ll_arg, rl_arg),
5607 build_int_cst (TREE_TYPE (ll_arg), 0));
5609 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5610 if (code == TRUTH_AND_EXPR
5611 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5612 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5613 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5614 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5615 return build2 (EQ_EXPR, truth_type,
5616 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5617 ll_arg, rl_arg),
5618 build_int_cst (TREE_TYPE (ll_arg), 0));
5620 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5622 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5623 return build2 (code, truth_type, lhs, rhs);
5624 return NULL_TREE;
5628 /* See if the comparisons can be merged. Then get all the parameters for
5629 each side. */
5631 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5632 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5633 return 0;
5635 volatilep = 0;
5636 ll_inner = decode_field_reference (ll_arg,
5637 &ll_bitsize, &ll_bitpos, &ll_mode,
5638 &ll_unsignedp, &volatilep, &ll_mask,
5639 &ll_and_mask);
5640 lr_inner = decode_field_reference (lr_arg,
5641 &lr_bitsize, &lr_bitpos, &lr_mode,
5642 &lr_unsignedp, &volatilep, &lr_mask,
5643 &lr_and_mask);
5644 rl_inner = decode_field_reference (rl_arg,
5645 &rl_bitsize, &rl_bitpos, &rl_mode,
5646 &rl_unsignedp, &volatilep, &rl_mask,
5647 &rl_and_mask);
5648 rr_inner = decode_field_reference (rr_arg,
5649 &rr_bitsize, &rr_bitpos, &rr_mode,
5650 &rr_unsignedp, &volatilep, &rr_mask,
5651 &rr_and_mask);
5653 /* It must be true that the inner operation on the lhs of each
5654 comparison must be the same if we are to be able to do anything.
5655 Then see if we have constants. If not, the same must be true for
5656 the rhs's. */
5657 if (volatilep || ll_inner == 0 || rl_inner == 0
5658 || ! operand_equal_p (ll_inner, rl_inner, 0))
5659 return 0;
5661 if (TREE_CODE (lr_arg) == INTEGER_CST
5662 && TREE_CODE (rr_arg) == INTEGER_CST)
5663 l_const = lr_arg, r_const = rr_arg;
5664 else if (lr_inner == 0 || rr_inner == 0
5665 || ! operand_equal_p (lr_inner, rr_inner, 0))
5666 return 0;
5667 else
5668 l_const = r_const = 0;
5670 /* If either comparison code is not correct for our logical operation,
5671 fail. However, we can convert a one-bit comparison against zero into
5672 the opposite comparison against that bit being set in the field. */
5674 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5675 if (lcode != wanted_code)
5677 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5679 /* Make the left operand unsigned, since we are only interested
5680 in the value of one bit. Otherwise we are doing the wrong
5681 thing below. */
5682 ll_unsignedp = 1;
5683 l_const = ll_mask;
5685 else
5686 return 0;
5689 /* This is analogous to the code for l_const above. */
5690 if (rcode != wanted_code)
5692 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5694 rl_unsignedp = 1;
5695 r_const = rl_mask;
5697 else
5698 return 0;
5701 /* See if we can find a mode that contains both fields being compared on
5702 the left. If we can't, fail. Otherwise, update all constants and masks
5703 to be relative to a field of that size. */
5704 first_bit = MIN (ll_bitpos, rl_bitpos);
5705 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5706 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5707 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5708 volatilep);
5709 if (lnmode == VOIDmode)
5710 return 0;
5712 lnbitsize = GET_MODE_BITSIZE (lnmode);
5713 lnbitpos = first_bit & ~ (lnbitsize - 1);
5714 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5715 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5717 if (BYTES_BIG_ENDIAN)
5719 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5720 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5723 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5724 size_int (xll_bitpos), 0);
5725 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5726 size_int (xrl_bitpos), 0);
5728 if (l_const)
5730 l_const = fold_convert (lntype, l_const);
5731 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5732 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5733 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5734 fold_build1 (BIT_NOT_EXPR,
5735 lntype, ll_mask),
5736 0)))
5738 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5740 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5743 if (r_const)
5745 r_const = fold_convert (lntype, r_const);
5746 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5747 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5748 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5749 fold_build1 (BIT_NOT_EXPR,
5750 lntype, rl_mask),
5751 0)))
5753 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5755 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5759 /* If the right sides are not constant, do the same for it. Also,
5760 disallow this optimization if a size or signedness mismatch occurs
5761 between the left and right sides. */
5762 if (l_const == 0)
5764 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5765 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5766 /* Make sure the two fields on the right
5767 correspond to the left without being swapped. */
5768 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5769 return 0;
5771 first_bit = MIN (lr_bitpos, rr_bitpos);
5772 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5773 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5774 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5775 volatilep);
5776 if (rnmode == VOIDmode)
5777 return 0;
5779 rnbitsize = GET_MODE_BITSIZE (rnmode);
5780 rnbitpos = first_bit & ~ (rnbitsize - 1);
5781 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5782 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5784 if (BYTES_BIG_ENDIAN)
5786 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5787 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5790 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5791 size_int (xlr_bitpos), 0);
5792 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5793 size_int (xrr_bitpos), 0);
5795 /* Make a mask that corresponds to both fields being compared.
5796 Do this for both items being compared. If the operands are the
5797 same size and the bits being compared are in the same position
5798 then we can do this by masking both and comparing the masked
5799 results. */
5800 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5801 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5802 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5804 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5805 ll_unsignedp || rl_unsignedp);
5806 if (! all_ones_mask_p (ll_mask, lnbitsize))
5807 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5809 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5810 lr_unsignedp || rr_unsignedp);
5811 if (! all_ones_mask_p (lr_mask, rnbitsize))
5812 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5814 return build2 (wanted_code, truth_type, lhs, rhs);
5817 /* There is still another way we can do something: If both pairs of
5818 fields being compared are adjacent, we may be able to make a wider
5819 field containing them both.
5821 Note that we still must mask the lhs/rhs expressions. Furthermore,
5822 the mask must be shifted to account for the shift done by
5823 make_bit_field_ref. */
5824 if ((ll_bitsize + ll_bitpos == rl_bitpos
5825 && lr_bitsize + lr_bitpos == rr_bitpos)
5826 || (ll_bitpos == rl_bitpos + rl_bitsize
5827 && lr_bitpos == rr_bitpos + rr_bitsize))
5829 tree type;
5831 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5832 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5833 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5834 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5836 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5837 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5838 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5839 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5841 /* Convert to the smaller type before masking out unwanted bits. */
5842 type = lntype;
5843 if (lntype != rntype)
5845 if (lnbitsize > rnbitsize)
5847 lhs = fold_convert (rntype, lhs);
5848 ll_mask = fold_convert (rntype, ll_mask);
5849 type = rntype;
5851 else if (lnbitsize < rnbitsize)
5853 rhs = fold_convert (lntype, rhs);
5854 lr_mask = fold_convert (lntype, lr_mask);
5855 type = lntype;
5859 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5860 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5862 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5863 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5865 return build2 (wanted_code, truth_type, lhs, rhs);
5868 return 0;
5871 /* Handle the case of comparisons with constants. If there is something in
5872 common between the masks, those bits of the constants must be the same.
5873 If not, the condition is always false. Test for this to avoid generating
5874 incorrect code below. */
5875 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5876 if (! integer_zerop (result)
5877 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5878 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5880 if (wanted_code == NE_EXPR)
5882 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5883 return constant_boolean_node (true, truth_type);
5885 else
5887 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5888 return constant_boolean_node (false, truth_type);
5892 /* Construct the expression we will return. First get the component
5893 reference we will make. Unless the mask is all ones the width of
5894 that field, perform the mask operation. Then compare with the
5895 merged constant. */
5896 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5897 ll_unsignedp || rl_unsignedp);
5899 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5900 if (! all_ones_mask_p (ll_mask, lnbitsize))
5901 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5903 return build2 (wanted_code, truth_type, result,
5904 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5907 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5908 constant. */
5910 static tree
5911 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5913 tree arg0 = op0;
5914 enum tree_code op_code;
5915 tree comp_const;
5916 tree minmax_const;
5917 int consts_equal, consts_lt;
5918 tree inner;
5920 STRIP_SIGN_NOPS (arg0);
5922 op_code = TREE_CODE (arg0);
5923 minmax_const = TREE_OPERAND (arg0, 1);
5924 comp_const = fold_convert (TREE_TYPE (arg0), op1);
5925 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5926 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5927 inner = TREE_OPERAND (arg0, 0);
5929 /* If something does not permit us to optimize, return the original tree. */
5930 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5931 || TREE_CODE (comp_const) != INTEGER_CST
5932 || TREE_OVERFLOW (comp_const)
5933 || TREE_CODE (minmax_const) != INTEGER_CST
5934 || TREE_OVERFLOW (minmax_const))
5935 return NULL_TREE;
5937 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5938 and GT_EXPR, doing the rest with recursive calls using logical
5939 simplifications. */
5940 switch (code)
5942 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5944 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5945 type, op0, op1);
5946 if (tem)
5947 return invert_truthvalue (tem);
5948 return NULL_TREE;
5951 case GE_EXPR:
5952 return
5953 fold_build2 (TRUTH_ORIF_EXPR, type,
5954 optimize_minmax_comparison
5955 (EQ_EXPR, type, arg0, comp_const),
5956 optimize_minmax_comparison
5957 (GT_EXPR, type, arg0, comp_const));
5959 case EQ_EXPR:
5960 if (op_code == MAX_EXPR && consts_equal)
5961 /* MAX (X, 0) == 0 -> X <= 0 */
5962 return fold_build2 (LE_EXPR, type, inner, comp_const);
5964 else if (op_code == MAX_EXPR && consts_lt)
5965 /* MAX (X, 0) == 5 -> X == 5 */
5966 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5968 else if (op_code == MAX_EXPR)
5969 /* MAX (X, 0) == -1 -> false */
5970 return omit_one_operand (type, integer_zero_node, inner);
5972 else if (consts_equal)
5973 /* MIN (X, 0) == 0 -> X >= 0 */
5974 return fold_build2 (GE_EXPR, type, inner, comp_const);
5976 else if (consts_lt)
5977 /* MIN (X, 0) == 5 -> false */
5978 return omit_one_operand (type, integer_zero_node, inner);
5980 else
5981 /* MIN (X, 0) == -1 -> X == -1 */
5982 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5984 case GT_EXPR:
5985 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5986 /* MAX (X, 0) > 0 -> X > 0
5987 MAX (X, 0) > 5 -> X > 5 */
5988 return fold_build2 (GT_EXPR, type, inner, comp_const);
5990 else if (op_code == MAX_EXPR)
5991 /* MAX (X, 0) > -1 -> true */
5992 return omit_one_operand (type, integer_one_node, inner);
5994 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5995 /* MIN (X, 0) > 0 -> false
5996 MIN (X, 0) > 5 -> false */
5997 return omit_one_operand (type, integer_zero_node, inner);
5999 else
6000 /* MIN (X, 0) > -1 -> X > -1 */
6001 return fold_build2 (GT_EXPR, type, inner, comp_const);
6003 default:
6004 return NULL_TREE;
6008 /* T is an integer expression that is being multiplied, divided, or taken a
6009 modulus (CODE says which and what kind of divide or modulus) by a
6010 constant C. See if we can eliminate that operation by folding it with
6011 other operations already in T. WIDE_TYPE, if non-null, is a type that
6012 should be used for the computation if wider than our type.
6014 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6015 (X * 2) + (Y * 4). We must, however, be assured that either the original
6016 expression would not overflow or that overflow is undefined for the type
6017 in the language in question.
6019 If we return a non-null expression, it is an equivalent form of the
6020 original computation, but need not be in the original type.
6022 We set *STRICT_OVERFLOW_P to true if the return values depends on
6023 signed overflow being undefined. Otherwise we do not change
6024 *STRICT_OVERFLOW_P. */
6026 static tree
6027 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6028 bool *strict_overflow_p)
6030 /* To avoid exponential search depth, refuse to allow recursion past
6031 three levels. Beyond that (1) it's highly unlikely that we'll find
6032 something interesting and (2) we've probably processed it before
6033 when we built the inner expression. */
6035 static int depth;
6036 tree ret;
6038 if (depth > 3)
6039 return NULL;
6041 depth++;
6042 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6043 depth--;
6045 return ret;
6048 static tree
6049 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6050 bool *strict_overflow_p)
6052 tree type = TREE_TYPE (t);
6053 enum tree_code tcode = TREE_CODE (t);
6054 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6055 > GET_MODE_SIZE (TYPE_MODE (type)))
6056 ? wide_type : type);
6057 tree t1, t2;
6058 int same_p = tcode == code;
6059 tree op0 = NULL_TREE, op1 = NULL_TREE;
6060 bool sub_strict_overflow_p;
6062 /* Don't deal with constants of zero here; they confuse the code below. */
6063 if (integer_zerop (c))
6064 return NULL_TREE;
6066 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6067 op0 = TREE_OPERAND (t, 0);
6069 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6070 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6072 /* Note that we need not handle conditional operations here since fold
6073 already handles those cases. So just do arithmetic here. */
6074 switch (tcode)
6076 case INTEGER_CST:
6077 /* For a constant, we can always simplify if we are a multiply
6078 or (for divide and modulus) if it is a multiple of our constant. */
6079 if (code == MULT_EXPR
6080 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6081 return const_binop (code, fold_convert (ctype, t),
6082 fold_convert (ctype, c), 0);
6083 break;
6085 CASE_CONVERT: case NON_LVALUE_EXPR:
6086 /* If op0 is an expression ... */
6087 if ((COMPARISON_CLASS_P (op0)
6088 || UNARY_CLASS_P (op0)
6089 || BINARY_CLASS_P (op0)
6090 || VL_EXP_CLASS_P (op0)
6091 || EXPRESSION_CLASS_P (op0))
6092 /* ... and has wrapping overflow, and its type is smaller
6093 than ctype, then we cannot pass through as widening. */
6094 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6095 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6096 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6097 && (TYPE_PRECISION (ctype)
6098 > TYPE_PRECISION (TREE_TYPE (op0))))
6099 /* ... or this is a truncation (t is narrower than op0),
6100 then we cannot pass through this narrowing. */
6101 || (TYPE_PRECISION (type)
6102 < TYPE_PRECISION (TREE_TYPE (op0)))
6103 /* ... or signedness changes for division or modulus,
6104 then we cannot pass through this conversion. */
6105 || (code != MULT_EXPR
6106 && (TYPE_UNSIGNED (ctype)
6107 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6108 /* ... or has undefined overflow while the converted to
6109 type has not, we cannot do the operation in the inner type
6110 as that would introduce undefined overflow. */
6111 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6112 && !TYPE_OVERFLOW_UNDEFINED (type))))
6113 break;
6115 /* Pass the constant down and see if we can make a simplification. If
6116 we can, replace this expression with the inner simplification for
6117 possible later conversion to our or some other type. */
6118 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6119 && TREE_CODE (t2) == INTEGER_CST
6120 && !TREE_OVERFLOW (t2)
6121 && (0 != (t1 = extract_muldiv (op0, t2, code,
6122 code == MULT_EXPR
6123 ? ctype : NULL_TREE,
6124 strict_overflow_p))))
6125 return t1;
6126 break;
6128 case ABS_EXPR:
6129 /* If widening the type changes it from signed to unsigned, then we
6130 must avoid building ABS_EXPR itself as unsigned. */
6131 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6133 tree cstype = (*signed_type_for) (ctype);
6134 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6135 != 0)
6137 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6138 return fold_convert (ctype, t1);
6140 break;
6142 /* If the constant is negative, we cannot simplify this. */
6143 if (tree_int_cst_sgn (c) == -1)
6144 break;
6145 /* FALLTHROUGH */
6146 case NEGATE_EXPR:
6147 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6148 != 0)
6149 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6150 break;
6152 case MIN_EXPR: case MAX_EXPR:
6153 /* If widening the type changes the signedness, then we can't perform
6154 this optimization as that changes the result. */
6155 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6156 break;
6158 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6159 sub_strict_overflow_p = false;
6160 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6161 &sub_strict_overflow_p)) != 0
6162 && (t2 = extract_muldiv (op1, c, code, wide_type,
6163 &sub_strict_overflow_p)) != 0)
6165 if (tree_int_cst_sgn (c) < 0)
6166 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6167 if (sub_strict_overflow_p)
6168 *strict_overflow_p = true;
6169 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6170 fold_convert (ctype, t2));
6172 break;
6174 case LSHIFT_EXPR: case RSHIFT_EXPR:
6175 /* If the second operand is constant, this is a multiplication
6176 or floor division, by a power of two, so we can treat it that
6177 way unless the multiplier or divisor overflows. Signed
6178 left-shift overflow is implementation-defined rather than
6179 undefined in C90, so do not convert signed left shift into
6180 multiplication. */
6181 if (TREE_CODE (op1) == INTEGER_CST
6182 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6183 /* const_binop may not detect overflow correctly,
6184 so check for it explicitly here. */
6185 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6186 && TREE_INT_CST_HIGH (op1) == 0
6187 && 0 != (t1 = fold_convert (ctype,
6188 const_binop (LSHIFT_EXPR,
6189 size_one_node,
6190 op1, 0)))
6191 && !TREE_OVERFLOW (t1))
6192 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6193 ? MULT_EXPR : FLOOR_DIV_EXPR,
6194 ctype, fold_convert (ctype, op0), t1),
6195 c, code, wide_type, strict_overflow_p);
6196 break;
6198 case PLUS_EXPR: case MINUS_EXPR:
6199 /* See if we can eliminate the operation on both sides. If we can, we
6200 can return a new PLUS or MINUS. If we can't, the only remaining
6201 cases where we can do anything are if the second operand is a
6202 constant. */
6203 sub_strict_overflow_p = false;
6204 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6205 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6206 if (t1 != 0 && t2 != 0
6207 && (code == MULT_EXPR
6208 /* If not multiplication, we can only do this if both operands
6209 are divisible by c. */
6210 || (multiple_of_p (ctype, op0, c)
6211 && multiple_of_p (ctype, op1, c))))
6213 if (sub_strict_overflow_p)
6214 *strict_overflow_p = true;
6215 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6216 fold_convert (ctype, t2));
6219 /* If this was a subtraction, negate OP1 and set it to be an addition.
6220 This simplifies the logic below. */
6221 if (tcode == MINUS_EXPR)
6222 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6224 if (TREE_CODE (op1) != INTEGER_CST)
6225 break;
6227 /* If either OP1 or C are negative, this optimization is not safe for
6228 some of the division and remainder types while for others we need
6229 to change the code. */
6230 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6232 if (code == CEIL_DIV_EXPR)
6233 code = FLOOR_DIV_EXPR;
6234 else if (code == FLOOR_DIV_EXPR)
6235 code = CEIL_DIV_EXPR;
6236 else if (code != MULT_EXPR
6237 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6238 break;
6241 /* If it's a multiply or a division/modulus operation of a multiple
6242 of our constant, do the operation and verify it doesn't overflow. */
6243 if (code == MULT_EXPR
6244 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6246 op1 = const_binop (code, fold_convert (ctype, op1),
6247 fold_convert (ctype, c), 0);
6248 /* We allow the constant to overflow with wrapping semantics. */
6249 if (op1 == 0
6250 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6251 break;
6253 else
6254 break;
6256 /* If we have an unsigned type is not a sizetype, we cannot widen
6257 the operation since it will change the result if the original
6258 computation overflowed. */
6259 if (TYPE_UNSIGNED (ctype)
6260 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6261 && ctype != type)
6262 break;
6264 /* If we were able to eliminate our operation from the first side,
6265 apply our operation to the second side and reform the PLUS. */
6266 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6267 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6269 /* The last case is if we are a multiply. In that case, we can
6270 apply the distributive law to commute the multiply and addition
6271 if the multiplication of the constants doesn't overflow. */
6272 if (code == MULT_EXPR)
6273 return fold_build2 (tcode, ctype,
6274 fold_build2 (code, ctype,
6275 fold_convert (ctype, op0),
6276 fold_convert (ctype, c)),
6277 op1);
6279 break;
6281 case MULT_EXPR:
6282 /* We have a special case here if we are doing something like
6283 (C * 8) % 4 since we know that's zero. */
6284 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6285 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6286 /* If the multiplication can overflow we cannot optimize this.
6287 ??? Until we can properly mark individual operations as
6288 not overflowing we need to treat sizetype special here as
6289 stor-layout relies on this opimization to make
6290 DECL_FIELD_BIT_OFFSET always a constant. */
6291 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6292 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6293 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6294 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6295 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6297 *strict_overflow_p = true;
6298 return omit_one_operand (type, integer_zero_node, op0);
6301 /* ... fall through ... */
6303 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6304 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6305 /* If we can extract our operation from the LHS, do so and return a
6306 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6307 do something only if the second operand is a constant. */
6308 if (same_p
6309 && (t1 = extract_muldiv (op0, c, code, wide_type,
6310 strict_overflow_p)) != 0)
6311 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6312 fold_convert (ctype, op1));
6313 else if (tcode == MULT_EXPR && code == MULT_EXPR
6314 && (t1 = extract_muldiv (op1, c, code, wide_type,
6315 strict_overflow_p)) != 0)
6316 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6317 fold_convert (ctype, t1));
6318 else if (TREE_CODE (op1) != INTEGER_CST)
6319 return 0;
6321 /* If these are the same operation types, we can associate them
6322 assuming no overflow. */
6323 if (tcode == code
6324 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6325 fold_convert (ctype, c), 1))
6326 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6327 TREE_INT_CST_HIGH (t1),
6328 (TYPE_UNSIGNED (ctype)
6329 && tcode != MULT_EXPR) ? -1 : 1,
6330 TREE_OVERFLOW (t1)))
6331 && !TREE_OVERFLOW (t1))
6332 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6334 /* If these operations "cancel" each other, we have the main
6335 optimizations of this pass, which occur when either constant is a
6336 multiple of the other, in which case we replace this with either an
6337 operation or CODE or TCODE.
6339 If we have an unsigned type that is not a sizetype, we cannot do
6340 this since it will change the result if the original computation
6341 overflowed. */
6342 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6343 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6344 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6345 || (tcode == MULT_EXPR
6346 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6347 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6348 && code != MULT_EXPR)))
6350 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6352 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6353 *strict_overflow_p = true;
6354 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6355 fold_convert (ctype,
6356 const_binop (TRUNC_DIV_EXPR,
6357 op1, c, 0)));
6359 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6361 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6362 *strict_overflow_p = true;
6363 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6364 fold_convert (ctype,
6365 const_binop (TRUNC_DIV_EXPR,
6366 c, op1, 0)));
6369 break;
6371 default:
6372 break;
6375 return 0;
6378 /* Return a node which has the indicated constant VALUE (either 0 or
6379 1), and is of the indicated TYPE. */
6381 tree
6382 constant_boolean_node (int value, tree type)
6384 if (type == integer_type_node)
6385 return value ? integer_one_node : integer_zero_node;
6386 else if (type == boolean_type_node)
6387 return value ? boolean_true_node : boolean_false_node;
6388 else
6389 return build_int_cst (type, value);
6393 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6394 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6395 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6396 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6397 COND is the first argument to CODE; otherwise (as in the example
6398 given here), it is the second argument. TYPE is the type of the
6399 original expression. Return NULL_TREE if no simplification is
6400 possible. */
6402 static tree
6403 fold_binary_op_with_conditional_arg (enum tree_code code,
6404 tree type, tree op0, tree op1,
6405 tree cond, tree arg, int cond_first_p)
6407 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6408 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6409 tree test, true_value, false_value;
6410 tree lhs = NULL_TREE;
6411 tree rhs = NULL_TREE;
6413 /* This transformation is only worthwhile if we don't have to wrap
6414 arg in a SAVE_EXPR, and the operation can be simplified on at least
6415 one of the branches once its pushed inside the COND_EXPR. */
6416 if (!TREE_CONSTANT (arg))
6417 return NULL_TREE;
6419 if (TREE_CODE (cond) == COND_EXPR)
6421 test = TREE_OPERAND (cond, 0);
6422 true_value = TREE_OPERAND (cond, 1);
6423 false_value = TREE_OPERAND (cond, 2);
6424 /* If this operand throws an expression, then it does not make
6425 sense to try to perform a logical or arithmetic operation
6426 involving it. */
6427 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6428 lhs = true_value;
6429 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6430 rhs = false_value;
6432 else
6434 tree testtype = TREE_TYPE (cond);
6435 test = cond;
6436 true_value = constant_boolean_node (true, testtype);
6437 false_value = constant_boolean_node (false, testtype);
6440 arg = fold_convert (arg_type, arg);
6441 if (lhs == 0)
6443 true_value = fold_convert (cond_type, true_value);
6444 if (cond_first_p)
6445 lhs = fold_build2 (code, type, true_value, arg);
6446 else
6447 lhs = fold_build2 (code, type, arg, true_value);
6449 if (rhs == 0)
6451 false_value = fold_convert (cond_type, false_value);
6452 if (cond_first_p)
6453 rhs = fold_build2 (code, type, false_value, arg);
6454 else
6455 rhs = fold_build2 (code, type, arg, false_value);
6458 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6459 return fold_convert (type, test);
6463 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6465 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6466 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6467 ADDEND is the same as X.
6469 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6470 and finite. The problematic cases are when X is zero, and its mode
6471 has signed zeros. In the case of rounding towards -infinity,
6472 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6473 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6475 bool
6476 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6478 if (!real_zerop (addend))
6479 return false;
6481 /* Don't allow the fold with -fsignaling-nans. */
6482 if (HONOR_SNANS (TYPE_MODE (type)))
6483 return false;
6485 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6486 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6487 return true;
6489 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6490 if (TREE_CODE (addend) == REAL_CST
6491 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6492 negate = !negate;
6494 /* The mode has signed zeros, and we have to honor their sign.
6495 In this situation, there is only one case we can return true for.
6496 X - 0 is the same as X unless rounding towards -infinity is
6497 supported. */
6498 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6501 /* Subroutine of fold() that checks comparisons of built-in math
6502 functions against real constants.
6504 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6505 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6506 is the type of the result and ARG0 and ARG1 are the operands of the
6507 comparison. ARG1 must be a TREE_REAL_CST.
6509 The function returns the constant folded tree if a simplification
6510 can be made, and NULL_TREE otherwise. */
6512 static tree
6513 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6514 tree type, tree arg0, tree arg1)
6516 REAL_VALUE_TYPE c;
6518 if (BUILTIN_SQRT_P (fcode))
6520 tree arg = CALL_EXPR_ARG (arg0, 0);
6521 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6523 c = TREE_REAL_CST (arg1);
6524 if (REAL_VALUE_NEGATIVE (c))
6526 /* sqrt(x) < y is always false, if y is negative. */
6527 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6528 return omit_one_operand (type, integer_zero_node, arg);
6530 /* sqrt(x) > y is always true, if y is negative and we
6531 don't care about NaNs, i.e. negative values of x. */
6532 if (code == NE_EXPR || !HONOR_NANS (mode))
6533 return omit_one_operand (type, integer_one_node, arg);
6535 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6536 return fold_build2 (GE_EXPR, type, arg,
6537 build_real (TREE_TYPE (arg), dconst0));
6539 else if (code == GT_EXPR || code == GE_EXPR)
6541 REAL_VALUE_TYPE c2;
6543 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6544 real_convert (&c2, mode, &c2);
6546 if (REAL_VALUE_ISINF (c2))
6548 /* sqrt(x) > y is x == +Inf, when y is very large. */
6549 if (HONOR_INFINITIES (mode))
6550 return fold_build2 (EQ_EXPR, type, arg,
6551 build_real (TREE_TYPE (arg), c2));
6553 /* sqrt(x) > y is always false, when y is very large
6554 and we don't care about infinities. */
6555 return omit_one_operand (type, integer_zero_node, arg);
6558 /* sqrt(x) > c is the same as x > c*c. */
6559 return fold_build2 (code, type, arg,
6560 build_real (TREE_TYPE (arg), c2));
6562 else if (code == LT_EXPR || code == LE_EXPR)
6564 REAL_VALUE_TYPE c2;
6566 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6567 real_convert (&c2, mode, &c2);
6569 if (REAL_VALUE_ISINF (c2))
6571 /* sqrt(x) < y is always true, when y is a very large
6572 value and we don't care about NaNs or Infinities. */
6573 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6574 return omit_one_operand (type, integer_one_node, arg);
6576 /* sqrt(x) < y is x != +Inf when y is very large and we
6577 don't care about NaNs. */
6578 if (! HONOR_NANS (mode))
6579 return fold_build2 (NE_EXPR, type, arg,
6580 build_real (TREE_TYPE (arg), c2));
6582 /* sqrt(x) < y is x >= 0 when y is very large and we
6583 don't care about Infinities. */
6584 if (! HONOR_INFINITIES (mode))
6585 return fold_build2 (GE_EXPR, type, arg,
6586 build_real (TREE_TYPE (arg), dconst0));
6588 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6589 if (lang_hooks.decls.global_bindings_p () != 0
6590 || CONTAINS_PLACEHOLDER_P (arg))
6591 return NULL_TREE;
6593 arg = save_expr (arg);
6594 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6595 fold_build2 (GE_EXPR, type, arg,
6596 build_real (TREE_TYPE (arg),
6597 dconst0)),
6598 fold_build2 (NE_EXPR, type, arg,
6599 build_real (TREE_TYPE (arg),
6600 c2)));
6603 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6604 if (! HONOR_NANS (mode))
6605 return fold_build2 (code, type, arg,
6606 build_real (TREE_TYPE (arg), c2));
6608 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6609 if (lang_hooks.decls.global_bindings_p () == 0
6610 && ! CONTAINS_PLACEHOLDER_P (arg))
6612 arg = save_expr (arg);
6613 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6614 fold_build2 (GE_EXPR, type, arg,
6615 build_real (TREE_TYPE (arg),
6616 dconst0)),
6617 fold_build2 (code, type, arg,
6618 build_real (TREE_TYPE (arg),
6619 c2)));
6624 return NULL_TREE;
6627 /* Subroutine of fold() that optimizes comparisons against Infinities,
6628 either +Inf or -Inf.
6630 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6631 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6632 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6634 The function returns the constant folded tree if a simplification
6635 can be made, and NULL_TREE otherwise. */
6637 static tree
6638 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6640 enum machine_mode mode;
6641 REAL_VALUE_TYPE max;
6642 tree temp;
6643 bool neg;
6645 mode = TYPE_MODE (TREE_TYPE (arg0));
6647 /* For negative infinity swap the sense of the comparison. */
6648 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6649 if (neg)
6650 code = swap_tree_comparison (code);
6652 switch (code)
6654 case GT_EXPR:
6655 /* x > +Inf is always false, if with ignore sNANs. */
6656 if (HONOR_SNANS (mode))
6657 return NULL_TREE;
6658 return omit_one_operand (type, integer_zero_node, arg0);
6660 case LE_EXPR:
6661 /* x <= +Inf is always true, if we don't case about NaNs. */
6662 if (! HONOR_NANS (mode))
6663 return omit_one_operand (type, integer_one_node, arg0);
6665 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6666 if (lang_hooks.decls.global_bindings_p () == 0
6667 && ! CONTAINS_PLACEHOLDER_P (arg0))
6669 arg0 = save_expr (arg0);
6670 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6672 break;
6674 case EQ_EXPR:
6675 case GE_EXPR:
6676 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6677 real_maxval (&max, neg, mode);
6678 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6679 arg0, build_real (TREE_TYPE (arg0), max));
6681 case LT_EXPR:
6682 /* x < +Inf is always equal to x <= DBL_MAX. */
6683 real_maxval (&max, neg, mode);
6684 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6685 arg0, build_real (TREE_TYPE (arg0), max));
6687 case NE_EXPR:
6688 /* x != +Inf is always equal to !(x > DBL_MAX). */
6689 real_maxval (&max, neg, mode);
6690 if (! HONOR_NANS (mode))
6691 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6692 arg0, build_real (TREE_TYPE (arg0), max));
6694 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6695 arg0, build_real (TREE_TYPE (arg0), max));
6696 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6698 default:
6699 break;
6702 return NULL_TREE;
6705 /* Subroutine of fold() that optimizes comparisons of a division by
6706 a nonzero integer constant against an integer constant, i.e.
6707 X/C1 op C2.
6709 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6710 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6711 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6713 The function returns the constant folded tree if a simplification
6714 can be made, and NULL_TREE otherwise. */
6716 static tree
6717 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6719 tree prod, tmp, hi, lo;
6720 tree arg00 = TREE_OPERAND (arg0, 0);
6721 tree arg01 = TREE_OPERAND (arg0, 1);
6722 unsigned HOST_WIDE_INT lpart;
6723 HOST_WIDE_INT hpart;
6724 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6725 bool neg_overflow;
6726 int overflow;
6728 /* We have to do this the hard way to detect unsigned overflow.
6729 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6730 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6731 TREE_INT_CST_HIGH (arg01),
6732 TREE_INT_CST_LOW (arg1),
6733 TREE_INT_CST_HIGH (arg1),
6734 &lpart, &hpart, unsigned_p);
6735 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6736 -1, overflow);
6737 neg_overflow = false;
6739 if (unsigned_p)
6741 tmp = int_const_binop (MINUS_EXPR, arg01,
6742 build_int_cst (TREE_TYPE (arg01), 1), 0);
6743 lo = prod;
6745 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6746 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6747 TREE_INT_CST_HIGH (prod),
6748 TREE_INT_CST_LOW (tmp),
6749 TREE_INT_CST_HIGH (tmp),
6750 &lpart, &hpart, unsigned_p);
6751 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6752 -1, overflow | TREE_OVERFLOW (prod));
6754 else if (tree_int_cst_sgn (arg01) >= 0)
6756 tmp = int_const_binop (MINUS_EXPR, arg01,
6757 build_int_cst (TREE_TYPE (arg01), 1), 0);
6758 switch (tree_int_cst_sgn (arg1))
6760 case -1:
6761 neg_overflow = true;
6762 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6763 hi = prod;
6764 break;
6766 case 0:
6767 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6768 hi = tmp;
6769 break;
6771 case 1:
6772 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6773 lo = prod;
6774 break;
6776 default:
6777 gcc_unreachable ();
6780 else
6782 /* A negative divisor reverses the relational operators. */
6783 code = swap_tree_comparison (code);
6785 tmp = int_const_binop (PLUS_EXPR, arg01,
6786 build_int_cst (TREE_TYPE (arg01), 1), 0);
6787 switch (tree_int_cst_sgn (arg1))
6789 case -1:
6790 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6791 lo = prod;
6792 break;
6794 case 0:
6795 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6796 lo = tmp;
6797 break;
6799 case 1:
6800 neg_overflow = true;
6801 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6802 hi = prod;
6803 break;
6805 default:
6806 gcc_unreachable ();
6810 switch (code)
6812 case EQ_EXPR:
6813 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6814 return omit_one_operand (type, integer_zero_node, arg00);
6815 if (TREE_OVERFLOW (hi))
6816 return fold_build2 (GE_EXPR, type, arg00, lo);
6817 if (TREE_OVERFLOW (lo))
6818 return fold_build2 (LE_EXPR, type, arg00, hi);
6819 return build_range_check (type, arg00, 1, lo, hi);
6821 case NE_EXPR:
6822 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6823 return omit_one_operand (type, integer_one_node, arg00);
6824 if (TREE_OVERFLOW (hi))
6825 return fold_build2 (LT_EXPR, type, arg00, lo);
6826 if (TREE_OVERFLOW (lo))
6827 return fold_build2 (GT_EXPR, type, arg00, hi);
6828 return build_range_check (type, arg00, 0, lo, hi);
6830 case LT_EXPR:
6831 if (TREE_OVERFLOW (lo))
6833 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6834 return omit_one_operand (type, tmp, arg00);
6836 return fold_build2 (LT_EXPR, type, arg00, lo);
6838 case LE_EXPR:
6839 if (TREE_OVERFLOW (hi))
6841 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6842 return omit_one_operand (type, tmp, arg00);
6844 return fold_build2 (LE_EXPR, type, arg00, hi);
6846 case GT_EXPR:
6847 if (TREE_OVERFLOW (hi))
6849 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6850 return omit_one_operand (type, tmp, arg00);
6852 return fold_build2 (GT_EXPR, type, arg00, hi);
6854 case GE_EXPR:
6855 if (TREE_OVERFLOW (lo))
6857 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6858 return omit_one_operand (type, tmp, arg00);
6860 return fold_build2 (GE_EXPR, type, arg00, lo);
6862 default:
6863 break;
6866 return NULL_TREE;
6870 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6871 equality/inequality test, then return a simplified form of the test
6872 using a sign testing. Otherwise return NULL. TYPE is the desired
6873 result type. */
6875 static tree
6876 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6877 tree result_type)
6879 /* If this is testing a single bit, we can optimize the test. */
6880 if ((code == NE_EXPR || code == EQ_EXPR)
6881 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6882 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6884 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6885 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6886 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6888 if (arg00 != NULL_TREE
6889 /* This is only a win if casting to a signed type is cheap,
6890 i.e. when arg00's type is not a partial mode. */
6891 && TYPE_PRECISION (TREE_TYPE (arg00))
6892 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6894 tree stype = signed_type_for (TREE_TYPE (arg00));
6895 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6896 result_type, fold_convert (stype, arg00),
6897 build_int_cst (stype, 0));
6901 return NULL_TREE;
6904 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6905 equality/inequality test, then return a simplified form of
6906 the test using shifts and logical operations. Otherwise return
6907 NULL. TYPE is the desired result type. */
6909 tree
6910 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6911 tree result_type)
6913 /* If this is testing a single bit, we can optimize the test. */
6914 if ((code == NE_EXPR || code == EQ_EXPR)
6915 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6916 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6918 tree inner = TREE_OPERAND (arg0, 0);
6919 tree type = TREE_TYPE (arg0);
6920 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6921 enum machine_mode operand_mode = TYPE_MODE (type);
6922 int ops_unsigned;
6923 tree signed_type, unsigned_type, intermediate_type;
6924 tree tem, one;
6926 /* First, see if we can fold the single bit test into a sign-bit
6927 test. */
6928 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6929 result_type);
6930 if (tem)
6931 return tem;
6933 /* Otherwise we have (A & C) != 0 where C is a single bit,
6934 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6935 Similarly for (A & C) == 0. */
6937 /* If INNER is a right shift of a constant and it plus BITNUM does
6938 not overflow, adjust BITNUM and INNER. */
6939 if (TREE_CODE (inner) == RSHIFT_EXPR
6940 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6941 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6942 && bitnum < TYPE_PRECISION (type)
6943 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6944 bitnum - TYPE_PRECISION (type)))
6946 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6947 inner = TREE_OPERAND (inner, 0);
6950 /* If we are going to be able to omit the AND below, we must do our
6951 operations as unsigned. If we must use the AND, we have a choice.
6952 Normally unsigned is faster, but for some machines signed is. */
6953 #ifdef LOAD_EXTEND_OP
6954 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6955 && !flag_syntax_only) ? 0 : 1;
6956 #else
6957 ops_unsigned = 1;
6958 #endif
6960 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6961 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6962 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6963 inner = fold_convert (intermediate_type, inner);
6965 if (bitnum != 0)
6966 inner = build2 (RSHIFT_EXPR, intermediate_type,
6967 inner, size_int (bitnum));
6969 one = build_int_cst (intermediate_type, 1);
6971 if (code == EQ_EXPR)
6972 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6974 /* Put the AND last so it can combine with more things. */
6975 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6977 /* Make sure to return the proper type. */
6978 inner = fold_convert (result_type, inner);
6980 return inner;
6982 return NULL_TREE;
6985 /* Check whether we are allowed to reorder operands arg0 and arg1,
6986 such that the evaluation of arg1 occurs before arg0. */
6988 static bool
6989 reorder_operands_p (const_tree arg0, const_tree arg1)
6991 if (! flag_evaluation_order)
6992 return true;
6993 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6994 return true;
6995 return ! TREE_SIDE_EFFECTS (arg0)
6996 && ! TREE_SIDE_EFFECTS (arg1);
6999 /* Test whether it is preferable two swap two operands, ARG0 and
7000 ARG1, for example because ARG0 is an integer constant and ARG1
7001 isn't. If REORDER is true, only recommend swapping if we can
7002 evaluate the operands in reverse order. */
7004 bool
7005 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7007 STRIP_SIGN_NOPS (arg0);
7008 STRIP_SIGN_NOPS (arg1);
7010 if (TREE_CODE (arg1) == INTEGER_CST)
7011 return 0;
7012 if (TREE_CODE (arg0) == INTEGER_CST)
7013 return 1;
7015 if (TREE_CODE (arg1) == REAL_CST)
7016 return 0;
7017 if (TREE_CODE (arg0) == REAL_CST)
7018 return 1;
7020 if (TREE_CODE (arg1) == FIXED_CST)
7021 return 0;
7022 if (TREE_CODE (arg0) == FIXED_CST)
7023 return 1;
7025 if (TREE_CODE (arg1) == COMPLEX_CST)
7026 return 0;
7027 if (TREE_CODE (arg0) == COMPLEX_CST)
7028 return 1;
7030 if (TREE_CONSTANT (arg1))
7031 return 0;
7032 if (TREE_CONSTANT (arg0))
7033 return 1;
7035 if (optimize_function_for_size_p (cfun))
7036 return 0;
7038 if (reorder && flag_evaluation_order
7039 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7040 return 0;
7042 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7043 for commutative and comparison operators. Ensuring a canonical
7044 form allows the optimizers to find additional redundancies without
7045 having to explicitly check for both orderings. */
7046 if (TREE_CODE (arg0) == SSA_NAME
7047 && TREE_CODE (arg1) == SSA_NAME
7048 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7049 return 1;
7051 /* Put SSA_NAMEs last. */
7052 if (TREE_CODE (arg1) == SSA_NAME)
7053 return 0;
7054 if (TREE_CODE (arg0) == SSA_NAME)
7055 return 1;
7057 /* Put variables last. */
7058 if (DECL_P (arg1))
7059 return 0;
7060 if (DECL_P (arg0))
7061 return 1;
7063 return 0;
7066 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7067 ARG0 is extended to a wider type. */
7069 static tree
7070 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7072 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7073 tree arg1_unw;
7074 tree shorter_type, outer_type;
7075 tree min, max;
7076 bool above, below;
7078 if (arg0_unw == arg0)
7079 return NULL_TREE;
7080 shorter_type = TREE_TYPE (arg0_unw);
7082 #ifdef HAVE_canonicalize_funcptr_for_compare
7083 /* Disable this optimization if we're casting a function pointer
7084 type on targets that require function pointer canonicalization. */
7085 if (HAVE_canonicalize_funcptr_for_compare
7086 && TREE_CODE (shorter_type) == POINTER_TYPE
7087 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7088 return NULL_TREE;
7089 #endif
7091 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7092 return NULL_TREE;
7094 arg1_unw = get_unwidened (arg1, NULL_TREE);
7096 /* If possible, express the comparison in the shorter mode. */
7097 if ((code == EQ_EXPR || code == NE_EXPR
7098 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7099 && (TREE_TYPE (arg1_unw) == shorter_type
7100 || ((TYPE_PRECISION (shorter_type)
7101 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7102 && (TYPE_UNSIGNED (shorter_type)
7103 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7104 || (TREE_CODE (arg1_unw) == INTEGER_CST
7105 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7106 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7107 && int_fits_type_p (arg1_unw, shorter_type))))
7108 return fold_build2 (code, type, arg0_unw,
7109 fold_convert (shorter_type, arg1_unw));
7111 if (TREE_CODE (arg1_unw) != INTEGER_CST
7112 || TREE_CODE (shorter_type) != INTEGER_TYPE
7113 || !int_fits_type_p (arg1_unw, shorter_type))
7114 return NULL_TREE;
7116 /* If we are comparing with the integer that does not fit into the range
7117 of the shorter type, the result is known. */
7118 outer_type = TREE_TYPE (arg1_unw);
7119 min = lower_bound_in_type (outer_type, shorter_type);
7120 max = upper_bound_in_type (outer_type, shorter_type);
7122 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7123 max, arg1_unw));
7124 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7125 arg1_unw, min));
7127 switch (code)
7129 case EQ_EXPR:
7130 if (above || below)
7131 return omit_one_operand (type, integer_zero_node, arg0);
7132 break;
7134 case NE_EXPR:
7135 if (above || below)
7136 return omit_one_operand (type, integer_one_node, arg0);
7137 break;
7139 case LT_EXPR:
7140 case LE_EXPR:
7141 if (above)
7142 return omit_one_operand (type, integer_one_node, arg0);
7143 else if (below)
7144 return omit_one_operand (type, integer_zero_node, arg0);
7146 case GT_EXPR:
7147 case GE_EXPR:
7148 if (above)
7149 return omit_one_operand (type, integer_zero_node, arg0);
7150 else if (below)
7151 return omit_one_operand (type, integer_one_node, arg0);
7153 default:
7154 break;
7157 return NULL_TREE;
7160 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7161 ARG0 just the signedness is changed. */
7163 static tree
7164 fold_sign_changed_comparison (enum tree_code code, tree type,
7165 tree arg0, tree arg1)
7167 tree arg0_inner;
7168 tree inner_type, outer_type;
7170 if (!CONVERT_EXPR_P (arg0))
7171 return NULL_TREE;
7173 outer_type = TREE_TYPE (arg0);
7174 arg0_inner = TREE_OPERAND (arg0, 0);
7175 inner_type = TREE_TYPE (arg0_inner);
7177 #ifdef HAVE_canonicalize_funcptr_for_compare
7178 /* Disable this optimization if we're casting a function pointer
7179 type on targets that require function pointer canonicalization. */
7180 if (HAVE_canonicalize_funcptr_for_compare
7181 && TREE_CODE (inner_type) == POINTER_TYPE
7182 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7183 return NULL_TREE;
7184 #endif
7186 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7187 return NULL_TREE;
7189 /* If the conversion is from an integral subtype to its basetype
7190 leave it alone. */
7191 if (TREE_TYPE (inner_type) == outer_type)
7192 return NULL_TREE;
7194 if (TREE_CODE (arg1) != INTEGER_CST
7195 && !(CONVERT_EXPR_P (arg1)
7196 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7197 return NULL_TREE;
7199 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7200 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7201 && code != NE_EXPR
7202 && code != EQ_EXPR)
7203 return NULL_TREE;
7205 if (TREE_CODE (arg1) == INTEGER_CST)
7206 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7207 TREE_INT_CST_HIGH (arg1), 0,
7208 TREE_OVERFLOW (arg1));
7209 else
7210 arg1 = fold_convert (inner_type, arg1);
7212 return fold_build2 (code, type, arg0_inner, arg1);
7215 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7216 step of the array. Reconstructs s and delta in the case of s * delta
7217 being an integer constant (and thus already folded).
7218 ADDR is the address. MULT is the multiplicative expression.
7219 If the function succeeds, the new address expression is returned. Otherwise
7220 NULL_TREE is returned. */
7222 static tree
7223 try_move_mult_to_index (tree addr, tree op1)
7225 tree s, delta, step;
7226 tree ref = TREE_OPERAND (addr, 0), pref;
7227 tree ret, pos;
7228 tree itype;
7229 bool mdim = false;
7231 /* Strip the nops that might be added when converting op1 to sizetype. */
7232 STRIP_NOPS (op1);
7234 /* Canonicalize op1 into a possibly non-constant delta
7235 and an INTEGER_CST s. */
7236 if (TREE_CODE (op1) == MULT_EXPR)
7238 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7240 STRIP_NOPS (arg0);
7241 STRIP_NOPS (arg1);
7243 if (TREE_CODE (arg0) == INTEGER_CST)
7245 s = arg0;
7246 delta = arg1;
7248 else if (TREE_CODE (arg1) == INTEGER_CST)
7250 s = arg1;
7251 delta = arg0;
7253 else
7254 return NULL_TREE;
7256 else if (TREE_CODE (op1) == INTEGER_CST)
7258 delta = op1;
7259 s = NULL_TREE;
7261 else
7263 /* Simulate we are delta * 1. */
7264 delta = op1;
7265 s = integer_one_node;
7268 for (;; ref = TREE_OPERAND (ref, 0))
7270 if (TREE_CODE (ref) == ARRAY_REF)
7272 /* Remember if this was a multi-dimensional array. */
7273 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7274 mdim = true;
7276 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7277 if (! itype)
7278 continue;
7280 step = array_ref_element_size (ref);
7281 if (TREE_CODE (step) != INTEGER_CST)
7282 continue;
7284 if (s)
7286 if (! tree_int_cst_equal (step, s))
7287 continue;
7289 else
7291 /* Try if delta is a multiple of step. */
7292 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7293 if (! tmp)
7294 continue;
7295 delta = tmp;
7298 /* Only fold here if we can verify we do not overflow one
7299 dimension of a multi-dimensional array. */
7300 if (mdim)
7302 tree tmp;
7304 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7305 || !INTEGRAL_TYPE_P (itype)
7306 || !TYPE_MAX_VALUE (itype)
7307 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7308 continue;
7310 tmp = fold_binary (PLUS_EXPR, itype,
7311 fold_convert (itype,
7312 TREE_OPERAND (ref, 1)),
7313 fold_convert (itype, delta));
7314 if (!tmp
7315 || TREE_CODE (tmp) != INTEGER_CST
7316 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7317 continue;
7320 break;
7322 else
7323 mdim = false;
7325 if (!handled_component_p (ref))
7326 return NULL_TREE;
7329 /* We found the suitable array reference. So copy everything up to it,
7330 and replace the index. */
7332 pref = TREE_OPERAND (addr, 0);
7333 ret = copy_node (pref);
7334 pos = ret;
7336 while (pref != ref)
7338 pref = TREE_OPERAND (pref, 0);
7339 TREE_OPERAND (pos, 0) = copy_node (pref);
7340 pos = TREE_OPERAND (pos, 0);
7343 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7344 fold_convert (itype,
7345 TREE_OPERAND (pos, 1)),
7346 fold_convert (itype, delta));
7348 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7352 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7353 means A >= Y && A != MAX, but in this case we know that
7354 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7356 static tree
7357 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7359 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7361 if (TREE_CODE (bound) == LT_EXPR)
7362 a = TREE_OPERAND (bound, 0);
7363 else if (TREE_CODE (bound) == GT_EXPR)
7364 a = TREE_OPERAND (bound, 1);
7365 else
7366 return NULL_TREE;
7368 typea = TREE_TYPE (a);
7369 if (!INTEGRAL_TYPE_P (typea)
7370 && !POINTER_TYPE_P (typea))
7371 return NULL_TREE;
7373 if (TREE_CODE (ineq) == LT_EXPR)
7375 a1 = TREE_OPERAND (ineq, 1);
7376 y = TREE_OPERAND (ineq, 0);
7378 else if (TREE_CODE (ineq) == GT_EXPR)
7380 a1 = TREE_OPERAND (ineq, 0);
7381 y = TREE_OPERAND (ineq, 1);
7383 else
7384 return NULL_TREE;
7386 if (TREE_TYPE (a1) != typea)
7387 return NULL_TREE;
7389 if (POINTER_TYPE_P (typea))
7391 /* Convert the pointer types into integer before taking the difference. */
7392 tree ta = fold_convert (ssizetype, a);
7393 tree ta1 = fold_convert (ssizetype, a1);
7394 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7396 else
7397 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7399 if (!diff || !integer_onep (diff))
7400 return NULL_TREE;
7402 return fold_build2 (GE_EXPR, type, a, y);
7405 /* Fold a sum or difference of at least one multiplication.
7406 Returns the folded tree or NULL if no simplification could be made. */
7408 static tree
7409 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7411 tree arg00, arg01, arg10, arg11;
7412 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7414 /* (A * C) +- (B * C) -> (A+-B) * C.
7415 (A * C) +- A -> A * (C+-1).
7416 We are most concerned about the case where C is a constant,
7417 but other combinations show up during loop reduction. Since
7418 it is not difficult, try all four possibilities. */
7420 if (TREE_CODE (arg0) == MULT_EXPR)
7422 arg00 = TREE_OPERAND (arg0, 0);
7423 arg01 = TREE_OPERAND (arg0, 1);
7425 else if (TREE_CODE (arg0) == INTEGER_CST)
7427 arg00 = build_one_cst (type);
7428 arg01 = arg0;
7430 else
7432 /* We cannot generate constant 1 for fract. */
7433 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7434 return NULL_TREE;
7435 arg00 = arg0;
7436 arg01 = build_one_cst (type);
7438 if (TREE_CODE (arg1) == MULT_EXPR)
7440 arg10 = TREE_OPERAND (arg1, 0);
7441 arg11 = TREE_OPERAND (arg1, 1);
7443 else if (TREE_CODE (arg1) == INTEGER_CST)
7445 arg10 = build_one_cst (type);
7446 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7447 the purpose of this canonicalization. */
7448 if (TREE_INT_CST_HIGH (arg1) == -1
7449 && negate_expr_p (arg1)
7450 && code == PLUS_EXPR)
7452 arg11 = negate_expr (arg1);
7453 code = MINUS_EXPR;
7455 else
7456 arg11 = arg1;
7458 else
7460 /* We cannot generate constant 1 for fract. */
7461 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7462 return NULL_TREE;
7463 arg10 = arg1;
7464 arg11 = build_one_cst (type);
7466 same = NULL_TREE;
7468 if (operand_equal_p (arg01, arg11, 0))
7469 same = arg01, alt0 = arg00, alt1 = arg10;
7470 else if (operand_equal_p (arg00, arg10, 0))
7471 same = arg00, alt0 = arg01, alt1 = arg11;
7472 else if (operand_equal_p (arg00, arg11, 0))
7473 same = arg00, alt0 = arg01, alt1 = arg10;
7474 else if (operand_equal_p (arg01, arg10, 0))
7475 same = arg01, alt0 = arg00, alt1 = arg11;
7477 /* No identical multiplicands; see if we can find a common
7478 power-of-two factor in non-power-of-two multiplies. This
7479 can help in multi-dimensional array access. */
7480 else if (host_integerp (arg01, 0)
7481 && host_integerp (arg11, 0))
7483 HOST_WIDE_INT int01, int11, tmp;
7484 bool swap = false;
7485 tree maybe_same;
7486 int01 = TREE_INT_CST_LOW (arg01);
7487 int11 = TREE_INT_CST_LOW (arg11);
7489 /* Move min of absolute values to int11. */
7490 if ((int01 >= 0 ? int01 : -int01)
7491 < (int11 >= 0 ? int11 : -int11))
7493 tmp = int01, int01 = int11, int11 = tmp;
7494 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7495 maybe_same = arg01;
7496 swap = true;
7498 else
7499 maybe_same = arg11;
7501 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7503 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7504 build_int_cst (TREE_TYPE (arg00),
7505 int01 / int11));
7506 alt1 = arg10;
7507 same = maybe_same;
7508 if (swap)
7509 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7513 if (same)
7514 return fold_build2 (MULT_EXPR, type,
7515 fold_build2 (code, type,
7516 fold_convert (type, alt0),
7517 fold_convert (type, alt1)),
7518 fold_convert (type, same));
7520 return NULL_TREE;
7523 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7524 specified by EXPR into the buffer PTR of length LEN bytes.
7525 Return the number of bytes placed in the buffer, or zero
7526 upon failure. */
7528 static int
7529 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7531 tree type = TREE_TYPE (expr);
7532 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7533 int byte, offset, word, words;
7534 unsigned char value;
7536 if (total_bytes > len)
7537 return 0;
7538 words = total_bytes / UNITS_PER_WORD;
7540 for (byte = 0; byte < total_bytes; byte++)
7542 int bitpos = byte * BITS_PER_UNIT;
7543 if (bitpos < HOST_BITS_PER_WIDE_INT)
7544 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7545 else
7546 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7547 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7549 if (total_bytes > UNITS_PER_WORD)
7551 word = byte / UNITS_PER_WORD;
7552 if (WORDS_BIG_ENDIAN)
7553 word = (words - 1) - word;
7554 offset = word * UNITS_PER_WORD;
7555 if (BYTES_BIG_ENDIAN)
7556 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7557 else
7558 offset += byte % UNITS_PER_WORD;
7560 else
7561 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7562 ptr[offset] = value;
7564 return total_bytes;
7568 /* Subroutine of native_encode_expr. Encode the REAL_CST
7569 specified by EXPR into the buffer PTR of length LEN bytes.
7570 Return the number of bytes placed in the buffer, or zero
7571 upon failure. */
7573 static int
7574 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7576 tree type = TREE_TYPE (expr);
7577 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7578 int byte, offset, word, words, bitpos;
7579 unsigned char value;
7581 /* There are always 32 bits in each long, no matter the size of
7582 the hosts long. We handle floating point representations with
7583 up to 192 bits. */
7584 long tmp[6];
7586 if (total_bytes > len)
7587 return 0;
7588 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7590 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7592 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7593 bitpos += BITS_PER_UNIT)
7595 byte = (bitpos / BITS_PER_UNIT) & 3;
7596 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7598 if (UNITS_PER_WORD < 4)
7600 word = byte / UNITS_PER_WORD;
7601 if (WORDS_BIG_ENDIAN)
7602 word = (words - 1) - word;
7603 offset = word * UNITS_PER_WORD;
7604 if (BYTES_BIG_ENDIAN)
7605 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7606 else
7607 offset += byte % UNITS_PER_WORD;
7609 else
7610 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7611 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7613 return total_bytes;
7616 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7617 specified by EXPR into the buffer PTR of length LEN bytes.
7618 Return the number of bytes placed in the buffer, or zero
7619 upon failure. */
7621 static int
7622 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7624 int rsize, isize;
7625 tree part;
7627 part = TREE_REALPART (expr);
7628 rsize = native_encode_expr (part, ptr, len);
7629 if (rsize == 0)
7630 return 0;
7631 part = TREE_IMAGPART (expr);
7632 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7633 if (isize != rsize)
7634 return 0;
7635 return rsize + isize;
7639 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7640 specified by EXPR into the buffer PTR of length LEN bytes.
7641 Return the number of bytes placed in the buffer, or zero
7642 upon failure. */
7644 static int
7645 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7647 int i, size, offset, count;
7648 tree itype, elem, elements;
7650 offset = 0;
7651 elements = TREE_VECTOR_CST_ELTS (expr);
7652 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7653 itype = TREE_TYPE (TREE_TYPE (expr));
7654 size = GET_MODE_SIZE (TYPE_MODE (itype));
7655 for (i = 0; i < count; i++)
7657 if (elements)
7659 elem = TREE_VALUE (elements);
7660 elements = TREE_CHAIN (elements);
7662 else
7663 elem = NULL_TREE;
7665 if (elem)
7667 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7668 return 0;
7670 else
7672 if (offset + size > len)
7673 return 0;
7674 memset (ptr+offset, 0, size);
7676 offset += size;
7678 return offset;
7682 /* Subroutine of native_encode_expr. Encode the STRING_CST
7683 specified by EXPR into the buffer PTR of length LEN bytes.
7684 Return the number of bytes placed in the buffer, or zero
7685 upon failure. */
7687 static int
7688 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7690 tree type = TREE_TYPE (expr);
7691 HOST_WIDE_INT total_bytes;
7693 if (TREE_CODE (type) != ARRAY_TYPE
7694 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7695 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7696 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7697 return 0;
7698 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7699 if (total_bytes > len)
7700 return 0;
7701 if (TREE_STRING_LENGTH (expr) < total_bytes)
7703 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7704 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7705 total_bytes - TREE_STRING_LENGTH (expr));
7707 else
7708 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7709 return total_bytes;
7713 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7714 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7715 buffer PTR of length LEN bytes. Return the number of bytes
7716 placed in the buffer, or zero upon failure. */
7719 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7721 switch (TREE_CODE (expr))
7723 case INTEGER_CST:
7724 return native_encode_int (expr, ptr, len);
7726 case REAL_CST:
7727 return native_encode_real (expr, ptr, len);
7729 case COMPLEX_CST:
7730 return native_encode_complex (expr, ptr, len);
7732 case VECTOR_CST:
7733 return native_encode_vector (expr, ptr, len);
7735 case STRING_CST:
7736 return native_encode_string (expr, ptr, len);
7738 default:
7739 return 0;
7744 /* Subroutine of native_interpret_expr. Interpret the contents of
7745 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7746 If the buffer cannot be interpreted, return NULL_TREE. */
7748 static tree
7749 native_interpret_int (tree type, const unsigned char *ptr, int len)
7751 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7752 int byte, offset, word, words;
7753 unsigned char value;
7754 unsigned int HOST_WIDE_INT lo = 0;
7755 HOST_WIDE_INT hi = 0;
7757 if (total_bytes > len)
7758 return NULL_TREE;
7759 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7760 return NULL_TREE;
7761 words = total_bytes / UNITS_PER_WORD;
7763 for (byte = 0; byte < total_bytes; byte++)
7765 int bitpos = byte * BITS_PER_UNIT;
7766 if (total_bytes > UNITS_PER_WORD)
7768 word = byte / UNITS_PER_WORD;
7769 if (WORDS_BIG_ENDIAN)
7770 word = (words - 1) - word;
7771 offset = word * UNITS_PER_WORD;
7772 if (BYTES_BIG_ENDIAN)
7773 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7774 else
7775 offset += byte % UNITS_PER_WORD;
7777 else
7778 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7779 value = ptr[offset];
7781 if (bitpos < HOST_BITS_PER_WIDE_INT)
7782 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7783 else
7784 hi |= (unsigned HOST_WIDE_INT) value
7785 << (bitpos - HOST_BITS_PER_WIDE_INT);
7788 return build_int_cst_wide_type (type, lo, hi);
7792 /* Subroutine of native_interpret_expr. Interpret the contents of
7793 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7794 If the buffer cannot be interpreted, return NULL_TREE. */
7796 static tree
7797 native_interpret_real (tree type, const unsigned char *ptr, int len)
7799 enum machine_mode mode = TYPE_MODE (type);
7800 int total_bytes = GET_MODE_SIZE (mode);
7801 int byte, offset, word, words, bitpos;
7802 unsigned char value;
7803 /* There are always 32 bits in each long, no matter the size of
7804 the hosts long. We handle floating point representations with
7805 up to 192 bits. */
7806 REAL_VALUE_TYPE r;
7807 long tmp[6];
7809 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7810 if (total_bytes > len || total_bytes > 24)
7811 return NULL_TREE;
7812 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7814 memset (tmp, 0, sizeof (tmp));
7815 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7816 bitpos += BITS_PER_UNIT)
7818 byte = (bitpos / BITS_PER_UNIT) & 3;
7819 if (UNITS_PER_WORD < 4)
7821 word = byte / UNITS_PER_WORD;
7822 if (WORDS_BIG_ENDIAN)
7823 word = (words - 1) - word;
7824 offset = word * UNITS_PER_WORD;
7825 if (BYTES_BIG_ENDIAN)
7826 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7827 else
7828 offset += byte % UNITS_PER_WORD;
7830 else
7831 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7832 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7834 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7837 real_from_target (&r, tmp, mode);
7838 return build_real (type, r);
7842 /* Subroutine of native_interpret_expr. Interpret the contents of
7843 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7844 If the buffer cannot be interpreted, return NULL_TREE. */
7846 static tree
7847 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7849 tree etype, rpart, ipart;
7850 int size;
7852 etype = TREE_TYPE (type);
7853 size = GET_MODE_SIZE (TYPE_MODE (etype));
7854 if (size * 2 > len)
7855 return NULL_TREE;
7856 rpart = native_interpret_expr (etype, ptr, size);
7857 if (!rpart)
7858 return NULL_TREE;
7859 ipart = native_interpret_expr (etype, ptr+size, size);
7860 if (!ipart)
7861 return NULL_TREE;
7862 return build_complex (type, rpart, ipart);
7866 /* Subroutine of native_interpret_expr. Interpret the contents of
7867 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7868 If the buffer cannot be interpreted, return NULL_TREE. */
7870 static tree
7871 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7873 tree etype, elem, elements;
7874 int i, size, count;
7876 etype = TREE_TYPE (type);
7877 size = GET_MODE_SIZE (TYPE_MODE (etype));
7878 count = TYPE_VECTOR_SUBPARTS (type);
7879 if (size * count > len)
7880 return NULL_TREE;
7882 elements = NULL_TREE;
7883 for (i = count - 1; i >= 0; i--)
7885 elem = native_interpret_expr (etype, ptr+(i*size), size);
7886 if (!elem)
7887 return NULL_TREE;
7888 elements = tree_cons (NULL_TREE, elem, elements);
7890 return build_vector (type, elements);
7894 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7895 the buffer PTR of length LEN as a constant of type TYPE. For
7896 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7897 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7898 return NULL_TREE. */
7900 tree
7901 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7903 switch (TREE_CODE (type))
7905 case INTEGER_TYPE:
7906 case ENUMERAL_TYPE:
7907 case BOOLEAN_TYPE:
7908 return native_interpret_int (type, ptr, len);
7910 case REAL_TYPE:
7911 return native_interpret_real (type, ptr, len);
7913 case COMPLEX_TYPE:
7914 return native_interpret_complex (type, ptr, len);
7916 case VECTOR_TYPE:
7917 return native_interpret_vector (type, ptr, len);
7919 default:
7920 return NULL_TREE;
7925 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7926 TYPE at compile-time. If we're unable to perform the conversion
7927 return NULL_TREE. */
7929 static tree
7930 fold_view_convert_expr (tree type, tree expr)
7932 /* We support up to 512-bit values (for V8DFmode). */
7933 unsigned char buffer[64];
7934 int len;
7936 /* Check that the host and target are sane. */
7937 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7938 return NULL_TREE;
7940 len = native_encode_expr (expr, buffer, sizeof (buffer));
7941 if (len == 0)
7942 return NULL_TREE;
7944 return native_interpret_expr (type, buffer, len);
7947 /* Build an expression for the address of T. Folds away INDIRECT_REF
7948 to avoid confusing the gimplify process. When IN_FOLD is true
7949 avoid modifications of T. */
7951 static tree
7952 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7954 /* The size of the object is not relevant when talking about its address. */
7955 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7956 t = TREE_OPERAND (t, 0);
7958 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7959 if (TREE_CODE (t) == INDIRECT_REF
7960 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7962 t = TREE_OPERAND (t, 0);
7964 if (TREE_TYPE (t) != ptrtype)
7965 t = build1 (NOP_EXPR, ptrtype, t);
7967 else if (!in_fold)
7969 tree base = t;
7971 while (handled_component_p (base))
7972 base = TREE_OPERAND (base, 0);
7974 if (DECL_P (base))
7975 TREE_ADDRESSABLE (base) = 1;
7977 t = build1 (ADDR_EXPR, ptrtype, t);
7979 else
7980 t = build1 (ADDR_EXPR, ptrtype, t);
7982 return t;
7985 /* Build an expression for the address of T with type PTRTYPE. This
7986 function modifies the input parameter 'T' by sometimes setting the
7987 TREE_ADDRESSABLE flag. */
7989 tree
7990 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7992 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7995 /* Build an expression for the address of T. This function modifies
7996 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7997 flag. When called from fold functions, use fold_addr_expr instead. */
7999 tree
8000 build_fold_addr_expr (tree t)
8002 return build_fold_addr_expr_with_type_1 (t,
8003 build_pointer_type (TREE_TYPE (t)),
8004 false);
8007 /* Same as build_fold_addr_expr, builds an expression for the address
8008 of T, but avoids touching the input node 't'. Fold functions
8009 should use this version. */
8011 static tree
8012 fold_addr_expr (tree t)
8014 tree ptrtype = build_pointer_type (TREE_TYPE (t));
8016 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
8019 /* Fold a unary expression of code CODE and type TYPE with operand
8020 OP0. Return the folded expression if folding is successful.
8021 Otherwise, return NULL_TREE. */
8023 tree
8024 fold_unary (enum tree_code code, tree type, tree op0)
8026 tree tem;
8027 tree arg0;
8028 enum tree_code_class kind = TREE_CODE_CLASS (code);
8030 gcc_assert (IS_EXPR_CODE_CLASS (kind)
8031 && TREE_CODE_LENGTH (code) == 1);
8033 arg0 = op0;
8034 if (arg0)
8036 if (CONVERT_EXPR_CODE_P (code)
8037 || code == FLOAT_EXPR || code == ABS_EXPR)
8039 /* Don't use STRIP_NOPS, because signedness of argument type
8040 matters. */
8041 STRIP_SIGN_NOPS (arg0);
8043 else
8045 /* Strip any conversions that don't change the mode. This
8046 is safe for every expression, except for a comparison
8047 expression because its signedness is derived from its
8048 operands.
8050 Note that this is done as an internal manipulation within
8051 the constant folder, in order to find the simplest
8052 representation of the arguments so that their form can be
8053 studied. In any cases, the appropriate type conversions
8054 should be put back in the tree that will get out of the
8055 constant folder. */
8056 STRIP_NOPS (arg0);
8060 if (TREE_CODE_CLASS (code) == tcc_unary)
8062 if (TREE_CODE (arg0) == COMPOUND_EXPR)
8063 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8064 fold_build1 (code, type,
8065 fold_convert (TREE_TYPE (op0),
8066 TREE_OPERAND (arg0, 1))));
8067 else if (TREE_CODE (arg0) == COND_EXPR)
8069 tree arg01 = TREE_OPERAND (arg0, 1);
8070 tree arg02 = TREE_OPERAND (arg0, 2);
8071 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8072 arg01 = fold_build1 (code, type,
8073 fold_convert (TREE_TYPE (op0), arg01));
8074 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8075 arg02 = fold_build1 (code, type,
8076 fold_convert (TREE_TYPE (op0), arg02));
8077 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8078 arg01, arg02);
8080 /* If this was a conversion, and all we did was to move into
8081 inside the COND_EXPR, bring it back out. But leave it if
8082 it is a conversion from integer to integer and the
8083 result precision is no wider than a word since such a
8084 conversion is cheap and may be optimized away by combine,
8085 while it couldn't if it were outside the COND_EXPR. Then return
8086 so we don't get into an infinite recursion loop taking the
8087 conversion out and then back in. */
8089 if ((CONVERT_EXPR_CODE_P (code)
8090 || code == NON_LVALUE_EXPR)
8091 && TREE_CODE (tem) == COND_EXPR
8092 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8093 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8094 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8095 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8096 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8097 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8098 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8099 && (INTEGRAL_TYPE_P
8100 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8101 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8102 || flag_syntax_only))
8103 tem = build1 (code, type,
8104 build3 (COND_EXPR,
8105 TREE_TYPE (TREE_OPERAND
8106 (TREE_OPERAND (tem, 1), 0)),
8107 TREE_OPERAND (tem, 0),
8108 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8109 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8110 return tem;
8112 else if (COMPARISON_CLASS_P (arg0))
8114 if (TREE_CODE (type) == BOOLEAN_TYPE)
8116 arg0 = copy_node (arg0);
8117 TREE_TYPE (arg0) = type;
8118 return arg0;
8120 else if (TREE_CODE (type) != INTEGER_TYPE)
8121 return fold_build3 (COND_EXPR, type, arg0,
8122 fold_build1 (code, type,
8123 integer_one_node),
8124 fold_build1 (code, type,
8125 integer_zero_node));
8129 switch (code)
8131 case PAREN_EXPR:
8132 /* Re-association barriers around constants and other re-association
8133 barriers can be removed. */
8134 if (CONSTANT_CLASS_P (op0)
8135 || TREE_CODE (op0) == PAREN_EXPR)
8136 return fold_convert (type, op0);
8137 return NULL_TREE;
8139 CASE_CONVERT:
8140 case FLOAT_EXPR:
8141 case FIX_TRUNC_EXPR:
8142 if (TREE_TYPE (op0) == type)
8143 return op0;
8145 /* If we have (type) (a CMP b) and type is an integral type, return
8146 new expression involving the new type. */
8147 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8148 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8149 TREE_OPERAND (op0, 1));
8151 /* Handle cases of two conversions in a row. */
8152 if (CONVERT_EXPR_P (op0))
8154 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8155 tree inter_type = TREE_TYPE (op0);
8156 int inside_int = INTEGRAL_TYPE_P (inside_type);
8157 int inside_ptr = POINTER_TYPE_P (inside_type);
8158 int inside_float = FLOAT_TYPE_P (inside_type);
8159 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8160 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8161 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8162 int inter_int = INTEGRAL_TYPE_P (inter_type);
8163 int inter_ptr = POINTER_TYPE_P (inter_type);
8164 int inter_float = FLOAT_TYPE_P (inter_type);
8165 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8166 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8167 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8168 int final_int = INTEGRAL_TYPE_P (type);
8169 int final_ptr = POINTER_TYPE_P (type);
8170 int final_float = FLOAT_TYPE_P (type);
8171 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8172 unsigned int final_prec = TYPE_PRECISION (type);
8173 int final_unsignedp = TYPE_UNSIGNED (type);
8175 /* In addition to the cases of two conversions in a row
8176 handled below, if we are converting something to its own
8177 type via an object of identical or wider precision, neither
8178 conversion is needed. */
8179 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8180 && (((inter_int || inter_ptr) && final_int)
8181 || (inter_float && final_float))
8182 && inter_prec >= final_prec)
8183 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8185 /* Likewise, if the intermediate and initial types are either both
8186 float or both integer, we don't need the middle conversion if the
8187 former is wider than the latter and doesn't change the signedness
8188 (for integers). Avoid this if the final type is a pointer since
8189 then we sometimes need the middle conversion. Likewise if the
8190 final type has a precision not equal to the size of its mode. */
8191 if (((inter_int && inside_int)
8192 || (inter_float && inside_float)
8193 || (inter_vec && inside_vec))
8194 && inter_prec >= inside_prec
8195 && (inter_float || inter_vec
8196 || inter_unsignedp == inside_unsignedp)
8197 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8198 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8199 && ! final_ptr
8200 && (! final_vec || inter_prec == inside_prec))
8201 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8203 /* If we have a sign-extension of a zero-extended value, we can
8204 replace that by a single zero-extension. */
8205 if (inside_int && inter_int && final_int
8206 && inside_prec < inter_prec && inter_prec < final_prec
8207 && inside_unsignedp && !inter_unsignedp)
8208 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8210 /* Two conversions in a row are not needed unless:
8211 - some conversion is floating-point (overstrict for now), or
8212 - some conversion is a vector (overstrict for now), or
8213 - the intermediate type is narrower than both initial and
8214 final, or
8215 - the intermediate type and innermost type differ in signedness,
8216 and the outermost type is wider than the intermediate, or
8217 - the initial type is a pointer type and the precisions of the
8218 intermediate and final types differ, or
8219 - the final type is a pointer type and the precisions of the
8220 initial and intermediate types differ. */
8221 if (! inside_float && ! inter_float && ! final_float
8222 && ! inside_vec && ! inter_vec && ! final_vec
8223 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8224 && ! (inside_int && inter_int
8225 && inter_unsignedp != inside_unsignedp
8226 && inter_prec < final_prec)
8227 && ((inter_unsignedp && inter_prec > inside_prec)
8228 == (final_unsignedp && final_prec > inter_prec))
8229 && ! (inside_ptr && inter_prec != final_prec)
8230 && ! (final_ptr && inside_prec != inter_prec)
8231 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8232 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8233 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8236 /* Handle (T *)&A.B.C for A being of type T and B and C
8237 living at offset zero. This occurs frequently in
8238 C++ upcasting and then accessing the base. */
8239 if (TREE_CODE (op0) == ADDR_EXPR
8240 && POINTER_TYPE_P (type)
8241 && handled_component_p (TREE_OPERAND (op0, 0)))
8243 HOST_WIDE_INT bitsize, bitpos;
8244 tree offset;
8245 enum machine_mode mode;
8246 int unsignedp, volatilep;
8247 tree base = TREE_OPERAND (op0, 0);
8248 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8249 &mode, &unsignedp, &volatilep, false);
8250 /* If the reference was to a (constant) zero offset, we can use
8251 the address of the base if it has the same base type
8252 as the result type. */
8253 if (! offset && bitpos == 0
8254 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8255 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8256 return fold_convert (type, fold_addr_expr (base));
8259 if (TREE_CODE (op0) == MODIFY_EXPR
8260 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8261 /* Detect assigning a bitfield. */
8262 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8263 && DECL_BIT_FIELD
8264 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8266 /* Don't leave an assignment inside a conversion
8267 unless assigning a bitfield. */
8268 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8269 /* First do the assignment, then return converted constant. */
8270 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8271 TREE_NO_WARNING (tem) = 1;
8272 TREE_USED (tem) = 1;
8273 return tem;
8276 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8277 constants (if x has signed type, the sign bit cannot be set
8278 in c). This folds extension into the BIT_AND_EXPR.
8279 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8280 very likely don't have maximal range for their precision and this
8281 transformation effectively doesn't preserve non-maximal ranges. */
8282 if (TREE_CODE (type) == INTEGER_TYPE
8283 && TREE_CODE (op0) == BIT_AND_EXPR
8284 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8285 /* Not if the conversion is to the sub-type. */
8286 && TREE_TYPE (type) != TREE_TYPE (op0))
8288 tree and = op0;
8289 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8290 int change = 0;
8292 if (TYPE_UNSIGNED (TREE_TYPE (and))
8293 || (TYPE_PRECISION (type)
8294 <= TYPE_PRECISION (TREE_TYPE (and))))
8295 change = 1;
8296 else if (TYPE_PRECISION (TREE_TYPE (and1))
8297 <= HOST_BITS_PER_WIDE_INT
8298 && host_integerp (and1, 1))
8300 unsigned HOST_WIDE_INT cst;
8302 cst = tree_low_cst (and1, 1);
8303 cst &= (HOST_WIDE_INT) -1
8304 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8305 change = (cst == 0);
8306 #ifdef LOAD_EXTEND_OP
8307 if (change
8308 && !flag_syntax_only
8309 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8310 == ZERO_EXTEND))
8312 tree uns = unsigned_type_for (TREE_TYPE (and0));
8313 and0 = fold_convert (uns, and0);
8314 and1 = fold_convert (uns, and1);
8316 #endif
8318 if (change)
8320 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8321 TREE_INT_CST_HIGH (and1), 0,
8322 TREE_OVERFLOW (and1));
8323 return fold_build2 (BIT_AND_EXPR, type,
8324 fold_convert (type, and0), tem);
8328 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8329 when one of the new casts will fold away. Conservatively we assume
8330 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8331 if (POINTER_TYPE_P (type)
8332 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8333 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8334 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8335 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8337 tree arg00 = TREE_OPERAND (arg0, 0);
8338 tree arg01 = TREE_OPERAND (arg0, 1);
8340 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8341 fold_convert (sizetype, arg01));
8344 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8345 of the same precision, and X is an integer type not narrower than
8346 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8347 if (INTEGRAL_TYPE_P (type)
8348 && TREE_CODE (op0) == BIT_NOT_EXPR
8349 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8350 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8351 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8353 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8354 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8355 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8356 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8359 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8360 type of X and Y (integer types only). */
8361 if (INTEGRAL_TYPE_P (type)
8362 && TREE_CODE (op0) == MULT_EXPR
8363 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8364 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8366 /* Be careful not to introduce new overflows. */
8367 tree mult_type;
8368 if (TYPE_OVERFLOW_WRAPS (type))
8369 mult_type = type;
8370 else
8371 mult_type = unsigned_type_for (type);
8373 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8375 tem = fold_build2 (MULT_EXPR, mult_type,
8376 fold_convert (mult_type,
8377 TREE_OPERAND (op0, 0)),
8378 fold_convert (mult_type,
8379 TREE_OPERAND (op0, 1)));
8380 return fold_convert (type, tem);
8384 tem = fold_convert_const (code, type, op0);
8385 return tem ? tem : NULL_TREE;
8387 case FIXED_CONVERT_EXPR:
8388 tem = fold_convert_const (code, type, arg0);
8389 return tem ? tem : NULL_TREE;
8391 case VIEW_CONVERT_EXPR:
8392 if (TREE_TYPE (op0) == type)
8393 return op0;
8394 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8395 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8397 /* For integral conversions with the same precision or pointer
8398 conversions use a NOP_EXPR instead. */
8399 if ((INTEGRAL_TYPE_P (type)
8400 || POINTER_TYPE_P (type))
8401 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8402 || POINTER_TYPE_P (TREE_TYPE (op0)))
8403 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8404 /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8405 a sub-type to its base type as generated by the Ada FE. */
8406 && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8407 && TREE_TYPE (TREE_TYPE (op0))))
8408 return fold_convert (type, op0);
8410 /* Strip inner integral conversions that do not change the precision. */
8411 if (CONVERT_EXPR_P (op0)
8412 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8413 || POINTER_TYPE_P (TREE_TYPE (op0)))
8414 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8415 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8416 && (TYPE_PRECISION (TREE_TYPE (op0))
8417 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8418 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8420 return fold_view_convert_expr (type, op0);
8422 case NEGATE_EXPR:
8423 tem = fold_negate_expr (arg0);
8424 if (tem)
8425 return fold_convert (type, tem);
8426 return NULL_TREE;
8428 case ABS_EXPR:
8429 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8430 return fold_abs_const (arg0, type);
8431 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8432 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8433 /* Convert fabs((double)float) into (double)fabsf(float). */
8434 else if (TREE_CODE (arg0) == NOP_EXPR
8435 && TREE_CODE (type) == REAL_TYPE)
8437 tree targ0 = strip_float_extensions (arg0);
8438 if (targ0 != arg0)
8439 return fold_convert (type, fold_build1 (ABS_EXPR,
8440 TREE_TYPE (targ0),
8441 targ0));
8443 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8444 else if (TREE_CODE (arg0) == ABS_EXPR)
8445 return arg0;
8446 else if (tree_expr_nonnegative_p (arg0))
8447 return arg0;
8449 /* Strip sign ops from argument. */
8450 if (TREE_CODE (type) == REAL_TYPE)
8452 tem = fold_strip_sign_ops (arg0);
8453 if (tem)
8454 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8456 return NULL_TREE;
8458 case CONJ_EXPR:
8459 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8460 return fold_convert (type, arg0);
8461 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8463 tree itype = TREE_TYPE (type);
8464 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8465 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8466 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8468 if (TREE_CODE (arg0) == COMPLEX_CST)
8470 tree itype = TREE_TYPE (type);
8471 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8472 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8473 return build_complex (type, rpart, negate_expr (ipart));
8475 if (TREE_CODE (arg0) == CONJ_EXPR)
8476 return fold_convert (type, TREE_OPERAND (arg0, 0));
8477 return NULL_TREE;
8479 case BIT_NOT_EXPR:
8480 if (TREE_CODE (arg0) == INTEGER_CST)
8481 return fold_not_const (arg0, type);
8482 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8483 return fold_convert (type, TREE_OPERAND (arg0, 0));
8484 /* Convert ~ (-A) to A - 1. */
8485 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8486 return fold_build2 (MINUS_EXPR, type,
8487 fold_convert (type, TREE_OPERAND (arg0, 0)),
8488 build_int_cst (type, 1));
8489 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8490 else if (INTEGRAL_TYPE_P (type)
8491 && ((TREE_CODE (arg0) == MINUS_EXPR
8492 && integer_onep (TREE_OPERAND (arg0, 1)))
8493 || (TREE_CODE (arg0) == PLUS_EXPR
8494 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8495 return fold_build1 (NEGATE_EXPR, type,
8496 fold_convert (type, TREE_OPERAND (arg0, 0)));
8497 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8498 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8499 && (tem = fold_unary (BIT_NOT_EXPR, type,
8500 fold_convert (type,
8501 TREE_OPERAND (arg0, 0)))))
8502 return fold_build2 (BIT_XOR_EXPR, type, tem,
8503 fold_convert (type, TREE_OPERAND (arg0, 1)));
8504 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8505 && (tem = fold_unary (BIT_NOT_EXPR, type,
8506 fold_convert (type,
8507 TREE_OPERAND (arg0, 1)))))
8508 return fold_build2 (BIT_XOR_EXPR, type,
8509 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8510 /* Perform BIT_NOT_EXPR on each element individually. */
8511 else if (TREE_CODE (arg0) == VECTOR_CST)
8513 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8514 int count = TYPE_VECTOR_SUBPARTS (type), i;
8516 for (i = 0; i < count; i++)
8518 if (elements)
8520 elem = TREE_VALUE (elements);
8521 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8522 if (elem == NULL_TREE)
8523 break;
8524 elements = TREE_CHAIN (elements);
8526 else
8527 elem = build_int_cst (TREE_TYPE (type), -1);
8528 list = tree_cons (NULL_TREE, elem, list);
8530 if (i == count)
8531 return build_vector (type, nreverse (list));
8534 return NULL_TREE;
8536 case TRUTH_NOT_EXPR:
8537 /* The argument to invert_truthvalue must have Boolean type. */
8538 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8539 arg0 = fold_convert (boolean_type_node, arg0);
8541 /* Note that the operand of this must be an int
8542 and its values must be 0 or 1.
8543 ("true" is a fixed value perhaps depending on the language,
8544 but we don't handle values other than 1 correctly yet.) */
8545 tem = fold_truth_not_expr (arg0);
8546 if (!tem)
8547 return NULL_TREE;
8548 return fold_convert (type, tem);
8550 case REALPART_EXPR:
8551 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8552 return fold_convert (type, arg0);
8553 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8554 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8555 TREE_OPERAND (arg0, 1));
8556 if (TREE_CODE (arg0) == COMPLEX_CST)
8557 return fold_convert (type, TREE_REALPART (arg0));
8558 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8560 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8561 tem = fold_build2 (TREE_CODE (arg0), itype,
8562 fold_build1 (REALPART_EXPR, itype,
8563 TREE_OPERAND (arg0, 0)),
8564 fold_build1 (REALPART_EXPR, itype,
8565 TREE_OPERAND (arg0, 1)));
8566 return fold_convert (type, tem);
8568 if (TREE_CODE (arg0) == CONJ_EXPR)
8570 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8571 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8572 return fold_convert (type, tem);
8574 if (TREE_CODE (arg0) == CALL_EXPR)
8576 tree fn = get_callee_fndecl (arg0);
8577 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8578 switch (DECL_FUNCTION_CODE (fn))
8580 CASE_FLT_FN (BUILT_IN_CEXPI):
8581 fn = mathfn_built_in (type, BUILT_IN_COS);
8582 if (fn)
8583 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8584 break;
8586 default:
8587 break;
8590 return NULL_TREE;
8592 case IMAGPART_EXPR:
8593 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8594 return fold_convert (type, integer_zero_node);
8595 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8596 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8597 TREE_OPERAND (arg0, 0));
8598 if (TREE_CODE (arg0) == COMPLEX_CST)
8599 return fold_convert (type, TREE_IMAGPART (arg0));
8600 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8602 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8603 tem = fold_build2 (TREE_CODE (arg0), itype,
8604 fold_build1 (IMAGPART_EXPR, itype,
8605 TREE_OPERAND (arg0, 0)),
8606 fold_build1 (IMAGPART_EXPR, itype,
8607 TREE_OPERAND (arg0, 1)));
8608 return fold_convert (type, tem);
8610 if (TREE_CODE (arg0) == CONJ_EXPR)
8612 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8613 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8614 return fold_convert (type, negate_expr (tem));
8616 if (TREE_CODE (arg0) == CALL_EXPR)
8618 tree fn = get_callee_fndecl (arg0);
8619 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8620 switch (DECL_FUNCTION_CODE (fn))
8622 CASE_FLT_FN (BUILT_IN_CEXPI):
8623 fn = mathfn_built_in (type, BUILT_IN_SIN);
8624 if (fn)
8625 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8626 break;
8628 default:
8629 break;
8632 return NULL_TREE;
8634 default:
8635 return NULL_TREE;
8636 } /* switch (code) */
8640 /* If the operation was a conversion do _not_ mark a resulting constant
8641 with TREE_OVERFLOW if the original constant was not. These conversions
8642 have implementation defined behavior and retaining the TREE_OVERFLOW
8643 flag here would confuse later passes such as VRP. */
8644 tree
8645 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8647 tree res = fold_unary (code, type, op0);
8648 if (res
8649 && TREE_CODE (res) == INTEGER_CST
8650 && TREE_CODE (op0) == INTEGER_CST
8651 && CONVERT_EXPR_CODE_P (code))
8652 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8654 return res;
8657 /* Fold a binary expression of code CODE and type TYPE with operands
8658 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8659 Return the folded expression if folding is successful. Otherwise,
8660 return NULL_TREE. */
8662 static tree
8663 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8665 enum tree_code compl_code;
8667 if (code == MIN_EXPR)
8668 compl_code = MAX_EXPR;
8669 else if (code == MAX_EXPR)
8670 compl_code = MIN_EXPR;
8671 else
8672 gcc_unreachable ();
8674 /* MIN (MAX (a, b), b) == b. */
8675 if (TREE_CODE (op0) == compl_code
8676 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8677 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8679 /* MIN (MAX (b, a), b) == b. */
8680 if (TREE_CODE (op0) == compl_code
8681 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8682 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8683 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8685 /* MIN (a, MAX (a, b)) == a. */
8686 if (TREE_CODE (op1) == compl_code
8687 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8688 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8689 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8691 /* MIN (a, MAX (b, a)) == a. */
8692 if (TREE_CODE (op1) == compl_code
8693 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8694 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8695 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8697 return NULL_TREE;
8700 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8701 by changing CODE to reduce the magnitude of constants involved in
8702 ARG0 of the comparison.
8703 Returns a canonicalized comparison tree if a simplification was
8704 possible, otherwise returns NULL_TREE.
8705 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8706 valid if signed overflow is undefined. */
8708 static tree
8709 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8710 tree arg0, tree arg1,
8711 bool *strict_overflow_p)
8713 enum tree_code code0 = TREE_CODE (arg0);
8714 tree t, cst0 = NULL_TREE;
8715 int sgn0;
8716 bool swap = false;
8718 /* Match A +- CST code arg1 and CST code arg1. We can change the
8719 first form only if overflow is undefined. */
8720 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8721 /* In principle pointers also have undefined overflow behavior,
8722 but that causes problems elsewhere. */
8723 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8724 && (code0 == MINUS_EXPR
8725 || code0 == PLUS_EXPR)
8726 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8727 || code0 == INTEGER_CST))
8728 return NULL_TREE;
8730 /* Identify the constant in arg0 and its sign. */
8731 if (code0 == INTEGER_CST)
8732 cst0 = arg0;
8733 else
8734 cst0 = TREE_OPERAND (arg0, 1);
8735 sgn0 = tree_int_cst_sgn (cst0);
8737 /* Overflowed constants and zero will cause problems. */
8738 if (integer_zerop (cst0)
8739 || TREE_OVERFLOW (cst0))
8740 return NULL_TREE;
8742 /* See if we can reduce the magnitude of the constant in
8743 arg0 by changing the comparison code. */
8744 if (code0 == INTEGER_CST)
8746 /* CST <= arg1 -> CST-1 < arg1. */
8747 if (code == LE_EXPR && sgn0 == 1)
8748 code = LT_EXPR;
8749 /* -CST < arg1 -> -CST-1 <= arg1. */
8750 else if (code == LT_EXPR && sgn0 == -1)
8751 code = LE_EXPR;
8752 /* CST > arg1 -> CST-1 >= arg1. */
8753 else if (code == GT_EXPR && sgn0 == 1)
8754 code = GE_EXPR;
8755 /* -CST >= arg1 -> -CST-1 > arg1. */
8756 else if (code == GE_EXPR && sgn0 == -1)
8757 code = GT_EXPR;
8758 else
8759 return NULL_TREE;
8760 /* arg1 code' CST' might be more canonical. */
8761 swap = true;
8763 else
8765 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8766 if (code == LT_EXPR
8767 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8768 code = LE_EXPR;
8769 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8770 else if (code == GT_EXPR
8771 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8772 code = GE_EXPR;
8773 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8774 else if (code == LE_EXPR
8775 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8776 code = LT_EXPR;
8777 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8778 else if (code == GE_EXPR
8779 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8780 code = GT_EXPR;
8781 else
8782 return NULL_TREE;
8783 *strict_overflow_p = true;
8786 /* Now build the constant reduced in magnitude. But not if that
8787 would produce one outside of its types range. */
8788 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8789 && ((sgn0 == 1
8790 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8791 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8792 || (sgn0 == -1
8793 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8794 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8795 /* We cannot swap the comparison here as that would cause us to
8796 endlessly recurse. */
8797 return NULL_TREE;
8799 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8800 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8801 if (code0 != INTEGER_CST)
8802 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8804 /* If swapping might yield to a more canonical form, do so. */
8805 if (swap)
8806 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8807 else
8808 return fold_build2 (code, type, t, arg1);
8811 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8812 overflow further. Try to decrease the magnitude of constants involved
8813 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8814 and put sole constants at the second argument position.
8815 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8817 static tree
8818 maybe_canonicalize_comparison (enum tree_code code, tree type,
8819 tree arg0, tree arg1)
8821 tree t;
8822 bool strict_overflow_p;
8823 const char * const warnmsg = G_("assuming signed overflow does not occur "
8824 "when reducing constant in comparison");
8826 /* Try canonicalization by simplifying arg0. */
8827 strict_overflow_p = false;
8828 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8829 &strict_overflow_p);
8830 if (t)
8832 if (strict_overflow_p)
8833 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8834 return t;
8837 /* Try canonicalization by simplifying arg1 using the swapped
8838 comparison. */
8839 code = swap_tree_comparison (code);
8840 strict_overflow_p = false;
8841 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8842 &strict_overflow_p);
8843 if (t && strict_overflow_p)
8844 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8845 return t;
8848 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8849 space. This is used to avoid issuing overflow warnings for
8850 expressions like &p->x which can not wrap. */
8852 static bool
8853 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8855 unsigned HOST_WIDE_INT offset_low, total_low;
8856 HOST_WIDE_INT size, offset_high, total_high;
8858 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8859 return true;
8861 if (bitpos < 0)
8862 return true;
8864 if (offset == NULL_TREE)
8866 offset_low = 0;
8867 offset_high = 0;
8869 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8870 return true;
8871 else
8873 offset_low = TREE_INT_CST_LOW (offset);
8874 offset_high = TREE_INT_CST_HIGH (offset);
8877 if (add_double_with_sign (offset_low, offset_high,
8878 bitpos / BITS_PER_UNIT, 0,
8879 &total_low, &total_high,
8880 true))
8881 return true;
8883 if (total_high != 0)
8884 return true;
8886 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8887 if (size <= 0)
8888 return true;
8890 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8891 array. */
8892 if (TREE_CODE (base) == ADDR_EXPR)
8894 HOST_WIDE_INT base_size;
8896 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8897 if (base_size > 0 && size < base_size)
8898 size = base_size;
8901 return total_low > (unsigned HOST_WIDE_INT) size;
8904 /* Subroutine of fold_binary. This routine performs all of the
8905 transformations that are common to the equality/inequality
8906 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8907 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8908 fold_binary should call fold_binary. Fold a comparison with
8909 tree code CODE and type TYPE with operands OP0 and OP1. Return
8910 the folded comparison or NULL_TREE. */
8912 static tree
8913 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8915 tree arg0, arg1, tem;
8917 arg0 = op0;
8918 arg1 = op1;
8920 STRIP_SIGN_NOPS (arg0);
8921 STRIP_SIGN_NOPS (arg1);
8923 tem = fold_relational_const (code, type, arg0, arg1);
8924 if (tem != NULL_TREE)
8925 return tem;
8927 /* If one arg is a real or integer constant, put it last. */
8928 if (tree_swap_operands_p (arg0, arg1, true))
8929 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8931 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8932 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8933 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8934 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8935 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8936 && (TREE_CODE (arg1) == INTEGER_CST
8937 && !TREE_OVERFLOW (arg1)))
8939 tree const1 = TREE_OPERAND (arg0, 1);
8940 tree const2 = arg1;
8941 tree variable = TREE_OPERAND (arg0, 0);
8942 tree lhs;
8943 int lhs_add;
8944 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8946 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8947 TREE_TYPE (arg1), const2, const1);
8949 /* If the constant operation overflowed this can be
8950 simplified as a comparison against INT_MAX/INT_MIN. */
8951 if (TREE_CODE (lhs) == INTEGER_CST
8952 && TREE_OVERFLOW (lhs))
8954 int const1_sgn = tree_int_cst_sgn (const1);
8955 enum tree_code code2 = code;
8957 /* Get the sign of the constant on the lhs if the
8958 operation were VARIABLE + CONST1. */
8959 if (TREE_CODE (arg0) == MINUS_EXPR)
8960 const1_sgn = -const1_sgn;
8962 /* The sign of the constant determines if we overflowed
8963 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8964 Canonicalize to the INT_MIN overflow by swapping the comparison
8965 if necessary. */
8966 if (const1_sgn == -1)
8967 code2 = swap_tree_comparison (code);
8969 /* We now can look at the canonicalized case
8970 VARIABLE + 1 CODE2 INT_MIN
8971 and decide on the result. */
8972 if (code2 == LT_EXPR
8973 || code2 == LE_EXPR
8974 || code2 == EQ_EXPR)
8975 return omit_one_operand (type, boolean_false_node, variable);
8976 else if (code2 == NE_EXPR
8977 || code2 == GE_EXPR
8978 || code2 == GT_EXPR)
8979 return omit_one_operand (type, boolean_true_node, variable);
8982 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8983 && (TREE_CODE (lhs) != INTEGER_CST
8984 || !TREE_OVERFLOW (lhs)))
8986 fold_overflow_warning (("assuming signed overflow does not occur "
8987 "when changing X +- C1 cmp C2 to "
8988 "X cmp C1 +- C2"),
8989 WARN_STRICT_OVERFLOW_COMPARISON);
8990 return fold_build2 (code, type, variable, lhs);
8994 /* For comparisons of pointers we can decompose it to a compile time
8995 comparison of the base objects and the offsets into the object.
8996 This requires at least one operand being an ADDR_EXPR or a
8997 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8998 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8999 && (TREE_CODE (arg0) == ADDR_EXPR
9000 || TREE_CODE (arg1) == ADDR_EXPR
9001 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9002 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9004 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9005 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9006 enum machine_mode mode;
9007 int volatilep, unsignedp;
9008 bool indirect_base0 = false, indirect_base1 = false;
9010 /* Get base and offset for the access. Strip ADDR_EXPR for
9011 get_inner_reference, but put it back by stripping INDIRECT_REF
9012 off the base object if possible. indirect_baseN will be true
9013 if baseN is not an address but refers to the object itself. */
9014 base0 = arg0;
9015 if (TREE_CODE (arg0) == ADDR_EXPR)
9017 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9018 &bitsize, &bitpos0, &offset0, &mode,
9019 &unsignedp, &volatilep, false);
9020 if (TREE_CODE (base0) == INDIRECT_REF)
9021 base0 = TREE_OPERAND (base0, 0);
9022 else
9023 indirect_base0 = true;
9025 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9027 base0 = TREE_OPERAND (arg0, 0);
9028 offset0 = TREE_OPERAND (arg0, 1);
9031 base1 = arg1;
9032 if (TREE_CODE (arg1) == ADDR_EXPR)
9034 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9035 &bitsize, &bitpos1, &offset1, &mode,
9036 &unsignedp, &volatilep, false);
9037 if (TREE_CODE (base1) == INDIRECT_REF)
9038 base1 = TREE_OPERAND (base1, 0);
9039 else
9040 indirect_base1 = true;
9042 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9044 base1 = TREE_OPERAND (arg1, 0);
9045 offset1 = TREE_OPERAND (arg1, 1);
9048 /* If we have equivalent bases we might be able to simplify. */
9049 if (indirect_base0 == indirect_base1
9050 && operand_equal_p (base0, base1, 0))
9052 /* We can fold this expression to a constant if the non-constant
9053 offset parts are equal. */
9054 if ((offset0 == offset1
9055 || (offset0 && offset1
9056 && operand_equal_p (offset0, offset1, 0)))
9057 && (code == EQ_EXPR
9058 || code == NE_EXPR
9059 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9062 if (code != EQ_EXPR
9063 && code != NE_EXPR
9064 && bitpos0 != bitpos1
9065 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9066 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9067 fold_overflow_warning (("assuming pointer wraparound does not "
9068 "occur when comparing P +- C1 with "
9069 "P +- C2"),
9070 WARN_STRICT_OVERFLOW_CONDITIONAL);
9072 switch (code)
9074 case EQ_EXPR:
9075 return constant_boolean_node (bitpos0 == bitpos1, type);
9076 case NE_EXPR:
9077 return constant_boolean_node (bitpos0 != bitpos1, type);
9078 case LT_EXPR:
9079 return constant_boolean_node (bitpos0 < bitpos1, type);
9080 case LE_EXPR:
9081 return constant_boolean_node (bitpos0 <= bitpos1, type);
9082 case GE_EXPR:
9083 return constant_boolean_node (bitpos0 >= bitpos1, type);
9084 case GT_EXPR:
9085 return constant_boolean_node (bitpos0 > bitpos1, type);
9086 default:;
9089 /* We can simplify the comparison to a comparison of the variable
9090 offset parts if the constant offset parts are equal.
9091 Be careful to use signed size type here because otherwise we
9092 mess with array offsets in the wrong way. This is possible
9093 because pointer arithmetic is restricted to retain within an
9094 object and overflow on pointer differences is undefined as of
9095 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9096 else if (bitpos0 == bitpos1
9097 && ((code == EQ_EXPR || code == NE_EXPR)
9098 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9100 tree signed_size_type_node;
9101 signed_size_type_node = signed_type_for (size_type_node);
9103 /* By converting to signed size type we cover middle-end pointer
9104 arithmetic which operates on unsigned pointer types of size
9105 type size and ARRAY_REF offsets which are properly sign or
9106 zero extended from their type in case it is narrower than
9107 size type. */
9108 if (offset0 == NULL_TREE)
9109 offset0 = build_int_cst (signed_size_type_node, 0);
9110 else
9111 offset0 = fold_convert (signed_size_type_node, offset0);
9112 if (offset1 == NULL_TREE)
9113 offset1 = build_int_cst (signed_size_type_node, 0);
9114 else
9115 offset1 = fold_convert (signed_size_type_node, offset1);
9117 if (code != EQ_EXPR
9118 && code != NE_EXPR
9119 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9120 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9121 fold_overflow_warning (("assuming pointer wraparound does not "
9122 "occur when comparing P +- C1 with "
9123 "P +- C2"),
9124 WARN_STRICT_OVERFLOW_COMPARISON);
9126 return fold_build2 (code, type, offset0, offset1);
9129 /* For non-equal bases we can simplify if they are addresses
9130 of local binding decls or constants. */
9131 else if (indirect_base0 && indirect_base1
9132 /* We know that !operand_equal_p (base0, base1, 0)
9133 because the if condition was false. But make
9134 sure two decls are not the same. */
9135 && base0 != base1
9136 && TREE_CODE (arg0) == ADDR_EXPR
9137 && TREE_CODE (arg1) == ADDR_EXPR
9138 && (((TREE_CODE (base0) == VAR_DECL
9139 || TREE_CODE (base0) == PARM_DECL)
9140 && (targetm.binds_local_p (base0)
9141 || CONSTANT_CLASS_P (base1)))
9142 || CONSTANT_CLASS_P (base0))
9143 && (((TREE_CODE (base1) == VAR_DECL
9144 || TREE_CODE (base1) == PARM_DECL)
9145 && (targetm.binds_local_p (base1)
9146 || CONSTANT_CLASS_P (base0)))
9147 || CONSTANT_CLASS_P (base1)))
9149 if (code == EQ_EXPR)
9150 return omit_two_operands (type, boolean_false_node, arg0, arg1);
9151 else if (code == NE_EXPR)
9152 return omit_two_operands (type, boolean_true_node, arg0, arg1);
9154 /* For equal offsets we can simplify to a comparison of the
9155 base addresses. */
9156 else if (bitpos0 == bitpos1
9157 && (indirect_base0
9158 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9159 && (indirect_base1
9160 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9161 && ((offset0 == offset1)
9162 || (offset0 && offset1
9163 && operand_equal_p (offset0, offset1, 0))))
9165 if (indirect_base0)
9166 base0 = fold_addr_expr (base0);
9167 if (indirect_base1)
9168 base1 = fold_addr_expr (base1);
9169 return fold_build2 (code, type, base0, base1);
9173 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9174 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9175 the resulting offset is smaller in absolute value than the
9176 original one. */
9177 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9178 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9179 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9180 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9181 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9182 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9183 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9185 tree const1 = TREE_OPERAND (arg0, 1);
9186 tree const2 = TREE_OPERAND (arg1, 1);
9187 tree variable1 = TREE_OPERAND (arg0, 0);
9188 tree variable2 = TREE_OPERAND (arg1, 0);
9189 tree cst;
9190 const char * const warnmsg = G_("assuming signed overflow does not "
9191 "occur when combining constants around "
9192 "a comparison");
9194 /* Put the constant on the side where it doesn't overflow and is
9195 of lower absolute value than before. */
9196 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9197 ? MINUS_EXPR : PLUS_EXPR,
9198 const2, const1, 0);
9199 if (!TREE_OVERFLOW (cst)
9200 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9202 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9203 return fold_build2 (code, type,
9204 variable1,
9205 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9206 variable2, cst));
9209 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9210 ? MINUS_EXPR : PLUS_EXPR,
9211 const1, const2, 0);
9212 if (!TREE_OVERFLOW (cst)
9213 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9215 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9216 return fold_build2 (code, type,
9217 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9218 variable1, cst),
9219 variable2);
9223 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9224 signed arithmetic case. That form is created by the compiler
9225 often enough for folding it to be of value. One example is in
9226 computing loop trip counts after Operator Strength Reduction. */
9227 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9228 && TREE_CODE (arg0) == MULT_EXPR
9229 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9230 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9231 && integer_zerop (arg1))
9233 tree const1 = TREE_OPERAND (arg0, 1);
9234 tree const2 = arg1; /* zero */
9235 tree variable1 = TREE_OPERAND (arg0, 0);
9236 enum tree_code cmp_code = code;
9238 gcc_assert (!integer_zerop (const1));
9240 fold_overflow_warning (("assuming signed overflow does not occur when "
9241 "eliminating multiplication in comparison "
9242 "with zero"),
9243 WARN_STRICT_OVERFLOW_COMPARISON);
9245 /* If const1 is negative we swap the sense of the comparison. */
9246 if (tree_int_cst_sgn (const1) < 0)
9247 cmp_code = swap_tree_comparison (cmp_code);
9249 return fold_build2 (cmp_code, type, variable1, const2);
9252 tem = maybe_canonicalize_comparison (code, type, op0, op1);
9253 if (tem)
9254 return tem;
9256 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9258 tree targ0 = strip_float_extensions (arg0);
9259 tree targ1 = strip_float_extensions (arg1);
9260 tree newtype = TREE_TYPE (targ0);
9262 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9263 newtype = TREE_TYPE (targ1);
9265 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9266 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9267 return fold_build2 (code, type, fold_convert (newtype, targ0),
9268 fold_convert (newtype, targ1));
9270 /* (-a) CMP (-b) -> b CMP a */
9271 if (TREE_CODE (arg0) == NEGATE_EXPR
9272 && TREE_CODE (arg1) == NEGATE_EXPR)
9273 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9274 TREE_OPERAND (arg0, 0));
9276 if (TREE_CODE (arg1) == REAL_CST)
9278 REAL_VALUE_TYPE cst;
9279 cst = TREE_REAL_CST (arg1);
9281 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9282 if (TREE_CODE (arg0) == NEGATE_EXPR)
9283 return fold_build2 (swap_tree_comparison (code), type,
9284 TREE_OPERAND (arg0, 0),
9285 build_real (TREE_TYPE (arg1),
9286 REAL_VALUE_NEGATE (cst)));
9288 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9289 /* a CMP (-0) -> a CMP 0 */
9290 if (REAL_VALUE_MINUS_ZERO (cst))
9291 return fold_build2 (code, type, arg0,
9292 build_real (TREE_TYPE (arg1), dconst0));
9294 /* x != NaN is always true, other ops are always false. */
9295 if (REAL_VALUE_ISNAN (cst)
9296 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9298 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9299 return omit_one_operand (type, tem, arg0);
9302 /* Fold comparisons against infinity. */
9303 if (REAL_VALUE_ISINF (cst)
9304 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9306 tem = fold_inf_compare (code, type, arg0, arg1);
9307 if (tem != NULL_TREE)
9308 return tem;
9312 /* If this is a comparison of a real constant with a PLUS_EXPR
9313 or a MINUS_EXPR of a real constant, we can convert it into a
9314 comparison with a revised real constant as long as no overflow
9315 occurs when unsafe_math_optimizations are enabled. */
9316 if (flag_unsafe_math_optimizations
9317 && TREE_CODE (arg1) == REAL_CST
9318 && (TREE_CODE (arg0) == PLUS_EXPR
9319 || TREE_CODE (arg0) == MINUS_EXPR)
9320 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9321 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9322 ? MINUS_EXPR : PLUS_EXPR,
9323 arg1, TREE_OPERAND (arg0, 1), 0))
9324 && !TREE_OVERFLOW (tem))
9325 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9327 /* Likewise, we can simplify a comparison of a real constant with
9328 a MINUS_EXPR whose first operand is also a real constant, i.e.
9329 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9330 floating-point types only if -fassociative-math is set. */
9331 if (flag_associative_math
9332 && TREE_CODE (arg1) == REAL_CST
9333 && TREE_CODE (arg0) == MINUS_EXPR
9334 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9335 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9336 arg1, 0))
9337 && !TREE_OVERFLOW (tem))
9338 return fold_build2 (swap_tree_comparison (code), type,
9339 TREE_OPERAND (arg0, 1), tem);
9341 /* Fold comparisons against built-in math functions. */
9342 if (TREE_CODE (arg1) == REAL_CST
9343 && flag_unsafe_math_optimizations
9344 && ! flag_errno_math)
9346 enum built_in_function fcode = builtin_mathfn_code (arg0);
9348 if (fcode != END_BUILTINS)
9350 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9351 if (tem != NULL_TREE)
9352 return tem;
9357 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9358 && CONVERT_EXPR_P (arg0))
9360 /* If we are widening one operand of an integer comparison,
9361 see if the other operand is similarly being widened. Perhaps we
9362 can do the comparison in the narrower type. */
9363 tem = fold_widened_comparison (code, type, arg0, arg1);
9364 if (tem)
9365 return tem;
9367 /* Or if we are changing signedness. */
9368 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9369 if (tem)
9370 return tem;
9373 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9374 constant, we can simplify it. */
9375 if (TREE_CODE (arg1) == INTEGER_CST
9376 && (TREE_CODE (arg0) == MIN_EXPR
9377 || TREE_CODE (arg0) == MAX_EXPR)
9378 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9380 tem = optimize_minmax_comparison (code, type, op0, op1);
9381 if (tem)
9382 return tem;
9385 /* Simplify comparison of something with itself. (For IEEE
9386 floating-point, we can only do some of these simplifications.) */
9387 if (operand_equal_p (arg0, arg1, 0))
9389 switch (code)
9391 case EQ_EXPR:
9392 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9393 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9394 return constant_boolean_node (1, type);
9395 break;
9397 case GE_EXPR:
9398 case LE_EXPR:
9399 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9400 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9401 return constant_boolean_node (1, type);
9402 return fold_build2 (EQ_EXPR, type, arg0, arg1);
9404 case NE_EXPR:
9405 /* For NE, we can only do this simplification if integer
9406 or we don't honor IEEE floating point NaNs. */
9407 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9408 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9409 break;
9410 /* ... fall through ... */
9411 case GT_EXPR:
9412 case LT_EXPR:
9413 return constant_boolean_node (0, type);
9414 default:
9415 gcc_unreachable ();
9419 /* If we are comparing an expression that just has comparisons
9420 of two integer values, arithmetic expressions of those comparisons,
9421 and constants, we can simplify it. There are only three cases
9422 to check: the two values can either be equal, the first can be
9423 greater, or the second can be greater. Fold the expression for
9424 those three values. Since each value must be 0 or 1, we have
9425 eight possibilities, each of which corresponds to the constant 0
9426 or 1 or one of the six possible comparisons.
9428 This handles common cases like (a > b) == 0 but also handles
9429 expressions like ((x > y) - (y > x)) > 0, which supposedly
9430 occur in macroized code. */
9432 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9434 tree cval1 = 0, cval2 = 0;
9435 int save_p = 0;
9437 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9438 /* Don't handle degenerate cases here; they should already
9439 have been handled anyway. */
9440 && cval1 != 0 && cval2 != 0
9441 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9442 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9443 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9444 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9445 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9446 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9447 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9449 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9450 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9452 /* We can't just pass T to eval_subst in case cval1 or cval2
9453 was the same as ARG1. */
9455 tree high_result
9456 = fold_build2 (code, type,
9457 eval_subst (arg0, cval1, maxval,
9458 cval2, minval),
9459 arg1);
9460 tree equal_result
9461 = fold_build2 (code, type,
9462 eval_subst (arg0, cval1, maxval,
9463 cval2, maxval),
9464 arg1);
9465 tree low_result
9466 = fold_build2 (code, type,
9467 eval_subst (arg0, cval1, minval,
9468 cval2, maxval),
9469 arg1);
9471 /* All three of these results should be 0 or 1. Confirm they are.
9472 Then use those values to select the proper code to use. */
9474 if (TREE_CODE (high_result) == INTEGER_CST
9475 && TREE_CODE (equal_result) == INTEGER_CST
9476 && TREE_CODE (low_result) == INTEGER_CST)
9478 /* Make a 3-bit mask with the high-order bit being the
9479 value for `>', the next for '=', and the low for '<'. */
9480 switch ((integer_onep (high_result) * 4)
9481 + (integer_onep (equal_result) * 2)
9482 + integer_onep (low_result))
9484 case 0:
9485 /* Always false. */
9486 return omit_one_operand (type, integer_zero_node, arg0);
9487 case 1:
9488 code = LT_EXPR;
9489 break;
9490 case 2:
9491 code = EQ_EXPR;
9492 break;
9493 case 3:
9494 code = LE_EXPR;
9495 break;
9496 case 4:
9497 code = GT_EXPR;
9498 break;
9499 case 5:
9500 code = NE_EXPR;
9501 break;
9502 case 6:
9503 code = GE_EXPR;
9504 break;
9505 case 7:
9506 /* Always true. */
9507 return omit_one_operand (type, integer_one_node, arg0);
9510 if (save_p)
9511 return save_expr (build2 (code, type, cval1, cval2));
9512 return fold_build2 (code, type, cval1, cval2);
9517 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9518 into a single range test. */
9519 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9520 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9521 && TREE_CODE (arg1) == INTEGER_CST
9522 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9523 && !integer_zerop (TREE_OPERAND (arg0, 1))
9524 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9525 && !TREE_OVERFLOW (arg1))
9527 tem = fold_div_compare (code, type, arg0, arg1);
9528 if (tem != NULL_TREE)
9529 return tem;
9532 /* Fold ~X op ~Y as Y op X. */
9533 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9534 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9536 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9537 return fold_build2 (code, type,
9538 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9539 TREE_OPERAND (arg0, 0));
9542 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9543 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9544 && TREE_CODE (arg1) == INTEGER_CST)
9546 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9547 return fold_build2 (swap_tree_comparison (code), type,
9548 TREE_OPERAND (arg0, 0),
9549 fold_build1 (BIT_NOT_EXPR, cmp_type,
9550 fold_convert (cmp_type, arg1)));
9553 return NULL_TREE;
9557 /* Subroutine of fold_binary. Optimize complex multiplications of the
9558 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9559 argument EXPR represents the expression "z" of type TYPE. */
9561 static tree
9562 fold_mult_zconjz (tree type, tree expr)
9564 tree itype = TREE_TYPE (type);
9565 tree rpart, ipart, tem;
9567 if (TREE_CODE (expr) == COMPLEX_EXPR)
9569 rpart = TREE_OPERAND (expr, 0);
9570 ipart = TREE_OPERAND (expr, 1);
9572 else if (TREE_CODE (expr) == COMPLEX_CST)
9574 rpart = TREE_REALPART (expr);
9575 ipart = TREE_IMAGPART (expr);
9577 else
9579 expr = save_expr (expr);
9580 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9581 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9584 rpart = save_expr (rpart);
9585 ipart = save_expr (ipart);
9586 tem = fold_build2 (PLUS_EXPR, itype,
9587 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9588 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9589 return fold_build2 (COMPLEX_EXPR, type, tem,
9590 fold_convert (itype, integer_zero_node));
9594 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9595 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9596 guarantees that P and N have the same least significant log2(M) bits.
9597 N is not otherwise constrained. In particular, N is not normalized to
9598 0 <= N < M as is common. In general, the precise value of P is unknown.
9599 M is chosen as large as possible such that constant N can be determined.
9601 Returns M and sets *RESIDUE to N. */
9603 static unsigned HOST_WIDE_INT
9604 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9606 enum tree_code code;
9608 *residue = 0;
9610 code = TREE_CODE (expr);
9611 if (code == ADDR_EXPR)
9613 expr = TREE_OPERAND (expr, 0);
9614 if (handled_component_p (expr))
9616 HOST_WIDE_INT bitsize, bitpos;
9617 tree offset;
9618 enum machine_mode mode;
9619 int unsignedp, volatilep;
9621 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9622 &mode, &unsignedp, &volatilep, false);
9623 *residue = bitpos / BITS_PER_UNIT;
9624 if (offset)
9626 if (TREE_CODE (offset) == INTEGER_CST)
9627 *residue += TREE_INT_CST_LOW (offset);
9628 else
9629 /* We don't handle more complicated offset expressions. */
9630 return 1;
9634 if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9635 return DECL_ALIGN_UNIT (expr);
9637 else if (code == POINTER_PLUS_EXPR)
9639 tree op0, op1;
9640 unsigned HOST_WIDE_INT modulus;
9641 enum tree_code inner_code;
9643 op0 = TREE_OPERAND (expr, 0);
9644 STRIP_NOPS (op0);
9645 modulus = get_pointer_modulus_and_residue (op0, residue);
9647 op1 = TREE_OPERAND (expr, 1);
9648 STRIP_NOPS (op1);
9649 inner_code = TREE_CODE (op1);
9650 if (inner_code == INTEGER_CST)
9652 *residue += TREE_INT_CST_LOW (op1);
9653 return modulus;
9655 else if (inner_code == MULT_EXPR)
9657 op1 = TREE_OPERAND (op1, 1);
9658 if (TREE_CODE (op1) == INTEGER_CST)
9660 unsigned HOST_WIDE_INT align;
9662 /* Compute the greatest power-of-2 divisor of op1. */
9663 align = TREE_INT_CST_LOW (op1);
9664 align &= -align;
9666 /* If align is non-zero and less than *modulus, replace
9667 *modulus with align., If align is 0, then either op1 is 0
9668 or the greatest power-of-2 divisor of op1 doesn't fit in an
9669 unsigned HOST_WIDE_INT. In either case, no additional
9670 constraint is imposed. */
9671 if (align)
9672 modulus = MIN (modulus, align);
9674 return modulus;
9679 /* If we get here, we were unable to determine anything useful about the
9680 expression. */
9681 return 1;
9685 /* Fold a binary expression of code CODE and type TYPE with operands
9686 OP0 and OP1. Return the folded expression if folding is
9687 successful. Otherwise, return NULL_TREE. */
9689 tree
9690 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9692 enum tree_code_class kind = TREE_CODE_CLASS (code);
9693 tree arg0, arg1, tem;
9694 tree t1 = NULL_TREE;
9695 bool strict_overflow_p;
9697 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9698 && TREE_CODE_LENGTH (code) == 2
9699 && op0 != NULL_TREE
9700 && op1 != NULL_TREE);
9702 arg0 = op0;
9703 arg1 = op1;
9705 /* Strip any conversions that don't change the mode. This is
9706 safe for every expression, except for a comparison expression
9707 because its signedness is derived from its operands. So, in
9708 the latter case, only strip conversions that don't change the
9709 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9710 preserved.
9712 Note that this is done as an internal manipulation within the
9713 constant folder, in order to find the simplest representation
9714 of the arguments so that their form can be studied. In any
9715 cases, the appropriate type conversions should be put back in
9716 the tree that will get out of the constant folder. */
9718 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9720 STRIP_SIGN_NOPS (arg0);
9721 STRIP_SIGN_NOPS (arg1);
9723 else
9725 STRIP_NOPS (arg0);
9726 STRIP_NOPS (arg1);
9729 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9730 constant but we can't do arithmetic on them. */
9731 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9732 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9733 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9734 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9735 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9736 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9738 if (kind == tcc_binary)
9740 /* Make sure type and arg0 have the same saturating flag. */
9741 gcc_assert (TYPE_SATURATING (type)
9742 == TYPE_SATURATING (TREE_TYPE (arg0)));
9743 tem = const_binop (code, arg0, arg1, 0);
9745 else if (kind == tcc_comparison)
9746 tem = fold_relational_const (code, type, arg0, arg1);
9747 else
9748 tem = NULL_TREE;
9750 if (tem != NULL_TREE)
9752 if (TREE_TYPE (tem) != type)
9753 tem = fold_convert (type, tem);
9754 return tem;
9758 /* If this is a commutative operation, and ARG0 is a constant, move it
9759 to ARG1 to reduce the number of tests below. */
9760 if (commutative_tree_code (code)
9761 && tree_swap_operands_p (arg0, arg1, true))
9762 return fold_build2 (code, type, op1, op0);
9764 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9766 First check for cases where an arithmetic operation is applied to a
9767 compound, conditional, or comparison operation. Push the arithmetic
9768 operation inside the compound or conditional to see if any folding
9769 can then be done. Convert comparison to conditional for this purpose.
9770 The also optimizes non-constant cases that used to be done in
9771 expand_expr.
9773 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9774 one of the operands is a comparison and the other is a comparison, a
9775 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9776 code below would make the expression more complex. Change it to a
9777 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9778 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9780 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9781 || code == EQ_EXPR || code == NE_EXPR)
9782 && ((truth_value_p (TREE_CODE (arg0))
9783 && (truth_value_p (TREE_CODE (arg1))
9784 || (TREE_CODE (arg1) == BIT_AND_EXPR
9785 && integer_onep (TREE_OPERAND (arg1, 1)))))
9786 || (truth_value_p (TREE_CODE (arg1))
9787 && (truth_value_p (TREE_CODE (arg0))
9788 || (TREE_CODE (arg0) == BIT_AND_EXPR
9789 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9791 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9792 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9793 : TRUTH_XOR_EXPR,
9794 boolean_type_node,
9795 fold_convert (boolean_type_node, arg0),
9796 fold_convert (boolean_type_node, arg1));
9798 if (code == EQ_EXPR)
9799 tem = invert_truthvalue (tem);
9801 return fold_convert (type, tem);
9804 if (TREE_CODE_CLASS (code) == tcc_binary
9805 || TREE_CODE_CLASS (code) == tcc_comparison)
9807 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9808 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9809 fold_build2 (code, type,
9810 fold_convert (TREE_TYPE (op0),
9811 TREE_OPERAND (arg0, 1)),
9812 op1));
9813 if (TREE_CODE (arg1) == COMPOUND_EXPR
9814 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9815 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9816 fold_build2 (code, type, op0,
9817 fold_convert (TREE_TYPE (op1),
9818 TREE_OPERAND (arg1, 1))));
9820 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9822 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9823 arg0, arg1,
9824 /*cond_first_p=*/1);
9825 if (tem != NULL_TREE)
9826 return tem;
9829 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9831 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9832 arg1, arg0,
9833 /*cond_first_p=*/0);
9834 if (tem != NULL_TREE)
9835 return tem;
9839 switch (code)
9841 case POINTER_PLUS_EXPR:
9842 /* 0 +p index -> (type)index */
9843 if (integer_zerop (arg0))
9844 return non_lvalue (fold_convert (type, arg1));
9846 /* PTR +p 0 -> PTR */
9847 if (integer_zerop (arg1))
9848 return non_lvalue (fold_convert (type, arg0));
9850 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9851 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9852 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9853 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9854 fold_convert (sizetype, arg1),
9855 fold_convert (sizetype, arg0)));
9857 /* index +p PTR -> PTR +p index */
9858 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9859 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9860 return fold_build2 (POINTER_PLUS_EXPR, type,
9861 fold_convert (type, arg1),
9862 fold_convert (sizetype, arg0));
9864 /* (PTR +p B) +p A -> PTR +p (B + A) */
9865 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9867 tree inner;
9868 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9869 tree arg00 = TREE_OPERAND (arg0, 0);
9870 inner = fold_build2 (PLUS_EXPR, sizetype,
9871 arg01, fold_convert (sizetype, arg1));
9872 return fold_convert (type,
9873 fold_build2 (POINTER_PLUS_EXPR,
9874 TREE_TYPE (arg00), arg00, inner));
9877 /* PTR_CST +p CST -> CST1 */
9878 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9879 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9881 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9882 of the array. Loop optimizer sometimes produce this type of
9883 expressions. */
9884 if (TREE_CODE (arg0) == ADDR_EXPR)
9886 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9887 if (tem)
9888 return fold_convert (type, tem);
9891 return NULL_TREE;
9893 case PLUS_EXPR:
9894 /* A + (-B) -> A - B */
9895 if (TREE_CODE (arg1) == NEGATE_EXPR)
9896 return fold_build2 (MINUS_EXPR, type,
9897 fold_convert (type, arg0),
9898 fold_convert (type, TREE_OPERAND (arg1, 0)));
9899 /* (-A) + B -> B - A */
9900 if (TREE_CODE (arg0) == NEGATE_EXPR
9901 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9902 return fold_build2 (MINUS_EXPR, type,
9903 fold_convert (type, arg1),
9904 fold_convert (type, TREE_OPERAND (arg0, 0)));
9906 if (INTEGRAL_TYPE_P (type))
9908 /* Convert ~A + 1 to -A. */
9909 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9910 && integer_onep (arg1))
9911 return fold_build1 (NEGATE_EXPR, type,
9912 fold_convert (type, TREE_OPERAND (arg0, 0)));
9914 /* ~X + X is -1. */
9915 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9916 && !TYPE_OVERFLOW_TRAPS (type))
9918 tree tem = TREE_OPERAND (arg0, 0);
9920 STRIP_NOPS (tem);
9921 if (operand_equal_p (tem, arg1, 0))
9923 t1 = build_int_cst_type (type, -1);
9924 return omit_one_operand (type, t1, arg1);
9928 /* X + ~X is -1. */
9929 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9930 && !TYPE_OVERFLOW_TRAPS (type))
9932 tree tem = TREE_OPERAND (arg1, 0);
9934 STRIP_NOPS (tem);
9935 if (operand_equal_p (arg0, tem, 0))
9937 t1 = build_int_cst_type (type, -1);
9938 return omit_one_operand (type, t1, arg0);
9942 /* X + (X / CST) * -CST is X % CST. */
9943 if (TREE_CODE (arg1) == MULT_EXPR
9944 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9945 && operand_equal_p (arg0,
9946 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9948 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9949 tree cst1 = TREE_OPERAND (arg1, 1);
9950 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9951 if (sum && integer_zerop (sum))
9952 return fold_convert (type,
9953 fold_build2 (TRUNC_MOD_EXPR,
9954 TREE_TYPE (arg0), arg0, cst0));
9958 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9959 same or one. Make sure type is not saturating.
9960 fold_plusminus_mult_expr will re-associate. */
9961 if ((TREE_CODE (arg0) == MULT_EXPR
9962 || TREE_CODE (arg1) == MULT_EXPR)
9963 && !TYPE_SATURATING (type)
9964 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9966 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9967 if (tem)
9968 return tem;
9971 if (! FLOAT_TYPE_P (type))
9973 if (integer_zerop (arg1))
9974 return non_lvalue (fold_convert (type, arg0));
9976 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9977 with a constant, and the two constants have no bits in common,
9978 we should treat this as a BIT_IOR_EXPR since this may produce more
9979 simplifications. */
9980 if (TREE_CODE (arg0) == BIT_AND_EXPR
9981 && TREE_CODE (arg1) == BIT_AND_EXPR
9982 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9983 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9984 && integer_zerop (const_binop (BIT_AND_EXPR,
9985 TREE_OPERAND (arg0, 1),
9986 TREE_OPERAND (arg1, 1), 0)))
9988 code = BIT_IOR_EXPR;
9989 goto bit_ior;
9992 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9993 (plus (plus (mult) (mult)) (foo)) so that we can
9994 take advantage of the factoring cases below. */
9995 if (((TREE_CODE (arg0) == PLUS_EXPR
9996 || TREE_CODE (arg0) == MINUS_EXPR)
9997 && TREE_CODE (arg1) == MULT_EXPR)
9998 || ((TREE_CODE (arg1) == PLUS_EXPR
9999 || TREE_CODE (arg1) == MINUS_EXPR)
10000 && TREE_CODE (arg0) == MULT_EXPR))
10002 tree parg0, parg1, parg, marg;
10003 enum tree_code pcode;
10005 if (TREE_CODE (arg1) == MULT_EXPR)
10006 parg = arg0, marg = arg1;
10007 else
10008 parg = arg1, marg = arg0;
10009 pcode = TREE_CODE (parg);
10010 parg0 = TREE_OPERAND (parg, 0);
10011 parg1 = TREE_OPERAND (parg, 1);
10012 STRIP_NOPS (parg0);
10013 STRIP_NOPS (parg1);
10015 if (TREE_CODE (parg0) == MULT_EXPR
10016 && TREE_CODE (parg1) != MULT_EXPR)
10017 return fold_build2 (pcode, type,
10018 fold_build2 (PLUS_EXPR, type,
10019 fold_convert (type, parg0),
10020 fold_convert (type, marg)),
10021 fold_convert (type, parg1));
10022 if (TREE_CODE (parg0) != MULT_EXPR
10023 && TREE_CODE (parg1) == MULT_EXPR)
10024 return fold_build2 (PLUS_EXPR, type,
10025 fold_convert (type, parg0),
10026 fold_build2 (pcode, type,
10027 fold_convert (type, marg),
10028 fold_convert (type,
10029 parg1)));
10032 else
10034 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10035 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10036 return non_lvalue (fold_convert (type, arg0));
10038 /* Likewise if the operands are reversed. */
10039 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10040 return non_lvalue (fold_convert (type, arg1));
10042 /* Convert X + -C into X - C. */
10043 if (TREE_CODE (arg1) == REAL_CST
10044 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10046 tem = fold_negate_const (arg1, type);
10047 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10048 return fold_build2 (MINUS_EXPR, type,
10049 fold_convert (type, arg0),
10050 fold_convert (type, tem));
10053 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10054 to __complex__ ( x, y ). This is not the same for SNaNs or
10055 if signed zeros are involved. */
10056 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10057 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10058 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10060 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10061 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10062 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10063 bool arg0rz = false, arg0iz = false;
10064 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10065 || (arg0i && (arg0iz = real_zerop (arg0i))))
10067 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10068 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10069 if (arg0rz && arg1i && real_zerop (arg1i))
10071 tree rp = arg1r ? arg1r
10072 : build1 (REALPART_EXPR, rtype, arg1);
10073 tree ip = arg0i ? arg0i
10074 : build1 (IMAGPART_EXPR, rtype, arg0);
10075 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10077 else if (arg0iz && arg1r && real_zerop (arg1r))
10079 tree rp = arg0r ? arg0r
10080 : build1 (REALPART_EXPR, rtype, arg0);
10081 tree ip = arg1i ? arg1i
10082 : build1 (IMAGPART_EXPR, rtype, arg1);
10083 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10088 if (flag_unsafe_math_optimizations
10089 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10090 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10091 && (tem = distribute_real_division (code, type, arg0, arg1)))
10092 return tem;
10094 /* Convert x+x into x*2.0. */
10095 if (operand_equal_p (arg0, arg1, 0)
10096 && SCALAR_FLOAT_TYPE_P (type))
10097 return fold_build2 (MULT_EXPR, type, arg0,
10098 build_real (type, dconst2));
10100 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10101 We associate floats only if the user has specified
10102 -fassociative-math. */
10103 if (flag_associative_math
10104 && TREE_CODE (arg1) == PLUS_EXPR
10105 && TREE_CODE (arg0) != MULT_EXPR)
10107 tree tree10 = TREE_OPERAND (arg1, 0);
10108 tree tree11 = TREE_OPERAND (arg1, 1);
10109 if (TREE_CODE (tree11) == MULT_EXPR
10110 && TREE_CODE (tree10) == MULT_EXPR)
10112 tree tree0;
10113 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10114 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10117 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10118 We associate floats only if the user has specified
10119 -fassociative-math. */
10120 if (flag_associative_math
10121 && TREE_CODE (arg0) == PLUS_EXPR
10122 && TREE_CODE (arg1) != MULT_EXPR)
10124 tree tree00 = TREE_OPERAND (arg0, 0);
10125 tree tree01 = TREE_OPERAND (arg0, 1);
10126 if (TREE_CODE (tree01) == MULT_EXPR
10127 && TREE_CODE (tree00) == MULT_EXPR)
10129 tree tree0;
10130 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10131 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10136 bit_rotate:
10137 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10138 is a rotate of A by C1 bits. */
10139 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10140 is a rotate of A by B bits. */
10142 enum tree_code code0, code1;
10143 tree rtype;
10144 code0 = TREE_CODE (arg0);
10145 code1 = TREE_CODE (arg1);
10146 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10147 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10148 && operand_equal_p (TREE_OPERAND (arg0, 0),
10149 TREE_OPERAND (arg1, 0), 0)
10150 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10151 TYPE_UNSIGNED (rtype))
10152 /* Only create rotates in complete modes. Other cases are not
10153 expanded properly. */
10154 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10156 tree tree01, tree11;
10157 enum tree_code code01, code11;
10159 tree01 = TREE_OPERAND (arg0, 1);
10160 tree11 = TREE_OPERAND (arg1, 1);
10161 STRIP_NOPS (tree01);
10162 STRIP_NOPS (tree11);
10163 code01 = TREE_CODE (tree01);
10164 code11 = TREE_CODE (tree11);
10165 if (code01 == INTEGER_CST
10166 && code11 == INTEGER_CST
10167 && TREE_INT_CST_HIGH (tree01) == 0
10168 && TREE_INT_CST_HIGH (tree11) == 0
10169 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10170 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10171 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10172 code0 == LSHIFT_EXPR ? tree01 : tree11);
10173 else if (code11 == MINUS_EXPR)
10175 tree tree110, tree111;
10176 tree110 = TREE_OPERAND (tree11, 0);
10177 tree111 = TREE_OPERAND (tree11, 1);
10178 STRIP_NOPS (tree110);
10179 STRIP_NOPS (tree111);
10180 if (TREE_CODE (tree110) == INTEGER_CST
10181 && 0 == compare_tree_int (tree110,
10182 TYPE_PRECISION
10183 (TREE_TYPE (TREE_OPERAND
10184 (arg0, 0))))
10185 && operand_equal_p (tree01, tree111, 0))
10186 return build2 ((code0 == LSHIFT_EXPR
10187 ? LROTATE_EXPR
10188 : RROTATE_EXPR),
10189 type, TREE_OPERAND (arg0, 0), tree01);
10191 else if (code01 == MINUS_EXPR)
10193 tree tree010, tree011;
10194 tree010 = TREE_OPERAND (tree01, 0);
10195 tree011 = TREE_OPERAND (tree01, 1);
10196 STRIP_NOPS (tree010);
10197 STRIP_NOPS (tree011);
10198 if (TREE_CODE (tree010) == INTEGER_CST
10199 && 0 == compare_tree_int (tree010,
10200 TYPE_PRECISION
10201 (TREE_TYPE (TREE_OPERAND
10202 (arg0, 0))))
10203 && operand_equal_p (tree11, tree011, 0))
10204 return build2 ((code0 != LSHIFT_EXPR
10205 ? LROTATE_EXPR
10206 : RROTATE_EXPR),
10207 type, TREE_OPERAND (arg0, 0), tree11);
10212 associate:
10213 /* In most languages, can't associate operations on floats through
10214 parentheses. Rather than remember where the parentheses were, we
10215 don't associate floats at all, unless the user has specified
10216 -fassociative-math.
10217 And, we need to make sure type is not saturating. */
10219 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10220 && !TYPE_SATURATING (type))
10222 tree var0, con0, lit0, minus_lit0;
10223 tree var1, con1, lit1, minus_lit1;
10224 bool ok = true;
10226 /* Split both trees into variables, constants, and literals. Then
10227 associate each group together, the constants with literals,
10228 then the result with variables. This increases the chances of
10229 literals being recombined later and of generating relocatable
10230 expressions for the sum of a constant and literal. */
10231 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10232 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10233 code == MINUS_EXPR);
10235 /* With undefined overflow we can only associate constants
10236 with one variable. */
10237 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10238 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10239 && var0 && var1)
10241 tree tmp0 = var0;
10242 tree tmp1 = var1;
10244 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10245 tmp0 = TREE_OPERAND (tmp0, 0);
10246 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10247 tmp1 = TREE_OPERAND (tmp1, 0);
10248 /* The only case we can still associate with two variables
10249 is if they are the same, modulo negation. */
10250 if (!operand_equal_p (tmp0, tmp1, 0))
10251 ok = false;
10254 /* Only do something if we found more than two objects. Otherwise,
10255 nothing has changed and we risk infinite recursion. */
10256 if (ok
10257 && (2 < ((var0 != 0) + (var1 != 0)
10258 + (con0 != 0) + (con1 != 0)
10259 + (lit0 != 0) + (lit1 != 0)
10260 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10262 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10263 if (code == MINUS_EXPR)
10264 code = PLUS_EXPR;
10266 var0 = associate_trees (var0, var1, code, type);
10267 con0 = associate_trees (con0, con1, code, type);
10268 lit0 = associate_trees (lit0, lit1, code, type);
10269 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10271 /* Preserve the MINUS_EXPR if the negative part of the literal is
10272 greater than the positive part. Otherwise, the multiplicative
10273 folding code (i.e extract_muldiv) may be fooled in case
10274 unsigned constants are subtracted, like in the following
10275 example: ((X*2 + 4) - 8U)/2. */
10276 if (minus_lit0 && lit0)
10278 if (TREE_CODE (lit0) == INTEGER_CST
10279 && TREE_CODE (minus_lit0) == INTEGER_CST
10280 && tree_int_cst_lt (lit0, minus_lit0))
10282 minus_lit0 = associate_trees (minus_lit0, lit0,
10283 MINUS_EXPR, type);
10284 lit0 = 0;
10286 else
10288 lit0 = associate_trees (lit0, minus_lit0,
10289 MINUS_EXPR, type);
10290 minus_lit0 = 0;
10293 if (minus_lit0)
10295 if (con0 == 0)
10296 return fold_convert (type,
10297 associate_trees (var0, minus_lit0,
10298 MINUS_EXPR, type));
10299 else
10301 con0 = associate_trees (con0, minus_lit0,
10302 MINUS_EXPR, type);
10303 return fold_convert (type,
10304 associate_trees (var0, con0,
10305 PLUS_EXPR, type));
10309 con0 = associate_trees (con0, lit0, code, type);
10310 return fold_convert (type, associate_trees (var0, con0,
10311 code, type));
10315 return NULL_TREE;
10317 case MINUS_EXPR:
10318 /* Pointer simplifications for subtraction, simple reassociations. */
10319 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10321 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10322 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10323 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10325 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10326 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10327 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10328 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10329 return fold_build2 (PLUS_EXPR, type,
10330 fold_build2 (MINUS_EXPR, type, arg00, arg10),
10331 fold_build2 (MINUS_EXPR, type, arg01, arg11));
10333 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10334 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10336 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10337 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10338 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10339 if (tmp)
10340 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10343 /* A - (-B) -> A + B */
10344 if (TREE_CODE (arg1) == NEGATE_EXPR)
10345 return fold_build2 (PLUS_EXPR, type, op0,
10346 fold_convert (type, TREE_OPERAND (arg1, 0)));
10347 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10348 if (TREE_CODE (arg0) == NEGATE_EXPR
10349 && (FLOAT_TYPE_P (type)
10350 || INTEGRAL_TYPE_P (type))
10351 && negate_expr_p (arg1)
10352 && reorder_operands_p (arg0, arg1))
10353 return fold_build2 (MINUS_EXPR, type,
10354 fold_convert (type, negate_expr (arg1)),
10355 fold_convert (type, TREE_OPERAND (arg0, 0)));
10356 /* Convert -A - 1 to ~A. */
10357 if (INTEGRAL_TYPE_P (type)
10358 && TREE_CODE (arg0) == NEGATE_EXPR
10359 && integer_onep (arg1)
10360 && !TYPE_OVERFLOW_TRAPS (type))
10361 return fold_build1 (BIT_NOT_EXPR, type,
10362 fold_convert (type, TREE_OPERAND (arg0, 0)));
10364 /* Convert -1 - A to ~A. */
10365 if (INTEGRAL_TYPE_P (type)
10366 && integer_all_onesp (arg0))
10367 return fold_build1 (BIT_NOT_EXPR, type, op1);
10370 /* X - (X / CST) * CST is X % CST. */
10371 if (INTEGRAL_TYPE_P (type)
10372 && TREE_CODE (arg1) == MULT_EXPR
10373 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10374 && operand_equal_p (arg0,
10375 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10376 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10377 TREE_OPERAND (arg1, 1), 0))
10378 return fold_convert (type,
10379 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10380 arg0, TREE_OPERAND (arg1, 1)));
10382 if (! FLOAT_TYPE_P (type))
10384 if (integer_zerop (arg0))
10385 return negate_expr (fold_convert (type, arg1));
10386 if (integer_zerop (arg1))
10387 return non_lvalue (fold_convert (type, arg0));
10389 /* Fold A - (A & B) into ~B & A. */
10390 if (!TREE_SIDE_EFFECTS (arg0)
10391 && TREE_CODE (arg1) == BIT_AND_EXPR)
10393 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10395 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10396 return fold_build2 (BIT_AND_EXPR, type,
10397 fold_build1 (BIT_NOT_EXPR, type, arg10),
10398 fold_convert (type, arg0));
10400 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10402 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10403 return fold_build2 (BIT_AND_EXPR, type,
10404 fold_build1 (BIT_NOT_EXPR, type, arg11),
10405 fold_convert (type, arg0));
10409 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10410 any power of 2 minus 1. */
10411 if (TREE_CODE (arg0) == BIT_AND_EXPR
10412 && TREE_CODE (arg1) == BIT_AND_EXPR
10413 && operand_equal_p (TREE_OPERAND (arg0, 0),
10414 TREE_OPERAND (arg1, 0), 0))
10416 tree mask0 = TREE_OPERAND (arg0, 1);
10417 tree mask1 = TREE_OPERAND (arg1, 1);
10418 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10420 if (operand_equal_p (tem, mask1, 0))
10422 tem = fold_build2 (BIT_XOR_EXPR, type,
10423 TREE_OPERAND (arg0, 0), mask1);
10424 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10429 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10430 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10431 return non_lvalue (fold_convert (type, arg0));
10433 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10434 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10435 (-ARG1 + ARG0) reduces to -ARG1. */
10436 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10437 return negate_expr (fold_convert (type, arg1));
10439 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10440 __complex__ ( x, -y ). This is not the same for SNaNs or if
10441 signed zeros are involved. */
10442 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10443 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10444 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10446 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10447 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10448 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10449 bool arg0rz = false, arg0iz = false;
10450 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10451 || (arg0i && (arg0iz = real_zerop (arg0i))))
10453 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10454 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10455 if (arg0rz && arg1i && real_zerop (arg1i))
10457 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10458 arg1r ? arg1r
10459 : build1 (REALPART_EXPR, rtype, arg1));
10460 tree ip = arg0i ? arg0i
10461 : build1 (IMAGPART_EXPR, rtype, arg0);
10462 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10464 else if (arg0iz && arg1r && real_zerop (arg1r))
10466 tree rp = arg0r ? arg0r
10467 : build1 (REALPART_EXPR, rtype, arg0);
10468 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10469 arg1i ? arg1i
10470 : build1 (IMAGPART_EXPR, rtype, arg1));
10471 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10476 /* Fold &x - &x. This can happen from &x.foo - &x.
10477 This is unsafe for certain floats even in non-IEEE formats.
10478 In IEEE, it is unsafe because it does wrong for NaNs.
10479 Also note that operand_equal_p is always false if an operand
10480 is volatile. */
10482 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10483 && operand_equal_p (arg0, arg1, 0))
10484 return fold_convert (type, integer_zero_node);
10486 /* A - B -> A + (-B) if B is easily negatable. */
10487 if (negate_expr_p (arg1)
10488 && ((FLOAT_TYPE_P (type)
10489 /* Avoid this transformation if B is a positive REAL_CST. */
10490 && (TREE_CODE (arg1) != REAL_CST
10491 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10492 || INTEGRAL_TYPE_P (type)))
10493 return fold_build2 (PLUS_EXPR, type,
10494 fold_convert (type, arg0),
10495 fold_convert (type, negate_expr (arg1)));
10497 /* Try folding difference of addresses. */
10499 HOST_WIDE_INT diff;
10501 if ((TREE_CODE (arg0) == ADDR_EXPR
10502 || TREE_CODE (arg1) == ADDR_EXPR)
10503 && ptr_difference_const (arg0, arg1, &diff))
10504 return build_int_cst_type (type, diff);
10507 /* Fold &a[i] - &a[j] to i-j. */
10508 if (TREE_CODE (arg0) == ADDR_EXPR
10509 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10510 && TREE_CODE (arg1) == ADDR_EXPR
10511 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10513 tree aref0 = TREE_OPERAND (arg0, 0);
10514 tree aref1 = TREE_OPERAND (arg1, 0);
10515 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10516 TREE_OPERAND (aref1, 0), 0))
10518 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10519 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10520 tree esz = array_ref_element_size (aref0);
10521 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10522 return fold_build2 (MULT_EXPR, type, diff,
10523 fold_convert (type, esz));
10528 if (flag_unsafe_math_optimizations
10529 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10530 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10531 && (tem = distribute_real_division (code, type, arg0, arg1)))
10532 return tem;
10534 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10535 same or one. Make sure type is not saturating.
10536 fold_plusminus_mult_expr will re-associate. */
10537 if ((TREE_CODE (arg0) == MULT_EXPR
10538 || TREE_CODE (arg1) == MULT_EXPR)
10539 && !TYPE_SATURATING (type)
10540 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10542 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10543 if (tem)
10544 return tem;
10547 goto associate;
10549 case MULT_EXPR:
10550 /* (-A) * (-B) -> A * B */
10551 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10552 return fold_build2 (MULT_EXPR, type,
10553 fold_convert (type, TREE_OPERAND (arg0, 0)),
10554 fold_convert (type, negate_expr (arg1)));
10555 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10556 return fold_build2 (MULT_EXPR, type,
10557 fold_convert (type, negate_expr (arg0)),
10558 fold_convert (type, TREE_OPERAND (arg1, 0)));
10560 if (! FLOAT_TYPE_P (type))
10562 if (integer_zerop (arg1))
10563 return omit_one_operand (type, arg1, arg0);
10564 if (integer_onep (arg1))
10565 return non_lvalue (fold_convert (type, arg0));
10566 /* Transform x * -1 into -x. Make sure to do the negation
10567 on the original operand with conversions not stripped
10568 because we can only strip non-sign-changing conversions. */
10569 if (integer_all_onesp (arg1))
10570 return fold_convert (type, negate_expr (op0));
10571 /* Transform x * -C into -x * C if x is easily negatable. */
10572 if (TREE_CODE (arg1) == INTEGER_CST
10573 && tree_int_cst_sgn (arg1) == -1
10574 && negate_expr_p (arg0)
10575 && (tem = negate_expr (arg1)) != arg1
10576 && !TREE_OVERFLOW (tem))
10577 return fold_build2 (MULT_EXPR, type,
10578 fold_convert (type, negate_expr (arg0)), tem);
10580 /* (a * (1 << b)) is (a << b) */
10581 if (TREE_CODE (arg1) == LSHIFT_EXPR
10582 && integer_onep (TREE_OPERAND (arg1, 0)))
10583 return fold_build2 (LSHIFT_EXPR, type, op0,
10584 TREE_OPERAND (arg1, 1));
10585 if (TREE_CODE (arg0) == LSHIFT_EXPR
10586 && integer_onep (TREE_OPERAND (arg0, 0)))
10587 return fold_build2 (LSHIFT_EXPR, type, op1,
10588 TREE_OPERAND (arg0, 1));
10590 /* (A + A) * C -> A * 2 * C */
10591 if (TREE_CODE (arg0) == PLUS_EXPR
10592 && TREE_CODE (arg1) == INTEGER_CST
10593 && operand_equal_p (TREE_OPERAND (arg0, 0),
10594 TREE_OPERAND (arg0, 1), 0))
10595 return fold_build2 (MULT_EXPR, type,
10596 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10597 TREE_OPERAND (arg0, 1)),
10598 fold_build2 (MULT_EXPR, type,
10599 build_int_cst (type, 2) , arg1));
10601 strict_overflow_p = false;
10602 if (TREE_CODE (arg1) == INTEGER_CST
10603 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10604 &strict_overflow_p)))
10606 if (strict_overflow_p)
10607 fold_overflow_warning (("assuming signed overflow does not "
10608 "occur when simplifying "
10609 "multiplication"),
10610 WARN_STRICT_OVERFLOW_MISC);
10611 return fold_convert (type, tem);
10614 /* Optimize z * conj(z) for integer complex numbers. */
10615 if (TREE_CODE (arg0) == CONJ_EXPR
10616 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10617 return fold_mult_zconjz (type, arg1);
10618 if (TREE_CODE (arg1) == CONJ_EXPR
10619 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10620 return fold_mult_zconjz (type, arg0);
10622 else
10624 /* Maybe fold x * 0 to 0. The expressions aren't the same
10625 when x is NaN, since x * 0 is also NaN. Nor are they the
10626 same in modes with signed zeros, since multiplying a
10627 negative value by 0 gives -0, not +0. */
10628 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10629 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10630 && real_zerop (arg1))
10631 return omit_one_operand (type, arg1, arg0);
10632 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10633 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10634 && real_onep (arg1))
10635 return non_lvalue (fold_convert (type, arg0));
10637 /* Transform x * -1.0 into -x. */
10638 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10639 && real_minus_onep (arg1))
10640 return fold_convert (type, negate_expr (arg0));
10642 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10643 the result for floating point types due to rounding so it is applied
10644 only if -fassociative-math was specify. */
10645 if (flag_associative_math
10646 && TREE_CODE (arg0) == RDIV_EXPR
10647 && TREE_CODE (arg1) == REAL_CST
10648 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10650 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10651 arg1, 0);
10652 if (tem)
10653 return fold_build2 (RDIV_EXPR, type, tem,
10654 TREE_OPERAND (arg0, 1));
10657 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10658 if (operand_equal_p (arg0, arg1, 0))
10660 tree tem = fold_strip_sign_ops (arg0);
10661 if (tem != NULL_TREE)
10663 tem = fold_convert (type, tem);
10664 return fold_build2 (MULT_EXPR, type, tem, tem);
10668 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10669 This is not the same for NaNs or if signed zeros are
10670 involved. */
10671 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10672 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10673 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10674 && TREE_CODE (arg1) == COMPLEX_CST
10675 && real_zerop (TREE_REALPART (arg1)))
10677 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10678 if (real_onep (TREE_IMAGPART (arg1)))
10679 return fold_build2 (COMPLEX_EXPR, type,
10680 negate_expr (fold_build1 (IMAGPART_EXPR,
10681 rtype, arg0)),
10682 fold_build1 (REALPART_EXPR, rtype, arg0));
10683 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10684 return fold_build2 (COMPLEX_EXPR, type,
10685 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10686 negate_expr (fold_build1 (REALPART_EXPR,
10687 rtype, arg0)));
10690 /* Optimize z * conj(z) for floating point complex numbers.
10691 Guarded by flag_unsafe_math_optimizations as non-finite
10692 imaginary components don't produce scalar results. */
10693 if (flag_unsafe_math_optimizations
10694 && TREE_CODE (arg0) == CONJ_EXPR
10695 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10696 return fold_mult_zconjz (type, arg1);
10697 if (flag_unsafe_math_optimizations
10698 && TREE_CODE (arg1) == CONJ_EXPR
10699 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10700 return fold_mult_zconjz (type, arg0);
10702 if (flag_unsafe_math_optimizations)
10704 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10705 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10707 /* Optimizations of root(...)*root(...). */
10708 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10710 tree rootfn, arg;
10711 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10712 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10714 /* Optimize sqrt(x)*sqrt(x) as x. */
10715 if (BUILTIN_SQRT_P (fcode0)
10716 && operand_equal_p (arg00, arg10, 0)
10717 && ! HONOR_SNANS (TYPE_MODE (type)))
10718 return arg00;
10720 /* Optimize root(x)*root(y) as root(x*y). */
10721 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10722 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10723 return build_call_expr (rootfn, 1, arg);
10726 /* Optimize expN(x)*expN(y) as expN(x+y). */
10727 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10729 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10730 tree arg = fold_build2 (PLUS_EXPR, type,
10731 CALL_EXPR_ARG (arg0, 0),
10732 CALL_EXPR_ARG (arg1, 0));
10733 return build_call_expr (expfn, 1, arg);
10736 /* Optimizations of pow(...)*pow(...). */
10737 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10738 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10739 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10741 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10742 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10743 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10744 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10746 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10747 if (operand_equal_p (arg01, arg11, 0))
10749 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10750 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10751 return build_call_expr (powfn, 2, arg, arg01);
10754 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10755 if (operand_equal_p (arg00, arg10, 0))
10757 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10758 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10759 return build_call_expr (powfn, 2, arg00, arg);
10763 /* Optimize tan(x)*cos(x) as sin(x). */
10764 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10765 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10766 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10767 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10768 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10769 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10770 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10771 CALL_EXPR_ARG (arg1, 0), 0))
10773 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10775 if (sinfn != NULL_TREE)
10776 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10779 /* Optimize x*pow(x,c) as pow(x,c+1). */
10780 if (fcode1 == BUILT_IN_POW
10781 || fcode1 == BUILT_IN_POWF
10782 || fcode1 == BUILT_IN_POWL)
10784 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10785 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10786 if (TREE_CODE (arg11) == REAL_CST
10787 && !TREE_OVERFLOW (arg11)
10788 && operand_equal_p (arg0, arg10, 0))
10790 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10791 REAL_VALUE_TYPE c;
10792 tree arg;
10794 c = TREE_REAL_CST (arg11);
10795 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10796 arg = build_real (type, c);
10797 return build_call_expr (powfn, 2, arg0, arg);
10801 /* Optimize pow(x,c)*x as pow(x,c+1). */
10802 if (fcode0 == BUILT_IN_POW
10803 || fcode0 == BUILT_IN_POWF
10804 || fcode0 == BUILT_IN_POWL)
10806 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10807 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10808 if (TREE_CODE (arg01) == REAL_CST
10809 && !TREE_OVERFLOW (arg01)
10810 && operand_equal_p (arg1, arg00, 0))
10812 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10813 REAL_VALUE_TYPE c;
10814 tree arg;
10816 c = TREE_REAL_CST (arg01);
10817 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10818 arg = build_real (type, c);
10819 return build_call_expr (powfn, 2, arg1, arg);
10823 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10824 if (optimize_function_for_speed_p (cfun)
10825 && operand_equal_p (arg0, arg1, 0))
10827 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10829 if (powfn)
10831 tree arg = build_real (type, dconst2);
10832 return build_call_expr (powfn, 2, arg0, arg);
10837 goto associate;
10839 case BIT_IOR_EXPR:
10840 bit_ior:
10841 if (integer_all_onesp (arg1))
10842 return omit_one_operand (type, arg1, arg0);
10843 if (integer_zerop (arg1))
10844 return non_lvalue (fold_convert (type, arg0));
10845 if (operand_equal_p (arg0, arg1, 0))
10846 return non_lvalue (fold_convert (type, arg0));
10848 /* ~X | X is -1. */
10849 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10850 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10852 t1 = fold_convert (type, integer_zero_node);
10853 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10854 return omit_one_operand (type, t1, arg1);
10857 /* X | ~X is -1. */
10858 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10859 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10861 t1 = fold_convert (type, integer_zero_node);
10862 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10863 return omit_one_operand (type, t1, arg0);
10866 /* Canonicalize (X & C1) | C2. */
10867 if (TREE_CODE (arg0) == BIT_AND_EXPR
10868 && TREE_CODE (arg1) == INTEGER_CST
10869 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10871 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10872 int width = TYPE_PRECISION (type), w;
10873 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10874 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10875 hi2 = TREE_INT_CST_HIGH (arg1);
10876 lo2 = TREE_INT_CST_LOW (arg1);
10878 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10879 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10880 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10882 if (width > HOST_BITS_PER_WIDE_INT)
10884 mhi = (unsigned HOST_WIDE_INT) -1
10885 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10886 mlo = -1;
10888 else
10890 mhi = 0;
10891 mlo = (unsigned HOST_WIDE_INT) -1
10892 >> (HOST_BITS_PER_WIDE_INT - width);
10895 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10896 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10897 return fold_build2 (BIT_IOR_EXPR, type,
10898 TREE_OPERAND (arg0, 0), arg1);
10900 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10901 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10902 mode which allows further optimizations. */
10903 hi1 &= mhi;
10904 lo1 &= mlo;
10905 hi2 &= mhi;
10906 lo2 &= mlo;
10907 hi3 = hi1 & ~hi2;
10908 lo3 = lo1 & ~lo2;
10909 for (w = BITS_PER_UNIT;
10910 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10911 w <<= 1)
10913 unsigned HOST_WIDE_INT mask
10914 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10915 if (((lo1 | lo2) & mask) == mask
10916 && (lo1 & ~mask) == 0 && hi1 == 0)
10918 hi3 = 0;
10919 lo3 = mask;
10920 break;
10923 if (hi3 != hi1 || lo3 != lo1)
10924 return fold_build2 (BIT_IOR_EXPR, type,
10925 fold_build2 (BIT_AND_EXPR, type,
10926 TREE_OPERAND (arg0, 0),
10927 build_int_cst_wide (type,
10928 lo3, hi3)),
10929 arg1);
10932 /* (X & Y) | Y is (X, Y). */
10933 if (TREE_CODE (arg0) == BIT_AND_EXPR
10934 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10935 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10936 /* (X & Y) | X is (Y, X). */
10937 if (TREE_CODE (arg0) == BIT_AND_EXPR
10938 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10939 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10940 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10941 /* X | (X & Y) is (Y, X). */
10942 if (TREE_CODE (arg1) == BIT_AND_EXPR
10943 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10944 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10945 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10946 /* X | (Y & X) is (Y, X). */
10947 if (TREE_CODE (arg1) == BIT_AND_EXPR
10948 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10949 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10950 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10952 t1 = distribute_bit_expr (code, type, arg0, arg1);
10953 if (t1 != NULL_TREE)
10954 return t1;
10956 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10958 This results in more efficient code for machines without a NAND
10959 instruction. Combine will canonicalize to the first form
10960 which will allow use of NAND instructions provided by the
10961 backend if they exist. */
10962 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10963 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10965 return fold_build1 (BIT_NOT_EXPR, type,
10966 build2 (BIT_AND_EXPR, type,
10967 fold_convert (type,
10968 TREE_OPERAND (arg0, 0)),
10969 fold_convert (type,
10970 TREE_OPERAND (arg1, 0))));
10973 /* See if this can be simplified into a rotate first. If that
10974 is unsuccessful continue in the association code. */
10975 goto bit_rotate;
10977 case BIT_XOR_EXPR:
10978 if (integer_zerop (arg1))
10979 return non_lvalue (fold_convert (type, arg0));
10980 if (integer_all_onesp (arg1))
10981 return fold_build1 (BIT_NOT_EXPR, type, op0);
10982 if (operand_equal_p (arg0, arg1, 0))
10983 return omit_one_operand (type, integer_zero_node, arg0);
10985 /* ~X ^ X is -1. */
10986 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10987 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10989 t1 = fold_convert (type, integer_zero_node);
10990 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10991 return omit_one_operand (type, t1, arg1);
10994 /* X ^ ~X is -1. */
10995 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10996 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10998 t1 = fold_convert (type, integer_zero_node);
10999 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
11000 return omit_one_operand (type, t1, arg0);
11003 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11004 with a constant, and the two constants have no bits in common,
11005 we should treat this as a BIT_IOR_EXPR since this may produce more
11006 simplifications. */
11007 if (TREE_CODE (arg0) == BIT_AND_EXPR
11008 && TREE_CODE (arg1) == BIT_AND_EXPR
11009 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11010 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11011 && integer_zerop (const_binop (BIT_AND_EXPR,
11012 TREE_OPERAND (arg0, 1),
11013 TREE_OPERAND (arg1, 1), 0)))
11015 code = BIT_IOR_EXPR;
11016 goto bit_ior;
11019 /* (X | Y) ^ X -> Y & ~ X*/
11020 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11021 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11023 tree t2 = TREE_OPERAND (arg0, 1);
11024 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11025 arg1);
11026 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11027 fold_convert (type, t1));
11028 return t1;
11031 /* (Y | X) ^ X -> Y & ~ X*/
11032 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11033 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11035 tree t2 = TREE_OPERAND (arg0, 0);
11036 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11037 arg1);
11038 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11039 fold_convert (type, t1));
11040 return t1;
11043 /* X ^ (X | Y) -> Y & ~ X*/
11044 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11045 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11047 tree t2 = TREE_OPERAND (arg1, 1);
11048 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11049 arg0);
11050 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11051 fold_convert (type, t1));
11052 return t1;
11055 /* X ^ (Y | X) -> Y & ~ X*/
11056 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11057 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11059 tree t2 = TREE_OPERAND (arg1, 0);
11060 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11061 arg0);
11062 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11063 fold_convert (type, t1));
11064 return t1;
11067 /* Convert ~X ^ ~Y to X ^ Y. */
11068 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11069 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11070 return fold_build2 (code, type,
11071 fold_convert (type, TREE_OPERAND (arg0, 0)),
11072 fold_convert (type, TREE_OPERAND (arg1, 0)));
11074 /* Convert ~X ^ C to X ^ ~C. */
11075 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11076 && TREE_CODE (arg1) == INTEGER_CST)
11077 return fold_build2 (code, type,
11078 fold_convert (type, TREE_OPERAND (arg0, 0)),
11079 fold_build1 (BIT_NOT_EXPR, type, arg1));
11081 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11082 if (TREE_CODE (arg0) == BIT_AND_EXPR
11083 && integer_onep (TREE_OPERAND (arg0, 1))
11084 && integer_onep (arg1))
11085 return fold_build2 (EQ_EXPR, type, arg0,
11086 build_int_cst (TREE_TYPE (arg0), 0));
11088 /* Fold (X & Y) ^ Y as ~X & Y. */
11089 if (TREE_CODE (arg0) == BIT_AND_EXPR
11090 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11092 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11093 return fold_build2 (BIT_AND_EXPR, type,
11094 fold_build1 (BIT_NOT_EXPR, type, tem),
11095 fold_convert (type, arg1));
11097 /* Fold (X & Y) ^ X as ~Y & X. */
11098 if (TREE_CODE (arg0) == BIT_AND_EXPR
11099 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11100 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11102 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11103 return fold_build2 (BIT_AND_EXPR, type,
11104 fold_build1 (BIT_NOT_EXPR, type, tem),
11105 fold_convert (type, arg1));
11107 /* Fold X ^ (X & Y) as X & ~Y. */
11108 if (TREE_CODE (arg1) == BIT_AND_EXPR
11109 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11111 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11112 return fold_build2 (BIT_AND_EXPR, type,
11113 fold_convert (type, arg0),
11114 fold_build1 (BIT_NOT_EXPR, type, tem));
11116 /* Fold X ^ (Y & X) as ~Y & X. */
11117 if (TREE_CODE (arg1) == BIT_AND_EXPR
11118 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11119 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11121 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11122 return fold_build2 (BIT_AND_EXPR, type,
11123 fold_build1 (BIT_NOT_EXPR, type, tem),
11124 fold_convert (type, arg0));
11127 /* See if this can be simplified into a rotate first. If that
11128 is unsuccessful continue in the association code. */
11129 goto bit_rotate;
11131 case BIT_AND_EXPR:
11132 if (integer_all_onesp (arg1))
11133 return non_lvalue (fold_convert (type, arg0));
11134 if (integer_zerop (arg1))
11135 return omit_one_operand (type, arg1, arg0);
11136 if (operand_equal_p (arg0, arg1, 0))
11137 return non_lvalue (fold_convert (type, arg0));
11139 /* ~X & X is always zero. */
11140 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11141 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11142 return omit_one_operand (type, integer_zero_node, arg1);
11144 /* X & ~X is always zero. */
11145 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11146 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11147 return omit_one_operand (type, integer_zero_node, arg0);
11149 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11150 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11151 && TREE_CODE (arg1) == INTEGER_CST
11152 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11154 tree tmp1 = fold_convert (type, arg1);
11155 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11156 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11157 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11158 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11159 return fold_convert (type,
11160 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11163 /* (X | Y) & Y is (X, Y). */
11164 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11165 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11166 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11167 /* (X | Y) & X is (Y, X). */
11168 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11169 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11170 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11171 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11172 /* X & (X | Y) is (Y, X). */
11173 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11174 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11175 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11176 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11177 /* X & (Y | X) is (Y, X). */
11178 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11179 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11180 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11181 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11183 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11184 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11185 && integer_onep (TREE_OPERAND (arg0, 1))
11186 && integer_onep (arg1))
11188 tem = TREE_OPERAND (arg0, 0);
11189 return fold_build2 (EQ_EXPR, type,
11190 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11191 build_int_cst (TREE_TYPE (tem), 1)),
11192 build_int_cst (TREE_TYPE (tem), 0));
11194 /* Fold ~X & 1 as (X & 1) == 0. */
11195 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11196 && integer_onep (arg1))
11198 tem = TREE_OPERAND (arg0, 0);
11199 return fold_build2 (EQ_EXPR, type,
11200 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11201 build_int_cst (TREE_TYPE (tem), 1)),
11202 build_int_cst (TREE_TYPE (tem), 0));
11205 /* Fold (X ^ Y) & Y as ~X & Y. */
11206 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11207 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11209 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11210 return fold_build2 (BIT_AND_EXPR, type,
11211 fold_build1 (BIT_NOT_EXPR, type, tem),
11212 fold_convert (type, arg1));
11214 /* Fold (X ^ Y) & X as ~Y & X. */
11215 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11216 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11217 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11219 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11220 return fold_build2 (BIT_AND_EXPR, type,
11221 fold_build1 (BIT_NOT_EXPR, type, tem),
11222 fold_convert (type, arg1));
11224 /* Fold X & (X ^ Y) as X & ~Y. */
11225 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11226 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11228 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11229 return fold_build2 (BIT_AND_EXPR, type,
11230 fold_convert (type, arg0),
11231 fold_build1 (BIT_NOT_EXPR, type, tem));
11233 /* Fold X & (Y ^ X) as ~Y & X. */
11234 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11235 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11236 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11238 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11239 return fold_build2 (BIT_AND_EXPR, type,
11240 fold_build1 (BIT_NOT_EXPR, type, tem),
11241 fold_convert (type, arg0));
11244 t1 = distribute_bit_expr (code, type, arg0, arg1);
11245 if (t1 != NULL_TREE)
11246 return t1;
11247 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11248 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11249 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11251 unsigned int prec
11252 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11254 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11255 && (~TREE_INT_CST_LOW (arg1)
11256 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11257 return fold_convert (type, TREE_OPERAND (arg0, 0));
11260 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11262 This results in more efficient code for machines without a NOR
11263 instruction. Combine will canonicalize to the first form
11264 which will allow use of NOR instructions provided by the
11265 backend if they exist. */
11266 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11267 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11269 return fold_build1 (BIT_NOT_EXPR, type,
11270 build2 (BIT_IOR_EXPR, type,
11271 fold_convert (type,
11272 TREE_OPERAND (arg0, 0)),
11273 fold_convert (type,
11274 TREE_OPERAND (arg1, 0))));
11277 /* If arg0 is derived from the address of an object or function, we may
11278 be able to fold this expression using the object or function's
11279 alignment. */
11280 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11282 unsigned HOST_WIDE_INT modulus, residue;
11283 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11285 modulus = get_pointer_modulus_and_residue (arg0, &residue);
11287 /* This works because modulus is a power of 2. If this weren't the
11288 case, we'd have to replace it by its greatest power-of-2
11289 divisor: modulus & -modulus. */
11290 if (low < modulus)
11291 return build_int_cst (type, residue & low);
11294 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11295 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11296 if the new mask might be further optimized. */
11297 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11298 || TREE_CODE (arg0) == RSHIFT_EXPR)
11299 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11300 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11301 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11302 < TYPE_PRECISION (TREE_TYPE (arg0))
11303 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11304 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11306 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11307 unsigned HOST_WIDE_INT mask
11308 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11309 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11310 tree shift_type = TREE_TYPE (arg0);
11312 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11313 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11314 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11315 && TYPE_PRECISION (TREE_TYPE (arg0))
11316 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11318 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11319 tree arg00 = TREE_OPERAND (arg0, 0);
11320 /* See if more bits can be proven as zero because of
11321 zero extension. */
11322 if (TREE_CODE (arg00) == NOP_EXPR
11323 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11325 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11326 if (TYPE_PRECISION (inner_type)
11327 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11328 && TYPE_PRECISION (inner_type) < prec)
11330 prec = TYPE_PRECISION (inner_type);
11331 /* See if we can shorten the right shift. */
11332 if (shiftc < prec)
11333 shift_type = inner_type;
11336 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11337 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11338 zerobits <<= prec - shiftc;
11339 /* For arithmetic shift if sign bit could be set, zerobits
11340 can contain actually sign bits, so no transformation is
11341 possible, unless MASK masks them all away. In that
11342 case the shift needs to be converted into logical shift. */
11343 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11344 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11346 if ((mask & zerobits) == 0)
11347 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11348 else
11349 zerobits = 0;
11353 /* ((X << 16) & 0xff00) is (X, 0). */
11354 if ((mask & zerobits) == mask)
11355 return omit_one_operand (type, build_int_cst (type, 0), arg0);
11357 newmask = mask | zerobits;
11358 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11360 unsigned int prec;
11362 /* Only do the transformation if NEWMASK is some integer
11363 mode's mask. */
11364 for (prec = BITS_PER_UNIT;
11365 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11366 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11367 break;
11368 if (prec < HOST_BITS_PER_WIDE_INT
11369 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11371 tree newmaskt;
11373 if (shift_type != TREE_TYPE (arg0))
11375 tem = fold_build2 (TREE_CODE (arg0), shift_type,
11376 fold_convert (shift_type,
11377 TREE_OPERAND (arg0, 0)),
11378 TREE_OPERAND (arg0, 1));
11379 tem = fold_convert (type, tem);
11381 else
11382 tem = op0;
11383 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11384 if (!tree_int_cst_equal (newmaskt, arg1))
11385 return fold_build2 (BIT_AND_EXPR, type, tem, newmaskt);
11390 goto associate;
11392 case RDIV_EXPR:
11393 /* Don't touch a floating-point divide by zero unless the mode
11394 of the constant can represent infinity. */
11395 if (TREE_CODE (arg1) == REAL_CST
11396 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11397 && real_zerop (arg1))
11398 return NULL_TREE;
11400 /* Optimize A / A to 1.0 if we don't care about
11401 NaNs or Infinities. Skip the transformation
11402 for non-real operands. */
11403 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11404 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11405 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11406 && operand_equal_p (arg0, arg1, 0))
11408 tree r = build_real (TREE_TYPE (arg0), dconst1);
11410 return omit_two_operands (type, r, arg0, arg1);
11413 /* The complex version of the above A / A optimization. */
11414 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11415 && operand_equal_p (arg0, arg1, 0))
11417 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11418 if (! HONOR_NANS (TYPE_MODE (elem_type))
11419 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11421 tree r = build_real (elem_type, dconst1);
11422 /* omit_two_operands will call fold_convert for us. */
11423 return omit_two_operands (type, r, arg0, arg1);
11427 /* (-A) / (-B) -> A / B */
11428 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11429 return fold_build2 (RDIV_EXPR, type,
11430 TREE_OPERAND (arg0, 0),
11431 negate_expr (arg1));
11432 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11433 return fold_build2 (RDIV_EXPR, type,
11434 negate_expr (arg0),
11435 TREE_OPERAND (arg1, 0));
11437 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11438 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11439 && real_onep (arg1))
11440 return non_lvalue (fold_convert (type, arg0));
11442 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11443 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11444 && real_minus_onep (arg1))
11445 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11447 /* If ARG1 is a constant, we can convert this to a multiply by the
11448 reciprocal. This does not have the same rounding properties,
11449 so only do this if -freciprocal-math. We can actually
11450 always safely do it if ARG1 is a power of two, but it's hard to
11451 tell if it is or not in a portable manner. */
11452 if (TREE_CODE (arg1) == REAL_CST)
11454 if (flag_reciprocal_math
11455 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11456 arg1, 0)))
11457 return fold_build2 (MULT_EXPR, type, arg0, tem);
11458 /* Find the reciprocal if optimizing and the result is exact. */
11459 if (optimize)
11461 REAL_VALUE_TYPE r;
11462 r = TREE_REAL_CST (arg1);
11463 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11465 tem = build_real (type, r);
11466 return fold_build2 (MULT_EXPR, type,
11467 fold_convert (type, arg0), tem);
11471 /* Convert A/B/C to A/(B*C). */
11472 if (flag_reciprocal_math
11473 && TREE_CODE (arg0) == RDIV_EXPR)
11474 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11475 fold_build2 (MULT_EXPR, type,
11476 TREE_OPERAND (arg0, 1), arg1));
11478 /* Convert A/(B/C) to (A/B)*C. */
11479 if (flag_reciprocal_math
11480 && TREE_CODE (arg1) == RDIV_EXPR)
11481 return fold_build2 (MULT_EXPR, type,
11482 fold_build2 (RDIV_EXPR, type, arg0,
11483 TREE_OPERAND (arg1, 0)),
11484 TREE_OPERAND (arg1, 1));
11486 /* Convert C1/(X*C2) into (C1/C2)/X. */
11487 if (flag_reciprocal_math
11488 && TREE_CODE (arg1) == MULT_EXPR
11489 && TREE_CODE (arg0) == REAL_CST
11490 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11492 tree tem = const_binop (RDIV_EXPR, arg0,
11493 TREE_OPERAND (arg1, 1), 0);
11494 if (tem)
11495 return fold_build2 (RDIV_EXPR, type, tem,
11496 TREE_OPERAND (arg1, 0));
11499 if (flag_unsafe_math_optimizations)
11501 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11502 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11504 /* Optimize sin(x)/cos(x) as tan(x). */
11505 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11506 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11507 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11508 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11509 CALL_EXPR_ARG (arg1, 0), 0))
11511 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11513 if (tanfn != NULL_TREE)
11514 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11517 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11518 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11519 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11520 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11521 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11522 CALL_EXPR_ARG (arg1, 0), 0))
11524 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11526 if (tanfn != NULL_TREE)
11528 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11529 return fold_build2 (RDIV_EXPR, type,
11530 build_real (type, dconst1), tmp);
11534 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11535 NaNs or Infinities. */
11536 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11537 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11538 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11540 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11541 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11543 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11544 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11545 && operand_equal_p (arg00, arg01, 0))
11547 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11549 if (cosfn != NULL_TREE)
11550 return build_call_expr (cosfn, 1, arg00);
11554 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11555 NaNs or Infinities. */
11556 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11557 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11558 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11560 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11561 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11563 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11564 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11565 && operand_equal_p (arg00, arg01, 0))
11567 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11569 if (cosfn != NULL_TREE)
11571 tree tmp = build_call_expr (cosfn, 1, arg00);
11572 return fold_build2 (RDIV_EXPR, type,
11573 build_real (type, dconst1),
11574 tmp);
11579 /* Optimize pow(x,c)/x as pow(x,c-1). */
11580 if (fcode0 == BUILT_IN_POW
11581 || fcode0 == BUILT_IN_POWF
11582 || fcode0 == BUILT_IN_POWL)
11584 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11585 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11586 if (TREE_CODE (arg01) == REAL_CST
11587 && !TREE_OVERFLOW (arg01)
11588 && operand_equal_p (arg1, arg00, 0))
11590 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11591 REAL_VALUE_TYPE c;
11592 tree arg;
11594 c = TREE_REAL_CST (arg01);
11595 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11596 arg = build_real (type, c);
11597 return build_call_expr (powfn, 2, arg1, arg);
11601 /* Optimize a/root(b/c) into a*root(c/b). */
11602 if (BUILTIN_ROOT_P (fcode1))
11604 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11606 if (TREE_CODE (rootarg) == RDIV_EXPR)
11608 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11609 tree b = TREE_OPERAND (rootarg, 0);
11610 tree c = TREE_OPERAND (rootarg, 1);
11612 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11614 tmp = build_call_expr (rootfn, 1, tmp);
11615 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11619 /* Optimize x/expN(y) into x*expN(-y). */
11620 if (BUILTIN_EXPONENT_P (fcode1))
11622 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11623 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11624 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11625 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11628 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11629 if (fcode1 == BUILT_IN_POW
11630 || fcode1 == BUILT_IN_POWF
11631 || fcode1 == BUILT_IN_POWL)
11633 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11634 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11635 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11636 tree neg11 = fold_convert (type, negate_expr (arg11));
11637 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11638 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11641 return NULL_TREE;
11643 case TRUNC_DIV_EXPR:
11644 case FLOOR_DIV_EXPR:
11645 /* Simplify A / (B << N) where A and B are positive and B is
11646 a power of 2, to A >> (N + log2(B)). */
11647 strict_overflow_p = false;
11648 if (TREE_CODE (arg1) == LSHIFT_EXPR
11649 && (TYPE_UNSIGNED (type)
11650 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11652 tree sval = TREE_OPERAND (arg1, 0);
11653 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11655 tree sh_cnt = TREE_OPERAND (arg1, 1);
11656 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11658 if (strict_overflow_p)
11659 fold_overflow_warning (("assuming signed overflow does not "
11660 "occur when simplifying A / (B << N)"),
11661 WARN_STRICT_OVERFLOW_MISC);
11663 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11664 sh_cnt, build_int_cst (NULL_TREE, pow2));
11665 return fold_build2 (RSHIFT_EXPR, type,
11666 fold_convert (type, arg0), sh_cnt);
11670 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11671 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11672 if (INTEGRAL_TYPE_P (type)
11673 && TYPE_UNSIGNED (type)
11674 && code == FLOOR_DIV_EXPR)
11675 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11677 /* Fall thru */
11679 case ROUND_DIV_EXPR:
11680 case CEIL_DIV_EXPR:
11681 case EXACT_DIV_EXPR:
11682 if (integer_onep (arg1))
11683 return non_lvalue (fold_convert (type, arg0));
11684 if (integer_zerop (arg1))
11685 return NULL_TREE;
11686 /* X / -1 is -X. */
11687 if (!TYPE_UNSIGNED (type)
11688 && TREE_CODE (arg1) == INTEGER_CST
11689 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11690 && TREE_INT_CST_HIGH (arg1) == -1)
11691 return fold_convert (type, negate_expr (arg0));
11693 /* Convert -A / -B to A / B when the type is signed and overflow is
11694 undefined. */
11695 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11696 && TREE_CODE (arg0) == NEGATE_EXPR
11697 && negate_expr_p (arg1))
11699 if (INTEGRAL_TYPE_P (type))
11700 fold_overflow_warning (("assuming signed overflow does not occur "
11701 "when distributing negation across "
11702 "division"),
11703 WARN_STRICT_OVERFLOW_MISC);
11704 return fold_build2 (code, type,
11705 fold_convert (type, TREE_OPERAND (arg0, 0)),
11706 fold_convert (type, negate_expr (arg1)));
11708 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11709 && TREE_CODE (arg1) == NEGATE_EXPR
11710 && negate_expr_p (arg0))
11712 if (INTEGRAL_TYPE_P (type))
11713 fold_overflow_warning (("assuming signed overflow does not occur "
11714 "when distributing negation across "
11715 "division"),
11716 WARN_STRICT_OVERFLOW_MISC);
11717 return fold_build2 (code, type,
11718 fold_convert (type, negate_expr (arg0)),
11719 fold_convert (type, TREE_OPERAND (arg1, 0)));
11722 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11723 operation, EXACT_DIV_EXPR.
11725 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11726 At one time others generated faster code, it's not clear if they do
11727 after the last round to changes to the DIV code in expmed.c. */
11728 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11729 && multiple_of_p (type, arg0, arg1))
11730 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11732 strict_overflow_p = false;
11733 if (TREE_CODE (arg1) == INTEGER_CST
11734 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11735 &strict_overflow_p)))
11737 if (strict_overflow_p)
11738 fold_overflow_warning (("assuming signed overflow does not occur "
11739 "when simplifying division"),
11740 WARN_STRICT_OVERFLOW_MISC);
11741 return fold_convert (type, tem);
11744 return NULL_TREE;
11746 case CEIL_MOD_EXPR:
11747 case FLOOR_MOD_EXPR:
11748 case ROUND_MOD_EXPR:
11749 case TRUNC_MOD_EXPR:
11750 /* X % 1 is always zero, but be sure to preserve any side
11751 effects in X. */
11752 if (integer_onep (arg1))
11753 return omit_one_operand (type, integer_zero_node, arg0);
11755 /* X % 0, return X % 0 unchanged so that we can get the
11756 proper warnings and errors. */
11757 if (integer_zerop (arg1))
11758 return NULL_TREE;
11760 /* 0 % X is always zero, but be sure to preserve any side
11761 effects in X. Place this after checking for X == 0. */
11762 if (integer_zerop (arg0))
11763 return omit_one_operand (type, integer_zero_node, arg1);
11765 /* X % -1 is zero. */
11766 if (!TYPE_UNSIGNED (type)
11767 && TREE_CODE (arg1) == INTEGER_CST
11768 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11769 && TREE_INT_CST_HIGH (arg1) == -1)
11770 return omit_one_operand (type, integer_zero_node, arg0);
11772 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11773 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11774 strict_overflow_p = false;
11775 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11776 && (TYPE_UNSIGNED (type)
11777 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11779 tree c = arg1;
11780 /* Also optimize A % (C << N) where C is a power of 2,
11781 to A & ((C << N) - 1). */
11782 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11783 c = TREE_OPERAND (arg1, 0);
11785 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11787 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11788 build_int_cst (TREE_TYPE (arg1), 1));
11789 if (strict_overflow_p)
11790 fold_overflow_warning (("assuming signed overflow does not "
11791 "occur when simplifying "
11792 "X % (power of two)"),
11793 WARN_STRICT_OVERFLOW_MISC);
11794 return fold_build2 (BIT_AND_EXPR, type,
11795 fold_convert (type, arg0),
11796 fold_convert (type, mask));
11800 /* X % -C is the same as X % C. */
11801 if (code == TRUNC_MOD_EXPR
11802 && !TYPE_UNSIGNED (type)
11803 && TREE_CODE (arg1) == INTEGER_CST
11804 && !TREE_OVERFLOW (arg1)
11805 && TREE_INT_CST_HIGH (arg1) < 0
11806 && !TYPE_OVERFLOW_TRAPS (type)
11807 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11808 && !sign_bit_p (arg1, arg1))
11809 return fold_build2 (code, type, fold_convert (type, arg0),
11810 fold_convert (type, negate_expr (arg1)));
11812 /* X % -Y is the same as X % Y. */
11813 if (code == TRUNC_MOD_EXPR
11814 && !TYPE_UNSIGNED (type)
11815 && TREE_CODE (arg1) == NEGATE_EXPR
11816 && !TYPE_OVERFLOW_TRAPS (type))
11817 return fold_build2 (code, type, fold_convert (type, arg0),
11818 fold_convert (type, TREE_OPERAND (arg1, 0)));
11820 if (TREE_CODE (arg1) == INTEGER_CST
11821 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11822 &strict_overflow_p)))
11824 if (strict_overflow_p)
11825 fold_overflow_warning (("assuming signed overflow does not occur "
11826 "when simplifying modulus"),
11827 WARN_STRICT_OVERFLOW_MISC);
11828 return fold_convert (type, tem);
11831 return NULL_TREE;
11833 case LROTATE_EXPR:
11834 case RROTATE_EXPR:
11835 if (integer_all_onesp (arg0))
11836 return omit_one_operand (type, arg0, arg1);
11837 goto shift;
11839 case RSHIFT_EXPR:
11840 /* Optimize -1 >> x for arithmetic right shifts. */
11841 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11842 && tree_expr_nonnegative_p (arg1))
11843 return omit_one_operand (type, arg0, arg1);
11844 /* ... fall through ... */
11846 case LSHIFT_EXPR:
11847 shift:
11848 if (integer_zerop (arg1))
11849 return non_lvalue (fold_convert (type, arg0));
11850 if (integer_zerop (arg0))
11851 return omit_one_operand (type, arg0, arg1);
11853 /* Since negative shift count is not well-defined,
11854 don't try to compute it in the compiler. */
11855 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11856 return NULL_TREE;
11858 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11859 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11860 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11861 && host_integerp (TREE_OPERAND (arg0, 1), false)
11862 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11864 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11865 + TREE_INT_CST_LOW (arg1));
11867 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11868 being well defined. */
11869 if (low >= TYPE_PRECISION (type))
11871 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11872 low = low % TYPE_PRECISION (type);
11873 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11874 return omit_one_operand (type, build_int_cst (type, 0),
11875 TREE_OPERAND (arg0, 0));
11876 else
11877 low = TYPE_PRECISION (type) - 1;
11880 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11881 build_int_cst (type, low));
11884 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11885 into x & ((unsigned)-1 >> c) for unsigned types. */
11886 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11887 || (TYPE_UNSIGNED (type)
11888 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11889 && host_integerp (arg1, false)
11890 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11891 && host_integerp (TREE_OPERAND (arg0, 1), false)
11892 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11894 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11895 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11896 tree lshift;
11897 tree arg00;
11899 if (low0 == low1)
11901 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11903 lshift = build_int_cst (type, -1);
11904 lshift = int_const_binop (code, lshift, arg1, 0);
11906 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11910 /* Rewrite an LROTATE_EXPR by a constant into an
11911 RROTATE_EXPR by a new constant. */
11912 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11914 tree tem = build_int_cst (TREE_TYPE (arg1),
11915 TYPE_PRECISION (type));
11916 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11917 return fold_build2 (RROTATE_EXPR, type, op0, tem);
11920 /* If we have a rotate of a bit operation with the rotate count and
11921 the second operand of the bit operation both constant,
11922 permute the two operations. */
11923 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11924 && (TREE_CODE (arg0) == BIT_AND_EXPR
11925 || TREE_CODE (arg0) == BIT_IOR_EXPR
11926 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11927 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11928 return fold_build2 (TREE_CODE (arg0), type,
11929 fold_build2 (code, type,
11930 TREE_OPERAND (arg0, 0), arg1),
11931 fold_build2 (code, type,
11932 TREE_OPERAND (arg0, 1), arg1));
11934 /* Two consecutive rotates adding up to the precision of the
11935 type can be ignored. */
11936 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11937 && TREE_CODE (arg0) == RROTATE_EXPR
11938 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11939 && TREE_INT_CST_HIGH (arg1) == 0
11940 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11941 && ((TREE_INT_CST_LOW (arg1)
11942 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11943 == (unsigned int) TYPE_PRECISION (type)))
11944 return TREE_OPERAND (arg0, 0);
11946 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11947 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11948 if the latter can be further optimized. */
11949 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11950 && TREE_CODE (arg0) == BIT_AND_EXPR
11951 && TREE_CODE (arg1) == INTEGER_CST
11952 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11954 tree mask = fold_build2 (code, type,
11955 fold_convert (type, TREE_OPERAND (arg0, 1)),
11956 arg1);
11957 tree shift = fold_build2 (code, type,
11958 fold_convert (type, TREE_OPERAND (arg0, 0)),
11959 arg1);
11960 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11961 if (tem)
11962 return tem;
11965 return NULL_TREE;
11967 case MIN_EXPR:
11968 if (operand_equal_p (arg0, arg1, 0))
11969 return omit_one_operand (type, arg0, arg1);
11970 if (INTEGRAL_TYPE_P (type)
11971 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11972 return omit_one_operand (type, arg1, arg0);
11973 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11974 if (tem)
11975 return tem;
11976 goto associate;
11978 case MAX_EXPR:
11979 if (operand_equal_p (arg0, arg1, 0))
11980 return omit_one_operand (type, arg0, arg1);
11981 if (INTEGRAL_TYPE_P (type)
11982 && TYPE_MAX_VALUE (type)
11983 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11984 return omit_one_operand (type, arg1, arg0);
11985 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11986 if (tem)
11987 return tem;
11988 goto associate;
11990 case TRUTH_ANDIF_EXPR:
11991 /* Note that the operands of this must be ints
11992 and their values must be 0 or 1.
11993 ("true" is a fixed value perhaps depending on the language.) */
11994 /* If first arg is constant zero, return it. */
11995 if (integer_zerop (arg0))
11996 return fold_convert (type, arg0);
11997 case TRUTH_AND_EXPR:
11998 /* If either arg is constant true, drop it. */
11999 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12000 return non_lvalue (fold_convert (type, arg1));
12001 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12002 /* Preserve sequence points. */
12003 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12004 return non_lvalue (fold_convert (type, arg0));
12005 /* If second arg is constant zero, result is zero, but first arg
12006 must be evaluated. */
12007 if (integer_zerop (arg1))
12008 return omit_one_operand (type, arg1, arg0);
12009 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12010 case will be handled here. */
12011 if (integer_zerop (arg0))
12012 return omit_one_operand (type, arg0, arg1);
12014 /* !X && X is always false. */
12015 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12016 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12017 return omit_one_operand (type, integer_zero_node, arg1);
12018 /* X && !X is always false. */
12019 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12020 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12021 return omit_one_operand (type, integer_zero_node, arg0);
12023 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12024 means A >= Y && A != MAX, but in this case we know that
12025 A < X <= MAX. */
12027 if (!TREE_SIDE_EFFECTS (arg0)
12028 && !TREE_SIDE_EFFECTS (arg1))
12030 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12031 if (tem && !operand_equal_p (tem, arg0, 0))
12032 return fold_build2 (code, type, tem, arg1);
12034 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12035 if (tem && !operand_equal_p (tem, arg1, 0))
12036 return fold_build2 (code, type, arg0, tem);
12039 truth_andor:
12040 /* We only do these simplifications if we are optimizing. */
12041 if (!optimize)
12042 return NULL_TREE;
12044 /* Check for things like (A || B) && (A || C). We can convert this
12045 to A || (B && C). Note that either operator can be any of the four
12046 truth and/or operations and the transformation will still be
12047 valid. Also note that we only care about order for the
12048 ANDIF and ORIF operators. If B contains side effects, this
12049 might change the truth-value of A. */
12050 if (TREE_CODE (arg0) == TREE_CODE (arg1)
12051 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12052 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12053 || TREE_CODE (arg0) == TRUTH_AND_EXPR
12054 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12055 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12057 tree a00 = TREE_OPERAND (arg0, 0);
12058 tree a01 = TREE_OPERAND (arg0, 1);
12059 tree a10 = TREE_OPERAND (arg1, 0);
12060 tree a11 = TREE_OPERAND (arg1, 1);
12061 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12062 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12063 && (code == TRUTH_AND_EXPR
12064 || code == TRUTH_OR_EXPR));
12066 if (operand_equal_p (a00, a10, 0))
12067 return fold_build2 (TREE_CODE (arg0), type, a00,
12068 fold_build2 (code, type, a01, a11));
12069 else if (commutative && operand_equal_p (a00, a11, 0))
12070 return fold_build2 (TREE_CODE (arg0), type, a00,
12071 fold_build2 (code, type, a01, a10));
12072 else if (commutative && operand_equal_p (a01, a10, 0))
12073 return fold_build2 (TREE_CODE (arg0), type, a01,
12074 fold_build2 (code, type, a00, a11));
12076 /* This case if tricky because we must either have commutative
12077 operators or else A10 must not have side-effects. */
12079 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12080 && operand_equal_p (a01, a11, 0))
12081 return fold_build2 (TREE_CODE (arg0), type,
12082 fold_build2 (code, type, a00, a10),
12083 a01);
12086 /* See if we can build a range comparison. */
12087 if (0 != (tem = fold_range_test (code, type, op0, op1)))
12088 return tem;
12090 /* Check for the possibility of merging component references. If our
12091 lhs is another similar operation, try to merge its rhs with our
12092 rhs. Then try to merge our lhs and rhs. */
12093 if (TREE_CODE (arg0) == code
12094 && 0 != (tem = fold_truthop (code, type,
12095 TREE_OPERAND (arg0, 1), arg1)))
12096 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12098 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12099 return tem;
12101 return NULL_TREE;
12103 case TRUTH_ORIF_EXPR:
12104 /* Note that the operands of this must be ints
12105 and their values must be 0 or true.
12106 ("true" is a fixed value perhaps depending on the language.) */
12107 /* If first arg is constant true, return it. */
12108 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12109 return fold_convert (type, arg0);
12110 case TRUTH_OR_EXPR:
12111 /* If either arg is constant zero, drop it. */
12112 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12113 return non_lvalue (fold_convert (type, arg1));
12114 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12115 /* Preserve sequence points. */
12116 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12117 return non_lvalue (fold_convert (type, arg0));
12118 /* If second arg is constant true, result is true, but we must
12119 evaluate first arg. */
12120 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12121 return omit_one_operand (type, arg1, arg0);
12122 /* Likewise for first arg, but note this only occurs here for
12123 TRUTH_OR_EXPR. */
12124 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12125 return omit_one_operand (type, arg0, arg1);
12127 /* !X || X is always true. */
12128 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12129 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12130 return omit_one_operand (type, integer_one_node, arg1);
12131 /* X || !X is always true. */
12132 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12133 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12134 return omit_one_operand (type, integer_one_node, arg0);
12136 goto truth_andor;
12138 case TRUTH_XOR_EXPR:
12139 /* If the second arg is constant zero, drop it. */
12140 if (integer_zerop (arg1))
12141 return non_lvalue (fold_convert (type, arg0));
12142 /* If the second arg is constant true, this is a logical inversion. */
12143 if (integer_onep (arg1))
12145 /* Only call invert_truthvalue if operand is a truth value. */
12146 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12147 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12148 else
12149 tem = invert_truthvalue (arg0);
12150 return non_lvalue (fold_convert (type, tem));
12152 /* Identical arguments cancel to zero. */
12153 if (operand_equal_p (arg0, arg1, 0))
12154 return omit_one_operand (type, integer_zero_node, arg0);
12156 /* !X ^ X is always true. */
12157 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12158 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12159 return omit_one_operand (type, integer_one_node, arg1);
12161 /* X ^ !X is always true. */
12162 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12163 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12164 return omit_one_operand (type, integer_one_node, arg0);
12166 return NULL_TREE;
12168 case EQ_EXPR:
12169 case NE_EXPR:
12170 tem = fold_comparison (code, type, op0, op1);
12171 if (tem != NULL_TREE)
12172 return tem;
12174 /* bool_var != 0 becomes bool_var. */
12175 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12176 && code == NE_EXPR)
12177 return non_lvalue (fold_convert (type, arg0));
12179 /* bool_var == 1 becomes bool_var. */
12180 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12181 && code == EQ_EXPR)
12182 return non_lvalue (fold_convert (type, arg0));
12184 /* bool_var != 1 becomes !bool_var. */
12185 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12186 && code == NE_EXPR)
12187 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12189 /* bool_var == 0 becomes !bool_var. */
12190 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12191 && code == EQ_EXPR)
12192 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12194 /* If this is an equality comparison of the address of two non-weak,
12195 unaliased symbols neither of which are extern (since we do not
12196 have access to attributes for externs), then we know the result. */
12197 if (TREE_CODE (arg0) == ADDR_EXPR
12198 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12199 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12200 && ! lookup_attribute ("alias",
12201 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12202 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12203 && TREE_CODE (arg1) == ADDR_EXPR
12204 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12205 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12206 && ! lookup_attribute ("alias",
12207 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12208 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12210 /* We know that we're looking at the address of two
12211 non-weak, unaliased, static _DECL nodes.
12213 It is both wasteful and incorrect to call operand_equal_p
12214 to compare the two ADDR_EXPR nodes. It is wasteful in that
12215 all we need to do is test pointer equality for the arguments
12216 to the two ADDR_EXPR nodes. It is incorrect to use
12217 operand_equal_p as that function is NOT equivalent to a
12218 C equality test. It can in fact return false for two
12219 objects which would test as equal using the C equality
12220 operator. */
12221 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12222 return constant_boolean_node (equal
12223 ? code == EQ_EXPR : code != EQ_EXPR,
12224 type);
12227 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12228 a MINUS_EXPR of a constant, we can convert it into a comparison with
12229 a revised constant as long as no overflow occurs. */
12230 if (TREE_CODE (arg1) == INTEGER_CST
12231 && (TREE_CODE (arg0) == PLUS_EXPR
12232 || TREE_CODE (arg0) == MINUS_EXPR)
12233 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12234 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12235 ? MINUS_EXPR : PLUS_EXPR,
12236 fold_convert (TREE_TYPE (arg0), arg1),
12237 TREE_OPERAND (arg0, 1), 0))
12238 && !TREE_OVERFLOW (tem))
12239 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12241 /* Similarly for a NEGATE_EXPR. */
12242 if (TREE_CODE (arg0) == NEGATE_EXPR
12243 && TREE_CODE (arg1) == INTEGER_CST
12244 && 0 != (tem = negate_expr (arg1))
12245 && TREE_CODE (tem) == INTEGER_CST
12246 && !TREE_OVERFLOW (tem))
12247 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12249 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12250 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12251 && TREE_CODE (arg1) == INTEGER_CST
12252 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12253 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12254 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12255 fold_convert (TREE_TYPE (arg0), arg1),
12256 TREE_OPERAND (arg0, 1)));
12258 /* Transform comparisons of the form X +- C CMP X. */
12259 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12260 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12261 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12262 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12263 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12265 tree cst = TREE_OPERAND (arg0, 1);
12267 if (code == EQ_EXPR
12268 && !integer_zerop (cst))
12269 return omit_two_operands (type, boolean_false_node,
12270 TREE_OPERAND (arg0, 0), arg1);
12271 else
12272 return omit_two_operands (type, boolean_true_node,
12273 TREE_OPERAND (arg0, 0), arg1);
12276 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12277 for !=. Don't do this for ordered comparisons due to overflow. */
12278 if (TREE_CODE (arg0) == MINUS_EXPR
12279 && integer_zerop (arg1))
12280 return fold_build2 (code, type,
12281 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12283 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12284 if (TREE_CODE (arg0) == ABS_EXPR
12285 && (integer_zerop (arg1) || real_zerop (arg1)))
12286 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12288 /* If this is an EQ or NE comparison with zero and ARG0 is
12289 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12290 two operations, but the latter can be done in one less insn
12291 on machines that have only two-operand insns or on which a
12292 constant cannot be the first operand. */
12293 if (TREE_CODE (arg0) == BIT_AND_EXPR
12294 && integer_zerop (arg1))
12296 tree arg00 = TREE_OPERAND (arg0, 0);
12297 tree arg01 = TREE_OPERAND (arg0, 1);
12298 if (TREE_CODE (arg00) == LSHIFT_EXPR
12299 && integer_onep (TREE_OPERAND (arg00, 0)))
12301 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12302 arg01, TREE_OPERAND (arg00, 1));
12303 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12304 build_int_cst (TREE_TYPE (arg0), 1));
12305 return fold_build2 (code, type,
12306 fold_convert (TREE_TYPE (arg1), tem), arg1);
12308 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12309 && integer_onep (TREE_OPERAND (arg01, 0)))
12311 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12312 arg00, TREE_OPERAND (arg01, 1));
12313 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12314 build_int_cst (TREE_TYPE (arg0), 1));
12315 return fold_build2 (code, type,
12316 fold_convert (TREE_TYPE (arg1), tem), arg1);
12320 /* If this is an NE or EQ comparison of zero against the result of a
12321 signed MOD operation whose second operand is a power of 2, make
12322 the MOD operation unsigned since it is simpler and equivalent. */
12323 if (integer_zerop (arg1)
12324 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12325 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12326 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12327 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12328 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12329 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12331 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12332 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12333 fold_convert (newtype,
12334 TREE_OPERAND (arg0, 0)),
12335 fold_convert (newtype,
12336 TREE_OPERAND (arg0, 1)));
12338 return fold_build2 (code, type, newmod,
12339 fold_convert (newtype, arg1));
12342 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12343 C1 is a valid shift constant, and C2 is a power of two, i.e.
12344 a single bit. */
12345 if (TREE_CODE (arg0) == BIT_AND_EXPR
12346 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12347 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12348 == INTEGER_CST
12349 && integer_pow2p (TREE_OPERAND (arg0, 1))
12350 && integer_zerop (arg1))
12352 tree itype = TREE_TYPE (arg0);
12353 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12354 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12356 /* Check for a valid shift count. */
12357 if (TREE_INT_CST_HIGH (arg001) == 0
12358 && TREE_INT_CST_LOW (arg001) < prec)
12360 tree arg01 = TREE_OPERAND (arg0, 1);
12361 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12362 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12363 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12364 can be rewritten as (X & (C2 << C1)) != 0. */
12365 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12367 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12368 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12369 return fold_build2 (code, type, tem, arg1);
12371 /* Otherwise, for signed (arithmetic) shifts,
12372 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12373 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12374 else if (!TYPE_UNSIGNED (itype))
12375 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12376 arg000, build_int_cst (itype, 0));
12377 /* Otherwise, of unsigned (logical) shifts,
12378 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12379 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12380 else
12381 return omit_one_operand (type,
12382 code == EQ_EXPR ? integer_one_node
12383 : integer_zero_node,
12384 arg000);
12388 /* If this is an NE comparison of zero with an AND of one, remove the
12389 comparison since the AND will give the correct value. */
12390 if (code == NE_EXPR
12391 && integer_zerop (arg1)
12392 && TREE_CODE (arg0) == BIT_AND_EXPR
12393 && integer_onep (TREE_OPERAND (arg0, 1)))
12394 return fold_convert (type, arg0);
12396 /* If we have (A & C) == C where C is a power of 2, convert this into
12397 (A & C) != 0. Similarly for NE_EXPR. */
12398 if (TREE_CODE (arg0) == BIT_AND_EXPR
12399 && integer_pow2p (TREE_OPERAND (arg0, 1))
12400 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12401 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12402 arg0, fold_convert (TREE_TYPE (arg0),
12403 integer_zero_node));
12405 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12406 bit, then fold the expression into A < 0 or A >= 0. */
12407 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12408 if (tem)
12409 return tem;
12411 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12412 Similarly for NE_EXPR. */
12413 if (TREE_CODE (arg0) == BIT_AND_EXPR
12414 && TREE_CODE (arg1) == INTEGER_CST
12415 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12417 tree notc = fold_build1 (BIT_NOT_EXPR,
12418 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12419 TREE_OPERAND (arg0, 1));
12420 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12421 arg1, notc);
12422 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12423 if (integer_nonzerop (dandnotc))
12424 return omit_one_operand (type, rslt, arg0);
12427 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12428 Similarly for NE_EXPR. */
12429 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12430 && TREE_CODE (arg1) == INTEGER_CST
12431 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12433 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12434 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12435 TREE_OPERAND (arg0, 1), notd);
12436 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12437 if (integer_nonzerop (candnotd))
12438 return omit_one_operand (type, rslt, arg0);
12441 /* If this is a comparison of a field, we may be able to simplify it. */
12442 if ((TREE_CODE (arg0) == COMPONENT_REF
12443 || TREE_CODE (arg0) == BIT_FIELD_REF)
12444 /* Handle the constant case even without -O
12445 to make sure the warnings are given. */
12446 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12448 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12449 if (t1)
12450 return t1;
12453 /* Optimize comparisons of strlen vs zero to a compare of the
12454 first character of the string vs zero. To wit,
12455 strlen(ptr) == 0 => *ptr == 0
12456 strlen(ptr) != 0 => *ptr != 0
12457 Other cases should reduce to one of these two (or a constant)
12458 due to the return value of strlen being unsigned. */
12459 if (TREE_CODE (arg0) == CALL_EXPR
12460 && integer_zerop (arg1))
12462 tree fndecl = get_callee_fndecl (arg0);
12464 if (fndecl
12465 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12466 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12467 && call_expr_nargs (arg0) == 1
12468 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12470 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12471 return fold_build2 (code, type, iref,
12472 build_int_cst (TREE_TYPE (iref), 0));
12476 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12477 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12478 if (TREE_CODE (arg0) == RSHIFT_EXPR
12479 && integer_zerop (arg1)
12480 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12482 tree arg00 = TREE_OPERAND (arg0, 0);
12483 tree arg01 = TREE_OPERAND (arg0, 1);
12484 tree itype = TREE_TYPE (arg00);
12485 if (TREE_INT_CST_HIGH (arg01) == 0
12486 && TREE_INT_CST_LOW (arg01)
12487 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12489 if (TYPE_UNSIGNED (itype))
12491 itype = signed_type_for (itype);
12492 arg00 = fold_convert (itype, arg00);
12494 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12495 type, arg00, build_int_cst (itype, 0));
12499 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12500 if (integer_zerop (arg1)
12501 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12502 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12503 TREE_OPERAND (arg0, 1));
12505 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12506 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12507 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12508 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12509 build_int_cst (TREE_TYPE (arg1), 0));
12510 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12511 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12512 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12513 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12514 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12515 build_int_cst (TREE_TYPE (arg1), 0));
12517 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12518 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12519 && TREE_CODE (arg1) == INTEGER_CST
12520 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12521 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12522 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12523 TREE_OPERAND (arg0, 1), arg1));
12525 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12526 (X & C) == 0 when C is a single bit. */
12527 if (TREE_CODE (arg0) == BIT_AND_EXPR
12528 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12529 && integer_zerop (arg1)
12530 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12532 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12533 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12534 TREE_OPERAND (arg0, 1));
12535 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12536 type, tem, arg1);
12539 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12540 constant C is a power of two, i.e. a single bit. */
12541 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12542 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12543 && integer_zerop (arg1)
12544 && integer_pow2p (TREE_OPERAND (arg0, 1))
12545 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12546 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12548 tree arg00 = TREE_OPERAND (arg0, 0);
12549 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12550 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12553 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12554 when is C is a power of two, i.e. a single bit. */
12555 if (TREE_CODE (arg0) == BIT_AND_EXPR
12556 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12557 && integer_zerop (arg1)
12558 && integer_pow2p (TREE_OPERAND (arg0, 1))
12559 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12560 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12562 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12563 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12564 arg000, TREE_OPERAND (arg0, 1));
12565 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12566 tem, build_int_cst (TREE_TYPE (tem), 0));
12569 if (integer_zerop (arg1)
12570 && tree_expr_nonzero_p (arg0))
12572 tree res = constant_boolean_node (code==NE_EXPR, type);
12573 return omit_one_operand (type, res, arg0);
12576 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12577 if (TREE_CODE (arg0) == NEGATE_EXPR
12578 && TREE_CODE (arg1) == NEGATE_EXPR)
12579 return fold_build2 (code, type,
12580 TREE_OPERAND (arg0, 0),
12581 TREE_OPERAND (arg1, 0));
12583 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12584 if (TREE_CODE (arg0) == BIT_AND_EXPR
12585 && TREE_CODE (arg1) == BIT_AND_EXPR)
12587 tree arg00 = TREE_OPERAND (arg0, 0);
12588 tree arg01 = TREE_OPERAND (arg0, 1);
12589 tree arg10 = TREE_OPERAND (arg1, 0);
12590 tree arg11 = TREE_OPERAND (arg1, 1);
12591 tree itype = TREE_TYPE (arg0);
12593 if (operand_equal_p (arg01, arg11, 0))
12594 return fold_build2 (code, type,
12595 fold_build2 (BIT_AND_EXPR, itype,
12596 fold_build2 (BIT_XOR_EXPR, itype,
12597 arg00, arg10),
12598 arg01),
12599 build_int_cst (itype, 0));
12601 if (operand_equal_p (arg01, arg10, 0))
12602 return fold_build2 (code, type,
12603 fold_build2 (BIT_AND_EXPR, itype,
12604 fold_build2 (BIT_XOR_EXPR, itype,
12605 arg00, arg11),
12606 arg01),
12607 build_int_cst (itype, 0));
12609 if (operand_equal_p (arg00, arg11, 0))
12610 return fold_build2 (code, type,
12611 fold_build2 (BIT_AND_EXPR, itype,
12612 fold_build2 (BIT_XOR_EXPR, itype,
12613 arg01, arg10),
12614 arg00),
12615 build_int_cst (itype, 0));
12617 if (operand_equal_p (arg00, arg10, 0))
12618 return fold_build2 (code, type,
12619 fold_build2 (BIT_AND_EXPR, itype,
12620 fold_build2 (BIT_XOR_EXPR, itype,
12621 arg01, arg11),
12622 arg00),
12623 build_int_cst (itype, 0));
12626 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12627 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12629 tree arg00 = TREE_OPERAND (arg0, 0);
12630 tree arg01 = TREE_OPERAND (arg0, 1);
12631 tree arg10 = TREE_OPERAND (arg1, 0);
12632 tree arg11 = TREE_OPERAND (arg1, 1);
12633 tree itype = TREE_TYPE (arg0);
12635 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12636 operand_equal_p guarantees no side-effects so we don't need
12637 to use omit_one_operand on Z. */
12638 if (operand_equal_p (arg01, arg11, 0))
12639 return fold_build2 (code, type, arg00, arg10);
12640 if (operand_equal_p (arg01, arg10, 0))
12641 return fold_build2 (code, type, arg00, arg11);
12642 if (operand_equal_p (arg00, arg11, 0))
12643 return fold_build2 (code, type, arg01, arg10);
12644 if (operand_equal_p (arg00, arg10, 0))
12645 return fold_build2 (code, type, arg01, arg11);
12647 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12648 if (TREE_CODE (arg01) == INTEGER_CST
12649 && TREE_CODE (arg11) == INTEGER_CST)
12650 return fold_build2 (code, type,
12651 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12652 fold_build2 (BIT_XOR_EXPR, itype,
12653 arg01, arg11)),
12654 arg10);
12657 /* Attempt to simplify equality/inequality comparisons of complex
12658 values. Only lower the comparison if the result is known or
12659 can be simplified to a single scalar comparison. */
12660 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12661 || TREE_CODE (arg0) == COMPLEX_CST)
12662 && (TREE_CODE (arg1) == COMPLEX_EXPR
12663 || TREE_CODE (arg1) == COMPLEX_CST))
12665 tree real0, imag0, real1, imag1;
12666 tree rcond, icond;
12668 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12670 real0 = TREE_OPERAND (arg0, 0);
12671 imag0 = TREE_OPERAND (arg0, 1);
12673 else
12675 real0 = TREE_REALPART (arg0);
12676 imag0 = TREE_IMAGPART (arg0);
12679 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12681 real1 = TREE_OPERAND (arg1, 0);
12682 imag1 = TREE_OPERAND (arg1, 1);
12684 else
12686 real1 = TREE_REALPART (arg1);
12687 imag1 = TREE_IMAGPART (arg1);
12690 rcond = fold_binary (code, type, real0, real1);
12691 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12693 if (integer_zerop (rcond))
12695 if (code == EQ_EXPR)
12696 return omit_two_operands (type, boolean_false_node,
12697 imag0, imag1);
12698 return fold_build2 (NE_EXPR, type, imag0, imag1);
12700 else
12702 if (code == NE_EXPR)
12703 return omit_two_operands (type, boolean_true_node,
12704 imag0, imag1);
12705 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12709 icond = fold_binary (code, type, imag0, imag1);
12710 if (icond && TREE_CODE (icond) == INTEGER_CST)
12712 if (integer_zerop (icond))
12714 if (code == EQ_EXPR)
12715 return omit_two_operands (type, boolean_false_node,
12716 real0, real1);
12717 return fold_build2 (NE_EXPR, type, real0, real1);
12719 else
12721 if (code == NE_EXPR)
12722 return omit_two_operands (type, boolean_true_node,
12723 real0, real1);
12724 return fold_build2 (EQ_EXPR, type, real0, real1);
12729 return NULL_TREE;
12731 case LT_EXPR:
12732 case GT_EXPR:
12733 case LE_EXPR:
12734 case GE_EXPR:
12735 tem = fold_comparison (code, type, op0, op1);
12736 if (tem != NULL_TREE)
12737 return tem;
12739 /* Transform comparisons of the form X +- C CMP X. */
12740 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12741 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12742 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12743 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12744 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12745 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12747 tree arg01 = TREE_OPERAND (arg0, 1);
12748 enum tree_code code0 = TREE_CODE (arg0);
12749 int is_positive;
12751 if (TREE_CODE (arg01) == REAL_CST)
12752 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12753 else
12754 is_positive = tree_int_cst_sgn (arg01);
12756 /* (X - c) > X becomes false. */
12757 if (code == GT_EXPR
12758 && ((code0 == MINUS_EXPR && is_positive >= 0)
12759 || (code0 == PLUS_EXPR && is_positive <= 0)))
12761 if (TREE_CODE (arg01) == INTEGER_CST
12762 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12763 fold_overflow_warning (("assuming signed overflow does not "
12764 "occur when assuming that (X - c) > X "
12765 "is always false"),
12766 WARN_STRICT_OVERFLOW_ALL);
12767 return constant_boolean_node (0, type);
12770 /* Likewise (X + c) < X becomes false. */
12771 if (code == LT_EXPR
12772 && ((code0 == PLUS_EXPR && is_positive >= 0)
12773 || (code0 == MINUS_EXPR && is_positive <= 0)))
12775 if (TREE_CODE (arg01) == INTEGER_CST
12776 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12777 fold_overflow_warning (("assuming signed overflow does not "
12778 "occur when assuming that "
12779 "(X + c) < X is always false"),
12780 WARN_STRICT_OVERFLOW_ALL);
12781 return constant_boolean_node (0, type);
12784 /* Convert (X - c) <= X to true. */
12785 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12786 && code == LE_EXPR
12787 && ((code0 == MINUS_EXPR && is_positive >= 0)
12788 || (code0 == PLUS_EXPR && is_positive <= 0)))
12790 if (TREE_CODE (arg01) == INTEGER_CST
12791 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12792 fold_overflow_warning (("assuming signed overflow does not "
12793 "occur when assuming that "
12794 "(X - c) <= X is always true"),
12795 WARN_STRICT_OVERFLOW_ALL);
12796 return constant_boolean_node (1, type);
12799 /* Convert (X + c) >= X to true. */
12800 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12801 && code == GE_EXPR
12802 && ((code0 == PLUS_EXPR && is_positive >= 0)
12803 || (code0 == MINUS_EXPR && is_positive <= 0)))
12805 if (TREE_CODE (arg01) == INTEGER_CST
12806 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12807 fold_overflow_warning (("assuming signed overflow does not "
12808 "occur when assuming that "
12809 "(X + c) >= X is always true"),
12810 WARN_STRICT_OVERFLOW_ALL);
12811 return constant_boolean_node (1, type);
12814 if (TREE_CODE (arg01) == INTEGER_CST)
12816 /* Convert X + c > X and X - c < X to true for integers. */
12817 if (code == GT_EXPR
12818 && ((code0 == PLUS_EXPR && is_positive > 0)
12819 || (code0 == MINUS_EXPR && is_positive < 0)))
12821 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12822 fold_overflow_warning (("assuming signed overflow does "
12823 "not occur when assuming that "
12824 "(X + c) > X is always true"),
12825 WARN_STRICT_OVERFLOW_ALL);
12826 return constant_boolean_node (1, type);
12829 if (code == LT_EXPR
12830 && ((code0 == MINUS_EXPR && is_positive > 0)
12831 || (code0 == PLUS_EXPR && is_positive < 0)))
12833 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12834 fold_overflow_warning (("assuming signed overflow does "
12835 "not occur when assuming that "
12836 "(X - c) < X is always true"),
12837 WARN_STRICT_OVERFLOW_ALL);
12838 return constant_boolean_node (1, type);
12841 /* Convert X + c <= X and X - c >= X to false for integers. */
12842 if (code == LE_EXPR
12843 && ((code0 == PLUS_EXPR && is_positive > 0)
12844 || (code0 == MINUS_EXPR && is_positive < 0)))
12846 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12847 fold_overflow_warning (("assuming signed overflow does "
12848 "not occur when assuming that "
12849 "(X + c) <= X is always false"),
12850 WARN_STRICT_OVERFLOW_ALL);
12851 return constant_boolean_node (0, type);
12854 if (code == GE_EXPR
12855 && ((code0 == MINUS_EXPR && is_positive > 0)
12856 || (code0 == PLUS_EXPR && is_positive < 0)))
12858 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12859 fold_overflow_warning (("assuming signed overflow does "
12860 "not occur when assuming that "
12861 "(X - c) >= X is always false"),
12862 WARN_STRICT_OVERFLOW_ALL);
12863 return constant_boolean_node (0, type);
12868 /* Comparisons with the highest or lowest possible integer of
12869 the specified precision will have known values. */
12871 tree arg1_type = TREE_TYPE (arg1);
12872 unsigned int width = TYPE_PRECISION (arg1_type);
12874 if (TREE_CODE (arg1) == INTEGER_CST
12875 && width <= 2 * HOST_BITS_PER_WIDE_INT
12876 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12878 HOST_WIDE_INT signed_max_hi;
12879 unsigned HOST_WIDE_INT signed_max_lo;
12880 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12882 if (width <= HOST_BITS_PER_WIDE_INT)
12884 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12885 - 1;
12886 signed_max_hi = 0;
12887 max_hi = 0;
12889 if (TYPE_UNSIGNED (arg1_type))
12891 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12892 min_lo = 0;
12893 min_hi = 0;
12895 else
12897 max_lo = signed_max_lo;
12898 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12899 min_hi = -1;
12902 else
12904 width -= HOST_BITS_PER_WIDE_INT;
12905 signed_max_lo = -1;
12906 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12907 - 1;
12908 max_lo = -1;
12909 min_lo = 0;
12911 if (TYPE_UNSIGNED (arg1_type))
12913 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12914 min_hi = 0;
12916 else
12918 max_hi = signed_max_hi;
12919 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12923 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12924 && TREE_INT_CST_LOW (arg1) == max_lo)
12925 switch (code)
12927 case GT_EXPR:
12928 return omit_one_operand (type, integer_zero_node, arg0);
12930 case GE_EXPR:
12931 return fold_build2 (EQ_EXPR, type, op0, op1);
12933 case LE_EXPR:
12934 return omit_one_operand (type, integer_one_node, arg0);
12936 case LT_EXPR:
12937 return fold_build2 (NE_EXPR, type, op0, op1);
12939 /* The GE_EXPR and LT_EXPR cases above are not normally
12940 reached because of previous transformations. */
12942 default:
12943 break;
12945 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12946 == max_hi
12947 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12948 switch (code)
12950 case GT_EXPR:
12951 arg1 = const_binop (PLUS_EXPR, arg1,
12952 build_int_cst (TREE_TYPE (arg1), 1), 0);
12953 return fold_build2 (EQ_EXPR, type,
12954 fold_convert (TREE_TYPE (arg1), arg0),
12955 arg1);
12956 case LE_EXPR:
12957 arg1 = const_binop (PLUS_EXPR, arg1,
12958 build_int_cst (TREE_TYPE (arg1), 1), 0);
12959 return fold_build2 (NE_EXPR, type,
12960 fold_convert (TREE_TYPE (arg1), arg0),
12961 arg1);
12962 default:
12963 break;
12965 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12966 == min_hi
12967 && TREE_INT_CST_LOW (arg1) == min_lo)
12968 switch (code)
12970 case LT_EXPR:
12971 return omit_one_operand (type, integer_zero_node, arg0);
12973 case LE_EXPR:
12974 return fold_build2 (EQ_EXPR, type, op0, op1);
12976 case GE_EXPR:
12977 return omit_one_operand (type, integer_one_node, arg0);
12979 case GT_EXPR:
12980 return fold_build2 (NE_EXPR, type, op0, op1);
12982 default:
12983 break;
12985 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12986 == min_hi
12987 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12988 switch (code)
12990 case GE_EXPR:
12991 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12992 return fold_build2 (NE_EXPR, type,
12993 fold_convert (TREE_TYPE (arg1), arg0),
12994 arg1);
12995 case LT_EXPR:
12996 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12997 return fold_build2 (EQ_EXPR, type,
12998 fold_convert (TREE_TYPE (arg1), arg0),
12999 arg1);
13000 default:
13001 break;
13004 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13005 && TREE_INT_CST_LOW (arg1) == signed_max_lo
13006 && TYPE_UNSIGNED (arg1_type)
13007 /* We will flip the signedness of the comparison operator
13008 associated with the mode of arg1, so the sign bit is
13009 specified by this mode. Check that arg1 is the signed
13010 max associated with this sign bit. */
13011 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13012 /* signed_type does not work on pointer types. */
13013 && INTEGRAL_TYPE_P (arg1_type))
13015 /* The following case also applies to X < signed_max+1
13016 and X >= signed_max+1 because previous transformations. */
13017 if (code == LE_EXPR || code == GT_EXPR)
13019 tree st;
13020 st = signed_type_for (TREE_TYPE (arg1));
13021 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13022 type, fold_convert (st, arg0),
13023 build_int_cst (st, 0));
13029 /* If we are comparing an ABS_EXPR with a constant, we can
13030 convert all the cases into explicit comparisons, but they may
13031 well not be faster than doing the ABS and one comparison.
13032 But ABS (X) <= C is a range comparison, which becomes a subtraction
13033 and a comparison, and is probably faster. */
13034 if (code == LE_EXPR
13035 && TREE_CODE (arg1) == INTEGER_CST
13036 && TREE_CODE (arg0) == ABS_EXPR
13037 && ! TREE_SIDE_EFFECTS (arg0)
13038 && (0 != (tem = negate_expr (arg1)))
13039 && TREE_CODE (tem) == INTEGER_CST
13040 && !TREE_OVERFLOW (tem))
13041 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13042 build2 (GE_EXPR, type,
13043 TREE_OPERAND (arg0, 0), tem),
13044 build2 (LE_EXPR, type,
13045 TREE_OPERAND (arg0, 0), arg1));
13047 /* Convert ABS_EXPR<x> >= 0 to true. */
13048 strict_overflow_p = false;
13049 if (code == GE_EXPR
13050 && (integer_zerop (arg1)
13051 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13052 && real_zerop (arg1)))
13053 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13055 if (strict_overflow_p)
13056 fold_overflow_warning (("assuming signed overflow does not occur "
13057 "when simplifying comparison of "
13058 "absolute value and zero"),
13059 WARN_STRICT_OVERFLOW_CONDITIONAL);
13060 return omit_one_operand (type, integer_one_node, arg0);
13063 /* Convert ABS_EXPR<x> < 0 to false. */
13064 strict_overflow_p = false;
13065 if (code == LT_EXPR
13066 && (integer_zerop (arg1) || real_zerop (arg1))
13067 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13069 if (strict_overflow_p)
13070 fold_overflow_warning (("assuming signed overflow does not occur "
13071 "when simplifying comparison of "
13072 "absolute value and zero"),
13073 WARN_STRICT_OVERFLOW_CONDITIONAL);
13074 return omit_one_operand (type, integer_zero_node, arg0);
13077 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13078 and similarly for >= into !=. */
13079 if ((code == LT_EXPR || code == GE_EXPR)
13080 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13081 && TREE_CODE (arg1) == LSHIFT_EXPR
13082 && integer_onep (TREE_OPERAND (arg1, 0)))
13083 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13084 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13085 TREE_OPERAND (arg1, 1)),
13086 build_int_cst (TREE_TYPE (arg0), 0));
13088 if ((code == LT_EXPR || code == GE_EXPR)
13089 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13090 && CONVERT_EXPR_P (arg1)
13091 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13092 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13093 return
13094 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13095 fold_convert (TREE_TYPE (arg0),
13096 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13097 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13098 1))),
13099 build_int_cst (TREE_TYPE (arg0), 0));
13101 return NULL_TREE;
13103 case UNORDERED_EXPR:
13104 case ORDERED_EXPR:
13105 case UNLT_EXPR:
13106 case UNLE_EXPR:
13107 case UNGT_EXPR:
13108 case UNGE_EXPR:
13109 case UNEQ_EXPR:
13110 case LTGT_EXPR:
13111 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13113 t1 = fold_relational_const (code, type, arg0, arg1);
13114 if (t1 != NULL_TREE)
13115 return t1;
13118 /* If the first operand is NaN, the result is constant. */
13119 if (TREE_CODE (arg0) == REAL_CST
13120 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13121 && (code != LTGT_EXPR || ! flag_trapping_math))
13123 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13124 ? integer_zero_node
13125 : integer_one_node;
13126 return omit_one_operand (type, t1, arg1);
13129 /* If the second operand is NaN, the result is constant. */
13130 if (TREE_CODE (arg1) == REAL_CST
13131 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13132 && (code != LTGT_EXPR || ! flag_trapping_math))
13134 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13135 ? integer_zero_node
13136 : integer_one_node;
13137 return omit_one_operand (type, t1, arg0);
13140 /* Simplify unordered comparison of something with itself. */
13141 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13142 && operand_equal_p (arg0, arg1, 0))
13143 return constant_boolean_node (1, type);
13145 if (code == LTGT_EXPR
13146 && !flag_trapping_math
13147 && operand_equal_p (arg0, arg1, 0))
13148 return constant_boolean_node (0, type);
13150 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13152 tree targ0 = strip_float_extensions (arg0);
13153 tree targ1 = strip_float_extensions (arg1);
13154 tree newtype = TREE_TYPE (targ0);
13156 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13157 newtype = TREE_TYPE (targ1);
13159 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13160 return fold_build2 (code, type, fold_convert (newtype, targ0),
13161 fold_convert (newtype, targ1));
13164 return NULL_TREE;
13166 case COMPOUND_EXPR:
13167 /* When pedantic, a compound expression can be neither an lvalue
13168 nor an integer constant expression. */
13169 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13170 return NULL_TREE;
13171 /* Don't let (0, 0) be null pointer constant. */
13172 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13173 : fold_convert (type, arg1);
13174 return pedantic_non_lvalue (tem);
13176 case COMPLEX_EXPR:
13177 if ((TREE_CODE (arg0) == REAL_CST
13178 && TREE_CODE (arg1) == REAL_CST)
13179 || (TREE_CODE (arg0) == INTEGER_CST
13180 && TREE_CODE (arg1) == INTEGER_CST))
13181 return build_complex (type, arg0, arg1);
13182 return NULL_TREE;
13184 case ASSERT_EXPR:
13185 /* An ASSERT_EXPR should never be passed to fold_binary. */
13186 gcc_unreachable ();
13188 default:
13189 return NULL_TREE;
13190 } /* switch (code) */
13193 /* Callback for walk_tree, looking for LABEL_EXPR.
13194 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13195 Do not check the sub-tree of GOTO_EXPR. */
13197 static tree
13198 contains_label_1 (tree *tp,
13199 int *walk_subtrees,
13200 void *data ATTRIBUTE_UNUSED)
13202 switch (TREE_CODE (*tp))
13204 case LABEL_EXPR:
13205 return *tp;
13206 case GOTO_EXPR:
13207 *walk_subtrees = 0;
13208 /* no break */
13209 default:
13210 return NULL_TREE;
13214 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13215 accessible from outside the sub-tree. Returns NULL_TREE if no
13216 addressable label is found. */
13218 static bool
13219 contains_label_p (tree st)
13221 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13224 /* Fold a ternary expression of code CODE and type TYPE with operands
13225 OP0, OP1, and OP2. Return the folded expression if folding is
13226 successful. Otherwise, return NULL_TREE. */
13228 tree
13229 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13231 tree tem;
13232 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13233 enum tree_code_class kind = TREE_CODE_CLASS (code);
13235 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13236 && TREE_CODE_LENGTH (code) == 3);
13238 /* Strip any conversions that don't change the mode. This is safe
13239 for every expression, except for a comparison expression because
13240 its signedness is derived from its operands. So, in the latter
13241 case, only strip conversions that don't change the signedness.
13243 Note that this is done as an internal manipulation within the
13244 constant folder, in order to find the simplest representation of
13245 the arguments so that their form can be studied. In any cases,
13246 the appropriate type conversions should be put back in the tree
13247 that will get out of the constant folder. */
13248 if (op0)
13250 arg0 = op0;
13251 STRIP_NOPS (arg0);
13254 if (op1)
13256 arg1 = op1;
13257 STRIP_NOPS (arg1);
13260 switch (code)
13262 case COMPONENT_REF:
13263 if (TREE_CODE (arg0) == CONSTRUCTOR
13264 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13266 unsigned HOST_WIDE_INT idx;
13267 tree field, value;
13268 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13269 if (field == arg1)
13270 return value;
13272 return NULL_TREE;
13274 case COND_EXPR:
13275 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13276 so all simple results must be passed through pedantic_non_lvalue. */
13277 if (TREE_CODE (arg0) == INTEGER_CST)
13279 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13280 tem = integer_zerop (arg0) ? op2 : op1;
13281 /* Only optimize constant conditions when the selected branch
13282 has the same type as the COND_EXPR. This avoids optimizing
13283 away "c ? x : throw", where the throw has a void type.
13284 Avoid throwing away that operand which contains label. */
13285 if ((!TREE_SIDE_EFFECTS (unused_op)
13286 || !contains_label_p (unused_op))
13287 && (! VOID_TYPE_P (TREE_TYPE (tem))
13288 || VOID_TYPE_P (type)))
13289 return pedantic_non_lvalue (tem);
13290 return NULL_TREE;
13292 if (operand_equal_p (arg1, op2, 0))
13293 return pedantic_omit_one_operand (type, arg1, arg0);
13295 /* If we have A op B ? A : C, we may be able to convert this to a
13296 simpler expression, depending on the operation and the values
13297 of B and C. Signed zeros prevent all of these transformations,
13298 for reasons given above each one.
13300 Also try swapping the arguments and inverting the conditional. */
13301 if (COMPARISON_CLASS_P (arg0)
13302 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13303 arg1, TREE_OPERAND (arg0, 1))
13304 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13306 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13307 if (tem)
13308 return tem;
13311 if (COMPARISON_CLASS_P (arg0)
13312 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13313 op2,
13314 TREE_OPERAND (arg0, 1))
13315 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13317 tem = fold_truth_not_expr (arg0);
13318 if (tem && COMPARISON_CLASS_P (tem))
13320 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13321 if (tem)
13322 return tem;
13326 /* If the second operand is simpler than the third, swap them
13327 since that produces better jump optimization results. */
13328 if (truth_value_p (TREE_CODE (arg0))
13329 && tree_swap_operands_p (op1, op2, false))
13331 /* See if this can be inverted. If it can't, possibly because
13332 it was a floating-point inequality comparison, don't do
13333 anything. */
13334 tem = fold_truth_not_expr (arg0);
13335 if (tem)
13336 return fold_build3 (code, type, tem, op2, op1);
13339 /* Convert A ? 1 : 0 to simply A. */
13340 if (integer_onep (op1)
13341 && integer_zerop (op2)
13342 /* If we try to convert OP0 to our type, the
13343 call to fold will try to move the conversion inside
13344 a COND, which will recurse. In that case, the COND_EXPR
13345 is probably the best choice, so leave it alone. */
13346 && type == TREE_TYPE (arg0))
13347 return pedantic_non_lvalue (arg0);
13349 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13350 over COND_EXPR in cases such as floating point comparisons. */
13351 if (integer_zerop (op1)
13352 && integer_onep (op2)
13353 && truth_value_p (TREE_CODE (arg0)))
13354 return pedantic_non_lvalue (fold_convert (type,
13355 invert_truthvalue (arg0)));
13357 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13358 if (TREE_CODE (arg0) == LT_EXPR
13359 && integer_zerop (TREE_OPERAND (arg0, 1))
13360 && integer_zerop (op2)
13361 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13363 /* sign_bit_p only checks ARG1 bits within A's precision.
13364 If <sign bit of A> has wider type than A, bits outside
13365 of A's precision in <sign bit of A> need to be checked.
13366 If they are all 0, this optimization needs to be done
13367 in unsigned A's type, if they are all 1 in signed A's type,
13368 otherwise this can't be done. */
13369 if (TYPE_PRECISION (TREE_TYPE (tem))
13370 < TYPE_PRECISION (TREE_TYPE (arg1))
13371 && TYPE_PRECISION (TREE_TYPE (tem))
13372 < TYPE_PRECISION (type))
13374 unsigned HOST_WIDE_INT mask_lo;
13375 HOST_WIDE_INT mask_hi;
13376 int inner_width, outer_width;
13377 tree tem_type;
13379 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13380 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13381 if (outer_width > TYPE_PRECISION (type))
13382 outer_width = TYPE_PRECISION (type);
13384 if (outer_width > HOST_BITS_PER_WIDE_INT)
13386 mask_hi = ((unsigned HOST_WIDE_INT) -1
13387 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13388 mask_lo = -1;
13390 else
13392 mask_hi = 0;
13393 mask_lo = ((unsigned HOST_WIDE_INT) -1
13394 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13396 if (inner_width > HOST_BITS_PER_WIDE_INT)
13398 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13399 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13400 mask_lo = 0;
13402 else
13403 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13404 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13406 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13407 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13409 tem_type = signed_type_for (TREE_TYPE (tem));
13410 tem = fold_convert (tem_type, tem);
13412 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13413 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13415 tem_type = unsigned_type_for (TREE_TYPE (tem));
13416 tem = fold_convert (tem_type, tem);
13418 else
13419 tem = NULL;
13422 if (tem)
13423 return fold_convert (type,
13424 fold_build2 (BIT_AND_EXPR,
13425 TREE_TYPE (tem), tem,
13426 fold_convert (TREE_TYPE (tem),
13427 arg1)));
13430 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13431 already handled above. */
13432 if (TREE_CODE (arg0) == BIT_AND_EXPR
13433 && integer_onep (TREE_OPERAND (arg0, 1))
13434 && integer_zerop (op2)
13435 && integer_pow2p (arg1))
13437 tree tem = TREE_OPERAND (arg0, 0);
13438 STRIP_NOPS (tem);
13439 if (TREE_CODE (tem) == RSHIFT_EXPR
13440 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13441 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13442 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13443 return fold_build2 (BIT_AND_EXPR, type,
13444 TREE_OPERAND (tem, 0), arg1);
13447 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13448 is probably obsolete because the first operand should be a
13449 truth value (that's why we have the two cases above), but let's
13450 leave it in until we can confirm this for all front-ends. */
13451 if (integer_zerop (op2)
13452 && TREE_CODE (arg0) == NE_EXPR
13453 && integer_zerop (TREE_OPERAND (arg0, 1))
13454 && integer_pow2p (arg1)
13455 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13456 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13457 arg1, OEP_ONLY_CONST))
13458 return pedantic_non_lvalue (fold_convert (type,
13459 TREE_OPERAND (arg0, 0)));
13461 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13462 if (integer_zerop (op2)
13463 && truth_value_p (TREE_CODE (arg0))
13464 && truth_value_p (TREE_CODE (arg1)))
13465 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13466 fold_convert (type, arg0),
13467 arg1);
13469 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13470 if (integer_onep (op2)
13471 && truth_value_p (TREE_CODE (arg0))
13472 && truth_value_p (TREE_CODE (arg1)))
13474 /* Only perform transformation if ARG0 is easily inverted. */
13475 tem = fold_truth_not_expr (arg0);
13476 if (tem)
13477 return fold_build2 (TRUTH_ORIF_EXPR, type,
13478 fold_convert (type, tem),
13479 arg1);
13482 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13483 if (integer_zerop (arg1)
13484 && truth_value_p (TREE_CODE (arg0))
13485 && truth_value_p (TREE_CODE (op2)))
13487 /* Only perform transformation if ARG0 is easily inverted. */
13488 tem = fold_truth_not_expr (arg0);
13489 if (tem)
13490 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13491 fold_convert (type, tem),
13492 op2);
13495 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13496 if (integer_onep (arg1)
13497 && truth_value_p (TREE_CODE (arg0))
13498 && truth_value_p (TREE_CODE (op2)))
13499 return fold_build2 (TRUTH_ORIF_EXPR, type,
13500 fold_convert (type, arg0),
13501 op2);
13503 return NULL_TREE;
13505 case CALL_EXPR:
13506 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13507 of fold_ternary on them. */
13508 gcc_unreachable ();
13510 case BIT_FIELD_REF:
13511 if ((TREE_CODE (arg0) == VECTOR_CST
13512 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13513 && type == TREE_TYPE (TREE_TYPE (arg0)))
13515 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13516 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13518 if (width != 0
13519 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13520 && (idx % width) == 0
13521 && (idx = idx / width)
13522 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13524 tree elements = NULL_TREE;
13526 if (TREE_CODE (arg0) == VECTOR_CST)
13527 elements = TREE_VECTOR_CST_ELTS (arg0);
13528 else
13530 unsigned HOST_WIDE_INT idx;
13531 tree value;
13533 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13534 elements = tree_cons (NULL_TREE, value, elements);
13536 while (idx-- > 0 && elements)
13537 elements = TREE_CHAIN (elements);
13538 if (elements)
13539 return TREE_VALUE (elements);
13540 else
13541 return fold_convert (type, integer_zero_node);
13545 /* A bit-field-ref that referenced the full argument can be stripped. */
13546 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13547 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13548 && integer_zerop (op2))
13549 return fold_convert (type, arg0);
13551 return NULL_TREE;
13553 default:
13554 return NULL_TREE;
13555 } /* switch (code) */
13558 /* Perform constant folding and related simplification of EXPR.
13559 The related simplifications include x*1 => x, x*0 => 0, etc.,
13560 and application of the associative law.
13561 NOP_EXPR conversions may be removed freely (as long as we
13562 are careful not to change the type of the overall expression).
13563 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13564 but we can constant-fold them if they have constant operands. */
13566 #ifdef ENABLE_FOLD_CHECKING
13567 # define fold(x) fold_1 (x)
13568 static tree fold_1 (tree);
13569 static
13570 #endif
13571 tree
13572 fold (tree expr)
13574 const tree t = expr;
13575 enum tree_code code = TREE_CODE (t);
13576 enum tree_code_class kind = TREE_CODE_CLASS (code);
13577 tree tem;
13579 /* Return right away if a constant. */
13580 if (kind == tcc_constant)
13581 return t;
13583 /* CALL_EXPR-like objects with variable numbers of operands are
13584 treated specially. */
13585 if (kind == tcc_vl_exp)
13587 if (code == CALL_EXPR)
13589 tem = fold_call_expr (expr, false);
13590 return tem ? tem : expr;
13592 return expr;
13595 if (IS_EXPR_CODE_CLASS (kind))
13597 tree type = TREE_TYPE (t);
13598 tree op0, op1, op2;
13600 switch (TREE_CODE_LENGTH (code))
13602 case 1:
13603 op0 = TREE_OPERAND (t, 0);
13604 tem = fold_unary (code, type, op0);
13605 return tem ? tem : expr;
13606 case 2:
13607 op0 = TREE_OPERAND (t, 0);
13608 op1 = TREE_OPERAND (t, 1);
13609 tem = fold_binary (code, type, op0, op1);
13610 return tem ? tem : expr;
13611 case 3:
13612 op0 = TREE_OPERAND (t, 0);
13613 op1 = TREE_OPERAND (t, 1);
13614 op2 = TREE_OPERAND (t, 2);
13615 tem = fold_ternary (code, type, op0, op1, op2);
13616 return tem ? tem : expr;
13617 default:
13618 break;
13622 switch (code)
13624 case ARRAY_REF:
13626 tree op0 = TREE_OPERAND (t, 0);
13627 tree op1 = TREE_OPERAND (t, 1);
13629 if (TREE_CODE (op1) == INTEGER_CST
13630 && TREE_CODE (op0) == CONSTRUCTOR
13631 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13633 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13634 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13635 unsigned HOST_WIDE_INT begin = 0;
13637 /* Find a matching index by means of a binary search. */
13638 while (begin != end)
13640 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13641 tree index = VEC_index (constructor_elt, elts, middle)->index;
13643 if (TREE_CODE (index) == INTEGER_CST
13644 && tree_int_cst_lt (index, op1))
13645 begin = middle + 1;
13646 else if (TREE_CODE (index) == INTEGER_CST
13647 && tree_int_cst_lt (op1, index))
13648 end = middle;
13649 else if (TREE_CODE (index) == RANGE_EXPR
13650 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13651 begin = middle + 1;
13652 else if (TREE_CODE (index) == RANGE_EXPR
13653 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13654 end = middle;
13655 else
13656 return VEC_index (constructor_elt, elts, middle)->value;
13660 return t;
13663 case CONST_DECL:
13664 return fold (DECL_INITIAL (t));
13666 default:
13667 return t;
13668 } /* switch (code) */
13671 #ifdef ENABLE_FOLD_CHECKING
13672 #undef fold
13674 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13675 static void fold_check_failed (const_tree, const_tree);
13676 void print_fold_checksum (const_tree);
13678 /* When --enable-checking=fold, compute a digest of expr before
13679 and after actual fold call to see if fold did not accidentally
13680 change original expr. */
13682 tree
13683 fold (tree expr)
13685 tree ret;
13686 struct md5_ctx ctx;
13687 unsigned char checksum_before[16], checksum_after[16];
13688 htab_t ht;
13690 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13691 md5_init_ctx (&ctx);
13692 fold_checksum_tree (expr, &ctx, ht);
13693 md5_finish_ctx (&ctx, checksum_before);
13694 htab_empty (ht);
13696 ret = fold_1 (expr);
13698 md5_init_ctx (&ctx);
13699 fold_checksum_tree (expr, &ctx, ht);
13700 md5_finish_ctx (&ctx, checksum_after);
13701 htab_delete (ht);
13703 if (memcmp (checksum_before, checksum_after, 16))
13704 fold_check_failed (expr, ret);
13706 return ret;
13709 void
13710 print_fold_checksum (const_tree expr)
13712 struct md5_ctx ctx;
13713 unsigned char checksum[16], cnt;
13714 htab_t ht;
13716 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13717 md5_init_ctx (&ctx);
13718 fold_checksum_tree (expr, &ctx, ht);
13719 md5_finish_ctx (&ctx, checksum);
13720 htab_delete (ht);
13721 for (cnt = 0; cnt < 16; ++cnt)
13722 fprintf (stderr, "%02x", checksum[cnt]);
13723 putc ('\n', stderr);
13726 static void
13727 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13729 internal_error ("fold check: original tree changed by fold");
13732 static void
13733 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13735 const void **slot;
13736 enum tree_code code;
13737 union tree_node buf;
13738 int i, len;
13740 recursive_label:
13742 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13743 <= sizeof (struct tree_function_decl))
13744 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13745 if (expr == NULL)
13746 return;
13747 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13748 if (*slot != NULL)
13749 return;
13750 *slot = expr;
13751 code = TREE_CODE (expr);
13752 if (TREE_CODE_CLASS (code) == tcc_declaration
13753 && DECL_ASSEMBLER_NAME_SET_P (expr))
13755 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13756 memcpy ((char *) &buf, expr, tree_size (expr));
13757 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13758 expr = (tree) &buf;
13760 else if (TREE_CODE_CLASS (code) == tcc_type
13761 && (TYPE_POINTER_TO (expr)
13762 || TYPE_REFERENCE_TO (expr)
13763 || TYPE_CACHED_VALUES_P (expr)
13764 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13765 || TYPE_NEXT_VARIANT (expr)))
13767 /* Allow these fields to be modified. */
13768 tree tmp;
13769 memcpy ((char *) &buf, expr, tree_size (expr));
13770 expr = tmp = (tree) &buf;
13771 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13772 TYPE_POINTER_TO (tmp) = NULL;
13773 TYPE_REFERENCE_TO (tmp) = NULL;
13774 TYPE_NEXT_VARIANT (tmp) = NULL;
13775 if (TYPE_CACHED_VALUES_P (tmp))
13777 TYPE_CACHED_VALUES_P (tmp) = 0;
13778 TYPE_CACHED_VALUES (tmp) = NULL;
13781 md5_process_bytes (expr, tree_size (expr), ctx);
13782 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13783 if (TREE_CODE_CLASS (code) != tcc_type
13784 && TREE_CODE_CLASS (code) != tcc_declaration
13785 && code != TREE_LIST
13786 && code != SSA_NAME)
13787 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13788 switch (TREE_CODE_CLASS (code))
13790 case tcc_constant:
13791 switch (code)
13793 case STRING_CST:
13794 md5_process_bytes (TREE_STRING_POINTER (expr),
13795 TREE_STRING_LENGTH (expr), ctx);
13796 break;
13797 case COMPLEX_CST:
13798 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13799 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13800 break;
13801 case VECTOR_CST:
13802 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13803 break;
13804 default:
13805 break;
13807 break;
13808 case tcc_exceptional:
13809 switch (code)
13811 case TREE_LIST:
13812 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13813 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13814 expr = TREE_CHAIN (expr);
13815 goto recursive_label;
13816 break;
13817 case TREE_VEC:
13818 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13819 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13820 break;
13821 default:
13822 break;
13824 break;
13825 case tcc_expression:
13826 case tcc_reference:
13827 case tcc_comparison:
13828 case tcc_unary:
13829 case tcc_binary:
13830 case tcc_statement:
13831 case tcc_vl_exp:
13832 len = TREE_OPERAND_LENGTH (expr);
13833 for (i = 0; i < len; ++i)
13834 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13835 break;
13836 case tcc_declaration:
13837 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13838 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13839 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13841 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13842 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13843 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13844 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13845 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13847 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13848 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13850 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13852 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13853 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13854 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13856 break;
13857 case tcc_type:
13858 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13859 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13860 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13861 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13862 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13863 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13864 if (INTEGRAL_TYPE_P (expr)
13865 || SCALAR_FLOAT_TYPE_P (expr))
13867 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13868 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13870 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13871 if (TREE_CODE (expr) == RECORD_TYPE
13872 || TREE_CODE (expr) == UNION_TYPE
13873 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13874 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13875 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13876 break;
13877 default:
13878 break;
13882 /* Helper function for outputting the checksum of a tree T. When
13883 debugging with gdb, you can "define mynext" to be "next" followed
13884 by "call debug_fold_checksum (op0)", then just trace down till the
13885 outputs differ. */
13887 void
13888 debug_fold_checksum (const_tree t)
13890 int i;
13891 unsigned char checksum[16];
13892 struct md5_ctx ctx;
13893 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13895 md5_init_ctx (&ctx);
13896 fold_checksum_tree (t, &ctx, ht);
13897 md5_finish_ctx (&ctx, checksum);
13898 htab_empty (ht);
13900 for (i = 0; i < 16; i++)
13901 fprintf (stderr, "%d ", checksum[i]);
13903 fprintf (stderr, "\n");
13906 #endif
13908 /* Fold a unary tree expression with code CODE of type TYPE with an
13909 operand OP0. Return a folded expression if successful. Otherwise,
13910 return a tree expression with code CODE of type TYPE with an
13911 operand OP0. */
13913 tree
13914 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13916 tree tem;
13917 #ifdef ENABLE_FOLD_CHECKING
13918 unsigned char checksum_before[16], checksum_after[16];
13919 struct md5_ctx ctx;
13920 htab_t ht;
13922 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13923 md5_init_ctx (&ctx);
13924 fold_checksum_tree (op0, &ctx, ht);
13925 md5_finish_ctx (&ctx, checksum_before);
13926 htab_empty (ht);
13927 #endif
13929 tem = fold_unary (code, type, op0);
13930 if (!tem)
13931 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13933 #ifdef ENABLE_FOLD_CHECKING
13934 md5_init_ctx (&ctx);
13935 fold_checksum_tree (op0, &ctx, ht);
13936 md5_finish_ctx (&ctx, checksum_after);
13937 htab_delete (ht);
13939 if (memcmp (checksum_before, checksum_after, 16))
13940 fold_check_failed (op0, tem);
13941 #endif
13942 return tem;
13945 /* Fold a binary tree expression with code CODE of type TYPE with
13946 operands OP0 and OP1. Return a folded expression if successful.
13947 Otherwise, return a tree expression with code CODE of type TYPE
13948 with operands OP0 and OP1. */
13950 tree
13951 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13952 MEM_STAT_DECL)
13954 tree tem;
13955 #ifdef ENABLE_FOLD_CHECKING
13956 unsigned char checksum_before_op0[16],
13957 checksum_before_op1[16],
13958 checksum_after_op0[16],
13959 checksum_after_op1[16];
13960 struct md5_ctx ctx;
13961 htab_t ht;
13963 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13964 md5_init_ctx (&ctx);
13965 fold_checksum_tree (op0, &ctx, ht);
13966 md5_finish_ctx (&ctx, checksum_before_op0);
13967 htab_empty (ht);
13969 md5_init_ctx (&ctx);
13970 fold_checksum_tree (op1, &ctx, ht);
13971 md5_finish_ctx (&ctx, checksum_before_op1);
13972 htab_empty (ht);
13973 #endif
13975 tem = fold_binary (code, type, op0, op1);
13976 if (!tem)
13977 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13979 #ifdef ENABLE_FOLD_CHECKING
13980 md5_init_ctx (&ctx);
13981 fold_checksum_tree (op0, &ctx, ht);
13982 md5_finish_ctx (&ctx, checksum_after_op0);
13983 htab_empty (ht);
13985 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13986 fold_check_failed (op0, tem);
13988 md5_init_ctx (&ctx);
13989 fold_checksum_tree (op1, &ctx, ht);
13990 md5_finish_ctx (&ctx, checksum_after_op1);
13991 htab_delete (ht);
13993 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13994 fold_check_failed (op1, tem);
13995 #endif
13996 return tem;
13999 /* Fold a ternary tree expression with code CODE of type TYPE with
14000 operands OP0, OP1, and OP2. Return a folded expression if
14001 successful. Otherwise, return a tree expression with code CODE of
14002 type TYPE with operands OP0, OP1, and OP2. */
14004 tree
14005 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
14006 MEM_STAT_DECL)
14008 tree tem;
14009 #ifdef ENABLE_FOLD_CHECKING
14010 unsigned char checksum_before_op0[16],
14011 checksum_before_op1[16],
14012 checksum_before_op2[16],
14013 checksum_after_op0[16],
14014 checksum_after_op1[16],
14015 checksum_after_op2[16];
14016 struct md5_ctx ctx;
14017 htab_t ht;
14019 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14020 md5_init_ctx (&ctx);
14021 fold_checksum_tree (op0, &ctx, ht);
14022 md5_finish_ctx (&ctx, checksum_before_op0);
14023 htab_empty (ht);
14025 md5_init_ctx (&ctx);
14026 fold_checksum_tree (op1, &ctx, ht);
14027 md5_finish_ctx (&ctx, checksum_before_op1);
14028 htab_empty (ht);
14030 md5_init_ctx (&ctx);
14031 fold_checksum_tree (op2, &ctx, ht);
14032 md5_finish_ctx (&ctx, checksum_before_op2);
14033 htab_empty (ht);
14034 #endif
14036 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14037 tem = fold_ternary (code, type, op0, op1, op2);
14038 if (!tem)
14039 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14041 #ifdef ENABLE_FOLD_CHECKING
14042 md5_init_ctx (&ctx);
14043 fold_checksum_tree (op0, &ctx, ht);
14044 md5_finish_ctx (&ctx, checksum_after_op0);
14045 htab_empty (ht);
14047 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14048 fold_check_failed (op0, tem);
14050 md5_init_ctx (&ctx);
14051 fold_checksum_tree (op1, &ctx, ht);
14052 md5_finish_ctx (&ctx, checksum_after_op1);
14053 htab_empty (ht);
14055 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14056 fold_check_failed (op1, tem);
14058 md5_init_ctx (&ctx);
14059 fold_checksum_tree (op2, &ctx, ht);
14060 md5_finish_ctx (&ctx, checksum_after_op2);
14061 htab_delete (ht);
14063 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14064 fold_check_failed (op2, tem);
14065 #endif
14066 return tem;
14069 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14070 arguments in ARGARRAY, and a null static chain.
14071 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14072 of type TYPE from the given operands as constructed by build_call_array. */
14074 tree
14075 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14077 tree tem;
14078 #ifdef ENABLE_FOLD_CHECKING
14079 unsigned char checksum_before_fn[16],
14080 checksum_before_arglist[16],
14081 checksum_after_fn[16],
14082 checksum_after_arglist[16];
14083 struct md5_ctx ctx;
14084 htab_t ht;
14085 int i;
14087 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14088 md5_init_ctx (&ctx);
14089 fold_checksum_tree (fn, &ctx, ht);
14090 md5_finish_ctx (&ctx, checksum_before_fn);
14091 htab_empty (ht);
14093 md5_init_ctx (&ctx);
14094 for (i = 0; i < nargs; i++)
14095 fold_checksum_tree (argarray[i], &ctx, ht);
14096 md5_finish_ctx (&ctx, checksum_before_arglist);
14097 htab_empty (ht);
14098 #endif
14100 tem = fold_builtin_call_array (type, fn, nargs, argarray);
14102 #ifdef ENABLE_FOLD_CHECKING
14103 md5_init_ctx (&ctx);
14104 fold_checksum_tree (fn, &ctx, ht);
14105 md5_finish_ctx (&ctx, checksum_after_fn);
14106 htab_empty (ht);
14108 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14109 fold_check_failed (fn, tem);
14111 md5_init_ctx (&ctx);
14112 for (i = 0; i < nargs; i++)
14113 fold_checksum_tree (argarray[i], &ctx, ht);
14114 md5_finish_ctx (&ctx, checksum_after_arglist);
14115 htab_delete (ht);
14117 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14118 fold_check_failed (NULL_TREE, tem);
14119 #endif
14120 return tem;
14123 /* Perform constant folding and related simplification of initializer
14124 expression EXPR. These behave identically to "fold_buildN" but ignore
14125 potential run-time traps and exceptions that fold must preserve. */
14127 #define START_FOLD_INIT \
14128 int saved_signaling_nans = flag_signaling_nans;\
14129 int saved_trapping_math = flag_trapping_math;\
14130 int saved_rounding_math = flag_rounding_math;\
14131 int saved_trapv = flag_trapv;\
14132 int saved_folding_initializer = folding_initializer;\
14133 flag_signaling_nans = 0;\
14134 flag_trapping_math = 0;\
14135 flag_rounding_math = 0;\
14136 flag_trapv = 0;\
14137 folding_initializer = 1;
14139 #define END_FOLD_INIT \
14140 flag_signaling_nans = saved_signaling_nans;\
14141 flag_trapping_math = saved_trapping_math;\
14142 flag_rounding_math = saved_rounding_math;\
14143 flag_trapv = saved_trapv;\
14144 folding_initializer = saved_folding_initializer;
14146 tree
14147 fold_build1_initializer (enum tree_code code, tree type, tree op)
14149 tree result;
14150 START_FOLD_INIT;
14152 result = fold_build1 (code, type, op);
14154 END_FOLD_INIT;
14155 return result;
14158 tree
14159 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14161 tree result;
14162 START_FOLD_INIT;
14164 result = fold_build2 (code, type, op0, op1);
14166 END_FOLD_INIT;
14167 return result;
14170 tree
14171 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14172 tree op2)
14174 tree result;
14175 START_FOLD_INIT;
14177 result = fold_build3 (code, type, op0, op1, op2);
14179 END_FOLD_INIT;
14180 return result;
14183 tree
14184 fold_build_call_array_initializer (tree type, tree fn,
14185 int nargs, tree *argarray)
14187 tree result;
14188 START_FOLD_INIT;
14190 result = fold_build_call_array (type, fn, nargs, argarray);
14192 END_FOLD_INIT;
14193 return result;
14196 #undef START_FOLD_INIT
14197 #undef END_FOLD_INIT
14199 /* Determine if first argument is a multiple of second argument. Return 0 if
14200 it is not, or we cannot easily determined it to be.
14202 An example of the sort of thing we care about (at this point; this routine
14203 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14204 fold cases do now) is discovering that
14206 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14208 is a multiple of
14210 SAVE_EXPR (J * 8)
14212 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14214 This code also handles discovering that
14216 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14218 is a multiple of 8 so we don't have to worry about dealing with a
14219 possible remainder.
14221 Note that we *look* inside a SAVE_EXPR only to determine how it was
14222 calculated; it is not safe for fold to do much of anything else with the
14223 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14224 at run time. For example, the latter example above *cannot* be implemented
14225 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14226 evaluation time of the original SAVE_EXPR is not necessarily the same at
14227 the time the new expression is evaluated. The only optimization of this
14228 sort that would be valid is changing
14230 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14232 divided by 8 to
14234 SAVE_EXPR (I) * SAVE_EXPR (J)
14236 (where the same SAVE_EXPR (J) is used in the original and the
14237 transformed version). */
14240 multiple_of_p (tree type, const_tree top, const_tree bottom)
14242 if (operand_equal_p (top, bottom, 0))
14243 return 1;
14245 if (TREE_CODE (type) != INTEGER_TYPE)
14246 return 0;
14248 switch (TREE_CODE (top))
14250 case BIT_AND_EXPR:
14251 /* Bitwise and provides a power of two multiple. If the mask is
14252 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14253 if (!integer_pow2p (bottom))
14254 return 0;
14255 /* FALLTHRU */
14257 case MULT_EXPR:
14258 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14259 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14261 case PLUS_EXPR:
14262 case MINUS_EXPR:
14263 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14264 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14266 case LSHIFT_EXPR:
14267 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14269 tree op1, t1;
14271 op1 = TREE_OPERAND (top, 1);
14272 /* const_binop may not detect overflow correctly,
14273 so check for it explicitly here. */
14274 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14275 > TREE_INT_CST_LOW (op1)
14276 && TREE_INT_CST_HIGH (op1) == 0
14277 && 0 != (t1 = fold_convert (type,
14278 const_binop (LSHIFT_EXPR,
14279 size_one_node,
14280 op1, 0)))
14281 && !TREE_OVERFLOW (t1))
14282 return multiple_of_p (type, t1, bottom);
14284 return 0;
14286 case NOP_EXPR:
14287 /* Can't handle conversions from non-integral or wider integral type. */
14288 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14289 || (TYPE_PRECISION (type)
14290 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14291 return 0;
14293 /* .. fall through ... */
14295 case SAVE_EXPR:
14296 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14298 case INTEGER_CST:
14299 if (TREE_CODE (bottom) != INTEGER_CST
14300 || integer_zerop (bottom)
14301 || (TYPE_UNSIGNED (type)
14302 && (tree_int_cst_sgn (top) < 0
14303 || tree_int_cst_sgn (bottom) < 0)))
14304 return 0;
14305 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14306 top, bottom, 0));
14308 default:
14309 return 0;
14313 /* Return true if CODE or TYPE is known to be non-negative. */
14315 static bool
14316 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14318 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14319 && truth_value_p (code))
14320 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14321 have a signed:1 type (where the value is -1 and 0). */
14322 return true;
14323 return false;
14326 /* Return true if (CODE OP0) is known to be non-negative. If the return
14327 value is based on the assumption that signed overflow is undefined,
14328 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14329 *STRICT_OVERFLOW_P. */
14331 bool
14332 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14333 bool *strict_overflow_p)
14335 if (TYPE_UNSIGNED (type))
14336 return true;
14338 switch (code)
14340 case ABS_EXPR:
14341 /* We can't return 1 if flag_wrapv is set because
14342 ABS_EXPR<INT_MIN> = INT_MIN. */
14343 if (!INTEGRAL_TYPE_P (type))
14344 return true;
14345 if (TYPE_OVERFLOW_UNDEFINED (type))
14347 *strict_overflow_p = true;
14348 return true;
14350 break;
14352 case NON_LVALUE_EXPR:
14353 case FLOAT_EXPR:
14354 case FIX_TRUNC_EXPR:
14355 return tree_expr_nonnegative_warnv_p (op0,
14356 strict_overflow_p);
14358 case NOP_EXPR:
14360 tree inner_type = TREE_TYPE (op0);
14361 tree outer_type = type;
14363 if (TREE_CODE (outer_type) == REAL_TYPE)
14365 if (TREE_CODE (inner_type) == REAL_TYPE)
14366 return tree_expr_nonnegative_warnv_p (op0,
14367 strict_overflow_p);
14368 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14370 if (TYPE_UNSIGNED (inner_type))
14371 return true;
14372 return tree_expr_nonnegative_warnv_p (op0,
14373 strict_overflow_p);
14376 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14378 if (TREE_CODE (inner_type) == REAL_TYPE)
14379 return tree_expr_nonnegative_warnv_p (op0,
14380 strict_overflow_p);
14381 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14382 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14383 && TYPE_UNSIGNED (inner_type);
14386 break;
14388 default:
14389 return tree_simple_nonnegative_warnv_p (code, type);
14392 /* We don't know sign of `t', so be conservative and return false. */
14393 return false;
14396 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14397 value is based on the assumption that signed overflow is undefined,
14398 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14399 *STRICT_OVERFLOW_P. */
14401 bool
14402 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14403 tree op1, bool *strict_overflow_p)
14405 if (TYPE_UNSIGNED (type))
14406 return true;
14408 switch (code)
14410 case POINTER_PLUS_EXPR:
14411 case PLUS_EXPR:
14412 if (FLOAT_TYPE_P (type))
14413 return (tree_expr_nonnegative_warnv_p (op0,
14414 strict_overflow_p)
14415 && tree_expr_nonnegative_warnv_p (op1,
14416 strict_overflow_p));
14418 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14419 both unsigned and at least 2 bits shorter than the result. */
14420 if (TREE_CODE (type) == INTEGER_TYPE
14421 && TREE_CODE (op0) == NOP_EXPR
14422 && TREE_CODE (op1) == NOP_EXPR)
14424 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14425 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14426 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14427 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14429 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14430 TYPE_PRECISION (inner2)) + 1;
14431 return prec < TYPE_PRECISION (type);
14434 break;
14436 case MULT_EXPR:
14437 if (FLOAT_TYPE_P (type))
14439 /* x * x for floating point x is always non-negative. */
14440 if (operand_equal_p (op0, op1, 0))
14441 return true;
14442 return (tree_expr_nonnegative_warnv_p (op0,
14443 strict_overflow_p)
14444 && tree_expr_nonnegative_warnv_p (op1,
14445 strict_overflow_p));
14448 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14449 both unsigned and their total bits is shorter than the result. */
14450 if (TREE_CODE (type) == INTEGER_TYPE
14451 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14452 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14454 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14455 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14456 : TREE_TYPE (op0);
14457 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14458 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14459 : TREE_TYPE (op1);
14461 bool unsigned0 = TYPE_UNSIGNED (inner0);
14462 bool unsigned1 = TYPE_UNSIGNED (inner1);
14464 if (TREE_CODE (op0) == INTEGER_CST)
14465 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14467 if (TREE_CODE (op1) == INTEGER_CST)
14468 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14470 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14471 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14473 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14474 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14475 : TYPE_PRECISION (inner0);
14477 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14478 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14479 : TYPE_PRECISION (inner1);
14481 return precision0 + precision1 < TYPE_PRECISION (type);
14484 return false;
14486 case BIT_AND_EXPR:
14487 case MAX_EXPR:
14488 return (tree_expr_nonnegative_warnv_p (op0,
14489 strict_overflow_p)
14490 || tree_expr_nonnegative_warnv_p (op1,
14491 strict_overflow_p));
14493 case BIT_IOR_EXPR:
14494 case BIT_XOR_EXPR:
14495 case MIN_EXPR:
14496 case RDIV_EXPR:
14497 case TRUNC_DIV_EXPR:
14498 case CEIL_DIV_EXPR:
14499 case FLOOR_DIV_EXPR:
14500 case ROUND_DIV_EXPR:
14501 return (tree_expr_nonnegative_warnv_p (op0,
14502 strict_overflow_p)
14503 && tree_expr_nonnegative_warnv_p (op1,
14504 strict_overflow_p));
14506 case TRUNC_MOD_EXPR:
14507 case CEIL_MOD_EXPR:
14508 case FLOOR_MOD_EXPR:
14509 case ROUND_MOD_EXPR:
14510 return tree_expr_nonnegative_warnv_p (op0,
14511 strict_overflow_p);
14512 default:
14513 return tree_simple_nonnegative_warnv_p (code, type);
14516 /* We don't know sign of `t', so be conservative and return false. */
14517 return false;
14520 /* Return true if T is known to be non-negative. If the return
14521 value is based on the assumption that signed overflow is undefined,
14522 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14523 *STRICT_OVERFLOW_P. */
14525 bool
14526 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14528 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14529 return true;
14531 switch (TREE_CODE (t))
14533 case INTEGER_CST:
14534 return tree_int_cst_sgn (t) >= 0;
14536 case REAL_CST:
14537 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14539 case FIXED_CST:
14540 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14542 case COND_EXPR:
14543 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14544 strict_overflow_p)
14545 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14546 strict_overflow_p));
14547 default:
14548 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14549 TREE_TYPE (t));
14551 /* We don't know sign of `t', so be conservative and return false. */
14552 return false;
14555 /* Return true if T is known to be non-negative. If the return
14556 value is based on the assumption that signed overflow is undefined,
14557 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14558 *STRICT_OVERFLOW_P. */
14560 bool
14561 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14562 tree arg0, tree arg1, bool *strict_overflow_p)
14564 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14565 switch (DECL_FUNCTION_CODE (fndecl))
14567 CASE_FLT_FN (BUILT_IN_ACOS):
14568 CASE_FLT_FN (BUILT_IN_ACOSH):
14569 CASE_FLT_FN (BUILT_IN_CABS):
14570 CASE_FLT_FN (BUILT_IN_COSH):
14571 CASE_FLT_FN (BUILT_IN_ERFC):
14572 CASE_FLT_FN (BUILT_IN_EXP):
14573 CASE_FLT_FN (BUILT_IN_EXP10):
14574 CASE_FLT_FN (BUILT_IN_EXP2):
14575 CASE_FLT_FN (BUILT_IN_FABS):
14576 CASE_FLT_FN (BUILT_IN_FDIM):
14577 CASE_FLT_FN (BUILT_IN_HYPOT):
14578 CASE_FLT_FN (BUILT_IN_POW10):
14579 CASE_INT_FN (BUILT_IN_FFS):
14580 CASE_INT_FN (BUILT_IN_PARITY):
14581 CASE_INT_FN (BUILT_IN_POPCOUNT):
14582 case BUILT_IN_BSWAP32:
14583 case BUILT_IN_BSWAP64:
14584 /* Always true. */
14585 return true;
14587 CASE_FLT_FN (BUILT_IN_SQRT):
14588 /* sqrt(-0.0) is -0.0. */
14589 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14590 return true;
14591 return tree_expr_nonnegative_warnv_p (arg0,
14592 strict_overflow_p);
14594 CASE_FLT_FN (BUILT_IN_ASINH):
14595 CASE_FLT_FN (BUILT_IN_ATAN):
14596 CASE_FLT_FN (BUILT_IN_ATANH):
14597 CASE_FLT_FN (BUILT_IN_CBRT):
14598 CASE_FLT_FN (BUILT_IN_CEIL):
14599 CASE_FLT_FN (BUILT_IN_ERF):
14600 CASE_FLT_FN (BUILT_IN_EXPM1):
14601 CASE_FLT_FN (BUILT_IN_FLOOR):
14602 CASE_FLT_FN (BUILT_IN_FMOD):
14603 CASE_FLT_FN (BUILT_IN_FREXP):
14604 CASE_FLT_FN (BUILT_IN_LCEIL):
14605 CASE_FLT_FN (BUILT_IN_LDEXP):
14606 CASE_FLT_FN (BUILT_IN_LFLOOR):
14607 CASE_FLT_FN (BUILT_IN_LLCEIL):
14608 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14609 CASE_FLT_FN (BUILT_IN_LLRINT):
14610 CASE_FLT_FN (BUILT_IN_LLROUND):
14611 CASE_FLT_FN (BUILT_IN_LRINT):
14612 CASE_FLT_FN (BUILT_IN_LROUND):
14613 CASE_FLT_FN (BUILT_IN_MODF):
14614 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14615 CASE_FLT_FN (BUILT_IN_RINT):
14616 CASE_FLT_FN (BUILT_IN_ROUND):
14617 CASE_FLT_FN (BUILT_IN_SCALB):
14618 CASE_FLT_FN (BUILT_IN_SCALBLN):
14619 CASE_FLT_FN (BUILT_IN_SCALBN):
14620 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14621 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14622 CASE_FLT_FN (BUILT_IN_SINH):
14623 CASE_FLT_FN (BUILT_IN_TANH):
14624 CASE_FLT_FN (BUILT_IN_TRUNC):
14625 /* True if the 1st argument is nonnegative. */
14626 return tree_expr_nonnegative_warnv_p (arg0,
14627 strict_overflow_p);
14629 CASE_FLT_FN (BUILT_IN_FMAX):
14630 /* True if the 1st OR 2nd arguments are nonnegative. */
14631 return (tree_expr_nonnegative_warnv_p (arg0,
14632 strict_overflow_p)
14633 || (tree_expr_nonnegative_warnv_p (arg1,
14634 strict_overflow_p)));
14636 CASE_FLT_FN (BUILT_IN_FMIN):
14637 /* True if the 1st AND 2nd arguments are nonnegative. */
14638 return (tree_expr_nonnegative_warnv_p (arg0,
14639 strict_overflow_p)
14640 && (tree_expr_nonnegative_warnv_p (arg1,
14641 strict_overflow_p)));
14643 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14644 /* True if the 2nd argument is nonnegative. */
14645 return tree_expr_nonnegative_warnv_p (arg1,
14646 strict_overflow_p);
14648 CASE_FLT_FN (BUILT_IN_POWI):
14649 /* True if the 1st argument is nonnegative or the second
14650 argument is an even integer. */
14651 if (TREE_CODE (arg1) == INTEGER_CST
14652 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14653 return true;
14654 return tree_expr_nonnegative_warnv_p (arg0,
14655 strict_overflow_p);
14657 CASE_FLT_FN (BUILT_IN_POW):
14658 /* True if the 1st argument is nonnegative or the second
14659 argument is an even integer valued real. */
14660 if (TREE_CODE (arg1) == REAL_CST)
14662 REAL_VALUE_TYPE c;
14663 HOST_WIDE_INT n;
14665 c = TREE_REAL_CST (arg1);
14666 n = real_to_integer (&c);
14667 if ((n & 1) == 0)
14669 REAL_VALUE_TYPE cint;
14670 real_from_integer (&cint, VOIDmode, n,
14671 n < 0 ? -1 : 0, 0);
14672 if (real_identical (&c, &cint))
14673 return true;
14676 return tree_expr_nonnegative_warnv_p (arg0,
14677 strict_overflow_p);
14679 default:
14680 break;
14682 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14683 type);
14686 /* Return true if T is known to be non-negative. If the return
14687 value is based on the assumption that signed overflow is undefined,
14688 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14689 *STRICT_OVERFLOW_P. */
14691 bool
14692 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14694 enum tree_code code = TREE_CODE (t);
14695 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14696 return true;
14698 switch (code)
14700 case TARGET_EXPR:
14702 tree temp = TARGET_EXPR_SLOT (t);
14703 t = TARGET_EXPR_INITIAL (t);
14705 /* If the initializer is non-void, then it's a normal expression
14706 that will be assigned to the slot. */
14707 if (!VOID_TYPE_P (t))
14708 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14710 /* Otherwise, the initializer sets the slot in some way. One common
14711 way is an assignment statement at the end of the initializer. */
14712 while (1)
14714 if (TREE_CODE (t) == BIND_EXPR)
14715 t = expr_last (BIND_EXPR_BODY (t));
14716 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14717 || TREE_CODE (t) == TRY_CATCH_EXPR)
14718 t = expr_last (TREE_OPERAND (t, 0));
14719 else if (TREE_CODE (t) == STATEMENT_LIST)
14720 t = expr_last (t);
14721 else
14722 break;
14724 if (TREE_CODE (t) == MODIFY_EXPR
14725 && TREE_OPERAND (t, 0) == temp)
14726 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14727 strict_overflow_p);
14729 return false;
14732 case CALL_EXPR:
14734 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14735 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14737 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14738 get_callee_fndecl (t),
14739 arg0,
14740 arg1,
14741 strict_overflow_p);
14743 case COMPOUND_EXPR:
14744 case MODIFY_EXPR:
14745 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14746 strict_overflow_p);
14747 case BIND_EXPR:
14748 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14749 strict_overflow_p);
14750 case SAVE_EXPR:
14751 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14752 strict_overflow_p);
14754 default:
14755 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14756 TREE_TYPE (t));
14759 /* We don't know sign of `t', so be conservative and return false. */
14760 return false;
14763 /* Return true if T is known to be non-negative. If the return
14764 value is based on the assumption that signed overflow is undefined,
14765 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14766 *STRICT_OVERFLOW_P. */
14768 bool
14769 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14771 enum tree_code code;
14772 if (t == error_mark_node)
14773 return false;
14775 code = TREE_CODE (t);
14776 switch (TREE_CODE_CLASS (code))
14778 case tcc_binary:
14779 case tcc_comparison:
14780 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14781 TREE_TYPE (t),
14782 TREE_OPERAND (t, 0),
14783 TREE_OPERAND (t, 1),
14784 strict_overflow_p);
14786 case tcc_unary:
14787 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14788 TREE_TYPE (t),
14789 TREE_OPERAND (t, 0),
14790 strict_overflow_p);
14792 case tcc_constant:
14793 case tcc_declaration:
14794 case tcc_reference:
14795 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14797 default:
14798 break;
14801 switch (code)
14803 case TRUTH_AND_EXPR:
14804 case TRUTH_OR_EXPR:
14805 case TRUTH_XOR_EXPR:
14806 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14807 TREE_TYPE (t),
14808 TREE_OPERAND (t, 0),
14809 TREE_OPERAND (t, 1),
14810 strict_overflow_p);
14811 case TRUTH_NOT_EXPR:
14812 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14813 TREE_TYPE (t),
14814 TREE_OPERAND (t, 0),
14815 strict_overflow_p);
14817 case COND_EXPR:
14818 case CONSTRUCTOR:
14819 case OBJ_TYPE_REF:
14820 case ASSERT_EXPR:
14821 case ADDR_EXPR:
14822 case WITH_SIZE_EXPR:
14823 case EXC_PTR_EXPR:
14824 case SSA_NAME:
14825 case FILTER_EXPR:
14826 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14828 default:
14829 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14833 /* Return true if `t' is known to be non-negative. Handle warnings
14834 about undefined signed overflow. */
14836 bool
14837 tree_expr_nonnegative_p (tree t)
14839 bool ret, strict_overflow_p;
14841 strict_overflow_p = false;
14842 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14843 if (strict_overflow_p)
14844 fold_overflow_warning (("assuming signed overflow does not occur when "
14845 "determining that expression is always "
14846 "non-negative"),
14847 WARN_STRICT_OVERFLOW_MISC);
14848 return ret;
14852 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14853 For floating point we further ensure that T is not denormal.
14854 Similar logic is present in nonzero_address in rtlanal.h.
14856 If the return value is based on the assumption that signed overflow
14857 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14858 change *STRICT_OVERFLOW_P. */
14860 bool
14861 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14862 bool *strict_overflow_p)
14864 switch (code)
14866 case ABS_EXPR:
14867 return tree_expr_nonzero_warnv_p (op0,
14868 strict_overflow_p);
14870 case NOP_EXPR:
14872 tree inner_type = TREE_TYPE (op0);
14873 tree outer_type = type;
14875 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14876 && tree_expr_nonzero_warnv_p (op0,
14877 strict_overflow_p));
14879 break;
14881 case NON_LVALUE_EXPR:
14882 return tree_expr_nonzero_warnv_p (op0,
14883 strict_overflow_p);
14885 default:
14886 break;
14889 return false;
14892 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14893 For floating point we further ensure that T is not denormal.
14894 Similar logic is present in nonzero_address in rtlanal.h.
14896 If the return value is based on the assumption that signed overflow
14897 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14898 change *STRICT_OVERFLOW_P. */
14900 bool
14901 tree_binary_nonzero_warnv_p (enum tree_code code,
14902 tree type,
14903 tree op0,
14904 tree op1, bool *strict_overflow_p)
14906 bool sub_strict_overflow_p;
14907 switch (code)
14909 case POINTER_PLUS_EXPR:
14910 case PLUS_EXPR:
14911 if (TYPE_OVERFLOW_UNDEFINED (type))
14913 /* With the presence of negative values it is hard
14914 to say something. */
14915 sub_strict_overflow_p = false;
14916 if (!tree_expr_nonnegative_warnv_p (op0,
14917 &sub_strict_overflow_p)
14918 || !tree_expr_nonnegative_warnv_p (op1,
14919 &sub_strict_overflow_p))
14920 return false;
14921 /* One of operands must be positive and the other non-negative. */
14922 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14923 overflows, on a twos-complement machine the sum of two
14924 nonnegative numbers can never be zero. */
14925 return (tree_expr_nonzero_warnv_p (op0,
14926 strict_overflow_p)
14927 || tree_expr_nonzero_warnv_p (op1,
14928 strict_overflow_p));
14930 break;
14932 case MULT_EXPR:
14933 if (TYPE_OVERFLOW_UNDEFINED (type))
14935 if (tree_expr_nonzero_warnv_p (op0,
14936 strict_overflow_p)
14937 && tree_expr_nonzero_warnv_p (op1,
14938 strict_overflow_p))
14940 *strict_overflow_p = true;
14941 return true;
14944 break;
14946 case MIN_EXPR:
14947 sub_strict_overflow_p = false;
14948 if (tree_expr_nonzero_warnv_p (op0,
14949 &sub_strict_overflow_p)
14950 && tree_expr_nonzero_warnv_p (op1,
14951 &sub_strict_overflow_p))
14953 if (sub_strict_overflow_p)
14954 *strict_overflow_p = true;
14956 break;
14958 case MAX_EXPR:
14959 sub_strict_overflow_p = false;
14960 if (tree_expr_nonzero_warnv_p (op0,
14961 &sub_strict_overflow_p))
14963 if (sub_strict_overflow_p)
14964 *strict_overflow_p = true;
14966 /* When both operands are nonzero, then MAX must be too. */
14967 if (tree_expr_nonzero_warnv_p (op1,
14968 strict_overflow_p))
14969 return true;
14971 /* MAX where operand 0 is positive is positive. */
14972 return tree_expr_nonnegative_warnv_p (op0,
14973 strict_overflow_p);
14975 /* MAX where operand 1 is positive is positive. */
14976 else if (tree_expr_nonzero_warnv_p (op1,
14977 &sub_strict_overflow_p)
14978 && tree_expr_nonnegative_warnv_p (op1,
14979 &sub_strict_overflow_p))
14981 if (sub_strict_overflow_p)
14982 *strict_overflow_p = true;
14983 return true;
14985 break;
14987 case BIT_IOR_EXPR:
14988 return (tree_expr_nonzero_warnv_p (op1,
14989 strict_overflow_p)
14990 || tree_expr_nonzero_warnv_p (op0,
14991 strict_overflow_p));
14993 default:
14994 break;
14997 return false;
15000 /* Return true when T is an address and is known to be nonzero.
15001 For floating point we further ensure that T is not denormal.
15002 Similar logic is present in nonzero_address in rtlanal.h.
15004 If the return value is based on the assumption that signed overflow
15005 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15006 change *STRICT_OVERFLOW_P. */
15008 bool
15009 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15011 bool sub_strict_overflow_p;
15012 switch (TREE_CODE (t))
15014 case INTEGER_CST:
15015 return !integer_zerop (t);
15017 case ADDR_EXPR:
15019 tree base = get_base_address (TREE_OPERAND (t, 0));
15021 if (!base)
15022 return false;
15024 /* Weak declarations may link to NULL. */
15025 if (VAR_OR_FUNCTION_DECL_P (base))
15026 return !DECL_WEAK (base);
15028 /* Constants are never weak. */
15029 if (CONSTANT_CLASS_P (base))
15030 return true;
15032 return false;
15035 case COND_EXPR:
15036 sub_strict_overflow_p = false;
15037 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15038 &sub_strict_overflow_p)
15039 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15040 &sub_strict_overflow_p))
15042 if (sub_strict_overflow_p)
15043 *strict_overflow_p = true;
15044 return true;
15046 break;
15048 default:
15049 break;
15051 return false;
15054 /* Return true when T is an address and is known to be nonzero.
15055 For floating point we further ensure that T is not denormal.
15056 Similar logic is present in nonzero_address in rtlanal.h.
15058 If the return value is based on the assumption that signed overflow
15059 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15060 change *STRICT_OVERFLOW_P. */
15062 bool
15063 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15065 tree type = TREE_TYPE (t);
15066 enum tree_code code;
15068 /* Doing something useful for floating point would need more work. */
15069 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15070 return false;
15072 code = TREE_CODE (t);
15073 switch (TREE_CODE_CLASS (code))
15075 case tcc_unary:
15076 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15077 strict_overflow_p);
15078 case tcc_binary:
15079 case tcc_comparison:
15080 return tree_binary_nonzero_warnv_p (code, type,
15081 TREE_OPERAND (t, 0),
15082 TREE_OPERAND (t, 1),
15083 strict_overflow_p);
15084 case tcc_constant:
15085 case tcc_declaration:
15086 case tcc_reference:
15087 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15089 default:
15090 break;
15093 switch (code)
15095 case TRUTH_NOT_EXPR:
15096 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15097 strict_overflow_p);
15099 case TRUTH_AND_EXPR:
15100 case TRUTH_OR_EXPR:
15101 case TRUTH_XOR_EXPR:
15102 return tree_binary_nonzero_warnv_p (code, type,
15103 TREE_OPERAND (t, 0),
15104 TREE_OPERAND (t, 1),
15105 strict_overflow_p);
15107 case COND_EXPR:
15108 case CONSTRUCTOR:
15109 case OBJ_TYPE_REF:
15110 case ASSERT_EXPR:
15111 case ADDR_EXPR:
15112 case WITH_SIZE_EXPR:
15113 case EXC_PTR_EXPR:
15114 case SSA_NAME:
15115 case FILTER_EXPR:
15116 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15118 case COMPOUND_EXPR:
15119 case MODIFY_EXPR:
15120 case BIND_EXPR:
15121 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15122 strict_overflow_p);
15124 case SAVE_EXPR:
15125 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15126 strict_overflow_p);
15128 case CALL_EXPR:
15129 return alloca_call_p (t);
15131 default:
15132 break;
15134 return false;
15137 /* Return true when T is an address and is known to be nonzero.
15138 Handle warnings about undefined signed overflow. */
15140 bool
15141 tree_expr_nonzero_p (tree t)
15143 bool ret, strict_overflow_p;
15145 strict_overflow_p = false;
15146 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15147 if (strict_overflow_p)
15148 fold_overflow_warning (("assuming signed overflow does not occur when "
15149 "determining that expression is always "
15150 "non-zero"),
15151 WARN_STRICT_OVERFLOW_MISC);
15152 return ret;
15155 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15156 attempt to fold the expression to a constant without modifying TYPE,
15157 OP0 or OP1.
15159 If the expression could be simplified to a constant, then return
15160 the constant. If the expression would not be simplified to a
15161 constant, then return NULL_TREE. */
15163 tree
15164 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15166 tree tem = fold_binary (code, type, op0, op1);
15167 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15170 /* Given the components of a unary expression CODE, TYPE and OP0,
15171 attempt to fold the expression to a constant without modifying
15172 TYPE or OP0.
15174 If the expression could be simplified to a constant, then return
15175 the constant. If the expression would not be simplified to a
15176 constant, then return NULL_TREE. */
15178 tree
15179 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15181 tree tem = fold_unary (code, type, op0);
15182 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15185 /* If EXP represents referencing an element in a constant string
15186 (either via pointer arithmetic or array indexing), return the
15187 tree representing the value accessed, otherwise return NULL. */
15189 tree
15190 fold_read_from_constant_string (tree exp)
15192 if ((TREE_CODE (exp) == INDIRECT_REF
15193 || TREE_CODE (exp) == ARRAY_REF)
15194 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15196 tree exp1 = TREE_OPERAND (exp, 0);
15197 tree index;
15198 tree string;
15200 if (TREE_CODE (exp) == INDIRECT_REF)
15201 string = string_constant (exp1, &index);
15202 else
15204 tree low_bound = array_ref_low_bound (exp);
15205 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15207 /* Optimize the special-case of a zero lower bound.
15209 We convert the low_bound to sizetype to avoid some problems
15210 with constant folding. (E.g. suppose the lower bound is 1,
15211 and its mode is QI. Without the conversion,l (ARRAY
15212 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15213 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15214 if (! integer_zerop (low_bound))
15215 index = size_diffop (index, fold_convert (sizetype, low_bound));
15217 string = exp1;
15220 if (string
15221 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15222 && TREE_CODE (string) == STRING_CST
15223 && TREE_CODE (index) == INTEGER_CST
15224 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15225 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15226 == MODE_INT)
15227 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15228 return build_int_cst_type (TREE_TYPE (exp),
15229 (TREE_STRING_POINTER (string)
15230 [TREE_INT_CST_LOW (index)]));
15232 return NULL;
15235 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15236 an integer constant, real, or fixed-point constant.
15238 TYPE is the type of the result. */
15240 static tree
15241 fold_negate_const (tree arg0, tree type)
15243 tree t = NULL_TREE;
15245 switch (TREE_CODE (arg0))
15247 case INTEGER_CST:
15249 unsigned HOST_WIDE_INT low;
15250 HOST_WIDE_INT high;
15251 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15252 TREE_INT_CST_HIGH (arg0),
15253 &low, &high);
15254 t = force_fit_type_double (type, low, high, 1,
15255 (overflow | TREE_OVERFLOW (arg0))
15256 && !TYPE_UNSIGNED (type));
15257 break;
15260 case REAL_CST:
15261 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15262 break;
15264 case FIXED_CST:
15266 FIXED_VALUE_TYPE f;
15267 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15268 &(TREE_FIXED_CST (arg0)), NULL,
15269 TYPE_SATURATING (type));
15270 t = build_fixed (type, f);
15271 /* Propagate overflow flags. */
15272 if (overflow_p | TREE_OVERFLOW (arg0))
15274 TREE_OVERFLOW (t) = 1;
15275 TREE_CONSTANT_OVERFLOW (t) = 1;
15277 else if (TREE_CONSTANT_OVERFLOW (arg0))
15278 TREE_CONSTANT_OVERFLOW (t) = 1;
15279 break;
15282 default:
15283 gcc_unreachable ();
15286 return t;
15289 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15290 an integer constant or real constant.
15292 TYPE is the type of the result. */
15294 tree
15295 fold_abs_const (tree arg0, tree type)
15297 tree t = NULL_TREE;
15299 switch (TREE_CODE (arg0))
15301 case INTEGER_CST:
15302 /* If the value is unsigned, then the absolute value is
15303 the same as the ordinary value. */
15304 if (TYPE_UNSIGNED (type))
15305 t = arg0;
15306 /* Similarly, if the value is non-negative. */
15307 else if (INT_CST_LT (integer_minus_one_node, arg0))
15308 t = arg0;
15309 /* If the value is negative, then the absolute value is
15310 its negation. */
15311 else
15313 unsigned HOST_WIDE_INT low;
15314 HOST_WIDE_INT high;
15315 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15316 TREE_INT_CST_HIGH (arg0),
15317 &low, &high);
15318 t = force_fit_type_double (type, low, high, -1,
15319 overflow | TREE_OVERFLOW (arg0));
15321 break;
15323 case REAL_CST:
15324 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15325 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15326 else
15327 t = arg0;
15328 break;
15330 default:
15331 gcc_unreachable ();
15334 return t;
15337 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15338 constant. TYPE is the type of the result. */
15340 static tree
15341 fold_not_const (tree arg0, tree type)
15343 tree t = NULL_TREE;
15345 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15347 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15348 ~TREE_INT_CST_HIGH (arg0), 0,
15349 TREE_OVERFLOW (arg0));
15351 return t;
15354 /* Given CODE, a relational operator, the target type, TYPE and two
15355 constant operands OP0 and OP1, return the result of the
15356 relational operation. If the result is not a compile time
15357 constant, then return NULL_TREE. */
15359 static tree
15360 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15362 int result, invert;
15364 /* From here on, the only cases we handle are when the result is
15365 known to be a constant. */
15367 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15369 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15370 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15372 /* Handle the cases where either operand is a NaN. */
15373 if (real_isnan (c0) || real_isnan (c1))
15375 switch (code)
15377 case EQ_EXPR:
15378 case ORDERED_EXPR:
15379 result = 0;
15380 break;
15382 case NE_EXPR:
15383 case UNORDERED_EXPR:
15384 case UNLT_EXPR:
15385 case UNLE_EXPR:
15386 case UNGT_EXPR:
15387 case UNGE_EXPR:
15388 case UNEQ_EXPR:
15389 result = 1;
15390 break;
15392 case LT_EXPR:
15393 case LE_EXPR:
15394 case GT_EXPR:
15395 case GE_EXPR:
15396 case LTGT_EXPR:
15397 if (flag_trapping_math)
15398 return NULL_TREE;
15399 result = 0;
15400 break;
15402 default:
15403 gcc_unreachable ();
15406 return constant_boolean_node (result, type);
15409 return constant_boolean_node (real_compare (code, c0, c1), type);
15412 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15414 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15415 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15416 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15419 /* Handle equality/inequality of complex constants. */
15420 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15422 tree rcond = fold_relational_const (code, type,
15423 TREE_REALPART (op0),
15424 TREE_REALPART (op1));
15425 tree icond = fold_relational_const (code, type,
15426 TREE_IMAGPART (op0),
15427 TREE_IMAGPART (op1));
15428 if (code == EQ_EXPR)
15429 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15430 else if (code == NE_EXPR)
15431 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15432 else
15433 return NULL_TREE;
15436 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15438 To compute GT, swap the arguments and do LT.
15439 To compute GE, do LT and invert the result.
15440 To compute LE, swap the arguments, do LT and invert the result.
15441 To compute NE, do EQ and invert the result.
15443 Therefore, the code below must handle only EQ and LT. */
15445 if (code == LE_EXPR || code == GT_EXPR)
15447 tree tem = op0;
15448 op0 = op1;
15449 op1 = tem;
15450 code = swap_tree_comparison (code);
15453 /* Note that it is safe to invert for real values here because we
15454 have already handled the one case that it matters. */
15456 invert = 0;
15457 if (code == NE_EXPR || code == GE_EXPR)
15459 invert = 1;
15460 code = invert_tree_comparison (code, false);
15463 /* Compute a result for LT or EQ if args permit;
15464 Otherwise return T. */
15465 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15467 if (code == EQ_EXPR)
15468 result = tree_int_cst_equal (op0, op1);
15469 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15470 result = INT_CST_LT_UNSIGNED (op0, op1);
15471 else
15472 result = INT_CST_LT (op0, op1);
15474 else
15475 return NULL_TREE;
15477 if (invert)
15478 result ^= 1;
15479 return constant_boolean_node (result, type);
15482 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15483 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15484 itself. */
15486 tree
15487 fold_build_cleanup_point_expr (tree type, tree expr)
15489 /* If the expression does not have side effects then we don't have to wrap
15490 it with a cleanup point expression. */
15491 if (!TREE_SIDE_EFFECTS (expr))
15492 return expr;
15494 /* If the expression is a return, check to see if the expression inside the
15495 return has no side effects or the right hand side of the modify expression
15496 inside the return. If either don't have side effects set we don't need to
15497 wrap the expression in a cleanup point expression. Note we don't check the
15498 left hand side of the modify because it should always be a return decl. */
15499 if (TREE_CODE (expr) == RETURN_EXPR)
15501 tree op = TREE_OPERAND (expr, 0);
15502 if (!op || !TREE_SIDE_EFFECTS (op))
15503 return expr;
15504 op = TREE_OPERAND (op, 1);
15505 if (!TREE_SIDE_EFFECTS (op))
15506 return expr;
15509 return build1 (CLEANUP_POINT_EXPR, type, expr);
15512 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15513 of an indirection through OP0, or NULL_TREE if no simplification is
15514 possible. */
15516 tree
15517 fold_indirect_ref_1 (tree type, tree op0)
15519 tree sub = op0;
15520 tree subtype;
15522 STRIP_NOPS (sub);
15523 subtype = TREE_TYPE (sub);
15524 if (!POINTER_TYPE_P (subtype))
15525 return NULL_TREE;
15527 if (TREE_CODE (sub) == ADDR_EXPR)
15529 tree op = TREE_OPERAND (sub, 0);
15530 tree optype = TREE_TYPE (op);
15531 /* *&CONST_DECL -> to the value of the const decl. */
15532 if (TREE_CODE (op) == CONST_DECL)
15533 return DECL_INITIAL (op);
15534 /* *&p => p; make sure to handle *&"str"[cst] here. */
15535 if (type == optype)
15537 tree fop = fold_read_from_constant_string (op);
15538 if (fop)
15539 return fop;
15540 else
15541 return op;
15543 /* *(foo *)&fooarray => fooarray[0] */
15544 else if (TREE_CODE (optype) == ARRAY_TYPE
15545 && type == TREE_TYPE (optype))
15547 tree type_domain = TYPE_DOMAIN (optype);
15548 tree min_val = size_zero_node;
15549 if (type_domain && TYPE_MIN_VALUE (type_domain))
15550 min_val = TYPE_MIN_VALUE (type_domain);
15551 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15553 /* *(foo *)&complexfoo => __real__ complexfoo */
15554 else if (TREE_CODE (optype) == COMPLEX_TYPE
15555 && type == TREE_TYPE (optype))
15556 return fold_build1 (REALPART_EXPR, type, op);
15557 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15558 else if (TREE_CODE (optype) == VECTOR_TYPE
15559 && type == TREE_TYPE (optype))
15561 tree part_width = TYPE_SIZE (type);
15562 tree index = bitsize_int (0);
15563 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15567 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15568 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15569 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15571 tree op00 = TREE_OPERAND (sub, 0);
15572 tree op01 = TREE_OPERAND (sub, 1);
15573 tree op00type;
15575 STRIP_NOPS (op00);
15576 op00type = TREE_TYPE (op00);
15577 if (TREE_CODE (op00) == ADDR_EXPR
15578 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15579 && type == TREE_TYPE (TREE_TYPE (op00type)))
15581 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15582 tree part_width = TYPE_SIZE (type);
15583 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15584 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15585 tree index = bitsize_int (indexi);
15587 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15588 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15589 part_width, index);
15595 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15596 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15597 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15599 tree op00 = TREE_OPERAND (sub, 0);
15600 tree op01 = TREE_OPERAND (sub, 1);
15601 tree op00type;
15603 STRIP_NOPS (op00);
15604 op00type = TREE_TYPE (op00);
15605 if (TREE_CODE (op00) == ADDR_EXPR
15606 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15607 && type == TREE_TYPE (TREE_TYPE (op00type)))
15609 tree size = TYPE_SIZE_UNIT (type);
15610 if (tree_int_cst_equal (size, op01))
15611 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15615 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15616 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15617 && type == TREE_TYPE (TREE_TYPE (subtype)))
15619 tree type_domain;
15620 tree min_val = size_zero_node;
15621 sub = build_fold_indirect_ref (sub);
15622 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15623 if (type_domain && TYPE_MIN_VALUE (type_domain))
15624 min_val = TYPE_MIN_VALUE (type_domain);
15625 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15628 return NULL_TREE;
15631 /* Builds an expression for an indirection through T, simplifying some
15632 cases. */
15634 tree
15635 build_fold_indirect_ref (tree t)
15637 tree type = TREE_TYPE (TREE_TYPE (t));
15638 tree sub = fold_indirect_ref_1 (type, t);
15640 if (sub)
15641 return sub;
15642 else
15643 return build1 (INDIRECT_REF, type, t);
15646 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15648 tree
15649 fold_indirect_ref (tree t)
15651 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15653 if (sub)
15654 return sub;
15655 else
15656 return t;
15659 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15660 whose result is ignored. The type of the returned tree need not be
15661 the same as the original expression. */
15663 tree
15664 fold_ignored_result (tree t)
15666 if (!TREE_SIDE_EFFECTS (t))
15667 return integer_zero_node;
15669 for (;;)
15670 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15672 case tcc_unary:
15673 t = TREE_OPERAND (t, 0);
15674 break;
15676 case tcc_binary:
15677 case tcc_comparison:
15678 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15679 t = TREE_OPERAND (t, 0);
15680 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15681 t = TREE_OPERAND (t, 1);
15682 else
15683 return t;
15684 break;
15686 case tcc_expression:
15687 switch (TREE_CODE (t))
15689 case COMPOUND_EXPR:
15690 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15691 return t;
15692 t = TREE_OPERAND (t, 0);
15693 break;
15695 case COND_EXPR:
15696 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15697 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15698 return t;
15699 t = TREE_OPERAND (t, 0);
15700 break;
15702 default:
15703 return t;
15705 break;
15707 default:
15708 return t;
15712 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15713 This can only be applied to objects of a sizetype. */
15715 tree
15716 round_up (tree value, int divisor)
15718 tree div = NULL_TREE;
15720 gcc_assert (divisor > 0);
15721 if (divisor == 1)
15722 return value;
15724 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15725 have to do anything. Only do this when we are not given a const,
15726 because in that case, this check is more expensive than just
15727 doing it. */
15728 if (TREE_CODE (value) != INTEGER_CST)
15730 div = build_int_cst (TREE_TYPE (value), divisor);
15732 if (multiple_of_p (TREE_TYPE (value), value, div))
15733 return value;
15736 /* If divisor is a power of two, simplify this to bit manipulation. */
15737 if (divisor == (divisor & -divisor))
15739 if (TREE_CODE (value) == INTEGER_CST)
15741 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15742 unsigned HOST_WIDE_INT high;
15743 bool overflow_p;
15745 if ((low & (divisor - 1)) == 0)
15746 return value;
15748 overflow_p = TREE_OVERFLOW (value);
15749 high = TREE_INT_CST_HIGH (value);
15750 low &= ~(divisor - 1);
15751 low += divisor;
15752 if (low == 0)
15754 high++;
15755 if (high == 0)
15756 overflow_p = true;
15759 return force_fit_type_double (TREE_TYPE (value), low, high,
15760 -1, overflow_p);
15762 else
15764 tree t;
15766 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15767 value = size_binop (PLUS_EXPR, value, t);
15768 t = build_int_cst (TREE_TYPE (value), -divisor);
15769 value = size_binop (BIT_AND_EXPR, value, t);
15772 else
15774 if (!div)
15775 div = build_int_cst (TREE_TYPE (value), divisor);
15776 value = size_binop (CEIL_DIV_EXPR, value, div);
15777 value = size_binop (MULT_EXPR, value, div);
15780 return value;
15783 /* Likewise, but round down. */
15785 tree
15786 round_down (tree value, int divisor)
15788 tree div = NULL_TREE;
15790 gcc_assert (divisor > 0);
15791 if (divisor == 1)
15792 return value;
15794 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15795 have to do anything. Only do this when we are not given a const,
15796 because in that case, this check is more expensive than just
15797 doing it. */
15798 if (TREE_CODE (value) != INTEGER_CST)
15800 div = build_int_cst (TREE_TYPE (value), divisor);
15802 if (multiple_of_p (TREE_TYPE (value), value, div))
15803 return value;
15806 /* If divisor is a power of two, simplify this to bit manipulation. */
15807 if (divisor == (divisor & -divisor))
15809 tree t;
15811 t = build_int_cst (TREE_TYPE (value), -divisor);
15812 value = size_binop (BIT_AND_EXPR, value, t);
15814 else
15816 if (!div)
15817 div = build_int_cst (TREE_TYPE (value), divisor);
15818 value = size_binop (FLOOR_DIV_EXPR, value, div);
15819 value = size_binop (MULT_EXPR, value, div);
15822 return value;
15825 /* Returns the pointer to the base of the object addressed by EXP and
15826 extracts the information about the offset of the access, storing it
15827 to PBITPOS and POFFSET. */
15829 static tree
15830 split_address_to_core_and_offset (tree exp,
15831 HOST_WIDE_INT *pbitpos, tree *poffset)
15833 tree core;
15834 enum machine_mode mode;
15835 int unsignedp, volatilep;
15836 HOST_WIDE_INT bitsize;
15838 if (TREE_CODE (exp) == ADDR_EXPR)
15840 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15841 poffset, &mode, &unsignedp, &volatilep,
15842 false);
15843 core = fold_addr_expr (core);
15845 else
15847 core = exp;
15848 *pbitpos = 0;
15849 *poffset = NULL_TREE;
15852 return core;
15855 /* Returns true if addresses of E1 and E2 differ by a constant, false
15856 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15858 bool
15859 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15861 tree core1, core2;
15862 HOST_WIDE_INT bitpos1, bitpos2;
15863 tree toffset1, toffset2, tdiff, type;
15865 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15866 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15868 if (bitpos1 % BITS_PER_UNIT != 0
15869 || bitpos2 % BITS_PER_UNIT != 0
15870 || !operand_equal_p (core1, core2, 0))
15871 return false;
15873 if (toffset1 && toffset2)
15875 type = TREE_TYPE (toffset1);
15876 if (type != TREE_TYPE (toffset2))
15877 toffset2 = fold_convert (type, toffset2);
15879 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15880 if (!cst_and_fits_in_hwi (tdiff))
15881 return false;
15883 *diff = int_cst_value (tdiff);
15885 else if (toffset1 || toffset2)
15887 /* If only one of the offsets is non-constant, the difference cannot
15888 be a constant. */
15889 return false;
15891 else
15892 *diff = 0;
15894 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15895 return true;
15898 /* Simplify the floating point expression EXP when the sign of the
15899 result is not significant. Return NULL_TREE if no simplification
15900 is possible. */
15902 tree
15903 fold_strip_sign_ops (tree exp)
15905 tree arg0, arg1;
15907 switch (TREE_CODE (exp))
15909 case ABS_EXPR:
15910 case NEGATE_EXPR:
15911 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15912 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15914 case MULT_EXPR:
15915 case RDIV_EXPR:
15916 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15917 return NULL_TREE;
15918 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15919 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15920 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15921 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15922 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15923 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15924 break;
15926 case COMPOUND_EXPR:
15927 arg0 = TREE_OPERAND (exp, 0);
15928 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15929 if (arg1)
15930 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15931 break;
15933 case COND_EXPR:
15934 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15935 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15936 if (arg0 || arg1)
15937 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15938 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15939 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15940 break;
15942 case CALL_EXPR:
15944 const enum built_in_function fcode = builtin_mathfn_code (exp);
15945 switch (fcode)
15947 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15948 /* Strip copysign function call, return the 1st argument. */
15949 arg0 = CALL_EXPR_ARG (exp, 0);
15950 arg1 = CALL_EXPR_ARG (exp, 1);
15951 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15953 default:
15954 /* Strip sign ops from the argument of "odd" math functions. */
15955 if (negate_mathfn_p (fcode))
15957 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15958 if (arg0)
15959 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15961 break;
15964 break;
15966 default:
15967 break;
15969 return NULL_TREE;