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, 2010
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
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
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
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. */
52 #include "coretypes.h"
57 #include "fixed-value.h"
66 #include "langhooks.h"
70 /* Nonzero if we are folding constants inside an initializer; zero
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 {
96 static bool negate_mathfn_p (enum built_in_function);
97 static bool negate_expr_p (tree);
98 static tree negate_expr (tree);
99 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
100 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
101 static tree const_binop (enum tree_code, tree, tree, int);
102 static enum comparison_code comparison_to_compcode (enum tree_code);
103 static enum tree_code compcode_to_comparison (enum comparison_code);
104 static int operand_equal_for_comparison_p (tree, tree, tree);
105 static int twoval_comparison_p (tree, tree *, tree *, int *);
106 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
107 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
108 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
109 static tree make_bit_field_ref (location_t, tree, tree,
110 HOST_WIDE_INT, HOST_WIDE_INT, int);
111 static tree optimize_bit_field_compare (location_t, enum tree_code,
113 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
115 enum machine_mode *, int *, int *,
117 static int all_ones_mask_p (const_tree, int);
118 static tree sign_bit_p (tree, const_tree);
119 static int simple_operand_p (const_tree);
120 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
121 static tree range_predecessor (tree);
122 static tree range_successor (tree);
123 extern tree make_range (tree, int *, tree *, tree *, bool *);
124 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (location_t, enum tree_code,
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (location_t,
135 enum tree_code, tree,
138 static tree fold_mathfn_compare (location_t,
139 enum built_in_function, enum tree_code,
141 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
142 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
143 static bool reorder_operands_p (const_tree, const_tree);
144 static tree fold_negate_const (tree, tree);
145 static tree fold_not_const (tree, tree);
146 static tree fold_relational_const (enum tree_code, tree, tree, tree);
147 static tree fold_convert_const (enum tree_code, tree, tree);
150 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
151 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
152 and SUM1. Then this yields nonzero if overflow occurred during the
155 Overflow occurs if A and B have the same sign, but A and SUM differ in
156 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
158 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
160 /* If ARG2 divides ARG1 with zero remainder, carries out the division
161 of type CODE and returns the quotient.
162 Otherwise returns NULL_TREE. */
165 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
170 /* The sign of the division is according to operand two, that
171 does the correct thing for POINTER_PLUS_EXPR where we want
172 a signed division. */
173 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
174 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
175 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
178 quo = double_int_divmod (tree_to_double_int (arg1),
179 tree_to_double_int (arg2),
182 if (double_int_zero_p (rem))
183 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
230 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
252 if (gimple_no_warning_p (stmt))
255 /* Use the smallest code level when deciding to issue the
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
264 locus = input_location;
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
310 negate_mathfn_p (enum built_in_function code)
314 CASE_FLT_FN (BUILT_IN_ASIN):
315 CASE_FLT_FN (BUILT_IN_ASINH):
316 CASE_FLT_FN (BUILT_IN_ATAN):
317 CASE_FLT_FN (BUILT_IN_ATANH):
318 CASE_FLT_FN (BUILT_IN_CASIN):
319 CASE_FLT_FN (BUILT_IN_CASINH):
320 CASE_FLT_FN (BUILT_IN_CATAN):
321 CASE_FLT_FN (BUILT_IN_CATANH):
322 CASE_FLT_FN (BUILT_IN_CBRT):
323 CASE_FLT_FN (BUILT_IN_CPROJ):
324 CASE_FLT_FN (BUILT_IN_CSIN):
325 CASE_FLT_FN (BUILT_IN_CSINH):
326 CASE_FLT_FN (BUILT_IN_CTAN):
327 CASE_FLT_FN (BUILT_IN_CTANH):
328 CASE_FLT_FN (BUILT_IN_ERF):
329 CASE_FLT_FN (BUILT_IN_LLROUND):
330 CASE_FLT_FN (BUILT_IN_LROUND):
331 CASE_FLT_FN (BUILT_IN_ROUND):
332 CASE_FLT_FN (BUILT_IN_SIN):
333 CASE_FLT_FN (BUILT_IN_SINH):
334 CASE_FLT_FN (BUILT_IN_TAN):
335 CASE_FLT_FN (BUILT_IN_TANH):
336 CASE_FLT_FN (BUILT_IN_TRUNC):
339 CASE_FLT_FN (BUILT_IN_LLRINT):
340 CASE_FLT_FN (BUILT_IN_LRINT):
341 CASE_FLT_FN (BUILT_IN_NEARBYINT):
342 CASE_FLT_FN (BUILT_IN_RINT):
343 return !flag_rounding_math;
351 /* Check whether we may negate an integer constant T without causing
355 may_negate_without_overflow_p (const_tree t)
357 unsigned HOST_WIDE_INT val;
361 gcc_assert (TREE_CODE (t) == INTEGER_CST);
363 type = TREE_TYPE (t);
364 if (TYPE_UNSIGNED (type))
367 prec = TYPE_PRECISION (type);
368 if (prec > HOST_BITS_PER_WIDE_INT)
370 if (TREE_INT_CST_LOW (t) != 0)
372 prec -= HOST_BITS_PER_WIDE_INT;
373 val = TREE_INT_CST_HIGH (t);
376 val = TREE_INT_CST_LOW (t);
377 if (prec < HOST_BITS_PER_WIDE_INT)
378 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
379 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
382 /* Determine whether an expression T can be cheaply negated using
383 the function negate_expr without introducing undefined overflow. */
386 negate_expr_p (tree t)
393 type = TREE_TYPE (t);
396 switch (TREE_CODE (t))
399 if (TYPE_OVERFLOW_WRAPS (type))
402 /* Check that -CST will not overflow type. */
403 return may_negate_without_overflow_p (t);
405 return (INTEGRAL_TYPE_P (type)
406 && TYPE_OVERFLOW_WRAPS (type));
413 /* We want to canonicalize to positive real constants. Pretend
414 that only negative ones can be easily negated. */
415 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
418 return negate_expr_p (TREE_REALPART (t))
419 && negate_expr_p (TREE_IMAGPART (t));
422 return negate_expr_p (TREE_OPERAND (t, 0))
423 && negate_expr_p (TREE_OPERAND (t, 1));
426 return negate_expr_p (TREE_OPERAND (t, 0));
429 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
430 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
432 /* -(A + B) -> (-B) - A. */
433 if (negate_expr_p (TREE_OPERAND (t, 1))
434 && reorder_operands_p (TREE_OPERAND (t, 0),
435 TREE_OPERAND (t, 1)))
437 /* -(A + B) -> (-A) - B. */
438 return negate_expr_p (TREE_OPERAND (t, 0));
441 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
443 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
444 && reorder_operands_p (TREE_OPERAND (t, 0),
445 TREE_OPERAND (t, 1));
448 if (TYPE_UNSIGNED (TREE_TYPE (t)))
454 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
455 return negate_expr_p (TREE_OPERAND (t, 1))
456 || negate_expr_p (TREE_OPERAND (t, 0));
464 /* In general we can't negate A / B, because if A is INT_MIN and
465 B is 1, we may turn this into INT_MIN / -1 which is undefined
466 and actually traps on some architectures. But if overflow is
467 undefined, we can negate, because - (INT_MIN / 1) is an
469 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
470 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
472 return negate_expr_p (TREE_OPERAND (t, 1))
473 || negate_expr_p (TREE_OPERAND (t, 0));
476 /* Negate -((double)float) as (double)(-float). */
477 if (TREE_CODE (type) == REAL_TYPE)
479 tree tem = strip_float_extensions (t);
481 return negate_expr_p (tem);
486 /* Negate -f(x) as f(-x). */
487 if (negate_mathfn_p (builtin_mathfn_code (t)))
488 return negate_expr_p (CALL_EXPR_ARG (t, 0));
492 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
493 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
495 tree op1 = TREE_OPERAND (t, 1);
496 if (TREE_INT_CST_HIGH (op1) == 0
497 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
498 == TREE_INT_CST_LOW (op1))
509 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
510 simplification is possible.
511 If negate_expr_p would return true for T, NULL_TREE will never be
515 fold_negate_expr (location_t loc, tree t)
517 tree type = TREE_TYPE (t);
520 switch (TREE_CODE (t))
522 /* Convert - (~A) to A + 1. */
524 if (INTEGRAL_TYPE_P (type))
525 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
526 build_int_cst (type, 1));
530 tem = fold_negate_const (t, type);
531 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
532 || !TYPE_OVERFLOW_TRAPS (type))
537 tem = fold_negate_const (t, type);
538 /* Two's complement FP formats, such as c4x, may overflow. */
539 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
544 tem = fold_negate_const (t, type);
549 tree rpart = negate_expr (TREE_REALPART (t));
550 tree ipart = negate_expr (TREE_IMAGPART (t));
552 if ((TREE_CODE (rpart) == REAL_CST
553 && TREE_CODE (ipart) == REAL_CST)
554 || (TREE_CODE (rpart) == INTEGER_CST
555 && TREE_CODE (ipart) == INTEGER_CST))
556 return build_complex (type, rpart, ipart);
561 if (negate_expr_p (t))
562 return fold_build2_loc (loc, COMPLEX_EXPR, type,
563 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
564 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
568 if (negate_expr_p (t))
569 return fold_build1_loc (loc, CONJ_EXPR, type,
570 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
574 return TREE_OPERAND (t, 0);
577 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
578 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
580 /* -(A + B) -> (-B) - A. */
581 if (negate_expr_p (TREE_OPERAND (t, 1))
582 && reorder_operands_p (TREE_OPERAND (t, 0),
583 TREE_OPERAND (t, 1)))
585 tem = negate_expr (TREE_OPERAND (t, 1));
586 return fold_build2_loc (loc, MINUS_EXPR, type,
587 tem, TREE_OPERAND (t, 0));
590 /* -(A + B) -> (-A) - B. */
591 if (negate_expr_p (TREE_OPERAND (t, 0)))
593 tem = negate_expr (TREE_OPERAND (t, 0));
594 return fold_build2_loc (loc, MINUS_EXPR, type,
595 tem, TREE_OPERAND (t, 1));
601 /* - (A - B) -> B - A */
602 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
603 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
604 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
605 return fold_build2_loc (loc, MINUS_EXPR, type,
606 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
610 if (TYPE_UNSIGNED (type))
616 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
618 tem = TREE_OPERAND (t, 1);
619 if (negate_expr_p (tem))
620 return fold_build2_loc (loc, TREE_CODE (t), type,
621 TREE_OPERAND (t, 0), negate_expr (tem));
622 tem = TREE_OPERAND (t, 0);
623 if (negate_expr_p (tem))
624 return fold_build2_loc (loc, TREE_CODE (t), type,
625 negate_expr (tem), TREE_OPERAND (t, 1));
634 /* In general we can't negate A / B, because if A is INT_MIN and
635 B is 1, we may turn this into INT_MIN / -1 which is undefined
636 and actually traps on some architectures. But if overflow is
637 undefined, we can negate, because - (INT_MIN / 1) is an
639 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
641 const char * const warnmsg = G_("assuming signed overflow does not "
642 "occur when negating a division");
643 tem = TREE_OPERAND (t, 1);
644 if (negate_expr_p (tem))
646 if (INTEGRAL_TYPE_P (type)
647 && (TREE_CODE (tem) != INTEGER_CST
648 || integer_onep (tem)))
649 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
650 return fold_build2_loc (loc, TREE_CODE (t), type,
651 TREE_OPERAND (t, 0), negate_expr (tem));
653 tem = TREE_OPERAND (t, 0);
654 if (negate_expr_p (tem))
656 if (INTEGRAL_TYPE_P (type)
657 && (TREE_CODE (tem) != INTEGER_CST
658 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
659 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
660 return fold_build2_loc (loc, TREE_CODE (t), type,
661 negate_expr (tem), TREE_OPERAND (t, 1));
667 /* Convert -((double)float) into (double)(-float). */
668 if (TREE_CODE (type) == REAL_TYPE)
670 tem = strip_float_extensions (t);
671 if (tem != t && negate_expr_p (tem))
672 return fold_convert_loc (loc, type, negate_expr (tem));
677 /* Negate -f(x) as f(-x). */
678 if (negate_mathfn_p (builtin_mathfn_code (t))
679 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
683 fndecl = get_callee_fndecl (t);
684 arg = negate_expr (CALL_EXPR_ARG (t, 0));
685 return build_call_expr_loc (loc, fndecl, 1, arg);
690 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
691 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
693 tree op1 = TREE_OPERAND (t, 1);
694 if (TREE_INT_CST_HIGH (op1) == 0
695 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
696 == TREE_INT_CST_LOW (op1))
698 tree ntype = TYPE_UNSIGNED (type)
699 ? signed_type_for (type)
700 : unsigned_type_for (type);
701 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
702 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
703 return fold_convert_loc (loc, type, temp);
715 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
716 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
728 loc = EXPR_LOCATION (t);
729 type = TREE_TYPE (t);
732 tem = fold_negate_expr (loc, t);
735 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
736 SET_EXPR_LOCATION (tem, loc);
738 return fold_convert_loc (loc, type, tem);
741 /* Split a tree IN into a constant, literal and variable parts that could be
742 combined with CODE to make IN. "constant" means an expression with
743 TREE_CONSTANT but that isn't an actual constant. CODE must be a
744 commutative arithmetic operation. Store the constant part into *CONP,
745 the literal in *LITP and return the variable part. If a part isn't
746 present, set it to null. If the tree does not decompose in this way,
747 return the entire tree as the variable part and the other parts as null.
749 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
750 case, we negate an operand that was subtracted. Except if it is a
751 literal for which we use *MINUS_LITP instead.
753 If NEGATE_P is true, we are negating all of IN, again except a literal
754 for which we use *MINUS_LITP instead.
756 If IN is itself a literal or constant, return it as appropriate.
758 Note that we do not guarantee that any of the three values will be the
759 same type as IN, but they will have the same signedness and mode. */
762 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
763 tree *minus_litp, int negate_p)
771 /* Strip any conversions that don't change the machine mode or signedness. */
772 STRIP_SIGN_NOPS (in);
774 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
775 || TREE_CODE (in) == FIXED_CST)
777 else if (TREE_CODE (in) == code
778 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
779 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
780 /* We can associate addition and subtraction together (even
781 though the C standard doesn't say so) for integers because
782 the value is not affected. For reals, the value might be
783 affected, so we can't. */
784 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
785 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
787 tree op0 = TREE_OPERAND (in, 0);
788 tree op1 = TREE_OPERAND (in, 1);
789 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
790 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
792 /* First see if either of the operands is a literal, then a constant. */
793 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
794 || TREE_CODE (op0) == FIXED_CST)
795 *litp = op0, op0 = 0;
796 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
797 || TREE_CODE (op1) == FIXED_CST)
798 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
800 if (op0 != 0 && TREE_CONSTANT (op0))
801 *conp = op0, op0 = 0;
802 else if (op1 != 0 && TREE_CONSTANT (op1))
803 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
805 /* If we haven't dealt with either operand, this is not a case we can
806 decompose. Otherwise, VAR is either of the ones remaining, if any. */
807 if (op0 != 0 && op1 != 0)
812 var = op1, neg_var_p = neg1_p;
814 /* Now do any needed negations. */
816 *minus_litp = *litp, *litp = 0;
818 *conp = negate_expr (*conp);
820 var = negate_expr (var);
822 else if (TREE_CONSTANT (in))
830 *minus_litp = *litp, *litp = 0;
831 else if (*minus_litp)
832 *litp = *minus_litp, *minus_litp = 0;
833 *conp = negate_expr (*conp);
834 var = negate_expr (var);
840 /* Re-associate trees split by the above function. T1 and T2 are
841 either expressions to associate or null. Return the new
842 expression, if any. LOC is the location of the new expression. If
843 we build an operation, do it in TYPE and with CODE. */
846 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
855 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
856 try to fold this since we will have infinite recursion. But do
857 deal with any NEGATE_EXPRs. */
858 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
859 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
861 if (code == PLUS_EXPR)
863 if (TREE_CODE (t1) == NEGATE_EXPR)
864 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
865 fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
866 else if (TREE_CODE (t2) == NEGATE_EXPR)
867 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
868 fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
869 else if (integer_zerop (t2))
870 return fold_convert_loc (loc, type, t1);
872 else if (code == MINUS_EXPR)
874 if (integer_zerop (t2))
875 return fold_convert_loc (loc, type, t1);
878 tem = build2 (code, type, fold_convert_loc (loc, type, t1),
879 fold_convert_loc (loc, type, t2));
880 goto associate_trees_exit;
883 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
884 fold_convert_loc (loc, type, t2));
885 associate_trees_exit:
886 protected_set_expr_location (tem, loc);
890 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
891 for use in int_const_binop, size_binop and size_diffop. */
894 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
896 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
898 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
913 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
914 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
915 && TYPE_MODE (type1) == TYPE_MODE (type2);
919 /* Combine two integer constants ARG1 and ARG2 under operation CODE
920 to produce a new constant. Return NULL_TREE if we don't know how
921 to evaluate CODE at compile-time.
923 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
926 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
928 unsigned HOST_WIDE_INT int1l, int2l;
929 HOST_WIDE_INT int1h, int2h;
930 unsigned HOST_WIDE_INT low;
932 unsigned HOST_WIDE_INT garbagel;
933 HOST_WIDE_INT garbageh;
935 tree type = TREE_TYPE (arg1);
936 int uns = TYPE_UNSIGNED (type);
938 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
941 int1l = TREE_INT_CST_LOW (arg1);
942 int1h = TREE_INT_CST_HIGH (arg1);
943 int2l = TREE_INT_CST_LOW (arg2);
944 int2h = TREE_INT_CST_HIGH (arg2);
949 low = int1l | int2l, hi = int1h | int2h;
953 low = int1l ^ int2l, hi = int1h ^ int2h;
957 low = int1l & int2l, hi = int1h & int2h;
963 /* It's unclear from the C standard whether shifts can overflow.
964 The following code ignores overflow; perhaps a C standard
965 interpretation ruling is needed. */
966 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
973 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
978 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
982 neg_double (int2l, int2h, &low, &hi);
983 add_double (int1l, int1h, low, hi, &low, &hi);
984 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
988 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
992 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
994 /* This is a shortcut for a common special case. */
995 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
996 && !TREE_OVERFLOW (arg1)
997 && !TREE_OVERFLOW (arg2)
998 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1000 if (code == CEIL_DIV_EXPR)
1003 low = int1l / int2l, hi = 0;
1007 /* ... fall through ... */
1009 case ROUND_DIV_EXPR:
1010 if (int2h == 0 && int2l == 0)
1012 if (int2h == 0 && int2l == 1)
1014 low = int1l, hi = int1h;
1017 if (int1l == int2l && int1h == int2h
1018 && ! (int1l == 0 && int1h == 0))
1023 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1024 &low, &hi, &garbagel, &garbageh);
1027 case TRUNC_MOD_EXPR:
1028 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1029 /* This is a shortcut for a common special case. */
1030 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1031 && !TREE_OVERFLOW (arg1)
1032 && !TREE_OVERFLOW (arg2)
1033 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1035 if (code == CEIL_MOD_EXPR)
1037 low = int1l % int2l, hi = 0;
1041 /* ... fall through ... */
1043 case ROUND_MOD_EXPR:
1044 if (int2h == 0 && int2l == 0)
1046 overflow = div_and_round_double (code, uns,
1047 int1l, int1h, int2l, int2h,
1048 &garbagel, &garbageh, &low, &hi);
1054 low = (((unsigned HOST_WIDE_INT) int1h
1055 < (unsigned HOST_WIDE_INT) int2h)
1056 || (((unsigned HOST_WIDE_INT) int1h
1057 == (unsigned HOST_WIDE_INT) int2h)
1060 low = (int1h < int2h
1061 || (int1h == int2h && int1l < int2l));
1063 if (low == (code == MIN_EXPR))
1064 low = int1l, hi = int1h;
1066 low = int2l, hi = int2h;
1075 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1077 /* Propagate overflow flags ourselves. */
1078 if (((!uns || is_sizetype) && overflow)
1079 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1082 TREE_OVERFLOW (t) = 1;
1086 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1087 ((!uns || is_sizetype) && overflow)
1088 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1093 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1094 constant. We assume ARG1 and ARG2 have the same data type, or at least
1095 are the same kind of constant and the same machine mode. Return zero if
1096 combining the constants is not allowed in the current operating mode.
1098 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1101 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1103 /* Sanity check for the recursive cases. */
1110 if (TREE_CODE (arg1) == INTEGER_CST)
1111 return int_const_binop (code, arg1, arg2, notrunc);
1113 if (TREE_CODE (arg1) == REAL_CST)
1115 enum machine_mode mode;
1118 REAL_VALUE_TYPE value;
1119 REAL_VALUE_TYPE result;
1123 /* The following codes are handled by real_arithmetic. */
1138 d1 = TREE_REAL_CST (arg1);
1139 d2 = TREE_REAL_CST (arg2);
1141 type = TREE_TYPE (arg1);
1142 mode = TYPE_MODE (type);
1144 /* Don't perform operation if we honor signaling NaNs and
1145 either operand is a NaN. */
1146 if (HONOR_SNANS (mode)
1147 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1150 /* Don't perform operation if it would raise a division
1151 by zero exception. */
1152 if (code == RDIV_EXPR
1153 && REAL_VALUES_EQUAL (d2, dconst0)
1154 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1157 /* If either operand is a NaN, just return it. Otherwise, set up
1158 for floating-point trap; we return an overflow. */
1159 if (REAL_VALUE_ISNAN (d1))
1161 else if (REAL_VALUE_ISNAN (d2))
1164 inexact = real_arithmetic (&value, code, &d1, &d2);
1165 real_convert (&result, mode, &value);
1167 /* Don't constant fold this floating point operation if
1168 the result has overflowed and flag_trapping_math. */
1169 if (flag_trapping_math
1170 && MODE_HAS_INFINITIES (mode)
1171 && REAL_VALUE_ISINF (result)
1172 && !REAL_VALUE_ISINF (d1)
1173 && !REAL_VALUE_ISINF (d2))
1176 /* Don't constant fold this floating point operation if the
1177 result may dependent upon the run-time rounding mode and
1178 flag_rounding_math is set, or if GCC's software emulation
1179 is unable to accurately represent the result. */
1180 if ((flag_rounding_math
1181 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1182 && (inexact || !real_identical (&result, &value)))
1185 t = build_real (type, result);
1187 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1191 if (TREE_CODE (arg1) == FIXED_CST)
1193 FIXED_VALUE_TYPE f1;
1194 FIXED_VALUE_TYPE f2;
1195 FIXED_VALUE_TYPE result;
1200 /* The following codes are handled by fixed_arithmetic. */
1206 case TRUNC_DIV_EXPR:
1207 f2 = TREE_FIXED_CST (arg2);
1212 f2.data.high = TREE_INT_CST_HIGH (arg2);
1213 f2.data.low = TREE_INT_CST_LOW (arg2);
1221 f1 = TREE_FIXED_CST (arg1);
1222 type = TREE_TYPE (arg1);
1223 sat_p = TYPE_SATURATING (type);
1224 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1225 t = build_fixed (type, result);
1226 /* Propagate overflow flags. */
1227 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1228 TREE_OVERFLOW (t) = 1;
1232 if (TREE_CODE (arg1) == COMPLEX_CST)
1234 tree type = TREE_TYPE (arg1);
1235 tree r1 = TREE_REALPART (arg1);
1236 tree i1 = TREE_IMAGPART (arg1);
1237 tree r2 = TREE_REALPART (arg2);
1238 tree i2 = TREE_IMAGPART (arg2);
1245 real = const_binop (code, r1, r2, notrunc);
1246 imag = const_binop (code, i1, i2, notrunc);
1250 if (COMPLEX_FLOAT_TYPE_P (type))
1251 return do_mpc_arg2 (arg1, arg2, type,
1252 /* do_nonfinite= */ folding_initializer,
1255 real = const_binop (MINUS_EXPR,
1256 const_binop (MULT_EXPR, r1, r2, notrunc),
1257 const_binop (MULT_EXPR, i1, i2, notrunc),
1259 imag = const_binop (PLUS_EXPR,
1260 const_binop (MULT_EXPR, r1, i2, notrunc),
1261 const_binop (MULT_EXPR, i1, r2, notrunc),
1266 if (COMPLEX_FLOAT_TYPE_P (type))
1267 return do_mpc_arg2 (arg1, arg2, type,
1268 /* do_nonfinite= */ folding_initializer,
1271 case TRUNC_DIV_EXPR:
1273 case FLOOR_DIV_EXPR:
1274 case ROUND_DIV_EXPR:
1275 if (flag_complex_method == 0)
1277 /* Keep this algorithm in sync with
1278 tree-complex.c:expand_complex_div_straight().
1280 Expand complex division to scalars, straightforward algorithm.
1281 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1285 = const_binop (PLUS_EXPR,
1286 const_binop (MULT_EXPR, r2, r2, notrunc),
1287 const_binop (MULT_EXPR, i2, i2, notrunc),
1290 = const_binop (PLUS_EXPR,
1291 const_binop (MULT_EXPR, r1, r2, notrunc),
1292 const_binop (MULT_EXPR, i1, i2, notrunc),
1295 = const_binop (MINUS_EXPR,
1296 const_binop (MULT_EXPR, i1, r2, notrunc),
1297 const_binop (MULT_EXPR, r1, i2, notrunc),
1300 real = const_binop (code, t1, magsquared, notrunc);
1301 imag = const_binop (code, t2, magsquared, notrunc);
1305 /* Keep this algorithm in sync with
1306 tree-complex.c:expand_complex_div_wide().
1308 Expand complex division to scalars, modified algorithm to minimize
1309 overflow with wide input ranges. */
1310 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1311 fold_abs_const (r2, TREE_TYPE (type)),
1312 fold_abs_const (i2, TREE_TYPE (type)));
1314 if (integer_nonzerop (compare))
1316 /* In the TRUE branch, we compute
1318 div = (br * ratio) + bi;
1319 tr = (ar * ratio) + ai;
1320 ti = (ai * ratio) - ar;
1323 tree ratio = const_binop (code, r2, i2, notrunc);
1324 tree div = const_binop (PLUS_EXPR, i2,
1325 const_binop (MULT_EXPR, r2, ratio,
1328 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
1329 real = const_binop (PLUS_EXPR, real, i1, notrunc);
1330 real = const_binop (code, real, div, notrunc);
1332 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
1333 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
1334 imag = const_binop (code, imag, div, notrunc);
1338 /* In the FALSE branch, we compute
1340 divisor = (d * ratio) + c;
1341 tr = (b * ratio) + a;
1342 ti = b - (a * ratio);
1345 tree ratio = const_binop (code, i2, r2, notrunc);
1346 tree div = const_binop (PLUS_EXPR, r2,
1347 const_binop (MULT_EXPR, i2, ratio,
1351 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
1352 real = const_binop (PLUS_EXPR, real, r1, notrunc);
1353 real = const_binop (code, real, div, notrunc);
1355 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
1356 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
1357 imag = const_binop (code, imag, div, notrunc);
1367 return build_complex (type, real, imag);
1370 if (TREE_CODE (arg1) == VECTOR_CST)
1372 tree type = TREE_TYPE(arg1);
1373 int count = TYPE_VECTOR_SUBPARTS (type), i;
1374 tree elements1, elements2, list = NULL_TREE;
1376 if(TREE_CODE(arg2) != VECTOR_CST)
1379 elements1 = TREE_VECTOR_CST_ELTS (arg1);
1380 elements2 = TREE_VECTOR_CST_ELTS (arg2);
1382 for (i = 0; i < count; i++)
1384 tree elem1, elem2, elem;
1386 /* The trailing elements can be empty and should be treated as 0 */
1388 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1391 elem1 = TREE_VALUE(elements1);
1392 elements1 = TREE_CHAIN (elements1);
1396 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1399 elem2 = TREE_VALUE(elements2);
1400 elements2 = TREE_CHAIN (elements2);
1403 elem = const_binop (code, elem1, elem2, notrunc);
1405 /* It is possible that const_binop cannot handle the given
1406 code and return NULL_TREE */
1407 if(elem == NULL_TREE)
1410 list = tree_cons (NULL_TREE, elem, list);
1412 return build_vector(type, nreverse(list));
1417 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1418 indicates which particular sizetype to create. */
1421 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1423 return build_int_cst (sizetype_tab[(int) kind], number);
1426 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1427 is a tree code. The type of the result is taken from the operands.
1428 Both must be equivalent integer types, ala int_binop_types_match_p.
1429 If the operands are constant, so is the result. */
1432 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1434 tree type = TREE_TYPE (arg0);
1436 if (arg0 == error_mark_node || arg1 == error_mark_node)
1437 return error_mark_node;
1439 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1442 /* Handle the special case of two integer constants faster. */
1443 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1445 /* And some specific cases even faster than that. */
1446 if (code == PLUS_EXPR)
1448 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1450 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1453 else if (code == MINUS_EXPR)
1455 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1458 else if (code == MULT_EXPR)
1460 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1464 /* Handle general case of two integer constants. */
1465 return int_const_binop (code, arg0, arg1, 0);
1468 return fold_build2_loc (loc, code, type, arg0, arg1);
1471 /* Given two values, either both of sizetype or both of bitsizetype,
1472 compute the difference between the two values. Return the value
1473 in signed type corresponding to the type of the operands. */
1476 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1478 tree type = TREE_TYPE (arg0);
1481 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1484 /* If the type is already signed, just do the simple thing. */
1485 if (!TYPE_UNSIGNED (type))
1486 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1488 if (type == sizetype)
1490 else if (type == bitsizetype)
1491 ctype = sbitsizetype;
1493 ctype = signed_type_for (type);
1495 /* If either operand is not a constant, do the conversions to the signed
1496 type and subtract. The hardware will do the right thing with any
1497 overflow in the subtraction. */
1498 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1499 return size_binop_loc (loc, MINUS_EXPR,
1500 fold_convert_loc (loc, ctype, arg0),
1501 fold_convert_loc (loc, ctype, arg1));
1503 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1504 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1505 overflow) and negate (which can't either). Special-case a result
1506 of zero while we're here. */
1507 if (tree_int_cst_equal (arg0, arg1))
1508 return build_int_cst (ctype, 0);
1509 else if (tree_int_cst_lt (arg1, arg0))
1510 return fold_convert_loc (loc, ctype,
1511 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1513 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1514 fold_convert_loc (loc, ctype,
1515 size_binop_loc (loc,
1520 /* A subroutine of fold_convert_const handling conversions of an
1521 INTEGER_CST to another integer type. */
1524 fold_convert_const_int_from_int (tree type, const_tree arg1)
1528 /* Given an integer constant, make new constant with new type,
1529 appropriately sign-extended or truncated. */
1530 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
1531 TREE_INT_CST_HIGH (arg1),
1532 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1533 (TREE_INT_CST_HIGH (arg1) < 0
1534 && (TYPE_UNSIGNED (type)
1535 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1536 | TREE_OVERFLOW (arg1));
1541 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1542 to an integer type. */
1545 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1550 /* The following code implements the floating point to integer
1551 conversion rules required by the Java Language Specification,
1552 that IEEE NaNs are mapped to zero and values that overflow
1553 the target precision saturate, i.e. values greater than
1554 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1555 are mapped to INT_MIN. These semantics are allowed by the
1556 C and C++ standards that simply state that the behavior of
1557 FP-to-integer conversion is unspecified upon overflow. */
1561 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1565 case FIX_TRUNC_EXPR:
1566 real_trunc (&r, VOIDmode, &x);
1573 /* If R is NaN, return zero and show we have an overflow. */
1574 if (REAL_VALUE_ISNAN (r))
1577 val = double_int_zero;
1580 /* See if R is less than the lower bound or greater than the
1585 tree lt = TYPE_MIN_VALUE (type);
1586 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1587 if (REAL_VALUES_LESS (r, l))
1590 val = tree_to_double_int (lt);
1596 tree ut = TYPE_MAX_VALUE (type);
1599 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1600 if (REAL_VALUES_LESS (u, r))
1603 val = tree_to_double_int (ut);
1609 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1611 t = force_fit_type_double (type, val.low, val.high, -1,
1612 overflow | TREE_OVERFLOW (arg1));
1616 /* A subroutine of fold_convert_const handling conversions of a
1617 FIXED_CST to an integer type. */
1620 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1623 double_int temp, temp_trunc;
1626 /* Right shift FIXED_CST to temp by fbit. */
1627 temp = TREE_FIXED_CST (arg1).data;
1628 mode = TREE_FIXED_CST (arg1).mode;
1629 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1631 temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1632 HOST_BITS_PER_DOUBLE_INT,
1633 SIGNED_FIXED_POINT_MODE_P (mode));
1635 /* Left shift temp to temp_trunc by fbit. */
1636 temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1637 HOST_BITS_PER_DOUBLE_INT,
1638 SIGNED_FIXED_POINT_MODE_P (mode));
1642 temp = double_int_zero;
1643 temp_trunc = double_int_zero;
1646 /* If FIXED_CST is negative, we need to round the value toward 0.
1647 By checking if the fractional bits are not zero to add 1 to temp. */
1648 if (SIGNED_FIXED_POINT_MODE_P (mode)
1649 && double_int_negative_p (temp_trunc)
1650 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1651 temp = double_int_add (temp, double_int_one);
1653 /* Given a fixed-point constant, make new constant with new type,
1654 appropriately sign-extended or truncated. */
1655 t = force_fit_type_double (type, temp.low, temp.high, -1,
1656 (double_int_negative_p (temp)
1657 && (TYPE_UNSIGNED (type)
1658 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1659 | TREE_OVERFLOW (arg1));
1664 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1665 to another floating point type. */
1668 fold_convert_const_real_from_real (tree type, const_tree arg1)
1670 REAL_VALUE_TYPE value;
1673 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1674 t = build_real (type, value);
1676 /* If converting an infinity or NAN to a representation that doesn't
1677 have one, set the overflow bit so that we can produce some kind of
1678 error message at the appropriate point if necessary. It's not the
1679 most user-friendly message, but it's better than nothing. */
1680 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1681 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1682 TREE_OVERFLOW (t) = 1;
1683 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1684 && !MODE_HAS_NANS (TYPE_MODE (type)))
1685 TREE_OVERFLOW (t) = 1;
1686 /* Regular overflow, conversion produced an infinity in a mode that
1687 can't represent them. */
1688 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1689 && REAL_VALUE_ISINF (value)
1690 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1691 TREE_OVERFLOW (t) = 1;
1693 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1697 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1698 to a floating point type. */
1701 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1703 REAL_VALUE_TYPE value;
1706 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1707 t = build_real (type, value);
1709 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1713 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1714 to another fixed-point type. */
1717 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1719 FIXED_VALUE_TYPE value;
1723 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1724 TYPE_SATURATING (type));
1725 t = build_fixed (type, value);
1727 /* Propagate overflow flags. */
1728 if (overflow_p | TREE_OVERFLOW (arg1))
1729 TREE_OVERFLOW (t) = 1;
1733 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1734 to a fixed-point type. */
1737 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1739 FIXED_VALUE_TYPE value;
1743 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1744 TREE_INT_CST (arg1),
1745 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1746 TYPE_SATURATING (type));
1747 t = build_fixed (type, value);
1749 /* Propagate overflow flags. */
1750 if (overflow_p | TREE_OVERFLOW (arg1))
1751 TREE_OVERFLOW (t) = 1;
1755 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1756 to a fixed-point type. */
1759 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1761 FIXED_VALUE_TYPE value;
1765 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1766 &TREE_REAL_CST (arg1),
1767 TYPE_SATURATING (type));
1768 t = build_fixed (type, value);
1770 /* Propagate overflow flags. */
1771 if (overflow_p | TREE_OVERFLOW (arg1))
1772 TREE_OVERFLOW (t) = 1;
1776 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1777 type TYPE. If no simplification can be done return NULL_TREE. */
1780 fold_convert_const (enum tree_code code, tree type, tree arg1)
1782 if (TREE_TYPE (arg1) == type)
1785 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1786 || TREE_CODE (type) == OFFSET_TYPE)
1788 if (TREE_CODE (arg1) == INTEGER_CST)
1789 return fold_convert_const_int_from_int (type, arg1);
1790 else if (TREE_CODE (arg1) == REAL_CST)
1791 return fold_convert_const_int_from_real (code, type, arg1);
1792 else if (TREE_CODE (arg1) == FIXED_CST)
1793 return fold_convert_const_int_from_fixed (type, arg1);
1795 else if (TREE_CODE (type) == REAL_TYPE)
1797 if (TREE_CODE (arg1) == INTEGER_CST)
1798 return build_real_from_int_cst (type, arg1);
1799 else if (TREE_CODE (arg1) == REAL_CST)
1800 return fold_convert_const_real_from_real (type, arg1);
1801 else if (TREE_CODE (arg1) == FIXED_CST)
1802 return fold_convert_const_real_from_fixed (type, arg1);
1804 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1806 if (TREE_CODE (arg1) == FIXED_CST)
1807 return fold_convert_const_fixed_from_fixed (type, arg1);
1808 else if (TREE_CODE (arg1) == INTEGER_CST)
1809 return fold_convert_const_fixed_from_int (type, arg1);
1810 else if (TREE_CODE (arg1) == REAL_CST)
1811 return fold_convert_const_fixed_from_real (type, arg1);
1816 /* Construct a vector of zero elements of vector type TYPE. */
1819 build_zero_vector (tree type)
1824 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1825 units = TYPE_VECTOR_SUBPARTS (type);
1828 for (i = 0; i < units; i++)
1829 list = tree_cons (NULL_TREE, elem, list);
1830 return build_vector (type, list);
1833 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1836 fold_convertible_p (const_tree type, const_tree arg)
1838 tree orig = TREE_TYPE (arg);
1843 if (TREE_CODE (arg) == ERROR_MARK
1844 || TREE_CODE (type) == ERROR_MARK
1845 || TREE_CODE (orig) == ERROR_MARK)
1848 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1851 switch (TREE_CODE (type))
1853 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1854 case POINTER_TYPE: case REFERENCE_TYPE:
1856 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1857 || TREE_CODE (orig) == OFFSET_TYPE)
1859 return (TREE_CODE (orig) == VECTOR_TYPE
1860 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1863 case FIXED_POINT_TYPE:
1867 return TREE_CODE (type) == TREE_CODE (orig);
1874 /* Convert expression ARG to type TYPE. Used by the middle-end for
1875 simple conversions in preference to calling the front-end's convert. */
1878 fold_convert_loc (location_t loc, tree type, tree arg)
1880 tree orig = TREE_TYPE (arg);
1886 if (TREE_CODE (arg) == ERROR_MARK
1887 || TREE_CODE (type) == ERROR_MARK
1888 || TREE_CODE (orig) == ERROR_MARK)
1889 return error_mark_node;
1891 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1892 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1894 switch (TREE_CODE (type))
1897 case REFERENCE_TYPE:
1898 /* Handle conversions between pointers to different address spaces. */
1899 if (POINTER_TYPE_P (orig)
1900 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1901 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1902 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1905 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1907 if (TREE_CODE (arg) == INTEGER_CST)
1909 tem = fold_convert_const (NOP_EXPR, type, arg);
1910 if (tem != NULL_TREE)
1913 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1914 || TREE_CODE (orig) == OFFSET_TYPE)
1915 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1916 if (TREE_CODE (orig) == COMPLEX_TYPE)
1917 return fold_convert_loc (loc, type,
1918 fold_build1_loc (loc, REALPART_EXPR,
1919 TREE_TYPE (orig), arg));
1920 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1921 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1922 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1925 if (TREE_CODE (arg) == INTEGER_CST)
1927 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1928 if (tem != NULL_TREE)
1931 else if (TREE_CODE (arg) == REAL_CST)
1933 tem = fold_convert_const (NOP_EXPR, type, arg);
1934 if (tem != NULL_TREE)
1937 else if (TREE_CODE (arg) == FIXED_CST)
1939 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1940 if (tem != NULL_TREE)
1944 switch (TREE_CODE (orig))
1947 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1948 case POINTER_TYPE: case REFERENCE_TYPE:
1949 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1952 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1954 case FIXED_POINT_TYPE:
1955 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1958 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1959 return fold_convert_loc (loc, type, tem);
1965 case FIXED_POINT_TYPE:
1966 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1967 || TREE_CODE (arg) == REAL_CST)
1969 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1970 if (tem != NULL_TREE)
1971 goto fold_convert_exit;
1974 switch (TREE_CODE (orig))
1976 case FIXED_POINT_TYPE:
1981 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1984 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1985 return fold_convert_loc (loc, type, tem);
1992 switch (TREE_CODE (orig))
1995 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1996 case POINTER_TYPE: case REFERENCE_TYPE:
1998 case FIXED_POINT_TYPE:
1999 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2000 fold_convert_loc (loc, TREE_TYPE (type), arg),
2001 fold_convert_loc (loc, TREE_TYPE (type),
2002 integer_zero_node));
2007 if (TREE_CODE (arg) == COMPLEX_EXPR)
2009 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2010 TREE_OPERAND (arg, 0));
2011 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2012 TREE_OPERAND (arg, 1));
2013 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2016 arg = save_expr (arg);
2017 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2018 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2019 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2020 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2021 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2029 if (integer_zerop (arg))
2030 return build_zero_vector (type);
2031 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2032 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2033 || TREE_CODE (orig) == VECTOR_TYPE);
2034 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2037 tem = fold_ignored_result (arg);
2038 if (TREE_CODE (tem) == MODIFY_EXPR)
2039 goto fold_convert_exit;
2040 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2046 protected_set_expr_location (tem, loc);
2050 /* Return false if expr can be assumed not to be an lvalue, true
2054 maybe_lvalue_p (const_tree x)
2056 /* We only need to wrap lvalue tree codes. */
2057 switch (TREE_CODE (x))
2068 case ALIGN_INDIRECT_REF:
2069 case MISALIGNED_INDIRECT_REF:
2071 case ARRAY_RANGE_REF:
2077 case PREINCREMENT_EXPR:
2078 case PREDECREMENT_EXPR:
2080 case TRY_CATCH_EXPR:
2081 case WITH_CLEANUP_EXPR:
2090 /* Assume the worst for front-end tree codes. */
2091 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2099 /* Return an expr equal to X but certainly not valid as an lvalue. */
2102 non_lvalue_loc (location_t loc, tree x)
2104 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2109 if (! maybe_lvalue_p (x))
2111 x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2112 SET_EXPR_LOCATION (x, loc);
2116 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2117 Zero means allow extended lvalues. */
2119 int pedantic_lvalues;
2121 /* When pedantic, return an expr equal to X but certainly not valid as a
2122 pedantic lvalue. Otherwise, return X. */
2125 pedantic_non_lvalue_loc (location_t loc, tree x)
2127 if (pedantic_lvalues)
2128 return non_lvalue_loc (loc, x);
2129 protected_set_expr_location (x, loc);
2133 /* Given a tree comparison code, return the code that is the logical inverse
2134 of the given code. It is not safe to do this for floating-point
2135 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2136 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2139 invert_tree_comparison (enum tree_code code, bool honor_nans)
2141 if (honor_nans && flag_trapping_math)
2151 return honor_nans ? UNLE_EXPR : LE_EXPR;
2153 return honor_nans ? UNLT_EXPR : LT_EXPR;
2155 return honor_nans ? UNGE_EXPR : GE_EXPR;
2157 return honor_nans ? UNGT_EXPR : GT_EXPR;
2171 return UNORDERED_EXPR;
2172 case UNORDERED_EXPR:
2173 return ORDERED_EXPR;
2179 /* Similar, but return the comparison that results if the operands are
2180 swapped. This is safe for floating-point. */
2183 swap_tree_comparison (enum tree_code code)
2190 case UNORDERED_EXPR:
2216 /* Convert a comparison tree code from an enum tree_code representation
2217 into a compcode bit-based encoding. This function is the inverse of
2218 compcode_to_comparison. */
2220 static enum comparison_code
2221 comparison_to_compcode (enum tree_code code)
2238 return COMPCODE_ORD;
2239 case UNORDERED_EXPR:
2240 return COMPCODE_UNORD;
2242 return COMPCODE_UNLT;
2244 return COMPCODE_UNEQ;
2246 return COMPCODE_UNLE;
2248 return COMPCODE_UNGT;
2250 return COMPCODE_LTGT;
2252 return COMPCODE_UNGE;
2258 /* Convert a compcode bit-based encoding of a comparison operator back
2259 to GCC's enum tree_code representation. This function is the
2260 inverse of comparison_to_compcode. */
2262 static enum tree_code
2263 compcode_to_comparison (enum comparison_code code)
2280 return ORDERED_EXPR;
2281 case COMPCODE_UNORD:
2282 return UNORDERED_EXPR;
2300 /* Return a tree for the comparison which is the combination of
2301 doing the AND or OR (depending on CODE) of the two operations LCODE
2302 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2303 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2304 if this makes the transformation invalid. */
2307 combine_comparisons (location_t loc,
2308 enum tree_code code, enum tree_code lcode,
2309 enum tree_code rcode, tree truth_type,
2310 tree ll_arg, tree lr_arg)
2312 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2313 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2314 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2319 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2320 compcode = lcompcode & rcompcode;
2323 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2324 compcode = lcompcode | rcompcode;
2333 /* Eliminate unordered comparisons, as well as LTGT and ORD
2334 which are not used unless the mode has NaNs. */
2335 compcode &= ~COMPCODE_UNORD;
2336 if (compcode == COMPCODE_LTGT)
2337 compcode = COMPCODE_NE;
2338 else if (compcode == COMPCODE_ORD)
2339 compcode = COMPCODE_TRUE;
2341 else if (flag_trapping_math)
2343 /* Check that the original operation and the optimized ones will trap
2344 under the same condition. */
2345 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2346 && (lcompcode != COMPCODE_EQ)
2347 && (lcompcode != COMPCODE_ORD);
2348 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2349 && (rcompcode != COMPCODE_EQ)
2350 && (rcompcode != COMPCODE_ORD);
2351 bool trap = (compcode & COMPCODE_UNORD) == 0
2352 && (compcode != COMPCODE_EQ)
2353 && (compcode != COMPCODE_ORD);
2355 /* In a short-circuited boolean expression the LHS might be
2356 such that the RHS, if evaluated, will never trap. For
2357 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2358 if neither x nor y is NaN. (This is a mixed blessing: for
2359 example, the expression above will never trap, hence
2360 optimizing it to x < y would be invalid). */
2361 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2362 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2365 /* If the comparison was short-circuited, and only the RHS
2366 trapped, we may now generate a spurious trap. */
2368 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2371 /* If we changed the conditions that cause a trap, we lose. */
2372 if ((ltrap || rtrap) != trap)
2376 if (compcode == COMPCODE_TRUE)
2377 return constant_boolean_node (true, truth_type);
2378 else if (compcode == COMPCODE_FALSE)
2379 return constant_boolean_node (false, truth_type);
2382 enum tree_code tcode;
2384 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2385 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2389 /* Return nonzero if two operands (typically of the same tree node)
2390 are necessarily equal. If either argument has side-effects this
2391 function returns zero. FLAGS modifies behavior as follows:
2393 If OEP_ONLY_CONST is set, only return nonzero for constants.
2394 This function tests whether the operands are indistinguishable;
2395 it does not test whether they are equal using C's == operation.
2396 The distinction is important for IEEE floating point, because
2397 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2398 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2400 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2401 even though it may hold multiple values during a function.
2402 This is because a GCC tree node guarantees that nothing else is
2403 executed between the evaluation of its "operands" (which may often
2404 be evaluated in arbitrary order). Hence if the operands themselves
2405 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2406 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2407 unset means assuming isochronic (or instantaneous) tree equivalence.
2408 Unless comparing arbitrary expression trees, such as from different
2409 statements, this flag can usually be left unset.
2411 If OEP_PURE_SAME is set, then pure functions with identical arguments
2412 are considered the same. It is used when the caller has other ways
2413 to ensure that global memory is unchanged in between. */
2416 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2418 /* If either is ERROR_MARK, they aren't equal. */
2419 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2420 || TREE_TYPE (arg0) == error_mark_node
2421 || TREE_TYPE (arg1) == error_mark_node)
2424 /* Similar, if either does not have a type (like a released SSA name),
2425 they aren't equal. */
2426 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2429 /* Check equality of integer constants before bailing out due to
2430 precision differences. */
2431 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2432 return tree_int_cst_equal (arg0, arg1);
2434 /* If both types don't have the same signedness, then we can't consider
2435 them equal. We must check this before the STRIP_NOPS calls
2436 because they may change the signedness of the arguments. As pointers
2437 strictly don't have a signedness, require either two pointers or
2438 two non-pointers as well. */
2439 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2440 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2443 /* We cannot consider pointers to different address space equal. */
2444 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2445 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2446 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2449 /* If both types don't have the same precision, then it is not safe
2451 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2457 /* In case both args are comparisons but with different comparison
2458 code, try to swap the comparison operands of one arg to produce
2459 a match and compare that variant. */
2460 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2461 && COMPARISON_CLASS_P (arg0)
2462 && COMPARISON_CLASS_P (arg1))
2464 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2466 if (TREE_CODE (arg0) == swap_code)
2467 return operand_equal_p (TREE_OPERAND (arg0, 0),
2468 TREE_OPERAND (arg1, 1), flags)
2469 && operand_equal_p (TREE_OPERAND (arg0, 1),
2470 TREE_OPERAND (arg1, 0), flags);
2473 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2474 /* This is needed for conversions and for COMPONENT_REF.
2475 Might as well play it safe and always test this. */
2476 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2477 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2478 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2481 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2482 We don't care about side effects in that case because the SAVE_EXPR
2483 takes care of that for us. In all other cases, two expressions are
2484 equal if they have no side effects. If we have two identical
2485 expressions with side effects that should be treated the same due
2486 to the only side effects being identical SAVE_EXPR's, that will
2487 be detected in the recursive calls below. */
2488 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2489 && (TREE_CODE (arg0) == SAVE_EXPR
2490 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2493 /* Next handle constant cases, those for which we can return 1 even
2494 if ONLY_CONST is set. */
2495 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2496 switch (TREE_CODE (arg0))
2499 return tree_int_cst_equal (arg0, arg1);
2502 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2503 TREE_FIXED_CST (arg1));
2506 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2507 TREE_REAL_CST (arg1)))
2511 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2513 /* If we do not distinguish between signed and unsigned zero,
2514 consider them equal. */
2515 if (real_zerop (arg0) && real_zerop (arg1))
2524 v1 = TREE_VECTOR_CST_ELTS (arg0);
2525 v2 = TREE_VECTOR_CST_ELTS (arg1);
2528 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2531 v1 = TREE_CHAIN (v1);
2532 v2 = TREE_CHAIN (v2);
2539 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2541 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2545 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2546 && ! memcmp (TREE_STRING_POINTER (arg0),
2547 TREE_STRING_POINTER (arg1),
2548 TREE_STRING_LENGTH (arg0)));
2551 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2557 if (flags & OEP_ONLY_CONST)
2560 /* Define macros to test an operand from arg0 and arg1 for equality and a
2561 variant that allows null and views null as being different from any
2562 non-null value. In the latter case, if either is null, the both
2563 must be; otherwise, do the normal comparison. */
2564 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2565 TREE_OPERAND (arg1, N), flags)
2567 #define OP_SAME_WITH_NULL(N) \
2568 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2569 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2571 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2574 /* Two conversions are equal only if signedness and modes match. */
2575 switch (TREE_CODE (arg0))
2578 case FIX_TRUNC_EXPR:
2579 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2580 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2590 case tcc_comparison:
2592 if (OP_SAME (0) && OP_SAME (1))
2595 /* For commutative ops, allow the other order. */
2596 return (commutative_tree_code (TREE_CODE (arg0))
2597 && operand_equal_p (TREE_OPERAND (arg0, 0),
2598 TREE_OPERAND (arg1, 1), flags)
2599 && operand_equal_p (TREE_OPERAND (arg0, 1),
2600 TREE_OPERAND (arg1, 0), flags));
2603 /* If either of the pointer (or reference) expressions we are
2604 dereferencing contain a side effect, these cannot be equal. */
2605 if (TREE_SIDE_EFFECTS (arg0)
2606 || TREE_SIDE_EFFECTS (arg1))
2609 switch (TREE_CODE (arg0))
2612 case ALIGN_INDIRECT_REF:
2613 case MISALIGNED_INDIRECT_REF:
2619 case ARRAY_RANGE_REF:
2620 /* Operands 2 and 3 may be null.
2621 Compare the array index by value if it is constant first as we
2622 may have different types but same value here. */
2624 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2625 TREE_OPERAND (arg1, 1))
2627 && OP_SAME_WITH_NULL (2)
2628 && OP_SAME_WITH_NULL (3));
2631 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2632 may be NULL when we're called to compare MEM_EXPRs. */
2633 return OP_SAME_WITH_NULL (0)
2635 && OP_SAME_WITH_NULL (2);
2638 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2644 case tcc_expression:
2645 switch (TREE_CODE (arg0))
2648 case TRUTH_NOT_EXPR:
2651 case TRUTH_ANDIF_EXPR:
2652 case TRUTH_ORIF_EXPR:
2653 return OP_SAME (0) && OP_SAME (1);
2655 case TRUTH_AND_EXPR:
2657 case TRUTH_XOR_EXPR:
2658 if (OP_SAME (0) && OP_SAME (1))
2661 /* Otherwise take into account this is a commutative operation. */
2662 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2663 TREE_OPERAND (arg1, 1), flags)
2664 && operand_equal_p (TREE_OPERAND (arg0, 1),
2665 TREE_OPERAND (arg1, 0), flags));
2668 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2675 switch (TREE_CODE (arg0))
2678 /* If the CALL_EXPRs call different functions, then they
2679 clearly can not be equal. */
2680 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2685 unsigned int cef = call_expr_flags (arg0);
2686 if (flags & OEP_PURE_SAME)
2687 cef &= ECF_CONST | ECF_PURE;
2694 /* Now see if all the arguments are the same. */
2696 const_call_expr_arg_iterator iter0, iter1;
2698 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2699 a1 = first_const_call_expr_arg (arg1, &iter1);
2701 a0 = next_const_call_expr_arg (&iter0),
2702 a1 = next_const_call_expr_arg (&iter1))
2703 if (! operand_equal_p (a0, a1, flags))
2706 /* If we get here and both argument lists are exhausted
2707 then the CALL_EXPRs are equal. */
2708 return ! (a0 || a1);
2714 case tcc_declaration:
2715 /* Consider __builtin_sqrt equal to sqrt. */
2716 return (TREE_CODE (arg0) == FUNCTION_DECL
2717 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2718 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2719 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2726 #undef OP_SAME_WITH_NULL
2729 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2730 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2732 When in doubt, return 0. */
2735 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2737 int unsignedp1, unsignedpo;
2738 tree primarg0, primarg1, primother;
2739 unsigned int correct_width;
2741 if (operand_equal_p (arg0, arg1, 0))
2744 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2745 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2748 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2749 and see if the inner values are the same. This removes any
2750 signedness comparison, which doesn't matter here. */
2751 primarg0 = arg0, primarg1 = arg1;
2752 STRIP_NOPS (primarg0);
2753 STRIP_NOPS (primarg1);
2754 if (operand_equal_p (primarg0, primarg1, 0))
2757 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2758 actual comparison operand, ARG0.
2760 First throw away any conversions to wider types
2761 already present in the operands. */
2763 primarg1 = get_narrower (arg1, &unsignedp1);
2764 primother = get_narrower (other, &unsignedpo);
2766 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2767 if (unsignedp1 == unsignedpo
2768 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2769 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2771 tree type = TREE_TYPE (arg0);
2773 /* Make sure shorter operand is extended the right way
2774 to match the longer operand. */
2775 primarg1 = fold_convert (signed_or_unsigned_type_for
2776 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2778 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2785 /* See if ARG is an expression that is either a comparison or is performing
2786 arithmetic on comparisons. The comparisons must only be comparing
2787 two different values, which will be stored in *CVAL1 and *CVAL2; if
2788 they are nonzero it means that some operands have already been found.
2789 No variables may be used anywhere else in the expression except in the
2790 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2791 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2793 If this is true, return 1. Otherwise, return zero. */
2796 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2798 enum tree_code code = TREE_CODE (arg);
2799 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2801 /* We can handle some of the tcc_expression cases here. */
2802 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2804 else if (tclass == tcc_expression
2805 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2806 || code == COMPOUND_EXPR))
2807 tclass = tcc_binary;
2809 else if (tclass == tcc_expression && code == SAVE_EXPR
2810 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2812 /* If we've already found a CVAL1 or CVAL2, this expression is
2813 two complex to handle. */
2814 if (*cval1 || *cval2)
2824 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2827 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2828 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2829 cval1, cval2, save_p));
2834 case tcc_expression:
2835 if (code == COND_EXPR)
2836 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2837 cval1, cval2, save_p)
2838 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2839 cval1, cval2, save_p)
2840 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2841 cval1, cval2, save_p));
2844 case tcc_comparison:
2845 /* First see if we can handle the first operand, then the second. For
2846 the second operand, we know *CVAL1 can't be zero. It must be that
2847 one side of the comparison is each of the values; test for the
2848 case where this isn't true by failing if the two operands
2851 if (operand_equal_p (TREE_OPERAND (arg, 0),
2852 TREE_OPERAND (arg, 1), 0))
2856 *cval1 = TREE_OPERAND (arg, 0);
2857 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2859 else if (*cval2 == 0)
2860 *cval2 = TREE_OPERAND (arg, 0);
2861 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2866 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2868 else if (*cval2 == 0)
2869 *cval2 = TREE_OPERAND (arg, 1);
2870 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2882 /* ARG is a tree that is known to contain just arithmetic operations and
2883 comparisons. Evaluate the operations in the tree substituting NEW0 for
2884 any occurrence of OLD0 as an operand of a comparison and likewise for
2888 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2889 tree old1, tree new1)
2891 tree type = TREE_TYPE (arg);
2892 enum tree_code code = TREE_CODE (arg);
2893 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2895 /* We can handle some of the tcc_expression cases here. */
2896 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2898 else if (tclass == tcc_expression
2899 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2900 tclass = tcc_binary;
2905 return fold_build1_loc (loc, code, type,
2906 eval_subst (loc, TREE_OPERAND (arg, 0),
2907 old0, new0, old1, new1));
2910 return fold_build2_loc (loc, code, type,
2911 eval_subst (loc, TREE_OPERAND (arg, 0),
2912 old0, new0, old1, new1),
2913 eval_subst (loc, TREE_OPERAND (arg, 1),
2914 old0, new0, old1, new1));
2916 case tcc_expression:
2920 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2924 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2928 return fold_build3_loc (loc, code, type,
2929 eval_subst (loc, TREE_OPERAND (arg, 0),
2930 old0, new0, old1, new1),
2931 eval_subst (loc, TREE_OPERAND (arg, 1),
2932 old0, new0, old1, new1),
2933 eval_subst (loc, TREE_OPERAND (arg, 2),
2934 old0, new0, old1, new1));
2938 /* Fall through - ??? */
2940 case tcc_comparison:
2942 tree arg0 = TREE_OPERAND (arg, 0);
2943 tree arg1 = TREE_OPERAND (arg, 1);
2945 /* We need to check both for exact equality and tree equality. The
2946 former will be true if the operand has a side-effect. In that
2947 case, we know the operand occurred exactly once. */
2949 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2951 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2954 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2956 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2959 return fold_build2_loc (loc, code, type, arg0, arg1);
2967 /* Return a tree for the case when the result of an expression is RESULT
2968 converted to TYPE and OMITTED was previously an operand of the expression
2969 but is now not needed (e.g., we folded OMITTED * 0).
2971 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2972 the conversion of RESULT to TYPE. */
2975 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2977 tree t = fold_convert_loc (loc, type, result);
2979 /* If the resulting operand is an empty statement, just return the omitted
2980 statement casted to void. */
2981 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2983 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2984 goto omit_one_operand_exit;
2987 if (TREE_SIDE_EFFECTS (omitted))
2989 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2990 goto omit_one_operand_exit;
2993 return non_lvalue_loc (loc, t);
2995 omit_one_operand_exit:
2996 protected_set_expr_location (t, loc);
3000 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3003 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3006 tree t = fold_convert_loc (loc, type, result);
3008 /* If the resulting operand is an empty statement, just return the omitted
3009 statement casted to void. */
3010 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3012 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3013 goto pedantic_omit_one_operand_exit;
3016 if (TREE_SIDE_EFFECTS (omitted))
3018 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3019 goto pedantic_omit_one_operand_exit;
3022 return pedantic_non_lvalue_loc (loc, t);
3024 pedantic_omit_one_operand_exit:
3025 protected_set_expr_location (t, loc);
3029 /* Return a tree for the case when the result of an expression is RESULT
3030 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3031 of the expression but are now not needed.
3033 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3034 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3035 evaluated before OMITTED2. Otherwise, if neither has side effects,
3036 just do the conversion of RESULT to TYPE. */
3039 omit_two_operands_loc (location_t loc, tree type, tree result,
3040 tree omitted1, tree omitted2)
3042 tree t = fold_convert_loc (loc, type, result);
3044 if (TREE_SIDE_EFFECTS (omitted2))
3046 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3047 SET_EXPR_LOCATION (t, loc);
3049 if (TREE_SIDE_EFFECTS (omitted1))
3051 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3052 SET_EXPR_LOCATION (t, loc);
3055 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3059 /* Return a simplified tree node for the truth-negation of ARG. This
3060 never alters ARG itself. We assume that ARG is an operation that
3061 returns a truth value (0 or 1).
3063 FIXME: one would think we would fold the result, but it causes
3064 problems with the dominator optimizer. */
3067 fold_truth_not_expr (location_t loc, tree arg)
3069 tree t, type = TREE_TYPE (arg);
3070 enum tree_code code = TREE_CODE (arg);
3071 location_t loc1, loc2;
3073 /* If this is a comparison, we can simply invert it, except for
3074 floating-point non-equality comparisons, in which case we just
3075 enclose a TRUTH_NOT_EXPR around what we have. */
3077 if (TREE_CODE_CLASS (code) == tcc_comparison)
3079 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3080 if (FLOAT_TYPE_P (op_type)
3081 && flag_trapping_math
3082 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3083 && code != NE_EXPR && code != EQ_EXPR)
3086 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3087 if (code == ERROR_MARK)
3090 t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3091 SET_EXPR_LOCATION (t, loc);
3098 return constant_boolean_node (integer_zerop (arg), type);
3100 case TRUTH_AND_EXPR:
3101 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3102 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3103 if (loc1 == UNKNOWN_LOCATION)
3105 if (loc2 == UNKNOWN_LOCATION)
3107 t = build2 (TRUTH_OR_EXPR, type,
3108 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3109 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3113 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3114 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3115 if (loc1 == UNKNOWN_LOCATION)
3117 if (loc2 == UNKNOWN_LOCATION)
3119 t = build2 (TRUTH_AND_EXPR, type,
3120 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3121 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3124 case TRUTH_XOR_EXPR:
3125 /* Here we can invert either operand. We invert the first operand
3126 unless the second operand is a TRUTH_NOT_EXPR in which case our
3127 result is the XOR of the first operand with the inside of the
3128 negation of the second operand. */
3130 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3131 t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3132 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3134 t = build2 (TRUTH_XOR_EXPR, type,
3135 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3136 TREE_OPERAND (arg, 1));
3139 case TRUTH_ANDIF_EXPR:
3140 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3141 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3142 if (loc1 == UNKNOWN_LOCATION)
3144 if (loc2 == UNKNOWN_LOCATION)
3146 t = build2 (TRUTH_ORIF_EXPR, type,
3147 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3148 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3151 case TRUTH_ORIF_EXPR:
3152 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3153 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3154 if (loc1 == UNKNOWN_LOCATION)
3156 if (loc2 == UNKNOWN_LOCATION)
3158 t = build2 (TRUTH_ANDIF_EXPR, type,
3159 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3160 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3163 case TRUTH_NOT_EXPR:
3164 return TREE_OPERAND (arg, 0);
3168 tree arg1 = TREE_OPERAND (arg, 1);
3169 tree arg2 = TREE_OPERAND (arg, 2);
3171 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3172 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3173 if (loc1 == UNKNOWN_LOCATION)
3175 if (loc2 == UNKNOWN_LOCATION)
3178 /* A COND_EXPR may have a throw as one operand, which
3179 then has void type. Just leave void operands
3181 t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3182 VOID_TYPE_P (TREE_TYPE (arg1))
3183 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3184 VOID_TYPE_P (TREE_TYPE (arg2))
3185 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3190 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3191 if (loc1 == UNKNOWN_LOCATION)
3193 t = build2 (COMPOUND_EXPR, type,
3194 TREE_OPERAND (arg, 0),
3195 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3198 case NON_LVALUE_EXPR:
3199 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3200 if (loc1 == UNKNOWN_LOCATION)
3202 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3205 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3207 t = build1 (TRUTH_NOT_EXPR, type, arg);
3211 /* ... fall through ... */
3214 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3215 if (loc1 == UNKNOWN_LOCATION)
3217 t = build1 (TREE_CODE (arg), type,
3218 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3222 if (!integer_onep (TREE_OPERAND (arg, 1)))
3224 t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3228 t = build1 (TRUTH_NOT_EXPR, type, arg);
3231 case CLEANUP_POINT_EXPR:
3232 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3233 if (loc1 == UNKNOWN_LOCATION)
3235 t = build1 (CLEANUP_POINT_EXPR, type,
3236 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3245 SET_EXPR_LOCATION (t, loc);
3250 /* Return a simplified tree node for the truth-negation of ARG. This
3251 never alters ARG itself. We assume that ARG is an operation that
3252 returns a truth value (0 or 1).
3254 FIXME: one would think we would fold the result, but it causes
3255 problems with the dominator optimizer. */
3258 invert_truthvalue_loc (location_t loc, tree arg)
3262 if (TREE_CODE (arg) == ERROR_MARK)
3265 tem = fold_truth_not_expr (loc, arg);
3268 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3269 SET_EXPR_LOCATION (tem, loc);
3275 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3276 operands are another bit-wise operation with a common input. If so,
3277 distribute the bit operations to save an operation and possibly two if
3278 constants are involved. For example, convert
3279 (A | B) & (A | C) into A | (B & C)
3280 Further simplification will occur if B and C are constants.
3282 If this optimization cannot be done, 0 will be returned. */
3285 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3286 tree arg0, tree arg1)
3291 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3292 || TREE_CODE (arg0) == code
3293 || (TREE_CODE (arg0) != BIT_AND_EXPR
3294 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3297 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3299 common = TREE_OPERAND (arg0, 0);
3300 left = TREE_OPERAND (arg0, 1);
3301 right = TREE_OPERAND (arg1, 1);
3303 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3305 common = TREE_OPERAND (arg0, 0);
3306 left = TREE_OPERAND (arg0, 1);
3307 right = TREE_OPERAND (arg1, 0);
3309 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3311 common = TREE_OPERAND (arg0, 1);
3312 left = TREE_OPERAND (arg0, 0);
3313 right = TREE_OPERAND (arg1, 1);
3315 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3317 common = TREE_OPERAND (arg0, 1);
3318 left = TREE_OPERAND (arg0, 0);
3319 right = TREE_OPERAND (arg1, 0);
3324 common = fold_convert_loc (loc, type, common);
3325 left = fold_convert_loc (loc, type, left);
3326 right = fold_convert_loc (loc, type, right);
3327 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3328 fold_build2_loc (loc, code, type, left, right));
3331 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3332 with code CODE. This optimization is unsafe. */
3334 distribute_real_division (location_t loc, enum tree_code code, tree type,
3335 tree arg0, tree arg1)
3337 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3338 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3340 /* (A / C) +- (B / C) -> (A +- B) / C. */
3342 && operand_equal_p (TREE_OPERAND (arg0, 1),
3343 TREE_OPERAND (arg1, 1), 0))
3344 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3345 fold_build2_loc (loc, code, type,
3346 TREE_OPERAND (arg0, 0),
3347 TREE_OPERAND (arg1, 0)),
3348 TREE_OPERAND (arg0, 1));
3350 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3351 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3352 TREE_OPERAND (arg1, 0), 0)
3353 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3354 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3356 REAL_VALUE_TYPE r0, r1;
3357 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3358 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3360 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3362 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3363 real_arithmetic (&r0, code, &r0, &r1);
3364 return fold_build2_loc (loc, MULT_EXPR, type,
3365 TREE_OPERAND (arg0, 0),
3366 build_real (type, r0));
3372 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3373 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3376 make_bit_field_ref (location_t loc, tree inner, tree type,
3377 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3379 tree result, bftype;
3383 tree size = TYPE_SIZE (TREE_TYPE (inner));
3384 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3385 || POINTER_TYPE_P (TREE_TYPE (inner)))
3386 && host_integerp (size, 0)
3387 && tree_low_cst (size, 0) == bitsize)
3388 return fold_convert_loc (loc, type, inner);
3392 if (TYPE_PRECISION (bftype) != bitsize
3393 || TYPE_UNSIGNED (bftype) == !unsignedp)
3394 bftype = build_nonstandard_integer_type (bitsize, 0);
3396 result = build3 (BIT_FIELD_REF, bftype, inner,
3397 size_int (bitsize), bitsize_int (bitpos));
3398 SET_EXPR_LOCATION (result, loc);
3401 result = fold_convert_loc (loc, type, result);
3406 /* Optimize a bit-field compare.
3408 There are two cases: First is a compare against a constant and the
3409 second is a comparison of two items where the fields are at the same
3410 bit position relative to the start of a chunk (byte, halfword, word)
3411 large enough to contain it. In these cases we can avoid the shift
3412 implicit in bitfield extractions.
3414 For constants, we emit a compare of the shifted constant with the
3415 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3416 compared. For two fields at the same position, we do the ANDs with the
3417 similar mask and compare the result of the ANDs.
3419 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3420 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3421 are the left and right operands of the comparison, respectively.
3423 If the optimization described above can be done, we return the resulting
3424 tree. Otherwise we return zero. */
3427 optimize_bit_field_compare (location_t loc, enum tree_code code,
3428 tree compare_type, tree lhs, tree rhs)
3430 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3431 tree type = TREE_TYPE (lhs);
3432 tree signed_type, unsigned_type;
3433 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3434 enum machine_mode lmode, rmode, nmode;
3435 int lunsignedp, runsignedp;
3436 int lvolatilep = 0, rvolatilep = 0;
3437 tree linner, rinner = NULL_TREE;
3441 /* Get all the information about the extractions being done. If the bit size
3442 if the same as the size of the underlying object, we aren't doing an
3443 extraction at all and so can do nothing. We also don't want to
3444 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3445 then will no longer be able to replace it. */
3446 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3447 &lunsignedp, &lvolatilep, false);
3448 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3449 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3454 /* If this is not a constant, we can only do something if bit positions,
3455 sizes, and signedness are the same. */
3456 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3457 &runsignedp, &rvolatilep, false);
3459 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3460 || lunsignedp != runsignedp || offset != 0
3461 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3465 /* See if we can find a mode to refer to this field. We should be able to,
3466 but fail if we can't. */
3467 nmode = get_best_mode (lbitsize, lbitpos,
3468 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3469 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3470 TYPE_ALIGN (TREE_TYPE (rinner))),
3471 word_mode, lvolatilep || rvolatilep);
3472 if (nmode == VOIDmode)
3475 /* Set signed and unsigned types of the precision of this mode for the
3477 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3478 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3480 /* Compute the bit position and size for the new reference and our offset
3481 within it. If the new reference is the same size as the original, we
3482 won't optimize anything, so return zero. */
3483 nbitsize = GET_MODE_BITSIZE (nmode);
3484 nbitpos = lbitpos & ~ (nbitsize - 1);
3486 if (nbitsize == lbitsize)
3489 if (BYTES_BIG_ENDIAN)
3490 lbitpos = nbitsize - lbitsize - lbitpos;
3492 /* Make the mask to be used against the extracted field. */
3493 mask = build_int_cst_type (unsigned_type, -1);
3494 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3495 mask = const_binop (RSHIFT_EXPR, mask,
3496 size_int (nbitsize - lbitsize - lbitpos), 0);
3499 /* If not comparing with constant, just rework the comparison
3501 return fold_build2_loc (loc, code, compare_type,
3502 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3503 make_bit_field_ref (loc, linner,
3508 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3509 make_bit_field_ref (loc, rinner,
3515 /* Otherwise, we are handling the constant case. See if the constant is too
3516 big for the field. Warn and return a tree of for 0 (false) if so. We do
3517 this not only for its own sake, but to avoid having to test for this
3518 error case below. If we didn't, we might generate wrong code.
3520 For unsigned fields, the constant shifted right by the field length should
3521 be all zero. For signed fields, the high-order bits should agree with
3526 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3527 fold_convert_loc (loc,
3528 unsigned_type, rhs),
3529 size_int (lbitsize), 0)))
3531 warning (0, "comparison is always %d due to width of bit-field",
3533 return constant_boolean_node (code == NE_EXPR, compare_type);
3538 tree tem = const_binop (RSHIFT_EXPR,
3539 fold_convert_loc (loc, signed_type, rhs),
3540 size_int (lbitsize - 1), 0);
3541 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3543 warning (0, "comparison is always %d due to width of bit-field",
3545 return constant_boolean_node (code == NE_EXPR, compare_type);
3549 /* Single-bit compares should always be against zero. */
3550 if (lbitsize == 1 && ! integer_zerop (rhs))
3552 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3553 rhs = build_int_cst (type, 0);
3556 /* Make a new bitfield reference, shift the constant over the
3557 appropriate number of bits and mask it with the computed mask
3558 (in case this was a signed field). If we changed it, make a new one. */
3559 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3562 TREE_SIDE_EFFECTS (lhs) = 1;
3563 TREE_THIS_VOLATILE (lhs) = 1;
3566 rhs = const_binop (BIT_AND_EXPR,
3567 const_binop (LSHIFT_EXPR,
3568 fold_convert_loc (loc, unsigned_type, rhs),
3569 size_int (lbitpos), 0),
3572 lhs = build2 (code, compare_type,
3573 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3575 SET_EXPR_LOCATION (lhs, loc);
3579 /* Subroutine for fold_truthop: decode a field reference.
3581 If EXP is a comparison reference, we return the innermost reference.
3583 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3584 set to the starting bit number.
3586 If the innermost field can be completely contained in a mode-sized
3587 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3589 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3590 otherwise it is not changed.
3592 *PUNSIGNEDP is set to the signedness of the field.
3594 *PMASK is set to the mask used. This is either contained in a
3595 BIT_AND_EXPR or derived from the width of the field.
3597 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3599 Return 0 if this is not a component reference or is one that we can't
3600 do anything with. */
3603 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3604 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3605 int *punsignedp, int *pvolatilep,
3606 tree *pmask, tree *pand_mask)
3608 tree outer_type = 0;
3610 tree mask, inner, offset;
3612 unsigned int precision;
3614 /* All the optimizations using this function assume integer fields.
3615 There are problems with FP fields since the type_for_size call
3616 below can fail for, e.g., XFmode. */
3617 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3620 /* We are interested in the bare arrangement of bits, so strip everything
3621 that doesn't affect the machine mode. However, record the type of the
3622 outermost expression if it may matter below. */
3623 if (CONVERT_EXPR_P (exp)
3624 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3625 outer_type = TREE_TYPE (exp);
3628 if (TREE_CODE (exp) == BIT_AND_EXPR)
3630 and_mask = TREE_OPERAND (exp, 1);
3631 exp = TREE_OPERAND (exp, 0);
3632 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3633 if (TREE_CODE (and_mask) != INTEGER_CST)
3637 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3638 punsignedp, pvolatilep, false);
3639 if ((inner == exp && and_mask == 0)
3640 || *pbitsize < 0 || offset != 0
3641 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3644 /* If the number of bits in the reference is the same as the bitsize of
3645 the outer type, then the outer type gives the signedness. Otherwise
3646 (in case of a small bitfield) the signedness is unchanged. */
3647 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3648 *punsignedp = TYPE_UNSIGNED (outer_type);
3650 /* Compute the mask to access the bitfield. */
3651 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3652 precision = TYPE_PRECISION (unsigned_type);
3654 mask = build_int_cst_type (unsigned_type, -1);
3656 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3657 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3659 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3661 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3662 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3665 *pand_mask = and_mask;
3669 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3673 all_ones_mask_p (const_tree mask, int size)
3675 tree type = TREE_TYPE (mask);
3676 unsigned int precision = TYPE_PRECISION (type);
3679 tmask = build_int_cst_type (signed_type_for (type), -1);
3682 tree_int_cst_equal (mask,
3683 const_binop (RSHIFT_EXPR,
3684 const_binop (LSHIFT_EXPR, tmask,
3685 size_int (precision - size),
3687 size_int (precision - size), 0));
3690 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3691 represents the sign bit of EXP's type. If EXP represents a sign
3692 or zero extension, also test VAL against the unextended type.
3693 The return value is the (sub)expression whose sign bit is VAL,
3694 or NULL_TREE otherwise. */
3697 sign_bit_p (tree exp, const_tree val)
3699 unsigned HOST_WIDE_INT mask_lo, lo;
3700 HOST_WIDE_INT mask_hi, hi;
3704 /* Tree EXP must have an integral type. */
3705 t = TREE_TYPE (exp);
3706 if (! INTEGRAL_TYPE_P (t))
3709 /* Tree VAL must be an integer constant. */
3710 if (TREE_CODE (val) != INTEGER_CST
3711 || TREE_OVERFLOW (val))
3714 width = TYPE_PRECISION (t);
3715 if (width > HOST_BITS_PER_WIDE_INT)
3717 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3720 mask_hi = ((unsigned HOST_WIDE_INT) -1
3721 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3727 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3730 mask_lo = ((unsigned HOST_WIDE_INT) -1
3731 >> (HOST_BITS_PER_WIDE_INT - width));
3734 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3735 treat VAL as if it were unsigned. */
3736 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3737 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3740 /* Handle extension from a narrower type. */
3741 if (TREE_CODE (exp) == NOP_EXPR
3742 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3743 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3748 /* Subroutine for fold_truthop: determine if an operand is simple enough
3749 to be evaluated unconditionally. */
3752 simple_operand_p (const_tree exp)
3754 /* Strip any conversions that don't change the machine mode. */
3757 return (CONSTANT_CLASS_P (exp)
3758 || TREE_CODE (exp) == SSA_NAME
3760 && ! TREE_ADDRESSABLE (exp)
3761 && ! TREE_THIS_VOLATILE (exp)
3762 && ! DECL_NONLOCAL (exp)
3763 /* Don't regard global variables as simple. They may be
3764 allocated in ways unknown to the compiler (shared memory,
3765 #pragma weak, etc). */
3766 && ! TREE_PUBLIC (exp)
3767 && ! DECL_EXTERNAL (exp)
3768 /* Loading a static variable is unduly expensive, but global
3769 registers aren't expensive. */
3770 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3773 /* The following functions are subroutines to fold_range_test and allow it to
3774 try to change a logical combination of comparisons into a range test.
3777 X == 2 || X == 3 || X == 4 || X == 5
3781 (unsigned) (X - 2) <= 3
3783 We describe each set of comparisons as being either inside or outside
3784 a range, using a variable named like IN_P, and then describe the
3785 range with a lower and upper bound. If one of the bounds is omitted,
3786 it represents either the highest or lowest value of the type.
3788 In the comments below, we represent a range by two numbers in brackets
3789 preceded by a "+" to designate being inside that range, or a "-" to
3790 designate being outside that range, so the condition can be inverted by
3791 flipping the prefix. An omitted bound is represented by a "-". For
3792 example, "- [-, 10]" means being outside the range starting at the lowest
3793 possible value and ending at 10, in other words, being greater than 10.
3794 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3797 We set up things so that the missing bounds are handled in a consistent
3798 manner so neither a missing bound nor "true" and "false" need to be
3799 handled using a special case. */
3801 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3802 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3803 and UPPER1_P are nonzero if the respective argument is an upper bound
3804 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3805 must be specified for a comparison. ARG1 will be converted to ARG0's
3806 type if both are specified. */
3809 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3810 tree arg1, int upper1_p)
3816 /* If neither arg represents infinity, do the normal operation.
3817 Else, if not a comparison, return infinity. Else handle the special
3818 comparison rules. Note that most of the cases below won't occur, but
3819 are handled for consistency. */
3821 if (arg0 != 0 && arg1 != 0)
3823 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3824 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3826 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3829 if (TREE_CODE_CLASS (code) != tcc_comparison)
3832 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3833 for neither. In real maths, we cannot assume open ended ranges are
3834 the same. But, this is computer arithmetic, where numbers are finite.
3835 We can therefore make the transformation of any unbounded range with
3836 the value Z, Z being greater than any representable number. This permits
3837 us to treat unbounded ranges as equal. */
3838 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3839 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3843 result = sgn0 == sgn1;
3846 result = sgn0 != sgn1;
3849 result = sgn0 < sgn1;
3852 result = sgn0 <= sgn1;
3855 result = sgn0 > sgn1;
3858 result = sgn0 >= sgn1;
3864 return constant_boolean_node (result, type);
3867 /* Given EXP, a logical expression, set the range it is testing into
3868 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3869 actually being tested. *PLOW and *PHIGH will be made of the same
3870 type as the returned expression. If EXP is not a comparison, we
3871 will most likely not be returning a useful value and range. Set
3872 *STRICT_OVERFLOW_P to true if the return value is only valid
3873 because signed overflow is undefined; otherwise, do not change
3874 *STRICT_OVERFLOW_P. */
3877 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3878 bool *strict_overflow_p)
3880 enum tree_code code;
3881 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3882 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3884 tree low, high, n_low, n_high;
3885 location_t loc = EXPR_LOCATION (exp);
3887 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3888 and see if we can refine the range. Some of the cases below may not
3889 happen, but it doesn't seem worth worrying about this. We "continue"
3890 the outer loop when we've changed something; otherwise we "break"
3891 the switch, which will "break" the while. */
3894 low = high = build_int_cst (TREE_TYPE (exp), 0);
3898 code = TREE_CODE (exp);
3899 exp_type = TREE_TYPE (exp);
3901 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3903 if (TREE_OPERAND_LENGTH (exp) > 0)
3904 arg0 = TREE_OPERAND (exp, 0);
3905 if (TREE_CODE_CLASS (code) == tcc_comparison
3906 || TREE_CODE_CLASS (code) == tcc_unary
3907 || TREE_CODE_CLASS (code) == tcc_binary)
3908 arg0_type = TREE_TYPE (arg0);
3909 if (TREE_CODE_CLASS (code) == tcc_binary
3910 || TREE_CODE_CLASS (code) == tcc_comparison
3911 || (TREE_CODE_CLASS (code) == tcc_expression
3912 && TREE_OPERAND_LENGTH (exp) > 1))
3913 arg1 = TREE_OPERAND (exp, 1);
3918 case TRUTH_NOT_EXPR:
3919 in_p = ! in_p, exp = arg0;
3922 case EQ_EXPR: case NE_EXPR:
3923 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3924 /* We can only do something if the range is testing for zero
3925 and if the second operand is an integer constant. Note that
3926 saying something is "in" the range we make is done by
3927 complementing IN_P since it will set in the initial case of
3928 being not equal to zero; "out" is leaving it alone. */
3929 if (low == 0 || high == 0
3930 || ! integer_zerop (low) || ! integer_zerop (high)
3931 || TREE_CODE (arg1) != INTEGER_CST)
3936 case NE_EXPR: /* - [c, c] */
3939 case EQ_EXPR: /* + [c, c] */
3940 in_p = ! in_p, low = high = arg1;
3942 case GT_EXPR: /* - [-, c] */
3943 low = 0, high = arg1;
3945 case GE_EXPR: /* + [c, -] */
3946 in_p = ! in_p, low = arg1, high = 0;
3948 case LT_EXPR: /* - [c, -] */
3949 low = arg1, high = 0;
3951 case LE_EXPR: /* + [-, c] */
3952 in_p = ! in_p, low = 0, high = arg1;
3958 /* If this is an unsigned comparison, we also know that EXP is
3959 greater than or equal to zero. We base the range tests we make
3960 on that fact, so we record it here so we can parse existing
3961 range tests. We test arg0_type since often the return type
3962 of, e.g. EQ_EXPR, is boolean. */
3963 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3965 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3967 build_int_cst (arg0_type, 0),
3971 in_p = n_in_p, low = n_low, high = n_high;
3973 /* If the high bound is missing, but we have a nonzero low
3974 bound, reverse the range so it goes from zero to the low bound
3976 if (high == 0 && low && ! integer_zerop (low))
3979 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3980 integer_one_node, 0);
3981 low = build_int_cst (arg0_type, 0);
3989 /* (-x) IN [a,b] -> x in [-b, -a] */
3990 n_low = range_binop (MINUS_EXPR, exp_type,
3991 build_int_cst (exp_type, 0),
3993 n_high = range_binop (MINUS_EXPR, exp_type,
3994 build_int_cst (exp_type, 0),
3996 low = n_low, high = n_high;
4002 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4003 build_int_cst (exp_type, 1));
4004 SET_EXPR_LOCATION (exp, loc);
4007 case PLUS_EXPR: case MINUS_EXPR:
4008 if (TREE_CODE (arg1) != INTEGER_CST)
4011 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4012 move a constant to the other side. */
4013 if (!TYPE_UNSIGNED (arg0_type)
4014 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4017 /* If EXP is signed, any overflow in the computation is undefined,
4018 so we don't worry about it so long as our computations on
4019 the bounds don't overflow. For unsigned, overflow is defined
4020 and this is exactly the right thing. */
4021 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4022 arg0_type, low, 0, arg1, 0);
4023 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4024 arg0_type, high, 1, arg1, 0);
4025 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4026 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4029 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4030 *strict_overflow_p = true;
4032 /* Check for an unsigned range which has wrapped around the maximum
4033 value thus making n_high < n_low, and normalize it. */
4034 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4036 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4037 integer_one_node, 0);
4038 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4039 integer_one_node, 0);
4041 /* If the range is of the form +/- [ x+1, x ], we won't
4042 be able to normalize it. But then, it represents the
4043 whole range or the empty set, so make it
4045 if (tree_int_cst_equal (n_low, low)
4046 && tree_int_cst_equal (n_high, high))
4052 low = n_low, high = n_high;
4057 CASE_CONVERT: case NON_LVALUE_EXPR:
4058 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4061 if (! INTEGRAL_TYPE_P (arg0_type)
4062 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4063 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4066 n_low = low, n_high = high;
4069 n_low = fold_convert_loc (loc, arg0_type, n_low);
4072 n_high = fold_convert_loc (loc, arg0_type, n_high);
4075 /* If we're converting arg0 from an unsigned type, to exp,
4076 a signed type, we will be doing the comparison as unsigned.
4077 The tests above have already verified that LOW and HIGH
4080 So we have to ensure that we will handle large unsigned
4081 values the same way that the current signed bounds treat
4084 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4088 /* For fixed-point modes, we need to pass the saturating flag
4089 as the 2nd parameter. */
4090 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4091 equiv_type = lang_hooks.types.type_for_mode
4092 (TYPE_MODE (arg0_type),
4093 TYPE_SATURATING (arg0_type));
4095 equiv_type = lang_hooks.types.type_for_mode
4096 (TYPE_MODE (arg0_type), 1);
4098 /* A range without an upper bound is, naturally, unbounded.
4099 Since convert would have cropped a very large value, use
4100 the max value for the destination type. */
4102 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4103 : TYPE_MAX_VALUE (arg0_type);
4105 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4106 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4107 fold_convert_loc (loc, arg0_type,
4109 build_int_cst (arg0_type, 1));
4111 /* If the low bound is specified, "and" the range with the
4112 range for which the original unsigned value will be
4116 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4117 1, n_low, n_high, 1,
4118 fold_convert_loc (loc, arg0_type,
4123 in_p = (n_in_p == in_p);
4127 /* Otherwise, "or" the range with the range of the input
4128 that will be interpreted as negative. */
4129 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4130 0, n_low, n_high, 1,
4131 fold_convert_loc (loc, arg0_type,
4136 in_p = (in_p != n_in_p);
4141 low = n_low, high = n_high;
4151 /* If EXP is a constant, we can evaluate whether this is true or false. */
4152 if (TREE_CODE (exp) == INTEGER_CST)
4154 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4156 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4162 *pin_p = in_p, *plow = low, *phigh = high;
4166 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4167 type, TYPE, return an expression to test if EXP is in (or out of, depending
4168 on IN_P) the range. Return 0 if the test couldn't be created. */
4171 build_range_check (location_t loc, tree type, tree exp, int in_p,
4172 tree low, tree high)
4174 tree etype = TREE_TYPE (exp), value;
4176 #ifdef HAVE_canonicalize_funcptr_for_compare
4177 /* Disable this optimization for function pointer expressions
4178 on targets that require function pointer canonicalization. */
4179 if (HAVE_canonicalize_funcptr_for_compare
4180 && TREE_CODE (etype) == POINTER_TYPE
4181 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4187 value = build_range_check (loc, type, exp, 1, low, high);
4189 return invert_truthvalue_loc (loc, value);
4194 if (low == 0 && high == 0)
4195 return build_int_cst (type, 1);
4198 return fold_build2_loc (loc, LE_EXPR, type, exp,
4199 fold_convert_loc (loc, etype, high));
4202 return fold_build2_loc (loc, GE_EXPR, type, exp,
4203 fold_convert_loc (loc, etype, low));
4205 if (operand_equal_p (low, high, 0))
4206 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4207 fold_convert_loc (loc, etype, low));
4209 if (integer_zerop (low))
4211 if (! TYPE_UNSIGNED (etype))
4213 etype = unsigned_type_for (etype);
4214 high = fold_convert_loc (loc, etype, high);
4215 exp = fold_convert_loc (loc, etype, exp);
4217 return build_range_check (loc, type, exp, 1, 0, high);
4220 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4221 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4223 unsigned HOST_WIDE_INT lo;
4227 prec = TYPE_PRECISION (etype);
4228 if (prec <= HOST_BITS_PER_WIDE_INT)
4231 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4235 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4236 lo = (unsigned HOST_WIDE_INT) -1;
4239 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4241 if (TYPE_UNSIGNED (etype))
4243 tree signed_etype = signed_type_for (etype);
4244 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4246 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4248 etype = signed_etype;
4249 exp = fold_convert_loc (loc, etype, exp);
4251 return fold_build2_loc (loc, GT_EXPR, type, exp,
4252 build_int_cst (etype, 0));
4256 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4257 This requires wrap-around arithmetics for the type of the expression.
4258 First make sure that arithmetics in this type is valid, then make sure
4259 that it wraps around. */
4260 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4261 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4262 TYPE_UNSIGNED (etype));
4264 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4266 tree utype, minv, maxv;
4268 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4269 for the type in question, as we rely on this here. */
4270 utype = unsigned_type_for (etype);
4271 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4272 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4273 integer_one_node, 1);
4274 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4276 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4283 high = fold_convert_loc (loc, etype, high);
4284 low = fold_convert_loc (loc, etype, low);
4285 exp = fold_convert_loc (loc, etype, exp);
4287 value = const_binop (MINUS_EXPR, high, low, 0);
4290 if (POINTER_TYPE_P (etype))
4292 if (value != 0 && !TREE_OVERFLOW (value))
4294 low = fold_convert_loc (loc, sizetype, low);
4295 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4296 return build_range_check (loc, type,
4297 fold_build2_loc (loc, POINTER_PLUS_EXPR,
4299 1, build_int_cst (etype, 0), value);
4304 if (value != 0 && !TREE_OVERFLOW (value))
4305 return build_range_check (loc, type,
4306 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4307 1, build_int_cst (etype, 0), value);
4312 /* Return the predecessor of VAL in its type, handling the infinite case. */
4315 range_predecessor (tree val)
4317 tree type = TREE_TYPE (val);
4319 if (INTEGRAL_TYPE_P (type)
4320 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4323 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4326 /* Return the successor of VAL in its type, handling the infinite case. */
4329 range_successor (tree val)
4331 tree type = TREE_TYPE (val);
4333 if (INTEGRAL_TYPE_P (type)
4334 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4337 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4340 /* Given two ranges, see if we can merge them into one. Return 1 if we
4341 can, 0 if we can't. Set the output range into the specified parameters. */
4344 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4345 tree high0, int in1_p, tree low1, tree high1)
4353 int lowequal = ((low0 == 0 && low1 == 0)
4354 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4355 low0, 0, low1, 0)));
4356 int highequal = ((high0 == 0 && high1 == 0)
4357 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4358 high0, 1, high1, 1)));
4360 /* Make range 0 be the range that starts first, or ends last if they
4361 start at the same value. Swap them if it isn't. */
4362 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4365 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4366 high1, 1, high0, 1))))
4368 temp = in0_p, in0_p = in1_p, in1_p = temp;
4369 tem = low0, low0 = low1, low1 = tem;
4370 tem = high0, high0 = high1, high1 = tem;
4373 /* Now flag two cases, whether the ranges are disjoint or whether the
4374 second range is totally subsumed in the first. Note that the tests
4375 below are simplified by the ones above. */
4376 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4377 high0, 1, low1, 0));
4378 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4379 high1, 1, high0, 1));
4381 /* We now have four cases, depending on whether we are including or
4382 excluding the two ranges. */
4385 /* If they don't overlap, the result is false. If the second range
4386 is a subset it is the result. Otherwise, the range is from the start
4387 of the second to the end of the first. */
4389 in_p = 0, low = high = 0;
4391 in_p = 1, low = low1, high = high1;
4393 in_p = 1, low = low1, high = high0;
4396 else if (in0_p && ! in1_p)
4398 /* If they don't overlap, the result is the first range. If they are
4399 equal, the result is false. If the second range is a subset of the
4400 first, and the ranges begin at the same place, we go from just after
4401 the end of the second range to the end of the first. If the second
4402 range is not a subset of the first, or if it is a subset and both
4403 ranges end at the same place, the range starts at the start of the
4404 first range and ends just before the second range.
4405 Otherwise, we can't describe this as a single range. */
4407 in_p = 1, low = low0, high = high0;
4408 else if (lowequal && highequal)
4409 in_p = 0, low = high = 0;
4410 else if (subset && lowequal)
4412 low = range_successor (high1);
4417 /* We are in the weird situation where high0 > high1 but
4418 high1 has no successor. Punt. */
4422 else if (! subset || highequal)
4425 high = range_predecessor (low1);
4429 /* low0 < low1 but low1 has no predecessor. Punt. */
4437 else if (! in0_p && in1_p)
4439 /* If they don't overlap, the result is the second range. If the second
4440 is a subset of the first, the result is false. Otherwise,
4441 the range starts just after the first range and ends at the
4442 end of the second. */
4444 in_p = 1, low = low1, high = high1;
4445 else if (subset || highequal)
4446 in_p = 0, low = high = 0;
4449 low = range_successor (high0);
4454 /* high1 > high0 but high0 has no successor. Punt. */
4462 /* The case where we are excluding both ranges. Here the complex case
4463 is if they don't overlap. In that case, the only time we have a
4464 range is if they are adjacent. If the second is a subset of the
4465 first, the result is the first. Otherwise, the range to exclude
4466 starts at the beginning of the first range and ends at the end of the
4470 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4471 range_successor (high0),
4473 in_p = 0, low = low0, high = high1;
4476 /* Canonicalize - [min, x] into - [-, x]. */
4477 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4478 switch (TREE_CODE (TREE_TYPE (low0)))
4481 if (TYPE_PRECISION (TREE_TYPE (low0))
4482 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4486 if (tree_int_cst_equal (low0,
4487 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4491 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4492 && integer_zerop (low0))
4499 /* Canonicalize - [x, max] into - [x, -]. */
4500 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4501 switch (TREE_CODE (TREE_TYPE (high1)))
4504 if (TYPE_PRECISION (TREE_TYPE (high1))
4505 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4509 if (tree_int_cst_equal (high1,
4510 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4514 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4515 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4517 integer_one_node, 1)))
4524 /* The ranges might be also adjacent between the maximum and
4525 minimum values of the given type. For
4526 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4527 return + [x + 1, y - 1]. */
4528 if (low0 == 0 && high1 == 0)
4530 low = range_successor (high0);
4531 high = range_predecessor (low1);
4532 if (low == 0 || high == 0)
4542 in_p = 0, low = low0, high = high0;
4544 in_p = 0, low = low0, high = high1;
4547 *pin_p = in_p, *plow = low, *phigh = high;
4552 /* Subroutine of fold, looking inside expressions of the form
4553 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4554 of the COND_EXPR. This function is being used also to optimize
4555 A op B ? C : A, by reversing the comparison first.
4557 Return a folded expression whose code is not a COND_EXPR
4558 anymore, or NULL_TREE if no folding opportunity is found. */
4561 fold_cond_expr_with_comparison (location_t loc, tree type,
4562 tree arg0, tree arg1, tree arg2)
4564 enum tree_code comp_code = TREE_CODE (arg0);
4565 tree arg00 = TREE_OPERAND (arg0, 0);
4566 tree arg01 = TREE_OPERAND (arg0, 1);
4567 tree arg1_type = TREE_TYPE (arg1);
4573 /* If we have A op 0 ? A : -A, consider applying the following
4576 A == 0? A : -A same as -A
4577 A != 0? A : -A same as A
4578 A >= 0? A : -A same as abs (A)
4579 A > 0? A : -A same as abs (A)
4580 A <= 0? A : -A same as -abs (A)
4581 A < 0? A : -A same as -abs (A)
4583 None of these transformations work for modes with signed
4584 zeros. If A is +/-0, the first two transformations will
4585 change the sign of the result (from +0 to -0, or vice
4586 versa). The last four will fix the sign of the result,
4587 even though the original expressions could be positive or
4588 negative, depending on the sign of A.
4590 Note that all these transformations are correct if A is
4591 NaN, since the two alternatives (A and -A) are also NaNs. */
4592 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4593 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4594 ? real_zerop (arg01)
4595 : integer_zerop (arg01))
4596 && ((TREE_CODE (arg2) == NEGATE_EXPR
4597 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4598 /* In the case that A is of the form X-Y, '-A' (arg2) may
4599 have already been folded to Y-X, check for that. */
4600 || (TREE_CODE (arg1) == MINUS_EXPR
4601 && TREE_CODE (arg2) == MINUS_EXPR
4602 && operand_equal_p (TREE_OPERAND (arg1, 0),
4603 TREE_OPERAND (arg2, 1), 0)
4604 && operand_equal_p (TREE_OPERAND (arg1, 1),
4605 TREE_OPERAND (arg2, 0), 0))))
4610 tem = fold_convert_loc (loc, arg1_type, arg1);
4611 return pedantic_non_lvalue_loc (loc,
4612 fold_convert_loc (loc, type,
4613 negate_expr (tem)));
4616 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4619 if (flag_trapping_math)
4624 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4625 arg1 = fold_convert_loc (loc, signed_type_for
4626 (TREE_TYPE (arg1)), arg1);
4627 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4628 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4631 if (flag_trapping_math)
4635 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4636 arg1 = fold_convert_loc (loc, signed_type_for
4637 (TREE_TYPE (arg1)), arg1);
4638 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4639 return negate_expr (fold_convert_loc (loc, type, tem));
4641 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4645 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4646 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4647 both transformations are correct when A is NaN: A != 0
4648 is then true, and A == 0 is false. */
4650 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4651 && integer_zerop (arg01) && integer_zerop (arg2))
4653 if (comp_code == NE_EXPR)
4654 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4655 else if (comp_code == EQ_EXPR)
4656 return build_int_cst (type, 0);
4659 /* Try some transformations of A op B ? A : B.
4661 A == B? A : B same as B
4662 A != B? A : B same as A
4663 A >= B? A : B same as max (A, B)
4664 A > B? A : B same as max (B, A)
4665 A <= B? A : B same as min (A, B)
4666 A < B? A : B same as min (B, A)
4668 As above, these transformations don't work in the presence
4669 of signed zeros. For example, if A and B are zeros of
4670 opposite sign, the first two transformations will change
4671 the sign of the result. In the last four, the original
4672 expressions give different results for (A=+0, B=-0) and
4673 (A=-0, B=+0), but the transformed expressions do not.
4675 The first two transformations are correct if either A or B
4676 is a NaN. In the first transformation, the condition will
4677 be false, and B will indeed be chosen. In the case of the
4678 second transformation, the condition A != B will be true,
4679 and A will be chosen.
4681 The conversions to max() and min() are not correct if B is
4682 a number and A is not. The conditions in the original
4683 expressions will be false, so all four give B. The min()
4684 and max() versions would give a NaN instead. */
4685 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4686 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4687 /* Avoid these transformations if the COND_EXPR may be used
4688 as an lvalue in the C++ front-end. PR c++/19199. */
4690 || (strcmp (lang_hooks.name, "GNU C++") != 0
4691 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4692 || ! maybe_lvalue_p (arg1)
4693 || ! maybe_lvalue_p (arg2)))
4695 tree comp_op0 = arg00;
4696 tree comp_op1 = arg01;
4697 tree comp_type = TREE_TYPE (comp_op0);
4699 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4700 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4710 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4712 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4717 /* In C++ a ?: expression can be an lvalue, so put the
4718 operand which will be used if they are equal first
4719 so that we can convert this back to the
4720 corresponding COND_EXPR. */
4721 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4723 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4724 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4725 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4726 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4727 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4728 comp_op1, comp_op0);
4729 return pedantic_non_lvalue_loc (loc,
4730 fold_convert_loc (loc, type, tem));
4737 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4739 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4740 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4741 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4742 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4743 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4744 comp_op1, comp_op0);
4745 return pedantic_non_lvalue_loc (loc,
4746 fold_convert_loc (loc, type, tem));
4750 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4751 return pedantic_non_lvalue_loc (loc,
4752 fold_convert_loc (loc, type, arg2));
4755 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4756 return pedantic_non_lvalue_loc (loc,
4757 fold_convert_loc (loc, type, arg1));
4760 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4765 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4766 we might still be able to simplify this. For example,
4767 if C1 is one less or one more than C2, this might have started
4768 out as a MIN or MAX and been transformed by this function.
4769 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4771 if (INTEGRAL_TYPE_P (type)
4772 && TREE_CODE (arg01) == INTEGER_CST
4773 && TREE_CODE (arg2) == INTEGER_CST)
4777 if (TREE_CODE (arg1) == INTEGER_CST)
4779 /* We can replace A with C1 in this case. */
4780 arg1 = fold_convert_loc (loc, type, arg01);
4781 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4784 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4785 MIN_EXPR, to preserve the signedness of the comparison. */
4786 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4788 && operand_equal_p (arg01,
4789 const_binop (PLUS_EXPR, arg2,
4790 build_int_cst (type, 1), 0),
4793 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4794 fold_convert_loc (loc, TREE_TYPE (arg00),
4796 return pedantic_non_lvalue_loc (loc,
4797 fold_convert_loc (loc, type, tem));
4802 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4804 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4806 && operand_equal_p (arg01,
4807 const_binop (MINUS_EXPR, arg2,
4808 build_int_cst (type, 1), 0),
4811 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4812 fold_convert_loc (loc, TREE_TYPE (arg00),
4814 return pedantic_non_lvalue_loc (loc,
4815 fold_convert_loc (loc, type, tem));
4820 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4821 MAX_EXPR, to preserve the signedness of the comparison. */
4822 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4824 && operand_equal_p (arg01,
4825 const_binop (MINUS_EXPR, arg2,
4826 build_int_cst (type, 1), 0),
4829 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4830 fold_convert_loc (loc, TREE_TYPE (arg00),
4832 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4837 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4838 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4840 && operand_equal_p (arg01,
4841 const_binop (PLUS_EXPR, arg2,
4842 build_int_cst (type, 1), 0),
4845 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4846 fold_convert_loc (loc, TREE_TYPE (arg00),
4848 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4862 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4863 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4864 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4868 /* EXP is some logical combination of boolean tests. See if we can
4869 merge it into some range test. Return the new tree if so. */
4872 fold_range_test (location_t loc, enum tree_code code, tree type,
4875 int or_op = (code == TRUTH_ORIF_EXPR
4876 || code == TRUTH_OR_EXPR);
4877 int in0_p, in1_p, in_p;
4878 tree low0, low1, low, high0, high1, high;
4879 bool strict_overflow_p = false;
4880 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4881 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4883 const char * const warnmsg = G_("assuming signed overflow does not occur "
4884 "when simplifying range test");
4886 /* If this is an OR operation, invert both sides; we will invert
4887 again at the end. */
4889 in0_p = ! in0_p, in1_p = ! in1_p;
4891 /* If both expressions are the same, if we can merge the ranges, and we
4892 can build the range test, return it or it inverted. If one of the
4893 ranges is always true or always false, consider it to be the same
4894 expression as the other. */
4895 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4896 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4898 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4900 : rhs != 0 ? rhs : integer_zero_node,
4903 if (strict_overflow_p)
4904 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4905 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4908 /* On machines where the branch cost is expensive, if this is a
4909 short-circuited branch and the underlying object on both sides
4910 is the same, make a non-short-circuit operation. */
4911 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4912 && lhs != 0 && rhs != 0
4913 && (code == TRUTH_ANDIF_EXPR
4914 || code == TRUTH_ORIF_EXPR)
4915 && operand_equal_p (lhs, rhs, 0))
4917 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4918 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4919 which cases we can't do this. */
4920 if (simple_operand_p (lhs))
4922 tem = build2 (code == TRUTH_ANDIF_EXPR
4923 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4925 SET_EXPR_LOCATION (tem, loc);
4929 else if (lang_hooks.decls.global_bindings_p () == 0
4930 && ! CONTAINS_PLACEHOLDER_P (lhs))
4932 tree common = save_expr (lhs);
4934 if (0 != (lhs = build_range_check (loc, type, common,
4935 or_op ? ! in0_p : in0_p,
4937 && (0 != (rhs = build_range_check (loc, type, common,
4938 or_op ? ! in1_p : in1_p,
4941 if (strict_overflow_p)
4942 fold_overflow_warning (warnmsg,
4943 WARN_STRICT_OVERFLOW_COMPARISON);
4944 tem = build2 (code == TRUTH_ANDIF_EXPR
4945 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4947 SET_EXPR_LOCATION (tem, loc);
4956 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4957 bit value. Arrange things so the extra bits will be set to zero if and
4958 only if C is signed-extended to its full width. If MASK is nonzero,
4959 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4962 unextend (tree c, int p, int unsignedp, tree mask)
4964 tree type = TREE_TYPE (c);
4965 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4968 if (p == modesize || unsignedp)
4971 /* We work by getting just the sign bit into the low-order bit, then
4972 into the high-order bit, then sign-extend. We then XOR that value
4974 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4975 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4977 /* We must use a signed type in order to get an arithmetic right shift.
4978 However, we must also avoid introducing accidental overflows, so that
4979 a subsequent call to integer_zerop will work. Hence we must
4980 do the type conversion here. At this point, the constant is either
4981 zero or one, and the conversion to a signed type can never overflow.
4982 We could get an overflow if this conversion is done anywhere else. */
4983 if (TYPE_UNSIGNED (type))
4984 temp = fold_convert (signed_type_for (type), temp);
4986 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4987 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4989 temp = const_binop (BIT_AND_EXPR, temp,
4990 fold_convert (TREE_TYPE (c), mask),
4992 /* If necessary, convert the type back to match the type of C. */
4993 if (TYPE_UNSIGNED (type))
4994 temp = fold_convert (type, temp);
4996 return fold_convert (type,
4997 const_binop (BIT_XOR_EXPR, c, temp, 0));
5000 /* Find ways of folding logical expressions of LHS and RHS:
5001 Try to merge two comparisons to the same innermost item.
5002 Look for range tests like "ch >= '0' && ch <= '9'".
5003 Look for combinations of simple terms on machines with expensive branches
5004 and evaluate the RHS unconditionally.
5006 For example, if we have p->a == 2 && p->b == 4 and we can make an
5007 object large enough to span both A and B, we can do this with a comparison
5008 against the object ANDed with the a mask.
5010 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5011 operations to do this with one comparison.
5013 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5014 function and the one above.
5016 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5017 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5019 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5022 We return the simplified tree or 0 if no optimization is possible. */
5025 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5028 /* If this is the "or" of two comparisons, we can do something if
5029 the comparisons are NE_EXPR. If this is the "and", we can do something
5030 if the comparisons are EQ_EXPR. I.e.,
5031 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5033 WANTED_CODE is this operation code. For single bit fields, we can
5034 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5035 comparison for one-bit fields. */
5037 enum tree_code wanted_code;
5038 enum tree_code lcode, rcode;
5039 tree ll_arg, lr_arg, rl_arg, rr_arg;
5040 tree ll_inner, lr_inner, rl_inner, rr_inner;
5041 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5042 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5043 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5044 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5045 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5046 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5047 enum machine_mode lnmode, rnmode;
5048 tree ll_mask, lr_mask, rl_mask, rr_mask;
5049 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5050 tree l_const, r_const;
5051 tree lntype, rntype, result;
5052 HOST_WIDE_INT first_bit, end_bit;
5054 tree orig_lhs = lhs, orig_rhs = rhs;
5055 enum tree_code orig_code = code;
5057 /* Start by getting the comparison codes. Fail if anything is volatile.
5058 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5059 it were surrounded with a NE_EXPR. */
5061 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5064 lcode = TREE_CODE (lhs);
5065 rcode = TREE_CODE (rhs);
5067 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5069 lhs = build2 (NE_EXPR, truth_type, lhs,
5070 build_int_cst (TREE_TYPE (lhs), 0));
5074 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5076 rhs = build2 (NE_EXPR, truth_type, rhs,
5077 build_int_cst (TREE_TYPE (rhs), 0));
5081 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5082 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5085 ll_arg = TREE_OPERAND (lhs, 0);
5086 lr_arg = TREE_OPERAND (lhs, 1);
5087 rl_arg = TREE_OPERAND (rhs, 0);
5088 rr_arg = TREE_OPERAND (rhs, 1);
5090 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5091 if (simple_operand_p (ll_arg)
5092 && simple_operand_p (lr_arg))
5095 if (operand_equal_p (ll_arg, rl_arg, 0)
5096 && operand_equal_p (lr_arg, rr_arg, 0))
5098 result = combine_comparisons (loc, code, lcode, rcode,
5099 truth_type, ll_arg, lr_arg);
5103 else if (operand_equal_p (ll_arg, rr_arg, 0)
5104 && operand_equal_p (lr_arg, rl_arg, 0))
5106 result = combine_comparisons (loc, code, lcode,
5107 swap_tree_comparison (rcode),
5108 truth_type, ll_arg, lr_arg);
5114 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5115 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5117 /* If the RHS can be evaluated unconditionally and its operands are
5118 simple, it wins to evaluate the RHS unconditionally on machines
5119 with expensive branches. In this case, this isn't a comparison
5120 that can be merged. Avoid doing this if the RHS is a floating-point
5121 comparison since those can trap. */
5123 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5125 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5126 && simple_operand_p (rl_arg)
5127 && simple_operand_p (rr_arg))
5129 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5130 if (code == TRUTH_OR_EXPR
5131 && lcode == NE_EXPR && integer_zerop (lr_arg)
5132 && rcode == NE_EXPR && integer_zerop (rr_arg)
5133 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5134 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5136 result = build2 (NE_EXPR, truth_type,
5137 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5139 build_int_cst (TREE_TYPE (ll_arg), 0));
5140 goto fold_truthop_exit;
5143 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5144 if (code == TRUTH_AND_EXPR
5145 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5146 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5147 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5148 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5150 result = build2 (EQ_EXPR, truth_type,
5151 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5153 build_int_cst (TREE_TYPE (ll_arg), 0));
5154 goto fold_truthop_exit;
5157 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5159 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5161 result = build2 (code, truth_type, lhs, rhs);
5162 goto fold_truthop_exit;
5168 /* See if the comparisons can be merged. Then get all the parameters for
5171 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5172 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5176 ll_inner = decode_field_reference (loc, ll_arg,
5177 &ll_bitsize, &ll_bitpos, &ll_mode,
5178 &ll_unsignedp, &volatilep, &ll_mask,
5180 lr_inner = decode_field_reference (loc, lr_arg,
5181 &lr_bitsize, &lr_bitpos, &lr_mode,
5182 &lr_unsignedp, &volatilep, &lr_mask,
5184 rl_inner = decode_field_reference (loc, rl_arg,
5185 &rl_bitsize, &rl_bitpos, &rl_mode,
5186 &rl_unsignedp, &volatilep, &rl_mask,
5188 rr_inner = decode_field_reference (loc, rr_arg,
5189 &rr_bitsize, &rr_bitpos, &rr_mode,
5190 &rr_unsignedp, &volatilep, &rr_mask,
5193 /* It must be true that the inner operation on the lhs of each
5194 comparison must be the same if we are to be able to do anything.
5195 Then see if we have constants. If not, the same must be true for
5197 if (volatilep || ll_inner == 0 || rl_inner == 0
5198 || ! operand_equal_p (ll_inner, rl_inner, 0))
5201 if (TREE_CODE (lr_arg) == INTEGER_CST
5202 && TREE_CODE (rr_arg) == INTEGER_CST)
5203 l_const = lr_arg, r_const = rr_arg;
5204 else if (lr_inner == 0 || rr_inner == 0
5205 || ! operand_equal_p (lr_inner, rr_inner, 0))
5208 l_const = r_const = 0;
5210 /* If either comparison code is not correct for our logical operation,
5211 fail. However, we can convert a one-bit comparison against zero into
5212 the opposite comparison against that bit being set in the field. */
5214 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5215 if (lcode != wanted_code)
5217 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5219 /* Make the left operand unsigned, since we are only interested
5220 in the value of one bit. Otherwise we are doing the wrong
5229 /* This is analogous to the code for l_const above. */
5230 if (rcode != wanted_code)
5232 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5241 /* See if we can find a mode that contains both fields being compared on
5242 the left. If we can't, fail. Otherwise, update all constants and masks
5243 to be relative to a field of that size. */
5244 first_bit = MIN (ll_bitpos, rl_bitpos);
5245 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5246 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5247 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5249 if (lnmode == VOIDmode)
5252 lnbitsize = GET_MODE_BITSIZE (lnmode);
5253 lnbitpos = first_bit & ~ (lnbitsize - 1);
5254 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5255 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5257 if (BYTES_BIG_ENDIAN)
5259 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5260 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5263 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5264 size_int (xll_bitpos), 0);
5265 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5266 size_int (xrl_bitpos), 0);
5270 l_const = fold_convert_loc (loc, lntype, l_const);
5271 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5272 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5273 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5274 fold_build1_loc (loc, BIT_NOT_EXPR,
5278 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5280 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5285 r_const = fold_convert_loc (loc, lntype, r_const);
5286 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5287 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5288 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5289 fold_build1_loc (loc, BIT_NOT_EXPR,
5293 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5295 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5299 /* If the right sides are not constant, do the same for it. Also,
5300 disallow this optimization if a size or signedness mismatch occurs
5301 between the left and right sides. */
5304 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5305 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5306 /* Make sure the two fields on the right
5307 correspond to the left without being swapped. */
5308 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5311 first_bit = MIN (lr_bitpos, rr_bitpos);
5312 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5313 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5314 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5316 if (rnmode == VOIDmode)
5319 rnbitsize = GET_MODE_BITSIZE (rnmode);
5320 rnbitpos = first_bit & ~ (rnbitsize - 1);
5321 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5322 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5324 if (BYTES_BIG_ENDIAN)
5326 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5327 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5330 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5332 size_int (xlr_bitpos), 0);
5333 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5335 size_int (xrr_bitpos), 0);
5337 /* Make a mask that corresponds to both fields being compared.
5338 Do this for both items being compared. If the operands are the
5339 same size and the bits being compared are in the same position
5340 then we can do this by masking both and comparing the masked
5342 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5343 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5344 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5346 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5347 ll_unsignedp || rl_unsignedp);
5348 if (! all_ones_mask_p (ll_mask, lnbitsize))
5349 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5351 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5352 lr_unsignedp || rr_unsignedp);
5353 if (! all_ones_mask_p (lr_mask, rnbitsize))
5354 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5356 result = build2 (wanted_code, truth_type, lhs, rhs);
5357 goto fold_truthop_exit;
5360 /* There is still another way we can do something: If both pairs of
5361 fields being compared are adjacent, we may be able to make a wider
5362 field containing them both.
5364 Note that we still must mask the lhs/rhs expressions. Furthermore,
5365 the mask must be shifted to account for the shift done by
5366 make_bit_field_ref. */
5367 if ((ll_bitsize + ll_bitpos == rl_bitpos
5368 && lr_bitsize + lr_bitpos == rr_bitpos)
5369 || (ll_bitpos == rl_bitpos + rl_bitsize
5370 && lr_bitpos == rr_bitpos + rr_bitsize))
5374 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5375 ll_bitsize + rl_bitsize,
5376 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5377 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5378 lr_bitsize + rr_bitsize,
5379 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5381 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5382 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5383 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5384 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5386 /* Convert to the smaller type before masking out unwanted bits. */
5388 if (lntype != rntype)
5390 if (lnbitsize > rnbitsize)
5392 lhs = fold_convert_loc (loc, rntype, lhs);
5393 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5396 else if (lnbitsize < rnbitsize)
5398 rhs = fold_convert_loc (loc, lntype, rhs);
5399 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5404 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5405 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5407 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5408 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5410 result = build2 (wanted_code, truth_type, lhs, rhs);
5411 goto fold_truthop_exit;
5417 /* Handle the case of comparisons with constants. If there is something in
5418 common between the masks, those bits of the constants must be the same.
5419 If not, the condition is always false. Test for this to avoid generating
5420 incorrect code below. */
5421 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5422 if (! integer_zerop (result)
5423 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5424 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5426 if (wanted_code == NE_EXPR)
5428 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5429 return constant_boolean_node (true, truth_type);
5433 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5434 return constant_boolean_node (false, truth_type);
5438 /* Construct the expression we will return. First get the component
5439 reference we will make. Unless the mask is all ones the width of
5440 that field, perform the mask operation. Then compare with the
5442 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5443 ll_unsignedp || rl_unsignedp);
5445 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5446 if (! all_ones_mask_p (ll_mask, lnbitsize))
5448 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5449 SET_EXPR_LOCATION (result, loc);
5452 result = build2 (wanted_code, truth_type, result,
5453 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5456 SET_EXPR_LOCATION (result, loc);
5460 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5464 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5468 enum tree_code op_code;
5471 int consts_equal, consts_lt;
5474 STRIP_SIGN_NOPS (arg0);
5476 op_code = TREE_CODE (arg0);
5477 minmax_const = TREE_OPERAND (arg0, 1);
5478 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5479 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5480 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5481 inner = TREE_OPERAND (arg0, 0);
5483 /* If something does not permit us to optimize, return the original tree. */
5484 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5485 || TREE_CODE (comp_const) != INTEGER_CST
5486 || TREE_OVERFLOW (comp_const)
5487 || TREE_CODE (minmax_const) != INTEGER_CST
5488 || TREE_OVERFLOW (minmax_const))
5491 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5492 and GT_EXPR, doing the rest with recursive calls using logical
5496 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5499 = optimize_minmax_comparison (loc,
5500 invert_tree_comparison (code, false),
5503 return invert_truthvalue_loc (loc, tem);
5509 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5510 optimize_minmax_comparison
5511 (loc, EQ_EXPR, type, arg0, comp_const),
5512 optimize_minmax_comparison
5513 (loc, GT_EXPR, type, arg0, comp_const));
5516 if (op_code == MAX_EXPR && consts_equal)
5517 /* MAX (X, 0) == 0 -> X <= 0 */
5518 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5520 else if (op_code == MAX_EXPR && consts_lt)
5521 /* MAX (X, 0) == 5 -> X == 5 */
5522 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5524 else if (op_code == MAX_EXPR)
5525 /* MAX (X, 0) == -1 -> false */
5526 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5528 else if (consts_equal)
5529 /* MIN (X, 0) == 0 -> X >= 0 */
5530 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5533 /* MIN (X, 0) == 5 -> false */
5534 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5537 /* MIN (X, 0) == -1 -> X == -1 */
5538 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5541 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5542 /* MAX (X, 0) > 0 -> X > 0
5543 MAX (X, 0) > 5 -> X > 5 */
5544 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5546 else if (op_code == MAX_EXPR)
5547 /* MAX (X, 0) > -1 -> true */
5548 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5550 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5551 /* MIN (X, 0) > 0 -> false
5552 MIN (X, 0) > 5 -> false */
5553 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5556 /* MIN (X, 0) > -1 -> X > -1 */
5557 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5564 /* T is an integer expression that is being multiplied, divided, or taken a
5565 modulus (CODE says which and what kind of divide or modulus) by a
5566 constant C. See if we can eliminate that operation by folding it with
5567 other operations already in T. WIDE_TYPE, if non-null, is a type that
5568 should be used for the computation if wider than our type.
5570 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5571 (X * 2) + (Y * 4). We must, however, be assured that either the original
5572 expression would not overflow or that overflow is undefined for the type
5573 in the language in question.
5575 If we return a non-null expression, it is an equivalent form of the
5576 original computation, but need not be in the original type.
5578 We set *STRICT_OVERFLOW_P to true if the return values depends on
5579 signed overflow being undefined. Otherwise we do not change
5580 *STRICT_OVERFLOW_P. */
5583 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5584 bool *strict_overflow_p)
5586 /* To avoid exponential search depth, refuse to allow recursion past
5587 three levels. Beyond that (1) it's highly unlikely that we'll find
5588 something interesting and (2) we've probably processed it before
5589 when we built the inner expression. */
5598 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5605 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5606 bool *strict_overflow_p)
5608 tree type = TREE_TYPE (t);
5609 enum tree_code tcode = TREE_CODE (t);
5610 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5611 > GET_MODE_SIZE (TYPE_MODE (type)))
5612 ? wide_type : type);
5614 int same_p = tcode == code;
5615 tree op0 = NULL_TREE, op1 = NULL_TREE;
5616 bool sub_strict_overflow_p;
5618 /* Don't deal with constants of zero here; they confuse the code below. */
5619 if (integer_zerop (c))
5622 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5623 op0 = TREE_OPERAND (t, 0);
5625 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5626 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5628 /* Note that we need not handle conditional operations here since fold
5629 already handles those cases. So just do arithmetic here. */
5633 /* For a constant, we can always simplify if we are a multiply
5634 or (for divide and modulus) if it is a multiple of our constant. */
5635 if (code == MULT_EXPR
5636 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5637 return const_binop (code, fold_convert (ctype, t),
5638 fold_convert (ctype, c), 0);
5641 CASE_CONVERT: case NON_LVALUE_EXPR:
5642 /* If op0 is an expression ... */
5643 if ((COMPARISON_CLASS_P (op0)
5644 || UNARY_CLASS_P (op0)
5645 || BINARY_CLASS_P (op0)
5646 || VL_EXP_CLASS_P (op0)
5647 || EXPRESSION_CLASS_P (op0))
5648 /* ... and has wrapping overflow, and its type is smaller
5649 than ctype, then we cannot pass through as widening. */
5650 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5651 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5652 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5653 && (TYPE_PRECISION (ctype)
5654 > TYPE_PRECISION (TREE_TYPE (op0))))
5655 /* ... or this is a truncation (t is narrower than op0),
5656 then we cannot pass through this narrowing. */
5657 || (TYPE_PRECISION (type)
5658 < TYPE_PRECISION (TREE_TYPE (op0)))
5659 /* ... or signedness changes for division or modulus,
5660 then we cannot pass through this conversion. */
5661 || (code != MULT_EXPR
5662 && (TYPE_UNSIGNED (ctype)
5663 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5664 /* ... or has undefined overflow while the converted to
5665 type has not, we cannot do the operation in the inner type
5666 as that would introduce undefined overflow. */
5667 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5668 && !TYPE_OVERFLOW_UNDEFINED (type))))
5671 /* Pass the constant down and see if we can make a simplification. If
5672 we can, replace this expression with the inner simplification for
5673 possible later conversion to our or some other type. */
5674 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5675 && TREE_CODE (t2) == INTEGER_CST
5676 && !TREE_OVERFLOW (t2)
5677 && (0 != (t1 = extract_muldiv (op0, t2, code,
5679 ? ctype : NULL_TREE,
5680 strict_overflow_p))))
5685 /* If widening the type changes it from signed to unsigned, then we
5686 must avoid building ABS_EXPR itself as unsigned. */
5687 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5689 tree cstype = (*signed_type_for) (ctype);
5690 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5693 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5694 return fold_convert (ctype, t1);
5698 /* If the constant is negative, we cannot simplify this. */
5699 if (tree_int_cst_sgn (c) == -1)
5703 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5705 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5708 case MIN_EXPR: case MAX_EXPR:
5709 /* If widening the type changes the signedness, then we can't perform
5710 this optimization as that changes the result. */
5711 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5714 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5715 sub_strict_overflow_p = false;
5716 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5717 &sub_strict_overflow_p)) != 0
5718 && (t2 = extract_muldiv (op1, c, code, wide_type,
5719 &sub_strict_overflow_p)) != 0)
5721 if (tree_int_cst_sgn (c) < 0)
5722 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5723 if (sub_strict_overflow_p)
5724 *strict_overflow_p = true;
5725 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5726 fold_convert (ctype, t2));
5730 case LSHIFT_EXPR: case RSHIFT_EXPR:
5731 /* If the second operand is constant, this is a multiplication
5732 or floor division, by a power of two, so we can treat it that
5733 way unless the multiplier or divisor overflows. Signed
5734 left-shift overflow is implementation-defined rather than
5735 undefined in C90, so do not convert signed left shift into
5737 if (TREE_CODE (op1) == INTEGER_CST
5738 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5739 /* const_binop may not detect overflow correctly,
5740 so check for it explicitly here. */
5741 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5742 && TREE_INT_CST_HIGH (op1) == 0
5743 && 0 != (t1 = fold_convert (ctype,
5744 const_binop (LSHIFT_EXPR,
5747 && !TREE_OVERFLOW (t1))
5748 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5749 ? MULT_EXPR : FLOOR_DIV_EXPR,
5751 fold_convert (ctype, op0),
5753 c, code, wide_type, strict_overflow_p);
5756 case PLUS_EXPR: case MINUS_EXPR:
5757 /* See if we can eliminate the operation on both sides. If we can, we
5758 can return a new PLUS or MINUS. If we can't, the only remaining
5759 cases where we can do anything are if the second operand is a
5761 sub_strict_overflow_p = false;
5762 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5763 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5764 if (t1 != 0 && t2 != 0
5765 && (code == MULT_EXPR
5766 /* If not multiplication, we can only do this if both operands
5767 are divisible by c. */
5768 || (multiple_of_p (ctype, op0, c)
5769 && multiple_of_p (ctype, op1, c))))
5771 if (sub_strict_overflow_p)
5772 *strict_overflow_p = true;
5773 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5774 fold_convert (ctype, t2));
5777 /* If this was a subtraction, negate OP1 and set it to be an addition.
5778 This simplifies the logic below. */
5779 if (tcode == MINUS_EXPR)
5781 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5782 /* If OP1 was not easily negatable, the constant may be OP0. */
5783 if (TREE_CODE (op0) == INTEGER_CST)
5794 if (TREE_CODE (op1) != INTEGER_CST)
5797 /* If either OP1 or C are negative, this optimization is not safe for
5798 some of the division and remainder types while for others we need
5799 to change the code. */
5800 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5802 if (code == CEIL_DIV_EXPR)
5803 code = FLOOR_DIV_EXPR;
5804 else if (code == FLOOR_DIV_EXPR)
5805 code = CEIL_DIV_EXPR;
5806 else if (code != MULT_EXPR
5807 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5811 /* If it's a multiply or a division/modulus operation of a multiple
5812 of our constant, do the operation and verify it doesn't overflow. */
5813 if (code == MULT_EXPR
5814 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5816 op1 = const_binop (code, fold_convert (ctype, op1),
5817 fold_convert (ctype, c), 0);
5818 /* We allow the constant to overflow with wrapping semantics. */
5820 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5826 /* If we have an unsigned type is not a sizetype, we cannot widen
5827 the operation since it will change the result if the original
5828 computation overflowed. */
5829 if (TYPE_UNSIGNED (ctype)
5830 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5834 /* If we were able to eliminate our operation from the first side,
5835 apply our operation to the second side and reform the PLUS. */
5836 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5837 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5839 /* The last case is if we are a multiply. In that case, we can
5840 apply the distributive law to commute the multiply and addition
5841 if the multiplication of the constants doesn't overflow. */
5842 if (code == MULT_EXPR)
5843 return fold_build2 (tcode, ctype,
5844 fold_build2 (code, ctype,
5845 fold_convert (ctype, op0),
5846 fold_convert (ctype, c)),
5852 /* We have a special case here if we are doing something like
5853 (C * 8) % 4 since we know that's zero. */
5854 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5855 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5856 /* If the multiplication can overflow we cannot optimize this.
5857 ??? Until we can properly mark individual operations as
5858 not overflowing we need to treat sizetype special here as
5859 stor-layout relies on this opimization to make
5860 DECL_FIELD_BIT_OFFSET always a constant. */
5861 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5862 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5863 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5864 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5865 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5867 *strict_overflow_p = true;
5868 return omit_one_operand (type, integer_zero_node, op0);
5871 /* ... fall through ... */
5873 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5874 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5875 /* If we can extract our operation from the LHS, do so and return a
5876 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5877 do something only if the second operand is a constant. */
5879 && (t1 = extract_muldiv (op0, c, code, wide_type,
5880 strict_overflow_p)) != 0)
5881 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5882 fold_convert (ctype, op1));
5883 else if (tcode == MULT_EXPR && code == MULT_EXPR
5884 && (t1 = extract_muldiv (op1, c, code, wide_type,
5885 strict_overflow_p)) != 0)
5886 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5887 fold_convert (ctype, t1));
5888 else if (TREE_CODE (op1) != INTEGER_CST)
5891 /* If these are the same operation types, we can associate them
5892 assuming no overflow. */
5894 && 0 != (t1 = int_const_binop (MULT_EXPR,
5895 fold_convert (ctype, op1),
5896 fold_convert (ctype, c), 1))
5897 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
5898 TREE_INT_CST_HIGH (t1),
5899 (TYPE_UNSIGNED (ctype)
5900 && tcode != MULT_EXPR) ? -1 : 1,
5901 TREE_OVERFLOW (t1)))
5902 && !TREE_OVERFLOW (t1))
5903 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5905 /* If these operations "cancel" each other, we have the main
5906 optimizations of this pass, which occur when either constant is a
5907 multiple of the other, in which case we replace this with either an
5908 operation or CODE or TCODE.
5910 If we have an unsigned type that is not a sizetype, we cannot do
5911 this since it will change the result if the original computation
5913 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5914 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5915 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5916 || (tcode == MULT_EXPR
5917 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5918 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5919 && code != MULT_EXPR)))
5921 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5923 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5924 *strict_overflow_p = true;
5925 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5926 fold_convert (ctype,
5927 const_binop (TRUNC_DIV_EXPR,
5930 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5932 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5933 *strict_overflow_p = true;
5934 return fold_build2 (code, ctype, fold_convert (ctype, op0),
5935 fold_convert (ctype,
5936 const_binop (TRUNC_DIV_EXPR,
5949 /* Return a node which has the indicated constant VALUE (either 0 or
5950 1), and is of the indicated TYPE. */
5953 constant_boolean_node (int value, tree type)
5955 if (type == integer_type_node)
5956 return value ? integer_one_node : integer_zero_node;
5957 else if (type == boolean_type_node)
5958 return value ? boolean_true_node : boolean_false_node;
5960 return build_int_cst (type, value);
5964 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5965 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5966 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5967 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5968 COND is the first argument to CODE; otherwise (as in the example
5969 given here), it is the second argument. TYPE is the type of the
5970 original expression. Return NULL_TREE if no simplification is
5974 fold_binary_op_with_conditional_arg (location_t loc,
5975 enum tree_code code,
5976 tree type, tree op0, tree op1,
5977 tree cond, tree arg, int cond_first_p)
5979 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5980 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5981 tree test, true_value, false_value;
5982 tree lhs = NULL_TREE;
5983 tree rhs = NULL_TREE;
5985 if (TREE_CODE (cond) == COND_EXPR)
5987 test = TREE_OPERAND (cond, 0);
5988 true_value = TREE_OPERAND (cond, 1);
5989 false_value = TREE_OPERAND (cond, 2);
5990 /* If this operand throws an expression, then it does not make
5991 sense to try to perform a logical or arithmetic operation
5993 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5995 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6000 tree testtype = TREE_TYPE (cond);
6002 true_value = constant_boolean_node (true, testtype);
6003 false_value = constant_boolean_node (false, testtype);
6006 /* This transformation is only worthwhile if we don't have to wrap ARG
6007 in a SAVE_EXPR and the operation can be simplified on at least one
6008 of the branches once its pushed inside the COND_EXPR. */
6009 if (!TREE_CONSTANT (arg)
6010 && (TREE_SIDE_EFFECTS (arg)
6011 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6014 arg = fold_convert_loc (loc, arg_type, arg);
6017 true_value = fold_convert_loc (loc, cond_type, true_value);
6019 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6021 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6025 false_value = fold_convert_loc (loc, cond_type, false_value);
6027 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6029 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6032 /* Check that we have simplified at least one of the branches. */
6033 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6036 return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6040 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6042 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6043 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6044 ADDEND is the same as X.
6046 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6047 and finite. The problematic cases are when X is zero, and its mode
6048 has signed zeros. In the case of rounding towards -infinity,
6049 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6050 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6053 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6055 if (!real_zerop (addend))
6058 /* Don't allow the fold with -fsignaling-nans. */
6059 if (HONOR_SNANS (TYPE_MODE (type)))
6062 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6063 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6066 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6067 if (TREE_CODE (addend) == REAL_CST
6068 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6071 /* The mode has signed zeros, and we have to honor their sign.
6072 In this situation, there is only one case we can return true for.
6073 X - 0 is the same as X unless rounding towards -infinity is
6075 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6078 /* Subroutine of fold() that checks comparisons of built-in math
6079 functions against real constants.
6081 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6082 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6083 is the type of the result and ARG0 and ARG1 are the operands of the
6084 comparison. ARG1 must be a TREE_REAL_CST.
6086 The function returns the constant folded tree if a simplification
6087 can be made, and NULL_TREE otherwise. */
6090 fold_mathfn_compare (location_t loc,
6091 enum built_in_function fcode, enum tree_code code,
6092 tree type, tree arg0, tree arg1)
6096 if (BUILTIN_SQRT_P (fcode))
6098 tree arg = CALL_EXPR_ARG (arg0, 0);
6099 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6101 c = TREE_REAL_CST (arg1);
6102 if (REAL_VALUE_NEGATIVE (c))
6104 /* sqrt(x) < y is always false, if y is negative. */
6105 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6106 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6108 /* sqrt(x) > y is always true, if y is negative and we
6109 don't care about NaNs, i.e. negative values of x. */
6110 if (code == NE_EXPR || !HONOR_NANS (mode))
6111 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6113 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6114 return fold_build2_loc (loc, GE_EXPR, type, arg,
6115 build_real (TREE_TYPE (arg), dconst0));
6117 else if (code == GT_EXPR || code == GE_EXPR)
6121 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6122 real_convert (&c2, mode, &c2);
6124 if (REAL_VALUE_ISINF (c2))
6126 /* sqrt(x) > y is x == +Inf, when y is very large. */
6127 if (HONOR_INFINITIES (mode))
6128 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6129 build_real (TREE_TYPE (arg), c2));
6131 /* sqrt(x) > y is always false, when y is very large
6132 and we don't care about infinities. */
6133 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6136 /* sqrt(x) > c is the same as x > c*c. */
6137 return fold_build2_loc (loc, code, type, arg,
6138 build_real (TREE_TYPE (arg), c2));
6140 else if (code == LT_EXPR || code == LE_EXPR)
6144 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6145 real_convert (&c2, mode, &c2);
6147 if (REAL_VALUE_ISINF (c2))
6149 /* sqrt(x) < y is always true, when y is a very large
6150 value and we don't care about NaNs or Infinities. */
6151 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6152 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6154 /* sqrt(x) < y is x != +Inf when y is very large and we
6155 don't care about NaNs. */
6156 if (! HONOR_NANS (mode))
6157 return fold_build2_loc (loc, NE_EXPR, type, arg,
6158 build_real (TREE_TYPE (arg), c2));
6160 /* sqrt(x) < y is x >= 0 when y is very large and we
6161 don't care about Infinities. */
6162 if (! HONOR_INFINITIES (mode))
6163 return fold_build2_loc (loc, GE_EXPR, type, arg,
6164 build_real (TREE_TYPE (arg), dconst0));
6166 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6167 if (lang_hooks.decls.global_bindings_p () != 0
6168 || CONTAINS_PLACEHOLDER_P (arg))
6171 arg = save_expr (arg);
6172 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6173 fold_build2_loc (loc, GE_EXPR, type, arg,
6174 build_real (TREE_TYPE (arg),
6176 fold_build2_loc (loc, NE_EXPR, type, arg,
6177 build_real (TREE_TYPE (arg),
6181 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6182 if (! HONOR_NANS (mode))
6183 return fold_build2_loc (loc, code, type, arg,
6184 build_real (TREE_TYPE (arg), c2));
6186 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6187 if (lang_hooks.decls.global_bindings_p () == 0
6188 && ! CONTAINS_PLACEHOLDER_P (arg))
6190 arg = save_expr (arg);
6191 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6192 fold_build2_loc (loc, GE_EXPR, type, arg,
6193 build_real (TREE_TYPE (arg),
6195 fold_build2_loc (loc, code, type, arg,
6196 build_real (TREE_TYPE (arg),
6205 /* Subroutine of fold() that optimizes comparisons against Infinities,
6206 either +Inf or -Inf.
6208 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6209 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6210 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6212 The function returns the constant folded tree if a simplification
6213 can be made, and NULL_TREE otherwise. */
6216 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6217 tree arg0, tree arg1)
6219 enum machine_mode mode;
6220 REAL_VALUE_TYPE max;
6224 mode = TYPE_MODE (TREE_TYPE (arg0));
6226 /* For negative infinity swap the sense of the comparison. */
6227 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6229 code = swap_tree_comparison (code);
6234 /* x > +Inf is always false, if with ignore sNANs. */
6235 if (HONOR_SNANS (mode))
6237 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6240 /* x <= +Inf is always true, if we don't case about NaNs. */
6241 if (! HONOR_NANS (mode))
6242 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6244 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6245 if (lang_hooks.decls.global_bindings_p () == 0
6246 && ! CONTAINS_PLACEHOLDER_P (arg0))
6248 arg0 = save_expr (arg0);
6249 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6255 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6256 real_maxval (&max, neg, mode);
6257 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6258 arg0, build_real (TREE_TYPE (arg0), max));
6261 /* x < +Inf is always equal to x <= DBL_MAX. */
6262 real_maxval (&max, neg, mode);
6263 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6264 arg0, build_real (TREE_TYPE (arg0), max));
6267 /* x != +Inf is always equal to !(x > DBL_MAX). */
6268 real_maxval (&max, neg, mode);
6269 if (! HONOR_NANS (mode))
6270 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6271 arg0, build_real (TREE_TYPE (arg0), max));
6273 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6274 arg0, build_real (TREE_TYPE (arg0), max));
6275 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6284 /* Subroutine of fold() that optimizes comparisons of a division by
6285 a nonzero integer constant against an integer constant, i.e.
6288 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6289 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6290 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6292 The function returns the constant folded tree if a simplification
6293 can be made, and NULL_TREE otherwise. */
6296 fold_div_compare (location_t loc,
6297 enum tree_code code, tree type, tree arg0, tree arg1)
6299 tree prod, tmp, hi, lo;
6300 tree arg00 = TREE_OPERAND (arg0, 0);
6301 tree arg01 = TREE_OPERAND (arg0, 1);
6302 unsigned HOST_WIDE_INT lpart;
6303 HOST_WIDE_INT hpart;
6304 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6308 /* We have to do this the hard way to detect unsigned overflow.
6309 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6310 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6311 TREE_INT_CST_HIGH (arg01),
6312 TREE_INT_CST_LOW (arg1),
6313 TREE_INT_CST_HIGH (arg1),
6314 &lpart, &hpart, unsigned_p);
6315 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6317 neg_overflow = false;
6321 tmp = int_const_binop (MINUS_EXPR, arg01,
6322 build_int_cst (TREE_TYPE (arg01), 1), 0);
6325 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6326 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6327 TREE_INT_CST_HIGH (prod),
6328 TREE_INT_CST_LOW (tmp),
6329 TREE_INT_CST_HIGH (tmp),
6330 &lpart, &hpart, unsigned_p);
6331 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6332 -1, overflow | TREE_OVERFLOW (prod));
6334 else if (tree_int_cst_sgn (arg01) >= 0)
6336 tmp = int_const_binop (MINUS_EXPR, arg01,
6337 build_int_cst (TREE_TYPE (arg01), 1), 0);
6338 switch (tree_int_cst_sgn (arg1))
6341 neg_overflow = true;
6342 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6347 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6352 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6362 /* A negative divisor reverses the relational operators. */
6363 code = swap_tree_comparison (code);
6365 tmp = int_const_binop (PLUS_EXPR, arg01,
6366 build_int_cst (TREE_TYPE (arg01), 1), 0);
6367 switch (tree_int_cst_sgn (arg1))
6370 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6375 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6380 neg_overflow = true;
6381 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6393 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6394 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6395 if (TREE_OVERFLOW (hi))
6396 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6397 if (TREE_OVERFLOW (lo))
6398 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6399 return build_range_check (loc, type, arg00, 1, lo, hi);
6402 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6403 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6404 if (TREE_OVERFLOW (hi))
6405 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6406 if (TREE_OVERFLOW (lo))
6407 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6408 return build_range_check (loc, type, arg00, 0, lo, hi);
6411 if (TREE_OVERFLOW (lo))
6413 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6414 return omit_one_operand_loc (loc, type, tmp, arg00);
6416 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6419 if (TREE_OVERFLOW (hi))
6421 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6422 return omit_one_operand_loc (loc, type, tmp, arg00);
6424 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6427 if (TREE_OVERFLOW (hi))
6429 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6430 return omit_one_operand_loc (loc, type, tmp, arg00);
6432 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6435 if (TREE_OVERFLOW (lo))
6437 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6438 return omit_one_operand_loc (loc, type, tmp, arg00);
6440 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6450 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6451 equality/inequality test, then return a simplified form of the test
6452 using a sign testing. Otherwise return NULL. TYPE is the desired
6456 fold_single_bit_test_into_sign_test (location_t loc,
6457 enum tree_code code, tree arg0, tree arg1,
6460 /* If this is testing a single bit, we can optimize the test. */
6461 if ((code == NE_EXPR || code == EQ_EXPR)
6462 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6463 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6465 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6466 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6467 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6469 if (arg00 != NULL_TREE
6470 /* This is only a win if casting to a signed type is cheap,
6471 i.e. when arg00's type is not a partial mode. */
6472 && TYPE_PRECISION (TREE_TYPE (arg00))
6473 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6475 tree stype = signed_type_for (TREE_TYPE (arg00));
6476 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6478 fold_convert_loc (loc, stype, arg00),
6479 build_int_cst (stype, 0));
6486 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6487 equality/inequality test, then return a simplified form of
6488 the test using shifts and logical operations. Otherwise return
6489 NULL. TYPE is the desired result type. */
6492 fold_single_bit_test (location_t loc, enum tree_code code,
6493 tree arg0, tree arg1, tree result_type)
6495 /* If this is testing a single bit, we can optimize the test. */
6496 if ((code == NE_EXPR || code == EQ_EXPR)
6497 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6498 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6500 tree inner = TREE_OPERAND (arg0, 0);
6501 tree type = TREE_TYPE (arg0);
6502 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6503 enum machine_mode operand_mode = TYPE_MODE (type);
6505 tree signed_type, unsigned_type, intermediate_type;
6508 /* First, see if we can fold the single bit test into a sign-bit
6510 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6515 /* Otherwise we have (A & C) != 0 where C is a single bit,
6516 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6517 Similarly for (A & C) == 0. */
6519 /* If INNER is a right shift of a constant and it plus BITNUM does
6520 not overflow, adjust BITNUM and INNER. */
6521 if (TREE_CODE (inner) == RSHIFT_EXPR
6522 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6523 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6524 && bitnum < TYPE_PRECISION (type)
6525 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6526 bitnum - TYPE_PRECISION (type)))
6528 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6529 inner = TREE_OPERAND (inner, 0);
6532 /* If we are going to be able to omit the AND below, we must do our
6533 operations as unsigned. If we must use the AND, we have a choice.
6534 Normally unsigned is faster, but for some machines signed is. */
6535 #ifdef LOAD_EXTEND_OP
6536 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6537 && !flag_syntax_only) ? 0 : 1;
6542 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6543 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6544 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6545 inner = fold_convert_loc (loc, intermediate_type, inner);
6548 inner = build2 (RSHIFT_EXPR, intermediate_type,
6549 inner, size_int (bitnum));
6551 one = build_int_cst (intermediate_type, 1);
6553 if (code == EQ_EXPR)
6554 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6556 /* Put the AND last so it can combine with more things. */
6557 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6559 /* Make sure to return the proper type. */
6560 inner = fold_convert_loc (loc, result_type, inner);
6567 /* Check whether we are allowed to reorder operands arg0 and arg1,
6568 such that the evaluation of arg1 occurs before arg0. */
6571 reorder_operands_p (const_tree arg0, const_tree arg1)
6573 if (! flag_evaluation_order)
6575 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6577 return ! TREE_SIDE_EFFECTS (arg0)
6578 && ! TREE_SIDE_EFFECTS (arg1);
6581 /* Test whether it is preferable two swap two operands, ARG0 and
6582 ARG1, for example because ARG0 is an integer constant and ARG1
6583 isn't. If REORDER is true, only recommend swapping if we can
6584 evaluate the operands in reverse order. */
6587 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6589 STRIP_SIGN_NOPS (arg0);
6590 STRIP_SIGN_NOPS (arg1);
6592 if (TREE_CODE (arg1) == INTEGER_CST)
6594 if (TREE_CODE (arg0) == INTEGER_CST)
6597 if (TREE_CODE (arg1) == REAL_CST)
6599 if (TREE_CODE (arg0) == REAL_CST)
6602 if (TREE_CODE (arg1) == FIXED_CST)
6604 if (TREE_CODE (arg0) == FIXED_CST)
6607 if (TREE_CODE (arg1) == COMPLEX_CST)
6609 if (TREE_CODE (arg0) == COMPLEX_CST)
6612 if (TREE_CONSTANT (arg1))
6614 if (TREE_CONSTANT (arg0))
6617 if (optimize_function_for_size_p (cfun))
6620 if (reorder && flag_evaluation_order
6621 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6624 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6625 for commutative and comparison operators. Ensuring a canonical
6626 form allows the optimizers to find additional redundancies without
6627 having to explicitly check for both orderings. */
6628 if (TREE_CODE (arg0) == SSA_NAME
6629 && TREE_CODE (arg1) == SSA_NAME
6630 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6633 /* Put SSA_NAMEs last. */
6634 if (TREE_CODE (arg1) == SSA_NAME)
6636 if (TREE_CODE (arg0) == SSA_NAME)
6639 /* Put variables last. */
6648 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6649 ARG0 is extended to a wider type. */
6652 fold_widened_comparison (location_t loc, enum tree_code code,
6653 tree type, tree arg0, tree arg1)
6655 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6657 tree shorter_type, outer_type;
6661 if (arg0_unw == arg0)
6663 shorter_type = TREE_TYPE (arg0_unw);
6665 #ifdef HAVE_canonicalize_funcptr_for_compare
6666 /* Disable this optimization if we're casting a function pointer
6667 type on targets that require function pointer canonicalization. */
6668 if (HAVE_canonicalize_funcptr_for_compare
6669 && TREE_CODE (shorter_type) == POINTER_TYPE
6670 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6674 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6677 arg1_unw = get_unwidened (arg1, NULL_TREE);
6679 /* If possible, express the comparison in the shorter mode. */
6680 if ((code == EQ_EXPR || code == NE_EXPR
6681 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6682 && (TREE_TYPE (arg1_unw) == shorter_type
6683 || ((TYPE_PRECISION (shorter_type)
6684 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6685 && (TYPE_UNSIGNED (shorter_type)
6686 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6687 || (TREE_CODE (arg1_unw) == INTEGER_CST
6688 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6689 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6690 && int_fits_type_p (arg1_unw, shorter_type))))
6691 return fold_build2_loc (loc, code, type, arg0_unw,
6692 fold_convert_loc (loc, shorter_type, arg1_unw));
6694 if (TREE_CODE (arg1_unw) != INTEGER_CST
6695 || TREE_CODE (shorter_type) != INTEGER_TYPE
6696 || !int_fits_type_p (arg1_unw, shorter_type))
6699 /* If we are comparing with the integer that does not fit into the range
6700 of the shorter type, the result is known. */
6701 outer_type = TREE_TYPE (arg1_unw);
6702 min = lower_bound_in_type (outer_type, shorter_type);
6703 max = upper_bound_in_type (outer_type, shorter_type);
6705 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6707 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6714 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6719 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6725 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6727 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6732 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6734 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6743 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6744 ARG0 just the signedness is changed. */
6747 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6748 tree arg0, tree arg1)
6751 tree inner_type, outer_type;
6753 if (!CONVERT_EXPR_P (arg0))
6756 outer_type = TREE_TYPE (arg0);
6757 arg0_inner = TREE_OPERAND (arg0, 0);
6758 inner_type = TREE_TYPE (arg0_inner);
6760 #ifdef HAVE_canonicalize_funcptr_for_compare
6761 /* Disable this optimization if we're casting a function pointer
6762 type on targets that require function pointer canonicalization. */
6763 if (HAVE_canonicalize_funcptr_for_compare
6764 && TREE_CODE (inner_type) == POINTER_TYPE
6765 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6769 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6772 if (TREE_CODE (arg1) != INTEGER_CST
6773 && !(CONVERT_EXPR_P (arg1)
6774 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6777 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6778 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6783 if (TREE_CODE (arg1) == INTEGER_CST)
6784 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6785 TREE_INT_CST_HIGH (arg1), 0,
6786 TREE_OVERFLOW (arg1));
6788 arg1 = fold_convert_loc (loc, inner_type, arg1);
6790 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6793 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6794 step of the array. Reconstructs s and delta in the case of s *
6795 delta being an integer constant (and thus already folded). ADDR is
6796 the address. MULT is the multiplicative expression. If the
6797 function succeeds, the new address expression is returned.
6798 Otherwise NULL_TREE is returned. LOC is the location of the
6799 resulting expression. */
6802 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6804 tree s, delta, step;
6805 tree ref = TREE_OPERAND (addr, 0), pref;
6810 /* Strip the nops that might be added when converting op1 to sizetype. */
6813 /* Canonicalize op1 into a possibly non-constant delta
6814 and an INTEGER_CST s. */
6815 if (TREE_CODE (op1) == MULT_EXPR)
6817 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6822 if (TREE_CODE (arg0) == INTEGER_CST)
6827 else if (TREE_CODE (arg1) == INTEGER_CST)
6835 else if (TREE_CODE (op1) == INTEGER_CST)
6842 /* Simulate we are delta * 1. */
6844 s = integer_one_node;
6847 for (;; ref = TREE_OPERAND (ref, 0))
6849 if (TREE_CODE (ref) == ARRAY_REF)
6853 /* Remember if this was a multi-dimensional array. */
6854 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6857 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6860 itype = TREE_TYPE (domain);
6862 step = array_ref_element_size (ref);
6863 if (TREE_CODE (step) != INTEGER_CST)
6868 if (! tree_int_cst_equal (step, s))
6873 /* Try if delta is a multiple of step. */
6874 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6880 /* Only fold here if we can verify we do not overflow one
6881 dimension of a multi-dimensional array. */
6886 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6887 || !TYPE_MAX_VALUE (domain)
6888 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6891 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6892 fold_convert_loc (loc, itype,
6893 TREE_OPERAND (ref, 1)),
6894 fold_convert_loc (loc, itype, delta));
6896 || TREE_CODE (tmp) != INTEGER_CST
6897 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6906 if (!handled_component_p (ref))
6910 /* We found the suitable array reference. So copy everything up to it,
6911 and replace the index. */
6913 pref = TREE_OPERAND (addr, 0);
6914 ret = copy_node (pref);
6915 SET_EXPR_LOCATION (ret, loc);
6920 pref = TREE_OPERAND (pref, 0);
6921 TREE_OPERAND (pos, 0) = copy_node (pref);
6922 pos = TREE_OPERAND (pos, 0);
6925 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6926 fold_convert_loc (loc, itype,
6927 TREE_OPERAND (pos, 1)),
6928 fold_convert_loc (loc, itype, delta));
6930 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6934 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6935 means A >= Y && A != MAX, but in this case we know that
6936 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6939 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6941 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6943 if (TREE_CODE (bound) == LT_EXPR)
6944 a = TREE_OPERAND (bound, 0);
6945 else if (TREE_CODE (bound) == GT_EXPR)
6946 a = TREE_OPERAND (bound, 1);
6950 typea = TREE_TYPE (a);
6951 if (!INTEGRAL_TYPE_P (typea)
6952 && !POINTER_TYPE_P (typea))
6955 if (TREE_CODE (ineq) == LT_EXPR)
6957 a1 = TREE_OPERAND (ineq, 1);
6958 y = TREE_OPERAND (ineq, 0);
6960 else if (TREE_CODE (ineq) == GT_EXPR)
6962 a1 = TREE_OPERAND (ineq, 0);
6963 y = TREE_OPERAND (ineq, 1);
6968 if (TREE_TYPE (a1) != typea)
6971 if (POINTER_TYPE_P (typea))
6973 /* Convert the pointer types into integer before taking the difference. */
6974 tree ta = fold_convert_loc (loc, ssizetype, a);
6975 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6976 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6979 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6981 if (!diff || !integer_onep (diff))
6984 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6987 /* Fold a sum or difference of at least one multiplication.
6988 Returns the folded tree or NULL if no simplification could be made. */
6991 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6992 tree arg0, tree arg1)
6994 tree arg00, arg01, arg10, arg11;
6995 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6997 /* (A * C) +- (B * C) -> (A+-B) * C.
6998 (A * C) +- A -> A * (C+-1).
6999 We are most concerned about the case where C is a constant,
7000 but other combinations show up during loop reduction. Since
7001 it is not difficult, try all four possibilities. */
7003 if (TREE_CODE (arg0) == MULT_EXPR)
7005 arg00 = TREE_OPERAND (arg0, 0);
7006 arg01 = TREE_OPERAND (arg0, 1);
7008 else if (TREE_CODE (arg0) == INTEGER_CST)
7010 arg00 = build_one_cst (type);
7015 /* We cannot generate constant 1 for fract. */
7016 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7019 arg01 = build_one_cst (type);
7021 if (TREE_CODE (arg1) == MULT_EXPR)
7023 arg10 = TREE_OPERAND (arg1, 0);
7024 arg11 = TREE_OPERAND (arg1, 1);
7026 else if (TREE_CODE (arg1) == INTEGER_CST)
7028 arg10 = build_one_cst (type);
7029 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7030 the purpose of this canonicalization. */
7031 if (TREE_INT_CST_HIGH (arg1) == -1
7032 && negate_expr_p (arg1)
7033 && code == PLUS_EXPR)
7035 arg11 = negate_expr (arg1);
7043 /* We cannot generate constant 1 for fract. */
7044 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7047 arg11 = build_one_cst (type);
7051 if (operand_equal_p (arg01, arg11, 0))
7052 same = arg01, alt0 = arg00, alt1 = arg10;
7053 else if (operand_equal_p (arg00, arg10, 0))
7054 same = arg00, alt0 = arg01, alt1 = arg11;
7055 else if (operand_equal_p (arg00, arg11, 0))
7056 same = arg00, alt0 = arg01, alt1 = arg10;
7057 else if (operand_equal_p (arg01, arg10, 0))
7058 same = arg01, alt0 = arg00, alt1 = arg11;
7060 /* No identical multiplicands; see if we can find a common
7061 power-of-two factor in non-power-of-two multiplies. This
7062 can help in multi-dimensional array access. */
7063 else if (host_integerp (arg01, 0)
7064 && host_integerp (arg11, 0))
7066 HOST_WIDE_INT int01, int11, tmp;
7069 int01 = TREE_INT_CST_LOW (arg01);
7070 int11 = TREE_INT_CST_LOW (arg11);
7072 /* Move min of absolute values to int11. */
7073 if ((int01 >= 0 ? int01 : -int01)
7074 < (int11 >= 0 ? int11 : -int11))
7076 tmp = int01, int01 = int11, int11 = tmp;
7077 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7084 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7085 /* The remainder should not be a constant, otherwise we
7086 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7087 increased the number of multiplications necessary. */
7088 && TREE_CODE (arg10) != INTEGER_CST)
7090 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7091 build_int_cst (TREE_TYPE (arg00),
7096 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7101 return fold_build2_loc (loc, MULT_EXPR, type,
7102 fold_build2_loc (loc, code, type,
7103 fold_convert_loc (loc, type, alt0),
7104 fold_convert_loc (loc, type, alt1)),
7105 fold_convert_loc (loc, type, same));
7110 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7111 specified by EXPR into the buffer PTR of length LEN bytes.
7112 Return the number of bytes placed in the buffer, or zero
7116 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7118 tree type = TREE_TYPE (expr);
7119 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7120 int byte, offset, word, words;
7121 unsigned char value;
7123 if (total_bytes > len)
7125 words = total_bytes / UNITS_PER_WORD;
7127 for (byte = 0; byte < total_bytes; byte++)
7129 int bitpos = byte * BITS_PER_UNIT;
7130 if (bitpos < HOST_BITS_PER_WIDE_INT)
7131 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7133 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7134 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7136 if (total_bytes > UNITS_PER_WORD)
7138 word = byte / UNITS_PER_WORD;
7139 if (WORDS_BIG_ENDIAN)
7140 word = (words - 1) - word;
7141 offset = word * UNITS_PER_WORD;
7142 if (BYTES_BIG_ENDIAN)
7143 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7145 offset += byte % UNITS_PER_WORD;
7148 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7149 ptr[offset] = value;
7155 /* Subroutine of native_encode_expr. Encode the REAL_CST
7156 specified by EXPR into the buffer PTR of length LEN bytes.
7157 Return the number of bytes placed in the buffer, or zero
7161 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7163 tree type = TREE_TYPE (expr);
7164 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7165 int byte, offset, word, words, bitpos;
7166 unsigned char value;
7168 /* There are always 32 bits in each long, no matter the size of
7169 the hosts long. We handle floating point representations with
7173 if (total_bytes > len)
7175 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7177 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7179 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7180 bitpos += BITS_PER_UNIT)
7182 byte = (bitpos / BITS_PER_UNIT) & 3;
7183 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7185 if (UNITS_PER_WORD < 4)
7187 word = byte / UNITS_PER_WORD;
7188 if (WORDS_BIG_ENDIAN)
7189 word = (words - 1) - word;
7190 offset = word * UNITS_PER_WORD;
7191 if (BYTES_BIG_ENDIAN)
7192 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7194 offset += byte % UNITS_PER_WORD;
7197 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7198 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7203 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7204 specified by EXPR into the buffer PTR of length LEN bytes.
7205 Return the number of bytes placed in the buffer, or zero
7209 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7214 part = TREE_REALPART (expr);
7215 rsize = native_encode_expr (part, ptr, len);
7218 part = TREE_IMAGPART (expr);
7219 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7222 return rsize + isize;
7226 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7227 specified by EXPR into the buffer PTR of length LEN bytes.
7228 Return the number of bytes placed in the buffer, or zero
7232 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7234 int i, size, offset, count;
7235 tree itype, elem, elements;
7238 elements = TREE_VECTOR_CST_ELTS (expr);
7239 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7240 itype = TREE_TYPE (TREE_TYPE (expr));
7241 size = GET_MODE_SIZE (TYPE_MODE (itype));
7242 for (i = 0; i < count; i++)
7246 elem = TREE_VALUE (elements);
7247 elements = TREE_CHAIN (elements);
7254 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7259 if (offset + size > len)
7261 memset (ptr+offset, 0, size);
7269 /* Subroutine of native_encode_expr. Encode the STRING_CST
7270 specified by EXPR into the buffer PTR of length LEN bytes.
7271 Return the number of bytes placed in the buffer, or zero
7275 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7277 tree type = TREE_TYPE (expr);
7278 HOST_WIDE_INT total_bytes;
7280 if (TREE_CODE (type) != ARRAY_TYPE
7281 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7282 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7283 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7285 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7286 if (total_bytes > len)
7288 if (TREE_STRING_LENGTH (expr) < total_bytes)
7290 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7291 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7292 total_bytes - TREE_STRING_LENGTH (expr));
7295 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7300 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7301 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7302 buffer PTR of length LEN bytes. Return the number of bytes
7303 placed in the buffer, or zero upon failure. */
7306 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7308 switch (TREE_CODE (expr))
7311 return native_encode_int (expr, ptr, len);
7314 return native_encode_real (expr, ptr, len);
7317 return native_encode_complex (expr, ptr, len);
7320 return native_encode_vector (expr, ptr, len);
7323 return native_encode_string (expr, ptr, len);
7331 /* Subroutine of native_interpret_expr. Interpret the contents of
7332 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7333 If the buffer cannot be interpreted, return NULL_TREE. */
7336 native_interpret_int (tree type, const unsigned char *ptr, int len)
7338 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7339 int byte, offset, word, words;
7340 unsigned char value;
7341 unsigned int HOST_WIDE_INT lo = 0;
7342 HOST_WIDE_INT hi = 0;
7344 if (total_bytes > len)
7346 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7348 words = total_bytes / UNITS_PER_WORD;
7350 for (byte = 0; byte < total_bytes; byte++)
7352 int bitpos = byte * BITS_PER_UNIT;
7353 if (total_bytes > UNITS_PER_WORD)
7355 word = byte / UNITS_PER_WORD;
7356 if (WORDS_BIG_ENDIAN)
7357 word = (words - 1) - word;
7358 offset = word * UNITS_PER_WORD;
7359 if (BYTES_BIG_ENDIAN)
7360 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7362 offset += byte % UNITS_PER_WORD;
7365 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7366 value = ptr[offset];
7368 if (bitpos < HOST_BITS_PER_WIDE_INT)
7369 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7371 hi |= (unsigned HOST_WIDE_INT) value
7372 << (bitpos - HOST_BITS_PER_WIDE_INT);
7375 return build_int_cst_wide_type (type, lo, hi);
7379 /* Subroutine of native_interpret_expr. Interpret the contents of
7380 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7381 If the buffer cannot be interpreted, return NULL_TREE. */
7384 native_interpret_real (tree type, const unsigned char *ptr, int len)
7386 enum machine_mode mode = TYPE_MODE (type);
7387 int total_bytes = GET_MODE_SIZE (mode);
7388 int byte, offset, word, words, bitpos;
7389 unsigned char value;
7390 /* There are always 32 bits in each long, no matter the size of
7391 the hosts long. We handle floating point representations with
7396 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7397 if (total_bytes > len || total_bytes > 24)
7399 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7401 memset (tmp, 0, sizeof (tmp));
7402 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7403 bitpos += BITS_PER_UNIT)
7405 byte = (bitpos / BITS_PER_UNIT) & 3;
7406 if (UNITS_PER_WORD < 4)
7408 word = byte / UNITS_PER_WORD;
7409 if (WORDS_BIG_ENDIAN)
7410 word = (words - 1) - word;
7411 offset = word * UNITS_PER_WORD;
7412 if (BYTES_BIG_ENDIAN)
7413 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7415 offset += byte % UNITS_PER_WORD;
7418 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7419 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7421 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7424 real_from_target (&r, tmp, mode);
7425 return build_real (type, r);
7429 /* Subroutine of native_interpret_expr. Interpret the contents of
7430 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7431 If the buffer cannot be interpreted, return NULL_TREE. */
7434 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7436 tree etype, rpart, ipart;
7439 etype = TREE_TYPE (type);
7440 size = GET_MODE_SIZE (TYPE_MODE (etype));
7443 rpart = native_interpret_expr (etype, ptr, size);
7446 ipart = native_interpret_expr (etype, ptr+size, size);
7449 return build_complex (type, rpart, ipart);
7453 /* Subroutine of native_interpret_expr. Interpret the contents of
7454 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7455 If the buffer cannot be interpreted, return NULL_TREE. */
7458 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7460 tree etype, elem, elements;
7463 etype = TREE_TYPE (type);
7464 size = GET_MODE_SIZE (TYPE_MODE (etype));
7465 count = TYPE_VECTOR_SUBPARTS (type);
7466 if (size * count > len)
7469 elements = NULL_TREE;
7470 for (i = count - 1; i >= 0; i--)
7472 elem = native_interpret_expr (etype, ptr+(i*size), size);
7475 elements = tree_cons (NULL_TREE, elem, elements);
7477 return build_vector (type, elements);
7481 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7482 the buffer PTR of length LEN as a constant of type TYPE. For
7483 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7484 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7485 return NULL_TREE. */
7488 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7490 switch (TREE_CODE (type))
7495 return native_interpret_int (type, ptr, len);
7498 return native_interpret_real (type, ptr, len);
7501 return native_interpret_complex (type, ptr, len);
7504 return native_interpret_vector (type, ptr, len);
7512 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7513 TYPE at compile-time. If we're unable to perform the conversion
7514 return NULL_TREE. */
7517 fold_view_convert_expr (tree type, tree expr)
7519 /* We support up to 512-bit values (for V8DFmode). */
7520 unsigned char buffer[64];
7523 /* Check that the host and target are sane. */
7524 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7527 len = native_encode_expr (expr, buffer, sizeof (buffer));
7531 return native_interpret_expr (type, buffer, len);
7534 /* Build an expression for the address of T. Folds away INDIRECT_REF
7535 to avoid confusing the gimplify process. */
7538 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7540 /* The size of the object is not relevant when talking about its address. */
7541 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7542 t = TREE_OPERAND (t, 0);
7544 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7545 if (TREE_CODE (t) == INDIRECT_REF
7546 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7548 t = TREE_OPERAND (t, 0);
7550 if (TREE_TYPE (t) != ptrtype)
7552 t = build1 (NOP_EXPR, ptrtype, t);
7553 SET_EXPR_LOCATION (t, loc);
7556 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7558 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7560 if (TREE_TYPE (t) != ptrtype)
7561 t = fold_convert_loc (loc, ptrtype, t);
7565 t = build1 (ADDR_EXPR, ptrtype, t);
7566 SET_EXPR_LOCATION (t, loc);
7572 /* Build an expression for the address of T. */
7575 build_fold_addr_expr_loc (location_t loc, tree t)
7577 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7579 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7582 /* Fold a unary expression of code CODE and type TYPE with operand
7583 OP0. Return the folded expression if folding is successful.
7584 Otherwise, return NULL_TREE. */
7587 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7591 enum tree_code_class kind = TREE_CODE_CLASS (code);
7593 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7594 && TREE_CODE_LENGTH (code) == 1);
7599 if (CONVERT_EXPR_CODE_P (code)
7600 || code == FLOAT_EXPR || code == ABS_EXPR)
7602 /* Don't use STRIP_NOPS, because signedness of argument type
7604 STRIP_SIGN_NOPS (arg0);
7608 /* Strip any conversions that don't change the mode. This
7609 is safe for every expression, except for a comparison
7610 expression because its signedness is derived from its
7613 Note that this is done as an internal manipulation within
7614 the constant folder, in order to find the simplest
7615 representation of the arguments so that their form can be
7616 studied. In any cases, the appropriate type conversions
7617 should be put back in the tree that will get out of the
7623 if (TREE_CODE_CLASS (code) == tcc_unary)
7625 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7626 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7627 fold_build1_loc (loc, code, type,
7628 fold_convert_loc (loc, TREE_TYPE (op0),
7629 TREE_OPERAND (arg0, 1))));
7630 else if (TREE_CODE (arg0) == COND_EXPR)
7632 tree arg01 = TREE_OPERAND (arg0, 1);
7633 tree arg02 = TREE_OPERAND (arg0, 2);
7634 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7635 arg01 = fold_build1_loc (loc, code, type,
7636 fold_convert_loc (loc,
7637 TREE_TYPE (op0), arg01));
7638 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7639 arg02 = fold_build1_loc (loc, code, type,
7640 fold_convert_loc (loc,
7641 TREE_TYPE (op0), arg02));
7642 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7645 /* If this was a conversion, and all we did was to move into
7646 inside the COND_EXPR, bring it back out. But leave it if
7647 it is a conversion from integer to integer and the
7648 result precision is no wider than a word since such a
7649 conversion is cheap and may be optimized away by combine,
7650 while it couldn't if it were outside the COND_EXPR. Then return
7651 so we don't get into an infinite recursion loop taking the
7652 conversion out and then back in. */
7654 if ((CONVERT_EXPR_CODE_P (code)
7655 || code == NON_LVALUE_EXPR)
7656 && TREE_CODE (tem) == COND_EXPR
7657 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7658 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7659 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7660 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7661 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7662 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7663 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7665 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7666 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7667 || flag_syntax_only))
7669 tem = build1 (code, type,
7671 TREE_TYPE (TREE_OPERAND
7672 (TREE_OPERAND (tem, 1), 0)),
7673 TREE_OPERAND (tem, 0),
7674 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7675 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7676 SET_EXPR_LOCATION (tem, loc);
7680 else if (COMPARISON_CLASS_P (arg0))
7682 if (TREE_CODE (type) == BOOLEAN_TYPE)
7684 arg0 = copy_node (arg0);
7685 TREE_TYPE (arg0) = type;
7688 else if (TREE_CODE (type) != INTEGER_TYPE)
7689 return fold_build3_loc (loc, COND_EXPR, type, arg0,
7690 fold_build1_loc (loc, code, type,
7692 fold_build1_loc (loc, code, type,
7693 integer_zero_node));
7700 /* Re-association barriers around constants and other re-association
7701 barriers can be removed. */
7702 if (CONSTANT_CLASS_P (op0)
7703 || TREE_CODE (op0) == PAREN_EXPR)
7704 return fold_convert_loc (loc, type, op0);
7709 case FIX_TRUNC_EXPR:
7710 if (TREE_TYPE (op0) == type)
7713 /* If we have (type) (a CMP b) and type is an integral type, return
7714 new expression involving the new type. */
7715 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7716 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7717 TREE_OPERAND (op0, 1));
7719 /* Handle cases of two conversions in a row. */
7720 if (CONVERT_EXPR_P (op0))
7722 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7723 tree inter_type = TREE_TYPE (op0);
7724 int inside_int = INTEGRAL_TYPE_P (inside_type);
7725 int inside_ptr = POINTER_TYPE_P (inside_type);
7726 int inside_float = FLOAT_TYPE_P (inside_type);
7727 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7728 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7729 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7730 int inter_int = INTEGRAL_TYPE_P (inter_type);
7731 int inter_ptr = POINTER_TYPE_P (inter_type);
7732 int inter_float = FLOAT_TYPE_P (inter_type);
7733 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7734 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7735 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7736 int final_int = INTEGRAL_TYPE_P (type);
7737 int final_ptr = POINTER_TYPE_P (type);
7738 int final_float = FLOAT_TYPE_P (type);
7739 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7740 unsigned int final_prec = TYPE_PRECISION (type);
7741 int final_unsignedp = TYPE_UNSIGNED (type);
7743 /* In addition to the cases of two conversions in a row
7744 handled below, if we are converting something to its own
7745 type via an object of identical or wider precision, neither
7746 conversion is needed. */
7747 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7748 && (((inter_int || inter_ptr) && final_int)
7749 || (inter_float && final_float))
7750 && inter_prec >= final_prec)
7751 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7753 /* Likewise, if the intermediate and initial types are either both
7754 float or both integer, we don't need the middle conversion if the
7755 former is wider than the latter and doesn't change the signedness
7756 (for integers). Avoid this if the final type is a pointer since
7757 then we sometimes need the middle conversion. Likewise if the
7758 final type has a precision not equal to the size of its mode. */
7759 if (((inter_int && inside_int)
7760 || (inter_float && inside_float)
7761 || (inter_vec && inside_vec))
7762 && inter_prec >= inside_prec
7763 && (inter_float || inter_vec
7764 || inter_unsignedp == inside_unsignedp)
7765 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7766 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7768 && (! final_vec || inter_prec == inside_prec))
7769 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7771 /* If we have a sign-extension of a zero-extended value, we can
7772 replace that by a single zero-extension. */
7773 if (inside_int && inter_int && final_int
7774 && inside_prec < inter_prec && inter_prec < final_prec
7775 && inside_unsignedp && !inter_unsignedp)
7776 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7778 /* Two conversions in a row are not needed unless:
7779 - some conversion is floating-point (overstrict for now), or
7780 - some conversion is a vector (overstrict for now), or
7781 - the intermediate type is narrower than both initial and
7783 - the intermediate type and innermost type differ in signedness,
7784 and the outermost type is wider than the intermediate, or
7785 - the initial type is a pointer type and the precisions of the
7786 intermediate and final types differ, or
7787 - the final type is a pointer type and the precisions of the
7788 initial and intermediate types differ. */
7789 if (! inside_float && ! inter_float && ! final_float
7790 && ! inside_vec && ! inter_vec && ! final_vec
7791 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7792 && ! (inside_int && inter_int
7793 && inter_unsignedp != inside_unsignedp
7794 && inter_prec < final_prec)
7795 && ((inter_unsignedp && inter_prec > inside_prec)
7796 == (final_unsignedp && final_prec > inter_prec))
7797 && ! (inside_ptr && inter_prec != final_prec)
7798 && ! (final_ptr && inside_prec != inter_prec)
7799 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7800 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7801 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7804 /* Handle (T *)&A.B.C for A being of type T and B and C
7805 living at offset zero. This occurs frequently in
7806 C++ upcasting and then accessing the base. */
7807 if (TREE_CODE (op0) == ADDR_EXPR
7808 && POINTER_TYPE_P (type)
7809 && handled_component_p (TREE_OPERAND (op0, 0)))
7811 HOST_WIDE_INT bitsize, bitpos;
7813 enum machine_mode mode;
7814 int unsignedp, volatilep;
7815 tree base = TREE_OPERAND (op0, 0);
7816 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7817 &mode, &unsignedp, &volatilep, false);
7818 /* If the reference was to a (constant) zero offset, we can use
7819 the address of the base if it has the same base type
7820 as the result type and the pointer type is unqualified. */
7821 if (! offset && bitpos == 0
7822 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7823 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7824 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7825 return fold_convert_loc (loc, type,
7826 build_fold_addr_expr_loc (loc, base));
7829 if (TREE_CODE (op0) == MODIFY_EXPR
7830 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7831 /* Detect assigning a bitfield. */
7832 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7834 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7836 /* Don't leave an assignment inside a conversion
7837 unless assigning a bitfield. */
7838 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7839 /* First do the assignment, then return converted constant. */
7840 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7841 TREE_NO_WARNING (tem) = 1;
7842 TREE_USED (tem) = 1;
7843 SET_EXPR_LOCATION (tem, loc);
7847 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7848 constants (if x has signed type, the sign bit cannot be set
7849 in c). This folds extension into the BIT_AND_EXPR.
7850 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7851 very likely don't have maximal range for their precision and this
7852 transformation effectively doesn't preserve non-maximal ranges. */
7853 if (TREE_CODE (type) == INTEGER_TYPE
7854 && TREE_CODE (op0) == BIT_AND_EXPR
7855 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7857 tree and_expr = op0;
7858 tree and0 = TREE_OPERAND (and_expr, 0);
7859 tree and1 = TREE_OPERAND (and_expr, 1);
7862 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7863 || (TYPE_PRECISION (type)
7864 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7866 else if (TYPE_PRECISION (TREE_TYPE (and1))
7867 <= HOST_BITS_PER_WIDE_INT
7868 && host_integerp (and1, 1))
7870 unsigned HOST_WIDE_INT cst;
7872 cst = tree_low_cst (and1, 1);
7873 cst &= (HOST_WIDE_INT) -1
7874 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7875 change = (cst == 0);
7876 #ifdef LOAD_EXTEND_OP
7878 && !flag_syntax_only
7879 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7882 tree uns = unsigned_type_for (TREE_TYPE (and0));
7883 and0 = fold_convert_loc (loc, uns, and0);
7884 and1 = fold_convert_loc (loc, uns, and1);
7890 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7891 TREE_INT_CST_HIGH (and1), 0,
7892 TREE_OVERFLOW (and1));
7893 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7894 fold_convert_loc (loc, type, and0), tem);
7898 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7899 when one of the new casts will fold away. Conservatively we assume
7900 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7901 if (POINTER_TYPE_P (type)
7902 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7903 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7904 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7905 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7907 tree arg00 = TREE_OPERAND (arg0, 0);
7908 tree arg01 = TREE_OPERAND (arg0, 1);
7910 return fold_build2_loc (loc,
7911 TREE_CODE (arg0), type,
7912 fold_convert_loc (loc, type, arg00),
7913 fold_convert_loc (loc, sizetype, arg01));
7916 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7917 of the same precision, and X is an integer type not narrower than
7918 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7919 if (INTEGRAL_TYPE_P (type)
7920 && TREE_CODE (op0) == BIT_NOT_EXPR
7921 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7922 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7923 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7925 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7926 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7927 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7928 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7929 fold_convert_loc (loc, type, tem));
7932 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7933 type of X and Y (integer types only). */
7934 if (INTEGRAL_TYPE_P (type)
7935 && TREE_CODE (op0) == MULT_EXPR
7936 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7937 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7939 /* Be careful not to introduce new overflows. */
7941 if (TYPE_OVERFLOW_WRAPS (type))
7944 mult_type = unsigned_type_for (type);
7946 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7948 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7949 fold_convert_loc (loc, mult_type,
7950 TREE_OPERAND (op0, 0)),
7951 fold_convert_loc (loc, mult_type,
7952 TREE_OPERAND (op0, 1)));
7953 return fold_convert_loc (loc, type, tem);
7957 tem = fold_convert_const (code, type, op0);
7958 return tem ? tem : NULL_TREE;
7960 case ADDR_SPACE_CONVERT_EXPR:
7961 if (integer_zerop (arg0))
7962 return fold_convert_const (code, type, arg0);
7965 case FIXED_CONVERT_EXPR:
7966 tem = fold_convert_const (code, type, arg0);
7967 return tem ? tem : NULL_TREE;
7969 case VIEW_CONVERT_EXPR:
7970 if (TREE_TYPE (op0) == type)
7972 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7973 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7974 type, TREE_OPERAND (op0, 0));
7976 /* For integral conversions with the same precision or pointer
7977 conversions use a NOP_EXPR instead. */
7978 if ((INTEGRAL_TYPE_P (type)
7979 || POINTER_TYPE_P (type))
7980 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7981 || POINTER_TYPE_P (TREE_TYPE (op0)))
7982 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7983 return fold_convert_loc (loc, type, op0);
7985 /* Strip inner integral conversions that do not change the precision. */
7986 if (CONVERT_EXPR_P (op0)
7987 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7988 || POINTER_TYPE_P (TREE_TYPE (op0)))
7989 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7990 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7991 && (TYPE_PRECISION (TREE_TYPE (op0))
7992 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7993 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7994 type, TREE_OPERAND (op0, 0));
7996 return fold_view_convert_expr (type, op0);
7999 tem = fold_negate_expr (loc, arg0);
8001 return fold_convert_loc (loc, type, tem);
8005 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8006 return fold_abs_const (arg0, type);
8007 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8008 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8009 /* Convert fabs((double)float) into (double)fabsf(float). */
8010 else if (TREE_CODE (arg0) == NOP_EXPR
8011 && TREE_CODE (type) == REAL_TYPE)
8013 tree targ0 = strip_float_extensions (arg0);
8015 return fold_convert_loc (loc, type,
8016 fold_build1_loc (loc, ABS_EXPR,
8020 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8021 else if (TREE_CODE (arg0) == ABS_EXPR)
8023 else if (tree_expr_nonnegative_p (arg0))
8026 /* Strip sign ops from argument. */
8027 if (TREE_CODE (type) == REAL_TYPE)
8029 tem = fold_strip_sign_ops (arg0);
8031 return fold_build1_loc (loc, ABS_EXPR, type,
8032 fold_convert_loc (loc, type, tem));
8037 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8038 return fold_convert_loc (loc, type, arg0);
8039 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8041 tree itype = TREE_TYPE (type);
8042 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8043 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8044 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8045 negate_expr (ipart));
8047 if (TREE_CODE (arg0) == COMPLEX_CST)
8049 tree itype = TREE_TYPE (type);
8050 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8051 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8052 return build_complex (type, rpart, negate_expr (ipart));
8054 if (TREE_CODE (arg0) == CONJ_EXPR)
8055 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8059 if (TREE_CODE (arg0) == INTEGER_CST)
8060 return fold_not_const (arg0, type);
8061 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8062 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8063 /* Convert ~ (-A) to A - 1. */
8064 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8065 return fold_build2_loc (loc, MINUS_EXPR, type,
8066 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8067 build_int_cst (type, 1));
8068 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8069 else if (INTEGRAL_TYPE_P (type)
8070 && ((TREE_CODE (arg0) == MINUS_EXPR
8071 && integer_onep (TREE_OPERAND (arg0, 1)))
8072 || (TREE_CODE (arg0) == PLUS_EXPR
8073 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8074 return fold_build1_loc (loc, NEGATE_EXPR, type,
8075 fold_convert_loc (loc, type,
8076 TREE_OPERAND (arg0, 0)));
8077 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8078 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8079 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8080 fold_convert_loc (loc, type,
8081 TREE_OPERAND (arg0, 0)))))
8082 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8083 fold_convert_loc (loc, type,
8084 TREE_OPERAND (arg0, 1)));
8085 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8086 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8087 fold_convert_loc (loc, type,
8088 TREE_OPERAND (arg0, 1)))))
8089 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8090 fold_convert_loc (loc, type,
8091 TREE_OPERAND (arg0, 0)), tem);
8092 /* Perform BIT_NOT_EXPR on each element individually. */
8093 else if (TREE_CODE (arg0) == VECTOR_CST)
8095 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8096 int count = TYPE_VECTOR_SUBPARTS (type), i;
8098 for (i = 0; i < count; i++)
8102 elem = TREE_VALUE (elements);
8103 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8104 if (elem == NULL_TREE)
8106 elements = TREE_CHAIN (elements);
8109 elem = build_int_cst (TREE_TYPE (type), -1);
8110 list = tree_cons (NULL_TREE, elem, list);
8113 return build_vector (type, nreverse (list));
8118 case TRUTH_NOT_EXPR:
8119 /* The argument to invert_truthvalue must have Boolean type. */
8120 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8121 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8123 /* Note that the operand of this must be an int
8124 and its values must be 0 or 1.
8125 ("true" is a fixed value perhaps depending on the language,
8126 but we don't handle values other than 1 correctly yet.) */
8127 tem = fold_truth_not_expr (loc, arg0);
8130 return fold_convert_loc (loc, type, tem);
8133 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8134 return fold_convert_loc (loc, type, arg0);
8135 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8136 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8137 TREE_OPERAND (arg0, 1));
8138 if (TREE_CODE (arg0) == COMPLEX_CST)
8139 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8140 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8142 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8143 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8144 fold_build1_loc (loc, REALPART_EXPR, itype,
8145 TREE_OPERAND (arg0, 0)),
8146 fold_build1_loc (loc, REALPART_EXPR, itype,
8147 TREE_OPERAND (arg0, 1)));
8148 return fold_convert_loc (loc, type, tem);
8150 if (TREE_CODE (arg0) == CONJ_EXPR)
8152 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8153 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8154 TREE_OPERAND (arg0, 0));
8155 return fold_convert_loc (loc, type, tem);
8157 if (TREE_CODE (arg0) == CALL_EXPR)
8159 tree fn = get_callee_fndecl (arg0);
8160 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8161 switch (DECL_FUNCTION_CODE (fn))
8163 CASE_FLT_FN (BUILT_IN_CEXPI):
8164 fn = mathfn_built_in (type, BUILT_IN_COS);
8166 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8176 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8177 return fold_convert_loc (loc, type, integer_zero_node);
8178 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8179 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8180 TREE_OPERAND (arg0, 0));
8181 if (TREE_CODE (arg0) == COMPLEX_CST)
8182 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8183 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8185 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8186 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8187 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8188 TREE_OPERAND (arg0, 0)),
8189 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8190 TREE_OPERAND (arg0, 1)));
8191 return fold_convert_loc (loc, type, tem);
8193 if (TREE_CODE (arg0) == CONJ_EXPR)
8195 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8196 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8197 return fold_convert_loc (loc, type, negate_expr (tem));
8199 if (TREE_CODE (arg0) == CALL_EXPR)
8201 tree fn = get_callee_fndecl (arg0);
8202 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8203 switch (DECL_FUNCTION_CODE (fn))
8205 CASE_FLT_FN (BUILT_IN_CEXPI):
8206 fn = mathfn_built_in (type, BUILT_IN_SIN);
8208 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8218 /* Fold *&X to X if X is an lvalue. */
8219 if (TREE_CODE (op0) == ADDR_EXPR)
8221 tree op00 = TREE_OPERAND (op0, 0);
8222 if ((TREE_CODE (op00) == VAR_DECL
8223 || TREE_CODE (op00) == PARM_DECL
8224 || TREE_CODE (op00) == RESULT_DECL)
8225 && !TREE_READONLY (op00))
8232 } /* switch (code) */
8236 /* If the operation was a conversion do _not_ mark a resulting constant
8237 with TREE_OVERFLOW if the original constant was not. These conversions
8238 have implementation defined behavior and retaining the TREE_OVERFLOW
8239 flag here would confuse later passes such as VRP. */
8241 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8242 tree type, tree op0)
8244 tree res = fold_unary_loc (loc, code, type, op0);
8246 && TREE_CODE (res) == INTEGER_CST
8247 && TREE_CODE (op0) == INTEGER_CST
8248 && CONVERT_EXPR_CODE_P (code))
8249 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8254 /* Fold a binary expression of code CODE and type TYPE with operands
8255 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8256 Return the folded expression if folding is successful. Otherwise,
8257 return NULL_TREE. */
8260 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8262 enum tree_code compl_code;
8264 if (code == MIN_EXPR)
8265 compl_code = MAX_EXPR;
8266 else if (code == MAX_EXPR)
8267 compl_code = MIN_EXPR;
8271 /* MIN (MAX (a, b), b) == b. */
8272 if (TREE_CODE (op0) == compl_code
8273 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8274 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8276 /* MIN (MAX (b, a), b) == b. */
8277 if (TREE_CODE (op0) == compl_code
8278 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8279 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8280 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8282 /* MIN (a, MAX (a, b)) == a. */
8283 if (TREE_CODE (op1) == compl_code
8284 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8285 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8286 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8288 /* MIN (a, MAX (b, a)) == a. */
8289 if (TREE_CODE (op1) == compl_code
8290 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8291 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8292 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8297 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8298 by changing CODE to reduce the magnitude of constants involved in
8299 ARG0 of the comparison.
8300 Returns a canonicalized comparison tree if a simplification was
8301 possible, otherwise returns NULL_TREE.
8302 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8303 valid if signed overflow is undefined. */
8306 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8307 tree arg0, tree arg1,
8308 bool *strict_overflow_p)
8310 enum tree_code code0 = TREE_CODE (arg0);
8311 tree t, cst0 = NULL_TREE;
8315 /* Match A +- CST code arg1 and CST code arg1. We can change the
8316 first form only if overflow is undefined. */
8317 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8318 /* In principle pointers also have undefined overflow behavior,
8319 but that causes problems elsewhere. */
8320 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8321 && (code0 == MINUS_EXPR
8322 || code0 == PLUS_EXPR)
8323 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8324 || code0 == INTEGER_CST))
8327 /* Identify the constant in arg0 and its sign. */
8328 if (code0 == INTEGER_CST)
8331 cst0 = TREE_OPERAND (arg0, 1);
8332 sgn0 = tree_int_cst_sgn (cst0);
8334 /* Overflowed constants and zero will cause problems. */
8335 if (integer_zerop (cst0)
8336 || TREE_OVERFLOW (cst0))
8339 /* See if we can reduce the magnitude of the constant in
8340 arg0 by changing the comparison code. */
8341 if (code0 == INTEGER_CST)
8343 /* CST <= arg1 -> CST-1 < arg1. */
8344 if (code == LE_EXPR && sgn0 == 1)
8346 /* -CST < arg1 -> -CST-1 <= arg1. */
8347 else if (code == LT_EXPR && sgn0 == -1)
8349 /* CST > arg1 -> CST-1 >= arg1. */
8350 else if (code == GT_EXPR && sgn0 == 1)
8352 /* -CST >= arg1 -> -CST-1 > arg1. */
8353 else if (code == GE_EXPR && sgn0 == -1)
8357 /* arg1 code' CST' might be more canonical. */
8362 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8364 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8366 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8367 else if (code == GT_EXPR
8368 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8370 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8371 else if (code == LE_EXPR
8372 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8374 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8375 else if (code == GE_EXPR
8376 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8380 *strict_overflow_p = true;
8383 /* Now build the constant reduced in magnitude. But not if that
8384 would produce one outside of its types range. */
8385 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8387 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8388 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8390 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8391 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8392 /* We cannot swap the comparison here as that would cause us to
8393 endlessly recurse. */
8396 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8397 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8398 if (code0 != INTEGER_CST)
8399 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8401 /* If swapping might yield to a more canonical form, do so. */
8403 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8405 return fold_build2_loc (loc, code, type, t, arg1);
8408 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8409 overflow further. Try to decrease the magnitude of constants involved
8410 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8411 and put sole constants at the second argument position.
8412 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8415 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8416 tree arg0, tree arg1)
8419 bool strict_overflow_p;
8420 const char * const warnmsg = G_("assuming signed overflow does not occur "
8421 "when reducing constant in comparison");
8423 /* Try canonicalization by simplifying arg0. */
8424 strict_overflow_p = false;
8425 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8426 &strict_overflow_p);
8429 if (strict_overflow_p)
8430 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8434 /* Try canonicalization by simplifying arg1 using the swapped
8436 code = swap_tree_comparison (code);
8437 strict_overflow_p = false;
8438 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8439 &strict_overflow_p);
8440 if (t && strict_overflow_p)
8441 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8445 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8446 space. This is used to avoid issuing overflow warnings for
8447 expressions like &p->x which can not wrap. */
8450 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8452 unsigned HOST_WIDE_INT offset_low, total_low;
8453 HOST_WIDE_INT size, offset_high, total_high;
8455 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8461 if (offset == NULL_TREE)
8466 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8470 offset_low = TREE_INT_CST_LOW (offset);
8471 offset_high = TREE_INT_CST_HIGH (offset);
8474 if (add_double_with_sign (offset_low, offset_high,
8475 bitpos / BITS_PER_UNIT, 0,
8476 &total_low, &total_high,
8480 if (total_high != 0)
8483 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8487 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8489 if (TREE_CODE (base) == ADDR_EXPR)
8491 HOST_WIDE_INT base_size;
8493 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8494 if (base_size > 0 && size < base_size)
8498 return total_low > (unsigned HOST_WIDE_INT) size;
8501 /* Subroutine of fold_binary. This routine performs all of the
8502 transformations that are common to the equality/inequality
8503 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8504 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8505 fold_binary should call fold_binary. Fold a comparison with
8506 tree code CODE and type TYPE with operands OP0 and OP1. Return
8507 the folded comparison or NULL_TREE. */
8510 fold_comparison (location_t loc, enum tree_code code, tree type,
8513 tree arg0, arg1, tem;
8518 STRIP_SIGN_NOPS (arg0);
8519 STRIP_SIGN_NOPS (arg1);
8521 tem = fold_relational_const (code, type, arg0, arg1);
8522 if (tem != NULL_TREE)
8525 /* If one arg is a real or integer constant, put it last. */
8526 if (tree_swap_operands_p (arg0, arg1, true))
8527 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8529 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8530 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8531 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8532 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8533 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8534 && (TREE_CODE (arg1) == INTEGER_CST
8535 && !TREE_OVERFLOW (arg1)))
8537 tree const1 = TREE_OPERAND (arg0, 1);
8539 tree variable = TREE_OPERAND (arg0, 0);
8542 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8544 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8545 TREE_TYPE (arg1), const2, const1);
8547 /* If the constant operation overflowed this can be
8548 simplified as a comparison against INT_MAX/INT_MIN. */
8549 if (TREE_CODE (lhs) == INTEGER_CST
8550 && TREE_OVERFLOW (lhs))
8552 int const1_sgn = tree_int_cst_sgn (const1);
8553 enum tree_code code2 = code;
8555 /* Get the sign of the constant on the lhs if the
8556 operation were VARIABLE + CONST1. */
8557 if (TREE_CODE (arg0) == MINUS_EXPR)
8558 const1_sgn = -const1_sgn;
8560 /* The sign of the constant determines if we overflowed
8561 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8562 Canonicalize to the INT_MIN overflow by swapping the comparison
8564 if (const1_sgn == -1)
8565 code2 = swap_tree_comparison (code);
8567 /* We now can look at the canonicalized case
8568 VARIABLE + 1 CODE2 INT_MIN
8569 and decide on the result. */
8570 if (code2 == LT_EXPR
8572 || code2 == EQ_EXPR)
8573 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8574 else if (code2 == NE_EXPR
8576 || code2 == GT_EXPR)
8577 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8580 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8581 && (TREE_CODE (lhs) != INTEGER_CST
8582 || !TREE_OVERFLOW (lhs)))
8584 fold_overflow_warning (("assuming signed overflow does not occur "
8585 "when changing X +- C1 cmp C2 to "
8587 WARN_STRICT_OVERFLOW_COMPARISON);
8588 return fold_build2_loc (loc, code, type, variable, lhs);
8592 /* For comparisons of pointers we can decompose it to a compile time
8593 comparison of the base objects and the offsets into the object.
8594 This requires at least one operand being an ADDR_EXPR or a
8595 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8596 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8597 && (TREE_CODE (arg0) == ADDR_EXPR
8598 || TREE_CODE (arg1) == ADDR_EXPR
8599 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8600 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8602 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8603 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8604 enum machine_mode mode;
8605 int volatilep, unsignedp;
8606 bool indirect_base0 = false, indirect_base1 = false;
8608 /* Get base and offset for the access. Strip ADDR_EXPR for
8609 get_inner_reference, but put it back by stripping INDIRECT_REF
8610 off the base object if possible. indirect_baseN will be true
8611 if baseN is not an address but refers to the object itself. */
8613 if (TREE_CODE (arg0) == ADDR_EXPR)
8615 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8616 &bitsize, &bitpos0, &offset0, &mode,
8617 &unsignedp, &volatilep, false);
8618 if (TREE_CODE (base0) == INDIRECT_REF)
8619 base0 = TREE_OPERAND (base0, 0);
8621 indirect_base0 = true;
8623 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8625 base0 = TREE_OPERAND (arg0, 0);
8626 offset0 = TREE_OPERAND (arg0, 1);
8630 if (TREE_CODE (arg1) == ADDR_EXPR)
8632 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8633 &bitsize, &bitpos1, &offset1, &mode,
8634 &unsignedp, &volatilep, false);
8635 if (TREE_CODE (base1) == INDIRECT_REF)
8636 base1 = TREE_OPERAND (base1, 0);
8638 indirect_base1 = true;
8640 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8642 base1 = TREE_OPERAND (arg1, 0);
8643 offset1 = TREE_OPERAND (arg1, 1);
8646 /* If we have equivalent bases we might be able to simplify. */
8647 if (indirect_base0 == indirect_base1
8648 && operand_equal_p (base0, base1, 0))
8650 /* We can fold this expression to a constant if the non-constant
8651 offset parts are equal. */
8652 if ((offset0 == offset1
8653 || (offset0 && offset1
8654 && operand_equal_p (offset0, offset1, 0)))
8657 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8662 && bitpos0 != bitpos1
8663 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8664 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8665 fold_overflow_warning (("assuming pointer wraparound does not "
8666 "occur when comparing P +- C1 with "
8668 WARN_STRICT_OVERFLOW_CONDITIONAL);
8673 return constant_boolean_node (bitpos0 == bitpos1, type);
8675 return constant_boolean_node (bitpos0 != bitpos1, type);
8677 return constant_boolean_node (bitpos0 < bitpos1, type);
8679 return constant_boolean_node (bitpos0 <= bitpos1, type);
8681 return constant_boolean_node (bitpos0 >= bitpos1, type);
8683 return constant_boolean_node (bitpos0 > bitpos1, type);
8687 /* We can simplify the comparison to a comparison of the variable
8688 offset parts if the constant offset parts are equal.
8689 Be careful to use signed size type here because otherwise we
8690 mess with array offsets in the wrong way. This is possible
8691 because pointer arithmetic is restricted to retain within an
8692 object and overflow on pointer differences is undefined as of
8693 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8694 else if (bitpos0 == bitpos1
8695 && ((code == EQ_EXPR || code == NE_EXPR)
8696 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8698 /* By converting to signed size type we cover middle-end pointer
8699 arithmetic which operates on unsigned pointer types of size
8700 type size and ARRAY_REF offsets which are properly sign or
8701 zero extended from their type in case it is narrower than
8703 if (offset0 == NULL_TREE)
8704 offset0 = build_int_cst (ssizetype, 0);
8706 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8707 if (offset1 == NULL_TREE)
8708 offset1 = build_int_cst (ssizetype, 0);
8710 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8714 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8715 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8716 fold_overflow_warning (("assuming pointer wraparound does not "
8717 "occur when comparing P +- C1 with "
8719 WARN_STRICT_OVERFLOW_COMPARISON);
8721 return fold_build2_loc (loc, code, type, offset0, offset1);
8724 /* For non-equal bases we can simplify if they are addresses
8725 of local binding decls or constants. */
8726 else if (indirect_base0 && indirect_base1
8727 /* We know that !operand_equal_p (base0, base1, 0)
8728 because the if condition was false. But make
8729 sure two decls are not the same. */
8731 && TREE_CODE (arg0) == ADDR_EXPR
8732 && TREE_CODE (arg1) == ADDR_EXPR
8733 && (((TREE_CODE (base0) == VAR_DECL
8734 || TREE_CODE (base0) == PARM_DECL)
8735 && (targetm.binds_local_p (base0)
8736 || CONSTANT_CLASS_P (base1)))
8737 || CONSTANT_CLASS_P (base0))
8738 && (((TREE_CODE (base1) == VAR_DECL
8739 || TREE_CODE (base1) == PARM_DECL)
8740 && (targetm.binds_local_p (base1)
8741 || CONSTANT_CLASS_P (base0)))
8742 || CONSTANT_CLASS_P (base1)))
8744 if (code == EQ_EXPR)
8745 return omit_two_operands_loc (loc, type, boolean_false_node,
8747 else if (code == NE_EXPR)
8748 return omit_two_operands_loc (loc, type, boolean_true_node,
8751 /* For equal offsets we can simplify to a comparison of the
8753 else if (bitpos0 == bitpos1
8755 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8757 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8758 && ((offset0 == offset1)
8759 || (offset0 && offset1
8760 && operand_equal_p (offset0, offset1, 0))))
8763 base0 = build_fold_addr_expr_loc (loc, base0);
8765 base1 = build_fold_addr_expr_loc (loc, base1);
8766 return fold_build2_loc (loc, code, type, base0, base1);
8770 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8771 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8772 the resulting offset is smaller in absolute value than the
8774 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8775 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8776 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8777 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8778 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8779 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8780 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8782 tree const1 = TREE_OPERAND (arg0, 1);
8783 tree const2 = TREE_OPERAND (arg1, 1);
8784 tree variable1 = TREE_OPERAND (arg0, 0);
8785 tree variable2 = TREE_OPERAND (arg1, 0);
8787 const char * const warnmsg = G_("assuming signed overflow does not "
8788 "occur when combining constants around "
8791 /* Put the constant on the side where it doesn't overflow and is
8792 of lower absolute value than before. */
8793 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8794 ? MINUS_EXPR : PLUS_EXPR,
8796 if (!TREE_OVERFLOW (cst)
8797 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8799 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8800 return fold_build2_loc (loc, code, type,
8802 fold_build2_loc (loc,
8803 TREE_CODE (arg1), TREE_TYPE (arg1),
8807 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8808 ? MINUS_EXPR : PLUS_EXPR,
8810 if (!TREE_OVERFLOW (cst)
8811 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8813 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8814 return fold_build2_loc (loc, code, type,
8815 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8821 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8822 signed arithmetic case. That form is created by the compiler
8823 often enough for folding it to be of value. One example is in
8824 computing loop trip counts after Operator Strength Reduction. */
8825 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8826 && TREE_CODE (arg0) == MULT_EXPR
8827 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8828 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8829 && integer_zerop (arg1))
8831 tree const1 = TREE_OPERAND (arg0, 1);
8832 tree const2 = arg1; /* zero */
8833 tree variable1 = TREE_OPERAND (arg0, 0);
8834 enum tree_code cmp_code = code;
8836 /* Handle unfolded multiplication by zero. */
8837 if (integer_zerop (const1))
8838 return fold_build2_loc (loc, cmp_code, type, const1, const2);
8840 fold_overflow_warning (("assuming signed overflow does not occur when "
8841 "eliminating multiplication in comparison "
8843 WARN_STRICT_OVERFLOW_COMPARISON);
8845 /* If const1 is negative we swap the sense of the comparison. */
8846 if (tree_int_cst_sgn (const1) < 0)
8847 cmp_code = swap_tree_comparison (cmp_code);
8849 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8852 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8856 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8858 tree targ0 = strip_float_extensions (arg0);
8859 tree targ1 = strip_float_extensions (arg1);
8860 tree newtype = TREE_TYPE (targ0);
8862 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8863 newtype = TREE_TYPE (targ1);
8865 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8866 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8867 return fold_build2_loc (loc, code, type,
8868 fold_convert_loc (loc, newtype, targ0),
8869 fold_convert_loc (loc, newtype, targ1));
8871 /* (-a) CMP (-b) -> b CMP a */
8872 if (TREE_CODE (arg0) == NEGATE_EXPR
8873 && TREE_CODE (arg1) == NEGATE_EXPR)
8874 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8875 TREE_OPERAND (arg0, 0));
8877 if (TREE_CODE (arg1) == REAL_CST)
8879 REAL_VALUE_TYPE cst;
8880 cst = TREE_REAL_CST (arg1);
8882 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8883 if (TREE_CODE (arg0) == NEGATE_EXPR)
8884 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8885 TREE_OPERAND (arg0, 0),
8886 build_real (TREE_TYPE (arg1),
8887 REAL_VALUE_NEGATE (cst)));
8889 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8890 /* a CMP (-0) -> a CMP 0 */
8891 if (REAL_VALUE_MINUS_ZERO (cst))
8892 return fold_build2_loc (loc, code, type, arg0,
8893 build_real (TREE_TYPE (arg1), dconst0));
8895 /* x != NaN is always true, other ops are always false. */
8896 if (REAL_VALUE_ISNAN (cst)
8897 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8899 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8900 return omit_one_operand_loc (loc, type, tem, arg0);
8903 /* Fold comparisons against infinity. */
8904 if (REAL_VALUE_ISINF (cst)
8905 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8907 tem = fold_inf_compare (loc, code, type, arg0, arg1);
8908 if (tem != NULL_TREE)
8913 /* If this is a comparison of a real constant with a PLUS_EXPR
8914 or a MINUS_EXPR of a real constant, we can convert it into a
8915 comparison with a revised real constant as long as no overflow
8916 occurs when unsafe_math_optimizations are enabled. */
8917 if (flag_unsafe_math_optimizations
8918 && TREE_CODE (arg1) == REAL_CST
8919 && (TREE_CODE (arg0) == PLUS_EXPR
8920 || TREE_CODE (arg0) == MINUS_EXPR)
8921 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8922 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8923 ? MINUS_EXPR : PLUS_EXPR,
8924 arg1, TREE_OPERAND (arg0, 1), 0))
8925 && !TREE_OVERFLOW (tem))
8926 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8928 /* Likewise, we can simplify a comparison of a real constant with
8929 a MINUS_EXPR whose first operand is also a real constant, i.e.
8930 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
8931 floating-point types only if -fassociative-math is set. */
8932 if (flag_associative_math
8933 && TREE_CODE (arg1) == REAL_CST
8934 && TREE_CODE (arg0) == MINUS_EXPR
8935 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8936 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8938 && !TREE_OVERFLOW (tem))
8939 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8940 TREE_OPERAND (arg0, 1), tem);
8942 /* Fold comparisons against built-in math functions. */
8943 if (TREE_CODE (arg1) == REAL_CST
8944 && flag_unsafe_math_optimizations
8945 && ! flag_errno_math)
8947 enum built_in_function fcode = builtin_mathfn_code (arg0);
8949 if (fcode != END_BUILTINS)
8951 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
8952 if (tem != NULL_TREE)
8958 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8959 && CONVERT_EXPR_P (arg0))
8961 /* If we are widening one operand of an integer comparison,
8962 see if the other operand is similarly being widened. Perhaps we
8963 can do the comparison in the narrower type. */
8964 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
8968 /* Or if we are changing signedness. */
8969 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
8974 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8975 constant, we can simplify it. */
8976 if (TREE_CODE (arg1) == INTEGER_CST
8977 && (TREE_CODE (arg0) == MIN_EXPR
8978 || TREE_CODE (arg0) == MAX_EXPR)
8979 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8981 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8986 /* Simplify comparison of something with itself. (For IEEE
8987 floating-point, we can only do some of these simplifications.) */
8988 if (operand_equal_p (arg0, arg1, 0))
8993 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8994 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8995 return constant_boolean_node (1, type);
9000 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9001 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9002 return constant_boolean_node (1, type);
9003 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9006 /* For NE, we can only do this simplification if integer
9007 or we don't honor IEEE floating point NaNs. */
9008 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9009 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9011 /* ... fall through ... */
9014 return constant_boolean_node (0, type);
9020 /* If we are comparing an expression that just has comparisons
9021 of two integer values, arithmetic expressions of those comparisons,
9022 and constants, we can simplify it. There are only three cases
9023 to check: the two values can either be equal, the first can be
9024 greater, or the second can be greater. Fold the expression for
9025 those three values. Since each value must be 0 or 1, we have
9026 eight possibilities, each of which corresponds to the constant 0
9027 or 1 or one of the six possible comparisons.
9029 This handles common cases like (a > b) == 0 but also handles
9030 expressions like ((x > y) - (y > x)) > 0, which supposedly
9031 occur in macroized code. */
9033 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9035 tree cval1 = 0, cval2 = 0;
9038 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9039 /* Don't handle degenerate cases here; they should already
9040 have been handled anyway. */
9041 && cval1 != 0 && cval2 != 0
9042 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9043 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9044 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9045 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9046 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9047 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9048 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9050 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9051 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9053 /* We can't just pass T to eval_subst in case cval1 or cval2
9054 was the same as ARG1. */
9057 = fold_build2_loc (loc, code, type,
9058 eval_subst (loc, arg0, cval1, maxval,
9062 = fold_build2_loc (loc, code, type,
9063 eval_subst (loc, arg0, cval1, maxval,
9067 = fold_build2_loc (loc, code, type,
9068 eval_subst (loc, arg0, cval1, minval,
9072 /* All three of these results should be 0 or 1. Confirm they are.
9073 Then use those values to select the proper code to use. */
9075 if (TREE_CODE (high_result) == INTEGER_CST
9076 && TREE_CODE (equal_result) == INTEGER_CST
9077 && TREE_CODE (low_result) == INTEGER_CST)
9079 /* Make a 3-bit mask with the high-order bit being the
9080 value for `>', the next for '=', and the low for '<'. */
9081 switch ((integer_onep (high_result) * 4)
9082 + (integer_onep (equal_result) * 2)
9083 + integer_onep (low_result))
9087 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9108 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9113 tem = save_expr (build2 (code, type, cval1, cval2));
9114 SET_EXPR_LOCATION (tem, loc);
9117 return fold_build2_loc (loc, code, type, cval1, cval2);
9122 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9123 into a single range test. */
9124 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9125 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9126 && TREE_CODE (arg1) == INTEGER_CST
9127 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9128 && !integer_zerop (TREE_OPERAND (arg0, 1))
9129 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9130 && !TREE_OVERFLOW (arg1))
9132 tem = fold_div_compare (loc, code, type, arg0, arg1);
9133 if (tem != NULL_TREE)
9137 /* Fold ~X op ~Y as Y op X. */
9138 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9139 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9141 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9142 return fold_build2_loc (loc, code, type,
9143 fold_convert_loc (loc, cmp_type,
9144 TREE_OPERAND (arg1, 0)),
9145 TREE_OPERAND (arg0, 0));
9148 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9149 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9150 && TREE_CODE (arg1) == INTEGER_CST)
9152 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9153 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9154 TREE_OPERAND (arg0, 0),
9155 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9156 fold_convert_loc (loc, cmp_type, arg1)));
9163 /* Subroutine of fold_binary. Optimize complex multiplications of the
9164 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9165 argument EXPR represents the expression "z" of type TYPE. */
9168 fold_mult_zconjz (location_t loc, tree type, tree expr)
9170 tree itype = TREE_TYPE (type);
9171 tree rpart, ipart, tem;
9173 if (TREE_CODE (expr) == COMPLEX_EXPR)
9175 rpart = TREE_OPERAND (expr, 0);
9176 ipart = TREE_OPERAND (expr, 1);
9178 else if (TREE_CODE (expr) == COMPLEX_CST)
9180 rpart = TREE_REALPART (expr);
9181 ipart = TREE_IMAGPART (expr);
9185 expr = save_expr (expr);
9186 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9187 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9190 rpart = save_expr (rpart);
9191 ipart = save_expr (ipart);
9192 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9193 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9194 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9195 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9196 fold_convert_loc (loc, itype, integer_zero_node));
9200 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9201 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9202 guarantees that P and N have the same least significant log2(M) bits.
9203 N is not otherwise constrained. In particular, N is not normalized to
9204 0 <= N < M as is common. In general, the precise value of P is unknown.
9205 M is chosen as large as possible such that constant N can be determined.
9207 Returns M and sets *RESIDUE to N.
9209 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9210 account. This is not always possible due to PR 35705.
9213 static unsigned HOST_WIDE_INT
9214 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9215 bool allow_func_align)
9217 enum tree_code code;
9221 code = TREE_CODE (expr);
9222 if (code == ADDR_EXPR)
9224 expr = TREE_OPERAND (expr, 0);
9225 if (handled_component_p (expr))
9227 HOST_WIDE_INT bitsize, bitpos;
9229 enum machine_mode mode;
9230 int unsignedp, volatilep;
9232 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9233 &mode, &unsignedp, &volatilep, false);
9234 *residue = bitpos / BITS_PER_UNIT;
9237 if (TREE_CODE (offset) == INTEGER_CST)
9238 *residue += TREE_INT_CST_LOW (offset);
9240 /* We don't handle more complicated offset expressions. */
9246 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9247 return DECL_ALIGN_UNIT (expr);
9249 else if (code == POINTER_PLUS_EXPR)
9252 unsigned HOST_WIDE_INT modulus;
9253 enum tree_code inner_code;
9255 op0 = TREE_OPERAND (expr, 0);
9257 modulus = get_pointer_modulus_and_residue (op0, residue,
9260 op1 = TREE_OPERAND (expr, 1);
9262 inner_code = TREE_CODE (op1);
9263 if (inner_code == INTEGER_CST)
9265 *residue += TREE_INT_CST_LOW (op1);
9268 else if (inner_code == MULT_EXPR)
9270 op1 = TREE_OPERAND (op1, 1);
9271 if (TREE_CODE (op1) == INTEGER_CST)
9273 unsigned HOST_WIDE_INT align;
9275 /* Compute the greatest power-of-2 divisor of op1. */
9276 align = TREE_INT_CST_LOW (op1);
9279 /* If align is non-zero and less than *modulus, replace
9280 *modulus with align., If align is 0, then either op1 is 0
9281 or the greatest power-of-2 divisor of op1 doesn't fit in an
9282 unsigned HOST_WIDE_INT. In either case, no additional
9283 constraint is imposed. */
9285 modulus = MIN (modulus, align);
9292 /* If we get here, we were unable to determine anything useful about the
9298 /* Fold a binary expression of code CODE and type TYPE with operands
9299 OP0 and OP1. LOC is the location of the resulting expression.
9300 Return the folded expression if folding is successful. Otherwise,
9301 return NULL_TREE. */
9304 fold_binary_loc (location_t loc,
9305 enum tree_code code, tree type, tree op0, tree op1)
9307 enum tree_code_class kind = TREE_CODE_CLASS (code);
9308 tree arg0, arg1, tem;
9309 tree t1 = NULL_TREE;
9310 bool strict_overflow_p;
9312 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9313 && TREE_CODE_LENGTH (code) == 2
9315 && op1 != NULL_TREE);
9320 /* Strip any conversions that don't change the mode. This is
9321 safe for every expression, except for a comparison expression
9322 because its signedness is derived from its operands. So, in
9323 the latter case, only strip conversions that don't change the
9324 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9327 Note that this is done as an internal manipulation within the
9328 constant folder, in order to find the simplest representation
9329 of the arguments so that their form can be studied. In any
9330 cases, the appropriate type conversions should be put back in
9331 the tree that will get out of the constant folder. */
9333 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9335 STRIP_SIGN_NOPS (arg0);
9336 STRIP_SIGN_NOPS (arg1);
9344 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9345 constant but we can't do arithmetic on them. */
9346 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9347 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9348 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9349 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9350 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9351 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9353 if (kind == tcc_binary)
9355 /* Make sure type and arg0 have the same saturating flag. */
9356 gcc_assert (TYPE_SATURATING (type)
9357 == TYPE_SATURATING (TREE_TYPE (arg0)));
9358 tem = const_binop (code, arg0, arg1, 0);
9360 else if (kind == tcc_comparison)
9361 tem = fold_relational_const (code, type, arg0, arg1);
9365 if (tem != NULL_TREE)
9367 if (TREE_TYPE (tem) != type)
9368 tem = fold_convert_loc (loc, type, tem);
9373 /* If this is a commutative operation, and ARG0 is a constant, move it
9374 to ARG1 to reduce the number of tests below. */
9375 if (commutative_tree_code (code)
9376 && tree_swap_operands_p (arg0, arg1, true))
9377 return fold_build2_loc (loc, code, type, op1, op0);
9379 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9381 First check for cases where an arithmetic operation is applied to a
9382 compound, conditional, or comparison operation. Push the arithmetic
9383 operation inside the compound or conditional to see if any folding
9384 can then be done. Convert comparison to conditional for this purpose.
9385 The also optimizes non-constant cases that used to be done in
9388 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9389 one of the operands is a comparison and the other is a comparison, a
9390 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9391 code below would make the expression more complex. Change it to a
9392 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9393 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9395 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9396 || code == EQ_EXPR || code == NE_EXPR)
9397 && ((truth_value_p (TREE_CODE (arg0))
9398 && (truth_value_p (TREE_CODE (arg1))
9399 || (TREE_CODE (arg1) == BIT_AND_EXPR
9400 && integer_onep (TREE_OPERAND (arg1, 1)))))
9401 || (truth_value_p (TREE_CODE (arg1))
9402 && (truth_value_p (TREE_CODE (arg0))
9403 || (TREE_CODE (arg0) == BIT_AND_EXPR
9404 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9406 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9407 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9410 fold_convert_loc (loc, boolean_type_node, arg0),
9411 fold_convert_loc (loc, boolean_type_node, arg1));
9413 if (code == EQ_EXPR)
9414 tem = invert_truthvalue_loc (loc, tem);
9416 return fold_convert_loc (loc, type, tem);
9419 if (TREE_CODE_CLASS (code) == tcc_binary
9420 || TREE_CODE_CLASS (code) == tcc_comparison)
9422 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9424 tem = fold_build2_loc (loc, code, type,
9425 fold_convert_loc (loc, TREE_TYPE (op0),
9426 TREE_OPERAND (arg0, 1)), op1);
9427 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9428 goto fold_binary_exit;
9430 if (TREE_CODE (arg1) == COMPOUND_EXPR
9431 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9433 tem = fold_build2_loc (loc, code, type, op0,
9434 fold_convert_loc (loc, TREE_TYPE (op1),
9435 TREE_OPERAND (arg1, 1)));
9436 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9437 goto fold_binary_exit;
9440 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9442 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9444 /*cond_first_p=*/1);
9445 if (tem != NULL_TREE)
9449 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9451 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9453 /*cond_first_p=*/0);
9454 if (tem != NULL_TREE)
9461 case POINTER_PLUS_EXPR:
9462 /* 0 +p index -> (type)index */
9463 if (integer_zerop (arg0))
9464 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9466 /* PTR +p 0 -> PTR */
9467 if (integer_zerop (arg1))
9468 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9470 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9471 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9472 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9473 return fold_convert_loc (loc, type,
9474 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9475 fold_convert_loc (loc, sizetype,
9477 fold_convert_loc (loc, sizetype,
9480 /* index +p PTR -> PTR +p index */
9481 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9482 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9483 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9484 fold_convert_loc (loc, type, arg1),
9485 fold_convert_loc (loc, sizetype, arg0));
9487 /* (PTR +p B) +p A -> PTR +p (B + A) */
9488 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9491 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9492 tree arg00 = TREE_OPERAND (arg0, 0);
9493 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9494 arg01, fold_convert_loc (loc, sizetype, arg1));
9495 return fold_convert_loc (loc, type,
9496 fold_build2_loc (loc, POINTER_PLUS_EXPR,
9501 /* PTR_CST +p CST -> CST1 */
9502 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9503 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9504 fold_convert_loc (loc, type, arg1));
9506 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9507 of the array. Loop optimizer sometimes produce this type of
9509 if (TREE_CODE (arg0) == ADDR_EXPR)
9511 tem = try_move_mult_to_index (loc, arg0,
9512 fold_convert_loc (loc, sizetype, arg1));
9514 return fold_convert_loc (loc, type, tem);
9520 /* A + (-B) -> A - B */
9521 if (TREE_CODE (arg1) == NEGATE_EXPR)
9522 return fold_build2_loc (loc, MINUS_EXPR, type,
9523 fold_convert_loc (loc, type, arg0),
9524 fold_convert_loc (loc, type,
9525 TREE_OPERAND (arg1, 0)));
9526 /* (-A) + B -> B - A */
9527 if (TREE_CODE (arg0) == NEGATE_EXPR
9528 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9529 return fold_build2_loc (loc, MINUS_EXPR, type,
9530 fold_convert_loc (loc, type, arg1),
9531 fold_convert_loc (loc, type,
9532 TREE_OPERAND (arg0, 0)));
9534 if (INTEGRAL_TYPE_P (type))
9536 /* Convert ~A + 1 to -A. */
9537 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9538 && integer_onep (arg1))
9539 return fold_build1_loc (loc, NEGATE_EXPR, type,
9540 fold_convert_loc (loc, type,
9541 TREE_OPERAND (arg0, 0)));
9544 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9545 && !TYPE_OVERFLOW_TRAPS (type))
9547 tree tem = TREE_OPERAND (arg0, 0);
9550 if (operand_equal_p (tem, arg1, 0))
9552 t1 = build_int_cst_type (type, -1);
9553 return omit_one_operand_loc (loc, type, t1, arg1);
9558 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9559 && !TYPE_OVERFLOW_TRAPS (type))
9561 tree tem = TREE_OPERAND (arg1, 0);
9564 if (operand_equal_p (arg0, tem, 0))
9566 t1 = build_int_cst_type (type, -1);
9567 return omit_one_operand_loc (loc, type, t1, arg0);
9571 /* X + (X / CST) * -CST is X % CST. */
9572 if (TREE_CODE (arg1) == MULT_EXPR
9573 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9574 && operand_equal_p (arg0,
9575 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9577 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9578 tree cst1 = TREE_OPERAND (arg1, 1);
9579 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9581 if (sum && integer_zerop (sum))
9582 return fold_convert_loc (loc, type,
9583 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9584 TREE_TYPE (arg0), arg0,
9589 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9590 same or one. Make sure type is not saturating.
9591 fold_plusminus_mult_expr will re-associate. */
9592 if ((TREE_CODE (arg0) == MULT_EXPR
9593 || TREE_CODE (arg1) == MULT_EXPR)
9594 && !TYPE_SATURATING (type)
9595 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9597 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9602 if (! FLOAT_TYPE_P (type))
9604 if (integer_zerop (arg1))
9605 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9607 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9608 with a constant, and the two constants have no bits in common,
9609 we should treat this as a BIT_IOR_EXPR since this may produce more
9611 if (TREE_CODE (arg0) == BIT_AND_EXPR
9612 && TREE_CODE (arg1) == BIT_AND_EXPR
9613 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9614 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9615 && integer_zerop (const_binop (BIT_AND_EXPR,
9616 TREE_OPERAND (arg0, 1),
9617 TREE_OPERAND (arg1, 1), 0)))
9619 code = BIT_IOR_EXPR;
9623 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9624 (plus (plus (mult) (mult)) (foo)) so that we can
9625 take advantage of the factoring cases below. */
9626 if (((TREE_CODE (arg0) == PLUS_EXPR
9627 || TREE_CODE (arg0) == MINUS_EXPR)
9628 && TREE_CODE (arg1) == MULT_EXPR)
9629 || ((TREE_CODE (arg1) == PLUS_EXPR
9630 || TREE_CODE (arg1) == MINUS_EXPR)
9631 && TREE_CODE (arg0) == MULT_EXPR))
9633 tree parg0, parg1, parg, marg;
9634 enum tree_code pcode;
9636 if (TREE_CODE (arg1) == MULT_EXPR)
9637 parg = arg0, marg = arg1;
9639 parg = arg1, marg = arg0;
9640 pcode = TREE_CODE (parg);
9641 parg0 = TREE_OPERAND (parg, 0);
9642 parg1 = TREE_OPERAND (parg, 1);
9646 if (TREE_CODE (parg0) == MULT_EXPR
9647 && TREE_CODE (parg1) != MULT_EXPR)
9648 return fold_build2_loc (loc, pcode, type,
9649 fold_build2_loc (loc, PLUS_EXPR, type,
9650 fold_convert_loc (loc, type,
9652 fold_convert_loc (loc, type,
9654 fold_convert_loc (loc, type, parg1));
9655 if (TREE_CODE (parg0) != MULT_EXPR
9656 && TREE_CODE (parg1) == MULT_EXPR)
9658 fold_build2_loc (loc, PLUS_EXPR, type,
9659 fold_convert_loc (loc, type, parg0),
9660 fold_build2_loc (loc, pcode, type,
9661 fold_convert_loc (loc, type, marg),
9662 fold_convert_loc (loc, type,
9668 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9669 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9670 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9672 /* Likewise if the operands are reversed. */
9673 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9674 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9676 /* Convert X + -C into X - C. */
9677 if (TREE_CODE (arg1) == REAL_CST
9678 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9680 tem = fold_negate_const (arg1, type);
9681 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9682 return fold_build2_loc (loc, MINUS_EXPR, type,
9683 fold_convert_loc (loc, type, arg0),
9684 fold_convert_loc (loc, type, tem));
9687 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9688 to __complex__ ( x, y ). This is not the same for SNaNs or
9689 if signed zeros are involved. */
9690 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9691 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9692 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9694 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9695 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9696 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9697 bool arg0rz = false, arg0iz = false;
9698 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9699 || (arg0i && (arg0iz = real_zerop (arg0i))))
9701 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9702 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9703 if (arg0rz && arg1i && real_zerop (arg1i))
9705 tree rp = arg1r ? arg1r
9706 : build1 (REALPART_EXPR, rtype, arg1);
9707 tree ip = arg0i ? arg0i
9708 : build1 (IMAGPART_EXPR, rtype, arg0);
9709 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9711 else if (arg0iz && arg1r && real_zerop (arg1r))
9713 tree rp = arg0r ? arg0r
9714 : build1 (REALPART_EXPR, rtype, arg0);
9715 tree ip = arg1i ? arg1i
9716 : build1 (IMAGPART_EXPR, rtype, arg1);
9717 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9722 if (flag_unsafe_math_optimizations
9723 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9724 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9725 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9728 /* Convert x+x into x*2.0. */
9729 if (operand_equal_p (arg0, arg1, 0)
9730 && SCALAR_FLOAT_TYPE_P (type))
9731 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9732 build_real (type, dconst2));
9734 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9735 We associate floats only if the user has specified
9736 -fassociative-math. */
9737 if (flag_associative_math
9738 && TREE_CODE (arg1) == PLUS_EXPR
9739 && TREE_CODE (arg0) != MULT_EXPR)
9741 tree tree10 = TREE_OPERAND (arg1, 0);
9742 tree tree11 = TREE_OPERAND (arg1, 1);
9743 if (TREE_CODE (tree11) == MULT_EXPR
9744 && TREE_CODE (tree10) == MULT_EXPR)
9747 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9748 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9751 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9752 We associate floats only if the user has specified
9753 -fassociative-math. */
9754 if (flag_associative_math
9755 && TREE_CODE (arg0) == PLUS_EXPR
9756 && TREE_CODE (arg1) != MULT_EXPR)
9758 tree tree00 = TREE_OPERAND (arg0, 0);
9759 tree tree01 = TREE_OPERAND (arg0, 1);
9760 if (TREE_CODE (tree01) == MULT_EXPR
9761 && TREE_CODE (tree00) == MULT_EXPR)
9764 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9765 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9771 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9772 is a rotate of A by C1 bits. */
9773 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9774 is a rotate of A by B bits. */
9776 enum tree_code code0, code1;
9778 code0 = TREE_CODE (arg0);
9779 code1 = TREE_CODE (arg1);
9780 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9781 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9782 && operand_equal_p (TREE_OPERAND (arg0, 0),
9783 TREE_OPERAND (arg1, 0), 0)
9784 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9785 TYPE_UNSIGNED (rtype))
9786 /* Only create rotates in complete modes. Other cases are not
9787 expanded properly. */
9788 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9790 tree tree01, tree11;
9791 enum tree_code code01, code11;
9793 tree01 = TREE_OPERAND (arg0, 1);
9794 tree11 = TREE_OPERAND (arg1, 1);
9795 STRIP_NOPS (tree01);
9796 STRIP_NOPS (tree11);
9797 code01 = TREE_CODE (tree01);
9798 code11 = TREE_CODE (tree11);
9799 if (code01 == INTEGER_CST
9800 && code11 == INTEGER_CST
9801 && TREE_INT_CST_HIGH (tree01) == 0
9802 && TREE_INT_CST_HIGH (tree11) == 0
9803 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9804 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9806 tem = build2 (LROTATE_EXPR,
9807 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9808 TREE_OPERAND (arg0, 0),
9809 code0 == LSHIFT_EXPR
9811 SET_EXPR_LOCATION (tem, loc);
9812 return fold_convert_loc (loc, type, tem);
9814 else if (code11 == MINUS_EXPR)
9816 tree tree110, tree111;
9817 tree110 = TREE_OPERAND (tree11, 0);
9818 tree111 = TREE_OPERAND (tree11, 1);
9819 STRIP_NOPS (tree110);
9820 STRIP_NOPS (tree111);
9821 if (TREE_CODE (tree110) == INTEGER_CST
9822 && 0 == compare_tree_int (tree110,
9824 (TREE_TYPE (TREE_OPERAND
9826 && operand_equal_p (tree01, tree111, 0))
9828 fold_convert_loc (loc, type,
9829 build2 ((code0 == LSHIFT_EXPR
9832 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9833 TREE_OPERAND (arg0, 0), tree01));
9835 else if (code01 == MINUS_EXPR)
9837 tree tree010, tree011;
9838 tree010 = TREE_OPERAND (tree01, 0);
9839 tree011 = TREE_OPERAND (tree01, 1);
9840 STRIP_NOPS (tree010);
9841 STRIP_NOPS (tree011);
9842 if (TREE_CODE (tree010) == INTEGER_CST
9843 && 0 == compare_tree_int (tree010,
9845 (TREE_TYPE (TREE_OPERAND
9847 && operand_equal_p (tree11, tree011, 0))
9848 return fold_convert_loc
9850 build2 ((code0 != LSHIFT_EXPR
9853 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9854 TREE_OPERAND (arg0, 0), tree11));
9860 /* In most languages, can't associate operations on floats through
9861 parentheses. Rather than remember where the parentheses were, we
9862 don't associate floats at all, unless the user has specified
9864 And, we need to make sure type is not saturating. */
9866 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9867 && !TYPE_SATURATING (type))
9869 tree var0, con0, lit0, minus_lit0;
9870 tree var1, con1, lit1, minus_lit1;
9873 /* Split both trees into variables, constants, and literals. Then
9874 associate each group together, the constants with literals,
9875 then the result with variables. This increases the chances of
9876 literals being recombined later and of generating relocatable
9877 expressions for the sum of a constant and literal. */
9878 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9879 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9880 code == MINUS_EXPR);
9882 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9883 if (code == MINUS_EXPR)
9886 /* With undefined overflow we can only associate constants with one
9887 variable, and constants whose association doesn't overflow. */
9888 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9889 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9896 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9897 tmp0 = TREE_OPERAND (tmp0, 0);
9898 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9899 tmp1 = TREE_OPERAND (tmp1, 0);
9900 /* The only case we can still associate with two variables
9901 is if they are the same, modulo negation. */
9902 if (!operand_equal_p (tmp0, tmp1, 0))
9906 if (ok && lit0 && lit1)
9908 tree tmp0 = fold_convert (type, lit0);
9909 tree tmp1 = fold_convert (type, lit1);
9911 if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
9912 && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
9917 /* Only do something if we found more than two objects. Otherwise,
9918 nothing has changed and we risk infinite recursion. */
9920 && (2 < ((var0 != 0) + (var1 != 0)
9921 + (con0 != 0) + (con1 != 0)
9922 + (lit0 != 0) + (lit1 != 0)
9923 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9925 var0 = associate_trees (loc, var0, var1, code, type);
9926 con0 = associate_trees (loc, con0, con1, code, type);
9927 lit0 = associate_trees (loc, lit0, lit1, code, type);
9928 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
9930 /* Preserve the MINUS_EXPR if the negative part of the literal is
9931 greater than the positive part. Otherwise, the multiplicative
9932 folding code (i.e extract_muldiv) may be fooled in case
9933 unsigned constants are subtracted, like in the following
9934 example: ((X*2 + 4) - 8U)/2. */
9935 if (minus_lit0 && lit0)
9937 if (TREE_CODE (lit0) == INTEGER_CST
9938 && TREE_CODE (minus_lit0) == INTEGER_CST
9939 && tree_int_cst_lt (lit0, minus_lit0))
9941 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9947 lit0 = associate_trees (loc, lit0, minus_lit0,
9956 fold_convert_loc (loc, type,
9957 associate_trees (loc, var0, minus_lit0,
9961 con0 = associate_trees (loc, con0, minus_lit0,
9964 fold_convert_loc (loc, type,
9965 associate_trees (loc, var0, con0,
9970 con0 = associate_trees (loc, con0, lit0, code, type);
9972 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9980 /* Pointer simplifications for subtraction, simple reassociations. */
9981 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9983 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9984 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9985 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9987 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9988 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9989 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9990 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9991 return fold_build2_loc (loc, PLUS_EXPR, type,
9992 fold_build2_loc (loc, MINUS_EXPR, type,
9994 fold_build2_loc (loc, MINUS_EXPR, type,
9997 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9998 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10000 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10001 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10002 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10003 fold_convert_loc (loc, type, arg1));
10005 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10008 /* A - (-B) -> A + B */
10009 if (TREE_CODE (arg1) == NEGATE_EXPR)
10010 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10011 fold_convert_loc (loc, type,
10012 TREE_OPERAND (arg1, 0)));
10013 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10014 if (TREE_CODE (arg0) == NEGATE_EXPR
10015 && (FLOAT_TYPE_P (type)
10016 || INTEGRAL_TYPE_P (type))
10017 && negate_expr_p (arg1)
10018 && reorder_operands_p (arg0, arg1))
10019 return fold_build2_loc (loc, MINUS_EXPR, type,
10020 fold_convert_loc (loc, type,
10021 negate_expr (arg1)),
10022 fold_convert_loc (loc, type,
10023 TREE_OPERAND (arg0, 0)));
10024 /* Convert -A - 1 to ~A. */
10025 if (INTEGRAL_TYPE_P (type)
10026 && TREE_CODE (arg0) == NEGATE_EXPR
10027 && integer_onep (arg1)
10028 && !TYPE_OVERFLOW_TRAPS (type))
10029 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10030 fold_convert_loc (loc, type,
10031 TREE_OPERAND (arg0, 0)));
10033 /* Convert -1 - A to ~A. */
10034 if (INTEGRAL_TYPE_P (type)
10035 && integer_all_onesp (arg0))
10036 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10039 /* X - (X / CST) * CST is X % CST. */
10040 if (INTEGRAL_TYPE_P (type)
10041 && TREE_CODE (arg1) == MULT_EXPR
10042 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10043 && operand_equal_p (arg0,
10044 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10045 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10046 TREE_OPERAND (arg1, 1), 0))
10048 fold_convert_loc (loc, type,
10049 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10050 arg0, TREE_OPERAND (arg1, 1)));
10052 if (! FLOAT_TYPE_P (type))
10054 if (integer_zerop (arg0))
10055 return negate_expr (fold_convert_loc (loc, type, arg1));
10056 if (integer_zerop (arg1))
10057 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10059 /* Fold A - (A & B) into ~B & A. */
10060 if (!TREE_SIDE_EFFECTS (arg0)
10061 && TREE_CODE (arg1) == BIT_AND_EXPR)
10063 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10065 tree arg10 = fold_convert_loc (loc, type,
10066 TREE_OPERAND (arg1, 0));
10067 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10068 fold_build1_loc (loc, BIT_NOT_EXPR,
10070 fold_convert_loc (loc, type, arg0));
10072 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10074 tree arg11 = fold_convert_loc (loc,
10075 type, TREE_OPERAND (arg1, 1));
10076 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10077 fold_build1_loc (loc, BIT_NOT_EXPR,
10079 fold_convert_loc (loc, type, arg0));
10083 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10084 any power of 2 minus 1. */
10085 if (TREE_CODE (arg0) == BIT_AND_EXPR
10086 && TREE_CODE (arg1) == BIT_AND_EXPR
10087 && operand_equal_p (TREE_OPERAND (arg0, 0),
10088 TREE_OPERAND (arg1, 0), 0))
10090 tree mask0 = TREE_OPERAND (arg0, 1);
10091 tree mask1 = TREE_OPERAND (arg1, 1);
10092 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10094 if (operand_equal_p (tem, mask1, 0))
10096 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10097 TREE_OPERAND (arg0, 0), mask1);
10098 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10103 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10104 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10105 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10107 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10108 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10109 (-ARG1 + ARG0) reduces to -ARG1. */
10110 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10111 return negate_expr (fold_convert_loc (loc, type, arg1));
10113 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10114 __complex__ ( x, -y ). This is not the same for SNaNs or if
10115 signed zeros are involved. */
10116 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10117 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10118 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10120 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10121 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10122 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10123 bool arg0rz = false, arg0iz = false;
10124 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10125 || (arg0i && (arg0iz = real_zerop (arg0i))))
10127 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10128 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10129 if (arg0rz && arg1i && real_zerop (arg1i))
10131 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10133 : build1 (REALPART_EXPR, rtype, arg1));
10134 tree ip = arg0i ? arg0i
10135 : build1 (IMAGPART_EXPR, rtype, arg0);
10136 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10138 else if (arg0iz && arg1r && real_zerop (arg1r))
10140 tree rp = arg0r ? arg0r
10141 : build1 (REALPART_EXPR, rtype, arg0);
10142 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10144 : build1 (IMAGPART_EXPR, rtype, arg1));
10145 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10150 /* Fold &x - &x. This can happen from &x.foo - &x.
10151 This is unsafe for certain floats even in non-IEEE formats.
10152 In IEEE, it is unsafe because it does wrong for NaNs.
10153 Also note that operand_equal_p is always false if an operand
10156 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10157 && operand_equal_p (arg0, arg1, 0))
10158 return fold_convert_loc (loc, type, integer_zero_node);
10160 /* A - B -> A + (-B) if B is easily negatable. */
10161 if (negate_expr_p (arg1)
10162 && ((FLOAT_TYPE_P (type)
10163 /* Avoid this transformation if B is a positive REAL_CST. */
10164 && (TREE_CODE (arg1) != REAL_CST
10165 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10166 || INTEGRAL_TYPE_P (type)))
10167 return fold_build2_loc (loc, PLUS_EXPR, type,
10168 fold_convert_loc (loc, type, arg0),
10169 fold_convert_loc (loc, type,
10170 negate_expr (arg1)));
10172 /* Try folding difference of addresses. */
10174 HOST_WIDE_INT diff;
10176 if ((TREE_CODE (arg0) == ADDR_EXPR
10177 || TREE_CODE (arg1) == ADDR_EXPR)
10178 && ptr_difference_const (arg0, arg1, &diff))
10179 return build_int_cst_type (type, diff);
10182 /* Fold &a[i] - &a[j] to i-j. */
10183 if (TREE_CODE (arg0) == ADDR_EXPR
10184 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10185 && TREE_CODE (arg1) == ADDR_EXPR
10186 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10188 tree aref0 = TREE_OPERAND (arg0, 0);
10189 tree aref1 = TREE_OPERAND (arg1, 0);
10190 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10191 TREE_OPERAND (aref1, 0), 0))
10193 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10194 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10195 tree esz = array_ref_element_size (aref0);
10196 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10197 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10198 fold_convert_loc (loc, type, esz));
10203 if (FLOAT_TYPE_P (type)
10204 && flag_unsafe_math_optimizations
10205 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10206 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10207 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10210 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10211 same or one. Make sure type is not saturating.
10212 fold_plusminus_mult_expr will re-associate. */
10213 if ((TREE_CODE (arg0) == MULT_EXPR
10214 || TREE_CODE (arg1) == MULT_EXPR)
10215 && !TYPE_SATURATING (type)
10216 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10218 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10226 /* (-A) * (-B) -> A * B */
10227 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10228 return fold_build2_loc (loc, MULT_EXPR, type,
10229 fold_convert_loc (loc, type,
10230 TREE_OPERAND (arg0, 0)),
10231 fold_convert_loc (loc, type,
10232 negate_expr (arg1)));
10233 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10234 return fold_build2_loc (loc, MULT_EXPR, type,
10235 fold_convert_loc (loc, type,
10236 negate_expr (arg0)),
10237 fold_convert_loc (loc, type,
10238 TREE_OPERAND (arg1, 0)));
10240 if (! FLOAT_TYPE_P (type))
10242 if (integer_zerop (arg1))
10243 return omit_one_operand_loc (loc, type, arg1, arg0);
10244 if (integer_onep (arg1))
10245 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10246 /* Transform x * -1 into -x. Make sure to do the negation
10247 on the original operand with conversions not stripped
10248 because we can only strip non-sign-changing conversions. */
10249 if (integer_all_onesp (arg1))
10250 return fold_convert_loc (loc, type, negate_expr (op0));
10251 /* Transform x * -C into -x * C if x is easily negatable. */
10252 if (TREE_CODE (arg1) == INTEGER_CST
10253 && tree_int_cst_sgn (arg1) == -1
10254 && negate_expr_p (arg0)
10255 && (tem = negate_expr (arg1)) != arg1
10256 && !TREE_OVERFLOW (tem))
10257 return fold_build2_loc (loc, MULT_EXPR, type,
10258 fold_convert_loc (loc, type,
10259 negate_expr (arg0)),
10262 /* (a * (1 << b)) is (a << b) */
10263 if (TREE_CODE (arg1) == LSHIFT_EXPR
10264 && integer_onep (TREE_OPERAND (arg1, 0)))
10265 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10266 TREE_OPERAND (arg1, 1));
10267 if (TREE_CODE (arg0) == LSHIFT_EXPR
10268 && integer_onep (TREE_OPERAND (arg0, 0)))
10269 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10270 TREE_OPERAND (arg0, 1));
10272 /* (A + A) * C -> A * 2 * C */
10273 if (TREE_CODE (arg0) == PLUS_EXPR
10274 && TREE_CODE (arg1) == INTEGER_CST
10275 && operand_equal_p (TREE_OPERAND (arg0, 0),
10276 TREE_OPERAND (arg0, 1), 0))
10277 return fold_build2_loc (loc, MULT_EXPR, type,
10278 omit_one_operand_loc (loc, type,
10279 TREE_OPERAND (arg0, 0),
10280 TREE_OPERAND (arg0, 1)),
10281 fold_build2_loc (loc, MULT_EXPR, type,
10282 build_int_cst (type, 2) , arg1));
10284 strict_overflow_p = false;
10285 if (TREE_CODE (arg1) == INTEGER_CST
10286 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10287 &strict_overflow_p)))
10289 if (strict_overflow_p)
10290 fold_overflow_warning (("assuming signed overflow does not "
10291 "occur when simplifying "
10293 WARN_STRICT_OVERFLOW_MISC);
10294 return fold_convert_loc (loc, type, tem);
10297 /* Optimize z * conj(z) for integer complex numbers. */
10298 if (TREE_CODE (arg0) == CONJ_EXPR
10299 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10300 return fold_mult_zconjz (loc, type, arg1);
10301 if (TREE_CODE (arg1) == CONJ_EXPR
10302 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10303 return fold_mult_zconjz (loc, type, arg0);
10307 /* Maybe fold x * 0 to 0. The expressions aren't the same
10308 when x is NaN, since x * 0 is also NaN. Nor are they the
10309 same in modes with signed zeros, since multiplying a
10310 negative value by 0 gives -0, not +0. */
10311 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10312 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10313 && real_zerop (arg1))
10314 return omit_one_operand_loc (loc, type, arg1, arg0);
10315 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10316 Likewise for complex arithmetic with signed zeros. */
10317 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10318 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10319 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10320 && real_onep (arg1))
10321 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10323 /* Transform x * -1.0 into -x. */
10324 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10325 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10326 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10327 && real_minus_onep (arg1))
10328 return fold_convert_loc (loc, type, negate_expr (arg0));
10330 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10331 the result for floating point types due to rounding so it is applied
10332 only if -fassociative-math was specify. */
10333 if (flag_associative_math
10334 && TREE_CODE (arg0) == RDIV_EXPR
10335 && TREE_CODE (arg1) == REAL_CST
10336 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10338 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10341 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10342 TREE_OPERAND (arg0, 1));
10345 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10346 if (operand_equal_p (arg0, arg1, 0))
10348 tree tem = fold_strip_sign_ops (arg0);
10349 if (tem != NULL_TREE)
10351 tem = fold_convert_loc (loc, type, tem);
10352 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10356 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10357 This is not the same for NaNs or if signed zeros are
10359 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10360 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10361 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10362 && TREE_CODE (arg1) == COMPLEX_CST
10363 && real_zerop (TREE_REALPART (arg1)))
10365 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10366 if (real_onep (TREE_IMAGPART (arg1)))
10368 fold_build2_loc (loc, COMPLEX_EXPR, type,
10369 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10371 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10372 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10374 fold_build2_loc (loc, COMPLEX_EXPR, type,
10375 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10376 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10380 /* Optimize z * conj(z) for floating point complex numbers.
10381 Guarded by flag_unsafe_math_optimizations as non-finite
10382 imaginary components don't produce scalar results. */
10383 if (flag_unsafe_math_optimizations
10384 && TREE_CODE (arg0) == CONJ_EXPR
10385 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10386 return fold_mult_zconjz (loc, type, arg1);
10387 if (flag_unsafe_math_optimizations
10388 && TREE_CODE (arg1) == CONJ_EXPR
10389 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10390 return fold_mult_zconjz (loc, type, arg0);
10392 if (flag_unsafe_math_optimizations)
10394 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10395 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10397 /* Optimizations of root(...)*root(...). */
10398 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10401 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10402 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10404 /* Optimize sqrt(x)*sqrt(x) as x. */
10405 if (BUILTIN_SQRT_P (fcode0)
10406 && operand_equal_p (arg00, arg10, 0)
10407 && ! HONOR_SNANS (TYPE_MODE (type)))
10410 /* Optimize root(x)*root(y) as root(x*y). */
10411 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10412 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10413 return build_call_expr_loc (loc, rootfn, 1, arg);
10416 /* Optimize expN(x)*expN(y) as expN(x+y). */
10417 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10419 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10420 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10421 CALL_EXPR_ARG (arg0, 0),
10422 CALL_EXPR_ARG (arg1, 0));
10423 return build_call_expr_loc (loc, expfn, 1, arg);
10426 /* Optimizations of pow(...)*pow(...). */
10427 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10428 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10429 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10431 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10432 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10433 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10434 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10436 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10437 if (operand_equal_p (arg01, arg11, 0))
10439 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10440 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10442 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10445 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10446 if (operand_equal_p (arg00, arg10, 0))
10448 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10449 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10451 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10455 /* Optimize tan(x)*cos(x) as sin(x). */
10456 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10457 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10458 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10459 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10460 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10461 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10462 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10463 CALL_EXPR_ARG (arg1, 0), 0))
10465 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10467 if (sinfn != NULL_TREE)
10468 return build_call_expr_loc (loc, sinfn, 1,
10469 CALL_EXPR_ARG (arg0, 0));
10472 /* Optimize x*pow(x,c) as pow(x,c+1). */
10473 if (fcode1 == BUILT_IN_POW
10474 || fcode1 == BUILT_IN_POWF
10475 || fcode1 == BUILT_IN_POWL)
10477 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10478 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10479 if (TREE_CODE (arg11) == REAL_CST
10480 && !TREE_OVERFLOW (arg11)
10481 && operand_equal_p (arg0, arg10, 0))
10483 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10487 c = TREE_REAL_CST (arg11);
10488 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10489 arg = build_real (type, c);
10490 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10494 /* Optimize pow(x,c)*x as pow(x,c+1). */
10495 if (fcode0 == BUILT_IN_POW
10496 || fcode0 == BUILT_IN_POWF
10497 || fcode0 == BUILT_IN_POWL)
10499 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10500 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10501 if (TREE_CODE (arg01) == REAL_CST
10502 && !TREE_OVERFLOW (arg01)
10503 && operand_equal_p (arg1, arg00, 0))
10505 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10509 c = TREE_REAL_CST (arg01);
10510 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10511 arg = build_real (type, c);
10512 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10516 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10517 if (optimize_function_for_speed_p (cfun)
10518 && operand_equal_p (arg0, arg1, 0))
10520 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10524 tree arg = build_real (type, dconst2);
10525 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10534 if (integer_all_onesp (arg1))
10535 return omit_one_operand_loc (loc, type, arg1, arg0);
10536 if (integer_zerop (arg1))
10537 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10538 if (operand_equal_p (arg0, arg1, 0))
10539 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10541 /* ~X | X is -1. */
10542 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10543 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10545 t1 = fold_convert_loc (loc, type, integer_zero_node);
10546 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10547 return omit_one_operand_loc (loc, type, t1, arg1);
10550 /* X | ~X is -1. */
10551 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10552 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10554 t1 = fold_convert_loc (loc, type, integer_zero_node);
10555 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10556 return omit_one_operand_loc (loc, type, t1, arg0);
10559 /* Canonicalize (X & C1) | C2. */
10560 if (TREE_CODE (arg0) == BIT_AND_EXPR
10561 && TREE_CODE (arg1) == INTEGER_CST
10562 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10564 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10565 int width = TYPE_PRECISION (type), w;
10566 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10567 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10568 hi2 = TREE_INT_CST_HIGH (arg1);
10569 lo2 = TREE_INT_CST_LOW (arg1);
10571 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10572 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10573 return omit_one_operand_loc (loc, type, arg1,
10574 TREE_OPERAND (arg0, 0));
10576 if (width > HOST_BITS_PER_WIDE_INT)
10578 mhi = (unsigned HOST_WIDE_INT) -1
10579 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10585 mlo = (unsigned HOST_WIDE_INT) -1
10586 >> (HOST_BITS_PER_WIDE_INT - width);
10589 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10590 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10591 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10592 TREE_OPERAND (arg0, 0), arg1);
10594 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10595 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10596 mode which allows further optimizations. */
10603 for (w = BITS_PER_UNIT;
10604 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10607 unsigned HOST_WIDE_INT mask
10608 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10609 if (((lo1 | lo2) & mask) == mask
10610 && (lo1 & ~mask) == 0 && hi1 == 0)
10617 if (hi3 != hi1 || lo3 != lo1)
10618 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10619 fold_build2_loc (loc, BIT_AND_EXPR, type,
10620 TREE_OPERAND (arg0, 0),
10621 build_int_cst_wide (type,
10626 /* (X & Y) | Y is (X, Y). */
10627 if (TREE_CODE (arg0) == BIT_AND_EXPR
10628 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10629 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10630 /* (X & Y) | X is (Y, X). */
10631 if (TREE_CODE (arg0) == BIT_AND_EXPR
10632 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10633 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10634 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10635 /* X | (X & Y) is (Y, X). */
10636 if (TREE_CODE (arg1) == BIT_AND_EXPR
10637 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10638 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10639 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10640 /* X | (Y & X) is (Y, X). */
10641 if (TREE_CODE (arg1) == BIT_AND_EXPR
10642 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10643 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10644 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10646 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10647 if (t1 != NULL_TREE)
10650 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10652 This results in more efficient code for machines without a NAND
10653 instruction. Combine will canonicalize to the first form
10654 which will allow use of NAND instructions provided by the
10655 backend if they exist. */
10656 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10657 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10660 fold_build1_loc (loc, BIT_NOT_EXPR, type,
10661 build2 (BIT_AND_EXPR, type,
10662 fold_convert_loc (loc, type,
10663 TREE_OPERAND (arg0, 0)),
10664 fold_convert_loc (loc, type,
10665 TREE_OPERAND (arg1, 0))));
10668 /* See if this can be simplified into a rotate first. If that
10669 is unsuccessful continue in the association code. */
10673 if (integer_zerop (arg1))
10674 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10675 if (integer_all_onesp (arg1))
10676 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10677 if (operand_equal_p (arg0, arg1, 0))
10678 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10680 /* ~X ^ X is -1. */
10681 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10682 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10684 t1 = fold_convert_loc (loc, type, integer_zero_node);
10685 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10686 return omit_one_operand_loc (loc, type, t1, arg1);
10689 /* X ^ ~X is -1. */
10690 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10691 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10693 t1 = fold_convert_loc (loc, type, integer_zero_node);
10694 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10695 return omit_one_operand_loc (loc, type, t1, arg0);
10698 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10699 with a constant, and the two constants have no bits in common,
10700 we should treat this as a BIT_IOR_EXPR since this may produce more
10701 simplifications. */
10702 if (TREE_CODE (arg0) == BIT_AND_EXPR
10703 && TREE_CODE (arg1) == BIT_AND_EXPR
10704 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10705 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10706 && integer_zerop (const_binop (BIT_AND_EXPR,
10707 TREE_OPERAND (arg0, 1),
10708 TREE_OPERAND (arg1, 1), 0)))
10710 code = BIT_IOR_EXPR;
10714 /* (X | Y) ^ X -> Y & ~ X*/
10715 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10716 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10718 tree t2 = TREE_OPERAND (arg0, 1);
10719 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10721 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10722 fold_convert_loc (loc, type, t2),
10723 fold_convert_loc (loc, type, t1));
10727 /* (Y | X) ^ X -> Y & ~ X*/
10728 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10729 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10731 tree t2 = TREE_OPERAND (arg0, 0);
10732 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10734 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10735 fold_convert_loc (loc, type, t2),
10736 fold_convert_loc (loc, type, t1));
10740 /* X ^ (X | Y) -> Y & ~ X*/
10741 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10742 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10744 tree t2 = TREE_OPERAND (arg1, 1);
10745 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10747 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10748 fold_convert_loc (loc, type, t2),
10749 fold_convert_loc (loc, type, t1));
10753 /* X ^ (Y | X) -> Y & ~ X*/
10754 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10755 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10757 tree t2 = TREE_OPERAND (arg1, 0);
10758 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10760 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10761 fold_convert_loc (loc, type, t2),
10762 fold_convert_loc (loc, type, t1));
10766 /* Convert ~X ^ ~Y to X ^ Y. */
10767 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10768 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10769 return fold_build2_loc (loc, code, type,
10770 fold_convert_loc (loc, type,
10771 TREE_OPERAND (arg0, 0)),
10772 fold_convert_loc (loc, type,
10773 TREE_OPERAND (arg1, 0)));
10775 /* Convert ~X ^ C to X ^ ~C. */
10776 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10777 && TREE_CODE (arg1) == INTEGER_CST)
10778 return fold_build2_loc (loc, code, type,
10779 fold_convert_loc (loc, type,
10780 TREE_OPERAND (arg0, 0)),
10781 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10783 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10784 if (TREE_CODE (arg0) == BIT_AND_EXPR
10785 && integer_onep (TREE_OPERAND (arg0, 1))
10786 && integer_onep (arg1))
10787 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10788 build_int_cst (TREE_TYPE (arg0), 0));
10790 /* Fold (X & Y) ^ Y as ~X & Y. */
10791 if (TREE_CODE (arg0) == BIT_AND_EXPR
10792 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10794 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10795 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10796 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10797 fold_convert_loc (loc, type, arg1));
10799 /* Fold (X & Y) ^ X as ~Y & X. */
10800 if (TREE_CODE (arg0) == BIT_AND_EXPR
10801 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10802 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10804 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10805 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10806 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10807 fold_convert_loc (loc, type, arg1));
10809 /* Fold X ^ (X & Y) as X & ~Y. */
10810 if (TREE_CODE (arg1) == BIT_AND_EXPR
10811 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10813 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10814 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10815 fold_convert_loc (loc, type, arg0),
10816 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10818 /* Fold X ^ (Y & X) as ~Y & X. */
10819 if (TREE_CODE (arg1) == BIT_AND_EXPR
10820 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10821 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10823 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10824 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10825 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10826 fold_convert_loc (loc, type, arg0));
10829 /* See if this can be simplified into a rotate first. If that
10830 is unsuccessful continue in the association code. */
10834 if (integer_all_onesp (arg1))
10835 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10836 if (integer_zerop (arg1))
10837 return omit_one_operand_loc (loc, type, arg1, arg0);
10838 if (operand_equal_p (arg0, arg1, 0))
10839 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10841 /* ~X & X is always zero. */
10842 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10843 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10844 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10846 /* X & ~X is always zero. */
10847 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10848 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10849 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10851 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10852 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10853 && TREE_CODE (arg1) == INTEGER_CST
10854 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10856 tree tmp1 = fold_convert_loc (loc, type, arg1);
10857 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10858 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10859 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10860 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10862 fold_convert_loc (loc, type,
10863 fold_build2_loc (loc, BIT_IOR_EXPR,
10864 type, tmp2, tmp3));
10867 /* (X | Y) & Y is (X, Y). */
10868 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10869 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10870 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10871 /* (X | Y) & X is (Y, X). */
10872 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10873 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10874 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10875 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10876 /* X & (X | Y) is (Y, X). */
10877 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10878 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10879 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10880 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10881 /* X & (Y | X) is (Y, X). */
10882 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10883 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10884 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10885 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10887 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10888 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10889 && integer_onep (TREE_OPERAND (arg0, 1))
10890 && integer_onep (arg1))
10892 tem = TREE_OPERAND (arg0, 0);
10893 return fold_build2_loc (loc, EQ_EXPR, type,
10894 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10895 build_int_cst (TREE_TYPE (tem), 1)),
10896 build_int_cst (TREE_TYPE (tem), 0));
10898 /* Fold ~X & 1 as (X & 1) == 0. */
10899 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10900 && integer_onep (arg1))
10902 tem = TREE_OPERAND (arg0, 0);
10903 return fold_build2_loc (loc, EQ_EXPR, type,
10904 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10905 build_int_cst (TREE_TYPE (tem), 1)),
10906 build_int_cst (TREE_TYPE (tem), 0));
10909 /* Fold (X ^ Y) & Y as ~X & Y. */
10910 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10911 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10913 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10914 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10915 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10916 fold_convert_loc (loc, type, arg1));
10918 /* Fold (X ^ Y) & X as ~Y & X. */
10919 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10920 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10921 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10923 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10924 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10925 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10926 fold_convert_loc (loc, type, arg1));
10928 /* Fold X & (X ^ Y) as X & ~Y. */
10929 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10930 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10932 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10933 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10934 fold_convert_loc (loc, type, arg0),
10935 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10937 /* Fold X & (Y ^ X) as ~Y & X. */
10938 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10939 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10940 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10942 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10943 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10944 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10945 fold_convert_loc (loc, type, arg0));
10948 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10949 if (t1 != NULL_TREE)
10951 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10952 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10953 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10956 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10958 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10959 && (~TREE_INT_CST_LOW (arg1)
10960 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10962 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10965 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10967 This results in more efficient code for machines without a NOR
10968 instruction. Combine will canonicalize to the first form
10969 which will allow use of NOR instructions provided by the
10970 backend if they exist. */
10971 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10972 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10974 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10975 build2 (BIT_IOR_EXPR, type,
10976 fold_convert_loc (loc, type,
10977 TREE_OPERAND (arg0, 0)),
10978 fold_convert_loc (loc, type,
10979 TREE_OPERAND (arg1, 0))));
10982 /* If arg0 is derived from the address of an object or function, we may
10983 be able to fold this expression using the object or function's
10985 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10987 unsigned HOST_WIDE_INT modulus, residue;
10988 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10990 modulus = get_pointer_modulus_and_residue (arg0, &residue,
10991 integer_onep (arg1));
10993 /* This works because modulus is a power of 2. If this weren't the
10994 case, we'd have to replace it by its greatest power-of-2
10995 divisor: modulus & -modulus. */
10997 return build_int_cst (type, residue & low);
11000 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11001 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11002 if the new mask might be further optimized. */
11003 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11004 || TREE_CODE (arg0) == RSHIFT_EXPR)
11005 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11006 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11007 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11008 < TYPE_PRECISION (TREE_TYPE (arg0))
11009 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11010 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11012 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11013 unsigned HOST_WIDE_INT mask
11014 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11015 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11016 tree shift_type = TREE_TYPE (arg0);
11018 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11019 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11020 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11021 && TYPE_PRECISION (TREE_TYPE (arg0))
11022 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11024 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11025 tree arg00 = TREE_OPERAND (arg0, 0);
11026 /* See if more bits can be proven as zero because of
11028 if (TREE_CODE (arg00) == NOP_EXPR
11029 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11031 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11032 if (TYPE_PRECISION (inner_type)
11033 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11034 && TYPE_PRECISION (inner_type) < prec)
11036 prec = TYPE_PRECISION (inner_type);
11037 /* See if we can shorten the right shift. */
11039 shift_type = inner_type;
11042 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11043 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11044 zerobits <<= prec - shiftc;
11045 /* For arithmetic shift if sign bit could be set, zerobits
11046 can contain actually sign bits, so no transformation is
11047 possible, unless MASK masks them all away. In that
11048 case the shift needs to be converted into logical shift. */
11049 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11050 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11052 if ((mask & zerobits) == 0)
11053 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11059 /* ((X << 16) & 0xff00) is (X, 0). */
11060 if ((mask & zerobits) == mask)
11061 return omit_one_operand_loc (loc, type,
11062 build_int_cst (type, 0), arg0);
11064 newmask = mask | zerobits;
11065 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11069 /* Only do the transformation if NEWMASK is some integer
11071 for (prec = BITS_PER_UNIT;
11072 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11073 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11075 if (prec < HOST_BITS_PER_WIDE_INT
11076 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11080 if (shift_type != TREE_TYPE (arg0))
11082 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11083 fold_convert_loc (loc, shift_type,
11084 TREE_OPERAND (arg0, 0)),
11085 TREE_OPERAND (arg0, 1));
11086 tem = fold_convert_loc (loc, type, tem);
11090 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11091 if (!tree_int_cst_equal (newmaskt, arg1))
11092 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11100 /* Don't touch a floating-point divide by zero unless the mode
11101 of the constant can represent infinity. */
11102 if (TREE_CODE (arg1) == REAL_CST
11103 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11104 && real_zerop (arg1))
11107 /* Optimize A / A to 1.0 if we don't care about
11108 NaNs or Infinities. Skip the transformation
11109 for non-real operands. */
11110 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11111 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11112 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11113 && operand_equal_p (arg0, arg1, 0))
11115 tree r = build_real (TREE_TYPE (arg0), dconst1);
11117 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11120 /* The complex version of the above A / A optimization. */
11121 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11122 && operand_equal_p (arg0, arg1, 0))
11124 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11125 if (! HONOR_NANS (TYPE_MODE (elem_type))
11126 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11128 tree r = build_real (elem_type, dconst1);
11129 /* omit_two_operands will call fold_convert for us. */
11130 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11134 /* (-A) / (-B) -> A / B */
11135 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11136 return fold_build2_loc (loc, RDIV_EXPR, type,
11137 TREE_OPERAND (arg0, 0),
11138 negate_expr (arg1));
11139 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11140 return fold_build2_loc (loc, RDIV_EXPR, type,
11141 negate_expr (arg0),
11142 TREE_OPERAND (arg1, 0));
11144 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11145 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11146 && real_onep (arg1))
11147 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11149 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11150 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11151 && real_minus_onep (arg1))
11152 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11153 negate_expr (arg0)));
11155 /* If ARG1 is a constant, we can convert this to a multiply by the
11156 reciprocal. This does not have the same rounding properties,
11157 so only do this if -freciprocal-math. We can actually
11158 always safely do it if ARG1 is a power of two, but it's hard to
11159 tell if it is or not in a portable manner. */
11160 if (TREE_CODE (arg1) == REAL_CST)
11162 if (flag_reciprocal_math
11163 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11165 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11166 /* Find the reciprocal if optimizing and the result is exact. */
11170 r = TREE_REAL_CST (arg1);
11171 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11173 tem = build_real (type, r);
11174 return fold_build2_loc (loc, MULT_EXPR, type,
11175 fold_convert_loc (loc, type, arg0), tem);
11179 /* Convert A/B/C to A/(B*C). */
11180 if (flag_reciprocal_math
11181 && TREE_CODE (arg0) == RDIV_EXPR)
11182 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11183 fold_build2_loc (loc, MULT_EXPR, type,
11184 TREE_OPERAND (arg0, 1), arg1));
11186 /* Convert A/(B/C) to (A/B)*C. */
11187 if (flag_reciprocal_math
11188 && TREE_CODE (arg1) == RDIV_EXPR)
11189 return fold_build2_loc (loc, MULT_EXPR, type,
11190 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11191 TREE_OPERAND (arg1, 0)),
11192 TREE_OPERAND (arg1, 1));
11194 /* Convert C1/(X*C2) into (C1/C2)/X. */
11195 if (flag_reciprocal_math
11196 && TREE_CODE (arg1) == MULT_EXPR
11197 && TREE_CODE (arg0) == REAL_CST
11198 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11200 tree tem = const_binop (RDIV_EXPR, arg0,
11201 TREE_OPERAND (arg1, 1), 0);
11203 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11204 TREE_OPERAND (arg1, 0));
11207 if (flag_unsafe_math_optimizations)
11209 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11210 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11212 /* Optimize sin(x)/cos(x) as tan(x). */
11213 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11214 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11215 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11216 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11217 CALL_EXPR_ARG (arg1, 0), 0))
11219 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11221 if (tanfn != NULL_TREE)
11222 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11225 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11226 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11227 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11228 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11229 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11230 CALL_EXPR_ARG (arg1, 0), 0))
11232 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11234 if (tanfn != NULL_TREE)
11236 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11237 CALL_EXPR_ARG (arg0, 0));
11238 return fold_build2_loc (loc, RDIV_EXPR, type,
11239 build_real (type, dconst1), tmp);
11243 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11244 NaNs or Infinities. */
11245 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11246 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11247 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11249 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11250 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11252 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11253 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11254 && operand_equal_p (arg00, arg01, 0))
11256 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11258 if (cosfn != NULL_TREE)
11259 return build_call_expr_loc (loc, cosfn, 1, arg00);
11263 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11264 NaNs or Infinities. */
11265 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11266 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11267 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11269 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11270 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11272 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11273 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11274 && operand_equal_p (arg00, arg01, 0))
11276 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11278 if (cosfn != NULL_TREE)
11280 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11281 return fold_build2_loc (loc, RDIV_EXPR, type,
11282 build_real (type, dconst1),
11288 /* Optimize pow(x,c)/x as pow(x,c-1). */
11289 if (fcode0 == BUILT_IN_POW
11290 || fcode0 == BUILT_IN_POWF
11291 || fcode0 == BUILT_IN_POWL)
11293 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11294 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11295 if (TREE_CODE (arg01) == REAL_CST
11296 && !TREE_OVERFLOW (arg01)
11297 && operand_equal_p (arg1, arg00, 0))
11299 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11303 c = TREE_REAL_CST (arg01);
11304 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11305 arg = build_real (type, c);
11306 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11310 /* Optimize a/root(b/c) into a*root(c/b). */
11311 if (BUILTIN_ROOT_P (fcode1))
11313 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11315 if (TREE_CODE (rootarg) == RDIV_EXPR)
11317 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11318 tree b = TREE_OPERAND (rootarg, 0);
11319 tree c = TREE_OPERAND (rootarg, 1);
11321 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11323 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11324 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11328 /* Optimize x/expN(y) into x*expN(-y). */
11329 if (BUILTIN_EXPONENT_P (fcode1))
11331 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11332 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11333 arg1 = build_call_expr_loc (loc,
11335 fold_convert_loc (loc, type, arg));
11336 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11339 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11340 if (fcode1 == BUILT_IN_POW
11341 || fcode1 == BUILT_IN_POWF
11342 || fcode1 == BUILT_IN_POWL)
11344 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11345 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11346 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11347 tree neg11 = fold_convert_loc (loc, type,
11348 negate_expr (arg11));
11349 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11350 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11355 case TRUNC_DIV_EXPR:
11356 case FLOOR_DIV_EXPR:
11357 /* Simplify A / (B << N) where A and B are positive and B is
11358 a power of 2, to A >> (N + log2(B)). */
11359 strict_overflow_p = false;
11360 if (TREE_CODE (arg1) == LSHIFT_EXPR
11361 && (TYPE_UNSIGNED (type)
11362 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11364 tree sval = TREE_OPERAND (arg1, 0);
11365 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11367 tree sh_cnt = TREE_OPERAND (arg1, 1);
11368 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11370 if (strict_overflow_p)
11371 fold_overflow_warning (("assuming signed overflow does not "
11372 "occur when simplifying A / (B << N)"),
11373 WARN_STRICT_OVERFLOW_MISC);
11375 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11376 sh_cnt, build_int_cst (NULL_TREE, pow2));
11377 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11378 fold_convert_loc (loc, type, arg0), sh_cnt);
11382 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11383 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11384 if (INTEGRAL_TYPE_P (type)
11385 && TYPE_UNSIGNED (type)
11386 && code == FLOOR_DIV_EXPR)
11387 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11391 case ROUND_DIV_EXPR:
11392 case CEIL_DIV_EXPR:
11393 case EXACT_DIV_EXPR:
11394 if (integer_onep (arg1))
11395 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11396 if (integer_zerop (arg1))
11398 /* X / -1 is -X. */
11399 if (!TYPE_UNSIGNED (type)
11400 && TREE_CODE (arg1) == INTEGER_CST
11401 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11402 && TREE_INT_CST_HIGH (arg1) == -1)
11403 return fold_convert_loc (loc, type, negate_expr (arg0));
11405 /* Convert -A / -B to A / B when the type is signed and overflow is
11407 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11408 && TREE_CODE (arg0) == NEGATE_EXPR
11409 && negate_expr_p (arg1))
11411 if (INTEGRAL_TYPE_P (type))
11412 fold_overflow_warning (("assuming signed overflow does not occur "
11413 "when distributing negation across "
11415 WARN_STRICT_OVERFLOW_MISC);
11416 return fold_build2_loc (loc, code, type,
11417 fold_convert_loc (loc, type,
11418 TREE_OPERAND (arg0, 0)),
11419 fold_convert_loc (loc, type,
11420 negate_expr (arg1)));
11422 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11423 && TREE_CODE (arg1) == NEGATE_EXPR
11424 && negate_expr_p (arg0))
11426 if (INTEGRAL_TYPE_P (type))
11427 fold_overflow_warning (("assuming signed overflow does not occur "
11428 "when distributing negation across "
11430 WARN_STRICT_OVERFLOW_MISC);
11431 return fold_build2_loc (loc, code, type,
11432 fold_convert_loc (loc, type,
11433 negate_expr (arg0)),
11434 fold_convert_loc (loc, type,
11435 TREE_OPERAND (arg1, 0)));
11438 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11439 operation, EXACT_DIV_EXPR.
11441 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11442 At one time others generated faster code, it's not clear if they do
11443 after the last round to changes to the DIV code in expmed.c. */
11444 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11445 && multiple_of_p (type, arg0, arg1))
11446 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11448 strict_overflow_p = false;
11449 if (TREE_CODE (arg1) == INTEGER_CST
11450 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11451 &strict_overflow_p)))
11453 if (strict_overflow_p)
11454 fold_overflow_warning (("assuming signed overflow does not occur "
11455 "when simplifying division"),
11456 WARN_STRICT_OVERFLOW_MISC);
11457 return fold_convert_loc (loc, type, tem);
11462 case CEIL_MOD_EXPR:
11463 case FLOOR_MOD_EXPR:
11464 case ROUND_MOD_EXPR:
11465 case TRUNC_MOD_EXPR:
11466 /* X % 1 is always zero, but be sure to preserve any side
11468 if (integer_onep (arg1))
11469 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11471 /* X % 0, return X % 0 unchanged so that we can get the
11472 proper warnings and errors. */
11473 if (integer_zerop (arg1))
11476 /* 0 % X is always zero, but be sure to preserve any side
11477 effects in X. Place this after checking for X == 0. */
11478 if (integer_zerop (arg0))
11479 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11481 /* X % -1 is zero. */
11482 if (!TYPE_UNSIGNED (type)
11483 && TREE_CODE (arg1) == INTEGER_CST
11484 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11485 && TREE_INT_CST_HIGH (arg1) == -1)
11486 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11488 /* X % -C is the same as X % C. */
11489 if (code == TRUNC_MOD_EXPR
11490 && !TYPE_UNSIGNED (type)
11491 && TREE_CODE (arg1) == INTEGER_CST
11492 && !TREE_OVERFLOW (arg1)
11493 && TREE_INT_CST_HIGH (arg1) < 0
11494 && !TYPE_OVERFLOW_TRAPS (type)
11495 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11496 && !sign_bit_p (arg1, arg1))
11497 return fold_build2_loc (loc, code, type,
11498 fold_convert_loc (loc, type, arg0),
11499 fold_convert_loc (loc, type,
11500 negate_expr (arg1)));
11502 /* X % -Y is the same as X % Y. */
11503 if (code == TRUNC_MOD_EXPR
11504 && !TYPE_UNSIGNED (type)
11505 && TREE_CODE (arg1) == NEGATE_EXPR
11506 && !TYPE_OVERFLOW_TRAPS (type))
11507 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11508 fold_convert_loc (loc, type,
11509 TREE_OPERAND (arg1, 0)));
11511 strict_overflow_p = false;
11512 if (TREE_CODE (arg1) == INTEGER_CST
11513 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11514 &strict_overflow_p)))
11516 if (strict_overflow_p)
11517 fold_overflow_warning (("assuming signed overflow does not occur "
11518 "when simplifying modulus"),
11519 WARN_STRICT_OVERFLOW_MISC);
11520 return fold_convert_loc (loc, type, tem);
11523 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11524 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11525 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11526 && (TYPE_UNSIGNED (type)
11527 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11530 /* Also optimize A % (C << N) where C is a power of 2,
11531 to A & ((C << N) - 1). */
11532 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11533 c = TREE_OPERAND (arg1, 0);
11535 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11538 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11539 build_int_cst (TREE_TYPE (arg1), 1));
11540 if (strict_overflow_p)
11541 fold_overflow_warning (("assuming signed overflow does not "
11542 "occur when simplifying "
11543 "X % (power of two)"),
11544 WARN_STRICT_OVERFLOW_MISC);
11545 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11546 fold_convert_loc (loc, type, arg0),
11547 fold_convert_loc (loc, type, mask));
11555 if (integer_all_onesp (arg0))
11556 return omit_one_operand_loc (loc, type, arg0, arg1);
11560 /* Optimize -1 >> x for arithmetic right shifts. */
11561 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11562 && tree_expr_nonnegative_p (arg1))
11563 return omit_one_operand_loc (loc, type, arg0, arg1);
11564 /* ... fall through ... */
11568 if (integer_zerop (arg1))
11569 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11570 if (integer_zerop (arg0))
11571 return omit_one_operand_loc (loc, type, arg0, arg1);
11573 /* Since negative shift count is not well-defined,
11574 don't try to compute it in the compiler. */
11575 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11578 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11579 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11580 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11581 && host_integerp (TREE_OPERAND (arg0, 1), false)
11582 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11584 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11585 + TREE_INT_CST_LOW (arg1));
11587 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11588 being well defined. */
11589 if (low >= TYPE_PRECISION (type))
11591 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11592 low = low % TYPE_PRECISION (type);
11593 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11594 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11595 TREE_OPERAND (arg0, 0));
11597 low = TYPE_PRECISION (type) - 1;
11600 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11601 build_int_cst (type, low));
11604 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11605 into x & ((unsigned)-1 >> c) for unsigned types. */
11606 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11607 || (TYPE_UNSIGNED (type)
11608 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11609 && host_integerp (arg1, false)
11610 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11611 && host_integerp (TREE_OPERAND (arg0, 1), false)
11612 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11614 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11615 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11621 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11623 lshift = build_int_cst (type, -1);
11624 lshift = int_const_binop (code, lshift, arg1, 0);
11626 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11630 /* Rewrite an LROTATE_EXPR by a constant into an
11631 RROTATE_EXPR by a new constant. */
11632 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11634 tree tem = build_int_cst (TREE_TYPE (arg1),
11635 TYPE_PRECISION (type));
11636 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11637 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11640 /* If we have a rotate of a bit operation with the rotate count and
11641 the second operand of the bit operation both constant,
11642 permute the two operations. */
11643 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11644 && (TREE_CODE (arg0) == BIT_AND_EXPR
11645 || TREE_CODE (arg0) == BIT_IOR_EXPR
11646 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11647 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11648 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11649 fold_build2_loc (loc, code, type,
11650 TREE_OPERAND (arg0, 0), arg1),
11651 fold_build2_loc (loc, code, type,
11652 TREE_OPERAND (arg0, 1), arg1));
11654 /* Two consecutive rotates adding up to the precision of the
11655 type can be ignored. */
11656 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11657 && TREE_CODE (arg0) == RROTATE_EXPR
11658 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11659 && TREE_INT_CST_HIGH (arg1) == 0
11660 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11661 && ((TREE_INT_CST_LOW (arg1)
11662 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11663 == (unsigned int) TYPE_PRECISION (type)))
11664 return TREE_OPERAND (arg0, 0);
11666 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11667 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11668 if the latter can be further optimized. */
11669 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11670 && TREE_CODE (arg0) == BIT_AND_EXPR
11671 && TREE_CODE (arg1) == INTEGER_CST
11672 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11674 tree mask = fold_build2_loc (loc, code, type,
11675 fold_convert_loc (loc, type,
11676 TREE_OPERAND (arg0, 1)),
11678 tree shift = fold_build2_loc (loc, code, type,
11679 fold_convert_loc (loc, type,
11680 TREE_OPERAND (arg0, 0)),
11682 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11690 if (operand_equal_p (arg0, arg1, 0))
11691 return omit_one_operand_loc (loc, type, arg0, arg1);
11692 if (INTEGRAL_TYPE_P (type)
11693 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11694 return omit_one_operand_loc (loc, type, arg1, arg0);
11695 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11701 if (operand_equal_p (arg0, arg1, 0))
11702 return omit_one_operand_loc (loc, type, arg0, arg1);
11703 if (INTEGRAL_TYPE_P (type)
11704 && TYPE_MAX_VALUE (type)
11705 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11706 return omit_one_operand_loc (loc, type, arg1, arg0);
11707 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11712 case TRUTH_ANDIF_EXPR:
11713 /* Note that the operands of this must be ints
11714 and their values must be 0 or 1.
11715 ("true" is a fixed value perhaps depending on the language.) */
11716 /* If first arg is constant zero, return it. */
11717 if (integer_zerop (arg0))
11718 return fold_convert_loc (loc, type, arg0);
11719 case TRUTH_AND_EXPR:
11720 /* If either arg is constant true, drop it. */
11721 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11722 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11723 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11724 /* Preserve sequence points. */
11725 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11726 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11727 /* If second arg is constant zero, result is zero, but first arg
11728 must be evaluated. */
11729 if (integer_zerop (arg1))
11730 return omit_one_operand_loc (loc, type, arg1, arg0);
11731 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11732 case will be handled here. */
11733 if (integer_zerop (arg0))
11734 return omit_one_operand_loc (loc, type, arg0, arg1);
11736 /* !X && X is always false. */
11737 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11738 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11739 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11740 /* X && !X is always false. */
11741 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11742 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11743 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11745 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11746 means A >= Y && A != MAX, but in this case we know that
11749 if (!TREE_SIDE_EFFECTS (arg0)
11750 && !TREE_SIDE_EFFECTS (arg1))
11752 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11753 if (tem && !operand_equal_p (tem, arg0, 0))
11754 return fold_build2_loc (loc, code, type, tem, arg1);
11756 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11757 if (tem && !operand_equal_p (tem, arg1, 0))
11758 return fold_build2_loc (loc, code, type, arg0, tem);
11762 /* We only do these simplifications if we are optimizing. */
11766 /* Check for things like (A || B) && (A || C). We can convert this
11767 to A || (B && C). Note that either operator can be any of the four
11768 truth and/or operations and the transformation will still be
11769 valid. Also note that we only care about order for the
11770 ANDIF and ORIF operators. If B contains side effects, this
11771 might change the truth-value of A. */
11772 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11773 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11774 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11775 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11776 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11777 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11779 tree a00 = TREE_OPERAND (arg0, 0);
11780 tree a01 = TREE_OPERAND (arg0, 1);
11781 tree a10 = TREE_OPERAND (arg1, 0);
11782 tree a11 = TREE_OPERAND (arg1, 1);
11783 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11784 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11785 && (code == TRUTH_AND_EXPR
11786 || code == TRUTH_OR_EXPR));
11788 if (operand_equal_p (a00, a10, 0))
11789 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11790 fold_build2_loc (loc, code, type, a01, a11));
11791 else if (commutative && operand_equal_p (a00, a11, 0))
11792 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11793 fold_build2_loc (loc, code, type, a01, a10));
11794 else if (commutative && operand_equal_p (a01, a10, 0))
11795 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
11796 fold_build2_loc (loc, code, type, a00, a11));
11798 /* This case if tricky because we must either have commutative
11799 operators or else A10 must not have side-effects. */
11801 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11802 && operand_equal_p (a01, a11, 0))
11803 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11804 fold_build2_loc (loc, code, type, a00, a10),
11808 /* See if we can build a range comparison. */
11809 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
11812 /* Check for the possibility of merging component references. If our
11813 lhs is another similar operation, try to merge its rhs with our
11814 rhs. Then try to merge our lhs and rhs. */
11815 if (TREE_CODE (arg0) == code
11816 && 0 != (tem = fold_truthop (loc, code, type,
11817 TREE_OPERAND (arg0, 1), arg1)))
11818 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11820 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
11825 case TRUTH_ORIF_EXPR:
11826 /* Note that the operands of this must be ints
11827 and their values must be 0 or true.
11828 ("true" is a fixed value perhaps depending on the language.) */
11829 /* If first arg is constant true, return it. */
11830 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11831 return fold_convert_loc (loc, type, arg0);
11832 case TRUTH_OR_EXPR:
11833 /* If either arg is constant zero, drop it. */
11834 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11835 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11836 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11837 /* Preserve sequence points. */
11838 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11839 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11840 /* If second arg is constant true, result is true, but we must
11841 evaluate first arg. */
11842 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11843 return omit_one_operand_loc (loc, type, arg1, arg0);
11844 /* Likewise for first arg, but note this only occurs here for
11846 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11847 return omit_one_operand_loc (loc, type, arg0, arg1);
11849 /* !X || X is always true. */
11850 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11851 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11852 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11853 /* X || !X is always true. */
11854 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11855 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11856 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11860 case TRUTH_XOR_EXPR:
11861 /* If the second arg is constant zero, drop it. */
11862 if (integer_zerop (arg1))
11863 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11864 /* If the second arg is constant true, this is a logical inversion. */
11865 if (integer_onep (arg1))
11867 /* Only call invert_truthvalue if operand is a truth value. */
11868 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11869 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11871 tem = invert_truthvalue_loc (loc, arg0);
11872 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11874 /* Identical arguments cancel to zero. */
11875 if (operand_equal_p (arg0, arg1, 0))
11876 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11878 /* !X ^ X is always true. */
11879 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11880 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11881 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11883 /* X ^ !X is always true. */
11884 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11885 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11886 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11892 tem = fold_comparison (loc, code, type, op0, op1);
11893 if (tem != NULL_TREE)
11896 /* bool_var != 0 becomes bool_var. */
11897 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11898 && code == NE_EXPR)
11899 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11901 /* bool_var == 1 becomes bool_var. */
11902 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11903 && code == EQ_EXPR)
11904 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11906 /* bool_var != 1 becomes !bool_var. */
11907 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11908 && code == NE_EXPR)
11909 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
11910 fold_convert_loc (loc, type, arg0));
11912 /* bool_var == 0 becomes !bool_var. */
11913 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11914 && code == EQ_EXPR)
11915 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
11916 fold_convert_loc (loc, type, arg0));
11918 /* !exp != 0 becomes !exp */
11919 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
11920 && code == NE_EXPR)
11921 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11923 /* If this is an equality comparison of the address of two non-weak,
11924 unaliased symbols neither of which are extern (since we do not
11925 have access to attributes for externs), then we know the result. */
11926 if (TREE_CODE (arg0) == ADDR_EXPR
11927 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11928 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11929 && ! lookup_attribute ("alias",
11930 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11931 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11932 && TREE_CODE (arg1) == ADDR_EXPR
11933 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11934 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11935 && ! lookup_attribute ("alias",
11936 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11937 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11939 /* We know that we're looking at the address of two
11940 non-weak, unaliased, static _DECL nodes.
11942 It is both wasteful and incorrect to call operand_equal_p
11943 to compare the two ADDR_EXPR nodes. It is wasteful in that
11944 all we need to do is test pointer equality for the arguments
11945 to the two ADDR_EXPR nodes. It is incorrect to use
11946 operand_equal_p as that function is NOT equivalent to a
11947 C equality test. It can in fact return false for two
11948 objects which would test as equal using the C equality
11950 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11951 return constant_boolean_node (equal
11952 ? code == EQ_EXPR : code != EQ_EXPR,
11956 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11957 a MINUS_EXPR of a constant, we can convert it into a comparison with
11958 a revised constant as long as no overflow occurs. */
11959 if (TREE_CODE (arg1) == INTEGER_CST
11960 && (TREE_CODE (arg0) == PLUS_EXPR
11961 || TREE_CODE (arg0) == MINUS_EXPR)
11962 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11963 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11964 ? MINUS_EXPR : PLUS_EXPR,
11965 fold_convert_loc (loc, TREE_TYPE (arg0),
11967 TREE_OPERAND (arg0, 1), 0))
11968 && !TREE_OVERFLOW (tem))
11969 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11971 /* Similarly for a NEGATE_EXPR. */
11972 if (TREE_CODE (arg0) == NEGATE_EXPR
11973 && TREE_CODE (arg1) == INTEGER_CST
11974 && 0 != (tem = negate_expr (arg1))
11975 && TREE_CODE (tem) == INTEGER_CST
11976 && !TREE_OVERFLOW (tem))
11977 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11979 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11980 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11981 && TREE_CODE (arg1) == INTEGER_CST
11982 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11983 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11984 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
11985 fold_convert_loc (loc,
11988 TREE_OPERAND (arg0, 1)));
11990 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
11991 if ((TREE_CODE (arg0) == PLUS_EXPR
11992 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
11993 || TREE_CODE (arg0) == MINUS_EXPR)
11994 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11995 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11996 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11998 tree val = TREE_OPERAND (arg0, 1);
11999 return omit_two_operands_loc (loc, type,
12000 fold_build2_loc (loc, code, type,
12002 build_int_cst (TREE_TYPE (val),
12004 TREE_OPERAND (arg0, 0), arg1);
12007 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12008 if (TREE_CODE (arg0) == MINUS_EXPR
12009 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12010 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12011 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12013 return omit_two_operands_loc (loc, type,
12015 ? boolean_true_node : boolean_false_node,
12016 TREE_OPERAND (arg0, 1), arg1);
12019 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12020 for !=. Don't do this for ordered comparisons due to overflow. */
12021 if (TREE_CODE (arg0) == MINUS_EXPR
12022 && integer_zerop (arg1))
12023 return fold_build2_loc (loc, code, type,
12024 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12026 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12027 if (TREE_CODE (arg0) == ABS_EXPR
12028 && (integer_zerop (arg1) || real_zerop (arg1)))
12029 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12031 /* If this is an EQ or NE comparison with zero and ARG0 is
12032 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12033 two operations, but the latter can be done in one less insn
12034 on machines that have only two-operand insns or on which a
12035 constant cannot be the first operand. */
12036 if (TREE_CODE (arg0) == BIT_AND_EXPR
12037 && integer_zerop (arg1))
12039 tree arg00 = TREE_OPERAND (arg0, 0);
12040 tree arg01 = TREE_OPERAND (arg0, 1);
12041 if (TREE_CODE (arg00) == LSHIFT_EXPR
12042 && integer_onep (TREE_OPERAND (arg00, 0)))
12044 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12045 arg01, TREE_OPERAND (arg00, 1));
12046 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12047 build_int_cst (TREE_TYPE (arg0), 1));
12048 return fold_build2_loc (loc, code, type,
12049 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12052 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12053 && integer_onep (TREE_OPERAND (arg01, 0)))
12055 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12056 arg00, TREE_OPERAND (arg01, 1));
12057 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12058 build_int_cst (TREE_TYPE (arg0), 1));
12059 return fold_build2_loc (loc, code, type,
12060 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12065 /* If this is an NE or EQ comparison of zero against the result of a
12066 signed MOD operation whose second operand is a power of 2, make
12067 the MOD operation unsigned since it is simpler and equivalent. */
12068 if (integer_zerop (arg1)
12069 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12070 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12071 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12072 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12073 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12074 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12076 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12077 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12078 fold_convert_loc (loc, newtype,
12079 TREE_OPERAND (arg0, 0)),
12080 fold_convert_loc (loc, newtype,
12081 TREE_OPERAND (arg0, 1)));
12083 return fold_build2_loc (loc, code, type, newmod,
12084 fold_convert_loc (loc, newtype, arg1));
12087 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12088 C1 is a valid shift constant, and C2 is a power of two, i.e.
12090 if (TREE_CODE (arg0) == BIT_AND_EXPR
12091 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12092 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12094 && integer_pow2p (TREE_OPERAND (arg0, 1))
12095 && integer_zerop (arg1))
12097 tree itype = TREE_TYPE (arg0);
12098 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12099 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12101 /* Check for a valid shift count. */
12102 if (TREE_INT_CST_HIGH (arg001) == 0
12103 && TREE_INT_CST_LOW (arg001) < prec)
12105 tree arg01 = TREE_OPERAND (arg0, 1);
12106 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12107 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12108 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12109 can be rewritten as (X & (C2 << C1)) != 0. */
12110 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12112 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12113 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12114 return fold_build2_loc (loc, code, type, tem, arg1);
12116 /* Otherwise, for signed (arithmetic) shifts,
12117 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12118 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12119 else if (!TYPE_UNSIGNED (itype))
12120 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12121 arg000, build_int_cst (itype, 0));
12122 /* Otherwise, of unsigned (logical) shifts,
12123 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12124 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12126 return omit_one_operand_loc (loc, type,
12127 code == EQ_EXPR ? integer_one_node
12128 : integer_zero_node,
12133 /* If this is an NE comparison of zero with an AND of one, remove the
12134 comparison since the AND will give the correct value. */
12135 if (code == NE_EXPR
12136 && integer_zerop (arg1)
12137 && TREE_CODE (arg0) == BIT_AND_EXPR
12138 && integer_onep (TREE_OPERAND (arg0, 1)))
12139 return fold_convert_loc (loc, type, arg0);
12141 /* If we have (A & C) == C where C is a power of 2, convert this into
12142 (A & C) != 0. Similarly for NE_EXPR. */
12143 if (TREE_CODE (arg0) == BIT_AND_EXPR
12144 && integer_pow2p (TREE_OPERAND (arg0, 1))
12145 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12146 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12147 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12148 integer_zero_node));
12150 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12151 bit, then fold the expression into A < 0 or A >= 0. */
12152 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12156 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12157 Similarly for NE_EXPR. */
12158 if (TREE_CODE (arg0) == BIT_AND_EXPR
12159 && TREE_CODE (arg1) == INTEGER_CST
12160 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12162 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12163 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12164 TREE_OPERAND (arg0, 1));
12165 tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12167 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12168 if (integer_nonzerop (dandnotc))
12169 return omit_one_operand_loc (loc, type, rslt, arg0);
12172 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12173 Similarly for NE_EXPR. */
12174 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12175 && TREE_CODE (arg1) == INTEGER_CST
12176 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12178 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12179 tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12180 TREE_OPERAND (arg0, 1), notd);
12181 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12182 if (integer_nonzerop (candnotd))
12183 return omit_one_operand_loc (loc, type, rslt, arg0);
12186 /* If this is a comparison of a field, we may be able to simplify it. */
12187 if ((TREE_CODE (arg0) == COMPONENT_REF
12188 || TREE_CODE (arg0) == BIT_FIELD_REF)
12189 /* Handle the constant case even without -O
12190 to make sure the warnings are given. */
12191 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12193 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12198 /* Optimize comparisons of strlen vs zero to a compare of the
12199 first character of the string vs zero. To wit,
12200 strlen(ptr) == 0 => *ptr == 0
12201 strlen(ptr) != 0 => *ptr != 0
12202 Other cases should reduce to one of these two (or a constant)
12203 due to the return value of strlen being unsigned. */
12204 if (TREE_CODE (arg0) == CALL_EXPR
12205 && integer_zerop (arg1))
12207 tree fndecl = get_callee_fndecl (arg0);
12210 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12211 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12212 && call_expr_nargs (arg0) == 1
12213 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12215 tree iref = build_fold_indirect_ref_loc (loc,
12216 CALL_EXPR_ARG (arg0, 0));
12217 return fold_build2_loc (loc, code, type, iref,
12218 build_int_cst (TREE_TYPE (iref), 0));
12222 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12223 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12224 if (TREE_CODE (arg0) == RSHIFT_EXPR
12225 && integer_zerop (arg1)
12226 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12228 tree arg00 = TREE_OPERAND (arg0, 0);
12229 tree arg01 = TREE_OPERAND (arg0, 1);
12230 tree itype = TREE_TYPE (arg00);
12231 if (TREE_INT_CST_HIGH (arg01) == 0
12232 && TREE_INT_CST_LOW (arg01)
12233 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12235 if (TYPE_UNSIGNED (itype))
12237 itype = signed_type_for (itype);
12238 arg00 = fold_convert_loc (loc, itype, arg00);
12240 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12241 type, arg00, build_int_cst (itype, 0));
12245 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12246 if (integer_zerop (arg1)
12247 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12248 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12249 TREE_OPERAND (arg0, 1));
12251 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12252 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12253 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12254 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12255 build_int_cst (TREE_TYPE (arg1), 0));
12256 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12257 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12258 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12259 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12260 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12261 build_int_cst (TREE_TYPE (arg1), 0));
12263 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12264 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12265 && TREE_CODE (arg1) == INTEGER_CST
12266 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12267 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12268 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12269 TREE_OPERAND (arg0, 1), arg1));
12271 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12272 (X & C) == 0 when C is a single bit. */
12273 if (TREE_CODE (arg0) == BIT_AND_EXPR
12274 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12275 && integer_zerop (arg1)
12276 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12278 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12279 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12280 TREE_OPERAND (arg0, 1));
12281 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12285 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12286 constant C is a power of two, i.e. a single bit. */
12287 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12288 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12289 && integer_zerop (arg1)
12290 && integer_pow2p (TREE_OPERAND (arg0, 1))
12291 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12292 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12294 tree arg00 = TREE_OPERAND (arg0, 0);
12295 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12296 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12299 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12300 when is C is a power of two, i.e. a single bit. */
12301 if (TREE_CODE (arg0) == BIT_AND_EXPR
12302 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12303 && integer_zerop (arg1)
12304 && integer_pow2p (TREE_OPERAND (arg0, 1))
12305 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12306 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12308 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12309 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12310 arg000, TREE_OPERAND (arg0, 1));
12311 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12312 tem, build_int_cst (TREE_TYPE (tem), 0));
12315 if (integer_zerop (arg1)
12316 && tree_expr_nonzero_p (arg0))
12318 tree res = constant_boolean_node (code==NE_EXPR, type);
12319 return omit_one_operand_loc (loc, type, res, arg0);
12322 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12323 if (TREE_CODE (arg0) == NEGATE_EXPR
12324 && TREE_CODE (arg1) == NEGATE_EXPR)
12325 return fold_build2_loc (loc, code, type,
12326 TREE_OPERAND (arg0, 0),
12327 TREE_OPERAND (arg1, 0));
12329 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12330 if (TREE_CODE (arg0) == BIT_AND_EXPR
12331 && TREE_CODE (arg1) == BIT_AND_EXPR)
12333 tree arg00 = TREE_OPERAND (arg0, 0);
12334 tree arg01 = TREE_OPERAND (arg0, 1);
12335 tree arg10 = TREE_OPERAND (arg1, 0);
12336 tree arg11 = TREE_OPERAND (arg1, 1);
12337 tree itype = TREE_TYPE (arg0);
12339 if (operand_equal_p (arg01, arg11, 0))
12340 return fold_build2_loc (loc, code, type,
12341 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12342 fold_build2_loc (loc,
12343 BIT_XOR_EXPR, itype,
12346 build_int_cst (itype, 0));
12348 if (operand_equal_p (arg01, arg10, 0))
12349 return fold_build2_loc (loc, code, type,
12350 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12351 fold_build2_loc (loc,
12352 BIT_XOR_EXPR, itype,
12355 build_int_cst (itype, 0));
12357 if (operand_equal_p (arg00, arg11, 0))
12358 return fold_build2_loc (loc, code, type,
12359 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12360 fold_build2_loc (loc,
12361 BIT_XOR_EXPR, itype,
12364 build_int_cst (itype, 0));
12366 if (operand_equal_p (arg00, arg10, 0))
12367 return fold_build2_loc (loc, code, type,
12368 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12369 fold_build2_loc (loc,
12370 BIT_XOR_EXPR, itype,
12373 build_int_cst (itype, 0));
12376 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12377 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12379 tree arg00 = TREE_OPERAND (arg0, 0);
12380 tree arg01 = TREE_OPERAND (arg0, 1);
12381 tree arg10 = TREE_OPERAND (arg1, 0);
12382 tree arg11 = TREE_OPERAND (arg1, 1);
12383 tree itype = TREE_TYPE (arg0);
12385 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12386 operand_equal_p guarantees no side-effects so we don't need
12387 to use omit_one_operand on Z. */
12388 if (operand_equal_p (arg01, arg11, 0))
12389 return fold_build2_loc (loc, code, type, arg00, arg10);
12390 if (operand_equal_p (arg01, arg10, 0))
12391 return fold_build2_loc (loc, code, type, arg00, arg11);
12392 if (operand_equal_p (arg00, arg11, 0))
12393 return fold_build2_loc (loc, code, type, arg01, arg10);
12394 if (operand_equal_p (arg00, arg10, 0))
12395 return fold_build2_loc (loc, code, type, arg01, arg11);
12397 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12398 if (TREE_CODE (arg01) == INTEGER_CST
12399 && TREE_CODE (arg11) == INTEGER_CST)
12400 return fold_build2_loc (loc, code, type,
12401 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12402 fold_build2_loc (loc,
12403 BIT_XOR_EXPR, itype,
12408 /* Attempt to simplify equality/inequality comparisons of complex
12409 values. Only lower the comparison if the result is known or
12410 can be simplified to a single scalar comparison. */
12411 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12412 || TREE_CODE (arg0) == COMPLEX_CST)
12413 && (TREE_CODE (arg1) == COMPLEX_EXPR
12414 || TREE_CODE (arg1) == COMPLEX_CST))
12416 tree real0, imag0, real1, imag1;
12419 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12421 real0 = TREE_OPERAND (arg0, 0);
12422 imag0 = TREE_OPERAND (arg0, 1);
12426 real0 = TREE_REALPART (arg0);
12427 imag0 = TREE_IMAGPART (arg0);
12430 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12432 real1 = TREE_OPERAND (arg1, 0);
12433 imag1 = TREE_OPERAND (arg1, 1);
12437 real1 = TREE_REALPART (arg1);
12438 imag1 = TREE_IMAGPART (arg1);
12441 rcond = fold_binary_loc (loc, code, type, real0, real1);
12442 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12444 if (integer_zerop (rcond))
12446 if (code == EQ_EXPR)
12447 return omit_two_operands_loc (loc, type, boolean_false_node,
12449 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12453 if (code == NE_EXPR)
12454 return omit_two_operands_loc (loc, type, boolean_true_node,
12456 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12460 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12461 if (icond && TREE_CODE (icond) == INTEGER_CST)
12463 if (integer_zerop (icond))
12465 if (code == EQ_EXPR)
12466 return omit_two_operands_loc (loc, type, boolean_false_node,
12468 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12472 if (code == NE_EXPR)
12473 return omit_two_operands_loc (loc, type, boolean_true_node,
12475 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12486 tem = fold_comparison (loc, code, type, op0, op1);
12487 if (tem != NULL_TREE)
12490 /* Transform comparisons of the form X +- C CMP X. */
12491 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12492 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12493 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12494 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12495 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12496 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12498 tree arg01 = TREE_OPERAND (arg0, 1);
12499 enum tree_code code0 = TREE_CODE (arg0);
12502 if (TREE_CODE (arg01) == REAL_CST)
12503 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12505 is_positive = tree_int_cst_sgn (arg01);
12507 /* (X - c) > X becomes false. */
12508 if (code == GT_EXPR
12509 && ((code0 == MINUS_EXPR && is_positive >= 0)
12510 || (code0 == PLUS_EXPR && is_positive <= 0)))
12512 if (TREE_CODE (arg01) == INTEGER_CST
12513 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12514 fold_overflow_warning (("assuming signed overflow does not "
12515 "occur when assuming that (X - c) > X "
12516 "is always false"),
12517 WARN_STRICT_OVERFLOW_ALL);
12518 return constant_boolean_node (0, type);
12521 /* Likewise (X + c) < X becomes false. */
12522 if (code == LT_EXPR
12523 && ((code0 == PLUS_EXPR && is_positive >= 0)
12524 || (code0 == MINUS_EXPR && is_positive <= 0)))
12526 if (TREE_CODE (arg01) == INTEGER_CST
12527 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12528 fold_overflow_warning (("assuming signed overflow does not "
12529 "occur when assuming that "
12530 "(X + c) < X is always false"),
12531 WARN_STRICT_OVERFLOW_ALL);
12532 return constant_boolean_node (0, type);
12535 /* Convert (X - c) <= X to true. */
12536 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12538 && ((code0 == MINUS_EXPR && is_positive >= 0)
12539 || (code0 == PLUS_EXPR && is_positive <= 0)))
12541 if (TREE_CODE (arg01) == INTEGER_CST
12542 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12543 fold_overflow_warning (("assuming signed overflow does not "
12544 "occur when assuming that "
12545 "(X - c) <= X is always true"),
12546 WARN_STRICT_OVERFLOW_ALL);
12547 return constant_boolean_node (1, type);
12550 /* Convert (X + c) >= X to true. */
12551 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12553 && ((code0 == PLUS_EXPR && is_positive >= 0)
12554 || (code0 == MINUS_EXPR && is_positive <= 0)))
12556 if (TREE_CODE (arg01) == INTEGER_CST
12557 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12558 fold_overflow_warning (("assuming signed overflow does not "
12559 "occur when assuming that "
12560 "(X + c) >= X is always true"),
12561 WARN_STRICT_OVERFLOW_ALL);
12562 return constant_boolean_node (1, type);
12565 if (TREE_CODE (arg01) == INTEGER_CST)
12567 /* Convert X + c > X and X - c < X to true for integers. */
12568 if (code == GT_EXPR
12569 && ((code0 == PLUS_EXPR && is_positive > 0)
12570 || (code0 == MINUS_EXPR && is_positive < 0)))
12572 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12573 fold_overflow_warning (("assuming signed overflow does "
12574 "not occur when assuming that "
12575 "(X + c) > X is always true"),
12576 WARN_STRICT_OVERFLOW_ALL);
12577 return constant_boolean_node (1, type);
12580 if (code == LT_EXPR
12581 && ((code0 == MINUS_EXPR && is_positive > 0)
12582 || (code0 == PLUS_EXPR && is_positive < 0)))
12584 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12585 fold_overflow_warning (("assuming signed overflow does "
12586 "not occur when assuming that "
12587 "(X - c) < X is always true"),
12588 WARN_STRICT_OVERFLOW_ALL);
12589 return constant_boolean_node (1, type);
12592 /* Convert X + c <= X and X - c >= X to false for integers. */
12593 if (code == LE_EXPR
12594 && ((code0 == PLUS_EXPR && is_positive > 0)
12595 || (code0 == MINUS_EXPR && is_positive < 0)))
12597 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12598 fold_overflow_warning (("assuming signed overflow does "
12599 "not occur when assuming that "
12600 "(X + c) <= X is always false"),
12601 WARN_STRICT_OVERFLOW_ALL);
12602 return constant_boolean_node (0, type);
12605 if (code == GE_EXPR
12606 && ((code0 == MINUS_EXPR && is_positive > 0)
12607 || (code0 == PLUS_EXPR && is_positive < 0)))
12609 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12610 fold_overflow_warning (("assuming signed overflow does "
12611 "not occur when assuming that "
12612 "(X - c) >= X is always false"),
12613 WARN_STRICT_OVERFLOW_ALL);
12614 return constant_boolean_node (0, type);
12619 /* Comparisons with the highest or lowest possible integer of
12620 the specified precision will have known values. */
12622 tree arg1_type = TREE_TYPE (arg1);
12623 unsigned int width = TYPE_PRECISION (arg1_type);
12625 if (TREE_CODE (arg1) == INTEGER_CST
12626 && width <= 2 * HOST_BITS_PER_WIDE_INT
12627 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12629 HOST_WIDE_INT signed_max_hi;
12630 unsigned HOST_WIDE_INT signed_max_lo;
12631 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12633 if (width <= HOST_BITS_PER_WIDE_INT)
12635 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12640 if (TYPE_UNSIGNED (arg1_type))
12642 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12648 max_lo = signed_max_lo;
12649 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12655 width -= HOST_BITS_PER_WIDE_INT;
12656 signed_max_lo = -1;
12657 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12662 if (TYPE_UNSIGNED (arg1_type))
12664 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12669 max_hi = signed_max_hi;
12670 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12674 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12675 && TREE_INT_CST_LOW (arg1) == max_lo)
12679 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12682 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12685 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12688 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12690 /* The GE_EXPR and LT_EXPR cases above are not normally
12691 reached because of previous transformations. */
12696 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12698 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12702 arg1 = const_binop (PLUS_EXPR, arg1,
12703 build_int_cst (TREE_TYPE (arg1), 1), 0);
12704 return fold_build2_loc (loc, EQ_EXPR, type,
12705 fold_convert_loc (loc,
12706 TREE_TYPE (arg1), arg0),
12709 arg1 = const_binop (PLUS_EXPR, arg1,
12710 build_int_cst (TREE_TYPE (arg1), 1), 0);
12711 return fold_build2_loc (loc, NE_EXPR, type,
12712 fold_convert_loc (loc, TREE_TYPE (arg1),
12718 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12720 && TREE_INT_CST_LOW (arg1) == min_lo)
12724 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12727 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12730 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12733 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12738 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12740 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12744 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12745 return fold_build2_loc (loc, NE_EXPR, type,
12746 fold_convert_loc (loc,
12747 TREE_TYPE (arg1), arg0),
12750 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12751 return fold_build2_loc (loc, EQ_EXPR, type,
12752 fold_convert_loc (loc, TREE_TYPE (arg1),
12759 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12760 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12761 && TYPE_UNSIGNED (arg1_type)
12762 /* We will flip the signedness of the comparison operator
12763 associated with the mode of arg1, so the sign bit is
12764 specified by this mode. Check that arg1 is the signed
12765 max associated with this sign bit. */
12766 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12767 /* signed_type does not work on pointer types. */
12768 && INTEGRAL_TYPE_P (arg1_type))
12770 /* The following case also applies to X < signed_max+1
12771 and X >= signed_max+1 because previous transformations. */
12772 if (code == LE_EXPR || code == GT_EXPR)
12775 st = signed_type_for (TREE_TYPE (arg1));
12776 return fold_build2_loc (loc,
12777 code == LE_EXPR ? GE_EXPR : LT_EXPR,
12778 type, fold_convert_loc (loc, st, arg0),
12779 build_int_cst (st, 0));
12785 /* If we are comparing an ABS_EXPR with a constant, we can
12786 convert all the cases into explicit comparisons, but they may
12787 well not be faster than doing the ABS and one comparison.
12788 But ABS (X) <= C is a range comparison, which becomes a subtraction
12789 and a comparison, and is probably faster. */
12790 if (code == LE_EXPR
12791 && TREE_CODE (arg1) == INTEGER_CST
12792 && TREE_CODE (arg0) == ABS_EXPR
12793 && ! TREE_SIDE_EFFECTS (arg0)
12794 && (0 != (tem = negate_expr (arg1)))
12795 && TREE_CODE (tem) == INTEGER_CST
12796 && !TREE_OVERFLOW (tem))
12797 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12798 build2 (GE_EXPR, type,
12799 TREE_OPERAND (arg0, 0), tem),
12800 build2 (LE_EXPR, type,
12801 TREE_OPERAND (arg0, 0), arg1));
12803 /* Convert ABS_EXPR<x> >= 0 to true. */
12804 strict_overflow_p = false;
12805 if (code == GE_EXPR
12806 && (integer_zerop (arg1)
12807 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12808 && real_zerop (arg1)))
12809 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12811 if (strict_overflow_p)
12812 fold_overflow_warning (("assuming signed overflow does not occur "
12813 "when simplifying comparison of "
12814 "absolute value and zero"),
12815 WARN_STRICT_OVERFLOW_CONDITIONAL);
12816 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12819 /* Convert ABS_EXPR<x> < 0 to false. */
12820 strict_overflow_p = false;
12821 if (code == LT_EXPR
12822 && (integer_zerop (arg1) || real_zerop (arg1))
12823 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12825 if (strict_overflow_p)
12826 fold_overflow_warning (("assuming signed overflow does not occur "
12827 "when simplifying comparison of "
12828 "absolute value and zero"),
12829 WARN_STRICT_OVERFLOW_CONDITIONAL);
12830 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12833 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12834 and similarly for >= into !=. */
12835 if ((code == LT_EXPR || code == GE_EXPR)
12836 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12837 && TREE_CODE (arg1) == LSHIFT_EXPR
12838 && integer_onep (TREE_OPERAND (arg1, 0)))
12840 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12841 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12842 TREE_OPERAND (arg1, 1)),
12843 build_int_cst (TREE_TYPE (arg0), 0));
12844 goto fold_binary_exit;
12847 if ((code == LT_EXPR || code == GE_EXPR)
12848 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12849 && CONVERT_EXPR_P (arg1)
12850 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12851 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12853 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12854 fold_convert_loc (loc, TREE_TYPE (arg0),
12855 build2 (RSHIFT_EXPR,
12856 TREE_TYPE (arg0), arg0,
12857 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12859 build_int_cst (TREE_TYPE (arg0), 0));
12860 goto fold_binary_exit;
12865 case UNORDERED_EXPR:
12873 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12875 t1 = fold_relational_const (code, type, arg0, arg1);
12876 if (t1 != NULL_TREE)
12880 /* If the first operand is NaN, the result is constant. */
12881 if (TREE_CODE (arg0) == REAL_CST
12882 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12883 && (code != LTGT_EXPR || ! flag_trapping_math))
12885 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12886 ? integer_zero_node
12887 : integer_one_node;
12888 return omit_one_operand_loc (loc, type, t1, arg1);
12891 /* If the second operand is NaN, the result is constant. */
12892 if (TREE_CODE (arg1) == REAL_CST
12893 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12894 && (code != LTGT_EXPR || ! flag_trapping_math))
12896 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12897 ? integer_zero_node
12898 : integer_one_node;
12899 return omit_one_operand_loc (loc, type, t1, arg0);
12902 /* Simplify unordered comparison of something with itself. */
12903 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12904 && operand_equal_p (arg0, arg1, 0))
12905 return constant_boolean_node (1, type);
12907 if (code == LTGT_EXPR
12908 && !flag_trapping_math
12909 && operand_equal_p (arg0, arg1, 0))
12910 return constant_boolean_node (0, type);
12912 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12914 tree targ0 = strip_float_extensions (arg0);
12915 tree targ1 = strip_float_extensions (arg1);
12916 tree newtype = TREE_TYPE (targ0);
12918 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12919 newtype = TREE_TYPE (targ1);
12921 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12922 return fold_build2_loc (loc, code, type,
12923 fold_convert_loc (loc, newtype, targ0),
12924 fold_convert_loc (loc, newtype, targ1));
12929 case COMPOUND_EXPR:
12930 /* When pedantic, a compound expression can be neither an lvalue
12931 nor an integer constant expression. */
12932 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12934 /* Don't let (0, 0) be null pointer constant. */
12935 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12936 : fold_convert_loc (loc, type, arg1);
12937 return pedantic_non_lvalue_loc (loc, tem);
12940 if ((TREE_CODE (arg0) == REAL_CST
12941 && TREE_CODE (arg1) == REAL_CST)
12942 || (TREE_CODE (arg0) == INTEGER_CST
12943 && TREE_CODE (arg1) == INTEGER_CST))
12944 return build_complex (type, arg0, arg1);
12948 /* An ASSERT_EXPR should never be passed to fold_binary. */
12949 gcc_unreachable ();
12953 } /* switch (code) */
12955 protected_set_expr_location (tem, loc);
12959 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12960 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
12964 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
12966 switch (TREE_CODE (*tp))
12972 *walk_subtrees = 0;
12974 /* ... fall through ... */
12981 /* Return whether the sub-tree ST contains a label which is accessible from
12982 outside the sub-tree. */
12985 contains_label_p (tree st)
12988 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
12991 /* Fold a ternary expression of code CODE and type TYPE with operands
12992 OP0, OP1, and OP2. Return the folded expression if folding is
12993 successful. Otherwise, return NULL_TREE. */
12996 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12997 tree op0, tree op1, tree op2)
13000 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13001 enum tree_code_class kind = TREE_CODE_CLASS (code);
13003 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13004 && TREE_CODE_LENGTH (code) == 3);
13006 /* Strip any conversions that don't change the mode. This is safe
13007 for every expression, except for a comparison expression because
13008 its signedness is derived from its operands. So, in the latter
13009 case, only strip conversions that don't change the signedness.
13011 Note that this is done as an internal manipulation within the
13012 constant folder, in order to find the simplest representation of
13013 the arguments so that their form can be studied. In any cases,
13014 the appropriate type conversions should be put back in the tree
13015 that will get out of the constant folder. */
13030 case COMPONENT_REF:
13031 if (TREE_CODE (arg0) == CONSTRUCTOR
13032 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13034 unsigned HOST_WIDE_INT idx;
13036 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13043 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13044 so all simple results must be passed through pedantic_non_lvalue. */
13045 if (TREE_CODE (arg0) == INTEGER_CST)
13047 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13048 tem = integer_zerop (arg0) ? op2 : op1;
13049 /* Only optimize constant conditions when the selected branch
13050 has the same type as the COND_EXPR. This avoids optimizing
13051 away "c ? x : throw", where the throw has a void type.
13052 Avoid throwing away that operand which contains label. */
13053 if ((!TREE_SIDE_EFFECTS (unused_op)
13054 || !contains_label_p (unused_op))
13055 && (! VOID_TYPE_P (TREE_TYPE (tem))
13056 || VOID_TYPE_P (type)))
13057 return pedantic_non_lvalue_loc (loc, tem);
13060 if (operand_equal_p (arg1, op2, 0))
13061 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13063 /* If we have A op B ? A : C, we may be able to convert this to a
13064 simpler expression, depending on the operation and the values
13065 of B and C. Signed zeros prevent all of these transformations,
13066 for reasons given above each one.
13068 Also try swapping the arguments and inverting the conditional. */
13069 if (COMPARISON_CLASS_P (arg0)
13070 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13071 arg1, TREE_OPERAND (arg0, 1))
13072 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13074 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13079 if (COMPARISON_CLASS_P (arg0)
13080 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13082 TREE_OPERAND (arg0, 1))
13083 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13085 tem = fold_truth_not_expr (loc, arg0);
13086 if (tem && COMPARISON_CLASS_P (tem))
13088 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13094 /* If the second operand is simpler than the third, swap them
13095 since that produces better jump optimization results. */
13096 if (truth_value_p (TREE_CODE (arg0))
13097 && tree_swap_operands_p (op1, op2, false))
13099 /* See if this can be inverted. If it can't, possibly because
13100 it was a floating-point inequality comparison, don't do
13102 tem = fold_truth_not_expr (loc, arg0);
13104 return fold_build3_loc (loc, code, type, tem, op2, op1);
13107 /* Convert A ? 1 : 0 to simply A. */
13108 if (integer_onep (op1)
13109 && integer_zerop (op2)
13110 /* If we try to convert OP0 to our type, the
13111 call to fold will try to move the conversion inside
13112 a COND, which will recurse. In that case, the COND_EXPR
13113 is probably the best choice, so leave it alone. */
13114 && type == TREE_TYPE (arg0))
13115 return pedantic_non_lvalue_loc (loc, arg0);
13117 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13118 over COND_EXPR in cases such as floating point comparisons. */
13119 if (integer_zerop (op1)
13120 && integer_onep (op2)
13121 && truth_value_p (TREE_CODE (arg0)))
13122 return pedantic_non_lvalue_loc (loc,
13123 fold_convert_loc (loc, type,
13124 invert_truthvalue_loc (loc,
13127 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13128 if (TREE_CODE (arg0) == LT_EXPR
13129 && integer_zerop (TREE_OPERAND (arg0, 1))
13130 && integer_zerop (op2)
13131 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13133 /* sign_bit_p only checks ARG1 bits within A's precision.
13134 If <sign bit of A> has wider type than A, bits outside
13135 of A's precision in <sign bit of A> need to be checked.
13136 If they are all 0, this optimization needs to be done
13137 in unsigned A's type, if they are all 1 in signed A's type,
13138 otherwise this can't be done. */
13139 if (TYPE_PRECISION (TREE_TYPE (tem))
13140 < TYPE_PRECISION (TREE_TYPE (arg1))
13141 && TYPE_PRECISION (TREE_TYPE (tem))
13142 < TYPE_PRECISION (type))
13144 unsigned HOST_WIDE_INT mask_lo;
13145 HOST_WIDE_INT mask_hi;
13146 int inner_width, outer_width;
13149 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13150 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13151 if (outer_width > TYPE_PRECISION (type))
13152 outer_width = TYPE_PRECISION (type);
13154 if (outer_width > HOST_BITS_PER_WIDE_INT)
13156 mask_hi = ((unsigned HOST_WIDE_INT) -1
13157 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13163 mask_lo = ((unsigned HOST_WIDE_INT) -1
13164 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13166 if (inner_width > HOST_BITS_PER_WIDE_INT)
13168 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13169 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13173 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13174 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13176 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13177 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13179 tem_type = signed_type_for (TREE_TYPE (tem));
13180 tem = fold_convert_loc (loc, tem_type, tem);
13182 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13183 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13185 tem_type = unsigned_type_for (TREE_TYPE (tem));
13186 tem = fold_convert_loc (loc, tem_type, tem);
13194 fold_convert_loc (loc, type,
13195 fold_build2_loc (loc, BIT_AND_EXPR,
13196 TREE_TYPE (tem), tem,
13197 fold_convert_loc (loc,
13202 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13203 already handled above. */
13204 if (TREE_CODE (arg0) == BIT_AND_EXPR
13205 && integer_onep (TREE_OPERAND (arg0, 1))
13206 && integer_zerop (op2)
13207 && integer_pow2p (arg1))
13209 tree tem = TREE_OPERAND (arg0, 0);
13211 if (TREE_CODE (tem) == RSHIFT_EXPR
13212 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13213 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13214 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13215 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13216 TREE_OPERAND (tem, 0), arg1);
13219 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13220 is probably obsolete because the first operand should be a
13221 truth value (that's why we have the two cases above), but let's
13222 leave it in until we can confirm this for all front-ends. */
13223 if (integer_zerop (op2)
13224 && TREE_CODE (arg0) == NE_EXPR
13225 && integer_zerop (TREE_OPERAND (arg0, 1))
13226 && integer_pow2p (arg1)
13227 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13228 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13229 arg1, OEP_ONLY_CONST))
13230 return pedantic_non_lvalue_loc (loc,
13231 fold_convert_loc (loc, type,
13232 TREE_OPERAND (arg0, 0)));
13234 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13235 if (integer_zerop (op2)
13236 && truth_value_p (TREE_CODE (arg0))
13237 && truth_value_p (TREE_CODE (arg1)))
13238 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13239 fold_convert_loc (loc, type, arg0),
13242 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13243 if (integer_onep (op2)
13244 && truth_value_p (TREE_CODE (arg0))
13245 && truth_value_p (TREE_CODE (arg1)))
13247 /* Only perform transformation if ARG0 is easily inverted. */
13248 tem = fold_truth_not_expr (loc, arg0);
13250 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13251 fold_convert_loc (loc, type, tem),
13255 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13256 if (integer_zerop (arg1)
13257 && truth_value_p (TREE_CODE (arg0))
13258 && truth_value_p (TREE_CODE (op2)))
13260 /* Only perform transformation if ARG0 is easily inverted. */
13261 tem = fold_truth_not_expr (loc, arg0);
13263 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13264 fold_convert_loc (loc, type, tem),
13268 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13269 if (integer_onep (arg1)
13270 && truth_value_p (TREE_CODE (arg0))
13271 && truth_value_p (TREE_CODE (op2)))
13272 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13273 fold_convert_loc (loc, type, arg0),
13279 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13280 of fold_ternary on them. */
13281 gcc_unreachable ();
13283 case BIT_FIELD_REF:
13284 if ((TREE_CODE (arg0) == VECTOR_CST
13285 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13286 && type == TREE_TYPE (TREE_TYPE (arg0)))
13288 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13289 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13292 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13293 && (idx % width) == 0
13294 && (idx = idx / width)
13295 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13297 tree elements = NULL_TREE;
13299 if (TREE_CODE (arg0) == VECTOR_CST)
13300 elements = TREE_VECTOR_CST_ELTS (arg0);
13303 unsigned HOST_WIDE_INT idx;
13306 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13307 elements = tree_cons (NULL_TREE, value, elements);
13309 while (idx-- > 0 && elements)
13310 elements = TREE_CHAIN (elements);
13312 return TREE_VALUE (elements);
13314 return fold_convert_loc (loc, type, integer_zero_node);
13318 /* A bit-field-ref that referenced the full argument can be stripped. */
13319 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13320 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13321 && integer_zerop (op2))
13322 return fold_convert_loc (loc, type, arg0);
13328 } /* switch (code) */
13331 /* Perform constant folding and related simplification of EXPR.
13332 The related simplifications include x*1 => x, x*0 => 0, etc.,
13333 and application of the associative law.
13334 NOP_EXPR conversions may be removed freely (as long as we
13335 are careful not to change the type of the overall expression).
13336 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13337 but we can constant-fold them if they have constant operands. */
13339 #ifdef ENABLE_FOLD_CHECKING
13340 # define fold(x) fold_1 (x)
13341 static tree fold_1 (tree);
13347 const tree t = expr;
13348 enum tree_code code = TREE_CODE (t);
13349 enum tree_code_class kind = TREE_CODE_CLASS (code);
13351 location_t loc = EXPR_LOCATION (expr);
13353 /* Return right away if a constant. */
13354 if (kind == tcc_constant)
13357 /* CALL_EXPR-like objects with variable numbers of operands are
13358 treated specially. */
13359 if (kind == tcc_vl_exp)
13361 if (code == CALL_EXPR)
13363 tem = fold_call_expr (loc, expr, false);
13364 return tem ? tem : expr;
13369 if (IS_EXPR_CODE_CLASS (kind))
13371 tree type = TREE_TYPE (t);
13372 tree op0, op1, op2;
13374 switch (TREE_CODE_LENGTH (code))
13377 op0 = TREE_OPERAND (t, 0);
13378 tem = fold_unary_loc (loc, code, type, op0);
13379 return tem ? tem : expr;
13381 op0 = TREE_OPERAND (t, 0);
13382 op1 = TREE_OPERAND (t, 1);
13383 tem = fold_binary_loc (loc, code, type, op0, op1);
13384 return tem ? tem : expr;
13386 op0 = TREE_OPERAND (t, 0);
13387 op1 = TREE_OPERAND (t, 1);
13388 op2 = TREE_OPERAND (t, 2);
13389 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13390 return tem ? tem : expr;
13400 tree op0 = TREE_OPERAND (t, 0);
13401 tree op1 = TREE_OPERAND (t, 1);
13403 if (TREE_CODE (op1) == INTEGER_CST
13404 && TREE_CODE (op0) == CONSTRUCTOR
13405 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13407 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13408 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13409 unsigned HOST_WIDE_INT begin = 0;
13411 /* Find a matching index by means of a binary search. */
13412 while (begin != end)
13414 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13415 tree index = VEC_index (constructor_elt, elts, middle)->index;
13417 if (TREE_CODE (index) == INTEGER_CST
13418 && tree_int_cst_lt (index, op1))
13419 begin = middle + 1;
13420 else if (TREE_CODE (index) == INTEGER_CST
13421 && tree_int_cst_lt (op1, index))
13423 else if (TREE_CODE (index) == RANGE_EXPR
13424 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13425 begin = middle + 1;
13426 else if (TREE_CODE (index) == RANGE_EXPR
13427 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13430 return VEC_index (constructor_elt, elts, middle)->value;
13438 return fold (DECL_INITIAL (t));
13442 } /* switch (code) */
13445 #ifdef ENABLE_FOLD_CHECKING
13448 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13449 static void fold_check_failed (const_tree, const_tree);
13450 void print_fold_checksum (const_tree);
13452 /* When --enable-checking=fold, compute a digest of expr before
13453 and after actual fold call to see if fold did not accidentally
13454 change original expr. */
13460 struct md5_ctx ctx;
13461 unsigned char checksum_before[16], checksum_after[16];
13464 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13465 md5_init_ctx (&ctx);
13466 fold_checksum_tree (expr, &ctx, ht);
13467 md5_finish_ctx (&ctx, checksum_before);
13470 ret = fold_1 (expr);
13472 md5_init_ctx (&ctx);
13473 fold_checksum_tree (expr, &ctx, ht);
13474 md5_finish_ctx (&ctx, checksum_after);
13477 if (memcmp (checksum_before, checksum_after, 16))
13478 fold_check_failed (expr, ret);
13484 print_fold_checksum (const_tree expr)
13486 struct md5_ctx ctx;
13487 unsigned char checksum[16], cnt;
13490 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13491 md5_init_ctx (&ctx);
13492 fold_checksum_tree (expr, &ctx, ht);
13493 md5_finish_ctx (&ctx, checksum);
13495 for (cnt = 0; cnt < 16; ++cnt)
13496 fprintf (stderr, "%02x", checksum[cnt]);
13497 putc ('\n', stderr);
13501 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13503 internal_error ("fold check: original tree changed by fold");
13507 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13510 enum tree_code code;
13511 union tree_node buf;
13516 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13517 <= sizeof (struct tree_function_decl))
13518 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13521 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13525 code = TREE_CODE (expr);
13526 if (TREE_CODE_CLASS (code) == tcc_declaration
13527 && DECL_ASSEMBLER_NAME_SET_P (expr))
13529 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13530 memcpy ((char *) &buf, expr, tree_size (expr));
13531 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13532 expr = (tree) &buf;
13534 else if (TREE_CODE_CLASS (code) == tcc_type
13535 && (TYPE_POINTER_TO (expr)
13536 || TYPE_REFERENCE_TO (expr)
13537 || TYPE_CACHED_VALUES_P (expr)
13538 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13539 || TYPE_NEXT_VARIANT (expr)))
13541 /* Allow these fields to be modified. */
13543 memcpy ((char *) &buf, expr, tree_size (expr));
13544 expr = tmp = (tree) &buf;
13545 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13546 TYPE_POINTER_TO (tmp) = NULL;
13547 TYPE_REFERENCE_TO (tmp) = NULL;
13548 TYPE_NEXT_VARIANT (tmp) = NULL;
13549 if (TYPE_CACHED_VALUES_P (tmp))
13551 TYPE_CACHED_VALUES_P (tmp) = 0;
13552 TYPE_CACHED_VALUES (tmp) = NULL;
13555 md5_process_bytes (expr, tree_size (expr), ctx);
13556 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13557 if (TREE_CODE_CLASS (code) != tcc_type
13558 && TREE_CODE_CLASS (code) != tcc_declaration
13559 && code != TREE_LIST
13560 && code != SSA_NAME)
13561 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13562 switch (TREE_CODE_CLASS (code))
13568 md5_process_bytes (TREE_STRING_POINTER (expr),
13569 TREE_STRING_LENGTH (expr), ctx);
13572 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13573 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13576 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13582 case tcc_exceptional:
13586 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13587 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13588 expr = TREE_CHAIN (expr);
13589 goto recursive_label;
13592 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13593 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13599 case tcc_expression:
13600 case tcc_reference:
13601 case tcc_comparison:
13604 case tcc_statement:
13606 len = TREE_OPERAND_LENGTH (expr);
13607 for (i = 0; i < len; ++i)
13608 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13610 case tcc_declaration:
13611 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13612 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13613 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13615 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13616 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13617 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13618 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13619 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13621 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13622 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13624 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13626 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13627 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13628 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13632 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13633 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13634 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13635 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13636 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13637 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13638 if (INTEGRAL_TYPE_P (expr)
13639 || SCALAR_FLOAT_TYPE_P (expr))
13641 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13642 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13644 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13645 if (TREE_CODE (expr) == RECORD_TYPE
13646 || TREE_CODE (expr) == UNION_TYPE
13647 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13648 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13649 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13656 /* Helper function for outputting the checksum of a tree T. When
13657 debugging with gdb, you can "define mynext" to be "next" followed
13658 by "call debug_fold_checksum (op0)", then just trace down till the
13662 debug_fold_checksum (const_tree t)
13665 unsigned char checksum[16];
13666 struct md5_ctx ctx;
13667 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13669 md5_init_ctx (&ctx);
13670 fold_checksum_tree (t, &ctx, ht);
13671 md5_finish_ctx (&ctx, checksum);
13674 for (i = 0; i < 16; i++)
13675 fprintf (stderr, "%d ", checksum[i]);
13677 fprintf (stderr, "\n");
13682 /* Fold a unary tree expression with code CODE of type TYPE with an
13683 operand OP0. LOC is the location of the resulting expression.
13684 Return a folded expression if successful. Otherwise, return a tree
13685 expression with code CODE of type TYPE with an operand OP0. */
13688 fold_build1_stat_loc (location_t loc,
13689 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13692 #ifdef ENABLE_FOLD_CHECKING
13693 unsigned char checksum_before[16], checksum_after[16];
13694 struct md5_ctx ctx;
13697 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13698 md5_init_ctx (&ctx);
13699 fold_checksum_tree (op0, &ctx, ht);
13700 md5_finish_ctx (&ctx, checksum_before);
13704 tem = fold_unary_loc (loc, code, type, op0);
13707 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13708 SET_EXPR_LOCATION (tem, loc);
13711 #ifdef ENABLE_FOLD_CHECKING
13712 md5_init_ctx (&ctx);
13713 fold_checksum_tree (op0, &ctx, ht);
13714 md5_finish_ctx (&ctx, checksum_after);
13717 if (memcmp (checksum_before, checksum_after, 16))
13718 fold_check_failed (op0, tem);
13723 /* Fold a binary tree expression with code CODE of type TYPE with
13724 operands OP0 and OP1. LOC is the location of the resulting
13725 expression. Return a folded expression if successful. Otherwise,
13726 return a tree expression with code CODE of type TYPE with operands
13730 fold_build2_stat_loc (location_t loc,
13731 enum tree_code code, tree type, tree op0, tree op1
13735 #ifdef ENABLE_FOLD_CHECKING
13736 unsigned char checksum_before_op0[16],
13737 checksum_before_op1[16],
13738 checksum_after_op0[16],
13739 checksum_after_op1[16];
13740 struct md5_ctx ctx;
13743 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13744 md5_init_ctx (&ctx);
13745 fold_checksum_tree (op0, &ctx, ht);
13746 md5_finish_ctx (&ctx, checksum_before_op0);
13749 md5_init_ctx (&ctx);
13750 fold_checksum_tree (op1, &ctx, ht);
13751 md5_finish_ctx (&ctx, checksum_before_op1);
13755 tem = fold_binary_loc (loc, code, type, op0, op1);
13758 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13759 SET_EXPR_LOCATION (tem, loc);
13762 #ifdef ENABLE_FOLD_CHECKING
13763 md5_init_ctx (&ctx);
13764 fold_checksum_tree (op0, &ctx, ht);
13765 md5_finish_ctx (&ctx, checksum_after_op0);
13768 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13769 fold_check_failed (op0, tem);
13771 md5_init_ctx (&ctx);
13772 fold_checksum_tree (op1, &ctx, ht);
13773 md5_finish_ctx (&ctx, checksum_after_op1);
13776 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13777 fold_check_failed (op1, tem);
13782 /* Fold a ternary tree expression with code CODE of type TYPE with
13783 operands OP0, OP1, and OP2. Return a folded expression if
13784 successful. Otherwise, return a tree expression with code CODE of
13785 type TYPE with operands OP0, OP1, and OP2. */
13788 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
13789 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13792 #ifdef ENABLE_FOLD_CHECKING
13793 unsigned char checksum_before_op0[16],
13794 checksum_before_op1[16],
13795 checksum_before_op2[16],
13796 checksum_after_op0[16],
13797 checksum_after_op1[16],
13798 checksum_after_op2[16];
13799 struct md5_ctx ctx;
13802 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13803 md5_init_ctx (&ctx);
13804 fold_checksum_tree (op0, &ctx, ht);
13805 md5_finish_ctx (&ctx, checksum_before_op0);
13808 md5_init_ctx (&ctx);
13809 fold_checksum_tree (op1, &ctx, ht);
13810 md5_finish_ctx (&ctx, checksum_before_op1);
13813 md5_init_ctx (&ctx);
13814 fold_checksum_tree (op2, &ctx, ht);
13815 md5_finish_ctx (&ctx, checksum_before_op2);
13819 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13820 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13823 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13824 SET_EXPR_LOCATION (tem, loc);
13827 #ifdef ENABLE_FOLD_CHECKING
13828 md5_init_ctx (&ctx);
13829 fold_checksum_tree (op0, &ctx, ht);
13830 md5_finish_ctx (&ctx, checksum_after_op0);
13833 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13834 fold_check_failed (op0, tem);
13836 md5_init_ctx (&ctx);
13837 fold_checksum_tree (op1, &ctx, ht);
13838 md5_finish_ctx (&ctx, checksum_after_op1);
13841 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13842 fold_check_failed (op1, tem);
13844 md5_init_ctx (&ctx);
13845 fold_checksum_tree (op2, &ctx, ht);
13846 md5_finish_ctx (&ctx, checksum_after_op2);
13849 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13850 fold_check_failed (op2, tem);
13855 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13856 arguments in ARGARRAY, and a null static chain.
13857 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13858 of type TYPE from the given operands as constructed by build_call_array. */
13861 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13862 int nargs, tree *argarray)
13865 #ifdef ENABLE_FOLD_CHECKING
13866 unsigned char checksum_before_fn[16],
13867 checksum_before_arglist[16],
13868 checksum_after_fn[16],
13869 checksum_after_arglist[16];
13870 struct md5_ctx ctx;
13874 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13875 md5_init_ctx (&ctx);
13876 fold_checksum_tree (fn, &ctx, ht);
13877 md5_finish_ctx (&ctx, checksum_before_fn);
13880 md5_init_ctx (&ctx);
13881 for (i = 0; i < nargs; i++)
13882 fold_checksum_tree (argarray[i], &ctx, ht);
13883 md5_finish_ctx (&ctx, checksum_before_arglist);
13887 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13889 #ifdef ENABLE_FOLD_CHECKING
13890 md5_init_ctx (&ctx);
13891 fold_checksum_tree (fn, &ctx, ht);
13892 md5_finish_ctx (&ctx, checksum_after_fn);
13895 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13896 fold_check_failed (fn, tem);
13898 md5_init_ctx (&ctx);
13899 for (i = 0; i < nargs; i++)
13900 fold_checksum_tree (argarray[i], &ctx, ht);
13901 md5_finish_ctx (&ctx, checksum_after_arglist);
13904 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13905 fold_check_failed (NULL_TREE, tem);
13910 /* Perform constant folding and related simplification of initializer
13911 expression EXPR. These behave identically to "fold_buildN" but ignore
13912 potential run-time traps and exceptions that fold must preserve. */
13914 #define START_FOLD_INIT \
13915 int saved_signaling_nans = flag_signaling_nans;\
13916 int saved_trapping_math = flag_trapping_math;\
13917 int saved_rounding_math = flag_rounding_math;\
13918 int saved_trapv = flag_trapv;\
13919 int saved_folding_initializer = folding_initializer;\
13920 flag_signaling_nans = 0;\
13921 flag_trapping_math = 0;\
13922 flag_rounding_math = 0;\
13924 folding_initializer = 1;
13926 #define END_FOLD_INIT \
13927 flag_signaling_nans = saved_signaling_nans;\
13928 flag_trapping_math = saved_trapping_math;\
13929 flag_rounding_math = saved_rounding_math;\
13930 flag_trapv = saved_trapv;\
13931 folding_initializer = saved_folding_initializer;
13934 fold_build1_initializer_loc (location_t loc, enum tree_code code,
13935 tree type, tree op)
13940 result = fold_build1_loc (loc, code, type, op);
13947 fold_build2_initializer_loc (location_t loc, enum tree_code code,
13948 tree type, tree op0, tree op1)
13953 result = fold_build2_loc (loc, code, type, op0, op1);
13960 fold_build3_initializer_loc (location_t loc, enum tree_code code,
13961 tree type, tree op0, tree op1, tree op2)
13966 result = fold_build3_loc (loc, code, type, op0, op1, op2);
13973 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13974 int nargs, tree *argarray)
13979 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13985 #undef START_FOLD_INIT
13986 #undef END_FOLD_INIT
13988 /* Determine if first argument is a multiple of second argument. Return 0 if
13989 it is not, or we cannot easily determined it to be.
13991 An example of the sort of thing we care about (at this point; this routine
13992 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13993 fold cases do now) is discovering that
13995 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14001 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14003 This code also handles discovering that
14005 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14007 is a multiple of 8 so we don't have to worry about dealing with a
14008 possible remainder.
14010 Note that we *look* inside a SAVE_EXPR only to determine how it was
14011 calculated; it is not safe for fold to do much of anything else with the
14012 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14013 at run time. For example, the latter example above *cannot* be implemented
14014 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14015 evaluation time of the original SAVE_EXPR is not necessarily the same at
14016 the time the new expression is evaluated. The only optimization of this
14017 sort that would be valid is changing
14019 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14023 SAVE_EXPR (I) * SAVE_EXPR (J)
14025 (where the same SAVE_EXPR (J) is used in the original and the
14026 transformed version). */
14029 multiple_of_p (tree type, const_tree top, const_tree bottom)
14031 if (operand_equal_p (top, bottom, 0))
14034 if (TREE_CODE (type) != INTEGER_TYPE)
14037 switch (TREE_CODE (top))
14040 /* Bitwise and provides a power of two multiple. If the mask is
14041 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14042 if (!integer_pow2p (bottom))
14047 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14048 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14052 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14053 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14056 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14060 op1 = TREE_OPERAND (top, 1);
14061 /* const_binop may not detect overflow correctly,
14062 so check for it explicitly here. */
14063 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14064 > TREE_INT_CST_LOW (op1)
14065 && TREE_INT_CST_HIGH (op1) == 0
14066 && 0 != (t1 = fold_convert (type,
14067 const_binop (LSHIFT_EXPR,
14070 && !TREE_OVERFLOW (t1))
14071 return multiple_of_p (type, t1, bottom);
14076 /* Can't handle conversions from non-integral or wider integral type. */
14077 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14078 || (TYPE_PRECISION (type)
14079 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14082 /* .. fall through ... */
14085 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14088 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14089 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14092 if (TREE_CODE (bottom) != INTEGER_CST
14093 || integer_zerop (bottom)
14094 || (TYPE_UNSIGNED (type)
14095 && (tree_int_cst_sgn (top) < 0
14096 || tree_int_cst_sgn (bottom) < 0)))
14098 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14106 /* Return true if CODE or TYPE is known to be non-negative. */
14109 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14111 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14112 && truth_value_p (code))
14113 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14114 have a signed:1 type (where the value is -1 and 0). */
14119 /* Return true if (CODE OP0) is known to be non-negative. If the return
14120 value is based on the assumption that signed overflow is undefined,
14121 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14122 *STRICT_OVERFLOW_P. */
14125 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14126 bool *strict_overflow_p)
14128 if (TYPE_UNSIGNED (type))
14134 /* We can't return 1 if flag_wrapv is set because
14135 ABS_EXPR<INT_MIN> = INT_MIN. */
14136 if (!INTEGRAL_TYPE_P (type))
14138 if (TYPE_OVERFLOW_UNDEFINED (type))
14140 *strict_overflow_p = true;
14145 case NON_LVALUE_EXPR:
14147 case FIX_TRUNC_EXPR:
14148 return tree_expr_nonnegative_warnv_p (op0,
14149 strict_overflow_p);
14153 tree inner_type = TREE_TYPE (op0);
14154 tree outer_type = type;
14156 if (TREE_CODE (outer_type) == REAL_TYPE)
14158 if (TREE_CODE (inner_type) == REAL_TYPE)
14159 return tree_expr_nonnegative_warnv_p (op0,
14160 strict_overflow_p);
14161 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14163 if (TYPE_UNSIGNED (inner_type))
14165 return tree_expr_nonnegative_warnv_p (op0,
14166 strict_overflow_p);
14169 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14171 if (TREE_CODE (inner_type) == REAL_TYPE)
14172 return tree_expr_nonnegative_warnv_p (op0,
14173 strict_overflow_p);
14174 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14175 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14176 && TYPE_UNSIGNED (inner_type);
14182 return tree_simple_nonnegative_warnv_p (code, type);
14185 /* We don't know sign of `t', so be conservative and return false. */
14189 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14190 value is based on the assumption that signed overflow is undefined,
14191 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14192 *STRICT_OVERFLOW_P. */
14195 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14196 tree op1, bool *strict_overflow_p)
14198 if (TYPE_UNSIGNED (type))
14203 case POINTER_PLUS_EXPR:
14205 if (FLOAT_TYPE_P (type))
14206 return (tree_expr_nonnegative_warnv_p (op0,
14208 && tree_expr_nonnegative_warnv_p (op1,
14209 strict_overflow_p));
14211 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14212 both unsigned and at least 2 bits shorter than the result. */
14213 if (TREE_CODE (type) == INTEGER_TYPE
14214 && TREE_CODE (op0) == NOP_EXPR
14215 && TREE_CODE (op1) == NOP_EXPR)
14217 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14218 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14219 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14220 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14222 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14223 TYPE_PRECISION (inner2)) + 1;
14224 return prec < TYPE_PRECISION (type);
14230 if (FLOAT_TYPE_P (type))
14232 /* x * x for floating point x is always non-negative. */
14233 if (operand_equal_p (op0, op1, 0))
14235 return (tree_expr_nonnegative_warnv_p (op0,
14237 && tree_expr_nonnegative_warnv_p (op1,
14238 strict_overflow_p));
14241 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14242 both unsigned and their total bits is shorter than the result. */
14243 if (TREE_CODE (type) == INTEGER_TYPE
14244 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14245 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14247 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14248 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14250 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14251 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14254 bool unsigned0 = TYPE_UNSIGNED (inner0);
14255 bool unsigned1 = TYPE_UNSIGNED (inner1);
14257 if (TREE_CODE (op0) == INTEGER_CST)
14258 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14260 if (TREE_CODE (op1) == INTEGER_CST)
14261 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14263 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14264 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14266 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14267 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14268 : TYPE_PRECISION (inner0);
14270 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14271 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14272 : TYPE_PRECISION (inner1);
14274 return precision0 + precision1 < TYPE_PRECISION (type);
14281 return (tree_expr_nonnegative_warnv_p (op0,
14283 || tree_expr_nonnegative_warnv_p (op1,
14284 strict_overflow_p));
14290 case TRUNC_DIV_EXPR:
14291 case CEIL_DIV_EXPR:
14292 case FLOOR_DIV_EXPR:
14293 case ROUND_DIV_EXPR:
14294 return (tree_expr_nonnegative_warnv_p (op0,
14296 && tree_expr_nonnegative_warnv_p (op1,
14297 strict_overflow_p));
14299 case TRUNC_MOD_EXPR:
14300 case CEIL_MOD_EXPR:
14301 case FLOOR_MOD_EXPR:
14302 case ROUND_MOD_EXPR:
14303 return tree_expr_nonnegative_warnv_p (op0,
14304 strict_overflow_p);
14306 return tree_simple_nonnegative_warnv_p (code, type);
14309 /* We don't know sign of `t', so be conservative and return false. */
14313 /* Return true if T is known to be non-negative. If the return
14314 value is based on the assumption that signed overflow is undefined,
14315 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14316 *STRICT_OVERFLOW_P. */
14319 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14321 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14324 switch (TREE_CODE (t))
14327 return tree_int_cst_sgn (t) >= 0;
14330 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14333 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14336 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14338 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14339 strict_overflow_p));
14341 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14344 /* We don't know sign of `t', so be conservative and return false. */
14348 /* Return true if T is known to be non-negative. If the return
14349 value is based on the assumption that signed overflow is undefined,
14350 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14351 *STRICT_OVERFLOW_P. */
14354 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14355 tree arg0, tree arg1, bool *strict_overflow_p)
14357 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14358 switch (DECL_FUNCTION_CODE (fndecl))
14360 CASE_FLT_FN (BUILT_IN_ACOS):
14361 CASE_FLT_FN (BUILT_IN_ACOSH):
14362 CASE_FLT_FN (BUILT_IN_CABS):
14363 CASE_FLT_FN (BUILT_IN_COSH):
14364 CASE_FLT_FN (BUILT_IN_ERFC):
14365 CASE_FLT_FN (BUILT_IN_EXP):
14366 CASE_FLT_FN (BUILT_IN_EXP10):
14367 CASE_FLT_FN (BUILT_IN_EXP2):
14368 CASE_FLT_FN (BUILT_IN_FABS):
14369 CASE_FLT_FN (BUILT_IN_FDIM):
14370 CASE_FLT_FN (BUILT_IN_HYPOT):
14371 CASE_FLT_FN (BUILT_IN_POW10):
14372 CASE_INT_FN (BUILT_IN_FFS):
14373 CASE_INT_FN (BUILT_IN_PARITY):
14374 CASE_INT_FN (BUILT_IN_POPCOUNT):
14375 case BUILT_IN_BSWAP32:
14376 case BUILT_IN_BSWAP64:
14380 CASE_FLT_FN (BUILT_IN_SQRT):
14381 /* sqrt(-0.0) is -0.0. */
14382 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14384 return tree_expr_nonnegative_warnv_p (arg0,
14385 strict_overflow_p);
14387 CASE_FLT_FN (BUILT_IN_ASINH):
14388 CASE_FLT_FN (BUILT_IN_ATAN):
14389 CASE_FLT_FN (BUILT_IN_ATANH):
14390 CASE_FLT_FN (BUILT_IN_CBRT):
14391 CASE_FLT_FN (BUILT_IN_CEIL):
14392 CASE_FLT_FN (BUILT_IN_ERF):
14393 CASE_FLT_FN (BUILT_IN_EXPM1):
14394 CASE_FLT_FN (BUILT_IN_FLOOR):
14395 CASE_FLT_FN (BUILT_IN_FMOD):
14396 CASE_FLT_FN (BUILT_IN_FREXP):
14397 CASE_FLT_FN (BUILT_IN_LCEIL):
14398 CASE_FLT_FN (BUILT_IN_LDEXP):
14399 CASE_FLT_FN (BUILT_IN_LFLOOR):
14400 CASE_FLT_FN (BUILT_IN_LLCEIL):
14401 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14402 CASE_FLT_FN (BUILT_IN_LLRINT):
14403 CASE_FLT_FN (BUILT_IN_LLROUND):
14404 CASE_FLT_FN (BUILT_IN_LRINT):
14405 CASE_FLT_FN (BUILT_IN_LROUND):
14406 CASE_FLT_FN (BUILT_IN_MODF):
14407 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14408 CASE_FLT_FN (BUILT_IN_RINT):
14409 CASE_FLT_FN (BUILT_IN_ROUND):
14410 CASE_FLT_FN (BUILT_IN_SCALB):
14411 CASE_FLT_FN (BUILT_IN_SCALBLN):
14412 CASE_FLT_FN (BUILT_IN_SCALBN):
14413 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14414 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14415 CASE_FLT_FN (BUILT_IN_SINH):
14416 CASE_FLT_FN (BUILT_IN_TANH):
14417 CASE_FLT_FN (BUILT_IN_TRUNC):
14418 /* True if the 1st argument is nonnegative. */
14419 return tree_expr_nonnegative_warnv_p (arg0,
14420 strict_overflow_p);
14422 CASE_FLT_FN (BUILT_IN_FMAX):
14423 /* True if the 1st OR 2nd arguments are nonnegative. */
14424 return (tree_expr_nonnegative_warnv_p (arg0,
14426 || (tree_expr_nonnegative_warnv_p (arg1,
14427 strict_overflow_p)));
14429 CASE_FLT_FN (BUILT_IN_FMIN):
14430 /* True if the 1st AND 2nd arguments are nonnegative. */
14431 return (tree_expr_nonnegative_warnv_p (arg0,
14433 && (tree_expr_nonnegative_warnv_p (arg1,
14434 strict_overflow_p)));
14436 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14437 /* True if the 2nd argument is nonnegative. */
14438 return tree_expr_nonnegative_warnv_p (arg1,
14439 strict_overflow_p);
14441 CASE_FLT_FN (BUILT_IN_POWI):
14442 /* True if the 1st argument is nonnegative or the second
14443 argument is an even integer. */
14444 if (TREE_CODE (arg1) == INTEGER_CST
14445 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14447 return tree_expr_nonnegative_warnv_p (arg0,
14448 strict_overflow_p);
14450 CASE_FLT_FN (BUILT_IN_POW):
14451 /* True if the 1st argument is nonnegative or the second
14452 argument is an even integer valued real. */
14453 if (TREE_CODE (arg1) == REAL_CST)
14458 c = TREE_REAL_CST (arg1);
14459 n = real_to_integer (&c);
14462 REAL_VALUE_TYPE cint;
14463 real_from_integer (&cint, VOIDmode, n,
14464 n < 0 ? -1 : 0, 0);
14465 if (real_identical (&c, &cint))
14469 return tree_expr_nonnegative_warnv_p (arg0,
14470 strict_overflow_p);
14475 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14479 /* Return true if T is known to be non-negative. If the return
14480 value is based on the assumption that signed overflow is undefined,
14481 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14482 *STRICT_OVERFLOW_P. */
14485 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14487 enum tree_code code = TREE_CODE (t);
14488 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14495 tree temp = TARGET_EXPR_SLOT (t);
14496 t = TARGET_EXPR_INITIAL (t);
14498 /* If the initializer is non-void, then it's a normal expression
14499 that will be assigned to the slot. */
14500 if (!VOID_TYPE_P (t))
14501 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14503 /* Otherwise, the initializer sets the slot in some way. One common
14504 way is an assignment statement at the end of the initializer. */
14507 if (TREE_CODE (t) == BIND_EXPR)
14508 t = expr_last (BIND_EXPR_BODY (t));
14509 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14510 || TREE_CODE (t) == TRY_CATCH_EXPR)
14511 t = expr_last (TREE_OPERAND (t, 0));
14512 else if (TREE_CODE (t) == STATEMENT_LIST)
14517 if (TREE_CODE (t) == MODIFY_EXPR
14518 && TREE_OPERAND (t, 0) == temp)
14519 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14520 strict_overflow_p);
14527 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14528 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14530 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14531 get_callee_fndecl (t),
14534 strict_overflow_p);
14536 case COMPOUND_EXPR:
14538 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14539 strict_overflow_p);
14541 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14542 strict_overflow_p);
14544 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14545 strict_overflow_p);
14548 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14552 /* We don't know sign of `t', so be conservative and return false. */
14556 /* Return true if T is known to be non-negative. If the return
14557 value is based on the assumption that signed overflow is undefined,
14558 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14559 *STRICT_OVERFLOW_P. */
14562 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14564 enum tree_code code;
14565 if (t == error_mark_node)
14568 code = TREE_CODE (t);
14569 switch (TREE_CODE_CLASS (code))
14572 case tcc_comparison:
14573 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14575 TREE_OPERAND (t, 0),
14576 TREE_OPERAND (t, 1),
14577 strict_overflow_p);
14580 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14582 TREE_OPERAND (t, 0),
14583 strict_overflow_p);
14586 case tcc_declaration:
14587 case tcc_reference:
14588 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14596 case TRUTH_AND_EXPR:
14597 case TRUTH_OR_EXPR:
14598 case TRUTH_XOR_EXPR:
14599 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14601 TREE_OPERAND (t, 0),
14602 TREE_OPERAND (t, 1),
14603 strict_overflow_p);
14604 case TRUTH_NOT_EXPR:
14605 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14607 TREE_OPERAND (t, 0),
14608 strict_overflow_p);
14615 case WITH_SIZE_EXPR:
14617 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14620 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14624 /* Return true if `t' is known to be non-negative. Handle warnings
14625 about undefined signed overflow. */
14628 tree_expr_nonnegative_p (tree t)
14630 bool ret, strict_overflow_p;
14632 strict_overflow_p = false;
14633 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14634 if (strict_overflow_p)
14635 fold_overflow_warning (("assuming signed overflow does not occur when "
14636 "determining that expression is always "
14638 WARN_STRICT_OVERFLOW_MISC);
14643 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14644 For floating point we further ensure that T is not denormal.
14645 Similar logic is present in nonzero_address in rtlanal.h.
14647 If the return value is based on the assumption that signed overflow
14648 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14649 change *STRICT_OVERFLOW_P. */
14652 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14653 bool *strict_overflow_p)
14658 return tree_expr_nonzero_warnv_p (op0,
14659 strict_overflow_p);
14663 tree inner_type = TREE_TYPE (op0);
14664 tree outer_type = type;
14666 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14667 && tree_expr_nonzero_warnv_p (op0,
14668 strict_overflow_p));
14672 case NON_LVALUE_EXPR:
14673 return tree_expr_nonzero_warnv_p (op0,
14674 strict_overflow_p);
14683 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14684 For floating point we further ensure that T is not denormal.
14685 Similar logic is present in nonzero_address in rtlanal.h.
14687 If the return value is based on the assumption that signed overflow
14688 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14689 change *STRICT_OVERFLOW_P. */
14692 tree_binary_nonzero_warnv_p (enum tree_code code,
14695 tree op1, bool *strict_overflow_p)
14697 bool sub_strict_overflow_p;
14700 case POINTER_PLUS_EXPR:
14702 if (TYPE_OVERFLOW_UNDEFINED (type))
14704 /* With the presence of negative values it is hard
14705 to say something. */
14706 sub_strict_overflow_p = false;
14707 if (!tree_expr_nonnegative_warnv_p (op0,
14708 &sub_strict_overflow_p)
14709 || !tree_expr_nonnegative_warnv_p (op1,
14710 &sub_strict_overflow_p))
14712 /* One of operands must be positive and the other non-negative. */
14713 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14714 overflows, on a twos-complement machine the sum of two
14715 nonnegative numbers can never be zero. */
14716 return (tree_expr_nonzero_warnv_p (op0,
14718 || tree_expr_nonzero_warnv_p (op1,
14719 strict_overflow_p));
14724 if (TYPE_OVERFLOW_UNDEFINED (type))
14726 if (tree_expr_nonzero_warnv_p (op0,
14728 && tree_expr_nonzero_warnv_p (op1,
14729 strict_overflow_p))
14731 *strict_overflow_p = true;
14738 sub_strict_overflow_p = false;
14739 if (tree_expr_nonzero_warnv_p (op0,
14740 &sub_strict_overflow_p)
14741 && tree_expr_nonzero_warnv_p (op1,
14742 &sub_strict_overflow_p))
14744 if (sub_strict_overflow_p)
14745 *strict_overflow_p = true;
14750 sub_strict_overflow_p = false;
14751 if (tree_expr_nonzero_warnv_p (op0,
14752 &sub_strict_overflow_p))
14754 if (sub_strict_overflow_p)
14755 *strict_overflow_p = true;
14757 /* When both operands are nonzero, then MAX must be too. */
14758 if (tree_expr_nonzero_warnv_p (op1,
14759 strict_overflow_p))
14762 /* MAX where operand 0 is positive is positive. */
14763 return tree_expr_nonnegative_warnv_p (op0,
14764 strict_overflow_p);
14766 /* MAX where operand 1 is positive is positive. */
14767 else if (tree_expr_nonzero_warnv_p (op1,
14768 &sub_strict_overflow_p)
14769 && tree_expr_nonnegative_warnv_p (op1,
14770 &sub_strict_overflow_p))
14772 if (sub_strict_overflow_p)
14773 *strict_overflow_p = true;
14779 return (tree_expr_nonzero_warnv_p (op1,
14781 || tree_expr_nonzero_warnv_p (op0,
14782 strict_overflow_p));
14791 /* Return true when T is an address and is known to be nonzero.
14792 For floating point we further ensure that T is not denormal.
14793 Similar logic is present in nonzero_address in rtlanal.h.
14795 If the return value is based on the assumption that signed overflow
14796 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14797 change *STRICT_OVERFLOW_P. */
14800 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14802 bool sub_strict_overflow_p;
14803 switch (TREE_CODE (t))
14806 return !integer_zerop (t);
14810 tree base = get_base_address (TREE_OPERAND (t, 0));
14815 /* Weak declarations may link to NULL. Other things may also be NULL
14816 so protect with -fdelete-null-pointer-checks; but not variables
14817 allocated on the stack. */
14819 && (flag_delete_null_pointer_checks
14820 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
14821 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
14823 /* Constants are never weak. */
14824 if (CONSTANT_CLASS_P (base))
14831 sub_strict_overflow_p = false;
14832 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14833 &sub_strict_overflow_p)
14834 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14835 &sub_strict_overflow_p))
14837 if (sub_strict_overflow_p)
14838 *strict_overflow_p = true;
14849 /* Return true when T is an address and is known to be nonzero.
14850 For floating point we further ensure that T is not denormal.
14851 Similar logic is present in nonzero_address in rtlanal.h.
14853 If the return value is based on the assumption that signed overflow
14854 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14855 change *STRICT_OVERFLOW_P. */
14858 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14860 tree type = TREE_TYPE (t);
14861 enum tree_code code;
14863 /* Doing something useful for floating point would need more work. */
14864 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14867 code = TREE_CODE (t);
14868 switch (TREE_CODE_CLASS (code))
14871 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14872 strict_overflow_p);
14874 case tcc_comparison:
14875 return tree_binary_nonzero_warnv_p (code, type,
14876 TREE_OPERAND (t, 0),
14877 TREE_OPERAND (t, 1),
14878 strict_overflow_p);
14880 case tcc_declaration:
14881 case tcc_reference:
14882 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14890 case TRUTH_NOT_EXPR:
14891 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14892 strict_overflow_p);
14894 case TRUTH_AND_EXPR:
14895 case TRUTH_OR_EXPR:
14896 case TRUTH_XOR_EXPR:
14897 return tree_binary_nonzero_warnv_p (code, type,
14898 TREE_OPERAND (t, 0),
14899 TREE_OPERAND (t, 1),
14900 strict_overflow_p);
14907 case WITH_SIZE_EXPR:
14909 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14911 case COMPOUND_EXPR:
14914 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14915 strict_overflow_p);
14918 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14919 strict_overflow_p);
14922 return alloca_call_p (t);
14930 /* Return true when T is an address and is known to be nonzero.
14931 Handle warnings about undefined signed overflow. */
14934 tree_expr_nonzero_p (tree t)
14936 bool ret, strict_overflow_p;
14938 strict_overflow_p = false;
14939 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14940 if (strict_overflow_p)
14941 fold_overflow_warning (("assuming signed overflow does not occur when "
14942 "determining that expression is always "
14944 WARN_STRICT_OVERFLOW_MISC);
14948 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14949 attempt to fold the expression to a constant without modifying TYPE,
14952 If the expression could be simplified to a constant, then return
14953 the constant. If the expression would not be simplified to a
14954 constant, then return NULL_TREE. */
14957 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14959 tree tem = fold_binary (code, type, op0, op1);
14960 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14963 /* Given the components of a unary expression CODE, TYPE and OP0,
14964 attempt to fold the expression to a constant without modifying
14967 If the expression could be simplified to a constant, then return
14968 the constant. If the expression would not be simplified to a
14969 constant, then return NULL_TREE. */
14972 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14974 tree tem = fold_unary (code, type, op0);
14975 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14978 /* If EXP represents referencing an element in a constant string
14979 (either via pointer arithmetic or array indexing), return the
14980 tree representing the value accessed, otherwise return NULL. */
14983 fold_read_from_constant_string (tree exp)
14985 if ((TREE_CODE (exp) == INDIRECT_REF
14986 || TREE_CODE (exp) == ARRAY_REF)
14987 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14989 tree exp1 = TREE_OPERAND (exp, 0);
14992 location_t loc = EXPR_LOCATION (exp);
14994 if (TREE_CODE (exp) == INDIRECT_REF)
14995 string = string_constant (exp1, &index);
14998 tree low_bound = array_ref_low_bound (exp);
14999 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15001 /* Optimize the special-case of a zero lower bound.
15003 We convert the low_bound to sizetype to avoid some problems
15004 with constant folding. (E.g. suppose the lower bound is 1,
15005 and its mode is QI. Without the conversion,l (ARRAY
15006 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15007 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15008 if (! integer_zerop (low_bound))
15009 index = size_diffop_loc (loc, index,
15010 fold_convert_loc (loc, sizetype, low_bound));
15016 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15017 && TREE_CODE (string) == STRING_CST
15018 && TREE_CODE (index) == INTEGER_CST
15019 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15020 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15022 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15023 return build_int_cst_type (TREE_TYPE (exp),
15024 (TREE_STRING_POINTER (string)
15025 [TREE_INT_CST_LOW (index)]));
15030 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15031 an integer constant, real, or fixed-point constant.
15033 TYPE is the type of the result. */
15036 fold_negate_const (tree arg0, tree type)
15038 tree t = NULL_TREE;
15040 switch (TREE_CODE (arg0))
15044 unsigned HOST_WIDE_INT low;
15045 HOST_WIDE_INT high;
15046 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15047 TREE_INT_CST_HIGH (arg0),
15049 t = force_fit_type_double (type, low, high, 1,
15050 (overflow | TREE_OVERFLOW (arg0))
15051 && !TYPE_UNSIGNED (type));
15056 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15061 FIXED_VALUE_TYPE f;
15062 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15063 &(TREE_FIXED_CST (arg0)), NULL,
15064 TYPE_SATURATING (type));
15065 t = build_fixed (type, f);
15066 /* Propagate overflow flags. */
15067 if (overflow_p | TREE_OVERFLOW (arg0))
15068 TREE_OVERFLOW (t) = 1;
15073 gcc_unreachable ();
15079 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15080 an integer constant or real constant.
15082 TYPE is the type of the result. */
15085 fold_abs_const (tree arg0, tree type)
15087 tree t = NULL_TREE;
15089 switch (TREE_CODE (arg0))
15092 /* If the value is unsigned, then the absolute value is
15093 the same as the ordinary value. */
15094 if (TYPE_UNSIGNED (type))
15096 /* Similarly, if the value is non-negative. */
15097 else if (INT_CST_LT (integer_minus_one_node, arg0))
15099 /* If the value is negative, then the absolute value is
15103 unsigned HOST_WIDE_INT low;
15104 HOST_WIDE_INT high;
15105 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15106 TREE_INT_CST_HIGH (arg0),
15108 t = force_fit_type_double (type, low, high, -1,
15109 overflow | TREE_OVERFLOW (arg0));
15114 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15115 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15121 gcc_unreachable ();
15127 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15128 constant. TYPE is the type of the result. */
15131 fold_not_const (tree arg0, tree type)
15133 tree t = NULL_TREE;
15135 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15137 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15138 ~TREE_INT_CST_HIGH (arg0), 0,
15139 TREE_OVERFLOW (arg0));
15144 /* Given CODE, a relational operator, the target type, TYPE and two
15145 constant operands OP0 and OP1, return the result of the
15146 relational operation. If the result is not a compile time
15147 constant, then return NULL_TREE. */
15150 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15152 int result, invert;
15154 /* From here on, the only cases we handle are when the result is
15155 known to be a constant. */
15157 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15159 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15160 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15162 /* Handle the cases where either operand is a NaN. */
15163 if (real_isnan (c0) || real_isnan (c1))
15173 case UNORDERED_EXPR:
15187 if (flag_trapping_math)
15193 gcc_unreachable ();
15196 return constant_boolean_node (result, type);
15199 return constant_boolean_node (real_compare (code, c0, c1), type);
15202 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15204 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15205 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15206 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15209 /* Handle equality/inequality of complex constants. */
15210 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15212 tree rcond = fold_relational_const (code, type,
15213 TREE_REALPART (op0),
15214 TREE_REALPART (op1));
15215 tree icond = fold_relational_const (code, type,
15216 TREE_IMAGPART (op0),
15217 TREE_IMAGPART (op1));
15218 if (code == EQ_EXPR)
15219 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15220 else if (code == NE_EXPR)
15221 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15226 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15228 To compute GT, swap the arguments and do LT.
15229 To compute GE, do LT and invert the result.
15230 To compute LE, swap the arguments, do LT and invert the result.
15231 To compute NE, do EQ and invert the result.
15233 Therefore, the code below must handle only EQ and LT. */
15235 if (code == LE_EXPR || code == GT_EXPR)
15240 code = swap_tree_comparison (code);
15243 /* Note that it is safe to invert for real values here because we
15244 have already handled the one case that it matters. */
15247 if (code == NE_EXPR || code == GE_EXPR)
15250 code = invert_tree_comparison (code, false);
15253 /* Compute a result for LT or EQ if args permit;
15254 Otherwise return T. */
15255 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15257 if (code == EQ_EXPR)
15258 result = tree_int_cst_equal (op0, op1);
15259 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15260 result = INT_CST_LT_UNSIGNED (op0, op1);
15262 result = INT_CST_LT (op0, op1);
15269 return constant_boolean_node (result, type);
15272 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15273 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15277 fold_build_cleanup_point_expr (tree type, tree expr)
15279 /* If the expression does not have side effects then we don't have to wrap
15280 it with a cleanup point expression. */
15281 if (!TREE_SIDE_EFFECTS (expr))
15284 /* If the expression is a return, check to see if the expression inside the
15285 return has no side effects or the right hand side of the modify expression
15286 inside the return. If either don't have side effects set we don't need to
15287 wrap the expression in a cleanup point expression. Note we don't check the
15288 left hand side of the modify because it should always be a return decl. */
15289 if (TREE_CODE (expr) == RETURN_EXPR)
15291 tree op = TREE_OPERAND (expr, 0);
15292 if (!op || !TREE_SIDE_EFFECTS (op))
15294 op = TREE_OPERAND (op, 1);
15295 if (!TREE_SIDE_EFFECTS (op))
15299 return build1 (CLEANUP_POINT_EXPR, type, expr);
15302 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15303 of an indirection through OP0, or NULL_TREE if no simplification is
15307 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15313 subtype = TREE_TYPE (sub);
15314 if (!POINTER_TYPE_P (subtype))
15317 if (TREE_CODE (sub) == ADDR_EXPR)
15319 tree op = TREE_OPERAND (sub, 0);
15320 tree optype = TREE_TYPE (op);
15321 /* *&CONST_DECL -> to the value of the const decl. */
15322 if (TREE_CODE (op) == CONST_DECL)
15323 return DECL_INITIAL (op);
15324 /* *&p => p; make sure to handle *&"str"[cst] here. */
15325 if (type == optype)
15327 tree fop = fold_read_from_constant_string (op);
15333 /* *(foo *)&fooarray => fooarray[0] */
15334 else if (TREE_CODE (optype) == ARRAY_TYPE
15335 && type == TREE_TYPE (optype))
15337 tree type_domain = TYPE_DOMAIN (optype);
15338 tree min_val = size_zero_node;
15339 if (type_domain && TYPE_MIN_VALUE (type_domain))
15340 min_val = TYPE_MIN_VALUE (type_domain);
15341 op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15342 SET_EXPR_LOCATION (op0, loc);
15345 /* *(foo *)&complexfoo => __real__ complexfoo */
15346 else if (TREE_CODE (optype) == COMPLEX_TYPE
15347 && type == TREE_TYPE (optype))
15348 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15349 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15350 else if (TREE_CODE (optype) == VECTOR_TYPE
15351 && type == TREE_TYPE (optype))
15353 tree part_width = TYPE_SIZE (type);
15354 tree index = bitsize_int (0);
15355 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15359 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15360 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15361 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15363 tree op00 = TREE_OPERAND (sub, 0);
15364 tree op01 = TREE_OPERAND (sub, 1);
15368 op00type = TREE_TYPE (op00);
15369 if (TREE_CODE (op00) == ADDR_EXPR
15370 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15371 && type == TREE_TYPE (TREE_TYPE (op00type)))
15373 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15374 tree part_width = TYPE_SIZE (type);
15375 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15376 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15377 tree index = bitsize_int (indexi);
15379 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15380 return fold_build3_loc (loc,
15381 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15382 part_width, index);
15388 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15389 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15390 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15392 tree op00 = TREE_OPERAND (sub, 0);
15393 tree op01 = TREE_OPERAND (sub, 1);
15397 op00type = TREE_TYPE (op00);
15398 if (TREE_CODE (op00) == ADDR_EXPR
15399 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15400 && type == TREE_TYPE (TREE_TYPE (op00type)))
15402 tree size = TYPE_SIZE_UNIT (type);
15403 if (tree_int_cst_equal (size, op01))
15404 return fold_build1_loc (loc, IMAGPART_EXPR, type,
15405 TREE_OPERAND (op00, 0));
15409 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15410 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15411 && type == TREE_TYPE (TREE_TYPE (subtype)))
15414 tree min_val = size_zero_node;
15415 sub = build_fold_indirect_ref_loc (loc, sub);
15416 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15417 if (type_domain && TYPE_MIN_VALUE (type_domain))
15418 min_val = TYPE_MIN_VALUE (type_domain);
15419 op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15420 SET_EXPR_LOCATION (op0, loc);
15427 /* Builds an expression for an indirection through T, simplifying some
15431 build_fold_indirect_ref_loc (location_t loc, tree t)
15433 tree type = TREE_TYPE (TREE_TYPE (t));
15434 tree sub = fold_indirect_ref_1 (loc, type, t);
15439 t = build1 (INDIRECT_REF, type, t);
15440 SET_EXPR_LOCATION (t, loc);
15444 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15447 fold_indirect_ref_loc (location_t loc, tree t)
15449 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15457 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15458 whose result is ignored. The type of the returned tree need not be
15459 the same as the original expression. */
15462 fold_ignored_result (tree t)
15464 if (!TREE_SIDE_EFFECTS (t))
15465 return integer_zero_node;
15468 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15471 t = TREE_OPERAND (t, 0);
15475 case tcc_comparison:
15476 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15477 t = TREE_OPERAND (t, 0);
15478 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15479 t = TREE_OPERAND (t, 1);
15484 case tcc_expression:
15485 switch (TREE_CODE (t))
15487 case COMPOUND_EXPR:
15488 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15490 t = TREE_OPERAND (t, 0);
15494 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15495 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15497 t = TREE_OPERAND (t, 0);
15510 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15511 This can only be applied to objects of a sizetype. */
15514 round_up_loc (location_t loc, tree value, int divisor)
15516 tree div = NULL_TREE;
15518 gcc_assert (divisor > 0);
15522 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15523 have to do anything. Only do this when we are not given a const,
15524 because in that case, this check is more expensive than just
15526 if (TREE_CODE (value) != INTEGER_CST)
15528 div = build_int_cst (TREE_TYPE (value), divisor);
15530 if (multiple_of_p (TREE_TYPE (value), value, div))
15534 /* If divisor is a power of two, simplify this to bit manipulation. */
15535 if (divisor == (divisor & -divisor))
15537 if (TREE_CODE (value) == INTEGER_CST)
15539 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15540 unsigned HOST_WIDE_INT high;
15543 if ((low & (divisor - 1)) == 0)
15546 overflow_p = TREE_OVERFLOW (value);
15547 high = TREE_INT_CST_HIGH (value);
15548 low &= ~(divisor - 1);
15557 return force_fit_type_double (TREE_TYPE (value), low, high,
15564 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15565 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15566 t = build_int_cst (TREE_TYPE (value), -divisor);
15567 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15573 div = build_int_cst (TREE_TYPE (value), divisor);
15574 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15575 value = size_binop_loc (loc, MULT_EXPR, value, div);
15581 /* Likewise, but round down. */
15584 round_down_loc (location_t loc, tree value, int divisor)
15586 tree div = NULL_TREE;
15588 gcc_assert (divisor > 0);
15592 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15593 have to do anything. Only do this when we are not given a const,
15594 because in that case, this check is more expensive than just
15596 if (TREE_CODE (value) != INTEGER_CST)
15598 div = build_int_cst (TREE_TYPE (value), divisor);
15600 if (multiple_of_p (TREE_TYPE (value), value, div))
15604 /* If divisor is a power of two, simplify this to bit manipulation. */
15605 if (divisor == (divisor & -divisor))
15609 t = build_int_cst (TREE_TYPE (value), -divisor);
15610 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15615 div = build_int_cst (TREE_TYPE (value), divisor);
15616 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15617 value = size_binop_loc (loc, MULT_EXPR, value, div);
15623 /* Returns the pointer to the base of the object addressed by EXP and
15624 extracts the information about the offset of the access, storing it
15625 to PBITPOS and POFFSET. */
15628 split_address_to_core_and_offset (tree exp,
15629 HOST_WIDE_INT *pbitpos, tree *poffset)
15632 enum machine_mode mode;
15633 int unsignedp, volatilep;
15634 HOST_WIDE_INT bitsize;
15635 location_t loc = EXPR_LOCATION (exp);
15637 if (TREE_CODE (exp) == ADDR_EXPR)
15639 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15640 poffset, &mode, &unsignedp, &volatilep,
15642 core = build_fold_addr_expr_loc (loc, core);
15648 *poffset = NULL_TREE;
15654 /* Returns true if addresses of E1 and E2 differ by a constant, false
15655 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15658 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15661 HOST_WIDE_INT bitpos1, bitpos2;
15662 tree toffset1, toffset2, tdiff, type;
15664 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15665 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15667 if (bitpos1 % BITS_PER_UNIT != 0
15668 || bitpos2 % BITS_PER_UNIT != 0
15669 || !operand_equal_p (core1, core2, 0))
15672 if (toffset1 && toffset2)
15674 type = TREE_TYPE (toffset1);
15675 if (type != TREE_TYPE (toffset2))
15676 toffset2 = fold_convert (type, toffset2);
15678 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15679 if (!cst_and_fits_in_hwi (tdiff))
15682 *diff = int_cst_value (tdiff);
15684 else if (toffset1 || toffset2)
15686 /* If only one of the offsets is non-constant, the difference cannot
15693 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15697 /* Simplify the floating point expression EXP when the sign of the
15698 result is not significant. Return NULL_TREE if no simplification
15702 fold_strip_sign_ops (tree exp)
15705 location_t loc = EXPR_LOCATION (exp);
15707 switch (TREE_CODE (exp))
15711 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15712 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15716 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15718 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15719 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15720 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15721 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15722 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15723 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15726 case COMPOUND_EXPR:
15727 arg0 = TREE_OPERAND (exp, 0);
15728 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15730 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15734 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15735 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15737 return fold_build3_loc (loc,
15738 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15739 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15740 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15745 const enum built_in_function fcode = builtin_mathfn_code (exp);
15748 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15749 /* Strip copysign function call, return the 1st argument. */
15750 arg0 = CALL_EXPR_ARG (exp, 0);
15751 arg1 = CALL_EXPR_ARG (exp, 1);
15752 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
15755 /* Strip sign ops from the argument of "odd" math functions. */
15756 if (negate_mathfn_p (fcode))
15758 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15760 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);