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, 2011
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"
60 #include "langhooks.h"
63 #include "tree-flow.h"
65 /* Nonzero if we are folding constants inside an initializer; zero
67 int folding_initializer = 0;
69 /* The following constants represent a bit based encoding of GCC's
70 comparison operators. This encoding simplifies transformations
71 on relational comparison operators, such as AND and OR. */
72 enum comparison_code {
91 static bool negate_mathfn_p (enum built_in_function);
92 static bool negate_expr_p (tree);
93 static tree negate_expr (tree);
94 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
95 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
96 static tree const_binop (enum tree_code, tree, tree);
97 static enum comparison_code comparison_to_compcode (enum tree_code);
98 static enum tree_code compcode_to_comparison (enum comparison_code);
99 static int operand_equal_for_comparison_p (tree, tree, tree);
100 static int twoval_comparison_p (tree, tree *, tree *, int *);
101 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
102 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
103 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
104 static tree make_bit_field_ref (location_t, tree, tree,
105 HOST_WIDE_INT, HOST_WIDE_INT, int);
106 static tree optimize_bit_field_compare (location_t, enum tree_code,
108 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
110 enum machine_mode *, int *, int *,
112 static int all_ones_mask_p (const_tree, int);
113 static tree sign_bit_p (tree, const_tree);
114 static int simple_operand_p (const_tree);
115 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
116 static tree range_predecessor (tree);
117 static tree range_successor (tree);
118 extern tree make_range (tree, int *, tree *, tree *, bool *);
119 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
121 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
122 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
123 static tree unextend (tree, int, int, tree);
124 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
125 static tree optimize_minmax_comparison (location_t, enum tree_code,
127 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
128 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
129 static tree fold_binary_op_with_conditional_arg (location_t,
130 enum tree_code, tree,
133 static tree fold_mathfn_compare (location_t,
134 enum built_in_function, enum tree_code,
136 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
137 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
138 static bool reorder_operands_p (const_tree, const_tree);
139 static tree fold_negate_const (tree, tree);
140 static tree fold_not_const (const_tree, tree);
141 static tree fold_relational_const (enum tree_code, tree, tree, tree);
142 static tree fold_convert_const (enum tree_code, tree, tree);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc != UNKNOWN_LOCATION ? tloc : loc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
167 SET_EXPR_LOCATION (x, loc);
173 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
174 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
175 and SUM1. Then this yields nonzero if overflow occurred during the
178 Overflow occurs if A and B have the same sign, but A and SUM differ in
179 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
181 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
183 /* If ARG2 divides ARG1 with zero remainder, carries out the division
184 of type CODE and returns the quotient.
185 Otherwise returns NULL_TREE. */
188 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
193 /* The sign of the division is according to operand two, that
194 does the correct thing for POINTER_PLUS_EXPR where we want
195 a signed division. */
196 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
197 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
198 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
201 quo = double_int_divmod (tree_to_double_int (arg1),
202 tree_to_double_int (arg2),
205 if (double_int_zero_p (rem))
206 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
211 /* This is nonzero if we should defer warnings about undefined
212 overflow. This facility exists because these warnings are a
213 special case. The code to estimate loop iterations does not want
214 to issue any warnings, since it works with expressions which do not
215 occur in user code. Various bits of cleanup code call fold(), but
216 only use the result if it has certain characteristics (e.g., is a
217 constant); that code only wants to issue a warning if the result is
220 static int fold_deferring_overflow_warnings;
222 /* If a warning about undefined overflow is deferred, this is the
223 warning. Note that this may cause us to turn two warnings into
224 one, but that is fine since it is sufficient to only give one
225 warning per expression. */
227 static const char* fold_deferred_overflow_warning;
229 /* If a warning about undefined overflow is deferred, this is the
230 level at which the warning should be emitted. */
232 static enum warn_strict_overflow_code fold_deferred_overflow_code;
234 /* Start deferring overflow warnings. We could use a stack here to
235 permit nested calls, but at present it is not necessary. */
238 fold_defer_overflow_warnings (void)
240 ++fold_deferring_overflow_warnings;
243 /* Stop deferring overflow warnings. If there is a pending warning,
244 and ISSUE is true, then issue the warning if appropriate. STMT is
245 the statement with which the warning should be associated (used for
246 location information); STMT may be NULL. CODE is the level of the
247 warning--a warn_strict_overflow_code value. This function will use
248 the smaller of CODE and the deferred code when deciding whether to
249 issue the warning. CODE may be zero to mean to always use the
253 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
258 gcc_assert (fold_deferring_overflow_warnings > 0);
259 --fold_deferring_overflow_warnings;
260 if (fold_deferring_overflow_warnings > 0)
262 if (fold_deferred_overflow_warning != NULL
264 && code < (int) fold_deferred_overflow_code)
265 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
269 warnmsg = fold_deferred_overflow_warning;
270 fold_deferred_overflow_warning = NULL;
272 if (!issue || warnmsg == NULL)
275 if (gimple_no_warning_p (stmt))
278 /* Use the smallest code level when deciding to issue the
280 if (code == 0 || code > (int) fold_deferred_overflow_code)
281 code = fold_deferred_overflow_code;
283 if (!issue_strict_overflow_warning (code))
287 locus = input_location;
289 locus = gimple_location (stmt);
290 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
293 /* Stop deferring overflow warnings, ignoring any deferred
297 fold_undefer_and_ignore_overflow_warnings (void)
299 fold_undefer_overflow_warnings (false, NULL, 0);
302 /* Whether we are deferring overflow warnings. */
305 fold_deferring_overflow_warnings_p (void)
307 return fold_deferring_overflow_warnings > 0;
310 /* This is called when we fold something based on the fact that signed
311 overflow is undefined. */
314 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
316 if (fold_deferring_overflow_warnings > 0)
318 if (fold_deferred_overflow_warning == NULL
319 || wc < fold_deferred_overflow_code)
321 fold_deferred_overflow_warning = gmsgid;
322 fold_deferred_overflow_code = wc;
325 else if (issue_strict_overflow_warning (wc))
326 warning (OPT_Wstrict_overflow, gmsgid);
329 /* Return true if the built-in mathematical function specified by CODE
330 is odd, i.e. -f(x) == f(-x). */
333 negate_mathfn_p (enum built_in_function code)
337 CASE_FLT_FN (BUILT_IN_ASIN):
338 CASE_FLT_FN (BUILT_IN_ASINH):
339 CASE_FLT_FN (BUILT_IN_ATAN):
340 CASE_FLT_FN (BUILT_IN_ATANH):
341 CASE_FLT_FN (BUILT_IN_CASIN):
342 CASE_FLT_FN (BUILT_IN_CASINH):
343 CASE_FLT_FN (BUILT_IN_CATAN):
344 CASE_FLT_FN (BUILT_IN_CATANH):
345 CASE_FLT_FN (BUILT_IN_CBRT):
346 CASE_FLT_FN (BUILT_IN_CPROJ):
347 CASE_FLT_FN (BUILT_IN_CSIN):
348 CASE_FLT_FN (BUILT_IN_CSINH):
349 CASE_FLT_FN (BUILT_IN_CTAN):
350 CASE_FLT_FN (BUILT_IN_CTANH):
351 CASE_FLT_FN (BUILT_IN_ERF):
352 CASE_FLT_FN (BUILT_IN_LLROUND):
353 CASE_FLT_FN (BUILT_IN_LROUND):
354 CASE_FLT_FN (BUILT_IN_ROUND):
355 CASE_FLT_FN (BUILT_IN_SIN):
356 CASE_FLT_FN (BUILT_IN_SINH):
357 CASE_FLT_FN (BUILT_IN_TAN):
358 CASE_FLT_FN (BUILT_IN_TANH):
359 CASE_FLT_FN (BUILT_IN_TRUNC):
362 CASE_FLT_FN (BUILT_IN_LLRINT):
363 CASE_FLT_FN (BUILT_IN_LRINT):
364 CASE_FLT_FN (BUILT_IN_NEARBYINT):
365 CASE_FLT_FN (BUILT_IN_RINT):
366 return !flag_rounding_math;
374 /* Check whether we may negate an integer constant T without causing
378 may_negate_without_overflow_p (const_tree t)
380 unsigned HOST_WIDE_INT val;
384 gcc_assert (TREE_CODE (t) == INTEGER_CST);
386 type = TREE_TYPE (t);
387 if (TYPE_UNSIGNED (type))
390 prec = TYPE_PRECISION (type);
391 if (prec > HOST_BITS_PER_WIDE_INT)
393 if (TREE_INT_CST_LOW (t) != 0)
395 prec -= HOST_BITS_PER_WIDE_INT;
396 val = TREE_INT_CST_HIGH (t);
399 val = TREE_INT_CST_LOW (t);
400 if (prec < HOST_BITS_PER_WIDE_INT)
401 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
402 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
405 /* Determine whether an expression T can be cheaply negated using
406 the function negate_expr without introducing undefined overflow. */
409 negate_expr_p (tree t)
416 type = TREE_TYPE (t);
419 switch (TREE_CODE (t))
422 if (TYPE_OVERFLOW_WRAPS (type))
425 /* Check that -CST will not overflow type. */
426 return may_negate_without_overflow_p (t);
428 return (INTEGRAL_TYPE_P (type)
429 && TYPE_OVERFLOW_WRAPS (type));
436 /* We want to canonicalize to positive real constants. Pretend
437 that only negative ones can be easily negated. */
438 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
441 return negate_expr_p (TREE_REALPART (t))
442 && negate_expr_p (TREE_IMAGPART (t));
445 return negate_expr_p (TREE_OPERAND (t, 0))
446 && negate_expr_p (TREE_OPERAND (t, 1));
449 return negate_expr_p (TREE_OPERAND (t, 0));
452 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
453 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
455 /* -(A + B) -> (-B) - A. */
456 if (negate_expr_p (TREE_OPERAND (t, 1))
457 && reorder_operands_p (TREE_OPERAND (t, 0),
458 TREE_OPERAND (t, 1)))
460 /* -(A + B) -> (-A) - B. */
461 return negate_expr_p (TREE_OPERAND (t, 0));
464 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
465 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
466 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
467 && reorder_operands_p (TREE_OPERAND (t, 0),
468 TREE_OPERAND (t, 1));
471 if (TYPE_UNSIGNED (TREE_TYPE (t)))
477 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
478 return negate_expr_p (TREE_OPERAND (t, 1))
479 || negate_expr_p (TREE_OPERAND (t, 0));
487 /* In general we can't negate A / B, because if A is INT_MIN and
488 B is 1, we may turn this into INT_MIN / -1 which is undefined
489 and actually traps on some architectures. But if overflow is
490 undefined, we can negate, because - (INT_MIN / 1) is an
492 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
493 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
495 return negate_expr_p (TREE_OPERAND (t, 1))
496 || negate_expr_p (TREE_OPERAND (t, 0));
499 /* Negate -((double)float) as (double)(-float). */
500 if (TREE_CODE (type) == REAL_TYPE)
502 tree tem = strip_float_extensions (t);
504 return negate_expr_p (tem);
509 /* Negate -f(x) as f(-x). */
510 if (negate_mathfn_p (builtin_mathfn_code (t)))
511 return negate_expr_p (CALL_EXPR_ARG (t, 0));
515 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
516 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
518 tree op1 = TREE_OPERAND (t, 1);
519 if (TREE_INT_CST_HIGH (op1) == 0
520 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
521 == TREE_INT_CST_LOW (op1))
532 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
533 simplification is possible.
534 If negate_expr_p would return true for T, NULL_TREE will never be
538 fold_negate_expr (location_t loc, tree t)
540 tree type = TREE_TYPE (t);
543 switch (TREE_CODE (t))
545 /* Convert - (~A) to A + 1. */
547 if (INTEGRAL_TYPE_P (type))
548 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
549 build_int_cst (type, 1));
553 tem = fold_negate_const (t, type);
554 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
555 || !TYPE_OVERFLOW_TRAPS (type))
560 tem = fold_negate_const (t, type);
561 /* Two's complement FP formats, such as c4x, may overflow. */
562 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
567 tem = fold_negate_const (t, type);
572 tree rpart = negate_expr (TREE_REALPART (t));
573 tree ipart = negate_expr (TREE_IMAGPART (t));
575 if ((TREE_CODE (rpart) == REAL_CST
576 && TREE_CODE (ipart) == REAL_CST)
577 || (TREE_CODE (rpart) == INTEGER_CST
578 && TREE_CODE (ipart) == INTEGER_CST))
579 return build_complex (type, rpart, ipart);
584 if (negate_expr_p (t))
585 return fold_build2_loc (loc, COMPLEX_EXPR, type,
586 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
587 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
591 if (negate_expr_p (t))
592 return fold_build1_loc (loc, CONJ_EXPR, type,
593 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
597 return TREE_OPERAND (t, 0);
600 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
601 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
603 /* -(A + B) -> (-B) - A. */
604 if (negate_expr_p (TREE_OPERAND (t, 1))
605 && reorder_operands_p (TREE_OPERAND (t, 0),
606 TREE_OPERAND (t, 1)))
608 tem = negate_expr (TREE_OPERAND (t, 1));
609 return fold_build2_loc (loc, MINUS_EXPR, type,
610 tem, TREE_OPERAND (t, 0));
613 /* -(A + B) -> (-A) - B. */
614 if (negate_expr_p (TREE_OPERAND (t, 0)))
616 tem = negate_expr (TREE_OPERAND (t, 0));
617 return fold_build2_loc (loc, MINUS_EXPR, type,
618 tem, TREE_OPERAND (t, 1));
624 /* - (A - B) -> B - A */
625 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
626 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
627 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
628 return fold_build2_loc (loc, MINUS_EXPR, type,
629 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
633 if (TYPE_UNSIGNED (type))
639 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
641 tem = TREE_OPERAND (t, 1);
642 if (negate_expr_p (tem))
643 return fold_build2_loc (loc, TREE_CODE (t), type,
644 TREE_OPERAND (t, 0), negate_expr (tem));
645 tem = TREE_OPERAND (t, 0);
646 if (negate_expr_p (tem))
647 return fold_build2_loc (loc, TREE_CODE (t), type,
648 negate_expr (tem), TREE_OPERAND (t, 1));
657 /* In general we can't negate A / B, because if A is INT_MIN and
658 B is 1, we may turn this into INT_MIN / -1 which is undefined
659 and actually traps on some architectures. But if overflow is
660 undefined, we can negate, because - (INT_MIN / 1) is an
662 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
664 const char * const warnmsg = G_("assuming signed overflow does not "
665 "occur when negating a division");
666 tem = TREE_OPERAND (t, 1);
667 if (negate_expr_p (tem))
669 if (INTEGRAL_TYPE_P (type)
670 && (TREE_CODE (tem) != INTEGER_CST
671 || integer_onep (tem)))
672 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
673 return fold_build2_loc (loc, TREE_CODE (t), type,
674 TREE_OPERAND (t, 0), negate_expr (tem));
676 tem = TREE_OPERAND (t, 0);
677 if (negate_expr_p (tem))
679 if (INTEGRAL_TYPE_P (type)
680 && (TREE_CODE (tem) != INTEGER_CST
681 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683 return fold_build2_loc (loc, TREE_CODE (t), type,
684 negate_expr (tem), TREE_OPERAND (t, 1));
690 /* Convert -((double)float) into (double)(-float). */
691 if (TREE_CODE (type) == REAL_TYPE)
693 tem = strip_float_extensions (t);
694 if (tem != t && negate_expr_p (tem))
695 return fold_convert_loc (loc, type, negate_expr (tem));
700 /* Negate -f(x) as f(-x). */
701 if (negate_mathfn_p (builtin_mathfn_code (t))
702 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
706 fndecl = get_callee_fndecl (t);
707 arg = negate_expr (CALL_EXPR_ARG (t, 0));
708 return build_call_expr_loc (loc, fndecl, 1, arg);
713 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
714 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
716 tree op1 = TREE_OPERAND (t, 1);
717 if (TREE_INT_CST_HIGH (op1) == 0
718 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
719 == TREE_INT_CST_LOW (op1))
721 tree ntype = TYPE_UNSIGNED (type)
722 ? signed_type_for (type)
723 : unsigned_type_for (type);
724 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
725 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
726 return fold_convert_loc (loc, type, temp);
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
751 loc = EXPR_LOCATION (t);
752 type = TREE_TYPE (t);
755 tem = fold_negate_expr (loc, t);
757 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758 return fold_convert_loc (loc, type, tem);
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead.
776 If IN is itself a literal or constant, return it as appropriate.
778 Note that we do not guarantee that any of the three values will be the
779 same type as IN, but they will have the same signedness and mode. */
782 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
783 tree *minus_litp, int negate_p)
791 /* Strip any conversions that don't change the machine mode or signedness. */
792 STRIP_SIGN_NOPS (in);
794 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
795 || TREE_CODE (in) == FIXED_CST)
797 else if (TREE_CODE (in) == code
798 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
799 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
800 /* We can associate addition and subtraction together (even
801 though the C standard doesn't say so) for integers because
802 the value is not affected. For reals, the value might be
803 affected, so we can't. */
804 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
807 tree op0 = TREE_OPERAND (in, 0);
808 tree op1 = TREE_OPERAND (in, 1);
809 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
810 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
814 || TREE_CODE (op0) == FIXED_CST)
815 *litp = op0, op0 = 0;
816 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
817 || TREE_CODE (op1) == FIXED_CST)
818 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
820 if (op0 != 0 && TREE_CONSTANT (op0))
821 *conp = op0, op0 = 0;
822 else if (op1 != 0 && TREE_CONSTANT (op1))
823 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0 != 0 && op1 != 0)
832 var = op1, neg_var_p = neg1_p;
834 /* Now do any needed negations. */
836 *minus_litp = *litp, *litp = 0;
838 *conp = negate_expr (*conp);
840 var = negate_expr (var);
842 else if (TREE_CONSTANT (in))
850 *minus_litp = *litp, *litp = 0;
851 else if (*minus_litp)
852 *litp = *minus_litp, *minus_litp = 0;
853 *conp = negate_expr (*conp);
854 var = negate_expr (var);
860 /* Re-associate trees split by the above function. T1 and T2 are
861 either expressions to associate or null. Return the new
862 expression, if any. LOC is the location of the new expression. If
863 we build an operation, do it in TYPE and with CODE. */
866 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
873 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
874 try to fold this since we will have infinite recursion. But do
875 deal with any NEGATE_EXPRs. */
876 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
877 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
879 if (code == PLUS_EXPR)
881 if (TREE_CODE (t1) == NEGATE_EXPR)
882 return build2_loc (loc, MINUS_EXPR, type,
883 fold_convert_loc (loc, type, t2),
884 fold_convert_loc (loc, type,
885 TREE_OPERAND (t1, 0)));
886 else if (TREE_CODE (t2) == NEGATE_EXPR)
887 return build2_loc (loc, MINUS_EXPR, type,
888 fold_convert_loc (loc, type, t1),
889 fold_convert_loc (loc, type,
890 TREE_OPERAND (t2, 0)));
891 else if (integer_zerop (t2))
892 return fold_convert_loc (loc, type, t1);
894 else if (code == MINUS_EXPR)
896 if (integer_zerop (t2))
897 return fold_convert_loc (loc, type, t1);
900 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
901 fold_convert_loc (loc, type, t2));
904 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
905 fold_convert_loc (loc, type, t2));
908 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
909 for use in int_const_binop, size_binop and size_diffop. */
912 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
914 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
916 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
931 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
932 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
933 && TYPE_MODE (type1) == TYPE_MODE (type2);
937 /* Combine two integer constants ARG1 and ARG2 under operation CODE
938 to produce a new constant. Return NULL_TREE if we don't know how
939 to evaluate CODE at compile-time.
941 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
944 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
946 double_int op1, op2, res, tmp;
948 tree type = TREE_TYPE (arg1);
949 bool uns = TYPE_UNSIGNED (type);
951 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
952 bool overflow = false;
954 op1 = tree_to_double_int (arg1);
955 op2 = tree_to_double_int (arg2);
960 res = double_int_ior (op1, op2);
964 res = double_int_xor (op1, op2);
968 res = double_int_and (op1, op2);
972 res = double_int_rshift (op1, double_int_to_shwi (op2),
973 TYPE_PRECISION (type), !uns);
977 /* It's unclear from the C standard whether shifts can overflow.
978 The following code ignores overflow; perhaps a C standard
979 interpretation ruling is needed. */
980 res = double_int_lshift (op1, double_int_to_shwi (op2),
981 TYPE_PRECISION (type), !uns);
985 res = double_int_rrotate (op1, double_int_to_shwi (op2),
986 TYPE_PRECISION (type));
990 res = double_int_lrotate (op1, double_int_to_shwi (op2),
991 TYPE_PRECISION (type));
995 overflow = add_double (op1.low, op1.high, op2.low, op2.high,
996 &res.low, &res.high);
1000 neg_double (op2.low, op2.high, &res.low, &res.high);
1001 add_double (op1.low, op1.high, res.low, res.high,
1002 &res.low, &res.high);
1003 overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
1007 overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1008 &res.low, &res.high);
1011 case TRUNC_DIV_EXPR:
1012 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1013 case EXACT_DIV_EXPR:
1014 /* This is a shortcut for a common special case. */
1015 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1016 && !TREE_OVERFLOW (arg1)
1017 && !TREE_OVERFLOW (arg2)
1018 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1020 if (code == CEIL_DIV_EXPR)
1021 op1.low += op2.low - 1;
1023 res.low = op1.low / op2.low, res.high = 0;
1027 /* ... fall through ... */
1029 case ROUND_DIV_EXPR:
1030 if (double_int_zero_p (op2))
1032 if (double_int_one_p (op2))
1037 if (double_int_equal_p (op1, op2)
1038 && ! double_int_zero_p (op1))
1040 res = double_int_one;
1043 overflow = div_and_round_double (code, uns,
1044 op1.low, op1.high, op2.low, op2.high,
1045 &res.low, &res.high,
1046 &tmp.low, &tmp.high);
1049 case TRUNC_MOD_EXPR:
1050 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1051 /* This is a shortcut for a common special case. */
1052 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1053 && !TREE_OVERFLOW (arg1)
1054 && !TREE_OVERFLOW (arg2)
1055 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1057 if (code == CEIL_MOD_EXPR)
1058 op1.low += op2.low - 1;
1059 res.low = op1.low % op2.low, res.high = 0;
1063 /* ... fall through ... */
1065 case ROUND_MOD_EXPR:
1066 if (double_int_zero_p (op2))
1068 overflow = div_and_round_double (code, uns,
1069 op1.low, op1.high, op2.low, op2.high,
1070 &tmp.low, &tmp.high,
1071 &res.low, &res.high);
1075 res = double_int_min (op1, op2, uns);
1079 res = double_int_max (op1, op2, uns);
1088 t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1090 /* Propagate overflow flags ourselves. */
1091 if (((!uns || is_sizetype) && overflow)
1092 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1095 TREE_OVERFLOW (t) = 1;
1099 t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1100 ((!uns || is_sizetype) && overflow)
1101 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1106 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1107 constant. We assume ARG1 and ARG2 have the same data type, or at least
1108 are the same kind of constant and the same machine mode. Return zero if
1109 combining the constants is not allowed in the current operating mode. */
1112 const_binop (enum tree_code code, tree arg1, tree arg2)
1114 /* Sanity check for the recursive cases. */
1121 if (TREE_CODE (arg1) == INTEGER_CST)
1122 return int_const_binop (code, arg1, arg2, 0);
1124 if (TREE_CODE (arg1) == REAL_CST)
1126 enum machine_mode mode;
1129 REAL_VALUE_TYPE value;
1130 REAL_VALUE_TYPE result;
1134 /* The following codes are handled by real_arithmetic. */
1149 d1 = TREE_REAL_CST (arg1);
1150 d2 = TREE_REAL_CST (arg2);
1152 type = TREE_TYPE (arg1);
1153 mode = TYPE_MODE (type);
1155 /* Don't perform operation if we honor signaling NaNs and
1156 either operand is a NaN. */
1157 if (HONOR_SNANS (mode)
1158 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1161 /* Don't perform operation if it would raise a division
1162 by zero exception. */
1163 if (code == RDIV_EXPR
1164 && REAL_VALUES_EQUAL (d2, dconst0)
1165 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1168 /* If either operand is a NaN, just return it. Otherwise, set up
1169 for floating-point trap; we return an overflow. */
1170 if (REAL_VALUE_ISNAN (d1))
1172 else if (REAL_VALUE_ISNAN (d2))
1175 inexact = real_arithmetic (&value, code, &d1, &d2);
1176 real_convert (&result, mode, &value);
1178 /* Don't constant fold this floating point operation if
1179 the result has overflowed and flag_trapping_math. */
1180 if (flag_trapping_math
1181 && MODE_HAS_INFINITIES (mode)
1182 && REAL_VALUE_ISINF (result)
1183 && !REAL_VALUE_ISINF (d1)
1184 && !REAL_VALUE_ISINF (d2))
1187 /* Don't constant fold this floating point operation if the
1188 result may dependent upon the run-time rounding mode and
1189 flag_rounding_math is set, or if GCC's software emulation
1190 is unable to accurately represent the result. */
1191 if ((flag_rounding_math
1192 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1193 && (inexact || !real_identical (&result, &value)))
1196 t = build_real (type, result);
1198 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1202 if (TREE_CODE (arg1) == FIXED_CST)
1204 FIXED_VALUE_TYPE f1;
1205 FIXED_VALUE_TYPE f2;
1206 FIXED_VALUE_TYPE result;
1211 /* The following codes are handled by fixed_arithmetic. */
1217 case TRUNC_DIV_EXPR:
1218 f2 = TREE_FIXED_CST (arg2);
1223 f2.data.high = TREE_INT_CST_HIGH (arg2);
1224 f2.data.low = TREE_INT_CST_LOW (arg2);
1232 f1 = TREE_FIXED_CST (arg1);
1233 type = TREE_TYPE (arg1);
1234 sat_p = TYPE_SATURATING (type);
1235 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1236 t = build_fixed (type, result);
1237 /* Propagate overflow flags. */
1238 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1239 TREE_OVERFLOW (t) = 1;
1243 if (TREE_CODE (arg1) == COMPLEX_CST)
1245 tree type = TREE_TYPE (arg1);
1246 tree r1 = TREE_REALPART (arg1);
1247 tree i1 = TREE_IMAGPART (arg1);
1248 tree r2 = TREE_REALPART (arg2);
1249 tree i2 = TREE_IMAGPART (arg2);
1256 real = const_binop (code, r1, r2);
1257 imag = const_binop (code, i1, i2);
1261 if (COMPLEX_FLOAT_TYPE_P (type))
1262 return do_mpc_arg2 (arg1, arg2, type,
1263 /* do_nonfinite= */ folding_initializer,
1266 real = const_binop (MINUS_EXPR,
1267 const_binop (MULT_EXPR, r1, r2),
1268 const_binop (MULT_EXPR, i1, i2));
1269 imag = const_binop (PLUS_EXPR,
1270 const_binop (MULT_EXPR, r1, i2),
1271 const_binop (MULT_EXPR, i1, r2));
1275 if (COMPLEX_FLOAT_TYPE_P (type))
1276 return do_mpc_arg2 (arg1, arg2, type,
1277 /* do_nonfinite= */ folding_initializer,
1280 case TRUNC_DIV_EXPR:
1282 case FLOOR_DIV_EXPR:
1283 case ROUND_DIV_EXPR:
1284 if (flag_complex_method == 0)
1286 /* Keep this algorithm in sync with
1287 tree-complex.c:expand_complex_div_straight().
1289 Expand complex division to scalars, straightforward algorithm.
1290 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1294 = const_binop (PLUS_EXPR,
1295 const_binop (MULT_EXPR, r2, r2),
1296 const_binop (MULT_EXPR, i2, i2));
1298 = const_binop (PLUS_EXPR,
1299 const_binop (MULT_EXPR, r1, r2),
1300 const_binop (MULT_EXPR, i1, i2));
1302 = const_binop (MINUS_EXPR,
1303 const_binop (MULT_EXPR, i1, r2),
1304 const_binop (MULT_EXPR, r1, i2));
1306 real = const_binop (code, t1, magsquared);
1307 imag = const_binop (code, t2, magsquared);
1311 /* Keep this algorithm in sync with
1312 tree-complex.c:expand_complex_div_wide().
1314 Expand complex division to scalars, modified algorithm to minimize
1315 overflow with wide input ranges. */
1316 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1317 fold_abs_const (r2, TREE_TYPE (type)),
1318 fold_abs_const (i2, TREE_TYPE (type)));
1320 if (integer_nonzerop (compare))
1322 /* In the TRUE branch, we compute
1324 div = (br * ratio) + bi;
1325 tr = (ar * ratio) + ai;
1326 ti = (ai * ratio) - ar;
1329 tree ratio = const_binop (code, r2, i2);
1330 tree div = const_binop (PLUS_EXPR, i2,
1331 const_binop (MULT_EXPR, r2, ratio));
1332 real = const_binop (MULT_EXPR, r1, ratio);
1333 real = const_binop (PLUS_EXPR, real, i1);
1334 real = const_binop (code, real, div);
1336 imag = const_binop (MULT_EXPR, i1, ratio);
1337 imag = const_binop (MINUS_EXPR, imag, r1);
1338 imag = const_binop (code, imag, div);
1342 /* In the FALSE branch, we compute
1344 divisor = (d * ratio) + c;
1345 tr = (b * ratio) + a;
1346 ti = b - (a * ratio);
1349 tree ratio = const_binop (code, i2, r2);
1350 tree div = const_binop (PLUS_EXPR, r2,
1351 const_binop (MULT_EXPR, i2, ratio));
1353 real = const_binop (MULT_EXPR, i1, ratio);
1354 real = const_binop (PLUS_EXPR, real, r1);
1355 real = const_binop (code, real, div);
1357 imag = const_binop (MULT_EXPR, r1, ratio);
1358 imag = const_binop (MINUS_EXPR, i1, imag);
1359 imag = const_binop (code, imag, div);
1369 return build_complex (type, real, imag);
1372 if (TREE_CODE (arg1) == VECTOR_CST)
1374 tree type = TREE_TYPE(arg1);
1375 int count = TYPE_VECTOR_SUBPARTS (type), i;
1376 tree elements1, elements2, list = NULL_TREE;
1378 if(TREE_CODE(arg2) != VECTOR_CST)
1381 elements1 = TREE_VECTOR_CST_ELTS (arg1);
1382 elements2 = TREE_VECTOR_CST_ELTS (arg2);
1384 for (i = 0; i < count; i++)
1386 tree elem1, elem2, elem;
1388 /* The trailing elements can be empty and should be treated as 0 */
1390 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1393 elem1 = TREE_VALUE(elements1);
1394 elements1 = TREE_CHAIN (elements1);
1398 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1401 elem2 = TREE_VALUE(elements2);
1402 elements2 = TREE_CHAIN (elements2);
1405 elem = const_binop (code, elem1, elem2);
1407 /* It is possible that const_binop cannot handle the given
1408 code and return NULL_TREE */
1409 if(elem == NULL_TREE)
1412 list = tree_cons (NULL_TREE, elem, list);
1414 return build_vector(type, nreverse(list));
1419 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1420 indicates which particular sizetype to create. */
1423 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1425 return build_int_cst (sizetype_tab[(int) kind], number);
1428 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1429 is a tree code. The type of the result is taken from the operands.
1430 Both must be equivalent integer types, ala int_binop_types_match_p.
1431 If the operands are constant, so is the result. */
1434 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1436 tree type = TREE_TYPE (arg0);
1438 if (arg0 == error_mark_node || arg1 == error_mark_node)
1439 return error_mark_node;
1441 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1444 /* Handle the special case of two integer constants faster. */
1445 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1447 /* And some specific cases even faster than that. */
1448 if (code == PLUS_EXPR)
1450 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1452 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1455 else if (code == MINUS_EXPR)
1457 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1460 else if (code == MULT_EXPR)
1462 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1466 /* Handle general case of two integer constants. */
1467 return int_const_binop (code, arg0, arg1, 0);
1470 return fold_build2_loc (loc, code, type, arg0, arg1);
1473 /* Given two values, either both of sizetype or both of bitsizetype,
1474 compute the difference between the two values. Return the value
1475 in signed type corresponding to the type of the operands. */
1478 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1480 tree type = TREE_TYPE (arg0);
1483 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1486 /* If the type is already signed, just do the simple thing. */
1487 if (!TYPE_UNSIGNED (type))
1488 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1490 if (type == sizetype)
1492 else if (type == bitsizetype)
1493 ctype = sbitsizetype;
1495 ctype = signed_type_for (type);
1497 /* If either operand is not a constant, do the conversions to the signed
1498 type and subtract. The hardware will do the right thing with any
1499 overflow in the subtraction. */
1500 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1501 return size_binop_loc (loc, MINUS_EXPR,
1502 fold_convert_loc (loc, ctype, arg0),
1503 fold_convert_loc (loc, ctype, arg1));
1505 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1506 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1507 overflow) and negate (which can't either). Special-case a result
1508 of zero while we're here. */
1509 if (tree_int_cst_equal (arg0, arg1))
1510 return build_int_cst (ctype, 0);
1511 else if (tree_int_cst_lt (arg1, arg0))
1512 return fold_convert_loc (loc, ctype,
1513 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1515 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1516 fold_convert_loc (loc, ctype,
1517 size_binop_loc (loc,
1522 /* A subroutine of fold_convert_const handling conversions of an
1523 INTEGER_CST to another integer type. */
1526 fold_convert_const_int_from_int (tree type, const_tree arg1)
1530 /* Given an integer constant, make new constant with new type,
1531 appropriately sign-extended or truncated. */
1532 t = force_fit_type_double (type, tree_to_double_int (arg1),
1533 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1534 (TREE_INT_CST_HIGH (arg1) < 0
1535 && (TYPE_UNSIGNED (type)
1536 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1537 | TREE_OVERFLOW (arg1));
1542 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1543 to an integer type. */
1546 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1551 /* The following code implements the floating point to integer
1552 conversion rules required by the Java Language Specification,
1553 that IEEE NaNs are mapped to zero and values that overflow
1554 the target precision saturate, i.e. values greater than
1555 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1556 are mapped to INT_MIN. These semantics are allowed by the
1557 C and C++ standards that simply state that the behavior of
1558 FP-to-integer conversion is unspecified upon overflow. */
1562 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1566 case FIX_TRUNC_EXPR:
1567 real_trunc (&r, VOIDmode, &x);
1574 /* If R is NaN, return zero and show we have an overflow. */
1575 if (REAL_VALUE_ISNAN (r))
1578 val = double_int_zero;
1581 /* See if R is less than the lower bound or greater than the
1586 tree lt = TYPE_MIN_VALUE (type);
1587 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1588 if (REAL_VALUES_LESS (r, l))
1591 val = tree_to_double_int (lt);
1597 tree ut = TYPE_MAX_VALUE (type);
1600 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1601 if (REAL_VALUES_LESS (u, r))
1604 val = tree_to_double_int (ut);
1610 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1612 t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1616 /* A subroutine of fold_convert_const handling conversions of a
1617 FIXED_CST to an integer type. */
1620 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1623 double_int temp, temp_trunc;
1626 /* Right shift FIXED_CST to temp by fbit. */
1627 temp = TREE_FIXED_CST (arg1).data;
1628 mode = TREE_FIXED_CST (arg1).mode;
1629 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1631 temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1632 HOST_BITS_PER_DOUBLE_INT,
1633 SIGNED_FIXED_POINT_MODE_P (mode));
1635 /* Left shift temp to temp_trunc by fbit. */
1636 temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1637 HOST_BITS_PER_DOUBLE_INT,
1638 SIGNED_FIXED_POINT_MODE_P (mode));
1642 temp = double_int_zero;
1643 temp_trunc = double_int_zero;
1646 /* If FIXED_CST is negative, we need to round the value toward 0.
1647 By checking if the fractional bits are not zero to add 1 to temp. */
1648 if (SIGNED_FIXED_POINT_MODE_P (mode)
1649 && double_int_negative_p (temp_trunc)
1650 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1651 temp = double_int_add (temp, double_int_one);
1653 /* Given a fixed-point constant, make new constant with new type,
1654 appropriately sign-extended or truncated. */
1655 t = force_fit_type_double (type, temp, -1,
1656 (double_int_negative_p (temp)
1657 && (TYPE_UNSIGNED (type)
1658 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1659 | TREE_OVERFLOW (arg1));
1664 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1665 to another floating point type. */
1668 fold_convert_const_real_from_real (tree type, const_tree arg1)
1670 REAL_VALUE_TYPE value;
1673 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1674 t = build_real (type, value);
1676 /* If converting an infinity or NAN to a representation that doesn't
1677 have one, set the overflow bit so that we can produce some kind of
1678 error message at the appropriate point if necessary. It's not the
1679 most user-friendly message, but it's better than nothing. */
1680 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1681 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1682 TREE_OVERFLOW (t) = 1;
1683 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1684 && !MODE_HAS_NANS (TYPE_MODE (type)))
1685 TREE_OVERFLOW (t) = 1;
1686 /* Regular overflow, conversion produced an infinity in a mode that
1687 can't represent them. */
1688 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1689 && REAL_VALUE_ISINF (value)
1690 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1691 TREE_OVERFLOW (t) = 1;
1693 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1697 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1698 to a floating point type. */
1701 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1703 REAL_VALUE_TYPE value;
1706 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1707 t = build_real (type, value);
1709 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1713 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1714 to another fixed-point type. */
1717 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1719 FIXED_VALUE_TYPE value;
1723 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1724 TYPE_SATURATING (type));
1725 t = build_fixed (type, value);
1727 /* Propagate overflow flags. */
1728 if (overflow_p | TREE_OVERFLOW (arg1))
1729 TREE_OVERFLOW (t) = 1;
1733 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1734 to a fixed-point type. */
1737 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1739 FIXED_VALUE_TYPE value;
1743 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1744 TREE_INT_CST (arg1),
1745 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1746 TYPE_SATURATING (type));
1747 t = build_fixed (type, value);
1749 /* Propagate overflow flags. */
1750 if (overflow_p | TREE_OVERFLOW (arg1))
1751 TREE_OVERFLOW (t) = 1;
1755 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1756 to a fixed-point type. */
1759 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1761 FIXED_VALUE_TYPE value;
1765 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1766 &TREE_REAL_CST (arg1),
1767 TYPE_SATURATING (type));
1768 t = build_fixed (type, value);
1770 /* Propagate overflow flags. */
1771 if (overflow_p | TREE_OVERFLOW (arg1))
1772 TREE_OVERFLOW (t) = 1;
1776 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1777 type TYPE. If no simplification can be done return NULL_TREE. */
1780 fold_convert_const (enum tree_code code, tree type, tree arg1)
1782 if (TREE_TYPE (arg1) == type)
1785 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1786 || TREE_CODE (type) == OFFSET_TYPE)
1788 if (TREE_CODE (arg1) == INTEGER_CST)
1789 return fold_convert_const_int_from_int (type, arg1);
1790 else if (TREE_CODE (arg1) == REAL_CST)
1791 return fold_convert_const_int_from_real (code, type, arg1);
1792 else if (TREE_CODE (arg1) == FIXED_CST)
1793 return fold_convert_const_int_from_fixed (type, arg1);
1795 else if (TREE_CODE (type) == REAL_TYPE)
1797 if (TREE_CODE (arg1) == INTEGER_CST)
1798 return build_real_from_int_cst (type, arg1);
1799 else if (TREE_CODE (arg1) == REAL_CST)
1800 return fold_convert_const_real_from_real (type, arg1);
1801 else if (TREE_CODE (arg1) == FIXED_CST)
1802 return fold_convert_const_real_from_fixed (type, arg1);
1804 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1806 if (TREE_CODE (arg1) == FIXED_CST)
1807 return fold_convert_const_fixed_from_fixed (type, arg1);
1808 else if (TREE_CODE (arg1) == INTEGER_CST)
1809 return fold_convert_const_fixed_from_int (type, arg1);
1810 else if (TREE_CODE (arg1) == REAL_CST)
1811 return fold_convert_const_fixed_from_real (type, arg1);
1816 /* Construct a vector of zero elements of vector type TYPE. */
1819 build_zero_vector (tree type)
1823 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1824 return build_vector_from_val (type, t);
1827 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1830 fold_convertible_p (const_tree type, const_tree arg)
1832 tree orig = TREE_TYPE (arg);
1837 if (TREE_CODE (arg) == ERROR_MARK
1838 || TREE_CODE (type) == ERROR_MARK
1839 || TREE_CODE (orig) == ERROR_MARK)
1842 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1845 switch (TREE_CODE (type))
1847 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1848 case POINTER_TYPE: case REFERENCE_TYPE:
1850 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1851 || TREE_CODE (orig) == OFFSET_TYPE)
1853 return (TREE_CODE (orig) == VECTOR_TYPE
1854 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1857 case FIXED_POINT_TYPE:
1861 return TREE_CODE (type) == TREE_CODE (orig);
1868 /* Convert expression ARG to type TYPE. Used by the middle-end for
1869 simple conversions in preference to calling the front-end's convert. */
1872 fold_convert_loc (location_t loc, tree type, tree arg)
1874 tree orig = TREE_TYPE (arg);
1880 if (TREE_CODE (arg) == ERROR_MARK
1881 || TREE_CODE (type) == ERROR_MARK
1882 || TREE_CODE (orig) == ERROR_MARK)
1883 return error_mark_node;
1885 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1886 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1888 switch (TREE_CODE (type))
1891 case REFERENCE_TYPE:
1892 /* Handle conversions between pointers to different address spaces. */
1893 if (POINTER_TYPE_P (orig)
1894 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1895 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1896 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1899 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1901 if (TREE_CODE (arg) == INTEGER_CST)
1903 tem = fold_convert_const (NOP_EXPR, type, arg);
1904 if (tem != NULL_TREE)
1907 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1908 || TREE_CODE (orig) == OFFSET_TYPE)
1909 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1910 if (TREE_CODE (orig) == COMPLEX_TYPE)
1911 return fold_convert_loc (loc, type,
1912 fold_build1_loc (loc, REALPART_EXPR,
1913 TREE_TYPE (orig), arg));
1914 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1915 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1916 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1919 if (TREE_CODE (arg) == INTEGER_CST)
1921 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1922 if (tem != NULL_TREE)
1925 else if (TREE_CODE (arg) == REAL_CST)
1927 tem = fold_convert_const (NOP_EXPR, type, arg);
1928 if (tem != NULL_TREE)
1931 else if (TREE_CODE (arg) == FIXED_CST)
1933 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1934 if (tem != NULL_TREE)
1938 switch (TREE_CODE (orig))
1941 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1942 case POINTER_TYPE: case REFERENCE_TYPE:
1943 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1946 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1948 case FIXED_POINT_TYPE:
1949 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1952 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1953 return fold_convert_loc (loc, type, tem);
1959 case FIXED_POINT_TYPE:
1960 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1961 || TREE_CODE (arg) == REAL_CST)
1963 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1964 if (tem != NULL_TREE)
1965 goto fold_convert_exit;
1968 switch (TREE_CODE (orig))
1970 case FIXED_POINT_TYPE:
1975 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1978 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1979 return fold_convert_loc (loc, type, tem);
1986 switch (TREE_CODE (orig))
1989 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1990 case POINTER_TYPE: case REFERENCE_TYPE:
1992 case FIXED_POINT_TYPE:
1993 return fold_build2_loc (loc, COMPLEX_EXPR, type,
1994 fold_convert_loc (loc, TREE_TYPE (type), arg),
1995 fold_convert_loc (loc, TREE_TYPE (type),
1996 integer_zero_node));
2001 if (TREE_CODE (arg) == COMPLEX_EXPR)
2003 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2004 TREE_OPERAND (arg, 0));
2005 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2006 TREE_OPERAND (arg, 1));
2007 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2010 arg = save_expr (arg);
2011 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2012 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2013 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2014 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2015 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2023 if (integer_zerop (arg))
2024 return build_zero_vector (type);
2025 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2026 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2027 || TREE_CODE (orig) == VECTOR_TYPE);
2028 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2031 tem = fold_ignored_result (arg);
2032 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2038 protected_set_expr_location_unshare (tem, loc);
2042 /* Return false if expr can be assumed not to be an lvalue, true
2046 maybe_lvalue_p (const_tree x)
2048 /* We only need to wrap lvalue tree codes. */
2049 switch (TREE_CODE (x))
2062 case ARRAY_RANGE_REF:
2068 case PREINCREMENT_EXPR:
2069 case PREDECREMENT_EXPR:
2071 case TRY_CATCH_EXPR:
2072 case WITH_CLEANUP_EXPR:
2081 /* Assume the worst for front-end tree codes. */
2082 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2090 /* Return an expr equal to X but certainly not valid as an lvalue. */
2093 non_lvalue_loc (location_t loc, tree x)
2095 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2100 if (! maybe_lvalue_p (x))
2102 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2105 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2106 Zero means allow extended lvalues. */
2108 int pedantic_lvalues;
2110 /* When pedantic, return an expr equal to X but certainly not valid as a
2111 pedantic lvalue. Otherwise, return X. */
2114 pedantic_non_lvalue_loc (location_t loc, tree x)
2116 if (pedantic_lvalues)
2117 return non_lvalue_loc (loc, x);
2119 return protected_set_expr_location_unshare (x, loc);
2122 /* Given a tree comparison code, return the code that is the logical inverse
2123 of the given code. It is not safe to do this for floating-point
2124 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2125 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2128 invert_tree_comparison (enum tree_code code, bool honor_nans)
2130 if (honor_nans && flag_trapping_math)
2140 return honor_nans ? UNLE_EXPR : LE_EXPR;
2142 return honor_nans ? UNLT_EXPR : LT_EXPR;
2144 return honor_nans ? UNGE_EXPR : GE_EXPR;
2146 return honor_nans ? UNGT_EXPR : GT_EXPR;
2160 return UNORDERED_EXPR;
2161 case UNORDERED_EXPR:
2162 return ORDERED_EXPR;
2168 /* Similar, but return the comparison that results if the operands are
2169 swapped. This is safe for floating-point. */
2172 swap_tree_comparison (enum tree_code code)
2179 case UNORDERED_EXPR:
2205 /* Convert a comparison tree code from an enum tree_code representation
2206 into a compcode bit-based encoding. This function is the inverse of
2207 compcode_to_comparison. */
2209 static enum comparison_code
2210 comparison_to_compcode (enum tree_code code)
2227 return COMPCODE_ORD;
2228 case UNORDERED_EXPR:
2229 return COMPCODE_UNORD;
2231 return COMPCODE_UNLT;
2233 return COMPCODE_UNEQ;
2235 return COMPCODE_UNLE;
2237 return COMPCODE_UNGT;
2239 return COMPCODE_LTGT;
2241 return COMPCODE_UNGE;
2247 /* Convert a compcode bit-based encoding of a comparison operator back
2248 to GCC's enum tree_code representation. This function is the
2249 inverse of comparison_to_compcode. */
2251 static enum tree_code
2252 compcode_to_comparison (enum comparison_code code)
2269 return ORDERED_EXPR;
2270 case COMPCODE_UNORD:
2271 return UNORDERED_EXPR;
2289 /* Return a tree for the comparison which is the combination of
2290 doing the AND or OR (depending on CODE) of the two operations LCODE
2291 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2292 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2293 if this makes the transformation invalid. */
2296 combine_comparisons (location_t loc,
2297 enum tree_code code, enum tree_code lcode,
2298 enum tree_code rcode, tree truth_type,
2299 tree ll_arg, tree lr_arg)
2301 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2302 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2303 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2308 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2309 compcode = lcompcode & rcompcode;
2312 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2313 compcode = lcompcode | rcompcode;
2322 /* Eliminate unordered comparisons, as well as LTGT and ORD
2323 which are not used unless the mode has NaNs. */
2324 compcode &= ~COMPCODE_UNORD;
2325 if (compcode == COMPCODE_LTGT)
2326 compcode = COMPCODE_NE;
2327 else if (compcode == COMPCODE_ORD)
2328 compcode = COMPCODE_TRUE;
2330 else if (flag_trapping_math)
2332 /* Check that the original operation and the optimized ones will trap
2333 under the same condition. */
2334 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2335 && (lcompcode != COMPCODE_EQ)
2336 && (lcompcode != COMPCODE_ORD);
2337 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2338 && (rcompcode != COMPCODE_EQ)
2339 && (rcompcode != COMPCODE_ORD);
2340 bool trap = (compcode & COMPCODE_UNORD) == 0
2341 && (compcode != COMPCODE_EQ)
2342 && (compcode != COMPCODE_ORD);
2344 /* In a short-circuited boolean expression the LHS might be
2345 such that the RHS, if evaluated, will never trap. For
2346 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2347 if neither x nor y is NaN. (This is a mixed blessing: for
2348 example, the expression above will never trap, hence
2349 optimizing it to x < y would be invalid). */
2350 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2351 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2354 /* If the comparison was short-circuited, and only the RHS
2355 trapped, we may now generate a spurious trap. */
2357 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2360 /* If we changed the conditions that cause a trap, we lose. */
2361 if ((ltrap || rtrap) != trap)
2365 if (compcode == COMPCODE_TRUE)
2366 return constant_boolean_node (true, truth_type);
2367 else if (compcode == COMPCODE_FALSE)
2368 return constant_boolean_node (false, truth_type);
2371 enum tree_code tcode;
2373 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2374 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2378 /* Return nonzero if two operands (typically of the same tree node)
2379 are necessarily equal. If either argument has side-effects this
2380 function returns zero. FLAGS modifies behavior as follows:
2382 If OEP_ONLY_CONST is set, only return nonzero for constants.
2383 This function tests whether the operands are indistinguishable;
2384 it does not test whether they are equal using C's == operation.
2385 The distinction is important for IEEE floating point, because
2386 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2387 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2389 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2390 even though it may hold multiple values during a function.
2391 This is because a GCC tree node guarantees that nothing else is
2392 executed between the evaluation of its "operands" (which may often
2393 be evaluated in arbitrary order). Hence if the operands themselves
2394 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2395 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2396 unset means assuming isochronic (or instantaneous) tree equivalence.
2397 Unless comparing arbitrary expression trees, such as from different
2398 statements, this flag can usually be left unset.
2400 If OEP_PURE_SAME is set, then pure functions with identical arguments
2401 are considered the same. It is used when the caller has other ways
2402 to ensure that global memory is unchanged in between. */
2405 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2407 /* If either is ERROR_MARK, they aren't equal. */
2408 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2409 || TREE_TYPE (arg0) == error_mark_node
2410 || TREE_TYPE (arg1) == error_mark_node)
2413 /* Similar, if either does not have a type (like a released SSA name),
2414 they aren't equal. */
2415 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2418 /* Check equality of integer constants before bailing out due to
2419 precision differences. */
2420 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2421 return tree_int_cst_equal (arg0, arg1);
2423 /* If both types don't have the same signedness, then we can't consider
2424 them equal. We must check this before the STRIP_NOPS calls
2425 because they may change the signedness of the arguments. As pointers
2426 strictly don't have a signedness, require either two pointers or
2427 two non-pointers as well. */
2428 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2429 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2432 /* We cannot consider pointers to different address space equal. */
2433 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2434 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2435 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2438 /* If both types don't have the same precision, then it is not safe
2440 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2446 /* In case both args are comparisons but with different comparison
2447 code, try to swap the comparison operands of one arg to produce
2448 a match and compare that variant. */
2449 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2450 && COMPARISON_CLASS_P (arg0)
2451 && COMPARISON_CLASS_P (arg1))
2453 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2455 if (TREE_CODE (arg0) == swap_code)
2456 return operand_equal_p (TREE_OPERAND (arg0, 0),
2457 TREE_OPERAND (arg1, 1), flags)
2458 && operand_equal_p (TREE_OPERAND (arg0, 1),
2459 TREE_OPERAND (arg1, 0), flags);
2462 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2463 /* This is needed for conversions and for COMPONENT_REF.
2464 Might as well play it safe and always test this. */
2465 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2466 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2467 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2470 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2471 We don't care about side effects in that case because the SAVE_EXPR
2472 takes care of that for us. In all other cases, two expressions are
2473 equal if they have no side effects. If we have two identical
2474 expressions with side effects that should be treated the same due
2475 to the only side effects being identical SAVE_EXPR's, that will
2476 be detected in the recursive calls below. */
2477 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2478 && (TREE_CODE (arg0) == SAVE_EXPR
2479 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2482 /* Next handle constant cases, those for which we can return 1 even
2483 if ONLY_CONST is set. */
2484 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2485 switch (TREE_CODE (arg0))
2488 return tree_int_cst_equal (arg0, arg1);
2491 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2492 TREE_FIXED_CST (arg1));
2495 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2496 TREE_REAL_CST (arg1)))
2500 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2502 /* If we do not distinguish between signed and unsigned zero,
2503 consider them equal. */
2504 if (real_zerop (arg0) && real_zerop (arg1))
2513 v1 = TREE_VECTOR_CST_ELTS (arg0);
2514 v2 = TREE_VECTOR_CST_ELTS (arg1);
2517 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2520 v1 = TREE_CHAIN (v1);
2521 v2 = TREE_CHAIN (v2);
2528 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2530 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2534 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2535 && ! memcmp (TREE_STRING_POINTER (arg0),
2536 TREE_STRING_POINTER (arg1),
2537 TREE_STRING_LENGTH (arg0)));
2540 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2546 if (flags & OEP_ONLY_CONST)
2549 /* Define macros to test an operand from arg0 and arg1 for equality and a
2550 variant that allows null and views null as being different from any
2551 non-null value. In the latter case, if either is null, the both
2552 must be; otherwise, do the normal comparison. */
2553 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2554 TREE_OPERAND (arg1, N), flags)
2556 #define OP_SAME_WITH_NULL(N) \
2557 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2558 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2560 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2563 /* Two conversions are equal only if signedness and modes match. */
2564 switch (TREE_CODE (arg0))
2567 case FIX_TRUNC_EXPR:
2568 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2569 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2579 case tcc_comparison:
2581 if (OP_SAME (0) && OP_SAME (1))
2584 /* For commutative ops, allow the other order. */
2585 return (commutative_tree_code (TREE_CODE (arg0))
2586 && operand_equal_p (TREE_OPERAND (arg0, 0),
2587 TREE_OPERAND (arg1, 1), flags)
2588 && operand_equal_p (TREE_OPERAND (arg0, 1),
2589 TREE_OPERAND (arg1, 0), flags));
2592 /* If either of the pointer (or reference) expressions we are
2593 dereferencing contain a side effect, these cannot be equal. */
2594 if (TREE_SIDE_EFFECTS (arg0)
2595 || TREE_SIDE_EFFECTS (arg1))
2598 switch (TREE_CODE (arg0))
2606 /* Require equal access sizes, and similar pointer types.
2607 We can have incomplete types for array references of
2608 variable-sized arrays from the Fortran frontent
2610 return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2611 || (TYPE_SIZE (TREE_TYPE (arg0))
2612 && TYPE_SIZE (TREE_TYPE (arg1))
2613 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2614 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2615 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2616 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2617 && OP_SAME (0) && OP_SAME (1));
2620 case ARRAY_RANGE_REF:
2621 /* Operands 2 and 3 may be null.
2622 Compare the array index by value if it is constant first as we
2623 may have different types but same value here. */
2625 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2626 TREE_OPERAND (arg1, 1))
2628 && OP_SAME_WITH_NULL (2)
2629 && OP_SAME_WITH_NULL (3));
2632 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2633 may be NULL when we're called to compare MEM_EXPRs. */
2634 return OP_SAME_WITH_NULL (0)
2636 && OP_SAME_WITH_NULL (2);
2639 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2645 case tcc_expression:
2646 switch (TREE_CODE (arg0))
2649 case TRUTH_NOT_EXPR:
2652 case TRUTH_ANDIF_EXPR:
2653 case TRUTH_ORIF_EXPR:
2654 return OP_SAME (0) && OP_SAME (1);
2657 case WIDEN_MULT_PLUS_EXPR:
2658 case WIDEN_MULT_MINUS_EXPR:
2661 /* The multiplcation operands are commutative. */
2664 case TRUTH_AND_EXPR:
2666 case TRUTH_XOR_EXPR:
2667 if (OP_SAME (0) && OP_SAME (1))
2670 /* Otherwise take into account this is a commutative operation. */
2671 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2672 TREE_OPERAND (arg1, 1), flags)
2673 && operand_equal_p (TREE_OPERAND (arg0, 1),
2674 TREE_OPERAND (arg1, 0), flags));
2679 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2686 switch (TREE_CODE (arg0))
2689 /* If the CALL_EXPRs call different functions, then they
2690 clearly can not be equal. */
2691 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2696 unsigned int cef = call_expr_flags (arg0);
2697 if (flags & OEP_PURE_SAME)
2698 cef &= ECF_CONST | ECF_PURE;
2705 /* Now see if all the arguments are the same. */
2707 const_call_expr_arg_iterator iter0, iter1;
2709 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2710 a1 = first_const_call_expr_arg (arg1, &iter1);
2712 a0 = next_const_call_expr_arg (&iter0),
2713 a1 = next_const_call_expr_arg (&iter1))
2714 if (! operand_equal_p (a0, a1, flags))
2717 /* If we get here and both argument lists are exhausted
2718 then the CALL_EXPRs are equal. */
2719 return ! (a0 || a1);
2725 case tcc_declaration:
2726 /* Consider __builtin_sqrt equal to sqrt. */
2727 return (TREE_CODE (arg0) == FUNCTION_DECL
2728 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2729 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2730 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2737 #undef OP_SAME_WITH_NULL
2740 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2741 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2743 When in doubt, return 0. */
2746 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2748 int unsignedp1, unsignedpo;
2749 tree primarg0, primarg1, primother;
2750 unsigned int correct_width;
2752 if (operand_equal_p (arg0, arg1, 0))
2755 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2756 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2759 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2760 and see if the inner values are the same. This removes any
2761 signedness comparison, which doesn't matter here. */
2762 primarg0 = arg0, primarg1 = arg1;
2763 STRIP_NOPS (primarg0);
2764 STRIP_NOPS (primarg1);
2765 if (operand_equal_p (primarg0, primarg1, 0))
2768 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2769 actual comparison operand, ARG0.
2771 First throw away any conversions to wider types
2772 already present in the operands. */
2774 primarg1 = get_narrower (arg1, &unsignedp1);
2775 primother = get_narrower (other, &unsignedpo);
2777 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2778 if (unsignedp1 == unsignedpo
2779 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2780 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2782 tree type = TREE_TYPE (arg0);
2784 /* Make sure shorter operand is extended the right way
2785 to match the longer operand. */
2786 primarg1 = fold_convert (signed_or_unsigned_type_for
2787 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2789 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2796 /* See if ARG is an expression that is either a comparison or is performing
2797 arithmetic on comparisons. The comparisons must only be comparing
2798 two different values, which will be stored in *CVAL1 and *CVAL2; if
2799 they are nonzero it means that some operands have already been found.
2800 No variables may be used anywhere else in the expression except in the
2801 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2802 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2804 If this is true, return 1. Otherwise, return zero. */
2807 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2809 enum tree_code code = TREE_CODE (arg);
2810 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2812 /* We can handle some of the tcc_expression cases here. */
2813 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2815 else if (tclass == tcc_expression
2816 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2817 || code == COMPOUND_EXPR))
2818 tclass = tcc_binary;
2820 else if (tclass == tcc_expression && code == SAVE_EXPR
2821 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2823 /* If we've already found a CVAL1 or CVAL2, this expression is
2824 two complex to handle. */
2825 if (*cval1 || *cval2)
2835 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2838 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2839 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2840 cval1, cval2, save_p));
2845 case tcc_expression:
2846 if (code == COND_EXPR)
2847 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2848 cval1, cval2, save_p)
2849 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2850 cval1, cval2, save_p)
2851 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2852 cval1, cval2, save_p));
2855 case tcc_comparison:
2856 /* First see if we can handle the first operand, then the second. For
2857 the second operand, we know *CVAL1 can't be zero. It must be that
2858 one side of the comparison is each of the values; test for the
2859 case where this isn't true by failing if the two operands
2862 if (operand_equal_p (TREE_OPERAND (arg, 0),
2863 TREE_OPERAND (arg, 1), 0))
2867 *cval1 = TREE_OPERAND (arg, 0);
2868 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2870 else if (*cval2 == 0)
2871 *cval2 = TREE_OPERAND (arg, 0);
2872 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2877 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2879 else if (*cval2 == 0)
2880 *cval2 = TREE_OPERAND (arg, 1);
2881 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2893 /* ARG is a tree that is known to contain just arithmetic operations and
2894 comparisons. Evaluate the operations in the tree substituting NEW0 for
2895 any occurrence of OLD0 as an operand of a comparison and likewise for
2899 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2900 tree old1, tree new1)
2902 tree type = TREE_TYPE (arg);
2903 enum tree_code code = TREE_CODE (arg);
2904 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2906 /* We can handle some of the tcc_expression cases here. */
2907 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2909 else if (tclass == tcc_expression
2910 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2911 tclass = tcc_binary;
2916 return fold_build1_loc (loc, code, type,
2917 eval_subst (loc, TREE_OPERAND (arg, 0),
2918 old0, new0, old1, new1));
2921 return fold_build2_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));
2927 case tcc_expression:
2931 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2935 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2939 return fold_build3_loc (loc, code, type,
2940 eval_subst (loc, TREE_OPERAND (arg, 0),
2941 old0, new0, old1, new1),
2942 eval_subst (loc, TREE_OPERAND (arg, 1),
2943 old0, new0, old1, new1),
2944 eval_subst (loc, TREE_OPERAND (arg, 2),
2945 old0, new0, old1, new1));
2949 /* Fall through - ??? */
2951 case tcc_comparison:
2953 tree arg0 = TREE_OPERAND (arg, 0);
2954 tree arg1 = TREE_OPERAND (arg, 1);
2956 /* We need to check both for exact equality and tree equality. The
2957 former will be true if the operand has a side-effect. In that
2958 case, we know the operand occurred exactly once. */
2960 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2962 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2965 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2967 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2970 return fold_build2_loc (loc, code, type, arg0, arg1);
2978 /* Return a tree for the case when the result of an expression is RESULT
2979 converted to TYPE and OMITTED was previously an operand of the expression
2980 but is now not needed (e.g., we folded OMITTED * 0).
2982 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2983 the conversion of RESULT to TYPE. */
2986 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2988 tree t = fold_convert_loc (loc, type, result);
2990 /* If the resulting operand is an empty statement, just return the omitted
2991 statement casted to void. */
2992 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2993 return build1_loc (loc, NOP_EXPR, void_type_node,
2994 fold_ignored_result (omitted));
2996 if (TREE_SIDE_EFFECTS (omitted))
2997 return build2_loc (loc, COMPOUND_EXPR, type,
2998 fold_ignored_result (omitted), t);
3000 return non_lvalue_loc (loc, t);
3003 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3006 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3009 tree t = fold_convert_loc (loc, type, result);
3011 /* If the resulting operand is an empty statement, just return the omitted
3012 statement casted to void. */
3013 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3014 return build1_loc (loc, NOP_EXPR, void_type_node,
3015 fold_ignored_result (omitted));
3017 if (TREE_SIDE_EFFECTS (omitted))
3018 return build2_loc (loc, COMPOUND_EXPR, type,
3019 fold_ignored_result (omitted), t);
3021 return pedantic_non_lvalue_loc (loc, t);
3024 /* Return a tree for the case when the result of an expression is RESULT
3025 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3026 of the expression but are now not needed.
3028 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3029 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3030 evaluated before OMITTED2. Otherwise, if neither has side effects,
3031 just do the conversion of RESULT to TYPE. */
3034 omit_two_operands_loc (location_t loc, tree type, tree result,
3035 tree omitted1, tree omitted2)
3037 tree t = fold_convert_loc (loc, type, result);
3039 if (TREE_SIDE_EFFECTS (omitted2))
3040 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3041 if (TREE_SIDE_EFFECTS (omitted1))
3042 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3044 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3048 /* Return a simplified tree node for the truth-negation of ARG. This
3049 never alters ARG itself. We assume that ARG is an operation that
3050 returns a truth value (0 or 1).
3052 FIXME: one would think we would fold the result, but it causes
3053 problems with the dominator optimizer. */
3056 fold_truth_not_expr (location_t loc, tree arg)
3058 tree type = TREE_TYPE (arg);
3059 enum tree_code code = TREE_CODE (arg);
3060 location_t loc1, loc2;
3062 /* If this is a comparison, we can simply invert it, except for
3063 floating-point non-equality comparisons, in which case we just
3064 enclose a TRUTH_NOT_EXPR around what we have. */
3066 if (TREE_CODE_CLASS (code) == tcc_comparison)
3068 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3069 if (FLOAT_TYPE_P (op_type)
3070 && flag_trapping_math
3071 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3072 && code != NE_EXPR && code != EQ_EXPR)
3075 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3076 if (code == ERROR_MARK)
3079 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3080 TREE_OPERAND (arg, 1));
3086 return constant_boolean_node (integer_zerop (arg), type);
3088 case TRUTH_AND_EXPR:
3089 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3090 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3091 return build2_loc (loc, TRUTH_OR_EXPR, type,
3092 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3093 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3096 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3097 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3098 return build2_loc (loc, TRUTH_AND_EXPR, type,
3099 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3100 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3102 case TRUTH_XOR_EXPR:
3103 /* Here we can invert either operand. We invert the first operand
3104 unless the second operand is a TRUTH_NOT_EXPR in which case our
3105 result is the XOR of the first operand with the inside of the
3106 negation of the second operand. */
3108 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3109 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3110 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3112 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3113 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3114 TREE_OPERAND (arg, 1));
3116 case TRUTH_ANDIF_EXPR:
3117 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3118 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3119 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3120 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3121 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3123 case TRUTH_ORIF_EXPR:
3124 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3125 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3126 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3127 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3128 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3130 case TRUTH_NOT_EXPR:
3131 return TREE_OPERAND (arg, 0);
3135 tree arg1 = TREE_OPERAND (arg, 1);
3136 tree arg2 = TREE_OPERAND (arg, 2);
3138 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3139 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3141 /* A COND_EXPR may have a throw as one operand, which
3142 then has void type. Just leave void operands
3144 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3145 VOID_TYPE_P (TREE_TYPE (arg1))
3146 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3147 VOID_TYPE_P (TREE_TYPE (arg2))
3148 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3152 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3153 return build2_loc (loc, COMPOUND_EXPR, type,
3154 TREE_OPERAND (arg, 0),
3155 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3157 case NON_LVALUE_EXPR:
3158 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3159 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3162 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3163 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3165 /* ... fall through ... */
3168 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3169 return build1_loc (loc, TREE_CODE (arg), type,
3170 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3173 if (!integer_onep (TREE_OPERAND (arg, 1)))
3175 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3178 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3180 case CLEANUP_POINT_EXPR:
3181 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3182 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3183 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3190 /* Return a simplified tree node for the truth-negation of ARG. This
3191 never alters ARG itself. We assume that ARG is an operation that
3192 returns a truth value (0 or 1).
3194 FIXME: one would think we would fold the result, but it causes
3195 problems with the dominator optimizer. */
3198 invert_truthvalue_loc (location_t loc, tree arg)
3202 if (TREE_CODE (arg) == ERROR_MARK)
3205 tem = fold_truth_not_expr (loc, arg);
3207 tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3212 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3213 operands are another bit-wise operation with a common input. If so,
3214 distribute the bit operations to save an operation and possibly two if
3215 constants are involved. For example, convert
3216 (A | B) & (A | C) into A | (B & C)
3217 Further simplification will occur if B and C are constants.
3219 If this optimization cannot be done, 0 will be returned. */
3222 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3223 tree arg0, tree arg1)
3228 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3229 || TREE_CODE (arg0) == code
3230 || (TREE_CODE (arg0) != BIT_AND_EXPR
3231 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3234 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3236 common = TREE_OPERAND (arg0, 0);
3237 left = TREE_OPERAND (arg0, 1);
3238 right = TREE_OPERAND (arg1, 1);
3240 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3242 common = TREE_OPERAND (arg0, 0);
3243 left = TREE_OPERAND (arg0, 1);
3244 right = TREE_OPERAND (arg1, 0);
3246 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3248 common = TREE_OPERAND (arg0, 1);
3249 left = TREE_OPERAND (arg0, 0);
3250 right = TREE_OPERAND (arg1, 1);
3252 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3254 common = TREE_OPERAND (arg0, 1);
3255 left = TREE_OPERAND (arg0, 0);
3256 right = TREE_OPERAND (arg1, 0);
3261 common = fold_convert_loc (loc, type, common);
3262 left = fold_convert_loc (loc, type, left);
3263 right = fold_convert_loc (loc, type, right);
3264 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3265 fold_build2_loc (loc, code, type, left, right));
3268 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3269 with code CODE. This optimization is unsafe. */
3271 distribute_real_division (location_t loc, enum tree_code code, tree type,
3272 tree arg0, tree arg1)
3274 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3275 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3277 /* (A / C) +- (B / C) -> (A +- B) / C. */
3279 && operand_equal_p (TREE_OPERAND (arg0, 1),
3280 TREE_OPERAND (arg1, 1), 0))
3281 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3282 fold_build2_loc (loc, code, type,
3283 TREE_OPERAND (arg0, 0),
3284 TREE_OPERAND (arg1, 0)),
3285 TREE_OPERAND (arg0, 1));
3287 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3288 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3289 TREE_OPERAND (arg1, 0), 0)
3290 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3291 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3293 REAL_VALUE_TYPE r0, r1;
3294 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3295 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3297 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3299 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3300 real_arithmetic (&r0, code, &r0, &r1);
3301 return fold_build2_loc (loc, MULT_EXPR, type,
3302 TREE_OPERAND (arg0, 0),
3303 build_real (type, r0));
3309 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3310 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3313 make_bit_field_ref (location_t loc, tree inner, tree type,
3314 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3316 tree result, bftype;
3320 tree size = TYPE_SIZE (TREE_TYPE (inner));
3321 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3322 || POINTER_TYPE_P (TREE_TYPE (inner)))
3323 && host_integerp (size, 0)
3324 && tree_low_cst (size, 0) == bitsize)
3325 return fold_convert_loc (loc, type, inner);
3329 if (TYPE_PRECISION (bftype) != bitsize
3330 || TYPE_UNSIGNED (bftype) == !unsignedp)
3331 bftype = build_nonstandard_integer_type (bitsize, 0);
3333 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3334 size_int (bitsize), bitsize_int (bitpos));
3337 result = fold_convert_loc (loc, type, result);
3342 /* Optimize a bit-field compare.
3344 There are two cases: First is a compare against a constant and the
3345 second is a comparison of two items where the fields are at the same
3346 bit position relative to the start of a chunk (byte, halfword, word)
3347 large enough to contain it. In these cases we can avoid the shift
3348 implicit in bitfield extractions.
3350 For constants, we emit a compare of the shifted constant with the
3351 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3352 compared. For two fields at the same position, we do the ANDs with the
3353 similar mask and compare the result of the ANDs.
3355 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3356 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3357 are the left and right operands of the comparison, respectively.
3359 If the optimization described above can be done, we return the resulting
3360 tree. Otherwise we return zero. */
3363 optimize_bit_field_compare (location_t loc, enum tree_code code,
3364 tree compare_type, tree lhs, tree rhs)
3366 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3367 tree type = TREE_TYPE (lhs);
3368 tree signed_type, unsigned_type;
3369 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3370 enum machine_mode lmode, rmode, nmode;
3371 int lunsignedp, runsignedp;
3372 int lvolatilep = 0, rvolatilep = 0;
3373 tree linner, rinner = NULL_TREE;
3377 /* Get all the information about the extractions being done. If the bit size
3378 if the same as the size of the underlying object, we aren't doing an
3379 extraction at all and so can do nothing. We also don't want to
3380 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3381 then will no longer be able to replace it. */
3382 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3383 &lunsignedp, &lvolatilep, false);
3384 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3385 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3390 /* If this is not a constant, we can only do something if bit positions,
3391 sizes, and signedness are the same. */
3392 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3393 &runsignedp, &rvolatilep, false);
3395 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3396 || lunsignedp != runsignedp || offset != 0
3397 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3401 /* See if we can find a mode to refer to this field. We should be able to,
3402 but fail if we can't. */
3404 && GET_MODE_BITSIZE (lmode) > 0
3405 && flag_strict_volatile_bitfields > 0)
3408 nmode = get_best_mode (lbitsize, lbitpos,
3409 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3410 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3411 TYPE_ALIGN (TREE_TYPE (rinner))),
3412 word_mode, lvolatilep || rvolatilep);
3413 if (nmode == VOIDmode)
3416 /* Set signed and unsigned types of the precision of this mode for the
3418 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3419 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3421 /* Compute the bit position and size for the new reference and our offset
3422 within it. If the new reference is the same size as the original, we
3423 won't optimize anything, so return zero. */
3424 nbitsize = GET_MODE_BITSIZE (nmode);
3425 nbitpos = lbitpos & ~ (nbitsize - 1);
3427 if (nbitsize == lbitsize)
3430 if (BYTES_BIG_ENDIAN)
3431 lbitpos = nbitsize - lbitsize - lbitpos;
3433 /* Make the mask to be used against the extracted field. */
3434 mask = build_int_cst_type (unsigned_type, -1);
3435 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3436 mask = const_binop (RSHIFT_EXPR, mask,
3437 size_int (nbitsize - lbitsize - lbitpos));
3440 /* If not comparing with constant, just rework the comparison
3442 return fold_build2_loc (loc, code, compare_type,
3443 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3444 make_bit_field_ref (loc, linner,
3449 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3450 make_bit_field_ref (loc, rinner,
3456 /* Otherwise, we are handling the constant case. See if the constant is too
3457 big for the field. Warn and return a tree of for 0 (false) if so. We do
3458 this not only for its own sake, but to avoid having to test for this
3459 error case below. If we didn't, we might generate wrong code.
3461 For unsigned fields, the constant shifted right by the field length should
3462 be all zero. For signed fields, the high-order bits should agree with
3467 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3468 fold_convert_loc (loc,
3469 unsigned_type, rhs),
3470 size_int (lbitsize))))
3472 warning (0, "comparison is always %d due to width of bit-field",
3474 return constant_boolean_node (code == NE_EXPR, compare_type);
3479 tree tem = const_binop (RSHIFT_EXPR,
3480 fold_convert_loc (loc, signed_type, rhs),
3481 size_int (lbitsize - 1));
3482 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3484 warning (0, "comparison is always %d due to width of bit-field",
3486 return constant_boolean_node (code == NE_EXPR, compare_type);
3490 /* Single-bit compares should always be against zero. */
3491 if (lbitsize == 1 && ! integer_zerop (rhs))
3493 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3494 rhs = build_int_cst (type, 0);
3497 /* Make a new bitfield reference, shift the constant over the
3498 appropriate number of bits and mask it with the computed mask
3499 (in case this was a signed field). If we changed it, make a new one. */
3500 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3503 TREE_SIDE_EFFECTS (lhs) = 1;
3504 TREE_THIS_VOLATILE (lhs) = 1;
3507 rhs = const_binop (BIT_AND_EXPR,
3508 const_binop (LSHIFT_EXPR,
3509 fold_convert_loc (loc, unsigned_type, rhs),
3510 size_int (lbitpos)),
3513 lhs = build2_loc (loc, code, compare_type,
3514 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3518 /* Subroutine for fold_truthop: decode a field reference.
3520 If EXP is a comparison reference, we return the innermost reference.
3522 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3523 set to the starting bit number.
3525 If the innermost field can be completely contained in a mode-sized
3526 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3528 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3529 otherwise it is not changed.
3531 *PUNSIGNEDP is set to the signedness of the field.
3533 *PMASK is set to the mask used. This is either contained in a
3534 BIT_AND_EXPR or derived from the width of the field.
3536 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3538 Return 0 if this is not a component reference or is one that we can't
3539 do anything with. */
3542 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3543 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3544 int *punsignedp, int *pvolatilep,
3545 tree *pmask, tree *pand_mask)
3547 tree outer_type = 0;
3549 tree mask, inner, offset;
3551 unsigned int precision;
3553 /* All the optimizations using this function assume integer fields.
3554 There are problems with FP fields since the type_for_size call
3555 below can fail for, e.g., XFmode. */
3556 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3559 /* We are interested in the bare arrangement of bits, so strip everything
3560 that doesn't affect the machine mode. However, record the type of the
3561 outermost expression if it may matter below. */
3562 if (CONVERT_EXPR_P (exp)
3563 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3564 outer_type = TREE_TYPE (exp);
3567 if (TREE_CODE (exp) == BIT_AND_EXPR)
3569 and_mask = TREE_OPERAND (exp, 1);
3570 exp = TREE_OPERAND (exp, 0);
3571 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3572 if (TREE_CODE (and_mask) != INTEGER_CST)
3576 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3577 punsignedp, pvolatilep, false);
3578 if ((inner == exp && and_mask == 0)
3579 || *pbitsize < 0 || offset != 0
3580 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3583 /* If the number of bits in the reference is the same as the bitsize of
3584 the outer type, then the outer type gives the signedness. Otherwise
3585 (in case of a small bitfield) the signedness is unchanged. */
3586 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3587 *punsignedp = TYPE_UNSIGNED (outer_type);
3589 /* Compute the mask to access the bitfield. */
3590 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3591 precision = TYPE_PRECISION (unsigned_type);
3593 mask = build_int_cst_type (unsigned_type, -1);
3595 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3596 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3598 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3600 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3601 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3604 *pand_mask = and_mask;
3608 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3612 all_ones_mask_p (const_tree mask, int size)
3614 tree type = TREE_TYPE (mask);
3615 unsigned int precision = TYPE_PRECISION (type);
3618 tmask = build_int_cst_type (signed_type_for (type), -1);
3621 tree_int_cst_equal (mask,
3622 const_binop (RSHIFT_EXPR,
3623 const_binop (LSHIFT_EXPR, tmask,
3624 size_int (precision - size)),
3625 size_int (precision - size)));
3628 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3629 represents the sign bit of EXP's type. If EXP represents a sign
3630 or zero extension, also test VAL against the unextended type.
3631 The return value is the (sub)expression whose sign bit is VAL,
3632 or NULL_TREE otherwise. */
3635 sign_bit_p (tree exp, const_tree val)
3637 unsigned HOST_WIDE_INT mask_lo, lo;
3638 HOST_WIDE_INT mask_hi, hi;
3642 /* Tree EXP must have an integral type. */
3643 t = TREE_TYPE (exp);
3644 if (! INTEGRAL_TYPE_P (t))
3647 /* Tree VAL must be an integer constant. */
3648 if (TREE_CODE (val) != INTEGER_CST
3649 || TREE_OVERFLOW (val))
3652 width = TYPE_PRECISION (t);
3653 if (width > HOST_BITS_PER_WIDE_INT)
3655 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3658 mask_hi = ((unsigned HOST_WIDE_INT) -1
3659 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3665 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3668 mask_lo = ((unsigned HOST_WIDE_INT) -1
3669 >> (HOST_BITS_PER_WIDE_INT - width));
3672 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3673 treat VAL as if it were unsigned. */
3674 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3675 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3678 /* Handle extension from a narrower type. */
3679 if (TREE_CODE (exp) == NOP_EXPR
3680 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3681 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3686 /* Subroutine for fold_truthop: determine if an operand is simple enough
3687 to be evaluated unconditionally. */
3690 simple_operand_p (const_tree exp)
3692 /* Strip any conversions that don't change the machine mode. */
3695 return (CONSTANT_CLASS_P (exp)
3696 || TREE_CODE (exp) == SSA_NAME
3698 && ! TREE_ADDRESSABLE (exp)
3699 && ! TREE_THIS_VOLATILE (exp)
3700 && ! DECL_NONLOCAL (exp)
3701 /* Don't regard global variables as simple. They may be
3702 allocated in ways unknown to the compiler (shared memory,
3703 #pragma weak, etc). */
3704 && ! TREE_PUBLIC (exp)
3705 && ! DECL_EXTERNAL (exp)
3706 /* Loading a static variable is unduly expensive, but global
3707 registers aren't expensive. */
3708 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3711 /* The following functions are subroutines to fold_range_test and allow it to
3712 try to change a logical combination of comparisons into a range test.
3715 X == 2 || X == 3 || X == 4 || X == 5
3719 (unsigned) (X - 2) <= 3
3721 We describe each set of comparisons as being either inside or outside
3722 a range, using a variable named like IN_P, and then describe the
3723 range with a lower and upper bound. If one of the bounds is omitted,
3724 it represents either the highest or lowest value of the type.
3726 In the comments below, we represent a range by two numbers in brackets
3727 preceded by a "+" to designate being inside that range, or a "-" to
3728 designate being outside that range, so the condition can be inverted by
3729 flipping the prefix. An omitted bound is represented by a "-". For
3730 example, "- [-, 10]" means being outside the range starting at the lowest
3731 possible value and ending at 10, in other words, being greater than 10.
3732 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3735 We set up things so that the missing bounds are handled in a consistent
3736 manner so neither a missing bound nor "true" and "false" need to be
3737 handled using a special case. */
3739 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3740 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3741 and UPPER1_P are nonzero if the respective argument is an upper bound
3742 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3743 must be specified for a comparison. ARG1 will be converted to ARG0's
3744 type if both are specified. */
3747 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3748 tree arg1, int upper1_p)
3754 /* If neither arg represents infinity, do the normal operation.
3755 Else, if not a comparison, return infinity. Else handle the special
3756 comparison rules. Note that most of the cases below won't occur, but
3757 are handled for consistency. */
3759 if (arg0 != 0 && arg1 != 0)
3761 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3762 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3764 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3767 if (TREE_CODE_CLASS (code) != tcc_comparison)
3770 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3771 for neither. In real maths, we cannot assume open ended ranges are
3772 the same. But, this is computer arithmetic, where numbers are finite.
3773 We can therefore make the transformation of any unbounded range with
3774 the value Z, Z being greater than any representable number. This permits
3775 us to treat unbounded ranges as equal. */
3776 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3777 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3781 result = sgn0 == sgn1;
3784 result = sgn0 != sgn1;
3787 result = sgn0 < sgn1;
3790 result = sgn0 <= sgn1;
3793 result = sgn0 > sgn1;
3796 result = sgn0 >= sgn1;
3802 return constant_boolean_node (result, type);
3805 /* Given EXP, a logical expression, set the range it is testing into
3806 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3807 actually being tested. *PLOW and *PHIGH will be made of the same
3808 type as the returned expression. If EXP is not a comparison, we
3809 will most likely not be returning a useful value and range. Set
3810 *STRICT_OVERFLOW_P to true if the return value is only valid
3811 because signed overflow is undefined; otherwise, do not change
3812 *STRICT_OVERFLOW_P. */
3815 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3816 bool *strict_overflow_p)
3818 enum tree_code code;
3819 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3820 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3822 tree low, high, n_low, n_high;
3823 location_t loc = EXPR_LOCATION (exp);
3825 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3826 and see if we can refine the range. Some of the cases below may not
3827 happen, but it doesn't seem worth worrying about this. We "continue"
3828 the outer loop when we've changed something; otherwise we "break"
3829 the switch, which will "break" the while. */
3832 low = high = build_int_cst (TREE_TYPE (exp), 0);
3836 code = TREE_CODE (exp);
3837 exp_type = TREE_TYPE (exp);
3839 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3841 if (TREE_OPERAND_LENGTH (exp) > 0)
3842 arg0 = TREE_OPERAND (exp, 0);
3843 if (TREE_CODE_CLASS (code) == tcc_comparison
3844 || TREE_CODE_CLASS (code) == tcc_unary
3845 || TREE_CODE_CLASS (code) == tcc_binary)
3846 arg0_type = TREE_TYPE (arg0);
3847 if (TREE_CODE_CLASS (code) == tcc_binary
3848 || TREE_CODE_CLASS (code) == tcc_comparison
3849 || (TREE_CODE_CLASS (code) == tcc_expression
3850 && TREE_OPERAND_LENGTH (exp) > 1))
3851 arg1 = TREE_OPERAND (exp, 1);
3856 case TRUTH_NOT_EXPR:
3857 in_p = ! in_p, exp = arg0;
3860 case EQ_EXPR: case NE_EXPR:
3861 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3862 /* We can only do something if the range is testing for zero
3863 and if the second operand is an integer constant. Note that
3864 saying something is "in" the range we make is done by
3865 complementing IN_P since it will set in the initial case of
3866 being not equal to zero; "out" is leaving it alone. */
3867 if (low == 0 || high == 0
3868 || ! integer_zerop (low) || ! integer_zerop (high)
3869 || TREE_CODE (arg1) != INTEGER_CST)
3874 case NE_EXPR: /* - [c, c] */
3877 case EQ_EXPR: /* + [c, c] */
3878 in_p = ! in_p, low = high = arg1;
3880 case GT_EXPR: /* - [-, c] */
3881 low = 0, high = arg1;
3883 case GE_EXPR: /* + [c, -] */
3884 in_p = ! in_p, low = arg1, high = 0;
3886 case LT_EXPR: /* - [c, -] */
3887 low = arg1, high = 0;
3889 case LE_EXPR: /* + [-, c] */
3890 in_p = ! in_p, low = 0, high = arg1;
3896 /* If this is an unsigned comparison, we also know that EXP is
3897 greater than or equal to zero. We base the range tests we make
3898 on that fact, so we record it here so we can parse existing
3899 range tests. We test arg0_type since often the return type
3900 of, e.g. EQ_EXPR, is boolean. */
3901 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3903 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3905 build_int_cst (arg0_type, 0),
3909 in_p = n_in_p, low = n_low, high = n_high;
3911 /* If the high bound is missing, but we have a nonzero low
3912 bound, reverse the range so it goes from zero to the low bound
3914 if (high == 0 && low && ! integer_zerop (low))
3917 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3918 integer_one_node, 0);
3919 low = build_int_cst (arg0_type, 0);
3927 /* (-x) IN [a,b] -> x in [-b, -a] */
3928 n_low = range_binop (MINUS_EXPR, exp_type,
3929 build_int_cst (exp_type, 0),
3931 n_high = range_binop (MINUS_EXPR, exp_type,
3932 build_int_cst (exp_type, 0),
3934 if (n_high != 0 && TREE_OVERFLOW (n_high))
3940 exp = build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3941 build_int_cst (exp_type, 1));
3944 case PLUS_EXPR: case MINUS_EXPR:
3945 if (TREE_CODE (arg1) != INTEGER_CST)
3948 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3949 move a constant to the other side. */
3950 if (!TYPE_UNSIGNED (arg0_type)
3951 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3954 /* If EXP is signed, any overflow in the computation is undefined,
3955 so we don't worry about it so long as our computations on
3956 the bounds don't overflow. For unsigned, overflow is defined
3957 and this is exactly the right thing. */
3958 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3959 arg0_type, low, 0, arg1, 0);
3960 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3961 arg0_type, high, 1, arg1, 0);
3962 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3963 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3966 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3967 *strict_overflow_p = true;
3970 /* Check for an unsigned range which has wrapped around the maximum
3971 value thus making n_high < n_low, and normalize it. */
3972 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3974 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3975 integer_one_node, 0);
3976 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3977 integer_one_node, 0);
3979 /* If the range is of the form +/- [ x+1, x ], we won't
3980 be able to normalize it. But then, it represents the
3981 whole range or the empty set, so make it
3983 if (tree_int_cst_equal (n_low, low)
3984 && tree_int_cst_equal (n_high, high))
3990 low = n_low, high = n_high;
3995 CASE_CONVERT: case NON_LVALUE_EXPR:
3996 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3999 if (! INTEGRAL_TYPE_P (arg0_type)
4000 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4001 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4004 n_low = low, n_high = high;
4007 n_low = fold_convert_loc (loc, arg0_type, n_low);
4010 n_high = fold_convert_loc (loc, arg0_type, n_high);
4013 /* If we're converting arg0 from an unsigned type, to exp,
4014 a signed type, we will be doing the comparison as unsigned.
4015 The tests above have already verified that LOW and HIGH
4018 So we have to ensure that we will handle large unsigned
4019 values the same way that the current signed bounds treat
4022 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4026 /* For fixed-point modes, we need to pass the saturating flag
4027 as the 2nd parameter. */
4028 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4029 equiv_type = lang_hooks.types.type_for_mode
4030 (TYPE_MODE (arg0_type),
4031 TYPE_SATURATING (arg0_type));
4033 equiv_type = lang_hooks.types.type_for_mode
4034 (TYPE_MODE (arg0_type), 1);
4036 /* A range without an upper bound is, naturally, unbounded.
4037 Since convert would have cropped a very large value, use
4038 the max value for the destination type. */
4040 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4041 : TYPE_MAX_VALUE (arg0_type);
4043 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4044 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4045 fold_convert_loc (loc, arg0_type,
4047 build_int_cst (arg0_type, 1));
4049 /* If the low bound is specified, "and" the range with the
4050 range for which the original unsigned value will be
4054 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4055 1, n_low, n_high, 1,
4056 fold_convert_loc (loc, arg0_type,
4061 in_p = (n_in_p == in_p);
4065 /* Otherwise, "or" the range with the range of the input
4066 that will be interpreted as negative. */
4067 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4068 0, n_low, n_high, 1,
4069 fold_convert_loc (loc, arg0_type,
4074 in_p = (in_p != n_in_p);
4079 low = n_low, high = n_high;
4089 /* If EXP is a constant, we can evaluate whether this is true or false. */
4090 if (TREE_CODE (exp) == INTEGER_CST)
4092 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4094 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4100 *pin_p = in_p, *plow = low, *phigh = high;
4104 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4105 type, TYPE, return an expression to test if EXP is in (or out of, depending
4106 on IN_P) the range. Return 0 if the test couldn't be created. */
4109 build_range_check (location_t loc, tree type, tree exp, int in_p,
4110 tree low, tree high)
4112 tree etype = TREE_TYPE (exp), value;
4114 #ifdef HAVE_canonicalize_funcptr_for_compare
4115 /* Disable this optimization for function pointer expressions
4116 on targets that require function pointer canonicalization. */
4117 if (HAVE_canonicalize_funcptr_for_compare
4118 && TREE_CODE (etype) == POINTER_TYPE
4119 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4125 value = build_range_check (loc, type, exp, 1, low, high);
4127 return invert_truthvalue_loc (loc, value);
4132 if (low == 0 && high == 0)
4133 return build_int_cst (type, 1);
4136 return fold_build2_loc (loc, LE_EXPR, type, exp,
4137 fold_convert_loc (loc, etype, high));
4140 return fold_build2_loc (loc, GE_EXPR, type, exp,
4141 fold_convert_loc (loc, etype, low));
4143 if (operand_equal_p (low, high, 0))
4144 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4145 fold_convert_loc (loc, etype, low));
4147 if (integer_zerop (low))
4149 if (! TYPE_UNSIGNED (etype))
4151 etype = unsigned_type_for (etype);
4152 high = fold_convert_loc (loc, etype, high);
4153 exp = fold_convert_loc (loc, etype, exp);
4155 return build_range_check (loc, type, exp, 1, 0, high);
4158 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4159 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4161 unsigned HOST_WIDE_INT lo;
4165 prec = TYPE_PRECISION (etype);
4166 if (prec <= HOST_BITS_PER_WIDE_INT)
4169 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4173 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4174 lo = (unsigned HOST_WIDE_INT) -1;
4177 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4179 if (TYPE_UNSIGNED (etype))
4181 tree signed_etype = signed_type_for (etype);
4182 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4184 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4186 etype = signed_etype;
4187 exp = fold_convert_loc (loc, etype, exp);
4189 return fold_build2_loc (loc, GT_EXPR, type, exp,
4190 build_int_cst (etype, 0));
4194 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4195 This requires wrap-around arithmetics for the type of the expression.
4196 First make sure that arithmetics in this type is valid, then make sure
4197 that it wraps around. */
4198 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4199 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4200 TYPE_UNSIGNED (etype));
4202 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4204 tree utype, minv, maxv;
4206 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4207 for the type in question, as we rely on this here. */
4208 utype = unsigned_type_for (etype);
4209 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4210 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4211 integer_one_node, 1);
4212 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4214 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4221 high = fold_convert_loc (loc, etype, high);
4222 low = fold_convert_loc (loc, etype, low);
4223 exp = fold_convert_loc (loc, etype, exp);
4225 value = const_binop (MINUS_EXPR, high, low);
4228 if (POINTER_TYPE_P (etype))
4230 if (value != 0 && !TREE_OVERFLOW (value))
4232 low = fold_convert_loc (loc, sizetype, low);
4233 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4234 return build_range_check (loc, type,
4235 fold_build2_loc (loc, POINTER_PLUS_EXPR,
4237 1, build_int_cst (etype, 0), value);
4242 if (value != 0 && !TREE_OVERFLOW (value))
4243 return build_range_check (loc, type,
4244 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4245 1, build_int_cst (etype, 0), value);
4250 /* Return the predecessor of VAL in its type, handling the infinite case. */
4253 range_predecessor (tree val)
4255 tree type = TREE_TYPE (val);
4257 if (INTEGRAL_TYPE_P (type)
4258 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4261 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4264 /* Return the successor of VAL in its type, handling the infinite case. */
4267 range_successor (tree val)
4269 tree type = TREE_TYPE (val);
4271 if (INTEGRAL_TYPE_P (type)
4272 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4275 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4278 /* Given two ranges, see if we can merge them into one. Return 1 if we
4279 can, 0 if we can't. Set the output range into the specified parameters. */
4282 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4283 tree high0, int in1_p, tree low1, tree high1)
4291 int lowequal = ((low0 == 0 && low1 == 0)
4292 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4293 low0, 0, low1, 0)));
4294 int highequal = ((high0 == 0 && high1 == 0)
4295 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4296 high0, 1, high1, 1)));
4298 /* Make range 0 be the range that starts first, or ends last if they
4299 start at the same value. Swap them if it isn't. */
4300 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4303 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4304 high1, 1, high0, 1))))
4306 temp = in0_p, in0_p = in1_p, in1_p = temp;
4307 tem = low0, low0 = low1, low1 = tem;
4308 tem = high0, high0 = high1, high1 = tem;
4311 /* Now flag two cases, whether the ranges are disjoint or whether the
4312 second range is totally subsumed in the first. Note that the tests
4313 below are simplified by the ones above. */
4314 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4315 high0, 1, low1, 0));
4316 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4317 high1, 1, high0, 1));
4319 /* We now have four cases, depending on whether we are including or
4320 excluding the two ranges. */
4323 /* If they don't overlap, the result is false. If the second range
4324 is a subset it is the result. Otherwise, the range is from the start
4325 of the second to the end of the first. */
4327 in_p = 0, low = high = 0;
4329 in_p = 1, low = low1, high = high1;
4331 in_p = 1, low = low1, high = high0;
4334 else if (in0_p && ! in1_p)
4336 /* If they don't overlap, the result is the first range. If they are
4337 equal, the result is false. If the second range is a subset of the
4338 first, and the ranges begin at the same place, we go from just after
4339 the end of the second range to the end of the first. If the second
4340 range is not a subset of the first, or if it is a subset and both
4341 ranges end at the same place, the range starts at the start of the
4342 first range and ends just before the second range.
4343 Otherwise, we can't describe this as a single range. */
4345 in_p = 1, low = low0, high = high0;
4346 else if (lowequal && highequal)
4347 in_p = 0, low = high = 0;
4348 else if (subset && lowequal)
4350 low = range_successor (high1);
4355 /* We are in the weird situation where high0 > high1 but
4356 high1 has no successor. Punt. */
4360 else if (! subset || highequal)
4363 high = range_predecessor (low1);
4367 /* low0 < low1 but low1 has no predecessor. Punt. */
4375 else if (! in0_p && in1_p)
4377 /* If they don't overlap, the result is the second range. If the second
4378 is a subset of the first, the result is false. Otherwise,
4379 the range starts just after the first range and ends at the
4380 end of the second. */
4382 in_p = 1, low = low1, high = high1;
4383 else if (subset || highequal)
4384 in_p = 0, low = high = 0;
4387 low = range_successor (high0);
4392 /* high1 > high0 but high0 has no successor. Punt. */
4400 /* The case where we are excluding both ranges. Here the complex case
4401 is if they don't overlap. In that case, the only time we have a
4402 range is if they are adjacent. If the second is a subset of the
4403 first, the result is the first. Otherwise, the range to exclude
4404 starts at the beginning of the first range and ends at the end of the
4408 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4409 range_successor (high0),
4411 in_p = 0, low = low0, high = high1;
4414 /* Canonicalize - [min, x] into - [-, x]. */
4415 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4416 switch (TREE_CODE (TREE_TYPE (low0)))
4419 if (TYPE_PRECISION (TREE_TYPE (low0))
4420 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4424 if (tree_int_cst_equal (low0,
4425 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4429 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4430 && integer_zerop (low0))
4437 /* Canonicalize - [x, max] into - [x, -]. */
4438 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4439 switch (TREE_CODE (TREE_TYPE (high1)))
4442 if (TYPE_PRECISION (TREE_TYPE (high1))
4443 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4447 if (tree_int_cst_equal (high1,
4448 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4452 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4453 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4455 integer_one_node, 1)))
4462 /* The ranges might be also adjacent between the maximum and
4463 minimum values of the given type. For
4464 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4465 return + [x + 1, y - 1]. */
4466 if (low0 == 0 && high1 == 0)
4468 low = range_successor (high0);
4469 high = range_predecessor (low1);
4470 if (low == 0 || high == 0)
4480 in_p = 0, low = low0, high = high0;
4482 in_p = 0, low = low0, high = high1;
4485 *pin_p = in_p, *plow = low, *phigh = high;
4490 /* Subroutine of fold, looking inside expressions of the form
4491 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4492 of the COND_EXPR. This function is being used also to optimize
4493 A op B ? C : A, by reversing the comparison first.
4495 Return a folded expression whose code is not a COND_EXPR
4496 anymore, or NULL_TREE if no folding opportunity is found. */
4499 fold_cond_expr_with_comparison (location_t loc, tree type,
4500 tree arg0, tree arg1, tree arg2)
4502 enum tree_code comp_code = TREE_CODE (arg0);
4503 tree arg00 = TREE_OPERAND (arg0, 0);
4504 tree arg01 = TREE_OPERAND (arg0, 1);
4505 tree arg1_type = TREE_TYPE (arg1);
4511 /* If we have A op 0 ? A : -A, consider applying the following
4514 A == 0? A : -A same as -A
4515 A != 0? A : -A same as A
4516 A >= 0? A : -A same as abs (A)
4517 A > 0? A : -A same as abs (A)
4518 A <= 0? A : -A same as -abs (A)
4519 A < 0? A : -A same as -abs (A)
4521 None of these transformations work for modes with signed
4522 zeros. If A is +/-0, the first two transformations will
4523 change the sign of the result (from +0 to -0, or vice
4524 versa). The last four will fix the sign of the result,
4525 even though the original expressions could be positive or
4526 negative, depending on the sign of A.
4528 Note that all these transformations are correct if A is
4529 NaN, since the two alternatives (A and -A) are also NaNs. */
4530 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4531 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4532 ? real_zerop (arg01)
4533 : integer_zerop (arg01))
4534 && ((TREE_CODE (arg2) == NEGATE_EXPR
4535 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4536 /* In the case that A is of the form X-Y, '-A' (arg2) may
4537 have already been folded to Y-X, check for that. */
4538 || (TREE_CODE (arg1) == MINUS_EXPR
4539 && TREE_CODE (arg2) == MINUS_EXPR
4540 && operand_equal_p (TREE_OPERAND (arg1, 0),
4541 TREE_OPERAND (arg2, 1), 0)
4542 && operand_equal_p (TREE_OPERAND (arg1, 1),
4543 TREE_OPERAND (arg2, 0), 0))))
4548 tem = fold_convert_loc (loc, arg1_type, arg1);
4549 return pedantic_non_lvalue_loc (loc,
4550 fold_convert_loc (loc, type,
4551 negate_expr (tem)));
4554 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4557 if (flag_trapping_math)
4562 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4563 arg1 = fold_convert_loc (loc, signed_type_for
4564 (TREE_TYPE (arg1)), arg1);
4565 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4566 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4569 if (flag_trapping_math)
4573 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4574 arg1 = fold_convert_loc (loc, signed_type_for
4575 (TREE_TYPE (arg1)), arg1);
4576 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4577 return negate_expr (fold_convert_loc (loc, type, tem));
4579 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4583 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4584 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4585 both transformations are correct when A is NaN: A != 0
4586 is then true, and A == 0 is false. */
4588 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4589 && integer_zerop (arg01) && integer_zerop (arg2))
4591 if (comp_code == NE_EXPR)
4592 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4593 else if (comp_code == EQ_EXPR)
4594 return build_int_cst (type, 0);
4597 /* Try some transformations of A op B ? A : B.
4599 A == B? A : B same as B
4600 A != B? A : B same as A
4601 A >= B? A : B same as max (A, B)
4602 A > B? A : B same as max (B, A)
4603 A <= B? A : B same as min (A, B)
4604 A < B? A : B same as min (B, A)
4606 As above, these transformations don't work in the presence
4607 of signed zeros. For example, if A and B are zeros of
4608 opposite sign, the first two transformations will change
4609 the sign of the result. In the last four, the original
4610 expressions give different results for (A=+0, B=-0) and
4611 (A=-0, B=+0), but the transformed expressions do not.
4613 The first two transformations are correct if either A or B
4614 is a NaN. In the first transformation, the condition will
4615 be false, and B will indeed be chosen. In the case of the
4616 second transformation, the condition A != B will be true,
4617 and A will be chosen.
4619 The conversions to max() and min() are not correct if B is
4620 a number and A is not. The conditions in the original
4621 expressions will be false, so all four give B. The min()
4622 and max() versions would give a NaN instead. */
4623 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4624 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4625 /* Avoid these transformations if the COND_EXPR may be used
4626 as an lvalue in the C++ front-end. PR c++/19199. */
4628 || (strcmp (lang_hooks.name, "GNU C++") != 0
4629 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4630 || ! maybe_lvalue_p (arg1)
4631 || ! maybe_lvalue_p (arg2)))
4633 tree comp_op0 = arg00;
4634 tree comp_op1 = arg01;
4635 tree comp_type = TREE_TYPE (comp_op0);
4637 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4638 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4648 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4650 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4655 /* In C++ a ?: expression can be an lvalue, so put the
4656 operand which will be used if they are equal first
4657 so that we can convert this back to the
4658 corresponding COND_EXPR. */
4659 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4661 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4662 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4663 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4664 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4665 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4666 comp_op1, comp_op0);
4667 return pedantic_non_lvalue_loc (loc,
4668 fold_convert_loc (loc, type, tem));
4675 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4677 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4678 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4679 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4680 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4681 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4682 comp_op1, comp_op0);
4683 return pedantic_non_lvalue_loc (loc,
4684 fold_convert_loc (loc, type, tem));
4688 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4689 return pedantic_non_lvalue_loc (loc,
4690 fold_convert_loc (loc, type, arg2));
4693 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4694 return pedantic_non_lvalue_loc (loc,
4695 fold_convert_loc (loc, type, arg1));
4698 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4703 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4704 we might still be able to simplify this. For example,
4705 if C1 is one less or one more than C2, this might have started
4706 out as a MIN or MAX and been transformed by this function.
4707 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4709 if (INTEGRAL_TYPE_P (type)
4710 && TREE_CODE (arg01) == INTEGER_CST
4711 && TREE_CODE (arg2) == INTEGER_CST)
4715 if (TREE_CODE (arg1) == INTEGER_CST)
4717 /* We can replace A with C1 in this case. */
4718 arg1 = fold_convert_loc (loc, type, arg01);
4719 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4722 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4723 MIN_EXPR, to preserve the signedness of the comparison. */
4724 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4726 && operand_equal_p (arg01,
4727 const_binop (PLUS_EXPR, arg2,
4728 build_int_cst (type, 1)),
4731 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4732 fold_convert_loc (loc, TREE_TYPE (arg00),
4734 return pedantic_non_lvalue_loc (loc,
4735 fold_convert_loc (loc, type, tem));
4740 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4742 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4744 && operand_equal_p (arg01,
4745 const_binop (MINUS_EXPR, arg2,
4746 build_int_cst (type, 1)),
4749 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4750 fold_convert_loc (loc, TREE_TYPE (arg00),
4752 return pedantic_non_lvalue_loc (loc,
4753 fold_convert_loc (loc, type, tem));
4758 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4759 MAX_EXPR, to preserve the signedness of the comparison. */
4760 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4762 && operand_equal_p (arg01,
4763 const_binop (MINUS_EXPR, arg2,
4764 build_int_cst (type, 1)),
4767 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4768 fold_convert_loc (loc, TREE_TYPE (arg00),
4770 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4775 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4776 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4778 && operand_equal_p (arg01,
4779 const_binop (PLUS_EXPR, arg2,
4780 build_int_cst (type, 1)),
4783 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4784 fold_convert_loc (loc, TREE_TYPE (arg00),
4786 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4800 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4801 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4802 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4806 /* EXP is some logical combination of boolean tests. See if we can
4807 merge it into some range test. Return the new tree if so. */
4810 fold_range_test (location_t loc, enum tree_code code, tree type,
4813 int or_op = (code == TRUTH_ORIF_EXPR
4814 || code == TRUTH_OR_EXPR);
4815 int in0_p, in1_p, in_p;
4816 tree low0, low1, low, high0, high1, high;
4817 bool strict_overflow_p = false;
4818 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4819 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4821 const char * const warnmsg = G_("assuming signed overflow does not occur "
4822 "when simplifying range test");
4824 /* If this is an OR operation, invert both sides; we will invert
4825 again at the end. */
4827 in0_p = ! in0_p, in1_p = ! in1_p;
4829 /* If both expressions are the same, if we can merge the ranges, and we
4830 can build the range test, return it or it inverted. If one of the
4831 ranges is always true or always false, consider it to be the same
4832 expression as the other. */
4833 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4834 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4836 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4838 : rhs != 0 ? rhs : integer_zero_node,
4841 if (strict_overflow_p)
4842 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4843 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4846 /* On machines where the branch cost is expensive, if this is a
4847 short-circuited branch and the underlying object on both sides
4848 is the same, make a non-short-circuit operation. */
4849 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4850 && lhs != 0 && rhs != 0
4851 && (code == TRUTH_ANDIF_EXPR
4852 || code == TRUTH_ORIF_EXPR)
4853 && operand_equal_p (lhs, rhs, 0))
4855 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4856 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4857 which cases we can't do this. */
4858 if (simple_operand_p (lhs))
4859 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4860 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4863 else if (lang_hooks.decls.global_bindings_p () == 0
4864 && ! CONTAINS_PLACEHOLDER_P (lhs))
4866 tree common = save_expr (lhs);
4868 if (0 != (lhs = build_range_check (loc, type, common,
4869 or_op ? ! in0_p : in0_p,
4871 && (0 != (rhs = build_range_check (loc, type, common,
4872 or_op ? ! in1_p : in1_p,
4875 if (strict_overflow_p)
4876 fold_overflow_warning (warnmsg,
4877 WARN_STRICT_OVERFLOW_COMPARISON);
4878 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4879 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4888 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4889 bit value. Arrange things so the extra bits will be set to zero if and
4890 only if C is signed-extended to its full width. If MASK is nonzero,
4891 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4894 unextend (tree c, int p, int unsignedp, tree mask)
4896 tree type = TREE_TYPE (c);
4897 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4900 if (p == modesize || unsignedp)
4903 /* We work by getting just the sign bit into the low-order bit, then
4904 into the high-order bit, then sign-extend. We then XOR that value
4906 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4907 temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4909 /* We must use a signed type in order to get an arithmetic right shift.
4910 However, we must also avoid introducing accidental overflows, so that
4911 a subsequent call to integer_zerop will work. Hence we must
4912 do the type conversion here. At this point, the constant is either
4913 zero or one, and the conversion to a signed type can never overflow.
4914 We could get an overflow if this conversion is done anywhere else. */
4915 if (TYPE_UNSIGNED (type))
4916 temp = fold_convert (signed_type_for (type), temp);
4918 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4919 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4921 temp = const_binop (BIT_AND_EXPR, temp,
4922 fold_convert (TREE_TYPE (c), mask));
4923 /* If necessary, convert the type back to match the type of C. */
4924 if (TYPE_UNSIGNED (type))
4925 temp = fold_convert (type, temp);
4927 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4930 /* For an expression that has the form
4934 we can drop one of the inner expressions and simplify to
4938 LOC is the location of the resulting expression. OP is the inner
4939 logical operation; the left-hand side in the examples above, while CMPOP
4940 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
4941 removing a condition that guards another, as in
4942 (A != NULL && A->...) || A == NULL
4943 which we must not transform. If RHS_ONLY is true, only eliminate the
4944 right-most operand of the inner logical operation. */
4947 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4950 tree type = TREE_TYPE (cmpop);
4951 enum tree_code code = TREE_CODE (cmpop);
4952 enum tree_code truthop_code = TREE_CODE (op);
4953 tree lhs = TREE_OPERAND (op, 0);
4954 tree rhs = TREE_OPERAND (op, 1);
4955 tree orig_lhs = lhs, orig_rhs = rhs;
4956 enum tree_code rhs_code = TREE_CODE (rhs);
4957 enum tree_code lhs_code = TREE_CODE (lhs);
4958 enum tree_code inv_code;
4960 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4963 if (TREE_CODE_CLASS (code) != tcc_comparison)
4966 if (rhs_code == truthop_code)
4968 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
4969 if (newrhs != NULL_TREE)
4972 rhs_code = TREE_CODE (rhs);
4975 if (lhs_code == truthop_code && !rhs_only)
4977 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
4978 if (newlhs != NULL_TREE)
4981 lhs_code = TREE_CODE (lhs);
4985 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
4986 if (inv_code == rhs_code
4987 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
4988 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
4990 if (!rhs_only && inv_code == lhs_code
4991 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
4992 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
4994 if (rhs != orig_rhs || lhs != orig_lhs)
4995 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5000 /* Find ways of folding logical expressions of LHS and RHS:
5001 Try to merge two comparisons to the same innermost item.
5002 Look for range tests like "ch >= '0' && ch <= '9'".
5003 Look for combinations of simple terms on machines with expensive branches
5004 and evaluate the RHS unconditionally.
5006 For example, if we have p->a == 2 && p->b == 4 and we can make an
5007 object large enough to span both A and B, we can do this with a comparison
5008 against the object ANDed with the a mask.
5010 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5011 operations to do this with one comparison.
5013 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5014 function and the one above.
5016 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5017 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5019 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5022 We return the simplified tree or 0 if no optimization is possible. */
5025 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5028 /* If this is the "or" of two comparisons, we can do something if
5029 the comparisons are NE_EXPR. If this is the "and", we can do something
5030 if the comparisons are EQ_EXPR. I.e.,
5031 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5033 WANTED_CODE is this operation code. For single bit fields, we can
5034 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5035 comparison for one-bit fields. */
5037 enum tree_code wanted_code;
5038 enum tree_code lcode, rcode;
5039 tree ll_arg, lr_arg, rl_arg, rr_arg;
5040 tree ll_inner, lr_inner, rl_inner, rr_inner;
5041 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5042 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5043 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5044 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5045 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5046 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5047 enum machine_mode lnmode, rnmode;
5048 tree ll_mask, lr_mask, rl_mask, rr_mask;
5049 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5050 tree l_const, r_const;
5051 tree lntype, rntype, result;
5052 HOST_WIDE_INT first_bit, end_bit;
5054 tree orig_lhs = lhs, orig_rhs = rhs;
5055 enum tree_code orig_code = code;
5057 /* Start by getting the comparison codes. Fail if anything is volatile.
5058 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5059 it were surrounded with a NE_EXPR. */
5061 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5064 lcode = TREE_CODE (lhs);
5065 rcode = TREE_CODE (rhs);
5067 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5069 lhs = build2 (NE_EXPR, truth_type, lhs,
5070 build_int_cst (TREE_TYPE (lhs), 0));
5074 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5076 rhs = build2 (NE_EXPR, truth_type, rhs,
5077 build_int_cst (TREE_TYPE (rhs), 0));
5081 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5082 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5085 ll_arg = TREE_OPERAND (lhs, 0);
5086 lr_arg = TREE_OPERAND (lhs, 1);
5087 rl_arg = TREE_OPERAND (rhs, 0);
5088 rr_arg = TREE_OPERAND (rhs, 1);
5090 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5091 if (simple_operand_p (ll_arg)
5092 && simple_operand_p (lr_arg))
5094 if (operand_equal_p (ll_arg, rl_arg, 0)
5095 && operand_equal_p (lr_arg, rr_arg, 0))
5097 result = combine_comparisons (loc, code, lcode, rcode,
5098 truth_type, ll_arg, lr_arg);
5102 else if (operand_equal_p (ll_arg, rr_arg, 0)
5103 && operand_equal_p (lr_arg, rl_arg, 0))
5105 result = combine_comparisons (loc, code, lcode,
5106 swap_tree_comparison (rcode),
5107 truth_type, ll_arg, lr_arg);
5113 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5114 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5116 /* If the RHS can be evaluated unconditionally and its operands are
5117 simple, it wins to evaluate the RHS unconditionally on machines
5118 with expensive branches. In this case, this isn't a comparison
5119 that can be merged. Avoid doing this if the RHS is a floating-point
5120 comparison since those can trap. */
5122 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5124 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5125 && simple_operand_p (rl_arg)
5126 && simple_operand_p (rr_arg))
5128 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5129 if (code == TRUTH_OR_EXPR
5130 && lcode == NE_EXPR && integer_zerop (lr_arg)
5131 && rcode == NE_EXPR && integer_zerop (rr_arg)
5132 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5133 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5134 return build2_loc (loc, NE_EXPR, truth_type,
5135 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5137 build_int_cst (TREE_TYPE (ll_arg), 0));
5139 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5140 if (code == TRUTH_AND_EXPR
5141 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5142 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5143 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5144 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5145 return build2_loc (loc, EQ_EXPR, truth_type,
5146 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5148 build_int_cst (TREE_TYPE (ll_arg), 0));
5150 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5152 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5153 return build2_loc (loc, code, truth_type, lhs, rhs);
5158 /* See if the comparisons can be merged. Then get all the parameters for
5161 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5162 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5166 ll_inner = decode_field_reference (loc, ll_arg,
5167 &ll_bitsize, &ll_bitpos, &ll_mode,
5168 &ll_unsignedp, &volatilep, &ll_mask,
5170 lr_inner = decode_field_reference (loc, lr_arg,
5171 &lr_bitsize, &lr_bitpos, &lr_mode,
5172 &lr_unsignedp, &volatilep, &lr_mask,
5174 rl_inner = decode_field_reference (loc, rl_arg,
5175 &rl_bitsize, &rl_bitpos, &rl_mode,
5176 &rl_unsignedp, &volatilep, &rl_mask,
5178 rr_inner = decode_field_reference (loc, rr_arg,
5179 &rr_bitsize, &rr_bitpos, &rr_mode,
5180 &rr_unsignedp, &volatilep, &rr_mask,
5183 /* It must be true that the inner operation on the lhs of each
5184 comparison must be the same if we are to be able to do anything.
5185 Then see if we have constants. If not, the same must be true for
5187 if (volatilep || ll_inner == 0 || rl_inner == 0
5188 || ! operand_equal_p (ll_inner, rl_inner, 0))
5191 if (TREE_CODE (lr_arg) == INTEGER_CST
5192 && TREE_CODE (rr_arg) == INTEGER_CST)
5193 l_const = lr_arg, r_const = rr_arg;
5194 else if (lr_inner == 0 || rr_inner == 0
5195 || ! operand_equal_p (lr_inner, rr_inner, 0))
5198 l_const = r_const = 0;
5200 /* If either comparison code is not correct for our logical operation,
5201 fail. However, we can convert a one-bit comparison against zero into
5202 the opposite comparison against that bit being set in the field. */
5204 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5205 if (lcode != wanted_code)
5207 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5209 /* Make the left operand unsigned, since we are only interested
5210 in the value of one bit. Otherwise we are doing the wrong
5219 /* This is analogous to the code for l_const above. */
5220 if (rcode != wanted_code)
5222 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5231 /* See if we can find a mode that contains both fields being compared on
5232 the left. If we can't, fail. Otherwise, update all constants and masks
5233 to be relative to a field of that size. */
5234 first_bit = MIN (ll_bitpos, rl_bitpos);
5235 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5236 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5237 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5239 if (lnmode == VOIDmode)
5242 lnbitsize = GET_MODE_BITSIZE (lnmode);
5243 lnbitpos = first_bit & ~ (lnbitsize - 1);
5244 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5245 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5247 if (BYTES_BIG_ENDIAN)
5249 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5250 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5253 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5254 size_int (xll_bitpos));
5255 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5256 size_int (xrl_bitpos));
5260 l_const = fold_convert_loc (loc, lntype, l_const);
5261 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5262 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5263 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5264 fold_build1_loc (loc, BIT_NOT_EXPR,
5267 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5269 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5274 r_const = fold_convert_loc (loc, lntype, r_const);
5275 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5276 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5277 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5278 fold_build1_loc (loc, BIT_NOT_EXPR,
5281 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5283 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5287 /* If the right sides are not constant, do the same for it. Also,
5288 disallow this optimization if a size or signedness mismatch occurs
5289 between the left and right sides. */
5292 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5293 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5294 /* Make sure the two fields on the right
5295 correspond to the left without being swapped. */
5296 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5299 first_bit = MIN (lr_bitpos, rr_bitpos);
5300 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5301 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5302 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5304 if (rnmode == VOIDmode)
5307 rnbitsize = GET_MODE_BITSIZE (rnmode);
5308 rnbitpos = first_bit & ~ (rnbitsize - 1);
5309 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5310 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5312 if (BYTES_BIG_ENDIAN)
5314 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5315 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5318 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5320 size_int (xlr_bitpos));
5321 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5323 size_int (xrr_bitpos));
5325 /* Make a mask that corresponds to both fields being compared.
5326 Do this for both items being compared. If the operands are the
5327 same size and the bits being compared are in the same position
5328 then we can do this by masking both and comparing the masked
5330 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5331 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5332 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5334 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5335 ll_unsignedp || rl_unsignedp);
5336 if (! all_ones_mask_p (ll_mask, lnbitsize))
5337 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5339 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5340 lr_unsignedp || rr_unsignedp);
5341 if (! all_ones_mask_p (lr_mask, rnbitsize))
5342 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5344 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5347 /* There is still another way we can do something: If both pairs of
5348 fields being compared are adjacent, we may be able to make a wider
5349 field containing them both.
5351 Note that we still must mask the lhs/rhs expressions. Furthermore,
5352 the mask must be shifted to account for the shift done by
5353 make_bit_field_ref. */
5354 if ((ll_bitsize + ll_bitpos == rl_bitpos
5355 && lr_bitsize + lr_bitpos == rr_bitpos)
5356 || (ll_bitpos == rl_bitpos + rl_bitsize
5357 && lr_bitpos == rr_bitpos + rr_bitsize))
5361 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5362 ll_bitsize + rl_bitsize,
5363 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5364 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5365 lr_bitsize + rr_bitsize,
5366 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5368 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5369 size_int (MIN (xll_bitpos, xrl_bitpos)));
5370 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5371 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5373 /* Convert to the smaller type before masking out unwanted bits. */
5375 if (lntype != rntype)
5377 if (lnbitsize > rnbitsize)
5379 lhs = fold_convert_loc (loc, rntype, lhs);
5380 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5383 else if (lnbitsize < rnbitsize)
5385 rhs = fold_convert_loc (loc, lntype, rhs);
5386 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5391 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5392 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5394 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5395 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5397 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5403 /* Handle the case of comparisons with constants. If there is something in
5404 common between the masks, those bits of the constants must be the same.
5405 If not, the condition is always false. Test for this to avoid generating
5406 incorrect code below. */
5407 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5408 if (! integer_zerop (result)
5409 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5410 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5412 if (wanted_code == NE_EXPR)
5414 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5415 return constant_boolean_node (true, truth_type);
5419 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5420 return constant_boolean_node (false, truth_type);
5424 /* Construct the expression we will return. First get the component
5425 reference we will make. Unless the mask is all ones the width of
5426 that field, perform the mask operation. Then compare with the
5428 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5429 ll_unsignedp || rl_unsignedp);
5431 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5432 if (! all_ones_mask_p (ll_mask, lnbitsize))
5433 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5435 return build2_loc (loc, wanted_code, truth_type, result,
5436 const_binop (BIT_IOR_EXPR, l_const, r_const));
5439 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5443 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5447 enum tree_code op_code;
5450 int consts_equal, consts_lt;
5453 STRIP_SIGN_NOPS (arg0);
5455 op_code = TREE_CODE (arg0);
5456 minmax_const = TREE_OPERAND (arg0, 1);
5457 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5458 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5459 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5460 inner = TREE_OPERAND (arg0, 0);
5462 /* If something does not permit us to optimize, return the original tree. */
5463 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5464 || TREE_CODE (comp_const) != INTEGER_CST
5465 || TREE_OVERFLOW (comp_const)
5466 || TREE_CODE (minmax_const) != INTEGER_CST
5467 || TREE_OVERFLOW (minmax_const))
5470 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5471 and GT_EXPR, doing the rest with recursive calls using logical
5475 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5478 = optimize_minmax_comparison (loc,
5479 invert_tree_comparison (code, false),
5482 return invert_truthvalue_loc (loc, tem);
5488 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5489 optimize_minmax_comparison
5490 (loc, EQ_EXPR, type, arg0, comp_const),
5491 optimize_minmax_comparison
5492 (loc, GT_EXPR, type, arg0, comp_const));
5495 if (op_code == MAX_EXPR && consts_equal)
5496 /* MAX (X, 0) == 0 -> X <= 0 */
5497 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5499 else if (op_code == MAX_EXPR && consts_lt)
5500 /* MAX (X, 0) == 5 -> X == 5 */
5501 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5503 else if (op_code == MAX_EXPR)
5504 /* MAX (X, 0) == -1 -> false */
5505 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5507 else if (consts_equal)
5508 /* MIN (X, 0) == 0 -> X >= 0 */
5509 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5512 /* MIN (X, 0) == 5 -> false */
5513 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5516 /* MIN (X, 0) == -1 -> X == -1 */
5517 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5520 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5521 /* MAX (X, 0) > 0 -> X > 0
5522 MAX (X, 0) > 5 -> X > 5 */
5523 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5525 else if (op_code == MAX_EXPR)
5526 /* MAX (X, 0) > -1 -> true */
5527 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5529 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5530 /* MIN (X, 0) > 0 -> false
5531 MIN (X, 0) > 5 -> false */
5532 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5535 /* MIN (X, 0) > -1 -> X > -1 */
5536 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5543 /* T is an integer expression that is being multiplied, divided, or taken a
5544 modulus (CODE says which and what kind of divide or modulus) by a
5545 constant C. See if we can eliminate that operation by folding it with
5546 other operations already in T. WIDE_TYPE, if non-null, is a type that
5547 should be used for the computation if wider than our type.
5549 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5550 (X * 2) + (Y * 4). We must, however, be assured that either the original
5551 expression would not overflow or that overflow is undefined for the type
5552 in the language in question.
5554 If we return a non-null expression, it is an equivalent form of the
5555 original computation, but need not be in the original type.
5557 We set *STRICT_OVERFLOW_P to true if the return values depends on
5558 signed overflow being undefined. Otherwise we do not change
5559 *STRICT_OVERFLOW_P. */
5562 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5563 bool *strict_overflow_p)
5565 /* To avoid exponential search depth, refuse to allow recursion past
5566 three levels. Beyond that (1) it's highly unlikely that we'll find
5567 something interesting and (2) we've probably processed it before
5568 when we built the inner expression. */
5577 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5584 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5585 bool *strict_overflow_p)
5587 tree type = TREE_TYPE (t);
5588 enum tree_code tcode = TREE_CODE (t);
5589 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5590 > GET_MODE_SIZE (TYPE_MODE (type)))
5591 ? wide_type : type);
5593 int same_p = tcode == code;
5594 tree op0 = NULL_TREE, op1 = NULL_TREE;
5595 bool sub_strict_overflow_p;
5597 /* Don't deal with constants of zero here; they confuse the code below. */
5598 if (integer_zerop (c))
5601 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5602 op0 = TREE_OPERAND (t, 0);
5604 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5605 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5607 /* Note that we need not handle conditional operations here since fold
5608 already handles those cases. So just do arithmetic here. */
5612 /* For a constant, we can always simplify if we are a multiply
5613 or (for divide and modulus) if it is a multiple of our constant. */
5614 if (code == MULT_EXPR
5615 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5616 return const_binop (code, fold_convert (ctype, t),
5617 fold_convert (ctype, c));
5620 CASE_CONVERT: case NON_LVALUE_EXPR:
5621 /* If op0 is an expression ... */
5622 if ((COMPARISON_CLASS_P (op0)
5623 || UNARY_CLASS_P (op0)
5624 || BINARY_CLASS_P (op0)
5625 || VL_EXP_CLASS_P (op0)
5626 || EXPRESSION_CLASS_P (op0))
5627 /* ... and has wrapping overflow, and its type is smaller
5628 than ctype, then we cannot pass through as widening. */
5629 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5630 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5631 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5632 && (TYPE_PRECISION (ctype)
5633 > TYPE_PRECISION (TREE_TYPE (op0))))
5634 /* ... or this is a truncation (t is narrower than op0),
5635 then we cannot pass through this narrowing. */
5636 || (TYPE_PRECISION (type)
5637 < TYPE_PRECISION (TREE_TYPE (op0)))
5638 /* ... or signedness changes for division or modulus,
5639 then we cannot pass through this conversion. */
5640 || (code != MULT_EXPR
5641 && (TYPE_UNSIGNED (ctype)
5642 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5643 /* ... or has undefined overflow while the converted to
5644 type has not, we cannot do the operation in the inner type
5645 as that would introduce undefined overflow. */
5646 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5647 && !TYPE_OVERFLOW_UNDEFINED (type))))
5650 /* Pass the constant down and see if we can make a simplification. If
5651 we can, replace this expression with the inner simplification for
5652 possible later conversion to our or some other type. */
5653 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5654 && TREE_CODE (t2) == INTEGER_CST
5655 && !TREE_OVERFLOW (t2)
5656 && (0 != (t1 = extract_muldiv (op0, t2, code,
5658 ? ctype : NULL_TREE,
5659 strict_overflow_p))))
5664 /* If widening the type changes it from signed to unsigned, then we
5665 must avoid building ABS_EXPR itself as unsigned. */
5666 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5668 tree cstype = (*signed_type_for) (ctype);
5669 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5672 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5673 return fold_convert (ctype, t1);
5677 /* If the constant is negative, we cannot simplify this. */
5678 if (tree_int_cst_sgn (c) == -1)
5682 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5684 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5687 case MIN_EXPR: case MAX_EXPR:
5688 /* If widening the type changes the signedness, then we can't perform
5689 this optimization as that changes the result. */
5690 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5693 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5694 sub_strict_overflow_p = false;
5695 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5696 &sub_strict_overflow_p)) != 0
5697 && (t2 = extract_muldiv (op1, c, code, wide_type,
5698 &sub_strict_overflow_p)) != 0)
5700 if (tree_int_cst_sgn (c) < 0)
5701 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5702 if (sub_strict_overflow_p)
5703 *strict_overflow_p = true;
5704 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5705 fold_convert (ctype, t2));
5709 case LSHIFT_EXPR: case RSHIFT_EXPR:
5710 /* If the second operand is constant, this is a multiplication
5711 or floor division, by a power of two, so we can treat it that
5712 way unless the multiplier or divisor overflows. Signed
5713 left-shift overflow is implementation-defined rather than
5714 undefined in C90, so do not convert signed left shift into
5716 if (TREE_CODE (op1) == INTEGER_CST
5717 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5718 /* const_binop may not detect overflow correctly,
5719 so check for it explicitly here. */
5720 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5721 && TREE_INT_CST_HIGH (op1) == 0
5722 && 0 != (t1 = fold_convert (ctype,
5723 const_binop (LSHIFT_EXPR,
5726 && !TREE_OVERFLOW (t1))
5727 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5728 ? MULT_EXPR : FLOOR_DIV_EXPR,
5730 fold_convert (ctype, op0),
5732 c, code, wide_type, strict_overflow_p);
5735 case PLUS_EXPR: case MINUS_EXPR:
5736 /* See if we can eliminate the operation on both sides. If we can, we
5737 can return a new PLUS or MINUS. If we can't, the only remaining
5738 cases where we can do anything are if the second operand is a
5740 sub_strict_overflow_p = false;
5741 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5742 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5743 if (t1 != 0 && t2 != 0
5744 && (code == MULT_EXPR
5745 /* If not multiplication, we can only do this if both operands
5746 are divisible by c. */
5747 || (multiple_of_p (ctype, op0, c)
5748 && multiple_of_p (ctype, op1, c))))
5750 if (sub_strict_overflow_p)
5751 *strict_overflow_p = true;
5752 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5753 fold_convert (ctype, t2));
5756 /* If this was a subtraction, negate OP1 and set it to be an addition.
5757 This simplifies the logic below. */
5758 if (tcode == MINUS_EXPR)
5760 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5761 /* If OP1 was not easily negatable, the constant may be OP0. */
5762 if (TREE_CODE (op0) == INTEGER_CST)
5773 if (TREE_CODE (op1) != INTEGER_CST)
5776 /* If either OP1 or C are negative, this optimization is not safe for
5777 some of the division and remainder types while for others we need
5778 to change the code. */
5779 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5781 if (code == CEIL_DIV_EXPR)
5782 code = FLOOR_DIV_EXPR;
5783 else if (code == FLOOR_DIV_EXPR)
5784 code = CEIL_DIV_EXPR;
5785 else if (code != MULT_EXPR
5786 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5790 /* If it's a multiply or a division/modulus operation of a multiple
5791 of our constant, do the operation and verify it doesn't overflow. */
5792 if (code == MULT_EXPR
5793 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5795 op1 = const_binop (code, fold_convert (ctype, op1),
5796 fold_convert (ctype, c));
5797 /* We allow the constant to overflow with wrapping semantics. */
5799 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5805 /* If we have an unsigned type is not a sizetype, we cannot widen
5806 the operation since it will change the result if the original
5807 computation overflowed. */
5808 if (TYPE_UNSIGNED (ctype)
5809 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5813 /* If we were able to eliminate our operation from the first side,
5814 apply our operation to the second side and reform the PLUS. */
5815 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5816 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5818 /* The last case is if we are a multiply. In that case, we can
5819 apply the distributive law to commute the multiply and addition
5820 if the multiplication of the constants doesn't overflow. */
5821 if (code == MULT_EXPR)
5822 return fold_build2 (tcode, ctype,
5823 fold_build2 (code, ctype,
5824 fold_convert (ctype, op0),
5825 fold_convert (ctype, c)),
5831 /* We have a special case here if we are doing something like
5832 (C * 8) % 4 since we know that's zero. */
5833 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5834 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5835 /* If the multiplication can overflow we cannot optimize this.
5836 ??? Until we can properly mark individual operations as
5837 not overflowing we need to treat sizetype special here as
5838 stor-layout relies on this opimization to make
5839 DECL_FIELD_BIT_OFFSET always a constant. */
5840 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5841 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5842 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5843 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5844 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5846 *strict_overflow_p = true;
5847 return omit_one_operand (type, integer_zero_node, op0);
5850 /* ... fall through ... */
5852 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5853 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5854 /* If we can extract our operation from the LHS, do so and return a
5855 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5856 do something only if the second operand is a constant. */
5858 && (t1 = extract_muldiv (op0, c, code, wide_type,
5859 strict_overflow_p)) != 0)
5860 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5861 fold_convert (ctype, op1));
5862 else if (tcode == MULT_EXPR && code == MULT_EXPR
5863 && (t1 = extract_muldiv (op1, c, code, wide_type,
5864 strict_overflow_p)) != 0)
5865 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5866 fold_convert (ctype, t1));
5867 else if (TREE_CODE (op1) != INTEGER_CST)
5870 /* If these are the same operation types, we can associate them
5871 assuming no overflow. */
5873 && 0 != (t1 = int_const_binop (MULT_EXPR,
5874 fold_convert (ctype, op1),
5875 fold_convert (ctype, c), 1))
5876 && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5877 (TYPE_UNSIGNED (ctype)
5878 && tcode != MULT_EXPR) ? -1 : 1,
5879 TREE_OVERFLOW (t1)))
5880 && !TREE_OVERFLOW (t1))
5881 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5883 /* If these operations "cancel" each other, we have the main
5884 optimizations of this pass, which occur when either constant is a
5885 multiple of the other, in which case we replace this with either an
5886 operation or CODE or TCODE.
5888 If we have an unsigned type that is not a sizetype, we cannot do
5889 this since it will change the result if the original computation
5891 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5892 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5893 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5894 || (tcode == MULT_EXPR
5895 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5896 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5897 && code != MULT_EXPR)))
5899 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5901 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5902 *strict_overflow_p = true;
5903 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5904 fold_convert (ctype,
5905 const_binop (TRUNC_DIV_EXPR,
5908 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5910 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5911 *strict_overflow_p = true;
5912 return fold_build2 (code, ctype, fold_convert (ctype, op0),
5913 fold_convert (ctype,
5914 const_binop (TRUNC_DIV_EXPR,
5927 /* Return a node which has the indicated constant VALUE (either 0 or
5928 1), and is of the indicated TYPE. */
5931 constant_boolean_node (int value, tree type)
5933 if (type == integer_type_node)
5934 return value ? integer_one_node : integer_zero_node;
5935 else if (type == boolean_type_node)
5936 return value ? boolean_true_node : boolean_false_node;
5938 return build_int_cst (type, value);
5942 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5943 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5944 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5945 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5946 COND is the first argument to CODE; otherwise (as in the example
5947 given here), it is the second argument. TYPE is the type of the
5948 original expression. Return NULL_TREE if no simplification is
5952 fold_binary_op_with_conditional_arg (location_t loc,
5953 enum tree_code code,
5954 tree type, tree op0, tree op1,
5955 tree cond, tree arg, int cond_first_p)
5957 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5958 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5959 tree test, true_value, false_value;
5960 tree lhs = NULL_TREE;
5961 tree rhs = NULL_TREE;
5963 if (TREE_CODE (cond) == COND_EXPR)
5965 test = TREE_OPERAND (cond, 0);
5966 true_value = TREE_OPERAND (cond, 1);
5967 false_value = TREE_OPERAND (cond, 2);
5968 /* If this operand throws an expression, then it does not make
5969 sense to try to perform a logical or arithmetic operation
5971 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5973 if (VOID_TYPE_P (TREE_TYPE (false_value)))
5978 tree testtype = TREE_TYPE (cond);
5980 true_value = constant_boolean_node (true, testtype);
5981 false_value = constant_boolean_node (false, testtype);
5984 /* This transformation is only worthwhile if we don't have to wrap ARG
5985 in a SAVE_EXPR and the operation can be simplified on at least one
5986 of the branches once its pushed inside the COND_EXPR. */
5987 if (!TREE_CONSTANT (arg)
5988 && (TREE_SIDE_EFFECTS (arg)
5989 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
5992 arg = fold_convert_loc (loc, arg_type, arg);
5995 true_value = fold_convert_loc (loc, cond_type, true_value);
5997 lhs = fold_build2_loc (loc, code, type, true_value, arg);
5999 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6003 false_value = fold_convert_loc (loc, cond_type, false_value);
6005 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6007 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6010 /* Check that we have simplified at least one of the branches. */
6011 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6014 return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6018 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6020 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6021 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6022 ADDEND is the same as X.
6024 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6025 and finite. The problematic cases are when X is zero, and its mode
6026 has signed zeros. In the case of rounding towards -infinity,
6027 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6028 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6031 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6033 if (!real_zerop (addend))
6036 /* Don't allow the fold with -fsignaling-nans. */
6037 if (HONOR_SNANS (TYPE_MODE (type)))
6040 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6041 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6044 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6045 if (TREE_CODE (addend) == REAL_CST
6046 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6049 /* The mode has signed zeros, and we have to honor their sign.
6050 In this situation, there is only one case we can return true for.
6051 X - 0 is the same as X unless rounding towards -infinity is
6053 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6056 /* Subroutine of fold() that checks comparisons of built-in math
6057 functions against real constants.
6059 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6060 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6061 is the type of the result and ARG0 and ARG1 are the operands of the
6062 comparison. ARG1 must be a TREE_REAL_CST.
6064 The function returns the constant folded tree if a simplification
6065 can be made, and NULL_TREE otherwise. */
6068 fold_mathfn_compare (location_t loc,
6069 enum built_in_function fcode, enum tree_code code,
6070 tree type, tree arg0, tree arg1)
6074 if (BUILTIN_SQRT_P (fcode))
6076 tree arg = CALL_EXPR_ARG (arg0, 0);
6077 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6079 c = TREE_REAL_CST (arg1);
6080 if (REAL_VALUE_NEGATIVE (c))
6082 /* sqrt(x) < y is always false, if y is negative. */
6083 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6084 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6086 /* sqrt(x) > y is always true, if y is negative and we
6087 don't care about NaNs, i.e. negative values of x. */
6088 if (code == NE_EXPR || !HONOR_NANS (mode))
6089 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6091 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6092 return fold_build2_loc (loc, GE_EXPR, type, arg,
6093 build_real (TREE_TYPE (arg), dconst0));
6095 else if (code == GT_EXPR || code == GE_EXPR)
6099 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6100 real_convert (&c2, mode, &c2);
6102 if (REAL_VALUE_ISINF (c2))
6104 /* sqrt(x) > y is x == +Inf, when y is very large. */
6105 if (HONOR_INFINITIES (mode))
6106 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6107 build_real (TREE_TYPE (arg), c2));
6109 /* sqrt(x) > y is always false, when y is very large
6110 and we don't care about infinities. */
6111 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6114 /* sqrt(x) > c is the same as x > c*c. */
6115 return fold_build2_loc (loc, code, type, arg,
6116 build_real (TREE_TYPE (arg), c2));
6118 else if (code == LT_EXPR || code == LE_EXPR)
6122 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6123 real_convert (&c2, mode, &c2);
6125 if (REAL_VALUE_ISINF (c2))
6127 /* sqrt(x) < y is always true, when y is a very large
6128 value and we don't care about NaNs or Infinities. */
6129 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6130 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6132 /* sqrt(x) < y is x != +Inf when y is very large and we
6133 don't care about NaNs. */
6134 if (! HONOR_NANS (mode))
6135 return fold_build2_loc (loc, NE_EXPR, type, arg,
6136 build_real (TREE_TYPE (arg), c2));
6138 /* sqrt(x) < y is x >= 0 when y is very large and we
6139 don't care about Infinities. */
6140 if (! HONOR_INFINITIES (mode))
6141 return fold_build2_loc (loc, GE_EXPR, type, arg,
6142 build_real (TREE_TYPE (arg), dconst0));
6144 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6145 if (lang_hooks.decls.global_bindings_p () != 0
6146 || CONTAINS_PLACEHOLDER_P (arg))
6149 arg = save_expr (arg);
6150 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6151 fold_build2_loc (loc, GE_EXPR, type, arg,
6152 build_real (TREE_TYPE (arg),
6154 fold_build2_loc (loc, NE_EXPR, type, arg,
6155 build_real (TREE_TYPE (arg),
6159 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6160 if (! HONOR_NANS (mode))
6161 return fold_build2_loc (loc, code, type, arg,
6162 build_real (TREE_TYPE (arg), c2));
6164 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6165 if (lang_hooks.decls.global_bindings_p () == 0
6166 && ! CONTAINS_PLACEHOLDER_P (arg))
6168 arg = save_expr (arg);
6169 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6170 fold_build2_loc (loc, GE_EXPR, type, arg,
6171 build_real (TREE_TYPE (arg),
6173 fold_build2_loc (loc, code, type, arg,
6174 build_real (TREE_TYPE (arg),
6183 /* Subroutine of fold() that optimizes comparisons against Infinities,
6184 either +Inf or -Inf.
6186 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6187 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6188 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6190 The function returns the constant folded tree if a simplification
6191 can be made, and NULL_TREE otherwise. */
6194 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6195 tree arg0, tree arg1)
6197 enum machine_mode mode;
6198 REAL_VALUE_TYPE max;
6202 mode = TYPE_MODE (TREE_TYPE (arg0));
6204 /* For negative infinity swap the sense of the comparison. */
6205 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6207 code = swap_tree_comparison (code);
6212 /* x > +Inf is always false, if with ignore sNANs. */
6213 if (HONOR_SNANS (mode))
6215 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6218 /* x <= +Inf is always true, if we don't case about NaNs. */
6219 if (! HONOR_NANS (mode))
6220 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6222 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6223 if (lang_hooks.decls.global_bindings_p () == 0
6224 && ! CONTAINS_PLACEHOLDER_P (arg0))
6226 arg0 = save_expr (arg0);
6227 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6233 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6234 real_maxval (&max, neg, mode);
6235 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6236 arg0, build_real (TREE_TYPE (arg0), max));
6239 /* x < +Inf is always equal to x <= DBL_MAX. */
6240 real_maxval (&max, neg, mode);
6241 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6242 arg0, build_real (TREE_TYPE (arg0), max));
6245 /* x != +Inf is always equal to !(x > DBL_MAX). */
6246 real_maxval (&max, neg, mode);
6247 if (! HONOR_NANS (mode))
6248 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6249 arg0, build_real (TREE_TYPE (arg0), max));
6251 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6252 arg0, build_real (TREE_TYPE (arg0), max));
6253 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6262 /* Subroutine of fold() that optimizes comparisons of a division by
6263 a nonzero integer constant against an integer constant, i.e.
6266 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6267 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6268 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6270 The function returns the constant folded tree if a simplification
6271 can be made, and NULL_TREE otherwise. */
6274 fold_div_compare (location_t loc,
6275 enum tree_code code, tree type, tree arg0, tree arg1)
6277 tree prod, tmp, hi, lo;
6278 tree arg00 = TREE_OPERAND (arg0, 0);
6279 tree arg01 = TREE_OPERAND (arg0, 1);
6281 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6285 /* We have to do this the hard way to detect unsigned overflow.
6286 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6287 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6288 TREE_INT_CST_HIGH (arg01),
6289 TREE_INT_CST_LOW (arg1),
6290 TREE_INT_CST_HIGH (arg1),
6291 &val.low, &val.high, unsigned_p);
6292 prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6293 neg_overflow = false;
6297 tmp = int_const_binop (MINUS_EXPR, arg01,
6298 build_int_cst (TREE_TYPE (arg01), 1), 0);
6301 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6302 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6303 TREE_INT_CST_HIGH (prod),
6304 TREE_INT_CST_LOW (tmp),
6305 TREE_INT_CST_HIGH (tmp),
6306 &val.low, &val.high, unsigned_p);
6307 hi = force_fit_type_double (TREE_TYPE (arg00), val,
6308 -1, overflow | TREE_OVERFLOW (prod));
6310 else if (tree_int_cst_sgn (arg01) >= 0)
6312 tmp = int_const_binop (MINUS_EXPR, arg01,
6313 build_int_cst (TREE_TYPE (arg01), 1), 0);
6314 switch (tree_int_cst_sgn (arg1))
6317 neg_overflow = true;
6318 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6323 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6328 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6338 /* A negative divisor reverses the relational operators. */
6339 code = swap_tree_comparison (code);
6341 tmp = int_const_binop (PLUS_EXPR, arg01,
6342 build_int_cst (TREE_TYPE (arg01), 1), 0);
6343 switch (tree_int_cst_sgn (arg1))
6346 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6351 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6356 neg_overflow = true;
6357 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6369 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6370 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6371 if (TREE_OVERFLOW (hi))
6372 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6373 if (TREE_OVERFLOW (lo))
6374 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6375 return build_range_check (loc, type, arg00, 1, lo, hi);
6378 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6379 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6380 if (TREE_OVERFLOW (hi))
6381 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6382 if (TREE_OVERFLOW (lo))
6383 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6384 return build_range_check (loc, type, arg00, 0, lo, hi);
6387 if (TREE_OVERFLOW (lo))
6389 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6390 return omit_one_operand_loc (loc, type, tmp, arg00);
6392 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6395 if (TREE_OVERFLOW (hi))
6397 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6398 return omit_one_operand_loc (loc, type, tmp, arg00);
6400 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6403 if (TREE_OVERFLOW (hi))
6405 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6406 return omit_one_operand_loc (loc, type, tmp, arg00);
6408 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6411 if (TREE_OVERFLOW (lo))
6413 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6414 return omit_one_operand_loc (loc, type, tmp, arg00);
6416 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6426 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6427 equality/inequality test, then return a simplified form of the test
6428 using a sign testing. Otherwise return NULL. TYPE is the desired
6432 fold_single_bit_test_into_sign_test (location_t loc,
6433 enum tree_code code, tree arg0, tree arg1,
6436 /* If this is testing a single bit, we can optimize the test. */
6437 if ((code == NE_EXPR || code == EQ_EXPR)
6438 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6439 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6441 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6442 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6443 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6445 if (arg00 != NULL_TREE
6446 /* This is only a win if casting to a signed type is cheap,
6447 i.e. when arg00's type is not a partial mode. */
6448 && TYPE_PRECISION (TREE_TYPE (arg00))
6449 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6451 tree stype = signed_type_for (TREE_TYPE (arg00));
6452 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6454 fold_convert_loc (loc, stype, arg00),
6455 build_int_cst (stype, 0));
6462 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6463 equality/inequality test, then return a simplified form of
6464 the test using shifts and logical operations. Otherwise return
6465 NULL. TYPE is the desired result type. */
6468 fold_single_bit_test (location_t loc, enum tree_code code,
6469 tree arg0, tree arg1, tree result_type)
6471 /* If this is testing a single bit, we can optimize the test. */
6472 if ((code == NE_EXPR || code == EQ_EXPR)
6473 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6474 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6476 tree inner = TREE_OPERAND (arg0, 0);
6477 tree type = TREE_TYPE (arg0);
6478 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6479 enum machine_mode operand_mode = TYPE_MODE (type);
6481 tree signed_type, unsigned_type, intermediate_type;
6484 /* First, see if we can fold the single bit test into a sign-bit
6486 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6491 /* Otherwise we have (A & C) != 0 where C is a single bit,
6492 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6493 Similarly for (A & C) == 0. */
6495 /* If INNER is a right shift of a constant and it plus BITNUM does
6496 not overflow, adjust BITNUM and INNER. */
6497 if (TREE_CODE (inner) == RSHIFT_EXPR
6498 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6499 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6500 && bitnum < TYPE_PRECISION (type)
6501 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6502 bitnum - TYPE_PRECISION (type)))
6504 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6505 inner = TREE_OPERAND (inner, 0);
6508 /* If we are going to be able to omit the AND below, we must do our
6509 operations as unsigned. If we must use the AND, we have a choice.
6510 Normally unsigned is faster, but for some machines signed is. */
6511 #ifdef LOAD_EXTEND_OP
6512 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6513 && !flag_syntax_only) ? 0 : 1;
6518 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6519 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6520 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6521 inner = fold_convert_loc (loc, intermediate_type, inner);
6524 inner = build2 (RSHIFT_EXPR, intermediate_type,
6525 inner, size_int (bitnum));
6527 one = build_int_cst (intermediate_type, 1);
6529 if (code == EQ_EXPR)
6530 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6532 /* Put the AND last so it can combine with more things. */
6533 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6535 /* Make sure to return the proper type. */
6536 inner = fold_convert_loc (loc, result_type, inner);
6543 /* Check whether we are allowed to reorder operands arg0 and arg1,
6544 such that the evaluation of arg1 occurs before arg0. */
6547 reorder_operands_p (const_tree arg0, const_tree arg1)
6549 if (! flag_evaluation_order)
6551 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6553 return ! TREE_SIDE_EFFECTS (arg0)
6554 && ! TREE_SIDE_EFFECTS (arg1);
6557 /* Test whether it is preferable two swap two operands, ARG0 and
6558 ARG1, for example because ARG0 is an integer constant and ARG1
6559 isn't. If REORDER is true, only recommend swapping if we can
6560 evaluate the operands in reverse order. */
6563 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6565 STRIP_SIGN_NOPS (arg0);
6566 STRIP_SIGN_NOPS (arg1);
6568 if (TREE_CODE (arg1) == INTEGER_CST)
6570 if (TREE_CODE (arg0) == INTEGER_CST)
6573 if (TREE_CODE (arg1) == REAL_CST)
6575 if (TREE_CODE (arg0) == REAL_CST)
6578 if (TREE_CODE (arg1) == FIXED_CST)
6580 if (TREE_CODE (arg0) == FIXED_CST)
6583 if (TREE_CODE (arg1) == COMPLEX_CST)
6585 if (TREE_CODE (arg0) == COMPLEX_CST)
6588 if (TREE_CONSTANT (arg1))
6590 if (TREE_CONSTANT (arg0))
6593 if (optimize_function_for_size_p (cfun))
6596 if (reorder && flag_evaluation_order
6597 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6600 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6601 for commutative and comparison operators. Ensuring a canonical
6602 form allows the optimizers to find additional redundancies without
6603 having to explicitly check for both orderings. */
6604 if (TREE_CODE (arg0) == SSA_NAME
6605 && TREE_CODE (arg1) == SSA_NAME
6606 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6609 /* Put SSA_NAMEs last. */
6610 if (TREE_CODE (arg1) == SSA_NAME)
6612 if (TREE_CODE (arg0) == SSA_NAME)
6615 /* Put variables last. */
6624 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6625 ARG0 is extended to a wider type. */
6628 fold_widened_comparison (location_t loc, enum tree_code code,
6629 tree type, tree arg0, tree arg1)
6631 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6633 tree shorter_type, outer_type;
6637 if (arg0_unw == arg0)
6639 shorter_type = TREE_TYPE (arg0_unw);
6641 #ifdef HAVE_canonicalize_funcptr_for_compare
6642 /* Disable this optimization if we're casting a function pointer
6643 type on targets that require function pointer canonicalization. */
6644 if (HAVE_canonicalize_funcptr_for_compare
6645 && TREE_CODE (shorter_type) == POINTER_TYPE
6646 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6650 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6653 arg1_unw = get_unwidened (arg1, NULL_TREE);
6655 /* If possible, express the comparison in the shorter mode. */
6656 if ((code == EQ_EXPR || code == NE_EXPR
6657 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6658 && (TREE_TYPE (arg1_unw) == shorter_type
6659 || ((TYPE_PRECISION (shorter_type)
6660 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6661 && (TYPE_UNSIGNED (shorter_type)
6662 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6663 || (TREE_CODE (arg1_unw) == INTEGER_CST
6664 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6665 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6666 && int_fits_type_p (arg1_unw, shorter_type))))
6667 return fold_build2_loc (loc, code, type, arg0_unw,
6668 fold_convert_loc (loc, shorter_type, arg1_unw));
6670 if (TREE_CODE (arg1_unw) != INTEGER_CST
6671 || TREE_CODE (shorter_type) != INTEGER_TYPE
6672 || !int_fits_type_p (arg1_unw, shorter_type))
6675 /* If we are comparing with the integer that does not fit into the range
6676 of the shorter type, the result is known. */
6677 outer_type = TREE_TYPE (arg1_unw);
6678 min = lower_bound_in_type (outer_type, shorter_type);
6679 max = upper_bound_in_type (outer_type, shorter_type);
6681 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6683 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6690 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6695 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6701 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6703 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6708 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6710 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6719 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6720 ARG0 just the signedness is changed. */
6723 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6724 tree arg0, tree arg1)
6727 tree inner_type, outer_type;
6729 if (!CONVERT_EXPR_P (arg0))
6732 outer_type = TREE_TYPE (arg0);
6733 arg0_inner = TREE_OPERAND (arg0, 0);
6734 inner_type = TREE_TYPE (arg0_inner);
6736 #ifdef HAVE_canonicalize_funcptr_for_compare
6737 /* Disable this optimization if we're casting a function pointer
6738 type on targets that require function pointer canonicalization. */
6739 if (HAVE_canonicalize_funcptr_for_compare
6740 && TREE_CODE (inner_type) == POINTER_TYPE
6741 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6745 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6748 if (TREE_CODE (arg1) != INTEGER_CST
6749 && !(CONVERT_EXPR_P (arg1)
6750 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6753 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6754 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6759 if (TREE_CODE (arg1) == INTEGER_CST)
6760 arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6761 0, TREE_OVERFLOW (arg1));
6763 arg1 = fold_convert_loc (loc, inner_type, arg1);
6765 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6768 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6769 step of the array. Reconstructs s and delta in the case of s *
6770 delta being an integer constant (and thus already folded). ADDR is
6771 the address. MULT is the multiplicative expression. If the
6772 function succeeds, the new address expression is returned.
6773 Otherwise NULL_TREE is returned. LOC is the location of the
6774 resulting expression. */
6777 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6779 tree s, delta, step;
6780 tree ref = TREE_OPERAND (addr, 0), pref;
6785 /* Strip the nops that might be added when converting op1 to sizetype. */
6788 /* Canonicalize op1 into a possibly non-constant delta
6789 and an INTEGER_CST s. */
6790 if (TREE_CODE (op1) == MULT_EXPR)
6792 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6797 if (TREE_CODE (arg0) == INTEGER_CST)
6802 else if (TREE_CODE (arg1) == INTEGER_CST)
6810 else if (TREE_CODE (op1) == INTEGER_CST)
6817 /* Simulate we are delta * 1. */
6819 s = integer_one_node;
6822 for (;; ref = TREE_OPERAND (ref, 0))
6824 if (TREE_CODE (ref) == ARRAY_REF)
6828 /* Remember if this was a multi-dimensional array. */
6829 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6832 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6835 itype = TREE_TYPE (domain);
6837 step = array_ref_element_size (ref);
6838 if (TREE_CODE (step) != INTEGER_CST)
6843 if (! tree_int_cst_equal (step, s))
6848 /* Try if delta is a multiple of step. */
6849 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6855 /* Only fold here if we can verify we do not overflow one
6856 dimension of a multi-dimensional array. */
6861 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6862 || !TYPE_MAX_VALUE (domain)
6863 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6866 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6867 fold_convert_loc (loc, itype,
6868 TREE_OPERAND (ref, 1)),
6869 fold_convert_loc (loc, itype, delta));
6871 || TREE_CODE (tmp) != INTEGER_CST
6872 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6881 if (!handled_component_p (ref))
6885 /* We found the suitable array reference. So copy everything up to it,
6886 and replace the index. */
6888 pref = TREE_OPERAND (addr, 0);
6889 ret = copy_node (pref);
6890 SET_EXPR_LOCATION (ret, loc);
6895 pref = TREE_OPERAND (pref, 0);
6896 TREE_OPERAND (pos, 0) = copy_node (pref);
6897 pos = TREE_OPERAND (pos, 0);
6900 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6901 fold_convert_loc (loc, itype,
6902 TREE_OPERAND (pos, 1)),
6903 fold_convert_loc (loc, itype, delta));
6905 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6909 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6910 means A >= Y && A != MAX, but in this case we know that
6911 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6914 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6916 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6918 if (TREE_CODE (bound) == LT_EXPR)
6919 a = TREE_OPERAND (bound, 0);
6920 else if (TREE_CODE (bound) == GT_EXPR)
6921 a = TREE_OPERAND (bound, 1);
6925 typea = TREE_TYPE (a);
6926 if (!INTEGRAL_TYPE_P (typea)
6927 && !POINTER_TYPE_P (typea))
6930 if (TREE_CODE (ineq) == LT_EXPR)
6932 a1 = TREE_OPERAND (ineq, 1);
6933 y = TREE_OPERAND (ineq, 0);
6935 else if (TREE_CODE (ineq) == GT_EXPR)
6937 a1 = TREE_OPERAND (ineq, 0);
6938 y = TREE_OPERAND (ineq, 1);
6943 if (TREE_TYPE (a1) != typea)
6946 if (POINTER_TYPE_P (typea))
6948 /* Convert the pointer types into integer before taking the difference. */
6949 tree ta = fold_convert_loc (loc, ssizetype, a);
6950 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6951 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6954 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6956 if (!diff || !integer_onep (diff))
6959 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6962 /* Fold a sum or difference of at least one multiplication.
6963 Returns the folded tree or NULL if no simplification could be made. */
6966 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6967 tree arg0, tree arg1)
6969 tree arg00, arg01, arg10, arg11;
6970 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6972 /* (A * C) +- (B * C) -> (A+-B) * C.
6973 (A * C) +- A -> A * (C+-1).
6974 We are most concerned about the case where C is a constant,
6975 but other combinations show up during loop reduction. Since
6976 it is not difficult, try all four possibilities. */
6978 if (TREE_CODE (arg0) == MULT_EXPR)
6980 arg00 = TREE_OPERAND (arg0, 0);
6981 arg01 = TREE_OPERAND (arg0, 1);
6983 else if (TREE_CODE (arg0) == INTEGER_CST)
6985 arg00 = build_one_cst (type);
6990 /* We cannot generate constant 1 for fract. */
6991 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6994 arg01 = build_one_cst (type);
6996 if (TREE_CODE (arg1) == MULT_EXPR)
6998 arg10 = TREE_OPERAND (arg1, 0);
6999 arg11 = TREE_OPERAND (arg1, 1);
7001 else if (TREE_CODE (arg1) == INTEGER_CST)
7003 arg10 = build_one_cst (type);
7004 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7005 the purpose of this canonicalization. */
7006 if (TREE_INT_CST_HIGH (arg1) == -1
7007 && negate_expr_p (arg1)
7008 && code == PLUS_EXPR)
7010 arg11 = negate_expr (arg1);
7018 /* We cannot generate constant 1 for fract. */
7019 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7022 arg11 = build_one_cst (type);
7026 if (operand_equal_p (arg01, arg11, 0))
7027 same = arg01, alt0 = arg00, alt1 = arg10;
7028 else if (operand_equal_p (arg00, arg10, 0))
7029 same = arg00, alt0 = arg01, alt1 = arg11;
7030 else if (operand_equal_p (arg00, arg11, 0))
7031 same = arg00, alt0 = arg01, alt1 = arg10;
7032 else if (operand_equal_p (arg01, arg10, 0))
7033 same = arg01, alt0 = arg00, alt1 = arg11;
7035 /* No identical multiplicands; see if we can find a common
7036 power-of-two factor in non-power-of-two multiplies. This
7037 can help in multi-dimensional array access. */
7038 else if (host_integerp (arg01, 0)
7039 && host_integerp (arg11, 0))
7041 HOST_WIDE_INT int01, int11, tmp;
7044 int01 = TREE_INT_CST_LOW (arg01);
7045 int11 = TREE_INT_CST_LOW (arg11);
7047 /* Move min of absolute values to int11. */
7048 if ((int01 >= 0 ? int01 : -int01)
7049 < (int11 >= 0 ? int11 : -int11))
7051 tmp = int01, int01 = int11, int11 = tmp;
7052 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7059 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7060 /* The remainder should not be a constant, otherwise we
7061 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7062 increased the number of multiplications necessary. */
7063 && TREE_CODE (arg10) != INTEGER_CST)
7065 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7066 build_int_cst (TREE_TYPE (arg00),
7071 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7076 return fold_build2_loc (loc, MULT_EXPR, type,
7077 fold_build2_loc (loc, code, type,
7078 fold_convert_loc (loc, type, alt0),
7079 fold_convert_loc (loc, type, alt1)),
7080 fold_convert_loc (loc, type, same));
7085 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7086 specified by EXPR into the buffer PTR of length LEN bytes.
7087 Return the number of bytes placed in the buffer, or zero
7091 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7093 tree type = TREE_TYPE (expr);
7094 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7095 int byte, offset, word, words;
7096 unsigned char value;
7098 if (total_bytes > len)
7100 words = total_bytes / UNITS_PER_WORD;
7102 for (byte = 0; byte < total_bytes; byte++)
7104 int bitpos = byte * BITS_PER_UNIT;
7105 if (bitpos < HOST_BITS_PER_WIDE_INT)
7106 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7108 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7109 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7111 if (total_bytes > UNITS_PER_WORD)
7113 word = byte / UNITS_PER_WORD;
7114 if (WORDS_BIG_ENDIAN)
7115 word = (words - 1) - word;
7116 offset = word * UNITS_PER_WORD;
7117 if (BYTES_BIG_ENDIAN)
7118 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7120 offset += byte % UNITS_PER_WORD;
7123 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7124 ptr[offset] = value;
7130 /* Subroutine of native_encode_expr. Encode the REAL_CST
7131 specified by EXPR into the buffer PTR of length LEN bytes.
7132 Return the number of bytes placed in the buffer, or zero
7136 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7138 tree type = TREE_TYPE (expr);
7139 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7140 int byte, offset, word, words, bitpos;
7141 unsigned char value;
7143 /* There are always 32 bits in each long, no matter the size of
7144 the hosts long. We handle floating point representations with
7148 if (total_bytes > len)
7150 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7152 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7154 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7155 bitpos += BITS_PER_UNIT)
7157 byte = (bitpos / BITS_PER_UNIT) & 3;
7158 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7160 if (UNITS_PER_WORD < 4)
7162 word = byte / UNITS_PER_WORD;
7163 if (WORDS_BIG_ENDIAN)
7164 word = (words - 1) - word;
7165 offset = word * UNITS_PER_WORD;
7166 if (BYTES_BIG_ENDIAN)
7167 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7169 offset += byte % UNITS_PER_WORD;
7172 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7173 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7178 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7179 specified by EXPR into the buffer PTR of length LEN bytes.
7180 Return the number of bytes placed in the buffer, or zero
7184 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7189 part = TREE_REALPART (expr);
7190 rsize = native_encode_expr (part, ptr, len);
7193 part = TREE_IMAGPART (expr);
7194 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7197 return rsize + isize;
7201 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7202 specified by EXPR into the buffer PTR of length LEN bytes.
7203 Return the number of bytes placed in the buffer, or zero
7207 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7209 int i, size, offset, count;
7210 tree itype, elem, elements;
7213 elements = TREE_VECTOR_CST_ELTS (expr);
7214 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7215 itype = TREE_TYPE (TREE_TYPE (expr));
7216 size = GET_MODE_SIZE (TYPE_MODE (itype));
7217 for (i = 0; i < count; i++)
7221 elem = TREE_VALUE (elements);
7222 elements = TREE_CHAIN (elements);
7229 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7234 if (offset + size > len)
7236 memset (ptr+offset, 0, size);
7244 /* Subroutine of native_encode_expr. Encode the STRING_CST
7245 specified by EXPR into the buffer PTR of length LEN bytes.
7246 Return the number of bytes placed in the buffer, or zero
7250 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7252 tree type = TREE_TYPE (expr);
7253 HOST_WIDE_INT total_bytes;
7255 if (TREE_CODE (type) != ARRAY_TYPE
7256 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7257 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7258 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7260 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7261 if (total_bytes > len)
7263 if (TREE_STRING_LENGTH (expr) < total_bytes)
7265 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7266 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7267 total_bytes - TREE_STRING_LENGTH (expr));
7270 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7275 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7276 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7277 buffer PTR of length LEN bytes. Return the number of bytes
7278 placed in the buffer, or zero upon failure. */
7281 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7283 switch (TREE_CODE (expr))
7286 return native_encode_int (expr, ptr, len);
7289 return native_encode_real (expr, ptr, len);
7292 return native_encode_complex (expr, ptr, len);
7295 return native_encode_vector (expr, ptr, len);
7298 return native_encode_string (expr, ptr, len);
7306 /* Subroutine of native_interpret_expr. Interpret the contents of
7307 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7308 If the buffer cannot be interpreted, return NULL_TREE. */
7311 native_interpret_int (tree type, const unsigned char *ptr, int len)
7313 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7314 int byte, offset, word, words;
7315 unsigned char value;
7318 if (total_bytes > len)
7320 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7323 result = double_int_zero;
7324 words = total_bytes / UNITS_PER_WORD;
7326 for (byte = 0; byte < total_bytes; byte++)
7328 int bitpos = byte * BITS_PER_UNIT;
7329 if (total_bytes > UNITS_PER_WORD)
7331 word = byte / UNITS_PER_WORD;
7332 if (WORDS_BIG_ENDIAN)
7333 word = (words - 1) - word;
7334 offset = word * UNITS_PER_WORD;
7335 if (BYTES_BIG_ENDIAN)
7336 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7338 offset += byte % UNITS_PER_WORD;
7341 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7342 value = ptr[offset];
7344 if (bitpos < HOST_BITS_PER_WIDE_INT)
7345 result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7347 result.high |= (unsigned HOST_WIDE_INT) value
7348 << (bitpos - HOST_BITS_PER_WIDE_INT);
7351 return double_int_to_tree (type, result);
7355 /* Subroutine of native_interpret_expr. Interpret the contents of
7356 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7357 If the buffer cannot be interpreted, return NULL_TREE. */
7360 native_interpret_real (tree type, const unsigned char *ptr, int len)
7362 enum machine_mode mode = TYPE_MODE (type);
7363 int total_bytes = GET_MODE_SIZE (mode);
7364 int byte, offset, word, words, bitpos;
7365 unsigned char value;
7366 /* There are always 32 bits in each long, no matter the size of
7367 the hosts long. We handle floating point representations with
7372 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7373 if (total_bytes > len || total_bytes > 24)
7375 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7377 memset (tmp, 0, sizeof (tmp));
7378 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7379 bitpos += BITS_PER_UNIT)
7381 byte = (bitpos / BITS_PER_UNIT) & 3;
7382 if (UNITS_PER_WORD < 4)
7384 word = byte / UNITS_PER_WORD;
7385 if (WORDS_BIG_ENDIAN)
7386 word = (words - 1) - word;
7387 offset = word * UNITS_PER_WORD;
7388 if (BYTES_BIG_ENDIAN)
7389 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7391 offset += byte % UNITS_PER_WORD;
7394 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7395 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7397 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7400 real_from_target (&r, tmp, mode);
7401 return build_real (type, r);
7405 /* Subroutine of native_interpret_expr. Interpret the contents of
7406 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7407 If the buffer cannot be interpreted, return NULL_TREE. */
7410 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7412 tree etype, rpart, ipart;
7415 etype = TREE_TYPE (type);
7416 size = GET_MODE_SIZE (TYPE_MODE (etype));
7419 rpart = native_interpret_expr (etype, ptr, size);
7422 ipart = native_interpret_expr (etype, ptr+size, size);
7425 return build_complex (type, rpart, ipart);
7429 /* Subroutine of native_interpret_expr. Interpret the contents of
7430 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7431 If the buffer cannot be interpreted, return NULL_TREE. */
7434 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7436 tree etype, elem, elements;
7439 etype = TREE_TYPE (type);
7440 size = GET_MODE_SIZE (TYPE_MODE (etype));
7441 count = TYPE_VECTOR_SUBPARTS (type);
7442 if (size * count > len)
7445 elements = NULL_TREE;
7446 for (i = count - 1; i >= 0; i--)
7448 elem = native_interpret_expr (etype, ptr+(i*size), size);
7451 elements = tree_cons (NULL_TREE, elem, elements);
7453 return build_vector (type, elements);
7457 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7458 the buffer PTR of length LEN as a constant of type TYPE. For
7459 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7460 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7461 return NULL_TREE. */
7464 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7466 switch (TREE_CODE (type))
7471 return native_interpret_int (type, ptr, len);
7474 return native_interpret_real (type, ptr, len);
7477 return native_interpret_complex (type, ptr, len);
7480 return native_interpret_vector (type, ptr, len);
7488 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7489 TYPE at compile-time. If we're unable to perform the conversion
7490 return NULL_TREE. */
7493 fold_view_convert_expr (tree type, tree expr)
7495 /* We support up to 512-bit values (for V8DFmode). */
7496 unsigned char buffer[64];
7499 /* Check that the host and target are sane. */
7500 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7503 len = native_encode_expr (expr, buffer, sizeof (buffer));
7507 return native_interpret_expr (type, buffer, len);
7510 /* Build an expression for the address of T. Folds away INDIRECT_REF
7511 to avoid confusing the gimplify process. */
7514 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7516 /* The size of the object is not relevant when talking about its address. */
7517 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7518 t = TREE_OPERAND (t, 0);
7520 if (TREE_CODE (t) == INDIRECT_REF)
7522 t = TREE_OPERAND (t, 0);
7524 if (TREE_TYPE (t) != ptrtype)
7525 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7527 else if (TREE_CODE (t) == MEM_REF
7528 && integer_zerop (TREE_OPERAND (t, 1)))
7529 return TREE_OPERAND (t, 0);
7530 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7532 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7534 if (TREE_TYPE (t) != ptrtype)
7535 t = fold_convert_loc (loc, ptrtype, t);
7538 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7543 /* Build an expression for the address of T. */
7546 build_fold_addr_expr_loc (location_t loc, tree t)
7548 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7550 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7553 /* Fold a unary expression of code CODE and type TYPE with operand
7554 OP0. Return the folded expression if folding is successful.
7555 Otherwise, return NULL_TREE. */
7558 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7562 enum tree_code_class kind = TREE_CODE_CLASS (code);
7564 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7565 && TREE_CODE_LENGTH (code) == 1);
7570 if (CONVERT_EXPR_CODE_P (code)
7571 || code == FLOAT_EXPR || code == ABS_EXPR)
7573 /* Don't use STRIP_NOPS, because signedness of argument type
7575 STRIP_SIGN_NOPS (arg0);
7579 /* Strip any conversions that don't change the mode. This
7580 is safe for every expression, except for a comparison
7581 expression because its signedness is derived from its
7584 Note that this is done as an internal manipulation within
7585 the constant folder, in order to find the simplest
7586 representation of the arguments so that their form can be
7587 studied. In any cases, the appropriate type conversions
7588 should be put back in the tree that will get out of the
7594 if (TREE_CODE_CLASS (code) == tcc_unary)
7596 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7597 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7598 fold_build1_loc (loc, code, type,
7599 fold_convert_loc (loc, TREE_TYPE (op0),
7600 TREE_OPERAND (arg0, 1))));
7601 else if (TREE_CODE (arg0) == COND_EXPR)
7603 tree arg01 = TREE_OPERAND (arg0, 1);
7604 tree arg02 = TREE_OPERAND (arg0, 2);
7605 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7606 arg01 = fold_build1_loc (loc, code, type,
7607 fold_convert_loc (loc,
7608 TREE_TYPE (op0), arg01));
7609 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7610 arg02 = fold_build1_loc (loc, code, type,
7611 fold_convert_loc (loc,
7612 TREE_TYPE (op0), arg02));
7613 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7616 /* If this was a conversion, and all we did was to move into
7617 inside the COND_EXPR, bring it back out. But leave it if
7618 it is a conversion from integer to integer and the
7619 result precision is no wider than a word since such a
7620 conversion is cheap and may be optimized away by combine,
7621 while it couldn't if it were outside the COND_EXPR. Then return
7622 so we don't get into an infinite recursion loop taking the
7623 conversion out and then back in. */
7625 if ((CONVERT_EXPR_CODE_P (code)
7626 || code == NON_LVALUE_EXPR)
7627 && TREE_CODE (tem) == COND_EXPR
7628 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7629 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7630 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7631 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7632 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7633 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7634 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7636 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7637 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7638 || flag_syntax_only))
7639 tem = build1_loc (loc, code, type,
7641 TREE_TYPE (TREE_OPERAND
7642 (TREE_OPERAND (tem, 1), 0)),
7643 TREE_OPERAND (tem, 0),
7644 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7645 TREE_OPERAND (TREE_OPERAND (tem, 2),
7649 else if (COMPARISON_CLASS_P (arg0))
7651 if (TREE_CODE (type) == BOOLEAN_TYPE)
7653 arg0 = copy_node (arg0);
7654 TREE_TYPE (arg0) = type;
7657 else if (TREE_CODE (type) != INTEGER_TYPE)
7658 return fold_build3_loc (loc, COND_EXPR, type, arg0,
7659 fold_build1_loc (loc, code, type,
7661 fold_build1_loc (loc, code, type,
7662 integer_zero_node));
7669 /* Re-association barriers around constants and other re-association
7670 barriers can be removed. */
7671 if (CONSTANT_CLASS_P (op0)
7672 || TREE_CODE (op0) == PAREN_EXPR)
7673 return fold_convert_loc (loc, type, op0);
7678 case FIX_TRUNC_EXPR:
7679 if (TREE_TYPE (op0) == type)
7682 /* If we have (type) (a CMP b) and type is an integral type, return
7683 new expression involving the new type. */
7684 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7685 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7686 TREE_OPERAND (op0, 1));
7688 /* Handle cases of two conversions in a row. */
7689 if (CONVERT_EXPR_P (op0))
7691 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7692 tree inter_type = TREE_TYPE (op0);
7693 int inside_int = INTEGRAL_TYPE_P (inside_type);
7694 int inside_ptr = POINTER_TYPE_P (inside_type);
7695 int inside_float = FLOAT_TYPE_P (inside_type);
7696 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7697 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7698 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7699 int inter_int = INTEGRAL_TYPE_P (inter_type);
7700 int inter_ptr = POINTER_TYPE_P (inter_type);
7701 int inter_float = FLOAT_TYPE_P (inter_type);
7702 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7703 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7704 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7705 int final_int = INTEGRAL_TYPE_P (type);
7706 int final_ptr = POINTER_TYPE_P (type);
7707 int final_float = FLOAT_TYPE_P (type);
7708 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7709 unsigned int final_prec = TYPE_PRECISION (type);
7710 int final_unsignedp = TYPE_UNSIGNED (type);
7712 /* In addition to the cases of two conversions in a row
7713 handled below, if we are converting something to its own
7714 type via an object of identical or wider precision, neither
7715 conversion is needed. */
7716 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7717 && (((inter_int || inter_ptr) && final_int)
7718 || (inter_float && final_float))
7719 && inter_prec >= final_prec)
7720 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7722 /* Likewise, if the intermediate and initial types are either both
7723 float or both integer, we don't need the middle conversion if the
7724 former is wider than the latter and doesn't change the signedness
7725 (for integers). Avoid this if the final type is a pointer since
7726 then we sometimes need the middle conversion. Likewise if the
7727 final type has a precision not equal to the size of its mode. */
7728 if (((inter_int && inside_int)
7729 || (inter_float && inside_float)
7730 || (inter_vec && inside_vec))
7731 && inter_prec >= inside_prec
7732 && (inter_float || inter_vec
7733 || inter_unsignedp == inside_unsignedp)
7734 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7735 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7737 && (! final_vec || inter_prec == inside_prec))
7738 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7740 /* If we have a sign-extension of a zero-extended value, we can
7741 replace that by a single zero-extension. */
7742 if (inside_int && inter_int && final_int
7743 && inside_prec < inter_prec && inter_prec < final_prec
7744 && inside_unsignedp && !inter_unsignedp)
7745 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7747 /* Two conversions in a row are not needed unless:
7748 - some conversion is floating-point (overstrict for now), or
7749 - some conversion is a vector (overstrict for now), or
7750 - the intermediate type is narrower than both initial and
7752 - the intermediate type and innermost type differ in signedness,
7753 and the outermost type is wider than the intermediate, or
7754 - the initial type is a pointer type and the precisions of the
7755 intermediate and final types differ, or
7756 - the final type is a pointer type and the precisions of the
7757 initial and intermediate types differ. */
7758 if (! inside_float && ! inter_float && ! final_float
7759 && ! inside_vec && ! inter_vec && ! final_vec
7760 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7761 && ! (inside_int && inter_int
7762 && inter_unsignedp != inside_unsignedp
7763 && inter_prec < final_prec)
7764 && ((inter_unsignedp && inter_prec > inside_prec)
7765 == (final_unsignedp && final_prec > inter_prec))
7766 && ! (inside_ptr && inter_prec != final_prec)
7767 && ! (final_ptr && inside_prec != inter_prec)
7768 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7769 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7770 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7773 /* Handle (T *)&A.B.C for A being of type T and B and C
7774 living at offset zero. This occurs frequently in
7775 C++ upcasting and then accessing the base. */
7776 if (TREE_CODE (op0) == ADDR_EXPR
7777 && POINTER_TYPE_P (type)
7778 && handled_component_p (TREE_OPERAND (op0, 0)))
7780 HOST_WIDE_INT bitsize, bitpos;
7782 enum machine_mode mode;
7783 int unsignedp, volatilep;
7784 tree base = TREE_OPERAND (op0, 0);
7785 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7786 &mode, &unsignedp, &volatilep, false);
7787 /* If the reference was to a (constant) zero offset, we can use
7788 the address of the base if it has the same base type
7789 as the result type and the pointer type is unqualified. */
7790 if (! offset && bitpos == 0
7791 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7792 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7793 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7794 return fold_convert_loc (loc, type,
7795 build_fold_addr_expr_loc (loc, base));
7798 if (TREE_CODE (op0) == MODIFY_EXPR
7799 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7800 /* Detect assigning a bitfield. */
7801 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7803 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7805 /* Don't leave an assignment inside a conversion
7806 unless assigning a bitfield. */
7807 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7808 /* First do the assignment, then return converted constant. */
7809 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7810 TREE_NO_WARNING (tem) = 1;
7811 TREE_USED (tem) = 1;
7815 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7816 constants (if x has signed type, the sign bit cannot be set
7817 in c). This folds extension into the BIT_AND_EXPR.
7818 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7819 very likely don't have maximal range for their precision and this
7820 transformation effectively doesn't preserve non-maximal ranges. */
7821 if (TREE_CODE (type) == INTEGER_TYPE
7822 && TREE_CODE (op0) == BIT_AND_EXPR
7823 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7825 tree and_expr = op0;
7826 tree and0 = TREE_OPERAND (and_expr, 0);
7827 tree and1 = TREE_OPERAND (and_expr, 1);
7830 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7831 || (TYPE_PRECISION (type)
7832 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7834 else if (TYPE_PRECISION (TREE_TYPE (and1))
7835 <= HOST_BITS_PER_WIDE_INT
7836 && host_integerp (and1, 1))
7838 unsigned HOST_WIDE_INT cst;
7840 cst = tree_low_cst (and1, 1);
7841 cst &= (HOST_WIDE_INT) -1
7842 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7843 change = (cst == 0);
7844 #ifdef LOAD_EXTEND_OP
7846 && !flag_syntax_only
7847 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7850 tree uns = unsigned_type_for (TREE_TYPE (and0));
7851 and0 = fold_convert_loc (loc, uns, and0);
7852 and1 = fold_convert_loc (loc, uns, and1);
7858 tem = force_fit_type_double (type, tree_to_double_int (and1),
7859 0, TREE_OVERFLOW (and1));
7860 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7861 fold_convert_loc (loc, type, and0), tem);
7865 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7866 when one of the new casts will fold away. Conservatively we assume
7867 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7868 if (POINTER_TYPE_P (type)
7869 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7870 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7871 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7872 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7874 tree arg00 = TREE_OPERAND (arg0, 0);
7875 tree arg01 = TREE_OPERAND (arg0, 1);
7877 return fold_build2_loc (loc,
7878 TREE_CODE (arg0), type,
7879 fold_convert_loc (loc, type, arg00),
7880 fold_convert_loc (loc, sizetype, arg01));
7883 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7884 of the same precision, and X is an integer type not narrower than
7885 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7886 if (INTEGRAL_TYPE_P (type)
7887 && TREE_CODE (op0) == BIT_NOT_EXPR
7888 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7889 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7890 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7892 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7893 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7894 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7895 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7896 fold_convert_loc (loc, type, tem));
7899 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7900 type of X and Y (integer types only). */
7901 if (INTEGRAL_TYPE_P (type)
7902 && TREE_CODE (op0) == MULT_EXPR
7903 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7904 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7906 /* Be careful not to introduce new overflows. */
7908 if (TYPE_OVERFLOW_WRAPS (type))
7911 mult_type = unsigned_type_for (type);
7913 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7915 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7916 fold_convert_loc (loc, mult_type,
7917 TREE_OPERAND (op0, 0)),
7918 fold_convert_loc (loc, mult_type,
7919 TREE_OPERAND (op0, 1)));
7920 return fold_convert_loc (loc, type, tem);
7924 tem = fold_convert_const (code, type, op0);
7925 return tem ? tem : NULL_TREE;
7927 case ADDR_SPACE_CONVERT_EXPR:
7928 if (integer_zerop (arg0))
7929 return fold_convert_const (code, type, arg0);
7932 case FIXED_CONVERT_EXPR:
7933 tem = fold_convert_const (code, type, arg0);
7934 return tem ? tem : NULL_TREE;
7936 case VIEW_CONVERT_EXPR:
7937 if (TREE_TYPE (op0) == type)
7939 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7940 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7941 type, TREE_OPERAND (op0, 0));
7942 if (TREE_CODE (op0) == MEM_REF)
7943 return fold_build2_loc (loc, MEM_REF, type,
7944 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7946 /* For integral conversions with the same precision or pointer
7947 conversions use a NOP_EXPR instead. */
7948 if ((INTEGRAL_TYPE_P (type)
7949 || POINTER_TYPE_P (type))
7950 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7951 || POINTER_TYPE_P (TREE_TYPE (op0)))
7952 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7953 return fold_convert_loc (loc, type, op0);
7955 /* Strip inner integral conversions that do not change the precision. */
7956 if (CONVERT_EXPR_P (op0)
7957 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7958 || POINTER_TYPE_P (TREE_TYPE (op0)))
7959 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7960 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7961 && (TYPE_PRECISION (TREE_TYPE (op0))
7962 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7963 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7964 type, TREE_OPERAND (op0, 0));
7966 return fold_view_convert_expr (type, op0);
7969 tem = fold_negate_expr (loc, arg0);
7971 return fold_convert_loc (loc, type, tem);
7975 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7976 return fold_abs_const (arg0, type);
7977 else if (TREE_CODE (arg0) == NEGATE_EXPR)
7978 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7979 /* Convert fabs((double)float) into (double)fabsf(float). */
7980 else if (TREE_CODE (arg0) == NOP_EXPR
7981 && TREE_CODE (type) == REAL_TYPE)
7983 tree targ0 = strip_float_extensions (arg0);
7985 return fold_convert_loc (loc, type,
7986 fold_build1_loc (loc, ABS_EXPR,
7990 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
7991 else if (TREE_CODE (arg0) == ABS_EXPR)
7993 else if (tree_expr_nonnegative_p (arg0))
7996 /* Strip sign ops from argument. */
7997 if (TREE_CODE (type) == REAL_TYPE)
7999 tem = fold_strip_sign_ops (arg0);
8001 return fold_build1_loc (loc, ABS_EXPR, type,
8002 fold_convert_loc (loc, type, tem));
8007 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8008 return fold_convert_loc (loc, type, arg0);
8009 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8011 tree itype = TREE_TYPE (type);
8012 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8013 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8014 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8015 negate_expr (ipart));
8017 if (TREE_CODE (arg0) == COMPLEX_CST)
8019 tree itype = TREE_TYPE (type);
8020 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8021 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8022 return build_complex (type, rpart, negate_expr (ipart));
8024 if (TREE_CODE (arg0) == CONJ_EXPR)
8025 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8029 if (TREE_CODE (arg0) == INTEGER_CST)
8030 return fold_not_const (arg0, type);
8031 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8032 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8033 /* Convert ~ (-A) to A - 1. */
8034 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8035 return fold_build2_loc (loc, MINUS_EXPR, type,
8036 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8037 build_int_cst (type, 1));
8038 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8039 else if (INTEGRAL_TYPE_P (type)
8040 && ((TREE_CODE (arg0) == MINUS_EXPR
8041 && integer_onep (TREE_OPERAND (arg0, 1)))
8042 || (TREE_CODE (arg0) == PLUS_EXPR
8043 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8044 return fold_build1_loc (loc, NEGATE_EXPR, type,
8045 fold_convert_loc (loc, type,
8046 TREE_OPERAND (arg0, 0)));
8047 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8048 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8049 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8050 fold_convert_loc (loc, type,
8051 TREE_OPERAND (arg0, 0)))))
8052 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8053 fold_convert_loc (loc, type,
8054 TREE_OPERAND (arg0, 1)));
8055 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8056 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8057 fold_convert_loc (loc, type,
8058 TREE_OPERAND (arg0, 1)))))
8059 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8060 fold_convert_loc (loc, type,
8061 TREE_OPERAND (arg0, 0)), tem);
8062 /* Perform BIT_NOT_EXPR on each element individually. */
8063 else if (TREE_CODE (arg0) == VECTOR_CST)
8065 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8066 int count = TYPE_VECTOR_SUBPARTS (type), i;
8068 for (i = 0; i < count; i++)
8072 elem = TREE_VALUE (elements);
8073 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8074 if (elem == NULL_TREE)
8076 elements = TREE_CHAIN (elements);
8079 elem = build_int_cst (TREE_TYPE (type), -1);
8080 list = tree_cons (NULL_TREE, elem, list);
8083 return build_vector (type, nreverse (list));
8088 case TRUTH_NOT_EXPR:
8089 /* The argument to invert_truthvalue must have Boolean type. */
8090 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8091 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8093 /* Note that the operand of this must be an int
8094 and its values must be 0 or 1.
8095 ("true" is a fixed value perhaps depending on the language,
8096 but we don't handle values other than 1 correctly yet.) */
8097 tem = fold_truth_not_expr (loc, arg0);
8100 return fold_convert_loc (loc, type, tem);
8103 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8104 return fold_convert_loc (loc, type, arg0);
8105 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8106 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8107 TREE_OPERAND (arg0, 1));
8108 if (TREE_CODE (arg0) == COMPLEX_CST)
8109 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8110 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8112 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8113 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8114 fold_build1_loc (loc, REALPART_EXPR, itype,
8115 TREE_OPERAND (arg0, 0)),
8116 fold_build1_loc (loc, REALPART_EXPR, itype,
8117 TREE_OPERAND (arg0, 1)));
8118 return fold_convert_loc (loc, type, tem);
8120 if (TREE_CODE (arg0) == CONJ_EXPR)
8122 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8123 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8124 TREE_OPERAND (arg0, 0));
8125 return fold_convert_loc (loc, type, tem);
8127 if (TREE_CODE (arg0) == CALL_EXPR)
8129 tree fn = get_callee_fndecl (arg0);
8130 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8131 switch (DECL_FUNCTION_CODE (fn))
8133 CASE_FLT_FN (BUILT_IN_CEXPI):
8134 fn = mathfn_built_in (type, BUILT_IN_COS);
8136 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8146 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8147 return build_zero_cst (type);
8148 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8149 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8150 TREE_OPERAND (arg0, 0));
8151 if (TREE_CODE (arg0) == COMPLEX_CST)
8152 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8153 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8155 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8156 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8157 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8158 TREE_OPERAND (arg0, 0)),
8159 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8160 TREE_OPERAND (arg0, 1)));
8161 return fold_convert_loc (loc, type, tem);
8163 if (TREE_CODE (arg0) == CONJ_EXPR)
8165 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8166 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8167 return fold_convert_loc (loc, type, negate_expr (tem));
8169 if (TREE_CODE (arg0) == CALL_EXPR)
8171 tree fn = get_callee_fndecl (arg0);
8172 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8173 switch (DECL_FUNCTION_CODE (fn))
8175 CASE_FLT_FN (BUILT_IN_CEXPI):
8176 fn = mathfn_built_in (type, BUILT_IN_SIN);
8178 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8188 /* Fold *&X to X if X is an lvalue. */
8189 if (TREE_CODE (op0) == ADDR_EXPR)
8191 tree op00 = TREE_OPERAND (op0, 0);
8192 if ((TREE_CODE (op00) == VAR_DECL
8193 || TREE_CODE (op00) == PARM_DECL
8194 || TREE_CODE (op00) == RESULT_DECL)
8195 && !TREE_READONLY (op00))
8202 } /* switch (code) */
8206 /* If the operation was a conversion do _not_ mark a resulting constant
8207 with TREE_OVERFLOW if the original constant was not. These conversions
8208 have implementation defined behavior and retaining the TREE_OVERFLOW
8209 flag here would confuse later passes such as VRP. */
8211 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8212 tree type, tree op0)
8214 tree res = fold_unary_loc (loc, code, type, op0);
8216 && TREE_CODE (res) == INTEGER_CST
8217 && TREE_CODE (op0) == INTEGER_CST
8218 && CONVERT_EXPR_CODE_P (code))
8219 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8224 /* Fold a binary expression of code CODE and type TYPE with operands
8225 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8226 Return the folded expression if folding is successful. Otherwise,
8227 return NULL_TREE. */
8230 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8232 enum tree_code compl_code;
8234 if (code == MIN_EXPR)
8235 compl_code = MAX_EXPR;
8236 else if (code == MAX_EXPR)
8237 compl_code = MIN_EXPR;
8241 /* MIN (MAX (a, b), b) == b. */
8242 if (TREE_CODE (op0) == compl_code
8243 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8244 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8246 /* MIN (MAX (b, a), b) == b. */
8247 if (TREE_CODE (op0) == compl_code
8248 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8249 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8250 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8252 /* MIN (a, MAX (a, b)) == a. */
8253 if (TREE_CODE (op1) == compl_code
8254 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8255 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8256 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8258 /* MIN (a, MAX (b, a)) == a. */
8259 if (TREE_CODE (op1) == compl_code
8260 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8261 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8262 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8267 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8268 by changing CODE to reduce the magnitude of constants involved in
8269 ARG0 of the comparison.
8270 Returns a canonicalized comparison tree if a simplification was
8271 possible, otherwise returns NULL_TREE.
8272 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8273 valid if signed overflow is undefined. */
8276 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8277 tree arg0, tree arg1,
8278 bool *strict_overflow_p)
8280 enum tree_code code0 = TREE_CODE (arg0);
8281 tree t, cst0 = NULL_TREE;
8285 /* Match A +- CST code arg1 and CST code arg1. We can change the
8286 first form only if overflow is undefined. */
8287 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8288 /* In principle pointers also have undefined overflow behavior,
8289 but that causes problems elsewhere. */
8290 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8291 && (code0 == MINUS_EXPR
8292 || code0 == PLUS_EXPR)
8293 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8294 || code0 == INTEGER_CST))
8297 /* Identify the constant in arg0 and its sign. */
8298 if (code0 == INTEGER_CST)
8301 cst0 = TREE_OPERAND (arg0, 1);
8302 sgn0 = tree_int_cst_sgn (cst0);
8304 /* Overflowed constants and zero will cause problems. */
8305 if (integer_zerop (cst0)
8306 || TREE_OVERFLOW (cst0))
8309 /* See if we can reduce the magnitude of the constant in
8310 arg0 by changing the comparison code. */
8311 if (code0 == INTEGER_CST)
8313 /* CST <= arg1 -> CST-1 < arg1. */
8314 if (code == LE_EXPR && sgn0 == 1)
8316 /* -CST < arg1 -> -CST-1 <= arg1. */
8317 else if (code == LT_EXPR && sgn0 == -1)
8319 /* CST > arg1 -> CST-1 >= arg1. */
8320 else if (code == GT_EXPR && sgn0 == 1)
8322 /* -CST >= arg1 -> -CST-1 > arg1. */
8323 else if (code == GE_EXPR && sgn0 == -1)
8327 /* arg1 code' CST' might be more canonical. */
8332 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8334 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8336 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8337 else if (code == GT_EXPR
8338 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8340 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8341 else if (code == LE_EXPR
8342 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8344 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8345 else if (code == GE_EXPR
8346 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8350 *strict_overflow_p = true;
8353 /* Now build the constant reduced in magnitude. But not if that
8354 would produce one outside of its types range. */
8355 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8357 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8358 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8360 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8361 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8362 /* We cannot swap the comparison here as that would cause us to
8363 endlessly recurse. */
8366 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8367 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8368 if (code0 != INTEGER_CST)
8369 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8371 /* If swapping might yield to a more canonical form, do so. */
8373 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8375 return fold_build2_loc (loc, code, type, t, arg1);
8378 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8379 overflow further. Try to decrease the magnitude of constants involved
8380 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8381 and put sole constants at the second argument position.
8382 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8385 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8386 tree arg0, tree arg1)
8389 bool strict_overflow_p;
8390 const char * const warnmsg = G_("assuming signed overflow does not occur "
8391 "when reducing constant in comparison");
8393 /* Try canonicalization by simplifying arg0. */
8394 strict_overflow_p = false;
8395 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8396 &strict_overflow_p);
8399 if (strict_overflow_p)
8400 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8404 /* Try canonicalization by simplifying arg1 using the swapped
8406 code = swap_tree_comparison (code);
8407 strict_overflow_p = false;
8408 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8409 &strict_overflow_p);
8410 if (t && strict_overflow_p)
8411 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8415 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8416 space. This is used to avoid issuing overflow warnings for
8417 expressions like &p->x which can not wrap. */
8420 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8422 unsigned HOST_WIDE_INT offset_low, total_low;
8423 HOST_WIDE_INT size, offset_high, total_high;
8425 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8431 if (offset == NULL_TREE)
8436 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8440 offset_low = TREE_INT_CST_LOW (offset);
8441 offset_high = TREE_INT_CST_HIGH (offset);
8444 if (add_double_with_sign (offset_low, offset_high,
8445 bitpos / BITS_PER_UNIT, 0,
8446 &total_low, &total_high,
8450 if (total_high != 0)
8453 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8457 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8459 if (TREE_CODE (base) == ADDR_EXPR)
8461 HOST_WIDE_INT base_size;
8463 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8464 if (base_size > 0 && size < base_size)
8468 return total_low > (unsigned HOST_WIDE_INT) size;
8471 /* Subroutine of fold_binary. This routine performs all of the
8472 transformations that are common to the equality/inequality
8473 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8474 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8475 fold_binary should call fold_binary. Fold a comparison with
8476 tree code CODE and type TYPE with operands OP0 and OP1. Return
8477 the folded comparison or NULL_TREE. */
8480 fold_comparison (location_t loc, enum tree_code code, tree type,
8483 tree arg0, arg1, tem;
8488 STRIP_SIGN_NOPS (arg0);
8489 STRIP_SIGN_NOPS (arg1);
8491 tem = fold_relational_const (code, type, arg0, arg1);
8492 if (tem != NULL_TREE)
8495 /* If one arg is a real or integer constant, put it last. */
8496 if (tree_swap_operands_p (arg0, arg1, true))
8497 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8499 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8500 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8501 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8502 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8503 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8504 && (TREE_CODE (arg1) == INTEGER_CST
8505 && !TREE_OVERFLOW (arg1)))
8507 tree const1 = TREE_OPERAND (arg0, 1);
8509 tree variable = TREE_OPERAND (arg0, 0);
8512 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8514 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8515 TREE_TYPE (arg1), const2, const1);
8517 /* If the constant operation overflowed this can be
8518 simplified as a comparison against INT_MAX/INT_MIN. */
8519 if (TREE_CODE (lhs) == INTEGER_CST
8520 && TREE_OVERFLOW (lhs))
8522 int const1_sgn = tree_int_cst_sgn (const1);
8523 enum tree_code code2 = code;
8525 /* Get the sign of the constant on the lhs if the
8526 operation were VARIABLE + CONST1. */
8527 if (TREE_CODE (arg0) == MINUS_EXPR)
8528 const1_sgn = -const1_sgn;
8530 /* The sign of the constant determines if we overflowed
8531 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8532 Canonicalize to the INT_MIN overflow by swapping the comparison
8534 if (const1_sgn == -1)
8535 code2 = swap_tree_comparison (code);
8537 /* We now can look at the canonicalized case
8538 VARIABLE + 1 CODE2 INT_MIN
8539 and decide on the result. */
8540 if (code2 == LT_EXPR
8542 || code2 == EQ_EXPR)
8543 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8544 else if (code2 == NE_EXPR
8546 || code2 == GT_EXPR)
8547 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8550 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8551 && (TREE_CODE (lhs) != INTEGER_CST
8552 || !TREE_OVERFLOW (lhs)))
8554 if (code != EQ_EXPR && code != NE_EXPR)
8555 fold_overflow_warning ("assuming signed overflow does not occur "
8556 "when changing X +- C1 cmp C2 to "
8558 WARN_STRICT_OVERFLOW_COMPARISON);
8559 return fold_build2_loc (loc, code, type, variable, lhs);
8563 /* For comparisons of pointers we can decompose it to a compile time
8564 comparison of the base objects and the offsets into the object.
8565 This requires at least one operand being an ADDR_EXPR or a
8566 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8567 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8568 && (TREE_CODE (arg0) == ADDR_EXPR
8569 || TREE_CODE (arg1) == ADDR_EXPR
8570 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8571 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8573 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8574 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8575 enum machine_mode mode;
8576 int volatilep, unsignedp;
8577 bool indirect_base0 = false, indirect_base1 = false;
8579 /* Get base and offset for the access. Strip ADDR_EXPR for
8580 get_inner_reference, but put it back by stripping INDIRECT_REF
8581 off the base object if possible. indirect_baseN will be true
8582 if baseN is not an address but refers to the object itself. */
8584 if (TREE_CODE (arg0) == ADDR_EXPR)
8586 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8587 &bitsize, &bitpos0, &offset0, &mode,
8588 &unsignedp, &volatilep, false);
8589 if (TREE_CODE (base0) == INDIRECT_REF)
8590 base0 = TREE_OPERAND (base0, 0);
8592 indirect_base0 = true;
8594 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8596 base0 = TREE_OPERAND (arg0, 0);
8597 STRIP_SIGN_NOPS (base0);
8598 if (TREE_CODE (base0) == ADDR_EXPR)
8600 base0 = TREE_OPERAND (base0, 0);
8601 indirect_base0 = true;
8603 offset0 = TREE_OPERAND (arg0, 1);
8607 if (TREE_CODE (arg1) == ADDR_EXPR)
8609 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8610 &bitsize, &bitpos1, &offset1, &mode,
8611 &unsignedp, &volatilep, false);
8612 if (TREE_CODE (base1) == INDIRECT_REF)
8613 base1 = TREE_OPERAND (base1, 0);
8615 indirect_base1 = true;
8617 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8619 base1 = TREE_OPERAND (arg1, 0);
8620 STRIP_SIGN_NOPS (base1);
8621 if (TREE_CODE (base1) == ADDR_EXPR)
8623 base1 = TREE_OPERAND (base1, 0);
8624 indirect_base1 = true;
8626 offset1 = TREE_OPERAND (arg1, 1);
8629 /* A local variable can never be pointed to by
8630 the default SSA name of an incoming parameter. */
8631 if ((TREE_CODE (arg0) == ADDR_EXPR
8633 && TREE_CODE (base0) == VAR_DECL
8634 && auto_var_in_fn_p (base0, current_function_decl)
8636 && TREE_CODE (base1) == SSA_NAME
8637 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8638 && SSA_NAME_IS_DEFAULT_DEF (base1))
8639 || (TREE_CODE (arg1) == ADDR_EXPR
8641 && TREE_CODE (base1) == VAR_DECL
8642 && auto_var_in_fn_p (base1, current_function_decl)
8644 && TREE_CODE (base0) == SSA_NAME
8645 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8646 && SSA_NAME_IS_DEFAULT_DEF (base0)))
8648 if (code == NE_EXPR)
8649 return constant_boolean_node (1, type);
8650 else if (code == EQ_EXPR)
8651 return constant_boolean_node (0, type);
8653 /* If we have equivalent bases we might be able to simplify. */
8654 else if (indirect_base0 == indirect_base1
8655 && operand_equal_p (base0, base1, 0))
8657 /* We can fold this expression to a constant if the non-constant
8658 offset parts are equal. */
8659 if ((offset0 == offset1
8660 || (offset0 && offset1
8661 && operand_equal_p (offset0, offset1, 0)))
8664 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8669 && bitpos0 != bitpos1
8670 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8671 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8672 fold_overflow_warning (("assuming pointer wraparound does not "
8673 "occur when comparing P +- C1 with "
8675 WARN_STRICT_OVERFLOW_CONDITIONAL);
8680 return constant_boolean_node (bitpos0 == bitpos1, type);
8682 return constant_boolean_node (bitpos0 != bitpos1, type);
8684 return constant_boolean_node (bitpos0 < bitpos1, type);
8686 return constant_boolean_node (bitpos0 <= bitpos1, type);
8688 return constant_boolean_node (bitpos0 >= bitpos1, type);
8690 return constant_boolean_node (bitpos0 > bitpos1, type);
8694 /* We can simplify the comparison to a comparison of the variable
8695 offset parts if the constant offset parts are equal.
8696 Be careful to use signed size type here because otherwise we
8697 mess with array offsets in the wrong way. This is possible
8698 because pointer arithmetic is restricted to retain within an
8699 object and overflow on pointer differences is undefined as of
8700 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8701 else if (bitpos0 == bitpos1
8702 && ((code == EQ_EXPR || code == NE_EXPR)
8703 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8705 /* By converting to signed size type we cover middle-end pointer
8706 arithmetic which operates on unsigned pointer types of size
8707 type size and ARRAY_REF offsets which are properly sign or
8708 zero extended from their type in case it is narrower than
8710 if (offset0 == NULL_TREE)
8711 offset0 = build_int_cst (ssizetype, 0);
8713 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8714 if (offset1 == NULL_TREE)
8715 offset1 = build_int_cst (ssizetype, 0);
8717 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8721 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8722 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8723 fold_overflow_warning (("assuming pointer wraparound does not "
8724 "occur when comparing P +- C1 with "
8726 WARN_STRICT_OVERFLOW_COMPARISON);
8728 return fold_build2_loc (loc, code, type, offset0, offset1);
8731 /* For non-equal bases we can simplify if they are addresses
8732 of local binding decls or constants. */
8733 else if (indirect_base0 && indirect_base1
8734 /* We know that !operand_equal_p (base0, base1, 0)
8735 because the if condition was false. But make
8736 sure two decls are not the same. */
8738 && TREE_CODE (arg0) == ADDR_EXPR
8739 && TREE_CODE (arg1) == ADDR_EXPR
8740 && (((TREE_CODE (base0) == VAR_DECL
8741 || TREE_CODE (base0) == PARM_DECL)
8742 && (targetm.binds_local_p (base0)
8743 || CONSTANT_CLASS_P (base1)))
8744 || CONSTANT_CLASS_P (base0))
8745 && (((TREE_CODE (base1) == VAR_DECL
8746 || TREE_CODE (base1) == PARM_DECL)
8747 && (targetm.binds_local_p (base1)
8748 || CONSTANT_CLASS_P (base0)))
8749 || CONSTANT_CLASS_P (base1)))
8751 if (code == EQ_EXPR)
8752 return omit_two_operands_loc (loc, type, boolean_false_node,
8754 else if (code == NE_EXPR)
8755 return omit_two_operands_loc (loc, type, boolean_true_node,
8758 /* For equal offsets we can simplify to a comparison of the
8760 else if (bitpos0 == bitpos1
8762 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8764 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8765 && ((offset0 == offset1)
8766 || (offset0 && offset1
8767 && operand_equal_p (offset0, offset1, 0))))
8770 base0 = build_fold_addr_expr_loc (loc, base0);
8772 base1 = build_fold_addr_expr_loc (loc, base1);
8773 return fold_build2_loc (loc, code, type, base0, base1);
8777 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8778 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8779 the resulting offset is smaller in absolute value than the
8781 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8782 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8783 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8784 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8785 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8786 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8787 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8789 tree const1 = TREE_OPERAND (arg0, 1);
8790 tree const2 = TREE_OPERAND (arg1, 1);
8791 tree variable1 = TREE_OPERAND (arg0, 0);
8792 tree variable2 = TREE_OPERAND (arg1, 0);
8794 const char * const warnmsg = G_("assuming signed overflow does not "
8795 "occur when combining constants around "
8798 /* Put the constant on the side where it doesn't overflow and is
8799 of lower absolute value than before. */
8800 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8801 ? MINUS_EXPR : PLUS_EXPR,
8803 if (!TREE_OVERFLOW (cst)
8804 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8806 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8807 return fold_build2_loc (loc, code, type,
8809 fold_build2_loc (loc,
8810 TREE_CODE (arg1), TREE_TYPE (arg1),
8814 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8815 ? MINUS_EXPR : PLUS_EXPR,
8817 if (!TREE_OVERFLOW (cst)
8818 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8820 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8821 return fold_build2_loc (loc, code, type,
8822 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8828 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8829 signed arithmetic case. That form is created by the compiler
8830 often enough for folding it to be of value. One example is in
8831 computing loop trip counts after Operator Strength Reduction. */
8832 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8833 && TREE_CODE (arg0) == MULT_EXPR
8834 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8835 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8836 && integer_zerop (arg1))
8838 tree const1 = TREE_OPERAND (arg0, 1);
8839 tree const2 = arg1; /* zero */
8840 tree variable1 = TREE_OPERAND (arg0, 0);
8841 enum tree_code cmp_code = code;
8843 /* Handle unfolded multiplication by zero. */
8844 if (integer_zerop (const1))
8845 return fold_build2_loc (loc, cmp_code, type, const1, const2);
8847 fold_overflow_warning (("assuming signed overflow does not occur when "
8848 "eliminating multiplication in comparison "
8850 WARN_STRICT_OVERFLOW_COMPARISON);
8852 /* If const1 is negative we swap the sense of the comparison. */
8853 if (tree_int_cst_sgn (const1) < 0)
8854 cmp_code = swap_tree_comparison (cmp_code);
8856 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8859 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8863 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8865 tree targ0 = strip_float_extensions (arg0);
8866 tree targ1 = strip_float_extensions (arg1);
8867 tree newtype = TREE_TYPE (targ0);
8869 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8870 newtype = TREE_TYPE (targ1);
8872 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8873 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8874 return fold_build2_loc (loc, code, type,
8875 fold_convert_loc (loc, newtype, targ0),
8876 fold_convert_loc (loc, newtype, targ1));
8878 /* (-a) CMP (-b) -> b CMP a */
8879 if (TREE_CODE (arg0) == NEGATE_EXPR
8880 && TREE_CODE (arg1) == NEGATE_EXPR)
8881 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8882 TREE_OPERAND (arg0, 0));
8884 if (TREE_CODE (arg1) == REAL_CST)
8886 REAL_VALUE_TYPE cst;
8887 cst = TREE_REAL_CST (arg1);
8889 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8890 if (TREE_CODE (arg0) == NEGATE_EXPR)
8891 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8892 TREE_OPERAND (arg0, 0),
8893 build_real (TREE_TYPE (arg1),
8894 real_value_negate (&cst)));
8896 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8897 /* a CMP (-0) -> a CMP 0 */
8898 if (REAL_VALUE_MINUS_ZERO (cst))
8899 return fold_build2_loc (loc, code, type, arg0,
8900 build_real (TREE_TYPE (arg1), dconst0));
8902 /* x != NaN is always true, other ops are always false. */
8903 if (REAL_VALUE_ISNAN (cst)
8904 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8906 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8907 return omit_one_operand_loc (loc, type, tem, arg0);
8910 /* Fold comparisons against infinity. */
8911 if (REAL_VALUE_ISINF (cst)
8912 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8914 tem = fold_inf_compare (loc, code, type, arg0, arg1);
8915 if (tem != NULL_TREE)
8920 /* If this is a comparison of a real constant with a PLUS_EXPR
8921 or a MINUS_EXPR of a real constant, we can convert it into a
8922 comparison with a revised real constant as long as no overflow
8923 occurs when unsafe_math_optimizations are enabled. */
8924 if (flag_unsafe_math_optimizations
8925 && TREE_CODE (arg1) == REAL_CST
8926 && (TREE_CODE (arg0) == PLUS_EXPR
8927 || TREE_CODE (arg0) == MINUS_EXPR)
8928 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8929 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8930 ? MINUS_EXPR : PLUS_EXPR,
8931 arg1, TREE_OPERAND (arg0, 1)))
8932 && !TREE_OVERFLOW (tem))
8933 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8935 /* Likewise, we can simplify a comparison of a real constant with
8936 a MINUS_EXPR whose first operand is also a real constant, i.e.
8937 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
8938 floating-point types only if -fassociative-math is set. */
8939 if (flag_associative_math
8940 && TREE_CODE (arg1) == REAL_CST
8941 && TREE_CODE (arg0) == MINUS_EXPR
8942 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8943 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8945 && !TREE_OVERFLOW (tem))
8946 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8947 TREE_OPERAND (arg0, 1), tem);
8949 /* Fold comparisons against built-in math functions. */
8950 if (TREE_CODE (arg1) == REAL_CST
8951 && flag_unsafe_math_optimizations
8952 && ! flag_errno_math)
8954 enum built_in_function fcode = builtin_mathfn_code (arg0);
8956 if (fcode != END_BUILTINS)
8958 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
8959 if (tem != NULL_TREE)
8965 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8966 && CONVERT_EXPR_P (arg0))
8968 /* If we are widening one operand of an integer comparison,
8969 see if the other operand is similarly being widened. Perhaps we
8970 can do the comparison in the narrower type. */
8971 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
8975 /* Or if we are changing signedness. */
8976 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
8981 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8982 constant, we can simplify it. */
8983 if (TREE_CODE (arg1) == INTEGER_CST
8984 && (TREE_CODE (arg0) == MIN_EXPR
8985 || TREE_CODE (arg0) == MAX_EXPR)
8986 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8988 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8993 /* Simplify comparison of something with itself. (For IEEE
8994 floating-point, we can only do some of these simplifications.) */
8995 if (operand_equal_p (arg0, arg1, 0))
9000 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9001 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9002 return constant_boolean_node (1, type);
9007 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9008 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9009 return constant_boolean_node (1, type);
9010 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9013 /* For NE, we can only do this simplification if integer
9014 or we don't honor IEEE floating point NaNs. */
9015 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9016 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9018 /* ... fall through ... */
9021 return constant_boolean_node (0, type);
9027 /* If we are comparing an expression that just has comparisons
9028 of two integer values, arithmetic expressions of those comparisons,
9029 and constants, we can simplify it. There are only three cases
9030 to check: the two values can either be equal, the first can be
9031 greater, or the second can be greater. Fold the expression for
9032 those three values. Since each value must be 0 or 1, we have
9033 eight possibilities, each of which corresponds to the constant 0
9034 or 1 or one of the six possible comparisons.
9036 This handles common cases like (a > b) == 0 but also handles
9037 expressions like ((x > y) - (y > x)) > 0, which supposedly
9038 occur in macroized code. */
9040 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9042 tree cval1 = 0, cval2 = 0;
9045 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9046 /* Don't handle degenerate cases here; they should already
9047 have been handled anyway. */
9048 && cval1 != 0 && cval2 != 0
9049 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9050 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9051 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9052 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9053 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9054 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9055 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9057 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9058 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9060 /* We can't just pass T to eval_subst in case cval1 or cval2
9061 was the same as ARG1. */
9064 = fold_build2_loc (loc, code, type,
9065 eval_subst (loc, arg0, cval1, maxval,
9069 = fold_build2_loc (loc, code, type,
9070 eval_subst (loc, arg0, cval1, maxval,
9074 = fold_build2_loc (loc, code, type,
9075 eval_subst (loc, arg0, cval1, minval,
9079 /* All three of these results should be 0 or 1. Confirm they are.
9080 Then use those values to select the proper code to use. */
9082 if (TREE_CODE (high_result) == INTEGER_CST
9083 && TREE_CODE (equal_result) == INTEGER_CST
9084 && TREE_CODE (low_result) == INTEGER_CST)
9086 /* Make a 3-bit mask with the high-order bit being the
9087 value for `>', the next for '=', and the low for '<'. */
9088 switch ((integer_onep (high_result) * 4)
9089 + (integer_onep (equal_result) * 2)
9090 + integer_onep (low_result))
9094 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9115 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9120 tem = save_expr (build2 (code, type, cval1, cval2));
9121 SET_EXPR_LOCATION (tem, loc);
9124 return fold_build2_loc (loc, code, type, cval1, cval2);
9129 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9130 into a single range test. */
9131 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9132 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9133 && TREE_CODE (arg1) == INTEGER_CST
9134 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9135 && !integer_zerop (TREE_OPERAND (arg0, 1))
9136 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9137 && !TREE_OVERFLOW (arg1))
9139 tem = fold_div_compare (loc, code, type, arg0, arg1);
9140 if (tem != NULL_TREE)
9144 /* Fold ~X op ~Y as Y op X. */
9145 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9146 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9148 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9149 return fold_build2_loc (loc, code, type,
9150 fold_convert_loc (loc, cmp_type,
9151 TREE_OPERAND (arg1, 0)),
9152 TREE_OPERAND (arg0, 0));
9155 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9156 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9157 && TREE_CODE (arg1) == INTEGER_CST)
9159 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9160 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9161 TREE_OPERAND (arg0, 0),
9162 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9163 fold_convert_loc (loc, cmp_type, arg1)));
9170 /* Subroutine of fold_binary. Optimize complex multiplications of the
9171 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9172 argument EXPR represents the expression "z" of type TYPE. */
9175 fold_mult_zconjz (location_t loc, tree type, tree expr)
9177 tree itype = TREE_TYPE (type);
9178 tree rpart, ipart, tem;
9180 if (TREE_CODE (expr) == COMPLEX_EXPR)
9182 rpart = TREE_OPERAND (expr, 0);
9183 ipart = TREE_OPERAND (expr, 1);
9185 else if (TREE_CODE (expr) == COMPLEX_CST)
9187 rpart = TREE_REALPART (expr);
9188 ipart = TREE_IMAGPART (expr);
9192 expr = save_expr (expr);
9193 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9194 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9197 rpart = save_expr (rpart);
9198 ipart = save_expr (ipart);
9199 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9200 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9201 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9202 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9203 build_zero_cst (itype));
9207 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9208 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9209 guarantees that P and N have the same least significant log2(M) bits.
9210 N is not otherwise constrained. In particular, N is not normalized to
9211 0 <= N < M as is common. In general, the precise value of P is unknown.
9212 M is chosen as large as possible such that constant N can be determined.
9214 Returns M and sets *RESIDUE to N.
9216 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9217 account. This is not always possible due to PR 35705.
9220 static unsigned HOST_WIDE_INT
9221 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9222 bool allow_func_align)
9224 enum tree_code code;
9228 code = TREE_CODE (expr);
9229 if (code == ADDR_EXPR)
9231 unsigned int bitalign;
9232 bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9233 *residue /= BITS_PER_UNIT;
9234 return bitalign / BITS_PER_UNIT;
9236 else if (code == POINTER_PLUS_EXPR)
9239 unsigned HOST_WIDE_INT modulus;
9240 enum tree_code inner_code;
9242 op0 = TREE_OPERAND (expr, 0);
9244 modulus = get_pointer_modulus_and_residue (op0, residue,
9247 op1 = TREE_OPERAND (expr, 1);
9249 inner_code = TREE_CODE (op1);
9250 if (inner_code == INTEGER_CST)
9252 *residue += TREE_INT_CST_LOW (op1);
9255 else if (inner_code == MULT_EXPR)
9257 op1 = TREE_OPERAND (op1, 1);
9258 if (TREE_CODE (op1) == INTEGER_CST)
9260 unsigned HOST_WIDE_INT align;
9262 /* Compute the greatest power-of-2 divisor of op1. */
9263 align = TREE_INT_CST_LOW (op1);
9266 /* If align is non-zero and less than *modulus, replace
9267 *modulus with align., If align is 0, then either op1 is 0
9268 or the greatest power-of-2 divisor of op1 doesn't fit in an
9269 unsigned HOST_WIDE_INT. In either case, no additional
9270 constraint is imposed. */
9272 modulus = MIN (modulus, align);
9279 /* If we get here, we were unable to determine anything useful about the
9285 /* Fold a binary expression of code CODE and type TYPE with operands
9286 OP0 and OP1. LOC is the location of the resulting expression.
9287 Return the folded expression if folding is successful. Otherwise,
9288 return NULL_TREE. */
9291 fold_binary_loc (location_t loc,
9292 enum tree_code code, tree type, tree op0, tree op1)
9294 enum tree_code_class kind = TREE_CODE_CLASS (code);
9295 tree arg0, arg1, tem;
9296 tree t1 = NULL_TREE;
9297 bool strict_overflow_p;
9299 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9300 && TREE_CODE_LENGTH (code) == 2
9302 && op1 != NULL_TREE);
9307 /* Strip any conversions that don't change the mode. This is
9308 safe for every expression, except for a comparison expression
9309 because its signedness is derived from its operands. So, in
9310 the latter case, only strip conversions that don't change the
9311 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9314 Note that this is done as an internal manipulation within the
9315 constant folder, in order to find the simplest representation
9316 of the arguments so that their form can be studied. In any
9317 cases, the appropriate type conversions should be put back in
9318 the tree that will get out of the constant folder. */
9320 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9322 STRIP_SIGN_NOPS (arg0);
9323 STRIP_SIGN_NOPS (arg1);
9331 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9332 constant but we can't do arithmetic on them. */
9333 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9334 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9335 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9336 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9337 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9338 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9340 if (kind == tcc_binary)
9342 /* Make sure type and arg0 have the same saturating flag. */
9343 gcc_assert (TYPE_SATURATING (type)
9344 == TYPE_SATURATING (TREE_TYPE (arg0)));
9345 tem = const_binop (code, arg0, arg1);
9347 else if (kind == tcc_comparison)
9348 tem = fold_relational_const (code, type, arg0, arg1);
9352 if (tem != NULL_TREE)
9354 if (TREE_TYPE (tem) != type)
9355 tem = fold_convert_loc (loc, type, tem);
9360 /* If this is a commutative operation, and ARG0 is a constant, move it
9361 to ARG1 to reduce the number of tests below. */
9362 if (commutative_tree_code (code)
9363 && tree_swap_operands_p (arg0, arg1, true))
9364 return fold_build2_loc (loc, code, type, op1, op0);
9366 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9368 First check for cases where an arithmetic operation is applied to a
9369 compound, conditional, or comparison operation. Push the arithmetic
9370 operation inside the compound or conditional to see if any folding
9371 can then be done. Convert comparison to conditional for this purpose.
9372 The also optimizes non-constant cases that used to be done in
9375 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9376 one of the operands is a comparison and the other is a comparison, a
9377 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9378 code below would make the expression more complex. Change it to a
9379 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9380 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9382 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9383 || code == EQ_EXPR || code == NE_EXPR)
9384 && ((truth_value_p (TREE_CODE (arg0))
9385 && (truth_value_p (TREE_CODE (arg1))
9386 || (TREE_CODE (arg1) == BIT_AND_EXPR
9387 && integer_onep (TREE_OPERAND (arg1, 1)))))
9388 || (truth_value_p (TREE_CODE (arg1))
9389 && (truth_value_p (TREE_CODE (arg0))
9390 || (TREE_CODE (arg0) == BIT_AND_EXPR
9391 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9393 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9394 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9397 fold_convert_loc (loc, boolean_type_node, arg0),
9398 fold_convert_loc (loc, boolean_type_node, arg1));
9400 if (code == EQ_EXPR)
9401 tem = invert_truthvalue_loc (loc, tem);
9403 return fold_convert_loc (loc, type, tem);
9406 if (TREE_CODE_CLASS (code) == tcc_binary
9407 || TREE_CODE_CLASS (code) == tcc_comparison)
9409 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9411 tem = fold_build2_loc (loc, code, type,
9412 fold_convert_loc (loc, TREE_TYPE (op0),
9413 TREE_OPERAND (arg0, 1)), op1);
9414 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9417 if (TREE_CODE (arg1) == COMPOUND_EXPR
9418 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9420 tem = fold_build2_loc (loc, code, type, op0,
9421 fold_convert_loc (loc, TREE_TYPE (op1),
9422 TREE_OPERAND (arg1, 1)));
9423 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9427 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9429 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9431 /*cond_first_p=*/1);
9432 if (tem != NULL_TREE)
9436 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9438 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9440 /*cond_first_p=*/0);
9441 if (tem != NULL_TREE)
9449 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9450 if (TREE_CODE (arg0) == ADDR_EXPR
9451 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9453 tree iref = TREE_OPERAND (arg0, 0);
9454 return fold_build2 (MEM_REF, type,
9455 TREE_OPERAND (iref, 0),
9456 int_const_binop (PLUS_EXPR, arg1,
9457 TREE_OPERAND (iref, 1), 0));
9460 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9461 if (TREE_CODE (arg0) == ADDR_EXPR
9462 && handled_component_p (TREE_OPERAND (arg0, 0)))
9465 HOST_WIDE_INT coffset;
9466 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9470 return fold_build2 (MEM_REF, type,
9471 build_fold_addr_expr (base),
9472 int_const_binop (PLUS_EXPR, arg1,
9473 size_int (coffset), 0));
9478 case POINTER_PLUS_EXPR:
9479 /* 0 +p index -> (type)index */
9480 if (integer_zerop (arg0))
9481 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9483 /* PTR +p 0 -> PTR */
9484 if (integer_zerop (arg1))
9485 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9487 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9488 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9489 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9490 return fold_convert_loc (loc, type,
9491 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9492 fold_convert_loc (loc, sizetype,
9494 fold_convert_loc (loc, sizetype,
9497 /* index +p PTR -> PTR +p index */
9498 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9499 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9500 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9501 fold_convert_loc (loc, type, arg1),
9502 fold_convert_loc (loc, sizetype, arg0));
9504 /* (PTR +p B) +p A -> PTR +p (B + A) */
9505 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9508 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9509 tree arg00 = TREE_OPERAND (arg0, 0);
9510 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9511 arg01, fold_convert_loc (loc, sizetype, arg1));
9512 return fold_convert_loc (loc, type,
9513 fold_build2_loc (loc, POINTER_PLUS_EXPR,
9518 /* PTR_CST +p CST -> CST1 */
9519 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9520 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9521 fold_convert_loc (loc, type, arg1));
9523 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9524 of the array. Loop optimizer sometimes produce this type of
9526 if (TREE_CODE (arg0) == ADDR_EXPR)
9528 tem = try_move_mult_to_index (loc, arg0,
9529 fold_convert_loc (loc, sizetype, arg1));
9531 return fold_convert_loc (loc, type, tem);
9537 /* A + (-B) -> A - B */
9538 if (TREE_CODE (arg1) == NEGATE_EXPR)
9539 return fold_build2_loc (loc, MINUS_EXPR, type,
9540 fold_convert_loc (loc, type, arg0),
9541 fold_convert_loc (loc, type,
9542 TREE_OPERAND (arg1, 0)));
9543 /* (-A) + B -> B - A */
9544 if (TREE_CODE (arg0) == NEGATE_EXPR
9545 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9546 return fold_build2_loc (loc, MINUS_EXPR, type,
9547 fold_convert_loc (loc, type, arg1),
9548 fold_convert_loc (loc, type,
9549 TREE_OPERAND (arg0, 0)));
9551 if (INTEGRAL_TYPE_P (type))
9553 /* Convert ~A + 1 to -A. */
9554 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9555 && integer_onep (arg1))
9556 return fold_build1_loc (loc, NEGATE_EXPR, type,
9557 fold_convert_loc (loc, type,
9558 TREE_OPERAND (arg0, 0)));
9561 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9562 && !TYPE_OVERFLOW_TRAPS (type))
9564 tree tem = TREE_OPERAND (arg0, 0);
9567 if (operand_equal_p (tem, arg1, 0))
9569 t1 = build_int_cst_type (type, -1);
9570 return omit_one_operand_loc (loc, type, t1, arg1);
9575 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9576 && !TYPE_OVERFLOW_TRAPS (type))
9578 tree tem = TREE_OPERAND (arg1, 0);
9581 if (operand_equal_p (arg0, tem, 0))
9583 t1 = build_int_cst_type (type, -1);
9584 return omit_one_operand_loc (loc, type, t1, arg0);
9588 /* X + (X / CST) * -CST is X % CST. */
9589 if (TREE_CODE (arg1) == MULT_EXPR
9590 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9591 && operand_equal_p (arg0,
9592 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9594 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9595 tree cst1 = TREE_OPERAND (arg1, 1);
9596 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9598 if (sum && integer_zerop (sum))
9599 return fold_convert_loc (loc, type,
9600 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9601 TREE_TYPE (arg0), arg0,
9606 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9607 same or one. Make sure type is not saturating.
9608 fold_plusminus_mult_expr will re-associate. */
9609 if ((TREE_CODE (arg0) == MULT_EXPR
9610 || TREE_CODE (arg1) == MULT_EXPR)
9611 && !TYPE_SATURATING (type)
9612 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9614 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9619 if (! FLOAT_TYPE_P (type))
9621 if (integer_zerop (arg1))
9622 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9624 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9625 with a constant, and the two constants have no bits in common,
9626 we should treat this as a BIT_IOR_EXPR since this may produce more
9628 if (TREE_CODE (arg0) == BIT_AND_EXPR
9629 && TREE_CODE (arg1) == BIT_AND_EXPR
9630 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9631 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9632 && integer_zerop (const_binop (BIT_AND_EXPR,
9633 TREE_OPERAND (arg0, 1),
9634 TREE_OPERAND (arg1, 1))))
9636 code = BIT_IOR_EXPR;
9640 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9641 (plus (plus (mult) (mult)) (foo)) so that we can
9642 take advantage of the factoring cases below. */
9643 if (((TREE_CODE (arg0) == PLUS_EXPR
9644 || TREE_CODE (arg0) == MINUS_EXPR)
9645 && TREE_CODE (arg1) == MULT_EXPR)
9646 || ((TREE_CODE (arg1) == PLUS_EXPR
9647 || TREE_CODE (arg1) == MINUS_EXPR)
9648 && TREE_CODE (arg0) == MULT_EXPR))
9650 tree parg0, parg1, parg, marg;
9651 enum tree_code pcode;
9653 if (TREE_CODE (arg1) == MULT_EXPR)
9654 parg = arg0, marg = arg1;
9656 parg = arg1, marg = arg0;
9657 pcode = TREE_CODE (parg);
9658 parg0 = TREE_OPERAND (parg, 0);
9659 parg1 = TREE_OPERAND (parg, 1);
9663 if (TREE_CODE (parg0) == MULT_EXPR
9664 && TREE_CODE (parg1) != MULT_EXPR)
9665 return fold_build2_loc (loc, pcode, type,
9666 fold_build2_loc (loc, PLUS_EXPR, type,
9667 fold_convert_loc (loc, type,
9669 fold_convert_loc (loc, type,
9671 fold_convert_loc (loc, type, parg1));
9672 if (TREE_CODE (parg0) != MULT_EXPR
9673 && TREE_CODE (parg1) == MULT_EXPR)
9675 fold_build2_loc (loc, PLUS_EXPR, type,
9676 fold_convert_loc (loc, type, parg0),
9677 fold_build2_loc (loc, pcode, type,
9678 fold_convert_loc (loc, type, marg),
9679 fold_convert_loc (loc, type,
9685 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9686 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9687 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9689 /* Likewise if the operands are reversed. */
9690 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9691 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9693 /* Convert X + -C into X - C. */
9694 if (TREE_CODE (arg1) == REAL_CST
9695 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9697 tem = fold_negate_const (arg1, type);
9698 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9699 return fold_build2_loc (loc, MINUS_EXPR, type,
9700 fold_convert_loc (loc, type, arg0),
9701 fold_convert_loc (loc, type, tem));
9704 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9705 to __complex__ ( x, y ). This is not the same for SNaNs or
9706 if signed zeros are involved. */
9707 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9708 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9709 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9711 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9712 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9713 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9714 bool arg0rz = false, arg0iz = false;
9715 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9716 || (arg0i && (arg0iz = real_zerop (arg0i))))
9718 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9719 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9720 if (arg0rz && arg1i && real_zerop (arg1i))
9722 tree rp = arg1r ? arg1r
9723 : build1 (REALPART_EXPR, rtype, arg1);
9724 tree ip = arg0i ? arg0i
9725 : build1 (IMAGPART_EXPR, rtype, arg0);
9726 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9728 else if (arg0iz && arg1r && real_zerop (arg1r))
9730 tree rp = arg0r ? arg0r
9731 : build1 (REALPART_EXPR, rtype, arg0);
9732 tree ip = arg1i ? arg1i
9733 : build1 (IMAGPART_EXPR, rtype, arg1);
9734 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9739 if (flag_unsafe_math_optimizations
9740 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9741 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9742 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9745 /* Convert x+x into x*2.0. */
9746 if (operand_equal_p (arg0, arg1, 0)
9747 && SCALAR_FLOAT_TYPE_P (type))
9748 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9749 build_real (type, dconst2));
9751 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9752 We associate floats only if the user has specified
9753 -fassociative-math. */
9754 if (flag_associative_math
9755 && TREE_CODE (arg1) == PLUS_EXPR
9756 && TREE_CODE (arg0) != MULT_EXPR)
9758 tree tree10 = TREE_OPERAND (arg1, 0);
9759 tree tree11 = TREE_OPERAND (arg1, 1);
9760 if (TREE_CODE (tree11) == MULT_EXPR
9761 && TREE_CODE (tree10) == MULT_EXPR)
9764 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9765 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9768 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9769 We associate floats only if the user has specified
9770 -fassociative-math. */
9771 if (flag_associative_math
9772 && TREE_CODE (arg0) == PLUS_EXPR
9773 && TREE_CODE (arg1) != MULT_EXPR)
9775 tree tree00 = TREE_OPERAND (arg0, 0);
9776 tree tree01 = TREE_OPERAND (arg0, 1);
9777 if (TREE_CODE (tree01) == MULT_EXPR
9778 && TREE_CODE (tree00) == MULT_EXPR)
9781 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9782 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9788 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9789 is a rotate of A by C1 bits. */
9790 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9791 is a rotate of A by B bits. */
9793 enum tree_code code0, code1;
9795 code0 = TREE_CODE (arg0);
9796 code1 = TREE_CODE (arg1);
9797 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9798 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9799 && operand_equal_p (TREE_OPERAND (arg0, 0),
9800 TREE_OPERAND (arg1, 0), 0)
9801 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9802 TYPE_UNSIGNED (rtype))
9803 /* Only create rotates in complete modes. Other cases are not
9804 expanded properly. */
9805 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9807 tree tree01, tree11;
9808 enum tree_code code01, code11;
9810 tree01 = TREE_OPERAND (arg0, 1);
9811 tree11 = TREE_OPERAND (arg1, 1);
9812 STRIP_NOPS (tree01);
9813 STRIP_NOPS (tree11);
9814 code01 = TREE_CODE (tree01);
9815 code11 = TREE_CODE (tree11);
9816 if (code01 == INTEGER_CST
9817 && code11 == INTEGER_CST
9818 && TREE_INT_CST_HIGH (tree01) == 0
9819 && TREE_INT_CST_HIGH (tree11) == 0
9820 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9821 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9823 tem = build2_loc (loc, LROTATE_EXPR,
9824 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9825 TREE_OPERAND (arg0, 0),
9826 code0 == LSHIFT_EXPR ? tree01 : tree11);
9827 return fold_convert_loc (loc, type, tem);
9829 else if (code11 == MINUS_EXPR)
9831 tree tree110, tree111;
9832 tree110 = TREE_OPERAND (tree11, 0);
9833 tree111 = TREE_OPERAND (tree11, 1);
9834 STRIP_NOPS (tree110);
9835 STRIP_NOPS (tree111);
9836 if (TREE_CODE (tree110) == INTEGER_CST
9837 && 0 == compare_tree_int (tree110,
9839 (TREE_TYPE (TREE_OPERAND
9841 && operand_equal_p (tree01, tree111, 0))
9843 fold_convert_loc (loc, type,
9844 build2 ((code0 == LSHIFT_EXPR
9847 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9848 TREE_OPERAND (arg0, 0), tree01));
9850 else if (code01 == MINUS_EXPR)
9852 tree tree010, tree011;
9853 tree010 = TREE_OPERAND (tree01, 0);
9854 tree011 = TREE_OPERAND (tree01, 1);
9855 STRIP_NOPS (tree010);
9856 STRIP_NOPS (tree011);
9857 if (TREE_CODE (tree010) == INTEGER_CST
9858 && 0 == compare_tree_int (tree010,
9860 (TREE_TYPE (TREE_OPERAND
9862 && operand_equal_p (tree11, tree011, 0))
9863 return fold_convert_loc
9865 build2 ((code0 != LSHIFT_EXPR
9868 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9869 TREE_OPERAND (arg0, 0), tree11));
9875 /* In most languages, can't associate operations on floats through
9876 parentheses. Rather than remember where the parentheses were, we
9877 don't associate floats at all, unless the user has specified
9879 And, we need to make sure type is not saturating. */
9881 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9882 && !TYPE_SATURATING (type))
9884 tree var0, con0, lit0, minus_lit0;
9885 tree var1, con1, lit1, minus_lit1;
9888 /* Split both trees into variables, constants, and literals. Then
9889 associate each group together, the constants with literals,
9890 then the result with variables. This increases the chances of
9891 literals being recombined later and of generating relocatable
9892 expressions for the sum of a constant and literal. */
9893 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9894 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9895 code == MINUS_EXPR);
9897 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9898 if (code == MINUS_EXPR)
9901 /* With undefined overflow we can only associate constants with one
9902 variable, and constants whose association doesn't overflow. */
9903 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9904 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9911 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9912 tmp0 = TREE_OPERAND (tmp0, 0);
9913 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9914 tmp1 = TREE_OPERAND (tmp1, 0);
9915 /* The only case we can still associate with two variables
9916 is if they are the same, modulo negation. */
9917 if (!operand_equal_p (tmp0, tmp1, 0))
9921 if (ok && lit0 && lit1)
9923 tree tmp0 = fold_convert (type, lit0);
9924 tree tmp1 = fold_convert (type, lit1);
9926 if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
9927 && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
9932 /* Only do something if we found more than two objects. Otherwise,
9933 nothing has changed and we risk infinite recursion. */
9935 && (2 < ((var0 != 0) + (var1 != 0)
9936 + (con0 != 0) + (con1 != 0)
9937 + (lit0 != 0) + (lit1 != 0)
9938 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9940 var0 = associate_trees (loc, var0, var1, code, type);
9941 con0 = associate_trees (loc, con0, con1, code, type);
9942 lit0 = associate_trees (loc, lit0, lit1, code, type);
9943 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
9945 /* Preserve the MINUS_EXPR if the negative part of the literal is
9946 greater than the positive part. Otherwise, the multiplicative
9947 folding code (i.e extract_muldiv) may be fooled in case
9948 unsigned constants are subtracted, like in the following
9949 example: ((X*2 + 4) - 8U)/2. */
9950 if (minus_lit0 && lit0)
9952 if (TREE_CODE (lit0) == INTEGER_CST
9953 && TREE_CODE (minus_lit0) == INTEGER_CST
9954 && tree_int_cst_lt (lit0, minus_lit0))
9956 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9962 lit0 = associate_trees (loc, lit0, minus_lit0,
9971 fold_convert_loc (loc, type,
9972 associate_trees (loc, var0, minus_lit0,
9976 con0 = associate_trees (loc, con0, minus_lit0,
9979 fold_convert_loc (loc, type,
9980 associate_trees (loc, var0, con0,
9985 con0 = associate_trees (loc, con0, lit0, code, type);
9987 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9995 /* Pointer simplifications for subtraction, simple reassociations. */
9996 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9998 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9999 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10000 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10002 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10003 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10004 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10005 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10006 return fold_build2_loc (loc, PLUS_EXPR, type,
10007 fold_build2_loc (loc, MINUS_EXPR, type,
10009 fold_build2_loc (loc, MINUS_EXPR, type,
10012 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10013 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10015 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10016 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10017 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10018 fold_convert_loc (loc, type, arg1));
10020 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10023 /* A - (-B) -> A + B */
10024 if (TREE_CODE (arg1) == NEGATE_EXPR)
10025 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10026 fold_convert_loc (loc, type,
10027 TREE_OPERAND (arg1, 0)));
10028 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10029 if (TREE_CODE (arg0) == NEGATE_EXPR
10030 && (FLOAT_TYPE_P (type)
10031 || INTEGRAL_TYPE_P (type))
10032 && negate_expr_p (arg1)
10033 && reorder_operands_p (arg0, arg1))
10034 return fold_build2_loc (loc, MINUS_EXPR, type,
10035 fold_convert_loc (loc, type,
10036 negate_expr (arg1)),
10037 fold_convert_loc (loc, type,
10038 TREE_OPERAND (arg0, 0)));
10039 /* Convert -A - 1 to ~A. */
10040 if (INTEGRAL_TYPE_P (type)
10041 && TREE_CODE (arg0) == NEGATE_EXPR
10042 && integer_onep (arg1)
10043 && !TYPE_OVERFLOW_TRAPS (type))
10044 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10045 fold_convert_loc (loc, type,
10046 TREE_OPERAND (arg0, 0)));
10048 /* Convert -1 - A to ~A. */
10049 if (INTEGRAL_TYPE_P (type)
10050 && integer_all_onesp (arg0))
10051 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10054 /* X - (X / CST) * CST is X % CST. */
10055 if (INTEGRAL_TYPE_P (type)
10056 && TREE_CODE (arg1) == MULT_EXPR
10057 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10058 && operand_equal_p (arg0,
10059 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10060 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10061 TREE_OPERAND (arg1, 1), 0))
10063 fold_convert_loc (loc, type,
10064 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10065 arg0, TREE_OPERAND (arg1, 1)));
10067 if (! FLOAT_TYPE_P (type))
10069 if (integer_zerop (arg0))
10070 return negate_expr (fold_convert_loc (loc, type, arg1));
10071 if (integer_zerop (arg1))
10072 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10074 /* Fold A - (A & B) into ~B & A. */
10075 if (!TREE_SIDE_EFFECTS (arg0)
10076 && TREE_CODE (arg1) == BIT_AND_EXPR)
10078 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10080 tree arg10 = fold_convert_loc (loc, type,
10081 TREE_OPERAND (arg1, 0));
10082 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10083 fold_build1_loc (loc, BIT_NOT_EXPR,
10085 fold_convert_loc (loc, type, arg0));
10087 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10089 tree arg11 = fold_convert_loc (loc,
10090 type, TREE_OPERAND (arg1, 1));
10091 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10092 fold_build1_loc (loc, BIT_NOT_EXPR,
10094 fold_convert_loc (loc, type, arg0));
10098 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10099 any power of 2 minus 1. */
10100 if (TREE_CODE (arg0) == BIT_AND_EXPR
10101 && TREE_CODE (arg1) == BIT_AND_EXPR
10102 && operand_equal_p (TREE_OPERAND (arg0, 0),
10103 TREE_OPERAND (arg1, 0), 0))
10105 tree mask0 = TREE_OPERAND (arg0, 1);
10106 tree mask1 = TREE_OPERAND (arg1, 1);
10107 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10109 if (operand_equal_p (tem, mask1, 0))
10111 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10112 TREE_OPERAND (arg0, 0), mask1);
10113 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10118 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10119 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10120 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10122 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10123 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10124 (-ARG1 + ARG0) reduces to -ARG1. */
10125 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10126 return negate_expr (fold_convert_loc (loc, type, arg1));
10128 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10129 __complex__ ( x, -y ). This is not the same for SNaNs or if
10130 signed zeros are involved. */
10131 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10132 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10133 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10135 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10136 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10137 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10138 bool arg0rz = false, arg0iz = false;
10139 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10140 || (arg0i && (arg0iz = real_zerop (arg0i))))
10142 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10143 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10144 if (arg0rz && arg1i && real_zerop (arg1i))
10146 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10148 : build1 (REALPART_EXPR, rtype, arg1));
10149 tree ip = arg0i ? arg0i
10150 : build1 (IMAGPART_EXPR, rtype, arg0);
10151 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10153 else if (arg0iz && arg1r && real_zerop (arg1r))
10155 tree rp = arg0r ? arg0r
10156 : build1 (REALPART_EXPR, rtype, arg0);
10157 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10159 : build1 (IMAGPART_EXPR, rtype, arg1));
10160 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10165 /* Fold &x - &x. This can happen from &x.foo - &x.
10166 This is unsafe for certain floats even in non-IEEE formats.
10167 In IEEE, it is unsafe because it does wrong for NaNs.
10168 Also note that operand_equal_p is always false if an operand
10171 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10172 && operand_equal_p (arg0, arg1, 0))
10173 return build_zero_cst (type);
10175 /* A - B -> A + (-B) if B is easily negatable. */
10176 if (negate_expr_p (arg1)
10177 && ((FLOAT_TYPE_P (type)
10178 /* Avoid this transformation if B is a positive REAL_CST. */
10179 && (TREE_CODE (arg1) != REAL_CST
10180 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10181 || INTEGRAL_TYPE_P (type)))
10182 return fold_build2_loc (loc, PLUS_EXPR, type,
10183 fold_convert_loc (loc, type, arg0),
10184 fold_convert_loc (loc, type,
10185 negate_expr (arg1)));
10187 /* Try folding difference of addresses. */
10189 HOST_WIDE_INT diff;
10191 if ((TREE_CODE (arg0) == ADDR_EXPR
10192 || TREE_CODE (arg1) == ADDR_EXPR)
10193 && ptr_difference_const (arg0, arg1, &diff))
10194 return build_int_cst_type (type, diff);
10197 /* Fold &a[i] - &a[j] to i-j. */
10198 if (TREE_CODE (arg0) == ADDR_EXPR
10199 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10200 && TREE_CODE (arg1) == ADDR_EXPR
10201 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10203 tree aref0 = TREE_OPERAND (arg0, 0);
10204 tree aref1 = TREE_OPERAND (arg1, 0);
10205 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10206 TREE_OPERAND (aref1, 0), 0))
10208 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10209 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10210 tree esz = array_ref_element_size (aref0);
10211 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10212 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10213 fold_convert_loc (loc, type, esz));
10218 if (FLOAT_TYPE_P (type)
10219 && flag_unsafe_math_optimizations
10220 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10221 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10222 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10225 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10226 same or one. Make sure type is not saturating.
10227 fold_plusminus_mult_expr will re-associate. */
10228 if ((TREE_CODE (arg0) == MULT_EXPR
10229 || TREE_CODE (arg1) == MULT_EXPR)
10230 && !TYPE_SATURATING (type)
10231 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10233 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10241 /* (-A) * (-B) -> A * B */
10242 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10243 return fold_build2_loc (loc, MULT_EXPR, type,
10244 fold_convert_loc (loc, type,
10245 TREE_OPERAND (arg0, 0)),
10246 fold_convert_loc (loc, type,
10247 negate_expr (arg1)));
10248 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10249 return fold_build2_loc (loc, MULT_EXPR, type,
10250 fold_convert_loc (loc, type,
10251 negate_expr (arg0)),
10252 fold_convert_loc (loc, type,
10253 TREE_OPERAND (arg1, 0)));
10255 if (! FLOAT_TYPE_P (type))
10257 if (integer_zerop (arg1))
10258 return omit_one_operand_loc (loc, type, arg1, arg0);
10259 if (integer_onep (arg1))
10260 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10261 /* Transform x * -1 into -x. Make sure to do the negation
10262 on the original operand with conversions not stripped
10263 because we can only strip non-sign-changing conversions. */
10264 if (integer_all_onesp (arg1))
10265 return fold_convert_loc (loc, type, negate_expr (op0));
10266 /* Transform x * -C into -x * C if x is easily negatable. */
10267 if (TREE_CODE (arg1) == INTEGER_CST
10268 && tree_int_cst_sgn (arg1) == -1
10269 && negate_expr_p (arg0)
10270 && (tem = negate_expr (arg1)) != arg1
10271 && !TREE_OVERFLOW (tem))
10272 return fold_build2_loc (loc, MULT_EXPR, type,
10273 fold_convert_loc (loc, type,
10274 negate_expr (arg0)),
10277 /* (a * (1 << b)) is (a << b) */
10278 if (TREE_CODE (arg1) == LSHIFT_EXPR
10279 && integer_onep (TREE_OPERAND (arg1, 0)))
10280 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10281 TREE_OPERAND (arg1, 1));
10282 if (TREE_CODE (arg0) == LSHIFT_EXPR
10283 && integer_onep (TREE_OPERAND (arg0, 0)))
10284 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10285 TREE_OPERAND (arg0, 1));
10287 /* (A + A) * C -> A * 2 * C */
10288 if (TREE_CODE (arg0) == PLUS_EXPR
10289 && TREE_CODE (arg1) == INTEGER_CST
10290 && operand_equal_p (TREE_OPERAND (arg0, 0),
10291 TREE_OPERAND (arg0, 1), 0))
10292 return fold_build2_loc (loc, MULT_EXPR, type,
10293 omit_one_operand_loc (loc, type,
10294 TREE_OPERAND (arg0, 0),
10295 TREE_OPERAND (arg0, 1)),
10296 fold_build2_loc (loc, MULT_EXPR, type,
10297 build_int_cst (type, 2) , arg1));
10299 strict_overflow_p = false;
10300 if (TREE_CODE (arg1) == INTEGER_CST
10301 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10302 &strict_overflow_p)))
10304 if (strict_overflow_p)
10305 fold_overflow_warning (("assuming signed overflow does not "
10306 "occur when simplifying "
10308 WARN_STRICT_OVERFLOW_MISC);
10309 return fold_convert_loc (loc, type, tem);
10312 /* Optimize z * conj(z) for integer complex numbers. */
10313 if (TREE_CODE (arg0) == CONJ_EXPR
10314 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10315 return fold_mult_zconjz (loc, type, arg1);
10316 if (TREE_CODE (arg1) == CONJ_EXPR
10317 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10318 return fold_mult_zconjz (loc, type, arg0);
10322 /* Maybe fold x * 0 to 0. The expressions aren't the same
10323 when x is NaN, since x * 0 is also NaN. Nor are they the
10324 same in modes with signed zeros, since multiplying a
10325 negative value by 0 gives -0, not +0. */
10326 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10327 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10328 && real_zerop (arg1))
10329 return omit_one_operand_loc (loc, type, arg1, arg0);
10330 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10331 Likewise for complex arithmetic with signed zeros. */
10332 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10333 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10334 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10335 && real_onep (arg1))
10336 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10338 /* Transform x * -1.0 into -x. */
10339 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10340 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10341 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10342 && real_minus_onep (arg1))
10343 return fold_convert_loc (loc, type, negate_expr (arg0));
10345 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10346 the result for floating point types due to rounding so it is applied
10347 only if -fassociative-math was specify. */
10348 if (flag_associative_math
10349 && TREE_CODE (arg0) == RDIV_EXPR
10350 && TREE_CODE (arg1) == REAL_CST
10351 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10353 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10356 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10357 TREE_OPERAND (arg0, 1));
10360 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10361 if (operand_equal_p (arg0, arg1, 0))
10363 tree tem = fold_strip_sign_ops (arg0);
10364 if (tem != NULL_TREE)
10366 tem = fold_convert_loc (loc, type, tem);
10367 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10371 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10372 This is not the same for NaNs or if signed zeros are
10374 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10375 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10376 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10377 && TREE_CODE (arg1) == COMPLEX_CST
10378 && real_zerop (TREE_REALPART (arg1)))
10380 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10381 if (real_onep (TREE_IMAGPART (arg1)))
10383 fold_build2_loc (loc, COMPLEX_EXPR, type,
10384 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10386 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10387 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10389 fold_build2_loc (loc, COMPLEX_EXPR, type,
10390 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10391 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10395 /* Optimize z * conj(z) for floating point complex numbers.
10396 Guarded by flag_unsafe_math_optimizations as non-finite
10397 imaginary components don't produce scalar results. */
10398 if (flag_unsafe_math_optimizations
10399 && TREE_CODE (arg0) == CONJ_EXPR
10400 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10401 return fold_mult_zconjz (loc, type, arg1);
10402 if (flag_unsafe_math_optimizations
10403 && TREE_CODE (arg1) == CONJ_EXPR
10404 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10405 return fold_mult_zconjz (loc, type, arg0);
10407 if (flag_unsafe_math_optimizations)
10409 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10410 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10412 /* Optimizations of root(...)*root(...). */
10413 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10416 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10417 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10419 /* Optimize sqrt(x)*sqrt(x) as x. */
10420 if (BUILTIN_SQRT_P (fcode0)
10421 && operand_equal_p (arg00, arg10, 0)
10422 && ! HONOR_SNANS (TYPE_MODE (type)))
10425 /* Optimize root(x)*root(y) as root(x*y). */
10426 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10427 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10428 return build_call_expr_loc (loc, rootfn, 1, arg);
10431 /* Optimize expN(x)*expN(y) as expN(x+y). */
10432 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10434 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10435 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10436 CALL_EXPR_ARG (arg0, 0),
10437 CALL_EXPR_ARG (arg1, 0));
10438 return build_call_expr_loc (loc, expfn, 1, arg);
10441 /* Optimizations of pow(...)*pow(...). */
10442 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10443 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10444 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10446 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10447 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10448 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10449 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10451 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10452 if (operand_equal_p (arg01, arg11, 0))
10454 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10455 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10457 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10460 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10461 if (operand_equal_p (arg00, arg10, 0))
10463 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10464 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10466 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10470 /* Optimize tan(x)*cos(x) as sin(x). */
10471 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10472 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10473 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10474 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10475 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10476 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10477 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10478 CALL_EXPR_ARG (arg1, 0), 0))
10480 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10482 if (sinfn != NULL_TREE)
10483 return build_call_expr_loc (loc, sinfn, 1,
10484 CALL_EXPR_ARG (arg0, 0));
10487 /* Optimize x*pow(x,c) as pow(x,c+1). */
10488 if (fcode1 == BUILT_IN_POW
10489 || fcode1 == BUILT_IN_POWF
10490 || fcode1 == BUILT_IN_POWL)
10492 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10493 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10494 if (TREE_CODE (arg11) == REAL_CST
10495 && !TREE_OVERFLOW (arg11)
10496 && operand_equal_p (arg0, arg10, 0))
10498 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10502 c = TREE_REAL_CST (arg11);
10503 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10504 arg = build_real (type, c);
10505 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10509 /* Optimize pow(x,c)*x as pow(x,c+1). */
10510 if (fcode0 == BUILT_IN_POW
10511 || fcode0 == BUILT_IN_POWF
10512 || fcode0 == BUILT_IN_POWL)
10514 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10515 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10516 if (TREE_CODE (arg01) == REAL_CST
10517 && !TREE_OVERFLOW (arg01)
10518 && operand_equal_p (arg1, arg00, 0))
10520 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10524 c = TREE_REAL_CST (arg01);
10525 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10526 arg = build_real (type, c);
10527 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10531 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10532 if (optimize_function_for_speed_p (cfun)
10533 && operand_equal_p (arg0, arg1, 0))
10535 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10539 tree arg = build_real (type, dconst2);
10540 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10549 if (integer_all_onesp (arg1))
10550 return omit_one_operand_loc (loc, type, arg1, arg0);
10551 if (integer_zerop (arg1))
10552 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10553 if (operand_equal_p (arg0, arg1, 0))
10554 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10556 /* ~X | X is -1. */
10557 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10558 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10560 t1 = build_zero_cst (type);
10561 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10562 return omit_one_operand_loc (loc, type, t1, arg1);
10565 /* X | ~X is -1. */
10566 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10567 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10569 t1 = build_zero_cst (type);
10570 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10571 return omit_one_operand_loc (loc, type, t1, arg0);
10574 /* Canonicalize (X & C1) | C2. */
10575 if (TREE_CODE (arg0) == BIT_AND_EXPR
10576 && TREE_CODE (arg1) == INTEGER_CST
10577 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10579 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10580 int width = TYPE_PRECISION (type), w;
10581 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10582 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10583 hi2 = TREE_INT_CST_HIGH (arg1);
10584 lo2 = TREE_INT_CST_LOW (arg1);
10586 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10587 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10588 return omit_one_operand_loc (loc, type, arg1,
10589 TREE_OPERAND (arg0, 0));
10591 if (width > HOST_BITS_PER_WIDE_INT)
10593 mhi = (unsigned HOST_WIDE_INT) -1
10594 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10600 mlo = (unsigned HOST_WIDE_INT) -1
10601 >> (HOST_BITS_PER_WIDE_INT - width);
10604 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10605 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10606 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10607 TREE_OPERAND (arg0, 0), arg1);
10609 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10610 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10611 mode which allows further optimizations. */
10618 for (w = BITS_PER_UNIT;
10619 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10622 unsigned HOST_WIDE_INT mask
10623 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10624 if (((lo1 | lo2) & mask) == mask
10625 && (lo1 & ~mask) == 0 && hi1 == 0)
10632 if (hi3 != hi1 || lo3 != lo1)
10633 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10634 fold_build2_loc (loc, BIT_AND_EXPR, type,
10635 TREE_OPERAND (arg0, 0),
10636 build_int_cst_wide (type,
10641 /* (X & Y) | Y is (X, Y). */
10642 if (TREE_CODE (arg0) == BIT_AND_EXPR
10643 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10644 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10645 /* (X & Y) | X is (Y, X). */
10646 if (TREE_CODE (arg0) == BIT_AND_EXPR
10647 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10648 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10649 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10650 /* X | (X & Y) is (Y, X). */
10651 if (TREE_CODE (arg1) == BIT_AND_EXPR
10652 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10653 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10654 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10655 /* X | (Y & X) is (Y, X). */
10656 if (TREE_CODE (arg1) == BIT_AND_EXPR
10657 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10658 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10659 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10661 /* (X & ~Y) | (~X & Y) is X ^ Y */
10662 if (TREE_CODE (arg0) == BIT_AND_EXPR
10663 && TREE_CODE (arg1) == BIT_AND_EXPR)
10665 tree a0, a1, l0, l1, n0, n1;
10667 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10668 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10670 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10671 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10673 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10674 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10676 if ((operand_equal_p (n0, a0, 0)
10677 && operand_equal_p (n1, a1, 0))
10678 || (operand_equal_p (n0, a1, 0)
10679 && operand_equal_p (n1, a0, 0)))
10680 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10683 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10684 if (t1 != NULL_TREE)
10687 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10689 This results in more efficient code for machines without a NAND
10690 instruction. Combine will canonicalize to the first form
10691 which will allow use of NAND instructions provided by the
10692 backend if they exist. */
10693 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10694 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10697 fold_build1_loc (loc, BIT_NOT_EXPR, type,
10698 build2 (BIT_AND_EXPR, type,
10699 fold_convert_loc (loc, type,
10700 TREE_OPERAND (arg0, 0)),
10701 fold_convert_loc (loc, type,
10702 TREE_OPERAND (arg1, 0))));
10705 /* See if this can be simplified into a rotate first. If that
10706 is unsuccessful continue in the association code. */
10710 if (integer_zerop (arg1))
10711 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10712 if (integer_all_onesp (arg1))
10713 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10714 if (operand_equal_p (arg0, arg1, 0))
10715 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10717 /* ~X ^ X is -1. */
10718 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10719 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10721 t1 = build_zero_cst (type);
10722 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10723 return omit_one_operand_loc (loc, type, t1, arg1);
10726 /* X ^ ~X is -1. */
10727 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10728 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10730 t1 = build_zero_cst (type);
10731 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10732 return omit_one_operand_loc (loc, type, t1, arg0);
10735 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10736 with a constant, and the two constants have no bits in common,
10737 we should treat this as a BIT_IOR_EXPR since this may produce more
10738 simplifications. */
10739 if (TREE_CODE (arg0) == BIT_AND_EXPR
10740 && TREE_CODE (arg1) == BIT_AND_EXPR
10741 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10742 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10743 && integer_zerop (const_binop (BIT_AND_EXPR,
10744 TREE_OPERAND (arg0, 1),
10745 TREE_OPERAND (arg1, 1))))
10747 code = BIT_IOR_EXPR;
10751 /* (X | Y) ^ X -> Y & ~ X*/
10752 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10753 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10755 tree t2 = TREE_OPERAND (arg0, 1);
10756 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10758 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10759 fold_convert_loc (loc, type, t2),
10760 fold_convert_loc (loc, type, t1));
10764 /* (Y | X) ^ X -> Y & ~ X*/
10765 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10766 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10768 tree t2 = TREE_OPERAND (arg0, 0);
10769 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10771 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10772 fold_convert_loc (loc, type, t2),
10773 fold_convert_loc (loc, type, t1));
10777 /* X ^ (X | Y) -> Y & ~ X*/
10778 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10779 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10781 tree t2 = TREE_OPERAND (arg1, 1);
10782 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10784 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10785 fold_convert_loc (loc, type, t2),
10786 fold_convert_loc (loc, type, t1));
10790 /* X ^ (Y | X) -> Y & ~ X*/
10791 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10792 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10794 tree t2 = TREE_OPERAND (arg1, 0);
10795 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10797 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10798 fold_convert_loc (loc, type, t2),
10799 fold_convert_loc (loc, type, t1));
10803 /* Convert ~X ^ ~Y to X ^ Y. */
10804 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10805 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10806 return fold_build2_loc (loc, code, type,
10807 fold_convert_loc (loc, type,
10808 TREE_OPERAND (arg0, 0)),
10809 fold_convert_loc (loc, type,
10810 TREE_OPERAND (arg1, 0)));
10812 /* Convert ~X ^ C to X ^ ~C. */
10813 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10814 && TREE_CODE (arg1) == INTEGER_CST)
10815 return fold_build2_loc (loc, code, type,
10816 fold_convert_loc (loc, type,
10817 TREE_OPERAND (arg0, 0)),
10818 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10820 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10821 if (TREE_CODE (arg0) == BIT_AND_EXPR
10822 && integer_onep (TREE_OPERAND (arg0, 1))
10823 && integer_onep (arg1))
10824 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10825 build_int_cst (TREE_TYPE (arg0), 0));
10827 /* Fold (X & Y) ^ Y as ~X & Y. */
10828 if (TREE_CODE (arg0) == BIT_AND_EXPR
10829 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10831 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10832 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10833 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10834 fold_convert_loc (loc, type, arg1));
10836 /* Fold (X & Y) ^ X as ~Y & X. */
10837 if (TREE_CODE (arg0) == BIT_AND_EXPR
10838 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10839 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10841 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10842 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10843 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10844 fold_convert_loc (loc, type, arg1));
10846 /* Fold X ^ (X & Y) as X & ~Y. */
10847 if (TREE_CODE (arg1) == BIT_AND_EXPR
10848 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10850 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10851 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10852 fold_convert_loc (loc, type, arg0),
10853 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10855 /* Fold X ^ (Y & X) as ~Y & X. */
10856 if (TREE_CODE (arg1) == BIT_AND_EXPR
10857 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10858 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10860 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10861 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10862 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10863 fold_convert_loc (loc, type, arg0));
10866 /* See if this can be simplified into a rotate first. If that
10867 is unsuccessful continue in the association code. */
10871 if (integer_all_onesp (arg1))
10872 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10873 if (integer_zerop (arg1))
10874 return omit_one_operand_loc (loc, type, arg1, arg0);
10875 if (operand_equal_p (arg0, arg1, 0))
10876 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10878 /* ~X & X is always zero. */
10879 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10880 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10881 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10883 /* X & ~X is always zero. */
10884 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10885 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10886 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10888 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10889 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10890 && TREE_CODE (arg1) == INTEGER_CST
10891 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10893 tree tmp1 = fold_convert_loc (loc, type, arg1);
10894 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10895 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10896 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10897 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10899 fold_convert_loc (loc, type,
10900 fold_build2_loc (loc, BIT_IOR_EXPR,
10901 type, tmp2, tmp3));
10904 /* (X | Y) & Y is (X, Y). */
10905 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10906 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10907 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10908 /* (X | Y) & X is (Y, X). */
10909 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10910 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10911 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10912 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10913 /* X & (X | Y) is (Y, X). */
10914 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10915 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10916 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10917 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10918 /* X & (Y | X) is (Y, X). */
10919 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10920 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10921 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10922 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10924 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10925 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10926 && integer_onep (TREE_OPERAND (arg0, 1))
10927 && integer_onep (arg1))
10929 tem = TREE_OPERAND (arg0, 0);
10930 return fold_build2_loc (loc, EQ_EXPR, type,
10931 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10932 build_int_cst (TREE_TYPE (tem), 1)),
10933 build_int_cst (TREE_TYPE (tem), 0));
10935 /* Fold ~X & 1 as (X & 1) == 0. */
10936 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10937 && integer_onep (arg1))
10939 tem = TREE_OPERAND (arg0, 0);
10940 return fold_build2_loc (loc, EQ_EXPR, type,
10941 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10942 build_int_cst (TREE_TYPE (tem), 1)),
10943 build_int_cst (TREE_TYPE (tem), 0));
10946 /* Fold (X ^ Y) & Y as ~X & Y. */
10947 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10948 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10950 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10951 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10952 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10953 fold_convert_loc (loc, type, arg1));
10955 /* Fold (X ^ Y) & X as ~Y & X. */
10956 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10957 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10958 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10960 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10961 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10962 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10963 fold_convert_loc (loc, type, arg1));
10965 /* Fold X & (X ^ Y) as X & ~Y. */
10966 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10967 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10969 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10970 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10971 fold_convert_loc (loc, type, arg0),
10972 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10974 /* Fold X & (Y ^ X) as ~Y & X. */
10975 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10976 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10977 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10979 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10980 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10981 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10982 fold_convert_loc (loc, type, arg0));
10985 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10986 ((A & N) + B) & M -> (A + B) & M
10987 Similarly if (N & M) == 0,
10988 ((A | N) + B) & M -> (A + B) & M
10989 and for - instead of + (or unary - instead of +)
10990 and/or ^ instead of |.
10991 If B is constant and (B & M) == 0, fold into A & M. */
10992 if (host_integerp (arg1, 1))
10994 unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
10995 if (~cst1 && (cst1 & (cst1 + 1)) == 0
10996 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10997 && (TREE_CODE (arg0) == PLUS_EXPR
10998 || TREE_CODE (arg0) == MINUS_EXPR
10999 || TREE_CODE (arg0) == NEGATE_EXPR)
11000 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11001 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11005 unsigned HOST_WIDE_INT cst0;
11007 /* Now we know that arg0 is (C + D) or (C - D) or
11008 -C and arg1 (M) is == (1LL << cst) - 1.
11009 Store C into PMOP[0] and D into PMOP[1]. */
11010 pmop[0] = TREE_OPERAND (arg0, 0);
11012 if (TREE_CODE (arg0) != NEGATE_EXPR)
11014 pmop[1] = TREE_OPERAND (arg0, 1);
11018 if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11019 || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11023 for (; which >= 0; which--)
11024 switch (TREE_CODE (pmop[which]))
11029 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11032 /* tree_low_cst not used, because we don't care about
11034 cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11036 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11041 else if (cst0 != 0)
11043 /* If C or D is of the form (A & N) where
11044 (N & M) == M, or of the form (A | N) or
11045 (A ^ N) where (N & M) == 0, replace it with A. */
11046 pmop[which] = TREE_OPERAND (pmop[which], 0);
11049 /* If C or D is a N where (N & M) == 0, it can be
11050 omitted (assumed 0). */
11051 if ((TREE_CODE (arg0) == PLUS_EXPR
11052 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11053 && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11054 pmop[which] = NULL;
11060 /* Only build anything new if we optimized one or both arguments
11062 if (pmop[0] != TREE_OPERAND (arg0, 0)
11063 || (TREE_CODE (arg0) != NEGATE_EXPR
11064 && pmop[1] != TREE_OPERAND (arg0, 1)))
11066 tree utype = TREE_TYPE (arg0);
11067 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11069 /* Perform the operations in a type that has defined
11070 overflow behavior. */
11071 utype = unsigned_type_for (TREE_TYPE (arg0));
11072 if (pmop[0] != NULL)
11073 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11074 if (pmop[1] != NULL)
11075 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11078 if (TREE_CODE (arg0) == NEGATE_EXPR)
11079 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11080 else if (TREE_CODE (arg0) == PLUS_EXPR)
11082 if (pmop[0] != NULL && pmop[1] != NULL)
11083 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11085 else if (pmop[0] != NULL)
11087 else if (pmop[1] != NULL)
11090 return build_int_cst (type, 0);
11092 else if (pmop[0] == NULL)
11093 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11095 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11097 /* TEM is now the new binary +, - or unary - replacement. */
11098 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11099 fold_convert_loc (loc, utype, arg1));
11100 return fold_convert_loc (loc, type, tem);
11105 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11106 if (t1 != NULL_TREE)
11108 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11109 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11110 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11113 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11115 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11116 && (~TREE_INT_CST_LOW (arg1)
11117 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11119 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11122 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11124 This results in more efficient code for machines without a NOR
11125 instruction. Combine will canonicalize to the first form
11126 which will allow use of NOR instructions provided by the
11127 backend if they exist. */
11128 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11129 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11131 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11132 build2 (BIT_IOR_EXPR, type,
11133 fold_convert_loc (loc, type,
11134 TREE_OPERAND (arg0, 0)),
11135 fold_convert_loc (loc, type,
11136 TREE_OPERAND (arg1, 0))));
11139 /* If arg0 is derived from the address of an object or function, we may
11140 be able to fold this expression using the object or function's
11142 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11144 unsigned HOST_WIDE_INT modulus, residue;
11145 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11147 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11148 integer_onep (arg1));
11150 /* This works because modulus is a power of 2. If this weren't the
11151 case, we'd have to replace it by its greatest power-of-2
11152 divisor: modulus & -modulus. */
11154 return build_int_cst (type, residue & low);
11157 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11158 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11159 if the new mask might be further optimized. */
11160 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11161 || TREE_CODE (arg0) == RSHIFT_EXPR)
11162 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11163 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11164 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11165 < TYPE_PRECISION (TREE_TYPE (arg0))
11166 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11167 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11169 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11170 unsigned HOST_WIDE_INT mask
11171 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11172 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11173 tree shift_type = TREE_TYPE (arg0);
11175 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11176 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11177 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11178 && TYPE_PRECISION (TREE_TYPE (arg0))
11179 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11181 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11182 tree arg00 = TREE_OPERAND (arg0, 0);
11183 /* See if more bits can be proven as zero because of
11185 if (TREE_CODE (arg00) == NOP_EXPR
11186 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11188 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11189 if (TYPE_PRECISION (inner_type)
11190 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11191 && TYPE_PRECISION (inner_type) < prec)
11193 prec = TYPE_PRECISION (inner_type);
11194 /* See if we can shorten the right shift. */
11196 shift_type = inner_type;
11199 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11200 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11201 zerobits <<= prec - shiftc;
11202 /* For arithmetic shift if sign bit could be set, zerobits
11203 can contain actually sign bits, so no transformation is
11204 possible, unless MASK masks them all away. In that
11205 case the shift needs to be converted into logical shift. */
11206 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11207 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11209 if ((mask & zerobits) == 0)
11210 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11216 /* ((X << 16) & 0xff00) is (X, 0). */
11217 if ((mask & zerobits) == mask)
11218 return omit_one_operand_loc (loc, type,
11219 build_int_cst (type, 0), arg0);
11221 newmask = mask | zerobits;
11222 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11226 /* Only do the transformation if NEWMASK is some integer
11228 for (prec = BITS_PER_UNIT;
11229 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11230 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11232 if (prec < HOST_BITS_PER_WIDE_INT
11233 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11237 if (shift_type != TREE_TYPE (arg0))
11239 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11240 fold_convert_loc (loc, shift_type,
11241 TREE_OPERAND (arg0, 0)),
11242 TREE_OPERAND (arg0, 1));
11243 tem = fold_convert_loc (loc, type, tem);
11247 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11248 if (!tree_int_cst_equal (newmaskt, arg1))
11249 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11257 /* Don't touch a floating-point divide by zero unless the mode
11258 of the constant can represent infinity. */
11259 if (TREE_CODE (arg1) == REAL_CST
11260 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11261 && real_zerop (arg1))
11264 /* Optimize A / A to 1.0 if we don't care about
11265 NaNs or Infinities. Skip the transformation
11266 for non-real operands. */
11267 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11268 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11269 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11270 && operand_equal_p (arg0, arg1, 0))
11272 tree r = build_real (TREE_TYPE (arg0), dconst1);
11274 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11277 /* The complex version of the above A / A optimization. */
11278 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11279 && operand_equal_p (arg0, arg1, 0))
11281 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11282 if (! HONOR_NANS (TYPE_MODE (elem_type))
11283 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11285 tree r = build_real (elem_type, dconst1);
11286 /* omit_two_operands will call fold_convert for us. */
11287 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11291 /* (-A) / (-B) -> A / B */
11292 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11293 return fold_build2_loc (loc, RDIV_EXPR, type,
11294 TREE_OPERAND (arg0, 0),
11295 negate_expr (arg1));
11296 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11297 return fold_build2_loc (loc, RDIV_EXPR, type,
11298 negate_expr (arg0),
11299 TREE_OPERAND (arg1, 0));
11301 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11302 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11303 && real_onep (arg1))
11304 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11306 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11307 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11308 && real_minus_onep (arg1))
11309 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11310 negate_expr (arg0)));
11312 /* If ARG1 is a constant, we can convert this to a multiply by the
11313 reciprocal. This does not have the same rounding properties,
11314 so only do this if -freciprocal-math. We can actually
11315 always safely do it if ARG1 is a power of two, but it's hard to
11316 tell if it is or not in a portable manner. */
11317 if (TREE_CODE (arg1) == REAL_CST)
11319 if (flag_reciprocal_math
11320 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11322 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11323 /* Find the reciprocal if optimizing and the result is exact. */
11327 r = TREE_REAL_CST (arg1);
11328 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11330 tem = build_real (type, r);
11331 return fold_build2_loc (loc, MULT_EXPR, type,
11332 fold_convert_loc (loc, type, arg0), tem);
11336 /* Convert A/B/C to A/(B*C). */
11337 if (flag_reciprocal_math
11338 && TREE_CODE (arg0) == RDIV_EXPR)
11339 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11340 fold_build2_loc (loc, MULT_EXPR, type,
11341 TREE_OPERAND (arg0, 1), arg1));
11343 /* Convert A/(B/C) to (A/B)*C. */
11344 if (flag_reciprocal_math
11345 && TREE_CODE (arg1) == RDIV_EXPR)
11346 return fold_build2_loc (loc, MULT_EXPR, type,
11347 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11348 TREE_OPERAND (arg1, 0)),
11349 TREE_OPERAND (arg1, 1));
11351 /* Convert C1/(X*C2) into (C1/C2)/X. */
11352 if (flag_reciprocal_math
11353 && TREE_CODE (arg1) == MULT_EXPR
11354 && TREE_CODE (arg0) == REAL_CST
11355 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11357 tree tem = const_binop (RDIV_EXPR, arg0,
11358 TREE_OPERAND (arg1, 1));
11360 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11361 TREE_OPERAND (arg1, 0));
11364 if (flag_unsafe_math_optimizations)
11366 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11367 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11369 /* Optimize sin(x)/cos(x) as tan(x). */
11370 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11371 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11372 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11373 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11374 CALL_EXPR_ARG (arg1, 0), 0))
11376 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11378 if (tanfn != NULL_TREE)
11379 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11382 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11383 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11384 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11385 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11386 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11387 CALL_EXPR_ARG (arg1, 0), 0))
11389 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11391 if (tanfn != NULL_TREE)
11393 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11394 CALL_EXPR_ARG (arg0, 0));
11395 return fold_build2_loc (loc, RDIV_EXPR, type,
11396 build_real (type, dconst1), tmp);
11400 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11401 NaNs or Infinities. */
11402 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11403 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11404 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11406 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11407 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11409 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11410 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11411 && operand_equal_p (arg00, arg01, 0))
11413 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11415 if (cosfn != NULL_TREE)
11416 return build_call_expr_loc (loc, cosfn, 1, arg00);
11420 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11421 NaNs or Infinities. */
11422 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11423 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11424 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11426 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11427 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11429 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11430 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11431 && operand_equal_p (arg00, arg01, 0))
11433 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11435 if (cosfn != NULL_TREE)
11437 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11438 return fold_build2_loc (loc, RDIV_EXPR, type,
11439 build_real (type, dconst1),
11445 /* Optimize pow(x,c)/x as pow(x,c-1). */
11446 if (fcode0 == BUILT_IN_POW
11447 || fcode0 == BUILT_IN_POWF
11448 || fcode0 == BUILT_IN_POWL)
11450 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11451 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11452 if (TREE_CODE (arg01) == REAL_CST
11453 && !TREE_OVERFLOW (arg01)
11454 && operand_equal_p (arg1, arg00, 0))
11456 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11460 c = TREE_REAL_CST (arg01);
11461 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11462 arg = build_real (type, c);
11463 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11467 /* Optimize a/root(b/c) into a*root(c/b). */
11468 if (BUILTIN_ROOT_P (fcode1))
11470 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11472 if (TREE_CODE (rootarg) == RDIV_EXPR)
11474 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11475 tree b = TREE_OPERAND (rootarg, 0);
11476 tree c = TREE_OPERAND (rootarg, 1);
11478 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11480 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11481 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11485 /* Optimize x/expN(y) into x*expN(-y). */
11486 if (BUILTIN_EXPONENT_P (fcode1))
11488 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11489 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11490 arg1 = build_call_expr_loc (loc,
11492 fold_convert_loc (loc, type, arg));
11493 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11496 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11497 if (fcode1 == BUILT_IN_POW
11498 || fcode1 == BUILT_IN_POWF
11499 || fcode1 == BUILT_IN_POWL)
11501 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11502 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11503 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11504 tree neg11 = fold_convert_loc (loc, type,
11505 negate_expr (arg11));
11506 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11507 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11512 case TRUNC_DIV_EXPR:
11513 /* Optimize (X & (-A)) / A where A is a power of 2,
11515 if (TREE_CODE (arg0) == BIT_AND_EXPR
11516 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11517 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11519 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11520 arg1, TREE_OPERAND (arg0, 1));
11521 if (sum && integer_zerop (sum)) {
11522 unsigned long pow2;
11524 if (TREE_INT_CST_LOW (arg1))
11525 pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11527 pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11528 + HOST_BITS_PER_WIDE_INT;
11530 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11531 TREE_OPERAND (arg0, 0),
11532 build_int_cst (NULL_TREE, pow2));
11538 case FLOOR_DIV_EXPR:
11539 /* Simplify A / (B << N) where A and B are positive and B is
11540 a power of 2, to A >> (N + log2(B)). */
11541 strict_overflow_p = false;
11542 if (TREE_CODE (arg1) == LSHIFT_EXPR
11543 && (TYPE_UNSIGNED (type)
11544 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11546 tree sval = TREE_OPERAND (arg1, 0);
11547 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11549 tree sh_cnt = TREE_OPERAND (arg1, 1);
11550 unsigned long pow2;
11552 if (TREE_INT_CST_LOW (sval))
11553 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11555 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11556 + HOST_BITS_PER_WIDE_INT;
11558 if (strict_overflow_p)
11559 fold_overflow_warning (("assuming signed overflow does not "
11560 "occur when simplifying A / (B << N)"),
11561 WARN_STRICT_OVERFLOW_MISC);
11563 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11564 sh_cnt, build_int_cst (NULL_TREE, pow2));
11565 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11566 fold_convert_loc (loc, type, arg0), sh_cnt);
11570 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11571 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11572 if (INTEGRAL_TYPE_P (type)
11573 && TYPE_UNSIGNED (type)
11574 && code == FLOOR_DIV_EXPR)
11575 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11579 case ROUND_DIV_EXPR:
11580 case CEIL_DIV_EXPR:
11581 case EXACT_DIV_EXPR:
11582 if (integer_onep (arg1))
11583 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11584 if (integer_zerop (arg1))
11586 /* X / -1 is -X. */
11587 if (!TYPE_UNSIGNED (type)
11588 && TREE_CODE (arg1) == INTEGER_CST
11589 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11590 && TREE_INT_CST_HIGH (arg1) == -1)
11591 return fold_convert_loc (loc, type, negate_expr (arg0));
11593 /* Convert -A / -B to A / B when the type is signed and overflow is
11595 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11596 && TREE_CODE (arg0) == NEGATE_EXPR
11597 && negate_expr_p (arg1))
11599 if (INTEGRAL_TYPE_P (type))
11600 fold_overflow_warning (("assuming signed overflow does not occur "
11601 "when distributing negation across "
11603 WARN_STRICT_OVERFLOW_MISC);
11604 return fold_build2_loc (loc, code, type,
11605 fold_convert_loc (loc, type,
11606 TREE_OPERAND (arg0, 0)),
11607 fold_convert_loc (loc, type,
11608 negate_expr (arg1)));
11610 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11611 && TREE_CODE (arg1) == NEGATE_EXPR
11612 && negate_expr_p (arg0))
11614 if (INTEGRAL_TYPE_P (type))
11615 fold_overflow_warning (("assuming signed overflow does not occur "
11616 "when distributing negation across "
11618 WARN_STRICT_OVERFLOW_MISC);
11619 return fold_build2_loc (loc, code, type,
11620 fold_convert_loc (loc, type,
11621 negate_expr (arg0)),
11622 fold_convert_loc (loc, type,
11623 TREE_OPERAND (arg1, 0)));
11626 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11627 operation, EXACT_DIV_EXPR.
11629 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11630 At one time others generated faster code, it's not clear if they do
11631 after the last round to changes to the DIV code in expmed.c. */
11632 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11633 && multiple_of_p (type, arg0, arg1))
11634 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11636 strict_overflow_p = false;
11637 if (TREE_CODE (arg1) == INTEGER_CST
11638 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11639 &strict_overflow_p)))
11641 if (strict_overflow_p)
11642 fold_overflow_warning (("assuming signed overflow does not occur "
11643 "when simplifying division"),
11644 WARN_STRICT_OVERFLOW_MISC);
11645 return fold_convert_loc (loc, type, tem);
11650 case CEIL_MOD_EXPR:
11651 case FLOOR_MOD_EXPR:
11652 case ROUND_MOD_EXPR:
11653 case TRUNC_MOD_EXPR:
11654 /* X % 1 is always zero, but be sure to preserve any side
11656 if (integer_onep (arg1))
11657 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11659 /* X % 0, return X % 0 unchanged so that we can get the
11660 proper warnings and errors. */
11661 if (integer_zerop (arg1))
11664 /* 0 % X is always zero, but be sure to preserve any side
11665 effects in X. Place this after checking for X == 0. */
11666 if (integer_zerop (arg0))
11667 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11669 /* X % -1 is zero. */
11670 if (!TYPE_UNSIGNED (type)
11671 && TREE_CODE (arg1) == INTEGER_CST
11672 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11673 && TREE_INT_CST_HIGH (arg1) == -1)
11674 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11676 /* X % -C is the same as X % C. */
11677 if (code == TRUNC_MOD_EXPR
11678 && !TYPE_UNSIGNED (type)
11679 && TREE_CODE (arg1) == INTEGER_CST
11680 && !TREE_OVERFLOW (arg1)
11681 && TREE_INT_CST_HIGH (arg1) < 0
11682 && !TYPE_OVERFLOW_TRAPS (type)
11683 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11684 && !sign_bit_p (arg1, arg1))
11685 return fold_build2_loc (loc, code, type,
11686 fold_convert_loc (loc, type, arg0),
11687 fold_convert_loc (loc, type,
11688 negate_expr (arg1)));
11690 /* X % -Y is the same as X % Y. */
11691 if (code == TRUNC_MOD_EXPR
11692 && !TYPE_UNSIGNED (type)
11693 && TREE_CODE (arg1) == NEGATE_EXPR
11694 && !TYPE_OVERFLOW_TRAPS (type))
11695 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11696 fold_convert_loc (loc, type,
11697 TREE_OPERAND (arg1, 0)));
11699 strict_overflow_p = false;
11700 if (TREE_CODE (arg1) == INTEGER_CST
11701 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11702 &strict_overflow_p)))
11704 if (strict_overflow_p)
11705 fold_overflow_warning (("assuming signed overflow does not occur "
11706 "when simplifying modulus"),
11707 WARN_STRICT_OVERFLOW_MISC);
11708 return fold_convert_loc (loc, type, tem);
11711 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11712 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11713 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11714 && (TYPE_UNSIGNED (type)
11715 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11718 /* Also optimize A % (C << N) where C is a power of 2,
11719 to A & ((C << N) - 1). */
11720 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11721 c = TREE_OPERAND (arg1, 0);
11723 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11726 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11727 build_int_cst (TREE_TYPE (arg1), 1));
11728 if (strict_overflow_p)
11729 fold_overflow_warning (("assuming signed overflow does not "
11730 "occur when simplifying "
11731 "X % (power of two)"),
11732 WARN_STRICT_OVERFLOW_MISC);
11733 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11734 fold_convert_loc (loc, type, arg0),
11735 fold_convert_loc (loc, type, mask));
11743 if (integer_all_onesp (arg0))
11744 return omit_one_operand_loc (loc, type, arg0, arg1);
11748 /* Optimize -1 >> x for arithmetic right shifts. */
11749 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11750 && tree_expr_nonnegative_p (arg1))
11751 return omit_one_operand_loc (loc, type, arg0, arg1);
11752 /* ... fall through ... */
11756 if (integer_zerop (arg1))
11757 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11758 if (integer_zerop (arg0))
11759 return omit_one_operand_loc (loc, type, arg0, arg1);
11761 /* Since negative shift count is not well-defined,
11762 don't try to compute it in the compiler. */
11763 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11766 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11767 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11768 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11769 && host_integerp (TREE_OPERAND (arg0, 1), false)
11770 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11772 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11773 + TREE_INT_CST_LOW (arg1));
11775 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11776 being well defined. */
11777 if (low >= TYPE_PRECISION (type))
11779 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11780 low = low % TYPE_PRECISION (type);
11781 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11782 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11783 TREE_OPERAND (arg0, 0));
11785 low = TYPE_PRECISION (type) - 1;
11788 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11789 build_int_cst (type, low));
11792 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11793 into x & ((unsigned)-1 >> c) for unsigned types. */
11794 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11795 || (TYPE_UNSIGNED (type)
11796 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11797 && host_integerp (arg1, false)
11798 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11799 && host_integerp (TREE_OPERAND (arg0, 1), false)
11800 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11802 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11803 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11809 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11811 lshift = build_int_cst (type, -1);
11812 lshift = int_const_binop (code, lshift, arg1, 0);
11814 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11818 /* Rewrite an LROTATE_EXPR by a constant into an
11819 RROTATE_EXPR by a new constant. */
11820 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11822 tree tem = build_int_cst (TREE_TYPE (arg1),
11823 TYPE_PRECISION (type));
11824 tem = const_binop (MINUS_EXPR, tem, arg1);
11825 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11828 /* If we have a rotate of a bit operation with the rotate count and
11829 the second operand of the bit operation both constant,
11830 permute the two operations. */
11831 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11832 && (TREE_CODE (arg0) == BIT_AND_EXPR
11833 || TREE_CODE (arg0) == BIT_IOR_EXPR
11834 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11835 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11836 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11837 fold_build2_loc (loc, code, type,
11838 TREE_OPERAND (arg0, 0), arg1),
11839 fold_build2_loc (loc, code, type,
11840 TREE_OPERAND (arg0, 1), arg1));
11842 /* Two consecutive rotates adding up to the precision of the
11843 type can be ignored. */
11844 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11845 && TREE_CODE (arg0) == RROTATE_EXPR
11846 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11847 && TREE_INT_CST_HIGH (arg1) == 0
11848 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11849 && ((TREE_INT_CST_LOW (arg1)
11850 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11851 == (unsigned int) TYPE_PRECISION (type)))
11852 return TREE_OPERAND (arg0, 0);
11854 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11855 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11856 if the latter can be further optimized. */
11857 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11858 && TREE_CODE (arg0) == BIT_AND_EXPR
11859 && TREE_CODE (arg1) == INTEGER_CST
11860 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11862 tree mask = fold_build2_loc (loc, code, type,
11863 fold_convert_loc (loc, type,
11864 TREE_OPERAND (arg0, 1)),
11866 tree shift = fold_build2_loc (loc, code, type,
11867 fold_convert_loc (loc, type,
11868 TREE_OPERAND (arg0, 0)),
11870 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11878 if (operand_equal_p (arg0, arg1, 0))
11879 return omit_one_operand_loc (loc, type, arg0, arg1);
11880 if (INTEGRAL_TYPE_P (type)
11881 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11882 return omit_one_operand_loc (loc, type, arg1, arg0);
11883 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11889 if (operand_equal_p (arg0, arg1, 0))
11890 return omit_one_operand_loc (loc, type, arg0, arg1);
11891 if (INTEGRAL_TYPE_P (type)
11892 && TYPE_MAX_VALUE (type)
11893 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11894 return omit_one_operand_loc (loc, type, arg1, arg0);
11895 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11900 case TRUTH_ANDIF_EXPR:
11901 /* Note that the operands of this must be ints
11902 and their values must be 0 or 1.
11903 ("true" is a fixed value perhaps depending on the language.) */
11904 /* If first arg is constant zero, return it. */
11905 if (integer_zerop (arg0))
11906 return fold_convert_loc (loc, type, arg0);
11907 case TRUTH_AND_EXPR:
11908 /* If either arg is constant true, drop it. */
11909 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11910 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11911 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11912 /* Preserve sequence points. */
11913 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11914 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11915 /* If second arg is constant zero, result is zero, but first arg
11916 must be evaluated. */
11917 if (integer_zerop (arg1))
11918 return omit_one_operand_loc (loc, type, arg1, arg0);
11919 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11920 case will be handled here. */
11921 if (integer_zerop (arg0))
11922 return omit_one_operand_loc (loc, type, arg0, arg1);
11924 /* !X && X is always false. */
11925 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11926 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11927 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11928 /* X && !X is always false. */
11929 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11930 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11931 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11933 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11934 means A >= Y && A != MAX, but in this case we know that
11937 if (!TREE_SIDE_EFFECTS (arg0)
11938 && !TREE_SIDE_EFFECTS (arg1))
11940 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11941 if (tem && !operand_equal_p (tem, arg0, 0))
11942 return fold_build2_loc (loc, code, type, tem, arg1);
11944 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11945 if (tem && !operand_equal_p (tem, arg1, 0))
11946 return fold_build2_loc (loc, code, type, arg0, tem);
11950 /* We only do these simplifications if we are optimizing. */
11954 /* Check for things like (A || B) && (A || C). We can convert this
11955 to A || (B && C). Note that either operator can be any of the four
11956 truth and/or operations and the transformation will still be
11957 valid. Also note that we only care about order for the
11958 ANDIF and ORIF operators. If B contains side effects, this
11959 might change the truth-value of A. */
11960 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11961 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11962 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11963 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11964 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11965 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11967 tree a00 = TREE_OPERAND (arg0, 0);
11968 tree a01 = TREE_OPERAND (arg0, 1);
11969 tree a10 = TREE_OPERAND (arg1, 0);
11970 tree a11 = TREE_OPERAND (arg1, 1);
11971 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11972 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11973 && (code == TRUTH_AND_EXPR
11974 || code == TRUTH_OR_EXPR));
11976 if (operand_equal_p (a00, a10, 0))
11977 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11978 fold_build2_loc (loc, code, type, a01, a11));
11979 else if (commutative && operand_equal_p (a00, a11, 0))
11980 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11981 fold_build2_loc (loc, code, type, a01, a10));
11982 else if (commutative && operand_equal_p (a01, a10, 0))
11983 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
11984 fold_build2_loc (loc, code, type, a00, a11));
11986 /* This case if tricky because we must either have commutative
11987 operators or else A10 must not have side-effects. */
11989 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11990 && operand_equal_p (a01, a11, 0))
11991 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11992 fold_build2_loc (loc, code, type, a00, a10),
11996 /* See if we can build a range comparison. */
11997 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12000 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12001 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12003 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12005 return fold_build2_loc (loc, code, type, tem, arg1);
12008 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12009 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12011 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12013 return fold_build2_loc (loc, code, type, arg0, tem);
12016 /* Check for the possibility of merging component references. If our
12017 lhs is another similar operation, try to merge its rhs with our
12018 rhs. Then try to merge our lhs and rhs. */
12019 if (TREE_CODE (arg0) == code
12020 && 0 != (tem = fold_truthop (loc, code, type,
12021 TREE_OPERAND (arg0, 1), arg1)))
12022 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12024 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12029 case TRUTH_ORIF_EXPR:
12030 /* Note that the operands of this must be ints
12031 and their values must be 0 or true.
12032 ("true" is a fixed value perhaps depending on the language.) */
12033 /* If first arg is constant true, return it. */
12034 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12035 return fold_convert_loc (loc, type, arg0);
12036 case TRUTH_OR_EXPR:
12037 /* If either arg is constant zero, drop it. */
12038 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12039 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12040 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12041 /* Preserve sequence points. */
12042 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12043 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12044 /* If second arg is constant true, result is true, but we must
12045 evaluate first arg. */
12046 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12047 return omit_one_operand_loc (loc, type, arg1, arg0);
12048 /* Likewise for first arg, but note this only occurs here for
12050 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12051 return omit_one_operand_loc (loc, type, arg0, arg1);
12053 /* !X || X is always true. */
12054 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12055 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12056 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12057 /* X || !X is always true. */
12058 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12059 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12060 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12062 /* (X && !Y) || (!X && Y) is X ^ Y */
12063 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12064 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12066 tree a0, a1, l0, l1, n0, n1;
12068 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12069 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12071 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12072 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12074 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12075 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12077 if ((operand_equal_p (n0, a0, 0)
12078 && operand_equal_p (n1, a1, 0))
12079 || (operand_equal_p (n0, a1, 0)
12080 && operand_equal_p (n1, a0, 0)))
12081 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12085 case TRUTH_XOR_EXPR:
12086 /* If the second arg is constant zero, drop it. */
12087 if (integer_zerop (arg1))
12088 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12089 /* If the second arg is constant true, this is a logical inversion. */
12090 if (integer_onep (arg1))
12092 /* Only call invert_truthvalue if operand is a truth value. */
12093 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12094 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12096 tem = invert_truthvalue_loc (loc, arg0);
12097 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12099 /* Identical arguments cancel to zero. */
12100 if (operand_equal_p (arg0, arg1, 0))
12101 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12103 /* !X ^ X is always true. */
12104 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12105 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12106 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12108 /* X ^ !X is always true. */
12109 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12110 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12111 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12120 tem = fold_comparison (loc, code, type, op0, op1);
12121 if (tem != NULL_TREE)
12124 /* bool_var != 0 becomes bool_var. */
12125 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12126 && code == NE_EXPR)
12127 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12129 /* bool_var == 1 becomes bool_var. */
12130 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12131 && code == EQ_EXPR)
12132 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12134 /* bool_var != 1 becomes !bool_var. */
12135 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12136 && code == NE_EXPR)
12137 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12138 fold_convert_loc (loc, type, arg0));
12140 /* bool_var == 0 becomes !bool_var. */
12141 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12142 && code == EQ_EXPR)
12143 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12144 fold_convert_loc (loc, type, arg0));
12146 /* !exp != 0 becomes !exp */
12147 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12148 && code == NE_EXPR)
12149 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12151 /* If this is an equality comparison of the address of two non-weak,
12152 unaliased symbols neither of which are extern (since we do not
12153 have access to attributes for externs), then we know the result. */
12154 if (TREE_CODE (arg0) == ADDR_EXPR
12155 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12156 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12157 && ! lookup_attribute ("alias",
12158 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12159 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12160 && TREE_CODE (arg1) == ADDR_EXPR
12161 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12162 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12163 && ! lookup_attribute ("alias",
12164 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12165 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12167 /* We know that we're looking at the address of two
12168 non-weak, unaliased, static _DECL nodes.
12170 It is both wasteful and incorrect to call operand_equal_p
12171 to compare the two ADDR_EXPR nodes. It is wasteful in that
12172 all we need to do is test pointer equality for the arguments
12173 to the two ADDR_EXPR nodes. It is incorrect to use
12174 operand_equal_p as that function is NOT equivalent to a
12175 C equality test. It can in fact return false for two
12176 objects which would test as equal using the C equality
12178 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12179 return constant_boolean_node (equal
12180 ? code == EQ_EXPR : code != EQ_EXPR,
12184 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12185 a MINUS_EXPR of a constant, we can convert it into a comparison with
12186 a revised constant as long as no overflow occurs. */
12187 if (TREE_CODE (arg1) == INTEGER_CST
12188 && (TREE_CODE (arg0) == PLUS_EXPR
12189 || TREE_CODE (arg0) == MINUS_EXPR)
12190 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12191 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12192 ? MINUS_EXPR : PLUS_EXPR,
12193 fold_convert_loc (loc, TREE_TYPE (arg0),
12195 TREE_OPERAND (arg0, 1)))
12196 && !TREE_OVERFLOW (tem))
12197 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12199 /* Similarly for a NEGATE_EXPR. */
12200 if (TREE_CODE (arg0) == NEGATE_EXPR
12201 && TREE_CODE (arg1) == INTEGER_CST
12202 && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12204 && TREE_CODE (tem) == INTEGER_CST
12205 && !TREE_OVERFLOW (tem))
12206 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12208 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12209 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12210 && TREE_CODE (arg1) == INTEGER_CST
12211 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12212 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12213 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12214 fold_convert_loc (loc,
12217 TREE_OPERAND (arg0, 1)));
12219 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12220 if ((TREE_CODE (arg0) == PLUS_EXPR
12221 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12222 || TREE_CODE (arg0) == MINUS_EXPR)
12223 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12226 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12227 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12229 tree val = TREE_OPERAND (arg0, 1);
12230 return omit_two_operands_loc (loc, type,
12231 fold_build2_loc (loc, code, type,
12233 build_int_cst (TREE_TYPE (val),
12235 TREE_OPERAND (arg0, 0), arg1);
12238 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12239 if (TREE_CODE (arg0) == MINUS_EXPR
12240 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12241 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12244 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12246 return omit_two_operands_loc (loc, type,
12248 ? boolean_true_node : boolean_false_node,
12249 TREE_OPERAND (arg0, 1), arg1);
12252 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12253 for !=. Don't do this for ordered comparisons due to overflow. */
12254 if (TREE_CODE (arg0) == MINUS_EXPR
12255 && integer_zerop (arg1))
12256 return fold_build2_loc (loc, code, type,
12257 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12259 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12260 if (TREE_CODE (arg0) == ABS_EXPR
12261 && (integer_zerop (arg1) || real_zerop (arg1)))
12262 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12264 /* If this is an EQ or NE comparison with zero and ARG0 is
12265 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12266 two operations, but the latter can be done in one less insn
12267 on machines that have only two-operand insns or on which a
12268 constant cannot be the first operand. */
12269 if (TREE_CODE (arg0) == BIT_AND_EXPR
12270 && integer_zerop (arg1))
12272 tree arg00 = TREE_OPERAND (arg0, 0);
12273 tree arg01 = TREE_OPERAND (arg0, 1);
12274 if (TREE_CODE (arg00) == LSHIFT_EXPR
12275 && integer_onep (TREE_OPERAND (arg00, 0)))
12277 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12278 arg01, TREE_OPERAND (arg00, 1));
12279 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12280 build_int_cst (TREE_TYPE (arg0), 1));
12281 return fold_build2_loc (loc, code, type,
12282 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12285 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12286 && integer_onep (TREE_OPERAND (arg01, 0)))
12288 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12289 arg00, TREE_OPERAND (arg01, 1));
12290 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12291 build_int_cst (TREE_TYPE (arg0), 1));
12292 return fold_build2_loc (loc, code, type,
12293 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12298 /* If this is an NE or EQ comparison of zero against the result of a
12299 signed MOD operation whose second operand is a power of 2, make
12300 the MOD operation unsigned since it is simpler and equivalent. */
12301 if (integer_zerop (arg1)
12302 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12303 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12304 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12305 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12306 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12307 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12309 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12310 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12311 fold_convert_loc (loc, newtype,
12312 TREE_OPERAND (arg0, 0)),
12313 fold_convert_loc (loc, newtype,
12314 TREE_OPERAND (arg0, 1)));
12316 return fold_build2_loc (loc, code, type, newmod,
12317 fold_convert_loc (loc, newtype, arg1));
12320 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12321 C1 is a valid shift constant, and C2 is a power of two, i.e.
12323 if (TREE_CODE (arg0) == BIT_AND_EXPR
12324 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12325 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12327 && integer_pow2p (TREE_OPERAND (arg0, 1))
12328 && integer_zerop (arg1))
12330 tree itype = TREE_TYPE (arg0);
12331 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12332 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12334 /* Check for a valid shift count. */
12335 if (TREE_INT_CST_HIGH (arg001) == 0
12336 && TREE_INT_CST_LOW (arg001) < prec)
12338 tree arg01 = TREE_OPERAND (arg0, 1);
12339 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12340 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12341 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12342 can be rewritten as (X & (C2 << C1)) != 0. */
12343 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12345 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12346 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12347 return fold_build2_loc (loc, code, type, tem,
12348 fold_convert_loc (loc, itype, arg1));
12350 /* Otherwise, for signed (arithmetic) shifts,
12351 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12352 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12353 else if (!TYPE_UNSIGNED (itype))
12354 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12355 arg000, build_int_cst (itype, 0));
12356 /* Otherwise, of unsigned (logical) shifts,
12357 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12358 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12360 return omit_one_operand_loc (loc, type,
12361 code == EQ_EXPR ? integer_one_node
12362 : integer_zero_node,
12367 /* If this is an NE comparison of zero with an AND of one, remove the
12368 comparison since the AND will give the correct value. */
12369 if (code == NE_EXPR
12370 && integer_zerop (arg1)
12371 && TREE_CODE (arg0) == BIT_AND_EXPR
12372 && integer_onep (TREE_OPERAND (arg0, 1)))
12373 return fold_convert_loc (loc, type, arg0);
12375 /* If we have (A & C) == C where C is a power of 2, convert this into
12376 (A & C) != 0. Similarly for NE_EXPR. */
12377 if (TREE_CODE (arg0) == BIT_AND_EXPR
12378 && integer_pow2p (TREE_OPERAND (arg0, 1))
12379 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12380 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12381 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12382 integer_zero_node));
12384 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12385 bit, then fold the expression into A < 0 or A >= 0. */
12386 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12390 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12391 Similarly for NE_EXPR. */
12392 if (TREE_CODE (arg0) == BIT_AND_EXPR
12393 && TREE_CODE (arg1) == INTEGER_CST
12394 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12396 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12397 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12398 TREE_OPERAND (arg0, 1));
12400 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12401 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12403 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12404 if (integer_nonzerop (dandnotc))
12405 return omit_one_operand_loc (loc, type, rslt, arg0);
12408 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12409 Similarly for NE_EXPR. */
12410 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12411 && TREE_CODE (arg1) == INTEGER_CST
12412 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12414 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12416 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12417 TREE_OPERAND (arg0, 1),
12418 fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12419 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12420 if (integer_nonzerop (candnotd))
12421 return omit_one_operand_loc (loc, type, rslt, arg0);
12424 /* If this is a comparison of a field, we may be able to simplify it. */
12425 if ((TREE_CODE (arg0) == COMPONENT_REF
12426 || TREE_CODE (arg0) == BIT_FIELD_REF)
12427 /* Handle the constant case even without -O
12428 to make sure the warnings are given. */
12429 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12431 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12436 /* Optimize comparisons of strlen vs zero to a compare of the
12437 first character of the string vs zero. To wit,
12438 strlen(ptr) == 0 => *ptr == 0
12439 strlen(ptr) != 0 => *ptr != 0
12440 Other cases should reduce to one of these two (or a constant)
12441 due to the return value of strlen being unsigned. */
12442 if (TREE_CODE (arg0) == CALL_EXPR
12443 && integer_zerop (arg1))
12445 tree fndecl = get_callee_fndecl (arg0);
12448 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12449 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12450 && call_expr_nargs (arg0) == 1
12451 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12453 tree iref = build_fold_indirect_ref_loc (loc,
12454 CALL_EXPR_ARG (arg0, 0));
12455 return fold_build2_loc (loc, code, type, iref,
12456 build_int_cst (TREE_TYPE (iref), 0));
12460 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12461 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12462 if (TREE_CODE (arg0) == RSHIFT_EXPR
12463 && integer_zerop (arg1)
12464 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12466 tree arg00 = TREE_OPERAND (arg0, 0);
12467 tree arg01 = TREE_OPERAND (arg0, 1);
12468 tree itype = TREE_TYPE (arg00);
12469 if (TREE_INT_CST_HIGH (arg01) == 0
12470 && TREE_INT_CST_LOW (arg01)
12471 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12473 if (TYPE_UNSIGNED (itype))
12475 itype = signed_type_for (itype);
12476 arg00 = fold_convert_loc (loc, itype, arg00);
12478 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12479 type, arg00, build_int_cst (itype, 0));
12483 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12484 if (integer_zerop (arg1)
12485 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12486 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12487 TREE_OPERAND (arg0, 1));
12489 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12490 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12491 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12492 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12493 build_int_cst (TREE_TYPE (arg0), 0));
12494 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12495 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12496 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12497 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12498 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12499 build_int_cst (TREE_TYPE (arg0), 0));
12501 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12502 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12503 && TREE_CODE (arg1) == INTEGER_CST
12504 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12505 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12506 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12507 TREE_OPERAND (arg0, 1), arg1));
12509 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12510 (X & C) == 0 when C is a single bit. */
12511 if (TREE_CODE (arg0) == BIT_AND_EXPR
12512 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12513 && integer_zerop (arg1)
12514 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12516 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12517 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12518 TREE_OPERAND (arg0, 1));
12519 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12521 fold_convert_loc (loc, TREE_TYPE (arg0),
12525 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12526 constant C is a power of two, i.e. a single bit. */
12527 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12528 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12529 && integer_zerop (arg1)
12530 && integer_pow2p (TREE_OPERAND (arg0, 1))
12531 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12532 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12534 tree arg00 = TREE_OPERAND (arg0, 0);
12535 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12536 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12539 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12540 when is C is a power of two, i.e. a single bit. */
12541 if (TREE_CODE (arg0) == BIT_AND_EXPR
12542 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12543 && integer_zerop (arg1)
12544 && integer_pow2p (TREE_OPERAND (arg0, 1))
12545 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12546 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12548 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12549 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12550 arg000, TREE_OPERAND (arg0, 1));
12551 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12552 tem, build_int_cst (TREE_TYPE (tem), 0));
12555 if (integer_zerop (arg1)
12556 && tree_expr_nonzero_p (arg0))
12558 tree res = constant_boolean_node (code==NE_EXPR, type);
12559 return omit_one_operand_loc (loc, type, res, arg0);
12562 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12563 if (TREE_CODE (arg0) == NEGATE_EXPR
12564 && TREE_CODE (arg1) == NEGATE_EXPR)
12565 return fold_build2_loc (loc, code, type,
12566 TREE_OPERAND (arg0, 0),
12567 fold_convert_loc (loc, TREE_TYPE (arg0),
12568 TREE_OPERAND (arg1, 0)));
12570 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12571 if (TREE_CODE (arg0) == BIT_AND_EXPR
12572 && TREE_CODE (arg1) == BIT_AND_EXPR)
12574 tree arg00 = TREE_OPERAND (arg0, 0);
12575 tree arg01 = TREE_OPERAND (arg0, 1);
12576 tree arg10 = TREE_OPERAND (arg1, 0);
12577 tree arg11 = TREE_OPERAND (arg1, 1);
12578 tree itype = TREE_TYPE (arg0);
12580 if (operand_equal_p (arg01, arg11, 0))
12581 return fold_build2_loc (loc, code, type,
12582 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12583 fold_build2_loc (loc,
12584 BIT_XOR_EXPR, itype,
12587 build_int_cst (itype, 0));
12589 if (operand_equal_p (arg01, arg10, 0))
12590 return fold_build2_loc (loc, code, type,
12591 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12592 fold_build2_loc (loc,
12593 BIT_XOR_EXPR, itype,
12596 build_int_cst (itype, 0));
12598 if (operand_equal_p (arg00, arg11, 0))
12599 return fold_build2_loc (loc, code, type,
12600 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12601 fold_build2_loc (loc,
12602 BIT_XOR_EXPR, itype,
12605 build_int_cst (itype, 0));
12607 if (operand_equal_p (arg00, arg10, 0))
12608 return fold_build2_loc (loc, code, type,
12609 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12610 fold_build2_loc (loc,
12611 BIT_XOR_EXPR, itype,
12614 build_int_cst (itype, 0));
12617 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12618 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12620 tree arg00 = TREE_OPERAND (arg0, 0);
12621 tree arg01 = TREE_OPERAND (arg0, 1);
12622 tree arg10 = TREE_OPERAND (arg1, 0);
12623 tree arg11 = TREE_OPERAND (arg1, 1);
12624 tree itype = TREE_TYPE (arg0);
12626 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12627 operand_equal_p guarantees no side-effects so we don't need
12628 to use omit_one_operand on Z. */
12629 if (operand_equal_p (arg01, arg11, 0))
12630 return fold_build2_loc (loc, code, type, arg00,
12631 fold_convert_loc (loc, TREE_TYPE (arg00),
12633 if (operand_equal_p (arg01, arg10, 0))
12634 return fold_build2_loc (loc, code, type, arg00,
12635 fold_convert_loc (loc, TREE_TYPE (arg00),
12637 if (operand_equal_p (arg00, arg11, 0))
12638 return fold_build2_loc (loc, code, type, arg01,
12639 fold_convert_loc (loc, TREE_TYPE (arg01),
12641 if (operand_equal_p (arg00, arg10, 0))
12642 return fold_build2_loc (loc, code, type, arg01,
12643 fold_convert_loc (loc, TREE_TYPE (arg01),
12646 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12647 if (TREE_CODE (arg01) == INTEGER_CST
12648 && TREE_CODE (arg11) == INTEGER_CST)
12650 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12651 fold_convert_loc (loc, itype, arg11));
12652 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12653 return fold_build2_loc (loc, code, type, tem,
12654 fold_convert_loc (loc, itype, arg10));
12658 /* Attempt to simplify equality/inequality comparisons of complex
12659 values. Only lower the comparison if the result is known or
12660 can be simplified to a single scalar comparison. */
12661 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12662 || TREE_CODE (arg0) == COMPLEX_CST)
12663 && (TREE_CODE (arg1) == COMPLEX_EXPR
12664 || TREE_CODE (arg1) == COMPLEX_CST))
12666 tree real0, imag0, real1, imag1;
12669 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12671 real0 = TREE_OPERAND (arg0, 0);
12672 imag0 = TREE_OPERAND (arg0, 1);
12676 real0 = TREE_REALPART (arg0);
12677 imag0 = TREE_IMAGPART (arg0);
12680 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12682 real1 = TREE_OPERAND (arg1, 0);
12683 imag1 = TREE_OPERAND (arg1, 1);
12687 real1 = TREE_REALPART (arg1);
12688 imag1 = TREE_IMAGPART (arg1);
12691 rcond = fold_binary_loc (loc, code, type, real0, real1);
12692 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12694 if (integer_zerop (rcond))
12696 if (code == EQ_EXPR)
12697 return omit_two_operands_loc (loc, type, boolean_false_node,
12699 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12703 if (code == NE_EXPR)
12704 return omit_two_operands_loc (loc, type, boolean_true_node,
12706 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12710 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12711 if (icond && TREE_CODE (icond) == INTEGER_CST)
12713 if (integer_zerop (icond))
12715 if (code == EQ_EXPR)
12716 return omit_two_operands_loc (loc, type, boolean_false_node,
12718 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12722 if (code == NE_EXPR)
12723 return omit_two_operands_loc (loc, type, boolean_true_node,
12725 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12736 tem = fold_comparison (loc, code, type, op0, op1);
12737 if (tem != NULL_TREE)
12740 /* Transform comparisons of the form X +- C CMP X. */
12741 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12742 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12743 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12744 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12745 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12746 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12748 tree arg01 = TREE_OPERAND (arg0, 1);
12749 enum tree_code code0 = TREE_CODE (arg0);
12752 if (TREE_CODE (arg01) == REAL_CST)
12753 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12755 is_positive = tree_int_cst_sgn (arg01);
12757 /* (X - c) > X becomes false. */
12758 if (code == GT_EXPR
12759 && ((code0 == MINUS_EXPR && is_positive >= 0)
12760 || (code0 == PLUS_EXPR && is_positive <= 0)))
12762 if (TREE_CODE (arg01) == INTEGER_CST
12763 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12764 fold_overflow_warning (("assuming signed overflow does not "
12765 "occur when assuming that (X - c) > X "
12766 "is always false"),
12767 WARN_STRICT_OVERFLOW_ALL);
12768 return constant_boolean_node (0, type);
12771 /* Likewise (X + c) < X becomes false. */
12772 if (code == LT_EXPR
12773 && ((code0 == PLUS_EXPR && is_positive >= 0)
12774 || (code0 == MINUS_EXPR && is_positive <= 0)))
12776 if (TREE_CODE (arg01) == INTEGER_CST
12777 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12778 fold_overflow_warning (("assuming signed overflow does not "
12779 "occur when assuming that "
12780 "(X + c) < X is always false"),
12781 WARN_STRICT_OVERFLOW_ALL);
12782 return constant_boolean_node (0, type);
12785 /* Convert (X - c) <= X to true. */
12786 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12788 && ((code0 == MINUS_EXPR && is_positive >= 0)
12789 || (code0 == PLUS_EXPR && is_positive <= 0)))
12791 if (TREE_CODE (arg01) == INTEGER_CST
12792 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12793 fold_overflow_warning (("assuming signed overflow does not "
12794 "occur when assuming that "
12795 "(X - c) <= X is always true"),
12796 WARN_STRICT_OVERFLOW_ALL);
12797 return constant_boolean_node (1, type);
12800 /* Convert (X + c) >= X to true. */
12801 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12803 && ((code0 == PLUS_EXPR && is_positive >= 0)
12804 || (code0 == MINUS_EXPR && is_positive <= 0)))
12806 if (TREE_CODE (arg01) == INTEGER_CST
12807 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12808 fold_overflow_warning (("assuming signed overflow does not "
12809 "occur when assuming that "
12810 "(X + c) >= X is always true"),
12811 WARN_STRICT_OVERFLOW_ALL);
12812 return constant_boolean_node (1, type);
12815 if (TREE_CODE (arg01) == INTEGER_CST)
12817 /* Convert X + c > X and X - c < X to true for integers. */
12818 if (code == GT_EXPR
12819 && ((code0 == PLUS_EXPR && is_positive > 0)
12820 || (code0 == MINUS_EXPR && is_positive < 0)))
12822 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12823 fold_overflow_warning (("assuming signed overflow does "
12824 "not occur when assuming that "
12825 "(X + c) > X is always true"),
12826 WARN_STRICT_OVERFLOW_ALL);
12827 return constant_boolean_node (1, type);
12830 if (code == LT_EXPR
12831 && ((code0 == MINUS_EXPR && is_positive > 0)
12832 || (code0 == PLUS_EXPR && is_positive < 0)))
12834 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12835 fold_overflow_warning (("assuming signed overflow does "
12836 "not occur when assuming that "
12837 "(X - c) < X is always true"),
12838 WARN_STRICT_OVERFLOW_ALL);
12839 return constant_boolean_node (1, type);
12842 /* Convert X + c <= X and X - c >= X to false for integers. */
12843 if (code == LE_EXPR
12844 && ((code0 == PLUS_EXPR && is_positive > 0)
12845 || (code0 == MINUS_EXPR && is_positive < 0)))
12847 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12848 fold_overflow_warning (("assuming signed overflow does "
12849 "not occur when assuming that "
12850 "(X + c) <= X is always false"),
12851 WARN_STRICT_OVERFLOW_ALL);
12852 return constant_boolean_node (0, type);
12855 if (code == GE_EXPR
12856 && ((code0 == MINUS_EXPR && is_positive > 0)
12857 || (code0 == PLUS_EXPR && is_positive < 0)))
12859 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12860 fold_overflow_warning (("assuming signed overflow does "
12861 "not occur when assuming that "
12862 "(X - c) >= X is always false"),
12863 WARN_STRICT_OVERFLOW_ALL);
12864 return constant_boolean_node (0, type);
12869 /* Comparisons with the highest or lowest possible integer of
12870 the specified precision will have known values. */
12872 tree arg1_type = TREE_TYPE (arg1);
12873 unsigned int width = TYPE_PRECISION (arg1_type);
12875 if (TREE_CODE (arg1) == INTEGER_CST
12876 && width <= 2 * HOST_BITS_PER_WIDE_INT
12877 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12879 HOST_WIDE_INT signed_max_hi;
12880 unsigned HOST_WIDE_INT signed_max_lo;
12881 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12883 if (width <= HOST_BITS_PER_WIDE_INT)
12885 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12890 if (TYPE_UNSIGNED (arg1_type))
12892 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12898 max_lo = signed_max_lo;
12899 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12905 width -= HOST_BITS_PER_WIDE_INT;
12906 signed_max_lo = -1;
12907 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12912 if (TYPE_UNSIGNED (arg1_type))
12914 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12919 max_hi = signed_max_hi;
12920 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12924 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12925 && TREE_INT_CST_LOW (arg1) == max_lo)
12929 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12932 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12935 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12938 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12940 /* The GE_EXPR and LT_EXPR cases above are not normally
12941 reached because of previous transformations. */
12946 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12948 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12952 arg1 = const_binop (PLUS_EXPR, arg1,
12953 build_int_cst (TREE_TYPE (arg1), 1));
12954 return fold_build2_loc (loc, EQ_EXPR, type,
12955 fold_convert_loc (loc,
12956 TREE_TYPE (arg1), arg0),
12959 arg1 = const_binop (PLUS_EXPR, arg1,
12960 build_int_cst (TREE_TYPE (arg1), 1));
12961 return fold_build2_loc (loc, NE_EXPR, type,
12962 fold_convert_loc (loc, TREE_TYPE (arg1),
12968 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12970 && TREE_INT_CST_LOW (arg1) == min_lo)
12974 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12977 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12980 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12983 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12988 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12990 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12994 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
12995 return fold_build2_loc (loc, NE_EXPR, type,
12996 fold_convert_loc (loc,
12997 TREE_TYPE (arg1), arg0),
13000 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13001 return fold_build2_loc (loc, EQ_EXPR, type,
13002 fold_convert_loc (loc, TREE_TYPE (arg1),
13009 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13010 && TREE_INT_CST_LOW (arg1) == signed_max_lo
13011 && TYPE_UNSIGNED (arg1_type)
13012 /* We will flip the signedness of the comparison operator
13013 associated with the mode of arg1, so the sign bit is
13014 specified by this mode. Check that arg1 is the signed
13015 max associated with this sign bit. */
13016 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13017 /* signed_type does not work on pointer types. */
13018 && INTEGRAL_TYPE_P (arg1_type))
13020 /* The following case also applies to X < signed_max+1
13021 and X >= signed_max+1 because previous transformations. */
13022 if (code == LE_EXPR || code == GT_EXPR)
13025 st = signed_type_for (TREE_TYPE (arg1));
13026 return fold_build2_loc (loc,
13027 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13028 type, fold_convert_loc (loc, st, arg0),
13029 build_int_cst (st, 0));
13035 /* If we are comparing an ABS_EXPR with a constant, we can
13036 convert all the cases into explicit comparisons, but they may
13037 well not be faster than doing the ABS and one comparison.
13038 But ABS (X) <= C is a range comparison, which becomes a subtraction
13039 and a comparison, and is probably faster. */
13040 if (code == LE_EXPR
13041 && TREE_CODE (arg1) == INTEGER_CST
13042 && TREE_CODE (arg0) == ABS_EXPR
13043 && ! TREE_SIDE_EFFECTS (arg0)
13044 && (0 != (tem = negate_expr (arg1)))
13045 && TREE_CODE (tem) == INTEGER_CST
13046 && !TREE_OVERFLOW (tem))
13047 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13048 build2 (GE_EXPR, type,
13049 TREE_OPERAND (arg0, 0), tem),
13050 build2 (LE_EXPR, type,
13051 TREE_OPERAND (arg0, 0), arg1));
13053 /* Convert ABS_EXPR<x> >= 0 to true. */
13054 strict_overflow_p = false;
13055 if (code == GE_EXPR
13056 && (integer_zerop (arg1)
13057 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13058 && real_zerop (arg1)))
13059 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13061 if (strict_overflow_p)
13062 fold_overflow_warning (("assuming signed overflow does not occur "
13063 "when simplifying comparison of "
13064 "absolute value and zero"),
13065 WARN_STRICT_OVERFLOW_CONDITIONAL);
13066 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13069 /* Convert ABS_EXPR<x> < 0 to false. */
13070 strict_overflow_p = false;
13071 if (code == LT_EXPR
13072 && (integer_zerop (arg1) || real_zerop (arg1))
13073 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13075 if (strict_overflow_p)
13076 fold_overflow_warning (("assuming signed overflow does not occur "
13077 "when simplifying comparison of "
13078 "absolute value and zero"),
13079 WARN_STRICT_OVERFLOW_CONDITIONAL);
13080 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13083 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13084 and similarly for >= into !=. */
13085 if ((code == LT_EXPR || code == GE_EXPR)
13086 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13087 && TREE_CODE (arg1) == LSHIFT_EXPR
13088 && integer_onep (TREE_OPERAND (arg1, 0)))
13089 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13090 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13091 TREE_OPERAND (arg1, 1)),
13092 build_int_cst (TREE_TYPE (arg0), 0));
13094 if ((code == LT_EXPR || code == GE_EXPR)
13095 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13096 && CONVERT_EXPR_P (arg1)
13097 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13098 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13100 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13101 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13102 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13103 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13104 build_int_cst (TREE_TYPE (arg0), 0));
13109 case UNORDERED_EXPR:
13117 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13119 t1 = fold_relational_const (code, type, arg0, arg1);
13120 if (t1 != NULL_TREE)
13124 /* If the first operand is NaN, the result is constant. */
13125 if (TREE_CODE (arg0) == REAL_CST
13126 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13127 && (code != LTGT_EXPR || ! flag_trapping_math))
13129 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13130 ? integer_zero_node
13131 : integer_one_node;
13132 return omit_one_operand_loc (loc, type, t1, arg1);
13135 /* If the second operand is NaN, the result is constant. */
13136 if (TREE_CODE (arg1) == REAL_CST
13137 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13138 && (code != LTGT_EXPR || ! flag_trapping_math))
13140 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13141 ? integer_zero_node
13142 : integer_one_node;
13143 return omit_one_operand_loc (loc, type, t1, arg0);
13146 /* Simplify unordered comparison of something with itself. */
13147 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13148 && operand_equal_p (arg0, arg1, 0))
13149 return constant_boolean_node (1, type);
13151 if (code == LTGT_EXPR
13152 && !flag_trapping_math
13153 && operand_equal_p (arg0, arg1, 0))
13154 return constant_boolean_node (0, type);
13156 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13158 tree targ0 = strip_float_extensions (arg0);
13159 tree targ1 = strip_float_extensions (arg1);
13160 tree newtype = TREE_TYPE (targ0);
13162 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13163 newtype = TREE_TYPE (targ1);
13165 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13166 return fold_build2_loc (loc, code, type,
13167 fold_convert_loc (loc, newtype, targ0),
13168 fold_convert_loc (loc, newtype, targ1));
13173 case COMPOUND_EXPR:
13174 /* When pedantic, a compound expression can be neither an lvalue
13175 nor an integer constant expression. */
13176 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13178 /* Don't let (0, 0) be null pointer constant. */
13179 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13180 : fold_convert_loc (loc, type, arg1);
13181 return pedantic_non_lvalue_loc (loc, tem);
13184 if ((TREE_CODE (arg0) == REAL_CST
13185 && TREE_CODE (arg1) == REAL_CST)
13186 || (TREE_CODE (arg0) == INTEGER_CST
13187 && TREE_CODE (arg1) == INTEGER_CST))
13188 return build_complex (type, arg0, arg1);
13189 if (TREE_CODE (arg0) == REALPART_EXPR
13190 && TREE_CODE (arg1) == IMAGPART_EXPR
13191 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 0)))
13192 == TYPE_MAIN_VARIANT (type))
13193 && operand_equal_p (TREE_OPERAND (arg0, 0),
13194 TREE_OPERAND (arg1, 0), 0))
13195 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13196 TREE_OPERAND (arg1, 0));
13200 /* An ASSERT_EXPR should never be passed to fold_binary. */
13201 gcc_unreachable ();
13205 } /* switch (code) */
13208 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13209 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13213 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13215 switch (TREE_CODE (*tp))
13221 *walk_subtrees = 0;
13223 /* ... fall through ... */
13230 /* Return whether the sub-tree ST contains a label which is accessible from
13231 outside the sub-tree. */
13234 contains_label_p (tree st)
13237 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13240 /* Fold a ternary expression of code CODE and type TYPE with operands
13241 OP0, OP1, and OP2. Return the folded expression if folding is
13242 successful. Otherwise, return NULL_TREE. */
13245 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13246 tree op0, tree op1, tree op2)
13249 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13250 enum tree_code_class kind = TREE_CODE_CLASS (code);
13252 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13253 && TREE_CODE_LENGTH (code) == 3);
13255 /* Strip any conversions that don't change the mode. This is safe
13256 for every expression, except for a comparison expression because
13257 its signedness is derived from its operands. So, in the latter
13258 case, only strip conversions that don't change the signedness.
13260 Note that this is done as an internal manipulation within the
13261 constant folder, in order to find the simplest representation of
13262 the arguments so that their form can be studied. In any cases,
13263 the appropriate type conversions should be put back in the tree
13264 that will get out of the constant folder. */
13285 case COMPONENT_REF:
13286 if (TREE_CODE (arg0) == CONSTRUCTOR
13287 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13289 unsigned HOST_WIDE_INT idx;
13291 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13298 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13299 so all simple results must be passed through pedantic_non_lvalue. */
13300 if (TREE_CODE (arg0) == INTEGER_CST)
13302 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13303 tem = integer_zerop (arg0) ? op2 : op1;
13304 /* Only optimize constant conditions when the selected branch
13305 has the same type as the COND_EXPR. This avoids optimizing
13306 away "c ? x : throw", where the throw has a void type.
13307 Avoid throwing away that operand which contains label. */
13308 if ((!TREE_SIDE_EFFECTS (unused_op)
13309 || !contains_label_p (unused_op))
13310 && (! VOID_TYPE_P (TREE_TYPE (tem))
13311 || VOID_TYPE_P (type)))
13312 return pedantic_non_lvalue_loc (loc, tem);
13315 if (operand_equal_p (arg1, op2, 0))
13316 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13318 /* If we have A op B ? A : C, we may be able to convert this to a
13319 simpler expression, depending on the operation and the values
13320 of B and C. Signed zeros prevent all of these transformations,
13321 for reasons given above each one.
13323 Also try swapping the arguments and inverting the conditional. */
13324 if (COMPARISON_CLASS_P (arg0)
13325 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13326 arg1, TREE_OPERAND (arg0, 1))
13327 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13329 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13334 if (COMPARISON_CLASS_P (arg0)
13335 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13337 TREE_OPERAND (arg0, 1))
13338 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13340 location_t loc0 = EXPR_LOCATION (arg0);
13341 if (loc0 == UNKNOWN_LOCATION)
13343 tem = fold_truth_not_expr (loc0, arg0);
13344 if (tem && COMPARISON_CLASS_P (tem))
13346 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13352 /* If the second operand is simpler than the third, swap them
13353 since that produces better jump optimization results. */
13354 if (truth_value_p (TREE_CODE (arg0))
13355 && tree_swap_operands_p (op1, op2, false))
13357 location_t loc0 = EXPR_LOCATION (arg0);
13358 if (loc0 == UNKNOWN_LOCATION)
13360 /* See if this can be inverted. If it can't, possibly because
13361 it was a floating-point inequality comparison, don't do
13363 tem = fold_truth_not_expr (loc0, arg0);
13365 return fold_build3_loc (loc, code, type, tem, op2, op1);
13368 /* Convert A ? 1 : 0 to simply A. */
13369 if (integer_onep (op1)
13370 && integer_zerop (op2)
13371 /* If we try to convert OP0 to our type, the
13372 call to fold will try to move the conversion inside
13373 a COND, which will recurse. In that case, the COND_EXPR
13374 is probably the best choice, so leave it alone. */
13375 && type == TREE_TYPE (arg0))
13376 return pedantic_non_lvalue_loc (loc, arg0);
13378 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13379 over COND_EXPR in cases such as floating point comparisons. */
13380 if (integer_zerop (op1)
13381 && integer_onep (op2)
13382 && truth_value_p (TREE_CODE (arg0)))
13383 return pedantic_non_lvalue_loc (loc,
13384 fold_convert_loc (loc, type,
13385 invert_truthvalue_loc (loc,
13388 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13389 if (TREE_CODE (arg0) == LT_EXPR
13390 && integer_zerop (TREE_OPERAND (arg0, 1))
13391 && integer_zerop (op2)
13392 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13394 /* sign_bit_p only checks ARG1 bits within A's precision.
13395 If <sign bit of A> has wider type than A, bits outside
13396 of A's precision in <sign bit of A> need to be checked.
13397 If they are all 0, this optimization needs to be done
13398 in unsigned A's type, if they are all 1 in signed A's type,
13399 otherwise this can't be done. */
13400 if (TYPE_PRECISION (TREE_TYPE (tem))
13401 < TYPE_PRECISION (TREE_TYPE (arg1))
13402 && TYPE_PRECISION (TREE_TYPE (tem))
13403 < TYPE_PRECISION (type))
13405 unsigned HOST_WIDE_INT mask_lo;
13406 HOST_WIDE_INT mask_hi;
13407 int inner_width, outer_width;
13410 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13411 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13412 if (outer_width > TYPE_PRECISION (type))
13413 outer_width = TYPE_PRECISION (type);
13415 if (outer_width > HOST_BITS_PER_WIDE_INT)
13417 mask_hi = ((unsigned HOST_WIDE_INT) -1
13418 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13424 mask_lo = ((unsigned HOST_WIDE_INT) -1
13425 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13427 if (inner_width > HOST_BITS_PER_WIDE_INT)
13429 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13430 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13434 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13435 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13437 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13438 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13440 tem_type = signed_type_for (TREE_TYPE (tem));
13441 tem = fold_convert_loc (loc, tem_type, tem);
13443 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13444 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13446 tem_type = unsigned_type_for (TREE_TYPE (tem));
13447 tem = fold_convert_loc (loc, tem_type, tem);
13455 fold_convert_loc (loc, type,
13456 fold_build2_loc (loc, BIT_AND_EXPR,
13457 TREE_TYPE (tem), tem,
13458 fold_convert_loc (loc,
13463 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13464 already handled above. */
13465 if (TREE_CODE (arg0) == BIT_AND_EXPR
13466 && integer_onep (TREE_OPERAND (arg0, 1))
13467 && integer_zerop (op2)
13468 && integer_pow2p (arg1))
13470 tree tem = TREE_OPERAND (arg0, 0);
13472 if (TREE_CODE (tem) == RSHIFT_EXPR
13473 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13474 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13475 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13476 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13477 TREE_OPERAND (tem, 0), arg1);
13480 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13481 is probably obsolete because the first operand should be a
13482 truth value (that's why we have the two cases above), but let's
13483 leave it in until we can confirm this for all front-ends. */
13484 if (integer_zerop (op2)
13485 && TREE_CODE (arg0) == NE_EXPR
13486 && integer_zerop (TREE_OPERAND (arg0, 1))
13487 && integer_pow2p (arg1)
13488 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13489 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13490 arg1, OEP_ONLY_CONST))
13491 return pedantic_non_lvalue_loc (loc,
13492 fold_convert_loc (loc, type,
13493 TREE_OPERAND (arg0, 0)));
13495 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13496 if (integer_zerop (op2)
13497 && truth_value_p (TREE_CODE (arg0))
13498 && truth_value_p (TREE_CODE (arg1)))
13499 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13500 fold_convert_loc (loc, type, arg0),
13503 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13504 if (integer_onep (op2)
13505 && truth_value_p (TREE_CODE (arg0))
13506 && truth_value_p (TREE_CODE (arg1)))
13508 location_t loc0 = EXPR_LOCATION (arg0);
13509 if (loc0 == UNKNOWN_LOCATION)
13511 /* Only perform transformation if ARG0 is easily inverted. */
13512 tem = fold_truth_not_expr (loc0, arg0);
13514 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13515 fold_convert_loc (loc, type, tem),
13519 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13520 if (integer_zerop (arg1)
13521 && truth_value_p (TREE_CODE (arg0))
13522 && truth_value_p (TREE_CODE (op2)))
13524 location_t loc0 = EXPR_LOCATION (arg0);
13525 if (loc0 == UNKNOWN_LOCATION)
13527 /* Only perform transformation if ARG0 is easily inverted. */
13528 tem = fold_truth_not_expr (loc0, arg0);
13530 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13531 fold_convert_loc (loc, type, tem),
13535 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13536 if (integer_onep (arg1)
13537 && truth_value_p (TREE_CODE (arg0))
13538 && truth_value_p (TREE_CODE (op2)))
13539 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13540 fold_convert_loc (loc, type, arg0),
13546 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13547 of fold_ternary on them. */
13548 gcc_unreachable ();
13550 case BIT_FIELD_REF:
13551 if ((TREE_CODE (arg0) == VECTOR_CST
13552 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13553 && type == TREE_TYPE (TREE_TYPE (arg0)))
13555 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13556 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13559 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13560 && (idx % width) == 0
13561 && (idx = idx / width)
13562 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13564 tree elements = NULL_TREE;
13566 if (TREE_CODE (arg0) == VECTOR_CST)
13567 elements = TREE_VECTOR_CST_ELTS (arg0);
13570 unsigned HOST_WIDE_INT idx;
13573 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13574 elements = tree_cons (NULL_TREE, value, elements);
13576 while (idx-- > 0 && elements)
13577 elements = TREE_CHAIN (elements);
13579 return TREE_VALUE (elements);
13581 return build_zero_cst (type);
13585 /* A bit-field-ref that referenced the full argument can be stripped. */
13586 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13587 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13588 && integer_zerop (op2))
13589 return fold_convert_loc (loc, type, arg0);
13594 /* For integers we can decompose the FMA if possible. */
13595 if (TREE_CODE (arg0) == INTEGER_CST
13596 && TREE_CODE (arg1) == INTEGER_CST)
13597 return fold_build2_loc (loc, PLUS_EXPR, type,
13598 const_binop (MULT_EXPR, arg0, arg1), arg2);
13599 if (integer_zerop (arg2))
13600 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13602 return fold_fma (loc, type, arg0, arg1, arg2);
13606 } /* switch (code) */
13609 /* Perform constant folding and related simplification of EXPR.
13610 The related simplifications include x*1 => x, x*0 => 0, etc.,
13611 and application of the associative law.
13612 NOP_EXPR conversions may be removed freely (as long as we
13613 are careful not to change the type of the overall expression).
13614 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13615 but we can constant-fold them if they have constant operands. */
13617 #ifdef ENABLE_FOLD_CHECKING
13618 # define fold(x) fold_1 (x)
13619 static tree fold_1 (tree);
13625 const tree t = expr;
13626 enum tree_code code = TREE_CODE (t);
13627 enum tree_code_class kind = TREE_CODE_CLASS (code);
13629 location_t loc = EXPR_LOCATION (expr);
13631 /* Return right away if a constant. */
13632 if (kind == tcc_constant)
13635 /* CALL_EXPR-like objects with variable numbers of operands are
13636 treated specially. */
13637 if (kind == tcc_vl_exp)
13639 if (code == CALL_EXPR)
13641 tem = fold_call_expr (loc, expr, false);
13642 return tem ? tem : expr;
13647 if (IS_EXPR_CODE_CLASS (kind))
13649 tree type = TREE_TYPE (t);
13650 tree op0, op1, op2;
13652 switch (TREE_CODE_LENGTH (code))
13655 op0 = TREE_OPERAND (t, 0);
13656 tem = fold_unary_loc (loc, code, type, op0);
13657 return tem ? tem : expr;
13659 op0 = TREE_OPERAND (t, 0);
13660 op1 = TREE_OPERAND (t, 1);
13661 tem = fold_binary_loc (loc, code, type, op0, op1);
13662 return tem ? tem : expr;
13664 op0 = TREE_OPERAND (t, 0);
13665 op1 = TREE_OPERAND (t, 1);
13666 op2 = TREE_OPERAND (t, 2);
13667 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13668 return tem ? tem : expr;
13678 tree op0 = TREE_OPERAND (t, 0);
13679 tree op1 = TREE_OPERAND (t, 1);
13681 if (TREE_CODE (op1) == INTEGER_CST
13682 && TREE_CODE (op0) == CONSTRUCTOR
13683 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13685 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13686 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13687 unsigned HOST_WIDE_INT begin = 0;
13689 /* Find a matching index by means of a binary search. */
13690 while (begin != end)
13692 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13693 tree index = VEC_index (constructor_elt, elts, middle)->index;
13695 if (TREE_CODE (index) == INTEGER_CST
13696 && tree_int_cst_lt (index, op1))
13697 begin = middle + 1;
13698 else if (TREE_CODE (index) == INTEGER_CST
13699 && tree_int_cst_lt (op1, index))
13701 else if (TREE_CODE (index) == RANGE_EXPR
13702 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13703 begin = middle + 1;
13704 else if (TREE_CODE (index) == RANGE_EXPR
13705 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13708 return VEC_index (constructor_elt, elts, middle)->value;
13716 return fold (DECL_INITIAL (t));
13720 } /* switch (code) */
13723 #ifdef ENABLE_FOLD_CHECKING
13726 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13727 static void fold_check_failed (const_tree, const_tree);
13728 void print_fold_checksum (const_tree);
13730 /* When --enable-checking=fold, compute a digest of expr before
13731 and after actual fold call to see if fold did not accidentally
13732 change original expr. */
13738 struct md5_ctx ctx;
13739 unsigned char checksum_before[16], checksum_after[16];
13742 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13743 md5_init_ctx (&ctx);
13744 fold_checksum_tree (expr, &ctx, ht);
13745 md5_finish_ctx (&ctx, checksum_before);
13748 ret = fold_1 (expr);
13750 md5_init_ctx (&ctx);
13751 fold_checksum_tree (expr, &ctx, ht);
13752 md5_finish_ctx (&ctx, checksum_after);
13755 if (memcmp (checksum_before, checksum_after, 16))
13756 fold_check_failed (expr, ret);
13762 print_fold_checksum (const_tree expr)
13764 struct md5_ctx ctx;
13765 unsigned char checksum[16], cnt;
13768 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13769 md5_init_ctx (&ctx);
13770 fold_checksum_tree (expr, &ctx, ht);
13771 md5_finish_ctx (&ctx, checksum);
13773 for (cnt = 0; cnt < 16; ++cnt)
13774 fprintf (stderr, "%02x", checksum[cnt]);
13775 putc ('\n', stderr);
13779 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13781 internal_error ("fold check: original tree changed by fold");
13785 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13788 enum tree_code code;
13789 union tree_node buf;
13794 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13795 <= sizeof (struct tree_function_decl))
13796 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13799 slot = (void **) htab_find_slot (ht, expr, INSERT);
13802 *slot = CONST_CAST_TREE (expr);
13803 code = TREE_CODE (expr);
13804 if (TREE_CODE_CLASS (code) == tcc_declaration
13805 && DECL_ASSEMBLER_NAME_SET_P (expr))
13807 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13808 memcpy ((char *) &buf, expr, tree_size (expr));
13809 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13810 expr = (tree) &buf;
13812 else if (TREE_CODE_CLASS (code) == tcc_type
13813 && (TYPE_POINTER_TO (expr)
13814 || TYPE_REFERENCE_TO (expr)
13815 || TYPE_CACHED_VALUES_P (expr)
13816 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13817 || TYPE_NEXT_VARIANT (expr)))
13819 /* Allow these fields to be modified. */
13821 memcpy ((char *) &buf, expr, tree_size (expr));
13822 expr = tmp = (tree) &buf;
13823 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13824 TYPE_POINTER_TO (tmp) = NULL;
13825 TYPE_REFERENCE_TO (tmp) = NULL;
13826 TYPE_NEXT_VARIANT (tmp) = NULL;
13827 if (TYPE_CACHED_VALUES_P (tmp))
13829 TYPE_CACHED_VALUES_P (tmp) = 0;
13830 TYPE_CACHED_VALUES (tmp) = NULL;
13833 md5_process_bytes (expr, tree_size (expr), ctx);
13834 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13835 if (TREE_CODE_CLASS (code) != tcc_type
13836 && TREE_CODE_CLASS (code) != tcc_declaration
13837 && code != TREE_LIST
13838 && code != SSA_NAME)
13839 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13840 switch (TREE_CODE_CLASS (code))
13846 md5_process_bytes (TREE_STRING_POINTER (expr),
13847 TREE_STRING_LENGTH (expr), ctx);
13850 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13851 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13854 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13860 case tcc_exceptional:
13864 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13865 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13866 expr = TREE_CHAIN (expr);
13867 goto recursive_label;
13870 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13871 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13877 case tcc_expression:
13878 case tcc_reference:
13879 case tcc_comparison:
13882 case tcc_statement:
13884 len = TREE_OPERAND_LENGTH (expr);
13885 for (i = 0; i < len; ++i)
13886 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13888 case tcc_declaration:
13889 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13890 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13891 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13893 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13894 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13895 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13896 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13897 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13899 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13900 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13902 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13904 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13905 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13906 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13910 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13911 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13912 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13913 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13914 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13915 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13916 if (INTEGRAL_TYPE_P (expr)
13917 || SCALAR_FLOAT_TYPE_P (expr))
13919 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13920 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13922 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13923 if (TREE_CODE (expr) == RECORD_TYPE
13924 || TREE_CODE (expr) == UNION_TYPE
13925 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13926 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13927 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13934 /* Helper function for outputting the checksum of a tree T. When
13935 debugging with gdb, you can "define mynext" to be "next" followed
13936 by "call debug_fold_checksum (op0)", then just trace down till the
13939 DEBUG_FUNCTION void
13940 debug_fold_checksum (const_tree t)
13943 unsigned char checksum[16];
13944 struct md5_ctx ctx;
13945 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13947 md5_init_ctx (&ctx);
13948 fold_checksum_tree (t, &ctx, ht);
13949 md5_finish_ctx (&ctx, checksum);
13952 for (i = 0; i < 16; i++)
13953 fprintf (stderr, "%d ", checksum[i]);
13955 fprintf (stderr, "\n");
13960 /* Fold a unary tree expression with code CODE of type TYPE with an
13961 operand OP0. LOC is the location of the resulting expression.
13962 Return a folded expression if successful. Otherwise, return a tree
13963 expression with code CODE of type TYPE with an operand OP0. */
13966 fold_build1_stat_loc (location_t loc,
13967 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13970 #ifdef ENABLE_FOLD_CHECKING
13971 unsigned char checksum_before[16], checksum_after[16];
13972 struct md5_ctx ctx;
13975 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13976 md5_init_ctx (&ctx);
13977 fold_checksum_tree (op0, &ctx, ht);
13978 md5_finish_ctx (&ctx, checksum_before);
13982 tem = fold_unary_loc (loc, code, type, op0);
13984 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13986 #ifdef ENABLE_FOLD_CHECKING
13987 md5_init_ctx (&ctx);
13988 fold_checksum_tree (op0, &ctx, ht);
13989 md5_finish_ctx (&ctx, checksum_after);
13992 if (memcmp (checksum_before, checksum_after, 16))
13993 fold_check_failed (op0, tem);
13998 /* Fold a binary tree expression with code CODE of type TYPE with
13999 operands OP0 and OP1. LOC is the location of the resulting
14000 expression. Return a folded expression if successful. Otherwise,
14001 return a tree expression with code CODE of type TYPE with operands
14005 fold_build2_stat_loc (location_t loc,
14006 enum tree_code code, tree type, tree op0, tree op1
14010 #ifdef ENABLE_FOLD_CHECKING
14011 unsigned char checksum_before_op0[16],
14012 checksum_before_op1[16],
14013 checksum_after_op0[16],
14014 checksum_after_op1[16];
14015 struct md5_ctx ctx;
14018 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14019 md5_init_ctx (&ctx);
14020 fold_checksum_tree (op0, &ctx, ht);
14021 md5_finish_ctx (&ctx, checksum_before_op0);
14024 md5_init_ctx (&ctx);
14025 fold_checksum_tree (op1, &ctx, ht);
14026 md5_finish_ctx (&ctx, checksum_before_op1);
14030 tem = fold_binary_loc (loc, code, type, op0, op1);
14032 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14034 #ifdef ENABLE_FOLD_CHECKING
14035 md5_init_ctx (&ctx);
14036 fold_checksum_tree (op0, &ctx, ht);
14037 md5_finish_ctx (&ctx, checksum_after_op0);
14040 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14041 fold_check_failed (op0, tem);
14043 md5_init_ctx (&ctx);
14044 fold_checksum_tree (op1, &ctx, ht);
14045 md5_finish_ctx (&ctx, checksum_after_op1);
14048 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14049 fold_check_failed (op1, tem);
14054 /* Fold a ternary tree expression with code CODE of type TYPE with
14055 operands OP0, OP1, and OP2. Return a folded expression if
14056 successful. Otherwise, return a tree expression with code CODE of
14057 type TYPE with operands OP0, OP1, and OP2. */
14060 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14061 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14064 #ifdef ENABLE_FOLD_CHECKING
14065 unsigned char checksum_before_op0[16],
14066 checksum_before_op1[16],
14067 checksum_before_op2[16],
14068 checksum_after_op0[16],
14069 checksum_after_op1[16],
14070 checksum_after_op2[16];
14071 struct md5_ctx ctx;
14074 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14075 md5_init_ctx (&ctx);
14076 fold_checksum_tree (op0, &ctx, ht);
14077 md5_finish_ctx (&ctx, checksum_before_op0);
14080 md5_init_ctx (&ctx);
14081 fold_checksum_tree (op1, &ctx, ht);
14082 md5_finish_ctx (&ctx, checksum_before_op1);
14085 md5_init_ctx (&ctx);
14086 fold_checksum_tree (op2, &ctx, ht);
14087 md5_finish_ctx (&ctx, checksum_before_op2);
14091 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14092 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14094 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14096 #ifdef ENABLE_FOLD_CHECKING
14097 md5_init_ctx (&ctx);
14098 fold_checksum_tree (op0, &ctx, ht);
14099 md5_finish_ctx (&ctx, checksum_after_op0);
14102 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14103 fold_check_failed (op0, tem);
14105 md5_init_ctx (&ctx);
14106 fold_checksum_tree (op1, &ctx, ht);
14107 md5_finish_ctx (&ctx, checksum_after_op1);
14110 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14111 fold_check_failed (op1, tem);
14113 md5_init_ctx (&ctx);
14114 fold_checksum_tree (op2, &ctx, ht);
14115 md5_finish_ctx (&ctx, checksum_after_op2);
14118 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14119 fold_check_failed (op2, tem);
14124 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14125 arguments in ARGARRAY, and a null static chain.
14126 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14127 of type TYPE from the given operands as constructed by build_call_array. */
14130 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14131 int nargs, tree *argarray)
14134 #ifdef ENABLE_FOLD_CHECKING
14135 unsigned char checksum_before_fn[16],
14136 checksum_before_arglist[16],
14137 checksum_after_fn[16],
14138 checksum_after_arglist[16];
14139 struct md5_ctx ctx;
14143 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14144 md5_init_ctx (&ctx);
14145 fold_checksum_tree (fn, &ctx, ht);
14146 md5_finish_ctx (&ctx, checksum_before_fn);
14149 md5_init_ctx (&ctx);
14150 for (i = 0; i < nargs; i++)
14151 fold_checksum_tree (argarray[i], &ctx, ht);
14152 md5_finish_ctx (&ctx, checksum_before_arglist);
14156 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14158 #ifdef ENABLE_FOLD_CHECKING
14159 md5_init_ctx (&ctx);
14160 fold_checksum_tree (fn, &ctx, ht);
14161 md5_finish_ctx (&ctx, checksum_after_fn);
14164 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14165 fold_check_failed (fn, tem);
14167 md5_init_ctx (&ctx);
14168 for (i = 0; i < nargs; i++)
14169 fold_checksum_tree (argarray[i], &ctx, ht);
14170 md5_finish_ctx (&ctx, checksum_after_arglist);
14173 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14174 fold_check_failed (NULL_TREE, tem);
14179 /* Perform constant folding and related simplification of initializer
14180 expression EXPR. These behave identically to "fold_buildN" but ignore
14181 potential run-time traps and exceptions that fold must preserve. */
14183 #define START_FOLD_INIT \
14184 int saved_signaling_nans = flag_signaling_nans;\
14185 int saved_trapping_math = flag_trapping_math;\
14186 int saved_rounding_math = flag_rounding_math;\
14187 int saved_trapv = flag_trapv;\
14188 int saved_folding_initializer = folding_initializer;\
14189 flag_signaling_nans = 0;\
14190 flag_trapping_math = 0;\
14191 flag_rounding_math = 0;\
14193 folding_initializer = 1;
14195 #define END_FOLD_INIT \
14196 flag_signaling_nans = saved_signaling_nans;\
14197 flag_trapping_math = saved_trapping_math;\
14198 flag_rounding_math = saved_rounding_math;\
14199 flag_trapv = saved_trapv;\
14200 folding_initializer = saved_folding_initializer;
14203 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14204 tree type, tree op)
14209 result = fold_build1_loc (loc, code, type, op);
14216 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14217 tree type, tree op0, tree op1)
14222 result = fold_build2_loc (loc, code, type, op0, op1);
14229 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14230 tree type, tree op0, tree op1, tree op2)
14235 result = fold_build3_loc (loc, code, type, op0, op1, op2);
14242 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14243 int nargs, tree *argarray)
14248 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14254 #undef START_FOLD_INIT
14255 #undef END_FOLD_INIT
14257 /* Determine if first argument is a multiple of second argument. Return 0 if
14258 it is not, or we cannot easily determined it to be.
14260 An example of the sort of thing we care about (at this point; this routine
14261 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14262 fold cases do now) is discovering that
14264 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14270 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14272 This code also handles discovering that
14274 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14276 is a multiple of 8 so we don't have to worry about dealing with a
14277 possible remainder.
14279 Note that we *look* inside a SAVE_EXPR only to determine how it was
14280 calculated; it is not safe for fold to do much of anything else with the
14281 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14282 at run time. For example, the latter example above *cannot* be implemented
14283 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14284 evaluation time of the original SAVE_EXPR is not necessarily the same at
14285 the time the new expression is evaluated. The only optimization of this
14286 sort that would be valid is changing
14288 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14292 SAVE_EXPR (I) * SAVE_EXPR (J)
14294 (where the same SAVE_EXPR (J) is used in the original and the
14295 transformed version). */
14298 multiple_of_p (tree type, const_tree top, const_tree bottom)
14300 if (operand_equal_p (top, bottom, 0))
14303 if (TREE_CODE (type) != INTEGER_TYPE)
14306 switch (TREE_CODE (top))
14309 /* Bitwise and provides a power of two multiple. If the mask is
14310 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14311 if (!integer_pow2p (bottom))
14316 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14317 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14321 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14322 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14325 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14329 op1 = TREE_OPERAND (top, 1);
14330 /* const_binop may not detect overflow correctly,
14331 so check for it explicitly here. */
14332 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14333 > TREE_INT_CST_LOW (op1)
14334 && TREE_INT_CST_HIGH (op1) == 0
14335 && 0 != (t1 = fold_convert (type,
14336 const_binop (LSHIFT_EXPR,
14339 && !TREE_OVERFLOW (t1))
14340 return multiple_of_p (type, t1, bottom);
14345 /* Can't handle conversions from non-integral or wider integral type. */
14346 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14347 || (TYPE_PRECISION (type)
14348 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14351 /* .. fall through ... */
14354 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14357 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14358 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14361 if (TREE_CODE (bottom) != INTEGER_CST
14362 || integer_zerop (bottom)
14363 || (TYPE_UNSIGNED (type)
14364 && (tree_int_cst_sgn (top) < 0
14365 || tree_int_cst_sgn (bottom) < 0)))
14367 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14375 /* Return true if CODE or TYPE is known to be non-negative. */
14378 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14380 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14381 && truth_value_p (code))
14382 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14383 have a signed:1 type (where the value is -1 and 0). */
14388 /* Return true if (CODE OP0) is known to be non-negative. If the return
14389 value is based on the assumption that signed overflow is undefined,
14390 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14391 *STRICT_OVERFLOW_P. */
14394 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14395 bool *strict_overflow_p)
14397 if (TYPE_UNSIGNED (type))
14403 /* We can't return 1 if flag_wrapv is set because
14404 ABS_EXPR<INT_MIN> = INT_MIN. */
14405 if (!INTEGRAL_TYPE_P (type))
14407 if (TYPE_OVERFLOW_UNDEFINED (type))
14409 *strict_overflow_p = true;
14414 case NON_LVALUE_EXPR:
14416 case FIX_TRUNC_EXPR:
14417 return tree_expr_nonnegative_warnv_p (op0,
14418 strict_overflow_p);
14422 tree inner_type = TREE_TYPE (op0);
14423 tree outer_type = type;
14425 if (TREE_CODE (outer_type) == REAL_TYPE)
14427 if (TREE_CODE (inner_type) == REAL_TYPE)
14428 return tree_expr_nonnegative_warnv_p (op0,
14429 strict_overflow_p);
14430 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14432 if (TYPE_UNSIGNED (inner_type))
14434 return tree_expr_nonnegative_warnv_p (op0,
14435 strict_overflow_p);
14438 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14440 if (TREE_CODE (inner_type) == REAL_TYPE)
14441 return tree_expr_nonnegative_warnv_p (op0,
14442 strict_overflow_p);
14443 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14444 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14445 && TYPE_UNSIGNED (inner_type);
14451 return tree_simple_nonnegative_warnv_p (code, type);
14454 /* We don't know sign of `t', so be conservative and return false. */
14458 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14459 value is based on the assumption that signed overflow is undefined,
14460 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14461 *STRICT_OVERFLOW_P. */
14464 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14465 tree op1, bool *strict_overflow_p)
14467 if (TYPE_UNSIGNED (type))
14472 case POINTER_PLUS_EXPR:
14474 if (FLOAT_TYPE_P (type))
14475 return (tree_expr_nonnegative_warnv_p (op0,
14477 && tree_expr_nonnegative_warnv_p (op1,
14478 strict_overflow_p));
14480 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14481 both unsigned and at least 2 bits shorter than the result. */
14482 if (TREE_CODE (type) == INTEGER_TYPE
14483 && TREE_CODE (op0) == NOP_EXPR
14484 && TREE_CODE (op1) == NOP_EXPR)
14486 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14487 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14488 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14489 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14491 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14492 TYPE_PRECISION (inner2)) + 1;
14493 return prec < TYPE_PRECISION (type);
14499 if (FLOAT_TYPE_P (type))
14501 /* x * x for floating point x is always non-negative. */
14502 if (operand_equal_p (op0, op1, 0))
14504 return (tree_expr_nonnegative_warnv_p (op0,
14506 && tree_expr_nonnegative_warnv_p (op1,
14507 strict_overflow_p));
14510 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14511 both unsigned and their total bits is shorter than the result. */
14512 if (TREE_CODE (type) == INTEGER_TYPE
14513 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14514 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14516 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14517 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14519 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14520 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14523 bool unsigned0 = TYPE_UNSIGNED (inner0);
14524 bool unsigned1 = TYPE_UNSIGNED (inner1);
14526 if (TREE_CODE (op0) == INTEGER_CST)
14527 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14529 if (TREE_CODE (op1) == INTEGER_CST)
14530 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14532 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14533 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14535 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14536 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14537 : TYPE_PRECISION (inner0);
14539 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14540 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14541 : TYPE_PRECISION (inner1);
14543 return precision0 + precision1 < TYPE_PRECISION (type);
14550 return (tree_expr_nonnegative_warnv_p (op0,
14552 || tree_expr_nonnegative_warnv_p (op1,
14553 strict_overflow_p));
14559 case TRUNC_DIV_EXPR:
14560 case CEIL_DIV_EXPR:
14561 case FLOOR_DIV_EXPR:
14562 case ROUND_DIV_EXPR:
14563 return (tree_expr_nonnegative_warnv_p (op0,
14565 && tree_expr_nonnegative_warnv_p (op1,
14566 strict_overflow_p));
14568 case TRUNC_MOD_EXPR:
14569 case CEIL_MOD_EXPR:
14570 case FLOOR_MOD_EXPR:
14571 case ROUND_MOD_EXPR:
14572 return tree_expr_nonnegative_warnv_p (op0,
14573 strict_overflow_p);
14575 return tree_simple_nonnegative_warnv_p (code, type);
14578 /* We don't know sign of `t', so be conservative and return false. */
14582 /* Return true if T is known to be non-negative. If the return
14583 value is based on the assumption that signed overflow is undefined,
14584 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14585 *STRICT_OVERFLOW_P. */
14588 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14590 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14593 switch (TREE_CODE (t))
14596 return tree_int_cst_sgn (t) >= 0;
14599 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14602 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14605 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14607 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14608 strict_overflow_p));
14610 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14613 /* We don't know sign of `t', so be conservative and return false. */
14617 /* Return true if T is known to be non-negative. If the return
14618 value is based on the assumption that signed overflow is undefined,
14619 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14620 *STRICT_OVERFLOW_P. */
14623 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14624 tree arg0, tree arg1, bool *strict_overflow_p)
14626 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14627 switch (DECL_FUNCTION_CODE (fndecl))
14629 CASE_FLT_FN (BUILT_IN_ACOS):
14630 CASE_FLT_FN (BUILT_IN_ACOSH):
14631 CASE_FLT_FN (BUILT_IN_CABS):
14632 CASE_FLT_FN (BUILT_IN_COSH):
14633 CASE_FLT_FN (BUILT_IN_ERFC):
14634 CASE_FLT_FN (BUILT_IN_EXP):
14635 CASE_FLT_FN (BUILT_IN_EXP10):
14636 CASE_FLT_FN (BUILT_IN_EXP2):
14637 CASE_FLT_FN (BUILT_IN_FABS):
14638 CASE_FLT_FN (BUILT_IN_FDIM):
14639 CASE_FLT_FN (BUILT_IN_HYPOT):
14640 CASE_FLT_FN (BUILT_IN_POW10):
14641 CASE_INT_FN (BUILT_IN_FFS):
14642 CASE_INT_FN (BUILT_IN_PARITY):
14643 CASE_INT_FN (BUILT_IN_POPCOUNT):
14644 case BUILT_IN_BSWAP32:
14645 case BUILT_IN_BSWAP64:
14649 CASE_FLT_FN (BUILT_IN_SQRT):
14650 /* sqrt(-0.0) is -0.0. */
14651 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14653 return tree_expr_nonnegative_warnv_p (arg0,
14654 strict_overflow_p);
14656 CASE_FLT_FN (BUILT_IN_ASINH):
14657 CASE_FLT_FN (BUILT_IN_ATAN):
14658 CASE_FLT_FN (BUILT_IN_ATANH):
14659 CASE_FLT_FN (BUILT_IN_CBRT):
14660 CASE_FLT_FN (BUILT_IN_CEIL):
14661 CASE_FLT_FN (BUILT_IN_ERF):
14662 CASE_FLT_FN (BUILT_IN_EXPM1):
14663 CASE_FLT_FN (BUILT_IN_FLOOR):
14664 CASE_FLT_FN (BUILT_IN_FMOD):
14665 CASE_FLT_FN (BUILT_IN_FREXP):
14666 CASE_FLT_FN (BUILT_IN_LCEIL):
14667 CASE_FLT_FN (BUILT_IN_LDEXP):
14668 CASE_FLT_FN (BUILT_IN_LFLOOR):
14669 CASE_FLT_FN (BUILT_IN_LLCEIL):
14670 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14671 CASE_FLT_FN (BUILT_IN_LLRINT):
14672 CASE_FLT_FN (BUILT_IN_LLROUND):
14673 CASE_FLT_FN (BUILT_IN_LRINT):
14674 CASE_FLT_FN (BUILT_IN_LROUND):
14675 CASE_FLT_FN (BUILT_IN_MODF):
14676 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14677 CASE_FLT_FN (BUILT_IN_RINT):
14678 CASE_FLT_FN (BUILT_IN_ROUND):
14679 CASE_FLT_FN (BUILT_IN_SCALB):
14680 CASE_FLT_FN (BUILT_IN_SCALBLN):
14681 CASE_FLT_FN (BUILT_IN_SCALBN):
14682 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14683 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14684 CASE_FLT_FN (BUILT_IN_SINH):
14685 CASE_FLT_FN (BUILT_IN_TANH):
14686 CASE_FLT_FN (BUILT_IN_TRUNC):
14687 /* True if the 1st argument is nonnegative. */
14688 return tree_expr_nonnegative_warnv_p (arg0,
14689 strict_overflow_p);
14691 CASE_FLT_FN (BUILT_IN_FMAX):
14692 /* True if the 1st OR 2nd arguments are nonnegative. */
14693 return (tree_expr_nonnegative_warnv_p (arg0,
14695 || (tree_expr_nonnegative_warnv_p (arg1,
14696 strict_overflow_p)));
14698 CASE_FLT_FN (BUILT_IN_FMIN):
14699 /* True if the 1st AND 2nd arguments are nonnegative. */
14700 return (tree_expr_nonnegative_warnv_p (arg0,
14702 && (tree_expr_nonnegative_warnv_p (arg1,
14703 strict_overflow_p)));
14705 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14706 /* True if the 2nd argument is nonnegative. */
14707 return tree_expr_nonnegative_warnv_p (arg1,
14708 strict_overflow_p);
14710 CASE_FLT_FN (BUILT_IN_POWI):
14711 /* True if the 1st argument is nonnegative or the second
14712 argument is an even integer. */
14713 if (TREE_CODE (arg1) == INTEGER_CST
14714 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14716 return tree_expr_nonnegative_warnv_p (arg0,
14717 strict_overflow_p);
14719 CASE_FLT_FN (BUILT_IN_POW):
14720 /* True if the 1st argument is nonnegative or the second
14721 argument is an even integer valued real. */
14722 if (TREE_CODE (arg1) == REAL_CST)
14727 c = TREE_REAL_CST (arg1);
14728 n = real_to_integer (&c);
14731 REAL_VALUE_TYPE cint;
14732 real_from_integer (&cint, VOIDmode, n,
14733 n < 0 ? -1 : 0, 0);
14734 if (real_identical (&c, &cint))
14738 return tree_expr_nonnegative_warnv_p (arg0,
14739 strict_overflow_p);
14744 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14748 /* Return true if T is known to be non-negative. If the return
14749 value is based on the assumption that signed overflow is undefined,
14750 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14751 *STRICT_OVERFLOW_P. */
14754 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14756 enum tree_code code = TREE_CODE (t);
14757 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14764 tree temp = TARGET_EXPR_SLOT (t);
14765 t = TARGET_EXPR_INITIAL (t);
14767 /* If the initializer is non-void, then it's a normal expression
14768 that will be assigned to the slot. */
14769 if (!VOID_TYPE_P (t))
14770 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14772 /* Otherwise, the initializer sets the slot in some way. One common
14773 way is an assignment statement at the end of the initializer. */
14776 if (TREE_CODE (t) == BIND_EXPR)
14777 t = expr_last (BIND_EXPR_BODY (t));
14778 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14779 || TREE_CODE (t) == TRY_CATCH_EXPR)
14780 t = expr_last (TREE_OPERAND (t, 0));
14781 else if (TREE_CODE (t) == STATEMENT_LIST)
14786 if (TREE_CODE (t) == MODIFY_EXPR
14787 && TREE_OPERAND (t, 0) == temp)
14788 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14789 strict_overflow_p);
14796 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14797 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14799 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14800 get_callee_fndecl (t),
14803 strict_overflow_p);
14805 case COMPOUND_EXPR:
14807 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14808 strict_overflow_p);
14810 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14811 strict_overflow_p);
14813 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14814 strict_overflow_p);
14817 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14821 /* We don't know sign of `t', so be conservative and return false. */
14825 /* Return true if T is known to be non-negative. If the return
14826 value is based on the assumption that signed overflow is undefined,
14827 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14828 *STRICT_OVERFLOW_P. */
14831 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14833 enum tree_code code;
14834 if (t == error_mark_node)
14837 code = TREE_CODE (t);
14838 switch (TREE_CODE_CLASS (code))
14841 case tcc_comparison:
14842 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14844 TREE_OPERAND (t, 0),
14845 TREE_OPERAND (t, 1),
14846 strict_overflow_p);
14849 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14851 TREE_OPERAND (t, 0),
14852 strict_overflow_p);
14855 case tcc_declaration:
14856 case tcc_reference:
14857 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14865 case TRUTH_AND_EXPR:
14866 case TRUTH_OR_EXPR:
14867 case TRUTH_XOR_EXPR:
14868 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14870 TREE_OPERAND (t, 0),
14871 TREE_OPERAND (t, 1),
14872 strict_overflow_p);
14873 case TRUTH_NOT_EXPR:
14874 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14876 TREE_OPERAND (t, 0),
14877 strict_overflow_p);
14884 case WITH_SIZE_EXPR:
14886 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14889 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14893 /* Return true if `t' is known to be non-negative. Handle warnings
14894 about undefined signed overflow. */
14897 tree_expr_nonnegative_p (tree t)
14899 bool ret, strict_overflow_p;
14901 strict_overflow_p = false;
14902 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14903 if (strict_overflow_p)
14904 fold_overflow_warning (("assuming signed overflow does not occur when "
14905 "determining that expression is always "
14907 WARN_STRICT_OVERFLOW_MISC);
14912 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14913 For floating point we further ensure that T is not denormal.
14914 Similar logic is present in nonzero_address in rtlanal.h.
14916 If the return value is based on the assumption that signed overflow
14917 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14918 change *STRICT_OVERFLOW_P. */
14921 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14922 bool *strict_overflow_p)
14927 return tree_expr_nonzero_warnv_p (op0,
14928 strict_overflow_p);
14932 tree inner_type = TREE_TYPE (op0);
14933 tree outer_type = type;
14935 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14936 && tree_expr_nonzero_warnv_p (op0,
14937 strict_overflow_p));
14941 case NON_LVALUE_EXPR:
14942 return tree_expr_nonzero_warnv_p (op0,
14943 strict_overflow_p);
14952 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14953 For floating point we further ensure that T is not denormal.
14954 Similar logic is present in nonzero_address in rtlanal.h.
14956 If the return value is based on the assumption that signed overflow
14957 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14958 change *STRICT_OVERFLOW_P. */
14961 tree_binary_nonzero_warnv_p (enum tree_code code,
14964 tree op1, bool *strict_overflow_p)
14966 bool sub_strict_overflow_p;
14969 case POINTER_PLUS_EXPR:
14971 if (TYPE_OVERFLOW_UNDEFINED (type))
14973 /* With the presence of negative values it is hard
14974 to say something. */
14975 sub_strict_overflow_p = false;
14976 if (!tree_expr_nonnegative_warnv_p (op0,
14977 &sub_strict_overflow_p)
14978 || !tree_expr_nonnegative_warnv_p (op1,
14979 &sub_strict_overflow_p))
14981 /* One of operands must be positive and the other non-negative. */
14982 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14983 overflows, on a twos-complement machine the sum of two
14984 nonnegative numbers can never be zero. */
14985 return (tree_expr_nonzero_warnv_p (op0,
14987 || tree_expr_nonzero_warnv_p (op1,
14988 strict_overflow_p));
14993 if (TYPE_OVERFLOW_UNDEFINED (type))
14995 if (tree_expr_nonzero_warnv_p (op0,
14997 && tree_expr_nonzero_warnv_p (op1,
14998 strict_overflow_p))
15000 *strict_overflow_p = true;
15007 sub_strict_overflow_p = false;
15008 if (tree_expr_nonzero_warnv_p (op0,
15009 &sub_strict_overflow_p)
15010 && tree_expr_nonzero_warnv_p (op1,
15011 &sub_strict_overflow_p))
15013 if (sub_strict_overflow_p)
15014 *strict_overflow_p = true;
15019 sub_strict_overflow_p = false;
15020 if (tree_expr_nonzero_warnv_p (op0,
15021 &sub_strict_overflow_p))
15023 if (sub_strict_overflow_p)
15024 *strict_overflow_p = true;
15026 /* When both operands are nonzero, then MAX must be too. */
15027 if (tree_expr_nonzero_warnv_p (op1,
15028 strict_overflow_p))
15031 /* MAX where operand 0 is positive is positive. */
15032 return tree_expr_nonnegative_warnv_p (op0,
15033 strict_overflow_p);
15035 /* MAX where operand 1 is positive is positive. */
15036 else if (tree_expr_nonzero_warnv_p (op1,
15037 &sub_strict_overflow_p)
15038 && tree_expr_nonnegative_warnv_p (op1,
15039 &sub_strict_overflow_p))
15041 if (sub_strict_overflow_p)
15042 *strict_overflow_p = true;
15048 return (tree_expr_nonzero_warnv_p (op1,
15050 || tree_expr_nonzero_warnv_p (op0,
15051 strict_overflow_p));
15060 /* Return true when T is an address and is known to be nonzero.
15061 For floating point we further ensure that T is not denormal.
15062 Similar logic is present in nonzero_address in rtlanal.h.
15064 If the return value is based on the assumption that signed overflow
15065 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15066 change *STRICT_OVERFLOW_P. */
15069 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15071 bool sub_strict_overflow_p;
15072 switch (TREE_CODE (t))
15075 return !integer_zerop (t);
15079 tree base = TREE_OPERAND (t, 0);
15080 if (!DECL_P (base))
15081 base = get_base_address (base);
15086 /* Weak declarations may link to NULL. Other things may also be NULL
15087 so protect with -fdelete-null-pointer-checks; but not variables
15088 allocated on the stack. */
15090 && (flag_delete_null_pointer_checks
15091 || (DECL_CONTEXT (base)
15092 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15093 && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15094 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15096 /* Constants are never weak. */
15097 if (CONSTANT_CLASS_P (base))
15104 sub_strict_overflow_p = false;
15105 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15106 &sub_strict_overflow_p)
15107 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15108 &sub_strict_overflow_p))
15110 if (sub_strict_overflow_p)
15111 *strict_overflow_p = true;
15122 /* Return true when T is an address and is known to be nonzero.
15123 For floating point we further ensure that T is not denormal.
15124 Similar logic is present in nonzero_address in rtlanal.h.
15126 If the return value is based on the assumption that signed overflow
15127 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15128 change *STRICT_OVERFLOW_P. */
15131 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15133 tree type = TREE_TYPE (t);
15134 enum tree_code code;
15136 /* Doing something useful for floating point would need more work. */
15137 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15140 code = TREE_CODE (t);
15141 switch (TREE_CODE_CLASS (code))
15144 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15145 strict_overflow_p);
15147 case tcc_comparison:
15148 return tree_binary_nonzero_warnv_p (code, type,
15149 TREE_OPERAND (t, 0),
15150 TREE_OPERAND (t, 1),
15151 strict_overflow_p);
15153 case tcc_declaration:
15154 case tcc_reference:
15155 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15163 case TRUTH_NOT_EXPR:
15164 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15165 strict_overflow_p);
15167 case TRUTH_AND_EXPR:
15168 case TRUTH_OR_EXPR:
15169 case TRUTH_XOR_EXPR:
15170 return tree_binary_nonzero_warnv_p (code, type,
15171 TREE_OPERAND (t, 0),
15172 TREE_OPERAND (t, 1),
15173 strict_overflow_p);
15180 case WITH_SIZE_EXPR:
15182 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15184 case COMPOUND_EXPR:
15187 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15188 strict_overflow_p);
15191 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15192 strict_overflow_p);
15195 return alloca_call_p (t);
15203 /* Return true when T is an address and is known to be nonzero.
15204 Handle warnings about undefined signed overflow. */
15207 tree_expr_nonzero_p (tree t)
15209 bool ret, strict_overflow_p;
15211 strict_overflow_p = false;
15212 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15213 if (strict_overflow_p)
15214 fold_overflow_warning (("assuming signed overflow does not occur when "
15215 "determining that expression is always "
15217 WARN_STRICT_OVERFLOW_MISC);
15221 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15222 attempt to fold the expression to a constant without modifying TYPE,
15225 If the expression could be simplified to a constant, then return
15226 the constant. If the expression would not be simplified to a
15227 constant, then return NULL_TREE. */
15230 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15232 tree tem = fold_binary (code, type, op0, op1);
15233 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15236 /* Given the components of a unary expression CODE, TYPE and OP0,
15237 attempt to fold the expression to a constant without modifying
15240 If the expression could be simplified to a constant, then return
15241 the constant. If the expression would not be simplified to a
15242 constant, then return NULL_TREE. */
15245 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15247 tree tem = fold_unary (code, type, op0);
15248 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15251 /* If EXP represents referencing an element in a constant string
15252 (either via pointer arithmetic or array indexing), return the
15253 tree representing the value accessed, otherwise return NULL. */
15256 fold_read_from_constant_string (tree exp)
15258 if ((TREE_CODE (exp) == INDIRECT_REF
15259 || TREE_CODE (exp) == ARRAY_REF)
15260 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15262 tree exp1 = TREE_OPERAND (exp, 0);
15265 location_t loc = EXPR_LOCATION (exp);
15267 if (TREE_CODE (exp) == INDIRECT_REF)
15268 string = string_constant (exp1, &index);
15271 tree low_bound = array_ref_low_bound (exp);
15272 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15274 /* Optimize the special-case of a zero lower bound.
15276 We convert the low_bound to sizetype to avoid some problems
15277 with constant folding. (E.g. suppose the lower bound is 1,
15278 and its mode is QI. Without the conversion,l (ARRAY
15279 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15280 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15281 if (! integer_zerop (low_bound))
15282 index = size_diffop_loc (loc, index,
15283 fold_convert_loc (loc, sizetype, low_bound));
15289 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15290 && TREE_CODE (string) == STRING_CST
15291 && TREE_CODE (index) == INTEGER_CST
15292 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15293 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15295 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15296 return build_int_cst_type (TREE_TYPE (exp),
15297 (TREE_STRING_POINTER (string)
15298 [TREE_INT_CST_LOW (index)]));
15303 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15304 an integer constant, real, or fixed-point constant.
15306 TYPE is the type of the result. */
15309 fold_negate_const (tree arg0, tree type)
15311 tree t = NULL_TREE;
15313 switch (TREE_CODE (arg0))
15317 double_int val = tree_to_double_int (arg0);
15318 int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15320 t = force_fit_type_double (type, val, 1,
15321 (overflow | TREE_OVERFLOW (arg0))
15322 && !TYPE_UNSIGNED (type));
15327 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15332 FIXED_VALUE_TYPE f;
15333 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15334 &(TREE_FIXED_CST (arg0)), NULL,
15335 TYPE_SATURATING (type));
15336 t = build_fixed (type, f);
15337 /* Propagate overflow flags. */
15338 if (overflow_p | TREE_OVERFLOW (arg0))
15339 TREE_OVERFLOW (t) = 1;
15344 gcc_unreachable ();
15350 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15351 an integer constant or real constant.
15353 TYPE is the type of the result. */
15356 fold_abs_const (tree arg0, tree type)
15358 tree t = NULL_TREE;
15360 switch (TREE_CODE (arg0))
15364 double_int val = tree_to_double_int (arg0);
15366 /* If the value is unsigned or non-negative, then the absolute value
15367 is the same as the ordinary value. */
15368 if (TYPE_UNSIGNED (type)
15369 || !double_int_negative_p (val))
15372 /* If the value is negative, then the absolute value is
15378 overflow = neg_double (val.low, val.high, &val.low, &val.high);
15379 t = force_fit_type_double (type, val, -1,
15380 overflow | TREE_OVERFLOW (arg0));
15386 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15387 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15393 gcc_unreachable ();
15399 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15400 constant. TYPE is the type of the result. */
15403 fold_not_const (const_tree arg0, tree type)
15407 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15409 val = double_int_not (tree_to_double_int (arg0));
15410 return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15413 /* Given CODE, a relational operator, the target type, TYPE and two
15414 constant operands OP0 and OP1, return the result of the
15415 relational operation. If the result is not a compile time
15416 constant, then return NULL_TREE. */
15419 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15421 int result, invert;
15423 /* From here on, the only cases we handle are when the result is
15424 known to be a constant. */
15426 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15428 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15429 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15431 /* Handle the cases where either operand is a NaN. */
15432 if (real_isnan (c0) || real_isnan (c1))
15442 case UNORDERED_EXPR:
15456 if (flag_trapping_math)
15462 gcc_unreachable ();
15465 return constant_boolean_node (result, type);
15468 return constant_boolean_node (real_compare (code, c0, c1), type);
15471 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15473 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15474 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15475 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15478 /* Handle equality/inequality of complex constants. */
15479 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15481 tree rcond = fold_relational_const (code, type,
15482 TREE_REALPART (op0),
15483 TREE_REALPART (op1));
15484 tree icond = fold_relational_const (code, type,
15485 TREE_IMAGPART (op0),
15486 TREE_IMAGPART (op1));
15487 if (code == EQ_EXPR)
15488 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15489 else if (code == NE_EXPR)
15490 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15495 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15497 To compute GT, swap the arguments and do LT.
15498 To compute GE, do LT and invert the result.
15499 To compute LE, swap the arguments, do LT and invert the result.
15500 To compute NE, do EQ and invert the result.
15502 Therefore, the code below must handle only EQ and LT. */
15504 if (code == LE_EXPR || code == GT_EXPR)
15509 code = swap_tree_comparison (code);
15512 /* Note that it is safe to invert for real values here because we
15513 have already handled the one case that it matters. */
15516 if (code == NE_EXPR || code == GE_EXPR)
15519 code = invert_tree_comparison (code, false);
15522 /* Compute a result for LT or EQ if args permit;
15523 Otherwise return T. */
15524 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15526 if (code == EQ_EXPR)
15527 result = tree_int_cst_equal (op0, op1);
15528 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15529 result = INT_CST_LT_UNSIGNED (op0, op1);
15531 result = INT_CST_LT (op0, op1);
15538 return constant_boolean_node (result, type);
15541 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15542 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15546 fold_build_cleanup_point_expr (tree type, tree expr)
15548 /* If the expression does not have side effects then we don't have to wrap
15549 it with a cleanup point expression. */
15550 if (!TREE_SIDE_EFFECTS (expr))
15553 /* If the expression is a return, check to see if the expression inside the
15554 return has no side effects or the right hand side of the modify expression
15555 inside the return. If either don't have side effects set we don't need to
15556 wrap the expression in a cleanup point expression. Note we don't check the
15557 left hand side of the modify because it should always be a return decl. */
15558 if (TREE_CODE (expr) == RETURN_EXPR)
15560 tree op = TREE_OPERAND (expr, 0);
15561 if (!op || !TREE_SIDE_EFFECTS (op))
15563 op = TREE_OPERAND (op, 1);
15564 if (!TREE_SIDE_EFFECTS (op))
15568 return build1 (CLEANUP_POINT_EXPR, type, expr);
15571 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15572 of an indirection through OP0, or NULL_TREE if no simplification is
15576 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15582 subtype = TREE_TYPE (sub);
15583 if (!POINTER_TYPE_P (subtype))
15586 if (TREE_CODE (sub) == ADDR_EXPR)
15588 tree op = TREE_OPERAND (sub, 0);
15589 tree optype = TREE_TYPE (op);
15590 /* *&CONST_DECL -> to the value of the const decl. */
15591 if (TREE_CODE (op) == CONST_DECL)
15592 return DECL_INITIAL (op);
15593 /* *&p => p; make sure to handle *&"str"[cst] here. */
15594 if (type == optype)
15596 tree fop = fold_read_from_constant_string (op);
15602 /* *(foo *)&fooarray => fooarray[0] */
15603 else if (TREE_CODE (optype) == ARRAY_TYPE
15604 && type == TREE_TYPE (optype)
15605 && (!in_gimple_form
15606 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15608 tree type_domain = TYPE_DOMAIN (optype);
15609 tree min_val = size_zero_node;
15610 if (type_domain && TYPE_MIN_VALUE (type_domain))
15611 min_val = TYPE_MIN_VALUE (type_domain);
15613 && TREE_CODE (min_val) != INTEGER_CST)
15615 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15616 NULL_TREE, NULL_TREE);
15618 /* *(foo *)&complexfoo => __real__ complexfoo */
15619 else if (TREE_CODE (optype) == COMPLEX_TYPE
15620 && type == TREE_TYPE (optype))
15621 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15622 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15623 else if (TREE_CODE (optype) == VECTOR_TYPE
15624 && type == TREE_TYPE (optype))
15626 tree part_width = TYPE_SIZE (type);
15627 tree index = bitsize_int (0);
15628 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15632 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15633 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15635 tree op00 = TREE_OPERAND (sub, 0);
15636 tree op01 = TREE_OPERAND (sub, 1);
15639 if (TREE_CODE (op00) == ADDR_EXPR)
15642 op00 = TREE_OPERAND (op00, 0);
15643 op00type = TREE_TYPE (op00);
15645 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15646 if (TREE_CODE (op00type) == VECTOR_TYPE
15647 && type == TREE_TYPE (op00type))
15649 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15650 tree part_width = TYPE_SIZE (type);
15651 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15652 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15653 tree index = bitsize_int (indexi);
15655 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15656 return fold_build3_loc (loc,
15657 BIT_FIELD_REF, type, op00,
15658 part_width, index);
15661 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15662 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15663 && type == TREE_TYPE (op00type))
15665 tree size = TYPE_SIZE_UNIT (type);
15666 if (tree_int_cst_equal (size, op01))
15667 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15669 /* ((foo *)&fooarray)[1] => fooarray[1] */
15670 else if (TREE_CODE (op00type) == ARRAY_TYPE
15671 && type == TREE_TYPE (op00type))
15673 tree type_domain = TYPE_DOMAIN (op00type);
15674 tree min_val = size_zero_node;
15675 if (type_domain && TYPE_MIN_VALUE (type_domain))
15676 min_val = TYPE_MIN_VALUE (type_domain);
15677 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15678 TYPE_SIZE_UNIT (type));
15679 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15680 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15681 NULL_TREE, NULL_TREE);
15686 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15687 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15688 && type == TREE_TYPE (TREE_TYPE (subtype))
15689 && (!in_gimple_form
15690 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15693 tree min_val = size_zero_node;
15694 sub = build_fold_indirect_ref_loc (loc, sub);
15695 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15696 if (type_domain && TYPE_MIN_VALUE (type_domain))
15697 min_val = TYPE_MIN_VALUE (type_domain);
15699 && TREE_CODE (min_val) != INTEGER_CST)
15701 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15708 /* Builds an expression for an indirection through T, simplifying some
15712 build_fold_indirect_ref_loc (location_t loc, tree t)
15714 tree type = TREE_TYPE (TREE_TYPE (t));
15715 tree sub = fold_indirect_ref_1 (loc, type, t);
15720 return build1_loc (loc, INDIRECT_REF, type, t);
15723 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15726 fold_indirect_ref_loc (location_t loc, tree t)
15728 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15736 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15737 whose result is ignored. The type of the returned tree need not be
15738 the same as the original expression. */
15741 fold_ignored_result (tree t)
15743 if (!TREE_SIDE_EFFECTS (t))
15744 return integer_zero_node;
15747 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15750 t = TREE_OPERAND (t, 0);
15754 case tcc_comparison:
15755 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15756 t = TREE_OPERAND (t, 0);
15757 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15758 t = TREE_OPERAND (t, 1);
15763 case tcc_expression:
15764 switch (TREE_CODE (t))
15766 case COMPOUND_EXPR:
15767 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15769 t = TREE_OPERAND (t, 0);
15773 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15774 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15776 t = TREE_OPERAND (t, 0);
15789 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15790 This can only be applied to objects of a sizetype. */
15793 round_up_loc (location_t loc, tree value, int divisor)
15795 tree div = NULL_TREE;
15797 gcc_assert (divisor > 0);
15801 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15802 have to do anything. Only do this when we are not given a const,
15803 because in that case, this check is more expensive than just
15805 if (TREE_CODE (value) != INTEGER_CST)
15807 div = build_int_cst (TREE_TYPE (value), divisor);
15809 if (multiple_of_p (TREE_TYPE (value), value, div))
15813 /* If divisor is a power of two, simplify this to bit manipulation. */
15814 if (divisor == (divisor & -divisor))
15816 if (TREE_CODE (value) == INTEGER_CST)
15818 double_int val = tree_to_double_int (value);
15821 if ((val.low & (divisor - 1)) == 0)
15824 overflow_p = TREE_OVERFLOW (value);
15825 val.low &= ~(divisor - 1);
15826 val.low += divisor;
15834 return force_fit_type_double (TREE_TYPE (value), val,
15841 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15842 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15843 t = build_int_cst (TREE_TYPE (value), -divisor);
15844 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15850 div = build_int_cst (TREE_TYPE (value), divisor);
15851 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15852 value = size_binop_loc (loc, MULT_EXPR, value, div);
15858 /* Likewise, but round down. */
15861 round_down_loc (location_t loc, tree value, int divisor)
15863 tree div = NULL_TREE;
15865 gcc_assert (divisor > 0);
15869 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15870 have to do anything. Only do this when we are not given a const,
15871 because in that case, this check is more expensive than just
15873 if (TREE_CODE (value) != INTEGER_CST)
15875 div = build_int_cst (TREE_TYPE (value), divisor);
15877 if (multiple_of_p (TREE_TYPE (value), value, div))
15881 /* If divisor is a power of two, simplify this to bit manipulation. */
15882 if (divisor == (divisor & -divisor))
15886 t = build_int_cst (TREE_TYPE (value), -divisor);
15887 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15892 div = build_int_cst (TREE_TYPE (value), divisor);
15893 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15894 value = size_binop_loc (loc, MULT_EXPR, value, div);
15900 /* Returns the pointer to the base of the object addressed by EXP and
15901 extracts the information about the offset of the access, storing it
15902 to PBITPOS and POFFSET. */
15905 split_address_to_core_and_offset (tree exp,
15906 HOST_WIDE_INT *pbitpos, tree *poffset)
15909 enum machine_mode mode;
15910 int unsignedp, volatilep;
15911 HOST_WIDE_INT bitsize;
15912 location_t loc = EXPR_LOCATION (exp);
15914 if (TREE_CODE (exp) == ADDR_EXPR)
15916 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15917 poffset, &mode, &unsignedp, &volatilep,
15919 core = build_fold_addr_expr_loc (loc, core);
15925 *poffset = NULL_TREE;
15931 /* Returns true if addresses of E1 and E2 differ by a constant, false
15932 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15935 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15938 HOST_WIDE_INT bitpos1, bitpos2;
15939 tree toffset1, toffset2, tdiff, type;
15941 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15942 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15944 if (bitpos1 % BITS_PER_UNIT != 0
15945 || bitpos2 % BITS_PER_UNIT != 0
15946 || !operand_equal_p (core1, core2, 0))
15949 if (toffset1 && toffset2)
15951 type = TREE_TYPE (toffset1);
15952 if (type != TREE_TYPE (toffset2))
15953 toffset2 = fold_convert (type, toffset2);
15955 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15956 if (!cst_and_fits_in_hwi (tdiff))
15959 *diff = int_cst_value (tdiff);
15961 else if (toffset1 || toffset2)
15963 /* If only one of the offsets is non-constant, the difference cannot
15970 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15974 /* Simplify the floating point expression EXP when the sign of the
15975 result is not significant. Return NULL_TREE if no simplification
15979 fold_strip_sign_ops (tree exp)
15982 location_t loc = EXPR_LOCATION (exp);
15984 switch (TREE_CODE (exp))
15988 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15989 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15993 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15995 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15996 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15997 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15998 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15999 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16000 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16003 case COMPOUND_EXPR:
16004 arg0 = TREE_OPERAND (exp, 0);
16005 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16007 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16011 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16012 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16014 return fold_build3_loc (loc,
16015 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16016 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16017 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16022 const enum built_in_function fcode = builtin_mathfn_code (exp);
16025 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16026 /* Strip copysign function call, return the 1st argument. */
16027 arg0 = CALL_EXPR_ARG (exp, 0);
16028 arg1 = CALL_EXPR_ARG (exp, 1);
16029 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16032 /* Strip sign ops from the argument of "odd" math functions. */
16033 if (negate_mathfn_p (fcode))
16035 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16037 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);