1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2023 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. */
43 #define INCLUDE_ALGORITHM
46 #include "coretypes.h"
55 #include "tree-ssa-operands.h"
56 #include "optabs-query.h"
58 #include "diagnostic-core.h"
61 #include "fold-const.h"
62 #include "fold-const-call.h"
63 #include "stor-layout.h"
65 #include "tree-iterator.h"
68 #include "langhooks.h"
73 #include "generic-match.h"
74 #include "gimple-iterator.h"
75 #include "gimple-fold.h"
76 #include "tree-into-ssa.h"
78 #include "case-cfn-macros.h"
79 #include "stringpool.h"
81 #include "tree-ssanames.h"
83 #include "stringpool.h"
85 #include "tree-vector-builder.h"
86 #include "vec-perm-indices.h"
88 #include "gimple-range.h"
90 /* Nonzero if we are folding constants inside an initializer or a C++
91 manifestly-constant-evaluated context; zero otherwise.
92 Should be used when folding in initializer enables additional
94 int folding_initializer
= 0;
96 /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
98 Should be used when certain constructs shouldn't be optimized
99 during folding in that context. */
100 bool folding_cxx_constexpr
= false;
102 /* The following constants represent a bit based encoding of GCC's
103 comparison operators. This encoding simplifies transformations
104 on relational comparison operators, such as AND and OR. */
105 enum comparison_code
{
124 static bool negate_expr_p (tree
);
125 static tree
negate_expr (tree
);
126 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
127 static enum comparison_code
comparison_to_compcode (enum tree_code
);
128 static enum tree_code
compcode_to_comparison (enum comparison_code
);
129 static bool twoval_comparison_p (tree
, tree
*, tree
*);
130 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
131 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
133 static bool simple_operand_p (const_tree
);
134 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
135 static tree
range_predecessor (tree
);
136 static tree
range_successor (tree
);
137 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
138 static tree
fold_cond_expr_with_comparison (location_t
, tree
, enum tree_code
,
139 tree
, tree
, tree
, tree
);
140 static tree
unextend (tree
, int, int, tree
);
141 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
142 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
143 static tree
fold_binary_op_with_conditional_arg (location_t
,
144 enum tree_code
, 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 tree
fold_negate_expr (location_t
, tree
);
154 /* This is a helper function to detect min/max for some operands of COND_EXPR.
155 The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
157 minmax_from_comparison (tree_code cmp
, tree exp0
, tree exp1
, tree exp2
, tree exp3
)
159 enum tree_code code
= ERROR_MARK
;
161 if (HONOR_NANS (exp0
) || HONOR_SIGNED_ZEROS (exp0
))
164 if (!operand_equal_p (exp0
, exp2
))
167 if (TREE_CODE (exp3
) == INTEGER_CST
&& TREE_CODE (exp1
) == INTEGER_CST
)
169 if (wi::to_widest (exp1
) == (wi::to_widest (exp3
) - 1))
171 /* X <= Y - 1 equals to X < Y. */
174 /* X > Y - 1 equals to X >= Y. */
177 /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
178 if (cmp
== NE_EXPR
&& TREE_CODE (exp0
) == SSA_NAME
)
181 get_range_query (cfun
)->range_of_expr (r
, exp0
);
182 if (r
.undefined_p ())
183 r
.set_varying (TREE_TYPE (exp0
));
185 widest_int min
= widest_int::from (r
.lower_bound (),
186 TYPE_SIGN (TREE_TYPE (exp0
)));
187 if (min
== wi::to_widest (exp1
))
191 if (wi::to_widest (exp1
) == (wi::to_widest (exp3
) + 1))
193 /* X < Y + 1 equals to X <= Y. */
196 /* X >= Y + 1 equals to X > Y. */
199 /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
200 if (cmp
== NE_EXPR
&& TREE_CODE (exp0
) == SSA_NAME
)
203 get_range_query (cfun
)->range_of_expr (r
, exp0
);
204 if (r
.undefined_p ())
205 r
.set_varying (TREE_TYPE (exp0
));
207 widest_int max
= widest_int::from (r
.upper_bound (),
208 TYPE_SIGN (TREE_TYPE (exp0
)));
209 if (max
== wi::to_widest (exp1
))
214 if (code
!= ERROR_MARK
215 || operand_equal_p (exp1
, exp3
))
217 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
219 if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
225 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
226 Otherwise, return LOC. */
229 expr_location_or (tree t
, location_t loc
)
231 location_t tloc
= EXPR_LOCATION (t
);
232 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
235 /* Similar to protected_set_expr_location, but never modify x in place,
236 if location can and needs to be set, unshare it. */
239 protected_set_expr_location_unshare (tree x
, location_t loc
)
241 if (CAN_HAVE_LOCATION_P (x
)
242 && EXPR_LOCATION (x
) != loc
243 && !(TREE_CODE (x
) == SAVE_EXPR
244 || TREE_CODE (x
) == TARGET_EXPR
245 || TREE_CODE (x
) == BIND_EXPR
))
248 SET_EXPR_LOCATION (x
, loc
);
253 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
254 division and returns the quotient. Otherwise returns
258 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
262 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
264 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
269 /* This is nonzero if we should defer warnings about undefined
270 overflow. This facility exists because these warnings are a
271 special case. The code to estimate loop iterations does not want
272 to issue any warnings, since it works with expressions which do not
273 occur in user code. Various bits of cleanup code call fold(), but
274 only use the result if it has certain characteristics (e.g., is a
275 constant); that code only wants to issue a warning if the result is
278 static int fold_deferring_overflow_warnings
;
280 /* If a warning about undefined overflow is deferred, this is the
281 warning. Note that this may cause us to turn two warnings into
282 one, but that is fine since it is sufficient to only give one
283 warning per expression. */
285 static const char* fold_deferred_overflow_warning
;
287 /* If a warning about undefined overflow is deferred, this is the
288 level at which the warning should be emitted. */
290 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
292 /* Start deferring overflow warnings. We could use a stack here to
293 permit nested calls, but at present it is not necessary. */
296 fold_defer_overflow_warnings (void)
298 ++fold_deferring_overflow_warnings
;
301 /* Stop deferring overflow warnings. If there is a pending warning,
302 and ISSUE is true, then issue the warning if appropriate. STMT is
303 the statement with which the warning should be associated (used for
304 location information); STMT may be NULL. CODE is the level of the
305 warning--a warn_strict_overflow_code value. This function will use
306 the smaller of CODE and the deferred code when deciding whether to
307 issue the warning. CODE may be zero to mean to always use the
311 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
316 gcc_assert (fold_deferring_overflow_warnings
> 0);
317 --fold_deferring_overflow_warnings
;
318 if (fold_deferring_overflow_warnings
> 0)
320 if (fold_deferred_overflow_warning
!= NULL
322 && code
< (int) fold_deferred_overflow_code
)
323 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
327 warnmsg
= fold_deferred_overflow_warning
;
328 fold_deferred_overflow_warning
= NULL
;
330 if (!issue
|| warnmsg
== NULL
)
333 if (warning_suppressed_p (stmt
, OPT_Wstrict_overflow
))
336 /* Use the smallest code level when deciding to issue the
338 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
339 code
= fold_deferred_overflow_code
;
341 if (!issue_strict_overflow_warning (code
))
345 locus
= input_location
;
347 locus
= gimple_location (stmt
);
348 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
351 /* Stop deferring overflow warnings, ignoring any deferred
355 fold_undefer_and_ignore_overflow_warnings (void)
357 fold_undefer_overflow_warnings (false, NULL
, 0);
360 /* Whether we are deferring overflow warnings. */
363 fold_deferring_overflow_warnings_p (void)
365 return fold_deferring_overflow_warnings
> 0;
368 /* This is called when we fold something based on the fact that signed
369 overflow is undefined. */
372 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
374 if (fold_deferring_overflow_warnings
> 0)
376 if (fold_deferred_overflow_warning
== NULL
377 || wc
< fold_deferred_overflow_code
)
379 fold_deferred_overflow_warning
= gmsgid
;
380 fold_deferred_overflow_code
= wc
;
383 else if (issue_strict_overflow_warning (wc
))
384 warning (OPT_Wstrict_overflow
, gmsgid
);
387 /* Return true if the built-in mathematical function specified by CODE
388 is odd, i.e. -f(x) == f(-x). */
391 negate_mathfn_p (combined_fn fn
)
431 CASE_CFN_ROUNDEVEN_FN
:
449 CASE_CFN_NEARBYINT_FN
:
452 return !flag_rounding_math
;
460 /* Check whether we may negate an integer constant T without causing
464 may_negate_without_overflow_p (const_tree t
)
468 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
470 type
= TREE_TYPE (t
);
471 if (TYPE_UNSIGNED (type
))
474 return !wi::only_sign_bit_p (wi::to_wide (t
));
477 /* Determine whether an expression T can be cheaply negated using
478 the function negate_expr without introducing undefined overflow. */
481 negate_expr_p (tree t
)
488 type
= TREE_TYPE (t
);
491 switch (TREE_CODE (t
))
494 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
497 /* Check that -CST will not overflow type. */
498 return may_negate_without_overflow_p (t
);
500 return (INTEGRAL_TYPE_P (type
)
501 && TYPE_OVERFLOW_WRAPS (type
));
507 return !TYPE_OVERFLOW_SANITIZED (type
);
510 /* We want to canonicalize to positive real constants. Pretend
511 that only negative ones can be easily negated. */
512 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
515 return negate_expr_p (TREE_REALPART (t
))
516 && negate_expr_p (TREE_IMAGPART (t
));
520 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
523 /* Steps don't prevent negation. */
524 unsigned int count
= vector_cst_encoded_nelts (t
);
525 for (unsigned int i
= 0; i
< count
; ++i
)
526 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
533 return negate_expr_p (TREE_OPERAND (t
, 0))
534 && negate_expr_p (TREE_OPERAND (t
, 1));
537 return negate_expr_p (TREE_OPERAND (t
, 0));
540 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
)
541 || HONOR_SIGNED_ZEROS (type
)
542 || (ANY_INTEGRAL_TYPE_P (type
)
543 && ! TYPE_OVERFLOW_WRAPS (type
)))
545 /* -(A + B) -> (-B) - A. */
546 if (negate_expr_p (TREE_OPERAND (t
, 1)))
548 /* -(A + B) -> (-A) - B. */
549 return negate_expr_p (TREE_OPERAND (t
, 0));
552 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
553 return !HONOR_SIGN_DEPENDENT_ROUNDING (type
)
554 && !HONOR_SIGNED_ZEROS (type
)
555 && (! ANY_INTEGRAL_TYPE_P (type
)
556 || TYPE_OVERFLOW_WRAPS (type
));
559 if (TYPE_UNSIGNED (type
))
561 /* INT_MIN/n * n doesn't overflow while negating one operand it does
562 if n is a (negative) power of two. */
563 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
564 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
565 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
567 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
568 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
570 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
576 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t
))
577 return negate_expr_p (TREE_OPERAND (t
, 1))
578 || negate_expr_p (TREE_OPERAND (t
, 0));
584 if (TYPE_UNSIGNED (type
))
586 /* In general we can't negate A in A / B, because if A is INT_MIN and
587 B is not 1 we change the sign of the result. */
588 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
589 && negate_expr_p (TREE_OPERAND (t
, 0)))
591 /* In general we can't negate B in A / B, because if A is INT_MIN and
592 B is 1, we may turn this into INT_MIN / -1 which is undefined
593 and actually traps on some architectures. */
594 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
595 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
596 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
597 && ! integer_onep (TREE_OPERAND (t
, 1))))
598 return negate_expr_p (TREE_OPERAND (t
, 1));
602 /* Negate -((double)float) as (double)(-float). */
603 if (SCALAR_FLOAT_TYPE_P (type
))
605 tree tem
= strip_float_extensions (t
);
607 return negate_expr_p (tem
);
612 /* Negate -f(x) as f(-x). */
613 if (negate_mathfn_p (get_call_combined_fn (t
)))
614 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
618 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
619 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
621 tree op1
= TREE_OPERAND (t
, 1);
622 if (wi::to_wide (op1
) == element_precision (type
) - 1)
633 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
634 simplification is possible.
635 If negate_expr_p would return true for T, NULL_TREE will never be
639 fold_negate_expr_1 (location_t loc
, tree t
)
641 tree type
= TREE_TYPE (t
);
644 switch (TREE_CODE (t
))
646 /* Convert - (~A) to A + 1. */
648 if (INTEGRAL_TYPE_P (type
))
649 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
650 build_one_cst (type
));
654 tem
= fold_negate_const (t
, type
);
655 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
656 || (ANY_INTEGRAL_TYPE_P (type
)
657 && !TYPE_OVERFLOW_TRAPS (type
)
658 && TYPE_OVERFLOW_WRAPS (type
))
659 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
666 tem
= fold_negate_const (t
, type
);
671 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
672 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
674 return build_complex (type
, rpart
, ipart
);
680 tree_vector_builder elts
;
681 elts
.new_unary_operation (type
, t
, true);
682 unsigned int count
= elts
.encoded_nelts ();
683 for (unsigned int i
= 0; i
< count
; ++i
)
685 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
686 if (elt
== NULL_TREE
)
688 elts
.quick_push (elt
);
691 return elts
.build ();
695 if (negate_expr_p (t
))
696 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
697 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
698 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
702 if (negate_expr_p (t
))
703 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
704 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
708 if (!TYPE_OVERFLOW_SANITIZED (type
))
709 return TREE_OPERAND (t
, 0);
713 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
714 && !HONOR_SIGNED_ZEROS (type
))
716 /* -(A + B) -> (-B) - A. */
717 if (negate_expr_p (TREE_OPERAND (t
, 1)))
719 tem
= negate_expr (TREE_OPERAND (t
, 1));
720 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
721 tem
, TREE_OPERAND (t
, 0));
724 /* -(A + B) -> (-A) - B. */
725 if (negate_expr_p (TREE_OPERAND (t
, 0)))
727 tem
= negate_expr (TREE_OPERAND (t
, 0));
728 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
729 tem
, TREE_OPERAND (t
, 1));
735 /* - (A - B) -> B - A */
736 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
737 && !HONOR_SIGNED_ZEROS (type
))
738 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
739 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
743 if (TYPE_UNSIGNED (type
))
749 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
751 tem
= TREE_OPERAND (t
, 1);
752 if (negate_expr_p (tem
))
753 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
754 TREE_OPERAND (t
, 0), negate_expr (tem
));
755 tem
= TREE_OPERAND (t
, 0);
756 if (negate_expr_p (tem
))
757 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
758 negate_expr (tem
), TREE_OPERAND (t
, 1));
765 if (TYPE_UNSIGNED (type
))
767 /* In general we can't negate A in A / B, because if A is INT_MIN and
768 B is not 1 we change the sign of the result. */
769 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
770 && negate_expr_p (TREE_OPERAND (t
, 0)))
771 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
772 negate_expr (TREE_OPERAND (t
, 0)),
773 TREE_OPERAND (t
, 1));
774 /* In general we can't negate B in A / B, because if A is INT_MIN and
775 B is 1, we may turn this into INT_MIN / -1 which is undefined
776 and actually traps on some architectures. */
777 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
778 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
779 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
780 && ! integer_onep (TREE_OPERAND (t
, 1))))
781 && negate_expr_p (TREE_OPERAND (t
, 1)))
782 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
784 negate_expr (TREE_OPERAND (t
, 1)));
788 /* Convert -((double)float) into (double)(-float). */
789 if (SCALAR_FLOAT_TYPE_P (type
))
791 tem
= strip_float_extensions (t
);
792 if (tem
!= t
&& negate_expr_p (tem
))
793 return fold_convert_loc (loc
, type
, negate_expr (tem
));
798 /* Negate -f(x) as f(-x). */
799 if (negate_mathfn_p (get_call_combined_fn (t
))
800 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
804 fndecl
= get_callee_fndecl (t
);
805 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
806 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
811 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
812 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
814 tree op1
= TREE_OPERAND (t
, 1);
815 if (wi::to_wide (op1
) == element_precision (type
) - 1)
817 tree ntype
= TYPE_UNSIGNED (type
)
818 ? signed_type_for (type
)
819 : unsigned_type_for (type
);
820 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
821 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
822 return fold_convert_loc (loc
, type
, temp
);
834 /* A wrapper for fold_negate_expr_1. */
837 fold_negate_expr (location_t loc
, tree t
)
839 tree type
= TREE_TYPE (t
);
841 tree tem
= fold_negate_expr_1 (loc
, t
);
842 if (tem
== NULL_TREE
)
844 return fold_convert_loc (loc
, type
, tem
);
847 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
848 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
860 loc
= EXPR_LOCATION (t
);
861 type
= TREE_TYPE (t
);
864 tem
= fold_negate_expr (loc
, t
);
866 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
867 return fold_convert_loc (loc
, type
, tem
);
870 /* Split a tree IN into a constant, literal and variable parts that could be
871 combined with CODE to make IN. "constant" means an expression with
872 TREE_CONSTANT but that isn't an actual constant. CODE must be a
873 commutative arithmetic operation. Store the constant part into *CONP,
874 the literal in *LITP and return the variable part. If a part isn't
875 present, set it to null. If the tree does not decompose in this way,
876 return the entire tree as the variable part and the other parts as null.
878 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
879 case, we negate an operand that was subtracted. Except if it is a
880 literal for which we use *MINUS_LITP instead.
882 If NEGATE_P is true, we are negating all of IN, again except a literal
883 for which we use *MINUS_LITP instead. If a variable part is of pointer
884 type, it is negated after converting to TYPE. This prevents us from
885 generating illegal MINUS pointer expression. LOC is the location of
886 the converted variable part.
888 If IN is itself a literal or constant, return it as appropriate.
890 Note that we do not guarantee that any of the three values will be the
891 same type as IN, but they will have the same signedness and mode. */
894 split_tree (tree in
, tree type
, enum tree_code code
,
895 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
896 tree
*litp
, tree
*minus_litp
, int negate_p
)
905 /* Strip any conversions that don't change the machine mode or signedness. */
906 STRIP_SIGN_NOPS (in
);
908 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
909 || TREE_CODE (in
) == FIXED_CST
)
911 else if (TREE_CODE (in
) == code
912 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
913 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
914 /* We can associate addition and subtraction together (even
915 though the C standard doesn't say so) for integers because
916 the value is not affected. For reals, the value might be
917 affected, so we can't. */
918 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
919 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
920 || (code
== MINUS_EXPR
921 && (TREE_CODE (in
) == PLUS_EXPR
922 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
924 tree op0
= TREE_OPERAND (in
, 0);
925 tree op1
= TREE_OPERAND (in
, 1);
926 bool neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
927 bool neg_litp_p
= false, neg_conp_p
= false, neg_var_p
= false;
929 /* First see if either of the operands is a literal, then a constant. */
930 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
931 || TREE_CODE (op0
) == FIXED_CST
)
932 *litp
= op0
, op0
= 0;
933 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
934 || TREE_CODE (op1
) == FIXED_CST
)
935 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
937 if (op0
!= 0 && TREE_CONSTANT (op0
))
938 *conp
= op0
, op0
= 0;
939 else if (op1
!= 0 && TREE_CONSTANT (op1
))
940 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
942 /* If we haven't dealt with either operand, this is not a case we can
943 decompose. Otherwise, VAR is either of the ones remaining, if any. */
944 if (op0
!= 0 && op1
!= 0)
949 var
= op1
, neg_var_p
= neg1_p
;
951 /* Now do any needed negations. */
953 *minus_litp
= *litp
, *litp
= 0;
954 if (neg_conp_p
&& *conp
)
955 *minus_conp
= *conp
, *conp
= 0;
956 if (neg_var_p
&& var
)
957 *minus_varp
= var
, var
= 0;
959 else if (TREE_CONSTANT (in
))
961 else if (TREE_CODE (in
) == BIT_NOT_EXPR
962 && code
== PLUS_EXPR
)
964 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
965 when IN is constant. */
966 *litp
= build_minus_one_cst (type
);
967 *minus_varp
= TREE_OPERAND (in
, 0);
975 *minus_litp
= *litp
, *litp
= 0;
976 else if (*minus_litp
)
977 *litp
= *minus_litp
, *minus_litp
= 0;
979 *minus_conp
= *conp
, *conp
= 0;
980 else if (*minus_conp
)
981 *conp
= *minus_conp
, *minus_conp
= 0;
983 *minus_varp
= var
, var
= 0;
984 else if (*minus_varp
)
985 var
= *minus_varp
, *minus_varp
= 0;
989 && TREE_OVERFLOW_P (*litp
))
990 *litp
= drop_tree_overflow (*litp
);
992 && TREE_OVERFLOW_P (*minus_litp
))
993 *minus_litp
= drop_tree_overflow (*minus_litp
);
998 /* Re-associate trees split by the above function. T1 and T2 are
999 either expressions to associate or null. Return the new
1000 expression, if any. LOC is the location of the new expression. If
1001 we build an operation, do it in TYPE and with CODE. */
1004 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
1008 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
1014 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1015 try to fold this since we will have infinite recursion. But do
1016 deal with any NEGATE_EXPRs. */
1017 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
1018 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
1019 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
1021 if (code
== PLUS_EXPR
)
1023 if (TREE_CODE (t1
) == NEGATE_EXPR
)
1024 return build2_loc (loc
, MINUS_EXPR
, type
,
1025 fold_convert_loc (loc
, type
, t2
),
1026 fold_convert_loc (loc
, type
,
1027 TREE_OPERAND (t1
, 0)));
1028 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
1029 return build2_loc (loc
, MINUS_EXPR
, type
,
1030 fold_convert_loc (loc
, type
, t1
),
1031 fold_convert_loc (loc
, type
,
1032 TREE_OPERAND (t2
, 0)));
1033 else if (integer_zerop (t2
))
1034 return fold_convert_loc (loc
, type
, t1
);
1036 else if (code
== MINUS_EXPR
)
1038 if (integer_zerop (t2
))
1039 return fold_convert_loc (loc
, type
, t1
);
1042 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
1043 fold_convert_loc (loc
, type
, t2
));
1046 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
1047 fold_convert_loc (loc
, type
, t2
));
1050 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1051 for use in int_const_binop, size_binop and size_diffop. */
1054 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
1056 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
1058 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
1073 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
1074 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
1075 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
1078 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1079 a new constant in RES. Return FALSE if we don't know how to
1080 evaluate CODE at compile-time. */
1083 wide_int_binop (wide_int
&res
,
1084 enum tree_code code
, const wide_int
&arg1
, const wide_int
&arg2
,
1085 signop sign
, wi::overflow_type
*overflow
)
1088 *overflow
= wi::OVF_NONE
;
1092 res
= wi::bit_or (arg1
, arg2
);
1096 res
= wi::bit_xor (arg1
, arg2
);
1100 res
= wi::bit_and (arg1
, arg2
);
1104 if (wi::neg_p (arg2
))
1106 res
= wi::lshift (arg1
, arg2
);
1110 if (wi::neg_p (arg2
))
1112 /* It's unclear from the C standard whether shifts can overflow.
1113 The following code ignores overflow; perhaps a C standard
1114 interpretation ruling is needed. */
1115 res
= wi::rshift (arg1
, arg2
, sign
);
1120 if (wi::neg_p (arg2
))
1123 if (code
== RROTATE_EXPR
)
1124 code
= LROTATE_EXPR
;
1126 code
= RROTATE_EXPR
;
1131 if (code
== RROTATE_EXPR
)
1132 res
= wi::rrotate (arg1
, tmp
);
1134 res
= wi::lrotate (arg1
, tmp
);
1138 res
= wi::add (arg1
, arg2
, sign
, overflow
);
1142 res
= wi::sub (arg1
, arg2
, sign
, overflow
);
1146 res
= wi::mul (arg1
, arg2
, sign
, overflow
);
1149 case MULT_HIGHPART_EXPR
:
1150 res
= wi::mul_high (arg1
, arg2
, sign
);
1153 case TRUNC_DIV_EXPR
:
1154 case EXACT_DIV_EXPR
:
1157 res
= wi::div_trunc (arg1
, arg2
, sign
, overflow
);
1160 case FLOOR_DIV_EXPR
:
1163 res
= wi::div_floor (arg1
, arg2
, sign
, overflow
);
1169 res
= wi::div_ceil (arg1
, arg2
, sign
, overflow
);
1172 case ROUND_DIV_EXPR
:
1175 res
= wi::div_round (arg1
, arg2
, sign
, overflow
);
1178 case TRUNC_MOD_EXPR
:
1181 res
= wi::mod_trunc (arg1
, arg2
, sign
, overflow
);
1184 case FLOOR_MOD_EXPR
:
1187 res
= wi::mod_floor (arg1
, arg2
, sign
, overflow
);
1193 res
= wi::mod_ceil (arg1
, arg2
, sign
, overflow
);
1196 case ROUND_MOD_EXPR
:
1199 res
= wi::mod_round (arg1
, arg2
, sign
, overflow
);
1203 res
= wi::min (arg1
, arg2
, sign
);
1207 res
= wi::max (arg1
, arg2
, sign
);
1216 /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1217 min value to RES. */
1219 can_min_p (const_tree arg1
, const_tree arg2
, poly_wide_int
&res
)
1221 if (known_le (wi::to_poly_widest (arg1
), wi::to_poly_widest (arg2
)))
1223 res
= wi::to_poly_wide (arg1
);
1226 else if (known_le (wi::to_poly_widest (arg2
), wi::to_poly_widest (arg1
)))
1228 res
= wi::to_poly_wide (arg2
);
1235 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1236 produce a new constant in RES. Return FALSE if we don't know how
1237 to evaluate CODE at compile-time. */
1240 poly_int_binop (poly_wide_int
&res
, enum tree_code code
,
1241 const_tree arg1
, const_tree arg2
,
1242 signop sign
, wi::overflow_type
*overflow
)
1244 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1245 gcc_assert (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
));
1249 res
= wi::add (wi::to_poly_wide (arg1
),
1250 wi::to_poly_wide (arg2
), sign
, overflow
);
1254 res
= wi::sub (wi::to_poly_wide (arg1
),
1255 wi::to_poly_wide (arg2
), sign
, overflow
);
1259 if (TREE_CODE (arg2
) == INTEGER_CST
)
1260 res
= wi::mul (wi::to_poly_wide (arg1
),
1261 wi::to_wide (arg2
), sign
, overflow
);
1262 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1263 res
= wi::mul (wi::to_poly_wide (arg2
),
1264 wi::to_wide (arg1
), sign
, overflow
);
1270 if (TREE_CODE (arg2
) == INTEGER_CST
)
1271 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1277 if (TREE_CODE (arg2
) != INTEGER_CST
1278 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1284 if (!can_min_p (arg1
, arg2
, res
))
1294 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1295 produce a new constant. Return NULL_TREE if we don't know how to
1296 evaluate CODE at compile-time. */
1299 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1302 poly_wide_int poly_res
;
1303 tree type
= TREE_TYPE (arg1
);
1304 signop sign
= TYPE_SIGN (type
);
1305 wi::overflow_type overflow
= wi::OVF_NONE
;
1307 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1309 wide_int warg1
= wi::to_wide (arg1
), res
;
1310 wide_int warg2
= wi::to_wide (arg2
, TYPE_PRECISION (type
));
1311 if (!wide_int_binop (res
, code
, warg1
, warg2
, sign
, &overflow
))
1315 else if (!poly_int_tree_p (arg1
)
1316 || !poly_int_tree_p (arg2
)
1317 || !poly_int_binop (poly_res
, code
, arg1
, arg2
, sign
, &overflow
))
1319 return force_fit_type (type
, poly_res
, overflowable
,
1320 (((sign
== SIGNED
|| overflowable
== -1)
1322 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1325 /* Return true if binary operation OP distributes over addition in operand
1326 OPNO, with the other operand being held constant. OPNO counts from 1. */
1329 distributes_over_addition_p (tree_code op
, int opno
)
1346 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1347 constant. We assume ARG1 and ARG2 have the same data type, or at least
1348 are the same kind of constant and the same machine mode. Return zero if
1349 combining the constants is not allowed in the current operating mode. */
1352 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1354 /* Sanity check for the recursive cases. */
1361 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1363 if (code
== POINTER_PLUS_EXPR
)
1364 return int_const_binop (PLUS_EXPR
,
1365 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1367 return int_const_binop (code
, arg1
, arg2
);
1370 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1375 REAL_VALUE_TYPE value
;
1376 REAL_VALUE_TYPE result
;
1380 /* The following codes are handled by real_arithmetic. */
1395 d1
= TREE_REAL_CST (arg1
);
1396 d2
= TREE_REAL_CST (arg2
);
1398 type
= TREE_TYPE (arg1
);
1399 mode
= TYPE_MODE (type
);
1401 /* Don't perform operation if we honor signaling NaNs and
1402 either operand is a signaling NaN. */
1403 if (HONOR_SNANS (mode
)
1404 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1405 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1408 /* Don't perform operation if it would raise a division
1409 by zero exception. */
1410 if (code
== RDIV_EXPR
1411 && real_equal (&d2
, &dconst0
)
1412 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1415 /* If either operand is a NaN, just return it. Otherwise, set up
1416 for floating-point trap; we return an overflow. */
1417 if (REAL_VALUE_ISNAN (d1
))
1419 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1422 t
= build_real (type
, d1
);
1425 else if (REAL_VALUE_ISNAN (d2
))
1427 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1430 t
= build_real (type
, d2
);
1434 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1435 real_convert (&result
, mode
, &value
);
1437 /* Don't constant fold this floating point operation if
1438 both operands are not NaN but the result is NaN, and
1439 flag_trapping_math. Such operations should raise an
1440 invalid operation exception. */
1441 if (flag_trapping_math
1442 && MODE_HAS_NANS (mode
)
1443 && REAL_VALUE_ISNAN (result
)
1444 && !REAL_VALUE_ISNAN (d1
)
1445 && !REAL_VALUE_ISNAN (d2
))
1448 /* Don't constant fold this floating point operation if
1449 the result has overflowed and flag_trapping_math. */
1450 if (flag_trapping_math
1451 && MODE_HAS_INFINITIES (mode
)
1452 && REAL_VALUE_ISINF (result
)
1453 && !REAL_VALUE_ISINF (d1
)
1454 && !REAL_VALUE_ISINF (d2
))
1457 /* Don't constant fold this floating point operation if the
1458 result may dependent upon the run-time rounding mode and
1459 flag_rounding_math is set, or if GCC's software emulation
1460 is unable to accurately represent the result. */
1461 if ((flag_rounding_math
1462 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1463 && (inexact
|| !real_identical (&result
, &value
)))
1466 t
= build_real (type
, result
);
1468 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1472 if (TREE_CODE (arg1
) == FIXED_CST
)
1474 FIXED_VALUE_TYPE f1
;
1475 FIXED_VALUE_TYPE f2
;
1476 FIXED_VALUE_TYPE result
;
1481 /* The following codes are handled by fixed_arithmetic. */
1487 case TRUNC_DIV_EXPR
:
1488 if (TREE_CODE (arg2
) != FIXED_CST
)
1490 f2
= TREE_FIXED_CST (arg2
);
1496 if (TREE_CODE (arg2
) != INTEGER_CST
)
1498 wi::tree_to_wide_ref w2
= wi::to_wide (arg2
);
1499 f2
.data
.high
= w2
.elt (1);
1500 f2
.data
.low
= w2
.ulow ();
1509 f1
= TREE_FIXED_CST (arg1
);
1510 type
= TREE_TYPE (arg1
);
1511 sat_p
= TYPE_SATURATING (type
);
1512 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1513 t
= build_fixed (type
, result
);
1514 /* Propagate overflow flags. */
1515 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1516 TREE_OVERFLOW (t
) = 1;
1520 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1522 tree type
= TREE_TYPE (arg1
);
1523 tree r1
= TREE_REALPART (arg1
);
1524 tree i1
= TREE_IMAGPART (arg1
);
1525 tree r2
= TREE_REALPART (arg2
);
1526 tree i2
= TREE_IMAGPART (arg2
);
1533 real
= const_binop (code
, r1
, r2
);
1534 imag
= const_binop (code
, i1
, i2
);
1538 if (COMPLEX_FLOAT_TYPE_P (type
))
1539 return do_mpc_arg2 (arg1
, arg2
, type
,
1540 /* do_nonfinite= */ folding_initializer
,
1543 real
= const_binop (MINUS_EXPR
,
1544 const_binop (MULT_EXPR
, r1
, r2
),
1545 const_binop (MULT_EXPR
, i1
, i2
));
1546 imag
= const_binop (PLUS_EXPR
,
1547 const_binop (MULT_EXPR
, r1
, i2
),
1548 const_binop (MULT_EXPR
, i1
, r2
));
1552 if (COMPLEX_FLOAT_TYPE_P (type
))
1553 return do_mpc_arg2 (arg1
, arg2
, type
,
1554 /* do_nonfinite= */ folding_initializer
,
1557 case TRUNC_DIV_EXPR
:
1559 case FLOOR_DIV_EXPR
:
1560 case ROUND_DIV_EXPR
:
1561 if (flag_complex_method
== 0)
1563 /* Keep this algorithm in sync with
1564 tree-complex.cc:expand_complex_div_straight().
1566 Expand complex division to scalars, straightforward algorithm.
1567 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1571 = const_binop (PLUS_EXPR
,
1572 const_binop (MULT_EXPR
, r2
, r2
),
1573 const_binop (MULT_EXPR
, i2
, i2
));
1575 = const_binop (PLUS_EXPR
,
1576 const_binop (MULT_EXPR
, r1
, r2
),
1577 const_binop (MULT_EXPR
, i1
, i2
));
1579 = const_binop (MINUS_EXPR
,
1580 const_binop (MULT_EXPR
, i1
, r2
),
1581 const_binop (MULT_EXPR
, r1
, i2
));
1583 real
= const_binop (code
, t1
, magsquared
);
1584 imag
= const_binop (code
, t2
, magsquared
);
1588 /* Keep this algorithm in sync with
1589 tree-complex.cc:expand_complex_div_wide().
1591 Expand complex division to scalars, modified algorithm to minimize
1592 overflow with wide input ranges. */
1593 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1594 fold_abs_const (r2
, TREE_TYPE (type
)),
1595 fold_abs_const (i2
, TREE_TYPE (type
)));
1597 if (integer_nonzerop (compare
))
1599 /* In the TRUE branch, we compute
1601 div = (br * ratio) + bi;
1602 tr = (ar * ratio) + ai;
1603 ti = (ai * ratio) - ar;
1606 tree ratio
= const_binop (code
, r2
, i2
);
1607 tree div
= const_binop (PLUS_EXPR
, i2
,
1608 const_binop (MULT_EXPR
, r2
, ratio
));
1609 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1610 real
= const_binop (PLUS_EXPR
, real
, i1
);
1611 real
= const_binop (code
, real
, div
);
1613 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1614 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1615 imag
= const_binop (code
, imag
, div
);
1619 /* In the FALSE branch, we compute
1621 divisor = (d * ratio) + c;
1622 tr = (b * ratio) + a;
1623 ti = b - (a * ratio);
1626 tree ratio
= const_binop (code
, i2
, r2
);
1627 tree div
= const_binop (PLUS_EXPR
, r2
,
1628 const_binop (MULT_EXPR
, i2
, ratio
));
1630 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1631 real
= const_binop (PLUS_EXPR
, real
, r1
);
1632 real
= const_binop (code
, real
, div
);
1634 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1635 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1636 imag
= const_binop (code
, imag
, div
);
1646 return build_complex (type
, real
, imag
);
1649 if (TREE_CODE (arg1
) == VECTOR_CST
1650 && TREE_CODE (arg2
) == VECTOR_CST
1651 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)),
1652 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
))))
1654 tree type
= TREE_TYPE (arg1
);
1656 if (VECTOR_CST_STEPPED_P (arg1
)
1657 && VECTOR_CST_STEPPED_P (arg2
))
1658 /* We can operate directly on the encoding if:
1660 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1662 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1664 Addition and subtraction are the supported operators
1665 for which this is true. */
1666 step_ok_p
= (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
1667 else if (VECTOR_CST_STEPPED_P (arg1
))
1668 /* We can operate directly on stepped encodings if:
1672 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1674 which is true if (x -> x op c) distributes over addition. */
1675 step_ok_p
= distributes_over_addition_p (code
, 1);
1677 /* Similarly in reverse. */
1678 step_ok_p
= distributes_over_addition_p (code
, 2);
1679 tree_vector_builder elts
;
1680 if (!elts
.new_binary_operation (type
, arg1
, arg2
, step_ok_p
))
1682 unsigned int count
= elts
.encoded_nelts ();
1683 for (unsigned int i
= 0; i
< count
; ++i
)
1685 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1686 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1688 tree elt
= const_binop (code
, elem1
, elem2
);
1690 /* It is possible that const_binop cannot handle the given
1691 code and return NULL_TREE */
1692 if (elt
== NULL_TREE
)
1694 elts
.quick_push (elt
);
1697 return elts
.build ();
1700 /* Shifts allow a scalar offset for a vector. */
1701 if (TREE_CODE (arg1
) == VECTOR_CST
1702 && TREE_CODE (arg2
) == INTEGER_CST
)
1704 tree type
= TREE_TYPE (arg1
);
1705 bool step_ok_p
= distributes_over_addition_p (code
, 1);
1706 tree_vector_builder elts
;
1707 if (!elts
.new_unary_operation (type
, arg1
, step_ok_p
))
1709 unsigned int count
= elts
.encoded_nelts ();
1710 for (unsigned int i
= 0; i
< count
; ++i
)
1712 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1714 tree elt
= const_binop (code
, elem1
, arg2
);
1716 /* It is possible that const_binop cannot handle the given
1717 code and return NULL_TREE. */
1718 if (elt
== NULL_TREE
)
1720 elts
.quick_push (elt
);
1723 return elts
.build ();
1728 /* Overload that adds a TYPE parameter to be able to dispatch
1729 to fold_relational_const. */
1732 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1734 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1735 return fold_relational_const (code
, type
, arg1
, arg2
);
1737 /* ??? Until we make the const_binop worker take the type of the
1738 result as argument put those cases that need it here. */
1741 case VEC_SERIES_EXPR
:
1742 if (CONSTANT_CLASS_P (arg1
)
1743 && CONSTANT_CLASS_P (arg2
))
1744 return build_vec_series (type
, arg1
, arg2
);
1748 if ((TREE_CODE (arg1
) == REAL_CST
1749 && TREE_CODE (arg2
) == REAL_CST
)
1750 || (TREE_CODE (arg1
) == INTEGER_CST
1751 && TREE_CODE (arg2
) == INTEGER_CST
))
1752 return build_complex (type
, arg1
, arg2
);
1755 case POINTER_DIFF_EXPR
:
1756 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1758 poly_offset_int res
= (wi::to_poly_offset (arg1
)
1759 - wi::to_poly_offset (arg2
));
1760 return force_fit_type (type
, res
, 1,
1761 TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1765 case VEC_PACK_TRUNC_EXPR
:
1766 case VEC_PACK_FIX_TRUNC_EXPR
:
1767 case VEC_PACK_FLOAT_EXPR
:
1769 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1771 if (TREE_CODE (arg1
) != VECTOR_CST
1772 || TREE_CODE (arg2
) != VECTOR_CST
)
1775 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1778 out_nelts
= in_nelts
* 2;
1779 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1780 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1782 tree_vector_builder
elts (type
, out_nelts
, 1);
1783 for (i
= 0; i
< out_nelts
; i
++)
1785 tree elt
= (i
< in_nelts
1786 ? VECTOR_CST_ELT (arg1
, i
)
1787 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1788 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1790 : code
== VEC_PACK_FLOAT_EXPR
1791 ? FLOAT_EXPR
: FIX_TRUNC_EXPR
,
1792 TREE_TYPE (type
), elt
);
1793 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1795 elts
.quick_push (elt
);
1798 return elts
.build ();
1801 case VEC_WIDEN_MULT_LO_EXPR
:
1802 case VEC_WIDEN_MULT_HI_EXPR
:
1803 case VEC_WIDEN_MULT_EVEN_EXPR
:
1804 case VEC_WIDEN_MULT_ODD_EXPR
:
1806 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1808 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1811 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1813 out_nelts
= in_nelts
/ 2;
1814 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1815 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1817 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1818 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1819 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1820 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1821 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1823 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1826 tree_vector_builder
elts (type
, out_nelts
, 1);
1827 for (out
= 0; out
< out_nelts
; out
++)
1829 unsigned int in
= (out
<< scale
) + ofs
;
1830 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1831 VECTOR_CST_ELT (arg1
, in
));
1832 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1833 VECTOR_CST_ELT (arg2
, in
));
1835 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1837 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1838 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1840 elts
.quick_push (elt
);
1843 return elts
.build ();
1849 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1852 /* Make sure type and arg0 have the same saturating flag. */
1853 gcc_checking_assert (TYPE_SATURATING (type
)
1854 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1856 return const_binop (code
, arg1
, arg2
);
1859 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1860 Return zero if computing the constants is not possible. */
1863 const_unop (enum tree_code code
, tree type
, tree arg0
)
1865 /* Don't perform the operation, other than NEGATE and ABS, if
1866 flag_signaling_nans is on and the operand is a signaling NaN. */
1867 if (TREE_CODE (arg0
) == REAL_CST
1868 && HONOR_SNANS (arg0
)
1869 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1870 && code
!= NEGATE_EXPR
1872 && code
!= ABSU_EXPR
)
1879 case FIX_TRUNC_EXPR
:
1880 case FIXED_CONVERT_EXPR
:
1881 return fold_convert_const (code
, type
, arg0
);
1883 case ADDR_SPACE_CONVERT_EXPR
:
1884 /* If the source address is 0, and the source address space
1885 cannot have a valid object at 0, fold to dest type null. */
1886 if (integer_zerop (arg0
)
1887 && !(targetm
.addr_space
.zero_address_valid
1888 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1889 return fold_convert_const (code
, type
, arg0
);
1892 case VIEW_CONVERT_EXPR
:
1893 return fold_view_convert_expr (type
, arg0
);
1897 /* Can't call fold_negate_const directly here as that doesn't
1898 handle all cases and we might not be able to negate some
1900 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1901 if (tem
&& CONSTANT_CLASS_P (tem
))
1908 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1909 return fold_abs_const (arg0
, type
);
1913 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1915 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1917 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1922 if (TREE_CODE (arg0
) == INTEGER_CST
)
1923 return fold_not_const (arg0
, type
);
1924 else if (POLY_INT_CST_P (arg0
))
1925 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1926 /* Perform BIT_NOT_EXPR on each element individually. */
1927 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1931 /* This can cope with stepped encodings because ~x == -1 - x. */
1932 tree_vector_builder elements
;
1933 elements
.new_unary_operation (type
, arg0
, true);
1934 unsigned int i
, count
= elements
.encoded_nelts ();
1935 for (i
= 0; i
< count
; ++i
)
1937 elem
= VECTOR_CST_ELT (arg0
, i
);
1938 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1939 if (elem
== NULL_TREE
)
1941 elements
.quick_push (elem
);
1944 return elements
.build ();
1948 case TRUTH_NOT_EXPR
:
1949 if (TREE_CODE (arg0
) == INTEGER_CST
)
1950 return constant_boolean_node (integer_zerop (arg0
), type
);
1954 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1955 return fold_convert (type
, TREE_REALPART (arg0
));
1959 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1960 return fold_convert (type
, TREE_IMAGPART (arg0
));
1963 case VEC_UNPACK_LO_EXPR
:
1964 case VEC_UNPACK_HI_EXPR
:
1965 case VEC_UNPACK_FLOAT_LO_EXPR
:
1966 case VEC_UNPACK_FLOAT_HI_EXPR
:
1967 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
1968 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
1970 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1971 enum tree_code subcode
;
1973 if (TREE_CODE (arg0
) != VECTOR_CST
)
1976 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1978 out_nelts
= in_nelts
/ 2;
1979 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1981 unsigned int offset
= 0;
1982 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1983 || code
== VEC_UNPACK_FLOAT_LO_EXPR
1984 || code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
))
1987 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1989 else if (code
== VEC_UNPACK_FLOAT_LO_EXPR
1990 || code
== VEC_UNPACK_FLOAT_HI_EXPR
)
1991 subcode
= FLOAT_EXPR
;
1993 subcode
= FIX_TRUNC_EXPR
;
1995 tree_vector_builder
elts (type
, out_nelts
, 1);
1996 for (i
= 0; i
< out_nelts
; i
++)
1998 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1999 VECTOR_CST_ELT (arg0
, i
+ offset
));
2000 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
2002 elts
.quick_push (elt
);
2005 return elts
.build ();
2008 case VEC_DUPLICATE_EXPR
:
2009 if (CONSTANT_CLASS_P (arg0
))
2010 return build_vector_from_val (type
, arg0
);
2020 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2021 indicates which particular sizetype to create. */
2024 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
2026 return build_int_cst (sizetype_tab
[(int) kind
], number
);
2029 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2030 is a tree code. The type of the result is taken from the operands.
2031 Both must be equivalent integer types, ala int_binop_types_match_p.
2032 If the operands are constant, so is the result. */
2035 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
2037 tree type
= TREE_TYPE (arg0
);
2039 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
2040 return error_mark_node
;
2042 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
2045 /* Handle the special case of two poly_int constants faster. */
2046 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
2048 /* And some specific cases even faster than that. */
2049 if (code
== PLUS_EXPR
)
2051 if (integer_zerop (arg0
)
2052 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
2054 if (integer_zerop (arg1
)
2055 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
2058 else if (code
== MINUS_EXPR
)
2060 if (integer_zerop (arg1
)
2061 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
2064 else if (code
== MULT_EXPR
)
2066 if (integer_onep (arg0
)
2067 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
2071 /* Handle general case of two integer constants. For sizetype
2072 constant calculations we always want to know about overflow,
2073 even in the unsigned case. */
2074 tree res
= int_const_binop (code
, arg0
, arg1
, -1);
2075 if (res
!= NULL_TREE
)
2079 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
2082 /* Given two values, either both of sizetype or both of bitsizetype,
2083 compute the difference between the two values. Return the value
2084 in signed type corresponding to the type of the operands. */
2087 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
2089 tree type
= TREE_TYPE (arg0
);
2092 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
2095 /* If the type is already signed, just do the simple thing. */
2096 if (!TYPE_UNSIGNED (type
))
2097 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
2099 if (type
== sizetype
)
2101 else if (type
== bitsizetype
)
2102 ctype
= sbitsizetype
;
2104 ctype
= signed_type_for (type
);
2106 /* If either operand is not a constant, do the conversions to the signed
2107 type and subtract. The hardware will do the right thing with any
2108 overflow in the subtraction. */
2109 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
2110 return size_binop_loc (loc
, MINUS_EXPR
,
2111 fold_convert_loc (loc
, ctype
, arg0
),
2112 fold_convert_loc (loc
, ctype
, arg1
));
2114 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2115 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2116 overflow) and negate (which can't either). Special-case a result
2117 of zero while we're here. */
2118 if (tree_int_cst_equal (arg0
, arg1
))
2119 return build_int_cst (ctype
, 0);
2120 else if (tree_int_cst_lt (arg1
, arg0
))
2121 return fold_convert_loc (loc
, ctype
,
2122 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
2124 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
2125 fold_convert_loc (loc
, ctype
,
2126 size_binop_loc (loc
,
2131 /* A subroutine of fold_convert_const handling conversions of an
2132 INTEGER_CST to another integer type. */
2135 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
2137 /* Given an integer constant, make new constant with new type,
2138 appropriately sign-extended or truncated. Use widest_int
2139 so that any extension is done according ARG1's type. */
2140 tree arg1_type
= TREE_TYPE (arg1
);
2141 unsigned prec
= MAX (TYPE_PRECISION (arg1_type
), TYPE_PRECISION (type
));
2142 return force_fit_type (type
, wide_int::from (wi::to_wide (arg1
), prec
,
2143 TYPE_SIGN (arg1_type
)),
2144 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
2145 TREE_OVERFLOW (arg1
));
2148 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2149 to an integer type. */
2152 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
2154 bool overflow
= false;
2157 /* The following code implements the floating point to integer
2158 conversion rules required by the Java Language Specification,
2159 that IEEE NaNs are mapped to zero and values that overflow
2160 the target precision saturate, i.e. values greater than
2161 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2162 are mapped to INT_MIN. These semantics are allowed by the
2163 C and C++ standards that simply state that the behavior of
2164 FP-to-integer conversion is unspecified upon overflow. */
2168 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2172 case FIX_TRUNC_EXPR
:
2173 real_trunc (&r
, VOIDmode
, &x
);
2180 /* If R is NaN, return zero and show we have an overflow. */
2181 if (REAL_VALUE_ISNAN (r
))
2184 val
= wi::zero (TYPE_PRECISION (type
));
2187 /* See if R is less than the lower bound or greater than the
2192 tree lt
= TYPE_MIN_VALUE (type
);
2193 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2194 if (real_less (&r
, &l
))
2197 val
= wi::to_wide (lt
);
2203 tree ut
= TYPE_MAX_VALUE (type
);
2206 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2207 if (real_less (&u
, &r
))
2210 val
= wi::to_wide (ut
);
2216 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2218 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2222 /* A subroutine of fold_convert_const handling conversions of a
2223 FIXED_CST to an integer type. */
2226 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2229 double_int temp
, temp_trunc
;
2232 /* Right shift FIXED_CST to temp by fbit. */
2233 temp
= TREE_FIXED_CST (arg1
).data
;
2234 mode
= TREE_FIXED_CST (arg1
).mode
;
2235 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2237 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2238 HOST_BITS_PER_DOUBLE_INT
,
2239 SIGNED_FIXED_POINT_MODE_P (mode
));
2241 /* Left shift temp to temp_trunc by fbit. */
2242 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2243 HOST_BITS_PER_DOUBLE_INT
,
2244 SIGNED_FIXED_POINT_MODE_P (mode
));
2248 temp
= double_int_zero
;
2249 temp_trunc
= double_int_zero
;
2252 /* If FIXED_CST is negative, we need to round the value toward 0.
2253 By checking if the fractional bits are not zero to add 1 to temp. */
2254 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2255 && temp_trunc
.is_negative ()
2256 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2257 temp
+= double_int_one
;
2259 /* Given a fixed-point constant, make new constant with new type,
2260 appropriately sign-extended or truncated. */
2261 t
= force_fit_type (type
, temp
, -1,
2262 (temp
.is_negative ()
2263 && (TYPE_UNSIGNED (type
)
2264 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2265 | TREE_OVERFLOW (arg1
));
2270 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2271 to another floating point type. */
2274 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2276 REAL_VALUE_TYPE value
;
2279 /* If the underlying modes are the same, simply treat it as
2280 copy and rebuild with TREE_REAL_CST information and the
2282 if (TYPE_MODE (type
) == TYPE_MODE (TREE_TYPE (arg1
)))
2284 t
= build_real (type
, TREE_REAL_CST (arg1
));
2288 /* Don't perform the operation if flag_signaling_nans is on
2289 and the operand is a signaling NaN. */
2290 if (HONOR_SNANS (arg1
)
2291 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2294 /* With flag_rounding_math we should respect the current rounding mode
2295 unless the conversion is exact. */
2296 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1
)
2297 && !exact_real_truncate (TYPE_MODE (type
), &TREE_REAL_CST (arg1
)))
2300 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2301 t
= build_real (type
, value
);
2303 /* If converting an infinity or NAN to a representation that doesn't
2304 have one, set the overflow bit so that we can produce some kind of
2305 error message at the appropriate point if necessary. It's not the
2306 most user-friendly message, but it's better than nothing. */
2307 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2308 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2309 TREE_OVERFLOW (t
) = 1;
2310 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2311 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2312 TREE_OVERFLOW (t
) = 1;
2313 /* Regular overflow, conversion produced an infinity in a mode that
2314 can't represent them. */
2315 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2316 && REAL_VALUE_ISINF (value
)
2317 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2318 TREE_OVERFLOW (t
) = 1;
2320 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2324 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2325 to a floating point type. */
2328 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2330 REAL_VALUE_TYPE value
;
2333 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2334 &TREE_FIXED_CST (arg1
));
2335 t
= build_real (type
, value
);
2337 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2341 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2342 to another fixed-point type. */
2345 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2347 FIXED_VALUE_TYPE value
;
2351 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2352 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2353 t
= build_fixed (type
, value
);
2355 /* Propagate overflow flags. */
2356 if (overflow_p
| TREE_OVERFLOW (arg1
))
2357 TREE_OVERFLOW (t
) = 1;
2361 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2362 to a fixed-point type. */
2365 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2367 FIXED_VALUE_TYPE value
;
2372 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2374 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2375 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2376 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2378 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2380 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2381 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2382 TYPE_SATURATING (type
));
2383 t
= build_fixed (type
, value
);
2385 /* Propagate overflow flags. */
2386 if (overflow_p
| TREE_OVERFLOW (arg1
))
2387 TREE_OVERFLOW (t
) = 1;
2391 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2392 to a fixed-point type. */
2395 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2397 FIXED_VALUE_TYPE value
;
2401 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2402 &TREE_REAL_CST (arg1
),
2403 TYPE_SATURATING (type
));
2404 t
= build_fixed (type
, value
);
2406 /* Propagate overflow flags. */
2407 if (overflow_p
| TREE_OVERFLOW (arg1
))
2408 TREE_OVERFLOW (t
) = 1;
2412 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2413 type TYPE. If no simplification can be done return NULL_TREE. */
2416 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2418 tree arg_type
= TREE_TYPE (arg1
);
2419 if (arg_type
== type
)
2422 /* We can't widen types, since the runtime value could overflow the
2423 original type before being extended to the new type. */
2424 if (POLY_INT_CST_P (arg1
)
2425 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2426 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2427 return build_poly_int_cst (type
,
2428 poly_wide_int::from (poly_int_cst_value (arg1
),
2429 TYPE_PRECISION (type
),
2430 TYPE_SIGN (arg_type
)));
2432 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2433 || TREE_CODE (type
) == OFFSET_TYPE
)
2435 if (TREE_CODE (arg1
) == INTEGER_CST
)
2436 return fold_convert_const_int_from_int (type
, arg1
);
2437 else if (TREE_CODE (arg1
) == REAL_CST
)
2438 return fold_convert_const_int_from_real (code
, type
, arg1
);
2439 else if (TREE_CODE (arg1
) == FIXED_CST
)
2440 return fold_convert_const_int_from_fixed (type
, arg1
);
2442 else if (SCALAR_FLOAT_TYPE_P (type
))
2444 if (TREE_CODE (arg1
) == INTEGER_CST
)
2446 tree res
= build_real_from_int_cst (type
, arg1
);
2447 /* Avoid the folding if flag_rounding_math is on and the
2448 conversion is not exact. */
2449 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
2452 wide_int w
= real_to_integer (&TREE_REAL_CST (res
), &fail
,
2453 TYPE_PRECISION (TREE_TYPE (arg1
)));
2454 if (fail
|| wi::ne_p (w
, wi::to_wide (arg1
)))
2459 else if (TREE_CODE (arg1
) == REAL_CST
)
2460 return fold_convert_const_real_from_real (type
, arg1
);
2461 else if (TREE_CODE (arg1
) == FIXED_CST
)
2462 return fold_convert_const_real_from_fixed (type
, arg1
);
2464 else if (FIXED_POINT_TYPE_P (type
))
2466 if (TREE_CODE (arg1
) == FIXED_CST
)
2467 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2468 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2469 return fold_convert_const_fixed_from_int (type
, arg1
);
2470 else if (TREE_CODE (arg1
) == REAL_CST
)
2471 return fold_convert_const_fixed_from_real (type
, arg1
);
2473 else if (VECTOR_TYPE_P (type
))
2475 if (TREE_CODE (arg1
) == VECTOR_CST
2476 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2478 tree elttype
= TREE_TYPE (type
);
2479 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2480 /* We can't handle steps directly when extending, since the
2481 values need to wrap at the original precision first. */
2483 = (INTEGRAL_TYPE_P (elttype
)
2484 && INTEGRAL_TYPE_P (arg1_elttype
)
2485 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2486 tree_vector_builder v
;
2487 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2489 unsigned int len
= v
.encoded_nelts ();
2490 for (unsigned int i
= 0; i
< len
; ++i
)
2492 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2493 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2494 if (cvt
== NULL_TREE
)
2504 /* Construct a vector of zero elements of vector type TYPE. */
2507 build_zero_vector (tree type
)
2511 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2512 return build_vector_from_val (type
, t
);
2515 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2518 fold_convertible_p (const_tree type
, const_tree arg
)
2520 const_tree orig
= TREE_TYPE (arg
);
2525 if (TREE_CODE (arg
) == ERROR_MARK
2526 || TREE_CODE (type
) == ERROR_MARK
2527 || TREE_CODE (orig
) == ERROR_MARK
)
2530 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2533 switch (TREE_CODE (type
))
2535 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2536 case POINTER_TYPE
: case REFERENCE_TYPE
:
2538 return (INTEGRAL_TYPE_P (orig
)
2539 || (POINTER_TYPE_P (orig
)
2540 && TYPE_PRECISION (type
) <= TYPE_PRECISION (orig
))
2541 || TREE_CODE (orig
) == OFFSET_TYPE
);
2544 case FIXED_POINT_TYPE
:
2546 return TREE_CODE (type
) == TREE_CODE (orig
);
2549 return (VECTOR_TYPE_P (orig
)
2550 && known_eq (TYPE_VECTOR_SUBPARTS (type
),
2551 TYPE_VECTOR_SUBPARTS (orig
))
2552 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2559 /* Convert expression ARG to type TYPE. Used by the middle-end for
2560 simple conversions in preference to calling the front-end's convert. */
2563 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2565 tree orig
= TREE_TYPE (arg
);
2571 if (TREE_CODE (arg
) == ERROR_MARK
2572 || TREE_CODE (type
) == ERROR_MARK
2573 || TREE_CODE (orig
) == ERROR_MARK
)
2574 return error_mark_node
;
2576 switch (TREE_CODE (type
))
2579 case REFERENCE_TYPE
:
2580 /* Handle conversions between pointers to different address spaces. */
2581 if (POINTER_TYPE_P (orig
)
2582 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2583 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2584 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2587 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2588 case OFFSET_TYPE
: case BITINT_TYPE
:
2589 if (TREE_CODE (arg
) == INTEGER_CST
)
2591 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2592 if (tem
!= NULL_TREE
)
2595 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2596 || TREE_CODE (orig
) == OFFSET_TYPE
)
2597 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2598 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2599 return fold_convert_loc (loc
, type
,
2600 fold_build1_loc (loc
, REALPART_EXPR
,
2601 TREE_TYPE (orig
), arg
));
2602 gcc_assert (VECTOR_TYPE_P (orig
)
2603 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2604 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2607 if (TREE_CODE (arg
) == INTEGER_CST
)
2609 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2610 if (tem
!= NULL_TREE
)
2613 else if (TREE_CODE (arg
) == REAL_CST
)
2615 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2616 if (tem
!= NULL_TREE
)
2619 else if (TREE_CODE (arg
) == FIXED_CST
)
2621 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2622 if (tem
!= NULL_TREE
)
2626 switch (TREE_CODE (orig
))
2628 case INTEGER_TYPE
: case BITINT_TYPE
:
2629 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2630 case POINTER_TYPE
: case REFERENCE_TYPE
:
2631 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2634 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2636 case FIXED_POINT_TYPE
:
2637 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2640 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2641 return fold_convert_loc (loc
, type
, tem
);
2647 case FIXED_POINT_TYPE
:
2648 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2649 || TREE_CODE (arg
) == REAL_CST
)
2651 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2652 if (tem
!= NULL_TREE
)
2653 goto fold_convert_exit
;
2656 switch (TREE_CODE (orig
))
2658 case FIXED_POINT_TYPE
:
2664 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2667 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2668 return fold_convert_loc (loc
, type
, tem
);
2675 switch (TREE_CODE (orig
))
2677 case INTEGER_TYPE
: case BITINT_TYPE
:
2678 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2679 case POINTER_TYPE
: case REFERENCE_TYPE
:
2681 case FIXED_POINT_TYPE
:
2682 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2683 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2684 fold_convert_loc (loc
, TREE_TYPE (type
),
2685 integer_zero_node
));
2690 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2692 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2693 TREE_OPERAND (arg
, 0));
2694 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2695 TREE_OPERAND (arg
, 1));
2696 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2699 arg
= save_expr (arg
);
2700 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2701 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2702 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2703 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2704 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2712 if (integer_zerop (arg
))
2713 return build_zero_vector (type
);
2714 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2715 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2716 || VECTOR_TYPE_P (orig
));
2717 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2720 tem
= fold_ignored_result (arg
);
2721 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2724 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2725 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2729 tem
= protected_set_expr_location_unshare (tem
, loc
);
2733 /* Return false if expr can be assumed not to be an lvalue, true
2737 maybe_lvalue_p (const_tree x
)
2739 /* We only need to wrap lvalue tree codes. */
2740 switch (TREE_CODE (x
))
2748 case COMPOUND_LITERAL_EXPR
:
2754 case ARRAY_RANGE_REF
:
2760 case PREINCREMENT_EXPR
:
2761 case PREDECREMENT_EXPR
:
2763 case TRY_CATCH_EXPR
:
2764 case WITH_CLEANUP_EXPR
:
2770 case VIEW_CONVERT_EXPR
:
2774 /* Assume the worst for front-end tree codes. */
2775 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2783 /* Return an expr equal to X but certainly not valid as an lvalue. */
2786 non_lvalue_loc (location_t loc
, tree x
)
2788 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2793 if (! maybe_lvalue_p (x
))
2795 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2798 /* Given a tree comparison code, return the code that is the logical inverse.
2799 It is generally not safe to do this for floating-point comparisons, except
2800 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2801 ERROR_MARK in this case. */
2804 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2806 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2807 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2817 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2819 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2821 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2823 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2837 return UNORDERED_EXPR
;
2838 case UNORDERED_EXPR
:
2839 return ORDERED_EXPR
;
2845 /* Similar, but return the comparison that results if the operands are
2846 swapped. This is safe for floating-point. */
2849 swap_tree_comparison (enum tree_code code
)
2856 case UNORDERED_EXPR
:
2882 /* Convert a comparison tree code from an enum tree_code representation
2883 into a compcode bit-based encoding. This function is the inverse of
2884 compcode_to_comparison. */
2886 static enum comparison_code
2887 comparison_to_compcode (enum tree_code code
)
2904 return COMPCODE_ORD
;
2905 case UNORDERED_EXPR
:
2906 return COMPCODE_UNORD
;
2908 return COMPCODE_UNLT
;
2910 return COMPCODE_UNEQ
;
2912 return COMPCODE_UNLE
;
2914 return COMPCODE_UNGT
;
2916 return COMPCODE_LTGT
;
2918 return COMPCODE_UNGE
;
2924 /* Convert a compcode bit-based encoding of a comparison operator back
2925 to GCC's enum tree_code representation. This function is the
2926 inverse of comparison_to_compcode. */
2928 static enum tree_code
2929 compcode_to_comparison (enum comparison_code code
)
2946 return ORDERED_EXPR
;
2947 case COMPCODE_UNORD
:
2948 return UNORDERED_EXPR
;
2966 /* Return true if COND1 tests the opposite condition of COND2. */
2969 inverse_conditions_p (const_tree cond1
, const_tree cond2
)
2971 return (COMPARISON_CLASS_P (cond1
)
2972 && COMPARISON_CLASS_P (cond2
)
2973 && (invert_tree_comparison
2975 HONOR_NANS (TREE_OPERAND (cond1
, 0))) == TREE_CODE (cond2
))
2976 && operand_equal_p (TREE_OPERAND (cond1
, 0),
2977 TREE_OPERAND (cond2
, 0), 0)
2978 && operand_equal_p (TREE_OPERAND (cond1
, 1),
2979 TREE_OPERAND (cond2
, 1), 0));
2982 /* Return a tree for the comparison which is the combination of
2983 doing the AND or OR (depending on CODE) of the two operations LCODE
2984 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2985 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2986 if this makes the transformation invalid. */
2989 combine_comparisons (location_t loc
,
2990 enum tree_code code
, enum tree_code lcode
,
2991 enum tree_code rcode
, tree truth_type
,
2992 tree ll_arg
, tree lr_arg
)
2994 bool honor_nans
= HONOR_NANS (ll_arg
);
2995 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2996 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
3001 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
3002 compcode
= lcompcode
& rcompcode
;
3005 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
3006 compcode
= lcompcode
| rcompcode
;
3015 /* Eliminate unordered comparisons, as well as LTGT and ORD
3016 which are not used unless the mode has NaNs. */
3017 compcode
&= ~COMPCODE_UNORD
;
3018 if (compcode
== COMPCODE_LTGT
)
3019 compcode
= COMPCODE_NE
;
3020 else if (compcode
== COMPCODE_ORD
)
3021 compcode
= COMPCODE_TRUE
;
3023 else if (flag_trapping_math
)
3025 /* Check that the original operation and the optimized ones will trap
3026 under the same condition. */
3027 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
3028 && (lcompcode
!= COMPCODE_EQ
)
3029 && (lcompcode
!= COMPCODE_ORD
);
3030 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
3031 && (rcompcode
!= COMPCODE_EQ
)
3032 && (rcompcode
!= COMPCODE_ORD
);
3033 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
3034 && (compcode
!= COMPCODE_EQ
)
3035 && (compcode
!= COMPCODE_ORD
);
3037 /* In a short-circuited boolean expression the LHS might be
3038 such that the RHS, if evaluated, will never trap. For
3039 example, in ORD (x, y) && (x < y), we evaluate the RHS only
3040 if neither x nor y is NaN. (This is a mixed blessing: for
3041 example, the expression above will never trap, hence
3042 optimizing it to x < y would be invalid). */
3043 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
3044 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
3047 /* If the comparison was short-circuited, and only the RHS
3048 trapped, we may now generate a spurious trap. */
3050 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3053 /* If we changed the conditions that cause a trap, we lose. */
3054 if ((ltrap
|| rtrap
) != trap
)
3058 if (compcode
== COMPCODE_TRUE
)
3059 return constant_boolean_node (true, truth_type
);
3060 else if (compcode
== COMPCODE_FALSE
)
3061 return constant_boolean_node (false, truth_type
);
3064 enum tree_code tcode
;
3066 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
3067 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
3071 /* Return nonzero if two operands (typically of the same tree node)
3072 are necessarily equal. FLAGS modifies behavior as follows:
3074 If OEP_ONLY_CONST is set, only return nonzero for constants.
3075 This function tests whether the operands are indistinguishable;
3076 it does not test whether they are equal using C's == operation.
3077 The distinction is important for IEEE floating point, because
3078 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3079 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3081 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3082 even though it may hold multiple values during a function.
3083 This is because a GCC tree node guarantees that nothing else is
3084 executed between the evaluation of its "operands" (which may often
3085 be evaluated in arbitrary order). Hence if the operands themselves
3086 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3087 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3088 unset means assuming isochronic (or instantaneous) tree equivalence.
3089 Unless comparing arbitrary expression trees, such as from different
3090 statements, this flag can usually be left unset.
3092 If OEP_PURE_SAME is set, then pure functions with identical arguments
3093 are considered the same. It is used when the caller has other ways
3094 to ensure that global memory is unchanged in between.
3096 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3097 not values of expressions.
3099 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3100 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3102 If OEP_BITWISE is set, then require the values to be bitwise identical
3103 rather than simply numerically equal. Do not take advantage of things
3104 like math-related flags or undefined behavior; only return true for
3105 values that are provably bitwise identical in all circumstances.
3107 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3108 any operand with side effect. This is unnecesarily conservative in the
3109 case we know that arg0 and arg1 are in disjoint code paths (such as in
3110 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3111 addresses with TREE_CONSTANT flag set so we know that &var == &var
3112 even if var is volatile. */
3115 operand_compare::operand_equal_p (const_tree arg0
, const_tree arg1
,
3119 if (verify_hash_value (arg0
, arg1
, flags
, &r
))
3122 STRIP_ANY_LOCATION_WRAPPER (arg0
);
3123 STRIP_ANY_LOCATION_WRAPPER (arg1
);
3125 /* If either is ERROR_MARK, they aren't equal. */
3126 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
3127 || TREE_TYPE (arg0
) == error_mark_node
3128 || TREE_TYPE (arg1
) == error_mark_node
)
3131 /* Similar, if either does not have a type (like a template id),
3132 they aren't equal. */
3133 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
3136 /* Bitwise identity makes no sense if the values have different layouts. */
3137 if ((flags
& OEP_BITWISE
)
3138 && !tree_nop_conversion_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3141 /* We cannot consider pointers to different address space equal. */
3142 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
3143 && POINTER_TYPE_P (TREE_TYPE (arg1
))
3144 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
3145 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
3148 /* Check equality of integer constants before bailing out due to
3149 precision differences. */
3150 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3152 /* Address of INTEGER_CST is not defined; check that we did not forget
3153 to drop the OEP_ADDRESS_OF flags. */
3154 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3155 return tree_int_cst_equal (arg0
, arg1
);
3158 if (!(flags
& OEP_ADDRESS_OF
))
3160 /* If both types don't have the same signedness, then we can't consider
3161 them equal. We must check this before the STRIP_NOPS calls
3162 because they may change the signedness of the arguments. As pointers
3163 strictly don't have a signedness, require either two pointers or
3164 two non-pointers as well. */
3165 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
3166 || POINTER_TYPE_P (TREE_TYPE (arg0
))
3167 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
3170 /* If both types don't have the same precision, then it is not safe
3172 if (element_precision (TREE_TYPE (arg0
))
3173 != element_precision (TREE_TYPE (arg1
)))
3180 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3181 sanity check once the issue is solved. */
3183 /* Addresses of conversions and SSA_NAMEs (and many other things)
3184 are not defined. Check that we did not forget to drop the
3185 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3186 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
3187 && TREE_CODE (arg0
) != SSA_NAME
);
3190 /* In case both args are comparisons but with different comparison
3191 code, try to swap the comparison operands of one arg to produce
3192 a match and compare that variant. */
3193 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3194 && COMPARISON_CLASS_P (arg0
)
3195 && COMPARISON_CLASS_P (arg1
))
3197 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
3199 if (TREE_CODE (arg0
) == swap_code
)
3200 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3201 TREE_OPERAND (arg1
, 1), flags
)
3202 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3203 TREE_OPERAND (arg1
, 0), flags
);
3206 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3208 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3209 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3211 else if (flags
& OEP_ADDRESS_OF
)
3213 /* If we are interested in comparing addresses ignore
3214 MEM_REF wrappings of the base that can appear just for
3216 if (TREE_CODE (arg0
) == MEM_REF
3218 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3219 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3220 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3222 else if (TREE_CODE (arg1
) == MEM_REF
3224 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3225 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3226 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3234 /* When not checking adddresses, this is needed for conversions and for
3235 COMPONENT_REF. Might as well play it safe and always test this. */
3236 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3237 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3238 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3239 && !(flags
& OEP_ADDRESS_OF
)))
3242 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3243 We don't care about side effects in that case because the SAVE_EXPR
3244 takes care of that for us. In all other cases, two expressions are
3245 equal if they have no side effects. If we have two identical
3246 expressions with side effects that should be treated the same due
3247 to the only side effects being identical SAVE_EXPR's, that will
3248 be detected in the recursive calls below.
3249 If we are taking an invariant address of two identical objects
3250 they are necessarily equal as well. */
3251 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3252 && (TREE_CODE (arg0
) == SAVE_EXPR
3253 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3254 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3257 /* Next handle constant cases, those for which we can return 1 even
3258 if ONLY_CONST is set. */
3259 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3260 switch (TREE_CODE (arg0
))
3263 return tree_int_cst_equal (arg0
, arg1
);
3266 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3267 TREE_FIXED_CST (arg1
));
3270 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3273 if (!(flags
& OEP_BITWISE
) && !HONOR_SIGNED_ZEROS (arg0
))
3275 /* If we do not distinguish between signed and unsigned zero,
3276 consider them equal. */
3277 if (real_zerop (arg0
) && real_zerop (arg1
))
3284 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3285 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3288 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3289 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3292 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3293 for (unsigned int i
= 0; i
< count
; ++i
)
3294 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3295 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3301 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3303 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3307 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3308 && ! memcmp (TREE_STRING_POINTER (arg0
),
3309 TREE_STRING_POINTER (arg1
),
3310 TREE_STRING_LENGTH (arg0
)));
3313 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3314 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3315 flags
| OEP_ADDRESS_OF
3316 | OEP_MATCH_SIDE_EFFECTS
);
3319 /* In GIMPLE empty constructors are allowed in initializers of
3321 if (!CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
))
3324 /* See sem_variable::equals in ipa-icf for a similar approach. */
3325 tree typ0
= TREE_TYPE (arg0
);
3326 tree typ1
= TREE_TYPE (arg1
);
3328 if (TREE_CODE (typ0
) != TREE_CODE (typ1
))
3330 else if (TREE_CODE (typ0
) == ARRAY_TYPE
)
3332 /* For arrays, check that the sizes all match. */
3333 const HOST_WIDE_INT siz0
= int_size_in_bytes (typ0
);
3334 if (TYPE_MODE (typ0
) != TYPE_MODE (typ1
)
3336 || siz0
!= int_size_in_bytes (typ1
))
3339 else if (!types_compatible_p (typ0
, typ1
))
3342 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3343 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3344 if (vec_safe_length (v0
) != vec_safe_length (v1
))
3347 /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3348 of the CONSTRUCTOR referenced indirectly. */
3349 flags
&= ~OEP_ADDRESS_OF
;
3351 for (unsigned idx
= 0; idx
< vec_safe_length (v0
); ++idx
)
3353 constructor_elt
*c0
= &(*v0
)[idx
];
3354 constructor_elt
*c1
= &(*v1
)[idx
];
3356 /* Check that the values are the same... */
3357 if (c0
->value
!= c1
->value
3358 && !operand_equal_p (c0
->value
, c1
->value
, flags
))
3361 /* ... and that they apply to the same field! */
3362 if (c0
->index
!= c1
->index
3363 && (TREE_CODE (typ0
) == ARRAY_TYPE
3364 ? !operand_equal_p (c0
->index
, c1
->index
, flags
)
3365 : !operand_equal_p (DECL_FIELD_OFFSET (c0
->index
),
3366 DECL_FIELD_OFFSET (c1
->index
),
3368 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0
->index
),
3369 DECL_FIELD_BIT_OFFSET (c1
->index
),
3381 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3382 two instances of undefined behavior will give identical results. */
3383 if (flags
& (OEP_ONLY_CONST
| OEP_BITWISE
))
3386 /* Define macros to test an operand from arg0 and arg1 for equality and a
3387 variant that allows null and views null as being different from any
3388 non-null value. In the latter case, if either is null, the both
3389 must be; otherwise, do the normal comparison. */
3390 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3391 TREE_OPERAND (arg1, N), flags)
3393 #define OP_SAME_WITH_NULL(N) \
3394 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3395 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3397 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3400 /* Two conversions are equal only if signedness and modes match. */
3401 switch (TREE_CODE (arg0
))
3404 case FIX_TRUNC_EXPR
:
3405 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3406 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3416 case tcc_comparison
:
3418 if (OP_SAME (0) && OP_SAME (1))
3421 /* For commutative ops, allow the other order. */
3422 return (commutative_tree_code (TREE_CODE (arg0
))
3423 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3424 TREE_OPERAND (arg1
, 1), flags
)
3425 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3426 TREE_OPERAND (arg1
, 0), flags
));
3429 /* If either of the pointer (or reference) expressions we are
3430 dereferencing contain a side effect, these cannot be equal,
3431 but their addresses can be. */
3432 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3433 && (TREE_SIDE_EFFECTS (arg0
)
3434 || TREE_SIDE_EFFECTS (arg1
)))
3437 switch (TREE_CODE (arg0
))
3440 if (!(flags
& OEP_ADDRESS_OF
))
3442 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3443 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3445 /* Verify that the access types are compatible. */
3446 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0
))
3447 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)))
3450 flags
&= ~OEP_ADDRESS_OF
;
3454 /* Require the same offset. */
3455 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3456 TYPE_SIZE (TREE_TYPE (arg1
)),
3457 flags
& ~OEP_ADDRESS_OF
))
3462 case VIEW_CONVERT_EXPR
:
3465 case TARGET_MEM_REF
:
3467 if (!(flags
& OEP_ADDRESS_OF
))
3469 /* Require equal access sizes */
3470 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3471 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3472 || !TYPE_SIZE (TREE_TYPE (arg1
))
3473 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3474 TYPE_SIZE (TREE_TYPE (arg1
)),
3477 /* Verify that access happens in similar types. */
3478 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3480 /* Verify that accesses are TBAA compatible. */
3481 if (!alias_ptr_types_compatible_p
3482 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3483 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3484 || (MR_DEPENDENCE_CLIQUE (arg0
)
3485 != MR_DEPENDENCE_CLIQUE (arg1
))
3486 || (MR_DEPENDENCE_BASE (arg0
)
3487 != MR_DEPENDENCE_BASE (arg1
)))
3489 /* Verify that alignment is compatible. */
3490 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3491 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3494 flags
&= ~OEP_ADDRESS_OF
;
3495 return (OP_SAME (0) && OP_SAME (1)
3496 /* TARGET_MEM_REF require equal extra operands. */
3497 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3498 || (OP_SAME_WITH_NULL (2)
3499 && OP_SAME_WITH_NULL (3)
3500 && OP_SAME_WITH_NULL (4))));
3503 case ARRAY_RANGE_REF
:
3506 flags
&= ~OEP_ADDRESS_OF
;
3507 /* Compare the array index by value if it is constant first as we
3508 may have different types but same value here. */
3509 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3510 TREE_OPERAND (arg1
, 1))
3512 && OP_SAME_WITH_NULL (2)
3513 && OP_SAME_WITH_NULL (3)
3514 /* Compare low bound and element size as with OEP_ADDRESS_OF
3515 we have to account for the offset of the ref. */
3516 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3517 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3518 || (operand_equal_p (array_ref_low_bound
3519 (CONST_CAST_TREE (arg0
)),
3521 (CONST_CAST_TREE (arg1
)), flags
)
3522 && operand_equal_p (array_ref_element_size
3523 (CONST_CAST_TREE (arg0
)),
3524 array_ref_element_size
3525 (CONST_CAST_TREE (arg1
)),
3529 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3530 may be NULL when we're called to compare MEM_EXPRs. */
3531 if (!OP_SAME_WITH_NULL (0))
3534 bool compare_address
= flags
& OEP_ADDRESS_OF
;
3536 /* Most of time we only need to compare FIELD_DECLs for equality.
3537 However when determining address look into actual offsets.
3538 These may match for unions and unshared record types. */
3539 flags
&= ~OEP_ADDRESS_OF
;
3543 && (flags
& OEP_ADDRESS_OF_SAME_FIELD
) == 0)
3545 tree field0
= TREE_OPERAND (arg0
, 1);
3546 tree field1
= TREE_OPERAND (arg1
, 1);
3548 /* Non-FIELD_DECL operands can appear in C++ templates. */
3549 if (TREE_CODE (field0
) != FIELD_DECL
3550 || TREE_CODE (field1
) != FIELD_DECL
3551 || !operand_equal_p (DECL_FIELD_OFFSET (field0
),
3552 DECL_FIELD_OFFSET (field1
), flags
)
3553 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0
),
3554 DECL_FIELD_BIT_OFFSET (field1
),
3562 return OP_SAME_WITH_NULL (2);
3567 flags
&= ~OEP_ADDRESS_OF
;
3568 return OP_SAME (1) && OP_SAME (2);
3574 case tcc_expression
:
3575 switch (TREE_CODE (arg0
))
3578 /* Be sure we pass right ADDRESS_OF flag. */
3579 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3580 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3581 TREE_OPERAND (arg1
, 0),
3582 flags
| OEP_ADDRESS_OF
);
3584 case TRUTH_NOT_EXPR
:
3587 case TRUTH_ANDIF_EXPR
:
3588 case TRUTH_ORIF_EXPR
:
3589 return OP_SAME (0) && OP_SAME (1);
3591 case WIDEN_MULT_PLUS_EXPR
:
3592 case WIDEN_MULT_MINUS_EXPR
:
3595 /* The multiplcation operands are commutative. */
3598 case TRUTH_AND_EXPR
:
3600 case TRUTH_XOR_EXPR
:
3601 if (OP_SAME (0) && OP_SAME (1))
3604 /* Otherwise take into account this is a commutative operation. */
3605 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3606 TREE_OPERAND (arg1
, 1), flags
)
3607 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3608 TREE_OPERAND (arg1
, 0), flags
));
3611 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3613 flags
&= ~OEP_ADDRESS_OF
;
3616 case BIT_INSERT_EXPR
:
3617 /* BIT_INSERT_EXPR has an implict operand as the type precision
3618 of op1. Need to check to make sure they are the same. */
3619 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3620 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3621 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3622 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3628 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3633 case PREDECREMENT_EXPR
:
3634 case PREINCREMENT_EXPR
:
3635 case POSTDECREMENT_EXPR
:
3636 case POSTINCREMENT_EXPR
:
3637 if (flags
& OEP_LEXICOGRAPHIC
)
3638 return OP_SAME (0) && OP_SAME (1);
3641 case CLEANUP_POINT_EXPR
:
3644 if (flags
& OEP_LEXICOGRAPHIC
)
3649 /* Virtual table reference. */
3650 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0
),
3651 OBJ_TYPE_REF_EXPR (arg1
), flags
))
3653 flags
&= ~OEP_ADDRESS_OF
;
3654 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0
))
3655 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1
)))
3657 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0
),
3658 OBJ_TYPE_REF_OBJECT (arg1
), flags
))
3660 if (virtual_method_call_p (arg0
))
3662 if (!virtual_method_call_p (arg1
))
3664 return types_same_for_odr (obj_type_ref_class (arg0
),
3665 obj_type_ref_class (arg1
));
3674 switch (TREE_CODE (arg0
))
3677 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3678 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3679 /* If not both CALL_EXPRs are either internal or normal function
3680 functions, then they are not equal. */
3682 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3684 /* If the CALL_EXPRs call different internal functions, then they
3686 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3691 /* If the CALL_EXPRs call different functions, then they are not
3693 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3698 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3700 unsigned int cef
= call_expr_flags (arg0
);
3701 if (flags
& OEP_PURE_SAME
)
3702 cef
&= ECF_CONST
| ECF_PURE
;
3705 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3709 /* Now see if all the arguments are the same. */
3711 const_call_expr_arg_iterator iter0
, iter1
;
3713 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3714 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3716 a0
= next_const_call_expr_arg (&iter0
),
3717 a1
= next_const_call_expr_arg (&iter1
))
3718 if (! operand_equal_p (a0
, a1
, flags
))
3721 /* If we get here and both argument lists are exhausted
3722 then the CALL_EXPRs are equal. */
3723 return ! (a0
|| a1
);
3729 case tcc_declaration
:
3730 /* Consider __builtin_sqrt equal to sqrt. */
3731 if (TREE_CODE (arg0
) == FUNCTION_DECL
)
3732 return (fndecl_built_in_p (arg0
) && fndecl_built_in_p (arg1
)
3733 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3734 && (DECL_UNCHECKED_FUNCTION_CODE (arg0
)
3735 == DECL_UNCHECKED_FUNCTION_CODE (arg1
)));
3738 && (flags
& OEP_DECL_NAME
)
3739 && (flags
& OEP_LEXICOGRAPHIC
))
3741 /* Consider decls with the same name equal. The caller needs
3742 to make sure they refer to the same entity (such as a function
3743 formal parameter). */
3744 tree a0name
= DECL_NAME (arg0
);
3745 tree a1name
= DECL_NAME (arg1
);
3746 const char *a0ns
= a0name
? IDENTIFIER_POINTER (a0name
) : NULL
;
3747 const char *a1ns
= a1name
? IDENTIFIER_POINTER (a1name
) : NULL
;
3748 return a0ns
&& a1ns
&& strcmp (a0ns
, a1ns
) == 0;
3752 case tcc_exceptional
:
3753 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3755 if (CONSTRUCTOR_NO_CLEARING (arg0
) != CONSTRUCTOR_NO_CLEARING (arg1
))
3758 /* In GIMPLE constructors are used only to build vectors from
3759 elements. Individual elements in the constructor must be
3760 indexed in increasing order and form an initial sequence.
3762 We make no effort to compare nonconstant ones in GENERIC. */
3763 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3764 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3767 /* Be sure that vectors constructed have the same representation.
3768 We only tested element precision and modes to match.
3769 Vectors may be BLKmode and thus also check that the number of
3771 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3772 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3775 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3776 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3777 unsigned int len
= vec_safe_length (v0
);
3779 if (len
!= vec_safe_length (v1
))
3782 for (unsigned int i
= 0; i
< len
; i
++)
3784 constructor_elt
*c0
= &(*v0
)[i
];
3785 constructor_elt
*c1
= &(*v1
)[i
];
3787 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3788 /* In GIMPLE the indexes can be either NULL or matching i.
3789 Double check this so we won't get false
3790 positives for GENERIC. */
3792 && (TREE_CODE (c0
->index
) != INTEGER_CST
3793 || compare_tree_int (c0
->index
, i
)))
3795 && (TREE_CODE (c1
->index
) != INTEGER_CST
3796 || compare_tree_int (c1
->index
, i
))))
3801 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3802 && (flags
& OEP_LEXICOGRAPHIC
))
3804 /* Compare the STATEMENT_LISTs. */
3805 tree_stmt_iterator tsi1
, tsi2
;
3806 tree body1
= CONST_CAST_TREE (arg0
);
3807 tree body2
= CONST_CAST_TREE (arg1
);
3808 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3809 tsi_next (&tsi1
), tsi_next (&tsi2
))
3811 /* The lists don't have the same number of statements. */
3812 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3814 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3816 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3817 flags
& (OEP_LEXICOGRAPHIC
3818 | OEP_NO_HASH_CHECK
)))
3825 switch (TREE_CODE (arg0
))
3828 if (flags
& OEP_LEXICOGRAPHIC
)
3829 return OP_SAME_WITH_NULL (0);
3831 case DEBUG_BEGIN_STMT
:
3832 if (flags
& OEP_LEXICOGRAPHIC
)
3844 #undef OP_SAME_WITH_NULL
3847 /* Generate a hash value for an expression. This can be used iteratively
3848 by passing a previous result as the HSTATE argument. */
3851 operand_compare::hash_operand (const_tree t
, inchash::hash
&hstate
,
3855 enum tree_code code
;
3856 enum tree_code_class tclass
;
3858 if (t
== NULL_TREE
|| t
== error_mark_node
)
3860 hstate
.merge_hash (0);
3864 STRIP_ANY_LOCATION_WRAPPER (t
);
3866 if (!(flags
& OEP_ADDRESS_OF
))
3869 code
= TREE_CODE (t
);
3873 /* Alas, constants aren't shared, so we can't rely on pointer
3876 hstate
.merge_hash (0);
3879 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3880 for (i
= 0; i
< TREE_INT_CST_EXT_NUNITS (t
); i
++)
3881 hstate
.add_hwi (TREE_INT_CST_ELT (t
, i
));
3886 if (!HONOR_SIGNED_ZEROS (t
) && real_zerop (t
))
3889 val2
= real_hash (TREE_REAL_CST_PTR (t
));
3890 hstate
.merge_hash (val2
);
3895 unsigned int val2
= fixed_hash (TREE_FIXED_CST_PTR (t
));
3896 hstate
.merge_hash (val2
);
3900 hstate
.add ((const void *) TREE_STRING_POINTER (t
),
3901 TREE_STRING_LENGTH (t
));
3904 hash_operand (TREE_REALPART (t
), hstate
, flags
);
3905 hash_operand (TREE_IMAGPART (t
), hstate
, flags
);
3909 hstate
.add_int (VECTOR_CST_NPATTERNS (t
));
3910 hstate
.add_int (VECTOR_CST_NELTS_PER_PATTERN (t
));
3911 unsigned int count
= vector_cst_encoded_nelts (t
);
3912 for (unsigned int i
= 0; i
< count
; ++i
)
3913 hash_operand (VECTOR_CST_ENCODED_ELT (t
, i
), hstate
, flags
);
3917 /* We can just compare by pointer. */
3918 hstate
.add_hwi (SSA_NAME_VERSION (t
));
3920 case PLACEHOLDER_EXPR
:
3921 /* The node itself doesn't matter. */
3928 /* A list of expressions, for a CALL_EXPR or as the elements of a
3930 for (; t
; t
= TREE_CHAIN (t
))
3931 hash_operand (TREE_VALUE (t
), hstate
, flags
);
3935 unsigned HOST_WIDE_INT idx
;
3937 flags
&= ~OEP_ADDRESS_OF
;
3938 hstate
.add_int (CONSTRUCTOR_NO_CLEARING (t
));
3939 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), idx
, field
, value
)
3941 /* In GIMPLE the indexes can be either NULL or matching i. */
3942 if (field
== NULL_TREE
)
3943 field
= bitsize_int (idx
);
3944 if (TREE_CODE (field
) == FIELD_DECL
)
3946 hash_operand (DECL_FIELD_OFFSET (field
), hstate
, flags
);
3947 hash_operand (DECL_FIELD_BIT_OFFSET (field
), hstate
, flags
);
3950 hash_operand (field
, hstate
, flags
);
3951 hash_operand (value
, hstate
, flags
);
3955 case STATEMENT_LIST
:
3957 tree_stmt_iterator i
;
3958 for (i
= tsi_start (CONST_CAST_TREE (t
));
3959 !tsi_end_p (i
); tsi_next (&i
))
3960 hash_operand (tsi_stmt (i
), hstate
, flags
);
3964 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
3965 hash_operand (TREE_VEC_ELT (t
, i
), hstate
, flags
);
3967 case IDENTIFIER_NODE
:
3968 hstate
.add_object (IDENTIFIER_HASH_VALUE (t
));
3971 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3972 Otherwise nodes that compare equal according to operand_equal_p might
3973 get different hash codes. However, don't do this for machine specific
3974 or front end builtins, since the function code is overloaded in those
3976 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
3977 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t
)))
3979 t
= builtin_decl_explicit (DECL_FUNCTION_CODE (t
));
3980 code
= TREE_CODE (t
);
3984 if (POLY_INT_CST_P (t
))
3986 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
3987 hstate
.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t
, i
)));
3990 tclass
= TREE_CODE_CLASS (code
);
3992 if (tclass
== tcc_declaration
)
3994 /* DECL's have a unique ID */
3995 hstate
.add_hwi (DECL_UID (t
));
3997 else if (tclass
== tcc_comparison
&& !commutative_tree_code (code
))
3999 /* For comparisons that can be swapped, use the lower
4001 enum tree_code ccode
= swap_tree_comparison (code
);
4004 hstate
.add_object (ccode
);
4005 hash_operand (TREE_OPERAND (t
, ccode
!= code
), hstate
, flags
);
4006 hash_operand (TREE_OPERAND (t
, ccode
== code
), hstate
, flags
);
4008 else if (CONVERT_EXPR_CODE_P (code
))
4010 /* NOP_EXPR and CONVERT_EXPR are considered equal by
4012 enum tree_code ccode
= NOP_EXPR
;
4013 hstate
.add_object (ccode
);
4015 /* Don't hash the type, that can lead to having nodes which
4016 compare equal according to operand_equal_p, but which
4017 have different hash codes. Make sure to include signedness
4018 in the hash computation. */
4019 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
4020 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
4022 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4023 else if (code
== MEM_REF
4024 && (flags
& OEP_ADDRESS_OF
) != 0
4025 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
4026 && DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0))
4027 && integer_zerop (TREE_OPERAND (t
, 1)))
4028 hash_operand (TREE_OPERAND (TREE_OPERAND (t
, 0), 0),
4030 /* Don't ICE on FE specific trees, or their arguments etc.
4031 during operand_equal_p hash verification. */
4032 else if (!IS_EXPR_CODE_CLASS (tclass
))
4033 gcc_assert (flags
& OEP_HASH_CHECK
);
4036 unsigned int sflags
= flags
;
4038 hstate
.add_object (code
);
4043 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
4044 flags
|= OEP_ADDRESS_OF
;
4050 case TARGET_MEM_REF
:
4051 flags
&= ~OEP_ADDRESS_OF
;
4056 if (sflags
& OEP_ADDRESS_OF
)
4058 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
4059 hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t
, 1)),
4060 hstate
, flags
& ~OEP_ADDRESS_OF
);
4061 hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t
, 1)),
4062 hstate
, flags
& ~OEP_ADDRESS_OF
);
4067 case ARRAY_RANGE_REF
:
4069 sflags
&= ~OEP_ADDRESS_OF
;
4073 flags
&= ~OEP_ADDRESS_OF
;
4076 case WIDEN_MULT_PLUS_EXPR
:
4077 case WIDEN_MULT_MINUS_EXPR
:
4079 /* The multiplication operands are commutative. */
4080 inchash::hash one
, two
;
4081 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
4082 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
4083 hstate
.add_commutative (one
, two
);
4084 hash_operand (TREE_OPERAND (t
, 2), two
, flags
);
4089 if (CALL_EXPR_FN (t
) == NULL_TREE
)
4090 hstate
.add_int (CALL_EXPR_IFN (t
));
4094 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4095 Usually different TARGET_EXPRs just should use
4096 different temporaries in their slots. */
4097 hash_operand (TARGET_EXPR_SLOT (t
), hstate
, flags
);
4101 /* Virtual table reference. */
4102 inchash::add_expr (OBJ_TYPE_REF_EXPR (t
), hstate
, flags
);
4103 flags
&= ~OEP_ADDRESS_OF
;
4104 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t
), hstate
, flags
);
4105 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t
), hstate
, flags
);
4106 if (!virtual_method_call_p (t
))
4108 if (tree c
= obj_type_ref_class (t
))
4110 c
= TYPE_NAME (TYPE_MAIN_VARIANT (c
));
4111 /* We compute mangled names only when free_lang_data is run.
4112 In that case we can hash precisely. */
4113 if (TREE_CODE (c
) == TYPE_DECL
4114 && DECL_ASSEMBLER_NAME_SET_P (c
))
4116 (IDENTIFIER_HASH_VALUE
4117 (DECL_ASSEMBLER_NAME (c
)));
4124 /* Don't hash the type, that can lead to having nodes which
4125 compare equal according to operand_equal_p, but which
4126 have different hash codes. */
4127 if (code
== NON_LVALUE_EXPR
)
4129 /* Make sure to include signness in the hash computation. */
4130 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
4131 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
4134 else if (commutative_tree_code (code
))
4136 /* It's a commutative expression. We want to hash it the same
4137 however it appears. We do this by first hashing both operands
4138 and then rehashing based on the order of their independent
4140 inchash::hash one
, two
;
4141 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
4142 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
4143 hstate
.add_commutative (one
, two
);
4146 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
4147 hash_operand (TREE_OPERAND (t
, i
), hstate
,
4148 i
== 0 ? flags
: sflags
);
4155 operand_compare::verify_hash_value (const_tree arg0
, const_tree arg1
,
4156 unsigned int flags
, bool *ret
)
4158 /* When checking and unless comparing DECL names, verify that if
4159 the outermost operand_equal_p call returns non-zero then ARG0
4160 and ARG1 have the same hash value. */
4161 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
4163 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
4165 if (arg0
!= arg1
&& !(flags
& OEP_DECL_NAME
))
4167 inchash::hash
hstate0 (0), hstate1 (0);
4168 hash_operand (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
4169 hash_operand (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
4170 hashval_t h0
= hstate0
.end ();
4171 hashval_t h1
= hstate1
.end ();
4172 gcc_assert (h0
== h1
);
4186 static operand_compare default_compare_instance
;
4188 /* Conveinece wrapper around operand_compare class because usually we do
4189 not need to play with the valueizer. */
4192 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
4194 return default_compare_instance
.operand_equal_p (arg0
, arg1
, flags
);
4200 /* Generate a hash value for an expression. This can be used iteratively
4201 by passing a previous result as the HSTATE argument.
4203 This function is intended to produce the same hash for expressions which
4204 would compare equal using operand_equal_p. */
4206 add_expr (const_tree t
, inchash::hash
&hstate
, unsigned int flags
)
4208 default_compare_instance
.hash_operand (t
, hstate
, flags
);
4213 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4214 with a different signedness or a narrower precision. */
4217 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
4219 if (operand_equal_p (arg0
, arg1
, 0))
4222 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
4223 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
4226 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4227 and see if the inner values are the same. This removes any
4228 signedness comparison, which doesn't matter here. */
4233 if (operand_equal_p (op0
, op1
, 0))
4236 /* Discard a single widening conversion from ARG1 and see if the inner
4237 value is the same as ARG0. */
4238 if (CONVERT_EXPR_P (arg1
)
4239 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4240 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4241 < TYPE_PRECISION (TREE_TYPE (arg1
))
4242 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
4248 /* See if ARG is an expression that is either a comparison or is performing
4249 arithmetic on comparisons. The comparisons must only be comparing
4250 two different values, which will be stored in *CVAL1 and *CVAL2; if
4251 they are nonzero it means that some operands have already been found.
4252 No variables may be used anywhere else in the expression except in the
4255 If this is true, return 1. Otherwise, return zero. */
4258 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
4260 enum tree_code code
= TREE_CODE (arg
);
4261 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4263 /* We can handle some of the tcc_expression cases here. */
4264 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4266 else if (tclass
== tcc_expression
4267 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
4268 || code
== COMPOUND_EXPR
))
4269 tclass
= tcc_binary
;
4274 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
4277 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4278 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
4283 case tcc_expression
:
4284 if (code
== COND_EXPR
)
4285 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4286 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
4287 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
4290 case tcc_comparison
:
4291 /* First see if we can handle the first operand, then the second. For
4292 the second operand, we know *CVAL1 can't be zero. It must be that
4293 one side of the comparison is each of the values; test for the
4294 case where this isn't true by failing if the two operands
4297 if (operand_equal_p (TREE_OPERAND (arg
, 0),
4298 TREE_OPERAND (arg
, 1), 0))
4302 *cval1
= TREE_OPERAND (arg
, 0);
4303 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
4305 else if (*cval2
== 0)
4306 *cval2
= TREE_OPERAND (arg
, 0);
4307 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
4312 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
4314 else if (*cval2
== 0)
4315 *cval2
= TREE_OPERAND (arg
, 1);
4316 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
4328 /* ARG is a tree that is known to contain just arithmetic operations and
4329 comparisons. Evaluate the operations in the tree substituting NEW0 for
4330 any occurrence of OLD0 as an operand of a comparison and likewise for
4334 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
4335 tree old1
, tree new1
)
4337 tree type
= TREE_TYPE (arg
);
4338 enum tree_code code
= TREE_CODE (arg
);
4339 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4341 /* We can handle some of the tcc_expression cases here. */
4342 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4344 else if (tclass
== tcc_expression
4345 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
4346 tclass
= tcc_binary
;
4351 return fold_build1_loc (loc
, code
, type
,
4352 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4353 old0
, new0
, old1
, new1
));
4356 return fold_build2_loc (loc
, code
, type
,
4357 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4358 old0
, new0
, old1
, new1
),
4359 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4360 old0
, new0
, old1
, new1
));
4362 case tcc_expression
:
4366 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
4370 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
4374 return fold_build3_loc (loc
, code
, type
,
4375 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4376 old0
, new0
, old1
, new1
),
4377 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4378 old0
, new0
, old1
, new1
),
4379 eval_subst (loc
, TREE_OPERAND (arg
, 2),
4380 old0
, new0
, old1
, new1
));
4384 /* Fall through - ??? */
4386 case tcc_comparison
:
4388 tree arg0
= TREE_OPERAND (arg
, 0);
4389 tree arg1
= TREE_OPERAND (arg
, 1);
4391 /* We need to check both for exact equality and tree equality. The
4392 former will be true if the operand has a side-effect. In that
4393 case, we know the operand occurred exactly once. */
4395 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
4397 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
4400 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
4402 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
4405 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
4413 /* Return a tree for the case when the result of an expression is RESULT
4414 converted to TYPE and OMITTED was previously an operand of the expression
4415 but is now not needed (e.g., we folded OMITTED * 0).
4417 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4418 the conversion of RESULT to TYPE. */
4421 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
4423 tree t
= fold_convert_loc (loc
, type
, result
);
4425 /* If the resulting operand is an empty statement, just return the omitted
4426 statement casted to void. */
4427 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
4428 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
4429 fold_ignored_result (omitted
));
4431 if (TREE_SIDE_EFFECTS (omitted
))
4432 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4433 fold_ignored_result (omitted
), t
);
4435 return non_lvalue_loc (loc
, t
);
4438 /* Return a tree for the case when the result of an expression is RESULT
4439 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4440 of the expression but are now not needed.
4442 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4443 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4444 evaluated before OMITTED2. Otherwise, if neither has side effects,
4445 just do the conversion of RESULT to TYPE. */
4448 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
4449 tree omitted1
, tree omitted2
)
4451 tree t
= fold_convert_loc (loc
, type
, result
);
4453 if (TREE_SIDE_EFFECTS (omitted2
))
4454 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
4455 if (TREE_SIDE_EFFECTS (omitted1
))
4456 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
4458 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
4462 /* Return a simplified tree node for the truth-negation of ARG. This
4463 never alters ARG itself. We assume that ARG is an operation that
4464 returns a truth value (0 or 1).
4466 FIXME: one would think we would fold the result, but it causes
4467 problems with the dominator optimizer. */
4470 fold_truth_not_expr (location_t loc
, tree arg
)
4472 tree type
= TREE_TYPE (arg
);
4473 enum tree_code code
= TREE_CODE (arg
);
4474 location_t loc1
, loc2
;
4476 /* If this is a comparison, we can simply invert it, except for
4477 floating-point non-equality comparisons, in which case we just
4478 enclose a TRUTH_NOT_EXPR around what we have. */
4480 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4482 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4483 if (FLOAT_TYPE_P (op_type
)
4484 && flag_trapping_math
4485 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
4486 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
4489 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
4490 if (code
== ERROR_MARK
)
4493 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
4494 TREE_OPERAND (arg
, 1));
4495 copy_warning (ret
, arg
);
4502 return constant_boolean_node (integer_zerop (arg
), type
);
4504 case TRUTH_AND_EXPR
:
4505 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4506 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4507 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
4508 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4509 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4512 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4513 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4514 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
4515 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4516 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4518 case TRUTH_XOR_EXPR
:
4519 /* Here we can invert either operand. We invert the first operand
4520 unless the second operand is a TRUTH_NOT_EXPR in which case our
4521 result is the XOR of the first operand with the inside of the
4522 negation of the second operand. */
4524 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
4525 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
4526 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
4528 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
4529 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
4530 TREE_OPERAND (arg
, 1));
4532 case TRUTH_ANDIF_EXPR
:
4533 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4534 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4535 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
4536 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4537 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4539 case TRUTH_ORIF_EXPR
:
4540 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4541 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4542 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
4543 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4544 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4546 case TRUTH_NOT_EXPR
:
4547 return TREE_OPERAND (arg
, 0);
4551 tree arg1
= TREE_OPERAND (arg
, 1);
4552 tree arg2
= TREE_OPERAND (arg
, 2);
4554 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4555 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
4557 /* A COND_EXPR may have a throw as one operand, which
4558 then has void type. Just leave void operands
4560 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
4561 VOID_TYPE_P (TREE_TYPE (arg1
))
4562 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
4563 VOID_TYPE_P (TREE_TYPE (arg2
))
4564 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
4568 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4569 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4570 TREE_OPERAND (arg
, 0),
4571 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
4573 case NON_LVALUE_EXPR
:
4574 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4575 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
4578 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4579 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4584 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4585 return build1_loc (loc
, TREE_CODE (arg
), type
,
4586 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4589 if (!integer_onep (TREE_OPERAND (arg
, 1)))
4591 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
4594 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4596 case CLEANUP_POINT_EXPR
:
4597 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4598 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
4599 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4606 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4607 assume that ARG is an operation that returns a truth value (0 or 1
4608 for scalars, 0 or -1 for vectors). Return the folded expression if
4609 folding is successful. Otherwise, return NULL_TREE. */
4612 fold_invert_truthvalue (location_t loc
, tree arg
)
4614 tree type
= TREE_TYPE (arg
);
4615 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
4621 /* Return a simplified tree node for the truth-negation of ARG. This
4622 never alters ARG itself. We assume that ARG is an operation that
4623 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4626 invert_truthvalue_loc (location_t loc
, tree arg
)
4628 if (TREE_CODE (arg
) == ERROR_MARK
)
4631 tree type
= TREE_TYPE (arg
);
4632 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
4638 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4639 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4640 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4641 is the original memory reference used to preserve the alias set of
4645 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
4646 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
4647 int unsignedp
, int reversep
)
4649 tree result
, bftype
;
4651 /* Attempt not to lose the access path if possible. */
4652 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
4654 tree ninner
= TREE_OPERAND (orig_inner
, 0);
4656 poly_int64 nbitsize
, nbitpos
;
4658 int nunsignedp
, nreversep
, nvolatilep
= 0;
4659 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
4660 &noffset
, &nmode
, &nunsignedp
,
4661 &nreversep
, &nvolatilep
);
4663 && noffset
== NULL_TREE
4664 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
4674 alias_set_type iset
= get_alias_set (orig_inner
);
4675 if (iset
== 0 && get_alias_set (inner
) != iset
)
4676 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
4677 build_fold_addr_expr (inner
),
4678 build_int_cst (ptr_type_node
, 0));
4680 if (known_eq (bitpos
, 0) && !reversep
)
4682 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
4683 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
4684 || POINTER_TYPE_P (TREE_TYPE (inner
)))
4685 && tree_fits_shwi_p (size
)
4686 && tree_to_shwi (size
) == bitsize
)
4687 return fold_convert_loc (loc
, type
, inner
);
4691 if (TYPE_PRECISION (bftype
) != bitsize
4692 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
4693 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4695 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4696 bitsize_int (bitsize
), bitsize_int (bitpos
));
4697 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4700 result
= fold_convert_loc (loc
, type
, result
);
4705 /* Optimize a bit-field compare.
4707 There are two cases: First is a compare against a constant and the
4708 second is a comparison of two items where the fields are at the same
4709 bit position relative to the start of a chunk (byte, halfword, word)
4710 large enough to contain it. In these cases we can avoid the shift
4711 implicit in bitfield extractions.
4713 For constants, we emit a compare of the shifted constant with the
4714 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4715 compared. For two fields at the same position, we do the ANDs with the
4716 similar mask and compare the result of the ANDs.
4718 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4719 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4720 are the left and right operands of the comparison, respectively.
4722 If the optimization described above can be done, we return the resulting
4723 tree. Otherwise we return zero. */
4726 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4727 tree compare_type
, tree lhs
, tree rhs
)
4729 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4730 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4731 tree type
= TREE_TYPE (lhs
);
4733 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4734 machine_mode lmode
, rmode
;
4735 scalar_int_mode nmode
;
4736 int lunsignedp
, runsignedp
;
4737 int lreversep
, rreversep
;
4738 int lvolatilep
= 0, rvolatilep
= 0;
4739 tree linner
, rinner
= NULL_TREE
;
4743 /* Get all the information about the extractions being done. If the bit size
4744 is the same as the size of the underlying object, we aren't doing an
4745 extraction at all and so can do nothing. We also don't want to
4746 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4747 then will no longer be able to replace it. */
4748 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4749 &lunsignedp
, &lreversep
, &lvolatilep
);
4751 || !known_size_p (plbitsize
)
4752 || !plbitsize
.is_constant (&lbitsize
)
4753 || !plbitpos
.is_constant (&lbitpos
)
4754 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4756 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4761 rreversep
= lreversep
;
4764 /* If this is not a constant, we can only do something if bit positions,
4765 sizes, signedness and storage order are the same. */
4767 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4768 &runsignedp
, &rreversep
, &rvolatilep
);
4771 || maybe_ne (lbitpos
, rbitpos
)
4772 || maybe_ne (lbitsize
, rbitsize
)
4773 || lunsignedp
!= runsignedp
4774 || lreversep
!= rreversep
4776 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4781 /* Honor the C++ memory model and mimic what RTL expansion does. */
4782 poly_uint64 bitstart
= 0;
4783 poly_uint64 bitend
= 0;
4784 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4786 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4787 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4791 /* See if we can find a mode to refer to this field. We should be able to,
4792 but fail if we can't. */
4793 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4794 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4795 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4796 TYPE_ALIGN (TREE_TYPE (rinner
))),
4797 BITS_PER_WORD
, false, &nmode
))
4800 /* Set signed and unsigned types of the precision of this mode for the
4802 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4804 /* Compute the bit position and size for the new reference and our offset
4805 within it. If the new reference is the same size as the original, we
4806 won't optimize anything, so return zero. */
4807 nbitsize
= GET_MODE_BITSIZE (nmode
);
4808 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4810 if (nbitsize
== lbitsize
)
4813 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4814 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4816 /* Make the mask to be used against the extracted field. */
4817 mask
= build_int_cst_type (unsigned_type
, -1);
4818 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4819 mask
= const_binop (RSHIFT_EXPR
, mask
,
4820 size_int (nbitsize
- lbitsize
- lbitpos
));
4827 /* If not comparing with constant, just rework the comparison
4829 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4830 nbitsize
, nbitpos
, 1, lreversep
);
4831 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4832 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4833 nbitsize
, nbitpos
, 1, rreversep
);
4834 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4835 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4838 /* Otherwise, we are handling the constant case. See if the constant is too
4839 big for the field. Warn and return a tree for 0 (false) if so. We do
4840 this not only for its own sake, but to avoid having to test for this
4841 error case below. If we didn't, we might generate wrong code.
4843 For unsigned fields, the constant shifted right by the field length should
4844 be all zero. For signed fields, the high-order bits should agree with
4849 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4851 warning (0, "comparison is always %d due to width of bit-field",
4853 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4858 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4859 if (tem
!= 0 && tem
!= -1)
4861 warning (0, "comparison is always %d due to width of bit-field",
4863 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4870 /* Single-bit compares should always be against zero. */
4871 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4873 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4874 rhs
= build_int_cst (type
, 0);
4877 /* Make a new bitfield reference, shift the constant over the
4878 appropriate number of bits and mask it with the computed mask
4879 (in case this was a signed field). If we changed it, make a new one. */
4880 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4881 nbitsize
, nbitpos
, 1, lreversep
);
4883 rhs
= const_binop (BIT_AND_EXPR
,
4884 const_binop (LSHIFT_EXPR
,
4885 fold_convert_loc (loc
, unsigned_type
, rhs
),
4886 size_int (lbitpos
)),
4889 lhs
= build2_loc (loc
, code
, compare_type
,
4890 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4894 /* Subroutine for fold_truth_andor_1: decode a field reference.
4896 If EXP is a comparison reference, we return the innermost reference.
4898 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4899 set to the starting bit number.
4901 If the innermost field can be completely contained in a mode-sized
4902 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4904 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4905 otherwise it is not changed.
4907 *PUNSIGNEDP is set to the signedness of the field.
4909 *PREVERSEP is set to the storage order of the field.
4911 *PMASK is set to the mask used. This is either contained in a
4912 BIT_AND_EXPR or derived from the width of the field.
4914 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4916 Return 0 if this is not a component reference or is one that we can't
4917 do anything with. */
4920 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4921 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4922 int *punsignedp
, int *preversep
, int *pvolatilep
,
4923 tree
*pmask
, tree
*pand_mask
)
4926 tree outer_type
= 0;
4928 tree mask
, inner
, offset
;
4930 unsigned int precision
;
4932 /* All the optimizations using this function assume integer fields.
4933 There are problems with FP fields since the type_for_size call
4934 below can fail for, e.g., XFmode. */
4935 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4938 /* We are interested in the bare arrangement of bits, so strip everything
4939 that doesn't affect the machine mode. However, record the type of the
4940 outermost expression if it may matter below. */
4941 if (CONVERT_EXPR_P (exp
)
4942 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4943 outer_type
= TREE_TYPE (exp
);
4946 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4948 and_mask
= TREE_OPERAND (exp
, 1);
4949 exp
= TREE_OPERAND (exp
, 0);
4950 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4951 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4955 poly_int64 poly_bitsize
, poly_bitpos
;
4956 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4957 pmode
, punsignedp
, preversep
, pvolatilep
);
4958 if ((inner
== exp
&& and_mask
== 0)
4959 || !poly_bitsize
.is_constant (pbitsize
)
4960 || !poly_bitpos
.is_constant (pbitpos
)
4963 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4964 /* Reject out-of-bound accesses (PR79731). */
4965 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4966 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4967 *pbitpos
+ *pbitsize
) < 0))
4970 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4971 if (unsigned_type
== NULL_TREE
)
4976 /* If the number of bits in the reference is the same as the bitsize of
4977 the outer type, then the outer type gives the signedness. Otherwise
4978 (in case of a small bitfield) the signedness is unchanged. */
4979 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4980 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4982 /* Compute the mask to access the bitfield. */
4983 precision
= TYPE_PRECISION (unsigned_type
);
4985 mask
= build_int_cst_type (unsigned_type
, -1);
4987 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4988 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4990 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4992 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4993 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4996 *pand_mask
= and_mask
;
5000 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
5001 bit positions and MASK is SIGNED. */
5004 all_ones_mask_p (const_tree mask
, unsigned int size
)
5006 tree type
= TREE_TYPE (mask
);
5007 unsigned int precision
= TYPE_PRECISION (type
);
5009 /* If this function returns true when the type of the mask is
5010 UNSIGNED, then there will be errors. In particular see
5011 gcc.c-torture/execute/990326-1.c. There does not appear to be
5012 any documentation paper trail as to why this is so. But the pre
5013 wide-int worked with that restriction and it has been preserved
5015 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
5018 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
5021 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5022 represents the sign bit of EXP's type. If EXP represents a sign
5023 or zero extension, also test VAL against the unextended type.
5024 The return value is the (sub)expression whose sign bit is VAL,
5025 or NULL_TREE otherwise. */
5028 sign_bit_p (tree exp
, const_tree val
)
5033 /* Tree EXP must have an integral type. */
5034 t
= TREE_TYPE (exp
);
5035 if (! INTEGRAL_TYPE_P (t
))
5038 /* Tree VAL must be an integer constant. */
5039 if (TREE_CODE (val
) != INTEGER_CST
5040 || TREE_OVERFLOW (val
))
5043 width
= TYPE_PRECISION (t
);
5044 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
5047 /* Handle extension from a narrower type. */
5048 if (TREE_CODE (exp
) == NOP_EXPR
5049 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
5050 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
5055 /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5056 operand is simple enough to be evaluated unconditionally. */
5059 simple_operand_p (const_tree exp
)
5061 /* Strip any conversions that don't change the machine mode. */
5064 return (CONSTANT_CLASS_P (exp
)
5065 || TREE_CODE (exp
) == SSA_NAME
5067 && ! TREE_ADDRESSABLE (exp
)
5068 && ! TREE_THIS_VOLATILE (exp
)
5069 && ! DECL_NONLOCAL (exp
)
5070 /* Don't regard global variables as simple. They may be
5071 allocated in ways unknown to the compiler (shared memory,
5072 #pragma weak, etc). */
5073 && ! TREE_PUBLIC (exp
)
5074 && ! DECL_EXTERNAL (exp
)
5075 /* Weakrefs are not safe to be read, since they can be NULL.
5076 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5077 have DECL_WEAK flag set. */
5078 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
5079 /* Loading a static variable is unduly expensive, but global
5080 registers aren't expensive. */
5081 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
5084 /* Determine if an operand is simple enough to be evaluated unconditionally.
5085 In addition to simple_operand_p, we assume that comparisons, conversions,
5086 and logic-not operations are simple, if their operands are simple, too. */
5089 simple_condition_p (tree exp
)
5091 enum tree_code code
;
5093 if (TREE_SIDE_EFFECTS (exp
) || generic_expr_could_trap_p (exp
))
5096 while (CONVERT_EXPR_P (exp
))
5097 exp
= TREE_OPERAND (exp
, 0);
5099 code
= TREE_CODE (exp
);
5101 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
5102 return (simple_operand_p (TREE_OPERAND (exp
, 0))
5103 && simple_operand_p (TREE_OPERAND (exp
, 1)));
5105 if (code
== TRUTH_NOT_EXPR
)
5106 return simple_condition_p (TREE_OPERAND (exp
, 0));
5108 return simple_operand_p (exp
);
5112 /* The following functions are subroutines to fold_range_test and allow it to
5113 try to change a logical combination of comparisons into a range test.
5116 X == 2 || X == 3 || X == 4 || X == 5
5120 (unsigned) (X - 2) <= 3
5122 We describe each set of comparisons as being either inside or outside
5123 a range, using a variable named like IN_P, and then describe the
5124 range with a lower and upper bound. If one of the bounds is omitted,
5125 it represents either the highest or lowest value of the type.
5127 In the comments below, we represent a range by two numbers in brackets
5128 preceded by a "+" to designate being inside that range, or a "-" to
5129 designate being outside that range, so the condition can be inverted by
5130 flipping the prefix. An omitted bound is represented by a "-". For
5131 example, "- [-, 10]" means being outside the range starting at the lowest
5132 possible value and ending at 10, in other words, being greater than 10.
5133 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5136 We set up things so that the missing bounds are handled in a consistent
5137 manner so neither a missing bound nor "true" and "false" need to be
5138 handled using a special case. */
5140 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5141 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5142 and UPPER1_P are nonzero if the respective argument is an upper bound
5143 and zero for a lower. TYPE, if nonzero, is the type of the result; it
5144 must be specified for a comparison. ARG1 will be converted to ARG0's
5145 type if both are specified. */
5148 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
5149 tree arg1
, int upper1_p
)
5155 /* If neither arg represents infinity, do the normal operation.
5156 Else, if not a comparison, return infinity. Else handle the special
5157 comparison rules. Note that most of the cases below won't occur, but
5158 are handled for consistency. */
5160 if (arg0
!= 0 && arg1
!= 0)
5162 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
5163 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
5165 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
5168 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5171 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5172 for neither. In real maths, we cannot assume open ended ranges are
5173 the same. But, this is computer arithmetic, where numbers are finite.
5174 We can therefore make the transformation of any unbounded range with
5175 the value Z, Z being greater than any representable number. This permits
5176 us to treat unbounded ranges as equal. */
5177 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
5178 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
5182 result
= sgn0
== sgn1
;
5185 result
= sgn0
!= sgn1
;
5188 result
= sgn0
< sgn1
;
5191 result
= sgn0
<= sgn1
;
5194 result
= sgn0
> sgn1
;
5197 result
= sgn0
>= sgn1
;
5203 return constant_boolean_node (result
, type
);
5206 /* Helper routine for make_range. Perform one step for it, return
5207 new expression if the loop should continue or NULL_TREE if it should
5211 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
5212 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
5213 bool *strict_overflow_p
)
5215 tree arg0_type
= TREE_TYPE (arg0
);
5216 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
5217 int in_p
= *p_in_p
, n_in_p
;
5221 case TRUTH_NOT_EXPR
:
5222 /* We can only do something if the range is testing for zero. */
5223 if (low
== NULL_TREE
|| high
== NULL_TREE
5224 || ! integer_zerop (low
) || ! integer_zerop (high
))
5229 case EQ_EXPR
: case NE_EXPR
:
5230 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
5231 /* We can only do something if the range is testing for zero
5232 and if the second operand is an integer constant. Note that
5233 saying something is "in" the range we make is done by
5234 complementing IN_P since it will set in the initial case of
5235 being not equal to zero; "out" is leaving it alone. */
5236 if (low
== NULL_TREE
|| high
== NULL_TREE
5237 || ! integer_zerop (low
) || ! integer_zerop (high
)
5238 || TREE_CODE (arg1
) != INTEGER_CST
)
5243 case NE_EXPR
: /* - [c, c] */
5246 case EQ_EXPR
: /* + [c, c] */
5247 in_p
= ! in_p
, low
= high
= arg1
;
5249 case GT_EXPR
: /* - [-, c] */
5250 low
= 0, high
= arg1
;
5252 case GE_EXPR
: /* + [c, -] */
5253 in_p
= ! in_p
, low
= arg1
, high
= 0;
5255 case LT_EXPR
: /* - [c, -] */
5256 low
= arg1
, high
= 0;
5258 case LE_EXPR
: /* + [-, c] */
5259 in_p
= ! in_p
, low
= 0, high
= arg1
;
5265 /* If this is an unsigned comparison, we also know that EXP is
5266 greater than or equal to zero. We base the range tests we make
5267 on that fact, so we record it here so we can parse existing
5268 range tests. We test arg0_type since often the return type
5269 of, e.g. EQ_EXPR, is boolean. */
5270 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
5272 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
5274 build_int_cst (arg0_type
, 0),
5278 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
5280 /* If the high bound is missing, but we have a nonzero low
5281 bound, reverse the range so it goes from zero to the low bound
5283 if (high
== 0 && low
&& ! integer_zerop (low
))
5286 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
5287 build_int_cst (TREE_TYPE (low
), 1), 0);
5288 low
= build_int_cst (arg0_type
, 0);
5298 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5299 low and high are non-NULL, then normalize will DTRT. */
5300 if (!TYPE_UNSIGNED (arg0_type
)
5301 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5303 if (low
== NULL_TREE
)
5304 low
= TYPE_MIN_VALUE (arg0_type
);
5305 if (high
== NULL_TREE
)
5306 high
= TYPE_MAX_VALUE (arg0_type
);
5309 /* (-x) IN [a,b] -> x in [-b, -a] */
5310 n_low
= range_binop (MINUS_EXPR
, exp_type
,
5311 build_int_cst (exp_type
, 0),
5313 n_high
= range_binop (MINUS_EXPR
, exp_type
,
5314 build_int_cst (exp_type
, 0),
5316 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
5322 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
5323 build_int_cst (exp_type
, 1));
5327 if (TREE_CODE (arg1
) != INTEGER_CST
)
5330 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5331 move a constant to the other side. */
5332 if (!TYPE_UNSIGNED (arg0_type
)
5333 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5336 /* If EXP is signed, any overflow in the computation is undefined,
5337 so we don't worry about it so long as our computations on
5338 the bounds don't overflow. For unsigned, overflow is defined
5339 and this is exactly the right thing. */
5340 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5341 arg0_type
, low
, 0, arg1
, 0);
5342 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5343 arg0_type
, high
, 1, arg1
, 0);
5344 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
5345 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
5348 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5349 *strict_overflow_p
= true;
5352 /* Check for an unsigned range which has wrapped around the maximum
5353 value thus making n_high < n_low, and normalize it. */
5354 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
5356 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
5357 build_int_cst (TREE_TYPE (n_high
), 1), 0);
5358 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
5359 build_int_cst (TREE_TYPE (n_low
), 1), 0);
5361 /* If the range is of the form +/- [ x+1, x ], we won't
5362 be able to normalize it. But then, it represents the
5363 whole range or the empty set, so make it
5365 if (tree_int_cst_equal (n_low
, low
)
5366 && tree_int_cst_equal (n_high
, high
))
5372 low
= n_low
, high
= n_high
;
5380 case NON_LVALUE_EXPR
:
5381 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
5384 if (! INTEGRAL_TYPE_P (arg0_type
)
5385 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
5386 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
5389 n_low
= low
, n_high
= high
;
5392 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
5395 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
5397 /* If we're converting arg0 from an unsigned type, to exp,
5398 a signed type, we will be doing the comparison as unsigned.
5399 The tests above have already verified that LOW and HIGH
5402 So we have to ensure that we will handle large unsigned
5403 values the same way that the current signed bounds treat
5406 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
5410 /* For fixed-point modes, we need to pass the saturating flag
5411 as the 2nd parameter. */
5412 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
5414 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
5415 TYPE_SATURATING (arg0_type
));
5416 else if (TREE_CODE (arg0_type
) == BITINT_TYPE
)
5417 equiv_type
= arg0_type
;
5420 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
5422 /* A range without an upper bound is, naturally, unbounded.
5423 Since convert would have cropped a very large value, use
5424 the max value for the destination type. */
5426 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
5427 : TYPE_MAX_VALUE (arg0_type
);
5429 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
5430 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
5431 fold_convert_loc (loc
, arg0_type
,
5433 build_int_cst (arg0_type
, 1));
5435 /* If the low bound is specified, "and" the range with the
5436 range for which the original unsigned value will be
5440 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
5441 1, fold_convert_loc (loc
, arg0_type
,
5446 in_p
= (n_in_p
== in_p
);
5450 /* Otherwise, "or" the range with the range of the input
5451 that will be interpreted as negative. */
5452 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
5453 1, fold_convert_loc (loc
, arg0_type
,
5458 in_p
= (in_p
!= n_in_p
);
5462 /* Otherwise, if we are converting arg0 from signed type, to exp,
5463 an unsigned type, we will do the comparison as signed. If
5464 high is non-NULL, we punt above if it doesn't fit in the signed
5465 type, so if we get through here, +[-, high] or +[low, high] are
5466 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5467 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5468 high is not, the +[low, -] range is equivalent to union of
5469 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5470 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5471 low being 0, which should be treated as [-, -]. */
5472 else if (TYPE_UNSIGNED (exp_type
)
5473 && !TYPE_UNSIGNED (arg0_type
)
5477 if (integer_zerop (low
))
5481 n_high
= fold_build2_loc (loc
, PLUS_EXPR
, arg0_type
,
5482 n_low
, build_int_cst (arg0_type
, -1));
5483 n_low
= build_zero_cst (arg0_type
);
5498 /* Given EXP, a logical expression, set the range it is testing into
5499 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5500 actually being tested. *PLOW and *PHIGH will be made of the same
5501 type as the returned expression. If EXP is not a comparison, we
5502 will most likely not be returning a useful value and range. Set
5503 *STRICT_OVERFLOW_P to true if the return value is only valid
5504 because signed overflow is undefined; otherwise, do not change
5505 *STRICT_OVERFLOW_P. */
5508 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
5509 bool *strict_overflow_p
)
5511 enum tree_code code
;
5512 tree arg0
, arg1
= NULL_TREE
;
5513 tree exp_type
, nexp
;
5516 location_t loc
= EXPR_LOCATION (exp
);
5518 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5519 and see if we can refine the range. Some of the cases below may not
5520 happen, but it doesn't seem worth worrying about this. We "continue"
5521 the outer loop when we've changed something; otherwise we "break"
5522 the switch, which will "break" the while. */
5525 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
5529 code
= TREE_CODE (exp
);
5530 exp_type
= TREE_TYPE (exp
);
5533 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
5535 if (TREE_OPERAND_LENGTH (exp
) > 0)
5536 arg0
= TREE_OPERAND (exp
, 0);
5537 if (TREE_CODE_CLASS (code
) == tcc_binary
5538 || TREE_CODE_CLASS (code
) == tcc_comparison
5539 || (TREE_CODE_CLASS (code
) == tcc_expression
5540 && TREE_OPERAND_LENGTH (exp
) > 1))
5541 arg1
= TREE_OPERAND (exp
, 1);
5543 if (arg0
== NULL_TREE
)
5546 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
5547 &high
, &in_p
, strict_overflow_p
);
5548 if (nexp
== NULL_TREE
)
5553 /* If EXP is a constant, we can evaluate whether this is true or false. */
5554 if (TREE_CODE (exp
) == INTEGER_CST
)
5556 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
5558 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5564 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5568 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5569 a bitwise check i.e. when
5570 LOW == 0xXX...X00...0
5571 HIGH == 0xXX...X11...1
5572 Return corresponding mask in MASK and stem in VALUE. */
5575 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
5578 if (TREE_CODE (low
) != INTEGER_CST
5579 || TREE_CODE (high
) != INTEGER_CST
)
5582 unsigned prec
= TYPE_PRECISION (type
);
5583 wide_int lo
= wi::to_wide (low
, prec
);
5584 wide_int hi
= wi::to_wide (high
, prec
);
5586 wide_int end_mask
= lo
^ hi
;
5587 if ((end_mask
& (end_mask
+ 1)) != 0
5588 || (lo
& end_mask
) != 0)
5591 wide_int stem_mask
= ~end_mask
;
5592 wide_int stem
= lo
& stem_mask
;
5593 if (stem
!= (hi
& stem_mask
))
5596 *mask
= wide_int_to_tree (type
, stem_mask
);
5597 *value
= wide_int_to_tree (type
, stem
);
5602 /* Helper routine for build_range_check and match.pd. Return the type to
5603 perform the check or NULL if it shouldn't be optimized. */
5606 range_check_type (tree etype
)
5608 /* First make sure that arithmetics in this type is valid, then make sure
5609 that it wraps around. */
5610 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
5611 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
), 1);
5613 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_UNSIGNED (etype
))
5615 tree utype
, minv
, maxv
;
5617 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5618 for the type in question, as we rely on this here. */
5619 utype
= unsigned_type_for (etype
);
5620 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
5621 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
5622 build_int_cst (TREE_TYPE (maxv
), 1), 1);
5623 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
5625 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
5631 else if (POINTER_TYPE_P (etype
)
5632 || TREE_CODE (etype
) == OFFSET_TYPE
5633 /* Right now all BITINT_TYPEs satisfy
5634 (unsigned) max + 1 == (unsigned) min, so no need to verify
5635 that like for INTEGER_TYPEs. */
5636 || TREE_CODE (etype
) == BITINT_TYPE
)
5637 etype
= unsigned_type_for (etype
);
5641 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5642 type, TYPE, return an expression to test if EXP is in (or out of, depending
5643 on IN_P) the range. Return 0 if the test couldn't be created. */
5646 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
5647 tree low
, tree high
)
5649 tree etype
= TREE_TYPE (exp
), mask
, value
;
5651 /* Disable this optimization for function pointer expressions
5652 on targets that require function pointer canonicalization. */
5653 if (targetm
.have_canonicalize_funcptr_for_compare ()
5654 && POINTER_TYPE_P (etype
)
5655 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype
)))
5660 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
5662 return invert_truthvalue_loc (loc
, value
);
5667 if (low
== 0 && high
== 0)
5668 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
5671 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
5672 fold_convert_loc (loc
, etype
, high
));
5675 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
5676 fold_convert_loc (loc
, etype
, low
));
5678 if (operand_equal_p (low
, high
, 0))
5679 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
5680 fold_convert_loc (loc
, etype
, low
));
5682 if (TREE_CODE (exp
) == BIT_AND_EXPR
5683 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
5684 return fold_build2_loc (loc
, EQ_EXPR
, type
,
5685 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
5689 if (integer_zerop (low
))
5691 if (! TYPE_UNSIGNED (etype
))
5693 etype
= unsigned_type_for (etype
);
5694 high
= fold_convert_loc (loc
, etype
, high
);
5695 exp
= fold_convert_loc (loc
, etype
, exp
);
5697 return build_range_check (loc
, type
, exp
, 1, 0, high
);
5700 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5701 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
5703 int prec
= TYPE_PRECISION (etype
);
5705 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
5707 if (TYPE_UNSIGNED (etype
))
5709 tree signed_etype
= signed_type_for (etype
);
5710 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
5712 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
5714 etype
= signed_etype
;
5715 exp
= fold_convert_loc (loc
, etype
, exp
);
5717 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
5718 build_int_cst (etype
, 0));
5722 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5723 This requires wrap-around arithmetics for the type of the expression. */
5724 etype
= range_check_type (etype
);
5725 if (etype
== NULL_TREE
)
5728 high
= fold_convert_loc (loc
, etype
, high
);
5729 low
= fold_convert_loc (loc
, etype
, low
);
5730 exp
= fold_convert_loc (loc
, etype
, exp
);
5732 value
= const_binop (MINUS_EXPR
, high
, low
);
5734 if (value
!= 0 && !TREE_OVERFLOW (value
))
5735 return build_range_check (loc
, type
,
5736 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5737 1, build_int_cst (etype
, 0), value
);
5742 /* Return the predecessor of VAL in its type, handling the infinite case. */
5745 range_predecessor (tree val
)
5747 tree type
= TREE_TYPE (val
);
5749 if (INTEGRAL_TYPE_P (type
)
5750 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5753 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5754 build_int_cst (TREE_TYPE (val
), 1), 0);
5757 /* Return the successor of VAL in its type, handling the infinite case. */
5760 range_successor (tree val
)
5762 tree type
= TREE_TYPE (val
);
5764 if (INTEGRAL_TYPE_P (type
)
5765 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5768 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5769 build_int_cst (TREE_TYPE (val
), 1), 0);
5772 /* Given two ranges, see if we can merge them into one. Return 1 if we
5773 can, 0 if we can't. Set the output range into the specified parameters. */
5776 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5777 tree high0
, int in1_p
, tree low1
, tree high1
)
5785 int lowequal
= ((low0
== 0 && low1
== 0)
5786 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5787 low0
, 0, low1
, 0)));
5788 int highequal
= ((high0
== 0 && high1
== 0)
5789 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5790 high0
, 1, high1
, 1)));
5792 /* Make range 0 be the range that starts first, or ends last if they
5793 start at the same value. Swap them if it isn't. */
5794 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5797 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5798 high1
, 1, high0
, 1))))
5800 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5801 tem
= low0
, low0
= low1
, low1
= tem
;
5802 tem
= high0
, high0
= high1
, high1
= tem
;
5805 /* If the second range is != high1 where high1 is the type maximum of
5806 the type, try first merging with < high1 range. */
5809 && TREE_CODE (low1
) == INTEGER_CST
5810 && (TREE_CODE (TREE_TYPE (low1
)) == INTEGER_TYPE
5811 || (TREE_CODE (TREE_TYPE (low1
)) == ENUMERAL_TYPE
5812 && known_eq (TYPE_PRECISION (TREE_TYPE (low1
)),
5813 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1
))))))
5814 && operand_equal_p (low1
, high1
, 0))
5816 if (tree_int_cst_equal (low1
, TYPE_MAX_VALUE (TREE_TYPE (low1
)))
5817 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5818 !in1_p
, NULL_TREE
, range_predecessor (low1
)))
5820 /* Similarly for the second range != low1 where low1 is the type minimum
5821 of the type, try first merging with > low1 range. */
5822 if (tree_int_cst_equal (low1
, TYPE_MIN_VALUE (TREE_TYPE (low1
)))
5823 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5824 !in1_p
, range_successor (low1
), NULL_TREE
))
5828 /* Now flag two cases, whether the ranges are disjoint or whether the
5829 second range is totally subsumed in the first. Note that the tests
5830 below are simplified by the ones above. */
5831 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5832 high0
, 1, low1
, 0));
5833 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5834 high1
, 1, high0
, 1));
5836 /* We now have four cases, depending on whether we are including or
5837 excluding the two ranges. */
5840 /* If they don't overlap, the result is false. If the second range
5841 is a subset it is the result. Otherwise, the range is from the start
5842 of the second to the end of the first. */
5844 in_p
= 0, low
= high
= 0;
5846 in_p
= 1, low
= low1
, high
= high1
;
5848 in_p
= 1, low
= low1
, high
= high0
;
5851 else if (in0_p
&& ! in1_p
)
5853 /* If they don't overlap, the result is the first range. If they are
5854 equal, the result is false. If the second range is a subset of the
5855 first, and the ranges begin at the same place, we go from just after
5856 the end of the second range to the end of the first. If the second
5857 range is not a subset of the first, or if it is a subset and both
5858 ranges end at the same place, the range starts at the start of the
5859 first range and ends just before the second range.
5860 Otherwise, we can't describe this as a single range. */
5862 in_p
= 1, low
= low0
, high
= high0
;
5863 else if (lowequal
&& highequal
)
5864 in_p
= 0, low
= high
= 0;
5865 else if (subset
&& lowequal
)
5867 low
= range_successor (high1
);
5872 /* We are in the weird situation where high0 > high1 but
5873 high1 has no successor. Punt. */
5877 else if (! subset
|| highequal
)
5880 high
= range_predecessor (low1
);
5884 /* low0 < low1 but low1 has no predecessor. Punt. */
5892 else if (! in0_p
&& in1_p
)
5894 /* If they don't overlap, the result is the second range. If the second
5895 is a subset of the first, the result is false. Otherwise,
5896 the range starts just after the first range and ends at the
5897 end of the second. */
5899 in_p
= 1, low
= low1
, high
= high1
;
5900 else if (subset
|| highequal
)
5901 in_p
= 0, low
= high
= 0;
5904 low
= range_successor (high0
);
5909 /* high1 > high0 but high0 has no successor. Punt. */
5917 /* The case where we are excluding both ranges. Here the complex case
5918 is if they don't overlap. In that case, the only time we have a
5919 range is if they are adjacent. If the second is a subset of the
5920 first, the result is the first. Otherwise, the range to exclude
5921 starts at the beginning of the first range and ends at the end of the
5925 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5926 range_successor (high0
),
5928 in_p
= 0, low
= low0
, high
= high1
;
5931 /* Canonicalize - [min, x] into - [-, x]. */
5932 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5933 switch (TREE_CODE (TREE_TYPE (low0
)))
5936 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5938 (TYPE_MODE (TREE_TYPE (low0
)))))
5942 if (tree_int_cst_equal (low0
,
5943 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5947 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5948 && integer_zerop (low0
))
5955 /* Canonicalize - [x, max] into - [x, -]. */
5956 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5957 switch (TREE_CODE (TREE_TYPE (high1
)))
5960 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5962 (TYPE_MODE (TREE_TYPE (high1
)))))
5966 if (tree_int_cst_equal (high1
,
5967 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5971 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5972 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5974 build_int_cst (TREE_TYPE (high1
), 1),
5982 /* The ranges might be also adjacent between the maximum and
5983 minimum values of the given type. For
5984 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5985 return + [x + 1, y - 1]. */
5986 if (low0
== 0 && high1
== 0)
5988 low
= range_successor (high0
);
5989 high
= range_predecessor (low1
);
5990 if (low
== 0 || high
== 0)
6000 in_p
= 0, low
= low0
, high
= high0
;
6002 in_p
= 0, low
= low0
, high
= high1
;
6005 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
6010 /* Subroutine of fold, looking inside expressions of the form
6011 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6012 are the three operands of the COND_EXPR. This function is
6013 being used also to optimize A op B ? C : A, by reversing the
6016 Return a folded expression whose code is not a COND_EXPR
6017 anymore, or NULL_TREE if no folding opportunity is found. */
6020 fold_cond_expr_with_comparison (location_t loc
, tree type
,
6021 enum tree_code comp_code
,
6022 tree arg00
, tree arg01
, tree arg1
, tree arg2
)
6024 tree arg1_type
= TREE_TYPE (arg1
);
6030 /* If we have A op 0 ? A : -A, consider applying the following
6033 A == 0? A : -A same as -A
6034 A != 0? A : -A same as A
6035 A >= 0? A : -A same as abs (A)
6036 A > 0? A : -A same as abs (A)
6037 A <= 0? A : -A same as -abs (A)
6038 A < 0? A : -A same as -abs (A)
6040 None of these transformations work for modes with signed
6041 zeros. If A is +/-0, the first two transformations will
6042 change the sign of the result (from +0 to -0, or vice
6043 versa). The last four will fix the sign of the result,
6044 even though the original expressions could be positive or
6045 negative, depending on the sign of A.
6047 Note that all these transformations are correct if A is
6048 NaN, since the two alternatives (A and -A) are also NaNs. */
6049 if (!HONOR_SIGNED_ZEROS (type
)
6050 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
6051 ? real_zerop (arg01
)
6052 : integer_zerop (arg01
))
6053 && ((TREE_CODE (arg2
) == NEGATE_EXPR
6054 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
6055 /* In the case that A is of the form X-Y, '-A' (arg2) may
6056 have already been folded to Y-X, check for that. */
6057 || (TREE_CODE (arg1
) == MINUS_EXPR
6058 && TREE_CODE (arg2
) == MINUS_EXPR
6059 && operand_equal_p (TREE_OPERAND (arg1
, 0),
6060 TREE_OPERAND (arg2
, 1), 0)
6061 && operand_equal_p (TREE_OPERAND (arg1
, 1),
6062 TREE_OPERAND (arg2
, 0), 0))))
6067 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
6068 return fold_convert_loc (loc
, type
, negate_expr (tem
));
6071 return fold_convert_loc (loc
, type
, arg1
);
6074 if (flag_trapping_math
)
6079 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
6081 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
6082 return fold_convert_loc (loc
, type
, tem
);
6085 if (flag_trapping_math
)
6090 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
6092 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
6093 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
6095 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6096 is not, invokes UB both in abs and in the negation of it.
6097 So, use ABSU_EXPR instead. */
6098 tree utype
= unsigned_type_for (TREE_TYPE (arg1
));
6099 tem
= fold_build1_loc (loc
, ABSU_EXPR
, utype
, arg1
);
6100 tem
= negate_expr (tem
);
6101 return fold_convert_loc (loc
, type
, tem
);
6105 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
6106 return negate_expr (fold_convert_loc (loc
, type
, tem
));
6109 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
6113 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6114 A == 0 ? A : 0 is always 0 unless A is -0. Note that
6115 both transformations are correct when A is NaN: A != 0
6116 is then true, and A == 0 is false. */
6118 if (!HONOR_SIGNED_ZEROS (type
)
6119 && integer_zerop (arg01
) && integer_zerop (arg2
))
6121 if (comp_code
== NE_EXPR
)
6122 return fold_convert_loc (loc
, type
, arg1
);
6123 else if (comp_code
== EQ_EXPR
)
6124 return build_zero_cst (type
);
6127 /* Try some transformations of A op B ? A : B.
6129 A == B? A : B same as B
6130 A != B? A : B same as A
6131 A >= B? A : B same as max (A, B)
6132 A > B? A : B same as max (B, A)
6133 A <= B? A : B same as min (A, B)
6134 A < B? A : B same as min (B, A)
6136 As above, these transformations don't work in the presence
6137 of signed zeros. For example, if A and B are zeros of
6138 opposite sign, the first two transformations will change
6139 the sign of the result. In the last four, the original
6140 expressions give different results for (A=+0, B=-0) and
6141 (A=-0, B=+0), but the transformed expressions do not.
6143 The first two transformations are correct if either A or B
6144 is a NaN. In the first transformation, the condition will
6145 be false, and B will indeed be chosen. In the case of the
6146 second transformation, the condition A != B will be true,
6147 and A will be chosen.
6149 The conversions to max() and min() are not correct if B is
6150 a number and A is not. The conditions in the original
6151 expressions will be false, so all four give B. The min()
6152 and max() versions would give a NaN instead. */
6153 if (!HONOR_SIGNED_ZEROS (type
)
6154 && operand_equal_for_comparison_p (arg01
, arg2
)
6155 /* Avoid these transformations if the COND_EXPR may be used
6156 as an lvalue in the C++ front-end. PR c++/19199. */
6158 || VECTOR_TYPE_P (type
)
6159 || (! lang_GNU_CXX ()
6160 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
6161 || ! maybe_lvalue_p (arg1
)
6162 || ! maybe_lvalue_p (arg2
)))
6164 tree comp_op0
= arg00
;
6165 tree comp_op1
= arg01
;
6166 tree comp_type
= TREE_TYPE (comp_op0
);
6171 return fold_convert_loc (loc
, type
, arg2
);
6173 return fold_convert_loc (loc
, type
, arg1
);
6178 /* In C++ a ?: expression can be an lvalue, so put the
6179 operand which will be used if they are equal first
6180 so that we can convert this back to the
6181 corresponding COND_EXPR. */
6182 if (!HONOR_NANS (arg1
))
6184 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6185 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6186 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
6187 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
6188 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
6189 comp_op1
, comp_op0
);
6190 return fold_convert_loc (loc
, type
, tem
);
6197 if (!HONOR_NANS (arg1
))
6199 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6200 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6201 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
6202 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
6203 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
6204 comp_op1
, comp_op0
);
6205 return fold_convert_loc (loc
, type
, tem
);
6209 if (!HONOR_NANS (arg1
))
6210 return fold_convert_loc (loc
, type
, arg2
);
6213 if (!HONOR_NANS (arg1
))
6214 return fold_convert_loc (loc
, type
, arg1
);
6217 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
6227 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6228 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6229 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6233 /* EXP is some logical combination of boolean tests. See if we can
6234 merge it into some range test. Return the new tree if so. */
6237 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
6240 int or_op
= (code
== TRUTH_ORIF_EXPR
6241 || code
== TRUTH_OR_EXPR
);
6242 int in0_p
, in1_p
, in_p
;
6243 tree low0
, low1
, low
, high0
, high1
, high
;
6244 bool strict_overflow_p
= false;
6246 const char * const warnmsg
= G_("assuming signed overflow does not occur "
6247 "when simplifying range test");
6249 if (!INTEGRAL_TYPE_P (type
))
6252 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
6253 /* If op0 is known true or false and this is a short-circuiting
6254 operation we must not merge with op1 since that makes side-effects
6255 unconditional. So special-case this. */
6257 && ((code
== TRUTH_ORIF_EXPR
&& in0_p
)
6258 || (code
== TRUTH_ANDIF_EXPR
&& !in0_p
)))
6260 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
6262 /* If this is an OR operation, invert both sides; we will invert
6263 again at the end. */
6265 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
6267 /* If both expressions are the same, if we can merge the ranges, and we
6268 can build the range test, return it or it inverted. If one of the
6269 ranges is always true or always false, consider it to be the same
6270 expression as the other. */
6271 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
6272 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
6274 && (tem
= (build_range_check (loc
, type
,
6276 : rhs
!= 0 ? rhs
: integer_zero_node
,
6277 in_p
, low
, high
))) != 0)
6279 if (strict_overflow_p
)
6280 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
6281 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
6284 /* On machines where the branch cost is expensive, if this is a
6285 short-circuited branch and the underlying object on both sides
6286 is the same, make a non-short-circuit operation. */
6287 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
6288 if (param_logical_op_non_short_circuit
!= -1)
6289 logical_op_non_short_circuit
6290 = param_logical_op_non_short_circuit
;
6291 if (logical_op_non_short_circuit
6292 && !sanitize_coverage_p ()
6293 && lhs
!= 0 && rhs
!= 0
6294 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6295 && operand_equal_p (lhs
, rhs
, 0))
6297 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6298 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6299 which cases we can't do this. */
6300 if (simple_operand_p (lhs
))
6301 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6302 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6305 else if (!lang_hooks
.decls
.global_bindings_p ()
6306 && !CONTAINS_PLACEHOLDER_P (lhs
))
6308 tree common
= save_expr (lhs
);
6310 if ((lhs
= build_range_check (loc
, type
, common
,
6311 or_op
? ! in0_p
: in0_p
,
6313 && (rhs
= build_range_check (loc
, type
, common
,
6314 or_op
? ! in1_p
: in1_p
,
6317 if (strict_overflow_p
)
6318 fold_overflow_warning (warnmsg
,
6319 WARN_STRICT_OVERFLOW_COMPARISON
);
6320 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6321 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6330 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6331 bit value. Arrange things so the extra bits will be set to zero if and
6332 only if C is signed-extended to its full width. If MASK is nonzero,
6333 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6336 unextend (tree c
, int p
, int unsignedp
, tree mask
)
6338 tree type
= TREE_TYPE (c
);
6339 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
6342 if (p
== modesize
|| unsignedp
)
6345 /* We work by getting just the sign bit into the low-order bit, then
6346 into the high-order bit, then sign-extend. We then XOR that value
6348 temp
= build_int_cst (TREE_TYPE (c
),
6349 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
6351 /* We must use a signed type in order to get an arithmetic right shift.
6352 However, we must also avoid introducing accidental overflows, so that
6353 a subsequent call to integer_zerop will work. Hence we must
6354 do the type conversion here. At this point, the constant is either
6355 zero or one, and the conversion to a signed type can never overflow.
6356 We could get an overflow if this conversion is done anywhere else. */
6357 if (TYPE_UNSIGNED (type
))
6358 temp
= fold_convert (signed_type_for (type
), temp
);
6360 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
6361 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
6363 temp
= const_binop (BIT_AND_EXPR
, temp
,
6364 fold_convert (TREE_TYPE (c
), mask
));
6365 /* If necessary, convert the type back to match the type of C. */
6366 if (TYPE_UNSIGNED (type
))
6367 temp
= fold_convert (type
, temp
);
6369 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
6372 /* For an expression that has the form
6376 we can drop one of the inner expressions and simplify to
6380 LOC is the location of the resulting expression. OP is the inner
6381 logical operation; the left-hand side in the examples above, while CMPOP
6382 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6383 removing a condition that guards another, as in
6384 (A != NULL && A->...) || A == NULL
6385 which we must not transform. If RHS_ONLY is true, only eliminate the
6386 right-most operand of the inner logical operation. */
6389 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
6392 tree type
= TREE_TYPE (cmpop
);
6393 enum tree_code code
= TREE_CODE (cmpop
);
6394 enum tree_code truthop_code
= TREE_CODE (op
);
6395 tree lhs
= TREE_OPERAND (op
, 0);
6396 tree rhs
= TREE_OPERAND (op
, 1);
6397 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
6398 enum tree_code rhs_code
= TREE_CODE (rhs
);
6399 enum tree_code lhs_code
= TREE_CODE (lhs
);
6400 enum tree_code inv_code
;
6402 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
6405 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
6408 if (rhs_code
== truthop_code
)
6410 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
6411 if (newrhs
!= NULL_TREE
)
6414 rhs_code
= TREE_CODE (rhs
);
6417 if (lhs_code
== truthop_code
&& !rhs_only
)
6419 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
6420 if (newlhs
!= NULL_TREE
)
6423 lhs_code
= TREE_CODE (lhs
);
6427 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
6428 if (inv_code
== rhs_code
6429 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6430 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6432 if (!rhs_only
&& inv_code
== lhs_code
6433 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6434 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6436 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
6437 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
6442 /* Find ways of folding logical expressions of LHS and RHS:
6443 Try to merge two comparisons to the same innermost item.
6444 Look for range tests like "ch >= '0' && ch <= '9'".
6445 Look for combinations of simple terms on machines with expensive branches
6446 and evaluate the RHS unconditionally.
6448 For example, if we have p->a == 2 && p->b == 4 and we can make an
6449 object large enough to span both A and B, we can do this with a comparison
6450 against the object ANDed with the a mask.
6452 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6453 operations to do this with one comparison.
6455 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6456 function and the one above.
6458 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6459 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6461 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6464 We return the simplified tree or 0 if no optimization is possible. */
6467 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
6470 /* If this is the "or" of two comparisons, we can do something if
6471 the comparisons are NE_EXPR. If this is the "and", we can do something
6472 if the comparisons are EQ_EXPR. I.e.,
6473 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6475 WANTED_CODE is this operation code. For single bit fields, we can
6476 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6477 comparison for one-bit fields. */
6479 enum tree_code wanted_code
;
6480 enum tree_code lcode
, rcode
;
6481 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
6482 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
6483 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
6484 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
6485 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
6486 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
6487 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
6488 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
6489 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
6490 scalar_int_mode lnmode
, rnmode
;
6491 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
6492 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
6493 tree l_const
, r_const
;
6494 tree lntype
, rntype
, result
;
6495 HOST_WIDE_INT first_bit
, end_bit
;
6498 /* Start by getting the comparison codes. Fail if anything is volatile.
6499 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6500 it were surrounded with a NE_EXPR. */
6502 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
6505 lcode
= TREE_CODE (lhs
);
6506 rcode
= TREE_CODE (rhs
);
6508 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
6510 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
6511 build_int_cst (TREE_TYPE (lhs
), 0));
6515 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
6517 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
6518 build_int_cst (TREE_TYPE (rhs
), 0));
6522 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
6523 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
6526 ll_arg
= TREE_OPERAND (lhs
, 0);
6527 lr_arg
= TREE_OPERAND (lhs
, 1);
6528 rl_arg
= TREE_OPERAND (rhs
, 0);
6529 rr_arg
= TREE_OPERAND (rhs
, 1);
6531 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6532 if (simple_operand_p (ll_arg
)
6533 && simple_operand_p (lr_arg
))
6535 if (operand_equal_p (ll_arg
, rl_arg
, 0)
6536 && operand_equal_p (lr_arg
, rr_arg
, 0))
6538 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
6539 truth_type
, ll_arg
, lr_arg
);
6543 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
6544 && operand_equal_p (lr_arg
, rl_arg
, 0))
6546 result
= combine_comparisons (loc
, code
, lcode
,
6547 swap_tree_comparison (rcode
),
6548 truth_type
, ll_arg
, lr_arg
);
6554 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
6555 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
6557 /* If the RHS can be evaluated unconditionally and its operands are
6558 simple, it wins to evaluate the RHS unconditionally on machines
6559 with expensive branches. In this case, this isn't a comparison
6560 that can be merged. */
6562 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
6564 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
6565 && simple_operand_p (rl_arg
)
6566 && simple_operand_p (rr_arg
))
6568 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6569 if (code
== TRUTH_OR_EXPR
6570 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
6571 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
6572 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6573 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6574 return build2_loc (loc
, NE_EXPR
, truth_type
,
6575 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6577 build_int_cst (TREE_TYPE (ll_arg
), 0));
6579 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6580 if (code
== TRUTH_AND_EXPR
6581 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
6582 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
6583 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6584 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6585 return build2_loc (loc
, EQ_EXPR
, truth_type
,
6586 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6588 build_int_cst (TREE_TYPE (ll_arg
), 0));
6591 /* See if the comparisons can be merged. Then get all the parameters for
6594 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
6595 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
6598 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
6600 ll_inner
= decode_field_reference (loc
, &ll_arg
,
6601 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
6602 &ll_unsignedp
, &ll_reversep
, &volatilep
,
6603 &ll_mask
, &ll_and_mask
);
6604 lr_inner
= decode_field_reference (loc
, &lr_arg
,
6605 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
6606 &lr_unsignedp
, &lr_reversep
, &volatilep
,
6607 &lr_mask
, &lr_and_mask
);
6608 rl_inner
= decode_field_reference (loc
, &rl_arg
,
6609 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
6610 &rl_unsignedp
, &rl_reversep
, &volatilep
,
6611 &rl_mask
, &rl_and_mask
);
6612 rr_inner
= decode_field_reference (loc
, &rr_arg
,
6613 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
6614 &rr_unsignedp
, &rr_reversep
, &volatilep
,
6615 &rr_mask
, &rr_and_mask
);
6617 /* It must be true that the inner operation on the lhs of each
6618 comparison must be the same if we are to be able to do anything.
6619 Then see if we have constants. If not, the same must be true for
6622 || ll_reversep
!= rl_reversep
6623 || ll_inner
== 0 || rl_inner
== 0
6624 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
6627 if (TREE_CODE (lr_arg
) == INTEGER_CST
6628 && TREE_CODE (rr_arg
) == INTEGER_CST
)
6630 l_const
= lr_arg
, r_const
= rr_arg
;
6631 lr_reversep
= ll_reversep
;
6633 else if (lr_reversep
!= rr_reversep
6634 || lr_inner
== 0 || rr_inner
== 0
6635 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
6638 l_const
= r_const
= 0;
6640 /* If either comparison code is not correct for our logical operation,
6641 fail. However, we can convert a one-bit comparison against zero into
6642 the opposite comparison against that bit being set in the field. */
6644 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
6645 if (lcode
!= wanted_code
)
6647 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
6649 /* Make the left operand unsigned, since we are only interested
6650 in the value of one bit. Otherwise we are doing the wrong
6659 /* This is analogous to the code for l_const above. */
6660 if (rcode
!= wanted_code
)
6662 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
6671 /* See if we can find a mode that contains both fields being compared on
6672 the left. If we can't, fail. Otherwise, update all constants and masks
6673 to be relative to a field of that size. */
6674 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
6675 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
6676 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6677 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
6678 volatilep
, &lnmode
))
6681 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
6682 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
6683 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
6684 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
6686 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6688 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
6689 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
6692 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
6693 size_int (xll_bitpos
));
6694 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
6695 size_int (xrl_bitpos
));
6696 if (ll_mask
== NULL_TREE
|| rl_mask
== NULL_TREE
)
6701 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
6702 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
6703 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
6704 if (l_const
== NULL_TREE
)
6706 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
6707 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6710 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6712 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6717 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
6718 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
6719 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
6720 if (r_const
== NULL_TREE
)
6722 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
6723 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6726 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6728 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6732 /* If the right sides are not constant, do the same for it. Also,
6733 disallow this optimization if a size, signedness or storage order
6734 mismatch occurs between the left and right sides. */
6737 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
6738 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
6739 || ll_reversep
!= lr_reversep
6740 /* Make sure the two fields on the right
6741 correspond to the left without being swapped. */
6742 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
6745 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
6746 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
6747 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6748 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
6749 volatilep
, &rnmode
))
6752 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
6753 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
6754 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
6755 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
6757 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6759 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
6760 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
6763 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6765 size_int (xlr_bitpos
));
6766 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6768 size_int (xrr_bitpos
));
6769 if (lr_mask
== NULL_TREE
|| rr_mask
== NULL_TREE
)
6772 /* Make a mask that corresponds to both fields being compared.
6773 Do this for both items being compared. If the operands are the
6774 same size and the bits being compared are in the same position
6775 then we can do this by masking both and comparing the masked
6777 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6778 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
6779 if (lnbitsize
== rnbitsize
6780 && xll_bitpos
== xlr_bitpos
6784 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6785 lntype
, lnbitsize
, lnbitpos
,
6786 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6787 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6788 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6790 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6791 rntype
, rnbitsize
, rnbitpos
,
6792 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6793 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6794 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6796 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6799 /* There is still another way we can do something: If both pairs of
6800 fields being compared are adjacent, we may be able to make a wider
6801 field containing them both.
6803 Note that we still must mask the lhs/rhs expressions. Furthermore,
6804 the mask must be shifted to account for the shift done by
6805 make_bit_field_ref. */
6806 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6807 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6808 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6809 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6817 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6818 ll_bitsize
+ rl_bitsize
,
6819 MIN (ll_bitpos
, rl_bitpos
),
6820 ll_unsignedp
, ll_reversep
);
6821 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6822 lr_bitsize
+ rr_bitsize
,
6823 MIN (lr_bitpos
, rr_bitpos
),
6824 lr_unsignedp
, lr_reversep
);
6826 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6827 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6828 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6829 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6830 if (ll_mask
== NULL_TREE
|| lr_mask
== NULL_TREE
)
6833 /* Convert to the smaller type before masking out unwanted bits. */
6835 if (lntype
!= rntype
)
6837 if (lnbitsize
> rnbitsize
)
6839 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6840 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6843 else if (lnbitsize
< rnbitsize
)
6845 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6846 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6851 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6852 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6854 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6855 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6857 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6863 /* Handle the case of comparisons with constants. If there is something in
6864 common between the masks, those bits of the constants must be the same.
6865 If not, the condition is always false. Test for this to avoid generating
6866 incorrect code below. */
6867 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6868 if (! integer_zerop (result
)
6869 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6870 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6872 if (wanted_code
== NE_EXPR
)
6874 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6875 return constant_boolean_node (true, truth_type
);
6879 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6880 return constant_boolean_node (false, truth_type
);
6887 /* Construct the expression we will return. First get the component
6888 reference we will make. Unless the mask is all ones the width of
6889 that field, perform the mask operation. Then compare with the
6891 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6892 lntype
, lnbitsize
, lnbitpos
,
6893 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6895 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6896 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6897 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6899 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6900 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6903 /* T is an integer expression that is being multiplied, divided, or taken a
6904 modulus (CODE says which and what kind of divide or modulus) by a
6905 constant C. See if we can eliminate that operation by folding it with
6906 other operations already in T. WIDE_TYPE, if non-null, is a type that
6907 should be used for the computation if wider than our type.
6909 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6910 (X * 2) + (Y * 4). We must, however, be assured that either the original
6911 expression would not overflow or that overflow is undefined for the type
6912 in the language in question.
6914 If we return a non-null expression, it is an equivalent form of the
6915 original computation, but need not be in the original type.
6917 We set *STRICT_OVERFLOW_P to true if the return values depends on
6918 signed overflow being undefined. Otherwise we do not change
6919 *STRICT_OVERFLOW_P. */
6922 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6923 bool *strict_overflow_p
)
6925 /* To avoid exponential search depth, refuse to allow recursion past
6926 three levels. Beyond that (1) it's highly unlikely that we'll find
6927 something interesting and (2) we've probably processed it before
6928 when we built the inner expression. */
6937 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6944 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6945 bool *strict_overflow_p
)
6947 tree type
= TREE_TYPE (t
);
6948 enum tree_code tcode
= TREE_CODE (t
);
6952 if (TREE_CODE (type
) == BITINT_TYPE
6953 || TREE_CODE (wide_type
) == BITINT_TYPE
)
6955 if (TYPE_PRECISION (wide_type
) > TYPE_PRECISION (type
))
6958 else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6959 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6963 bool same_p
= tcode
== code
;
6964 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6965 bool sub_strict_overflow_p
;
6967 /* Don't deal with constants of zero here; they confuse the code below. */
6968 if (integer_zerop (c
))
6971 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6972 op0
= TREE_OPERAND (t
, 0);
6974 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6975 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6977 /* Note that we need not handle conditional operations here since fold
6978 already handles those cases. So just do arithmetic here. */
6982 /* For a constant, we can always simplify if we are a multiply
6983 or (for divide and modulus) if it is a multiple of our constant. */
6984 if (code
== MULT_EXPR
6985 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6988 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6989 fold_convert (ctype
, c
));
6990 /* If the multiplication overflowed, we lost information on it.
6991 See PR68142 and PR69845. */
6992 if (TREE_OVERFLOW (tem
))
6998 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6999 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
7001 /* If op0 is an expression ... */
7002 if ((COMPARISON_CLASS_P (op0
)
7003 || UNARY_CLASS_P (op0
)
7004 || BINARY_CLASS_P (op0
)
7005 || VL_EXP_CLASS_P (op0
)
7006 || EXPRESSION_CLASS_P (op0
))
7007 /* ... and has wrapping overflow, and its type is smaller
7008 than ctype, then we cannot pass through as widening. */
7009 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
7010 && (TYPE_PRECISION (ctype
)
7011 > TYPE_PRECISION (TREE_TYPE (op0
))))
7012 /* ... or this is a truncation (t is narrower than op0),
7013 then we cannot pass through this narrowing. */
7014 || (TYPE_PRECISION (type
)
7015 < TYPE_PRECISION (TREE_TYPE (op0
)))
7016 /* ... or signedness changes for division or modulus,
7017 then we cannot pass through this conversion. */
7018 || (code
!= MULT_EXPR
7019 && (TYPE_UNSIGNED (ctype
)
7020 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
7021 /* ... or has undefined overflow while the converted to
7022 type has not, we cannot do the operation in the inner type
7023 as that would introduce undefined overflow. */
7024 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
7025 && !TYPE_OVERFLOW_UNDEFINED (type
))))
7028 /* Pass the constant down and see if we can make a simplification. If
7029 we can, replace this expression with the inner simplification for
7030 possible later conversion to our or some other type. */
7031 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
7032 && TREE_CODE (t2
) == INTEGER_CST
7033 && !TREE_OVERFLOW (t2
)
7034 && (t1
= extract_muldiv (op0
, t2
, code
,
7035 code
== MULT_EXPR
? ctype
: NULL_TREE
,
7036 strict_overflow_p
)) != 0)
7041 /* If widening the type changes it from signed to unsigned, then we
7042 must avoid building ABS_EXPR itself as unsigned. */
7043 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
7045 tree cstype
= (*signed_type_for
) (ctype
);
7046 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
7049 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
7050 return fold_convert (ctype
, t1
);
7054 /* If the constant is negative, we cannot simplify this. */
7055 if (tree_int_cst_sgn (c
) == -1)
7059 /* For division and modulus, type can't be unsigned, as e.g.
7060 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
7061 For signed types, even with wrapping overflow, this is fine. */
7062 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
7064 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
7066 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
7069 case MIN_EXPR
: case MAX_EXPR
:
7070 /* If widening the type changes the signedness, then we can't perform
7071 this optimization as that changes the result. */
7072 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
7075 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
7076 sub_strict_overflow_p
= false;
7077 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
7078 &sub_strict_overflow_p
)) != 0
7079 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
7080 &sub_strict_overflow_p
)) != 0)
7082 if (tree_int_cst_sgn (c
) < 0)
7083 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
7084 if (sub_strict_overflow_p
)
7085 *strict_overflow_p
= true;
7086 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7087 fold_convert (ctype
, t2
));
7091 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
7092 /* If the second operand is constant, this is a multiplication
7093 or floor division, by a power of two, so we can treat it that
7094 way unless the multiplier or divisor overflows. Signed
7095 left-shift overflow is implementation-defined rather than
7096 undefined in C90, so do not convert signed left shift into
7098 if (TREE_CODE (op1
) == INTEGER_CST
7099 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
7100 /* const_binop may not detect overflow correctly,
7101 so check for it explicitly here. */
7102 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
7104 && (t1
= fold_convert (ctype
,
7105 const_binop (LSHIFT_EXPR
, size_one_node
,
7107 && !TREE_OVERFLOW (t1
))
7108 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
7109 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
7111 fold_convert (ctype
, op0
),
7113 c
, code
, wide_type
, strict_overflow_p
);
7116 case PLUS_EXPR
: case MINUS_EXPR
:
7117 /* See if we can eliminate the operation on both sides. If we can, we
7118 can return a new PLUS or MINUS. If we can't, the only remaining
7119 cases where we can do anything are if the second operand is a
7121 sub_strict_overflow_p
= false;
7122 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
7123 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
7124 if (t1
!= 0 && t2
!= 0
7125 && TYPE_OVERFLOW_WRAPS (ctype
)
7126 && (code
== MULT_EXPR
7127 /* If not multiplication, we can only do this if both operands
7128 are divisible by c. */
7129 || (multiple_of_p (ctype
, op0
, c
)
7130 && multiple_of_p (ctype
, op1
, c
))))
7132 if (sub_strict_overflow_p
)
7133 *strict_overflow_p
= true;
7134 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7135 fold_convert (ctype
, t2
));
7138 /* If this was a subtraction, negate OP1 and set it to be an addition.
7139 This simplifies the logic below. */
7140 if (tcode
== MINUS_EXPR
)
7142 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
7143 /* If OP1 was not easily negatable, the constant may be OP0. */
7144 if (TREE_CODE (op0
) == INTEGER_CST
)
7146 std::swap (op0
, op1
);
7151 if (TREE_CODE (op1
) != INTEGER_CST
)
7154 /* If either OP1 or C are negative, this optimization is not safe for
7155 some of the division and remainder types while for others we need
7156 to change the code. */
7157 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
7159 if (code
== CEIL_DIV_EXPR
)
7160 code
= FLOOR_DIV_EXPR
;
7161 else if (code
== FLOOR_DIV_EXPR
)
7162 code
= CEIL_DIV_EXPR
;
7163 else if (code
!= MULT_EXPR
7164 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
7168 /* If it's a multiply or a division/modulus operation of a multiple
7169 of our constant, do the operation and verify it doesn't overflow. */
7170 if (code
== MULT_EXPR
7171 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7174 op1
= const_binop (code
, fold_convert (ctype
, op1
),
7175 fold_convert (ctype
, c
));
7176 /* We allow the constant to overflow with wrapping semantics. */
7178 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
7184 /* If we have an unsigned type, we cannot widen the operation since it
7185 will change the result if the original computation overflowed. */
7186 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
7189 /* The last case is if we are a multiply. In that case, we can
7190 apply the distributive law to commute the multiply and addition
7191 if the multiplication of the constants doesn't overflow
7192 and overflow is defined. With undefined overflow
7193 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
7194 But fold_plusminus_mult_expr would factor back any power-of-two
7195 value so do not distribute in the first place in this case. */
7196 if (code
== MULT_EXPR
7197 && TYPE_OVERFLOW_WRAPS (ctype
)
7198 && !(tree_fits_shwi_p (c
) && pow2p_hwi (absu_hwi (tree_to_shwi (c
)))))
7199 return fold_build2 (tcode
, ctype
,
7200 fold_build2 (code
, ctype
,
7201 fold_convert (ctype
, op0
),
7202 fold_convert (ctype
, c
)),
7208 /* We have a special case here if we are doing something like
7209 (C * 8) % 4 since we know that's zero. */
7210 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
7211 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
7212 /* If the multiplication can overflow we cannot optimize this. */
7213 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
7214 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
7215 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7218 *strict_overflow_p
= true;
7219 return omit_one_operand (type
, integer_zero_node
, op0
);
7222 /* ... fall through ... */
7224 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
7225 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
7226 /* If we can extract our operation from the LHS, do so and return a
7227 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7228 do something only if the second operand is a constant. */
7230 && TYPE_OVERFLOW_WRAPS (ctype
)
7231 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
7232 strict_overflow_p
)) != 0)
7233 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7234 fold_convert (ctype
, op1
));
7235 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
7236 && TYPE_OVERFLOW_WRAPS (ctype
)
7237 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
7238 strict_overflow_p
)) != 0)
7239 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7240 fold_convert (ctype
, t1
));
7241 else if (TREE_CODE (op1
) != INTEGER_CST
)
7244 /* If these are the same operation types, we can associate them
7245 assuming no overflow. */
7248 bool overflow_p
= false;
7249 wi::overflow_type overflow_mul
;
7250 signop sign
= TYPE_SIGN (ctype
);
7251 unsigned prec
= TYPE_PRECISION (ctype
);
7252 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
7253 wi::to_wide (c
, prec
),
7254 sign
, &overflow_mul
);
7255 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
7257 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
7260 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7261 wide_int_to_tree (ctype
, mul
));
7264 /* If these operations "cancel" each other, we have the main
7265 optimizations of this pass, which occur when either constant is a
7266 multiple of the other, in which case we replace this with either an
7267 operation or CODE or TCODE.
7269 If we have an unsigned type, we cannot do this since it will change
7270 the result if the original computation overflowed. */
7271 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
7272 && !TYPE_OVERFLOW_SANITIZED (ctype
)
7273 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
7274 || (tcode
== MULT_EXPR
7275 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
7276 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
7277 && code
!= MULT_EXPR
)))
7279 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7282 *strict_overflow_p
= true;
7283 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7284 fold_convert (ctype
,
7285 const_binop (TRUNC_DIV_EXPR
,
7288 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
7291 *strict_overflow_p
= true;
7292 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
7293 fold_convert (ctype
,
7294 const_binop (TRUNC_DIV_EXPR
,
7307 /* Return a node which has the indicated constant VALUE (either 0 or
7308 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7309 and is of the indicated TYPE. */
7312 constant_boolean_node (bool value
, tree type
)
7314 if (type
== integer_type_node
)
7315 return value
? integer_one_node
: integer_zero_node
;
7316 else if (type
== boolean_type_node
)
7317 return value
? boolean_true_node
: boolean_false_node
;
7318 else if (VECTOR_TYPE_P (type
))
7319 return build_vector_from_val (type
,
7320 build_int_cst (TREE_TYPE (type
),
7323 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
7327 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7328 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7329 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7330 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7331 COND is the first argument to CODE; otherwise (as in the example
7332 given here), it is the second argument. TYPE is the type of the
7333 original expression. Return NULL_TREE if no simplification is
7337 fold_binary_op_with_conditional_arg (location_t loc
,
7338 enum tree_code code
,
7339 tree type
, tree op0
, tree op1
,
7340 tree cond
, tree arg
, int cond_first_p
)
7342 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
7343 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
7344 tree test
, true_value
, false_value
;
7345 tree lhs
= NULL_TREE
;
7346 tree rhs
= NULL_TREE
;
7347 enum tree_code cond_code
= COND_EXPR
;
7349 /* Do not move possibly trapping operations into the conditional as this
7350 pessimizes code and causes gimplification issues when applied late. */
7351 if (operation_could_trap_p (code
, FLOAT_TYPE_P (type
),
7352 ANY_INTEGRAL_TYPE_P (type
)
7353 && TYPE_OVERFLOW_TRAPS (type
), op1
))
7356 if (TREE_CODE (cond
) == COND_EXPR
7357 || TREE_CODE (cond
) == VEC_COND_EXPR
)
7359 test
= TREE_OPERAND (cond
, 0);
7360 true_value
= TREE_OPERAND (cond
, 1);
7361 false_value
= TREE_OPERAND (cond
, 2);
7362 /* If this operand throws an expression, then it does not make
7363 sense to try to perform a logical or arithmetic operation
7365 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
7367 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
7370 else if (!(TREE_CODE (type
) != VECTOR_TYPE
7371 && VECTOR_TYPE_P (TREE_TYPE (cond
))))
7373 tree testtype
= TREE_TYPE (cond
);
7375 true_value
= constant_boolean_node (true, testtype
);
7376 false_value
= constant_boolean_node (false, testtype
);
7379 /* Detect the case of mixing vector and scalar types - bail out. */
7382 if (VECTOR_TYPE_P (TREE_TYPE (test
)))
7383 cond_code
= VEC_COND_EXPR
;
7385 /* This transformation is only worthwhile if we don't have to wrap ARG
7386 in a SAVE_EXPR and the operation can be simplified without recursing
7387 on at least one of the branches once its pushed inside the COND_EXPR. */
7388 if (!TREE_CONSTANT (arg
)
7389 && (TREE_SIDE_EFFECTS (arg
)
7390 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
7391 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
7394 arg
= fold_convert_loc (loc
, arg_type
, arg
);
7397 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
7399 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
7401 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
7405 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
7407 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
7409 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
7412 /* Check that we have simplified at least one of the branches. */
7413 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
7416 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
7420 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7422 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7423 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7424 if ARG - ZERO_ARG is the same as X.
7426 If ARG is NULL, check for any value of type TYPE.
7428 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7429 and finite. The problematic cases are when X is zero, and its mode
7430 has signed zeros. In the case of rounding towards -infinity,
7431 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7432 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7435 fold_real_zero_addition_p (const_tree type
, const_tree arg
,
7436 const_tree zero_arg
, int negate
)
7438 if (!real_zerop (zero_arg
))
7441 /* Don't allow the fold with -fsignaling-nans. */
7442 if (arg
? tree_expr_maybe_signaling_nan_p (arg
) : HONOR_SNANS (type
))
7445 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7446 if (!HONOR_SIGNED_ZEROS (type
))
7449 /* There is no case that is safe for all rounding modes. */
7450 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
7453 /* In a vector or complex, we would need to check the sign of all zeros. */
7454 if (TREE_CODE (zero_arg
) == VECTOR_CST
)
7455 zero_arg
= uniform_vector_p (zero_arg
);
7456 if (!zero_arg
|| TREE_CODE (zero_arg
) != REAL_CST
)
7459 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7460 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg
)))
7463 /* The mode has signed zeros, and we have to honor their sign.
7464 In this situation, there are only two cases we can return true for.
7465 (i) X - 0 is the same as X with default rounding.
7466 (ii) X + 0 is X when X can't possibly be -0.0. */
7467 return negate
|| (arg
&& !tree_expr_maybe_real_minus_zero_p (arg
));
7470 /* Subroutine of match.pd that optimizes comparisons of a division by
7471 a nonzero integer constant against an integer constant, i.e.
7474 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7475 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7478 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
7479 tree
*hi
, bool *neg_overflow
)
7481 tree prod
, tmp
, type
= TREE_TYPE (c1
);
7482 signop sign
= TYPE_SIGN (type
);
7483 wi::overflow_type overflow
;
7485 /* We have to do this the hard way to detect unsigned overflow.
7486 prod = int_const_binop (MULT_EXPR, c1, c2); */
7487 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
7488 prod
= force_fit_type (type
, val
, -1, overflow
);
7489 *neg_overflow
= false;
7491 if (sign
== UNSIGNED
)
7493 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7496 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7497 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
7498 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
7500 else if (tree_int_cst_sgn (c1
) >= 0)
7502 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7503 switch (tree_int_cst_sgn (c2
))
7506 *neg_overflow
= true;
7507 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7512 *lo
= fold_negate_const (tmp
, type
);
7517 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7527 /* A negative divisor reverses the relational operators. */
7528 code
= swap_tree_comparison (code
);
7530 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
7531 switch (tree_int_cst_sgn (c2
))
7534 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7539 *hi
= fold_negate_const (tmp
, type
);
7544 *neg_overflow
= true;
7545 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7554 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
7557 if (TREE_OVERFLOW (*lo
)
7558 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
7560 if (TREE_OVERFLOW (*hi
)
7561 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
7567 /* Test whether it is preferable to swap two operands, ARG0 and
7568 ARG1, for example because ARG0 is an integer constant and ARG1
7572 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
7574 if (CONSTANT_CLASS_P (arg1
))
7576 if (CONSTANT_CLASS_P (arg0
))
7582 if (TREE_CONSTANT (arg1
))
7584 if (TREE_CONSTANT (arg0
))
7587 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7588 for commutative and comparison operators. Ensuring a canonical
7589 form allows the optimizers to find additional redundancies without
7590 having to explicitly check for both orderings. */
7591 if (TREE_CODE (arg0
) == SSA_NAME
7592 && TREE_CODE (arg1
) == SSA_NAME
7593 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
7596 /* Put SSA_NAMEs last. */
7597 if (TREE_CODE (arg1
) == SSA_NAME
)
7599 if (TREE_CODE (arg0
) == SSA_NAME
)
7602 /* Put variables last. */
7612 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7613 means A >= Y && A != MAX, but in this case we know that
7614 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7617 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7619 tree a
, typea
, type
= TREE_TYPE (bound
), a1
, diff
, y
;
7621 if (TREE_CODE (bound
) == LT_EXPR
)
7622 a
= TREE_OPERAND (bound
, 0);
7623 else if (TREE_CODE (bound
) == GT_EXPR
)
7624 a
= TREE_OPERAND (bound
, 1);
7628 typea
= TREE_TYPE (a
);
7629 if (!INTEGRAL_TYPE_P (typea
)
7630 && !POINTER_TYPE_P (typea
))
7633 if (TREE_CODE (ineq
) == LT_EXPR
)
7635 a1
= TREE_OPERAND (ineq
, 1);
7636 y
= TREE_OPERAND (ineq
, 0);
7638 else if (TREE_CODE (ineq
) == GT_EXPR
)
7640 a1
= TREE_OPERAND (ineq
, 0);
7641 y
= TREE_OPERAND (ineq
, 1);
7646 if (TREE_TYPE (a1
) != typea
)
7649 if (POINTER_TYPE_P (typea
))
7651 /* Convert the pointer types into integer before taking the difference. */
7652 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7653 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7654 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7657 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7659 if (!diff
|| !integer_onep (diff
))
7662 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7665 /* Fold a sum or difference of at least one multiplication.
7666 Returns the folded tree or NULL if no simplification could be made. */
7669 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7670 tree arg0
, tree arg1
)
7672 tree arg00
, arg01
, arg10
, arg11
;
7673 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7675 /* (A * C) +- (B * C) -> (A+-B) * C.
7676 (A * C) +- A -> A * (C+-1).
7677 We are most concerned about the case where C is a constant,
7678 but other combinations show up during loop reduction. Since
7679 it is not difficult, try all four possibilities. */
7681 if (TREE_CODE (arg0
) == MULT_EXPR
)
7683 arg00
= TREE_OPERAND (arg0
, 0);
7684 arg01
= TREE_OPERAND (arg0
, 1);
7686 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7688 arg00
= build_one_cst (type
);
7693 /* We cannot generate constant 1 for fract. */
7694 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7697 arg01
= build_one_cst (type
);
7699 if (TREE_CODE (arg1
) == MULT_EXPR
)
7701 arg10
= TREE_OPERAND (arg1
, 0);
7702 arg11
= TREE_OPERAND (arg1
, 1);
7704 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7706 arg10
= build_one_cst (type
);
7707 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7708 the purpose of this canonicalization. */
7709 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7710 && negate_expr_p (arg1
)
7711 && code
== PLUS_EXPR
)
7713 arg11
= negate_expr (arg1
);
7721 /* We cannot generate constant 1 for fract. */
7722 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7725 arg11
= build_one_cst (type
);
7729 /* Prefer factoring a common non-constant. */
7730 if (operand_equal_p (arg00
, arg10
, 0))
7731 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7732 else if (operand_equal_p (arg01
, arg11
, 0))
7733 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7734 else if (operand_equal_p (arg00
, arg11
, 0))
7735 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7736 else if (operand_equal_p (arg01
, arg10
, 0))
7737 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7739 /* No identical multiplicands; see if we can find a common
7740 power-of-two factor in non-power-of-two multiplies. This
7741 can help in multi-dimensional array access. */
7742 else if (tree_fits_shwi_p (arg01
) && tree_fits_shwi_p (arg11
))
7744 HOST_WIDE_INT int01
= tree_to_shwi (arg01
);
7745 HOST_WIDE_INT int11
= tree_to_shwi (arg11
);
7750 /* Move min of absolute values to int11. */
7751 if (absu_hwi (int01
) < absu_hwi (int11
))
7753 tmp
= int01
, int01
= int11
, int11
= tmp
;
7754 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7761 const unsigned HOST_WIDE_INT factor
= absu_hwi (int11
);
7763 && pow2p_hwi (factor
)
7764 && (int01
& (factor
- 1)) == 0
7765 /* The remainder should not be a constant, otherwise we
7766 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7767 increased the number of multiplications necessary. */
7768 && TREE_CODE (arg10
) != INTEGER_CST
)
7770 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7771 build_int_cst (TREE_TYPE (arg00
),
7776 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7783 if (! ANY_INTEGRAL_TYPE_P (type
)
7784 || TYPE_OVERFLOW_WRAPS (type
)
7785 /* We are neither factoring zero nor minus one. */
7786 || TREE_CODE (same
) == INTEGER_CST
)
7787 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7788 fold_build2_loc (loc
, code
, type
,
7789 fold_convert_loc (loc
, type
, alt0
),
7790 fold_convert_loc (loc
, type
, alt1
)),
7791 fold_convert_loc (loc
, type
, same
));
7793 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7794 same may be minus one and thus the multiplication may overflow. Perform
7795 the sum operation in an unsigned type. */
7796 tree utype
= unsigned_type_for (type
);
7797 tree tem
= fold_build2_loc (loc
, code
, utype
,
7798 fold_convert_loc (loc
, utype
, alt0
),
7799 fold_convert_loc (loc
, utype
, alt1
));
7800 /* If the sum evaluated to a constant that is not -INF the multiplication
7802 if (TREE_CODE (tem
) == INTEGER_CST
7803 && (wi::to_wide (tem
)
7804 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7805 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7806 fold_convert (type
, tem
), same
);
7808 /* Do not resort to unsigned multiplication because
7809 we lose the no-overflow property of the expression. */
7813 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7814 specified by EXPR into the buffer PTR of length LEN bytes.
7815 Return the number of bytes placed in the buffer, or zero
7819 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7821 tree type
= TREE_TYPE (expr
);
7823 if (TREE_CODE (type
) == BITINT_TYPE
)
7825 struct bitint_info info
;
7826 bool ok
= targetm
.c
.bitint_type_info (TYPE_PRECISION (type
), &info
);
7828 scalar_int_mode limb_mode
= as_a
<scalar_int_mode
> (info
.limb_mode
);
7829 if (TYPE_PRECISION (type
) > GET_MODE_PRECISION (limb_mode
))
7831 total_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
7832 /* More work is needed when adding _BitInt support to PDP endian
7833 if limb is smaller than word, or if _BitInt limb ordering doesn't
7834 match target endianity here. */
7835 gcc_checking_assert (info
.big_endian
== WORDS_BIG_ENDIAN
7836 && (BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
7837 || (GET_MODE_SIZE (limb_mode
)
7838 >= UNITS_PER_WORD
)));
7841 total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7844 total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7845 int byte
, offset
, word
, words
;
7846 unsigned char value
;
7848 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7855 return MIN (len
, total_bytes
- off
);
7857 words
= total_bytes
/ UNITS_PER_WORD
;
7859 for (byte
= 0; byte
< total_bytes
; byte
++)
7861 int bitpos
= byte
* BITS_PER_UNIT
;
7862 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7864 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7866 if (total_bytes
> UNITS_PER_WORD
)
7868 word
= byte
/ UNITS_PER_WORD
;
7869 if (WORDS_BIG_ENDIAN
)
7870 word
= (words
- 1) - word
;
7871 offset
= word
* UNITS_PER_WORD
;
7872 if (BYTES_BIG_ENDIAN
)
7873 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7875 offset
+= byte
% UNITS_PER_WORD
;
7878 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7879 if (offset
>= off
&& offset
- off
< len
)
7880 ptr
[offset
- off
] = value
;
7882 return MIN (len
, total_bytes
- off
);
7886 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7887 specified by EXPR into the buffer PTR of length LEN bytes.
7888 Return the number of bytes placed in the buffer, or zero
7892 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7894 tree type
= TREE_TYPE (expr
);
7895 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7896 int total_bytes
= GET_MODE_SIZE (mode
);
7897 FIXED_VALUE_TYPE value
;
7898 tree i_value
, i_type
;
7900 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7903 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7905 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7908 value
= TREE_FIXED_CST (expr
);
7909 i_value
= double_int_to_tree (i_type
, value
.data
);
7911 return native_encode_int (i_value
, ptr
, len
, off
);
7915 /* Subroutine of native_encode_expr. Encode the REAL_CST
7916 specified by EXPR into the buffer PTR of length LEN bytes.
7917 Return the number of bytes placed in the buffer, or zero
7921 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7923 tree type
= TREE_TYPE (expr
);
7924 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7925 int byte
, offset
, word
, words
, bitpos
;
7926 unsigned char value
;
7928 /* There are always 32 bits in each long, no matter the size of
7929 the hosts long. We handle floating point representations with
7933 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7940 return MIN (len
, total_bytes
- off
);
7942 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7944 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7946 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7947 bitpos
+= BITS_PER_UNIT
)
7949 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7950 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7952 if (UNITS_PER_WORD
< 4)
7954 word
= byte
/ UNITS_PER_WORD
;
7955 if (WORDS_BIG_ENDIAN
)
7956 word
= (words
- 1) - word
;
7957 offset
= word
* UNITS_PER_WORD
;
7958 if (BYTES_BIG_ENDIAN
)
7959 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7961 offset
+= byte
% UNITS_PER_WORD
;
7966 if (BYTES_BIG_ENDIAN
)
7968 /* Reverse bytes within each long, or within the entire float
7969 if it's smaller than a long (for HFmode). */
7970 offset
= MIN (3, total_bytes
- 1) - offset
;
7971 gcc_assert (offset
>= 0);
7974 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7976 && offset
- off
< len
)
7977 ptr
[offset
- off
] = value
;
7979 return MIN (len
, total_bytes
- off
);
7982 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7983 specified by EXPR into the buffer PTR of length LEN bytes.
7984 Return the number of bytes placed in the buffer, or zero
7988 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7993 part
= TREE_REALPART (expr
);
7994 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7995 if (off
== -1 && rsize
== 0)
7997 part
= TREE_IMAGPART (expr
);
7999 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
8000 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
8002 if (off
== -1 && isize
!= rsize
)
8004 return rsize
+ isize
;
8007 /* Like native_encode_vector, but only encode the first COUNT elements.
8008 The other arguments are as for native_encode_vector. */
8011 native_encode_vector_part (const_tree expr
, unsigned char *ptr
, int len
,
8012 int off
, unsigned HOST_WIDE_INT count
)
8014 tree itype
= TREE_TYPE (TREE_TYPE (expr
));
8015 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr
))
8016 && TYPE_PRECISION (itype
) <= BITS_PER_UNIT
)
8018 /* This is the only case in which elements can be smaller than a byte.
8019 Element 0 is always in the lsb of the containing byte. */
8020 unsigned int elt_bits
= TYPE_PRECISION (itype
);
8021 int total_bytes
= CEIL (elt_bits
* count
, BITS_PER_UNIT
);
8022 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8028 /* Zero the buffer and then set bits later where necessary. */
8029 int extract_bytes
= MIN (len
, total_bytes
- off
);
8031 memset (ptr
, 0, extract_bytes
);
8033 unsigned int elts_per_byte
= BITS_PER_UNIT
/ elt_bits
;
8034 unsigned int first_elt
= off
* elts_per_byte
;
8035 unsigned int extract_elts
= extract_bytes
* elts_per_byte
;
8036 for (unsigned int i
= 0; i
< extract_elts
; ++i
)
8038 tree elt
= VECTOR_CST_ELT (expr
, first_elt
+ i
);
8039 if (TREE_CODE (elt
) != INTEGER_CST
)
8042 if (ptr
&& wi::extract_uhwi (wi::to_wide (elt
), 0, 1))
8044 unsigned int bit
= i
* elt_bits
;
8045 ptr
[bit
/ BITS_PER_UNIT
] |= 1 << (bit
% BITS_PER_UNIT
);
8048 return extract_bytes
;
8052 int size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
8053 for (unsigned HOST_WIDE_INT i
= 0; i
< count
; i
++)
8060 tree elem
= VECTOR_CST_ELT (expr
, i
);
8061 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
8063 if ((off
== -1 && res
!= size
) || res
== 0)
8067 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
8074 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
8075 specified by EXPR into the buffer PTR of length LEN bytes.
8076 Return the number of bytes placed in the buffer, or zero
8080 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8082 unsigned HOST_WIDE_INT count
;
8083 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
8085 return native_encode_vector_part (expr
, ptr
, len
, off
, count
);
8089 /* Subroutine of native_encode_expr. Encode the STRING_CST
8090 specified by EXPR into the buffer PTR of length LEN bytes.
8091 Return the number of bytes placed in the buffer, or zero
8095 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8097 tree type
= TREE_TYPE (expr
);
8099 /* Wide-char strings are encoded in target byte-order so native
8100 encoding them is trivial. */
8101 if (BITS_PER_UNIT
!= CHAR_BIT
8102 || TREE_CODE (type
) != ARRAY_TYPE
8103 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
8104 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
8107 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
8108 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8112 len
= MIN (total_bytes
- off
, len
);
8118 if (off
< TREE_STRING_LENGTH (expr
))
8120 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
8121 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
8123 memset (ptr
+ written
, 0, len
- written
);
8129 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
8130 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
8131 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
8132 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
8133 sufficient to encode the entire EXPR, or if OFF is out of bounds.
8134 Otherwise, start at byte offset OFF and encode at most LEN bytes.
8135 Return the number of bytes placed in the buffer, or zero upon failure. */
8138 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8140 /* We don't support starting at negative offset and -1 is special. */
8144 switch (TREE_CODE (expr
))
8147 return native_encode_int (expr
, ptr
, len
, off
);
8150 return native_encode_real (expr
, ptr
, len
, off
);
8153 return native_encode_fixed (expr
, ptr
, len
, off
);
8156 return native_encode_complex (expr
, ptr
, len
, off
);
8159 return native_encode_vector (expr
, ptr
, len
, off
);
8162 return native_encode_string (expr
, ptr
, len
, off
);
8169 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8170 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8171 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8172 machine modes, we can't just use build_nonstandard_integer_type. */
8175 find_bitfield_repr_type (int fieldsize
, int len
)
8178 for (int pass
= 0; pass
< 2; pass
++)
8180 enum mode_class mclass
= pass
? MODE_PARTIAL_INT
: MODE_INT
;
8181 FOR_EACH_MODE_IN_CLASS (mode
, mclass
)
8182 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8183 && known_eq (GET_MODE_PRECISION (mode
),
8184 GET_MODE_BITSIZE (mode
))
8185 && known_le (GET_MODE_SIZE (mode
), len
))
8187 tree ret
= lang_hooks
.types
.type_for_mode (mode
, 1);
8188 if (ret
&& TYPE_MODE (ret
) == mode
)
8193 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
8194 if (int_n_enabled_p
[i
]
8195 && int_n_data
[i
].bitsize
>= (unsigned) (BITS_PER_UNIT
* fieldsize
)
8196 && int_n_trees
[i
].unsigned_type
)
8198 tree ret
= int_n_trees
[i
].unsigned_type
;
8199 mode
= TYPE_MODE (ret
);
8200 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8201 && known_eq (GET_MODE_PRECISION (mode
),
8202 GET_MODE_BITSIZE (mode
))
8203 && known_le (GET_MODE_SIZE (mode
), len
))
8210 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8211 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8212 to be non-NULL and OFF zero), then in addition to filling the
8213 bytes pointed by PTR with the value also clear any bits pointed
8214 by MASK that are known to be initialized, keep them as is for
8215 e.g. uninitialized padding bits or uninitialized fields. */
8218 native_encode_initializer (tree init
, unsigned char *ptr
, int len
,
8219 int off
, unsigned char *mask
)
8223 /* We don't support starting at negative offset and -1 is special. */
8224 if (off
< -1 || init
== NULL_TREE
)
8227 gcc_assert (mask
== NULL
|| (off
== 0 && ptr
));
8230 switch (TREE_CODE (init
))
8232 case VIEW_CONVERT_EXPR
:
8233 case NON_LVALUE_EXPR
:
8234 return native_encode_initializer (TREE_OPERAND (init
, 0), ptr
, len
, off
,
8237 r
= native_encode_expr (init
, ptr
, len
, off
);
8239 memset (mask
, 0, r
);
8242 tree type
= TREE_TYPE (init
);
8243 HOST_WIDE_INT total_bytes
= int_size_in_bytes (type
);
8244 if (total_bytes
< 0)
8246 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8248 int o
= off
== -1 ? 0 : off
;
8249 if (TREE_CODE (type
) == ARRAY_TYPE
)
8252 unsigned HOST_WIDE_INT cnt
;
8253 HOST_WIDE_INT curpos
= 0, fieldsize
, valueinit
= -1;
8254 constructor_elt
*ce
;
8256 if (!TYPE_DOMAIN (type
)
8257 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
8260 fieldsize
= int_size_in_bytes (TREE_TYPE (type
));
8264 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
8266 memset (ptr
, '\0', MIN (total_bytes
- off
, len
));
8268 for (cnt
= 0; ; cnt
++)
8270 tree val
= NULL_TREE
, index
= NULL_TREE
;
8271 HOST_WIDE_INT pos
= curpos
, count
= 0;
8273 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8278 else if (mask
== NULL
8279 || CONSTRUCTOR_NO_CLEARING (init
)
8280 || curpos
>= total_bytes
)
8285 if (index
&& TREE_CODE (index
) == RANGE_EXPR
)
8287 if (TREE_CODE (TREE_OPERAND (index
, 0)) != INTEGER_CST
8288 || TREE_CODE (TREE_OPERAND (index
, 1)) != INTEGER_CST
)
8291 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 0))
8292 - wi::to_offset (min_index
),
8293 TYPE_PRECISION (sizetype
));
8295 if (!wi::fits_shwi_p (pos
))
8297 pos
= wpos
.to_shwi ();
8299 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 1))
8300 - wi::to_offset (TREE_OPERAND (index
, 0)),
8301 TYPE_PRECISION (sizetype
));
8302 if (!wi::fits_shwi_p (wcount
))
8304 count
= wcount
.to_shwi ();
8308 if (TREE_CODE (index
) != INTEGER_CST
)
8311 = wi::sext (wi::to_offset (index
)
8312 - wi::to_offset (min_index
),
8313 TYPE_PRECISION (sizetype
));
8315 if (!wi::fits_shwi_p (wpos
))
8317 pos
= wpos
.to_shwi ();
8320 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
) && curpos
!= pos
)
8322 if (valueinit
== -1)
8324 tree zero
= build_zero_cst (TREE_TYPE (type
));
8325 r
= native_encode_initializer (zero
, ptr
+ curpos
,
8328 if (TREE_CODE (zero
) == CONSTRUCTOR
)
8333 curpos
+= fieldsize
;
8335 while (curpos
!= pos
)
8337 memcpy (ptr
+ curpos
, ptr
+ valueinit
, fieldsize
);
8338 memcpy (mask
+ curpos
, mask
+ valueinit
, fieldsize
);
8339 curpos
+= fieldsize
;
8349 && (curpos
+ fieldsize
8350 <= (HOST_WIDE_INT
) off
+ len
)))
8355 memcpy (ptr
+ (curpos
- o
), ptr
+ (pos
- o
),
8358 memcpy (mask
+ curpos
, mask
+ pos
, fieldsize
);
8360 else if (!native_encode_initializer (val
,
8377 else if (curpos
+ fieldsize
> off
8378 && curpos
< (HOST_WIDE_INT
) off
+ len
)
8380 /* Partial overlap. */
8381 unsigned char *p
= NULL
;
8384 gcc_assert (mask
== NULL
);
8388 p
= ptr
+ curpos
- off
;
8389 l
= MIN ((HOST_WIDE_INT
) off
+ len
- curpos
,
8398 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8401 curpos
+= fieldsize
;
8403 while (count
-- != 0);
8405 return MIN (total_bytes
- off
, len
);
8407 else if (TREE_CODE (type
) == RECORD_TYPE
8408 || TREE_CODE (type
) == UNION_TYPE
)
8410 unsigned HOST_WIDE_INT cnt
;
8411 constructor_elt
*ce
;
8412 tree fld_base
= TYPE_FIELDS (type
);
8413 tree to_free
= NULL_TREE
;
8415 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
|| mask
== NULL
);
8417 memset (ptr
, '\0', MIN (total_bytes
- o
, len
));
8418 for (cnt
= 0; ; cnt
++)
8420 tree val
= NULL_TREE
, field
= NULL_TREE
;
8421 HOST_WIDE_INT pos
= 0, fieldsize
;
8422 unsigned HOST_WIDE_INT bpos
= 0, epos
= 0;
8427 to_free
= NULL_TREE
;
8430 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8434 if (field
== NULL_TREE
)
8437 pos
= int_byte_position (field
);
8438 if (off
!= -1 && (HOST_WIDE_INT
) off
+ len
<= pos
)
8441 else if (mask
== NULL
8442 || CONSTRUCTOR_NO_CLEARING (init
))
8447 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
))
8450 for (fld
= fld_base
; fld
; fld
= DECL_CHAIN (fld
))
8452 if (TREE_CODE (fld
) != FIELD_DECL
)
8456 if (DECL_PADDING_P (fld
))
8458 if (DECL_SIZE_UNIT (fld
) == NULL_TREE
8459 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld
)))
8461 if (integer_zerop (DECL_SIZE_UNIT (fld
)))
8465 if (fld
== NULL_TREE
)
8471 fld_base
= DECL_CHAIN (fld
);
8476 pos
= int_byte_position (field
);
8477 val
= build_zero_cst (TREE_TYPE (fld
));
8478 if (TREE_CODE (val
) == CONSTRUCTOR
)
8483 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
8484 && TYPE_DOMAIN (TREE_TYPE (field
))
8485 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field
))))
8487 if (mask
|| off
!= -1)
8489 if (val
== NULL_TREE
)
8491 if (TREE_CODE (TREE_TYPE (val
)) != ARRAY_TYPE
)
8493 fieldsize
= int_size_in_bytes (TREE_TYPE (val
));
8495 || (int) fieldsize
!= fieldsize
8496 || (pos
+ fieldsize
) > INT_MAX
)
8498 if (pos
+ fieldsize
> total_bytes
)
8500 if (ptr
!= NULL
&& total_bytes
< len
)
8501 memset (ptr
+ total_bytes
, '\0',
8502 MIN (pos
+ fieldsize
, len
) - total_bytes
);
8503 total_bytes
= pos
+ fieldsize
;
8508 if (DECL_SIZE_UNIT (field
) == NULL_TREE
8509 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field
)))
8511 fieldsize
= tree_to_shwi (DECL_SIZE_UNIT (field
));
8516 /* Prepare to deal with integral bit-fields and filter out other
8517 bit-fields that do not start and end on a byte boundary. */
8518 if (DECL_BIT_FIELD (field
))
8520 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8522 bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8523 if (INTEGRAL_TYPE_P (TREE_TYPE (field
)))
8525 bpos
%= BITS_PER_UNIT
;
8526 fieldsize
= TYPE_PRECISION (TREE_TYPE (field
)) + bpos
;
8527 epos
= fieldsize
% BITS_PER_UNIT
;
8528 fieldsize
+= BITS_PER_UNIT
- 1;
8529 fieldsize
/= BITS_PER_UNIT
;
8531 else if (bpos
% BITS_PER_UNIT
8532 || DECL_SIZE (field
) == NULL_TREE
8533 || !tree_fits_shwi_p (DECL_SIZE (field
))
8534 || tree_to_shwi (DECL_SIZE (field
)) % BITS_PER_UNIT
)
8538 if (off
!= -1 && pos
+ fieldsize
<= off
)
8541 if (val
== NULL_TREE
)
8544 if (DECL_BIT_FIELD (field
)
8545 && INTEGRAL_TYPE_P (TREE_TYPE (field
)))
8547 /* FIXME: Handle PDP endian. */
8548 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8551 if (TREE_CODE (val
) != INTEGER_CST
)
8554 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8555 tree repr_type
= NULL_TREE
;
8556 HOST_WIDE_INT rpos
= 0;
8557 if (repr
&& INTEGRAL_TYPE_P (TREE_TYPE (repr
)))
8559 rpos
= int_byte_position (repr
);
8560 repr_type
= TREE_TYPE (repr
);
8564 repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8565 if (repr_type
== NULL_TREE
)
8567 HOST_WIDE_INT repr_size
= int_size_in_bytes (repr_type
);
8568 gcc_assert (repr_size
> 0 && repr_size
<= len
);
8569 if (pos
+ repr_size
<= o
+ len
)
8573 rpos
= o
+ len
- repr_size
;
8574 gcc_assert (rpos
<= pos
);
8580 wide_int w
= wi::to_wide (val
, TYPE_PRECISION (repr_type
));
8581 int diff
= (TYPE_PRECISION (repr_type
)
8582 - TYPE_PRECISION (TREE_TYPE (field
)));
8583 HOST_WIDE_INT bitoff
= (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8584 if (!BYTES_BIG_ENDIAN
)
8585 w
= wi::lshift (w
, bitoff
);
8587 w
= wi::lshift (w
, diff
- bitoff
);
8588 val
= wide_int_to_tree (repr_type
, w
);
8590 unsigned char buf
[MAX_BITSIZE_MODE_ANY_INT
8591 / BITS_PER_UNIT
+ 1];
8592 int l
= native_encode_int (val
, buf
, sizeof buf
, 0);
8593 if (l
* BITS_PER_UNIT
!= TYPE_PRECISION (repr_type
))
8599 /* If the bitfield does not start at byte boundary, handle
8600 the partial byte at the start. */
8602 && (off
== -1 || (pos
>= off
&& len
>= 1)))
8604 if (!BYTES_BIG_ENDIAN
)
8606 int msk
= (1 << bpos
) - 1;
8607 buf
[pos
- rpos
] &= ~msk
;
8608 buf
[pos
- rpos
] |= ptr
[pos
- o
] & msk
;
8611 if (fieldsize
> 1 || epos
== 0)
8614 mask
[pos
] &= (msk
| ~((1 << epos
) - 1));
8619 int msk
= (1 << (BITS_PER_UNIT
- bpos
)) - 1;
8620 buf
[pos
- rpos
] &= msk
;
8621 buf
[pos
- rpos
] |= ptr
[pos
- o
] & ~msk
;
8624 if (fieldsize
> 1 || epos
== 0)
8628 | ((1 << (BITS_PER_UNIT
- epos
))
8633 /* If the bitfield does not end at byte boundary, handle
8634 the partial byte at the end. */
8637 || pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
))
8639 if (!BYTES_BIG_ENDIAN
)
8641 int msk
= (1 << epos
) - 1;
8642 buf
[pos
- rpos
+ fieldsize
- 1] &= msk
;
8643 buf
[pos
- rpos
+ fieldsize
- 1]
8644 |= ptr
[pos
+ fieldsize
- 1 - o
] & ~msk
;
8645 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8646 mask
[pos
+ fieldsize
- 1] &= ~msk
;
8650 int msk
= (1 << (BITS_PER_UNIT
- epos
)) - 1;
8651 buf
[pos
- rpos
+ fieldsize
- 1] &= ~msk
;
8652 buf
[pos
- rpos
+ fieldsize
- 1]
8653 |= ptr
[pos
+ fieldsize
- 1 - o
] & msk
;
8654 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8655 mask
[pos
+ fieldsize
- 1] &= msk
;
8660 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8662 memcpy (ptr
+ pos
- o
, buf
+ (pos
- rpos
), fieldsize
);
8663 if (mask
&& (fieldsize
> (bpos
!= 0) + (epos
!= 0)))
8664 memset (mask
+ pos
+ (bpos
!= 0), 0,
8665 fieldsize
- (bpos
!= 0) - (epos
!= 0));
8669 /* Partial overlap. */
8670 HOST_WIDE_INT fsz
= fieldsize
;
8671 gcc_assert (mask
== NULL
);
8677 if (pos
+ fsz
> (HOST_WIDE_INT
) off
+ len
)
8678 fsz
= (HOST_WIDE_INT
) off
+ len
- pos
;
8679 memcpy (ptr
+ pos
- off
, buf
+ (pos
- rpos
), fsz
);
8686 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8688 int fldsize
= fieldsize
;
8691 tree fld
= DECL_CHAIN (field
);
8694 if (TREE_CODE (fld
) == FIELD_DECL
)
8696 fld
= DECL_CHAIN (fld
);
8698 if (fld
== NULL_TREE
)
8699 fldsize
= len
- pos
;
8701 r
= native_encode_initializer (val
, ptr
? ptr
+ pos
- o
8705 mask
? mask
+ pos
: NULL
);
8709 && fldsize
!= fieldsize
8711 && pos
+ r
> total_bytes
)
8712 total_bytes
= pos
+ r
;
8716 /* Partial overlap. */
8717 unsigned char *p
= NULL
;
8720 gcc_assert (mask
== NULL
);
8724 p
= ptr
+ pos
- off
;
8725 l
= MIN ((HOST_WIDE_INT
) off
+ len
- pos
,
8734 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8738 return MIN (total_bytes
- off
, len
);
8745 /* Subroutine of native_interpret_expr. Interpret the contents of
8746 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8747 If the buffer cannot be interpreted, return NULL_TREE. */
8750 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
8753 if (TREE_CODE (type
) == BITINT_TYPE
)
8755 struct bitint_info info
;
8756 bool ok
= targetm
.c
.bitint_type_info (TYPE_PRECISION (type
), &info
);
8758 scalar_int_mode limb_mode
= as_a
<scalar_int_mode
> (info
.limb_mode
);
8759 if (TYPE_PRECISION (type
) > GET_MODE_PRECISION (limb_mode
))
8761 total_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
8762 /* More work is needed when adding _BitInt support to PDP endian
8763 if limb is smaller than word, or if _BitInt limb ordering doesn't
8764 match target endianity here. */
8765 gcc_checking_assert (info
.big_endian
== WORDS_BIG_ENDIAN
8766 && (BYTES_BIG_ENDIAN
== WORDS_BIG_ENDIAN
8767 || (GET_MODE_SIZE (limb_mode
)
8768 >= UNITS_PER_WORD
)));
8771 total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8774 total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8776 if (total_bytes
> len
8777 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8780 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
8782 return wide_int_to_tree (type
, result
);
8786 /* Subroutine of native_interpret_expr. Interpret the contents of
8787 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8788 If the buffer cannot be interpreted, return NULL_TREE. */
8791 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
8793 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
8794 int total_bytes
= GET_MODE_SIZE (mode
);
8796 FIXED_VALUE_TYPE fixed_value
;
8798 if (total_bytes
> len
8799 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8802 result
= double_int::from_buffer (ptr
, total_bytes
);
8803 fixed_value
= fixed_from_double_int (result
, mode
);
8805 return build_fixed (type
, fixed_value
);
8809 /* Subroutine of native_interpret_expr. Interpret the contents of
8810 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8811 If the buffer cannot be interpreted, return NULL_TREE. */
8814 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
8816 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8817 int total_bytes
= GET_MODE_SIZE (mode
);
8818 unsigned char value
;
8819 /* There are always 32 bits in each long, no matter the size of
8820 the hosts long. We handle floating point representations with
8825 if (total_bytes
> len
|| total_bytes
> 24)
8827 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
8829 memset (tmp
, 0, sizeof (tmp
));
8830 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
8831 bitpos
+= BITS_PER_UNIT
)
8833 /* Both OFFSET and BYTE index within a long;
8834 bitpos indexes the whole float. */
8835 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
8836 if (UNITS_PER_WORD
< 4)
8838 int word
= byte
/ UNITS_PER_WORD
;
8839 if (WORDS_BIG_ENDIAN
)
8840 word
= (words
- 1) - word
;
8841 offset
= word
* UNITS_PER_WORD
;
8842 if (BYTES_BIG_ENDIAN
)
8843 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
8845 offset
+= byte
% UNITS_PER_WORD
;
8850 if (BYTES_BIG_ENDIAN
)
8852 /* Reverse bytes within each long, or within the entire float
8853 if it's smaller than a long (for HFmode). */
8854 offset
= MIN (3, total_bytes
- 1) - offset
;
8855 gcc_assert (offset
>= 0);
8858 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
8860 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
8863 real_from_target (&r
, tmp
, mode
);
8864 return build_real (type
, r
);
8868 /* Subroutine of native_interpret_expr. Interpret the contents of
8869 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8870 If the buffer cannot be interpreted, return NULL_TREE. */
8873 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
8875 tree etype
, rpart
, ipart
;
8878 etype
= TREE_TYPE (type
);
8879 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8882 rpart
= native_interpret_expr (etype
, ptr
, size
);
8885 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
8888 return build_complex (type
, rpart
, ipart
);
8891 /* Read a vector of type TYPE from the target memory image given by BYTES,
8892 which contains LEN bytes. The vector is known to be encodable using
8893 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8895 Return the vector on success, otherwise return null. */
8898 native_interpret_vector_part (tree type
, const unsigned char *bytes
,
8899 unsigned int len
, unsigned int npatterns
,
8900 unsigned int nelts_per_pattern
)
8902 tree elt_type
= TREE_TYPE (type
);
8903 if (VECTOR_BOOLEAN_TYPE_P (type
)
8904 && TYPE_PRECISION (elt_type
) <= BITS_PER_UNIT
)
8906 /* This is the only case in which elements can be smaller than a byte.
8907 Element 0 is always in the lsb of the containing byte. */
8908 unsigned int elt_bits
= TYPE_PRECISION (elt_type
);
8909 if (elt_bits
* npatterns
* nelts_per_pattern
> len
* BITS_PER_UNIT
)
8912 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8913 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8915 unsigned int bit_index
= i
* elt_bits
;
8916 unsigned int byte_index
= bit_index
/ BITS_PER_UNIT
;
8917 unsigned int lsb
= bit_index
% BITS_PER_UNIT
;
8918 builder
.quick_push (bytes
[byte_index
] & (1 << lsb
)
8919 ? build_all_ones_cst (elt_type
)
8920 : build_zero_cst (elt_type
));
8922 return builder
.build ();
8925 unsigned int elt_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (elt_type
));
8926 if (elt_bytes
* npatterns
* nelts_per_pattern
> len
)
8929 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8930 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8932 tree elt
= native_interpret_expr (elt_type
, bytes
, elt_bytes
);
8935 builder
.quick_push (elt
);
8938 return builder
.build ();
8941 /* Subroutine of native_interpret_expr. Interpret the contents of
8942 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8943 If the buffer cannot be interpreted, return NULL_TREE. */
8946 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
8948 unsigned HOST_WIDE_INT size
;
8950 if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type
)).is_constant (&size
)
8954 unsigned HOST_WIDE_INT count
= TYPE_VECTOR_SUBPARTS (type
).to_constant ();
8955 return native_interpret_vector_part (type
, ptr
, len
, count
, 1);
8959 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8960 the buffer PTR of length LEN as a constant of type TYPE. For
8961 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8962 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8963 return NULL_TREE. */
8966 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
8968 switch (TREE_CODE (type
))
8974 case REFERENCE_TYPE
:
8977 return native_interpret_int (type
, ptr
, len
);
8980 if (tree ret
= native_interpret_real (type
, ptr
, len
))
8982 /* For floating point values in composite modes, punt if this
8983 folding doesn't preserve bit representation. As the mode doesn't
8984 have fixed precision while GCC pretends it does, there could be
8985 valid values that GCC can't really represent accurately.
8986 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8987 bit combinationations which GCC doesn't preserve. */
8988 unsigned char buf
[24 * 2];
8989 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8990 int total_bytes
= GET_MODE_SIZE (mode
);
8991 memcpy (buf
+ 24, ptr
, total_bytes
);
8992 clear_type_padding_in_mask (type
, buf
+ 24);
8993 if (native_encode_expr (ret
, buf
, total_bytes
, 0) != total_bytes
8994 || memcmp (buf
+ 24, buf
, total_bytes
) != 0)
9000 case FIXED_POINT_TYPE
:
9001 return native_interpret_fixed (type
, ptr
, len
);
9004 return native_interpret_complex (type
, ptr
, len
);
9007 return native_interpret_vector (type
, ptr
, len
);
9014 /* Returns true if we can interpret the contents of a native encoding
9018 can_native_interpret_type_p (tree type
)
9020 switch (TREE_CODE (type
))
9026 case REFERENCE_TYPE
:
9027 case FIXED_POINT_TYPE
:
9038 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
9039 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
9042 native_interpret_aggregate (tree type
, const unsigned char *ptr
, int off
,
9045 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
9046 if (TREE_CODE (type
) == ARRAY_TYPE
)
9048 HOST_WIDE_INT eltsz
= int_size_in_bytes (TREE_TYPE (type
));
9049 if (eltsz
< 0 || eltsz
> len
|| TYPE_DOMAIN (type
) == NULL_TREE
)
9052 HOST_WIDE_INT cnt
= 0;
9053 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
9055 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
9057 cnt
= tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1;
9061 HOST_WIDE_INT pos
= 0;
9062 for (HOST_WIDE_INT i
= 0; i
< cnt
; i
++, pos
+= eltsz
)
9065 if (pos
>= len
|| pos
+ eltsz
> len
)
9067 if (can_native_interpret_type_p (TREE_TYPE (type
)))
9069 v
= native_interpret_expr (TREE_TYPE (type
),
9070 ptr
+ off
+ pos
, eltsz
);
9074 else if (TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
9075 || TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
9076 v
= native_interpret_aggregate (TREE_TYPE (type
), ptr
, off
+ pos
,
9080 CONSTRUCTOR_APPEND_ELT (elts
, size_int (i
), v
);
9082 return build_constructor (type
, elts
);
9084 if (TREE_CODE (type
) != RECORD_TYPE
)
9086 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
9088 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_PADDING_P (field
)
9089 || is_empty_type (TREE_TYPE (field
)))
9092 HOST_WIDE_INT bitoff
= 0, pos
= 0, sz
= 0;
9095 if (DECL_BIT_FIELD (field
))
9097 fld
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
9098 if (fld
&& INTEGRAL_TYPE_P (TREE_TYPE (fld
)))
9100 poly_int64 bitoffset
;
9101 poly_uint64 field_offset
, fld_offset
;
9102 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
9103 && poly_int_tree_p (DECL_FIELD_OFFSET (fld
), &fld_offset
))
9104 bitoffset
= (field_offset
- fld_offset
) * BITS_PER_UNIT
;
9107 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
9108 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld
)));
9109 diff
= (TYPE_PRECISION (TREE_TYPE (fld
))
9110 - TYPE_PRECISION (TREE_TYPE (field
)));
9111 if (!bitoffset
.is_constant (&bitoff
)
9118 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
9120 int fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
9121 int bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
9122 bpos
%= BITS_PER_UNIT
;
9124 fieldsize
+= BITS_PER_UNIT
- 1;
9125 fieldsize
/= BITS_PER_UNIT
;
9126 tree repr_type
= find_bitfield_repr_type (fieldsize
, len
);
9127 if (repr_type
== NULL_TREE
)
9129 sz
= int_size_in_bytes (repr_type
);
9130 if (sz
< 0 || sz
> len
)
9132 pos
= int_byte_position (field
);
9133 if (pos
< 0 || pos
> len
|| pos
+ fieldsize
> len
)
9136 if (pos
+ sz
<= len
)
9141 gcc_assert (rpos
<= pos
);
9143 bitoff
= (HOST_WIDE_INT
) (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
9145 diff
= (TYPE_PRECISION (repr_type
)
9146 - TYPE_PRECISION (TREE_TYPE (field
)));
9147 v
= native_interpret_expr (repr_type
, ptr
+ off
+ pos
, sz
);
9156 sz
= int_size_in_bytes (TREE_TYPE (fld
));
9157 if (sz
< 0 || sz
> len
)
9159 tree byte_pos
= byte_position (fld
);
9160 if (!tree_fits_shwi_p (byte_pos
))
9162 pos
= tree_to_shwi (byte_pos
);
9163 if (pos
< 0 || pos
> len
|| pos
+ sz
> len
)
9166 if (fld
== NULL_TREE
)
9167 /* Already handled above. */;
9168 else if (can_native_interpret_type_p (TREE_TYPE (fld
)))
9170 v
= native_interpret_expr (TREE_TYPE (fld
),
9171 ptr
+ off
+ pos
, sz
);
9175 else if (TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
9176 || TREE_CODE (TREE_TYPE (fld
)) == ARRAY_TYPE
)
9177 v
= native_interpret_aggregate (TREE_TYPE (fld
), ptr
, off
+ pos
, sz
);
9182 if (TREE_CODE (v
) != INTEGER_CST
)
9185 /* FIXME: Figure out how to handle PDP endian bitfields. */
9186 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
9188 if (!BYTES_BIG_ENDIAN
)
9189 v
= wide_int_to_tree (TREE_TYPE (field
),
9190 wi::lrshift (wi::to_wide (v
), bitoff
));
9192 v
= wide_int_to_tree (TREE_TYPE (field
),
9193 wi::lrshift (wi::to_wide (v
),
9196 CONSTRUCTOR_APPEND_ELT (elts
, field
, v
);
9198 return build_constructor (type
, elts
);
9201 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9202 or extracted constant positions and/or sizes aren't byte aligned. */
9204 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9205 bits between adjacent elements. AMNT should be within
9208 00011111|11100000 << 2 = 01111111|10000000
9209 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9212 shift_bytes_in_array_left (unsigned char *ptr
, unsigned int sz
,
9218 unsigned char carry_over
= 0U;
9219 unsigned char carry_mask
= (~0U) << (unsigned char) (BITS_PER_UNIT
- amnt
);
9220 unsigned char clear_mask
= (~0U) << amnt
;
9222 for (unsigned int i
= 0; i
< sz
; i
++)
9224 unsigned prev_carry_over
= carry_over
;
9225 carry_over
= (ptr
[i
] & carry_mask
) >> (BITS_PER_UNIT
- amnt
);
9230 ptr
[i
] &= clear_mask
;
9231 ptr
[i
] |= prev_carry_over
;
9236 /* Like shift_bytes_in_array_left but for big-endian.
9237 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9238 bits between adjacent elements. AMNT should be within
9241 00011111|11100000 >> 2 = 00000111|11111000
9242 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9245 shift_bytes_in_array_right (unsigned char *ptr
, unsigned int sz
,
9251 unsigned char carry_over
= 0U;
9252 unsigned char carry_mask
= ~(~0U << amnt
);
9254 for (unsigned int i
= 0; i
< sz
; i
++)
9256 unsigned prev_carry_over
= carry_over
;
9257 carry_over
= ptr
[i
] & carry_mask
;
9259 carry_over
<<= (unsigned char) BITS_PER_UNIT
- amnt
;
9261 ptr
[i
] |= prev_carry_over
;
9265 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9266 directly on the VECTOR_CST encoding, in a way that works for variable-
9267 length vectors. Return the resulting VECTOR_CST on success or null
9271 fold_view_convert_vector_encoding (tree type
, tree expr
)
9273 tree expr_type
= TREE_TYPE (expr
);
9274 poly_uint64 type_bits
, expr_bits
;
9275 if (!poly_int_tree_p (TYPE_SIZE (type
), &type_bits
)
9276 || !poly_int_tree_p (TYPE_SIZE (expr_type
), &expr_bits
))
9279 poly_uint64 type_units
= TYPE_VECTOR_SUBPARTS (type
);
9280 poly_uint64 expr_units
= TYPE_VECTOR_SUBPARTS (expr_type
);
9281 unsigned int type_elt_bits
= vector_element_size (type_bits
, type_units
);
9282 unsigned int expr_elt_bits
= vector_element_size (expr_bits
, expr_units
);
9284 /* We can only preserve the semantics of a stepped pattern if the new
9285 vector element is an integer of the same size. */
9286 if (VECTOR_CST_STEPPED_P (expr
)
9287 && (!INTEGRAL_TYPE_P (type
) || type_elt_bits
!= expr_elt_bits
))
9290 /* The number of bits needed to encode one element from every pattern
9291 of the original vector. */
9292 unsigned int expr_sequence_bits
9293 = VECTOR_CST_NPATTERNS (expr
) * expr_elt_bits
;
9295 /* The number of bits needed to encode one element from every pattern
9297 unsigned int type_sequence_bits
9298 = least_common_multiple (expr_sequence_bits
, type_elt_bits
);
9300 /* Don't try to read more bytes than are available, which can happen
9301 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9302 The general VIEW_CONVERT handling can cope with that case, so there's
9303 no point complicating things here. */
9304 unsigned int nelts_per_pattern
= VECTOR_CST_NELTS_PER_PATTERN (expr
);
9305 unsigned int buffer_bytes
= CEIL (nelts_per_pattern
* type_sequence_bits
,
9307 unsigned int buffer_bits
= buffer_bytes
* BITS_PER_UNIT
;
9308 if (known_gt (buffer_bits
, expr_bits
))
9311 /* Get enough bytes of EXPR to form the new encoding. */
9312 auto_vec
<unsigned char, 128> buffer (buffer_bytes
);
9313 buffer
.quick_grow (buffer_bytes
);
9314 if (native_encode_vector_part (expr
, buffer
.address (), buffer_bytes
, 0,
9315 buffer_bits
/ expr_elt_bits
)
9316 != (int) buffer_bytes
)
9319 /* Reencode the bytes as TYPE. */
9320 unsigned int type_npatterns
= type_sequence_bits
/ type_elt_bits
;
9321 return native_interpret_vector_part (type
, &buffer
[0], buffer
.length (),
9322 type_npatterns
, nelts_per_pattern
);
9325 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9326 TYPE at compile-time. If we're unable to perform the conversion
9327 return NULL_TREE. */
9330 fold_view_convert_expr (tree type
, tree expr
)
9332 /* We support up to 1024-bit values (for GCN/RISC-V V128QImode). */
9333 unsigned char buffer
[128];
9336 /* Check that the host and target are sane. */
9337 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
9340 if (VECTOR_TYPE_P (type
) && TREE_CODE (expr
) == VECTOR_CST
)
9341 if (tree res
= fold_view_convert_vector_encoding (type
, expr
))
9344 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
9348 return native_interpret_expr (type
, buffer
, len
);
9351 /* Build an expression for the address of T. Folds away INDIRECT_REF
9352 to avoid confusing the gimplify process. */
9355 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
9357 /* The size of the object is not relevant when talking about its address. */
9358 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
9359 t
= TREE_OPERAND (t
, 0);
9361 if (INDIRECT_REF_P (t
))
9363 t
= TREE_OPERAND (t
, 0);
9365 if (TREE_TYPE (t
) != ptrtype
)
9366 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
9368 else if (TREE_CODE (t
) == MEM_REF
9369 && integer_zerop (TREE_OPERAND (t
, 1)))
9371 t
= TREE_OPERAND (t
, 0);
9373 if (TREE_TYPE (t
) != ptrtype
)
9374 t
= fold_convert_loc (loc
, ptrtype
, t
);
9376 else if (TREE_CODE (t
) == MEM_REF
9377 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
9378 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
9379 TREE_OPERAND (t
, 0),
9380 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
9381 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
9383 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
9385 if (TREE_TYPE (t
) != ptrtype
)
9386 t
= fold_convert_loc (loc
, ptrtype
, t
);
9389 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
9394 /* Build an expression for the address of T. */
9397 build_fold_addr_expr_loc (location_t loc
, tree t
)
9399 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
9401 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
9404 /* Fold a unary expression of code CODE and type TYPE with operand
9405 OP0. Return the folded expression if folding is successful.
9406 Otherwise, return NULL_TREE. */
9409 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
9413 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9415 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9416 && TREE_CODE_LENGTH (code
) == 1);
9421 if (CONVERT_EXPR_CODE_P (code
)
9422 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
9424 /* Don't use STRIP_NOPS, because signedness of argument type
9426 STRIP_SIGN_NOPS (arg0
);
9430 /* Strip any conversions that don't change the mode. This
9431 is safe for every expression, except for a comparison
9432 expression because its signedness is derived from its
9435 Note that this is done as an internal manipulation within
9436 the constant folder, in order to find the simplest
9437 representation of the arguments so that their form can be
9438 studied. In any cases, the appropriate type conversions
9439 should be put back in the tree that will get out of the
9444 if (CONSTANT_CLASS_P (arg0
))
9446 tree tem
= const_unop (code
, type
, arg0
);
9449 if (TREE_TYPE (tem
) != type
)
9450 tem
= fold_convert_loc (loc
, type
, tem
);
9456 tem
= generic_simplify (loc
, code
, type
, op0
);
9460 if (TREE_CODE_CLASS (code
) == tcc_unary
)
9462 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9463 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9464 fold_build1_loc (loc
, code
, type
,
9465 fold_convert_loc (loc
, TREE_TYPE (op0
),
9466 TREE_OPERAND (arg0
, 1))));
9467 else if (TREE_CODE (arg0
) == COND_EXPR
)
9469 tree arg01
= TREE_OPERAND (arg0
, 1);
9470 tree arg02
= TREE_OPERAND (arg0
, 2);
9471 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
9472 arg01
= fold_build1_loc (loc
, code
, type
,
9473 fold_convert_loc (loc
,
9474 TREE_TYPE (op0
), arg01
));
9475 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
9476 arg02
= fold_build1_loc (loc
, code
, type
,
9477 fold_convert_loc (loc
,
9478 TREE_TYPE (op0
), arg02
));
9479 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9482 /* If this was a conversion, and all we did was to move into
9483 inside the COND_EXPR, bring it back out. But leave it if
9484 it is a conversion from integer to integer and the
9485 result precision is no wider than a word since such a
9486 conversion is cheap and may be optimized away by combine,
9487 while it couldn't if it were outside the COND_EXPR. Then return
9488 so we don't get into an infinite recursion loop taking the
9489 conversion out and then back in. */
9491 if ((CONVERT_EXPR_CODE_P (code
)
9492 || code
== NON_LVALUE_EXPR
)
9493 && TREE_CODE (tem
) == COND_EXPR
9494 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
9495 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
9496 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 1)))
9497 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 2)))
9498 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
9499 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
9500 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9502 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
9503 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
9504 || flag_syntax_only
))
9505 tem
= build1_loc (loc
, code
, type
,
9507 TREE_TYPE (TREE_OPERAND
9508 (TREE_OPERAND (tem
, 1), 0)),
9509 TREE_OPERAND (tem
, 0),
9510 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
9511 TREE_OPERAND (TREE_OPERAND (tem
, 2),
9519 case NON_LVALUE_EXPR
:
9520 if (!maybe_lvalue_p (op0
))
9521 return fold_convert_loc (loc
, type
, op0
);
9526 case FIX_TRUNC_EXPR
:
9527 if (COMPARISON_CLASS_P (op0
))
9529 /* If we have (type) (a CMP b) and type is an integral type, return
9530 new expression involving the new type. Canonicalize
9531 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9533 Do not fold the result as that would not simplify further, also
9534 folding again results in recursions. */
9535 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
9536 return build2_loc (loc
, TREE_CODE (op0
), type
,
9537 TREE_OPERAND (op0
, 0),
9538 TREE_OPERAND (op0
, 1));
9539 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
9540 && TREE_CODE (type
) != VECTOR_TYPE
)
9541 return build3_loc (loc
, COND_EXPR
, type
, op0
,
9542 constant_boolean_node (true, type
),
9543 constant_boolean_node (false, type
));
9546 /* Handle (T *)&A.B.C for A being of type T and B and C
9547 living at offset zero. This occurs frequently in
9548 C++ upcasting and then accessing the base. */
9549 if (TREE_CODE (op0
) == ADDR_EXPR
9550 && POINTER_TYPE_P (type
)
9551 && handled_component_p (TREE_OPERAND (op0
, 0)))
9553 poly_int64 bitsize
, bitpos
;
9556 int unsignedp
, reversep
, volatilep
;
9558 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
9559 &offset
, &mode
, &unsignedp
, &reversep
,
9561 /* If the reference was to a (constant) zero offset, we can use
9562 the address of the base if it has the same base type
9563 as the result type and the pointer type is unqualified. */
9565 && known_eq (bitpos
, 0)
9566 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
9567 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
9568 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
9569 return fold_convert_loc (loc
, type
,
9570 build_fold_addr_expr_loc (loc
, base
));
9573 if (TREE_CODE (op0
) == MODIFY_EXPR
9574 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
9575 /* Detect assigning a bitfield. */
9576 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
9578 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
9580 /* Don't leave an assignment inside a conversion
9581 unless assigning a bitfield. */
9582 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
9583 /* First do the assignment, then return converted constant. */
9584 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
9585 suppress_warning (tem
/* What warning? */);
9586 TREE_USED (tem
) = 1;
9590 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9591 constants (if x has signed type, the sign bit cannot be set
9592 in c). This folds extension into the BIT_AND_EXPR.
9593 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9594 very likely don't have maximal range for their precision and this
9595 transformation effectively doesn't preserve non-maximal ranges. */
9596 if (TREE_CODE (type
) == INTEGER_TYPE
9597 && TREE_CODE (op0
) == BIT_AND_EXPR
9598 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
9600 tree and_expr
= op0
;
9601 tree and0
= TREE_OPERAND (and_expr
, 0);
9602 tree and1
= TREE_OPERAND (and_expr
, 1);
9605 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
9606 || (TYPE_PRECISION (type
)
9607 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
9609 else if (TYPE_PRECISION (TREE_TYPE (and1
))
9610 <= HOST_BITS_PER_WIDE_INT
9611 && tree_fits_uhwi_p (and1
))
9613 unsigned HOST_WIDE_INT cst
;
9615 cst
= tree_to_uhwi (and1
);
9616 cst
&= HOST_WIDE_INT_M1U
9617 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
9618 change
= (cst
== 0);
9620 && !flag_syntax_only
9621 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
9624 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
9625 and0
= fold_convert_loc (loc
, uns
, and0
);
9626 and1
= fold_convert_loc (loc
, uns
, and1
);
9631 tree and1_type
= TREE_TYPE (and1
);
9632 unsigned prec
= MAX (TYPE_PRECISION (and1_type
),
9633 TYPE_PRECISION (type
));
9634 tem
= force_fit_type (type
,
9635 wide_int::from (wi::to_wide (and1
), prec
,
9636 TYPE_SIGN (and1_type
)),
9637 0, TREE_OVERFLOW (and1
));
9638 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9639 fold_convert_loc (loc
, type
, and0
), tem
);
9643 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9644 cast (T1)X will fold away. We assume that this happens when X itself
9646 if (POINTER_TYPE_P (type
)
9647 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9648 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
9650 tree arg00
= TREE_OPERAND (arg0
, 0);
9651 tree arg01
= TREE_OPERAND (arg0
, 1);
9653 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9654 when the pointed type needs higher alignment than
9655 the p+ first operand's pointed type. */
9657 && sanitize_flags_p (SANITIZE_ALIGNMENT
)
9658 && (min_align_of_type (TREE_TYPE (type
))
9659 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00
)))))
9662 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9663 when type is a reference type and arg00's type is not,
9664 because arg00 could be validly nullptr and if arg01 doesn't return,
9665 we don't want false positive binding of reference to nullptr. */
9666 if (TREE_CODE (type
) == REFERENCE_TYPE
9668 && sanitize_flags_p (SANITIZE_NULL
)
9669 && TREE_CODE (TREE_TYPE (arg00
)) != REFERENCE_TYPE
)
9672 arg00
= fold_convert_loc (loc
, type
, arg00
);
9673 return fold_build_pointer_plus_loc (loc
, arg00
, arg01
);
9676 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9677 of the same precision, and X is an integer type not narrower than
9678 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9679 if (INTEGRAL_TYPE_P (type
)
9680 && TREE_CODE (op0
) == BIT_NOT_EXPR
9681 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9682 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
9683 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
9685 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
9686 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9687 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
9688 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
9689 fold_convert_loc (loc
, type
, tem
));
9692 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9693 type of X and Y (integer types only). */
9694 if (INTEGRAL_TYPE_P (type
)
9695 && TREE_CODE (op0
) == MULT_EXPR
9696 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9697 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
))
9698 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
9699 || !sanitize_flags_p (SANITIZE_SI_OVERFLOW
)))
9701 /* Be careful not to introduce new overflows. */
9703 if (TYPE_OVERFLOW_WRAPS (type
))
9706 mult_type
= unsigned_type_for (type
);
9708 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9710 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
9711 fold_convert_loc (loc
, mult_type
,
9712 TREE_OPERAND (op0
, 0)),
9713 fold_convert_loc (loc
, mult_type
,
9714 TREE_OPERAND (op0
, 1)));
9715 return fold_convert_loc (loc
, type
, tem
);
9721 case VIEW_CONVERT_EXPR
:
9722 if (TREE_CODE (op0
) == MEM_REF
)
9724 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
9725 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
9726 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
9727 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
9728 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
9735 tem
= fold_negate_expr (loc
, arg0
);
9737 return fold_convert_loc (loc
, type
, tem
);
9741 /* Convert fabs((double)float) into (double)fabsf(float). */
9742 if (TREE_CODE (arg0
) == NOP_EXPR
9743 && TREE_CODE (type
) == REAL_TYPE
)
9745 tree targ0
= strip_float_extensions (arg0
);
9747 return fold_convert_loc (loc
, type
,
9748 fold_build1_loc (loc
, ABS_EXPR
,
9755 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9756 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9757 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9758 fold_convert_loc (loc
, type
,
9759 TREE_OPERAND (arg0
, 0)))))
9760 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
9761 fold_convert_loc (loc
, type
,
9762 TREE_OPERAND (arg0
, 1)));
9763 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9764 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9765 fold_convert_loc (loc
, type
,
9766 TREE_OPERAND (arg0
, 1)))))
9767 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
9768 fold_convert_loc (loc
, type
,
9769 TREE_OPERAND (arg0
, 0)), tem
);
9773 case TRUTH_NOT_EXPR
:
9774 /* Note that the operand of this must be an int
9775 and its values must be 0 or 1.
9776 ("true" is a fixed value perhaps depending on the language,
9777 but we don't handle values other than 1 correctly yet.) */
9778 tem
= fold_truth_not_expr (loc
, arg0
);
9781 return fold_convert_loc (loc
, type
, tem
);
9784 /* Fold *&X to X if X is an lvalue. */
9785 if (TREE_CODE (op0
) == ADDR_EXPR
)
9787 tree op00
= TREE_OPERAND (op0
, 0);
9789 || TREE_CODE (op00
) == PARM_DECL
9790 || TREE_CODE (op00
) == RESULT_DECL
)
9791 && !TREE_READONLY (op00
))
9798 } /* switch (code) */
9802 /* If the operation was a conversion do _not_ mark a resulting constant
9803 with TREE_OVERFLOW if the original constant was not. These conversions
9804 have implementation defined behavior and retaining the TREE_OVERFLOW
9805 flag here would confuse later passes such as VRP. */
9807 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
9808 tree type
, tree op0
)
9810 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
9812 && TREE_CODE (res
) == INTEGER_CST
9813 && TREE_CODE (op0
) == INTEGER_CST
9814 && CONVERT_EXPR_CODE_P (code
))
9815 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
9820 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9821 operands OP0 and OP1. LOC is the location of the resulting expression.
9822 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9823 Return the folded expression if folding is successful. Otherwise,
9824 return NULL_TREE. */
9826 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
9827 tree arg0
, tree arg1
, tree op0
, tree op1
)
9831 /* We only do these simplifications if we are optimizing. */
9835 /* Check for things like (A || B) && (A || C). We can convert this
9836 to A || (B && C). Note that either operator can be any of the four
9837 truth and/or operations and the transformation will still be
9838 valid. Also note that we only care about order for the
9839 ANDIF and ORIF operators. If B contains side effects, this
9840 might change the truth-value of A. */
9841 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9842 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
9843 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
9844 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
9845 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
9846 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
9848 tree a00
= TREE_OPERAND (arg0
, 0);
9849 tree a01
= TREE_OPERAND (arg0
, 1);
9850 tree a10
= TREE_OPERAND (arg1
, 0);
9851 tree a11
= TREE_OPERAND (arg1
, 1);
9852 bool commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
9853 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
9854 && (code
== TRUTH_AND_EXPR
9855 || code
== TRUTH_OR_EXPR
));
9857 if (operand_equal_p (a00
, a10
, 0))
9858 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9859 fold_build2_loc (loc
, code
, type
, a01
, a11
));
9860 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
9861 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9862 fold_build2_loc (loc
, code
, type
, a01
, a10
));
9863 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
9864 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
9865 fold_build2_loc (loc
, code
, type
, a00
, a11
));
9867 /* This case if tricky because we must either have commutative
9868 operators or else A10 must not have side-effects. */
9870 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
9871 && operand_equal_p (a01
, a11
, 0))
9872 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
9873 fold_build2_loc (loc
, code
, type
, a00
, a10
),
9877 /* See if we can build a range comparison. */
9878 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
9881 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
9882 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
9884 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
9886 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
9889 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
9890 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
9892 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
9894 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
9897 /* Check for the possibility of merging component references. If our
9898 lhs is another similar operation, try to merge its rhs with our
9899 rhs. Then try to merge our lhs and rhs. */
9900 if (TREE_CODE (arg0
) == code
9901 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
9902 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
9903 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9905 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
9908 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
9909 if (param_logical_op_non_short_circuit
!= -1)
9910 logical_op_non_short_circuit
9911 = param_logical_op_non_short_circuit
;
9912 if (logical_op_non_short_circuit
9913 && !sanitize_coverage_p ()
9914 && (code
== TRUTH_AND_EXPR
9915 || code
== TRUTH_ANDIF_EXPR
9916 || code
== TRUTH_OR_EXPR
9917 || code
== TRUTH_ORIF_EXPR
))
9919 enum tree_code ncode
, icode
;
9921 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
9922 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
9923 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
9925 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9926 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9927 We don't want to pack more than two leafs to a non-IF AND/OR
9929 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9930 equal to IF-CODE, then we don't want to add right-hand operand.
9931 If the inner right-hand side of left-hand operand has
9932 side-effects, or isn't simple, then we can't add to it,
9933 as otherwise we might destroy if-sequence. */
9934 if (TREE_CODE (arg0
) == icode
9935 && simple_condition_p (arg1
)
9936 /* Needed for sequence points to handle trappings, and
9938 && simple_condition_p (TREE_OPERAND (arg0
, 1)))
9940 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
9942 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
9945 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9946 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9947 else if (TREE_CODE (arg1
) == icode
9948 && simple_condition_p (arg0
)
9949 /* Needed for sequence points to handle trappings, and
9951 && simple_condition_p (TREE_OPERAND (arg1
, 0)))
9953 tem
= fold_build2_loc (loc
, ncode
, type
,
9954 arg0
, TREE_OPERAND (arg1
, 0));
9955 return fold_build2_loc (loc
, icode
, type
, tem
,
9956 TREE_OPERAND (arg1
, 1));
9958 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9960 For sequence point consistancy, we need to check for trapping,
9961 and side-effects. */
9962 else if (code
== icode
&& simple_condition_p (arg0
)
9963 && simple_condition_p (arg1
))
9964 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
9970 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9971 by changing CODE to reduce the magnitude of constants involved in
9972 ARG0 of the comparison.
9973 Returns a canonicalized comparison tree if a simplification was
9974 possible, otherwise returns NULL_TREE.
9975 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9976 valid if signed overflow is undefined. */
9979 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
9980 tree arg0
, tree arg1
,
9981 bool *strict_overflow_p
)
9983 enum tree_code code0
= TREE_CODE (arg0
);
9984 tree t
, cst0
= NULL_TREE
;
9987 /* Match A +- CST code arg1. We can change this only if overflow
9989 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9990 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
9991 /* In principle pointers also have undefined overflow behavior,
9992 but that causes problems elsewhere. */
9993 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
9994 && (code0
== MINUS_EXPR
9995 || code0
== PLUS_EXPR
)
9996 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
9999 /* Identify the constant in arg0 and its sign. */
10000 cst0
= TREE_OPERAND (arg0
, 1);
10001 sgn0
= tree_int_cst_sgn (cst0
);
10003 /* Overflowed constants and zero will cause problems. */
10004 if (integer_zerop (cst0
)
10005 || TREE_OVERFLOW (cst0
))
10008 /* See if we can reduce the magnitude of the constant in
10009 arg0 by changing the comparison code. */
10010 /* A - CST < arg1 -> A - CST-1 <= arg1. */
10011 if (code
== LT_EXPR
10012 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
10014 /* A + CST > arg1 -> A + CST-1 >= arg1. */
10015 else if (code
== GT_EXPR
10016 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
10018 /* A + CST <= arg1 -> A + CST-1 < arg1. */
10019 else if (code
== LE_EXPR
10020 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
10022 /* A - CST >= arg1 -> A - CST-1 > arg1. */
10023 else if (code
== GE_EXPR
10024 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
10028 *strict_overflow_p
= true;
10030 /* Now build the constant reduced in magnitude. But not if that
10031 would produce one outside of its types range. */
10032 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
10034 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
10035 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
10037 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
10038 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
10041 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
10042 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
10043 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
10044 t
= fold_convert (TREE_TYPE (arg1
), t
);
10046 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
10049 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
10050 overflow further. Try to decrease the magnitude of constants involved
10051 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
10052 and put sole constants at the second argument position.
10053 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
10056 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
10057 tree arg0
, tree arg1
)
10060 bool strict_overflow_p
;
10061 const char * const warnmsg
= G_("assuming signed overflow does not occur "
10062 "when reducing constant in comparison");
10064 /* Try canonicalization by simplifying arg0. */
10065 strict_overflow_p
= false;
10066 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
10067 &strict_overflow_p
);
10070 if (strict_overflow_p
)
10071 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
10075 /* Try canonicalization by simplifying arg1 using the swapped
10077 code
= swap_tree_comparison (code
);
10078 strict_overflow_p
= false;
10079 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
10080 &strict_overflow_p
);
10081 if (t
&& strict_overflow_p
)
10082 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
10086 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
10087 space. This is used to avoid issuing overflow warnings for
10088 expressions like &p->x which cannot wrap. */
10091 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
10093 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
10096 if (maybe_lt (bitpos
, 0))
10099 poly_wide_int wi_offset
;
10100 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
10101 if (offset
== NULL_TREE
)
10102 wi_offset
= wi::zero (precision
);
10103 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
10106 wi_offset
= wi::to_poly_wide (offset
);
10108 wi::overflow_type overflow
;
10109 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
10111 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
10115 poly_uint64 total_hwi
, size
;
10116 if (!total
.to_uhwi (&total_hwi
)
10117 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
10119 || known_eq (size
, 0U))
10122 if (known_le (total_hwi
, size
))
10125 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
10127 if (TREE_CODE (base
) == ADDR_EXPR
10128 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
10130 && maybe_ne (size
, 0U)
10131 && known_le (total_hwi
, size
))
10137 /* Return a positive integer when the symbol DECL is known to have
10138 a nonzero address, zero when it's known not to (e.g., it's a weak
10139 symbol), and a negative integer when the symbol is not yet in the
10140 symbol table and so whether or not its address is zero is unknown.
10141 For function local objects always return positive integer. */
10143 maybe_nonzero_address (tree decl
)
10145 /* Normally, don't do anything for variables and functions before symtab is
10146 built; it is quite possible that DECL will be declared weak later.
10147 But if folding_initializer, we need a constant answer now, so create
10148 the symtab entry and prevent later weak declaration. */
10149 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
10150 if (struct symtab_node
*symbol
10151 = (folding_initializer
10152 ? symtab_node::get_create (decl
)
10153 : symtab_node::get (decl
)))
10154 return symbol
->nonzero_address ();
10156 /* Function local objects are never NULL. */
10158 && (DECL_CONTEXT (decl
)
10159 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
10160 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
10166 /* Subroutine of fold_binary. This routine performs all of the
10167 transformations that are common to the equality/inequality
10168 operators (EQ_EXPR and NE_EXPR) and the ordering operators
10169 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10170 fold_binary should call fold_binary. Fold a comparison with
10171 tree code CODE and type TYPE with operands OP0 and OP1. Return
10172 the folded comparison or NULL_TREE. */
10175 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
10176 tree op0
, tree op1
)
10178 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
10179 tree arg0
, arg1
, tem
;
10184 STRIP_SIGN_NOPS (arg0
);
10185 STRIP_SIGN_NOPS (arg1
);
10187 /* For comparisons of pointers we can decompose it to a compile time
10188 comparison of the base objects and the offsets into the object.
10189 This requires at least one operand being an ADDR_EXPR or a
10190 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10191 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
10192 && (TREE_CODE (arg0
) == ADDR_EXPR
10193 || TREE_CODE (arg1
) == ADDR_EXPR
10194 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10195 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
10197 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
10198 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
10200 int volatilep
, reversep
, unsignedp
;
10201 bool indirect_base0
= false, indirect_base1
= false;
10203 /* Get base and offset for the access. Strip ADDR_EXPR for
10204 get_inner_reference, but put it back by stripping INDIRECT_REF
10205 off the base object if possible. indirect_baseN will be true
10206 if baseN is not an address but refers to the object itself. */
10208 if (TREE_CODE (arg0
) == ADDR_EXPR
)
10211 = get_inner_reference (TREE_OPERAND (arg0
, 0),
10212 &bitsize
, &bitpos0
, &offset0
, &mode
,
10213 &unsignedp
, &reversep
, &volatilep
);
10214 if (INDIRECT_REF_P (base0
))
10215 base0
= TREE_OPERAND (base0
, 0);
10217 indirect_base0
= true;
10219 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10221 base0
= TREE_OPERAND (arg0
, 0);
10222 STRIP_SIGN_NOPS (base0
);
10223 if (TREE_CODE (base0
) == ADDR_EXPR
)
10226 = get_inner_reference (TREE_OPERAND (base0
, 0),
10227 &bitsize
, &bitpos0
, &offset0
, &mode
,
10228 &unsignedp
, &reversep
, &volatilep
);
10229 if (INDIRECT_REF_P (base0
))
10230 base0
= TREE_OPERAND (base0
, 0);
10232 indirect_base0
= true;
10234 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
10235 offset0
= TREE_OPERAND (arg0
, 1);
10237 offset0
= size_binop (PLUS_EXPR
, offset0
,
10238 TREE_OPERAND (arg0
, 1));
10239 if (poly_int_tree_p (offset0
))
10241 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
10242 TYPE_PRECISION (sizetype
));
10243 tem
<<= LOG2_BITS_PER_UNIT
;
10245 if (tem
.to_shwi (&bitpos0
))
10246 offset0
= NULL_TREE
;
10251 if (TREE_CODE (arg1
) == ADDR_EXPR
)
10254 = get_inner_reference (TREE_OPERAND (arg1
, 0),
10255 &bitsize
, &bitpos1
, &offset1
, &mode
,
10256 &unsignedp
, &reversep
, &volatilep
);
10257 if (INDIRECT_REF_P (base1
))
10258 base1
= TREE_OPERAND (base1
, 0);
10260 indirect_base1
= true;
10262 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10264 base1
= TREE_OPERAND (arg1
, 0);
10265 STRIP_SIGN_NOPS (base1
);
10266 if (TREE_CODE (base1
) == ADDR_EXPR
)
10269 = get_inner_reference (TREE_OPERAND (base1
, 0),
10270 &bitsize
, &bitpos1
, &offset1
, &mode
,
10271 &unsignedp
, &reversep
, &volatilep
);
10272 if (INDIRECT_REF_P (base1
))
10273 base1
= TREE_OPERAND (base1
, 0);
10275 indirect_base1
= true;
10277 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
10278 offset1
= TREE_OPERAND (arg1
, 1);
10280 offset1
= size_binop (PLUS_EXPR
, offset1
,
10281 TREE_OPERAND (arg1
, 1));
10282 if (poly_int_tree_p (offset1
))
10284 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
10285 TYPE_PRECISION (sizetype
));
10286 tem
<<= LOG2_BITS_PER_UNIT
;
10288 if (tem
.to_shwi (&bitpos1
))
10289 offset1
= NULL_TREE
;
10293 /* If we have equivalent bases we might be able to simplify. */
10294 if (indirect_base0
== indirect_base1
10295 && operand_equal_p (base0
, base1
,
10296 indirect_base0
? OEP_ADDRESS_OF
: 0))
10298 /* We can fold this expression to a constant if the non-constant
10299 offset parts are equal. */
10300 if ((offset0
== offset1
10301 || (offset0
&& offset1
10302 && operand_equal_p (offset0
, offset1
, 0)))
10305 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10306 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10309 && maybe_ne (bitpos0
, bitpos1
)
10310 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10311 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10312 fold_overflow_warning (("assuming pointer wraparound does not "
10313 "occur when comparing P +- C1 with "
10315 WARN_STRICT_OVERFLOW_CONDITIONAL
);
10320 if (known_eq (bitpos0
, bitpos1
))
10321 return constant_boolean_node (true, type
);
10322 if (known_ne (bitpos0
, bitpos1
))
10323 return constant_boolean_node (false, type
);
10326 if (known_ne (bitpos0
, bitpos1
))
10327 return constant_boolean_node (true, type
);
10328 if (known_eq (bitpos0
, bitpos1
))
10329 return constant_boolean_node (false, type
);
10332 if (known_lt (bitpos0
, bitpos1
))
10333 return constant_boolean_node (true, type
);
10334 if (known_ge (bitpos0
, bitpos1
))
10335 return constant_boolean_node (false, type
);
10338 if (known_le (bitpos0
, bitpos1
))
10339 return constant_boolean_node (true, type
);
10340 if (known_gt (bitpos0
, bitpos1
))
10341 return constant_boolean_node (false, type
);
10344 if (known_ge (bitpos0
, bitpos1
))
10345 return constant_boolean_node (true, type
);
10346 if (known_lt (bitpos0
, bitpos1
))
10347 return constant_boolean_node (false, type
);
10350 if (known_gt (bitpos0
, bitpos1
))
10351 return constant_boolean_node (true, type
);
10352 if (known_le (bitpos0
, bitpos1
))
10353 return constant_boolean_node (false, type
);
10358 /* We can simplify the comparison to a comparison of the variable
10359 offset parts if the constant offset parts are equal.
10360 Be careful to use signed sizetype here because otherwise we
10361 mess with array offsets in the wrong way. This is possible
10362 because pointer arithmetic is restricted to retain within an
10363 object and overflow on pointer differences is undefined as of
10364 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10365 else if (known_eq (bitpos0
, bitpos1
)
10368 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10369 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10371 /* By converting to signed sizetype we cover middle-end pointer
10372 arithmetic which operates on unsigned pointer types of size
10373 type size and ARRAY_REF offsets which are properly sign or
10374 zero extended from their type in case it is narrower than
10376 if (offset0
== NULL_TREE
)
10377 offset0
= build_int_cst (ssizetype
, 0);
10379 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
10380 if (offset1
== NULL_TREE
)
10381 offset1
= build_int_cst (ssizetype
, 0);
10383 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
10386 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10387 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10388 fold_overflow_warning (("assuming pointer wraparound does not "
10389 "occur when comparing P +- C1 with "
10391 WARN_STRICT_OVERFLOW_COMPARISON
);
10393 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
10396 /* For equal offsets we can simplify to a comparison of the
10398 else if (known_eq (bitpos0
, bitpos1
)
10400 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
10402 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
10403 && ((offset0
== offset1
)
10404 || (offset0
&& offset1
10405 && operand_equal_p (offset0
, offset1
, 0))))
10407 if (indirect_base0
)
10408 base0
= build_fold_addr_expr_loc (loc
, base0
);
10409 if (indirect_base1
)
10410 base1
= build_fold_addr_expr_loc (loc
, base1
);
10411 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
10413 /* Comparison between an ordinary (non-weak) symbol and a null
10414 pointer can be eliminated since such symbols must have a non
10415 null address. In C, relational expressions between pointers
10416 to objects and null pointers are undefined. The results
10417 below follow the C++ rules with the additional property that
10418 every object pointer compares greater than a null pointer.
10420 else if (((DECL_P (base0
)
10421 && maybe_nonzero_address (base0
) > 0
10422 /* Avoid folding references to struct members at offset 0 to
10423 prevent tests like '&ptr->firstmember == 0' from getting
10424 eliminated. When ptr is null, although the -> expression
10425 is strictly speaking invalid, GCC retains it as a matter
10426 of QoI. See PR c/44555. */
10427 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
10428 || CONSTANT_CLASS_P (base0
))
10430 /* The caller guarantees that when one of the arguments is
10431 constant (i.e., null in this case) it is second. */
10432 && integer_zerop (arg1
))
10439 return constant_boolean_node (false, type
);
10443 return constant_boolean_node (true, type
);
10445 gcc_unreachable ();
10450 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10451 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10452 the resulting offset is smaller in absolute value than the
10453 original one and has the same sign. */
10454 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10455 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
10456 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10457 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10458 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
10459 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
10460 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10461 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
10463 tree const1
= TREE_OPERAND (arg0
, 1);
10464 tree const2
= TREE_OPERAND (arg1
, 1);
10465 tree variable1
= TREE_OPERAND (arg0
, 0);
10466 tree variable2
= TREE_OPERAND (arg1
, 0);
10468 const char * const warnmsg
= G_("assuming signed overflow does not "
10469 "occur when combining constants around "
10472 /* Put the constant on the side where it doesn't overflow and is
10473 of lower absolute value and of same sign than before. */
10474 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10475 ? MINUS_EXPR
: PLUS_EXPR
,
10477 if (!TREE_OVERFLOW (cst
)
10478 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
10479 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
10481 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10482 return fold_build2_loc (loc
, code
, type
,
10484 fold_build2_loc (loc
, TREE_CODE (arg1
),
10489 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10490 ? MINUS_EXPR
: PLUS_EXPR
,
10492 if (!TREE_OVERFLOW (cst
)
10493 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
10494 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
10496 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10497 return fold_build2_loc (loc
, code
, type
,
10498 fold_build2_loc (loc
, TREE_CODE (arg0
),
10505 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
10509 /* If we are comparing an expression that just has comparisons
10510 of two integer values, arithmetic expressions of those comparisons,
10511 and constants, we can simplify it. There are only three cases
10512 to check: the two values can either be equal, the first can be
10513 greater, or the second can be greater. Fold the expression for
10514 those three values. Since each value must be 0 or 1, we have
10515 eight possibilities, each of which corresponds to the constant 0
10516 or 1 or one of the six possible comparisons.
10518 This handles common cases like (a > b) == 0 but also handles
10519 expressions like ((x > y) - (y > x)) > 0, which supposedly
10520 occur in macroized code. */
10522 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
10524 tree cval1
= 0, cval2
= 0;
10526 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
10527 /* Don't handle degenerate cases here; they should already
10528 have been handled anyway. */
10529 && cval1
!= 0 && cval2
!= 0
10530 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
10531 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
10532 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
10533 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
10534 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
10535 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
10536 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
10538 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
10539 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
10541 /* We can't just pass T to eval_subst in case cval1 or cval2
10542 was the same as ARG1. */
10545 = fold_build2_loc (loc
, code
, type
,
10546 eval_subst (loc
, arg0
, cval1
, maxval
,
10550 = fold_build2_loc (loc
, code
, type
,
10551 eval_subst (loc
, arg0
, cval1
, maxval
,
10555 = fold_build2_loc (loc
, code
, type
,
10556 eval_subst (loc
, arg0
, cval1
, minval
,
10560 /* All three of these results should be 0 or 1. Confirm they are.
10561 Then use those values to select the proper code to use. */
10563 if (TREE_CODE (high_result
) == INTEGER_CST
10564 && TREE_CODE (equal_result
) == INTEGER_CST
10565 && TREE_CODE (low_result
) == INTEGER_CST
)
10567 /* Make a 3-bit mask with the high-order bit being the
10568 value for `>', the next for '=', and the low for '<'. */
10569 switch ((integer_onep (high_result
) * 4)
10570 + (integer_onep (equal_result
) * 2)
10571 + integer_onep (low_result
))
10574 /* Always false. */
10575 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10596 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10599 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
10608 /* Subroutine of fold_binary. Optimize complex multiplications of the
10609 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10610 argument EXPR represents the expression "z" of type TYPE. */
10613 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
10615 tree itype
= TREE_TYPE (type
);
10616 tree rpart
, ipart
, tem
;
10618 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
10620 rpart
= TREE_OPERAND (expr
, 0);
10621 ipart
= TREE_OPERAND (expr
, 1);
10623 else if (TREE_CODE (expr
) == COMPLEX_CST
)
10625 rpart
= TREE_REALPART (expr
);
10626 ipart
= TREE_IMAGPART (expr
);
10630 expr
= save_expr (expr
);
10631 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
10632 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
10635 rpart
= save_expr (rpart
);
10636 ipart
= save_expr (ipart
);
10637 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
10638 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
10639 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
10640 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
10641 build_zero_cst (itype
));
10645 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10646 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10647 true if successful. */
10650 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
10652 unsigned HOST_WIDE_INT i
, nunits
;
10654 if (TREE_CODE (arg
) == VECTOR_CST
10655 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
10657 for (i
= 0; i
< nunits
; ++i
)
10658 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
10660 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
10662 constructor_elt
*elt
;
10664 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
10665 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
10668 elts
[i
] = elt
->value
;
10672 for (; i
< nelts
; i
++)
10674 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
10678 /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10679 mask for VLA vec_perm folding.
10680 REASON if specified, will contain the reason why SEL is not suitable.
10681 Used only for debugging and unit-testing. */
10684 valid_mask_for_fold_vec_perm_cst_p (tree arg0
, tree arg1
,
10685 const vec_perm_indices
&sel
,
10686 const char **reason
= NULL
)
10688 unsigned sel_npatterns
= sel
.encoding ().npatterns ();
10689 unsigned sel_nelts_per_pattern
= sel
.encoding ().nelts_per_pattern ();
10691 if (!(pow2p_hwi (sel_npatterns
)
10692 && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0
))
10693 && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1
))))
10696 *reason
= "npatterns is not power of 2";
10700 /* We want to avoid cases where sel.length is not a multiple of npatterns.
10701 For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10703 if (!multiple_p (sel
.length (), sel_npatterns
, &esel
))
10706 *reason
= "sel.length is not multiple of sel_npatterns";
10710 if (sel_nelts_per_pattern
< 3)
10713 for (unsigned pattern
= 0; pattern
< sel_npatterns
; pattern
++)
10715 poly_uint64 a1
= sel
[pattern
+ sel_npatterns
];
10716 poly_uint64 a2
= sel
[pattern
+ 2 * sel_npatterns
];
10717 HOST_WIDE_INT step
;
10718 if (!poly_int64 (a2
- a1
).is_constant (&step
))
10721 *reason
= "step is not constant";
10724 // FIXME: Punt on step < 0 for now, revisit later.
10730 if (!pow2p_hwi (step
))
10733 *reason
= "step is not power of 2";
10737 /* Ensure that stepped sequence of the pattern selects elements
10738 only from the same input vector. */
10740 poly_uint64 r1
, re
;
10741 poly_uint64 ae
= a1
+ (esel
- 2) * step
;
10742 poly_uint64 arg_len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
10744 if (!(can_div_trunc_p (a1
, arg_len
, &q1
, &r1
)
10745 && can_div_trunc_p (ae
, arg_len
, &qe
, &re
)
10749 *reason
= "crossed input vectors";
10753 /* Ensure that the stepped sequence always selects from the same
10755 tree arg
= ((q1
& 1) == 0) ? arg0
: arg1
;
10756 unsigned arg_npatterns
= VECTOR_CST_NPATTERNS (arg
);
10758 if (!multiple_p (step
, arg_npatterns
))
10761 *reason
= "step is not multiple of npatterns";
10765 /* If a1 chooses base element from arg, ensure that it's a natural
10766 stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10767 to preserve arg's encoding. */
10769 if (maybe_lt (r1
, arg_npatterns
))
10771 unsigned HOST_WIDE_INT index
;
10772 if (!r1
.is_constant (&index
))
10775 tree arg_elem0
= vector_cst_elt (arg
, index
);
10776 tree arg_elem1
= vector_cst_elt (arg
, index
+ arg_npatterns
);
10777 tree arg_elem2
= vector_cst_elt (arg
, index
+ arg_npatterns
* 2);
10780 if (!(step1
= const_binop (MINUS_EXPR
, arg_elem1
, arg_elem0
))
10781 || !(step2
= const_binop (MINUS_EXPR
, arg_elem2
, arg_elem1
))
10782 || !operand_equal_p (step1
, step2
, 0))
10785 *reason
= "not a natural stepped sequence";
10794 /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10795 the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10796 REASON has same purpose as described in
10797 valid_mask_for_fold_vec_perm_cst_p. */
10800 fold_vec_perm_cst (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
,
10801 const char **reason
= NULL
)
10803 unsigned res_npatterns
, res_nelts_per_pattern
;
10804 unsigned HOST_WIDE_INT res_nelts
;
10806 /* First try to implement the fold in a VLA-friendly way.
10808 (1) If the selector is simply a duplication of N elements, the
10809 result is likewise a duplication of N elements.
10811 (2) If the selector is N elements followed by a duplication
10812 of N elements, the result is too.
10814 (3) If the selector is N elements followed by an interleaving
10815 of N linear series, the situation is more complex.
10817 valid_mask_for_fold_vec_perm_cst_p detects whether we
10818 can handle this case. If we can, then each of the N linear
10819 series either (a) selects the same element each time or
10820 (b) selects a linear series from one of the input patterns.
10822 If (b) holds for one of the linear series, the result
10823 will contain a linear series, and so the result will have
10824 the same shape as the selector. If (a) holds for all of
10825 the linear series, the result will be the same as (2) above.
10827 (b) can only hold if one of the input patterns has a
10828 stepped encoding. */
10830 if (valid_mask_for_fold_vec_perm_cst_p (arg0
, arg1
, sel
, reason
))
10832 res_npatterns
= sel
.encoding ().npatterns ();
10833 res_nelts_per_pattern
= sel
.encoding ().nelts_per_pattern ();
10834 if (res_nelts_per_pattern
== 3
10835 && VECTOR_CST_NELTS_PER_PATTERN (arg0
) < 3
10836 && VECTOR_CST_NELTS_PER_PATTERN (arg1
) < 3)
10837 res_nelts_per_pattern
= 2;
10838 res_nelts
= res_npatterns
* res_nelts_per_pattern
;
10840 else if (TYPE_VECTOR_SUBPARTS (type
).is_constant (&res_nelts
))
10842 res_npatterns
= res_nelts
;
10843 res_nelts_per_pattern
= 1;
10848 tree_vector_builder
out_elts (type
, res_npatterns
, res_nelts_per_pattern
);
10849 for (unsigned i
= 0; i
< res_nelts
; i
++)
10851 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
10854 unsigned HOST_WIDE_INT index
;
10856 /* Punt if sel[i] /trunc_div len cannot be determined,
10857 because the input vector to be chosen will depend on
10858 runtime vector length.
10859 For example if len == 4 + 4x, and sel[i] == 4,
10860 If len at runtime equals 4, we choose arg1[0].
10861 For any other value of len > 4 at runtime, we choose arg0[4].
10862 which makes the element choice dependent on runtime vector length. */
10863 if (!can_div_trunc_p (sel
[i
], len
, &q
, &r
))
10866 *reason
= "cannot divide selector element by arg len";
10870 /* sel[i] % len will give the index of element in the chosen input
10871 vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10872 we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10873 if (!r
.is_constant (&index
))
10876 *reason
= "remainder is not constant";
10880 tree arg
= ((q
& 1) == 0) ? arg0
: arg1
;
10881 tree elem
= vector_cst_elt (arg
, index
);
10882 out_elts
.quick_push (elem
);
10885 return out_elts
.build ();
10888 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10889 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10890 NULL_TREE otherwise. */
10893 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10896 unsigned HOST_WIDE_INT nelts
;
10898 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), sel
.length ())
10899 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
10900 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))));
10902 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10903 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10906 if (TREE_CODE (arg0
) == VECTOR_CST
10907 && TREE_CODE (arg1
) == VECTOR_CST
)
10908 return fold_vec_perm_cst (type
, arg0
, arg1
, sel
);
10910 /* For fall back case, we want to ensure we have VLS vectors
10911 with equal length. */
10912 if (!sel
.length ().is_constant (&nelts
))
10915 gcc_assert (known_eq (sel
.length (),
10916 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))));
10917 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10918 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10919 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10922 vec
<constructor_elt
, va_gc
> *v
;
10923 vec_alloc (v
, nelts
);
10924 for (i
= 0; i
< nelts
; i
++)
10926 HOST_WIDE_INT index
;
10927 if (!sel
[i
].is_constant (&index
))
10929 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, in_elts
[index
]);
10931 return build_constructor (type
, v
);
10934 /* Try to fold a pointer difference of type TYPE two address expressions of
10935 array references AREF0 and AREF1 using location LOC. Return a
10936 simplified expression for the difference or NULL_TREE. */
10939 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10940 tree aref0
, tree aref1
,
10941 bool use_pointer_diff
)
10943 tree base0
= TREE_OPERAND (aref0
, 0);
10944 tree base1
= TREE_OPERAND (aref1
, 0);
10945 tree base_offset
= build_int_cst (type
, 0);
10947 /* If the bases are array references as well, recurse. If the bases
10948 are pointer indirections compute the difference of the pointers.
10949 If the bases are equal, we are set. */
10950 if ((TREE_CODE (base0
) == ARRAY_REF
10951 && TREE_CODE (base1
) == ARRAY_REF
10953 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10954 use_pointer_diff
)))
10955 || (INDIRECT_REF_P (base0
)
10956 && INDIRECT_REF_P (base1
)
10959 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10960 TREE_OPERAND (base0
, 0),
10961 TREE_OPERAND (base1
, 0))
10962 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10963 fold_convert (type
,
10964 TREE_OPERAND (base0
, 0)),
10965 fold_convert (type
,
10966 TREE_OPERAND (base1
, 0)))))
10967 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10969 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10970 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10971 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10972 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10973 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10975 fold_build2_loc (loc
, MULT_EXPR
, type
,
10981 /* If the real or vector real constant CST of type TYPE has an exact
10982 inverse, return it, else return NULL. */
10985 exact_inverse (tree type
, tree cst
)
10991 switch (TREE_CODE (cst
))
10994 r
= TREE_REAL_CST (cst
);
10996 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10997 return build_real (type
, r
);
11003 unit_type
= TREE_TYPE (type
);
11004 mode
= TYPE_MODE (unit_type
);
11006 tree_vector_builder elts
;
11007 if (!elts
.new_unary_operation (type
, cst
, false))
11009 unsigned int count
= elts
.encoded_nelts ();
11010 for (unsigned int i
= 0; i
< count
; ++i
)
11012 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
11013 if (!exact_real_inverse (mode
, &r
))
11015 elts
.quick_push (build_real (unit_type
, r
));
11018 return elts
.build ();
11026 /* Mask out the tz least significant bits of X of type TYPE where
11027 tz is the number of trailing zeroes in Y. */
11029 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
11031 int tz
= wi::ctz (y
);
11033 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
11037 /* Return true when T is an address and is known to be nonzero.
11038 For floating point we further ensure that T is not denormal.
11039 Similar logic is present in nonzero_address in rtlanal.h.
11041 If the return value is based on the assumption that signed overflow
11042 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
11043 change *STRICT_OVERFLOW_P. */
11046 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
11048 tree type
= TREE_TYPE (t
);
11049 enum tree_code code
;
11051 /* Doing something useful for floating point would need more work. */
11052 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
11055 code
= TREE_CODE (t
);
11056 switch (TREE_CODE_CLASS (code
))
11059 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
11060 strict_overflow_p
);
11062 case tcc_comparison
:
11063 return tree_binary_nonzero_warnv_p (code
, type
,
11064 TREE_OPERAND (t
, 0),
11065 TREE_OPERAND (t
, 1),
11066 strict_overflow_p
);
11068 case tcc_declaration
:
11069 case tcc_reference
:
11070 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
11078 case TRUTH_NOT_EXPR
:
11079 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
11080 strict_overflow_p
);
11082 case TRUTH_AND_EXPR
:
11083 case TRUTH_OR_EXPR
:
11084 case TRUTH_XOR_EXPR
:
11085 return tree_binary_nonzero_warnv_p (code
, type
,
11086 TREE_OPERAND (t
, 0),
11087 TREE_OPERAND (t
, 1),
11088 strict_overflow_p
);
11094 case WITH_SIZE_EXPR
:
11096 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
11098 case COMPOUND_EXPR
:
11101 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
11102 strict_overflow_p
);
11105 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
11106 strict_overflow_p
);
11110 tree fndecl
= get_callee_fndecl (t
);
11111 if (!fndecl
) return false;
11112 if (flag_delete_null_pointer_checks
&& !flag_check_new
11113 && DECL_IS_OPERATOR_NEW_P (fndecl
)
11114 && !TREE_NOTHROW (fndecl
))
11116 if (flag_delete_null_pointer_checks
11117 && lookup_attribute ("returns_nonnull",
11118 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
11120 return alloca_call_p (t
);
11129 /* Return true when T is an address and is known to be nonzero.
11130 Handle warnings about undefined signed overflow. */
11133 tree_expr_nonzero_p (tree t
)
11135 bool ret
, strict_overflow_p
;
11137 strict_overflow_p
= false;
11138 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
11139 if (strict_overflow_p
)
11140 fold_overflow_warning (("assuming signed overflow does not occur when "
11141 "determining that expression is always "
11143 WARN_STRICT_OVERFLOW_MISC
);
11147 /* Return true if T is known not to be equal to an integer W. */
11150 expr_not_equal_to (tree t
, const wide_int
&w
)
11153 switch (TREE_CODE (t
))
11156 return wi::to_wide (t
) != w
;
11159 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
11162 get_range_query (cfun
)->range_of_expr (vr
, t
);
11163 if (!vr
.undefined_p () && !vr
.contains_p (w
))
11165 /* If T has some known zero bits and W has any of those bits set,
11166 then T is known not to be equal to W. */
11167 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
11168 TYPE_PRECISION (TREE_TYPE (t
))), 0))
11177 /* Fold a binary expression of code CODE and type TYPE with operands
11178 OP0 and OP1. LOC is the location of the resulting expression.
11179 Return the folded expression if folding is successful. Otherwise,
11180 return NULL_TREE. */
11183 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
11184 tree op0
, tree op1
)
11186 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11187 tree arg0
, arg1
, tem
;
11188 tree t1
= NULL_TREE
;
11189 bool strict_overflow_p
;
11192 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11193 && TREE_CODE_LENGTH (code
) == 2
11194 && op0
!= NULL_TREE
11195 && op1
!= NULL_TREE
);
11200 /* Strip any conversions that don't change the mode. This is
11201 safe for every expression, except for a comparison expression
11202 because its signedness is derived from its operands. So, in
11203 the latter case, only strip conversions that don't change the
11204 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
11207 Note that this is done as an internal manipulation within the
11208 constant folder, in order to find the simplest representation
11209 of the arguments so that their form can be studied. In any
11210 cases, the appropriate type conversions should be put back in
11211 the tree that will get out of the constant folder. */
11213 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
11215 STRIP_SIGN_NOPS (arg0
);
11216 STRIP_SIGN_NOPS (arg1
);
11224 /* Note that TREE_CONSTANT isn't enough: static var addresses are
11225 constant but we can't do arithmetic on them. */
11226 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
11228 tem
= const_binop (code
, type
, arg0
, arg1
);
11229 if (tem
!= NULL_TREE
)
11231 if (TREE_TYPE (tem
) != type
)
11232 tem
= fold_convert_loc (loc
, type
, tem
);
11237 /* If this is a commutative operation, and ARG0 is a constant, move it
11238 to ARG1 to reduce the number of tests below. */
11239 if (commutative_tree_code (code
)
11240 && tree_swap_operands_p (arg0
, arg1
))
11241 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
11243 /* Likewise if this is a comparison, and ARG0 is a constant, move it
11244 to ARG1 to reduce the number of tests below. */
11245 if (kind
== tcc_comparison
11246 && tree_swap_operands_p (arg0
, arg1
))
11247 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
11249 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
11253 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11255 First check for cases where an arithmetic operation is applied to a
11256 compound, conditional, or comparison operation. Push the arithmetic
11257 operation inside the compound or conditional to see if any folding
11258 can then be done. Convert comparison to conditional for this purpose.
11259 The also optimizes non-constant cases that used to be done in
11262 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11263 one of the operands is a comparison and the other is a comparison, a
11264 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11265 code below would make the expression more complex. Change it to a
11266 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11267 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11269 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
11270 || code
== EQ_EXPR
|| code
== NE_EXPR
)
11271 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
11272 && ((truth_value_p (TREE_CODE (arg0
))
11273 && (truth_value_p (TREE_CODE (arg1
))
11274 || (TREE_CODE (arg1
) == BIT_AND_EXPR
11275 && integer_onep (TREE_OPERAND (arg1
, 1)))))
11276 || (truth_value_p (TREE_CODE (arg1
))
11277 && (truth_value_p (TREE_CODE (arg0
))
11278 || (TREE_CODE (arg0
) == BIT_AND_EXPR
11279 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
11281 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
11282 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
11285 fold_convert_loc (loc
, boolean_type_node
, arg0
),
11286 fold_convert_loc (loc
, boolean_type_node
, arg1
));
11288 if (code
== EQ_EXPR
)
11289 tem
= invert_truthvalue_loc (loc
, tem
);
11291 return fold_convert_loc (loc
, type
, tem
);
11294 if (TREE_CODE_CLASS (code
) == tcc_binary
11295 || TREE_CODE_CLASS (code
) == tcc_comparison
)
11297 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
11299 tem
= fold_build2_loc (loc
, code
, type
,
11300 fold_convert_loc (loc
, TREE_TYPE (op0
),
11301 TREE_OPERAND (arg0
, 1)), op1
);
11302 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
11305 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
11307 tem
= fold_build2_loc (loc
, code
, type
, op0
,
11308 fold_convert_loc (loc
, TREE_TYPE (op1
),
11309 TREE_OPERAND (arg1
, 1)));
11310 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
11314 if (TREE_CODE (arg0
) == COND_EXPR
11315 || TREE_CODE (arg0
) == VEC_COND_EXPR
11316 || COMPARISON_CLASS_P (arg0
))
11318 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
11320 /*cond_first_p=*/1);
11321 if (tem
!= NULL_TREE
)
11325 if (TREE_CODE (arg1
) == COND_EXPR
11326 || TREE_CODE (arg1
) == VEC_COND_EXPR
11327 || COMPARISON_CLASS_P (arg1
))
11329 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
11331 /*cond_first_p=*/0);
11332 if (tem
!= NULL_TREE
)
11340 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11341 if (TREE_CODE (arg0
) == ADDR_EXPR
11342 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
11344 tree iref
= TREE_OPERAND (arg0
, 0);
11345 return fold_build2 (MEM_REF
, type
,
11346 TREE_OPERAND (iref
, 0),
11347 int_const_binop (PLUS_EXPR
, arg1
,
11348 TREE_OPERAND (iref
, 1)));
11351 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11352 if (TREE_CODE (arg0
) == ADDR_EXPR
11353 && handled_component_p (TREE_OPERAND (arg0
, 0)))
11356 poly_int64 coffset
;
11357 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
11361 return fold_build2 (MEM_REF
, type
,
11362 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
11363 int_const_binop (PLUS_EXPR
, arg1
,
11364 size_int (coffset
)));
11369 case POINTER_PLUS_EXPR
:
11370 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11371 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11372 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
11373 return fold_convert_loc (loc
, type
,
11374 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
11375 fold_convert_loc (loc
, sizetype
,
11377 fold_convert_loc (loc
, sizetype
,
11383 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
11385 /* X + (X / CST) * -CST is X % CST. */
11386 if (TREE_CODE (arg1
) == MULT_EXPR
11387 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
11388 && operand_equal_p (arg0
,
11389 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
11391 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
11392 tree cst1
= TREE_OPERAND (arg1
, 1);
11393 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
11395 if (sum
&& integer_zerop (sum
))
11396 return fold_convert_loc (loc
, type
,
11397 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
11398 TREE_TYPE (arg0
), arg0
,
11403 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11404 one. Make sure the type is not saturating and has the signedness of
11405 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11406 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11407 if ((TREE_CODE (arg0
) == MULT_EXPR
11408 || TREE_CODE (arg1
) == MULT_EXPR
)
11409 && !TYPE_SATURATING (type
)
11410 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11411 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11412 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11414 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11419 if (! FLOAT_TYPE_P (type
))
11421 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11422 (plus (plus (mult) (mult)) (foo)) so that we can
11423 take advantage of the factoring cases below. */
11424 if (ANY_INTEGRAL_TYPE_P (type
)
11425 && TYPE_OVERFLOW_WRAPS (type
)
11426 && (((TREE_CODE (arg0
) == PLUS_EXPR
11427 || TREE_CODE (arg0
) == MINUS_EXPR
)
11428 && TREE_CODE (arg1
) == MULT_EXPR
)
11429 || ((TREE_CODE (arg1
) == PLUS_EXPR
11430 || TREE_CODE (arg1
) == MINUS_EXPR
)
11431 && TREE_CODE (arg0
) == MULT_EXPR
)))
11433 tree parg0
, parg1
, parg
, marg
;
11434 enum tree_code pcode
;
11436 if (TREE_CODE (arg1
) == MULT_EXPR
)
11437 parg
= arg0
, marg
= arg1
;
11439 parg
= arg1
, marg
= arg0
;
11440 pcode
= TREE_CODE (parg
);
11441 parg0
= TREE_OPERAND (parg
, 0);
11442 parg1
= TREE_OPERAND (parg
, 1);
11443 STRIP_NOPS (parg0
);
11444 STRIP_NOPS (parg1
);
11446 if (TREE_CODE (parg0
) == MULT_EXPR
11447 && TREE_CODE (parg1
) != MULT_EXPR
)
11448 return fold_build2_loc (loc
, pcode
, type
,
11449 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11450 fold_convert_loc (loc
, type
,
11452 fold_convert_loc (loc
, type
,
11454 fold_convert_loc (loc
, type
, parg1
));
11455 if (TREE_CODE (parg0
) != MULT_EXPR
11456 && TREE_CODE (parg1
) == MULT_EXPR
)
11458 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11459 fold_convert_loc (loc
, type
, parg0
),
11460 fold_build2_loc (loc
, pcode
, type
,
11461 fold_convert_loc (loc
, type
, marg
),
11462 fold_convert_loc (loc
, type
,
11468 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11469 to __complex__ ( x, y ). This is not the same for SNaNs or
11470 if signed zeros are involved. */
11471 if (!HONOR_SNANS (arg0
)
11472 && !HONOR_SIGNED_ZEROS (arg0
)
11473 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11475 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11476 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11477 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11478 bool arg0rz
= false, arg0iz
= false;
11479 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11480 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11482 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11483 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11484 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11486 tree rp
= arg1r
? arg1r
11487 : build1 (REALPART_EXPR
, rtype
, arg1
);
11488 tree ip
= arg0i
? arg0i
11489 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11490 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11492 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11494 tree rp
= arg0r
? arg0r
11495 : build1 (REALPART_EXPR
, rtype
, arg0
);
11496 tree ip
= arg1i
? arg1i
11497 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11498 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11503 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11504 We associate floats only if the user has specified
11505 -fassociative-math. */
11506 if (flag_associative_math
11507 && TREE_CODE (arg1
) == PLUS_EXPR
11508 && TREE_CODE (arg0
) != MULT_EXPR
)
11510 tree tree10
= TREE_OPERAND (arg1
, 0);
11511 tree tree11
= TREE_OPERAND (arg1
, 1);
11512 if (TREE_CODE (tree11
) == MULT_EXPR
11513 && TREE_CODE (tree10
) == MULT_EXPR
)
11516 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11517 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11520 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11521 We associate floats only if the user has specified
11522 -fassociative-math. */
11523 if (flag_associative_math
11524 && TREE_CODE (arg0
) == PLUS_EXPR
11525 && TREE_CODE (arg1
) != MULT_EXPR
)
11527 tree tree00
= TREE_OPERAND (arg0
, 0);
11528 tree tree01
= TREE_OPERAND (arg0
, 1);
11529 if (TREE_CODE (tree01
) == MULT_EXPR
11530 && TREE_CODE (tree00
) == MULT_EXPR
)
11533 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11534 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11540 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11541 is a rotate of A by C1 bits. */
11542 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11543 is a rotate of A by B bits.
11544 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11545 though in this case CODE must be | and not + or ^, otherwise
11546 it doesn't return A when B is 0. */
11548 enum tree_code code0
, code1
;
11550 code0
= TREE_CODE (arg0
);
11551 code1
= TREE_CODE (arg1
);
11552 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11553 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11554 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11555 TREE_OPERAND (arg1
, 0), 0)
11556 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11557 TYPE_UNSIGNED (rtype
))
11558 /* Only create rotates in complete modes. Other cases are not
11559 expanded properly. */
11560 && (element_precision (rtype
)
11561 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11563 tree tree01
, tree11
;
11564 tree orig_tree01
, orig_tree11
;
11565 enum tree_code code01
, code11
;
11567 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11568 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11569 STRIP_NOPS (tree01
);
11570 STRIP_NOPS (tree11
);
11571 code01
= TREE_CODE (tree01
);
11572 code11
= TREE_CODE (tree11
);
11573 if (code11
!= MINUS_EXPR
11574 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11576 std::swap (code0
, code1
);
11577 std::swap (code01
, code11
);
11578 std::swap (tree01
, tree11
);
11579 std::swap (orig_tree01
, orig_tree11
);
11581 if (code01
== INTEGER_CST
11582 && code11
== INTEGER_CST
11583 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11584 == element_precision (rtype
)))
11586 tem
= build2_loc (loc
, LROTATE_EXPR
,
11587 rtype
, TREE_OPERAND (arg0
, 0),
11588 code0
== LSHIFT_EXPR
11589 ? orig_tree01
: orig_tree11
);
11590 return fold_convert_loc (loc
, type
, tem
);
11592 else if (code11
== MINUS_EXPR
)
11594 tree tree110
, tree111
;
11595 tree110
= TREE_OPERAND (tree11
, 0);
11596 tree111
= TREE_OPERAND (tree11
, 1);
11597 STRIP_NOPS (tree110
);
11598 STRIP_NOPS (tree111
);
11599 if (TREE_CODE (tree110
) == INTEGER_CST
11600 && compare_tree_int (tree110
,
11601 element_precision (rtype
)) == 0
11602 && operand_equal_p (tree01
, tree111
, 0))
11604 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11605 ? LROTATE_EXPR
: RROTATE_EXPR
),
11606 rtype
, TREE_OPERAND (arg0
, 0),
11608 return fold_convert_loc (loc
, type
, tem
);
11611 else if (code
== BIT_IOR_EXPR
11612 && code11
== BIT_AND_EXPR
11613 && pow2p_hwi (element_precision (rtype
)))
11615 tree tree110
, tree111
;
11616 tree110
= TREE_OPERAND (tree11
, 0);
11617 tree111
= TREE_OPERAND (tree11
, 1);
11618 STRIP_NOPS (tree110
);
11619 STRIP_NOPS (tree111
);
11620 if (TREE_CODE (tree110
) == NEGATE_EXPR
11621 && TREE_CODE (tree111
) == INTEGER_CST
11622 && compare_tree_int (tree111
,
11623 element_precision (rtype
) - 1) == 0
11624 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11626 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11627 ? LROTATE_EXPR
: RROTATE_EXPR
),
11628 rtype
, TREE_OPERAND (arg0
, 0),
11630 return fold_convert_loc (loc
, type
, tem
);
11637 /* In most languages, can't associate operations on floats through
11638 parentheses. Rather than remember where the parentheses were, we
11639 don't associate floats at all, unless the user has specified
11640 -fassociative-math.
11641 And, we need to make sure type is not saturating. */
11643 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11644 && !TYPE_SATURATING (type
)
11645 && !TYPE_OVERFLOW_SANITIZED (type
))
11647 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11648 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11652 /* Split both trees into variables, constants, and literals. Then
11653 associate each group together, the constants with literals,
11654 then the result with variables. This increases the chances of
11655 literals being recombined later and of generating relocatable
11656 expressions for the sum of a constant and literal. */
11657 var0
= split_tree (arg0
, type
, code
,
11658 &minus_var0
, &con0
, &minus_con0
,
11659 &lit0
, &minus_lit0
, 0);
11660 var1
= split_tree (arg1
, type
, code
,
11661 &minus_var1
, &con1
, &minus_con1
,
11662 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11664 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11665 if (code
== MINUS_EXPR
)
11668 /* With undefined overflow prefer doing association in a type
11669 which wraps on overflow, if that is one of the operand types. */
11670 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11671 && !TYPE_OVERFLOW_WRAPS (type
))
11673 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11674 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11675 atype
= TREE_TYPE (arg0
);
11676 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11677 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11678 atype
= TREE_TYPE (arg1
);
11679 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11682 /* With undefined overflow we can only associate constants with one
11683 variable, and constants whose association doesn't overflow. */
11684 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11685 && !TYPE_OVERFLOW_WRAPS (atype
))
11687 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11689 /* ??? If split_tree would handle NEGATE_EXPR we could
11690 simply reject these cases and the allowed cases would
11691 be the var0/minus_var1 ones. */
11692 tree tmp0
= var0
? var0
: minus_var0
;
11693 tree tmp1
= var1
? var1
: minus_var1
;
11694 bool one_neg
= false;
11696 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11698 tmp0
= TREE_OPERAND (tmp0
, 0);
11699 one_neg
= !one_neg
;
11701 if (CONVERT_EXPR_P (tmp0
)
11702 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11703 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11704 <= TYPE_PRECISION (atype
)))
11705 tmp0
= TREE_OPERAND (tmp0
, 0);
11706 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11708 tmp1
= TREE_OPERAND (tmp1
, 0);
11709 one_neg
= !one_neg
;
11711 if (CONVERT_EXPR_P (tmp1
)
11712 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11713 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11714 <= TYPE_PRECISION (atype
)))
11715 tmp1
= TREE_OPERAND (tmp1
, 0);
11716 /* The only case we can still associate with two variables
11717 is if they cancel out. */
11719 || !operand_equal_p (tmp0
, tmp1
, 0))
11722 else if ((var0
&& minus_var1
11723 && ! operand_equal_p (var0
, minus_var1
, 0))
11724 || (minus_var0
&& var1
11725 && ! operand_equal_p (minus_var0
, var1
, 0)))
11729 /* Only do something if we found more than two objects. Otherwise,
11730 nothing has changed and we risk infinite recursion. */
11732 && ((var0
!= 0) + (var1
!= 0)
11733 + (minus_var0
!= 0) + (minus_var1
!= 0)
11734 + (con0
!= 0) + (con1
!= 0)
11735 + (minus_con0
!= 0) + (minus_con1
!= 0)
11736 + (lit0
!= 0) + (lit1
!= 0)
11737 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11739 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11740 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11742 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11743 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11745 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11746 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11749 if (minus_var0
&& var0
)
11751 var0
= associate_trees (loc
, var0
, minus_var0
,
11752 MINUS_EXPR
, atype
);
11755 if (minus_con0
&& con0
)
11757 con0
= associate_trees (loc
, con0
, minus_con0
,
11758 MINUS_EXPR
, atype
);
11762 /* Preserve the MINUS_EXPR if the negative part of the literal is
11763 greater than the positive part. Otherwise, the multiplicative
11764 folding code (i.e extract_muldiv) may be fooled in case
11765 unsigned constants are subtracted, like in the following
11766 example: ((X*2 + 4) - 8U)/2. */
11767 if (minus_lit0
&& lit0
)
11769 if (TREE_CODE (lit0
) == INTEGER_CST
11770 && TREE_CODE (minus_lit0
) == INTEGER_CST
11771 && tree_int_cst_lt (lit0
, minus_lit0
)
11772 /* But avoid ending up with only negated parts. */
11775 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11776 MINUS_EXPR
, atype
);
11781 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11782 MINUS_EXPR
, atype
);
11787 /* Don't introduce overflows through reassociation. */
11788 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11789 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11792 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11793 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11795 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11799 /* Eliminate minus_con0. */
11803 con0
= associate_trees (loc
, con0
, minus_con0
,
11804 MINUS_EXPR
, atype
);
11806 var0
= associate_trees (loc
, var0
, minus_con0
,
11807 MINUS_EXPR
, atype
);
11809 gcc_unreachable ();
11813 /* Eliminate minus_var0. */
11817 con0
= associate_trees (loc
, con0
, minus_var0
,
11818 MINUS_EXPR
, atype
);
11820 gcc_unreachable ();
11825 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11832 case POINTER_DIFF_EXPR
:
11834 /* Fold &a[i] - &a[j] to i-j. */
11835 if (TREE_CODE (arg0
) == ADDR_EXPR
11836 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11837 && TREE_CODE (arg1
) == ADDR_EXPR
11838 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11840 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11841 TREE_OPERAND (arg0
, 0),
11842 TREE_OPERAND (arg1
, 0),
11844 == POINTER_DIFF_EXPR
);
11849 /* Further transformations are not for pointers. */
11850 if (code
== POINTER_DIFF_EXPR
)
11853 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11854 if (TREE_CODE (arg0
) == NEGATE_EXPR
11855 && negate_expr_p (op1
)
11856 /* If arg0 is e.g. unsigned int and type is int, then this could
11857 introduce UB, because if A is INT_MIN at runtime, the original
11858 expression can be well defined while the latter is not.
11860 && !(ANY_INTEGRAL_TYPE_P (type
)
11861 && TYPE_OVERFLOW_UNDEFINED (type
)
11862 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11863 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11864 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11865 fold_convert_loc (loc
, type
,
11866 TREE_OPERAND (arg0
, 0)));
11868 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11869 __complex__ ( x, -y ). This is not the same for SNaNs or if
11870 signed zeros are involved. */
11871 if (!HONOR_SNANS (arg0
)
11872 && !HONOR_SIGNED_ZEROS (arg0
)
11873 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11875 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11876 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11877 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11878 bool arg0rz
= false, arg0iz
= false;
11879 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11880 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11882 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11883 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11884 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11886 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11888 : build1 (REALPART_EXPR
, rtype
, arg1
));
11889 tree ip
= arg0i
? arg0i
11890 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11891 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11893 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11895 tree rp
= arg0r
? arg0r
11896 : build1 (REALPART_EXPR
, rtype
, arg0
);
11897 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11899 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11900 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11905 /* A - B -> A + (-B) if B is easily negatable. */
11906 if (negate_expr_p (op1
)
11907 && ! TYPE_OVERFLOW_SANITIZED (type
)
11908 && ((FLOAT_TYPE_P (type
)
11909 /* Avoid this transformation if B is a positive REAL_CST. */
11910 && (TREE_CODE (op1
) != REAL_CST
11911 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11912 || INTEGRAL_TYPE_P (type
)))
11913 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11914 fold_convert_loc (loc
, type
, arg0
),
11915 negate_expr (op1
));
11917 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11918 one. Make sure the type is not saturating and has the signedness of
11919 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11920 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11921 if ((TREE_CODE (arg0
) == MULT_EXPR
11922 || TREE_CODE (arg1
) == MULT_EXPR
)
11923 && !TYPE_SATURATING (type
)
11924 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11925 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11926 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11928 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11936 if (! FLOAT_TYPE_P (type
))
11938 /* Transform x * -C into -x * C if x is easily negatable. */
11939 if (TREE_CODE (op1
) == INTEGER_CST
11940 && tree_int_cst_sgn (op1
) == -1
11941 && negate_expr_p (op0
)
11942 && negate_expr_p (op1
)
11943 && (tem
= negate_expr (op1
)) != op1
11944 && ! TREE_OVERFLOW (tem
))
11945 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11946 fold_convert_loc (loc
, type
,
11947 negate_expr (op0
)), tem
);
11949 strict_overflow_p
= false;
11950 if (TREE_CODE (arg1
) == INTEGER_CST
11951 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11952 &strict_overflow_p
)) != 0)
11954 if (strict_overflow_p
)
11955 fold_overflow_warning (("assuming signed overflow does not "
11956 "occur when simplifying "
11958 WARN_STRICT_OVERFLOW_MISC
);
11959 return fold_convert_loc (loc
, type
, tem
);
11962 /* Optimize z * conj(z) for integer complex numbers. */
11963 if (TREE_CODE (arg0
) == CONJ_EXPR
11964 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11965 return fold_mult_zconjz (loc
, type
, arg1
);
11966 if (TREE_CODE (arg1
) == CONJ_EXPR
11967 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11968 return fold_mult_zconjz (loc
, type
, arg0
);
11972 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11973 This is not the same for NaNs or if signed zeros are
11975 if (!HONOR_NANS (arg0
)
11976 && !HONOR_SIGNED_ZEROS (arg0
)
11977 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11978 && TREE_CODE (arg1
) == COMPLEX_CST
11979 && real_zerop (TREE_REALPART (arg1
)))
11981 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11982 if (real_onep (TREE_IMAGPART (arg1
)))
11984 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11985 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11987 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11988 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11990 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11991 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11992 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11996 /* Optimize z * conj(z) for floating point complex numbers.
11997 Guarded by flag_unsafe_math_optimizations as non-finite
11998 imaginary components don't produce scalar results. */
11999 if (flag_unsafe_math_optimizations
12000 && TREE_CODE (arg0
) == CONJ_EXPR
12001 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12002 return fold_mult_zconjz (loc
, type
, arg1
);
12003 if (flag_unsafe_math_optimizations
12004 && TREE_CODE (arg1
) == CONJ_EXPR
12005 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12006 return fold_mult_zconjz (loc
, type
, arg0
);
12011 /* Canonicalize (X & C1) | C2. */
12012 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12013 && TREE_CODE (arg1
) == INTEGER_CST
12014 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12016 int width
= TYPE_PRECISION (type
), w
;
12017 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
12018 wide_int c2
= wi::to_wide (arg1
);
12020 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
12021 if ((c1
& c2
) == c1
)
12022 return omit_one_operand_loc (loc
, type
, arg1
,
12023 TREE_OPERAND (arg0
, 0));
12025 wide_int msk
= wi::mask (width
, false,
12026 TYPE_PRECISION (TREE_TYPE (arg1
)));
12028 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
12029 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
12031 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12032 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
12035 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
12036 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
12037 mode which allows further optimizations. */
12040 wide_int c3
= wi::bit_and_not (c1
, c2
);
12041 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
12043 wide_int mask
= wi::mask (w
, false,
12044 TYPE_PRECISION (type
));
12045 if (((c1
| c2
) & mask
) == mask
12046 && wi::bit_and_not (c1
, mask
) == 0)
12055 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12056 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
12057 wide_int_to_tree (type
, c3
));
12058 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
12062 /* See if this can be simplified into a rotate first. If that
12063 is unsuccessful continue in the association code. */
12067 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
12068 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12069 && INTEGRAL_TYPE_P (type
)
12070 && integer_onep (TREE_OPERAND (arg0
, 1))
12071 && integer_onep (arg1
))
12072 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
12073 build_zero_cst (TREE_TYPE (arg0
)));
12075 /* See if this can be simplified into a rotate first. If that
12076 is unsuccessful continue in the association code. */
12080 /* Fold !X & 1 as X == 0. */
12081 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12082 && integer_onep (arg1
))
12084 tem
= TREE_OPERAND (arg0
, 0);
12085 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
12086 build_zero_cst (TREE_TYPE (tem
)));
12089 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
12090 multiple of 1 << CST. */
12091 if (TREE_CODE (arg1
) == INTEGER_CST
)
12093 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12094 wide_int ncst1
= -cst1
;
12095 if ((cst1
& ncst1
) == ncst1
12096 && multiple_of_p (type
, arg0
,
12097 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
12098 return fold_convert_loc (loc
, type
, arg0
);
12101 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
12103 if (TREE_CODE (arg1
) == INTEGER_CST
12104 && TREE_CODE (arg0
) == MULT_EXPR
12105 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12107 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
12109 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
12112 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
12114 else if (masked
!= warg1
)
12116 /* Avoid the transform if arg1 is a mask of some
12117 mode which allows further optimizations. */
12118 int pop
= wi::popcount (warg1
);
12119 if (!(pop
>= BITS_PER_UNIT
12121 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
12122 return fold_build2_loc (loc
, code
, type
, op0
,
12123 wide_int_to_tree (type
, masked
));
12127 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
12128 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
12129 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
12131 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
12133 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
12136 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12142 /* Don't touch a floating-point divide by zero unless the mode
12143 of the constant can represent infinity. */
12144 if (TREE_CODE (arg1
) == REAL_CST
12145 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
12146 && real_zerop (arg1
))
12149 /* (-A) / (-B) -> A / B */
12150 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
12151 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12152 TREE_OPERAND (arg0
, 0),
12153 negate_expr (arg1
));
12154 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
12155 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12156 negate_expr (arg0
),
12157 TREE_OPERAND (arg1
, 0));
12160 case TRUNC_DIV_EXPR
:
12163 case FLOOR_DIV_EXPR
:
12164 /* Simplify A / (B << N) where A and B are positive and B is
12165 a power of 2, to A >> (N + log2(B)). */
12166 strict_overflow_p
= false;
12167 if (TREE_CODE (arg1
) == LSHIFT_EXPR
12168 && (TYPE_UNSIGNED (type
)
12169 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
12171 tree sval
= TREE_OPERAND (arg1
, 0);
12172 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
12174 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
12175 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
12176 wi::exact_log2 (wi::to_wide (sval
)));
12178 if (strict_overflow_p
)
12179 fold_overflow_warning (("assuming signed overflow does not "
12180 "occur when simplifying A / (B << N)"),
12181 WARN_STRICT_OVERFLOW_MISC
);
12183 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
12185 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
12186 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
12192 case ROUND_DIV_EXPR
:
12193 case CEIL_DIV_EXPR
:
12194 case EXACT_DIV_EXPR
:
12195 if (integer_zerop (arg1
))
12198 /* Convert -A / -B to A / B when the type is signed and overflow is
12200 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12201 && TREE_CODE (op0
) == NEGATE_EXPR
12202 && negate_expr_p (op1
))
12204 if (ANY_INTEGRAL_TYPE_P (type
))
12205 fold_overflow_warning (("assuming signed overflow does not occur "
12206 "when distributing negation across "
12208 WARN_STRICT_OVERFLOW_MISC
);
12209 return fold_build2_loc (loc
, code
, type
,
12210 fold_convert_loc (loc
, type
,
12211 TREE_OPERAND (arg0
, 0)),
12212 negate_expr (op1
));
12214 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12215 && TREE_CODE (arg1
) == NEGATE_EXPR
12216 && negate_expr_p (op0
))
12218 if (ANY_INTEGRAL_TYPE_P (type
))
12219 fold_overflow_warning (("assuming signed overflow does not occur "
12220 "when distributing negation across "
12222 WARN_STRICT_OVERFLOW_MISC
);
12223 return fold_build2_loc (loc
, code
, type
,
12225 fold_convert_loc (loc
, type
,
12226 TREE_OPERAND (arg1
, 0)));
12229 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12230 operation, EXACT_DIV_EXPR.
12232 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12233 At one time others generated faster code, it's not clear if they do
12234 after the last round to changes to the DIV code in expmed.cc. */
12235 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
12236 && multiple_of_p (type
, arg0
, arg1
))
12237 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
12238 fold_convert (type
, arg0
),
12239 fold_convert (type
, arg1
));
12241 strict_overflow_p
= false;
12242 if (TREE_CODE (arg1
) == INTEGER_CST
12243 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12244 &strict_overflow_p
)) != 0)
12246 if (strict_overflow_p
)
12247 fold_overflow_warning (("assuming signed overflow does not occur "
12248 "when simplifying division"),
12249 WARN_STRICT_OVERFLOW_MISC
);
12250 return fold_convert_loc (loc
, type
, tem
);
12255 case CEIL_MOD_EXPR
:
12256 case FLOOR_MOD_EXPR
:
12257 case ROUND_MOD_EXPR
:
12258 case TRUNC_MOD_EXPR
:
12259 strict_overflow_p
= false;
12260 if (TREE_CODE (arg1
) == INTEGER_CST
12261 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12262 &strict_overflow_p
)) != 0)
12264 if (strict_overflow_p
)
12265 fold_overflow_warning (("assuming signed overflow does not occur "
12266 "when simplifying modulus"),
12267 WARN_STRICT_OVERFLOW_MISC
);
12268 return fold_convert_loc (loc
, type
, tem
);
12277 /* Since negative shift count is not well-defined,
12278 don't try to compute it in the compiler. */
12279 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
12282 prec
= element_precision (type
);
12284 /* If we have a rotate of a bit operation with the rotate count and
12285 the second operand of the bit operation both constant,
12286 permute the two operations. */
12287 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12288 && (TREE_CODE (arg0
) == BIT_AND_EXPR
12289 || TREE_CODE (arg0
) == BIT_IOR_EXPR
12290 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
12291 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12293 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12294 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12295 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
12296 fold_build2_loc (loc
, code
, type
,
12298 fold_build2_loc (loc
, code
, type
,
12302 /* Two consecutive rotates adding up to the some integer
12303 multiple of the precision of the type can be ignored. */
12304 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12305 && TREE_CODE (arg0
) == RROTATE_EXPR
12306 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12307 && wi::umod_trunc (wi::to_wide (arg1
)
12308 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
12310 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12318 case TRUTH_ANDIF_EXPR
:
12319 /* Note that the operands of this must be ints
12320 and their values must be 0 or 1.
12321 ("true" is a fixed value perhaps depending on the language.) */
12322 /* If first arg is constant zero, return it. */
12323 if (integer_zerop (arg0
))
12324 return fold_convert_loc (loc
, type
, arg0
);
12326 case TRUTH_AND_EXPR
:
12327 /* If either arg is constant true, drop it. */
12328 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12329 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12330 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
12331 /* Preserve sequence points. */
12332 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12333 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12334 /* If second arg is constant zero, result is zero, but first arg
12335 must be evaluated. */
12336 if (integer_zerop (arg1
))
12337 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12338 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12339 case will be handled here. */
12340 if (integer_zerop (arg0
))
12341 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12343 /* !X && X is always false. */
12344 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12345 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12346 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12347 /* X && !X is always false. */
12348 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12349 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12350 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12352 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12353 means A >= Y && A != MAX, but in this case we know that
12356 if (!TREE_SIDE_EFFECTS (arg0
)
12357 && !TREE_SIDE_EFFECTS (arg1
))
12359 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12360 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12361 return fold_convert (type
,
12362 fold_build2_loc (loc
, code
, TREE_TYPE (arg1
),
12365 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12366 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12367 return fold_convert (type
,
12368 fold_build2_loc (loc
, code
, TREE_TYPE (arg0
),
12372 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12378 case TRUTH_ORIF_EXPR
:
12379 /* Note that the operands of this must be ints
12380 and their values must be 0 or true.
12381 ("true" is a fixed value perhaps depending on the language.) */
12382 /* If first arg is constant true, return it. */
12383 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12384 return fold_convert_loc (loc
, type
, arg0
);
12386 case TRUTH_OR_EXPR
:
12387 /* If either arg is constant zero, drop it. */
12388 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12389 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12390 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12391 /* Preserve sequence points. */
12392 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12393 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12394 /* If second arg is constant true, result is true, but we must
12395 evaluate first arg. */
12396 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12397 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12398 /* Likewise for first arg, but note this only occurs here for
12400 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12401 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12403 /* !X || X is always true. */
12404 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12405 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12406 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12407 /* X || !X is always true. */
12408 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12409 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12410 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12412 /* (X && !Y) || (!X && Y) is X ^ Y */
12413 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12414 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12416 tree a0
, a1
, l0
, l1
, n0
, n1
;
12418 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12419 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12421 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12422 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12424 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12425 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12427 if ((operand_equal_p (n0
, a0
, 0)
12428 && operand_equal_p (n1
, a1
, 0))
12429 || (operand_equal_p (n0
, a1
, 0)
12430 && operand_equal_p (n1
, a0
, 0)))
12431 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12434 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12440 case TRUTH_XOR_EXPR
:
12441 /* If the second arg is constant zero, drop it. */
12442 if (integer_zerop (arg1
))
12443 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12444 /* If the second arg is constant true, this is a logical inversion. */
12445 if (integer_onep (arg1
))
12447 tem
= invert_truthvalue_loc (loc
, arg0
);
12448 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12450 /* Identical arguments cancel to zero. */
12451 if (operand_equal_p (arg0
, arg1
, 0))
12452 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12454 /* !X ^ X is always true. */
12455 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12456 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12457 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12459 /* X ^ !X is always true. */
12460 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12461 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12462 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12471 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12472 if (tem
!= NULL_TREE
)
12475 /* bool_var != 1 becomes !bool_var. */
12476 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12477 && code
== NE_EXPR
)
12478 return fold_convert_loc (loc
, type
,
12479 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12480 TREE_TYPE (arg0
), arg0
));
12482 /* bool_var == 0 becomes !bool_var. */
12483 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12484 && code
== EQ_EXPR
)
12485 return fold_convert_loc (loc
, type
,
12486 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12487 TREE_TYPE (arg0
), arg0
));
12489 /* !exp != 0 becomes !exp */
12490 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12491 && code
== NE_EXPR
)
12492 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12494 /* If this is an EQ or NE comparison with zero and ARG0 is
12495 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12496 two operations, but the latter can be done in one less insn
12497 on machines that have only two-operand insns or on which a
12498 constant cannot be the first operand. */
12499 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12500 && integer_zerop (arg1
))
12502 tree arg00
= TREE_OPERAND (arg0
, 0);
12503 tree arg01
= TREE_OPERAND (arg0
, 1);
12504 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12505 && integer_onep (TREE_OPERAND (arg00
, 0)))
12507 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12508 arg01
, TREE_OPERAND (arg00
, 1));
12509 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12510 build_one_cst (TREE_TYPE (arg0
)));
12511 return fold_build2_loc (loc
, code
, type
,
12512 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12515 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12516 && integer_onep (TREE_OPERAND (arg01
, 0)))
12518 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12519 arg00
, TREE_OPERAND (arg01
, 1));
12520 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12521 build_one_cst (TREE_TYPE (arg0
)));
12522 return fold_build2_loc (loc
, code
, type
,
12523 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12528 /* If this is a comparison of a field, we may be able to simplify it. */
12529 if ((TREE_CODE (arg0
) == COMPONENT_REF
12530 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12531 /* Handle the constant case even without -O
12532 to make sure the warnings are given. */
12533 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12535 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12540 /* Optimize comparisons of strlen vs zero to a compare of the
12541 first character of the string vs zero. To wit,
12542 strlen(ptr) == 0 => *ptr == 0
12543 strlen(ptr) != 0 => *ptr != 0
12544 Other cases should reduce to one of these two (or a constant)
12545 due to the return value of strlen being unsigned. */
12546 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12548 tree fndecl
= get_callee_fndecl (arg0
);
12551 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12552 && call_expr_nargs (arg0
) == 1
12553 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12557 = build_pointer_type (build_qualified_type (char_type_node
,
12559 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12560 CALL_EXPR_ARG (arg0
, 0));
12561 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12562 return fold_build2_loc (loc
, code
, type
, iref
,
12563 build_int_cst (TREE_TYPE (iref
), 0));
12567 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12568 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12569 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12570 && integer_zerop (arg1
)
12571 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12573 tree arg00
= TREE_OPERAND (arg0
, 0);
12574 tree arg01
= TREE_OPERAND (arg0
, 1);
12575 tree itype
= TREE_TYPE (arg00
);
12576 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12578 if (TYPE_UNSIGNED (itype
))
12580 itype
= signed_type_for (itype
);
12581 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12583 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12584 type
, arg00
, build_zero_cst (itype
));
12588 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12589 (X & C) == 0 when C is a single bit. */
12590 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12591 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12592 && integer_zerop (arg1
)
12593 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12595 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12596 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12597 TREE_OPERAND (arg0
, 1));
12598 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12600 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12604 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12605 constant C is a power of two, i.e. a single bit. */
12606 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12607 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12608 && integer_zerop (arg1
)
12609 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12610 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12611 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12613 tree arg00
= TREE_OPERAND (arg0
, 0);
12614 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12615 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12618 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12619 when is C is a power of two, i.e. a single bit. */
12620 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12621 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12622 && integer_zerop (arg1
)
12623 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12624 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12625 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12627 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12628 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12629 arg000
, TREE_OPERAND (arg0
, 1));
12630 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12631 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12634 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12635 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12637 tree arg00
= TREE_OPERAND (arg0
, 0);
12638 tree arg01
= TREE_OPERAND (arg0
, 1);
12639 tree arg10
= TREE_OPERAND (arg1
, 0);
12640 tree arg11
= TREE_OPERAND (arg1
, 1);
12641 tree itype
= TREE_TYPE (arg0
);
12643 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12644 operand_equal_p guarantees no side-effects so we don't need
12645 to use omit_one_operand on Z. */
12646 if (operand_equal_p (arg01
, arg11
, 0))
12647 return fold_build2_loc (loc
, code
, type
, arg00
,
12648 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12650 if (operand_equal_p (arg01
, arg10
, 0))
12651 return fold_build2_loc (loc
, code
, type
, arg00
,
12652 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12654 if (operand_equal_p (arg00
, arg11
, 0))
12655 return fold_build2_loc (loc
, code
, type
, arg01
,
12656 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12658 if (operand_equal_p (arg00
, arg10
, 0))
12659 return fold_build2_loc (loc
, code
, type
, arg01
,
12660 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12663 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12664 if (TREE_CODE (arg01
) == INTEGER_CST
12665 && TREE_CODE (arg11
) == INTEGER_CST
)
12667 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12668 fold_convert_loc (loc
, itype
, arg11
));
12669 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12670 return fold_build2_loc (loc
, code
, type
, tem
,
12671 fold_convert_loc (loc
, itype
, arg10
));
12675 /* Attempt to simplify equality/inequality comparisons of complex
12676 values. Only lower the comparison if the result is known or
12677 can be simplified to a single scalar comparison. */
12678 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12679 || TREE_CODE (arg0
) == COMPLEX_CST
)
12680 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12681 || TREE_CODE (arg1
) == COMPLEX_CST
))
12683 tree real0
, imag0
, real1
, imag1
;
12686 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12688 real0
= TREE_OPERAND (arg0
, 0);
12689 imag0
= TREE_OPERAND (arg0
, 1);
12693 real0
= TREE_REALPART (arg0
);
12694 imag0
= TREE_IMAGPART (arg0
);
12697 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12699 real1
= TREE_OPERAND (arg1
, 0);
12700 imag1
= TREE_OPERAND (arg1
, 1);
12704 real1
= TREE_REALPART (arg1
);
12705 imag1
= TREE_IMAGPART (arg1
);
12708 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12709 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12711 if (integer_zerop (rcond
))
12713 if (code
== EQ_EXPR
)
12714 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12716 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12720 if (code
== NE_EXPR
)
12721 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12723 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12727 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12728 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12730 if (integer_zerop (icond
))
12732 if (code
== EQ_EXPR
)
12733 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12735 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12739 if (code
== NE_EXPR
)
12740 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12742 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12753 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12754 if (tem
!= NULL_TREE
)
12757 /* Transform comparisons of the form X +- C CMP X. */
12758 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12759 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12760 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12761 && !HONOR_SNANS (arg0
))
12763 tree arg01
= TREE_OPERAND (arg0
, 1);
12764 enum tree_code code0
= TREE_CODE (arg0
);
12765 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12767 /* (X - c) > X becomes false. */
12768 if (code
== GT_EXPR
12769 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12770 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12771 return constant_boolean_node (0, type
);
12773 /* Likewise (X + c) < X becomes false. */
12774 if (code
== LT_EXPR
12775 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12776 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12777 return constant_boolean_node (0, type
);
12779 /* Convert (X - c) <= X to true. */
12780 if (!HONOR_NANS (arg1
)
12782 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12783 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12784 return constant_boolean_node (1, type
);
12786 /* Convert (X + c) >= X to true. */
12787 if (!HONOR_NANS (arg1
)
12789 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12790 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12791 return constant_boolean_node (1, type
);
12794 /* If we are comparing an ABS_EXPR with a constant, we can
12795 convert all the cases into explicit comparisons, but they may
12796 well not be faster than doing the ABS and one comparison.
12797 But ABS (X) <= C is a range comparison, which becomes a subtraction
12798 and a comparison, and is probably faster. */
12799 if (code
== LE_EXPR
12800 && TREE_CODE (arg1
) == INTEGER_CST
12801 && TREE_CODE (arg0
) == ABS_EXPR
12802 && ! TREE_SIDE_EFFECTS (arg0
)
12803 && (tem
= negate_expr (arg1
)) != 0
12804 && TREE_CODE (tem
) == INTEGER_CST
12805 && !TREE_OVERFLOW (tem
))
12806 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12807 build2 (GE_EXPR
, type
,
12808 TREE_OPERAND (arg0
, 0), tem
),
12809 build2 (LE_EXPR
, type
,
12810 TREE_OPERAND (arg0
, 0), arg1
));
12812 /* Convert ABS_EXPR<x> >= 0 to true. */
12813 strict_overflow_p
= false;
12814 if (code
== GE_EXPR
12815 && (integer_zerop (arg1
)
12816 || (! HONOR_NANS (arg0
)
12817 && real_zerop (arg1
)))
12818 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12820 if (strict_overflow_p
)
12821 fold_overflow_warning (("assuming signed overflow does not occur "
12822 "when simplifying comparison of "
12823 "absolute value and zero"),
12824 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12825 return omit_one_operand_loc (loc
, type
,
12826 constant_boolean_node (true, type
),
12830 /* Convert ABS_EXPR<x> < 0 to false. */
12831 strict_overflow_p
= false;
12832 if (code
== LT_EXPR
12833 && (integer_zerop (arg1
) || real_zerop (arg1
))
12834 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12836 if (strict_overflow_p
)
12837 fold_overflow_warning (("assuming signed overflow does not occur "
12838 "when simplifying comparison of "
12839 "absolute value and zero"),
12840 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12841 return omit_one_operand_loc (loc
, type
,
12842 constant_boolean_node (false, type
),
12846 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12847 and similarly for >= into !=. */
12848 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12849 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12850 && TREE_CODE (arg1
) == LSHIFT_EXPR
12851 && integer_onep (TREE_OPERAND (arg1
, 0)))
12852 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12853 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12854 TREE_OPERAND (arg1
, 1)),
12855 build_zero_cst (TREE_TYPE (arg0
)));
12857 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12858 otherwise Y might be >= # of bits in X's type and thus e.g.
12859 (unsigned char) (1 << Y) for Y 15 might be 0.
12860 If the cast is widening, then 1 << Y should have unsigned type,
12861 otherwise if Y is number of bits in the signed shift type minus 1,
12862 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12863 31 might be 0xffffffff80000000. */
12864 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12865 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12866 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0
)))
12867 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12868 && CONVERT_EXPR_P (arg1
)
12869 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12870 && (element_precision (TREE_TYPE (arg1
))
12871 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12872 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12873 || (element_precision (TREE_TYPE (arg1
))
12874 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12875 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12877 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12878 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12879 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12880 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12881 build_zero_cst (TREE_TYPE (arg0
)));
12886 case UNORDERED_EXPR
:
12894 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12896 tree targ0
= strip_float_extensions (arg0
);
12897 tree targ1
= strip_float_extensions (arg1
);
12898 tree newtype
= TREE_TYPE (targ0
);
12900 if (element_precision (TREE_TYPE (targ1
)) > element_precision (newtype
))
12901 newtype
= TREE_TYPE (targ1
);
12903 if (element_precision (newtype
) < element_precision (TREE_TYPE (arg0
)))
12904 return fold_build2_loc (loc
, code
, type
,
12905 fold_convert_loc (loc
, newtype
, targ0
),
12906 fold_convert_loc (loc
, newtype
, targ1
));
12911 case COMPOUND_EXPR
:
12912 /* When pedantic, a compound expression can be neither an lvalue
12913 nor an integer constant expression. */
12914 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12916 /* Don't let (0, 0) be null pointer constant. */
12917 tem
= integer_zerop (arg1
) ? build1_loc (loc
, NOP_EXPR
, type
, arg1
)
12918 : fold_convert_loc (loc
, type
, arg1
);
12923 } /* switch (code) */
12926 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12927 ((A & N) + B) & M -> (A + B) & M
12928 Similarly if (N & M) == 0,
12929 ((A | N) + B) & M -> (A + B) & M
12930 and for - instead of + (or unary - instead of +)
12931 and/or ^ instead of |.
12932 If B is constant and (B & M) == 0, fold into A & M.
12934 This function is a helper for match.pd patterns. Return non-NULL
12935 type in which the simplified operation should be performed only
12936 if any optimization is possible.
12938 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12939 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12940 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12943 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12944 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12945 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12948 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12949 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12950 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12952 || (cst1
& (cst1
+ 1)) != 0
12953 || !INTEGRAL_TYPE_P (type
)
12954 || (!TYPE_OVERFLOW_WRAPS (type
)
12955 && TREE_CODE (type
) != INTEGER_TYPE
)
12956 || (wi::max_value (type
) & cst1
) != cst1
)
12959 enum tree_code codes
[2] = { code00
, code01
};
12960 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12964 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12965 arg1 (M) is == (1LL << cst) - 1.
12966 Store C into PMOP[0] and D into PMOP[1]. */
12969 which
= code
!= NEGATE_EXPR
;
12971 for (; which
>= 0; which
--)
12972 switch (codes
[which
])
12977 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12978 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12979 if (codes
[which
] == BIT_AND_EXPR
)
12984 else if (cst0
!= 0)
12986 /* If C or D is of the form (A & N) where
12987 (N & M) == M, or of the form (A | N) or
12988 (A ^ N) where (N & M) == 0, replace it with A. */
12989 pmop
[which
] = arg0xx
[2 * which
];
12992 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
12994 /* If C or D is a N where (N & M) == 0, it can be
12995 omitted (replaced with 0). */
12996 if ((code
== PLUS_EXPR
12997 || (code
== MINUS_EXPR
&& which
== 0))
12998 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
12999 pmop
[which
] = build_int_cst (type
, 0);
13000 /* Similarly, with C - N where (-N & M) == 0. */
13001 if (code
== MINUS_EXPR
13003 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
13004 pmop
[which
] = build_int_cst (type
, 0);
13007 gcc_unreachable ();
13010 /* Only build anything new if we optimized one or both arguments above. */
13011 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
13014 if (TYPE_OVERFLOW_WRAPS (type
))
13017 return unsigned_type_for (type
);
13020 /* Used by contains_label_[p1]. */
13022 struct contains_label_data
13024 hash_set
<tree
> *pset
;
13025 bool inside_switch_p
;
13028 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13029 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
13030 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
13033 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
13035 contains_label_data
*d
= (contains_label_data
*) data
;
13036 switch (TREE_CODE (*tp
))
13041 case CASE_LABEL_EXPR
:
13042 if (!d
->inside_switch_p
)
13047 if (!d
->inside_switch_p
)
13049 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
13051 d
->inside_switch_p
= true;
13052 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
13054 d
->inside_switch_p
= false;
13055 *walk_subtrees
= 0;
13060 *walk_subtrees
= 0;
13068 /* Return whether the sub-tree ST contains a label which is accessible from
13069 outside the sub-tree. */
13072 contains_label_p (tree st
)
13074 hash_set
<tree
> pset
;
13075 contains_label_data data
= { &pset
, false };
13076 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
13079 /* Fold a ternary expression of code CODE and type TYPE with operands
13080 OP0, OP1, and OP2. Return the folded expression if folding is
13081 successful. Otherwise, return NULL_TREE. */
13084 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
13085 tree op0
, tree op1
, tree op2
)
13088 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
13089 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13091 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
13092 && TREE_CODE_LENGTH (code
) == 3);
13094 /* If this is a commutative operation, and OP0 is a constant, move it
13095 to OP1 to reduce the number of tests below. */
13096 if (commutative_ternary_tree_code (code
)
13097 && tree_swap_operands_p (op0
, op1
))
13098 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
13100 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
13104 /* Strip any conversions that don't change the mode. This is safe
13105 for every expression, except for a comparison expression because
13106 its signedness is derived from its operands. So, in the latter
13107 case, only strip conversions that don't change the signedness.
13109 Note that this is done as an internal manipulation within the
13110 constant folder, in order to find the simplest representation of
13111 the arguments so that their form can be studied. In any cases,
13112 the appropriate type conversions should be put back in the tree
13113 that will get out of the constant folder. */
13134 case COMPONENT_REF
:
13135 if (TREE_CODE (arg0
) == CONSTRUCTOR
13136 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
13138 unsigned HOST_WIDE_INT idx
;
13140 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
13147 case VEC_COND_EXPR
:
13148 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13149 so all simple results must be passed through pedantic_non_lvalue. */
13150 if (TREE_CODE (arg0
) == INTEGER_CST
)
13152 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
13153 tem
= integer_zerop (arg0
) ? op2
: op1
;
13154 /* Only optimize constant conditions when the selected branch
13155 has the same type as the COND_EXPR. This avoids optimizing
13156 away "c ? x : throw", where the throw has a void type.
13157 Avoid throwing away that operand which contains label. */
13158 if ((!TREE_SIDE_EFFECTS (unused_op
)
13159 || !contains_label_p (unused_op
))
13160 && (! VOID_TYPE_P (TREE_TYPE (tem
))
13161 || VOID_TYPE_P (type
)))
13162 return protected_set_expr_location_unshare (tem
, loc
);
13165 else if (TREE_CODE (arg0
) == VECTOR_CST
)
13167 unsigned HOST_WIDE_INT nelts
;
13168 if ((TREE_CODE (arg1
) == VECTOR_CST
13169 || TREE_CODE (arg1
) == CONSTRUCTOR
)
13170 && (TREE_CODE (arg2
) == VECTOR_CST
13171 || TREE_CODE (arg2
) == CONSTRUCTOR
)
13172 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
13174 vec_perm_builder
sel (nelts
, nelts
, 1);
13175 for (unsigned int i
= 0; i
< nelts
; i
++)
13177 tree val
= VECTOR_CST_ELT (arg0
, i
);
13178 if (integer_all_onesp (val
))
13179 sel
.quick_push (i
);
13180 else if (integer_zerop (val
))
13181 sel
.quick_push (nelts
+ i
);
13182 else /* Currently unreachable. */
13185 vec_perm_indices
indices (sel
, 2, nelts
);
13186 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
13187 if (t
!= NULL_TREE
)
13192 /* If we have A op B ? A : C, we may be able to convert this to a
13193 simpler expression, depending on the operation and the values
13194 of B and C. Signed zeros prevent all of these transformations,
13195 for reasons given above each one.
13197 Also try swapping the arguments and inverting the conditional. */
13198 if (COMPARISON_CLASS_P (arg0
)
13199 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
13200 && !HONOR_SIGNED_ZEROS (op1
))
13202 tem
= fold_cond_expr_with_comparison (loc
, type
, TREE_CODE (arg0
),
13203 TREE_OPERAND (arg0
, 0),
13204 TREE_OPERAND (arg0
, 1),
13210 if (COMPARISON_CLASS_P (arg0
)
13211 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
13212 && !HONOR_SIGNED_ZEROS (op2
))
13214 enum tree_code comp_code
= TREE_CODE (arg0
);
13215 tree arg00
= TREE_OPERAND (arg0
, 0);
13216 tree arg01
= TREE_OPERAND (arg0
, 1);
13217 comp_code
= invert_tree_comparison (comp_code
, HONOR_NANS (arg00
));
13218 if (comp_code
!= ERROR_MARK
)
13219 tem
= fold_cond_expr_with_comparison (loc
, type
, comp_code
,
13227 /* If the second operand is simpler than the third, swap them
13228 since that produces better jump optimization results. */
13229 if (truth_value_p (TREE_CODE (arg0
))
13230 && tree_swap_operands_p (op1
, op2
))
13232 location_t loc0
= expr_location_or (arg0
, loc
);
13233 /* See if this can be inverted. If it can't, possibly because
13234 it was a floating-point inequality comparison, don't do
13236 tem
= fold_invert_truthvalue (loc0
, arg0
);
13238 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
13241 /* Convert A ? 1 : 0 to simply A. */
13242 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
13243 : (integer_onep (op1
)
13244 && !VECTOR_TYPE_P (type
)))
13245 && integer_zerop (op2
)
13246 /* If we try to convert OP0 to our type, the
13247 call to fold will try to move the conversion inside
13248 a COND, which will recurse. In that case, the COND_EXPR
13249 is probably the best choice, so leave it alone. */
13250 && type
== TREE_TYPE (arg0
))
13251 return protected_set_expr_location_unshare (arg0
, loc
);
13253 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13254 over COND_EXPR in cases such as floating point comparisons. */
13255 if (integer_zerop (op1
)
13256 && code
== COND_EXPR
13257 && integer_onep (op2
)
13258 && !VECTOR_TYPE_P (type
)
13259 && truth_value_p (TREE_CODE (arg0
)))
13260 return fold_convert_loc (loc
, type
,
13261 invert_truthvalue_loc (loc
, arg0
));
13263 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13264 if (TREE_CODE (arg0
) == LT_EXPR
13265 && integer_zerop (TREE_OPERAND (arg0
, 1))
13266 && integer_zerop (op2
)
13267 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
13269 /* sign_bit_p looks through both zero and sign extensions,
13270 but for this optimization only sign extensions are
13272 tree tem2
= TREE_OPERAND (arg0
, 0);
13273 while (tem
!= tem2
)
13275 if (TREE_CODE (tem2
) != NOP_EXPR
13276 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
13281 tem2
= TREE_OPERAND (tem2
, 0);
13283 /* sign_bit_p only checks ARG1 bits within A's precision.
13284 If <sign bit of A> has wider type than A, bits outside
13285 of A's precision in <sign bit of A> need to be checked.
13286 If they are all 0, this optimization needs to be done
13287 in unsigned A's type, if they are all 1 in signed A's type,
13288 otherwise this can't be done. */
13290 && TYPE_PRECISION (TREE_TYPE (tem
))
13291 < TYPE_PRECISION (TREE_TYPE (arg1
))
13292 && TYPE_PRECISION (TREE_TYPE (tem
))
13293 < TYPE_PRECISION (type
))
13295 int inner_width
, outer_width
;
13298 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
13299 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
13300 if (outer_width
> TYPE_PRECISION (type
))
13301 outer_width
= TYPE_PRECISION (type
);
13303 wide_int mask
= wi::shifted_mask
13304 (inner_width
, outer_width
- inner_width
, false,
13305 TYPE_PRECISION (TREE_TYPE (arg1
)));
13307 wide_int common
= mask
& wi::to_wide (arg1
);
13308 if (common
== mask
)
13310 tem_type
= signed_type_for (TREE_TYPE (tem
));
13311 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13313 else if (common
== 0)
13315 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
13316 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13324 fold_convert_loc (loc
, type
,
13325 fold_build2_loc (loc
, BIT_AND_EXPR
,
13326 TREE_TYPE (tem
), tem
,
13327 fold_convert_loc (loc
,
13332 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13333 already handled above. */
13334 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13335 && integer_onep (TREE_OPERAND (arg0
, 1))
13336 && integer_zerop (op2
)
13337 && integer_pow2p (arg1
))
13339 tree tem
= TREE_OPERAND (arg0
, 0);
13341 if (TREE_CODE (tem
) == RSHIFT_EXPR
13342 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
13343 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
13344 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
13345 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
13346 fold_convert_loc (loc
, type
,
13347 TREE_OPERAND (tem
, 0)),
13351 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13352 is probably obsolete because the first operand should be a
13353 truth value (that's why we have the two cases above), but let's
13354 leave it in until we can confirm this for all front-ends. */
13355 if (integer_zerop (op2
)
13356 && TREE_CODE (arg0
) == NE_EXPR
13357 && integer_zerop (TREE_OPERAND (arg0
, 1))
13358 && integer_pow2p (arg1
)
13359 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13360 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13361 arg1
, OEP_ONLY_CONST
)
13362 /* operand_equal_p compares just value, not precision, so e.g.
13363 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13364 second operand 32-bit -128, which is not a power of two (or vice
13366 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
13367 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
13369 /* Disable the transformations below for vectors, since
13370 fold_binary_op_with_conditional_arg may undo them immediately,
13371 yielding an infinite loop. */
13372 if (code
== VEC_COND_EXPR
)
13375 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13376 if (integer_zerop (op2
)
13377 && truth_value_p (TREE_CODE (arg0
))
13378 && truth_value_p (TREE_CODE (arg1
))
13379 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13380 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13381 : TRUTH_ANDIF_EXPR
,
13382 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13384 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13385 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13386 && truth_value_p (TREE_CODE (arg0
))
13387 && truth_value_p (TREE_CODE (arg1
))
13388 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13390 location_t loc0
= expr_location_or (arg0
, loc
);
13391 /* Only perform transformation if ARG0 is easily inverted. */
13392 tem
= fold_invert_truthvalue (loc0
, arg0
);
13394 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13397 type
, fold_convert_loc (loc
, type
, tem
),
13401 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13402 if (integer_zerop (arg1
)
13403 && truth_value_p (TREE_CODE (arg0
))
13404 && truth_value_p (TREE_CODE (op2
))
13405 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13407 location_t loc0
= expr_location_or (arg0
, loc
);
13408 /* Only perform transformation if ARG0 is easily inverted. */
13409 tem
= fold_invert_truthvalue (loc0
, arg0
);
13411 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13412 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13413 type
, fold_convert_loc (loc
, type
, tem
),
13417 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13418 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13419 && truth_value_p (TREE_CODE (arg0
))
13420 && truth_value_p (TREE_CODE (op2
))
13421 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13422 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13423 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13424 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13429 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13430 of fold_ternary on them. */
13431 gcc_unreachable ();
13433 case BIT_FIELD_REF
:
13434 if (TREE_CODE (arg0
) == VECTOR_CST
13435 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13436 || (VECTOR_TYPE_P (type
)
13437 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13438 && tree_fits_uhwi_p (op1
)
13439 && tree_fits_uhwi_p (op2
))
13441 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13442 unsigned HOST_WIDE_INT width
13443 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13444 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13445 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13446 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13449 && (idx
% width
) == 0
13450 && (n
% width
) == 0
13451 && known_le ((idx
+ n
) / width
,
13452 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13457 if (TREE_CODE (arg0
) == VECTOR_CST
)
13461 tem
= VECTOR_CST_ELT (arg0
, idx
);
13462 if (VECTOR_TYPE_P (type
))
13463 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13467 tree_vector_builder
vals (type
, n
, 1);
13468 for (unsigned i
= 0; i
< n
; ++i
)
13469 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13470 return vals
.build ();
13475 /* On constants we can use native encode/interpret to constant
13476 fold (nearly) all BIT_FIELD_REFs. */
13477 if (CONSTANT_CLASS_P (arg0
)
13478 && can_native_interpret_type_p (type
)
13479 && BITS_PER_UNIT
== 8
13480 && tree_fits_uhwi_p (op1
)
13481 && tree_fits_uhwi_p (op2
))
13483 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13484 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13485 /* Limit us to a reasonable amount of work. To relax the
13486 other limitations we need bit-shifting of the buffer
13487 and rounding up the size. */
13488 if (bitpos
% BITS_PER_UNIT
== 0
13489 && bitsize
% BITS_PER_UNIT
== 0
13490 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13492 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13493 unsigned HOST_WIDE_INT len
13494 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13495 bitpos
/ BITS_PER_UNIT
);
13497 && len
* BITS_PER_UNIT
>= bitsize
)
13499 tree v
= native_interpret_expr (type
, b
,
13500 bitsize
/ BITS_PER_UNIT
);
13509 case VEC_PERM_EXPR
:
13510 /* Perform constant folding of BIT_INSERT_EXPR. */
13511 if (TREE_CODE (arg2
) == VECTOR_CST
13512 && TREE_CODE (op0
) == VECTOR_CST
13513 && TREE_CODE (op1
) == VECTOR_CST
)
13515 /* Build a vector of integers from the tree mask. */
13516 vec_perm_builder builder
;
13517 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13520 /* Create a vec_perm_indices for the integer vector. */
13521 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13522 bool single_arg
= (op0
== op1
);
13523 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13524 return fold_vec_perm (type
, op0
, op1
, sel
);
13528 case BIT_INSERT_EXPR
:
13529 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13530 if (TREE_CODE (arg0
) == INTEGER_CST
13531 && TREE_CODE (arg1
) == INTEGER_CST
)
13533 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13534 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13535 wide_int tem
= (wi::to_wide (arg0
)
13536 & wi::shifted_mask (bitpos
, bitsize
, true,
13537 TYPE_PRECISION (type
)));
13539 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13541 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13543 else if (TREE_CODE (arg0
) == VECTOR_CST
13544 && CONSTANT_CLASS_P (arg1
)
13545 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13548 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13549 unsigned HOST_WIDE_INT elsize
13550 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13551 if (bitpos
% elsize
== 0)
13553 unsigned k
= bitpos
/ elsize
;
13554 unsigned HOST_WIDE_INT nelts
;
13555 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13557 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13559 tree_vector_builder
elts (type
, nelts
, 1);
13560 elts
.quick_grow (nelts
);
13561 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13562 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13563 return elts
.build ();
13571 } /* switch (code) */
13574 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13575 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13576 constructor element index of the value returned. If the element is
13577 not found NULL_TREE is returned and *CTOR_IDX is updated to
13578 the index of the element after the ACCESS_INDEX position (which
13579 may be outside of the CTOR array). */
13582 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13583 unsigned *ctor_idx
)
13585 tree index_type
= NULL_TREE
;
13586 signop index_sgn
= UNSIGNED
;
13587 offset_int low_bound
= 0;
13589 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13591 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13592 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13594 /* Static constructors for variably sized objects makes no sense. */
13595 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13596 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13597 /* ??? When it is obvious that the range is signed, treat it so. */
13598 if (TYPE_UNSIGNED (index_type
)
13599 && TYPE_MAX_VALUE (domain_type
)
13600 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13601 TYPE_MIN_VALUE (domain_type
)))
13603 index_sgn
= SIGNED
;
13605 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13610 index_sgn
= TYPE_SIGN (index_type
);
13611 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13617 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13620 offset_int index
= low_bound
;
13622 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13624 offset_int max_index
= index
;
13627 bool first_p
= true;
13629 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13631 /* Array constructor might explicitly set index, or specify a range,
13632 or leave index NULL meaning that it is next index after previous
13636 if (TREE_CODE (cfield
) == INTEGER_CST
)
13638 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13641 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13642 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13645 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13647 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13652 index
= max_index
+ 1;
13654 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13655 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13661 /* Do we have match? */
13662 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13664 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13671 else if (in_gimple_form
)
13672 /* We're past the element we search for. Note during parsing
13673 the elements might not be sorted.
13674 ??? We should use a binary search and a flag on the
13675 CONSTRUCTOR as to whether elements are sorted in declaration
13684 /* Perform constant folding and related simplification of EXPR.
13685 The related simplifications include x*1 => x, x*0 => 0, etc.,
13686 and application of the associative law.
13687 NOP_EXPR conversions may be removed freely (as long as we
13688 are careful not to change the type of the overall expression).
13689 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13690 but we can constant-fold them if they have constant operands. */
13692 #ifdef ENABLE_FOLD_CHECKING
13693 # define fold(x) fold_1 (x)
13694 static tree
fold_1 (tree
);
13700 const tree t
= expr
;
13701 enum tree_code code
= TREE_CODE (t
);
13702 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13704 location_t loc
= EXPR_LOCATION (expr
);
13706 /* Return right away if a constant. */
13707 if (kind
== tcc_constant
)
13710 /* CALL_EXPR-like objects with variable numbers of operands are
13711 treated specially. */
13712 if (kind
== tcc_vl_exp
)
13714 if (code
== CALL_EXPR
)
13716 tem
= fold_call_expr (loc
, expr
, false);
13717 return tem
? tem
: expr
;
13722 if (IS_EXPR_CODE_CLASS (kind
))
13724 tree type
= TREE_TYPE (t
);
13725 tree op0
, op1
, op2
;
13727 switch (TREE_CODE_LENGTH (code
))
13730 op0
= TREE_OPERAND (t
, 0);
13731 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13732 return tem
? tem
: expr
;
13734 op0
= TREE_OPERAND (t
, 0);
13735 op1
= TREE_OPERAND (t
, 1);
13736 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13737 return tem
? tem
: expr
;
13739 op0
= TREE_OPERAND (t
, 0);
13740 op1
= TREE_OPERAND (t
, 1);
13741 op2
= TREE_OPERAND (t
, 2);
13742 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13743 return tem
? tem
: expr
;
13753 tree op0
= TREE_OPERAND (t
, 0);
13754 tree op1
= TREE_OPERAND (t
, 1);
13756 if (TREE_CODE (op1
) == INTEGER_CST
13757 && TREE_CODE (op0
) == CONSTRUCTOR
13758 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13760 tree val
= get_array_ctor_element_at_index (op0
,
13761 wi::to_offset (op1
));
13769 /* Return a VECTOR_CST if possible. */
13772 tree type
= TREE_TYPE (t
);
13773 if (TREE_CODE (type
) != VECTOR_TYPE
)
13778 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13779 if (! CONSTANT_CLASS_P (val
))
13782 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13786 return fold (DECL_INITIAL (t
));
13790 } /* switch (code) */
13793 #ifdef ENABLE_FOLD_CHECKING
13796 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13797 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13798 static void fold_check_failed (const_tree
, const_tree
);
13799 void print_fold_checksum (const_tree
);
13801 /* When --enable-checking=fold, compute a digest of expr before
13802 and after actual fold call to see if fold did not accidentally
13803 change original expr. */
13809 struct md5_ctx ctx
;
13810 unsigned char checksum_before
[16], checksum_after
[16];
13811 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13813 md5_init_ctx (&ctx
);
13814 fold_checksum_tree (expr
, &ctx
, &ht
);
13815 md5_finish_ctx (&ctx
, checksum_before
);
13818 ret
= fold_1 (expr
);
13820 md5_init_ctx (&ctx
);
13821 fold_checksum_tree (expr
, &ctx
, &ht
);
13822 md5_finish_ctx (&ctx
, checksum_after
);
13824 if (memcmp (checksum_before
, checksum_after
, 16))
13825 fold_check_failed (expr
, ret
);
13831 print_fold_checksum (const_tree expr
)
13833 struct md5_ctx ctx
;
13834 unsigned char checksum
[16], cnt
;
13835 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13837 md5_init_ctx (&ctx
);
13838 fold_checksum_tree (expr
, &ctx
, &ht
);
13839 md5_finish_ctx (&ctx
, checksum
);
13840 for (cnt
= 0; cnt
< 16; ++cnt
)
13841 fprintf (stderr
, "%02x", checksum
[cnt
]);
13842 putc ('\n', stderr
);
13846 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13848 internal_error ("fold check: original tree changed by fold");
13852 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13853 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13855 const tree_node
**slot
;
13856 enum tree_code code
;
13857 union tree_node
*buf
;
13863 slot
= ht
->find_slot (expr
, INSERT
);
13867 code
= TREE_CODE (expr
);
13868 if (TREE_CODE_CLASS (code
) == tcc_declaration
13869 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13871 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13872 size_t sz
= tree_size (expr
);
13873 buf
= XALLOCAVAR (union tree_node
, sz
);
13874 memcpy ((char *) buf
, expr
, sz
);
13875 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13876 buf
->decl_with_vis
.symtab_node
= NULL
;
13877 buf
->base
.nowarning_flag
= 0;
13880 else if (TREE_CODE_CLASS (code
) == tcc_type
13881 && (TYPE_POINTER_TO (expr
)
13882 || TYPE_REFERENCE_TO (expr
)
13883 || TYPE_CACHED_VALUES_P (expr
)
13884 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13885 || TYPE_NEXT_VARIANT (expr
)
13886 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13888 /* Allow these fields to be modified. */
13890 size_t sz
= tree_size (expr
);
13891 buf
= XALLOCAVAR (union tree_node
, sz
);
13892 memcpy ((char *) buf
, expr
, sz
);
13893 expr
= tmp
= (tree
) buf
;
13894 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13895 TYPE_POINTER_TO (tmp
) = NULL
;
13896 TYPE_REFERENCE_TO (tmp
) = NULL
;
13897 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13898 TYPE_ALIAS_SET (tmp
) = -1;
13899 if (TYPE_CACHED_VALUES_P (tmp
))
13901 TYPE_CACHED_VALUES_P (tmp
) = 0;
13902 TYPE_CACHED_VALUES (tmp
) = NULL
;
13905 else if (warning_suppressed_p (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13907 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13908 that and change builtins.cc etc. instead - see PR89543. */
13909 size_t sz
= tree_size (expr
);
13910 buf
= XALLOCAVAR (union tree_node
, sz
);
13911 memcpy ((char *) buf
, expr
, sz
);
13912 buf
->base
.nowarning_flag
= 0;
13915 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13916 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13917 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13918 if (TREE_CODE_CLASS (code
) != tcc_type
13919 && TREE_CODE_CLASS (code
) != tcc_declaration
13920 && code
!= TREE_LIST
13921 && code
!= SSA_NAME
13922 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13923 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13924 switch (TREE_CODE_CLASS (code
))
13930 md5_process_bytes (TREE_STRING_POINTER (expr
),
13931 TREE_STRING_LENGTH (expr
), ctx
);
13934 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13935 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13938 len
= vector_cst_encoded_nelts (expr
);
13939 for (i
= 0; i
< len
; ++i
)
13940 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13946 case tcc_exceptional
:
13950 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13951 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13952 expr
= TREE_CHAIN (expr
);
13953 goto recursive_label
;
13956 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13957 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13963 case tcc_expression
:
13964 case tcc_reference
:
13965 case tcc_comparison
:
13968 case tcc_statement
:
13970 len
= TREE_OPERAND_LENGTH (expr
);
13971 for (i
= 0; i
< len
; ++i
)
13972 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13974 case tcc_declaration
:
13975 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13976 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13977 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13979 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13980 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13981 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13982 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13983 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13986 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13988 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13990 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13991 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13993 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13997 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13998 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13999 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
14000 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
14001 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
14002 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
14003 if (INTEGRAL_TYPE_P (expr
)
14004 || SCALAR_FLOAT_TYPE_P (expr
))
14006 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
14007 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
14009 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
14010 if (RECORD_OR_UNION_TYPE_P (expr
))
14011 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
14012 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
14019 /* Helper function for outputting the checksum of a tree T. When
14020 debugging with gdb, you can "define mynext" to be "next" followed
14021 by "call debug_fold_checksum (op0)", then just trace down till the
14024 DEBUG_FUNCTION
void
14025 debug_fold_checksum (const_tree t
)
14028 unsigned char checksum
[16];
14029 struct md5_ctx ctx
;
14030 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14032 md5_init_ctx (&ctx
);
14033 fold_checksum_tree (t
, &ctx
, &ht
);
14034 md5_finish_ctx (&ctx
, checksum
);
14037 for (i
= 0; i
< 16; i
++)
14038 fprintf (stderr
, "%d ", checksum
[i
]);
14040 fprintf (stderr
, "\n");
14045 /* Fold a unary tree expression with code CODE of type TYPE with an
14046 operand OP0. LOC is the location of the resulting expression.
14047 Return a folded expression if successful. Otherwise, return a tree
14048 expression with code CODE of type TYPE with an operand OP0. */
14051 fold_build1_loc (location_t loc
,
14052 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
14055 #ifdef ENABLE_FOLD_CHECKING
14056 unsigned char checksum_before
[16], checksum_after
[16];
14057 struct md5_ctx ctx
;
14058 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14060 md5_init_ctx (&ctx
);
14061 fold_checksum_tree (op0
, &ctx
, &ht
);
14062 md5_finish_ctx (&ctx
, checksum_before
);
14066 tem
= fold_unary_loc (loc
, code
, type
, op0
);
14068 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
14070 #ifdef ENABLE_FOLD_CHECKING
14071 md5_init_ctx (&ctx
);
14072 fold_checksum_tree (op0
, &ctx
, &ht
);
14073 md5_finish_ctx (&ctx
, checksum_after
);
14075 if (memcmp (checksum_before
, checksum_after
, 16))
14076 fold_check_failed (op0
, tem
);
14081 /* Fold a binary tree expression with code CODE of type TYPE with
14082 operands OP0 and OP1. LOC is the location of the resulting
14083 expression. Return a folded expression if successful. Otherwise,
14084 return a tree expression with code CODE of type TYPE with operands
14088 fold_build2_loc (location_t loc
,
14089 enum tree_code code
, tree type
, tree op0
, tree op1
14093 #ifdef ENABLE_FOLD_CHECKING
14094 unsigned char checksum_before_op0
[16],
14095 checksum_before_op1
[16],
14096 checksum_after_op0
[16],
14097 checksum_after_op1
[16];
14098 struct md5_ctx ctx
;
14099 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14101 md5_init_ctx (&ctx
);
14102 fold_checksum_tree (op0
, &ctx
, &ht
);
14103 md5_finish_ctx (&ctx
, checksum_before_op0
);
14106 md5_init_ctx (&ctx
);
14107 fold_checksum_tree (op1
, &ctx
, &ht
);
14108 md5_finish_ctx (&ctx
, checksum_before_op1
);
14112 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
14114 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
14116 #ifdef ENABLE_FOLD_CHECKING
14117 md5_init_ctx (&ctx
);
14118 fold_checksum_tree (op0
, &ctx
, &ht
);
14119 md5_finish_ctx (&ctx
, checksum_after_op0
);
14122 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14123 fold_check_failed (op0
, tem
);
14125 md5_init_ctx (&ctx
);
14126 fold_checksum_tree (op1
, &ctx
, &ht
);
14127 md5_finish_ctx (&ctx
, checksum_after_op1
);
14129 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14130 fold_check_failed (op1
, tem
);
14135 /* Fold a ternary tree expression with code CODE of type TYPE with
14136 operands OP0, OP1, and OP2. Return a folded expression if
14137 successful. Otherwise, return a tree expression with code CODE of
14138 type TYPE with operands OP0, OP1, and OP2. */
14141 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
14142 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
14145 #ifdef ENABLE_FOLD_CHECKING
14146 unsigned char checksum_before_op0
[16],
14147 checksum_before_op1
[16],
14148 checksum_before_op2
[16],
14149 checksum_after_op0
[16],
14150 checksum_after_op1
[16],
14151 checksum_after_op2
[16];
14152 struct md5_ctx ctx
;
14153 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14155 md5_init_ctx (&ctx
);
14156 fold_checksum_tree (op0
, &ctx
, &ht
);
14157 md5_finish_ctx (&ctx
, checksum_before_op0
);
14160 md5_init_ctx (&ctx
);
14161 fold_checksum_tree (op1
, &ctx
, &ht
);
14162 md5_finish_ctx (&ctx
, checksum_before_op1
);
14165 md5_init_ctx (&ctx
);
14166 fold_checksum_tree (op2
, &ctx
, &ht
);
14167 md5_finish_ctx (&ctx
, checksum_before_op2
);
14171 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
14172 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
14174 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
14176 #ifdef ENABLE_FOLD_CHECKING
14177 md5_init_ctx (&ctx
);
14178 fold_checksum_tree (op0
, &ctx
, &ht
);
14179 md5_finish_ctx (&ctx
, checksum_after_op0
);
14182 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14183 fold_check_failed (op0
, tem
);
14185 md5_init_ctx (&ctx
);
14186 fold_checksum_tree (op1
, &ctx
, &ht
);
14187 md5_finish_ctx (&ctx
, checksum_after_op1
);
14190 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14191 fold_check_failed (op1
, tem
);
14193 md5_init_ctx (&ctx
);
14194 fold_checksum_tree (op2
, &ctx
, &ht
);
14195 md5_finish_ctx (&ctx
, checksum_after_op2
);
14197 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
14198 fold_check_failed (op2
, tem
);
14203 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14204 arguments in ARGARRAY, and a null static chain.
14205 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14206 of type TYPE from the given operands as constructed by build_call_array. */
14209 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
14210 int nargs
, tree
*argarray
)
14213 #ifdef ENABLE_FOLD_CHECKING
14214 unsigned char checksum_before_fn
[16],
14215 checksum_before_arglist
[16],
14216 checksum_after_fn
[16],
14217 checksum_after_arglist
[16];
14218 struct md5_ctx ctx
;
14219 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14222 md5_init_ctx (&ctx
);
14223 fold_checksum_tree (fn
, &ctx
, &ht
);
14224 md5_finish_ctx (&ctx
, checksum_before_fn
);
14227 md5_init_ctx (&ctx
);
14228 for (i
= 0; i
< nargs
; i
++)
14229 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14230 md5_finish_ctx (&ctx
, checksum_before_arglist
);
14234 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
14236 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14238 #ifdef ENABLE_FOLD_CHECKING
14239 md5_init_ctx (&ctx
);
14240 fold_checksum_tree (fn
, &ctx
, &ht
);
14241 md5_finish_ctx (&ctx
, checksum_after_fn
);
14244 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
14245 fold_check_failed (fn
, tem
);
14247 md5_init_ctx (&ctx
);
14248 for (i
= 0; i
< nargs
; i
++)
14249 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14250 md5_finish_ctx (&ctx
, checksum_after_arglist
);
14252 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
14253 fold_check_failed (NULL_TREE
, tem
);
14258 /* Perform constant folding and related simplification of initializer
14259 expression EXPR. These behave identically to "fold_buildN" but ignore
14260 potential run-time traps and exceptions that fold must preserve. */
14262 #define START_FOLD_INIT \
14263 int saved_signaling_nans = flag_signaling_nans;\
14264 int saved_trapping_math = flag_trapping_math;\
14265 int saved_rounding_math = flag_rounding_math;\
14266 int saved_trapv = flag_trapv;\
14267 int saved_folding_initializer = folding_initializer;\
14268 flag_signaling_nans = 0;\
14269 flag_trapping_math = 0;\
14270 flag_rounding_math = 0;\
14272 folding_initializer = 1;
14274 #define END_FOLD_INIT \
14275 flag_signaling_nans = saved_signaling_nans;\
14276 flag_trapping_math = saved_trapping_math;\
14277 flag_rounding_math = saved_rounding_math;\
14278 flag_trapv = saved_trapv;\
14279 folding_initializer = saved_folding_initializer;
14282 fold_init (tree expr
)
14287 result
= fold (expr
);
14294 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
14295 tree type
, tree op
)
14300 result
= fold_build1_loc (loc
, code
, type
, op
);
14307 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
14308 tree type
, tree op0
, tree op1
)
14313 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
14320 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
14321 int nargs
, tree
*argarray
)
14326 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14333 fold_binary_initializer_loc (location_t loc
, tree_code code
, tree type
,
14334 tree lhs
, tree rhs
)
14339 result
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
14345 #undef START_FOLD_INIT
14346 #undef END_FOLD_INIT
14348 /* Determine if first argument is a multiple of second argument. Return
14349 false if it is not, or we cannot easily determined it to be.
14351 An example of the sort of thing we care about (at this point; this routine
14352 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14353 fold cases do now) is discovering that
14355 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14361 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14363 This code also handles discovering that
14365 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14367 is a multiple of 8 so we don't have to worry about dealing with a
14368 possible remainder.
14370 Note that we *look* inside a SAVE_EXPR only to determine how it was
14371 calculated; it is not safe for fold to do much of anything else with the
14372 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14373 at run time. For example, the latter example above *cannot* be implemented
14374 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14375 evaluation time of the original SAVE_EXPR is not necessarily the same at
14376 the time the new expression is evaluated. The only optimization of this
14377 sort that would be valid is changing
14379 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14383 SAVE_EXPR (I) * SAVE_EXPR (J)
14385 (where the same SAVE_EXPR (J) is used in the original and the
14386 transformed version).
14388 NOWRAP specifies whether all outer operations in TYPE should
14389 be considered not wrapping. Any type conversion within TOP acts
14390 as a barrier and we will fall back to NOWRAP being false.
14391 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14392 as not wrapping even though they are generally using unsigned arithmetic. */
14395 multiple_of_p (tree type
, const_tree top
, const_tree bottom
, bool nowrap
)
14400 if (operand_equal_p (top
, bottom
, 0))
14403 if (TREE_CODE (type
) != INTEGER_TYPE
)
14406 switch (TREE_CODE (top
))
14409 /* Bitwise and provides a power of two multiple. If the mask is
14410 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14411 if (!integer_pow2p (bottom
))
14413 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14414 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14417 /* If the multiplication can wrap we cannot recurse further unless
14418 the bottom is a power of two which is where wrapping does not
14421 && !TYPE_OVERFLOW_UNDEFINED (type
)
14422 && !integer_pow2p (bottom
))
14424 if (TREE_CODE (bottom
) == INTEGER_CST
)
14426 op1
= TREE_OPERAND (top
, 0);
14427 op2
= TREE_OPERAND (top
, 1);
14428 if (TREE_CODE (op1
) == INTEGER_CST
)
14429 std::swap (op1
, op2
);
14430 if (TREE_CODE (op2
) == INTEGER_CST
)
14432 if (multiple_of_p (type
, op2
, bottom
, nowrap
))
14434 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14435 if (multiple_of_p (type
, bottom
, op2
, nowrap
))
14437 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14438 wi::to_widest (op2
));
14439 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14441 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14442 return multiple_of_p (type
, op1
, op2
, nowrap
);
14445 return multiple_of_p (type
, op1
, bottom
, nowrap
);
14448 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14449 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14452 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14453 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14455 op1
= TREE_OPERAND (top
, 1);
14456 if (wi::to_widest (op1
) < TYPE_PRECISION (type
))
14459 = wi::one (TYPE_PRECISION (type
)) << wi::to_wide (op1
);
14460 return multiple_of_p (type
,
14461 wide_int_to_tree (type
, mul_op
), bottom
,
14469 /* If the addition or subtraction can wrap we cannot recurse further
14470 unless bottom is a power of two which is where wrapping does not
14473 && !TYPE_OVERFLOW_UNDEFINED (type
)
14474 && !integer_pow2p (bottom
))
14477 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14478 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14479 but 0xfffffffd is not. */
14480 op1
= TREE_OPERAND (top
, 1);
14481 if (TREE_CODE (top
) == PLUS_EXPR
14483 && TYPE_UNSIGNED (type
)
14484 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14485 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14487 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14488 precisely, so be conservative here checking if both op0 and op1
14489 are multiple of bottom. Note we check the second operand first
14490 since it's usually simpler. */
14491 return (multiple_of_p (type
, op1
, bottom
, nowrap
)
14492 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14495 /* Can't handle conversions from non-integral or wider integral type. */
14496 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14497 || (TYPE_PRECISION (type
)
14498 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14500 /* NOWRAP only extends to operations in the outermost type so
14501 make sure to strip it off here. */
14502 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top
, 0)),
14503 TREE_OPERAND (top
, 0), bottom
, false);
14506 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
);
14509 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14510 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
, nowrap
));
14513 if (TREE_CODE (bottom
) != INTEGER_CST
|| integer_zerop (bottom
))
14515 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14519 if (TREE_CODE (bottom
) == INTEGER_CST
14520 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14521 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14523 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14525 /* Check for special cases to see if top is defined as multiple
14528 top = (X & ~(bottom - 1) ; bottom is power of 2
14534 if (code
== BIT_AND_EXPR
14535 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14536 && TREE_CODE (op2
) == INTEGER_CST
14537 && integer_pow2p (bottom
)
14538 && wi::multiple_of_p (wi::to_widest (op2
),
14539 wi::to_widest (bottom
), SIGNED
))
14542 op1
= gimple_assign_rhs1 (stmt
);
14543 if (code
== MINUS_EXPR
14544 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14545 && TREE_CODE (op2
) == SSA_NAME
14546 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14547 && gimple_code (stmt
) == GIMPLE_ASSIGN
14548 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14549 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14550 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14557 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14558 return multiple_p (wi::to_poly_widest (top
),
14559 wi::to_poly_widest (bottom
));
14565 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14566 This function returns true for integer expressions, and returns
14567 false if uncertain. */
14570 tree_expr_finite_p (const_tree x
)
14572 machine_mode mode
= element_mode (x
);
14573 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14575 switch (TREE_CODE (x
))
14578 return real_isfinite (TREE_REAL_CST_PTR (x
));
14580 return tree_expr_finite_p (TREE_REALPART (x
))
14581 && tree_expr_finite_p (TREE_IMAGPART (x
));
14586 case NON_LVALUE_EXPR
:
14589 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14592 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14593 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14595 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14596 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14598 switch (get_call_combined_fn (x
))
14602 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14607 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14608 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14618 /* Return true if expression X evaluates to an infinity.
14619 This function returns false for integer expressions. */
14622 tree_expr_infinite_p (const_tree x
)
14624 if (!HONOR_INFINITIES (x
))
14626 switch (TREE_CODE (x
))
14629 return real_isinf (TREE_REAL_CST_PTR (x
));
14632 case NON_LVALUE_EXPR
:
14634 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14636 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14637 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14643 /* Return true if expression X could evaluate to an infinity.
14644 This function returns false for integer expressions, and returns
14645 true if uncertain. */
14648 tree_expr_maybe_infinite_p (const_tree x
)
14650 if (!HONOR_INFINITIES (x
))
14652 switch (TREE_CODE (x
))
14655 return real_isinf (TREE_REAL_CST_PTR (x
));
14660 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14662 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14663 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14669 /* Return true if expression X evaluates to a signaling NaN.
14670 This function returns false for integer expressions. */
14673 tree_expr_signaling_nan_p (const_tree x
)
14675 if (!HONOR_SNANS (x
))
14677 switch (TREE_CODE (x
))
14680 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14681 case NON_LVALUE_EXPR
:
14683 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14685 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14686 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14692 /* Return true if expression X could evaluate to a signaling NaN.
14693 This function returns false for integer expressions, and returns
14694 true if uncertain. */
14697 tree_expr_maybe_signaling_nan_p (const_tree x
)
14699 if (!HONOR_SNANS (x
))
14701 switch (TREE_CODE (x
))
14704 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14710 case NON_LVALUE_EXPR
:
14712 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14715 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14716 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14718 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14719 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14721 switch (get_call_combined_fn (x
))
14725 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14730 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14731 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14740 /* Return true if expression X evaluates to a NaN.
14741 This function returns false for integer expressions. */
14744 tree_expr_nan_p (const_tree x
)
14746 if (!HONOR_NANS (x
))
14748 switch (TREE_CODE (x
))
14751 return real_isnan (TREE_REAL_CST_PTR (x
));
14752 case NON_LVALUE_EXPR
:
14754 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14756 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14757 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14763 /* Return true if expression X could evaluate to a NaN.
14764 This function returns false for integer expressions, and returns
14765 true if uncertain. */
14768 tree_expr_maybe_nan_p (const_tree x
)
14770 if (!HONOR_NANS (x
))
14772 switch (TREE_CODE (x
))
14775 return real_isnan (TREE_REAL_CST_PTR (x
));
14781 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14782 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14786 case NON_LVALUE_EXPR
:
14788 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14791 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14792 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14794 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14795 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14797 switch (get_call_combined_fn (x
))
14801 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14806 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14807 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14816 /* Return true if expression X could evaluate to -0.0.
14817 This function returns true if uncertain. */
14820 tree_expr_maybe_real_minus_zero_p (const_tree x
)
14822 if (!HONOR_SIGNED_ZEROS (x
))
14824 switch (TREE_CODE (x
))
14827 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x
));
14832 case NON_LVALUE_EXPR
:
14834 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 0));
14836 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 1))
14837 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 2));
14839 switch (get_call_combined_fn (x
))
14850 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14851 * but currently those predicates require tree and not const_tree. */
14855 #define tree_expr_nonnegative_warnv_p(X, Y) \
14856 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14858 #define RECURSE(X) \
14859 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14861 /* Return true if CODE or TYPE is known to be non-negative. */
14864 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14866 if (!VECTOR_TYPE_P (type
)
14867 && (TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14868 && truth_value_p (code
))
14869 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14870 have a signed:1 type (where the value is -1 and 0). */
14875 /* Return true if (CODE OP0) is known to be non-negative. If the return
14876 value is based on the assumption that signed overflow is undefined,
14877 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14878 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14881 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14882 bool *strict_overflow_p
, int depth
)
14884 if (TYPE_UNSIGNED (type
))
14890 /* We can't return 1 if flag_wrapv is set because
14891 ABS_EXPR<INT_MIN> = INT_MIN. */
14892 if (!ANY_INTEGRAL_TYPE_P (type
))
14894 if (TYPE_OVERFLOW_UNDEFINED (type
))
14896 *strict_overflow_p
= true;
14901 case NON_LVALUE_EXPR
:
14903 case FIX_TRUNC_EXPR
:
14904 return RECURSE (op0
);
14908 tree inner_type
= TREE_TYPE (op0
);
14909 tree outer_type
= type
;
14911 if (SCALAR_FLOAT_TYPE_P (outer_type
))
14913 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14914 return RECURSE (op0
);
14915 if (INTEGRAL_TYPE_P (inner_type
))
14917 if (TYPE_UNSIGNED (inner_type
))
14919 return RECURSE (op0
);
14922 else if (INTEGRAL_TYPE_P (outer_type
))
14924 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14925 return RECURSE (op0
);
14926 if (INTEGRAL_TYPE_P (inner_type
))
14927 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14928 && TYPE_UNSIGNED (inner_type
);
14934 return tree_simple_nonnegative_warnv_p (code
, type
);
14937 /* We don't know sign of `t', so be conservative and return false. */
14941 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14942 value is based on the assumption that signed overflow is undefined,
14943 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14944 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14947 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14948 tree op1
, bool *strict_overflow_p
,
14951 if (TYPE_UNSIGNED (type
))
14956 case POINTER_PLUS_EXPR
:
14958 if (FLOAT_TYPE_P (type
))
14959 return RECURSE (op0
) && RECURSE (op1
);
14961 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14962 both unsigned and at least 2 bits shorter than the result. */
14963 if (TREE_CODE (type
) == INTEGER_TYPE
14964 && TREE_CODE (op0
) == NOP_EXPR
14965 && TREE_CODE (op1
) == NOP_EXPR
)
14967 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14968 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14969 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14970 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14972 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14973 TYPE_PRECISION (inner2
)) + 1;
14974 return prec
< TYPE_PRECISION (type
);
14980 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14982 /* x * x is always non-negative for floating point x
14983 or without overflow. */
14984 if (operand_equal_p (op0
, op1
, 0)
14985 || (RECURSE (op0
) && RECURSE (op1
)))
14987 if (ANY_INTEGRAL_TYPE_P (type
)
14988 && TYPE_OVERFLOW_UNDEFINED (type
))
14989 *strict_overflow_p
= true;
14994 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14995 both unsigned and their total bits is shorter than the result. */
14996 if (TREE_CODE (type
) == INTEGER_TYPE
14997 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14998 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
15000 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
15001 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
15003 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
15004 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
15007 bool unsigned0
= TYPE_UNSIGNED (inner0
);
15008 bool unsigned1
= TYPE_UNSIGNED (inner1
);
15010 if (TREE_CODE (op0
) == INTEGER_CST
)
15011 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
15013 if (TREE_CODE (op1
) == INTEGER_CST
)
15014 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
15016 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
15017 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
15019 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
15020 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
15021 : TYPE_PRECISION (inner0
);
15023 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
15024 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
15025 : TYPE_PRECISION (inner1
);
15027 return precision0
+ precision1
< TYPE_PRECISION (type
);
15033 return RECURSE (op0
) || RECURSE (op1
);
15036 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
15038 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
15039 return RECURSE (op0
) && RECURSE (op1
);
15040 return RECURSE (op0
) || RECURSE (op1
);
15046 case TRUNC_DIV_EXPR
:
15047 case CEIL_DIV_EXPR
:
15048 case FLOOR_DIV_EXPR
:
15049 case ROUND_DIV_EXPR
:
15050 return RECURSE (op0
) && RECURSE (op1
);
15052 case TRUNC_MOD_EXPR
:
15053 return RECURSE (op0
);
15055 case FLOOR_MOD_EXPR
:
15056 return RECURSE (op1
);
15058 case CEIL_MOD_EXPR
:
15059 case ROUND_MOD_EXPR
:
15061 return tree_simple_nonnegative_warnv_p (code
, type
);
15064 /* We don't know sign of `t', so be conservative and return false. */
15068 /* Return true if T is known to be non-negative. If the return
15069 value is based on the assumption that signed overflow is undefined,
15070 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15071 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15074 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15076 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15079 switch (TREE_CODE (t
))
15082 return tree_int_cst_sgn (t
) >= 0;
15085 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
15088 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
15091 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15094 /* Limit the depth of recursion to avoid quadratic behavior.
15095 This is expected to catch almost all occurrences in practice.
15096 If this code misses important cases that unbounded recursion
15097 would not, passes that need this information could be revised
15098 to provide it through dataflow propagation. */
15099 return (!name_registered_for_update_p (t
)
15100 && depth
< param_max_ssa_name_query_depth
15101 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
15102 strict_overflow_p
, depth
));
15105 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15109 /* Return true if T is known to be non-negative. If the return
15110 value is based on the assumption that signed overflow is undefined,
15111 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15112 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15115 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
15116 bool *strict_overflow_p
, int depth
)
15147 case CFN_BUILT_IN_BSWAP16
:
15148 case CFN_BUILT_IN_BSWAP32
:
15149 case CFN_BUILT_IN_BSWAP64
:
15150 case CFN_BUILT_IN_BSWAP128
:
15156 /* sqrt(-0.0) is -0.0. */
15157 if (!HONOR_SIGNED_ZEROS (type
))
15159 return RECURSE (arg0
);
15191 CASE_CFN_LLRINT_FN
:
15193 CASE_CFN_LLROUND_FN
:
15197 CASE_CFN_LROUND_FN
:
15200 CASE_CFN_NEARBYINT
:
15201 CASE_CFN_NEARBYINT_FN
:
15206 CASE_CFN_ROUNDEVEN
:
15207 CASE_CFN_ROUNDEVEN_FN
:
15210 CASE_CFN_SCALBLN_FN
:
15212 CASE_CFN_SCALBN_FN
:
15214 CASE_CFN_SIGNIFICAND
:
15221 /* True if the 1st argument is nonnegative. */
15222 return RECURSE (arg0
);
15226 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15227 things. In the presence of sNaNs, we're only guaranteed to be
15228 non-negative if both operands are non-negative. In the presence
15229 of qNaNs, we're non-negative if either operand is non-negative
15230 and can't be a qNaN, or if both operands are non-negative. */
15231 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
15232 tree_expr_maybe_signaling_nan_p (arg1
))
15233 return RECURSE (arg0
) && RECURSE (arg1
);
15234 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
15237 && !tree_expr_maybe_nan_p (arg1
));
15241 /* True if the 1st AND 2nd arguments are nonnegative. */
15242 return RECURSE (arg0
) && RECURSE (arg1
);
15245 CASE_CFN_COPYSIGN_FN
:
15246 /* True if the 2nd argument is nonnegative. */
15247 return RECURSE (arg1
);
15250 /* True if the 1st argument is nonnegative or the second
15251 argument is an even integer. */
15252 if (TREE_CODE (arg1
) == INTEGER_CST
15253 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
15255 return RECURSE (arg0
);
15259 /* True if the 1st argument is nonnegative or the second
15260 argument is an even integer valued real. */
15261 if (TREE_CODE (arg1
) == REAL_CST
)
15266 c
= TREE_REAL_CST (arg1
);
15267 n
= real_to_integer (&c
);
15270 REAL_VALUE_TYPE cint
;
15271 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
15272 if (real_identical (&c
, &cint
))
15276 return RECURSE (arg0
);
15281 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
15284 /* Return true if T is known to be non-negative. If the return
15285 value is based on the assumption that signed overflow is undefined,
15286 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15287 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15290 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15292 enum tree_code code
= TREE_CODE (t
);
15293 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15300 tree temp
= TARGET_EXPR_SLOT (t
);
15301 t
= TARGET_EXPR_INITIAL (t
);
15303 /* If the initializer is non-void, then it's a normal expression
15304 that will be assigned to the slot. */
15305 if (!VOID_TYPE_P (TREE_TYPE (t
)))
15306 return RECURSE (t
);
15308 /* Otherwise, the initializer sets the slot in some way. One common
15309 way is an assignment statement at the end of the initializer. */
15312 if (TREE_CODE (t
) == BIND_EXPR
)
15313 t
= expr_last (BIND_EXPR_BODY (t
));
15314 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
15315 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
15316 t
= expr_last (TREE_OPERAND (t
, 0));
15317 else if (TREE_CODE (t
) == STATEMENT_LIST
)
15322 if (TREE_CODE (t
) == MODIFY_EXPR
15323 && TREE_OPERAND (t
, 0) == temp
)
15324 return RECURSE (TREE_OPERAND (t
, 1));
15331 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
15332 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
15334 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
15335 get_call_combined_fn (t
),
15338 strict_overflow_p
, depth
);
15340 case COMPOUND_EXPR
:
15342 return RECURSE (TREE_OPERAND (t
, 1));
15345 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
15348 return RECURSE (TREE_OPERAND (t
, 0));
15351 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15356 #undef tree_expr_nonnegative_warnv_p
15358 /* Return true if T is known to be non-negative. If the return
15359 value is based on the assumption that signed overflow is undefined,
15360 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15361 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15364 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15366 enum tree_code code
;
15367 if (t
== error_mark_node
)
15370 code
= TREE_CODE (t
);
15371 switch (TREE_CODE_CLASS (code
))
15374 case tcc_comparison
:
15375 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15377 TREE_OPERAND (t
, 0),
15378 TREE_OPERAND (t
, 1),
15379 strict_overflow_p
, depth
);
15382 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15384 TREE_OPERAND (t
, 0),
15385 strict_overflow_p
, depth
);
15388 case tcc_declaration
:
15389 case tcc_reference
:
15390 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15398 case TRUTH_AND_EXPR
:
15399 case TRUTH_OR_EXPR
:
15400 case TRUTH_XOR_EXPR
:
15401 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15403 TREE_OPERAND (t
, 0),
15404 TREE_OPERAND (t
, 1),
15405 strict_overflow_p
, depth
);
15406 case TRUTH_NOT_EXPR
:
15407 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15409 TREE_OPERAND (t
, 0),
15410 strict_overflow_p
, depth
);
15416 case WITH_SIZE_EXPR
:
15418 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15421 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15425 /* Return true if `t' is known to be non-negative. Handle warnings
15426 about undefined signed overflow. */
15429 tree_expr_nonnegative_p (tree t
)
15431 bool ret
, strict_overflow_p
;
15433 strict_overflow_p
= false;
15434 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15435 if (strict_overflow_p
)
15436 fold_overflow_warning (("assuming signed overflow does not occur when "
15437 "determining that expression is always "
15439 WARN_STRICT_OVERFLOW_MISC
);
15444 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15445 For floating point we further ensure that T is not denormal.
15446 Similar logic is present in nonzero_address in rtlanal.h.
15448 If the return value is based on the assumption that signed overflow
15449 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15450 change *STRICT_OVERFLOW_P. */
15453 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15454 bool *strict_overflow_p
)
15459 return tree_expr_nonzero_warnv_p (op0
,
15460 strict_overflow_p
);
15464 tree inner_type
= TREE_TYPE (op0
);
15465 tree outer_type
= type
;
15467 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15468 && tree_expr_nonzero_warnv_p (op0
,
15469 strict_overflow_p
));
15473 case NON_LVALUE_EXPR
:
15474 return tree_expr_nonzero_warnv_p (op0
,
15475 strict_overflow_p
);
15484 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15485 For floating point we further ensure that T is not denormal.
15486 Similar logic is present in nonzero_address in rtlanal.h.
15488 If the return value is based on the assumption that signed overflow
15489 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15490 change *STRICT_OVERFLOW_P. */
15493 tree_binary_nonzero_warnv_p (enum tree_code code
,
15496 tree op1
, bool *strict_overflow_p
)
15498 bool sub_strict_overflow_p
;
15501 case POINTER_PLUS_EXPR
:
15503 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15505 /* With the presence of negative values it is hard
15506 to say something. */
15507 sub_strict_overflow_p
= false;
15508 if (!tree_expr_nonnegative_warnv_p (op0
,
15509 &sub_strict_overflow_p
)
15510 || !tree_expr_nonnegative_warnv_p (op1
,
15511 &sub_strict_overflow_p
))
15513 /* One of operands must be positive and the other non-negative. */
15514 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15515 overflows, on a twos-complement machine the sum of two
15516 nonnegative numbers can never be zero. */
15517 return (tree_expr_nonzero_warnv_p (op0
,
15519 || tree_expr_nonzero_warnv_p (op1
,
15520 strict_overflow_p
));
15525 if (TYPE_OVERFLOW_UNDEFINED (type
))
15527 if (tree_expr_nonzero_warnv_p (op0
,
15529 && tree_expr_nonzero_warnv_p (op1
,
15530 strict_overflow_p
))
15532 *strict_overflow_p
= true;
15539 sub_strict_overflow_p
= false;
15540 if (tree_expr_nonzero_warnv_p (op0
,
15541 &sub_strict_overflow_p
)
15542 && tree_expr_nonzero_warnv_p (op1
,
15543 &sub_strict_overflow_p
))
15545 if (sub_strict_overflow_p
)
15546 *strict_overflow_p
= true;
15551 sub_strict_overflow_p
= false;
15552 if (tree_expr_nonzero_warnv_p (op0
,
15553 &sub_strict_overflow_p
))
15555 if (sub_strict_overflow_p
)
15556 *strict_overflow_p
= true;
15558 /* When both operands are nonzero, then MAX must be too. */
15559 if (tree_expr_nonzero_warnv_p (op1
,
15560 strict_overflow_p
))
15563 /* MAX where operand 0 is positive is positive. */
15564 return tree_expr_nonnegative_warnv_p (op0
,
15565 strict_overflow_p
);
15567 /* MAX where operand 1 is positive is positive. */
15568 else if (tree_expr_nonzero_warnv_p (op1
,
15569 &sub_strict_overflow_p
)
15570 && tree_expr_nonnegative_warnv_p (op1
,
15571 &sub_strict_overflow_p
))
15573 if (sub_strict_overflow_p
)
15574 *strict_overflow_p
= true;
15580 return (tree_expr_nonzero_warnv_p (op1
,
15582 || tree_expr_nonzero_warnv_p (op0
,
15583 strict_overflow_p
));
15592 /* Return true when T is an address and is known to be nonzero.
15593 For floating point we further ensure that T is not denormal.
15594 Similar logic is present in nonzero_address in rtlanal.h.
15596 If the return value is based on the assumption that signed overflow
15597 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15598 change *STRICT_OVERFLOW_P. */
15601 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15603 bool sub_strict_overflow_p
;
15604 switch (TREE_CODE (t
))
15607 return !integer_zerop (t
);
15611 tree base
= TREE_OPERAND (t
, 0);
15613 if (!DECL_P (base
))
15614 base
= get_base_address (base
);
15616 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15617 base
= TARGET_EXPR_SLOT (base
);
15622 /* For objects in symbol table check if we know they are non-zero.
15623 Don't do anything for variables and functions before symtab is built;
15624 it is quite possible that they will be declared weak later. */
15625 int nonzero_addr
= maybe_nonzero_address (base
);
15626 if (nonzero_addr
>= 0)
15627 return nonzero_addr
;
15629 /* Constants are never weak. */
15630 if (CONSTANT_CLASS_P (base
))
15637 sub_strict_overflow_p
= false;
15638 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15639 &sub_strict_overflow_p
)
15640 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15641 &sub_strict_overflow_p
))
15643 if (sub_strict_overflow_p
)
15644 *strict_overflow_p
= true;
15650 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15652 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15660 #define integer_valued_real_p(X) \
15661 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15663 #define RECURSE(X) \
15664 ((integer_valued_real_p) (X, depth + 1))
15666 /* Return true if the floating point result of (CODE OP0) has an
15667 integer value. We also allow +Inf, -Inf and NaN to be considered
15668 integer values. Return false for signaling NaN.
15670 DEPTH is the current nesting depth of the query. */
15673 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15681 return RECURSE (op0
);
15685 tree type
= TREE_TYPE (op0
);
15686 if (TREE_CODE (type
) == INTEGER_TYPE
)
15688 if (SCALAR_FLOAT_TYPE_P (type
))
15689 return RECURSE (op0
);
15699 /* Return true if the floating point result of (CODE OP0 OP1) has an
15700 integer value. We also allow +Inf, -Inf and NaN to be considered
15701 integer values. Return false for signaling NaN.
15703 DEPTH is the current nesting depth of the query. */
15706 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15715 return RECURSE (op0
) && RECURSE (op1
);
15723 /* Return true if the floating point result of calling FNDECL with arguments
15724 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15725 considered integer values. Return false for signaling NaN. If FNDECL
15726 takes fewer than 2 arguments, the remaining ARGn are null.
15728 DEPTH is the current nesting depth of the query. */
15731 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15739 CASE_CFN_NEARBYINT
:
15740 CASE_CFN_NEARBYINT_FN
:
15745 CASE_CFN_ROUNDEVEN
:
15746 CASE_CFN_ROUNDEVEN_FN
:
15755 return RECURSE (arg0
) && RECURSE (arg1
);
15763 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15764 has an integer value. We also allow +Inf, -Inf and NaN to be
15765 considered integer values. Return false for signaling NaN.
15767 DEPTH is the current nesting depth of the query. */
15770 integer_valued_real_single_p (tree t
, int depth
)
15772 switch (TREE_CODE (t
))
15775 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15778 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15781 /* Limit the depth of recursion to avoid quadratic behavior.
15782 This is expected to catch almost all occurrences in practice.
15783 If this code misses important cases that unbounded recursion
15784 would not, passes that need this information could be revised
15785 to provide it through dataflow propagation. */
15786 return (!name_registered_for_update_p (t
)
15787 && depth
< param_max_ssa_name_query_depth
15788 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15797 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15798 has an integer value. We also allow +Inf, -Inf and NaN to be
15799 considered integer values. Return false for signaling NaN.
15801 DEPTH is the current nesting depth of the query. */
15804 integer_valued_real_invalid_p (tree t
, int depth
)
15806 switch (TREE_CODE (t
))
15808 case COMPOUND_EXPR
:
15811 return RECURSE (TREE_OPERAND (t
, 1));
15814 return RECURSE (TREE_OPERAND (t
, 0));
15823 #undef integer_valued_real_p
15825 /* Return true if the floating point expression T has an integer value.
15826 We also allow +Inf, -Inf and NaN to be considered integer values.
15827 Return false for signaling NaN.
15829 DEPTH is the current nesting depth of the query. */
15832 integer_valued_real_p (tree t
, int depth
)
15834 if (t
== error_mark_node
)
15837 STRIP_ANY_LOCATION_WRAPPER (t
);
15839 tree_code code
= TREE_CODE (t
);
15840 switch (TREE_CODE_CLASS (code
))
15843 case tcc_comparison
:
15844 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15845 TREE_OPERAND (t
, 1), depth
);
15848 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15851 case tcc_declaration
:
15852 case tcc_reference
:
15853 return integer_valued_real_single_p (t
, depth
);
15863 return integer_valued_real_single_p (t
, depth
);
15867 tree arg0
= (call_expr_nargs (t
) > 0
15868 ? CALL_EXPR_ARG (t
, 0)
15870 tree arg1
= (call_expr_nargs (t
) > 1
15871 ? CALL_EXPR_ARG (t
, 1)
15873 return integer_valued_real_call_p (get_call_combined_fn (t
),
15874 arg0
, arg1
, depth
);
15878 return integer_valued_real_invalid_p (t
, depth
);
15882 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15883 attempt to fold the expression to a constant without modifying TYPE,
15886 If the expression could be simplified to a constant, then return
15887 the constant. If the expression would not be simplified to a
15888 constant, then return NULL_TREE. */
15891 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15893 tree tem
= fold_binary (code
, type
, op0
, op1
);
15894 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15897 /* Given the components of a unary expression CODE, TYPE and OP0,
15898 attempt to fold the expression to a constant without modifying
15901 If the expression could be simplified to a constant, then return
15902 the constant. If the expression would not be simplified to a
15903 constant, then return NULL_TREE. */
15906 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15908 tree tem
= fold_unary (code
, type
, op0
);
15909 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15912 /* If EXP represents referencing an element in a constant string
15913 (either via pointer arithmetic or array indexing), return the
15914 tree representing the value accessed, otherwise return NULL. */
15917 fold_read_from_constant_string (tree exp
)
15919 if ((INDIRECT_REF_P (exp
)
15920 || TREE_CODE (exp
) == ARRAY_REF
)
15921 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15923 tree exp1
= TREE_OPERAND (exp
, 0);
15926 location_t loc
= EXPR_LOCATION (exp
);
15928 if (INDIRECT_REF_P (exp
))
15929 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15932 tree low_bound
= array_ref_low_bound (exp
);
15933 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15935 /* Optimize the special-case of a zero lower bound.
15937 We convert the low_bound to sizetype to avoid some problems
15938 with constant folding. (E.g. suppose the lower bound is 1,
15939 and its mode is QI. Without the conversion,l (ARRAY
15940 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15941 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15942 if (! integer_zerop (low_bound
))
15943 index
= size_diffop_loc (loc
, index
,
15944 fold_convert_loc (loc
, sizetype
, low_bound
));
15949 scalar_int_mode char_mode
;
15951 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15952 && TREE_CODE (string
) == STRING_CST
15953 && tree_fits_uhwi_p (index
)
15954 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15955 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15957 && GET_MODE_SIZE (char_mode
) == 1)
15958 return build_int_cst_type (TREE_TYPE (exp
),
15959 (TREE_STRING_POINTER (string
)
15960 [TREE_INT_CST_LOW (index
)]));
15965 /* Folds a read from vector element at IDX of vector ARG. */
15968 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15970 unsigned HOST_WIDE_INT i
;
15971 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15972 && known_ge (idx
, 0u)
15973 && idx
.is_constant (&i
))
15975 if (TREE_CODE (arg
) == VECTOR_CST
)
15976 return VECTOR_CST_ELT (arg
, i
);
15977 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15979 if (CONSTRUCTOR_NELTS (arg
)
15980 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg
, 0)->value
)))
15982 if (i
>= CONSTRUCTOR_NELTS (arg
))
15983 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
15984 return CONSTRUCTOR_ELT (arg
, i
)->value
;
15990 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15991 an integer constant, real, or fixed-point constant.
15993 TYPE is the type of the result. */
15996 fold_negate_const (tree arg0
, tree type
)
15998 tree t
= NULL_TREE
;
16000 switch (TREE_CODE (arg0
))
16003 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16008 FIXED_VALUE_TYPE f
;
16009 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
16010 &(TREE_FIXED_CST (arg0
)), NULL
,
16011 TYPE_SATURATING (type
));
16012 t
= build_fixed (type
, f
);
16013 /* Propagate overflow flags. */
16014 if (overflow_p
| TREE_OVERFLOW (arg0
))
16015 TREE_OVERFLOW (t
) = 1;
16020 if (poly_int_tree_p (arg0
))
16022 wi::overflow_type overflow
;
16023 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
16024 t
= force_fit_type (type
, res
, 1,
16025 (overflow
&& ! TYPE_UNSIGNED (type
))
16026 || TREE_OVERFLOW (arg0
));
16030 gcc_unreachable ();
16036 /* Return the tree for abs (ARG0) when ARG0 is known to be either
16037 an integer constant or real constant.
16039 TYPE is the type of the result. */
16042 fold_abs_const (tree arg0
, tree type
)
16044 tree t
= NULL_TREE
;
16046 switch (TREE_CODE (arg0
))
16050 /* If the value is unsigned or non-negative, then the absolute value
16051 is the same as the ordinary value. */
16052 wide_int val
= wi::to_wide (arg0
);
16053 wi::overflow_type overflow
= wi::OVF_NONE
;
16054 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
16057 /* If the value is negative, then the absolute value is
16060 val
= wi::neg (val
, &overflow
);
16062 /* Force to the destination type, set TREE_OVERFLOW for signed
16064 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
16069 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
16070 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16076 gcc_unreachable ();
16082 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
16083 constant. TYPE is the type of the result. */
16086 fold_not_const (const_tree arg0
, tree type
)
16088 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
16090 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
16093 /* Given CODE, a relational operator, the target type, TYPE and two
16094 constant operands OP0 and OP1, return the result of the
16095 relational operation. If the result is not a compile time
16096 constant, then return NULL_TREE. */
16099 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
16101 int result
, invert
;
16103 /* From here on, the only cases we handle are when the result is
16104 known to be a constant. */
16106 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
16108 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
16109 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
16111 /* Handle the cases where either operand is a NaN. */
16112 if (real_isnan (c0
) || real_isnan (c1
))
16122 case UNORDERED_EXPR
:
16136 if (flag_trapping_math
)
16142 gcc_unreachable ();
16145 return constant_boolean_node (result
, type
);
16148 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
16151 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
16153 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
16154 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
16155 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
16158 /* Handle equality/inequality of complex constants. */
16159 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
16161 tree rcond
= fold_relational_const (code
, type
,
16162 TREE_REALPART (op0
),
16163 TREE_REALPART (op1
));
16164 tree icond
= fold_relational_const (code
, type
,
16165 TREE_IMAGPART (op0
),
16166 TREE_IMAGPART (op1
));
16167 if (code
== EQ_EXPR
)
16168 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
16169 else if (code
== NE_EXPR
)
16170 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
16175 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
16177 if (!VECTOR_TYPE_P (type
))
16179 /* Have vector comparison with scalar boolean result. */
16180 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
16181 && known_eq (VECTOR_CST_NELTS (op0
),
16182 VECTOR_CST_NELTS (op1
)));
16183 unsigned HOST_WIDE_INT nunits
;
16184 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
16186 for (unsigned i
= 0; i
< nunits
; i
++)
16188 tree elem0
= VECTOR_CST_ELT (op0
, i
);
16189 tree elem1
= VECTOR_CST_ELT (op1
, i
);
16190 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
16191 if (tmp
== NULL_TREE
)
16193 if (integer_zerop (tmp
))
16194 return constant_boolean_node (code
== NE_EXPR
, type
);
16196 return constant_boolean_node (code
== EQ_EXPR
, type
);
16198 tree_vector_builder elts
;
16199 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
16201 unsigned int count
= elts
.encoded_nelts ();
16202 for (unsigned i
= 0; i
< count
; i
++)
16204 tree elem_type
= TREE_TYPE (type
);
16205 tree elem0
= VECTOR_CST_ELT (op0
, i
);
16206 tree elem1
= VECTOR_CST_ELT (op1
, i
);
16208 tree tem
= fold_relational_const (code
, elem_type
,
16211 if (tem
== NULL_TREE
)
16214 elts
.quick_push (build_int_cst (elem_type
,
16215 integer_zerop (tem
) ? 0 : -1));
16218 return elts
.build ();
16221 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16223 To compute GT, swap the arguments and do LT.
16224 To compute GE, do LT and invert the result.
16225 To compute LE, swap the arguments, do LT and invert the result.
16226 To compute NE, do EQ and invert the result.
16228 Therefore, the code below must handle only EQ and LT. */
16230 if (code
== LE_EXPR
|| code
== GT_EXPR
)
16232 std::swap (op0
, op1
);
16233 code
= swap_tree_comparison (code
);
16236 /* Note that it is safe to invert for real values here because we
16237 have already handled the one case that it matters. */
16240 if (code
== NE_EXPR
|| code
== GE_EXPR
)
16243 code
= invert_tree_comparison (code
, false);
16246 /* Compute a result for LT or EQ if args permit;
16247 Otherwise return T. */
16248 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
16250 if (code
== EQ_EXPR
)
16251 result
= tree_int_cst_equal (op0
, op1
);
16253 result
= tree_int_cst_lt (op0
, op1
);
16260 return constant_boolean_node (result
, type
);
16263 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16264 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16268 fold_build_cleanup_point_expr (tree type
, tree expr
)
16270 /* If the expression does not have side effects then we don't have to wrap
16271 it with a cleanup point expression. */
16272 if (!TREE_SIDE_EFFECTS (expr
))
16275 /* If the expression is a return, check to see if the expression inside the
16276 return has no side effects or the right hand side of the modify expression
16277 inside the return. If either don't have side effects set we don't need to
16278 wrap the expression in a cleanup point expression. Note we don't check the
16279 left hand side of the modify because it should always be a return decl. */
16280 if (TREE_CODE (expr
) == RETURN_EXPR
)
16282 tree op
= TREE_OPERAND (expr
, 0);
16283 if (!op
|| !TREE_SIDE_EFFECTS (op
))
16285 op
= TREE_OPERAND (op
, 1);
16286 if (!TREE_SIDE_EFFECTS (op
))
16290 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
16293 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16294 of an indirection through OP0, or NULL_TREE if no simplification is
16298 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
16302 poly_uint64 const_op01
;
16305 subtype
= TREE_TYPE (sub
);
16306 if (!POINTER_TYPE_P (subtype
)
16307 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
16310 if (TREE_CODE (sub
) == ADDR_EXPR
)
16312 tree op
= TREE_OPERAND (sub
, 0);
16313 tree optype
= TREE_TYPE (op
);
16315 /* *&CONST_DECL -> to the value of the const decl. */
16316 if (TREE_CODE (op
) == CONST_DECL
)
16317 return DECL_INITIAL (op
);
16318 /* *&p => p; make sure to handle *&"str"[cst] here. */
16319 if (type
== optype
)
16321 tree fop
= fold_read_from_constant_string (op
);
16327 /* *(foo *)&fooarray => fooarray[0] */
16328 else if (TREE_CODE (optype
) == ARRAY_TYPE
16329 && type
== TREE_TYPE (optype
)
16330 && (!in_gimple_form
16331 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16333 tree type_domain
= TYPE_DOMAIN (optype
);
16334 tree min_val
= size_zero_node
;
16335 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16336 min_val
= TYPE_MIN_VALUE (type_domain
);
16338 && TREE_CODE (min_val
) != INTEGER_CST
)
16340 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
16341 NULL_TREE
, NULL_TREE
);
16343 /* *(foo *)&complexfoo => __real__ complexfoo */
16344 else if (TREE_CODE (optype
) == COMPLEX_TYPE
16345 && type
== TREE_TYPE (optype
))
16346 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
16347 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16348 else if (VECTOR_TYPE_P (optype
)
16349 && type
== TREE_TYPE (optype
))
16351 tree part_width
= TYPE_SIZE (type
);
16352 tree index
= bitsize_int (0);
16353 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
16358 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16359 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
16361 tree op00
= TREE_OPERAND (sub
, 0);
16362 tree op01
= TREE_OPERAND (sub
, 1);
16365 if (TREE_CODE (op00
) == ADDR_EXPR
)
16368 op00
= TREE_OPERAND (op00
, 0);
16369 op00type
= TREE_TYPE (op00
);
16371 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16372 if (VECTOR_TYPE_P (op00type
)
16373 && type
== TREE_TYPE (op00type
)
16374 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16375 but we want to treat offsets with MSB set as negative.
16376 For the code below negative offsets are invalid and
16377 TYPE_SIZE of the element is something unsigned, so
16378 check whether op01 fits into poly_int64, which implies
16379 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16380 then just use poly_uint64 because we want to treat the
16381 value as unsigned. */
16382 && tree_fits_poly_int64_p (op01
))
16384 tree part_width
= TYPE_SIZE (type
);
16385 poly_uint64 max_offset
16386 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
16387 * TYPE_VECTOR_SUBPARTS (op00type
));
16388 if (known_lt (const_op01
, max_offset
))
16390 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
16391 return fold_build3_loc (loc
,
16392 BIT_FIELD_REF
, type
, op00
,
16393 part_width
, index
);
16396 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16397 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16398 && type
== TREE_TYPE (op00type
))
16400 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
16402 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16404 /* ((foo *)&fooarray)[1] => fooarray[1] */
16405 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16406 && type
== TREE_TYPE (op00type
))
16408 tree type_domain
= TYPE_DOMAIN (op00type
);
16409 tree min_val
= size_zero_node
;
16410 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16411 min_val
= TYPE_MIN_VALUE (type_domain
);
16412 poly_uint64 type_size
, index
;
16413 if (poly_int_tree_p (min_val
)
16414 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
16415 && multiple_p (const_op01
, type_size
, &index
))
16417 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
16418 op01
= wide_int_to_tree (sizetype
, off
);
16419 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16420 NULL_TREE
, NULL_TREE
);
16426 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16427 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16428 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16429 && (!in_gimple_form
16430 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16433 tree min_val
= size_zero_node
;
16434 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16435 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16436 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16437 min_val
= TYPE_MIN_VALUE (type_domain
);
16439 && TREE_CODE (min_val
) != INTEGER_CST
)
16441 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16448 /* Builds an expression for an indirection through T, simplifying some
16452 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16454 tree type
= TREE_TYPE (TREE_TYPE (t
));
16455 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16460 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16463 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16466 fold_indirect_ref_loc (location_t loc
, tree t
)
16468 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16476 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16477 whose result is ignored. The type of the returned tree need not be
16478 the same as the original expression. */
16481 fold_ignored_result (tree t
)
16483 if (!TREE_SIDE_EFFECTS (t
))
16484 return integer_zero_node
;
16487 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16490 t
= TREE_OPERAND (t
, 0);
16494 case tcc_comparison
:
16495 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16496 t
= TREE_OPERAND (t
, 0);
16497 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16498 t
= TREE_OPERAND (t
, 1);
16503 case tcc_expression
:
16504 switch (TREE_CODE (t
))
16506 case COMPOUND_EXPR
:
16507 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16509 t
= TREE_OPERAND (t
, 0);
16513 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16514 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16516 t
= TREE_OPERAND (t
, 0);
16529 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16532 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16534 tree div
= NULL_TREE
;
16539 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16540 have to do anything. Only do this when we are not given a const,
16541 because in that case, this check is more expensive than just
16543 if (TREE_CODE (value
) != INTEGER_CST
)
16545 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16547 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16551 /* If divisor is a power of two, simplify this to bit manipulation. */
16552 if (pow2_or_zerop (divisor
))
16554 if (TREE_CODE (value
) == INTEGER_CST
)
16556 wide_int val
= wi::to_wide (value
);
16559 if ((val
& (divisor
- 1)) == 0)
16562 overflow_p
= TREE_OVERFLOW (value
);
16563 val
+= divisor
- 1;
16564 val
&= (int) -divisor
;
16568 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16574 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16575 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16576 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16577 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16583 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16584 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16585 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16591 /* Likewise, but round down. */
16594 round_down_loc (location_t loc
, tree value
, int divisor
)
16596 tree div
= NULL_TREE
;
16598 gcc_assert (divisor
> 0);
16602 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16603 have to do anything. Only do this when we are not given a const,
16604 because in that case, this check is more expensive than just
16606 if (TREE_CODE (value
) != INTEGER_CST
)
16608 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16610 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16614 /* If divisor is a power of two, simplify this to bit manipulation. */
16615 if (pow2_or_zerop (divisor
))
16619 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16620 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16625 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16626 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16627 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16633 /* Returns the pointer to the base of the object addressed by EXP and
16634 extracts the information about the offset of the access, storing it
16635 to PBITPOS and POFFSET. */
16638 split_address_to_core_and_offset (tree exp
,
16639 poly_int64
*pbitpos
, tree
*poffset
)
16643 int unsignedp
, reversep
, volatilep
;
16644 poly_int64 bitsize
;
16645 location_t loc
= EXPR_LOCATION (exp
);
16647 if (TREE_CODE (exp
) == SSA_NAME
)
16648 if (gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (exp
)))
16649 if (gimple_assign_rhs_code (def
) == ADDR_EXPR
)
16650 exp
= gimple_assign_rhs1 (def
);
16652 if (TREE_CODE (exp
) == ADDR_EXPR
)
16654 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16655 poffset
, &mode
, &unsignedp
, &reversep
,
16657 core
= build_fold_addr_expr_loc (loc
, core
);
16659 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16661 core
= TREE_OPERAND (exp
, 0);
16664 *poffset
= TREE_OPERAND (exp
, 1);
16665 if (poly_int_tree_p (*poffset
))
16667 poly_offset_int tem
16668 = wi::sext (wi::to_poly_offset (*poffset
),
16669 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16670 tem
<<= LOG2_BITS_PER_UNIT
;
16671 if (tem
.to_shwi (pbitpos
))
16672 *poffset
= NULL_TREE
;
16679 *poffset
= NULL_TREE
;
16685 /* Returns true if addresses of E1 and E2 differ by a constant, false
16686 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16689 ptr_difference_const (tree e1
, tree e2
, poly_int64
*diff
)
16692 poly_int64 bitpos1
, bitpos2
;
16693 tree toffset1
, toffset2
, tdiff
, type
;
16695 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16696 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16698 poly_int64 bytepos1
, bytepos2
;
16699 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16700 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16701 || !operand_equal_p (core1
, core2
, 0))
16704 if (toffset1
&& toffset2
)
16706 type
= TREE_TYPE (toffset1
);
16707 if (type
!= TREE_TYPE (toffset2
))
16708 toffset2
= fold_convert (type
, toffset2
);
16710 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16711 if (!cst_and_fits_in_hwi (tdiff
))
16714 *diff
= int_cst_value (tdiff
);
16716 else if (toffset1
|| toffset2
)
16718 /* If only one of the offsets is non-constant, the difference cannot
16725 *diff
+= bytepos1
- bytepos2
;
16729 /* Return OFF converted to a pointer offset type suitable as offset for
16730 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16732 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16734 if (ptrofftype_p (TREE_TYPE (off
)))
16736 return fold_convert_loc (loc
, sizetype
, off
);
16739 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16741 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16743 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16744 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16747 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16749 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16751 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16752 ptr
, size_int (off
));
16755 /* Return a pointer to a NUL-terminated string containing the sequence
16756 of bytes corresponding to the representation of the object referred to
16757 by SRC (or a subsequence of such bytes within it if SRC is a reference
16758 to an initialized constant array plus some constant offset).
16759 Set *STRSIZE the number of bytes in the constant sequence including
16760 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16761 where A is the array that stores the constant sequence that SRC points
16762 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16763 need not point to a string or even an array of characters but may point
16764 to an object of any type. */
16767 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16769 /* The offset into the array A storing the string, and A's byte size. */
16777 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16779 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16783 unsigned HOST_WIDE_INT offset
= 0;
16784 if (offset_node
!= NULL_TREE
)
16786 if (!tree_fits_uhwi_p (offset_node
))
16789 offset
= tree_to_uhwi (offset_node
);
16792 if (!tree_fits_uhwi_p (mem_size
))
16795 /* ARRAY_SIZE is the byte size of the array the constant sequence
16796 is stored in and equal to sizeof A. INIT_BYTES is the number
16797 of bytes in the constant sequence used to initialize the array,
16798 including any embedded NULs as well as the terminating NUL (for
16799 strings), but not including any trailing zeros/NULs past
16800 the terminating one appended implicitly to a string literal to
16801 zero out the remainder of the array it's stored in. For example,
16803 const char a[7] = "abc\0d";
16804 n = strlen (a + 1);
16805 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16806 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16807 is equal to strlen (A) + 1. */
16808 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16809 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16810 const char *string
= TREE_STRING_POINTER (src
);
16812 /* Ideally this would turn into a gcc_checking_assert over time. */
16813 if (init_bytes
> array_size
)
16814 init_bytes
= array_size
;
16816 if (init_bytes
== 0 || offset
>= array_size
)
16821 /* Compute and store the number of characters from the beginning
16822 of the substring at OFFSET to the end, including the terminating
16823 nul. Offsets past the initial length refer to null strings. */
16824 if (offset
< init_bytes
)
16825 *strsize
= init_bytes
- offset
;
16831 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16832 /* Support only properly NUL-terminated single byte strings. */
16833 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16835 if (string
[init_bytes
- 1] != '\0')
16839 return offset
< init_bytes
? string
+ offset
: "";
16842 /* Return a pointer to a NUL-terminated string corresponding to
16843 the expression STR referencing a constant string, possibly
16844 involving a constant offset. Return null if STR either doesn't
16845 reference a constant string or if it involves a nonconstant
16849 c_getstr (tree str
)
16851 return getbyterep (str
, NULL
);
16854 /* Given a tree T, compute which bits in T may be nonzero. */
16857 tree_nonzero_bits (const_tree t
)
16859 switch (TREE_CODE (t
))
16862 return wi::to_wide (t
);
16864 return get_nonzero_bits (t
);
16865 case NON_LVALUE_EXPR
:
16867 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16869 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16870 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16873 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16874 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16876 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16877 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16879 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16880 TYPE_PRECISION (TREE_TYPE (t
)),
16881 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16883 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16885 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16886 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16887 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16888 return wi::bit_or (nzbits1
, nzbits2
);
16892 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16894 tree type
= TREE_TYPE (t
);
16895 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16896 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16897 TYPE_PRECISION (type
));
16898 return wi::neg_p (arg1
)
16899 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16900 : wi::lshift (nzbits
, arg1
);
16904 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16906 tree type
= TREE_TYPE (t
);
16907 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16908 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16909 TYPE_PRECISION (type
));
16910 return wi::neg_p (arg1
)
16911 ? wi::lshift (nzbits
, -arg1
)
16912 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16919 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16922 /* Helper function for address compare simplifications in match.pd.
16923 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16924 TYPE is the type of comparison operands.
16925 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16926 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16927 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16928 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16929 and 2 if unknown. */
16932 address_compare (tree_code code
, tree type
, tree op0
, tree op1
,
16933 tree
&base0
, tree
&base1
, poly_int64
&off0
, poly_int64
&off1
,
16936 if (TREE_CODE (op0
) == SSA_NAME
)
16937 op0
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0
));
16938 if (TREE_CODE (op1
) == SSA_NAME
)
16939 op1
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1
));
16940 gcc_checking_assert (TREE_CODE (op0
) == ADDR_EXPR
);
16941 gcc_checking_assert (TREE_CODE (op1
) == ADDR_EXPR
);
16942 base0
= get_addr_base_and_unit_offset (TREE_OPERAND (op0
, 0), &off0
);
16943 base1
= get_addr_base_and_unit_offset (TREE_OPERAND (op1
, 0), &off1
);
16944 if (base0
&& TREE_CODE (base0
) == MEM_REF
)
16946 off0
+= mem_ref_offset (base0
).force_shwi ();
16947 base0
= TREE_OPERAND (base0
, 0);
16949 if (base1
&& TREE_CODE (base1
) == MEM_REF
)
16951 off1
+= mem_ref_offset (base1
).force_shwi ();
16952 base1
= TREE_OPERAND (base1
, 0);
16954 if (base0
== NULL_TREE
|| base1
== NULL_TREE
)
16958 /* Punt in GENERIC on variables with value expressions;
16959 the value expressions might point to fields/elements
16960 of other vars etc. */
16962 && ((VAR_P (base0
) && DECL_HAS_VALUE_EXPR_P (base0
))
16963 || (VAR_P (base1
) && DECL_HAS_VALUE_EXPR_P (base1
))))
16965 else if (decl_in_symtab_p (base0
) && decl_in_symtab_p (base1
))
16967 symtab_node
*node0
= symtab_node::get_create (base0
);
16968 symtab_node
*node1
= symtab_node::get_create (base1
);
16969 equal
= node0
->equal_address_to (node1
);
16971 else if ((DECL_P (base0
)
16972 || TREE_CODE (base0
) == SSA_NAME
16973 || TREE_CODE (base0
) == STRING_CST
)
16975 || TREE_CODE (base1
) == SSA_NAME
16976 || TREE_CODE (base1
) == STRING_CST
))
16977 equal
= (base0
== base1
);
16978 /* Assume different STRING_CSTs with the same content will be
16981 && TREE_CODE (base0
) == STRING_CST
16982 && TREE_CODE (base1
) == STRING_CST
16983 && TREE_STRING_LENGTH (base0
) == TREE_STRING_LENGTH (base1
)
16984 && memcmp (TREE_STRING_POINTER (base0
), TREE_STRING_POINTER (base1
),
16985 TREE_STRING_LENGTH (base0
)) == 0)
16989 if (code
== EQ_EXPR
16991 /* If the offsets are equal we can ignore overflow. */
16992 || known_eq (off0
, off1
)
16993 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
16994 /* Or if we compare using pointers to decls or strings. */
16995 || (POINTER_TYPE_P (type
)
16996 && (DECL_P (base0
) || TREE_CODE (base0
) == STRING_CST
)))
17002 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
17005 /* At this point we know (or assume) the two pointers point at
17006 different objects. */
17007 HOST_WIDE_INT ioff0
= -1, ioff1
= -1;
17008 off0
.is_constant (&ioff0
);
17009 off1
.is_constant (&ioff1
);
17010 /* Punt on non-zero offsets from functions. */
17011 if ((TREE_CODE (base0
) == FUNCTION_DECL
&& ioff0
)
17012 || (TREE_CODE (base1
) == FUNCTION_DECL
&& ioff1
))
17014 /* Or if the bases are neither decls nor string literals. */
17015 if (!DECL_P (base0
) && TREE_CODE (base0
) != STRING_CST
)
17017 if (!DECL_P (base1
) && TREE_CODE (base1
) != STRING_CST
)
17019 /* For initializers, assume addresses of different functions are
17021 if (folding_initializer
17022 && TREE_CODE (base0
) == FUNCTION_DECL
17023 && TREE_CODE (base1
) == FUNCTION_DECL
)
17026 /* Compute whether one address points to the start of one
17027 object and another one to the end of another one. */
17028 poly_int64 size0
= 0, size1
= 0;
17029 if (TREE_CODE (base0
) == STRING_CST
)
17031 if (ioff0
< 0 || ioff0
> TREE_STRING_LENGTH (base0
))
17034 size0
= TREE_STRING_LENGTH (base0
);
17036 else if (TREE_CODE (base0
) == FUNCTION_DECL
)
17040 tree sz0
= DECL_SIZE_UNIT (base0
);
17041 if (!tree_fits_poly_int64_p (sz0
))
17044 size0
= tree_to_poly_int64 (sz0
);
17046 if (TREE_CODE (base1
) == STRING_CST
)
17048 if (ioff1
< 0 || ioff1
> TREE_STRING_LENGTH (base1
))
17051 size1
= TREE_STRING_LENGTH (base1
);
17053 else if (TREE_CODE (base1
) == FUNCTION_DECL
)
17057 tree sz1
= DECL_SIZE_UNIT (base1
);
17058 if (!tree_fits_poly_int64_p (sz1
))
17061 size1
= tree_to_poly_int64 (sz1
);
17065 /* If one offset is pointing (or could be) to the beginning of one
17066 object and the other is pointing to one past the last byte of the
17067 other object, punt. */
17068 if (maybe_eq (off0
, 0) && maybe_eq (off1
, size1
))
17070 else if (maybe_eq (off1
, 0) && maybe_eq (off0
, size0
))
17072 /* If both offsets are the same, there are some cases we know that are
17073 ok. Either if we know they aren't zero, or if we know both sizes
17076 && known_eq (off0
, off1
)
17077 && (known_ne (off0
, 0)
17078 || (known_ne (size0
, 0) && known_ne (size1
, 0))))
17082 /* At this point, equal is 2 if either one or both pointers are out of
17083 bounds of their object, or one points to start of its object and the
17084 other points to end of its object. This is unspecified behavior
17085 e.g. in C++. Otherwise equal is 0. */
17086 if (folding_cxx_constexpr
&& equal
)
17089 /* When both pointers point to string literals, even when equal is 0,
17090 due to tail merging of string literals the pointers might be the same. */
17091 if (TREE_CODE (base0
) == STRING_CST
&& TREE_CODE (base1
) == STRING_CST
)
17095 || ioff0
> TREE_STRING_LENGTH (base0
)
17096 || ioff1
> TREE_STRING_LENGTH (base1
))
17099 /* If the bytes in the string literals starting at the pointers
17100 differ, the pointers need to be different. */
17101 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
,
17102 TREE_STRING_POINTER (base1
) + ioff1
,
17103 MIN (TREE_STRING_LENGTH (base0
) - ioff0
,
17104 TREE_STRING_LENGTH (base1
) - ioff1
)) == 0)
17106 HOST_WIDE_INT ioffmin
= MIN (ioff0
, ioff1
);
17107 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
- ioffmin
,
17108 TREE_STRING_POINTER (base1
) + ioff1
- ioffmin
,
17110 /* If even the bytes in the string literal before the
17111 pointers are the same, the string literals could be
17118 if (folding_cxx_constexpr
)
17121 /* If this is a pointer comparison, ignore for now even
17122 valid equalities where one pointer is the offset zero
17123 of one object and the other to one past end of another one. */
17124 if (!INTEGRAL_TYPE_P (type
))
17127 /* Assume that string literals can't be adjacent to variables
17128 (automatic or global). */
17129 if (TREE_CODE (base0
) == STRING_CST
|| TREE_CODE (base1
) == STRING_CST
)
17132 /* Assume that automatic variables can't be adjacent to global
17134 if (is_global_var (base0
) != is_global_var (base1
))
17140 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17142 ctor_single_nonzero_element (const_tree t
)
17144 unsigned HOST_WIDE_INT idx
;
17145 constructor_elt
*ce
;
17146 tree elt
= NULL_TREE
;
17148 if (TREE_CODE (t
) != CONSTRUCTOR
)
17150 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
17151 if (!integer_zerop (ce
->value
) && !real_zerop (ce
->value
))
17162 namespace selftest
{
17164 /* Helper functions for writing tests of folding trees. */
17166 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17169 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
17172 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
17175 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17176 wrapping WRAPPED_EXPR. */
17179 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
17182 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
17183 ASSERT_NE (wrapped_expr
, result
);
17184 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
17185 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
17188 /* Verify that various arithmetic binary operations are folded
17192 test_arithmetic_folding ()
17194 tree type
= integer_type_node
;
17195 tree x
= create_tmp_var_raw (type
, "x");
17196 tree zero
= build_zero_cst (type
);
17197 tree one
= build_int_cst (type
, 1);
17200 /* 1 <-- (0 + 1) */
17201 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
17203 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
17206 /* (nonlvalue)x <-- (x + 0) */
17207 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
17211 /* 0 <-- (x - x) */
17212 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
17214 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
17217 /* Multiplication. */
17218 /* 0 <-- (x * 0) */
17219 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
17222 /* (nonlvalue)x <-- (x * 1) */
17223 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
17227 namespace test_fold_vec_perm_cst
{
17229 /* Build a VECTOR_CST corresponding to VMODE, and has
17230 encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17231 Fill it with randomized elements, using rand() % THRESHOLD. */
17234 build_vec_cst_rand (machine_mode vmode
, unsigned npatterns
,
17235 unsigned nelts_per_pattern
,
17236 int step
= 0, bool natural_stepped
= false,
17237 int threshold
= 100)
17239 tree inner_type
= lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vmode
), 1);
17240 tree vectype
= build_vector_type_for_mode (inner_type
, vmode
);
17241 tree_vector_builder
builder (vectype
, npatterns
, nelts_per_pattern
);
17243 // Fill a0 for each pattern
17244 for (unsigned i
= 0; i
< npatterns
; i
++)
17245 builder
.quick_push (build_int_cst (inner_type
, rand () % threshold
));
17247 if (nelts_per_pattern
== 1)
17248 return builder
.build ();
17250 // Fill a1 for each pattern
17251 for (unsigned i
= 0; i
< npatterns
; i
++)
17254 if (natural_stepped
)
17256 tree a0
= builder
[i
];
17257 wide_int a0_val
= wi::to_wide (a0
);
17258 wide_int a1_val
= a0_val
+ step
;
17259 a1
= wide_int_to_tree (inner_type
, a1_val
);
17262 a1
= build_int_cst (inner_type
, rand () % threshold
);
17263 builder
.quick_push (a1
);
17265 if (nelts_per_pattern
== 2)
17266 return builder
.build ();
17268 for (unsigned i
= npatterns
* 2; i
< npatterns
* nelts_per_pattern
; i
++)
17270 tree prev_elem
= builder
[i
- npatterns
];
17271 wide_int prev_elem_val
= wi::to_wide (prev_elem
);
17272 wide_int val
= prev_elem_val
+ step
;
17273 builder
.quick_push (wide_int_to_tree (inner_type
, val
));
17276 return builder
.build ();
17279 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17280 when result is VLA. */
17283 validate_res (unsigned npatterns
, unsigned nelts_per_pattern
,
17284 tree res
, tree
*expected_res
)
17286 /* Actual npatterns and encoded_elts in res may be less than expected due
17287 to canonicalization. */
17288 ASSERT_TRUE (res
!= NULL_TREE
);
17289 ASSERT_TRUE (VECTOR_CST_NPATTERNS (res
) <= npatterns
);
17290 ASSERT_TRUE (vector_cst_encoded_nelts (res
) <= npatterns
* nelts_per_pattern
);
17292 for (unsigned i
= 0; i
< npatterns
* nelts_per_pattern
; i
++)
17293 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res
, i
), expected_res
[i
], 0));
17296 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17297 when the result is VLS. */
17300 validate_res_vls (tree res
, tree
*expected_res
, unsigned expected_nelts
)
17302 ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res
), expected_nelts
));
17303 for (unsigned i
= 0; i
< expected_nelts
; i
++)
17304 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res
, i
), expected_res
[i
], 0));
17307 /* Helper routine to push multiple elements into BUILDER. */
17308 template<unsigned N
>
17309 static void builder_push_elems (vec_perm_builder
& builder
,
17310 poly_uint64 (&elems
)[N
])
17312 for (unsigned i
= 0; i
< N
; i
++)
17313 builder
.quick_push (elems
[i
]);
17316 #define ARG0(index) vector_cst_elt (arg0, index)
17317 #define ARG1(index) vector_cst_elt (arg1, index)
17319 /* Test cases where result is VNx4SI and input vectors are V4SI. */
17322 test_vnx4si_v4si (machine_mode vnx4si_mode
, machine_mode v4si_mode
)
17324 for (int i
= 0; i
< 10; i
++)
17327 sel = { 0, 4, 1, 5, ... }
17328 res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17330 tree arg0
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17331 tree arg1
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17334 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vnx4si_mode
), 1);
17335 tree res_type
= build_vector_type_for_mode (inner_type
, vnx4si_mode
);
17337 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17338 vec_perm_builder
builder (res_len
, 4, 1);
17339 poly_uint64 mask_elems
[] = { 0, 4, 1, 5 };
17340 builder_push_elems (builder
, mask_elems
);
17342 vec_perm_indices
sel (builder
, 2, res_len
);
17343 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17345 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17346 validate_res (4, 1, res
, expected_res
);
17349 /* Case 2: Same as case 1, but contains an out of bounds access which
17350 should wrap around.
17351 sel = {0, 8, 4, 12, ...} (4, 1)
17352 res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17354 tree arg0
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17355 tree arg1
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17358 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vnx4si_mode
), 1);
17359 tree res_type
= build_vector_type_for_mode (inner_type
, vnx4si_mode
);
17361 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17362 vec_perm_builder
builder (res_len
, 4, 1);
17363 poly_uint64 mask_elems
[] = { 0, 8, 4, 12 };
17364 builder_push_elems (builder
, mask_elems
);
17366 vec_perm_indices
sel (builder
, 2, res_len
);
17367 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17369 tree expected_res
[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17370 validate_res (4, 1, res
, expected_res
);
17375 /* Test cases where result is V4SI and input vectors are VNx4SI. */
17378 test_v4si_vnx4si (machine_mode v4si_mode
, machine_mode vnx4si_mode
)
17380 for (int i
= 0; i
< 10; i
++)
17383 sel = { 0, 1, 2, 3}
17384 res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17386 tree arg0
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17387 tree arg1
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17390 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (v4si_mode
), 1);
17391 tree res_type
= build_vector_type_for_mode (inner_type
, v4si_mode
);
17393 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17394 vec_perm_builder
builder (res_len
, 4, 1);
17395 poly_uint64 mask_elems
[] = {0, 1, 2, 3};
17396 builder_push_elems (builder
, mask_elems
);
17398 vec_perm_indices
sel (builder
, 2, res_len
);
17399 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17401 tree expected_res
[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17402 validate_res_vls (res
, expected_res
, 4);
17405 /* Case 2: Same as Case 1, but crossing input vector.
17407 In this case,the index 4 is ambiguous since len = 4 + 4x.
17408 Since we cannot determine, which vector to choose from during
17409 compile time, should return NULL_TREE. */
17411 tree arg0
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17412 tree arg1
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17415 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (v4si_mode
), 1);
17416 tree res_type
= build_vector_type_for_mode (inner_type
, v4si_mode
);
17418 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17419 vec_perm_builder
builder (res_len
, 4, 1);
17420 poly_uint64 mask_elems
[] = {0, 2, 4, 6};
17421 builder_push_elems (builder
, mask_elems
);
17423 vec_perm_indices
sel (builder
, 2, res_len
);
17424 const char *reason
;
17425 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
, &reason
);
17427 ASSERT_TRUE (res
== NULL_TREE
);
17428 ASSERT_TRUE (!strcmp (reason
, "cannot divide selector element by arg len"));
17433 /* Test all input vectors. */
17436 test_all_nunits (machine_mode vmode
)
17438 /* Test with 10 different inputs. */
17439 for (int i
= 0; i
< 10; i
++)
17441 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17442 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17443 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17445 /* Case 1: mask = {0, ...} // (1, 1)
17446 res = { arg0[0], ... } // (1, 1) */
17448 vec_perm_builder
builder (len
, 1, 1);
17449 builder
.quick_push (0);
17450 vec_perm_indices
sel (builder
, 2, len
);
17451 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17452 tree expected_res
[] = { ARG0(0) };
17453 validate_res (1, 1, res
, expected_res
);
17456 /* Case 2: mask = {len, ...} // (1, 1)
17457 res = { arg1[0], ... } // (1, 1) */
17459 vec_perm_builder
builder (len
, 1, 1);
17460 builder
.quick_push (len
);
17461 vec_perm_indices
sel (builder
, 2, len
);
17462 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17464 tree expected_res
[] = { ARG1(0) };
17465 validate_res (1, 1, res
, expected_res
);
17470 /* Test all vectors which contain at-least 2 elements. */
17473 test_nunits_min_2 (machine_mode vmode
)
17475 for (int i
= 0; i
< 10; i
++)
17477 /* Case 1: mask = { 0, len, ... } // (2, 1)
17478 res = { arg0[0], arg1[0], ... } // (2, 1) */
17480 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17481 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17482 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17484 vec_perm_builder
builder (len
, 2, 1);
17485 poly_uint64 mask_elems
[] = { 0, len
};
17486 builder_push_elems (builder
, mask_elems
);
17488 vec_perm_indices
sel (builder
, 2, len
);
17489 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17491 tree expected_res
[] = { ARG0(0), ARG1(0) };
17492 validate_res (2, 1, res
, expected_res
);
17495 /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17496 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17498 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17499 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17500 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17502 vec_perm_builder
builder (len
, 2, 2);
17503 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17504 builder_push_elems (builder
, mask_elems
);
17506 vec_perm_indices
sel (builder
, 2, len
);
17507 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17509 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17510 validate_res (2, 2, res
, expected_res
);
17513 /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17514 Test that the stepped sequence of the pattern selects from
17515 same input pattern. Since input vectors have npatterns = 2,
17516 and step (a2 - a1) = 1, step is not a multiple of npatterns
17517 in input vector. So return NULL_TREE. */
17519 tree arg0
= build_vec_cst_rand (vmode
, 2, 3, 1, true);
17520 tree arg1
= build_vec_cst_rand (vmode
, 2, 3, 1);
17521 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17523 vec_perm_builder
builder (len
, 1, 3);
17524 poly_uint64 mask_elems
[] = { 0, 0, 1 };
17525 builder_push_elems (builder
, mask_elems
);
17527 vec_perm_indices
sel (builder
, 2, len
);
17528 const char *reason
;
17529 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
,
17531 ASSERT_TRUE (res
== NULL_TREE
);
17532 ASSERT_TRUE (!strcmp (reason
, "step is not multiple of npatterns"));
17535 /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17536 Test that stepped sequence of the pattern selects from arg0.
17537 res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17539 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1, true);
17540 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17541 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17543 vec_perm_builder
builder (len
, 1, 3);
17544 poly_uint64 mask_elems
[] = { len
, 0, 1 };
17545 builder_push_elems (builder
, mask_elems
);
17547 vec_perm_indices
sel (builder
, 2, len
);
17548 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17550 tree expected_res
[] = { ARG1(0), ARG0(0), ARG0(1) };
17551 validate_res (1, 3, res
, expected_res
);
17554 /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17555 In this case ensure that arg has a natural stepped sequence
17556 to preserve arg's encoding.
17558 As a concrete example, consider:
17559 arg0: { -16, -9, -10, ... } // (1, 3)
17560 arg1: { -12, -5, -6, ... } // (1, 3)
17561 sel = { 0, len, len + 1, ... } // (1, 3)
17563 This will create res with following encoding:
17564 res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17565 = { -16, -12, -5, ... }
17567 The step in above encoding would be: (-5) - (-12) = 7
17568 And hence res[3] would be computed as -5 + 7 = 2.
17569 instead of arg1[2], ie, -6.
17570 Ensure that valid_mask_for_fold_vec_perm_cst returns false
17573 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17574 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17575 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17577 vec_perm_builder
builder (len
, 1, 3);
17578 poly_uint64 mask_elems
[] = { 0, len
, len
+1 };
17579 builder_push_elems (builder
, mask_elems
);
17581 vec_perm_indices
sel (builder
, 2, len
);
17582 const char *reason
;
17583 /* FIXME: It may happen that build_vec_cst_rand may build a natural
17584 stepped pattern, even if we didn't explicitly tell it to. So folding
17585 may not always fail, but if it does, ensure that's because arg1 does
17586 not have a natural stepped sequence (and not due to other reason) */
17587 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17588 if (res
== NULL_TREE
)
17589 ASSERT_TRUE (!strcmp (reason
, "not a natural stepped sequence"));
17592 /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17593 sequence and thus folding should be valid for this case. */
17595 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17596 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1, true);
17597 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17599 vec_perm_builder
builder (len
, 1, 3);
17600 poly_uint64 mask_elems
[] = { 0, len
, len
+1 };
17601 builder_push_elems (builder
, mask_elems
);
17603 vec_perm_indices
sel (builder
, 2, len
);
17604 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17606 tree expected_res
[] = { ARG0(0), ARG1(0), ARG1(1) };
17607 validate_res (1, 3, res
, expected_res
);
17610 /* Case 8: Same as aarch64/sve/slp_3.c:
17611 arg0, arg1 are dup vectors.
17612 sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17613 So res = { arg0[0], arg1[0], ... } // (2, 1)
17615 In this case, since the input vectors are dup, only the first two
17616 elements per pattern in sel are considered significant. */
17618 tree arg0
= build_vec_cst_rand (vmode
, 1, 1);
17619 tree arg1
= build_vec_cst_rand (vmode
, 1, 1);
17620 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17622 vec_perm_builder
builder (len
, 2, 3);
17623 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1, 2, len
+ 2 };
17624 builder_push_elems (builder
, mask_elems
);
17626 vec_perm_indices
sel (builder
, 2, len
);
17627 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17629 tree expected_res
[] = { ARG0(0), ARG1(0) };
17630 validate_res (2, 1, res
, expected_res
);
17635 /* Test all vectors which contain at-least 4 elements. */
17638 test_nunits_min_4 (machine_mode vmode
)
17640 for (int i
= 0; i
< 10; i
++)
17642 /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17643 res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17645 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17646 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17647 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17649 vec_perm_builder
builder (len
, 4, 1);
17650 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17651 builder_push_elems (builder
, mask_elems
);
17653 vec_perm_indices
sel (builder
, 2, len
);
17654 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17656 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17657 validate_res (4, 1, res
, expected_res
);
17660 /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17661 res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17663 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17664 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17665 poly_uint64 arg0_len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17667 vec_perm_builder
builder (arg0_len
, 1, 3);
17668 poly_uint64 mask_elems
[] = {0, 1, 2};
17669 builder_push_elems (builder
, mask_elems
);
17671 vec_perm_indices
sel (builder
, 2, arg0_len
);
17672 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17673 tree expected_res
[] = { ARG0(0), ARG0(1), ARG0(2) };
17674 validate_res (1, 3, res
, expected_res
);
17677 /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17678 res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17680 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17681 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17682 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17684 vec_perm_builder
builder (len
, 1, 3);
17685 poly_uint64 mask_elems
[] = {len
, len
+ 1, len
+ 2};
17686 builder_push_elems (builder
, mask_elems
);
17688 vec_perm_indices
sel (builder
, 2, len
);
17689 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17690 tree expected_res
[] = { ARG1(0), ARG1(1), ARG1(2) };
17691 validate_res (1, 3, res
, expected_res
);
17695 sel = { len, 0, 2, ... } // (1, 3)
17696 This should return NULL because we cross the input vectors.
17698 Let's assume len = C + Cx
17701 esel = arg0_len / sel_npatterns = C + Cx
17702 ae = 0 + (esel - 2) * S
17703 = 0 + (C + Cx - 2) * 2
17707 Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17708 Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17709 Since q1 != qe, we cross input vectors.
17710 So return NULL_TREE. */
17712 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17713 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17714 poly_uint64 arg0_len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17716 vec_perm_builder
builder (arg0_len
, 1, 3);
17717 poly_uint64 mask_elems
[] = { arg0_len
, 0, 2 };
17718 builder_push_elems (builder
, mask_elems
);
17720 vec_perm_indices
sel (builder
, 2, arg0_len
);
17721 const char *reason
;
17722 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17723 ASSERT_TRUE (res
== NULL_TREE
);
17724 ASSERT_TRUE (!strcmp (reason
, "crossed input vectors"));
17727 /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17728 mask = { 0, len, 1, len + 1, ...} // (2, 2)
17729 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17731 Note that fold_vec_perm_cst will set
17732 res_npatterns = max(4, max(4, 2)) = 4
17733 However after canonicalizing, we will end up with shape (2, 2). */
17735 tree arg0
= build_vec_cst_rand (vmode
, 4, 1);
17736 tree arg1
= build_vec_cst_rand (vmode
, 4, 1);
17737 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17739 vec_perm_builder
builder (len
, 2, 2);
17740 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17741 builder_push_elems (builder
, mask_elems
);
17743 vec_perm_indices
sel (builder
, 2, len
);
17744 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17745 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17746 validate_res (2, 2, res
, expected_res
);
17749 /* Case 6: Test combination in sel, where one pattern is dup and other
17750 is stepped sequence.
17751 sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17752 res = { arg0[0], arg0[0], arg0[0],
17753 arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17755 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17756 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17757 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17759 vec_perm_builder
builder (len
, 2, 3);
17760 poly_uint64 mask_elems
[] = { 0, 0, 0, 1, 0, 2 };
17761 builder_push_elems (builder
, mask_elems
);
17763 vec_perm_indices
sel (builder
, 2, len
);
17764 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17766 tree expected_res
[] = { ARG0(0), ARG0(0), ARG0(0),
17767 ARG0(1), ARG0(0), ARG0(2) };
17768 validate_res (2, 3, res
, expected_res
);
17771 /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17772 when arg0, arg1 and sel have different number of patterns.
17773 arg0 is of shape (1, 1)
17774 arg1 is of shape (4, 1)
17775 sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17777 In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17779 step = (len+2) - (len+1) = 1
17780 arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17781 Since step is not a multiple of arg_npatterns,
17782 valid_mask_for_fold_vec_perm_cst should return false,
17783 and thus fold_vec_perm_cst should return NULL_TREE. */
17785 tree arg0
= build_vec_cst_rand (vmode
, 1, 1);
17786 tree arg1
= build_vec_cst_rand (vmode
, 4, 1);
17787 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17789 vec_perm_builder
builder (len
, 2, 3);
17790 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1, 2, len
+ 2 };
17791 builder_push_elems (builder
, mask_elems
);
17793 vec_perm_indices
sel (builder
, 2, len
);
17794 const char *reason
;
17795 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17797 ASSERT_TRUE (res
== NULL_TREE
);
17798 ASSERT_TRUE (!strcmp (reason
, "step is not multiple of npatterns"));
17801 /* Case 8: PR111754: When input vector is not a stepped sequence,
17802 check that the result is not a stepped sequence either, even
17803 if sel has a stepped sequence. */
17805 tree arg0
= build_vec_cst_rand (vmode
, 1, 2);
17806 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17808 vec_perm_builder
builder (len
, 1, 3);
17809 poly_uint64 mask_elems
[] = { 0, 1, 2 };
17810 builder_push_elems (builder
, mask_elems
);
17812 vec_perm_indices
sel (builder
, 1, len
);
17813 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg0
, sel
);
17815 tree expected_res
[] = { ARG0(0), ARG0(1) };
17816 validate_res (sel
.encoding ().npatterns (), 2, res
, expected_res
);
17819 /* Case 9: If sel doesn't contain a stepped sequence,
17820 check that the result has same encoding as sel, irrespective
17821 of shape of input vectors. */
17823 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17824 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17825 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17827 vec_perm_builder
builder (len
, 1, 2);
17828 poly_uint64 mask_elems
[] = { 0, len
};
17829 builder_push_elems (builder
, mask_elems
);
17831 vec_perm_indices
sel (builder
, 2, len
);
17832 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17834 tree expected_res
[] = { ARG0(0), ARG1(0) };
17835 validate_res (sel
.encoding ().npatterns (),
17836 sel
.encoding ().nelts_per_pattern (), res
, expected_res
);
17841 /* Test all vectors which contain at-least 8 elements. */
17844 test_nunits_min_8 (machine_mode vmode
)
17846 for (int i
= 0; i
< 10; i
++)
17848 /* Case 1: sel_npatterns (4) > input npatterns (2)
17849 sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17850 res: { arg0[0], arg0[0], arg0[0], arg1[0],
17851 arg0[2], arg0[0], arg0[3], arg1[0],
17852 arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17854 tree arg0
= build_vec_cst_rand (vmode
, 2, 3, 2);
17855 tree arg1
= build_vec_cst_rand (vmode
, 2, 3, 2);
17856 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17858 vec_perm_builder
builder(len
, 4, 3);
17859 poly_uint64 mask_elems
[] = { 0, 0, 1, len
, 2, 0, 3, len
,
17861 builder_push_elems (builder
, mask_elems
);
17863 vec_perm_indices
sel (builder
, 2, len
);
17864 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17866 tree expected_res
[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17867 ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17868 ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17869 validate_res (4, 3, res
, expected_res
);
17874 /* Test vectors for which nunits[0] <= 4. */
17877 test_nunits_max_4 (machine_mode vmode
)
17879 /* Case 1: mask = {0, 4, ...} // (1, 2)
17880 This should return NULL_TREE because the index 4 may choose
17881 from either arg0 or arg1 depending on vector length. */
17883 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17884 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17885 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17887 vec_perm_builder
builder (len
, 1, 2);
17888 poly_uint64 mask_elems
[] = {0, 4};
17889 builder_push_elems (builder
, mask_elems
);
17891 vec_perm_indices
sel (builder
, 2, len
);
17892 const char *reason
;
17893 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17894 ASSERT_TRUE (res
== NULL_TREE
);
17895 ASSERT_TRUE (reason
!= NULL
);
17896 ASSERT_TRUE (!strcmp (reason
, "cannot divide selector element by arg len"));
17903 /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17906 is_simple_vla_size (poly_uint64 size
)
17908 if (size
.is_constant ()
17909 || !pow2p_hwi (size
.coeffs
[0]))
17911 for (unsigned i
= 1; i
< ARRAY_SIZE (size
.coeffs
); ++i
)
17912 if (size
.coeffs
[i
] != (i
<= 1 ? size
.coeffs
[0] : 0))
17917 /* Execute fold_vec_perm_cst unit tests. */
17922 machine_mode vnx4si_mode
= E_VOIDmode
;
17923 machine_mode v4si_mode
= E_VOIDmode
;
17925 machine_mode vmode
;
17926 FOR_EACH_MODE_IN_CLASS (vmode
, MODE_VECTOR_INT
)
17928 /* Obtain modes corresponding to VNx4SI and V4SI,
17929 to call mixed mode tests below.
17930 FIXME: Is there a better way to do this ? */
17931 if (GET_MODE_INNER (vmode
) == SImode
)
17933 poly_uint64 nunits
= GET_MODE_NUNITS (vmode
);
17934 if (is_simple_vla_size (nunits
)
17935 && nunits
.coeffs
[0] == 4)
17936 vnx4si_mode
= vmode
;
17937 else if (known_eq (nunits
, poly_uint64 (4)))
17941 if (!is_simple_vla_size (GET_MODE_NUNITS (vmode
))
17942 || !targetm
.vector_mode_supported_p (vmode
))
17945 poly_uint64 nunits
= GET_MODE_NUNITS (vmode
);
17946 test_all_nunits (vmode
);
17947 if (nunits
.coeffs
[0] >= 2)
17948 test_nunits_min_2 (vmode
);
17949 if (nunits
.coeffs
[0] >= 4)
17950 test_nunits_min_4 (vmode
);
17951 if (nunits
.coeffs
[0] >= 8)
17952 test_nunits_min_8 (vmode
);
17954 if (nunits
.coeffs
[0] <= 4)
17955 test_nunits_max_4 (vmode
);
17958 if (vnx4si_mode
!= E_VOIDmode
&& v4si_mode
!= E_VOIDmode
17959 && targetm
.vector_mode_supported_p (vnx4si_mode
)
17960 && targetm
.vector_mode_supported_p (v4si_mode
))
17962 test_vnx4si_v4si (vnx4si_mode
, v4si_mode
);
17963 test_v4si_vnx4si (v4si_mode
, vnx4si_mode
);
17966 } // end of test_fold_vec_perm_cst namespace
17968 /* Verify that various binary operations on vectors are folded
17972 test_vector_folding ()
17974 tree inner_type
= integer_type_node
;
17975 tree type
= build_vector_type (inner_type
, 4);
17976 tree zero
= build_zero_cst (type
);
17977 tree one
= build_one_cst (type
);
17978 tree index
= build_index_vector (type
, 0, 1);
17980 /* Verify equality tests that return a scalar boolean result. */
17981 tree res_type
= boolean_type_node
;
17982 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
17983 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
17984 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
17985 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
17986 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
17987 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
17989 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
17991 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
17995 /* Verify folding of VEC_DUPLICATE_EXPRs. */
17998 test_vec_duplicate_folding ()
18000 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
18001 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
18002 /* This will be 1 if VEC_MODE isn't a vector mode. */
18003 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
18005 tree type
= build_vector_type (ssizetype
, nunits
);
18006 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
18007 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
18008 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
18011 /* Run all of the selftests within this file. */
18014 fold_const_cc_tests ()
18016 test_arithmetic_folding ();
18017 test_vector_folding ();
18018 test_vec_duplicate_folding ();
18019 test_fold_vec_perm_cst::test ();
18022 } // namespace selftest
18024 #endif /* CHECKING_P */