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 /* (1) If SEL is a suitable mask as determined by
10807 valid_mask_for_fold_vec_perm_cst_p, then:
10808 res_npatterns = max of npatterns between ARG0, ARG1, and SEL
10809 res_nelts_per_pattern = max of nelts_per_pattern between
10810 ARG0, ARG1 and SEL.
10811 (2) If SEL is not a suitable mask, and TYPE is VLS then:
10812 res_npatterns = nelts in result vector.
10813 res_nelts_per_pattern = 1.
10814 This exception is made so that VLS ARG0, ARG1 and SEL work as before. */
10815 if (valid_mask_for_fold_vec_perm_cst_p (arg0
, arg1
, sel
, reason
))
10818 = std::max (VECTOR_CST_NPATTERNS (arg0
),
10819 std::max (VECTOR_CST_NPATTERNS (arg1
),
10820 sel
.encoding ().npatterns ()));
10822 res_nelts_per_pattern
10823 = std::max (VECTOR_CST_NELTS_PER_PATTERN (arg0
),
10824 std::max (VECTOR_CST_NELTS_PER_PATTERN (arg1
),
10825 sel
.encoding ().nelts_per_pattern ()));
10827 res_nelts
= res_npatterns
* res_nelts_per_pattern
;
10829 else if (TYPE_VECTOR_SUBPARTS (type
).is_constant (&res_nelts
))
10831 res_npatterns
= res_nelts
;
10832 res_nelts_per_pattern
= 1;
10837 tree_vector_builder
out_elts (type
, res_npatterns
, res_nelts_per_pattern
);
10838 for (unsigned i
= 0; i
< res_nelts
; i
++)
10840 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
10843 unsigned HOST_WIDE_INT index
;
10845 /* Punt if sel[i] /trunc_div len cannot be determined,
10846 because the input vector to be chosen will depend on
10847 runtime vector length.
10848 For example if len == 4 + 4x, and sel[i] == 4,
10849 If len at runtime equals 4, we choose arg1[0].
10850 For any other value of len > 4 at runtime, we choose arg0[4].
10851 which makes the element choice dependent on runtime vector length. */
10852 if (!can_div_trunc_p (sel
[i
], len
, &q
, &r
))
10855 *reason
= "cannot divide selector element by arg len";
10859 /* sel[i] % len will give the index of element in the chosen input
10860 vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10861 we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10862 if (!r
.is_constant (&index
))
10865 *reason
= "remainder is not constant";
10869 tree arg
= ((q
& 1) == 0) ? arg0
: arg1
;
10870 tree elem
= vector_cst_elt (arg
, index
);
10871 out_elts
.quick_push (elem
);
10874 return out_elts
.build ();
10877 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10878 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10879 NULL_TREE otherwise. */
10882 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10885 unsigned HOST_WIDE_INT nelts
;
10887 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), sel
.length ())
10888 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
10889 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))));
10891 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10892 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10895 if (TREE_CODE (arg0
) == VECTOR_CST
10896 && TREE_CODE (arg1
) == VECTOR_CST
)
10897 return fold_vec_perm_cst (type
, arg0
, arg1
, sel
);
10899 /* For fall back case, we want to ensure we have VLS vectors
10900 with equal length. */
10901 if (!sel
.length ().is_constant (&nelts
))
10904 gcc_assert (known_eq (sel
.length (),
10905 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))));
10906 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10907 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10908 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10911 vec
<constructor_elt
, va_gc
> *v
;
10912 vec_alloc (v
, nelts
);
10913 for (i
= 0; i
< nelts
; i
++)
10915 HOST_WIDE_INT index
;
10916 if (!sel
[i
].is_constant (&index
))
10918 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, in_elts
[index
]);
10920 return build_constructor (type
, v
);
10923 /* Try to fold a pointer difference of type TYPE two address expressions of
10924 array references AREF0 and AREF1 using location LOC. Return a
10925 simplified expression for the difference or NULL_TREE. */
10928 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10929 tree aref0
, tree aref1
,
10930 bool use_pointer_diff
)
10932 tree base0
= TREE_OPERAND (aref0
, 0);
10933 tree base1
= TREE_OPERAND (aref1
, 0);
10934 tree base_offset
= build_int_cst (type
, 0);
10936 /* If the bases are array references as well, recurse. If the bases
10937 are pointer indirections compute the difference of the pointers.
10938 If the bases are equal, we are set. */
10939 if ((TREE_CODE (base0
) == ARRAY_REF
10940 && TREE_CODE (base1
) == ARRAY_REF
10942 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10943 use_pointer_diff
)))
10944 || (INDIRECT_REF_P (base0
)
10945 && INDIRECT_REF_P (base1
)
10948 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10949 TREE_OPERAND (base0
, 0),
10950 TREE_OPERAND (base1
, 0))
10951 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10952 fold_convert (type
,
10953 TREE_OPERAND (base0
, 0)),
10954 fold_convert (type
,
10955 TREE_OPERAND (base1
, 0)))))
10956 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10958 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10959 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10960 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10961 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10962 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10964 fold_build2_loc (loc
, MULT_EXPR
, type
,
10970 /* If the real or vector real constant CST of type TYPE has an exact
10971 inverse, return it, else return NULL. */
10974 exact_inverse (tree type
, tree cst
)
10980 switch (TREE_CODE (cst
))
10983 r
= TREE_REAL_CST (cst
);
10985 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10986 return build_real (type
, r
);
10992 unit_type
= TREE_TYPE (type
);
10993 mode
= TYPE_MODE (unit_type
);
10995 tree_vector_builder elts
;
10996 if (!elts
.new_unary_operation (type
, cst
, false))
10998 unsigned int count
= elts
.encoded_nelts ();
10999 for (unsigned int i
= 0; i
< count
; ++i
)
11001 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
11002 if (!exact_real_inverse (mode
, &r
))
11004 elts
.quick_push (build_real (unit_type
, r
));
11007 return elts
.build ();
11015 /* Mask out the tz least significant bits of X of type TYPE where
11016 tz is the number of trailing zeroes in Y. */
11018 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
11020 int tz
= wi::ctz (y
);
11022 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
11026 /* Return true when T is an address and is known to be nonzero.
11027 For floating point we further ensure that T is not denormal.
11028 Similar logic is present in nonzero_address in rtlanal.h.
11030 If the return value is based on the assumption that signed overflow
11031 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
11032 change *STRICT_OVERFLOW_P. */
11035 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
11037 tree type
= TREE_TYPE (t
);
11038 enum tree_code code
;
11040 /* Doing something useful for floating point would need more work. */
11041 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
11044 code
= TREE_CODE (t
);
11045 switch (TREE_CODE_CLASS (code
))
11048 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
11049 strict_overflow_p
);
11051 case tcc_comparison
:
11052 return tree_binary_nonzero_warnv_p (code
, type
,
11053 TREE_OPERAND (t
, 0),
11054 TREE_OPERAND (t
, 1),
11055 strict_overflow_p
);
11057 case tcc_declaration
:
11058 case tcc_reference
:
11059 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
11067 case TRUTH_NOT_EXPR
:
11068 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
11069 strict_overflow_p
);
11071 case TRUTH_AND_EXPR
:
11072 case TRUTH_OR_EXPR
:
11073 case TRUTH_XOR_EXPR
:
11074 return tree_binary_nonzero_warnv_p (code
, type
,
11075 TREE_OPERAND (t
, 0),
11076 TREE_OPERAND (t
, 1),
11077 strict_overflow_p
);
11083 case WITH_SIZE_EXPR
:
11085 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
11087 case COMPOUND_EXPR
:
11090 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
11091 strict_overflow_p
);
11094 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
11095 strict_overflow_p
);
11099 tree fndecl
= get_callee_fndecl (t
);
11100 if (!fndecl
) return false;
11101 if (flag_delete_null_pointer_checks
&& !flag_check_new
11102 && DECL_IS_OPERATOR_NEW_P (fndecl
)
11103 && !TREE_NOTHROW (fndecl
))
11105 if (flag_delete_null_pointer_checks
11106 && lookup_attribute ("returns_nonnull",
11107 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
11109 return alloca_call_p (t
);
11118 /* Return true when T is an address and is known to be nonzero.
11119 Handle warnings about undefined signed overflow. */
11122 tree_expr_nonzero_p (tree t
)
11124 bool ret
, strict_overflow_p
;
11126 strict_overflow_p
= false;
11127 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
11128 if (strict_overflow_p
)
11129 fold_overflow_warning (("assuming signed overflow does not occur when "
11130 "determining that expression is always "
11132 WARN_STRICT_OVERFLOW_MISC
);
11136 /* Return true if T is known not to be equal to an integer W. */
11139 expr_not_equal_to (tree t
, const wide_int
&w
)
11142 switch (TREE_CODE (t
))
11145 return wi::to_wide (t
) != w
;
11148 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
11151 get_range_query (cfun
)->range_of_expr (vr
, t
);
11152 if (!vr
.undefined_p () && !vr
.contains_p (w
))
11154 /* If T has some known zero bits and W has any of those bits set,
11155 then T is known not to be equal to W. */
11156 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
11157 TYPE_PRECISION (TREE_TYPE (t
))), 0))
11166 /* Fold a binary expression of code CODE and type TYPE with operands
11167 OP0 and OP1. LOC is the location of the resulting expression.
11168 Return the folded expression if folding is successful. Otherwise,
11169 return NULL_TREE. */
11172 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
11173 tree op0
, tree op1
)
11175 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11176 tree arg0
, arg1
, tem
;
11177 tree t1
= NULL_TREE
;
11178 bool strict_overflow_p
;
11181 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11182 && TREE_CODE_LENGTH (code
) == 2
11183 && op0
!= NULL_TREE
11184 && op1
!= NULL_TREE
);
11189 /* Strip any conversions that don't change the mode. This is
11190 safe for every expression, except for a comparison expression
11191 because its signedness is derived from its operands. So, in
11192 the latter case, only strip conversions that don't change the
11193 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
11196 Note that this is done as an internal manipulation within the
11197 constant folder, in order to find the simplest representation
11198 of the arguments so that their form can be studied. In any
11199 cases, the appropriate type conversions should be put back in
11200 the tree that will get out of the constant folder. */
11202 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
11204 STRIP_SIGN_NOPS (arg0
);
11205 STRIP_SIGN_NOPS (arg1
);
11213 /* Note that TREE_CONSTANT isn't enough: static var addresses are
11214 constant but we can't do arithmetic on them. */
11215 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
11217 tem
= const_binop (code
, type
, arg0
, arg1
);
11218 if (tem
!= NULL_TREE
)
11220 if (TREE_TYPE (tem
) != type
)
11221 tem
= fold_convert_loc (loc
, type
, tem
);
11226 /* If this is a commutative operation, and ARG0 is a constant, move it
11227 to ARG1 to reduce the number of tests below. */
11228 if (commutative_tree_code (code
)
11229 && tree_swap_operands_p (arg0
, arg1
))
11230 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
11232 /* Likewise if this is a comparison, and ARG0 is a constant, move it
11233 to ARG1 to reduce the number of tests below. */
11234 if (kind
== tcc_comparison
11235 && tree_swap_operands_p (arg0
, arg1
))
11236 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
11238 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
11242 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11244 First check for cases where an arithmetic operation is applied to a
11245 compound, conditional, or comparison operation. Push the arithmetic
11246 operation inside the compound or conditional to see if any folding
11247 can then be done. Convert comparison to conditional for this purpose.
11248 The also optimizes non-constant cases that used to be done in
11251 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11252 one of the operands is a comparison and the other is a comparison, a
11253 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11254 code below would make the expression more complex. Change it to a
11255 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11256 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11258 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
11259 || code
== EQ_EXPR
|| code
== NE_EXPR
)
11260 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
11261 && ((truth_value_p (TREE_CODE (arg0
))
11262 && (truth_value_p (TREE_CODE (arg1
))
11263 || (TREE_CODE (arg1
) == BIT_AND_EXPR
11264 && integer_onep (TREE_OPERAND (arg1
, 1)))))
11265 || (truth_value_p (TREE_CODE (arg1
))
11266 && (truth_value_p (TREE_CODE (arg0
))
11267 || (TREE_CODE (arg0
) == BIT_AND_EXPR
11268 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
11270 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
11271 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
11274 fold_convert_loc (loc
, boolean_type_node
, arg0
),
11275 fold_convert_loc (loc
, boolean_type_node
, arg1
));
11277 if (code
== EQ_EXPR
)
11278 tem
= invert_truthvalue_loc (loc
, tem
);
11280 return fold_convert_loc (loc
, type
, tem
);
11283 if (TREE_CODE_CLASS (code
) == tcc_binary
11284 || TREE_CODE_CLASS (code
) == tcc_comparison
)
11286 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
11288 tem
= fold_build2_loc (loc
, code
, type
,
11289 fold_convert_loc (loc
, TREE_TYPE (op0
),
11290 TREE_OPERAND (arg0
, 1)), op1
);
11291 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
11294 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
11296 tem
= fold_build2_loc (loc
, code
, type
, op0
,
11297 fold_convert_loc (loc
, TREE_TYPE (op1
),
11298 TREE_OPERAND (arg1
, 1)));
11299 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
11303 if (TREE_CODE (arg0
) == COND_EXPR
11304 || TREE_CODE (arg0
) == VEC_COND_EXPR
11305 || COMPARISON_CLASS_P (arg0
))
11307 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
11309 /*cond_first_p=*/1);
11310 if (tem
!= NULL_TREE
)
11314 if (TREE_CODE (arg1
) == COND_EXPR
11315 || TREE_CODE (arg1
) == VEC_COND_EXPR
11316 || COMPARISON_CLASS_P (arg1
))
11318 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
11320 /*cond_first_p=*/0);
11321 if (tem
!= NULL_TREE
)
11329 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11330 if (TREE_CODE (arg0
) == ADDR_EXPR
11331 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
11333 tree iref
= TREE_OPERAND (arg0
, 0);
11334 return fold_build2 (MEM_REF
, type
,
11335 TREE_OPERAND (iref
, 0),
11336 int_const_binop (PLUS_EXPR
, arg1
,
11337 TREE_OPERAND (iref
, 1)));
11340 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11341 if (TREE_CODE (arg0
) == ADDR_EXPR
11342 && handled_component_p (TREE_OPERAND (arg0
, 0)))
11345 poly_int64 coffset
;
11346 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
11350 return fold_build2 (MEM_REF
, type
,
11351 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
11352 int_const_binop (PLUS_EXPR
, arg1
,
11353 size_int (coffset
)));
11358 case POINTER_PLUS_EXPR
:
11359 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11360 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11361 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
11362 return fold_convert_loc (loc
, type
,
11363 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
11364 fold_convert_loc (loc
, sizetype
,
11366 fold_convert_loc (loc
, sizetype
,
11372 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
11374 /* X + (X / CST) * -CST is X % CST. */
11375 if (TREE_CODE (arg1
) == MULT_EXPR
11376 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
11377 && operand_equal_p (arg0
,
11378 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
11380 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
11381 tree cst1
= TREE_OPERAND (arg1
, 1);
11382 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
11384 if (sum
&& integer_zerop (sum
))
11385 return fold_convert_loc (loc
, type
,
11386 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
11387 TREE_TYPE (arg0
), arg0
,
11392 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11393 one. Make sure the type is not saturating and has the signedness of
11394 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11395 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11396 if ((TREE_CODE (arg0
) == MULT_EXPR
11397 || TREE_CODE (arg1
) == MULT_EXPR
)
11398 && !TYPE_SATURATING (type
)
11399 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11400 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11401 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11403 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11408 if (! FLOAT_TYPE_P (type
))
11410 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11411 (plus (plus (mult) (mult)) (foo)) so that we can
11412 take advantage of the factoring cases below. */
11413 if (ANY_INTEGRAL_TYPE_P (type
)
11414 && TYPE_OVERFLOW_WRAPS (type
)
11415 && (((TREE_CODE (arg0
) == PLUS_EXPR
11416 || TREE_CODE (arg0
) == MINUS_EXPR
)
11417 && TREE_CODE (arg1
) == MULT_EXPR
)
11418 || ((TREE_CODE (arg1
) == PLUS_EXPR
11419 || TREE_CODE (arg1
) == MINUS_EXPR
)
11420 && TREE_CODE (arg0
) == MULT_EXPR
)))
11422 tree parg0
, parg1
, parg
, marg
;
11423 enum tree_code pcode
;
11425 if (TREE_CODE (arg1
) == MULT_EXPR
)
11426 parg
= arg0
, marg
= arg1
;
11428 parg
= arg1
, marg
= arg0
;
11429 pcode
= TREE_CODE (parg
);
11430 parg0
= TREE_OPERAND (parg
, 0);
11431 parg1
= TREE_OPERAND (parg
, 1);
11432 STRIP_NOPS (parg0
);
11433 STRIP_NOPS (parg1
);
11435 if (TREE_CODE (parg0
) == MULT_EXPR
11436 && TREE_CODE (parg1
) != MULT_EXPR
)
11437 return fold_build2_loc (loc
, pcode
, type
,
11438 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11439 fold_convert_loc (loc
, type
,
11441 fold_convert_loc (loc
, type
,
11443 fold_convert_loc (loc
, type
, parg1
));
11444 if (TREE_CODE (parg0
) != MULT_EXPR
11445 && TREE_CODE (parg1
) == MULT_EXPR
)
11447 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11448 fold_convert_loc (loc
, type
, parg0
),
11449 fold_build2_loc (loc
, pcode
, type
,
11450 fold_convert_loc (loc
, type
, marg
),
11451 fold_convert_loc (loc
, type
,
11457 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11458 to __complex__ ( x, y ). This is not the same for SNaNs or
11459 if signed zeros are involved. */
11460 if (!HONOR_SNANS (arg0
)
11461 && !HONOR_SIGNED_ZEROS (arg0
)
11462 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11464 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11465 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11466 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11467 bool arg0rz
= false, arg0iz
= false;
11468 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11469 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11471 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11472 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11473 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11475 tree rp
= arg1r
? arg1r
11476 : build1 (REALPART_EXPR
, rtype
, arg1
);
11477 tree ip
= arg0i
? arg0i
11478 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11479 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11481 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11483 tree rp
= arg0r
? arg0r
11484 : build1 (REALPART_EXPR
, rtype
, arg0
);
11485 tree ip
= arg1i
? arg1i
11486 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11487 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11492 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11493 We associate floats only if the user has specified
11494 -fassociative-math. */
11495 if (flag_associative_math
11496 && TREE_CODE (arg1
) == PLUS_EXPR
11497 && TREE_CODE (arg0
) != MULT_EXPR
)
11499 tree tree10
= TREE_OPERAND (arg1
, 0);
11500 tree tree11
= TREE_OPERAND (arg1
, 1);
11501 if (TREE_CODE (tree11
) == MULT_EXPR
11502 && TREE_CODE (tree10
) == MULT_EXPR
)
11505 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11506 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11509 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11510 We associate floats only if the user has specified
11511 -fassociative-math. */
11512 if (flag_associative_math
11513 && TREE_CODE (arg0
) == PLUS_EXPR
11514 && TREE_CODE (arg1
) != MULT_EXPR
)
11516 tree tree00
= TREE_OPERAND (arg0
, 0);
11517 tree tree01
= TREE_OPERAND (arg0
, 1);
11518 if (TREE_CODE (tree01
) == MULT_EXPR
11519 && TREE_CODE (tree00
) == MULT_EXPR
)
11522 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11523 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11529 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11530 is a rotate of A by C1 bits. */
11531 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11532 is a rotate of A by B bits.
11533 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11534 though in this case CODE must be | and not + or ^, otherwise
11535 it doesn't return A when B is 0. */
11537 enum tree_code code0
, code1
;
11539 code0
= TREE_CODE (arg0
);
11540 code1
= TREE_CODE (arg1
);
11541 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11542 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11543 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11544 TREE_OPERAND (arg1
, 0), 0)
11545 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11546 TYPE_UNSIGNED (rtype
))
11547 /* Only create rotates in complete modes. Other cases are not
11548 expanded properly. */
11549 && (element_precision (rtype
)
11550 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11552 tree tree01
, tree11
;
11553 tree orig_tree01
, orig_tree11
;
11554 enum tree_code code01
, code11
;
11556 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11557 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11558 STRIP_NOPS (tree01
);
11559 STRIP_NOPS (tree11
);
11560 code01
= TREE_CODE (tree01
);
11561 code11
= TREE_CODE (tree11
);
11562 if (code11
!= MINUS_EXPR
11563 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11565 std::swap (code0
, code1
);
11566 std::swap (code01
, code11
);
11567 std::swap (tree01
, tree11
);
11568 std::swap (orig_tree01
, orig_tree11
);
11570 if (code01
== INTEGER_CST
11571 && code11
== INTEGER_CST
11572 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11573 == element_precision (rtype
)))
11575 tem
= build2_loc (loc
, LROTATE_EXPR
,
11576 rtype
, TREE_OPERAND (arg0
, 0),
11577 code0
== LSHIFT_EXPR
11578 ? orig_tree01
: orig_tree11
);
11579 return fold_convert_loc (loc
, type
, tem
);
11581 else if (code11
== MINUS_EXPR
)
11583 tree tree110
, tree111
;
11584 tree110
= TREE_OPERAND (tree11
, 0);
11585 tree111
= TREE_OPERAND (tree11
, 1);
11586 STRIP_NOPS (tree110
);
11587 STRIP_NOPS (tree111
);
11588 if (TREE_CODE (tree110
) == INTEGER_CST
11589 && compare_tree_int (tree110
,
11590 element_precision (rtype
)) == 0
11591 && operand_equal_p (tree01
, tree111
, 0))
11593 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11594 ? LROTATE_EXPR
: RROTATE_EXPR
),
11595 rtype
, TREE_OPERAND (arg0
, 0),
11597 return fold_convert_loc (loc
, type
, tem
);
11600 else if (code
== BIT_IOR_EXPR
11601 && code11
== BIT_AND_EXPR
11602 && pow2p_hwi (element_precision (rtype
)))
11604 tree tree110
, tree111
;
11605 tree110
= TREE_OPERAND (tree11
, 0);
11606 tree111
= TREE_OPERAND (tree11
, 1);
11607 STRIP_NOPS (tree110
);
11608 STRIP_NOPS (tree111
);
11609 if (TREE_CODE (tree110
) == NEGATE_EXPR
11610 && TREE_CODE (tree111
) == INTEGER_CST
11611 && compare_tree_int (tree111
,
11612 element_precision (rtype
) - 1) == 0
11613 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11615 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11616 ? LROTATE_EXPR
: RROTATE_EXPR
),
11617 rtype
, TREE_OPERAND (arg0
, 0),
11619 return fold_convert_loc (loc
, type
, tem
);
11626 /* In most languages, can't associate operations on floats through
11627 parentheses. Rather than remember where the parentheses were, we
11628 don't associate floats at all, unless the user has specified
11629 -fassociative-math.
11630 And, we need to make sure type is not saturating. */
11632 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11633 && !TYPE_SATURATING (type
)
11634 && !TYPE_OVERFLOW_SANITIZED (type
))
11636 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11637 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11641 /* Split both trees into variables, constants, and literals. Then
11642 associate each group together, the constants with literals,
11643 then the result with variables. This increases the chances of
11644 literals being recombined later and of generating relocatable
11645 expressions for the sum of a constant and literal. */
11646 var0
= split_tree (arg0
, type
, code
,
11647 &minus_var0
, &con0
, &minus_con0
,
11648 &lit0
, &minus_lit0
, 0);
11649 var1
= split_tree (arg1
, type
, code
,
11650 &minus_var1
, &con1
, &minus_con1
,
11651 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11653 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11654 if (code
== MINUS_EXPR
)
11657 /* With undefined overflow prefer doing association in a type
11658 which wraps on overflow, if that is one of the operand types. */
11659 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11660 && !TYPE_OVERFLOW_WRAPS (type
))
11662 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11663 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11664 atype
= TREE_TYPE (arg0
);
11665 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11666 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11667 atype
= TREE_TYPE (arg1
);
11668 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11671 /* With undefined overflow we can only associate constants with one
11672 variable, and constants whose association doesn't overflow. */
11673 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11674 && !TYPE_OVERFLOW_WRAPS (atype
))
11676 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11678 /* ??? If split_tree would handle NEGATE_EXPR we could
11679 simply reject these cases and the allowed cases would
11680 be the var0/minus_var1 ones. */
11681 tree tmp0
= var0
? var0
: minus_var0
;
11682 tree tmp1
= var1
? var1
: minus_var1
;
11683 bool one_neg
= false;
11685 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11687 tmp0
= TREE_OPERAND (tmp0
, 0);
11688 one_neg
= !one_neg
;
11690 if (CONVERT_EXPR_P (tmp0
)
11691 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11692 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11693 <= TYPE_PRECISION (atype
)))
11694 tmp0
= TREE_OPERAND (tmp0
, 0);
11695 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11697 tmp1
= TREE_OPERAND (tmp1
, 0);
11698 one_neg
= !one_neg
;
11700 if (CONVERT_EXPR_P (tmp1
)
11701 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11702 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11703 <= TYPE_PRECISION (atype
)))
11704 tmp1
= TREE_OPERAND (tmp1
, 0);
11705 /* The only case we can still associate with two variables
11706 is if they cancel out. */
11708 || !operand_equal_p (tmp0
, tmp1
, 0))
11711 else if ((var0
&& minus_var1
11712 && ! operand_equal_p (var0
, minus_var1
, 0))
11713 || (minus_var0
&& var1
11714 && ! operand_equal_p (minus_var0
, var1
, 0)))
11718 /* Only do something if we found more than two objects. Otherwise,
11719 nothing has changed and we risk infinite recursion. */
11721 && ((var0
!= 0) + (var1
!= 0)
11722 + (minus_var0
!= 0) + (minus_var1
!= 0)
11723 + (con0
!= 0) + (con1
!= 0)
11724 + (minus_con0
!= 0) + (minus_con1
!= 0)
11725 + (lit0
!= 0) + (lit1
!= 0)
11726 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11728 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11729 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11731 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11732 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11734 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11735 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11738 if (minus_var0
&& var0
)
11740 var0
= associate_trees (loc
, var0
, minus_var0
,
11741 MINUS_EXPR
, atype
);
11744 if (minus_con0
&& con0
)
11746 con0
= associate_trees (loc
, con0
, minus_con0
,
11747 MINUS_EXPR
, atype
);
11751 /* Preserve the MINUS_EXPR if the negative part of the literal is
11752 greater than the positive part. Otherwise, the multiplicative
11753 folding code (i.e extract_muldiv) may be fooled in case
11754 unsigned constants are subtracted, like in the following
11755 example: ((X*2 + 4) - 8U)/2. */
11756 if (minus_lit0
&& lit0
)
11758 if (TREE_CODE (lit0
) == INTEGER_CST
11759 && TREE_CODE (minus_lit0
) == INTEGER_CST
11760 && tree_int_cst_lt (lit0
, minus_lit0
)
11761 /* But avoid ending up with only negated parts. */
11764 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11765 MINUS_EXPR
, atype
);
11770 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11771 MINUS_EXPR
, atype
);
11776 /* Don't introduce overflows through reassociation. */
11777 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11778 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11781 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11782 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11784 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11788 /* Eliminate minus_con0. */
11792 con0
= associate_trees (loc
, con0
, minus_con0
,
11793 MINUS_EXPR
, atype
);
11795 var0
= associate_trees (loc
, var0
, minus_con0
,
11796 MINUS_EXPR
, atype
);
11798 gcc_unreachable ();
11802 /* Eliminate minus_var0. */
11806 con0
= associate_trees (loc
, con0
, minus_var0
,
11807 MINUS_EXPR
, atype
);
11809 gcc_unreachable ();
11814 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11821 case POINTER_DIFF_EXPR
:
11823 /* Fold &a[i] - &a[j] to i-j. */
11824 if (TREE_CODE (arg0
) == ADDR_EXPR
11825 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11826 && TREE_CODE (arg1
) == ADDR_EXPR
11827 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11829 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11830 TREE_OPERAND (arg0
, 0),
11831 TREE_OPERAND (arg1
, 0),
11833 == POINTER_DIFF_EXPR
);
11838 /* Further transformations are not for pointers. */
11839 if (code
== POINTER_DIFF_EXPR
)
11842 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11843 if (TREE_CODE (arg0
) == NEGATE_EXPR
11844 && negate_expr_p (op1
)
11845 /* If arg0 is e.g. unsigned int and type is int, then this could
11846 introduce UB, because if A is INT_MIN at runtime, the original
11847 expression can be well defined while the latter is not.
11849 && !(ANY_INTEGRAL_TYPE_P (type
)
11850 && TYPE_OVERFLOW_UNDEFINED (type
)
11851 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11852 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11853 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11854 fold_convert_loc (loc
, type
,
11855 TREE_OPERAND (arg0
, 0)));
11857 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11858 __complex__ ( x, -y ). This is not the same for SNaNs or if
11859 signed zeros are involved. */
11860 if (!HONOR_SNANS (arg0
)
11861 && !HONOR_SIGNED_ZEROS (arg0
)
11862 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11864 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11865 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11866 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11867 bool arg0rz
= false, arg0iz
= false;
11868 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11869 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11871 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11872 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11873 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11875 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11877 : build1 (REALPART_EXPR
, rtype
, arg1
));
11878 tree ip
= arg0i
? arg0i
11879 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11880 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11882 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11884 tree rp
= arg0r
? arg0r
11885 : build1 (REALPART_EXPR
, rtype
, arg0
);
11886 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11888 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11889 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11894 /* A - B -> A + (-B) if B is easily negatable. */
11895 if (negate_expr_p (op1
)
11896 && ! TYPE_OVERFLOW_SANITIZED (type
)
11897 && ((FLOAT_TYPE_P (type
)
11898 /* Avoid this transformation if B is a positive REAL_CST. */
11899 && (TREE_CODE (op1
) != REAL_CST
11900 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11901 || INTEGRAL_TYPE_P (type
)))
11902 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11903 fold_convert_loc (loc
, type
, arg0
),
11904 negate_expr (op1
));
11906 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11907 one. Make sure the type is not saturating and has the signedness of
11908 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11909 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11910 if ((TREE_CODE (arg0
) == MULT_EXPR
11911 || TREE_CODE (arg1
) == MULT_EXPR
)
11912 && !TYPE_SATURATING (type
)
11913 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11914 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11915 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11917 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11925 if (! FLOAT_TYPE_P (type
))
11927 /* Transform x * -C into -x * C if x is easily negatable. */
11928 if (TREE_CODE (op1
) == INTEGER_CST
11929 && tree_int_cst_sgn (op1
) == -1
11930 && negate_expr_p (op0
)
11931 && negate_expr_p (op1
)
11932 && (tem
= negate_expr (op1
)) != op1
11933 && ! TREE_OVERFLOW (tem
))
11934 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11935 fold_convert_loc (loc
, type
,
11936 negate_expr (op0
)), tem
);
11938 strict_overflow_p
= false;
11939 if (TREE_CODE (arg1
) == INTEGER_CST
11940 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11941 &strict_overflow_p
)) != 0)
11943 if (strict_overflow_p
)
11944 fold_overflow_warning (("assuming signed overflow does not "
11945 "occur when simplifying "
11947 WARN_STRICT_OVERFLOW_MISC
);
11948 return fold_convert_loc (loc
, type
, tem
);
11951 /* Optimize z * conj(z) for integer complex numbers. */
11952 if (TREE_CODE (arg0
) == CONJ_EXPR
11953 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11954 return fold_mult_zconjz (loc
, type
, arg1
);
11955 if (TREE_CODE (arg1
) == CONJ_EXPR
11956 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11957 return fold_mult_zconjz (loc
, type
, arg0
);
11961 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11962 This is not the same for NaNs or if signed zeros are
11964 if (!HONOR_NANS (arg0
)
11965 && !HONOR_SIGNED_ZEROS (arg0
)
11966 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11967 && TREE_CODE (arg1
) == COMPLEX_CST
11968 && real_zerop (TREE_REALPART (arg1
)))
11970 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11971 if (real_onep (TREE_IMAGPART (arg1
)))
11973 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11974 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11976 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11977 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11979 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11980 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11981 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11985 /* Optimize z * conj(z) for floating point complex numbers.
11986 Guarded by flag_unsafe_math_optimizations as non-finite
11987 imaginary components don't produce scalar results. */
11988 if (flag_unsafe_math_optimizations
11989 && TREE_CODE (arg0
) == CONJ_EXPR
11990 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11991 return fold_mult_zconjz (loc
, type
, arg1
);
11992 if (flag_unsafe_math_optimizations
11993 && TREE_CODE (arg1
) == CONJ_EXPR
11994 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11995 return fold_mult_zconjz (loc
, type
, arg0
);
12000 /* Canonicalize (X & C1) | C2. */
12001 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12002 && TREE_CODE (arg1
) == INTEGER_CST
12003 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12005 int width
= TYPE_PRECISION (type
), w
;
12006 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
12007 wide_int c2
= wi::to_wide (arg1
);
12009 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
12010 if ((c1
& c2
) == c1
)
12011 return omit_one_operand_loc (loc
, type
, arg1
,
12012 TREE_OPERAND (arg0
, 0));
12014 wide_int msk
= wi::mask (width
, false,
12015 TYPE_PRECISION (TREE_TYPE (arg1
)));
12017 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
12018 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
12020 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12021 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
12024 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
12025 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
12026 mode which allows further optimizations. */
12029 wide_int c3
= wi::bit_and_not (c1
, c2
);
12030 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
12032 wide_int mask
= wi::mask (w
, false,
12033 TYPE_PRECISION (type
));
12034 if (((c1
| c2
) & mask
) == mask
12035 && wi::bit_and_not (c1
, mask
) == 0)
12044 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12045 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
12046 wide_int_to_tree (type
, c3
));
12047 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
12051 /* See if this can be simplified into a rotate first. If that
12052 is unsuccessful continue in the association code. */
12056 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
12057 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12058 && INTEGRAL_TYPE_P (type
)
12059 && integer_onep (TREE_OPERAND (arg0
, 1))
12060 && integer_onep (arg1
))
12061 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
12062 build_zero_cst (TREE_TYPE (arg0
)));
12064 /* See if this can be simplified into a rotate first. If that
12065 is unsuccessful continue in the association code. */
12069 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
12070 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12071 && INTEGRAL_TYPE_P (type
)
12072 && integer_onep (TREE_OPERAND (arg0
, 1))
12073 && integer_onep (arg1
))
12076 tem
= TREE_OPERAND (arg0
, 0);
12077 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
12078 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
12080 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
12081 build_zero_cst (TREE_TYPE (tem
)));
12083 /* Fold ~X & 1 as (X & 1) == 0. */
12084 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
12085 && INTEGRAL_TYPE_P (type
)
12086 && integer_onep (arg1
))
12089 tem
= TREE_OPERAND (arg0
, 0);
12090 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
12091 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
12093 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
12094 build_zero_cst (TREE_TYPE (tem
)));
12096 /* Fold !X & 1 as X == 0. */
12097 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12098 && integer_onep (arg1
))
12100 tem
= TREE_OPERAND (arg0
, 0);
12101 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
12102 build_zero_cst (TREE_TYPE (tem
)));
12105 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
12106 multiple of 1 << CST. */
12107 if (TREE_CODE (arg1
) == INTEGER_CST
)
12109 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12110 wide_int ncst1
= -cst1
;
12111 if ((cst1
& ncst1
) == ncst1
12112 && multiple_of_p (type
, arg0
,
12113 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
12114 return fold_convert_loc (loc
, type
, arg0
);
12117 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
12119 if (TREE_CODE (arg1
) == INTEGER_CST
12120 && TREE_CODE (arg0
) == MULT_EXPR
12121 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12123 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
12125 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
12128 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
12130 else if (masked
!= warg1
)
12132 /* Avoid the transform if arg1 is a mask of some
12133 mode which allows further optimizations. */
12134 int pop
= wi::popcount (warg1
);
12135 if (!(pop
>= BITS_PER_UNIT
12137 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
12138 return fold_build2_loc (loc
, code
, type
, op0
,
12139 wide_int_to_tree (type
, masked
));
12143 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
12144 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
12145 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
12147 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
12149 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
12152 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12158 /* Don't touch a floating-point divide by zero unless the mode
12159 of the constant can represent infinity. */
12160 if (TREE_CODE (arg1
) == REAL_CST
12161 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
12162 && real_zerop (arg1
))
12165 /* (-A) / (-B) -> A / B */
12166 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
12167 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12168 TREE_OPERAND (arg0
, 0),
12169 negate_expr (arg1
));
12170 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
12171 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12172 negate_expr (arg0
),
12173 TREE_OPERAND (arg1
, 0));
12176 case TRUNC_DIV_EXPR
:
12179 case FLOOR_DIV_EXPR
:
12180 /* Simplify A / (B << N) where A and B are positive and B is
12181 a power of 2, to A >> (N + log2(B)). */
12182 strict_overflow_p
= false;
12183 if (TREE_CODE (arg1
) == LSHIFT_EXPR
12184 && (TYPE_UNSIGNED (type
)
12185 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
12187 tree sval
= TREE_OPERAND (arg1
, 0);
12188 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
12190 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
12191 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
12192 wi::exact_log2 (wi::to_wide (sval
)));
12194 if (strict_overflow_p
)
12195 fold_overflow_warning (("assuming signed overflow does not "
12196 "occur when simplifying A / (B << N)"),
12197 WARN_STRICT_OVERFLOW_MISC
);
12199 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
12201 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
12202 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
12208 case ROUND_DIV_EXPR
:
12209 case CEIL_DIV_EXPR
:
12210 case EXACT_DIV_EXPR
:
12211 if (integer_zerop (arg1
))
12214 /* Convert -A / -B to A / B when the type is signed and overflow is
12216 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12217 && TREE_CODE (op0
) == NEGATE_EXPR
12218 && negate_expr_p (op1
))
12220 if (ANY_INTEGRAL_TYPE_P (type
))
12221 fold_overflow_warning (("assuming signed overflow does not occur "
12222 "when distributing negation across "
12224 WARN_STRICT_OVERFLOW_MISC
);
12225 return fold_build2_loc (loc
, code
, type
,
12226 fold_convert_loc (loc
, type
,
12227 TREE_OPERAND (arg0
, 0)),
12228 negate_expr (op1
));
12230 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12231 && TREE_CODE (arg1
) == NEGATE_EXPR
12232 && negate_expr_p (op0
))
12234 if (ANY_INTEGRAL_TYPE_P (type
))
12235 fold_overflow_warning (("assuming signed overflow does not occur "
12236 "when distributing negation across "
12238 WARN_STRICT_OVERFLOW_MISC
);
12239 return fold_build2_loc (loc
, code
, type
,
12241 fold_convert_loc (loc
, type
,
12242 TREE_OPERAND (arg1
, 0)));
12245 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12246 operation, EXACT_DIV_EXPR.
12248 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12249 At one time others generated faster code, it's not clear if they do
12250 after the last round to changes to the DIV code in expmed.cc. */
12251 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
12252 && multiple_of_p (type
, arg0
, arg1
))
12253 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
12254 fold_convert (type
, arg0
),
12255 fold_convert (type
, arg1
));
12257 strict_overflow_p
= false;
12258 if (TREE_CODE (arg1
) == INTEGER_CST
12259 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12260 &strict_overflow_p
)) != 0)
12262 if (strict_overflow_p
)
12263 fold_overflow_warning (("assuming signed overflow does not occur "
12264 "when simplifying division"),
12265 WARN_STRICT_OVERFLOW_MISC
);
12266 return fold_convert_loc (loc
, type
, tem
);
12271 case CEIL_MOD_EXPR
:
12272 case FLOOR_MOD_EXPR
:
12273 case ROUND_MOD_EXPR
:
12274 case TRUNC_MOD_EXPR
:
12275 strict_overflow_p
= false;
12276 if (TREE_CODE (arg1
) == INTEGER_CST
12277 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12278 &strict_overflow_p
)) != 0)
12280 if (strict_overflow_p
)
12281 fold_overflow_warning (("assuming signed overflow does not occur "
12282 "when simplifying modulus"),
12283 WARN_STRICT_OVERFLOW_MISC
);
12284 return fold_convert_loc (loc
, type
, tem
);
12293 /* Since negative shift count is not well-defined,
12294 don't try to compute it in the compiler. */
12295 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
12298 prec
= element_precision (type
);
12300 /* If we have a rotate of a bit operation with the rotate count and
12301 the second operand of the bit operation both constant,
12302 permute the two operations. */
12303 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12304 && (TREE_CODE (arg0
) == BIT_AND_EXPR
12305 || TREE_CODE (arg0
) == BIT_IOR_EXPR
12306 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
12307 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12309 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12310 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12311 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
12312 fold_build2_loc (loc
, code
, type
,
12314 fold_build2_loc (loc
, code
, type
,
12318 /* Two consecutive rotates adding up to the some integer
12319 multiple of the precision of the type can be ignored. */
12320 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12321 && TREE_CODE (arg0
) == RROTATE_EXPR
12322 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12323 && wi::umod_trunc (wi::to_wide (arg1
)
12324 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
12326 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12334 case TRUTH_ANDIF_EXPR
:
12335 /* Note that the operands of this must be ints
12336 and their values must be 0 or 1.
12337 ("true" is a fixed value perhaps depending on the language.) */
12338 /* If first arg is constant zero, return it. */
12339 if (integer_zerop (arg0
))
12340 return fold_convert_loc (loc
, type
, arg0
);
12342 case TRUTH_AND_EXPR
:
12343 /* If either arg is constant true, drop it. */
12344 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12345 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12346 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
12347 /* Preserve sequence points. */
12348 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12349 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12350 /* If second arg is constant zero, result is zero, but first arg
12351 must be evaluated. */
12352 if (integer_zerop (arg1
))
12353 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12354 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12355 case will be handled here. */
12356 if (integer_zerop (arg0
))
12357 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12359 /* !X && X is always false. */
12360 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12361 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12362 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12363 /* X && !X is always false. */
12364 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12365 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12366 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12368 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12369 means A >= Y && A != MAX, but in this case we know that
12372 if (!TREE_SIDE_EFFECTS (arg0
)
12373 && !TREE_SIDE_EFFECTS (arg1
))
12375 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12376 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12377 return fold_convert (type
,
12378 fold_build2_loc (loc
, code
, TREE_TYPE (arg1
),
12381 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12382 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12383 return fold_convert (type
,
12384 fold_build2_loc (loc
, code
, TREE_TYPE (arg0
),
12388 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12394 case TRUTH_ORIF_EXPR
:
12395 /* Note that the operands of this must be ints
12396 and their values must be 0 or true.
12397 ("true" is a fixed value perhaps depending on the language.) */
12398 /* If first arg is constant true, return it. */
12399 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12400 return fold_convert_loc (loc
, type
, arg0
);
12402 case TRUTH_OR_EXPR
:
12403 /* If either arg is constant zero, drop it. */
12404 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12405 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12406 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12407 /* Preserve sequence points. */
12408 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12409 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12410 /* If second arg is constant true, result is true, but we must
12411 evaluate first arg. */
12412 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12413 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12414 /* Likewise for first arg, but note this only occurs here for
12416 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12417 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12419 /* !X || X is always true. */
12420 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12421 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12422 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12423 /* X || !X is always true. */
12424 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12425 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12426 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12428 /* (X && !Y) || (!X && Y) is X ^ Y */
12429 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12430 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12432 tree a0
, a1
, l0
, l1
, n0
, n1
;
12434 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12435 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12437 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12438 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12440 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12441 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12443 if ((operand_equal_p (n0
, a0
, 0)
12444 && operand_equal_p (n1
, a1
, 0))
12445 || (operand_equal_p (n0
, a1
, 0)
12446 && operand_equal_p (n1
, a0
, 0)))
12447 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12450 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12456 case TRUTH_XOR_EXPR
:
12457 /* If the second arg is constant zero, drop it. */
12458 if (integer_zerop (arg1
))
12459 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12460 /* If the second arg is constant true, this is a logical inversion. */
12461 if (integer_onep (arg1
))
12463 tem
= invert_truthvalue_loc (loc
, arg0
);
12464 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12466 /* Identical arguments cancel to zero. */
12467 if (operand_equal_p (arg0
, arg1
, 0))
12468 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12470 /* !X ^ X is always true. */
12471 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12472 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12473 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12475 /* X ^ !X is always true. */
12476 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12477 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12478 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12487 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12488 if (tem
!= NULL_TREE
)
12491 /* bool_var != 1 becomes !bool_var. */
12492 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12493 && code
== NE_EXPR
)
12494 return fold_convert_loc (loc
, type
,
12495 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12496 TREE_TYPE (arg0
), arg0
));
12498 /* bool_var == 0 becomes !bool_var. */
12499 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12500 && code
== EQ_EXPR
)
12501 return fold_convert_loc (loc
, type
,
12502 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12503 TREE_TYPE (arg0
), arg0
));
12505 /* !exp != 0 becomes !exp */
12506 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12507 && code
== NE_EXPR
)
12508 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12510 /* If this is an EQ or NE comparison with zero and ARG0 is
12511 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12512 two operations, but the latter can be done in one less insn
12513 on machines that have only two-operand insns or on which a
12514 constant cannot be the first operand. */
12515 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12516 && integer_zerop (arg1
))
12518 tree arg00
= TREE_OPERAND (arg0
, 0);
12519 tree arg01
= TREE_OPERAND (arg0
, 1);
12520 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12521 && integer_onep (TREE_OPERAND (arg00
, 0)))
12523 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12524 arg01
, TREE_OPERAND (arg00
, 1));
12525 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12526 build_one_cst (TREE_TYPE (arg0
)));
12527 return fold_build2_loc (loc
, code
, type
,
12528 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12531 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12532 && integer_onep (TREE_OPERAND (arg01
, 0)))
12534 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12535 arg00
, TREE_OPERAND (arg01
, 1));
12536 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12537 build_one_cst (TREE_TYPE (arg0
)));
12538 return fold_build2_loc (loc
, code
, type
,
12539 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12544 /* If this is a comparison of a field, we may be able to simplify it. */
12545 if ((TREE_CODE (arg0
) == COMPONENT_REF
12546 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12547 /* Handle the constant case even without -O
12548 to make sure the warnings are given. */
12549 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12551 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12556 /* Optimize comparisons of strlen vs zero to a compare of the
12557 first character of the string vs zero. To wit,
12558 strlen(ptr) == 0 => *ptr == 0
12559 strlen(ptr) != 0 => *ptr != 0
12560 Other cases should reduce to one of these two (or a constant)
12561 due to the return value of strlen being unsigned. */
12562 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12564 tree fndecl
= get_callee_fndecl (arg0
);
12567 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12568 && call_expr_nargs (arg0
) == 1
12569 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12573 = build_pointer_type (build_qualified_type (char_type_node
,
12575 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12576 CALL_EXPR_ARG (arg0
, 0));
12577 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12578 return fold_build2_loc (loc
, code
, type
, iref
,
12579 build_int_cst (TREE_TYPE (iref
), 0));
12583 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12584 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12585 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12586 && integer_zerop (arg1
)
12587 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12589 tree arg00
= TREE_OPERAND (arg0
, 0);
12590 tree arg01
= TREE_OPERAND (arg0
, 1);
12591 tree itype
= TREE_TYPE (arg00
);
12592 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12594 if (TYPE_UNSIGNED (itype
))
12596 itype
= signed_type_for (itype
);
12597 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12599 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12600 type
, arg00
, build_zero_cst (itype
));
12604 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12605 (X & C) == 0 when C is a single bit. */
12606 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12607 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12608 && integer_zerop (arg1
)
12609 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12611 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12612 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12613 TREE_OPERAND (arg0
, 1));
12614 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12616 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12620 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12621 constant C is a power of two, i.e. a single bit. */
12622 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12623 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12624 && integer_zerop (arg1
)
12625 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12626 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12627 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12629 tree arg00
= TREE_OPERAND (arg0
, 0);
12630 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12631 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12634 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12635 when is C is a power of two, i.e. a single bit. */
12636 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12637 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12638 && integer_zerop (arg1
)
12639 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12640 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12641 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12643 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12644 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12645 arg000
, TREE_OPERAND (arg0
, 1));
12646 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12647 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12650 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12651 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12653 tree arg00
= TREE_OPERAND (arg0
, 0);
12654 tree arg01
= TREE_OPERAND (arg0
, 1);
12655 tree arg10
= TREE_OPERAND (arg1
, 0);
12656 tree arg11
= TREE_OPERAND (arg1
, 1);
12657 tree itype
= TREE_TYPE (arg0
);
12659 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12660 operand_equal_p guarantees no side-effects so we don't need
12661 to use omit_one_operand on Z. */
12662 if (operand_equal_p (arg01
, arg11
, 0))
12663 return fold_build2_loc (loc
, code
, type
, arg00
,
12664 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12666 if (operand_equal_p (arg01
, arg10
, 0))
12667 return fold_build2_loc (loc
, code
, type
, arg00
,
12668 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12670 if (operand_equal_p (arg00
, arg11
, 0))
12671 return fold_build2_loc (loc
, code
, type
, arg01
,
12672 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12674 if (operand_equal_p (arg00
, arg10
, 0))
12675 return fold_build2_loc (loc
, code
, type
, arg01
,
12676 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12679 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12680 if (TREE_CODE (arg01
) == INTEGER_CST
12681 && TREE_CODE (arg11
) == INTEGER_CST
)
12683 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12684 fold_convert_loc (loc
, itype
, arg11
));
12685 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12686 return fold_build2_loc (loc
, code
, type
, tem
,
12687 fold_convert_loc (loc
, itype
, arg10
));
12691 /* Attempt to simplify equality/inequality comparisons of complex
12692 values. Only lower the comparison if the result is known or
12693 can be simplified to a single scalar comparison. */
12694 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12695 || TREE_CODE (arg0
) == COMPLEX_CST
)
12696 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12697 || TREE_CODE (arg1
) == COMPLEX_CST
))
12699 tree real0
, imag0
, real1
, imag1
;
12702 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12704 real0
= TREE_OPERAND (arg0
, 0);
12705 imag0
= TREE_OPERAND (arg0
, 1);
12709 real0
= TREE_REALPART (arg0
);
12710 imag0
= TREE_IMAGPART (arg0
);
12713 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12715 real1
= TREE_OPERAND (arg1
, 0);
12716 imag1
= TREE_OPERAND (arg1
, 1);
12720 real1
= TREE_REALPART (arg1
);
12721 imag1
= TREE_IMAGPART (arg1
);
12724 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12725 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12727 if (integer_zerop (rcond
))
12729 if (code
== EQ_EXPR
)
12730 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12732 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12736 if (code
== NE_EXPR
)
12737 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12739 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12743 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12744 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12746 if (integer_zerop (icond
))
12748 if (code
== EQ_EXPR
)
12749 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12751 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12755 if (code
== NE_EXPR
)
12756 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12758 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12769 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12770 if (tem
!= NULL_TREE
)
12773 /* Transform comparisons of the form X +- C CMP X. */
12774 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12775 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12776 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12777 && !HONOR_SNANS (arg0
))
12779 tree arg01
= TREE_OPERAND (arg0
, 1);
12780 enum tree_code code0
= TREE_CODE (arg0
);
12781 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12783 /* (X - c) > X becomes false. */
12784 if (code
== GT_EXPR
12785 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12786 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12787 return constant_boolean_node (0, type
);
12789 /* Likewise (X + c) < X becomes false. */
12790 if (code
== LT_EXPR
12791 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12792 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12793 return constant_boolean_node (0, type
);
12795 /* Convert (X - c) <= X to true. */
12796 if (!HONOR_NANS (arg1
)
12798 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12799 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12800 return constant_boolean_node (1, type
);
12802 /* Convert (X + c) >= X to true. */
12803 if (!HONOR_NANS (arg1
)
12805 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12806 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12807 return constant_boolean_node (1, type
);
12810 /* If we are comparing an ABS_EXPR with a constant, we can
12811 convert all the cases into explicit comparisons, but they may
12812 well not be faster than doing the ABS and one comparison.
12813 But ABS (X) <= C is a range comparison, which becomes a subtraction
12814 and a comparison, and is probably faster. */
12815 if (code
== LE_EXPR
12816 && TREE_CODE (arg1
) == INTEGER_CST
12817 && TREE_CODE (arg0
) == ABS_EXPR
12818 && ! TREE_SIDE_EFFECTS (arg0
)
12819 && (tem
= negate_expr (arg1
)) != 0
12820 && TREE_CODE (tem
) == INTEGER_CST
12821 && !TREE_OVERFLOW (tem
))
12822 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12823 build2 (GE_EXPR
, type
,
12824 TREE_OPERAND (arg0
, 0), tem
),
12825 build2 (LE_EXPR
, type
,
12826 TREE_OPERAND (arg0
, 0), arg1
));
12828 /* Convert ABS_EXPR<x> >= 0 to true. */
12829 strict_overflow_p
= false;
12830 if (code
== GE_EXPR
12831 && (integer_zerop (arg1
)
12832 || (! HONOR_NANS (arg0
)
12833 && 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 (true, type
),
12846 /* Convert ABS_EXPR<x> < 0 to false. */
12847 strict_overflow_p
= false;
12848 if (code
== LT_EXPR
12849 && (integer_zerop (arg1
) || real_zerop (arg1
))
12850 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12852 if (strict_overflow_p
)
12853 fold_overflow_warning (("assuming signed overflow does not occur "
12854 "when simplifying comparison of "
12855 "absolute value and zero"),
12856 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12857 return omit_one_operand_loc (loc
, type
,
12858 constant_boolean_node (false, type
),
12862 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12863 and similarly for >= into !=. */
12864 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12865 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12866 && TREE_CODE (arg1
) == LSHIFT_EXPR
12867 && integer_onep (TREE_OPERAND (arg1
, 0)))
12868 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12869 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12870 TREE_OPERAND (arg1
, 1)),
12871 build_zero_cst (TREE_TYPE (arg0
)));
12873 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12874 otherwise Y might be >= # of bits in X's type and thus e.g.
12875 (unsigned char) (1 << Y) for Y 15 might be 0.
12876 If the cast is widening, then 1 << Y should have unsigned type,
12877 otherwise if Y is number of bits in the signed shift type minus 1,
12878 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12879 31 might be 0xffffffff80000000. */
12880 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12881 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12882 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0
)))
12883 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12884 && CONVERT_EXPR_P (arg1
)
12885 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12886 && (element_precision (TREE_TYPE (arg1
))
12887 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12888 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12889 || (element_precision (TREE_TYPE (arg1
))
12890 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12891 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12893 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12894 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12895 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12896 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12897 build_zero_cst (TREE_TYPE (arg0
)));
12902 case UNORDERED_EXPR
:
12910 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12912 tree targ0
= strip_float_extensions (arg0
);
12913 tree targ1
= strip_float_extensions (arg1
);
12914 tree newtype
= TREE_TYPE (targ0
);
12916 if (element_precision (TREE_TYPE (targ1
)) > element_precision (newtype
))
12917 newtype
= TREE_TYPE (targ1
);
12919 if (element_precision (newtype
) < element_precision (TREE_TYPE (arg0
)))
12920 return fold_build2_loc (loc
, code
, type
,
12921 fold_convert_loc (loc
, newtype
, targ0
),
12922 fold_convert_loc (loc
, newtype
, targ1
));
12927 case COMPOUND_EXPR
:
12928 /* When pedantic, a compound expression can be neither an lvalue
12929 nor an integer constant expression. */
12930 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12932 /* Don't let (0, 0) be null pointer constant. */
12933 tem
= integer_zerop (arg1
) ? build1_loc (loc
, NOP_EXPR
, type
, arg1
)
12934 : fold_convert_loc (loc
, type
, arg1
);
12939 } /* switch (code) */
12942 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12943 ((A & N) + B) & M -> (A + B) & M
12944 Similarly if (N & M) == 0,
12945 ((A | N) + B) & M -> (A + B) & M
12946 and for - instead of + (or unary - instead of +)
12947 and/or ^ instead of |.
12948 If B is constant and (B & M) == 0, fold into A & M.
12950 This function is a helper for match.pd patterns. Return non-NULL
12951 type in which the simplified operation should be performed only
12952 if any optimization is possible.
12954 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12955 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12956 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12959 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12960 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12961 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12964 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12965 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12966 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12968 || (cst1
& (cst1
+ 1)) != 0
12969 || !INTEGRAL_TYPE_P (type
)
12970 || (!TYPE_OVERFLOW_WRAPS (type
)
12971 && TREE_CODE (type
) != INTEGER_TYPE
)
12972 || (wi::max_value (type
) & cst1
) != cst1
)
12975 enum tree_code codes
[2] = { code00
, code01
};
12976 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12980 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12981 arg1 (M) is == (1LL << cst) - 1.
12982 Store C into PMOP[0] and D into PMOP[1]. */
12985 which
= code
!= NEGATE_EXPR
;
12987 for (; which
>= 0; which
--)
12988 switch (codes
[which
])
12993 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12994 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12995 if (codes
[which
] == BIT_AND_EXPR
)
13000 else if (cst0
!= 0)
13002 /* If C or D is of the form (A & N) where
13003 (N & M) == M, or of the form (A | N) or
13004 (A ^ N) where (N & M) == 0, replace it with A. */
13005 pmop
[which
] = arg0xx
[2 * which
];
13008 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
13010 /* If C or D is a N where (N & M) == 0, it can be
13011 omitted (replaced with 0). */
13012 if ((code
== PLUS_EXPR
13013 || (code
== MINUS_EXPR
&& which
== 0))
13014 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
13015 pmop
[which
] = build_int_cst (type
, 0);
13016 /* Similarly, with C - N where (-N & M) == 0. */
13017 if (code
== MINUS_EXPR
13019 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
13020 pmop
[which
] = build_int_cst (type
, 0);
13023 gcc_unreachable ();
13026 /* Only build anything new if we optimized one or both arguments above. */
13027 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
13030 if (TYPE_OVERFLOW_WRAPS (type
))
13033 return unsigned_type_for (type
);
13036 /* Used by contains_label_[p1]. */
13038 struct contains_label_data
13040 hash_set
<tree
> *pset
;
13041 bool inside_switch_p
;
13044 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13045 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
13046 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
13049 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
13051 contains_label_data
*d
= (contains_label_data
*) data
;
13052 switch (TREE_CODE (*tp
))
13057 case CASE_LABEL_EXPR
:
13058 if (!d
->inside_switch_p
)
13063 if (!d
->inside_switch_p
)
13065 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
13067 d
->inside_switch_p
= true;
13068 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
13070 d
->inside_switch_p
= false;
13071 *walk_subtrees
= 0;
13076 *walk_subtrees
= 0;
13084 /* Return whether the sub-tree ST contains a label which is accessible from
13085 outside the sub-tree. */
13088 contains_label_p (tree st
)
13090 hash_set
<tree
> pset
;
13091 contains_label_data data
= { &pset
, false };
13092 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
13095 /* Fold a ternary expression of code CODE and type TYPE with operands
13096 OP0, OP1, and OP2. Return the folded expression if folding is
13097 successful. Otherwise, return NULL_TREE. */
13100 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
13101 tree op0
, tree op1
, tree op2
)
13104 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
13105 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13107 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
13108 && TREE_CODE_LENGTH (code
) == 3);
13110 /* If this is a commutative operation, and OP0 is a constant, move it
13111 to OP1 to reduce the number of tests below. */
13112 if (commutative_ternary_tree_code (code
)
13113 && tree_swap_operands_p (op0
, op1
))
13114 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
13116 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
13120 /* Strip any conversions that don't change the mode. This is safe
13121 for every expression, except for a comparison expression because
13122 its signedness is derived from its operands. So, in the latter
13123 case, only strip conversions that don't change the signedness.
13125 Note that this is done as an internal manipulation within the
13126 constant folder, in order to find the simplest representation of
13127 the arguments so that their form can be studied. In any cases,
13128 the appropriate type conversions should be put back in the tree
13129 that will get out of the constant folder. */
13150 case COMPONENT_REF
:
13151 if (TREE_CODE (arg0
) == CONSTRUCTOR
13152 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
13154 unsigned HOST_WIDE_INT idx
;
13156 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
13163 case VEC_COND_EXPR
:
13164 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13165 so all simple results must be passed through pedantic_non_lvalue. */
13166 if (TREE_CODE (arg0
) == INTEGER_CST
)
13168 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
13169 tem
= integer_zerop (arg0
) ? op2
: op1
;
13170 /* Only optimize constant conditions when the selected branch
13171 has the same type as the COND_EXPR. This avoids optimizing
13172 away "c ? x : throw", where the throw has a void type.
13173 Avoid throwing away that operand which contains label. */
13174 if ((!TREE_SIDE_EFFECTS (unused_op
)
13175 || !contains_label_p (unused_op
))
13176 && (! VOID_TYPE_P (TREE_TYPE (tem
))
13177 || VOID_TYPE_P (type
)))
13178 return protected_set_expr_location_unshare (tem
, loc
);
13181 else if (TREE_CODE (arg0
) == VECTOR_CST
)
13183 unsigned HOST_WIDE_INT nelts
;
13184 if ((TREE_CODE (arg1
) == VECTOR_CST
13185 || TREE_CODE (arg1
) == CONSTRUCTOR
)
13186 && (TREE_CODE (arg2
) == VECTOR_CST
13187 || TREE_CODE (arg2
) == CONSTRUCTOR
)
13188 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
13190 vec_perm_builder
sel (nelts
, nelts
, 1);
13191 for (unsigned int i
= 0; i
< nelts
; i
++)
13193 tree val
= VECTOR_CST_ELT (arg0
, i
);
13194 if (integer_all_onesp (val
))
13195 sel
.quick_push (i
);
13196 else if (integer_zerop (val
))
13197 sel
.quick_push (nelts
+ i
);
13198 else /* Currently unreachable. */
13201 vec_perm_indices
indices (sel
, 2, nelts
);
13202 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
13203 if (t
!= NULL_TREE
)
13208 /* If we have A op B ? A : C, we may be able to convert this to a
13209 simpler expression, depending on the operation and the values
13210 of B and C. Signed zeros prevent all of these transformations,
13211 for reasons given above each one.
13213 Also try swapping the arguments and inverting the conditional. */
13214 if (COMPARISON_CLASS_P (arg0
)
13215 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
13216 && !HONOR_SIGNED_ZEROS (op1
))
13218 tem
= fold_cond_expr_with_comparison (loc
, type
, TREE_CODE (arg0
),
13219 TREE_OPERAND (arg0
, 0),
13220 TREE_OPERAND (arg0
, 1),
13226 if (COMPARISON_CLASS_P (arg0
)
13227 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
13228 && !HONOR_SIGNED_ZEROS (op2
))
13230 enum tree_code comp_code
= TREE_CODE (arg0
);
13231 tree arg00
= TREE_OPERAND (arg0
, 0);
13232 tree arg01
= TREE_OPERAND (arg0
, 1);
13233 comp_code
= invert_tree_comparison (comp_code
, HONOR_NANS (arg00
));
13234 if (comp_code
!= ERROR_MARK
)
13235 tem
= fold_cond_expr_with_comparison (loc
, type
, comp_code
,
13243 /* If the second operand is simpler than the third, swap them
13244 since that produces better jump optimization results. */
13245 if (truth_value_p (TREE_CODE (arg0
))
13246 && tree_swap_operands_p (op1
, op2
))
13248 location_t loc0
= expr_location_or (arg0
, loc
);
13249 /* See if this can be inverted. If it can't, possibly because
13250 it was a floating-point inequality comparison, don't do
13252 tem
= fold_invert_truthvalue (loc0
, arg0
);
13254 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
13257 /* Convert A ? 1 : 0 to simply A. */
13258 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
13259 : (integer_onep (op1
)
13260 && !VECTOR_TYPE_P (type
)))
13261 && integer_zerop (op2
)
13262 /* If we try to convert OP0 to our type, the
13263 call to fold will try to move the conversion inside
13264 a COND, which will recurse. In that case, the COND_EXPR
13265 is probably the best choice, so leave it alone. */
13266 && type
== TREE_TYPE (arg0
))
13267 return protected_set_expr_location_unshare (arg0
, loc
);
13269 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13270 over COND_EXPR in cases such as floating point comparisons. */
13271 if (integer_zerop (op1
)
13272 && code
== COND_EXPR
13273 && integer_onep (op2
)
13274 && !VECTOR_TYPE_P (type
)
13275 && truth_value_p (TREE_CODE (arg0
)))
13276 return fold_convert_loc (loc
, type
,
13277 invert_truthvalue_loc (loc
, arg0
));
13279 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13280 if (TREE_CODE (arg0
) == LT_EXPR
13281 && integer_zerop (TREE_OPERAND (arg0
, 1))
13282 && integer_zerop (op2
)
13283 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
13285 /* sign_bit_p looks through both zero and sign extensions,
13286 but for this optimization only sign extensions are
13288 tree tem2
= TREE_OPERAND (arg0
, 0);
13289 while (tem
!= tem2
)
13291 if (TREE_CODE (tem2
) != NOP_EXPR
13292 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
13297 tem2
= TREE_OPERAND (tem2
, 0);
13299 /* sign_bit_p only checks ARG1 bits within A's precision.
13300 If <sign bit of A> has wider type than A, bits outside
13301 of A's precision in <sign bit of A> need to be checked.
13302 If they are all 0, this optimization needs to be done
13303 in unsigned A's type, if they are all 1 in signed A's type,
13304 otherwise this can't be done. */
13306 && TYPE_PRECISION (TREE_TYPE (tem
))
13307 < TYPE_PRECISION (TREE_TYPE (arg1
))
13308 && TYPE_PRECISION (TREE_TYPE (tem
))
13309 < TYPE_PRECISION (type
))
13311 int inner_width
, outer_width
;
13314 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
13315 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
13316 if (outer_width
> TYPE_PRECISION (type
))
13317 outer_width
= TYPE_PRECISION (type
);
13319 wide_int mask
= wi::shifted_mask
13320 (inner_width
, outer_width
- inner_width
, false,
13321 TYPE_PRECISION (TREE_TYPE (arg1
)));
13323 wide_int common
= mask
& wi::to_wide (arg1
);
13324 if (common
== mask
)
13326 tem_type
= signed_type_for (TREE_TYPE (tem
));
13327 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13329 else if (common
== 0)
13331 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
13332 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13340 fold_convert_loc (loc
, type
,
13341 fold_build2_loc (loc
, BIT_AND_EXPR
,
13342 TREE_TYPE (tem
), tem
,
13343 fold_convert_loc (loc
,
13348 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13349 already handled above. */
13350 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13351 && integer_onep (TREE_OPERAND (arg0
, 1))
13352 && integer_zerop (op2
)
13353 && integer_pow2p (arg1
))
13355 tree tem
= TREE_OPERAND (arg0
, 0);
13357 if (TREE_CODE (tem
) == RSHIFT_EXPR
13358 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
13359 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
13360 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
13361 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
13362 fold_convert_loc (loc
, type
,
13363 TREE_OPERAND (tem
, 0)),
13367 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13368 is probably obsolete because the first operand should be a
13369 truth value (that's why we have the two cases above), but let's
13370 leave it in until we can confirm this for all front-ends. */
13371 if (integer_zerop (op2
)
13372 && TREE_CODE (arg0
) == NE_EXPR
13373 && integer_zerop (TREE_OPERAND (arg0
, 1))
13374 && integer_pow2p (arg1
)
13375 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13376 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13377 arg1
, OEP_ONLY_CONST
)
13378 /* operand_equal_p compares just value, not precision, so e.g.
13379 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13380 second operand 32-bit -128, which is not a power of two (or vice
13382 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
13383 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
13385 /* Disable the transformations below for vectors, since
13386 fold_binary_op_with_conditional_arg may undo them immediately,
13387 yielding an infinite loop. */
13388 if (code
== VEC_COND_EXPR
)
13391 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13392 if (integer_zerop (op2
)
13393 && truth_value_p (TREE_CODE (arg0
))
13394 && truth_value_p (TREE_CODE (arg1
))
13395 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13396 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13397 : TRUTH_ANDIF_EXPR
,
13398 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13400 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13401 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13402 && truth_value_p (TREE_CODE (arg0
))
13403 && truth_value_p (TREE_CODE (arg1
))
13404 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13406 location_t loc0
= expr_location_or (arg0
, loc
);
13407 /* Only perform transformation if ARG0 is easily inverted. */
13408 tem
= fold_invert_truthvalue (loc0
, arg0
);
13410 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13413 type
, fold_convert_loc (loc
, type
, tem
),
13417 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13418 if (integer_zerop (arg1
)
13419 && truth_value_p (TREE_CODE (arg0
))
13420 && truth_value_p (TREE_CODE (op2
))
13421 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13423 location_t loc0
= expr_location_or (arg0
, loc
);
13424 /* Only perform transformation if ARG0 is easily inverted. */
13425 tem
= fold_invert_truthvalue (loc0
, arg0
);
13427 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13428 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13429 type
, fold_convert_loc (loc
, type
, tem
),
13433 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13434 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13435 && truth_value_p (TREE_CODE (arg0
))
13436 && truth_value_p (TREE_CODE (op2
))
13437 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13438 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13439 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13440 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13445 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13446 of fold_ternary on them. */
13447 gcc_unreachable ();
13449 case BIT_FIELD_REF
:
13450 if (TREE_CODE (arg0
) == VECTOR_CST
13451 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13452 || (VECTOR_TYPE_P (type
)
13453 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13454 && tree_fits_uhwi_p (op1
)
13455 && tree_fits_uhwi_p (op2
))
13457 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13458 unsigned HOST_WIDE_INT width
13459 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13460 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13461 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13462 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13465 && (idx
% width
) == 0
13466 && (n
% width
) == 0
13467 && known_le ((idx
+ n
) / width
,
13468 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13473 if (TREE_CODE (arg0
) == VECTOR_CST
)
13477 tem
= VECTOR_CST_ELT (arg0
, idx
);
13478 if (VECTOR_TYPE_P (type
))
13479 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13483 tree_vector_builder
vals (type
, n
, 1);
13484 for (unsigned i
= 0; i
< n
; ++i
)
13485 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13486 return vals
.build ();
13491 /* On constants we can use native encode/interpret to constant
13492 fold (nearly) all BIT_FIELD_REFs. */
13493 if (CONSTANT_CLASS_P (arg0
)
13494 && can_native_interpret_type_p (type
)
13495 && BITS_PER_UNIT
== 8
13496 && tree_fits_uhwi_p (op1
)
13497 && tree_fits_uhwi_p (op2
))
13499 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13500 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13501 /* Limit us to a reasonable amount of work. To relax the
13502 other limitations we need bit-shifting of the buffer
13503 and rounding up the size. */
13504 if (bitpos
% BITS_PER_UNIT
== 0
13505 && bitsize
% BITS_PER_UNIT
== 0
13506 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13508 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13509 unsigned HOST_WIDE_INT len
13510 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13511 bitpos
/ BITS_PER_UNIT
);
13513 && len
* BITS_PER_UNIT
>= bitsize
)
13515 tree v
= native_interpret_expr (type
, b
,
13516 bitsize
/ BITS_PER_UNIT
);
13525 case VEC_PERM_EXPR
:
13526 /* Perform constant folding of BIT_INSERT_EXPR. */
13527 if (TREE_CODE (arg2
) == VECTOR_CST
13528 && TREE_CODE (op0
) == VECTOR_CST
13529 && TREE_CODE (op1
) == VECTOR_CST
)
13531 /* Build a vector of integers from the tree mask. */
13532 vec_perm_builder builder
;
13533 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13536 /* Create a vec_perm_indices for the integer vector. */
13537 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13538 bool single_arg
= (op0
== op1
);
13539 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13540 return fold_vec_perm (type
, op0
, op1
, sel
);
13544 case BIT_INSERT_EXPR
:
13545 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13546 if (TREE_CODE (arg0
) == INTEGER_CST
13547 && TREE_CODE (arg1
) == INTEGER_CST
)
13549 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13550 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13551 wide_int tem
= (wi::to_wide (arg0
)
13552 & wi::shifted_mask (bitpos
, bitsize
, true,
13553 TYPE_PRECISION (type
)));
13555 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13557 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13559 else if (TREE_CODE (arg0
) == VECTOR_CST
13560 && CONSTANT_CLASS_P (arg1
)
13561 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13564 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13565 unsigned HOST_WIDE_INT elsize
13566 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13567 if (bitpos
% elsize
== 0)
13569 unsigned k
= bitpos
/ elsize
;
13570 unsigned HOST_WIDE_INT nelts
;
13571 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13573 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13575 tree_vector_builder
elts (type
, nelts
, 1);
13576 elts
.quick_grow (nelts
);
13577 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13578 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13579 return elts
.build ();
13587 } /* switch (code) */
13590 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13591 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13592 constructor element index of the value returned. If the element is
13593 not found NULL_TREE is returned and *CTOR_IDX is updated to
13594 the index of the element after the ACCESS_INDEX position (which
13595 may be outside of the CTOR array). */
13598 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13599 unsigned *ctor_idx
)
13601 tree index_type
= NULL_TREE
;
13602 signop index_sgn
= UNSIGNED
;
13603 offset_int low_bound
= 0;
13605 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13607 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13608 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13610 /* Static constructors for variably sized objects makes no sense. */
13611 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13612 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13613 /* ??? When it is obvious that the range is signed, treat it so. */
13614 if (TYPE_UNSIGNED (index_type
)
13615 && TYPE_MAX_VALUE (domain_type
)
13616 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13617 TYPE_MIN_VALUE (domain_type
)))
13619 index_sgn
= SIGNED
;
13621 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13626 index_sgn
= TYPE_SIGN (index_type
);
13627 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13633 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13636 offset_int index
= low_bound
;
13638 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13640 offset_int max_index
= index
;
13643 bool first_p
= true;
13645 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13647 /* Array constructor might explicitly set index, or specify a range,
13648 or leave index NULL meaning that it is next index after previous
13652 if (TREE_CODE (cfield
) == INTEGER_CST
)
13654 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13657 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13658 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13661 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13663 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13668 index
= max_index
+ 1;
13670 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13671 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13677 /* Do we have match? */
13678 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13680 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13687 else if (in_gimple_form
)
13688 /* We're past the element we search for. Note during parsing
13689 the elements might not be sorted.
13690 ??? We should use a binary search and a flag on the
13691 CONSTRUCTOR as to whether elements are sorted in declaration
13700 /* Perform constant folding and related simplification of EXPR.
13701 The related simplifications include x*1 => x, x*0 => 0, etc.,
13702 and application of the associative law.
13703 NOP_EXPR conversions may be removed freely (as long as we
13704 are careful not to change the type of the overall expression).
13705 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13706 but we can constant-fold them if they have constant operands. */
13708 #ifdef ENABLE_FOLD_CHECKING
13709 # define fold(x) fold_1 (x)
13710 static tree
fold_1 (tree
);
13716 const tree t
= expr
;
13717 enum tree_code code
= TREE_CODE (t
);
13718 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13720 location_t loc
= EXPR_LOCATION (expr
);
13722 /* Return right away if a constant. */
13723 if (kind
== tcc_constant
)
13726 /* CALL_EXPR-like objects with variable numbers of operands are
13727 treated specially. */
13728 if (kind
== tcc_vl_exp
)
13730 if (code
== CALL_EXPR
)
13732 tem
= fold_call_expr (loc
, expr
, false);
13733 return tem
? tem
: expr
;
13738 if (IS_EXPR_CODE_CLASS (kind
))
13740 tree type
= TREE_TYPE (t
);
13741 tree op0
, op1
, op2
;
13743 switch (TREE_CODE_LENGTH (code
))
13746 op0
= TREE_OPERAND (t
, 0);
13747 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13748 return tem
? tem
: expr
;
13750 op0
= TREE_OPERAND (t
, 0);
13751 op1
= TREE_OPERAND (t
, 1);
13752 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13753 return tem
? tem
: expr
;
13755 op0
= TREE_OPERAND (t
, 0);
13756 op1
= TREE_OPERAND (t
, 1);
13757 op2
= TREE_OPERAND (t
, 2);
13758 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13759 return tem
? tem
: expr
;
13769 tree op0
= TREE_OPERAND (t
, 0);
13770 tree op1
= TREE_OPERAND (t
, 1);
13772 if (TREE_CODE (op1
) == INTEGER_CST
13773 && TREE_CODE (op0
) == CONSTRUCTOR
13774 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13776 tree val
= get_array_ctor_element_at_index (op0
,
13777 wi::to_offset (op1
));
13785 /* Return a VECTOR_CST if possible. */
13788 tree type
= TREE_TYPE (t
);
13789 if (TREE_CODE (type
) != VECTOR_TYPE
)
13794 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13795 if (! CONSTANT_CLASS_P (val
))
13798 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13802 return fold (DECL_INITIAL (t
));
13806 } /* switch (code) */
13809 #ifdef ENABLE_FOLD_CHECKING
13812 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13813 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13814 static void fold_check_failed (const_tree
, const_tree
);
13815 void print_fold_checksum (const_tree
);
13817 /* When --enable-checking=fold, compute a digest of expr before
13818 and after actual fold call to see if fold did not accidentally
13819 change original expr. */
13825 struct md5_ctx ctx
;
13826 unsigned char checksum_before
[16], checksum_after
[16];
13827 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13829 md5_init_ctx (&ctx
);
13830 fold_checksum_tree (expr
, &ctx
, &ht
);
13831 md5_finish_ctx (&ctx
, checksum_before
);
13834 ret
= fold_1 (expr
);
13836 md5_init_ctx (&ctx
);
13837 fold_checksum_tree (expr
, &ctx
, &ht
);
13838 md5_finish_ctx (&ctx
, checksum_after
);
13840 if (memcmp (checksum_before
, checksum_after
, 16))
13841 fold_check_failed (expr
, ret
);
13847 print_fold_checksum (const_tree expr
)
13849 struct md5_ctx ctx
;
13850 unsigned char checksum
[16], cnt
;
13851 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13853 md5_init_ctx (&ctx
);
13854 fold_checksum_tree (expr
, &ctx
, &ht
);
13855 md5_finish_ctx (&ctx
, checksum
);
13856 for (cnt
= 0; cnt
< 16; ++cnt
)
13857 fprintf (stderr
, "%02x", checksum
[cnt
]);
13858 putc ('\n', stderr
);
13862 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13864 internal_error ("fold check: original tree changed by fold");
13868 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13869 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13871 const tree_node
**slot
;
13872 enum tree_code code
;
13873 union tree_node
*buf
;
13879 slot
= ht
->find_slot (expr
, INSERT
);
13883 code
= TREE_CODE (expr
);
13884 if (TREE_CODE_CLASS (code
) == tcc_declaration
13885 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13887 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13888 size_t sz
= tree_size (expr
);
13889 buf
= XALLOCAVAR (union tree_node
, sz
);
13890 memcpy ((char *) buf
, expr
, sz
);
13891 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13892 buf
->decl_with_vis
.symtab_node
= NULL
;
13893 buf
->base
.nowarning_flag
= 0;
13896 else if (TREE_CODE_CLASS (code
) == tcc_type
13897 && (TYPE_POINTER_TO (expr
)
13898 || TYPE_REFERENCE_TO (expr
)
13899 || TYPE_CACHED_VALUES_P (expr
)
13900 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13901 || TYPE_NEXT_VARIANT (expr
)
13902 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13904 /* Allow these fields to be modified. */
13906 size_t sz
= tree_size (expr
);
13907 buf
= XALLOCAVAR (union tree_node
, sz
);
13908 memcpy ((char *) buf
, expr
, sz
);
13909 expr
= tmp
= (tree
) buf
;
13910 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13911 TYPE_POINTER_TO (tmp
) = NULL
;
13912 TYPE_REFERENCE_TO (tmp
) = NULL
;
13913 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13914 TYPE_ALIAS_SET (tmp
) = -1;
13915 if (TYPE_CACHED_VALUES_P (tmp
))
13917 TYPE_CACHED_VALUES_P (tmp
) = 0;
13918 TYPE_CACHED_VALUES (tmp
) = NULL
;
13921 else if (warning_suppressed_p (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13923 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13924 that and change builtins.cc etc. instead - see PR89543. */
13925 size_t sz
= tree_size (expr
);
13926 buf
= XALLOCAVAR (union tree_node
, sz
);
13927 memcpy ((char *) buf
, expr
, sz
);
13928 buf
->base
.nowarning_flag
= 0;
13931 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13932 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13933 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13934 if (TREE_CODE_CLASS (code
) != tcc_type
13935 && TREE_CODE_CLASS (code
) != tcc_declaration
13936 && code
!= TREE_LIST
13937 && code
!= SSA_NAME
13938 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13939 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13940 switch (TREE_CODE_CLASS (code
))
13946 md5_process_bytes (TREE_STRING_POINTER (expr
),
13947 TREE_STRING_LENGTH (expr
), ctx
);
13950 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13951 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13954 len
= vector_cst_encoded_nelts (expr
);
13955 for (i
= 0; i
< len
; ++i
)
13956 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13962 case tcc_exceptional
:
13966 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13967 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13968 expr
= TREE_CHAIN (expr
);
13969 goto recursive_label
;
13972 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13973 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13979 case tcc_expression
:
13980 case tcc_reference
:
13981 case tcc_comparison
:
13984 case tcc_statement
:
13986 len
= TREE_OPERAND_LENGTH (expr
);
13987 for (i
= 0; i
< len
; ++i
)
13988 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13990 case tcc_declaration
:
13991 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13992 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13993 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13995 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13996 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13997 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13998 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13999 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
14002 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
14004 if (TREE_CODE (expr
) == FUNCTION_DECL
)
14006 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
14007 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
14009 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
14013 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
14014 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
14015 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
14016 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
14017 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
14018 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
14019 if (INTEGRAL_TYPE_P (expr
)
14020 || SCALAR_FLOAT_TYPE_P (expr
))
14022 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
14023 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
14025 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
14026 if (RECORD_OR_UNION_TYPE_P (expr
))
14027 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
14028 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
14035 /* Helper function for outputting the checksum of a tree T. When
14036 debugging with gdb, you can "define mynext" to be "next" followed
14037 by "call debug_fold_checksum (op0)", then just trace down till the
14040 DEBUG_FUNCTION
void
14041 debug_fold_checksum (const_tree t
)
14044 unsigned char checksum
[16];
14045 struct md5_ctx ctx
;
14046 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14048 md5_init_ctx (&ctx
);
14049 fold_checksum_tree (t
, &ctx
, &ht
);
14050 md5_finish_ctx (&ctx
, checksum
);
14053 for (i
= 0; i
< 16; i
++)
14054 fprintf (stderr
, "%d ", checksum
[i
]);
14056 fprintf (stderr
, "\n");
14061 /* Fold a unary tree expression with code CODE of type TYPE with an
14062 operand OP0. LOC is the location of the resulting expression.
14063 Return a folded expression if successful. Otherwise, return a tree
14064 expression with code CODE of type TYPE with an operand OP0. */
14067 fold_build1_loc (location_t loc
,
14068 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
14071 #ifdef ENABLE_FOLD_CHECKING
14072 unsigned char checksum_before
[16], checksum_after
[16];
14073 struct md5_ctx ctx
;
14074 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14076 md5_init_ctx (&ctx
);
14077 fold_checksum_tree (op0
, &ctx
, &ht
);
14078 md5_finish_ctx (&ctx
, checksum_before
);
14082 tem
= fold_unary_loc (loc
, code
, type
, op0
);
14084 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
14086 #ifdef ENABLE_FOLD_CHECKING
14087 md5_init_ctx (&ctx
);
14088 fold_checksum_tree (op0
, &ctx
, &ht
);
14089 md5_finish_ctx (&ctx
, checksum_after
);
14091 if (memcmp (checksum_before
, checksum_after
, 16))
14092 fold_check_failed (op0
, tem
);
14097 /* Fold a binary tree expression with code CODE of type TYPE with
14098 operands OP0 and OP1. LOC is the location of the resulting
14099 expression. Return a folded expression if successful. Otherwise,
14100 return a tree expression with code CODE of type TYPE with operands
14104 fold_build2_loc (location_t loc
,
14105 enum tree_code code
, tree type
, tree op0
, tree op1
14109 #ifdef ENABLE_FOLD_CHECKING
14110 unsigned char checksum_before_op0
[16],
14111 checksum_before_op1
[16],
14112 checksum_after_op0
[16],
14113 checksum_after_op1
[16];
14114 struct md5_ctx ctx
;
14115 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14117 md5_init_ctx (&ctx
);
14118 fold_checksum_tree (op0
, &ctx
, &ht
);
14119 md5_finish_ctx (&ctx
, checksum_before_op0
);
14122 md5_init_ctx (&ctx
);
14123 fold_checksum_tree (op1
, &ctx
, &ht
);
14124 md5_finish_ctx (&ctx
, checksum_before_op1
);
14128 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
14130 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
14132 #ifdef ENABLE_FOLD_CHECKING
14133 md5_init_ctx (&ctx
);
14134 fold_checksum_tree (op0
, &ctx
, &ht
);
14135 md5_finish_ctx (&ctx
, checksum_after_op0
);
14138 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14139 fold_check_failed (op0
, tem
);
14141 md5_init_ctx (&ctx
);
14142 fold_checksum_tree (op1
, &ctx
, &ht
);
14143 md5_finish_ctx (&ctx
, checksum_after_op1
);
14145 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14146 fold_check_failed (op1
, tem
);
14151 /* Fold a ternary tree expression with code CODE of type TYPE with
14152 operands OP0, OP1, and OP2. Return a folded expression if
14153 successful. Otherwise, return a tree expression with code CODE of
14154 type TYPE with operands OP0, OP1, and OP2. */
14157 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
14158 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
14161 #ifdef ENABLE_FOLD_CHECKING
14162 unsigned char checksum_before_op0
[16],
14163 checksum_before_op1
[16],
14164 checksum_before_op2
[16],
14165 checksum_after_op0
[16],
14166 checksum_after_op1
[16],
14167 checksum_after_op2
[16];
14168 struct md5_ctx ctx
;
14169 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14171 md5_init_ctx (&ctx
);
14172 fold_checksum_tree (op0
, &ctx
, &ht
);
14173 md5_finish_ctx (&ctx
, checksum_before_op0
);
14176 md5_init_ctx (&ctx
);
14177 fold_checksum_tree (op1
, &ctx
, &ht
);
14178 md5_finish_ctx (&ctx
, checksum_before_op1
);
14181 md5_init_ctx (&ctx
);
14182 fold_checksum_tree (op2
, &ctx
, &ht
);
14183 md5_finish_ctx (&ctx
, checksum_before_op2
);
14187 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
14188 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
14190 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
14192 #ifdef ENABLE_FOLD_CHECKING
14193 md5_init_ctx (&ctx
);
14194 fold_checksum_tree (op0
, &ctx
, &ht
);
14195 md5_finish_ctx (&ctx
, checksum_after_op0
);
14198 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14199 fold_check_failed (op0
, tem
);
14201 md5_init_ctx (&ctx
);
14202 fold_checksum_tree (op1
, &ctx
, &ht
);
14203 md5_finish_ctx (&ctx
, checksum_after_op1
);
14206 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14207 fold_check_failed (op1
, tem
);
14209 md5_init_ctx (&ctx
);
14210 fold_checksum_tree (op2
, &ctx
, &ht
);
14211 md5_finish_ctx (&ctx
, checksum_after_op2
);
14213 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
14214 fold_check_failed (op2
, tem
);
14219 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14220 arguments in ARGARRAY, and a null static chain.
14221 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14222 of type TYPE from the given operands as constructed by build_call_array. */
14225 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
14226 int nargs
, tree
*argarray
)
14229 #ifdef ENABLE_FOLD_CHECKING
14230 unsigned char checksum_before_fn
[16],
14231 checksum_before_arglist
[16],
14232 checksum_after_fn
[16],
14233 checksum_after_arglist
[16];
14234 struct md5_ctx ctx
;
14235 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14238 md5_init_ctx (&ctx
);
14239 fold_checksum_tree (fn
, &ctx
, &ht
);
14240 md5_finish_ctx (&ctx
, checksum_before_fn
);
14243 md5_init_ctx (&ctx
);
14244 for (i
= 0; i
< nargs
; i
++)
14245 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14246 md5_finish_ctx (&ctx
, checksum_before_arglist
);
14250 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
14252 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14254 #ifdef ENABLE_FOLD_CHECKING
14255 md5_init_ctx (&ctx
);
14256 fold_checksum_tree (fn
, &ctx
, &ht
);
14257 md5_finish_ctx (&ctx
, checksum_after_fn
);
14260 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
14261 fold_check_failed (fn
, tem
);
14263 md5_init_ctx (&ctx
);
14264 for (i
= 0; i
< nargs
; i
++)
14265 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14266 md5_finish_ctx (&ctx
, checksum_after_arglist
);
14268 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
14269 fold_check_failed (NULL_TREE
, tem
);
14274 /* Perform constant folding and related simplification of initializer
14275 expression EXPR. These behave identically to "fold_buildN" but ignore
14276 potential run-time traps and exceptions that fold must preserve. */
14278 #define START_FOLD_INIT \
14279 int saved_signaling_nans = flag_signaling_nans;\
14280 int saved_trapping_math = flag_trapping_math;\
14281 int saved_rounding_math = flag_rounding_math;\
14282 int saved_trapv = flag_trapv;\
14283 int saved_folding_initializer = folding_initializer;\
14284 flag_signaling_nans = 0;\
14285 flag_trapping_math = 0;\
14286 flag_rounding_math = 0;\
14288 folding_initializer = 1;
14290 #define END_FOLD_INIT \
14291 flag_signaling_nans = saved_signaling_nans;\
14292 flag_trapping_math = saved_trapping_math;\
14293 flag_rounding_math = saved_rounding_math;\
14294 flag_trapv = saved_trapv;\
14295 folding_initializer = saved_folding_initializer;
14298 fold_init (tree expr
)
14303 result
= fold (expr
);
14310 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
14311 tree type
, tree op
)
14316 result
= fold_build1_loc (loc
, code
, type
, op
);
14323 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
14324 tree type
, tree op0
, tree op1
)
14329 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
14336 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
14337 int nargs
, tree
*argarray
)
14342 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14349 fold_binary_initializer_loc (location_t loc
, tree_code code
, tree type
,
14350 tree lhs
, tree rhs
)
14355 result
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
14361 #undef START_FOLD_INIT
14362 #undef END_FOLD_INIT
14364 /* Determine if first argument is a multiple of second argument. Return
14365 false if it is not, or we cannot easily determined it to be.
14367 An example of the sort of thing we care about (at this point; this routine
14368 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14369 fold cases do now) is discovering that
14371 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14377 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14379 This code also handles discovering that
14381 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14383 is a multiple of 8 so we don't have to worry about dealing with a
14384 possible remainder.
14386 Note that we *look* inside a SAVE_EXPR only to determine how it was
14387 calculated; it is not safe for fold to do much of anything else with the
14388 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14389 at run time. For example, the latter example above *cannot* be implemented
14390 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14391 evaluation time of the original SAVE_EXPR is not necessarily the same at
14392 the time the new expression is evaluated. The only optimization of this
14393 sort that would be valid is changing
14395 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14399 SAVE_EXPR (I) * SAVE_EXPR (J)
14401 (where the same SAVE_EXPR (J) is used in the original and the
14402 transformed version).
14404 NOWRAP specifies whether all outer operations in TYPE should
14405 be considered not wrapping. Any type conversion within TOP acts
14406 as a barrier and we will fall back to NOWRAP being false.
14407 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14408 as not wrapping even though they are generally using unsigned arithmetic. */
14411 multiple_of_p (tree type
, const_tree top
, const_tree bottom
, bool nowrap
)
14416 if (operand_equal_p (top
, bottom
, 0))
14419 if (TREE_CODE (type
) != INTEGER_TYPE
)
14422 switch (TREE_CODE (top
))
14425 /* Bitwise and provides a power of two multiple. If the mask is
14426 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14427 if (!integer_pow2p (bottom
))
14429 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14430 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14433 /* If the multiplication can wrap we cannot recurse further unless
14434 the bottom is a power of two which is where wrapping does not
14437 && !TYPE_OVERFLOW_UNDEFINED (type
)
14438 && !integer_pow2p (bottom
))
14440 if (TREE_CODE (bottom
) == INTEGER_CST
)
14442 op1
= TREE_OPERAND (top
, 0);
14443 op2
= TREE_OPERAND (top
, 1);
14444 if (TREE_CODE (op1
) == INTEGER_CST
)
14445 std::swap (op1
, op2
);
14446 if (TREE_CODE (op2
) == INTEGER_CST
)
14448 if (multiple_of_p (type
, op2
, bottom
, nowrap
))
14450 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14451 if (multiple_of_p (type
, bottom
, op2
, nowrap
))
14453 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14454 wi::to_widest (op2
));
14455 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14457 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14458 return multiple_of_p (type
, op1
, op2
, nowrap
);
14461 return multiple_of_p (type
, op1
, bottom
, nowrap
);
14464 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14465 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14468 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14469 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14471 op1
= TREE_OPERAND (top
, 1);
14472 if (wi::to_widest (op1
) < TYPE_PRECISION (type
))
14475 = wi::one (TYPE_PRECISION (type
)) << wi::to_wide (op1
);
14476 return multiple_of_p (type
,
14477 wide_int_to_tree (type
, mul_op
), bottom
,
14485 /* If the addition or subtraction can wrap we cannot recurse further
14486 unless bottom is a power of two which is where wrapping does not
14489 && !TYPE_OVERFLOW_UNDEFINED (type
)
14490 && !integer_pow2p (bottom
))
14493 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14494 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14495 but 0xfffffffd is not. */
14496 op1
= TREE_OPERAND (top
, 1);
14497 if (TREE_CODE (top
) == PLUS_EXPR
14499 && TYPE_UNSIGNED (type
)
14500 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14501 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14503 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14504 precisely, so be conservative here checking if both op0 and op1
14505 are multiple of bottom. Note we check the second operand first
14506 since it's usually simpler. */
14507 return (multiple_of_p (type
, op1
, bottom
, nowrap
)
14508 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14511 /* Can't handle conversions from non-integral or wider integral type. */
14512 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14513 || (TYPE_PRECISION (type
)
14514 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14516 /* NOWRAP only extends to operations in the outermost type so
14517 make sure to strip it off here. */
14518 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top
, 0)),
14519 TREE_OPERAND (top
, 0), bottom
, false);
14522 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
);
14525 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14526 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
, nowrap
));
14529 if (TREE_CODE (bottom
) != INTEGER_CST
|| integer_zerop (bottom
))
14531 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14535 if (TREE_CODE (bottom
) == INTEGER_CST
14536 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14537 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14539 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14541 /* Check for special cases to see if top is defined as multiple
14544 top = (X & ~(bottom - 1) ; bottom is power of 2
14550 if (code
== BIT_AND_EXPR
14551 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14552 && TREE_CODE (op2
) == INTEGER_CST
14553 && integer_pow2p (bottom
)
14554 && wi::multiple_of_p (wi::to_widest (op2
),
14555 wi::to_widest (bottom
), UNSIGNED
))
14558 op1
= gimple_assign_rhs1 (stmt
);
14559 if (code
== MINUS_EXPR
14560 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14561 && TREE_CODE (op2
) == SSA_NAME
14562 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14563 && gimple_code (stmt
) == GIMPLE_ASSIGN
14564 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14565 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14566 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14573 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14574 return multiple_p (wi::to_poly_widest (top
),
14575 wi::to_poly_widest (bottom
));
14581 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14582 This function returns true for integer expressions, and returns
14583 false if uncertain. */
14586 tree_expr_finite_p (const_tree x
)
14588 machine_mode mode
= element_mode (x
);
14589 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14591 switch (TREE_CODE (x
))
14594 return real_isfinite (TREE_REAL_CST_PTR (x
));
14596 return tree_expr_finite_p (TREE_REALPART (x
))
14597 && tree_expr_finite_p (TREE_IMAGPART (x
));
14602 case NON_LVALUE_EXPR
:
14605 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14608 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14609 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14611 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14612 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14614 switch (get_call_combined_fn (x
))
14618 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14623 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14624 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14634 /* Return true if expression X evaluates to an infinity.
14635 This function returns false for integer expressions. */
14638 tree_expr_infinite_p (const_tree x
)
14640 if (!HONOR_INFINITIES (x
))
14642 switch (TREE_CODE (x
))
14645 return real_isinf (TREE_REAL_CST_PTR (x
));
14648 case NON_LVALUE_EXPR
:
14650 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14652 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14653 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14659 /* Return true if expression X could evaluate to an infinity.
14660 This function returns false for integer expressions, and returns
14661 true if uncertain. */
14664 tree_expr_maybe_infinite_p (const_tree x
)
14666 if (!HONOR_INFINITIES (x
))
14668 switch (TREE_CODE (x
))
14671 return real_isinf (TREE_REAL_CST_PTR (x
));
14676 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14678 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14679 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14685 /* Return true if expression X evaluates to a signaling NaN.
14686 This function returns false for integer expressions. */
14689 tree_expr_signaling_nan_p (const_tree x
)
14691 if (!HONOR_SNANS (x
))
14693 switch (TREE_CODE (x
))
14696 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14697 case NON_LVALUE_EXPR
:
14699 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14701 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14702 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14708 /* Return true if expression X could evaluate to a signaling NaN.
14709 This function returns false for integer expressions, and returns
14710 true if uncertain. */
14713 tree_expr_maybe_signaling_nan_p (const_tree x
)
14715 if (!HONOR_SNANS (x
))
14717 switch (TREE_CODE (x
))
14720 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14726 case NON_LVALUE_EXPR
:
14728 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14731 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14732 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14734 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14735 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14737 switch (get_call_combined_fn (x
))
14741 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14746 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14747 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14756 /* Return true if expression X evaluates to a NaN.
14757 This function returns false for integer expressions. */
14760 tree_expr_nan_p (const_tree x
)
14762 if (!HONOR_NANS (x
))
14764 switch (TREE_CODE (x
))
14767 return real_isnan (TREE_REAL_CST_PTR (x
));
14768 case NON_LVALUE_EXPR
:
14770 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14772 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14773 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14779 /* Return true if expression X could evaluate to a NaN.
14780 This function returns false for integer expressions, and returns
14781 true if uncertain. */
14784 tree_expr_maybe_nan_p (const_tree x
)
14786 if (!HONOR_NANS (x
))
14788 switch (TREE_CODE (x
))
14791 return real_isnan (TREE_REAL_CST_PTR (x
));
14797 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14798 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14802 case NON_LVALUE_EXPR
:
14804 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14807 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14808 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14810 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14811 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14813 switch (get_call_combined_fn (x
))
14817 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14822 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14823 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14832 /* Return true if expression X could evaluate to -0.0.
14833 This function returns true if uncertain. */
14836 tree_expr_maybe_real_minus_zero_p (const_tree x
)
14838 if (!HONOR_SIGNED_ZEROS (x
))
14840 switch (TREE_CODE (x
))
14843 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x
));
14848 case NON_LVALUE_EXPR
:
14850 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 0));
14852 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 1))
14853 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 2));
14855 switch (get_call_combined_fn (x
))
14866 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14867 * but currently those predicates require tree and not const_tree. */
14871 #define tree_expr_nonnegative_warnv_p(X, Y) \
14872 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14874 #define RECURSE(X) \
14875 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14877 /* Return true if CODE or TYPE is known to be non-negative. */
14880 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14882 if (!VECTOR_TYPE_P (type
)
14883 && (TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14884 && truth_value_p (code
))
14885 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14886 have a signed:1 type (where the value is -1 and 0). */
14891 /* Return true if (CODE OP0) is known to be non-negative. If the return
14892 value is based on the assumption that signed overflow is undefined,
14893 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14894 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14897 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14898 bool *strict_overflow_p
, int depth
)
14900 if (TYPE_UNSIGNED (type
))
14906 /* We can't return 1 if flag_wrapv is set because
14907 ABS_EXPR<INT_MIN> = INT_MIN. */
14908 if (!ANY_INTEGRAL_TYPE_P (type
))
14910 if (TYPE_OVERFLOW_UNDEFINED (type
))
14912 *strict_overflow_p
= true;
14917 case NON_LVALUE_EXPR
:
14919 case FIX_TRUNC_EXPR
:
14920 return RECURSE (op0
);
14924 tree inner_type
= TREE_TYPE (op0
);
14925 tree outer_type
= type
;
14927 if (SCALAR_FLOAT_TYPE_P (outer_type
))
14929 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14930 return RECURSE (op0
);
14931 if (INTEGRAL_TYPE_P (inner_type
))
14933 if (TYPE_UNSIGNED (inner_type
))
14935 return RECURSE (op0
);
14938 else if (INTEGRAL_TYPE_P (outer_type
))
14940 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14941 return RECURSE (op0
);
14942 if (INTEGRAL_TYPE_P (inner_type
))
14943 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14944 && TYPE_UNSIGNED (inner_type
);
14950 return tree_simple_nonnegative_warnv_p (code
, type
);
14953 /* We don't know sign of `t', so be conservative and return false. */
14957 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14958 value is based on the assumption that signed overflow is undefined,
14959 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14960 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14963 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14964 tree op1
, bool *strict_overflow_p
,
14967 if (TYPE_UNSIGNED (type
))
14972 case POINTER_PLUS_EXPR
:
14974 if (FLOAT_TYPE_P (type
))
14975 return RECURSE (op0
) && RECURSE (op1
);
14977 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14978 both unsigned and at least 2 bits shorter than the result. */
14979 if (TREE_CODE (type
) == INTEGER_TYPE
14980 && TREE_CODE (op0
) == NOP_EXPR
14981 && TREE_CODE (op1
) == NOP_EXPR
)
14983 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14984 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14985 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14986 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14988 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14989 TYPE_PRECISION (inner2
)) + 1;
14990 return prec
< TYPE_PRECISION (type
);
14996 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14998 /* x * x is always non-negative for floating point x
14999 or without overflow. */
15000 if (operand_equal_p (op0
, op1
, 0)
15001 || (RECURSE (op0
) && RECURSE (op1
)))
15003 if (ANY_INTEGRAL_TYPE_P (type
)
15004 && TYPE_OVERFLOW_UNDEFINED (type
))
15005 *strict_overflow_p
= true;
15010 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
15011 both unsigned and their total bits is shorter than the result. */
15012 if (TREE_CODE (type
) == INTEGER_TYPE
15013 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
15014 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
15016 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
15017 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
15019 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
15020 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
15023 bool unsigned0
= TYPE_UNSIGNED (inner0
);
15024 bool unsigned1
= TYPE_UNSIGNED (inner1
);
15026 if (TREE_CODE (op0
) == INTEGER_CST
)
15027 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
15029 if (TREE_CODE (op1
) == INTEGER_CST
)
15030 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
15032 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
15033 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
15035 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
15036 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
15037 : TYPE_PRECISION (inner0
);
15039 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
15040 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
15041 : TYPE_PRECISION (inner1
);
15043 return precision0
+ precision1
< TYPE_PRECISION (type
);
15049 return RECURSE (op0
) || RECURSE (op1
);
15052 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
15054 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
15055 return RECURSE (op0
) && RECURSE (op1
);
15056 return RECURSE (op0
) || RECURSE (op1
);
15062 case TRUNC_DIV_EXPR
:
15063 case CEIL_DIV_EXPR
:
15064 case FLOOR_DIV_EXPR
:
15065 case ROUND_DIV_EXPR
:
15066 return RECURSE (op0
) && RECURSE (op1
);
15068 case TRUNC_MOD_EXPR
:
15069 return RECURSE (op0
);
15071 case FLOOR_MOD_EXPR
:
15072 return RECURSE (op1
);
15074 case CEIL_MOD_EXPR
:
15075 case ROUND_MOD_EXPR
:
15077 return tree_simple_nonnegative_warnv_p (code
, type
);
15080 /* We don't know sign of `t', so be conservative and return false. */
15084 /* Return true if T is known to be non-negative. If the return
15085 value is based on the assumption that signed overflow is undefined,
15086 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15087 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15090 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15092 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15095 switch (TREE_CODE (t
))
15098 return tree_int_cst_sgn (t
) >= 0;
15101 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
15104 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
15107 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15110 /* Limit the depth of recursion to avoid quadratic behavior.
15111 This is expected to catch almost all occurrences in practice.
15112 If this code misses important cases that unbounded recursion
15113 would not, passes that need this information could be revised
15114 to provide it through dataflow propagation. */
15115 return (!name_registered_for_update_p (t
)
15116 && depth
< param_max_ssa_name_query_depth
15117 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
15118 strict_overflow_p
, depth
));
15121 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15125 /* Return true if T is known to be non-negative. If the return
15126 value is based on the assumption that signed overflow is undefined,
15127 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15128 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15131 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
15132 bool *strict_overflow_p
, int depth
)
15163 case CFN_BUILT_IN_BSWAP16
:
15164 case CFN_BUILT_IN_BSWAP32
:
15165 case CFN_BUILT_IN_BSWAP64
:
15166 case CFN_BUILT_IN_BSWAP128
:
15172 /* sqrt(-0.0) is -0.0. */
15173 if (!HONOR_SIGNED_ZEROS (type
))
15175 return RECURSE (arg0
);
15207 CASE_CFN_LLRINT_FN
:
15209 CASE_CFN_LLROUND_FN
:
15213 CASE_CFN_LROUND_FN
:
15216 CASE_CFN_NEARBYINT
:
15217 CASE_CFN_NEARBYINT_FN
:
15222 CASE_CFN_ROUNDEVEN
:
15223 CASE_CFN_ROUNDEVEN_FN
:
15226 CASE_CFN_SCALBLN_FN
:
15228 CASE_CFN_SCALBN_FN
:
15230 CASE_CFN_SIGNIFICAND
:
15237 /* True if the 1st argument is nonnegative. */
15238 return RECURSE (arg0
);
15242 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15243 things. In the presence of sNaNs, we're only guaranteed to be
15244 non-negative if both operands are non-negative. In the presence
15245 of qNaNs, we're non-negative if either operand is non-negative
15246 and can't be a qNaN, or if both operands are non-negative. */
15247 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
15248 tree_expr_maybe_signaling_nan_p (arg1
))
15249 return RECURSE (arg0
) && RECURSE (arg1
);
15250 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
15253 && !tree_expr_maybe_nan_p (arg1
));
15257 /* True if the 1st AND 2nd arguments are nonnegative. */
15258 return RECURSE (arg0
) && RECURSE (arg1
);
15261 CASE_CFN_COPYSIGN_FN
:
15262 /* True if the 2nd argument is nonnegative. */
15263 return RECURSE (arg1
);
15266 /* True if the 1st argument is nonnegative or the second
15267 argument is an even integer. */
15268 if (TREE_CODE (arg1
) == INTEGER_CST
15269 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
15271 return RECURSE (arg0
);
15275 /* True if the 1st argument is nonnegative or the second
15276 argument is an even integer valued real. */
15277 if (TREE_CODE (arg1
) == REAL_CST
)
15282 c
= TREE_REAL_CST (arg1
);
15283 n
= real_to_integer (&c
);
15286 REAL_VALUE_TYPE cint
;
15287 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
15288 if (real_identical (&c
, &cint
))
15292 return RECURSE (arg0
);
15297 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
15300 /* Return true if T is known to be non-negative. If the return
15301 value is based on the assumption that signed overflow is undefined,
15302 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15303 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15306 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15308 enum tree_code code
= TREE_CODE (t
);
15309 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15316 tree temp
= TARGET_EXPR_SLOT (t
);
15317 t
= TARGET_EXPR_INITIAL (t
);
15319 /* If the initializer is non-void, then it's a normal expression
15320 that will be assigned to the slot. */
15321 if (!VOID_TYPE_P (TREE_TYPE (t
)))
15322 return RECURSE (t
);
15324 /* Otherwise, the initializer sets the slot in some way. One common
15325 way is an assignment statement at the end of the initializer. */
15328 if (TREE_CODE (t
) == BIND_EXPR
)
15329 t
= expr_last (BIND_EXPR_BODY (t
));
15330 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
15331 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
15332 t
= expr_last (TREE_OPERAND (t
, 0));
15333 else if (TREE_CODE (t
) == STATEMENT_LIST
)
15338 if (TREE_CODE (t
) == MODIFY_EXPR
15339 && TREE_OPERAND (t
, 0) == temp
)
15340 return RECURSE (TREE_OPERAND (t
, 1));
15347 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
15348 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
15350 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
15351 get_call_combined_fn (t
),
15354 strict_overflow_p
, depth
);
15356 case COMPOUND_EXPR
:
15358 return RECURSE (TREE_OPERAND (t
, 1));
15361 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
15364 return RECURSE (TREE_OPERAND (t
, 0));
15367 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15372 #undef tree_expr_nonnegative_warnv_p
15374 /* Return true if T is known to be non-negative. If the return
15375 value is based on the assumption that signed overflow is undefined,
15376 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15377 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15380 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15382 enum tree_code code
;
15383 if (t
== error_mark_node
)
15386 code
= TREE_CODE (t
);
15387 switch (TREE_CODE_CLASS (code
))
15390 case tcc_comparison
:
15391 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15393 TREE_OPERAND (t
, 0),
15394 TREE_OPERAND (t
, 1),
15395 strict_overflow_p
, depth
);
15398 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15400 TREE_OPERAND (t
, 0),
15401 strict_overflow_p
, depth
);
15404 case tcc_declaration
:
15405 case tcc_reference
:
15406 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15414 case TRUTH_AND_EXPR
:
15415 case TRUTH_OR_EXPR
:
15416 case TRUTH_XOR_EXPR
:
15417 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15419 TREE_OPERAND (t
, 0),
15420 TREE_OPERAND (t
, 1),
15421 strict_overflow_p
, depth
);
15422 case TRUTH_NOT_EXPR
:
15423 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15425 TREE_OPERAND (t
, 0),
15426 strict_overflow_p
, depth
);
15432 case WITH_SIZE_EXPR
:
15434 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15437 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15441 /* Return true if `t' is known to be non-negative. Handle warnings
15442 about undefined signed overflow. */
15445 tree_expr_nonnegative_p (tree t
)
15447 bool ret
, strict_overflow_p
;
15449 strict_overflow_p
= false;
15450 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15451 if (strict_overflow_p
)
15452 fold_overflow_warning (("assuming signed overflow does not occur when "
15453 "determining that expression is always "
15455 WARN_STRICT_OVERFLOW_MISC
);
15460 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15461 For floating point we further ensure that T is not denormal.
15462 Similar logic is present in nonzero_address in rtlanal.h.
15464 If the return value is based on the assumption that signed overflow
15465 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15466 change *STRICT_OVERFLOW_P. */
15469 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15470 bool *strict_overflow_p
)
15475 return tree_expr_nonzero_warnv_p (op0
,
15476 strict_overflow_p
);
15480 tree inner_type
= TREE_TYPE (op0
);
15481 tree outer_type
= type
;
15483 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15484 && tree_expr_nonzero_warnv_p (op0
,
15485 strict_overflow_p
));
15489 case NON_LVALUE_EXPR
:
15490 return tree_expr_nonzero_warnv_p (op0
,
15491 strict_overflow_p
);
15500 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15501 For floating point we further ensure that T is not denormal.
15502 Similar logic is present in nonzero_address in rtlanal.h.
15504 If the return value is based on the assumption that signed overflow
15505 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15506 change *STRICT_OVERFLOW_P. */
15509 tree_binary_nonzero_warnv_p (enum tree_code code
,
15512 tree op1
, bool *strict_overflow_p
)
15514 bool sub_strict_overflow_p
;
15517 case POINTER_PLUS_EXPR
:
15519 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15521 /* With the presence of negative values it is hard
15522 to say something. */
15523 sub_strict_overflow_p
= false;
15524 if (!tree_expr_nonnegative_warnv_p (op0
,
15525 &sub_strict_overflow_p
)
15526 || !tree_expr_nonnegative_warnv_p (op1
,
15527 &sub_strict_overflow_p
))
15529 /* One of operands must be positive and the other non-negative. */
15530 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15531 overflows, on a twos-complement machine the sum of two
15532 nonnegative numbers can never be zero. */
15533 return (tree_expr_nonzero_warnv_p (op0
,
15535 || tree_expr_nonzero_warnv_p (op1
,
15536 strict_overflow_p
));
15541 if (TYPE_OVERFLOW_UNDEFINED (type
))
15543 if (tree_expr_nonzero_warnv_p (op0
,
15545 && tree_expr_nonzero_warnv_p (op1
,
15546 strict_overflow_p
))
15548 *strict_overflow_p
= true;
15555 sub_strict_overflow_p
= false;
15556 if (tree_expr_nonzero_warnv_p (op0
,
15557 &sub_strict_overflow_p
)
15558 && tree_expr_nonzero_warnv_p (op1
,
15559 &sub_strict_overflow_p
))
15561 if (sub_strict_overflow_p
)
15562 *strict_overflow_p
= true;
15567 sub_strict_overflow_p
= false;
15568 if (tree_expr_nonzero_warnv_p (op0
,
15569 &sub_strict_overflow_p
))
15571 if (sub_strict_overflow_p
)
15572 *strict_overflow_p
= true;
15574 /* When both operands are nonzero, then MAX must be too. */
15575 if (tree_expr_nonzero_warnv_p (op1
,
15576 strict_overflow_p
))
15579 /* MAX where operand 0 is positive is positive. */
15580 return tree_expr_nonnegative_warnv_p (op0
,
15581 strict_overflow_p
);
15583 /* MAX where operand 1 is positive is positive. */
15584 else if (tree_expr_nonzero_warnv_p (op1
,
15585 &sub_strict_overflow_p
)
15586 && tree_expr_nonnegative_warnv_p (op1
,
15587 &sub_strict_overflow_p
))
15589 if (sub_strict_overflow_p
)
15590 *strict_overflow_p
= true;
15596 return (tree_expr_nonzero_warnv_p (op1
,
15598 || tree_expr_nonzero_warnv_p (op0
,
15599 strict_overflow_p
));
15608 /* Return true when T is an address and is known to be nonzero.
15609 For floating point we further ensure that T is not denormal.
15610 Similar logic is present in nonzero_address in rtlanal.h.
15612 If the return value is based on the assumption that signed overflow
15613 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15614 change *STRICT_OVERFLOW_P. */
15617 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15619 bool sub_strict_overflow_p
;
15620 switch (TREE_CODE (t
))
15623 return !integer_zerop (t
);
15627 tree base
= TREE_OPERAND (t
, 0);
15629 if (!DECL_P (base
))
15630 base
= get_base_address (base
);
15632 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15633 base
= TARGET_EXPR_SLOT (base
);
15638 /* For objects in symbol table check if we know they are non-zero.
15639 Don't do anything for variables and functions before symtab is built;
15640 it is quite possible that they will be declared weak later. */
15641 int nonzero_addr
= maybe_nonzero_address (base
);
15642 if (nonzero_addr
>= 0)
15643 return nonzero_addr
;
15645 /* Constants are never weak. */
15646 if (CONSTANT_CLASS_P (base
))
15653 sub_strict_overflow_p
= false;
15654 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15655 &sub_strict_overflow_p
)
15656 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15657 &sub_strict_overflow_p
))
15659 if (sub_strict_overflow_p
)
15660 *strict_overflow_p
= true;
15666 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15668 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15676 #define integer_valued_real_p(X) \
15677 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15679 #define RECURSE(X) \
15680 ((integer_valued_real_p) (X, depth + 1))
15682 /* Return true if the floating point result of (CODE OP0) has an
15683 integer value. We also allow +Inf, -Inf and NaN to be considered
15684 integer values. Return false for signaling NaN.
15686 DEPTH is the current nesting depth of the query. */
15689 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15697 return RECURSE (op0
);
15701 tree type
= TREE_TYPE (op0
);
15702 if (TREE_CODE (type
) == INTEGER_TYPE
)
15704 if (SCALAR_FLOAT_TYPE_P (type
))
15705 return RECURSE (op0
);
15715 /* Return true if the floating point result of (CODE OP0 OP1) has an
15716 integer value. We also allow +Inf, -Inf and NaN to be considered
15717 integer values. Return false for signaling NaN.
15719 DEPTH is the current nesting depth of the query. */
15722 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15731 return RECURSE (op0
) && RECURSE (op1
);
15739 /* Return true if the floating point result of calling FNDECL with arguments
15740 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15741 considered integer values. Return false for signaling NaN. If FNDECL
15742 takes fewer than 2 arguments, the remaining ARGn are null.
15744 DEPTH is the current nesting depth of the query. */
15747 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15755 CASE_CFN_NEARBYINT
:
15756 CASE_CFN_NEARBYINT_FN
:
15761 CASE_CFN_ROUNDEVEN
:
15762 CASE_CFN_ROUNDEVEN_FN
:
15771 return RECURSE (arg0
) && RECURSE (arg1
);
15779 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15780 has an integer value. We also allow +Inf, -Inf and NaN to be
15781 considered integer values. Return false for signaling NaN.
15783 DEPTH is the current nesting depth of the query. */
15786 integer_valued_real_single_p (tree t
, int depth
)
15788 switch (TREE_CODE (t
))
15791 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15794 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15797 /* Limit the depth of recursion to avoid quadratic behavior.
15798 This is expected to catch almost all occurrences in practice.
15799 If this code misses important cases that unbounded recursion
15800 would not, passes that need this information could be revised
15801 to provide it through dataflow propagation. */
15802 return (!name_registered_for_update_p (t
)
15803 && depth
< param_max_ssa_name_query_depth
15804 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15813 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15814 has an integer value. We also allow +Inf, -Inf and NaN to be
15815 considered integer values. Return false for signaling NaN.
15817 DEPTH is the current nesting depth of the query. */
15820 integer_valued_real_invalid_p (tree t
, int depth
)
15822 switch (TREE_CODE (t
))
15824 case COMPOUND_EXPR
:
15827 return RECURSE (TREE_OPERAND (t
, 1));
15830 return RECURSE (TREE_OPERAND (t
, 0));
15839 #undef integer_valued_real_p
15841 /* Return true if the floating point expression T has an integer value.
15842 We also allow +Inf, -Inf and NaN to be considered integer values.
15843 Return false for signaling NaN.
15845 DEPTH is the current nesting depth of the query. */
15848 integer_valued_real_p (tree t
, int depth
)
15850 if (t
== error_mark_node
)
15853 STRIP_ANY_LOCATION_WRAPPER (t
);
15855 tree_code code
= TREE_CODE (t
);
15856 switch (TREE_CODE_CLASS (code
))
15859 case tcc_comparison
:
15860 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15861 TREE_OPERAND (t
, 1), depth
);
15864 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15867 case tcc_declaration
:
15868 case tcc_reference
:
15869 return integer_valued_real_single_p (t
, depth
);
15879 return integer_valued_real_single_p (t
, depth
);
15883 tree arg0
= (call_expr_nargs (t
) > 0
15884 ? CALL_EXPR_ARG (t
, 0)
15886 tree arg1
= (call_expr_nargs (t
) > 1
15887 ? CALL_EXPR_ARG (t
, 1)
15889 return integer_valued_real_call_p (get_call_combined_fn (t
),
15890 arg0
, arg1
, depth
);
15894 return integer_valued_real_invalid_p (t
, depth
);
15898 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15899 attempt to fold the expression to a constant without modifying TYPE,
15902 If the expression could be simplified to a constant, then return
15903 the constant. If the expression would not be simplified to a
15904 constant, then return NULL_TREE. */
15907 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15909 tree tem
= fold_binary (code
, type
, op0
, op1
);
15910 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15913 /* Given the components of a unary expression CODE, TYPE and OP0,
15914 attempt to fold the expression to a constant without modifying
15917 If the expression could be simplified to a constant, then return
15918 the constant. If the expression would not be simplified to a
15919 constant, then return NULL_TREE. */
15922 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15924 tree tem
= fold_unary (code
, type
, op0
);
15925 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15928 /* If EXP represents referencing an element in a constant string
15929 (either via pointer arithmetic or array indexing), return the
15930 tree representing the value accessed, otherwise return NULL. */
15933 fold_read_from_constant_string (tree exp
)
15935 if ((INDIRECT_REF_P (exp
)
15936 || TREE_CODE (exp
) == ARRAY_REF
)
15937 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15939 tree exp1
= TREE_OPERAND (exp
, 0);
15942 location_t loc
= EXPR_LOCATION (exp
);
15944 if (INDIRECT_REF_P (exp
))
15945 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15948 tree low_bound
= array_ref_low_bound (exp
);
15949 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15951 /* Optimize the special-case of a zero lower bound.
15953 We convert the low_bound to sizetype to avoid some problems
15954 with constant folding. (E.g. suppose the lower bound is 1,
15955 and its mode is QI. Without the conversion,l (ARRAY
15956 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15957 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15958 if (! integer_zerop (low_bound
))
15959 index
= size_diffop_loc (loc
, index
,
15960 fold_convert_loc (loc
, sizetype
, low_bound
));
15965 scalar_int_mode char_mode
;
15967 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15968 && TREE_CODE (string
) == STRING_CST
15969 && tree_fits_uhwi_p (index
)
15970 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15971 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15973 && GET_MODE_SIZE (char_mode
) == 1)
15974 return build_int_cst_type (TREE_TYPE (exp
),
15975 (TREE_STRING_POINTER (string
)
15976 [TREE_INT_CST_LOW (index
)]));
15981 /* Folds a read from vector element at IDX of vector ARG. */
15984 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15986 unsigned HOST_WIDE_INT i
;
15987 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15988 && known_ge (idx
, 0u)
15989 && idx
.is_constant (&i
))
15991 if (TREE_CODE (arg
) == VECTOR_CST
)
15992 return VECTOR_CST_ELT (arg
, i
);
15993 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15995 if (CONSTRUCTOR_NELTS (arg
)
15996 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg
, 0)->value
)))
15998 if (i
>= CONSTRUCTOR_NELTS (arg
))
15999 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
16000 return CONSTRUCTOR_ELT (arg
, i
)->value
;
16006 /* Return the tree for neg (ARG0) when ARG0 is known to be either
16007 an integer constant, real, or fixed-point constant.
16009 TYPE is the type of the result. */
16012 fold_negate_const (tree arg0
, tree type
)
16014 tree t
= NULL_TREE
;
16016 switch (TREE_CODE (arg0
))
16019 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16024 FIXED_VALUE_TYPE f
;
16025 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
16026 &(TREE_FIXED_CST (arg0
)), NULL
,
16027 TYPE_SATURATING (type
));
16028 t
= build_fixed (type
, f
);
16029 /* Propagate overflow flags. */
16030 if (overflow_p
| TREE_OVERFLOW (arg0
))
16031 TREE_OVERFLOW (t
) = 1;
16036 if (poly_int_tree_p (arg0
))
16038 wi::overflow_type overflow
;
16039 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
16040 t
= force_fit_type (type
, res
, 1,
16041 (overflow
&& ! TYPE_UNSIGNED (type
))
16042 || TREE_OVERFLOW (arg0
));
16046 gcc_unreachable ();
16052 /* Return the tree for abs (ARG0) when ARG0 is known to be either
16053 an integer constant or real constant.
16055 TYPE is the type of the result. */
16058 fold_abs_const (tree arg0
, tree type
)
16060 tree t
= NULL_TREE
;
16062 switch (TREE_CODE (arg0
))
16066 /* If the value is unsigned or non-negative, then the absolute value
16067 is the same as the ordinary value. */
16068 wide_int val
= wi::to_wide (arg0
);
16069 wi::overflow_type overflow
= wi::OVF_NONE
;
16070 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
16073 /* If the value is negative, then the absolute value is
16076 val
= wi::neg (val
, &overflow
);
16078 /* Force to the destination type, set TREE_OVERFLOW for signed
16080 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
16085 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
16086 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16092 gcc_unreachable ();
16098 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
16099 constant. TYPE is the type of the result. */
16102 fold_not_const (const_tree arg0
, tree type
)
16104 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
16106 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
16109 /* Given CODE, a relational operator, the target type, TYPE and two
16110 constant operands OP0 and OP1, return the result of the
16111 relational operation. If the result is not a compile time
16112 constant, then return NULL_TREE. */
16115 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
16117 int result
, invert
;
16119 /* From here on, the only cases we handle are when the result is
16120 known to be a constant. */
16122 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
16124 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
16125 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
16127 /* Handle the cases where either operand is a NaN. */
16128 if (real_isnan (c0
) || real_isnan (c1
))
16138 case UNORDERED_EXPR
:
16152 if (flag_trapping_math
)
16158 gcc_unreachable ();
16161 return constant_boolean_node (result
, type
);
16164 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
16167 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
16169 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
16170 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
16171 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
16174 /* Handle equality/inequality of complex constants. */
16175 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
16177 tree rcond
= fold_relational_const (code
, type
,
16178 TREE_REALPART (op0
),
16179 TREE_REALPART (op1
));
16180 tree icond
= fold_relational_const (code
, type
,
16181 TREE_IMAGPART (op0
),
16182 TREE_IMAGPART (op1
));
16183 if (code
== EQ_EXPR
)
16184 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
16185 else if (code
== NE_EXPR
)
16186 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
16191 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
16193 if (!VECTOR_TYPE_P (type
))
16195 /* Have vector comparison with scalar boolean result. */
16196 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
16197 && known_eq (VECTOR_CST_NELTS (op0
),
16198 VECTOR_CST_NELTS (op1
)));
16199 unsigned HOST_WIDE_INT nunits
;
16200 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
16202 for (unsigned i
= 0; i
< nunits
; i
++)
16204 tree elem0
= VECTOR_CST_ELT (op0
, i
);
16205 tree elem1
= VECTOR_CST_ELT (op1
, i
);
16206 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
16207 if (tmp
== NULL_TREE
)
16209 if (integer_zerop (tmp
))
16210 return constant_boolean_node (code
== NE_EXPR
, type
);
16212 return constant_boolean_node (code
== EQ_EXPR
, type
);
16214 tree_vector_builder elts
;
16215 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
16217 unsigned int count
= elts
.encoded_nelts ();
16218 for (unsigned i
= 0; i
< count
; i
++)
16220 tree elem_type
= TREE_TYPE (type
);
16221 tree elem0
= VECTOR_CST_ELT (op0
, i
);
16222 tree elem1
= VECTOR_CST_ELT (op1
, i
);
16224 tree tem
= fold_relational_const (code
, elem_type
,
16227 if (tem
== NULL_TREE
)
16230 elts
.quick_push (build_int_cst (elem_type
,
16231 integer_zerop (tem
) ? 0 : -1));
16234 return elts
.build ();
16237 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16239 To compute GT, swap the arguments and do LT.
16240 To compute GE, do LT and invert the result.
16241 To compute LE, swap the arguments, do LT and invert the result.
16242 To compute NE, do EQ and invert the result.
16244 Therefore, the code below must handle only EQ and LT. */
16246 if (code
== LE_EXPR
|| code
== GT_EXPR
)
16248 std::swap (op0
, op1
);
16249 code
= swap_tree_comparison (code
);
16252 /* Note that it is safe to invert for real values here because we
16253 have already handled the one case that it matters. */
16256 if (code
== NE_EXPR
|| code
== GE_EXPR
)
16259 code
= invert_tree_comparison (code
, false);
16262 /* Compute a result for LT or EQ if args permit;
16263 Otherwise return T. */
16264 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
16266 if (code
== EQ_EXPR
)
16267 result
= tree_int_cst_equal (op0
, op1
);
16269 result
= tree_int_cst_lt (op0
, op1
);
16276 return constant_boolean_node (result
, type
);
16279 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16280 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16284 fold_build_cleanup_point_expr (tree type
, tree expr
)
16286 /* If the expression does not have side effects then we don't have to wrap
16287 it with a cleanup point expression. */
16288 if (!TREE_SIDE_EFFECTS (expr
))
16291 /* If the expression is a return, check to see if the expression inside the
16292 return has no side effects or the right hand side of the modify expression
16293 inside the return. If either don't have side effects set we don't need to
16294 wrap the expression in a cleanup point expression. Note we don't check the
16295 left hand side of the modify because it should always be a return decl. */
16296 if (TREE_CODE (expr
) == RETURN_EXPR
)
16298 tree op
= TREE_OPERAND (expr
, 0);
16299 if (!op
|| !TREE_SIDE_EFFECTS (op
))
16301 op
= TREE_OPERAND (op
, 1);
16302 if (!TREE_SIDE_EFFECTS (op
))
16306 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
16309 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16310 of an indirection through OP0, or NULL_TREE if no simplification is
16314 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
16318 poly_uint64 const_op01
;
16321 subtype
= TREE_TYPE (sub
);
16322 if (!POINTER_TYPE_P (subtype
)
16323 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
16326 if (TREE_CODE (sub
) == ADDR_EXPR
)
16328 tree op
= TREE_OPERAND (sub
, 0);
16329 tree optype
= TREE_TYPE (op
);
16331 /* *&CONST_DECL -> to the value of the const decl. */
16332 if (TREE_CODE (op
) == CONST_DECL
)
16333 return DECL_INITIAL (op
);
16334 /* *&p => p; make sure to handle *&"str"[cst] here. */
16335 if (type
== optype
)
16337 tree fop
= fold_read_from_constant_string (op
);
16343 /* *(foo *)&fooarray => fooarray[0] */
16344 else if (TREE_CODE (optype
) == ARRAY_TYPE
16345 && type
== TREE_TYPE (optype
)
16346 && (!in_gimple_form
16347 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16349 tree type_domain
= TYPE_DOMAIN (optype
);
16350 tree min_val
= size_zero_node
;
16351 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16352 min_val
= TYPE_MIN_VALUE (type_domain
);
16354 && TREE_CODE (min_val
) != INTEGER_CST
)
16356 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
16357 NULL_TREE
, NULL_TREE
);
16359 /* *(foo *)&complexfoo => __real__ complexfoo */
16360 else if (TREE_CODE (optype
) == COMPLEX_TYPE
16361 && type
== TREE_TYPE (optype
))
16362 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
16363 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16364 else if (VECTOR_TYPE_P (optype
)
16365 && type
== TREE_TYPE (optype
))
16367 tree part_width
= TYPE_SIZE (type
);
16368 tree index
= bitsize_int (0);
16369 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
16374 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16375 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
16377 tree op00
= TREE_OPERAND (sub
, 0);
16378 tree op01
= TREE_OPERAND (sub
, 1);
16381 if (TREE_CODE (op00
) == ADDR_EXPR
)
16384 op00
= TREE_OPERAND (op00
, 0);
16385 op00type
= TREE_TYPE (op00
);
16387 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16388 if (VECTOR_TYPE_P (op00type
)
16389 && type
== TREE_TYPE (op00type
)
16390 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16391 but we want to treat offsets with MSB set as negative.
16392 For the code below negative offsets are invalid and
16393 TYPE_SIZE of the element is something unsigned, so
16394 check whether op01 fits into poly_int64, which implies
16395 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16396 then just use poly_uint64 because we want to treat the
16397 value as unsigned. */
16398 && tree_fits_poly_int64_p (op01
))
16400 tree part_width
= TYPE_SIZE (type
);
16401 poly_uint64 max_offset
16402 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
16403 * TYPE_VECTOR_SUBPARTS (op00type
));
16404 if (known_lt (const_op01
, max_offset
))
16406 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
16407 return fold_build3_loc (loc
,
16408 BIT_FIELD_REF
, type
, op00
,
16409 part_width
, index
);
16412 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16413 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16414 && type
== TREE_TYPE (op00type
))
16416 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
16418 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16420 /* ((foo *)&fooarray)[1] => fooarray[1] */
16421 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16422 && type
== TREE_TYPE (op00type
))
16424 tree type_domain
= TYPE_DOMAIN (op00type
);
16425 tree min_val
= size_zero_node
;
16426 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16427 min_val
= TYPE_MIN_VALUE (type_domain
);
16428 poly_uint64 type_size
, index
;
16429 if (poly_int_tree_p (min_val
)
16430 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
16431 && multiple_p (const_op01
, type_size
, &index
))
16433 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
16434 op01
= wide_int_to_tree (sizetype
, off
);
16435 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16436 NULL_TREE
, NULL_TREE
);
16442 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16443 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16444 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16445 && (!in_gimple_form
16446 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16449 tree min_val
= size_zero_node
;
16450 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16451 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16452 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16453 min_val
= TYPE_MIN_VALUE (type_domain
);
16455 && TREE_CODE (min_val
) != INTEGER_CST
)
16457 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16464 /* Builds an expression for an indirection through T, simplifying some
16468 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16470 tree type
= TREE_TYPE (TREE_TYPE (t
));
16471 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16476 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16479 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16482 fold_indirect_ref_loc (location_t loc
, tree t
)
16484 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16492 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16493 whose result is ignored. The type of the returned tree need not be
16494 the same as the original expression. */
16497 fold_ignored_result (tree t
)
16499 if (!TREE_SIDE_EFFECTS (t
))
16500 return integer_zero_node
;
16503 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16506 t
= TREE_OPERAND (t
, 0);
16510 case tcc_comparison
:
16511 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16512 t
= TREE_OPERAND (t
, 0);
16513 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16514 t
= TREE_OPERAND (t
, 1);
16519 case tcc_expression
:
16520 switch (TREE_CODE (t
))
16522 case COMPOUND_EXPR
:
16523 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16525 t
= TREE_OPERAND (t
, 0);
16529 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16530 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16532 t
= TREE_OPERAND (t
, 0);
16545 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16548 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16550 tree div
= NULL_TREE
;
16555 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16556 have to do anything. Only do this when we are not given a const,
16557 because in that case, this check is more expensive than just
16559 if (TREE_CODE (value
) != INTEGER_CST
)
16561 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16563 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16567 /* If divisor is a power of two, simplify this to bit manipulation. */
16568 if (pow2_or_zerop (divisor
))
16570 if (TREE_CODE (value
) == INTEGER_CST
)
16572 wide_int val
= wi::to_wide (value
);
16575 if ((val
& (divisor
- 1)) == 0)
16578 overflow_p
= TREE_OVERFLOW (value
);
16579 val
+= divisor
- 1;
16580 val
&= (int) -divisor
;
16584 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16590 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16591 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16592 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16593 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16599 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16600 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16601 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16607 /* Likewise, but round down. */
16610 round_down_loc (location_t loc
, tree value
, int divisor
)
16612 tree div
= NULL_TREE
;
16614 gcc_assert (divisor
> 0);
16618 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16619 have to do anything. Only do this when we are not given a const,
16620 because in that case, this check is more expensive than just
16622 if (TREE_CODE (value
) != INTEGER_CST
)
16624 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16626 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16630 /* If divisor is a power of two, simplify this to bit manipulation. */
16631 if (pow2_or_zerop (divisor
))
16635 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16636 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16641 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16642 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16643 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16649 /* Returns the pointer to the base of the object addressed by EXP and
16650 extracts the information about the offset of the access, storing it
16651 to PBITPOS and POFFSET. */
16654 split_address_to_core_and_offset (tree exp
,
16655 poly_int64
*pbitpos
, tree
*poffset
)
16659 int unsignedp
, reversep
, volatilep
;
16660 poly_int64 bitsize
;
16661 location_t loc
= EXPR_LOCATION (exp
);
16663 if (TREE_CODE (exp
) == SSA_NAME
)
16664 if (gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (exp
)))
16665 if (gimple_assign_rhs_code (def
) == ADDR_EXPR
)
16666 exp
= gimple_assign_rhs1 (def
);
16668 if (TREE_CODE (exp
) == ADDR_EXPR
)
16670 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16671 poffset
, &mode
, &unsignedp
, &reversep
,
16673 core
= build_fold_addr_expr_loc (loc
, core
);
16675 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16677 core
= TREE_OPERAND (exp
, 0);
16680 *poffset
= TREE_OPERAND (exp
, 1);
16681 if (poly_int_tree_p (*poffset
))
16683 poly_offset_int tem
16684 = wi::sext (wi::to_poly_offset (*poffset
),
16685 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16686 tem
<<= LOG2_BITS_PER_UNIT
;
16687 if (tem
.to_shwi (pbitpos
))
16688 *poffset
= NULL_TREE
;
16695 *poffset
= NULL_TREE
;
16701 /* Returns true if addresses of E1 and E2 differ by a constant, false
16702 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16705 ptr_difference_const (tree e1
, tree e2
, poly_int64
*diff
)
16708 poly_int64 bitpos1
, bitpos2
;
16709 tree toffset1
, toffset2
, tdiff
, type
;
16711 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16712 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16714 poly_int64 bytepos1
, bytepos2
;
16715 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16716 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16717 || !operand_equal_p (core1
, core2
, 0))
16720 if (toffset1
&& toffset2
)
16722 type
= TREE_TYPE (toffset1
);
16723 if (type
!= TREE_TYPE (toffset2
))
16724 toffset2
= fold_convert (type
, toffset2
);
16726 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16727 if (!cst_and_fits_in_hwi (tdiff
))
16730 *diff
= int_cst_value (tdiff
);
16732 else if (toffset1
|| toffset2
)
16734 /* If only one of the offsets is non-constant, the difference cannot
16741 *diff
+= bytepos1
- bytepos2
;
16745 /* Return OFF converted to a pointer offset type suitable as offset for
16746 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16748 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16750 if (ptrofftype_p (TREE_TYPE (off
)))
16752 return fold_convert_loc (loc
, sizetype
, off
);
16755 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16757 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16759 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16760 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16763 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16765 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16767 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16768 ptr
, size_int (off
));
16771 /* Return a pointer to a NUL-terminated string containing the sequence
16772 of bytes corresponding to the representation of the object referred to
16773 by SRC (or a subsequence of such bytes within it if SRC is a reference
16774 to an initialized constant array plus some constant offset).
16775 Set *STRSIZE the number of bytes in the constant sequence including
16776 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16777 where A is the array that stores the constant sequence that SRC points
16778 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16779 need not point to a string or even an array of characters but may point
16780 to an object of any type. */
16783 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16785 /* The offset into the array A storing the string, and A's byte size. */
16793 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16795 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16799 unsigned HOST_WIDE_INT offset
= 0;
16800 if (offset_node
!= NULL_TREE
)
16802 if (!tree_fits_uhwi_p (offset_node
))
16805 offset
= tree_to_uhwi (offset_node
);
16808 if (!tree_fits_uhwi_p (mem_size
))
16811 /* ARRAY_SIZE is the byte size of the array the constant sequence
16812 is stored in and equal to sizeof A. INIT_BYTES is the number
16813 of bytes in the constant sequence used to initialize the array,
16814 including any embedded NULs as well as the terminating NUL (for
16815 strings), but not including any trailing zeros/NULs past
16816 the terminating one appended implicitly to a string literal to
16817 zero out the remainder of the array it's stored in. For example,
16819 const char a[7] = "abc\0d";
16820 n = strlen (a + 1);
16821 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16822 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16823 is equal to strlen (A) + 1. */
16824 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16825 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16826 const char *string
= TREE_STRING_POINTER (src
);
16828 /* Ideally this would turn into a gcc_checking_assert over time. */
16829 if (init_bytes
> array_size
)
16830 init_bytes
= array_size
;
16832 if (init_bytes
== 0 || offset
>= array_size
)
16837 /* Compute and store the number of characters from the beginning
16838 of the substring at OFFSET to the end, including the terminating
16839 nul. Offsets past the initial length refer to null strings. */
16840 if (offset
< init_bytes
)
16841 *strsize
= init_bytes
- offset
;
16847 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16848 /* Support only properly NUL-terminated single byte strings. */
16849 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16851 if (string
[init_bytes
- 1] != '\0')
16855 return offset
< init_bytes
? string
+ offset
: "";
16858 /* Return a pointer to a NUL-terminated string corresponding to
16859 the expression STR referencing a constant string, possibly
16860 involving a constant offset. Return null if STR either doesn't
16861 reference a constant string or if it involves a nonconstant
16865 c_getstr (tree str
)
16867 return getbyterep (str
, NULL
);
16870 /* Given a tree T, compute which bits in T may be nonzero. */
16873 tree_nonzero_bits (const_tree t
)
16875 switch (TREE_CODE (t
))
16878 return wi::to_wide (t
);
16880 return get_nonzero_bits (t
);
16881 case NON_LVALUE_EXPR
:
16883 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16885 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16886 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16889 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16890 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16892 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16893 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16895 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16896 TYPE_PRECISION (TREE_TYPE (t
)),
16897 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16899 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16901 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16902 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16903 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16904 return wi::bit_or (nzbits1
, nzbits2
);
16908 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16910 tree type
= TREE_TYPE (t
);
16911 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16912 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16913 TYPE_PRECISION (type
));
16914 return wi::neg_p (arg1
)
16915 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16916 : wi::lshift (nzbits
, arg1
);
16920 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16922 tree type
= TREE_TYPE (t
);
16923 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16924 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16925 TYPE_PRECISION (type
));
16926 return wi::neg_p (arg1
)
16927 ? wi::lshift (nzbits
, -arg1
)
16928 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16935 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16938 /* Helper function for address compare simplifications in match.pd.
16939 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16940 TYPE is the type of comparison operands.
16941 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16942 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16943 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16944 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16945 and 2 if unknown. */
16948 address_compare (tree_code code
, tree type
, tree op0
, tree op1
,
16949 tree
&base0
, tree
&base1
, poly_int64
&off0
, poly_int64
&off1
,
16952 if (TREE_CODE (op0
) == SSA_NAME
)
16953 op0
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0
));
16954 if (TREE_CODE (op1
) == SSA_NAME
)
16955 op1
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1
));
16956 gcc_checking_assert (TREE_CODE (op0
) == ADDR_EXPR
);
16957 gcc_checking_assert (TREE_CODE (op1
) == ADDR_EXPR
);
16958 base0
= get_addr_base_and_unit_offset (TREE_OPERAND (op0
, 0), &off0
);
16959 base1
= get_addr_base_and_unit_offset (TREE_OPERAND (op1
, 0), &off1
);
16960 if (base0
&& TREE_CODE (base0
) == MEM_REF
)
16962 off0
+= mem_ref_offset (base0
).force_shwi ();
16963 base0
= TREE_OPERAND (base0
, 0);
16965 if (base1
&& TREE_CODE (base1
) == MEM_REF
)
16967 off1
+= mem_ref_offset (base1
).force_shwi ();
16968 base1
= TREE_OPERAND (base1
, 0);
16970 if (base0
== NULL_TREE
|| base1
== NULL_TREE
)
16974 /* Punt in GENERIC on variables with value expressions;
16975 the value expressions might point to fields/elements
16976 of other vars etc. */
16978 && ((VAR_P (base0
) && DECL_HAS_VALUE_EXPR_P (base0
))
16979 || (VAR_P (base1
) && DECL_HAS_VALUE_EXPR_P (base1
))))
16981 else if (decl_in_symtab_p (base0
) && decl_in_symtab_p (base1
))
16983 symtab_node
*node0
= symtab_node::get_create (base0
);
16984 symtab_node
*node1
= symtab_node::get_create (base1
);
16985 equal
= node0
->equal_address_to (node1
);
16987 else if ((DECL_P (base0
)
16988 || TREE_CODE (base0
) == SSA_NAME
16989 || TREE_CODE (base0
) == STRING_CST
)
16991 || TREE_CODE (base1
) == SSA_NAME
16992 || TREE_CODE (base1
) == STRING_CST
))
16993 equal
= (base0
== base1
);
16994 /* Assume different STRING_CSTs with the same content will be
16997 && TREE_CODE (base0
) == STRING_CST
16998 && TREE_CODE (base1
) == STRING_CST
16999 && TREE_STRING_LENGTH (base0
) == TREE_STRING_LENGTH (base1
)
17000 && memcmp (TREE_STRING_POINTER (base0
), TREE_STRING_POINTER (base1
),
17001 TREE_STRING_LENGTH (base0
)) == 0)
17005 if (code
== EQ_EXPR
17007 /* If the offsets are equal we can ignore overflow. */
17008 || known_eq (off0
, off1
)
17009 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
17010 /* Or if we compare using pointers to decls or strings. */
17011 || (POINTER_TYPE_P (type
)
17012 && (DECL_P (base0
) || TREE_CODE (base0
) == STRING_CST
)))
17018 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
17021 /* At this point we know (or assume) the two pointers point at
17022 different objects. */
17023 HOST_WIDE_INT ioff0
= -1, ioff1
= -1;
17024 off0
.is_constant (&ioff0
);
17025 off1
.is_constant (&ioff1
);
17026 /* Punt on non-zero offsets from functions. */
17027 if ((TREE_CODE (base0
) == FUNCTION_DECL
&& ioff0
)
17028 || (TREE_CODE (base1
) == FUNCTION_DECL
&& ioff1
))
17030 /* Or if the bases are neither decls nor string literals. */
17031 if (!DECL_P (base0
) && TREE_CODE (base0
) != STRING_CST
)
17033 if (!DECL_P (base1
) && TREE_CODE (base1
) != STRING_CST
)
17035 /* For initializers, assume addresses of different functions are
17037 if (folding_initializer
17038 && TREE_CODE (base0
) == FUNCTION_DECL
17039 && TREE_CODE (base1
) == FUNCTION_DECL
)
17042 /* Compute whether one address points to the start of one
17043 object and another one to the end of another one. */
17044 poly_int64 size0
= 0, size1
= 0;
17045 if (TREE_CODE (base0
) == STRING_CST
)
17047 if (ioff0
< 0 || ioff0
> TREE_STRING_LENGTH (base0
))
17050 size0
= TREE_STRING_LENGTH (base0
);
17052 else if (TREE_CODE (base0
) == FUNCTION_DECL
)
17056 tree sz0
= DECL_SIZE_UNIT (base0
);
17057 if (!tree_fits_poly_int64_p (sz0
))
17060 size0
= tree_to_poly_int64 (sz0
);
17062 if (TREE_CODE (base1
) == STRING_CST
)
17064 if (ioff1
< 0 || ioff1
> TREE_STRING_LENGTH (base1
))
17067 size1
= TREE_STRING_LENGTH (base1
);
17069 else if (TREE_CODE (base1
) == FUNCTION_DECL
)
17073 tree sz1
= DECL_SIZE_UNIT (base1
);
17074 if (!tree_fits_poly_int64_p (sz1
))
17077 size1
= tree_to_poly_int64 (sz1
);
17081 /* If one offset is pointing (or could be) to the beginning of one
17082 object and the other is pointing to one past the last byte of the
17083 other object, punt. */
17084 if (maybe_eq (off0
, 0) && maybe_eq (off1
, size1
))
17086 else if (maybe_eq (off1
, 0) && maybe_eq (off0
, size0
))
17088 /* If both offsets are the same, there are some cases we know that are
17089 ok. Either if we know they aren't zero, or if we know both sizes
17092 && known_eq (off0
, off1
)
17093 && (known_ne (off0
, 0)
17094 || (known_ne (size0
, 0) && known_ne (size1
, 0))))
17098 /* At this point, equal is 2 if either one or both pointers are out of
17099 bounds of their object, or one points to start of its object and the
17100 other points to end of its object. This is unspecified behavior
17101 e.g. in C++. Otherwise equal is 0. */
17102 if (folding_cxx_constexpr
&& equal
)
17105 /* When both pointers point to string literals, even when equal is 0,
17106 due to tail merging of string literals the pointers might be the same. */
17107 if (TREE_CODE (base0
) == STRING_CST
&& TREE_CODE (base1
) == STRING_CST
)
17111 || ioff0
> TREE_STRING_LENGTH (base0
)
17112 || ioff1
> TREE_STRING_LENGTH (base1
))
17115 /* If the bytes in the string literals starting at the pointers
17116 differ, the pointers need to be different. */
17117 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
,
17118 TREE_STRING_POINTER (base1
) + ioff1
,
17119 MIN (TREE_STRING_LENGTH (base0
) - ioff0
,
17120 TREE_STRING_LENGTH (base1
) - ioff1
)) == 0)
17122 HOST_WIDE_INT ioffmin
= MIN (ioff0
, ioff1
);
17123 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
- ioffmin
,
17124 TREE_STRING_POINTER (base1
) + ioff1
- ioffmin
,
17126 /* If even the bytes in the string literal before the
17127 pointers are the same, the string literals could be
17134 if (folding_cxx_constexpr
)
17137 /* If this is a pointer comparison, ignore for now even
17138 valid equalities where one pointer is the offset zero
17139 of one object and the other to one past end of another one. */
17140 if (!INTEGRAL_TYPE_P (type
))
17143 /* Assume that string literals can't be adjacent to variables
17144 (automatic or global). */
17145 if (TREE_CODE (base0
) == STRING_CST
|| TREE_CODE (base1
) == STRING_CST
)
17148 /* Assume that automatic variables can't be adjacent to global
17150 if (is_global_var (base0
) != is_global_var (base1
))
17156 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
17158 ctor_single_nonzero_element (const_tree t
)
17160 unsigned HOST_WIDE_INT idx
;
17161 constructor_elt
*ce
;
17162 tree elt
= NULL_TREE
;
17164 if (TREE_CODE (t
) != CONSTRUCTOR
)
17166 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
17167 if (!integer_zerop (ce
->value
) && !real_zerop (ce
->value
))
17178 namespace selftest
{
17180 /* Helper functions for writing tests of folding trees. */
17182 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17185 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
17188 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
17191 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17192 wrapping WRAPPED_EXPR. */
17195 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
17198 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
17199 ASSERT_NE (wrapped_expr
, result
);
17200 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
17201 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
17204 /* Verify that various arithmetic binary operations are folded
17208 test_arithmetic_folding ()
17210 tree type
= integer_type_node
;
17211 tree x
= create_tmp_var_raw (type
, "x");
17212 tree zero
= build_zero_cst (type
);
17213 tree one
= build_int_cst (type
, 1);
17216 /* 1 <-- (0 + 1) */
17217 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
17219 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
17222 /* (nonlvalue)x <-- (x + 0) */
17223 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
17227 /* 0 <-- (x - x) */
17228 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
17230 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
17233 /* Multiplication. */
17234 /* 0 <-- (x * 0) */
17235 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
17238 /* (nonlvalue)x <-- (x * 1) */
17239 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
17243 namespace test_fold_vec_perm_cst
{
17245 /* Build a VECTOR_CST corresponding to VMODE, and has
17246 encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17247 Fill it with randomized elements, using rand() % THRESHOLD. */
17250 build_vec_cst_rand (machine_mode vmode
, unsigned npatterns
,
17251 unsigned nelts_per_pattern
,
17252 int step
= 0, bool natural_stepped
= false,
17253 int threshold
= 100)
17255 tree inner_type
= lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vmode
), 1);
17256 tree vectype
= build_vector_type_for_mode (inner_type
, vmode
);
17257 tree_vector_builder
builder (vectype
, npatterns
, nelts_per_pattern
);
17259 // Fill a0 for each pattern
17260 for (unsigned i
= 0; i
< npatterns
; i
++)
17261 builder
.quick_push (build_int_cst (inner_type
, rand () % threshold
));
17263 if (nelts_per_pattern
== 1)
17264 return builder
.build ();
17266 // Fill a1 for each pattern
17267 for (unsigned i
= 0; i
< npatterns
; i
++)
17270 if (natural_stepped
)
17272 tree a0
= builder
[i
];
17273 wide_int a0_val
= wi::to_wide (a0
);
17274 wide_int a1_val
= a0_val
+ step
;
17275 a1
= wide_int_to_tree (inner_type
, a1_val
);
17278 a1
= build_int_cst (inner_type
, rand () % threshold
);
17279 builder
.quick_push (a1
);
17281 if (nelts_per_pattern
== 2)
17282 return builder
.build ();
17284 for (unsigned i
= npatterns
* 2; i
< npatterns
* nelts_per_pattern
; i
++)
17286 tree prev_elem
= builder
[i
- npatterns
];
17287 wide_int prev_elem_val
= wi::to_wide (prev_elem
);
17288 wide_int val
= prev_elem_val
+ step
;
17289 builder
.quick_push (wide_int_to_tree (inner_type
, val
));
17292 return builder
.build ();
17295 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17296 when result is VLA. */
17299 validate_res (unsigned npatterns
, unsigned nelts_per_pattern
,
17300 tree res
, tree
*expected_res
)
17302 /* Actual npatterns and encoded_elts in res may be less than expected due
17303 to canonicalization. */
17304 ASSERT_TRUE (res
!= NULL_TREE
);
17305 ASSERT_TRUE (VECTOR_CST_NPATTERNS (res
) <= npatterns
);
17306 ASSERT_TRUE (vector_cst_encoded_nelts (res
) <= npatterns
* nelts_per_pattern
);
17308 for (unsigned i
= 0; i
< npatterns
* nelts_per_pattern
; i
++)
17309 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res
, i
), expected_res
[i
], 0));
17312 /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17313 when the result is VLS. */
17316 validate_res_vls (tree res
, tree
*expected_res
, unsigned expected_nelts
)
17318 ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res
), expected_nelts
));
17319 for (unsigned i
= 0; i
< expected_nelts
; i
++)
17320 ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res
, i
), expected_res
[i
], 0));
17323 /* Helper routine to push multiple elements into BUILDER. */
17324 template<unsigned N
>
17325 static void builder_push_elems (vec_perm_builder
& builder
,
17326 poly_uint64 (&elems
)[N
])
17328 for (unsigned i
= 0; i
< N
; i
++)
17329 builder
.quick_push (elems
[i
]);
17332 #define ARG0(index) vector_cst_elt (arg0, index)
17333 #define ARG1(index) vector_cst_elt (arg1, index)
17335 /* Test cases where result is VNx4SI and input vectors are V4SI. */
17338 test_vnx4si_v4si (machine_mode vnx4si_mode
, machine_mode v4si_mode
)
17340 for (int i
= 0; i
< 10; i
++)
17343 sel = { 0, 4, 1, 5, ... }
17344 res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17346 tree arg0
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17347 tree arg1
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17350 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vnx4si_mode
), 1);
17351 tree res_type
= build_vector_type_for_mode (inner_type
, vnx4si_mode
);
17353 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17354 vec_perm_builder
builder (res_len
, 4, 1);
17355 poly_uint64 mask_elems
[] = { 0, 4, 1, 5 };
17356 builder_push_elems (builder
, mask_elems
);
17358 vec_perm_indices
sel (builder
, 2, res_len
);
17359 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17361 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17362 validate_res (4, 1, res
, expected_res
);
17365 /* Case 2: Same as case 1, but contains an out of bounds access which
17366 should wrap around.
17367 sel = {0, 8, 4, 12, ...} (4, 1)
17368 res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17370 tree arg0
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17371 tree arg1
= build_vec_cst_rand (v4si_mode
, 4, 1, 0);
17374 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (vnx4si_mode
), 1);
17375 tree res_type
= build_vector_type_for_mode (inner_type
, vnx4si_mode
);
17377 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17378 vec_perm_builder
builder (res_len
, 4, 1);
17379 poly_uint64 mask_elems
[] = { 0, 8, 4, 12 };
17380 builder_push_elems (builder
, mask_elems
);
17382 vec_perm_indices
sel (builder
, 2, res_len
);
17383 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17385 tree expected_res
[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17386 validate_res (4, 1, res
, expected_res
);
17391 /* Test cases where result is V4SI and input vectors are VNx4SI. */
17394 test_v4si_vnx4si (machine_mode v4si_mode
, machine_mode vnx4si_mode
)
17396 for (int i
= 0; i
< 10; i
++)
17399 sel = { 0, 1, 2, 3}
17400 res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17402 tree arg0
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17403 tree arg1
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17406 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (v4si_mode
), 1);
17407 tree res_type
= build_vector_type_for_mode (inner_type
, v4si_mode
);
17409 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17410 vec_perm_builder
builder (res_len
, 4, 1);
17411 poly_uint64 mask_elems
[] = {0, 1, 2, 3};
17412 builder_push_elems (builder
, mask_elems
);
17414 vec_perm_indices
sel (builder
, 2, res_len
);
17415 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
);
17417 tree expected_res
[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17418 validate_res_vls (res
, expected_res
, 4);
17421 /* Case 2: Same as Case 1, but crossing input vector.
17423 In this case,the index 4 is ambiguous since len = 4 + 4x.
17424 Since we cannot determine, which vector to choose from during
17425 compile time, should return NULL_TREE. */
17427 tree arg0
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17428 tree arg1
= build_vec_cst_rand (vnx4si_mode
, 4, 1);
17431 = lang_hooks
.types
.type_for_mode (GET_MODE_INNER (v4si_mode
), 1);
17432 tree res_type
= build_vector_type_for_mode (inner_type
, v4si_mode
);
17434 poly_uint64 res_len
= TYPE_VECTOR_SUBPARTS (res_type
);
17435 vec_perm_builder
builder (res_len
, 4, 1);
17436 poly_uint64 mask_elems
[] = {0, 2, 4, 6};
17437 builder_push_elems (builder
, mask_elems
);
17439 vec_perm_indices
sel (builder
, 2, res_len
);
17440 const char *reason
;
17441 tree res
= fold_vec_perm_cst (res_type
, arg0
, arg1
, sel
, &reason
);
17443 ASSERT_TRUE (res
== NULL_TREE
);
17444 ASSERT_TRUE (!strcmp (reason
, "cannot divide selector element by arg len"));
17449 /* Test all input vectors. */
17452 test_all_nunits (machine_mode vmode
)
17454 /* Test with 10 different inputs. */
17455 for (int i
= 0; i
< 10; i
++)
17457 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17458 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17459 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17461 /* Case 1: mask = {0, ...} // (1, 1)
17462 res = { arg0[0], ... } // (1, 1) */
17464 vec_perm_builder
builder (len
, 1, 1);
17465 builder
.quick_push (0);
17466 vec_perm_indices
sel (builder
, 2, len
);
17467 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17468 tree expected_res
[] = { ARG0(0) };
17469 validate_res (1, 1, res
, expected_res
);
17472 /* Case 2: mask = {len, ...} // (1, 1)
17473 res = { arg1[0], ... } // (1, 1) */
17475 vec_perm_builder
builder (len
, 1, 1);
17476 builder
.quick_push (len
);
17477 vec_perm_indices
sel (builder
, 2, len
);
17478 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17480 tree expected_res
[] = { ARG1(0) };
17481 validate_res (1, 1, res
, expected_res
);
17486 /* Test all vectors which contain at-least 2 elements. */
17489 test_nunits_min_2 (machine_mode vmode
)
17491 for (int i
= 0; i
< 10; i
++)
17493 /* Case 1: mask = { 0, len, ... } // (2, 1)
17494 res = { arg0[0], arg1[0], ... } // (2, 1) */
17496 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17497 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17498 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17500 vec_perm_builder
builder (len
, 2, 1);
17501 poly_uint64 mask_elems
[] = { 0, len
};
17502 builder_push_elems (builder
, mask_elems
);
17504 vec_perm_indices
sel (builder
, 2, len
);
17505 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17507 tree expected_res
[] = { ARG0(0), ARG1(0) };
17508 validate_res (2, 1, res
, expected_res
);
17511 /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17512 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17514 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17515 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17516 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17518 vec_perm_builder
builder (len
, 2, 2);
17519 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17520 builder_push_elems (builder
, mask_elems
);
17522 vec_perm_indices
sel (builder
, 2, len
);
17523 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17525 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17526 validate_res (2, 2, res
, expected_res
);
17529 /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17530 Test that the stepped sequence of the pattern selects from
17531 same input pattern. Since input vectors have npatterns = 2,
17532 and step (a2 - a1) = 1, step is not a multiple of npatterns
17533 in input vector. So return NULL_TREE. */
17535 tree arg0
= build_vec_cst_rand (vmode
, 2, 3, 1, true);
17536 tree arg1
= build_vec_cst_rand (vmode
, 2, 3, 1);
17537 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17539 vec_perm_builder
builder (len
, 1, 3);
17540 poly_uint64 mask_elems
[] = { 0, 0, 1 };
17541 builder_push_elems (builder
, mask_elems
);
17543 vec_perm_indices
sel (builder
, 2, len
);
17544 const char *reason
;
17545 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
,
17547 ASSERT_TRUE (res
== NULL_TREE
);
17548 ASSERT_TRUE (!strcmp (reason
, "step is not multiple of npatterns"));
17551 /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17552 Test that stepped sequence of the pattern selects from arg0.
17553 res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17555 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1, true);
17556 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17557 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17559 vec_perm_builder
builder (len
, 1, 3);
17560 poly_uint64 mask_elems
[] = { len
, 0, 1 };
17561 builder_push_elems (builder
, mask_elems
);
17563 vec_perm_indices
sel (builder
, 2, len
);
17564 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17566 tree expected_res
[] = { ARG1(0), ARG0(0), ARG0(1) };
17567 validate_res (1, 3, res
, expected_res
);
17570 /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17571 In this case ensure that arg has a natural stepped sequence
17572 to preserve arg's encoding.
17574 As a concrete example, consider:
17575 arg0: { -16, -9, -10, ... } // (1, 3)
17576 arg1: { -12, -5, -6, ... } // (1, 3)
17577 sel = { 0, len, len + 1, ... } // (1, 3)
17579 This will create res with following encoding:
17580 res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17581 = { -16, -12, -5, ... }
17583 The step in above encoding would be: (-5) - (-12) = 7
17584 And hence res[3] would be computed as -5 + 7 = 2.
17585 instead of arg1[2], ie, -6.
17586 Ensure that valid_mask_for_fold_vec_perm_cst returns false
17589 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17590 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17591 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17593 vec_perm_builder
builder (len
, 1, 3);
17594 poly_uint64 mask_elems
[] = { 0, len
, len
+1 };
17595 builder_push_elems (builder
, mask_elems
);
17597 vec_perm_indices
sel (builder
, 2, len
);
17598 const char *reason
;
17599 /* FIXME: It may happen that build_vec_cst_rand may build a natural
17600 stepped pattern, even if we didn't explicitly tell it to. So folding
17601 may not always fail, but if it does, ensure that's because arg1 does
17602 not have a natural stepped sequence (and not due to other reason) */
17603 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17604 if (res
== NULL_TREE
)
17605 ASSERT_TRUE (!strcmp (reason
, "not a natural stepped sequence"));
17608 /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17609 sequence and thus folding should be valid for this case. */
17611 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17612 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1, true);
17613 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17615 vec_perm_builder
builder (len
, 1, 3);
17616 poly_uint64 mask_elems
[] = { 0, len
, len
+1 };
17617 builder_push_elems (builder
, mask_elems
);
17619 vec_perm_indices
sel (builder
, 2, len
);
17620 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17622 tree expected_res
[] = { ARG0(0), ARG1(0), ARG1(1) };
17623 validate_res (1, 3, res
, expected_res
);
17628 /* Test all vectors which contain at-least 4 elements. */
17631 test_nunits_min_4 (machine_mode vmode
)
17633 for (int i
= 0; i
< 10; i
++)
17635 /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17636 res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17638 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17639 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17640 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17642 vec_perm_builder
builder (len
, 4, 1);
17643 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17644 builder_push_elems (builder
, mask_elems
);
17646 vec_perm_indices
sel (builder
, 2, len
);
17647 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17649 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17650 validate_res (4, 1, res
, expected_res
);
17653 /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17654 res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17656 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17657 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17658 poly_uint64 arg0_len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17660 vec_perm_builder
builder (arg0_len
, 1, 3);
17661 poly_uint64 mask_elems
[] = {0, 1, 2};
17662 builder_push_elems (builder
, mask_elems
);
17664 vec_perm_indices
sel (builder
, 2, arg0_len
);
17665 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17666 tree expected_res
[] = { ARG0(0), ARG0(1), ARG0(2) };
17667 validate_res (1, 3, res
, expected_res
);
17670 /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17671 res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17673 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17674 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17675 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17677 vec_perm_builder
builder (len
, 1, 3);
17678 poly_uint64 mask_elems
[] = {len
, len
+ 1, len
+ 2};
17679 builder_push_elems (builder
, mask_elems
);
17681 vec_perm_indices
sel (builder
, 2, len
);
17682 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17683 tree expected_res
[] = { ARG1(0), ARG1(1), ARG1(2) };
17684 validate_res (1, 3, res
, expected_res
);
17688 sel = { len, 0, 2, ... } // (1, 3)
17689 This should return NULL because we cross the input vectors.
17691 Let's assume len = C + Cx
17694 esel = arg0_len / sel_npatterns = C + Cx
17695 ae = 0 + (esel - 2) * S
17696 = 0 + (C + Cx - 2) * 2
17700 Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17701 Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17702 Since q1 != qe, we cross input vectors.
17703 So return NULL_TREE. */
17705 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 2);
17706 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 2);
17707 poly_uint64 arg0_len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17709 vec_perm_builder
builder (arg0_len
, 1, 3);
17710 poly_uint64 mask_elems
[] = { arg0_len
, 0, 2 };
17711 builder_push_elems (builder
, mask_elems
);
17713 vec_perm_indices
sel (builder
, 2, arg0_len
);
17714 const char *reason
;
17715 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17716 ASSERT_TRUE (res
== NULL_TREE
);
17717 ASSERT_TRUE (!strcmp (reason
, "crossed input vectors"));
17720 /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17721 mask = { 0, len, 1, len + 1, ...} // (2, 2)
17722 res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17724 Note that fold_vec_perm_cst will set
17725 res_npatterns = max(4, max(4, 2)) = 4
17726 However after canonicalizing, we will end up with shape (2, 2). */
17728 tree arg0
= build_vec_cst_rand (vmode
, 4, 1);
17729 tree arg1
= build_vec_cst_rand (vmode
, 4, 1);
17730 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17732 vec_perm_builder
builder (len
, 2, 2);
17733 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1 };
17734 builder_push_elems (builder
, mask_elems
);
17736 vec_perm_indices
sel (builder
, 2, len
);
17737 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17738 tree expected_res
[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17739 validate_res (2, 2, res
, expected_res
);
17742 /* Case 6: Test combination in sel, where one pattern is dup and other
17743 is stepped sequence.
17744 sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17745 res = { arg0[0], arg0[0], arg0[0],
17746 arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17748 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17749 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17750 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17752 vec_perm_builder
builder (len
, 2, 3);
17753 poly_uint64 mask_elems
[] = { 0, 0, 0, 1, 0, 2 };
17754 builder_push_elems (builder
, mask_elems
);
17756 vec_perm_indices
sel (builder
, 2, len
);
17757 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17759 tree expected_res
[] = { ARG0(0), ARG0(0), ARG0(0),
17760 ARG0(1), ARG0(0), ARG0(2) };
17761 validate_res (2, 3, res
, expected_res
);
17764 /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17765 when arg0, arg1 and sel have different number of patterns.
17766 arg0 is of shape (1, 1)
17767 arg1 is of shape (4, 1)
17768 sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17770 In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17772 step = (len+2) - (len+1) = 1
17773 arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17774 Since step is not a multiple of arg_npatterns,
17775 valid_mask_for_fold_vec_perm_cst should return false,
17776 and thus fold_vec_perm_cst should return NULL_TREE. */
17778 tree arg0
= build_vec_cst_rand (vmode
, 1, 1);
17779 tree arg1
= build_vec_cst_rand (vmode
, 4, 1);
17780 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17782 vec_perm_builder
builder (len
, 2, 3);
17783 poly_uint64 mask_elems
[] = { 0, len
, 1, len
+ 1, 2, len
+ 2 };
17784 builder_push_elems (builder
, mask_elems
);
17786 vec_perm_indices
sel (builder
, 2, len
);
17787 const char *reason
;
17788 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17790 ASSERT_TRUE (res
== NULL_TREE
);
17791 ASSERT_TRUE (!strcmp (reason
, "step is not multiple of npatterns"));
17796 /* Test all vectors which contain at-least 8 elements. */
17799 test_nunits_min_8 (machine_mode vmode
)
17801 for (int i
= 0; i
< 10; i
++)
17803 /* Case 1: sel_npatterns (4) > input npatterns (2)
17804 sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17805 res: { arg0[0], arg0[0], arg0[0], arg1[0],
17806 arg0[2], arg0[0], arg0[3], arg1[0],
17807 arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17809 tree arg0
= build_vec_cst_rand (vmode
, 2, 3, 2);
17810 tree arg1
= build_vec_cst_rand (vmode
, 2, 3, 2);
17811 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17813 vec_perm_builder
builder(len
, 4, 3);
17814 poly_uint64 mask_elems
[] = { 0, 0, 1, len
, 2, 0, 3, len
,
17816 builder_push_elems (builder
, mask_elems
);
17818 vec_perm_indices
sel (builder
, 2, len
);
17819 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
);
17821 tree expected_res
[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17822 ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17823 ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17824 validate_res (4, 3, res
, expected_res
);
17829 /* Test vectors for which nunits[0] <= 4. */
17832 test_nunits_max_4 (machine_mode vmode
)
17834 /* Case 1: mask = {0, 4, ...} // (1, 2)
17835 This should return NULL_TREE because the index 4 may choose
17836 from either arg0 or arg1 depending on vector length. */
17838 tree arg0
= build_vec_cst_rand (vmode
, 1, 3, 1);
17839 tree arg1
= build_vec_cst_rand (vmode
, 1, 3, 1);
17840 poly_uint64 len
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
17842 vec_perm_builder
builder (len
, 1, 2);
17843 poly_uint64 mask_elems
[] = {0, 4};
17844 builder_push_elems (builder
, mask_elems
);
17846 vec_perm_indices
sel (builder
, 2, len
);
17847 const char *reason
;
17848 tree res
= fold_vec_perm_cst (TREE_TYPE (arg0
), arg0
, arg1
, sel
, &reason
);
17849 ASSERT_TRUE (res
== NULL_TREE
);
17850 ASSERT_TRUE (reason
!= NULL
);
17851 ASSERT_TRUE (!strcmp (reason
, "cannot divide selector element by arg len"));
17858 /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17861 is_simple_vla_size (poly_uint64 size
)
17863 if (size
.is_constant ()
17864 || !pow2p_hwi (size
.coeffs
[0]))
17866 for (unsigned i
= 1; i
< ARRAY_SIZE (size
.coeffs
); ++i
)
17867 if (size
.coeffs
[i
] != (i
<= 1 ? size
.coeffs
[0] : 0))
17872 /* Execute fold_vec_perm_cst unit tests. */
17877 machine_mode vnx4si_mode
= E_VOIDmode
;
17878 machine_mode v4si_mode
= E_VOIDmode
;
17880 machine_mode vmode
;
17881 FOR_EACH_MODE_IN_CLASS (vmode
, MODE_VECTOR_INT
)
17883 /* Obtain modes corresponding to VNx4SI and V4SI,
17884 to call mixed mode tests below.
17885 FIXME: Is there a better way to do this ? */
17886 if (GET_MODE_INNER (vmode
) == SImode
)
17888 poly_uint64 nunits
= GET_MODE_NUNITS (vmode
);
17889 if (is_simple_vla_size (nunits
)
17890 && nunits
.coeffs
[0] == 4)
17891 vnx4si_mode
= vmode
;
17892 else if (known_eq (nunits
, poly_uint64 (4)))
17896 if (!is_simple_vla_size (GET_MODE_NUNITS (vmode
))
17897 || !targetm
.vector_mode_supported_p (vmode
))
17900 poly_uint64 nunits
= GET_MODE_NUNITS (vmode
);
17901 test_all_nunits (vmode
);
17902 if (nunits
.coeffs
[0] >= 2)
17903 test_nunits_min_2 (vmode
);
17904 if (nunits
.coeffs
[0] >= 4)
17905 test_nunits_min_4 (vmode
);
17906 if (nunits
.coeffs
[0] >= 8)
17907 test_nunits_min_8 (vmode
);
17909 if (nunits
.coeffs
[0] <= 4)
17910 test_nunits_max_4 (vmode
);
17913 if (vnx4si_mode
!= E_VOIDmode
&& v4si_mode
!= E_VOIDmode
17914 && targetm
.vector_mode_supported_p (vnx4si_mode
)
17915 && targetm
.vector_mode_supported_p (v4si_mode
))
17917 test_vnx4si_v4si (vnx4si_mode
, v4si_mode
);
17918 test_v4si_vnx4si (v4si_mode
, vnx4si_mode
);
17921 } // end of test_fold_vec_perm_cst namespace
17923 /* Verify that various binary operations on vectors are folded
17927 test_vector_folding ()
17929 tree inner_type
= integer_type_node
;
17930 tree type
= build_vector_type (inner_type
, 4);
17931 tree zero
= build_zero_cst (type
);
17932 tree one
= build_one_cst (type
);
17933 tree index
= build_index_vector (type
, 0, 1);
17935 /* Verify equality tests that return a scalar boolean result. */
17936 tree res_type
= boolean_type_node
;
17937 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
17938 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
17939 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
17940 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
17941 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
17942 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
17944 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
17946 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
17950 /* Verify folding of VEC_DUPLICATE_EXPRs. */
17953 test_vec_duplicate_folding ()
17955 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
17956 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
17957 /* This will be 1 if VEC_MODE isn't a vector mode. */
17958 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
17960 tree type
= build_vector_type (ssizetype
, nunits
);
17961 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
17962 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
17963 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
17966 /* Run all of the selftests within this file. */
17969 fold_const_cc_tests ()
17971 test_arithmetic_folding ();
17972 test_vector_folding ();
17973 test_vec_duplicate_folding ();
17974 test_fold_vec_perm_cst::test ();
17977 } // namespace selftest
17979 #endif /* CHECKING_P */