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 and size_binop.
32 fold takes a tree as argument and returns a simplified tree.
34 size_binop takes a tree code for an arithmetic operation
35 and two operands that are trees, and produces a tree for the
36 result, assuming the type comes from `sizetype'.
38 size_int takes an integer value, and creates a tree constant
39 with type from `sizetype'.
41 Note: Since the folders get called on non-gimple code as well as
42 gimple code, we need to handle GIMPLE tuples as well as their
43 corresponding tree equivalents. */
47 #include "coretypes.h"
56 #include "diagnostic-core.h"
61 #include "langhooks.h"
64 #include "tree-flow.h"
66 /* Nonzero if we are folding constants inside an initializer; zero
68 int folding_initializer = 0;
70 /* The following constants represent a bit based encoding of GCC's
71 comparison operators. This encoding simplifies transformations
72 on relational comparison operators, such as AND and OR. */
73 enum comparison_code {
92 static bool negate_mathfn_p (enum built_in_function);
93 static bool negate_expr_p (tree);
94 static tree negate_expr (tree);
95 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
96 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
97 static tree const_binop (enum tree_code, tree, tree);
98 static enum comparison_code comparison_to_compcode (enum tree_code);
99 static enum tree_code compcode_to_comparison (enum comparison_code);
100 static int operand_equal_for_comparison_p (tree, tree, tree);
101 static int twoval_comparison_p (tree, tree *, tree *, int *);
102 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
103 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
104 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
105 static tree make_bit_field_ref (location_t, tree, tree,
106 HOST_WIDE_INT, HOST_WIDE_INT, int);
107 static tree optimize_bit_field_compare (location_t, enum tree_code,
109 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
111 enum machine_mode *, int *, int *,
113 static int all_ones_mask_p (const_tree, int);
114 static tree sign_bit_p (tree, const_tree);
115 static int simple_operand_p (const_tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 extern tree make_range (tree, int *, tree *, tree *, bool *);
120 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
122 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
123 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
124 static tree unextend (tree, int, int, tree);
125 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
126 static tree optimize_minmax_comparison (location_t, enum tree_code,
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131 enum tree_code, tree,
134 static tree fold_mathfn_compare (location_t,
135 enum built_in_function, enum tree_code,
137 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
138 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (const_tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143 static tree fold_convert_const (enum tree_code, tree, tree);
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
148 and SUM1. Then this yields nonzero if overflow occurred during the
151 Overflow occurs if A and B have the same sign, but A and SUM differ in
152 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
156 /* If ARG2 divides ARG1 with zero remainder, carries out the division
157 of type CODE and returns the quotient.
158 Otherwise returns NULL_TREE. */
161 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
166 /* The sign of the division is according to operand two, that
167 does the correct thing for POINTER_PLUS_EXPR where we want
168 a signed division. */
169 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
170 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
171 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
174 quo = double_int_divmod (tree_to_double_int (arg1),
175 tree_to_double_int (arg2),
178 if (double_int_zero_p (rem))
179 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
184 /* This is nonzero if we should defer warnings about undefined
185 overflow. This facility exists because these warnings are a
186 special case. The code to estimate loop iterations does not want
187 to issue any warnings, since it works with expressions which do not
188 occur in user code. Various bits of cleanup code call fold(), but
189 only use the result if it has certain characteristics (e.g., is a
190 constant); that code only wants to issue a warning if the result is
193 static int fold_deferring_overflow_warnings;
195 /* If a warning about undefined overflow is deferred, this is the
196 warning. Note that this may cause us to turn two warnings into
197 one, but that is fine since it is sufficient to only give one
198 warning per expression. */
200 static const char* fold_deferred_overflow_warning;
202 /* If a warning about undefined overflow is deferred, this is the
203 level at which the warning should be emitted. */
205 static enum warn_strict_overflow_code fold_deferred_overflow_code;
207 /* Start deferring overflow warnings. We could use a stack here to
208 permit nested calls, but at present it is not necessary. */
211 fold_defer_overflow_warnings (void)
213 ++fold_deferring_overflow_warnings;
216 /* Stop deferring overflow warnings. If there is a pending warning,
217 and ISSUE is true, then issue the warning if appropriate. STMT is
218 the statement with which the warning should be associated (used for
219 location information); STMT may be NULL. CODE is the level of the
220 warning--a warn_strict_overflow_code value. This function will use
221 the smaller of CODE and the deferred code when deciding whether to
222 issue the warning. CODE may be zero to mean to always use the
226 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
231 gcc_assert (fold_deferring_overflow_warnings > 0);
232 --fold_deferring_overflow_warnings;
233 if (fold_deferring_overflow_warnings > 0)
235 if (fold_deferred_overflow_warning != NULL
237 && code < (int) fold_deferred_overflow_code)
238 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
242 warnmsg = fold_deferred_overflow_warning;
243 fold_deferred_overflow_warning = NULL;
245 if (!issue || warnmsg == NULL)
248 if (gimple_no_warning_p (stmt))
251 /* Use the smallest code level when deciding to issue the
253 if (code == 0 || code > (int) fold_deferred_overflow_code)
254 code = fold_deferred_overflow_code;
256 if (!issue_strict_overflow_warning (code))
260 locus = input_location;
262 locus = gimple_location (stmt);
263 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
266 /* Stop deferring overflow warnings, ignoring any deferred
270 fold_undefer_and_ignore_overflow_warnings (void)
272 fold_undefer_overflow_warnings (false, NULL, 0);
275 /* Whether we are deferring overflow warnings. */
278 fold_deferring_overflow_warnings_p (void)
280 return fold_deferring_overflow_warnings > 0;
283 /* This is called when we fold something based on the fact that signed
284 overflow is undefined. */
287 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
289 if (fold_deferring_overflow_warnings > 0)
291 if (fold_deferred_overflow_warning == NULL
292 || wc < fold_deferred_overflow_code)
294 fold_deferred_overflow_warning = gmsgid;
295 fold_deferred_overflow_code = wc;
298 else if (issue_strict_overflow_warning (wc))
299 warning (OPT_Wstrict_overflow, gmsgid);
302 /* Return true if the built-in mathematical function specified by CODE
303 is odd, i.e. -f(x) == f(-x). */
306 negate_mathfn_p (enum built_in_function code)
310 CASE_FLT_FN (BUILT_IN_ASIN):
311 CASE_FLT_FN (BUILT_IN_ASINH):
312 CASE_FLT_FN (BUILT_IN_ATAN):
313 CASE_FLT_FN (BUILT_IN_ATANH):
314 CASE_FLT_FN (BUILT_IN_CASIN):
315 CASE_FLT_FN (BUILT_IN_CASINH):
316 CASE_FLT_FN (BUILT_IN_CATAN):
317 CASE_FLT_FN (BUILT_IN_CATANH):
318 CASE_FLT_FN (BUILT_IN_CBRT):
319 CASE_FLT_FN (BUILT_IN_CPROJ):
320 CASE_FLT_FN (BUILT_IN_CSIN):
321 CASE_FLT_FN (BUILT_IN_CSINH):
322 CASE_FLT_FN (BUILT_IN_CTAN):
323 CASE_FLT_FN (BUILT_IN_CTANH):
324 CASE_FLT_FN (BUILT_IN_ERF):
325 CASE_FLT_FN (BUILT_IN_LLROUND):
326 CASE_FLT_FN (BUILT_IN_LROUND):
327 CASE_FLT_FN (BUILT_IN_ROUND):
328 CASE_FLT_FN (BUILT_IN_SIN):
329 CASE_FLT_FN (BUILT_IN_SINH):
330 CASE_FLT_FN (BUILT_IN_TAN):
331 CASE_FLT_FN (BUILT_IN_TANH):
332 CASE_FLT_FN (BUILT_IN_TRUNC):
335 CASE_FLT_FN (BUILT_IN_LLRINT):
336 CASE_FLT_FN (BUILT_IN_LRINT):
337 CASE_FLT_FN (BUILT_IN_NEARBYINT):
338 CASE_FLT_FN (BUILT_IN_RINT):
339 return !flag_rounding_math;
347 /* Check whether we may negate an integer constant T without causing
351 may_negate_without_overflow_p (const_tree t)
353 unsigned HOST_WIDE_INT val;
357 gcc_assert (TREE_CODE (t) == INTEGER_CST);
359 type = TREE_TYPE (t);
360 if (TYPE_UNSIGNED (type))
363 prec = TYPE_PRECISION (type);
364 if (prec > HOST_BITS_PER_WIDE_INT)
366 if (TREE_INT_CST_LOW (t) != 0)
368 prec -= HOST_BITS_PER_WIDE_INT;
369 val = TREE_INT_CST_HIGH (t);
372 val = TREE_INT_CST_LOW (t);
373 if (prec < HOST_BITS_PER_WIDE_INT)
374 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
375 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
378 /* Determine whether an expression T can be cheaply negated using
379 the function negate_expr without introducing undefined overflow. */
382 negate_expr_p (tree t)
389 type = TREE_TYPE (t);
392 switch (TREE_CODE (t))
395 if (TYPE_OVERFLOW_WRAPS (type))
398 /* Check that -CST will not overflow type. */
399 return may_negate_without_overflow_p (t);
401 return (INTEGRAL_TYPE_P (type)
402 && TYPE_OVERFLOW_WRAPS (type));
409 /* We want to canonicalize to positive real constants. Pretend
410 that only negative ones can be easily negated. */
411 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
414 return negate_expr_p (TREE_REALPART (t))
415 && negate_expr_p (TREE_IMAGPART (t));
418 return negate_expr_p (TREE_OPERAND (t, 0))
419 && negate_expr_p (TREE_OPERAND (t, 1));
422 return negate_expr_p (TREE_OPERAND (t, 0));
425 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
426 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
428 /* -(A + B) -> (-B) - A. */
429 if (negate_expr_p (TREE_OPERAND (t, 1))
430 && reorder_operands_p (TREE_OPERAND (t, 0),
431 TREE_OPERAND (t, 1)))
433 /* -(A + B) -> (-A) - B. */
434 return negate_expr_p (TREE_OPERAND (t, 0));
437 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
438 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
439 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
440 && reorder_operands_p (TREE_OPERAND (t, 0),
441 TREE_OPERAND (t, 1));
444 if (TYPE_UNSIGNED (TREE_TYPE (t)))
450 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
451 return negate_expr_p (TREE_OPERAND (t, 1))
452 || negate_expr_p (TREE_OPERAND (t, 0));
460 /* In general we can't negate A / B, because if A is INT_MIN and
461 B is 1, we may turn this into INT_MIN / -1 which is undefined
462 and actually traps on some architectures. But if overflow is
463 undefined, we can negate, because - (INT_MIN / 1) is an
465 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
466 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
472 /* Negate -((double)float) as (double)(-float). */
473 if (TREE_CODE (type) == REAL_TYPE)
475 tree tem = strip_float_extensions (t);
477 return negate_expr_p (tem);
482 /* Negate -f(x) as f(-x). */
483 if (negate_mathfn_p (builtin_mathfn_code (t)))
484 return negate_expr_p (CALL_EXPR_ARG (t, 0));
488 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
489 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
491 tree op1 = TREE_OPERAND (t, 1);
492 if (TREE_INT_CST_HIGH (op1) == 0
493 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
494 == TREE_INT_CST_LOW (op1))
505 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
506 simplification is possible.
507 If negate_expr_p would return true for T, NULL_TREE will never be
511 fold_negate_expr (location_t loc, tree t)
513 tree type = TREE_TYPE (t);
516 switch (TREE_CODE (t))
518 /* Convert - (~A) to A + 1. */
520 if (INTEGRAL_TYPE_P (type))
521 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
522 build_int_cst (type, 1));
526 tem = fold_negate_const (t, type);
527 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
528 || !TYPE_OVERFLOW_TRAPS (type))
533 tem = fold_negate_const (t, type);
534 /* Two's complement FP formats, such as c4x, may overflow. */
535 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
540 tem = fold_negate_const (t, type);
545 tree rpart = negate_expr (TREE_REALPART (t));
546 tree ipart = negate_expr (TREE_IMAGPART (t));
548 if ((TREE_CODE (rpart) == REAL_CST
549 && TREE_CODE (ipart) == REAL_CST)
550 || (TREE_CODE (rpart) == INTEGER_CST
551 && TREE_CODE (ipart) == INTEGER_CST))
552 return build_complex (type, rpart, ipart);
557 if (negate_expr_p (t))
558 return fold_build2_loc (loc, COMPLEX_EXPR, type,
559 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
560 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
564 if (negate_expr_p (t))
565 return fold_build1_loc (loc, CONJ_EXPR, type,
566 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
570 return TREE_OPERAND (t, 0);
573 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
574 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
576 /* -(A + B) -> (-B) - A. */
577 if (negate_expr_p (TREE_OPERAND (t, 1))
578 && reorder_operands_p (TREE_OPERAND (t, 0),
579 TREE_OPERAND (t, 1)))
581 tem = negate_expr (TREE_OPERAND (t, 1));
582 return fold_build2_loc (loc, MINUS_EXPR, type,
583 tem, TREE_OPERAND (t, 0));
586 /* -(A + B) -> (-A) - B. */
587 if (negate_expr_p (TREE_OPERAND (t, 0)))
589 tem = negate_expr (TREE_OPERAND (t, 0));
590 return fold_build2_loc (loc, MINUS_EXPR, type,
591 tem, TREE_OPERAND (t, 1));
597 /* - (A - B) -> B - A */
598 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
599 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
600 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
601 return fold_build2_loc (loc, MINUS_EXPR, type,
602 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
606 if (TYPE_UNSIGNED (type))
612 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
614 tem = TREE_OPERAND (t, 1);
615 if (negate_expr_p (tem))
616 return fold_build2_loc (loc, TREE_CODE (t), type,
617 TREE_OPERAND (t, 0), negate_expr (tem));
618 tem = TREE_OPERAND (t, 0);
619 if (negate_expr_p (tem))
620 return fold_build2_loc (loc, TREE_CODE (t), type,
621 negate_expr (tem), TREE_OPERAND (t, 1));
630 /* In general we can't negate A / B, because if A is INT_MIN and
631 B is 1, we may turn this into INT_MIN / -1 which is undefined
632 and actually traps on some architectures. But if overflow is
633 undefined, we can negate, because - (INT_MIN / 1) is an
635 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
637 const char * const warnmsg = G_("assuming signed overflow does not "
638 "occur when negating a division");
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
642 if (INTEGRAL_TYPE_P (type)
643 && (TREE_CODE (tem) != INTEGER_CST
644 || integer_onep (tem)))
645 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
646 return fold_build2_loc (loc, TREE_CODE (t), type,
647 TREE_OPERAND (t, 0), negate_expr (tem));
649 tem = TREE_OPERAND (t, 0);
650 if (negate_expr_p (tem))
652 if (INTEGRAL_TYPE_P (type)
653 && (TREE_CODE (tem) != INTEGER_CST
654 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
655 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (tem), TREE_OPERAND (t, 1));
663 /* Convert -((double)float) into (double)(-float). */
664 if (TREE_CODE (type) == REAL_TYPE)
666 tem = strip_float_extensions (t);
667 if (tem != t && negate_expr_p (tem))
668 return fold_convert_loc (loc, type, negate_expr (tem));
673 /* Negate -f(x) as f(-x). */
674 if (negate_mathfn_p (builtin_mathfn_code (t))
675 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
679 fndecl = get_callee_fndecl (t);
680 arg = negate_expr (CALL_EXPR_ARG (t, 0));
681 return build_call_expr_loc (loc, fndecl, 1, arg);
686 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
687 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
689 tree op1 = TREE_OPERAND (t, 1);
690 if (TREE_INT_CST_HIGH (op1) == 0
691 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
692 == TREE_INT_CST_LOW (op1))
694 tree ntype = TYPE_UNSIGNED (type)
695 ? signed_type_for (type)
696 : unsigned_type_for (type);
697 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
698 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
699 return fold_convert_loc (loc, type, temp);
711 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
712 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
724 loc = EXPR_LOCATION (t);
725 type = TREE_TYPE (t);
728 tem = fold_negate_expr (loc, t);
731 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
732 SET_EXPR_LOCATION (tem, loc);
734 return fold_convert_loc (loc, type, tem);
737 /* Split a tree IN into a constant, literal and variable parts that could be
738 combined with CODE to make IN. "constant" means an expression with
739 TREE_CONSTANT but that isn't an actual constant. CODE must be a
740 commutative arithmetic operation. Store the constant part into *CONP,
741 the literal in *LITP and return the variable part. If a part isn't
742 present, set it to null. If the tree does not decompose in this way,
743 return the entire tree as the variable part and the other parts as null.
745 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
746 case, we negate an operand that was subtracted. Except if it is a
747 literal for which we use *MINUS_LITP instead.
749 If NEGATE_P is true, we are negating all of IN, again except a literal
750 for which we use *MINUS_LITP instead.
752 If IN is itself a literal or constant, return it as appropriate.
754 Note that we do not guarantee that any of the three values will be the
755 same type as IN, but they will have the same signedness and mode. */
758 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
759 tree *minus_litp, int negate_p)
767 /* Strip any conversions that don't change the machine mode or signedness. */
768 STRIP_SIGN_NOPS (in);
770 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
771 || TREE_CODE (in) == FIXED_CST)
773 else if (TREE_CODE (in) == code
774 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
775 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
776 /* We can associate addition and subtraction together (even
777 though the C standard doesn't say so) for integers because
778 the value is not affected. For reals, the value might be
779 affected, so we can't. */
780 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
781 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
783 tree op0 = TREE_OPERAND (in, 0);
784 tree op1 = TREE_OPERAND (in, 1);
785 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
786 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
788 /* First see if either of the operands is a literal, then a constant. */
789 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
790 || TREE_CODE (op0) == FIXED_CST)
791 *litp = op0, op0 = 0;
792 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
793 || TREE_CODE (op1) == FIXED_CST)
794 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
796 if (op0 != 0 && TREE_CONSTANT (op0))
797 *conp = op0, op0 = 0;
798 else if (op1 != 0 && TREE_CONSTANT (op1))
799 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
801 /* If we haven't dealt with either operand, this is not a case we can
802 decompose. Otherwise, VAR is either of the ones remaining, if any. */
803 if (op0 != 0 && op1 != 0)
808 var = op1, neg_var_p = neg1_p;
810 /* Now do any needed negations. */
812 *minus_litp = *litp, *litp = 0;
814 *conp = negate_expr (*conp);
816 var = negate_expr (var);
818 else if (TREE_CONSTANT (in))
826 *minus_litp = *litp, *litp = 0;
827 else if (*minus_litp)
828 *litp = *minus_litp, *minus_litp = 0;
829 *conp = negate_expr (*conp);
830 var = negate_expr (var);
836 /* Re-associate trees split by the above function. T1 and T2 are
837 either expressions to associate or null. Return the new
838 expression, if any. LOC is the location of the new expression. If
839 we build an operation, do it in TYPE and with CODE. */
842 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
851 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
852 try to fold this since we will have infinite recursion. But do
853 deal with any NEGATE_EXPRs. */
854 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
855 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
857 if (code == PLUS_EXPR)
859 if (TREE_CODE (t1) == NEGATE_EXPR)
860 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
861 fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
862 else if (TREE_CODE (t2) == NEGATE_EXPR)
863 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
864 fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
865 else if (integer_zerop (t2))
866 return fold_convert_loc (loc, type, t1);
868 else if (code == MINUS_EXPR)
870 if (integer_zerop (t2))
871 return fold_convert_loc (loc, type, t1);
874 tem = build2 (code, type, fold_convert_loc (loc, type, t1),
875 fold_convert_loc (loc, type, t2));
876 goto associate_trees_exit;
879 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
880 fold_convert_loc (loc, type, t2));
881 associate_trees_exit:
882 protected_set_expr_location (tem, loc);
886 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
887 for use in int_const_binop, size_binop and size_diffop. */
890 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
892 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
894 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
909 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
910 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
911 && TYPE_MODE (type1) == TYPE_MODE (type2);
915 /* Combine two integer constants ARG1 and ARG2 under operation CODE
916 to produce a new constant. Return NULL_TREE if we don't know how
917 to evaluate CODE at compile-time.
919 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
922 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
924 double_int op1, op2, res, tmp;
926 tree type = TREE_TYPE (arg1);
927 bool uns = TYPE_UNSIGNED (type);
929 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
930 bool overflow = false;
932 op1 = tree_to_double_int (arg1);
933 op2 = tree_to_double_int (arg2);
938 res = double_int_ior (op1, op2);
942 res = double_int_xor (op1, op2);
946 res = double_int_and (op1, op2);
950 res = double_int_rshift (op1, double_int_to_shwi (op2),
951 TYPE_PRECISION (type), !uns);
955 /* It's unclear from the C standard whether shifts can overflow.
956 The following code ignores overflow; perhaps a C standard
957 interpretation ruling is needed. */
958 res = double_int_lshift (op1, double_int_to_shwi (op2),
959 TYPE_PRECISION (type), !uns);
963 res = double_int_rrotate (op1, double_int_to_shwi (op2),
964 TYPE_PRECISION (type));
968 res = double_int_lrotate (op1, double_int_to_shwi (op2),
969 TYPE_PRECISION (type));
973 overflow = add_double (op1.low, op1.high, op2.low, op2.high,
974 &res.low, &res.high);
978 neg_double (op2.low, op2.high, &res.low, &res.high);
979 add_double (op1.low, op1.high, res.low, res.high,
980 &res.low, &res.high);
981 overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
985 overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
986 &res.low, &res.high);
990 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
992 /* This is a shortcut for a common special case. */
993 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
994 && !TREE_OVERFLOW (arg1)
995 && !TREE_OVERFLOW (arg2)
996 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
998 if (code == CEIL_DIV_EXPR)
999 op1.low += op2.low - 1;
1001 res.low = op1.low / op2.low, res.high = 0;
1005 /* ... fall through ... */
1007 case ROUND_DIV_EXPR:
1008 if (double_int_zero_p (op2))
1010 if (double_int_one_p (op2))
1015 if (double_int_equal_p (op1, op2)
1016 && ! double_int_zero_p (op1))
1018 res = double_int_one;
1021 overflow = div_and_round_double (code, uns,
1022 op1.low, op1.high, op2.low, op2.high,
1023 &res.low, &res.high,
1024 &tmp.low, &tmp.high);
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 (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1031 && !TREE_OVERFLOW (arg1)
1032 && !TREE_OVERFLOW (arg2)
1033 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1035 if (code == CEIL_MOD_EXPR)
1036 op1.low += op2.low - 1;
1037 res.low = op1.low % op2.low, res.high = 0;
1041 /* ... fall through ... */
1043 case ROUND_MOD_EXPR:
1044 if (double_int_zero_p (op2))
1046 overflow = div_and_round_double (code, uns,
1047 op1.low, op1.high, op2.low, op2.high,
1048 &tmp.low, &tmp.high,
1049 &res.low, &res.high);
1053 res = double_int_min (op1, op2, uns);
1057 res = double_int_max (op1, op2, uns);
1066 t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1068 /* Propagate overflow flags ourselves. */
1069 if (((!uns || is_sizetype) && overflow)
1070 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1073 TREE_OVERFLOW (t) = 1;
1077 t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1078 ((!uns || is_sizetype) && overflow)
1079 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1084 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1085 constant. We assume ARG1 and ARG2 have the same data type, or at least
1086 are the same kind of constant and the same machine mode. Return zero if
1087 combining the constants is not allowed in the current operating mode. */
1090 const_binop (enum tree_code code, tree arg1, tree arg2)
1092 /* Sanity check for the recursive cases. */
1099 if (TREE_CODE (arg1) == INTEGER_CST)
1100 return int_const_binop (code, arg1, arg2, 0);
1102 if (TREE_CODE (arg1) == REAL_CST)
1104 enum machine_mode mode;
1107 REAL_VALUE_TYPE value;
1108 REAL_VALUE_TYPE result;
1112 /* The following codes are handled by real_arithmetic. */
1127 d1 = TREE_REAL_CST (arg1);
1128 d2 = TREE_REAL_CST (arg2);
1130 type = TREE_TYPE (arg1);
1131 mode = TYPE_MODE (type);
1133 /* Don't perform operation if we honor signaling NaNs and
1134 either operand is a NaN. */
1135 if (HONOR_SNANS (mode)
1136 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1139 /* Don't perform operation if it would raise a division
1140 by zero exception. */
1141 if (code == RDIV_EXPR
1142 && REAL_VALUES_EQUAL (d2, dconst0)
1143 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1146 /* If either operand is a NaN, just return it. Otherwise, set up
1147 for floating-point trap; we return an overflow. */
1148 if (REAL_VALUE_ISNAN (d1))
1150 else if (REAL_VALUE_ISNAN (d2))
1153 inexact = real_arithmetic (&value, code, &d1, &d2);
1154 real_convert (&result, mode, &value);
1156 /* Don't constant fold this floating point operation if
1157 the result has overflowed and flag_trapping_math. */
1158 if (flag_trapping_math
1159 && MODE_HAS_INFINITIES (mode)
1160 && REAL_VALUE_ISINF (result)
1161 && !REAL_VALUE_ISINF (d1)
1162 && !REAL_VALUE_ISINF (d2))
1165 /* Don't constant fold this floating point operation if the
1166 result may dependent upon the run-time rounding mode and
1167 flag_rounding_math is set, or if GCC's software emulation
1168 is unable to accurately represent the result. */
1169 if ((flag_rounding_math
1170 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1171 && (inexact || !real_identical (&result, &value)))
1174 t = build_real (type, result);
1176 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1180 if (TREE_CODE (arg1) == FIXED_CST)
1182 FIXED_VALUE_TYPE f1;
1183 FIXED_VALUE_TYPE f2;
1184 FIXED_VALUE_TYPE result;
1189 /* The following codes are handled by fixed_arithmetic. */
1195 case TRUNC_DIV_EXPR:
1196 f2 = TREE_FIXED_CST (arg2);
1201 f2.data.high = TREE_INT_CST_HIGH (arg2);
1202 f2.data.low = TREE_INT_CST_LOW (arg2);
1210 f1 = TREE_FIXED_CST (arg1);
1211 type = TREE_TYPE (arg1);
1212 sat_p = TYPE_SATURATING (type);
1213 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1214 t = build_fixed (type, result);
1215 /* Propagate overflow flags. */
1216 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1217 TREE_OVERFLOW (t) = 1;
1221 if (TREE_CODE (arg1) == COMPLEX_CST)
1223 tree type = TREE_TYPE (arg1);
1224 tree r1 = TREE_REALPART (arg1);
1225 tree i1 = TREE_IMAGPART (arg1);
1226 tree r2 = TREE_REALPART (arg2);
1227 tree i2 = TREE_IMAGPART (arg2);
1234 real = const_binop (code, r1, r2);
1235 imag = const_binop (code, i1, i2);
1239 if (COMPLEX_FLOAT_TYPE_P (type))
1240 return do_mpc_arg2 (arg1, arg2, type,
1241 /* do_nonfinite= */ folding_initializer,
1244 real = const_binop (MINUS_EXPR,
1245 const_binop (MULT_EXPR, r1, r2),
1246 const_binop (MULT_EXPR, i1, i2));
1247 imag = const_binop (PLUS_EXPR,
1248 const_binop (MULT_EXPR, r1, i2),
1249 const_binop (MULT_EXPR, i1, r2));
1253 if (COMPLEX_FLOAT_TYPE_P (type))
1254 return do_mpc_arg2 (arg1, arg2, type,
1255 /* do_nonfinite= */ folding_initializer,
1258 case TRUNC_DIV_EXPR:
1260 case FLOOR_DIV_EXPR:
1261 case ROUND_DIV_EXPR:
1262 if (flag_complex_method == 0)
1264 /* Keep this algorithm in sync with
1265 tree-complex.c:expand_complex_div_straight().
1267 Expand complex division to scalars, straightforward algorithm.
1268 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1272 = const_binop (PLUS_EXPR,
1273 const_binop (MULT_EXPR, r2, r2),
1274 const_binop (MULT_EXPR, i2, i2));
1276 = const_binop (PLUS_EXPR,
1277 const_binop (MULT_EXPR, r1, r2),
1278 const_binop (MULT_EXPR, i1, i2));
1280 = const_binop (MINUS_EXPR,
1281 const_binop (MULT_EXPR, i1, r2),
1282 const_binop (MULT_EXPR, r1, i2));
1284 real = const_binop (code, t1, magsquared);
1285 imag = const_binop (code, t2, magsquared);
1289 /* Keep this algorithm in sync with
1290 tree-complex.c:expand_complex_div_wide().
1292 Expand complex division to scalars, modified algorithm to minimize
1293 overflow with wide input ranges. */
1294 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1295 fold_abs_const (r2, TREE_TYPE (type)),
1296 fold_abs_const (i2, TREE_TYPE (type)));
1298 if (integer_nonzerop (compare))
1300 /* In the TRUE branch, we compute
1302 div = (br * ratio) + bi;
1303 tr = (ar * ratio) + ai;
1304 ti = (ai * ratio) - ar;
1307 tree ratio = const_binop (code, r2, i2);
1308 tree div = const_binop (PLUS_EXPR, i2,
1309 const_binop (MULT_EXPR, r2, ratio));
1310 real = const_binop (MULT_EXPR, r1, ratio);
1311 real = const_binop (PLUS_EXPR, real, i1);
1312 real = const_binop (code, real, div);
1314 imag = const_binop (MULT_EXPR, i1, ratio);
1315 imag = const_binop (MINUS_EXPR, imag, r1);
1316 imag = const_binop (code, imag, div);
1320 /* In the FALSE branch, we compute
1322 divisor = (d * ratio) + c;
1323 tr = (b * ratio) + a;
1324 ti = b - (a * ratio);
1327 tree ratio = const_binop (code, i2, r2);
1328 tree div = const_binop (PLUS_EXPR, r2,
1329 const_binop (MULT_EXPR, i2, ratio));
1331 real = const_binop (MULT_EXPR, i1, ratio);
1332 real = const_binop (PLUS_EXPR, real, r1);
1333 real = const_binop (code, real, div);
1335 imag = const_binop (MULT_EXPR, r1, ratio);
1336 imag = const_binop (MINUS_EXPR, i1, imag);
1337 imag = const_binop (code, imag, div);
1347 return build_complex (type, real, imag);
1350 if (TREE_CODE (arg1) == VECTOR_CST)
1352 tree type = TREE_TYPE(arg1);
1353 int count = TYPE_VECTOR_SUBPARTS (type), i;
1354 tree elements1, elements2, list = NULL_TREE;
1356 if(TREE_CODE(arg2) != VECTOR_CST)
1359 elements1 = TREE_VECTOR_CST_ELTS (arg1);
1360 elements2 = TREE_VECTOR_CST_ELTS (arg2);
1362 for (i = 0; i < count; i++)
1364 tree elem1, elem2, elem;
1366 /* The trailing elements can be empty and should be treated as 0 */
1368 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1371 elem1 = TREE_VALUE(elements1);
1372 elements1 = TREE_CHAIN (elements1);
1376 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1379 elem2 = TREE_VALUE(elements2);
1380 elements2 = TREE_CHAIN (elements2);
1383 elem = const_binop (code, elem1, elem2);
1385 /* It is possible that const_binop cannot handle the given
1386 code and return NULL_TREE */
1387 if(elem == NULL_TREE)
1390 list = tree_cons (NULL_TREE, elem, list);
1392 return build_vector(type, nreverse(list));
1397 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1398 indicates which particular sizetype to create. */
1401 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1403 return build_int_cst (sizetype_tab[(int) kind], number);
1406 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1407 is a tree code. The type of the result is taken from the operands.
1408 Both must be equivalent integer types, ala int_binop_types_match_p.
1409 If the operands are constant, so is the result. */
1412 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1414 tree type = TREE_TYPE (arg0);
1416 if (arg0 == error_mark_node || arg1 == error_mark_node)
1417 return error_mark_node;
1419 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1422 /* Handle the special case of two integer constants faster. */
1423 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1425 /* And some specific cases even faster than that. */
1426 if (code == PLUS_EXPR)
1428 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1430 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1433 else if (code == MINUS_EXPR)
1435 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1438 else if (code == MULT_EXPR)
1440 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1444 /* Handle general case of two integer constants. */
1445 return int_const_binop (code, arg0, arg1, 0);
1448 return fold_build2_loc (loc, code, type, arg0, arg1);
1451 /* Given two values, either both of sizetype or both of bitsizetype,
1452 compute the difference between the two values. Return the value
1453 in signed type corresponding to the type of the operands. */
1456 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1458 tree type = TREE_TYPE (arg0);
1461 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1464 /* If the type is already signed, just do the simple thing. */
1465 if (!TYPE_UNSIGNED (type))
1466 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1468 if (type == sizetype)
1470 else if (type == bitsizetype)
1471 ctype = sbitsizetype;
1473 ctype = signed_type_for (type);
1475 /* If either operand is not a constant, do the conversions to the signed
1476 type and subtract. The hardware will do the right thing with any
1477 overflow in the subtraction. */
1478 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1479 return size_binop_loc (loc, MINUS_EXPR,
1480 fold_convert_loc (loc, ctype, arg0),
1481 fold_convert_loc (loc, ctype, arg1));
1483 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1484 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1485 overflow) and negate (which can't either). Special-case a result
1486 of zero while we're here. */
1487 if (tree_int_cst_equal (arg0, arg1))
1488 return build_int_cst (ctype, 0);
1489 else if (tree_int_cst_lt (arg1, arg0))
1490 return fold_convert_loc (loc, ctype,
1491 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1493 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1494 fold_convert_loc (loc, ctype,
1495 size_binop_loc (loc,
1500 /* A subroutine of fold_convert_const handling conversions of an
1501 INTEGER_CST to another integer type. */
1504 fold_convert_const_int_from_int (tree type, const_tree arg1)
1508 /* Given an integer constant, make new constant with new type,
1509 appropriately sign-extended or truncated. */
1510 t = force_fit_type_double (type, tree_to_double_int (arg1),
1511 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1512 (TREE_INT_CST_HIGH (arg1) < 0
1513 && (TYPE_UNSIGNED (type)
1514 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1515 | TREE_OVERFLOW (arg1));
1520 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1521 to an integer type. */
1524 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1529 /* The following code implements the floating point to integer
1530 conversion rules required by the Java Language Specification,
1531 that IEEE NaNs are mapped to zero and values that overflow
1532 the target precision saturate, i.e. values greater than
1533 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1534 are mapped to INT_MIN. These semantics are allowed by the
1535 C and C++ standards that simply state that the behavior of
1536 FP-to-integer conversion is unspecified upon overflow. */
1540 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1544 case FIX_TRUNC_EXPR:
1545 real_trunc (&r, VOIDmode, &x);
1552 /* If R is NaN, return zero and show we have an overflow. */
1553 if (REAL_VALUE_ISNAN (r))
1556 val = double_int_zero;
1559 /* See if R is less than the lower bound or greater than the
1564 tree lt = TYPE_MIN_VALUE (type);
1565 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1566 if (REAL_VALUES_LESS (r, l))
1569 val = tree_to_double_int (lt);
1575 tree ut = TYPE_MAX_VALUE (type);
1578 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1579 if (REAL_VALUES_LESS (u, r))
1582 val = tree_to_double_int (ut);
1588 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1590 t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1594 /* A subroutine of fold_convert_const handling conversions of a
1595 FIXED_CST to an integer type. */
1598 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1601 double_int temp, temp_trunc;
1604 /* Right shift FIXED_CST to temp by fbit. */
1605 temp = TREE_FIXED_CST (arg1).data;
1606 mode = TREE_FIXED_CST (arg1).mode;
1607 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1609 temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1610 HOST_BITS_PER_DOUBLE_INT,
1611 SIGNED_FIXED_POINT_MODE_P (mode));
1613 /* Left shift temp to temp_trunc by fbit. */
1614 temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1615 HOST_BITS_PER_DOUBLE_INT,
1616 SIGNED_FIXED_POINT_MODE_P (mode));
1620 temp = double_int_zero;
1621 temp_trunc = double_int_zero;
1624 /* If FIXED_CST is negative, we need to round the value toward 0.
1625 By checking if the fractional bits are not zero to add 1 to temp. */
1626 if (SIGNED_FIXED_POINT_MODE_P (mode)
1627 && double_int_negative_p (temp_trunc)
1628 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1629 temp = double_int_add (temp, double_int_one);
1631 /* Given a fixed-point constant, make new constant with new type,
1632 appropriately sign-extended or truncated. */
1633 t = force_fit_type_double (type, temp, -1,
1634 (double_int_negative_p (temp)
1635 && (TYPE_UNSIGNED (type)
1636 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1637 | TREE_OVERFLOW (arg1));
1642 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1643 to another floating point type. */
1646 fold_convert_const_real_from_real (tree type, const_tree arg1)
1648 REAL_VALUE_TYPE value;
1651 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1652 t = build_real (type, value);
1654 /* If converting an infinity or NAN to a representation that doesn't
1655 have one, set the overflow bit so that we can produce some kind of
1656 error message at the appropriate point if necessary. It's not the
1657 most user-friendly message, but it's better than nothing. */
1658 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1659 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1660 TREE_OVERFLOW (t) = 1;
1661 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1662 && !MODE_HAS_NANS (TYPE_MODE (type)))
1663 TREE_OVERFLOW (t) = 1;
1664 /* Regular overflow, conversion produced an infinity in a mode that
1665 can't represent them. */
1666 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1667 && REAL_VALUE_ISINF (value)
1668 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1669 TREE_OVERFLOW (t) = 1;
1671 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1675 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1676 to a floating point type. */
1679 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1681 REAL_VALUE_TYPE value;
1684 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1685 t = build_real (type, value);
1687 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1691 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1692 to another fixed-point type. */
1695 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1697 FIXED_VALUE_TYPE value;
1701 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1702 TYPE_SATURATING (type));
1703 t = build_fixed (type, value);
1705 /* Propagate overflow flags. */
1706 if (overflow_p | TREE_OVERFLOW (arg1))
1707 TREE_OVERFLOW (t) = 1;
1711 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1712 to a fixed-point type. */
1715 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1717 FIXED_VALUE_TYPE value;
1721 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1722 TREE_INT_CST (arg1),
1723 TYPE_UNSIGNED (TREE_TYPE (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 a REAL_CST
1734 to a fixed-point type. */
1737 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1739 FIXED_VALUE_TYPE value;
1743 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1744 &TREE_REAL_CST (arg1),
1745 TYPE_SATURATING (type));
1746 t = build_fixed (type, value);
1748 /* Propagate overflow flags. */
1749 if (overflow_p | TREE_OVERFLOW (arg1))
1750 TREE_OVERFLOW (t) = 1;
1754 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1755 type TYPE. If no simplification can be done return NULL_TREE. */
1758 fold_convert_const (enum tree_code code, tree type, tree arg1)
1760 if (TREE_TYPE (arg1) == type)
1763 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1764 || TREE_CODE (type) == OFFSET_TYPE)
1766 if (TREE_CODE (arg1) == INTEGER_CST)
1767 return fold_convert_const_int_from_int (type, arg1);
1768 else if (TREE_CODE (arg1) == REAL_CST)
1769 return fold_convert_const_int_from_real (code, type, arg1);
1770 else if (TREE_CODE (arg1) == FIXED_CST)
1771 return fold_convert_const_int_from_fixed (type, arg1);
1773 else if (TREE_CODE (type) == REAL_TYPE)
1775 if (TREE_CODE (arg1) == INTEGER_CST)
1776 return build_real_from_int_cst (type, arg1);
1777 else if (TREE_CODE (arg1) == REAL_CST)
1778 return fold_convert_const_real_from_real (type, arg1);
1779 else if (TREE_CODE (arg1) == FIXED_CST)
1780 return fold_convert_const_real_from_fixed (type, arg1);
1782 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1784 if (TREE_CODE (arg1) == FIXED_CST)
1785 return fold_convert_const_fixed_from_fixed (type, arg1);
1786 else if (TREE_CODE (arg1) == INTEGER_CST)
1787 return fold_convert_const_fixed_from_int (type, arg1);
1788 else if (TREE_CODE (arg1) == REAL_CST)
1789 return fold_convert_const_fixed_from_real (type, arg1);
1794 /* Construct a vector of zero elements of vector type TYPE. */
1797 build_zero_vector (tree type)
1801 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1802 return build_vector_from_val (type, t);
1805 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1808 fold_convertible_p (const_tree type, const_tree arg)
1810 tree orig = TREE_TYPE (arg);
1815 if (TREE_CODE (arg) == ERROR_MARK
1816 || TREE_CODE (type) == ERROR_MARK
1817 || TREE_CODE (orig) == ERROR_MARK)
1820 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1823 switch (TREE_CODE (type))
1825 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1826 case POINTER_TYPE: case REFERENCE_TYPE:
1828 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1829 || TREE_CODE (orig) == OFFSET_TYPE)
1831 return (TREE_CODE (orig) == VECTOR_TYPE
1832 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1835 case FIXED_POINT_TYPE:
1839 return TREE_CODE (type) == TREE_CODE (orig);
1846 /* Convert expression ARG to type TYPE. Used by the middle-end for
1847 simple conversions in preference to calling the front-end's convert. */
1850 fold_convert_loc (location_t loc, tree type, tree arg)
1852 tree orig = TREE_TYPE (arg);
1858 if (TREE_CODE (arg) == ERROR_MARK
1859 || TREE_CODE (type) == ERROR_MARK
1860 || TREE_CODE (orig) == ERROR_MARK)
1861 return error_mark_node;
1863 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1864 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1866 switch (TREE_CODE (type))
1869 case REFERENCE_TYPE:
1870 /* Handle conversions between pointers to different address spaces. */
1871 if (POINTER_TYPE_P (orig)
1872 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1873 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1874 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1877 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1879 if (TREE_CODE (arg) == INTEGER_CST)
1881 tem = fold_convert_const (NOP_EXPR, type, arg);
1882 if (tem != NULL_TREE)
1885 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1886 || TREE_CODE (orig) == OFFSET_TYPE)
1887 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1888 if (TREE_CODE (orig) == COMPLEX_TYPE)
1889 return fold_convert_loc (loc, type,
1890 fold_build1_loc (loc, REALPART_EXPR,
1891 TREE_TYPE (orig), arg));
1892 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1893 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1894 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1897 if (TREE_CODE (arg) == INTEGER_CST)
1899 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1900 if (tem != NULL_TREE)
1903 else if (TREE_CODE (arg) == REAL_CST)
1905 tem = fold_convert_const (NOP_EXPR, type, arg);
1906 if (tem != NULL_TREE)
1909 else if (TREE_CODE (arg) == FIXED_CST)
1911 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1912 if (tem != NULL_TREE)
1916 switch (TREE_CODE (orig))
1919 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1920 case POINTER_TYPE: case REFERENCE_TYPE:
1921 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1924 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1926 case FIXED_POINT_TYPE:
1927 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1930 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1931 return fold_convert_loc (loc, type, tem);
1937 case FIXED_POINT_TYPE:
1938 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1939 || TREE_CODE (arg) == REAL_CST)
1941 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1942 if (tem != NULL_TREE)
1943 goto fold_convert_exit;
1946 switch (TREE_CODE (orig))
1948 case FIXED_POINT_TYPE:
1953 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1956 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1957 return fold_convert_loc (loc, type, tem);
1964 switch (TREE_CODE (orig))
1967 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1968 case POINTER_TYPE: case REFERENCE_TYPE:
1970 case FIXED_POINT_TYPE:
1971 return fold_build2_loc (loc, COMPLEX_EXPR, type,
1972 fold_convert_loc (loc, TREE_TYPE (type), arg),
1973 fold_convert_loc (loc, TREE_TYPE (type),
1974 integer_zero_node));
1979 if (TREE_CODE (arg) == COMPLEX_EXPR)
1981 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1982 TREE_OPERAND (arg, 0));
1983 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1984 TREE_OPERAND (arg, 1));
1985 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1988 arg = save_expr (arg);
1989 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1990 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1991 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1992 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1993 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2001 if (integer_zerop (arg))
2002 return build_zero_vector (type);
2003 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2004 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2005 || TREE_CODE (orig) == VECTOR_TYPE);
2006 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2009 tem = fold_ignored_result (arg);
2010 if (TREE_CODE (tem) == MODIFY_EXPR)
2011 goto fold_convert_exit;
2012 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2018 protected_set_expr_location (tem, loc);
2022 /* Return false if expr can be assumed not to be an lvalue, true
2026 maybe_lvalue_p (const_tree x)
2028 /* We only need to wrap lvalue tree codes. */
2029 switch (TREE_CODE (x))
2042 case ARRAY_RANGE_REF:
2048 case PREINCREMENT_EXPR:
2049 case PREDECREMENT_EXPR:
2051 case TRY_CATCH_EXPR:
2052 case WITH_CLEANUP_EXPR:
2061 /* Assume the worst for front-end tree codes. */
2062 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2070 /* Return an expr equal to X but certainly not valid as an lvalue. */
2073 non_lvalue_loc (location_t loc, tree x)
2075 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2080 if (! maybe_lvalue_p (x))
2082 x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2083 SET_EXPR_LOCATION (x, loc);
2087 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2088 Zero means allow extended lvalues. */
2090 int pedantic_lvalues;
2092 /* When pedantic, return an expr equal to X but certainly not valid as a
2093 pedantic lvalue. Otherwise, return X. */
2096 pedantic_non_lvalue_loc (location_t loc, tree x)
2098 if (pedantic_lvalues)
2099 return non_lvalue_loc (loc, x);
2100 protected_set_expr_location (x, loc);
2104 /* Given a tree comparison code, return the code that is the logical inverse
2105 of the given code. It is not safe to do this for floating-point
2106 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2107 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2110 invert_tree_comparison (enum tree_code code, bool honor_nans)
2112 if (honor_nans && flag_trapping_math)
2122 return honor_nans ? UNLE_EXPR : LE_EXPR;
2124 return honor_nans ? UNLT_EXPR : LT_EXPR;
2126 return honor_nans ? UNGE_EXPR : GE_EXPR;
2128 return honor_nans ? UNGT_EXPR : GT_EXPR;
2142 return UNORDERED_EXPR;
2143 case UNORDERED_EXPR:
2144 return ORDERED_EXPR;
2150 /* Similar, but return the comparison that results if the operands are
2151 swapped. This is safe for floating-point. */
2154 swap_tree_comparison (enum tree_code code)
2161 case UNORDERED_EXPR:
2187 /* Convert a comparison tree code from an enum tree_code representation
2188 into a compcode bit-based encoding. This function is the inverse of
2189 compcode_to_comparison. */
2191 static enum comparison_code
2192 comparison_to_compcode (enum tree_code code)
2209 return COMPCODE_ORD;
2210 case UNORDERED_EXPR:
2211 return COMPCODE_UNORD;
2213 return COMPCODE_UNLT;
2215 return COMPCODE_UNEQ;
2217 return COMPCODE_UNLE;
2219 return COMPCODE_UNGT;
2221 return COMPCODE_LTGT;
2223 return COMPCODE_UNGE;
2229 /* Convert a compcode bit-based encoding of a comparison operator back
2230 to GCC's enum tree_code representation. This function is the
2231 inverse of comparison_to_compcode. */
2233 static enum tree_code
2234 compcode_to_comparison (enum comparison_code code)
2251 return ORDERED_EXPR;
2252 case COMPCODE_UNORD:
2253 return UNORDERED_EXPR;
2271 /* Return a tree for the comparison which is the combination of
2272 doing the AND or OR (depending on CODE) of the two operations LCODE
2273 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2274 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2275 if this makes the transformation invalid. */
2278 combine_comparisons (location_t loc,
2279 enum tree_code code, enum tree_code lcode,
2280 enum tree_code rcode, tree truth_type,
2281 tree ll_arg, tree lr_arg)
2283 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2284 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2285 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2290 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2291 compcode = lcompcode & rcompcode;
2294 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2295 compcode = lcompcode | rcompcode;
2304 /* Eliminate unordered comparisons, as well as LTGT and ORD
2305 which are not used unless the mode has NaNs. */
2306 compcode &= ~COMPCODE_UNORD;
2307 if (compcode == COMPCODE_LTGT)
2308 compcode = COMPCODE_NE;
2309 else if (compcode == COMPCODE_ORD)
2310 compcode = COMPCODE_TRUE;
2312 else if (flag_trapping_math)
2314 /* Check that the original operation and the optimized ones will trap
2315 under the same condition. */
2316 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2317 && (lcompcode != COMPCODE_EQ)
2318 && (lcompcode != COMPCODE_ORD);
2319 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2320 && (rcompcode != COMPCODE_EQ)
2321 && (rcompcode != COMPCODE_ORD);
2322 bool trap = (compcode & COMPCODE_UNORD) == 0
2323 && (compcode != COMPCODE_EQ)
2324 && (compcode != COMPCODE_ORD);
2326 /* In a short-circuited boolean expression the LHS might be
2327 such that the RHS, if evaluated, will never trap. For
2328 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2329 if neither x nor y is NaN. (This is a mixed blessing: for
2330 example, the expression above will never trap, hence
2331 optimizing it to x < y would be invalid). */
2332 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2333 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2336 /* If the comparison was short-circuited, and only the RHS
2337 trapped, we may now generate a spurious trap. */
2339 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2342 /* If we changed the conditions that cause a trap, we lose. */
2343 if ((ltrap || rtrap) != trap)
2347 if (compcode == COMPCODE_TRUE)
2348 return constant_boolean_node (true, truth_type);
2349 else if (compcode == COMPCODE_FALSE)
2350 return constant_boolean_node (false, truth_type);
2353 enum tree_code tcode;
2355 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2356 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2360 /* Return nonzero if two operands (typically of the same tree node)
2361 are necessarily equal. If either argument has side-effects this
2362 function returns zero. FLAGS modifies behavior as follows:
2364 If OEP_ONLY_CONST is set, only return nonzero for constants.
2365 This function tests whether the operands are indistinguishable;
2366 it does not test whether they are equal using C's == operation.
2367 The distinction is important for IEEE floating point, because
2368 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2369 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2371 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2372 even though it may hold multiple values during a function.
2373 This is because a GCC tree node guarantees that nothing else is
2374 executed between the evaluation of its "operands" (which may often
2375 be evaluated in arbitrary order). Hence if the operands themselves
2376 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2377 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2378 unset means assuming isochronic (or instantaneous) tree equivalence.
2379 Unless comparing arbitrary expression trees, such as from different
2380 statements, this flag can usually be left unset.
2382 If OEP_PURE_SAME is set, then pure functions with identical arguments
2383 are considered the same. It is used when the caller has other ways
2384 to ensure that global memory is unchanged in between. */
2387 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2389 /* If either is ERROR_MARK, they aren't equal. */
2390 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2391 || TREE_TYPE (arg0) == error_mark_node
2392 || TREE_TYPE (arg1) == error_mark_node)
2395 /* Similar, if either does not have a type (like a released SSA name),
2396 they aren't equal. */
2397 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2400 /* Check equality of integer constants before bailing out due to
2401 precision differences. */
2402 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2403 return tree_int_cst_equal (arg0, arg1);
2405 /* If both types don't have the same signedness, then we can't consider
2406 them equal. We must check this before the STRIP_NOPS calls
2407 because they may change the signedness of the arguments. As pointers
2408 strictly don't have a signedness, require either two pointers or
2409 two non-pointers as well. */
2410 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2411 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2414 /* We cannot consider pointers to different address space equal. */
2415 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2416 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2417 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2420 /* If both types don't have the same precision, then it is not safe
2422 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2428 /* In case both args are comparisons but with different comparison
2429 code, try to swap the comparison operands of one arg to produce
2430 a match and compare that variant. */
2431 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2432 && COMPARISON_CLASS_P (arg0)
2433 && COMPARISON_CLASS_P (arg1))
2435 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2437 if (TREE_CODE (arg0) == swap_code)
2438 return operand_equal_p (TREE_OPERAND (arg0, 0),
2439 TREE_OPERAND (arg1, 1), flags)
2440 && operand_equal_p (TREE_OPERAND (arg0, 1),
2441 TREE_OPERAND (arg1, 0), flags);
2444 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2445 /* This is needed for conversions and for COMPONENT_REF.
2446 Might as well play it safe and always test this. */
2447 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2448 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2449 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2452 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2453 We don't care about side effects in that case because the SAVE_EXPR
2454 takes care of that for us. In all other cases, two expressions are
2455 equal if they have no side effects. If we have two identical
2456 expressions with side effects that should be treated the same due
2457 to the only side effects being identical SAVE_EXPR's, that will
2458 be detected in the recursive calls below. */
2459 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2460 && (TREE_CODE (arg0) == SAVE_EXPR
2461 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2464 /* Next handle constant cases, those for which we can return 1 even
2465 if ONLY_CONST is set. */
2466 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2467 switch (TREE_CODE (arg0))
2470 return tree_int_cst_equal (arg0, arg1);
2473 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2474 TREE_FIXED_CST (arg1));
2477 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2478 TREE_REAL_CST (arg1)))
2482 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2484 /* If we do not distinguish between signed and unsigned zero,
2485 consider them equal. */
2486 if (real_zerop (arg0) && real_zerop (arg1))
2495 v1 = TREE_VECTOR_CST_ELTS (arg0);
2496 v2 = TREE_VECTOR_CST_ELTS (arg1);
2499 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2502 v1 = TREE_CHAIN (v1);
2503 v2 = TREE_CHAIN (v2);
2510 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2512 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2516 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2517 && ! memcmp (TREE_STRING_POINTER (arg0),
2518 TREE_STRING_POINTER (arg1),
2519 TREE_STRING_LENGTH (arg0)));
2522 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2528 if (flags & OEP_ONLY_CONST)
2531 /* Define macros to test an operand from arg0 and arg1 for equality and a
2532 variant that allows null and views null as being different from any
2533 non-null value. In the latter case, if either is null, the both
2534 must be; otherwise, do the normal comparison. */
2535 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2536 TREE_OPERAND (arg1, N), flags)
2538 #define OP_SAME_WITH_NULL(N) \
2539 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2540 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2542 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2545 /* Two conversions are equal only if signedness and modes match. */
2546 switch (TREE_CODE (arg0))
2549 case FIX_TRUNC_EXPR:
2550 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2551 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2561 case tcc_comparison:
2563 if (OP_SAME (0) && OP_SAME (1))
2566 /* For commutative ops, allow the other order. */
2567 return (commutative_tree_code (TREE_CODE (arg0))
2568 && operand_equal_p (TREE_OPERAND (arg0, 0),
2569 TREE_OPERAND (arg1, 1), flags)
2570 && operand_equal_p (TREE_OPERAND (arg0, 1),
2571 TREE_OPERAND (arg1, 0), flags));
2574 /* If either of the pointer (or reference) expressions we are
2575 dereferencing contain a side effect, these cannot be equal. */
2576 if (TREE_SIDE_EFFECTS (arg0)
2577 || TREE_SIDE_EFFECTS (arg1))
2580 switch (TREE_CODE (arg0))
2588 /* Require equal access sizes, and similar pointer types.
2589 We can have incomplete types for array references of
2590 variable-sized arrays from the Fortran frontent
2592 return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2593 || (TYPE_SIZE (TREE_TYPE (arg0))
2594 && TYPE_SIZE (TREE_TYPE (arg1))
2595 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2596 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2597 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2598 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2599 && OP_SAME (0) && OP_SAME (1));
2602 case ARRAY_RANGE_REF:
2603 /* Operands 2 and 3 may be null.
2604 Compare the array index by value if it is constant first as we
2605 may have different types but same value here. */
2607 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2608 TREE_OPERAND (arg1, 1))
2610 && OP_SAME_WITH_NULL (2)
2611 && OP_SAME_WITH_NULL (3));
2614 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2615 may be NULL when we're called to compare MEM_EXPRs. */
2616 return OP_SAME_WITH_NULL (0)
2618 && OP_SAME_WITH_NULL (2);
2621 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2627 case tcc_expression:
2628 switch (TREE_CODE (arg0))
2631 case TRUTH_NOT_EXPR:
2634 case TRUTH_ANDIF_EXPR:
2635 case TRUTH_ORIF_EXPR:
2636 return OP_SAME (0) && OP_SAME (1);
2639 case WIDEN_MULT_PLUS_EXPR:
2640 case WIDEN_MULT_MINUS_EXPR:
2643 /* The multiplcation operands are commutative. */
2646 case TRUTH_AND_EXPR:
2648 case TRUTH_XOR_EXPR:
2649 if (OP_SAME (0) && OP_SAME (1))
2652 /* Otherwise take into account this is a commutative operation. */
2653 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2654 TREE_OPERAND (arg1, 1), flags)
2655 && operand_equal_p (TREE_OPERAND (arg0, 1),
2656 TREE_OPERAND (arg1, 0), flags));
2661 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2668 switch (TREE_CODE (arg0))
2671 /* If the CALL_EXPRs call different functions, then they
2672 clearly can not be equal. */
2673 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2678 unsigned int cef = call_expr_flags (arg0);
2679 if (flags & OEP_PURE_SAME)
2680 cef &= ECF_CONST | ECF_PURE;
2687 /* Now see if all the arguments are the same. */
2689 const_call_expr_arg_iterator iter0, iter1;
2691 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2692 a1 = first_const_call_expr_arg (arg1, &iter1);
2694 a0 = next_const_call_expr_arg (&iter0),
2695 a1 = next_const_call_expr_arg (&iter1))
2696 if (! operand_equal_p (a0, a1, flags))
2699 /* If we get here and both argument lists are exhausted
2700 then the CALL_EXPRs are equal. */
2701 return ! (a0 || a1);
2707 case tcc_declaration:
2708 /* Consider __builtin_sqrt equal to sqrt. */
2709 return (TREE_CODE (arg0) == FUNCTION_DECL
2710 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2711 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2712 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2719 #undef OP_SAME_WITH_NULL
2722 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2723 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2725 When in doubt, return 0. */
2728 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2730 int unsignedp1, unsignedpo;
2731 tree primarg0, primarg1, primother;
2732 unsigned int correct_width;
2734 if (operand_equal_p (arg0, arg1, 0))
2737 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2738 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2741 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2742 and see if the inner values are the same. This removes any
2743 signedness comparison, which doesn't matter here. */
2744 primarg0 = arg0, primarg1 = arg1;
2745 STRIP_NOPS (primarg0);
2746 STRIP_NOPS (primarg1);
2747 if (operand_equal_p (primarg0, primarg1, 0))
2750 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2751 actual comparison operand, ARG0.
2753 First throw away any conversions to wider types
2754 already present in the operands. */
2756 primarg1 = get_narrower (arg1, &unsignedp1);
2757 primother = get_narrower (other, &unsignedpo);
2759 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2760 if (unsignedp1 == unsignedpo
2761 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2762 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2764 tree type = TREE_TYPE (arg0);
2766 /* Make sure shorter operand is extended the right way
2767 to match the longer operand. */
2768 primarg1 = fold_convert (signed_or_unsigned_type_for
2769 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2771 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2778 /* See if ARG is an expression that is either a comparison or is performing
2779 arithmetic on comparisons. The comparisons must only be comparing
2780 two different values, which will be stored in *CVAL1 and *CVAL2; if
2781 they are nonzero it means that some operands have already been found.
2782 No variables may be used anywhere else in the expression except in the
2783 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2784 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2786 If this is true, return 1. Otherwise, return zero. */
2789 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2791 enum tree_code code = TREE_CODE (arg);
2792 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2794 /* We can handle some of the tcc_expression cases here. */
2795 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2797 else if (tclass == tcc_expression
2798 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2799 || code == COMPOUND_EXPR))
2800 tclass = tcc_binary;
2802 else if (tclass == tcc_expression && code == SAVE_EXPR
2803 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2805 /* If we've already found a CVAL1 or CVAL2, this expression is
2806 two complex to handle. */
2807 if (*cval1 || *cval2)
2817 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2820 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2821 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2822 cval1, cval2, save_p));
2827 case tcc_expression:
2828 if (code == COND_EXPR)
2829 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2830 cval1, cval2, save_p)
2831 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2832 cval1, cval2, save_p)
2833 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2834 cval1, cval2, save_p));
2837 case tcc_comparison:
2838 /* First see if we can handle the first operand, then the second. For
2839 the second operand, we know *CVAL1 can't be zero. It must be that
2840 one side of the comparison is each of the values; test for the
2841 case where this isn't true by failing if the two operands
2844 if (operand_equal_p (TREE_OPERAND (arg, 0),
2845 TREE_OPERAND (arg, 1), 0))
2849 *cval1 = TREE_OPERAND (arg, 0);
2850 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2852 else if (*cval2 == 0)
2853 *cval2 = TREE_OPERAND (arg, 0);
2854 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2859 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2861 else if (*cval2 == 0)
2862 *cval2 = TREE_OPERAND (arg, 1);
2863 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2875 /* ARG is a tree that is known to contain just arithmetic operations and
2876 comparisons. Evaluate the operations in the tree substituting NEW0 for
2877 any occurrence of OLD0 as an operand of a comparison and likewise for
2881 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2882 tree old1, tree new1)
2884 tree type = TREE_TYPE (arg);
2885 enum tree_code code = TREE_CODE (arg);
2886 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2888 /* We can handle some of the tcc_expression cases here. */
2889 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2891 else if (tclass == tcc_expression
2892 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2893 tclass = tcc_binary;
2898 return fold_build1_loc (loc, code, type,
2899 eval_subst (loc, TREE_OPERAND (arg, 0),
2900 old0, new0, old1, new1));
2903 return fold_build2_loc (loc, code, type,
2904 eval_subst (loc, TREE_OPERAND (arg, 0),
2905 old0, new0, old1, new1),
2906 eval_subst (loc, TREE_OPERAND (arg, 1),
2907 old0, new0, old1, new1));
2909 case tcc_expression:
2913 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2917 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2921 return fold_build3_loc (loc, code, type,
2922 eval_subst (loc, TREE_OPERAND (arg, 0),
2923 old0, new0, old1, new1),
2924 eval_subst (loc, TREE_OPERAND (arg, 1),
2925 old0, new0, old1, new1),
2926 eval_subst (loc, TREE_OPERAND (arg, 2),
2927 old0, new0, old1, new1));
2931 /* Fall through - ??? */
2933 case tcc_comparison:
2935 tree arg0 = TREE_OPERAND (arg, 0);
2936 tree arg1 = TREE_OPERAND (arg, 1);
2938 /* We need to check both for exact equality and tree equality. The
2939 former will be true if the operand has a side-effect. In that
2940 case, we know the operand occurred exactly once. */
2942 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2944 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2947 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2949 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2952 return fold_build2_loc (loc, code, type, arg0, arg1);
2960 /* Return a tree for the case when the result of an expression is RESULT
2961 converted to TYPE and OMITTED was previously an operand of the expression
2962 but is now not needed (e.g., we folded OMITTED * 0).
2964 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2965 the conversion of RESULT to TYPE. */
2968 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2970 tree t = fold_convert_loc (loc, type, result);
2972 /* If the resulting operand is an empty statement, just return the omitted
2973 statement casted to void. */
2974 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2976 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2977 goto omit_one_operand_exit;
2980 if (TREE_SIDE_EFFECTS (omitted))
2982 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2983 goto omit_one_operand_exit;
2986 return non_lvalue_loc (loc, t);
2988 omit_one_operand_exit:
2989 protected_set_expr_location (t, loc);
2993 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2996 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2999 tree t = fold_convert_loc (loc, type, result);
3001 /* If the resulting operand is an empty statement, just return the omitted
3002 statement casted to void. */
3003 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3005 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3006 goto pedantic_omit_one_operand_exit;
3009 if (TREE_SIDE_EFFECTS (omitted))
3011 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3012 goto pedantic_omit_one_operand_exit;
3015 return pedantic_non_lvalue_loc (loc, t);
3017 pedantic_omit_one_operand_exit:
3018 protected_set_expr_location (t, loc);
3022 /* Return a tree for the case when the result of an expression is RESULT
3023 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3024 of the expression but are now not needed.
3026 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3027 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3028 evaluated before OMITTED2. Otherwise, if neither has side effects,
3029 just do the conversion of RESULT to TYPE. */
3032 omit_two_operands_loc (location_t loc, tree type, tree result,
3033 tree omitted1, tree omitted2)
3035 tree t = fold_convert_loc (loc, type, result);
3037 if (TREE_SIDE_EFFECTS (omitted2))
3039 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3040 SET_EXPR_LOCATION (t, loc);
3042 if (TREE_SIDE_EFFECTS (omitted1))
3044 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3045 SET_EXPR_LOCATION (t, loc);
3048 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3052 /* Return a simplified tree node for the truth-negation of ARG. This
3053 never alters ARG itself. We assume that ARG is an operation that
3054 returns a truth value (0 or 1).
3056 FIXME: one would think we would fold the result, but it causes
3057 problems with the dominator optimizer. */
3060 fold_truth_not_expr (location_t loc, tree arg)
3062 tree t, type = TREE_TYPE (arg);
3063 enum tree_code code = TREE_CODE (arg);
3064 location_t loc1, loc2;
3066 /* If this is a comparison, we can simply invert it, except for
3067 floating-point non-equality comparisons, in which case we just
3068 enclose a TRUTH_NOT_EXPR around what we have. */
3070 if (TREE_CODE_CLASS (code) == tcc_comparison)
3072 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3073 if (FLOAT_TYPE_P (op_type)
3074 && flag_trapping_math
3075 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3076 && code != NE_EXPR && code != EQ_EXPR)
3079 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3080 if (code == ERROR_MARK)
3083 t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3084 SET_EXPR_LOCATION (t, loc);
3091 return constant_boolean_node (integer_zerop (arg), type);
3093 case TRUTH_AND_EXPR:
3094 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3095 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3096 if (loc1 == UNKNOWN_LOCATION)
3098 if (loc2 == UNKNOWN_LOCATION)
3100 t = build2 (TRUTH_OR_EXPR, type,
3101 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3102 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3106 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3107 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3108 if (loc1 == UNKNOWN_LOCATION)
3110 if (loc2 == UNKNOWN_LOCATION)
3112 t = build2 (TRUTH_AND_EXPR, type,
3113 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3114 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3117 case TRUTH_XOR_EXPR:
3118 /* Here we can invert either operand. We invert the first operand
3119 unless the second operand is a TRUTH_NOT_EXPR in which case our
3120 result is the XOR of the first operand with the inside of the
3121 negation of the second operand. */
3123 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3124 t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3125 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3127 t = build2 (TRUTH_XOR_EXPR, type,
3128 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3129 TREE_OPERAND (arg, 1));
3132 case TRUTH_ANDIF_EXPR:
3133 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3134 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3135 if (loc1 == UNKNOWN_LOCATION)
3137 if (loc2 == UNKNOWN_LOCATION)
3139 t = build2 (TRUTH_ORIF_EXPR, type,
3140 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3141 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3144 case TRUTH_ORIF_EXPR:
3145 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3146 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3147 if (loc1 == UNKNOWN_LOCATION)
3149 if (loc2 == UNKNOWN_LOCATION)
3151 t = build2 (TRUTH_ANDIF_EXPR, type,
3152 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3153 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3156 case TRUTH_NOT_EXPR:
3157 return TREE_OPERAND (arg, 0);
3161 tree arg1 = TREE_OPERAND (arg, 1);
3162 tree arg2 = TREE_OPERAND (arg, 2);
3164 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3165 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3166 if (loc1 == UNKNOWN_LOCATION)
3168 if (loc2 == UNKNOWN_LOCATION)
3171 /* A COND_EXPR may have a throw as one operand, which
3172 then has void type. Just leave void operands
3174 t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3175 VOID_TYPE_P (TREE_TYPE (arg1))
3176 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3177 VOID_TYPE_P (TREE_TYPE (arg2))
3178 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3183 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3184 if (loc1 == UNKNOWN_LOCATION)
3186 t = build2 (COMPOUND_EXPR, type,
3187 TREE_OPERAND (arg, 0),
3188 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3191 case NON_LVALUE_EXPR:
3192 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3193 if (loc1 == UNKNOWN_LOCATION)
3195 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3198 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3200 t = build1 (TRUTH_NOT_EXPR, type, arg);
3204 /* ... fall through ... */
3207 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3208 if (loc1 == UNKNOWN_LOCATION)
3210 t = build1 (TREE_CODE (arg), type,
3211 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3215 if (!integer_onep (TREE_OPERAND (arg, 1)))
3217 t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3221 t = build1 (TRUTH_NOT_EXPR, type, arg);
3224 case CLEANUP_POINT_EXPR:
3225 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3226 if (loc1 == UNKNOWN_LOCATION)
3228 t = build1 (CLEANUP_POINT_EXPR, type,
3229 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3238 SET_EXPR_LOCATION (t, loc);
3243 /* Return a simplified tree node for the truth-negation of ARG. This
3244 never alters ARG itself. We assume that ARG is an operation that
3245 returns a truth value (0 or 1).
3247 FIXME: one would think we would fold the result, but it causes
3248 problems with the dominator optimizer. */
3251 invert_truthvalue_loc (location_t loc, tree arg)
3255 if (TREE_CODE (arg) == ERROR_MARK)
3258 tem = fold_truth_not_expr (loc, arg);
3261 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3262 SET_EXPR_LOCATION (tem, loc);
3268 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3269 operands are another bit-wise operation with a common input. If so,
3270 distribute the bit operations to save an operation and possibly two if
3271 constants are involved. For example, convert
3272 (A | B) & (A | C) into A | (B & C)
3273 Further simplification will occur if B and C are constants.
3275 If this optimization cannot be done, 0 will be returned. */
3278 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3279 tree arg0, tree arg1)
3284 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3285 || TREE_CODE (arg0) == code
3286 || (TREE_CODE (arg0) != BIT_AND_EXPR
3287 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3290 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3292 common = TREE_OPERAND (arg0, 0);
3293 left = TREE_OPERAND (arg0, 1);
3294 right = TREE_OPERAND (arg1, 1);
3296 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3298 common = TREE_OPERAND (arg0, 0);
3299 left = TREE_OPERAND (arg0, 1);
3300 right = TREE_OPERAND (arg1, 0);
3302 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3304 common = TREE_OPERAND (arg0, 1);
3305 left = TREE_OPERAND (arg0, 0);
3306 right = TREE_OPERAND (arg1, 1);
3308 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3310 common = TREE_OPERAND (arg0, 1);
3311 left = TREE_OPERAND (arg0, 0);
3312 right = TREE_OPERAND (arg1, 0);
3317 common = fold_convert_loc (loc, type, common);
3318 left = fold_convert_loc (loc, type, left);
3319 right = fold_convert_loc (loc, type, right);
3320 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3321 fold_build2_loc (loc, code, type, left, right));
3324 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3325 with code CODE. This optimization is unsafe. */
3327 distribute_real_division (location_t loc, enum tree_code code, tree type,
3328 tree arg0, tree arg1)
3330 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3331 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3333 /* (A / C) +- (B / C) -> (A +- B) / C. */
3335 && operand_equal_p (TREE_OPERAND (arg0, 1),
3336 TREE_OPERAND (arg1, 1), 0))
3337 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3338 fold_build2_loc (loc, code, type,
3339 TREE_OPERAND (arg0, 0),
3340 TREE_OPERAND (arg1, 0)),
3341 TREE_OPERAND (arg0, 1));
3343 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3344 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3345 TREE_OPERAND (arg1, 0), 0)
3346 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3347 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3349 REAL_VALUE_TYPE r0, r1;
3350 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3351 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3353 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3355 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3356 real_arithmetic (&r0, code, &r0, &r1);
3357 return fold_build2_loc (loc, MULT_EXPR, type,
3358 TREE_OPERAND (arg0, 0),
3359 build_real (type, r0));
3365 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3366 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3369 make_bit_field_ref (location_t loc, tree inner, tree type,
3370 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3372 tree result, bftype;
3376 tree size = TYPE_SIZE (TREE_TYPE (inner));
3377 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3378 || POINTER_TYPE_P (TREE_TYPE (inner)))
3379 && host_integerp (size, 0)
3380 && tree_low_cst (size, 0) == bitsize)
3381 return fold_convert_loc (loc, type, inner);
3385 if (TYPE_PRECISION (bftype) != bitsize
3386 || TYPE_UNSIGNED (bftype) == !unsignedp)
3387 bftype = build_nonstandard_integer_type (bitsize, 0);
3389 result = build3 (BIT_FIELD_REF, bftype, inner,
3390 size_int (bitsize), bitsize_int (bitpos));
3391 SET_EXPR_LOCATION (result, loc);
3394 result = fold_convert_loc (loc, type, result);
3399 /* Optimize a bit-field compare.
3401 There are two cases: First is a compare against a constant and the
3402 second is a comparison of two items where the fields are at the same
3403 bit position relative to the start of a chunk (byte, halfword, word)
3404 large enough to contain it. In these cases we can avoid the shift
3405 implicit in bitfield extractions.
3407 For constants, we emit a compare of the shifted constant with the
3408 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3409 compared. For two fields at the same position, we do the ANDs with the
3410 similar mask and compare the result of the ANDs.
3412 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3413 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3414 are the left and right operands of the comparison, respectively.
3416 If the optimization described above can be done, we return the resulting
3417 tree. Otherwise we return zero. */
3420 optimize_bit_field_compare (location_t loc, enum tree_code code,
3421 tree compare_type, tree lhs, tree rhs)
3423 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3424 tree type = TREE_TYPE (lhs);
3425 tree signed_type, unsigned_type;
3426 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3427 enum machine_mode lmode, rmode, nmode;
3428 int lunsignedp, runsignedp;
3429 int lvolatilep = 0, rvolatilep = 0;
3430 tree linner, rinner = NULL_TREE;
3434 /* Get all the information about the extractions being done. If the bit size
3435 if the same as the size of the underlying object, we aren't doing an
3436 extraction at all and so can do nothing. We also don't want to
3437 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3438 then will no longer be able to replace it. */
3439 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3440 &lunsignedp, &lvolatilep, false);
3441 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3442 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3447 /* If this is not a constant, we can only do something if bit positions,
3448 sizes, and signedness are the same. */
3449 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3450 &runsignedp, &rvolatilep, false);
3452 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3453 || lunsignedp != runsignedp || offset != 0
3454 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3458 /* See if we can find a mode to refer to this field. We should be able to,
3459 but fail if we can't. */
3461 && GET_MODE_BITSIZE (lmode) > 0
3462 && flag_strict_volatile_bitfields > 0)
3465 nmode = get_best_mode (lbitsize, lbitpos,
3466 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3467 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3468 TYPE_ALIGN (TREE_TYPE (rinner))),
3469 word_mode, lvolatilep || rvolatilep);
3470 if (nmode == VOIDmode)
3473 /* Set signed and unsigned types of the precision of this mode for the
3475 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3476 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3478 /* Compute the bit position and size for the new reference and our offset
3479 within it. If the new reference is the same size as the original, we
3480 won't optimize anything, so return zero. */
3481 nbitsize = GET_MODE_BITSIZE (nmode);
3482 nbitpos = lbitpos & ~ (nbitsize - 1);
3484 if (nbitsize == lbitsize)
3487 if (BYTES_BIG_ENDIAN)
3488 lbitpos = nbitsize - lbitsize - lbitpos;
3490 /* Make the mask to be used against the extracted field. */
3491 mask = build_int_cst_type (unsigned_type, -1);
3492 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3493 mask = const_binop (RSHIFT_EXPR, mask,
3494 size_int (nbitsize - lbitsize - lbitpos));
3497 /* If not comparing with constant, just rework the comparison
3499 return fold_build2_loc (loc, code, compare_type,
3500 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3501 make_bit_field_ref (loc, linner,
3506 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3507 make_bit_field_ref (loc, rinner,
3513 /* Otherwise, we are handling the constant case. See if the constant is too
3514 big for the field. Warn and return a tree of for 0 (false) if so. We do
3515 this not only for its own sake, but to avoid having to test for this
3516 error case below. If we didn't, we might generate wrong code.
3518 For unsigned fields, the constant shifted right by the field length should
3519 be all zero. For signed fields, the high-order bits should agree with
3524 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3525 fold_convert_loc (loc,
3526 unsigned_type, rhs),
3527 size_int (lbitsize))))
3529 warning (0, "comparison is always %d due to width of bit-field",
3531 return constant_boolean_node (code == NE_EXPR, compare_type);
3536 tree tem = const_binop (RSHIFT_EXPR,
3537 fold_convert_loc (loc, signed_type, rhs),
3538 size_int (lbitsize - 1));
3539 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3541 warning (0, "comparison is always %d due to width of bit-field",
3543 return constant_boolean_node (code == NE_EXPR, compare_type);
3547 /* Single-bit compares should always be against zero. */
3548 if (lbitsize == 1 && ! integer_zerop (rhs))
3550 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3551 rhs = build_int_cst (type, 0);
3554 /* Make a new bitfield reference, shift the constant over the
3555 appropriate number of bits and mask it with the computed mask
3556 (in case this was a signed field). If we changed it, make a new one. */
3557 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3560 TREE_SIDE_EFFECTS (lhs) = 1;
3561 TREE_THIS_VOLATILE (lhs) = 1;
3564 rhs = const_binop (BIT_AND_EXPR,
3565 const_binop (LSHIFT_EXPR,
3566 fold_convert_loc (loc, unsigned_type, rhs),
3567 size_int (lbitpos)),
3570 lhs = build2 (code, compare_type,
3571 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3573 SET_EXPR_LOCATION (lhs, loc);
3577 /* Subroutine for fold_truthop: decode a field reference.
3579 If EXP is a comparison reference, we return the innermost reference.
3581 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3582 set to the starting bit number.
3584 If the innermost field can be completely contained in a mode-sized
3585 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3587 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3588 otherwise it is not changed.
3590 *PUNSIGNEDP is set to the signedness of the field.
3592 *PMASK is set to the mask used. This is either contained in a
3593 BIT_AND_EXPR or derived from the width of the field.
3595 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3597 Return 0 if this is not a component reference or is one that we can't
3598 do anything with. */
3601 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3602 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3603 int *punsignedp, int *pvolatilep,
3604 tree *pmask, tree *pand_mask)
3606 tree outer_type = 0;
3608 tree mask, inner, offset;
3610 unsigned int precision;
3612 /* All the optimizations using this function assume integer fields.
3613 There are problems with FP fields since the type_for_size call
3614 below can fail for, e.g., XFmode. */
3615 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3618 /* We are interested in the bare arrangement of bits, so strip everything
3619 that doesn't affect the machine mode. However, record the type of the
3620 outermost expression if it may matter below. */
3621 if (CONVERT_EXPR_P (exp)
3622 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3623 outer_type = TREE_TYPE (exp);
3626 if (TREE_CODE (exp) == BIT_AND_EXPR)
3628 and_mask = TREE_OPERAND (exp, 1);
3629 exp = TREE_OPERAND (exp, 0);
3630 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3631 if (TREE_CODE (and_mask) != INTEGER_CST)
3635 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3636 punsignedp, pvolatilep, false);
3637 if ((inner == exp && and_mask == 0)
3638 || *pbitsize < 0 || offset != 0
3639 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3642 /* If the number of bits in the reference is the same as the bitsize of
3643 the outer type, then the outer type gives the signedness. Otherwise
3644 (in case of a small bitfield) the signedness is unchanged. */
3645 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3646 *punsignedp = TYPE_UNSIGNED (outer_type);
3648 /* Compute the mask to access the bitfield. */
3649 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3650 precision = TYPE_PRECISION (unsigned_type);
3652 mask = build_int_cst_type (unsigned_type, -1);
3654 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3655 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3657 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3659 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3660 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3663 *pand_mask = and_mask;
3667 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3671 all_ones_mask_p (const_tree mask, int size)
3673 tree type = TREE_TYPE (mask);
3674 unsigned int precision = TYPE_PRECISION (type);
3677 tmask = build_int_cst_type (signed_type_for (type), -1);
3680 tree_int_cst_equal (mask,
3681 const_binop (RSHIFT_EXPR,
3682 const_binop (LSHIFT_EXPR, tmask,
3683 size_int (precision - size)),
3684 size_int (precision - size)));
3687 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3688 represents the sign bit of EXP's type. If EXP represents a sign
3689 or zero extension, also test VAL against the unextended type.
3690 The return value is the (sub)expression whose sign bit is VAL,
3691 or NULL_TREE otherwise. */
3694 sign_bit_p (tree exp, const_tree val)
3696 unsigned HOST_WIDE_INT mask_lo, lo;
3697 HOST_WIDE_INT mask_hi, hi;
3701 /* Tree EXP must have an integral type. */
3702 t = TREE_TYPE (exp);
3703 if (! INTEGRAL_TYPE_P (t))
3706 /* Tree VAL must be an integer constant. */
3707 if (TREE_CODE (val) != INTEGER_CST
3708 || TREE_OVERFLOW (val))
3711 width = TYPE_PRECISION (t);
3712 if (width > HOST_BITS_PER_WIDE_INT)
3714 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3717 mask_hi = ((unsigned HOST_WIDE_INT) -1
3718 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3724 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3727 mask_lo = ((unsigned HOST_WIDE_INT) -1
3728 >> (HOST_BITS_PER_WIDE_INT - width));
3731 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3732 treat VAL as if it were unsigned. */
3733 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3734 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3737 /* Handle extension from a narrower type. */
3738 if (TREE_CODE (exp) == NOP_EXPR
3739 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3740 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3745 /* Subroutine for fold_truthop: determine if an operand is simple enough
3746 to be evaluated unconditionally. */
3749 simple_operand_p (const_tree exp)
3751 /* Strip any conversions that don't change the machine mode. */
3754 return (CONSTANT_CLASS_P (exp)
3755 || TREE_CODE (exp) == SSA_NAME
3757 && ! TREE_ADDRESSABLE (exp)
3758 && ! TREE_THIS_VOLATILE (exp)
3759 && ! DECL_NONLOCAL (exp)
3760 /* Don't regard global variables as simple. They may be
3761 allocated in ways unknown to the compiler (shared memory,
3762 #pragma weak, etc). */
3763 && ! TREE_PUBLIC (exp)
3764 && ! DECL_EXTERNAL (exp)
3765 /* Loading a static variable is unduly expensive, but global
3766 registers aren't expensive. */
3767 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3770 /* The following functions are subroutines to fold_range_test and allow it to
3771 try to change a logical combination of comparisons into a range test.
3774 X == 2 || X == 3 || X == 4 || X == 5
3778 (unsigned) (X - 2) <= 3
3780 We describe each set of comparisons as being either inside or outside
3781 a range, using a variable named like IN_P, and then describe the
3782 range with a lower and upper bound. If one of the bounds is omitted,
3783 it represents either the highest or lowest value of the type.
3785 In the comments below, we represent a range by two numbers in brackets
3786 preceded by a "+" to designate being inside that range, or a "-" to
3787 designate being outside that range, so the condition can be inverted by
3788 flipping the prefix. An omitted bound is represented by a "-". For
3789 example, "- [-, 10]" means being outside the range starting at the lowest
3790 possible value and ending at 10, in other words, being greater than 10.
3791 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3794 We set up things so that the missing bounds are handled in a consistent
3795 manner so neither a missing bound nor "true" and "false" need to be
3796 handled using a special case. */
3798 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3799 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3800 and UPPER1_P are nonzero if the respective argument is an upper bound
3801 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3802 must be specified for a comparison. ARG1 will be converted to ARG0's
3803 type if both are specified. */
3806 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3807 tree arg1, int upper1_p)
3813 /* If neither arg represents infinity, do the normal operation.
3814 Else, if not a comparison, return infinity. Else handle the special
3815 comparison rules. Note that most of the cases below won't occur, but
3816 are handled for consistency. */
3818 if (arg0 != 0 && arg1 != 0)
3820 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3821 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3823 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3826 if (TREE_CODE_CLASS (code) != tcc_comparison)
3829 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3830 for neither. In real maths, we cannot assume open ended ranges are
3831 the same. But, this is computer arithmetic, where numbers are finite.
3832 We can therefore make the transformation of any unbounded range with
3833 the value Z, Z being greater than any representable number. This permits
3834 us to treat unbounded ranges as equal. */
3835 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3836 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3840 result = sgn0 == sgn1;
3843 result = sgn0 != sgn1;
3846 result = sgn0 < sgn1;
3849 result = sgn0 <= sgn1;
3852 result = sgn0 > sgn1;
3855 result = sgn0 >= sgn1;
3861 return constant_boolean_node (result, type);
3864 /* Given EXP, a logical expression, set the range it is testing into
3865 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3866 actually being tested. *PLOW and *PHIGH will be made of the same
3867 type as the returned expression. If EXP is not a comparison, we
3868 will most likely not be returning a useful value and range. Set
3869 *STRICT_OVERFLOW_P to true if the return value is only valid
3870 because signed overflow is undefined; otherwise, do not change
3871 *STRICT_OVERFLOW_P. */
3874 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3875 bool *strict_overflow_p)
3877 enum tree_code code;
3878 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3879 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3881 tree low, high, n_low, n_high;
3882 location_t loc = EXPR_LOCATION (exp);
3884 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3885 and see if we can refine the range. Some of the cases below may not
3886 happen, but it doesn't seem worth worrying about this. We "continue"
3887 the outer loop when we've changed something; otherwise we "break"
3888 the switch, which will "break" the while. */
3891 low = high = build_int_cst (TREE_TYPE (exp), 0);
3895 code = TREE_CODE (exp);
3896 exp_type = TREE_TYPE (exp);
3898 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3900 if (TREE_OPERAND_LENGTH (exp) > 0)
3901 arg0 = TREE_OPERAND (exp, 0);
3902 if (TREE_CODE_CLASS (code) == tcc_comparison
3903 || TREE_CODE_CLASS (code) == tcc_unary
3904 || TREE_CODE_CLASS (code) == tcc_binary)
3905 arg0_type = TREE_TYPE (arg0);
3906 if (TREE_CODE_CLASS (code) == tcc_binary
3907 || TREE_CODE_CLASS (code) == tcc_comparison
3908 || (TREE_CODE_CLASS (code) == tcc_expression
3909 && TREE_OPERAND_LENGTH (exp) > 1))
3910 arg1 = TREE_OPERAND (exp, 1);
3915 case TRUTH_NOT_EXPR:
3916 in_p = ! in_p, exp = arg0;
3919 case EQ_EXPR: case NE_EXPR:
3920 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3921 /* We can only do something if the range is testing for zero
3922 and if the second operand is an integer constant. Note that
3923 saying something is "in" the range we make is done by
3924 complementing IN_P since it will set in the initial case of
3925 being not equal to zero; "out" is leaving it alone. */
3926 if (low == 0 || high == 0
3927 || ! integer_zerop (low) || ! integer_zerop (high)
3928 || TREE_CODE (arg1) != INTEGER_CST)
3933 case NE_EXPR: /* - [c, c] */
3936 case EQ_EXPR: /* + [c, c] */
3937 in_p = ! in_p, low = high = arg1;
3939 case GT_EXPR: /* - [-, c] */
3940 low = 0, high = arg1;
3942 case GE_EXPR: /* + [c, -] */
3943 in_p = ! in_p, low = arg1, high = 0;
3945 case LT_EXPR: /* - [c, -] */
3946 low = arg1, high = 0;
3948 case LE_EXPR: /* + [-, c] */
3949 in_p = ! in_p, low = 0, high = arg1;
3955 /* If this is an unsigned comparison, we also know that EXP is
3956 greater than or equal to zero. We base the range tests we make
3957 on that fact, so we record it here so we can parse existing
3958 range tests. We test arg0_type since often the return type
3959 of, e.g. EQ_EXPR, is boolean. */
3960 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3962 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3964 build_int_cst (arg0_type, 0),
3968 in_p = n_in_p, low = n_low, high = n_high;
3970 /* If the high bound is missing, but we have a nonzero low
3971 bound, reverse the range so it goes from zero to the low bound
3973 if (high == 0 && low && ! integer_zerop (low))
3976 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3977 integer_one_node, 0);
3978 low = build_int_cst (arg0_type, 0);
3986 /* (-x) IN [a,b] -> x in [-b, -a] */
3987 n_low = range_binop (MINUS_EXPR, exp_type,
3988 build_int_cst (exp_type, 0),
3990 n_high = range_binop (MINUS_EXPR, exp_type,
3991 build_int_cst (exp_type, 0),
3993 if (n_high != 0 && TREE_OVERFLOW (n_high))
3999 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4000 build_int_cst (exp_type, 1));
4001 SET_EXPR_LOCATION (exp, loc);
4004 case PLUS_EXPR: case MINUS_EXPR:
4005 if (TREE_CODE (arg1) != INTEGER_CST)
4008 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4009 move a constant to the other side. */
4010 if (!TYPE_UNSIGNED (arg0_type)
4011 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4014 /* If EXP is signed, any overflow in the computation is undefined,
4015 so we don't worry about it so long as our computations on
4016 the bounds don't overflow. For unsigned, overflow is defined
4017 and this is exactly the right thing. */
4018 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4019 arg0_type, low, 0, arg1, 0);
4020 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4021 arg0_type, high, 1, arg1, 0);
4022 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4023 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4026 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4027 *strict_overflow_p = true;
4030 /* Check for an unsigned range which has wrapped around the maximum
4031 value thus making n_high < n_low, and normalize it. */
4032 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4034 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4035 integer_one_node, 0);
4036 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4037 integer_one_node, 0);
4039 /* If the range is of the form +/- [ x+1, x ], we won't
4040 be able to normalize it. But then, it represents the
4041 whole range or the empty set, so make it
4043 if (tree_int_cst_equal (n_low, low)
4044 && tree_int_cst_equal (n_high, high))
4050 low = n_low, high = n_high;
4055 CASE_CONVERT: case NON_LVALUE_EXPR:
4056 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4059 if (! INTEGRAL_TYPE_P (arg0_type)
4060 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4061 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4064 n_low = low, n_high = high;
4067 n_low = fold_convert_loc (loc, arg0_type, n_low);
4070 n_high = fold_convert_loc (loc, arg0_type, n_high);
4073 /* If we're converting arg0 from an unsigned type, to exp,
4074 a signed type, we will be doing the comparison as unsigned.
4075 The tests above have already verified that LOW and HIGH
4078 So we have to ensure that we will handle large unsigned
4079 values the same way that the current signed bounds treat
4082 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4086 /* For fixed-point modes, we need to pass the saturating flag
4087 as the 2nd parameter. */
4088 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4089 equiv_type = lang_hooks.types.type_for_mode
4090 (TYPE_MODE (arg0_type),
4091 TYPE_SATURATING (arg0_type));
4093 equiv_type = lang_hooks.types.type_for_mode
4094 (TYPE_MODE (arg0_type), 1);
4096 /* A range without an upper bound is, naturally, unbounded.
4097 Since convert would have cropped a very large value, use
4098 the max value for the destination type. */
4100 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4101 : TYPE_MAX_VALUE (arg0_type);
4103 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4104 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4105 fold_convert_loc (loc, arg0_type,
4107 build_int_cst (arg0_type, 1));
4109 /* If the low bound is specified, "and" the range with the
4110 range for which the original unsigned value will be
4114 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4115 1, n_low, n_high, 1,
4116 fold_convert_loc (loc, arg0_type,
4121 in_p = (n_in_p == in_p);
4125 /* Otherwise, "or" the range with the range of the input
4126 that will be interpreted as negative. */
4127 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4128 0, n_low, n_high, 1,
4129 fold_convert_loc (loc, arg0_type,
4134 in_p = (in_p != n_in_p);
4139 low = n_low, high = n_high;
4149 /* If EXP is a constant, we can evaluate whether this is true or false. */
4150 if (TREE_CODE (exp) == INTEGER_CST)
4152 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4154 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4160 *pin_p = in_p, *plow = low, *phigh = high;
4164 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4165 type, TYPE, return an expression to test if EXP is in (or out of, depending
4166 on IN_P) the range. Return 0 if the test couldn't be created. */
4169 build_range_check (location_t loc, tree type, tree exp, int in_p,
4170 tree low, tree high)
4172 tree etype = TREE_TYPE (exp), value;
4174 #ifdef HAVE_canonicalize_funcptr_for_compare
4175 /* Disable this optimization for function pointer expressions
4176 on targets that require function pointer canonicalization. */
4177 if (HAVE_canonicalize_funcptr_for_compare
4178 && TREE_CODE (etype) == POINTER_TYPE
4179 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4185 value = build_range_check (loc, type, exp, 1, low, high);
4187 return invert_truthvalue_loc (loc, value);
4192 if (low == 0 && high == 0)
4193 return build_int_cst (type, 1);
4196 return fold_build2_loc (loc, LE_EXPR, type, exp,
4197 fold_convert_loc (loc, etype, high));
4200 return fold_build2_loc (loc, GE_EXPR, type, exp,
4201 fold_convert_loc (loc, etype, low));
4203 if (operand_equal_p (low, high, 0))
4204 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4205 fold_convert_loc (loc, etype, low));
4207 if (integer_zerop (low))
4209 if (! TYPE_UNSIGNED (etype))
4211 etype = unsigned_type_for (etype);
4212 high = fold_convert_loc (loc, etype, high);
4213 exp = fold_convert_loc (loc, etype, exp);
4215 return build_range_check (loc, type, exp, 1, 0, high);
4218 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4219 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4221 unsigned HOST_WIDE_INT lo;
4225 prec = TYPE_PRECISION (etype);
4226 if (prec <= HOST_BITS_PER_WIDE_INT)
4229 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4233 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4234 lo = (unsigned HOST_WIDE_INT) -1;
4237 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4239 if (TYPE_UNSIGNED (etype))
4241 tree signed_etype = signed_type_for (etype);
4242 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4244 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4246 etype = signed_etype;
4247 exp = fold_convert_loc (loc, etype, exp);
4249 return fold_build2_loc (loc, GT_EXPR, type, exp,
4250 build_int_cst (etype, 0));
4254 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4255 This requires wrap-around arithmetics for the type of the expression.
4256 First make sure that arithmetics in this type is valid, then make sure
4257 that it wraps around. */
4258 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4259 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4260 TYPE_UNSIGNED (etype));
4262 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4264 tree utype, minv, maxv;
4266 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4267 for the type in question, as we rely on this here. */
4268 utype = unsigned_type_for (etype);
4269 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4270 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4271 integer_one_node, 1);
4272 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4274 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4281 high = fold_convert_loc (loc, etype, high);
4282 low = fold_convert_loc (loc, etype, low);
4283 exp = fold_convert_loc (loc, etype, exp);
4285 value = const_binop (MINUS_EXPR, high, low);
4288 if (POINTER_TYPE_P (etype))
4290 if (value != 0 && !TREE_OVERFLOW (value))
4292 low = fold_convert_loc (loc, sizetype, low);
4293 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4294 return build_range_check (loc, type,
4295 fold_build2_loc (loc, POINTER_PLUS_EXPR,
4297 1, build_int_cst (etype, 0), value);
4302 if (value != 0 && !TREE_OVERFLOW (value))
4303 return build_range_check (loc, type,
4304 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4305 1, build_int_cst (etype, 0), value);
4310 /* Return the predecessor of VAL in its type, handling the infinite case. */
4313 range_predecessor (tree val)
4315 tree type = TREE_TYPE (val);
4317 if (INTEGRAL_TYPE_P (type)
4318 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4321 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4324 /* Return the successor of VAL in its type, handling the infinite case. */
4327 range_successor (tree val)
4329 tree type = TREE_TYPE (val);
4331 if (INTEGRAL_TYPE_P (type)
4332 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4335 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4338 /* Given two ranges, see if we can merge them into one. Return 1 if we
4339 can, 0 if we can't. Set the output range into the specified parameters. */
4342 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4343 tree high0, int in1_p, tree low1, tree high1)
4351 int lowequal = ((low0 == 0 && low1 == 0)
4352 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4353 low0, 0, low1, 0)));
4354 int highequal = ((high0 == 0 && high1 == 0)
4355 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4356 high0, 1, high1, 1)));
4358 /* Make range 0 be the range that starts first, or ends last if they
4359 start at the same value. Swap them if it isn't. */
4360 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4363 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4364 high1, 1, high0, 1))))
4366 temp = in0_p, in0_p = in1_p, in1_p = temp;
4367 tem = low0, low0 = low1, low1 = tem;
4368 tem = high0, high0 = high1, high1 = tem;
4371 /* Now flag two cases, whether the ranges are disjoint or whether the
4372 second range is totally subsumed in the first. Note that the tests
4373 below are simplified by the ones above. */
4374 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4375 high0, 1, low1, 0));
4376 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4377 high1, 1, high0, 1));
4379 /* We now have four cases, depending on whether we are including or
4380 excluding the two ranges. */
4383 /* If they don't overlap, the result is false. If the second range
4384 is a subset it is the result. Otherwise, the range is from the start
4385 of the second to the end of the first. */
4387 in_p = 0, low = high = 0;
4389 in_p = 1, low = low1, high = high1;
4391 in_p = 1, low = low1, high = high0;
4394 else if (in0_p && ! in1_p)
4396 /* If they don't overlap, the result is the first range. If they are
4397 equal, the result is false. If the second range is a subset of the
4398 first, and the ranges begin at the same place, we go from just after
4399 the end of the second range to the end of the first. If the second
4400 range is not a subset of the first, or if it is a subset and both
4401 ranges end at the same place, the range starts at the start of the
4402 first range and ends just before the second range.
4403 Otherwise, we can't describe this as a single range. */
4405 in_p = 1, low = low0, high = high0;
4406 else if (lowequal && highequal)
4407 in_p = 0, low = high = 0;
4408 else if (subset && lowequal)
4410 low = range_successor (high1);
4415 /* We are in the weird situation where high0 > high1 but
4416 high1 has no successor. Punt. */
4420 else if (! subset || highequal)
4423 high = range_predecessor (low1);
4427 /* low0 < low1 but low1 has no predecessor. Punt. */
4435 else if (! in0_p && in1_p)
4437 /* If they don't overlap, the result is the second range. If the second
4438 is a subset of the first, the result is false. Otherwise,
4439 the range starts just after the first range and ends at the
4440 end of the second. */
4442 in_p = 1, low = low1, high = high1;
4443 else if (subset || highequal)
4444 in_p = 0, low = high = 0;
4447 low = range_successor (high0);
4452 /* high1 > high0 but high0 has no successor. Punt. */
4460 /* The case where we are excluding both ranges. Here the complex case
4461 is if they don't overlap. In that case, the only time we have a
4462 range is if they are adjacent. If the second is a subset of the
4463 first, the result is the first. Otherwise, the range to exclude
4464 starts at the beginning of the first range and ends at the end of the
4468 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4469 range_successor (high0),
4471 in_p = 0, low = low0, high = high1;
4474 /* Canonicalize - [min, x] into - [-, x]. */
4475 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4476 switch (TREE_CODE (TREE_TYPE (low0)))
4479 if (TYPE_PRECISION (TREE_TYPE (low0))
4480 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4484 if (tree_int_cst_equal (low0,
4485 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4489 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4490 && integer_zerop (low0))
4497 /* Canonicalize - [x, max] into - [x, -]. */
4498 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4499 switch (TREE_CODE (TREE_TYPE (high1)))
4502 if (TYPE_PRECISION (TREE_TYPE (high1))
4503 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4507 if (tree_int_cst_equal (high1,
4508 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4512 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4513 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4515 integer_one_node, 1)))
4522 /* The ranges might be also adjacent between the maximum and
4523 minimum values of the given type. For
4524 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4525 return + [x + 1, y - 1]. */
4526 if (low0 == 0 && high1 == 0)
4528 low = range_successor (high0);
4529 high = range_predecessor (low1);
4530 if (low == 0 || high == 0)
4540 in_p = 0, low = low0, high = high0;
4542 in_p = 0, low = low0, high = high1;
4545 *pin_p = in_p, *plow = low, *phigh = high;
4550 /* Subroutine of fold, looking inside expressions of the form
4551 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4552 of the COND_EXPR. This function is being used also to optimize
4553 A op B ? C : A, by reversing the comparison first.
4555 Return a folded expression whose code is not a COND_EXPR
4556 anymore, or NULL_TREE if no folding opportunity is found. */
4559 fold_cond_expr_with_comparison (location_t loc, tree type,
4560 tree arg0, tree arg1, tree arg2)
4562 enum tree_code comp_code = TREE_CODE (arg0);
4563 tree arg00 = TREE_OPERAND (arg0, 0);
4564 tree arg01 = TREE_OPERAND (arg0, 1);
4565 tree arg1_type = TREE_TYPE (arg1);
4571 /* If we have A op 0 ? A : -A, consider applying the following
4574 A == 0? A : -A same as -A
4575 A != 0? A : -A same as A
4576 A >= 0? A : -A same as abs (A)
4577 A > 0? A : -A same as abs (A)
4578 A <= 0? A : -A same as -abs (A)
4579 A < 0? A : -A same as -abs (A)
4581 None of these transformations work for modes with signed
4582 zeros. If A is +/-0, the first two transformations will
4583 change the sign of the result (from +0 to -0, or vice
4584 versa). The last four will fix the sign of the result,
4585 even though the original expressions could be positive or
4586 negative, depending on the sign of A.
4588 Note that all these transformations are correct if A is
4589 NaN, since the two alternatives (A and -A) are also NaNs. */
4590 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4591 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4592 ? real_zerop (arg01)
4593 : integer_zerop (arg01))
4594 && ((TREE_CODE (arg2) == NEGATE_EXPR
4595 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4596 /* In the case that A is of the form X-Y, '-A' (arg2) may
4597 have already been folded to Y-X, check for that. */
4598 || (TREE_CODE (arg1) == MINUS_EXPR
4599 && TREE_CODE (arg2) == MINUS_EXPR
4600 && operand_equal_p (TREE_OPERAND (arg1, 0),
4601 TREE_OPERAND (arg2, 1), 0)
4602 && operand_equal_p (TREE_OPERAND (arg1, 1),
4603 TREE_OPERAND (arg2, 0), 0))))
4608 tem = fold_convert_loc (loc, arg1_type, arg1);
4609 return pedantic_non_lvalue_loc (loc,
4610 fold_convert_loc (loc, type,
4611 negate_expr (tem)));
4614 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4617 if (flag_trapping_math)
4622 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4623 arg1 = fold_convert_loc (loc, signed_type_for
4624 (TREE_TYPE (arg1)), arg1);
4625 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4626 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4629 if (flag_trapping_math)
4633 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4634 arg1 = fold_convert_loc (loc, signed_type_for
4635 (TREE_TYPE (arg1)), arg1);
4636 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4637 return negate_expr (fold_convert_loc (loc, type, tem));
4639 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4643 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4644 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4645 both transformations are correct when A is NaN: A != 0
4646 is then true, and A == 0 is false. */
4648 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4649 && integer_zerop (arg01) && integer_zerop (arg2))
4651 if (comp_code == NE_EXPR)
4652 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4653 else if (comp_code == EQ_EXPR)
4654 return build_int_cst (type, 0);
4657 /* Try some transformations of A op B ? A : B.
4659 A == B? A : B same as B
4660 A != B? A : B same as A
4661 A >= B? A : B same as max (A, B)
4662 A > B? A : B same as max (B, A)
4663 A <= B? A : B same as min (A, B)
4664 A < B? A : B same as min (B, A)
4666 As above, these transformations don't work in the presence
4667 of signed zeros. For example, if A and B are zeros of
4668 opposite sign, the first two transformations will change
4669 the sign of the result. In the last four, the original
4670 expressions give different results for (A=+0, B=-0) and
4671 (A=-0, B=+0), but the transformed expressions do not.
4673 The first two transformations are correct if either A or B
4674 is a NaN. In the first transformation, the condition will
4675 be false, and B will indeed be chosen. In the case of the
4676 second transformation, the condition A != B will be true,
4677 and A will be chosen.
4679 The conversions to max() and min() are not correct if B is
4680 a number and A is not. The conditions in the original
4681 expressions will be false, so all four give B. The min()
4682 and max() versions would give a NaN instead. */
4683 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4684 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4685 /* Avoid these transformations if the COND_EXPR may be used
4686 as an lvalue in the C++ front-end. PR c++/19199. */
4688 || (strcmp (lang_hooks.name, "GNU C++") != 0
4689 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4690 || ! maybe_lvalue_p (arg1)
4691 || ! maybe_lvalue_p (arg2)))
4693 tree comp_op0 = arg00;
4694 tree comp_op1 = arg01;
4695 tree comp_type = TREE_TYPE (comp_op0);
4697 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4698 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4708 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4710 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4715 /* In C++ a ?: expression can be an lvalue, so put the
4716 operand which will be used if they are equal first
4717 so that we can convert this back to the
4718 corresponding COND_EXPR. */
4719 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4721 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4722 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4723 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4724 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4725 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4726 comp_op1, comp_op0);
4727 return pedantic_non_lvalue_loc (loc,
4728 fold_convert_loc (loc, type, tem));
4735 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4737 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4738 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4739 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4740 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4741 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4742 comp_op1, comp_op0);
4743 return pedantic_non_lvalue_loc (loc,
4744 fold_convert_loc (loc, type, tem));
4748 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4749 return pedantic_non_lvalue_loc (loc,
4750 fold_convert_loc (loc, type, arg2));
4753 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4754 return pedantic_non_lvalue_loc (loc,
4755 fold_convert_loc (loc, type, arg1));
4758 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4763 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4764 we might still be able to simplify this. For example,
4765 if C1 is one less or one more than C2, this might have started
4766 out as a MIN or MAX and been transformed by this function.
4767 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4769 if (INTEGRAL_TYPE_P (type)
4770 && TREE_CODE (arg01) == INTEGER_CST
4771 && TREE_CODE (arg2) == INTEGER_CST)
4775 if (TREE_CODE (arg1) == INTEGER_CST)
4777 /* We can replace A with C1 in this case. */
4778 arg1 = fold_convert_loc (loc, type, arg01);
4779 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4782 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4783 MIN_EXPR, to preserve the signedness of the comparison. */
4784 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4786 && operand_equal_p (arg01,
4787 const_binop (PLUS_EXPR, arg2,
4788 build_int_cst (type, 1)),
4791 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4792 fold_convert_loc (loc, TREE_TYPE (arg00),
4794 return pedantic_non_lvalue_loc (loc,
4795 fold_convert_loc (loc, type, tem));
4800 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4802 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4804 && operand_equal_p (arg01,
4805 const_binop (MINUS_EXPR, arg2,
4806 build_int_cst (type, 1)),
4809 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4810 fold_convert_loc (loc, TREE_TYPE (arg00),
4812 return pedantic_non_lvalue_loc (loc,
4813 fold_convert_loc (loc, type, tem));
4818 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4819 MAX_EXPR, to preserve the signedness of the comparison. */
4820 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4822 && operand_equal_p (arg01,
4823 const_binop (MINUS_EXPR, arg2,
4824 build_int_cst (type, 1)),
4827 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4828 fold_convert_loc (loc, TREE_TYPE (arg00),
4830 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4835 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4836 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4838 && operand_equal_p (arg01,
4839 const_binop (PLUS_EXPR, arg2,
4840 build_int_cst (type, 1)),
4843 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4844 fold_convert_loc (loc, TREE_TYPE (arg00),
4846 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4860 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4861 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4862 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4866 /* EXP is some logical combination of boolean tests. See if we can
4867 merge it into some range test. Return the new tree if so. */
4870 fold_range_test (location_t loc, enum tree_code code, tree type,
4873 int or_op = (code == TRUTH_ORIF_EXPR
4874 || code == TRUTH_OR_EXPR);
4875 int in0_p, in1_p, in_p;
4876 tree low0, low1, low, high0, high1, high;
4877 bool strict_overflow_p = false;
4878 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4879 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4881 const char * const warnmsg = G_("assuming signed overflow does not occur "
4882 "when simplifying range test");
4884 /* If this is an OR operation, invert both sides; we will invert
4885 again at the end. */
4887 in0_p = ! in0_p, in1_p = ! in1_p;
4889 /* If both expressions are the same, if we can merge the ranges, and we
4890 can build the range test, return it or it inverted. If one of the
4891 ranges is always true or always false, consider it to be the same
4892 expression as the other. */
4893 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4894 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4896 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4898 : rhs != 0 ? rhs : integer_zero_node,
4901 if (strict_overflow_p)
4902 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4903 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4906 /* On machines where the branch cost is expensive, if this is a
4907 short-circuited branch and the underlying object on both sides
4908 is the same, make a non-short-circuit operation. */
4909 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4910 && lhs != 0 && rhs != 0
4911 && (code == TRUTH_ANDIF_EXPR
4912 || code == TRUTH_ORIF_EXPR)
4913 && operand_equal_p (lhs, rhs, 0))
4915 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4916 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4917 which cases we can't do this. */
4918 if (simple_operand_p (lhs))
4920 tem = build2 (code == TRUTH_ANDIF_EXPR
4921 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4923 SET_EXPR_LOCATION (tem, loc);
4927 else if (lang_hooks.decls.global_bindings_p () == 0
4928 && ! CONTAINS_PLACEHOLDER_P (lhs))
4930 tree common = save_expr (lhs);
4932 if (0 != (lhs = build_range_check (loc, type, common,
4933 or_op ? ! in0_p : in0_p,
4935 && (0 != (rhs = build_range_check (loc, type, common,
4936 or_op ? ! in1_p : in1_p,
4939 if (strict_overflow_p)
4940 fold_overflow_warning (warnmsg,
4941 WARN_STRICT_OVERFLOW_COMPARISON);
4942 tem = build2 (code == TRUTH_ANDIF_EXPR
4943 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4945 SET_EXPR_LOCATION (tem, loc);
4954 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4955 bit value. Arrange things so the extra bits will be set to zero if and
4956 only if C is signed-extended to its full width. If MASK is nonzero,
4957 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4960 unextend (tree c, int p, int unsignedp, tree mask)
4962 tree type = TREE_TYPE (c);
4963 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4966 if (p == modesize || unsignedp)
4969 /* We work by getting just the sign bit into the low-order bit, then
4970 into the high-order bit, then sign-extend. We then XOR that value
4972 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4973 temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4975 /* We must use a signed type in order to get an arithmetic right shift.
4976 However, we must also avoid introducing accidental overflows, so that
4977 a subsequent call to integer_zerop will work. Hence we must
4978 do the type conversion here. At this point, the constant is either
4979 zero or one, and the conversion to a signed type can never overflow.
4980 We could get an overflow if this conversion is done anywhere else. */
4981 if (TYPE_UNSIGNED (type))
4982 temp = fold_convert (signed_type_for (type), temp);
4984 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4985 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4987 temp = const_binop (BIT_AND_EXPR, temp,
4988 fold_convert (TREE_TYPE (c), mask));
4989 /* If necessary, convert the type back to match the type of C. */
4990 if (TYPE_UNSIGNED (type))
4991 temp = fold_convert (type, temp);
4993 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4996 /* For an expression that has the form
5000 we can drop one of the inner expressions and simplify to
5004 LOC is the location of the resulting expression. OP is the inner
5005 logical operation; the left-hand side in the examples above, while CMPOP
5006 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5007 removing a condition that guards another, as in
5008 (A != NULL && A->...) || A == NULL
5009 which we must not transform. If RHS_ONLY is true, only eliminate the
5010 right-most operand of the inner logical operation. */
5013 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5016 tree type = TREE_TYPE (cmpop);
5017 enum tree_code code = TREE_CODE (cmpop);
5018 enum tree_code truthop_code = TREE_CODE (op);
5019 tree lhs = TREE_OPERAND (op, 0);
5020 tree rhs = TREE_OPERAND (op, 1);
5021 tree orig_lhs = lhs, orig_rhs = rhs;
5022 enum tree_code rhs_code = TREE_CODE (rhs);
5023 enum tree_code lhs_code = TREE_CODE (lhs);
5024 enum tree_code inv_code;
5026 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5029 if (TREE_CODE_CLASS (code) != tcc_comparison)
5032 if (rhs_code == truthop_code)
5034 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5035 if (newrhs != NULL_TREE)
5038 rhs_code = TREE_CODE (rhs);
5041 if (lhs_code == truthop_code && !rhs_only)
5043 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5044 if (newlhs != NULL_TREE)
5047 lhs_code = TREE_CODE (lhs);
5051 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5052 if (inv_code == rhs_code
5053 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5054 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5056 if (!rhs_only && inv_code == lhs_code
5057 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5058 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5060 if (rhs != orig_rhs || lhs != orig_lhs)
5061 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5066 /* Find ways of folding logical expressions of LHS and RHS:
5067 Try to merge two comparisons to the same innermost item.
5068 Look for range tests like "ch >= '0' && ch <= '9'".
5069 Look for combinations of simple terms on machines with expensive branches
5070 and evaluate the RHS unconditionally.
5072 For example, if we have p->a == 2 && p->b == 4 and we can make an
5073 object large enough to span both A and B, we can do this with a comparison
5074 against the object ANDed with the a mask.
5076 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5077 operations to do this with one comparison.
5079 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5080 function and the one above.
5082 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5083 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5085 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5088 We return the simplified tree or 0 if no optimization is possible. */
5091 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5094 /* If this is the "or" of two comparisons, we can do something if
5095 the comparisons are NE_EXPR. If this is the "and", we can do something
5096 if the comparisons are EQ_EXPR. I.e.,
5097 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5099 WANTED_CODE is this operation code. For single bit fields, we can
5100 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5101 comparison for one-bit fields. */
5103 enum tree_code wanted_code;
5104 enum tree_code lcode, rcode;
5105 tree ll_arg, lr_arg, rl_arg, rr_arg;
5106 tree ll_inner, lr_inner, rl_inner, rr_inner;
5107 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5108 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5109 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5110 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5111 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5112 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5113 enum machine_mode lnmode, rnmode;
5114 tree ll_mask, lr_mask, rl_mask, rr_mask;
5115 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5116 tree l_const, r_const;
5117 tree lntype, rntype, result;
5118 HOST_WIDE_INT first_bit, end_bit;
5120 tree orig_lhs = lhs, orig_rhs = rhs;
5121 enum tree_code orig_code = code;
5123 /* Start by getting the comparison codes. Fail if anything is volatile.
5124 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5125 it were surrounded with a NE_EXPR. */
5127 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5130 lcode = TREE_CODE (lhs);
5131 rcode = TREE_CODE (rhs);
5133 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5135 lhs = build2 (NE_EXPR, truth_type, lhs,
5136 build_int_cst (TREE_TYPE (lhs), 0));
5140 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5142 rhs = build2 (NE_EXPR, truth_type, rhs,
5143 build_int_cst (TREE_TYPE (rhs), 0));
5147 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5148 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5151 ll_arg = TREE_OPERAND (lhs, 0);
5152 lr_arg = TREE_OPERAND (lhs, 1);
5153 rl_arg = TREE_OPERAND (rhs, 0);
5154 rr_arg = TREE_OPERAND (rhs, 1);
5156 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5157 if (simple_operand_p (ll_arg)
5158 && simple_operand_p (lr_arg))
5161 if (operand_equal_p (ll_arg, rl_arg, 0)
5162 && operand_equal_p (lr_arg, rr_arg, 0))
5164 result = combine_comparisons (loc, code, lcode, rcode,
5165 truth_type, ll_arg, lr_arg);
5169 else if (operand_equal_p (ll_arg, rr_arg, 0)
5170 && operand_equal_p (lr_arg, rl_arg, 0))
5172 result = combine_comparisons (loc, code, lcode,
5173 swap_tree_comparison (rcode),
5174 truth_type, ll_arg, lr_arg);
5180 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5181 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5183 /* If the RHS can be evaluated unconditionally and its operands are
5184 simple, it wins to evaluate the RHS unconditionally on machines
5185 with expensive branches. In this case, this isn't a comparison
5186 that can be merged. Avoid doing this if the RHS is a floating-point
5187 comparison since those can trap. */
5189 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5191 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5192 && simple_operand_p (rl_arg)
5193 && simple_operand_p (rr_arg))
5195 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5196 if (code == TRUTH_OR_EXPR
5197 && lcode == NE_EXPR && integer_zerop (lr_arg)
5198 && rcode == NE_EXPR && integer_zerop (rr_arg)
5199 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5200 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5202 result = build2 (NE_EXPR, truth_type,
5203 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5205 build_int_cst (TREE_TYPE (ll_arg), 0));
5206 goto fold_truthop_exit;
5209 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5210 if (code == TRUTH_AND_EXPR
5211 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5212 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5213 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5214 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5216 result = build2 (EQ_EXPR, truth_type,
5217 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5219 build_int_cst (TREE_TYPE (ll_arg), 0));
5220 goto fold_truthop_exit;
5223 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5225 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5227 result = build2 (code, truth_type, lhs, rhs);
5228 goto fold_truthop_exit;
5234 /* See if the comparisons can be merged. Then get all the parameters for
5237 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5238 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5242 ll_inner = decode_field_reference (loc, ll_arg,
5243 &ll_bitsize, &ll_bitpos, &ll_mode,
5244 &ll_unsignedp, &volatilep, &ll_mask,
5246 lr_inner = decode_field_reference (loc, lr_arg,
5247 &lr_bitsize, &lr_bitpos, &lr_mode,
5248 &lr_unsignedp, &volatilep, &lr_mask,
5250 rl_inner = decode_field_reference (loc, rl_arg,
5251 &rl_bitsize, &rl_bitpos, &rl_mode,
5252 &rl_unsignedp, &volatilep, &rl_mask,
5254 rr_inner = decode_field_reference (loc, rr_arg,
5255 &rr_bitsize, &rr_bitpos, &rr_mode,
5256 &rr_unsignedp, &volatilep, &rr_mask,
5259 /* It must be true that the inner operation on the lhs of each
5260 comparison must be the same if we are to be able to do anything.
5261 Then see if we have constants. If not, the same must be true for
5263 if (volatilep || ll_inner == 0 || rl_inner == 0
5264 || ! operand_equal_p (ll_inner, rl_inner, 0))
5267 if (TREE_CODE (lr_arg) == INTEGER_CST
5268 && TREE_CODE (rr_arg) == INTEGER_CST)
5269 l_const = lr_arg, r_const = rr_arg;
5270 else if (lr_inner == 0 || rr_inner == 0
5271 || ! operand_equal_p (lr_inner, rr_inner, 0))
5274 l_const = r_const = 0;
5276 /* If either comparison code is not correct for our logical operation,
5277 fail. However, we can convert a one-bit comparison against zero into
5278 the opposite comparison against that bit being set in the field. */
5280 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5281 if (lcode != wanted_code)
5283 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5285 /* Make the left operand unsigned, since we are only interested
5286 in the value of one bit. Otherwise we are doing the wrong
5295 /* This is analogous to the code for l_const above. */
5296 if (rcode != wanted_code)
5298 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5307 /* See if we can find a mode that contains both fields being compared on
5308 the left. If we can't, fail. Otherwise, update all constants and masks
5309 to be relative to a field of that size. */
5310 first_bit = MIN (ll_bitpos, rl_bitpos);
5311 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5312 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5313 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5315 if (lnmode == VOIDmode)
5318 lnbitsize = GET_MODE_BITSIZE (lnmode);
5319 lnbitpos = first_bit & ~ (lnbitsize - 1);
5320 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5321 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5323 if (BYTES_BIG_ENDIAN)
5325 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5326 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5329 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5330 size_int (xll_bitpos));
5331 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5332 size_int (xrl_bitpos));
5336 l_const = fold_convert_loc (loc, lntype, l_const);
5337 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5338 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5339 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5340 fold_build1_loc (loc, BIT_NOT_EXPR,
5343 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5345 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5350 r_const = fold_convert_loc (loc, lntype, r_const);
5351 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5352 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5353 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5354 fold_build1_loc (loc, BIT_NOT_EXPR,
5357 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5359 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5363 /* If the right sides are not constant, do the same for it. Also,
5364 disallow this optimization if a size or signedness mismatch occurs
5365 between the left and right sides. */
5368 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5369 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5370 /* Make sure the two fields on the right
5371 correspond to the left without being swapped. */
5372 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5375 first_bit = MIN (lr_bitpos, rr_bitpos);
5376 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5377 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5378 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5380 if (rnmode == VOIDmode)
5383 rnbitsize = GET_MODE_BITSIZE (rnmode);
5384 rnbitpos = first_bit & ~ (rnbitsize - 1);
5385 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5386 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5388 if (BYTES_BIG_ENDIAN)
5390 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5391 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5394 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5396 size_int (xlr_bitpos));
5397 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5399 size_int (xrr_bitpos));
5401 /* Make a mask that corresponds to both fields being compared.
5402 Do this for both items being compared. If the operands are the
5403 same size and the bits being compared are in the same position
5404 then we can do this by masking both and comparing the masked
5406 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5407 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5408 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5410 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5411 ll_unsignedp || rl_unsignedp);
5412 if (! all_ones_mask_p (ll_mask, lnbitsize))
5413 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5415 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5416 lr_unsignedp || rr_unsignedp);
5417 if (! all_ones_mask_p (lr_mask, rnbitsize))
5418 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5420 result = build2 (wanted_code, truth_type, lhs, rhs);
5421 goto fold_truthop_exit;
5424 /* There is still another way we can do something: If both pairs of
5425 fields being compared are adjacent, we may be able to make a wider
5426 field containing them both.
5428 Note that we still must mask the lhs/rhs expressions. Furthermore,
5429 the mask must be shifted to account for the shift done by
5430 make_bit_field_ref. */
5431 if ((ll_bitsize + ll_bitpos == rl_bitpos
5432 && lr_bitsize + lr_bitpos == rr_bitpos)
5433 || (ll_bitpos == rl_bitpos + rl_bitsize
5434 && lr_bitpos == rr_bitpos + rr_bitsize))
5438 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5439 ll_bitsize + rl_bitsize,
5440 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5441 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5442 lr_bitsize + rr_bitsize,
5443 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5445 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5446 size_int (MIN (xll_bitpos, xrl_bitpos)));
5447 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5448 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5450 /* Convert to the smaller type before masking out unwanted bits. */
5452 if (lntype != rntype)
5454 if (lnbitsize > rnbitsize)
5456 lhs = fold_convert_loc (loc, rntype, lhs);
5457 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5460 else if (lnbitsize < rnbitsize)
5462 rhs = fold_convert_loc (loc, lntype, rhs);
5463 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5468 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5469 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5471 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5472 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5474 result = build2 (wanted_code, truth_type, lhs, rhs);
5475 goto fold_truthop_exit;
5481 /* Handle the case of comparisons with constants. If there is something in
5482 common between the masks, those bits of the constants must be the same.
5483 If not, the condition is always false. Test for this to avoid generating
5484 incorrect code below. */
5485 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5486 if (! integer_zerop (result)
5487 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5488 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5490 if (wanted_code == NE_EXPR)
5492 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5493 return constant_boolean_node (true, truth_type);
5497 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5498 return constant_boolean_node (false, truth_type);
5502 /* Construct the expression we will return. First get the component
5503 reference we will make. Unless the mask is all ones the width of
5504 that field, perform the mask operation. Then compare with the
5506 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5507 ll_unsignedp || rl_unsignedp);
5509 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5510 if (! all_ones_mask_p (ll_mask, lnbitsize))
5512 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5513 SET_EXPR_LOCATION (result, loc);
5516 result = build2 (wanted_code, truth_type, result,
5517 const_binop (BIT_IOR_EXPR, l_const, r_const));
5520 SET_EXPR_LOCATION (result, loc);
5524 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5528 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5532 enum tree_code op_code;
5535 int consts_equal, consts_lt;
5538 STRIP_SIGN_NOPS (arg0);
5540 op_code = TREE_CODE (arg0);
5541 minmax_const = TREE_OPERAND (arg0, 1);
5542 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5543 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5544 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5545 inner = TREE_OPERAND (arg0, 0);
5547 /* If something does not permit us to optimize, return the original tree. */
5548 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5549 || TREE_CODE (comp_const) != INTEGER_CST
5550 || TREE_OVERFLOW (comp_const)
5551 || TREE_CODE (minmax_const) != INTEGER_CST
5552 || TREE_OVERFLOW (minmax_const))
5555 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5556 and GT_EXPR, doing the rest with recursive calls using logical
5560 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5563 = optimize_minmax_comparison (loc,
5564 invert_tree_comparison (code, false),
5567 return invert_truthvalue_loc (loc, tem);
5573 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5574 optimize_minmax_comparison
5575 (loc, EQ_EXPR, type, arg0, comp_const),
5576 optimize_minmax_comparison
5577 (loc, GT_EXPR, type, arg0, comp_const));
5580 if (op_code == MAX_EXPR && consts_equal)
5581 /* MAX (X, 0) == 0 -> X <= 0 */
5582 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5584 else if (op_code == MAX_EXPR && consts_lt)
5585 /* MAX (X, 0) == 5 -> X == 5 */
5586 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5588 else if (op_code == MAX_EXPR)
5589 /* MAX (X, 0) == -1 -> false */
5590 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5592 else if (consts_equal)
5593 /* MIN (X, 0) == 0 -> X >= 0 */
5594 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5597 /* MIN (X, 0) == 5 -> false */
5598 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5601 /* MIN (X, 0) == -1 -> X == -1 */
5602 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5605 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5606 /* MAX (X, 0) > 0 -> X > 0
5607 MAX (X, 0) > 5 -> X > 5 */
5608 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5610 else if (op_code == MAX_EXPR)
5611 /* MAX (X, 0) > -1 -> true */
5612 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5614 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5615 /* MIN (X, 0) > 0 -> false
5616 MIN (X, 0) > 5 -> false */
5617 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5620 /* MIN (X, 0) > -1 -> X > -1 */
5621 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5628 /* T is an integer expression that is being multiplied, divided, or taken a
5629 modulus (CODE says which and what kind of divide or modulus) by a
5630 constant C. See if we can eliminate that operation by folding it with
5631 other operations already in T. WIDE_TYPE, if non-null, is a type that
5632 should be used for the computation if wider than our type.
5634 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5635 (X * 2) + (Y * 4). We must, however, be assured that either the original
5636 expression would not overflow or that overflow is undefined for the type
5637 in the language in question.
5639 If we return a non-null expression, it is an equivalent form of the
5640 original computation, but need not be in the original type.
5642 We set *STRICT_OVERFLOW_P to true if the return values depends on
5643 signed overflow being undefined. Otherwise we do not change
5644 *STRICT_OVERFLOW_P. */
5647 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5648 bool *strict_overflow_p)
5650 /* To avoid exponential search depth, refuse to allow recursion past
5651 three levels. Beyond that (1) it's highly unlikely that we'll find
5652 something interesting and (2) we've probably processed it before
5653 when we built the inner expression. */
5662 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5669 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5670 bool *strict_overflow_p)
5672 tree type = TREE_TYPE (t);
5673 enum tree_code tcode = TREE_CODE (t);
5674 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5675 > GET_MODE_SIZE (TYPE_MODE (type)))
5676 ? wide_type : type);
5678 int same_p = tcode == code;
5679 tree op0 = NULL_TREE, op1 = NULL_TREE;
5680 bool sub_strict_overflow_p;
5682 /* Don't deal with constants of zero here; they confuse the code below. */
5683 if (integer_zerop (c))
5686 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5687 op0 = TREE_OPERAND (t, 0);
5689 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5690 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5692 /* Note that we need not handle conditional operations here since fold
5693 already handles those cases. So just do arithmetic here. */
5697 /* For a constant, we can always simplify if we are a multiply
5698 or (for divide and modulus) if it is a multiple of our constant. */
5699 if (code == MULT_EXPR
5700 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5701 return const_binop (code, fold_convert (ctype, t),
5702 fold_convert (ctype, c));
5705 CASE_CONVERT: case NON_LVALUE_EXPR:
5706 /* If op0 is an expression ... */
5707 if ((COMPARISON_CLASS_P (op0)
5708 || UNARY_CLASS_P (op0)
5709 || BINARY_CLASS_P (op0)
5710 || VL_EXP_CLASS_P (op0)
5711 || EXPRESSION_CLASS_P (op0))
5712 /* ... and has wrapping overflow, and its type is smaller
5713 than ctype, then we cannot pass through as widening. */
5714 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5715 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5716 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5717 && (TYPE_PRECISION (ctype)
5718 > TYPE_PRECISION (TREE_TYPE (op0))))
5719 /* ... or this is a truncation (t is narrower than op0),
5720 then we cannot pass through this narrowing. */
5721 || (TYPE_PRECISION (type)
5722 < TYPE_PRECISION (TREE_TYPE (op0)))
5723 /* ... or signedness changes for division or modulus,
5724 then we cannot pass through this conversion. */
5725 || (code != MULT_EXPR
5726 && (TYPE_UNSIGNED (ctype)
5727 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5728 /* ... or has undefined overflow while the converted to
5729 type has not, we cannot do the operation in the inner type
5730 as that would introduce undefined overflow. */
5731 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5732 && !TYPE_OVERFLOW_UNDEFINED (type))))
5735 /* Pass the constant down and see if we can make a simplification. If
5736 we can, replace this expression with the inner simplification for
5737 possible later conversion to our or some other type. */
5738 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5739 && TREE_CODE (t2) == INTEGER_CST
5740 && !TREE_OVERFLOW (t2)
5741 && (0 != (t1 = extract_muldiv (op0, t2, code,
5743 ? ctype : NULL_TREE,
5744 strict_overflow_p))))
5749 /* If widening the type changes it from signed to unsigned, then we
5750 must avoid building ABS_EXPR itself as unsigned. */
5751 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5753 tree cstype = (*signed_type_for) (ctype);
5754 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5757 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5758 return fold_convert (ctype, t1);
5762 /* If the constant is negative, we cannot simplify this. */
5763 if (tree_int_cst_sgn (c) == -1)
5767 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5769 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5772 case MIN_EXPR: case MAX_EXPR:
5773 /* If widening the type changes the signedness, then we can't perform
5774 this optimization as that changes the result. */
5775 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5778 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5779 sub_strict_overflow_p = false;
5780 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5781 &sub_strict_overflow_p)) != 0
5782 && (t2 = extract_muldiv (op1, c, code, wide_type,
5783 &sub_strict_overflow_p)) != 0)
5785 if (tree_int_cst_sgn (c) < 0)
5786 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5787 if (sub_strict_overflow_p)
5788 *strict_overflow_p = true;
5789 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5790 fold_convert (ctype, t2));
5794 case LSHIFT_EXPR: case RSHIFT_EXPR:
5795 /* If the second operand is constant, this is a multiplication
5796 or floor division, by a power of two, so we can treat it that
5797 way unless the multiplier or divisor overflows. Signed
5798 left-shift overflow is implementation-defined rather than
5799 undefined in C90, so do not convert signed left shift into
5801 if (TREE_CODE (op1) == INTEGER_CST
5802 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5803 /* const_binop may not detect overflow correctly,
5804 so check for it explicitly here. */
5805 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5806 && TREE_INT_CST_HIGH (op1) == 0
5807 && 0 != (t1 = fold_convert (ctype,
5808 const_binop (LSHIFT_EXPR,
5811 && !TREE_OVERFLOW (t1))
5812 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5813 ? MULT_EXPR : FLOOR_DIV_EXPR,
5815 fold_convert (ctype, op0),
5817 c, code, wide_type, strict_overflow_p);
5820 case PLUS_EXPR: case MINUS_EXPR:
5821 /* See if we can eliminate the operation on both sides. If we can, we
5822 can return a new PLUS or MINUS. If we can't, the only remaining
5823 cases where we can do anything are if the second operand is a
5825 sub_strict_overflow_p = false;
5826 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5827 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5828 if (t1 != 0 && t2 != 0
5829 && (code == MULT_EXPR
5830 /* If not multiplication, we can only do this if both operands
5831 are divisible by c. */
5832 || (multiple_of_p (ctype, op0, c)
5833 && multiple_of_p (ctype, op1, c))))
5835 if (sub_strict_overflow_p)
5836 *strict_overflow_p = true;
5837 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5838 fold_convert (ctype, t2));
5841 /* If this was a subtraction, negate OP1 and set it to be an addition.
5842 This simplifies the logic below. */
5843 if (tcode == MINUS_EXPR)
5845 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5846 /* If OP1 was not easily negatable, the constant may be OP0. */
5847 if (TREE_CODE (op0) == INTEGER_CST)
5858 if (TREE_CODE (op1) != INTEGER_CST)
5861 /* If either OP1 or C are negative, this optimization is not safe for
5862 some of the division and remainder types while for others we need
5863 to change the code. */
5864 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5866 if (code == CEIL_DIV_EXPR)
5867 code = FLOOR_DIV_EXPR;
5868 else if (code == FLOOR_DIV_EXPR)
5869 code = CEIL_DIV_EXPR;
5870 else if (code != MULT_EXPR
5871 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5875 /* If it's a multiply or a division/modulus operation of a multiple
5876 of our constant, do the operation and verify it doesn't overflow. */
5877 if (code == MULT_EXPR
5878 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5880 op1 = const_binop (code, fold_convert (ctype, op1),
5881 fold_convert (ctype, c));
5882 /* We allow the constant to overflow with wrapping semantics. */
5884 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5890 /* If we have an unsigned type is not a sizetype, we cannot widen
5891 the operation since it will change the result if the original
5892 computation overflowed. */
5893 if (TYPE_UNSIGNED (ctype)
5894 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5898 /* If we were able to eliminate our operation from the first side,
5899 apply our operation to the second side and reform the PLUS. */
5900 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5901 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5903 /* The last case is if we are a multiply. In that case, we can
5904 apply the distributive law to commute the multiply and addition
5905 if the multiplication of the constants doesn't overflow. */
5906 if (code == MULT_EXPR)
5907 return fold_build2 (tcode, ctype,
5908 fold_build2 (code, ctype,
5909 fold_convert (ctype, op0),
5910 fold_convert (ctype, c)),
5916 /* We have a special case here if we are doing something like
5917 (C * 8) % 4 since we know that's zero. */
5918 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5919 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5920 /* If the multiplication can overflow we cannot optimize this.
5921 ??? Until we can properly mark individual operations as
5922 not overflowing we need to treat sizetype special here as
5923 stor-layout relies on this opimization to make
5924 DECL_FIELD_BIT_OFFSET always a constant. */
5925 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5926 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5927 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5928 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5929 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5931 *strict_overflow_p = true;
5932 return omit_one_operand (type, integer_zero_node, op0);
5935 /* ... fall through ... */
5937 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5938 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5939 /* If we can extract our operation from the LHS, do so and return a
5940 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5941 do something only if the second operand is a constant. */
5943 && (t1 = extract_muldiv (op0, c, code, wide_type,
5944 strict_overflow_p)) != 0)
5945 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5946 fold_convert (ctype, op1));
5947 else if (tcode == MULT_EXPR && code == MULT_EXPR
5948 && (t1 = extract_muldiv (op1, c, code, wide_type,
5949 strict_overflow_p)) != 0)
5950 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5951 fold_convert (ctype, t1));
5952 else if (TREE_CODE (op1) != INTEGER_CST)
5955 /* If these are the same operation types, we can associate them
5956 assuming no overflow. */
5958 && 0 != (t1 = int_const_binop (MULT_EXPR,
5959 fold_convert (ctype, op1),
5960 fold_convert (ctype, c), 1))
5961 && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5962 (TYPE_UNSIGNED (ctype)
5963 && tcode != MULT_EXPR) ? -1 : 1,
5964 TREE_OVERFLOW (t1)))
5965 && !TREE_OVERFLOW (t1))
5966 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5968 /* If these operations "cancel" each other, we have the main
5969 optimizations of this pass, which occur when either constant is a
5970 multiple of the other, in which case we replace this with either an
5971 operation or CODE or TCODE.
5973 If we have an unsigned type that is not a sizetype, we cannot do
5974 this since it will change the result if the original computation
5976 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5977 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5978 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5979 || (tcode == MULT_EXPR
5980 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5981 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5982 && code != MULT_EXPR)))
5984 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5986 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5987 *strict_overflow_p = true;
5988 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5989 fold_convert (ctype,
5990 const_binop (TRUNC_DIV_EXPR,
5993 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5995 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5996 *strict_overflow_p = true;
5997 return fold_build2 (code, ctype, fold_convert (ctype, op0),
5998 fold_convert (ctype,
5999 const_binop (TRUNC_DIV_EXPR,
6012 /* Return a node which has the indicated constant VALUE (either 0 or
6013 1), and is of the indicated TYPE. */
6016 constant_boolean_node (int value, tree type)
6018 if (type == integer_type_node)
6019 return value ? integer_one_node : integer_zero_node;
6020 else if (type == boolean_type_node)
6021 return value ? boolean_true_node : boolean_false_node;
6023 return build_int_cst (type, value);
6027 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6028 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6029 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6030 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6031 COND is the first argument to CODE; otherwise (as in the example
6032 given here), it is the second argument. TYPE is the type of the
6033 original expression. Return NULL_TREE if no simplification is
6037 fold_binary_op_with_conditional_arg (location_t loc,
6038 enum tree_code code,
6039 tree type, tree op0, tree op1,
6040 tree cond, tree arg, int cond_first_p)
6042 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6043 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6044 tree test, true_value, false_value;
6045 tree lhs = NULL_TREE;
6046 tree rhs = NULL_TREE;
6048 if (TREE_CODE (cond) == COND_EXPR)
6050 test = TREE_OPERAND (cond, 0);
6051 true_value = TREE_OPERAND (cond, 1);
6052 false_value = TREE_OPERAND (cond, 2);
6053 /* If this operand throws an expression, then it does not make
6054 sense to try to perform a logical or arithmetic operation
6056 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6058 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6063 tree testtype = TREE_TYPE (cond);
6065 true_value = constant_boolean_node (true, testtype);
6066 false_value = constant_boolean_node (false, testtype);
6069 /* This transformation is only worthwhile if we don't have to wrap ARG
6070 in a SAVE_EXPR and the operation can be simplified on at least one
6071 of the branches once its pushed inside the COND_EXPR. */
6072 if (!TREE_CONSTANT (arg)
6073 && (TREE_SIDE_EFFECTS (arg)
6074 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6077 arg = fold_convert_loc (loc, arg_type, arg);
6080 true_value = fold_convert_loc (loc, cond_type, true_value);
6082 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6084 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6088 false_value = fold_convert_loc (loc, cond_type, false_value);
6090 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6092 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6095 /* Check that we have simplified at least one of the branches. */
6096 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6099 return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6103 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6105 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6106 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6107 ADDEND is the same as X.
6109 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6110 and finite. The problematic cases are when X is zero, and its mode
6111 has signed zeros. In the case of rounding towards -infinity,
6112 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6113 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6116 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6118 if (!real_zerop (addend))
6121 /* Don't allow the fold with -fsignaling-nans. */
6122 if (HONOR_SNANS (TYPE_MODE (type)))
6125 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6126 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6129 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6130 if (TREE_CODE (addend) == REAL_CST
6131 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6134 /* The mode has signed zeros, and we have to honor their sign.
6135 In this situation, there is only one case we can return true for.
6136 X - 0 is the same as X unless rounding towards -infinity is
6138 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6141 /* Subroutine of fold() that checks comparisons of built-in math
6142 functions against real constants.
6144 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6145 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6146 is the type of the result and ARG0 and ARG1 are the operands of the
6147 comparison. ARG1 must be a TREE_REAL_CST.
6149 The function returns the constant folded tree if a simplification
6150 can be made, and NULL_TREE otherwise. */
6153 fold_mathfn_compare (location_t loc,
6154 enum built_in_function fcode, enum tree_code code,
6155 tree type, tree arg0, tree arg1)
6159 if (BUILTIN_SQRT_P (fcode))
6161 tree arg = CALL_EXPR_ARG (arg0, 0);
6162 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6164 c = TREE_REAL_CST (arg1);
6165 if (REAL_VALUE_NEGATIVE (c))
6167 /* sqrt(x) < y is always false, if y is negative. */
6168 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6169 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6171 /* sqrt(x) > y is always true, if y is negative and we
6172 don't care about NaNs, i.e. negative values of x. */
6173 if (code == NE_EXPR || !HONOR_NANS (mode))
6174 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6176 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6177 return fold_build2_loc (loc, GE_EXPR, type, arg,
6178 build_real (TREE_TYPE (arg), dconst0));
6180 else if (code == GT_EXPR || code == GE_EXPR)
6184 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6185 real_convert (&c2, mode, &c2);
6187 if (REAL_VALUE_ISINF (c2))
6189 /* sqrt(x) > y is x == +Inf, when y is very large. */
6190 if (HONOR_INFINITIES (mode))
6191 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6192 build_real (TREE_TYPE (arg), c2));
6194 /* sqrt(x) > y is always false, when y is very large
6195 and we don't care about infinities. */
6196 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6199 /* sqrt(x) > c is the same as x > c*c. */
6200 return fold_build2_loc (loc, code, type, arg,
6201 build_real (TREE_TYPE (arg), c2));
6203 else if (code == LT_EXPR || code == LE_EXPR)
6207 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6208 real_convert (&c2, mode, &c2);
6210 if (REAL_VALUE_ISINF (c2))
6212 /* sqrt(x) < y is always true, when y is a very large
6213 value and we don't care about NaNs or Infinities. */
6214 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6215 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6217 /* sqrt(x) < y is x != +Inf when y is very large and we
6218 don't care about NaNs. */
6219 if (! HONOR_NANS (mode))
6220 return fold_build2_loc (loc, NE_EXPR, type, arg,
6221 build_real (TREE_TYPE (arg), c2));
6223 /* sqrt(x) < y is x >= 0 when y is very large and we
6224 don't care about Infinities. */
6225 if (! HONOR_INFINITIES (mode))
6226 return fold_build2_loc (loc, GE_EXPR, type, arg,
6227 build_real (TREE_TYPE (arg), dconst0));
6229 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6230 if (lang_hooks.decls.global_bindings_p () != 0
6231 || CONTAINS_PLACEHOLDER_P (arg))
6234 arg = save_expr (arg);
6235 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6236 fold_build2_loc (loc, GE_EXPR, type, arg,
6237 build_real (TREE_TYPE (arg),
6239 fold_build2_loc (loc, NE_EXPR, type, arg,
6240 build_real (TREE_TYPE (arg),
6244 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6245 if (! HONOR_NANS (mode))
6246 return fold_build2_loc (loc, code, type, arg,
6247 build_real (TREE_TYPE (arg), c2));
6249 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6250 if (lang_hooks.decls.global_bindings_p () == 0
6251 && ! CONTAINS_PLACEHOLDER_P (arg))
6253 arg = save_expr (arg);
6254 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6255 fold_build2_loc (loc, GE_EXPR, type, arg,
6256 build_real (TREE_TYPE (arg),
6258 fold_build2_loc (loc, code, type, arg,
6259 build_real (TREE_TYPE (arg),
6268 /* Subroutine of fold() that optimizes comparisons against Infinities,
6269 either +Inf or -Inf.
6271 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6272 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6273 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6275 The function returns the constant folded tree if a simplification
6276 can be made, and NULL_TREE otherwise. */
6279 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6280 tree arg0, tree arg1)
6282 enum machine_mode mode;
6283 REAL_VALUE_TYPE max;
6287 mode = TYPE_MODE (TREE_TYPE (arg0));
6289 /* For negative infinity swap the sense of the comparison. */
6290 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6292 code = swap_tree_comparison (code);
6297 /* x > +Inf is always false, if with ignore sNANs. */
6298 if (HONOR_SNANS (mode))
6300 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6303 /* x <= +Inf is always true, if we don't case about NaNs. */
6304 if (! HONOR_NANS (mode))
6305 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6307 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6308 if (lang_hooks.decls.global_bindings_p () == 0
6309 && ! CONTAINS_PLACEHOLDER_P (arg0))
6311 arg0 = save_expr (arg0);
6312 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6318 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6319 real_maxval (&max, neg, mode);
6320 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6321 arg0, build_real (TREE_TYPE (arg0), max));
6324 /* x < +Inf is always equal to x <= DBL_MAX. */
6325 real_maxval (&max, neg, mode);
6326 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6327 arg0, build_real (TREE_TYPE (arg0), max));
6330 /* x != +Inf is always equal to !(x > DBL_MAX). */
6331 real_maxval (&max, neg, mode);
6332 if (! HONOR_NANS (mode))
6333 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6334 arg0, build_real (TREE_TYPE (arg0), max));
6336 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6337 arg0, build_real (TREE_TYPE (arg0), max));
6338 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6347 /* Subroutine of fold() that optimizes comparisons of a division by
6348 a nonzero integer constant against an integer constant, i.e.
6351 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6352 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6353 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6355 The function returns the constant folded tree if a simplification
6356 can be made, and NULL_TREE otherwise. */
6359 fold_div_compare (location_t loc,
6360 enum tree_code code, tree type, tree arg0, tree arg1)
6362 tree prod, tmp, hi, lo;
6363 tree arg00 = TREE_OPERAND (arg0, 0);
6364 tree arg01 = TREE_OPERAND (arg0, 1);
6366 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6370 /* We have to do this the hard way to detect unsigned overflow.
6371 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6372 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6373 TREE_INT_CST_HIGH (arg01),
6374 TREE_INT_CST_LOW (arg1),
6375 TREE_INT_CST_HIGH (arg1),
6376 &val.low, &val.high, unsigned_p);
6377 prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6378 neg_overflow = false;
6382 tmp = int_const_binop (MINUS_EXPR, arg01,
6383 build_int_cst (TREE_TYPE (arg01), 1), 0);
6386 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6387 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6388 TREE_INT_CST_HIGH (prod),
6389 TREE_INT_CST_LOW (tmp),
6390 TREE_INT_CST_HIGH (tmp),
6391 &val.low, &val.high, unsigned_p);
6392 hi = force_fit_type_double (TREE_TYPE (arg00), val,
6393 -1, overflow | TREE_OVERFLOW (prod));
6395 else if (tree_int_cst_sgn (arg01) >= 0)
6397 tmp = int_const_binop (MINUS_EXPR, arg01,
6398 build_int_cst (TREE_TYPE (arg01), 1), 0);
6399 switch (tree_int_cst_sgn (arg1))
6402 neg_overflow = true;
6403 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6408 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6413 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6423 /* A negative divisor reverses the relational operators. */
6424 code = swap_tree_comparison (code);
6426 tmp = int_const_binop (PLUS_EXPR, arg01,
6427 build_int_cst (TREE_TYPE (arg01), 1), 0);
6428 switch (tree_int_cst_sgn (arg1))
6431 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6436 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6441 neg_overflow = true;
6442 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6454 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6455 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6456 if (TREE_OVERFLOW (hi))
6457 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6458 if (TREE_OVERFLOW (lo))
6459 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6460 return build_range_check (loc, type, arg00, 1, lo, hi);
6463 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6464 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6465 if (TREE_OVERFLOW (hi))
6466 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6467 if (TREE_OVERFLOW (lo))
6468 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6469 return build_range_check (loc, type, arg00, 0, lo, hi);
6472 if (TREE_OVERFLOW (lo))
6474 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6475 return omit_one_operand_loc (loc, type, tmp, arg00);
6477 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6480 if (TREE_OVERFLOW (hi))
6482 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6483 return omit_one_operand_loc (loc, type, tmp, arg00);
6485 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6488 if (TREE_OVERFLOW (hi))
6490 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6491 return omit_one_operand_loc (loc, type, tmp, arg00);
6493 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6496 if (TREE_OVERFLOW (lo))
6498 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6499 return omit_one_operand_loc (loc, type, tmp, arg00);
6501 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6511 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6512 equality/inequality test, then return a simplified form of the test
6513 using a sign testing. Otherwise return NULL. TYPE is the desired
6517 fold_single_bit_test_into_sign_test (location_t loc,
6518 enum tree_code code, tree arg0, tree arg1,
6521 /* If this is testing a single bit, we can optimize the test. */
6522 if ((code == NE_EXPR || code == EQ_EXPR)
6523 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6524 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6526 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6527 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6528 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6530 if (arg00 != NULL_TREE
6531 /* This is only a win if casting to a signed type is cheap,
6532 i.e. when arg00's type is not a partial mode. */
6533 && TYPE_PRECISION (TREE_TYPE (arg00))
6534 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6536 tree stype = signed_type_for (TREE_TYPE (arg00));
6537 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6539 fold_convert_loc (loc, stype, arg00),
6540 build_int_cst (stype, 0));
6547 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6548 equality/inequality test, then return a simplified form of
6549 the test using shifts and logical operations. Otherwise return
6550 NULL. TYPE is the desired result type. */
6553 fold_single_bit_test (location_t loc, enum tree_code code,
6554 tree arg0, tree arg1, tree result_type)
6556 /* If this is testing a single bit, we can optimize the test. */
6557 if ((code == NE_EXPR || code == EQ_EXPR)
6558 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6559 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6561 tree inner = TREE_OPERAND (arg0, 0);
6562 tree type = TREE_TYPE (arg0);
6563 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6564 enum machine_mode operand_mode = TYPE_MODE (type);
6566 tree signed_type, unsigned_type, intermediate_type;
6569 /* First, see if we can fold the single bit test into a sign-bit
6571 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6576 /* Otherwise we have (A & C) != 0 where C is a single bit,
6577 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6578 Similarly for (A & C) == 0. */
6580 /* If INNER is a right shift of a constant and it plus BITNUM does
6581 not overflow, adjust BITNUM and INNER. */
6582 if (TREE_CODE (inner) == RSHIFT_EXPR
6583 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6584 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6585 && bitnum < TYPE_PRECISION (type)
6586 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6587 bitnum - TYPE_PRECISION (type)))
6589 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6590 inner = TREE_OPERAND (inner, 0);
6593 /* If we are going to be able to omit the AND below, we must do our
6594 operations as unsigned. If we must use the AND, we have a choice.
6595 Normally unsigned is faster, but for some machines signed is. */
6596 #ifdef LOAD_EXTEND_OP
6597 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6598 && !flag_syntax_only) ? 0 : 1;
6603 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6604 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6605 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6606 inner = fold_convert_loc (loc, intermediate_type, inner);
6609 inner = build2 (RSHIFT_EXPR, intermediate_type,
6610 inner, size_int (bitnum));
6612 one = build_int_cst (intermediate_type, 1);
6614 if (code == EQ_EXPR)
6615 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6617 /* Put the AND last so it can combine with more things. */
6618 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6620 /* Make sure to return the proper type. */
6621 inner = fold_convert_loc (loc, result_type, inner);
6628 /* Check whether we are allowed to reorder operands arg0 and arg1,
6629 such that the evaluation of arg1 occurs before arg0. */
6632 reorder_operands_p (const_tree arg0, const_tree arg1)
6634 if (! flag_evaluation_order)
6636 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6638 return ! TREE_SIDE_EFFECTS (arg0)
6639 && ! TREE_SIDE_EFFECTS (arg1);
6642 /* Test whether it is preferable two swap two operands, ARG0 and
6643 ARG1, for example because ARG0 is an integer constant and ARG1
6644 isn't. If REORDER is true, only recommend swapping if we can
6645 evaluate the operands in reverse order. */
6648 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6650 STRIP_SIGN_NOPS (arg0);
6651 STRIP_SIGN_NOPS (arg1);
6653 if (TREE_CODE (arg1) == INTEGER_CST)
6655 if (TREE_CODE (arg0) == INTEGER_CST)
6658 if (TREE_CODE (arg1) == REAL_CST)
6660 if (TREE_CODE (arg0) == REAL_CST)
6663 if (TREE_CODE (arg1) == FIXED_CST)
6665 if (TREE_CODE (arg0) == FIXED_CST)
6668 if (TREE_CODE (arg1) == COMPLEX_CST)
6670 if (TREE_CODE (arg0) == COMPLEX_CST)
6673 if (TREE_CONSTANT (arg1))
6675 if (TREE_CONSTANT (arg0))
6678 if (optimize_function_for_size_p (cfun))
6681 if (reorder && flag_evaluation_order
6682 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6685 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6686 for commutative and comparison operators. Ensuring a canonical
6687 form allows the optimizers to find additional redundancies without
6688 having to explicitly check for both orderings. */
6689 if (TREE_CODE (arg0) == SSA_NAME
6690 && TREE_CODE (arg1) == SSA_NAME
6691 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6694 /* Put SSA_NAMEs last. */
6695 if (TREE_CODE (arg1) == SSA_NAME)
6697 if (TREE_CODE (arg0) == SSA_NAME)
6700 /* Put variables last. */
6709 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6710 ARG0 is extended to a wider type. */
6713 fold_widened_comparison (location_t loc, enum tree_code code,
6714 tree type, tree arg0, tree arg1)
6716 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6718 tree shorter_type, outer_type;
6722 if (arg0_unw == arg0)
6724 shorter_type = TREE_TYPE (arg0_unw);
6726 #ifdef HAVE_canonicalize_funcptr_for_compare
6727 /* Disable this optimization if we're casting a function pointer
6728 type on targets that require function pointer canonicalization. */
6729 if (HAVE_canonicalize_funcptr_for_compare
6730 && TREE_CODE (shorter_type) == POINTER_TYPE
6731 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6735 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6738 arg1_unw = get_unwidened (arg1, NULL_TREE);
6740 /* If possible, express the comparison in the shorter mode. */
6741 if ((code == EQ_EXPR || code == NE_EXPR
6742 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6743 && (TREE_TYPE (arg1_unw) == shorter_type
6744 || ((TYPE_PRECISION (shorter_type)
6745 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6746 && (TYPE_UNSIGNED (shorter_type)
6747 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6748 || (TREE_CODE (arg1_unw) == INTEGER_CST
6749 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6750 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6751 && int_fits_type_p (arg1_unw, shorter_type))))
6752 return fold_build2_loc (loc, code, type, arg0_unw,
6753 fold_convert_loc (loc, shorter_type, arg1_unw));
6755 if (TREE_CODE (arg1_unw) != INTEGER_CST
6756 || TREE_CODE (shorter_type) != INTEGER_TYPE
6757 || !int_fits_type_p (arg1_unw, shorter_type))
6760 /* If we are comparing with the integer that does not fit into the range
6761 of the shorter type, the result is known. */
6762 outer_type = TREE_TYPE (arg1_unw);
6763 min = lower_bound_in_type (outer_type, shorter_type);
6764 max = upper_bound_in_type (outer_type, shorter_type);
6766 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6768 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6775 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6780 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6786 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6788 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6793 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6795 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6804 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6805 ARG0 just the signedness is changed. */
6808 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6809 tree arg0, tree arg1)
6812 tree inner_type, outer_type;
6814 if (!CONVERT_EXPR_P (arg0))
6817 outer_type = TREE_TYPE (arg0);
6818 arg0_inner = TREE_OPERAND (arg0, 0);
6819 inner_type = TREE_TYPE (arg0_inner);
6821 #ifdef HAVE_canonicalize_funcptr_for_compare
6822 /* Disable this optimization if we're casting a function pointer
6823 type on targets that require function pointer canonicalization. */
6824 if (HAVE_canonicalize_funcptr_for_compare
6825 && TREE_CODE (inner_type) == POINTER_TYPE
6826 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6830 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6833 if (TREE_CODE (arg1) != INTEGER_CST
6834 && !(CONVERT_EXPR_P (arg1)
6835 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6838 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6839 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6844 if (TREE_CODE (arg1) == INTEGER_CST)
6845 arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6846 0, TREE_OVERFLOW (arg1));
6848 arg1 = fold_convert_loc (loc, inner_type, arg1);
6850 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6853 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6854 step of the array. Reconstructs s and delta in the case of s *
6855 delta being an integer constant (and thus already folded). ADDR is
6856 the address. MULT is the multiplicative expression. If the
6857 function succeeds, the new address expression is returned.
6858 Otherwise NULL_TREE is returned. LOC is the location of the
6859 resulting expression. */
6862 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6864 tree s, delta, step;
6865 tree ref = TREE_OPERAND (addr, 0), pref;
6870 /* Strip the nops that might be added when converting op1 to sizetype. */
6873 /* Canonicalize op1 into a possibly non-constant delta
6874 and an INTEGER_CST s. */
6875 if (TREE_CODE (op1) == MULT_EXPR)
6877 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6882 if (TREE_CODE (arg0) == INTEGER_CST)
6887 else if (TREE_CODE (arg1) == INTEGER_CST)
6895 else if (TREE_CODE (op1) == INTEGER_CST)
6902 /* Simulate we are delta * 1. */
6904 s = integer_one_node;
6907 for (;; ref = TREE_OPERAND (ref, 0))
6909 if (TREE_CODE (ref) == ARRAY_REF)
6913 /* Remember if this was a multi-dimensional array. */
6914 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6917 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6920 itype = TREE_TYPE (domain);
6922 step = array_ref_element_size (ref);
6923 if (TREE_CODE (step) != INTEGER_CST)
6928 if (! tree_int_cst_equal (step, s))
6933 /* Try if delta is a multiple of step. */
6934 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6940 /* Only fold here if we can verify we do not overflow one
6941 dimension of a multi-dimensional array. */
6946 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6947 || !TYPE_MAX_VALUE (domain)
6948 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6951 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6952 fold_convert_loc (loc, itype,
6953 TREE_OPERAND (ref, 1)),
6954 fold_convert_loc (loc, itype, delta));
6956 || TREE_CODE (tmp) != INTEGER_CST
6957 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6966 if (!handled_component_p (ref))
6970 /* We found the suitable array reference. So copy everything up to it,
6971 and replace the index. */
6973 pref = TREE_OPERAND (addr, 0);
6974 ret = copy_node (pref);
6975 SET_EXPR_LOCATION (ret, loc);
6980 pref = TREE_OPERAND (pref, 0);
6981 TREE_OPERAND (pos, 0) = copy_node (pref);
6982 pos = TREE_OPERAND (pos, 0);
6985 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6986 fold_convert_loc (loc, itype,
6987 TREE_OPERAND (pos, 1)),
6988 fold_convert_loc (loc, itype, delta));
6990 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6994 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6995 means A >= Y && A != MAX, but in this case we know that
6996 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6999 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7001 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7003 if (TREE_CODE (bound) == LT_EXPR)
7004 a = TREE_OPERAND (bound, 0);
7005 else if (TREE_CODE (bound) == GT_EXPR)
7006 a = TREE_OPERAND (bound, 1);
7010 typea = TREE_TYPE (a);
7011 if (!INTEGRAL_TYPE_P (typea)
7012 && !POINTER_TYPE_P (typea))
7015 if (TREE_CODE (ineq) == LT_EXPR)
7017 a1 = TREE_OPERAND (ineq, 1);
7018 y = TREE_OPERAND (ineq, 0);
7020 else if (TREE_CODE (ineq) == GT_EXPR)
7022 a1 = TREE_OPERAND (ineq, 0);
7023 y = TREE_OPERAND (ineq, 1);
7028 if (TREE_TYPE (a1) != typea)
7031 if (POINTER_TYPE_P (typea))
7033 /* Convert the pointer types into integer before taking the difference. */
7034 tree ta = fold_convert_loc (loc, ssizetype, a);
7035 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7036 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7039 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7041 if (!diff || !integer_onep (diff))
7044 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7047 /* Fold a sum or difference of at least one multiplication.
7048 Returns the folded tree or NULL if no simplification could be made. */
7051 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7052 tree arg0, tree arg1)
7054 tree arg00, arg01, arg10, arg11;
7055 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7057 /* (A * C) +- (B * C) -> (A+-B) * C.
7058 (A * C) +- A -> A * (C+-1).
7059 We are most concerned about the case where C is a constant,
7060 but other combinations show up during loop reduction. Since
7061 it is not difficult, try all four possibilities. */
7063 if (TREE_CODE (arg0) == MULT_EXPR)
7065 arg00 = TREE_OPERAND (arg0, 0);
7066 arg01 = TREE_OPERAND (arg0, 1);
7068 else if (TREE_CODE (arg0) == INTEGER_CST)
7070 arg00 = build_one_cst (type);
7075 /* We cannot generate constant 1 for fract. */
7076 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7079 arg01 = build_one_cst (type);
7081 if (TREE_CODE (arg1) == MULT_EXPR)
7083 arg10 = TREE_OPERAND (arg1, 0);
7084 arg11 = TREE_OPERAND (arg1, 1);
7086 else if (TREE_CODE (arg1) == INTEGER_CST)
7088 arg10 = build_one_cst (type);
7089 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7090 the purpose of this canonicalization. */
7091 if (TREE_INT_CST_HIGH (arg1) == -1
7092 && negate_expr_p (arg1)
7093 && code == PLUS_EXPR)
7095 arg11 = negate_expr (arg1);
7103 /* We cannot generate constant 1 for fract. */
7104 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7107 arg11 = build_one_cst (type);
7111 if (operand_equal_p (arg01, arg11, 0))
7112 same = arg01, alt0 = arg00, alt1 = arg10;
7113 else if (operand_equal_p (arg00, arg10, 0))
7114 same = arg00, alt0 = arg01, alt1 = arg11;
7115 else if (operand_equal_p (arg00, arg11, 0))
7116 same = arg00, alt0 = arg01, alt1 = arg10;
7117 else if (operand_equal_p (arg01, arg10, 0))
7118 same = arg01, alt0 = arg00, alt1 = arg11;
7120 /* No identical multiplicands; see if we can find a common
7121 power-of-two factor in non-power-of-two multiplies. This
7122 can help in multi-dimensional array access. */
7123 else if (host_integerp (arg01, 0)
7124 && host_integerp (arg11, 0))
7126 HOST_WIDE_INT int01, int11, tmp;
7129 int01 = TREE_INT_CST_LOW (arg01);
7130 int11 = TREE_INT_CST_LOW (arg11);
7132 /* Move min of absolute values to int11. */
7133 if ((int01 >= 0 ? int01 : -int01)
7134 < (int11 >= 0 ? int11 : -int11))
7136 tmp = int01, int01 = int11, int11 = tmp;
7137 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7144 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7145 /* The remainder should not be a constant, otherwise we
7146 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7147 increased the number of multiplications necessary. */
7148 && TREE_CODE (arg10) != INTEGER_CST)
7150 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7151 build_int_cst (TREE_TYPE (arg00),
7156 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7161 return fold_build2_loc (loc, MULT_EXPR, type,
7162 fold_build2_loc (loc, code, type,
7163 fold_convert_loc (loc, type, alt0),
7164 fold_convert_loc (loc, type, alt1)),
7165 fold_convert_loc (loc, type, same));
7170 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7171 specified by EXPR into the buffer PTR of length LEN bytes.
7172 Return the number of bytes placed in the buffer, or zero
7176 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7178 tree type = TREE_TYPE (expr);
7179 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7180 int byte, offset, word, words;
7181 unsigned char value;
7183 if (total_bytes > len)
7185 words = total_bytes / UNITS_PER_WORD;
7187 for (byte = 0; byte < total_bytes; byte++)
7189 int bitpos = byte * BITS_PER_UNIT;
7190 if (bitpos < HOST_BITS_PER_WIDE_INT)
7191 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7193 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7194 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7196 if (total_bytes > UNITS_PER_WORD)
7198 word = byte / UNITS_PER_WORD;
7199 if (WORDS_BIG_ENDIAN)
7200 word = (words - 1) - word;
7201 offset = word * UNITS_PER_WORD;
7202 if (BYTES_BIG_ENDIAN)
7203 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7205 offset += byte % UNITS_PER_WORD;
7208 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7209 ptr[offset] = value;
7215 /* Subroutine of native_encode_expr. Encode the REAL_CST
7216 specified by EXPR into the buffer PTR of length LEN bytes.
7217 Return the number of bytes placed in the buffer, or zero
7221 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7223 tree type = TREE_TYPE (expr);
7224 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7225 int byte, offset, word, words, bitpos;
7226 unsigned char value;
7228 /* There are always 32 bits in each long, no matter the size of
7229 the hosts long. We handle floating point representations with
7233 if (total_bytes > len)
7235 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7237 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7239 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7240 bitpos += BITS_PER_UNIT)
7242 byte = (bitpos / BITS_PER_UNIT) & 3;
7243 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7245 if (UNITS_PER_WORD < 4)
7247 word = byte / UNITS_PER_WORD;
7248 if (WORDS_BIG_ENDIAN)
7249 word = (words - 1) - word;
7250 offset = word * UNITS_PER_WORD;
7251 if (BYTES_BIG_ENDIAN)
7252 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7254 offset += byte % UNITS_PER_WORD;
7257 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7258 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7263 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7264 specified by EXPR into the buffer PTR of length LEN bytes.
7265 Return the number of bytes placed in the buffer, or zero
7269 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7274 part = TREE_REALPART (expr);
7275 rsize = native_encode_expr (part, ptr, len);
7278 part = TREE_IMAGPART (expr);
7279 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7282 return rsize + isize;
7286 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7287 specified by EXPR into the buffer PTR of length LEN bytes.
7288 Return the number of bytes placed in the buffer, or zero
7292 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7294 int i, size, offset, count;
7295 tree itype, elem, elements;
7298 elements = TREE_VECTOR_CST_ELTS (expr);
7299 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7300 itype = TREE_TYPE (TREE_TYPE (expr));
7301 size = GET_MODE_SIZE (TYPE_MODE (itype));
7302 for (i = 0; i < count; i++)
7306 elem = TREE_VALUE (elements);
7307 elements = TREE_CHAIN (elements);
7314 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7319 if (offset + size > len)
7321 memset (ptr+offset, 0, size);
7329 /* Subroutine of native_encode_expr. Encode the STRING_CST
7330 specified by EXPR into the buffer PTR of length LEN bytes.
7331 Return the number of bytes placed in the buffer, or zero
7335 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7337 tree type = TREE_TYPE (expr);
7338 HOST_WIDE_INT total_bytes;
7340 if (TREE_CODE (type) != ARRAY_TYPE
7341 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7342 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7343 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7345 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7346 if (total_bytes > len)
7348 if (TREE_STRING_LENGTH (expr) < total_bytes)
7350 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7351 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7352 total_bytes - TREE_STRING_LENGTH (expr));
7355 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7360 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7361 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7362 buffer PTR of length LEN bytes. Return the number of bytes
7363 placed in the buffer, or zero upon failure. */
7366 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7368 switch (TREE_CODE (expr))
7371 return native_encode_int (expr, ptr, len);
7374 return native_encode_real (expr, ptr, len);
7377 return native_encode_complex (expr, ptr, len);
7380 return native_encode_vector (expr, ptr, len);
7383 return native_encode_string (expr, ptr, len);
7391 /* Subroutine of native_interpret_expr. Interpret the contents of
7392 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7393 If the buffer cannot be interpreted, return NULL_TREE. */
7396 native_interpret_int (tree type, const unsigned char *ptr, int len)
7398 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7399 int byte, offset, word, words;
7400 unsigned char value;
7403 if (total_bytes > len)
7405 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7408 result = double_int_zero;
7409 words = total_bytes / UNITS_PER_WORD;
7411 for (byte = 0; byte < total_bytes; byte++)
7413 int bitpos = byte * BITS_PER_UNIT;
7414 if (total_bytes > UNITS_PER_WORD)
7416 word = byte / UNITS_PER_WORD;
7417 if (WORDS_BIG_ENDIAN)
7418 word = (words - 1) - word;
7419 offset = word * UNITS_PER_WORD;
7420 if (BYTES_BIG_ENDIAN)
7421 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7423 offset += byte % UNITS_PER_WORD;
7426 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7427 value = ptr[offset];
7429 if (bitpos < HOST_BITS_PER_WIDE_INT)
7430 result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7432 result.high |= (unsigned HOST_WIDE_INT) value
7433 << (bitpos - HOST_BITS_PER_WIDE_INT);
7436 return double_int_to_tree (type, result);
7440 /* Subroutine of native_interpret_expr. Interpret the contents of
7441 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7442 If the buffer cannot be interpreted, return NULL_TREE. */
7445 native_interpret_real (tree type, const unsigned char *ptr, int len)
7447 enum machine_mode mode = TYPE_MODE (type);
7448 int total_bytes = GET_MODE_SIZE (mode);
7449 int byte, offset, word, words, bitpos;
7450 unsigned char value;
7451 /* There are always 32 bits in each long, no matter the size of
7452 the hosts long. We handle floating point representations with
7457 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7458 if (total_bytes > len || total_bytes > 24)
7460 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7462 memset (tmp, 0, sizeof (tmp));
7463 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7464 bitpos += BITS_PER_UNIT)
7466 byte = (bitpos / BITS_PER_UNIT) & 3;
7467 if (UNITS_PER_WORD < 4)
7469 word = byte / UNITS_PER_WORD;
7470 if (WORDS_BIG_ENDIAN)
7471 word = (words - 1) - word;
7472 offset = word * UNITS_PER_WORD;
7473 if (BYTES_BIG_ENDIAN)
7474 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7476 offset += byte % UNITS_PER_WORD;
7479 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7480 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7482 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7485 real_from_target (&r, tmp, mode);
7486 return build_real (type, r);
7490 /* Subroutine of native_interpret_expr. Interpret the contents of
7491 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7492 If the buffer cannot be interpreted, return NULL_TREE. */
7495 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7497 tree etype, rpart, ipart;
7500 etype = TREE_TYPE (type);
7501 size = GET_MODE_SIZE (TYPE_MODE (etype));
7504 rpart = native_interpret_expr (etype, ptr, size);
7507 ipart = native_interpret_expr (etype, ptr+size, size);
7510 return build_complex (type, rpart, ipart);
7514 /* Subroutine of native_interpret_expr. Interpret the contents of
7515 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7516 If the buffer cannot be interpreted, return NULL_TREE. */
7519 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7521 tree etype, elem, elements;
7524 etype = TREE_TYPE (type);
7525 size = GET_MODE_SIZE (TYPE_MODE (etype));
7526 count = TYPE_VECTOR_SUBPARTS (type);
7527 if (size * count > len)
7530 elements = NULL_TREE;
7531 for (i = count - 1; i >= 0; i--)
7533 elem = native_interpret_expr (etype, ptr+(i*size), size);
7536 elements = tree_cons (NULL_TREE, elem, elements);
7538 return build_vector (type, elements);
7542 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7543 the buffer PTR of length LEN as a constant of type TYPE. For
7544 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7545 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7546 return NULL_TREE. */
7549 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7551 switch (TREE_CODE (type))
7556 return native_interpret_int (type, ptr, len);
7559 return native_interpret_real (type, ptr, len);
7562 return native_interpret_complex (type, ptr, len);
7565 return native_interpret_vector (type, ptr, len);
7573 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7574 TYPE at compile-time. If we're unable to perform the conversion
7575 return NULL_TREE. */
7578 fold_view_convert_expr (tree type, tree expr)
7580 /* We support up to 512-bit values (for V8DFmode). */
7581 unsigned char buffer[64];
7584 /* Check that the host and target are sane. */
7585 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7588 len = native_encode_expr (expr, buffer, sizeof (buffer));
7592 return native_interpret_expr (type, buffer, len);
7595 /* Build an expression for the address of T. Folds away INDIRECT_REF
7596 to avoid confusing the gimplify process. */
7599 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7601 /* The size of the object is not relevant when talking about its address. */
7602 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7603 t = TREE_OPERAND (t, 0);
7605 if (TREE_CODE (t) == INDIRECT_REF)
7607 t = TREE_OPERAND (t, 0);
7609 if (TREE_TYPE (t) != ptrtype)
7611 t = build1 (NOP_EXPR, ptrtype, t);
7612 SET_EXPR_LOCATION (t, loc);
7615 else if (TREE_CODE (t) == MEM_REF
7616 && integer_zerop (TREE_OPERAND (t, 1)))
7617 return TREE_OPERAND (t, 0);
7618 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7620 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7622 if (TREE_TYPE (t) != ptrtype)
7623 t = fold_convert_loc (loc, ptrtype, t);
7627 t = build1 (ADDR_EXPR, ptrtype, t);
7628 SET_EXPR_LOCATION (t, loc);
7634 /* Build an expression for the address of T. */
7637 build_fold_addr_expr_loc (location_t loc, tree t)
7639 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7641 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7644 /* Fold a unary expression of code CODE and type TYPE with operand
7645 OP0. Return the folded expression if folding is successful.
7646 Otherwise, return NULL_TREE. */
7649 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7653 enum tree_code_class kind = TREE_CODE_CLASS (code);
7655 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7656 && TREE_CODE_LENGTH (code) == 1);
7661 if (CONVERT_EXPR_CODE_P (code)
7662 || code == FLOAT_EXPR || code == ABS_EXPR)
7664 /* Don't use STRIP_NOPS, because signedness of argument type
7666 STRIP_SIGN_NOPS (arg0);
7670 /* Strip any conversions that don't change the mode. This
7671 is safe for every expression, except for a comparison
7672 expression because its signedness is derived from its
7675 Note that this is done as an internal manipulation within
7676 the constant folder, in order to find the simplest
7677 representation of the arguments so that their form can be
7678 studied. In any cases, the appropriate type conversions
7679 should be put back in the tree that will get out of the
7685 if (TREE_CODE_CLASS (code) == tcc_unary)
7687 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7688 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7689 fold_build1_loc (loc, code, type,
7690 fold_convert_loc (loc, TREE_TYPE (op0),
7691 TREE_OPERAND (arg0, 1))));
7692 else if (TREE_CODE (arg0) == COND_EXPR)
7694 tree arg01 = TREE_OPERAND (arg0, 1);
7695 tree arg02 = TREE_OPERAND (arg0, 2);
7696 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7697 arg01 = fold_build1_loc (loc, code, type,
7698 fold_convert_loc (loc,
7699 TREE_TYPE (op0), arg01));
7700 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7701 arg02 = fold_build1_loc (loc, code, type,
7702 fold_convert_loc (loc,
7703 TREE_TYPE (op0), arg02));
7704 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7707 /* If this was a conversion, and all we did was to move into
7708 inside the COND_EXPR, bring it back out. But leave it if
7709 it is a conversion from integer to integer and the
7710 result precision is no wider than a word since such a
7711 conversion is cheap and may be optimized away by combine,
7712 while it couldn't if it were outside the COND_EXPR. Then return
7713 so we don't get into an infinite recursion loop taking the
7714 conversion out and then back in. */
7716 if ((CONVERT_EXPR_CODE_P (code)
7717 || code == NON_LVALUE_EXPR)
7718 && TREE_CODE (tem) == COND_EXPR
7719 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7720 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7721 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7722 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7723 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7724 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7725 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7727 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7728 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7729 || flag_syntax_only))
7731 tem = build1 (code, type,
7733 TREE_TYPE (TREE_OPERAND
7734 (TREE_OPERAND (tem, 1), 0)),
7735 TREE_OPERAND (tem, 0),
7736 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7737 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7738 SET_EXPR_LOCATION (tem, loc);
7742 else if (COMPARISON_CLASS_P (arg0))
7744 if (TREE_CODE (type) == BOOLEAN_TYPE)
7746 arg0 = copy_node (arg0);
7747 TREE_TYPE (arg0) = type;
7750 else if (TREE_CODE (type) != INTEGER_TYPE)
7751 return fold_build3_loc (loc, COND_EXPR, type, arg0,
7752 fold_build1_loc (loc, code, type,
7754 fold_build1_loc (loc, code, type,
7755 integer_zero_node));
7762 /* Re-association barriers around constants and other re-association
7763 barriers can be removed. */
7764 if (CONSTANT_CLASS_P (op0)
7765 || TREE_CODE (op0) == PAREN_EXPR)
7766 return fold_convert_loc (loc, type, op0);
7771 case FIX_TRUNC_EXPR:
7772 if (TREE_TYPE (op0) == type)
7775 /* If we have (type) (a CMP b) and type is an integral type, return
7776 new expression involving the new type. */
7777 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7778 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7779 TREE_OPERAND (op0, 1));
7781 /* Handle cases of two conversions in a row. */
7782 if (CONVERT_EXPR_P (op0))
7784 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7785 tree inter_type = TREE_TYPE (op0);
7786 int inside_int = INTEGRAL_TYPE_P (inside_type);
7787 int inside_ptr = POINTER_TYPE_P (inside_type);
7788 int inside_float = FLOAT_TYPE_P (inside_type);
7789 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7790 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7791 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7792 int inter_int = INTEGRAL_TYPE_P (inter_type);
7793 int inter_ptr = POINTER_TYPE_P (inter_type);
7794 int inter_float = FLOAT_TYPE_P (inter_type);
7795 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7796 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7797 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7798 int final_int = INTEGRAL_TYPE_P (type);
7799 int final_ptr = POINTER_TYPE_P (type);
7800 int final_float = FLOAT_TYPE_P (type);
7801 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7802 unsigned int final_prec = TYPE_PRECISION (type);
7803 int final_unsignedp = TYPE_UNSIGNED (type);
7805 /* In addition to the cases of two conversions in a row
7806 handled below, if we are converting something to its own
7807 type via an object of identical or wider precision, neither
7808 conversion is needed. */
7809 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7810 && (((inter_int || inter_ptr) && final_int)
7811 || (inter_float && final_float))
7812 && inter_prec >= final_prec)
7813 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7815 /* Likewise, if the intermediate and initial types are either both
7816 float or both integer, we don't need the middle conversion if the
7817 former is wider than the latter and doesn't change the signedness
7818 (for integers). Avoid this if the final type is a pointer since
7819 then we sometimes need the middle conversion. Likewise if the
7820 final type has a precision not equal to the size of its mode. */
7821 if (((inter_int && inside_int)
7822 || (inter_float && inside_float)
7823 || (inter_vec && inside_vec))
7824 && inter_prec >= inside_prec
7825 && (inter_float || inter_vec
7826 || inter_unsignedp == inside_unsignedp)
7827 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7828 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7830 && (! final_vec || inter_prec == inside_prec))
7831 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7833 /* If we have a sign-extension of a zero-extended value, we can
7834 replace that by a single zero-extension. */
7835 if (inside_int && inter_int && final_int
7836 && inside_prec < inter_prec && inter_prec < final_prec
7837 && inside_unsignedp && !inter_unsignedp)
7838 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7840 /* Two conversions in a row are not needed unless:
7841 - some conversion is floating-point (overstrict for now), or
7842 - some conversion is a vector (overstrict for now), or
7843 - the intermediate type is narrower than both initial and
7845 - the intermediate type and innermost type differ in signedness,
7846 and the outermost type is wider than the intermediate, or
7847 - the initial type is a pointer type and the precisions of the
7848 intermediate and final types differ, or
7849 - the final type is a pointer type and the precisions of the
7850 initial and intermediate types differ. */
7851 if (! inside_float && ! inter_float && ! final_float
7852 && ! inside_vec && ! inter_vec && ! final_vec
7853 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7854 && ! (inside_int && inter_int
7855 && inter_unsignedp != inside_unsignedp
7856 && inter_prec < final_prec)
7857 && ((inter_unsignedp && inter_prec > inside_prec)
7858 == (final_unsignedp && final_prec > inter_prec))
7859 && ! (inside_ptr && inter_prec != final_prec)
7860 && ! (final_ptr && inside_prec != inter_prec)
7861 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7862 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7863 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7866 /* Handle (T *)&A.B.C for A being of type T and B and C
7867 living at offset zero. This occurs frequently in
7868 C++ upcasting and then accessing the base. */
7869 if (TREE_CODE (op0) == ADDR_EXPR
7870 && POINTER_TYPE_P (type)
7871 && handled_component_p (TREE_OPERAND (op0, 0)))
7873 HOST_WIDE_INT bitsize, bitpos;
7875 enum machine_mode mode;
7876 int unsignedp, volatilep;
7877 tree base = TREE_OPERAND (op0, 0);
7878 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7879 &mode, &unsignedp, &volatilep, false);
7880 /* If the reference was to a (constant) zero offset, we can use
7881 the address of the base if it has the same base type
7882 as the result type and the pointer type is unqualified. */
7883 if (! offset && bitpos == 0
7884 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7885 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7886 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7887 return fold_convert_loc (loc, type,
7888 build_fold_addr_expr_loc (loc, base));
7891 if (TREE_CODE (op0) == MODIFY_EXPR
7892 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7893 /* Detect assigning a bitfield. */
7894 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7896 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7898 /* Don't leave an assignment inside a conversion
7899 unless assigning a bitfield. */
7900 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7901 /* First do the assignment, then return converted constant. */
7902 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7903 TREE_NO_WARNING (tem) = 1;
7904 TREE_USED (tem) = 1;
7905 SET_EXPR_LOCATION (tem, loc);
7909 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7910 constants (if x has signed type, the sign bit cannot be set
7911 in c). This folds extension into the BIT_AND_EXPR.
7912 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7913 very likely don't have maximal range for their precision and this
7914 transformation effectively doesn't preserve non-maximal ranges. */
7915 if (TREE_CODE (type) == INTEGER_TYPE
7916 && TREE_CODE (op0) == BIT_AND_EXPR
7917 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7919 tree and_expr = op0;
7920 tree and0 = TREE_OPERAND (and_expr, 0);
7921 tree and1 = TREE_OPERAND (and_expr, 1);
7924 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7925 || (TYPE_PRECISION (type)
7926 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7928 else if (TYPE_PRECISION (TREE_TYPE (and1))
7929 <= HOST_BITS_PER_WIDE_INT
7930 && host_integerp (and1, 1))
7932 unsigned HOST_WIDE_INT cst;
7934 cst = tree_low_cst (and1, 1);
7935 cst &= (HOST_WIDE_INT) -1
7936 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7937 change = (cst == 0);
7938 #ifdef LOAD_EXTEND_OP
7940 && !flag_syntax_only
7941 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7944 tree uns = unsigned_type_for (TREE_TYPE (and0));
7945 and0 = fold_convert_loc (loc, uns, and0);
7946 and1 = fold_convert_loc (loc, uns, and1);
7952 tem = force_fit_type_double (type, tree_to_double_int (and1),
7953 0, TREE_OVERFLOW (and1));
7954 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7955 fold_convert_loc (loc, type, and0), tem);
7959 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7960 when one of the new casts will fold away. Conservatively we assume
7961 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7962 if (POINTER_TYPE_P (type)
7963 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7964 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7965 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7966 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7968 tree arg00 = TREE_OPERAND (arg0, 0);
7969 tree arg01 = TREE_OPERAND (arg0, 1);
7971 return fold_build2_loc (loc,
7972 TREE_CODE (arg0), type,
7973 fold_convert_loc (loc, type, arg00),
7974 fold_convert_loc (loc, sizetype, arg01));
7977 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7978 of the same precision, and X is an integer type not narrower than
7979 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7980 if (INTEGRAL_TYPE_P (type)
7981 && TREE_CODE (op0) == BIT_NOT_EXPR
7982 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7983 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7984 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7986 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7987 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7988 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7989 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7990 fold_convert_loc (loc, type, tem));
7993 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7994 type of X and Y (integer types only). */
7995 if (INTEGRAL_TYPE_P (type)
7996 && TREE_CODE (op0) == MULT_EXPR
7997 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7998 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8000 /* Be careful not to introduce new overflows. */
8002 if (TYPE_OVERFLOW_WRAPS (type))
8005 mult_type = unsigned_type_for (type);
8007 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8009 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8010 fold_convert_loc (loc, mult_type,
8011 TREE_OPERAND (op0, 0)),
8012 fold_convert_loc (loc, mult_type,
8013 TREE_OPERAND (op0, 1)));
8014 return fold_convert_loc (loc, type, tem);
8018 tem = fold_convert_const (code, type, op0);
8019 return tem ? tem : NULL_TREE;
8021 case ADDR_SPACE_CONVERT_EXPR:
8022 if (integer_zerop (arg0))
8023 return fold_convert_const (code, type, arg0);
8026 case FIXED_CONVERT_EXPR:
8027 tem = fold_convert_const (code, type, arg0);
8028 return tem ? tem : NULL_TREE;
8030 case VIEW_CONVERT_EXPR:
8031 if (TREE_TYPE (op0) == type)
8033 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8034 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8035 type, TREE_OPERAND (op0, 0));
8036 if (TREE_CODE (op0) == MEM_REF)
8037 return fold_build2_loc (loc, MEM_REF, type,
8038 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8040 /* For integral conversions with the same precision or pointer
8041 conversions use a NOP_EXPR instead. */
8042 if ((INTEGRAL_TYPE_P (type)
8043 || POINTER_TYPE_P (type))
8044 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8045 || POINTER_TYPE_P (TREE_TYPE (op0)))
8046 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8047 return fold_convert_loc (loc, type, op0);
8049 /* Strip inner integral conversions that do not change the precision. */
8050 if (CONVERT_EXPR_P (op0)
8051 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8052 || POINTER_TYPE_P (TREE_TYPE (op0)))
8053 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8054 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8055 && (TYPE_PRECISION (TREE_TYPE (op0))
8056 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8057 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8058 type, TREE_OPERAND (op0, 0));
8060 return fold_view_convert_expr (type, op0);
8063 tem = fold_negate_expr (loc, arg0);
8065 return fold_convert_loc (loc, type, tem);
8069 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8070 return fold_abs_const (arg0, type);
8071 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8072 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8073 /* Convert fabs((double)float) into (double)fabsf(float). */
8074 else if (TREE_CODE (arg0) == NOP_EXPR
8075 && TREE_CODE (type) == REAL_TYPE)
8077 tree targ0 = strip_float_extensions (arg0);
8079 return fold_convert_loc (loc, type,
8080 fold_build1_loc (loc, ABS_EXPR,
8084 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8085 else if (TREE_CODE (arg0) == ABS_EXPR)
8087 else if (tree_expr_nonnegative_p (arg0))
8090 /* Strip sign ops from argument. */
8091 if (TREE_CODE (type) == REAL_TYPE)
8093 tem = fold_strip_sign_ops (arg0);
8095 return fold_build1_loc (loc, ABS_EXPR, type,
8096 fold_convert_loc (loc, type, tem));
8101 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8102 return fold_convert_loc (loc, type, arg0);
8103 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8105 tree itype = TREE_TYPE (type);
8106 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8107 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8108 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8109 negate_expr (ipart));
8111 if (TREE_CODE (arg0) == COMPLEX_CST)
8113 tree itype = TREE_TYPE (type);
8114 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8115 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8116 return build_complex (type, rpart, negate_expr (ipart));
8118 if (TREE_CODE (arg0) == CONJ_EXPR)
8119 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8123 if (TREE_CODE (arg0) == INTEGER_CST)
8124 return fold_not_const (arg0, type);
8125 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8126 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8127 /* Convert ~ (-A) to A - 1. */
8128 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8129 return fold_build2_loc (loc, MINUS_EXPR, type,
8130 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8131 build_int_cst (type, 1));
8132 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8133 else if (INTEGRAL_TYPE_P (type)
8134 && ((TREE_CODE (arg0) == MINUS_EXPR
8135 && integer_onep (TREE_OPERAND (arg0, 1)))
8136 || (TREE_CODE (arg0) == PLUS_EXPR
8137 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8138 return fold_build1_loc (loc, NEGATE_EXPR, type,
8139 fold_convert_loc (loc, type,
8140 TREE_OPERAND (arg0, 0)));
8141 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8142 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8143 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8144 fold_convert_loc (loc, type,
8145 TREE_OPERAND (arg0, 0)))))
8146 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8147 fold_convert_loc (loc, type,
8148 TREE_OPERAND (arg0, 1)));
8149 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8150 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8151 fold_convert_loc (loc, type,
8152 TREE_OPERAND (arg0, 1)))))
8153 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8154 fold_convert_loc (loc, type,
8155 TREE_OPERAND (arg0, 0)), tem);
8156 /* Perform BIT_NOT_EXPR on each element individually. */
8157 else if (TREE_CODE (arg0) == VECTOR_CST)
8159 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8160 int count = TYPE_VECTOR_SUBPARTS (type), i;
8162 for (i = 0; i < count; i++)
8166 elem = TREE_VALUE (elements);
8167 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8168 if (elem == NULL_TREE)
8170 elements = TREE_CHAIN (elements);
8173 elem = build_int_cst (TREE_TYPE (type), -1);
8174 list = tree_cons (NULL_TREE, elem, list);
8177 return build_vector (type, nreverse (list));
8182 case TRUTH_NOT_EXPR:
8183 /* The argument to invert_truthvalue must have Boolean type. */
8184 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8185 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8187 /* Note that the operand of this must be an int
8188 and its values must be 0 or 1.
8189 ("true" is a fixed value perhaps depending on the language,
8190 but we don't handle values other than 1 correctly yet.) */
8191 tem = fold_truth_not_expr (loc, arg0);
8194 return fold_convert_loc (loc, type, tem);
8197 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8198 return fold_convert_loc (loc, type, arg0);
8199 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8200 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8201 TREE_OPERAND (arg0, 1));
8202 if (TREE_CODE (arg0) == COMPLEX_CST)
8203 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8204 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8206 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8207 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8208 fold_build1_loc (loc, REALPART_EXPR, itype,
8209 TREE_OPERAND (arg0, 0)),
8210 fold_build1_loc (loc, REALPART_EXPR, itype,
8211 TREE_OPERAND (arg0, 1)));
8212 return fold_convert_loc (loc, type, tem);
8214 if (TREE_CODE (arg0) == CONJ_EXPR)
8216 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8217 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8218 TREE_OPERAND (arg0, 0));
8219 return fold_convert_loc (loc, type, tem);
8221 if (TREE_CODE (arg0) == CALL_EXPR)
8223 tree fn = get_callee_fndecl (arg0);
8224 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8225 switch (DECL_FUNCTION_CODE (fn))
8227 CASE_FLT_FN (BUILT_IN_CEXPI):
8228 fn = mathfn_built_in (type, BUILT_IN_COS);
8230 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8240 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8241 return build_zero_cst (type);
8242 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8243 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8244 TREE_OPERAND (arg0, 0));
8245 if (TREE_CODE (arg0) == COMPLEX_CST)
8246 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8247 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8249 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8250 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8251 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8252 TREE_OPERAND (arg0, 0)),
8253 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8254 TREE_OPERAND (arg0, 1)));
8255 return fold_convert_loc (loc, type, tem);
8257 if (TREE_CODE (arg0) == CONJ_EXPR)
8259 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8260 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8261 return fold_convert_loc (loc, type, negate_expr (tem));
8263 if (TREE_CODE (arg0) == CALL_EXPR)
8265 tree fn = get_callee_fndecl (arg0);
8266 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8267 switch (DECL_FUNCTION_CODE (fn))
8269 CASE_FLT_FN (BUILT_IN_CEXPI):
8270 fn = mathfn_built_in (type, BUILT_IN_SIN);
8272 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8282 /* Fold *&X to X if X is an lvalue. */
8283 if (TREE_CODE (op0) == ADDR_EXPR)
8285 tree op00 = TREE_OPERAND (op0, 0);
8286 if ((TREE_CODE (op00) == VAR_DECL
8287 || TREE_CODE (op00) == PARM_DECL
8288 || TREE_CODE (op00) == RESULT_DECL)
8289 && !TREE_READONLY (op00))
8296 } /* switch (code) */
8300 /* If the operation was a conversion do _not_ mark a resulting constant
8301 with TREE_OVERFLOW if the original constant was not. These conversions
8302 have implementation defined behavior and retaining the TREE_OVERFLOW
8303 flag here would confuse later passes such as VRP. */
8305 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8306 tree type, tree op0)
8308 tree res = fold_unary_loc (loc, code, type, op0);
8310 && TREE_CODE (res) == INTEGER_CST
8311 && TREE_CODE (op0) == INTEGER_CST
8312 && CONVERT_EXPR_CODE_P (code))
8313 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8318 /* Fold a binary expression of code CODE and type TYPE with operands
8319 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8320 Return the folded expression if folding is successful. Otherwise,
8321 return NULL_TREE. */
8324 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8326 enum tree_code compl_code;
8328 if (code == MIN_EXPR)
8329 compl_code = MAX_EXPR;
8330 else if (code == MAX_EXPR)
8331 compl_code = MIN_EXPR;
8335 /* MIN (MAX (a, b), b) == b. */
8336 if (TREE_CODE (op0) == compl_code
8337 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8338 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8340 /* MIN (MAX (b, a), b) == b. */
8341 if (TREE_CODE (op0) == compl_code
8342 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8343 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8344 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8346 /* MIN (a, MAX (a, b)) == a. */
8347 if (TREE_CODE (op1) == compl_code
8348 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8349 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8350 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8352 /* MIN (a, MAX (b, a)) == a. */
8353 if (TREE_CODE (op1) == compl_code
8354 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8355 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8356 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8361 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8362 by changing CODE to reduce the magnitude of constants involved in
8363 ARG0 of the comparison.
8364 Returns a canonicalized comparison tree if a simplification was
8365 possible, otherwise returns NULL_TREE.
8366 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8367 valid if signed overflow is undefined. */
8370 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8371 tree arg0, tree arg1,
8372 bool *strict_overflow_p)
8374 enum tree_code code0 = TREE_CODE (arg0);
8375 tree t, cst0 = NULL_TREE;
8379 /* Match A +- CST code arg1 and CST code arg1. We can change the
8380 first form only if overflow is undefined. */
8381 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8382 /* In principle pointers also have undefined overflow behavior,
8383 but that causes problems elsewhere. */
8384 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8385 && (code0 == MINUS_EXPR
8386 || code0 == PLUS_EXPR)
8387 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8388 || code0 == INTEGER_CST))
8391 /* Identify the constant in arg0 and its sign. */
8392 if (code0 == INTEGER_CST)
8395 cst0 = TREE_OPERAND (arg0, 1);
8396 sgn0 = tree_int_cst_sgn (cst0);
8398 /* Overflowed constants and zero will cause problems. */
8399 if (integer_zerop (cst0)
8400 || TREE_OVERFLOW (cst0))
8403 /* See if we can reduce the magnitude of the constant in
8404 arg0 by changing the comparison code. */
8405 if (code0 == INTEGER_CST)
8407 /* CST <= arg1 -> CST-1 < arg1. */
8408 if (code == LE_EXPR && sgn0 == 1)
8410 /* -CST < arg1 -> -CST-1 <= arg1. */
8411 else if (code == LT_EXPR && sgn0 == -1)
8413 /* CST > arg1 -> CST-1 >= arg1. */
8414 else if (code == GT_EXPR && sgn0 == 1)
8416 /* -CST >= arg1 -> -CST-1 > arg1. */
8417 else if (code == GE_EXPR && sgn0 == -1)
8421 /* arg1 code' CST' might be more canonical. */
8426 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8428 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8430 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8431 else if (code == GT_EXPR
8432 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8434 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8435 else if (code == LE_EXPR
8436 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8438 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8439 else if (code == GE_EXPR
8440 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8444 *strict_overflow_p = true;
8447 /* Now build the constant reduced in magnitude. But not if that
8448 would produce one outside of its types range. */
8449 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8451 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8452 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8454 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8455 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8456 /* We cannot swap the comparison here as that would cause us to
8457 endlessly recurse. */
8460 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8461 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8462 if (code0 != INTEGER_CST)
8463 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8465 /* If swapping might yield to a more canonical form, do so. */
8467 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8469 return fold_build2_loc (loc, code, type, t, arg1);
8472 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8473 overflow further. Try to decrease the magnitude of constants involved
8474 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8475 and put sole constants at the second argument position.
8476 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8479 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8480 tree arg0, tree arg1)
8483 bool strict_overflow_p;
8484 const char * const warnmsg = G_("assuming signed overflow does not occur "
8485 "when reducing constant in comparison");
8487 /* Try canonicalization by simplifying arg0. */
8488 strict_overflow_p = false;
8489 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8490 &strict_overflow_p);
8493 if (strict_overflow_p)
8494 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8498 /* Try canonicalization by simplifying arg1 using the swapped
8500 code = swap_tree_comparison (code);
8501 strict_overflow_p = false;
8502 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8503 &strict_overflow_p);
8504 if (t && strict_overflow_p)
8505 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8509 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8510 space. This is used to avoid issuing overflow warnings for
8511 expressions like &p->x which can not wrap. */
8514 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8516 unsigned HOST_WIDE_INT offset_low, total_low;
8517 HOST_WIDE_INT size, offset_high, total_high;
8519 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8525 if (offset == NULL_TREE)
8530 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8534 offset_low = TREE_INT_CST_LOW (offset);
8535 offset_high = TREE_INT_CST_HIGH (offset);
8538 if (add_double_with_sign (offset_low, offset_high,
8539 bitpos / BITS_PER_UNIT, 0,
8540 &total_low, &total_high,
8544 if (total_high != 0)
8547 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8551 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8553 if (TREE_CODE (base) == ADDR_EXPR)
8555 HOST_WIDE_INT base_size;
8557 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8558 if (base_size > 0 && size < base_size)
8562 return total_low > (unsigned HOST_WIDE_INT) size;
8565 /* Subroutine of fold_binary. This routine performs all of the
8566 transformations that are common to the equality/inequality
8567 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8568 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8569 fold_binary should call fold_binary. Fold a comparison with
8570 tree code CODE and type TYPE with operands OP0 and OP1. Return
8571 the folded comparison or NULL_TREE. */
8574 fold_comparison (location_t loc, enum tree_code code, tree type,
8577 tree arg0, arg1, tem;
8582 STRIP_SIGN_NOPS (arg0);
8583 STRIP_SIGN_NOPS (arg1);
8585 tem = fold_relational_const (code, type, arg0, arg1);
8586 if (tem != NULL_TREE)
8589 /* If one arg is a real or integer constant, put it last. */
8590 if (tree_swap_operands_p (arg0, arg1, true))
8591 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8593 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8594 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8595 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8596 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8597 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8598 && (TREE_CODE (arg1) == INTEGER_CST
8599 && !TREE_OVERFLOW (arg1)))
8601 tree const1 = TREE_OPERAND (arg0, 1);
8603 tree variable = TREE_OPERAND (arg0, 0);
8606 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8608 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8609 TREE_TYPE (arg1), const2, const1);
8611 /* If the constant operation overflowed this can be
8612 simplified as a comparison against INT_MAX/INT_MIN. */
8613 if (TREE_CODE (lhs) == INTEGER_CST
8614 && TREE_OVERFLOW (lhs))
8616 int const1_sgn = tree_int_cst_sgn (const1);
8617 enum tree_code code2 = code;
8619 /* Get the sign of the constant on the lhs if the
8620 operation were VARIABLE + CONST1. */
8621 if (TREE_CODE (arg0) == MINUS_EXPR)
8622 const1_sgn = -const1_sgn;
8624 /* The sign of the constant determines if we overflowed
8625 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8626 Canonicalize to the INT_MIN overflow by swapping the comparison
8628 if (const1_sgn == -1)
8629 code2 = swap_tree_comparison (code);
8631 /* We now can look at the canonicalized case
8632 VARIABLE + 1 CODE2 INT_MIN
8633 and decide on the result. */
8634 if (code2 == LT_EXPR
8636 || code2 == EQ_EXPR)
8637 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8638 else if (code2 == NE_EXPR
8640 || code2 == GT_EXPR)
8641 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8644 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8645 && (TREE_CODE (lhs) != INTEGER_CST
8646 || !TREE_OVERFLOW (lhs)))
8648 fold_overflow_warning ("assuming signed overflow does not occur "
8649 "when changing X +- C1 cmp C2 to "
8651 WARN_STRICT_OVERFLOW_COMPARISON);
8652 return fold_build2_loc (loc, code, type, variable, lhs);
8656 /* For comparisons of pointers we can decompose it to a compile time
8657 comparison of the base objects and the offsets into the object.
8658 This requires at least one operand being an ADDR_EXPR or a
8659 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8660 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8661 && (TREE_CODE (arg0) == ADDR_EXPR
8662 || TREE_CODE (arg1) == ADDR_EXPR
8663 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8664 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8666 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8667 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8668 enum machine_mode mode;
8669 int volatilep, unsignedp;
8670 bool indirect_base0 = false, indirect_base1 = false;
8672 /* Get base and offset for the access. Strip ADDR_EXPR for
8673 get_inner_reference, but put it back by stripping INDIRECT_REF
8674 off the base object if possible. indirect_baseN will be true
8675 if baseN is not an address but refers to the object itself. */
8677 if (TREE_CODE (arg0) == ADDR_EXPR)
8679 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8680 &bitsize, &bitpos0, &offset0, &mode,
8681 &unsignedp, &volatilep, false);
8682 if (TREE_CODE (base0) == INDIRECT_REF)
8683 base0 = TREE_OPERAND (base0, 0);
8685 indirect_base0 = true;
8687 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8689 base0 = TREE_OPERAND (arg0, 0);
8690 STRIP_SIGN_NOPS (base0);
8691 if (TREE_CODE (base0) == ADDR_EXPR)
8693 base0 = TREE_OPERAND (base0, 0);
8694 indirect_base0 = true;
8696 offset0 = TREE_OPERAND (arg0, 1);
8700 if (TREE_CODE (arg1) == ADDR_EXPR)
8702 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8703 &bitsize, &bitpos1, &offset1, &mode,
8704 &unsignedp, &volatilep, false);
8705 if (TREE_CODE (base1) == INDIRECT_REF)
8706 base1 = TREE_OPERAND (base1, 0);
8708 indirect_base1 = true;
8710 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8712 base1 = TREE_OPERAND (arg1, 0);
8713 STRIP_SIGN_NOPS (base1);
8714 if (TREE_CODE (base1) == ADDR_EXPR)
8716 base1 = TREE_OPERAND (base1, 0);
8717 indirect_base1 = true;
8719 offset1 = TREE_OPERAND (arg1, 1);
8722 /* A local variable can never be pointed to by
8723 the default SSA name of an incoming parameter. */
8724 if ((TREE_CODE (arg0) == ADDR_EXPR
8726 && TREE_CODE (base0) == VAR_DECL
8727 && auto_var_in_fn_p (base0, current_function_decl)
8729 && TREE_CODE (base1) == SSA_NAME
8730 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8731 && SSA_NAME_IS_DEFAULT_DEF (base1))
8732 || (TREE_CODE (arg1) == ADDR_EXPR
8734 && TREE_CODE (base1) == VAR_DECL
8735 && auto_var_in_fn_p (base1, current_function_decl)
8737 && TREE_CODE (base0) == SSA_NAME
8738 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8739 && SSA_NAME_IS_DEFAULT_DEF (base0)))
8741 if (code == NE_EXPR)
8742 return constant_boolean_node (1, type);
8743 else if (code == EQ_EXPR)
8744 return constant_boolean_node (0, type);
8746 /* If we have equivalent bases we might be able to simplify. */
8747 else if (indirect_base0 == indirect_base1
8748 && operand_equal_p (base0, base1, 0))
8750 /* We can fold this expression to a constant if the non-constant
8751 offset parts are equal. */
8752 if ((offset0 == offset1
8753 || (offset0 && offset1
8754 && operand_equal_p (offset0, offset1, 0)))
8757 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8762 && bitpos0 != bitpos1
8763 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8764 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8765 fold_overflow_warning (("assuming pointer wraparound does not "
8766 "occur when comparing P +- C1 with "
8768 WARN_STRICT_OVERFLOW_CONDITIONAL);
8773 return constant_boolean_node (bitpos0 == bitpos1, type);
8775 return constant_boolean_node (bitpos0 != bitpos1, type);
8777 return constant_boolean_node (bitpos0 < bitpos1, type);
8779 return constant_boolean_node (bitpos0 <= bitpos1, type);
8781 return constant_boolean_node (bitpos0 >= bitpos1, type);
8783 return constant_boolean_node (bitpos0 > bitpos1, type);
8787 /* We can simplify the comparison to a comparison of the variable
8788 offset parts if the constant offset parts are equal.
8789 Be careful to use signed size type here because otherwise we
8790 mess with array offsets in the wrong way. This is possible
8791 because pointer arithmetic is restricted to retain within an
8792 object and overflow on pointer differences is undefined as of
8793 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8794 else if (bitpos0 == bitpos1
8795 && ((code == EQ_EXPR || code == NE_EXPR)
8796 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8798 /* By converting to signed size type we cover middle-end pointer
8799 arithmetic which operates on unsigned pointer types of size
8800 type size and ARRAY_REF offsets which are properly sign or
8801 zero extended from their type in case it is narrower than
8803 if (offset0 == NULL_TREE)
8804 offset0 = build_int_cst (ssizetype, 0);
8806 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8807 if (offset1 == NULL_TREE)
8808 offset1 = build_int_cst (ssizetype, 0);
8810 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8814 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8815 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8816 fold_overflow_warning (("assuming pointer wraparound does not "
8817 "occur when comparing P +- C1 with "
8819 WARN_STRICT_OVERFLOW_COMPARISON);
8821 return fold_build2_loc (loc, code, type, offset0, offset1);
8824 /* For non-equal bases we can simplify if they are addresses
8825 of local binding decls or constants. */
8826 else if (indirect_base0 && indirect_base1
8827 /* We know that !operand_equal_p (base0, base1, 0)
8828 because the if condition was false. But make
8829 sure two decls are not the same. */
8831 && TREE_CODE (arg0) == ADDR_EXPR
8832 && TREE_CODE (arg1) == ADDR_EXPR
8833 && (((TREE_CODE (base0) == VAR_DECL
8834 || TREE_CODE (base0) == PARM_DECL)
8835 && (targetm.binds_local_p (base0)
8836 || CONSTANT_CLASS_P (base1)))
8837 || CONSTANT_CLASS_P (base0))
8838 && (((TREE_CODE (base1) == VAR_DECL
8839 || TREE_CODE (base1) == PARM_DECL)
8840 && (targetm.binds_local_p (base1)
8841 || CONSTANT_CLASS_P (base0)))
8842 || CONSTANT_CLASS_P (base1)))
8844 if (code == EQ_EXPR)
8845 return omit_two_operands_loc (loc, type, boolean_false_node,
8847 else if (code == NE_EXPR)
8848 return omit_two_operands_loc (loc, type, boolean_true_node,
8851 /* For equal offsets we can simplify to a comparison of the
8853 else if (bitpos0 == bitpos1
8855 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8857 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8858 && ((offset0 == offset1)
8859 || (offset0 && offset1
8860 && operand_equal_p (offset0, offset1, 0))))
8863 base0 = build_fold_addr_expr_loc (loc, base0);
8865 base1 = build_fold_addr_expr_loc (loc, base1);
8866 return fold_build2_loc (loc, code, type, base0, base1);
8870 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8871 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8872 the resulting offset is smaller in absolute value than the
8874 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8875 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8876 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8877 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8878 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8879 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8880 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8882 tree const1 = TREE_OPERAND (arg0, 1);
8883 tree const2 = TREE_OPERAND (arg1, 1);
8884 tree variable1 = TREE_OPERAND (arg0, 0);
8885 tree variable2 = TREE_OPERAND (arg1, 0);
8887 const char * const warnmsg = G_("assuming signed overflow does not "
8888 "occur when combining constants around "
8891 /* Put the constant on the side where it doesn't overflow and is
8892 of lower absolute value than before. */
8893 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8894 ? MINUS_EXPR : PLUS_EXPR,
8896 if (!TREE_OVERFLOW (cst)
8897 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8899 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8900 return fold_build2_loc (loc, code, type,
8902 fold_build2_loc (loc,
8903 TREE_CODE (arg1), TREE_TYPE (arg1),
8907 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8908 ? MINUS_EXPR : PLUS_EXPR,
8910 if (!TREE_OVERFLOW (cst)
8911 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8913 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8914 return fold_build2_loc (loc, code, type,
8915 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8921 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8922 signed arithmetic case. That form is created by the compiler
8923 often enough for folding it to be of value. One example is in
8924 computing loop trip counts after Operator Strength Reduction. */
8925 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8926 && TREE_CODE (arg0) == MULT_EXPR
8927 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8928 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8929 && integer_zerop (arg1))
8931 tree const1 = TREE_OPERAND (arg0, 1);
8932 tree const2 = arg1; /* zero */
8933 tree variable1 = TREE_OPERAND (arg0, 0);
8934 enum tree_code cmp_code = code;
8936 /* Handle unfolded multiplication by zero. */
8937 if (integer_zerop (const1))
8938 return fold_build2_loc (loc, cmp_code, type, const1, const2);
8940 fold_overflow_warning (("assuming signed overflow does not occur when "
8941 "eliminating multiplication in comparison "
8943 WARN_STRICT_OVERFLOW_COMPARISON);
8945 /* If const1 is negative we swap the sense of the comparison. */
8946 if (tree_int_cst_sgn (const1) < 0)
8947 cmp_code = swap_tree_comparison (cmp_code);
8949 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8952 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8956 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8958 tree targ0 = strip_float_extensions (arg0);
8959 tree targ1 = strip_float_extensions (arg1);
8960 tree newtype = TREE_TYPE (targ0);
8962 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8963 newtype = TREE_TYPE (targ1);
8965 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8966 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8967 return fold_build2_loc (loc, code, type,
8968 fold_convert_loc (loc, newtype, targ0),
8969 fold_convert_loc (loc, newtype, targ1));
8971 /* (-a) CMP (-b) -> b CMP a */
8972 if (TREE_CODE (arg0) == NEGATE_EXPR
8973 && TREE_CODE (arg1) == NEGATE_EXPR)
8974 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8975 TREE_OPERAND (arg0, 0));
8977 if (TREE_CODE (arg1) == REAL_CST)
8979 REAL_VALUE_TYPE cst;
8980 cst = TREE_REAL_CST (arg1);
8982 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8983 if (TREE_CODE (arg0) == NEGATE_EXPR)
8984 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8985 TREE_OPERAND (arg0, 0),
8986 build_real (TREE_TYPE (arg1),
8987 real_value_negate (&cst)));
8989 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8990 /* a CMP (-0) -> a CMP 0 */
8991 if (REAL_VALUE_MINUS_ZERO (cst))
8992 return fold_build2_loc (loc, code, type, arg0,
8993 build_real (TREE_TYPE (arg1), dconst0));
8995 /* x != NaN is always true, other ops are always false. */
8996 if (REAL_VALUE_ISNAN (cst)
8997 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8999 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9000 return omit_one_operand_loc (loc, type, tem, arg0);
9003 /* Fold comparisons against infinity. */
9004 if (REAL_VALUE_ISINF (cst)
9005 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9007 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9008 if (tem != NULL_TREE)
9013 /* If this is a comparison of a real constant with a PLUS_EXPR
9014 or a MINUS_EXPR of a real constant, we can convert it into a
9015 comparison with a revised real constant as long as no overflow
9016 occurs when unsafe_math_optimizations are enabled. */
9017 if (flag_unsafe_math_optimizations
9018 && TREE_CODE (arg1) == REAL_CST
9019 && (TREE_CODE (arg0) == PLUS_EXPR
9020 || TREE_CODE (arg0) == MINUS_EXPR)
9021 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9022 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9023 ? MINUS_EXPR : PLUS_EXPR,
9024 arg1, TREE_OPERAND (arg0, 1)))
9025 && !TREE_OVERFLOW (tem))
9026 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9028 /* Likewise, we can simplify a comparison of a real constant with
9029 a MINUS_EXPR whose first operand is also a real constant, i.e.
9030 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9031 floating-point types only if -fassociative-math is set. */
9032 if (flag_associative_math
9033 && TREE_CODE (arg1) == REAL_CST
9034 && TREE_CODE (arg0) == MINUS_EXPR
9035 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9036 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9038 && !TREE_OVERFLOW (tem))
9039 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9040 TREE_OPERAND (arg0, 1), tem);
9042 /* Fold comparisons against built-in math functions. */
9043 if (TREE_CODE (arg1) == REAL_CST
9044 && flag_unsafe_math_optimizations
9045 && ! flag_errno_math)
9047 enum built_in_function fcode = builtin_mathfn_code (arg0);
9049 if (fcode != END_BUILTINS)
9051 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9052 if (tem != NULL_TREE)
9058 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9059 && CONVERT_EXPR_P (arg0))
9061 /* If we are widening one operand of an integer comparison,
9062 see if the other operand is similarly being widened. Perhaps we
9063 can do the comparison in the narrower type. */
9064 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9068 /* Or if we are changing signedness. */
9069 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9074 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9075 constant, we can simplify it. */
9076 if (TREE_CODE (arg1) == INTEGER_CST
9077 && (TREE_CODE (arg0) == MIN_EXPR
9078 || TREE_CODE (arg0) == MAX_EXPR)
9079 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9081 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9086 /* Simplify comparison of something with itself. (For IEEE
9087 floating-point, we can only do some of these simplifications.) */
9088 if (operand_equal_p (arg0, arg1, 0))
9093 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9094 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9095 return constant_boolean_node (1, type);
9100 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9101 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9102 return constant_boolean_node (1, type);
9103 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9106 /* For NE, we can only do this simplification if integer
9107 or we don't honor IEEE floating point NaNs. */
9108 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9109 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9111 /* ... fall through ... */
9114 return constant_boolean_node (0, type);
9120 /* If we are comparing an expression that just has comparisons
9121 of two integer values, arithmetic expressions of those comparisons,
9122 and constants, we can simplify it. There are only three cases
9123 to check: the two values can either be equal, the first can be
9124 greater, or the second can be greater. Fold the expression for
9125 those three values. Since each value must be 0 or 1, we have
9126 eight possibilities, each of which corresponds to the constant 0
9127 or 1 or one of the six possible comparisons.
9129 This handles common cases like (a > b) == 0 but also handles
9130 expressions like ((x > y) - (y > x)) > 0, which supposedly
9131 occur in macroized code. */
9133 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9135 tree cval1 = 0, cval2 = 0;
9138 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9139 /* Don't handle degenerate cases here; they should already
9140 have been handled anyway. */
9141 && cval1 != 0 && cval2 != 0
9142 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9143 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9144 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9145 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9146 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9147 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9148 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9150 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9151 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9153 /* We can't just pass T to eval_subst in case cval1 or cval2
9154 was the same as ARG1. */
9157 = fold_build2_loc (loc, code, type,
9158 eval_subst (loc, arg0, cval1, maxval,
9162 = fold_build2_loc (loc, code, type,
9163 eval_subst (loc, arg0, cval1, maxval,
9167 = fold_build2_loc (loc, code, type,
9168 eval_subst (loc, arg0, cval1, minval,
9172 /* All three of these results should be 0 or 1. Confirm they are.
9173 Then use those values to select the proper code to use. */
9175 if (TREE_CODE (high_result) == INTEGER_CST
9176 && TREE_CODE (equal_result) == INTEGER_CST
9177 && TREE_CODE (low_result) == INTEGER_CST)
9179 /* Make a 3-bit mask with the high-order bit being the
9180 value for `>', the next for '=', and the low for '<'. */
9181 switch ((integer_onep (high_result) * 4)
9182 + (integer_onep (equal_result) * 2)
9183 + integer_onep (low_result))
9187 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9208 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9213 tem = save_expr (build2 (code, type, cval1, cval2));
9214 SET_EXPR_LOCATION (tem, loc);
9217 return fold_build2_loc (loc, code, type, cval1, cval2);
9222 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9223 into a single range test. */
9224 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9225 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9226 && TREE_CODE (arg1) == INTEGER_CST
9227 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9228 && !integer_zerop (TREE_OPERAND (arg0, 1))
9229 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9230 && !TREE_OVERFLOW (arg1))
9232 tem = fold_div_compare (loc, code, type, arg0, arg1);
9233 if (tem != NULL_TREE)
9237 /* Fold ~X op ~Y as Y op X. */
9238 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9239 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9241 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9242 return fold_build2_loc (loc, code, type,
9243 fold_convert_loc (loc, cmp_type,
9244 TREE_OPERAND (arg1, 0)),
9245 TREE_OPERAND (arg0, 0));
9248 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9249 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9250 && TREE_CODE (arg1) == INTEGER_CST)
9252 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9253 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9254 TREE_OPERAND (arg0, 0),
9255 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9256 fold_convert_loc (loc, cmp_type, arg1)));
9263 /* Subroutine of fold_binary. Optimize complex multiplications of the
9264 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9265 argument EXPR represents the expression "z" of type TYPE. */
9268 fold_mult_zconjz (location_t loc, tree type, tree expr)
9270 tree itype = TREE_TYPE (type);
9271 tree rpart, ipart, tem;
9273 if (TREE_CODE (expr) == COMPLEX_EXPR)
9275 rpart = TREE_OPERAND (expr, 0);
9276 ipart = TREE_OPERAND (expr, 1);
9278 else if (TREE_CODE (expr) == COMPLEX_CST)
9280 rpart = TREE_REALPART (expr);
9281 ipart = TREE_IMAGPART (expr);
9285 expr = save_expr (expr);
9286 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9287 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9290 rpart = save_expr (rpart);
9291 ipart = save_expr (ipart);
9292 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9293 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9294 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9295 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9296 build_zero_cst (itype));
9300 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9301 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9302 guarantees that P and N have the same least significant log2(M) bits.
9303 N is not otherwise constrained. In particular, N is not normalized to
9304 0 <= N < M as is common. In general, the precise value of P is unknown.
9305 M is chosen as large as possible such that constant N can be determined.
9307 Returns M and sets *RESIDUE to N.
9309 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9310 account. This is not always possible due to PR 35705.
9313 static unsigned HOST_WIDE_INT
9314 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9315 bool allow_func_align)
9317 enum tree_code code;
9321 code = TREE_CODE (expr);
9322 if (code == ADDR_EXPR)
9324 expr = TREE_OPERAND (expr, 0);
9325 if (handled_component_p (expr))
9327 HOST_WIDE_INT bitsize, bitpos;
9329 enum machine_mode mode;
9330 int unsignedp, volatilep;
9332 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9333 &mode, &unsignedp, &volatilep, false);
9334 *residue = bitpos / BITS_PER_UNIT;
9337 if (TREE_CODE (offset) == INTEGER_CST)
9338 *residue += TREE_INT_CST_LOW (offset);
9340 /* We don't handle more complicated offset expressions. */
9346 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9347 return DECL_ALIGN_UNIT (expr);
9349 else if (code == POINTER_PLUS_EXPR)
9352 unsigned HOST_WIDE_INT modulus;
9353 enum tree_code inner_code;
9355 op0 = TREE_OPERAND (expr, 0);
9357 modulus = get_pointer_modulus_and_residue (op0, residue,
9360 op1 = TREE_OPERAND (expr, 1);
9362 inner_code = TREE_CODE (op1);
9363 if (inner_code == INTEGER_CST)
9365 *residue += TREE_INT_CST_LOW (op1);
9368 else if (inner_code == MULT_EXPR)
9370 op1 = TREE_OPERAND (op1, 1);
9371 if (TREE_CODE (op1) == INTEGER_CST)
9373 unsigned HOST_WIDE_INT align;
9375 /* Compute the greatest power-of-2 divisor of op1. */
9376 align = TREE_INT_CST_LOW (op1);
9379 /* If align is non-zero and less than *modulus, replace
9380 *modulus with align., If align is 0, then either op1 is 0
9381 or the greatest power-of-2 divisor of op1 doesn't fit in an
9382 unsigned HOST_WIDE_INT. In either case, no additional
9383 constraint is imposed. */
9385 modulus = MIN (modulus, align);
9392 /* If we get here, we were unable to determine anything useful about the
9398 /* Fold a binary expression of code CODE and type TYPE with operands
9399 OP0 and OP1. LOC is the location of the resulting expression.
9400 Return the folded expression if folding is successful. Otherwise,
9401 return NULL_TREE. */
9404 fold_binary_loc (location_t loc,
9405 enum tree_code code, tree type, tree op0, tree op1)
9407 enum tree_code_class kind = TREE_CODE_CLASS (code);
9408 tree arg0, arg1, tem;
9409 tree t1 = NULL_TREE;
9410 bool strict_overflow_p;
9412 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9413 && TREE_CODE_LENGTH (code) == 2
9415 && op1 != NULL_TREE);
9420 /* Strip any conversions that don't change the mode. This is
9421 safe for every expression, except for a comparison expression
9422 because its signedness is derived from its operands. So, in
9423 the latter case, only strip conversions that don't change the
9424 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9427 Note that this is done as an internal manipulation within the
9428 constant folder, in order to find the simplest representation
9429 of the arguments so that their form can be studied. In any
9430 cases, the appropriate type conversions should be put back in
9431 the tree that will get out of the constant folder. */
9433 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9435 STRIP_SIGN_NOPS (arg0);
9436 STRIP_SIGN_NOPS (arg1);
9444 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9445 constant but we can't do arithmetic on them. */
9446 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9447 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9448 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9449 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9450 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9451 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9453 if (kind == tcc_binary)
9455 /* Make sure type and arg0 have the same saturating flag. */
9456 gcc_assert (TYPE_SATURATING (type)
9457 == TYPE_SATURATING (TREE_TYPE (arg0)));
9458 tem = const_binop (code, arg0, arg1);
9460 else if (kind == tcc_comparison)
9461 tem = fold_relational_const (code, type, arg0, arg1);
9465 if (tem != NULL_TREE)
9467 if (TREE_TYPE (tem) != type)
9468 tem = fold_convert_loc (loc, type, tem);
9473 /* If this is a commutative operation, and ARG0 is a constant, move it
9474 to ARG1 to reduce the number of tests below. */
9475 if (commutative_tree_code (code)
9476 && tree_swap_operands_p (arg0, arg1, true))
9477 return fold_build2_loc (loc, code, type, op1, op0);
9479 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9481 First check for cases where an arithmetic operation is applied to a
9482 compound, conditional, or comparison operation. Push the arithmetic
9483 operation inside the compound or conditional to see if any folding
9484 can then be done. Convert comparison to conditional for this purpose.
9485 The also optimizes non-constant cases that used to be done in
9488 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9489 one of the operands is a comparison and the other is a comparison, a
9490 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9491 code below would make the expression more complex. Change it to a
9492 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9493 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9495 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9496 || code == EQ_EXPR || code == NE_EXPR)
9497 && ((truth_value_p (TREE_CODE (arg0))
9498 && (truth_value_p (TREE_CODE (arg1))
9499 || (TREE_CODE (arg1) == BIT_AND_EXPR
9500 && integer_onep (TREE_OPERAND (arg1, 1)))))
9501 || (truth_value_p (TREE_CODE (arg1))
9502 && (truth_value_p (TREE_CODE (arg0))
9503 || (TREE_CODE (arg0) == BIT_AND_EXPR
9504 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9506 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9507 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9510 fold_convert_loc (loc, boolean_type_node, arg0),
9511 fold_convert_loc (loc, boolean_type_node, arg1));
9513 if (code == EQ_EXPR)
9514 tem = invert_truthvalue_loc (loc, tem);
9516 return fold_convert_loc (loc, type, tem);
9519 if (TREE_CODE_CLASS (code) == tcc_binary
9520 || TREE_CODE_CLASS (code) == tcc_comparison)
9522 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9524 tem = fold_build2_loc (loc, code, type,
9525 fold_convert_loc (loc, TREE_TYPE (op0),
9526 TREE_OPERAND (arg0, 1)), op1);
9527 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9528 goto fold_binary_exit;
9530 if (TREE_CODE (arg1) == COMPOUND_EXPR
9531 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9533 tem = fold_build2_loc (loc, code, type, op0,
9534 fold_convert_loc (loc, TREE_TYPE (op1),
9535 TREE_OPERAND (arg1, 1)));
9536 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9537 goto fold_binary_exit;
9540 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9542 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9544 /*cond_first_p=*/1);
9545 if (tem != NULL_TREE)
9549 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9551 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9553 /*cond_first_p=*/0);
9554 if (tem != NULL_TREE)
9562 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9563 if (TREE_CODE (arg0) == ADDR_EXPR
9564 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9566 tree iref = TREE_OPERAND (arg0, 0);
9567 return fold_build2 (MEM_REF, type,
9568 TREE_OPERAND (iref, 0),
9569 int_const_binop (PLUS_EXPR, arg1,
9570 TREE_OPERAND (iref, 1), 0));
9573 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9574 if (TREE_CODE (arg0) == ADDR_EXPR
9575 && handled_component_p (TREE_OPERAND (arg0, 0)))
9578 HOST_WIDE_INT coffset;
9579 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9583 return fold_build2 (MEM_REF, type,
9584 build_fold_addr_expr (base),
9585 int_const_binop (PLUS_EXPR, arg1,
9586 size_int (coffset), 0));
9591 case POINTER_PLUS_EXPR:
9592 /* 0 +p index -> (type)index */
9593 if (integer_zerop (arg0))
9594 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9596 /* PTR +p 0 -> PTR */
9597 if (integer_zerop (arg1))
9598 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9600 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9601 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9602 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9603 return fold_convert_loc (loc, type,
9604 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9605 fold_convert_loc (loc, sizetype,
9607 fold_convert_loc (loc, sizetype,
9610 /* index +p PTR -> PTR +p index */
9611 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9612 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9613 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9614 fold_convert_loc (loc, type, arg1),
9615 fold_convert_loc (loc, sizetype, arg0));
9617 /* (PTR +p B) +p A -> PTR +p (B + A) */
9618 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9621 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9622 tree arg00 = TREE_OPERAND (arg0, 0);
9623 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9624 arg01, fold_convert_loc (loc, sizetype, arg1));
9625 return fold_convert_loc (loc, type,
9626 fold_build2_loc (loc, POINTER_PLUS_EXPR,
9631 /* PTR_CST +p CST -> CST1 */
9632 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9633 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9634 fold_convert_loc (loc, type, arg1));
9636 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9637 of the array. Loop optimizer sometimes produce this type of
9639 if (TREE_CODE (arg0) == ADDR_EXPR)
9641 tem = try_move_mult_to_index (loc, arg0,
9642 fold_convert_loc (loc, sizetype, arg1));
9644 return fold_convert_loc (loc, type, tem);
9650 /* A + (-B) -> A - B */
9651 if (TREE_CODE (arg1) == NEGATE_EXPR)
9652 return fold_build2_loc (loc, MINUS_EXPR, type,
9653 fold_convert_loc (loc, type, arg0),
9654 fold_convert_loc (loc, type,
9655 TREE_OPERAND (arg1, 0)));
9656 /* (-A) + B -> B - A */
9657 if (TREE_CODE (arg0) == NEGATE_EXPR
9658 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9659 return fold_build2_loc (loc, MINUS_EXPR, type,
9660 fold_convert_loc (loc, type, arg1),
9661 fold_convert_loc (loc, type,
9662 TREE_OPERAND (arg0, 0)));
9664 if (INTEGRAL_TYPE_P (type))
9666 /* Convert ~A + 1 to -A. */
9667 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9668 && integer_onep (arg1))
9669 return fold_build1_loc (loc, NEGATE_EXPR, type,
9670 fold_convert_loc (loc, type,
9671 TREE_OPERAND (arg0, 0)));
9674 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9675 && !TYPE_OVERFLOW_TRAPS (type))
9677 tree tem = TREE_OPERAND (arg0, 0);
9680 if (operand_equal_p (tem, arg1, 0))
9682 t1 = build_int_cst_type (type, -1);
9683 return omit_one_operand_loc (loc, type, t1, arg1);
9688 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9689 && !TYPE_OVERFLOW_TRAPS (type))
9691 tree tem = TREE_OPERAND (arg1, 0);
9694 if (operand_equal_p (arg0, tem, 0))
9696 t1 = build_int_cst_type (type, -1);
9697 return omit_one_operand_loc (loc, type, t1, arg0);
9701 /* X + (X / CST) * -CST is X % CST. */
9702 if (TREE_CODE (arg1) == MULT_EXPR
9703 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9704 && operand_equal_p (arg0,
9705 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9707 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9708 tree cst1 = TREE_OPERAND (arg1, 1);
9709 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9711 if (sum && integer_zerop (sum))
9712 return fold_convert_loc (loc, type,
9713 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9714 TREE_TYPE (arg0), arg0,
9719 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9720 same or one. Make sure type is not saturating.
9721 fold_plusminus_mult_expr will re-associate. */
9722 if ((TREE_CODE (arg0) == MULT_EXPR
9723 || TREE_CODE (arg1) == MULT_EXPR)
9724 && !TYPE_SATURATING (type)
9725 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9727 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9732 if (! FLOAT_TYPE_P (type))
9734 if (integer_zerop (arg1))
9735 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9737 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9738 with a constant, and the two constants have no bits in common,
9739 we should treat this as a BIT_IOR_EXPR since this may produce more
9741 if (TREE_CODE (arg0) == BIT_AND_EXPR
9742 && TREE_CODE (arg1) == BIT_AND_EXPR
9743 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9744 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9745 && integer_zerop (const_binop (BIT_AND_EXPR,
9746 TREE_OPERAND (arg0, 1),
9747 TREE_OPERAND (arg1, 1))))
9749 code = BIT_IOR_EXPR;
9753 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9754 (plus (plus (mult) (mult)) (foo)) so that we can
9755 take advantage of the factoring cases below. */
9756 if (((TREE_CODE (arg0) == PLUS_EXPR
9757 || TREE_CODE (arg0) == MINUS_EXPR)
9758 && TREE_CODE (arg1) == MULT_EXPR)
9759 || ((TREE_CODE (arg1) == PLUS_EXPR
9760 || TREE_CODE (arg1) == MINUS_EXPR)
9761 && TREE_CODE (arg0) == MULT_EXPR))
9763 tree parg0, parg1, parg, marg;
9764 enum tree_code pcode;
9766 if (TREE_CODE (arg1) == MULT_EXPR)
9767 parg = arg0, marg = arg1;
9769 parg = arg1, marg = arg0;
9770 pcode = TREE_CODE (parg);
9771 parg0 = TREE_OPERAND (parg, 0);
9772 parg1 = TREE_OPERAND (parg, 1);
9776 if (TREE_CODE (parg0) == MULT_EXPR
9777 && TREE_CODE (parg1) != MULT_EXPR)
9778 return fold_build2_loc (loc, pcode, type,
9779 fold_build2_loc (loc, PLUS_EXPR, type,
9780 fold_convert_loc (loc, type,
9782 fold_convert_loc (loc, type,
9784 fold_convert_loc (loc, type, parg1));
9785 if (TREE_CODE (parg0) != MULT_EXPR
9786 && TREE_CODE (parg1) == MULT_EXPR)
9788 fold_build2_loc (loc, PLUS_EXPR, type,
9789 fold_convert_loc (loc, type, parg0),
9790 fold_build2_loc (loc, pcode, type,
9791 fold_convert_loc (loc, type, marg),
9792 fold_convert_loc (loc, type,
9798 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9799 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9800 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9802 /* Likewise if the operands are reversed. */
9803 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9804 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9806 /* Convert X + -C into X - C. */
9807 if (TREE_CODE (arg1) == REAL_CST
9808 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9810 tem = fold_negate_const (arg1, type);
9811 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9812 return fold_build2_loc (loc, MINUS_EXPR, type,
9813 fold_convert_loc (loc, type, arg0),
9814 fold_convert_loc (loc, type, tem));
9817 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9818 to __complex__ ( x, y ). This is not the same for SNaNs or
9819 if signed zeros are involved. */
9820 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9821 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9822 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9824 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9825 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9826 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9827 bool arg0rz = false, arg0iz = false;
9828 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9829 || (arg0i && (arg0iz = real_zerop (arg0i))))
9831 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9832 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9833 if (arg0rz && arg1i && real_zerop (arg1i))
9835 tree rp = arg1r ? arg1r
9836 : build1 (REALPART_EXPR, rtype, arg1);
9837 tree ip = arg0i ? arg0i
9838 : build1 (IMAGPART_EXPR, rtype, arg0);
9839 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9841 else if (arg0iz && arg1r && real_zerop (arg1r))
9843 tree rp = arg0r ? arg0r
9844 : build1 (REALPART_EXPR, rtype, arg0);
9845 tree ip = arg1i ? arg1i
9846 : build1 (IMAGPART_EXPR, rtype, arg1);
9847 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9852 if (flag_unsafe_math_optimizations
9853 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9854 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9855 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9858 /* Convert x+x into x*2.0. */
9859 if (operand_equal_p (arg0, arg1, 0)
9860 && SCALAR_FLOAT_TYPE_P (type))
9861 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9862 build_real (type, dconst2));
9864 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9865 We associate floats only if the user has specified
9866 -fassociative-math. */
9867 if (flag_associative_math
9868 && TREE_CODE (arg1) == PLUS_EXPR
9869 && TREE_CODE (arg0) != MULT_EXPR)
9871 tree tree10 = TREE_OPERAND (arg1, 0);
9872 tree tree11 = TREE_OPERAND (arg1, 1);
9873 if (TREE_CODE (tree11) == MULT_EXPR
9874 && TREE_CODE (tree10) == MULT_EXPR)
9877 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9878 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9881 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9882 We associate floats only if the user has specified
9883 -fassociative-math. */
9884 if (flag_associative_math
9885 && TREE_CODE (arg0) == PLUS_EXPR
9886 && TREE_CODE (arg1) != MULT_EXPR)
9888 tree tree00 = TREE_OPERAND (arg0, 0);
9889 tree tree01 = TREE_OPERAND (arg0, 1);
9890 if (TREE_CODE (tree01) == MULT_EXPR
9891 && TREE_CODE (tree00) == MULT_EXPR)
9894 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9895 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9901 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9902 is a rotate of A by C1 bits. */
9903 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9904 is a rotate of A by B bits. */
9906 enum tree_code code0, code1;
9908 code0 = TREE_CODE (arg0);
9909 code1 = TREE_CODE (arg1);
9910 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9911 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9912 && operand_equal_p (TREE_OPERAND (arg0, 0),
9913 TREE_OPERAND (arg1, 0), 0)
9914 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9915 TYPE_UNSIGNED (rtype))
9916 /* Only create rotates in complete modes. Other cases are not
9917 expanded properly. */
9918 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9920 tree tree01, tree11;
9921 enum tree_code code01, code11;
9923 tree01 = TREE_OPERAND (arg0, 1);
9924 tree11 = TREE_OPERAND (arg1, 1);
9925 STRIP_NOPS (tree01);
9926 STRIP_NOPS (tree11);
9927 code01 = TREE_CODE (tree01);
9928 code11 = TREE_CODE (tree11);
9929 if (code01 == INTEGER_CST
9930 && code11 == INTEGER_CST
9931 && TREE_INT_CST_HIGH (tree01) == 0
9932 && TREE_INT_CST_HIGH (tree11) == 0
9933 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9934 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9936 tem = build2 (LROTATE_EXPR,
9937 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9938 TREE_OPERAND (arg0, 0),
9939 code0 == LSHIFT_EXPR
9941 SET_EXPR_LOCATION (tem, loc);
9942 return fold_convert_loc (loc, type, tem);
9944 else if (code11 == MINUS_EXPR)
9946 tree tree110, tree111;
9947 tree110 = TREE_OPERAND (tree11, 0);
9948 tree111 = TREE_OPERAND (tree11, 1);
9949 STRIP_NOPS (tree110);
9950 STRIP_NOPS (tree111);
9951 if (TREE_CODE (tree110) == INTEGER_CST
9952 && 0 == compare_tree_int (tree110,
9954 (TREE_TYPE (TREE_OPERAND
9956 && operand_equal_p (tree01, tree111, 0))
9958 fold_convert_loc (loc, type,
9959 build2 ((code0 == LSHIFT_EXPR
9962 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9963 TREE_OPERAND (arg0, 0), tree01));
9965 else if (code01 == MINUS_EXPR)
9967 tree tree010, tree011;
9968 tree010 = TREE_OPERAND (tree01, 0);
9969 tree011 = TREE_OPERAND (tree01, 1);
9970 STRIP_NOPS (tree010);
9971 STRIP_NOPS (tree011);
9972 if (TREE_CODE (tree010) == INTEGER_CST
9973 && 0 == compare_tree_int (tree010,
9975 (TREE_TYPE (TREE_OPERAND
9977 && operand_equal_p (tree11, tree011, 0))
9978 return fold_convert_loc
9980 build2 ((code0 != LSHIFT_EXPR
9983 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9984 TREE_OPERAND (arg0, 0), tree11));
9990 /* In most languages, can't associate operations on floats through
9991 parentheses. Rather than remember where the parentheses were, we
9992 don't associate floats at all, unless the user has specified
9994 And, we need to make sure type is not saturating. */
9996 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9997 && !TYPE_SATURATING (type))
9999 tree var0, con0, lit0, minus_lit0;
10000 tree var1, con1, lit1, minus_lit1;
10003 /* Split both trees into variables, constants, and literals. Then
10004 associate each group together, the constants with literals,
10005 then the result with variables. This increases the chances of
10006 literals being recombined later and of generating relocatable
10007 expressions for the sum of a constant and literal. */
10008 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10009 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10010 code == MINUS_EXPR);
10012 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10013 if (code == MINUS_EXPR)
10016 /* With undefined overflow we can only associate constants with one
10017 variable, and constants whose association doesn't overflow. */
10018 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10019 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10026 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10027 tmp0 = TREE_OPERAND (tmp0, 0);
10028 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10029 tmp1 = TREE_OPERAND (tmp1, 0);
10030 /* The only case we can still associate with two variables
10031 is if they are the same, modulo negation. */
10032 if (!operand_equal_p (tmp0, tmp1, 0))
10036 if (ok && lit0 && lit1)
10038 tree tmp0 = fold_convert (type, lit0);
10039 tree tmp1 = fold_convert (type, lit1);
10041 if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10042 && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10047 /* Only do something if we found more than two objects. Otherwise,
10048 nothing has changed and we risk infinite recursion. */
10050 && (2 < ((var0 != 0) + (var1 != 0)
10051 + (con0 != 0) + (con1 != 0)
10052 + (lit0 != 0) + (lit1 != 0)
10053 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10055 var0 = associate_trees (loc, var0, var1, code, type);
10056 con0 = associate_trees (loc, con0, con1, code, type);
10057 lit0 = associate_trees (loc, lit0, lit1, code, type);
10058 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10060 /* Preserve the MINUS_EXPR if the negative part of the literal is
10061 greater than the positive part. Otherwise, the multiplicative
10062 folding code (i.e extract_muldiv) may be fooled in case
10063 unsigned constants are subtracted, like in the following
10064 example: ((X*2 + 4) - 8U)/2. */
10065 if (minus_lit0 && lit0)
10067 if (TREE_CODE (lit0) == INTEGER_CST
10068 && TREE_CODE (minus_lit0) == INTEGER_CST
10069 && tree_int_cst_lt (lit0, minus_lit0))
10071 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10077 lit0 = associate_trees (loc, lit0, minus_lit0,
10086 fold_convert_loc (loc, type,
10087 associate_trees (loc, var0, minus_lit0,
10088 MINUS_EXPR, type));
10091 con0 = associate_trees (loc, con0, minus_lit0,
10094 fold_convert_loc (loc, type,
10095 associate_trees (loc, var0, con0,
10100 con0 = associate_trees (loc, con0, lit0, code, type);
10102 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10110 /* Pointer simplifications for subtraction, simple reassociations. */
10111 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10113 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10114 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10115 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10117 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10118 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10119 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10120 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10121 return fold_build2_loc (loc, PLUS_EXPR, type,
10122 fold_build2_loc (loc, MINUS_EXPR, type,
10124 fold_build2_loc (loc, MINUS_EXPR, type,
10127 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10128 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10130 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10131 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10132 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10133 fold_convert_loc (loc, type, arg1));
10135 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10138 /* A - (-B) -> A + B */
10139 if (TREE_CODE (arg1) == NEGATE_EXPR)
10140 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10141 fold_convert_loc (loc, type,
10142 TREE_OPERAND (arg1, 0)));
10143 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10144 if (TREE_CODE (arg0) == NEGATE_EXPR
10145 && (FLOAT_TYPE_P (type)
10146 || INTEGRAL_TYPE_P (type))
10147 && negate_expr_p (arg1)
10148 && reorder_operands_p (arg0, arg1))
10149 return fold_build2_loc (loc, MINUS_EXPR, type,
10150 fold_convert_loc (loc, type,
10151 negate_expr (arg1)),
10152 fold_convert_loc (loc, type,
10153 TREE_OPERAND (arg0, 0)));
10154 /* Convert -A - 1 to ~A. */
10155 if (INTEGRAL_TYPE_P (type)
10156 && TREE_CODE (arg0) == NEGATE_EXPR
10157 && integer_onep (arg1)
10158 && !TYPE_OVERFLOW_TRAPS (type))
10159 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10160 fold_convert_loc (loc, type,
10161 TREE_OPERAND (arg0, 0)));
10163 /* Convert -1 - A to ~A. */
10164 if (INTEGRAL_TYPE_P (type)
10165 && integer_all_onesp (arg0))
10166 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10169 /* X - (X / CST) * CST is X % CST. */
10170 if (INTEGRAL_TYPE_P (type)
10171 && TREE_CODE (arg1) == MULT_EXPR
10172 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10173 && operand_equal_p (arg0,
10174 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10175 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10176 TREE_OPERAND (arg1, 1), 0))
10178 fold_convert_loc (loc, type,
10179 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10180 arg0, TREE_OPERAND (arg1, 1)));
10182 if (! FLOAT_TYPE_P (type))
10184 if (integer_zerop (arg0))
10185 return negate_expr (fold_convert_loc (loc, type, arg1));
10186 if (integer_zerop (arg1))
10187 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10189 /* Fold A - (A & B) into ~B & A. */
10190 if (!TREE_SIDE_EFFECTS (arg0)
10191 && TREE_CODE (arg1) == BIT_AND_EXPR)
10193 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10195 tree arg10 = fold_convert_loc (loc, type,
10196 TREE_OPERAND (arg1, 0));
10197 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10198 fold_build1_loc (loc, BIT_NOT_EXPR,
10200 fold_convert_loc (loc, type, arg0));
10202 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10204 tree arg11 = fold_convert_loc (loc,
10205 type, TREE_OPERAND (arg1, 1));
10206 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10207 fold_build1_loc (loc, BIT_NOT_EXPR,
10209 fold_convert_loc (loc, type, arg0));
10213 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10214 any power of 2 minus 1. */
10215 if (TREE_CODE (arg0) == BIT_AND_EXPR
10216 && TREE_CODE (arg1) == BIT_AND_EXPR
10217 && operand_equal_p (TREE_OPERAND (arg0, 0),
10218 TREE_OPERAND (arg1, 0), 0))
10220 tree mask0 = TREE_OPERAND (arg0, 1);
10221 tree mask1 = TREE_OPERAND (arg1, 1);
10222 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10224 if (operand_equal_p (tem, mask1, 0))
10226 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10227 TREE_OPERAND (arg0, 0), mask1);
10228 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10233 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10234 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10235 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10237 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10238 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10239 (-ARG1 + ARG0) reduces to -ARG1. */
10240 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10241 return negate_expr (fold_convert_loc (loc, type, arg1));
10243 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10244 __complex__ ( x, -y ). This is not the same for SNaNs or if
10245 signed zeros are involved. */
10246 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10247 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10248 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10250 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10251 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10252 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10253 bool arg0rz = false, arg0iz = false;
10254 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10255 || (arg0i && (arg0iz = real_zerop (arg0i))))
10257 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10258 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10259 if (arg0rz && arg1i && real_zerop (arg1i))
10261 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10263 : build1 (REALPART_EXPR, rtype, arg1));
10264 tree ip = arg0i ? arg0i
10265 : build1 (IMAGPART_EXPR, rtype, arg0);
10266 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10268 else if (arg0iz && arg1r && real_zerop (arg1r))
10270 tree rp = arg0r ? arg0r
10271 : build1 (REALPART_EXPR, rtype, arg0);
10272 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10274 : build1 (IMAGPART_EXPR, rtype, arg1));
10275 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10280 /* Fold &x - &x. This can happen from &x.foo - &x.
10281 This is unsafe for certain floats even in non-IEEE formats.
10282 In IEEE, it is unsafe because it does wrong for NaNs.
10283 Also note that operand_equal_p is always false if an operand
10286 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10287 && operand_equal_p (arg0, arg1, 0))
10288 return build_zero_cst (type);
10290 /* A - B -> A + (-B) if B is easily negatable. */
10291 if (negate_expr_p (arg1)
10292 && ((FLOAT_TYPE_P (type)
10293 /* Avoid this transformation if B is a positive REAL_CST. */
10294 && (TREE_CODE (arg1) != REAL_CST
10295 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10296 || INTEGRAL_TYPE_P (type)))
10297 return fold_build2_loc (loc, PLUS_EXPR, type,
10298 fold_convert_loc (loc, type, arg0),
10299 fold_convert_loc (loc, type,
10300 negate_expr (arg1)));
10302 /* Try folding difference of addresses. */
10304 HOST_WIDE_INT diff;
10306 if ((TREE_CODE (arg0) == ADDR_EXPR
10307 || TREE_CODE (arg1) == ADDR_EXPR)
10308 && ptr_difference_const (arg0, arg1, &diff))
10309 return build_int_cst_type (type, diff);
10312 /* Fold &a[i] - &a[j] to i-j. */
10313 if (TREE_CODE (arg0) == ADDR_EXPR
10314 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10315 && TREE_CODE (arg1) == ADDR_EXPR
10316 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10318 tree aref0 = TREE_OPERAND (arg0, 0);
10319 tree aref1 = TREE_OPERAND (arg1, 0);
10320 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10321 TREE_OPERAND (aref1, 0), 0))
10323 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10324 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10325 tree esz = array_ref_element_size (aref0);
10326 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10327 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10328 fold_convert_loc (loc, type, esz));
10333 if (FLOAT_TYPE_P (type)
10334 && flag_unsafe_math_optimizations
10335 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10336 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10337 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10340 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10341 same or one. Make sure type is not saturating.
10342 fold_plusminus_mult_expr will re-associate. */
10343 if ((TREE_CODE (arg0) == MULT_EXPR
10344 || TREE_CODE (arg1) == MULT_EXPR)
10345 && !TYPE_SATURATING (type)
10346 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10348 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10356 /* (-A) * (-B) -> A * B */
10357 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10358 return fold_build2_loc (loc, MULT_EXPR, type,
10359 fold_convert_loc (loc, type,
10360 TREE_OPERAND (arg0, 0)),
10361 fold_convert_loc (loc, type,
10362 negate_expr (arg1)));
10363 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10364 return fold_build2_loc (loc, MULT_EXPR, type,
10365 fold_convert_loc (loc, type,
10366 negate_expr (arg0)),
10367 fold_convert_loc (loc, type,
10368 TREE_OPERAND (arg1, 0)));
10370 if (! FLOAT_TYPE_P (type))
10372 if (integer_zerop (arg1))
10373 return omit_one_operand_loc (loc, type, arg1, arg0);
10374 if (integer_onep (arg1))
10375 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10376 /* Transform x * -1 into -x. Make sure to do the negation
10377 on the original operand with conversions not stripped
10378 because we can only strip non-sign-changing conversions. */
10379 if (integer_all_onesp (arg1))
10380 return fold_convert_loc (loc, type, negate_expr (op0));
10381 /* Transform x * -C into -x * C if x is easily negatable. */
10382 if (TREE_CODE (arg1) == INTEGER_CST
10383 && tree_int_cst_sgn (arg1) == -1
10384 && negate_expr_p (arg0)
10385 && (tem = negate_expr (arg1)) != arg1
10386 && !TREE_OVERFLOW (tem))
10387 return fold_build2_loc (loc, MULT_EXPR, type,
10388 fold_convert_loc (loc, type,
10389 negate_expr (arg0)),
10392 /* (a * (1 << b)) is (a << b) */
10393 if (TREE_CODE (arg1) == LSHIFT_EXPR
10394 && integer_onep (TREE_OPERAND (arg1, 0)))
10395 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10396 TREE_OPERAND (arg1, 1));
10397 if (TREE_CODE (arg0) == LSHIFT_EXPR
10398 && integer_onep (TREE_OPERAND (arg0, 0)))
10399 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10400 TREE_OPERAND (arg0, 1));
10402 /* (A + A) * C -> A * 2 * C */
10403 if (TREE_CODE (arg0) == PLUS_EXPR
10404 && TREE_CODE (arg1) == INTEGER_CST
10405 && operand_equal_p (TREE_OPERAND (arg0, 0),
10406 TREE_OPERAND (arg0, 1), 0))
10407 return fold_build2_loc (loc, MULT_EXPR, type,
10408 omit_one_operand_loc (loc, type,
10409 TREE_OPERAND (arg0, 0),
10410 TREE_OPERAND (arg0, 1)),
10411 fold_build2_loc (loc, MULT_EXPR, type,
10412 build_int_cst (type, 2) , arg1));
10414 strict_overflow_p = false;
10415 if (TREE_CODE (arg1) == INTEGER_CST
10416 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10417 &strict_overflow_p)))
10419 if (strict_overflow_p)
10420 fold_overflow_warning (("assuming signed overflow does not "
10421 "occur when simplifying "
10423 WARN_STRICT_OVERFLOW_MISC);
10424 return fold_convert_loc (loc, type, tem);
10427 /* Optimize z * conj(z) for integer complex numbers. */
10428 if (TREE_CODE (arg0) == CONJ_EXPR
10429 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10430 return fold_mult_zconjz (loc, type, arg1);
10431 if (TREE_CODE (arg1) == CONJ_EXPR
10432 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10433 return fold_mult_zconjz (loc, type, arg0);
10437 /* Maybe fold x * 0 to 0. The expressions aren't the same
10438 when x is NaN, since x * 0 is also NaN. Nor are they the
10439 same in modes with signed zeros, since multiplying a
10440 negative value by 0 gives -0, not +0. */
10441 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10442 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10443 && real_zerop (arg1))
10444 return omit_one_operand_loc (loc, type, arg1, arg0);
10445 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10446 Likewise for complex arithmetic with signed zeros. */
10447 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10448 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10449 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10450 && real_onep (arg1))
10451 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10453 /* Transform x * -1.0 into -x. */
10454 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10455 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10456 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10457 && real_minus_onep (arg1))
10458 return fold_convert_loc (loc, type, negate_expr (arg0));
10460 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10461 the result for floating point types due to rounding so it is applied
10462 only if -fassociative-math was specify. */
10463 if (flag_associative_math
10464 && TREE_CODE (arg0) == RDIV_EXPR
10465 && TREE_CODE (arg1) == REAL_CST
10466 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10468 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10471 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10472 TREE_OPERAND (arg0, 1));
10475 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10476 if (operand_equal_p (arg0, arg1, 0))
10478 tree tem = fold_strip_sign_ops (arg0);
10479 if (tem != NULL_TREE)
10481 tem = fold_convert_loc (loc, type, tem);
10482 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10486 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10487 This is not the same for NaNs or if signed zeros are
10489 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10490 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10491 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10492 && TREE_CODE (arg1) == COMPLEX_CST
10493 && real_zerop (TREE_REALPART (arg1)))
10495 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10496 if (real_onep (TREE_IMAGPART (arg1)))
10498 fold_build2_loc (loc, COMPLEX_EXPR, type,
10499 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10501 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10502 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10504 fold_build2_loc (loc, COMPLEX_EXPR, type,
10505 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10506 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10510 /* Optimize z * conj(z) for floating point complex numbers.
10511 Guarded by flag_unsafe_math_optimizations as non-finite
10512 imaginary components don't produce scalar results. */
10513 if (flag_unsafe_math_optimizations
10514 && TREE_CODE (arg0) == CONJ_EXPR
10515 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10516 return fold_mult_zconjz (loc, type, arg1);
10517 if (flag_unsafe_math_optimizations
10518 && TREE_CODE (arg1) == CONJ_EXPR
10519 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10520 return fold_mult_zconjz (loc, type, arg0);
10522 if (flag_unsafe_math_optimizations)
10524 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10525 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10527 /* Optimizations of root(...)*root(...). */
10528 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10531 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10532 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10534 /* Optimize sqrt(x)*sqrt(x) as x. */
10535 if (BUILTIN_SQRT_P (fcode0)
10536 && operand_equal_p (arg00, arg10, 0)
10537 && ! HONOR_SNANS (TYPE_MODE (type)))
10540 /* Optimize root(x)*root(y) as root(x*y). */
10541 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10542 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10543 return build_call_expr_loc (loc, rootfn, 1, arg);
10546 /* Optimize expN(x)*expN(y) as expN(x+y). */
10547 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10549 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10550 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10551 CALL_EXPR_ARG (arg0, 0),
10552 CALL_EXPR_ARG (arg1, 0));
10553 return build_call_expr_loc (loc, expfn, 1, arg);
10556 /* Optimizations of pow(...)*pow(...). */
10557 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10558 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10559 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10561 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10562 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10563 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10564 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10566 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10567 if (operand_equal_p (arg01, arg11, 0))
10569 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10570 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10572 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10575 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10576 if (operand_equal_p (arg00, arg10, 0))
10578 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10579 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10581 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10585 /* Optimize tan(x)*cos(x) as sin(x). */
10586 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10587 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10588 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10589 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10590 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10591 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10592 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10593 CALL_EXPR_ARG (arg1, 0), 0))
10595 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10597 if (sinfn != NULL_TREE)
10598 return build_call_expr_loc (loc, sinfn, 1,
10599 CALL_EXPR_ARG (arg0, 0));
10602 /* Optimize x*pow(x,c) as pow(x,c+1). */
10603 if (fcode1 == BUILT_IN_POW
10604 || fcode1 == BUILT_IN_POWF
10605 || fcode1 == BUILT_IN_POWL)
10607 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10608 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10609 if (TREE_CODE (arg11) == REAL_CST
10610 && !TREE_OVERFLOW (arg11)
10611 && operand_equal_p (arg0, arg10, 0))
10613 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10617 c = TREE_REAL_CST (arg11);
10618 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10619 arg = build_real (type, c);
10620 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10624 /* Optimize pow(x,c)*x as pow(x,c+1). */
10625 if (fcode0 == BUILT_IN_POW
10626 || fcode0 == BUILT_IN_POWF
10627 || fcode0 == BUILT_IN_POWL)
10629 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10630 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10631 if (TREE_CODE (arg01) == REAL_CST
10632 && !TREE_OVERFLOW (arg01)
10633 && operand_equal_p (arg1, arg00, 0))
10635 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10639 c = TREE_REAL_CST (arg01);
10640 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10641 arg = build_real (type, c);
10642 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10646 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10647 if (optimize_function_for_speed_p (cfun)
10648 && operand_equal_p (arg0, arg1, 0))
10650 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10654 tree arg = build_real (type, dconst2);
10655 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10664 if (integer_all_onesp (arg1))
10665 return omit_one_operand_loc (loc, type, arg1, arg0);
10666 if (integer_zerop (arg1))
10667 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10668 if (operand_equal_p (arg0, arg1, 0))
10669 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10671 /* ~X | X is -1. */
10672 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10673 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10675 t1 = build_zero_cst (type);
10676 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10677 return omit_one_operand_loc (loc, type, t1, arg1);
10680 /* X | ~X is -1. */
10681 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10682 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10684 t1 = build_zero_cst (type);
10685 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10686 return omit_one_operand_loc (loc, type, t1, arg0);
10689 /* Canonicalize (X & C1) | C2. */
10690 if (TREE_CODE (arg0) == BIT_AND_EXPR
10691 && TREE_CODE (arg1) == INTEGER_CST
10692 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10694 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10695 int width = TYPE_PRECISION (type), w;
10696 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10697 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10698 hi2 = TREE_INT_CST_HIGH (arg1);
10699 lo2 = TREE_INT_CST_LOW (arg1);
10701 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10702 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10703 return omit_one_operand_loc (loc, type, arg1,
10704 TREE_OPERAND (arg0, 0));
10706 if (width > HOST_BITS_PER_WIDE_INT)
10708 mhi = (unsigned HOST_WIDE_INT) -1
10709 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10715 mlo = (unsigned HOST_WIDE_INT) -1
10716 >> (HOST_BITS_PER_WIDE_INT - width);
10719 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10720 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10721 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10722 TREE_OPERAND (arg0, 0), arg1);
10724 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10725 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10726 mode which allows further optimizations. */
10733 for (w = BITS_PER_UNIT;
10734 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10737 unsigned HOST_WIDE_INT mask
10738 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10739 if (((lo1 | lo2) & mask) == mask
10740 && (lo1 & ~mask) == 0 && hi1 == 0)
10747 if (hi3 != hi1 || lo3 != lo1)
10748 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10749 fold_build2_loc (loc, BIT_AND_EXPR, type,
10750 TREE_OPERAND (arg0, 0),
10751 build_int_cst_wide (type,
10756 /* (X & Y) | Y is (X, Y). */
10757 if (TREE_CODE (arg0) == BIT_AND_EXPR
10758 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10759 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10760 /* (X & Y) | X is (Y, X). */
10761 if (TREE_CODE (arg0) == BIT_AND_EXPR
10762 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10763 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10764 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10765 /* X | (X & Y) is (Y, X). */
10766 if (TREE_CODE (arg1) == BIT_AND_EXPR
10767 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10768 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10769 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10770 /* X | (Y & X) is (Y, X). */
10771 if (TREE_CODE (arg1) == BIT_AND_EXPR
10772 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10773 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10774 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10776 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10777 if (t1 != NULL_TREE)
10780 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10782 This results in more efficient code for machines without a NAND
10783 instruction. Combine will canonicalize to the first form
10784 which will allow use of NAND instructions provided by the
10785 backend if they exist. */
10786 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10787 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10790 fold_build1_loc (loc, BIT_NOT_EXPR, type,
10791 build2 (BIT_AND_EXPR, type,
10792 fold_convert_loc (loc, type,
10793 TREE_OPERAND (arg0, 0)),
10794 fold_convert_loc (loc, type,
10795 TREE_OPERAND (arg1, 0))));
10798 /* See if this can be simplified into a rotate first. If that
10799 is unsuccessful continue in the association code. */
10803 if (integer_zerop (arg1))
10804 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10805 if (integer_all_onesp (arg1))
10806 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10807 if (operand_equal_p (arg0, arg1, 0))
10808 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10810 /* ~X ^ X is -1. */
10811 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10812 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10814 t1 = build_zero_cst (type);
10815 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10816 return omit_one_operand_loc (loc, type, t1, arg1);
10819 /* X ^ ~X is -1. */
10820 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10821 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10823 t1 = build_zero_cst (type);
10824 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10825 return omit_one_operand_loc (loc, type, t1, arg0);
10828 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10829 with a constant, and the two constants have no bits in common,
10830 we should treat this as a BIT_IOR_EXPR since this may produce more
10831 simplifications. */
10832 if (TREE_CODE (arg0) == BIT_AND_EXPR
10833 && TREE_CODE (arg1) == BIT_AND_EXPR
10834 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10835 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10836 && integer_zerop (const_binop (BIT_AND_EXPR,
10837 TREE_OPERAND (arg0, 1),
10838 TREE_OPERAND (arg1, 1))))
10840 code = BIT_IOR_EXPR;
10844 /* (X | Y) ^ X -> Y & ~ X*/
10845 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10846 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10848 tree t2 = TREE_OPERAND (arg0, 1);
10849 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10851 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10852 fold_convert_loc (loc, type, t2),
10853 fold_convert_loc (loc, type, t1));
10857 /* (Y | X) ^ X -> Y & ~ X*/
10858 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10859 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10861 tree t2 = TREE_OPERAND (arg0, 0);
10862 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10864 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10865 fold_convert_loc (loc, type, t2),
10866 fold_convert_loc (loc, type, t1));
10870 /* X ^ (X | Y) -> Y & ~ X*/
10871 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10872 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10874 tree t2 = TREE_OPERAND (arg1, 1);
10875 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10877 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10878 fold_convert_loc (loc, type, t2),
10879 fold_convert_loc (loc, type, t1));
10883 /* X ^ (Y | X) -> Y & ~ X*/
10884 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10885 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10887 tree t2 = TREE_OPERAND (arg1, 0);
10888 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10890 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10891 fold_convert_loc (loc, type, t2),
10892 fold_convert_loc (loc, type, t1));
10896 /* Convert ~X ^ ~Y to X ^ Y. */
10897 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10898 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10899 return fold_build2_loc (loc, code, type,
10900 fold_convert_loc (loc, type,
10901 TREE_OPERAND (arg0, 0)),
10902 fold_convert_loc (loc, type,
10903 TREE_OPERAND (arg1, 0)));
10905 /* Convert ~X ^ C to X ^ ~C. */
10906 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10907 && TREE_CODE (arg1) == INTEGER_CST)
10908 return fold_build2_loc (loc, code, type,
10909 fold_convert_loc (loc, type,
10910 TREE_OPERAND (arg0, 0)),
10911 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10913 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10914 if (TREE_CODE (arg0) == BIT_AND_EXPR
10915 && integer_onep (TREE_OPERAND (arg0, 1))
10916 && integer_onep (arg1))
10917 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10918 build_int_cst (TREE_TYPE (arg0), 0));
10920 /* Fold (X & Y) ^ Y as ~X & Y. */
10921 if (TREE_CODE (arg0) == BIT_AND_EXPR
10922 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10924 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10925 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10926 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10927 fold_convert_loc (loc, type, arg1));
10929 /* Fold (X & Y) ^ X as ~Y & X. */
10930 if (TREE_CODE (arg0) == BIT_AND_EXPR
10931 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10932 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10934 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10935 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10936 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10937 fold_convert_loc (loc, type, arg1));
10939 /* Fold X ^ (X & Y) as X & ~Y. */
10940 if (TREE_CODE (arg1) == BIT_AND_EXPR
10941 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10943 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10944 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10945 fold_convert_loc (loc, type, arg0),
10946 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10948 /* Fold X ^ (Y & X) as ~Y & X. */
10949 if (TREE_CODE (arg1) == BIT_AND_EXPR
10950 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10951 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10953 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10954 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10955 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10956 fold_convert_loc (loc, type, arg0));
10959 /* See if this can be simplified into a rotate first. If that
10960 is unsuccessful continue in the association code. */
10964 if (integer_all_onesp (arg1))
10965 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10966 if (integer_zerop (arg1))
10967 return omit_one_operand_loc (loc, type, arg1, arg0);
10968 if (operand_equal_p (arg0, arg1, 0))
10969 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10971 /* ~X & X is always zero. */
10972 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10973 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10974 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10976 /* X & ~X is always zero. */
10977 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10978 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10979 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10981 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10982 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10983 && TREE_CODE (arg1) == INTEGER_CST
10984 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10986 tree tmp1 = fold_convert_loc (loc, type, arg1);
10987 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10988 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10989 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10990 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10992 fold_convert_loc (loc, type,
10993 fold_build2_loc (loc, BIT_IOR_EXPR,
10994 type, tmp2, tmp3));
10997 /* (X | Y) & Y is (X, Y). */
10998 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10999 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11000 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11001 /* (X | Y) & X is (Y, X). */
11002 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11003 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11004 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11005 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11006 /* X & (X | Y) is (Y, X). */
11007 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11008 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11009 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11010 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11011 /* X & (Y | X) is (Y, X). */
11012 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11013 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11014 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11015 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11017 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11018 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11019 && integer_onep (TREE_OPERAND (arg0, 1))
11020 && integer_onep (arg1))
11022 tem = TREE_OPERAND (arg0, 0);
11023 return fold_build2_loc (loc, EQ_EXPR, type,
11024 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11025 build_int_cst (TREE_TYPE (tem), 1)),
11026 build_int_cst (TREE_TYPE (tem), 0));
11028 /* Fold ~X & 1 as (X & 1) == 0. */
11029 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11030 && integer_onep (arg1))
11032 tem = TREE_OPERAND (arg0, 0);
11033 return fold_build2_loc (loc, EQ_EXPR, type,
11034 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11035 build_int_cst (TREE_TYPE (tem), 1)),
11036 build_int_cst (TREE_TYPE (tem), 0));
11039 /* Fold (X ^ Y) & Y as ~X & Y. */
11040 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11041 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11043 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11044 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11045 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11046 fold_convert_loc (loc, type, arg1));
11048 /* Fold (X ^ Y) & X as ~Y & X. */
11049 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11050 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11051 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11053 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11054 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11055 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11056 fold_convert_loc (loc, type, arg1));
11058 /* Fold X & (X ^ Y) as X & ~Y. */
11059 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11060 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11062 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11063 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11064 fold_convert_loc (loc, type, arg0),
11065 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11067 /* Fold X & (Y ^ X) as ~Y & X. */
11068 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11069 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11070 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11072 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11073 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11074 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11075 fold_convert_loc (loc, type, arg0));
11078 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11079 ((A & N) + B) & M -> (A + B) & M
11080 Similarly if (N & M) == 0,
11081 ((A | N) + B) & M -> (A + B) & M
11082 and for - instead of + (or unary - instead of +)
11083 and/or ^ instead of |.
11084 If B is constant and (B & M) == 0, fold into A & M. */
11085 if (host_integerp (arg1, 1))
11087 unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11088 if (~cst1 && (cst1 & (cst1 + 1)) == 0
11089 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11090 && (TREE_CODE (arg0) == PLUS_EXPR
11091 || TREE_CODE (arg0) == MINUS_EXPR
11092 || TREE_CODE (arg0) == NEGATE_EXPR)
11093 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11094 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11098 unsigned HOST_WIDE_INT cst0;
11100 /* Now we know that arg0 is (C + D) or (C - D) or
11101 -C and arg1 (M) is == (1LL << cst) - 1.
11102 Store C into PMOP[0] and D into PMOP[1]. */
11103 pmop[0] = TREE_OPERAND (arg0, 0);
11105 if (TREE_CODE (arg0) != NEGATE_EXPR)
11107 pmop[1] = TREE_OPERAND (arg0, 1);
11111 if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11112 || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11116 for (; which >= 0; which--)
11117 switch (TREE_CODE (pmop[which]))
11122 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11125 /* tree_low_cst not used, because we don't care about
11127 cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11129 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11134 else if (cst0 != 0)
11136 /* If C or D is of the form (A & N) where
11137 (N & M) == M, or of the form (A | N) or
11138 (A ^ N) where (N & M) == 0, replace it with A. */
11139 pmop[which] = TREE_OPERAND (pmop[which], 0);
11142 /* If C or D is a N where (N & M) == 0, it can be
11143 omitted (assumed 0). */
11144 if ((TREE_CODE (arg0) == PLUS_EXPR
11145 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11146 && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11147 pmop[which] = NULL;
11153 /* Only build anything new if we optimized one or both arguments
11155 if (pmop[0] != TREE_OPERAND (arg0, 0)
11156 || (TREE_CODE (arg0) != NEGATE_EXPR
11157 && pmop[1] != TREE_OPERAND (arg0, 1)))
11159 tree utype = TREE_TYPE (arg0);
11160 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11162 /* Perform the operations in a type that has defined
11163 overflow behavior. */
11164 utype = unsigned_type_for (TREE_TYPE (arg0));
11165 if (pmop[0] != NULL)
11166 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11167 if (pmop[1] != NULL)
11168 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11171 if (TREE_CODE (arg0) == NEGATE_EXPR)
11172 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11173 else if (TREE_CODE (arg0) == PLUS_EXPR)
11175 if (pmop[0] != NULL && pmop[1] != NULL)
11176 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11178 else if (pmop[0] != NULL)
11180 else if (pmop[1] != NULL)
11183 return build_int_cst (type, 0);
11185 else if (pmop[0] == NULL)
11186 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11188 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11190 /* TEM is now the new binary +, - or unary - replacement. */
11191 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11192 fold_convert_loc (loc, utype, arg1));
11193 return fold_convert_loc (loc, type, tem);
11198 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11199 if (t1 != NULL_TREE)
11201 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11202 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11203 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11206 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11208 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11209 && (~TREE_INT_CST_LOW (arg1)
11210 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11212 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11215 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11217 This results in more efficient code for machines without a NOR
11218 instruction. Combine will canonicalize to the first form
11219 which will allow use of NOR instructions provided by the
11220 backend if they exist. */
11221 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11222 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11224 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11225 build2 (BIT_IOR_EXPR, type,
11226 fold_convert_loc (loc, type,
11227 TREE_OPERAND (arg0, 0)),
11228 fold_convert_loc (loc, type,
11229 TREE_OPERAND (arg1, 0))));
11232 /* If arg0 is derived from the address of an object or function, we may
11233 be able to fold this expression using the object or function's
11235 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11237 unsigned HOST_WIDE_INT modulus, residue;
11238 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11240 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11241 integer_onep (arg1));
11243 /* This works because modulus is a power of 2. If this weren't the
11244 case, we'd have to replace it by its greatest power-of-2
11245 divisor: modulus & -modulus. */
11247 return build_int_cst (type, residue & low);
11250 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11251 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11252 if the new mask might be further optimized. */
11253 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11254 || TREE_CODE (arg0) == RSHIFT_EXPR)
11255 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11256 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11257 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11258 < TYPE_PRECISION (TREE_TYPE (arg0))
11259 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11260 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11262 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11263 unsigned HOST_WIDE_INT mask
11264 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11265 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11266 tree shift_type = TREE_TYPE (arg0);
11268 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11269 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11270 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11271 && TYPE_PRECISION (TREE_TYPE (arg0))
11272 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11274 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11275 tree arg00 = TREE_OPERAND (arg0, 0);
11276 /* See if more bits can be proven as zero because of
11278 if (TREE_CODE (arg00) == NOP_EXPR
11279 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11281 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11282 if (TYPE_PRECISION (inner_type)
11283 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11284 && TYPE_PRECISION (inner_type) < prec)
11286 prec = TYPE_PRECISION (inner_type);
11287 /* See if we can shorten the right shift. */
11289 shift_type = inner_type;
11292 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11293 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11294 zerobits <<= prec - shiftc;
11295 /* For arithmetic shift if sign bit could be set, zerobits
11296 can contain actually sign bits, so no transformation is
11297 possible, unless MASK masks them all away. In that
11298 case the shift needs to be converted into logical shift. */
11299 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11300 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11302 if ((mask & zerobits) == 0)
11303 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11309 /* ((X << 16) & 0xff00) is (X, 0). */
11310 if ((mask & zerobits) == mask)
11311 return omit_one_operand_loc (loc, type,
11312 build_int_cst (type, 0), arg0);
11314 newmask = mask | zerobits;
11315 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11319 /* Only do the transformation if NEWMASK is some integer
11321 for (prec = BITS_PER_UNIT;
11322 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11323 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11325 if (prec < HOST_BITS_PER_WIDE_INT
11326 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11330 if (shift_type != TREE_TYPE (arg0))
11332 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11333 fold_convert_loc (loc, shift_type,
11334 TREE_OPERAND (arg0, 0)),
11335 TREE_OPERAND (arg0, 1));
11336 tem = fold_convert_loc (loc, type, tem);
11340 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11341 if (!tree_int_cst_equal (newmaskt, arg1))
11342 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11350 /* Don't touch a floating-point divide by zero unless the mode
11351 of the constant can represent infinity. */
11352 if (TREE_CODE (arg1) == REAL_CST
11353 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11354 && real_zerop (arg1))
11357 /* Optimize A / A to 1.0 if we don't care about
11358 NaNs or Infinities. Skip the transformation
11359 for non-real operands. */
11360 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11361 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11362 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11363 && operand_equal_p (arg0, arg1, 0))
11365 tree r = build_real (TREE_TYPE (arg0), dconst1);
11367 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11370 /* The complex version of the above A / A optimization. */
11371 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11372 && operand_equal_p (arg0, arg1, 0))
11374 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11375 if (! HONOR_NANS (TYPE_MODE (elem_type))
11376 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11378 tree r = build_real (elem_type, dconst1);
11379 /* omit_two_operands will call fold_convert for us. */
11380 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11384 /* (-A) / (-B) -> A / B */
11385 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11386 return fold_build2_loc (loc, RDIV_EXPR, type,
11387 TREE_OPERAND (arg0, 0),
11388 negate_expr (arg1));
11389 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11390 return fold_build2_loc (loc, RDIV_EXPR, type,
11391 negate_expr (arg0),
11392 TREE_OPERAND (arg1, 0));
11394 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11395 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11396 && real_onep (arg1))
11397 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11399 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11400 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11401 && real_minus_onep (arg1))
11402 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11403 negate_expr (arg0)));
11405 /* If ARG1 is a constant, we can convert this to a multiply by the
11406 reciprocal. This does not have the same rounding properties,
11407 so only do this if -freciprocal-math. We can actually
11408 always safely do it if ARG1 is a power of two, but it's hard to
11409 tell if it is or not in a portable manner. */
11410 if (TREE_CODE (arg1) == REAL_CST)
11412 if (flag_reciprocal_math
11413 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11415 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11416 /* Find the reciprocal if optimizing and the result is exact. */
11420 r = TREE_REAL_CST (arg1);
11421 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11423 tem = build_real (type, r);
11424 return fold_build2_loc (loc, MULT_EXPR, type,
11425 fold_convert_loc (loc, type, arg0), tem);
11429 /* Convert A/B/C to A/(B*C). */
11430 if (flag_reciprocal_math
11431 && TREE_CODE (arg0) == RDIV_EXPR)
11432 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11433 fold_build2_loc (loc, MULT_EXPR, type,
11434 TREE_OPERAND (arg0, 1), arg1));
11436 /* Convert A/(B/C) to (A/B)*C. */
11437 if (flag_reciprocal_math
11438 && TREE_CODE (arg1) == RDIV_EXPR)
11439 return fold_build2_loc (loc, MULT_EXPR, type,
11440 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11441 TREE_OPERAND (arg1, 0)),
11442 TREE_OPERAND (arg1, 1));
11444 /* Convert C1/(X*C2) into (C1/C2)/X. */
11445 if (flag_reciprocal_math
11446 && TREE_CODE (arg1) == MULT_EXPR
11447 && TREE_CODE (arg0) == REAL_CST
11448 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11450 tree tem = const_binop (RDIV_EXPR, arg0,
11451 TREE_OPERAND (arg1, 1));
11453 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11454 TREE_OPERAND (arg1, 0));
11457 if (flag_unsafe_math_optimizations)
11459 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11460 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11462 /* Optimize sin(x)/cos(x) as tan(x). */
11463 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11464 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11465 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11466 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11467 CALL_EXPR_ARG (arg1, 0), 0))
11469 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11471 if (tanfn != NULL_TREE)
11472 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11475 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11476 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11477 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11478 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11479 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11480 CALL_EXPR_ARG (arg1, 0), 0))
11482 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11484 if (tanfn != NULL_TREE)
11486 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11487 CALL_EXPR_ARG (arg0, 0));
11488 return fold_build2_loc (loc, RDIV_EXPR, type,
11489 build_real (type, dconst1), tmp);
11493 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11494 NaNs or Infinities. */
11495 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11496 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11497 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11499 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11500 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11502 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11503 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11504 && operand_equal_p (arg00, arg01, 0))
11506 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11508 if (cosfn != NULL_TREE)
11509 return build_call_expr_loc (loc, cosfn, 1, arg00);
11513 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11514 NaNs or Infinities. */
11515 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11516 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11517 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11519 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11520 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11522 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11523 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11524 && operand_equal_p (arg00, arg01, 0))
11526 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11528 if (cosfn != NULL_TREE)
11530 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11531 return fold_build2_loc (loc, RDIV_EXPR, type,
11532 build_real (type, dconst1),
11538 /* Optimize pow(x,c)/x as pow(x,c-1). */
11539 if (fcode0 == BUILT_IN_POW
11540 || fcode0 == BUILT_IN_POWF
11541 || fcode0 == BUILT_IN_POWL)
11543 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11544 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11545 if (TREE_CODE (arg01) == REAL_CST
11546 && !TREE_OVERFLOW (arg01)
11547 && operand_equal_p (arg1, arg00, 0))
11549 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11553 c = TREE_REAL_CST (arg01);
11554 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11555 arg = build_real (type, c);
11556 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11560 /* Optimize a/root(b/c) into a*root(c/b). */
11561 if (BUILTIN_ROOT_P (fcode1))
11563 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11565 if (TREE_CODE (rootarg) == RDIV_EXPR)
11567 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11568 tree b = TREE_OPERAND (rootarg, 0);
11569 tree c = TREE_OPERAND (rootarg, 1);
11571 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11573 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11574 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11578 /* Optimize x/expN(y) into x*expN(-y). */
11579 if (BUILTIN_EXPONENT_P (fcode1))
11581 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11582 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11583 arg1 = build_call_expr_loc (loc,
11585 fold_convert_loc (loc, type, arg));
11586 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11589 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11590 if (fcode1 == BUILT_IN_POW
11591 || fcode1 == BUILT_IN_POWF
11592 || fcode1 == BUILT_IN_POWL)
11594 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11595 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11596 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11597 tree neg11 = fold_convert_loc (loc, type,
11598 negate_expr (arg11));
11599 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11600 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11605 case TRUNC_DIV_EXPR:
11606 /* Optimize (X & (-A)) / A where A is a power of 2,
11608 if (TREE_CODE (arg0) == BIT_AND_EXPR
11609 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11610 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11612 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11613 arg1, TREE_OPERAND (arg0, 1));
11614 if (sum && integer_zerop (sum)) {
11615 unsigned long pow2;
11617 if (TREE_INT_CST_LOW (arg1))
11618 pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11620 pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11621 + HOST_BITS_PER_WIDE_INT;
11623 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11624 TREE_OPERAND (arg0, 0),
11625 build_int_cst (NULL_TREE, pow2));
11631 case FLOOR_DIV_EXPR:
11632 /* Simplify A / (B << N) where A and B are positive and B is
11633 a power of 2, to A >> (N + log2(B)). */
11634 strict_overflow_p = false;
11635 if (TREE_CODE (arg1) == LSHIFT_EXPR
11636 && (TYPE_UNSIGNED (type)
11637 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11639 tree sval = TREE_OPERAND (arg1, 0);
11640 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11642 tree sh_cnt = TREE_OPERAND (arg1, 1);
11643 unsigned long pow2;
11645 if (TREE_INT_CST_LOW (sval))
11646 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11648 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11649 + HOST_BITS_PER_WIDE_INT;
11651 if (strict_overflow_p)
11652 fold_overflow_warning (("assuming signed overflow does not "
11653 "occur when simplifying A / (B << N)"),
11654 WARN_STRICT_OVERFLOW_MISC);
11656 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11657 sh_cnt, build_int_cst (NULL_TREE, pow2));
11658 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11659 fold_convert_loc (loc, type, arg0), sh_cnt);
11663 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11664 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11665 if (INTEGRAL_TYPE_P (type)
11666 && TYPE_UNSIGNED (type)
11667 && code == FLOOR_DIV_EXPR)
11668 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11672 case ROUND_DIV_EXPR:
11673 case CEIL_DIV_EXPR:
11674 case EXACT_DIV_EXPR:
11675 if (integer_onep (arg1))
11676 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11677 if (integer_zerop (arg1))
11679 /* X / -1 is -X. */
11680 if (!TYPE_UNSIGNED (type)
11681 && TREE_CODE (arg1) == INTEGER_CST
11682 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11683 && TREE_INT_CST_HIGH (arg1) == -1)
11684 return fold_convert_loc (loc, type, negate_expr (arg0));
11686 /* Convert -A / -B to A / B when the type is signed and overflow is
11688 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11689 && TREE_CODE (arg0) == NEGATE_EXPR
11690 && negate_expr_p (arg1))
11692 if (INTEGRAL_TYPE_P (type))
11693 fold_overflow_warning (("assuming signed overflow does not occur "
11694 "when distributing negation across "
11696 WARN_STRICT_OVERFLOW_MISC);
11697 return fold_build2_loc (loc, code, type,
11698 fold_convert_loc (loc, type,
11699 TREE_OPERAND (arg0, 0)),
11700 fold_convert_loc (loc, type,
11701 negate_expr (arg1)));
11703 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11704 && TREE_CODE (arg1) == NEGATE_EXPR
11705 && negate_expr_p (arg0))
11707 if (INTEGRAL_TYPE_P (type))
11708 fold_overflow_warning (("assuming signed overflow does not occur "
11709 "when distributing negation across "
11711 WARN_STRICT_OVERFLOW_MISC);
11712 return fold_build2_loc (loc, code, type,
11713 fold_convert_loc (loc, type,
11714 negate_expr (arg0)),
11715 fold_convert_loc (loc, type,
11716 TREE_OPERAND (arg1, 0)));
11719 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11720 operation, EXACT_DIV_EXPR.
11722 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11723 At one time others generated faster code, it's not clear if they do
11724 after the last round to changes to the DIV code in expmed.c. */
11725 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11726 && multiple_of_p (type, arg0, arg1))
11727 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11729 strict_overflow_p = false;
11730 if (TREE_CODE (arg1) == INTEGER_CST
11731 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11732 &strict_overflow_p)))
11734 if (strict_overflow_p)
11735 fold_overflow_warning (("assuming signed overflow does not occur "
11736 "when simplifying division"),
11737 WARN_STRICT_OVERFLOW_MISC);
11738 return fold_convert_loc (loc, type, tem);
11743 case CEIL_MOD_EXPR:
11744 case FLOOR_MOD_EXPR:
11745 case ROUND_MOD_EXPR:
11746 case TRUNC_MOD_EXPR:
11747 /* X % 1 is always zero, but be sure to preserve any side
11749 if (integer_onep (arg1))
11750 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11752 /* X % 0, return X % 0 unchanged so that we can get the
11753 proper warnings and errors. */
11754 if (integer_zerop (arg1))
11757 /* 0 % X is always zero, but be sure to preserve any side
11758 effects in X. Place this after checking for X == 0. */
11759 if (integer_zerop (arg0))
11760 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11762 /* X % -1 is zero. */
11763 if (!TYPE_UNSIGNED (type)
11764 && TREE_CODE (arg1) == INTEGER_CST
11765 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11766 && TREE_INT_CST_HIGH (arg1) == -1)
11767 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11769 /* X % -C is the same as X % C. */
11770 if (code == TRUNC_MOD_EXPR
11771 && !TYPE_UNSIGNED (type)
11772 && TREE_CODE (arg1) == INTEGER_CST
11773 && !TREE_OVERFLOW (arg1)
11774 && TREE_INT_CST_HIGH (arg1) < 0
11775 && !TYPE_OVERFLOW_TRAPS (type)
11776 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11777 && !sign_bit_p (arg1, arg1))
11778 return fold_build2_loc (loc, code, type,
11779 fold_convert_loc (loc, type, arg0),
11780 fold_convert_loc (loc, type,
11781 negate_expr (arg1)));
11783 /* X % -Y is the same as X % Y. */
11784 if (code == TRUNC_MOD_EXPR
11785 && !TYPE_UNSIGNED (type)
11786 && TREE_CODE (arg1) == NEGATE_EXPR
11787 && !TYPE_OVERFLOW_TRAPS (type))
11788 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11789 fold_convert_loc (loc, type,
11790 TREE_OPERAND (arg1, 0)));
11792 strict_overflow_p = false;
11793 if (TREE_CODE (arg1) == INTEGER_CST
11794 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11795 &strict_overflow_p)))
11797 if (strict_overflow_p)
11798 fold_overflow_warning (("assuming signed overflow does not occur "
11799 "when simplifying modulus"),
11800 WARN_STRICT_OVERFLOW_MISC);
11801 return fold_convert_loc (loc, type, tem);
11804 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11805 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11806 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11807 && (TYPE_UNSIGNED (type)
11808 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11811 /* Also optimize A % (C << N) where C is a power of 2,
11812 to A & ((C << N) - 1). */
11813 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11814 c = TREE_OPERAND (arg1, 0);
11816 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11819 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11820 build_int_cst (TREE_TYPE (arg1), 1));
11821 if (strict_overflow_p)
11822 fold_overflow_warning (("assuming signed overflow does not "
11823 "occur when simplifying "
11824 "X % (power of two)"),
11825 WARN_STRICT_OVERFLOW_MISC);
11826 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11827 fold_convert_loc (loc, type, arg0),
11828 fold_convert_loc (loc, type, mask));
11836 if (integer_all_onesp (arg0))
11837 return omit_one_operand_loc (loc, type, arg0, arg1);
11841 /* Optimize -1 >> x for arithmetic right shifts. */
11842 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11843 && tree_expr_nonnegative_p (arg1))
11844 return omit_one_operand_loc (loc, type, arg0, arg1);
11845 /* ... fall through ... */
11849 if (integer_zerop (arg1))
11850 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11851 if (integer_zerop (arg0))
11852 return omit_one_operand_loc (loc, type, arg0, arg1);
11854 /* Since negative shift count is not well-defined,
11855 don't try to compute it in the compiler. */
11856 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11859 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11860 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11861 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11862 && host_integerp (TREE_OPERAND (arg0, 1), false)
11863 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11865 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11866 + TREE_INT_CST_LOW (arg1));
11868 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11869 being well defined. */
11870 if (low >= TYPE_PRECISION (type))
11872 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11873 low = low % TYPE_PRECISION (type);
11874 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11875 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11876 TREE_OPERAND (arg0, 0));
11878 low = TYPE_PRECISION (type) - 1;
11881 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11882 build_int_cst (type, low));
11885 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11886 into x & ((unsigned)-1 >> c) for unsigned types. */
11887 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11888 || (TYPE_UNSIGNED (type)
11889 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11890 && host_integerp (arg1, false)
11891 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11892 && host_integerp (TREE_OPERAND (arg0, 1), false)
11893 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11895 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11896 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11902 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11904 lshift = build_int_cst (type, -1);
11905 lshift = int_const_binop (code, lshift, arg1, 0);
11907 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11911 /* Rewrite an LROTATE_EXPR by a constant into an
11912 RROTATE_EXPR by a new constant. */
11913 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11915 tree tem = build_int_cst (TREE_TYPE (arg1),
11916 TYPE_PRECISION (type));
11917 tem = const_binop (MINUS_EXPR, tem, arg1);
11918 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11921 /* If we have a rotate of a bit operation with the rotate count and
11922 the second operand of the bit operation both constant,
11923 permute the two operations. */
11924 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11925 && (TREE_CODE (arg0) == BIT_AND_EXPR
11926 || TREE_CODE (arg0) == BIT_IOR_EXPR
11927 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11928 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11929 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11930 fold_build2_loc (loc, code, type,
11931 TREE_OPERAND (arg0, 0), arg1),
11932 fold_build2_loc (loc, code, type,
11933 TREE_OPERAND (arg0, 1), arg1));
11935 /* Two consecutive rotates adding up to the precision of the
11936 type can be ignored. */
11937 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11938 && TREE_CODE (arg0) == RROTATE_EXPR
11939 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11940 && TREE_INT_CST_HIGH (arg1) == 0
11941 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11942 && ((TREE_INT_CST_LOW (arg1)
11943 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11944 == (unsigned int) TYPE_PRECISION (type)))
11945 return TREE_OPERAND (arg0, 0);
11947 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11948 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11949 if the latter can be further optimized. */
11950 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11951 && TREE_CODE (arg0) == BIT_AND_EXPR
11952 && TREE_CODE (arg1) == INTEGER_CST
11953 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11955 tree mask = fold_build2_loc (loc, code, type,
11956 fold_convert_loc (loc, type,
11957 TREE_OPERAND (arg0, 1)),
11959 tree shift = fold_build2_loc (loc, code, type,
11960 fold_convert_loc (loc, type,
11961 TREE_OPERAND (arg0, 0)),
11963 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11971 if (operand_equal_p (arg0, arg1, 0))
11972 return omit_one_operand_loc (loc, type, arg0, arg1);
11973 if (INTEGRAL_TYPE_P (type)
11974 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11975 return omit_one_operand_loc (loc, type, arg1, arg0);
11976 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11982 if (operand_equal_p (arg0, arg1, 0))
11983 return omit_one_operand_loc (loc, type, arg0, arg1);
11984 if (INTEGRAL_TYPE_P (type)
11985 && TYPE_MAX_VALUE (type)
11986 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11987 return omit_one_operand_loc (loc, type, arg1, arg0);
11988 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11993 case TRUTH_ANDIF_EXPR:
11994 /* Note that the operands of this must be ints
11995 and their values must be 0 or 1.
11996 ("true" is a fixed value perhaps depending on the language.) */
11997 /* If first arg is constant zero, return it. */
11998 if (integer_zerop (arg0))
11999 return fold_convert_loc (loc, type, arg0);
12000 case TRUTH_AND_EXPR:
12001 /* If either arg is constant true, drop it. */
12002 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12003 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12004 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12005 /* Preserve sequence points. */
12006 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12007 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12008 /* If second arg is constant zero, result is zero, but first arg
12009 must be evaluated. */
12010 if (integer_zerop (arg1))
12011 return omit_one_operand_loc (loc, type, arg1, arg0);
12012 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12013 case will be handled here. */
12014 if (integer_zerop (arg0))
12015 return omit_one_operand_loc (loc, type, arg0, arg1);
12017 /* !X && X is always false. */
12018 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12019 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12020 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12021 /* X && !X is always false. */
12022 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12023 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12024 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12026 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12027 means A >= Y && A != MAX, but in this case we know that
12030 if (!TREE_SIDE_EFFECTS (arg0)
12031 && !TREE_SIDE_EFFECTS (arg1))
12033 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12034 if (tem && !operand_equal_p (tem, arg0, 0))
12035 return fold_build2_loc (loc, code, type, tem, arg1);
12037 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12038 if (tem && !operand_equal_p (tem, arg1, 0))
12039 return fold_build2_loc (loc, code, type, arg0, tem);
12043 /* We only do these simplifications if we are optimizing. */
12047 /* Check for things like (A || B) && (A || C). We can convert this
12048 to A || (B && C). Note that either operator can be any of the four
12049 truth and/or operations and the transformation will still be
12050 valid. Also note that we only care about order for the
12051 ANDIF and ORIF operators. If B contains side effects, this
12052 might change the truth-value of A. */
12053 if (TREE_CODE (arg0) == TREE_CODE (arg1)
12054 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12055 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12056 || TREE_CODE (arg0) == TRUTH_AND_EXPR
12057 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12058 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12060 tree a00 = TREE_OPERAND (arg0, 0);
12061 tree a01 = TREE_OPERAND (arg0, 1);
12062 tree a10 = TREE_OPERAND (arg1, 0);
12063 tree a11 = TREE_OPERAND (arg1, 1);
12064 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12065 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12066 && (code == TRUTH_AND_EXPR
12067 || code == TRUTH_OR_EXPR));
12069 if (operand_equal_p (a00, a10, 0))
12070 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12071 fold_build2_loc (loc, code, type, a01, a11));
12072 else if (commutative && operand_equal_p (a00, a11, 0))
12073 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12074 fold_build2_loc (loc, code, type, a01, a10));
12075 else if (commutative && operand_equal_p (a01, a10, 0))
12076 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12077 fold_build2_loc (loc, code, type, a00, a11));
12079 /* This case if tricky because we must either have commutative
12080 operators or else A10 must not have side-effects. */
12082 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12083 && operand_equal_p (a01, a11, 0))
12084 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12085 fold_build2_loc (loc, code, type, a00, a10),
12089 /* See if we can build a range comparison. */
12090 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12093 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12094 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12096 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12098 return fold_build2_loc (loc, code, type, tem, arg1);
12101 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12102 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12104 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12106 return fold_build2_loc (loc, code, type, arg0, tem);
12109 /* Check for the possibility of merging component references. If our
12110 lhs is another similar operation, try to merge its rhs with our
12111 rhs. Then try to merge our lhs and rhs. */
12112 if (TREE_CODE (arg0) == code
12113 && 0 != (tem = fold_truthop (loc, code, type,
12114 TREE_OPERAND (arg0, 1), arg1)))
12115 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12117 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12122 case TRUTH_ORIF_EXPR:
12123 /* Note that the operands of this must be ints
12124 and their values must be 0 or true.
12125 ("true" is a fixed value perhaps depending on the language.) */
12126 /* If first arg is constant true, return it. */
12127 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12128 return fold_convert_loc (loc, type, arg0);
12129 case TRUTH_OR_EXPR:
12130 /* If either arg is constant zero, drop it. */
12131 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12132 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12133 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12134 /* Preserve sequence points. */
12135 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12136 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12137 /* If second arg is constant true, result is true, but we must
12138 evaluate first arg. */
12139 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12140 return omit_one_operand_loc (loc, type, arg1, arg0);
12141 /* Likewise for first arg, but note this only occurs here for
12143 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12144 return omit_one_operand_loc (loc, type, arg0, arg1);
12146 /* !X || X is always true. */
12147 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12148 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12149 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12150 /* X || !X is always true. */
12151 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12152 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12153 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12157 case TRUTH_XOR_EXPR:
12158 /* If the second arg is constant zero, drop it. */
12159 if (integer_zerop (arg1))
12160 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12161 /* If the second arg is constant true, this is a logical inversion. */
12162 if (integer_onep (arg1))
12164 /* Only call invert_truthvalue if operand is a truth value. */
12165 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12166 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12168 tem = invert_truthvalue_loc (loc, arg0);
12169 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12171 /* Identical arguments cancel to zero. */
12172 if (operand_equal_p (arg0, arg1, 0))
12173 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12175 /* !X ^ X is always true. */
12176 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12177 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12178 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12180 /* X ^ !X is always true. */
12181 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12182 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12183 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12189 tem = fold_comparison (loc, code, type, op0, op1);
12190 if (tem != NULL_TREE)
12193 /* bool_var != 0 becomes bool_var. */
12194 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12195 && code == NE_EXPR)
12196 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12198 /* bool_var == 1 becomes bool_var. */
12199 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12200 && code == EQ_EXPR)
12201 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12203 /* bool_var != 1 becomes !bool_var. */
12204 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12205 && code == NE_EXPR)
12206 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12207 fold_convert_loc (loc, type, arg0));
12209 /* bool_var == 0 becomes !bool_var. */
12210 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12211 && code == EQ_EXPR)
12212 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12213 fold_convert_loc (loc, type, arg0));
12215 /* !exp != 0 becomes !exp */
12216 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12217 && code == NE_EXPR)
12218 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12220 /* If this is an equality comparison of the address of two non-weak,
12221 unaliased symbols neither of which are extern (since we do not
12222 have access to attributes for externs), then we know the result. */
12223 if (TREE_CODE (arg0) == ADDR_EXPR
12224 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12225 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12226 && ! lookup_attribute ("alias",
12227 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12228 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12229 && TREE_CODE (arg1) == ADDR_EXPR
12230 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12231 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12232 && ! lookup_attribute ("alias",
12233 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12234 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12236 /* We know that we're looking at the address of two
12237 non-weak, unaliased, static _DECL nodes.
12239 It is both wasteful and incorrect to call operand_equal_p
12240 to compare the two ADDR_EXPR nodes. It is wasteful in that
12241 all we need to do is test pointer equality for the arguments
12242 to the two ADDR_EXPR nodes. It is incorrect to use
12243 operand_equal_p as that function is NOT equivalent to a
12244 C equality test. It can in fact return false for two
12245 objects which would test as equal using the C equality
12247 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12248 return constant_boolean_node (equal
12249 ? code == EQ_EXPR : code != EQ_EXPR,
12253 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12254 a MINUS_EXPR of a constant, we can convert it into a comparison with
12255 a revised constant as long as no overflow occurs. */
12256 if (TREE_CODE (arg1) == INTEGER_CST
12257 && (TREE_CODE (arg0) == PLUS_EXPR
12258 || TREE_CODE (arg0) == MINUS_EXPR)
12259 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12260 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12261 ? MINUS_EXPR : PLUS_EXPR,
12262 fold_convert_loc (loc, TREE_TYPE (arg0),
12264 TREE_OPERAND (arg0, 1)))
12265 && !TREE_OVERFLOW (tem))
12266 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12268 /* Similarly for a NEGATE_EXPR. */
12269 if (TREE_CODE (arg0) == NEGATE_EXPR
12270 && TREE_CODE (arg1) == INTEGER_CST
12271 && 0 != (tem = negate_expr (arg1))
12272 && TREE_CODE (tem) == INTEGER_CST
12273 && !TREE_OVERFLOW (tem))
12274 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12276 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12277 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12278 && TREE_CODE (arg1) == INTEGER_CST
12279 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12280 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12281 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12282 fold_convert_loc (loc,
12285 TREE_OPERAND (arg0, 1)));
12287 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12288 if ((TREE_CODE (arg0) == PLUS_EXPR
12289 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12290 || TREE_CODE (arg0) == MINUS_EXPR)
12291 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12292 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12293 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12295 tree val = TREE_OPERAND (arg0, 1);
12296 return omit_two_operands_loc (loc, type,
12297 fold_build2_loc (loc, code, type,
12299 build_int_cst (TREE_TYPE (val),
12301 TREE_OPERAND (arg0, 0), arg1);
12304 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12305 if (TREE_CODE (arg0) == MINUS_EXPR
12306 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12307 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12308 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12310 return omit_two_operands_loc (loc, type,
12312 ? boolean_true_node : boolean_false_node,
12313 TREE_OPERAND (arg0, 1), arg1);
12316 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12317 for !=. Don't do this for ordered comparisons due to overflow. */
12318 if (TREE_CODE (arg0) == MINUS_EXPR
12319 && integer_zerop (arg1))
12320 return fold_build2_loc (loc, code, type,
12321 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12323 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12324 if (TREE_CODE (arg0) == ABS_EXPR
12325 && (integer_zerop (arg1) || real_zerop (arg1)))
12326 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12328 /* If this is an EQ or NE comparison with zero and ARG0 is
12329 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12330 two operations, but the latter can be done in one less insn
12331 on machines that have only two-operand insns or on which a
12332 constant cannot be the first operand. */
12333 if (TREE_CODE (arg0) == BIT_AND_EXPR
12334 && integer_zerop (arg1))
12336 tree arg00 = TREE_OPERAND (arg0, 0);
12337 tree arg01 = TREE_OPERAND (arg0, 1);
12338 if (TREE_CODE (arg00) == LSHIFT_EXPR
12339 && integer_onep (TREE_OPERAND (arg00, 0)))
12341 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12342 arg01, TREE_OPERAND (arg00, 1));
12343 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12344 build_int_cst (TREE_TYPE (arg0), 1));
12345 return fold_build2_loc (loc, code, type,
12346 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12349 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12350 && integer_onep (TREE_OPERAND (arg01, 0)))
12352 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12353 arg00, TREE_OPERAND (arg01, 1));
12354 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12355 build_int_cst (TREE_TYPE (arg0), 1));
12356 return fold_build2_loc (loc, code, type,
12357 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12362 /* If this is an NE or EQ comparison of zero against the result of a
12363 signed MOD operation whose second operand is a power of 2, make
12364 the MOD operation unsigned since it is simpler and equivalent. */
12365 if (integer_zerop (arg1)
12366 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12367 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12368 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12369 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12370 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12371 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12373 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12374 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12375 fold_convert_loc (loc, newtype,
12376 TREE_OPERAND (arg0, 0)),
12377 fold_convert_loc (loc, newtype,
12378 TREE_OPERAND (arg0, 1)));
12380 return fold_build2_loc (loc, code, type, newmod,
12381 fold_convert_loc (loc, newtype, arg1));
12384 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12385 C1 is a valid shift constant, and C2 is a power of two, i.e.
12387 if (TREE_CODE (arg0) == BIT_AND_EXPR
12388 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12389 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12391 && integer_pow2p (TREE_OPERAND (arg0, 1))
12392 && integer_zerop (arg1))
12394 tree itype = TREE_TYPE (arg0);
12395 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12396 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12398 /* Check for a valid shift count. */
12399 if (TREE_INT_CST_HIGH (arg001) == 0
12400 && TREE_INT_CST_LOW (arg001) < prec)
12402 tree arg01 = TREE_OPERAND (arg0, 1);
12403 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12404 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12405 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12406 can be rewritten as (X & (C2 << C1)) != 0. */
12407 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12409 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12410 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12411 return fold_build2_loc (loc, code, type, tem, arg1);
12413 /* Otherwise, for signed (arithmetic) shifts,
12414 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12415 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12416 else if (!TYPE_UNSIGNED (itype))
12417 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12418 arg000, build_int_cst (itype, 0));
12419 /* Otherwise, of unsigned (logical) shifts,
12420 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12421 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12423 return omit_one_operand_loc (loc, type,
12424 code == EQ_EXPR ? integer_one_node
12425 : integer_zero_node,
12430 /* If this is an NE comparison of zero with an AND of one, remove the
12431 comparison since the AND will give the correct value. */
12432 if (code == NE_EXPR
12433 && integer_zerop (arg1)
12434 && TREE_CODE (arg0) == BIT_AND_EXPR
12435 && integer_onep (TREE_OPERAND (arg0, 1)))
12436 return fold_convert_loc (loc, type, arg0);
12438 /* If we have (A & C) == C where C is a power of 2, convert this into
12439 (A & C) != 0. Similarly for NE_EXPR. */
12440 if (TREE_CODE (arg0) == BIT_AND_EXPR
12441 && integer_pow2p (TREE_OPERAND (arg0, 1))
12442 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12443 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12444 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12445 integer_zero_node));
12447 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12448 bit, then fold the expression into A < 0 or A >= 0. */
12449 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12453 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12454 Similarly for NE_EXPR. */
12455 if (TREE_CODE (arg0) == BIT_AND_EXPR
12456 && TREE_CODE (arg1) == INTEGER_CST
12457 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12459 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12460 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12461 TREE_OPERAND (arg0, 1));
12462 tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12464 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12465 if (integer_nonzerop (dandnotc))
12466 return omit_one_operand_loc (loc, type, rslt, arg0);
12469 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12470 Similarly for NE_EXPR. */
12471 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12472 && TREE_CODE (arg1) == INTEGER_CST
12473 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12475 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12476 tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12477 TREE_OPERAND (arg0, 1), notd);
12478 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12479 if (integer_nonzerop (candnotd))
12480 return omit_one_operand_loc (loc, type, rslt, arg0);
12483 /* If this is a comparison of a field, we may be able to simplify it. */
12484 if ((TREE_CODE (arg0) == COMPONENT_REF
12485 || TREE_CODE (arg0) == BIT_FIELD_REF)
12486 /* Handle the constant case even without -O
12487 to make sure the warnings are given. */
12488 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12490 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12495 /* Optimize comparisons of strlen vs zero to a compare of the
12496 first character of the string vs zero. To wit,
12497 strlen(ptr) == 0 => *ptr == 0
12498 strlen(ptr) != 0 => *ptr != 0
12499 Other cases should reduce to one of these two (or a constant)
12500 due to the return value of strlen being unsigned. */
12501 if (TREE_CODE (arg0) == CALL_EXPR
12502 && integer_zerop (arg1))
12504 tree fndecl = get_callee_fndecl (arg0);
12507 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12508 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12509 && call_expr_nargs (arg0) == 1
12510 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12512 tree iref = build_fold_indirect_ref_loc (loc,
12513 CALL_EXPR_ARG (arg0, 0));
12514 return fold_build2_loc (loc, code, type, iref,
12515 build_int_cst (TREE_TYPE (iref), 0));
12519 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12520 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12521 if (TREE_CODE (arg0) == RSHIFT_EXPR
12522 && integer_zerop (arg1)
12523 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12525 tree arg00 = TREE_OPERAND (arg0, 0);
12526 tree arg01 = TREE_OPERAND (arg0, 1);
12527 tree itype = TREE_TYPE (arg00);
12528 if (TREE_INT_CST_HIGH (arg01) == 0
12529 && TREE_INT_CST_LOW (arg01)
12530 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12532 if (TYPE_UNSIGNED (itype))
12534 itype = signed_type_for (itype);
12535 arg00 = fold_convert_loc (loc, itype, arg00);
12537 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12538 type, arg00, build_int_cst (itype, 0));
12542 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12543 if (integer_zerop (arg1)
12544 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12545 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12546 TREE_OPERAND (arg0, 1));
12548 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12549 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12550 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12551 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12552 build_int_cst (TREE_TYPE (arg1), 0));
12553 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12554 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12555 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12556 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12557 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12558 build_int_cst (TREE_TYPE (arg1), 0));
12560 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12561 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12562 && TREE_CODE (arg1) == INTEGER_CST
12563 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12564 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12565 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12566 TREE_OPERAND (arg0, 1), arg1));
12568 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12569 (X & C) == 0 when C is a single bit. */
12570 if (TREE_CODE (arg0) == BIT_AND_EXPR
12571 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12572 && integer_zerop (arg1)
12573 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12575 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12576 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12577 TREE_OPERAND (arg0, 1));
12578 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12582 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12583 constant C is a power of two, i.e. a single bit. */
12584 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12585 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12586 && integer_zerop (arg1)
12587 && integer_pow2p (TREE_OPERAND (arg0, 1))
12588 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12589 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12591 tree arg00 = TREE_OPERAND (arg0, 0);
12592 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12593 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12596 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12597 when is C is a power of two, i.e. a single bit. */
12598 if (TREE_CODE (arg0) == BIT_AND_EXPR
12599 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12600 && integer_zerop (arg1)
12601 && integer_pow2p (TREE_OPERAND (arg0, 1))
12602 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12603 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12605 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12606 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12607 arg000, TREE_OPERAND (arg0, 1));
12608 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12609 tem, build_int_cst (TREE_TYPE (tem), 0));
12612 if (integer_zerop (arg1)
12613 && tree_expr_nonzero_p (arg0))
12615 tree res = constant_boolean_node (code==NE_EXPR, type);
12616 return omit_one_operand_loc (loc, type, res, arg0);
12619 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12620 if (TREE_CODE (arg0) == NEGATE_EXPR
12621 && TREE_CODE (arg1) == NEGATE_EXPR)
12622 return fold_build2_loc (loc, code, type,
12623 TREE_OPERAND (arg0, 0),
12624 TREE_OPERAND (arg1, 0));
12626 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12627 if (TREE_CODE (arg0) == BIT_AND_EXPR
12628 && TREE_CODE (arg1) == BIT_AND_EXPR)
12630 tree arg00 = TREE_OPERAND (arg0, 0);
12631 tree arg01 = TREE_OPERAND (arg0, 1);
12632 tree arg10 = TREE_OPERAND (arg1, 0);
12633 tree arg11 = TREE_OPERAND (arg1, 1);
12634 tree itype = TREE_TYPE (arg0);
12636 if (operand_equal_p (arg01, arg11, 0))
12637 return fold_build2_loc (loc, code, type,
12638 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12639 fold_build2_loc (loc,
12640 BIT_XOR_EXPR, itype,
12643 build_int_cst (itype, 0));
12645 if (operand_equal_p (arg01, arg10, 0))
12646 return fold_build2_loc (loc, code, type,
12647 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12648 fold_build2_loc (loc,
12649 BIT_XOR_EXPR, itype,
12652 build_int_cst (itype, 0));
12654 if (operand_equal_p (arg00, arg11, 0))
12655 return fold_build2_loc (loc, code, type,
12656 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12657 fold_build2_loc (loc,
12658 BIT_XOR_EXPR, itype,
12661 build_int_cst (itype, 0));
12663 if (operand_equal_p (arg00, arg10, 0))
12664 return fold_build2_loc (loc, code, type,
12665 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12666 fold_build2_loc (loc,
12667 BIT_XOR_EXPR, itype,
12670 build_int_cst (itype, 0));
12673 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12674 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12676 tree arg00 = TREE_OPERAND (arg0, 0);
12677 tree arg01 = TREE_OPERAND (arg0, 1);
12678 tree arg10 = TREE_OPERAND (arg1, 0);
12679 tree arg11 = TREE_OPERAND (arg1, 1);
12680 tree itype = TREE_TYPE (arg0);
12682 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12683 operand_equal_p guarantees no side-effects so we don't need
12684 to use omit_one_operand on Z. */
12685 if (operand_equal_p (arg01, arg11, 0))
12686 return fold_build2_loc (loc, code, type, arg00, arg10);
12687 if (operand_equal_p (arg01, arg10, 0))
12688 return fold_build2_loc (loc, code, type, arg00, arg11);
12689 if (operand_equal_p (arg00, arg11, 0))
12690 return fold_build2_loc (loc, code, type, arg01, arg10);
12691 if (operand_equal_p (arg00, arg10, 0))
12692 return fold_build2_loc (loc, code, type, arg01, arg11);
12694 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12695 if (TREE_CODE (arg01) == INTEGER_CST
12696 && TREE_CODE (arg11) == INTEGER_CST)
12697 return fold_build2_loc (loc, code, type,
12698 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12699 fold_build2_loc (loc,
12700 BIT_XOR_EXPR, itype,
12705 /* Attempt to simplify equality/inequality comparisons of complex
12706 values. Only lower the comparison if the result is known or
12707 can be simplified to a single scalar comparison. */
12708 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12709 || TREE_CODE (arg0) == COMPLEX_CST)
12710 && (TREE_CODE (arg1) == COMPLEX_EXPR
12711 || TREE_CODE (arg1) == COMPLEX_CST))
12713 tree real0, imag0, real1, imag1;
12716 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12718 real0 = TREE_OPERAND (arg0, 0);
12719 imag0 = TREE_OPERAND (arg0, 1);
12723 real0 = TREE_REALPART (arg0);
12724 imag0 = TREE_IMAGPART (arg0);
12727 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12729 real1 = TREE_OPERAND (arg1, 0);
12730 imag1 = TREE_OPERAND (arg1, 1);
12734 real1 = TREE_REALPART (arg1);
12735 imag1 = TREE_IMAGPART (arg1);
12738 rcond = fold_binary_loc (loc, code, type, real0, real1);
12739 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12741 if (integer_zerop (rcond))
12743 if (code == EQ_EXPR)
12744 return omit_two_operands_loc (loc, type, boolean_false_node,
12746 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12750 if (code == NE_EXPR)
12751 return omit_two_operands_loc (loc, type, boolean_true_node,
12753 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12757 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12758 if (icond && TREE_CODE (icond) == INTEGER_CST)
12760 if (integer_zerop (icond))
12762 if (code == EQ_EXPR)
12763 return omit_two_operands_loc (loc, type, boolean_false_node,
12765 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12769 if (code == NE_EXPR)
12770 return omit_two_operands_loc (loc, type, boolean_true_node,
12772 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12783 tem = fold_comparison (loc, code, type, op0, op1);
12784 if (tem != NULL_TREE)
12787 /* Transform comparisons of the form X +- C CMP X. */
12788 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12789 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12790 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12791 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12792 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12793 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12795 tree arg01 = TREE_OPERAND (arg0, 1);
12796 enum tree_code code0 = TREE_CODE (arg0);
12799 if (TREE_CODE (arg01) == REAL_CST)
12800 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12802 is_positive = tree_int_cst_sgn (arg01);
12804 /* (X - c) > X becomes false. */
12805 if (code == GT_EXPR
12806 && ((code0 == MINUS_EXPR && is_positive >= 0)
12807 || (code0 == PLUS_EXPR && is_positive <= 0)))
12809 if (TREE_CODE (arg01) == INTEGER_CST
12810 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12811 fold_overflow_warning (("assuming signed overflow does not "
12812 "occur when assuming that (X - c) > X "
12813 "is always false"),
12814 WARN_STRICT_OVERFLOW_ALL);
12815 return constant_boolean_node (0, type);
12818 /* Likewise (X + c) < X becomes false. */
12819 if (code == LT_EXPR
12820 && ((code0 == PLUS_EXPR && is_positive >= 0)
12821 || (code0 == MINUS_EXPR && is_positive <= 0)))
12823 if (TREE_CODE (arg01) == INTEGER_CST
12824 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12825 fold_overflow_warning (("assuming signed overflow does not "
12826 "occur when assuming that "
12827 "(X + c) < X is always false"),
12828 WARN_STRICT_OVERFLOW_ALL);
12829 return constant_boolean_node (0, type);
12832 /* Convert (X - c) <= X to true. */
12833 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12835 && ((code0 == MINUS_EXPR && is_positive >= 0)
12836 || (code0 == PLUS_EXPR && is_positive <= 0)))
12838 if (TREE_CODE (arg01) == INTEGER_CST
12839 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12840 fold_overflow_warning (("assuming signed overflow does not "
12841 "occur when assuming that "
12842 "(X - c) <= X is always true"),
12843 WARN_STRICT_OVERFLOW_ALL);
12844 return constant_boolean_node (1, type);
12847 /* Convert (X + c) >= X to true. */
12848 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12850 && ((code0 == PLUS_EXPR && is_positive >= 0)
12851 || (code0 == MINUS_EXPR && is_positive <= 0)))
12853 if (TREE_CODE (arg01) == INTEGER_CST
12854 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12855 fold_overflow_warning (("assuming signed overflow does not "
12856 "occur when assuming that "
12857 "(X + c) >= X is always true"),
12858 WARN_STRICT_OVERFLOW_ALL);
12859 return constant_boolean_node (1, type);
12862 if (TREE_CODE (arg01) == INTEGER_CST)
12864 /* Convert X + c > X and X - c < X to true for integers. */
12865 if (code == GT_EXPR
12866 && ((code0 == PLUS_EXPR && is_positive > 0)
12867 || (code0 == MINUS_EXPR && is_positive < 0)))
12869 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12870 fold_overflow_warning (("assuming signed overflow does "
12871 "not occur when assuming that "
12872 "(X + c) > X is always true"),
12873 WARN_STRICT_OVERFLOW_ALL);
12874 return constant_boolean_node (1, type);
12877 if (code == LT_EXPR
12878 && ((code0 == MINUS_EXPR && is_positive > 0)
12879 || (code0 == PLUS_EXPR && is_positive < 0)))
12881 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12882 fold_overflow_warning (("assuming signed overflow does "
12883 "not occur when assuming that "
12884 "(X - c) < X is always true"),
12885 WARN_STRICT_OVERFLOW_ALL);
12886 return constant_boolean_node (1, type);
12889 /* Convert X + c <= X and X - c >= X to false for integers. */
12890 if (code == LE_EXPR
12891 && ((code0 == PLUS_EXPR && is_positive > 0)
12892 || (code0 == MINUS_EXPR && is_positive < 0)))
12894 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12895 fold_overflow_warning (("assuming signed overflow does "
12896 "not occur when assuming that "
12897 "(X + c) <= X is always false"),
12898 WARN_STRICT_OVERFLOW_ALL);
12899 return constant_boolean_node (0, type);
12902 if (code == GE_EXPR
12903 && ((code0 == MINUS_EXPR && is_positive > 0)
12904 || (code0 == PLUS_EXPR && is_positive < 0)))
12906 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12907 fold_overflow_warning (("assuming signed overflow does "
12908 "not occur when assuming that "
12909 "(X - c) >= X is always false"),
12910 WARN_STRICT_OVERFLOW_ALL);
12911 return constant_boolean_node (0, type);
12916 /* Comparisons with the highest or lowest possible integer of
12917 the specified precision will have known values. */
12919 tree arg1_type = TREE_TYPE (arg1);
12920 unsigned int width = TYPE_PRECISION (arg1_type);
12922 if (TREE_CODE (arg1) == INTEGER_CST
12923 && width <= 2 * HOST_BITS_PER_WIDE_INT
12924 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12926 HOST_WIDE_INT signed_max_hi;
12927 unsigned HOST_WIDE_INT signed_max_lo;
12928 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12930 if (width <= HOST_BITS_PER_WIDE_INT)
12932 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12937 if (TYPE_UNSIGNED (arg1_type))
12939 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12945 max_lo = signed_max_lo;
12946 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12952 width -= HOST_BITS_PER_WIDE_INT;
12953 signed_max_lo = -1;
12954 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12959 if (TYPE_UNSIGNED (arg1_type))
12961 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12966 max_hi = signed_max_hi;
12967 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12971 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12972 && TREE_INT_CST_LOW (arg1) == max_lo)
12976 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12979 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12982 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12985 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12987 /* The GE_EXPR and LT_EXPR cases above are not normally
12988 reached because of previous transformations. */
12993 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12995 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12999 arg1 = const_binop (PLUS_EXPR, arg1,
13000 build_int_cst (TREE_TYPE (arg1), 1));
13001 return fold_build2_loc (loc, EQ_EXPR, type,
13002 fold_convert_loc (loc,
13003 TREE_TYPE (arg1), arg0),
13006 arg1 = const_binop (PLUS_EXPR, arg1,
13007 build_int_cst (TREE_TYPE (arg1), 1));
13008 return fold_build2_loc (loc, NE_EXPR, type,
13009 fold_convert_loc (loc, TREE_TYPE (arg1),
13015 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13017 && TREE_INT_CST_LOW (arg1) == min_lo)
13021 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13024 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13027 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13030 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13035 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13037 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13041 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13042 return fold_build2_loc (loc, NE_EXPR, type,
13043 fold_convert_loc (loc,
13044 TREE_TYPE (arg1), arg0),
13047 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13048 return fold_build2_loc (loc, EQ_EXPR, type,
13049 fold_convert_loc (loc, TREE_TYPE (arg1),
13056 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13057 && TREE_INT_CST_LOW (arg1) == signed_max_lo
13058 && TYPE_UNSIGNED (arg1_type)
13059 /* We will flip the signedness of the comparison operator
13060 associated with the mode of arg1, so the sign bit is
13061 specified by this mode. Check that arg1 is the signed
13062 max associated with this sign bit. */
13063 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13064 /* signed_type does not work on pointer types. */
13065 && INTEGRAL_TYPE_P (arg1_type))
13067 /* The following case also applies to X < signed_max+1
13068 and X >= signed_max+1 because previous transformations. */
13069 if (code == LE_EXPR || code == GT_EXPR)
13072 st = signed_type_for (TREE_TYPE (arg1));
13073 return fold_build2_loc (loc,
13074 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13075 type, fold_convert_loc (loc, st, arg0),
13076 build_int_cst (st, 0));
13082 /* If we are comparing an ABS_EXPR with a constant, we can
13083 convert all the cases into explicit comparisons, but they may
13084 well not be faster than doing the ABS and one comparison.
13085 But ABS (X) <= C is a range comparison, which becomes a subtraction
13086 and a comparison, and is probably faster. */
13087 if (code == LE_EXPR
13088 && TREE_CODE (arg1) == INTEGER_CST
13089 && TREE_CODE (arg0) == ABS_EXPR
13090 && ! TREE_SIDE_EFFECTS (arg0)
13091 && (0 != (tem = negate_expr (arg1)))
13092 && TREE_CODE (tem) == INTEGER_CST
13093 && !TREE_OVERFLOW (tem))
13094 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13095 build2 (GE_EXPR, type,
13096 TREE_OPERAND (arg0, 0), tem),
13097 build2 (LE_EXPR, type,
13098 TREE_OPERAND (arg0, 0), arg1));
13100 /* Convert ABS_EXPR<x> >= 0 to true. */
13101 strict_overflow_p = false;
13102 if (code == GE_EXPR
13103 && (integer_zerop (arg1)
13104 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13105 && real_zerop (arg1)))
13106 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13108 if (strict_overflow_p)
13109 fold_overflow_warning (("assuming signed overflow does not occur "
13110 "when simplifying comparison of "
13111 "absolute value and zero"),
13112 WARN_STRICT_OVERFLOW_CONDITIONAL);
13113 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13116 /* Convert ABS_EXPR<x> < 0 to false. */
13117 strict_overflow_p = false;
13118 if (code == LT_EXPR
13119 && (integer_zerop (arg1) || real_zerop (arg1))
13120 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13122 if (strict_overflow_p)
13123 fold_overflow_warning (("assuming signed overflow does not occur "
13124 "when simplifying comparison of "
13125 "absolute value and zero"),
13126 WARN_STRICT_OVERFLOW_CONDITIONAL);
13127 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13130 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13131 and similarly for >= into !=. */
13132 if ((code == LT_EXPR || code == GE_EXPR)
13133 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13134 && TREE_CODE (arg1) == LSHIFT_EXPR
13135 && integer_onep (TREE_OPERAND (arg1, 0)))
13137 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13138 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13139 TREE_OPERAND (arg1, 1)),
13140 build_int_cst (TREE_TYPE (arg0), 0));
13141 goto fold_binary_exit;
13144 if ((code == LT_EXPR || code == GE_EXPR)
13145 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13146 && CONVERT_EXPR_P (arg1)
13147 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13148 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13150 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13151 fold_convert_loc (loc, TREE_TYPE (arg0),
13152 build2 (RSHIFT_EXPR,
13153 TREE_TYPE (arg0), arg0,
13154 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13156 build_int_cst (TREE_TYPE (arg0), 0));
13157 goto fold_binary_exit;
13162 case UNORDERED_EXPR:
13170 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13172 t1 = fold_relational_const (code, type, arg0, arg1);
13173 if (t1 != NULL_TREE)
13177 /* If the first operand is NaN, the result is constant. */
13178 if (TREE_CODE (arg0) == REAL_CST
13179 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13180 && (code != LTGT_EXPR || ! flag_trapping_math))
13182 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13183 ? integer_zero_node
13184 : integer_one_node;
13185 return omit_one_operand_loc (loc, type, t1, arg1);
13188 /* If the second operand is NaN, the result is constant. */
13189 if (TREE_CODE (arg1) == REAL_CST
13190 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13191 && (code != LTGT_EXPR || ! flag_trapping_math))
13193 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13194 ? integer_zero_node
13195 : integer_one_node;
13196 return omit_one_operand_loc (loc, type, t1, arg0);
13199 /* Simplify unordered comparison of something with itself. */
13200 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13201 && operand_equal_p (arg0, arg1, 0))
13202 return constant_boolean_node (1, type);
13204 if (code == LTGT_EXPR
13205 && !flag_trapping_math
13206 && operand_equal_p (arg0, arg1, 0))
13207 return constant_boolean_node (0, type);
13209 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13211 tree targ0 = strip_float_extensions (arg0);
13212 tree targ1 = strip_float_extensions (arg1);
13213 tree newtype = TREE_TYPE (targ0);
13215 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13216 newtype = TREE_TYPE (targ1);
13218 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13219 return fold_build2_loc (loc, code, type,
13220 fold_convert_loc (loc, newtype, targ0),
13221 fold_convert_loc (loc, newtype, targ1));
13226 case COMPOUND_EXPR:
13227 /* When pedantic, a compound expression can be neither an lvalue
13228 nor an integer constant expression. */
13229 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13231 /* Don't let (0, 0) be null pointer constant. */
13232 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13233 : fold_convert_loc (loc, type, arg1);
13234 return pedantic_non_lvalue_loc (loc, tem);
13237 if ((TREE_CODE (arg0) == REAL_CST
13238 && TREE_CODE (arg1) == REAL_CST)
13239 || (TREE_CODE (arg0) == INTEGER_CST
13240 && TREE_CODE (arg1) == INTEGER_CST))
13241 return build_complex (type, arg0, arg1);
13245 /* An ASSERT_EXPR should never be passed to fold_binary. */
13246 gcc_unreachable ();
13250 } /* switch (code) */
13252 protected_set_expr_location (tem, loc);
13256 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13257 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13261 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13263 switch (TREE_CODE (*tp))
13269 *walk_subtrees = 0;
13271 /* ... fall through ... */
13278 /* Return whether the sub-tree ST contains a label which is accessible from
13279 outside the sub-tree. */
13282 contains_label_p (tree st)
13285 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13288 /* Fold a ternary expression of code CODE and type TYPE with operands
13289 OP0, OP1, and OP2. Return the folded expression if folding is
13290 successful. Otherwise, return NULL_TREE. */
13293 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13294 tree op0, tree op1, tree op2)
13297 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13298 enum tree_code_class kind = TREE_CODE_CLASS (code);
13300 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13301 && TREE_CODE_LENGTH (code) == 3);
13303 /* Strip any conversions that don't change the mode. This is safe
13304 for every expression, except for a comparison expression because
13305 its signedness is derived from its operands. So, in the latter
13306 case, only strip conversions that don't change the signedness.
13308 Note that this is done as an internal manipulation within the
13309 constant folder, in order to find the simplest representation of
13310 the arguments so that their form can be studied. In any cases,
13311 the appropriate type conversions should be put back in the tree
13312 that will get out of the constant folder. */
13333 case COMPONENT_REF:
13334 if (TREE_CODE (arg0) == CONSTRUCTOR
13335 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13337 unsigned HOST_WIDE_INT idx;
13339 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13346 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13347 so all simple results must be passed through pedantic_non_lvalue. */
13348 if (TREE_CODE (arg0) == INTEGER_CST)
13350 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13351 tem = integer_zerop (arg0) ? op2 : op1;
13352 /* Only optimize constant conditions when the selected branch
13353 has the same type as the COND_EXPR. This avoids optimizing
13354 away "c ? x : throw", where the throw has a void type.
13355 Avoid throwing away that operand which contains label. */
13356 if ((!TREE_SIDE_EFFECTS (unused_op)
13357 || !contains_label_p (unused_op))
13358 && (! VOID_TYPE_P (TREE_TYPE (tem))
13359 || VOID_TYPE_P (type)))
13360 return pedantic_non_lvalue_loc (loc, tem);
13363 if (operand_equal_p (arg1, op2, 0))
13364 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13366 /* If we have A op B ? A : C, we may be able to convert this to a
13367 simpler expression, depending on the operation and the values
13368 of B and C. Signed zeros prevent all of these transformations,
13369 for reasons given above each one.
13371 Also try swapping the arguments and inverting the conditional. */
13372 if (COMPARISON_CLASS_P (arg0)
13373 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13374 arg1, TREE_OPERAND (arg0, 1))
13375 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13377 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13382 if (COMPARISON_CLASS_P (arg0)
13383 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13385 TREE_OPERAND (arg0, 1))
13386 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13388 tem = fold_truth_not_expr (loc, arg0);
13389 if (tem && COMPARISON_CLASS_P (tem))
13391 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13397 /* If the second operand is simpler than the third, swap them
13398 since that produces better jump optimization results. */
13399 if (truth_value_p (TREE_CODE (arg0))
13400 && tree_swap_operands_p (op1, op2, false))
13402 /* See if this can be inverted. If it can't, possibly because
13403 it was a floating-point inequality comparison, don't do
13405 tem = fold_truth_not_expr (loc, arg0);
13407 return fold_build3_loc (loc, code, type, tem, op2, op1);
13410 /* Convert A ? 1 : 0 to simply A. */
13411 if (integer_onep (op1)
13412 && integer_zerop (op2)
13413 /* If we try to convert OP0 to our type, the
13414 call to fold will try to move the conversion inside
13415 a COND, which will recurse. In that case, the COND_EXPR
13416 is probably the best choice, so leave it alone. */
13417 && type == TREE_TYPE (arg0))
13418 return pedantic_non_lvalue_loc (loc, arg0);
13420 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13421 over COND_EXPR in cases such as floating point comparisons. */
13422 if (integer_zerop (op1)
13423 && integer_onep (op2)
13424 && truth_value_p (TREE_CODE (arg0)))
13425 return pedantic_non_lvalue_loc (loc,
13426 fold_convert_loc (loc, type,
13427 invert_truthvalue_loc (loc,
13430 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13431 if (TREE_CODE (arg0) == LT_EXPR
13432 && integer_zerop (TREE_OPERAND (arg0, 1))
13433 && integer_zerop (op2)
13434 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13436 /* sign_bit_p only checks ARG1 bits within A's precision.
13437 If <sign bit of A> has wider type than A, bits outside
13438 of A's precision in <sign bit of A> need to be checked.
13439 If they are all 0, this optimization needs to be done
13440 in unsigned A's type, if they are all 1 in signed A's type,
13441 otherwise this can't be done. */
13442 if (TYPE_PRECISION (TREE_TYPE (tem))
13443 < TYPE_PRECISION (TREE_TYPE (arg1))
13444 && TYPE_PRECISION (TREE_TYPE (tem))
13445 < TYPE_PRECISION (type))
13447 unsigned HOST_WIDE_INT mask_lo;
13448 HOST_WIDE_INT mask_hi;
13449 int inner_width, outer_width;
13452 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13453 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13454 if (outer_width > TYPE_PRECISION (type))
13455 outer_width = TYPE_PRECISION (type);
13457 if (outer_width > HOST_BITS_PER_WIDE_INT)
13459 mask_hi = ((unsigned HOST_WIDE_INT) -1
13460 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13466 mask_lo = ((unsigned HOST_WIDE_INT) -1
13467 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13469 if (inner_width > HOST_BITS_PER_WIDE_INT)
13471 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13472 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13476 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13477 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13479 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13480 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13482 tem_type = signed_type_for (TREE_TYPE (tem));
13483 tem = fold_convert_loc (loc, tem_type, tem);
13485 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13486 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13488 tem_type = unsigned_type_for (TREE_TYPE (tem));
13489 tem = fold_convert_loc (loc, tem_type, tem);
13497 fold_convert_loc (loc, type,
13498 fold_build2_loc (loc, BIT_AND_EXPR,
13499 TREE_TYPE (tem), tem,
13500 fold_convert_loc (loc,
13505 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13506 already handled above. */
13507 if (TREE_CODE (arg0) == BIT_AND_EXPR
13508 && integer_onep (TREE_OPERAND (arg0, 1))
13509 && integer_zerop (op2)
13510 && integer_pow2p (arg1))
13512 tree tem = TREE_OPERAND (arg0, 0);
13514 if (TREE_CODE (tem) == RSHIFT_EXPR
13515 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13516 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13517 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13518 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13519 TREE_OPERAND (tem, 0), arg1);
13522 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13523 is probably obsolete because the first operand should be a
13524 truth value (that's why we have the two cases above), but let's
13525 leave it in until we can confirm this for all front-ends. */
13526 if (integer_zerop (op2)
13527 && TREE_CODE (arg0) == NE_EXPR
13528 && integer_zerop (TREE_OPERAND (arg0, 1))
13529 && integer_pow2p (arg1)
13530 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13531 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13532 arg1, OEP_ONLY_CONST))
13533 return pedantic_non_lvalue_loc (loc,
13534 fold_convert_loc (loc, type,
13535 TREE_OPERAND (arg0, 0)));
13537 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13538 if (integer_zerop (op2)
13539 && truth_value_p (TREE_CODE (arg0))
13540 && truth_value_p (TREE_CODE (arg1)))
13541 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13542 fold_convert_loc (loc, type, arg0),
13545 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13546 if (integer_onep (op2)
13547 && truth_value_p (TREE_CODE (arg0))
13548 && truth_value_p (TREE_CODE (arg1)))
13550 /* Only perform transformation if ARG0 is easily inverted. */
13551 tem = fold_truth_not_expr (loc, arg0);
13553 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13554 fold_convert_loc (loc, type, tem),
13558 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13559 if (integer_zerop (arg1)
13560 && truth_value_p (TREE_CODE (arg0))
13561 && truth_value_p (TREE_CODE (op2)))
13563 /* Only perform transformation if ARG0 is easily inverted. */
13564 tem = fold_truth_not_expr (loc, arg0);
13566 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13567 fold_convert_loc (loc, type, tem),
13571 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13572 if (integer_onep (arg1)
13573 && truth_value_p (TREE_CODE (arg0))
13574 && truth_value_p (TREE_CODE (op2)))
13575 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13576 fold_convert_loc (loc, type, arg0),
13582 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13583 of fold_ternary on them. */
13584 gcc_unreachable ();
13586 case BIT_FIELD_REF:
13587 if ((TREE_CODE (arg0) == VECTOR_CST
13588 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13589 && type == TREE_TYPE (TREE_TYPE (arg0)))
13591 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13592 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13595 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13596 && (idx % width) == 0
13597 && (idx = idx / width)
13598 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13600 tree elements = NULL_TREE;
13602 if (TREE_CODE (arg0) == VECTOR_CST)
13603 elements = TREE_VECTOR_CST_ELTS (arg0);
13606 unsigned HOST_WIDE_INT idx;
13609 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13610 elements = tree_cons (NULL_TREE, value, elements);
13612 while (idx-- > 0 && elements)
13613 elements = TREE_CHAIN (elements);
13615 return TREE_VALUE (elements);
13617 return build_zero_cst (type);
13621 /* A bit-field-ref that referenced the full argument can be stripped. */
13622 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13623 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13624 && integer_zerop (op2))
13625 return fold_convert_loc (loc, type, arg0);
13630 /* For integers we can decompose the FMA if possible. */
13631 if (TREE_CODE (arg0) == INTEGER_CST
13632 && TREE_CODE (arg1) == INTEGER_CST)
13633 return fold_build2_loc (loc, PLUS_EXPR, type,
13634 const_binop (MULT_EXPR, arg0, arg1), arg2);
13635 if (integer_zerop (arg2))
13636 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13638 return fold_fma (loc, type, arg0, arg1, arg2);
13642 } /* switch (code) */
13645 /* Perform constant folding and related simplification of EXPR.
13646 The related simplifications include x*1 => x, x*0 => 0, etc.,
13647 and application of the associative law.
13648 NOP_EXPR conversions may be removed freely (as long as we
13649 are careful not to change the type of the overall expression).
13650 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13651 but we can constant-fold them if they have constant operands. */
13653 #ifdef ENABLE_FOLD_CHECKING
13654 # define fold(x) fold_1 (x)
13655 static tree fold_1 (tree);
13661 const tree t = expr;
13662 enum tree_code code = TREE_CODE (t);
13663 enum tree_code_class kind = TREE_CODE_CLASS (code);
13665 location_t loc = EXPR_LOCATION (expr);
13667 /* Return right away if a constant. */
13668 if (kind == tcc_constant)
13671 /* CALL_EXPR-like objects with variable numbers of operands are
13672 treated specially. */
13673 if (kind == tcc_vl_exp)
13675 if (code == CALL_EXPR)
13677 tem = fold_call_expr (loc, expr, false);
13678 return tem ? tem : expr;
13683 if (IS_EXPR_CODE_CLASS (kind))
13685 tree type = TREE_TYPE (t);
13686 tree op0, op1, op2;
13688 switch (TREE_CODE_LENGTH (code))
13691 op0 = TREE_OPERAND (t, 0);
13692 tem = fold_unary_loc (loc, code, type, op0);
13693 return tem ? tem : expr;
13695 op0 = TREE_OPERAND (t, 0);
13696 op1 = TREE_OPERAND (t, 1);
13697 tem = fold_binary_loc (loc, code, type, op0, op1);
13698 return tem ? tem : expr;
13700 op0 = TREE_OPERAND (t, 0);
13701 op1 = TREE_OPERAND (t, 1);
13702 op2 = TREE_OPERAND (t, 2);
13703 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13704 return tem ? tem : expr;
13714 tree op0 = TREE_OPERAND (t, 0);
13715 tree op1 = TREE_OPERAND (t, 1);
13717 if (TREE_CODE (op1) == INTEGER_CST
13718 && TREE_CODE (op0) == CONSTRUCTOR
13719 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13721 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13722 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13723 unsigned HOST_WIDE_INT begin = 0;
13725 /* Find a matching index by means of a binary search. */
13726 while (begin != end)
13728 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13729 tree index = VEC_index (constructor_elt, elts, middle)->index;
13731 if (TREE_CODE (index) == INTEGER_CST
13732 && tree_int_cst_lt (index, op1))
13733 begin = middle + 1;
13734 else if (TREE_CODE (index) == INTEGER_CST
13735 && tree_int_cst_lt (op1, index))
13737 else if (TREE_CODE (index) == RANGE_EXPR
13738 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13739 begin = middle + 1;
13740 else if (TREE_CODE (index) == RANGE_EXPR
13741 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13744 return VEC_index (constructor_elt, elts, middle)->value;
13752 return fold (DECL_INITIAL (t));
13756 } /* switch (code) */
13759 #ifdef ENABLE_FOLD_CHECKING
13762 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13763 static void fold_check_failed (const_tree, const_tree);
13764 void print_fold_checksum (const_tree);
13766 /* When --enable-checking=fold, compute a digest of expr before
13767 and after actual fold call to see if fold did not accidentally
13768 change original expr. */
13774 struct md5_ctx ctx;
13775 unsigned char checksum_before[16], checksum_after[16];
13778 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13779 md5_init_ctx (&ctx);
13780 fold_checksum_tree (expr, &ctx, ht);
13781 md5_finish_ctx (&ctx, checksum_before);
13784 ret = fold_1 (expr);
13786 md5_init_ctx (&ctx);
13787 fold_checksum_tree (expr, &ctx, ht);
13788 md5_finish_ctx (&ctx, checksum_after);
13791 if (memcmp (checksum_before, checksum_after, 16))
13792 fold_check_failed (expr, ret);
13798 print_fold_checksum (const_tree expr)
13800 struct md5_ctx ctx;
13801 unsigned char checksum[16], cnt;
13804 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13805 md5_init_ctx (&ctx);
13806 fold_checksum_tree (expr, &ctx, ht);
13807 md5_finish_ctx (&ctx, checksum);
13809 for (cnt = 0; cnt < 16; ++cnt)
13810 fprintf (stderr, "%02x", checksum[cnt]);
13811 putc ('\n', stderr);
13815 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13817 internal_error ("fold check: original tree changed by fold");
13821 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13824 enum tree_code code;
13825 union tree_node buf;
13830 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13831 <= sizeof (struct tree_function_decl))
13832 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13835 slot = (void **) htab_find_slot (ht, expr, INSERT);
13838 *slot = CONST_CAST_TREE (expr);
13839 code = TREE_CODE (expr);
13840 if (TREE_CODE_CLASS (code) == tcc_declaration
13841 && DECL_ASSEMBLER_NAME_SET_P (expr))
13843 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13844 memcpy ((char *) &buf, expr, tree_size (expr));
13845 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13846 expr = (tree) &buf;
13848 else if (TREE_CODE_CLASS (code) == tcc_type
13849 && (TYPE_POINTER_TO (expr)
13850 || TYPE_REFERENCE_TO (expr)
13851 || TYPE_CACHED_VALUES_P (expr)
13852 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13853 || TYPE_NEXT_VARIANT (expr)))
13855 /* Allow these fields to be modified. */
13857 memcpy ((char *) &buf, expr, tree_size (expr));
13858 expr = tmp = (tree) &buf;
13859 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13860 TYPE_POINTER_TO (tmp) = NULL;
13861 TYPE_REFERENCE_TO (tmp) = NULL;
13862 TYPE_NEXT_VARIANT (tmp) = NULL;
13863 if (TYPE_CACHED_VALUES_P (tmp))
13865 TYPE_CACHED_VALUES_P (tmp) = 0;
13866 TYPE_CACHED_VALUES (tmp) = NULL;
13869 md5_process_bytes (expr, tree_size (expr), ctx);
13870 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13871 if (TREE_CODE_CLASS (code) != tcc_type
13872 && TREE_CODE_CLASS (code) != tcc_declaration
13873 && code != TREE_LIST
13874 && code != SSA_NAME)
13875 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13876 switch (TREE_CODE_CLASS (code))
13882 md5_process_bytes (TREE_STRING_POINTER (expr),
13883 TREE_STRING_LENGTH (expr), ctx);
13886 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13887 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13890 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13896 case tcc_exceptional:
13900 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13901 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13902 expr = TREE_CHAIN (expr);
13903 goto recursive_label;
13906 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13907 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13913 case tcc_expression:
13914 case tcc_reference:
13915 case tcc_comparison:
13918 case tcc_statement:
13920 len = TREE_OPERAND_LENGTH (expr);
13921 for (i = 0; i < len; ++i)
13922 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13924 case tcc_declaration:
13925 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13926 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13927 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13929 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13930 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13931 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13932 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13933 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13935 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13936 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13938 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13940 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13941 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13942 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13946 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13947 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13948 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13949 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13950 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13951 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13952 if (INTEGRAL_TYPE_P (expr)
13953 || SCALAR_FLOAT_TYPE_P (expr))
13955 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13956 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13958 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13959 if (TREE_CODE (expr) == RECORD_TYPE
13960 || TREE_CODE (expr) == UNION_TYPE
13961 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13962 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13963 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13970 /* Helper function for outputting the checksum of a tree T. When
13971 debugging with gdb, you can "define mynext" to be "next" followed
13972 by "call debug_fold_checksum (op0)", then just trace down till the
13975 DEBUG_FUNCTION void
13976 debug_fold_checksum (const_tree t)
13979 unsigned char checksum[16];
13980 struct md5_ctx ctx;
13981 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13983 md5_init_ctx (&ctx);
13984 fold_checksum_tree (t, &ctx, ht);
13985 md5_finish_ctx (&ctx, checksum);
13988 for (i = 0; i < 16; i++)
13989 fprintf (stderr, "%d ", checksum[i]);
13991 fprintf (stderr, "\n");
13996 /* Fold a unary tree expression with code CODE of type TYPE with an
13997 operand OP0. LOC is the location of the resulting expression.
13998 Return a folded expression if successful. Otherwise, return a tree
13999 expression with code CODE of type TYPE with an operand OP0. */
14002 fold_build1_stat_loc (location_t loc,
14003 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14006 #ifdef ENABLE_FOLD_CHECKING
14007 unsigned char checksum_before[16], checksum_after[16];
14008 struct md5_ctx ctx;
14011 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14012 md5_init_ctx (&ctx);
14013 fold_checksum_tree (op0, &ctx, ht);
14014 md5_finish_ctx (&ctx, checksum_before);
14018 tem = fold_unary_loc (loc, code, type, op0);
14021 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14022 SET_EXPR_LOCATION (tem, loc);
14025 #ifdef ENABLE_FOLD_CHECKING
14026 md5_init_ctx (&ctx);
14027 fold_checksum_tree (op0, &ctx, ht);
14028 md5_finish_ctx (&ctx, checksum_after);
14031 if (memcmp (checksum_before, checksum_after, 16))
14032 fold_check_failed (op0, tem);
14037 /* Fold a binary tree expression with code CODE of type TYPE with
14038 operands OP0 and OP1. LOC is the location of the resulting
14039 expression. Return a folded expression if successful. Otherwise,
14040 return a tree expression with code CODE of type TYPE with operands
14044 fold_build2_stat_loc (location_t loc,
14045 enum tree_code code, tree type, tree op0, tree op1
14049 #ifdef ENABLE_FOLD_CHECKING
14050 unsigned char checksum_before_op0[16],
14051 checksum_before_op1[16],
14052 checksum_after_op0[16],
14053 checksum_after_op1[16];
14054 struct md5_ctx ctx;
14057 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14058 md5_init_ctx (&ctx);
14059 fold_checksum_tree (op0, &ctx, ht);
14060 md5_finish_ctx (&ctx, checksum_before_op0);
14063 md5_init_ctx (&ctx);
14064 fold_checksum_tree (op1, &ctx, ht);
14065 md5_finish_ctx (&ctx, checksum_before_op1);
14069 tem = fold_binary_loc (loc, code, type, op0, op1);
14072 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14073 SET_EXPR_LOCATION (tem, loc);
14076 #ifdef ENABLE_FOLD_CHECKING
14077 md5_init_ctx (&ctx);
14078 fold_checksum_tree (op0, &ctx, ht);
14079 md5_finish_ctx (&ctx, checksum_after_op0);
14082 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14083 fold_check_failed (op0, tem);
14085 md5_init_ctx (&ctx);
14086 fold_checksum_tree (op1, &ctx, ht);
14087 md5_finish_ctx (&ctx, checksum_after_op1);
14090 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14091 fold_check_failed (op1, tem);
14096 /* Fold a ternary tree expression with code CODE of type TYPE with
14097 operands OP0, OP1, and OP2. Return a folded expression if
14098 successful. Otherwise, return a tree expression with code CODE of
14099 type TYPE with operands OP0, OP1, and OP2. */
14102 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14103 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14106 #ifdef ENABLE_FOLD_CHECKING
14107 unsigned char checksum_before_op0[16],
14108 checksum_before_op1[16],
14109 checksum_before_op2[16],
14110 checksum_after_op0[16],
14111 checksum_after_op1[16],
14112 checksum_after_op2[16];
14113 struct md5_ctx ctx;
14116 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14117 md5_init_ctx (&ctx);
14118 fold_checksum_tree (op0, &ctx, ht);
14119 md5_finish_ctx (&ctx, checksum_before_op0);
14122 md5_init_ctx (&ctx);
14123 fold_checksum_tree (op1, &ctx, ht);
14124 md5_finish_ctx (&ctx, checksum_before_op1);
14127 md5_init_ctx (&ctx);
14128 fold_checksum_tree (op2, &ctx, ht);
14129 md5_finish_ctx (&ctx, checksum_before_op2);
14133 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14134 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14137 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14138 SET_EXPR_LOCATION (tem, loc);
14141 #ifdef ENABLE_FOLD_CHECKING
14142 md5_init_ctx (&ctx);
14143 fold_checksum_tree (op0, &ctx, ht);
14144 md5_finish_ctx (&ctx, checksum_after_op0);
14147 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14148 fold_check_failed (op0, tem);
14150 md5_init_ctx (&ctx);
14151 fold_checksum_tree (op1, &ctx, ht);
14152 md5_finish_ctx (&ctx, checksum_after_op1);
14155 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14156 fold_check_failed (op1, tem);
14158 md5_init_ctx (&ctx);
14159 fold_checksum_tree (op2, &ctx, ht);
14160 md5_finish_ctx (&ctx, checksum_after_op2);
14163 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14164 fold_check_failed (op2, tem);
14169 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14170 arguments in ARGARRAY, and a null static chain.
14171 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14172 of type TYPE from the given operands as constructed by build_call_array. */
14175 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14176 int nargs, tree *argarray)
14179 #ifdef ENABLE_FOLD_CHECKING
14180 unsigned char checksum_before_fn[16],
14181 checksum_before_arglist[16],
14182 checksum_after_fn[16],
14183 checksum_after_arglist[16];
14184 struct md5_ctx ctx;
14188 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14189 md5_init_ctx (&ctx);
14190 fold_checksum_tree (fn, &ctx, ht);
14191 md5_finish_ctx (&ctx, checksum_before_fn);
14194 md5_init_ctx (&ctx);
14195 for (i = 0; i < nargs; i++)
14196 fold_checksum_tree (argarray[i], &ctx, ht);
14197 md5_finish_ctx (&ctx, checksum_before_arglist);
14201 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14203 #ifdef ENABLE_FOLD_CHECKING
14204 md5_init_ctx (&ctx);
14205 fold_checksum_tree (fn, &ctx, ht);
14206 md5_finish_ctx (&ctx, checksum_after_fn);
14209 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14210 fold_check_failed (fn, tem);
14212 md5_init_ctx (&ctx);
14213 for (i = 0; i < nargs; i++)
14214 fold_checksum_tree (argarray[i], &ctx, ht);
14215 md5_finish_ctx (&ctx, checksum_after_arglist);
14218 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14219 fold_check_failed (NULL_TREE, tem);
14224 /* Perform constant folding and related simplification of initializer
14225 expression EXPR. These behave identically to "fold_buildN" but ignore
14226 potential run-time traps and exceptions that fold must preserve. */
14228 #define START_FOLD_INIT \
14229 int saved_signaling_nans = flag_signaling_nans;\
14230 int saved_trapping_math = flag_trapping_math;\
14231 int saved_rounding_math = flag_rounding_math;\
14232 int saved_trapv = flag_trapv;\
14233 int saved_folding_initializer = folding_initializer;\
14234 flag_signaling_nans = 0;\
14235 flag_trapping_math = 0;\
14236 flag_rounding_math = 0;\
14238 folding_initializer = 1;
14240 #define END_FOLD_INIT \
14241 flag_signaling_nans = saved_signaling_nans;\
14242 flag_trapping_math = saved_trapping_math;\
14243 flag_rounding_math = saved_rounding_math;\
14244 flag_trapv = saved_trapv;\
14245 folding_initializer = saved_folding_initializer;
14248 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14249 tree type, tree op)
14254 result = fold_build1_loc (loc, code, type, op);
14261 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14262 tree type, tree op0, tree op1)
14267 result = fold_build2_loc (loc, code, type, op0, op1);
14274 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14275 tree type, tree op0, tree op1, tree op2)
14280 result = fold_build3_loc (loc, code, type, op0, op1, op2);
14287 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14288 int nargs, tree *argarray)
14293 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14299 #undef START_FOLD_INIT
14300 #undef END_FOLD_INIT
14302 /* Determine if first argument is a multiple of second argument. Return 0 if
14303 it is not, or we cannot easily determined it to be.
14305 An example of the sort of thing we care about (at this point; this routine
14306 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14307 fold cases do now) is discovering that
14309 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14315 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14317 This code also handles discovering that
14319 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14321 is a multiple of 8 so we don't have to worry about dealing with a
14322 possible remainder.
14324 Note that we *look* inside a SAVE_EXPR only to determine how it was
14325 calculated; it is not safe for fold to do much of anything else with the
14326 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14327 at run time. For example, the latter example above *cannot* be implemented
14328 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14329 evaluation time of the original SAVE_EXPR is not necessarily the same at
14330 the time the new expression is evaluated. The only optimization of this
14331 sort that would be valid is changing
14333 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14337 SAVE_EXPR (I) * SAVE_EXPR (J)
14339 (where the same SAVE_EXPR (J) is used in the original and the
14340 transformed version). */
14343 multiple_of_p (tree type, const_tree top, const_tree bottom)
14345 if (operand_equal_p (top, bottom, 0))
14348 if (TREE_CODE (type) != INTEGER_TYPE)
14351 switch (TREE_CODE (top))
14354 /* Bitwise and provides a power of two multiple. If the mask is
14355 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14356 if (!integer_pow2p (bottom))
14361 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14362 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14366 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14367 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14370 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14374 op1 = TREE_OPERAND (top, 1);
14375 /* const_binop may not detect overflow correctly,
14376 so check for it explicitly here. */
14377 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14378 > TREE_INT_CST_LOW (op1)
14379 && TREE_INT_CST_HIGH (op1) == 0
14380 && 0 != (t1 = fold_convert (type,
14381 const_binop (LSHIFT_EXPR,
14384 && !TREE_OVERFLOW (t1))
14385 return multiple_of_p (type, t1, bottom);
14390 /* Can't handle conversions from non-integral or wider integral type. */
14391 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14392 || (TYPE_PRECISION (type)
14393 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14396 /* .. fall through ... */
14399 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14402 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14403 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14406 if (TREE_CODE (bottom) != INTEGER_CST
14407 || integer_zerop (bottom)
14408 || (TYPE_UNSIGNED (type)
14409 && (tree_int_cst_sgn (top) < 0
14410 || tree_int_cst_sgn (bottom) < 0)))
14412 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14420 /* Return true if CODE or TYPE is known to be non-negative. */
14423 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14425 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14426 && truth_value_p (code))
14427 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14428 have a signed:1 type (where the value is -1 and 0). */
14433 /* Return true if (CODE OP0) is known to be non-negative. If the return
14434 value is based on the assumption that signed overflow is undefined,
14435 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14436 *STRICT_OVERFLOW_P. */
14439 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14440 bool *strict_overflow_p)
14442 if (TYPE_UNSIGNED (type))
14448 /* We can't return 1 if flag_wrapv is set because
14449 ABS_EXPR<INT_MIN> = INT_MIN. */
14450 if (!INTEGRAL_TYPE_P (type))
14452 if (TYPE_OVERFLOW_UNDEFINED (type))
14454 *strict_overflow_p = true;
14459 case NON_LVALUE_EXPR:
14461 case FIX_TRUNC_EXPR:
14462 return tree_expr_nonnegative_warnv_p (op0,
14463 strict_overflow_p);
14467 tree inner_type = TREE_TYPE (op0);
14468 tree outer_type = type;
14470 if (TREE_CODE (outer_type) == REAL_TYPE)
14472 if (TREE_CODE (inner_type) == REAL_TYPE)
14473 return tree_expr_nonnegative_warnv_p (op0,
14474 strict_overflow_p);
14475 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14477 if (TYPE_UNSIGNED (inner_type))
14479 return tree_expr_nonnegative_warnv_p (op0,
14480 strict_overflow_p);
14483 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14485 if (TREE_CODE (inner_type) == REAL_TYPE)
14486 return tree_expr_nonnegative_warnv_p (op0,
14487 strict_overflow_p);
14488 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14489 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14490 && TYPE_UNSIGNED (inner_type);
14496 return tree_simple_nonnegative_warnv_p (code, type);
14499 /* We don't know sign of `t', so be conservative and return false. */
14503 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14504 value is based on the assumption that signed overflow is undefined,
14505 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14506 *STRICT_OVERFLOW_P. */
14509 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14510 tree op1, bool *strict_overflow_p)
14512 if (TYPE_UNSIGNED (type))
14517 case POINTER_PLUS_EXPR:
14519 if (FLOAT_TYPE_P (type))
14520 return (tree_expr_nonnegative_warnv_p (op0,
14522 && tree_expr_nonnegative_warnv_p (op1,
14523 strict_overflow_p));
14525 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14526 both unsigned and at least 2 bits shorter than the result. */
14527 if (TREE_CODE (type) == INTEGER_TYPE
14528 && TREE_CODE (op0) == NOP_EXPR
14529 && TREE_CODE (op1) == NOP_EXPR)
14531 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14532 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14533 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14534 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14536 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14537 TYPE_PRECISION (inner2)) + 1;
14538 return prec < TYPE_PRECISION (type);
14544 if (FLOAT_TYPE_P (type))
14546 /* x * x for floating point x is always non-negative. */
14547 if (operand_equal_p (op0, op1, 0))
14549 return (tree_expr_nonnegative_warnv_p (op0,
14551 && tree_expr_nonnegative_warnv_p (op1,
14552 strict_overflow_p));
14555 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14556 both unsigned and their total bits is shorter than the result. */
14557 if (TREE_CODE (type) == INTEGER_TYPE
14558 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14559 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14561 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14562 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14564 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14565 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14568 bool unsigned0 = TYPE_UNSIGNED (inner0);
14569 bool unsigned1 = TYPE_UNSIGNED (inner1);
14571 if (TREE_CODE (op0) == INTEGER_CST)
14572 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14574 if (TREE_CODE (op1) == INTEGER_CST)
14575 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14577 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14578 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14580 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14581 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14582 : TYPE_PRECISION (inner0);
14584 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14585 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14586 : TYPE_PRECISION (inner1);
14588 return precision0 + precision1 < TYPE_PRECISION (type);
14595 return (tree_expr_nonnegative_warnv_p (op0,
14597 || tree_expr_nonnegative_warnv_p (op1,
14598 strict_overflow_p));
14604 case TRUNC_DIV_EXPR:
14605 case CEIL_DIV_EXPR:
14606 case FLOOR_DIV_EXPR:
14607 case ROUND_DIV_EXPR:
14608 return (tree_expr_nonnegative_warnv_p (op0,
14610 && tree_expr_nonnegative_warnv_p (op1,
14611 strict_overflow_p));
14613 case TRUNC_MOD_EXPR:
14614 case CEIL_MOD_EXPR:
14615 case FLOOR_MOD_EXPR:
14616 case ROUND_MOD_EXPR:
14617 return tree_expr_nonnegative_warnv_p (op0,
14618 strict_overflow_p);
14620 return tree_simple_nonnegative_warnv_p (code, type);
14623 /* We don't know sign of `t', so be conservative and return false. */
14627 /* Return true if T is known to be non-negative. If the return
14628 value is based on the assumption that signed overflow is undefined,
14629 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14630 *STRICT_OVERFLOW_P. */
14633 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14635 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14638 switch (TREE_CODE (t))
14641 return tree_int_cst_sgn (t) >= 0;
14644 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14647 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14650 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14652 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14653 strict_overflow_p));
14655 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14658 /* We don't know sign of `t', so be conservative and return false. */
14662 /* Return true if T is known to be non-negative. If the return
14663 value is based on the assumption that signed overflow is undefined,
14664 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14665 *STRICT_OVERFLOW_P. */
14668 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14669 tree arg0, tree arg1, bool *strict_overflow_p)
14671 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14672 switch (DECL_FUNCTION_CODE (fndecl))
14674 CASE_FLT_FN (BUILT_IN_ACOS):
14675 CASE_FLT_FN (BUILT_IN_ACOSH):
14676 CASE_FLT_FN (BUILT_IN_CABS):
14677 CASE_FLT_FN (BUILT_IN_COSH):
14678 CASE_FLT_FN (BUILT_IN_ERFC):
14679 CASE_FLT_FN (BUILT_IN_EXP):
14680 CASE_FLT_FN (BUILT_IN_EXP10):
14681 CASE_FLT_FN (BUILT_IN_EXP2):
14682 CASE_FLT_FN (BUILT_IN_FABS):
14683 CASE_FLT_FN (BUILT_IN_FDIM):
14684 CASE_FLT_FN (BUILT_IN_HYPOT):
14685 CASE_FLT_FN (BUILT_IN_POW10):
14686 CASE_INT_FN (BUILT_IN_FFS):
14687 CASE_INT_FN (BUILT_IN_PARITY):
14688 CASE_INT_FN (BUILT_IN_POPCOUNT):
14689 case BUILT_IN_BSWAP32:
14690 case BUILT_IN_BSWAP64:
14694 CASE_FLT_FN (BUILT_IN_SQRT):
14695 /* sqrt(-0.0) is -0.0. */
14696 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14698 return tree_expr_nonnegative_warnv_p (arg0,
14699 strict_overflow_p);
14701 CASE_FLT_FN (BUILT_IN_ASINH):
14702 CASE_FLT_FN (BUILT_IN_ATAN):
14703 CASE_FLT_FN (BUILT_IN_ATANH):
14704 CASE_FLT_FN (BUILT_IN_CBRT):
14705 CASE_FLT_FN (BUILT_IN_CEIL):
14706 CASE_FLT_FN (BUILT_IN_ERF):
14707 CASE_FLT_FN (BUILT_IN_EXPM1):
14708 CASE_FLT_FN (BUILT_IN_FLOOR):
14709 CASE_FLT_FN (BUILT_IN_FMOD):
14710 CASE_FLT_FN (BUILT_IN_FREXP):
14711 CASE_FLT_FN (BUILT_IN_LCEIL):
14712 CASE_FLT_FN (BUILT_IN_LDEXP):
14713 CASE_FLT_FN (BUILT_IN_LFLOOR):
14714 CASE_FLT_FN (BUILT_IN_LLCEIL):
14715 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14716 CASE_FLT_FN (BUILT_IN_LLRINT):
14717 CASE_FLT_FN (BUILT_IN_LLROUND):
14718 CASE_FLT_FN (BUILT_IN_LRINT):
14719 CASE_FLT_FN (BUILT_IN_LROUND):
14720 CASE_FLT_FN (BUILT_IN_MODF):
14721 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14722 CASE_FLT_FN (BUILT_IN_RINT):
14723 CASE_FLT_FN (BUILT_IN_ROUND):
14724 CASE_FLT_FN (BUILT_IN_SCALB):
14725 CASE_FLT_FN (BUILT_IN_SCALBLN):
14726 CASE_FLT_FN (BUILT_IN_SCALBN):
14727 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14728 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14729 CASE_FLT_FN (BUILT_IN_SINH):
14730 CASE_FLT_FN (BUILT_IN_TANH):
14731 CASE_FLT_FN (BUILT_IN_TRUNC):
14732 /* True if the 1st argument is nonnegative. */
14733 return tree_expr_nonnegative_warnv_p (arg0,
14734 strict_overflow_p);
14736 CASE_FLT_FN (BUILT_IN_FMAX):
14737 /* True if the 1st OR 2nd arguments are nonnegative. */
14738 return (tree_expr_nonnegative_warnv_p (arg0,
14740 || (tree_expr_nonnegative_warnv_p (arg1,
14741 strict_overflow_p)));
14743 CASE_FLT_FN (BUILT_IN_FMIN):
14744 /* True if the 1st AND 2nd arguments are nonnegative. */
14745 return (tree_expr_nonnegative_warnv_p (arg0,
14747 && (tree_expr_nonnegative_warnv_p (arg1,
14748 strict_overflow_p)));
14750 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14751 /* True if the 2nd argument is nonnegative. */
14752 return tree_expr_nonnegative_warnv_p (arg1,
14753 strict_overflow_p);
14755 CASE_FLT_FN (BUILT_IN_POWI):
14756 /* True if the 1st argument is nonnegative or the second
14757 argument is an even integer. */
14758 if (TREE_CODE (arg1) == INTEGER_CST
14759 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14761 return tree_expr_nonnegative_warnv_p (arg0,
14762 strict_overflow_p);
14764 CASE_FLT_FN (BUILT_IN_POW):
14765 /* True if the 1st argument is nonnegative or the second
14766 argument is an even integer valued real. */
14767 if (TREE_CODE (arg1) == REAL_CST)
14772 c = TREE_REAL_CST (arg1);
14773 n = real_to_integer (&c);
14776 REAL_VALUE_TYPE cint;
14777 real_from_integer (&cint, VOIDmode, n,
14778 n < 0 ? -1 : 0, 0);
14779 if (real_identical (&c, &cint))
14783 return tree_expr_nonnegative_warnv_p (arg0,
14784 strict_overflow_p);
14789 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14793 /* Return true if T is known to be non-negative. If the return
14794 value is based on the assumption that signed overflow is undefined,
14795 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14796 *STRICT_OVERFLOW_P. */
14799 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14801 enum tree_code code = TREE_CODE (t);
14802 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14809 tree temp = TARGET_EXPR_SLOT (t);
14810 t = TARGET_EXPR_INITIAL (t);
14812 /* If the initializer is non-void, then it's a normal expression
14813 that will be assigned to the slot. */
14814 if (!VOID_TYPE_P (t))
14815 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14817 /* Otherwise, the initializer sets the slot in some way. One common
14818 way is an assignment statement at the end of the initializer. */
14821 if (TREE_CODE (t) == BIND_EXPR)
14822 t = expr_last (BIND_EXPR_BODY (t));
14823 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14824 || TREE_CODE (t) == TRY_CATCH_EXPR)
14825 t = expr_last (TREE_OPERAND (t, 0));
14826 else if (TREE_CODE (t) == STATEMENT_LIST)
14831 if (TREE_CODE (t) == MODIFY_EXPR
14832 && TREE_OPERAND (t, 0) == temp)
14833 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14834 strict_overflow_p);
14841 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14842 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14844 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14845 get_callee_fndecl (t),
14848 strict_overflow_p);
14850 case COMPOUND_EXPR:
14852 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14853 strict_overflow_p);
14855 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14856 strict_overflow_p);
14858 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14859 strict_overflow_p);
14862 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14866 /* We don't know sign of `t', so be conservative and return false. */
14870 /* Return true if T is known to be non-negative. If the return
14871 value is based on the assumption that signed overflow is undefined,
14872 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14873 *STRICT_OVERFLOW_P. */
14876 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14878 enum tree_code code;
14879 if (t == error_mark_node)
14882 code = TREE_CODE (t);
14883 switch (TREE_CODE_CLASS (code))
14886 case tcc_comparison:
14887 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14889 TREE_OPERAND (t, 0),
14890 TREE_OPERAND (t, 1),
14891 strict_overflow_p);
14894 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14896 TREE_OPERAND (t, 0),
14897 strict_overflow_p);
14900 case tcc_declaration:
14901 case tcc_reference:
14902 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14910 case TRUTH_AND_EXPR:
14911 case TRUTH_OR_EXPR:
14912 case TRUTH_XOR_EXPR:
14913 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14915 TREE_OPERAND (t, 0),
14916 TREE_OPERAND (t, 1),
14917 strict_overflow_p);
14918 case TRUTH_NOT_EXPR:
14919 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14921 TREE_OPERAND (t, 0),
14922 strict_overflow_p);
14929 case WITH_SIZE_EXPR:
14931 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14934 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14938 /* Return true if `t' is known to be non-negative. Handle warnings
14939 about undefined signed overflow. */
14942 tree_expr_nonnegative_p (tree t)
14944 bool ret, strict_overflow_p;
14946 strict_overflow_p = false;
14947 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14948 if (strict_overflow_p)
14949 fold_overflow_warning (("assuming signed overflow does not occur when "
14950 "determining that expression is always "
14952 WARN_STRICT_OVERFLOW_MISC);
14957 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14958 For floating point we further ensure that T is not denormal.
14959 Similar logic is present in nonzero_address in rtlanal.h.
14961 If the return value is based on the assumption that signed overflow
14962 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14963 change *STRICT_OVERFLOW_P. */
14966 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14967 bool *strict_overflow_p)
14972 return tree_expr_nonzero_warnv_p (op0,
14973 strict_overflow_p);
14977 tree inner_type = TREE_TYPE (op0);
14978 tree outer_type = type;
14980 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14981 && tree_expr_nonzero_warnv_p (op0,
14982 strict_overflow_p));
14986 case NON_LVALUE_EXPR:
14987 return tree_expr_nonzero_warnv_p (op0,
14988 strict_overflow_p);
14997 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14998 For floating point we further ensure that T is not denormal.
14999 Similar logic is present in nonzero_address in rtlanal.h.
15001 If the return value is based on the assumption that signed overflow
15002 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15003 change *STRICT_OVERFLOW_P. */
15006 tree_binary_nonzero_warnv_p (enum tree_code code,
15009 tree op1, bool *strict_overflow_p)
15011 bool sub_strict_overflow_p;
15014 case POINTER_PLUS_EXPR:
15016 if (TYPE_OVERFLOW_UNDEFINED (type))
15018 /* With the presence of negative values it is hard
15019 to say something. */
15020 sub_strict_overflow_p = false;
15021 if (!tree_expr_nonnegative_warnv_p (op0,
15022 &sub_strict_overflow_p)
15023 || !tree_expr_nonnegative_warnv_p (op1,
15024 &sub_strict_overflow_p))
15026 /* One of operands must be positive and the other non-negative. */
15027 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15028 overflows, on a twos-complement machine the sum of two
15029 nonnegative numbers can never be zero. */
15030 return (tree_expr_nonzero_warnv_p (op0,
15032 || tree_expr_nonzero_warnv_p (op1,
15033 strict_overflow_p));
15038 if (TYPE_OVERFLOW_UNDEFINED (type))
15040 if (tree_expr_nonzero_warnv_p (op0,
15042 && tree_expr_nonzero_warnv_p (op1,
15043 strict_overflow_p))
15045 *strict_overflow_p = true;
15052 sub_strict_overflow_p = false;
15053 if (tree_expr_nonzero_warnv_p (op0,
15054 &sub_strict_overflow_p)
15055 && tree_expr_nonzero_warnv_p (op1,
15056 &sub_strict_overflow_p))
15058 if (sub_strict_overflow_p)
15059 *strict_overflow_p = true;
15064 sub_strict_overflow_p = false;
15065 if (tree_expr_nonzero_warnv_p (op0,
15066 &sub_strict_overflow_p))
15068 if (sub_strict_overflow_p)
15069 *strict_overflow_p = true;
15071 /* When both operands are nonzero, then MAX must be too. */
15072 if (tree_expr_nonzero_warnv_p (op1,
15073 strict_overflow_p))
15076 /* MAX where operand 0 is positive is positive. */
15077 return tree_expr_nonnegative_warnv_p (op0,
15078 strict_overflow_p);
15080 /* MAX where operand 1 is positive is positive. */
15081 else if (tree_expr_nonzero_warnv_p (op1,
15082 &sub_strict_overflow_p)
15083 && tree_expr_nonnegative_warnv_p (op1,
15084 &sub_strict_overflow_p))
15086 if (sub_strict_overflow_p)
15087 *strict_overflow_p = true;
15093 return (tree_expr_nonzero_warnv_p (op1,
15095 || tree_expr_nonzero_warnv_p (op0,
15096 strict_overflow_p));
15105 /* Return true when T is an address and is known to be nonzero.
15106 For floating point we further ensure that T is not denormal.
15107 Similar logic is present in nonzero_address in rtlanal.h.
15109 If the return value is based on the assumption that signed overflow
15110 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15111 change *STRICT_OVERFLOW_P. */
15114 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15116 bool sub_strict_overflow_p;
15117 switch (TREE_CODE (t))
15120 return !integer_zerop (t);
15124 tree base = TREE_OPERAND (t, 0);
15125 if (!DECL_P (base))
15126 base = get_base_address (base);
15131 /* Weak declarations may link to NULL. Other things may also be NULL
15132 so protect with -fdelete-null-pointer-checks; but not variables
15133 allocated on the stack. */
15135 && (flag_delete_null_pointer_checks
15136 || (DECL_CONTEXT (base)
15137 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15138 && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15139 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15141 /* Constants are never weak. */
15142 if (CONSTANT_CLASS_P (base))
15149 sub_strict_overflow_p = false;
15150 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15151 &sub_strict_overflow_p)
15152 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15153 &sub_strict_overflow_p))
15155 if (sub_strict_overflow_p)
15156 *strict_overflow_p = true;
15167 /* Return true when T is an address and is known to be nonzero.
15168 For floating point we further ensure that T is not denormal.
15169 Similar logic is present in nonzero_address in rtlanal.h.
15171 If the return value is based on the assumption that signed overflow
15172 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15173 change *STRICT_OVERFLOW_P. */
15176 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15178 tree type = TREE_TYPE (t);
15179 enum tree_code code;
15181 /* Doing something useful for floating point would need more work. */
15182 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15185 code = TREE_CODE (t);
15186 switch (TREE_CODE_CLASS (code))
15189 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15190 strict_overflow_p);
15192 case tcc_comparison:
15193 return tree_binary_nonzero_warnv_p (code, type,
15194 TREE_OPERAND (t, 0),
15195 TREE_OPERAND (t, 1),
15196 strict_overflow_p);
15198 case tcc_declaration:
15199 case tcc_reference:
15200 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15208 case TRUTH_NOT_EXPR:
15209 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15210 strict_overflow_p);
15212 case TRUTH_AND_EXPR:
15213 case TRUTH_OR_EXPR:
15214 case TRUTH_XOR_EXPR:
15215 return tree_binary_nonzero_warnv_p (code, type,
15216 TREE_OPERAND (t, 0),
15217 TREE_OPERAND (t, 1),
15218 strict_overflow_p);
15225 case WITH_SIZE_EXPR:
15227 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15229 case COMPOUND_EXPR:
15232 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15233 strict_overflow_p);
15236 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15237 strict_overflow_p);
15240 return alloca_call_p (t);
15248 /* Return true when T is an address and is known to be nonzero.
15249 Handle warnings about undefined signed overflow. */
15252 tree_expr_nonzero_p (tree t)
15254 bool ret, strict_overflow_p;
15256 strict_overflow_p = false;
15257 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15258 if (strict_overflow_p)
15259 fold_overflow_warning (("assuming signed overflow does not occur when "
15260 "determining that expression is always "
15262 WARN_STRICT_OVERFLOW_MISC);
15266 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15267 attempt to fold the expression to a constant without modifying TYPE,
15270 If the expression could be simplified to a constant, then return
15271 the constant. If the expression would not be simplified to a
15272 constant, then return NULL_TREE. */
15275 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15277 tree tem = fold_binary (code, type, op0, op1);
15278 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15281 /* Given the components of a unary expression CODE, TYPE and OP0,
15282 attempt to fold the expression to a constant without modifying
15285 If the expression could be simplified to a constant, then return
15286 the constant. If the expression would not be simplified to a
15287 constant, then return NULL_TREE. */
15290 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15292 tree tem = fold_unary (code, type, op0);
15293 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15296 /* If EXP represents referencing an element in a constant string
15297 (either via pointer arithmetic or array indexing), return the
15298 tree representing the value accessed, otherwise return NULL. */
15301 fold_read_from_constant_string (tree exp)
15303 if ((TREE_CODE (exp) == INDIRECT_REF
15304 || TREE_CODE (exp) == ARRAY_REF)
15305 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15307 tree exp1 = TREE_OPERAND (exp, 0);
15310 location_t loc = EXPR_LOCATION (exp);
15312 if (TREE_CODE (exp) == INDIRECT_REF)
15313 string = string_constant (exp1, &index);
15316 tree low_bound = array_ref_low_bound (exp);
15317 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15319 /* Optimize the special-case of a zero lower bound.
15321 We convert the low_bound to sizetype to avoid some problems
15322 with constant folding. (E.g. suppose the lower bound is 1,
15323 and its mode is QI. Without the conversion,l (ARRAY
15324 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15325 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15326 if (! integer_zerop (low_bound))
15327 index = size_diffop_loc (loc, index,
15328 fold_convert_loc (loc, sizetype, low_bound));
15334 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15335 && TREE_CODE (string) == STRING_CST
15336 && TREE_CODE (index) == INTEGER_CST
15337 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15338 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15340 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15341 return build_int_cst_type (TREE_TYPE (exp),
15342 (TREE_STRING_POINTER (string)
15343 [TREE_INT_CST_LOW (index)]));
15348 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15349 an integer constant, real, or fixed-point constant.
15351 TYPE is the type of the result. */
15354 fold_negate_const (tree arg0, tree type)
15356 tree t = NULL_TREE;
15358 switch (TREE_CODE (arg0))
15362 double_int val = tree_to_double_int (arg0);
15363 int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15365 t = force_fit_type_double (type, val, 1,
15366 (overflow | TREE_OVERFLOW (arg0))
15367 && !TYPE_UNSIGNED (type));
15372 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15377 FIXED_VALUE_TYPE f;
15378 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15379 &(TREE_FIXED_CST (arg0)), NULL,
15380 TYPE_SATURATING (type));
15381 t = build_fixed (type, f);
15382 /* Propagate overflow flags. */
15383 if (overflow_p | TREE_OVERFLOW (arg0))
15384 TREE_OVERFLOW (t) = 1;
15389 gcc_unreachable ();
15395 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15396 an integer constant or real constant.
15398 TYPE is the type of the result. */
15401 fold_abs_const (tree arg0, tree type)
15403 tree t = NULL_TREE;
15405 switch (TREE_CODE (arg0))
15409 double_int val = tree_to_double_int (arg0);
15411 /* If the value is unsigned or non-negative, then the absolute value
15412 is the same as the ordinary value. */
15413 if (TYPE_UNSIGNED (type)
15414 || !double_int_negative_p (val))
15417 /* If the value is negative, then the absolute value is
15423 overflow = neg_double (val.low, val.high, &val.low, &val.high);
15424 t = force_fit_type_double (type, val, -1,
15425 overflow | TREE_OVERFLOW (arg0));
15431 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15432 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15438 gcc_unreachable ();
15444 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15445 constant. TYPE is the type of the result. */
15448 fold_not_const (const_tree arg0, tree type)
15452 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15454 val = double_int_not (tree_to_double_int (arg0));
15455 return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15458 /* Given CODE, a relational operator, the target type, TYPE and two
15459 constant operands OP0 and OP1, return the result of the
15460 relational operation. If the result is not a compile time
15461 constant, then return NULL_TREE. */
15464 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15466 int result, invert;
15468 /* From here on, the only cases we handle are when the result is
15469 known to be a constant. */
15471 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15473 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15474 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15476 /* Handle the cases where either operand is a NaN. */
15477 if (real_isnan (c0) || real_isnan (c1))
15487 case UNORDERED_EXPR:
15501 if (flag_trapping_math)
15507 gcc_unreachable ();
15510 return constant_boolean_node (result, type);
15513 return constant_boolean_node (real_compare (code, c0, c1), type);
15516 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15518 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15519 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15520 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15523 /* Handle equality/inequality of complex constants. */
15524 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15526 tree rcond = fold_relational_const (code, type,
15527 TREE_REALPART (op0),
15528 TREE_REALPART (op1));
15529 tree icond = fold_relational_const (code, type,
15530 TREE_IMAGPART (op0),
15531 TREE_IMAGPART (op1));
15532 if (code == EQ_EXPR)
15533 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15534 else if (code == NE_EXPR)
15535 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15540 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15542 To compute GT, swap the arguments and do LT.
15543 To compute GE, do LT and invert the result.
15544 To compute LE, swap the arguments, do LT and invert the result.
15545 To compute NE, do EQ and invert the result.
15547 Therefore, the code below must handle only EQ and LT. */
15549 if (code == LE_EXPR || code == GT_EXPR)
15554 code = swap_tree_comparison (code);
15557 /* Note that it is safe to invert for real values here because we
15558 have already handled the one case that it matters. */
15561 if (code == NE_EXPR || code == GE_EXPR)
15564 code = invert_tree_comparison (code, false);
15567 /* Compute a result for LT or EQ if args permit;
15568 Otherwise return T. */
15569 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15571 if (code == EQ_EXPR)
15572 result = tree_int_cst_equal (op0, op1);
15573 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15574 result = INT_CST_LT_UNSIGNED (op0, op1);
15576 result = INT_CST_LT (op0, op1);
15583 return constant_boolean_node (result, type);
15586 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15587 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15591 fold_build_cleanup_point_expr (tree type, tree expr)
15593 /* If the expression does not have side effects then we don't have to wrap
15594 it with a cleanup point expression. */
15595 if (!TREE_SIDE_EFFECTS (expr))
15598 /* If the expression is a return, check to see if the expression inside the
15599 return has no side effects or the right hand side of the modify expression
15600 inside the return. If either don't have side effects set we don't need to
15601 wrap the expression in a cleanup point expression. Note we don't check the
15602 left hand side of the modify because it should always be a return decl. */
15603 if (TREE_CODE (expr) == RETURN_EXPR)
15605 tree op = TREE_OPERAND (expr, 0);
15606 if (!op || !TREE_SIDE_EFFECTS (op))
15608 op = TREE_OPERAND (op, 1);
15609 if (!TREE_SIDE_EFFECTS (op))
15613 return build1 (CLEANUP_POINT_EXPR, type, expr);
15616 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15617 of an indirection through OP0, or NULL_TREE if no simplification is
15621 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15627 subtype = TREE_TYPE (sub);
15628 if (!POINTER_TYPE_P (subtype))
15631 if (TREE_CODE (sub) == ADDR_EXPR)
15633 tree op = TREE_OPERAND (sub, 0);
15634 tree optype = TREE_TYPE (op);
15635 /* *&CONST_DECL -> to the value of the const decl. */
15636 if (TREE_CODE (op) == CONST_DECL)
15637 return DECL_INITIAL (op);
15638 /* *&p => p; make sure to handle *&"str"[cst] here. */
15639 if (type == optype)
15641 tree fop = fold_read_from_constant_string (op);
15647 /* *(foo *)&fooarray => fooarray[0] */
15648 else if (TREE_CODE (optype) == ARRAY_TYPE
15649 && type == TREE_TYPE (optype))
15651 tree type_domain = TYPE_DOMAIN (optype);
15652 tree min_val = size_zero_node;
15653 if (type_domain && TYPE_MIN_VALUE (type_domain))
15654 min_val = TYPE_MIN_VALUE (type_domain);
15655 op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15656 SET_EXPR_LOCATION (op0, loc);
15659 /* *(foo *)&complexfoo => __real__ complexfoo */
15660 else if (TREE_CODE (optype) == COMPLEX_TYPE
15661 && type == TREE_TYPE (optype))
15662 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15663 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15664 else if (TREE_CODE (optype) == VECTOR_TYPE
15665 && type == TREE_TYPE (optype))
15667 tree part_width = TYPE_SIZE (type);
15668 tree index = bitsize_int (0);
15669 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15673 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15674 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15676 tree op00 = TREE_OPERAND (sub, 0);
15677 tree op01 = TREE_OPERAND (sub, 1);
15680 if (TREE_CODE (op00) == ADDR_EXPR)
15683 op00 = TREE_OPERAND (op00, 0);
15684 op00type = TREE_TYPE (op00);
15686 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15687 if (TREE_CODE (op00type) == VECTOR_TYPE
15688 && type == TREE_TYPE (op00type))
15690 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15691 tree part_width = TYPE_SIZE (type);
15692 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15693 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15694 tree index = bitsize_int (indexi);
15696 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15697 return fold_build3_loc (loc,
15698 BIT_FIELD_REF, type, op00,
15699 part_width, index);
15702 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15703 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15704 && type == TREE_TYPE (op00type))
15706 tree size = TYPE_SIZE_UNIT (type);
15707 if (tree_int_cst_equal (size, op01))
15708 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15710 /* ((foo *)&fooarray)[1] => fooarray[1] */
15711 else if (TREE_CODE (op00type) == ARRAY_TYPE
15712 && type == TREE_TYPE (op00type))
15714 tree type_domain = TYPE_DOMAIN (op00type);
15715 tree min_val = size_zero_node;
15716 if (type_domain && TYPE_MIN_VALUE (type_domain))
15717 min_val = TYPE_MIN_VALUE (type_domain);
15718 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15719 TYPE_SIZE_UNIT (type));
15720 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15721 op0 = build4 (ARRAY_REF, type, op00, op01,
15722 NULL_TREE, NULL_TREE);
15723 SET_EXPR_LOCATION (op0, loc);
15729 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15730 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15731 && type == TREE_TYPE (TREE_TYPE (subtype)))
15734 tree min_val = size_zero_node;
15735 sub = build_fold_indirect_ref_loc (loc, sub);
15736 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15737 if (type_domain && TYPE_MIN_VALUE (type_domain))
15738 min_val = TYPE_MIN_VALUE (type_domain);
15739 op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15740 SET_EXPR_LOCATION (op0, loc);
15747 /* Builds an expression for an indirection through T, simplifying some
15751 build_fold_indirect_ref_loc (location_t loc, tree t)
15753 tree type = TREE_TYPE (TREE_TYPE (t));
15754 tree sub = fold_indirect_ref_1 (loc, type, t);
15759 t = build1 (INDIRECT_REF, type, t);
15760 SET_EXPR_LOCATION (t, loc);
15764 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15767 fold_indirect_ref_loc (location_t loc, tree t)
15769 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15777 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15778 whose result is ignored. The type of the returned tree need not be
15779 the same as the original expression. */
15782 fold_ignored_result (tree t)
15784 if (!TREE_SIDE_EFFECTS (t))
15785 return integer_zero_node;
15788 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15791 t = TREE_OPERAND (t, 0);
15795 case tcc_comparison:
15796 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15797 t = TREE_OPERAND (t, 0);
15798 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15799 t = TREE_OPERAND (t, 1);
15804 case tcc_expression:
15805 switch (TREE_CODE (t))
15807 case COMPOUND_EXPR:
15808 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15810 t = TREE_OPERAND (t, 0);
15814 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15815 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15817 t = TREE_OPERAND (t, 0);
15830 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15831 This can only be applied to objects of a sizetype. */
15834 round_up_loc (location_t loc, tree value, int divisor)
15836 tree div = NULL_TREE;
15838 gcc_assert (divisor > 0);
15842 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15843 have to do anything. Only do this when we are not given a const,
15844 because in that case, this check is more expensive than just
15846 if (TREE_CODE (value) != INTEGER_CST)
15848 div = build_int_cst (TREE_TYPE (value), divisor);
15850 if (multiple_of_p (TREE_TYPE (value), value, div))
15854 /* If divisor is a power of two, simplify this to bit manipulation. */
15855 if (divisor == (divisor & -divisor))
15857 if (TREE_CODE (value) == INTEGER_CST)
15859 double_int val = tree_to_double_int (value);
15862 if ((val.low & (divisor - 1)) == 0)
15865 overflow_p = TREE_OVERFLOW (value);
15866 val.low &= ~(divisor - 1);
15867 val.low += divisor;
15875 return force_fit_type_double (TREE_TYPE (value), val,
15882 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15883 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15884 t = build_int_cst (TREE_TYPE (value), -divisor);
15885 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15891 div = build_int_cst (TREE_TYPE (value), divisor);
15892 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15893 value = size_binop_loc (loc, MULT_EXPR, value, div);
15899 /* Likewise, but round down. */
15902 round_down_loc (location_t loc, tree value, int divisor)
15904 tree div = NULL_TREE;
15906 gcc_assert (divisor > 0);
15910 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15911 have to do anything. Only do this when we are not given a const,
15912 because in that case, this check is more expensive than just
15914 if (TREE_CODE (value) != INTEGER_CST)
15916 div = build_int_cst (TREE_TYPE (value), divisor);
15918 if (multiple_of_p (TREE_TYPE (value), value, div))
15922 /* If divisor is a power of two, simplify this to bit manipulation. */
15923 if (divisor == (divisor & -divisor))
15927 t = build_int_cst (TREE_TYPE (value), -divisor);
15928 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15933 div = build_int_cst (TREE_TYPE (value), divisor);
15934 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15935 value = size_binop_loc (loc, MULT_EXPR, value, div);
15941 /* Returns the pointer to the base of the object addressed by EXP and
15942 extracts the information about the offset of the access, storing it
15943 to PBITPOS and POFFSET. */
15946 split_address_to_core_and_offset (tree exp,
15947 HOST_WIDE_INT *pbitpos, tree *poffset)
15950 enum machine_mode mode;
15951 int unsignedp, volatilep;
15952 HOST_WIDE_INT bitsize;
15953 location_t loc = EXPR_LOCATION (exp);
15955 if (TREE_CODE (exp) == ADDR_EXPR)
15957 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15958 poffset, &mode, &unsignedp, &volatilep,
15960 core = build_fold_addr_expr_loc (loc, core);
15966 *poffset = NULL_TREE;
15972 /* Returns true if addresses of E1 and E2 differ by a constant, false
15973 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15976 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15979 HOST_WIDE_INT bitpos1, bitpos2;
15980 tree toffset1, toffset2, tdiff, type;
15982 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15983 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15985 if (bitpos1 % BITS_PER_UNIT != 0
15986 || bitpos2 % BITS_PER_UNIT != 0
15987 || !operand_equal_p (core1, core2, 0))
15990 if (toffset1 && toffset2)
15992 type = TREE_TYPE (toffset1);
15993 if (type != TREE_TYPE (toffset2))
15994 toffset2 = fold_convert (type, toffset2);
15996 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15997 if (!cst_and_fits_in_hwi (tdiff))
16000 *diff = int_cst_value (tdiff);
16002 else if (toffset1 || toffset2)
16004 /* If only one of the offsets is non-constant, the difference cannot
16011 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16015 /* Simplify the floating point expression EXP when the sign of the
16016 result is not significant. Return NULL_TREE if no simplification
16020 fold_strip_sign_ops (tree exp)
16023 location_t loc = EXPR_LOCATION (exp);
16025 switch (TREE_CODE (exp))
16029 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16030 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16034 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16036 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16037 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16038 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16039 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16040 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16041 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16044 case COMPOUND_EXPR:
16045 arg0 = TREE_OPERAND (exp, 0);
16046 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16048 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16052 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16053 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16055 return fold_build3_loc (loc,
16056 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16057 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16058 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16063 const enum built_in_function fcode = builtin_mathfn_code (exp);
16066 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16067 /* Strip copysign function call, return the 1st argument. */
16068 arg0 = CALL_EXPR_ARG (exp, 0);
16069 arg1 = CALL_EXPR_ARG (exp, 1);
16070 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16073 /* Strip sign ops from the argument of "odd" math functions. */
16074 if (negate_mathfn_p (fcode))
16076 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16078 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);