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. */
45 #include "coretypes.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
57 #include "diagnostic-core.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
64 #include "tree-iterator.h"
67 #include "langhooks.h"
72 #include "generic-match.h"
73 #include "gimple-iterator.h"
74 #include "gimple-fold.h"
75 #include "tree-into-ssa.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
80 #include "tree-ssanames.h"
82 #include "stringpool.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 #include "gimple-range.h"
89 /* Nonzero if we are folding constants inside an initializer or a C++
90 manifestly-constant-evaluated context; zero otherwise.
91 Should be used when folding in initializer enables additional
93 int folding_initializer
= 0;
95 /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
97 Should be used when certain constructs shouldn't be optimized
98 during folding in that context. */
99 bool folding_cxx_constexpr
= false;
101 /* The following constants represent a bit based encoding of GCC's
102 comparison operators. This encoding simplifies transformations
103 on relational comparison operators, such as AND and OR. */
104 enum comparison_code
{
123 static bool negate_expr_p (tree
);
124 static tree
negate_expr (tree
);
125 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
126 static enum comparison_code
comparison_to_compcode (enum tree_code
);
127 static enum tree_code
compcode_to_comparison (enum comparison_code
);
128 static bool twoval_comparison_p (tree
, tree
*, tree
*);
129 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
130 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
132 static bool simple_operand_p (const_tree
);
133 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
134 static tree
range_predecessor (tree
);
135 static tree
range_successor (tree
);
136 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
137 static tree
fold_cond_expr_with_comparison (location_t
, tree
, enum tree_code
,
138 tree
, tree
, tree
, tree
);
139 static tree
unextend (tree
, int, int, tree
);
140 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
141 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
142 static tree
fold_binary_op_with_conditional_arg (location_t
,
143 enum tree_code
, tree
,
146 static tree
fold_negate_const (tree
, tree
);
147 static tree
fold_not_const (const_tree
, tree
);
148 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
149 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
150 static tree
fold_view_convert_expr (tree
, tree
);
151 static tree
fold_negate_expr (location_t
, tree
);
154 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
155 Otherwise, return LOC. */
158 expr_location_or (tree t
, location_t loc
)
160 location_t tloc
= EXPR_LOCATION (t
);
161 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
164 /* Similar to protected_set_expr_location, but never modify x in place,
165 if location can and needs to be set, unshare it. */
168 protected_set_expr_location_unshare (tree x
, location_t loc
)
170 if (CAN_HAVE_LOCATION_P (x
)
171 && EXPR_LOCATION (x
) != loc
172 && !(TREE_CODE (x
) == SAVE_EXPR
173 || TREE_CODE (x
) == TARGET_EXPR
174 || TREE_CODE (x
) == BIND_EXPR
))
177 SET_EXPR_LOCATION (x
, loc
);
182 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
183 division and returns the quotient. Otherwise returns
187 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
191 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
193 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
198 /* This is nonzero if we should defer warnings about undefined
199 overflow. This facility exists because these warnings are a
200 special case. The code to estimate loop iterations does not want
201 to issue any warnings, since it works with expressions which do not
202 occur in user code. Various bits of cleanup code call fold(), but
203 only use the result if it has certain characteristics (e.g., is a
204 constant); that code only wants to issue a warning if the result is
207 static int fold_deferring_overflow_warnings
;
209 /* If a warning about undefined overflow is deferred, this is the
210 warning. Note that this may cause us to turn two warnings into
211 one, but that is fine since it is sufficient to only give one
212 warning per expression. */
214 static const char* fold_deferred_overflow_warning
;
216 /* If a warning about undefined overflow is deferred, this is the
217 level at which the warning should be emitted. */
219 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
221 /* Start deferring overflow warnings. We could use a stack here to
222 permit nested calls, but at present it is not necessary. */
225 fold_defer_overflow_warnings (void)
227 ++fold_deferring_overflow_warnings
;
230 /* Stop deferring overflow warnings. If there is a pending warning,
231 and ISSUE is true, then issue the warning if appropriate. STMT is
232 the statement with which the warning should be associated (used for
233 location information); STMT may be NULL. CODE is the level of the
234 warning--a warn_strict_overflow_code value. This function will use
235 the smaller of CODE and the deferred code when deciding whether to
236 issue the warning. CODE may be zero to mean to always use the
240 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
245 gcc_assert (fold_deferring_overflow_warnings
> 0);
246 --fold_deferring_overflow_warnings
;
247 if (fold_deferring_overflow_warnings
> 0)
249 if (fold_deferred_overflow_warning
!= NULL
251 && code
< (int) fold_deferred_overflow_code
)
252 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
256 warnmsg
= fold_deferred_overflow_warning
;
257 fold_deferred_overflow_warning
= NULL
;
259 if (!issue
|| warnmsg
== NULL
)
262 if (warning_suppressed_p (stmt
, OPT_Wstrict_overflow
))
265 /* Use the smallest code level when deciding to issue the
267 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
268 code
= fold_deferred_overflow_code
;
270 if (!issue_strict_overflow_warning (code
))
274 locus
= input_location
;
276 locus
= gimple_location (stmt
);
277 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
280 /* Stop deferring overflow warnings, ignoring any deferred
284 fold_undefer_and_ignore_overflow_warnings (void)
286 fold_undefer_overflow_warnings (false, NULL
, 0);
289 /* Whether we are deferring overflow warnings. */
292 fold_deferring_overflow_warnings_p (void)
294 return fold_deferring_overflow_warnings
> 0;
297 /* This is called when we fold something based on the fact that signed
298 overflow is undefined. */
301 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
303 if (fold_deferring_overflow_warnings
> 0)
305 if (fold_deferred_overflow_warning
== NULL
306 || wc
< fold_deferred_overflow_code
)
308 fold_deferred_overflow_warning
= gmsgid
;
309 fold_deferred_overflow_code
= wc
;
312 else if (issue_strict_overflow_warning (wc
))
313 warning (OPT_Wstrict_overflow
, gmsgid
);
316 /* Return true if the built-in mathematical function specified by CODE
317 is odd, i.e. -f(x) == f(-x). */
320 negate_mathfn_p (combined_fn fn
)
360 CASE_CFN_ROUNDEVEN_FN
:
378 CASE_CFN_NEARBYINT_FN
:
381 return !flag_rounding_math
;
389 /* Check whether we may negate an integer constant T without causing
393 may_negate_without_overflow_p (const_tree t
)
397 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
399 type
= TREE_TYPE (t
);
400 if (TYPE_UNSIGNED (type
))
403 return !wi::only_sign_bit_p (wi::to_wide (t
));
406 /* Determine whether an expression T can be cheaply negated using
407 the function negate_expr without introducing undefined overflow. */
410 negate_expr_p (tree t
)
417 type
= TREE_TYPE (t
);
420 switch (TREE_CODE (t
))
423 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
426 /* Check that -CST will not overflow type. */
427 return may_negate_without_overflow_p (t
);
429 return (INTEGRAL_TYPE_P (type
)
430 && TYPE_OVERFLOW_WRAPS (type
));
436 return !TYPE_OVERFLOW_SANITIZED (type
);
439 /* We want to canonicalize to positive real constants. Pretend
440 that only negative ones can be easily negated. */
441 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
444 return negate_expr_p (TREE_REALPART (t
))
445 && negate_expr_p (TREE_IMAGPART (t
));
449 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
452 /* Steps don't prevent negation. */
453 unsigned int count
= vector_cst_encoded_nelts (t
);
454 for (unsigned int i
= 0; i
< count
; ++i
)
455 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
462 return negate_expr_p (TREE_OPERAND (t
, 0))
463 && negate_expr_p (TREE_OPERAND (t
, 1));
466 return negate_expr_p (TREE_OPERAND (t
, 0));
469 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
)
470 || HONOR_SIGNED_ZEROS (type
)
471 || (ANY_INTEGRAL_TYPE_P (type
)
472 && ! TYPE_OVERFLOW_WRAPS (type
)))
474 /* -(A + B) -> (-B) - A. */
475 if (negate_expr_p (TREE_OPERAND (t
, 1)))
477 /* -(A + B) -> (-A) - B. */
478 return negate_expr_p (TREE_OPERAND (t
, 0));
481 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
482 return !HONOR_SIGN_DEPENDENT_ROUNDING (type
)
483 && !HONOR_SIGNED_ZEROS (type
)
484 && (! ANY_INTEGRAL_TYPE_P (type
)
485 || TYPE_OVERFLOW_WRAPS (type
));
488 if (TYPE_UNSIGNED (type
))
490 /* INT_MIN/n * n doesn't overflow while negating one operand it does
491 if n is a (negative) power of two. */
492 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
493 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
494 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
496 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
497 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
499 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
505 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t
))
506 return negate_expr_p (TREE_OPERAND (t
, 1))
507 || negate_expr_p (TREE_OPERAND (t
, 0));
513 if (TYPE_UNSIGNED (type
))
515 /* In general we can't negate A in A / B, because if A is INT_MIN and
516 B is not 1 we change the sign of the result. */
517 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
518 && negate_expr_p (TREE_OPERAND (t
, 0)))
520 /* In general we can't negate B in A / B, because if A is INT_MIN and
521 B is 1, we may turn this into INT_MIN / -1 which is undefined
522 and actually traps on some architectures. */
523 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
524 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
525 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
526 && ! integer_onep (TREE_OPERAND (t
, 1))))
527 return negate_expr_p (TREE_OPERAND (t
, 1));
531 /* Negate -((double)float) as (double)(-float). */
532 if (TREE_CODE (type
) == REAL_TYPE
)
534 tree tem
= strip_float_extensions (t
);
536 return negate_expr_p (tem
);
541 /* Negate -f(x) as f(-x). */
542 if (negate_mathfn_p (get_call_combined_fn (t
)))
543 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
547 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
548 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
550 tree op1
= TREE_OPERAND (t
, 1);
551 if (wi::to_wide (op1
) == element_precision (type
) - 1)
562 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
563 simplification is possible.
564 If negate_expr_p would return true for T, NULL_TREE will never be
568 fold_negate_expr_1 (location_t loc
, tree t
)
570 tree type
= TREE_TYPE (t
);
573 switch (TREE_CODE (t
))
575 /* Convert - (~A) to A + 1. */
577 if (INTEGRAL_TYPE_P (type
))
578 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
579 build_one_cst (type
));
583 tem
= fold_negate_const (t
, type
);
584 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
585 || (ANY_INTEGRAL_TYPE_P (type
)
586 && !TYPE_OVERFLOW_TRAPS (type
)
587 && TYPE_OVERFLOW_WRAPS (type
))
588 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
595 tem
= fold_negate_const (t
, type
);
600 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
601 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
603 return build_complex (type
, rpart
, ipart
);
609 tree_vector_builder elts
;
610 elts
.new_unary_operation (type
, t
, true);
611 unsigned int count
= elts
.encoded_nelts ();
612 for (unsigned int i
= 0; i
< count
; ++i
)
614 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
615 if (elt
== NULL_TREE
)
617 elts
.quick_push (elt
);
620 return elts
.build ();
624 if (negate_expr_p (t
))
625 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
626 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
627 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
631 if (negate_expr_p (t
))
632 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
633 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
637 if (!TYPE_OVERFLOW_SANITIZED (type
))
638 return TREE_OPERAND (t
, 0);
642 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
643 && !HONOR_SIGNED_ZEROS (type
))
645 /* -(A + B) -> (-B) - A. */
646 if (negate_expr_p (TREE_OPERAND (t
, 1)))
648 tem
= negate_expr (TREE_OPERAND (t
, 1));
649 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
650 tem
, TREE_OPERAND (t
, 0));
653 /* -(A + B) -> (-A) - B. */
654 if (negate_expr_p (TREE_OPERAND (t
, 0)))
656 tem
= negate_expr (TREE_OPERAND (t
, 0));
657 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
658 tem
, TREE_OPERAND (t
, 1));
664 /* - (A - B) -> B - A */
665 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
666 && !HONOR_SIGNED_ZEROS (type
))
667 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
668 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
672 if (TYPE_UNSIGNED (type
))
678 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
680 tem
= TREE_OPERAND (t
, 1);
681 if (negate_expr_p (tem
))
682 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
683 TREE_OPERAND (t
, 0), negate_expr (tem
));
684 tem
= TREE_OPERAND (t
, 0);
685 if (negate_expr_p (tem
))
686 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
687 negate_expr (tem
), TREE_OPERAND (t
, 1));
694 if (TYPE_UNSIGNED (type
))
696 /* In general we can't negate A in A / B, because if A is INT_MIN and
697 B is not 1 we change the sign of the result. */
698 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
699 && negate_expr_p (TREE_OPERAND (t
, 0)))
700 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
701 negate_expr (TREE_OPERAND (t
, 0)),
702 TREE_OPERAND (t
, 1));
703 /* In general we can't negate B in A / B, because if A is INT_MIN and
704 B is 1, we may turn this into INT_MIN / -1 which is undefined
705 and actually traps on some architectures. */
706 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
707 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
708 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
709 && ! integer_onep (TREE_OPERAND (t
, 1))))
710 && negate_expr_p (TREE_OPERAND (t
, 1)))
711 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
713 negate_expr (TREE_OPERAND (t
, 1)));
717 /* Convert -((double)float) into (double)(-float). */
718 if (TREE_CODE (type
) == REAL_TYPE
)
720 tem
= strip_float_extensions (t
);
721 if (tem
!= t
&& negate_expr_p (tem
))
722 return fold_convert_loc (loc
, type
, negate_expr (tem
));
727 /* Negate -f(x) as f(-x). */
728 if (negate_mathfn_p (get_call_combined_fn (t
))
729 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
733 fndecl
= get_callee_fndecl (t
);
734 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
735 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
740 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
741 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
743 tree op1
= TREE_OPERAND (t
, 1);
744 if (wi::to_wide (op1
) == element_precision (type
) - 1)
746 tree ntype
= TYPE_UNSIGNED (type
)
747 ? signed_type_for (type
)
748 : unsigned_type_for (type
);
749 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
750 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
751 return fold_convert_loc (loc
, type
, temp
);
763 /* A wrapper for fold_negate_expr_1. */
766 fold_negate_expr (location_t loc
, tree t
)
768 tree type
= TREE_TYPE (t
);
770 tree tem
= fold_negate_expr_1 (loc
, t
);
771 if (tem
== NULL_TREE
)
773 return fold_convert_loc (loc
, type
, tem
);
776 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
777 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
789 loc
= EXPR_LOCATION (t
);
790 type
= TREE_TYPE (t
);
793 tem
= fold_negate_expr (loc
, t
);
795 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
796 return fold_convert_loc (loc
, type
, tem
);
799 /* Split a tree IN into a constant, literal and variable parts that could be
800 combined with CODE to make IN. "constant" means an expression with
801 TREE_CONSTANT but that isn't an actual constant. CODE must be a
802 commutative arithmetic operation. Store the constant part into *CONP,
803 the literal in *LITP and return the variable part. If a part isn't
804 present, set it to null. If the tree does not decompose in this way,
805 return the entire tree as the variable part and the other parts as null.
807 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
808 case, we negate an operand that was subtracted. Except if it is a
809 literal for which we use *MINUS_LITP instead.
811 If NEGATE_P is true, we are negating all of IN, again except a literal
812 for which we use *MINUS_LITP instead. If a variable part is of pointer
813 type, it is negated after converting to TYPE. This prevents us from
814 generating illegal MINUS pointer expression. LOC is the location of
815 the converted variable part.
817 If IN is itself a literal or constant, return it as appropriate.
819 Note that we do not guarantee that any of the three values will be the
820 same type as IN, but they will have the same signedness and mode. */
823 split_tree (tree in
, tree type
, enum tree_code code
,
824 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
825 tree
*litp
, tree
*minus_litp
, int negate_p
)
834 /* Strip any conversions that don't change the machine mode or signedness. */
835 STRIP_SIGN_NOPS (in
);
837 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
838 || TREE_CODE (in
) == FIXED_CST
)
840 else if (TREE_CODE (in
) == code
841 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
842 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
843 /* We can associate addition and subtraction together (even
844 though the C standard doesn't say so) for integers because
845 the value is not affected. For reals, the value might be
846 affected, so we can't. */
847 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
848 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
849 || (code
== MINUS_EXPR
850 && (TREE_CODE (in
) == PLUS_EXPR
851 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
853 tree op0
= TREE_OPERAND (in
, 0);
854 tree op1
= TREE_OPERAND (in
, 1);
855 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
856 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
858 /* First see if either of the operands is a literal, then a constant. */
859 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
860 || TREE_CODE (op0
) == FIXED_CST
)
861 *litp
= op0
, op0
= 0;
862 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
863 || TREE_CODE (op1
) == FIXED_CST
)
864 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
866 if (op0
!= 0 && TREE_CONSTANT (op0
))
867 *conp
= op0
, op0
= 0;
868 else if (op1
!= 0 && TREE_CONSTANT (op1
))
869 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
871 /* If we haven't dealt with either operand, this is not a case we can
872 decompose. Otherwise, VAR is either of the ones remaining, if any. */
873 if (op0
!= 0 && op1
!= 0)
878 var
= op1
, neg_var_p
= neg1_p
;
880 /* Now do any needed negations. */
882 *minus_litp
= *litp
, *litp
= 0;
883 if (neg_conp_p
&& *conp
)
884 *minus_conp
= *conp
, *conp
= 0;
885 if (neg_var_p
&& var
)
886 *minus_varp
= var
, var
= 0;
888 else if (TREE_CONSTANT (in
))
890 else if (TREE_CODE (in
) == BIT_NOT_EXPR
891 && code
== PLUS_EXPR
)
893 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
894 when IN is constant. */
895 *litp
= build_minus_one_cst (type
);
896 *minus_varp
= TREE_OPERAND (in
, 0);
904 *minus_litp
= *litp
, *litp
= 0;
905 else if (*minus_litp
)
906 *litp
= *minus_litp
, *minus_litp
= 0;
908 *minus_conp
= *conp
, *conp
= 0;
909 else if (*minus_conp
)
910 *conp
= *minus_conp
, *minus_conp
= 0;
912 *minus_varp
= var
, var
= 0;
913 else if (*minus_varp
)
914 var
= *minus_varp
, *minus_varp
= 0;
918 && TREE_OVERFLOW_P (*litp
))
919 *litp
= drop_tree_overflow (*litp
);
921 && TREE_OVERFLOW_P (*minus_litp
))
922 *minus_litp
= drop_tree_overflow (*minus_litp
);
927 /* Re-associate trees split by the above function. T1 and T2 are
928 either expressions to associate or null. Return the new
929 expression, if any. LOC is the location of the new expression. If
930 we build an operation, do it in TYPE and with CODE. */
933 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
937 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
943 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
944 try to fold this since we will have infinite recursion. But do
945 deal with any NEGATE_EXPRs. */
946 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
947 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
948 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
950 if (code
== PLUS_EXPR
)
952 if (TREE_CODE (t1
) == NEGATE_EXPR
)
953 return build2_loc (loc
, MINUS_EXPR
, type
,
954 fold_convert_loc (loc
, type
, t2
),
955 fold_convert_loc (loc
, type
,
956 TREE_OPERAND (t1
, 0)));
957 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
958 return build2_loc (loc
, MINUS_EXPR
, type
,
959 fold_convert_loc (loc
, type
, t1
),
960 fold_convert_loc (loc
, type
,
961 TREE_OPERAND (t2
, 0)));
962 else if (integer_zerop (t2
))
963 return fold_convert_loc (loc
, type
, t1
);
965 else if (code
== MINUS_EXPR
)
967 if (integer_zerop (t2
))
968 return fold_convert_loc (loc
, type
, t1
);
971 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
972 fold_convert_loc (loc
, type
, t2
));
975 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
976 fold_convert_loc (loc
, type
, t2
));
979 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
980 for use in int_const_binop, size_binop and size_diffop. */
983 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
985 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
987 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
1002 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
1003 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
1004 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
1007 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1008 a new constant in RES. Return FALSE if we don't know how to
1009 evaluate CODE at compile-time. */
1012 wide_int_binop (wide_int
&res
,
1013 enum tree_code code
, const wide_int
&arg1
, const wide_int
&arg2
,
1014 signop sign
, wi::overflow_type
*overflow
)
1017 *overflow
= wi::OVF_NONE
;
1021 res
= wi::bit_or (arg1
, arg2
);
1025 res
= wi::bit_xor (arg1
, arg2
);
1029 res
= wi::bit_and (arg1
, arg2
);
1033 if (wi::neg_p (arg2
))
1035 res
= wi::lshift (arg1
, arg2
);
1039 if (wi::neg_p (arg2
))
1041 /* It's unclear from the C standard whether shifts can overflow.
1042 The following code ignores overflow; perhaps a C standard
1043 interpretation ruling is needed. */
1044 res
= wi::rshift (arg1
, arg2
, sign
);
1049 if (wi::neg_p (arg2
))
1052 if (code
== RROTATE_EXPR
)
1053 code
= LROTATE_EXPR
;
1055 code
= RROTATE_EXPR
;
1060 if (code
== RROTATE_EXPR
)
1061 res
= wi::rrotate (arg1
, tmp
);
1063 res
= wi::lrotate (arg1
, tmp
);
1067 res
= wi::add (arg1
, arg2
, sign
, overflow
);
1071 res
= wi::sub (arg1
, arg2
, sign
, overflow
);
1075 res
= wi::mul (arg1
, arg2
, sign
, overflow
);
1078 case MULT_HIGHPART_EXPR
:
1079 res
= wi::mul_high (arg1
, arg2
, sign
);
1082 case TRUNC_DIV_EXPR
:
1083 case EXACT_DIV_EXPR
:
1086 res
= wi::div_trunc (arg1
, arg2
, sign
, overflow
);
1089 case FLOOR_DIV_EXPR
:
1092 res
= wi::div_floor (arg1
, arg2
, sign
, overflow
);
1098 res
= wi::div_ceil (arg1
, arg2
, sign
, overflow
);
1101 case ROUND_DIV_EXPR
:
1104 res
= wi::div_round (arg1
, arg2
, sign
, overflow
);
1107 case TRUNC_MOD_EXPR
:
1110 res
= wi::mod_trunc (arg1
, arg2
, sign
, overflow
);
1113 case FLOOR_MOD_EXPR
:
1116 res
= wi::mod_floor (arg1
, arg2
, sign
, overflow
);
1122 res
= wi::mod_ceil (arg1
, arg2
, sign
, overflow
);
1125 case ROUND_MOD_EXPR
:
1128 res
= wi::mod_round (arg1
, arg2
, sign
, overflow
);
1132 res
= wi::min (arg1
, arg2
, sign
);
1136 res
= wi::max (arg1
, arg2
, sign
);
1145 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1146 produce a new constant in RES. Return FALSE if we don't know how
1147 to evaluate CODE at compile-time. */
1150 poly_int_binop (poly_wide_int
&res
, enum tree_code code
,
1151 const_tree arg1
, const_tree arg2
,
1152 signop sign
, wi::overflow_type
*overflow
)
1154 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1155 gcc_assert (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
));
1159 res
= wi::add (wi::to_poly_wide (arg1
),
1160 wi::to_poly_wide (arg2
), sign
, overflow
);
1164 res
= wi::sub (wi::to_poly_wide (arg1
),
1165 wi::to_poly_wide (arg2
), sign
, overflow
);
1169 if (TREE_CODE (arg2
) == INTEGER_CST
)
1170 res
= wi::mul (wi::to_poly_wide (arg1
),
1171 wi::to_wide (arg2
), sign
, overflow
);
1172 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1173 res
= wi::mul (wi::to_poly_wide (arg2
),
1174 wi::to_wide (arg1
), sign
, overflow
);
1180 if (TREE_CODE (arg2
) == INTEGER_CST
)
1181 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1187 if (TREE_CODE (arg2
) != INTEGER_CST
1188 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1199 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1200 produce a new constant. Return NULL_TREE if we don't know how to
1201 evaluate CODE at compile-time. */
1204 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1207 poly_wide_int poly_res
;
1208 tree type
= TREE_TYPE (arg1
);
1209 signop sign
= TYPE_SIGN (type
);
1210 wi::overflow_type overflow
= wi::OVF_NONE
;
1212 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1214 wide_int warg1
= wi::to_wide (arg1
), res
;
1215 wide_int warg2
= wi::to_wide (arg2
, TYPE_PRECISION (type
));
1216 if (!wide_int_binop (res
, code
, warg1
, warg2
, sign
, &overflow
))
1220 else if (!poly_int_tree_p (arg1
)
1221 || !poly_int_tree_p (arg2
)
1222 || !poly_int_binop (poly_res
, code
, arg1
, arg2
, sign
, &overflow
))
1224 return force_fit_type (type
, poly_res
, overflowable
,
1225 (((sign
== SIGNED
|| overflowable
== -1)
1227 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1230 /* Return true if binary operation OP distributes over addition in operand
1231 OPNO, with the other operand being held constant. OPNO counts from 1. */
1234 distributes_over_addition_p (tree_code op
, int opno
)
1251 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1252 constant. We assume ARG1 and ARG2 have the same data type, or at least
1253 are the same kind of constant and the same machine mode. Return zero if
1254 combining the constants is not allowed in the current operating mode. */
1257 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1259 /* Sanity check for the recursive cases. */
1266 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1268 if (code
== POINTER_PLUS_EXPR
)
1269 return int_const_binop (PLUS_EXPR
,
1270 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1272 return int_const_binop (code
, arg1
, arg2
);
1275 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1280 REAL_VALUE_TYPE value
;
1281 REAL_VALUE_TYPE result
;
1285 /* The following codes are handled by real_arithmetic. */
1300 d1
= TREE_REAL_CST (arg1
);
1301 d2
= TREE_REAL_CST (arg2
);
1303 type
= TREE_TYPE (arg1
);
1304 mode
= TYPE_MODE (type
);
1306 /* Don't perform operation if we honor signaling NaNs and
1307 either operand is a signaling NaN. */
1308 if (HONOR_SNANS (mode
)
1309 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1310 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1313 /* Don't perform operation if it would raise a division
1314 by zero exception. */
1315 if (code
== RDIV_EXPR
1316 && real_equal (&d2
, &dconst0
)
1317 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1320 /* If either operand is a NaN, just return it. Otherwise, set up
1321 for floating-point trap; we return an overflow. */
1322 if (REAL_VALUE_ISNAN (d1
))
1324 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1327 t
= build_real (type
, d1
);
1330 else if (REAL_VALUE_ISNAN (d2
))
1332 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1335 t
= build_real (type
, d2
);
1339 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1340 real_convert (&result
, mode
, &value
);
1342 /* Don't constant fold this floating point operation if
1343 both operands are not NaN but the result is NaN, and
1344 flag_trapping_math. Such operations should raise an
1345 invalid operation exception. */
1346 if (flag_trapping_math
1347 && MODE_HAS_NANS (mode
)
1348 && REAL_VALUE_ISNAN (result
)
1349 && !REAL_VALUE_ISNAN (d1
)
1350 && !REAL_VALUE_ISNAN (d2
))
1353 /* Don't constant fold this floating point operation if
1354 the result has overflowed and flag_trapping_math. */
1355 if (flag_trapping_math
1356 && MODE_HAS_INFINITIES (mode
)
1357 && REAL_VALUE_ISINF (result
)
1358 && !REAL_VALUE_ISINF (d1
)
1359 && !REAL_VALUE_ISINF (d2
))
1362 /* Don't constant fold this floating point operation if the
1363 result may dependent upon the run-time rounding mode and
1364 flag_rounding_math is set, or if GCC's software emulation
1365 is unable to accurately represent the result. */
1366 if ((flag_rounding_math
1367 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1368 && (inexact
|| !real_identical (&result
, &value
)))
1371 t
= build_real (type
, result
);
1373 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1377 if (TREE_CODE (arg1
) == FIXED_CST
)
1379 FIXED_VALUE_TYPE f1
;
1380 FIXED_VALUE_TYPE f2
;
1381 FIXED_VALUE_TYPE result
;
1386 /* The following codes are handled by fixed_arithmetic. */
1392 case TRUNC_DIV_EXPR
:
1393 if (TREE_CODE (arg2
) != FIXED_CST
)
1395 f2
= TREE_FIXED_CST (arg2
);
1401 if (TREE_CODE (arg2
) != INTEGER_CST
)
1403 wi::tree_to_wide_ref w2
= wi::to_wide (arg2
);
1404 f2
.data
.high
= w2
.elt (1);
1405 f2
.data
.low
= w2
.ulow ();
1414 f1
= TREE_FIXED_CST (arg1
);
1415 type
= TREE_TYPE (arg1
);
1416 sat_p
= TYPE_SATURATING (type
);
1417 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1418 t
= build_fixed (type
, result
);
1419 /* Propagate overflow flags. */
1420 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1421 TREE_OVERFLOW (t
) = 1;
1425 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1427 tree type
= TREE_TYPE (arg1
);
1428 tree r1
= TREE_REALPART (arg1
);
1429 tree i1
= TREE_IMAGPART (arg1
);
1430 tree r2
= TREE_REALPART (arg2
);
1431 tree i2
= TREE_IMAGPART (arg2
);
1438 real
= const_binop (code
, r1
, r2
);
1439 imag
= const_binop (code
, i1
, i2
);
1443 if (COMPLEX_FLOAT_TYPE_P (type
))
1444 return do_mpc_arg2 (arg1
, arg2
, type
,
1445 /* do_nonfinite= */ folding_initializer
,
1448 real
= const_binop (MINUS_EXPR
,
1449 const_binop (MULT_EXPR
, r1
, r2
),
1450 const_binop (MULT_EXPR
, i1
, i2
));
1451 imag
= const_binop (PLUS_EXPR
,
1452 const_binop (MULT_EXPR
, r1
, i2
),
1453 const_binop (MULT_EXPR
, i1
, r2
));
1457 if (COMPLEX_FLOAT_TYPE_P (type
))
1458 return do_mpc_arg2 (arg1
, arg2
, type
,
1459 /* do_nonfinite= */ folding_initializer
,
1462 case TRUNC_DIV_EXPR
:
1464 case FLOOR_DIV_EXPR
:
1465 case ROUND_DIV_EXPR
:
1466 if (flag_complex_method
== 0)
1468 /* Keep this algorithm in sync with
1469 tree-complex.cc:expand_complex_div_straight().
1471 Expand complex division to scalars, straightforward algorithm.
1472 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1476 = const_binop (PLUS_EXPR
,
1477 const_binop (MULT_EXPR
, r2
, r2
),
1478 const_binop (MULT_EXPR
, i2
, i2
));
1480 = const_binop (PLUS_EXPR
,
1481 const_binop (MULT_EXPR
, r1
, r2
),
1482 const_binop (MULT_EXPR
, i1
, i2
));
1484 = const_binop (MINUS_EXPR
,
1485 const_binop (MULT_EXPR
, i1
, r2
),
1486 const_binop (MULT_EXPR
, r1
, i2
));
1488 real
= const_binop (code
, t1
, magsquared
);
1489 imag
= const_binop (code
, t2
, magsquared
);
1493 /* Keep this algorithm in sync with
1494 tree-complex.cc:expand_complex_div_wide().
1496 Expand complex division to scalars, modified algorithm to minimize
1497 overflow with wide input ranges. */
1498 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1499 fold_abs_const (r2
, TREE_TYPE (type
)),
1500 fold_abs_const (i2
, TREE_TYPE (type
)));
1502 if (integer_nonzerop (compare
))
1504 /* In the TRUE branch, we compute
1506 div = (br * ratio) + bi;
1507 tr = (ar * ratio) + ai;
1508 ti = (ai * ratio) - ar;
1511 tree ratio
= const_binop (code
, r2
, i2
);
1512 tree div
= const_binop (PLUS_EXPR
, i2
,
1513 const_binop (MULT_EXPR
, r2
, ratio
));
1514 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1515 real
= const_binop (PLUS_EXPR
, real
, i1
);
1516 real
= const_binop (code
, real
, div
);
1518 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1519 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1520 imag
= const_binop (code
, imag
, div
);
1524 /* In the FALSE branch, we compute
1526 divisor = (d * ratio) + c;
1527 tr = (b * ratio) + a;
1528 ti = b - (a * ratio);
1531 tree ratio
= const_binop (code
, i2
, r2
);
1532 tree div
= const_binop (PLUS_EXPR
, r2
,
1533 const_binop (MULT_EXPR
, i2
, ratio
));
1535 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1536 real
= const_binop (PLUS_EXPR
, real
, r1
);
1537 real
= const_binop (code
, real
, div
);
1539 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1540 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1541 imag
= const_binop (code
, imag
, div
);
1551 return build_complex (type
, real
, imag
);
1554 if (TREE_CODE (arg1
) == VECTOR_CST
1555 && TREE_CODE (arg2
) == VECTOR_CST
1556 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)),
1557 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
))))
1559 tree type
= TREE_TYPE (arg1
);
1561 if (VECTOR_CST_STEPPED_P (arg1
)
1562 && VECTOR_CST_STEPPED_P (arg2
))
1563 /* We can operate directly on the encoding if:
1565 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1567 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1569 Addition and subtraction are the supported operators
1570 for which this is true. */
1571 step_ok_p
= (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
1572 else if (VECTOR_CST_STEPPED_P (arg1
))
1573 /* We can operate directly on stepped encodings if:
1577 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1579 which is true if (x -> x op c) distributes over addition. */
1580 step_ok_p
= distributes_over_addition_p (code
, 1);
1582 /* Similarly in reverse. */
1583 step_ok_p
= distributes_over_addition_p (code
, 2);
1584 tree_vector_builder elts
;
1585 if (!elts
.new_binary_operation (type
, arg1
, arg2
, step_ok_p
))
1587 unsigned int count
= elts
.encoded_nelts ();
1588 for (unsigned int i
= 0; i
< count
; ++i
)
1590 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1591 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1593 tree elt
= const_binop (code
, elem1
, elem2
);
1595 /* It is possible that const_binop cannot handle the given
1596 code and return NULL_TREE */
1597 if (elt
== NULL_TREE
)
1599 elts
.quick_push (elt
);
1602 return elts
.build ();
1605 /* Shifts allow a scalar offset for a vector. */
1606 if (TREE_CODE (arg1
) == VECTOR_CST
1607 && TREE_CODE (arg2
) == INTEGER_CST
)
1609 tree type
= TREE_TYPE (arg1
);
1610 bool step_ok_p
= distributes_over_addition_p (code
, 1);
1611 tree_vector_builder elts
;
1612 if (!elts
.new_unary_operation (type
, arg1
, step_ok_p
))
1614 unsigned int count
= elts
.encoded_nelts ();
1615 for (unsigned int i
= 0; i
< count
; ++i
)
1617 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1619 tree elt
= const_binop (code
, elem1
, arg2
);
1621 /* It is possible that const_binop cannot handle the given
1622 code and return NULL_TREE. */
1623 if (elt
== NULL_TREE
)
1625 elts
.quick_push (elt
);
1628 return elts
.build ();
1633 /* Overload that adds a TYPE parameter to be able to dispatch
1634 to fold_relational_const. */
1637 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1639 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1640 return fold_relational_const (code
, type
, arg1
, arg2
);
1642 /* ??? Until we make the const_binop worker take the type of the
1643 result as argument put those cases that need it here. */
1646 case VEC_SERIES_EXPR
:
1647 if (CONSTANT_CLASS_P (arg1
)
1648 && CONSTANT_CLASS_P (arg2
))
1649 return build_vec_series (type
, arg1
, arg2
);
1653 if ((TREE_CODE (arg1
) == REAL_CST
1654 && TREE_CODE (arg2
) == REAL_CST
)
1655 || (TREE_CODE (arg1
) == INTEGER_CST
1656 && TREE_CODE (arg2
) == INTEGER_CST
))
1657 return build_complex (type
, arg1
, arg2
);
1660 case POINTER_DIFF_EXPR
:
1661 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1663 poly_offset_int res
= (wi::to_poly_offset (arg1
)
1664 - wi::to_poly_offset (arg2
));
1665 return force_fit_type (type
, res
, 1,
1666 TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1670 case VEC_PACK_TRUNC_EXPR
:
1671 case VEC_PACK_FIX_TRUNC_EXPR
:
1672 case VEC_PACK_FLOAT_EXPR
:
1674 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1676 if (TREE_CODE (arg1
) != VECTOR_CST
1677 || TREE_CODE (arg2
) != VECTOR_CST
)
1680 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1683 out_nelts
= in_nelts
* 2;
1684 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1685 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1687 tree_vector_builder
elts (type
, out_nelts
, 1);
1688 for (i
= 0; i
< out_nelts
; i
++)
1690 tree elt
= (i
< in_nelts
1691 ? VECTOR_CST_ELT (arg1
, i
)
1692 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1693 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1695 : code
== VEC_PACK_FLOAT_EXPR
1696 ? FLOAT_EXPR
: FIX_TRUNC_EXPR
,
1697 TREE_TYPE (type
), elt
);
1698 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1700 elts
.quick_push (elt
);
1703 return elts
.build ();
1706 case VEC_WIDEN_MULT_LO_EXPR
:
1707 case VEC_WIDEN_MULT_HI_EXPR
:
1708 case VEC_WIDEN_MULT_EVEN_EXPR
:
1709 case VEC_WIDEN_MULT_ODD_EXPR
:
1711 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1713 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1716 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1718 out_nelts
= in_nelts
/ 2;
1719 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1720 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1722 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1723 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1724 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1725 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1726 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1728 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1731 tree_vector_builder
elts (type
, out_nelts
, 1);
1732 for (out
= 0; out
< out_nelts
; out
++)
1734 unsigned int in
= (out
<< scale
) + ofs
;
1735 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1736 VECTOR_CST_ELT (arg1
, in
));
1737 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1738 VECTOR_CST_ELT (arg2
, in
));
1740 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1742 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1743 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1745 elts
.quick_push (elt
);
1748 return elts
.build ();
1754 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1757 /* Make sure type and arg0 have the same saturating flag. */
1758 gcc_checking_assert (TYPE_SATURATING (type
)
1759 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1761 return const_binop (code
, arg1
, arg2
);
1764 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1765 Return zero if computing the constants is not possible. */
1768 const_unop (enum tree_code code
, tree type
, tree arg0
)
1770 /* Don't perform the operation, other than NEGATE and ABS, if
1771 flag_signaling_nans is on and the operand is a signaling NaN. */
1772 if (TREE_CODE (arg0
) == REAL_CST
1773 && HONOR_SNANS (arg0
)
1774 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1775 && code
!= NEGATE_EXPR
1777 && code
!= ABSU_EXPR
)
1784 case FIX_TRUNC_EXPR
:
1785 case FIXED_CONVERT_EXPR
:
1786 return fold_convert_const (code
, type
, arg0
);
1788 case ADDR_SPACE_CONVERT_EXPR
:
1789 /* If the source address is 0, and the source address space
1790 cannot have a valid object at 0, fold to dest type null. */
1791 if (integer_zerop (arg0
)
1792 && !(targetm
.addr_space
.zero_address_valid
1793 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1794 return fold_convert_const (code
, type
, arg0
);
1797 case VIEW_CONVERT_EXPR
:
1798 return fold_view_convert_expr (type
, arg0
);
1802 /* Can't call fold_negate_const directly here as that doesn't
1803 handle all cases and we might not be able to negate some
1805 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1806 if (tem
&& CONSTANT_CLASS_P (tem
))
1813 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1814 return fold_abs_const (arg0
, type
);
1818 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1820 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1822 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1827 if (TREE_CODE (arg0
) == INTEGER_CST
)
1828 return fold_not_const (arg0
, type
);
1829 else if (POLY_INT_CST_P (arg0
))
1830 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1831 /* Perform BIT_NOT_EXPR on each element individually. */
1832 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1836 /* This can cope with stepped encodings because ~x == -1 - x. */
1837 tree_vector_builder elements
;
1838 elements
.new_unary_operation (type
, arg0
, true);
1839 unsigned int i
, count
= elements
.encoded_nelts ();
1840 for (i
= 0; i
< count
; ++i
)
1842 elem
= VECTOR_CST_ELT (arg0
, i
);
1843 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1844 if (elem
== NULL_TREE
)
1846 elements
.quick_push (elem
);
1849 return elements
.build ();
1853 case TRUTH_NOT_EXPR
:
1854 if (TREE_CODE (arg0
) == INTEGER_CST
)
1855 return constant_boolean_node (integer_zerop (arg0
), type
);
1859 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1860 return fold_convert (type
, TREE_REALPART (arg0
));
1864 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1865 return fold_convert (type
, TREE_IMAGPART (arg0
));
1868 case VEC_UNPACK_LO_EXPR
:
1869 case VEC_UNPACK_HI_EXPR
:
1870 case VEC_UNPACK_FLOAT_LO_EXPR
:
1871 case VEC_UNPACK_FLOAT_HI_EXPR
:
1872 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
1873 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
1875 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1876 enum tree_code subcode
;
1878 if (TREE_CODE (arg0
) != VECTOR_CST
)
1881 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1883 out_nelts
= in_nelts
/ 2;
1884 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1886 unsigned int offset
= 0;
1887 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1888 || code
== VEC_UNPACK_FLOAT_LO_EXPR
1889 || code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
))
1892 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1894 else if (code
== VEC_UNPACK_FLOAT_LO_EXPR
1895 || code
== VEC_UNPACK_FLOAT_HI_EXPR
)
1896 subcode
= FLOAT_EXPR
;
1898 subcode
= FIX_TRUNC_EXPR
;
1900 tree_vector_builder
elts (type
, out_nelts
, 1);
1901 for (i
= 0; i
< out_nelts
; i
++)
1903 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1904 VECTOR_CST_ELT (arg0
, i
+ offset
));
1905 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1907 elts
.quick_push (elt
);
1910 return elts
.build ();
1913 case VEC_DUPLICATE_EXPR
:
1914 if (CONSTANT_CLASS_P (arg0
))
1915 return build_vector_from_val (type
, arg0
);
1925 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1926 indicates which particular sizetype to create. */
1929 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
1931 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1934 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1935 is a tree code. The type of the result is taken from the operands.
1936 Both must be equivalent integer types, ala int_binop_types_match_p.
1937 If the operands are constant, so is the result. */
1940 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1942 tree type
= TREE_TYPE (arg0
);
1944 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1945 return error_mark_node
;
1947 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1950 /* Handle the special case of two poly_int constants faster. */
1951 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
1953 /* And some specific cases even faster than that. */
1954 if (code
== PLUS_EXPR
)
1956 if (integer_zerop (arg0
)
1957 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1959 if (integer_zerop (arg1
)
1960 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1963 else if (code
== MINUS_EXPR
)
1965 if (integer_zerop (arg1
)
1966 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1969 else if (code
== MULT_EXPR
)
1971 if (integer_onep (arg0
)
1972 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1976 /* Handle general case of two integer constants. For sizetype
1977 constant calculations we always want to know about overflow,
1978 even in the unsigned case. */
1979 tree res
= int_const_binop (code
, arg0
, arg1
, -1);
1980 if (res
!= NULL_TREE
)
1984 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1987 /* Given two values, either both of sizetype or both of bitsizetype,
1988 compute the difference between the two values. Return the value
1989 in signed type corresponding to the type of the operands. */
1992 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1994 tree type
= TREE_TYPE (arg0
);
1997 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
2000 /* If the type is already signed, just do the simple thing. */
2001 if (!TYPE_UNSIGNED (type
))
2002 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
2004 if (type
== sizetype
)
2006 else if (type
== bitsizetype
)
2007 ctype
= sbitsizetype
;
2009 ctype
= signed_type_for (type
);
2011 /* If either operand is not a constant, do the conversions to the signed
2012 type and subtract. The hardware will do the right thing with any
2013 overflow in the subtraction. */
2014 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
2015 return size_binop_loc (loc
, MINUS_EXPR
,
2016 fold_convert_loc (loc
, ctype
, arg0
),
2017 fold_convert_loc (loc
, ctype
, arg1
));
2019 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2020 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2021 overflow) and negate (which can't either). Special-case a result
2022 of zero while we're here. */
2023 if (tree_int_cst_equal (arg0
, arg1
))
2024 return build_int_cst (ctype
, 0);
2025 else if (tree_int_cst_lt (arg1
, arg0
))
2026 return fold_convert_loc (loc
, ctype
,
2027 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
2029 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
2030 fold_convert_loc (loc
, ctype
,
2031 size_binop_loc (loc
,
2036 /* A subroutine of fold_convert_const handling conversions of an
2037 INTEGER_CST to another integer type. */
2040 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
2042 /* Given an integer constant, make new constant with new type,
2043 appropriately sign-extended or truncated. Use widest_int
2044 so that any extension is done according ARG1's type. */
2045 return force_fit_type (type
, wi::to_widest (arg1
),
2046 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
2047 TREE_OVERFLOW (arg1
));
2050 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2051 to an integer type. */
2054 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
2056 bool overflow
= false;
2059 /* The following code implements the floating point to integer
2060 conversion rules required by the Java Language Specification,
2061 that IEEE NaNs are mapped to zero and values that overflow
2062 the target precision saturate, i.e. values greater than
2063 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2064 are mapped to INT_MIN. These semantics are allowed by the
2065 C and C++ standards that simply state that the behavior of
2066 FP-to-integer conversion is unspecified upon overflow. */
2070 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2074 case FIX_TRUNC_EXPR
:
2075 real_trunc (&r
, VOIDmode
, &x
);
2082 /* If R is NaN, return zero and show we have an overflow. */
2083 if (REAL_VALUE_ISNAN (r
))
2086 val
= wi::zero (TYPE_PRECISION (type
));
2089 /* See if R is less than the lower bound or greater than the
2094 tree lt
= TYPE_MIN_VALUE (type
);
2095 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2096 if (real_less (&r
, &l
))
2099 val
= wi::to_wide (lt
);
2105 tree ut
= TYPE_MAX_VALUE (type
);
2108 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2109 if (real_less (&u
, &r
))
2112 val
= wi::to_wide (ut
);
2118 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2120 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2124 /* A subroutine of fold_convert_const handling conversions of a
2125 FIXED_CST to an integer type. */
2128 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2131 double_int temp
, temp_trunc
;
2134 /* Right shift FIXED_CST to temp by fbit. */
2135 temp
= TREE_FIXED_CST (arg1
).data
;
2136 mode
= TREE_FIXED_CST (arg1
).mode
;
2137 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2139 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2140 HOST_BITS_PER_DOUBLE_INT
,
2141 SIGNED_FIXED_POINT_MODE_P (mode
));
2143 /* Left shift temp to temp_trunc by fbit. */
2144 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2145 HOST_BITS_PER_DOUBLE_INT
,
2146 SIGNED_FIXED_POINT_MODE_P (mode
));
2150 temp
= double_int_zero
;
2151 temp_trunc
= double_int_zero
;
2154 /* If FIXED_CST is negative, we need to round the value toward 0.
2155 By checking if the fractional bits are not zero to add 1 to temp. */
2156 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2157 && temp_trunc
.is_negative ()
2158 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2159 temp
+= double_int_one
;
2161 /* Given a fixed-point constant, make new constant with new type,
2162 appropriately sign-extended or truncated. */
2163 t
= force_fit_type (type
, temp
, -1,
2164 (temp
.is_negative ()
2165 && (TYPE_UNSIGNED (type
)
2166 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2167 | TREE_OVERFLOW (arg1
));
2172 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2173 to another floating point type. */
2176 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2178 REAL_VALUE_TYPE value
;
2181 /* If the underlying modes are the same, simply treat it as
2182 copy and rebuild with TREE_REAL_CST information and the
2184 if (TYPE_MODE (type
) == TYPE_MODE (TREE_TYPE (arg1
)))
2186 t
= build_real (type
, TREE_REAL_CST (arg1
));
2190 /* Don't perform the operation if flag_signaling_nans is on
2191 and the operand is a signaling NaN. */
2192 if (HONOR_SNANS (arg1
)
2193 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2196 /* With flag_rounding_math we should respect the current rounding mode
2197 unless the conversion is exact. */
2198 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1
)
2199 && !exact_real_truncate (TYPE_MODE (type
), &TREE_REAL_CST (arg1
)))
2202 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2203 t
= build_real (type
, value
);
2205 /* If converting an infinity or NAN to a representation that doesn't
2206 have one, set the overflow bit so that we can produce some kind of
2207 error message at the appropriate point if necessary. It's not the
2208 most user-friendly message, but it's better than nothing. */
2209 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2210 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2211 TREE_OVERFLOW (t
) = 1;
2212 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2213 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2214 TREE_OVERFLOW (t
) = 1;
2215 /* Regular overflow, conversion produced an infinity in a mode that
2216 can't represent them. */
2217 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2218 && REAL_VALUE_ISINF (value
)
2219 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2220 TREE_OVERFLOW (t
) = 1;
2222 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2226 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2227 to a floating point type. */
2230 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2232 REAL_VALUE_TYPE value
;
2235 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2236 &TREE_FIXED_CST (arg1
));
2237 t
= build_real (type
, value
);
2239 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2243 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2244 to another fixed-point type. */
2247 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2249 FIXED_VALUE_TYPE value
;
2253 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2254 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2255 t
= build_fixed (type
, value
);
2257 /* Propagate overflow flags. */
2258 if (overflow_p
| TREE_OVERFLOW (arg1
))
2259 TREE_OVERFLOW (t
) = 1;
2263 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2264 to a fixed-point type. */
2267 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2269 FIXED_VALUE_TYPE value
;
2274 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2276 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2277 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2278 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2280 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2282 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2283 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2284 TYPE_SATURATING (type
));
2285 t
= build_fixed (type
, value
);
2287 /* Propagate overflow flags. */
2288 if (overflow_p
| TREE_OVERFLOW (arg1
))
2289 TREE_OVERFLOW (t
) = 1;
2293 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2294 to a fixed-point type. */
2297 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2299 FIXED_VALUE_TYPE value
;
2303 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2304 &TREE_REAL_CST (arg1
),
2305 TYPE_SATURATING (type
));
2306 t
= build_fixed (type
, value
);
2308 /* Propagate overflow flags. */
2309 if (overflow_p
| TREE_OVERFLOW (arg1
))
2310 TREE_OVERFLOW (t
) = 1;
2314 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2315 type TYPE. If no simplification can be done return NULL_TREE. */
2318 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2320 tree arg_type
= TREE_TYPE (arg1
);
2321 if (arg_type
== type
)
2324 /* We can't widen types, since the runtime value could overflow the
2325 original type before being extended to the new type. */
2326 if (POLY_INT_CST_P (arg1
)
2327 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2328 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2329 return build_poly_int_cst (type
,
2330 poly_wide_int::from (poly_int_cst_value (arg1
),
2331 TYPE_PRECISION (type
),
2332 TYPE_SIGN (arg_type
)));
2334 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2335 || TREE_CODE (type
) == OFFSET_TYPE
)
2337 if (TREE_CODE (arg1
) == INTEGER_CST
)
2338 return fold_convert_const_int_from_int (type
, arg1
);
2339 else if (TREE_CODE (arg1
) == REAL_CST
)
2340 return fold_convert_const_int_from_real (code
, type
, arg1
);
2341 else if (TREE_CODE (arg1
) == FIXED_CST
)
2342 return fold_convert_const_int_from_fixed (type
, arg1
);
2344 else if (TREE_CODE (type
) == REAL_TYPE
)
2346 if (TREE_CODE (arg1
) == INTEGER_CST
)
2348 tree res
= build_real_from_int_cst (type
, arg1
);
2349 /* Avoid the folding if flag_rounding_math is on and the
2350 conversion is not exact. */
2351 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
2354 wide_int w
= real_to_integer (&TREE_REAL_CST (res
), &fail
,
2355 TYPE_PRECISION (TREE_TYPE (arg1
)));
2356 if (fail
|| wi::ne_p (w
, wi::to_wide (arg1
)))
2361 else if (TREE_CODE (arg1
) == REAL_CST
)
2362 return fold_convert_const_real_from_real (type
, arg1
);
2363 else if (TREE_CODE (arg1
) == FIXED_CST
)
2364 return fold_convert_const_real_from_fixed (type
, arg1
);
2366 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2368 if (TREE_CODE (arg1
) == FIXED_CST
)
2369 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2370 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2371 return fold_convert_const_fixed_from_int (type
, arg1
);
2372 else if (TREE_CODE (arg1
) == REAL_CST
)
2373 return fold_convert_const_fixed_from_real (type
, arg1
);
2375 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2377 if (TREE_CODE (arg1
) == VECTOR_CST
2378 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2380 tree elttype
= TREE_TYPE (type
);
2381 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2382 /* We can't handle steps directly when extending, since the
2383 values need to wrap at the original precision first. */
2385 = (INTEGRAL_TYPE_P (elttype
)
2386 && INTEGRAL_TYPE_P (arg1_elttype
)
2387 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2388 tree_vector_builder v
;
2389 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2391 unsigned int len
= v
.encoded_nelts ();
2392 for (unsigned int i
= 0; i
< len
; ++i
)
2394 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2395 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2396 if (cvt
== NULL_TREE
)
2406 /* Construct a vector of zero elements of vector type TYPE. */
2409 build_zero_vector (tree type
)
2413 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2414 return build_vector_from_val (type
, t
);
2417 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2420 fold_convertible_p (const_tree type
, const_tree arg
)
2422 const_tree orig
= TREE_TYPE (arg
);
2427 if (TREE_CODE (arg
) == ERROR_MARK
2428 || TREE_CODE (type
) == ERROR_MARK
2429 || TREE_CODE (orig
) == ERROR_MARK
)
2432 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2435 switch (TREE_CODE (type
))
2437 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2438 case POINTER_TYPE
: case REFERENCE_TYPE
:
2440 return (INTEGRAL_TYPE_P (orig
)
2441 || (POINTER_TYPE_P (orig
)
2442 && TYPE_PRECISION (type
) <= TYPE_PRECISION (orig
))
2443 || TREE_CODE (orig
) == OFFSET_TYPE
);
2446 case FIXED_POINT_TYPE
:
2448 return TREE_CODE (type
) == TREE_CODE (orig
);
2451 return (VECTOR_TYPE_P (orig
)
2452 && known_eq (TYPE_VECTOR_SUBPARTS (type
),
2453 TYPE_VECTOR_SUBPARTS (orig
))
2454 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2461 /* Convert expression ARG to type TYPE. Used by the middle-end for
2462 simple conversions in preference to calling the front-end's convert. */
2465 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2467 tree orig
= TREE_TYPE (arg
);
2473 if (TREE_CODE (arg
) == ERROR_MARK
2474 || TREE_CODE (type
) == ERROR_MARK
2475 || TREE_CODE (orig
) == ERROR_MARK
)
2476 return error_mark_node
;
2478 switch (TREE_CODE (type
))
2481 case REFERENCE_TYPE
:
2482 /* Handle conversions between pointers to different address spaces. */
2483 if (POINTER_TYPE_P (orig
)
2484 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2485 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2486 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2489 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2491 if (TREE_CODE (arg
) == INTEGER_CST
)
2493 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2494 if (tem
!= NULL_TREE
)
2497 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2498 || TREE_CODE (orig
) == OFFSET_TYPE
)
2499 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2500 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2501 return fold_convert_loc (loc
, type
,
2502 fold_build1_loc (loc
, REALPART_EXPR
,
2503 TREE_TYPE (orig
), arg
));
2504 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2505 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2506 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2509 if (TREE_CODE (arg
) == INTEGER_CST
)
2511 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2512 if (tem
!= NULL_TREE
)
2515 else if (TREE_CODE (arg
) == REAL_CST
)
2517 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2518 if (tem
!= NULL_TREE
)
2521 else if (TREE_CODE (arg
) == FIXED_CST
)
2523 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2524 if (tem
!= NULL_TREE
)
2528 switch (TREE_CODE (orig
))
2531 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2532 case POINTER_TYPE
: case REFERENCE_TYPE
:
2533 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2536 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2538 case FIXED_POINT_TYPE
:
2539 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2542 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2543 return fold_convert_loc (loc
, type
, tem
);
2549 case FIXED_POINT_TYPE
:
2550 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2551 || TREE_CODE (arg
) == REAL_CST
)
2553 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2554 if (tem
!= NULL_TREE
)
2555 goto fold_convert_exit
;
2558 switch (TREE_CODE (orig
))
2560 case FIXED_POINT_TYPE
:
2565 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2568 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2569 return fold_convert_loc (loc
, type
, tem
);
2576 switch (TREE_CODE (orig
))
2579 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2580 case POINTER_TYPE
: case REFERENCE_TYPE
:
2582 case FIXED_POINT_TYPE
:
2583 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2584 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2585 fold_convert_loc (loc
, TREE_TYPE (type
),
2586 integer_zero_node
));
2591 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2593 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2594 TREE_OPERAND (arg
, 0));
2595 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2596 TREE_OPERAND (arg
, 1));
2597 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2600 arg
= save_expr (arg
);
2601 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2602 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2603 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2604 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2605 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2613 if (integer_zerop (arg
))
2614 return build_zero_vector (type
);
2615 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2616 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2617 || TREE_CODE (orig
) == VECTOR_TYPE
);
2618 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2621 tem
= fold_ignored_result (arg
);
2622 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2625 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2626 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2630 tem
= protected_set_expr_location_unshare (tem
, loc
);
2634 /* Return false if expr can be assumed not to be an lvalue, true
2638 maybe_lvalue_p (const_tree x
)
2640 /* We only need to wrap lvalue tree codes. */
2641 switch (TREE_CODE (x
))
2654 case ARRAY_RANGE_REF
:
2660 case PREINCREMENT_EXPR
:
2661 case PREDECREMENT_EXPR
:
2663 case TRY_CATCH_EXPR
:
2664 case WITH_CLEANUP_EXPR
:
2670 case VIEW_CONVERT_EXPR
:
2674 /* Assume the worst for front-end tree codes. */
2675 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2683 /* Return an expr equal to X but certainly not valid as an lvalue. */
2686 non_lvalue_loc (location_t loc
, tree x
)
2688 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2693 if (! maybe_lvalue_p (x
))
2695 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2698 /* Given a tree comparison code, return the code that is the logical inverse.
2699 It is generally not safe to do this for floating-point comparisons, except
2700 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2701 ERROR_MARK in this case. */
2704 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2706 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2707 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2717 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2719 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2721 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2723 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2737 return UNORDERED_EXPR
;
2738 case UNORDERED_EXPR
:
2739 return ORDERED_EXPR
;
2745 /* Similar, but return the comparison that results if the operands are
2746 swapped. This is safe for floating-point. */
2749 swap_tree_comparison (enum tree_code code
)
2756 case UNORDERED_EXPR
:
2782 /* Convert a comparison tree code from an enum tree_code representation
2783 into a compcode bit-based encoding. This function is the inverse of
2784 compcode_to_comparison. */
2786 static enum comparison_code
2787 comparison_to_compcode (enum tree_code code
)
2804 return COMPCODE_ORD
;
2805 case UNORDERED_EXPR
:
2806 return COMPCODE_UNORD
;
2808 return COMPCODE_UNLT
;
2810 return COMPCODE_UNEQ
;
2812 return COMPCODE_UNLE
;
2814 return COMPCODE_UNGT
;
2816 return COMPCODE_LTGT
;
2818 return COMPCODE_UNGE
;
2824 /* Convert a compcode bit-based encoding of a comparison operator back
2825 to GCC's enum tree_code representation. This function is the
2826 inverse of comparison_to_compcode. */
2828 static enum tree_code
2829 compcode_to_comparison (enum comparison_code code
)
2846 return ORDERED_EXPR
;
2847 case COMPCODE_UNORD
:
2848 return UNORDERED_EXPR
;
2866 /* Return true if COND1 tests the opposite condition of COND2. */
2869 inverse_conditions_p (const_tree cond1
, const_tree cond2
)
2871 return (COMPARISON_CLASS_P (cond1
)
2872 && COMPARISON_CLASS_P (cond2
)
2873 && (invert_tree_comparison
2875 HONOR_NANS (TREE_OPERAND (cond1
, 0))) == TREE_CODE (cond2
))
2876 && operand_equal_p (TREE_OPERAND (cond1
, 0),
2877 TREE_OPERAND (cond2
, 0), 0)
2878 && operand_equal_p (TREE_OPERAND (cond1
, 1),
2879 TREE_OPERAND (cond2
, 1), 0));
2882 /* Return a tree for the comparison which is the combination of
2883 doing the AND or OR (depending on CODE) of the two operations LCODE
2884 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2885 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2886 if this makes the transformation invalid. */
2889 combine_comparisons (location_t loc
,
2890 enum tree_code code
, enum tree_code lcode
,
2891 enum tree_code rcode
, tree truth_type
,
2892 tree ll_arg
, tree lr_arg
)
2894 bool honor_nans
= HONOR_NANS (ll_arg
);
2895 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2896 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2901 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2902 compcode
= lcompcode
& rcompcode
;
2905 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2906 compcode
= lcompcode
| rcompcode
;
2915 /* Eliminate unordered comparisons, as well as LTGT and ORD
2916 which are not used unless the mode has NaNs. */
2917 compcode
&= ~COMPCODE_UNORD
;
2918 if (compcode
== COMPCODE_LTGT
)
2919 compcode
= COMPCODE_NE
;
2920 else if (compcode
== COMPCODE_ORD
)
2921 compcode
= COMPCODE_TRUE
;
2923 else if (flag_trapping_math
)
2925 /* Check that the original operation and the optimized ones will trap
2926 under the same condition. */
2927 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2928 && (lcompcode
!= COMPCODE_EQ
)
2929 && (lcompcode
!= COMPCODE_ORD
);
2930 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2931 && (rcompcode
!= COMPCODE_EQ
)
2932 && (rcompcode
!= COMPCODE_ORD
);
2933 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2934 && (compcode
!= COMPCODE_EQ
)
2935 && (compcode
!= COMPCODE_ORD
);
2937 /* In a short-circuited boolean expression the LHS might be
2938 such that the RHS, if evaluated, will never trap. For
2939 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2940 if neither x nor y is NaN. (This is a mixed blessing: for
2941 example, the expression above will never trap, hence
2942 optimizing it to x < y would be invalid). */
2943 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2944 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2947 /* If the comparison was short-circuited, and only the RHS
2948 trapped, we may now generate a spurious trap. */
2950 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2953 /* If we changed the conditions that cause a trap, we lose. */
2954 if ((ltrap
|| rtrap
) != trap
)
2958 if (compcode
== COMPCODE_TRUE
)
2959 return constant_boolean_node (true, truth_type
);
2960 else if (compcode
== COMPCODE_FALSE
)
2961 return constant_boolean_node (false, truth_type
);
2964 enum tree_code tcode
;
2966 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2967 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2971 /* Return nonzero if two operands (typically of the same tree node)
2972 are necessarily equal. FLAGS modifies behavior as follows:
2974 If OEP_ONLY_CONST is set, only return nonzero for constants.
2975 This function tests whether the operands are indistinguishable;
2976 it does not test whether they are equal using C's == operation.
2977 The distinction is important for IEEE floating point, because
2978 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2979 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2981 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2982 even though it may hold multiple values during a function.
2983 This is because a GCC tree node guarantees that nothing else is
2984 executed between the evaluation of its "operands" (which may often
2985 be evaluated in arbitrary order). Hence if the operands themselves
2986 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2987 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2988 unset means assuming isochronic (or instantaneous) tree equivalence.
2989 Unless comparing arbitrary expression trees, such as from different
2990 statements, this flag can usually be left unset.
2992 If OEP_PURE_SAME is set, then pure functions with identical arguments
2993 are considered the same. It is used when the caller has other ways
2994 to ensure that global memory is unchanged in between.
2996 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2997 not values of expressions.
2999 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3000 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3002 If OEP_BITWISE is set, then require the values to be bitwise identical
3003 rather than simply numerically equal. Do not take advantage of things
3004 like math-related flags or undefined behavior; only return true for
3005 values that are provably bitwise identical in all circumstances.
3007 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3008 any operand with side effect. This is unnecesarily conservative in the
3009 case we know that arg0 and arg1 are in disjoint code paths (such as in
3010 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3011 addresses with TREE_CONSTANT flag set so we know that &var == &var
3012 even if var is volatile. */
3015 operand_compare::operand_equal_p (const_tree arg0
, const_tree arg1
,
3019 if (verify_hash_value (arg0
, arg1
, flags
, &r
))
3022 STRIP_ANY_LOCATION_WRAPPER (arg0
);
3023 STRIP_ANY_LOCATION_WRAPPER (arg1
);
3025 /* If either is ERROR_MARK, they aren't equal. */
3026 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
3027 || TREE_TYPE (arg0
) == error_mark_node
3028 || TREE_TYPE (arg1
) == error_mark_node
)
3031 /* Similar, if either does not have a type (like a template id),
3032 they aren't equal. */
3033 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
3036 /* Bitwise identity makes no sense if the values have different layouts. */
3037 if ((flags
& OEP_BITWISE
)
3038 && !tree_nop_conversion_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3041 /* We cannot consider pointers to different address space equal. */
3042 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
3043 && POINTER_TYPE_P (TREE_TYPE (arg1
))
3044 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
3045 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
3048 /* Check equality of integer constants before bailing out due to
3049 precision differences. */
3050 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3052 /* Address of INTEGER_CST is not defined; check that we did not forget
3053 to drop the OEP_ADDRESS_OF flags. */
3054 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3055 return tree_int_cst_equal (arg0
, arg1
);
3058 if (!(flags
& OEP_ADDRESS_OF
))
3060 /* If both types don't have the same signedness, then we can't consider
3061 them equal. We must check this before the STRIP_NOPS calls
3062 because they may change the signedness of the arguments. As pointers
3063 strictly don't have a signedness, require either two pointers or
3064 two non-pointers as well. */
3065 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
3066 || POINTER_TYPE_P (TREE_TYPE (arg0
))
3067 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
3070 /* If both types don't have the same precision, then it is not safe
3072 if (element_precision (TREE_TYPE (arg0
))
3073 != element_precision (TREE_TYPE (arg1
)))
3080 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3081 sanity check once the issue is solved. */
3083 /* Addresses of conversions and SSA_NAMEs (and many other things)
3084 are not defined. Check that we did not forget to drop the
3085 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3086 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
3087 && TREE_CODE (arg0
) != SSA_NAME
);
3090 /* In case both args are comparisons but with different comparison
3091 code, try to swap the comparison operands of one arg to produce
3092 a match and compare that variant. */
3093 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3094 && COMPARISON_CLASS_P (arg0
)
3095 && COMPARISON_CLASS_P (arg1
))
3097 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
3099 if (TREE_CODE (arg0
) == swap_code
)
3100 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3101 TREE_OPERAND (arg1
, 1), flags
)
3102 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3103 TREE_OPERAND (arg1
, 0), flags
);
3106 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3108 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3109 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3111 else if (flags
& OEP_ADDRESS_OF
)
3113 /* If we are interested in comparing addresses ignore
3114 MEM_REF wrappings of the base that can appear just for
3116 if (TREE_CODE (arg0
) == MEM_REF
3118 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3119 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3120 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3122 else if (TREE_CODE (arg1
) == MEM_REF
3124 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3125 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3126 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3134 /* When not checking adddresses, this is needed for conversions and for
3135 COMPONENT_REF. Might as well play it safe and always test this. */
3136 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3137 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3138 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3139 && !(flags
& OEP_ADDRESS_OF
)))
3142 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3143 We don't care about side effects in that case because the SAVE_EXPR
3144 takes care of that for us. In all other cases, two expressions are
3145 equal if they have no side effects. If we have two identical
3146 expressions with side effects that should be treated the same due
3147 to the only side effects being identical SAVE_EXPR's, that will
3148 be detected in the recursive calls below.
3149 If we are taking an invariant address of two identical objects
3150 they are necessarily equal as well. */
3151 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3152 && (TREE_CODE (arg0
) == SAVE_EXPR
3153 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3154 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3157 /* Next handle constant cases, those for which we can return 1 even
3158 if ONLY_CONST is set. */
3159 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3160 switch (TREE_CODE (arg0
))
3163 return tree_int_cst_equal (arg0
, arg1
);
3166 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3167 TREE_FIXED_CST (arg1
));
3170 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3173 if (!(flags
& OEP_BITWISE
) && !HONOR_SIGNED_ZEROS (arg0
))
3175 /* If we do not distinguish between signed and unsigned zero,
3176 consider them equal. */
3177 if (real_zerop (arg0
) && real_zerop (arg1
))
3184 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3185 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3188 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3189 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3192 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3193 for (unsigned int i
= 0; i
< count
; ++i
)
3194 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3195 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3201 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3203 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3207 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3208 && ! memcmp (TREE_STRING_POINTER (arg0
),
3209 TREE_STRING_POINTER (arg1
),
3210 TREE_STRING_LENGTH (arg0
)));
3213 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3214 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3215 flags
| OEP_ADDRESS_OF
3216 | OEP_MATCH_SIDE_EFFECTS
);
3218 /* In GIMPLE empty constructors are allowed in initializers of
3220 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
3225 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3226 two instances of undefined behavior will give identical results. */
3227 if (flags
& (OEP_ONLY_CONST
| OEP_BITWISE
))
3230 /* Define macros to test an operand from arg0 and arg1 for equality and a
3231 variant that allows null and views null as being different from any
3232 non-null value. In the latter case, if either is null, the both
3233 must be; otherwise, do the normal comparison. */
3234 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3235 TREE_OPERAND (arg1, N), flags)
3237 #define OP_SAME_WITH_NULL(N) \
3238 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3239 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3241 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3244 /* Two conversions are equal only if signedness and modes match. */
3245 switch (TREE_CODE (arg0
))
3248 case FIX_TRUNC_EXPR
:
3249 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3250 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3260 case tcc_comparison
:
3262 if (OP_SAME (0) && OP_SAME (1))
3265 /* For commutative ops, allow the other order. */
3266 return (commutative_tree_code (TREE_CODE (arg0
))
3267 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3268 TREE_OPERAND (arg1
, 1), flags
)
3269 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3270 TREE_OPERAND (arg1
, 0), flags
));
3273 /* If either of the pointer (or reference) expressions we are
3274 dereferencing contain a side effect, these cannot be equal,
3275 but their addresses can be. */
3276 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3277 && (TREE_SIDE_EFFECTS (arg0
)
3278 || TREE_SIDE_EFFECTS (arg1
)))
3281 switch (TREE_CODE (arg0
))
3284 if (!(flags
& OEP_ADDRESS_OF
))
3286 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3287 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3289 /* Verify that the access types are compatible. */
3290 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0
))
3291 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)))
3294 flags
&= ~OEP_ADDRESS_OF
;
3298 /* Require the same offset. */
3299 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3300 TYPE_SIZE (TREE_TYPE (arg1
)),
3301 flags
& ~OEP_ADDRESS_OF
))
3306 case VIEW_CONVERT_EXPR
:
3309 case TARGET_MEM_REF
:
3311 if (!(flags
& OEP_ADDRESS_OF
))
3313 /* Require equal access sizes */
3314 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3315 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3316 || !TYPE_SIZE (TREE_TYPE (arg1
))
3317 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3318 TYPE_SIZE (TREE_TYPE (arg1
)),
3321 /* Verify that access happens in similar types. */
3322 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3324 /* Verify that accesses are TBAA compatible. */
3325 if (!alias_ptr_types_compatible_p
3326 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3327 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3328 || (MR_DEPENDENCE_CLIQUE (arg0
)
3329 != MR_DEPENDENCE_CLIQUE (arg1
))
3330 || (MR_DEPENDENCE_BASE (arg0
)
3331 != MR_DEPENDENCE_BASE (arg1
)))
3333 /* Verify that alignment is compatible. */
3334 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3335 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3338 flags
&= ~OEP_ADDRESS_OF
;
3339 return (OP_SAME (0) && OP_SAME (1)
3340 /* TARGET_MEM_REF require equal extra operands. */
3341 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3342 || (OP_SAME_WITH_NULL (2)
3343 && OP_SAME_WITH_NULL (3)
3344 && OP_SAME_WITH_NULL (4))));
3347 case ARRAY_RANGE_REF
:
3350 flags
&= ~OEP_ADDRESS_OF
;
3351 /* Compare the array index by value if it is constant first as we
3352 may have different types but same value here. */
3353 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3354 TREE_OPERAND (arg1
, 1))
3356 && OP_SAME_WITH_NULL (2)
3357 && OP_SAME_WITH_NULL (3)
3358 /* Compare low bound and element size as with OEP_ADDRESS_OF
3359 we have to account for the offset of the ref. */
3360 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3361 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3362 || (operand_equal_p (array_ref_low_bound
3363 (CONST_CAST_TREE (arg0
)),
3365 (CONST_CAST_TREE (arg1
)), flags
)
3366 && operand_equal_p (array_ref_element_size
3367 (CONST_CAST_TREE (arg0
)),
3368 array_ref_element_size
3369 (CONST_CAST_TREE (arg1
)),
3373 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3374 may be NULL when we're called to compare MEM_EXPRs. */
3375 if (!OP_SAME_WITH_NULL (0))
3378 bool compare_address
= flags
& OEP_ADDRESS_OF
;
3380 /* Most of time we only need to compare FIELD_DECLs for equality.
3381 However when determining address look into actual offsets.
3382 These may match for unions and unshared record types. */
3383 flags
&= ~OEP_ADDRESS_OF
;
3387 && (flags
& OEP_ADDRESS_OF_SAME_FIELD
) == 0)
3389 tree field0
= TREE_OPERAND (arg0
, 1);
3390 tree field1
= TREE_OPERAND (arg1
, 1);
3392 /* Non-FIELD_DECL operands can appear in C++ templates. */
3393 if (TREE_CODE (field0
) != FIELD_DECL
3394 || TREE_CODE (field1
) != FIELD_DECL
3395 || !operand_equal_p (DECL_FIELD_OFFSET (field0
),
3396 DECL_FIELD_OFFSET (field1
), flags
)
3397 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0
),
3398 DECL_FIELD_BIT_OFFSET (field1
),
3406 return OP_SAME_WITH_NULL (2);
3411 flags
&= ~OEP_ADDRESS_OF
;
3412 return OP_SAME (1) && OP_SAME (2);
3418 case tcc_expression
:
3419 switch (TREE_CODE (arg0
))
3422 /* Be sure we pass right ADDRESS_OF flag. */
3423 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3424 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3425 TREE_OPERAND (arg1
, 0),
3426 flags
| OEP_ADDRESS_OF
);
3428 case TRUTH_NOT_EXPR
:
3431 case TRUTH_ANDIF_EXPR
:
3432 case TRUTH_ORIF_EXPR
:
3433 return OP_SAME (0) && OP_SAME (1);
3435 case WIDEN_MULT_PLUS_EXPR
:
3436 case WIDEN_MULT_MINUS_EXPR
:
3439 /* The multiplcation operands are commutative. */
3442 case TRUTH_AND_EXPR
:
3444 case TRUTH_XOR_EXPR
:
3445 if (OP_SAME (0) && OP_SAME (1))
3448 /* Otherwise take into account this is a commutative operation. */
3449 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3450 TREE_OPERAND (arg1
, 1), flags
)
3451 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3452 TREE_OPERAND (arg1
, 0), flags
));
3455 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3457 flags
&= ~OEP_ADDRESS_OF
;
3460 case BIT_INSERT_EXPR
:
3461 /* BIT_INSERT_EXPR has an implict operand as the type precision
3462 of op1. Need to check to make sure they are the same. */
3463 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3464 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3465 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3466 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3472 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3477 case PREDECREMENT_EXPR
:
3478 case PREINCREMENT_EXPR
:
3479 case POSTDECREMENT_EXPR
:
3480 case POSTINCREMENT_EXPR
:
3481 if (flags
& OEP_LEXICOGRAPHIC
)
3482 return OP_SAME (0) && OP_SAME (1);
3485 case CLEANUP_POINT_EXPR
:
3488 if (flags
& OEP_LEXICOGRAPHIC
)
3493 /* Virtual table reference. */
3494 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0
),
3495 OBJ_TYPE_REF_EXPR (arg1
), flags
))
3497 flags
&= ~OEP_ADDRESS_OF
;
3498 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0
))
3499 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1
)))
3501 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0
),
3502 OBJ_TYPE_REF_OBJECT (arg1
), flags
))
3504 if (virtual_method_call_p (arg0
))
3506 if (!virtual_method_call_p (arg1
))
3508 return types_same_for_odr (obj_type_ref_class (arg0
),
3509 obj_type_ref_class (arg1
));
3518 switch (TREE_CODE (arg0
))
3521 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3522 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3523 /* If not both CALL_EXPRs are either internal or normal function
3524 functions, then they are not equal. */
3526 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3528 /* If the CALL_EXPRs call different internal functions, then they
3530 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3535 /* If the CALL_EXPRs call different functions, then they are not
3537 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3542 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3544 unsigned int cef
= call_expr_flags (arg0
);
3545 if (flags
& OEP_PURE_SAME
)
3546 cef
&= ECF_CONST
| ECF_PURE
;
3549 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3553 /* Now see if all the arguments are the same. */
3555 const_call_expr_arg_iterator iter0
, iter1
;
3557 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3558 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3560 a0
= next_const_call_expr_arg (&iter0
),
3561 a1
= next_const_call_expr_arg (&iter1
))
3562 if (! operand_equal_p (a0
, a1
, flags
))
3565 /* If we get here and both argument lists are exhausted
3566 then the CALL_EXPRs are equal. */
3567 return ! (a0
|| a1
);
3573 case tcc_declaration
:
3574 /* Consider __builtin_sqrt equal to sqrt. */
3575 if (TREE_CODE (arg0
) == FUNCTION_DECL
)
3576 return (fndecl_built_in_p (arg0
) && fndecl_built_in_p (arg1
)
3577 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3578 && (DECL_UNCHECKED_FUNCTION_CODE (arg0
)
3579 == DECL_UNCHECKED_FUNCTION_CODE (arg1
)));
3582 && (flags
& OEP_DECL_NAME
)
3583 && (flags
& OEP_LEXICOGRAPHIC
))
3585 /* Consider decls with the same name equal. The caller needs
3586 to make sure they refer to the same entity (such as a function
3587 formal parameter). */
3588 tree a0name
= DECL_NAME (arg0
);
3589 tree a1name
= DECL_NAME (arg1
);
3590 const char *a0ns
= a0name
? IDENTIFIER_POINTER (a0name
) : NULL
;
3591 const char *a1ns
= a1name
? IDENTIFIER_POINTER (a1name
) : NULL
;
3592 return a0ns
&& a1ns
&& strcmp (a0ns
, a1ns
) == 0;
3596 case tcc_exceptional
:
3597 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3599 if (CONSTRUCTOR_NO_CLEARING (arg0
) != CONSTRUCTOR_NO_CLEARING (arg1
))
3602 /* In GIMPLE constructors are used only to build vectors from
3603 elements. Individual elements in the constructor must be
3604 indexed in increasing order and form an initial sequence.
3606 We make no effort to compare constructors in generic.
3607 (see sem_variable::equals in ipa-icf which can do so for
3609 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3610 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3613 /* Be sure that vectors constructed have the same representation.
3614 We only tested element precision and modes to match.
3615 Vectors may be BLKmode and thus also check that the number of
3617 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3618 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3621 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3622 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3623 unsigned int len
= vec_safe_length (v0
);
3625 if (len
!= vec_safe_length (v1
))
3628 for (unsigned int i
= 0; i
< len
; i
++)
3630 constructor_elt
*c0
= &(*v0
)[i
];
3631 constructor_elt
*c1
= &(*v1
)[i
];
3633 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3634 /* In GIMPLE the indexes can be either NULL or matching i.
3635 Double check this so we won't get false
3636 positives for GENERIC. */
3638 && (TREE_CODE (c0
->index
) != INTEGER_CST
3639 || compare_tree_int (c0
->index
, i
)))
3641 && (TREE_CODE (c1
->index
) != INTEGER_CST
3642 || compare_tree_int (c1
->index
, i
))))
3647 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3648 && (flags
& OEP_LEXICOGRAPHIC
))
3650 /* Compare the STATEMENT_LISTs. */
3651 tree_stmt_iterator tsi1
, tsi2
;
3652 tree body1
= CONST_CAST_TREE (arg0
);
3653 tree body2
= CONST_CAST_TREE (arg1
);
3654 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3655 tsi_next (&tsi1
), tsi_next (&tsi2
))
3657 /* The lists don't have the same number of statements. */
3658 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3660 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3662 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3663 flags
& (OEP_LEXICOGRAPHIC
3664 | OEP_NO_HASH_CHECK
)))
3671 switch (TREE_CODE (arg0
))
3674 if (flags
& OEP_LEXICOGRAPHIC
)
3675 return OP_SAME_WITH_NULL (0);
3677 case DEBUG_BEGIN_STMT
:
3678 if (flags
& OEP_LEXICOGRAPHIC
)
3690 #undef OP_SAME_WITH_NULL
3693 /* Generate a hash value for an expression. This can be used iteratively
3694 by passing a previous result as the HSTATE argument. */
3697 operand_compare::hash_operand (const_tree t
, inchash::hash
&hstate
,
3701 enum tree_code code
;
3702 enum tree_code_class tclass
;
3704 if (t
== NULL_TREE
|| t
== error_mark_node
)
3706 hstate
.merge_hash (0);
3710 STRIP_ANY_LOCATION_WRAPPER (t
);
3712 if (!(flags
& OEP_ADDRESS_OF
))
3715 code
= TREE_CODE (t
);
3719 /* Alas, constants aren't shared, so we can't rely on pointer
3722 hstate
.merge_hash (0);
3725 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3726 for (i
= 0; i
< TREE_INT_CST_EXT_NUNITS (t
); i
++)
3727 hstate
.add_hwi (TREE_INT_CST_ELT (t
, i
));
3732 if (!HONOR_SIGNED_ZEROS (t
) && real_zerop (t
))
3735 val2
= real_hash (TREE_REAL_CST_PTR (t
));
3736 hstate
.merge_hash (val2
);
3741 unsigned int val2
= fixed_hash (TREE_FIXED_CST_PTR (t
));
3742 hstate
.merge_hash (val2
);
3746 hstate
.add ((const void *) TREE_STRING_POINTER (t
),
3747 TREE_STRING_LENGTH (t
));
3750 hash_operand (TREE_REALPART (t
), hstate
, flags
);
3751 hash_operand (TREE_IMAGPART (t
), hstate
, flags
);
3755 hstate
.add_int (VECTOR_CST_NPATTERNS (t
));
3756 hstate
.add_int (VECTOR_CST_NELTS_PER_PATTERN (t
));
3757 unsigned int count
= vector_cst_encoded_nelts (t
);
3758 for (unsigned int i
= 0; i
< count
; ++i
)
3759 hash_operand (VECTOR_CST_ENCODED_ELT (t
, i
), hstate
, flags
);
3763 /* We can just compare by pointer. */
3764 hstate
.add_hwi (SSA_NAME_VERSION (t
));
3766 case PLACEHOLDER_EXPR
:
3767 /* The node itself doesn't matter. */
3774 /* A list of expressions, for a CALL_EXPR or as the elements of a
3776 for (; t
; t
= TREE_CHAIN (t
))
3777 hash_operand (TREE_VALUE (t
), hstate
, flags
);
3781 unsigned HOST_WIDE_INT idx
;
3783 flags
&= ~OEP_ADDRESS_OF
;
3784 hstate
.add_int (CONSTRUCTOR_NO_CLEARING (t
));
3785 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), idx
, field
, value
)
3787 /* In GIMPLE the indexes can be either NULL or matching i. */
3788 if (field
== NULL_TREE
)
3789 field
= bitsize_int (idx
);
3790 hash_operand (field
, hstate
, flags
);
3791 hash_operand (value
, hstate
, flags
);
3795 case STATEMENT_LIST
:
3797 tree_stmt_iterator i
;
3798 for (i
= tsi_start (CONST_CAST_TREE (t
));
3799 !tsi_end_p (i
); tsi_next (&i
))
3800 hash_operand (tsi_stmt (i
), hstate
, flags
);
3804 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
3805 hash_operand (TREE_VEC_ELT (t
, i
), hstate
, flags
);
3807 case IDENTIFIER_NODE
:
3808 hstate
.add_object (IDENTIFIER_HASH_VALUE (t
));
3811 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3812 Otherwise nodes that compare equal according to operand_equal_p might
3813 get different hash codes. However, don't do this for machine specific
3814 or front end builtins, since the function code is overloaded in those
3816 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
3817 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t
)))
3819 t
= builtin_decl_explicit (DECL_FUNCTION_CODE (t
));
3820 code
= TREE_CODE (t
);
3824 if (POLY_INT_CST_P (t
))
3826 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
3827 hstate
.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t
, i
)));
3830 tclass
= TREE_CODE_CLASS (code
);
3832 if (tclass
== tcc_declaration
)
3834 /* DECL's have a unique ID */
3835 hstate
.add_hwi (DECL_UID (t
));
3837 else if (tclass
== tcc_comparison
&& !commutative_tree_code (code
))
3839 /* For comparisons that can be swapped, use the lower
3841 enum tree_code ccode
= swap_tree_comparison (code
);
3844 hstate
.add_object (ccode
);
3845 hash_operand (TREE_OPERAND (t
, ccode
!= code
), hstate
, flags
);
3846 hash_operand (TREE_OPERAND (t
, ccode
== code
), hstate
, flags
);
3848 else if (CONVERT_EXPR_CODE_P (code
))
3850 /* NOP_EXPR and CONVERT_EXPR are considered equal by
3852 enum tree_code ccode
= NOP_EXPR
;
3853 hstate
.add_object (ccode
);
3855 /* Don't hash the type, that can lead to having nodes which
3856 compare equal according to operand_equal_p, but which
3857 have different hash codes. Make sure to include signedness
3858 in the hash computation. */
3859 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3860 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3862 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
3863 else if (code
== MEM_REF
3864 && (flags
& OEP_ADDRESS_OF
) != 0
3865 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
3866 && DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0))
3867 && integer_zerop (TREE_OPERAND (t
, 1)))
3868 hash_operand (TREE_OPERAND (TREE_OPERAND (t
, 0), 0),
3870 /* Don't ICE on FE specific trees, or their arguments etc.
3871 during operand_equal_p hash verification. */
3872 else if (!IS_EXPR_CODE_CLASS (tclass
))
3873 gcc_assert (flags
& OEP_HASH_CHECK
);
3876 unsigned int sflags
= flags
;
3878 hstate
.add_object (code
);
3883 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3884 flags
|= OEP_ADDRESS_OF
;
3890 case TARGET_MEM_REF
:
3891 flags
&= ~OEP_ADDRESS_OF
;
3896 if (sflags
& OEP_ADDRESS_OF
)
3898 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3899 hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t
, 1)),
3900 hstate
, flags
& ~OEP_ADDRESS_OF
);
3901 hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t
, 1)),
3902 hstate
, flags
& ~OEP_ADDRESS_OF
);
3907 case ARRAY_RANGE_REF
:
3909 sflags
&= ~OEP_ADDRESS_OF
;
3913 flags
&= ~OEP_ADDRESS_OF
;
3916 case WIDEN_MULT_PLUS_EXPR
:
3917 case WIDEN_MULT_MINUS_EXPR
:
3919 /* The multiplication operands are commutative. */
3920 inchash::hash one
, two
;
3921 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3922 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3923 hstate
.add_commutative (one
, two
);
3924 hash_operand (TREE_OPERAND (t
, 2), two
, flags
);
3929 if (CALL_EXPR_FN (t
) == NULL_TREE
)
3930 hstate
.add_int (CALL_EXPR_IFN (t
));
3934 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
3935 Usually different TARGET_EXPRs just should use
3936 different temporaries in their slots. */
3937 hash_operand (TARGET_EXPR_SLOT (t
), hstate
, flags
);
3941 /* Virtual table reference. */
3942 inchash::add_expr (OBJ_TYPE_REF_EXPR (t
), hstate
, flags
);
3943 flags
&= ~OEP_ADDRESS_OF
;
3944 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t
), hstate
, flags
);
3945 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t
), hstate
, flags
);
3946 if (!virtual_method_call_p (t
))
3948 if (tree c
= obj_type_ref_class (t
))
3950 c
= TYPE_NAME (TYPE_MAIN_VARIANT (c
));
3951 /* We compute mangled names only when free_lang_data is run.
3952 In that case we can hash precisely. */
3953 if (TREE_CODE (c
) == TYPE_DECL
3954 && DECL_ASSEMBLER_NAME_SET_P (c
))
3956 (IDENTIFIER_HASH_VALUE
3957 (DECL_ASSEMBLER_NAME (c
)));
3964 /* Don't hash the type, that can lead to having nodes which
3965 compare equal according to operand_equal_p, but which
3966 have different hash codes. */
3967 if (code
== NON_LVALUE_EXPR
)
3969 /* Make sure to include signness in the hash computation. */
3970 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3971 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3974 else if (commutative_tree_code (code
))
3976 /* It's a commutative expression. We want to hash it the same
3977 however it appears. We do this by first hashing both operands
3978 and then rehashing based on the order of their independent
3980 inchash::hash one
, two
;
3981 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3982 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3983 hstate
.add_commutative (one
, two
);
3986 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
3987 hash_operand (TREE_OPERAND (t
, i
), hstate
,
3988 i
== 0 ? flags
: sflags
);
3995 operand_compare::verify_hash_value (const_tree arg0
, const_tree arg1
,
3996 unsigned int flags
, bool *ret
)
3998 /* When checking and unless comparing DECL names, verify that if
3999 the outermost operand_equal_p call returns non-zero then ARG0
4000 and ARG1 have the same hash value. */
4001 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
4003 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
4005 if (arg0
!= arg1
&& !(flags
& OEP_DECL_NAME
))
4007 inchash::hash
hstate0 (0), hstate1 (0);
4008 hash_operand (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
4009 hash_operand (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
4010 hashval_t h0
= hstate0
.end ();
4011 hashval_t h1
= hstate1
.end ();
4012 gcc_assert (h0
== h1
);
4026 static operand_compare default_compare_instance
;
4028 /* Conveinece wrapper around operand_compare class because usually we do
4029 not need to play with the valueizer. */
4032 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
4034 return default_compare_instance
.operand_equal_p (arg0
, arg1
, flags
);
4040 /* Generate a hash value for an expression. This can be used iteratively
4041 by passing a previous result as the HSTATE argument.
4043 This function is intended to produce the same hash for expressions which
4044 would compare equal using operand_equal_p. */
4046 add_expr (const_tree t
, inchash::hash
&hstate
, unsigned int flags
)
4048 default_compare_instance
.hash_operand (t
, hstate
, flags
);
4053 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4054 with a different signedness or a narrower precision. */
4057 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
4059 if (operand_equal_p (arg0
, arg1
, 0))
4062 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
4063 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
4066 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4067 and see if the inner values are the same. This removes any
4068 signedness comparison, which doesn't matter here. */
4073 if (operand_equal_p (op0
, op1
, 0))
4076 /* Discard a single widening conversion from ARG1 and see if the inner
4077 value is the same as ARG0. */
4078 if (CONVERT_EXPR_P (arg1
)
4079 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4080 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4081 < TYPE_PRECISION (TREE_TYPE (arg1
))
4082 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
4088 /* See if ARG is an expression that is either a comparison or is performing
4089 arithmetic on comparisons. The comparisons must only be comparing
4090 two different values, which will be stored in *CVAL1 and *CVAL2; if
4091 they are nonzero it means that some operands have already been found.
4092 No variables may be used anywhere else in the expression except in the
4095 If this is true, return 1. Otherwise, return zero. */
4098 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
4100 enum tree_code code
= TREE_CODE (arg
);
4101 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4103 /* We can handle some of the tcc_expression cases here. */
4104 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4106 else if (tclass
== tcc_expression
4107 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
4108 || code
== COMPOUND_EXPR
))
4109 tclass
= tcc_binary
;
4114 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
4117 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4118 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
4123 case tcc_expression
:
4124 if (code
== COND_EXPR
)
4125 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4126 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
4127 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
4130 case tcc_comparison
:
4131 /* First see if we can handle the first operand, then the second. For
4132 the second operand, we know *CVAL1 can't be zero. It must be that
4133 one side of the comparison is each of the values; test for the
4134 case where this isn't true by failing if the two operands
4137 if (operand_equal_p (TREE_OPERAND (arg
, 0),
4138 TREE_OPERAND (arg
, 1), 0))
4142 *cval1
= TREE_OPERAND (arg
, 0);
4143 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
4145 else if (*cval2
== 0)
4146 *cval2
= TREE_OPERAND (arg
, 0);
4147 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
4152 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
4154 else if (*cval2
== 0)
4155 *cval2
= TREE_OPERAND (arg
, 1);
4156 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
4168 /* ARG is a tree that is known to contain just arithmetic operations and
4169 comparisons. Evaluate the operations in the tree substituting NEW0 for
4170 any occurrence of OLD0 as an operand of a comparison and likewise for
4174 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
4175 tree old1
, tree new1
)
4177 tree type
= TREE_TYPE (arg
);
4178 enum tree_code code
= TREE_CODE (arg
);
4179 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4181 /* We can handle some of the tcc_expression cases here. */
4182 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4184 else if (tclass
== tcc_expression
4185 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
4186 tclass
= tcc_binary
;
4191 return fold_build1_loc (loc
, code
, type
,
4192 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4193 old0
, new0
, old1
, new1
));
4196 return fold_build2_loc (loc
, code
, type
,
4197 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4198 old0
, new0
, old1
, new1
),
4199 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4200 old0
, new0
, old1
, new1
));
4202 case tcc_expression
:
4206 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
4210 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
4214 return fold_build3_loc (loc
, code
, type
,
4215 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4216 old0
, new0
, old1
, new1
),
4217 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4218 old0
, new0
, old1
, new1
),
4219 eval_subst (loc
, TREE_OPERAND (arg
, 2),
4220 old0
, new0
, old1
, new1
));
4224 /* Fall through - ??? */
4226 case tcc_comparison
:
4228 tree arg0
= TREE_OPERAND (arg
, 0);
4229 tree arg1
= TREE_OPERAND (arg
, 1);
4231 /* We need to check both for exact equality and tree equality. The
4232 former will be true if the operand has a side-effect. In that
4233 case, we know the operand occurred exactly once. */
4235 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
4237 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
4240 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
4242 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
4245 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
4253 /* Return a tree for the case when the result of an expression is RESULT
4254 converted to TYPE and OMITTED was previously an operand of the expression
4255 but is now not needed (e.g., we folded OMITTED * 0).
4257 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4258 the conversion of RESULT to TYPE. */
4261 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
4263 tree t
= fold_convert_loc (loc
, type
, result
);
4265 /* If the resulting operand is an empty statement, just return the omitted
4266 statement casted to void. */
4267 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
4268 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
4269 fold_ignored_result (omitted
));
4271 if (TREE_SIDE_EFFECTS (omitted
))
4272 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4273 fold_ignored_result (omitted
), t
);
4275 return non_lvalue_loc (loc
, t
);
4278 /* Return a tree for the case when the result of an expression is RESULT
4279 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4280 of the expression but are now not needed.
4282 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4283 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4284 evaluated before OMITTED2. Otherwise, if neither has side effects,
4285 just do the conversion of RESULT to TYPE. */
4288 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
4289 tree omitted1
, tree omitted2
)
4291 tree t
= fold_convert_loc (loc
, type
, result
);
4293 if (TREE_SIDE_EFFECTS (omitted2
))
4294 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
4295 if (TREE_SIDE_EFFECTS (omitted1
))
4296 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
4298 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
4302 /* Return a simplified tree node for the truth-negation of ARG. This
4303 never alters ARG itself. We assume that ARG is an operation that
4304 returns a truth value (0 or 1).
4306 FIXME: one would think we would fold the result, but it causes
4307 problems with the dominator optimizer. */
4310 fold_truth_not_expr (location_t loc
, tree arg
)
4312 tree type
= TREE_TYPE (arg
);
4313 enum tree_code code
= TREE_CODE (arg
);
4314 location_t loc1
, loc2
;
4316 /* If this is a comparison, we can simply invert it, except for
4317 floating-point non-equality comparisons, in which case we just
4318 enclose a TRUTH_NOT_EXPR around what we have. */
4320 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4322 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4323 if (FLOAT_TYPE_P (op_type
)
4324 && flag_trapping_math
4325 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
4326 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
4329 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
4330 if (code
== ERROR_MARK
)
4333 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
4334 TREE_OPERAND (arg
, 1));
4335 copy_warning (ret
, arg
);
4342 return constant_boolean_node (integer_zerop (arg
), type
);
4344 case TRUTH_AND_EXPR
:
4345 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4346 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4347 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
4348 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4349 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4352 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4353 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4354 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
4355 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4356 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4358 case TRUTH_XOR_EXPR
:
4359 /* Here we can invert either operand. We invert the first operand
4360 unless the second operand is a TRUTH_NOT_EXPR in which case our
4361 result is the XOR of the first operand with the inside of the
4362 negation of the second operand. */
4364 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
4365 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
4366 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
4368 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
4369 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
4370 TREE_OPERAND (arg
, 1));
4372 case TRUTH_ANDIF_EXPR
:
4373 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4374 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4375 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
4376 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4377 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4379 case TRUTH_ORIF_EXPR
:
4380 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4381 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4382 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
4383 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4384 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4386 case TRUTH_NOT_EXPR
:
4387 return TREE_OPERAND (arg
, 0);
4391 tree arg1
= TREE_OPERAND (arg
, 1);
4392 tree arg2
= TREE_OPERAND (arg
, 2);
4394 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4395 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
4397 /* A COND_EXPR may have a throw as one operand, which
4398 then has void type. Just leave void operands
4400 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
4401 VOID_TYPE_P (TREE_TYPE (arg1
))
4402 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
4403 VOID_TYPE_P (TREE_TYPE (arg2
))
4404 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
4408 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4409 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4410 TREE_OPERAND (arg
, 0),
4411 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
4413 case NON_LVALUE_EXPR
:
4414 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4415 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
4418 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4419 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4424 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4425 return build1_loc (loc
, TREE_CODE (arg
), type
,
4426 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4429 if (!integer_onep (TREE_OPERAND (arg
, 1)))
4431 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
4434 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4436 case CLEANUP_POINT_EXPR
:
4437 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4438 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
4439 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4446 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4447 assume that ARG is an operation that returns a truth value (0 or 1
4448 for scalars, 0 or -1 for vectors). Return the folded expression if
4449 folding is successful. Otherwise, return NULL_TREE. */
4452 fold_invert_truthvalue (location_t loc
, tree arg
)
4454 tree type
= TREE_TYPE (arg
);
4455 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
4461 /* Return a simplified tree node for the truth-negation of ARG. This
4462 never alters ARG itself. We assume that ARG is an operation that
4463 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4466 invert_truthvalue_loc (location_t loc
, tree arg
)
4468 if (TREE_CODE (arg
) == ERROR_MARK
)
4471 tree type
= TREE_TYPE (arg
);
4472 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
4478 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4479 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4480 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4481 is the original memory reference used to preserve the alias set of
4485 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
4486 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
4487 int unsignedp
, int reversep
)
4489 tree result
, bftype
;
4491 /* Attempt not to lose the access path if possible. */
4492 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
4494 tree ninner
= TREE_OPERAND (orig_inner
, 0);
4496 poly_int64 nbitsize
, nbitpos
;
4498 int nunsignedp
, nreversep
, nvolatilep
= 0;
4499 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
4500 &noffset
, &nmode
, &nunsignedp
,
4501 &nreversep
, &nvolatilep
);
4503 && noffset
== NULL_TREE
4504 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
4514 alias_set_type iset
= get_alias_set (orig_inner
);
4515 if (iset
== 0 && get_alias_set (inner
) != iset
)
4516 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
4517 build_fold_addr_expr (inner
),
4518 build_int_cst (ptr_type_node
, 0));
4520 if (known_eq (bitpos
, 0) && !reversep
)
4522 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
4523 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
4524 || POINTER_TYPE_P (TREE_TYPE (inner
)))
4525 && tree_fits_shwi_p (size
)
4526 && tree_to_shwi (size
) == bitsize
)
4527 return fold_convert_loc (loc
, type
, inner
);
4531 if (TYPE_PRECISION (bftype
) != bitsize
4532 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
4533 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4535 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4536 bitsize_int (bitsize
), bitsize_int (bitpos
));
4537 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4540 result
= fold_convert_loc (loc
, type
, result
);
4545 /* Optimize a bit-field compare.
4547 There are two cases: First is a compare against a constant and the
4548 second is a comparison of two items where the fields are at the same
4549 bit position relative to the start of a chunk (byte, halfword, word)
4550 large enough to contain it. In these cases we can avoid the shift
4551 implicit in bitfield extractions.
4553 For constants, we emit a compare of the shifted constant with the
4554 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4555 compared. For two fields at the same position, we do the ANDs with the
4556 similar mask and compare the result of the ANDs.
4558 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4559 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4560 are the left and right operands of the comparison, respectively.
4562 If the optimization described above can be done, we return the resulting
4563 tree. Otherwise we return zero. */
4566 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4567 tree compare_type
, tree lhs
, tree rhs
)
4569 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4570 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4571 tree type
= TREE_TYPE (lhs
);
4573 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4574 machine_mode lmode
, rmode
;
4575 scalar_int_mode nmode
;
4576 int lunsignedp
, runsignedp
;
4577 int lreversep
, rreversep
;
4578 int lvolatilep
= 0, rvolatilep
= 0;
4579 tree linner
, rinner
= NULL_TREE
;
4583 /* Get all the information about the extractions being done. If the bit size
4584 is the same as the size of the underlying object, we aren't doing an
4585 extraction at all and so can do nothing. We also don't want to
4586 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4587 then will no longer be able to replace it. */
4588 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4589 &lunsignedp
, &lreversep
, &lvolatilep
);
4591 || !known_size_p (plbitsize
)
4592 || !plbitsize
.is_constant (&lbitsize
)
4593 || !plbitpos
.is_constant (&lbitpos
)
4594 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4596 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4601 rreversep
= lreversep
;
4604 /* If this is not a constant, we can only do something if bit positions,
4605 sizes, signedness and storage order are the same. */
4607 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4608 &runsignedp
, &rreversep
, &rvolatilep
);
4611 || maybe_ne (lbitpos
, rbitpos
)
4612 || maybe_ne (lbitsize
, rbitsize
)
4613 || lunsignedp
!= runsignedp
4614 || lreversep
!= rreversep
4616 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4621 /* Honor the C++ memory model and mimic what RTL expansion does. */
4622 poly_uint64 bitstart
= 0;
4623 poly_uint64 bitend
= 0;
4624 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4626 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4627 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4631 /* See if we can find a mode to refer to this field. We should be able to,
4632 but fail if we can't. */
4633 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4634 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4635 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4636 TYPE_ALIGN (TREE_TYPE (rinner
))),
4637 BITS_PER_WORD
, false, &nmode
))
4640 /* Set signed and unsigned types of the precision of this mode for the
4642 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4644 /* Compute the bit position and size for the new reference and our offset
4645 within it. If the new reference is the same size as the original, we
4646 won't optimize anything, so return zero. */
4647 nbitsize
= GET_MODE_BITSIZE (nmode
);
4648 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4650 if (nbitsize
== lbitsize
)
4653 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4654 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4656 /* Make the mask to be used against the extracted field. */
4657 mask
= build_int_cst_type (unsigned_type
, -1);
4658 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4659 mask
= const_binop (RSHIFT_EXPR
, mask
,
4660 size_int (nbitsize
- lbitsize
- lbitpos
));
4667 /* If not comparing with constant, just rework the comparison
4669 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4670 nbitsize
, nbitpos
, 1, lreversep
);
4671 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4672 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4673 nbitsize
, nbitpos
, 1, rreversep
);
4674 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4675 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4678 /* Otherwise, we are handling the constant case. See if the constant is too
4679 big for the field. Warn and return a tree for 0 (false) if so. We do
4680 this not only for its own sake, but to avoid having to test for this
4681 error case below. If we didn't, we might generate wrong code.
4683 For unsigned fields, the constant shifted right by the field length should
4684 be all zero. For signed fields, the high-order bits should agree with
4689 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4691 warning (0, "comparison is always %d due to width of bit-field",
4693 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4698 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4699 if (tem
!= 0 && tem
!= -1)
4701 warning (0, "comparison is always %d due to width of bit-field",
4703 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4710 /* Single-bit compares should always be against zero. */
4711 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4713 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4714 rhs
= build_int_cst (type
, 0);
4717 /* Make a new bitfield reference, shift the constant over the
4718 appropriate number of bits and mask it with the computed mask
4719 (in case this was a signed field). If we changed it, make a new one. */
4720 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4721 nbitsize
, nbitpos
, 1, lreversep
);
4723 rhs
= const_binop (BIT_AND_EXPR
,
4724 const_binop (LSHIFT_EXPR
,
4725 fold_convert_loc (loc
, unsigned_type
, rhs
),
4726 size_int (lbitpos
)),
4729 lhs
= build2_loc (loc
, code
, compare_type
,
4730 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4734 /* Subroutine for fold_truth_andor_1: decode a field reference.
4736 If EXP is a comparison reference, we return the innermost reference.
4738 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4739 set to the starting bit number.
4741 If the innermost field can be completely contained in a mode-sized
4742 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4744 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4745 otherwise it is not changed.
4747 *PUNSIGNEDP is set to the signedness of the field.
4749 *PREVERSEP is set to the storage order of the field.
4751 *PMASK is set to the mask used. This is either contained in a
4752 BIT_AND_EXPR or derived from the width of the field.
4754 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4756 Return 0 if this is not a component reference or is one that we can't
4757 do anything with. */
4760 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4761 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4762 int *punsignedp
, int *preversep
, int *pvolatilep
,
4763 tree
*pmask
, tree
*pand_mask
)
4766 tree outer_type
= 0;
4768 tree mask
, inner
, offset
;
4770 unsigned int precision
;
4772 /* All the optimizations using this function assume integer fields.
4773 There are problems with FP fields since the type_for_size call
4774 below can fail for, e.g., XFmode. */
4775 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4778 /* We are interested in the bare arrangement of bits, so strip everything
4779 that doesn't affect the machine mode. However, record the type of the
4780 outermost expression if it may matter below. */
4781 if (CONVERT_EXPR_P (exp
)
4782 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4783 outer_type
= TREE_TYPE (exp
);
4786 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4788 and_mask
= TREE_OPERAND (exp
, 1);
4789 exp
= TREE_OPERAND (exp
, 0);
4790 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4791 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4795 poly_int64 poly_bitsize
, poly_bitpos
;
4796 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4797 pmode
, punsignedp
, preversep
, pvolatilep
);
4798 if ((inner
== exp
&& and_mask
== 0)
4799 || !poly_bitsize
.is_constant (pbitsize
)
4800 || !poly_bitpos
.is_constant (pbitpos
)
4803 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4804 /* Reject out-of-bound accesses (PR79731). */
4805 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4806 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4807 *pbitpos
+ *pbitsize
) < 0))
4810 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4811 if (unsigned_type
== NULL_TREE
)
4816 /* If the number of bits in the reference is the same as the bitsize of
4817 the outer type, then the outer type gives the signedness. Otherwise
4818 (in case of a small bitfield) the signedness is unchanged. */
4819 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4820 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4822 /* Compute the mask to access the bitfield. */
4823 precision
= TYPE_PRECISION (unsigned_type
);
4825 mask
= build_int_cst_type (unsigned_type
, -1);
4827 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4828 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4830 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4832 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4833 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4836 *pand_mask
= and_mask
;
4840 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4841 bit positions and MASK is SIGNED. */
4844 all_ones_mask_p (const_tree mask
, unsigned int size
)
4846 tree type
= TREE_TYPE (mask
);
4847 unsigned int precision
= TYPE_PRECISION (type
);
4849 /* If this function returns true when the type of the mask is
4850 UNSIGNED, then there will be errors. In particular see
4851 gcc.c-torture/execute/990326-1.c. There does not appear to be
4852 any documentation paper trail as to why this is so. But the pre
4853 wide-int worked with that restriction and it has been preserved
4855 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4858 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
4861 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4862 represents the sign bit of EXP's type. If EXP represents a sign
4863 or zero extension, also test VAL against the unextended type.
4864 The return value is the (sub)expression whose sign bit is VAL,
4865 or NULL_TREE otherwise. */
4868 sign_bit_p (tree exp
, const_tree val
)
4873 /* Tree EXP must have an integral type. */
4874 t
= TREE_TYPE (exp
);
4875 if (! INTEGRAL_TYPE_P (t
))
4878 /* Tree VAL must be an integer constant. */
4879 if (TREE_CODE (val
) != INTEGER_CST
4880 || TREE_OVERFLOW (val
))
4883 width
= TYPE_PRECISION (t
);
4884 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
4887 /* Handle extension from a narrower type. */
4888 if (TREE_CODE (exp
) == NOP_EXPR
4889 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4890 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4895 /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4896 operand is simple enough to be evaluated unconditionally. */
4899 simple_operand_p (const_tree exp
)
4901 /* Strip any conversions that don't change the machine mode. */
4904 return (CONSTANT_CLASS_P (exp
)
4905 || TREE_CODE (exp
) == SSA_NAME
4907 && ! TREE_ADDRESSABLE (exp
)
4908 && ! TREE_THIS_VOLATILE (exp
)
4909 && ! DECL_NONLOCAL (exp
)
4910 /* Don't regard global variables as simple. They may be
4911 allocated in ways unknown to the compiler (shared memory,
4912 #pragma weak, etc). */
4913 && ! TREE_PUBLIC (exp
)
4914 && ! DECL_EXTERNAL (exp
)
4915 /* Weakrefs are not safe to be read, since they can be NULL.
4916 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4917 have DECL_WEAK flag set. */
4918 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4919 /* Loading a static variable is unduly expensive, but global
4920 registers aren't expensive. */
4921 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4924 /* Determine if an operand is simple enough to be evaluated unconditionally.
4925 In addition to simple_operand_p, we assume that comparisons, conversions,
4926 and logic-not operations are simple, if their operands are simple, too. */
4929 simple_condition_p (tree exp
)
4931 enum tree_code code
;
4933 if (TREE_SIDE_EFFECTS (exp
) || generic_expr_could_trap_p (exp
))
4936 while (CONVERT_EXPR_P (exp
))
4937 exp
= TREE_OPERAND (exp
, 0);
4939 code
= TREE_CODE (exp
);
4941 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4942 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4943 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4945 if (code
== TRUTH_NOT_EXPR
)
4946 return simple_condition_p (TREE_OPERAND (exp
, 0));
4948 return simple_operand_p (exp
);
4952 /* The following functions are subroutines to fold_range_test and allow it to
4953 try to change a logical combination of comparisons into a range test.
4956 X == 2 || X == 3 || X == 4 || X == 5
4960 (unsigned) (X - 2) <= 3
4962 We describe each set of comparisons as being either inside or outside
4963 a range, using a variable named like IN_P, and then describe the
4964 range with a lower and upper bound. If one of the bounds is omitted,
4965 it represents either the highest or lowest value of the type.
4967 In the comments below, we represent a range by two numbers in brackets
4968 preceded by a "+" to designate being inside that range, or a "-" to
4969 designate being outside that range, so the condition can be inverted by
4970 flipping the prefix. An omitted bound is represented by a "-". For
4971 example, "- [-, 10]" means being outside the range starting at the lowest
4972 possible value and ending at 10, in other words, being greater than 10.
4973 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4976 We set up things so that the missing bounds are handled in a consistent
4977 manner so neither a missing bound nor "true" and "false" need to be
4978 handled using a special case. */
4980 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4981 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4982 and UPPER1_P are nonzero if the respective argument is an upper bound
4983 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4984 must be specified for a comparison. ARG1 will be converted to ARG0's
4985 type if both are specified. */
4988 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4989 tree arg1
, int upper1_p
)
4995 /* If neither arg represents infinity, do the normal operation.
4996 Else, if not a comparison, return infinity. Else handle the special
4997 comparison rules. Note that most of the cases below won't occur, but
4998 are handled for consistency. */
5000 if (arg0
!= 0 && arg1
!= 0)
5002 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
5003 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
5005 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
5008 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5011 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5012 for neither. In real maths, we cannot assume open ended ranges are
5013 the same. But, this is computer arithmetic, where numbers are finite.
5014 We can therefore make the transformation of any unbounded range with
5015 the value Z, Z being greater than any representable number. This permits
5016 us to treat unbounded ranges as equal. */
5017 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
5018 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
5022 result
= sgn0
== sgn1
;
5025 result
= sgn0
!= sgn1
;
5028 result
= sgn0
< sgn1
;
5031 result
= sgn0
<= sgn1
;
5034 result
= sgn0
> sgn1
;
5037 result
= sgn0
>= sgn1
;
5043 return constant_boolean_node (result
, type
);
5046 /* Helper routine for make_range. Perform one step for it, return
5047 new expression if the loop should continue or NULL_TREE if it should
5051 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
5052 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
5053 bool *strict_overflow_p
)
5055 tree arg0_type
= TREE_TYPE (arg0
);
5056 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
5057 int in_p
= *p_in_p
, n_in_p
;
5061 case TRUTH_NOT_EXPR
:
5062 /* We can only do something if the range is testing for zero. */
5063 if (low
== NULL_TREE
|| high
== NULL_TREE
5064 || ! integer_zerop (low
) || ! integer_zerop (high
))
5069 case EQ_EXPR
: case NE_EXPR
:
5070 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
5071 /* We can only do something if the range is testing for zero
5072 and if the second operand is an integer constant. Note that
5073 saying something is "in" the range we make is done by
5074 complementing IN_P since it will set in the initial case of
5075 being not equal to zero; "out" is leaving it alone. */
5076 if (low
== NULL_TREE
|| high
== NULL_TREE
5077 || ! integer_zerop (low
) || ! integer_zerop (high
)
5078 || TREE_CODE (arg1
) != INTEGER_CST
)
5083 case NE_EXPR
: /* - [c, c] */
5086 case EQ_EXPR
: /* + [c, c] */
5087 in_p
= ! in_p
, low
= high
= arg1
;
5089 case GT_EXPR
: /* - [-, c] */
5090 low
= 0, high
= arg1
;
5092 case GE_EXPR
: /* + [c, -] */
5093 in_p
= ! in_p
, low
= arg1
, high
= 0;
5095 case LT_EXPR
: /* - [c, -] */
5096 low
= arg1
, high
= 0;
5098 case LE_EXPR
: /* + [-, c] */
5099 in_p
= ! in_p
, low
= 0, high
= arg1
;
5105 /* If this is an unsigned comparison, we also know that EXP is
5106 greater than or equal to zero. We base the range tests we make
5107 on that fact, so we record it here so we can parse existing
5108 range tests. We test arg0_type since often the return type
5109 of, e.g. EQ_EXPR, is boolean. */
5110 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
5112 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
5114 build_int_cst (arg0_type
, 0),
5118 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
5120 /* If the high bound is missing, but we have a nonzero low
5121 bound, reverse the range so it goes from zero to the low bound
5123 if (high
== 0 && low
&& ! integer_zerop (low
))
5126 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
5127 build_int_cst (TREE_TYPE (low
), 1), 0);
5128 low
= build_int_cst (arg0_type
, 0);
5138 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5139 low and high are non-NULL, then normalize will DTRT. */
5140 if (!TYPE_UNSIGNED (arg0_type
)
5141 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5143 if (low
== NULL_TREE
)
5144 low
= TYPE_MIN_VALUE (arg0_type
);
5145 if (high
== NULL_TREE
)
5146 high
= TYPE_MAX_VALUE (arg0_type
);
5149 /* (-x) IN [a,b] -> x in [-b, -a] */
5150 n_low
= range_binop (MINUS_EXPR
, exp_type
,
5151 build_int_cst (exp_type
, 0),
5153 n_high
= range_binop (MINUS_EXPR
, exp_type
,
5154 build_int_cst (exp_type
, 0),
5156 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
5162 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
5163 build_int_cst (exp_type
, 1));
5167 if (TREE_CODE (arg1
) != INTEGER_CST
)
5170 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5171 move a constant to the other side. */
5172 if (!TYPE_UNSIGNED (arg0_type
)
5173 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5176 /* If EXP is signed, any overflow in the computation is undefined,
5177 so we don't worry about it so long as our computations on
5178 the bounds don't overflow. For unsigned, overflow is defined
5179 and this is exactly the right thing. */
5180 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5181 arg0_type
, low
, 0, arg1
, 0);
5182 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5183 arg0_type
, high
, 1, arg1
, 0);
5184 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
5185 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
5188 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5189 *strict_overflow_p
= true;
5192 /* Check for an unsigned range which has wrapped around the maximum
5193 value thus making n_high < n_low, and normalize it. */
5194 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
5196 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
5197 build_int_cst (TREE_TYPE (n_high
), 1), 0);
5198 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
5199 build_int_cst (TREE_TYPE (n_low
), 1), 0);
5201 /* If the range is of the form +/- [ x+1, x ], we won't
5202 be able to normalize it. But then, it represents the
5203 whole range or the empty set, so make it
5205 if (tree_int_cst_equal (n_low
, low
)
5206 && tree_int_cst_equal (n_high
, high
))
5212 low
= n_low
, high
= n_high
;
5220 case NON_LVALUE_EXPR
:
5221 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
5224 if (! INTEGRAL_TYPE_P (arg0_type
)
5225 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
5226 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
5229 n_low
= low
, n_high
= high
;
5232 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
5235 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
5237 /* If we're converting arg0 from an unsigned type, to exp,
5238 a signed type, we will be doing the comparison as unsigned.
5239 The tests above have already verified that LOW and HIGH
5242 So we have to ensure that we will handle large unsigned
5243 values the same way that the current signed bounds treat
5246 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
5250 /* For fixed-point modes, we need to pass the saturating flag
5251 as the 2nd parameter. */
5252 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
5254 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
5255 TYPE_SATURATING (arg0_type
));
5258 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
5260 /* A range without an upper bound is, naturally, unbounded.
5261 Since convert would have cropped a very large value, use
5262 the max value for the destination type. */
5264 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
5265 : TYPE_MAX_VALUE (arg0_type
);
5267 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
5268 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
5269 fold_convert_loc (loc
, arg0_type
,
5271 build_int_cst (arg0_type
, 1));
5273 /* If the low bound is specified, "and" the range with the
5274 range for which the original unsigned value will be
5278 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
5279 1, fold_convert_loc (loc
, arg0_type
,
5284 in_p
= (n_in_p
== in_p
);
5288 /* Otherwise, "or" the range with the range of the input
5289 that will be interpreted as negative. */
5290 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
5291 1, fold_convert_loc (loc
, arg0_type
,
5296 in_p
= (in_p
!= n_in_p
);
5300 /* Otherwise, if we are converting arg0 from signed type, to exp,
5301 an unsigned type, we will do the comparison as signed. If
5302 high is non-NULL, we punt above if it doesn't fit in the signed
5303 type, so if we get through here, +[-, high] or +[low, high] are
5304 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5305 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5306 high is not, the +[low, -] range is equivalent to union of
5307 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5308 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5309 low being 0, which should be treated as [-, -]. */
5310 else if (TYPE_UNSIGNED (exp_type
)
5311 && !TYPE_UNSIGNED (arg0_type
)
5315 if (integer_zerop (low
))
5319 n_high
= fold_build2_loc (loc
, PLUS_EXPR
, arg0_type
,
5320 n_low
, build_int_cst (arg0_type
, -1));
5321 n_low
= build_zero_cst (arg0_type
);
5336 /* Given EXP, a logical expression, set the range it is testing into
5337 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5338 actually being tested. *PLOW and *PHIGH will be made of the same
5339 type as the returned expression. If EXP is not a comparison, we
5340 will most likely not be returning a useful value and range. Set
5341 *STRICT_OVERFLOW_P to true if the return value is only valid
5342 because signed overflow is undefined; otherwise, do not change
5343 *STRICT_OVERFLOW_P. */
5346 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
5347 bool *strict_overflow_p
)
5349 enum tree_code code
;
5350 tree arg0
, arg1
= NULL_TREE
;
5351 tree exp_type
, nexp
;
5354 location_t loc
= EXPR_LOCATION (exp
);
5356 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5357 and see if we can refine the range. Some of the cases below may not
5358 happen, but it doesn't seem worth worrying about this. We "continue"
5359 the outer loop when we've changed something; otherwise we "break"
5360 the switch, which will "break" the while. */
5363 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
5367 code
= TREE_CODE (exp
);
5368 exp_type
= TREE_TYPE (exp
);
5371 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
5373 if (TREE_OPERAND_LENGTH (exp
) > 0)
5374 arg0
= TREE_OPERAND (exp
, 0);
5375 if (TREE_CODE_CLASS (code
) == tcc_binary
5376 || TREE_CODE_CLASS (code
) == tcc_comparison
5377 || (TREE_CODE_CLASS (code
) == tcc_expression
5378 && TREE_OPERAND_LENGTH (exp
) > 1))
5379 arg1
= TREE_OPERAND (exp
, 1);
5381 if (arg0
== NULL_TREE
)
5384 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
5385 &high
, &in_p
, strict_overflow_p
);
5386 if (nexp
== NULL_TREE
)
5391 /* If EXP is a constant, we can evaluate whether this is true or false. */
5392 if (TREE_CODE (exp
) == INTEGER_CST
)
5394 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
5396 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5402 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5406 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5407 a bitwise check i.e. when
5408 LOW == 0xXX...X00...0
5409 HIGH == 0xXX...X11...1
5410 Return corresponding mask in MASK and stem in VALUE. */
5413 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
5416 if (TREE_CODE (low
) != INTEGER_CST
5417 || TREE_CODE (high
) != INTEGER_CST
)
5420 unsigned prec
= TYPE_PRECISION (type
);
5421 wide_int lo
= wi::to_wide (low
, prec
);
5422 wide_int hi
= wi::to_wide (high
, prec
);
5424 wide_int end_mask
= lo
^ hi
;
5425 if ((end_mask
& (end_mask
+ 1)) != 0
5426 || (lo
& end_mask
) != 0)
5429 wide_int stem_mask
= ~end_mask
;
5430 wide_int stem
= lo
& stem_mask
;
5431 if (stem
!= (hi
& stem_mask
))
5434 *mask
= wide_int_to_tree (type
, stem_mask
);
5435 *value
= wide_int_to_tree (type
, stem
);
5440 /* Helper routine for build_range_check and match.pd. Return the type to
5441 perform the check or NULL if it shouldn't be optimized. */
5444 range_check_type (tree etype
)
5446 /* First make sure that arithmetics in this type is valid, then make sure
5447 that it wraps around. */
5448 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
5449 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
), 1);
5451 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_UNSIGNED (etype
))
5453 tree utype
, minv
, maxv
;
5455 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5456 for the type in question, as we rely on this here. */
5457 utype
= unsigned_type_for (etype
);
5458 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
5459 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
5460 build_int_cst (TREE_TYPE (maxv
), 1), 1);
5461 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
5463 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
5469 else if (POINTER_TYPE_P (etype
) || TREE_CODE (etype
) == OFFSET_TYPE
)
5470 etype
= unsigned_type_for (etype
);
5474 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5475 type, TYPE, return an expression to test if EXP is in (or out of, depending
5476 on IN_P) the range. Return 0 if the test couldn't be created. */
5479 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
5480 tree low
, tree high
)
5482 tree etype
= TREE_TYPE (exp
), mask
, value
;
5484 /* Disable this optimization for function pointer expressions
5485 on targets that require function pointer canonicalization. */
5486 if (targetm
.have_canonicalize_funcptr_for_compare ()
5487 && POINTER_TYPE_P (etype
)
5488 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype
)))
5493 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
5495 return invert_truthvalue_loc (loc
, value
);
5500 if (low
== 0 && high
== 0)
5501 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
5504 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
5505 fold_convert_loc (loc
, etype
, high
));
5508 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
5509 fold_convert_loc (loc
, etype
, low
));
5511 if (operand_equal_p (low
, high
, 0))
5512 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
5513 fold_convert_loc (loc
, etype
, low
));
5515 if (TREE_CODE (exp
) == BIT_AND_EXPR
5516 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
5517 return fold_build2_loc (loc
, EQ_EXPR
, type
,
5518 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
5522 if (integer_zerop (low
))
5524 if (! TYPE_UNSIGNED (etype
))
5526 etype
= unsigned_type_for (etype
);
5527 high
= fold_convert_loc (loc
, etype
, high
);
5528 exp
= fold_convert_loc (loc
, etype
, exp
);
5530 return build_range_check (loc
, type
, exp
, 1, 0, high
);
5533 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5534 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
5536 int prec
= TYPE_PRECISION (etype
);
5538 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
5540 if (TYPE_UNSIGNED (etype
))
5542 tree signed_etype
= signed_type_for (etype
);
5543 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
5545 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
5547 etype
= signed_etype
;
5548 exp
= fold_convert_loc (loc
, etype
, exp
);
5550 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
5551 build_int_cst (etype
, 0));
5555 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5556 This requires wrap-around arithmetics for the type of the expression. */
5557 etype
= range_check_type (etype
);
5558 if (etype
== NULL_TREE
)
5561 high
= fold_convert_loc (loc
, etype
, high
);
5562 low
= fold_convert_loc (loc
, etype
, low
);
5563 exp
= fold_convert_loc (loc
, etype
, exp
);
5565 value
= const_binop (MINUS_EXPR
, high
, low
);
5567 if (value
!= 0 && !TREE_OVERFLOW (value
))
5568 return build_range_check (loc
, type
,
5569 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5570 1, build_int_cst (etype
, 0), value
);
5575 /* Return the predecessor of VAL in its type, handling the infinite case. */
5578 range_predecessor (tree val
)
5580 tree type
= TREE_TYPE (val
);
5582 if (INTEGRAL_TYPE_P (type
)
5583 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5586 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5587 build_int_cst (TREE_TYPE (val
), 1), 0);
5590 /* Return the successor of VAL in its type, handling the infinite case. */
5593 range_successor (tree val
)
5595 tree type
= TREE_TYPE (val
);
5597 if (INTEGRAL_TYPE_P (type
)
5598 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5601 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5602 build_int_cst (TREE_TYPE (val
), 1), 0);
5605 /* Given two ranges, see if we can merge them into one. Return 1 if we
5606 can, 0 if we can't. Set the output range into the specified parameters. */
5609 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5610 tree high0
, int in1_p
, tree low1
, tree high1
)
5618 int lowequal
= ((low0
== 0 && low1
== 0)
5619 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5620 low0
, 0, low1
, 0)));
5621 int highequal
= ((high0
== 0 && high1
== 0)
5622 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5623 high0
, 1, high1
, 1)));
5625 /* Make range 0 be the range that starts first, or ends last if they
5626 start at the same value. Swap them if it isn't. */
5627 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5630 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5631 high1
, 1, high0
, 1))))
5633 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5634 tem
= low0
, low0
= low1
, low1
= tem
;
5635 tem
= high0
, high0
= high1
, high1
= tem
;
5638 /* If the second range is != high1 where high1 is the type maximum of
5639 the type, try first merging with < high1 range. */
5642 && TREE_CODE (low1
) == INTEGER_CST
5643 && (TREE_CODE (TREE_TYPE (low1
)) == INTEGER_TYPE
5644 || (TREE_CODE (TREE_TYPE (low1
)) == ENUMERAL_TYPE
5645 && known_eq (TYPE_PRECISION (TREE_TYPE (low1
)),
5646 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1
))))))
5647 && operand_equal_p (low1
, high1
, 0))
5649 if (tree_int_cst_equal (low1
, TYPE_MAX_VALUE (TREE_TYPE (low1
)))
5650 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5651 !in1_p
, NULL_TREE
, range_predecessor (low1
)))
5653 /* Similarly for the second range != low1 where low1 is the type minimum
5654 of the type, try first merging with > low1 range. */
5655 if (tree_int_cst_equal (low1
, TYPE_MIN_VALUE (TREE_TYPE (low1
)))
5656 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5657 !in1_p
, range_successor (low1
), NULL_TREE
))
5661 /* Now flag two cases, whether the ranges are disjoint or whether the
5662 second range is totally subsumed in the first. Note that the tests
5663 below are simplified by the ones above. */
5664 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5665 high0
, 1, low1
, 0));
5666 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5667 high1
, 1, high0
, 1));
5669 /* We now have four cases, depending on whether we are including or
5670 excluding the two ranges. */
5673 /* If they don't overlap, the result is false. If the second range
5674 is a subset it is the result. Otherwise, the range is from the start
5675 of the second to the end of the first. */
5677 in_p
= 0, low
= high
= 0;
5679 in_p
= 1, low
= low1
, high
= high1
;
5681 in_p
= 1, low
= low1
, high
= high0
;
5684 else if (in0_p
&& ! in1_p
)
5686 /* If they don't overlap, the result is the first range. If they are
5687 equal, the result is false. If the second range is a subset of the
5688 first, and the ranges begin at the same place, we go from just after
5689 the end of the second range to the end of the first. If the second
5690 range is not a subset of the first, or if it is a subset and both
5691 ranges end at the same place, the range starts at the start of the
5692 first range and ends just before the second range.
5693 Otherwise, we can't describe this as a single range. */
5695 in_p
= 1, low
= low0
, high
= high0
;
5696 else if (lowequal
&& highequal
)
5697 in_p
= 0, low
= high
= 0;
5698 else if (subset
&& lowequal
)
5700 low
= range_successor (high1
);
5705 /* We are in the weird situation where high0 > high1 but
5706 high1 has no successor. Punt. */
5710 else if (! subset
|| highequal
)
5713 high
= range_predecessor (low1
);
5717 /* low0 < low1 but low1 has no predecessor. Punt. */
5725 else if (! in0_p
&& in1_p
)
5727 /* If they don't overlap, the result is the second range. If the second
5728 is a subset of the first, the result is false. Otherwise,
5729 the range starts just after the first range and ends at the
5730 end of the second. */
5732 in_p
= 1, low
= low1
, high
= high1
;
5733 else if (subset
|| highequal
)
5734 in_p
= 0, low
= high
= 0;
5737 low
= range_successor (high0
);
5742 /* high1 > high0 but high0 has no successor. Punt. */
5750 /* The case where we are excluding both ranges. Here the complex case
5751 is if they don't overlap. In that case, the only time we have a
5752 range is if they are adjacent. If the second is a subset of the
5753 first, the result is the first. Otherwise, the range to exclude
5754 starts at the beginning of the first range and ends at the end of the
5758 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5759 range_successor (high0
),
5761 in_p
= 0, low
= low0
, high
= high1
;
5764 /* Canonicalize - [min, x] into - [-, x]. */
5765 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5766 switch (TREE_CODE (TREE_TYPE (low0
)))
5769 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5771 (TYPE_MODE (TREE_TYPE (low0
)))))
5775 if (tree_int_cst_equal (low0
,
5776 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5780 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5781 && integer_zerop (low0
))
5788 /* Canonicalize - [x, max] into - [x, -]. */
5789 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5790 switch (TREE_CODE (TREE_TYPE (high1
)))
5793 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5795 (TYPE_MODE (TREE_TYPE (high1
)))))
5799 if (tree_int_cst_equal (high1
,
5800 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5804 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5805 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5807 build_int_cst (TREE_TYPE (high1
), 1),
5815 /* The ranges might be also adjacent between the maximum and
5816 minimum values of the given type. For
5817 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5818 return + [x + 1, y - 1]. */
5819 if (low0
== 0 && high1
== 0)
5821 low
= range_successor (high0
);
5822 high
= range_predecessor (low1
);
5823 if (low
== 0 || high
== 0)
5833 in_p
= 0, low
= low0
, high
= high0
;
5835 in_p
= 0, low
= low0
, high
= high1
;
5838 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5843 /* Subroutine of fold, looking inside expressions of the form
5844 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5845 are the three operands of the COND_EXPR. This function is
5846 being used also to optimize A op B ? C : A, by reversing the
5849 Return a folded expression whose code is not a COND_EXPR
5850 anymore, or NULL_TREE if no folding opportunity is found. */
5853 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5854 enum tree_code comp_code
,
5855 tree arg00
, tree arg01
, tree arg1
, tree arg2
)
5857 tree arg1_type
= TREE_TYPE (arg1
);
5863 /* If we have A op 0 ? A : -A, consider applying the following
5866 A == 0? A : -A same as -A
5867 A != 0? A : -A same as A
5868 A >= 0? A : -A same as abs (A)
5869 A > 0? A : -A same as abs (A)
5870 A <= 0? A : -A same as -abs (A)
5871 A < 0? A : -A same as -abs (A)
5873 None of these transformations work for modes with signed
5874 zeros. If A is +/-0, the first two transformations will
5875 change the sign of the result (from +0 to -0, or vice
5876 versa). The last four will fix the sign of the result,
5877 even though the original expressions could be positive or
5878 negative, depending on the sign of A.
5880 Note that all these transformations are correct if A is
5881 NaN, since the two alternatives (A and -A) are also NaNs. */
5882 if (!HONOR_SIGNED_ZEROS (type
)
5883 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5884 ? real_zerop (arg01
)
5885 : integer_zerop (arg01
))
5886 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5887 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5888 /* In the case that A is of the form X-Y, '-A' (arg2) may
5889 have already been folded to Y-X, check for that. */
5890 || (TREE_CODE (arg1
) == MINUS_EXPR
5891 && TREE_CODE (arg2
) == MINUS_EXPR
5892 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5893 TREE_OPERAND (arg2
, 1), 0)
5894 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5895 TREE_OPERAND (arg2
, 0), 0))))
5900 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5901 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5904 return fold_convert_loc (loc
, type
, arg1
);
5907 if (flag_trapping_math
)
5912 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5914 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5915 return fold_convert_loc (loc
, type
, tem
);
5918 if (flag_trapping_math
)
5923 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5925 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
5926 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
5928 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
5929 is not, invokes UB both in abs and in the negation of it.
5930 So, use ABSU_EXPR instead. */
5931 tree utype
= unsigned_type_for (TREE_TYPE (arg1
));
5932 tem
= fold_build1_loc (loc
, ABSU_EXPR
, utype
, arg1
);
5933 tem
= negate_expr (tem
);
5934 return fold_convert_loc (loc
, type
, tem
);
5938 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5939 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5942 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5946 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5947 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5948 both transformations are correct when A is NaN: A != 0
5949 is then true, and A == 0 is false. */
5951 if (!HONOR_SIGNED_ZEROS (type
)
5952 && integer_zerop (arg01
) && integer_zerop (arg2
))
5954 if (comp_code
== NE_EXPR
)
5955 return fold_convert_loc (loc
, type
, arg1
);
5956 else if (comp_code
== EQ_EXPR
)
5957 return build_zero_cst (type
);
5960 /* Try some transformations of A op B ? A : B.
5962 A == B? A : B same as B
5963 A != B? A : B same as A
5964 A >= B? A : B same as max (A, B)
5965 A > B? A : B same as max (B, A)
5966 A <= B? A : B same as min (A, B)
5967 A < B? A : B same as min (B, A)
5969 As above, these transformations don't work in the presence
5970 of signed zeros. For example, if A and B are zeros of
5971 opposite sign, the first two transformations will change
5972 the sign of the result. In the last four, the original
5973 expressions give different results for (A=+0, B=-0) and
5974 (A=-0, B=+0), but the transformed expressions do not.
5976 The first two transformations are correct if either A or B
5977 is a NaN. In the first transformation, the condition will
5978 be false, and B will indeed be chosen. In the case of the
5979 second transformation, the condition A != B will be true,
5980 and A will be chosen.
5982 The conversions to max() and min() are not correct if B is
5983 a number and A is not. The conditions in the original
5984 expressions will be false, so all four give B. The min()
5985 and max() versions would give a NaN instead. */
5986 if (!HONOR_SIGNED_ZEROS (type
)
5987 && operand_equal_for_comparison_p (arg01
, arg2
)
5988 /* Avoid these transformations if the COND_EXPR may be used
5989 as an lvalue in the C++ front-end. PR c++/19199. */
5991 || VECTOR_TYPE_P (type
)
5992 || (! lang_GNU_CXX ()
5993 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5994 || ! maybe_lvalue_p (arg1
)
5995 || ! maybe_lvalue_p (arg2
)))
5997 tree comp_op0
= arg00
;
5998 tree comp_op1
= arg01
;
5999 tree comp_type
= TREE_TYPE (comp_op0
);
6004 return fold_convert_loc (loc
, type
, arg2
);
6006 return fold_convert_loc (loc
, type
, arg1
);
6011 /* In C++ a ?: expression can be an lvalue, so put the
6012 operand which will be used if they are equal first
6013 so that we can convert this back to the
6014 corresponding COND_EXPR. */
6015 if (!HONOR_NANS (arg1
))
6017 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6018 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6019 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
6020 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
6021 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
6022 comp_op1
, comp_op0
);
6023 return fold_convert_loc (loc
, type
, tem
);
6030 if (!HONOR_NANS (arg1
))
6032 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6033 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6034 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
6035 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
6036 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
6037 comp_op1
, comp_op0
);
6038 return fold_convert_loc (loc
, type
, tem
);
6042 if (!HONOR_NANS (arg1
))
6043 return fold_convert_loc (loc
, type
, arg2
);
6046 if (!HONOR_NANS (arg1
))
6047 return fold_convert_loc (loc
, type
, arg1
);
6050 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
6060 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6061 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6062 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6066 /* EXP is some logical combination of boolean tests. See if we can
6067 merge it into some range test. Return the new tree if so. */
6070 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
6073 int or_op
= (code
== TRUTH_ORIF_EXPR
6074 || code
== TRUTH_OR_EXPR
);
6075 int in0_p
, in1_p
, in_p
;
6076 tree low0
, low1
, low
, high0
, high1
, high
;
6077 bool strict_overflow_p
= false;
6079 const char * const warnmsg
= G_("assuming signed overflow does not occur "
6080 "when simplifying range test");
6082 if (!INTEGRAL_TYPE_P (type
))
6085 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
6086 /* If op0 is known true or false and this is a short-circuiting
6087 operation we must not merge with op1 since that makes side-effects
6088 unconditional. So special-case this. */
6090 && ((code
== TRUTH_ORIF_EXPR
&& in0_p
)
6091 || (code
== TRUTH_ANDIF_EXPR
&& !in0_p
)))
6093 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
6095 /* If this is an OR operation, invert both sides; we will invert
6096 again at the end. */
6098 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
6100 /* If both expressions are the same, if we can merge the ranges, and we
6101 can build the range test, return it or it inverted. If one of the
6102 ranges is always true or always false, consider it to be the same
6103 expression as the other. */
6104 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
6105 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
6107 && (tem
= (build_range_check (loc
, type
,
6109 : rhs
!= 0 ? rhs
: integer_zero_node
,
6110 in_p
, low
, high
))) != 0)
6112 if (strict_overflow_p
)
6113 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
6114 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
6117 /* On machines where the branch cost is expensive, if this is a
6118 short-circuited branch and the underlying object on both sides
6119 is the same, make a non-short-circuit operation. */
6120 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
6121 if (param_logical_op_non_short_circuit
!= -1)
6122 logical_op_non_short_circuit
6123 = param_logical_op_non_short_circuit
;
6124 if (logical_op_non_short_circuit
6125 && !sanitize_coverage_p ()
6126 && lhs
!= 0 && rhs
!= 0
6127 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6128 && operand_equal_p (lhs
, rhs
, 0))
6130 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6131 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6132 which cases we can't do this. */
6133 if (simple_operand_p (lhs
))
6134 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6135 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6138 else if (!lang_hooks
.decls
.global_bindings_p ()
6139 && !CONTAINS_PLACEHOLDER_P (lhs
))
6141 tree common
= save_expr (lhs
);
6143 if ((lhs
= build_range_check (loc
, type
, common
,
6144 or_op
? ! in0_p
: in0_p
,
6146 && (rhs
= build_range_check (loc
, type
, common
,
6147 or_op
? ! in1_p
: in1_p
,
6150 if (strict_overflow_p
)
6151 fold_overflow_warning (warnmsg
,
6152 WARN_STRICT_OVERFLOW_COMPARISON
);
6153 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6154 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6163 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6164 bit value. Arrange things so the extra bits will be set to zero if and
6165 only if C is signed-extended to its full width. If MASK is nonzero,
6166 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6169 unextend (tree c
, int p
, int unsignedp
, tree mask
)
6171 tree type
= TREE_TYPE (c
);
6172 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
6175 if (p
== modesize
|| unsignedp
)
6178 /* We work by getting just the sign bit into the low-order bit, then
6179 into the high-order bit, then sign-extend. We then XOR that value
6181 temp
= build_int_cst (TREE_TYPE (c
),
6182 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
6184 /* We must use a signed type in order to get an arithmetic right shift.
6185 However, we must also avoid introducing accidental overflows, so that
6186 a subsequent call to integer_zerop will work. Hence we must
6187 do the type conversion here. At this point, the constant is either
6188 zero or one, and the conversion to a signed type can never overflow.
6189 We could get an overflow if this conversion is done anywhere else. */
6190 if (TYPE_UNSIGNED (type
))
6191 temp
= fold_convert (signed_type_for (type
), temp
);
6193 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
6194 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
6196 temp
= const_binop (BIT_AND_EXPR
, temp
,
6197 fold_convert (TREE_TYPE (c
), mask
));
6198 /* If necessary, convert the type back to match the type of C. */
6199 if (TYPE_UNSIGNED (type
))
6200 temp
= fold_convert (type
, temp
);
6202 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
6205 /* For an expression that has the form
6209 we can drop one of the inner expressions and simplify to
6213 LOC is the location of the resulting expression. OP is the inner
6214 logical operation; the left-hand side in the examples above, while CMPOP
6215 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6216 removing a condition that guards another, as in
6217 (A != NULL && A->...) || A == NULL
6218 which we must not transform. If RHS_ONLY is true, only eliminate the
6219 right-most operand of the inner logical operation. */
6222 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
6225 tree type
= TREE_TYPE (cmpop
);
6226 enum tree_code code
= TREE_CODE (cmpop
);
6227 enum tree_code truthop_code
= TREE_CODE (op
);
6228 tree lhs
= TREE_OPERAND (op
, 0);
6229 tree rhs
= TREE_OPERAND (op
, 1);
6230 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
6231 enum tree_code rhs_code
= TREE_CODE (rhs
);
6232 enum tree_code lhs_code
= TREE_CODE (lhs
);
6233 enum tree_code inv_code
;
6235 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
6238 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
6241 if (rhs_code
== truthop_code
)
6243 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
6244 if (newrhs
!= NULL_TREE
)
6247 rhs_code
= TREE_CODE (rhs
);
6250 if (lhs_code
== truthop_code
&& !rhs_only
)
6252 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
6253 if (newlhs
!= NULL_TREE
)
6256 lhs_code
= TREE_CODE (lhs
);
6260 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
6261 if (inv_code
== rhs_code
6262 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6263 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6265 if (!rhs_only
&& inv_code
== lhs_code
6266 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6267 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6269 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
6270 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
6275 /* Find ways of folding logical expressions of LHS and RHS:
6276 Try to merge two comparisons to the same innermost item.
6277 Look for range tests like "ch >= '0' && ch <= '9'".
6278 Look for combinations of simple terms on machines with expensive branches
6279 and evaluate the RHS unconditionally.
6281 For example, if we have p->a == 2 && p->b == 4 and we can make an
6282 object large enough to span both A and B, we can do this with a comparison
6283 against the object ANDed with the a mask.
6285 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6286 operations to do this with one comparison.
6288 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6289 function and the one above.
6291 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6292 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6294 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6297 We return the simplified tree or 0 if no optimization is possible. */
6300 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
6303 /* If this is the "or" of two comparisons, we can do something if
6304 the comparisons are NE_EXPR. If this is the "and", we can do something
6305 if the comparisons are EQ_EXPR. I.e.,
6306 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6308 WANTED_CODE is this operation code. For single bit fields, we can
6309 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6310 comparison for one-bit fields. */
6312 enum tree_code wanted_code
;
6313 enum tree_code lcode
, rcode
;
6314 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
6315 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
6316 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
6317 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
6318 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
6319 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
6320 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
6321 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
6322 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
6323 scalar_int_mode lnmode
, rnmode
;
6324 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
6325 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
6326 tree l_const
, r_const
;
6327 tree lntype
, rntype
, result
;
6328 HOST_WIDE_INT first_bit
, end_bit
;
6331 /* Start by getting the comparison codes. Fail if anything is volatile.
6332 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6333 it were surrounded with a NE_EXPR. */
6335 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
6338 lcode
= TREE_CODE (lhs
);
6339 rcode
= TREE_CODE (rhs
);
6341 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
6343 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
6344 build_int_cst (TREE_TYPE (lhs
), 0));
6348 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
6350 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
6351 build_int_cst (TREE_TYPE (rhs
), 0));
6355 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
6356 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
6359 ll_arg
= TREE_OPERAND (lhs
, 0);
6360 lr_arg
= TREE_OPERAND (lhs
, 1);
6361 rl_arg
= TREE_OPERAND (rhs
, 0);
6362 rr_arg
= TREE_OPERAND (rhs
, 1);
6364 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6365 if (simple_operand_p (ll_arg
)
6366 && simple_operand_p (lr_arg
))
6368 if (operand_equal_p (ll_arg
, rl_arg
, 0)
6369 && operand_equal_p (lr_arg
, rr_arg
, 0))
6371 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
6372 truth_type
, ll_arg
, lr_arg
);
6376 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
6377 && operand_equal_p (lr_arg
, rl_arg
, 0))
6379 result
= combine_comparisons (loc
, code
, lcode
,
6380 swap_tree_comparison (rcode
),
6381 truth_type
, ll_arg
, lr_arg
);
6387 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
6388 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
6390 /* If the RHS can be evaluated unconditionally and its operands are
6391 simple, it wins to evaluate the RHS unconditionally on machines
6392 with expensive branches. In this case, this isn't a comparison
6393 that can be merged. */
6395 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
6397 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
6398 && simple_operand_p (rl_arg
)
6399 && simple_operand_p (rr_arg
))
6401 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6402 if (code
== TRUTH_OR_EXPR
6403 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
6404 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
6405 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6406 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6407 return build2_loc (loc
, NE_EXPR
, truth_type
,
6408 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6410 build_int_cst (TREE_TYPE (ll_arg
), 0));
6412 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6413 if (code
== TRUTH_AND_EXPR
6414 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
6415 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
6416 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6417 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6418 return build2_loc (loc
, EQ_EXPR
, truth_type
,
6419 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6421 build_int_cst (TREE_TYPE (ll_arg
), 0));
6424 /* See if the comparisons can be merged. Then get all the parameters for
6427 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
6428 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
6431 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
6433 ll_inner
= decode_field_reference (loc
, &ll_arg
,
6434 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
6435 &ll_unsignedp
, &ll_reversep
, &volatilep
,
6436 &ll_mask
, &ll_and_mask
);
6437 lr_inner
= decode_field_reference (loc
, &lr_arg
,
6438 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
6439 &lr_unsignedp
, &lr_reversep
, &volatilep
,
6440 &lr_mask
, &lr_and_mask
);
6441 rl_inner
= decode_field_reference (loc
, &rl_arg
,
6442 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
6443 &rl_unsignedp
, &rl_reversep
, &volatilep
,
6444 &rl_mask
, &rl_and_mask
);
6445 rr_inner
= decode_field_reference (loc
, &rr_arg
,
6446 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
6447 &rr_unsignedp
, &rr_reversep
, &volatilep
,
6448 &rr_mask
, &rr_and_mask
);
6450 /* It must be true that the inner operation on the lhs of each
6451 comparison must be the same if we are to be able to do anything.
6452 Then see if we have constants. If not, the same must be true for
6455 || ll_reversep
!= rl_reversep
6456 || ll_inner
== 0 || rl_inner
== 0
6457 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
6460 if (TREE_CODE (lr_arg
) == INTEGER_CST
6461 && TREE_CODE (rr_arg
) == INTEGER_CST
)
6463 l_const
= lr_arg
, r_const
= rr_arg
;
6464 lr_reversep
= ll_reversep
;
6466 else if (lr_reversep
!= rr_reversep
6467 || lr_inner
== 0 || rr_inner
== 0
6468 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
6471 l_const
= r_const
= 0;
6473 /* If either comparison code is not correct for our logical operation,
6474 fail. However, we can convert a one-bit comparison against zero into
6475 the opposite comparison against that bit being set in the field. */
6477 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
6478 if (lcode
!= wanted_code
)
6480 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
6482 /* Make the left operand unsigned, since we are only interested
6483 in the value of one bit. Otherwise we are doing the wrong
6492 /* This is analogous to the code for l_const above. */
6493 if (rcode
!= wanted_code
)
6495 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
6504 /* See if we can find a mode that contains both fields being compared on
6505 the left. If we can't, fail. Otherwise, update all constants and masks
6506 to be relative to a field of that size. */
6507 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
6508 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
6509 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6510 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
6511 volatilep
, &lnmode
))
6514 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
6515 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
6516 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
6517 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
6519 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6521 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
6522 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
6525 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
6526 size_int (xll_bitpos
));
6527 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
6528 size_int (xrl_bitpos
));
6529 if (ll_mask
== NULL_TREE
|| rl_mask
== NULL_TREE
)
6534 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
6535 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
6536 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
6537 if (l_const
== NULL_TREE
)
6539 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
6540 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6543 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6545 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6550 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
6551 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
6552 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
6553 if (r_const
== NULL_TREE
)
6555 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
6556 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6559 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6561 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6565 /* If the right sides are not constant, do the same for it. Also,
6566 disallow this optimization if a size, signedness or storage order
6567 mismatch occurs between the left and right sides. */
6570 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
6571 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
6572 || ll_reversep
!= lr_reversep
6573 /* Make sure the two fields on the right
6574 correspond to the left without being swapped. */
6575 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
6578 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
6579 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
6580 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6581 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
6582 volatilep
, &rnmode
))
6585 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
6586 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
6587 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
6588 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
6590 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6592 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
6593 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
6596 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6598 size_int (xlr_bitpos
));
6599 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6601 size_int (xrr_bitpos
));
6602 if (lr_mask
== NULL_TREE
|| rr_mask
== NULL_TREE
)
6605 /* Make a mask that corresponds to both fields being compared.
6606 Do this for both items being compared. If the operands are the
6607 same size and the bits being compared are in the same position
6608 then we can do this by masking both and comparing the masked
6610 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6611 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
6612 if (lnbitsize
== rnbitsize
6613 && xll_bitpos
== xlr_bitpos
6617 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6618 lntype
, lnbitsize
, lnbitpos
,
6619 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6620 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6621 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6623 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6624 rntype
, rnbitsize
, rnbitpos
,
6625 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6626 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6627 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6629 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6632 /* There is still another way we can do something: If both pairs of
6633 fields being compared are adjacent, we may be able to make a wider
6634 field containing them both.
6636 Note that we still must mask the lhs/rhs expressions. Furthermore,
6637 the mask must be shifted to account for the shift done by
6638 make_bit_field_ref. */
6639 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6640 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6641 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6642 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6650 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6651 ll_bitsize
+ rl_bitsize
,
6652 MIN (ll_bitpos
, rl_bitpos
),
6653 ll_unsignedp
, ll_reversep
);
6654 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6655 lr_bitsize
+ rr_bitsize
,
6656 MIN (lr_bitpos
, rr_bitpos
),
6657 lr_unsignedp
, lr_reversep
);
6659 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6660 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6661 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6662 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6663 if (ll_mask
== NULL_TREE
|| lr_mask
== NULL_TREE
)
6666 /* Convert to the smaller type before masking out unwanted bits. */
6668 if (lntype
!= rntype
)
6670 if (lnbitsize
> rnbitsize
)
6672 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6673 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6676 else if (lnbitsize
< rnbitsize
)
6678 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6679 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6684 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6685 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6687 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6688 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6690 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6696 /* Handle the case of comparisons with constants. If there is something in
6697 common between the masks, those bits of the constants must be the same.
6698 If not, the condition is always false. Test for this to avoid generating
6699 incorrect code below. */
6700 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6701 if (! integer_zerop (result
)
6702 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6703 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6705 if (wanted_code
== NE_EXPR
)
6707 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6708 return constant_boolean_node (true, truth_type
);
6712 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6713 return constant_boolean_node (false, truth_type
);
6720 /* Construct the expression we will return. First get the component
6721 reference we will make. Unless the mask is all ones the width of
6722 that field, perform the mask operation. Then compare with the
6724 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6725 lntype
, lnbitsize
, lnbitpos
,
6726 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6728 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6729 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6730 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6732 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6733 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6736 /* T is an integer expression that is being multiplied, divided, or taken a
6737 modulus (CODE says which and what kind of divide or modulus) by a
6738 constant C. See if we can eliminate that operation by folding it with
6739 other operations already in T. WIDE_TYPE, if non-null, is a type that
6740 should be used for the computation if wider than our type.
6742 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6743 (X * 2) + (Y * 4). We must, however, be assured that either the original
6744 expression would not overflow or that overflow is undefined for the type
6745 in the language in question.
6747 If we return a non-null expression, it is an equivalent form of the
6748 original computation, but need not be in the original type.
6750 We set *STRICT_OVERFLOW_P to true if the return values depends on
6751 signed overflow being undefined. Otherwise we do not change
6752 *STRICT_OVERFLOW_P. */
6755 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6756 bool *strict_overflow_p
)
6758 /* To avoid exponential search depth, refuse to allow recursion past
6759 three levels. Beyond that (1) it's highly unlikely that we'll find
6760 something interesting and (2) we've probably processed it before
6761 when we built the inner expression. */
6770 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6777 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6778 bool *strict_overflow_p
)
6780 tree type
= TREE_TYPE (t
);
6781 enum tree_code tcode
= TREE_CODE (t
);
6782 tree ctype
= (wide_type
!= 0
6783 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6784 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6785 ? wide_type
: type
);
6787 int same_p
= tcode
== code
;
6788 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6789 bool sub_strict_overflow_p
;
6791 /* Don't deal with constants of zero here; they confuse the code below. */
6792 if (integer_zerop (c
))
6795 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6796 op0
= TREE_OPERAND (t
, 0);
6798 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6799 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6801 /* Note that we need not handle conditional operations here since fold
6802 already handles those cases. So just do arithmetic here. */
6806 /* For a constant, we can always simplify if we are a multiply
6807 or (for divide and modulus) if it is a multiple of our constant. */
6808 if (code
== MULT_EXPR
6809 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6812 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6813 fold_convert (ctype
, c
));
6814 /* If the multiplication overflowed, we lost information on it.
6815 See PR68142 and PR69845. */
6816 if (TREE_OVERFLOW (tem
))
6822 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6823 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
6825 /* If op0 is an expression ... */
6826 if ((COMPARISON_CLASS_P (op0
)
6827 || UNARY_CLASS_P (op0
)
6828 || BINARY_CLASS_P (op0
)
6829 || VL_EXP_CLASS_P (op0
)
6830 || EXPRESSION_CLASS_P (op0
))
6831 /* ... and has wrapping overflow, and its type is smaller
6832 than ctype, then we cannot pass through as widening. */
6833 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
6834 && (TYPE_PRECISION (ctype
)
6835 > TYPE_PRECISION (TREE_TYPE (op0
))))
6836 /* ... or this is a truncation (t is narrower than op0),
6837 then we cannot pass through this narrowing. */
6838 || (TYPE_PRECISION (type
)
6839 < TYPE_PRECISION (TREE_TYPE (op0
)))
6840 /* ... or signedness changes for division or modulus,
6841 then we cannot pass through this conversion. */
6842 || (code
!= MULT_EXPR
6843 && (TYPE_UNSIGNED (ctype
)
6844 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6845 /* ... or has undefined overflow while the converted to
6846 type has not, we cannot do the operation in the inner type
6847 as that would introduce undefined overflow. */
6848 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
6849 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6852 /* Pass the constant down and see if we can make a simplification. If
6853 we can, replace this expression with the inner simplification for
6854 possible later conversion to our or some other type. */
6855 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6856 && TREE_CODE (t2
) == INTEGER_CST
6857 && !TREE_OVERFLOW (t2
)
6858 && (t1
= extract_muldiv (op0
, t2
, code
,
6859 code
== MULT_EXPR
? ctype
: NULL_TREE
,
6860 strict_overflow_p
)) != 0)
6865 /* If widening the type changes it from signed to unsigned, then we
6866 must avoid building ABS_EXPR itself as unsigned. */
6867 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6869 tree cstype
= (*signed_type_for
) (ctype
);
6870 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6873 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6874 return fold_convert (ctype
, t1
);
6878 /* If the constant is negative, we cannot simplify this. */
6879 if (tree_int_cst_sgn (c
) == -1)
6883 /* For division and modulus, type can't be unsigned, as e.g.
6884 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6885 For signed types, even with wrapping overflow, this is fine. */
6886 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6888 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6890 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6893 case MIN_EXPR
: case MAX_EXPR
:
6894 /* If widening the type changes the signedness, then we can't perform
6895 this optimization as that changes the result. */
6896 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6899 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6900 sub_strict_overflow_p
= false;
6901 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6902 &sub_strict_overflow_p
)) != 0
6903 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6904 &sub_strict_overflow_p
)) != 0)
6906 if (tree_int_cst_sgn (c
) < 0)
6907 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6908 if (sub_strict_overflow_p
)
6909 *strict_overflow_p
= true;
6910 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6911 fold_convert (ctype
, t2
));
6915 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6916 /* If the second operand is constant, this is a multiplication
6917 or floor division, by a power of two, so we can treat it that
6918 way unless the multiplier or divisor overflows. Signed
6919 left-shift overflow is implementation-defined rather than
6920 undefined in C90, so do not convert signed left shift into
6922 if (TREE_CODE (op1
) == INTEGER_CST
6923 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6924 /* const_binop may not detect overflow correctly,
6925 so check for it explicitly here. */
6926 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
6928 && (t1
= fold_convert (ctype
,
6929 const_binop (LSHIFT_EXPR
, size_one_node
,
6931 && !TREE_OVERFLOW (t1
))
6932 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6933 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6935 fold_convert (ctype
, op0
),
6937 c
, code
, wide_type
, strict_overflow_p
);
6940 case PLUS_EXPR
: case MINUS_EXPR
:
6941 /* See if we can eliminate the operation on both sides. If we can, we
6942 can return a new PLUS or MINUS. If we can't, the only remaining
6943 cases where we can do anything are if the second operand is a
6945 sub_strict_overflow_p
= false;
6946 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6947 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6948 if (t1
!= 0 && t2
!= 0
6949 && TYPE_OVERFLOW_WRAPS (ctype
)
6950 && (code
== MULT_EXPR
6951 /* If not multiplication, we can only do this if both operands
6952 are divisible by c. */
6953 || (multiple_of_p (ctype
, op0
, c
)
6954 && multiple_of_p (ctype
, op1
, c
))))
6956 if (sub_strict_overflow_p
)
6957 *strict_overflow_p
= true;
6958 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6959 fold_convert (ctype
, t2
));
6962 /* If this was a subtraction, negate OP1 and set it to be an addition.
6963 This simplifies the logic below. */
6964 if (tcode
== MINUS_EXPR
)
6966 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6967 /* If OP1 was not easily negatable, the constant may be OP0. */
6968 if (TREE_CODE (op0
) == INTEGER_CST
)
6970 std::swap (op0
, op1
);
6975 if (TREE_CODE (op1
) != INTEGER_CST
)
6978 /* If either OP1 or C are negative, this optimization is not safe for
6979 some of the division and remainder types while for others we need
6980 to change the code. */
6981 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6983 if (code
== CEIL_DIV_EXPR
)
6984 code
= FLOOR_DIV_EXPR
;
6985 else if (code
== FLOOR_DIV_EXPR
)
6986 code
= CEIL_DIV_EXPR
;
6987 else if (code
!= MULT_EXPR
6988 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6992 /* If it's a multiply or a division/modulus operation of a multiple
6993 of our constant, do the operation and verify it doesn't overflow. */
6994 if (code
== MULT_EXPR
6995 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6998 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6999 fold_convert (ctype
, c
));
7000 /* We allow the constant to overflow with wrapping semantics. */
7002 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
7008 /* If we have an unsigned type, we cannot widen the operation since it
7009 will change the result if the original computation overflowed. */
7010 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
7013 /* The last case is if we are a multiply. In that case, we can
7014 apply the distributive law to commute the multiply and addition
7015 if the multiplication of the constants doesn't overflow
7016 and overflow is defined. With undefined overflow
7017 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
7018 But fold_plusminus_mult_expr would factor back any power-of-two
7019 value so do not distribute in the first place in this case. */
7020 if (code
== MULT_EXPR
7021 && TYPE_OVERFLOW_WRAPS (ctype
)
7022 && !(tree_fits_shwi_p (c
) && pow2p_hwi (absu_hwi (tree_to_shwi (c
)))))
7023 return fold_build2 (tcode
, ctype
,
7024 fold_build2 (code
, ctype
,
7025 fold_convert (ctype
, op0
),
7026 fold_convert (ctype
, c
)),
7032 /* We have a special case here if we are doing something like
7033 (C * 8) % 4 since we know that's zero. */
7034 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
7035 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
7036 /* If the multiplication can overflow we cannot optimize this. */
7037 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
7038 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
7039 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7042 *strict_overflow_p
= true;
7043 return omit_one_operand (type
, integer_zero_node
, op0
);
7046 /* ... fall through ... */
7048 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
7049 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
7050 /* If we can extract our operation from the LHS, do so and return a
7051 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7052 do something only if the second operand is a constant. */
7054 && TYPE_OVERFLOW_WRAPS (ctype
)
7055 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
7056 strict_overflow_p
)) != 0)
7057 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7058 fold_convert (ctype
, op1
));
7059 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
7060 && TYPE_OVERFLOW_WRAPS (ctype
)
7061 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
7062 strict_overflow_p
)) != 0)
7063 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7064 fold_convert (ctype
, t1
));
7065 else if (TREE_CODE (op1
) != INTEGER_CST
)
7068 /* If these are the same operation types, we can associate them
7069 assuming no overflow. */
7072 bool overflow_p
= false;
7073 wi::overflow_type overflow_mul
;
7074 signop sign
= TYPE_SIGN (ctype
);
7075 unsigned prec
= TYPE_PRECISION (ctype
);
7076 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
7077 wi::to_wide (c
, prec
),
7078 sign
, &overflow_mul
);
7079 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
7081 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
7084 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7085 wide_int_to_tree (ctype
, mul
));
7088 /* If these operations "cancel" each other, we have the main
7089 optimizations of this pass, which occur when either constant is a
7090 multiple of the other, in which case we replace this with either an
7091 operation or CODE or TCODE.
7093 If we have an unsigned type, we cannot do this since it will change
7094 the result if the original computation overflowed. */
7095 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
7096 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
7097 || (tcode
== MULT_EXPR
7098 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
7099 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
7100 && code
!= MULT_EXPR
)))
7102 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7105 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
7106 *strict_overflow_p
= true;
7107 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7108 fold_convert (ctype
,
7109 const_binop (TRUNC_DIV_EXPR
,
7112 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
7115 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
7116 *strict_overflow_p
= true;
7117 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
7118 fold_convert (ctype
,
7119 const_binop (TRUNC_DIV_EXPR
,
7132 /* Return a node which has the indicated constant VALUE (either 0 or
7133 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7134 and is of the indicated TYPE. */
7137 constant_boolean_node (bool value
, tree type
)
7139 if (type
== integer_type_node
)
7140 return value
? integer_one_node
: integer_zero_node
;
7141 else if (type
== boolean_type_node
)
7142 return value
? boolean_true_node
: boolean_false_node
;
7143 else if (TREE_CODE (type
) == VECTOR_TYPE
)
7144 return build_vector_from_val (type
,
7145 build_int_cst (TREE_TYPE (type
),
7148 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
7152 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7153 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7154 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7155 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7156 COND is the first argument to CODE; otherwise (as in the example
7157 given here), it is the second argument. TYPE is the type of the
7158 original expression. Return NULL_TREE if no simplification is
7162 fold_binary_op_with_conditional_arg (location_t loc
,
7163 enum tree_code code
,
7164 tree type
, tree op0
, tree op1
,
7165 tree cond
, tree arg
, int cond_first_p
)
7167 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
7168 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
7169 tree test
, true_value
, false_value
;
7170 tree lhs
= NULL_TREE
;
7171 tree rhs
= NULL_TREE
;
7172 enum tree_code cond_code
= COND_EXPR
;
7174 /* Do not move possibly trapping operations into the conditional as this
7175 pessimizes code and causes gimplification issues when applied late. */
7176 if (operation_could_trap_p (code
, FLOAT_TYPE_P (type
),
7177 ANY_INTEGRAL_TYPE_P (type
)
7178 && TYPE_OVERFLOW_TRAPS (type
), op1
))
7181 if (TREE_CODE (cond
) == COND_EXPR
7182 || TREE_CODE (cond
) == VEC_COND_EXPR
)
7184 test
= TREE_OPERAND (cond
, 0);
7185 true_value
= TREE_OPERAND (cond
, 1);
7186 false_value
= TREE_OPERAND (cond
, 2);
7187 /* If this operand throws an expression, then it does not make
7188 sense to try to perform a logical or arithmetic operation
7190 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
7192 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
7195 else if (!(TREE_CODE (type
) != VECTOR_TYPE
7196 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
7198 tree testtype
= TREE_TYPE (cond
);
7200 true_value
= constant_boolean_node (true, testtype
);
7201 false_value
= constant_boolean_node (false, testtype
);
7204 /* Detect the case of mixing vector and scalar types - bail out. */
7207 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
7208 cond_code
= VEC_COND_EXPR
;
7210 /* This transformation is only worthwhile if we don't have to wrap ARG
7211 in a SAVE_EXPR and the operation can be simplified without recursing
7212 on at least one of the branches once its pushed inside the COND_EXPR. */
7213 if (!TREE_CONSTANT (arg
)
7214 && (TREE_SIDE_EFFECTS (arg
)
7215 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
7216 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
7219 arg
= fold_convert_loc (loc
, arg_type
, arg
);
7222 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
7224 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
7226 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
7230 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
7232 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
7234 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
7237 /* Check that we have simplified at least one of the branches. */
7238 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
7241 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
7245 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7247 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7248 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7249 if ARG - ZERO_ARG is the same as X.
7251 If ARG is NULL, check for any value of type TYPE.
7253 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7254 and finite. The problematic cases are when X is zero, and its mode
7255 has signed zeros. In the case of rounding towards -infinity,
7256 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7257 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7260 fold_real_zero_addition_p (const_tree type
, const_tree arg
,
7261 const_tree zero_arg
, int negate
)
7263 if (!real_zerop (zero_arg
))
7266 /* Don't allow the fold with -fsignaling-nans. */
7267 if (arg
? tree_expr_maybe_signaling_nan_p (arg
) : HONOR_SNANS (type
))
7270 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7271 if (!HONOR_SIGNED_ZEROS (type
))
7274 /* There is no case that is safe for all rounding modes. */
7275 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
7278 /* In a vector or complex, we would need to check the sign of all zeros. */
7279 if (TREE_CODE (zero_arg
) == VECTOR_CST
)
7280 zero_arg
= uniform_vector_p (zero_arg
);
7281 if (!zero_arg
|| TREE_CODE (zero_arg
) != REAL_CST
)
7284 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7285 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg
)))
7288 /* The mode has signed zeros, and we have to honor their sign.
7289 In this situation, there are only two cases we can return true for.
7290 (i) X - 0 is the same as X with default rounding.
7291 (ii) X + 0 is X when X can't possibly be -0.0. */
7292 return negate
|| (arg
&& !tree_expr_maybe_real_minus_zero_p (arg
));
7295 /* Subroutine of match.pd that optimizes comparisons of a division by
7296 a nonzero integer constant against an integer constant, i.e.
7299 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7300 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7303 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
7304 tree
*hi
, bool *neg_overflow
)
7306 tree prod
, tmp
, type
= TREE_TYPE (c1
);
7307 signop sign
= TYPE_SIGN (type
);
7308 wi::overflow_type overflow
;
7310 /* We have to do this the hard way to detect unsigned overflow.
7311 prod = int_const_binop (MULT_EXPR, c1, c2); */
7312 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
7313 prod
= force_fit_type (type
, val
, -1, overflow
);
7314 *neg_overflow
= false;
7316 if (sign
== UNSIGNED
)
7318 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7321 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7322 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
7323 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
7325 else if (tree_int_cst_sgn (c1
) >= 0)
7327 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7328 switch (tree_int_cst_sgn (c2
))
7331 *neg_overflow
= true;
7332 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7337 *lo
= fold_negate_const (tmp
, type
);
7342 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7352 /* A negative divisor reverses the relational operators. */
7353 code
= swap_tree_comparison (code
);
7355 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
7356 switch (tree_int_cst_sgn (c2
))
7359 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7364 *hi
= fold_negate_const (tmp
, type
);
7369 *neg_overflow
= true;
7370 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7379 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
7382 if (TREE_OVERFLOW (*lo
)
7383 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
7385 if (TREE_OVERFLOW (*hi
)
7386 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
7393 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7394 equality/inequality test, then return a simplified form of the test
7395 using a sign testing. Otherwise return NULL. TYPE is the desired
7399 fold_single_bit_test_into_sign_test (location_t loc
,
7400 enum tree_code code
, tree arg0
, tree arg1
,
7403 /* If this is testing a single bit, we can optimize the test. */
7404 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7405 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7406 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7408 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7409 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7410 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
7412 if (arg00
!= NULL_TREE
7413 /* This is only a win if casting to a signed type is cheap,
7414 i.e. when arg00's type is not a partial mode. */
7415 && type_has_mode_precision_p (TREE_TYPE (arg00
)))
7417 tree stype
= signed_type_for (TREE_TYPE (arg00
));
7418 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
7420 fold_convert_loc (loc
, stype
, arg00
),
7421 build_int_cst (stype
, 0));
7428 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7429 equality/inequality test, then return a simplified form of
7430 the test using shifts and logical operations. Otherwise return
7431 NULL. TYPE is the desired result type. */
7434 fold_single_bit_test (location_t loc
, enum tree_code code
,
7435 tree arg0
, tree arg1
, tree result_type
)
7437 /* If this is testing a single bit, we can optimize the test. */
7438 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7439 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7440 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7442 tree inner
= TREE_OPERAND (arg0
, 0);
7443 tree type
= TREE_TYPE (arg0
);
7444 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
7445 scalar_int_mode operand_mode
= SCALAR_INT_TYPE_MODE (type
);
7447 tree signed_type
, unsigned_type
, intermediate_type
;
7450 /* First, see if we can fold the single bit test into a sign-bit
7452 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
7457 /* Otherwise we have (A & C) != 0 where C is a single bit,
7458 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7459 Similarly for (A & C) == 0. */
7461 /* If INNER is a right shift of a constant and it plus BITNUM does
7462 not overflow, adjust BITNUM and INNER. */
7463 if (TREE_CODE (inner
) == RSHIFT_EXPR
7464 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
7465 && bitnum
< TYPE_PRECISION (type
)
7466 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner
, 1)),
7467 TYPE_PRECISION (type
) - bitnum
))
7469 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
7470 inner
= TREE_OPERAND (inner
, 0);
7473 /* If we are going to be able to omit the AND below, we must do our
7474 operations as unsigned. If we must use the AND, we have a choice.
7475 Normally unsigned is faster, but for some machines signed is. */
7476 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
7477 && !flag_syntax_only
) ? 0 : 1;
7479 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
7480 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
7481 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
7482 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
7485 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
7486 inner
, size_int (bitnum
));
7488 one
= build_int_cst (intermediate_type
, 1);
7490 if (code
== EQ_EXPR
)
7491 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
7493 /* Put the AND last so it can combine with more things. */
7494 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
7496 /* Make sure to return the proper type. */
7497 inner
= fold_convert_loc (loc
, result_type
, inner
);
7504 /* Test whether it is preferable to swap two operands, ARG0 and
7505 ARG1, for example because ARG0 is an integer constant and ARG1
7509 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
7511 if (CONSTANT_CLASS_P (arg1
))
7513 if (CONSTANT_CLASS_P (arg0
))
7519 if (TREE_CONSTANT (arg1
))
7521 if (TREE_CONSTANT (arg0
))
7524 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7525 for commutative and comparison operators. Ensuring a canonical
7526 form allows the optimizers to find additional redundancies without
7527 having to explicitly check for both orderings. */
7528 if (TREE_CODE (arg0
) == SSA_NAME
7529 && TREE_CODE (arg1
) == SSA_NAME
7530 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
7533 /* Put SSA_NAMEs last. */
7534 if (TREE_CODE (arg1
) == SSA_NAME
)
7536 if (TREE_CODE (arg0
) == SSA_NAME
)
7539 /* Put variables last. */
7549 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7550 means A >= Y && A != MAX, but in this case we know that
7551 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7554 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7556 tree a
, typea
, type
= TREE_TYPE (bound
), a1
, diff
, y
;
7558 if (TREE_CODE (bound
) == LT_EXPR
)
7559 a
= TREE_OPERAND (bound
, 0);
7560 else if (TREE_CODE (bound
) == GT_EXPR
)
7561 a
= TREE_OPERAND (bound
, 1);
7565 typea
= TREE_TYPE (a
);
7566 if (!INTEGRAL_TYPE_P (typea
)
7567 && !POINTER_TYPE_P (typea
))
7570 if (TREE_CODE (ineq
) == LT_EXPR
)
7572 a1
= TREE_OPERAND (ineq
, 1);
7573 y
= TREE_OPERAND (ineq
, 0);
7575 else if (TREE_CODE (ineq
) == GT_EXPR
)
7577 a1
= TREE_OPERAND (ineq
, 0);
7578 y
= TREE_OPERAND (ineq
, 1);
7583 if (TREE_TYPE (a1
) != typea
)
7586 if (POINTER_TYPE_P (typea
))
7588 /* Convert the pointer types into integer before taking the difference. */
7589 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7590 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7591 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7594 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7596 if (!diff
|| !integer_onep (diff
))
7599 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7602 /* Fold a sum or difference of at least one multiplication.
7603 Returns the folded tree or NULL if no simplification could be made. */
7606 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7607 tree arg0
, tree arg1
)
7609 tree arg00
, arg01
, arg10
, arg11
;
7610 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7612 /* (A * C) +- (B * C) -> (A+-B) * C.
7613 (A * C) +- A -> A * (C+-1).
7614 We are most concerned about the case where C is a constant,
7615 but other combinations show up during loop reduction. Since
7616 it is not difficult, try all four possibilities. */
7618 if (TREE_CODE (arg0
) == MULT_EXPR
)
7620 arg00
= TREE_OPERAND (arg0
, 0);
7621 arg01
= TREE_OPERAND (arg0
, 1);
7623 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7625 arg00
= build_one_cst (type
);
7630 /* We cannot generate constant 1 for fract. */
7631 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7634 arg01
= build_one_cst (type
);
7636 if (TREE_CODE (arg1
) == MULT_EXPR
)
7638 arg10
= TREE_OPERAND (arg1
, 0);
7639 arg11
= TREE_OPERAND (arg1
, 1);
7641 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7643 arg10
= build_one_cst (type
);
7644 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7645 the purpose of this canonicalization. */
7646 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7647 && negate_expr_p (arg1
)
7648 && code
== PLUS_EXPR
)
7650 arg11
= negate_expr (arg1
);
7658 /* We cannot generate constant 1 for fract. */
7659 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7662 arg11
= build_one_cst (type
);
7666 /* Prefer factoring a common non-constant. */
7667 if (operand_equal_p (arg00
, arg10
, 0))
7668 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7669 else if (operand_equal_p (arg01
, arg11
, 0))
7670 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7671 else if (operand_equal_p (arg00
, arg11
, 0))
7672 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7673 else if (operand_equal_p (arg01
, arg10
, 0))
7674 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7676 /* No identical multiplicands; see if we can find a common
7677 power-of-two factor in non-power-of-two multiplies. This
7678 can help in multi-dimensional array access. */
7679 else if (tree_fits_shwi_p (arg01
) && tree_fits_shwi_p (arg11
))
7681 HOST_WIDE_INT int01
= tree_to_shwi (arg01
);
7682 HOST_WIDE_INT int11
= tree_to_shwi (arg11
);
7687 /* Move min of absolute values to int11. */
7688 if (absu_hwi (int01
) < absu_hwi (int11
))
7690 tmp
= int01
, int01
= int11
, int11
= tmp
;
7691 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7698 const unsigned HOST_WIDE_INT factor
= absu_hwi (int11
);
7700 && pow2p_hwi (factor
)
7701 && (int01
& (factor
- 1)) == 0
7702 /* The remainder should not be a constant, otherwise we
7703 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7704 increased the number of multiplications necessary. */
7705 && TREE_CODE (arg10
) != INTEGER_CST
)
7707 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7708 build_int_cst (TREE_TYPE (arg00
),
7713 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7720 if (! ANY_INTEGRAL_TYPE_P (type
)
7721 || TYPE_OVERFLOW_WRAPS (type
)
7722 /* We are neither factoring zero nor minus one. */
7723 || TREE_CODE (same
) == INTEGER_CST
)
7724 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7725 fold_build2_loc (loc
, code
, type
,
7726 fold_convert_loc (loc
, type
, alt0
),
7727 fold_convert_loc (loc
, type
, alt1
)),
7728 fold_convert_loc (loc
, type
, same
));
7730 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7731 same may be minus one and thus the multiplication may overflow. Perform
7732 the sum operation in an unsigned type. */
7733 tree utype
= unsigned_type_for (type
);
7734 tree tem
= fold_build2_loc (loc
, code
, utype
,
7735 fold_convert_loc (loc
, utype
, alt0
),
7736 fold_convert_loc (loc
, utype
, alt1
));
7737 /* If the sum evaluated to a constant that is not -INF the multiplication
7739 if (TREE_CODE (tem
) == INTEGER_CST
7740 && (wi::to_wide (tem
)
7741 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7742 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7743 fold_convert (type
, tem
), same
);
7745 /* Do not resort to unsigned multiplication because
7746 we lose the no-overflow property of the expression. */
7750 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7751 specified by EXPR into the buffer PTR of length LEN bytes.
7752 Return the number of bytes placed in the buffer, or zero
7756 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7758 tree type
= TREE_TYPE (expr
);
7759 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7760 int byte
, offset
, word
, words
;
7761 unsigned char value
;
7763 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7770 return MIN (len
, total_bytes
- off
);
7772 words
= total_bytes
/ UNITS_PER_WORD
;
7774 for (byte
= 0; byte
< total_bytes
; byte
++)
7776 int bitpos
= byte
* BITS_PER_UNIT
;
7777 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7779 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7781 if (total_bytes
> UNITS_PER_WORD
)
7783 word
= byte
/ UNITS_PER_WORD
;
7784 if (WORDS_BIG_ENDIAN
)
7785 word
= (words
- 1) - word
;
7786 offset
= word
* UNITS_PER_WORD
;
7787 if (BYTES_BIG_ENDIAN
)
7788 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7790 offset
+= byte
% UNITS_PER_WORD
;
7793 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7794 if (offset
>= off
&& offset
- off
< len
)
7795 ptr
[offset
- off
] = value
;
7797 return MIN (len
, total_bytes
- off
);
7801 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7802 specified by EXPR into the buffer PTR of length LEN bytes.
7803 Return the number of bytes placed in the buffer, or zero
7807 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7809 tree type
= TREE_TYPE (expr
);
7810 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7811 int total_bytes
= GET_MODE_SIZE (mode
);
7812 FIXED_VALUE_TYPE value
;
7813 tree i_value
, i_type
;
7815 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7818 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7820 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7823 value
= TREE_FIXED_CST (expr
);
7824 i_value
= double_int_to_tree (i_type
, value
.data
);
7826 return native_encode_int (i_value
, ptr
, len
, off
);
7830 /* Subroutine of native_encode_expr. Encode the REAL_CST
7831 specified by EXPR into the buffer PTR of length LEN bytes.
7832 Return the number of bytes placed in the buffer, or zero
7836 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7838 tree type
= TREE_TYPE (expr
);
7839 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7840 int byte
, offset
, word
, words
, bitpos
;
7841 unsigned char value
;
7843 /* There are always 32 bits in each long, no matter the size of
7844 the hosts long. We handle floating point representations with
7848 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7855 return MIN (len
, total_bytes
- off
);
7857 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7859 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7861 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7862 bitpos
+= BITS_PER_UNIT
)
7864 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7865 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7867 if (UNITS_PER_WORD
< 4)
7869 word
= byte
/ UNITS_PER_WORD
;
7870 if (WORDS_BIG_ENDIAN
)
7871 word
= (words
- 1) - word
;
7872 offset
= word
* UNITS_PER_WORD
;
7873 if (BYTES_BIG_ENDIAN
)
7874 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7876 offset
+= byte
% UNITS_PER_WORD
;
7881 if (BYTES_BIG_ENDIAN
)
7883 /* Reverse bytes within each long, or within the entire float
7884 if it's smaller than a long (for HFmode). */
7885 offset
= MIN (3, total_bytes
- 1) - offset
;
7886 gcc_assert (offset
>= 0);
7889 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7891 && offset
- off
< len
)
7892 ptr
[offset
- off
] = value
;
7894 return MIN (len
, total_bytes
- off
);
7897 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7898 specified by EXPR into the buffer PTR of length LEN bytes.
7899 Return the number of bytes placed in the buffer, or zero
7903 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7908 part
= TREE_REALPART (expr
);
7909 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7910 if (off
== -1 && rsize
== 0)
7912 part
= TREE_IMAGPART (expr
);
7914 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7915 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7917 if (off
== -1 && isize
!= rsize
)
7919 return rsize
+ isize
;
7922 /* Like native_encode_vector, but only encode the first COUNT elements.
7923 The other arguments are as for native_encode_vector. */
7926 native_encode_vector_part (const_tree expr
, unsigned char *ptr
, int len
,
7927 int off
, unsigned HOST_WIDE_INT count
)
7929 tree itype
= TREE_TYPE (TREE_TYPE (expr
));
7930 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr
))
7931 && TYPE_PRECISION (itype
) <= BITS_PER_UNIT
)
7933 /* This is the only case in which elements can be smaller than a byte.
7934 Element 0 is always in the lsb of the containing byte. */
7935 unsigned int elt_bits
= TYPE_PRECISION (itype
);
7936 int total_bytes
= CEIL (elt_bits
* count
, BITS_PER_UNIT
);
7937 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7943 /* Zero the buffer and then set bits later where necessary. */
7944 int extract_bytes
= MIN (len
, total_bytes
- off
);
7946 memset (ptr
, 0, extract_bytes
);
7948 unsigned int elts_per_byte
= BITS_PER_UNIT
/ elt_bits
;
7949 unsigned int first_elt
= off
* elts_per_byte
;
7950 unsigned int extract_elts
= extract_bytes
* elts_per_byte
;
7951 for (unsigned int i
= 0; i
< extract_elts
; ++i
)
7953 tree elt
= VECTOR_CST_ELT (expr
, first_elt
+ i
);
7954 if (TREE_CODE (elt
) != INTEGER_CST
)
7957 if (ptr
&& wi::extract_uhwi (wi::to_wide (elt
), 0, 1))
7959 unsigned int bit
= i
* elt_bits
;
7960 ptr
[bit
/ BITS_PER_UNIT
] |= 1 << (bit
% BITS_PER_UNIT
);
7963 return extract_bytes
;
7967 int size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7968 for (unsigned HOST_WIDE_INT i
= 0; i
< count
; i
++)
7975 tree elem
= VECTOR_CST_ELT (expr
, i
);
7976 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7978 if ((off
== -1 && res
!= size
) || res
== 0)
7982 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
7989 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7990 specified by EXPR into the buffer PTR of length LEN bytes.
7991 Return the number of bytes placed in the buffer, or zero
7995 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7997 unsigned HOST_WIDE_INT count
;
7998 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
8000 return native_encode_vector_part (expr
, ptr
, len
, off
, count
);
8004 /* Subroutine of native_encode_expr. Encode the STRING_CST
8005 specified by EXPR into the buffer PTR of length LEN bytes.
8006 Return the number of bytes placed in the buffer, or zero
8010 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8012 tree type
= TREE_TYPE (expr
);
8014 /* Wide-char strings are encoded in target byte-order so native
8015 encoding them is trivial. */
8016 if (BITS_PER_UNIT
!= CHAR_BIT
8017 || TREE_CODE (type
) != ARRAY_TYPE
8018 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
8019 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
8022 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
8023 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8027 len
= MIN (total_bytes
- off
, len
);
8033 if (off
< TREE_STRING_LENGTH (expr
))
8035 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
8036 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
8038 memset (ptr
+ written
, 0, len
- written
);
8044 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
8045 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
8046 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
8047 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
8048 sufficient to encode the entire EXPR, or if OFF is out of bounds.
8049 Otherwise, start at byte offset OFF and encode at most LEN bytes.
8050 Return the number of bytes placed in the buffer, or zero upon failure. */
8053 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8055 /* We don't support starting at negative offset and -1 is special. */
8059 switch (TREE_CODE (expr
))
8062 return native_encode_int (expr
, ptr
, len
, off
);
8065 return native_encode_real (expr
, ptr
, len
, off
);
8068 return native_encode_fixed (expr
, ptr
, len
, off
);
8071 return native_encode_complex (expr
, ptr
, len
, off
);
8074 return native_encode_vector (expr
, ptr
, len
, off
);
8077 return native_encode_string (expr
, ptr
, len
, off
);
8084 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8085 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8086 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8087 machine modes, we can't just use build_nonstandard_integer_type. */
8090 find_bitfield_repr_type (int fieldsize
, int len
)
8093 for (int pass
= 0; pass
< 2; pass
++)
8095 enum mode_class mclass
= pass
? MODE_PARTIAL_INT
: MODE_INT
;
8096 FOR_EACH_MODE_IN_CLASS (mode
, mclass
)
8097 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8098 && known_eq (GET_MODE_PRECISION (mode
),
8099 GET_MODE_BITSIZE (mode
))
8100 && known_le (GET_MODE_SIZE (mode
), len
))
8102 tree ret
= lang_hooks
.types
.type_for_mode (mode
, 1);
8103 if (ret
&& TYPE_MODE (ret
) == mode
)
8108 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
8109 if (int_n_enabled_p
[i
]
8110 && int_n_data
[i
].bitsize
>= (unsigned) (BITS_PER_UNIT
* fieldsize
)
8111 && int_n_trees
[i
].unsigned_type
)
8113 tree ret
= int_n_trees
[i
].unsigned_type
;
8114 mode
= TYPE_MODE (ret
);
8115 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8116 && known_eq (GET_MODE_PRECISION (mode
),
8117 GET_MODE_BITSIZE (mode
))
8118 && known_le (GET_MODE_SIZE (mode
), len
))
8125 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8126 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8127 to be non-NULL and OFF zero), then in addition to filling the
8128 bytes pointed by PTR with the value also clear any bits pointed
8129 by MASK that are known to be initialized, keep them as is for
8130 e.g. uninitialized padding bits or uninitialized fields. */
8133 native_encode_initializer (tree init
, unsigned char *ptr
, int len
,
8134 int off
, unsigned char *mask
)
8138 /* We don't support starting at negative offset and -1 is special. */
8139 if (off
< -1 || init
== NULL_TREE
)
8142 gcc_assert (mask
== NULL
|| (off
== 0 && ptr
));
8145 switch (TREE_CODE (init
))
8147 case VIEW_CONVERT_EXPR
:
8148 case NON_LVALUE_EXPR
:
8149 return native_encode_initializer (TREE_OPERAND (init
, 0), ptr
, len
, off
,
8152 r
= native_encode_expr (init
, ptr
, len
, off
);
8154 memset (mask
, 0, r
);
8157 tree type
= TREE_TYPE (init
);
8158 HOST_WIDE_INT total_bytes
= int_size_in_bytes (type
);
8159 if (total_bytes
< 0)
8161 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8163 int o
= off
== -1 ? 0 : off
;
8164 if (TREE_CODE (type
) == ARRAY_TYPE
)
8167 unsigned HOST_WIDE_INT cnt
;
8168 HOST_WIDE_INT curpos
= 0, fieldsize
, valueinit
= -1;
8169 constructor_elt
*ce
;
8171 if (!TYPE_DOMAIN (type
)
8172 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
8175 fieldsize
= int_size_in_bytes (TREE_TYPE (type
));
8179 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
8181 memset (ptr
, '\0', MIN (total_bytes
- off
, len
));
8183 for (cnt
= 0; ; cnt
++)
8185 tree val
= NULL_TREE
, index
= NULL_TREE
;
8186 HOST_WIDE_INT pos
= curpos
, count
= 0;
8188 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8193 else if (mask
== NULL
8194 || CONSTRUCTOR_NO_CLEARING (init
)
8195 || curpos
>= total_bytes
)
8200 if (index
&& TREE_CODE (index
) == RANGE_EXPR
)
8202 if (TREE_CODE (TREE_OPERAND (index
, 0)) != INTEGER_CST
8203 || TREE_CODE (TREE_OPERAND (index
, 1)) != INTEGER_CST
)
8206 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 0))
8207 - wi::to_offset (min_index
),
8208 TYPE_PRECISION (sizetype
));
8210 if (!wi::fits_shwi_p (pos
))
8212 pos
= wpos
.to_shwi ();
8214 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 1))
8215 - wi::to_offset (TREE_OPERAND (index
, 0)),
8216 TYPE_PRECISION (sizetype
));
8217 if (!wi::fits_shwi_p (wcount
))
8219 count
= wcount
.to_shwi ();
8223 if (TREE_CODE (index
) != INTEGER_CST
)
8226 = wi::sext (wi::to_offset (index
)
8227 - wi::to_offset (min_index
),
8228 TYPE_PRECISION (sizetype
));
8230 if (!wi::fits_shwi_p (wpos
))
8232 pos
= wpos
.to_shwi ();
8235 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
) && curpos
!= pos
)
8237 if (valueinit
== -1)
8239 tree zero
= build_zero_cst (TREE_TYPE (type
));
8240 r
= native_encode_initializer (zero
, ptr
+ curpos
,
8243 if (TREE_CODE (zero
) == CONSTRUCTOR
)
8248 curpos
+= fieldsize
;
8250 while (curpos
!= pos
)
8252 memcpy (ptr
+ curpos
, ptr
+ valueinit
, fieldsize
);
8253 memcpy (mask
+ curpos
, mask
+ valueinit
, fieldsize
);
8254 curpos
+= fieldsize
;
8264 && (curpos
+ fieldsize
8265 <= (HOST_WIDE_INT
) off
+ len
)))
8270 memcpy (ptr
+ (curpos
- o
), ptr
+ (pos
- o
),
8273 memcpy (mask
+ curpos
, mask
+ pos
, fieldsize
);
8275 else if (!native_encode_initializer (val
,
8292 else if (curpos
+ fieldsize
> off
8293 && curpos
< (HOST_WIDE_INT
) off
+ len
)
8295 /* Partial overlap. */
8296 unsigned char *p
= NULL
;
8299 gcc_assert (mask
== NULL
);
8303 p
= ptr
+ curpos
- off
;
8304 l
= MIN ((HOST_WIDE_INT
) off
+ len
- curpos
,
8313 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8316 curpos
+= fieldsize
;
8318 while (count
-- != 0);
8320 return MIN (total_bytes
- off
, len
);
8322 else if (TREE_CODE (type
) == RECORD_TYPE
8323 || TREE_CODE (type
) == UNION_TYPE
)
8325 unsigned HOST_WIDE_INT cnt
;
8326 constructor_elt
*ce
;
8327 tree fld_base
= TYPE_FIELDS (type
);
8328 tree to_free
= NULL_TREE
;
8330 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
|| mask
== NULL
);
8332 memset (ptr
, '\0', MIN (total_bytes
- o
, len
));
8333 for (cnt
= 0; ; cnt
++)
8335 tree val
= NULL_TREE
, field
= NULL_TREE
;
8336 HOST_WIDE_INT pos
= 0, fieldsize
;
8337 unsigned HOST_WIDE_INT bpos
= 0, epos
= 0;
8342 to_free
= NULL_TREE
;
8345 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8349 if (field
== NULL_TREE
)
8352 pos
= int_byte_position (field
);
8353 if (off
!= -1 && (HOST_WIDE_INT
) off
+ len
<= pos
)
8356 else if (mask
== NULL
8357 || CONSTRUCTOR_NO_CLEARING (init
))
8362 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
))
8365 for (fld
= fld_base
; fld
; fld
= DECL_CHAIN (fld
))
8367 if (TREE_CODE (fld
) != FIELD_DECL
)
8371 if (DECL_PADDING_P (fld
))
8373 if (DECL_SIZE_UNIT (fld
) == NULL_TREE
8374 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld
)))
8376 if (integer_zerop (DECL_SIZE_UNIT (fld
)))
8380 if (fld
== NULL_TREE
)
8386 fld_base
= DECL_CHAIN (fld
);
8391 pos
= int_byte_position (field
);
8392 val
= build_zero_cst (TREE_TYPE (fld
));
8393 if (TREE_CODE (val
) == CONSTRUCTOR
)
8398 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
8399 && TYPE_DOMAIN (TREE_TYPE (field
))
8400 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field
))))
8402 if (mask
|| off
!= -1)
8404 if (val
== NULL_TREE
)
8406 if (TREE_CODE (TREE_TYPE (val
)) != ARRAY_TYPE
)
8408 fieldsize
= int_size_in_bytes (TREE_TYPE (val
));
8410 || (int) fieldsize
!= fieldsize
8411 || (pos
+ fieldsize
) > INT_MAX
)
8413 if (pos
+ fieldsize
> total_bytes
)
8415 if (ptr
!= NULL
&& total_bytes
< len
)
8416 memset (ptr
+ total_bytes
, '\0',
8417 MIN (pos
+ fieldsize
, len
) - total_bytes
);
8418 total_bytes
= pos
+ fieldsize
;
8423 if (DECL_SIZE_UNIT (field
) == NULL_TREE
8424 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field
)))
8426 fieldsize
= tree_to_shwi (DECL_SIZE_UNIT (field
));
8431 if (DECL_BIT_FIELD (field
))
8433 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8435 fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8436 bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8437 if (bpos
% BITS_PER_UNIT
)
8438 bpos
%= BITS_PER_UNIT
;
8442 epos
= fieldsize
% BITS_PER_UNIT
;
8443 fieldsize
+= BITS_PER_UNIT
- 1;
8444 fieldsize
/= BITS_PER_UNIT
;
8447 if (off
!= -1 && pos
+ fieldsize
<= off
)
8450 if (val
== NULL_TREE
)
8453 if (DECL_BIT_FIELD (field
))
8455 /* FIXME: Handle PDP endian. */
8456 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8459 if (TREE_CODE (val
) != INTEGER_CST
)
8462 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8463 tree repr_type
= NULL_TREE
;
8464 HOST_WIDE_INT rpos
= 0;
8465 if (repr
&& INTEGRAL_TYPE_P (TREE_TYPE (repr
)))
8467 rpos
= int_byte_position (repr
);
8468 repr_type
= TREE_TYPE (repr
);
8472 repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8473 if (repr_type
== NULL_TREE
)
8475 HOST_WIDE_INT repr_size
= int_size_in_bytes (repr_type
);
8476 gcc_assert (repr_size
> 0 && repr_size
<= len
);
8477 if (pos
+ repr_size
<= o
+ len
)
8481 rpos
= o
+ len
- repr_size
;
8482 gcc_assert (rpos
<= pos
);
8488 wide_int w
= wi::to_wide (val
, TYPE_PRECISION (repr_type
));
8489 int diff
= (TYPE_PRECISION (repr_type
)
8490 - TYPE_PRECISION (TREE_TYPE (field
)));
8491 HOST_WIDE_INT bitoff
= (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8492 if (!BYTES_BIG_ENDIAN
)
8493 w
= wi::lshift (w
, bitoff
);
8495 w
= wi::lshift (w
, diff
- bitoff
);
8496 val
= wide_int_to_tree (repr_type
, w
);
8498 unsigned char buf
[MAX_BITSIZE_MODE_ANY_INT
8499 / BITS_PER_UNIT
+ 1];
8500 int l
= native_encode_int (val
, buf
, sizeof buf
, 0);
8501 if (l
* BITS_PER_UNIT
!= TYPE_PRECISION (repr_type
))
8507 /* If the bitfield does not start at byte boundary, handle
8508 the partial byte at the start. */
8510 && (off
== -1 || (pos
>= off
&& len
>= 1)))
8512 if (!BYTES_BIG_ENDIAN
)
8514 int msk
= (1 << bpos
) - 1;
8515 buf
[pos
- rpos
] &= ~msk
;
8516 buf
[pos
- rpos
] |= ptr
[pos
- o
] & msk
;
8519 if (fieldsize
> 1 || epos
== 0)
8522 mask
[pos
] &= (msk
| ~((1 << epos
) - 1));
8527 int msk
= (1 << (BITS_PER_UNIT
- bpos
)) - 1;
8528 buf
[pos
- rpos
] &= msk
;
8529 buf
[pos
- rpos
] |= ptr
[pos
- o
] & ~msk
;
8532 if (fieldsize
> 1 || epos
== 0)
8536 | ((1 << (BITS_PER_UNIT
- epos
))
8541 /* If the bitfield does not end at byte boundary, handle
8542 the partial byte at the end. */
8545 || pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
))
8547 if (!BYTES_BIG_ENDIAN
)
8549 int msk
= (1 << epos
) - 1;
8550 buf
[pos
- rpos
+ fieldsize
- 1] &= msk
;
8551 buf
[pos
- rpos
+ fieldsize
- 1]
8552 |= ptr
[pos
+ fieldsize
- 1 - o
] & ~msk
;
8553 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8554 mask
[pos
+ fieldsize
- 1] &= ~msk
;
8558 int msk
= (1 << (BITS_PER_UNIT
- epos
)) - 1;
8559 buf
[pos
- rpos
+ fieldsize
- 1] &= ~msk
;
8560 buf
[pos
- rpos
+ fieldsize
- 1]
8561 |= ptr
[pos
+ fieldsize
- 1 - o
] & msk
;
8562 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8563 mask
[pos
+ fieldsize
- 1] &= msk
;
8568 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8570 memcpy (ptr
+ pos
- o
, buf
+ (pos
- rpos
), fieldsize
);
8571 if (mask
&& (fieldsize
> (bpos
!= 0) + (epos
!= 0)))
8572 memset (mask
+ pos
+ (bpos
!= 0), 0,
8573 fieldsize
- (bpos
!= 0) - (epos
!= 0));
8577 /* Partial overlap. */
8578 HOST_WIDE_INT fsz
= fieldsize
;
8579 gcc_assert (mask
== NULL
);
8585 if (pos
+ fsz
> (HOST_WIDE_INT
) off
+ len
)
8586 fsz
= (HOST_WIDE_INT
) off
+ len
- pos
;
8587 memcpy (ptr
+ pos
- off
, buf
+ (pos
- rpos
), fsz
);
8594 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8596 int fldsize
= fieldsize
;
8599 tree fld
= DECL_CHAIN (field
);
8602 if (TREE_CODE (fld
) == FIELD_DECL
)
8604 fld
= DECL_CHAIN (fld
);
8606 if (fld
== NULL_TREE
)
8607 fldsize
= len
- pos
;
8609 r
= native_encode_initializer (val
, ptr
? ptr
+ pos
- o
8613 mask
? mask
+ pos
: NULL
);
8617 && fldsize
!= fieldsize
8619 && pos
+ r
> total_bytes
)
8620 total_bytes
= pos
+ r
;
8624 /* Partial overlap. */
8625 unsigned char *p
= NULL
;
8628 gcc_assert (mask
== NULL
);
8632 p
= ptr
+ pos
- off
;
8633 l
= MIN ((HOST_WIDE_INT
) off
+ len
- pos
,
8642 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8646 return MIN (total_bytes
- off
, len
);
8653 /* Subroutine of native_interpret_expr. Interpret the contents of
8654 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8655 If the buffer cannot be interpreted, return NULL_TREE. */
8658 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
8660 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8662 if (total_bytes
> len
8663 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8666 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
8668 return wide_int_to_tree (type
, result
);
8672 /* Subroutine of native_interpret_expr. Interpret the contents of
8673 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8674 If the buffer cannot be interpreted, return NULL_TREE. */
8677 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
8679 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
8680 int total_bytes
= GET_MODE_SIZE (mode
);
8682 FIXED_VALUE_TYPE fixed_value
;
8684 if (total_bytes
> len
8685 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8688 result
= double_int::from_buffer (ptr
, total_bytes
);
8689 fixed_value
= fixed_from_double_int (result
, mode
);
8691 return build_fixed (type
, fixed_value
);
8695 /* Subroutine of native_interpret_expr. Interpret the contents of
8696 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8697 If the buffer cannot be interpreted, return NULL_TREE. */
8700 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
8702 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8703 int total_bytes
= GET_MODE_SIZE (mode
);
8704 unsigned char value
;
8705 /* There are always 32 bits in each long, no matter the size of
8706 the hosts long. We handle floating point representations with
8711 if (total_bytes
> len
|| total_bytes
> 24)
8713 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
8715 memset (tmp
, 0, sizeof (tmp
));
8716 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
8717 bitpos
+= BITS_PER_UNIT
)
8719 /* Both OFFSET and BYTE index within a long;
8720 bitpos indexes the whole float. */
8721 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
8722 if (UNITS_PER_WORD
< 4)
8724 int word
= byte
/ UNITS_PER_WORD
;
8725 if (WORDS_BIG_ENDIAN
)
8726 word
= (words
- 1) - word
;
8727 offset
= word
* UNITS_PER_WORD
;
8728 if (BYTES_BIG_ENDIAN
)
8729 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
8731 offset
+= byte
% UNITS_PER_WORD
;
8736 if (BYTES_BIG_ENDIAN
)
8738 /* Reverse bytes within each long, or within the entire float
8739 if it's smaller than a long (for HFmode). */
8740 offset
= MIN (3, total_bytes
- 1) - offset
;
8741 gcc_assert (offset
>= 0);
8744 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
8746 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
8749 real_from_target (&r
, tmp
, mode
);
8750 return build_real (type
, r
);
8754 /* Subroutine of native_interpret_expr. Interpret the contents of
8755 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8756 If the buffer cannot be interpreted, return NULL_TREE. */
8759 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
8761 tree etype
, rpart
, ipart
;
8764 etype
= TREE_TYPE (type
);
8765 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8768 rpart
= native_interpret_expr (etype
, ptr
, size
);
8771 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
8774 return build_complex (type
, rpart
, ipart
);
8777 /* Read a vector of type TYPE from the target memory image given by BYTES,
8778 which contains LEN bytes. The vector is known to be encodable using
8779 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8781 Return the vector on success, otherwise return null. */
8784 native_interpret_vector_part (tree type
, const unsigned char *bytes
,
8785 unsigned int len
, unsigned int npatterns
,
8786 unsigned int nelts_per_pattern
)
8788 tree elt_type
= TREE_TYPE (type
);
8789 if (VECTOR_BOOLEAN_TYPE_P (type
)
8790 && TYPE_PRECISION (elt_type
) <= BITS_PER_UNIT
)
8792 /* This is the only case in which elements can be smaller than a byte.
8793 Element 0 is always in the lsb of the containing byte. */
8794 unsigned int elt_bits
= TYPE_PRECISION (elt_type
);
8795 if (elt_bits
* npatterns
* nelts_per_pattern
> len
* BITS_PER_UNIT
)
8798 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8799 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8801 unsigned int bit_index
= i
* elt_bits
;
8802 unsigned int byte_index
= bit_index
/ BITS_PER_UNIT
;
8803 unsigned int lsb
= bit_index
% BITS_PER_UNIT
;
8804 builder
.quick_push (bytes
[byte_index
] & (1 << lsb
)
8805 ? build_all_ones_cst (elt_type
)
8806 : build_zero_cst (elt_type
));
8808 return builder
.build ();
8811 unsigned int elt_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (elt_type
));
8812 if (elt_bytes
* npatterns
* nelts_per_pattern
> len
)
8815 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8816 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8818 tree elt
= native_interpret_expr (elt_type
, bytes
, elt_bytes
);
8821 builder
.quick_push (elt
);
8824 return builder
.build ();
8827 /* Subroutine of native_interpret_expr. Interpret the contents of
8828 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8829 If the buffer cannot be interpreted, return NULL_TREE. */
8832 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
8836 unsigned HOST_WIDE_INT count
;
8838 etype
= TREE_TYPE (type
);
8839 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8840 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&count
)
8841 || size
* count
> len
)
8844 return native_interpret_vector_part (type
, ptr
, len
, count
, 1);
8848 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8849 the buffer PTR of length LEN as a constant of type TYPE. For
8850 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8851 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8852 return NULL_TREE. */
8855 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
8857 switch (TREE_CODE (type
))
8863 case REFERENCE_TYPE
:
8865 return native_interpret_int (type
, ptr
, len
);
8868 if (tree ret
= native_interpret_real (type
, ptr
, len
))
8870 /* For floating point values in composite modes, punt if this
8871 folding doesn't preserve bit representation. As the mode doesn't
8872 have fixed precision while GCC pretends it does, there could be
8873 valid values that GCC can't really represent accurately.
8874 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8875 bit combinationations which GCC doesn't preserve. */
8876 unsigned char buf
[24 * 2];
8877 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8878 int total_bytes
= GET_MODE_SIZE (mode
);
8879 memcpy (buf
+ 24, ptr
, total_bytes
);
8880 clear_type_padding_in_mask (type
, buf
+ 24);
8881 if (native_encode_expr (ret
, buf
, total_bytes
, 0) != total_bytes
8882 || memcmp (buf
+ 24, buf
, total_bytes
) != 0)
8888 case FIXED_POINT_TYPE
:
8889 return native_interpret_fixed (type
, ptr
, len
);
8892 return native_interpret_complex (type
, ptr
, len
);
8895 return native_interpret_vector (type
, ptr
, len
);
8902 /* Returns true if we can interpret the contents of a native encoding
8906 can_native_interpret_type_p (tree type
)
8908 switch (TREE_CODE (type
))
8914 case REFERENCE_TYPE
:
8915 case FIXED_POINT_TYPE
:
8926 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8927 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8930 native_interpret_aggregate (tree type
, const unsigned char *ptr
, int off
,
8933 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
8934 if (TREE_CODE (type
) == ARRAY_TYPE
)
8936 HOST_WIDE_INT eltsz
= int_size_in_bytes (TREE_TYPE (type
));
8937 if (eltsz
< 0 || eltsz
> len
|| TYPE_DOMAIN (type
) == NULL_TREE
)
8940 HOST_WIDE_INT cnt
= 0;
8941 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
8943 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
8945 cnt
= tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1;
8949 HOST_WIDE_INT pos
= 0;
8950 for (HOST_WIDE_INT i
= 0; i
< cnt
; i
++, pos
+= eltsz
)
8953 if (pos
>= len
|| pos
+ eltsz
> len
)
8955 if (can_native_interpret_type_p (TREE_TYPE (type
)))
8957 v
= native_interpret_expr (TREE_TYPE (type
),
8958 ptr
+ off
+ pos
, eltsz
);
8962 else if (TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
8963 || TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
8964 v
= native_interpret_aggregate (TREE_TYPE (type
), ptr
, off
+ pos
,
8968 CONSTRUCTOR_APPEND_ELT (elts
, size_int (i
), v
);
8970 return build_constructor (type
, elts
);
8972 if (TREE_CODE (type
) != RECORD_TYPE
)
8974 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8976 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_PADDING_P (field
))
8979 HOST_WIDE_INT bitoff
= 0, pos
= 0, sz
= 0;
8982 if (DECL_BIT_FIELD (field
))
8984 fld
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8985 if (fld
&& INTEGRAL_TYPE_P (TREE_TYPE (fld
)))
8987 poly_int64 bitoffset
;
8988 poly_uint64 field_offset
, fld_offset
;
8989 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
8990 && poly_int_tree_p (DECL_FIELD_OFFSET (fld
), &fld_offset
))
8991 bitoffset
= (field_offset
- fld_offset
) * BITS_PER_UNIT
;
8994 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
8995 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld
)));
8996 diff
= (TYPE_PRECISION (TREE_TYPE (fld
))
8997 - TYPE_PRECISION (TREE_TYPE (field
)));
8998 if (!bitoffset
.is_constant (&bitoff
)
9005 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
9007 int fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
9008 int bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
9009 bpos
%= BITS_PER_UNIT
;
9011 fieldsize
+= BITS_PER_UNIT
- 1;
9012 fieldsize
/= BITS_PER_UNIT
;
9013 tree repr_type
= find_bitfield_repr_type (fieldsize
, len
);
9014 if (repr_type
== NULL_TREE
)
9016 sz
= int_size_in_bytes (repr_type
);
9017 if (sz
< 0 || sz
> len
)
9019 pos
= int_byte_position (field
);
9020 if (pos
< 0 || pos
> len
|| pos
+ fieldsize
> len
)
9023 if (pos
+ sz
<= len
)
9028 gcc_assert (rpos
<= pos
);
9030 bitoff
= (HOST_WIDE_INT
) (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
9032 diff
= (TYPE_PRECISION (repr_type
)
9033 - TYPE_PRECISION (TREE_TYPE (field
)));
9034 v
= native_interpret_expr (repr_type
, ptr
+ off
+ pos
, sz
);
9043 sz
= int_size_in_bytes (TREE_TYPE (fld
));
9044 if (sz
< 0 || sz
> len
)
9046 tree byte_pos
= byte_position (fld
);
9047 if (!tree_fits_shwi_p (byte_pos
))
9049 pos
= tree_to_shwi (byte_pos
);
9050 if (pos
< 0 || pos
> len
|| pos
+ sz
> len
)
9053 if (fld
== NULL_TREE
)
9054 /* Already handled above. */;
9055 else if (can_native_interpret_type_p (TREE_TYPE (fld
)))
9057 v
= native_interpret_expr (TREE_TYPE (fld
),
9058 ptr
+ off
+ pos
, sz
);
9062 else if (TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
9063 || TREE_CODE (TREE_TYPE (fld
)) == ARRAY_TYPE
)
9064 v
= native_interpret_aggregate (TREE_TYPE (fld
), ptr
, off
+ pos
, sz
);
9069 if (TREE_CODE (v
) != INTEGER_CST
)
9072 /* FIXME: Figure out how to handle PDP endian bitfields. */
9073 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
9075 if (!BYTES_BIG_ENDIAN
)
9076 v
= wide_int_to_tree (TREE_TYPE (field
),
9077 wi::lrshift (wi::to_wide (v
), bitoff
));
9079 v
= wide_int_to_tree (TREE_TYPE (field
),
9080 wi::lrshift (wi::to_wide (v
),
9083 CONSTRUCTOR_APPEND_ELT (elts
, field
, v
);
9085 return build_constructor (type
, elts
);
9088 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9089 or extracted constant positions and/or sizes aren't byte aligned. */
9091 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9092 bits between adjacent elements. AMNT should be within
9095 00011111|11100000 << 2 = 01111111|10000000
9096 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9099 shift_bytes_in_array_left (unsigned char *ptr
, unsigned int sz
,
9105 unsigned char carry_over
= 0U;
9106 unsigned char carry_mask
= (~0U) << (unsigned char) (BITS_PER_UNIT
- amnt
);
9107 unsigned char clear_mask
= (~0U) << amnt
;
9109 for (unsigned int i
= 0; i
< sz
; i
++)
9111 unsigned prev_carry_over
= carry_over
;
9112 carry_over
= (ptr
[i
] & carry_mask
) >> (BITS_PER_UNIT
- amnt
);
9117 ptr
[i
] &= clear_mask
;
9118 ptr
[i
] |= prev_carry_over
;
9123 /* Like shift_bytes_in_array_left but for big-endian.
9124 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9125 bits between adjacent elements. AMNT should be within
9128 00011111|11100000 >> 2 = 00000111|11111000
9129 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9132 shift_bytes_in_array_right (unsigned char *ptr
, unsigned int sz
,
9138 unsigned char carry_over
= 0U;
9139 unsigned char carry_mask
= ~(~0U << amnt
);
9141 for (unsigned int i
= 0; i
< sz
; i
++)
9143 unsigned prev_carry_over
= carry_over
;
9144 carry_over
= ptr
[i
] & carry_mask
;
9146 carry_over
<<= (unsigned char) BITS_PER_UNIT
- amnt
;
9148 ptr
[i
] |= prev_carry_over
;
9152 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9153 directly on the VECTOR_CST encoding, in a way that works for variable-
9154 length vectors. Return the resulting VECTOR_CST on success or null
9158 fold_view_convert_vector_encoding (tree type
, tree expr
)
9160 tree expr_type
= TREE_TYPE (expr
);
9161 poly_uint64 type_bits
, expr_bits
;
9162 if (!poly_int_tree_p (TYPE_SIZE (type
), &type_bits
)
9163 || !poly_int_tree_p (TYPE_SIZE (expr_type
), &expr_bits
))
9166 poly_uint64 type_units
= TYPE_VECTOR_SUBPARTS (type
);
9167 poly_uint64 expr_units
= TYPE_VECTOR_SUBPARTS (expr_type
);
9168 unsigned int type_elt_bits
= vector_element_size (type_bits
, type_units
);
9169 unsigned int expr_elt_bits
= vector_element_size (expr_bits
, expr_units
);
9171 /* We can only preserve the semantics of a stepped pattern if the new
9172 vector element is an integer of the same size. */
9173 if (VECTOR_CST_STEPPED_P (expr
)
9174 && (!INTEGRAL_TYPE_P (type
) || type_elt_bits
!= expr_elt_bits
))
9177 /* The number of bits needed to encode one element from every pattern
9178 of the original vector. */
9179 unsigned int expr_sequence_bits
9180 = VECTOR_CST_NPATTERNS (expr
) * expr_elt_bits
;
9182 /* The number of bits needed to encode one element from every pattern
9184 unsigned int type_sequence_bits
9185 = least_common_multiple (expr_sequence_bits
, type_elt_bits
);
9187 /* Don't try to read more bytes than are available, which can happen
9188 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9189 The general VIEW_CONVERT handling can cope with that case, so there's
9190 no point complicating things here. */
9191 unsigned int nelts_per_pattern
= VECTOR_CST_NELTS_PER_PATTERN (expr
);
9192 unsigned int buffer_bytes
= CEIL (nelts_per_pattern
* type_sequence_bits
,
9194 unsigned int buffer_bits
= buffer_bytes
* BITS_PER_UNIT
;
9195 if (known_gt (buffer_bits
, expr_bits
))
9198 /* Get enough bytes of EXPR to form the new encoding. */
9199 auto_vec
<unsigned char, 128> buffer (buffer_bytes
);
9200 buffer
.quick_grow (buffer_bytes
);
9201 if (native_encode_vector_part (expr
, buffer
.address (), buffer_bytes
, 0,
9202 buffer_bits
/ expr_elt_bits
)
9203 != (int) buffer_bytes
)
9206 /* Reencode the bytes as TYPE. */
9207 unsigned int type_npatterns
= type_sequence_bits
/ type_elt_bits
;
9208 return native_interpret_vector_part (type
, &buffer
[0], buffer
.length (),
9209 type_npatterns
, nelts_per_pattern
);
9212 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9213 TYPE at compile-time. If we're unable to perform the conversion
9214 return NULL_TREE. */
9217 fold_view_convert_expr (tree type
, tree expr
)
9219 /* We support up to 512-bit values (for V8DFmode). */
9220 unsigned char buffer
[64];
9223 /* Check that the host and target are sane. */
9224 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
9227 if (VECTOR_TYPE_P (type
) && TREE_CODE (expr
) == VECTOR_CST
)
9228 if (tree res
= fold_view_convert_vector_encoding (type
, expr
))
9231 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
9235 return native_interpret_expr (type
, buffer
, len
);
9238 /* Build an expression for the address of T. Folds away INDIRECT_REF
9239 to avoid confusing the gimplify process. */
9242 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
9244 /* The size of the object is not relevant when talking about its address. */
9245 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
9246 t
= TREE_OPERAND (t
, 0);
9248 if (TREE_CODE (t
) == INDIRECT_REF
)
9250 t
= TREE_OPERAND (t
, 0);
9252 if (TREE_TYPE (t
) != ptrtype
)
9253 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
9255 else if (TREE_CODE (t
) == MEM_REF
9256 && integer_zerop (TREE_OPERAND (t
, 1)))
9258 t
= TREE_OPERAND (t
, 0);
9260 if (TREE_TYPE (t
) != ptrtype
)
9261 t
= fold_convert_loc (loc
, ptrtype
, t
);
9263 else if (TREE_CODE (t
) == MEM_REF
9264 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
9265 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
9266 TREE_OPERAND (t
, 0),
9267 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
9268 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
9270 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
9272 if (TREE_TYPE (t
) != ptrtype
)
9273 t
= fold_convert_loc (loc
, ptrtype
, t
);
9276 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
9281 /* Build an expression for the address of T. */
9284 build_fold_addr_expr_loc (location_t loc
, tree t
)
9286 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
9288 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
9291 /* Fold a unary expression of code CODE and type TYPE with operand
9292 OP0. Return the folded expression if folding is successful.
9293 Otherwise, return NULL_TREE. */
9296 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
9300 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9302 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9303 && TREE_CODE_LENGTH (code
) == 1);
9308 if (CONVERT_EXPR_CODE_P (code
)
9309 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
9311 /* Don't use STRIP_NOPS, because signedness of argument type
9313 STRIP_SIGN_NOPS (arg0
);
9317 /* Strip any conversions that don't change the mode. This
9318 is safe for every expression, except for a comparison
9319 expression because its signedness is derived from its
9322 Note that this is done as an internal manipulation within
9323 the constant folder, in order to find the simplest
9324 representation of the arguments so that their form can be
9325 studied. In any cases, the appropriate type conversions
9326 should be put back in the tree that will get out of the
9331 if (CONSTANT_CLASS_P (arg0
))
9333 tree tem
= const_unop (code
, type
, arg0
);
9336 if (TREE_TYPE (tem
) != type
)
9337 tem
= fold_convert_loc (loc
, type
, tem
);
9343 tem
= generic_simplify (loc
, code
, type
, op0
);
9347 if (TREE_CODE_CLASS (code
) == tcc_unary
)
9349 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9350 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9351 fold_build1_loc (loc
, code
, type
,
9352 fold_convert_loc (loc
, TREE_TYPE (op0
),
9353 TREE_OPERAND (arg0
, 1))));
9354 else if (TREE_CODE (arg0
) == COND_EXPR
)
9356 tree arg01
= TREE_OPERAND (arg0
, 1);
9357 tree arg02
= TREE_OPERAND (arg0
, 2);
9358 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
9359 arg01
= fold_build1_loc (loc
, code
, type
,
9360 fold_convert_loc (loc
,
9361 TREE_TYPE (op0
), arg01
));
9362 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
9363 arg02
= fold_build1_loc (loc
, code
, type
,
9364 fold_convert_loc (loc
,
9365 TREE_TYPE (op0
), arg02
));
9366 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9369 /* If this was a conversion, and all we did was to move into
9370 inside the COND_EXPR, bring it back out. But leave it if
9371 it is a conversion from integer to integer and the
9372 result precision is no wider than a word since such a
9373 conversion is cheap and may be optimized away by combine,
9374 while it couldn't if it were outside the COND_EXPR. Then return
9375 so we don't get into an infinite recursion loop taking the
9376 conversion out and then back in. */
9378 if ((CONVERT_EXPR_CODE_P (code
)
9379 || code
== NON_LVALUE_EXPR
)
9380 && TREE_CODE (tem
) == COND_EXPR
9381 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
9382 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
9383 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 1)))
9384 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 2)))
9385 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
9386 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
9387 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9389 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
9390 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
9391 || flag_syntax_only
))
9392 tem
= build1_loc (loc
, code
, type
,
9394 TREE_TYPE (TREE_OPERAND
9395 (TREE_OPERAND (tem
, 1), 0)),
9396 TREE_OPERAND (tem
, 0),
9397 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
9398 TREE_OPERAND (TREE_OPERAND (tem
, 2),
9406 case NON_LVALUE_EXPR
:
9407 if (!maybe_lvalue_p (op0
))
9408 return fold_convert_loc (loc
, type
, op0
);
9413 case FIX_TRUNC_EXPR
:
9414 if (COMPARISON_CLASS_P (op0
))
9416 /* If we have (type) (a CMP b) and type is an integral type, return
9417 new expression involving the new type. Canonicalize
9418 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9420 Do not fold the result as that would not simplify further, also
9421 folding again results in recursions. */
9422 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
9423 return build2_loc (loc
, TREE_CODE (op0
), type
,
9424 TREE_OPERAND (op0
, 0),
9425 TREE_OPERAND (op0
, 1));
9426 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
9427 && TREE_CODE (type
) != VECTOR_TYPE
)
9428 return build3_loc (loc
, COND_EXPR
, type
, op0
,
9429 constant_boolean_node (true, type
),
9430 constant_boolean_node (false, type
));
9433 /* Handle (T *)&A.B.C for A being of type T and B and C
9434 living at offset zero. This occurs frequently in
9435 C++ upcasting and then accessing the base. */
9436 if (TREE_CODE (op0
) == ADDR_EXPR
9437 && POINTER_TYPE_P (type
)
9438 && handled_component_p (TREE_OPERAND (op0
, 0)))
9440 poly_int64 bitsize
, bitpos
;
9443 int unsignedp
, reversep
, volatilep
;
9445 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
9446 &offset
, &mode
, &unsignedp
, &reversep
,
9448 /* If the reference was to a (constant) zero offset, we can use
9449 the address of the base if it has the same base type
9450 as the result type and the pointer type is unqualified. */
9452 && known_eq (bitpos
, 0)
9453 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
9454 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
9455 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
9456 return fold_convert_loc (loc
, type
,
9457 build_fold_addr_expr_loc (loc
, base
));
9460 if (TREE_CODE (op0
) == MODIFY_EXPR
9461 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
9462 /* Detect assigning a bitfield. */
9463 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
9465 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
9467 /* Don't leave an assignment inside a conversion
9468 unless assigning a bitfield. */
9469 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
9470 /* First do the assignment, then return converted constant. */
9471 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
9472 suppress_warning (tem
/* What warning? */);
9473 TREE_USED (tem
) = 1;
9477 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9478 constants (if x has signed type, the sign bit cannot be set
9479 in c). This folds extension into the BIT_AND_EXPR.
9480 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9481 very likely don't have maximal range for their precision and this
9482 transformation effectively doesn't preserve non-maximal ranges. */
9483 if (TREE_CODE (type
) == INTEGER_TYPE
9484 && TREE_CODE (op0
) == BIT_AND_EXPR
9485 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
9487 tree and_expr
= op0
;
9488 tree and0
= TREE_OPERAND (and_expr
, 0);
9489 tree and1
= TREE_OPERAND (and_expr
, 1);
9492 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
9493 || (TYPE_PRECISION (type
)
9494 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
9496 else if (TYPE_PRECISION (TREE_TYPE (and1
))
9497 <= HOST_BITS_PER_WIDE_INT
9498 && tree_fits_uhwi_p (and1
))
9500 unsigned HOST_WIDE_INT cst
;
9502 cst
= tree_to_uhwi (and1
);
9503 cst
&= HOST_WIDE_INT_M1U
9504 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
9505 change
= (cst
== 0);
9507 && !flag_syntax_only
9508 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
9511 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
9512 and0
= fold_convert_loc (loc
, uns
, and0
);
9513 and1
= fold_convert_loc (loc
, uns
, and1
);
9518 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
9519 TREE_OVERFLOW (and1
));
9520 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9521 fold_convert_loc (loc
, type
, and0
), tem
);
9525 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9526 cast (T1)X will fold away. We assume that this happens when X itself
9528 if (POINTER_TYPE_P (type
)
9529 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9530 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
9532 tree arg00
= TREE_OPERAND (arg0
, 0);
9533 tree arg01
= TREE_OPERAND (arg0
, 1);
9535 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9536 when the pointed type needs higher alignment than
9537 the p+ first operand's pointed type. */
9539 && sanitize_flags_p (SANITIZE_ALIGNMENT
)
9540 && (min_align_of_type (TREE_TYPE (type
))
9541 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00
)))))
9544 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9545 when type is a reference type and arg00's type is not,
9546 because arg00 could be validly nullptr and if arg01 doesn't return,
9547 we don't want false positive binding of reference to nullptr. */
9548 if (TREE_CODE (type
) == REFERENCE_TYPE
9550 && sanitize_flags_p (SANITIZE_NULL
)
9551 && TREE_CODE (TREE_TYPE (arg00
)) != REFERENCE_TYPE
)
9554 arg00
= fold_convert_loc (loc
, type
, arg00
);
9555 return fold_build_pointer_plus_loc (loc
, arg00
, arg01
);
9558 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9559 of the same precision, and X is an integer type not narrower than
9560 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9561 if (INTEGRAL_TYPE_P (type
)
9562 && TREE_CODE (op0
) == BIT_NOT_EXPR
9563 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9564 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
9565 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
9567 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
9568 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9569 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
9570 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
9571 fold_convert_loc (loc
, type
, tem
));
9574 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9575 type of X and Y (integer types only). */
9576 if (INTEGRAL_TYPE_P (type
)
9577 && TREE_CODE (op0
) == MULT_EXPR
9578 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9579 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
))
9580 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
9581 || !sanitize_flags_p (SANITIZE_SI_OVERFLOW
)))
9583 /* Be careful not to introduce new overflows. */
9585 if (TYPE_OVERFLOW_WRAPS (type
))
9588 mult_type
= unsigned_type_for (type
);
9590 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9592 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
9593 fold_convert_loc (loc
, mult_type
,
9594 TREE_OPERAND (op0
, 0)),
9595 fold_convert_loc (loc
, mult_type
,
9596 TREE_OPERAND (op0
, 1)));
9597 return fold_convert_loc (loc
, type
, tem
);
9603 case VIEW_CONVERT_EXPR
:
9604 if (TREE_CODE (op0
) == MEM_REF
)
9606 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
9607 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
9608 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
9609 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
9610 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
9617 tem
= fold_negate_expr (loc
, arg0
);
9619 return fold_convert_loc (loc
, type
, tem
);
9623 /* Convert fabs((double)float) into (double)fabsf(float). */
9624 if (TREE_CODE (arg0
) == NOP_EXPR
9625 && TREE_CODE (type
) == REAL_TYPE
)
9627 tree targ0
= strip_float_extensions (arg0
);
9629 return fold_convert_loc (loc
, type
,
9630 fold_build1_loc (loc
, ABS_EXPR
,
9637 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9638 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9639 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9640 fold_convert_loc (loc
, type
,
9641 TREE_OPERAND (arg0
, 0)))))
9642 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
9643 fold_convert_loc (loc
, type
,
9644 TREE_OPERAND (arg0
, 1)));
9645 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9646 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9647 fold_convert_loc (loc
, type
,
9648 TREE_OPERAND (arg0
, 1)))))
9649 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
9650 fold_convert_loc (loc
, type
,
9651 TREE_OPERAND (arg0
, 0)), tem
);
9655 case TRUTH_NOT_EXPR
:
9656 /* Note that the operand of this must be an int
9657 and its values must be 0 or 1.
9658 ("true" is a fixed value perhaps depending on the language,
9659 but we don't handle values other than 1 correctly yet.) */
9660 tem
= fold_truth_not_expr (loc
, arg0
);
9663 return fold_convert_loc (loc
, type
, tem
);
9666 /* Fold *&X to X if X is an lvalue. */
9667 if (TREE_CODE (op0
) == ADDR_EXPR
)
9669 tree op00
= TREE_OPERAND (op0
, 0);
9671 || TREE_CODE (op00
) == PARM_DECL
9672 || TREE_CODE (op00
) == RESULT_DECL
)
9673 && !TREE_READONLY (op00
))
9680 } /* switch (code) */
9684 /* If the operation was a conversion do _not_ mark a resulting constant
9685 with TREE_OVERFLOW if the original constant was not. These conversions
9686 have implementation defined behavior and retaining the TREE_OVERFLOW
9687 flag here would confuse later passes such as VRP. */
9689 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
9690 tree type
, tree op0
)
9692 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
9694 && TREE_CODE (res
) == INTEGER_CST
9695 && TREE_CODE (op0
) == INTEGER_CST
9696 && CONVERT_EXPR_CODE_P (code
))
9697 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
9702 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9703 operands OP0 and OP1. LOC is the location of the resulting expression.
9704 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9705 Return the folded expression if folding is successful. Otherwise,
9706 return NULL_TREE. */
9708 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
9709 tree arg0
, tree arg1
, tree op0
, tree op1
)
9713 /* We only do these simplifications if we are optimizing. */
9717 /* Check for things like (A || B) && (A || C). We can convert this
9718 to A || (B && C). Note that either operator can be any of the four
9719 truth and/or operations and the transformation will still be
9720 valid. Also note that we only care about order for the
9721 ANDIF and ORIF operators. If B contains side effects, this
9722 might change the truth-value of A. */
9723 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9724 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
9725 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
9726 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
9727 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
9728 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
9730 tree a00
= TREE_OPERAND (arg0
, 0);
9731 tree a01
= TREE_OPERAND (arg0
, 1);
9732 tree a10
= TREE_OPERAND (arg1
, 0);
9733 tree a11
= TREE_OPERAND (arg1
, 1);
9734 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
9735 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
9736 && (code
== TRUTH_AND_EXPR
9737 || code
== TRUTH_OR_EXPR
));
9739 if (operand_equal_p (a00
, a10
, 0))
9740 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9741 fold_build2_loc (loc
, code
, type
, a01
, a11
));
9742 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
9743 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9744 fold_build2_loc (loc
, code
, type
, a01
, a10
));
9745 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
9746 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
9747 fold_build2_loc (loc
, code
, type
, a00
, a11
));
9749 /* This case if tricky because we must either have commutative
9750 operators or else A10 must not have side-effects. */
9752 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
9753 && operand_equal_p (a01
, a11
, 0))
9754 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
9755 fold_build2_loc (loc
, code
, type
, a00
, a10
),
9759 /* See if we can build a range comparison. */
9760 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
9763 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
9764 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
9766 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
9768 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
9771 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
9772 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
9774 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
9776 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
9779 /* Check for the possibility of merging component references. If our
9780 lhs is another similar operation, try to merge its rhs with our
9781 rhs. Then try to merge our lhs and rhs. */
9782 if (TREE_CODE (arg0
) == code
9783 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
9784 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
9785 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9787 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
9790 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
9791 if (param_logical_op_non_short_circuit
!= -1)
9792 logical_op_non_short_circuit
9793 = param_logical_op_non_short_circuit
;
9794 if (logical_op_non_short_circuit
9795 && !sanitize_coverage_p ()
9796 && (code
== TRUTH_AND_EXPR
9797 || code
== TRUTH_ANDIF_EXPR
9798 || code
== TRUTH_OR_EXPR
9799 || code
== TRUTH_ORIF_EXPR
))
9801 enum tree_code ncode
, icode
;
9803 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
9804 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
9805 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
9807 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9808 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9809 We don't want to pack more than two leafs to a non-IF AND/OR
9811 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9812 equal to IF-CODE, then we don't want to add right-hand operand.
9813 If the inner right-hand side of left-hand operand has
9814 side-effects, or isn't simple, then we can't add to it,
9815 as otherwise we might destroy if-sequence. */
9816 if (TREE_CODE (arg0
) == icode
9817 && simple_condition_p (arg1
)
9818 /* Needed for sequence points to handle trappings, and
9820 && simple_condition_p (TREE_OPERAND (arg0
, 1)))
9822 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
9824 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
9827 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9828 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9829 else if (TREE_CODE (arg1
) == icode
9830 && simple_condition_p (arg0
)
9831 /* Needed for sequence points to handle trappings, and
9833 && simple_condition_p (TREE_OPERAND (arg1
, 0)))
9835 tem
= fold_build2_loc (loc
, ncode
, type
,
9836 arg0
, TREE_OPERAND (arg1
, 0));
9837 return fold_build2_loc (loc
, icode
, type
, tem
,
9838 TREE_OPERAND (arg1
, 1));
9840 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9842 For sequence point consistancy, we need to check for trapping,
9843 and side-effects. */
9844 else if (code
== icode
&& simple_condition_p (arg0
)
9845 && simple_condition_p (arg1
))
9846 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
9852 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9853 by changing CODE to reduce the magnitude of constants involved in
9854 ARG0 of the comparison.
9855 Returns a canonicalized comparison tree if a simplification was
9856 possible, otherwise returns NULL_TREE.
9857 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9858 valid if signed overflow is undefined. */
9861 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
9862 tree arg0
, tree arg1
,
9863 bool *strict_overflow_p
)
9865 enum tree_code code0
= TREE_CODE (arg0
);
9866 tree t
, cst0
= NULL_TREE
;
9869 /* Match A +- CST code arg1. We can change this only if overflow
9871 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9872 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
9873 /* In principle pointers also have undefined overflow behavior,
9874 but that causes problems elsewhere. */
9875 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
9876 && (code0
== MINUS_EXPR
9877 || code0
== PLUS_EXPR
)
9878 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
9881 /* Identify the constant in arg0 and its sign. */
9882 cst0
= TREE_OPERAND (arg0
, 1);
9883 sgn0
= tree_int_cst_sgn (cst0
);
9885 /* Overflowed constants and zero will cause problems. */
9886 if (integer_zerop (cst0
)
9887 || TREE_OVERFLOW (cst0
))
9890 /* See if we can reduce the magnitude of the constant in
9891 arg0 by changing the comparison code. */
9892 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9894 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9896 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9897 else if (code
== GT_EXPR
9898 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9900 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9901 else if (code
== LE_EXPR
9902 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9904 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9905 else if (code
== GE_EXPR
9906 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9910 *strict_overflow_p
= true;
9912 /* Now build the constant reduced in magnitude. But not if that
9913 would produce one outside of its types range. */
9914 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
9916 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
9917 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
9919 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
9920 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
9923 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
9924 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
9925 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
9926 t
= fold_convert (TREE_TYPE (arg1
), t
);
9928 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
9931 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9932 overflow further. Try to decrease the magnitude of constants involved
9933 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9934 and put sole constants at the second argument position.
9935 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9938 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
9939 tree arg0
, tree arg1
)
9942 bool strict_overflow_p
;
9943 const char * const warnmsg
= G_("assuming signed overflow does not occur "
9944 "when reducing constant in comparison");
9946 /* Try canonicalization by simplifying arg0. */
9947 strict_overflow_p
= false;
9948 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
9949 &strict_overflow_p
);
9952 if (strict_overflow_p
)
9953 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9957 /* Try canonicalization by simplifying arg1 using the swapped
9959 code
= swap_tree_comparison (code
);
9960 strict_overflow_p
= false;
9961 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
9962 &strict_overflow_p
);
9963 if (t
&& strict_overflow_p
)
9964 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9968 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9969 space. This is used to avoid issuing overflow warnings for
9970 expressions like &p->x which cannot wrap. */
9973 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
9975 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
9978 if (maybe_lt (bitpos
, 0))
9981 poly_wide_int wi_offset
;
9982 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
9983 if (offset
== NULL_TREE
)
9984 wi_offset
= wi::zero (precision
);
9985 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
9988 wi_offset
= wi::to_poly_wide (offset
);
9990 wi::overflow_type overflow
;
9991 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
9993 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
9997 poly_uint64 total_hwi
, size
;
9998 if (!total
.to_uhwi (&total_hwi
)
9999 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
10001 || known_eq (size
, 0U))
10004 if (known_le (total_hwi
, size
))
10007 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
10009 if (TREE_CODE (base
) == ADDR_EXPR
10010 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
10012 && maybe_ne (size
, 0U)
10013 && known_le (total_hwi
, size
))
10019 /* Return a positive integer when the symbol DECL is known to have
10020 a nonzero address, zero when it's known not to (e.g., it's a weak
10021 symbol), and a negative integer when the symbol is not yet in the
10022 symbol table and so whether or not its address is zero is unknown.
10023 For function local objects always return positive integer. */
10025 maybe_nonzero_address (tree decl
)
10027 /* Normally, don't do anything for variables and functions before symtab is
10028 built; it is quite possible that DECL will be declared weak later.
10029 But if folding_initializer, we need a constant answer now, so create
10030 the symtab entry and prevent later weak declaration. */
10031 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
10032 if (struct symtab_node
*symbol
10033 = (folding_initializer
10034 ? symtab_node::get_create (decl
)
10035 : symtab_node::get (decl
)))
10036 return symbol
->nonzero_address ();
10038 /* Function local objects are never NULL. */
10040 && (DECL_CONTEXT (decl
)
10041 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
10042 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
10048 /* Subroutine of fold_binary. This routine performs all of the
10049 transformations that are common to the equality/inequality
10050 operators (EQ_EXPR and NE_EXPR) and the ordering operators
10051 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10052 fold_binary should call fold_binary. Fold a comparison with
10053 tree code CODE and type TYPE with operands OP0 and OP1. Return
10054 the folded comparison or NULL_TREE. */
10057 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
10058 tree op0
, tree op1
)
10060 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
10061 tree arg0
, arg1
, tem
;
10066 STRIP_SIGN_NOPS (arg0
);
10067 STRIP_SIGN_NOPS (arg1
);
10069 /* For comparisons of pointers we can decompose it to a compile time
10070 comparison of the base objects and the offsets into the object.
10071 This requires at least one operand being an ADDR_EXPR or a
10072 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10073 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
10074 && (TREE_CODE (arg0
) == ADDR_EXPR
10075 || TREE_CODE (arg1
) == ADDR_EXPR
10076 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10077 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
10079 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
10080 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
10082 int volatilep
, reversep
, unsignedp
;
10083 bool indirect_base0
= false, indirect_base1
= false;
10085 /* Get base and offset for the access. Strip ADDR_EXPR for
10086 get_inner_reference, but put it back by stripping INDIRECT_REF
10087 off the base object if possible. indirect_baseN will be true
10088 if baseN is not an address but refers to the object itself. */
10090 if (TREE_CODE (arg0
) == ADDR_EXPR
)
10093 = get_inner_reference (TREE_OPERAND (arg0
, 0),
10094 &bitsize
, &bitpos0
, &offset0
, &mode
,
10095 &unsignedp
, &reversep
, &volatilep
);
10096 if (TREE_CODE (base0
) == INDIRECT_REF
)
10097 base0
= TREE_OPERAND (base0
, 0);
10099 indirect_base0
= true;
10101 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10103 base0
= TREE_OPERAND (arg0
, 0);
10104 STRIP_SIGN_NOPS (base0
);
10105 if (TREE_CODE (base0
) == ADDR_EXPR
)
10108 = get_inner_reference (TREE_OPERAND (base0
, 0),
10109 &bitsize
, &bitpos0
, &offset0
, &mode
,
10110 &unsignedp
, &reversep
, &volatilep
);
10111 if (TREE_CODE (base0
) == INDIRECT_REF
)
10112 base0
= TREE_OPERAND (base0
, 0);
10114 indirect_base0
= true;
10116 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
10117 offset0
= TREE_OPERAND (arg0
, 1);
10119 offset0
= size_binop (PLUS_EXPR
, offset0
,
10120 TREE_OPERAND (arg0
, 1));
10121 if (poly_int_tree_p (offset0
))
10123 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
10124 TYPE_PRECISION (sizetype
));
10125 tem
<<= LOG2_BITS_PER_UNIT
;
10127 if (tem
.to_shwi (&bitpos0
))
10128 offset0
= NULL_TREE
;
10133 if (TREE_CODE (arg1
) == ADDR_EXPR
)
10136 = get_inner_reference (TREE_OPERAND (arg1
, 0),
10137 &bitsize
, &bitpos1
, &offset1
, &mode
,
10138 &unsignedp
, &reversep
, &volatilep
);
10139 if (TREE_CODE (base1
) == INDIRECT_REF
)
10140 base1
= TREE_OPERAND (base1
, 0);
10142 indirect_base1
= true;
10144 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10146 base1
= TREE_OPERAND (arg1
, 0);
10147 STRIP_SIGN_NOPS (base1
);
10148 if (TREE_CODE (base1
) == ADDR_EXPR
)
10151 = get_inner_reference (TREE_OPERAND (base1
, 0),
10152 &bitsize
, &bitpos1
, &offset1
, &mode
,
10153 &unsignedp
, &reversep
, &volatilep
);
10154 if (TREE_CODE (base1
) == INDIRECT_REF
)
10155 base1
= TREE_OPERAND (base1
, 0);
10157 indirect_base1
= true;
10159 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
10160 offset1
= TREE_OPERAND (arg1
, 1);
10162 offset1
= size_binop (PLUS_EXPR
, offset1
,
10163 TREE_OPERAND (arg1
, 1));
10164 if (poly_int_tree_p (offset1
))
10166 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
10167 TYPE_PRECISION (sizetype
));
10168 tem
<<= LOG2_BITS_PER_UNIT
;
10170 if (tem
.to_shwi (&bitpos1
))
10171 offset1
= NULL_TREE
;
10175 /* If we have equivalent bases we might be able to simplify. */
10176 if (indirect_base0
== indirect_base1
10177 && operand_equal_p (base0
, base1
,
10178 indirect_base0
? OEP_ADDRESS_OF
: 0))
10180 /* We can fold this expression to a constant if the non-constant
10181 offset parts are equal. */
10182 if ((offset0
== offset1
10183 || (offset0
&& offset1
10184 && operand_equal_p (offset0
, offset1
, 0)))
10187 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10188 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10191 && maybe_ne (bitpos0
, bitpos1
)
10192 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10193 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10194 fold_overflow_warning (("assuming pointer wraparound does not "
10195 "occur when comparing P +- C1 with "
10197 WARN_STRICT_OVERFLOW_CONDITIONAL
);
10202 if (known_eq (bitpos0
, bitpos1
))
10203 return constant_boolean_node (true, type
);
10204 if (known_ne (bitpos0
, bitpos1
))
10205 return constant_boolean_node (false, type
);
10208 if (known_ne (bitpos0
, bitpos1
))
10209 return constant_boolean_node (true, type
);
10210 if (known_eq (bitpos0
, bitpos1
))
10211 return constant_boolean_node (false, type
);
10214 if (known_lt (bitpos0
, bitpos1
))
10215 return constant_boolean_node (true, type
);
10216 if (known_ge (bitpos0
, bitpos1
))
10217 return constant_boolean_node (false, type
);
10220 if (known_le (bitpos0
, bitpos1
))
10221 return constant_boolean_node (true, type
);
10222 if (known_gt (bitpos0
, bitpos1
))
10223 return constant_boolean_node (false, type
);
10226 if (known_ge (bitpos0
, bitpos1
))
10227 return constant_boolean_node (true, type
);
10228 if (known_lt (bitpos0
, bitpos1
))
10229 return constant_boolean_node (false, type
);
10232 if (known_gt (bitpos0
, bitpos1
))
10233 return constant_boolean_node (true, type
);
10234 if (known_le (bitpos0
, bitpos1
))
10235 return constant_boolean_node (false, type
);
10240 /* We can simplify the comparison to a comparison of the variable
10241 offset parts if the constant offset parts are equal.
10242 Be careful to use signed sizetype here because otherwise we
10243 mess with array offsets in the wrong way. This is possible
10244 because pointer arithmetic is restricted to retain within an
10245 object and overflow on pointer differences is undefined as of
10246 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10247 else if (known_eq (bitpos0
, bitpos1
)
10250 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10251 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10253 /* By converting to signed sizetype we cover middle-end pointer
10254 arithmetic which operates on unsigned pointer types of size
10255 type size and ARRAY_REF offsets which are properly sign or
10256 zero extended from their type in case it is narrower than
10258 if (offset0
== NULL_TREE
)
10259 offset0
= build_int_cst (ssizetype
, 0);
10261 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
10262 if (offset1
== NULL_TREE
)
10263 offset1
= build_int_cst (ssizetype
, 0);
10265 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
10268 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10269 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10270 fold_overflow_warning (("assuming pointer wraparound does not "
10271 "occur when comparing P +- C1 with "
10273 WARN_STRICT_OVERFLOW_COMPARISON
);
10275 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
10278 /* For equal offsets we can simplify to a comparison of the
10280 else if (known_eq (bitpos0
, bitpos1
)
10282 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
10284 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
10285 && ((offset0
== offset1
)
10286 || (offset0
&& offset1
10287 && operand_equal_p (offset0
, offset1
, 0))))
10289 if (indirect_base0
)
10290 base0
= build_fold_addr_expr_loc (loc
, base0
);
10291 if (indirect_base1
)
10292 base1
= build_fold_addr_expr_loc (loc
, base1
);
10293 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
10295 /* Comparison between an ordinary (non-weak) symbol and a null
10296 pointer can be eliminated since such symbols must have a non
10297 null address. In C, relational expressions between pointers
10298 to objects and null pointers are undefined. The results
10299 below follow the C++ rules with the additional property that
10300 every object pointer compares greater than a null pointer.
10302 else if (((DECL_P (base0
)
10303 && maybe_nonzero_address (base0
) > 0
10304 /* Avoid folding references to struct members at offset 0 to
10305 prevent tests like '&ptr->firstmember == 0' from getting
10306 eliminated. When ptr is null, although the -> expression
10307 is strictly speaking invalid, GCC retains it as a matter
10308 of QoI. See PR c/44555. */
10309 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
10310 || CONSTANT_CLASS_P (base0
))
10312 /* The caller guarantees that when one of the arguments is
10313 constant (i.e., null in this case) it is second. */
10314 && integer_zerop (arg1
))
10321 return constant_boolean_node (false, type
);
10325 return constant_boolean_node (true, type
);
10327 gcc_unreachable ();
10332 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10333 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10334 the resulting offset is smaller in absolute value than the
10335 original one and has the same sign. */
10336 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10337 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
10338 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10339 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10340 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
10341 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
10342 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10343 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
10345 tree const1
= TREE_OPERAND (arg0
, 1);
10346 tree const2
= TREE_OPERAND (arg1
, 1);
10347 tree variable1
= TREE_OPERAND (arg0
, 0);
10348 tree variable2
= TREE_OPERAND (arg1
, 0);
10350 const char * const warnmsg
= G_("assuming signed overflow does not "
10351 "occur when combining constants around "
10354 /* Put the constant on the side where it doesn't overflow and is
10355 of lower absolute value and of same sign than before. */
10356 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10357 ? MINUS_EXPR
: PLUS_EXPR
,
10359 if (!TREE_OVERFLOW (cst
)
10360 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
10361 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
10363 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10364 return fold_build2_loc (loc
, code
, type
,
10366 fold_build2_loc (loc
, TREE_CODE (arg1
),
10371 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10372 ? MINUS_EXPR
: PLUS_EXPR
,
10374 if (!TREE_OVERFLOW (cst
)
10375 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
10376 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
10378 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10379 return fold_build2_loc (loc
, code
, type
,
10380 fold_build2_loc (loc
, TREE_CODE (arg0
),
10387 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
10391 /* If we are comparing an expression that just has comparisons
10392 of two integer values, arithmetic expressions of those comparisons,
10393 and constants, we can simplify it. There are only three cases
10394 to check: the two values can either be equal, the first can be
10395 greater, or the second can be greater. Fold the expression for
10396 those three values. Since each value must be 0 or 1, we have
10397 eight possibilities, each of which corresponds to the constant 0
10398 or 1 or one of the six possible comparisons.
10400 This handles common cases like (a > b) == 0 but also handles
10401 expressions like ((x > y) - (y > x)) > 0, which supposedly
10402 occur in macroized code. */
10404 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
10406 tree cval1
= 0, cval2
= 0;
10408 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
10409 /* Don't handle degenerate cases here; they should already
10410 have been handled anyway. */
10411 && cval1
!= 0 && cval2
!= 0
10412 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
10413 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
10414 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
10415 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
10416 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
10417 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
10418 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
10420 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
10421 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
10423 /* We can't just pass T to eval_subst in case cval1 or cval2
10424 was the same as ARG1. */
10427 = fold_build2_loc (loc
, code
, type
,
10428 eval_subst (loc
, arg0
, cval1
, maxval
,
10432 = fold_build2_loc (loc
, code
, type
,
10433 eval_subst (loc
, arg0
, cval1
, maxval
,
10437 = fold_build2_loc (loc
, code
, type
,
10438 eval_subst (loc
, arg0
, cval1
, minval
,
10442 /* All three of these results should be 0 or 1. Confirm they are.
10443 Then use those values to select the proper code to use. */
10445 if (TREE_CODE (high_result
) == INTEGER_CST
10446 && TREE_CODE (equal_result
) == INTEGER_CST
10447 && TREE_CODE (low_result
) == INTEGER_CST
)
10449 /* Make a 3-bit mask with the high-order bit being the
10450 value for `>', the next for '=', and the low for '<'. */
10451 switch ((integer_onep (high_result
) * 4)
10452 + (integer_onep (equal_result
) * 2)
10453 + integer_onep (low_result
))
10456 /* Always false. */
10457 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10478 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10481 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
10490 /* Subroutine of fold_binary. Optimize complex multiplications of the
10491 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10492 argument EXPR represents the expression "z" of type TYPE. */
10495 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
10497 tree itype
= TREE_TYPE (type
);
10498 tree rpart
, ipart
, tem
;
10500 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
10502 rpart
= TREE_OPERAND (expr
, 0);
10503 ipart
= TREE_OPERAND (expr
, 1);
10505 else if (TREE_CODE (expr
) == COMPLEX_CST
)
10507 rpart
= TREE_REALPART (expr
);
10508 ipart
= TREE_IMAGPART (expr
);
10512 expr
= save_expr (expr
);
10513 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
10514 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
10517 rpart
= save_expr (rpart
);
10518 ipart
= save_expr (ipart
);
10519 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
10520 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
10521 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
10522 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
10523 build_zero_cst (itype
));
10527 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10528 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10529 true if successful. */
10532 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
10534 unsigned HOST_WIDE_INT i
, nunits
;
10536 if (TREE_CODE (arg
) == VECTOR_CST
10537 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
10539 for (i
= 0; i
< nunits
; ++i
)
10540 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
10542 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
10544 constructor_elt
*elt
;
10546 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
10547 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
10550 elts
[i
] = elt
->value
;
10554 for (; i
< nelts
; i
++)
10556 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
10560 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10561 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10562 NULL_TREE otherwise. */
10565 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10568 unsigned HOST_WIDE_INT nelts
;
10569 bool need_ctor
= false;
10571 if (!sel
.length ().is_constant (&nelts
))
10573 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), nelts
)
10574 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)), nelts
)
10575 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)), nelts
));
10576 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10577 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10580 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10581 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10582 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10585 tree_vector_builder
out_elts (type
, nelts
, 1);
10586 for (i
= 0; i
< nelts
; i
++)
10588 HOST_WIDE_INT index
;
10589 if (!sel
[i
].is_constant (&index
))
10591 if (!CONSTANT_CLASS_P (in_elts
[index
]))
10593 out_elts
.quick_push (unshare_expr (in_elts
[index
]));
10598 vec
<constructor_elt
, va_gc
> *v
;
10599 vec_alloc (v
, nelts
);
10600 for (i
= 0; i
< nelts
; i
++)
10601 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
10602 return build_constructor (type
, v
);
10605 return out_elts
.build ();
10608 /* Try to fold a pointer difference of type TYPE two address expressions of
10609 array references AREF0 and AREF1 using location LOC. Return a
10610 simplified expression for the difference or NULL_TREE. */
10613 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10614 tree aref0
, tree aref1
,
10615 bool use_pointer_diff
)
10617 tree base0
= TREE_OPERAND (aref0
, 0);
10618 tree base1
= TREE_OPERAND (aref1
, 0);
10619 tree base_offset
= build_int_cst (type
, 0);
10621 /* If the bases are array references as well, recurse. If the bases
10622 are pointer indirections compute the difference of the pointers.
10623 If the bases are equal, we are set. */
10624 if ((TREE_CODE (base0
) == ARRAY_REF
10625 && TREE_CODE (base1
) == ARRAY_REF
10627 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10628 use_pointer_diff
)))
10629 || (INDIRECT_REF_P (base0
)
10630 && INDIRECT_REF_P (base1
)
10633 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10634 TREE_OPERAND (base0
, 0),
10635 TREE_OPERAND (base1
, 0))
10636 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10637 fold_convert (type
,
10638 TREE_OPERAND (base0
, 0)),
10639 fold_convert (type
,
10640 TREE_OPERAND (base1
, 0)))))
10641 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10643 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10644 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10645 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10646 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10647 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10649 fold_build2_loc (loc
, MULT_EXPR
, type
,
10655 /* If the real or vector real constant CST of type TYPE has an exact
10656 inverse, return it, else return NULL. */
10659 exact_inverse (tree type
, tree cst
)
10665 switch (TREE_CODE (cst
))
10668 r
= TREE_REAL_CST (cst
);
10670 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10671 return build_real (type
, r
);
10677 unit_type
= TREE_TYPE (type
);
10678 mode
= TYPE_MODE (unit_type
);
10680 tree_vector_builder elts
;
10681 if (!elts
.new_unary_operation (type
, cst
, false))
10683 unsigned int count
= elts
.encoded_nelts ();
10684 for (unsigned int i
= 0; i
< count
; ++i
)
10686 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
10687 if (!exact_real_inverse (mode
, &r
))
10689 elts
.quick_push (build_real (unit_type
, r
));
10692 return elts
.build ();
10700 /* Mask out the tz least significant bits of X of type TYPE where
10701 tz is the number of trailing zeroes in Y. */
10703 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
10705 int tz
= wi::ctz (y
);
10707 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
10711 /* Return true when T is an address and is known to be nonzero.
10712 For floating point we further ensure that T is not denormal.
10713 Similar logic is present in nonzero_address in rtlanal.h.
10715 If the return value is based on the assumption that signed overflow
10716 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10717 change *STRICT_OVERFLOW_P. */
10720 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
10722 tree type
= TREE_TYPE (t
);
10723 enum tree_code code
;
10725 /* Doing something useful for floating point would need more work. */
10726 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
10729 code
= TREE_CODE (t
);
10730 switch (TREE_CODE_CLASS (code
))
10733 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10734 strict_overflow_p
);
10736 case tcc_comparison
:
10737 return tree_binary_nonzero_warnv_p (code
, type
,
10738 TREE_OPERAND (t
, 0),
10739 TREE_OPERAND (t
, 1),
10740 strict_overflow_p
);
10742 case tcc_declaration
:
10743 case tcc_reference
:
10744 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10752 case TRUTH_NOT_EXPR
:
10753 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10754 strict_overflow_p
);
10756 case TRUTH_AND_EXPR
:
10757 case TRUTH_OR_EXPR
:
10758 case TRUTH_XOR_EXPR
:
10759 return tree_binary_nonzero_warnv_p (code
, type
,
10760 TREE_OPERAND (t
, 0),
10761 TREE_OPERAND (t
, 1),
10762 strict_overflow_p
);
10768 case WITH_SIZE_EXPR
:
10770 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10772 case COMPOUND_EXPR
:
10775 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
10776 strict_overflow_p
);
10779 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
10780 strict_overflow_p
);
10784 tree fndecl
= get_callee_fndecl (t
);
10785 if (!fndecl
) return false;
10786 if (flag_delete_null_pointer_checks
&& !flag_check_new
10787 && DECL_IS_OPERATOR_NEW_P (fndecl
)
10788 && !TREE_NOTHROW (fndecl
))
10790 if (flag_delete_null_pointer_checks
10791 && lookup_attribute ("returns_nonnull",
10792 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
10794 return alloca_call_p (t
);
10803 /* Return true when T is an address and is known to be nonzero.
10804 Handle warnings about undefined signed overflow. */
10807 tree_expr_nonzero_p (tree t
)
10809 bool ret
, strict_overflow_p
;
10811 strict_overflow_p
= false;
10812 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
10813 if (strict_overflow_p
)
10814 fold_overflow_warning (("assuming signed overflow does not occur when "
10815 "determining that expression is always "
10817 WARN_STRICT_OVERFLOW_MISC
);
10821 /* Return true if T is known not to be equal to an integer W. */
10824 expr_not_equal_to (tree t
, const wide_int
&w
)
10827 switch (TREE_CODE (t
))
10830 return wi::to_wide (t
) != w
;
10833 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
10837 get_range_query (cfun
)->range_of_expr (vr
, t
);
10839 get_global_range_query ()->range_of_expr (vr
, t
);
10841 if (!vr
.undefined_p ()
10842 && !vr
.contains_p (wide_int_to_tree (TREE_TYPE (t
), w
)))
10844 /* If T has some known zero bits and W has any of those bits set,
10845 then T is known not to be equal to W. */
10846 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
10847 TYPE_PRECISION (TREE_TYPE (t
))), 0))
10856 /* Fold a binary expression of code CODE and type TYPE with operands
10857 OP0 and OP1. LOC is the location of the resulting expression.
10858 Return the folded expression if folding is successful. Otherwise,
10859 return NULL_TREE. */
10862 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
10863 tree op0
, tree op1
)
10865 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
10866 tree arg0
, arg1
, tem
;
10867 tree t1
= NULL_TREE
;
10868 bool strict_overflow_p
;
10871 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
10872 && TREE_CODE_LENGTH (code
) == 2
10873 && op0
!= NULL_TREE
10874 && op1
!= NULL_TREE
);
10879 /* Strip any conversions that don't change the mode. This is
10880 safe for every expression, except for a comparison expression
10881 because its signedness is derived from its operands. So, in
10882 the latter case, only strip conversions that don't change the
10883 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10886 Note that this is done as an internal manipulation within the
10887 constant folder, in order to find the simplest representation
10888 of the arguments so that their form can be studied. In any
10889 cases, the appropriate type conversions should be put back in
10890 the tree that will get out of the constant folder. */
10892 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
10894 STRIP_SIGN_NOPS (arg0
);
10895 STRIP_SIGN_NOPS (arg1
);
10903 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10904 constant but we can't do arithmetic on them. */
10905 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
10907 tem
= const_binop (code
, type
, arg0
, arg1
);
10908 if (tem
!= NULL_TREE
)
10910 if (TREE_TYPE (tem
) != type
)
10911 tem
= fold_convert_loc (loc
, type
, tem
);
10916 /* If this is a commutative operation, and ARG0 is a constant, move it
10917 to ARG1 to reduce the number of tests below. */
10918 if (commutative_tree_code (code
)
10919 && tree_swap_operands_p (arg0
, arg1
))
10920 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
10922 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10923 to ARG1 to reduce the number of tests below. */
10924 if (kind
== tcc_comparison
10925 && tree_swap_operands_p (arg0
, arg1
))
10926 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
10928 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
10932 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10934 First check for cases where an arithmetic operation is applied to a
10935 compound, conditional, or comparison operation. Push the arithmetic
10936 operation inside the compound or conditional to see if any folding
10937 can then be done. Convert comparison to conditional for this purpose.
10938 The also optimizes non-constant cases that used to be done in
10941 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10942 one of the operands is a comparison and the other is a comparison, a
10943 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10944 code below would make the expression more complex. Change it to a
10945 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10946 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10948 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
10949 || code
== EQ_EXPR
|| code
== NE_EXPR
)
10950 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
10951 && ((truth_value_p (TREE_CODE (arg0
))
10952 && (truth_value_p (TREE_CODE (arg1
))
10953 || (TREE_CODE (arg1
) == BIT_AND_EXPR
10954 && integer_onep (TREE_OPERAND (arg1
, 1)))))
10955 || (truth_value_p (TREE_CODE (arg1
))
10956 && (truth_value_p (TREE_CODE (arg0
))
10957 || (TREE_CODE (arg0
) == BIT_AND_EXPR
10958 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
10960 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
10961 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
10964 fold_convert_loc (loc
, boolean_type_node
, arg0
),
10965 fold_convert_loc (loc
, boolean_type_node
, arg1
));
10967 if (code
== EQ_EXPR
)
10968 tem
= invert_truthvalue_loc (loc
, tem
);
10970 return fold_convert_loc (loc
, type
, tem
);
10973 if (TREE_CODE_CLASS (code
) == tcc_binary
10974 || TREE_CODE_CLASS (code
) == tcc_comparison
)
10976 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
10978 tem
= fold_build2_loc (loc
, code
, type
,
10979 fold_convert_loc (loc
, TREE_TYPE (op0
),
10980 TREE_OPERAND (arg0
, 1)), op1
);
10981 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10984 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
10986 tem
= fold_build2_loc (loc
, code
, type
, op0
,
10987 fold_convert_loc (loc
, TREE_TYPE (op1
),
10988 TREE_OPERAND (arg1
, 1)));
10989 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
10993 if (TREE_CODE (arg0
) == COND_EXPR
10994 || TREE_CODE (arg0
) == VEC_COND_EXPR
10995 || COMPARISON_CLASS_P (arg0
))
10997 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10999 /*cond_first_p=*/1);
11000 if (tem
!= NULL_TREE
)
11004 if (TREE_CODE (arg1
) == COND_EXPR
11005 || TREE_CODE (arg1
) == VEC_COND_EXPR
11006 || COMPARISON_CLASS_P (arg1
))
11008 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
11010 /*cond_first_p=*/0);
11011 if (tem
!= NULL_TREE
)
11019 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11020 if (TREE_CODE (arg0
) == ADDR_EXPR
11021 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
11023 tree iref
= TREE_OPERAND (arg0
, 0);
11024 return fold_build2 (MEM_REF
, type
,
11025 TREE_OPERAND (iref
, 0),
11026 int_const_binop (PLUS_EXPR
, arg1
,
11027 TREE_OPERAND (iref
, 1)));
11030 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11031 if (TREE_CODE (arg0
) == ADDR_EXPR
11032 && handled_component_p (TREE_OPERAND (arg0
, 0)))
11035 poly_int64 coffset
;
11036 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
11040 return fold_build2 (MEM_REF
, type
,
11041 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
11042 int_const_binop (PLUS_EXPR
, arg1
,
11043 size_int (coffset
)));
11048 case POINTER_PLUS_EXPR
:
11049 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11050 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11051 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
11052 return fold_convert_loc (loc
, type
,
11053 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
11054 fold_convert_loc (loc
, sizetype
,
11056 fold_convert_loc (loc
, sizetype
,
11062 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
11064 /* X + (X / CST) * -CST is X % CST. */
11065 if (TREE_CODE (arg1
) == MULT_EXPR
11066 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
11067 && operand_equal_p (arg0
,
11068 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
11070 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
11071 tree cst1
= TREE_OPERAND (arg1
, 1);
11072 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
11074 if (sum
&& integer_zerop (sum
))
11075 return fold_convert_loc (loc
, type
,
11076 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
11077 TREE_TYPE (arg0
), arg0
,
11082 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11083 one. Make sure the type is not saturating and has the signedness of
11084 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11085 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11086 if ((TREE_CODE (arg0
) == MULT_EXPR
11087 || TREE_CODE (arg1
) == MULT_EXPR
)
11088 && !TYPE_SATURATING (type
)
11089 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11090 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11091 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11093 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11098 if (! FLOAT_TYPE_P (type
))
11100 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11101 (plus (plus (mult) (mult)) (foo)) so that we can
11102 take advantage of the factoring cases below. */
11103 if (ANY_INTEGRAL_TYPE_P (type
)
11104 && TYPE_OVERFLOW_WRAPS (type
)
11105 && (((TREE_CODE (arg0
) == PLUS_EXPR
11106 || TREE_CODE (arg0
) == MINUS_EXPR
)
11107 && TREE_CODE (arg1
) == MULT_EXPR
)
11108 || ((TREE_CODE (arg1
) == PLUS_EXPR
11109 || TREE_CODE (arg1
) == MINUS_EXPR
)
11110 && TREE_CODE (arg0
) == MULT_EXPR
)))
11112 tree parg0
, parg1
, parg
, marg
;
11113 enum tree_code pcode
;
11115 if (TREE_CODE (arg1
) == MULT_EXPR
)
11116 parg
= arg0
, marg
= arg1
;
11118 parg
= arg1
, marg
= arg0
;
11119 pcode
= TREE_CODE (parg
);
11120 parg0
= TREE_OPERAND (parg
, 0);
11121 parg1
= TREE_OPERAND (parg
, 1);
11122 STRIP_NOPS (parg0
);
11123 STRIP_NOPS (parg1
);
11125 if (TREE_CODE (parg0
) == MULT_EXPR
11126 && TREE_CODE (parg1
) != MULT_EXPR
)
11127 return fold_build2_loc (loc
, pcode
, type
,
11128 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11129 fold_convert_loc (loc
, type
,
11131 fold_convert_loc (loc
, type
,
11133 fold_convert_loc (loc
, type
, parg1
));
11134 if (TREE_CODE (parg0
) != MULT_EXPR
11135 && TREE_CODE (parg1
) == MULT_EXPR
)
11137 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11138 fold_convert_loc (loc
, type
, parg0
),
11139 fold_build2_loc (loc
, pcode
, type
,
11140 fold_convert_loc (loc
, type
, marg
),
11141 fold_convert_loc (loc
, type
,
11147 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11148 to __complex__ ( x, y ). This is not the same for SNaNs or
11149 if signed zeros are involved. */
11150 if (!HONOR_SNANS (arg0
)
11151 && !HONOR_SIGNED_ZEROS (arg0
)
11152 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11154 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11155 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11156 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11157 bool arg0rz
= false, arg0iz
= false;
11158 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11159 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11161 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11162 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11163 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11165 tree rp
= arg1r
? arg1r
11166 : build1 (REALPART_EXPR
, rtype
, arg1
);
11167 tree ip
= arg0i
? arg0i
11168 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11169 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11171 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11173 tree rp
= arg0r
? arg0r
11174 : build1 (REALPART_EXPR
, rtype
, arg0
);
11175 tree ip
= arg1i
? arg1i
11176 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11177 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11182 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11183 We associate floats only if the user has specified
11184 -fassociative-math. */
11185 if (flag_associative_math
11186 && TREE_CODE (arg1
) == PLUS_EXPR
11187 && TREE_CODE (arg0
) != MULT_EXPR
)
11189 tree tree10
= TREE_OPERAND (arg1
, 0);
11190 tree tree11
= TREE_OPERAND (arg1
, 1);
11191 if (TREE_CODE (tree11
) == MULT_EXPR
11192 && TREE_CODE (tree10
) == MULT_EXPR
)
11195 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11196 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11199 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11200 We associate floats only if the user has specified
11201 -fassociative-math. */
11202 if (flag_associative_math
11203 && TREE_CODE (arg0
) == PLUS_EXPR
11204 && TREE_CODE (arg1
) != MULT_EXPR
)
11206 tree tree00
= TREE_OPERAND (arg0
, 0);
11207 tree tree01
= TREE_OPERAND (arg0
, 1);
11208 if (TREE_CODE (tree01
) == MULT_EXPR
11209 && TREE_CODE (tree00
) == MULT_EXPR
)
11212 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11213 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11219 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11220 is a rotate of A by C1 bits. */
11221 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11222 is a rotate of A by B bits.
11223 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11224 though in this case CODE must be | and not + or ^, otherwise
11225 it doesn't return A when B is 0. */
11227 enum tree_code code0
, code1
;
11229 code0
= TREE_CODE (arg0
);
11230 code1
= TREE_CODE (arg1
);
11231 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11232 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11233 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11234 TREE_OPERAND (arg1
, 0), 0)
11235 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11236 TYPE_UNSIGNED (rtype
))
11237 /* Only create rotates in complete modes. Other cases are not
11238 expanded properly. */
11239 && (element_precision (rtype
)
11240 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11242 tree tree01
, tree11
;
11243 tree orig_tree01
, orig_tree11
;
11244 enum tree_code code01
, code11
;
11246 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11247 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11248 STRIP_NOPS (tree01
);
11249 STRIP_NOPS (tree11
);
11250 code01
= TREE_CODE (tree01
);
11251 code11
= TREE_CODE (tree11
);
11252 if (code11
!= MINUS_EXPR
11253 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11255 std::swap (code0
, code1
);
11256 std::swap (code01
, code11
);
11257 std::swap (tree01
, tree11
);
11258 std::swap (orig_tree01
, orig_tree11
);
11260 if (code01
== INTEGER_CST
11261 && code11
== INTEGER_CST
11262 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11263 == element_precision (rtype
)))
11265 tem
= build2_loc (loc
, LROTATE_EXPR
,
11266 rtype
, TREE_OPERAND (arg0
, 0),
11267 code0
== LSHIFT_EXPR
11268 ? orig_tree01
: orig_tree11
);
11269 return fold_convert_loc (loc
, type
, tem
);
11271 else if (code11
== MINUS_EXPR
)
11273 tree tree110
, tree111
;
11274 tree110
= TREE_OPERAND (tree11
, 0);
11275 tree111
= TREE_OPERAND (tree11
, 1);
11276 STRIP_NOPS (tree110
);
11277 STRIP_NOPS (tree111
);
11278 if (TREE_CODE (tree110
) == INTEGER_CST
11279 && compare_tree_int (tree110
,
11280 element_precision (rtype
)) == 0
11281 && operand_equal_p (tree01
, tree111
, 0))
11283 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11284 ? LROTATE_EXPR
: RROTATE_EXPR
),
11285 rtype
, TREE_OPERAND (arg0
, 0),
11287 return fold_convert_loc (loc
, type
, tem
);
11290 else if (code
== BIT_IOR_EXPR
11291 && code11
== BIT_AND_EXPR
11292 && pow2p_hwi (element_precision (rtype
)))
11294 tree tree110
, tree111
;
11295 tree110
= TREE_OPERAND (tree11
, 0);
11296 tree111
= TREE_OPERAND (tree11
, 1);
11297 STRIP_NOPS (tree110
);
11298 STRIP_NOPS (tree111
);
11299 if (TREE_CODE (tree110
) == NEGATE_EXPR
11300 && TREE_CODE (tree111
) == INTEGER_CST
11301 && compare_tree_int (tree111
,
11302 element_precision (rtype
) - 1) == 0
11303 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11305 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11306 ? LROTATE_EXPR
: RROTATE_EXPR
),
11307 rtype
, TREE_OPERAND (arg0
, 0),
11309 return fold_convert_loc (loc
, type
, tem
);
11316 /* In most languages, can't associate operations on floats through
11317 parentheses. Rather than remember where the parentheses were, we
11318 don't associate floats at all, unless the user has specified
11319 -fassociative-math.
11320 And, we need to make sure type is not saturating. */
11322 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11323 && !TYPE_SATURATING (type
))
11325 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11326 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11330 /* Split both trees into variables, constants, and literals. Then
11331 associate each group together, the constants with literals,
11332 then the result with variables. This increases the chances of
11333 literals being recombined later and of generating relocatable
11334 expressions for the sum of a constant and literal. */
11335 var0
= split_tree (arg0
, type
, code
,
11336 &minus_var0
, &con0
, &minus_con0
,
11337 &lit0
, &minus_lit0
, 0);
11338 var1
= split_tree (arg1
, type
, code
,
11339 &minus_var1
, &con1
, &minus_con1
,
11340 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11342 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11343 if (code
== MINUS_EXPR
)
11346 /* With undefined overflow prefer doing association in a type
11347 which wraps on overflow, if that is one of the operand types. */
11348 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11349 && !TYPE_OVERFLOW_WRAPS (type
))
11351 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11352 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11353 atype
= TREE_TYPE (arg0
);
11354 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11355 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11356 atype
= TREE_TYPE (arg1
);
11357 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11360 /* With undefined overflow we can only associate constants with one
11361 variable, and constants whose association doesn't overflow. */
11362 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11363 && !TYPE_OVERFLOW_WRAPS (atype
))
11365 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11367 /* ??? If split_tree would handle NEGATE_EXPR we could
11368 simply reject these cases and the allowed cases would
11369 be the var0/minus_var1 ones. */
11370 tree tmp0
= var0
? var0
: minus_var0
;
11371 tree tmp1
= var1
? var1
: minus_var1
;
11372 bool one_neg
= false;
11374 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11376 tmp0
= TREE_OPERAND (tmp0
, 0);
11377 one_neg
= !one_neg
;
11379 if (CONVERT_EXPR_P (tmp0
)
11380 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11381 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11382 <= TYPE_PRECISION (atype
)))
11383 tmp0
= TREE_OPERAND (tmp0
, 0);
11384 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11386 tmp1
= TREE_OPERAND (tmp1
, 0);
11387 one_neg
= !one_neg
;
11389 if (CONVERT_EXPR_P (tmp1
)
11390 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11391 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11392 <= TYPE_PRECISION (atype
)))
11393 tmp1
= TREE_OPERAND (tmp1
, 0);
11394 /* The only case we can still associate with two variables
11395 is if they cancel out. */
11397 || !operand_equal_p (tmp0
, tmp1
, 0))
11400 else if ((var0
&& minus_var1
11401 && ! operand_equal_p (var0
, minus_var1
, 0))
11402 || (minus_var0
&& var1
11403 && ! operand_equal_p (minus_var0
, var1
, 0)))
11407 /* Only do something if we found more than two objects. Otherwise,
11408 nothing has changed and we risk infinite recursion. */
11410 && ((var0
!= 0) + (var1
!= 0)
11411 + (minus_var0
!= 0) + (minus_var1
!= 0)
11412 + (con0
!= 0) + (con1
!= 0)
11413 + (minus_con0
!= 0) + (minus_con1
!= 0)
11414 + (lit0
!= 0) + (lit1
!= 0)
11415 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11417 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11418 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11420 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11421 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11423 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11424 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11427 if (minus_var0
&& var0
)
11429 var0
= associate_trees (loc
, var0
, minus_var0
,
11430 MINUS_EXPR
, atype
);
11433 if (minus_con0
&& con0
)
11435 con0
= associate_trees (loc
, con0
, minus_con0
,
11436 MINUS_EXPR
, atype
);
11440 /* Preserve the MINUS_EXPR if the negative part of the literal is
11441 greater than the positive part. Otherwise, the multiplicative
11442 folding code (i.e extract_muldiv) may be fooled in case
11443 unsigned constants are subtracted, like in the following
11444 example: ((X*2 + 4) - 8U)/2. */
11445 if (minus_lit0
&& lit0
)
11447 if (TREE_CODE (lit0
) == INTEGER_CST
11448 && TREE_CODE (minus_lit0
) == INTEGER_CST
11449 && tree_int_cst_lt (lit0
, minus_lit0
)
11450 /* But avoid ending up with only negated parts. */
11453 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11454 MINUS_EXPR
, atype
);
11459 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11460 MINUS_EXPR
, atype
);
11465 /* Don't introduce overflows through reassociation. */
11466 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11467 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11470 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11471 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11473 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11477 /* Eliminate minus_con0. */
11481 con0
= associate_trees (loc
, con0
, minus_con0
,
11482 MINUS_EXPR
, atype
);
11484 var0
= associate_trees (loc
, var0
, minus_con0
,
11485 MINUS_EXPR
, atype
);
11487 gcc_unreachable ();
11491 /* Eliminate minus_var0. */
11495 con0
= associate_trees (loc
, con0
, minus_var0
,
11496 MINUS_EXPR
, atype
);
11498 gcc_unreachable ();
11503 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11510 case POINTER_DIFF_EXPR
:
11512 /* Fold &a[i] - &a[j] to i-j. */
11513 if (TREE_CODE (arg0
) == ADDR_EXPR
11514 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11515 && TREE_CODE (arg1
) == ADDR_EXPR
11516 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11518 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11519 TREE_OPERAND (arg0
, 0),
11520 TREE_OPERAND (arg1
, 0),
11522 == POINTER_DIFF_EXPR
);
11527 /* Further transformations are not for pointers. */
11528 if (code
== POINTER_DIFF_EXPR
)
11531 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11532 if (TREE_CODE (arg0
) == NEGATE_EXPR
11533 && negate_expr_p (op1
)
11534 /* If arg0 is e.g. unsigned int and type is int, then this could
11535 introduce UB, because if A is INT_MIN at runtime, the original
11536 expression can be well defined while the latter is not.
11538 && !(ANY_INTEGRAL_TYPE_P (type
)
11539 && TYPE_OVERFLOW_UNDEFINED (type
)
11540 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11541 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11542 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11543 fold_convert_loc (loc
, type
,
11544 TREE_OPERAND (arg0
, 0)));
11546 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11547 __complex__ ( x, -y ). This is not the same for SNaNs or if
11548 signed zeros are involved. */
11549 if (!HONOR_SNANS (arg0
)
11550 && !HONOR_SIGNED_ZEROS (arg0
)
11551 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11553 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11554 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11555 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11556 bool arg0rz
= false, arg0iz
= false;
11557 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11558 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11560 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11561 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11562 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11564 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11566 : build1 (REALPART_EXPR
, rtype
, arg1
));
11567 tree ip
= arg0i
? arg0i
11568 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11569 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11571 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11573 tree rp
= arg0r
? arg0r
11574 : build1 (REALPART_EXPR
, rtype
, arg0
);
11575 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11577 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11578 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11583 /* A - B -> A + (-B) if B is easily negatable. */
11584 if (negate_expr_p (op1
)
11585 && ! TYPE_OVERFLOW_SANITIZED (type
)
11586 && ((FLOAT_TYPE_P (type
)
11587 /* Avoid this transformation if B is a positive REAL_CST. */
11588 && (TREE_CODE (op1
) != REAL_CST
11589 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11590 || INTEGRAL_TYPE_P (type
)))
11591 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11592 fold_convert_loc (loc
, type
, arg0
),
11593 negate_expr (op1
));
11595 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11596 one. Make sure the type is not saturating and has the signedness of
11597 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11598 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11599 if ((TREE_CODE (arg0
) == MULT_EXPR
11600 || TREE_CODE (arg1
) == MULT_EXPR
)
11601 && !TYPE_SATURATING (type
)
11602 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11603 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11604 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11606 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11614 if (! FLOAT_TYPE_P (type
))
11616 /* Transform x * -C into -x * C if x is easily negatable. */
11617 if (TREE_CODE (op1
) == INTEGER_CST
11618 && tree_int_cst_sgn (op1
) == -1
11619 && negate_expr_p (op0
)
11620 && negate_expr_p (op1
)
11621 && (tem
= negate_expr (op1
)) != op1
11622 && ! TREE_OVERFLOW (tem
))
11623 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11624 fold_convert_loc (loc
, type
,
11625 negate_expr (op0
)), tem
);
11627 strict_overflow_p
= false;
11628 if (TREE_CODE (arg1
) == INTEGER_CST
11629 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11630 &strict_overflow_p
)) != 0)
11632 if (strict_overflow_p
)
11633 fold_overflow_warning (("assuming signed overflow does not "
11634 "occur when simplifying "
11636 WARN_STRICT_OVERFLOW_MISC
);
11637 return fold_convert_loc (loc
, type
, tem
);
11640 /* Optimize z * conj(z) for integer complex numbers. */
11641 if (TREE_CODE (arg0
) == CONJ_EXPR
11642 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11643 return fold_mult_zconjz (loc
, type
, arg1
);
11644 if (TREE_CODE (arg1
) == CONJ_EXPR
11645 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11646 return fold_mult_zconjz (loc
, type
, arg0
);
11650 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11651 This is not the same for NaNs or if signed zeros are
11653 if (!HONOR_NANS (arg0
)
11654 && !HONOR_SIGNED_ZEROS (arg0
)
11655 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11656 && TREE_CODE (arg1
) == COMPLEX_CST
11657 && real_zerop (TREE_REALPART (arg1
)))
11659 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11660 if (real_onep (TREE_IMAGPART (arg1
)))
11662 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11663 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11665 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11666 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11668 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11669 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11670 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11674 /* Optimize z * conj(z) for floating point complex numbers.
11675 Guarded by flag_unsafe_math_optimizations as non-finite
11676 imaginary components don't produce scalar results. */
11677 if (flag_unsafe_math_optimizations
11678 && TREE_CODE (arg0
) == CONJ_EXPR
11679 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11680 return fold_mult_zconjz (loc
, type
, arg1
);
11681 if (flag_unsafe_math_optimizations
11682 && TREE_CODE (arg1
) == CONJ_EXPR
11683 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11684 return fold_mult_zconjz (loc
, type
, arg0
);
11689 /* Canonicalize (X & C1) | C2. */
11690 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11691 && TREE_CODE (arg1
) == INTEGER_CST
11692 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11694 int width
= TYPE_PRECISION (type
), w
;
11695 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
11696 wide_int c2
= wi::to_wide (arg1
);
11698 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11699 if ((c1
& c2
) == c1
)
11700 return omit_one_operand_loc (loc
, type
, arg1
,
11701 TREE_OPERAND (arg0
, 0));
11703 wide_int msk
= wi::mask (width
, false,
11704 TYPE_PRECISION (TREE_TYPE (arg1
)));
11706 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11707 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
11709 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11710 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11713 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11714 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11715 mode which allows further optimizations. */
11718 wide_int c3
= wi::bit_and_not (c1
, c2
);
11719 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
11721 wide_int mask
= wi::mask (w
, false,
11722 TYPE_PRECISION (type
));
11723 if (((c1
| c2
) & mask
) == mask
11724 && wi::bit_and_not (c1
, mask
) == 0)
11733 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11734 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
11735 wide_int_to_tree (type
, c3
));
11736 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11740 /* See if this can be simplified into a rotate first. If that
11741 is unsuccessful continue in the association code. */
11745 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11746 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11747 && INTEGRAL_TYPE_P (type
)
11748 && integer_onep (TREE_OPERAND (arg0
, 1))
11749 && integer_onep (arg1
))
11750 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11751 build_zero_cst (TREE_TYPE (arg0
)));
11753 /* See if this can be simplified into a rotate first. If that
11754 is unsuccessful continue in the association code. */
11758 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11759 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11760 && INTEGRAL_TYPE_P (type
)
11761 && integer_onep (TREE_OPERAND (arg0
, 1))
11762 && integer_onep (arg1
))
11765 tem
= TREE_OPERAND (arg0
, 0);
11766 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11767 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11769 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11770 build_zero_cst (TREE_TYPE (tem
)));
11772 /* Fold ~X & 1 as (X & 1) == 0. */
11773 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11774 && INTEGRAL_TYPE_P (type
)
11775 && integer_onep (arg1
))
11778 tem
= TREE_OPERAND (arg0
, 0);
11779 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11780 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11782 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11783 build_zero_cst (TREE_TYPE (tem
)));
11785 /* Fold !X & 1 as X == 0. */
11786 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11787 && integer_onep (arg1
))
11789 tem
= TREE_OPERAND (arg0
, 0);
11790 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11791 build_zero_cst (TREE_TYPE (tem
)));
11794 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11795 multiple of 1 << CST. */
11796 if (TREE_CODE (arg1
) == INTEGER_CST
)
11798 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
11799 wide_int ncst1
= -cst1
;
11800 if ((cst1
& ncst1
) == ncst1
11801 && multiple_of_p (type
, arg0
,
11802 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11803 return fold_convert_loc (loc
, type
, arg0
);
11806 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11808 if (TREE_CODE (arg1
) == INTEGER_CST
11809 && TREE_CODE (arg0
) == MULT_EXPR
11810 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11812 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
11814 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
11817 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11819 else if (masked
!= warg1
)
11821 /* Avoid the transform if arg1 is a mask of some
11822 mode which allows further optimizations. */
11823 int pop
= wi::popcount (warg1
);
11824 if (!(pop
>= BITS_PER_UNIT
11826 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
11827 return fold_build2_loc (loc
, code
, type
, op0
,
11828 wide_int_to_tree (type
, masked
));
11832 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11833 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11834 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11836 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11838 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
11841 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11847 /* Don't touch a floating-point divide by zero unless the mode
11848 of the constant can represent infinity. */
11849 if (TREE_CODE (arg1
) == REAL_CST
11850 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
11851 && real_zerop (arg1
))
11854 /* (-A) / (-B) -> A / B */
11855 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
11856 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11857 TREE_OPERAND (arg0
, 0),
11858 negate_expr (arg1
));
11859 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11860 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11861 negate_expr (arg0
),
11862 TREE_OPERAND (arg1
, 0));
11865 case TRUNC_DIV_EXPR
:
11868 case FLOOR_DIV_EXPR
:
11869 /* Simplify A / (B << N) where A and B are positive and B is
11870 a power of 2, to A >> (N + log2(B)). */
11871 strict_overflow_p
= false;
11872 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11873 && (TYPE_UNSIGNED (type
)
11874 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
11876 tree sval
= TREE_OPERAND (arg1
, 0);
11877 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
11879 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
11880 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
11881 wi::exact_log2 (wi::to_wide (sval
)));
11883 if (strict_overflow_p
)
11884 fold_overflow_warning (("assuming signed overflow does not "
11885 "occur when simplifying A / (B << N)"),
11886 WARN_STRICT_OVERFLOW_MISC
);
11888 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
11890 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11891 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
11897 case ROUND_DIV_EXPR
:
11898 case CEIL_DIV_EXPR
:
11899 case EXACT_DIV_EXPR
:
11900 if (integer_zerop (arg1
))
11903 /* Convert -A / -B to A / B when the type is signed and overflow is
11905 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11906 && TREE_CODE (op0
) == NEGATE_EXPR
11907 && negate_expr_p (op1
))
11909 if (ANY_INTEGRAL_TYPE_P (type
))
11910 fold_overflow_warning (("assuming signed overflow does not occur "
11911 "when distributing negation across "
11913 WARN_STRICT_OVERFLOW_MISC
);
11914 return fold_build2_loc (loc
, code
, type
,
11915 fold_convert_loc (loc
, type
,
11916 TREE_OPERAND (arg0
, 0)),
11917 negate_expr (op1
));
11919 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11920 && TREE_CODE (arg1
) == NEGATE_EXPR
11921 && negate_expr_p (op0
))
11923 if (ANY_INTEGRAL_TYPE_P (type
))
11924 fold_overflow_warning (("assuming signed overflow does not occur "
11925 "when distributing negation across "
11927 WARN_STRICT_OVERFLOW_MISC
);
11928 return fold_build2_loc (loc
, code
, type
,
11930 fold_convert_loc (loc
, type
,
11931 TREE_OPERAND (arg1
, 0)));
11934 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11935 operation, EXACT_DIV_EXPR.
11937 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11938 At one time others generated faster code, it's not clear if they do
11939 after the last round to changes to the DIV code in expmed.cc. */
11940 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
11941 && multiple_of_p (type
, arg0
, arg1
))
11942 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
11943 fold_convert (type
, arg0
),
11944 fold_convert (type
, arg1
));
11946 strict_overflow_p
= false;
11947 if (TREE_CODE (arg1
) == INTEGER_CST
11948 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11949 &strict_overflow_p
)) != 0)
11951 if (strict_overflow_p
)
11952 fold_overflow_warning (("assuming signed overflow does not occur "
11953 "when simplifying division"),
11954 WARN_STRICT_OVERFLOW_MISC
);
11955 return fold_convert_loc (loc
, type
, tem
);
11960 case CEIL_MOD_EXPR
:
11961 case FLOOR_MOD_EXPR
:
11962 case ROUND_MOD_EXPR
:
11963 case TRUNC_MOD_EXPR
:
11964 strict_overflow_p
= false;
11965 if (TREE_CODE (arg1
) == INTEGER_CST
11966 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11967 &strict_overflow_p
)) != 0)
11969 if (strict_overflow_p
)
11970 fold_overflow_warning (("assuming signed overflow does not occur "
11971 "when simplifying modulus"),
11972 WARN_STRICT_OVERFLOW_MISC
);
11973 return fold_convert_loc (loc
, type
, tem
);
11982 /* Since negative shift count is not well-defined,
11983 don't try to compute it in the compiler. */
11984 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
11987 prec
= element_precision (type
);
11989 /* If we have a rotate of a bit operation with the rotate count and
11990 the second operand of the bit operation both constant,
11991 permute the two operations. */
11992 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11993 && (TREE_CODE (arg0
) == BIT_AND_EXPR
11994 || TREE_CODE (arg0
) == BIT_IOR_EXPR
11995 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
11996 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11998 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11999 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12000 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
12001 fold_build2_loc (loc
, code
, type
,
12003 fold_build2_loc (loc
, code
, type
,
12007 /* Two consecutive rotates adding up to the some integer
12008 multiple of the precision of the type can be ignored. */
12009 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12010 && TREE_CODE (arg0
) == RROTATE_EXPR
12011 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12012 && wi::umod_trunc (wi::to_wide (arg1
)
12013 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
12015 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12023 case TRUTH_ANDIF_EXPR
:
12024 /* Note that the operands of this must be ints
12025 and their values must be 0 or 1.
12026 ("true" is a fixed value perhaps depending on the language.) */
12027 /* If first arg is constant zero, return it. */
12028 if (integer_zerop (arg0
))
12029 return fold_convert_loc (loc
, type
, arg0
);
12031 case TRUTH_AND_EXPR
:
12032 /* If either arg is constant true, drop it. */
12033 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12034 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12035 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
12036 /* Preserve sequence points. */
12037 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12038 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12039 /* If second arg is constant zero, result is zero, but first arg
12040 must be evaluated. */
12041 if (integer_zerop (arg1
))
12042 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12043 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12044 case will be handled here. */
12045 if (integer_zerop (arg0
))
12046 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12048 /* !X && X is always false. */
12049 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12050 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12051 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12052 /* X && !X is always false. */
12053 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12054 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12055 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12057 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12058 means A >= Y && A != MAX, but in this case we know that
12061 if (!TREE_SIDE_EFFECTS (arg0
)
12062 && !TREE_SIDE_EFFECTS (arg1
))
12064 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12065 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12066 return fold_convert (type
,
12067 fold_build2_loc (loc
, code
, TREE_TYPE (arg1
),
12070 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12071 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12072 return fold_convert (type
,
12073 fold_build2_loc (loc
, code
, TREE_TYPE (arg0
),
12077 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12083 case TRUTH_ORIF_EXPR
:
12084 /* Note that the operands of this must be ints
12085 and their values must be 0 or true.
12086 ("true" is a fixed value perhaps depending on the language.) */
12087 /* If first arg is constant true, return it. */
12088 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12089 return fold_convert_loc (loc
, type
, arg0
);
12091 case TRUTH_OR_EXPR
:
12092 /* If either arg is constant zero, drop it. */
12093 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12094 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12095 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12096 /* Preserve sequence points. */
12097 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12098 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12099 /* If second arg is constant true, result is true, but we must
12100 evaluate first arg. */
12101 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12102 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12103 /* Likewise for first arg, but note this only occurs here for
12105 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12106 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12108 /* !X || X is always true. */
12109 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12110 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12111 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12112 /* X || !X is always true. */
12113 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12114 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12115 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12117 /* (X && !Y) || (!X && Y) is X ^ Y */
12118 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12119 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12121 tree a0
, a1
, l0
, l1
, n0
, n1
;
12123 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12124 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12126 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12127 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12129 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12130 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12132 if ((operand_equal_p (n0
, a0
, 0)
12133 && operand_equal_p (n1
, a1
, 0))
12134 || (operand_equal_p (n0
, a1
, 0)
12135 && operand_equal_p (n1
, a0
, 0)))
12136 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12139 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12145 case TRUTH_XOR_EXPR
:
12146 /* If the second arg is constant zero, drop it. */
12147 if (integer_zerop (arg1
))
12148 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12149 /* If the second arg is constant true, this is a logical inversion. */
12150 if (integer_onep (arg1
))
12152 tem
= invert_truthvalue_loc (loc
, arg0
);
12153 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12155 /* Identical arguments cancel to zero. */
12156 if (operand_equal_p (arg0
, arg1
, 0))
12157 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12159 /* !X ^ X is always true. */
12160 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12161 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12162 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12164 /* X ^ !X is always true. */
12165 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12166 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12167 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12176 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12177 if (tem
!= NULL_TREE
)
12180 /* bool_var != 1 becomes !bool_var. */
12181 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12182 && code
== NE_EXPR
)
12183 return fold_convert_loc (loc
, type
,
12184 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12185 TREE_TYPE (arg0
), arg0
));
12187 /* bool_var == 0 becomes !bool_var. */
12188 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12189 && code
== EQ_EXPR
)
12190 return fold_convert_loc (loc
, type
,
12191 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12192 TREE_TYPE (arg0
), arg0
));
12194 /* !exp != 0 becomes !exp */
12195 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12196 && code
== NE_EXPR
)
12197 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12199 /* If this is an EQ or NE comparison with zero and ARG0 is
12200 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12201 two operations, but the latter can be done in one less insn
12202 on machines that have only two-operand insns or on which a
12203 constant cannot be the first operand. */
12204 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12205 && integer_zerop (arg1
))
12207 tree arg00
= TREE_OPERAND (arg0
, 0);
12208 tree arg01
= TREE_OPERAND (arg0
, 1);
12209 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12210 && integer_onep (TREE_OPERAND (arg00
, 0)))
12212 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12213 arg01
, TREE_OPERAND (arg00
, 1));
12214 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12215 build_one_cst (TREE_TYPE (arg0
)));
12216 return fold_build2_loc (loc
, code
, type
,
12217 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12220 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12221 && integer_onep (TREE_OPERAND (arg01
, 0)))
12223 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12224 arg00
, TREE_OPERAND (arg01
, 1));
12225 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12226 build_one_cst (TREE_TYPE (arg0
)));
12227 return fold_build2_loc (loc
, code
, type
,
12228 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12233 /* If this is a comparison of a field, we may be able to simplify it. */
12234 if ((TREE_CODE (arg0
) == COMPONENT_REF
12235 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12236 /* Handle the constant case even without -O
12237 to make sure the warnings are given. */
12238 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12240 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12245 /* Optimize comparisons of strlen vs zero to a compare of the
12246 first character of the string vs zero. To wit,
12247 strlen(ptr) == 0 => *ptr == 0
12248 strlen(ptr) != 0 => *ptr != 0
12249 Other cases should reduce to one of these two (or a constant)
12250 due to the return value of strlen being unsigned. */
12251 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12253 tree fndecl
= get_callee_fndecl (arg0
);
12256 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12257 && call_expr_nargs (arg0
) == 1
12258 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12262 = build_pointer_type (build_qualified_type (char_type_node
,
12264 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12265 CALL_EXPR_ARG (arg0
, 0));
12266 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12267 return fold_build2_loc (loc
, code
, type
, iref
,
12268 build_int_cst (TREE_TYPE (iref
), 0));
12272 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12273 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12274 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12275 && integer_zerop (arg1
)
12276 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12278 tree arg00
= TREE_OPERAND (arg0
, 0);
12279 tree arg01
= TREE_OPERAND (arg0
, 1);
12280 tree itype
= TREE_TYPE (arg00
);
12281 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12283 if (TYPE_UNSIGNED (itype
))
12285 itype
= signed_type_for (itype
);
12286 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12288 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12289 type
, arg00
, build_zero_cst (itype
));
12293 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12294 (X & C) == 0 when C is a single bit. */
12295 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12296 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12297 && integer_zerop (arg1
)
12298 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12300 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12301 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12302 TREE_OPERAND (arg0
, 1));
12303 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12305 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12309 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12310 constant C is a power of two, i.e. a single bit. */
12311 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12312 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12313 && integer_zerop (arg1
)
12314 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12315 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12316 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12318 tree arg00
= TREE_OPERAND (arg0
, 0);
12319 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12320 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12323 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12324 when is C is a power of two, i.e. a single bit. */
12325 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12326 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12327 && integer_zerop (arg1
)
12328 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12329 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12330 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12332 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12333 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12334 arg000
, TREE_OPERAND (arg0
, 1));
12335 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12336 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12339 if (integer_zerop (arg1
)
12340 && tree_expr_nonzero_p (arg0
))
12342 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
12343 return omit_one_operand_loc (loc
, type
, res
, arg0
);
12346 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12347 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12349 tree arg00
= TREE_OPERAND (arg0
, 0);
12350 tree arg01
= TREE_OPERAND (arg0
, 1);
12351 tree arg10
= TREE_OPERAND (arg1
, 0);
12352 tree arg11
= TREE_OPERAND (arg1
, 1);
12353 tree itype
= TREE_TYPE (arg0
);
12355 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12356 operand_equal_p guarantees no side-effects so we don't need
12357 to use omit_one_operand on Z. */
12358 if (operand_equal_p (arg01
, arg11
, 0))
12359 return fold_build2_loc (loc
, code
, type
, arg00
,
12360 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12362 if (operand_equal_p (arg01
, arg10
, 0))
12363 return fold_build2_loc (loc
, code
, type
, arg00
,
12364 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12366 if (operand_equal_p (arg00
, arg11
, 0))
12367 return fold_build2_loc (loc
, code
, type
, arg01
,
12368 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12370 if (operand_equal_p (arg00
, arg10
, 0))
12371 return fold_build2_loc (loc
, code
, type
, arg01
,
12372 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12375 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12376 if (TREE_CODE (arg01
) == INTEGER_CST
12377 && TREE_CODE (arg11
) == INTEGER_CST
)
12379 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12380 fold_convert_loc (loc
, itype
, arg11
));
12381 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12382 return fold_build2_loc (loc
, code
, type
, tem
,
12383 fold_convert_loc (loc
, itype
, arg10
));
12387 /* Attempt to simplify equality/inequality comparisons of complex
12388 values. Only lower the comparison if the result is known or
12389 can be simplified to a single scalar comparison. */
12390 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12391 || TREE_CODE (arg0
) == COMPLEX_CST
)
12392 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12393 || TREE_CODE (arg1
) == COMPLEX_CST
))
12395 tree real0
, imag0
, real1
, imag1
;
12398 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12400 real0
= TREE_OPERAND (arg0
, 0);
12401 imag0
= TREE_OPERAND (arg0
, 1);
12405 real0
= TREE_REALPART (arg0
);
12406 imag0
= TREE_IMAGPART (arg0
);
12409 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12411 real1
= TREE_OPERAND (arg1
, 0);
12412 imag1
= TREE_OPERAND (arg1
, 1);
12416 real1
= TREE_REALPART (arg1
);
12417 imag1
= TREE_IMAGPART (arg1
);
12420 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12421 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12423 if (integer_zerop (rcond
))
12425 if (code
== EQ_EXPR
)
12426 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12428 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12432 if (code
== NE_EXPR
)
12433 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12435 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12439 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12440 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12442 if (integer_zerop (icond
))
12444 if (code
== EQ_EXPR
)
12445 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12447 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12451 if (code
== NE_EXPR
)
12452 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12454 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12465 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12466 if (tem
!= NULL_TREE
)
12469 /* Transform comparisons of the form X +- C CMP X. */
12470 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12471 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12472 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12473 && !HONOR_SNANS (arg0
))
12475 tree arg01
= TREE_OPERAND (arg0
, 1);
12476 enum tree_code code0
= TREE_CODE (arg0
);
12477 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12479 /* (X - c) > X becomes false. */
12480 if (code
== GT_EXPR
12481 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12482 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12483 return constant_boolean_node (0, type
);
12485 /* Likewise (X + c) < X becomes false. */
12486 if (code
== LT_EXPR
12487 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12488 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12489 return constant_boolean_node (0, type
);
12491 /* Convert (X - c) <= X to true. */
12492 if (!HONOR_NANS (arg1
)
12494 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12495 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12496 return constant_boolean_node (1, type
);
12498 /* Convert (X + c) >= X to true. */
12499 if (!HONOR_NANS (arg1
)
12501 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12502 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12503 return constant_boolean_node (1, type
);
12506 /* If we are comparing an ABS_EXPR with a constant, we can
12507 convert all the cases into explicit comparisons, but they may
12508 well not be faster than doing the ABS and one comparison.
12509 But ABS (X) <= C is a range comparison, which becomes a subtraction
12510 and a comparison, and is probably faster. */
12511 if (code
== LE_EXPR
12512 && TREE_CODE (arg1
) == INTEGER_CST
12513 && TREE_CODE (arg0
) == ABS_EXPR
12514 && ! TREE_SIDE_EFFECTS (arg0
)
12515 && (tem
= negate_expr (arg1
)) != 0
12516 && TREE_CODE (tem
) == INTEGER_CST
12517 && !TREE_OVERFLOW (tem
))
12518 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12519 build2 (GE_EXPR
, type
,
12520 TREE_OPERAND (arg0
, 0), tem
),
12521 build2 (LE_EXPR
, type
,
12522 TREE_OPERAND (arg0
, 0), arg1
));
12524 /* Convert ABS_EXPR<x> >= 0 to true. */
12525 strict_overflow_p
= false;
12526 if (code
== GE_EXPR
12527 && (integer_zerop (arg1
)
12528 || (! HONOR_NANS (arg0
)
12529 && real_zerop (arg1
)))
12530 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12532 if (strict_overflow_p
)
12533 fold_overflow_warning (("assuming signed overflow does not occur "
12534 "when simplifying comparison of "
12535 "absolute value and zero"),
12536 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12537 return omit_one_operand_loc (loc
, type
,
12538 constant_boolean_node (true, type
),
12542 /* Convert ABS_EXPR<x> < 0 to false. */
12543 strict_overflow_p
= false;
12544 if (code
== LT_EXPR
12545 && (integer_zerop (arg1
) || real_zerop (arg1
))
12546 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12548 if (strict_overflow_p
)
12549 fold_overflow_warning (("assuming signed overflow does not occur "
12550 "when simplifying comparison of "
12551 "absolute value and zero"),
12552 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12553 return omit_one_operand_loc (loc
, type
,
12554 constant_boolean_node (false, type
),
12558 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12559 and similarly for >= into !=. */
12560 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12561 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12562 && TREE_CODE (arg1
) == LSHIFT_EXPR
12563 && integer_onep (TREE_OPERAND (arg1
, 0)))
12564 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12565 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12566 TREE_OPERAND (arg1
, 1)),
12567 build_zero_cst (TREE_TYPE (arg0
)));
12569 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12570 otherwise Y might be >= # of bits in X's type and thus e.g.
12571 (unsigned char) (1 << Y) for Y 15 might be 0.
12572 If the cast is widening, then 1 << Y should have unsigned type,
12573 otherwise if Y is number of bits in the signed shift type minus 1,
12574 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12575 31 might be 0xffffffff80000000. */
12576 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12577 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12578 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0
)))
12579 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12580 && CONVERT_EXPR_P (arg1
)
12581 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12582 && (element_precision (TREE_TYPE (arg1
))
12583 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12584 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12585 || (element_precision (TREE_TYPE (arg1
))
12586 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12587 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12589 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12590 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12591 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12592 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12593 build_zero_cst (TREE_TYPE (arg0
)));
12598 case UNORDERED_EXPR
:
12606 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12608 tree targ0
= strip_float_extensions (arg0
);
12609 tree targ1
= strip_float_extensions (arg1
);
12610 tree newtype
= TREE_TYPE (targ0
);
12612 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
12613 newtype
= TREE_TYPE (targ1
);
12615 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
12616 return fold_build2_loc (loc
, code
, type
,
12617 fold_convert_loc (loc
, newtype
, targ0
),
12618 fold_convert_loc (loc
, newtype
, targ1
));
12623 case COMPOUND_EXPR
:
12624 /* When pedantic, a compound expression can be neither an lvalue
12625 nor an integer constant expression. */
12626 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12628 /* Don't let (0, 0) be null pointer constant. */
12629 tem
= integer_zerop (arg1
) ? build1_loc (loc
, NOP_EXPR
, type
, arg1
)
12630 : fold_convert_loc (loc
, type
, arg1
);
12635 } /* switch (code) */
12638 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12639 ((A & N) + B) & M -> (A + B) & M
12640 Similarly if (N & M) == 0,
12641 ((A | N) + B) & M -> (A + B) & M
12642 and for - instead of + (or unary - instead of +)
12643 and/or ^ instead of |.
12644 If B is constant and (B & M) == 0, fold into A & M.
12646 This function is a helper for match.pd patterns. Return non-NULL
12647 type in which the simplified operation should be performed only
12648 if any optimization is possible.
12650 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12651 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12652 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12655 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12656 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12657 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12660 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12661 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12662 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12664 || (cst1
& (cst1
+ 1)) != 0
12665 || !INTEGRAL_TYPE_P (type
)
12666 || (!TYPE_OVERFLOW_WRAPS (type
)
12667 && TREE_CODE (type
) != INTEGER_TYPE
)
12668 || (wi::max_value (type
) & cst1
) != cst1
)
12671 enum tree_code codes
[2] = { code00
, code01
};
12672 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12676 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12677 arg1 (M) is == (1LL << cst) - 1.
12678 Store C into PMOP[0] and D into PMOP[1]. */
12681 which
= code
!= NEGATE_EXPR
;
12683 for (; which
>= 0; which
--)
12684 switch (codes
[which
])
12689 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12690 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12691 if (codes
[which
] == BIT_AND_EXPR
)
12696 else if (cst0
!= 0)
12698 /* If C or D is of the form (A & N) where
12699 (N & M) == M, or of the form (A | N) or
12700 (A ^ N) where (N & M) == 0, replace it with A. */
12701 pmop
[which
] = arg0xx
[2 * which
];
12704 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
12706 /* If C or D is a N where (N & M) == 0, it can be
12707 omitted (replaced with 0). */
12708 if ((code
== PLUS_EXPR
12709 || (code
== MINUS_EXPR
&& which
== 0))
12710 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
12711 pmop
[which
] = build_int_cst (type
, 0);
12712 /* Similarly, with C - N where (-N & M) == 0. */
12713 if (code
== MINUS_EXPR
12715 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
12716 pmop
[which
] = build_int_cst (type
, 0);
12719 gcc_unreachable ();
12722 /* Only build anything new if we optimized one or both arguments above. */
12723 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
12726 if (TYPE_OVERFLOW_WRAPS (type
))
12729 return unsigned_type_for (type
);
12732 /* Used by contains_label_[p1]. */
12734 struct contains_label_data
12736 hash_set
<tree
> *pset
;
12737 bool inside_switch_p
;
12740 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12741 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12742 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12745 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
12747 contains_label_data
*d
= (contains_label_data
*) data
;
12748 switch (TREE_CODE (*tp
))
12753 case CASE_LABEL_EXPR
:
12754 if (!d
->inside_switch_p
)
12759 if (!d
->inside_switch_p
)
12761 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
12763 d
->inside_switch_p
= true;
12764 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
12766 d
->inside_switch_p
= false;
12767 *walk_subtrees
= 0;
12772 *walk_subtrees
= 0;
12780 /* Return whether the sub-tree ST contains a label which is accessible from
12781 outside the sub-tree. */
12784 contains_label_p (tree st
)
12786 hash_set
<tree
> pset
;
12787 contains_label_data data
= { &pset
, false };
12788 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
12791 /* Fold a ternary expression of code CODE and type TYPE with operands
12792 OP0, OP1, and OP2. Return the folded expression if folding is
12793 successful. Otherwise, return NULL_TREE. */
12796 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
12797 tree op0
, tree op1
, tree op2
)
12800 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
12801 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
12803 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
12804 && TREE_CODE_LENGTH (code
) == 3);
12806 /* If this is a commutative operation, and OP0 is a constant, move it
12807 to OP1 to reduce the number of tests below. */
12808 if (commutative_ternary_tree_code (code
)
12809 && tree_swap_operands_p (op0
, op1
))
12810 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
12812 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
12816 /* Strip any conversions that don't change the mode. This is safe
12817 for every expression, except for a comparison expression because
12818 its signedness is derived from its operands. So, in the latter
12819 case, only strip conversions that don't change the signedness.
12821 Note that this is done as an internal manipulation within the
12822 constant folder, in order to find the simplest representation of
12823 the arguments so that their form can be studied. In any cases,
12824 the appropriate type conversions should be put back in the tree
12825 that will get out of the constant folder. */
12846 case COMPONENT_REF
:
12847 if (TREE_CODE (arg0
) == CONSTRUCTOR
12848 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
12850 unsigned HOST_WIDE_INT idx
;
12852 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
12859 case VEC_COND_EXPR
:
12860 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12861 so all simple results must be passed through pedantic_non_lvalue. */
12862 if (TREE_CODE (arg0
) == INTEGER_CST
)
12864 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
12865 tem
= integer_zerop (arg0
) ? op2
: op1
;
12866 /* Only optimize constant conditions when the selected branch
12867 has the same type as the COND_EXPR. This avoids optimizing
12868 away "c ? x : throw", where the throw has a void type.
12869 Avoid throwing away that operand which contains label. */
12870 if ((!TREE_SIDE_EFFECTS (unused_op
)
12871 || !contains_label_p (unused_op
))
12872 && (! VOID_TYPE_P (TREE_TYPE (tem
))
12873 || VOID_TYPE_P (type
)))
12874 return protected_set_expr_location_unshare (tem
, loc
);
12877 else if (TREE_CODE (arg0
) == VECTOR_CST
)
12879 unsigned HOST_WIDE_INT nelts
;
12880 if ((TREE_CODE (arg1
) == VECTOR_CST
12881 || TREE_CODE (arg1
) == CONSTRUCTOR
)
12882 && (TREE_CODE (arg2
) == VECTOR_CST
12883 || TREE_CODE (arg2
) == CONSTRUCTOR
)
12884 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
12886 vec_perm_builder
sel (nelts
, nelts
, 1);
12887 for (unsigned int i
= 0; i
< nelts
; i
++)
12889 tree val
= VECTOR_CST_ELT (arg0
, i
);
12890 if (integer_all_onesp (val
))
12891 sel
.quick_push (i
);
12892 else if (integer_zerop (val
))
12893 sel
.quick_push (nelts
+ i
);
12894 else /* Currently unreachable. */
12897 vec_perm_indices
indices (sel
, 2, nelts
);
12898 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
12899 if (t
!= NULL_TREE
)
12904 /* If we have A op B ? A : C, we may be able to convert this to a
12905 simpler expression, depending on the operation and the values
12906 of B and C. Signed zeros prevent all of these transformations,
12907 for reasons given above each one.
12909 Also try swapping the arguments and inverting the conditional. */
12910 if (COMPARISON_CLASS_P (arg0
)
12911 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
12912 && !HONOR_SIGNED_ZEROS (op1
))
12914 tem
= fold_cond_expr_with_comparison (loc
, type
, TREE_CODE (arg0
),
12915 TREE_OPERAND (arg0
, 0),
12916 TREE_OPERAND (arg0
, 1),
12922 if (COMPARISON_CLASS_P (arg0
)
12923 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
12924 && !HONOR_SIGNED_ZEROS (op2
))
12926 enum tree_code comp_code
= TREE_CODE (arg0
);
12927 tree arg00
= TREE_OPERAND (arg0
, 0);
12928 tree arg01
= TREE_OPERAND (arg0
, 1);
12929 comp_code
= invert_tree_comparison (comp_code
, HONOR_NANS (arg00
));
12930 if (comp_code
!= ERROR_MARK
)
12931 tem
= fold_cond_expr_with_comparison (loc
, type
, comp_code
,
12939 /* If the second operand is simpler than the third, swap them
12940 since that produces better jump optimization results. */
12941 if (truth_value_p (TREE_CODE (arg0
))
12942 && tree_swap_operands_p (op1
, op2
))
12944 location_t loc0
= expr_location_or (arg0
, loc
);
12945 /* See if this can be inverted. If it can't, possibly because
12946 it was a floating-point inequality comparison, don't do
12948 tem
= fold_invert_truthvalue (loc0
, arg0
);
12950 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
12953 /* Convert A ? 1 : 0 to simply A. */
12954 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
12955 : (integer_onep (op1
)
12956 && !VECTOR_TYPE_P (type
)))
12957 && integer_zerop (op2
)
12958 /* If we try to convert OP0 to our type, the
12959 call to fold will try to move the conversion inside
12960 a COND, which will recurse. In that case, the COND_EXPR
12961 is probably the best choice, so leave it alone. */
12962 && type
== TREE_TYPE (arg0
))
12963 return protected_set_expr_location_unshare (arg0
, loc
);
12965 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12966 over COND_EXPR in cases such as floating point comparisons. */
12967 if (integer_zerop (op1
)
12968 && code
== COND_EXPR
12969 && integer_onep (op2
)
12970 && !VECTOR_TYPE_P (type
)
12971 && truth_value_p (TREE_CODE (arg0
)))
12972 return fold_convert_loc (loc
, type
,
12973 invert_truthvalue_loc (loc
, arg0
));
12975 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12976 if (TREE_CODE (arg0
) == LT_EXPR
12977 && integer_zerop (TREE_OPERAND (arg0
, 1))
12978 && integer_zerop (op2
)
12979 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
12981 /* sign_bit_p looks through both zero and sign extensions,
12982 but for this optimization only sign extensions are
12984 tree tem2
= TREE_OPERAND (arg0
, 0);
12985 while (tem
!= tem2
)
12987 if (TREE_CODE (tem2
) != NOP_EXPR
12988 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
12993 tem2
= TREE_OPERAND (tem2
, 0);
12995 /* sign_bit_p only checks ARG1 bits within A's precision.
12996 If <sign bit of A> has wider type than A, bits outside
12997 of A's precision in <sign bit of A> need to be checked.
12998 If they are all 0, this optimization needs to be done
12999 in unsigned A's type, if they are all 1 in signed A's type,
13000 otherwise this can't be done. */
13002 && TYPE_PRECISION (TREE_TYPE (tem
))
13003 < TYPE_PRECISION (TREE_TYPE (arg1
))
13004 && TYPE_PRECISION (TREE_TYPE (tem
))
13005 < TYPE_PRECISION (type
))
13007 int inner_width
, outer_width
;
13010 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
13011 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
13012 if (outer_width
> TYPE_PRECISION (type
))
13013 outer_width
= TYPE_PRECISION (type
);
13015 wide_int mask
= wi::shifted_mask
13016 (inner_width
, outer_width
- inner_width
, false,
13017 TYPE_PRECISION (TREE_TYPE (arg1
)));
13019 wide_int common
= mask
& wi::to_wide (arg1
);
13020 if (common
== mask
)
13022 tem_type
= signed_type_for (TREE_TYPE (tem
));
13023 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13025 else if (common
== 0)
13027 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
13028 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13036 fold_convert_loc (loc
, type
,
13037 fold_build2_loc (loc
, BIT_AND_EXPR
,
13038 TREE_TYPE (tem
), tem
,
13039 fold_convert_loc (loc
,
13044 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13045 already handled above. */
13046 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13047 && integer_onep (TREE_OPERAND (arg0
, 1))
13048 && integer_zerop (op2
)
13049 && integer_pow2p (arg1
))
13051 tree tem
= TREE_OPERAND (arg0
, 0);
13053 if (TREE_CODE (tem
) == RSHIFT_EXPR
13054 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
13055 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
13056 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
13057 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
13058 fold_convert_loc (loc
, type
,
13059 TREE_OPERAND (tem
, 0)),
13063 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13064 is probably obsolete because the first operand should be a
13065 truth value (that's why we have the two cases above), but let's
13066 leave it in until we can confirm this for all front-ends. */
13067 if (integer_zerop (op2
)
13068 && TREE_CODE (arg0
) == NE_EXPR
13069 && integer_zerop (TREE_OPERAND (arg0
, 1))
13070 && integer_pow2p (arg1
)
13071 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13072 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13073 arg1
, OEP_ONLY_CONST
)
13074 /* operand_equal_p compares just value, not precision, so e.g.
13075 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13076 second operand 32-bit -128, which is not a power of two (or vice
13078 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
13079 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
13081 /* Disable the transformations below for vectors, since
13082 fold_binary_op_with_conditional_arg may undo them immediately,
13083 yielding an infinite loop. */
13084 if (code
== VEC_COND_EXPR
)
13087 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13088 if (integer_zerop (op2
)
13089 && truth_value_p (TREE_CODE (arg0
))
13090 && truth_value_p (TREE_CODE (arg1
))
13091 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13092 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13093 : TRUTH_ANDIF_EXPR
,
13094 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13096 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13097 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13098 && truth_value_p (TREE_CODE (arg0
))
13099 && truth_value_p (TREE_CODE (arg1
))
13100 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13102 location_t loc0
= expr_location_or (arg0
, loc
);
13103 /* Only perform transformation if ARG0 is easily inverted. */
13104 tem
= fold_invert_truthvalue (loc0
, arg0
);
13106 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13109 type
, fold_convert_loc (loc
, type
, tem
),
13113 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13114 if (integer_zerop (arg1
)
13115 && truth_value_p (TREE_CODE (arg0
))
13116 && truth_value_p (TREE_CODE (op2
))
13117 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13119 location_t loc0
= expr_location_or (arg0
, loc
);
13120 /* Only perform transformation if ARG0 is easily inverted. */
13121 tem
= fold_invert_truthvalue (loc0
, arg0
);
13123 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13124 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13125 type
, fold_convert_loc (loc
, type
, tem
),
13129 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13130 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13131 && truth_value_p (TREE_CODE (arg0
))
13132 && truth_value_p (TREE_CODE (op2
))
13133 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13134 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13135 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13136 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13141 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13142 of fold_ternary on them. */
13143 gcc_unreachable ();
13145 case BIT_FIELD_REF
:
13146 if (TREE_CODE (arg0
) == VECTOR_CST
13147 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13148 || (VECTOR_TYPE_P (type
)
13149 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13150 && tree_fits_uhwi_p (op1
)
13151 && tree_fits_uhwi_p (op2
))
13153 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13154 unsigned HOST_WIDE_INT width
13155 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13156 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13157 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13158 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13161 && (idx
% width
) == 0
13162 && (n
% width
) == 0
13163 && known_le ((idx
+ n
) / width
,
13164 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13169 if (TREE_CODE (arg0
) == VECTOR_CST
)
13173 tem
= VECTOR_CST_ELT (arg0
, idx
);
13174 if (VECTOR_TYPE_P (type
))
13175 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13179 tree_vector_builder
vals (type
, n
, 1);
13180 for (unsigned i
= 0; i
< n
; ++i
)
13181 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13182 return vals
.build ();
13187 /* On constants we can use native encode/interpret to constant
13188 fold (nearly) all BIT_FIELD_REFs. */
13189 if (CONSTANT_CLASS_P (arg0
)
13190 && can_native_interpret_type_p (type
)
13191 && BITS_PER_UNIT
== 8
13192 && tree_fits_uhwi_p (op1
)
13193 && tree_fits_uhwi_p (op2
))
13195 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13196 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13197 /* Limit us to a reasonable amount of work. To relax the
13198 other limitations we need bit-shifting of the buffer
13199 and rounding up the size. */
13200 if (bitpos
% BITS_PER_UNIT
== 0
13201 && bitsize
% BITS_PER_UNIT
== 0
13202 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13204 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13205 unsigned HOST_WIDE_INT len
13206 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13207 bitpos
/ BITS_PER_UNIT
);
13209 && len
* BITS_PER_UNIT
>= bitsize
)
13211 tree v
= native_interpret_expr (type
, b
,
13212 bitsize
/ BITS_PER_UNIT
);
13221 case VEC_PERM_EXPR
:
13222 /* Perform constant folding of BIT_INSERT_EXPR. */
13223 if (TREE_CODE (arg2
) == VECTOR_CST
13224 && TREE_CODE (op0
) == VECTOR_CST
13225 && TREE_CODE (op1
) == VECTOR_CST
)
13227 /* Build a vector of integers from the tree mask. */
13228 vec_perm_builder builder
;
13229 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13232 /* Create a vec_perm_indices for the integer vector. */
13233 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13234 bool single_arg
= (op0
== op1
);
13235 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13236 return fold_vec_perm (type
, op0
, op1
, sel
);
13240 case BIT_INSERT_EXPR
:
13241 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13242 if (TREE_CODE (arg0
) == INTEGER_CST
13243 && TREE_CODE (arg1
) == INTEGER_CST
)
13245 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13246 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13247 wide_int tem
= (wi::to_wide (arg0
)
13248 & wi::shifted_mask (bitpos
, bitsize
, true,
13249 TYPE_PRECISION (type
)));
13251 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13253 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13255 else if (TREE_CODE (arg0
) == VECTOR_CST
13256 && CONSTANT_CLASS_P (arg1
)
13257 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13260 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13261 unsigned HOST_WIDE_INT elsize
13262 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13263 if (bitpos
% elsize
== 0)
13265 unsigned k
= bitpos
/ elsize
;
13266 unsigned HOST_WIDE_INT nelts
;
13267 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13269 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13271 tree_vector_builder
elts (type
, nelts
, 1);
13272 elts
.quick_grow (nelts
);
13273 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13274 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13275 return elts
.build ();
13283 } /* switch (code) */
13286 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13287 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13288 constructor element index of the value returned. If the element is
13289 not found NULL_TREE is returned and *CTOR_IDX is updated to
13290 the index of the element after the ACCESS_INDEX position (which
13291 may be outside of the CTOR array). */
13294 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13295 unsigned *ctor_idx
)
13297 tree index_type
= NULL_TREE
;
13298 signop index_sgn
= UNSIGNED
;
13299 offset_int low_bound
= 0;
13301 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13303 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13304 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13306 /* Static constructors for variably sized objects makes no sense. */
13307 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13308 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13309 /* ??? When it is obvious that the range is signed, treat it so. */
13310 if (TYPE_UNSIGNED (index_type
)
13311 && TYPE_MAX_VALUE (domain_type
)
13312 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13313 TYPE_MIN_VALUE (domain_type
)))
13315 index_sgn
= SIGNED
;
13317 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13322 index_sgn
= TYPE_SIGN (index_type
);
13323 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13329 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13332 offset_int index
= low_bound
;
13334 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13336 offset_int max_index
= index
;
13339 bool first_p
= true;
13341 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13343 /* Array constructor might explicitly set index, or specify a range,
13344 or leave index NULL meaning that it is next index after previous
13348 if (TREE_CODE (cfield
) == INTEGER_CST
)
13350 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13353 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13354 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13357 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13359 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13364 index
= max_index
+ 1;
13366 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13367 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13373 /* Do we have match? */
13374 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13376 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13383 else if (in_gimple_form
)
13384 /* We're past the element we search for. Note during parsing
13385 the elements might not be sorted.
13386 ??? We should use a binary search and a flag on the
13387 CONSTRUCTOR as to whether elements are sorted in declaration
13396 /* Perform constant folding and related simplification of EXPR.
13397 The related simplifications include x*1 => x, x*0 => 0, etc.,
13398 and application of the associative law.
13399 NOP_EXPR conversions may be removed freely (as long as we
13400 are careful not to change the type of the overall expression).
13401 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13402 but we can constant-fold them if they have constant operands. */
13404 #ifdef ENABLE_FOLD_CHECKING
13405 # define fold(x) fold_1 (x)
13406 static tree
fold_1 (tree
);
13412 const tree t
= expr
;
13413 enum tree_code code
= TREE_CODE (t
);
13414 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13416 location_t loc
= EXPR_LOCATION (expr
);
13418 /* Return right away if a constant. */
13419 if (kind
== tcc_constant
)
13422 /* CALL_EXPR-like objects with variable numbers of operands are
13423 treated specially. */
13424 if (kind
== tcc_vl_exp
)
13426 if (code
== CALL_EXPR
)
13428 tem
= fold_call_expr (loc
, expr
, false);
13429 return tem
? tem
: expr
;
13434 if (IS_EXPR_CODE_CLASS (kind
))
13436 tree type
= TREE_TYPE (t
);
13437 tree op0
, op1
, op2
;
13439 switch (TREE_CODE_LENGTH (code
))
13442 op0
= TREE_OPERAND (t
, 0);
13443 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13444 return tem
? tem
: expr
;
13446 op0
= TREE_OPERAND (t
, 0);
13447 op1
= TREE_OPERAND (t
, 1);
13448 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13449 return tem
? tem
: expr
;
13451 op0
= TREE_OPERAND (t
, 0);
13452 op1
= TREE_OPERAND (t
, 1);
13453 op2
= TREE_OPERAND (t
, 2);
13454 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13455 return tem
? tem
: expr
;
13465 tree op0
= TREE_OPERAND (t
, 0);
13466 tree op1
= TREE_OPERAND (t
, 1);
13468 if (TREE_CODE (op1
) == INTEGER_CST
13469 && TREE_CODE (op0
) == CONSTRUCTOR
13470 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13472 tree val
= get_array_ctor_element_at_index (op0
,
13473 wi::to_offset (op1
));
13481 /* Return a VECTOR_CST if possible. */
13484 tree type
= TREE_TYPE (t
);
13485 if (TREE_CODE (type
) != VECTOR_TYPE
)
13490 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13491 if (! CONSTANT_CLASS_P (val
))
13494 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13498 return fold (DECL_INITIAL (t
));
13502 } /* switch (code) */
13505 #ifdef ENABLE_FOLD_CHECKING
13508 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13509 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13510 static void fold_check_failed (const_tree
, const_tree
);
13511 void print_fold_checksum (const_tree
);
13513 /* When --enable-checking=fold, compute a digest of expr before
13514 and after actual fold call to see if fold did not accidentally
13515 change original expr. */
13521 struct md5_ctx ctx
;
13522 unsigned char checksum_before
[16], checksum_after
[16];
13523 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13525 md5_init_ctx (&ctx
);
13526 fold_checksum_tree (expr
, &ctx
, &ht
);
13527 md5_finish_ctx (&ctx
, checksum_before
);
13530 ret
= fold_1 (expr
);
13532 md5_init_ctx (&ctx
);
13533 fold_checksum_tree (expr
, &ctx
, &ht
);
13534 md5_finish_ctx (&ctx
, checksum_after
);
13536 if (memcmp (checksum_before
, checksum_after
, 16))
13537 fold_check_failed (expr
, ret
);
13543 print_fold_checksum (const_tree expr
)
13545 struct md5_ctx ctx
;
13546 unsigned char checksum
[16], cnt
;
13547 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13549 md5_init_ctx (&ctx
);
13550 fold_checksum_tree (expr
, &ctx
, &ht
);
13551 md5_finish_ctx (&ctx
, checksum
);
13552 for (cnt
= 0; cnt
< 16; ++cnt
)
13553 fprintf (stderr
, "%02x", checksum
[cnt
]);
13554 putc ('\n', stderr
);
13558 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13560 internal_error ("fold check: original tree changed by fold");
13564 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13565 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13567 const tree_node
**slot
;
13568 enum tree_code code
;
13569 union tree_node
*buf
;
13575 slot
= ht
->find_slot (expr
, INSERT
);
13579 code
= TREE_CODE (expr
);
13580 if (TREE_CODE_CLASS (code
) == tcc_declaration
13581 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13583 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13584 size_t sz
= tree_size (expr
);
13585 buf
= XALLOCAVAR (union tree_node
, sz
);
13586 memcpy ((char *) buf
, expr
, sz
);
13587 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13588 buf
->decl_with_vis
.symtab_node
= NULL
;
13589 buf
->base
.nowarning_flag
= 0;
13592 else if (TREE_CODE_CLASS (code
) == tcc_type
13593 && (TYPE_POINTER_TO (expr
)
13594 || TYPE_REFERENCE_TO (expr
)
13595 || TYPE_CACHED_VALUES_P (expr
)
13596 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13597 || TYPE_NEXT_VARIANT (expr
)
13598 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13600 /* Allow these fields to be modified. */
13602 size_t sz
= tree_size (expr
);
13603 buf
= XALLOCAVAR (union tree_node
, sz
);
13604 memcpy ((char *) buf
, expr
, sz
);
13605 expr
= tmp
= (tree
) buf
;
13606 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13607 TYPE_POINTER_TO (tmp
) = NULL
;
13608 TYPE_REFERENCE_TO (tmp
) = NULL
;
13609 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13610 TYPE_ALIAS_SET (tmp
) = -1;
13611 if (TYPE_CACHED_VALUES_P (tmp
))
13613 TYPE_CACHED_VALUES_P (tmp
) = 0;
13614 TYPE_CACHED_VALUES (tmp
) = NULL
;
13617 else if (warning_suppressed_p (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13619 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13620 that and change builtins.cc etc. instead - see PR89543. */
13621 size_t sz
= tree_size (expr
);
13622 buf
= XALLOCAVAR (union tree_node
, sz
);
13623 memcpy ((char *) buf
, expr
, sz
);
13624 buf
->base
.nowarning_flag
= 0;
13627 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13628 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13629 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13630 if (TREE_CODE_CLASS (code
) != tcc_type
13631 && TREE_CODE_CLASS (code
) != tcc_declaration
13632 && code
!= TREE_LIST
13633 && code
!= SSA_NAME
13634 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13635 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13636 switch (TREE_CODE_CLASS (code
))
13642 md5_process_bytes (TREE_STRING_POINTER (expr
),
13643 TREE_STRING_LENGTH (expr
), ctx
);
13646 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13647 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13650 len
= vector_cst_encoded_nelts (expr
);
13651 for (i
= 0; i
< len
; ++i
)
13652 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13658 case tcc_exceptional
:
13662 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13663 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13664 expr
= TREE_CHAIN (expr
);
13665 goto recursive_label
;
13668 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13669 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13675 case tcc_expression
:
13676 case tcc_reference
:
13677 case tcc_comparison
:
13680 case tcc_statement
:
13682 len
= TREE_OPERAND_LENGTH (expr
);
13683 for (i
= 0; i
< len
; ++i
)
13684 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13686 case tcc_declaration
:
13687 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13688 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13689 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13691 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13692 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13693 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13694 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13695 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13698 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13700 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13702 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13703 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13705 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13709 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13710 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13711 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
13712 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
13713 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
13714 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
13715 if (INTEGRAL_TYPE_P (expr
)
13716 || SCALAR_FLOAT_TYPE_P (expr
))
13718 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
13719 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
13721 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
13722 if (TREE_CODE (expr
) == RECORD_TYPE
13723 || TREE_CODE (expr
) == UNION_TYPE
13724 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
13725 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
13726 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
13733 /* Helper function for outputting the checksum of a tree T. When
13734 debugging with gdb, you can "define mynext" to be "next" followed
13735 by "call debug_fold_checksum (op0)", then just trace down till the
13738 DEBUG_FUNCTION
void
13739 debug_fold_checksum (const_tree t
)
13742 unsigned char checksum
[16];
13743 struct md5_ctx ctx
;
13744 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13746 md5_init_ctx (&ctx
);
13747 fold_checksum_tree (t
, &ctx
, &ht
);
13748 md5_finish_ctx (&ctx
, checksum
);
13751 for (i
= 0; i
< 16; i
++)
13752 fprintf (stderr
, "%d ", checksum
[i
]);
13754 fprintf (stderr
, "\n");
13759 /* Fold a unary tree expression with code CODE of type TYPE with an
13760 operand OP0. LOC is the location of the resulting expression.
13761 Return a folded expression if successful. Otherwise, return a tree
13762 expression with code CODE of type TYPE with an operand OP0. */
13765 fold_build1_loc (location_t loc
,
13766 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
13769 #ifdef ENABLE_FOLD_CHECKING
13770 unsigned char checksum_before
[16], checksum_after
[16];
13771 struct md5_ctx ctx
;
13772 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13774 md5_init_ctx (&ctx
);
13775 fold_checksum_tree (op0
, &ctx
, &ht
);
13776 md5_finish_ctx (&ctx
, checksum_before
);
13780 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13782 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
13784 #ifdef ENABLE_FOLD_CHECKING
13785 md5_init_ctx (&ctx
);
13786 fold_checksum_tree (op0
, &ctx
, &ht
);
13787 md5_finish_ctx (&ctx
, checksum_after
);
13789 if (memcmp (checksum_before
, checksum_after
, 16))
13790 fold_check_failed (op0
, tem
);
13795 /* Fold a binary tree expression with code CODE of type TYPE with
13796 operands OP0 and OP1. LOC is the location of the resulting
13797 expression. Return a folded expression if successful. Otherwise,
13798 return a tree expression with code CODE of type TYPE with operands
13802 fold_build2_loc (location_t loc
,
13803 enum tree_code code
, tree type
, tree op0
, tree op1
13807 #ifdef ENABLE_FOLD_CHECKING
13808 unsigned char checksum_before_op0
[16],
13809 checksum_before_op1
[16],
13810 checksum_after_op0
[16],
13811 checksum_after_op1
[16];
13812 struct md5_ctx ctx
;
13813 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13815 md5_init_ctx (&ctx
);
13816 fold_checksum_tree (op0
, &ctx
, &ht
);
13817 md5_finish_ctx (&ctx
, checksum_before_op0
);
13820 md5_init_ctx (&ctx
);
13821 fold_checksum_tree (op1
, &ctx
, &ht
);
13822 md5_finish_ctx (&ctx
, checksum_before_op1
);
13826 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13828 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
13830 #ifdef ENABLE_FOLD_CHECKING
13831 md5_init_ctx (&ctx
);
13832 fold_checksum_tree (op0
, &ctx
, &ht
);
13833 md5_finish_ctx (&ctx
, checksum_after_op0
);
13836 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13837 fold_check_failed (op0
, tem
);
13839 md5_init_ctx (&ctx
);
13840 fold_checksum_tree (op1
, &ctx
, &ht
);
13841 md5_finish_ctx (&ctx
, checksum_after_op1
);
13843 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13844 fold_check_failed (op1
, tem
);
13849 /* Fold a ternary tree expression with code CODE of type TYPE with
13850 operands OP0, OP1, and OP2. Return a folded expression if
13851 successful. Otherwise, return a tree expression with code CODE of
13852 type TYPE with operands OP0, OP1, and OP2. */
13855 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
13856 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
13859 #ifdef ENABLE_FOLD_CHECKING
13860 unsigned char checksum_before_op0
[16],
13861 checksum_before_op1
[16],
13862 checksum_before_op2
[16],
13863 checksum_after_op0
[16],
13864 checksum_after_op1
[16],
13865 checksum_after_op2
[16];
13866 struct md5_ctx ctx
;
13867 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13869 md5_init_ctx (&ctx
);
13870 fold_checksum_tree (op0
, &ctx
, &ht
);
13871 md5_finish_ctx (&ctx
, checksum_before_op0
);
13874 md5_init_ctx (&ctx
);
13875 fold_checksum_tree (op1
, &ctx
, &ht
);
13876 md5_finish_ctx (&ctx
, checksum_before_op1
);
13879 md5_init_ctx (&ctx
);
13880 fold_checksum_tree (op2
, &ctx
, &ht
);
13881 md5_finish_ctx (&ctx
, checksum_before_op2
);
13885 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
13886 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13888 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
13890 #ifdef ENABLE_FOLD_CHECKING
13891 md5_init_ctx (&ctx
);
13892 fold_checksum_tree (op0
, &ctx
, &ht
);
13893 md5_finish_ctx (&ctx
, checksum_after_op0
);
13896 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13897 fold_check_failed (op0
, tem
);
13899 md5_init_ctx (&ctx
);
13900 fold_checksum_tree (op1
, &ctx
, &ht
);
13901 md5_finish_ctx (&ctx
, checksum_after_op1
);
13904 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13905 fold_check_failed (op1
, tem
);
13907 md5_init_ctx (&ctx
);
13908 fold_checksum_tree (op2
, &ctx
, &ht
);
13909 md5_finish_ctx (&ctx
, checksum_after_op2
);
13911 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
13912 fold_check_failed (op2
, tem
);
13917 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13918 arguments in ARGARRAY, and a null static chain.
13919 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13920 of type TYPE from the given operands as constructed by build_call_array. */
13923 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
13924 int nargs
, tree
*argarray
)
13927 #ifdef ENABLE_FOLD_CHECKING
13928 unsigned char checksum_before_fn
[16],
13929 checksum_before_arglist
[16],
13930 checksum_after_fn
[16],
13931 checksum_after_arglist
[16];
13932 struct md5_ctx ctx
;
13933 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13936 md5_init_ctx (&ctx
);
13937 fold_checksum_tree (fn
, &ctx
, &ht
);
13938 md5_finish_ctx (&ctx
, checksum_before_fn
);
13941 md5_init_ctx (&ctx
);
13942 for (i
= 0; i
< nargs
; i
++)
13943 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13944 md5_finish_ctx (&ctx
, checksum_before_arglist
);
13948 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
13950 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13952 #ifdef ENABLE_FOLD_CHECKING
13953 md5_init_ctx (&ctx
);
13954 fold_checksum_tree (fn
, &ctx
, &ht
);
13955 md5_finish_ctx (&ctx
, checksum_after_fn
);
13958 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
13959 fold_check_failed (fn
, tem
);
13961 md5_init_ctx (&ctx
);
13962 for (i
= 0; i
< nargs
; i
++)
13963 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13964 md5_finish_ctx (&ctx
, checksum_after_arglist
);
13966 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
13967 fold_check_failed (NULL_TREE
, tem
);
13972 /* Perform constant folding and related simplification of initializer
13973 expression EXPR. These behave identically to "fold_buildN" but ignore
13974 potential run-time traps and exceptions that fold must preserve. */
13976 #define START_FOLD_INIT \
13977 int saved_signaling_nans = flag_signaling_nans;\
13978 int saved_trapping_math = flag_trapping_math;\
13979 int saved_rounding_math = flag_rounding_math;\
13980 int saved_trapv = flag_trapv;\
13981 int saved_folding_initializer = folding_initializer;\
13982 flag_signaling_nans = 0;\
13983 flag_trapping_math = 0;\
13984 flag_rounding_math = 0;\
13986 folding_initializer = 1;
13988 #define END_FOLD_INIT \
13989 flag_signaling_nans = saved_signaling_nans;\
13990 flag_trapping_math = saved_trapping_math;\
13991 flag_rounding_math = saved_rounding_math;\
13992 flag_trapv = saved_trapv;\
13993 folding_initializer = saved_folding_initializer;
13996 fold_init (tree expr
)
14001 result
= fold (expr
);
14008 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
14009 tree type
, tree op
)
14014 result
= fold_build1_loc (loc
, code
, type
, op
);
14021 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
14022 tree type
, tree op0
, tree op1
)
14027 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
14034 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
14035 int nargs
, tree
*argarray
)
14040 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14047 fold_binary_initializer_loc (location_t loc
, tree_code code
, tree type
,
14048 tree lhs
, tree rhs
)
14053 result
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
14059 #undef START_FOLD_INIT
14060 #undef END_FOLD_INIT
14062 /* Determine if first argument is a multiple of second argument. Return 0 if
14063 it is not, or we cannot easily determined it to be.
14065 An example of the sort of thing we care about (at this point; this routine
14066 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14067 fold cases do now) is discovering that
14069 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14075 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14077 This code also handles discovering that
14079 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14081 is a multiple of 8 so we don't have to worry about dealing with a
14082 possible remainder.
14084 Note that we *look* inside a SAVE_EXPR only to determine how it was
14085 calculated; it is not safe for fold to do much of anything else with the
14086 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14087 at run time. For example, the latter example above *cannot* be implemented
14088 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14089 evaluation time of the original SAVE_EXPR is not necessarily the same at
14090 the time the new expression is evaluated. The only optimization of this
14091 sort that would be valid is changing
14093 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14097 SAVE_EXPR (I) * SAVE_EXPR (J)
14099 (where the same SAVE_EXPR (J) is used in the original and the
14100 transformed version).
14102 NOWRAP specifies whether all outer operations in TYPE should
14103 be considered not wrapping. Any type conversion within TOP acts
14104 as a barrier and we will fall back to NOWRAP being false.
14105 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14106 as not wrapping even though they are generally using unsigned arithmetic. */
14109 multiple_of_p (tree type
, const_tree top
, const_tree bottom
, bool nowrap
)
14114 if (operand_equal_p (top
, bottom
, 0))
14117 if (TREE_CODE (type
) != INTEGER_TYPE
)
14120 switch (TREE_CODE (top
))
14123 /* Bitwise and provides a power of two multiple. If the mask is
14124 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14125 if (!integer_pow2p (bottom
))
14127 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14128 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14131 /* If the multiplication can wrap we cannot recurse further unless
14132 the bottom is a power of two which is where wrapping does not
14135 && !TYPE_OVERFLOW_UNDEFINED (type
)
14136 && !integer_pow2p (bottom
))
14138 if (TREE_CODE (bottom
) == INTEGER_CST
)
14140 op1
= TREE_OPERAND (top
, 0);
14141 op2
= TREE_OPERAND (top
, 1);
14142 if (TREE_CODE (op1
) == INTEGER_CST
)
14143 std::swap (op1
, op2
);
14144 if (TREE_CODE (op2
) == INTEGER_CST
)
14146 if (multiple_of_p (type
, op2
, bottom
, nowrap
))
14148 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14149 if (multiple_of_p (type
, bottom
, op2
, nowrap
))
14151 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14152 wi::to_widest (op2
));
14153 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14155 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14156 return multiple_of_p (type
, op1
, op2
, nowrap
);
14159 return multiple_of_p (type
, op1
, bottom
, nowrap
);
14162 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14163 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14166 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14167 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14169 op1
= TREE_OPERAND (top
, 1);
14170 if (wi::to_widest (op1
) < TYPE_PRECISION (type
))
14173 = wi::one (TYPE_PRECISION (type
)) << wi::to_wide (op1
);
14174 return multiple_of_p (type
,
14175 wide_int_to_tree (type
, mul_op
), bottom
,
14183 /* If the addition or subtraction can wrap we cannot recurse further
14184 unless bottom is a power of two which is where wrapping does not
14187 && !TYPE_OVERFLOW_UNDEFINED (type
)
14188 && !integer_pow2p (bottom
))
14191 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14192 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14193 but 0xfffffffd is not. */
14194 op1
= TREE_OPERAND (top
, 1);
14195 if (TREE_CODE (top
) == PLUS_EXPR
14197 && TYPE_UNSIGNED (type
)
14198 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14199 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14201 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14202 precisely, so be conservative here checking if both op0 and op1
14203 are multiple of bottom. Note we check the second operand first
14204 since it's usually simpler. */
14205 return (multiple_of_p (type
, op1
, bottom
, nowrap
)
14206 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14209 /* Can't handle conversions from non-integral or wider integral type. */
14210 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14211 || (TYPE_PRECISION (type
)
14212 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14214 /* NOWRAP only extends to operations in the outermost type so
14215 make sure to strip it off here. */
14216 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top
, 0)),
14217 TREE_OPERAND (top
, 0), bottom
, false);
14220 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
);
14223 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14224 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
, nowrap
));
14227 if (TREE_CODE (bottom
) != INTEGER_CST
|| integer_zerop (bottom
))
14229 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14233 if (TREE_CODE (bottom
) == INTEGER_CST
14234 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14235 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14237 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14239 /* Check for special cases to see if top is defined as multiple
14242 top = (X & ~(bottom - 1) ; bottom is power of 2
14248 if (code
== BIT_AND_EXPR
14249 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14250 && TREE_CODE (op2
) == INTEGER_CST
14251 && integer_pow2p (bottom
)
14252 && wi::multiple_of_p (wi::to_widest (op2
),
14253 wi::to_widest (bottom
), UNSIGNED
))
14256 op1
= gimple_assign_rhs1 (stmt
);
14257 if (code
== MINUS_EXPR
14258 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14259 && TREE_CODE (op2
) == SSA_NAME
14260 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14261 && gimple_code (stmt
) == GIMPLE_ASSIGN
14262 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14263 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14264 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14271 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14272 return multiple_p (wi::to_poly_widest (top
),
14273 wi::to_poly_widest (bottom
));
14279 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14280 This function returns true for integer expressions, and returns
14281 false if uncertain. */
14284 tree_expr_finite_p (const_tree x
)
14286 machine_mode mode
= element_mode (x
);
14287 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14289 switch (TREE_CODE (x
))
14292 return real_isfinite (TREE_REAL_CST_PTR (x
));
14294 return tree_expr_finite_p (TREE_REALPART (x
))
14295 && tree_expr_finite_p (TREE_IMAGPART (x
));
14300 case NON_LVALUE_EXPR
:
14303 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14306 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14307 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14309 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14310 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14312 switch (get_call_combined_fn (x
))
14316 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14321 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14322 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14332 /* Return true if expression X evaluates to an infinity.
14333 This function returns false for integer expressions. */
14336 tree_expr_infinite_p (const_tree x
)
14338 if (!HONOR_INFINITIES (x
))
14340 switch (TREE_CODE (x
))
14343 return real_isinf (TREE_REAL_CST_PTR (x
));
14346 case NON_LVALUE_EXPR
:
14348 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14350 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14351 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14357 /* Return true if expression X could evaluate to an infinity.
14358 This function returns false for integer expressions, and returns
14359 true if uncertain. */
14362 tree_expr_maybe_infinite_p (const_tree x
)
14364 if (!HONOR_INFINITIES (x
))
14366 switch (TREE_CODE (x
))
14369 return real_isinf (TREE_REAL_CST_PTR (x
));
14374 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14376 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14377 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14383 /* Return true if expression X evaluates to a signaling NaN.
14384 This function returns false for integer expressions. */
14387 tree_expr_signaling_nan_p (const_tree x
)
14389 if (!HONOR_SNANS (x
))
14391 switch (TREE_CODE (x
))
14394 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14395 case NON_LVALUE_EXPR
:
14397 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14399 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14400 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14406 /* Return true if expression X could evaluate to a signaling NaN.
14407 This function returns false for integer expressions, and returns
14408 true if uncertain. */
14411 tree_expr_maybe_signaling_nan_p (const_tree x
)
14413 if (!HONOR_SNANS (x
))
14415 switch (TREE_CODE (x
))
14418 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14424 case NON_LVALUE_EXPR
:
14426 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14429 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14430 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14432 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14433 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14435 switch (get_call_combined_fn (x
))
14439 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14444 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14445 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14454 /* Return true if expression X evaluates to a NaN.
14455 This function returns false for integer expressions. */
14458 tree_expr_nan_p (const_tree x
)
14460 if (!HONOR_NANS (x
))
14462 switch (TREE_CODE (x
))
14465 return real_isnan (TREE_REAL_CST_PTR (x
));
14466 case NON_LVALUE_EXPR
:
14468 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14470 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14471 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14477 /* Return true if expression X could evaluate to a NaN.
14478 This function returns false for integer expressions, and returns
14479 true if uncertain. */
14482 tree_expr_maybe_nan_p (const_tree x
)
14484 if (!HONOR_NANS (x
))
14486 switch (TREE_CODE (x
))
14489 return real_isnan (TREE_REAL_CST_PTR (x
));
14495 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14496 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14500 case NON_LVALUE_EXPR
:
14502 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14505 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14506 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14508 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14509 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14511 switch (get_call_combined_fn (x
))
14515 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14520 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14521 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14530 /* Return true if expression X could evaluate to -0.0.
14531 This function returns true if uncertain. */
14534 tree_expr_maybe_real_minus_zero_p (const_tree x
)
14536 if (!HONOR_SIGNED_ZEROS (x
))
14538 switch (TREE_CODE (x
))
14541 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x
));
14546 case NON_LVALUE_EXPR
:
14548 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 0));
14550 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 1))
14551 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 2));
14553 switch (get_call_combined_fn (x
))
14564 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14565 * but currently those predicates require tree and not const_tree. */
14569 #define tree_expr_nonnegative_warnv_p(X, Y) \
14570 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14572 #define RECURSE(X) \
14573 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14575 /* Return true if CODE or TYPE is known to be non-negative. */
14578 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14580 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14581 && truth_value_p (code
))
14582 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14583 have a signed:1 type (where the value is -1 and 0). */
14588 /* Return true if (CODE OP0) is known to be non-negative. If the return
14589 value is based on the assumption that signed overflow is undefined,
14590 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14591 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14594 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14595 bool *strict_overflow_p
, int depth
)
14597 if (TYPE_UNSIGNED (type
))
14603 /* We can't return 1 if flag_wrapv is set because
14604 ABS_EXPR<INT_MIN> = INT_MIN. */
14605 if (!ANY_INTEGRAL_TYPE_P (type
))
14607 if (TYPE_OVERFLOW_UNDEFINED (type
))
14609 *strict_overflow_p
= true;
14614 case NON_LVALUE_EXPR
:
14616 case FIX_TRUNC_EXPR
:
14617 return RECURSE (op0
);
14621 tree inner_type
= TREE_TYPE (op0
);
14622 tree outer_type
= type
;
14624 if (TREE_CODE (outer_type
) == REAL_TYPE
)
14626 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14627 return RECURSE (op0
);
14628 if (INTEGRAL_TYPE_P (inner_type
))
14630 if (TYPE_UNSIGNED (inner_type
))
14632 return RECURSE (op0
);
14635 else if (INTEGRAL_TYPE_P (outer_type
))
14637 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14638 return RECURSE (op0
);
14639 if (INTEGRAL_TYPE_P (inner_type
))
14640 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14641 && TYPE_UNSIGNED (inner_type
);
14647 return tree_simple_nonnegative_warnv_p (code
, type
);
14650 /* We don't know sign of `t', so be conservative and return false. */
14654 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14655 value is based on the assumption that signed overflow is undefined,
14656 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14657 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14660 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14661 tree op1
, bool *strict_overflow_p
,
14664 if (TYPE_UNSIGNED (type
))
14669 case POINTER_PLUS_EXPR
:
14671 if (FLOAT_TYPE_P (type
))
14672 return RECURSE (op0
) && RECURSE (op1
);
14674 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14675 both unsigned and at least 2 bits shorter than the result. */
14676 if (TREE_CODE (type
) == INTEGER_TYPE
14677 && TREE_CODE (op0
) == NOP_EXPR
14678 && TREE_CODE (op1
) == NOP_EXPR
)
14680 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14681 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14682 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14683 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14685 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14686 TYPE_PRECISION (inner2
)) + 1;
14687 return prec
< TYPE_PRECISION (type
);
14693 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14695 /* x * x is always non-negative for floating point x
14696 or without overflow. */
14697 if (operand_equal_p (op0
, op1
, 0)
14698 || (RECURSE (op0
) && RECURSE (op1
)))
14700 if (ANY_INTEGRAL_TYPE_P (type
)
14701 && TYPE_OVERFLOW_UNDEFINED (type
))
14702 *strict_overflow_p
= true;
14707 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14708 both unsigned and their total bits is shorter than the result. */
14709 if (TREE_CODE (type
) == INTEGER_TYPE
14710 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14711 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
14713 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
14714 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
14716 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
14717 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
14720 bool unsigned0
= TYPE_UNSIGNED (inner0
);
14721 bool unsigned1
= TYPE_UNSIGNED (inner1
);
14723 if (TREE_CODE (op0
) == INTEGER_CST
)
14724 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
14726 if (TREE_CODE (op1
) == INTEGER_CST
)
14727 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
14729 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
14730 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
14732 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
14733 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
14734 : TYPE_PRECISION (inner0
);
14736 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
14737 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
14738 : TYPE_PRECISION (inner1
);
14740 return precision0
+ precision1
< TYPE_PRECISION (type
);
14746 return RECURSE (op0
) || RECURSE (op1
);
14749 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14751 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
14752 return RECURSE (op0
) && RECURSE (op1
);
14753 return RECURSE (op0
) || RECURSE (op1
);
14759 case TRUNC_DIV_EXPR
:
14760 case CEIL_DIV_EXPR
:
14761 case FLOOR_DIV_EXPR
:
14762 case ROUND_DIV_EXPR
:
14763 return RECURSE (op0
) && RECURSE (op1
);
14765 case TRUNC_MOD_EXPR
:
14766 return RECURSE (op0
);
14768 case FLOOR_MOD_EXPR
:
14769 return RECURSE (op1
);
14771 case CEIL_MOD_EXPR
:
14772 case ROUND_MOD_EXPR
:
14774 return tree_simple_nonnegative_warnv_p (code
, type
);
14777 /* We don't know sign of `t', so be conservative and return false. */
14781 /* Return true if T is known to be non-negative. If the return
14782 value is based on the assumption that signed overflow is undefined,
14783 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14784 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14787 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14789 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14792 switch (TREE_CODE (t
))
14795 return tree_int_cst_sgn (t
) >= 0;
14798 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
14801 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
14804 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
14807 /* Limit the depth of recursion to avoid quadratic behavior.
14808 This is expected to catch almost all occurrences in practice.
14809 If this code misses important cases that unbounded recursion
14810 would not, passes that need this information could be revised
14811 to provide it through dataflow propagation. */
14812 return (!name_registered_for_update_p (t
)
14813 && depth
< param_max_ssa_name_query_depth
14814 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
14815 strict_overflow_p
, depth
));
14818 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
14822 /* Return true if T is known to be non-negative. If the return
14823 value is based on the assumption that signed overflow is undefined,
14824 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14825 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14828 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
14829 bool *strict_overflow_p
, int depth
)
14860 case CFN_BUILT_IN_BSWAP16
:
14861 case CFN_BUILT_IN_BSWAP32
:
14862 case CFN_BUILT_IN_BSWAP64
:
14863 case CFN_BUILT_IN_BSWAP128
:
14869 /* sqrt(-0.0) is -0.0. */
14870 if (!HONOR_SIGNED_ZEROS (type
))
14872 return RECURSE (arg0
);
14904 CASE_CFN_LLRINT_FN
:
14906 CASE_CFN_LLROUND_FN
:
14910 CASE_CFN_LROUND_FN
:
14913 CASE_CFN_NEARBYINT
:
14914 CASE_CFN_NEARBYINT_FN
:
14919 CASE_CFN_ROUNDEVEN
:
14920 CASE_CFN_ROUNDEVEN_FN
:
14923 CASE_CFN_SCALBLN_FN
:
14925 CASE_CFN_SCALBN_FN
:
14927 CASE_CFN_SIGNIFICAND
:
14934 /* True if the 1st argument is nonnegative. */
14935 return RECURSE (arg0
);
14939 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14940 things. In the presence of sNaNs, we're only guaranteed to be
14941 non-negative if both operands are non-negative. In the presence
14942 of qNaNs, we're non-negative if either operand is non-negative
14943 and can't be a qNaN, or if both operands are non-negative. */
14944 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
14945 tree_expr_maybe_signaling_nan_p (arg1
))
14946 return RECURSE (arg0
) && RECURSE (arg1
);
14947 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
14950 && !tree_expr_maybe_nan_p (arg1
));
14954 /* True if the 1st AND 2nd arguments are nonnegative. */
14955 return RECURSE (arg0
) && RECURSE (arg1
);
14958 CASE_CFN_COPYSIGN_FN
:
14959 /* True if the 2nd argument is nonnegative. */
14960 return RECURSE (arg1
);
14963 /* True if the 1st argument is nonnegative or the second
14964 argument is an even integer. */
14965 if (TREE_CODE (arg1
) == INTEGER_CST
14966 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
14968 return RECURSE (arg0
);
14972 /* True if the 1st argument is nonnegative or the second
14973 argument is an even integer valued real. */
14974 if (TREE_CODE (arg1
) == REAL_CST
)
14979 c
= TREE_REAL_CST (arg1
);
14980 n
= real_to_integer (&c
);
14983 REAL_VALUE_TYPE cint
;
14984 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
14985 if (real_identical (&c
, &cint
))
14989 return RECURSE (arg0
);
14994 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
14997 /* Return true if T is known to be non-negative. If the return
14998 value is based on the assumption that signed overflow is undefined,
14999 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15000 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15003 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15005 enum tree_code code
= TREE_CODE (t
);
15006 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15013 tree temp
= TARGET_EXPR_SLOT (t
);
15014 t
= TARGET_EXPR_INITIAL (t
);
15016 /* If the initializer is non-void, then it's a normal expression
15017 that will be assigned to the slot. */
15018 if (!VOID_TYPE_P (TREE_TYPE (t
)))
15019 return RECURSE (t
);
15021 /* Otherwise, the initializer sets the slot in some way. One common
15022 way is an assignment statement at the end of the initializer. */
15025 if (TREE_CODE (t
) == BIND_EXPR
)
15026 t
= expr_last (BIND_EXPR_BODY (t
));
15027 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
15028 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
15029 t
= expr_last (TREE_OPERAND (t
, 0));
15030 else if (TREE_CODE (t
) == STATEMENT_LIST
)
15035 if (TREE_CODE (t
) == MODIFY_EXPR
15036 && TREE_OPERAND (t
, 0) == temp
)
15037 return RECURSE (TREE_OPERAND (t
, 1));
15044 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
15045 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
15047 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
15048 get_call_combined_fn (t
),
15051 strict_overflow_p
, depth
);
15053 case COMPOUND_EXPR
:
15055 return RECURSE (TREE_OPERAND (t
, 1));
15058 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
15061 return RECURSE (TREE_OPERAND (t
, 0));
15064 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15069 #undef tree_expr_nonnegative_warnv_p
15071 /* Return true if T is known to be non-negative. If the return
15072 value is based on the assumption that signed overflow is undefined,
15073 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15074 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15077 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15079 enum tree_code code
;
15080 if (t
== error_mark_node
)
15083 code
= TREE_CODE (t
);
15084 switch (TREE_CODE_CLASS (code
))
15087 case tcc_comparison
:
15088 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15090 TREE_OPERAND (t
, 0),
15091 TREE_OPERAND (t
, 1),
15092 strict_overflow_p
, depth
);
15095 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15097 TREE_OPERAND (t
, 0),
15098 strict_overflow_p
, depth
);
15101 case tcc_declaration
:
15102 case tcc_reference
:
15103 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15111 case TRUTH_AND_EXPR
:
15112 case TRUTH_OR_EXPR
:
15113 case TRUTH_XOR_EXPR
:
15114 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15116 TREE_OPERAND (t
, 0),
15117 TREE_OPERAND (t
, 1),
15118 strict_overflow_p
, depth
);
15119 case TRUTH_NOT_EXPR
:
15120 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15122 TREE_OPERAND (t
, 0),
15123 strict_overflow_p
, depth
);
15129 case WITH_SIZE_EXPR
:
15131 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15134 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15138 /* Return true if `t' is known to be non-negative. Handle warnings
15139 about undefined signed overflow. */
15142 tree_expr_nonnegative_p (tree t
)
15144 bool ret
, strict_overflow_p
;
15146 strict_overflow_p
= false;
15147 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15148 if (strict_overflow_p
)
15149 fold_overflow_warning (("assuming signed overflow does not occur when "
15150 "determining that expression is always "
15152 WARN_STRICT_OVERFLOW_MISC
);
15157 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15158 For floating point we further ensure that T is not denormal.
15159 Similar logic is present in nonzero_address in rtlanal.h.
15161 If the return value is based on the assumption that signed overflow
15162 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15163 change *STRICT_OVERFLOW_P. */
15166 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15167 bool *strict_overflow_p
)
15172 return tree_expr_nonzero_warnv_p (op0
,
15173 strict_overflow_p
);
15177 tree inner_type
= TREE_TYPE (op0
);
15178 tree outer_type
= type
;
15180 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15181 && tree_expr_nonzero_warnv_p (op0
,
15182 strict_overflow_p
));
15186 case NON_LVALUE_EXPR
:
15187 return tree_expr_nonzero_warnv_p (op0
,
15188 strict_overflow_p
);
15197 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15198 For floating point we further ensure that T is not denormal.
15199 Similar logic is present in nonzero_address in rtlanal.h.
15201 If the return value is based on the assumption that signed overflow
15202 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15203 change *STRICT_OVERFLOW_P. */
15206 tree_binary_nonzero_warnv_p (enum tree_code code
,
15209 tree op1
, bool *strict_overflow_p
)
15211 bool sub_strict_overflow_p
;
15214 case POINTER_PLUS_EXPR
:
15216 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15218 /* With the presence of negative values it is hard
15219 to say something. */
15220 sub_strict_overflow_p
= false;
15221 if (!tree_expr_nonnegative_warnv_p (op0
,
15222 &sub_strict_overflow_p
)
15223 || !tree_expr_nonnegative_warnv_p (op1
,
15224 &sub_strict_overflow_p
))
15226 /* One of operands must be positive and the other non-negative. */
15227 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15228 overflows, on a twos-complement machine the sum of two
15229 nonnegative numbers can never be zero. */
15230 return (tree_expr_nonzero_warnv_p (op0
,
15232 || tree_expr_nonzero_warnv_p (op1
,
15233 strict_overflow_p
));
15238 if (TYPE_OVERFLOW_UNDEFINED (type
))
15240 if (tree_expr_nonzero_warnv_p (op0
,
15242 && tree_expr_nonzero_warnv_p (op1
,
15243 strict_overflow_p
))
15245 *strict_overflow_p
= true;
15252 sub_strict_overflow_p
= false;
15253 if (tree_expr_nonzero_warnv_p (op0
,
15254 &sub_strict_overflow_p
)
15255 && tree_expr_nonzero_warnv_p (op1
,
15256 &sub_strict_overflow_p
))
15258 if (sub_strict_overflow_p
)
15259 *strict_overflow_p
= true;
15264 sub_strict_overflow_p
= false;
15265 if (tree_expr_nonzero_warnv_p (op0
,
15266 &sub_strict_overflow_p
))
15268 if (sub_strict_overflow_p
)
15269 *strict_overflow_p
= true;
15271 /* When both operands are nonzero, then MAX must be too. */
15272 if (tree_expr_nonzero_warnv_p (op1
,
15273 strict_overflow_p
))
15276 /* MAX where operand 0 is positive is positive. */
15277 return tree_expr_nonnegative_warnv_p (op0
,
15278 strict_overflow_p
);
15280 /* MAX where operand 1 is positive is positive. */
15281 else if (tree_expr_nonzero_warnv_p (op1
,
15282 &sub_strict_overflow_p
)
15283 && tree_expr_nonnegative_warnv_p (op1
,
15284 &sub_strict_overflow_p
))
15286 if (sub_strict_overflow_p
)
15287 *strict_overflow_p
= true;
15293 return (tree_expr_nonzero_warnv_p (op1
,
15295 || tree_expr_nonzero_warnv_p (op0
,
15296 strict_overflow_p
));
15305 /* Return true when T is an address and is known to be nonzero.
15306 For floating point we further ensure that T is not denormal.
15307 Similar logic is present in nonzero_address in rtlanal.h.
15309 If the return value is based on the assumption that signed overflow
15310 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15311 change *STRICT_OVERFLOW_P. */
15314 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15316 bool sub_strict_overflow_p
;
15317 switch (TREE_CODE (t
))
15320 return !integer_zerop (t
);
15324 tree base
= TREE_OPERAND (t
, 0);
15326 if (!DECL_P (base
))
15327 base
= get_base_address (base
);
15329 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15330 base
= TARGET_EXPR_SLOT (base
);
15335 /* For objects in symbol table check if we know they are non-zero.
15336 Don't do anything for variables and functions before symtab is built;
15337 it is quite possible that they will be declared weak later. */
15338 int nonzero_addr
= maybe_nonzero_address (base
);
15339 if (nonzero_addr
>= 0)
15340 return nonzero_addr
;
15342 /* Constants are never weak. */
15343 if (CONSTANT_CLASS_P (base
))
15350 sub_strict_overflow_p
= false;
15351 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15352 &sub_strict_overflow_p
)
15353 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15354 &sub_strict_overflow_p
))
15356 if (sub_strict_overflow_p
)
15357 *strict_overflow_p
= true;
15363 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15365 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15373 #define integer_valued_real_p(X) \
15374 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15376 #define RECURSE(X) \
15377 ((integer_valued_real_p) (X, depth + 1))
15379 /* Return true if the floating point result of (CODE OP0) has an
15380 integer value. We also allow +Inf, -Inf and NaN to be considered
15381 integer values. Return false for signaling NaN.
15383 DEPTH is the current nesting depth of the query. */
15386 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15394 return RECURSE (op0
);
15398 tree type
= TREE_TYPE (op0
);
15399 if (TREE_CODE (type
) == INTEGER_TYPE
)
15401 if (TREE_CODE (type
) == REAL_TYPE
)
15402 return RECURSE (op0
);
15412 /* Return true if the floating point result of (CODE OP0 OP1) has an
15413 integer value. We also allow +Inf, -Inf and NaN to be considered
15414 integer values. Return false for signaling NaN.
15416 DEPTH is the current nesting depth of the query. */
15419 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15428 return RECURSE (op0
) && RECURSE (op1
);
15436 /* Return true if the floating point result of calling FNDECL with arguments
15437 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15438 considered integer values. Return false for signaling NaN. If FNDECL
15439 takes fewer than 2 arguments, the remaining ARGn are null.
15441 DEPTH is the current nesting depth of the query. */
15444 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15452 CASE_CFN_NEARBYINT
:
15453 CASE_CFN_NEARBYINT_FN
:
15458 CASE_CFN_ROUNDEVEN
:
15459 CASE_CFN_ROUNDEVEN_FN
:
15468 return RECURSE (arg0
) && RECURSE (arg1
);
15476 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15477 has an integer value. We also allow +Inf, -Inf and NaN to be
15478 considered integer values. Return false for signaling NaN.
15480 DEPTH is the current nesting depth of the query. */
15483 integer_valued_real_single_p (tree t
, int depth
)
15485 switch (TREE_CODE (t
))
15488 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15491 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15494 /* Limit the depth of recursion to avoid quadratic behavior.
15495 This is expected to catch almost all occurrences in practice.
15496 If this code misses important cases that unbounded recursion
15497 would not, passes that need this information could be revised
15498 to provide it through dataflow propagation. */
15499 return (!name_registered_for_update_p (t
)
15500 && depth
< param_max_ssa_name_query_depth
15501 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15510 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15511 has an integer value. We also allow +Inf, -Inf and NaN to be
15512 considered integer values. Return false for signaling NaN.
15514 DEPTH is the current nesting depth of the query. */
15517 integer_valued_real_invalid_p (tree t
, int depth
)
15519 switch (TREE_CODE (t
))
15521 case COMPOUND_EXPR
:
15524 return RECURSE (TREE_OPERAND (t
, 1));
15527 return RECURSE (TREE_OPERAND (t
, 0));
15536 #undef integer_valued_real_p
15538 /* Return true if the floating point expression T has an integer value.
15539 We also allow +Inf, -Inf and NaN to be considered integer values.
15540 Return false for signaling NaN.
15542 DEPTH is the current nesting depth of the query. */
15545 integer_valued_real_p (tree t
, int depth
)
15547 if (t
== error_mark_node
)
15550 STRIP_ANY_LOCATION_WRAPPER (t
);
15552 tree_code code
= TREE_CODE (t
);
15553 switch (TREE_CODE_CLASS (code
))
15556 case tcc_comparison
:
15557 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15558 TREE_OPERAND (t
, 1), depth
);
15561 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15564 case tcc_declaration
:
15565 case tcc_reference
:
15566 return integer_valued_real_single_p (t
, depth
);
15576 return integer_valued_real_single_p (t
, depth
);
15580 tree arg0
= (call_expr_nargs (t
) > 0
15581 ? CALL_EXPR_ARG (t
, 0)
15583 tree arg1
= (call_expr_nargs (t
) > 1
15584 ? CALL_EXPR_ARG (t
, 1)
15586 return integer_valued_real_call_p (get_call_combined_fn (t
),
15587 arg0
, arg1
, depth
);
15591 return integer_valued_real_invalid_p (t
, depth
);
15595 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15596 attempt to fold the expression to a constant without modifying TYPE,
15599 If the expression could be simplified to a constant, then return
15600 the constant. If the expression would not be simplified to a
15601 constant, then return NULL_TREE. */
15604 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15606 tree tem
= fold_binary (code
, type
, op0
, op1
);
15607 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15610 /* Given the components of a unary expression CODE, TYPE and OP0,
15611 attempt to fold the expression to a constant without modifying
15614 If the expression could be simplified to a constant, then return
15615 the constant. If the expression would not be simplified to a
15616 constant, then return NULL_TREE. */
15619 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15621 tree tem
= fold_unary (code
, type
, op0
);
15622 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15625 /* If EXP represents referencing an element in a constant string
15626 (either via pointer arithmetic or array indexing), return the
15627 tree representing the value accessed, otherwise return NULL. */
15630 fold_read_from_constant_string (tree exp
)
15632 if ((TREE_CODE (exp
) == INDIRECT_REF
15633 || TREE_CODE (exp
) == ARRAY_REF
)
15634 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15636 tree exp1
= TREE_OPERAND (exp
, 0);
15639 location_t loc
= EXPR_LOCATION (exp
);
15641 if (TREE_CODE (exp
) == INDIRECT_REF
)
15642 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15645 tree low_bound
= array_ref_low_bound (exp
);
15646 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15648 /* Optimize the special-case of a zero lower bound.
15650 We convert the low_bound to sizetype to avoid some problems
15651 with constant folding. (E.g. suppose the lower bound is 1,
15652 and its mode is QI. Without the conversion,l (ARRAY
15653 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15654 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15655 if (! integer_zerop (low_bound
))
15656 index
= size_diffop_loc (loc
, index
,
15657 fold_convert_loc (loc
, sizetype
, low_bound
));
15662 scalar_int_mode char_mode
;
15664 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15665 && TREE_CODE (string
) == STRING_CST
15666 && tree_fits_uhwi_p (index
)
15667 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15668 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15670 && GET_MODE_SIZE (char_mode
) == 1)
15671 return build_int_cst_type (TREE_TYPE (exp
),
15672 (TREE_STRING_POINTER (string
)
15673 [TREE_INT_CST_LOW (index
)]));
15678 /* Folds a read from vector element at IDX of vector ARG. */
15681 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15683 unsigned HOST_WIDE_INT i
;
15684 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15685 && known_ge (idx
, 0u)
15686 && idx
.is_constant (&i
))
15688 if (TREE_CODE (arg
) == VECTOR_CST
)
15689 return VECTOR_CST_ELT (arg
, i
);
15690 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15692 if (CONSTRUCTOR_NELTS (arg
)
15693 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg
, 0)->value
)))
15695 if (i
>= CONSTRUCTOR_NELTS (arg
))
15696 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
15697 return CONSTRUCTOR_ELT (arg
, i
)->value
;
15703 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15704 an integer constant, real, or fixed-point constant.
15706 TYPE is the type of the result. */
15709 fold_negate_const (tree arg0
, tree type
)
15711 tree t
= NULL_TREE
;
15713 switch (TREE_CODE (arg0
))
15716 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15721 FIXED_VALUE_TYPE f
;
15722 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
15723 &(TREE_FIXED_CST (arg0
)), NULL
,
15724 TYPE_SATURATING (type
));
15725 t
= build_fixed (type
, f
);
15726 /* Propagate overflow flags. */
15727 if (overflow_p
| TREE_OVERFLOW (arg0
))
15728 TREE_OVERFLOW (t
) = 1;
15733 if (poly_int_tree_p (arg0
))
15735 wi::overflow_type overflow
;
15736 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
15737 t
= force_fit_type (type
, res
, 1,
15738 (overflow
&& ! TYPE_UNSIGNED (type
))
15739 || TREE_OVERFLOW (arg0
));
15743 gcc_unreachable ();
15749 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15750 an integer constant or real constant.
15752 TYPE is the type of the result. */
15755 fold_abs_const (tree arg0
, tree type
)
15757 tree t
= NULL_TREE
;
15759 switch (TREE_CODE (arg0
))
15763 /* If the value is unsigned or non-negative, then the absolute value
15764 is the same as the ordinary value. */
15765 wide_int val
= wi::to_wide (arg0
);
15766 wi::overflow_type overflow
= wi::OVF_NONE
;
15767 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
15770 /* If the value is negative, then the absolute value is
15773 val
= wi::neg (val
, &overflow
);
15775 /* Force to the destination type, set TREE_OVERFLOW for signed
15777 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
15782 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
15783 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15789 gcc_unreachable ();
15795 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15796 constant. TYPE is the type of the result. */
15799 fold_not_const (const_tree arg0
, tree type
)
15801 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
15803 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
15806 /* Given CODE, a relational operator, the target type, TYPE and two
15807 constant operands OP0 and OP1, return the result of the
15808 relational operation. If the result is not a compile time
15809 constant, then return NULL_TREE. */
15812 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
15814 int result
, invert
;
15816 /* From here on, the only cases we handle are when the result is
15817 known to be a constant. */
15819 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
15821 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
15822 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
15824 /* Handle the cases where either operand is a NaN. */
15825 if (real_isnan (c0
) || real_isnan (c1
))
15835 case UNORDERED_EXPR
:
15849 if (flag_trapping_math
)
15855 gcc_unreachable ();
15858 return constant_boolean_node (result
, type
);
15861 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
15864 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
15866 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
15867 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
15868 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
15871 /* Handle equality/inequality of complex constants. */
15872 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
15874 tree rcond
= fold_relational_const (code
, type
,
15875 TREE_REALPART (op0
),
15876 TREE_REALPART (op1
));
15877 tree icond
= fold_relational_const (code
, type
,
15878 TREE_IMAGPART (op0
),
15879 TREE_IMAGPART (op1
));
15880 if (code
== EQ_EXPR
)
15881 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
15882 else if (code
== NE_EXPR
)
15883 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
15888 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
15890 if (!VECTOR_TYPE_P (type
))
15892 /* Have vector comparison with scalar boolean result. */
15893 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
15894 && known_eq (VECTOR_CST_NELTS (op0
),
15895 VECTOR_CST_NELTS (op1
)));
15896 unsigned HOST_WIDE_INT nunits
;
15897 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
15899 for (unsigned i
= 0; i
< nunits
; i
++)
15901 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15902 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15903 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
15904 if (tmp
== NULL_TREE
)
15906 if (integer_zerop (tmp
))
15907 return constant_boolean_node (code
== NE_EXPR
, type
);
15909 return constant_boolean_node (code
== EQ_EXPR
, type
);
15911 tree_vector_builder elts
;
15912 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
15914 unsigned int count
= elts
.encoded_nelts ();
15915 for (unsigned i
= 0; i
< count
; i
++)
15917 tree elem_type
= TREE_TYPE (type
);
15918 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15919 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15921 tree tem
= fold_relational_const (code
, elem_type
,
15924 if (tem
== NULL_TREE
)
15927 elts
.quick_push (build_int_cst (elem_type
,
15928 integer_zerop (tem
) ? 0 : -1));
15931 return elts
.build ();
15934 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15936 To compute GT, swap the arguments and do LT.
15937 To compute GE, do LT and invert the result.
15938 To compute LE, swap the arguments, do LT and invert the result.
15939 To compute NE, do EQ and invert the result.
15941 Therefore, the code below must handle only EQ and LT. */
15943 if (code
== LE_EXPR
|| code
== GT_EXPR
)
15945 std::swap (op0
, op1
);
15946 code
= swap_tree_comparison (code
);
15949 /* Note that it is safe to invert for real values here because we
15950 have already handled the one case that it matters. */
15953 if (code
== NE_EXPR
|| code
== GE_EXPR
)
15956 code
= invert_tree_comparison (code
, false);
15959 /* Compute a result for LT or EQ if args permit;
15960 Otherwise return T. */
15961 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
15963 if (code
== EQ_EXPR
)
15964 result
= tree_int_cst_equal (op0
, op1
);
15966 result
= tree_int_cst_lt (op0
, op1
);
15973 return constant_boolean_node (result
, type
);
15976 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15977 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15981 fold_build_cleanup_point_expr (tree type
, tree expr
)
15983 /* If the expression does not have side effects then we don't have to wrap
15984 it with a cleanup point expression. */
15985 if (!TREE_SIDE_EFFECTS (expr
))
15988 /* If the expression is a return, check to see if the expression inside the
15989 return has no side effects or the right hand side of the modify expression
15990 inside the return. If either don't have side effects set we don't need to
15991 wrap the expression in a cleanup point expression. Note we don't check the
15992 left hand side of the modify because it should always be a return decl. */
15993 if (TREE_CODE (expr
) == RETURN_EXPR
)
15995 tree op
= TREE_OPERAND (expr
, 0);
15996 if (!op
|| !TREE_SIDE_EFFECTS (op
))
15998 op
= TREE_OPERAND (op
, 1);
15999 if (!TREE_SIDE_EFFECTS (op
))
16003 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
16006 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16007 of an indirection through OP0, or NULL_TREE if no simplification is
16011 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
16015 poly_uint64 const_op01
;
16018 subtype
= TREE_TYPE (sub
);
16019 if (!POINTER_TYPE_P (subtype
)
16020 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
16023 if (TREE_CODE (sub
) == ADDR_EXPR
)
16025 tree op
= TREE_OPERAND (sub
, 0);
16026 tree optype
= TREE_TYPE (op
);
16028 /* *&CONST_DECL -> to the value of the const decl. */
16029 if (TREE_CODE (op
) == CONST_DECL
)
16030 return DECL_INITIAL (op
);
16031 /* *&p => p; make sure to handle *&"str"[cst] here. */
16032 if (type
== optype
)
16034 tree fop
= fold_read_from_constant_string (op
);
16040 /* *(foo *)&fooarray => fooarray[0] */
16041 else if (TREE_CODE (optype
) == ARRAY_TYPE
16042 && type
== TREE_TYPE (optype
)
16043 && (!in_gimple_form
16044 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16046 tree type_domain
= TYPE_DOMAIN (optype
);
16047 tree min_val
= size_zero_node
;
16048 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16049 min_val
= TYPE_MIN_VALUE (type_domain
);
16051 && TREE_CODE (min_val
) != INTEGER_CST
)
16053 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
16054 NULL_TREE
, NULL_TREE
);
16056 /* *(foo *)&complexfoo => __real__ complexfoo */
16057 else if (TREE_CODE (optype
) == COMPLEX_TYPE
16058 && type
== TREE_TYPE (optype
))
16059 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
16060 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16061 else if (VECTOR_TYPE_P (optype
)
16062 && type
== TREE_TYPE (optype
))
16064 tree part_width
= TYPE_SIZE (type
);
16065 tree index
= bitsize_int (0);
16066 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
16071 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16072 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
16074 tree op00
= TREE_OPERAND (sub
, 0);
16075 tree op01
= TREE_OPERAND (sub
, 1);
16078 if (TREE_CODE (op00
) == ADDR_EXPR
)
16081 op00
= TREE_OPERAND (op00
, 0);
16082 op00type
= TREE_TYPE (op00
);
16084 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16085 if (VECTOR_TYPE_P (op00type
)
16086 && type
== TREE_TYPE (op00type
)
16087 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16088 but we want to treat offsets with MSB set as negative.
16089 For the code below negative offsets are invalid and
16090 TYPE_SIZE of the element is something unsigned, so
16091 check whether op01 fits into poly_int64, which implies
16092 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16093 then just use poly_uint64 because we want to treat the
16094 value as unsigned. */
16095 && tree_fits_poly_int64_p (op01
))
16097 tree part_width
= TYPE_SIZE (type
);
16098 poly_uint64 max_offset
16099 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
16100 * TYPE_VECTOR_SUBPARTS (op00type
));
16101 if (known_lt (const_op01
, max_offset
))
16103 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
16104 return fold_build3_loc (loc
,
16105 BIT_FIELD_REF
, type
, op00
,
16106 part_width
, index
);
16109 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16110 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16111 && type
== TREE_TYPE (op00type
))
16113 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
16115 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16117 /* ((foo *)&fooarray)[1] => fooarray[1] */
16118 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16119 && type
== TREE_TYPE (op00type
))
16121 tree type_domain
= TYPE_DOMAIN (op00type
);
16122 tree min_val
= size_zero_node
;
16123 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16124 min_val
= TYPE_MIN_VALUE (type_domain
);
16125 poly_uint64 type_size
, index
;
16126 if (poly_int_tree_p (min_val
)
16127 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
16128 && multiple_p (const_op01
, type_size
, &index
))
16130 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
16131 op01
= wide_int_to_tree (sizetype
, off
);
16132 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16133 NULL_TREE
, NULL_TREE
);
16139 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16140 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16141 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16142 && (!in_gimple_form
16143 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16146 tree min_val
= size_zero_node
;
16147 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16148 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16149 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16150 min_val
= TYPE_MIN_VALUE (type_domain
);
16152 && TREE_CODE (min_val
) != INTEGER_CST
)
16154 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16161 /* Builds an expression for an indirection through T, simplifying some
16165 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16167 tree type
= TREE_TYPE (TREE_TYPE (t
));
16168 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16173 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16176 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16179 fold_indirect_ref_loc (location_t loc
, tree t
)
16181 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16189 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16190 whose result is ignored. The type of the returned tree need not be
16191 the same as the original expression. */
16194 fold_ignored_result (tree t
)
16196 if (!TREE_SIDE_EFFECTS (t
))
16197 return integer_zero_node
;
16200 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16203 t
= TREE_OPERAND (t
, 0);
16207 case tcc_comparison
:
16208 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16209 t
= TREE_OPERAND (t
, 0);
16210 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16211 t
= TREE_OPERAND (t
, 1);
16216 case tcc_expression
:
16217 switch (TREE_CODE (t
))
16219 case COMPOUND_EXPR
:
16220 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16222 t
= TREE_OPERAND (t
, 0);
16226 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16227 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16229 t
= TREE_OPERAND (t
, 0);
16242 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16245 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16247 tree div
= NULL_TREE
;
16252 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16253 have to do anything. Only do this when we are not given a const,
16254 because in that case, this check is more expensive than just
16256 if (TREE_CODE (value
) != INTEGER_CST
)
16258 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16260 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16264 /* If divisor is a power of two, simplify this to bit manipulation. */
16265 if (pow2_or_zerop (divisor
))
16267 if (TREE_CODE (value
) == INTEGER_CST
)
16269 wide_int val
= wi::to_wide (value
);
16272 if ((val
& (divisor
- 1)) == 0)
16275 overflow_p
= TREE_OVERFLOW (value
);
16276 val
+= divisor
- 1;
16277 val
&= (int) -divisor
;
16281 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16287 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16288 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16289 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16290 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16296 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16297 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16298 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16304 /* Likewise, but round down. */
16307 round_down_loc (location_t loc
, tree value
, int divisor
)
16309 tree div
= NULL_TREE
;
16311 gcc_assert (divisor
> 0);
16315 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16316 have to do anything. Only do this when we are not given a const,
16317 because in that case, this check is more expensive than just
16319 if (TREE_CODE (value
) != INTEGER_CST
)
16321 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16323 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16327 /* If divisor is a power of two, simplify this to bit manipulation. */
16328 if (pow2_or_zerop (divisor
))
16332 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16333 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16338 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16339 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16340 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16346 /* Returns the pointer to the base of the object addressed by EXP and
16347 extracts the information about the offset of the access, storing it
16348 to PBITPOS and POFFSET. */
16351 split_address_to_core_and_offset (tree exp
,
16352 poly_int64_pod
*pbitpos
, tree
*poffset
)
16356 int unsignedp
, reversep
, volatilep
;
16357 poly_int64 bitsize
;
16358 location_t loc
= EXPR_LOCATION (exp
);
16360 if (TREE_CODE (exp
) == SSA_NAME
)
16361 if (gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (exp
)))
16362 if (gimple_assign_rhs_code (def
) == ADDR_EXPR
)
16363 exp
= gimple_assign_rhs1 (def
);
16365 if (TREE_CODE (exp
) == ADDR_EXPR
)
16367 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16368 poffset
, &mode
, &unsignedp
, &reversep
,
16370 core
= build_fold_addr_expr_loc (loc
, core
);
16372 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16374 core
= TREE_OPERAND (exp
, 0);
16377 *poffset
= TREE_OPERAND (exp
, 1);
16378 if (poly_int_tree_p (*poffset
))
16380 poly_offset_int tem
16381 = wi::sext (wi::to_poly_offset (*poffset
),
16382 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16383 tem
<<= LOG2_BITS_PER_UNIT
;
16384 if (tem
.to_shwi (pbitpos
))
16385 *poffset
= NULL_TREE
;
16392 *poffset
= NULL_TREE
;
16398 /* Returns true if addresses of E1 and E2 differ by a constant, false
16399 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16402 ptr_difference_const (tree e1
, tree e2
, poly_int64_pod
*diff
)
16405 poly_int64 bitpos1
, bitpos2
;
16406 tree toffset1
, toffset2
, tdiff
, type
;
16408 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16409 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16411 poly_int64 bytepos1
, bytepos2
;
16412 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16413 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16414 || !operand_equal_p (core1
, core2
, 0))
16417 if (toffset1
&& toffset2
)
16419 type
= TREE_TYPE (toffset1
);
16420 if (type
!= TREE_TYPE (toffset2
))
16421 toffset2
= fold_convert (type
, toffset2
);
16423 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16424 if (!cst_and_fits_in_hwi (tdiff
))
16427 *diff
= int_cst_value (tdiff
);
16429 else if (toffset1
|| toffset2
)
16431 /* If only one of the offsets is non-constant, the difference cannot
16438 *diff
+= bytepos1
- bytepos2
;
16442 /* Return OFF converted to a pointer offset type suitable as offset for
16443 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16445 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16447 if (ptrofftype_p (TREE_TYPE (off
)))
16449 return fold_convert_loc (loc
, sizetype
, off
);
16452 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16454 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16456 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16457 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16460 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16462 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16464 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16465 ptr
, size_int (off
));
16468 /* Return a pointer to a NUL-terminated string containing the sequence
16469 of bytes corresponding to the representation of the object referred to
16470 by SRC (or a subsequence of such bytes within it if SRC is a reference
16471 to an initialized constant array plus some constant offset).
16472 Set *STRSIZE the number of bytes in the constant sequence including
16473 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16474 where A is the array that stores the constant sequence that SRC points
16475 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16476 need not point to a string or even an array of characters but may point
16477 to an object of any type. */
16480 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16482 /* The offset into the array A storing the string, and A's byte size. */
16490 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16492 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16496 unsigned HOST_WIDE_INT offset
= 0;
16497 if (offset_node
!= NULL_TREE
)
16499 if (!tree_fits_uhwi_p (offset_node
))
16502 offset
= tree_to_uhwi (offset_node
);
16505 if (!tree_fits_uhwi_p (mem_size
))
16508 /* ARRAY_SIZE is the byte size of the array the constant sequence
16509 is stored in and equal to sizeof A. INIT_BYTES is the number
16510 of bytes in the constant sequence used to initialize the array,
16511 including any embedded NULs as well as the terminating NUL (for
16512 strings), but not including any trailing zeros/NULs past
16513 the terminating one appended implicitly to a string literal to
16514 zero out the remainder of the array it's stored in. For example,
16516 const char a[7] = "abc\0d";
16517 n = strlen (a + 1);
16518 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16519 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16520 is equal to strlen (A) + 1. */
16521 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16522 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16523 const char *string
= TREE_STRING_POINTER (src
);
16525 /* Ideally this would turn into a gcc_checking_assert over time. */
16526 if (init_bytes
> array_size
)
16527 init_bytes
= array_size
;
16529 if (init_bytes
== 0 || offset
>= array_size
)
16534 /* Compute and store the number of characters from the beginning
16535 of the substring at OFFSET to the end, including the terminating
16536 nul. Offsets past the initial length refer to null strings. */
16537 if (offset
< init_bytes
)
16538 *strsize
= init_bytes
- offset
;
16544 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16545 /* Support only properly NUL-terminated single byte strings. */
16546 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16548 if (string
[init_bytes
- 1] != '\0')
16552 return offset
< init_bytes
? string
+ offset
: "";
16555 /* Return a pointer to a NUL-terminated string corresponding to
16556 the expression STR referencing a constant string, possibly
16557 involving a constant offset. Return null if STR either doesn't
16558 reference a constant string or if it involves a nonconstant
16562 c_getstr (tree str
)
16564 return getbyterep (str
, NULL
);
16567 /* Given a tree T, compute which bits in T may be nonzero. */
16570 tree_nonzero_bits (const_tree t
)
16572 switch (TREE_CODE (t
))
16575 return wi::to_wide (t
);
16577 return get_nonzero_bits (t
);
16578 case NON_LVALUE_EXPR
:
16580 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16582 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16583 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16586 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16587 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16589 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16590 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16592 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16593 TYPE_PRECISION (TREE_TYPE (t
)),
16594 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16596 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16598 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16599 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16600 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16601 return wi::bit_or (nzbits1
, nzbits2
);
16605 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16607 tree type
= TREE_TYPE (t
);
16608 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16609 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16610 TYPE_PRECISION (type
));
16611 return wi::neg_p (arg1
)
16612 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16613 : wi::lshift (nzbits
, arg1
);
16617 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16619 tree type
= TREE_TYPE (t
);
16620 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16621 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16622 TYPE_PRECISION (type
));
16623 return wi::neg_p (arg1
)
16624 ? wi::lshift (nzbits
, -arg1
)
16625 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16632 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16635 /* Helper function for address compare simplifications in match.pd.
16636 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16637 TYPE is the type of comparison operands.
16638 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16639 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16640 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16641 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16642 and 2 if unknown. */
16645 address_compare (tree_code code
, tree type
, tree op0
, tree op1
,
16646 tree
&base0
, tree
&base1
, poly_int64
&off0
, poly_int64
&off1
,
16649 if (TREE_CODE (op0
) == SSA_NAME
)
16650 op0
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0
));
16651 if (TREE_CODE (op1
) == SSA_NAME
)
16652 op1
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1
));
16653 gcc_checking_assert (TREE_CODE (op0
) == ADDR_EXPR
);
16654 gcc_checking_assert (TREE_CODE (op1
) == ADDR_EXPR
);
16655 base0
= get_addr_base_and_unit_offset (TREE_OPERAND (op0
, 0), &off0
);
16656 base1
= get_addr_base_and_unit_offset (TREE_OPERAND (op1
, 0), &off1
);
16657 if (base0
&& TREE_CODE (base0
) == MEM_REF
)
16659 off0
+= mem_ref_offset (base0
).force_shwi ();
16660 base0
= TREE_OPERAND (base0
, 0);
16662 if (base1
&& TREE_CODE (base1
) == MEM_REF
)
16664 off1
+= mem_ref_offset (base1
).force_shwi ();
16665 base1
= TREE_OPERAND (base1
, 0);
16667 if (base0
== NULL_TREE
|| base1
== NULL_TREE
)
16671 /* Punt in GENERIC on variables with value expressions;
16672 the value expressions might point to fields/elements
16673 of other vars etc. */
16675 && ((VAR_P (base0
) && DECL_HAS_VALUE_EXPR_P (base0
))
16676 || (VAR_P (base1
) && DECL_HAS_VALUE_EXPR_P (base1
))))
16678 else if (decl_in_symtab_p (base0
) && decl_in_symtab_p (base1
))
16680 symtab_node
*node0
= symtab_node::get_create (base0
);
16681 symtab_node
*node1
= symtab_node::get_create (base1
);
16682 equal
= node0
->equal_address_to (node1
);
16684 else if ((DECL_P (base0
)
16685 || TREE_CODE (base0
) == SSA_NAME
16686 || TREE_CODE (base0
) == STRING_CST
)
16688 || TREE_CODE (base1
) == SSA_NAME
16689 || TREE_CODE (base1
) == STRING_CST
))
16690 equal
= (base0
== base1
);
16691 /* Assume different STRING_CSTs with the same content will be
16694 && TREE_CODE (base0
) == STRING_CST
16695 && TREE_CODE (base1
) == STRING_CST
16696 && TREE_STRING_LENGTH (base0
) == TREE_STRING_LENGTH (base1
)
16697 && memcmp (TREE_STRING_POINTER (base0
), TREE_STRING_POINTER (base1
),
16698 TREE_STRING_LENGTH (base0
)) == 0)
16702 if (code
== EQ_EXPR
16704 /* If the offsets are equal we can ignore overflow. */
16705 || known_eq (off0
, off1
)
16706 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
16707 /* Or if we compare using pointers to decls or strings. */
16708 || (POINTER_TYPE_P (type
)
16709 && (DECL_P (base0
) || TREE_CODE (base0
) == STRING_CST
)))
16715 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
16718 /* At this point we know (or assume) the two pointers point at
16719 different objects. */
16720 HOST_WIDE_INT ioff0
= -1, ioff1
= -1;
16721 off0
.is_constant (&ioff0
);
16722 off1
.is_constant (&ioff1
);
16723 /* Punt on non-zero offsets from functions. */
16724 if ((TREE_CODE (base0
) == FUNCTION_DECL
&& ioff0
)
16725 || (TREE_CODE (base1
) == FUNCTION_DECL
&& ioff1
))
16727 /* Or if the bases are neither decls nor string literals. */
16728 if (!DECL_P (base0
) && TREE_CODE (base0
) != STRING_CST
)
16730 if (!DECL_P (base1
) && TREE_CODE (base1
) != STRING_CST
)
16732 /* For initializers, assume addresses of different functions are
16734 if (folding_initializer
16735 && TREE_CODE (base0
) == FUNCTION_DECL
16736 && TREE_CODE (base1
) == FUNCTION_DECL
)
16739 /* Compute whether one address points to the start of one
16740 object and another one to the end of another one. */
16741 poly_int64 size0
= 0, size1
= 0;
16742 if (TREE_CODE (base0
) == STRING_CST
)
16744 if (ioff0
< 0 || ioff0
> TREE_STRING_LENGTH (base0
))
16747 size0
= TREE_STRING_LENGTH (base0
);
16749 else if (TREE_CODE (base0
) == FUNCTION_DECL
)
16753 tree sz0
= DECL_SIZE_UNIT (base0
);
16754 if (!tree_fits_poly_int64_p (sz0
))
16757 size0
= tree_to_poly_int64 (sz0
);
16759 if (TREE_CODE (base1
) == STRING_CST
)
16761 if (ioff1
< 0 || ioff1
> TREE_STRING_LENGTH (base1
))
16764 size1
= TREE_STRING_LENGTH (base1
);
16766 else if (TREE_CODE (base1
) == FUNCTION_DECL
)
16770 tree sz1
= DECL_SIZE_UNIT (base1
);
16771 if (!tree_fits_poly_int64_p (sz1
))
16774 size1
= tree_to_poly_int64 (sz1
);
16778 /* If one offset is pointing (or could be) to the beginning of one
16779 object and the other is pointing to one past the last byte of the
16780 other object, punt. */
16781 if (maybe_eq (off0
, 0) && maybe_eq (off1
, size1
))
16783 else if (maybe_eq (off1
, 0) && maybe_eq (off0
, size0
))
16785 /* If both offsets are the same, there are some cases we know that are
16786 ok. Either if we know they aren't zero, or if we know both sizes
16789 && known_eq (off0
, off1
)
16790 && (known_ne (off0
, 0)
16791 || (known_ne (size0
, 0) && known_ne (size1
, 0))))
16795 /* At this point, equal is 2 if either one or both pointers are out of
16796 bounds of their object, or one points to start of its object and the
16797 other points to end of its object. This is unspecified behavior
16798 e.g. in C++. Otherwise equal is 0. */
16799 if (folding_cxx_constexpr
&& equal
)
16802 /* When both pointers point to string literals, even when equal is 0,
16803 due to tail merging of string literals the pointers might be the same. */
16804 if (TREE_CODE (base0
) == STRING_CST
&& TREE_CODE (base1
) == STRING_CST
)
16808 || ioff0
> TREE_STRING_LENGTH (base0
)
16809 || ioff1
> TREE_STRING_LENGTH (base1
))
16812 /* If the bytes in the string literals starting at the pointers
16813 differ, the pointers need to be different. */
16814 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
,
16815 TREE_STRING_POINTER (base1
) + ioff1
,
16816 MIN (TREE_STRING_LENGTH (base0
) - ioff0
,
16817 TREE_STRING_LENGTH (base1
) - ioff1
)) == 0)
16819 HOST_WIDE_INT ioffmin
= MIN (ioff0
, ioff1
);
16820 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
- ioffmin
,
16821 TREE_STRING_POINTER (base1
) + ioff1
- ioffmin
,
16823 /* If even the bytes in the string literal before the
16824 pointers are the same, the string literals could be
16831 if (folding_cxx_constexpr
)
16834 /* If this is a pointer comparison, ignore for now even
16835 valid equalities where one pointer is the offset zero
16836 of one object and the other to one past end of another one. */
16837 if (!INTEGRAL_TYPE_P (type
))
16840 /* Assume that string literals can't be adjacent to variables
16841 (automatic or global). */
16842 if (TREE_CODE (base0
) == STRING_CST
|| TREE_CODE (base1
) == STRING_CST
)
16845 /* Assume that automatic variables can't be adjacent to global
16847 if (is_global_var (base0
) != is_global_var (base1
))
16853 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16855 ctor_single_nonzero_element (const_tree t
)
16857 unsigned HOST_WIDE_INT idx
;
16858 constructor_elt
*ce
;
16859 tree elt
= NULL_TREE
;
16861 if (TREE_CODE (t
) != CONSTRUCTOR
)
16863 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
16864 if (!integer_zerop (ce
->value
) && !real_zerop (ce
->value
))
16875 namespace selftest
{
16877 /* Helper functions for writing tests of folding trees. */
16879 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16882 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
16885 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
16888 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16889 wrapping WRAPPED_EXPR. */
16892 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
16895 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
16896 ASSERT_NE (wrapped_expr
, result
);
16897 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
16898 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
16901 /* Verify that various arithmetic binary operations are folded
16905 test_arithmetic_folding ()
16907 tree type
= integer_type_node
;
16908 tree x
= create_tmp_var_raw (type
, "x");
16909 tree zero
= build_zero_cst (type
);
16910 tree one
= build_int_cst (type
, 1);
16913 /* 1 <-- (0 + 1) */
16914 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
16916 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
16919 /* (nonlvalue)x <-- (x + 0) */
16920 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
16924 /* 0 <-- (x - x) */
16925 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
16927 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
16930 /* Multiplication. */
16931 /* 0 <-- (x * 0) */
16932 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
16935 /* (nonlvalue)x <-- (x * 1) */
16936 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
16940 /* Verify that various binary operations on vectors are folded
16944 test_vector_folding ()
16946 tree inner_type
= integer_type_node
;
16947 tree type
= build_vector_type (inner_type
, 4);
16948 tree zero
= build_zero_cst (type
);
16949 tree one
= build_one_cst (type
);
16950 tree index
= build_index_vector (type
, 0, 1);
16952 /* Verify equality tests that return a scalar boolean result. */
16953 tree res_type
= boolean_type_node
;
16954 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
16955 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
16956 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
16957 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
16958 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
16959 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16961 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
16963 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16967 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16970 test_vec_duplicate_folding ()
16972 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
16973 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
16974 /* This will be 1 if VEC_MODE isn't a vector mode. */
16975 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
16977 tree type
= build_vector_type (ssizetype
, nunits
);
16978 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
16979 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
16980 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
16983 /* Run all of the selftests within this file. */
16986 fold_const_cc_tests ()
16988 test_arithmetic_folding ();
16989 test_vector_folding ();
16990 test_vec_duplicate_folding ();
16993 } // namespace selftest
16995 #endif /* CHECKING_P */