1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
45 #include "coretypes.h"
53 #include "fold-const.h"
54 #include "stor-layout.h"
56 #include "tree-iterator.h"
58 #include "insn-config.h"
68 #include "diagnostic-core.h"
70 #include "langhooks.h"
72 #include "internal-fn.h"
78 #include "generic-match.h"
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
85 /* Nonzero if we are folding constants inside an initializer; zero
87 int folding_initializer = 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
111 static bool negate_mathfn_p (enum built_in_function);
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int operand_equal_for_comparison_p (tree, tree, tree);
119 static int twoval_comparison_p (tree, tree *, tree *, int *);
120 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
121 static tree make_bit_field_ref (location_t, tree, tree,
122 HOST_WIDE_INT, HOST_WIDE_INT, int);
123 static tree optimize_bit_field_compare (location_t, enum tree_code,
125 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
127 machine_mode *, int *, int *,
129 static int simple_operand_p (const_tree);
130 static bool simple_operand_p_2 (tree);
131 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
132 static tree range_predecessor (tree);
133 static tree range_successor (tree);
134 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
135 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
136 static tree unextend (tree, int, int, tree);
137 static tree optimize_minmax_comparison (location_t, enum tree_code,
139 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
140 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
141 static tree fold_binary_op_with_conditional_arg (location_t,
142 enum tree_code, tree,
145 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
146 static bool reorder_operands_p (const_tree, const_tree);
147 static tree fold_negate_const (tree, tree);
148 static tree fold_not_const (const_tree, tree);
149 static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 static tree fold_convert_const (enum tree_code, tree, tree);
151 static tree fold_view_convert_expr (tree, tree);
152 static bool vec_cst_ctor_to_array (tree, tree *);
155 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
156 Otherwise, return LOC. */
159 expr_location_or (tree t, location_t loc)
161 location_t tloc = EXPR_LOCATION (t);
162 return tloc == UNKNOWN_LOCATION ? loc : tloc;
165 /* Similar to protected_set_expr_location, but never modify x in place,
166 if location can and needs to be set, unshare it. */
169 protected_set_expr_location_unshare (tree x, location_t loc)
171 if (CAN_HAVE_LOCATION_P (x)
172 && EXPR_LOCATION (x) != loc
173 && !(TREE_CODE (x) == SAVE_EXPR
174 || TREE_CODE (x) == TARGET_EXPR
175 || TREE_CODE (x) == BIND_EXPR))
178 SET_EXPR_LOCATION (x, loc);
183 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
184 division and returns the quotient. Otherwise returns
188 div_if_zero_remainder (const_tree arg1, const_tree arg2)
192 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
194 return wide_int_to_tree (TREE_TYPE (arg1), quo);
199 /* This is nonzero if we should defer warnings about undefined
200 overflow. This facility exists because these warnings are a
201 special case. The code to estimate loop iterations does not want
202 to issue any warnings, since it works with expressions which do not
203 occur in user code. Various bits of cleanup code call fold(), but
204 only use the result if it has certain characteristics (e.g., is a
205 constant); that code only wants to issue a warning if the result is
208 static int fold_deferring_overflow_warnings;
210 /* If a warning about undefined overflow is deferred, this is the
211 warning. Note that this may cause us to turn two warnings into
212 one, but that is fine since it is sufficient to only give one
213 warning per expression. */
215 static const char* fold_deferred_overflow_warning;
217 /* If a warning about undefined overflow is deferred, this is the
218 level at which the warning should be emitted. */
220 static enum warn_strict_overflow_code fold_deferred_overflow_code;
222 /* Start deferring overflow warnings. We could use a stack here to
223 permit nested calls, but at present it is not necessary. */
226 fold_defer_overflow_warnings (void)
228 ++fold_deferring_overflow_warnings;
231 /* Stop deferring overflow warnings. If there is a pending warning,
232 and ISSUE is true, then issue the warning if appropriate. STMT is
233 the statement with which the warning should be associated (used for
234 location information); STMT may be NULL. CODE is the level of the
235 warning--a warn_strict_overflow_code value. This function will use
236 the smaller of CODE and the deferred code when deciding whether to
237 issue the warning. CODE may be zero to mean to always use the
241 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
246 gcc_assert (fold_deferring_overflow_warnings > 0);
247 --fold_deferring_overflow_warnings;
248 if (fold_deferring_overflow_warnings > 0)
250 if (fold_deferred_overflow_warning != NULL
252 && code < (int) fold_deferred_overflow_code)
253 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
257 warnmsg = fold_deferred_overflow_warning;
258 fold_deferred_overflow_warning = NULL;
260 if (!issue || warnmsg == NULL)
263 if (gimple_no_warning_p (stmt))
266 /* Use the smallest code level when deciding to issue the
268 if (code == 0 || code > (int) fold_deferred_overflow_code)
269 code = fold_deferred_overflow_code;
271 if (!issue_strict_overflow_warning (code))
275 locus = input_location;
277 locus = gimple_location (stmt);
278 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
281 /* Stop deferring overflow warnings, ignoring any deferred
285 fold_undefer_and_ignore_overflow_warnings (void)
287 fold_undefer_overflow_warnings (false, NULL, 0);
290 /* Whether we are deferring overflow warnings. */
293 fold_deferring_overflow_warnings_p (void)
295 return fold_deferring_overflow_warnings > 0;
298 /* This is called when we fold something based on the fact that signed
299 overflow is undefined. */
302 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
304 if (fold_deferring_overflow_warnings > 0)
306 if (fold_deferred_overflow_warning == NULL
307 || wc < fold_deferred_overflow_code)
309 fold_deferred_overflow_warning = gmsgid;
310 fold_deferred_overflow_code = wc;
313 else if (issue_strict_overflow_warning (wc))
314 warning (OPT_Wstrict_overflow, gmsgid);
317 /* Return true if the built-in mathematical function specified by CODE
318 is odd, i.e. -f(x) == f(-x). */
321 negate_mathfn_p (enum built_in_function code)
325 CASE_FLT_FN (BUILT_IN_ASIN):
326 CASE_FLT_FN (BUILT_IN_ASINH):
327 CASE_FLT_FN (BUILT_IN_ATAN):
328 CASE_FLT_FN (BUILT_IN_ATANH):
329 CASE_FLT_FN (BUILT_IN_CASIN):
330 CASE_FLT_FN (BUILT_IN_CASINH):
331 CASE_FLT_FN (BUILT_IN_CATAN):
332 CASE_FLT_FN (BUILT_IN_CATANH):
333 CASE_FLT_FN (BUILT_IN_CBRT):
334 CASE_FLT_FN (BUILT_IN_CPROJ):
335 CASE_FLT_FN (BUILT_IN_CSIN):
336 CASE_FLT_FN (BUILT_IN_CSINH):
337 CASE_FLT_FN (BUILT_IN_CTAN):
338 CASE_FLT_FN (BUILT_IN_CTANH):
339 CASE_FLT_FN (BUILT_IN_ERF):
340 CASE_FLT_FN (BUILT_IN_LLROUND):
341 CASE_FLT_FN (BUILT_IN_LROUND):
342 CASE_FLT_FN (BUILT_IN_ROUND):
343 CASE_FLT_FN (BUILT_IN_SIN):
344 CASE_FLT_FN (BUILT_IN_SINH):
345 CASE_FLT_FN (BUILT_IN_TAN):
346 CASE_FLT_FN (BUILT_IN_TANH):
347 CASE_FLT_FN (BUILT_IN_TRUNC):
350 CASE_FLT_FN (BUILT_IN_LLRINT):
351 CASE_FLT_FN (BUILT_IN_LRINT):
352 CASE_FLT_FN (BUILT_IN_NEARBYINT):
353 CASE_FLT_FN (BUILT_IN_RINT):
354 return !flag_rounding_math;
362 /* Check whether we may negate an integer constant T without causing
366 may_negate_without_overflow_p (const_tree t)
370 gcc_assert (TREE_CODE (t) == INTEGER_CST);
372 type = TREE_TYPE (t);
373 if (TYPE_UNSIGNED (type))
376 return !wi::only_sign_bit_p (t);
379 /* Determine whether an expression T can be cheaply negated using
380 the function negate_expr without introducing undefined overflow. */
383 negate_expr_p (tree t)
390 type = TREE_TYPE (t);
393 switch (TREE_CODE (t))
396 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
399 /* Check that -CST will not overflow type. */
400 return may_negate_without_overflow_p (t);
402 return (INTEGRAL_TYPE_P (type)
403 && TYPE_OVERFLOW_WRAPS (type));
409 return !TYPE_OVERFLOW_SANITIZED (type);
412 /* We want to canonicalize to positive real constants. Pretend
413 that only negative ones can be easily negated. */
414 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
417 return negate_expr_p (TREE_REALPART (t))
418 && negate_expr_p (TREE_IMAGPART (t));
422 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
425 int count = TYPE_VECTOR_SUBPARTS (type), i;
427 for (i = 0; i < count; i++)
428 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
435 return negate_expr_p (TREE_OPERAND (t, 0))
436 && negate_expr_p (TREE_OPERAND (t, 1));
439 return negate_expr_p (TREE_OPERAND (t, 0));
442 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
443 || HONOR_SIGNED_ZEROS (element_mode (type)))
445 /* -(A + B) -> (-B) - A. */
446 if (negate_expr_p (TREE_OPERAND (t, 1))
447 && reorder_operands_p (TREE_OPERAND (t, 0),
448 TREE_OPERAND (t, 1)))
450 /* -(A + B) -> (-A) - B. */
451 return negate_expr_p (TREE_OPERAND (t, 0));
454 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
455 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
456 && !HONOR_SIGNED_ZEROS (element_mode (type))
457 && reorder_operands_p (TREE_OPERAND (t, 0),
458 TREE_OPERAND (t, 1));
461 if (TYPE_UNSIGNED (TREE_TYPE (t)))
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
475 /* In general we can't negate A / B, because if A is INT_MIN and
476 B is 1, we may turn this into INT_MIN / -1 which is undefined
477 and actually traps on some architectures. But if overflow is
478 undefined, we can negate, because - (INT_MIN / 1) is an
480 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
482 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
484 /* If overflow is undefined then we have to be careful because
485 we ask whether it's ok to associate the negate with the
486 division which is not ok for example for
487 -((a - b) / c) where (-(a - b)) / c may invoke undefined
488 overflow because of negating INT_MIN. So do not use
489 negate_expr_p here but open-code the two important cases. */
490 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
491 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
492 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
495 else if (negate_expr_p (TREE_OPERAND (t, 0)))
497 return negate_expr_p (TREE_OPERAND (t, 1));
500 /* Negate -((double)float) as (double)(-float). */
501 if (TREE_CODE (type) == REAL_TYPE)
503 tree tem = strip_float_extensions (t);
505 return negate_expr_p (tem);
510 /* Negate -f(x) as f(-x). */
511 if (negate_mathfn_p (builtin_mathfn_code (t)))
512 return negate_expr_p (CALL_EXPR_ARG (t, 0));
516 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
517 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
519 tree op1 = TREE_OPERAND (t, 1);
520 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
531 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
532 simplification is possible.
533 If negate_expr_p would return true for T, NULL_TREE will never be
537 fold_negate_expr (location_t loc, tree t)
539 tree type = TREE_TYPE (t);
542 switch (TREE_CODE (t))
544 /* Convert - (~A) to A + 1. */
546 if (INTEGRAL_TYPE_P (type))
547 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
548 build_one_cst (type));
552 tem = fold_negate_const (t, type);
553 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
554 || (ANY_INTEGRAL_TYPE_P (type)
555 && !TYPE_OVERFLOW_TRAPS (type)
556 && TYPE_OVERFLOW_WRAPS (type))
557 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
562 tem = fold_negate_const (t, type);
566 tem = fold_negate_const (t, type);
571 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
572 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
574 return build_complex (type, rpart, ipart);
580 int count = TYPE_VECTOR_SUBPARTS (type), i;
581 tree *elts = XALLOCAVEC (tree, count);
583 for (i = 0; i < count; i++)
585 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
586 if (elts[i] == NULL_TREE)
590 return build_vector (type, elts);
594 if (negate_expr_p (t))
595 return fold_build2_loc (loc, COMPLEX_EXPR, type,
596 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
597 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
601 if (negate_expr_p (t))
602 return fold_build1_loc (loc, CONJ_EXPR, type,
603 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
607 if (!TYPE_OVERFLOW_SANITIZED (type))
608 return TREE_OPERAND (t, 0);
612 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
613 && !HONOR_SIGNED_ZEROS (element_mode (type)))
615 /* -(A + B) -> (-B) - A. */
616 if (negate_expr_p (TREE_OPERAND (t, 1))
617 && reorder_operands_p (TREE_OPERAND (t, 0),
618 TREE_OPERAND (t, 1)))
620 tem = negate_expr (TREE_OPERAND (t, 1));
621 return fold_build2_loc (loc, MINUS_EXPR, type,
622 tem, TREE_OPERAND (t, 0));
625 /* -(A + B) -> (-A) - B. */
626 if (negate_expr_p (TREE_OPERAND (t, 0)))
628 tem = negate_expr (TREE_OPERAND (t, 0));
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 tem, TREE_OPERAND (t, 1));
636 /* - (A - B) -> B - A */
637 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
638 && !HONOR_SIGNED_ZEROS (element_mode (type))
639 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
640 return fold_build2_loc (loc, MINUS_EXPR, type,
641 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
645 if (TYPE_UNSIGNED (type))
651 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
653 tem = TREE_OPERAND (t, 1);
654 if (negate_expr_p (tem))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 TREE_OPERAND (t, 0), negate_expr (tem));
657 tem = TREE_OPERAND (t, 0);
658 if (negate_expr_p (tem))
659 return fold_build2_loc (loc, TREE_CODE (t), type,
660 negate_expr (tem), TREE_OPERAND (t, 1));
667 /* In general we can't negate A / B, because if A is INT_MIN and
668 B is 1, we may turn this into INT_MIN / -1 which is undefined
669 and actually traps on some architectures. But if overflow is
670 undefined, we can negate, because - (INT_MIN / 1) is an
672 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
674 const char * const warnmsg = G_("assuming signed overflow does not "
675 "occur when negating a division");
676 tem = TREE_OPERAND (t, 1);
677 if (negate_expr_p (tem))
679 if (INTEGRAL_TYPE_P (type)
680 && (TREE_CODE (tem) != INTEGER_CST
681 || integer_onep (tem)))
682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683 return fold_build2_loc (loc, TREE_CODE (t), type,
684 TREE_OPERAND (t, 0), negate_expr (tem));
686 /* If overflow is undefined then we have to be careful because
687 we ask whether it's ok to associate the negate with the
688 division which is not ok for example for
689 -((a - b) / c) where (-(a - b)) / c may invoke undefined
690 overflow because of negating INT_MIN. So do not use
691 negate_expr_p here but open-code the two important cases. */
692 tem = TREE_OPERAND (t, 0);
693 if ((INTEGRAL_TYPE_P (type)
694 && (TREE_CODE (tem) == NEGATE_EXPR
695 || (TREE_CODE (tem) == INTEGER_CST
696 && may_negate_without_overflow_p (tem))))
697 || !INTEGRAL_TYPE_P (type))
698 return fold_build2_loc (loc, TREE_CODE (t), type,
699 negate_expr (tem), TREE_OPERAND (t, 1));
704 /* Convert -((double)float) into (double)(-float). */
705 if (TREE_CODE (type) == REAL_TYPE)
707 tem = strip_float_extensions (t);
708 if (tem != t && negate_expr_p (tem))
709 return fold_convert_loc (loc, type, negate_expr (tem));
714 /* Negate -f(x) as f(-x). */
715 if (negate_mathfn_p (builtin_mathfn_code (t))
716 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
720 fndecl = get_callee_fndecl (t);
721 arg = negate_expr (CALL_EXPR_ARG (t, 0));
722 return build_call_expr_loc (loc, fndecl, 1, arg);
727 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
728 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
730 tree op1 = TREE_OPERAND (t, 1);
731 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
733 tree ntype = TYPE_UNSIGNED (type)
734 ? signed_type_for (type)
735 : unsigned_type_for (type);
736 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
737 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
738 return fold_convert_loc (loc, type, temp);
750 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
751 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
763 loc = EXPR_LOCATION (t);
764 type = TREE_TYPE (t);
767 tem = fold_negate_expr (loc, t);
769 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
770 return fold_convert_loc (loc, type, tem);
773 /* Split a tree IN into a constant, literal and variable parts that could be
774 combined with CODE to make IN. "constant" means an expression with
775 TREE_CONSTANT but that isn't an actual constant. CODE must be a
776 commutative arithmetic operation. Store the constant part into *CONP,
777 the literal in *LITP and return the variable part. If a part isn't
778 present, set it to null. If the tree does not decompose in this way,
779 return the entire tree as the variable part and the other parts as null.
781 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
782 case, we negate an operand that was subtracted. Except if it is a
783 literal for which we use *MINUS_LITP instead.
785 If NEGATE_P is true, we are negating all of IN, again except a literal
786 for which we use *MINUS_LITP instead.
788 If IN is itself a literal or constant, return it as appropriate.
790 Note that we do not guarantee that any of the three values will be the
791 same type as IN, but they will have the same signedness and mode. */
794 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
795 tree *minus_litp, int negate_p)
803 /* Strip any conversions that don't change the machine mode or signedness. */
804 STRIP_SIGN_NOPS (in);
806 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
807 || TREE_CODE (in) == FIXED_CST)
809 else if (TREE_CODE (in) == code
810 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
811 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
812 /* We can associate addition and subtraction together (even
813 though the C standard doesn't say so) for integers because
814 the value is not affected. For reals, the value might be
815 affected, so we can't. */
816 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
817 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
819 tree op0 = TREE_OPERAND (in, 0);
820 tree op1 = TREE_OPERAND (in, 1);
821 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
822 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
824 /* First see if either of the operands is a literal, then a constant. */
825 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
826 || TREE_CODE (op0) == FIXED_CST)
827 *litp = op0, op0 = 0;
828 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
829 || TREE_CODE (op1) == FIXED_CST)
830 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
832 if (op0 != 0 && TREE_CONSTANT (op0))
833 *conp = op0, op0 = 0;
834 else if (op1 != 0 && TREE_CONSTANT (op1))
835 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
837 /* If we haven't dealt with either operand, this is not a case we can
838 decompose. Otherwise, VAR is either of the ones remaining, if any. */
839 if (op0 != 0 && op1 != 0)
844 var = op1, neg_var_p = neg1_p;
846 /* Now do any needed negations. */
848 *minus_litp = *litp, *litp = 0;
850 *conp = negate_expr (*conp);
852 var = negate_expr (var);
854 else if (TREE_CODE (in) == BIT_NOT_EXPR
855 && code == PLUS_EXPR)
857 /* -X - 1 is folded to ~X, undo that here. */
858 *minus_litp = build_one_cst (TREE_TYPE (in));
859 var = negate_expr (TREE_OPERAND (in, 0));
861 else if (TREE_CONSTANT (in))
869 *minus_litp = *litp, *litp = 0;
870 else if (*minus_litp)
871 *litp = *minus_litp, *minus_litp = 0;
872 *conp = negate_expr (*conp);
873 var = negate_expr (var);
879 /* Re-associate trees split by the above function. T1 and T2 are
880 either expressions to associate or null. Return the new
881 expression, if any. LOC is the location of the new expression. If
882 we build an operation, do it in TYPE and with CODE. */
885 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
892 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
893 try to fold this since we will have infinite recursion. But do
894 deal with any NEGATE_EXPRs. */
895 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
896 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
898 if (code == PLUS_EXPR)
900 if (TREE_CODE (t1) == NEGATE_EXPR)
901 return build2_loc (loc, MINUS_EXPR, type,
902 fold_convert_loc (loc, type, t2),
903 fold_convert_loc (loc, type,
904 TREE_OPERAND (t1, 0)));
905 else if (TREE_CODE (t2) == NEGATE_EXPR)
906 return build2_loc (loc, MINUS_EXPR, type,
907 fold_convert_loc (loc, type, t1),
908 fold_convert_loc (loc, type,
909 TREE_OPERAND (t2, 0)));
910 else if (integer_zerop (t2))
911 return fold_convert_loc (loc, type, t1);
913 else if (code == MINUS_EXPR)
915 if (integer_zerop (t2))
916 return fold_convert_loc (loc, type, t1);
919 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
920 fold_convert_loc (loc, type, t2));
923 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
924 fold_convert_loc (loc, type, t2));
927 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
928 for use in int_const_binop, size_binop and size_diffop. */
931 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
933 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
935 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
951 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
952 && TYPE_MODE (type1) == TYPE_MODE (type2);
956 /* Combine two integer constants ARG1 and ARG2 under operation CODE
957 to produce a new constant. Return NULL_TREE if we don't know how
958 to evaluate CODE at compile-time. */
961 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
966 tree type = TREE_TYPE (arg1);
967 signop sign = TYPE_SIGN (type);
968 bool overflow = false;
970 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
971 TYPE_SIGN (TREE_TYPE (parg2)));
976 res = wi::bit_or (arg1, arg2);
980 res = wi::bit_xor (arg1, arg2);
984 res = wi::bit_and (arg1, arg2);
989 if (wi::neg_p (arg2))
992 if (code == RSHIFT_EXPR)
998 if (code == RSHIFT_EXPR)
999 /* It's unclear from the C standard whether shifts can overflow.
1000 The following code ignores overflow; perhaps a C standard
1001 interpretation ruling is needed. */
1002 res = wi::rshift (arg1, arg2, sign);
1004 res = wi::lshift (arg1, arg2);
1009 if (wi::neg_p (arg2))
1012 if (code == RROTATE_EXPR)
1013 code = LROTATE_EXPR;
1015 code = RROTATE_EXPR;
1018 if (code == RROTATE_EXPR)
1019 res = wi::rrotate (arg1, arg2);
1021 res = wi::lrotate (arg1, arg2);
1025 res = wi::add (arg1, arg2, sign, &overflow);
1029 res = wi::sub (arg1, arg2, sign, &overflow);
1033 res = wi::mul (arg1, arg2, sign, &overflow);
1036 case MULT_HIGHPART_EXPR:
1037 res = wi::mul_high (arg1, arg2, sign);
1040 case TRUNC_DIV_EXPR:
1041 case EXACT_DIV_EXPR:
1044 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1047 case FLOOR_DIV_EXPR:
1050 res = wi::div_floor (arg1, arg2, sign, &overflow);
1056 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1059 case ROUND_DIV_EXPR:
1062 res = wi::div_round (arg1, arg2, sign, &overflow);
1065 case TRUNC_MOD_EXPR:
1068 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1071 case FLOOR_MOD_EXPR:
1074 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1080 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1083 case ROUND_MOD_EXPR:
1086 res = wi::mod_round (arg1, arg2, sign, &overflow);
1090 res = wi::min (arg1, arg2, sign);
1094 res = wi::max (arg1, arg2, sign);
1101 t = force_fit_type (type, res, overflowable,
1102 (((sign == SIGNED || overflowable == -1)
1104 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1110 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1112 return int_const_binop_1 (code, arg1, arg2, 1);
1115 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1116 constant. We assume ARG1 and ARG2 have the same data type, or at least
1117 are the same kind of constant and the same machine mode. Return zero if
1118 combining the constants is not allowed in the current operating mode. */
1121 const_binop (enum tree_code code, tree arg1, tree arg2)
1123 /* Sanity check for the recursive cases. */
1130 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1132 if (code == POINTER_PLUS_EXPR)
1133 return int_const_binop (PLUS_EXPR,
1134 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1136 return int_const_binop (code, arg1, arg2);
1139 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1144 REAL_VALUE_TYPE value;
1145 REAL_VALUE_TYPE result;
1149 /* The following codes are handled by real_arithmetic. */
1164 d1 = TREE_REAL_CST (arg1);
1165 d2 = TREE_REAL_CST (arg2);
1167 type = TREE_TYPE (arg1);
1168 mode = TYPE_MODE (type);
1170 /* Don't perform operation if we honor signaling NaNs and
1171 either operand is a NaN. */
1172 if (HONOR_SNANS (mode)
1173 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1176 /* Don't perform operation if it would raise a division
1177 by zero exception. */
1178 if (code == RDIV_EXPR
1179 && REAL_VALUES_EQUAL (d2, dconst0)
1180 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1183 /* If either operand is a NaN, just return it. Otherwise, set up
1184 for floating-point trap; we return an overflow. */
1185 if (REAL_VALUE_ISNAN (d1))
1187 else if (REAL_VALUE_ISNAN (d2))
1190 inexact = real_arithmetic (&value, code, &d1, &d2);
1191 real_convert (&result, mode, &value);
1193 /* Don't constant fold this floating point operation if
1194 the result has overflowed and flag_trapping_math. */
1195 if (flag_trapping_math
1196 && MODE_HAS_INFINITIES (mode)
1197 && REAL_VALUE_ISINF (result)
1198 && !REAL_VALUE_ISINF (d1)
1199 && !REAL_VALUE_ISINF (d2))
1202 /* Don't constant fold this floating point operation if the
1203 result may dependent upon the run-time rounding mode and
1204 flag_rounding_math is set, or if GCC's software emulation
1205 is unable to accurately represent the result. */
1206 if ((flag_rounding_math
1207 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1208 && (inexact || !real_identical (&result, &value)))
1211 t = build_real (type, result);
1213 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1217 if (TREE_CODE (arg1) == FIXED_CST)
1219 FIXED_VALUE_TYPE f1;
1220 FIXED_VALUE_TYPE f2;
1221 FIXED_VALUE_TYPE result;
1226 /* The following codes are handled by fixed_arithmetic. */
1232 case TRUNC_DIV_EXPR:
1233 if (TREE_CODE (arg2) != FIXED_CST)
1235 f2 = TREE_FIXED_CST (arg2);
1241 if (TREE_CODE (arg2) != INTEGER_CST)
1244 f2.data.high = w2.elt (1);
1245 f2.data.low = w2.elt (0);
1254 f1 = TREE_FIXED_CST (arg1);
1255 type = TREE_TYPE (arg1);
1256 sat_p = TYPE_SATURATING (type);
1257 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1258 t = build_fixed (type, result);
1259 /* Propagate overflow flags. */
1260 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1261 TREE_OVERFLOW (t) = 1;
1265 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1267 tree type = TREE_TYPE (arg1);
1268 tree r1 = TREE_REALPART (arg1);
1269 tree i1 = TREE_IMAGPART (arg1);
1270 tree r2 = TREE_REALPART (arg2);
1271 tree i2 = TREE_IMAGPART (arg2);
1278 real = const_binop (code, r1, r2);
1279 imag = const_binop (code, i1, i2);
1283 if (COMPLEX_FLOAT_TYPE_P (type))
1284 return do_mpc_arg2 (arg1, arg2, type,
1285 /* do_nonfinite= */ folding_initializer,
1288 real = const_binop (MINUS_EXPR,
1289 const_binop (MULT_EXPR, r1, r2),
1290 const_binop (MULT_EXPR, i1, i2));
1291 imag = const_binop (PLUS_EXPR,
1292 const_binop (MULT_EXPR, r1, i2),
1293 const_binop (MULT_EXPR, i1, r2));
1297 if (COMPLEX_FLOAT_TYPE_P (type))
1298 return do_mpc_arg2 (arg1, arg2, type,
1299 /* do_nonfinite= */ folding_initializer,
1302 case TRUNC_DIV_EXPR:
1304 case FLOOR_DIV_EXPR:
1305 case ROUND_DIV_EXPR:
1306 if (flag_complex_method == 0)
1308 /* Keep this algorithm in sync with
1309 tree-complex.c:expand_complex_div_straight().
1311 Expand complex division to scalars, straightforward algorithm.
1312 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1316 = const_binop (PLUS_EXPR,
1317 const_binop (MULT_EXPR, r2, r2),
1318 const_binop (MULT_EXPR, i2, i2));
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r1, r2),
1322 const_binop (MULT_EXPR, i1, i2));
1324 = const_binop (MINUS_EXPR,
1325 const_binop (MULT_EXPR, i1, r2),
1326 const_binop (MULT_EXPR, r1, i2));
1328 real = const_binop (code, t1, magsquared);
1329 imag = const_binop (code, t2, magsquared);
1333 /* Keep this algorithm in sync with
1334 tree-complex.c:expand_complex_div_wide().
1336 Expand complex division to scalars, modified algorithm to minimize
1337 overflow with wide input ranges. */
1338 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1339 fold_abs_const (r2, TREE_TYPE (type)),
1340 fold_abs_const (i2, TREE_TYPE (type)));
1342 if (integer_nonzerop (compare))
1344 /* In the TRUE branch, we compute
1346 div = (br * ratio) + bi;
1347 tr = (ar * ratio) + ai;
1348 ti = (ai * ratio) - ar;
1351 tree ratio = const_binop (code, r2, i2);
1352 tree div = const_binop (PLUS_EXPR, i2,
1353 const_binop (MULT_EXPR, r2, ratio));
1354 real = const_binop (MULT_EXPR, r1, ratio);
1355 real = const_binop (PLUS_EXPR, real, i1);
1356 real = const_binop (code, real, div);
1358 imag = const_binop (MULT_EXPR, i1, ratio);
1359 imag = const_binop (MINUS_EXPR, imag, r1);
1360 imag = const_binop (code, imag, div);
1364 /* In the FALSE branch, we compute
1366 divisor = (d * ratio) + c;
1367 tr = (b * ratio) + a;
1368 ti = b - (a * ratio);
1371 tree ratio = const_binop (code, i2, r2);
1372 tree div = const_binop (PLUS_EXPR, r2,
1373 const_binop (MULT_EXPR, i2, ratio));
1375 real = const_binop (MULT_EXPR, i1, ratio);
1376 real = const_binop (PLUS_EXPR, real, r1);
1377 real = const_binop (code, real, div);
1379 imag = const_binop (MULT_EXPR, r1, ratio);
1380 imag = const_binop (MINUS_EXPR, i1, imag);
1381 imag = const_binop (code, imag, div);
1391 return build_complex (type, real, imag);
1394 if (TREE_CODE (arg1) == VECTOR_CST
1395 && TREE_CODE (arg2) == VECTOR_CST)
1397 tree type = TREE_TYPE (arg1);
1398 int count = TYPE_VECTOR_SUBPARTS (type), i;
1399 tree *elts = XALLOCAVEC (tree, count);
1401 for (i = 0; i < count; i++)
1403 tree elem1 = VECTOR_CST_ELT (arg1, i);
1404 tree elem2 = VECTOR_CST_ELT (arg2, i);
1406 elts[i] = const_binop (code, elem1, elem2);
1408 /* It is possible that const_binop cannot handle the given
1409 code and return NULL_TREE */
1410 if (elts[i] == NULL_TREE)
1414 return build_vector (type, elts);
1417 /* Shifts allow a scalar offset for a vector. */
1418 if (TREE_CODE (arg1) == VECTOR_CST
1419 && TREE_CODE (arg2) == INTEGER_CST)
1421 tree type = TREE_TYPE (arg1);
1422 int count = TYPE_VECTOR_SUBPARTS (type), i;
1423 tree *elts = XALLOCAVEC (tree, count);
1425 for (i = 0; i < count; i++)
1427 tree elem1 = VECTOR_CST_ELT (arg1, i);
1429 elts[i] = const_binop (code, elem1, arg2);
1431 /* It is possible that const_binop cannot handle the given
1432 code and return NULL_TREE. */
1433 if (elts[i] == NULL_TREE)
1437 return build_vector (type, elts);
1442 /* Overload that adds a TYPE parameter to be able to dispatch
1443 to fold_relational_const. */
1446 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1448 if (TREE_CODE_CLASS (code) == tcc_comparison)
1449 return fold_relational_const (code, type, arg1, arg2);
1451 /* ??? Until we make the const_binop worker take the type of the
1452 result as argument put those cases that need it here. */
1456 if ((TREE_CODE (arg1) == REAL_CST
1457 && TREE_CODE (arg2) == REAL_CST)
1458 || (TREE_CODE (arg1) == INTEGER_CST
1459 && TREE_CODE (arg2) == INTEGER_CST))
1460 return build_complex (type, arg1, arg2);
1463 case VEC_PACK_TRUNC_EXPR:
1464 case VEC_PACK_FIX_TRUNC_EXPR:
1466 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1469 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1470 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1471 if (TREE_CODE (arg1) != VECTOR_CST
1472 || TREE_CODE (arg2) != VECTOR_CST)
1475 elts = XALLOCAVEC (tree, nelts);
1476 if (!vec_cst_ctor_to_array (arg1, elts)
1477 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1480 for (i = 0; i < nelts; i++)
1482 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1483 ? NOP_EXPR : FIX_TRUNC_EXPR,
1484 TREE_TYPE (type), elts[i]);
1485 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1489 return build_vector (type, elts);
1492 case VEC_WIDEN_MULT_LO_EXPR:
1493 case VEC_WIDEN_MULT_HI_EXPR:
1494 case VEC_WIDEN_MULT_EVEN_EXPR:
1495 case VEC_WIDEN_MULT_ODD_EXPR:
1497 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1498 unsigned int out, ofs, scale;
1501 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1502 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1503 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1506 elts = XALLOCAVEC (tree, nelts * 4);
1507 if (!vec_cst_ctor_to_array (arg1, elts)
1508 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1511 if (code == VEC_WIDEN_MULT_LO_EXPR)
1512 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1513 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1514 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1515 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1517 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1520 for (out = 0; out < nelts; out++)
1522 unsigned int in1 = (out << scale) + ofs;
1523 unsigned int in2 = in1 + nelts * 2;
1526 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1527 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1529 if (t1 == NULL_TREE || t2 == NULL_TREE)
1531 elts[out] = const_binop (MULT_EXPR, t1, t2);
1532 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1536 return build_vector (type, elts);
1542 if (TREE_CODE_CLASS (code) != tcc_binary)
1545 /* Make sure type and arg0 have the same saturating flag. */
1546 gcc_checking_assert (TYPE_SATURATING (type)
1547 == TYPE_SATURATING (TREE_TYPE (arg1)));
1549 return const_binop (code, arg1, arg2);
1552 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1553 Return zero if computing the constants is not possible. */
1556 const_unop (enum tree_code code, tree type, tree arg0)
1562 case FIX_TRUNC_EXPR:
1563 case FIXED_CONVERT_EXPR:
1564 return fold_convert_const (code, type, arg0);
1566 case ADDR_SPACE_CONVERT_EXPR:
1567 if (integer_zerop (arg0))
1568 return fold_convert_const (code, type, arg0);
1571 case VIEW_CONVERT_EXPR:
1572 return fold_view_convert_expr (type, arg0);
1576 /* Can't call fold_negate_const directly here as that doesn't
1577 handle all cases and we might not be able to negate some
1579 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1580 if (tem && CONSTANT_CLASS_P (tem))
1586 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1587 return fold_abs_const (arg0, type);
1591 if (TREE_CODE (arg0) == COMPLEX_CST)
1593 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1595 return build_complex (type, TREE_REALPART (arg0), ipart);
1600 if (TREE_CODE (arg0) == INTEGER_CST)
1601 return fold_not_const (arg0, type);
1602 /* Perform BIT_NOT_EXPR on each element individually. */
1603 else if (TREE_CODE (arg0) == VECTOR_CST)
1607 unsigned count = VECTOR_CST_NELTS (arg0), i;
1609 elements = XALLOCAVEC (tree, count);
1610 for (i = 0; i < count; i++)
1612 elem = VECTOR_CST_ELT (arg0, i);
1613 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1614 if (elem == NULL_TREE)
1619 return build_vector (type, elements);
1623 case TRUTH_NOT_EXPR:
1624 if (TREE_CODE (arg0) == INTEGER_CST)
1625 return constant_boolean_node (integer_zerop (arg0), type);
1629 if (TREE_CODE (arg0) == COMPLEX_CST)
1630 return fold_convert (type, TREE_REALPART (arg0));
1634 if (TREE_CODE (arg0) == COMPLEX_CST)
1635 return fold_convert (type, TREE_IMAGPART (arg0));
1638 case VEC_UNPACK_LO_EXPR:
1639 case VEC_UNPACK_HI_EXPR:
1640 case VEC_UNPACK_FLOAT_LO_EXPR:
1641 case VEC_UNPACK_FLOAT_HI_EXPR:
1643 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1645 enum tree_code subcode;
1647 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1648 if (TREE_CODE (arg0) != VECTOR_CST)
1651 elts = XALLOCAVEC (tree, nelts * 2);
1652 if (!vec_cst_ctor_to_array (arg0, elts))
1655 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1656 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1659 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1662 subcode = FLOAT_EXPR;
1664 for (i = 0; i < nelts; i++)
1666 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1667 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1671 return build_vector (type, elts);
1674 case REDUC_MIN_EXPR:
1675 case REDUC_MAX_EXPR:
1676 case REDUC_PLUS_EXPR:
1678 unsigned int nelts, i;
1680 enum tree_code subcode;
1682 if (TREE_CODE (arg0) != VECTOR_CST)
1684 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1686 elts = XALLOCAVEC (tree, nelts);
1687 if (!vec_cst_ctor_to_array (arg0, elts))
1692 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1693 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1694 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1695 default: gcc_unreachable ();
1698 for (i = 1; i < nelts; i++)
1700 elts[0] = const_binop (subcode, elts[0], elts[i]);
1701 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1715 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1716 indicates which particular sizetype to create. */
1719 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1721 return build_int_cst (sizetype_tab[(int) kind], number);
1724 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1725 is a tree code. The type of the result is taken from the operands.
1726 Both must be equivalent integer types, ala int_binop_types_match_p.
1727 If the operands are constant, so is the result. */
1730 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1732 tree type = TREE_TYPE (arg0);
1734 if (arg0 == error_mark_node || arg1 == error_mark_node)
1735 return error_mark_node;
1737 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1740 /* Handle the special case of two integer constants faster. */
1741 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1743 /* And some specific cases even faster than that. */
1744 if (code == PLUS_EXPR)
1746 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1748 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1751 else if (code == MINUS_EXPR)
1753 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1756 else if (code == MULT_EXPR)
1758 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1762 /* Handle general case of two integer constants. For sizetype
1763 constant calculations we always want to know about overflow,
1764 even in the unsigned case. */
1765 return int_const_binop_1 (code, arg0, arg1, -1);
1768 return fold_build2_loc (loc, code, type, arg0, arg1);
1771 /* Given two values, either both of sizetype or both of bitsizetype,
1772 compute the difference between the two values. Return the value
1773 in signed type corresponding to the type of the operands. */
1776 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1778 tree type = TREE_TYPE (arg0);
1781 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1784 /* If the type is already signed, just do the simple thing. */
1785 if (!TYPE_UNSIGNED (type))
1786 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1788 if (type == sizetype)
1790 else if (type == bitsizetype)
1791 ctype = sbitsizetype;
1793 ctype = signed_type_for (type);
1795 /* If either operand is not a constant, do the conversions to the signed
1796 type and subtract. The hardware will do the right thing with any
1797 overflow in the subtraction. */
1798 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1799 return size_binop_loc (loc, MINUS_EXPR,
1800 fold_convert_loc (loc, ctype, arg0),
1801 fold_convert_loc (loc, ctype, arg1));
1803 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1804 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1805 overflow) and negate (which can't either). Special-case a result
1806 of zero while we're here. */
1807 if (tree_int_cst_equal (arg0, arg1))
1808 return build_int_cst (ctype, 0);
1809 else if (tree_int_cst_lt (arg1, arg0))
1810 return fold_convert_loc (loc, ctype,
1811 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1813 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1814 fold_convert_loc (loc, ctype,
1815 size_binop_loc (loc,
1820 /* A subroutine of fold_convert_const handling conversions of an
1821 INTEGER_CST to another integer type. */
1824 fold_convert_const_int_from_int (tree type, const_tree arg1)
1826 /* Given an integer constant, make new constant with new type,
1827 appropriately sign-extended or truncated. Use widest_int
1828 so that any extension is done according ARG1's type. */
1829 return force_fit_type (type, wi::to_widest (arg1),
1830 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1831 TREE_OVERFLOW (arg1));
1834 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1835 to an integer type. */
1838 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1840 bool overflow = false;
1843 /* The following code implements the floating point to integer
1844 conversion rules required by the Java Language Specification,
1845 that IEEE NaNs are mapped to zero and values that overflow
1846 the target precision saturate, i.e. values greater than
1847 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1848 are mapped to INT_MIN. These semantics are allowed by the
1849 C and C++ standards that simply state that the behavior of
1850 FP-to-integer conversion is unspecified upon overflow. */
1854 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1858 case FIX_TRUNC_EXPR:
1859 real_trunc (&r, VOIDmode, &x);
1866 /* If R is NaN, return zero and show we have an overflow. */
1867 if (REAL_VALUE_ISNAN (r))
1870 val = wi::zero (TYPE_PRECISION (type));
1873 /* See if R is less than the lower bound or greater than the
1878 tree lt = TYPE_MIN_VALUE (type);
1879 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1880 if (REAL_VALUES_LESS (r, l))
1889 tree ut = TYPE_MAX_VALUE (type);
1892 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1893 if (REAL_VALUES_LESS (u, r))
1902 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1904 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1908 /* A subroutine of fold_convert_const handling conversions of a
1909 FIXED_CST to an integer type. */
1912 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1915 double_int temp, temp_trunc;
1918 /* Right shift FIXED_CST to temp by fbit. */
1919 temp = TREE_FIXED_CST (arg1).data;
1920 mode = TREE_FIXED_CST (arg1).mode;
1921 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1923 temp = temp.rshift (GET_MODE_FBIT (mode),
1924 HOST_BITS_PER_DOUBLE_INT,
1925 SIGNED_FIXED_POINT_MODE_P (mode));
1927 /* Left shift temp to temp_trunc by fbit. */
1928 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1929 HOST_BITS_PER_DOUBLE_INT,
1930 SIGNED_FIXED_POINT_MODE_P (mode));
1934 temp = double_int_zero;
1935 temp_trunc = double_int_zero;
1938 /* If FIXED_CST is negative, we need to round the value toward 0.
1939 By checking if the fractional bits are not zero to add 1 to temp. */
1940 if (SIGNED_FIXED_POINT_MODE_P (mode)
1941 && temp_trunc.is_negative ()
1942 && TREE_FIXED_CST (arg1).data != temp_trunc)
1943 temp += double_int_one;
1945 /* Given a fixed-point constant, make new constant with new type,
1946 appropriately sign-extended or truncated. */
1947 t = force_fit_type (type, temp, -1,
1948 (temp.is_negative ()
1949 && (TYPE_UNSIGNED (type)
1950 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1951 | TREE_OVERFLOW (arg1));
1956 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1957 to another floating point type. */
1960 fold_convert_const_real_from_real (tree type, const_tree arg1)
1962 REAL_VALUE_TYPE value;
1965 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1966 t = build_real (type, value);
1968 /* If converting an infinity or NAN to a representation that doesn't
1969 have one, set the overflow bit so that we can produce some kind of
1970 error message at the appropriate point if necessary. It's not the
1971 most user-friendly message, but it's better than nothing. */
1972 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1973 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1974 TREE_OVERFLOW (t) = 1;
1975 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1976 && !MODE_HAS_NANS (TYPE_MODE (type)))
1977 TREE_OVERFLOW (t) = 1;
1978 /* Regular overflow, conversion produced an infinity in a mode that
1979 can't represent them. */
1980 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1981 && REAL_VALUE_ISINF (value)
1982 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1983 TREE_OVERFLOW (t) = 1;
1985 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1989 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1990 to a floating point type. */
1993 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1995 REAL_VALUE_TYPE value;
1998 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1999 t = build_real (type, value);
2001 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2005 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2006 to another fixed-point type. */
2009 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2011 FIXED_VALUE_TYPE value;
2015 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2016 TYPE_SATURATING (type));
2017 t = build_fixed (type, value);
2019 /* Propagate overflow flags. */
2020 if (overflow_p | TREE_OVERFLOW (arg1))
2021 TREE_OVERFLOW (t) = 1;
2025 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2026 to a fixed-point type. */
2029 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2031 FIXED_VALUE_TYPE value;
2036 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2038 di.low = TREE_INT_CST_ELT (arg1, 0);
2039 if (TREE_INT_CST_NUNITS (arg1) == 1)
2040 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2042 di.high = TREE_INT_CST_ELT (arg1, 1);
2044 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2045 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2046 TYPE_SATURATING (type));
2047 t = build_fixed (type, value);
2049 /* Propagate overflow flags. */
2050 if (overflow_p | TREE_OVERFLOW (arg1))
2051 TREE_OVERFLOW (t) = 1;
2055 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2056 to a fixed-point type. */
2059 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2061 FIXED_VALUE_TYPE value;
2065 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2066 &TREE_REAL_CST (arg1),
2067 TYPE_SATURATING (type));
2068 t = build_fixed (type, value);
2070 /* Propagate overflow flags. */
2071 if (overflow_p | TREE_OVERFLOW (arg1))
2072 TREE_OVERFLOW (t) = 1;
2076 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2077 type TYPE. If no simplification can be done return NULL_TREE. */
2080 fold_convert_const (enum tree_code code, tree type, tree arg1)
2082 if (TREE_TYPE (arg1) == type)
2085 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2086 || TREE_CODE (type) == OFFSET_TYPE)
2088 if (TREE_CODE (arg1) == INTEGER_CST)
2089 return fold_convert_const_int_from_int (type, arg1);
2090 else if (TREE_CODE (arg1) == REAL_CST)
2091 return fold_convert_const_int_from_real (code, type, arg1);
2092 else if (TREE_CODE (arg1) == FIXED_CST)
2093 return fold_convert_const_int_from_fixed (type, arg1);
2095 else if (TREE_CODE (type) == REAL_TYPE)
2097 if (TREE_CODE (arg1) == INTEGER_CST)
2098 return build_real_from_int_cst (type, arg1);
2099 else if (TREE_CODE (arg1) == REAL_CST)
2100 return fold_convert_const_real_from_real (type, arg1);
2101 else if (TREE_CODE (arg1) == FIXED_CST)
2102 return fold_convert_const_real_from_fixed (type, arg1);
2104 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2106 if (TREE_CODE (arg1) == FIXED_CST)
2107 return fold_convert_const_fixed_from_fixed (type, arg1);
2108 else if (TREE_CODE (arg1) == INTEGER_CST)
2109 return fold_convert_const_fixed_from_int (type, arg1);
2110 else if (TREE_CODE (arg1) == REAL_CST)
2111 return fold_convert_const_fixed_from_real (type, arg1);
2116 /* Construct a vector of zero elements of vector type TYPE. */
2119 build_zero_vector (tree type)
2123 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2124 return build_vector_from_val (type, t);
2127 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2130 fold_convertible_p (const_tree type, const_tree arg)
2132 tree orig = TREE_TYPE (arg);
2137 if (TREE_CODE (arg) == ERROR_MARK
2138 || TREE_CODE (type) == ERROR_MARK
2139 || TREE_CODE (orig) == ERROR_MARK)
2142 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2145 switch (TREE_CODE (type))
2147 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2148 case POINTER_TYPE: case REFERENCE_TYPE:
2150 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2151 || TREE_CODE (orig) == OFFSET_TYPE)
2153 return (TREE_CODE (orig) == VECTOR_TYPE
2154 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2157 case FIXED_POINT_TYPE:
2161 return TREE_CODE (type) == TREE_CODE (orig);
2168 /* Convert expression ARG to type TYPE. Used by the middle-end for
2169 simple conversions in preference to calling the front-end's convert. */
2172 fold_convert_loc (location_t loc, tree type, tree arg)
2174 tree orig = TREE_TYPE (arg);
2180 if (TREE_CODE (arg) == ERROR_MARK
2181 || TREE_CODE (type) == ERROR_MARK
2182 || TREE_CODE (orig) == ERROR_MARK)
2183 return error_mark_node;
2185 switch (TREE_CODE (type))
2188 case REFERENCE_TYPE:
2189 /* Handle conversions between pointers to different address spaces. */
2190 if (POINTER_TYPE_P (orig)
2191 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2192 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2193 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2196 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2198 if (TREE_CODE (arg) == INTEGER_CST)
2200 tem = fold_convert_const (NOP_EXPR, type, arg);
2201 if (tem != NULL_TREE)
2204 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2205 || TREE_CODE (orig) == OFFSET_TYPE)
2206 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2207 if (TREE_CODE (orig) == COMPLEX_TYPE)
2208 return fold_convert_loc (loc, type,
2209 fold_build1_loc (loc, REALPART_EXPR,
2210 TREE_TYPE (orig), arg));
2211 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2212 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2213 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2216 if (TREE_CODE (arg) == INTEGER_CST)
2218 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2219 if (tem != NULL_TREE)
2222 else if (TREE_CODE (arg) == REAL_CST)
2224 tem = fold_convert_const (NOP_EXPR, type, arg);
2225 if (tem != NULL_TREE)
2228 else if (TREE_CODE (arg) == FIXED_CST)
2230 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2231 if (tem != NULL_TREE)
2235 switch (TREE_CODE (orig))
2238 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2239 case POINTER_TYPE: case REFERENCE_TYPE:
2240 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2243 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2245 case FIXED_POINT_TYPE:
2246 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2249 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2250 return fold_convert_loc (loc, type, tem);
2256 case FIXED_POINT_TYPE:
2257 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2258 || TREE_CODE (arg) == REAL_CST)
2260 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2261 if (tem != NULL_TREE)
2262 goto fold_convert_exit;
2265 switch (TREE_CODE (orig))
2267 case FIXED_POINT_TYPE:
2272 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2275 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2276 return fold_convert_loc (loc, type, tem);
2283 switch (TREE_CODE (orig))
2286 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2287 case POINTER_TYPE: case REFERENCE_TYPE:
2289 case FIXED_POINT_TYPE:
2290 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2291 fold_convert_loc (loc, TREE_TYPE (type), arg),
2292 fold_convert_loc (loc, TREE_TYPE (type),
2293 integer_zero_node));
2298 if (TREE_CODE (arg) == COMPLEX_EXPR)
2300 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2301 TREE_OPERAND (arg, 0));
2302 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2303 TREE_OPERAND (arg, 1));
2304 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2307 arg = save_expr (arg);
2308 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2309 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2310 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2311 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2312 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2320 if (integer_zerop (arg))
2321 return build_zero_vector (type);
2322 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2323 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2324 || TREE_CODE (orig) == VECTOR_TYPE);
2325 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2328 tem = fold_ignored_result (arg);
2329 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2332 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2333 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2337 protected_set_expr_location_unshare (tem, loc);
2341 /* Return false if expr can be assumed not to be an lvalue, true
2345 maybe_lvalue_p (const_tree x)
2347 /* We only need to wrap lvalue tree codes. */
2348 switch (TREE_CODE (x))
2361 case ARRAY_RANGE_REF:
2367 case PREINCREMENT_EXPR:
2368 case PREDECREMENT_EXPR:
2370 case TRY_CATCH_EXPR:
2371 case WITH_CLEANUP_EXPR:
2380 /* Assume the worst for front-end tree codes. */
2381 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2389 /* Return an expr equal to X but certainly not valid as an lvalue. */
2392 non_lvalue_loc (location_t loc, tree x)
2394 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2399 if (! maybe_lvalue_p (x))
2401 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2404 /* When pedantic, return an expr equal to X but certainly not valid as a
2405 pedantic lvalue. Otherwise, return X. */
2408 pedantic_non_lvalue_loc (location_t loc, tree x)
2410 return protected_set_expr_location_unshare (x, loc);
2413 /* Given a tree comparison code, return the code that is the logical inverse.
2414 It is generally not safe to do this for floating-point comparisons, except
2415 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2416 ERROR_MARK in this case. */
2419 invert_tree_comparison (enum tree_code code, bool honor_nans)
2421 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2422 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2432 return honor_nans ? UNLE_EXPR : LE_EXPR;
2434 return honor_nans ? UNLT_EXPR : LT_EXPR;
2436 return honor_nans ? UNGE_EXPR : GE_EXPR;
2438 return honor_nans ? UNGT_EXPR : GT_EXPR;
2452 return UNORDERED_EXPR;
2453 case UNORDERED_EXPR:
2454 return ORDERED_EXPR;
2460 /* Similar, but return the comparison that results if the operands are
2461 swapped. This is safe for floating-point. */
2464 swap_tree_comparison (enum tree_code code)
2471 case UNORDERED_EXPR:
2497 /* Convert a comparison tree code from an enum tree_code representation
2498 into a compcode bit-based encoding. This function is the inverse of
2499 compcode_to_comparison. */
2501 static enum comparison_code
2502 comparison_to_compcode (enum tree_code code)
2519 return COMPCODE_ORD;
2520 case UNORDERED_EXPR:
2521 return COMPCODE_UNORD;
2523 return COMPCODE_UNLT;
2525 return COMPCODE_UNEQ;
2527 return COMPCODE_UNLE;
2529 return COMPCODE_UNGT;
2531 return COMPCODE_LTGT;
2533 return COMPCODE_UNGE;
2539 /* Convert a compcode bit-based encoding of a comparison operator back
2540 to GCC's enum tree_code representation. This function is the
2541 inverse of comparison_to_compcode. */
2543 static enum tree_code
2544 compcode_to_comparison (enum comparison_code code)
2561 return ORDERED_EXPR;
2562 case COMPCODE_UNORD:
2563 return UNORDERED_EXPR;
2581 /* Return a tree for the comparison which is the combination of
2582 doing the AND or OR (depending on CODE) of the two operations LCODE
2583 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2584 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2585 if this makes the transformation invalid. */
2588 combine_comparisons (location_t loc,
2589 enum tree_code code, enum tree_code lcode,
2590 enum tree_code rcode, tree truth_type,
2591 tree ll_arg, tree lr_arg)
2593 bool honor_nans = HONOR_NANS (ll_arg);
2594 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2595 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2600 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2601 compcode = lcompcode & rcompcode;
2604 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2605 compcode = lcompcode | rcompcode;
2614 /* Eliminate unordered comparisons, as well as LTGT and ORD
2615 which are not used unless the mode has NaNs. */
2616 compcode &= ~COMPCODE_UNORD;
2617 if (compcode == COMPCODE_LTGT)
2618 compcode = COMPCODE_NE;
2619 else if (compcode == COMPCODE_ORD)
2620 compcode = COMPCODE_TRUE;
2622 else if (flag_trapping_math)
2624 /* Check that the original operation and the optimized ones will trap
2625 under the same condition. */
2626 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2627 && (lcompcode != COMPCODE_EQ)
2628 && (lcompcode != COMPCODE_ORD);
2629 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2630 && (rcompcode != COMPCODE_EQ)
2631 && (rcompcode != COMPCODE_ORD);
2632 bool trap = (compcode & COMPCODE_UNORD) == 0
2633 && (compcode != COMPCODE_EQ)
2634 && (compcode != COMPCODE_ORD);
2636 /* In a short-circuited boolean expression the LHS might be
2637 such that the RHS, if evaluated, will never trap. For
2638 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2639 if neither x nor y is NaN. (This is a mixed blessing: for
2640 example, the expression above will never trap, hence
2641 optimizing it to x < y would be invalid). */
2642 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2643 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2646 /* If the comparison was short-circuited, and only the RHS
2647 trapped, we may now generate a spurious trap. */
2649 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2652 /* If we changed the conditions that cause a trap, we lose. */
2653 if ((ltrap || rtrap) != trap)
2657 if (compcode == COMPCODE_TRUE)
2658 return constant_boolean_node (true, truth_type);
2659 else if (compcode == COMPCODE_FALSE)
2660 return constant_boolean_node (false, truth_type);
2663 enum tree_code tcode;
2665 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2666 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2670 /* Return nonzero if two operands (typically of the same tree node)
2671 are necessarily equal. If either argument has side-effects this
2672 function returns zero. FLAGS modifies behavior as follows:
2674 If OEP_ONLY_CONST is set, only return nonzero for constants.
2675 This function tests whether the operands are indistinguishable;
2676 it does not test whether they are equal using C's == operation.
2677 The distinction is important for IEEE floating point, because
2678 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2679 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2681 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2682 even though it may hold multiple values during a function.
2683 This is because a GCC tree node guarantees that nothing else is
2684 executed between the evaluation of its "operands" (which may often
2685 be evaluated in arbitrary order). Hence if the operands themselves
2686 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2687 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2688 unset means assuming isochronic (or instantaneous) tree equivalence.
2689 Unless comparing arbitrary expression trees, such as from different
2690 statements, this flag can usually be left unset.
2692 If OEP_PURE_SAME is set, then pure functions with identical arguments
2693 are considered the same. It is used when the caller has other ways
2694 to ensure that global memory is unchanged in between. */
2697 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2699 /* If either is ERROR_MARK, they aren't equal. */
2700 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2701 || TREE_TYPE (arg0) == error_mark_node
2702 || TREE_TYPE (arg1) == error_mark_node)
2705 /* Similar, if either does not have a type (like a released SSA name),
2706 they aren't equal. */
2707 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2710 /* Check equality of integer constants before bailing out due to
2711 precision differences. */
2712 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2713 return tree_int_cst_equal (arg0, arg1);
2715 /* If both types don't have the same signedness, then we can't consider
2716 them equal. We must check this before the STRIP_NOPS calls
2717 because they may change the signedness of the arguments. As pointers
2718 strictly don't have a signedness, require either two pointers or
2719 two non-pointers as well. */
2720 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2721 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2724 /* We cannot consider pointers to different address space equal. */
2725 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2726 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2727 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2730 /* If both types don't have the same precision, then it is not safe
2732 if (element_precision (TREE_TYPE (arg0))
2733 != element_precision (TREE_TYPE (arg1)))
2739 /* In case both args are comparisons but with different comparison
2740 code, try to swap the comparison operands of one arg to produce
2741 a match and compare that variant. */
2742 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2743 && COMPARISON_CLASS_P (arg0)
2744 && COMPARISON_CLASS_P (arg1))
2746 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2748 if (TREE_CODE (arg0) == swap_code)
2749 return operand_equal_p (TREE_OPERAND (arg0, 0),
2750 TREE_OPERAND (arg1, 1), flags)
2751 && operand_equal_p (TREE_OPERAND (arg0, 1),
2752 TREE_OPERAND (arg1, 0), flags);
2755 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2756 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2757 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2760 /* This is needed for conversions and for COMPONENT_REF.
2761 Might as well play it safe and always test this. */
2762 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2763 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2764 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2767 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2768 We don't care about side effects in that case because the SAVE_EXPR
2769 takes care of that for us. In all other cases, two expressions are
2770 equal if they have no side effects. If we have two identical
2771 expressions with side effects that should be treated the same due
2772 to the only side effects being identical SAVE_EXPR's, that will
2773 be detected in the recursive calls below.
2774 If we are taking an invariant address of two identical objects
2775 they are necessarily equal as well. */
2776 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2777 && (TREE_CODE (arg0) == SAVE_EXPR
2778 || (flags & OEP_CONSTANT_ADDRESS_OF)
2779 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2782 /* Next handle constant cases, those for which we can return 1 even
2783 if ONLY_CONST is set. */
2784 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2785 switch (TREE_CODE (arg0))
2788 return tree_int_cst_equal (arg0, arg1);
2791 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2792 TREE_FIXED_CST (arg1));
2795 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2796 TREE_REAL_CST (arg1)))
2800 if (!HONOR_SIGNED_ZEROS (arg0))
2802 /* If we do not distinguish between signed and unsigned zero,
2803 consider them equal. */
2804 if (real_zerop (arg0) && real_zerop (arg1))
2813 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2816 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2818 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2819 VECTOR_CST_ELT (arg1, i), flags))
2826 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2828 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2832 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2833 && ! memcmp (TREE_STRING_POINTER (arg0),
2834 TREE_STRING_POINTER (arg1),
2835 TREE_STRING_LENGTH (arg0)));
2838 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2839 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2840 ? OEP_CONSTANT_ADDRESS_OF | OEP_ADDRESS_OF : 0);
2845 if (flags & OEP_ONLY_CONST)
2848 /* Define macros to test an operand from arg0 and arg1 for equality and a
2849 variant that allows null and views null as being different from any
2850 non-null value. In the latter case, if either is null, the both
2851 must be; otherwise, do the normal comparison. */
2852 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2853 TREE_OPERAND (arg1, N), flags)
2855 #define OP_SAME_WITH_NULL(N) \
2856 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2857 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2859 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2862 /* Two conversions are equal only if signedness and modes match. */
2863 switch (TREE_CODE (arg0))
2866 case FIX_TRUNC_EXPR:
2867 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2868 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2878 case tcc_comparison:
2880 if (OP_SAME (0) && OP_SAME (1))
2883 /* For commutative ops, allow the other order. */
2884 return (commutative_tree_code (TREE_CODE (arg0))
2885 && operand_equal_p (TREE_OPERAND (arg0, 0),
2886 TREE_OPERAND (arg1, 1), flags)
2887 && operand_equal_p (TREE_OPERAND (arg0, 1),
2888 TREE_OPERAND (arg1, 0), flags));
2891 /* If either of the pointer (or reference) expressions we are
2892 dereferencing contain a side effect, these cannot be equal,
2893 but their addresses can be. */
2894 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2895 && (TREE_SIDE_EFFECTS (arg0)
2896 || TREE_SIDE_EFFECTS (arg1)))
2899 switch (TREE_CODE (arg0))
2902 if (!(flags & OEP_ADDRESS_OF)
2903 && (TYPE_ALIGN (TREE_TYPE (arg0))
2904 != TYPE_ALIGN (TREE_TYPE (arg1))))
2906 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2913 case TARGET_MEM_REF:
2915 /* Require equal access sizes, and similar pointer types.
2916 We can have incomplete types for array references of
2917 variable-sized arrays from the Fortran frontend
2918 though. Also verify the types are compatible. */
2919 if (!((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2920 || (TYPE_SIZE (TREE_TYPE (arg0))
2921 && TYPE_SIZE (TREE_TYPE (arg1))
2922 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2923 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2924 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2925 && ((flags & OEP_ADDRESS_OF)
2926 || (alias_ptr_types_compatible_p
2927 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2928 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2929 && (MR_DEPENDENCE_CLIQUE (arg0)
2930 == MR_DEPENDENCE_CLIQUE (arg1))
2931 && (MR_DEPENDENCE_BASE (arg0)
2932 == MR_DEPENDENCE_BASE (arg1))
2933 && (TYPE_ALIGN (TREE_TYPE (arg0))
2934 == TYPE_ALIGN (TREE_TYPE (arg1)))))))
2936 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2937 return (OP_SAME (0) && OP_SAME (1)
2938 /* TARGET_MEM_REF require equal extra operands. */
2939 && (TREE_CODE (arg0) != TARGET_MEM_REF
2940 || (OP_SAME_WITH_NULL (2)
2941 && OP_SAME_WITH_NULL (3)
2942 && OP_SAME_WITH_NULL (4))));
2945 case ARRAY_RANGE_REF:
2946 /* Operands 2 and 3 may be null.
2947 Compare the array index by value if it is constant first as we
2948 may have different types but same value here. */
2951 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2952 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2953 TREE_OPERAND (arg1, 1))
2955 && OP_SAME_WITH_NULL (2)
2956 && OP_SAME_WITH_NULL (3));
2959 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2960 may be NULL when we're called to compare MEM_EXPRs. */
2961 if (!OP_SAME_WITH_NULL (0)
2964 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2965 return OP_SAME_WITH_NULL (2);
2970 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2971 return OP_SAME (1) && OP_SAME (2);
2977 case tcc_expression:
2978 switch (TREE_CODE (arg0))
2981 return operand_equal_p (TREE_OPERAND (arg0, 0),
2982 TREE_OPERAND (arg1, 0),
2983 flags | OEP_ADDRESS_OF);
2985 case TRUTH_NOT_EXPR:
2988 case TRUTH_ANDIF_EXPR:
2989 case TRUTH_ORIF_EXPR:
2990 return OP_SAME (0) && OP_SAME (1);
2993 case WIDEN_MULT_PLUS_EXPR:
2994 case WIDEN_MULT_MINUS_EXPR:
2997 /* The multiplcation operands are commutative. */
3000 case TRUTH_AND_EXPR:
3002 case TRUTH_XOR_EXPR:
3003 if (OP_SAME (0) && OP_SAME (1))
3006 /* Otherwise take into account this is a commutative operation. */
3007 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3008 TREE_OPERAND (arg1, 1), flags)
3009 && operand_equal_p (TREE_OPERAND (arg0, 1),
3010 TREE_OPERAND (arg1, 0), flags));
3015 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3022 switch (TREE_CODE (arg0))
3025 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3026 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3027 /* If not both CALL_EXPRs are either internal or normal function
3028 functions, then they are not equal. */
3030 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3032 /* If the CALL_EXPRs call different internal functions, then they
3034 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3039 /* If the CALL_EXPRs call different functions, then they are not
3041 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3047 unsigned int cef = call_expr_flags (arg0);
3048 if (flags & OEP_PURE_SAME)
3049 cef &= ECF_CONST | ECF_PURE;
3056 /* Now see if all the arguments are the same. */
3058 const_call_expr_arg_iterator iter0, iter1;
3060 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3061 a1 = first_const_call_expr_arg (arg1, &iter1);
3063 a0 = next_const_call_expr_arg (&iter0),
3064 a1 = next_const_call_expr_arg (&iter1))
3065 if (! operand_equal_p (a0, a1, flags))
3068 /* If we get here and both argument lists are exhausted
3069 then the CALL_EXPRs are equal. */
3070 return ! (a0 || a1);
3076 case tcc_declaration:
3077 /* Consider __builtin_sqrt equal to sqrt. */
3078 return (TREE_CODE (arg0) == FUNCTION_DECL
3079 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3080 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3081 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3088 #undef OP_SAME_WITH_NULL
3091 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3092 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3094 When in doubt, return 0. */
3097 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3099 int unsignedp1, unsignedpo;
3100 tree primarg0, primarg1, primother;
3101 unsigned int correct_width;
3103 if (operand_equal_p (arg0, arg1, 0))
3106 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3107 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3110 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3111 and see if the inner values are the same. This removes any
3112 signedness comparison, which doesn't matter here. */
3113 primarg0 = arg0, primarg1 = arg1;
3114 STRIP_NOPS (primarg0);
3115 STRIP_NOPS (primarg1);
3116 if (operand_equal_p (primarg0, primarg1, 0))
3119 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3120 actual comparison operand, ARG0.
3122 First throw away any conversions to wider types
3123 already present in the operands. */
3125 primarg1 = get_narrower (arg1, &unsignedp1);
3126 primother = get_narrower (other, &unsignedpo);
3128 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3129 if (unsignedp1 == unsignedpo
3130 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3131 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3133 tree type = TREE_TYPE (arg0);
3135 /* Make sure shorter operand is extended the right way
3136 to match the longer operand. */
3137 primarg1 = fold_convert (signed_or_unsigned_type_for
3138 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3140 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3147 /* See if ARG is an expression that is either a comparison or is performing
3148 arithmetic on comparisons. The comparisons must only be comparing
3149 two different values, which will be stored in *CVAL1 and *CVAL2; if
3150 they are nonzero it means that some operands have already been found.
3151 No variables may be used anywhere else in the expression except in the
3152 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3153 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3155 If this is true, return 1. Otherwise, return zero. */
3158 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3160 enum tree_code code = TREE_CODE (arg);
3161 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3163 /* We can handle some of the tcc_expression cases here. */
3164 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3166 else if (tclass == tcc_expression
3167 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3168 || code == COMPOUND_EXPR))
3169 tclass = tcc_binary;
3171 else if (tclass == tcc_expression && code == SAVE_EXPR
3172 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3174 /* If we've already found a CVAL1 or CVAL2, this expression is
3175 two complex to handle. */
3176 if (*cval1 || *cval2)
3186 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3189 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3190 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3191 cval1, cval2, save_p));
3196 case tcc_expression:
3197 if (code == COND_EXPR)
3198 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3199 cval1, cval2, save_p)
3200 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3201 cval1, cval2, save_p)
3202 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3203 cval1, cval2, save_p));
3206 case tcc_comparison:
3207 /* First see if we can handle the first operand, then the second. For
3208 the second operand, we know *CVAL1 can't be zero. It must be that
3209 one side of the comparison is each of the values; test for the
3210 case where this isn't true by failing if the two operands
3213 if (operand_equal_p (TREE_OPERAND (arg, 0),
3214 TREE_OPERAND (arg, 1), 0))
3218 *cval1 = TREE_OPERAND (arg, 0);
3219 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3221 else if (*cval2 == 0)
3222 *cval2 = TREE_OPERAND (arg, 0);
3223 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3228 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3230 else if (*cval2 == 0)
3231 *cval2 = TREE_OPERAND (arg, 1);
3232 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3244 /* ARG is a tree that is known to contain just arithmetic operations and
3245 comparisons. Evaluate the operations in the tree substituting NEW0 for
3246 any occurrence of OLD0 as an operand of a comparison and likewise for
3250 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3251 tree old1, tree new1)
3253 tree type = TREE_TYPE (arg);
3254 enum tree_code code = TREE_CODE (arg);
3255 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3257 /* We can handle some of the tcc_expression cases here. */
3258 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3260 else if (tclass == tcc_expression
3261 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3262 tclass = tcc_binary;
3267 return fold_build1_loc (loc, code, type,
3268 eval_subst (loc, TREE_OPERAND (arg, 0),
3269 old0, new0, old1, new1));
3272 return fold_build2_loc (loc, code, type,
3273 eval_subst (loc, TREE_OPERAND (arg, 0),
3274 old0, new0, old1, new1),
3275 eval_subst (loc, TREE_OPERAND (arg, 1),
3276 old0, new0, old1, new1));
3278 case tcc_expression:
3282 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3286 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3290 return fold_build3_loc (loc, code, type,
3291 eval_subst (loc, TREE_OPERAND (arg, 0),
3292 old0, new0, old1, new1),
3293 eval_subst (loc, TREE_OPERAND (arg, 1),
3294 old0, new0, old1, new1),
3295 eval_subst (loc, TREE_OPERAND (arg, 2),
3296 old0, new0, old1, new1));
3300 /* Fall through - ??? */
3302 case tcc_comparison:
3304 tree arg0 = TREE_OPERAND (arg, 0);
3305 tree arg1 = TREE_OPERAND (arg, 1);
3307 /* We need to check both for exact equality and tree equality. The
3308 former will be true if the operand has a side-effect. In that
3309 case, we know the operand occurred exactly once. */
3311 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3313 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3316 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3318 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3321 return fold_build2_loc (loc, code, type, arg0, arg1);
3329 /* Return a tree for the case when the result of an expression is RESULT
3330 converted to TYPE and OMITTED was previously an operand of the expression
3331 but is now not needed (e.g., we folded OMITTED * 0).
3333 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3334 the conversion of RESULT to TYPE. */
3337 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3339 tree t = fold_convert_loc (loc, type, result);
3341 /* If the resulting operand is an empty statement, just return the omitted
3342 statement casted to void. */
3343 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3344 return build1_loc (loc, NOP_EXPR, void_type_node,
3345 fold_ignored_result (omitted));
3347 if (TREE_SIDE_EFFECTS (omitted))
3348 return build2_loc (loc, COMPOUND_EXPR, type,
3349 fold_ignored_result (omitted), t);
3351 return non_lvalue_loc (loc, t);
3354 /* Return a tree for the case when the result of an expression is RESULT
3355 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3356 of the expression but are now not needed.
3358 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3359 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3360 evaluated before OMITTED2. Otherwise, if neither has side effects,
3361 just do the conversion of RESULT to TYPE. */
3364 omit_two_operands_loc (location_t loc, tree type, tree result,
3365 tree omitted1, tree omitted2)
3367 tree t = fold_convert_loc (loc, type, result);
3369 if (TREE_SIDE_EFFECTS (omitted2))
3370 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3371 if (TREE_SIDE_EFFECTS (omitted1))
3372 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3374 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3378 /* Return a simplified tree node for the truth-negation of ARG. This
3379 never alters ARG itself. We assume that ARG is an operation that
3380 returns a truth value (0 or 1).
3382 FIXME: one would think we would fold the result, but it causes
3383 problems with the dominator optimizer. */
3386 fold_truth_not_expr (location_t loc, tree arg)
3388 tree type = TREE_TYPE (arg);
3389 enum tree_code code = TREE_CODE (arg);
3390 location_t loc1, loc2;
3392 /* If this is a comparison, we can simply invert it, except for
3393 floating-point non-equality comparisons, in which case we just
3394 enclose a TRUTH_NOT_EXPR around what we have. */
3396 if (TREE_CODE_CLASS (code) == tcc_comparison)
3398 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3399 if (FLOAT_TYPE_P (op_type)
3400 && flag_trapping_math
3401 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3402 && code != NE_EXPR && code != EQ_EXPR)
3405 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3406 if (code == ERROR_MARK)
3409 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3410 TREE_OPERAND (arg, 1));
3416 return constant_boolean_node (integer_zerop (arg), type);
3418 case TRUTH_AND_EXPR:
3419 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3420 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3421 return build2_loc (loc, TRUTH_OR_EXPR, type,
3422 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3423 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3426 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3427 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3428 return build2_loc (loc, TRUTH_AND_EXPR, type,
3429 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3430 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3432 case TRUTH_XOR_EXPR:
3433 /* Here we can invert either operand. We invert the first operand
3434 unless the second operand is a TRUTH_NOT_EXPR in which case our
3435 result is the XOR of the first operand with the inside of the
3436 negation of the second operand. */
3438 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3439 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3440 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3442 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3443 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3444 TREE_OPERAND (arg, 1));
3446 case TRUTH_ANDIF_EXPR:
3447 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3448 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3449 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3450 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3451 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3453 case TRUTH_ORIF_EXPR:
3454 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3455 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3456 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3457 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3458 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3460 case TRUTH_NOT_EXPR:
3461 return TREE_OPERAND (arg, 0);
3465 tree arg1 = TREE_OPERAND (arg, 1);
3466 tree arg2 = TREE_OPERAND (arg, 2);
3468 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3469 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3471 /* A COND_EXPR may have a throw as one operand, which
3472 then has void type. Just leave void operands
3474 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3475 VOID_TYPE_P (TREE_TYPE (arg1))
3476 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3477 VOID_TYPE_P (TREE_TYPE (arg2))
3478 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3482 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3483 return build2_loc (loc, COMPOUND_EXPR, type,
3484 TREE_OPERAND (arg, 0),
3485 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3487 case NON_LVALUE_EXPR:
3488 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3489 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3492 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3493 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3495 /* ... fall through ... */
3498 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3499 return build1_loc (loc, TREE_CODE (arg), type,
3500 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3503 if (!integer_onep (TREE_OPERAND (arg, 1)))
3505 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3508 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3510 case CLEANUP_POINT_EXPR:
3511 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3512 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3513 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3520 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3521 assume that ARG is an operation that returns a truth value (0 or 1
3522 for scalars, 0 or -1 for vectors). Return the folded expression if
3523 folding is successful. Otherwise, return NULL_TREE. */
3526 fold_invert_truthvalue (location_t loc, tree arg)
3528 tree type = TREE_TYPE (arg);
3529 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3535 /* Return a simplified tree node for the truth-negation of ARG. This
3536 never alters ARG itself. We assume that ARG is an operation that
3537 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3540 invert_truthvalue_loc (location_t loc, tree arg)
3542 if (TREE_CODE (arg) == ERROR_MARK)
3545 tree type = TREE_TYPE (arg);
3546 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3552 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3553 with code CODE. This optimization is unsafe. */
3555 distribute_real_division (location_t loc, enum tree_code code, tree type,
3556 tree arg0, tree arg1)
3558 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3559 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3561 /* (A / C) +- (B / C) -> (A +- B) / C. */
3563 && operand_equal_p (TREE_OPERAND (arg0, 1),
3564 TREE_OPERAND (arg1, 1), 0))
3565 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3566 fold_build2_loc (loc, code, type,
3567 TREE_OPERAND (arg0, 0),
3568 TREE_OPERAND (arg1, 0)),
3569 TREE_OPERAND (arg0, 1));
3571 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3572 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3573 TREE_OPERAND (arg1, 0), 0)
3574 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3575 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3577 REAL_VALUE_TYPE r0, r1;
3578 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3579 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3581 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3583 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3584 real_arithmetic (&r0, code, &r0, &r1);
3585 return fold_build2_loc (loc, MULT_EXPR, type,
3586 TREE_OPERAND (arg0, 0),
3587 build_real (type, r0));
3593 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3594 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3597 make_bit_field_ref (location_t loc, tree inner, tree type,
3598 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3600 tree result, bftype;
3604 tree size = TYPE_SIZE (TREE_TYPE (inner));
3605 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3606 || POINTER_TYPE_P (TREE_TYPE (inner)))
3607 && tree_fits_shwi_p (size)
3608 && tree_to_shwi (size) == bitsize)
3609 return fold_convert_loc (loc, type, inner);
3613 if (TYPE_PRECISION (bftype) != bitsize
3614 || TYPE_UNSIGNED (bftype) == !unsignedp)
3615 bftype = build_nonstandard_integer_type (bitsize, 0);
3617 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3618 size_int (bitsize), bitsize_int (bitpos));
3621 result = fold_convert_loc (loc, type, result);
3626 /* Optimize a bit-field compare.
3628 There are two cases: First is a compare against a constant and the
3629 second is a comparison of two items where the fields are at the same
3630 bit position relative to the start of a chunk (byte, halfword, word)
3631 large enough to contain it. In these cases we can avoid the shift
3632 implicit in bitfield extractions.
3634 For constants, we emit a compare of the shifted constant with the
3635 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3636 compared. For two fields at the same position, we do the ANDs with the
3637 similar mask and compare the result of the ANDs.
3639 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3640 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3641 are the left and right operands of the comparison, respectively.
3643 If the optimization described above can be done, we return the resulting
3644 tree. Otherwise we return zero. */
3647 optimize_bit_field_compare (location_t loc, enum tree_code code,
3648 tree compare_type, tree lhs, tree rhs)
3650 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3651 tree type = TREE_TYPE (lhs);
3653 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3654 machine_mode lmode, rmode, nmode;
3655 int lunsignedp, runsignedp;
3656 int lvolatilep = 0, rvolatilep = 0;
3657 tree linner, rinner = NULL_TREE;
3661 /* Get all the information about the extractions being done. If the bit size
3662 if the same as the size of the underlying object, we aren't doing an
3663 extraction at all and so can do nothing. We also don't want to
3664 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3665 then will no longer be able to replace it. */
3666 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3667 &lunsignedp, &lvolatilep, false);
3668 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3669 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3674 /* If this is not a constant, we can only do something if bit positions,
3675 sizes, and signedness are the same. */
3676 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3677 &runsignedp, &rvolatilep, false);
3679 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3680 || lunsignedp != runsignedp || offset != 0
3681 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3685 /* See if we can find a mode to refer to this field. We should be able to,
3686 but fail if we can't. */
3687 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3688 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3689 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3690 TYPE_ALIGN (TREE_TYPE (rinner))),
3692 if (nmode == VOIDmode)
3695 /* Set signed and unsigned types of the precision of this mode for the
3697 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3699 /* Compute the bit position and size for the new reference and our offset
3700 within it. If the new reference is the same size as the original, we
3701 won't optimize anything, so return zero. */
3702 nbitsize = GET_MODE_BITSIZE (nmode);
3703 nbitpos = lbitpos & ~ (nbitsize - 1);
3705 if (nbitsize == lbitsize)
3708 if (BYTES_BIG_ENDIAN)
3709 lbitpos = nbitsize - lbitsize - lbitpos;
3711 /* Make the mask to be used against the extracted field. */
3712 mask = build_int_cst_type (unsigned_type, -1);
3713 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3714 mask = const_binop (RSHIFT_EXPR, mask,
3715 size_int (nbitsize - lbitsize - lbitpos));
3718 /* If not comparing with constant, just rework the comparison
3720 return fold_build2_loc (loc, code, compare_type,
3721 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3722 make_bit_field_ref (loc, linner,
3727 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3728 make_bit_field_ref (loc, rinner,
3734 /* Otherwise, we are handling the constant case. See if the constant is too
3735 big for the field. Warn and return a tree of for 0 (false) if so. We do
3736 this not only for its own sake, but to avoid having to test for this
3737 error case below. If we didn't, we might generate wrong code.
3739 For unsigned fields, the constant shifted right by the field length should
3740 be all zero. For signed fields, the high-order bits should agree with
3745 if (wi::lrshift (rhs, lbitsize) != 0)
3747 warning (0, "comparison is always %d due to width of bit-field",
3749 return constant_boolean_node (code == NE_EXPR, compare_type);
3754 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3755 if (tem != 0 && tem != -1)
3757 warning (0, "comparison is always %d due to width of bit-field",
3759 return constant_boolean_node (code == NE_EXPR, compare_type);
3763 /* Single-bit compares should always be against zero. */
3764 if (lbitsize == 1 && ! integer_zerop (rhs))
3766 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3767 rhs = build_int_cst (type, 0);
3770 /* Make a new bitfield reference, shift the constant over the
3771 appropriate number of bits and mask it with the computed mask
3772 (in case this was a signed field). If we changed it, make a new one. */
3773 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3775 rhs = const_binop (BIT_AND_EXPR,
3776 const_binop (LSHIFT_EXPR,
3777 fold_convert_loc (loc, unsigned_type, rhs),
3778 size_int (lbitpos)),
3781 lhs = build2_loc (loc, code, compare_type,
3782 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3786 /* Subroutine for fold_truth_andor_1: decode a field reference.
3788 If EXP is a comparison reference, we return the innermost reference.
3790 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3791 set to the starting bit number.
3793 If the innermost field can be completely contained in a mode-sized
3794 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3796 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3797 otherwise it is not changed.
3799 *PUNSIGNEDP is set to the signedness of the field.
3801 *PMASK is set to the mask used. This is either contained in a
3802 BIT_AND_EXPR or derived from the width of the field.
3804 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3806 Return 0 if this is not a component reference or is one that we can't
3807 do anything with. */
3810 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3811 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3812 int *punsignedp, int *pvolatilep,
3813 tree *pmask, tree *pand_mask)
3815 tree outer_type = 0;
3817 tree mask, inner, offset;
3819 unsigned int precision;
3821 /* All the optimizations using this function assume integer fields.
3822 There are problems with FP fields since the type_for_size call
3823 below can fail for, e.g., XFmode. */
3824 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3827 /* We are interested in the bare arrangement of bits, so strip everything
3828 that doesn't affect the machine mode. However, record the type of the
3829 outermost expression if it may matter below. */
3830 if (CONVERT_EXPR_P (exp)
3831 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3832 outer_type = TREE_TYPE (exp);
3835 if (TREE_CODE (exp) == BIT_AND_EXPR)
3837 and_mask = TREE_OPERAND (exp, 1);
3838 exp = TREE_OPERAND (exp, 0);
3839 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3840 if (TREE_CODE (and_mask) != INTEGER_CST)
3844 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3845 punsignedp, pvolatilep, false);
3846 if ((inner == exp && and_mask == 0)
3847 || *pbitsize < 0 || offset != 0
3848 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3851 /* If the number of bits in the reference is the same as the bitsize of
3852 the outer type, then the outer type gives the signedness. Otherwise
3853 (in case of a small bitfield) the signedness is unchanged. */
3854 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3855 *punsignedp = TYPE_UNSIGNED (outer_type);
3857 /* Compute the mask to access the bitfield. */
3858 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3859 precision = TYPE_PRECISION (unsigned_type);
3861 mask = build_int_cst_type (unsigned_type, -1);
3863 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3864 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3866 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3868 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3869 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3872 *pand_mask = and_mask;
3876 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3877 bit positions and MASK is SIGNED. */
3880 all_ones_mask_p (const_tree mask, unsigned int size)
3882 tree type = TREE_TYPE (mask);
3883 unsigned int precision = TYPE_PRECISION (type);
3885 /* If this function returns true when the type of the mask is
3886 UNSIGNED, then there will be errors. In particular see
3887 gcc.c-torture/execute/990326-1.c. There does not appear to be
3888 any documentation paper trail as to why this is so. But the pre
3889 wide-int worked with that restriction and it has been preserved
3891 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3894 return wi::mask (size, false, precision) == mask;
3897 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3898 represents the sign bit of EXP's type. If EXP represents a sign
3899 or zero extension, also test VAL against the unextended type.
3900 The return value is the (sub)expression whose sign bit is VAL,
3901 or NULL_TREE otherwise. */
3904 sign_bit_p (tree exp, const_tree val)
3909 /* Tree EXP must have an integral type. */
3910 t = TREE_TYPE (exp);
3911 if (! INTEGRAL_TYPE_P (t))
3914 /* Tree VAL must be an integer constant. */
3915 if (TREE_CODE (val) != INTEGER_CST
3916 || TREE_OVERFLOW (val))
3919 width = TYPE_PRECISION (t);
3920 if (wi::only_sign_bit_p (val, width))
3923 /* Handle extension from a narrower type. */
3924 if (TREE_CODE (exp) == NOP_EXPR
3925 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3926 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3931 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3932 to be evaluated unconditionally. */
3935 simple_operand_p (const_tree exp)
3937 /* Strip any conversions that don't change the machine mode. */
3940 return (CONSTANT_CLASS_P (exp)
3941 || TREE_CODE (exp) == SSA_NAME
3943 && ! TREE_ADDRESSABLE (exp)
3944 && ! TREE_THIS_VOLATILE (exp)
3945 && ! DECL_NONLOCAL (exp)
3946 /* Don't regard global variables as simple. They may be
3947 allocated in ways unknown to the compiler (shared memory,
3948 #pragma weak, etc). */
3949 && ! TREE_PUBLIC (exp)
3950 && ! DECL_EXTERNAL (exp)
3951 /* Weakrefs are not safe to be read, since they can be NULL.
3952 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3953 have DECL_WEAK flag set. */
3954 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
3955 /* Loading a static variable is unduly expensive, but global
3956 registers aren't expensive. */
3957 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3960 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3961 to be evaluated unconditionally.
3962 I addition to simple_operand_p, we assume that comparisons, conversions,
3963 and logic-not operations are simple, if their operands are simple, too. */
3966 simple_operand_p_2 (tree exp)
3968 enum tree_code code;
3970 if (TREE_SIDE_EFFECTS (exp)
3971 || tree_could_trap_p (exp))
3974 while (CONVERT_EXPR_P (exp))
3975 exp = TREE_OPERAND (exp, 0);
3977 code = TREE_CODE (exp);
3979 if (TREE_CODE_CLASS (code) == tcc_comparison)
3980 return (simple_operand_p (TREE_OPERAND (exp, 0))
3981 && simple_operand_p (TREE_OPERAND (exp, 1)));
3983 if (code == TRUTH_NOT_EXPR)
3984 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3986 return simple_operand_p (exp);
3990 /* The following functions are subroutines to fold_range_test and allow it to
3991 try to change a logical combination of comparisons into a range test.
3994 X == 2 || X == 3 || X == 4 || X == 5
3998 (unsigned) (X - 2) <= 3
4000 We describe each set of comparisons as being either inside or outside
4001 a range, using a variable named like IN_P, and then describe the
4002 range with a lower and upper bound. If one of the bounds is omitted,
4003 it represents either the highest or lowest value of the type.
4005 In the comments below, we represent a range by two numbers in brackets
4006 preceded by a "+" to designate being inside that range, or a "-" to
4007 designate being outside that range, so the condition can be inverted by
4008 flipping the prefix. An omitted bound is represented by a "-". For
4009 example, "- [-, 10]" means being outside the range starting at the lowest
4010 possible value and ending at 10, in other words, being greater than 10.
4011 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4014 We set up things so that the missing bounds are handled in a consistent
4015 manner so neither a missing bound nor "true" and "false" need to be
4016 handled using a special case. */
4018 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4019 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4020 and UPPER1_P are nonzero if the respective argument is an upper bound
4021 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4022 must be specified for a comparison. ARG1 will be converted to ARG0's
4023 type if both are specified. */
4026 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4027 tree arg1, int upper1_p)
4033 /* If neither arg represents infinity, do the normal operation.
4034 Else, if not a comparison, return infinity. Else handle the special
4035 comparison rules. Note that most of the cases below won't occur, but
4036 are handled for consistency. */
4038 if (arg0 != 0 && arg1 != 0)
4040 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4041 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4043 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4046 if (TREE_CODE_CLASS (code) != tcc_comparison)
4049 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4050 for neither. In real maths, we cannot assume open ended ranges are
4051 the same. But, this is computer arithmetic, where numbers are finite.
4052 We can therefore make the transformation of any unbounded range with
4053 the value Z, Z being greater than any representable number. This permits
4054 us to treat unbounded ranges as equal. */
4055 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4056 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4060 result = sgn0 == sgn1;
4063 result = sgn0 != sgn1;
4066 result = sgn0 < sgn1;
4069 result = sgn0 <= sgn1;
4072 result = sgn0 > sgn1;
4075 result = sgn0 >= sgn1;
4081 return constant_boolean_node (result, type);
4084 /* Helper routine for make_range. Perform one step for it, return
4085 new expression if the loop should continue or NULL_TREE if it should
4089 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4090 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4091 bool *strict_overflow_p)
4093 tree arg0_type = TREE_TYPE (arg0);
4094 tree n_low, n_high, low = *p_low, high = *p_high;
4095 int in_p = *p_in_p, n_in_p;
4099 case TRUTH_NOT_EXPR:
4100 /* We can only do something if the range is testing for zero. */
4101 if (low == NULL_TREE || high == NULL_TREE
4102 || ! integer_zerop (low) || ! integer_zerop (high))
4107 case EQ_EXPR: case NE_EXPR:
4108 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4109 /* We can only do something if the range is testing for zero
4110 and if the second operand is an integer constant. Note that
4111 saying something is "in" the range we make is done by
4112 complementing IN_P since it will set in the initial case of
4113 being not equal to zero; "out" is leaving it alone. */
4114 if (low == NULL_TREE || high == NULL_TREE
4115 || ! integer_zerop (low) || ! integer_zerop (high)
4116 || TREE_CODE (arg1) != INTEGER_CST)
4121 case NE_EXPR: /* - [c, c] */
4124 case EQ_EXPR: /* + [c, c] */
4125 in_p = ! in_p, low = high = arg1;
4127 case GT_EXPR: /* - [-, c] */
4128 low = 0, high = arg1;
4130 case GE_EXPR: /* + [c, -] */
4131 in_p = ! in_p, low = arg1, high = 0;
4133 case LT_EXPR: /* - [c, -] */
4134 low = arg1, high = 0;
4136 case LE_EXPR: /* + [-, c] */
4137 in_p = ! in_p, low = 0, high = arg1;
4143 /* If this is an unsigned comparison, we also know that EXP is
4144 greater than or equal to zero. We base the range tests we make
4145 on that fact, so we record it here so we can parse existing
4146 range tests. We test arg0_type since often the return type
4147 of, e.g. EQ_EXPR, is boolean. */
4148 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4150 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4152 build_int_cst (arg0_type, 0),
4156 in_p = n_in_p, low = n_low, high = n_high;
4158 /* If the high bound is missing, but we have a nonzero low
4159 bound, reverse the range so it goes from zero to the low bound
4161 if (high == 0 && low && ! integer_zerop (low))
4164 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4165 build_int_cst (TREE_TYPE (low), 1), 0);
4166 low = build_int_cst (arg0_type, 0);
4176 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4177 low and high are non-NULL, then normalize will DTRT. */
4178 if (!TYPE_UNSIGNED (arg0_type)
4179 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4181 if (low == NULL_TREE)
4182 low = TYPE_MIN_VALUE (arg0_type);
4183 if (high == NULL_TREE)
4184 high = TYPE_MAX_VALUE (arg0_type);
4187 /* (-x) IN [a,b] -> x in [-b, -a] */
4188 n_low = range_binop (MINUS_EXPR, exp_type,
4189 build_int_cst (exp_type, 0),
4191 n_high = range_binop (MINUS_EXPR, exp_type,
4192 build_int_cst (exp_type, 0),
4194 if (n_high != 0 && TREE_OVERFLOW (n_high))
4200 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4201 build_int_cst (exp_type, 1));
4205 if (TREE_CODE (arg1) != INTEGER_CST)
4208 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4209 move a constant to the other side. */
4210 if (!TYPE_UNSIGNED (arg0_type)
4211 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4214 /* If EXP is signed, any overflow in the computation is undefined,
4215 so we don't worry about it so long as our computations on
4216 the bounds don't overflow. For unsigned, overflow is defined
4217 and this is exactly the right thing. */
4218 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4219 arg0_type, low, 0, arg1, 0);
4220 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4221 arg0_type, high, 1, arg1, 0);
4222 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4223 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4226 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4227 *strict_overflow_p = true;
4230 /* Check for an unsigned range which has wrapped around the maximum
4231 value thus making n_high < n_low, and normalize it. */
4232 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4234 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4235 build_int_cst (TREE_TYPE (n_high), 1), 0);
4236 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4237 build_int_cst (TREE_TYPE (n_low), 1), 0);
4239 /* If the range is of the form +/- [ x+1, x ], we won't
4240 be able to normalize it. But then, it represents the
4241 whole range or the empty set, so make it
4243 if (tree_int_cst_equal (n_low, low)
4244 && tree_int_cst_equal (n_high, high))
4250 low = n_low, high = n_high;
4258 case NON_LVALUE_EXPR:
4259 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4262 if (! INTEGRAL_TYPE_P (arg0_type)
4263 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4264 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4267 n_low = low, n_high = high;
4270 n_low = fold_convert_loc (loc, arg0_type, n_low);
4273 n_high = fold_convert_loc (loc, arg0_type, n_high);
4275 /* If we're converting arg0 from an unsigned type, to exp,
4276 a signed type, we will be doing the comparison as unsigned.
4277 The tests above have already verified that LOW and HIGH
4280 So we have to ensure that we will handle large unsigned
4281 values the same way that the current signed bounds treat
4284 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4288 /* For fixed-point modes, we need to pass the saturating flag
4289 as the 2nd parameter. */
4290 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4292 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4293 TYPE_SATURATING (arg0_type));
4296 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4298 /* A range without an upper bound is, naturally, unbounded.
4299 Since convert would have cropped a very large value, use
4300 the max value for the destination type. */
4302 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4303 : TYPE_MAX_VALUE (arg0_type);
4305 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4306 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4307 fold_convert_loc (loc, arg0_type,
4309 build_int_cst (arg0_type, 1));
4311 /* If the low bound is specified, "and" the range with the
4312 range for which the original unsigned value will be
4316 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4317 1, fold_convert_loc (loc, arg0_type,
4322 in_p = (n_in_p == in_p);
4326 /* Otherwise, "or" the range with the range of the input
4327 that will be interpreted as negative. */
4328 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4329 1, fold_convert_loc (loc, arg0_type,
4334 in_p = (in_p != n_in_p);
4348 /* Given EXP, a logical expression, set the range it is testing into
4349 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4350 actually being tested. *PLOW and *PHIGH will be made of the same
4351 type as the returned expression. If EXP is not a comparison, we
4352 will most likely not be returning a useful value and range. Set
4353 *STRICT_OVERFLOW_P to true if the return value is only valid
4354 because signed overflow is undefined; otherwise, do not change
4355 *STRICT_OVERFLOW_P. */
4358 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4359 bool *strict_overflow_p)
4361 enum tree_code code;
4362 tree arg0, arg1 = NULL_TREE;
4363 tree exp_type, nexp;
4366 location_t loc = EXPR_LOCATION (exp);
4368 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4369 and see if we can refine the range. Some of the cases below may not
4370 happen, but it doesn't seem worth worrying about this. We "continue"
4371 the outer loop when we've changed something; otherwise we "break"
4372 the switch, which will "break" the while. */
4375 low = high = build_int_cst (TREE_TYPE (exp), 0);
4379 code = TREE_CODE (exp);
4380 exp_type = TREE_TYPE (exp);
4383 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4385 if (TREE_OPERAND_LENGTH (exp) > 0)
4386 arg0 = TREE_OPERAND (exp, 0);
4387 if (TREE_CODE_CLASS (code) == tcc_binary
4388 || TREE_CODE_CLASS (code) == tcc_comparison
4389 || (TREE_CODE_CLASS (code) == tcc_expression
4390 && TREE_OPERAND_LENGTH (exp) > 1))
4391 arg1 = TREE_OPERAND (exp, 1);
4393 if (arg0 == NULL_TREE)
4396 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4397 &high, &in_p, strict_overflow_p);
4398 if (nexp == NULL_TREE)
4403 /* If EXP is a constant, we can evaluate whether this is true or false. */
4404 if (TREE_CODE (exp) == INTEGER_CST)
4406 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4408 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4414 *pin_p = in_p, *plow = low, *phigh = high;
4418 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4419 type, TYPE, return an expression to test if EXP is in (or out of, depending
4420 on IN_P) the range. Return 0 if the test couldn't be created. */
4423 build_range_check (location_t loc, tree type, tree exp, int in_p,
4424 tree low, tree high)
4426 tree etype = TREE_TYPE (exp), value;
4428 /* Disable this optimization for function pointer expressions
4429 on targets that require function pointer canonicalization. */
4430 if (targetm.have_canonicalize_funcptr_for_compare ()
4431 && TREE_CODE (etype) == POINTER_TYPE
4432 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4437 value = build_range_check (loc, type, exp, 1, low, high);
4439 return invert_truthvalue_loc (loc, value);
4444 if (low == 0 && high == 0)
4445 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4448 return fold_build2_loc (loc, LE_EXPR, type, exp,
4449 fold_convert_loc (loc, etype, high));
4452 return fold_build2_loc (loc, GE_EXPR, type, exp,
4453 fold_convert_loc (loc, etype, low));
4455 if (operand_equal_p (low, high, 0))
4456 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4457 fold_convert_loc (loc, etype, low));
4459 if (integer_zerop (low))
4461 if (! TYPE_UNSIGNED (etype))
4463 etype = unsigned_type_for (etype);
4464 high = fold_convert_loc (loc, etype, high);
4465 exp = fold_convert_loc (loc, etype, exp);
4467 return build_range_check (loc, type, exp, 1, 0, high);
4470 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4471 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4473 int prec = TYPE_PRECISION (etype);
4475 if (wi::mask (prec - 1, false, prec) == high)
4477 if (TYPE_UNSIGNED (etype))
4479 tree signed_etype = signed_type_for (etype);
4480 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4482 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4484 etype = signed_etype;
4485 exp = fold_convert_loc (loc, etype, exp);
4487 return fold_build2_loc (loc, GT_EXPR, type, exp,
4488 build_int_cst (etype, 0));
4492 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4493 This requires wrap-around arithmetics for the type of the expression.
4494 First make sure that arithmetics in this type is valid, then make sure
4495 that it wraps around. */
4496 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4497 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4498 TYPE_UNSIGNED (etype));
4500 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4502 tree utype, minv, maxv;
4504 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4505 for the type in question, as we rely on this here. */
4506 utype = unsigned_type_for (etype);
4507 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4508 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4509 build_int_cst (TREE_TYPE (maxv), 1), 1);
4510 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4512 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4519 high = fold_convert_loc (loc, etype, high);
4520 low = fold_convert_loc (loc, etype, low);
4521 exp = fold_convert_loc (loc, etype, exp);
4523 value = const_binop (MINUS_EXPR, high, low);
4526 if (POINTER_TYPE_P (etype))
4528 if (value != 0 && !TREE_OVERFLOW (value))
4530 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4531 return build_range_check (loc, type,
4532 fold_build_pointer_plus_loc (loc, exp, low),
4533 1, build_int_cst (etype, 0), value);
4538 if (value != 0 && !TREE_OVERFLOW (value))
4539 return build_range_check (loc, type,
4540 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4541 1, build_int_cst (etype, 0), value);
4546 /* Return the predecessor of VAL in its type, handling the infinite case. */
4549 range_predecessor (tree val)
4551 tree type = TREE_TYPE (val);
4553 if (INTEGRAL_TYPE_P (type)
4554 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4557 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4558 build_int_cst (TREE_TYPE (val), 1), 0);
4561 /* Return the successor of VAL in its type, handling the infinite case. */
4564 range_successor (tree val)
4566 tree type = TREE_TYPE (val);
4568 if (INTEGRAL_TYPE_P (type)
4569 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4572 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4573 build_int_cst (TREE_TYPE (val), 1), 0);
4576 /* Given two ranges, see if we can merge them into one. Return 1 if we
4577 can, 0 if we can't. Set the output range into the specified parameters. */
4580 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4581 tree high0, int in1_p, tree low1, tree high1)
4589 int lowequal = ((low0 == 0 && low1 == 0)
4590 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4591 low0, 0, low1, 0)));
4592 int highequal = ((high0 == 0 && high1 == 0)
4593 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4594 high0, 1, high1, 1)));
4596 /* Make range 0 be the range that starts first, or ends last if they
4597 start at the same value. Swap them if it isn't. */
4598 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4601 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4602 high1, 1, high0, 1))))
4604 temp = in0_p, in0_p = in1_p, in1_p = temp;
4605 tem = low0, low0 = low1, low1 = tem;
4606 tem = high0, high0 = high1, high1 = tem;
4609 /* Now flag two cases, whether the ranges are disjoint or whether the
4610 second range is totally subsumed in the first. Note that the tests
4611 below are simplified by the ones above. */
4612 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4613 high0, 1, low1, 0));
4614 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4615 high1, 1, high0, 1));
4617 /* We now have four cases, depending on whether we are including or
4618 excluding the two ranges. */
4621 /* If they don't overlap, the result is false. If the second range
4622 is a subset it is the result. Otherwise, the range is from the start
4623 of the second to the end of the first. */
4625 in_p = 0, low = high = 0;
4627 in_p = 1, low = low1, high = high1;
4629 in_p = 1, low = low1, high = high0;
4632 else if (in0_p && ! in1_p)
4634 /* If they don't overlap, the result is the first range. If they are
4635 equal, the result is false. If the second range is a subset of the
4636 first, and the ranges begin at the same place, we go from just after
4637 the end of the second range to the end of the first. If the second
4638 range is not a subset of the first, or if it is a subset and both
4639 ranges end at the same place, the range starts at the start of the
4640 first range and ends just before the second range.
4641 Otherwise, we can't describe this as a single range. */
4643 in_p = 1, low = low0, high = high0;
4644 else if (lowequal && highequal)
4645 in_p = 0, low = high = 0;
4646 else if (subset && lowequal)
4648 low = range_successor (high1);
4653 /* We are in the weird situation where high0 > high1 but
4654 high1 has no successor. Punt. */
4658 else if (! subset || highequal)
4661 high = range_predecessor (low1);
4665 /* low0 < low1 but low1 has no predecessor. Punt. */
4673 else if (! in0_p && in1_p)
4675 /* If they don't overlap, the result is the second range. If the second
4676 is a subset of the first, the result is false. Otherwise,
4677 the range starts just after the first range and ends at the
4678 end of the second. */
4680 in_p = 1, low = low1, high = high1;
4681 else if (subset || highequal)
4682 in_p = 0, low = high = 0;
4685 low = range_successor (high0);
4690 /* high1 > high0 but high0 has no successor. Punt. */
4698 /* The case where we are excluding both ranges. Here the complex case
4699 is if they don't overlap. In that case, the only time we have a
4700 range is if they are adjacent. If the second is a subset of the
4701 first, the result is the first. Otherwise, the range to exclude
4702 starts at the beginning of the first range and ends at the end of the
4706 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4707 range_successor (high0),
4709 in_p = 0, low = low0, high = high1;
4712 /* Canonicalize - [min, x] into - [-, x]. */
4713 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4714 switch (TREE_CODE (TREE_TYPE (low0)))
4717 if (TYPE_PRECISION (TREE_TYPE (low0))
4718 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4722 if (tree_int_cst_equal (low0,
4723 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4727 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4728 && integer_zerop (low0))
4735 /* Canonicalize - [x, max] into - [x, -]. */
4736 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4737 switch (TREE_CODE (TREE_TYPE (high1)))
4740 if (TYPE_PRECISION (TREE_TYPE (high1))
4741 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4745 if (tree_int_cst_equal (high1,
4746 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4750 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4751 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4753 build_int_cst (TREE_TYPE (high1), 1),
4761 /* The ranges might be also adjacent between the maximum and
4762 minimum values of the given type. For
4763 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4764 return + [x + 1, y - 1]. */
4765 if (low0 == 0 && high1 == 0)
4767 low = range_successor (high0);
4768 high = range_predecessor (low1);
4769 if (low == 0 || high == 0)
4779 in_p = 0, low = low0, high = high0;
4781 in_p = 0, low = low0, high = high1;
4784 *pin_p = in_p, *plow = low, *phigh = high;
4789 /* Subroutine of fold, looking inside expressions of the form
4790 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4791 of the COND_EXPR. This function is being used also to optimize
4792 A op B ? C : A, by reversing the comparison first.
4794 Return a folded expression whose code is not a COND_EXPR
4795 anymore, or NULL_TREE if no folding opportunity is found. */
4798 fold_cond_expr_with_comparison (location_t loc, tree type,
4799 tree arg0, tree arg1, tree arg2)
4801 enum tree_code comp_code = TREE_CODE (arg0);
4802 tree arg00 = TREE_OPERAND (arg0, 0);
4803 tree arg01 = TREE_OPERAND (arg0, 1);
4804 tree arg1_type = TREE_TYPE (arg1);
4810 /* If we have A op 0 ? A : -A, consider applying the following
4813 A == 0? A : -A same as -A
4814 A != 0? A : -A same as A
4815 A >= 0? A : -A same as abs (A)
4816 A > 0? A : -A same as abs (A)
4817 A <= 0? A : -A same as -abs (A)
4818 A < 0? A : -A same as -abs (A)
4820 None of these transformations work for modes with signed
4821 zeros. If A is +/-0, the first two transformations will
4822 change the sign of the result (from +0 to -0, or vice
4823 versa). The last four will fix the sign of the result,
4824 even though the original expressions could be positive or
4825 negative, depending on the sign of A.
4827 Note that all these transformations are correct if A is
4828 NaN, since the two alternatives (A and -A) are also NaNs. */
4829 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4830 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4831 ? real_zerop (arg01)
4832 : integer_zerop (arg01))
4833 && ((TREE_CODE (arg2) == NEGATE_EXPR
4834 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4835 /* In the case that A is of the form X-Y, '-A' (arg2) may
4836 have already been folded to Y-X, check for that. */
4837 || (TREE_CODE (arg1) == MINUS_EXPR
4838 && TREE_CODE (arg2) == MINUS_EXPR
4839 && operand_equal_p (TREE_OPERAND (arg1, 0),
4840 TREE_OPERAND (arg2, 1), 0)
4841 && operand_equal_p (TREE_OPERAND (arg1, 1),
4842 TREE_OPERAND (arg2, 0), 0))))
4847 tem = fold_convert_loc (loc, arg1_type, arg1);
4848 return pedantic_non_lvalue_loc (loc,
4849 fold_convert_loc (loc, type,
4850 negate_expr (tem)));
4853 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4856 if (flag_trapping_math)
4861 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4862 arg1 = fold_convert_loc (loc, signed_type_for
4863 (TREE_TYPE (arg1)), arg1);
4864 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4865 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4868 if (flag_trapping_math)
4872 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4873 arg1 = fold_convert_loc (loc, signed_type_for
4874 (TREE_TYPE (arg1)), arg1);
4875 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4876 return negate_expr (fold_convert_loc (loc, type, tem));
4878 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4882 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4883 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4884 both transformations are correct when A is NaN: A != 0
4885 is then true, and A == 0 is false. */
4887 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4888 && integer_zerop (arg01) && integer_zerop (arg2))
4890 if (comp_code == NE_EXPR)
4891 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4892 else if (comp_code == EQ_EXPR)
4893 return build_zero_cst (type);
4896 /* Try some transformations of A op B ? A : B.
4898 A == B? A : B same as B
4899 A != B? A : B same as A
4900 A >= B? A : B same as max (A, B)
4901 A > B? A : B same as max (B, A)
4902 A <= B? A : B same as min (A, B)
4903 A < B? A : B same as min (B, A)
4905 As above, these transformations don't work in the presence
4906 of signed zeros. For example, if A and B are zeros of
4907 opposite sign, the first two transformations will change
4908 the sign of the result. In the last four, the original
4909 expressions give different results for (A=+0, B=-0) and
4910 (A=-0, B=+0), but the transformed expressions do not.
4912 The first two transformations are correct if either A or B
4913 is a NaN. In the first transformation, the condition will
4914 be false, and B will indeed be chosen. In the case of the
4915 second transformation, the condition A != B will be true,
4916 and A will be chosen.
4918 The conversions to max() and min() are not correct if B is
4919 a number and A is not. The conditions in the original
4920 expressions will be false, so all four give B. The min()
4921 and max() versions would give a NaN instead. */
4922 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4923 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4924 /* Avoid these transformations if the COND_EXPR may be used
4925 as an lvalue in the C++ front-end. PR c++/19199. */
4927 || VECTOR_TYPE_P (type)
4928 || (! lang_GNU_CXX ()
4929 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4930 || ! maybe_lvalue_p (arg1)
4931 || ! maybe_lvalue_p (arg2)))
4933 tree comp_op0 = arg00;
4934 tree comp_op1 = arg01;
4935 tree comp_type = TREE_TYPE (comp_op0);
4937 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4938 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4948 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4950 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4955 /* In C++ a ?: expression can be an lvalue, so put the
4956 operand which will be used if they are equal first
4957 so that we can convert this back to the
4958 corresponding COND_EXPR. */
4959 if (!HONOR_NANS (arg1))
4961 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4962 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4963 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4964 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4965 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4966 comp_op1, comp_op0);
4967 return pedantic_non_lvalue_loc (loc,
4968 fold_convert_loc (loc, type, tem));
4975 if (!HONOR_NANS (arg1))
4977 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4978 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4979 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4980 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4981 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4982 comp_op1, comp_op0);
4983 return pedantic_non_lvalue_loc (loc,
4984 fold_convert_loc (loc, type, tem));
4988 if (!HONOR_NANS (arg1))
4989 return pedantic_non_lvalue_loc (loc,
4990 fold_convert_loc (loc, type, arg2));
4993 if (!HONOR_NANS (arg1))
4994 return pedantic_non_lvalue_loc (loc,
4995 fold_convert_loc (loc, type, arg1));
4998 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5003 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5004 we might still be able to simplify this. For example,
5005 if C1 is one less or one more than C2, this might have started
5006 out as a MIN or MAX and been transformed by this function.
5007 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5009 if (INTEGRAL_TYPE_P (type)
5010 && TREE_CODE (arg01) == INTEGER_CST
5011 && TREE_CODE (arg2) == INTEGER_CST)
5015 if (TREE_CODE (arg1) == INTEGER_CST)
5017 /* We can replace A with C1 in this case. */
5018 arg1 = fold_convert_loc (loc, type, arg01);
5019 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5022 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5023 MIN_EXPR, to preserve the signedness of the comparison. */
5024 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5026 && operand_equal_p (arg01,
5027 const_binop (PLUS_EXPR, arg2,
5028 build_int_cst (type, 1)),
5031 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5032 fold_convert_loc (loc, TREE_TYPE (arg00),
5034 return pedantic_non_lvalue_loc (loc,
5035 fold_convert_loc (loc, type, tem));
5040 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5042 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5044 && operand_equal_p (arg01,
5045 const_binop (MINUS_EXPR, arg2,
5046 build_int_cst (type, 1)),
5049 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5050 fold_convert_loc (loc, TREE_TYPE (arg00),
5052 return pedantic_non_lvalue_loc (loc,
5053 fold_convert_loc (loc, type, tem));
5058 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5059 MAX_EXPR, to preserve the signedness of the comparison. */
5060 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5062 && operand_equal_p (arg01,
5063 const_binop (MINUS_EXPR, arg2,
5064 build_int_cst (type, 1)),
5067 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5068 fold_convert_loc (loc, TREE_TYPE (arg00),
5070 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5075 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5076 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5078 && operand_equal_p (arg01,
5079 const_binop (PLUS_EXPR, arg2,
5080 build_int_cst (type, 1)),
5083 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5084 fold_convert_loc (loc, TREE_TYPE (arg00),
5086 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5100 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5101 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5102 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5106 /* EXP is some logical combination of boolean tests. See if we can
5107 merge it into some range test. Return the new tree if so. */
5110 fold_range_test (location_t loc, enum tree_code code, tree type,
5113 int or_op = (code == TRUTH_ORIF_EXPR
5114 || code == TRUTH_OR_EXPR);
5115 int in0_p, in1_p, in_p;
5116 tree low0, low1, low, high0, high1, high;
5117 bool strict_overflow_p = false;
5119 const char * const warnmsg = G_("assuming signed overflow does not occur "
5120 "when simplifying range test");
5122 if (!INTEGRAL_TYPE_P (type))
5125 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5126 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5128 /* If this is an OR operation, invert both sides; we will invert
5129 again at the end. */
5131 in0_p = ! in0_p, in1_p = ! in1_p;
5133 /* If both expressions are the same, if we can merge the ranges, and we
5134 can build the range test, return it or it inverted. If one of the
5135 ranges is always true or always false, consider it to be the same
5136 expression as the other. */
5137 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5138 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5140 && 0 != (tem = (build_range_check (loc, type,
5142 : rhs != 0 ? rhs : integer_zero_node,
5145 if (strict_overflow_p)
5146 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5147 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5150 /* On machines where the branch cost is expensive, if this is a
5151 short-circuited branch and the underlying object on both sides
5152 is the same, make a non-short-circuit operation. */
5153 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5154 && lhs != 0 && rhs != 0
5155 && (code == TRUTH_ANDIF_EXPR
5156 || code == TRUTH_ORIF_EXPR)
5157 && operand_equal_p (lhs, rhs, 0))
5159 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5160 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5161 which cases we can't do this. */
5162 if (simple_operand_p (lhs))
5163 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5164 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5167 else if (!lang_hooks.decls.global_bindings_p ()
5168 && !CONTAINS_PLACEHOLDER_P (lhs))
5170 tree common = save_expr (lhs);
5172 if (0 != (lhs = build_range_check (loc, type, common,
5173 or_op ? ! in0_p : in0_p,
5175 && (0 != (rhs = build_range_check (loc, type, common,
5176 or_op ? ! in1_p : in1_p,
5179 if (strict_overflow_p)
5180 fold_overflow_warning (warnmsg,
5181 WARN_STRICT_OVERFLOW_COMPARISON);
5182 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5183 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5192 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5193 bit value. Arrange things so the extra bits will be set to zero if and
5194 only if C is signed-extended to its full width. If MASK is nonzero,
5195 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5198 unextend (tree c, int p, int unsignedp, tree mask)
5200 tree type = TREE_TYPE (c);
5201 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5204 if (p == modesize || unsignedp)
5207 /* We work by getting just the sign bit into the low-order bit, then
5208 into the high-order bit, then sign-extend. We then XOR that value
5210 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5212 /* We must use a signed type in order to get an arithmetic right shift.
5213 However, we must also avoid introducing accidental overflows, so that
5214 a subsequent call to integer_zerop will work. Hence we must
5215 do the type conversion here. At this point, the constant is either
5216 zero or one, and the conversion to a signed type can never overflow.
5217 We could get an overflow if this conversion is done anywhere else. */
5218 if (TYPE_UNSIGNED (type))
5219 temp = fold_convert (signed_type_for (type), temp);
5221 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5222 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5224 temp = const_binop (BIT_AND_EXPR, temp,
5225 fold_convert (TREE_TYPE (c), mask));
5226 /* If necessary, convert the type back to match the type of C. */
5227 if (TYPE_UNSIGNED (type))
5228 temp = fold_convert (type, temp);
5230 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5233 /* For an expression that has the form
5237 we can drop one of the inner expressions and simplify to
5241 LOC is the location of the resulting expression. OP is the inner
5242 logical operation; the left-hand side in the examples above, while CMPOP
5243 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5244 removing a condition that guards another, as in
5245 (A != NULL && A->...) || A == NULL
5246 which we must not transform. If RHS_ONLY is true, only eliminate the
5247 right-most operand of the inner logical operation. */
5250 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5253 tree type = TREE_TYPE (cmpop);
5254 enum tree_code code = TREE_CODE (cmpop);
5255 enum tree_code truthop_code = TREE_CODE (op);
5256 tree lhs = TREE_OPERAND (op, 0);
5257 tree rhs = TREE_OPERAND (op, 1);
5258 tree orig_lhs = lhs, orig_rhs = rhs;
5259 enum tree_code rhs_code = TREE_CODE (rhs);
5260 enum tree_code lhs_code = TREE_CODE (lhs);
5261 enum tree_code inv_code;
5263 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5266 if (TREE_CODE_CLASS (code) != tcc_comparison)
5269 if (rhs_code == truthop_code)
5271 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5272 if (newrhs != NULL_TREE)
5275 rhs_code = TREE_CODE (rhs);
5278 if (lhs_code == truthop_code && !rhs_only)
5280 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5281 if (newlhs != NULL_TREE)
5284 lhs_code = TREE_CODE (lhs);
5288 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5289 if (inv_code == rhs_code
5290 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5291 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5293 if (!rhs_only && inv_code == lhs_code
5294 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5295 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5297 if (rhs != orig_rhs || lhs != orig_lhs)
5298 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5303 /* Find ways of folding logical expressions of LHS and RHS:
5304 Try to merge two comparisons to the same innermost item.
5305 Look for range tests like "ch >= '0' && ch <= '9'".
5306 Look for combinations of simple terms on machines with expensive branches
5307 and evaluate the RHS unconditionally.
5309 For example, if we have p->a == 2 && p->b == 4 and we can make an
5310 object large enough to span both A and B, we can do this with a comparison
5311 against the object ANDed with the a mask.
5313 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5314 operations to do this with one comparison.
5316 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5317 function and the one above.
5319 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5320 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5322 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5325 We return the simplified tree or 0 if no optimization is possible. */
5328 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5331 /* If this is the "or" of two comparisons, we can do something if
5332 the comparisons are NE_EXPR. If this is the "and", we can do something
5333 if the comparisons are EQ_EXPR. I.e.,
5334 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5336 WANTED_CODE is this operation code. For single bit fields, we can
5337 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5338 comparison for one-bit fields. */
5340 enum tree_code wanted_code;
5341 enum tree_code lcode, rcode;
5342 tree ll_arg, lr_arg, rl_arg, rr_arg;
5343 tree ll_inner, lr_inner, rl_inner, rr_inner;
5344 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5345 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5346 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5347 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5348 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5349 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5350 machine_mode lnmode, rnmode;
5351 tree ll_mask, lr_mask, rl_mask, rr_mask;
5352 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5353 tree l_const, r_const;
5354 tree lntype, rntype, result;
5355 HOST_WIDE_INT first_bit, end_bit;
5358 /* Start by getting the comparison codes. Fail if anything is volatile.
5359 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5360 it were surrounded with a NE_EXPR. */
5362 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5365 lcode = TREE_CODE (lhs);
5366 rcode = TREE_CODE (rhs);
5368 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5370 lhs = build2 (NE_EXPR, truth_type, lhs,
5371 build_int_cst (TREE_TYPE (lhs), 0));
5375 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5377 rhs = build2 (NE_EXPR, truth_type, rhs,
5378 build_int_cst (TREE_TYPE (rhs), 0));
5382 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5383 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5386 ll_arg = TREE_OPERAND (lhs, 0);
5387 lr_arg = TREE_OPERAND (lhs, 1);
5388 rl_arg = TREE_OPERAND (rhs, 0);
5389 rr_arg = TREE_OPERAND (rhs, 1);
5391 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5392 if (simple_operand_p (ll_arg)
5393 && simple_operand_p (lr_arg))
5395 if (operand_equal_p (ll_arg, rl_arg, 0)
5396 && operand_equal_p (lr_arg, rr_arg, 0))
5398 result = combine_comparisons (loc, code, lcode, rcode,
5399 truth_type, ll_arg, lr_arg);
5403 else if (operand_equal_p (ll_arg, rr_arg, 0)
5404 && operand_equal_p (lr_arg, rl_arg, 0))
5406 result = combine_comparisons (loc, code, lcode,
5407 swap_tree_comparison (rcode),
5408 truth_type, ll_arg, lr_arg);
5414 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5415 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5417 /* If the RHS can be evaluated unconditionally and its operands are
5418 simple, it wins to evaluate the RHS unconditionally on machines
5419 with expensive branches. In this case, this isn't a comparison
5420 that can be merged. */
5422 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5424 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5425 && simple_operand_p (rl_arg)
5426 && simple_operand_p (rr_arg))
5428 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5429 if (code == TRUTH_OR_EXPR
5430 && lcode == NE_EXPR && integer_zerop (lr_arg)
5431 && rcode == NE_EXPR && integer_zerop (rr_arg)
5432 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5433 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5434 return build2_loc (loc, NE_EXPR, truth_type,
5435 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5437 build_int_cst (TREE_TYPE (ll_arg), 0));
5439 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5440 if (code == TRUTH_AND_EXPR
5441 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5442 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5443 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5444 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5445 return build2_loc (loc, EQ_EXPR, truth_type,
5446 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5448 build_int_cst (TREE_TYPE (ll_arg), 0));
5451 /* See if the comparisons can be merged. Then get all the parameters for
5454 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5455 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5459 ll_inner = decode_field_reference (loc, ll_arg,
5460 &ll_bitsize, &ll_bitpos, &ll_mode,
5461 &ll_unsignedp, &volatilep, &ll_mask,
5463 lr_inner = decode_field_reference (loc, lr_arg,
5464 &lr_bitsize, &lr_bitpos, &lr_mode,
5465 &lr_unsignedp, &volatilep, &lr_mask,
5467 rl_inner = decode_field_reference (loc, rl_arg,
5468 &rl_bitsize, &rl_bitpos, &rl_mode,
5469 &rl_unsignedp, &volatilep, &rl_mask,
5471 rr_inner = decode_field_reference (loc, rr_arg,
5472 &rr_bitsize, &rr_bitpos, &rr_mode,
5473 &rr_unsignedp, &volatilep, &rr_mask,
5476 /* It must be true that the inner operation on the lhs of each
5477 comparison must be the same if we are to be able to do anything.
5478 Then see if we have constants. If not, the same must be true for
5480 if (volatilep || ll_inner == 0 || rl_inner == 0
5481 || ! operand_equal_p (ll_inner, rl_inner, 0))
5484 if (TREE_CODE (lr_arg) == INTEGER_CST
5485 && TREE_CODE (rr_arg) == INTEGER_CST)
5486 l_const = lr_arg, r_const = rr_arg;
5487 else if (lr_inner == 0 || rr_inner == 0
5488 || ! operand_equal_p (lr_inner, rr_inner, 0))
5491 l_const = r_const = 0;
5493 /* If either comparison code is not correct for our logical operation,
5494 fail. However, we can convert a one-bit comparison against zero into
5495 the opposite comparison against that bit being set in the field. */
5497 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5498 if (lcode != wanted_code)
5500 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5502 /* Make the left operand unsigned, since we are only interested
5503 in the value of one bit. Otherwise we are doing the wrong
5512 /* This is analogous to the code for l_const above. */
5513 if (rcode != wanted_code)
5515 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5524 /* See if we can find a mode that contains both fields being compared on
5525 the left. If we can't, fail. Otherwise, update all constants and masks
5526 to be relative to a field of that size. */
5527 first_bit = MIN (ll_bitpos, rl_bitpos);
5528 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5529 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5530 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5532 if (lnmode == VOIDmode)
5535 lnbitsize = GET_MODE_BITSIZE (lnmode);
5536 lnbitpos = first_bit & ~ (lnbitsize - 1);
5537 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5538 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5540 if (BYTES_BIG_ENDIAN)
5542 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5543 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5546 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5547 size_int (xll_bitpos));
5548 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5549 size_int (xrl_bitpos));
5553 l_const = fold_convert_loc (loc, lntype, l_const);
5554 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5555 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5556 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5557 fold_build1_loc (loc, BIT_NOT_EXPR,
5560 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5562 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5567 r_const = fold_convert_loc (loc, lntype, r_const);
5568 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5569 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5570 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5571 fold_build1_loc (loc, BIT_NOT_EXPR,
5574 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5576 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5580 /* If the right sides are not constant, do the same for it. Also,
5581 disallow this optimization if a size or signedness mismatch occurs
5582 between the left and right sides. */
5585 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5586 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5587 /* Make sure the two fields on the right
5588 correspond to the left without being swapped. */
5589 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5592 first_bit = MIN (lr_bitpos, rr_bitpos);
5593 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5594 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5595 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5597 if (rnmode == VOIDmode)
5600 rnbitsize = GET_MODE_BITSIZE (rnmode);
5601 rnbitpos = first_bit & ~ (rnbitsize - 1);
5602 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5603 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5605 if (BYTES_BIG_ENDIAN)
5607 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5608 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5611 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5613 size_int (xlr_bitpos));
5614 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5616 size_int (xrr_bitpos));
5618 /* Make a mask that corresponds to both fields being compared.
5619 Do this for both items being compared. If the operands are the
5620 same size and the bits being compared are in the same position
5621 then we can do this by masking both and comparing the masked
5623 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5624 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5625 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5627 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5628 ll_unsignedp || rl_unsignedp);
5629 if (! all_ones_mask_p (ll_mask, lnbitsize))
5630 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5632 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5633 lr_unsignedp || rr_unsignedp);
5634 if (! all_ones_mask_p (lr_mask, rnbitsize))
5635 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5637 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5640 /* There is still another way we can do something: If both pairs of
5641 fields being compared are adjacent, we may be able to make a wider
5642 field containing them both.
5644 Note that we still must mask the lhs/rhs expressions. Furthermore,
5645 the mask must be shifted to account for the shift done by
5646 make_bit_field_ref. */
5647 if ((ll_bitsize + ll_bitpos == rl_bitpos
5648 && lr_bitsize + lr_bitpos == rr_bitpos)
5649 || (ll_bitpos == rl_bitpos + rl_bitsize
5650 && lr_bitpos == rr_bitpos + rr_bitsize))
5654 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5655 ll_bitsize + rl_bitsize,
5656 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5657 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5658 lr_bitsize + rr_bitsize,
5659 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5661 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5662 size_int (MIN (xll_bitpos, xrl_bitpos)));
5663 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5664 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5666 /* Convert to the smaller type before masking out unwanted bits. */
5668 if (lntype != rntype)
5670 if (lnbitsize > rnbitsize)
5672 lhs = fold_convert_loc (loc, rntype, lhs);
5673 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5676 else if (lnbitsize < rnbitsize)
5678 rhs = fold_convert_loc (loc, lntype, rhs);
5679 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5684 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5685 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5687 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5688 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5690 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5696 /* Handle the case of comparisons with constants. If there is something in
5697 common between the masks, those bits of the constants must be the same.
5698 If not, the condition is always false. Test for this to avoid generating
5699 incorrect code below. */
5700 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5701 if (! integer_zerop (result)
5702 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5703 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5705 if (wanted_code == NE_EXPR)
5707 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5708 return constant_boolean_node (true, truth_type);
5712 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5713 return constant_boolean_node (false, truth_type);
5717 /* Construct the expression we will return. First get the component
5718 reference we will make. Unless the mask is all ones the width of
5719 that field, perform the mask operation. Then compare with the
5721 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5722 ll_unsignedp || rl_unsignedp);
5724 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5725 if (! all_ones_mask_p (ll_mask, lnbitsize))
5726 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5728 return build2_loc (loc, wanted_code, truth_type, result,
5729 const_binop (BIT_IOR_EXPR, l_const, r_const));
5732 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5736 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5740 enum tree_code op_code;
5743 int consts_equal, consts_lt;
5746 STRIP_SIGN_NOPS (arg0);
5748 op_code = TREE_CODE (arg0);
5749 minmax_const = TREE_OPERAND (arg0, 1);
5750 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5751 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5752 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5753 inner = TREE_OPERAND (arg0, 0);
5755 /* If something does not permit us to optimize, return the original tree. */
5756 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5757 || TREE_CODE (comp_const) != INTEGER_CST
5758 || TREE_OVERFLOW (comp_const)
5759 || TREE_CODE (minmax_const) != INTEGER_CST
5760 || TREE_OVERFLOW (minmax_const))
5763 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5764 and GT_EXPR, doing the rest with recursive calls using logical
5768 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5771 = optimize_minmax_comparison (loc,
5772 invert_tree_comparison (code, false),
5775 return invert_truthvalue_loc (loc, tem);
5781 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5782 optimize_minmax_comparison
5783 (loc, EQ_EXPR, type, arg0, comp_const),
5784 optimize_minmax_comparison
5785 (loc, GT_EXPR, type, arg0, comp_const));
5788 if (op_code == MAX_EXPR && consts_equal)
5789 /* MAX (X, 0) == 0 -> X <= 0 */
5790 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5792 else if (op_code == MAX_EXPR && consts_lt)
5793 /* MAX (X, 0) == 5 -> X == 5 */
5794 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5796 else if (op_code == MAX_EXPR)
5797 /* MAX (X, 0) == -1 -> false */
5798 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5800 else if (consts_equal)
5801 /* MIN (X, 0) == 0 -> X >= 0 */
5802 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5805 /* MIN (X, 0) == 5 -> false */
5806 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5809 /* MIN (X, 0) == -1 -> X == -1 */
5810 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5813 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5814 /* MAX (X, 0) > 0 -> X > 0
5815 MAX (X, 0) > 5 -> X > 5 */
5816 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5818 else if (op_code == MAX_EXPR)
5819 /* MAX (X, 0) > -1 -> true */
5820 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5822 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5823 /* MIN (X, 0) > 0 -> false
5824 MIN (X, 0) > 5 -> false */
5825 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5828 /* MIN (X, 0) > -1 -> X > -1 */
5829 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5836 /* T is an integer expression that is being multiplied, divided, or taken a
5837 modulus (CODE says which and what kind of divide or modulus) by a
5838 constant C. See if we can eliminate that operation by folding it with
5839 other operations already in T. WIDE_TYPE, if non-null, is a type that
5840 should be used for the computation if wider than our type.
5842 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5843 (X * 2) + (Y * 4). We must, however, be assured that either the original
5844 expression would not overflow or that overflow is undefined for the type
5845 in the language in question.
5847 If we return a non-null expression, it is an equivalent form of the
5848 original computation, but need not be in the original type.
5850 We set *STRICT_OVERFLOW_P to true if the return values depends on
5851 signed overflow being undefined. Otherwise we do not change
5852 *STRICT_OVERFLOW_P. */
5855 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5856 bool *strict_overflow_p)
5858 /* To avoid exponential search depth, refuse to allow recursion past
5859 three levels. Beyond that (1) it's highly unlikely that we'll find
5860 something interesting and (2) we've probably processed it before
5861 when we built the inner expression. */
5870 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5877 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5878 bool *strict_overflow_p)
5880 tree type = TREE_TYPE (t);
5881 enum tree_code tcode = TREE_CODE (t);
5882 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5883 > GET_MODE_SIZE (TYPE_MODE (type)))
5884 ? wide_type : type);
5886 int same_p = tcode == code;
5887 tree op0 = NULL_TREE, op1 = NULL_TREE;
5888 bool sub_strict_overflow_p;
5890 /* Don't deal with constants of zero here; they confuse the code below. */
5891 if (integer_zerop (c))
5894 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5895 op0 = TREE_OPERAND (t, 0);
5897 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5898 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5900 /* Note that we need not handle conditional operations here since fold
5901 already handles those cases. So just do arithmetic here. */
5905 /* For a constant, we can always simplify if we are a multiply
5906 or (for divide and modulus) if it is a multiple of our constant. */
5907 if (code == MULT_EXPR
5908 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5909 return const_binop (code, fold_convert (ctype, t),
5910 fold_convert (ctype, c));
5913 CASE_CONVERT: case NON_LVALUE_EXPR:
5914 /* If op0 is an expression ... */
5915 if ((COMPARISON_CLASS_P (op0)
5916 || UNARY_CLASS_P (op0)
5917 || BINARY_CLASS_P (op0)
5918 || VL_EXP_CLASS_P (op0)
5919 || EXPRESSION_CLASS_P (op0))
5920 /* ... and has wrapping overflow, and its type is smaller
5921 than ctype, then we cannot pass through as widening. */
5922 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5923 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5924 && (TYPE_PRECISION (ctype)
5925 > TYPE_PRECISION (TREE_TYPE (op0))))
5926 /* ... or this is a truncation (t is narrower than op0),
5927 then we cannot pass through this narrowing. */
5928 || (TYPE_PRECISION (type)
5929 < TYPE_PRECISION (TREE_TYPE (op0)))
5930 /* ... or signedness changes for division or modulus,
5931 then we cannot pass through this conversion. */
5932 || (code != MULT_EXPR
5933 && (TYPE_UNSIGNED (ctype)
5934 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5935 /* ... or has undefined overflow while the converted to
5936 type has not, we cannot do the operation in the inner type
5937 as that would introduce undefined overflow. */
5938 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5939 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
5940 && !TYPE_OVERFLOW_UNDEFINED (type))))
5943 /* Pass the constant down and see if we can make a simplification. If
5944 we can, replace this expression with the inner simplification for
5945 possible later conversion to our or some other type. */
5946 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5947 && TREE_CODE (t2) == INTEGER_CST
5948 && !TREE_OVERFLOW (t2)
5949 && (0 != (t1 = extract_muldiv (op0, t2, code,
5951 ? ctype : NULL_TREE,
5952 strict_overflow_p))))
5957 /* If widening the type changes it from signed to unsigned, then we
5958 must avoid building ABS_EXPR itself as unsigned. */
5959 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5961 tree cstype = (*signed_type_for) (ctype);
5962 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5965 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5966 return fold_convert (ctype, t1);
5970 /* If the constant is negative, we cannot simplify this. */
5971 if (tree_int_cst_sgn (c) == -1)
5975 /* For division and modulus, type can't be unsigned, as e.g.
5976 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5977 For signed types, even with wrapping overflow, this is fine. */
5978 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
5980 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5982 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5985 case MIN_EXPR: case MAX_EXPR:
5986 /* If widening the type changes the signedness, then we can't perform
5987 this optimization as that changes the result. */
5988 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5991 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5992 sub_strict_overflow_p = false;
5993 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5994 &sub_strict_overflow_p)) != 0
5995 && (t2 = extract_muldiv (op1, c, code, wide_type,
5996 &sub_strict_overflow_p)) != 0)
5998 if (tree_int_cst_sgn (c) < 0)
5999 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6000 if (sub_strict_overflow_p)
6001 *strict_overflow_p = true;
6002 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6003 fold_convert (ctype, t2));
6007 case LSHIFT_EXPR: case RSHIFT_EXPR:
6008 /* If the second operand is constant, this is a multiplication
6009 or floor division, by a power of two, so we can treat it that
6010 way unless the multiplier or divisor overflows. Signed
6011 left-shift overflow is implementation-defined rather than
6012 undefined in C90, so do not convert signed left shift into
6014 if (TREE_CODE (op1) == INTEGER_CST
6015 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6016 /* const_binop may not detect overflow correctly,
6017 so check for it explicitly here. */
6018 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6019 && 0 != (t1 = fold_convert (ctype,
6020 const_binop (LSHIFT_EXPR,
6023 && !TREE_OVERFLOW (t1))
6024 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6025 ? MULT_EXPR : FLOOR_DIV_EXPR,
6027 fold_convert (ctype, op0),
6029 c, code, wide_type, strict_overflow_p);
6032 case PLUS_EXPR: case MINUS_EXPR:
6033 /* See if we can eliminate the operation on both sides. If we can, we
6034 can return a new PLUS or MINUS. If we can't, the only remaining
6035 cases where we can do anything are if the second operand is a
6037 sub_strict_overflow_p = false;
6038 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6039 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6040 if (t1 != 0 && t2 != 0
6041 && (code == MULT_EXPR
6042 /* If not multiplication, we can only do this if both operands
6043 are divisible by c. */
6044 || (multiple_of_p (ctype, op0, c)
6045 && multiple_of_p (ctype, op1, c))))
6047 if (sub_strict_overflow_p)
6048 *strict_overflow_p = true;
6049 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6050 fold_convert (ctype, t2));
6053 /* If this was a subtraction, negate OP1 and set it to be an addition.
6054 This simplifies the logic below. */
6055 if (tcode == MINUS_EXPR)
6057 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6058 /* If OP1 was not easily negatable, the constant may be OP0. */
6059 if (TREE_CODE (op0) == INTEGER_CST)
6061 std::swap (op0, op1);
6066 if (TREE_CODE (op1) != INTEGER_CST)
6069 /* If either OP1 or C are negative, this optimization is not safe for
6070 some of the division and remainder types while for others we need
6071 to change the code. */
6072 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6074 if (code == CEIL_DIV_EXPR)
6075 code = FLOOR_DIV_EXPR;
6076 else if (code == FLOOR_DIV_EXPR)
6077 code = CEIL_DIV_EXPR;
6078 else if (code != MULT_EXPR
6079 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6083 /* If it's a multiply or a division/modulus operation of a multiple
6084 of our constant, do the operation and verify it doesn't overflow. */
6085 if (code == MULT_EXPR
6086 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6088 op1 = const_binop (code, fold_convert (ctype, op1),
6089 fold_convert (ctype, c));
6090 /* We allow the constant to overflow with wrapping semantics. */
6092 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6098 /* If we have an unsigned type, we cannot widen the operation since it
6099 will change the result if the original computation overflowed. */
6100 if (TYPE_UNSIGNED (ctype) && ctype != type)
6103 /* If we were able to eliminate our operation from the first side,
6104 apply our operation to the second side and reform the PLUS. */
6105 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6106 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6108 /* The last case is if we are a multiply. In that case, we can
6109 apply the distributive law to commute the multiply and addition
6110 if the multiplication of the constants doesn't overflow
6111 and overflow is defined. With undefined overflow
6112 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6113 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6114 return fold_build2 (tcode, ctype,
6115 fold_build2 (code, ctype,
6116 fold_convert (ctype, op0),
6117 fold_convert (ctype, c)),
6123 /* We have a special case here if we are doing something like
6124 (C * 8) % 4 since we know that's zero. */
6125 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6126 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6127 /* If the multiplication can overflow we cannot optimize this. */
6128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6129 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6130 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6132 *strict_overflow_p = true;
6133 return omit_one_operand (type, integer_zero_node, op0);
6136 /* ... fall through ... */
6138 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6139 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6140 /* If we can extract our operation from the LHS, do so and return a
6141 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6142 do something only if the second operand is a constant. */
6144 && (t1 = extract_muldiv (op0, c, code, wide_type,
6145 strict_overflow_p)) != 0)
6146 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6147 fold_convert (ctype, op1));
6148 else if (tcode == MULT_EXPR && code == MULT_EXPR
6149 && (t1 = extract_muldiv (op1, c, code, wide_type,
6150 strict_overflow_p)) != 0)
6151 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6152 fold_convert (ctype, t1));
6153 else if (TREE_CODE (op1) != INTEGER_CST)
6156 /* If these are the same operation types, we can associate them
6157 assuming no overflow. */
6160 bool overflow_p = false;
6161 bool overflow_mul_p;
6162 signop sign = TYPE_SIGN (ctype);
6163 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6164 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6166 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6170 mul = wide_int::from (mul, TYPE_PRECISION (ctype),
6171 TYPE_SIGN (TREE_TYPE (op1)));
6172 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6173 wide_int_to_tree (ctype, mul));
6177 /* If these operations "cancel" each other, we have the main
6178 optimizations of this pass, which occur when either constant is a
6179 multiple of the other, in which case we replace this with either an
6180 operation or CODE or TCODE.
6182 If we have an unsigned type, we cannot do this since it will change
6183 the result if the original computation overflowed. */
6184 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6185 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6186 || (tcode == MULT_EXPR
6187 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6188 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6189 && code != MULT_EXPR)))
6191 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6193 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6194 *strict_overflow_p = true;
6195 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6196 fold_convert (ctype,
6197 const_binop (TRUNC_DIV_EXPR,
6200 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6202 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6203 *strict_overflow_p = true;
6204 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6205 fold_convert (ctype,
6206 const_binop (TRUNC_DIV_EXPR,
6219 /* Return a node which has the indicated constant VALUE (either 0 or
6220 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6221 and is of the indicated TYPE. */
6224 constant_boolean_node (bool value, tree type)
6226 if (type == integer_type_node)
6227 return value ? integer_one_node : integer_zero_node;
6228 else if (type == boolean_type_node)
6229 return value ? boolean_true_node : boolean_false_node;
6230 else if (TREE_CODE (type) == VECTOR_TYPE)
6231 return build_vector_from_val (type,
6232 build_int_cst (TREE_TYPE (type),
6235 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6239 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6240 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6241 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6242 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6243 COND is the first argument to CODE; otherwise (as in the example
6244 given here), it is the second argument. TYPE is the type of the
6245 original expression. Return NULL_TREE if no simplification is
6249 fold_binary_op_with_conditional_arg (location_t loc,
6250 enum tree_code code,
6251 tree type, tree op0, tree op1,
6252 tree cond, tree arg, int cond_first_p)
6254 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6255 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6256 tree test, true_value, false_value;
6257 tree lhs = NULL_TREE;
6258 tree rhs = NULL_TREE;
6259 enum tree_code cond_code = COND_EXPR;
6261 if (TREE_CODE (cond) == COND_EXPR
6262 || TREE_CODE (cond) == VEC_COND_EXPR)
6264 test = TREE_OPERAND (cond, 0);
6265 true_value = TREE_OPERAND (cond, 1);
6266 false_value = TREE_OPERAND (cond, 2);
6267 /* If this operand throws an expression, then it does not make
6268 sense to try to perform a logical or arithmetic operation
6270 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6272 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6277 tree testtype = TREE_TYPE (cond);
6279 true_value = constant_boolean_node (true, testtype);
6280 false_value = constant_boolean_node (false, testtype);
6283 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6284 cond_code = VEC_COND_EXPR;
6286 /* This transformation is only worthwhile if we don't have to wrap ARG
6287 in a SAVE_EXPR and the operation can be simplified without recursing
6288 on at least one of the branches once its pushed inside the COND_EXPR. */
6289 if (!TREE_CONSTANT (arg)
6290 && (TREE_SIDE_EFFECTS (arg)
6291 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6292 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6295 arg = fold_convert_loc (loc, arg_type, arg);
6298 true_value = fold_convert_loc (loc, cond_type, true_value);
6300 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6302 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6306 false_value = fold_convert_loc (loc, cond_type, false_value);
6308 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6310 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6313 /* Check that we have simplified at least one of the branches. */
6314 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6317 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6321 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6323 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6324 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6325 ADDEND is the same as X.
6327 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6328 and finite. The problematic cases are when X is zero, and its mode
6329 has signed zeros. In the case of rounding towards -infinity,
6330 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6331 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6334 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6336 if (!real_zerop (addend))
6339 /* Don't allow the fold with -fsignaling-nans. */
6340 if (HONOR_SNANS (element_mode (type)))
6343 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6344 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6347 /* In a vector or complex, we would need to check the sign of all zeros. */
6348 if (TREE_CODE (addend) != REAL_CST)
6351 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6352 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6355 /* The mode has signed zeros, and we have to honor their sign.
6356 In this situation, there is only one case we can return true for.
6357 X - 0 is the same as X unless rounding towards -infinity is
6359 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6362 /* Subroutine of fold() that optimizes comparisons of a division by
6363 a nonzero integer constant against an integer constant, i.e.
6366 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6367 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6368 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6370 The function returns the constant folded tree if a simplification
6371 can be made, and NULL_TREE otherwise. */
6374 fold_div_compare (location_t loc,
6375 enum tree_code code, tree type, tree arg0, tree arg1)
6377 tree prod, tmp, hi, lo;
6378 tree arg00 = TREE_OPERAND (arg0, 0);
6379 tree arg01 = TREE_OPERAND (arg0, 1);
6380 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6381 bool neg_overflow = false;
6384 /* We have to do this the hard way to detect unsigned overflow.
6385 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6386 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6387 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6388 neg_overflow = false;
6390 if (sign == UNSIGNED)
6392 tmp = int_const_binop (MINUS_EXPR, arg01,
6393 build_int_cst (TREE_TYPE (arg01), 1));
6396 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6397 val = wi::add (prod, tmp, sign, &overflow);
6398 hi = force_fit_type (TREE_TYPE (arg00), val,
6399 -1, overflow | TREE_OVERFLOW (prod));
6401 else if (tree_int_cst_sgn (arg01) >= 0)
6403 tmp = int_const_binop (MINUS_EXPR, arg01,
6404 build_int_cst (TREE_TYPE (arg01), 1));
6405 switch (tree_int_cst_sgn (arg1))
6408 neg_overflow = true;
6409 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6414 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6419 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6429 /* A negative divisor reverses the relational operators. */
6430 code = swap_tree_comparison (code);
6432 tmp = int_const_binop (PLUS_EXPR, arg01,
6433 build_int_cst (TREE_TYPE (arg01), 1));
6434 switch (tree_int_cst_sgn (arg1))
6437 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6442 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6447 neg_overflow = true;
6448 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6460 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6461 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6462 if (TREE_OVERFLOW (hi))
6463 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6464 if (TREE_OVERFLOW (lo))
6465 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6466 return build_range_check (loc, type, arg00, 1, lo, hi);
6469 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6470 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6471 if (TREE_OVERFLOW (hi))
6472 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6473 if (TREE_OVERFLOW (lo))
6474 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6475 return build_range_check (loc, type, arg00, 0, lo, hi);
6478 if (TREE_OVERFLOW (lo))
6480 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6481 return omit_one_operand_loc (loc, type, tmp, arg00);
6483 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6486 if (TREE_OVERFLOW (hi))
6488 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6489 return omit_one_operand_loc (loc, type, tmp, arg00);
6491 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6494 if (TREE_OVERFLOW (hi))
6496 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6497 return omit_one_operand_loc (loc, type, tmp, arg00);
6499 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6502 if (TREE_OVERFLOW (lo))
6504 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6505 return omit_one_operand_loc (loc, type, tmp, arg00);
6507 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6517 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6518 equality/inequality test, then return a simplified form of the test
6519 using a sign testing. Otherwise return NULL. TYPE is the desired
6523 fold_single_bit_test_into_sign_test (location_t loc,
6524 enum tree_code code, tree arg0, tree arg1,
6527 /* If this is testing a single bit, we can optimize the test. */
6528 if ((code == NE_EXPR || code == EQ_EXPR)
6529 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6530 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6532 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6533 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6534 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6536 if (arg00 != NULL_TREE
6537 /* This is only a win if casting to a signed type is cheap,
6538 i.e. when arg00's type is not a partial mode. */
6539 && TYPE_PRECISION (TREE_TYPE (arg00))
6540 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6542 tree stype = signed_type_for (TREE_TYPE (arg00));
6543 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6545 fold_convert_loc (loc, stype, arg00),
6546 build_int_cst (stype, 0));
6553 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6554 equality/inequality test, then return a simplified form of
6555 the test using shifts and logical operations. Otherwise return
6556 NULL. TYPE is the desired result type. */
6559 fold_single_bit_test (location_t loc, enum tree_code code,
6560 tree arg0, tree arg1, tree result_type)
6562 /* If this is testing a single bit, we can optimize the test. */
6563 if ((code == NE_EXPR || code == EQ_EXPR)
6564 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6565 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6567 tree inner = TREE_OPERAND (arg0, 0);
6568 tree type = TREE_TYPE (arg0);
6569 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6570 machine_mode operand_mode = TYPE_MODE (type);
6572 tree signed_type, unsigned_type, intermediate_type;
6575 /* First, see if we can fold the single bit test into a sign-bit
6577 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6582 /* Otherwise we have (A & C) != 0 where C is a single bit,
6583 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6584 Similarly for (A & C) == 0. */
6586 /* If INNER is a right shift of a constant and it plus BITNUM does
6587 not overflow, adjust BITNUM and INNER. */
6588 if (TREE_CODE (inner) == RSHIFT_EXPR
6589 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6590 && bitnum < TYPE_PRECISION (type)
6591 && wi::ltu_p (TREE_OPERAND (inner, 1),
6592 TYPE_PRECISION (type) - bitnum))
6594 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6595 inner = TREE_OPERAND (inner, 0);
6598 /* If we are going to be able to omit the AND below, we must do our
6599 operations as unsigned. If we must use the AND, we have a choice.
6600 Normally unsigned is faster, but for some machines signed is. */
6601 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6602 && !flag_syntax_only) ? 0 : 1;
6604 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6605 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6606 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6607 inner = fold_convert_loc (loc, intermediate_type, inner);
6610 inner = build2 (RSHIFT_EXPR, intermediate_type,
6611 inner, size_int (bitnum));
6613 one = build_int_cst (intermediate_type, 1);
6615 if (code == EQ_EXPR)
6616 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6618 /* Put the AND last so it can combine with more things. */
6619 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6621 /* Make sure to return the proper type. */
6622 inner = fold_convert_loc (loc, result_type, inner);
6629 /* Check whether we are allowed to reorder operands arg0 and arg1,
6630 such that the evaluation of arg1 occurs before arg0. */
6633 reorder_operands_p (const_tree arg0, const_tree arg1)
6635 if (! flag_evaluation_order)
6637 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6639 return ! TREE_SIDE_EFFECTS (arg0)
6640 && ! TREE_SIDE_EFFECTS (arg1);
6643 /* Test whether it is preferable two swap two operands, ARG0 and
6644 ARG1, for example because ARG0 is an integer constant and ARG1
6645 isn't. If REORDER is true, only recommend swapping if we can
6646 evaluate the operands in reverse order. */
6649 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6651 if (CONSTANT_CLASS_P (arg1))
6653 if (CONSTANT_CLASS_P (arg0))
6659 if (TREE_CONSTANT (arg1))
6661 if (TREE_CONSTANT (arg0))
6664 if (reorder && flag_evaluation_order
6665 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6668 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6669 for commutative and comparison operators. Ensuring a canonical
6670 form allows the optimizers to find additional redundancies without
6671 having to explicitly check for both orderings. */
6672 if (TREE_CODE (arg0) == SSA_NAME
6673 && TREE_CODE (arg1) == SSA_NAME
6674 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6677 /* Put SSA_NAMEs last. */
6678 if (TREE_CODE (arg1) == SSA_NAME)
6680 if (TREE_CODE (arg0) == SSA_NAME)
6683 /* Put variables last. */
6693 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6694 means A >= Y && A != MAX, but in this case we know that
6695 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6698 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6700 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6702 if (TREE_CODE (bound) == LT_EXPR)
6703 a = TREE_OPERAND (bound, 0);
6704 else if (TREE_CODE (bound) == GT_EXPR)
6705 a = TREE_OPERAND (bound, 1);
6709 typea = TREE_TYPE (a);
6710 if (!INTEGRAL_TYPE_P (typea)
6711 && !POINTER_TYPE_P (typea))
6714 if (TREE_CODE (ineq) == LT_EXPR)
6716 a1 = TREE_OPERAND (ineq, 1);
6717 y = TREE_OPERAND (ineq, 0);
6719 else if (TREE_CODE (ineq) == GT_EXPR)
6721 a1 = TREE_OPERAND (ineq, 0);
6722 y = TREE_OPERAND (ineq, 1);
6727 if (TREE_TYPE (a1) != typea)
6730 if (POINTER_TYPE_P (typea))
6732 /* Convert the pointer types into integer before taking the difference. */
6733 tree ta = fold_convert_loc (loc, ssizetype, a);
6734 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6735 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6738 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6740 if (!diff || !integer_onep (diff))
6743 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6746 /* Fold a sum or difference of at least one multiplication.
6747 Returns the folded tree or NULL if no simplification could be made. */
6750 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6751 tree arg0, tree arg1)
6753 tree arg00, arg01, arg10, arg11;
6754 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6756 /* (A * C) +- (B * C) -> (A+-B) * C.
6757 (A * C) +- A -> A * (C+-1).
6758 We are most concerned about the case where C is a constant,
6759 but other combinations show up during loop reduction. Since
6760 it is not difficult, try all four possibilities. */
6762 if (TREE_CODE (arg0) == MULT_EXPR)
6764 arg00 = TREE_OPERAND (arg0, 0);
6765 arg01 = TREE_OPERAND (arg0, 1);
6767 else if (TREE_CODE (arg0) == INTEGER_CST)
6769 arg00 = build_one_cst (type);
6774 /* We cannot generate constant 1 for fract. */
6775 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6778 arg01 = build_one_cst (type);
6780 if (TREE_CODE (arg1) == MULT_EXPR)
6782 arg10 = TREE_OPERAND (arg1, 0);
6783 arg11 = TREE_OPERAND (arg1, 1);
6785 else if (TREE_CODE (arg1) == INTEGER_CST)
6787 arg10 = build_one_cst (type);
6788 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6789 the purpose of this canonicalization. */
6790 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6791 && negate_expr_p (arg1)
6792 && code == PLUS_EXPR)
6794 arg11 = negate_expr (arg1);
6802 /* We cannot generate constant 1 for fract. */
6803 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6806 arg11 = build_one_cst (type);
6810 if (operand_equal_p (arg01, arg11, 0))
6811 same = arg01, alt0 = arg00, alt1 = arg10;
6812 else if (operand_equal_p (arg00, arg10, 0))
6813 same = arg00, alt0 = arg01, alt1 = arg11;
6814 else if (operand_equal_p (arg00, arg11, 0))
6815 same = arg00, alt0 = arg01, alt1 = arg10;
6816 else if (operand_equal_p (arg01, arg10, 0))
6817 same = arg01, alt0 = arg00, alt1 = arg11;
6819 /* No identical multiplicands; see if we can find a common
6820 power-of-two factor in non-power-of-two multiplies. This
6821 can help in multi-dimensional array access. */
6822 else if (tree_fits_shwi_p (arg01)
6823 && tree_fits_shwi_p (arg11))
6825 HOST_WIDE_INT int01, int11, tmp;
6828 int01 = tree_to_shwi (arg01);
6829 int11 = tree_to_shwi (arg11);
6831 /* Move min of absolute values to int11. */
6832 if (absu_hwi (int01) < absu_hwi (int11))
6834 tmp = int01, int01 = int11, int11 = tmp;
6835 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6842 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6843 /* The remainder should not be a constant, otherwise we
6844 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6845 increased the number of multiplications necessary. */
6846 && TREE_CODE (arg10) != INTEGER_CST)
6848 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6849 build_int_cst (TREE_TYPE (arg00),
6854 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6859 return fold_build2_loc (loc, MULT_EXPR, type,
6860 fold_build2_loc (loc, code, type,
6861 fold_convert_loc (loc, type, alt0),
6862 fold_convert_loc (loc, type, alt1)),
6863 fold_convert_loc (loc, type, same));
6868 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6869 specified by EXPR into the buffer PTR of length LEN bytes.
6870 Return the number of bytes placed in the buffer, or zero
6874 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6876 tree type = TREE_TYPE (expr);
6877 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6878 int byte, offset, word, words;
6879 unsigned char value;
6881 if ((off == -1 && total_bytes > len)
6882 || off >= total_bytes)
6886 words = total_bytes / UNITS_PER_WORD;
6888 for (byte = 0; byte < total_bytes; byte++)
6890 int bitpos = byte * BITS_PER_UNIT;
6891 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6893 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
6895 if (total_bytes > UNITS_PER_WORD)
6897 word = byte / UNITS_PER_WORD;
6898 if (WORDS_BIG_ENDIAN)
6899 word = (words - 1) - word;
6900 offset = word * UNITS_PER_WORD;
6901 if (BYTES_BIG_ENDIAN)
6902 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6904 offset += byte % UNITS_PER_WORD;
6907 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6909 && offset - off < len)
6910 ptr[offset - off] = value;
6912 return MIN (len, total_bytes - off);
6916 /* Subroutine of native_encode_expr. Encode the FIXED_CST
6917 specified by EXPR into the buffer PTR of length LEN bytes.
6918 Return the number of bytes placed in the buffer, or zero
6922 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
6924 tree type = TREE_TYPE (expr);
6925 machine_mode mode = TYPE_MODE (type);
6926 int total_bytes = GET_MODE_SIZE (mode);
6927 FIXED_VALUE_TYPE value;
6928 tree i_value, i_type;
6930 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
6933 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
6935 if (NULL_TREE == i_type
6936 || TYPE_PRECISION (i_type) != total_bytes)
6939 value = TREE_FIXED_CST (expr);
6940 i_value = double_int_to_tree (i_type, value.data);
6942 return native_encode_int (i_value, ptr, len, off);
6946 /* Subroutine of native_encode_expr. Encode the REAL_CST
6947 specified by EXPR into the buffer PTR of length LEN bytes.
6948 Return the number of bytes placed in the buffer, or zero
6952 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
6954 tree type = TREE_TYPE (expr);
6955 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6956 int byte, offset, word, words, bitpos;
6957 unsigned char value;
6959 /* There are always 32 bits in each long, no matter the size of
6960 the hosts long. We handle floating point representations with
6964 if ((off == -1 && total_bytes > len)
6965 || off >= total_bytes)
6969 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
6971 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
6973 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
6974 bitpos += BITS_PER_UNIT)
6976 byte = (bitpos / BITS_PER_UNIT) & 3;
6977 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
6979 if (UNITS_PER_WORD < 4)
6981 word = byte / UNITS_PER_WORD;
6982 if (WORDS_BIG_ENDIAN)
6983 word = (words - 1) - word;
6984 offset = word * UNITS_PER_WORD;
6985 if (BYTES_BIG_ENDIAN)
6986 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6988 offset += byte % UNITS_PER_WORD;
6991 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
6992 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
6994 && offset - off < len)
6995 ptr[offset - off] = value;
6997 return MIN (len, total_bytes - off);
7000 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7001 specified by EXPR into the buffer PTR of length LEN bytes.
7002 Return the number of bytes placed in the buffer, or zero
7006 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7011 part = TREE_REALPART (expr);
7012 rsize = native_encode_expr (part, ptr, len, off);
7016 part = TREE_IMAGPART (expr);
7018 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7019 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7023 return rsize + isize;
7027 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7028 specified by EXPR into the buffer PTR of length LEN bytes.
7029 Return the number of bytes placed in the buffer, or zero
7033 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7040 count = VECTOR_CST_NELTS (expr);
7041 itype = TREE_TYPE (TREE_TYPE (expr));
7042 size = GET_MODE_SIZE (TYPE_MODE (itype));
7043 for (i = 0; i < count; i++)
7050 elem = VECTOR_CST_ELT (expr, i);
7051 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7052 if ((off == -1 && res != size)
7065 /* Subroutine of native_encode_expr. Encode the STRING_CST
7066 specified by EXPR into the buffer PTR of length LEN bytes.
7067 Return the number of bytes placed in the buffer, or zero
7071 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7073 tree type = TREE_TYPE (expr);
7074 HOST_WIDE_INT total_bytes;
7076 if (TREE_CODE (type) != ARRAY_TYPE
7077 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7078 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7079 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7081 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7082 if ((off == -1 && total_bytes > len)
7083 || off >= total_bytes)
7087 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7090 if (off < TREE_STRING_LENGTH (expr))
7092 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7093 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7095 memset (ptr + written, 0,
7096 MIN (total_bytes - written, len - written));
7099 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7100 return MIN (total_bytes - off, len);
7104 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7105 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7106 buffer PTR of length LEN bytes. If OFF is not -1 then start
7107 the encoding at byte offset OFF and encode at most LEN bytes.
7108 Return the number of bytes placed in the buffer, or zero upon failure. */
7111 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7113 switch (TREE_CODE (expr))
7116 return native_encode_int (expr, ptr, len, off);
7119 return native_encode_real (expr, ptr, len, off);
7122 return native_encode_fixed (expr, ptr, len, off);
7125 return native_encode_complex (expr, ptr, len, off);
7128 return native_encode_vector (expr, ptr, len, off);
7131 return native_encode_string (expr, ptr, len, off);
7139 /* Subroutine of native_interpret_expr. Interpret the contents of
7140 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7141 If the buffer cannot be interpreted, return NULL_TREE. */
7144 native_interpret_int (tree type, const unsigned char *ptr, int len)
7146 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7148 if (total_bytes > len
7149 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7152 wide_int result = wi::from_buffer (ptr, total_bytes);
7154 return wide_int_to_tree (type, result);
7158 /* Subroutine of native_interpret_expr. Interpret the contents of
7159 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7160 If the buffer cannot be interpreted, return NULL_TREE. */
7163 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7165 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7167 FIXED_VALUE_TYPE fixed_value;
7169 if (total_bytes > len
7170 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7173 result = double_int::from_buffer (ptr, total_bytes);
7174 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7176 return build_fixed (type, fixed_value);
7180 /* Subroutine of native_interpret_expr. Interpret the contents of
7181 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7182 If the buffer cannot be interpreted, return NULL_TREE. */
7185 native_interpret_real (tree type, const unsigned char *ptr, int len)
7187 machine_mode mode = TYPE_MODE (type);
7188 int total_bytes = GET_MODE_SIZE (mode);
7189 unsigned char value;
7190 /* There are always 32 bits in each long, no matter the size of
7191 the hosts long. We handle floating point representations with
7196 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7197 if (total_bytes > len || total_bytes > 24)
7199 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7201 memset (tmp, 0, sizeof (tmp));
7202 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7203 bitpos += BITS_PER_UNIT)
7205 /* Both OFFSET and BYTE index within a long;
7206 bitpos indexes the whole float. */
7207 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7208 if (UNITS_PER_WORD < 4)
7210 int word = byte / UNITS_PER_WORD;
7211 if (WORDS_BIG_ENDIAN)
7212 word = (words - 1) - word;
7213 offset = word * UNITS_PER_WORD;
7214 if (BYTES_BIG_ENDIAN)
7215 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7217 offset += byte % UNITS_PER_WORD;
7222 if (BYTES_BIG_ENDIAN)
7224 /* Reverse bytes within each long, or within the entire float
7225 if it's smaller than a long (for HFmode). */
7226 offset = MIN (3, total_bytes - 1) - offset;
7227 gcc_assert (offset >= 0);
7230 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7232 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7235 real_from_target (&r, tmp, mode);
7236 return build_real (type, r);
7240 /* Subroutine of native_interpret_expr. Interpret the contents of
7241 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7242 If the buffer cannot be interpreted, return NULL_TREE. */
7245 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7247 tree etype, rpart, ipart;
7250 etype = TREE_TYPE (type);
7251 size = GET_MODE_SIZE (TYPE_MODE (etype));
7254 rpart = native_interpret_expr (etype, ptr, size);
7257 ipart = native_interpret_expr (etype, ptr+size, size);
7260 return build_complex (type, rpart, ipart);
7264 /* Subroutine of native_interpret_expr. Interpret the contents of
7265 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7266 If the buffer cannot be interpreted, return NULL_TREE. */
7269 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7275 etype = TREE_TYPE (type);
7276 size = GET_MODE_SIZE (TYPE_MODE (etype));
7277 count = TYPE_VECTOR_SUBPARTS (type);
7278 if (size * count > len)
7281 elements = XALLOCAVEC (tree, count);
7282 for (i = count - 1; i >= 0; i--)
7284 elem = native_interpret_expr (etype, ptr+(i*size), size);
7289 return build_vector (type, elements);
7293 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7294 the buffer PTR of length LEN as a constant of type TYPE. For
7295 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7296 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7297 return NULL_TREE. */
7300 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7302 switch (TREE_CODE (type))
7308 case REFERENCE_TYPE:
7309 return native_interpret_int (type, ptr, len);
7312 return native_interpret_real (type, ptr, len);
7314 case FIXED_POINT_TYPE:
7315 return native_interpret_fixed (type, ptr, len);
7318 return native_interpret_complex (type, ptr, len);
7321 return native_interpret_vector (type, ptr, len);
7328 /* Returns true if we can interpret the contents of a native encoding
7332 can_native_interpret_type_p (tree type)
7334 switch (TREE_CODE (type))
7340 case REFERENCE_TYPE:
7341 case FIXED_POINT_TYPE:
7351 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7352 TYPE at compile-time. If we're unable to perform the conversion
7353 return NULL_TREE. */
7356 fold_view_convert_expr (tree type, tree expr)
7358 /* We support up to 512-bit values (for V8DFmode). */
7359 unsigned char buffer[64];
7362 /* Check that the host and target are sane. */
7363 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7366 len = native_encode_expr (expr, buffer, sizeof (buffer));
7370 return native_interpret_expr (type, buffer, len);
7373 /* Build an expression for the address of T. Folds away INDIRECT_REF
7374 to avoid confusing the gimplify process. */
7377 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7379 /* The size of the object is not relevant when talking about its address. */
7380 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7381 t = TREE_OPERAND (t, 0);
7383 if (TREE_CODE (t) == INDIRECT_REF)
7385 t = TREE_OPERAND (t, 0);
7387 if (TREE_TYPE (t) != ptrtype)
7388 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7390 else if (TREE_CODE (t) == MEM_REF
7391 && integer_zerop (TREE_OPERAND (t, 1)))
7392 return TREE_OPERAND (t, 0);
7393 else if (TREE_CODE (t) == MEM_REF
7394 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7395 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7396 TREE_OPERAND (t, 0),
7397 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7398 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7400 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7402 if (TREE_TYPE (t) != ptrtype)
7403 t = fold_convert_loc (loc, ptrtype, t);
7406 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7411 /* Build an expression for the address of T. */
7414 build_fold_addr_expr_loc (location_t loc, tree t)
7416 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7418 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7421 /* Fold a unary expression of code CODE and type TYPE with operand
7422 OP0. Return the folded expression if folding is successful.
7423 Otherwise, return NULL_TREE. */
7426 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7430 enum tree_code_class kind = TREE_CODE_CLASS (code);
7432 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7433 && TREE_CODE_LENGTH (code) == 1);
7438 if (CONVERT_EXPR_CODE_P (code)
7439 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7441 /* Don't use STRIP_NOPS, because signedness of argument type
7443 STRIP_SIGN_NOPS (arg0);
7447 /* Strip any conversions that don't change the mode. This
7448 is safe for every expression, except for a comparison
7449 expression because its signedness is derived from its
7452 Note that this is done as an internal manipulation within
7453 the constant folder, in order to find the simplest
7454 representation of the arguments so that their form can be
7455 studied. In any cases, the appropriate type conversions
7456 should be put back in the tree that will get out of the
7461 if (CONSTANT_CLASS_P (arg0))
7463 tree tem = const_unop (code, type, arg0);
7466 if (TREE_TYPE (tem) != type)
7467 tem = fold_convert_loc (loc, type, tem);
7473 tem = generic_simplify (loc, code, type, op0);
7477 if (TREE_CODE_CLASS (code) == tcc_unary)
7479 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7480 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7481 fold_build1_loc (loc, code, type,
7482 fold_convert_loc (loc, TREE_TYPE (op0),
7483 TREE_OPERAND (arg0, 1))));
7484 else if (TREE_CODE (arg0) == COND_EXPR)
7486 tree arg01 = TREE_OPERAND (arg0, 1);
7487 tree arg02 = TREE_OPERAND (arg0, 2);
7488 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7489 arg01 = fold_build1_loc (loc, code, type,
7490 fold_convert_loc (loc,
7491 TREE_TYPE (op0), arg01));
7492 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7493 arg02 = fold_build1_loc (loc, code, type,
7494 fold_convert_loc (loc,
7495 TREE_TYPE (op0), arg02));
7496 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7499 /* If this was a conversion, and all we did was to move into
7500 inside the COND_EXPR, bring it back out. But leave it if
7501 it is a conversion from integer to integer and the
7502 result precision is no wider than a word since such a
7503 conversion is cheap and may be optimized away by combine,
7504 while it couldn't if it were outside the COND_EXPR. Then return
7505 so we don't get into an infinite recursion loop taking the
7506 conversion out and then back in. */
7508 if ((CONVERT_EXPR_CODE_P (code)
7509 || code == NON_LVALUE_EXPR)
7510 && TREE_CODE (tem) == COND_EXPR
7511 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7512 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7513 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7514 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7515 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7516 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7517 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7519 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7520 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7521 || flag_syntax_only))
7522 tem = build1_loc (loc, code, type,
7524 TREE_TYPE (TREE_OPERAND
7525 (TREE_OPERAND (tem, 1), 0)),
7526 TREE_OPERAND (tem, 0),
7527 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7528 TREE_OPERAND (TREE_OPERAND (tem, 2),
7536 case NON_LVALUE_EXPR:
7537 if (!maybe_lvalue_p (op0))
7538 return fold_convert_loc (loc, type, op0);
7543 case FIX_TRUNC_EXPR:
7544 if (COMPARISON_CLASS_P (op0))
7546 /* If we have (type) (a CMP b) and type is an integral type, return
7547 new expression involving the new type. Canonicalize
7548 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7550 Do not fold the result as that would not simplify further, also
7551 folding again results in recursions. */
7552 if (TREE_CODE (type) == BOOLEAN_TYPE)
7553 return build2_loc (loc, TREE_CODE (op0), type,
7554 TREE_OPERAND (op0, 0),
7555 TREE_OPERAND (op0, 1));
7556 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7557 && TREE_CODE (type) != VECTOR_TYPE)
7558 return build3_loc (loc, COND_EXPR, type, op0,
7559 constant_boolean_node (true, type),
7560 constant_boolean_node (false, type));
7563 /* Handle (T *)&A.B.C for A being of type T and B and C
7564 living at offset zero. This occurs frequently in
7565 C++ upcasting and then accessing the base. */
7566 if (TREE_CODE (op0) == ADDR_EXPR
7567 && POINTER_TYPE_P (type)
7568 && handled_component_p (TREE_OPERAND (op0, 0)))
7570 HOST_WIDE_INT bitsize, bitpos;
7573 int unsignedp, volatilep;
7574 tree base = TREE_OPERAND (op0, 0);
7575 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7576 &mode, &unsignedp, &volatilep, false);
7577 /* If the reference was to a (constant) zero offset, we can use
7578 the address of the base if it has the same base type
7579 as the result type and the pointer type is unqualified. */
7580 if (! offset && bitpos == 0
7581 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7582 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7583 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7584 return fold_convert_loc (loc, type,
7585 build_fold_addr_expr_loc (loc, base));
7588 if (TREE_CODE (op0) == MODIFY_EXPR
7589 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7590 /* Detect assigning a bitfield. */
7591 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7593 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7595 /* Don't leave an assignment inside a conversion
7596 unless assigning a bitfield. */
7597 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7598 /* First do the assignment, then return converted constant. */
7599 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7600 TREE_NO_WARNING (tem) = 1;
7601 TREE_USED (tem) = 1;
7605 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7606 constants (if x has signed type, the sign bit cannot be set
7607 in c). This folds extension into the BIT_AND_EXPR.
7608 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7609 very likely don't have maximal range for their precision and this
7610 transformation effectively doesn't preserve non-maximal ranges. */
7611 if (TREE_CODE (type) == INTEGER_TYPE
7612 && TREE_CODE (op0) == BIT_AND_EXPR
7613 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7615 tree and_expr = op0;
7616 tree and0 = TREE_OPERAND (and_expr, 0);
7617 tree and1 = TREE_OPERAND (and_expr, 1);
7620 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7621 || (TYPE_PRECISION (type)
7622 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7624 else if (TYPE_PRECISION (TREE_TYPE (and1))
7625 <= HOST_BITS_PER_WIDE_INT
7626 && tree_fits_uhwi_p (and1))
7628 unsigned HOST_WIDE_INT cst;
7630 cst = tree_to_uhwi (and1);
7631 cst &= HOST_WIDE_INT_M1U
7632 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7633 change = (cst == 0);
7635 && !flag_syntax_only
7636 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7639 tree uns = unsigned_type_for (TREE_TYPE (and0));
7640 and0 = fold_convert_loc (loc, uns, and0);
7641 and1 = fold_convert_loc (loc, uns, and1);
7646 tem = force_fit_type (type, wi::to_widest (and1), 0,
7647 TREE_OVERFLOW (and1));
7648 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7649 fold_convert_loc (loc, type, and0), tem);
7653 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7654 when one of the new casts will fold away. Conservatively we assume
7655 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7656 if (POINTER_TYPE_P (type)
7657 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7658 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
7659 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7660 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7661 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7663 tree arg00 = TREE_OPERAND (arg0, 0);
7664 tree arg01 = TREE_OPERAND (arg0, 1);
7666 return fold_build_pointer_plus_loc
7667 (loc, fold_convert_loc (loc, type, arg00), arg01);
7670 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7671 of the same precision, and X is an integer type not narrower than
7672 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7673 if (INTEGRAL_TYPE_P (type)
7674 && TREE_CODE (op0) == BIT_NOT_EXPR
7675 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7676 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7677 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7679 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7680 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7681 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7682 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7683 fold_convert_loc (loc, type, tem));
7686 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7687 type of X and Y (integer types only). */
7688 if (INTEGRAL_TYPE_P (type)
7689 && TREE_CODE (op0) == MULT_EXPR
7690 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7691 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7693 /* Be careful not to introduce new overflows. */
7695 if (TYPE_OVERFLOW_WRAPS (type))
7698 mult_type = unsigned_type_for (type);
7700 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7702 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7703 fold_convert_loc (loc, mult_type,
7704 TREE_OPERAND (op0, 0)),
7705 fold_convert_loc (loc, mult_type,
7706 TREE_OPERAND (op0, 1)));
7707 return fold_convert_loc (loc, type, tem);
7713 case VIEW_CONVERT_EXPR:
7714 if (TREE_CODE (op0) == MEM_REF)
7715 return fold_build2_loc (loc, MEM_REF, type,
7716 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7721 tem = fold_negate_expr (loc, arg0);
7723 return fold_convert_loc (loc, type, tem);
7727 /* Convert fabs((double)float) into (double)fabsf(float). */
7728 if (TREE_CODE (arg0) == NOP_EXPR
7729 && TREE_CODE (type) == REAL_TYPE)
7731 tree targ0 = strip_float_extensions (arg0);
7733 return fold_convert_loc (loc, type,
7734 fold_build1_loc (loc, ABS_EXPR,
7739 /* Strip sign ops from argument. */
7740 if (TREE_CODE (type) == REAL_TYPE)
7742 tem = fold_strip_sign_ops (arg0);
7744 return fold_build1_loc (loc, ABS_EXPR, type,
7745 fold_convert_loc (loc, type, tem));
7750 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7751 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7752 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7753 fold_convert_loc (loc, type,
7754 TREE_OPERAND (arg0, 0)))))
7755 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7756 fold_convert_loc (loc, type,
7757 TREE_OPERAND (arg0, 1)));
7758 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7759 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7760 fold_convert_loc (loc, type,
7761 TREE_OPERAND (arg0, 1)))))
7762 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7763 fold_convert_loc (loc, type,
7764 TREE_OPERAND (arg0, 0)), tem);
7768 case TRUTH_NOT_EXPR:
7769 /* Note that the operand of this must be an int
7770 and its values must be 0 or 1.
7771 ("true" is a fixed value perhaps depending on the language,
7772 but we don't handle values other than 1 correctly yet.) */
7773 tem = fold_truth_not_expr (loc, arg0);
7776 return fold_convert_loc (loc, type, tem);
7779 /* Fold *&X to X if X is an lvalue. */
7780 if (TREE_CODE (op0) == ADDR_EXPR)
7782 tree op00 = TREE_OPERAND (op0, 0);
7783 if ((TREE_CODE (op00) == VAR_DECL
7784 || TREE_CODE (op00) == PARM_DECL
7785 || TREE_CODE (op00) == RESULT_DECL)
7786 && !TREE_READONLY (op00))
7793 } /* switch (code) */
7797 /* If the operation was a conversion do _not_ mark a resulting constant
7798 with TREE_OVERFLOW if the original constant was not. These conversions
7799 have implementation defined behavior and retaining the TREE_OVERFLOW
7800 flag here would confuse later passes such as VRP. */
7802 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7803 tree type, tree op0)
7805 tree res = fold_unary_loc (loc, code, type, op0);
7807 && TREE_CODE (res) == INTEGER_CST
7808 && TREE_CODE (op0) == INTEGER_CST
7809 && CONVERT_EXPR_CODE_P (code))
7810 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7815 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7816 operands OP0 and OP1. LOC is the location of the resulting expression.
7817 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7818 Return the folded expression if folding is successful. Otherwise,
7819 return NULL_TREE. */
7821 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7822 tree arg0, tree arg1, tree op0, tree op1)
7826 /* We only do these simplifications if we are optimizing. */
7830 /* Check for things like (A || B) && (A || C). We can convert this
7831 to A || (B && C). Note that either operator can be any of the four
7832 truth and/or operations and the transformation will still be
7833 valid. Also note that we only care about order for the
7834 ANDIF and ORIF operators. If B contains side effects, this
7835 might change the truth-value of A. */
7836 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7837 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7838 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7839 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7840 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7841 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7843 tree a00 = TREE_OPERAND (arg0, 0);
7844 tree a01 = TREE_OPERAND (arg0, 1);
7845 tree a10 = TREE_OPERAND (arg1, 0);
7846 tree a11 = TREE_OPERAND (arg1, 1);
7847 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7848 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7849 && (code == TRUTH_AND_EXPR
7850 || code == TRUTH_OR_EXPR));
7852 if (operand_equal_p (a00, a10, 0))
7853 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7854 fold_build2_loc (loc, code, type, a01, a11));
7855 else if (commutative && operand_equal_p (a00, a11, 0))
7856 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7857 fold_build2_loc (loc, code, type, a01, a10));
7858 else if (commutative && operand_equal_p (a01, a10, 0))
7859 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7860 fold_build2_loc (loc, code, type, a00, a11));
7862 /* This case if tricky because we must either have commutative
7863 operators or else A10 must not have side-effects. */
7865 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7866 && operand_equal_p (a01, a11, 0))
7867 return fold_build2_loc (loc, TREE_CODE (arg0), type,
7868 fold_build2_loc (loc, code, type, a00, a10),
7872 /* See if we can build a range comparison. */
7873 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
7876 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
7877 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
7879 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
7881 return fold_build2_loc (loc, code, type, tem, arg1);
7884 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
7885 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
7887 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
7889 return fold_build2_loc (loc, code, type, arg0, tem);
7892 /* Check for the possibility of merging component references. If our
7893 lhs is another similar operation, try to merge its rhs with our
7894 rhs. Then try to merge our lhs and rhs. */
7895 if (TREE_CODE (arg0) == code
7896 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
7897 TREE_OPERAND (arg0, 1), arg1)))
7898 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
7900 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
7903 if (LOGICAL_OP_NON_SHORT_CIRCUIT
7904 && (code == TRUTH_AND_EXPR
7905 || code == TRUTH_ANDIF_EXPR
7906 || code == TRUTH_OR_EXPR
7907 || code == TRUTH_ORIF_EXPR))
7909 enum tree_code ncode, icode;
7911 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
7912 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
7913 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
7915 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
7916 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
7917 We don't want to pack more than two leafs to a non-IF AND/OR
7919 If tree-code of left-hand operand isn't an AND/OR-IF code and not
7920 equal to IF-CODE, then we don't want to add right-hand operand.
7921 If the inner right-hand side of left-hand operand has
7922 side-effects, or isn't simple, then we can't add to it,
7923 as otherwise we might destroy if-sequence. */
7924 if (TREE_CODE (arg0) == icode
7925 && simple_operand_p_2 (arg1)
7926 /* Needed for sequence points to handle trappings, and
7928 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
7930 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
7932 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
7935 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
7936 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
7937 else if (TREE_CODE (arg1) == icode
7938 && simple_operand_p_2 (arg0)
7939 /* Needed for sequence points to handle trappings, and
7941 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
7943 tem = fold_build2_loc (loc, ncode, type,
7944 arg0, TREE_OPERAND (arg1, 0));
7945 return fold_build2_loc (loc, icode, type, tem,
7946 TREE_OPERAND (arg1, 1));
7948 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
7950 For sequence point consistancy, we need to check for trapping,
7951 and side-effects. */
7952 else if (code == icode && simple_operand_p_2 (arg0)
7953 && simple_operand_p_2 (arg1))
7954 return fold_build2_loc (loc, ncode, type, arg0, arg1);
7960 /* Fold a binary expression of code CODE and type TYPE with operands
7961 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
7962 Return the folded expression if folding is successful. Otherwise,
7963 return NULL_TREE. */
7966 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
7968 enum tree_code compl_code;
7970 if (code == MIN_EXPR)
7971 compl_code = MAX_EXPR;
7972 else if (code == MAX_EXPR)
7973 compl_code = MIN_EXPR;
7977 /* MIN (MAX (a, b), b) == b. */
7978 if (TREE_CODE (op0) == compl_code
7979 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
7980 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
7982 /* MIN (MAX (b, a), b) == b. */
7983 if (TREE_CODE (op0) == compl_code
7984 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
7985 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
7986 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
7988 /* MIN (a, MAX (a, b)) == a. */
7989 if (TREE_CODE (op1) == compl_code
7990 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
7991 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
7992 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
7994 /* MIN (a, MAX (b, a)) == a. */
7995 if (TREE_CODE (op1) == compl_code
7996 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
7997 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
7998 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8003 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8004 by changing CODE to reduce the magnitude of constants involved in
8005 ARG0 of the comparison.
8006 Returns a canonicalized comparison tree if a simplification was
8007 possible, otherwise returns NULL_TREE.
8008 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8009 valid if signed overflow is undefined. */
8012 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8013 tree arg0, tree arg1,
8014 bool *strict_overflow_p)
8016 enum tree_code code0 = TREE_CODE (arg0);
8017 tree t, cst0 = NULL_TREE;
8020 /* Match A +- CST code arg1. We can change this only if overflow
8022 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8024 /* In principle pointers also have undefined overflow behavior,
8025 but that causes problems elsewhere. */
8026 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8027 && (code0 == MINUS_EXPR
8028 || code0 == PLUS_EXPR)
8029 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8032 /* Identify the constant in arg0 and its sign. */
8033 cst0 = TREE_OPERAND (arg0, 1);
8034 sgn0 = tree_int_cst_sgn (cst0);
8036 /* Overflowed constants and zero will cause problems. */
8037 if (integer_zerop (cst0)
8038 || TREE_OVERFLOW (cst0))
8041 /* See if we can reduce the magnitude of the constant in
8042 arg0 by changing the comparison code. */
8043 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8045 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8047 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8048 else if (code == GT_EXPR
8049 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8051 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8052 else if (code == LE_EXPR
8053 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8055 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8056 else if (code == GE_EXPR
8057 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8061 *strict_overflow_p = true;
8063 /* Now build the constant reduced in magnitude. But not if that
8064 would produce one outside of its types range. */
8065 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8067 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8068 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8070 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8071 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8074 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8075 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8076 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8077 t = fold_convert (TREE_TYPE (arg1), t);
8079 return fold_build2_loc (loc, code, type, t, arg1);
8082 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8083 overflow further. Try to decrease the magnitude of constants involved
8084 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8085 and put sole constants at the second argument position.
8086 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8089 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8090 tree arg0, tree arg1)
8093 bool strict_overflow_p;
8094 const char * const warnmsg = G_("assuming signed overflow does not occur "
8095 "when reducing constant in comparison");
8097 /* Try canonicalization by simplifying arg0. */
8098 strict_overflow_p = false;
8099 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8100 &strict_overflow_p);
8103 if (strict_overflow_p)
8104 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8108 /* Try canonicalization by simplifying arg1 using the swapped
8110 code = swap_tree_comparison (code);
8111 strict_overflow_p = false;
8112 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8113 &strict_overflow_p);
8114 if (t && strict_overflow_p)
8115 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8119 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8120 space. This is used to avoid issuing overflow warnings for
8121 expressions like &p->x which can not wrap. */
8124 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8126 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8133 int precision = TYPE_PRECISION (TREE_TYPE (base));
8134 if (offset == NULL_TREE)
8135 wi_offset = wi::zero (precision);
8136 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8142 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8143 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8147 if (!wi::fits_uhwi_p (total))
8150 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8154 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8156 if (TREE_CODE (base) == ADDR_EXPR)
8158 HOST_WIDE_INT base_size;
8160 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8161 if (base_size > 0 && size < base_size)
8165 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8168 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8169 kind INTEGER_CST. This makes sure to properly sign-extend the
8172 static HOST_WIDE_INT
8173 size_low_cst (const_tree t)
8175 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8176 int prec = TYPE_PRECISION (TREE_TYPE (t));
8177 if (prec < HOST_BITS_PER_WIDE_INT)
8178 return sext_hwi (w, prec);
8182 /* Subroutine of fold_binary. This routine performs all of the
8183 transformations that are common to the equality/inequality
8184 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8185 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8186 fold_binary should call fold_binary. Fold a comparison with
8187 tree code CODE and type TYPE with operands OP0 and OP1. Return
8188 the folded comparison or NULL_TREE. */
8191 fold_comparison (location_t loc, enum tree_code code, tree type,
8194 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8195 tree arg0, arg1, tem;
8200 STRIP_SIGN_NOPS (arg0);
8201 STRIP_SIGN_NOPS (arg1);
8203 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8204 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8206 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8207 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8208 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8209 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8210 && TREE_CODE (arg1) == INTEGER_CST
8211 && !TREE_OVERFLOW (arg1))
8213 const enum tree_code
8214 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8215 tree const1 = TREE_OPERAND (arg0, 1);
8216 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8217 tree variable = TREE_OPERAND (arg0, 0);
8218 tree new_const = int_const_binop (reverse_op, const2, const1);
8220 /* If the constant operation overflowed this can be
8221 simplified as a comparison against INT_MAX/INT_MIN. */
8222 if (TREE_OVERFLOW (new_const)
8223 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8225 int const1_sgn = tree_int_cst_sgn (const1);
8226 enum tree_code code2 = code;
8228 /* Get the sign of the constant on the lhs if the
8229 operation were VARIABLE + CONST1. */
8230 if (TREE_CODE (arg0) == MINUS_EXPR)
8231 const1_sgn = -const1_sgn;
8233 /* The sign of the constant determines if we overflowed
8234 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8235 Canonicalize to the INT_MIN overflow by swapping the comparison
8237 if (const1_sgn == -1)
8238 code2 = swap_tree_comparison (code);
8240 /* We now can look at the canonicalized case
8241 VARIABLE + 1 CODE2 INT_MIN
8242 and decide on the result. */
8249 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8255 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8264 fold_overflow_warning ("assuming signed overflow does not occur "
8265 "when changing X +- C1 cmp C2 to "
8267 WARN_STRICT_OVERFLOW_COMPARISON);
8268 return fold_build2_loc (loc, code, type, variable, new_const);
8272 /* For comparisons of pointers we can decompose it to a compile time
8273 comparison of the base objects and the offsets into the object.
8274 This requires at least one operand being an ADDR_EXPR or a
8275 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8276 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8277 && (TREE_CODE (arg0) == ADDR_EXPR
8278 || TREE_CODE (arg1) == ADDR_EXPR
8279 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8280 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8282 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8283 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8285 int volatilep, unsignedp;
8286 bool indirect_base0 = false, indirect_base1 = false;
8288 /* Get base and offset for the access. Strip ADDR_EXPR for
8289 get_inner_reference, but put it back by stripping INDIRECT_REF
8290 off the base object if possible. indirect_baseN will be true
8291 if baseN is not an address but refers to the object itself. */
8293 if (TREE_CODE (arg0) == ADDR_EXPR)
8295 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8296 &bitsize, &bitpos0, &offset0, &mode,
8297 &unsignedp, &volatilep, false);
8298 if (TREE_CODE (base0) == INDIRECT_REF)
8299 base0 = TREE_OPERAND (base0, 0);
8301 indirect_base0 = true;
8303 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8305 base0 = TREE_OPERAND (arg0, 0);
8306 STRIP_SIGN_NOPS (base0);
8307 if (TREE_CODE (base0) == ADDR_EXPR)
8309 base0 = TREE_OPERAND (base0, 0);
8310 indirect_base0 = true;
8312 offset0 = TREE_OPERAND (arg0, 1);
8313 if (tree_fits_shwi_p (offset0))
8315 HOST_WIDE_INT off = size_low_cst (offset0);
8316 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8318 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8320 bitpos0 = off * BITS_PER_UNIT;
8321 offset0 = NULL_TREE;
8327 if (TREE_CODE (arg1) == ADDR_EXPR)
8329 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8330 &bitsize, &bitpos1, &offset1, &mode,
8331 &unsignedp, &volatilep, false);
8332 if (TREE_CODE (base1) == INDIRECT_REF)
8333 base1 = TREE_OPERAND (base1, 0);
8335 indirect_base1 = true;
8337 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8339 base1 = TREE_OPERAND (arg1, 0);
8340 STRIP_SIGN_NOPS (base1);
8341 if (TREE_CODE (base1) == ADDR_EXPR)
8343 base1 = TREE_OPERAND (base1, 0);
8344 indirect_base1 = true;
8346 offset1 = TREE_OPERAND (arg1, 1);
8347 if (tree_fits_shwi_p (offset1))
8349 HOST_WIDE_INT off = size_low_cst (offset1);
8350 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8352 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8354 bitpos1 = off * BITS_PER_UNIT;
8355 offset1 = NULL_TREE;
8360 /* If we have equivalent bases we might be able to simplify. */
8361 if (indirect_base0 == indirect_base1
8362 && operand_equal_p (base0, base1, 0))
8364 /* We can fold this expression to a constant if the non-constant
8365 offset parts are equal. */
8366 if ((offset0 == offset1
8367 || (offset0 && offset1
8368 && operand_equal_p (offset0, offset1, 0)))
8371 || (indirect_base0 && DECL_P (base0))
8372 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8376 && bitpos0 != bitpos1
8377 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8378 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8379 fold_overflow_warning (("assuming pointer wraparound does not "
8380 "occur when comparing P +- C1 with "
8382 WARN_STRICT_OVERFLOW_CONDITIONAL);
8387 return constant_boolean_node (bitpos0 == bitpos1, type);
8389 return constant_boolean_node (bitpos0 != bitpos1, type);
8391 return constant_boolean_node (bitpos0 < bitpos1, type);
8393 return constant_boolean_node (bitpos0 <= bitpos1, type);
8395 return constant_boolean_node (bitpos0 >= bitpos1, type);
8397 return constant_boolean_node (bitpos0 > bitpos1, type);
8401 /* We can simplify the comparison to a comparison of the variable
8402 offset parts if the constant offset parts are equal.
8403 Be careful to use signed sizetype here because otherwise we
8404 mess with array offsets in the wrong way. This is possible
8405 because pointer arithmetic is restricted to retain within an
8406 object and overflow on pointer differences is undefined as of
8407 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8408 else if (bitpos0 == bitpos1
8410 || (indirect_base0 && DECL_P (base0))
8411 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8413 /* By converting to signed sizetype we cover middle-end pointer
8414 arithmetic which operates on unsigned pointer types of size
8415 type size and ARRAY_REF offsets which are properly sign or
8416 zero extended from their type in case it is narrower than
8418 if (offset0 == NULL_TREE)
8419 offset0 = build_int_cst (ssizetype, 0);
8421 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8422 if (offset1 == NULL_TREE)
8423 offset1 = build_int_cst (ssizetype, 0);
8425 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8428 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8429 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8430 fold_overflow_warning (("assuming pointer wraparound does not "
8431 "occur when comparing P +- C1 with "
8433 WARN_STRICT_OVERFLOW_COMPARISON);
8435 return fold_build2_loc (loc, code, type, offset0, offset1);
8438 /* For equal offsets we can simplify to a comparison of the
8440 else if (bitpos0 == bitpos1
8442 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8444 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8445 && ((offset0 == offset1)
8446 || (offset0 && offset1
8447 && operand_equal_p (offset0, offset1, 0))))
8450 base0 = build_fold_addr_expr_loc (loc, base0);
8452 base1 = build_fold_addr_expr_loc (loc, base1);
8453 return fold_build2_loc (loc, code, type, base0, base1);
8457 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8458 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8459 the resulting offset is smaller in absolute value than the
8460 original one and has the same sign. */
8461 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8462 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8463 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8464 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8465 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8466 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8467 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8468 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8470 tree const1 = TREE_OPERAND (arg0, 1);
8471 tree const2 = TREE_OPERAND (arg1, 1);
8472 tree variable1 = TREE_OPERAND (arg0, 0);
8473 tree variable2 = TREE_OPERAND (arg1, 0);
8475 const char * const warnmsg = G_("assuming signed overflow does not "
8476 "occur when combining constants around "
8479 /* Put the constant on the side where it doesn't overflow and is
8480 of lower absolute value and of same sign than before. */
8481 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8482 ? MINUS_EXPR : PLUS_EXPR,
8484 if (!TREE_OVERFLOW (cst)
8485 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8486 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8488 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8489 return fold_build2_loc (loc, code, type,
8491 fold_build2_loc (loc, TREE_CODE (arg1),
8496 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8497 ? MINUS_EXPR : PLUS_EXPR,
8499 if (!TREE_OVERFLOW (cst)
8500 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8501 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8503 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8504 return fold_build2_loc (loc, code, type,
8505 fold_build2_loc (loc, TREE_CODE (arg0),
8512 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8516 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8517 constant, we can simplify it. */
8518 if (TREE_CODE (arg1) == INTEGER_CST
8519 && (TREE_CODE (arg0) == MIN_EXPR
8520 || TREE_CODE (arg0) == MAX_EXPR)
8521 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8523 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8528 /* If we are comparing an expression that just has comparisons
8529 of two integer values, arithmetic expressions of those comparisons,
8530 and constants, we can simplify it. There are only three cases
8531 to check: the two values can either be equal, the first can be
8532 greater, or the second can be greater. Fold the expression for
8533 those three values. Since each value must be 0 or 1, we have
8534 eight possibilities, each of which corresponds to the constant 0
8535 or 1 or one of the six possible comparisons.
8537 This handles common cases like (a > b) == 0 but also handles
8538 expressions like ((x > y) - (y > x)) > 0, which supposedly
8539 occur in macroized code. */
8541 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8543 tree cval1 = 0, cval2 = 0;
8546 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8547 /* Don't handle degenerate cases here; they should already
8548 have been handled anyway. */
8549 && cval1 != 0 && cval2 != 0
8550 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8551 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8552 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8553 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8554 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8555 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8556 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8558 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8559 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8561 /* We can't just pass T to eval_subst in case cval1 or cval2
8562 was the same as ARG1. */
8565 = fold_build2_loc (loc, code, type,
8566 eval_subst (loc, arg0, cval1, maxval,
8570 = fold_build2_loc (loc, code, type,
8571 eval_subst (loc, arg0, cval1, maxval,
8575 = fold_build2_loc (loc, code, type,
8576 eval_subst (loc, arg0, cval1, minval,
8580 /* All three of these results should be 0 or 1. Confirm they are.
8581 Then use those values to select the proper code to use. */
8583 if (TREE_CODE (high_result) == INTEGER_CST
8584 && TREE_CODE (equal_result) == INTEGER_CST
8585 && TREE_CODE (low_result) == INTEGER_CST)
8587 /* Make a 3-bit mask with the high-order bit being the
8588 value for `>', the next for '=', and the low for '<'. */
8589 switch ((integer_onep (high_result) * 4)
8590 + (integer_onep (equal_result) * 2)
8591 + integer_onep (low_result))
8595 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8616 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8621 tem = save_expr (build2 (code, type, cval1, cval2));
8622 SET_EXPR_LOCATION (tem, loc);
8625 return fold_build2_loc (loc, code, type, cval1, cval2);
8630 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8631 into a single range test. */
8632 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8633 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8634 && TREE_CODE (arg1) == INTEGER_CST
8635 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8636 && !integer_zerop (TREE_OPERAND (arg0, 1))
8637 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8638 && !TREE_OVERFLOW (arg1))
8640 tem = fold_div_compare (loc, code, type, arg0, arg1);
8641 if (tem != NULL_TREE)
8649 /* Subroutine of fold_binary. Optimize complex multiplications of the
8650 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8651 argument EXPR represents the expression "z" of type TYPE. */
8654 fold_mult_zconjz (location_t loc, tree type, tree expr)
8656 tree itype = TREE_TYPE (type);
8657 tree rpart, ipart, tem;
8659 if (TREE_CODE (expr) == COMPLEX_EXPR)
8661 rpart = TREE_OPERAND (expr, 0);
8662 ipart = TREE_OPERAND (expr, 1);
8664 else if (TREE_CODE (expr) == COMPLEX_CST)
8666 rpart = TREE_REALPART (expr);
8667 ipart = TREE_IMAGPART (expr);
8671 expr = save_expr (expr);
8672 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8673 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8676 rpart = save_expr (rpart);
8677 ipart = save_expr (ipart);
8678 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8679 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8680 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8681 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8682 build_zero_cst (itype));
8686 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8687 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8690 vec_cst_ctor_to_array (tree arg, tree *elts)
8692 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8694 if (TREE_CODE (arg) == VECTOR_CST)
8696 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8697 elts[i] = VECTOR_CST_ELT (arg, i);
8699 else if (TREE_CODE (arg) == CONSTRUCTOR)
8701 constructor_elt *elt;
8703 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8704 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8707 elts[i] = elt->value;
8711 for (; i < nelts; i++)
8713 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8717 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8718 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8719 NULL_TREE otherwise. */
8722 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8724 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8726 bool need_ctor = false;
8728 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8729 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8730 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8731 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8734 elts = XALLOCAVEC (tree, nelts * 3);
8735 if (!vec_cst_ctor_to_array (arg0, elts)
8736 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8739 for (i = 0; i < nelts; i++)
8741 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8743 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8748 vec<constructor_elt, va_gc> *v;
8749 vec_alloc (v, nelts);
8750 for (i = 0; i < nelts; i++)
8751 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8752 return build_constructor (type, v);
8755 return build_vector (type, &elts[2 * nelts]);
8758 /* Try to fold a pointer difference of type TYPE two address expressions of
8759 array references AREF0 and AREF1 using location LOC. Return a
8760 simplified expression for the difference or NULL_TREE. */
8763 fold_addr_of_array_ref_difference (location_t loc, tree type,
8764 tree aref0, tree aref1)
8766 tree base0 = TREE_OPERAND (aref0, 0);
8767 tree base1 = TREE_OPERAND (aref1, 0);
8768 tree base_offset = build_int_cst (type, 0);
8770 /* If the bases are array references as well, recurse. If the bases
8771 are pointer indirections compute the difference of the pointers.
8772 If the bases are equal, we are set. */
8773 if ((TREE_CODE (base0) == ARRAY_REF
8774 && TREE_CODE (base1) == ARRAY_REF
8776 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8777 || (INDIRECT_REF_P (base0)
8778 && INDIRECT_REF_P (base1)
8779 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
8780 TREE_OPERAND (base0, 0),
8781 TREE_OPERAND (base1, 0))))
8782 || operand_equal_p (base0, base1, 0))
8784 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8785 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8786 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8787 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8788 return fold_build2_loc (loc, PLUS_EXPR, type,
8790 fold_build2_loc (loc, MULT_EXPR, type,
8796 /* If the real or vector real constant CST of type TYPE has an exact
8797 inverse, return it, else return NULL. */
8800 exact_inverse (tree type, tree cst)
8803 tree unit_type, *elts;
8805 unsigned vec_nelts, i;
8807 switch (TREE_CODE (cst))
8810 r = TREE_REAL_CST (cst);
8812 if (exact_real_inverse (TYPE_MODE (type), &r))
8813 return build_real (type, r);
8818 vec_nelts = VECTOR_CST_NELTS (cst);
8819 elts = XALLOCAVEC (tree, vec_nelts);
8820 unit_type = TREE_TYPE (type);
8821 mode = TYPE_MODE (unit_type);
8823 for (i = 0; i < vec_nelts; i++)
8825 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8826 if (!exact_real_inverse (mode, &r))
8828 elts[i] = build_real (unit_type, r);
8831 return build_vector (type, elts);
8838 /* Mask out the tz least significant bits of X of type TYPE where
8839 tz is the number of trailing zeroes in Y. */
8841 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8843 int tz = wi::ctz (y);
8845 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8849 /* Return true when T is an address and is known to be nonzero.
8850 For floating point we further ensure that T is not denormal.
8851 Similar logic is present in nonzero_address in rtlanal.h.
8853 If the return value is based on the assumption that signed overflow
8854 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8855 change *STRICT_OVERFLOW_P. */
8858 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8860 tree type = TREE_TYPE (t);
8861 enum tree_code code;
8863 /* Doing something useful for floating point would need more work. */
8864 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8867 code = TREE_CODE (t);
8868 switch (TREE_CODE_CLASS (code))
8871 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8874 case tcc_comparison:
8875 return tree_binary_nonzero_warnv_p (code, type,
8876 TREE_OPERAND (t, 0),
8877 TREE_OPERAND (t, 1),
8880 case tcc_declaration:
8882 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8890 case TRUTH_NOT_EXPR:
8891 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8894 case TRUTH_AND_EXPR:
8896 case TRUTH_XOR_EXPR:
8897 return tree_binary_nonzero_warnv_p (code, type,
8898 TREE_OPERAND (t, 0),
8899 TREE_OPERAND (t, 1),
8907 case WITH_SIZE_EXPR:
8909 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8914 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8918 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8923 tree fndecl = get_callee_fndecl (t);
8924 if (!fndecl) return false;
8925 if (flag_delete_null_pointer_checks && !flag_check_new
8926 && DECL_IS_OPERATOR_NEW (fndecl)
8927 && !TREE_NOTHROW (fndecl))
8929 if (flag_delete_null_pointer_checks
8930 && lookup_attribute ("returns_nonnull",
8931 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
8933 return alloca_call_p (t);
8942 /* Return true when T is an address and is known to be nonzero.
8943 Handle warnings about undefined signed overflow. */
8946 tree_expr_nonzero_p (tree t)
8948 bool ret, strict_overflow_p;
8950 strict_overflow_p = false;
8951 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
8952 if (strict_overflow_p)
8953 fold_overflow_warning (("assuming signed overflow does not occur when "
8954 "determining that expression is always "
8956 WARN_STRICT_OVERFLOW_MISC);
8960 /* Fold a binary expression of code CODE and type TYPE with operands
8961 OP0 and OP1. LOC is the location of the resulting expression.
8962 Return the folded expression if folding is successful. Otherwise,
8963 return NULL_TREE. */
8966 fold_binary_loc (location_t loc,
8967 enum tree_code code, tree type, tree op0, tree op1)
8969 enum tree_code_class kind = TREE_CODE_CLASS (code);
8970 tree arg0, arg1, tem;
8971 tree t1 = NULL_TREE;
8972 bool strict_overflow_p;
8975 gcc_assert (IS_EXPR_CODE_CLASS (kind)
8976 && TREE_CODE_LENGTH (code) == 2
8978 && op1 != NULL_TREE);
8983 /* Strip any conversions that don't change the mode. This is
8984 safe for every expression, except for a comparison expression
8985 because its signedness is derived from its operands. So, in
8986 the latter case, only strip conversions that don't change the
8987 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
8990 Note that this is done as an internal manipulation within the
8991 constant folder, in order to find the simplest representation
8992 of the arguments so that their form can be studied. In any
8993 cases, the appropriate type conversions should be put back in
8994 the tree that will get out of the constant folder. */
8996 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
8998 STRIP_SIGN_NOPS (arg0);
8999 STRIP_SIGN_NOPS (arg1);
9007 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9008 constant but we can't do arithmetic on them. */
9009 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9011 tem = const_binop (code, type, arg0, arg1);
9012 if (tem != NULL_TREE)
9014 if (TREE_TYPE (tem) != type)
9015 tem = fold_convert_loc (loc, type, tem);
9020 /* If this is a commutative operation, and ARG0 is a constant, move it
9021 to ARG1 to reduce the number of tests below. */
9022 if (commutative_tree_code (code)
9023 && tree_swap_operands_p (arg0, arg1, true))
9024 return fold_build2_loc (loc, code, type, op1, op0);
9026 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9027 to ARG1 to reduce the number of tests below. */
9028 if (kind == tcc_comparison
9029 && tree_swap_operands_p (arg0, arg1, true))
9030 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9032 tem = generic_simplify (loc, code, type, op0, op1);
9036 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9038 First check for cases where an arithmetic operation is applied to a
9039 compound, conditional, or comparison operation. Push the arithmetic
9040 operation inside the compound or conditional to see if any folding
9041 can then be done. Convert comparison to conditional for this purpose.
9042 The also optimizes non-constant cases that used to be done in
9045 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9046 one of the operands is a comparison and the other is a comparison, a
9047 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9048 code below would make the expression more complex. Change it to a
9049 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9050 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9052 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9053 || code == EQ_EXPR || code == NE_EXPR)
9054 && TREE_CODE (type) != VECTOR_TYPE
9055 && ((truth_value_p (TREE_CODE (arg0))
9056 && (truth_value_p (TREE_CODE (arg1))
9057 || (TREE_CODE (arg1) == BIT_AND_EXPR
9058 && integer_onep (TREE_OPERAND (arg1, 1)))))
9059 || (truth_value_p (TREE_CODE (arg1))
9060 && (truth_value_p (TREE_CODE (arg0))
9061 || (TREE_CODE (arg0) == BIT_AND_EXPR
9062 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9064 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9065 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9068 fold_convert_loc (loc, boolean_type_node, arg0),
9069 fold_convert_loc (loc, boolean_type_node, arg1));
9071 if (code == EQ_EXPR)
9072 tem = invert_truthvalue_loc (loc, tem);
9074 return fold_convert_loc (loc, type, tem);
9077 if (TREE_CODE_CLASS (code) == tcc_binary
9078 || TREE_CODE_CLASS (code) == tcc_comparison)
9080 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9082 tem = fold_build2_loc (loc, code, type,
9083 fold_convert_loc (loc, TREE_TYPE (op0),
9084 TREE_OPERAND (arg0, 1)), op1);
9085 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9088 if (TREE_CODE (arg1) == COMPOUND_EXPR
9089 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9091 tem = fold_build2_loc (loc, code, type, op0,
9092 fold_convert_loc (loc, TREE_TYPE (op1),
9093 TREE_OPERAND (arg1, 1)));
9094 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9098 if (TREE_CODE (arg0) == COND_EXPR
9099 || TREE_CODE (arg0) == VEC_COND_EXPR
9100 || COMPARISON_CLASS_P (arg0))
9102 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9104 /*cond_first_p=*/1);
9105 if (tem != NULL_TREE)
9109 if (TREE_CODE (arg1) == COND_EXPR
9110 || TREE_CODE (arg1) == VEC_COND_EXPR
9111 || COMPARISON_CLASS_P (arg1))
9113 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9115 /*cond_first_p=*/0);
9116 if (tem != NULL_TREE)
9124 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9125 if (TREE_CODE (arg0) == ADDR_EXPR
9126 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9128 tree iref = TREE_OPERAND (arg0, 0);
9129 return fold_build2 (MEM_REF, type,
9130 TREE_OPERAND (iref, 0),
9131 int_const_binop (PLUS_EXPR, arg1,
9132 TREE_OPERAND (iref, 1)));
9135 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9136 if (TREE_CODE (arg0) == ADDR_EXPR
9137 && handled_component_p (TREE_OPERAND (arg0, 0)))
9140 HOST_WIDE_INT coffset;
9141 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9145 return fold_build2 (MEM_REF, type,
9146 build_fold_addr_expr (base),
9147 int_const_binop (PLUS_EXPR, arg1,
9148 size_int (coffset)));
9153 case POINTER_PLUS_EXPR:
9154 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9155 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9156 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9157 return fold_convert_loc (loc, type,
9158 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9159 fold_convert_loc (loc, sizetype,
9161 fold_convert_loc (loc, sizetype,
9167 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9169 /* X + (X / CST) * -CST is X % CST. */
9170 if (TREE_CODE (arg1) == MULT_EXPR
9171 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9172 && operand_equal_p (arg0,
9173 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9175 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9176 tree cst1 = TREE_OPERAND (arg1, 1);
9177 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9179 if (sum && integer_zerop (sum))
9180 return fold_convert_loc (loc, type,
9181 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9182 TREE_TYPE (arg0), arg0,
9187 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9188 one. Make sure the type is not saturating and has the signedness of
9189 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9190 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9191 if ((TREE_CODE (arg0) == MULT_EXPR
9192 || TREE_CODE (arg1) == MULT_EXPR)
9193 && !TYPE_SATURATING (type)
9194 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9195 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9196 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9198 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9203 if (! FLOAT_TYPE_P (type))
9205 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9206 (plus (plus (mult) (mult)) (foo)) so that we can
9207 take advantage of the factoring cases below. */
9208 if (ANY_INTEGRAL_TYPE_P (type)
9209 && TYPE_OVERFLOW_WRAPS (type)
9210 && (((TREE_CODE (arg0) == PLUS_EXPR
9211 || TREE_CODE (arg0) == MINUS_EXPR)
9212 && TREE_CODE (arg1) == MULT_EXPR)
9213 || ((TREE_CODE (arg1) == PLUS_EXPR
9214 || TREE_CODE (arg1) == MINUS_EXPR)
9215 && TREE_CODE (arg0) == MULT_EXPR)))
9217 tree parg0, parg1, parg, marg;
9218 enum tree_code pcode;
9220 if (TREE_CODE (arg1) == MULT_EXPR)
9221 parg = arg0, marg = arg1;
9223 parg = arg1, marg = arg0;
9224 pcode = TREE_CODE (parg);
9225 parg0 = TREE_OPERAND (parg, 0);
9226 parg1 = TREE_OPERAND (parg, 1);
9230 if (TREE_CODE (parg0) == MULT_EXPR
9231 && TREE_CODE (parg1) != MULT_EXPR)
9232 return fold_build2_loc (loc, pcode, type,
9233 fold_build2_loc (loc, PLUS_EXPR, type,
9234 fold_convert_loc (loc, type,
9236 fold_convert_loc (loc, type,
9238 fold_convert_loc (loc, type, parg1));
9239 if (TREE_CODE (parg0) != MULT_EXPR
9240 && TREE_CODE (parg1) == MULT_EXPR)
9242 fold_build2_loc (loc, PLUS_EXPR, type,
9243 fold_convert_loc (loc, type, parg0),
9244 fold_build2_loc (loc, pcode, type,
9245 fold_convert_loc (loc, type, marg),
9246 fold_convert_loc (loc, type,
9252 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9253 to __complex__ ( x, y ). This is not the same for SNaNs or
9254 if signed zeros are involved. */
9255 if (!HONOR_SNANS (element_mode (arg0))
9256 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9257 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9259 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9260 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9261 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9262 bool arg0rz = false, arg0iz = false;
9263 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9264 || (arg0i && (arg0iz = real_zerop (arg0i))))
9266 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9267 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9268 if (arg0rz && arg1i && real_zerop (arg1i))
9270 tree rp = arg1r ? arg1r
9271 : build1 (REALPART_EXPR, rtype, arg1);
9272 tree ip = arg0i ? arg0i
9273 : build1 (IMAGPART_EXPR, rtype, arg0);
9274 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9276 else if (arg0iz && arg1r && real_zerop (arg1r))
9278 tree rp = arg0r ? arg0r
9279 : build1 (REALPART_EXPR, rtype, arg0);
9280 tree ip = arg1i ? arg1i
9281 : build1 (IMAGPART_EXPR, rtype, arg1);
9282 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9287 if (flag_unsafe_math_optimizations
9288 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9289 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9290 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9293 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9294 We associate floats only if the user has specified
9295 -fassociative-math. */
9296 if (flag_associative_math
9297 && TREE_CODE (arg1) == PLUS_EXPR
9298 && TREE_CODE (arg0) != MULT_EXPR)
9300 tree tree10 = TREE_OPERAND (arg1, 0);
9301 tree tree11 = TREE_OPERAND (arg1, 1);
9302 if (TREE_CODE (tree11) == MULT_EXPR
9303 && TREE_CODE (tree10) == MULT_EXPR)
9306 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9307 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9310 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9311 We associate floats only if the user has specified
9312 -fassociative-math. */
9313 if (flag_associative_math
9314 && TREE_CODE (arg0) == PLUS_EXPR
9315 && TREE_CODE (arg1) != MULT_EXPR)
9317 tree tree00 = TREE_OPERAND (arg0, 0);
9318 tree tree01 = TREE_OPERAND (arg0, 1);
9319 if (TREE_CODE (tree01) == MULT_EXPR
9320 && TREE_CODE (tree00) == MULT_EXPR)
9323 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9324 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9330 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9331 is a rotate of A by C1 bits. */
9332 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9333 is a rotate of A by B bits. */
9335 enum tree_code code0, code1;
9337 code0 = TREE_CODE (arg0);
9338 code1 = TREE_CODE (arg1);
9339 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9340 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9341 && operand_equal_p (TREE_OPERAND (arg0, 0),
9342 TREE_OPERAND (arg1, 0), 0)
9343 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9344 TYPE_UNSIGNED (rtype))
9345 /* Only create rotates in complete modes. Other cases are not
9346 expanded properly. */
9347 && (element_precision (rtype)
9348 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9350 tree tree01, tree11;
9351 enum tree_code code01, code11;
9353 tree01 = TREE_OPERAND (arg0, 1);
9354 tree11 = TREE_OPERAND (arg1, 1);
9355 STRIP_NOPS (tree01);
9356 STRIP_NOPS (tree11);
9357 code01 = TREE_CODE (tree01);
9358 code11 = TREE_CODE (tree11);
9359 if (code01 == INTEGER_CST
9360 && code11 == INTEGER_CST
9361 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9362 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9364 tem = build2_loc (loc, LROTATE_EXPR,
9365 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9366 TREE_OPERAND (arg0, 0),
9367 code0 == LSHIFT_EXPR
9368 ? TREE_OPERAND (arg0, 1)
9369 : TREE_OPERAND (arg1, 1));
9370 return fold_convert_loc (loc, type, tem);
9372 else if (code11 == MINUS_EXPR)
9374 tree tree110, tree111;
9375 tree110 = TREE_OPERAND (tree11, 0);
9376 tree111 = TREE_OPERAND (tree11, 1);
9377 STRIP_NOPS (tree110);
9378 STRIP_NOPS (tree111);
9379 if (TREE_CODE (tree110) == INTEGER_CST
9380 && 0 == compare_tree_int (tree110,
9382 (TREE_TYPE (TREE_OPERAND
9384 && operand_equal_p (tree01, tree111, 0))
9386 fold_convert_loc (loc, type,
9387 build2 ((code0 == LSHIFT_EXPR
9390 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9391 TREE_OPERAND (arg0, 0),
9392 TREE_OPERAND (arg0, 1)));
9394 else if (code01 == MINUS_EXPR)
9396 tree tree010, tree011;
9397 tree010 = TREE_OPERAND (tree01, 0);
9398 tree011 = TREE_OPERAND (tree01, 1);
9399 STRIP_NOPS (tree010);
9400 STRIP_NOPS (tree011);
9401 if (TREE_CODE (tree010) == INTEGER_CST
9402 && 0 == compare_tree_int (tree010,
9404 (TREE_TYPE (TREE_OPERAND
9406 && operand_equal_p (tree11, tree011, 0))
9407 return fold_convert_loc
9409 build2 ((code0 != LSHIFT_EXPR
9412 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9413 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9419 /* In most languages, can't associate operations on floats through
9420 parentheses. Rather than remember where the parentheses were, we
9421 don't associate floats at all, unless the user has specified
9423 And, we need to make sure type is not saturating. */
9425 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9426 && !TYPE_SATURATING (type))
9428 tree var0, con0, lit0, minus_lit0;
9429 tree var1, con1, lit1, minus_lit1;
9433 /* Split both trees into variables, constants, and literals. Then
9434 associate each group together, the constants with literals,
9435 then the result with variables. This increases the chances of
9436 literals being recombined later and of generating relocatable
9437 expressions for the sum of a constant and literal. */
9438 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9439 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9440 code == MINUS_EXPR);
9442 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9443 if (code == MINUS_EXPR)
9446 /* With undefined overflow prefer doing association in a type
9447 which wraps on overflow, if that is one of the operand types. */
9448 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9449 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9451 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9452 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9453 atype = TREE_TYPE (arg0);
9454 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9455 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9456 atype = TREE_TYPE (arg1);
9457 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9460 /* With undefined overflow we can only associate constants with one
9461 variable, and constants whose association doesn't overflow. */
9462 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9463 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9470 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9471 tmp0 = TREE_OPERAND (tmp0, 0);
9472 if (CONVERT_EXPR_P (tmp0)
9473 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9474 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9475 <= TYPE_PRECISION (atype)))
9476 tmp0 = TREE_OPERAND (tmp0, 0);
9477 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9478 tmp1 = TREE_OPERAND (tmp1, 0);
9479 if (CONVERT_EXPR_P (tmp1)
9480 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9481 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9482 <= TYPE_PRECISION (atype)))
9483 tmp1 = TREE_OPERAND (tmp1, 0);
9484 /* The only case we can still associate with two variables
9485 is if they are the same, modulo negation and bit-pattern
9486 preserving conversions. */
9487 if (!operand_equal_p (tmp0, tmp1, 0))
9492 /* Only do something if we found more than two objects. Otherwise,
9493 nothing has changed and we risk infinite recursion. */
9495 && (2 < ((var0 != 0) + (var1 != 0)
9496 + (con0 != 0) + (con1 != 0)
9497 + (lit0 != 0) + (lit1 != 0)
9498 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9500 bool any_overflows = false;
9501 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9502 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9503 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9504 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9505 var0 = associate_trees (loc, var0, var1, code, atype);
9506 con0 = associate_trees (loc, con0, con1, code, atype);
9507 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9508 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9511 /* Preserve the MINUS_EXPR if the negative part of the literal is
9512 greater than the positive part. Otherwise, the multiplicative
9513 folding code (i.e extract_muldiv) may be fooled in case
9514 unsigned constants are subtracted, like in the following
9515 example: ((X*2 + 4) - 8U)/2. */
9516 if (minus_lit0 && lit0)
9518 if (TREE_CODE (lit0) == INTEGER_CST
9519 && TREE_CODE (minus_lit0) == INTEGER_CST
9520 && tree_int_cst_lt (lit0, minus_lit0))
9522 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9528 lit0 = associate_trees (loc, lit0, minus_lit0,
9534 /* Don't introduce overflows through reassociation. */
9536 && ((lit0 && TREE_OVERFLOW_P (lit0))
9537 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9544 fold_convert_loc (loc, type,
9545 associate_trees (loc, var0, minus_lit0,
9546 MINUS_EXPR, atype));
9549 con0 = associate_trees (loc, con0, minus_lit0,
9552 fold_convert_loc (loc, type,
9553 associate_trees (loc, var0, con0,
9558 con0 = associate_trees (loc, con0, lit0, code, atype);
9560 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9568 /* Pointer simplifications for subtraction, simple reassociations. */
9569 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9571 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9572 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9573 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9575 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9576 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9577 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9578 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9579 return fold_build2_loc (loc, PLUS_EXPR, type,
9580 fold_build2_loc (loc, MINUS_EXPR, type,
9582 fold_build2_loc (loc, MINUS_EXPR, type,
9585 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9586 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9588 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9589 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9590 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
9591 fold_convert_loc (loc, type, arg1));
9593 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
9595 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
9597 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9599 tree arg10 = fold_convert_loc (loc, type,
9600 TREE_OPERAND (arg1, 0));
9601 tree arg11 = fold_convert_loc (loc, type,
9602 TREE_OPERAND (arg1, 1));
9603 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
9604 fold_convert_loc (loc, type, arg0),
9607 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
9610 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9611 if (TREE_CODE (arg0) == NEGATE_EXPR
9612 && negate_expr_p (arg1)
9613 && reorder_operands_p (arg0, arg1))
9614 return fold_build2_loc (loc, MINUS_EXPR, type,
9615 fold_convert_loc (loc, type,
9616 negate_expr (arg1)),
9617 fold_convert_loc (loc, type,
9618 TREE_OPERAND (arg0, 0)));
9620 if (! FLOAT_TYPE_P (type))
9622 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9623 any power of 2 minus 1. */
9624 if (TREE_CODE (arg0) == BIT_AND_EXPR
9625 && TREE_CODE (arg1) == BIT_AND_EXPR
9626 && operand_equal_p (TREE_OPERAND (arg0, 0),
9627 TREE_OPERAND (arg1, 0), 0))
9629 tree mask0 = TREE_OPERAND (arg0, 1);
9630 tree mask1 = TREE_OPERAND (arg1, 1);
9631 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
9633 if (operand_equal_p (tem, mask1, 0))
9635 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
9636 TREE_OPERAND (arg0, 0), mask1);
9637 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
9642 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9643 __complex__ ( x, -y ). This is not the same for SNaNs or if
9644 signed zeros are involved. */
9645 if (!HONOR_SNANS (element_mode (arg0))
9646 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9647 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9649 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9650 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9651 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9652 bool arg0rz = false, arg0iz = false;
9653 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9654 || (arg0i && (arg0iz = real_zerop (arg0i))))
9656 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9657 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9658 if (arg0rz && arg1i && real_zerop (arg1i))
9660 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9662 : build1 (REALPART_EXPR, rtype, arg1));
9663 tree ip = arg0i ? arg0i
9664 : build1 (IMAGPART_EXPR, rtype, arg0);
9665 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9667 else if (arg0iz && arg1r && real_zerop (arg1r))
9669 tree rp = arg0r ? arg0r
9670 : build1 (REALPART_EXPR, rtype, arg0);
9671 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9673 : build1 (IMAGPART_EXPR, rtype, arg1));
9674 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9679 /* A - B -> A + (-B) if B is easily negatable. */
9680 if (negate_expr_p (arg1)
9681 && !TYPE_OVERFLOW_SANITIZED (type)
9682 && ((FLOAT_TYPE_P (type)
9683 /* Avoid this transformation if B is a positive REAL_CST. */
9684 && (TREE_CODE (arg1) != REAL_CST
9685 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9686 || INTEGRAL_TYPE_P (type)))
9687 return fold_build2_loc (loc, PLUS_EXPR, type,
9688 fold_convert_loc (loc, type, arg0),
9689 fold_convert_loc (loc, type,
9690 negate_expr (arg1)));
9692 /* Fold &a[i] - &a[j] to i-j. */
9693 if (TREE_CODE (arg0) == ADDR_EXPR
9694 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9695 && TREE_CODE (arg1) == ADDR_EXPR
9696 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9698 tree tem = fold_addr_of_array_ref_difference (loc, type,
9699 TREE_OPERAND (arg0, 0),
9700 TREE_OPERAND (arg1, 0));
9705 if (FLOAT_TYPE_P (type)
9706 && flag_unsafe_math_optimizations
9707 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9708 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9709 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9712 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9713 one. Make sure the type is not saturating and has the signedness of
9714 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9715 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9716 if ((TREE_CODE (arg0) == MULT_EXPR
9717 || TREE_CODE (arg1) == MULT_EXPR)
9718 && !TYPE_SATURATING (type)
9719 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9720 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9721 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9723 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9731 /* (-A) * (-B) -> A * B */
9732 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9733 return fold_build2_loc (loc, MULT_EXPR, type,
9734 fold_convert_loc (loc, type,
9735 TREE_OPERAND (arg0, 0)),
9736 fold_convert_loc (loc, type,
9737 negate_expr (arg1)));
9738 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9739 return fold_build2_loc (loc, MULT_EXPR, type,
9740 fold_convert_loc (loc, type,
9741 negate_expr (arg0)),
9742 fold_convert_loc (loc, type,
9743 TREE_OPERAND (arg1, 0)));
9745 if (! FLOAT_TYPE_P (type))
9747 /* Transform x * -C into -x * C if x is easily negatable. */
9748 if (TREE_CODE (arg1) == INTEGER_CST
9749 && tree_int_cst_sgn (arg1) == -1
9750 && negate_expr_p (arg0)
9751 && (tem = negate_expr (arg1)) != arg1
9752 && !TREE_OVERFLOW (tem))
9753 return fold_build2_loc (loc, MULT_EXPR, type,
9754 fold_convert_loc (loc, type,
9755 negate_expr (arg0)),
9758 /* (a * (1 << b)) is (a << b) */
9759 if (TREE_CODE (arg1) == LSHIFT_EXPR
9760 && integer_onep (TREE_OPERAND (arg1, 0)))
9761 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
9762 TREE_OPERAND (arg1, 1));
9763 if (TREE_CODE (arg0) == LSHIFT_EXPR
9764 && integer_onep (TREE_OPERAND (arg0, 0)))
9765 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
9766 TREE_OPERAND (arg0, 1));
9768 /* (A + A) * C -> A * 2 * C */
9769 if (TREE_CODE (arg0) == PLUS_EXPR
9770 && TREE_CODE (arg1) == INTEGER_CST
9771 && operand_equal_p (TREE_OPERAND (arg0, 0),
9772 TREE_OPERAND (arg0, 1), 0))
9773 return fold_build2_loc (loc, MULT_EXPR, type,
9774 omit_one_operand_loc (loc, type,
9775 TREE_OPERAND (arg0, 0),
9776 TREE_OPERAND (arg0, 1)),
9777 fold_build2_loc (loc, MULT_EXPR, type,
9778 build_int_cst (type, 2) , arg1));
9780 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9781 sign-changing only. */
9782 if (TREE_CODE (arg1) == INTEGER_CST
9783 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9784 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9785 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9787 strict_overflow_p = false;
9788 if (TREE_CODE (arg1) == INTEGER_CST
9789 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9790 &strict_overflow_p)))
9792 if (strict_overflow_p)
9793 fold_overflow_warning (("assuming signed overflow does not "
9794 "occur when simplifying "
9796 WARN_STRICT_OVERFLOW_MISC);
9797 return fold_convert_loc (loc, type, tem);
9800 /* Optimize z * conj(z) for integer complex numbers. */
9801 if (TREE_CODE (arg0) == CONJ_EXPR
9802 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9803 return fold_mult_zconjz (loc, type, arg1);
9804 if (TREE_CODE (arg1) == CONJ_EXPR
9805 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9806 return fold_mult_zconjz (loc, type, arg0);
9810 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
9811 the result for floating point types due to rounding so it is applied
9812 only if -fassociative-math was specify. */
9813 if (flag_associative_math
9814 && TREE_CODE (arg0) == RDIV_EXPR
9815 && TREE_CODE (arg1) == REAL_CST
9816 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
9818 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
9821 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
9822 TREE_OPERAND (arg0, 1));
9825 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
9826 if (operand_equal_p (arg0, arg1, 0))
9828 tree tem = fold_strip_sign_ops (arg0);
9829 if (tem != NULL_TREE)
9831 tem = fold_convert_loc (loc, type, tem);
9832 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
9836 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9837 This is not the same for NaNs or if signed zeros are
9839 if (!HONOR_NANS (arg0)
9840 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9841 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9842 && TREE_CODE (arg1) == COMPLEX_CST
9843 && real_zerop (TREE_REALPART (arg1)))
9845 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9846 if (real_onep (TREE_IMAGPART (arg1)))
9848 fold_build2_loc (loc, COMPLEX_EXPR, type,
9849 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9851 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9852 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9854 fold_build2_loc (loc, COMPLEX_EXPR, type,
9855 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9856 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9860 /* Optimize z * conj(z) for floating point complex numbers.
9861 Guarded by flag_unsafe_math_optimizations as non-finite
9862 imaginary components don't produce scalar results. */
9863 if (flag_unsafe_math_optimizations
9864 && TREE_CODE (arg0) == CONJ_EXPR
9865 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9866 return fold_mult_zconjz (loc, type, arg1);
9867 if (flag_unsafe_math_optimizations
9868 && TREE_CODE (arg1) == CONJ_EXPR
9869 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9870 return fold_mult_zconjz (loc, type, arg0);
9872 if (flag_unsafe_math_optimizations)
9875 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9878 && operand_equal_p (arg0, arg1, 0))
9880 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
9884 tree arg = build_real (type, dconst2);
9885 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
9893 /* Canonicalize (X & C1) | C2. */
9894 if (TREE_CODE (arg0) == BIT_AND_EXPR
9895 && TREE_CODE (arg1) == INTEGER_CST
9896 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9898 int width = TYPE_PRECISION (type), w;
9899 wide_int c1 = TREE_OPERAND (arg0, 1);
9902 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9903 if ((c1 & c2) == c1)
9904 return omit_one_operand_loc (loc, type, arg1,
9905 TREE_OPERAND (arg0, 0));
9907 wide_int msk = wi::mask (width, false,
9908 TYPE_PRECISION (TREE_TYPE (arg1)));
9910 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9911 if (msk.and_not (c1 | c2) == 0)
9912 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9913 TREE_OPERAND (arg0, 0), arg1);
9915 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9916 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9917 mode which allows further optimizations. */
9920 wide_int c3 = c1.and_not (c2);
9921 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9923 wide_int mask = wi::mask (w, false,
9924 TYPE_PRECISION (type));
9925 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9933 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9934 fold_build2_loc (loc, BIT_AND_EXPR, type,
9935 TREE_OPERAND (arg0, 0),
9936 wide_int_to_tree (type,
9941 /* (X & ~Y) | (~X & Y) is X ^ Y */
9942 if (TREE_CODE (arg0) == BIT_AND_EXPR
9943 && TREE_CODE (arg1) == BIT_AND_EXPR)
9945 tree a0, a1, l0, l1, n0, n1;
9947 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9948 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9950 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9951 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9953 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
9954 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
9956 if ((operand_equal_p (n0, a0, 0)
9957 && operand_equal_p (n1, a1, 0))
9958 || (operand_equal_p (n0, a1, 0)
9959 && operand_equal_p (n1, a0, 0)))
9960 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
9963 /* See if this can be simplified into a rotate first. If that
9964 is unsuccessful continue in the association code. */
9968 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9969 if (TREE_CODE (arg0) == BIT_AND_EXPR
9970 && INTEGRAL_TYPE_P (type)
9971 && integer_onep (TREE_OPERAND (arg0, 1))
9972 && integer_onep (arg1))
9973 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9974 build_zero_cst (TREE_TYPE (arg0)));
9976 /* See if this can be simplified into a rotate first. If that
9977 is unsuccessful continue in the association code. */
9981 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
9982 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
9983 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
9984 || (TREE_CODE (arg0) == EQ_EXPR
9985 && integer_zerop (TREE_OPERAND (arg0, 1))))
9986 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9987 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
9989 /* X & ~X , X & (X == 0), and X & !X are always zero. */
9990 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
9991 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
9992 || (TREE_CODE (arg1) == EQ_EXPR
9993 && integer_zerop (TREE_OPERAND (arg1, 1))))
9994 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9995 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9997 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9998 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9999 && INTEGRAL_TYPE_P (type)
10000 && integer_onep (TREE_OPERAND (arg0, 1))
10001 && integer_onep (arg1))
10004 tem = TREE_OPERAND (arg0, 0);
10005 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10006 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10008 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10009 build_zero_cst (TREE_TYPE (tem)));
10011 /* Fold ~X & 1 as (X & 1) == 0. */
10012 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10013 && INTEGRAL_TYPE_P (type)
10014 && integer_onep (arg1))
10017 tem = TREE_OPERAND (arg0, 0);
10018 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10019 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10021 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10022 build_zero_cst (TREE_TYPE (tem)));
10024 /* Fold !X & 1 as X == 0. */
10025 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10026 && integer_onep (arg1))
10028 tem = TREE_OPERAND (arg0, 0);
10029 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10030 build_zero_cst (TREE_TYPE (tem)));
10033 /* Fold (X ^ Y) & Y as ~X & Y. */
10034 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10035 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10037 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10038 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10039 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10040 fold_convert_loc (loc, type, arg1));
10042 /* Fold (X ^ Y) & X as ~Y & X. */
10043 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10044 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10045 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10047 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10048 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10049 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10050 fold_convert_loc (loc, type, arg1));
10052 /* Fold X & (X ^ Y) as X & ~Y. */
10053 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10054 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10056 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10057 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10058 fold_convert_loc (loc, type, arg0),
10059 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10061 /* Fold X & (Y ^ X) as ~Y & X. */
10062 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10063 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10064 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10066 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10067 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10068 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10069 fold_convert_loc (loc, type, arg0));
10072 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10073 multiple of 1 << CST. */
10074 if (TREE_CODE (arg1) == INTEGER_CST)
10076 wide_int cst1 = arg1;
10077 wide_int ncst1 = -cst1;
10078 if ((cst1 & ncst1) == ncst1
10079 && multiple_of_p (type, arg0,
10080 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10081 return fold_convert_loc (loc, type, arg0);
10084 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10086 if (TREE_CODE (arg1) == INTEGER_CST
10087 && TREE_CODE (arg0) == MULT_EXPR
10088 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10090 wide_int warg1 = arg1;
10091 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10094 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10096 else if (masked != warg1)
10098 /* Avoid the transform if arg1 is a mask of some
10099 mode which allows further optimizations. */
10100 int pop = wi::popcount (warg1);
10101 if (!(pop >= BITS_PER_UNIT
10102 && exact_log2 (pop) != -1
10103 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10104 return fold_build2_loc (loc, code, type, op0,
10105 wide_int_to_tree (type, masked));
10109 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10110 ((A & N) + B) & M -> (A + B) & M
10111 Similarly if (N & M) == 0,
10112 ((A | N) + B) & M -> (A + B) & M
10113 and for - instead of + (or unary - instead of +)
10114 and/or ^ instead of |.
10115 If B is constant and (B & M) == 0, fold into A & M. */
10116 if (TREE_CODE (arg1) == INTEGER_CST)
10118 wide_int cst1 = arg1;
10119 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10120 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10121 && (TREE_CODE (arg0) == PLUS_EXPR
10122 || TREE_CODE (arg0) == MINUS_EXPR
10123 || TREE_CODE (arg0) == NEGATE_EXPR)
10124 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10125 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10131 /* Now we know that arg0 is (C + D) or (C - D) or
10132 -C and arg1 (M) is == (1LL << cst) - 1.
10133 Store C into PMOP[0] and D into PMOP[1]. */
10134 pmop[0] = TREE_OPERAND (arg0, 0);
10136 if (TREE_CODE (arg0) != NEGATE_EXPR)
10138 pmop[1] = TREE_OPERAND (arg0, 1);
10142 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10145 for (; which >= 0; which--)
10146 switch (TREE_CODE (pmop[which]))
10151 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10154 cst0 = TREE_OPERAND (pmop[which], 1);
10156 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10161 else if (cst0 != 0)
10163 /* If C or D is of the form (A & N) where
10164 (N & M) == M, or of the form (A | N) or
10165 (A ^ N) where (N & M) == 0, replace it with A. */
10166 pmop[which] = TREE_OPERAND (pmop[which], 0);
10169 /* If C or D is a N where (N & M) == 0, it can be
10170 omitted (assumed 0). */
10171 if ((TREE_CODE (arg0) == PLUS_EXPR
10172 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10173 && (cst1 & pmop[which]) == 0)
10174 pmop[which] = NULL;
10180 /* Only build anything new if we optimized one or both arguments
10182 if (pmop[0] != TREE_OPERAND (arg0, 0)
10183 || (TREE_CODE (arg0) != NEGATE_EXPR
10184 && pmop[1] != TREE_OPERAND (arg0, 1)))
10186 tree utype = TREE_TYPE (arg0);
10187 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10189 /* Perform the operations in a type that has defined
10190 overflow behavior. */
10191 utype = unsigned_type_for (TREE_TYPE (arg0));
10192 if (pmop[0] != NULL)
10193 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10194 if (pmop[1] != NULL)
10195 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10198 if (TREE_CODE (arg0) == NEGATE_EXPR)
10199 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10200 else if (TREE_CODE (arg0) == PLUS_EXPR)
10202 if (pmop[0] != NULL && pmop[1] != NULL)
10203 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10205 else if (pmop[0] != NULL)
10207 else if (pmop[1] != NULL)
10210 return build_int_cst (type, 0);
10212 else if (pmop[0] == NULL)
10213 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10215 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10217 /* TEM is now the new binary +, - or unary - replacement. */
10218 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10219 fold_convert_loc (loc, utype, arg1));
10220 return fold_convert_loc (loc, type, tem);
10225 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10226 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10227 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10229 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10231 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10234 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10240 /* Don't touch a floating-point divide by zero unless the mode
10241 of the constant can represent infinity. */
10242 if (TREE_CODE (arg1) == REAL_CST
10243 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10244 && real_zerop (arg1))
10247 /* (-A) / (-B) -> A / B */
10248 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10249 return fold_build2_loc (loc, RDIV_EXPR, type,
10250 TREE_OPERAND (arg0, 0),
10251 negate_expr (arg1));
10252 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10253 return fold_build2_loc (loc, RDIV_EXPR, type,
10254 negate_expr (arg0),
10255 TREE_OPERAND (arg1, 0));
10257 /* Convert A/B/C to A/(B*C). */
10258 if (flag_reciprocal_math
10259 && TREE_CODE (arg0) == RDIV_EXPR)
10260 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10261 fold_build2_loc (loc, MULT_EXPR, type,
10262 TREE_OPERAND (arg0, 1), arg1));
10264 /* Convert A/(B/C) to (A/B)*C. */
10265 if (flag_reciprocal_math
10266 && TREE_CODE (arg1) == RDIV_EXPR)
10267 return fold_build2_loc (loc, MULT_EXPR, type,
10268 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
10269 TREE_OPERAND (arg1, 0)),
10270 TREE_OPERAND (arg1, 1));
10272 /* Convert C1/(X*C2) into (C1/C2)/X. */
10273 if (flag_reciprocal_math
10274 && TREE_CODE (arg1) == MULT_EXPR
10275 && TREE_CODE (arg0) == REAL_CST
10276 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10278 tree tem = const_binop (RDIV_EXPR, arg0,
10279 TREE_OPERAND (arg1, 1));
10281 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10282 TREE_OPERAND (arg1, 0));
10287 case TRUNC_DIV_EXPR:
10288 /* Optimize (X & (-A)) / A where A is a power of 2,
10290 if (TREE_CODE (arg0) == BIT_AND_EXPR
10291 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
10292 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
10294 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
10295 arg1, TREE_OPERAND (arg0, 1));
10296 if (sum && integer_zerop (sum)) {
10297 tree pow2 = build_int_cst (integer_type_node,
10298 wi::exact_log2 (arg1));
10299 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10300 TREE_OPERAND (arg0, 0), pow2);
10306 case FLOOR_DIV_EXPR:
10307 /* Simplify A / (B << N) where A and B are positive and B is
10308 a power of 2, to A >> (N + log2(B)). */
10309 strict_overflow_p = false;
10310 if (TREE_CODE (arg1) == LSHIFT_EXPR
10311 && (TYPE_UNSIGNED (type)
10312 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10314 tree sval = TREE_OPERAND (arg1, 0);
10315 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10317 tree sh_cnt = TREE_OPERAND (arg1, 1);
10318 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10319 wi::exact_log2 (sval));
10321 if (strict_overflow_p)
10322 fold_overflow_warning (("assuming signed overflow does not "
10323 "occur when simplifying A / (B << N)"),
10324 WARN_STRICT_OVERFLOW_MISC);
10326 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10328 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10329 fold_convert_loc (loc, type, arg0), sh_cnt);
10335 case ROUND_DIV_EXPR:
10336 case CEIL_DIV_EXPR:
10337 case EXACT_DIV_EXPR:
10338 if (integer_zerop (arg1))
10341 /* Convert -A / -B to A / B when the type is signed and overflow is
10343 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10344 && TREE_CODE (arg0) == NEGATE_EXPR
10345 && negate_expr_p (arg1))
10347 if (INTEGRAL_TYPE_P (type))
10348 fold_overflow_warning (("assuming signed overflow does not occur "
10349 "when distributing negation across "
10351 WARN_STRICT_OVERFLOW_MISC);
10352 return fold_build2_loc (loc, code, type,
10353 fold_convert_loc (loc, type,
10354 TREE_OPERAND (arg0, 0)),
10355 fold_convert_loc (loc, type,
10356 negate_expr (arg1)));
10358 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10359 && TREE_CODE (arg1) == NEGATE_EXPR
10360 && negate_expr_p (arg0))
10362 if (INTEGRAL_TYPE_P (type))
10363 fold_overflow_warning (("assuming signed overflow does not occur "
10364 "when distributing negation across "
10366 WARN_STRICT_OVERFLOW_MISC);
10367 return fold_build2_loc (loc, code, type,
10368 fold_convert_loc (loc, type,
10369 negate_expr (arg0)),
10370 fold_convert_loc (loc, type,
10371 TREE_OPERAND (arg1, 0)));
10374 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10375 operation, EXACT_DIV_EXPR.
10377 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10378 At one time others generated faster code, it's not clear if they do
10379 after the last round to changes to the DIV code in expmed.c. */
10380 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10381 && multiple_of_p (type, arg0, arg1))
10382 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10383 fold_convert (type, arg0),
10384 fold_convert (type, arg1));
10386 strict_overflow_p = false;
10387 if (TREE_CODE (arg1) == INTEGER_CST
10388 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10389 &strict_overflow_p)))
10391 if (strict_overflow_p)
10392 fold_overflow_warning (("assuming signed overflow does not occur "
10393 "when simplifying division"),
10394 WARN_STRICT_OVERFLOW_MISC);
10395 return fold_convert_loc (loc, type, tem);
10400 case CEIL_MOD_EXPR:
10401 case FLOOR_MOD_EXPR:
10402 case ROUND_MOD_EXPR:
10403 case TRUNC_MOD_EXPR:
10404 strict_overflow_p = false;
10405 if (TREE_CODE (arg1) == INTEGER_CST
10406 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10407 &strict_overflow_p)))
10409 if (strict_overflow_p)
10410 fold_overflow_warning (("assuming signed overflow does not occur "
10411 "when simplifying modulus"),
10412 WARN_STRICT_OVERFLOW_MISC);
10413 return fold_convert_loc (loc, type, tem);
10422 /* Since negative shift count is not well-defined,
10423 don't try to compute it in the compiler. */
10424 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10427 prec = element_precision (type);
10429 /* If we have a rotate of a bit operation with the rotate count and
10430 the second operand of the bit operation both constant,
10431 permute the two operations. */
10432 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10433 && (TREE_CODE (arg0) == BIT_AND_EXPR
10434 || TREE_CODE (arg0) == BIT_IOR_EXPR
10435 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10436 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10437 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10438 fold_build2_loc (loc, code, type,
10439 TREE_OPERAND (arg0, 0), arg1),
10440 fold_build2_loc (loc, code, type,
10441 TREE_OPERAND (arg0, 1), arg1));
10443 /* Two consecutive rotates adding up to the some integer
10444 multiple of the precision of the type can be ignored. */
10445 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10446 && TREE_CODE (arg0) == RROTATE_EXPR
10447 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10448 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10450 return TREE_OPERAND (arg0, 0);
10455 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
10461 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
10466 case TRUTH_ANDIF_EXPR:
10467 /* Note that the operands of this must be ints
10468 and their values must be 0 or 1.
10469 ("true" is a fixed value perhaps depending on the language.) */
10470 /* If first arg is constant zero, return it. */
10471 if (integer_zerop (arg0))
10472 return fold_convert_loc (loc, type, arg0);
10473 case TRUTH_AND_EXPR:
10474 /* If either arg is constant true, drop it. */
10475 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10476 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10477 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10478 /* Preserve sequence points. */
10479 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10480 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10481 /* If second arg is constant zero, result is zero, but first arg
10482 must be evaluated. */
10483 if (integer_zerop (arg1))
10484 return omit_one_operand_loc (loc, type, arg1, arg0);
10485 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10486 case will be handled here. */
10487 if (integer_zerop (arg0))
10488 return omit_one_operand_loc (loc, type, arg0, arg1);
10490 /* !X && X is always false. */
10491 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10492 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10493 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10494 /* X && !X is always false. */
10495 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10496 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10497 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10499 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10500 means A >= Y && A != MAX, but in this case we know that
10503 if (!TREE_SIDE_EFFECTS (arg0)
10504 && !TREE_SIDE_EFFECTS (arg1))
10506 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10507 if (tem && !operand_equal_p (tem, arg0, 0))
10508 return fold_build2_loc (loc, code, type, tem, arg1);
10510 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10511 if (tem && !operand_equal_p (tem, arg1, 0))
10512 return fold_build2_loc (loc, code, type, arg0, tem);
10515 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10521 case TRUTH_ORIF_EXPR:
10522 /* Note that the operands of this must be ints
10523 and their values must be 0 or true.
10524 ("true" is a fixed value perhaps depending on the language.) */
10525 /* If first arg is constant true, return it. */
10526 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10527 return fold_convert_loc (loc, type, arg0);
10528 case TRUTH_OR_EXPR:
10529 /* If either arg is constant zero, drop it. */
10530 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10531 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10532 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10533 /* Preserve sequence points. */
10534 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10535 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10536 /* If second arg is constant true, result is true, but we must
10537 evaluate first arg. */
10538 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10539 return omit_one_operand_loc (loc, type, arg1, arg0);
10540 /* Likewise for first arg, but note this only occurs here for
10542 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10543 return omit_one_operand_loc (loc, type, arg0, arg1);
10545 /* !X || X is always true. */
10546 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10547 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10548 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10549 /* X || !X is always true. */
10550 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10551 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10552 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10554 /* (X && !Y) || (!X && Y) is X ^ Y */
10555 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10556 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10558 tree a0, a1, l0, l1, n0, n1;
10560 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10561 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10563 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10564 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10566 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10567 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10569 if ((operand_equal_p (n0, a0, 0)
10570 && operand_equal_p (n1, a1, 0))
10571 || (operand_equal_p (n0, a1, 0)
10572 && operand_equal_p (n1, a0, 0)))
10573 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10576 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10582 case TRUTH_XOR_EXPR:
10583 /* If the second arg is constant zero, drop it. */
10584 if (integer_zerop (arg1))
10585 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10586 /* If the second arg is constant true, this is a logical inversion. */
10587 if (integer_onep (arg1))
10589 tem = invert_truthvalue_loc (loc, arg0);
10590 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10592 /* Identical arguments cancel to zero. */
10593 if (operand_equal_p (arg0, arg1, 0))
10594 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10596 /* !X ^ X is always true. */
10597 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10598 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10599 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10601 /* X ^ !X is always true. */
10602 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10603 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10604 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10613 tem = fold_comparison (loc, code, type, op0, op1);
10614 if (tem != NULL_TREE)
10617 /* bool_var != 1 becomes !bool_var. */
10618 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10619 && code == NE_EXPR)
10620 return fold_convert_loc (loc, type,
10621 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10622 TREE_TYPE (arg0), arg0));
10624 /* bool_var == 0 becomes !bool_var. */
10625 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10626 && code == EQ_EXPR)
10627 return fold_convert_loc (loc, type,
10628 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10629 TREE_TYPE (arg0), arg0));
10631 /* !exp != 0 becomes !exp */
10632 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10633 && code == NE_EXPR)
10634 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10636 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10637 if ((TREE_CODE (arg0) == PLUS_EXPR
10638 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10639 || TREE_CODE (arg0) == MINUS_EXPR)
10640 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10643 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10644 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10646 tree val = TREE_OPERAND (arg0, 1);
10647 return omit_two_operands_loc (loc, type,
10648 fold_build2_loc (loc, code, type,
10650 build_int_cst (TREE_TYPE (val),
10652 TREE_OPERAND (arg0, 0), arg1);
10655 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10656 if (TREE_CODE (arg0) == MINUS_EXPR
10657 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10658 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10661 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10663 return omit_two_operands_loc (loc, type,
10665 ? boolean_true_node : boolean_false_node,
10666 TREE_OPERAND (arg0, 1), arg1);
10669 /* If this is an EQ or NE comparison with zero and ARG0 is
10670 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10671 two operations, but the latter can be done in one less insn
10672 on machines that have only two-operand insns or on which a
10673 constant cannot be the first operand. */
10674 if (TREE_CODE (arg0) == BIT_AND_EXPR
10675 && integer_zerop (arg1))
10677 tree arg00 = TREE_OPERAND (arg0, 0);
10678 tree arg01 = TREE_OPERAND (arg0, 1);
10679 if (TREE_CODE (arg00) == LSHIFT_EXPR
10680 && integer_onep (TREE_OPERAND (arg00, 0)))
10682 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10683 arg01, TREE_OPERAND (arg00, 1));
10684 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10685 build_int_cst (TREE_TYPE (arg0), 1));
10686 return fold_build2_loc (loc, code, type,
10687 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10690 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10691 && integer_onep (TREE_OPERAND (arg01, 0)))
10693 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10694 arg00, TREE_OPERAND (arg01, 1));
10695 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10696 build_int_cst (TREE_TYPE (arg0), 1));
10697 return fold_build2_loc (loc, code, type,
10698 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10703 /* If this is an NE or EQ comparison of zero against the result of a
10704 signed MOD operation whose second operand is a power of 2, make
10705 the MOD operation unsigned since it is simpler and equivalent. */
10706 if (integer_zerop (arg1)
10707 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10708 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10709 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10710 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10711 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10712 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10714 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10715 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10716 fold_convert_loc (loc, newtype,
10717 TREE_OPERAND (arg0, 0)),
10718 fold_convert_loc (loc, newtype,
10719 TREE_OPERAND (arg0, 1)));
10721 return fold_build2_loc (loc, code, type, newmod,
10722 fold_convert_loc (loc, newtype, arg1));
10725 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10726 C1 is a valid shift constant, and C2 is a power of two, i.e.
10728 if (TREE_CODE (arg0) == BIT_AND_EXPR
10729 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10730 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10732 && integer_pow2p (TREE_OPERAND (arg0, 1))
10733 && integer_zerop (arg1))
10735 tree itype = TREE_TYPE (arg0);
10736 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10737 prec = TYPE_PRECISION (itype);
10739 /* Check for a valid shift count. */
10740 if (wi::ltu_p (arg001, prec))
10742 tree arg01 = TREE_OPERAND (arg0, 1);
10743 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10744 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10745 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10746 can be rewritten as (X & (C2 << C1)) != 0. */
10747 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10749 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10750 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10751 return fold_build2_loc (loc, code, type, tem,
10752 fold_convert_loc (loc, itype, arg1));
10754 /* Otherwise, for signed (arithmetic) shifts,
10755 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10756 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10757 else if (!TYPE_UNSIGNED (itype))
10758 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10759 arg000, build_int_cst (itype, 0));
10760 /* Otherwise, of unsigned (logical) shifts,
10761 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10762 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10764 return omit_one_operand_loc (loc, type,
10765 code == EQ_EXPR ? integer_one_node
10766 : integer_zero_node,
10771 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10772 Similarly for NE_EXPR. */
10773 if (TREE_CODE (arg0) == BIT_AND_EXPR
10774 && TREE_CODE (arg1) == INTEGER_CST
10775 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10777 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10778 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10779 TREE_OPERAND (arg0, 1));
10781 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10782 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10784 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10785 if (integer_nonzerop (dandnotc))
10786 return omit_one_operand_loc (loc, type, rslt, arg0);
10789 /* If this is a comparison of a field, we may be able to simplify it. */
10790 if ((TREE_CODE (arg0) == COMPONENT_REF
10791 || TREE_CODE (arg0) == BIT_FIELD_REF)
10792 /* Handle the constant case even without -O
10793 to make sure the warnings are given. */
10794 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10796 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10801 /* Optimize comparisons of strlen vs zero to a compare of the
10802 first character of the string vs zero. To wit,
10803 strlen(ptr) == 0 => *ptr == 0
10804 strlen(ptr) != 0 => *ptr != 0
10805 Other cases should reduce to one of these two (or a constant)
10806 due to the return value of strlen being unsigned. */
10807 if (TREE_CODE (arg0) == CALL_EXPR
10808 && integer_zerop (arg1))
10810 tree fndecl = get_callee_fndecl (arg0);
10813 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10814 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10815 && call_expr_nargs (arg0) == 1
10816 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10818 tree iref = build_fold_indirect_ref_loc (loc,
10819 CALL_EXPR_ARG (arg0, 0));
10820 return fold_build2_loc (loc, code, type, iref,
10821 build_int_cst (TREE_TYPE (iref), 0));
10825 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10826 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10827 if (TREE_CODE (arg0) == RSHIFT_EXPR
10828 && integer_zerop (arg1)
10829 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10831 tree arg00 = TREE_OPERAND (arg0, 0);
10832 tree arg01 = TREE_OPERAND (arg0, 1);
10833 tree itype = TREE_TYPE (arg00);
10834 if (wi::eq_p (arg01, element_precision (itype) - 1))
10836 if (TYPE_UNSIGNED (itype))
10838 itype = signed_type_for (itype);
10839 arg00 = fold_convert_loc (loc, itype, arg00);
10841 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10842 type, arg00, build_zero_cst (itype));
10846 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10847 (X & C) == 0 when C is a single bit. */
10848 if (TREE_CODE (arg0) == BIT_AND_EXPR
10849 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10850 && integer_zerop (arg1)
10851 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10853 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10854 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10855 TREE_OPERAND (arg0, 1));
10856 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10858 fold_convert_loc (loc, TREE_TYPE (arg0),
10862 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10863 constant C is a power of two, i.e. a single bit. */
10864 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10865 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10866 && integer_zerop (arg1)
10867 && integer_pow2p (TREE_OPERAND (arg0, 1))
10868 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10869 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10871 tree arg00 = TREE_OPERAND (arg0, 0);
10872 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10873 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10876 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10877 when is C is a power of two, i.e. a single bit. */
10878 if (TREE_CODE (arg0) == BIT_AND_EXPR
10879 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10880 && integer_zerop (arg1)
10881 && integer_pow2p (TREE_OPERAND (arg0, 1))
10882 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10883 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10885 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10886 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10887 arg000, TREE_OPERAND (arg0, 1));
10888 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10889 tem, build_int_cst (TREE_TYPE (tem), 0));
10892 if (integer_zerop (arg1)
10893 && tree_expr_nonzero_p (arg0))
10895 tree res = constant_boolean_node (code==NE_EXPR, type);
10896 return omit_one_operand_loc (loc, type, res, arg0);
10899 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10900 if (TREE_CODE (arg0) == BIT_AND_EXPR
10901 && TREE_CODE (arg1) == BIT_AND_EXPR)
10903 tree arg00 = TREE_OPERAND (arg0, 0);
10904 tree arg01 = TREE_OPERAND (arg0, 1);
10905 tree arg10 = TREE_OPERAND (arg1, 0);
10906 tree arg11 = TREE_OPERAND (arg1, 1);
10907 tree itype = TREE_TYPE (arg0);
10909 if (operand_equal_p (arg01, arg11, 0))
10910 return fold_build2_loc (loc, code, type,
10911 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10912 fold_build2_loc (loc,
10913 BIT_XOR_EXPR, itype,
10916 build_zero_cst (itype));
10918 if (operand_equal_p (arg01, arg10, 0))
10919 return fold_build2_loc (loc, code, type,
10920 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10921 fold_build2_loc (loc,
10922 BIT_XOR_EXPR, itype,
10925 build_zero_cst (itype));
10927 if (operand_equal_p (arg00, arg11, 0))
10928 return fold_build2_loc (loc, code, type,
10929 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10930 fold_build2_loc (loc,
10931 BIT_XOR_EXPR, itype,
10934 build_zero_cst (itype));
10936 if (operand_equal_p (arg00, arg10, 0))
10937 return fold_build2_loc (loc, code, type,
10938 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10939 fold_build2_loc (loc,
10940 BIT_XOR_EXPR, itype,
10943 build_zero_cst (itype));
10946 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10947 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10949 tree arg00 = TREE_OPERAND (arg0, 0);
10950 tree arg01 = TREE_OPERAND (arg0, 1);
10951 tree arg10 = TREE_OPERAND (arg1, 0);
10952 tree arg11 = TREE_OPERAND (arg1, 1);
10953 tree itype = TREE_TYPE (arg0);
10955 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10956 operand_equal_p guarantees no side-effects so we don't need
10957 to use omit_one_operand on Z. */
10958 if (operand_equal_p (arg01, arg11, 0))
10959 return fold_build2_loc (loc, code, type, arg00,
10960 fold_convert_loc (loc, TREE_TYPE (arg00),
10962 if (operand_equal_p (arg01, arg10, 0))
10963 return fold_build2_loc (loc, code, type, arg00,
10964 fold_convert_loc (loc, TREE_TYPE (arg00),
10966 if (operand_equal_p (arg00, arg11, 0))
10967 return fold_build2_loc (loc, code, type, arg01,
10968 fold_convert_loc (loc, TREE_TYPE (arg01),
10970 if (operand_equal_p (arg00, arg10, 0))
10971 return fold_build2_loc (loc, code, type, arg01,
10972 fold_convert_loc (loc, TREE_TYPE (arg01),
10975 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10976 if (TREE_CODE (arg01) == INTEGER_CST
10977 && TREE_CODE (arg11) == INTEGER_CST)
10979 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10980 fold_convert_loc (loc, itype, arg11));
10981 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10982 return fold_build2_loc (loc, code, type, tem,
10983 fold_convert_loc (loc, itype, arg10));
10987 /* Attempt to simplify equality/inequality comparisons of complex
10988 values. Only lower the comparison if the result is known or
10989 can be simplified to a single scalar comparison. */
10990 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10991 || TREE_CODE (arg0) == COMPLEX_CST)
10992 && (TREE_CODE (arg1) == COMPLEX_EXPR
10993 || TREE_CODE (arg1) == COMPLEX_CST))
10995 tree real0, imag0, real1, imag1;
10998 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11000 real0 = TREE_OPERAND (arg0, 0);
11001 imag0 = TREE_OPERAND (arg0, 1);
11005 real0 = TREE_REALPART (arg0);
11006 imag0 = TREE_IMAGPART (arg0);
11009 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11011 real1 = TREE_OPERAND (arg1, 0);
11012 imag1 = TREE_OPERAND (arg1, 1);
11016 real1 = TREE_REALPART (arg1);
11017 imag1 = TREE_IMAGPART (arg1);
11020 rcond = fold_binary_loc (loc, code, type, real0, real1);
11021 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11023 if (integer_zerop (rcond))
11025 if (code == EQ_EXPR)
11026 return omit_two_operands_loc (loc, type, boolean_false_node,
11028 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11032 if (code == NE_EXPR)
11033 return omit_two_operands_loc (loc, type, boolean_true_node,
11035 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11039 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11040 if (icond && TREE_CODE (icond) == INTEGER_CST)
11042 if (integer_zerop (icond))
11044 if (code == EQ_EXPR)
11045 return omit_two_operands_loc (loc, type, boolean_false_node,
11047 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11051 if (code == NE_EXPR)
11052 return omit_two_operands_loc (loc, type, boolean_true_node,
11054 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11065 tem = fold_comparison (loc, code, type, op0, op1);
11066 if (tem != NULL_TREE)
11069 /* Transform comparisons of the form X +- C CMP X. */
11070 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11071 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11072 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11073 && !HONOR_SNANS (arg0))
11074 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11075 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11077 tree arg01 = TREE_OPERAND (arg0, 1);
11078 enum tree_code code0 = TREE_CODE (arg0);
11081 if (TREE_CODE (arg01) == REAL_CST)
11082 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11084 is_positive = tree_int_cst_sgn (arg01);
11086 /* (X - c) > X becomes false. */
11087 if (code == GT_EXPR
11088 && ((code0 == MINUS_EXPR && is_positive >= 0)
11089 || (code0 == PLUS_EXPR && is_positive <= 0)))
11091 if (TREE_CODE (arg01) == INTEGER_CST
11092 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11093 fold_overflow_warning (("assuming signed overflow does not "
11094 "occur when assuming that (X - c) > X "
11095 "is always false"),
11096 WARN_STRICT_OVERFLOW_ALL);
11097 return constant_boolean_node (0, type);
11100 /* Likewise (X + c) < X becomes false. */
11101 if (code == LT_EXPR
11102 && ((code0 == PLUS_EXPR && is_positive >= 0)
11103 || (code0 == MINUS_EXPR && is_positive <= 0)))
11105 if (TREE_CODE (arg01) == INTEGER_CST
11106 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11107 fold_overflow_warning (("assuming signed overflow does not "
11108 "occur when assuming that "
11109 "(X + c) < X is always false"),
11110 WARN_STRICT_OVERFLOW_ALL);
11111 return constant_boolean_node (0, type);
11114 /* Convert (X - c) <= X to true. */
11115 if (!HONOR_NANS (arg1)
11117 && ((code0 == MINUS_EXPR && is_positive >= 0)
11118 || (code0 == PLUS_EXPR && is_positive <= 0)))
11120 if (TREE_CODE (arg01) == INTEGER_CST
11121 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11122 fold_overflow_warning (("assuming signed overflow does not "
11123 "occur when assuming that "
11124 "(X - c) <= X is always true"),
11125 WARN_STRICT_OVERFLOW_ALL);
11126 return constant_boolean_node (1, type);
11129 /* Convert (X + c) >= X to true. */
11130 if (!HONOR_NANS (arg1)
11132 && ((code0 == PLUS_EXPR && is_positive >= 0)
11133 || (code0 == MINUS_EXPR && is_positive <= 0)))
11135 if (TREE_CODE (arg01) == INTEGER_CST
11136 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11137 fold_overflow_warning (("assuming signed overflow does not "
11138 "occur when assuming that "
11139 "(X + c) >= X is always true"),
11140 WARN_STRICT_OVERFLOW_ALL);
11141 return constant_boolean_node (1, type);
11144 if (TREE_CODE (arg01) == INTEGER_CST)
11146 /* Convert X + c > X and X - c < X to true for integers. */
11147 if (code == GT_EXPR
11148 && ((code0 == PLUS_EXPR && is_positive > 0)
11149 || (code0 == MINUS_EXPR && is_positive < 0)))
11151 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11152 fold_overflow_warning (("assuming signed overflow does "
11153 "not occur when assuming that "
11154 "(X + c) > X is always true"),
11155 WARN_STRICT_OVERFLOW_ALL);
11156 return constant_boolean_node (1, type);
11159 if (code == LT_EXPR
11160 && ((code0 == MINUS_EXPR && is_positive > 0)
11161 || (code0 == PLUS_EXPR && is_positive < 0)))
11163 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11164 fold_overflow_warning (("assuming signed overflow does "
11165 "not occur when assuming that "
11166 "(X - c) < X is always true"),
11167 WARN_STRICT_OVERFLOW_ALL);
11168 return constant_boolean_node (1, type);
11171 /* Convert X + c <= X and X - c >= X to false for integers. */
11172 if (code == LE_EXPR
11173 && ((code0 == PLUS_EXPR && is_positive > 0)
11174 || (code0 == MINUS_EXPR && is_positive < 0)))
11176 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11177 fold_overflow_warning (("assuming signed overflow does "
11178 "not occur when assuming that "
11179 "(X + c) <= X is always false"),
11180 WARN_STRICT_OVERFLOW_ALL);
11181 return constant_boolean_node (0, type);
11184 if (code == GE_EXPR
11185 && ((code0 == MINUS_EXPR && is_positive > 0)
11186 || (code0 == PLUS_EXPR && is_positive < 0)))
11188 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11189 fold_overflow_warning (("assuming signed overflow does "
11190 "not occur when assuming that "
11191 "(X - c) >= X is always false"),
11192 WARN_STRICT_OVERFLOW_ALL);
11193 return constant_boolean_node (0, type);
11198 /* If we are comparing an ABS_EXPR with a constant, we can
11199 convert all the cases into explicit comparisons, but they may
11200 well not be faster than doing the ABS and one comparison.
11201 But ABS (X) <= C is a range comparison, which becomes a subtraction
11202 and a comparison, and is probably faster. */
11203 if (code == LE_EXPR
11204 && TREE_CODE (arg1) == INTEGER_CST
11205 && TREE_CODE (arg0) == ABS_EXPR
11206 && ! TREE_SIDE_EFFECTS (arg0)
11207 && (0 != (tem = negate_expr (arg1)))
11208 && TREE_CODE (tem) == INTEGER_CST
11209 && !TREE_OVERFLOW (tem))
11210 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11211 build2 (GE_EXPR, type,
11212 TREE_OPERAND (arg0, 0), tem),
11213 build2 (LE_EXPR, type,
11214 TREE_OPERAND (arg0, 0), arg1));
11216 /* Convert ABS_EXPR<x> >= 0 to true. */
11217 strict_overflow_p = false;
11218 if (code == GE_EXPR
11219 && (integer_zerop (arg1)
11220 || (! HONOR_NANS (arg0)
11221 && real_zerop (arg1)))
11222 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11224 if (strict_overflow_p)
11225 fold_overflow_warning (("assuming signed overflow does not occur "
11226 "when simplifying comparison of "
11227 "absolute value and zero"),
11228 WARN_STRICT_OVERFLOW_CONDITIONAL);
11229 return omit_one_operand_loc (loc, type,
11230 constant_boolean_node (true, type),
11234 /* Convert ABS_EXPR<x> < 0 to false. */
11235 strict_overflow_p = false;
11236 if (code == LT_EXPR
11237 && (integer_zerop (arg1) || real_zerop (arg1))
11238 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11240 if (strict_overflow_p)
11241 fold_overflow_warning (("assuming signed overflow does not occur "
11242 "when simplifying comparison of "
11243 "absolute value and zero"),
11244 WARN_STRICT_OVERFLOW_CONDITIONAL);
11245 return omit_one_operand_loc (loc, type,
11246 constant_boolean_node (false, type),
11250 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11251 and similarly for >= into !=. */
11252 if ((code == LT_EXPR || code == GE_EXPR)
11253 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11254 && TREE_CODE (arg1) == LSHIFT_EXPR
11255 && integer_onep (TREE_OPERAND (arg1, 0)))
11256 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11257 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11258 TREE_OPERAND (arg1, 1)),
11259 build_zero_cst (TREE_TYPE (arg0)));
11261 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11262 otherwise Y might be >= # of bits in X's type and thus e.g.
11263 (unsigned char) (1 << Y) for Y 15 might be 0.
11264 If the cast is widening, then 1 << Y should have unsigned type,
11265 otherwise if Y is number of bits in the signed shift type minus 1,
11266 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11267 31 might be 0xffffffff80000000. */
11268 if ((code == LT_EXPR || code == GE_EXPR)
11269 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11270 && CONVERT_EXPR_P (arg1)
11271 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11272 && (element_precision (TREE_TYPE (arg1))
11273 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11274 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11275 || (element_precision (TREE_TYPE (arg1))
11276 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11277 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11279 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11280 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11281 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11282 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11283 build_zero_cst (TREE_TYPE (arg0)));
11288 case UNORDERED_EXPR:
11296 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11298 tree targ0 = strip_float_extensions (arg0);
11299 tree targ1 = strip_float_extensions (arg1);
11300 tree newtype = TREE_TYPE (targ0);
11302 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11303 newtype = TREE_TYPE (targ1);
11305 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11306 return fold_build2_loc (loc, code, type,
11307 fold_convert_loc (loc, newtype, targ0),
11308 fold_convert_loc (loc, newtype, targ1));
11313 case COMPOUND_EXPR:
11314 /* When pedantic, a compound expression can be neither an lvalue
11315 nor an integer constant expression. */
11316 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11318 /* Don't let (0, 0) be null pointer constant. */
11319 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11320 : fold_convert_loc (loc, type, arg1);
11321 return pedantic_non_lvalue_loc (loc, tem);
11324 /* An ASSERT_EXPR should never be passed to fold_binary. */
11325 gcc_unreachable ();
11329 } /* switch (code) */
11332 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11333 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11337 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11339 switch (TREE_CODE (*tp))
11345 *walk_subtrees = 0;
11347 /* ... fall through ... */
11354 /* Return whether the sub-tree ST contains a label which is accessible from
11355 outside the sub-tree. */
11358 contains_label_p (tree st)
11361 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11364 /* Fold a ternary expression of code CODE and type TYPE with operands
11365 OP0, OP1, and OP2. Return the folded expression if folding is
11366 successful. Otherwise, return NULL_TREE. */
11369 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11370 tree op0, tree op1, tree op2)
11373 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11374 enum tree_code_class kind = TREE_CODE_CLASS (code);
11376 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11377 && TREE_CODE_LENGTH (code) == 3);
11379 /* If this is a commutative operation, and OP0 is a constant, move it
11380 to OP1 to reduce the number of tests below. */
11381 if (commutative_ternary_tree_code (code)
11382 && tree_swap_operands_p (op0, op1, true))
11383 return fold_build3_loc (loc, code, type, op1, op0, op2);
11385 tem = generic_simplify (loc, code, type, op0, op1, op2);
11389 /* Strip any conversions that don't change the mode. This is safe
11390 for every expression, except for a comparison expression because
11391 its signedness is derived from its operands. So, in the latter
11392 case, only strip conversions that don't change the signedness.
11394 Note that this is done as an internal manipulation within the
11395 constant folder, in order to find the simplest representation of
11396 the arguments so that their form can be studied. In any cases,
11397 the appropriate type conversions should be put back in the tree
11398 that will get out of the constant folder. */
11419 case COMPONENT_REF:
11420 if (TREE_CODE (arg0) == CONSTRUCTOR
11421 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11423 unsigned HOST_WIDE_INT idx;
11425 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11432 case VEC_COND_EXPR:
11433 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11434 so all simple results must be passed through pedantic_non_lvalue. */
11435 if (TREE_CODE (arg0) == INTEGER_CST)
11437 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11438 tem = integer_zerop (arg0) ? op2 : op1;
11439 /* Only optimize constant conditions when the selected branch
11440 has the same type as the COND_EXPR. This avoids optimizing
11441 away "c ? x : throw", where the throw has a void type.
11442 Avoid throwing away that operand which contains label. */
11443 if ((!TREE_SIDE_EFFECTS (unused_op)
11444 || !contains_label_p (unused_op))
11445 && (! VOID_TYPE_P (TREE_TYPE (tem))
11446 || VOID_TYPE_P (type)))
11447 return pedantic_non_lvalue_loc (loc, tem);
11450 else if (TREE_CODE (arg0) == VECTOR_CST)
11452 if ((TREE_CODE (arg1) == VECTOR_CST
11453 || TREE_CODE (arg1) == CONSTRUCTOR)
11454 && (TREE_CODE (arg2) == VECTOR_CST
11455 || TREE_CODE (arg2) == CONSTRUCTOR))
11457 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11458 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11459 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11460 for (i = 0; i < nelts; i++)
11462 tree val = VECTOR_CST_ELT (arg0, i);
11463 if (integer_all_onesp (val))
11465 else if (integer_zerop (val))
11466 sel[i] = nelts + i;
11467 else /* Currently unreachable. */
11470 tree t = fold_vec_perm (type, arg1, arg2, sel);
11471 if (t != NULL_TREE)
11476 /* If we have A op B ? A : C, we may be able to convert this to a
11477 simpler expression, depending on the operation and the values
11478 of B and C. Signed zeros prevent all of these transformations,
11479 for reasons given above each one.
11481 Also try swapping the arguments and inverting the conditional. */
11482 if (COMPARISON_CLASS_P (arg0)
11483 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11484 arg1, TREE_OPERAND (arg0, 1))
11485 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11487 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11492 if (COMPARISON_CLASS_P (arg0)
11493 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11495 TREE_OPERAND (arg0, 1))
11496 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11498 location_t loc0 = expr_location_or (arg0, loc);
11499 tem = fold_invert_truthvalue (loc0, arg0);
11500 if (tem && COMPARISON_CLASS_P (tem))
11502 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11508 /* If the second operand is simpler than the third, swap them
11509 since that produces better jump optimization results. */
11510 if (truth_value_p (TREE_CODE (arg0))
11511 && tree_swap_operands_p (op1, op2, false))
11513 location_t loc0 = expr_location_or (arg0, loc);
11514 /* See if this can be inverted. If it can't, possibly because
11515 it was a floating-point inequality comparison, don't do
11517 tem = fold_invert_truthvalue (loc0, arg0);
11519 return fold_build3_loc (loc, code, type, tem, op2, op1);
11522 /* Convert A ? 1 : 0 to simply A. */
11523 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11524 : (integer_onep (op1)
11525 && !VECTOR_TYPE_P (type)))
11526 && integer_zerop (op2)
11527 /* If we try to convert OP0 to our type, the
11528 call to fold will try to move the conversion inside
11529 a COND, which will recurse. In that case, the COND_EXPR
11530 is probably the best choice, so leave it alone. */
11531 && type == TREE_TYPE (arg0))
11532 return pedantic_non_lvalue_loc (loc, arg0);
11534 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11535 over COND_EXPR in cases such as floating point comparisons. */
11536 if (integer_zerop (op1)
11537 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
11538 : (integer_onep (op2)
11539 && !VECTOR_TYPE_P (type)))
11540 && truth_value_p (TREE_CODE (arg0)))
11541 return pedantic_non_lvalue_loc (loc,
11542 fold_convert_loc (loc, type,
11543 invert_truthvalue_loc (loc,
11546 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11547 if (TREE_CODE (arg0) == LT_EXPR
11548 && integer_zerop (TREE_OPERAND (arg0, 1))
11549 && integer_zerop (op2)
11550 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11552 /* sign_bit_p looks through both zero and sign extensions,
11553 but for this optimization only sign extensions are
11555 tree tem2 = TREE_OPERAND (arg0, 0);
11556 while (tem != tem2)
11558 if (TREE_CODE (tem2) != NOP_EXPR
11559 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11564 tem2 = TREE_OPERAND (tem2, 0);
11566 /* sign_bit_p only checks ARG1 bits within A's precision.
11567 If <sign bit of A> has wider type than A, bits outside
11568 of A's precision in <sign bit of A> need to be checked.
11569 If they are all 0, this optimization needs to be done
11570 in unsigned A's type, if they are all 1 in signed A's type,
11571 otherwise this can't be done. */
11573 && TYPE_PRECISION (TREE_TYPE (tem))
11574 < TYPE_PRECISION (TREE_TYPE (arg1))
11575 && TYPE_PRECISION (TREE_TYPE (tem))
11576 < TYPE_PRECISION (type))
11578 int inner_width, outer_width;
11581 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11582 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11583 if (outer_width > TYPE_PRECISION (type))
11584 outer_width = TYPE_PRECISION (type);
11586 wide_int mask = wi::shifted_mask
11587 (inner_width, outer_width - inner_width, false,
11588 TYPE_PRECISION (TREE_TYPE (arg1)));
11590 wide_int common = mask & arg1;
11591 if (common == mask)
11593 tem_type = signed_type_for (TREE_TYPE (tem));
11594 tem = fold_convert_loc (loc, tem_type, tem);
11596 else if (common == 0)
11598 tem_type = unsigned_type_for (TREE_TYPE (tem));
11599 tem = fold_convert_loc (loc, tem_type, tem);
11607 fold_convert_loc (loc, type,
11608 fold_build2_loc (loc, BIT_AND_EXPR,
11609 TREE_TYPE (tem), tem,
11610 fold_convert_loc (loc,
11615 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11616 already handled above. */
11617 if (TREE_CODE (arg0) == BIT_AND_EXPR
11618 && integer_onep (TREE_OPERAND (arg0, 1))
11619 && integer_zerop (op2)
11620 && integer_pow2p (arg1))
11622 tree tem = TREE_OPERAND (arg0, 0);
11624 if (TREE_CODE (tem) == RSHIFT_EXPR
11625 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11626 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11627 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11628 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11629 TREE_OPERAND (tem, 0), arg1);
11632 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11633 is probably obsolete because the first operand should be a
11634 truth value (that's why we have the two cases above), but let's
11635 leave it in until we can confirm this for all front-ends. */
11636 if (integer_zerop (op2)
11637 && TREE_CODE (arg0) == NE_EXPR
11638 && integer_zerop (TREE_OPERAND (arg0, 1))
11639 && integer_pow2p (arg1)
11640 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11641 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11642 arg1, OEP_ONLY_CONST))
11643 return pedantic_non_lvalue_loc (loc,
11644 fold_convert_loc (loc, type,
11645 TREE_OPERAND (arg0, 0)));
11647 /* Disable the transformations below for vectors, since
11648 fold_binary_op_with_conditional_arg may undo them immediately,
11649 yielding an infinite loop. */
11650 if (code == VEC_COND_EXPR)
11653 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11654 if (integer_zerop (op2)
11655 && truth_value_p (TREE_CODE (arg0))
11656 && truth_value_p (TREE_CODE (arg1))
11657 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11658 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11659 : TRUTH_ANDIF_EXPR,
11660 type, fold_convert_loc (loc, type, arg0), arg1);
11662 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11663 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11664 && truth_value_p (TREE_CODE (arg0))
11665 && truth_value_p (TREE_CODE (arg1))
11666 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11668 location_t loc0 = expr_location_or (arg0, loc);
11669 /* Only perform transformation if ARG0 is easily inverted. */
11670 tem = fold_invert_truthvalue (loc0, arg0);
11672 return fold_build2_loc (loc, code == VEC_COND_EXPR
11675 type, fold_convert_loc (loc, type, tem),
11679 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11680 if (integer_zerop (arg1)
11681 && truth_value_p (TREE_CODE (arg0))
11682 && truth_value_p (TREE_CODE (op2))
11683 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11685 location_t loc0 = expr_location_or (arg0, loc);
11686 /* Only perform transformation if ARG0 is easily inverted. */
11687 tem = fold_invert_truthvalue (loc0, arg0);
11689 return fold_build2_loc (loc, code == VEC_COND_EXPR
11690 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11691 type, fold_convert_loc (loc, type, tem),
11695 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11696 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11697 && truth_value_p (TREE_CODE (arg0))
11698 && truth_value_p (TREE_CODE (op2))
11699 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11700 return fold_build2_loc (loc, code == VEC_COND_EXPR
11701 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11702 type, fold_convert_loc (loc, type, arg0), op2);
11707 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11708 of fold_ternary on them. */
11709 gcc_unreachable ();
11711 case BIT_FIELD_REF:
11712 if ((TREE_CODE (arg0) == VECTOR_CST
11713 || (TREE_CODE (arg0) == CONSTRUCTOR
11714 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
11715 && (type == TREE_TYPE (TREE_TYPE (arg0))
11716 || (TREE_CODE (type) == VECTOR_TYPE
11717 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11719 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11720 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11721 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11722 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11725 && (idx % width) == 0
11726 && (n % width) == 0
11727 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11732 if (TREE_CODE (arg0) == VECTOR_CST)
11735 return VECTOR_CST_ELT (arg0, idx);
11737 tree *vals = XALLOCAVEC (tree, n);
11738 for (unsigned i = 0; i < n; ++i)
11739 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11740 return build_vector (type, vals);
11743 /* Constructor elements can be subvectors. */
11744 unsigned HOST_WIDE_INT k = 1;
11745 if (CONSTRUCTOR_NELTS (arg0) != 0)
11747 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
11748 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
11749 k = TYPE_VECTOR_SUBPARTS (cons_elem);
11752 /* We keep an exact subset of the constructor elements. */
11753 if ((idx % k) == 0 && (n % k) == 0)
11755 if (CONSTRUCTOR_NELTS (arg0) == 0)
11756 return build_constructor (type, NULL);
11761 if (idx < CONSTRUCTOR_NELTS (arg0))
11762 return CONSTRUCTOR_ELT (arg0, idx)->value;
11763 return build_zero_cst (type);
11766 vec<constructor_elt, va_gc> *vals;
11767 vec_alloc (vals, n);
11768 for (unsigned i = 0;
11769 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
11771 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
11773 (arg0, idx + i)->value);
11774 return build_constructor (type, vals);
11776 /* The bitfield references a single constructor element. */
11777 else if (idx + n <= (idx / k + 1) * k)
11779 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
11780 return build_zero_cst (type);
11782 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
11784 return fold_build3_loc (loc, code, type,
11785 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
11786 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
11791 /* A bit-field-ref that referenced the full argument can be stripped. */
11792 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11793 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
11794 && integer_zerop (op2))
11795 return fold_convert_loc (loc, type, arg0);
11797 /* On constants we can use native encode/interpret to constant
11798 fold (nearly) all BIT_FIELD_REFs. */
11799 if (CONSTANT_CLASS_P (arg0)
11800 && can_native_interpret_type_p (type)
11801 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
11802 /* This limitation should not be necessary, we just need to
11803 round this up to mode size. */
11804 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
11805 /* Need bit-shifting of the buffer to relax the following. */
11806 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
11808 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11809 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11810 unsigned HOST_WIDE_INT clen;
11811 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
11812 /* ??? We cannot tell native_encode_expr to start at
11813 some random byte only. So limit us to a reasonable amount
11817 unsigned char *b = XALLOCAVEC (unsigned char, clen);
11818 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
11820 && len * BITS_PER_UNIT >= bitpos + bitsize)
11822 tree v = native_interpret_expr (type,
11823 b + bitpos / BITS_PER_UNIT,
11824 bitsize / BITS_PER_UNIT);
11834 /* For integers we can decompose the FMA if possible. */
11835 if (TREE_CODE (arg0) == INTEGER_CST
11836 && TREE_CODE (arg1) == INTEGER_CST)
11837 return fold_build2_loc (loc, PLUS_EXPR, type,
11838 const_binop (MULT_EXPR, arg0, arg1), arg2);
11839 if (integer_zerop (arg2))
11840 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11842 return fold_fma (loc, type, arg0, arg1, arg2);
11844 case VEC_PERM_EXPR:
11845 if (TREE_CODE (arg2) == VECTOR_CST)
11847 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11848 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11849 unsigned char *sel2 = sel + nelts;
11850 bool need_mask_canon = false;
11851 bool need_mask_canon2 = false;
11852 bool all_in_vec0 = true;
11853 bool all_in_vec1 = true;
11854 bool maybe_identity = true;
11855 bool single_arg = (op0 == op1);
11856 bool changed = false;
11858 mask2 = 2 * nelts - 1;
11859 mask = single_arg ? (nelts - 1) : mask2;
11860 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11861 for (i = 0; i < nelts; i++)
11863 tree val = VECTOR_CST_ELT (arg2, i);
11864 if (TREE_CODE (val) != INTEGER_CST)
11867 /* Make sure that the perm value is in an acceptable
11870 need_mask_canon |= wi::gtu_p (t, mask);
11871 need_mask_canon2 |= wi::gtu_p (t, mask2);
11872 sel[i] = t.to_uhwi () & mask;
11873 sel2[i] = t.to_uhwi () & mask2;
11875 if (sel[i] < nelts)
11876 all_in_vec1 = false;
11878 all_in_vec0 = false;
11880 if ((sel[i] & (nelts-1)) != i)
11881 maybe_identity = false;
11884 if (maybe_identity)
11894 else if (all_in_vec1)
11897 for (i = 0; i < nelts; i++)
11899 need_mask_canon = true;
11902 if ((TREE_CODE (op0) == VECTOR_CST
11903 || TREE_CODE (op0) == CONSTRUCTOR)
11904 && (TREE_CODE (op1) == VECTOR_CST
11905 || TREE_CODE (op1) == CONSTRUCTOR))
11907 tree t = fold_vec_perm (type, op0, op1, sel);
11908 if (t != NULL_TREE)
11912 if (op0 == op1 && !single_arg)
11915 /* Some targets are deficient and fail to expand a single
11916 argument permutation while still allowing an equivalent
11917 2-argument version. */
11918 if (need_mask_canon && arg2 == op2
11919 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11920 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11922 need_mask_canon = need_mask_canon2;
11926 if (need_mask_canon && arg2 == op2)
11928 tree *tsel = XALLOCAVEC (tree, nelts);
11929 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11930 for (i = 0; i < nelts; i++)
11931 tsel[i] = build_int_cst (eltype, sel[i]);
11932 op2 = build_vector (TREE_TYPE (arg2), tsel);
11937 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11943 } /* switch (code) */
11946 /* Perform constant folding and related simplification of EXPR.
11947 The related simplifications include x*1 => x, x*0 => 0, etc.,
11948 and application of the associative law.
11949 NOP_EXPR conversions may be removed freely (as long as we
11950 are careful not to change the type of the overall expression).
11951 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11952 but we can constant-fold them if they have constant operands. */
11954 #ifdef ENABLE_FOLD_CHECKING
11955 # define fold(x) fold_1 (x)
11956 static tree fold_1 (tree);
11962 const tree t = expr;
11963 enum tree_code code = TREE_CODE (t);
11964 enum tree_code_class kind = TREE_CODE_CLASS (code);
11966 location_t loc = EXPR_LOCATION (expr);
11968 /* Return right away if a constant. */
11969 if (kind == tcc_constant)
11972 /* CALL_EXPR-like objects with variable numbers of operands are
11973 treated specially. */
11974 if (kind == tcc_vl_exp)
11976 if (code == CALL_EXPR)
11978 tem = fold_call_expr (loc, expr, false);
11979 return tem ? tem : expr;
11984 if (IS_EXPR_CODE_CLASS (kind))
11986 tree type = TREE_TYPE (t);
11987 tree op0, op1, op2;
11989 switch (TREE_CODE_LENGTH (code))
11992 op0 = TREE_OPERAND (t, 0);
11993 tem = fold_unary_loc (loc, code, type, op0);
11994 return tem ? tem : expr;
11996 op0 = TREE_OPERAND (t, 0);
11997 op1 = TREE_OPERAND (t, 1);
11998 tem = fold_binary_loc (loc, code, type, op0, op1);
11999 return tem ? tem : expr;
12001 op0 = TREE_OPERAND (t, 0);
12002 op1 = TREE_OPERAND (t, 1);
12003 op2 = TREE_OPERAND (t, 2);
12004 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12005 return tem ? tem : expr;
12015 tree op0 = TREE_OPERAND (t, 0);
12016 tree op1 = TREE_OPERAND (t, 1);
12018 if (TREE_CODE (op1) == INTEGER_CST
12019 && TREE_CODE (op0) == CONSTRUCTOR
12020 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12022 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
12023 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
12024 unsigned HOST_WIDE_INT begin = 0;
12026 /* Find a matching index by means of a binary search. */
12027 while (begin != end)
12029 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
12030 tree index = (*elts)[middle].index;
12032 if (TREE_CODE (index) == INTEGER_CST
12033 && tree_int_cst_lt (index, op1))
12034 begin = middle + 1;
12035 else if (TREE_CODE (index) == INTEGER_CST
12036 && tree_int_cst_lt (op1, index))
12038 else if (TREE_CODE (index) == RANGE_EXPR
12039 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
12040 begin = middle + 1;
12041 else if (TREE_CODE (index) == RANGE_EXPR
12042 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
12045 return (*elts)[middle].value;
12052 /* Return a VECTOR_CST if possible. */
12055 tree type = TREE_TYPE (t);
12056 if (TREE_CODE (type) != VECTOR_TYPE)
12059 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
12060 unsigned HOST_WIDE_INT idx, pos = 0;
12063 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
12065 if (!CONSTANT_CLASS_P (value))
12067 if (TREE_CODE (value) == VECTOR_CST)
12069 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
12070 vec[pos++] = VECTOR_CST_ELT (value, i);
12073 vec[pos++] = value;
12075 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
12076 vec[pos] = build_zero_cst (TREE_TYPE (type));
12078 return build_vector (type, vec);
12082 return fold (DECL_INITIAL (t));
12086 } /* switch (code) */
12089 #ifdef ENABLE_FOLD_CHECKING
12092 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12093 hash_table<nofree_ptr_hash<const tree_node> > *);
12094 static void fold_check_failed (const_tree, const_tree);
12095 void print_fold_checksum (const_tree);
12097 /* When --enable-checking=fold, compute a digest of expr before
12098 and after actual fold call to see if fold did not accidentally
12099 change original expr. */
12105 struct md5_ctx ctx;
12106 unsigned char checksum_before[16], checksum_after[16];
12107 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12109 md5_init_ctx (&ctx);
12110 fold_checksum_tree (expr, &ctx, &ht);
12111 md5_finish_ctx (&ctx, checksum_before);
12114 ret = fold_1 (expr);
12116 md5_init_ctx (&ctx);
12117 fold_checksum_tree (expr, &ctx, &ht);
12118 md5_finish_ctx (&ctx, checksum_after);
12120 if (memcmp (checksum_before, checksum_after, 16))
12121 fold_check_failed (expr, ret);
12127 print_fold_checksum (const_tree expr)
12129 struct md5_ctx ctx;
12130 unsigned char checksum[16], cnt;
12131 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12133 md5_init_ctx (&ctx);
12134 fold_checksum_tree (expr, &ctx, &ht);
12135 md5_finish_ctx (&ctx, checksum);
12136 for (cnt = 0; cnt < 16; ++cnt)
12137 fprintf (stderr, "%02x", checksum[cnt]);
12138 putc ('\n', stderr);
12142 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12144 internal_error ("fold check: original tree changed by fold");
12148 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12149 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12151 const tree_node **slot;
12152 enum tree_code code;
12153 union tree_node buf;
12159 slot = ht->find_slot (expr, INSERT);
12163 code = TREE_CODE (expr);
12164 if (TREE_CODE_CLASS (code) == tcc_declaration
12165 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12167 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12168 memcpy ((char *) &buf, expr, tree_size (expr));
12169 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12170 buf.decl_with_vis.symtab_node = NULL;
12171 expr = (tree) &buf;
12173 else if (TREE_CODE_CLASS (code) == tcc_type
12174 && (TYPE_POINTER_TO (expr)
12175 || TYPE_REFERENCE_TO (expr)
12176 || TYPE_CACHED_VALUES_P (expr)
12177 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12178 || TYPE_NEXT_VARIANT (expr)))
12180 /* Allow these fields to be modified. */
12182 memcpy ((char *) &buf, expr, tree_size (expr));
12183 expr = tmp = (tree) &buf;
12184 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12185 TYPE_POINTER_TO (tmp) = NULL;
12186 TYPE_REFERENCE_TO (tmp) = NULL;
12187 TYPE_NEXT_VARIANT (tmp) = NULL;
12188 if (TYPE_CACHED_VALUES_P (tmp))
12190 TYPE_CACHED_VALUES_P (tmp) = 0;
12191 TYPE_CACHED_VALUES (tmp) = NULL;
12194 md5_process_bytes (expr, tree_size (expr), ctx);
12195 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12196 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12197 if (TREE_CODE_CLASS (code) != tcc_type
12198 && TREE_CODE_CLASS (code) != tcc_declaration
12199 && code != TREE_LIST
12200 && code != SSA_NAME
12201 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12202 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12203 switch (TREE_CODE_CLASS (code))
12209 md5_process_bytes (TREE_STRING_POINTER (expr),
12210 TREE_STRING_LENGTH (expr), ctx);
12213 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12214 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12217 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12218 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12224 case tcc_exceptional:
12228 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12229 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12230 expr = TREE_CHAIN (expr);
12231 goto recursive_label;
12234 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12235 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12241 case tcc_expression:
12242 case tcc_reference:
12243 case tcc_comparison:
12246 case tcc_statement:
12248 len = TREE_OPERAND_LENGTH (expr);
12249 for (i = 0; i < len; ++i)
12250 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12252 case tcc_declaration:
12253 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12254 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12255 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12257 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12258 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12259 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12260 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12261 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12264 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12266 if (TREE_CODE (expr) == FUNCTION_DECL)
12268 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12269 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12271 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12275 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12276 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12277 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12278 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12279 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12280 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12281 if (INTEGRAL_TYPE_P (expr)
12282 || SCALAR_FLOAT_TYPE_P (expr))
12284 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12285 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12287 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12288 if (TREE_CODE (expr) == RECORD_TYPE
12289 || TREE_CODE (expr) == UNION_TYPE
12290 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12291 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12292 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12299 /* Helper function for outputting the checksum of a tree T. When
12300 debugging with gdb, you can "define mynext" to be "next" followed
12301 by "call debug_fold_checksum (op0)", then just trace down till the
12304 DEBUG_FUNCTION void
12305 debug_fold_checksum (const_tree t)
12308 unsigned char checksum[16];
12309 struct md5_ctx ctx;
12310 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12312 md5_init_ctx (&ctx);
12313 fold_checksum_tree (t, &ctx, &ht);
12314 md5_finish_ctx (&ctx, checksum);
12317 for (i = 0; i < 16; i++)
12318 fprintf (stderr, "%d ", checksum[i]);
12320 fprintf (stderr, "\n");
12325 /* Fold a unary tree expression with code CODE of type TYPE with an
12326 operand OP0. LOC is the location of the resulting expression.
12327 Return a folded expression if successful. Otherwise, return a tree
12328 expression with code CODE of type TYPE with an operand OP0. */
12331 fold_build1_stat_loc (location_t loc,
12332 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12335 #ifdef ENABLE_FOLD_CHECKING
12336 unsigned char checksum_before[16], checksum_after[16];
12337 struct md5_ctx ctx;
12338 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12340 md5_init_ctx (&ctx);
12341 fold_checksum_tree (op0, &ctx, &ht);
12342 md5_finish_ctx (&ctx, checksum_before);
12346 tem = fold_unary_loc (loc, code, type, op0);
12348 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12350 #ifdef ENABLE_FOLD_CHECKING
12351 md5_init_ctx (&ctx);
12352 fold_checksum_tree (op0, &ctx, &ht);
12353 md5_finish_ctx (&ctx, checksum_after);
12355 if (memcmp (checksum_before, checksum_after, 16))
12356 fold_check_failed (op0, tem);
12361 /* Fold a binary tree expression with code CODE of type TYPE with
12362 operands OP0 and OP1. LOC is the location of the resulting
12363 expression. Return a folded expression if successful. Otherwise,
12364 return a tree expression with code CODE of type TYPE with operands
12368 fold_build2_stat_loc (location_t loc,
12369 enum tree_code code, tree type, tree op0, tree op1
12373 #ifdef ENABLE_FOLD_CHECKING
12374 unsigned char checksum_before_op0[16],
12375 checksum_before_op1[16],
12376 checksum_after_op0[16],
12377 checksum_after_op1[16];
12378 struct md5_ctx ctx;
12379 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12381 md5_init_ctx (&ctx);
12382 fold_checksum_tree (op0, &ctx, &ht);
12383 md5_finish_ctx (&ctx, checksum_before_op0);
12386 md5_init_ctx (&ctx);
12387 fold_checksum_tree (op1, &ctx, &ht);
12388 md5_finish_ctx (&ctx, checksum_before_op1);
12392 tem = fold_binary_loc (loc, code, type, op0, op1);
12394 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12396 #ifdef ENABLE_FOLD_CHECKING
12397 md5_init_ctx (&ctx);
12398 fold_checksum_tree (op0, &ctx, &ht);
12399 md5_finish_ctx (&ctx, checksum_after_op0);
12402 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12403 fold_check_failed (op0, tem);
12405 md5_init_ctx (&ctx);
12406 fold_checksum_tree (op1, &ctx, &ht);
12407 md5_finish_ctx (&ctx, checksum_after_op1);
12409 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12410 fold_check_failed (op1, tem);
12415 /* Fold a ternary tree expression with code CODE of type TYPE with
12416 operands OP0, OP1, and OP2. Return a folded expression if
12417 successful. Otherwise, return a tree expression with code CODE of
12418 type TYPE with operands OP0, OP1, and OP2. */
12421 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12422 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12425 #ifdef ENABLE_FOLD_CHECKING
12426 unsigned char checksum_before_op0[16],
12427 checksum_before_op1[16],
12428 checksum_before_op2[16],
12429 checksum_after_op0[16],
12430 checksum_after_op1[16],
12431 checksum_after_op2[16];
12432 struct md5_ctx ctx;
12433 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12435 md5_init_ctx (&ctx);
12436 fold_checksum_tree (op0, &ctx, &ht);
12437 md5_finish_ctx (&ctx, checksum_before_op0);
12440 md5_init_ctx (&ctx);
12441 fold_checksum_tree (op1, &ctx, &ht);
12442 md5_finish_ctx (&ctx, checksum_before_op1);
12445 md5_init_ctx (&ctx);
12446 fold_checksum_tree (op2, &ctx, &ht);
12447 md5_finish_ctx (&ctx, checksum_before_op2);
12451 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12452 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12454 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12456 #ifdef ENABLE_FOLD_CHECKING
12457 md5_init_ctx (&ctx);
12458 fold_checksum_tree (op0, &ctx, &ht);
12459 md5_finish_ctx (&ctx, checksum_after_op0);
12462 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12463 fold_check_failed (op0, tem);
12465 md5_init_ctx (&ctx);
12466 fold_checksum_tree (op1, &ctx, &ht);
12467 md5_finish_ctx (&ctx, checksum_after_op1);
12470 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12471 fold_check_failed (op1, tem);
12473 md5_init_ctx (&ctx);
12474 fold_checksum_tree (op2, &ctx, &ht);
12475 md5_finish_ctx (&ctx, checksum_after_op2);
12477 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12478 fold_check_failed (op2, tem);
12483 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12484 arguments in ARGARRAY, and a null static chain.
12485 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12486 of type TYPE from the given operands as constructed by build_call_array. */
12489 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12490 int nargs, tree *argarray)
12493 #ifdef ENABLE_FOLD_CHECKING
12494 unsigned char checksum_before_fn[16],
12495 checksum_before_arglist[16],
12496 checksum_after_fn[16],
12497 checksum_after_arglist[16];
12498 struct md5_ctx ctx;
12499 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12502 md5_init_ctx (&ctx);
12503 fold_checksum_tree (fn, &ctx, &ht);
12504 md5_finish_ctx (&ctx, checksum_before_fn);
12507 md5_init_ctx (&ctx);
12508 for (i = 0; i < nargs; i++)
12509 fold_checksum_tree (argarray[i], &ctx, &ht);
12510 md5_finish_ctx (&ctx, checksum_before_arglist);
12514 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12516 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12518 #ifdef ENABLE_FOLD_CHECKING
12519 md5_init_ctx (&ctx);
12520 fold_checksum_tree (fn, &ctx, &ht);
12521 md5_finish_ctx (&ctx, checksum_after_fn);
12524 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12525 fold_check_failed (fn, tem);
12527 md5_init_ctx (&ctx);
12528 for (i = 0; i < nargs; i++)
12529 fold_checksum_tree (argarray[i], &ctx, &ht);
12530 md5_finish_ctx (&ctx, checksum_after_arglist);
12532 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12533 fold_check_failed (NULL_TREE, tem);
12538 /* Perform constant folding and related simplification of initializer
12539 expression EXPR. These behave identically to "fold_buildN" but ignore
12540 potential run-time traps and exceptions that fold must preserve. */
12542 #define START_FOLD_INIT \
12543 int saved_signaling_nans = flag_signaling_nans;\
12544 int saved_trapping_math = flag_trapping_math;\
12545 int saved_rounding_math = flag_rounding_math;\
12546 int saved_trapv = flag_trapv;\
12547 int saved_folding_initializer = folding_initializer;\
12548 flag_signaling_nans = 0;\
12549 flag_trapping_math = 0;\
12550 flag_rounding_math = 0;\
12552 folding_initializer = 1;
12554 #define END_FOLD_INIT \
12555 flag_signaling_nans = saved_signaling_nans;\
12556 flag_trapping_math = saved_trapping_math;\
12557 flag_rounding_math = saved_rounding_math;\
12558 flag_trapv = saved_trapv;\
12559 folding_initializer = saved_folding_initializer;
12562 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12563 tree type, tree op)
12568 result = fold_build1_loc (loc, code, type, op);
12575 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12576 tree type, tree op0, tree op1)
12581 result = fold_build2_loc (loc, code, type, op0, op1);
12588 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12589 int nargs, tree *argarray)
12594 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12600 #undef START_FOLD_INIT
12601 #undef END_FOLD_INIT
12603 /* Determine if first argument is a multiple of second argument. Return 0 if
12604 it is not, or we cannot easily determined it to be.
12606 An example of the sort of thing we care about (at this point; this routine
12607 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12608 fold cases do now) is discovering that
12610 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12616 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12618 This code also handles discovering that
12620 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12622 is a multiple of 8 so we don't have to worry about dealing with a
12623 possible remainder.
12625 Note that we *look* inside a SAVE_EXPR only to determine how it was
12626 calculated; it is not safe for fold to do much of anything else with the
12627 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12628 at run time. For example, the latter example above *cannot* be implemented
12629 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12630 evaluation time of the original SAVE_EXPR is not necessarily the same at
12631 the time the new expression is evaluated. The only optimization of this
12632 sort that would be valid is changing
12634 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12638 SAVE_EXPR (I) * SAVE_EXPR (J)
12640 (where the same SAVE_EXPR (J) is used in the original and the
12641 transformed version). */
12644 multiple_of_p (tree type, const_tree top, const_tree bottom)
12646 if (operand_equal_p (top, bottom, 0))
12649 if (TREE_CODE (type) != INTEGER_TYPE)
12652 switch (TREE_CODE (top))
12655 /* Bitwise and provides a power of two multiple. If the mask is
12656 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12657 if (!integer_pow2p (bottom))
12662 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12663 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12667 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12668 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12671 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12675 op1 = TREE_OPERAND (top, 1);
12676 /* const_binop may not detect overflow correctly,
12677 so check for it explicitly here. */
12678 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12679 && 0 != (t1 = fold_convert (type,
12680 const_binop (LSHIFT_EXPR,
12683 && !TREE_OVERFLOW (t1))
12684 return multiple_of_p (type, t1, bottom);
12689 /* Can't handle conversions from non-integral or wider integral type. */
12690 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12691 || (TYPE_PRECISION (type)
12692 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12695 /* .. fall through ... */
12698 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12701 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12702 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12705 if (TREE_CODE (bottom) != INTEGER_CST
12706 || integer_zerop (bottom)
12707 || (TYPE_UNSIGNED (type)
12708 && (tree_int_cst_sgn (top) < 0
12709 || tree_int_cst_sgn (bottom) < 0)))
12711 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12719 /* Return true if CODE or TYPE is known to be non-negative. */
12722 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12724 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12725 && truth_value_p (code))
12726 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12727 have a signed:1 type (where the value is -1 and 0). */
12732 /* Return true if (CODE OP0) is known to be non-negative. If the return
12733 value is based on the assumption that signed overflow is undefined,
12734 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12735 *STRICT_OVERFLOW_P. */
12738 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12739 bool *strict_overflow_p)
12741 if (TYPE_UNSIGNED (type))
12747 /* We can't return 1 if flag_wrapv is set because
12748 ABS_EXPR<INT_MIN> = INT_MIN. */
12749 if (!ANY_INTEGRAL_TYPE_P (type))
12751 if (TYPE_OVERFLOW_UNDEFINED (type))
12753 *strict_overflow_p = true;
12758 case NON_LVALUE_EXPR:
12760 case FIX_TRUNC_EXPR:
12761 return tree_expr_nonnegative_warnv_p (op0,
12762 strict_overflow_p);
12766 tree inner_type = TREE_TYPE (op0);
12767 tree outer_type = type;
12769 if (TREE_CODE (outer_type) == REAL_TYPE)
12771 if (TREE_CODE (inner_type) == REAL_TYPE)
12772 return tree_expr_nonnegative_warnv_p (op0,
12773 strict_overflow_p);
12774 if (INTEGRAL_TYPE_P (inner_type))
12776 if (TYPE_UNSIGNED (inner_type))
12778 return tree_expr_nonnegative_warnv_p (op0,
12779 strict_overflow_p);
12782 else if (INTEGRAL_TYPE_P (outer_type))
12784 if (TREE_CODE (inner_type) == REAL_TYPE)
12785 return tree_expr_nonnegative_warnv_p (op0,
12786 strict_overflow_p);
12787 if (INTEGRAL_TYPE_P (inner_type))
12788 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12789 && TYPE_UNSIGNED (inner_type);
12795 return tree_simple_nonnegative_warnv_p (code, type);
12798 /* We don't know sign of `t', so be conservative and return false. */
12802 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12803 value is based on the assumption that signed overflow is undefined,
12804 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12805 *STRICT_OVERFLOW_P. */
12808 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12809 tree op1, bool *strict_overflow_p)
12811 if (TYPE_UNSIGNED (type))
12816 case POINTER_PLUS_EXPR:
12818 if (FLOAT_TYPE_P (type))
12819 return (tree_expr_nonnegative_warnv_p (op0,
12821 && tree_expr_nonnegative_warnv_p (op1,
12822 strict_overflow_p));
12824 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12825 both unsigned and at least 2 bits shorter than the result. */
12826 if (TREE_CODE (type) == INTEGER_TYPE
12827 && TREE_CODE (op0) == NOP_EXPR
12828 && TREE_CODE (op1) == NOP_EXPR)
12830 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12831 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12832 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12833 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12835 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12836 TYPE_PRECISION (inner2)) + 1;
12837 return prec < TYPE_PRECISION (type);
12843 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12845 /* x * x is always non-negative for floating point x
12846 or without overflow. */
12847 if (operand_equal_p (op0, op1, 0)
12848 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
12849 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
12851 if (ANY_INTEGRAL_TYPE_P (type)
12852 && TYPE_OVERFLOW_UNDEFINED (type))
12853 *strict_overflow_p = true;
12858 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12859 both unsigned and their total bits is shorter than the result. */
12860 if (TREE_CODE (type) == INTEGER_TYPE
12861 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12862 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12864 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12865 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12867 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12868 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12871 bool unsigned0 = TYPE_UNSIGNED (inner0);
12872 bool unsigned1 = TYPE_UNSIGNED (inner1);
12874 if (TREE_CODE (op0) == INTEGER_CST)
12875 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12877 if (TREE_CODE (op1) == INTEGER_CST)
12878 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12880 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12881 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12883 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12884 ? tree_int_cst_min_precision (op0, UNSIGNED)
12885 : TYPE_PRECISION (inner0);
12887 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12888 ? tree_int_cst_min_precision (op1, UNSIGNED)
12889 : TYPE_PRECISION (inner1);
12891 return precision0 + precision1 < TYPE_PRECISION (type);
12898 return (tree_expr_nonnegative_warnv_p (op0,
12900 || tree_expr_nonnegative_warnv_p (op1,
12901 strict_overflow_p));
12907 case TRUNC_DIV_EXPR:
12908 case CEIL_DIV_EXPR:
12909 case FLOOR_DIV_EXPR:
12910 case ROUND_DIV_EXPR:
12911 return (tree_expr_nonnegative_warnv_p (op0,
12913 && tree_expr_nonnegative_warnv_p (op1,
12914 strict_overflow_p));
12916 case TRUNC_MOD_EXPR:
12917 case CEIL_MOD_EXPR:
12918 case FLOOR_MOD_EXPR:
12919 case ROUND_MOD_EXPR:
12920 return tree_expr_nonnegative_warnv_p (op0,
12921 strict_overflow_p);
12923 return tree_simple_nonnegative_warnv_p (code, type);
12926 /* We don't know sign of `t', so be conservative and return false. */
12930 /* Return true if T is known to be non-negative. If the return
12931 value is based on the assumption that signed overflow is undefined,
12932 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12933 *STRICT_OVERFLOW_P. */
12936 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
12938 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12941 switch (TREE_CODE (t))
12944 return tree_int_cst_sgn (t) >= 0;
12947 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12950 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12953 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
12955 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
12956 strict_overflow_p));
12958 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
12961 /* We don't know sign of `t', so be conservative and return false. */
12965 /* Return true if T is known to be non-negative. If the return
12966 value is based on the assumption that signed overflow is undefined,
12967 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12968 *STRICT_OVERFLOW_P. */
12971 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
12972 tree arg0, tree arg1, bool *strict_overflow_p)
12974 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
12975 switch (DECL_FUNCTION_CODE (fndecl))
12977 CASE_FLT_FN (BUILT_IN_ACOS):
12978 CASE_FLT_FN (BUILT_IN_ACOSH):
12979 CASE_FLT_FN (BUILT_IN_CABS):
12980 CASE_FLT_FN (BUILT_IN_COSH):
12981 CASE_FLT_FN (BUILT_IN_ERFC):
12982 CASE_FLT_FN (BUILT_IN_EXP):
12983 CASE_FLT_FN (BUILT_IN_EXP10):
12984 CASE_FLT_FN (BUILT_IN_EXP2):
12985 CASE_FLT_FN (BUILT_IN_FABS):
12986 CASE_FLT_FN (BUILT_IN_FDIM):
12987 CASE_FLT_FN (BUILT_IN_HYPOT):
12988 CASE_FLT_FN (BUILT_IN_POW10):
12989 CASE_INT_FN (BUILT_IN_FFS):
12990 CASE_INT_FN (BUILT_IN_PARITY):
12991 CASE_INT_FN (BUILT_IN_POPCOUNT):
12992 CASE_INT_FN (BUILT_IN_CLZ):
12993 CASE_INT_FN (BUILT_IN_CLRSB):
12994 case BUILT_IN_BSWAP32:
12995 case BUILT_IN_BSWAP64:
12999 CASE_FLT_FN (BUILT_IN_SQRT):
13000 /* sqrt(-0.0) is -0.0. */
13001 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13003 return tree_expr_nonnegative_warnv_p (arg0,
13004 strict_overflow_p);
13006 CASE_FLT_FN (BUILT_IN_ASINH):
13007 CASE_FLT_FN (BUILT_IN_ATAN):
13008 CASE_FLT_FN (BUILT_IN_ATANH):
13009 CASE_FLT_FN (BUILT_IN_CBRT):
13010 CASE_FLT_FN (BUILT_IN_CEIL):
13011 CASE_FLT_FN (BUILT_IN_ERF):
13012 CASE_FLT_FN (BUILT_IN_EXPM1):
13013 CASE_FLT_FN (BUILT_IN_FLOOR):
13014 CASE_FLT_FN (BUILT_IN_FMOD):
13015 CASE_FLT_FN (BUILT_IN_FREXP):
13016 CASE_FLT_FN (BUILT_IN_ICEIL):
13017 CASE_FLT_FN (BUILT_IN_IFLOOR):
13018 CASE_FLT_FN (BUILT_IN_IRINT):
13019 CASE_FLT_FN (BUILT_IN_IROUND):
13020 CASE_FLT_FN (BUILT_IN_LCEIL):
13021 CASE_FLT_FN (BUILT_IN_LDEXP):
13022 CASE_FLT_FN (BUILT_IN_LFLOOR):
13023 CASE_FLT_FN (BUILT_IN_LLCEIL):
13024 CASE_FLT_FN (BUILT_IN_LLFLOOR):
13025 CASE_FLT_FN (BUILT_IN_LLRINT):
13026 CASE_FLT_FN (BUILT_IN_LLROUND):
13027 CASE_FLT_FN (BUILT_IN_LRINT):
13028 CASE_FLT_FN (BUILT_IN_LROUND):
13029 CASE_FLT_FN (BUILT_IN_MODF):
13030 CASE_FLT_FN (BUILT_IN_NEARBYINT):
13031 CASE_FLT_FN (BUILT_IN_RINT):
13032 CASE_FLT_FN (BUILT_IN_ROUND):
13033 CASE_FLT_FN (BUILT_IN_SCALB):
13034 CASE_FLT_FN (BUILT_IN_SCALBLN):
13035 CASE_FLT_FN (BUILT_IN_SCALBN):
13036 CASE_FLT_FN (BUILT_IN_SIGNBIT):
13037 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
13038 CASE_FLT_FN (BUILT_IN_SINH):
13039 CASE_FLT_FN (BUILT_IN_TANH):
13040 CASE_FLT_FN (BUILT_IN_TRUNC):
13041 /* True if the 1st argument is nonnegative. */
13042 return tree_expr_nonnegative_warnv_p (arg0,
13043 strict_overflow_p);
13045 CASE_FLT_FN (BUILT_IN_FMAX):
13046 /* True if the 1st OR 2nd arguments are nonnegative. */
13047 return (tree_expr_nonnegative_warnv_p (arg0,
13049 || (tree_expr_nonnegative_warnv_p (arg1,
13050 strict_overflow_p)));
13052 CASE_FLT_FN (BUILT_IN_FMIN):
13053 /* True if the 1st AND 2nd arguments are nonnegative. */
13054 return (tree_expr_nonnegative_warnv_p (arg0,
13056 && (tree_expr_nonnegative_warnv_p (arg1,
13057 strict_overflow_p)));
13059 CASE_FLT_FN (BUILT_IN_COPYSIGN):
13060 /* True if the 2nd argument is nonnegative. */
13061 return tree_expr_nonnegative_warnv_p (arg1,
13062 strict_overflow_p);
13064 CASE_FLT_FN (BUILT_IN_POWI):
13065 /* True if the 1st argument is nonnegative or the second
13066 argument is an even integer. */
13067 if (TREE_CODE (arg1) == INTEGER_CST
13068 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13070 return tree_expr_nonnegative_warnv_p (arg0,
13071 strict_overflow_p);
13073 CASE_FLT_FN (BUILT_IN_POW):
13074 /* True if the 1st argument is nonnegative or the second
13075 argument is an even integer valued real. */
13076 if (TREE_CODE (arg1) == REAL_CST)
13081 c = TREE_REAL_CST (arg1);
13082 n = real_to_integer (&c);
13085 REAL_VALUE_TYPE cint;
13086 real_from_integer (&cint, VOIDmode, n, SIGNED);
13087 if (real_identical (&c, &cint))
13091 return tree_expr_nonnegative_warnv_p (arg0,
13092 strict_overflow_p);
13097 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
13101 /* Return true if T is known to be non-negative. If the return
13102 value is based on the assumption that signed overflow is undefined,
13103 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13104 *STRICT_OVERFLOW_P. */
13107 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13109 enum tree_code code = TREE_CODE (t);
13110 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13117 tree temp = TARGET_EXPR_SLOT (t);
13118 t = TARGET_EXPR_INITIAL (t);
13120 /* If the initializer is non-void, then it's a normal expression
13121 that will be assigned to the slot. */
13122 if (!VOID_TYPE_P (t))
13123 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13125 /* Otherwise, the initializer sets the slot in some way. One common
13126 way is an assignment statement at the end of the initializer. */
13129 if (TREE_CODE (t) == BIND_EXPR)
13130 t = expr_last (BIND_EXPR_BODY (t));
13131 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13132 || TREE_CODE (t) == TRY_CATCH_EXPR)
13133 t = expr_last (TREE_OPERAND (t, 0));
13134 else if (TREE_CODE (t) == STATEMENT_LIST)
13139 if (TREE_CODE (t) == MODIFY_EXPR
13140 && TREE_OPERAND (t, 0) == temp)
13141 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13142 strict_overflow_p);
13149 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13150 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13152 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13153 get_callee_fndecl (t),
13156 strict_overflow_p);
13158 case COMPOUND_EXPR:
13160 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13161 strict_overflow_p);
13163 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13164 strict_overflow_p);
13166 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13167 strict_overflow_p);
13170 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
13174 /* We don't know sign of `t', so be conservative and return false. */
13178 /* Return true if T is known to be non-negative. If the return
13179 value is based on the assumption that signed overflow is undefined,
13180 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13181 *STRICT_OVERFLOW_P. */
13184 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13186 enum tree_code code;
13187 if (t == error_mark_node)
13190 code = TREE_CODE (t);
13191 switch (TREE_CODE_CLASS (code))
13194 case tcc_comparison:
13195 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13197 TREE_OPERAND (t, 0),
13198 TREE_OPERAND (t, 1),
13199 strict_overflow_p);
13202 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13204 TREE_OPERAND (t, 0),
13205 strict_overflow_p);
13208 case tcc_declaration:
13209 case tcc_reference:
13210 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13218 case TRUTH_AND_EXPR:
13219 case TRUTH_OR_EXPR:
13220 case TRUTH_XOR_EXPR:
13221 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13223 TREE_OPERAND (t, 0),
13224 TREE_OPERAND (t, 1),
13225 strict_overflow_p);
13226 case TRUTH_NOT_EXPR:
13227 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13229 TREE_OPERAND (t, 0),
13230 strict_overflow_p);
13237 case WITH_SIZE_EXPR:
13239 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13242 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
13246 /* Return true if `t' is known to be non-negative. Handle warnings
13247 about undefined signed overflow. */
13250 tree_expr_nonnegative_p (tree t)
13252 bool ret, strict_overflow_p;
13254 strict_overflow_p = false;
13255 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13256 if (strict_overflow_p)
13257 fold_overflow_warning (("assuming signed overflow does not occur when "
13258 "determining that expression is always "
13260 WARN_STRICT_OVERFLOW_MISC);
13265 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13266 For floating point we further ensure that T is not denormal.
13267 Similar logic is present in nonzero_address in rtlanal.h.
13269 If the return value is based on the assumption that signed overflow
13270 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13271 change *STRICT_OVERFLOW_P. */
13274 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13275 bool *strict_overflow_p)
13280 return tree_expr_nonzero_warnv_p (op0,
13281 strict_overflow_p);
13285 tree inner_type = TREE_TYPE (op0);
13286 tree outer_type = type;
13288 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13289 && tree_expr_nonzero_warnv_p (op0,
13290 strict_overflow_p));
13294 case NON_LVALUE_EXPR:
13295 return tree_expr_nonzero_warnv_p (op0,
13296 strict_overflow_p);
13305 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13306 For floating point we further ensure that T is not denormal.
13307 Similar logic is present in nonzero_address in rtlanal.h.
13309 If the return value is based on the assumption that signed overflow
13310 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13311 change *STRICT_OVERFLOW_P. */
13314 tree_binary_nonzero_warnv_p (enum tree_code code,
13317 tree op1, bool *strict_overflow_p)
13319 bool sub_strict_overflow_p;
13322 case POINTER_PLUS_EXPR:
13324 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13326 /* With the presence of negative values it is hard
13327 to say something. */
13328 sub_strict_overflow_p = false;
13329 if (!tree_expr_nonnegative_warnv_p (op0,
13330 &sub_strict_overflow_p)
13331 || !tree_expr_nonnegative_warnv_p (op1,
13332 &sub_strict_overflow_p))
13334 /* One of operands must be positive and the other non-negative. */
13335 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13336 overflows, on a twos-complement machine the sum of two
13337 nonnegative numbers can never be zero. */
13338 return (tree_expr_nonzero_warnv_p (op0,
13340 || tree_expr_nonzero_warnv_p (op1,
13341 strict_overflow_p));
13346 if (TYPE_OVERFLOW_UNDEFINED (type))
13348 if (tree_expr_nonzero_warnv_p (op0,
13350 && tree_expr_nonzero_warnv_p (op1,
13351 strict_overflow_p))
13353 *strict_overflow_p = true;
13360 sub_strict_overflow_p = false;
13361 if (tree_expr_nonzero_warnv_p (op0,
13362 &sub_strict_overflow_p)
13363 && tree_expr_nonzero_warnv_p (op1,
13364 &sub_strict_overflow_p))
13366 if (sub_strict_overflow_p)
13367 *strict_overflow_p = true;
13372 sub_strict_overflow_p = false;
13373 if (tree_expr_nonzero_warnv_p (op0,
13374 &sub_strict_overflow_p))
13376 if (sub_strict_overflow_p)
13377 *strict_overflow_p = true;
13379 /* When both operands are nonzero, then MAX must be too. */
13380 if (tree_expr_nonzero_warnv_p (op1,
13381 strict_overflow_p))
13384 /* MAX where operand 0 is positive is positive. */
13385 return tree_expr_nonnegative_warnv_p (op0,
13386 strict_overflow_p);
13388 /* MAX where operand 1 is positive is positive. */
13389 else if (tree_expr_nonzero_warnv_p (op1,
13390 &sub_strict_overflow_p)
13391 && tree_expr_nonnegative_warnv_p (op1,
13392 &sub_strict_overflow_p))
13394 if (sub_strict_overflow_p)
13395 *strict_overflow_p = true;
13401 return (tree_expr_nonzero_warnv_p (op1,
13403 || tree_expr_nonzero_warnv_p (op0,
13404 strict_overflow_p));
13413 /* Return true when T is an address and is known to be nonzero.
13414 For floating point we further ensure that T is not denormal.
13415 Similar logic is present in nonzero_address in rtlanal.h.
13417 If the return value is based on the assumption that signed overflow
13418 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13419 change *STRICT_OVERFLOW_P. */
13422 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13424 bool sub_strict_overflow_p;
13425 switch (TREE_CODE (t))
13428 return !integer_zerop (t);
13432 tree base = TREE_OPERAND (t, 0);
13434 if (!DECL_P (base))
13435 base = get_base_address (base);
13440 /* For objects in symbol table check if we know they are non-zero.
13441 Don't do anything for variables and functions before symtab is built;
13442 it is quite possible that they will be declared weak later. */
13443 if (DECL_P (base) && decl_in_symtab_p (base))
13445 struct symtab_node *symbol;
13447 symbol = symtab_node::get_create (base);
13449 return symbol->nonzero_address ();
13454 /* Function local objects are never NULL. */
13456 && (DECL_CONTEXT (base)
13457 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13458 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13461 /* Constants are never weak. */
13462 if (CONSTANT_CLASS_P (base))
13469 sub_strict_overflow_p = false;
13470 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13471 &sub_strict_overflow_p)
13472 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13473 &sub_strict_overflow_p))
13475 if (sub_strict_overflow_p)
13476 *strict_overflow_p = true;
13487 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13488 attempt to fold the expression to a constant without modifying TYPE,
13491 If the expression could be simplified to a constant, then return
13492 the constant. If the expression would not be simplified to a
13493 constant, then return NULL_TREE. */
13496 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13498 tree tem = fold_binary (code, type, op0, op1);
13499 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13502 /* Given the components of a unary expression CODE, TYPE and OP0,
13503 attempt to fold the expression to a constant without modifying
13506 If the expression could be simplified to a constant, then return
13507 the constant. If the expression would not be simplified to a
13508 constant, then return NULL_TREE. */
13511 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13513 tree tem = fold_unary (code, type, op0);
13514 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13517 /* If EXP represents referencing an element in a constant string
13518 (either via pointer arithmetic or array indexing), return the
13519 tree representing the value accessed, otherwise return NULL. */
13522 fold_read_from_constant_string (tree exp)
13524 if ((TREE_CODE (exp) == INDIRECT_REF
13525 || TREE_CODE (exp) == ARRAY_REF)
13526 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13528 tree exp1 = TREE_OPERAND (exp, 0);
13531 location_t loc = EXPR_LOCATION (exp);
13533 if (TREE_CODE (exp) == INDIRECT_REF)
13534 string = string_constant (exp1, &index);
13537 tree low_bound = array_ref_low_bound (exp);
13538 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13540 /* Optimize the special-case of a zero lower bound.
13542 We convert the low_bound to sizetype to avoid some problems
13543 with constant folding. (E.g. suppose the lower bound is 1,
13544 and its mode is QI. Without the conversion,l (ARRAY
13545 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13546 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13547 if (! integer_zerop (low_bound))
13548 index = size_diffop_loc (loc, index,
13549 fold_convert_loc (loc, sizetype, low_bound));
13555 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13556 && TREE_CODE (string) == STRING_CST
13557 && TREE_CODE (index) == INTEGER_CST
13558 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13559 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13561 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13562 return build_int_cst_type (TREE_TYPE (exp),
13563 (TREE_STRING_POINTER (string)
13564 [TREE_INT_CST_LOW (index)]));
13569 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13570 an integer constant, real, or fixed-point constant.
13572 TYPE is the type of the result. */
13575 fold_negate_const (tree arg0, tree type)
13577 tree t = NULL_TREE;
13579 switch (TREE_CODE (arg0))
13584 wide_int val = wi::neg (arg0, &overflow);
13585 t = force_fit_type (type, val, 1,
13586 (overflow | TREE_OVERFLOW (arg0))
13587 && !TYPE_UNSIGNED (type));
13592 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13597 FIXED_VALUE_TYPE f;
13598 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13599 &(TREE_FIXED_CST (arg0)), NULL,
13600 TYPE_SATURATING (type));
13601 t = build_fixed (type, f);
13602 /* Propagate overflow flags. */
13603 if (overflow_p | TREE_OVERFLOW (arg0))
13604 TREE_OVERFLOW (t) = 1;
13609 gcc_unreachable ();
13615 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13616 an integer constant or real constant.
13618 TYPE is the type of the result. */
13621 fold_abs_const (tree arg0, tree type)
13623 tree t = NULL_TREE;
13625 switch (TREE_CODE (arg0))
13629 /* If the value is unsigned or non-negative, then the absolute value
13630 is the same as the ordinary value. */
13631 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13634 /* If the value is negative, then the absolute value is
13639 wide_int val = wi::neg (arg0, &overflow);
13640 t = force_fit_type (type, val, -1,
13641 overflow | TREE_OVERFLOW (arg0));
13647 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13648 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13654 gcc_unreachable ();
13660 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13661 constant. TYPE is the type of the result. */
13664 fold_not_const (const_tree arg0, tree type)
13666 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13668 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13671 /* Given CODE, a relational operator, the target type, TYPE and two
13672 constant operands OP0 and OP1, return the result of the
13673 relational operation. If the result is not a compile time
13674 constant, then return NULL_TREE. */
13677 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13679 int result, invert;
13681 /* From here on, the only cases we handle are when the result is
13682 known to be a constant. */
13684 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13686 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13687 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13689 /* Handle the cases where either operand is a NaN. */
13690 if (real_isnan (c0) || real_isnan (c1))
13700 case UNORDERED_EXPR:
13714 if (flag_trapping_math)
13720 gcc_unreachable ();
13723 return constant_boolean_node (result, type);
13726 return constant_boolean_node (real_compare (code, c0, c1), type);
13729 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13731 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13732 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13733 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13736 /* Handle equality/inequality of complex constants. */
13737 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13739 tree rcond = fold_relational_const (code, type,
13740 TREE_REALPART (op0),
13741 TREE_REALPART (op1));
13742 tree icond = fold_relational_const (code, type,
13743 TREE_IMAGPART (op0),
13744 TREE_IMAGPART (op1));
13745 if (code == EQ_EXPR)
13746 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13747 else if (code == NE_EXPR)
13748 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13753 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13755 unsigned count = VECTOR_CST_NELTS (op0);
13756 tree *elts = XALLOCAVEC (tree, count);
13757 gcc_assert (VECTOR_CST_NELTS (op1) == count
13758 && TYPE_VECTOR_SUBPARTS (type) == count);
13760 for (unsigned i = 0; i < count; i++)
13762 tree elem_type = TREE_TYPE (type);
13763 tree elem0 = VECTOR_CST_ELT (op0, i);
13764 tree elem1 = VECTOR_CST_ELT (op1, i);
13766 tree tem = fold_relational_const (code, elem_type,
13769 if (tem == NULL_TREE)
13772 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13775 return build_vector (type, elts);
13778 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13780 To compute GT, swap the arguments and do LT.
13781 To compute GE, do LT and invert the result.
13782 To compute LE, swap the arguments, do LT and invert the result.
13783 To compute NE, do EQ and invert the result.
13785 Therefore, the code below must handle only EQ and LT. */
13787 if (code == LE_EXPR || code == GT_EXPR)
13789 std::swap (op0, op1);
13790 code = swap_tree_comparison (code);
13793 /* Note that it is safe to invert for real values here because we
13794 have already handled the one case that it matters. */
13797 if (code == NE_EXPR || code == GE_EXPR)
13800 code = invert_tree_comparison (code, false);
13803 /* Compute a result for LT or EQ if args permit;
13804 Otherwise return T. */
13805 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13807 if (code == EQ_EXPR)
13808 result = tree_int_cst_equal (op0, op1);
13810 result = tree_int_cst_lt (op0, op1);
13817 return constant_boolean_node (result, type);
13820 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13821 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13825 fold_build_cleanup_point_expr (tree type, tree expr)
13827 /* If the expression does not have side effects then we don't have to wrap
13828 it with a cleanup point expression. */
13829 if (!TREE_SIDE_EFFECTS (expr))
13832 /* If the expression is a return, check to see if the expression inside the
13833 return has no side effects or the right hand side of the modify expression
13834 inside the return. If either don't have side effects set we don't need to
13835 wrap the expression in a cleanup point expression. Note we don't check the
13836 left hand side of the modify because it should always be a return decl. */
13837 if (TREE_CODE (expr) == RETURN_EXPR)
13839 tree op = TREE_OPERAND (expr, 0);
13840 if (!op || !TREE_SIDE_EFFECTS (op))
13842 op = TREE_OPERAND (op, 1);
13843 if (!TREE_SIDE_EFFECTS (op))
13847 return build1 (CLEANUP_POINT_EXPR, type, expr);
13850 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13851 of an indirection through OP0, or NULL_TREE if no simplification is
13855 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13861 subtype = TREE_TYPE (sub);
13862 if (!POINTER_TYPE_P (subtype))
13865 if (TREE_CODE (sub) == ADDR_EXPR)
13867 tree op = TREE_OPERAND (sub, 0);
13868 tree optype = TREE_TYPE (op);
13869 /* *&CONST_DECL -> to the value of the const decl. */
13870 if (TREE_CODE (op) == CONST_DECL)
13871 return DECL_INITIAL (op);
13872 /* *&p => p; make sure to handle *&"str"[cst] here. */
13873 if (type == optype)
13875 tree fop = fold_read_from_constant_string (op);
13881 /* *(foo *)&fooarray => fooarray[0] */
13882 else if (TREE_CODE (optype) == ARRAY_TYPE
13883 && type == TREE_TYPE (optype)
13884 && (!in_gimple_form
13885 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13887 tree type_domain = TYPE_DOMAIN (optype);
13888 tree min_val = size_zero_node;
13889 if (type_domain && TYPE_MIN_VALUE (type_domain))
13890 min_val = TYPE_MIN_VALUE (type_domain);
13892 && TREE_CODE (min_val) != INTEGER_CST)
13894 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13895 NULL_TREE, NULL_TREE);
13897 /* *(foo *)&complexfoo => __real__ complexfoo */
13898 else if (TREE_CODE (optype) == COMPLEX_TYPE
13899 && type == TREE_TYPE (optype))
13900 return fold_build1_loc (loc, REALPART_EXPR, type, op);
13901 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13902 else if (TREE_CODE (optype) == VECTOR_TYPE
13903 && type == TREE_TYPE (optype))
13905 tree part_width = TYPE_SIZE (type);
13906 tree index = bitsize_int (0);
13907 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
13911 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
13912 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13914 tree op00 = TREE_OPERAND (sub, 0);
13915 tree op01 = TREE_OPERAND (sub, 1);
13918 if (TREE_CODE (op00) == ADDR_EXPR)
13921 op00 = TREE_OPERAND (op00, 0);
13922 op00type = TREE_TYPE (op00);
13924 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
13925 if (TREE_CODE (op00type) == VECTOR_TYPE
13926 && type == TREE_TYPE (op00type))
13928 HOST_WIDE_INT offset = tree_to_shwi (op01);
13929 tree part_width = TYPE_SIZE (type);
13930 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
13931 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
13932 tree index = bitsize_int (indexi);
13934 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
13935 return fold_build3_loc (loc,
13936 BIT_FIELD_REF, type, op00,
13937 part_width, index);
13940 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13941 else if (TREE_CODE (op00type) == COMPLEX_TYPE
13942 && type == TREE_TYPE (op00type))
13944 tree size = TYPE_SIZE_UNIT (type);
13945 if (tree_int_cst_equal (size, op01))
13946 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
13948 /* ((foo *)&fooarray)[1] => fooarray[1] */
13949 else if (TREE_CODE (op00type) == ARRAY_TYPE
13950 && type == TREE_TYPE (op00type))
13952 tree type_domain = TYPE_DOMAIN (op00type);
13953 tree min_val = size_zero_node;
13954 if (type_domain && TYPE_MIN_VALUE (type_domain))
13955 min_val = TYPE_MIN_VALUE (type_domain);
13956 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
13957 TYPE_SIZE_UNIT (type));
13958 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
13959 return build4_loc (loc, ARRAY_REF, type, op00, op01,
13960 NULL_TREE, NULL_TREE);
13965 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
13966 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
13967 && type == TREE_TYPE (TREE_TYPE (subtype))
13968 && (!in_gimple_form
13969 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13972 tree min_val = size_zero_node;
13973 sub = build_fold_indirect_ref_loc (loc, sub);
13974 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
13975 if (type_domain && TYPE_MIN_VALUE (type_domain))
13976 min_val = TYPE_MIN_VALUE (type_domain);
13978 && TREE_CODE (min_val) != INTEGER_CST)
13980 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
13987 /* Builds an expression for an indirection through T, simplifying some
13991 build_fold_indirect_ref_loc (location_t loc, tree t)
13993 tree type = TREE_TYPE (TREE_TYPE (t));
13994 tree sub = fold_indirect_ref_1 (loc, type, t);
13999 return build1_loc (loc, INDIRECT_REF, type, t);
14002 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14005 fold_indirect_ref_loc (location_t loc, tree t)
14007 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14015 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14016 whose result is ignored. The type of the returned tree need not be
14017 the same as the original expression. */
14020 fold_ignored_result (tree t)
14022 if (!TREE_SIDE_EFFECTS (t))
14023 return integer_zero_node;
14026 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14029 t = TREE_OPERAND (t, 0);
14033 case tcc_comparison:
14034 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14035 t = TREE_OPERAND (t, 0);
14036 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14037 t = TREE_OPERAND (t, 1);
14042 case tcc_expression:
14043 switch (TREE_CODE (t))
14045 case COMPOUND_EXPR:
14046 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14048 t = TREE_OPERAND (t, 0);
14052 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14053 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14055 t = TREE_OPERAND (t, 0);
14068 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14071 round_up_loc (location_t loc, tree value, unsigned int divisor)
14073 tree div = NULL_TREE;
14078 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14079 have to do anything. Only do this when we are not given a const,
14080 because in that case, this check is more expensive than just
14082 if (TREE_CODE (value) != INTEGER_CST)
14084 div = build_int_cst (TREE_TYPE (value), divisor);
14086 if (multiple_of_p (TREE_TYPE (value), value, div))
14090 /* If divisor is a power of two, simplify this to bit manipulation. */
14091 if (divisor == (divisor & -divisor))
14093 if (TREE_CODE (value) == INTEGER_CST)
14095 wide_int val = value;
14098 if ((val & (divisor - 1)) == 0)
14101 overflow_p = TREE_OVERFLOW (value);
14102 val += divisor - 1;
14103 val &= - (int) divisor;
14107 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14113 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14114 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14115 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14116 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14122 div = build_int_cst (TREE_TYPE (value), divisor);
14123 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14124 value = size_binop_loc (loc, MULT_EXPR, value, div);
14130 /* Likewise, but round down. */
14133 round_down_loc (location_t loc, tree value, int divisor)
14135 tree div = NULL_TREE;
14137 gcc_assert (divisor > 0);
14141 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14142 have to do anything. Only do this when we are not given a const,
14143 because in that case, this check is more expensive than just
14145 if (TREE_CODE (value) != INTEGER_CST)
14147 div = build_int_cst (TREE_TYPE (value), divisor);
14149 if (multiple_of_p (TREE_TYPE (value), value, div))
14153 /* If divisor is a power of two, simplify this to bit manipulation. */
14154 if (divisor == (divisor & -divisor))
14158 t = build_int_cst (TREE_TYPE (value), -divisor);
14159 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14164 div = build_int_cst (TREE_TYPE (value), divisor);
14165 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14166 value = size_binop_loc (loc, MULT_EXPR, value, div);
14172 /* Returns the pointer to the base of the object addressed by EXP and
14173 extracts the information about the offset of the access, storing it
14174 to PBITPOS and POFFSET. */
14177 split_address_to_core_and_offset (tree exp,
14178 HOST_WIDE_INT *pbitpos, tree *poffset)
14182 int unsignedp, volatilep;
14183 HOST_WIDE_INT bitsize;
14184 location_t loc = EXPR_LOCATION (exp);
14186 if (TREE_CODE (exp) == ADDR_EXPR)
14188 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14189 poffset, &mode, &unsignedp, &volatilep,
14191 core = build_fold_addr_expr_loc (loc, core);
14197 *poffset = NULL_TREE;
14203 /* Returns true if addresses of E1 and E2 differ by a constant, false
14204 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14207 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14210 HOST_WIDE_INT bitpos1, bitpos2;
14211 tree toffset1, toffset2, tdiff, type;
14213 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14214 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14216 if (bitpos1 % BITS_PER_UNIT != 0
14217 || bitpos2 % BITS_PER_UNIT != 0
14218 || !operand_equal_p (core1, core2, 0))
14221 if (toffset1 && toffset2)
14223 type = TREE_TYPE (toffset1);
14224 if (type != TREE_TYPE (toffset2))
14225 toffset2 = fold_convert (type, toffset2);
14227 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14228 if (!cst_and_fits_in_hwi (tdiff))
14231 *diff = int_cst_value (tdiff);
14233 else if (toffset1 || toffset2)
14235 /* If only one of the offsets is non-constant, the difference cannot
14242 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14246 /* Simplify the floating point expression EXP when the sign of the
14247 result is not significant. Return NULL_TREE if no simplification
14251 fold_strip_sign_ops (tree exp)
14254 location_t loc = EXPR_LOCATION (exp);
14256 switch (TREE_CODE (exp))
14260 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14261 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
14265 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
14267 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14268 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14269 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
14270 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
14271 arg0 ? arg0 : TREE_OPERAND (exp, 0),
14272 arg1 ? arg1 : TREE_OPERAND (exp, 1));
14275 case COMPOUND_EXPR:
14276 arg0 = TREE_OPERAND (exp, 0);
14277 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14279 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
14283 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14284 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
14286 return fold_build3_loc (loc,
14287 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
14288 arg0 ? arg0 : TREE_OPERAND (exp, 1),
14289 arg1 ? arg1 : TREE_OPERAND (exp, 2));
14294 const enum built_in_function fcode = builtin_mathfn_code (exp);
14297 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14298 /* Strip copysign function call, return the 1st argument. */
14299 arg0 = CALL_EXPR_ARG (exp, 0);
14300 arg1 = CALL_EXPR_ARG (exp, 1);
14301 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
14304 /* Strip sign ops from the argument of "odd" math functions. */
14305 if (negate_mathfn_p (fcode))
14307 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
14309 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
14322 /* Return OFF converted to a pointer offset type suitable as offset for
14323 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14325 convert_to_ptrofftype_loc (location_t loc, tree off)
14327 return fold_convert_loc (loc, sizetype, off);
14330 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14332 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14334 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14335 ptr, convert_to_ptrofftype_loc (loc, off));
14338 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14340 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14342 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14343 ptr, size_int (off));