1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2022 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 bool simple_operand_p_2 (tree
);
134 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
135 static tree
range_predecessor (tree
);
136 static tree
range_successor (tree
);
137 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
138 static tree
fold_cond_expr_with_comparison (location_t
, tree
, enum tree_code
,
139 tree
, tree
, tree
, tree
);
140 static tree
unextend (tree
, int, int, tree
);
141 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
142 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
143 static tree
fold_binary_op_with_conditional_arg (location_t
,
144 enum tree_code
, tree
,
147 static tree
fold_negate_const (tree
, tree
);
148 static tree
fold_not_const (const_tree
, tree
);
149 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
150 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
151 static tree
fold_view_convert_expr (tree
, tree
);
152 static tree
fold_negate_expr (location_t
, tree
);
155 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
156 Otherwise, return LOC. */
159 expr_location_or (tree t
, location_t loc
)
161 location_t tloc
= EXPR_LOCATION (t
);
162 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
165 /* Similar to protected_set_expr_location, but never modify x in place,
166 if location can and needs to be set, unshare it. */
169 protected_set_expr_location_unshare (tree x
, location_t loc
)
171 if (CAN_HAVE_LOCATION_P (x
)
172 && EXPR_LOCATION (x
) != loc
173 && !(TREE_CODE (x
) == SAVE_EXPR
174 || TREE_CODE (x
) == TARGET_EXPR
175 || TREE_CODE (x
) == BIND_EXPR
))
178 SET_EXPR_LOCATION (x
, loc
);
183 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
184 division and returns the quotient. Otherwise returns
188 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
192 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
194 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
199 /* This is nonzero if we should defer warnings about undefined
200 overflow. This facility exists because these warnings are a
201 special case. The code to estimate loop iterations does not want
202 to issue any warnings, since it works with expressions which do not
203 occur in user code. Various bits of cleanup code call fold(), but
204 only use the result if it has certain characteristics (e.g., is a
205 constant); that code only wants to issue a warning if the result is
208 static int fold_deferring_overflow_warnings
;
210 /* If a warning about undefined overflow is deferred, this is the
211 warning. Note that this may cause us to turn two warnings into
212 one, but that is fine since it is sufficient to only give one
213 warning per expression. */
215 static const char* fold_deferred_overflow_warning
;
217 /* If a warning about undefined overflow is deferred, this is the
218 level at which the warning should be emitted. */
220 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
222 /* Start deferring overflow warnings. We could use a stack here to
223 permit nested calls, but at present it is not necessary. */
226 fold_defer_overflow_warnings (void)
228 ++fold_deferring_overflow_warnings
;
231 /* Stop deferring overflow warnings. If there is a pending warning,
232 and ISSUE is true, then issue the warning if appropriate. STMT is
233 the statement with which the warning should be associated (used for
234 location information); STMT may be NULL. CODE is the level of the
235 warning--a warn_strict_overflow_code value. This function will use
236 the smaller of CODE and the deferred code when deciding whether to
237 issue the warning. CODE may be zero to mean to always use the
241 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
246 gcc_assert (fold_deferring_overflow_warnings
> 0);
247 --fold_deferring_overflow_warnings
;
248 if (fold_deferring_overflow_warnings
> 0)
250 if (fold_deferred_overflow_warning
!= NULL
252 && code
< (int) fold_deferred_overflow_code
)
253 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
257 warnmsg
= fold_deferred_overflow_warning
;
258 fold_deferred_overflow_warning
= NULL
;
260 if (!issue
|| warnmsg
== NULL
)
263 if (warning_suppressed_p (stmt
, OPT_Wstrict_overflow
))
266 /* Use the smallest code level when deciding to issue the
268 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
269 code
= fold_deferred_overflow_code
;
271 if (!issue_strict_overflow_warning (code
))
275 locus
= input_location
;
277 locus
= gimple_location (stmt
);
278 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
281 /* Stop deferring overflow warnings, ignoring any deferred
285 fold_undefer_and_ignore_overflow_warnings (void)
287 fold_undefer_overflow_warnings (false, NULL
, 0);
290 /* Whether we are deferring overflow warnings. */
293 fold_deferring_overflow_warnings_p (void)
295 return fold_deferring_overflow_warnings
> 0;
298 /* This is called when we fold something based on the fact that signed
299 overflow is undefined. */
302 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
304 if (fold_deferring_overflow_warnings
> 0)
306 if (fold_deferred_overflow_warning
== NULL
307 || wc
< fold_deferred_overflow_code
)
309 fold_deferred_overflow_warning
= gmsgid
;
310 fold_deferred_overflow_code
= wc
;
313 else if (issue_strict_overflow_warning (wc
))
314 warning (OPT_Wstrict_overflow
, gmsgid
);
317 /* Return true if the built-in mathematical function specified by CODE
318 is odd, i.e. -f(x) == f(-x). */
321 negate_mathfn_p (combined_fn fn
)
344 CASE_CFN_ROUNDEVEN_FN
:
356 return !flag_rounding_math
;
364 /* Check whether we may negate an integer constant T without causing
368 may_negate_without_overflow_p (const_tree t
)
372 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
374 type
= TREE_TYPE (t
);
375 if (TYPE_UNSIGNED (type
))
378 return !wi::only_sign_bit_p (wi::to_wide (t
));
381 /* Determine whether an expression T can be cheaply negated using
382 the function negate_expr without introducing undefined overflow. */
385 negate_expr_p (tree t
)
392 type
= TREE_TYPE (t
);
395 switch (TREE_CODE (t
))
398 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
401 /* Check that -CST will not overflow type. */
402 return may_negate_without_overflow_p (t
);
404 return (INTEGRAL_TYPE_P (type
)
405 && TYPE_OVERFLOW_WRAPS (type
));
411 return !TYPE_OVERFLOW_SANITIZED (type
);
414 /* We want to canonicalize to positive real constants. Pretend
415 that only negative ones can be easily negated. */
416 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
419 return negate_expr_p (TREE_REALPART (t
))
420 && negate_expr_p (TREE_IMAGPART (t
));
424 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
427 /* Steps don't prevent negation. */
428 unsigned int count
= vector_cst_encoded_nelts (t
);
429 for (unsigned int i
= 0; i
< count
; ++i
)
430 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
437 return negate_expr_p (TREE_OPERAND (t
, 0))
438 && negate_expr_p (TREE_OPERAND (t
, 1));
441 return negate_expr_p (TREE_OPERAND (t
, 0));
444 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
)
445 || HONOR_SIGNED_ZEROS (type
)
446 || (ANY_INTEGRAL_TYPE_P (type
)
447 && ! TYPE_OVERFLOW_WRAPS (type
)))
449 /* -(A + B) -> (-B) - A. */
450 if (negate_expr_p (TREE_OPERAND (t
, 1)))
452 /* -(A + B) -> (-A) - B. */
453 return negate_expr_p (TREE_OPERAND (t
, 0));
456 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
457 return !HONOR_SIGN_DEPENDENT_ROUNDING (type
)
458 && !HONOR_SIGNED_ZEROS (type
)
459 && (! ANY_INTEGRAL_TYPE_P (type
)
460 || TYPE_OVERFLOW_WRAPS (type
));
463 if (TYPE_UNSIGNED (type
))
465 /* INT_MIN/n * n doesn't overflow while negating one operand it does
466 if n is a (negative) power of two. */
467 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
468 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
469 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
471 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
472 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
474 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
480 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t
))
481 return negate_expr_p (TREE_OPERAND (t
, 1))
482 || negate_expr_p (TREE_OPERAND (t
, 0));
488 if (TYPE_UNSIGNED (type
))
490 /* In general we can't negate A in A / B, because if A is INT_MIN and
491 B is not 1 we change the sign of the result. */
492 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
493 && negate_expr_p (TREE_OPERAND (t
, 0)))
495 /* In general we can't negate B in A / B, because if A is INT_MIN and
496 B is 1, we may turn this into INT_MIN / -1 which is undefined
497 and actually traps on some architectures. */
498 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
499 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
500 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
501 && ! integer_onep (TREE_OPERAND (t
, 1))))
502 return negate_expr_p (TREE_OPERAND (t
, 1));
506 /* Negate -((double)float) as (double)(-float). */
507 if (TREE_CODE (type
) == REAL_TYPE
)
509 tree tem
= strip_float_extensions (t
);
511 return negate_expr_p (tem
);
516 /* Negate -f(x) as f(-x). */
517 if (negate_mathfn_p (get_call_combined_fn (t
)))
518 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
522 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
523 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
525 tree op1
= TREE_OPERAND (t
, 1);
526 if (wi::to_wide (op1
) == element_precision (type
) - 1)
537 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
538 simplification is possible.
539 If negate_expr_p would return true for T, NULL_TREE will never be
543 fold_negate_expr_1 (location_t loc
, tree t
)
545 tree type
= TREE_TYPE (t
);
548 switch (TREE_CODE (t
))
550 /* Convert - (~A) to A + 1. */
552 if (INTEGRAL_TYPE_P (type
))
553 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
554 build_one_cst (type
));
558 tem
= fold_negate_const (t
, type
);
559 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
560 || (ANY_INTEGRAL_TYPE_P (type
)
561 && !TYPE_OVERFLOW_TRAPS (type
)
562 && TYPE_OVERFLOW_WRAPS (type
))
563 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
570 tem
= fold_negate_const (t
, type
);
575 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
576 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
578 return build_complex (type
, rpart
, ipart
);
584 tree_vector_builder elts
;
585 elts
.new_unary_operation (type
, t
, true);
586 unsigned int count
= elts
.encoded_nelts ();
587 for (unsigned int i
= 0; i
< count
; ++i
)
589 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
590 if (elt
== NULL_TREE
)
592 elts
.quick_push (elt
);
595 return elts
.build ();
599 if (negate_expr_p (t
))
600 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
601 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
602 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
606 if (negate_expr_p (t
))
607 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
608 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
612 if (!TYPE_OVERFLOW_SANITIZED (type
))
613 return TREE_OPERAND (t
, 0);
617 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
618 && !HONOR_SIGNED_ZEROS (type
))
620 /* -(A + B) -> (-B) - A. */
621 if (negate_expr_p (TREE_OPERAND (t
, 1)))
623 tem
= negate_expr (TREE_OPERAND (t
, 1));
624 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
625 tem
, TREE_OPERAND (t
, 0));
628 /* -(A + B) -> (-A) - B. */
629 if (negate_expr_p (TREE_OPERAND (t
, 0)))
631 tem
= negate_expr (TREE_OPERAND (t
, 0));
632 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
633 tem
, TREE_OPERAND (t
, 1));
639 /* - (A - B) -> B - A */
640 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
641 && !HONOR_SIGNED_ZEROS (type
))
642 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
643 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
647 if (TYPE_UNSIGNED (type
))
653 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
655 tem
= TREE_OPERAND (t
, 1);
656 if (negate_expr_p (tem
))
657 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
658 TREE_OPERAND (t
, 0), negate_expr (tem
));
659 tem
= TREE_OPERAND (t
, 0);
660 if (negate_expr_p (tem
))
661 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
662 negate_expr (tem
), TREE_OPERAND (t
, 1));
669 if (TYPE_UNSIGNED (type
))
671 /* In general we can't negate A in A / B, because if A is INT_MIN and
672 B is not 1 we change the sign of the result. */
673 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
674 && negate_expr_p (TREE_OPERAND (t
, 0)))
675 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
676 negate_expr (TREE_OPERAND (t
, 0)),
677 TREE_OPERAND (t
, 1));
678 /* In general we can't negate B in A / B, because if A is INT_MIN and
679 B is 1, we may turn this into INT_MIN / -1 which is undefined
680 and actually traps on some architectures. */
681 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
682 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
683 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
684 && ! integer_onep (TREE_OPERAND (t
, 1))))
685 && negate_expr_p (TREE_OPERAND (t
, 1)))
686 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
688 negate_expr (TREE_OPERAND (t
, 1)));
692 /* Convert -((double)float) into (double)(-float). */
693 if (TREE_CODE (type
) == REAL_TYPE
)
695 tem
= strip_float_extensions (t
);
696 if (tem
!= t
&& negate_expr_p (tem
))
697 return fold_convert_loc (loc
, type
, negate_expr (tem
));
702 /* Negate -f(x) as f(-x). */
703 if (negate_mathfn_p (get_call_combined_fn (t
))
704 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
708 fndecl
= get_callee_fndecl (t
);
709 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
710 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
715 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
716 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
718 tree op1
= TREE_OPERAND (t
, 1);
719 if (wi::to_wide (op1
) == element_precision (type
) - 1)
721 tree ntype
= TYPE_UNSIGNED (type
)
722 ? signed_type_for (type
)
723 : unsigned_type_for (type
);
724 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
725 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
726 return fold_convert_loc (loc
, type
, temp
);
738 /* A wrapper for fold_negate_expr_1. */
741 fold_negate_expr (location_t loc
, tree t
)
743 tree type
= TREE_TYPE (t
);
745 tree tem
= fold_negate_expr_1 (loc
, t
);
746 if (tem
== NULL_TREE
)
748 return fold_convert_loc (loc
, type
, tem
);
751 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
752 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
764 loc
= EXPR_LOCATION (t
);
765 type
= TREE_TYPE (t
);
768 tem
= fold_negate_expr (loc
, t
);
770 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
771 return fold_convert_loc (loc
, type
, tem
);
774 /* Split a tree IN into a constant, literal and variable parts that could be
775 combined with CODE to make IN. "constant" means an expression with
776 TREE_CONSTANT but that isn't an actual constant. CODE must be a
777 commutative arithmetic operation. Store the constant part into *CONP,
778 the literal in *LITP and return the variable part. If a part isn't
779 present, set it to null. If the tree does not decompose in this way,
780 return the entire tree as the variable part and the other parts as null.
782 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
783 case, we negate an operand that was subtracted. Except if it is a
784 literal for which we use *MINUS_LITP instead.
786 If NEGATE_P is true, we are negating all of IN, again except a literal
787 for which we use *MINUS_LITP instead. If a variable part is of pointer
788 type, it is negated after converting to TYPE. This prevents us from
789 generating illegal MINUS pointer expression. LOC is the location of
790 the converted variable part.
792 If IN is itself a literal or constant, return it as appropriate.
794 Note that we do not guarantee that any of the three values will be the
795 same type as IN, but they will have the same signedness and mode. */
798 split_tree (tree in
, tree type
, enum tree_code code
,
799 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
800 tree
*litp
, tree
*minus_litp
, int negate_p
)
809 /* Strip any conversions that don't change the machine mode or signedness. */
810 STRIP_SIGN_NOPS (in
);
812 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
813 || TREE_CODE (in
) == FIXED_CST
)
815 else if (TREE_CODE (in
) == code
816 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
817 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
818 /* We can associate addition and subtraction together (even
819 though the C standard doesn't say so) for integers because
820 the value is not affected. For reals, the value might be
821 affected, so we can't. */
822 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
823 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
824 || (code
== MINUS_EXPR
825 && (TREE_CODE (in
) == PLUS_EXPR
826 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
828 tree op0
= TREE_OPERAND (in
, 0);
829 tree op1
= TREE_OPERAND (in
, 1);
830 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
831 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
833 /* First see if either of the operands is a literal, then a constant. */
834 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
835 || TREE_CODE (op0
) == FIXED_CST
)
836 *litp
= op0
, op0
= 0;
837 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
838 || TREE_CODE (op1
) == FIXED_CST
)
839 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
841 if (op0
!= 0 && TREE_CONSTANT (op0
))
842 *conp
= op0
, op0
= 0;
843 else if (op1
!= 0 && TREE_CONSTANT (op1
))
844 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
846 /* If we haven't dealt with either operand, this is not a case we can
847 decompose. Otherwise, VAR is either of the ones remaining, if any. */
848 if (op0
!= 0 && op1
!= 0)
853 var
= op1
, neg_var_p
= neg1_p
;
855 /* Now do any needed negations. */
857 *minus_litp
= *litp
, *litp
= 0;
858 if (neg_conp_p
&& *conp
)
859 *minus_conp
= *conp
, *conp
= 0;
860 if (neg_var_p
&& var
)
861 *minus_varp
= var
, var
= 0;
863 else if (TREE_CONSTANT (in
))
865 else if (TREE_CODE (in
) == BIT_NOT_EXPR
866 && code
== PLUS_EXPR
)
868 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
869 when IN is constant. */
870 *litp
= build_minus_one_cst (type
);
871 *minus_varp
= TREE_OPERAND (in
, 0);
879 *minus_litp
= *litp
, *litp
= 0;
880 else if (*minus_litp
)
881 *litp
= *minus_litp
, *minus_litp
= 0;
883 *minus_conp
= *conp
, *conp
= 0;
884 else if (*minus_conp
)
885 *conp
= *minus_conp
, *minus_conp
= 0;
887 *minus_varp
= var
, var
= 0;
888 else if (*minus_varp
)
889 var
= *minus_varp
, *minus_varp
= 0;
893 && TREE_OVERFLOW_P (*litp
))
894 *litp
= drop_tree_overflow (*litp
);
896 && TREE_OVERFLOW_P (*minus_litp
))
897 *minus_litp
= drop_tree_overflow (*minus_litp
);
902 /* Re-associate trees split by the above function. T1 and T2 are
903 either expressions to associate or null. Return the new
904 expression, if any. LOC is the location of the new expression. If
905 we build an operation, do it in TYPE and with CODE. */
908 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
912 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
918 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
919 try to fold this since we will have infinite recursion. But do
920 deal with any NEGATE_EXPRs. */
921 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
922 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
923 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
925 if (code
== PLUS_EXPR
)
927 if (TREE_CODE (t1
) == NEGATE_EXPR
)
928 return build2_loc (loc
, MINUS_EXPR
, type
,
929 fold_convert_loc (loc
, type
, t2
),
930 fold_convert_loc (loc
, type
,
931 TREE_OPERAND (t1
, 0)));
932 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
933 return build2_loc (loc
, MINUS_EXPR
, type
,
934 fold_convert_loc (loc
, type
, t1
),
935 fold_convert_loc (loc
, type
,
936 TREE_OPERAND (t2
, 0)));
937 else if (integer_zerop (t2
))
938 return fold_convert_loc (loc
, type
, t1
);
940 else if (code
== MINUS_EXPR
)
942 if (integer_zerop (t2
))
943 return fold_convert_loc (loc
, type
, t1
);
946 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
947 fold_convert_loc (loc
, type
, t2
));
950 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
951 fold_convert_loc (loc
, type
, t2
));
954 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
955 for use in int_const_binop, size_binop and size_diffop. */
958 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
960 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
962 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
977 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
978 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
979 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
982 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
983 a new constant in RES. Return FALSE if we don't know how to
984 evaluate CODE at compile-time. */
987 wide_int_binop (wide_int
&res
,
988 enum tree_code code
, const wide_int
&arg1
, const wide_int
&arg2
,
989 signop sign
, wi::overflow_type
*overflow
)
992 *overflow
= wi::OVF_NONE
;
996 res
= wi::bit_or (arg1
, arg2
);
1000 res
= wi::bit_xor (arg1
, arg2
);
1004 res
= wi::bit_and (arg1
, arg2
);
1008 if (wi::neg_p (arg2
))
1010 res
= wi::lshift (arg1
, arg2
);
1014 if (wi::neg_p (arg2
))
1016 /* It's unclear from the C standard whether shifts can overflow.
1017 The following code ignores overflow; perhaps a C standard
1018 interpretation ruling is needed. */
1019 res
= wi::rshift (arg1
, arg2
, sign
);
1024 if (wi::neg_p (arg2
))
1027 if (code
== RROTATE_EXPR
)
1028 code
= LROTATE_EXPR
;
1030 code
= RROTATE_EXPR
;
1035 if (code
== RROTATE_EXPR
)
1036 res
= wi::rrotate (arg1
, tmp
);
1038 res
= wi::lrotate (arg1
, tmp
);
1042 res
= wi::add (arg1
, arg2
, sign
, overflow
);
1046 res
= wi::sub (arg1
, arg2
, sign
, overflow
);
1050 res
= wi::mul (arg1
, arg2
, sign
, overflow
);
1053 case MULT_HIGHPART_EXPR
:
1054 res
= wi::mul_high (arg1
, arg2
, sign
);
1057 case TRUNC_DIV_EXPR
:
1058 case EXACT_DIV_EXPR
:
1061 res
= wi::div_trunc (arg1
, arg2
, sign
, overflow
);
1064 case FLOOR_DIV_EXPR
:
1067 res
= wi::div_floor (arg1
, arg2
, sign
, overflow
);
1073 res
= wi::div_ceil (arg1
, arg2
, sign
, overflow
);
1076 case ROUND_DIV_EXPR
:
1079 res
= wi::div_round (arg1
, arg2
, sign
, overflow
);
1082 case TRUNC_MOD_EXPR
:
1085 res
= wi::mod_trunc (arg1
, arg2
, sign
, overflow
);
1088 case FLOOR_MOD_EXPR
:
1091 res
= wi::mod_floor (arg1
, arg2
, sign
, overflow
);
1097 res
= wi::mod_ceil (arg1
, arg2
, sign
, overflow
);
1100 case ROUND_MOD_EXPR
:
1103 res
= wi::mod_round (arg1
, arg2
, sign
, overflow
);
1107 res
= wi::min (arg1
, arg2
, sign
);
1111 res
= wi::max (arg1
, arg2
, sign
);
1120 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1121 produce a new constant in RES. Return FALSE if we don't know how
1122 to evaluate CODE at compile-time. */
1125 poly_int_binop (poly_wide_int
&res
, enum tree_code code
,
1126 const_tree arg1
, const_tree arg2
,
1127 signop sign
, wi::overflow_type
*overflow
)
1129 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1130 gcc_assert (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
));
1134 res
= wi::add (wi::to_poly_wide (arg1
),
1135 wi::to_poly_wide (arg2
), sign
, overflow
);
1139 res
= wi::sub (wi::to_poly_wide (arg1
),
1140 wi::to_poly_wide (arg2
), sign
, overflow
);
1144 if (TREE_CODE (arg2
) == INTEGER_CST
)
1145 res
= wi::mul (wi::to_poly_wide (arg1
),
1146 wi::to_wide (arg2
), sign
, overflow
);
1147 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1148 res
= wi::mul (wi::to_poly_wide (arg2
),
1149 wi::to_wide (arg1
), sign
, overflow
);
1155 if (TREE_CODE (arg2
) == INTEGER_CST
)
1156 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1162 if (TREE_CODE (arg2
) != INTEGER_CST
1163 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1174 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1175 produce a new constant. Return NULL_TREE if we don't know how to
1176 evaluate CODE at compile-time. */
1179 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1182 poly_wide_int poly_res
;
1183 tree type
= TREE_TYPE (arg1
);
1184 signop sign
= TYPE_SIGN (type
);
1185 wi::overflow_type overflow
= wi::OVF_NONE
;
1187 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1189 wide_int warg1
= wi::to_wide (arg1
), res
;
1190 wide_int warg2
= wi::to_wide (arg2
, TYPE_PRECISION (type
));
1191 if (!wide_int_binop (res
, code
, warg1
, warg2
, sign
, &overflow
))
1195 else if (!poly_int_tree_p (arg1
)
1196 || !poly_int_tree_p (arg2
)
1197 || !poly_int_binop (poly_res
, code
, arg1
, arg2
, sign
, &overflow
))
1199 return force_fit_type (type
, poly_res
, overflowable
,
1200 (((sign
== SIGNED
|| overflowable
== -1)
1202 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1205 /* Return true if binary operation OP distributes over addition in operand
1206 OPNO, with the other operand being held constant. OPNO counts from 1. */
1209 distributes_over_addition_p (tree_code op
, int opno
)
1226 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1227 constant. We assume ARG1 and ARG2 have the same data type, or at least
1228 are the same kind of constant and the same machine mode. Return zero if
1229 combining the constants is not allowed in the current operating mode. */
1232 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1234 /* Sanity check for the recursive cases. */
1241 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1243 if (code
== POINTER_PLUS_EXPR
)
1244 return int_const_binop (PLUS_EXPR
,
1245 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1247 return int_const_binop (code
, arg1
, arg2
);
1250 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1255 REAL_VALUE_TYPE value
;
1256 REAL_VALUE_TYPE result
;
1260 /* The following codes are handled by real_arithmetic. */
1275 d1
= TREE_REAL_CST (arg1
);
1276 d2
= TREE_REAL_CST (arg2
);
1278 type
= TREE_TYPE (arg1
);
1279 mode
= TYPE_MODE (type
);
1281 /* Don't perform operation if we honor signaling NaNs and
1282 either operand is a signaling NaN. */
1283 if (HONOR_SNANS (mode
)
1284 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1285 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1288 /* Don't perform operation if it would raise a division
1289 by zero exception. */
1290 if (code
== RDIV_EXPR
1291 && real_equal (&d2
, &dconst0
)
1292 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1295 /* If either operand is a NaN, just return it. Otherwise, set up
1296 for floating-point trap; we return an overflow. */
1297 if (REAL_VALUE_ISNAN (d1
))
1299 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1302 t
= build_real (type
, d1
);
1305 else if (REAL_VALUE_ISNAN (d2
))
1307 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1310 t
= build_real (type
, d2
);
1314 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1315 real_convert (&result
, mode
, &value
);
1317 /* Don't constant fold this floating point operation if
1318 both operands are not NaN but the result is NaN, and
1319 flag_trapping_math. Such operations should raise an
1320 invalid operation exception. */
1321 if (flag_trapping_math
1322 && MODE_HAS_NANS (mode
)
1323 && REAL_VALUE_ISNAN (result
)
1324 && !REAL_VALUE_ISNAN (d1
)
1325 && !REAL_VALUE_ISNAN (d2
))
1328 /* Don't constant fold this floating point operation if
1329 the result has overflowed and flag_trapping_math. */
1330 if (flag_trapping_math
1331 && MODE_HAS_INFINITIES (mode
)
1332 && REAL_VALUE_ISINF (result
)
1333 && !REAL_VALUE_ISINF (d1
)
1334 && !REAL_VALUE_ISINF (d2
))
1337 /* Don't constant fold this floating point operation if the
1338 result may dependent upon the run-time rounding mode and
1339 flag_rounding_math is set, or if GCC's software emulation
1340 is unable to accurately represent the result. */
1341 if ((flag_rounding_math
1342 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1343 && (inexact
|| !real_identical (&result
, &value
)))
1346 t
= build_real (type
, result
);
1348 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1352 if (TREE_CODE (arg1
) == FIXED_CST
)
1354 FIXED_VALUE_TYPE f1
;
1355 FIXED_VALUE_TYPE f2
;
1356 FIXED_VALUE_TYPE result
;
1361 /* The following codes are handled by fixed_arithmetic. */
1367 case TRUNC_DIV_EXPR
:
1368 if (TREE_CODE (arg2
) != FIXED_CST
)
1370 f2
= TREE_FIXED_CST (arg2
);
1376 if (TREE_CODE (arg2
) != INTEGER_CST
)
1378 wi::tree_to_wide_ref w2
= wi::to_wide (arg2
);
1379 f2
.data
.high
= w2
.elt (1);
1380 f2
.data
.low
= w2
.ulow ();
1389 f1
= TREE_FIXED_CST (arg1
);
1390 type
= TREE_TYPE (arg1
);
1391 sat_p
= TYPE_SATURATING (type
);
1392 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1393 t
= build_fixed (type
, result
);
1394 /* Propagate overflow flags. */
1395 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1396 TREE_OVERFLOW (t
) = 1;
1400 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1402 tree type
= TREE_TYPE (arg1
);
1403 tree r1
= TREE_REALPART (arg1
);
1404 tree i1
= TREE_IMAGPART (arg1
);
1405 tree r2
= TREE_REALPART (arg2
);
1406 tree i2
= TREE_IMAGPART (arg2
);
1413 real
= const_binop (code
, r1
, r2
);
1414 imag
= const_binop (code
, i1
, i2
);
1418 if (COMPLEX_FLOAT_TYPE_P (type
))
1419 return do_mpc_arg2 (arg1
, arg2
, type
,
1420 /* do_nonfinite= */ folding_initializer
,
1423 real
= const_binop (MINUS_EXPR
,
1424 const_binop (MULT_EXPR
, r1
, r2
),
1425 const_binop (MULT_EXPR
, i1
, i2
));
1426 imag
= const_binop (PLUS_EXPR
,
1427 const_binop (MULT_EXPR
, r1
, i2
),
1428 const_binop (MULT_EXPR
, i1
, r2
));
1432 if (COMPLEX_FLOAT_TYPE_P (type
))
1433 return do_mpc_arg2 (arg1
, arg2
, type
,
1434 /* do_nonfinite= */ folding_initializer
,
1437 case TRUNC_DIV_EXPR
:
1439 case FLOOR_DIV_EXPR
:
1440 case ROUND_DIV_EXPR
:
1441 if (flag_complex_method
== 0)
1443 /* Keep this algorithm in sync with
1444 tree-complex.cc:expand_complex_div_straight().
1446 Expand complex division to scalars, straightforward algorithm.
1447 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1451 = const_binop (PLUS_EXPR
,
1452 const_binop (MULT_EXPR
, r2
, r2
),
1453 const_binop (MULT_EXPR
, i2
, i2
));
1455 = const_binop (PLUS_EXPR
,
1456 const_binop (MULT_EXPR
, r1
, r2
),
1457 const_binop (MULT_EXPR
, i1
, i2
));
1459 = const_binop (MINUS_EXPR
,
1460 const_binop (MULT_EXPR
, i1
, r2
),
1461 const_binop (MULT_EXPR
, r1
, i2
));
1463 real
= const_binop (code
, t1
, magsquared
);
1464 imag
= const_binop (code
, t2
, magsquared
);
1468 /* Keep this algorithm in sync with
1469 tree-complex.cc:expand_complex_div_wide().
1471 Expand complex division to scalars, modified algorithm to minimize
1472 overflow with wide input ranges. */
1473 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1474 fold_abs_const (r2
, TREE_TYPE (type
)),
1475 fold_abs_const (i2
, TREE_TYPE (type
)));
1477 if (integer_nonzerop (compare
))
1479 /* In the TRUE branch, we compute
1481 div = (br * ratio) + bi;
1482 tr = (ar * ratio) + ai;
1483 ti = (ai * ratio) - ar;
1486 tree ratio
= const_binop (code
, r2
, i2
);
1487 tree div
= const_binop (PLUS_EXPR
, i2
,
1488 const_binop (MULT_EXPR
, r2
, ratio
));
1489 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1490 real
= const_binop (PLUS_EXPR
, real
, i1
);
1491 real
= const_binop (code
, real
, div
);
1493 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1494 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1495 imag
= const_binop (code
, imag
, div
);
1499 /* In the FALSE branch, we compute
1501 divisor = (d * ratio) + c;
1502 tr = (b * ratio) + a;
1503 ti = b - (a * ratio);
1506 tree ratio
= const_binop (code
, i2
, r2
);
1507 tree div
= const_binop (PLUS_EXPR
, r2
,
1508 const_binop (MULT_EXPR
, i2
, ratio
));
1510 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1511 real
= const_binop (PLUS_EXPR
, real
, r1
);
1512 real
= const_binop (code
, real
, div
);
1514 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1515 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1516 imag
= const_binop (code
, imag
, div
);
1526 return build_complex (type
, real
, imag
);
1529 if (TREE_CODE (arg1
) == VECTOR_CST
1530 && TREE_CODE (arg2
) == VECTOR_CST
1531 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)),
1532 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
))))
1534 tree type
= TREE_TYPE (arg1
);
1536 if (VECTOR_CST_STEPPED_P (arg1
)
1537 && VECTOR_CST_STEPPED_P (arg2
))
1538 /* We can operate directly on the encoding if:
1540 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1542 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1544 Addition and subtraction are the supported operators
1545 for which this is true. */
1546 step_ok_p
= (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
1547 else if (VECTOR_CST_STEPPED_P (arg1
))
1548 /* We can operate directly on stepped encodings if:
1552 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1554 which is true if (x -> x op c) distributes over addition. */
1555 step_ok_p
= distributes_over_addition_p (code
, 1);
1557 /* Similarly in reverse. */
1558 step_ok_p
= distributes_over_addition_p (code
, 2);
1559 tree_vector_builder elts
;
1560 if (!elts
.new_binary_operation (type
, arg1
, arg2
, step_ok_p
))
1562 unsigned int count
= elts
.encoded_nelts ();
1563 for (unsigned int i
= 0; i
< count
; ++i
)
1565 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1566 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1568 tree elt
= const_binop (code
, elem1
, elem2
);
1570 /* It is possible that const_binop cannot handle the given
1571 code and return NULL_TREE */
1572 if (elt
== NULL_TREE
)
1574 elts
.quick_push (elt
);
1577 return elts
.build ();
1580 /* Shifts allow a scalar offset for a vector. */
1581 if (TREE_CODE (arg1
) == VECTOR_CST
1582 && TREE_CODE (arg2
) == INTEGER_CST
)
1584 tree type
= TREE_TYPE (arg1
);
1585 bool step_ok_p
= distributes_over_addition_p (code
, 1);
1586 tree_vector_builder elts
;
1587 if (!elts
.new_unary_operation (type
, arg1
, step_ok_p
))
1589 unsigned int count
= elts
.encoded_nelts ();
1590 for (unsigned int i
= 0; i
< count
; ++i
)
1592 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1594 tree elt
= const_binop (code
, elem1
, arg2
);
1596 /* It is possible that const_binop cannot handle the given
1597 code and return NULL_TREE. */
1598 if (elt
== NULL_TREE
)
1600 elts
.quick_push (elt
);
1603 return elts
.build ();
1608 /* Overload that adds a TYPE parameter to be able to dispatch
1609 to fold_relational_const. */
1612 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1614 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1615 return fold_relational_const (code
, type
, arg1
, arg2
);
1617 /* ??? Until we make the const_binop worker take the type of the
1618 result as argument put those cases that need it here. */
1621 case VEC_SERIES_EXPR
:
1622 if (CONSTANT_CLASS_P (arg1
)
1623 && CONSTANT_CLASS_P (arg2
))
1624 return build_vec_series (type
, arg1
, arg2
);
1628 if ((TREE_CODE (arg1
) == REAL_CST
1629 && TREE_CODE (arg2
) == REAL_CST
)
1630 || (TREE_CODE (arg1
) == INTEGER_CST
1631 && TREE_CODE (arg2
) == INTEGER_CST
))
1632 return build_complex (type
, arg1
, arg2
);
1635 case POINTER_DIFF_EXPR
:
1636 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1638 poly_offset_int res
= (wi::to_poly_offset (arg1
)
1639 - wi::to_poly_offset (arg2
));
1640 return force_fit_type (type
, res
, 1,
1641 TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1645 case VEC_PACK_TRUNC_EXPR
:
1646 case VEC_PACK_FIX_TRUNC_EXPR
:
1647 case VEC_PACK_FLOAT_EXPR
:
1649 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1651 if (TREE_CODE (arg1
) != VECTOR_CST
1652 || TREE_CODE (arg2
) != VECTOR_CST
)
1655 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1658 out_nelts
= in_nelts
* 2;
1659 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1660 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1662 tree_vector_builder
elts (type
, out_nelts
, 1);
1663 for (i
= 0; i
< out_nelts
; i
++)
1665 tree elt
= (i
< in_nelts
1666 ? VECTOR_CST_ELT (arg1
, i
)
1667 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1668 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1670 : code
== VEC_PACK_FLOAT_EXPR
1671 ? FLOAT_EXPR
: FIX_TRUNC_EXPR
,
1672 TREE_TYPE (type
), elt
);
1673 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1675 elts
.quick_push (elt
);
1678 return elts
.build ();
1681 case VEC_WIDEN_MULT_LO_EXPR
:
1682 case VEC_WIDEN_MULT_HI_EXPR
:
1683 case VEC_WIDEN_MULT_EVEN_EXPR
:
1684 case VEC_WIDEN_MULT_ODD_EXPR
:
1686 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1688 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1691 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1693 out_nelts
= in_nelts
/ 2;
1694 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1695 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1697 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1698 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1699 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1700 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1701 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1703 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1706 tree_vector_builder
elts (type
, out_nelts
, 1);
1707 for (out
= 0; out
< out_nelts
; out
++)
1709 unsigned int in
= (out
<< scale
) + ofs
;
1710 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1711 VECTOR_CST_ELT (arg1
, in
));
1712 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1713 VECTOR_CST_ELT (arg2
, in
));
1715 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1717 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1718 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1720 elts
.quick_push (elt
);
1723 return elts
.build ();
1729 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1732 /* Make sure type and arg0 have the same saturating flag. */
1733 gcc_checking_assert (TYPE_SATURATING (type
)
1734 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1736 return const_binop (code
, arg1
, arg2
);
1739 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1740 Return zero if computing the constants is not possible. */
1743 const_unop (enum tree_code code
, tree type
, tree arg0
)
1745 /* Don't perform the operation, other than NEGATE and ABS, if
1746 flag_signaling_nans is on and the operand is a signaling NaN. */
1747 if (TREE_CODE (arg0
) == REAL_CST
1748 && HONOR_SNANS (arg0
)
1749 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1750 && code
!= NEGATE_EXPR
1752 && code
!= ABSU_EXPR
)
1759 case FIX_TRUNC_EXPR
:
1760 case FIXED_CONVERT_EXPR
:
1761 return fold_convert_const (code
, type
, arg0
);
1763 case ADDR_SPACE_CONVERT_EXPR
:
1764 /* If the source address is 0, and the source address space
1765 cannot have a valid object at 0, fold to dest type null. */
1766 if (integer_zerop (arg0
)
1767 && !(targetm
.addr_space
.zero_address_valid
1768 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1769 return fold_convert_const (code
, type
, arg0
);
1772 case VIEW_CONVERT_EXPR
:
1773 return fold_view_convert_expr (type
, arg0
);
1777 /* Can't call fold_negate_const directly here as that doesn't
1778 handle all cases and we might not be able to negate some
1780 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1781 if (tem
&& CONSTANT_CLASS_P (tem
))
1788 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1789 return fold_abs_const (arg0
, type
);
1793 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1795 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1797 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1802 if (TREE_CODE (arg0
) == INTEGER_CST
)
1803 return fold_not_const (arg0
, type
);
1804 else if (POLY_INT_CST_P (arg0
))
1805 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1806 /* Perform BIT_NOT_EXPR on each element individually. */
1807 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1811 /* This can cope with stepped encodings because ~x == -1 - x. */
1812 tree_vector_builder elements
;
1813 elements
.new_unary_operation (type
, arg0
, true);
1814 unsigned int i
, count
= elements
.encoded_nelts ();
1815 for (i
= 0; i
< count
; ++i
)
1817 elem
= VECTOR_CST_ELT (arg0
, i
);
1818 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1819 if (elem
== NULL_TREE
)
1821 elements
.quick_push (elem
);
1824 return elements
.build ();
1828 case TRUTH_NOT_EXPR
:
1829 if (TREE_CODE (arg0
) == INTEGER_CST
)
1830 return constant_boolean_node (integer_zerop (arg0
), type
);
1834 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1835 return fold_convert (type
, TREE_REALPART (arg0
));
1839 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1840 return fold_convert (type
, TREE_IMAGPART (arg0
));
1843 case VEC_UNPACK_LO_EXPR
:
1844 case VEC_UNPACK_HI_EXPR
:
1845 case VEC_UNPACK_FLOAT_LO_EXPR
:
1846 case VEC_UNPACK_FLOAT_HI_EXPR
:
1847 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
1848 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
1850 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1851 enum tree_code subcode
;
1853 if (TREE_CODE (arg0
) != VECTOR_CST
)
1856 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1858 out_nelts
= in_nelts
/ 2;
1859 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1861 unsigned int offset
= 0;
1862 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1863 || code
== VEC_UNPACK_FLOAT_LO_EXPR
1864 || code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
))
1867 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1869 else if (code
== VEC_UNPACK_FLOAT_LO_EXPR
1870 || code
== VEC_UNPACK_FLOAT_HI_EXPR
)
1871 subcode
= FLOAT_EXPR
;
1873 subcode
= FIX_TRUNC_EXPR
;
1875 tree_vector_builder
elts (type
, out_nelts
, 1);
1876 for (i
= 0; i
< out_nelts
; i
++)
1878 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1879 VECTOR_CST_ELT (arg0
, i
+ offset
));
1880 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1882 elts
.quick_push (elt
);
1885 return elts
.build ();
1888 case VEC_DUPLICATE_EXPR
:
1889 if (CONSTANT_CLASS_P (arg0
))
1890 return build_vector_from_val (type
, arg0
);
1900 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1901 indicates which particular sizetype to create. */
1904 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
1906 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1909 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1910 is a tree code. The type of the result is taken from the operands.
1911 Both must be equivalent integer types, ala int_binop_types_match_p.
1912 If the operands are constant, so is the result. */
1915 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1917 tree type
= TREE_TYPE (arg0
);
1919 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1920 return error_mark_node
;
1922 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1925 /* Handle the special case of two poly_int constants faster. */
1926 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
1928 /* And some specific cases even faster than that. */
1929 if (code
== PLUS_EXPR
)
1931 if (integer_zerop (arg0
)
1932 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1934 if (integer_zerop (arg1
)
1935 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1938 else if (code
== MINUS_EXPR
)
1940 if (integer_zerop (arg1
)
1941 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1944 else if (code
== MULT_EXPR
)
1946 if (integer_onep (arg0
)
1947 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1951 /* Handle general case of two integer constants. For sizetype
1952 constant calculations we always want to know about overflow,
1953 even in the unsigned case. */
1954 tree res
= int_const_binop (code
, arg0
, arg1
, -1);
1955 if (res
!= NULL_TREE
)
1959 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1962 /* Given two values, either both of sizetype or both of bitsizetype,
1963 compute the difference between the two values. Return the value
1964 in signed type corresponding to the type of the operands. */
1967 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1969 tree type
= TREE_TYPE (arg0
);
1972 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1975 /* If the type is already signed, just do the simple thing. */
1976 if (!TYPE_UNSIGNED (type
))
1977 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1979 if (type
== sizetype
)
1981 else if (type
== bitsizetype
)
1982 ctype
= sbitsizetype
;
1984 ctype
= signed_type_for (type
);
1986 /* If either operand is not a constant, do the conversions to the signed
1987 type and subtract. The hardware will do the right thing with any
1988 overflow in the subtraction. */
1989 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1990 return size_binop_loc (loc
, MINUS_EXPR
,
1991 fold_convert_loc (loc
, ctype
, arg0
),
1992 fold_convert_loc (loc
, ctype
, arg1
));
1994 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1995 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1996 overflow) and negate (which can't either). Special-case a result
1997 of zero while we're here. */
1998 if (tree_int_cst_equal (arg0
, arg1
))
1999 return build_int_cst (ctype
, 0);
2000 else if (tree_int_cst_lt (arg1
, arg0
))
2001 return fold_convert_loc (loc
, ctype
,
2002 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
2004 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
2005 fold_convert_loc (loc
, ctype
,
2006 size_binop_loc (loc
,
2011 /* A subroutine of fold_convert_const handling conversions of an
2012 INTEGER_CST to another integer type. */
2015 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
2017 /* Given an integer constant, make new constant with new type,
2018 appropriately sign-extended or truncated. Use widest_int
2019 so that any extension is done according ARG1's type. */
2020 return force_fit_type (type
, wi::to_widest (arg1
),
2021 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
2022 TREE_OVERFLOW (arg1
));
2025 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2026 to an integer type. */
2029 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
2031 bool overflow
= false;
2034 /* The following code implements the floating point to integer
2035 conversion rules required by the Java Language Specification,
2036 that IEEE NaNs are mapped to zero and values that overflow
2037 the target precision saturate, i.e. values greater than
2038 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2039 are mapped to INT_MIN. These semantics are allowed by the
2040 C and C++ standards that simply state that the behavior of
2041 FP-to-integer conversion is unspecified upon overflow. */
2045 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2049 case FIX_TRUNC_EXPR
:
2050 real_trunc (&r
, VOIDmode
, &x
);
2057 /* If R is NaN, return zero and show we have an overflow. */
2058 if (REAL_VALUE_ISNAN (r
))
2061 val
= wi::zero (TYPE_PRECISION (type
));
2064 /* See if R is less than the lower bound or greater than the
2069 tree lt
= TYPE_MIN_VALUE (type
);
2070 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2071 if (real_less (&r
, &l
))
2074 val
= wi::to_wide (lt
);
2080 tree ut
= TYPE_MAX_VALUE (type
);
2083 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2084 if (real_less (&u
, &r
))
2087 val
= wi::to_wide (ut
);
2093 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2095 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2099 /* A subroutine of fold_convert_const handling conversions of a
2100 FIXED_CST to an integer type. */
2103 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2106 double_int temp
, temp_trunc
;
2109 /* Right shift FIXED_CST to temp by fbit. */
2110 temp
= TREE_FIXED_CST (arg1
).data
;
2111 mode
= TREE_FIXED_CST (arg1
).mode
;
2112 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2114 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2115 HOST_BITS_PER_DOUBLE_INT
,
2116 SIGNED_FIXED_POINT_MODE_P (mode
));
2118 /* Left shift temp to temp_trunc by fbit. */
2119 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2120 HOST_BITS_PER_DOUBLE_INT
,
2121 SIGNED_FIXED_POINT_MODE_P (mode
));
2125 temp
= double_int_zero
;
2126 temp_trunc
= double_int_zero
;
2129 /* If FIXED_CST is negative, we need to round the value toward 0.
2130 By checking if the fractional bits are not zero to add 1 to temp. */
2131 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2132 && temp_trunc
.is_negative ()
2133 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2134 temp
+= double_int_one
;
2136 /* Given a fixed-point constant, make new constant with new type,
2137 appropriately sign-extended or truncated. */
2138 t
= force_fit_type (type
, temp
, -1,
2139 (temp
.is_negative ()
2140 && (TYPE_UNSIGNED (type
)
2141 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2142 | TREE_OVERFLOW (arg1
));
2147 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2148 to another floating point type. */
2151 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2153 REAL_VALUE_TYPE value
;
2156 /* Don't perform the operation if flag_signaling_nans is on
2157 and the operand is a signaling NaN. */
2158 if (HONOR_SNANS (arg1
)
2159 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2162 /* With flag_rounding_math we should respect the current rounding mode
2163 unless the conversion is exact. */
2164 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1
)
2165 && !exact_real_truncate (TYPE_MODE (type
), &TREE_REAL_CST (arg1
)))
2168 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2169 t
= build_real (type
, value
);
2171 /* If converting an infinity or NAN to a representation that doesn't
2172 have one, set the overflow bit so that we can produce some kind of
2173 error message at the appropriate point if necessary. It's not the
2174 most user-friendly message, but it's better than nothing. */
2175 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2176 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2177 TREE_OVERFLOW (t
) = 1;
2178 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2179 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2180 TREE_OVERFLOW (t
) = 1;
2181 /* Regular overflow, conversion produced an infinity in a mode that
2182 can't represent them. */
2183 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2184 && REAL_VALUE_ISINF (value
)
2185 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2186 TREE_OVERFLOW (t
) = 1;
2188 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2192 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2193 to a floating point type. */
2196 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2198 REAL_VALUE_TYPE value
;
2201 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2202 &TREE_FIXED_CST (arg1
));
2203 t
= build_real (type
, value
);
2205 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2209 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2210 to another fixed-point type. */
2213 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2215 FIXED_VALUE_TYPE value
;
2219 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2220 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2221 t
= build_fixed (type
, value
);
2223 /* Propagate overflow flags. */
2224 if (overflow_p
| TREE_OVERFLOW (arg1
))
2225 TREE_OVERFLOW (t
) = 1;
2229 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2230 to a fixed-point type. */
2233 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2235 FIXED_VALUE_TYPE value
;
2240 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2242 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2243 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2244 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2246 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2248 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2249 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2250 TYPE_SATURATING (type
));
2251 t
= build_fixed (type
, value
);
2253 /* Propagate overflow flags. */
2254 if (overflow_p
| TREE_OVERFLOW (arg1
))
2255 TREE_OVERFLOW (t
) = 1;
2259 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2260 to a fixed-point type. */
2263 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2265 FIXED_VALUE_TYPE value
;
2269 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2270 &TREE_REAL_CST (arg1
),
2271 TYPE_SATURATING (type
));
2272 t
= build_fixed (type
, value
);
2274 /* Propagate overflow flags. */
2275 if (overflow_p
| TREE_OVERFLOW (arg1
))
2276 TREE_OVERFLOW (t
) = 1;
2280 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2281 type TYPE. If no simplification can be done return NULL_TREE. */
2284 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2286 tree arg_type
= TREE_TYPE (arg1
);
2287 if (arg_type
== type
)
2290 /* We can't widen types, since the runtime value could overflow the
2291 original type before being extended to the new type. */
2292 if (POLY_INT_CST_P (arg1
)
2293 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2294 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2295 return build_poly_int_cst (type
,
2296 poly_wide_int::from (poly_int_cst_value (arg1
),
2297 TYPE_PRECISION (type
),
2298 TYPE_SIGN (arg_type
)));
2300 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2301 || TREE_CODE (type
) == OFFSET_TYPE
)
2303 if (TREE_CODE (arg1
) == INTEGER_CST
)
2304 return fold_convert_const_int_from_int (type
, arg1
);
2305 else if (TREE_CODE (arg1
) == REAL_CST
)
2306 return fold_convert_const_int_from_real (code
, type
, arg1
);
2307 else if (TREE_CODE (arg1
) == FIXED_CST
)
2308 return fold_convert_const_int_from_fixed (type
, arg1
);
2310 else if (TREE_CODE (type
) == REAL_TYPE
)
2312 if (TREE_CODE (arg1
) == INTEGER_CST
)
2314 tree res
= build_real_from_int_cst (type
, arg1
);
2315 /* Avoid the folding if flag_rounding_math is on and the
2316 conversion is not exact. */
2317 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
2320 wide_int w
= real_to_integer (&TREE_REAL_CST (res
), &fail
,
2321 TYPE_PRECISION (TREE_TYPE (arg1
)));
2322 if (fail
|| wi::ne_p (w
, wi::to_wide (arg1
)))
2327 else if (TREE_CODE (arg1
) == REAL_CST
)
2328 return fold_convert_const_real_from_real (type
, arg1
);
2329 else if (TREE_CODE (arg1
) == FIXED_CST
)
2330 return fold_convert_const_real_from_fixed (type
, arg1
);
2332 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2334 if (TREE_CODE (arg1
) == FIXED_CST
)
2335 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2336 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2337 return fold_convert_const_fixed_from_int (type
, arg1
);
2338 else if (TREE_CODE (arg1
) == REAL_CST
)
2339 return fold_convert_const_fixed_from_real (type
, arg1
);
2341 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2343 if (TREE_CODE (arg1
) == VECTOR_CST
2344 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2346 tree elttype
= TREE_TYPE (type
);
2347 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2348 /* We can't handle steps directly when extending, since the
2349 values need to wrap at the original precision first. */
2351 = (INTEGRAL_TYPE_P (elttype
)
2352 && INTEGRAL_TYPE_P (arg1_elttype
)
2353 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2354 tree_vector_builder v
;
2355 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2357 unsigned int len
= v
.encoded_nelts ();
2358 for (unsigned int i
= 0; i
< len
; ++i
)
2360 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2361 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2362 if (cvt
== NULL_TREE
)
2372 /* Construct a vector of zero elements of vector type TYPE. */
2375 build_zero_vector (tree type
)
2379 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2380 return build_vector_from_val (type
, t
);
2383 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2386 fold_convertible_p (const_tree type
, const_tree arg
)
2388 const_tree orig
= TREE_TYPE (arg
);
2393 if (TREE_CODE (arg
) == ERROR_MARK
2394 || TREE_CODE (type
) == ERROR_MARK
2395 || TREE_CODE (orig
) == ERROR_MARK
)
2398 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2401 switch (TREE_CODE (type
))
2403 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2404 case POINTER_TYPE
: case REFERENCE_TYPE
:
2406 return (INTEGRAL_TYPE_P (orig
)
2407 || (POINTER_TYPE_P (orig
)
2408 && TYPE_PRECISION (type
) <= TYPE_PRECISION (orig
))
2409 || TREE_CODE (orig
) == OFFSET_TYPE
);
2412 case FIXED_POINT_TYPE
:
2414 return TREE_CODE (type
) == TREE_CODE (orig
);
2417 return (VECTOR_TYPE_P (orig
)
2418 && known_eq (TYPE_VECTOR_SUBPARTS (type
),
2419 TYPE_VECTOR_SUBPARTS (orig
))
2420 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2427 /* Convert expression ARG to type TYPE. Used by the middle-end for
2428 simple conversions in preference to calling the front-end's convert. */
2431 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2433 tree orig
= TREE_TYPE (arg
);
2439 if (TREE_CODE (arg
) == ERROR_MARK
2440 || TREE_CODE (type
) == ERROR_MARK
2441 || TREE_CODE (orig
) == ERROR_MARK
)
2442 return error_mark_node
;
2444 switch (TREE_CODE (type
))
2447 case REFERENCE_TYPE
:
2448 /* Handle conversions between pointers to different address spaces. */
2449 if (POINTER_TYPE_P (orig
)
2450 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2451 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2452 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2455 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2457 if (TREE_CODE (arg
) == INTEGER_CST
)
2459 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2460 if (tem
!= NULL_TREE
)
2463 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2464 || TREE_CODE (orig
) == OFFSET_TYPE
)
2465 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2466 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2467 return fold_convert_loc (loc
, type
,
2468 fold_build1_loc (loc
, REALPART_EXPR
,
2469 TREE_TYPE (orig
), arg
));
2470 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2471 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2472 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2475 if (TREE_CODE (arg
) == INTEGER_CST
)
2477 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2478 if (tem
!= NULL_TREE
)
2481 else if (TREE_CODE (arg
) == REAL_CST
)
2483 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2484 if (tem
!= NULL_TREE
)
2487 else if (TREE_CODE (arg
) == FIXED_CST
)
2489 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2490 if (tem
!= NULL_TREE
)
2494 switch (TREE_CODE (orig
))
2497 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2498 case POINTER_TYPE
: case REFERENCE_TYPE
:
2499 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2502 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2504 case FIXED_POINT_TYPE
:
2505 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2508 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2509 return fold_convert_loc (loc
, type
, tem
);
2515 case FIXED_POINT_TYPE
:
2516 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2517 || TREE_CODE (arg
) == REAL_CST
)
2519 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2520 if (tem
!= NULL_TREE
)
2521 goto fold_convert_exit
;
2524 switch (TREE_CODE (orig
))
2526 case FIXED_POINT_TYPE
:
2531 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2534 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2535 return fold_convert_loc (loc
, type
, tem
);
2542 switch (TREE_CODE (orig
))
2545 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2546 case POINTER_TYPE
: case REFERENCE_TYPE
:
2548 case FIXED_POINT_TYPE
:
2549 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2550 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2551 fold_convert_loc (loc
, TREE_TYPE (type
),
2552 integer_zero_node
));
2557 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2559 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2560 TREE_OPERAND (arg
, 0));
2561 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2562 TREE_OPERAND (arg
, 1));
2563 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2566 arg
= save_expr (arg
);
2567 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2568 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2569 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2570 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2571 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2579 if (integer_zerop (arg
))
2580 return build_zero_vector (type
);
2581 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2582 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2583 || TREE_CODE (orig
) == VECTOR_TYPE
);
2584 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2587 tem
= fold_ignored_result (arg
);
2588 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2591 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2592 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2596 protected_set_expr_location_unshare (tem
, loc
);
2600 /* Return false if expr can be assumed not to be an lvalue, true
2604 maybe_lvalue_p (const_tree x
)
2606 /* We only need to wrap lvalue tree codes. */
2607 switch (TREE_CODE (x
))
2620 case ARRAY_RANGE_REF
:
2626 case PREINCREMENT_EXPR
:
2627 case PREDECREMENT_EXPR
:
2629 case TRY_CATCH_EXPR
:
2630 case WITH_CLEANUP_EXPR
:
2636 case VIEW_CONVERT_EXPR
:
2640 /* Assume the worst for front-end tree codes. */
2641 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2649 /* Return an expr equal to X but certainly not valid as an lvalue. */
2652 non_lvalue_loc (location_t loc
, tree x
)
2654 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2659 if (! maybe_lvalue_p (x
))
2661 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2664 /* Given a tree comparison code, return the code that is the logical inverse.
2665 It is generally not safe to do this for floating-point comparisons, except
2666 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2667 ERROR_MARK in this case. */
2670 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2672 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2673 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2683 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2685 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2687 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2689 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2703 return UNORDERED_EXPR
;
2704 case UNORDERED_EXPR
:
2705 return ORDERED_EXPR
;
2711 /* Similar, but return the comparison that results if the operands are
2712 swapped. This is safe for floating-point. */
2715 swap_tree_comparison (enum tree_code code
)
2722 case UNORDERED_EXPR
:
2748 /* Convert a comparison tree code from an enum tree_code representation
2749 into a compcode bit-based encoding. This function is the inverse of
2750 compcode_to_comparison. */
2752 static enum comparison_code
2753 comparison_to_compcode (enum tree_code code
)
2770 return COMPCODE_ORD
;
2771 case UNORDERED_EXPR
:
2772 return COMPCODE_UNORD
;
2774 return COMPCODE_UNLT
;
2776 return COMPCODE_UNEQ
;
2778 return COMPCODE_UNLE
;
2780 return COMPCODE_UNGT
;
2782 return COMPCODE_LTGT
;
2784 return COMPCODE_UNGE
;
2790 /* Convert a compcode bit-based encoding of a comparison operator back
2791 to GCC's enum tree_code representation. This function is the
2792 inverse of comparison_to_compcode. */
2794 static enum tree_code
2795 compcode_to_comparison (enum comparison_code code
)
2812 return ORDERED_EXPR
;
2813 case COMPCODE_UNORD
:
2814 return UNORDERED_EXPR
;
2832 /* Return true if COND1 tests the opposite condition of COND2. */
2835 inverse_conditions_p (const_tree cond1
, const_tree cond2
)
2837 return (COMPARISON_CLASS_P (cond1
)
2838 && COMPARISON_CLASS_P (cond2
)
2839 && (invert_tree_comparison
2841 HONOR_NANS (TREE_OPERAND (cond1
, 0))) == TREE_CODE (cond2
))
2842 && operand_equal_p (TREE_OPERAND (cond1
, 0),
2843 TREE_OPERAND (cond2
, 0), 0)
2844 && operand_equal_p (TREE_OPERAND (cond1
, 1),
2845 TREE_OPERAND (cond2
, 1), 0));
2848 /* Return a tree for the comparison which is the combination of
2849 doing the AND or OR (depending on CODE) of the two operations LCODE
2850 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2851 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2852 if this makes the transformation invalid. */
2855 combine_comparisons (location_t loc
,
2856 enum tree_code code
, enum tree_code lcode
,
2857 enum tree_code rcode
, tree truth_type
,
2858 tree ll_arg
, tree lr_arg
)
2860 bool honor_nans
= HONOR_NANS (ll_arg
);
2861 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2862 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2867 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2868 compcode
= lcompcode
& rcompcode
;
2871 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2872 compcode
= lcompcode
| rcompcode
;
2881 /* Eliminate unordered comparisons, as well as LTGT and ORD
2882 which are not used unless the mode has NaNs. */
2883 compcode
&= ~COMPCODE_UNORD
;
2884 if (compcode
== COMPCODE_LTGT
)
2885 compcode
= COMPCODE_NE
;
2886 else if (compcode
== COMPCODE_ORD
)
2887 compcode
= COMPCODE_TRUE
;
2889 else if (flag_trapping_math
)
2891 /* Check that the original operation and the optimized ones will trap
2892 under the same condition. */
2893 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2894 && (lcompcode
!= COMPCODE_EQ
)
2895 && (lcompcode
!= COMPCODE_ORD
);
2896 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2897 && (rcompcode
!= COMPCODE_EQ
)
2898 && (rcompcode
!= COMPCODE_ORD
);
2899 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2900 && (compcode
!= COMPCODE_EQ
)
2901 && (compcode
!= COMPCODE_ORD
);
2903 /* In a short-circuited boolean expression the LHS might be
2904 such that the RHS, if evaluated, will never trap. For
2905 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2906 if neither x nor y is NaN. (This is a mixed blessing: for
2907 example, the expression above will never trap, hence
2908 optimizing it to x < y would be invalid). */
2909 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2910 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2913 /* If the comparison was short-circuited, and only the RHS
2914 trapped, we may now generate a spurious trap. */
2916 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2919 /* If we changed the conditions that cause a trap, we lose. */
2920 if ((ltrap
|| rtrap
) != trap
)
2924 if (compcode
== COMPCODE_TRUE
)
2925 return constant_boolean_node (true, truth_type
);
2926 else if (compcode
== COMPCODE_FALSE
)
2927 return constant_boolean_node (false, truth_type
);
2930 enum tree_code tcode
;
2932 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2933 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2937 /* Return nonzero if two operands (typically of the same tree node)
2938 are necessarily equal. FLAGS modifies behavior as follows:
2940 If OEP_ONLY_CONST is set, only return nonzero for constants.
2941 This function tests whether the operands are indistinguishable;
2942 it does not test whether they are equal using C's == operation.
2943 The distinction is important for IEEE floating point, because
2944 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2945 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2947 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2948 even though it may hold multiple values during a function.
2949 This is because a GCC tree node guarantees that nothing else is
2950 executed between the evaluation of its "operands" (which may often
2951 be evaluated in arbitrary order). Hence if the operands themselves
2952 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2953 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2954 unset means assuming isochronic (or instantaneous) tree equivalence.
2955 Unless comparing arbitrary expression trees, such as from different
2956 statements, this flag can usually be left unset.
2958 If OEP_PURE_SAME is set, then pure functions with identical arguments
2959 are considered the same. It is used when the caller has other ways
2960 to ensure that global memory is unchanged in between.
2962 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2963 not values of expressions.
2965 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2966 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2968 If OEP_BITWISE is set, then require the values to be bitwise identical
2969 rather than simply numerically equal. Do not take advantage of things
2970 like math-related flags or undefined behavior; only return true for
2971 values that are provably bitwise identical in all circumstances.
2973 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2974 any operand with side effect. This is unnecesarily conservative in the
2975 case we know that arg0 and arg1 are in disjoint code paths (such as in
2976 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2977 addresses with TREE_CONSTANT flag set so we know that &var == &var
2978 even if var is volatile. */
2981 operand_compare::operand_equal_p (const_tree arg0
, const_tree arg1
,
2985 if (verify_hash_value (arg0
, arg1
, flags
, &r
))
2988 STRIP_ANY_LOCATION_WRAPPER (arg0
);
2989 STRIP_ANY_LOCATION_WRAPPER (arg1
);
2991 /* If either is ERROR_MARK, they aren't equal. */
2992 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2993 || TREE_TYPE (arg0
) == error_mark_node
2994 || TREE_TYPE (arg1
) == error_mark_node
)
2997 /* Similar, if either does not have a type (like a template id),
2998 they aren't equal. */
2999 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
3002 /* Bitwise identity makes no sense if the values have different layouts. */
3003 if ((flags
& OEP_BITWISE
)
3004 && !tree_nop_conversion_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3007 /* We cannot consider pointers to different address space equal. */
3008 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
3009 && POINTER_TYPE_P (TREE_TYPE (arg1
))
3010 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
3011 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
3014 /* Check equality of integer constants before bailing out due to
3015 precision differences. */
3016 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3018 /* Address of INTEGER_CST is not defined; check that we did not forget
3019 to drop the OEP_ADDRESS_OF flags. */
3020 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3021 return tree_int_cst_equal (arg0
, arg1
);
3024 if (!(flags
& OEP_ADDRESS_OF
))
3026 /* If both types don't have the same signedness, then we can't consider
3027 them equal. We must check this before the STRIP_NOPS calls
3028 because they may change the signedness of the arguments. As pointers
3029 strictly don't have a signedness, require either two pointers or
3030 two non-pointers as well. */
3031 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
3032 || POINTER_TYPE_P (TREE_TYPE (arg0
))
3033 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
3036 /* If both types don't have the same precision, then it is not safe
3038 if (element_precision (TREE_TYPE (arg0
))
3039 != element_precision (TREE_TYPE (arg1
)))
3046 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3047 sanity check once the issue is solved. */
3049 /* Addresses of conversions and SSA_NAMEs (and many other things)
3050 are not defined. Check that we did not forget to drop the
3051 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3052 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
3053 && TREE_CODE (arg0
) != SSA_NAME
);
3056 /* In case both args are comparisons but with different comparison
3057 code, try to swap the comparison operands of one arg to produce
3058 a match and compare that variant. */
3059 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3060 && COMPARISON_CLASS_P (arg0
)
3061 && COMPARISON_CLASS_P (arg1
))
3063 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
3065 if (TREE_CODE (arg0
) == swap_code
)
3066 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3067 TREE_OPERAND (arg1
, 1), flags
)
3068 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3069 TREE_OPERAND (arg1
, 0), flags
);
3072 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3074 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3075 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3077 else if (flags
& OEP_ADDRESS_OF
)
3079 /* If we are interested in comparing addresses ignore
3080 MEM_REF wrappings of the base that can appear just for
3082 if (TREE_CODE (arg0
) == MEM_REF
3084 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3085 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3086 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3088 else if (TREE_CODE (arg1
) == MEM_REF
3090 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3091 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3092 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3100 /* When not checking adddresses, this is needed for conversions and for
3101 COMPONENT_REF. Might as well play it safe and always test this. */
3102 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3103 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3104 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3105 && !(flags
& OEP_ADDRESS_OF
)))
3108 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3109 We don't care about side effects in that case because the SAVE_EXPR
3110 takes care of that for us. In all other cases, two expressions are
3111 equal if they have no side effects. If we have two identical
3112 expressions with side effects that should be treated the same due
3113 to the only side effects being identical SAVE_EXPR's, that will
3114 be detected in the recursive calls below.
3115 If we are taking an invariant address of two identical objects
3116 they are necessarily equal as well. */
3117 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3118 && (TREE_CODE (arg0
) == SAVE_EXPR
3119 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3120 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3123 /* Next handle constant cases, those for which we can return 1 even
3124 if ONLY_CONST is set. */
3125 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3126 switch (TREE_CODE (arg0
))
3129 return tree_int_cst_equal (arg0
, arg1
);
3132 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3133 TREE_FIXED_CST (arg1
));
3136 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3139 if (!(flags
& OEP_BITWISE
) && !HONOR_SIGNED_ZEROS (arg0
))
3141 /* If we do not distinguish between signed and unsigned zero,
3142 consider them equal. */
3143 if (real_zerop (arg0
) && real_zerop (arg1
))
3150 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3151 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3154 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3155 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3158 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3159 for (unsigned int i
= 0; i
< count
; ++i
)
3160 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3161 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3167 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3169 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3173 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3174 && ! memcmp (TREE_STRING_POINTER (arg0
),
3175 TREE_STRING_POINTER (arg1
),
3176 TREE_STRING_LENGTH (arg0
)));
3179 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3180 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3181 flags
| OEP_ADDRESS_OF
3182 | OEP_MATCH_SIDE_EFFECTS
);
3184 /* In GIMPLE empty constructors are allowed in initializers of
3186 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
3191 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3192 two instances of undefined behavior will give identical results. */
3193 if (flags
& (OEP_ONLY_CONST
| OEP_BITWISE
))
3196 /* Define macros to test an operand from arg0 and arg1 for equality and a
3197 variant that allows null and views null as being different from any
3198 non-null value. In the latter case, if either is null, the both
3199 must be; otherwise, do the normal comparison. */
3200 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3201 TREE_OPERAND (arg1, N), flags)
3203 #define OP_SAME_WITH_NULL(N) \
3204 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3205 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3207 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3210 /* Two conversions are equal only if signedness and modes match. */
3211 switch (TREE_CODE (arg0
))
3214 case FIX_TRUNC_EXPR
:
3215 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3216 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3226 case tcc_comparison
:
3228 if (OP_SAME (0) && OP_SAME (1))
3231 /* For commutative ops, allow the other order. */
3232 return (commutative_tree_code (TREE_CODE (arg0
))
3233 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3234 TREE_OPERAND (arg1
, 1), flags
)
3235 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3236 TREE_OPERAND (arg1
, 0), flags
));
3239 /* If either of the pointer (or reference) expressions we are
3240 dereferencing contain a side effect, these cannot be equal,
3241 but their addresses can be. */
3242 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3243 && (TREE_SIDE_EFFECTS (arg0
)
3244 || TREE_SIDE_EFFECTS (arg1
)))
3247 switch (TREE_CODE (arg0
))
3250 if (!(flags
& OEP_ADDRESS_OF
))
3252 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3253 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3255 /* Verify that the access types are compatible. */
3256 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0
))
3257 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)))
3260 flags
&= ~OEP_ADDRESS_OF
;
3264 /* Require the same offset. */
3265 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3266 TYPE_SIZE (TREE_TYPE (arg1
)),
3267 flags
& ~OEP_ADDRESS_OF
))
3272 case VIEW_CONVERT_EXPR
:
3275 case TARGET_MEM_REF
:
3277 if (!(flags
& OEP_ADDRESS_OF
))
3279 /* Require equal access sizes */
3280 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3281 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3282 || !TYPE_SIZE (TREE_TYPE (arg1
))
3283 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3284 TYPE_SIZE (TREE_TYPE (arg1
)),
3287 /* Verify that access happens in similar types. */
3288 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3290 /* Verify that accesses are TBAA compatible. */
3291 if (!alias_ptr_types_compatible_p
3292 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3293 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3294 || (MR_DEPENDENCE_CLIQUE (arg0
)
3295 != MR_DEPENDENCE_CLIQUE (arg1
))
3296 || (MR_DEPENDENCE_BASE (arg0
)
3297 != MR_DEPENDENCE_BASE (arg1
)))
3299 /* Verify that alignment is compatible. */
3300 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3301 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3304 flags
&= ~OEP_ADDRESS_OF
;
3305 return (OP_SAME (0) && OP_SAME (1)
3306 /* TARGET_MEM_REF require equal extra operands. */
3307 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3308 || (OP_SAME_WITH_NULL (2)
3309 && OP_SAME_WITH_NULL (3)
3310 && OP_SAME_WITH_NULL (4))));
3313 case ARRAY_RANGE_REF
:
3316 flags
&= ~OEP_ADDRESS_OF
;
3317 /* Compare the array index by value if it is constant first as we
3318 may have different types but same value here. */
3319 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3320 TREE_OPERAND (arg1
, 1))
3322 && OP_SAME_WITH_NULL (2)
3323 && OP_SAME_WITH_NULL (3)
3324 /* Compare low bound and element size as with OEP_ADDRESS_OF
3325 we have to account for the offset of the ref. */
3326 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3327 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3328 || (operand_equal_p (array_ref_low_bound
3329 (CONST_CAST_TREE (arg0
)),
3331 (CONST_CAST_TREE (arg1
)), flags
)
3332 && operand_equal_p (array_ref_element_size
3333 (CONST_CAST_TREE (arg0
)),
3334 array_ref_element_size
3335 (CONST_CAST_TREE (arg1
)),
3339 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3340 may be NULL when we're called to compare MEM_EXPRs. */
3341 if (!OP_SAME_WITH_NULL (0))
3344 bool compare_address
= flags
& OEP_ADDRESS_OF
;
3346 /* Most of time we only need to compare FIELD_DECLs for equality.
3347 However when determining address look into actual offsets.
3348 These may match for unions and unshared record types. */
3349 flags
&= ~OEP_ADDRESS_OF
;
3353 && (flags
& OEP_ADDRESS_OF_SAME_FIELD
) == 0)
3355 if (TREE_OPERAND (arg0
, 2)
3356 || TREE_OPERAND (arg1
, 2))
3357 return OP_SAME_WITH_NULL (2);
3358 tree field0
= TREE_OPERAND (arg0
, 1);
3359 tree field1
= TREE_OPERAND (arg1
, 1);
3361 /* Non-FIELD_DECL operands can appear in C++ templates. */
3362 if (TREE_CODE (field0
) != FIELD_DECL
3363 || TREE_CODE (field1
) != FIELD_DECL
3364 || !operand_equal_p (DECL_FIELD_OFFSET (field0
),
3365 DECL_FIELD_OFFSET (field1
), flags
)
3366 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0
),
3367 DECL_FIELD_BIT_OFFSET (field1
),
3375 return OP_SAME_WITH_NULL (2);
3380 flags
&= ~OEP_ADDRESS_OF
;
3381 return OP_SAME (1) && OP_SAME (2);
3387 case tcc_expression
:
3388 switch (TREE_CODE (arg0
))
3391 /* Be sure we pass right ADDRESS_OF flag. */
3392 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3393 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3394 TREE_OPERAND (arg1
, 0),
3395 flags
| OEP_ADDRESS_OF
);
3397 case TRUTH_NOT_EXPR
:
3400 case TRUTH_ANDIF_EXPR
:
3401 case TRUTH_ORIF_EXPR
:
3402 return OP_SAME (0) && OP_SAME (1);
3404 case WIDEN_MULT_PLUS_EXPR
:
3405 case WIDEN_MULT_MINUS_EXPR
:
3408 /* The multiplcation operands are commutative. */
3411 case TRUTH_AND_EXPR
:
3413 case TRUTH_XOR_EXPR
:
3414 if (OP_SAME (0) && OP_SAME (1))
3417 /* Otherwise take into account this is a commutative operation. */
3418 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3419 TREE_OPERAND (arg1
, 1), flags
)
3420 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3421 TREE_OPERAND (arg1
, 0), flags
));
3424 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3426 flags
&= ~OEP_ADDRESS_OF
;
3429 case BIT_INSERT_EXPR
:
3430 /* BIT_INSERT_EXPR has an implict operand as the type precision
3431 of op1. Need to check to make sure they are the same. */
3432 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3433 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3434 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3435 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3441 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3446 case PREDECREMENT_EXPR
:
3447 case PREINCREMENT_EXPR
:
3448 case POSTDECREMENT_EXPR
:
3449 case POSTINCREMENT_EXPR
:
3450 if (flags
& OEP_LEXICOGRAPHIC
)
3451 return OP_SAME (0) && OP_SAME (1);
3454 case CLEANUP_POINT_EXPR
:
3457 if (flags
& OEP_LEXICOGRAPHIC
)
3462 /* Virtual table reference. */
3463 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0
),
3464 OBJ_TYPE_REF_EXPR (arg1
), flags
))
3466 flags
&= ~OEP_ADDRESS_OF
;
3467 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0
))
3468 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1
)))
3470 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0
),
3471 OBJ_TYPE_REF_OBJECT (arg1
), flags
))
3473 if (virtual_method_call_p (arg0
))
3475 if (!virtual_method_call_p (arg1
))
3477 return types_same_for_odr (obj_type_ref_class (arg0
),
3478 obj_type_ref_class (arg1
));
3487 switch (TREE_CODE (arg0
))
3490 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3491 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3492 /* If not both CALL_EXPRs are either internal or normal function
3493 functions, then they are not equal. */
3495 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3497 /* If the CALL_EXPRs call different internal functions, then they
3499 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3504 /* If the CALL_EXPRs call different functions, then they are not
3506 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3511 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3513 unsigned int cef
= call_expr_flags (arg0
);
3514 if (flags
& OEP_PURE_SAME
)
3515 cef
&= ECF_CONST
| ECF_PURE
;
3518 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3522 /* Now see if all the arguments are the same. */
3524 const_call_expr_arg_iterator iter0
, iter1
;
3526 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3527 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3529 a0
= next_const_call_expr_arg (&iter0
),
3530 a1
= next_const_call_expr_arg (&iter1
))
3531 if (! operand_equal_p (a0
, a1
, flags
))
3534 /* If we get here and both argument lists are exhausted
3535 then the CALL_EXPRs are equal. */
3536 return ! (a0
|| a1
);
3542 case tcc_declaration
:
3543 /* Consider __builtin_sqrt equal to sqrt. */
3544 if (TREE_CODE (arg0
) == FUNCTION_DECL
)
3545 return (fndecl_built_in_p (arg0
) && fndecl_built_in_p (arg1
)
3546 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3547 && (DECL_UNCHECKED_FUNCTION_CODE (arg0
)
3548 == DECL_UNCHECKED_FUNCTION_CODE (arg1
)));
3551 && (flags
& OEP_DECL_NAME
)
3552 && (flags
& OEP_LEXICOGRAPHIC
))
3554 /* Consider decls with the same name equal. The caller needs
3555 to make sure they refer to the same entity (such as a function
3556 formal parameter). */
3557 tree a0name
= DECL_NAME (arg0
);
3558 tree a1name
= DECL_NAME (arg1
);
3559 const char *a0ns
= a0name
? IDENTIFIER_POINTER (a0name
) : NULL
;
3560 const char *a1ns
= a1name
? IDENTIFIER_POINTER (a1name
) : NULL
;
3561 return a0ns
&& a1ns
&& strcmp (a0ns
, a1ns
) == 0;
3565 case tcc_exceptional
:
3566 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3568 if (CONSTRUCTOR_NO_CLEARING (arg0
) != CONSTRUCTOR_NO_CLEARING (arg1
))
3571 /* In GIMPLE constructors are used only to build vectors from
3572 elements. Individual elements in the constructor must be
3573 indexed in increasing order and form an initial sequence.
3575 We make no effort to compare constructors in generic.
3576 (see sem_variable::equals in ipa-icf which can do so for
3578 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3579 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3582 /* Be sure that vectors constructed have the same representation.
3583 We only tested element precision and modes to match.
3584 Vectors may be BLKmode and thus also check that the number of
3586 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3587 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3590 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3591 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3592 unsigned int len
= vec_safe_length (v0
);
3594 if (len
!= vec_safe_length (v1
))
3597 for (unsigned int i
= 0; i
< len
; i
++)
3599 constructor_elt
*c0
= &(*v0
)[i
];
3600 constructor_elt
*c1
= &(*v1
)[i
];
3602 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3603 /* In GIMPLE the indexes can be either NULL or matching i.
3604 Double check this so we won't get false
3605 positives for GENERIC. */
3607 && (TREE_CODE (c0
->index
) != INTEGER_CST
3608 || compare_tree_int (c0
->index
, i
)))
3610 && (TREE_CODE (c1
->index
) != INTEGER_CST
3611 || compare_tree_int (c1
->index
, i
))))
3616 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3617 && (flags
& OEP_LEXICOGRAPHIC
))
3619 /* Compare the STATEMENT_LISTs. */
3620 tree_stmt_iterator tsi1
, tsi2
;
3621 tree body1
= CONST_CAST_TREE (arg0
);
3622 tree body2
= CONST_CAST_TREE (arg1
);
3623 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3624 tsi_next (&tsi1
), tsi_next (&tsi2
))
3626 /* The lists don't have the same number of statements. */
3627 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3629 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3631 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3632 flags
& (OEP_LEXICOGRAPHIC
3633 | OEP_NO_HASH_CHECK
)))
3640 switch (TREE_CODE (arg0
))
3643 if (flags
& OEP_LEXICOGRAPHIC
)
3644 return OP_SAME_WITH_NULL (0);
3646 case DEBUG_BEGIN_STMT
:
3647 if (flags
& OEP_LEXICOGRAPHIC
)
3659 #undef OP_SAME_WITH_NULL
3662 /* Generate a hash value for an expression. This can be used iteratively
3663 by passing a previous result as the HSTATE argument. */
3666 operand_compare::hash_operand (const_tree t
, inchash::hash
&hstate
,
3670 enum tree_code code
;
3671 enum tree_code_class tclass
;
3673 if (t
== NULL_TREE
|| t
== error_mark_node
)
3675 hstate
.merge_hash (0);
3679 STRIP_ANY_LOCATION_WRAPPER (t
);
3681 if (!(flags
& OEP_ADDRESS_OF
))
3684 code
= TREE_CODE (t
);
3688 /* Alas, constants aren't shared, so we can't rely on pointer
3691 hstate
.merge_hash (0);
3694 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3695 for (i
= 0; i
< TREE_INT_CST_EXT_NUNITS (t
); i
++)
3696 hstate
.add_hwi (TREE_INT_CST_ELT (t
, i
));
3701 if (!HONOR_SIGNED_ZEROS (t
) && real_zerop (t
))
3704 val2
= real_hash (TREE_REAL_CST_PTR (t
));
3705 hstate
.merge_hash (val2
);
3710 unsigned int val2
= fixed_hash (TREE_FIXED_CST_PTR (t
));
3711 hstate
.merge_hash (val2
);
3715 hstate
.add ((const void *) TREE_STRING_POINTER (t
),
3716 TREE_STRING_LENGTH (t
));
3719 hash_operand (TREE_REALPART (t
), hstate
, flags
);
3720 hash_operand (TREE_IMAGPART (t
), hstate
, flags
);
3724 hstate
.add_int (VECTOR_CST_NPATTERNS (t
));
3725 hstate
.add_int (VECTOR_CST_NELTS_PER_PATTERN (t
));
3726 unsigned int count
= vector_cst_encoded_nelts (t
);
3727 for (unsigned int i
= 0; i
< count
; ++i
)
3728 hash_operand (VECTOR_CST_ENCODED_ELT (t
, i
), hstate
, flags
);
3732 /* We can just compare by pointer. */
3733 hstate
.add_hwi (SSA_NAME_VERSION (t
));
3735 case PLACEHOLDER_EXPR
:
3736 /* The node itself doesn't matter. */
3743 /* A list of expressions, for a CALL_EXPR or as the elements of a
3745 for (; t
; t
= TREE_CHAIN (t
))
3746 hash_operand (TREE_VALUE (t
), hstate
, flags
);
3750 unsigned HOST_WIDE_INT idx
;
3752 flags
&= ~OEP_ADDRESS_OF
;
3753 hstate
.add_int (CONSTRUCTOR_NO_CLEARING (t
));
3754 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), idx
, field
, value
)
3756 /* In GIMPLE the indexes can be either NULL or matching i. */
3757 if (field
== NULL_TREE
)
3758 field
= bitsize_int (idx
);
3759 hash_operand (field
, hstate
, flags
);
3760 hash_operand (value
, hstate
, flags
);
3764 case STATEMENT_LIST
:
3766 tree_stmt_iterator i
;
3767 for (i
= tsi_start (CONST_CAST_TREE (t
));
3768 !tsi_end_p (i
); tsi_next (&i
))
3769 hash_operand (tsi_stmt (i
), hstate
, flags
);
3773 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
3774 hash_operand (TREE_VEC_ELT (t
, i
), hstate
, flags
);
3776 case IDENTIFIER_NODE
:
3777 hstate
.add_object (IDENTIFIER_HASH_VALUE (t
));
3780 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3781 Otherwise nodes that compare equal according to operand_equal_p might
3782 get different hash codes. However, don't do this for machine specific
3783 or front end builtins, since the function code is overloaded in those
3785 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
3786 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t
)))
3788 t
= builtin_decl_explicit (DECL_FUNCTION_CODE (t
));
3789 code
= TREE_CODE (t
);
3793 if (POLY_INT_CST_P (t
))
3795 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
3796 hstate
.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t
, i
)));
3799 tclass
= TREE_CODE_CLASS (code
);
3801 if (tclass
== tcc_declaration
)
3803 /* DECL's have a unique ID */
3804 hstate
.add_hwi (DECL_UID (t
));
3806 else if (tclass
== tcc_comparison
&& !commutative_tree_code (code
))
3808 /* For comparisons that can be swapped, use the lower
3810 enum tree_code ccode
= swap_tree_comparison (code
);
3813 hstate
.add_object (ccode
);
3814 hash_operand (TREE_OPERAND (t
, ccode
!= code
), hstate
, flags
);
3815 hash_operand (TREE_OPERAND (t
, ccode
== code
), hstate
, flags
);
3817 else if (CONVERT_EXPR_CODE_P (code
))
3819 /* NOP_EXPR and CONVERT_EXPR are considered equal by
3821 enum tree_code ccode
= NOP_EXPR
;
3822 hstate
.add_object (ccode
);
3824 /* Don't hash the type, that can lead to having nodes which
3825 compare equal according to operand_equal_p, but which
3826 have different hash codes. Make sure to include signedness
3827 in the hash computation. */
3828 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3829 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3831 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
3832 else if (code
== MEM_REF
3833 && (flags
& OEP_ADDRESS_OF
) != 0
3834 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
3835 && DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0))
3836 && integer_zerop (TREE_OPERAND (t
, 1)))
3837 hash_operand (TREE_OPERAND (TREE_OPERAND (t
, 0), 0),
3839 /* Don't ICE on FE specific trees, or their arguments etc.
3840 during operand_equal_p hash verification. */
3841 else if (!IS_EXPR_CODE_CLASS (tclass
))
3842 gcc_assert (flags
& OEP_HASH_CHECK
);
3845 unsigned int sflags
= flags
;
3847 hstate
.add_object (code
);
3852 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3853 flags
|= OEP_ADDRESS_OF
;
3859 case TARGET_MEM_REF
:
3860 flags
&= ~OEP_ADDRESS_OF
;
3865 if (sflags
& OEP_ADDRESS_OF
)
3867 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3868 if (TREE_OPERAND (t
, 2))
3869 hash_operand (TREE_OPERAND (t
, 2), hstate
,
3870 flags
& ~OEP_ADDRESS_OF
);
3873 tree field
= TREE_OPERAND (t
, 1);
3874 hash_operand (DECL_FIELD_OFFSET (field
),
3875 hstate
, flags
& ~OEP_ADDRESS_OF
);
3876 hash_operand (DECL_FIELD_BIT_OFFSET (field
),
3877 hstate
, flags
& ~OEP_ADDRESS_OF
);
3883 case ARRAY_RANGE_REF
:
3885 sflags
&= ~OEP_ADDRESS_OF
;
3889 flags
&= ~OEP_ADDRESS_OF
;
3892 case WIDEN_MULT_PLUS_EXPR
:
3893 case WIDEN_MULT_MINUS_EXPR
:
3895 /* The multiplication operands are commutative. */
3896 inchash::hash one
, two
;
3897 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3898 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3899 hstate
.add_commutative (one
, two
);
3900 hash_operand (TREE_OPERAND (t
, 2), two
, flags
);
3905 if (CALL_EXPR_FN (t
) == NULL_TREE
)
3906 hstate
.add_int (CALL_EXPR_IFN (t
));
3910 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
3911 Usually different TARGET_EXPRs just should use
3912 different temporaries in their slots. */
3913 hash_operand (TARGET_EXPR_SLOT (t
), hstate
, flags
);
3917 /* Virtual table reference. */
3918 inchash::add_expr (OBJ_TYPE_REF_EXPR (t
), hstate
, flags
);
3919 flags
&= ~OEP_ADDRESS_OF
;
3920 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t
), hstate
, flags
);
3921 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t
), hstate
, flags
);
3922 if (!virtual_method_call_p (t
))
3924 if (tree c
= obj_type_ref_class (t
))
3926 c
= TYPE_NAME (TYPE_MAIN_VARIANT (c
));
3927 /* We compute mangled names only when free_lang_data is run.
3928 In that case we can hash precisely. */
3929 if (TREE_CODE (c
) == TYPE_DECL
3930 && DECL_ASSEMBLER_NAME_SET_P (c
))
3932 (IDENTIFIER_HASH_VALUE
3933 (DECL_ASSEMBLER_NAME (c
)));
3940 /* Don't hash the type, that can lead to having nodes which
3941 compare equal according to operand_equal_p, but which
3942 have different hash codes. */
3943 if (code
== NON_LVALUE_EXPR
)
3945 /* Make sure to include signness in the hash computation. */
3946 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3947 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3950 else if (commutative_tree_code (code
))
3952 /* It's a commutative expression. We want to hash it the same
3953 however it appears. We do this by first hashing both operands
3954 and then rehashing based on the order of their independent
3956 inchash::hash one
, two
;
3957 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3958 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3959 hstate
.add_commutative (one
, two
);
3962 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
3963 hash_operand (TREE_OPERAND (t
, i
), hstate
,
3964 i
== 0 ? flags
: sflags
);
3971 operand_compare::verify_hash_value (const_tree arg0
, const_tree arg1
,
3972 unsigned int flags
, bool *ret
)
3974 /* When checking and unless comparing DECL names, verify that if
3975 the outermost operand_equal_p call returns non-zero then ARG0
3976 and ARG1 have the same hash value. */
3977 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
3979 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
3981 if (arg0
!= arg1
&& !(flags
& OEP_DECL_NAME
))
3983 inchash::hash
hstate0 (0), hstate1 (0);
3984 hash_operand (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
3985 hash_operand (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
3986 hashval_t h0
= hstate0
.end ();
3987 hashval_t h1
= hstate1
.end ();
3988 gcc_assert (h0
== h1
);
4002 static operand_compare default_compare_instance
;
4004 /* Conveinece wrapper around operand_compare class because usually we do
4005 not need to play with the valueizer. */
4008 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
4010 return default_compare_instance
.operand_equal_p (arg0
, arg1
, flags
);
4016 /* Generate a hash value for an expression. This can be used iteratively
4017 by passing a previous result as the HSTATE argument.
4019 This function is intended to produce the same hash for expressions which
4020 would compare equal using operand_equal_p. */
4022 add_expr (const_tree t
, inchash::hash
&hstate
, unsigned int flags
)
4024 default_compare_instance
.hash_operand (t
, hstate
, flags
);
4029 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4030 with a different signedness or a narrower precision. */
4033 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
4035 if (operand_equal_p (arg0
, arg1
, 0))
4038 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
4039 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
4042 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4043 and see if the inner values are the same. This removes any
4044 signedness comparison, which doesn't matter here. */
4049 if (operand_equal_p (op0
, op1
, 0))
4052 /* Discard a single widening conversion from ARG1 and see if the inner
4053 value is the same as ARG0. */
4054 if (CONVERT_EXPR_P (arg1
)
4055 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4056 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4057 < TYPE_PRECISION (TREE_TYPE (arg1
))
4058 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
4064 /* See if ARG is an expression that is either a comparison or is performing
4065 arithmetic on comparisons. The comparisons must only be comparing
4066 two different values, which will be stored in *CVAL1 and *CVAL2; if
4067 they are nonzero it means that some operands have already been found.
4068 No variables may be used anywhere else in the expression except in the
4071 If this is true, return 1. Otherwise, return zero. */
4074 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
4076 enum tree_code code
= TREE_CODE (arg
);
4077 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4079 /* We can handle some of the tcc_expression cases here. */
4080 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4082 else if (tclass
== tcc_expression
4083 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
4084 || code
== COMPOUND_EXPR
))
4085 tclass
= tcc_binary
;
4090 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
4093 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4094 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
4099 case tcc_expression
:
4100 if (code
== COND_EXPR
)
4101 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4102 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
4103 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
4106 case tcc_comparison
:
4107 /* First see if we can handle the first operand, then the second. For
4108 the second operand, we know *CVAL1 can't be zero. It must be that
4109 one side of the comparison is each of the values; test for the
4110 case where this isn't true by failing if the two operands
4113 if (operand_equal_p (TREE_OPERAND (arg
, 0),
4114 TREE_OPERAND (arg
, 1), 0))
4118 *cval1
= TREE_OPERAND (arg
, 0);
4119 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
4121 else if (*cval2
== 0)
4122 *cval2
= TREE_OPERAND (arg
, 0);
4123 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
4128 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
4130 else if (*cval2
== 0)
4131 *cval2
= TREE_OPERAND (arg
, 1);
4132 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
4144 /* ARG is a tree that is known to contain just arithmetic operations and
4145 comparisons. Evaluate the operations in the tree substituting NEW0 for
4146 any occurrence of OLD0 as an operand of a comparison and likewise for
4150 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
4151 tree old1
, tree new1
)
4153 tree type
= TREE_TYPE (arg
);
4154 enum tree_code code
= TREE_CODE (arg
);
4155 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4157 /* We can handle some of the tcc_expression cases here. */
4158 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4160 else if (tclass
== tcc_expression
4161 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
4162 tclass
= tcc_binary
;
4167 return fold_build1_loc (loc
, code
, type
,
4168 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4169 old0
, new0
, old1
, new1
));
4172 return fold_build2_loc (loc
, code
, type
,
4173 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4174 old0
, new0
, old1
, new1
),
4175 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4176 old0
, new0
, old1
, new1
));
4178 case tcc_expression
:
4182 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
4186 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
4190 return fold_build3_loc (loc
, code
, type
,
4191 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4192 old0
, new0
, old1
, new1
),
4193 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4194 old0
, new0
, old1
, new1
),
4195 eval_subst (loc
, TREE_OPERAND (arg
, 2),
4196 old0
, new0
, old1
, new1
));
4200 /* Fall through - ??? */
4202 case tcc_comparison
:
4204 tree arg0
= TREE_OPERAND (arg
, 0);
4205 tree arg1
= TREE_OPERAND (arg
, 1);
4207 /* We need to check both for exact equality and tree equality. The
4208 former will be true if the operand has a side-effect. In that
4209 case, we know the operand occurred exactly once. */
4211 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
4213 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
4216 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
4218 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
4221 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
4229 /* Return a tree for the case when the result of an expression is RESULT
4230 converted to TYPE and OMITTED was previously an operand of the expression
4231 but is now not needed (e.g., we folded OMITTED * 0).
4233 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4234 the conversion of RESULT to TYPE. */
4237 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
4239 tree t
= fold_convert_loc (loc
, type
, result
);
4241 /* If the resulting operand is an empty statement, just return the omitted
4242 statement casted to void. */
4243 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
4244 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
4245 fold_ignored_result (omitted
));
4247 if (TREE_SIDE_EFFECTS (omitted
))
4248 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4249 fold_ignored_result (omitted
), t
);
4251 return non_lvalue_loc (loc
, t
);
4254 /* Return a tree for the case when the result of an expression is RESULT
4255 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4256 of the expression but are now not needed.
4258 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4259 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4260 evaluated before OMITTED2. Otherwise, if neither has side effects,
4261 just do the conversion of RESULT to TYPE. */
4264 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
4265 tree omitted1
, tree omitted2
)
4267 tree t
= fold_convert_loc (loc
, type
, result
);
4269 if (TREE_SIDE_EFFECTS (omitted2
))
4270 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
4271 if (TREE_SIDE_EFFECTS (omitted1
))
4272 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
4274 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
4278 /* Return a simplified tree node for the truth-negation of ARG. This
4279 never alters ARG itself. We assume that ARG is an operation that
4280 returns a truth value (0 or 1).
4282 FIXME: one would think we would fold the result, but it causes
4283 problems with the dominator optimizer. */
4286 fold_truth_not_expr (location_t loc
, tree arg
)
4288 tree type
= TREE_TYPE (arg
);
4289 enum tree_code code
= TREE_CODE (arg
);
4290 location_t loc1
, loc2
;
4292 /* If this is a comparison, we can simply invert it, except for
4293 floating-point non-equality comparisons, in which case we just
4294 enclose a TRUTH_NOT_EXPR around what we have. */
4296 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4298 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4299 if (FLOAT_TYPE_P (op_type
)
4300 && flag_trapping_math
4301 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
4302 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
4305 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
4306 if (code
== ERROR_MARK
)
4309 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
4310 TREE_OPERAND (arg
, 1));
4311 copy_warning (ret
, arg
);
4318 return constant_boolean_node (integer_zerop (arg
), type
);
4320 case TRUTH_AND_EXPR
:
4321 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4322 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4323 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
4324 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4325 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4328 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4329 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4330 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
4331 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4332 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4334 case TRUTH_XOR_EXPR
:
4335 /* Here we can invert either operand. We invert the first operand
4336 unless the second operand is a TRUTH_NOT_EXPR in which case our
4337 result is the XOR of the first operand with the inside of the
4338 negation of the second operand. */
4340 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
4341 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
4342 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
4344 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
4345 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
4346 TREE_OPERAND (arg
, 1));
4348 case TRUTH_ANDIF_EXPR
:
4349 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4350 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4351 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
4352 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4353 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4355 case TRUTH_ORIF_EXPR
:
4356 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4357 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4358 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
4359 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4360 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4362 case TRUTH_NOT_EXPR
:
4363 return TREE_OPERAND (arg
, 0);
4367 tree arg1
= TREE_OPERAND (arg
, 1);
4368 tree arg2
= TREE_OPERAND (arg
, 2);
4370 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4371 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
4373 /* A COND_EXPR may have a throw as one operand, which
4374 then has void type. Just leave void operands
4376 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
4377 VOID_TYPE_P (TREE_TYPE (arg1
))
4378 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
4379 VOID_TYPE_P (TREE_TYPE (arg2
))
4380 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
4384 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4385 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4386 TREE_OPERAND (arg
, 0),
4387 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
4389 case NON_LVALUE_EXPR
:
4390 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4391 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
4394 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4395 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4400 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4401 return build1_loc (loc
, TREE_CODE (arg
), type
,
4402 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4405 if (!integer_onep (TREE_OPERAND (arg
, 1)))
4407 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
4410 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4412 case CLEANUP_POINT_EXPR
:
4413 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4414 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
4415 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4422 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4423 assume that ARG is an operation that returns a truth value (0 or 1
4424 for scalars, 0 or -1 for vectors). Return the folded expression if
4425 folding is successful. Otherwise, return NULL_TREE. */
4428 fold_invert_truthvalue (location_t loc
, tree arg
)
4430 tree type
= TREE_TYPE (arg
);
4431 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
4437 /* Return a simplified tree node for the truth-negation of ARG. This
4438 never alters ARG itself. We assume that ARG is an operation that
4439 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4442 invert_truthvalue_loc (location_t loc
, tree arg
)
4444 if (TREE_CODE (arg
) == ERROR_MARK
)
4447 tree type
= TREE_TYPE (arg
);
4448 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
4454 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4455 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4456 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4457 is the original memory reference used to preserve the alias set of
4461 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
4462 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
4463 int unsignedp
, int reversep
)
4465 tree result
, bftype
;
4467 /* Attempt not to lose the access path if possible. */
4468 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
4470 tree ninner
= TREE_OPERAND (orig_inner
, 0);
4472 poly_int64 nbitsize
, nbitpos
;
4474 int nunsignedp
, nreversep
, nvolatilep
= 0;
4475 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
4476 &noffset
, &nmode
, &nunsignedp
,
4477 &nreversep
, &nvolatilep
);
4479 && noffset
== NULL_TREE
4480 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
4490 alias_set_type iset
= get_alias_set (orig_inner
);
4491 if (iset
== 0 && get_alias_set (inner
) != iset
)
4492 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
4493 build_fold_addr_expr (inner
),
4494 build_int_cst (ptr_type_node
, 0));
4496 if (known_eq (bitpos
, 0) && !reversep
)
4498 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
4499 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
4500 || POINTER_TYPE_P (TREE_TYPE (inner
)))
4501 && tree_fits_shwi_p (size
)
4502 && tree_to_shwi (size
) == bitsize
)
4503 return fold_convert_loc (loc
, type
, inner
);
4507 if (TYPE_PRECISION (bftype
) != bitsize
4508 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
4509 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4511 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4512 bitsize_int (bitsize
), bitsize_int (bitpos
));
4513 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4516 result
= fold_convert_loc (loc
, type
, result
);
4521 /* Optimize a bit-field compare.
4523 There are two cases: First is a compare against a constant and the
4524 second is a comparison of two items where the fields are at the same
4525 bit position relative to the start of a chunk (byte, halfword, word)
4526 large enough to contain it. In these cases we can avoid the shift
4527 implicit in bitfield extractions.
4529 For constants, we emit a compare of the shifted constant with the
4530 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4531 compared. For two fields at the same position, we do the ANDs with the
4532 similar mask and compare the result of the ANDs.
4534 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4535 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4536 are the left and right operands of the comparison, respectively.
4538 If the optimization described above can be done, we return the resulting
4539 tree. Otherwise we return zero. */
4542 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4543 tree compare_type
, tree lhs
, tree rhs
)
4545 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4546 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4547 tree type
= TREE_TYPE (lhs
);
4549 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4550 machine_mode lmode
, rmode
;
4551 scalar_int_mode nmode
;
4552 int lunsignedp
, runsignedp
;
4553 int lreversep
, rreversep
;
4554 int lvolatilep
= 0, rvolatilep
= 0;
4555 tree linner
, rinner
= NULL_TREE
;
4559 /* Get all the information about the extractions being done. If the bit size
4560 is the same as the size of the underlying object, we aren't doing an
4561 extraction at all and so can do nothing. We also don't want to
4562 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4563 then will no longer be able to replace it. */
4564 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4565 &lunsignedp
, &lreversep
, &lvolatilep
);
4567 || !known_size_p (plbitsize
)
4568 || !plbitsize
.is_constant (&lbitsize
)
4569 || !plbitpos
.is_constant (&lbitpos
)
4570 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4572 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4577 rreversep
= lreversep
;
4580 /* If this is not a constant, we can only do something if bit positions,
4581 sizes, signedness and storage order are the same. */
4583 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4584 &runsignedp
, &rreversep
, &rvolatilep
);
4587 || maybe_ne (lbitpos
, rbitpos
)
4588 || maybe_ne (lbitsize
, rbitsize
)
4589 || lunsignedp
!= runsignedp
4590 || lreversep
!= rreversep
4592 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4597 /* Honor the C++ memory model and mimic what RTL expansion does. */
4598 poly_uint64 bitstart
= 0;
4599 poly_uint64 bitend
= 0;
4600 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4602 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4603 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4607 /* See if we can find a mode to refer to this field. We should be able to,
4608 but fail if we can't. */
4609 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4610 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4611 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4612 TYPE_ALIGN (TREE_TYPE (rinner
))),
4613 BITS_PER_WORD
, false, &nmode
))
4616 /* Set signed and unsigned types of the precision of this mode for the
4618 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4620 /* Compute the bit position and size for the new reference and our offset
4621 within it. If the new reference is the same size as the original, we
4622 won't optimize anything, so return zero. */
4623 nbitsize
= GET_MODE_BITSIZE (nmode
);
4624 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4626 if (nbitsize
== lbitsize
)
4629 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4630 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4632 /* Make the mask to be used against the extracted field. */
4633 mask
= build_int_cst_type (unsigned_type
, -1);
4634 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4635 mask
= const_binop (RSHIFT_EXPR
, mask
,
4636 size_int (nbitsize
- lbitsize
- lbitpos
));
4643 /* If not comparing with constant, just rework the comparison
4645 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4646 nbitsize
, nbitpos
, 1, lreversep
);
4647 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4648 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4649 nbitsize
, nbitpos
, 1, rreversep
);
4650 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4651 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4654 /* Otherwise, we are handling the constant case. See if the constant is too
4655 big for the field. Warn and return a tree for 0 (false) if so. We do
4656 this not only for its own sake, but to avoid having to test for this
4657 error case below. If we didn't, we might generate wrong code.
4659 For unsigned fields, the constant shifted right by the field length should
4660 be all zero. For signed fields, the high-order bits should agree with
4665 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4667 warning (0, "comparison is always %d due to width of bit-field",
4669 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4674 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4675 if (tem
!= 0 && tem
!= -1)
4677 warning (0, "comparison is always %d due to width of bit-field",
4679 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4686 /* Single-bit compares should always be against zero. */
4687 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4689 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4690 rhs
= build_int_cst (type
, 0);
4693 /* Make a new bitfield reference, shift the constant over the
4694 appropriate number of bits and mask it with the computed mask
4695 (in case this was a signed field). If we changed it, make a new one. */
4696 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4697 nbitsize
, nbitpos
, 1, lreversep
);
4699 rhs
= const_binop (BIT_AND_EXPR
,
4700 const_binop (LSHIFT_EXPR
,
4701 fold_convert_loc (loc
, unsigned_type
, rhs
),
4702 size_int (lbitpos
)),
4705 lhs
= build2_loc (loc
, code
, compare_type
,
4706 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4710 /* Subroutine for fold_truth_andor_1: decode a field reference.
4712 If EXP is a comparison reference, we return the innermost reference.
4714 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4715 set to the starting bit number.
4717 If the innermost field can be completely contained in a mode-sized
4718 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4720 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4721 otherwise it is not changed.
4723 *PUNSIGNEDP is set to the signedness of the field.
4725 *PREVERSEP is set to the storage order of the field.
4727 *PMASK is set to the mask used. This is either contained in a
4728 BIT_AND_EXPR or derived from the width of the field.
4730 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4732 Return 0 if this is not a component reference or is one that we can't
4733 do anything with. */
4736 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4737 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4738 int *punsignedp
, int *preversep
, int *pvolatilep
,
4739 tree
*pmask
, tree
*pand_mask
)
4742 tree outer_type
= 0;
4744 tree mask
, inner
, offset
;
4746 unsigned int precision
;
4748 /* All the optimizations using this function assume integer fields.
4749 There are problems with FP fields since the type_for_size call
4750 below can fail for, e.g., XFmode. */
4751 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4754 /* We are interested in the bare arrangement of bits, so strip everything
4755 that doesn't affect the machine mode. However, record the type of the
4756 outermost expression if it may matter below. */
4757 if (CONVERT_EXPR_P (exp
)
4758 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4759 outer_type
= TREE_TYPE (exp
);
4762 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4764 and_mask
= TREE_OPERAND (exp
, 1);
4765 exp
= TREE_OPERAND (exp
, 0);
4766 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4767 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4771 poly_int64 poly_bitsize
, poly_bitpos
;
4772 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4773 pmode
, punsignedp
, preversep
, pvolatilep
);
4774 if ((inner
== exp
&& and_mask
== 0)
4775 || !poly_bitsize
.is_constant (pbitsize
)
4776 || !poly_bitpos
.is_constant (pbitpos
)
4779 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4780 /* Reject out-of-bound accesses (PR79731). */
4781 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4782 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4783 *pbitpos
+ *pbitsize
) < 0))
4786 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4787 if (unsigned_type
== NULL_TREE
)
4792 /* If the number of bits in the reference is the same as the bitsize of
4793 the outer type, then the outer type gives the signedness. Otherwise
4794 (in case of a small bitfield) the signedness is unchanged. */
4795 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4796 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4798 /* Compute the mask to access the bitfield. */
4799 precision
= TYPE_PRECISION (unsigned_type
);
4801 mask
= build_int_cst_type (unsigned_type
, -1);
4803 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4804 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4806 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4808 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4809 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4812 *pand_mask
= and_mask
;
4816 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4817 bit positions and MASK is SIGNED. */
4820 all_ones_mask_p (const_tree mask
, unsigned int size
)
4822 tree type
= TREE_TYPE (mask
);
4823 unsigned int precision
= TYPE_PRECISION (type
);
4825 /* If this function returns true when the type of the mask is
4826 UNSIGNED, then there will be errors. In particular see
4827 gcc.c-torture/execute/990326-1.c. There does not appear to be
4828 any documentation paper trail as to why this is so. But the pre
4829 wide-int worked with that restriction and it has been preserved
4831 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4834 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
4837 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4838 represents the sign bit of EXP's type. If EXP represents a sign
4839 or zero extension, also test VAL against the unextended type.
4840 The return value is the (sub)expression whose sign bit is VAL,
4841 or NULL_TREE otherwise. */
4844 sign_bit_p (tree exp
, const_tree val
)
4849 /* Tree EXP must have an integral type. */
4850 t
= TREE_TYPE (exp
);
4851 if (! INTEGRAL_TYPE_P (t
))
4854 /* Tree VAL must be an integer constant. */
4855 if (TREE_CODE (val
) != INTEGER_CST
4856 || TREE_OVERFLOW (val
))
4859 width
= TYPE_PRECISION (t
);
4860 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
4863 /* Handle extension from a narrower type. */
4864 if (TREE_CODE (exp
) == NOP_EXPR
4865 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4866 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4871 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4872 to be evaluated unconditionally. */
4875 simple_operand_p (const_tree exp
)
4877 /* Strip any conversions that don't change the machine mode. */
4880 return (CONSTANT_CLASS_P (exp
)
4881 || TREE_CODE (exp
) == SSA_NAME
4883 && ! TREE_ADDRESSABLE (exp
)
4884 && ! TREE_THIS_VOLATILE (exp
)
4885 && ! DECL_NONLOCAL (exp
)
4886 /* Don't regard global variables as simple. They may be
4887 allocated in ways unknown to the compiler (shared memory,
4888 #pragma weak, etc). */
4889 && ! TREE_PUBLIC (exp
)
4890 && ! DECL_EXTERNAL (exp
)
4891 /* Weakrefs are not safe to be read, since they can be NULL.
4892 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4893 have DECL_WEAK flag set. */
4894 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4895 /* Loading a static variable is unduly expensive, but global
4896 registers aren't expensive. */
4897 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4900 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4901 to be evaluated unconditionally.
4902 I addition to simple_operand_p, we assume that comparisons, conversions,
4903 and logic-not operations are simple, if their operands are simple, too. */
4906 simple_operand_p_2 (tree exp
)
4908 enum tree_code code
;
4910 if (TREE_SIDE_EFFECTS (exp
) || generic_expr_could_trap_p (exp
))
4913 while (CONVERT_EXPR_P (exp
))
4914 exp
= TREE_OPERAND (exp
, 0);
4916 code
= TREE_CODE (exp
);
4918 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4919 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4920 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4922 if (code
== TRUTH_NOT_EXPR
)
4923 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4925 return simple_operand_p (exp
);
4929 /* The following functions are subroutines to fold_range_test and allow it to
4930 try to change a logical combination of comparisons into a range test.
4933 X == 2 || X == 3 || X == 4 || X == 5
4937 (unsigned) (X - 2) <= 3
4939 We describe each set of comparisons as being either inside or outside
4940 a range, using a variable named like IN_P, and then describe the
4941 range with a lower and upper bound. If one of the bounds is omitted,
4942 it represents either the highest or lowest value of the type.
4944 In the comments below, we represent a range by two numbers in brackets
4945 preceded by a "+" to designate being inside that range, or a "-" to
4946 designate being outside that range, so the condition can be inverted by
4947 flipping the prefix. An omitted bound is represented by a "-". For
4948 example, "- [-, 10]" means being outside the range starting at the lowest
4949 possible value and ending at 10, in other words, being greater than 10.
4950 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4953 We set up things so that the missing bounds are handled in a consistent
4954 manner so neither a missing bound nor "true" and "false" need to be
4955 handled using a special case. */
4957 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4958 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4959 and UPPER1_P are nonzero if the respective argument is an upper bound
4960 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4961 must be specified for a comparison. ARG1 will be converted to ARG0's
4962 type if both are specified. */
4965 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4966 tree arg1
, int upper1_p
)
4972 /* If neither arg represents infinity, do the normal operation.
4973 Else, if not a comparison, return infinity. Else handle the special
4974 comparison rules. Note that most of the cases below won't occur, but
4975 are handled for consistency. */
4977 if (arg0
!= 0 && arg1
!= 0)
4979 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4980 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4982 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4985 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4988 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4989 for neither. In real maths, we cannot assume open ended ranges are
4990 the same. But, this is computer arithmetic, where numbers are finite.
4991 We can therefore make the transformation of any unbounded range with
4992 the value Z, Z being greater than any representable number. This permits
4993 us to treat unbounded ranges as equal. */
4994 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4995 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4999 result
= sgn0
== sgn1
;
5002 result
= sgn0
!= sgn1
;
5005 result
= sgn0
< sgn1
;
5008 result
= sgn0
<= sgn1
;
5011 result
= sgn0
> sgn1
;
5014 result
= sgn0
>= sgn1
;
5020 return constant_boolean_node (result
, type
);
5023 /* Helper routine for make_range. Perform one step for it, return
5024 new expression if the loop should continue or NULL_TREE if it should
5028 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
5029 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
5030 bool *strict_overflow_p
)
5032 tree arg0_type
= TREE_TYPE (arg0
);
5033 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
5034 int in_p
= *p_in_p
, n_in_p
;
5038 case TRUTH_NOT_EXPR
:
5039 /* We can only do something if the range is testing for zero. */
5040 if (low
== NULL_TREE
|| high
== NULL_TREE
5041 || ! integer_zerop (low
) || ! integer_zerop (high
))
5046 case EQ_EXPR
: case NE_EXPR
:
5047 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
5048 /* We can only do something if the range is testing for zero
5049 and if the second operand is an integer constant. Note that
5050 saying something is "in" the range we make is done by
5051 complementing IN_P since it will set in the initial case of
5052 being not equal to zero; "out" is leaving it alone. */
5053 if (low
== NULL_TREE
|| high
== NULL_TREE
5054 || ! integer_zerop (low
) || ! integer_zerop (high
)
5055 || TREE_CODE (arg1
) != INTEGER_CST
)
5060 case NE_EXPR
: /* - [c, c] */
5063 case EQ_EXPR
: /* + [c, c] */
5064 in_p
= ! in_p
, low
= high
= arg1
;
5066 case GT_EXPR
: /* - [-, c] */
5067 low
= 0, high
= arg1
;
5069 case GE_EXPR
: /* + [c, -] */
5070 in_p
= ! in_p
, low
= arg1
, high
= 0;
5072 case LT_EXPR
: /* - [c, -] */
5073 low
= arg1
, high
= 0;
5075 case LE_EXPR
: /* + [-, c] */
5076 in_p
= ! in_p
, low
= 0, high
= arg1
;
5082 /* If this is an unsigned comparison, we also know that EXP is
5083 greater than or equal to zero. We base the range tests we make
5084 on that fact, so we record it here so we can parse existing
5085 range tests. We test arg0_type since often the return type
5086 of, e.g. EQ_EXPR, is boolean. */
5087 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
5089 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
5091 build_int_cst (arg0_type
, 0),
5095 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
5097 /* If the high bound is missing, but we have a nonzero low
5098 bound, reverse the range so it goes from zero to the low bound
5100 if (high
== 0 && low
&& ! integer_zerop (low
))
5103 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
5104 build_int_cst (TREE_TYPE (low
), 1), 0);
5105 low
= build_int_cst (arg0_type
, 0);
5115 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5116 low and high are non-NULL, then normalize will DTRT. */
5117 if (!TYPE_UNSIGNED (arg0_type
)
5118 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5120 if (low
== NULL_TREE
)
5121 low
= TYPE_MIN_VALUE (arg0_type
);
5122 if (high
== NULL_TREE
)
5123 high
= TYPE_MAX_VALUE (arg0_type
);
5126 /* (-x) IN [a,b] -> x in [-b, -a] */
5127 n_low
= range_binop (MINUS_EXPR
, exp_type
,
5128 build_int_cst (exp_type
, 0),
5130 n_high
= range_binop (MINUS_EXPR
, exp_type
,
5131 build_int_cst (exp_type
, 0),
5133 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
5139 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
5140 build_int_cst (exp_type
, 1));
5144 if (TREE_CODE (arg1
) != INTEGER_CST
)
5147 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5148 move a constant to the other side. */
5149 if (!TYPE_UNSIGNED (arg0_type
)
5150 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5153 /* If EXP is signed, any overflow in the computation is undefined,
5154 so we don't worry about it so long as our computations on
5155 the bounds don't overflow. For unsigned, overflow is defined
5156 and this is exactly the right thing. */
5157 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5158 arg0_type
, low
, 0, arg1
, 0);
5159 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5160 arg0_type
, high
, 1, arg1
, 0);
5161 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
5162 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
5165 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5166 *strict_overflow_p
= true;
5169 /* Check for an unsigned range which has wrapped around the maximum
5170 value thus making n_high < n_low, and normalize it. */
5171 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
5173 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
5174 build_int_cst (TREE_TYPE (n_high
), 1), 0);
5175 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
5176 build_int_cst (TREE_TYPE (n_low
), 1), 0);
5178 /* If the range is of the form +/- [ x+1, x ], we won't
5179 be able to normalize it. But then, it represents the
5180 whole range or the empty set, so make it
5182 if (tree_int_cst_equal (n_low
, low
)
5183 && tree_int_cst_equal (n_high
, high
))
5189 low
= n_low
, high
= n_high
;
5197 case NON_LVALUE_EXPR
:
5198 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
5201 if (! INTEGRAL_TYPE_P (arg0_type
)
5202 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
5203 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
5206 n_low
= low
, n_high
= high
;
5209 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
5212 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
5214 /* If we're converting arg0 from an unsigned type, to exp,
5215 a signed type, we will be doing the comparison as unsigned.
5216 The tests above have already verified that LOW and HIGH
5219 So we have to ensure that we will handle large unsigned
5220 values the same way that the current signed bounds treat
5223 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
5227 /* For fixed-point modes, we need to pass the saturating flag
5228 as the 2nd parameter. */
5229 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
5231 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
5232 TYPE_SATURATING (arg0_type
));
5235 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
5237 /* A range without an upper bound is, naturally, unbounded.
5238 Since convert would have cropped a very large value, use
5239 the max value for the destination type. */
5241 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
5242 : TYPE_MAX_VALUE (arg0_type
);
5244 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
5245 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
5246 fold_convert_loc (loc
, arg0_type
,
5248 build_int_cst (arg0_type
, 1));
5250 /* If the low bound is specified, "and" the range with the
5251 range for which the original unsigned value will be
5255 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
5256 1, fold_convert_loc (loc
, arg0_type
,
5261 in_p
= (n_in_p
== in_p
);
5265 /* Otherwise, "or" the range with the range of the input
5266 that will be interpreted as negative. */
5267 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
5268 1, fold_convert_loc (loc
, arg0_type
,
5273 in_p
= (in_p
!= n_in_p
);
5277 /* Otherwise, if we are converting arg0 from signed type, to exp,
5278 an unsigned type, we will do the comparison as signed. If
5279 high is non-NULL, we punt above if it doesn't fit in the signed
5280 type, so if we get through here, +[-, high] or +[low, high] are
5281 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5282 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5283 high is not, the +[low, -] range is equivalent to union of
5284 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5285 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5286 low being 0, which should be treated as [-, -]. */
5287 else if (TYPE_UNSIGNED (exp_type
)
5288 && !TYPE_UNSIGNED (arg0_type
)
5292 if (integer_zerop (low
))
5296 n_high
= fold_build2_loc (loc
, PLUS_EXPR
, arg0_type
,
5297 n_low
, build_int_cst (arg0_type
, -1));
5298 n_low
= build_zero_cst (arg0_type
);
5313 /* Given EXP, a logical expression, set the range it is testing into
5314 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5315 actually being tested. *PLOW and *PHIGH will be made of the same
5316 type as the returned expression. If EXP is not a comparison, we
5317 will most likely not be returning a useful value and range. Set
5318 *STRICT_OVERFLOW_P to true if the return value is only valid
5319 because signed overflow is undefined; otherwise, do not change
5320 *STRICT_OVERFLOW_P. */
5323 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
5324 bool *strict_overflow_p
)
5326 enum tree_code code
;
5327 tree arg0
, arg1
= NULL_TREE
;
5328 tree exp_type
, nexp
;
5331 location_t loc
= EXPR_LOCATION (exp
);
5333 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5334 and see if we can refine the range. Some of the cases below may not
5335 happen, but it doesn't seem worth worrying about this. We "continue"
5336 the outer loop when we've changed something; otherwise we "break"
5337 the switch, which will "break" the while. */
5340 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
5344 code
= TREE_CODE (exp
);
5345 exp_type
= TREE_TYPE (exp
);
5348 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
5350 if (TREE_OPERAND_LENGTH (exp
) > 0)
5351 arg0
= TREE_OPERAND (exp
, 0);
5352 if (TREE_CODE_CLASS (code
) == tcc_binary
5353 || TREE_CODE_CLASS (code
) == tcc_comparison
5354 || (TREE_CODE_CLASS (code
) == tcc_expression
5355 && TREE_OPERAND_LENGTH (exp
) > 1))
5356 arg1
= TREE_OPERAND (exp
, 1);
5358 if (arg0
== NULL_TREE
)
5361 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
5362 &high
, &in_p
, strict_overflow_p
);
5363 if (nexp
== NULL_TREE
)
5368 /* If EXP is a constant, we can evaluate whether this is true or false. */
5369 if (TREE_CODE (exp
) == INTEGER_CST
)
5371 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
5373 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5379 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5383 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5384 a bitwise check i.e. when
5385 LOW == 0xXX...X00...0
5386 HIGH == 0xXX...X11...1
5387 Return corresponding mask in MASK and stem in VALUE. */
5390 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
5393 if (TREE_CODE (low
) != INTEGER_CST
5394 || TREE_CODE (high
) != INTEGER_CST
)
5397 unsigned prec
= TYPE_PRECISION (type
);
5398 wide_int lo
= wi::to_wide (low
, prec
);
5399 wide_int hi
= wi::to_wide (high
, prec
);
5401 wide_int end_mask
= lo
^ hi
;
5402 if ((end_mask
& (end_mask
+ 1)) != 0
5403 || (lo
& end_mask
) != 0)
5406 wide_int stem_mask
= ~end_mask
;
5407 wide_int stem
= lo
& stem_mask
;
5408 if (stem
!= (hi
& stem_mask
))
5411 *mask
= wide_int_to_tree (type
, stem_mask
);
5412 *value
= wide_int_to_tree (type
, stem
);
5417 /* Helper routine for build_range_check and match.pd. Return the type to
5418 perform the check or NULL if it shouldn't be optimized. */
5421 range_check_type (tree etype
)
5423 /* First make sure that arithmetics in this type is valid, then make sure
5424 that it wraps around. */
5425 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
5426 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
), 1);
5428 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_UNSIGNED (etype
))
5430 tree utype
, minv
, maxv
;
5432 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5433 for the type in question, as we rely on this here. */
5434 utype
= unsigned_type_for (etype
);
5435 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
5436 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
5437 build_int_cst (TREE_TYPE (maxv
), 1), 1);
5438 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
5440 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
5446 else if (POINTER_TYPE_P (etype
) || TREE_CODE (etype
) == OFFSET_TYPE
)
5447 etype
= unsigned_type_for (etype
);
5451 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5452 type, TYPE, return an expression to test if EXP is in (or out of, depending
5453 on IN_P) the range. Return 0 if the test couldn't be created. */
5456 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
5457 tree low
, tree high
)
5459 tree etype
= TREE_TYPE (exp
), mask
, value
;
5461 /* Disable this optimization for function pointer expressions
5462 on targets that require function pointer canonicalization. */
5463 if (targetm
.have_canonicalize_funcptr_for_compare ()
5464 && POINTER_TYPE_P (etype
)
5465 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype
)))
5470 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
5472 return invert_truthvalue_loc (loc
, value
);
5477 if (low
== 0 && high
== 0)
5478 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
5481 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
5482 fold_convert_loc (loc
, etype
, high
));
5485 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
5486 fold_convert_loc (loc
, etype
, low
));
5488 if (operand_equal_p (low
, high
, 0))
5489 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
5490 fold_convert_loc (loc
, etype
, low
));
5492 if (TREE_CODE (exp
) == BIT_AND_EXPR
5493 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
5494 return fold_build2_loc (loc
, EQ_EXPR
, type
,
5495 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
5499 if (integer_zerop (low
))
5501 if (! TYPE_UNSIGNED (etype
))
5503 etype
= unsigned_type_for (etype
);
5504 high
= fold_convert_loc (loc
, etype
, high
);
5505 exp
= fold_convert_loc (loc
, etype
, exp
);
5507 return build_range_check (loc
, type
, exp
, 1, 0, high
);
5510 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5511 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
5513 int prec
= TYPE_PRECISION (etype
);
5515 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
5517 if (TYPE_UNSIGNED (etype
))
5519 tree signed_etype
= signed_type_for (etype
);
5520 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
5522 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
5524 etype
= signed_etype
;
5525 exp
= fold_convert_loc (loc
, etype
, exp
);
5527 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
5528 build_int_cst (etype
, 0));
5532 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5533 This requires wrap-around arithmetics for the type of the expression. */
5534 etype
= range_check_type (etype
);
5535 if (etype
== NULL_TREE
)
5538 high
= fold_convert_loc (loc
, etype
, high
);
5539 low
= fold_convert_loc (loc
, etype
, low
);
5540 exp
= fold_convert_loc (loc
, etype
, exp
);
5542 value
= const_binop (MINUS_EXPR
, high
, low
);
5544 if (value
!= 0 && !TREE_OVERFLOW (value
))
5545 return build_range_check (loc
, type
,
5546 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5547 1, build_int_cst (etype
, 0), value
);
5552 /* Return the predecessor of VAL in its type, handling the infinite case. */
5555 range_predecessor (tree val
)
5557 tree type
= TREE_TYPE (val
);
5559 if (INTEGRAL_TYPE_P (type
)
5560 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5563 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5564 build_int_cst (TREE_TYPE (val
), 1), 0);
5567 /* Return the successor of VAL in its type, handling the infinite case. */
5570 range_successor (tree val
)
5572 tree type
= TREE_TYPE (val
);
5574 if (INTEGRAL_TYPE_P (type
)
5575 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5578 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5579 build_int_cst (TREE_TYPE (val
), 1), 0);
5582 /* Given two ranges, see if we can merge them into one. Return 1 if we
5583 can, 0 if we can't. Set the output range into the specified parameters. */
5586 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5587 tree high0
, int in1_p
, tree low1
, tree high1
)
5595 int lowequal
= ((low0
== 0 && low1
== 0)
5596 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5597 low0
, 0, low1
, 0)));
5598 int highequal
= ((high0
== 0 && high1
== 0)
5599 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5600 high0
, 1, high1
, 1)));
5602 /* Make range 0 be the range that starts first, or ends last if they
5603 start at the same value. Swap them if it isn't. */
5604 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5607 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5608 high1
, 1, high0
, 1))))
5610 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5611 tem
= low0
, low0
= low1
, low1
= tem
;
5612 tem
= high0
, high0
= high1
, high1
= tem
;
5615 /* If the second range is != high1 where high1 is the type maximum of
5616 the type, try first merging with < high1 range. */
5619 && TREE_CODE (low1
) == INTEGER_CST
5620 && (TREE_CODE (TREE_TYPE (low1
)) == INTEGER_TYPE
5621 || (TREE_CODE (TREE_TYPE (low1
)) == ENUMERAL_TYPE
5622 && known_eq (TYPE_PRECISION (TREE_TYPE (low1
)),
5623 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1
))))))
5624 && operand_equal_p (low1
, high1
, 0))
5626 if (tree_int_cst_equal (low1
, TYPE_MAX_VALUE (TREE_TYPE (low1
)))
5627 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5628 !in1_p
, NULL_TREE
, range_predecessor (low1
)))
5630 /* Similarly for the second range != low1 where low1 is the type minimum
5631 of the type, try first merging with > low1 range. */
5632 if (tree_int_cst_equal (low1
, TYPE_MIN_VALUE (TREE_TYPE (low1
)))
5633 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5634 !in1_p
, range_successor (low1
), NULL_TREE
))
5638 /* Now flag two cases, whether the ranges are disjoint or whether the
5639 second range is totally subsumed in the first. Note that the tests
5640 below are simplified by the ones above. */
5641 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5642 high0
, 1, low1
, 0));
5643 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5644 high1
, 1, high0
, 1));
5646 /* We now have four cases, depending on whether we are including or
5647 excluding the two ranges. */
5650 /* If they don't overlap, the result is false. If the second range
5651 is a subset it is the result. Otherwise, the range is from the start
5652 of the second to the end of the first. */
5654 in_p
= 0, low
= high
= 0;
5656 in_p
= 1, low
= low1
, high
= high1
;
5658 in_p
= 1, low
= low1
, high
= high0
;
5661 else if (in0_p
&& ! in1_p
)
5663 /* If they don't overlap, the result is the first range. If they are
5664 equal, the result is false. If the second range is a subset of the
5665 first, and the ranges begin at the same place, we go from just after
5666 the end of the second range to the end of the first. If the second
5667 range is not a subset of the first, or if it is a subset and both
5668 ranges end at the same place, the range starts at the start of the
5669 first range and ends just before the second range.
5670 Otherwise, we can't describe this as a single range. */
5672 in_p
= 1, low
= low0
, high
= high0
;
5673 else if (lowequal
&& highequal
)
5674 in_p
= 0, low
= high
= 0;
5675 else if (subset
&& lowequal
)
5677 low
= range_successor (high1
);
5682 /* We are in the weird situation where high0 > high1 but
5683 high1 has no successor. Punt. */
5687 else if (! subset
|| highequal
)
5690 high
= range_predecessor (low1
);
5694 /* low0 < low1 but low1 has no predecessor. Punt. */
5702 else if (! in0_p
&& in1_p
)
5704 /* If they don't overlap, the result is the second range. If the second
5705 is a subset of the first, the result is false. Otherwise,
5706 the range starts just after the first range and ends at the
5707 end of the second. */
5709 in_p
= 1, low
= low1
, high
= high1
;
5710 else if (subset
|| highequal
)
5711 in_p
= 0, low
= high
= 0;
5714 low
= range_successor (high0
);
5719 /* high1 > high0 but high0 has no successor. Punt. */
5727 /* The case where we are excluding both ranges. Here the complex case
5728 is if they don't overlap. In that case, the only time we have a
5729 range is if they are adjacent. If the second is a subset of the
5730 first, the result is the first. Otherwise, the range to exclude
5731 starts at the beginning of the first range and ends at the end of the
5735 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5736 range_successor (high0
),
5738 in_p
= 0, low
= low0
, high
= high1
;
5741 /* Canonicalize - [min, x] into - [-, x]. */
5742 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5743 switch (TREE_CODE (TREE_TYPE (low0
)))
5746 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5748 (TYPE_MODE (TREE_TYPE (low0
)))))
5752 if (tree_int_cst_equal (low0
,
5753 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5757 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5758 && integer_zerop (low0
))
5765 /* Canonicalize - [x, max] into - [x, -]. */
5766 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5767 switch (TREE_CODE (TREE_TYPE (high1
)))
5770 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5772 (TYPE_MODE (TREE_TYPE (high1
)))))
5776 if (tree_int_cst_equal (high1
,
5777 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5781 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5782 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5784 build_int_cst (TREE_TYPE (high1
), 1),
5792 /* The ranges might be also adjacent between the maximum and
5793 minimum values of the given type. For
5794 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5795 return + [x + 1, y - 1]. */
5796 if (low0
== 0 && high1
== 0)
5798 low
= range_successor (high0
);
5799 high
= range_predecessor (low1
);
5800 if (low
== 0 || high
== 0)
5810 in_p
= 0, low
= low0
, high
= high0
;
5812 in_p
= 0, low
= low0
, high
= high1
;
5815 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5820 /* Subroutine of fold, looking inside expressions of the form
5821 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5822 are the three operands of the COND_EXPR. This function is
5823 being used also to optimize A op B ? C : A, by reversing the
5826 Return a folded expression whose code is not a COND_EXPR
5827 anymore, or NULL_TREE if no folding opportunity is found. */
5830 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5831 enum tree_code comp_code
,
5832 tree arg00
, tree arg01
, tree arg1
, tree arg2
)
5834 tree arg1_type
= TREE_TYPE (arg1
);
5840 /* If we have A op 0 ? A : -A, consider applying the following
5843 A == 0? A : -A same as -A
5844 A != 0? A : -A same as A
5845 A >= 0? A : -A same as abs (A)
5846 A > 0? A : -A same as abs (A)
5847 A <= 0? A : -A same as -abs (A)
5848 A < 0? A : -A same as -abs (A)
5850 None of these transformations work for modes with signed
5851 zeros. If A is +/-0, the first two transformations will
5852 change the sign of the result (from +0 to -0, or vice
5853 versa). The last four will fix the sign of the result,
5854 even though the original expressions could be positive or
5855 negative, depending on the sign of A.
5857 Note that all these transformations are correct if A is
5858 NaN, since the two alternatives (A and -A) are also NaNs. */
5859 if (!HONOR_SIGNED_ZEROS (type
)
5860 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5861 ? real_zerop (arg01
)
5862 : integer_zerop (arg01
))
5863 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5864 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5865 /* In the case that A is of the form X-Y, '-A' (arg2) may
5866 have already been folded to Y-X, check for that. */
5867 || (TREE_CODE (arg1
) == MINUS_EXPR
5868 && TREE_CODE (arg2
) == MINUS_EXPR
5869 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5870 TREE_OPERAND (arg2
, 1), 0)
5871 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5872 TREE_OPERAND (arg2
, 0), 0))))
5877 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5878 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5881 return fold_convert_loc (loc
, type
, arg1
);
5884 if (flag_trapping_math
)
5889 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5891 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5892 return fold_convert_loc (loc
, type
, tem
);
5895 if (flag_trapping_math
)
5900 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5902 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
5903 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
5905 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
5906 is not, invokes UB both in abs and in the negation of it.
5907 So, use ABSU_EXPR instead. */
5908 tree utype
= unsigned_type_for (TREE_TYPE (arg1
));
5909 tem
= fold_build1_loc (loc
, ABSU_EXPR
, utype
, arg1
);
5910 tem
= negate_expr (tem
);
5911 return fold_convert_loc (loc
, type
, tem
);
5915 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5916 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5919 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5923 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5924 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5925 both transformations are correct when A is NaN: A != 0
5926 is then true, and A == 0 is false. */
5928 if (!HONOR_SIGNED_ZEROS (type
)
5929 && integer_zerop (arg01
) && integer_zerop (arg2
))
5931 if (comp_code
== NE_EXPR
)
5932 return fold_convert_loc (loc
, type
, arg1
);
5933 else if (comp_code
== EQ_EXPR
)
5934 return build_zero_cst (type
);
5937 /* Try some transformations of A op B ? A : B.
5939 A == B? A : B same as B
5940 A != B? A : B same as A
5941 A >= B? A : B same as max (A, B)
5942 A > B? A : B same as max (B, A)
5943 A <= B? A : B same as min (A, B)
5944 A < B? A : B same as min (B, A)
5946 As above, these transformations don't work in the presence
5947 of signed zeros. For example, if A and B are zeros of
5948 opposite sign, the first two transformations will change
5949 the sign of the result. In the last four, the original
5950 expressions give different results for (A=+0, B=-0) and
5951 (A=-0, B=+0), but the transformed expressions do not.
5953 The first two transformations are correct if either A or B
5954 is a NaN. In the first transformation, the condition will
5955 be false, and B will indeed be chosen. In the case of the
5956 second transformation, the condition A != B will be true,
5957 and A will be chosen.
5959 The conversions to max() and min() are not correct if B is
5960 a number and A is not. The conditions in the original
5961 expressions will be false, so all four give B. The min()
5962 and max() versions would give a NaN instead. */
5963 if (!HONOR_SIGNED_ZEROS (type
)
5964 && operand_equal_for_comparison_p (arg01
, arg2
)
5965 /* Avoid these transformations if the COND_EXPR may be used
5966 as an lvalue in the C++ front-end. PR c++/19199. */
5968 || VECTOR_TYPE_P (type
)
5969 || (! lang_GNU_CXX ()
5970 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5971 || ! maybe_lvalue_p (arg1
)
5972 || ! maybe_lvalue_p (arg2
)))
5974 tree comp_op0
= arg00
;
5975 tree comp_op1
= arg01
;
5976 tree comp_type
= TREE_TYPE (comp_op0
);
5981 return fold_convert_loc (loc
, type
, arg2
);
5983 return fold_convert_loc (loc
, type
, arg1
);
5988 /* In C++ a ?: expression can be an lvalue, so put the
5989 operand which will be used if they are equal first
5990 so that we can convert this back to the
5991 corresponding COND_EXPR. */
5992 if (!HONOR_NANS (arg1
))
5994 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5995 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5996 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5997 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5998 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5999 comp_op1
, comp_op0
);
6000 return fold_convert_loc (loc
, type
, tem
);
6007 if (!HONOR_NANS (arg1
))
6009 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6010 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6011 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
6012 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
6013 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
6014 comp_op1
, comp_op0
);
6015 return fold_convert_loc (loc
, type
, tem
);
6019 if (!HONOR_NANS (arg1
))
6020 return fold_convert_loc (loc
, type
, arg2
);
6023 if (!HONOR_NANS (arg1
))
6024 return fold_convert_loc (loc
, type
, arg1
);
6027 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
6037 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6038 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6039 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6043 /* EXP is some logical combination of boolean tests. See if we can
6044 merge it into some range test. Return the new tree if so. */
6047 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
6050 int or_op
= (code
== TRUTH_ORIF_EXPR
6051 || code
== TRUTH_OR_EXPR
);
6052 int in0_p
, in1_p
, in_p
;
6053 tree low0
, low1
, low
, high0
, high1
, high
;
6054 bool strict_overflow_p
= false;
6056 const char * const warnmsg
= G_("assuming signed overflow does not occur "
6057 "when simplifying range test");
6059 if (!INTEGRAL_TYPE_P (type
))
6062 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
6063 /* If op0 is known true or false and this is a short-circuiting
6064 operation we must not merge with op1 since that makes side-effects
6065 unconditional. So special-case this. */
6067 && ((code
== TRUTH_ORIF_EXPR
&& in0_p
)
6068 || (code
== TRUTH_ANDIF_EXPR
&& !in0_p
)))
6070 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
6072 /* If this is an OR operation, invert both sides; we will invert
6073 again at the end. */
6075 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
6077 /* If both expressions are the same, if we can merge the ranges, and we
6078 can build the range test, return it or it inverted. If one of the
6079 ranges is always true or always false, consider it to be the same
6080 expression as the other. */
6081 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
6082 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
6084 && (tem
= (build_range_check (loc
, type
,
6086 : rhs
!= 0 ? rhs
: integer_zero_node
,
6087 in_p
, low
, high
))) != 0)
6089 if (strict_overflow_p
)
6090 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
6091 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
6094 /* On machines where the branch cost is expensive, if this is a
6095 short-circuited branch and the underlying object on both sides
6096 is the same, make a non-short-circuit operation. */
6097 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
6098 if (param_logical_op_non_short_circuit
!= -1)
6099 logical_op_non_short_circuit
6100 = param_logical_op_non_short_circuit
;
6101 if (logical_op_non_short_circuit
6102 && !sanitize_coverage_p ()
6103 && lhs
!= 0 && rhs
!= 0
6104 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6105 && operand_equal_p (lhs
, rhs
, 0))
6107 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6108 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6109 which cases we can't do this. */
6110 if (simple_operand_p (lhs
))
6111 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6112 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6115 else if (!lang_hooks
.decls
.global_bindings_p ()
6116 && !CONTAINS_PLACEHOLDER_P (lhs
))
6118 tree common
= save_expr (lhs
);
6120 if ((lhs
= build_range_check (loc
, type
, common
,
6121 or_op
? ! in0_p
: in0_p
,
6123 && (rhs
= build_range_check (loc
, type
, common
,
6124 or_op
? ! in1_p
: in1_p
,
6127 if (strict_overflow_p
)
6128 fold_overflow_warning (warnmsg
,
6129 WARN_STRICT_OVERFLOW_COMPARISON
);
6130 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6131 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6140 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6141 bit value. Arrange things so the extra bits will be set to zero if and
6142 only if C is signed-extended to its full width. If MASK is nonzero,
6143 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6146 unextend (tree c
, int p
, int unsignedp
, tree mask
)
6148 tree type
= TREE_TYPE (c
);
6149 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
6152 if (p
== modesize
|| unsignedp
)
6155 /* We work by getting just the sign bit into the low-order bit, then
6156 into the high-order bit, then sign-extend. We then XOR that value
6158 temp
= build_int_cst (TREE_TYPE (c
),
6159 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
6161 /* We must use a signed type in order to get an arithmetic right shift.
6162 However, we must also avoid introducing accidental overflows, so that
6163 a subsequent call to integer_zerop will work. Hence we must
6164 do the type conversion here. At this point, the constant is either
6165 zero or one, and the conversion to a signed type can never overflow.
6166 We could get an overflow if this conversion is done anywhere else. */
6167 if (TYPE_UNSIGNED (type
))
6168 temp
= fold_convert (signed_type_for (type
), temp
);
6170 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
6171 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
6173 temp
= const_binop (BIT_AND_EXPR
, temp
,
6174 fold_convert (TREE_TYPE (c
), mask
));
6175 /* If necessary, convert the type back to match the type of C. */
6176 if (TYPE_UNSIGNED (type
))
6177 temp
= fold_convert (type
, temp
);
6179 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
6182 /* For an expression that has the form
6186 we can drop one of the inner expressions and simplify to
6190 LOC is the location of the resulting expression. OP is the inner
6191 logical operation; the left-hand side in the examples above, while CMPOP
6192 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6193 removing a condition that guards another, as in
6194 (A != NULL && A->...) || A == NULL
6195 which we must not transform. If RHS_ONLY is true, only eliminate the
6196 right-most operand of the inner logical operation. */
6199 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
6202 tree type
= TREE_TYPE (cmpop
);
6203 enum tree_code code
= TREE_CODE (cmpop
);
6204 enum tree_code truthop_code
= TREE_CODE (op
);
6205 tree lhs
= TREE_OPERAND (op
, 0);
6206 tree rhs
= TREE_OPERAND (op
, 1);
6207 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
6208 enum tree_code rhs_code
= TREE_CODE (rhs
);
6209 enum tree_code lhs_code
= TREE_CODE (lhs
);
6210 enum tree_code inv_code
;
6212 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
6215 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
6218 if (rhs_code
== truthop_code
)
6220 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
6221 if (newrhs
!= NULL_TREE
)
6224 rhs_code
= TREE_CODE (rhs
);
6227 if (lhs_code
== truthop_code
&& !rhs_only
)
6229 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
6230 if (newlhs
!= NULL_TREE
)
6233 lhs_code
= TREE_CODE (lhs
);
6237 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
6238 if (inv_code
== rhs_code
6239 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6240 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6242 if (!rhs_only
&& inv_code
== lhs_code
6243 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6244 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6246 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
6247 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
6252 /* Find ways of folding logical expressions of LHS and RHS:
6253 Try to merge two comparisons to the same innermost item.
6254 Look for range tests like "ch >= '0' && ch <= '9'".
6255 Look for combinations of simple terms on machines with expensive branches
6256 and evaluate the RHS unconditionally.
6258 For example, if we have p->a == 2 && p->b == 4 and we can make an
6259 object large enough to span both A and B, we can do this with a comparison
6260 against the object ANDed with the a mask.
6262 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6263 operations to do this with one comparison.
6265 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6266 function and the one above.
6268 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6269 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6271 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6274 We return the simplified tree or 0 if no optimization is possible. */
6277 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
6280 /* If this is the "or" of two comparisons, we can do something if
6281 the comparisons are NE_EXPR. If this is the "and", we can do something
6282 if the comparisons are EQ_EXPR. I.e.,
6283 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6285 WANTED_CODE is this operation code. For single bit fields, we can
6286 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6287 comparison for one-bit fields. */
6289 enum tree_code wanted_code
;
6290 enum tree_code lcode
, rcode
;
6291 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
6292 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
6293 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
6294 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
6295 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
6296 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
6297 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
6298 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
6299 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
6300 scalar_int_mode lnmode
, rnmode
;
6301 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
6302 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
6303 tree l_const
, r_const
;
6304 tree lntype
, rntype
, result
;
6305 HOST_WIDE_INT first_bit
, end_bit
;
6308 /* Start by getting the comparison codes. Fail if anything is volatile.
6309 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6310 it were surrounded with a NE_EXPR. */
6312 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
6315 lcode
= TREE_CODE (lhs
);
6316 rcode
= TREE_CODE (rhs
);
6318 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
6320 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
6321 build_int_cst (TREE_TYPE (lhs
), 0));
6325 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
6327 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
6328 build_int_cst (TREE_TYPE (rhs
), 0));
6332 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
6333 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
6336 ll_arg
= TREE_OPERAND (lhs
, 0);
6337 lr_arg
= TREE_OPERAND (lhs
, 1);
6338 rl_arg
= TREE_OPERAND (rhs
, 0);
6339 rr_arg
= TREE_OPERAND (rhs
, 1);
6341 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6342 if (simple_operand_p (ll_arg
)
6343 && simple_operand_p (lr_arg
))
6345 if (operand_equal_p (ll_arg
, rl_arg
, 0)
6346 && operand_equal_p (lr_arg
, rr_arg
, 0))
6348 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
6349 truth_type
, ll_arg
, lr_arg
);
6353 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
6354 && operand_equal_p (lr_arg
, rl_arg
, 0))
6356 result
= combine_comparisons (loc
, code
, lcode
,
6357 swap_tree_comparison (rcode
),
6358 truth_type
, ll_arg
, lr_arg
);
6364 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
6365 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
6367 /* If the RHS can be evaluated unconditionally and its operands are
6368 simple, it wins to evaluate the RHS unconditionally on machines
6369 with expensive branches. In this case, this isn't a comparison
6370 that can be merged. */
6372 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
6374 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
6375 && simple_operand_p (rl_arg
)
6376 && simple_operand_p (rr_arg
))
6378 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6379 if (code
== TRUTH_OR_EXPR
6380 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
6381 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
6382 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6383 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6384 return build2_loc (loc
, NE_EXPR
, truth_type
,
6385 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6387 build_int_cst (TREE_TYPE (ll_arg
), 0));
6389 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6390 if (code
== TRUTH_AND_EXPR
6391 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
6392 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
6393 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6394 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6395 return build2_loc (loc
, EQ_EXPR
, truth_type
,
6396 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6398 build_int_cst (TREE_TYPE (ll_arg
), 0));
6401 /* See if the comparisons can be merged. Then get all the parameters for
6404 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
6405 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
6408 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
6410 ll_inner
= decode_field_reference (loc
, &ll_arg
,
6411 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
6412 &ll_unsignedp
, &ll_reversep
, &volatilep
,
6413 &ll_mask
, &ll_and_mask
);
6414 lr_inner
= decode_field_reference (loc
, &lr_arg
,
6415 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
6416 &lr_unsignedp
, &lr_reversep
, &volatilep
,
6417 &lr_mask
, &lr_and_mask
);
6418 rl_inner
= decode_field_reference (loc
, &rl_arg
,
6419 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
6420 &rl_unsignedp
, &rl_reversep
, &volatilep
,
6421 &rl_mask
, &rl_and_mask
);
6422 rr_inner
= decode_field_reference (loc
, &rr_arg
,
6423 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
6424 &rr_unsignedp
, &rr_reversep
, &volatilep
,
6425 &rr_mask
, &rr_and_mask
);
6427 /* It must be true that the inner operation on the lhs of each
6428 comparison must be the same if we are to be able to do anything.
6429 Then see if we have constants. If not, the same must be true for
6432 || ll_reversep
!= rl_reversep
6433 || ll_inner
== 0 || rl_inner
== 0
6434 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
6437 if (TREE_CODE (lr_arg
) == INTEGER_CST
6438 && TREE_CODE (rr_arg
) == INTEGER_CST
)
6440 l_const
= lr_arg
, r_const
= rr_arg
;
6441 lr_reversep
= ll_reversep
;
6443 else if (lr_reversep
!= rr_reversep
6444 || lr_inner
== 0 || rr_inner
== 0
6445 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
6448 l_const
= r_const
= 0;
6450 /* If either comparison code is not correct for our logical operation,
6451 fail. However, we can convert a one-bit comparison against zero into
6452 the opposite comparison against that bit being set in the field. */
6454 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
6455 if (lcode
!= wanted_code
)
6457 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
6459 /* Make the left operand unsigned, since we are only interested
6460 in the value of one bit. Otherwise we are doing the wrong
6469 /* This is analogous to the code for l_const above. */
6470 if (rcode
!= wanted_code
)
6472 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
6481 /* See if we can find a mode that contains both fields being compared on
6482 the left. If we can't, fail. Otherwise, update all constants and masks
6483 to be relative to a field of that size. */
6484 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
6485 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
6486 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6487 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
6488 volatilep
, &lnmode
))
6491 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
6492 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
6493 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
6494 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
6496 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6498 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
6499 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
6502 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
6503 size_int (xll_bitpos
));
6504 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
6505 size_int (xrl_bitpos
));
6506 if (ll_mask
== NULL_TREE
|| rl_mask
== NULL_TREE
)
6511 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
6512 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
6513 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
6514 if (l_const
== NULL_TREE
)
6516 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
6517 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6520 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6522 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6527 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
6528 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
6529 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
6530 if (r_const
== NULL_TREE
)
6532 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
6533 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6536 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6538 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6542 /* If the right sides are not constant, do the same for it. Also,
6543 disallow this optimization if a size, signedness or storage order
6544 mismatch occurs between the left and right sides. */
6547 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
6548 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
6549 || ll_reversep
!= lr_reversep
6550 /* Make sure the two fields on the right
6551 correspond to the left without being swapped. */
6552 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
6555 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
6556 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
6557 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6558 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
6559 volatilep
, &rnmode
))
6562 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
6563 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
6564 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
6565 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
6567 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6569 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
6570 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
6573 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6575 size_int (xlr_bitpos
));
6576 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6578 size_int (xrr_bitpos
));
6579 if (lr_mask
== NULL_TREE
|| rr_mask
== NULL_TREE
)
6582 /* Make a mask that corresponds to both fields being compared.
6583 Do this for both items being compared. If the operands are the
6584 same size and the bits being compared are in the same position
6585 then we can do this by masking both and comparing the masked
6587 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6588 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
6589 if (lnbitsize
== rnbitsize
6590 && xll_bitpos
== xlr_bitpos
6594 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6595 lntype
, lnbitsize
, lnbitpos
,
6596 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6597 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6598 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6600 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6601 rntype
, rnbitsize
, rnbitpos
,
6602 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6603 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6604 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6606 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6609 /* There is still another way we can do something: If both pairs of
6610 fields being compared are adjacent, we may be able to make a wider
6611 field containing them both.
6613 Note that we still must mask the lhs/rhs expressions. Furthermore,
6614 the mask must be shifted to account for the shift done by
6615 make_bit_field_ref. */
6616 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6617 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6618 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6619 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6627 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6628 ll_bitsize
+ rl_bitsize
,
6629 MIN (ll_bitpos
, rl_bitpos
),
6630 ll_unsignedp
, ll_reversep
);
6631 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6632 lr_bitsize
+ rr_bitsize
,
6633 MIN (lr_bitpos
, rr_bitpos
),
6634 lr_unsignedp
, lr_reversep
);
6636 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6637 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6638 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6639 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6640 if (ll_mask
== NULL_TREE
|| lr_mask
== NULL_TREE
)
6643 /* Convert to the smaller type before masking out unwanted bits. */
6645 if (lntype
!= rntype
)
6647 if (lnbitsize
> rnbitsize
)
6649 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6650 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6653 else if (lnbitsize
< rnbitsize
)
6655 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6656 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6661 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6662 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6664 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6665 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6667 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6673 /* Handle the case of comparisons with constants. If there is something in
6674 common between the masks, those bits of the constants must be the same.
6675 If not, the condition is always false. Test for this to avoid generating
6676 incorrect code below. */
6677 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6678 if (! integer_zerop (result
)
6679 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6680 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6682 if (wanted_code
== NE_EXPR
)
6684 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6685 return constant_boolean_node (true, truth_type
);
6689 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6690 return constant_boolean_node (false, truth_type
);
6697 /* Construct the expression we will return. First get the component
6698 reference we will make. Unless the mask is all ones the width of
6699 that field, perform the mask operation. Then compare with the
6701 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6702 lntype
, lnbitsize
, lnbitpos
,
6703 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6705 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6706 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6707 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6709 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6710 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6713 /* T is an integer expression that is being multiplied, divided, or taken a
6714 modulus (CODE says which and what kind of divide or modulus) by a
6715 constant C. See if we can eliminate that operation by folding it with
6716 other operations already in T. WIDE_TYPE, if non-null, is a type that
6717 should be used for the computation if wider than our type.
6719 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6720 (X * 2) + (Y * 4). We must, however, be assured that either the original
6721 expression would not overflow or that overflow is undefined for the type
6722 in the language in question.
6724 If we return a non-null expression, it is an equivalent form of the
6725 original computation, but need not be in the original type.
6727 We set *STRICT_OVERFLOW_P to true if the return values depends on
6728 signed overflow being undefined. Otherwise we do not change
6729 *STRICT_OVERFLOW_P. */
6732 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6733 bool *strict_overflow_p
)
6735 /* To avoid exponential search depth, refuse to allow recursion past
6736 three levels. Beyond that (1) it's highly unlikely that we'll find
6737 something interesting and (2) we've probably processed it before
6738 when we built the inner expression. */
6747 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6754 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6755 bool *strict_overflow_p
)
6757 tree type
= TREE_TYPE (t
);
6758 enum tree_code tcode
= TREE_CODE (t
);
6759 tree ctype
= (wide_type
!= 0
6760 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6761 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6762 ? wide_type
: type
);
6764 int same_p
= tcode
== code
;
6765 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6766 bool sub_strict_overflow_p
;
6768 /* Don't deal with constants of zero here; they confuse the code below. */
6769 if (integer_zerop (c
))
6772 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6773 op0
= TREE_OPERAND (t
, 0);
6775 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6776 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6778 /* Note that we need not handle conditional operations here since fold
6779 already handles those cases. So just do arithmetic here. */
6783 /* For a constant, we can always simplify if we are a multiply
6784 or (for divide and modulus) if it is a multiple of our constant. */
6785 if (code
== MULT_EXPR
6786 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6789 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6790 fold_convert (ctype
, c
));
6791 /* If the multiplication overflowed, we lost information on it.
6792 See PR68142 and PR69845. */
6793 if (TREE_OVERFLOW (tem
))
6799 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6800 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
6802 /* If op0 is an expression ... */
6803 if ((COMPARISON_CLASS_P (op0
)
6804 || UNARY_CLASS_P (op0
)
6805 || BINARY_CLASS_P (op0
)
6806 || VL_EXP_CLASS_P (op0
)
6807 || EXPRESSION_CLASS_P (op0
))
6808 /* ... and has wrapping overflow, and its type is smaller
6809 than ctype, then we cannot pass through as widening. */
6810 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
6811 && (TYPE_PRECISION (ctype
)
6812 > TYPE_PRECISION (TREE_TYPE (op0
))))
6813 /* ... or this is a truncation (t is narrower than op0),
6814 then we cannot pass through this narrowing. */
6815 || (TYPE_PRECISION (type
)
6816 < TYPE_PRECISION (TREE_TYPE (op0
)))
6817 /* ... or signedness changes for division or modulus,
6818 then we cannot pass through this conversion. */
6819 || (code
!= MULT_EXPR
6820 && (TYPE_UNSIGNED (ctype
)
6821 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6822 /* ... or has undefined overflow while the converted to
6823 type has not, we cannot do the operation in the inner type
6824 as that would introduce undefined overflow. */
6825 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
6826 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6829 /* Pass the constant down and see if we can make a simplification. If
6830 we can, replace this expression with the inner simplification for
6831 possible later conversion to our or some other type. */
6832 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6833 && TREE_CODE (t2
) == INTEGER_CST
6834 && !TREE_OVERFLOW (t2
)
6835 && (t1
= extract_muldiv (op0
, t2
, code
,
6836 code
== MULT_EXPR
? ctype
: NULL_TREE
,
6837 strict_overflow_p
)) != 0)
6842 /* If widening the type changes it from signed to unsigned, then we
6843 must avoid building ABS_EXPR itself as unsigned. */
6844 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6846 tree cstype
= (*signed_type_for
) (ctype
);
6847 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6850 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6851 return fold_convert (ctype
, t1
);
6855 /* If the constant is negative, we cannot simplify this. */
6856 if (tree_int_cst_sgn (c
) == -1)
6860 /* For division and modulus, type can't be unsigned, as e.g.
6861 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6862 For signed types, even with wrapping overflow, this is fine. */
6863 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6865 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6867 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6870 case MIN_EXPR
: case MAX_EXPR
:
6871 /* If widening the type changes the signedness, then we can't perform
6872 this optimization as that changes the result. */
6873 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6876 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6877 sub_strict_overflow_p
= false;
6878 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6879 &sub_strict_overflow_p
)) != 0
6880 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6881 &sub_strict_overflow_p
)) != 0)
6883 if (tree_int_cst_sgn (c
) < 0)
6884 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6885 if (sub_strict_overflow_p
)
6886 *strict_overflow_p
= true;
6887 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6888 fold_convert (ctype
, t2
));
6892 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6893 /* If the second operand is constant, this is a multiplication
6894 or floor division, by a power of two, so we can treat it that
6895 way unless the multiplier or divisor overflows. Signed
6896 left-shift overflow is implementation-defined rather than
6897 undefined in C90, so do not convert signed left shift into
6899 if (TREE_CODE (op1
) == INTEGER_CST
6900 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6901 /* const_binop may not detect overflow correctly,
6902 so check for it explicitly here. */
6903 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
6905 && (t1
= fold_convert (ctype
,
6906 const_binop (LSHIFT_EXPR
, size_one_node
,
6908 && !TREE_OVERFLOW (t1
))
6909 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6910 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6912 fold_convert (ctype
, op0
),
6914 c
, code
, wide_type
, strict_overflow_p
);
6917 case PLUS_EXPR
: case MINUS_EXPR
:
6918 /* See if we can eliminate the operation on both sides. If we can, we
6919 can return a new PLUS or MINUS. If we can't, the only remaining
6920 cases where we can do anything are if the second operand is a
6922 sub_strict_overflow_p
= false;
6923 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6924 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6925 if (t1
!= 0 && t2
!= 0
6926 && TYPE_OVERFLOW_WRAPS (ctype
)
6927 && (code
== MULT_EXPR
6928 /* If not multiplication, we can only do this if both operands
6929 are divisible by c. */
6930 || (multiple_of_p (ctype
, op0
, c
)
6931 && multiple_of_p (ctype
, op1
, c
))))
6933 if (sub_strict_overflow_p
)
6934 *strict_overflow_p
= true;
6935 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6936 fold_convert (ctype
, t2
));
6939 /* If this was a subtraction, negate OP1 and set it to be an addition.
6940 This simplifies the logic below. */
6941 if (tcode
== MINUS_EXPR
)
6943 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6944 /* If OP1 was not easily negatable, the constant may be OP0. */
6945 if (TREE_CODE (op0
) == INTEGER_CST
)
6947 std::swap (op0
, op1
);
6952 if (TREE_CODE (op1
) != INTEGER_CST
)
6955 /* If either OP1 or C are negative, this optimization is not safe for
6956 some of the division and remainder types while for others we need
6957 to change the code. */
6958 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6960 if (code
== CEIL_DIV_EXPR
)
6961 code
= FLOOR_DIV_EXPR
;
6962 else if (code
== FLOOR_DIV_EXPR
)
6963 code
= CEIL_DIV_EXPR
;
6964 else if (code
!= MULT_EXPR
6965 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6969 /* If it's a multiply or a division/modulus operation of a multiple
6970 of our constant, do the operation and verify it doesn't overflow. */
6971 if (code
== MULT_EXPR
6972 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6975 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6976 fold_convert (ctype
, c
));
6977 /* We allow the constant to overflow with wrapping semantics. */
6979 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6985 /* If we have an unsigned type, we cannot widen the operation since it
6986 will change the result if the original computation overflowed. */
6987 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6990 /* The last case is if we are a multiply. In that case, we can
6991 apply the distributive law to commute the multiply and addition
6992 if the multiplication of the constants doesn't overflow
6993 and overflow is defined. With undefined overflow
6994 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6995 But fold_plusminus_mult_expr would factor back any power-of-two
6996 value so do not distribute in the first place in this case. */
6997 if (code
== MULT_EXPR
6998 && TYPE_OVERFLOW_WRAPS (ctype
)
6999 && !(tree_fits_shwi_p (c
) && pow2p_hwi (absu_hwi (tree_to_shwi (c
)))))
7000 return fold_build2 (tcode
, ctype
,
7001 fold_build2 (code
, ctype
,
7002 fold_convert (ctype
, op0
),
7003 fold_convert (ctype
, c
)),
7009 /* We have a special case here if we are doing something like
7010 (C * 8) % 4 since we know that's zero. */
7011 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
7012 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
7013 /* If the multiplication can overflow we cannot optimize this. */
7014 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
7015 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
7016 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7019 *strict_overflow_p
= true;
7020 return omit_one_operand (type
, integer_zero_node
, op0
);
7023 /* ... fall through ... */
7025 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
7026 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
7027 /* If we can extract our operation from the LHS, do so and return a
7028 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7029 do something only if the second operand is a constant. */
7031 && TYPE_OVERFLOW_WRAPS (ctype
)
7032 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
7033 strict_overflow_p
)) != 0)
7034 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7035 fold_convert (ctype
, op1
));
7036 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
7037 && TYPE_OVERFLOW_WRAPS (ctype
)
7038 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
7039 strict_overflow_p
)) != 0)
7040 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7041 fold_convert (ctype
, t1
));
7042 else if (TREE_CODE (op1
) != INTEGER_CST
)
7045 /* If these are the same operation types, we can associate them
7046 assuming no overflow. */
7049 bool overflow_p
= false;
7050 wi::overflow_type overflow_mul
;
7051 signop sign
= TYPE_SIGN (ctype
);
7052 unsigned prec
= TYPE_PRECISION (ctype
);
7053 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
7054 wi::to_wide (c
, prec
),
7055 sign
, &overflow_mul
);
7056 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
7058 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
7061 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7062 wide_int_to_tree (ctype
, mul
));
7065 /* If these operations "cancel" each other, we have the main
7066 optimizations of this pass, which occur when either constant is a
7067 multiple of the other, in which case we replace this with either an
7068 operation or CODE or TCODE.
7070 If we have an unsigned type, we cannot do this since it will change
7071 the result if the original computation overflowed. */
7072 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
7073 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
7074 || (tcode
== MULT_EXPR
7075 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
7076 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
7077 && code
!= MULT_EXPR
)))
7079 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7082 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
7083 *strict_overflow_p
= true;
7084 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7085 fold_convert (ctype
,
7086 const_binop (TRUNC_DIV_EXPR
,
7089 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
7092 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
7093 *strict_overflow_p
= true;
7094 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
7095 fold_convert (ctype
,
7096 const_binop (TRUNC_DIV_EXPR
,
7109 /* Return a node which has the indicated constant VALUE (either 0 or
7110 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7111 and is of the indicated TYPE. */
7114 constant_boolean_node (bool value
, tree type
)
7116 if (type
== integer_type_node
)
7117 return value
? integer_one_node
: integer_zero_node
;
7118 else if (type
== boolean_type_node
)
7119 return value
? boolean_true_node
: boolean_false_node
;
7120 else if (TREE_CODE (type
) == VECTOR_TYPE
)
7121 return build_vector_from_val (type
,
7122 build_int_cst (TREE_TYPE (type
),
7125 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
7129 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7130 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7131 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7132 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7133 COND is the first argument to CODE; otherwise (as in the example
7134 given here), it is the second argument. TYPE is the type of the
7135 original expression. Return NULL_TREE if no simplification is
7139 fold_binary_op_with_conditional_arg (location_t loc
,
7140 enum tree_code code
,
7141 tree type
, tree op0
, tree op1
,
7142 tree cond
, tree arg
, int cond_first_p
)
7144 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
7145 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
7146 tree test
, true_value
, false_value
;
7147 tree lhs
= NULL_TREE
;
7148 tree rhs
= NULL_TREE
;
7149 enum tree_code cond_code
= COND_EXPR
;
7151 /* Do not move possibly trapping operations into the conditional as this
7152 pessimizes code and causes gimplification issues when applied late. */
7153 if (operation_could_trap_p (code
, FLOAT_TYPE_P (type
),
7154 ANY_INTEGRAL_TYPE_P (type
)
7155 && TYPE_OVERFLOW_TRAPS (type
), op1
))
7158 if (TREE_CODE (cond
) == COND_EXPR
7159 || TREE_CODE (cond
) == VEC_COND_EXPR
)
7161 test
= TREE_OPERAND (cond
, 0);
7162 true_value
= TREE_OPERAND (cond
, 1);
7163 false_value
= TREE_OPERAND (cond
, 2);
7164 /* If this operand throws an expression, then it does not make
7165 sense to try to perform a logical or arithmetic operation
7167 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
7169 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
7172 else if (!(TREE_CODE (type
) != VECTOR_TYPE
7173 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
7175 tree testtype
= TREE_TYPE (cond
);
7177 true_value
= constant_boolean_node (true, testtype
);
7178 false_value
= constant_boolean_node (false, testtype
);
7181 /* Detect the case of mixing vector and scalar types - bail out. */
7184 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
7185 cond_code
= VEC_COND_EXPR
;
7187 /* This transformation is only worthwhile if we don't have to wrap ARG
7188 in a SAVE_EXPR and the operation can be simplified without recursing
7189 on at least one of the branches once its pushed inside the COND_EXPR. */
7190 if (!TREE_CONSTANT (arg
)
7191 && (TREE_SIDE_EFFECTS (arg
)
7192 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
7193 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
7196 arg
= fold_convert_loc (loc
, arg_type
, arg
);
7199 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
7201 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
7203 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
7207 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
7209 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
7211 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
7214 /* Check that we have simplified at least one of the branches. */
7215 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
7218 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
7222 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7224 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7225 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7226 if ARG - ZERO_ARG is the same as X.
7228 If ARG is NULL, check for any value of type TYPE.
7230 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7231 and finite. The problematic cases are when X is zero, and its mode
7232 has signed zeros. In the case of rounding towards -infinity,
7233 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7234 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7237 fold_real_zero_addition_p (const_tree type
, const_tree arg
,
7238 const_tree zero_arg
, int negate
)
7240 if (!real_zerop (zero_arg
))
7243 /* Don't allow the fold with -fsignaling-nans. */
7244 if (arg
? tree_expr_maybe_signaling_nan_p (arg
) : HONOR_SNANS (type
))
7247 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7248 if (!HONOR_SIGNED_ZEROS (type
))
7251 /* There is no case that is safe for all rounding modes. */
7252 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
7255 /* In a vector or complex, we would need to check the sign of all zeros. */
7256 if (TREE_CODE (zero_arg
) == VECTOR_CST
)
7257 zero_arg
= uniform_vector_p (zero_arg
);
7258 if (!zero_arg
|| TREE_CODE (zero_arg
) != REAL_CST
)
7261 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7262 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg
)))
7265 /* The mode has signed zeros, and we have to honor their sign.
7266 In this situation, there are only two cases we can return true for.
7267 (i) X - 0 is the same as X with default rounding.
7268 (ii) X + 0 is X when X can't possibly be -0.0. */
7269 return negate
|| (arg
&& !tree_expr_maybe_real_minus_zero_p (arg
));
7272 /* Subroutine of match.pd that optimizes comparisons of a division by
7273 a nonzero integer constant against an integer constant, i.e.
7276 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7277 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7280 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
7281 tree
*hi
, bool *neg_overflow
)
7283 tree prod
, tmp
, type
= TREE_TYPE (c1
);
7284 signop sign
= TYPE_SIGN (type
);
7285 wi::overflow_type overflow
;
7287 /* We have to do this the hard way to detect unsigned overflow.
7288 prod = int_const_binop (MULT_EXPR, c1, c2); */
7289 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
7290 prod
= force_fit_type (type
, val
, -1, overflow
);
7291 *neg_overflow
= false;
7293 if (sign
== UNSIGNED
)
7295 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7298 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7299 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
7300 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
7302 else if (tree_int_cst_sgn (c1
) >= 0)
7304 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7305 switch (tree_int_cst_sgn (c2
))
7308 *neg_overflow
= true;
7309 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7314 *lo
= fold_negate_const (tmp
, type
);
7319 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7329 /* A negative divisor reverses the relational operators. */
7330 code
= swap_tree_comparison (code
);
7332 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
7333 switch (tree_int_cst_sgn (c2
))
7336 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7341 *hi
= fold_negate_const (tmp
, type
);
7346 *neg_overflow
= true;
7347 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7356 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
7359 if (TREE_OVERFLOW (*lo
)
7360 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
7362 if (TREE_OVERFLOW (*hi
)
7363 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
7370 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7371 equality/inequality test, then return a simplified form of the test
7372 using a sign testing. Otherwise return NULL. TYPE is the desired
7376 fold_single_bit_test_into_sign_test (location_t loc
,
7377 enum tree_code code
, tree arg0
, tree arg1
,
7380 /* If this is testing a single bit, we can optimize the test. */
7381 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7382 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7383 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7385 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7386 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7387 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
7389 if (arg00
!= NULL_TREE
7390 /* This is only a win if casting to a signed type is cheap,
7391 i.e. when arg00's type is not a partial mode. */
7392 && type_has_mode_precision_p (TREE_TYPE (arg00
)))
7394 tree stype
= signed_type_for (TREE_TYPE (arg00
));
7395 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
7397 fold_convert_loc (loc
, stype
, arg00
),
7398 build_int_cst (stype
, 0));
7405 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7406 equality/inequality test, then return a simplified form of
7407 the test using shifts and logical operations. Otherwise return
7408 NULL. TYPE is the desired result type. */
7411 fold_single_bit_test (location_t loc
, enum tree_code code
,
7412 tree arg0
, tree arg1
, tree result_type
)
7414 /* If this is testing a single bit, we can optimize the test. */
7415 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7416 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7417 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7419 tree inner
= TREE_OPERAND (arg0
, 0);
7420 tree type
= TREE_TYPE (arg0
);
7421 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
7422 scalar_int_mode operand_mode
= SCALAR_INT_TYPE_MODE (type
);
7424 tree signed_type
, unsigned_type
, intermediate_type
;
7427 /* First, see if we can fold the single bit test into a sign-bit
7429 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
7434 /* Otherwise we have (A & C) != 0 where C is a single bit,
7435 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7436 Similarly for (A & C) == 0. */
7438 /* If INNER is a right shift of a constant and it plus BITNUM does
7439 not overflow, adjust BITNUM and INNER. */
7440 if (TREE_CODE (inner
) == RSHIFT_EXPR
7441 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
7442 && bitnum
< TYPE_PRECISION (type
)
7443 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner
, 1)),
7444 TYPE_PRECISION (type
) - bitnum
))
7446 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
7447 inner
= TREE_OPERAND (inner
, 0);
7450 /* If we are going to be able to omit the AND below, we must do our
7451 operations as unsigned. If we must use the AND, we have a choice.
7452 Normally unsigned is faster, but for some machines signed is. */
7453 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
7454 && !flag_syntax_only
) ? 0 : 1;
7456 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
7457 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
7458 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
7459 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
7462 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
7463 inner
, size_int (bitnum
));
7465 one
= build_int_cst (intermediate_type
, 1);
7467 if (code
== EQ_EXPR
)
7468 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
7470 /* Put the AND last so it can combine with more things. */
7471 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
7473 /* Make sure to return the proper type. */
7474 inner
= fold_convert_loc (loc
, result_type
, inner
);
7481 /* Test whether it is preferable to swap two operands, ARG0 and
7482 ARG1, for example because ARG0 is an integer constant and ARG1
7486 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
7488 if (CONSTANT_CLASS_P (arg1
))
7490 if (CONSTANT_CLASS_P (arg0
))
7496 if (TREE_CONSTANT (arg1
))
7498 if (TREE_CONSTANT (arg0
))
7501 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7502 for commutative and comparison operators. Ensuring a canonical
7503 form allows the optimizers to find additional redundancies without
7504 having to explicitly check for both orderings. */
7505 if (TREE_CODE (arg0
) == SSA_NAME
7506 && TREE_CODE (arg1
) == SSA_NAME
7507 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
7510 /* Put SSA_NAMEs last. */
7511 if (TREE_CODE (arg1
) == SSA_NAME
)
7513 if (TREE_CODE (arg0
) == SSA_NAME
)
7516 /* Put variables last. */
7526 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7527 means A >= Y && A != MAX, but in this case we know that
7528 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7531 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7533 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
7535 if (TREE_CODE (bound
) == LT_EXPR
)
7536 a
= TREE_OPERAND (bound
, 0);
7537 else if (TREE_CODE (bound
) == GT_EXPR
)
7538 a
= TREE_OPERAND (bound
, 1);
7542 typea
= TREE_TYPE (a
);
7543 if (!INTEGRAL_TYPE_P (typea
)
7544 && !POINTER_TYPE_P (typea
))
7547 if (TREE_CODE (ineq
) == LT_EXPR
)
7549 a1
= TREE_OPERAND (ineq
, 1);
7550 y
= TREE_OPERAND (ineq
, 0);
7552 else if (TREE_CODE (ineq
) == GT_EXPR
)
7554 a1
= TREE_OPERAND (ineq
, 0);
7555 y
= TREE_OPERAND (ineq
, 1);
7560 if (TREE_TYPE (a1
) != typea
)
7563 if (POINTER_TYPE_P (typea
))
7565 /* Convert the pointer types into integer before taking the difference. */
7566 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7567 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7568 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7571 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7573 if (!diff
|| !integer_onep (diff
))
7576 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7579 /* Fold a sum or difference of at least one multiplication.
7580 Returns the folded tree or NULL if no simplification could be made. */
7583 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7584 tree arg0
, tree arg1
)
7586 tree arg00
, arg01
, arg10
, arg11
;
7587 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7589 /* (A * C) +- (B * C) -> (A+-B) * C.
7590 (A * C) +- A -> A * (C+-1).
7591 We are most concerned about the case where C is a constant,
7592 but other combinations show up during loop reduction. Since
7593 it is not difficult, try all four possibilities. */
7595 if (TREE_CODE (arg0
) == MULT_EXPR
)
7597 arg00
= TREE_OPERAND (arg0
, 0);
7598 arg01
= TREE_OPERAND (arg0
, 1);
7600 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7602 arg00
= build_one_cst (type
);
7607 /* We cannot generate constant 1 for fract. */
7608 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7611 arg01
= build_one_cst (type
);
7613 if (TREE_CODE (arg1
) == MULT_EXPR
)
7615 arg10
= TREE_OPERAND (arg1
, 0);
7616 arg11
= TREE_OPERAND (arg1
, 1);
7618 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7620 arg10
= build_one_cst (type
);
7621 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7622 the purpose of this canonicalization. */
7623 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7624 && negate_expr_p (arg1
)
7625 && code
== PLUS_EXPR
)
7627 arg11
= negate_expr (arg1
);
7635 /* We cannot generate constant 1 for fract. */
7636 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7639 arg11
= build_one_cst (type
);
7643 /* Prefer factoring a common non-constant. */
7644 if (operand_equal_p (arg00
, arg10
, 0))
7645 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7646 else if (operand_equal_p (arg01
, arg11
, 0))
7647 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7648 else if (operand_equal_p (arg00
, arg11
, 0))
7649 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7650 else if (operand_equal_p (arg01
, arg10
, 0))
7651 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7653 /* No identical multiplicands; see if we can find a common
7654 power-of-two factor in non-power-of-two multiplies. This
7655 can help in multi-dimensional array access. */
7656 else if (tree_fits_shwi_p (arg01
) && tree_fits_shwi_p (arg11
))
7658 HOST_WIDE_INT int01
= tree_to_shwi (arg01
);
7659 HOST_WIDE_INT int11
= tree_to_shwi (arg11
);
7664 /* Move min of absolute values to int11. */
7665 if (absu_hwi (int01
) < absu_hwi (int11
))
7667 tmp
= int01
, int01
= int11
, int11
= tmp
;
7668 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7675 const unsigned HOST_WIDE_INT factor
= absu_hwi (int11
);
7677 && pow2p_hwi (factor
)
7678 && (int01
& (factor
- 1)) == 0
7679 /* The remainder should not be a constant, otherwise we
7680 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7681 increased the number of multiplications necessary. */
7682 && TREE_CODE (arg10
) != INTEGER_CST
)
7684 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7685 build_int_cst (TREE_TYPE (arg00
),
7690 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7697 if (! ANY_INTEGRAL_TYPE_P (type
)
7698 || TYPE_OVERFLOW_WRAPS (type
)
7699 /* We are neither factoring zero nor minus one. */
7700 || TREE_CODE (same
) == INTEGER_CST
)
7701 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7702 fold_build2_loc (loc
, code
, type
,
7703 fold_convert_loc (loc
, type
, alt0
),
7704 fold_convert_loc (loc
, type
, alt1
)),
7705 fold_convert_loc (loc
, type
, same
));
7707 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7708 same may be minus one and thus the multiplication may overflow. Perform
7709 the sum operation in an unsigned type. */
7710 tree utype
= unsigned_type_for (type
);
7711 tree tem
= fold_build2_loc (loc
, code
, utype
,
7712 fold_convert_loc (loc
, utype
, alt0
),
7713 fold_convert_loc (loc
, utype
, alt1
));
7714 /* If the sum evaluated to a constant that is not -INF the multiplication
7716 if (TREE_CODE (tem
) == INTEGER_CST
7717 && (wi::to_wide (tem
)
7718 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7719 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7720 fold_convert (type
, tem
), same
);
7722 /* Do not resort to unsigned multiplication because
7723 we lose the no-overflow property of the expression. */
7727 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7728 specified by EXPR into the buffer PTR of length LEN bytes.
7729 Return the number of bytes placed in the buffer, or zero
7733 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7735 tree type
= TREE_TYPE (expr
);
7736 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7737 int byte
, offset
, word
, words
;
7738 unsigned char value
;
7740 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7747 return MIN (len
, total_bytes
- off
);
7749 words
= total_bytes
/ UNITS_PER_WORD
;
7751 for (byte
= 0; byte
< total_bytes
; byte
++)
7753 int bitpos
= byte
* BITS_PER_UNIT
;
7754 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7756 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7758 if (total_bytes
> UNITS_PER_WORD
)
7760 word
= byte
/ UNITS_PER_WORD
;
7761 if (WORDS_BIG_ENDIAN
)
7762 word
= (words
- 1) - word
;
7763 offset
= word
* UNITS_PER_WORD
;
7764 if (BYTES_BIG_ENDIAN
)
7765 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7767 offset
+= byte
% UNITS_PER_WORD
;
7770 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7771 if (offset
>= off
&& offset
- off
< len
)
7772 ptr
[offset
- off
] = value
;
7774 return MIN (len
, total_bytes
- off
);
7778 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7779 specified by EXPR into the buffer PTR of length LEN bytes.
7780 Return the number of bytes placed in the buffer, or zero
7784 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7786 tree type
= TREE_TYPE (expr
);
7787 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7788 int total_bytes
= GET_MODE_SIZE (mode
);
7789 FIXED_VALUE_TYPE value
;
7790 tree i_value
, i_type
;
7792 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7795 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7797 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7800 value
= TREE_FIXED_CST (expr
);
7801 i_value
= double_int_to_tree (i_type
, value
.data
);
7803 return native_encode_int (i_value
, ptr
, len
, off
);
7807 /* Subroutine of native_encode_expr. Encode the REAL_CST
7808 specified by EXPR into the buffer PTR of length LEN bytes.
7809 Return the number of bytes placed in the buffer, or zero
7813 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7815 tree type
= TREE_TYPE (expr
);
7816 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7817 int byte
, offset
, word
, words
, bitpos
;
7818 unsigned char value
;
7820 /* There are always 32 bits in each long, no matter the size of
7821 the hosts long. We handle floating point representations with
7825 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7832 return MIN (len
, total_bytes
- off
);
7834 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7836 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7838 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7839 bitpos
+= BITS_PER_UNIT
)
7841 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7842 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7844 if (UNITS_PER_WORD
< 4)
7846 word
= byte
/ UNITS_PER_WORD
;
7847 if (WORDS_BIG_ENDIAN
)
7848 word
= (words
- 1) - word
;
7849 offset
= word
* UNITS_PER_WORD
;
7850 if (BYTES_BIG_ENDIAN
)
7851 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7853 offset
+= byte
% UNITS_PER_WORD
;
7858 if (BYTES_BIG_ENDIAN
)
7860 /* Reverse bytes within each long, or within the entire float
7861 if it's smaller than a long (for HFmode). */
7862 offset
= MIN (3, total_bytes
- 1) - offset
;
7863 gcc_assert (offset
>= 0);
7866 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7868 && offset
- off
< len
)
7869 ptr
[offset
- off
] = value
;
7871 return MIN (len
, total_bytes
- off
);
7874 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7875 specified by EXPR into the buffer PTR of length LEN bytes.
7876 Return the number of bytes placed in the buffer, or zero
7880 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7885 part
= TREE_REALPART (expr
);
7886 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7887 if (off
== -1 && rsize
== 0)
7889 part
= TREE_IMAGPART (expr
);
7891 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7892 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7894 if (off
== -1 && isize
!= rsize
)
7896 return rsize
+ isize
;
7899 /* Like native_encode_vector, but only encode the first COUNT elements.
7900 The other arguments are as for native_encode_vector. */
7903 native_encode_vector_part (const_tree expr
, unsigned char *ptr
, int len
,
7904 int off
, unsigned HOST_WIDE_INT count
)
7906 tree itype
= TREE_TYPE (TREE_TYPE (expr
));
7907 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr
))
7908 && TYPE_PRECISION (itype
) <= BITS_PER_UNIT
)
7910 /* This is the only case in which elements can be smaller than a byte.
7911 Element 0 is always in the lsb of the containing byte. */
7912 unsigned int elt_bits
= TYPE_PRECISION (itype
);
7913 int total_bytes
= CEIL (elt_bits
* count
, BITS_PER_UNIT
);
7914 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7920 /* Zero the buffer and then set bits later where necessary. */
7921 int extract_bytes
= MIN (len
, total_bytes
- off
);
7923 memset (ptr
, 0, extract_bytes
);
7925 unsigned int elts_per_byte
= BITS_PER_UNIT
/ elt_bits
;
7926 unsigned int first_elt
= off
* elts_per_byte
;
7927 unsigned int extract_elts
= extract_bytes
* elts_per_byte
;
7928 for (unsigned int i
= 0; i
< extract_elts
; ++i
)
7930 tree elt
= VECTOR_CST_ELT (expr
, first_elt
+ i
);
7931 if (TREE_CODE (elt
) != INTEGER_CST
)
7934 if (ptr
&& wi::extract_uhwi (wi::to_wide (elt
), 0, 1))
7936 unsigned int bit
= i
* elt_bits
;
7937 ptr
[bit
/ BITS_PER_UNIT
] |= 1 << (bit
% BITS_PER_UNIT
);
7940 return extract_bytes
;
7944 int size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7945 for (unsigned HOST_WIDE_INT i
= 0; i
< count
; i
++)
7952 tree elem
= VECTOR_CST_ELT (expr
, i
);
7953 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7955 if ((off
== -1 && res
!= size
) || res
== 0)
7959 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
7966 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7967 specified by EXPR into the buffer PTR of length LEN bytes.
7968 Return the number of bytes placed in the buffer, or zero
7972 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7974 unsigned HOST_WIDE_INT count
;
7975 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
7977 return native_encode_vector_part (expr
, ptr
, len
, off
, count
);
7981 /* Subroutine of native_encode_expr. Encode the STRING_CST
7982 specified by EXPR into the buffer PTR of length LEN bytes.
7983 Return the number of bytes placed in the buffer, or zero
7987 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7989 tree type
= TREE_TYPE (expr
);
7991 /* Wide-char strings are encoded in target byte-order so native
7992 encoding them is trivial. */
7993 if (BITS_PER_UNIT
!= CHAR_BIT
7994 || TREE_CODE (type
) != ARRAY_TYPE
7995 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7996 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7999 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
8000 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8004 len
= MIN (total_bytes
- off
, len
);
8010 if (off
< TREE_STRING_LENGTH (expr
))
8012 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
8013 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
8015 memset (ptr
+ written
, 0, len
- written
);
8021 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
8022 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
8023 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
8024 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
8025 sufficient to encode the entire EXPR, or if OFF is out of bounds.
8026 Otherwise, start at byte offset OFF and encode at most LEN bytes.
8027 Return the number of bytes placed in the buffer, or zero upon failure. */
8030 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
8032 /* We don't support starting at negative offset and -1 is special. */
8036 switch (TREE_CODE (expr
))
8039 return native_encode_int (expr
, ptr
, len
, off
);
8042 return native_encode_real (expr
, ptr
, len
, off
);
8045 return native_encode_fixed (expr
, ptr
, len
, off
);
8048 return native_encode_complex (expr
, ptr
, len
, off
);
8051 return native_encode_vector (expr
, ptr
, len
, off
);
8054 return native_encode_string (expr
, ptr
, len
, off
);
8061 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8062 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8063 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8064 machine modes, we can't just use build_nonstandard_integer_type. */
8067 find_bitfield_repr_type (int fieldsize
, int len
)
8070 for (int pass
= 0; pass
< 2; pass
++)
8072 enum mode_class mclass
= pass
? MODE_PARTIAL_INT
: MODE_INT
;
8073 FOR_EACH_MODE_IN_CLASS (mode
, mclass
)
8074 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8075 && known_eq (GET_MODE_PRECISION (mode
),
8076 GET_MODE_BITSIZE (mode
))
8077 && known_le (GET_MODE_SIZE (mode
), len
))
8079 tree ret
= lang_hooks
.types
.type_for_mode (mode
, 1);
8080 if (ret
&& TYPE_MODE (ret
) == mode
)
8085 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
8086 if (int_n_enabled_p
[i
]
8087 && int_n_data
[i
].bitsize
>= (unsigned) (BITS_PER_UNIT
* fieldsize
)
8088 && int_n_trees
[i
].unsigned_type
)
8090 tree ret
= int_n_trees
[i
].unsigned_type
;
8091 mode
= TYPE_MODE (ret
);
8092 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8093 && known_eq (GET_MODE_PRECISION (mode
),
8094 GET_MODE_BITSIZE (mode
))
8095 && known_le (GET_MODE_SIZE (mode
), len
))
8102 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8103 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8104 to be non-NULL and OFF zero), then in addition to filling the
8105 bytes pointed by PTR with the value also clear any bits pointed
8106 by MASK that are known to be initialized, keep them as is for
8107 e.g. uninitialized padding bits or uninitialized fields. */
8110 native_encode_initializer (tree init
, unsigned char *ptr
, int len
,
8111 int off
, unsigned char *mask
)
8115 /* We don't support starting at negative offset and -1 is special. */
8116 if (off
< -1 || init
== NULL_TREE
)
8119 gcc_assert (mask
== NULL
|| (off
== 0 && ptr
));
8122 switch (TREE_CODE (init
))
8124 case VIEW_CONVERT_EXPR
:
8125 case NON_LVALUE_EXPR
:
8126 return native_encode_initializer (TREE_OPERAND (init
, 0), ptr
, len
, off
,
8129 r
= native_encode_expr (init
, ptr
, len
, off
);
8131 memset (mask
, 0, r
);
8134 tree type
= TREE_TYPE (init
);
8135 HOST_WIDE_INT total_bytes
= int_size_in_bytes (type
);
8136 if (total_bytes
< 0)
8138 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8140 int o
= off
== -1 ? 0 : off
;
8141 if (TREE_CODE (type
) == ARRAY_TYPE
)
8144 unsigned HOST_WIDE_INT cnt
;
8145 HOST_WIDE_INT curpos
= 0, fieldsize
, valueinit
= -1;
8146 constructor_elt
*ce
;
8148 if (!TYPE_DOMAIN (type
)
8149 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
8152 fieldsize
= int_size_in_bytes (TREE_TYPE (type
));
8156 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
8158 memset (ptr
, '\0', MIN (total_bytes
- off
, len
));
8160 for (cnt
= 0; ; cnt
++)
8162 tree val
= NULL_TREE
, index
= NULL_TREE
;
8163 HOST_WIDE_INT pos
= curpos
, count
= 0;
8165 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8170 else if (mask
== NULL
8171 || CONSTRUCTOR_NO_CLEARING (init
)
8172 || curpos
>= total_bytes
)
8177 if (index
&& TREE_CODE (index
) == RANGE_EXPR
)
8179 if (TREE_CODE (TREE_OPERAND (index
, 0)) != INTEGER_CST
8180 || TREE_CODE (TREE_OPERAND (index
, 1)) != INTEGER_CST
)
8183 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 0))
8184 - wi::to_offset (min_index
),
8185 TYPE_PRECISION (sizetype
));
8187 if (!wi::fits_shwi_p (pos
))
8189 pos
= wpos
.to_shwi ();
8191 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 1))
8192 - wi::to_offset (TREE_OPERAND (index
, 0)),
8193 TYPE_PRECISION (sizetype
));
8194 if (!wi::fits_shwi_p (wcount
))
8196 count
= wcount
.to_shwi ();
8200 if (TREE_CODE (index
) != INTEGER_CST
)
8203 = wi::sext (wi::to_offset (index
)
8204 - wi::to_offset (min_index
),
8205 TYPE_PRECISION (sizetype
));
8207 if (!wi::fits_shwi_p (wpos
))
8209 pos
= wpos
.to_shwi ();
8212 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
) && curpos
!= pos
)
8214 if (valueinit
== -1)
8216 tree zero
= build_zero_cst (TREE_TYPE (type
));
8217 r
= native_encode_initializer (zero
, ptr
+ curpos
,
8220 if (TREE_CODE (zero
) == CONSTRUCTOR
)
8225 curpos
+= fieldsize
;
8227 while (curpos
!= pos
)
8229 memcpy (ptr
+ curpos
, ptr
+ valueinit
, fieldsize
);
8230 memcpy (mask
+ curpos
, mask
+ valueinit
, fieldsize
);
8231 curpos
+= fieldsize
;
8241 && (curpos
+ fieldsize
8242 <= (HOST_WIDE_INT
) off
+ len
)))
8247 memcpy (ptr
+ (curpos
- o
), ptr
+ (pos
- o
),
8250 memcpy (mask
+ curpos
, mask
+ pos
, fieldsize
);
8252 else if (!native_encode_initializer (val
,
8269 else if (curpos
+ fieldsize
> off
8270 && curpos
< (HOST_WIDE_INT
) off
+ len
)
8272 /* Partial overlap. */
8273 unsigned char *p
= NULL
;
8276 gcc_assert (mask
== NULL
);
8280 p
= ptr
+ curpos
- off
;
8281 l
= MIN ((HOST_WIDE_INT
) off
+ len
- curpos
,
8290 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8293 curpos
+= fieldsize
;
8295 while (count
-- != 0);
8297 return MIN (total_bytes
- off
, len
);
8299 else if (TREE_CODE (type
) == RECORD_TYPE
8300 || TREE_CODE (type
) == UNION_TYPE
)
8302 unsigned HOST_WIDE_INT cnt
;
8303 constructor_elt
*ce
;
8304 tree fld_base
= TYPE_FIELDS (type
);
8305 tree to_free
= NULL_TREE
;
8307 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
|| mask
== NULL
);
8309 memset (ptr
, '\0', MIN (total_bytes
- o
, len
));
8310 for (cnt
= 0; ; cnt
++)
8312 tree val
= NULL_TREE
, field
= NULL_TREE
;
8313 HOST_WIDE_INT pos
= 0, fieldsize
;
8314 unsigned HOST_WIDE_INT bpos
= 0, epos
= 0;
8319 to_free
= NULL_TREE
;
8322 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8326 if (field
== NULL_TREE
)
8329 pos
= int_byte_position (field
);
8330 if (off
!= -1 && (HOST_WIDE_INT
) off
+ len
<= pos
)
8333 else if (mask
== NULL
8334 || CONSTRUCTOR_NO_CLEARING (init
))
8339 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
))
8342 for (fld
= fld_base
; fld
; fld
= DECL_CHAIN (fld
))
8344 if (TREE_CODE (fld
) != FIELD_DECL
)
8348 if (DECL_PADDING_P (fld
))
8350 if (DECL_SIZE_UNIT (fld
) == NULL_TREE
8351 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld
)))
8353 if (integer_zerop (DECL_SIZE_UNIT (fld
)))
8357 if (fld
== NULL_TREE
)
8363 fld_base
= DECL_CHAIN (fld
);
8368 pos
= int_byte_position (field
);
8369 val
= build_zero_cst (TREE_TYPE (fld
));
8370 if (TREE_CODE (val
) == CONSTRUCTOR
)
8375 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
8376 && TYPE_DOMAIN (TREE_TYPE (field
))
8377 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field
))))
8379 if (mask
|| off
!= -1)
8381 if (val
== NULL_TREE
)
8383 if (TREE_CODE (TREE_TYPE (val
)) != ARRAY_TYPE
)
8385 fieldsize
= int_size_in_bytes (TREE_TYPE (val
));
8387 || (int) fieldsize
!= fieldsize
8388 || (pos
+ fieldsize
) > INT_MAX
)
8390 if (pos
+ fieldsize
> total_bytes
)
8392 if (ptr
!= NULL
&& total_bytes
< len
)
8393 memset (ptr
+ total_bytes
, '\0',
8394 MIN (pos
+ fieldsize
, len
) - total_bytes
);
8395 total_bytes
= pos
+ fieldsize
;
8400 if (DECL_SIZE_UNIT (field
) == NULL_TREE
8401 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field
)))
8403 fieldsize
= tree_to_shwi (DECL_SIZE_UNIT (field
));
8408 if (DECL_BIT_FIELD (field
))
8410 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8412 fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8413 bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8414 if (bpos
% BITS_PER_UNIT
)
8415 bpos
%= BITS_PER_UNIT
;
8419 epos
= fieldsize
% BITS_PER_UNIT
;
8420 fieldsize
+= BITS_PER_UNIT
- 1;
8421 fieldsize
/= BITS_PER_UNIT
;
8424 if (off
!= -1 && pos
+ fieldsize
<= off
)
8427 if (val
== NULL_TREE
)
8430 if (DECL_BIT_FIELD (field
))
8432 /* FIXME: Handle PDP endian. */
8433 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8436 if (TREE_CODE (val
) != INTEGER_CST
)
8439 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8440 tree repr_type
= NULL_TREE
;
8441 HOST_WIDE_INT rpos
= 0;
8442 if (repr
&& INTEGRAL_TYPE_P (TREE_TYPE (repr
)))
8444 rpos
= int_byte_position (repr
);
8445 repr_type
= TREE_TYPE (repr
);
8449 repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8450 if (repr_type
== NULL_TREE
)
8452 HOST_WIDE_INT repr_size
= int_size_in_bytes (repr_type
);
8453 gcc_assert (repr_size
> 0 && repr_size
<= len
);
8454 if (pos
+ repr_size
<= o
+ len
)
8458 rpos
= o
+ len
- repr_size
;
8459 gcc_assert (rpos
<= pos
);
8465 wide_int w
= wi::to_wide (val
, TYPE_PRECISION (repr_type
));
8466 int diff
= (TYPE_PRECISION (repr_type
)
8467 - TYPE_PRECISION (TREE_TYPE (field
)));
8468 HOST_WIDE_INT bitoff
= (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8469 if (!BYTES_BIG_ENDIAN
)
8470 w
= wi::lshift (w
, bitoff
);
8472 w
= wi::lshift (w
, diff
- bitoff
);
8473 val
= wide_int_to_tree (repr_type
, w
);
8475 unsigned char buf
[MAX_BITSIZE_MODE_ANY_INT
8476 / BITS_PER_UNIT
+ 1];
8477 int l
= native_encode_int (val
, buf
, sizeof buf
, 0);
8478 if (l
* BITS_PER_UNIT
!= TYPE_PRECISION (repr_type
))
8484 /* If the bitfield does not start at byte boundary, handle
8485 the partial byte at the start. */
8487 && (off
== -1 || (pos
>= off
&& len
>= 1)))
8489 if (!BYTES_BIG_ENDIAN
)
8491 int msk
= (1 << bpos
) - 1;
8492 buf
[pos
- rpos
] &= ~msk
;
8493 buf
[pos
- rpos
] |= ptr
[pos
- o
] & msk
;
8496 if (fieldsize
> 1 || epos
== 0)
8499 mask
[pos
] &= (msk
| ~((1 << epos
) - 1));
8504 int msk
= (1 << (BITS_PER_UNIT
- bpos
)) - 1;
8505 buf
[pos
- rpos
] &= msk
;
8506 buf
[pos
- rpos
] |= ptr
[pos
- o
] & ~msk
;
8509 if (fieldsize
> 1 || epos
== 0)
8513 | ((1 << (BITS_PER_UNIT
- epos
))
8518 /* If the bitfield does not end at byte boundary, handle
8519 the partial byte at the end. */
8522 || pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
))
8524 if (!BYTES_BIG_ENDIAN
)
8526 int msk
= (1 << epos
) - 1;
8527 buf
[pos
- rpos
+ fieldsize
- 1] &= msk
;
8528 buf
[pos
- rpos
+ fieldsize
- 1]
8529 |= ptr
[pos
+ fieldsize
- 1 - o
] & ~msk
;
8530 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8531 mask
[pos
+ fieldsize
- 1] &= ~msk
;
8535 int msk
= (1 << (BITS_PER_UNIT
- epos
)) - 1;
8536 buf
[pos
- rpos
+ fieldsize
- 1] &= ~msk
;
8537 buf
[pos
- rpos
+ fieldsize
- 1]
8538 |= ptr
[pos
+ fieldsize
- 1 - o
] & msk
;
8539 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8540 mask
[pos
+ fieldsize
- 1] &= msk
;
8545 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8547 memcpy (ptr
+ pos
- o
, buf
+ (pos
- rpos
), fieldsize
);
8548 if (mask
&& (fieldsize
> (bpos
!= 0) + (epos
!= 0)))
8549 memset (mask
+ pos
+ (bpos
!= 0), 0,
8550 fieldsize
- (bpos
!= 0) - (epos
!= 0));
8554 /* Partial overlap. */
8555 HOST_WIDE_INT fsz
= fieldsize
;
8556 gcc_assert (mask
== NULL
);
8562 if (pos
+ fsz
> (HOST_WIDE_INT
) off
+ len
)
8563 fsz
= (HOST_WIDE_INT
) off
+ len
- pos
;
8564 memcpy (ptr
+ pos
- off
, buf
+ (pos
- rpos
), fsz
);
8571 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8573 int fldsize
= fieldsize
;
8576 tree fld
= DECL_CHAIN (field
);
8579 if (TREE_CODE (fld
) == FIELD_DECL
)
8581 fld
= DECL_CHAIN (fld
);
8583 if (fld
== NULL_TREE
)
8584 fldsize
= len
- pos
;
8586 r
= native_encode_initializer (val
, ptr
? ptr
+ pos
- o
8590 mask
? mask
+ pos
: NULL
);
8594 && fldsize
!= fieldsize
8596 && pos
+ r
> total_bytes
)
8597 total_bytes
= pos
+ r
;
8601 /* Partial overlap. */
8602 unsigned char *p
= NULL
;
8605 gcc_assert (mask
== NULL
);
8609 p
= ptr
+ pos
- off
;
8610 l
= MIN ((HOST_WIDE_INT
) off
+ len
- pos
,
8619 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8623 return MIN (total_bytes
- off
, len
);
8630 /* Subroutine of native_interpret_expr. Interpret the contents of
8631 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8632 If the buffer cannot be interpreted, return NULL_TREE. */
8635 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
8637 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8639 if (total_bytes
> len
8640 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8643 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
8645 return wide_int_to_tree (type
, result
);
8649 /* Subroutine of native_interpret_expr. Interpret the contents of
8650 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8651 If the buffer cannot be interpreted, return NULL_TREE. */
8654 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
8656 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
8657 int total_bytes
= GET_MODE_SIZE (mode
);
8659 FIXED_VALUE_TYPE fixed_value
;
8661 if (total_bytes
> len
8662 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8665 result
= double_int::from_buffer (ptr
, total_bytes
);
8666 fixed_value
= fixed_from_double_int (result
, mode
);
8668 return build_fixed (type
, fixed_value
);
8672 /* Subroutine of native_interpret_expr. Interpret the contents of
8673 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8674 If the buffer cannot be interpreted, return NULL_TREE. */
8677 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
8679 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8680 int total_bytes
= GET_MODE_SIZE (mode
);
8681 unsigned char value
;
8682 /* There are always 32 bits in each long, no matter the size of
8683 the hosts long. We handle floating point representations with
8688 if (total_bytes
> len
|| total_bytes
> 24)
8690 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
8692 memset (tmp
, 0, sizeof (tmp
));
8693 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
8694 bitpos
+= BITS_PER_UNIT
)
8696 /* Both OFFSET and BYTE index within a long;
8697 bitpos indexes the whole float. */
8698 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
8699 if (UNITS_PER_WORD
< 4)
8701 int word
= byte
/ UNITS_PER_WORD
;
8702 if (WORDS_BIG_ENDIAN
)
8703 word
= (words
- 1) - word
;
8704 offset
= word
* UNITS_PER_WORD
;
8705 if (BYTES_BIG_ENDIAN
)
8706 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
8708 offset
+= byte
% UNITS_PER_WORD
;
8713 if (BYTES_BIG_ENDIAN
)
8715 /* Reverse bytes within each long, or within the entire float
8716 if it's smaller than a long (for HFmode). */
8717 offset
= MIN (3, total_bytes
- 1) - offset
;
8718 gcc_assert (offset
>= 0);
8721 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
8723 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
8726 real_from_target (&r
, tmp
, mode
);
8727 return build_real (type
, r
);
8731 /* Subroutine of native_interpret_expr. Interpret the contents of
8732 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8733 If the buffer cannot be interpreted, return NULL_TREE. */
8736 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
8738 tree etype
, rpart
, ipart
;
8741 etype
= TREE_TYPE (type
);
8742 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8745 rpart
= native_interpret_expr (etype
, ptr
, size
);
8748 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
8751 return build_complex (type
, rpart
, ipart
);
8754 /* Read a vector of type TYPE from the target memory image given by BYTES,
8755 which contains LEN bytes. The vector is known to be encodable using
8756 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8758 Return the vector on success, otherwise return null. */
8761 native_interpret_vector_part (tree type
, const unsigned char *bytes
,
8762 unsigned int len
, unsigned int npatterns
,
8763 unsigned int nelts_per_pattern
)
8765 tree elt_type
= TREE_TYPE (type
);
8766 if (VECTOR_BOOLEAN_TYPE_P (type
)
8767 && TYPE_PRECISION (elt_type
) <= BITS_PER_UNIT
)
8769 /* This is the only case in which elements can be smaller than a byte.
8770 Element 0 is always in the lsb of the containing byte. */
8771 unsigned int elt_bits
= TYPE_PRECISION (elt_type
);
8772 if (elt_bits
* npatterns
* nelts_per_pattern
> len
* BITS_PER_UNIT
)
8775 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8776 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8778 unsigned int bit_index
= i
* elt_bits
;
8779 unsigned int byte_index
= bit_index
/ BITS_PER_UNIT
;
8780 unsigned int lsb
= bit_index
% BITS_PER_UNIT
;
8781 builder
.quick_push (bytes
[byte_index
] & (1 << lsb
)
8782 ? build_all_ones_cst (elt_type
)
8783 : build_zero_cst (elt_type
));
8785 return builder
.build ();
8788 unsigned int elt_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (elt_type
));
8789 if (elt_bytes
* npatterns
* nelts_per_pattern
> len
)
8792 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8793 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8795 tree elt
= native_interpret_expr (elt_type
, bytes
, elt_bytes
);
8798 builder
.quick_push (elt
);
8801 return builder
.build ();
8804 /* Subroutine of native_interpret_expr. Interpret the contents of
8805 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8806 If the buffer cannot be interpreted, return NULL_TREE. */
8809 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
8813 unsigned HOST_WIDE_INT count
;
8815 etype
= TREE_TYPE (type
);
8816 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8817 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&count
)
8818 || size
* count
> len
)
8821 return native_interpret_vector_part (type
, ptr
, len
, count
, 1);
8825 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8826 the buffer PTR of length LEN as a constant of type TYPE. For
8827 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8828 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8829 return NULL_TREE. */
8832 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
8834 switch (TREE_CODE (type
))
8840 case REFERENCE_TYPE
:
8842 return native_interpret_int (type
, ptr
, len
);
8845 if (tree ret
= native_interpret_real (type
, ptr
, len
))
8847 /* For floating point values in composite modes, punt if this
8848 folding doesn't preserve bit representation. As the mode doesn't
8849 have fixed precision while GCC pretends it does, there could be
8850 valid values that GCC can't really represent accurately.
8851 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8852 bit combinationations which GCC doesn't preserve. */
8853 unsigned char buf
[24];
8854 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8855 int total_bytes
= GET_MODE_SIZE (mode
);
8856 if (native_encode_expr (ret
, buf
, total_bytes
, 0) != total_bytes
8857 || memcmp (ptr
, buf
, total_bytes
) != 0)
8863 case FIXED_POINT_TYPE
:
8864 return native_interpret_fixed (type
, ptr
, len
);
8867 return native_interpret_complex (type
, ptr
, len
);
8870 return native_interpret_vector (type
, ptr
, len
);
8877 /* Returns true if we can interpret the contents of a native encoding
8881 can_native_interpret_type_p (tree type
)
8883 switch (TREE_CODE (type
))
8889 case REFERENCE_TYPE
:
8890 case FIXED_POINT_TYPE
:
8901 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8902 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8905 native_interpret_aggregate (tree type
, const unsigned char *ptr
, int off
,
8908 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
8909 if (TREE_CODE (type
) == ARRAY_TYPE
)
8911 HOST_WIDE_INT eltsz
= int_size_in_bytes (TREE_TYPE (type
));
8912 if (eltsz
< 0 || eltsz
> len
|| TYPE_DOMAIN (type
) == NULL_TREE
)
8915 HOST_WIDE_INT cnt
= 0;
8916 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
8918 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
8920 cnt
= tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1;
8924 HOST_WIDE_INT pos
= 0;
8925 for (HOST_WIDE_INT i
= 0; i
< cnt
; i
++, pos
+= eltsz
)
8928 if (pos
>= len
|| pos
+ eltsz
> len
)
8930 if (can_native_interpret_type_p (TREE_TYPE (type
)))
8932 v
= native_interpret_expr (TREE_TYPE (type
),
8933 ptr
+ off
+ pos
, eltsz
);
8937 else if (TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
8938 || TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
8939 v
= native_interpret_aggregate (TREE_TYPE (type
), ptr
, off
+ pos
,
8943 CONSTRUCTOR_APPEND_ELT (elts
, size_int (i
), v
);
8945 return build_constructor (type
, elts
);
8947 if (TREE_CODE (type
) != RECORD_TYPE
)
8949 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8951 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_PADDING_P (field
))
8954 HOST_WIDE_INT bitoff
= 0, pos
= 0, sz
= 0;
8957 if (DECL_BIT_FIELD (field
))
8959 fld
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8960 if (fld
&& INTEGRAL_TYPE_P (TREE_TYPE (fld
)))
8962 poly_int64 bitoffset
;
8963 poly_uint64 field_offset
, fld_offset
;
8964 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
8965 && poly_int_tree_p (DECL_FIELD_OFFSET (fld
), &fld_offset
))
8966 bitoffset
= (field_offset
- fld_offset
) * BITS_PER_UNIT
;
8969 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
8970 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld
)));
8971 diff
= (TYPE_PRECISION (TREE_TYPE (fld
))
8972 - TYPE_PRECISION (TREE_TYPE (field
)));
8973 if (!bitoffset
.is_constant (&bitoff
)
8980 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8982 int fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8983 int bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8984 bpos
%= BITS_PER_UNIT
;
8986 fieldsize
+= BITS_PER_UNIT
- 1;
8987 fieldsize
/= BITS_PER_UNIT
;
8988 tree repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8989 if (repr_type
== NULL_TREE
)
8991 sz
= int_size_in_bytes (repr_type
);
8992 if (sz
< 0 || sz
> len
)
8994 pos
= int_byte_position (field
);
8995 if (pos
< 0 || pos
> len
|| pos
+ fieldsize
> len
)
8998 if (pos
+ sz
<= len
)
9003 gcc_assert (rpos
<= pos
);
9005 bitoff
= (HOST_WIDE_INT
) (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
9007 diff
= (TYPE_PRECISION (repr_type
)
9008 - TYPE_PRECISION (TREE_TYPE (field
)));
9009 v
= native_interpret_expr (repr_type
, ptr
+ off
+ pos
, sz
);
9018 sz
= int_size_in_bytes (TREE_TYPE (fld
));
9019 if (sz
< 0 || sz
> len
)
9021 tree byte_pos
= byte_position (fld
);
9022 if (!tree_fits_shwi_p (byte_pos
))
9024 pos
= tree_to_shwi (byte_pos
);
9025 if (pos
< 0 || pos
> len
|| pos
+ sz
> len
)
9028 if (fld
== NULL_TREE
)
9029 /* Already handled above. */;
9030 else if (can_native_interpret_type_p (TREE_TYPE (fld
)))
9032 v
= native_interpret_expr (TREE_TYPE (fld
),
9033 ptr
+ off
+ pos
, sz
);
9037 else if (TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
9038 || TREE_CODE (TREE_TYPE (fld
)) == ARRAY_TYPE
)
9039 v
= native_interpret_aggregate (TREE_TYPE (fld
), ptr
, off
+ pos
, sz
);
9044 if (TREE_CODE (v
) != INTEGER_CST
)
9047 /* FIXME: Figure out how to handle PDP endian bitfields. */
9048 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
9050 if (!BYTES_BIG_ENDIAN
)
9051 v
= wide_int_to_tree (TREE_TYPE (field
),
9052 wi::lrshift (wi::to_wide (v
), bitoff
));
9054 v
= wide_int_to_tree (TREE_TYPE (field
),
9055 wi::lrshift (wi::to_wide (v
),
9058 CONSTRUCTOR_APPEND_ELT (elts
, field
, v
);
9060 return build_constructor (type
, elts
);
9063 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9064 or extracted constant positions and/or sizes aren't byte aligned. */
9066 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9067 bits between adjacent elements. AMNT should be within
9070 00011111|11100000 << 2 = 01111111|10000000
9071 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9074 shift_bytes_in_array_left (unsigned char *ptr
, unsigned int sz
,
9080 unsigned char carry_over
= 0U;
9081 unsigned char carry_mask
= (~0U) << (unsigned char) (BITS_PER_UNIT
- amnt
);
9082 unsigned char clear_mask
= (~0U) << amnt
;
9084 for (unsigned int i
= 0; i
< sz
; i
++)
9086 unsigned prev_carry_over
= carry_over
;
9087 carry_over
= (ptr
[i
] & carry_mask
) >> (BITS_PER_UNIT
- amnt
);
9092 ptr
[i
] &= clear_mask
;
9093 ptr
[i
] |= prev_carry_over
;
9098 /* Like shift_bytes_in_array_left but for big-endian.
9099 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9100 bits between adjacent elements. AMNT should be within
9103 00011111|11100000 >> 2 = 00000111|11111000
9104 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9107 shift_bytes_in_array_right (unsigned char *ptr
, unsigned int sz
,
9113 unsigned char carry_over
= 0U;
9114 unsigned char carry_mask
= ~(~0U << amnt
);
9116 for (unsigned int i
= 0; i
< sz
; i
++)
9118 unsigned prev_carry_over
= carry_over
;
9119 carry_over
= ptr
[i
] & carry_mask
;
9121 carry_over
<<= (unsigned char) BITS_PER_UNIT
- amnt
;
9123 ptr
[i
] |= prev_carry_over
;
9127 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9128 directly on the VECTOR_CST encoding, in a way that works for variable-
9129 length vectors. Return the resulting VECTOR_CST on success or null
9133 fold_view_convert_vector_encoding (tree type
, tree expr
)
9135 tree expr_type
= TREE_TYPE (expr
);
9136 poly_uint64 type_bits
, expr_bits
;
9137 if (!poly_int_tree_p (TYPE_SIZE (type
), &type_bits
)
9138 || !poly_int_tree_p (TYPE_SIZE (expr_type
), &expr_bits
))
9141 poly_uint64 type_units
= TYPE_VECTOR_SUBPARTS (type
);
9142 poly_uint64 expr_units
= TYPE_VECTOR_SUBPARTS (expr_type
);
9143 unsigned int type_elt_bits
= vector_element_size (type_bits
, type_units
);
9144 unsigned int expr_elt_bits
= vector_element_size (expr_bits
, expr_units
);
9146 /* We can only preserve the semantics of a stepped pattern if the new
9147 vector element is an integer of the same size. */
9148 if (VECTOR_CST_STEPPED_P (expr
)
9149 && (!INTEGRAL_TYPE_P (type
) || type_elt_bits
!= expr_elt_bits
))
9152 /* The number of bits needed to encode one element from every pattern
9153 of the original vector. */
9154 unsigned int expr_sequence_bits
9155 = VECTOR_CST_NPATTERNS (expr
) * expr_elt_bits
;
9157 /* The number of bits needed to encode one element from every pattern
9159 unsigned int type_sequence_bits
9160 = least_common_multiple (expr_sequence_bits
, type_elt_bits
);
9162 /* Don't try to read more bytes than are available, which can happen
9163 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9164 The general VIEW_CONVERT handling can cope with that case, so there's
9165 no point complicating things here. */
9166 unsigned int nelts_per_pattern
= VECTOR_CST_NELTS_PER_PATTERN (expr
);
9167 unsigned int buffer_bytes
= CEIL (nelts_per_pattern
* type_sequence_bits
,
9169 unsigned int buffer_bits
= buffer_bytes
* BITS_PER_UNIT
;
9170 if (known_gt (buffer_bits
, expr_bits
))
9173 /* Get enough bytes of EXPR to form the new encoding. */
9174 auto_vec
<unsigned char, 128> buffer (buffer_bytes
);
9175 buffer
.quick_grow (buffer_bytes
);
9176 if (native_encode_vector_part (expr
, buffer
.address (), buffer_bytes
, 0,
9177 buffer_bits
/ expr_elt_bits
)
9178 != (int) buffer_bytes
)
9181 /* Reencode the bytes as TYPE. */
9182 unsigned int type_npatterns
= type_sequence_bits
/ type_elt_bits
;
9183 return native_interpret_vector_part (type
, &buffer
[0], buffer
.length (),
9184 type_npatterns
, nelts_per_pattern
);
9187 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9188 TYPE at compile-time. If we're unable to perform the conversion
9189 return NULL_TREE. */
9192 fold_view_convert_expr (tree type
, tree expr
)
9194 /* We support up to 512-bit values (for V8DFmode). */
9195 unsigned char buffer
[64];
9198 /* Check that the host and target are sane. */
9199 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
9202 if (VECTOR_TYPE_P (type
) && TREE_CODE (expr
) == VECTOR_CST
)
9203 if (tree res
= fold_view_convert_vector_encoding (type
, expr
))
9206 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
9210 return native_interpret_expr (type
, buffer
, len
);
9213 /* Build an expression for the address of T. Folds away INDIRECT_REF
9214 to avoid confusing the gimplify process. */
9217 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
9219 /* The size of the object is not relevant when talking about its address. */
9220 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
9221 t
= TREE_OPERAND (t
, 0);
9223 if (TREE_CODE (t
) == INDIRECT_REF
)
9225 t
= TREE_OPERAND (t
, 0);
9227 if (TREE_TYPE (t
) != ptrtype
)
9228 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
9230 else if (TREE_CODE (t
) == MEM_REF
9231 && integer_zerop (TREE_OPERAND (t
, 1)))
9233 t
= TREE_OPERAND (t
, 0);
9235 if (TREE_TYPE (t
) != ptrtype
)
9236 t
= fold_convert_loc (loc
, ptrtype
, t
);
9238 else if (TREE_CODE (t
) == MEM_REF
9239 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
9240 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
9241 TREE_OPERAND (t
, 0),
9242 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
9243 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
9245 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
9247 if (TREE_TYPE (t
) != ptrtype
)
9248 t
= fold_convert_loc (loc
, ptrtype
, t
);
9251 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
9256 /* Build an expression for the address of T. */
9259 build_fold_addr_expr_loc (location_t loc
, tree t
)
9261 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
9263 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
9266 /* Fold a unary expression of code CODE and type TYPE with operand
9267 OP0. Return the folded expression if folding is successful.
9268 Otherwise, return NULL_TREE. */
9271 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
9275 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9277 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9278 && TREE_CODE_LENGTH (code
) == 1);
9283 if (CONVERT_EXPR_CODE_P (code
)
9284 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
9286 /* Don't use STRIP_NOPS, because signedness of argument type
9288 STRIP_SIGN_NOPS (arg0
);
9292 /* Strip any conversions that don't change the mode. This
9293 is safe for every expression, except for a comparison
9294 expression because its signedness is derived from its
9297 Note that this is done as an internal manipulation within
9298 the constant folder, in order to find the simplest
9299 representation of the arguments so that their form can be
9300 studied. In any cases, the appropriate type conversions
9301 should be put back in the tree that will get out of the
9306 if (CONSTANT_CLASS_P (arg0
))
9308 tree tem
= const_unop (code
, type
, arg0
);
9311 if (TREE_TYPE (tem
) != type
)
9312 tem
= fold_convert_loc (loc
, type
, tem
);
9318 tem
= generic_simplify (loc
, code
, type
, op0
);
9322 if (TREE_CODE_CLASS (code
) == tcc_unary
)
9324 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9325 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9326 fold_build1_loc (loc
, code
, type
,
9327 fold_convert_loc (loc
, TREE_TYPE (op0
),
9328 TREE_OPERAND (arg0
, 1))));
9329 else if (TREE_CODE (arg0
) == COND_EXPR
)
9331 tree arg01
= TREE_OPERAND (arg0
, 1);
9332 tree arg02
= TREE_OPERAND (arg0
, 2);
9333 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
9334 arg01
= fold_build1_loc (loc
, code
, type
,
9335 fold_convert_loc (loc
,
9336 TREE_TYPE (op0
), arg01
));
9337 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
9338 arg02
= fold_build1_loc (loc
, code
, type
,
9339 fold_convert_loc (loc
,
9340 TREE_TYPE (op0
), arg02
));
9341 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9344 /* If this was a conversion, and all we did was to move into
9345 inside the COND_EXPR, bring it back out. But leave it if
9346 it is a conversion from integer to integer and the
9347 result precision is no wider than a word since such a
9348 conversion is cheap and may be optimized away by combine,
9349 while it couldn't if it were outside the COND_EXPR. Then return
9350 so we don't get into an infinite recursion loop taking the
9351 conversion out and then back in. */
9353 if ((CONVERT_EXPR_CODE_P (code
)
9354 || code
== NON_LVALUE_EXPR
)
9355 && TREE_CODE (tem
) == COND_EXPR
9356 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
9357 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
9358 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
9359 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
9360 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
9361 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
9362 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9364 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
9365 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
9366 || flag_syntax_only
))
9367 tem
= build1_loc (loc
, code
, type
,
9369 TREE_TYPE (TREE_OPERAND
9370 (TREE_OPERAND (tem
, 1), 0)),
9371 TREE_OPERAND (tem
, 0),
9372 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
9373 TREE_OPERAND (TREE_OPERAND (tem
, 2),
9381 case NON_LVALUE_EXPR
:
9382 if (!maybe_lvalue_p (op0
))
9383 return fold_convert_loc (loc
, type
, op0
);
9388 case FIX_TRUNC_EXPR
:
9389 if (COMPARISON_CLASS_P (op0
))
9391 /* If we have (type) (a CMP b) and type is an integral type, return
9392 new expression involving the new type. Canonicalize
9393 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9395 Do not fold the result as that would not simplify further, also
9396 folding again results in recursions. */
9397 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
9398 return build2_loc (loc
, TREE_CODE (op0
), type
,
9399 TREE_OPERAND (op0
, 0),
9400 TREE_OPERAND (op0
, 1));
9401 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
9402 && TREE_CODE (type
) != VECTOR_TYPE
)
9403 return build3_loc (loc
, COND_EXPR
, type
, op0
,
9404 constant_boolean_node (true, type
),
9405 constant_boolean_node (false, type
));
9408 /* Handle (T *)&A.B.C for A being of type T and B and C
9409 living at offset zero. This occurs frequently in
9410 C++ upcasting and then accessing the base. */
9411 if (TREE_CODE (op0
) == ADDR_EXPR
9412 && POINTER_TYPE_P (type
)
9413 && handled_component_p (TREE_OPERAND (op0
, 0)))
9415 poly_int64 bitsize
, bitpos
;
9418 int unsignedp
, reversep
, volatilep
;
9420 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
9421 &offset
, &mode
, &unsignedp
, &reversep
,
9423 /* If the reference was to a (constant) zero offset, we can use
9424 the address of the base if it has the same base type
9425 as the result type and the pointer type is unqualified. */
9427 && known_eq (bitpos
, 0)
9428 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
9429 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
9430 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
9431 return fold_convert_loc (loc
, type
,
9432 build_fold_addr_expr_loc (loc
, base
));
9435 if (TREE_CODE (op0
) == MODIFY_EXPR
9436 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
9437 /* Detect assigning a bitfield. */
9438 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
9440 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
9442 /* Don't leave an assignment inside a conversion
9443 unless assigning a bitfield. */
9444 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
9445 /* First do the assignment, then return converted constant. */
9446 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
9447 suppress_warning (tem
/* What warning? */);
9448 TREE_USED (tem
) = 1;
9452 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9453 constants (if x has signed type, the sign bit cannot be set
9454 in c). This folds extension into the BIT_AND_EXPR.
9455 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9456 very likely don't have maximal range for their precision and this
9457 transformation effectively doesn't preserve non-maximal ranges. */
9458 if (TREE_CODE (type
) == INTEGER_TYPE
9459 && TREE_CODE (op0
) == BIT_AND_EXPR
9460 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
9462 tree and_expr
= op0
;
9463 tree and0
= TREE_OPERAND (and_expr
, 0);
9464 tree and1
= TREE_OPERAND (and_expr
, 1);
9467 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
9468 || (TYPE_PRECISION (type
)
9469 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
9471 else if (TYPE_PRECISION (TREE_TYPE (and1
))
9472 <= HOST_BITS_PER_WIDE_INT
9473 && tree_fits_uhwi_p (and1
))
9475 unsigned HOST_WIDE_INT cst
;
9477 cst
= tree_to_uhwi (and1
);
9478 cst
&= HOST_WIDE_INT_M1U
9479 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
9480 change
= (cst
== 0);
9482 && !flag_syntax_only
9483 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
9486 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
9487 and0
= fold_convert_loc (loc
, uns
, and0
);
9488 and1
= fold_convert_loc (loc
, uns
, and1
);
9493 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
9494 TREE_OVERFLOW (and1
));
9495 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9496 fold_convert_loc (loc
, type
, and0
), tem
);
9500 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9501 cast (T1)X will fold away. We assume that this happens when X itself
9503 if (POINTER_TYPE_P (type
)
9504 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9505 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
9507 tree arg00
= TREE_OPERAND (arg0
, 0);
9508 tree arg01
= TREE_OPERAND (arg0
, 1);
9510 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9511 when the pointed type needs higher alignment than
9512 the p+ first operand's pointed type. */
9514 && sanitize_flags_p (SANITIZE_ALIGNMENT
)
9515 && (min_align_of_type (TREE_TYPE (type
))
9516 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00
)))))
9519 arg00
= fold_convert_loc (loc
, type
, arg00
);
9520 return fold_build_pointer_plus_loc (loc
, arg00
, arg01
);
9523 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9524 of the same precision, and X is an integer type not narrower than
9525 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9526 if (INTEGRAL_TYPE_P (type
)
9527 && TREE_CODE (op0
) == BIT_NOT_EXPR
9528 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9529 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
9530 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
9532 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
9533 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9534 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
9535 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
9536 fold_convert_loc (loc
, type
, tem
));
9539 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9540 type of X and Y (integer types only). */
9541 if (INTEGRAL_TYPE_P (type
)
9542 && TREE_CODE (op0
) == MULT_EXPR
9543 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9544 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9546 /* Be careful not to introduce new overflows. */
9548 if (TYPE_OVERFLOW_WRAPS (type
))
9551 mult_type
= unsigned_type_for (type
);
9553 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9555 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
9556 fold_convert_loc (loc
, mult_type
,
9557 TREE_OPERAND (op0
, 0)),
9558 fold_convert_loc (loc
, mult_type
,
9559 TREE_OPERAND (op0
, 1)));
9560 return fold_convert_loc (loc
, type
, tem
);
9566 case VIEW_CONVERT_EXPR
:
9567 if (TREE_CODE (op0
) == MEM_REF
)
9569 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
9570 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
9571 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
9572 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
9573 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
9580 tem
= fold_negate_expr (loc
, arg0
);
9582 return fold_convert_loc (loc
, type
, tem
);
9586 /* Convert fabs((double)float) into (double)fabsf(float). */
9587 if (TREE_CODE (arg0
) == NOP_EXPR
9588 && TREE_CODE (type
) == REAL_TYPE
)
9590 tree targ0
= strip_float_extensions (arg0
);
9592 return fold_convert_loc (loc
, type
,
9593 fold_build1_loc (loc
, ABS_EXPR
,
9600 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9601 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9602 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9603 fold_convert_loc (loc
, type
,
9604 TREE_OPERAND (arg0
, 0)))))
9605 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
9606 fold_convert_loc (loc
, type
,
9607 TREE_OPERAND (arg0
, 1)));
9608 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9609 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9610 fold_convert_loc (loc
, type
,
9611 TREE_OPERAND (arg0
, 1)))))
9612 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
9613 fold_convert_loc (loc
, type
,
9614 TREE_OPERAND (arg0
, 0)), tem
);
9618 case TRUTH_NOT_EXPR
:
9619 /* Note that the operand of this must be an int
9620 and its values must be 0 or 1.
9621 ("true" is a fixed value perhaps depending on the language,
9622 but we don't handle values other than 1 correctly yet.) */
9623 tem
= fold_truth_not_expr (loc
, arg0
);
9626 return fold_convert_loc (loc
, type
, tem
);
9629 /* Fold *&X to X if X is an lvalue. */
9630 if (TREE_CODE (op0
) == ADDR_EXPR
)
9632 tree op00
= TREE_OPERAND (op0
, 0);
9634 || TREE_CODE (op00
) == PARM_DECL
9635 || TREE_CODE (op00
) == RESULT_DECL
)
9636 && !TREE_READONLY (op00
))
9643 } /* switch (code) */
9647 /* If the operation was a conversion do _not_ mark a resulting constant
9648 with TREE_OVERFLOW if the original constant was not. These conversions
9649 have implementation defined behavior and retaining the TREE_OVERFLOW
9650 flag here would confuse later passes such as VRP. */
9652 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
9653 tree type
, tree op0
)
9655 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
9657 && TREE_CODE (res
) == INTEGER_CST
9658 && TREE_CODE (op0
) == INTEGER_CST
9659 && CONVERT_EXPR_CODE_P (code
))
9660 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
9665 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9666 operands OP0 and OP1. LOC is the location of the resulting expression.
9667 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9668 Return the folded expression if folding is successful. Otherwise,
9669 return NULL_TREE. */
9671 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
9672 tree arg0
, tree arg1
, tree op0
, tree op1
)
9676 /* We only do these simplifications if we are optimizing. */
9680 /* Check for things like (A || B) && (A || C). We can convert this
9681 to A || (B && C). Note that either operator can be any of the four
9682 truth and/or operations and the transformation will still be
9683 valid. Also note that we only care about order for the
9684 ANDIF and ORIF operators. If B contains side effects, this
9685 might change the truth-value of A. */
9686 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9687 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
9688 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
9689 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
9690 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
9691 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
9693 tree a00
= TREE_OPERAND (arg0
, 0);
9694 tree a01
= TREE_OPERAND (arg0
, 1);
9695 tree a10
= TREE_OPERAND (arg1
, 0);
9696 tree a11
= TREE_OPERAND (arg1
, 1);
9697 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
9698 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
9699 && (code
== TRUTH_AND_EXPR
9700 || code
== TRUTH_OR_EXPR
));
9702 if (operand_equal_p (a00
, a10
, 0))
9703 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9704 fold_build2_loc (loc
, code
, type
, a01
, a11
));
9705 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
9706 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9707 fold_build2_loc (loc
, code
, type
, a01
, a10
));
9708 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
9709 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
9710 fold_build2_loc (loc
, code
, type
, a00
, a11
));
9712 /* This case if tricky because we must either have commutative
9713 operators or else A10 must not have side-effects. */
9715 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
9716 && operand_equal_p (a01
, a11
, 0))
9717 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
9718 fold_build2_loc (loc
, code
, type
, a00
, a10
),
9722 /* See if we can build a range comparison. */
9723 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
9726 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
9727 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
9729 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
9731 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
9734 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
9735 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
9737 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
9739 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
9742 /* Check for the possibility of merging component references. If our
9743 lhs is another similar operation, try to merge its rhs with our
9744 rhs. Then try to merge our lhs and rhs. */
9745 if (TREE_CODE (arg0
) == code
9746 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
9747 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
9748 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9750 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
9753 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
9754 if (param_logical_op_non_short_circuit
!= -1)
9755 logical_op_non_short_circuit
9756 = param_logical_op_non_short_circuit
;
9757 if (logical_op_non_short_circuit
9758 && !sanitize_coverage_p ()
9759 && (code
== TRUTH_AND_EXPR
9760 || code
== TRUTH_ANDIF_EXPR
9761 || code
== TRUTH_OR_EXPR
9762 || code
== TRUTH_ORIF_EXPR
))
9764 enum tree_code ncode
, icode
;
9766 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
9767 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
9768 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
9770 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9771 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9772 We don't want to pack more than two leafs to a non-IF AND/OR
9774 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9775 equal to IF-CODE, then we don't want to add right-hand operand.
9776 If the inner right-hand side of left-hand operand has
9777 side-effects, or isn't simple, then we can't add to it,
9778 as otherwise we might destroy if-sequence. */
9779 if (TREE_CODE (arg0
) == icode
9780 && simple_operand_p_2 (arg1
)
9781 /* Needed for sequence points to handle trappings, and
9783 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
9785 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
9787 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
9790 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9791 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9792 else if (TREE_CODE (arg1
) == icode
9793 && simple_operand_p_2 (arg0
)
9794 /* Needed for sequence points to handle trappings, and
9796 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
9798 tem
= fold_build2_loc (loc
, ncode
, type
,
9799 arg0
, TREE_OPERAND (arg1
, 0));
9800 return fold_build2_loc (loc
, icode
, type
, tem
,
9801 TREE_OPERAND (arg1
, 1));
9803 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9805 For sequence point consistancy, we need to check for trapping,
9806 and side-effects. */
9807 else if (code
== icode
&& simple_operand_p_2 (arg0
)
9808 && simple_operand_p_2 (arg1
))
9809 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
9815 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9816 by changing CODE to reduce the magnitude of constants involved in
9817 ARG0 of the comparison.
9818 Returns a canonicalized comparison tree if a simplification was
9819 possible, otherwise returns NULL_TREE.
9820 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9821 valid if signed overflow is undefined. */
9824 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
9825 tree arg0
, tree arg1
,
9826 bool *strict_overflow_p
)
9828 enum tree_code code0
= TREE_CODE (arg0
);
9829 tree t
, cst0
= NULL_TREE
;
9832 /* Match A +- CST code arg1. We can change this only if overflow
9834 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9835 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
9836 /* In principle pointers also have undefined overflow behavior,
9837 but that causes problems elsewhere. */
9838 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
9839 && (code0
== MINUS_EXPR
9840 || code0
== PLUS_EXPR
)
9841 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
9844 /* Identify the constant in arg0 and its sign. */
9845 cst0
= TREE_OPERAND (arg0
, 1);
9846 sgn0
= tree_int_cst_sgn (cst0
);
9848 /* Overflowed constants and zero will cause problems. */
9849 if (integer_zerop (cst0
)
9850 || TREE_OVERFLOW (cst0
))
9853 /* See if we can reduce the magnitude of the constant in
9854 arg0 by changing the comparison code. */
9855 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9857 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9859 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9860 else if (code
== GT_EXPR
9861 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9863 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9864 else if (code
== LE_EXPR
9865 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9867 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9868 else if (code
== GE_EXPR
9869 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9873 *strict_overflow_p
= true;
9875 /* Now build the constant reduced in magnitude. But not if that
9876 would produce one outside of its types range. */
9877 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
9879 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
9880 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
9882 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
9883 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
9886 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
9887 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
9888 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
9889 t
= fold_convert (TREE_TYPE (arg1
), t
);
9891 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
9894 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9895 overflow further. Try to decrease the magnitude of constants involved
9896 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9897 and put sole constants at the second argument position.
9898 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9901 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
9902 tree arg0
, tree arg1
)
9905 bool strict_overflow_p
;
9906 const char * const warnmsg
= G_("assuming signed overflow does not occur "
9907 "when reducing constant in comparison");
9909 /* Try canonicalization by simplifying arg0. */
9910 strict_overflow_p
= false;
9911 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
9912 &strict_overflow_p
);
9915 if (strict_overflow_p
)
9916 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9920 /* Try canonicalization by simplifying arg1 using the swapped
9922 code
= swap_tree_comparison (code
);
9923 strict_overflow_p
= false;
9924 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
9925 &strict_overflow_p
);
9926 if (t
&& strict_overflow_p
)
9927 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9931 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9932 space. This is used to avoid issuing overflow warnings for
9933 expressions like &p->x which cannot wrap. */
9936 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
9938 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
9941 if (maybe_lt (bitpos
, 0))
9944 poly_wide_int wi_offset
;
9945 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
9946 if (offset
== NULL_TREE
)
9947 wi_offset
= wi::zero (precision
);
9948 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
9951 wi_offset
= wi::to_poly_wide (offset
);
9953 wi::overflow_type overflow
;
9954 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
9956 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
9960 poly_uint64 total_hwi
, size
;
9961 if (!total
.to_uhwi (&total_hwi
)
9962 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
9964 || known_eq (size
, 0U))
9967 if (known_le (total_hwi
, size
))
9970 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9972 if (TREE_CODE (base
) == ADDR_EXPR
9973 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
9975 && maybe_ne (size
, 0U)
9976 && known_le (total_hwi
, size
))
9982 /* Return a positive integer when the symbol DECL is known to have
9983 a nonzero address, zero when it's known not to (e.g., it's a weak
9984 symbol), and a negative integer when the symbol is not yet in the
9985 symbol table and so whether or not its address is zero is unknown.
9986 For function local objects always return positive integer. */
9988 maybe_nonzero_address (tree decl
)
9990 /* Normally, don't do anything for variables and functions before symtab is
9991 built; it is quite possible that DECL will be declared weak later.
9992 But if folding_initializer, we need a constant answer now, so create
9993 the symtab entry and prevent later weak declaration. */
9994 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
9995 if (struct symtab_node
*symbol
9996 = (folding_initializer
9997 ? symtab_node::get_create (decl
)
9998 : symtab_node::get (decl
)))
9999 return symbol
->nonzero_address ();
10001 /* Function local objects are never NULL. */
10003 && (DECL_CONTEXT (decl
)
10004 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
10005 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
10011 /* Subroutine of fold_binary. This routine performs all of the
10012 transformations that are common to the equality/inequality
10013 operators (EQ_EXPR and NE_EXPR) and the ordering operators
10014 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10015 fold_binary should call fold_binary. Fold a comparison with
10016 tree code CODE and type TYPE with operands OP0 and OP1. Return
10017 the folded comparison or NULL_TREE. */
10020 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
10021 tree op0
, tree op1
)
10023 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
10024 tree arg0
, arg1
, tem
;
10029 STRIP_SIGN_NOPS (arg0
);
10030 STRIP_SIGN_NOPS (arg1
);
10032 /* For comparisons of pointers we can decompose it to a compile time
10033 comparison of the base objects and the offsets into the object.
10034 This requires at least one operand being an ADDR_EXPR or a
10035 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10036 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
10037 && (TREE_CODE (arg0
) == ADDR_EXPR
10038 || TREE_CODE (arg1
) == ADDR_EXPR
10039 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10040 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
10042 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
10043 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
10045 int volatilep
, reversep
, unsignedp
;
10046 bool indirect_base0
= false, indirect_base1
= false;
10048 /* Get base and offset for the access. Strip ADDR_EXPR for
10049 get_inner_reference, but put it back by stripping INDIRECT_REF
10050 off the base object if possible. indirect_baseN will be true
10051 if baseN is not an address but refers to the object itself. */
10053 if (TREE_CODE (arg0
) == ADDR_EXPR
)
10056 = get_inner_reference (TREE_OPERAND (arg0
, 0),
10057 &bitsize
, &bitpos0
, &offset0
, &mode
,
10058 &unsignedp
, &reversep
, &volatilep
);
10059 if (TREE_CODE (base0
) == INDIRECT_REF
)
10060 base0
= TREE_OPERAND (base0
, 0);
10062 indirect_base0
= true;
10064 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10066 base0
= TREE_OPERAND (arg0
, 0);
10067 STRIP_SIGN_NOPS (base0
);
10068 if (TREE_CODE (base0
) == ADDR_EXPR
)
10071 = get_inner_reference (TREE_OPERAND (base0
, 0),
10072 &bitsize
, &bitpos0
, &offset0
, &mode
,
10073 &unsignedp
, &reversep
, &volatilep
);
10074 if (TREE_CODE (base0
) == INDIRECT_REF
)
10075 base0
= TREE_OPERAND (base0
, 0);
10077 indirect_base0
= true;
10079 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
10080 offset0
= TREE_OPERAND (arg0
, 1);
10082 offset0
= size_binop (PLUS_EXPR
, offset0
,
10083 TREE_OPERAND (arg0
, 1));
10084 if (poly_int_tree_p (offset0
))
10086 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
10087 TYPE_PRECISION (sizetype
));
10088 tem
<<= LOG2_BITS_PER_UNIT
;
10090 if (tem
.to_shwi (&bitpos0
))
10091 offset0
= NULL_TREE
;
10096 if (TREE_CODE (arg1
) == ADDR_EXPR
)
10099 = get_inner_reference (TREE_OPERAND (arg1
, 0),
10100 &bitsize
, &bitpos1
, &offset1
, &mode
,
10101 &unsignedp
, &reversep
, &volatilep
);
10102 if (TREE_CODE (base1
) == INDIRECT_REF
)
10103 base1
= TREE_OPERAND (base1
, 0);
10105 indirect_base1
= true;
10107 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10109 base1
= TREE_OPERAND (arg1
, 0);
10110 STRIP_SIGN_NOPS (base1
);
10111 if (TREE_CODE (base1
) == ADDR_EXPR
)
10114 = get_inner_reference (TREE_OPERAND (base1
, 0),
10115 &bitsize
, &bitpos1
, &offset1
, &mode
,
10116 &unsignedp
, &reversep
, &volatilep
);
10117 if (TREE_CODE (base1
) == INDIRECT_REF
)
10118 base1
= TREE_OPERAND (base1
, 0);
10120 indirect_base1
= true;
10122 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
10123 offset1
= TREE_OPERAND (arg1
, 1);
10125 offset1
= size_binop (PLUS_EXPR
, offset1
,
10126 TREE_OPERAND (arg1
, 1));
10127 if (poly_int_tree_p (offset1
))
10129 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
10130 TYPE_PRECISION (sizetype
));
10131 tem
<<= LOG2_BITS_PER_UNIT
;
10133 if (tem
.to_shwi (&bitpos1
))
10134 offset1
= NULL_TREE
;
10138 /* If we have equivalent bases we might be able to simplify. */
10139 if (indirect_base0
== indirect_base1
10140 && operand_equal_p (base0
, base1
,
10141 indirect_base0
? OEP_ADDRESS_OF
: 0))
10143 /* We can fold this expression to a constant if the non-constant
10144 offset parts are equal. */
10145 if ((offset0
== offset1
10146 || (offset0
&& offset1
10147 && operand_equal_p (offset0
, offset1
, 0)))
10150 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10151 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10154 && maybe_ne (bitpos0
, bitpos1
)
10155 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10156 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10157 fold_overflow_warning (("assuming pointer wraparound does not "
10158 "occur when comparing P +- C1 with "
10160 WARN_STRICT_OVERFLOW_CONDITIONAL
);
10165 if (known_eq (bitpos0
, bitpos1
))
10166 return constant_boolean_node (true, type
);
10167 if (known_ne (bitpos0
, bitpos1
))
10168 return constant_boolean_node (false, type
);
10171 if (known_ne (bitpos0
, bitpos1
))
10172 return constant_boolean_node (true, type
);
10173 if (known_eq (bitpos0
, bitpos1
))
10174 return constant_boolean_node (false, type
);
10177 if (known_lt (bitpos0
, bitpos1
))
10178 return constant_boolean_node (true, type
);
10179 if (known_ge (bitpos0
, bitpos1
))
10180 return constant_boolean_node (false, type
);
10183 if (known_le (bitpos0
, bitpos1
))
10184 return constant_boolean_node (true, type
);
10185 if (known_gt (bitpos0
, bitpos1
))
10186 return constant_boolean_node (false, type
);
10189 if (known_ge (bitpos0
, bitpos1
))
10190 return constant_boolean_node (true, type
);
10191 if (known_lt (bitpos0
, bitpos1
))
10192 return constant_boolean_node (false, type
);
10195 if (known_gt (bitpos0
, bitpos1
))
10196 return constant_boolean_node (true, type
);
10197 if (known_le (bitpos0
, bitpos1
))
10198 return constant_boolean_node (false, type
);
10203 /* We can simplify the comparison to a comparison of the variable
10204 offset parts if the constant offset parts are equal.
10205 Be careful to use signed sizetype here because otherwise we
10206 mess with array offsets in the wrong way. This is possible
10207 because pointer arithmetic is restricted to retain within an
10208 object and overflow on pointer differences is undefined as of
10209 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10210 else if (known_eq (bitpos0
, bitpos1
)
10213 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10214 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10216 /* By converting to signed sizetype we cover middle-end pointer
10217 arithmetic which operates on unsigned pointer types of size
10218 type size and ARRAY_REF offsets which are properly sign or
10219 zero extended from their type in case it is narrower than
10221 if (offset0
== NULL_TREE
)
10222 offset0
= build_int_cst (ssizetype
, 0);
10224 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
10225 if (offset1
== NULL_TREE
)
10226 offset1
= build_int_cst (ssizetype
, 0);
10228 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
10231 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10232 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10233 fold_overflow_warning (("assuming pointer wraparound does not "
10234 "occur when comparing P +- C1 with "
10236 WARN_STRICT_OVERFLOW_COMPARISON
);
10238 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
10241 /* For equal offsets we can simplify to a comparison of the
10243 else if (known_eq (bitpos0
, bitpos1
)
10245 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
10247 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
10248 && ((offset0
== offset1
)
10249 || (offset0
&& offset1
10250 && operand_equal_p (offset0
, offset1
, 0))))
10252 if (indirect_base0
)
10253 base0
= build_fold_addr_expr_loc (loc
, base0
);
10254 if (indirect_base1
)
10255 base1
= build_fold_addr_expr_loc (loc
, base1
);
10256 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
10258 /* Comparison between an ordinary (non-weak) symbol and a null
10259 pointer can be eliminated since such symbols must have a non
10260 null address. In C, relational expressions between pointers
10261 to objects and null pointers are undefined. The results
10262 below follow the C++ rules with the additional property that
10263 every object pointer compares greater than a null pointer.
10265 else if (((DECL_P (base0
)
10266 && maybe_nonzero_address (base0
) > 0
10267 /* Avoid folding references to struct members at offset 0 to
10268 prevent tests like '&ptr->firstmember == 0' from getting
10269 eliminated. When ptr is null, although the -> expression
10270 is strictly speaking invalid, GCC retains it as a matter
10271 of QoI. See PR c/44555. */
10272 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
10273 || CONSTANT_CLASS_P (base0
))
10275 /* The caller guarantees that when one of the arguments is
10276 constant (i.e., null in this case) it is second. */
10277 && integer_zerop (arg1
))
10284 return constant_boolean_node (false, type
);
10288 return constant_boolean_node (true, type
);
10290 gcc_unreachable ();
10295 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10296 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10297 the resulting offset is smaller in absolute value than the
10298 original one and has the same sign. */
10299 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10300 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
10301 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10302 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10303 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
10304 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
10305 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10306 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
10308 tree const1
= TREE_OPERAND (arg0
, 1);
10309 tree const2
= TREE_OPERAND (arg1
, 1);
10310 tree variable1
= TREE_OPERAND (arg0
, 0);
10311 tree variable2
= TREE_OPERAND (arg1
, 0);
10313 const char * const warnmsg
= G_("assuming signed overflow does not "
10314 "occur when combining constants around "
10317 /* Put the constant on the side where it doesn't overflow and is
10318 of lower absolute value and of same sign than before. */
10319 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10320 ? MINUS_EXPR
: PLUS_EXPR
,
10322 if (!TREE_OVERFLOW (cst
)
10323 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
10324 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
10326 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10327 return fold_build2_loc (loc
, code
, type
,
10329 fold_build2_loc (loc
, TREE_CODE (arg1
),
10334 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10335 ? MINUS_EXPR
: PLUS_EXPR
,
10337 if (!TREE_OVERFLOW (cst
)
10338 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
10339 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
10341 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10342 return fold_build2_loc (loc
, code
, type
,
10343 fold_build2_loc (loc
, TREE_CODE (arg0
),
10350 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
10354 /* If we are comparing an expression that just has comparisons
10355 of two integer values, arithmetic expressions of those comparisons,
10356 and constants, we can simplify it. There are only three cases
10357 to check: the two values can either be equal, the first can be
10358 greater, or the second can be greater. Fold the expression for
10359 those three values. Since each value must be 0 or 1, we have
10360 eight possibilities, each of which corresponds to the constant 0
10361 or 1 or one of the six possible comparisons.
10363 This handles common cases like (a > b) == 0 but also handles
10364 expressions like ((x > y) - (y > x)) > 0, which supposedly
10365 occur in macroized code. */
10367 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
10369 tree cval1
= 0, cval2
= 0;
10371 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
10372 /* Don't handle degenerate cases here; they should already
10373 have been handled anyway. */
10374 && cval1
!= 0 && cval2
!= 0
10375 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
10376 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
10377 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
10378 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
10379 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
10380 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
10381 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
10383 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
10384 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
10386 /* We can't just pass T to eval_subst in case cval1 or cval2
10387 was the same as ARG1. */
10390 = fold_build2_loc (loc
, code
, type
,
10391 eval_subst (loc
, arg0
, cval1
, maxval
,
10395 = fold_build2_loc (loc
, code
, type
,
10396 eval_subst (loc
, arg0
, cval1
, maxval
,
10400 = fold_build2_loc (loc
, code
, type
,
10401 eval_subst (loc
, arg0
, cval1
, minval
,
10405 /* All three of these results should be 0 or 1. Confirm they are.
10406 Then use those values to select the proper code to use. */
10408 if (TREE_CODE (high_result
) == INTEGER_CST
10409 && TREE_CODE (equal_result
) == INTEGER_CST
10410 && TREE_CODE (low_result
) == INTEGER_CST
)
10412 /* Make a 3-bit mask with the high-order bit being the
10413 value for `>', the next for '=', and the low for '<'. */
10414 switch ((integer_onep (high_result
) * 4)
10415 + (integer_onep (equal_result
) * 2)
10416 + integer_onep (low_result
))
10419 /* Always false. */
10420 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10441 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10444 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
10453 /* Subroutine of fold_binary. Optimize complex multiplications of the
10454 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10455 argument EXPR represents the expression "z" of type TYPE. */
10458 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
10460 tree itype
= TREE_TYPE (type
);
10461 tree rpart
, ipart
, tem
;
10463 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
10465 rpart
= TREE_OPERAND (expr
, 0);
10466 ipart
= TREE_OPERAND (expr
, 1);
10468 else if (TREE_CODE (expr
) == COMPLEX_CST
)
10470 rpart
= TREE_REALPART (expr
);
10471 ipart
= TREE_IMAGPART (expr
);
10475 expr
= save_expr (expr
);
10476 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
10477 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
10480 rpart
= save_expr (rpart
);
10481 ipart
= save_expr (ipart
);
10482 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
10483 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
10484 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
10485 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
10486 build_zero_cst (itype
));
10490 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10491 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10492 true if successful. */
10495 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
10497 unsigned HOST_WIDE_INT i
, nunits
;
10499 if (TREE_CODE (arg
) == VECTOR_CST
10500 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
10502 for (i
= 0; i
< nunits
; ++i
)
10503 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
10505 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
10507 constructor_elt
*elt
;
10509 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
10510 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
10513 elts
[i
] = elt
->value
;
10517 for (; i
< nelts
; i
++)
10519 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
10523 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10524 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10525 NULL_TREE otherwise. */
10528 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10531 unsigned HOST_WIDE_INT nelts
;
10532 bool need_ctor
= false;
10534 if (!sel
.length ().is_constant (&nelts
))
10536 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), nelts
)
10537 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)), nelts
)
10538 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)), nelts
));
10539 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10540 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10543 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10544 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10545 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10548 tree_vector_builder
out_elts (type
, nelts
, 1);
10549 for (i
= 0; i
< nelts
; i
++)
10551 HOST_WIDE_INT index
;
10552 if (!sel
[i
].is_constant (&index
))
10554 if (!CONSTANT_CLASS_P (in_elts
[index
]))
10556 out_elts
.quick_push (unshare_expr (in_elts
[index
]));
10561 vec
<constructor_elt
, va_gc
> *v
;
10562 vec_alloc (v
, nelts
);
10563 for (i
= 0; i
< nelts
; i
++)
10564 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
10565 return build_constructor (type
, v
);
10568 return out_elts
.build ();
10571 /* Try to fold a pointer difference of type TYPE two address expressions of
10572 array references AREF0 and AREF1 using location LOC. Return a
10573 simplified expression for the difference or NULL_TREE. */
10576 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10577 tree aref0
, tree aref1
,
10578 bool use_pointer_diff
)
10580 tree base0
= TREE_OPERAND (aref0
, 0);
10581 tree base1
= TREE_OPERAND (aref1
, 0);
10582 tree base_offset
= build_int_cst (type
, 0);
10584 /* If the bases are array references as well, recurse. If the bases
10585 are pointer indirections compute the difference of the pointers.
10586 If the bases are equal, we are set. */
10587 if ((TREE_CODE (base0
) == ARRAY_REF
10588 && TREE_CODE (base1
) == ARRAY_REF
10590 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10591 use_pointer_diff
)))
10592 || (INDIRECT_REF_P (base0
)
10593 && INDIRECT_REF_P (base1
)
10596 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10597 TREE_OPERAND (base0
, 0),
10598 TREE_OPERAND (base1
, 0))
10599 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10600 fold_convert (type
,
10601 TREE_OPERAND (base0
, 0)),
10602 fold_convert (type
,
10603 TREE_OPERAND (base1
, 0)))))
10604 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10606 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10607 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10608 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10609 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10610 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10612 fold_build2_loc (loc
, MULT_EXPR
, type
,
10618 /* If the real or vector real constant CST of type TYPE has an exact
10619 inverse, return it, else return NULL. */
10622 exact_inverse (tree type
, tree cst
)
10628 switch (TREE_CODE (cst
))
10631 r
= TREE_REAL_CST (cst
);
10633 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10634 return build_real (type
, r
);
10640 unit_type
= TREE_TYPE (type
);
10641 mode
= TYPE_MODE (unit_type
);
10643 tree_vector_builder elts
;
10644 if (!elts
.new_unary_operation (type
, cst
, false))
10646 unsigned int count
= elts
.encoded_nelts ();
10647 for (unsigned int i
= 0; i
< count
; ++i
)
10649 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
10650 if (!exact_real_inverse (mode
, &r
))
10652 elts
.quick_push (build_real (unit_type
, r
));
10655 return elts
.build ();
10663 /* Mask out the tz least significant bits of X of type TYPE where
10664 tz is the number of trailing zeroes in Y. */
10666 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
10668 int tz
= wi::ctz (y
);
10670 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
10674 /* Return true when T is an address and is known to be nonzero.
10675 For floating point we further ensure that T is not denormal.
10676 Similar logic is present in nonzero_address in rtlanal.h.
10678 If the return value is based on the assumption that signed overflow
10679 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10680 change *STRICT_OVERFLOW_P. */
10683 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
10685 tree type
= TREE_TYPE (t
);
10686 enum tree_code code
;
10688 /* Doing something useful for floating point would need more work. */
10689 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
10692 code
= TREE_CODE (t
);
10693 switch (TREE_CODE_CLASS (code
))
10696 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10697 strict_overflow_p
);
10699 case tcc_comparison
:
10700 return tree_binary_nonzero_warnv_p (code
, type
,
10701 TREE_OPERAND (t
, 0),
10702 TREE_OPERAND (t
, 1),
10703 strict_overflow_p
);
10705 case tcc_declaration
:
10706 case tcc_reference
:
10707 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10715 case TRUTH_NOT_EXPR
:
10716 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10717 strict_overflow_p
);
10719 case TRUTH_AND_EXPR
:
10720 case TRUTH_OR_EXPR
:
10721 case TRUTH_XOR_EXPR
:
10722 return tree_binary_nonzero_warnv_p (code
, type
,
10723 TREE_OPERAND (t
, 0),
10724 TREE_OPERAND (t
, 1),
10725 strict_overflow_p
);
10732 case WITH_SIZE_EXPR
:
10734 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10736 case COMPOUND_EXPR
:
10739 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
10740 strict_overflow_p
);
10743 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
10744 strict_overflow_p
);
10748 tree fndecl
= get_callee_fndecl (t
);
10749 if (!fndecl
) return false;
10750 if (flag_delete_null_pointer_checks
&& !flag_check_new
10751 && DECL_IS_OPERATOR_NEW_P (fndecl
)
10752 && !TREE_NOTHROW (fndecl
))
10754 if (flag_delete_null_pointer_checks
10755 && lookup_attribute ("returns_nonnull",
10756 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
10758 return alloca_call_p (t
);
10767 /* Return true when T is an address and is known to be nonzero.
10768 Handle warnings about undefined signed overflow. */
10771 tree_expr_nonzero_p (tree t
)
10773 bool ret
, strict_overflow_p
;
10775 strict_overflow_p
= false;
10776 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
10777 if (strict_overflow_p
)
10778 fold_overflow_warning (("assuming signed overflow does not occur when "
10779 "determining that expression is always "
10781 WARN_STRICT_OVERFLOW_MISC
);
10785 /* Return true if T is known not to be equal to an integer W. */
10788 expr_not_equal_to (tree t
, const wide_int
&w
)
10791 switch (TREE_CODE (t
))
10794 return wi::to_wide (t
) != w
;
10797 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
10801 get_range_query (cfun
)->range_of_expr (vr
, t
);
10803 get_global_range_query ()->range_of_expr (vr
, t
);
10805 if (!vr
.undefined_p ()
10806 && !vr
.contains_p (wide_int_to_tree (TREE_TYPE (t
), w
)))
10808 /* If T has some known zero bits and W has any of those bits set,
10809 then T is known not to be equal to W. */
10810 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
10811 TYPE_PRECISION (TREE_TYPE (t
))), 0))
10820 /* Fold a binary expression of code CODE and type TYPE with operands
10821 OP0 and OP1. LOC is the location of the resulting expression.
10822 Return the folded expression if folding is successful. Otherwise,
10823 return NULL_TREE. */
10826 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
10827 tree op0
, tree op1
)
10829 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
10830 tree arg0
, arg1
, tem
;
10831 tree t1
= NULL_TREE
;
10832 bool strict_overflow_p
;
10835 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
10836 && TREE_CODE_LENGTH (code
) == 2
10837 && op0
!= NULL_TREE
10838 && op1
!= NULL_TREE
);
10843 /* Strip any conversions that don't change the mode. This is
10844 safe for every expression, except for a comparison expression
10845 because its signedness is derived from its operands. So, in
10846 the latter case, only strip conversions that don't change the
10847 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10850 Note that this is done as an internal manipulation within the
10851 constant folder, in order to find the simplest representation
10852 of the arguments so that their form can be studied. In any
10853 cases, the appropriate type conversions should be put back in
10854 the tree that will get out of the constant folder. */
10856 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
10858 STRIP_SIGN_NOPS (arg0
);
10859 STRIP_SIGN_NOPS (arg1
);
10867 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10868 constant but we can't do arithmetic on them. */
10869 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
10871 tem
= const_binop (code
, type
, arg0
, arg1
);
10872 if (tem
!= NULL_TREE
)
10874 if (TREE_TYPE (tem
) != type
)
10875 tem
= fold_convert_loc (loc
, type
, tem
);
10880 /* If this is a commutative operation, and ARG0 is a constant, move it
10881 to ARG1 to reduce the number of tests below. */
10882 if (commutative_tree_code (code
)
10883 && tree_swap_operands_p (arg0
, arg1
))
10884 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
10886 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10887 to ARG1 to reduce the number of tests below. */
10888 if (kind
== tcc_comparison
10889 && tree_swap_operands_p (arg0
, arg1
))
10890 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
10892 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
10896 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10898 First check for cases where an arithmetic operation is applied to a
10899 compound, conditional, or comparison operation. Push the arithmetic
10900 operation inside the compound or conditional to see if any folding
10901 can then be done. Convert comparison to conditional for this purpose.
10902 The also optimizes non-constant cases that used to be done in
10905 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10906 one of the operands is a comparison and the other is a comparison, a
10907 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10908 code below would make the expression more complex. Change it to a
10909 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10910 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10912 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
10913 || code
== EQ_EXPR
|| code
== NE_EXPR
)
10914 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
10915 && ((truth_value_p (TREE_CODE (arg0
))
10916 && (truth_value_p (TREE_CODE (arg1
))
10917 || (TREE_CODE (arg1
) == BIT_AND_EXPR
10918 && integer_onep (TREE_OPERAND (arg1
, 1)))))
10919 || (truth_value_p (TREE_CODE (arg1
))
10920 && (truth_value_p (TREE_CODE (arg0
))
10921 || (TREE_CODE (arg0
) == BIT_AND_EXPR
10922 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
10924 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
10925 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
10928 fold_convert_loc (loc
, boolean_type_node
, arg0
),
10929 fold_convert_loc (loc
, boolean_type_node
, arg1
));
10931 if (code
== EQ_EXPR
)
10932 tem
= invert_truthvalue_loc (loc
, tem
);
10934 return fold_convert_loc (loc
, type
, tem
);
10937 if (TREE_CODE_CLASS (code
) == tcc_binary
10938 || TREE_CODE_CLASS (code
) == tcc_comparison
)
10940 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
10942 tem
= fold_build2_loc (loc
, code
, type
,
10943 fold_convert_loc (loc
, TREE_TYPE (op0
),
10944 TREE_OPERAND (arg0
, 1)), op1
);
10945 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10948 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
10950 tem
= fold_build2_loc (loc
, code
, type
, op0
,
10951 fold_convert_loc (loc
, TREE_TYPE (op1
),
10952 TREE_OPERAND (arg1
, 1)));
10953 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
10957 if (TREE_CODE (arg0
) == COND_EXPR
10958 || TREE_CODE (arg0
) == VEC_COND_EXPR
10959 || COMPARISON_CLASS_P (arg0
))
10961 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10963 /*cond_first_p=*/1);
10964 if (tem
!= NULL_TREE
)
10968 if (TREE_CODE (arg1
) == COND_EXPR
10969 || TREE_CODE (arg1
) == VEC_COND_EXPR
10970 || COMPARISON_CLASS_P (arg1
))
10972 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10974 /*cond_first_p=*/0);
10975 if (tem
!= NULL_TREE
)
10983 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10984 if (TREE_CODE (arg0
) == ADDR_EXPR
10985 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
10987 tree iref
= TREE_OPERAND (arg0
, 0);
10988 return fold_build2 (MEM_REF
, type
,
10989 TREE_OPERAND (iref
, 0),
10990 int_const_binop (PLUS_EXPR
, arg1
,
10991 TREE_OPERAND (iref
, 1)));
10994 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10995 if (TREE_CODE (arg0
) == ADDR_EXPR
10996 && handled_component_p (TREE_OPERAND (arg0
, 0)))
10999 poly_int64 coffset
;
11000 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
11004 return fold_build2 (MEM_REF
, type
,
11005 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
11006 int_const_binop (PLUS_EXPR
, arg1
,
11007 size_int (coffset
)));
11012 case POINTER_PLUS_EXPR
:
11013 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11014 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11015 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
11016 return fold_convert_loc (loc
, type
,
11017 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
11018 fold_convert_loc (loc
, sizetype
,
11020 fold_convert_loc (loc
, sizetype
,
11026 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
11028 /* X + (X / CST) * -CST is X % CST. */
11029 if (TREE_CODE (arg1
) == MULT_EXPR
11030 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
11031 && operand_equal_p (arg0
,
11032 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
11034 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
11035 tree cst1
= TREE_OPERAND (arg1
, 1);
11036 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
11038 if (sum
&& integer_zerop (sum
))
11039 return fold_convert_loc (loc
, type
,
11040 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
11041 TREE_TYPE (arg0
), arg0
,
11046 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11047 one. Make sure the type is not saturating and has the signedness of
11048 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11049 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11050 if ((TREE_CODE (arg0
) == MULT_EXPR
11051 || TREE_CODE (arg1
) == MULT_EXPR
)
11052 && !TYPE_SATURATING (type
)
11053 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11054 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11055 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11057 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11062 if (! FLOAT_TYPE_P (type
))
11064 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11065 (plus (plus (mult) (mult)) (foo)) so that we can
11066 take advantage of the factoring cases below. */
11067 if (ANY_INTEGRAL_TYPE_P (type
)
11068 && TYPE_OVERFLOW_WRAPS (type
)
11069 && (((TREE_CODE (arg0
) == PLUS_EXPR
11070 || TREE_CODE (arg0
) == MINUS_EXPR
)
11071 && TREE_CODE (arg1
) == MULT_EXPR
)
11072 || ((TREE_CODE (arg1
) == PLUS_EXPR
11073 || TREE_CODE (arg1
) == MINUS_EXPR
)
11074 && TREE_CODE (arg0
) == MULT_EXPR
)))
11076 tree parg0
, parg1
, parg
, marg
;
11077 enum tree_code pcode
;
11079 if (TREE_CODE (arg1
) == MULT_EXPR
)
11080 parg
= arg0
, marg
= arg1
;
11082 parg
= arg1
, marg
= arg0
;
11083 pcode
= TREE_CODE (parg
);
11084 parg0
= TREE_OPERAND (parg
, 0);
11085 parg1
= TREE_OPERAND (parg
, 1);
11086 STRIP_NOPS (parg0
);
11087 STRIP_NOPS (parg1
);
11089 if (TREE_CODE (parg0
) == MULT_EXPR
11090 && TREE_CODE (parg1
) != MULT_EXPR
)
11091 return fold_build2_loc (loc
, pcode
, type
,
11092 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11093 fold_convert_loc (loc
, type
,
11095 fold_convert_loc (loc
, type
,
11097 fold_convert_loc (loc
, type
, parg1
));
11098 if (TREE_CODE (parg0
) != MULT_EXPR
11099 && TREE_CODE (parg1
) == MULT_EXPR
)
11101 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11102 fold_convert_loc (loc
, type
, parg0
),
11103 fold_build2_loc (loc
, pcode
, type
,
11104 fold_convert_loc (loc
, type
, marg
),
11105 fold_convert_loc (loc
, type
,
11111 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11112 to __complex__ ( x, y ). This is not the same for SNaNs or
11113 if signed zeros are involved. */
11114 if (!HONOR_SNANS (arg0
)
11115 && !HONOR_SIGNED_ZEROS (arg0
)
11116 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11118 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11119 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11120 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11121 bool arg0rz
= false, arg0iz
= false;
11122 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11123 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11125 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11126 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11127 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11129 tree rp
= arg1r
? arg1r
11130 : build1 (REALPART_EXPR
, rtype
, arg1
);
11131 tree ip
= arg0i
? arg0i
11132 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11133 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11135 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11137 tree rp
= arg0r
? arg0r
11138 : build1 (REALPART_EXPR
, rtype
, arg0
);
11139 tree ip
= arg1i
? arg1i
11140 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11141 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11146 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11147 We associate floats only if the user has specified
11148 -fassociative-math. */
11149 if (flag_associative_math
11150 && TREE_CODE (arg1
) == PLUS_EXPR
11151 && TREE_CODE (arg0
) != MULT_EXPR
)
11153 tree tree10
= TREE_OPERAND (arg1
, 0);
11154 tree tree11
= TREE_OPERAND (arg1
, 1);
11155 if (TREE_CODE (tree11
) == MULT_EXPR
11156 && TREE_CODE (tree10
) == MULT_EXPR
)
11159 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11160 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11163 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11164 We associate floats only if the user has specified
11165 -fassociative-math. */
11166 if (flag_associative_math
11167 && TREE_CODE (arg0
) == PLUS_EXPR
11168 && TREE_CODE (arg1
) != MULT_EXPR
)
11170 tree tree00
= TREE_OPERAND (arg0
, 0);
11171 tree tree01
= TREE_OPERAND (arg0
, 1);
11172 if (TREE_CODE (tree01
) == MULT_EXPR
11173 && TREE_CODE (tree00
) == MULT_EXPR
)
11176 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11177 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11183 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11184 is a rotate of A by C1 bits. */
11185 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11186 is a rotate of A by B bits.
11187 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11188 though in this case CODE must be | and not + or ^, otherwise
11189 it doesn't return A when B is 0. */
11191 enum tree_code code0
, code1
;
11193 code0
= TREE_CODE (arg0
);
11194 code1
= TREE_CODE (arg1
);
11195 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11196 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11197 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11198 TREE_OPERAND (arg1
, 0), 0)
11199 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11200 TYPE_UNSIGNED (rtype
))
11201 /* Only create rotates in complete modes. Other cases are not
11202 expanded properly. */
11203 && (element_precision (rtype
)
11204 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11206 tree tree01
, tree11
;
11207 tree orig_tree01
, orig_tree11
;
11208 enum tree_code code01
, code11
;
11210 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11211 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11212 STRIP_NOPS (tree01
);
11213 STRIP_NOPS (tree11
);
11214 code01
= TREE_CODE (tree01
);
11215 code11
= TREE_CODE (tree11
);
11216 if (code11
!= MINUS_EXPR
11217 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11219 std::swap (code0
, code1
);
11220 std::swap (code01
, code11
);
11221 std::swap (tree01
, tree11
);
11222 std::swap (orig_tree01
, orig_tree11
);
11224 if (code01
== INTEGER_CST
11225 && code11
== INTEGER_CST
11226 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11227 == element_precision (rtype
)))
11229 tem
= build2_loc (loc
, LROTATE_EXPR
,
11230 rtype
, TREE_OPERAND (arg0
, 0),
11231 code0
== LSHIFT_EXPR
11232 ? orig_tree01
: orig_tree11
);
11233 return fold_convert_loc (loc
, type
, tem
);
11235 else if (code11
== MINUS_EXPR
)
11237 tree tree110
, tree111
;
11238 tree110
= TREE_OPERAND (tree11
, 0);
11239 tree111
= TREE_OPERAND (tree11
, 1);
11240 STRIP_NOPS (tree110
);
11241 STRIP_NOPS (tree111
);
11242 if (TREE_CODE (tree110
) == INTEGER_CST
11243 && compare_tree_int (tree110
,
11244 element_precision (rtype
)) == 0
11245 && operand_equal_p (tree01
, tree111
, 0))
11247 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11248 ? LROTATE_EXPR
: RROTATE_EXPR
),
11249 rtype
, TREE_OPERAND (arg0
, 0),
11251 return fold_convert_loc (loc
, type
, tem
);
11254 else if (code
== BIT_IOR_EXPR
11255 && code11
== BIT_AND_EXPR
11256 && pow2p_hwi (element_precision (rtype
)))
11258 tree tree110
, tree111
;
11259 tree110
= TREE_OPERAND (tree11
, 0);
11260 tree111
= TREE_OPERAND (tree11
, 1);
11261 STRIP_NOPS (tree110
);
11262 STRIP_NOPS (tree111
);
11263 if (TREE_CODE (tree110
) == NEGATE_EXPR
11264 && TREE_CODE (tree111
) == INTEGER_CST
11265 && compare_tree_int (tree111
,
11266 element_precision (rtype
) - 1) == 0
11267 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11269 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11270 ? LROTATE_EXPR
: RROTATE_EXPR
),
11271 rtype
, TREE_OPERAND (arg0
, 0),
11273 return fold_convert_loc (loc
, type
, tem
);
11280 /* In most languages, can't associate operations on floats through
11281 parentheses. Rather than remember where the parentheses were, we
11282 don't associate floats at all, unless the user has specified
11283 -fassociative-math.
11284 And, we need to make sure type is not saturating. */
11286 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11287 && !TYPE_SATURATING (type
))
11289 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11290 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11294 /* Split both trees into variables, constants, and literals. Then
11295 associate each group together, the constants with literals,
11296 then the result with variables. This increases the chances of
11297 literals being recombined later and of generating relocatable
11298 expressions for the sum of a constant and literal. */
11299 var0
= split_tree (arg0
, type
, code
,
11300 &minus_var0
, &con0
, &minus_con0
,
11301 &lit0
, &minus_lit0
, 0);
11302 var1
= split_tree (arg1
, type
, code
,
11303 &minus_var1
, &con1
, &minus_con1
,
11304 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11306 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11307 if (code
== MINUS_EXPR
)
11310 /* With undefined overflow prefer doing association in a type
11311 which wraps on overflow, if that is one of the operand types. */
11312 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11313 && !TYPE_OVERFLOW_WRAPS (type
))
11315 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11316 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11317 atype
= TREE_TYPE (arg0
);
11318 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11319 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11320 atype
= TREE_TYPE (arg1
);
11321 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11324 /* With undefined overflow we can only associate constants with one
11325 variable, and constants whose association doesn't overflow. */
11326 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11327 && !TYPE_OVERFLOW_WRAPS (atype
))
11329 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11331 /* ??? If split_tree would handle NEGATE_EXPR we could
11332 simply reject these cases and the allowed cases would
11333 be the var0/minus_var1 ones. */
11334 tree tmp0
= var0
? var0
: minus_var0
;
11335 tree tmp1
= var1
? var1
: minus_var1
;
11336 bool one_neg
= false;
11338 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11340 tmp0
= TREE_OPERAND (tmp0
, 0);
11341 one_neg
= !one_neg
;
11343 if (CONVERT_EXPR_P (tmp0
)
11344 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11345 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11346 <= TYPE_PRECISION (atype
)))
11347 tmp0
= TREE_OPERAND (tmp0
, 0);
11348 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11350 tmp1
= TREE_OPERAND (tmp1
, 0);
11351 one_neg
= !one_neg
;
11353 if (CONVERT_EXPR_P (tmp1
)
11354 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11355 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11356 <= TYPE_PRECISION (atype
)))
11357 tmp1
= TREE_OPERAND (tmp1
, 0);
11358 /* The only case we can still associate with two variables
11359 is if they cancel out. */
11361 || !operand_equal_p (tmp0
, tmp1
, 0))
11364 else if ((var0
&& minus_var1
11365 && ! operand_equal_p (var0
, minus_var1
, 0))
11366 || (minus_var0
&& var1
11367 && ! operand_equal_p (minus_var0
, var1
, 0)))
11371 /* Only do something if we found more than two objects. Otherwise,
11372 nothing has changed and we risk infinite recursion. */
11374 && ((var0
!= 0) + (var1
!= 0)
11375 + (minus_var0
!= 0) + (minus_var1
!= 0)
11376 + (con0
!= 0) + (con1
!= 0)
11377 + (minus_con0
!= 0) + (minus_con1
!= 0)
11378 + (lit0
!= 0) + (lit1
!= 0)
11379 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11381 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11382 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11384 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11385 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11387 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11388 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11391 if (minus_var0
&& var0
)
11393 var0
= associate_trees (loc
, var0
, minus_var0
,
11394 MINUS_EXPR
, atype
);
11397 if (minus_con0
&& con0
)
11399 con0
= associate_trees (loc
, con0
, minus_con0
,
11400 MINUS_EXPR
, atype
);
11404 /* Preserve the MINUS_EXPR if the negative part of the literal is
11405 greater than the positive part. Otherwise, the multiplicative
11406 folding code (i.e extract_muldiv) may be fooled in case
11407 unsigned constants are subtracted, like in the following
11408 example: ((X*2 + 4) - 8U)/2. */
11409 if (minus_lit0
&& lit0
)
11411 if (TREE_CODE (lit0
) == INTEGER_CST
11412 && TREE_CODE (minus_lit0
) == INTEGER_CST
11413 && tree_int_cst_lt (lit0
, minus_lit0
)
11414 /* But avoid ending up with only negated parts. */
11417 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11418 MINUS_EXPR
, atype
);
11423 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11424 MINUS_EXPR
, atype
);
11429 /* Don't introduce overflows through reassociation. */
11430 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11431 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11434 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11435 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11437 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11441 /* Eliminate minus_con0. */
11445 con0
= associate_trees (loc
, con0
, minus_con0
,
11446 MINUS_EXPR
, atype
);
11448 var0
= associate_trees (loc
, var0
, minus_con0
,
11449 MINUS_EXPR
, atype
);
11451 gcc_unreachable ();
11455 /* Eliminate minus_var0. */
11459 con0
= associate_trees (loc
, con0
, minus_var0
,
11460 MINUS_EXPR
, atype
);
11462 gcc_unreachable ();
11467 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11474 case POINTER_DIFF_EXPR
:
11476 /* Fold &a[i] - &a[j] to i-j. */
11477 if (TREE_CODE (arg0
) == ADDR_EXPR
11478 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11479 && TREE_CODE (arg1
) == ADDR_EXPR
11480 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11482 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11483 TREE_OPERAND (arg0
, 0),
11484 TREE_OPERAND (arg1
, 0),
11486 == POINTER_DIFF_EXPR
);
11491 /* Further transformations are not for pointers. */
11492 if (code
== POINTER_DIFF_EXPR
)
11495 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11496 if (TREE_CODE (arg0
) == NEGATE_EXPR
11497 && negate_expr_p (op1
)
11498 /* If arg0 is e.g. unsigned int and type is int, then this could
11499 introduce UB, because if A is INT_MIN at runtime, the original
11500 expression can be well defined while the latter is not.
11502 && !(ANY_INTEGRAL_TYPE_P (type
)
11503 && TYPE_OVERFLOW_UNDEFINED (type
)
11504 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11505 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11506 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11507 fold_convert_loc (loc
, type
,
11508 TREE_OPERAND (arg0
, 0)));
11510 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11511 __complex__ ( x, -y ). This is not the same for SNaNs or if
11512 signed zeros are involved. */
11513 if (!HONOR_SNANS (arg0
)
11514 && !HONOR_SIGNED_ZEROS (arg0
)
11515 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11517 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11518 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11519 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11520 bool arg0rz
= false, arg0iz
= false;
11521 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11522 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11524 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11525 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11526 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11528 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11530 : build1 (REALPART_EXPR
, rtype
, arg1
));
11531 tree ip
= arg0i
? arg0i
11532 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11533 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11535 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11537 tree rp
= arg0r
? arg0r
11538 : build1 (REALPART_EXPR
, rtype
, arg0
);
11539 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11541 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11542 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11547 /* A - B -> A + (-B) if B is easily negatable. */
11548 if (negate_expr_p (op1
)
11549 && ! TYPE_OVERFLOW_SANITIZED (type
)
11550 && ((FLOAT_TYPE_P (type
)
11551 /* Avoid this transformation if B is a positive REAL_CST. */
11552 && (TREE_CODE (op1
) != REAL_CST
11553 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11554 || INTEGRAL_TYPE_P (type
)))
11555 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11556 fold_convert_loc (loc
, type
, arg0
),
11557 negate_expr (op1
));
11559 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11560 one. Make sure the type is not saturating and has the signedness of
11561 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11562 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11563 if ((TREE_CODE (arg0
) == MULT_EXPR
11564 || TREE_CODE (arg1
) == MULT_EXPR
)
11565 && !TYPE_SATURATING (type
)
11566 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11567 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11568 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11570 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11578 if (! FLOAT_TYPE_P (type
))
11580 /* Transform x * -C into -x * C if x is easily negatable. */
11581 if (TREE_CODE (op1
) == INTEGER_CST
11582 && tree_int_cst_sgn (op1
) == -1
11583 && negate_expr_p (op0
)
11584 && negate_expr_p (op1
)
11585 && (tem
= negate_expr (op1
)) != op1
11586 && ! TREE_OVERFLOW (tem
))
11587 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11588 fold_convert_loc (loc
, type
,
11589 negate_expr (op0
)), tem
);
11591 strict_overflow_p
= false;
11592 if (TREE_CODE (arg1
) == INTEGER_CST
11593 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11594 &strict_overflow_p
)) != 0)
11596 if (strict_overflow_p
)
11597 fold_overflow_warning (("assuming signed overflow does not "
11598 "occur when simplifying "
11600 WARN_STRICT_OVERFLOW_MISC
);
11601 return fold_convert_loc (loc
, type
, tem
);
11604 /* Optimize z * conj(z) for integer complex numbers. */
11605 if (TREE_CODE (arg0
) == CONJ_EXPR
11606 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11607 return fold_mult_zconjz (loc
, type
, arg1
);
11608 if (TREE_CODE (arg1
) == CONJ_EXPR
11609 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11610 return fold_mult_zconjz (loc
, type
, arg0
);
11614 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11615 This is not the same for NaNs or if signed zeros are
11617 if (!HONOR_NANS (arg0
)
11618 && !HONOR_SIGNED_ZEROS (arg0
)
11619 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11620 && TREE_CODE (arg1
) == COMPLEX_CST
11621 && real_zerop (TREE_REALPART (arg1
)))
11623 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11624 if (real_onep (TREE_IMAGPART (arg1
)))
11626 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11627 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11629 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11630 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11632 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11633 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11634 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11638 /* Optimize z * conj(z) for floating point complex numbers.
11639 Guarded by flag_unsafe_math_optimizations as non-finite
11640 imaginary components don't produce scalar results. */
11641 if (flag_unsafe_math_optimizations
11642 && TREE_CODE (arg0
) == CONJ_EXPR
11643 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11644 return fold_mult_zconjz (loc
, type
, arg1
);
11645 if (flag_unsafe_math_optimizations
11646 && TREE_CODE (arg1
) == CONJ_EXPR
11647 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11648 return fold_mult_zconjz (loc
, type
, arg0
);
11653 /* Canonicalize (X & C1) | C2. */
11654 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11655 && TREE_CODE (arg1
) == INTEGER_CST
11656 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11658 int width
= TYPE_PRECISION (type
), w
;
11659 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
11660 wide_int c2
= wi::to_wide (arg1
);
11662 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11663 if ((c1
& c2
) == c1
)
11664 return omit_one_operand_loc (loc
, type
, arg1
,
11665 TREE_OPERAND (arg0
, 0));
11667 wide_int msk
= wi::mask (width
, false,
11668 TYPE_PRECISION (TREE_TYPE (arg1
)));
11670 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11671 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
11673 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11674 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11677 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11678 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11679 mode which allows further optimizations. */
11682 wide_int c3
= wi::bit_and_not (c1
, c2
);
11683 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
11685 wide_int mask
= wi::mask (w
, false,
11686 TYPE_PRECISION (type
));
11687 if (((c1
| c2
) & mask
) == mask
11688 && wi::bit_and_not (c1
, mask
) == 0)
11697 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11698 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
11699 wide_int_to_tree (type
, c3
));
11700 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11704 /* See if this can be simplified into a rotate first. If that
11705 is unsuccessful continue in the association code. */
11709 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11710 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11711 && INTEGRAL_TYPE_P (type
)
11712 && integer_onep (TREE_OPERAND (arg0
, 1))
11713 && integer_onep (arg1
))
11714 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11715 build_zero_cst (TREE_TYPE (arg0
)));
11717 /* See if this can be simplified into a rotate first. If that
11718 is unsuccessful continue in the association code. */
11722 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11723 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11724 && INTEGRAL_TYPE_P (type
)
11725 && integer_onep (TREE_OPERAND (arg0
, 1))
11726 && integer_onep (arg1
))
11729 tem
= TREE_OPERAND (arg0
, 0);
11730 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11731 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11733 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11734 build_zero_cst (TREE_TYPE (tem
)));
11736 /* Fold ~X & 1 as (X & 1) == 0. */
11737 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11738 && INTEGRAL_TYPE_P (type
)
11739 && integer_onep (arg1
))
11742 tem
= TREE_OPERAND (arg0
, 0);
11743 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11744 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11746 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11747 build_zero_cst (TREE_TYPE (tem
)));
11749 /* Fold !X & 1 as X == 0. */
11750 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11751 && integer_onep (arg1
))
11753 tem
= TREE_OPERAND (arg0
, 0);
11754 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11755 build_zero_cst (TREE_TYPE (tem
)));
11758 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11759 multiple of 1 << CST. */
11760 if (TREE_CODE (arg1
) == INTEGER_CST
)
11762 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
11763 wide_int ncst1
= -cst1
;
11764 if ((cst1
& ncst1
) == ncst1
11765 && multiple_of_p (type
, arg0
,
11766 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11767 return fold_convert_loc (loc
, type
, arg0
);
11770 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11772 if (TREE_CODE (arg1
) == INTEGER_CST
11773 && TREE_CODE (arg0
) == MULT_EXPR
11774 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11776 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
11778 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
11781 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11783 else if (masked
!= warg1
)
11785 /* Avoid the transform if arg1 is a mask of some
11786 mode which allows further optimizations. */
11787 int pop
= wi::popcount (warg1
);
11788 if (!(pop
>= BITS_PER_UNIT
11790 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
11791 return fold_build2_loc (loc
, code
, type
, op0
,
11792 wide_int_to_tree (type
, masked
));
11796 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11797 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11798 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11800 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11802 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
11805 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11811 /* Don't touch a floating-point divide by zero unless the mode
11812 of the constant can represent infinity. */
11813 if (TREE_CODE (arg1
) == REAL_CST
11814 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
11815 && real_zerop (arg1
))
11818 /* (-A) / (-B) -> A / B */
11819 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
11820 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11821 TREE_OPERAND (arg0
, 0),
11822 negate_expr (arg1
));
11823 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11824 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11825 negate_expr (arg0
),
11826 TREE_OPERAND (arg1
, 0));
11829 case TRUNC_DIV_EXPR
:
11832 case FLOOR_DIV_EXPR
:
11833 /* Simplify A / (B << N) where A and B are positive and B is
11834 a power of 2, to A >> (N + log2(B)). */
11835 strict_overflow_p
= false;
11836 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11837 && (TYPE_UNSIGNED (type
)
11838 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
11840 tree sval
= TREE_OPERAND (arg1
, 0);
11841 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
11843 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
11844 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
11845 wi::exact_log2 (wi::to_wide (sval
)));
11847 if (strict_overflow_p
)
11848 fold_overflow_warning (("assuming signed overflow does not "
11849 "occur when simplifying A / (B << N)"),
11850 WARN_STRICT_OVERFLOW_MISC
);
11852 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
11854 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11855 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
11861 case ROUND_DIV_EXPR
:
11862 case CEIL_DIV_EXPR
:
11863 case EXACT_DIV_EXPR
:
11864 if (integer_zerop (arg1
))
11867 /* Convert -A / -B to A / B when the type is signed and overflow is
11869 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11870 && TREE_CODE (op0
) == NEGATE_EXPR
11871 && negate_expr_p (op1
))
11873 if (ANY_INTEGRAL_TYPE_P (type
))
11874 fold_overflow_warning (("assuming signed overflow does not occur "
11875 "when distributing negation across "
11877 WARN_STRICT_OVERFLOW_MISC
);
11878 return fold_build2_loc (loc
, code
, type
,
11879 fold_convert_loc (loc
, type
,
11880 TREE_OPERAND (arg0
, 0)),
11881 negate_expr (op1
));
11883 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11884 && TREE_CODE (arg1
) == NEGATE_EXPR
11885 && negate_expr_p (op0
))
11887 if (ANY_INTEGRAL_TYPE_P (type
))
11888 fold_overflow_warning (("assuming signed overflow does not occur "
11889 "when distributing negation across "
11891 WARN_STRICT_OVERFLOW_MISC
);
11892 return fold_build2_loc (loc
, code
, type
,
11894 fold_convert_loc (loc
, type
,
11895 TREE_OPERAND (arg1
, 0)));
11898 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11899 operation, EXACT_DIV_EXPR.
11901 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11902 At one time others generated faster code, it's not clear if they do
11903 after the last round to changes to the DIV code in expmed.cc. */
11904 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
11905 && multiple_of_p (type
, arg0
, arg1
))
11906 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
11907 fold_convert (type
, arg0
),
11908 fold_convert (type
, arg1
));
11910 strict_overflow_p
= false;
11911 if (TREE_CODE (arg1
) == INTEGER_CST
11912 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11913 &strict_overflow_p
)) != 0)
11915 if (strict_overflow_p
)
11916 fold_overflow_warning (("assuming signed overflow does not occur "
11917 "when simplifying division"),
11918 WARN_STRICT_OVERFLOW_MISC
);
11919 return fold_convert_loc (loc
, type
, tem
);
11924 case CEIL_MOD_EXPR
:
11925 case FLOOR_MOD_EXPR
:
11926 case ROUND_MOD_EXPR
:
11927 case TRUNC_MOD_EXPR
:
11928 strict_overflow_p
= false;
11929 if (TREE_CODE (arg1
) == INTEGER_CST
11930 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11931 &strict_overflow_p
)) != 0)
11933 if (strict_overflow_p
)
11934 fold_overflow_warning (("assuming signed overflow does not occur "
11935 "when simplifying modulus"),
11936 WARN_STRICT_OVERFLOW_MISC
);
11937 return fold_convert_loc (loc
, type
, tem
);
11946 /* Since negative shift count is not well-defined,
11947 don't try to compute it in the compiler. */
11948 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
11951 prec
= element_precision (type
);
11953 /* If we have a rotate of a bit operation with the rotate count and
11954 the second operand of the bit operation both constant,
11955 permute the two operations. */
11956 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11957 && (TREE_CODE (arg0
) == BIT_AND_EXPR
11958 || TREE_CODE (arg0
) == BIT_IOR_EXPR
11959 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
11960 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11962 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11963 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11964 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
11965 fold_build2_loc (loc
, code
, type
,
11967 fold_build2_loc (loc
, code
, type
,
11971 /* Two consecutive rotates adding up to the some integer
11972 multiple of the precision of the type can be ignored. */
11973 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11974 && TREE_CODE (arg0
) == RROTATE_EXPR
11975 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11976 && wi::umod_trunc (wi::to_wide (arg1
)
11977 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
11979 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11987 case TRUTH_ANDIF_EXPR
:
11988 /* Note that the operands of this must be ints
11989 and their values must be 0 or 1.
11990 ("true" is a fixed value perhaps depending on the language.) */
11991 /* If first arg is constant zero, return it. */
11992 if (integer_zerop (arg0
))
11993 return fold_convert_loc (loc
, type
, arg0
);
11995 case TRUTH_AND_EXPR
:
11996 /* If either arg is constant true, drop it. */
11997 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11998 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11999 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
12000 /* Preserve sequence points. */
12001 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12002 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12003 /* If second arg is constant zero, result is zero, but first arg
12004 must be evaluated. */
12005 if (integer_zerop (arg1
))
12006 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12007 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12008 case will be handled here. */
12009 if (integer_zerop (arg0
))
12010 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12012 /* !X && X is always false. */
12013 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12014 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12015 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12016 /* X && !X is always false. */
12017 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12018 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12019 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12021 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12022 means A >= Y && A != MAX, but in this case we know that
12025 if (!TREE_SIDE_EFFECTS (arg0
)
12026 && !TREE_SIDE_EFFECTS (arg1
))
12028 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12029 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12030 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
12032 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12033 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12034 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
12037 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12043 case TRUTH_ORIF_EXPR
:
12044 /* Note that the operands of this must be ints
12045 and their values must be 0 or true.
12046 ("true" is a fixed value perhaps depending on the language.) */
12047 /* If first arg is constant true, return it. */
12048 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12049 return fold_convert_loc (loc
, type
, arg0
);
12051 case TRUTH_OR_EXPR
:
12052 /* If either arg is constant zero, drop it. */
12053 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12054 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12055 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12056 /* Preserve sequence points. */
12057 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12058 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12059 /* If second arg is constant true, result is true, but we must
12060 evaluate first arg. */
12061 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12062 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12063 /* Likewise for first arg, but note this only occurs here for
12065 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12066 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12068 /* !X || X is always true. */
12069 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12070 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12071 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12072 /* X || !X is always true. */
12073 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12074 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12075 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12077 /* (X && !Y) || (!X && Y) is X ^ Y */
12078 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12079 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12081 tree a0
, a1
, l0
, l1
, n0
, n1
;
12083 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12084 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12086 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12087 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12089 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12090 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12092 if ((operand_equal_p (n0
, a0
, 0)
12093 && operand_equal_p (n1
, a1
, 0))
12094 || (operand_equal_p (n0
, a1
, 0)
12095 && operand_equal_p (n1
, a0
, 0)))
12096 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12099 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12105 case TRUTH_XOR_EXPR
:
12106 /* If the second arg is constant zero, drop it. */
12107 if (integer_zerop (arg1
))
12108 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12109 /* If the second arg is constant true, this is a logical inversion. */
12110 if (integer_onep (arg1
))
12112 tem
= invert_truthvalue_loc (loc
, arg0
);
12113 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12115 /* Identical arguments cancel to zero. */
12116 if (operand_equal_p (arg0
, arg1
, 0))
12117 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12119 /* !X ^ X is always true. */
12120 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12121 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12122 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12124 /* X ^ !X is always true. */
12125 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12126 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12127 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12136 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12137 if (tem
!= NULL_TREE
)
12140 /* bool_var != 1 becomes !bool_var. */
12141 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12142 && code
== NE_EXPR
)
12143 return fold_convert_loc (loc
, type
,
12144 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12145 TREE_TYPE (arg0
), arg0
));
12147 /* bool_var == 0 becomes !bool_var. */
12148 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12149 && code
== EQ_EXPR
)
12150 return fold_convert_loc (loc
, type
,
12151 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12152 TREE_TYPE (arg0
), arg0
));
12154 /* !exp != 0 becomes !exp */
12155 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12156 && code
== NE_EXPR
)
12157 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12159 /* If this is an EQ or NE comparison with zero and ARG0 is
12160 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12161 two operations, but the latter can be done in one less insn
12162 on machines that have only two-operand insns or on which a
12163 constant cannot be the first operand. */
12164 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12165 && integer_zerop (arg1
))
12167 tree arg00
= TREE_OPERAND (arg0
, 0);
12168 tree arg01
= TREE_OPERAND (arg0
, 1);
12169 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12170 && integer_onep (TREE_OPERAND (arg00
, 0)))
12172 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12173 arg01
, TREE_OPERAND (arg00
, 1));
12174 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12175 build_one_cst (TREE_TYPE (arg0
)));
12176 return fold_build2_loc (loc
, code
, type
,
12177 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12180 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12181 && integer_onep (TREE_OPERAND (arg01
, 0)))
12183 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12184 arg00
, TREE_OPERAND (arg01
, 1));
12185 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12186 build_one_cst (TREE_TYPE (arg0
)));
12187 return fold_build2_loc (loc
, code
, type
,
12188 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12193 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12194 C1 is a valid shift constant, and C2 is a power of two, i.e.
12196 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12197 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12198 && integer_zerop (arg1
))
12200 tree arg00
= TREE_OPERAND (arg0
, 0);
12201 STRIP_NOPS (arg00
);
12202 if (TREE_CODE (arg00
) == RSHIFT_EXPR
12203 && TREE_CODE (TREE_OPERAND (arg00
, 1)) == INTEGER_CST
)
12205 tree itype
= TREE_TYPE (arg00
);
12206 tree arg001
= TREE_OPERAND (arg00
, 1);
12207 prec
= TYPE_PRECISION (itype
);
12209 /* Check for a valid shift count. */
12210 if (wi::ltu_p (wi::to_wide (arg001
), prec
))
12212 tree arg01
= TREE_OPERAND (arg0
, 1);
12213 tree arg000
= TREE_OPERAND (arg00
, 0);
12214 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
12215 /* If (C2 << C1) doesn't overflow, then
12216 ((X >> C1) & C2) != 0 can be rewritten as
12217 (X & (C2 << C1)) != 0. */
12218 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
12220 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
,
12222 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12224 return fold_build2_loc (loc
, code
, type
, tem
,
12225 fold_convert_loc (loc
, itype
, arg1
));
12227 /* Otherwise, for signed (arithmetic) shifts,
12228 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12229 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12230 else if (!TYPE_UNSIGNED (itype
))
12231 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
12234 build_int_cst (itype
, 0));
12235 /* Otherwise, of unsigned (logical) shifts,
12236 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12237 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12239 return omit_one_operand_loc (loc
, type
,
12240 code
== EQ_EXPR
? integer_one_node
12241 : integer_zero_node
,
12247 /* If this is a comparison of a field, we may be able to simplify it. */
12248 if ((TREE_CODE (arg0
) == COMPONENT_REF
12249 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12250 /* Handle the constant case even without -O
12251 to make sure the warnings are given. */
12252 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12254 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12259 /* Optimize comparisons of strlen vs zero to a compare of the
12260 first character of the string vs zero. To wit,
12261 strlen(ptr) == 0 => *ptr == 0
12262 strlen(ptr) != 0 => *ptr != 0
12263 Other cases should reduce to one of these two (or a constant)
12264 due to the return value of strlen being unsigned. */
12265 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12267 tree fndecl
= get_callee_fndecl (arg0
);
12270 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12271 && call_expr_nargs (arg0
) == 1
12272 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12276 = build_pointer_type (build_qualified_type (char_type_node
,
12278 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12279 CALL_EXPR_ARG (arg0
, 0));
12280 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12281 return fold_build2_loc (loc
, code
, type
, iref
,
12282 build_int_cst (TREE_TYPE (iref
), 0));
12286 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12287 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12288 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12289 && integer_zerop (arg1
)
12290 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12292 tree arg00
= TREE_OPERAND (arg0
, 0);
12293 tree arg01
= TREE_OPERAND (arg0
, 1);
12294 tree itype
= TREE_TYPE (arg00
);
12295 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12297 if (TYPE_UNSIGNED (itype
))
12299 itype
= signed_type_for (itype
);
12300 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12302 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12303 type
, arg00
, build_zero_cst (itype
));
12307 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12308 (X & C) == 0 when C is a single bit. */
12309 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12310 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12311 && integer_zerop (arg1
)
12312 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12314 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12315 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12316 TREE_OPERAND (arg0
, 1));
12317 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12319 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12323 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12324 constant C is a power of two, i.e. a single bit. */
12325 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12326 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_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 arg00
= TREE_OPERAND (arg0
, 0);
12333 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12334 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12337 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12338 when is C is a power of two, i.e. a single bit. */
12339 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12340 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12341 && integer_zerop (arg1
)
12342 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12343 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12344 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12346 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12347 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12348 arg000
, TREE_OPERAND (arg0
, 1));
12349 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12350 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12353 if (integer_zerop (arg1
)
12354 && tree_expr_nonzero_p (arg0
))
12356 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
12357 return omit_one_operand_loc (loc
, type
, res
, arg0
);
12360 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12361 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12363 tree arg00
= TREE_OPERAND (arg0
, 0);
12364 tree arg01
= TREE_OPERAND (arg0
, 1);
12365 tree arg10
= TREE_OPERAND (arg1
, 0);
12366 tree arg11
= TREE_OPERAND (arg1
, 1);
12367 tree itype
= TREE_TYPE (arg0
);
12369 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12370 operand_equal_p guarantees no side-effects so we don't need
12371 to use omit_one_operand on Z. */
12372 if (operand_equal_p (arg01
, arg11
, 0))
12373 return fold_build2_loc (loc
, code
, type
, arg00
,
12374 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12376 if (operand_equal_p (arg01
, arg10
, 0))
12377 return fold_build2_loc (loc
, code
, type
, arg00
,
12378 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12380 if (operand_equal_p (arg00
, arg11
, 0))
12381 return fold_build2_loc (loc
, code
, type
, arg01
,
12382 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12384 if (operand_equal_p (arg00
, arg10
, 0))
12385 return fold_build2_loc (loc
, code
, type
, arg01
,
12386 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12389 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12390 if (TREE_CODE (arg01
) == INTEGER_CST
12391 && TREE_CODE (arg11
) == INTEGER_CST
)
12393 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12394 fold_convert_loc (loc
, itype
, arg11
));
12395 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12396 return fold_build2_loc (loc
, code
, type
, tem
,
12397 fold_convert_loc (loc
, itype
, arg10
));
12401 /* Attempt to simplify equality/inequality comparisons of complex
12402 values. Only lower the comparison if the result is known or
12403 can be simplified to a single scalar comparison. */
12404 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12405 || TREE_CODE (arg0
) == COMPLEX_CST
)
12406 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12407 || TREE_CODE (arg1
) == COMPLEX_CST
))
12409 tree real0
, imag0
, real1
, imag1
;
12412 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12414 real0
= TREE_OPERAND (arg0
, 0);
12415 imag0
= TREE_OPERAND (arg0
, 1);
12419 real0
= TREE_REALPART (arg0
);
12420 imag0
= TREE_IMAGPART (arg0
);
12423 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12425 real1
= TREE_OPERAND (arg1
, 0);
12426 imag1
= TREE_OPERAND (arg1
, 1);
12430 real1
= TREE_REALPART (arg1
);
12431 imag1
= TREE_IMAGPART (arg1
);
12434 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12435 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12437 if (integer_zerop (rcond
))
12439 if (code
== EQ_EXPR
)
12440 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12442 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12446 if (code
== NE_EXPR
)
12447 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12449 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12453 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12454 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12456 if (integer_zerop (icond
))
12458 if (code
== EQ_EXPR
)
12459 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12461 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12465 if (code
== NE_EXPR
)
12466 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12468 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12479 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12480 if (tem
!= NULL_TREE
)
12483 /* Transform comparisons of the form X +- C CMP X. */
12484 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12485 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12486 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12487 && !HONOR_SNANS (arg0
))
12489 tree arg01
= TREE_OPERAND (arg0
, 1);
12490 enum tree_code code0
= TREE_CODE (arg0
);
12491 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12493 /* (X - c) > X becomes false. */
12494 if (code
== GT_EXPR
12495 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12496 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12497 return constant_boolean_node (0, type
);
12499 /* Likewise (X + c) < X becomes false. */
12500 if (code
== LT_EXPR
12501 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12502 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12503 return constant_boolean_node (0, type
);
12505 /* Convert (X - c) <= X to true. */
12506 if (!HONOR_NANS (arg1
)
12508 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12509 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12510 return constant_boolean_node (1, type
);
12512 /* Convert (X + c) >= X to true. */
12513 if (!HONOR_NANS (arg1
)
12515 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12516 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12517 return constant_boolean_node (1, type
);
12520 /* If we are comparing an ABS_EXPR with a constant, we can
12521 convert all the cases into explicit comparisons, but they may
12522 well not be faster than doing the ABS and one comparison.
12523 But ABS (X) <= C is a range comparison, which becomes a subtraction
12524 and a comparison, and is probably faster. */
12525 if (code
== LE_EXPR
12526 && TREE_CODE (arg1
) == INTEGER_CST
12527 && TREE_CODE (arg0
) == ABS_EXPR
12528 && ! TREE_SIDE_EFFECTS (arg0
)
12529 && (tem
= negate_expr (arg1
)) != 0
12530 && TREE_CODE (tem
) == INTEGER_CST
12531 && !TREE_OVERFLOW (tem
))
12532 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12533 build2 (GE_EXPR
, type
,
12534 TREE_OPERAND (arg0
, 0), tem
),
12535 build2 (LE_EXPR
, type
,
12536 TREE_OPERAND (arg0
, 0), arg1
));
12538 /* Convert ABS_EXPR<x> >= 0 to true. */
12539 strict_overflow_p
= false;
12540 if (code
== GE_EXPR
12541 && (integer_zerop (arg1
)
12542 || (! HONOR_NANS (arg0
)
12543 && real_zerop (arg1
)))
12544 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12546 if (strict_overflow_p
)
12547 fold_overflow_warning (("assuming signed overflow does not occur "
12548 "when simplifying comparison of "
12549 "absolute value and zero"),
12550 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12551 return omit_one_operand_loc (loc
, type
,
12552 constant_boolean_node (true, type
),
12556 /* Convert ABS_EXPR<x> < 0 to false. */
12557 strict_overflow_p
= false;
12558 if (code
== LT_EXPR
12559 && (integer_zerop (arg1
) || real_zerop (arg1
))
12560 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12562 if (strict_overflow_p
)
12563 fold_overflow_warning (("assuming signed overflow does not occur "
12564 "when simplifying comparison of "
12565 "absolute value and zero"),
12566 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12567 return omit_one_operand_loc (loc
, type
,
12568 constant_boolean_node (false, type
),
12572 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12573 and similarly for >= into !=. */
12574 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12575 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12576 && TREE_CODE (arg1
) == LSHIFT_EXPR
12577 && integer_onep (TREE_OPERAND (arg1
, 0)))
12578 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12579 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12580 TREE_OPERAND (arg1
, 1)),
12581 build_zero_cst (TREE_TYPE (arg0
)));
12583 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12584 otherwise Y might be >= # of bits in X's type and thus e.g.
12585 (unsigned char) (1 << Y) for Y 15 might be 0.
12586 If the cast is widening, then 1 << Y should have unsigned type,
12587 otherwise if Y is number of bits in the signed shift type minus 1,
12588 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12589 31 might be 0xffffffff80000000. */
12590 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12591 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12592 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0
)))
12593 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12594 && CONVERT_EXPR_P (arg1
)
12595 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12596 && (element_precision (TREE_TYPE (arg1
))
12597 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12598 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12599 || (element_precision (TREE_TYPE (arg1
))
12600 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12601 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12603 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12604 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12605 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12606 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12607 build_zero_cst (TREE_TYPE (arg0
)));
12612 case UNORDERED_EXPR
:
12620 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12622 tree targ0
= strip_float_extensions (arg0
);
12623 tree targ1
= strip_float_extensions (arg1
);
12624 tree newtype
= TREE_TYPE (targ0
);
12626 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
12627 newtype
= TREE_TYPE (targ1
);
12629 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
12630 return fold_build2_loc (loc
, code
, type
,
12631 fold_convert_loc (loc
, newtype
, targ0
),
12632 fold_convert_loc (loc
, newtype
, targ1
));
12637 case COMPOUND_EXPR
:
12638 /* When pedantic, a compound expression can be neither an lvalue
12639 nor an integer constant expression. */
12640 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12642 /* Don't let (0, 0) be null pointer constant. */
12643 tem
= integer_zerop (arg1
) ? build1_loc (loc
, NOP_EXPR
, type
, arg1
)
12644 : fold_convert_loc (loc
, type
, arg1
);
12648 /* An ASSERT_EXPR should never be passed to fold_binary. */
12649 gcc_unreachable ();
12653 } /* switch (code) */
12656 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12657 ((A & N) + B) & M -> (A + B) & M
12658 Similarly if (N & M) == 0,
12659 ((A | N) + B) & M -> (A + B) & M
12660 and for - instead of + (or unary - instead of +)
12661 and/or ^ instead of |.
12662 If B is constant and (B & M) == 0, fold into A & M.
12664 This function is a helper for match.pd patterns. Return non-NULL
12665 type in which the simplified operation should be performed only
12666 if any optimization is possible.
12668 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12669 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12670 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12673 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12674 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12675 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12678 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12679 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12680 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12682 || (cst1
& (cst1
+ 1)) != 0
12683 || !INTEGRAL_TYPE_P (type
)
12684 || (!TYPE_OVERFLOW_WRAPS (type
)
12685 && TREE_CODE (type
) != INTEGER_TYPE
)
12686 || (wi::max_value (type
) & cst1
) != cst1
)
12689 enum tree_code codes
[2] = { code00
, code01
};
12690 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12694 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12695 arg1 (M) is == (1LL << cst) - 1.
12696 Store C into PMOP[0] and D into PMOP[1]. */
12699 which
= code
!= NEGATE_EXPR
;
12701 for (; which
>= 0; which
--)
12702 switch (codes
[which
])
12707 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12708 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12709 if (codes
[which
] == BIT_AND_EXPR
)
12714 else if (cst0
!= 0)
12716 /* If C or D is of the form (A & N) where
12717 (N & M) == M, or of the form (A | N) or
12718 (A ^ N) where (N & M) == 0, replace it with A. */
12719 pmop
[which
] = arg0xx
[2 * which
];
12722 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
12724 /* If C or D is a N where (N & M) == 0, it can be
12725 omitted (replaced with 0). */
12726 if ((code
== PLUS_EXPR
12727 || (code
== MINUS_EXPR
&& which
== 0))
12728 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
12729 pmop
[which
] = build_int_cst (type
, 0);
12730 /* Similarly, with C - N where (-N & M) == 0. */
12731 if (code
== MINUS_EXPR
12733 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
12734 pmop
[which
] = build_int_cst (type
, 0);
12737 gcc_unreachable ();
12740 /* Only build anything new if we optimized one or both arguments above. */
12741 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
12744 if (TYPE_OVERFLOW_WRAPS (type
))
12747 return unsigned_type_for (type
);
12750 /* Used by contains_label_[p1]. */
12752 struct contains_label_data
12754 hash_set
<tree
> *pset
;
12755 bool inside_switch_p
;
12758 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12759 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12760 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12763 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
12765 contains_label_data
*d
= (contains_label_data
*) data
;
12766 switch (TREE_CODE (*tp
))
12771 case CASE_LABEL_EXPR
:
12772 if (!d
->inside_switch_p
)
12777 if (!d
->inside_switch_p
)
12779 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
12781 d
->inside_switch_p
= true;
12782 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
12784 d
->inside_switch_p
= false;
12785 *walk_subtrees
= 0;
12790 *walk_subtrees
= 0;
12798 /* Return whether the sub-tree ST contains a label which is accessible from
12799 outside the sub-tree. */
12802 contains_label_p (tree st
)
12804 hash_set
<tree
> pset
;
12805 contains_label_data data
= { &pset
, false };
12806 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
12809 /* Fold a ternary expression of code CODE and type TYPE with operands
12810 OP0, OP1, and OP2. Return the folded expression if folding is
12811 successful. Otherwise, return NULL_TREE. */
12814 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
12815 tree op0
, tree op1
, tree op2
)
12818 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
12819 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
12821 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
12822 && TREE_CODE_LENGTH (code
) == 3);
12824 /* If this is a commutative operation, and OP0 is a constant, move it
12825 to OP1 to reduce the number of tests below. */
12826 if (commutative_ternary_tree_code (code
)
12827 && tree_swap_operands_p (op0
, op1
))
12828 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
12830 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
12834 /* Strip any conversions that don't change the mode. This is safe
12835 for every expression, except for a comparison expression because
12836 its signedness is derived from its operands. So, in the latter
12837 case, only strip conversions that don't change the signedness.
12839 Note that this is done as an internal manipulation within the
12840 constant folder, in order to find the simplest representation of
12841 the arguments so that their form can be studied. In any cases,
12842 the appropriate type conversions should be put back in the tree
12843 that will get out of the constant folder. */
12864 case COMPONENT_REF
:
12865 if (TREE_CODE (arg0
) == CONSTRUCTOR
12866 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
12868 unsigned HOST_WIDE_INT idx
;
12870 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
12877 case VEC_COND_EXPR
:
12878 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12879 so all simple results must be passed through pedantic_non_lvalue. */
12880 if (TREE_CODE (arg0
) == INTEGER_CST
)
12882 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
12883 tem
= integer_zerop (arg0
) ? op2
: op1
;
12884 /* Only optimize constant conditions when the selected branch
12885 has the same type as the COND_EXPR. This avoids optimizing
12886 away "c ? x : throw", where the throw has a void type.
12887 Avoid throwing away that operand which contains label. */
12888 if ((!TREE_SIDE_EFFECTS (unused_op
)
12889 || !contains_label_p (unused_op
))
12890 && (! VOID_TYPE_P (TREE_TYPE (tem
))
12891 || VOID_TYPE_P (type
)))
12892 return protected_set_expr_location_unshare (tem
, loc
);
12895 else if (TREE_CODE (arg0
) == VECTOR_CST
)
12897 unsigned HOST_WIDE_INT nelts
;
12898 if ((TREE_CODE (arg1
) == VECTOR_CST
12899 || TREE_CODE (arg1
) == CONSTRUCTOR
)
12900 && (TREE_CODE (arg2
) == VECTOR_CST
12901 || TREE_CODE (arg2
) == CONSTRUCTOR
)
12902 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
12904 vec_perm_builder
sel (nelts
, nelts
, 1);
12905 for (unsigned int i
= 0; i
< nelts
; i
++)
12907 tree val
= VECTOR_CST_ELT (arg0
, i
);
12908 if (integer_all_onesp (val
))
12909 sel
.quick_push (i
);
12910 else if (integer_zerop (val
))
12911 sel
.quick_push (nelts
+ i
);
12912 else /* Currently unreachable. */
12915 vec_perm_indices
indices (sel
, 2, nelts
);
12916 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
12917 if (t
!= NULL_TREE
)
12922 /* If we have A op B ? A : C, we may be able to convert this to a
12923 simpler expression, depending on the operation and the values
12924 of B and C. Signed zeros prevent all of these transformations,
12925 for reasons given above each one.
12927 Also try swapping the arguments and inverting the conditional. */
12928 if (COMPARISON_CLASS_P (arg0
)
12929 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
12930 && !HONOR_SIGNED_ZEROS (op1
))
12932 tem
= fold_cond_expr_with_comparison (loc
, type
, TREE_CODE (arg0
),
12933 TREE_OPERAND (arg0
, 0),
12934 TREE_OPERAND (arg0
, 1),
12940 if (COMPARISON_CLASS_P (arg0
)
12941 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
12942 && !HONOR_SIGNED_ZEROS (op2
))
12944 enum tree_code comp_code
= TREE_CODE (arg0
);
12945 tree arg00
= TREE_OPERAND (arg0
, 0);
12946 tree arg01
= TREE_OPERAND (arg0
, 1);
12947 comp_code
= invert_tree_comparison (comp_code
, HONOR_NANS (arg00
));
12948 if (comp_code
!= ERROR_MARK
)
12949 tem
= fold_cond_expr_with_comparison (loc
, type
, comp_code
,
12957 /* If the second operand is simpler than the third, swap them
12958 since that produces better jump optimization results. */
12959 if (truth_value_p (TREE_CODE (arg0
))
12960 && tree_swap_operands_p (op1
, op2
))
12962 location_t loc0
= expr_location_or (arg0
, loc
);
12963 /* See if this can be inverted. If it can't, possibly because
12964 it was a floating-point inequality comparison, don't do
12966 tem
= fold_invert_truthvalue (loc0
, arg0
);
12968 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
12971 /* Convert A ? 1 : 0 to simply A. */
12972 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
12973 : (integer_onep (op1
)
12974 && !VECTOR_TYPE_P (type
)))
12975 && integer_zerop (op2
)
12976 /* If we try to convert OP0 to our type, the
12977 call to fold will try to move the conversion inside
12978 a COND, which will recurse. In that case, the COND_EXPR
12979 is probably the best choice, so leave it alone. */
12980 && type
== TREE_TYPE (arg0
))
12981 return protected_set_expr_location_unshare (arg0
, loc
);
12983 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12984 over COND_EXPR in cases such as floating point comparisons. */
12985 if (integer_zerop (op1
)
12986 && code
== COND_EXPR
12987 && integer_onep (op2
)
12988 && !VECTOR_TYPE_P (type
)
12989 && truth_value_p (TREE_CODE (arg0
)))
12990 return fold_convert_loc (loc
, type
,
12991 invert_truthvalue_loc (loc
, arg0
));
12993 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12994 if (TREE_CODE (arg0
) == LT_EXPR
12995 && integer_zerop (TREE_OPERAND (arg0
, 1))
12996 && integer_zerop (op2
)
12997 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
12999 /* sign_bit_p looks through both zero and sign extensions,
13000 but for this optimization only sign extensions are
13002 tree tem2
= TREE_OPERAND (arg0
, 0);
13003 while (tem
!= tem2
)
13005 if (TREE_CODE (tem2
) != NOP_EXPR
13006 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
13011 tem2
= TREE_OPERAND (tem2
, 0);
13013 /* sign_bit_p only checks ARG1 bits within A's precision.
13014 If <sign bit of A> has wider type than A, bits outside
13015 of A's precision in <sign bit of A> need to be checked.
13016 If they are all 0, this optimization needs to be done
13017 in unsigned A's type, if they are all 1 in signed A's type,
13018 otherwise this can't be done. */
13020 && TYPE_PRECISION (TREE_TYPE (tem
))
13021 < TYPE_PRECISION (TREE_TYPE (arg1
))
13022 && TYPE_PRECISION (TREE_TYPE (tem
))
13023 < TYPE_PRECISION (type
))
13025 int inner_width
, outer_width
;
13028 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
13029 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
13030 if (outer_width
> TYPE_PRECISION (type
))
13031 outer_width
= TYPE_PRECISION (type
);
13033 wide_int mask
= wi::shifted_mask
13034 (inner_width
, outer_width
- inner_width
, false,
13035 TYPE_PRECISION (TREE_TYPE (arg1
)));
13037 wide_int common
= mask
& wi::to_wide (arg1
);
13038 if (common
== mask
)
13040 tem_type
= signed_type_for (TREE_TYPE (tem
));
13041 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13043 else if (common
== 0)
13045 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
13046 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13054 fold_convert_loc (loc
, type
,
13055 fold_build2_loc (loc
, BIT_AND_EXPR
,
13056 TREE_TYPE (tem
), tem
,
13057 fold_convert_loc (loc
,
13062 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13063 already handled above. */
13064 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13065 && integer_onep (TREE_OPERAND (arg0
, 1))
13066 && integer_zerop (op2
)
13067 && integer_pow2p (arg1
))
13069 tree tem
= TREE_OPERAND (arg0
, 0);
13071 if (TREE_CODE (tem
) == RSHIFT_EXPR
13072 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
13073 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
13074 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
13075 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
13076 fold_convert_loc (loc
, type
,
13077 TREE_OPERAND (tem
, 0)),
13081 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13082 is probably obsolete because the first operand should be a
13083 truth value (that's why we have the two cases above), but let's
13084 leave it in until we can confirm this for all front-ends. */
13085 if (integer_zerop (op2
)
13086 && TREE_CODE (arg0
) == NE_EXPR
13087 && integer_zerop (TREE_OPERAND (arg0
, 1))
13088 && integer_pow2p (arg1
)
13089 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13090 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13091 arg1
, OEP_ONLY_CONST
)
13092 /* operand_equal_p compares just value, not precision, so e.g.
13093 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13094 second operand 32-bit -128, which is not a power of two (or vice
13096 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
13097 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
13099 /* Disable the transformations below for vectors, since
13100 fold_binary_op_with_conditional_arg may undo them immediately,
13101 yielding an infinite loop. */
13102 if (code
== VEC_COND_EXPR
)
13105 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13106 if (integer_zerop (op2
)
13107 && truth_value_p (TREE_CODE (arg0
))
13108 && truth_value_p (TREE_CODE (arg1
))
13109 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13110 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13111 : TRUTH_ANDIF_EXPR
,
13112 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13114 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13115 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13116 && truth_value_p (TREE_CODE (arg0
))
13117 && truth_value_p (TREE_CODE (arg1
))
13118 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13120 location_t loc0
= expr_location_or (arg0
, loc
);
13121 /* Only perform transformation if ARG0 is easily inverted. */
13122 tem
= fold_invert_truthvalue (loc0
, arg0
);
13124 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13127 type
, fold_convert_loc (loc
, type
, tem
),
13131 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13132 if (integer_zerop (arg1
)
13133 && truth_value_p (TREE_CODE (arg0
))
13134 && truth_value_p (TREE_CODE (op2
))
13135 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13137 location_t loc0
= expr_location_or (arg0
, loc
);
13138 /* Only perform transformation if ARG0 is easily inverted. */
13139 tem
= fold_invert_truthvalue (loc0
, arg0
);
13141 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13142 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13143 type
, fold_convert_loc (loc
, type
, tem
),
13147 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13148 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13149 && truth_value_p (TREE_CODE (arg0
))
13150 && truth_value_p (TREE_CODE (op2
))
13151 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13152 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13153 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13154 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13159 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13160 of fold_ternary on them. */
13161 gcc_unreachable ();
13163 case BIT_FIELD_REF
:
13164 if (TREE_CODE (arg0
) == VECTOR_CST
13165 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13166 || (VECTOR_TYPE_P (type
)
13167 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13168 && tree_fits_uhwi_p (op1
)
13169 && tree_fits_uhwi_p (op2
))
13171 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13172 unsigned HOST_WIDE_INT width
13173 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13174 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13175 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13176 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13179 && (idx
% width
) == 0
13180 && (n
% width
) == 0
13181 && known_le ((idx
+ n
) / width
,
13182 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13187 if (TREE_CODE (arg0
) == VECTOR_CST
)
13191 tem
= VECTOR_CST_ELT (arg0
, idx
);
13192 if (VECTOR_TYPE_P (type
))
13193 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13197 tree_vector_builder
vals (type
, n
, 1);
13198 for (unsigned i
= 0; i
< n
; ++i
)
13199 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13200 return vals
.build ();
13205 /* On constants we can use native encode/interpret to constant
13206 fold (nearly) all BIT_FIELD_REFs. */
13207 if (CONSTANT_CLASS_P (arg0
)
13208 && can_native_interpret_type_p (type
)
13209 && BITS_PER_UNIT
== 8
13210 && tree_fits_uhwi_p (op1
)
13211 && tree_fits_uhwi_p (op2
))
13213 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13214 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13215 /* Limit us to a reasonable amount of work. To relax the
13216 other limitations we need bit-shifting of the buffer
13217 and rounding up the size. */
13218 if (bitpos
% BITS_PER_UNIT
== 0
13219 && bitsize
% BITS_PER_UNIT
== 0
13220 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13222 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13223 unsigned HOST_WIDE_INT len
13224 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13225 bitpos
/ BITS_PER_UNIT
);
13227 && len
* BITS_PER_UNIT
>= bitsize
)
13229 tree v
= native_interpret_expr (type
, b
,
13230 bitsize
/ BITS_PER_UNIT
);
13239 case VEC_PERM_EXPR
:
13240 /* Perform constant folding of BIT_INSERT_EXPR. */
13241 if (TREE_CODE (arg2
) == VECTOR_CST
13242 && TREE_CODE (op0
) == VECTOR_CST
13243 && TREE_CODE (op1
) == VECTOR_CST
)
13245 /* Build a vector of integers from the tree mask. */
13246 vec_perm_builder builder
;
13247 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13250 /* Create a vec_perm_indices for the integer vector. */
13251 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13252 bool single_arg
= (op0
== op1
);
13253 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13254 return fold_vec_perm (type
, op0
, op1
, sel
);
13258 case BIT_INSERT_EXPR
:
13259 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13260 if (TREE_CODE (arg0
) == INTEGER_CST
13261 && TREE_CODE (arg1
) == INTEGER_CST
)
13263 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13264 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13265 wide_int tem
= (wi::to_wide (arg0
)
13266 & wi::shifted_mask (bitpos
, bitsize
, true,
13267 TYPE_PRECISION (type
)));
13269 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13271 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13273 else if (TREE_CODE (arg0
) == VECTOR_CST
13274 && CONSTANT_CLASS_P (arg1
)
13275 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13278 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13279 unsigned HOST_WIDE_INT elsize
13280 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13281 if (bitpos
% elsize
== 0)
13283 unsigned k
= bitpos
/ elsize
;
13284 unsigned HOST_WIDE_INT nelts
;
13285 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13287 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13289 tree_vector_builder
elts (type
, nelts
, 1);
13290 elts
.quick_grow (nelts
);
13291 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13292 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13293 return elts
.build ();
13301 } /* switch (code) */
13304 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13305 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13306 constructor element index of the value returned. If the element is
13307 not found NULL_TREE is returned and *CTOR_IDX is updated to
13308 the index of the element after the ACCESS_INDEX position (which
13309 may be outside of the CTOR array). */
13312 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13313 unsigned *ctor_idx
)
13315 tree index_type
= NULL_TREE
;
13316 signop index_sgn
= UNSIGNED
;
13317 offset_int low_bound
= 0;
13319 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13321 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13322 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13324 /* Static constructors for variably sized objects makes no sense. */
13325 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13326 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13327 /* ??? When it is obvious that the range is signed, treat it so. */
13328 if (TYPE_UNSIGNED (index_type
)
13329 && TYPE_MAX_VALUE (domain_type
)
13330 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13331 TYPE_MIN_VALUE (domain_type
)))
13333 index_sgn
= SIGNED
;
13335 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13340 index_sgn
= TYPE_SIGN (index_type
);
13341 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13347 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13350 offset_int index
= low_bound
;
13352 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13354 offset_int max_index
= index
;
13357 bool first_p
= true;
13359 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13361 /* Array constructor might explicitly set index, or specify a range,
13362 or leave index NULL meaning that it is next index after previous
13366 if (TREE_CODE (cfield
) == INTEGER_CST
)
13368 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13371 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13372 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13375 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13377 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13382 index
= max_index
+ 1;
13384 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13385 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13391 /* Do we have match? */
13392 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13394 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13401 else if (in_gimple_form
)
13402 /* We're past the element we search for. Note during parsing
13403 the elements might not be sorted.
13404 ??? We should use a binary search and a flag on the
13405 CONSTRUCTOR as to whether elements are sorted in declaration
13414 /* Perform constant folding and related simplification of EXPR.
13415 The related simplifications include x*1 => x, x*0 => 0, etc.,
13416 and application of the associative law.
13417 NOP_EXPR conversions may be removed freely (as long as we
13418 are careful not to change the type of the overall expression).
13419 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13420 but we can constant-fold them if they have constant operands. */
13422 #ifdef ENABLE_FOLD_CHECKING
13423 # define fold(x) fold_1 (x)
13424 static tree
fold_1 (tree
);
13430 const tree t
= expr
;
13431 enum tree_code code
= TREE_CODE (t
);
13432 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13434 location_t loc
= EXPR_LOCATION (expr
);
13436 /* Return right away if a constant. */
13437 if (kind
== tcc_constant
)
13440 /* CALL_EXPR-like objects with variable numbers of operands are
13441 treated specially. */
13442 if (kind
== tcc_vl_exp
)
13444 if (code
== CALL_EXPR
)
13446 tem
= fold_call_expr (loc
, expr
, false);
13447 return tem
? tem
: expr
;
13452 if (IS_EXPR_CODE_CLASS (kind
))
13454 tree type
= TREE_TYPE (t
);
13455 tree op0
, op1
, op2
;
13457 switch (TREE_CODE_LENGTH (code
))
13460 op0
= TREE_OPERAND (t
, 0);
13461 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13462 return tem
? tem
: expr
;
13464 op0
= TREE_OPERAND (t
, 0);
13465 op1
= TREE_OPERAND (t
, 1);
13466 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13467 return tem
? tem
: expr
;
13469 op0
= TREE_OPERAND (t
, 0);
13470 op1
= TREE_OPERAND (t
, 1);
13471 op2
= TREE_OPERAND (t
, 2);
13472 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13473 return tem
? tem
: expr
;
13483 tree op0
= TREE_OPERAND (t
, 0);
13484 tree op1
= TREE_OPERAND (t
, 1);
13486 if (TREE_CODE (op1
) == INTEGER_CST
13487 && TREE_CODE (op0
) == CONSTRUCTOR
13488 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13490 tree val
= get_array_ctor_element_at_index (op0
,
13491 wi::to_offset (op1
));
13499 /* Return a VECTOR_CST if possible. */
13502 tree type
= TREE_TYPE (t
);
13503 if (TREE_CODE (type
) != VECTOR_TYPE
)
13508 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13509 if (! CONSTANT_CLASS_P (val
))
13512 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13516 return fold (DECL_INITIAL (t
));
13520 } /* switch (code) */
13523 #ifdef ENABLE_FOLD_CHECKING
13526 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13527 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13528 static void fold_check_failed (const_tree
, const_tree
);
13529 void print_fold_checksum (const_tree
);
13531 /* When --enable-checking=fold, compute a digest of expr before
13532 and after actual fold call to see if fold did not accidentally
13533 change original expr. */
13539 struct md5_ctx ctx
;
13540 unsigned char checksum_before
[16], checksum_after
[16];
13541 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13543 md5_init_ctx (&ctx
);
13544 fold_checksum_tree (expr
, &ctx
, &ht
);
13545 md5_finish_ctx (&ctx
, checksum_before
);
13548 ret
= fold_1 (expr
);
13550 md5_init_ctx (&ctx
);
13551 fold_checksum_tree (expr
, &ctx
, &ht
);
13552 md5_finish_ctx (&ctx
, checksum_after
);
13554 if (memcmp (checksum_before
, checksum_after
, 16))
13555 fold_check_failed (expr
, ret
);
13561 print_fold_checksum (const_tree expr
)
13563 struct md5_ctx ctx
;
13564 unsigned char checksum
[16], cnt
;
13565 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13567 md5_init_ctx (&ctx
);
13568 fold_checksum_tree (expr
, &ctx
, &ht
);
13569 md5_finish_ctx (&ctx
, checksum
);
13570 for (cnt
= 0; cnt
< 16; ++cnt
)
13571 fprintf (stderr
, "%02x", checksum
[cnt
]);
13572 putc ('\n', stderr
);
13576 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13578 internal_error ("fold check: original tree changed by fold");
13582 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13583 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13585 const tree_node
**slot
;
13586 enum tree_code code
;
13587 union tree_node
*buf
;
13593 slot
= ht
->find_slot (expr
, INSERT
);
13597 code
= TREE_CODE (expr
);
13598 if (TREE_CODE_CLASS (code
) == tcc_declaration
13599 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13601 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13602 size_t sz
= tree_size (expr
);
13603 buf
= XALLOCAVAR (union tree_node
, sz
);
13604 memcpy ((char *) buf
, expr
, sz
);
13605 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13606 buf
->decl_with_vis
.symtab_node
= NULL
;
13607 buf
->base
.nowarning_flag
= 0;
13610 else if (TREE_CODE_CLASS (code
) == tcc_type
13611 && (TYPE_POINTER_TO (expr
)
13612 || TYPE_REFERENCE_TO (expr
)
13613 || TYPE_CACHED_VALUES_P (expr
)
13614 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13615 || TYPE_NEXT_VARIANT (expr
)
13616 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13618 /* Allow these fields to be modified. */
13620 size_t sz
= tree_size (expr
);
13621 buf
= XALLOCAVAR (union tree_node
, sz
);
13622 memcpy ((char *) buf
, expr
, sz
);
13623 expr
= tmp
= (tree
) buf
;
13624 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13625 TYPE_POINTER_TO (tmp
) = NULL
;
13626 TYPE_REFERENCE_TO (tmp
) = NULL
;
13627 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13628 TYPE_ALIAS_SET (tmp
) = -1;
13629 if (TYPE_CACHED_VALUES_P (tmp
))
13631 TYPE_CACHED_VALUES_P (tmp
) = 0;
13632 TYPE_CACHED_VALUES (tmp
) = NULL
;
13635 else if (warning_suppressed_p (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13637 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13638 that and change builtins.cc etc. instead - see PR89543. */
13639 size_t sz
= tree_size (expr
);
13640 buf
= XALLOCAVAR (union tree_node
, sz
);
13641 memcpy ((char *) buf
, expr
, sz
);
13642 buf
->base
.nowarning_flag
= 0;
13645 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13646 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13647 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13648 if (TREE_CODE_CLASS (code
) != tcc_type
13649 && TREE_CODE_CLASS (code
) != tcc_declaration
13650 && code
!= TREE_LIST
13651 && code
!= SSA_NAME
13652 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13653 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13654 switch (TREE_CODE_CLASS (code
))
13660 md5_process_bytes (TREE_STRING_POINTER (expr
),
13661 TREE_STRING_LENGTH (expr
), ctx
);
13664 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13665 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13668 len
= vector_cst_encoded_nelts (expr
);
13669 for (i
= 0; i
< len
; ++i
)
13670 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13676 case tcc_exceptional
:
13680 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13681 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13682 expr
= TREE_CHAIN (expr
);
13683 goto recursive_label
;
13686 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13687 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13693 case tcc_expression
:
13694 case tcc_reference
:
13695 case tcc_comparison
:
13698 case tcc_statement
:
13700 len
= TREE_OPERAND_LENGTH (expr
);
13701 for (i
= 0; i
< len
; ++i
)
13702 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13704 case tcc_declaration
:
13705 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13706 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13707 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13709 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13710 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13711 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13712 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13713 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13716 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13718 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13720 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13721 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13723 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13727 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13728 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13729 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
13730 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
13731 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
13732 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
13733 if (INTEGRAL_TYPE_P (expr
)
13734 || SCALAR_FLOAT_TYPE_P (expr
))
13736 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
13737 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
13739 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
13740 if (TREE_CODE (expr
) == RECORD_TYPE
13741 || TREE_CODE (expr
) == UNION_TYPE
13742 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
13743 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
13744 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
13751 /* Helper function for outputting the checksum of a tree T. When
13752 debugging with gdb, you can "define mynext" to be "next" followed
13753 by "call debug_fold_checksum (op0)", then just trace down till the
13756 DEBUG_FUNCTION
void
13757 debug_fold_checksum (const_tree t
)
13760 unsigned char checksum
[16];
13761 struct md5_ctx ctx
;
13762 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13764 md5_init_ctx (&ctx
);
13765 fold_checksum_tree (t
, &ctx
, &ht
);
13766 md5_finish_ctx (&ctx
, checksum
);
13769 for (i
= 0; i
< 16; i
++)
13770 fprintf (stderr
, "%d ", checksum
[i
]);
13772 fprintf (stderr
, "\n");
13777 /* Fold a unary tree expression with code CODE of type TYPE with an
13778 operand OP0. LOC is the location of the resulting expression.
13779 Return a folded expression if successful. Otherwise, return a tree
13780 expression with code CODE of type TYPE with an operand OP0. */
13783 fold_build1_loc (location_t loc
,
13784 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
13787 #ifdef ENABLE_FOLD_CHECKING
13788 unsigned char checksum_before
[16], checksum_after
[16];
13789 struct md5_ctx ctx
;
13790 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13792 md5_init_ctx (&ctx
);
13793 fold_checksum_tree (op0
, &ctx
, &ht
);
13794 md5_finish_ctx (&ctx
, checksum_before
);
13798 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13800 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
13802 #ifdef ENABLE_FOLD_CHECKING
13803 md5_init_ctx (&ctx
);
13804 fold_checksum_tree (op0
, &ctx
, &ht
);
13805 md5_finish_ctx (&ctx
, checksum_after
);
13807 if (memcmp (checksum_before
, checksum_after
, 16))
13808 fold_check_failed (op0
, tem
);
13813 /* Fold a binary tree expression with code CODE of type TYPE with
13814 operands OP0 and OP1. LOC is the location of the resulting
13815 expression. Return a folded expression if successful. Otherwise,
13816 return a tree expression with code CODE of type TYPE with operands
13820 fold_build2_loc (location_t loc
,
13821 enum tree_code code
, tree type
, tree op0
, tree op1
13825 #ifdef ENABLE_FOLD_CHECKING
13826 unsigned char checksum_before_op0
[16],
13827 checksum_before_op1
[16],
13828 checksum_after_op0
[16],
13829 checksum_after_op1
[16];
13830 struct md5_ctx ctx
;
13831 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13833 md5_init_ctx (&ctx
);
13834 fold_checksum_tree (op0
, &ctx
, &ht
);
13835 md5_finish_ctx (&ctx
, checksum_before_op0
);
13838 md5_init_ctx (&ctx
);
13839 fold_checksum_tree (op1
, &ctx
, &ht
);
13840 md5_finish_ctx (&ctx
, checksum_before_op1
);
13844 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13846 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
13848 #ifdef ENABLE_FOLD_CHECKING
13849 md5_init_ctx (&ctx
);
13850 fold_checksum_tree (op0
, &ctx
, &ht
);
13851 md5_finish_ctx (&ctx
, checksum_after_op0
);
13854 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13855 fold_check_failed (op0
, tem
);
13857 md5_init_ctx (&ctx
);
13858 fold_checksum_tree (op1
, &ctx
, &ht
);
13859 md5_finish_ctx (&ctx
, checksum_after_op1
);
13861 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13862 fold_check_failed (op1
, tem
);
13867 /* Fold a ternary tree expression with code CODE of type TYPE with
13868 operands OP0, OP1, and OP2. Return a folded expression if
13869 successful. Otherwise, return a tree expression with code CODE of
13870 type TYPE with operands OP0, OP1, and OP2. */
13873 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
13874 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
13877 #ifdef ENABLE_FOLD_CHECKING
13878 unsigned char checksum_before_op0
[16],
13879 checksum_before_op1
[16],
13880 checksum_before_op2
[16],
13881 checksum_after_op0
[16],
13882 checksum_after_op1
[16],
13883 checksum_after_op2
[16];
13884 struct md5_ctx ctx
;
13885 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13887 md5_init_ctx (&ctx
);
13888 fold_checksum_tree (op0
, &ctx
, &ht
);
13889 md5_finish_ctx (&ctx
, checksum_before_op0
);
13892 md5_init_ctx (&ctx
);
13893 fold_checksum_tree (op1
, &ctx
, &ht
);
13894 md5_finish_ctx (&ctx
, checksum_before_op1
);
13897 md5_init_ctx (&ctx
);
13898 fold_checksum_tree (op2
, &ctx
, &ht
);
13899 md5_finish_ctx (&ctx
, checksum_before_op2
);
13903 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
13904 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13906 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
13908 #ifdef ENABLE_FOLD_CHECKING
13909 md5_init_ctx (&ctx
);
13910 fold_checksum_tree (op0
, &ctx
, &ht
);
13911 md5_finish_ctx (&ctx
, checksum_after_op0
);
13914 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13915 fold_check_failed (op0
, tem
);
13917 md5_init_ctx (&ctx
);
13918 fold_checksum_tree (op1
, &ctx
, &ht
);
13919 md5_finish_ctx (&ctx
, checksum_after_op1
);
13922 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13923 fold_check_failed (op1
, tem
);
13925 md5_init_ctx (&ctx
);
13926 fold_checksum_tree (op2
, &ctx
, &ht
);
13927 md5_finish_ctx (&ctx
, checksum_after_op2
);
13929 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
13930 fold_check_failed (op2
, tem
);
13935 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13936 arguments in ARGARRAY, and a null static chain.
13937 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13938 of type TYPE from the given operands as constructed by build_call_array. */
13941 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
13942 int nargs
, tree
*argarray
)
13945 #ifdef ENABLE_FOLD_CHECKING
13946 unsigned char checksum_before_fn
[16],
13947 checksum_before_arglist
[16],
13948 checksum_after_fn
[16],
13949 checksum_after_arglist
[16];
13950 struct md5_ctx ctx
;
13951 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13954 md5_init_ctx (&ctx
);
13955 fold_checksum_tree (fn
, &ctx
, &ht
);
13956 md5_finish_ctx (&ctx
, checksum_before_fn
);
13959 md5_init_ctx (&ctx
);
13960 for (i
= 0; i
< nargs
; i
++)
13961 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13962 md5_finish_ctx (&ctx
, checksum_before_arglist
);
13966 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
13968 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13970 #ifdef ENABLE_FOLD_CHECKING
13971 md5_init_ctx (&ctx
);
13972 fold_checksum_tree (fn
, &ctx
, &ht
);
13973 md5_finish_ctx (&ctx
, checksum_after_fn
);
13976 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
13977 fold_check_failed (fn
, tem
);
13979 md5_init_ctx (&ctx
);
13980 for (i
= 0; i
< nargs
; i
++)
13981 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13982 md5_finish_ctx (&ctx
, checksum_after_arglist
);
13984 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
13985 fold_check_failed (NULL_TREE
, tem
);
13990 /* Perform constant folding and related simplification of initializer
13991 expression EXPR. These behave identically to "fold_buildN" but ignore
13992 potential run-time traps and exceptions that fold must preserve. */
13994 #define START_FOLD_INIT \
13995 int saved_signaling_nans = flag_signaling_nans;\
13996 int saved_trapping_math = flag_trapping_math;\
13997 int saved_rounding_math = flag_rounding_math;\
13998 int saved_trapv = flag_trapv;\
13999 int saved_folding_initializer = folding_initializer;\
14000 flag_signaling_nans = 0;\
14001 flag_trapping_math = 0;\
14002 flag_rounding_math = 0;\
14004 folding_initializer = 1;
14006 #define END_FOLD_INIT \
14007 flag_signaling_nans = saved_signaling_nans;\
14008 flag_trapping_math = saved_trapping_math;\
14009 flag_rounding_math = saved_rounding_math;\
14010 flag_trapv = saved_trapv;\
14011 folding_initializer = saved_folding_initializer;
14014 fold_init (tree expr
)
14019 result
= fold (expr
);
14026 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
14027 tree type
, tree op
)
14032 result
= fold_build1_loc (loc
, code
, type
, op
);
14039 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
14040 tree type
, tree op0
, tree op1
)
14045 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
14052 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
14053 int nargs
, tree
*argarray
)
14058 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14065 fold_binary_initializer_loc (location_t loc
, tree_code code
, tree type
,
14066 tree lhs
, tree rhs
)
14071 result
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
14077 #undef START_FOLD_INIT
14078 #undef END_FOLD_INIT
14080 /* Determine if first argument is a multiple of second argument. Return 0 if
14081 it is not, or we cannot easily determined it to be.
14083 An example of the sort of thing we care about (at this point; this routine
14084 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14085 fold cases do now) is discovering that
14087 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14093 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14095 This code also handles discovering that
14097 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14099 is a multiple of 8 so we don't have to worry about dealing with a
14100 possible remainder.
14102 Note that we *look* inside a SAVE_EXPR only to determine how it was
14103 calculated; it is not safe for fold to do much of anything else with the
14104 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14105 at run time. For example, the latter example above *cannot* be implemented
14106 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14107 evaluation time of the original SAVE_EXPR is not necessarily the same at
14108 the time the new expression is evaluated. The only optimization of this
14109 sort that would be valid is changing
14111 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14115 SAVE_EXPR (I) * SAVE_EXPR (J)
14117 (where the same SAVE_EXPR (J) is used in the original and the
14118 transformed version).
14120 NOWRAP specifies whether all outer operations in TYPE should
14121 be considered not wrapping. Any type conversion within TOP acts
14122 as a barrier and we will fall back to NOWRAP being false.
14123 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14124 as not wrapping even though they are generally using unsigned arithmetic. */
14127 multiple_of_p (tree type
, const_tree top
, const_tree bottom
, bool nowrap
)
14132 if (operand_equal_p (top
, bottom
, 0))
14135 if (TREE_CODE (type
) != INTEGER_TYPE
)
14138 switch (TREE_CODE (top
))
14141 /* Bitwise and provides a power of two multiple. If the mask is
14142 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14143 if (!integer_pow2p (bottom
))
14145 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14146 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14149 /* If the multiplication can wrap we cannot recurse further unless
14150 the bottom is a power of two which is where wrapping does not
14153 && !TYPE_OVERFLOW_UNDEFINED (type
)
14154 && !integer_pow2p (bottom
))
14156 if (TREE_CODE (bottom
) == INTEGER_CST
)
14158 op1
= TREE_OPERAND (top
, 0);
14159 op2
= TREE_OPERAND (top
, 1);
14160 if (TREE_CODE (op1
) == INTEGER_CST
)
14161 std::swap (op1
, op2
);
14162 if (TREE_CODE (op2
) == INTEGER_CST
)
14164 if (multiple_of_p (type
, op2
, bottom
, nowrap
))
14166 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14167 if (multiple_of_p (type
, bottom
, op2
, nowrap
))
14169 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14170 wi::to_widest (op2
));
14171 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14173 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14174 return multiple_of_p (type
, op1
, op2
, nowrap
);
14177 return multiple_of_p (type
, op1
, bottom
, nowrap
);
14180 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14181 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14184 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14185 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14187 op1
= TREE_OPERAND (top
, 1);
14188 if (wi::to_widest (op1
) < TYPE_PRECISION (type
))
14191 = wi::one (TYPE_PRECISION (type
)) << wi::to_wide (op1
);
14192 return multiple_of_p (type
,
14193 wide_int_to_tree (type
, mul_op
), bottom
,
14201 /* If the addition or subtraction can wrap we cannot recurse further
14202 unless bottom is a power of two which is where wrapping does not
14205 && !TYPE_OVERFLOW_UNDEFINED (type
)
14206 && !integer_pow2p (bottom
))
14209 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14210 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14211 but 0xfffffffd is not. */
14212 op1
= TREE_OPERAND (top
, 1);
14213 if (TREE_CODE (top
) == PLUS_EXPR
14215 && TYPE_UNSIGNED (type
)
14216 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14217 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14219 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14220 precisely, so be conservative here checking if both op0 and op1
14221 are multiple of bottom. Note we check the second operand first
14222 since it's usually simpler. */
14223 return (multiple_of_p (type
, op1
, bottom
, nowrap
)
14224 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14227 /* Can't handle conversions from non-integral or wider integral type. */
14228 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14229 || (TYPE_PRECISION (type
)
14230 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14232 /* NOWRAP only extends to operations in the outermost type so
14233 make sure to strip it off here. */
14234 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top
, 0)),
14235 TREE_OPERAND (top
, 0), bottom
, false);
14238 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
);
14241 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14242 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
, nowrap
));
14245 if (TREE_CODE (bottom
) != INTEGER_CST
|| integer_zerop (bottom
))
14247 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14251 if (TREE_CODE (bottom
) == INTEGER_CST
14252 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14253 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14255 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14257 /* Check for special cases to see if top is defined as multiple
14260 top = (X & ~(bottom - 1) ; bottom is power of 2
14266 if (code
== BIT_AND_EXPR
14267 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14268 && TREE_CODE (op2
) == INTEGER_CST
14269 && integer_pow2p (bottom
)
14270 && wi::multiple_of_p (wi::to_widest (op2
),
14271 wi::to_widest (bottom
), UNSIGNED
))
14274 op1
= gimple_assign_rhs1 (stmt
);
14275 if (code
== MINUS_EXPR
14276 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14277 && TREE_CODE (op2
) == SSA_NAME
14278 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14279 && gimple_code (stmt
) == GIMPLE_ASSIGN
14280 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14281 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14282 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14289 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14290 return multiple_p (wi::to_poly_widest (top
),
14291 wi::to_poly_widest (bottom
));
14297 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14298 This function returns true for integer expressions, and returns
14299 false if uncertain. */
14302 tree_expr_finite_p (const_tree x
)
14304 machine_mode mode
= element_mode (x
);
14305 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14307 switch (TREE_CODE (x
))
14310 return real_isfinite (TREE_REAL_CST_PTR (x
));
14312 return tree_expr_finite_p (TREE_REALPART (x
))
14313 && tree_expr_finite_p (TREE_IMAGPART (x
));
14318 case NON_LVALUE_EXPR
:
14321 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14324 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14325 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14327 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14328 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14330 switch (get_call_combined_fn (x
))
14333 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14336 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14337 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14347 /* Return true if expression X evaluates to an infinity.
14348 This function returns false for integer expressions. */
14351 tree_expr_infinite_p (const_tree x
)
14353 if (!HONOR_INFINITIES (x
))
14355 switch (TREE_CODE (x
))
14358 return real_isinf (TREE_REAL_CST_PTR (x
));
14361 case NON_LVALUE_EXPR
:
14363 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14365 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14366 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14372 /* Return true if expression X could evaluate to an infinity.
14373 This function returns false for integer expressions, and returns
14374 true if uncertain. */
14377 tree_expr_maybe_infinite_p (const_tree x
)
14379 if (!HONOR_INFINITIES (x
))
14381 switch (TREE_CODE (x
))
14384 return real_isinf (TREE_REAL_CST_PTR (x
));
14389 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14391 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14392 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14398 /* Return true if expression X evaluates to a signaling NaN.
14399 This function returns false for integer expressions. */
14402 tree_expr_signaling_nan_p (const_tree x
)
14404 if (!HONOR_SNANS (x
))
14406 switch (TREE_CODE (x
))
14409 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14410 case NON_LVALUE_EXPR
:
14412 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14414 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14415 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14421 /* Return true if expression X could evaluate to a signaling NaN.
14422 This function returns false for integer expressions, and returns
14423 true if uncertain. */
14426 tree_expr_maybe_signaling_nan_p (const_tree x
)
14428 if (!HONOR_SNANS (x
))
14430 switch (TREE_CODE (x
))
14433 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14439 case NON_LVALUE_EXPR
:
14441 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14444 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14445 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14447 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14448 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14450 switch (get_call_combined_fn (x
))
14453 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14456 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14457 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14466 /* Return true if expression X evaluates to a NaN.
14467 This function returns false for integer expressions. */
14470 tree_expr_nan_p (const_tree x
)
14472 if (!HONOR_NANS (x
))
14474 switch (TREE_CODE (x
))
14477 return real_isnan (TREE_REAL_CST_PTR (x
));
14478 case NON_LVALUE_EXPR
:
14480 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14482 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14483 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14489 /* Return true if expression X could evaluate to a NaN.
14490 This function returns false for integer expressions, and returns
14491 true if uncertain. */
14494 tree_expr_maybe_nan_p (const_tree x
)
14496 if (!HONOR_NANS (x
))
14498 switch (TREE_CODE (x
))
14501 return real_isnan (TREE_REAL_CST_PTR (x
));
14507 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14508 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14512 case NON_LVALUE_EXPR
:
14514 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14517 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14518 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14520 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14521 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14523 switch (get_call_combined_fn (x
))
14526 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14529 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14530 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14539 /* Return true if expression X could evaluate to -0.0.
14540 This function returns true if uncertain. */
14543 tree_expr_maybe_real_minus_zero_p (const_tree x
)
14545 if (!HONOR_SIGNED_ZEROS (x
))
14547 switch (TREE_CODE (x
))
14550 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x
));
14555 case NON_LVALUE_EXPR
:
14557 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 0));
14559 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 1))
14560 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 2));
14562 switch (get_call_combined_fn (x
))
14572 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14573 * but currently those predicates require tree and not const_tree. */
14577 #define tree_expr_nonnegative_warnv_p(X, Y) \
14578 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14580 #define RECURSE(X) \
14581 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14583 /* Return true if CODE or TYPE is known to be non-negative. */
14586 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14588 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14589 && truth_value_p (code
))
14590 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14591 have a signed:1 type (where the value is -1 and 0). */
14596 /* Return true if (CODE OP0) is known to be non-negative. If the return
14597 value is based on the assumption that signed overflow is undefined,
14598 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14599 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14602 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14603 bool *strict_overflow_p
, int depth
)
14605 if (TYPE_UNSIGNED (type
))
14611 /* We can't return 1 if flag_wrapv is set because
14612 ABS_EXPR<INT_MIN> = INT_MIN. */
14613 if (!ANY_INTEGRAL_TYPE_P (type
))
14615 if (TYPE_OVERFLOW_UNDEFINED (type
))
14617 *strict_overflow_p
= true;
14622 case NON_LVALUE_EXPR
:
14624 case FIX_TRUNC_EXPR
:
14625 return RECURSE (op0
);
14629 tree inner_type
= TREE_TYPE (op0
);
14630 tree outer_type
= type
;
14632 if (TREE_CODE (outer_type
) == REAL_TYPE
)
14634 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14635 return RECURSE (op0
);
14636 if (INTEGRAL_TYPE_P (inner_type
))
14638 if (TYPE_UNSIGNED (inner_type
))
14640 return RECURSE (op0
);
14643 else if (INTEGRAL_TYPE_P (outer_type
))
14645 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14646 return RECURSE (op0
);
14647 if (INTEGRAL_TYPE_P (inner_type
))
14648 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14649 && TYPE_UNSIGNED (inner_type
);
14655 return tree_simple_nonnegative_warnv_p (code
, type
);
14658 /* We don't know sign of `t', so be conservative and return false. */
14662 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14663 value is based on the assumption that signed overflow is undefined,
14664 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14665 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14668 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14669 tree op1
, bool *strict_overflow_p
,
14672 if (TYPE_UNSIGNED (type
))
14677 case POINTER_PLUS_EXPR
:
14679 if (FLOAT_TYPE_P (type
))
14680 return RECURSE (op0
) && RECURSE (op1
);
14682 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14683 both unsigned and at least 2 bits shorter than the result. */
14684 if (TREE_CODE (type
) == INTEGER_TYPE
14685 && TREE_CODE (op0
) == NOP_EXPR
14686 && TREE_CODE (op1
) == NOP_EXPR
)
14688 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14689 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14690 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14691 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14693 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14694 TYPE_PRECISION (inner2
)) + 1;
14695 return prec
< TYPE_PRECISION (type
);
14701 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14703 /* x * x is always non-negative for floating point x
14704 or without overflow. */
14705 if (operand_equal_p (op0
, op1
, 0)
14706 || (RECURSE (op0
) && RECURSE (op1
)))
14708 if (ANY_INTEGRAL_TYPE_P (type
)
14709 && TYPE_OVERFLOW_UNDEFINED (type
))
14710 *strict_overflow_p
= true;
14715 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14716 both unsigned and their total bits is shorter than the result. */
14717 if (TREE_CODE (type
) == INTEGER_TYPE
14718 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14719 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
14721 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
14722 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
14724 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
14725 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
14728 bool unsigned0
= TYPE_UNSIGNED (inner0
);
14729 bool unsigned1
= TYPE_UNSIGNED (inner1
);
14731 if (TREE_CODE (op0
) == INTEGER_CST
)
14732 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
14734 if (TREE_CODE (op1
) == INTEGER_CST
)
14735 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
14737 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
14738 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
14740 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
14741 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
14742 : TYPE_PRECISION (inner0
);
14744 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
14745 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
14746 : TYPE_PRECISION (inner1
);
14748 return precision0
+ precision1
< TYPE_PRECISION (type
);
14754 return RECURSE (op0
) || RECURSE (op1
);
14757 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14759 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
14760 return RECURSE (op0
) && RECURSE (op1
);
14761 return RECURSE (op0
) || RECURSE (op1
);
14767 case TRUNC_DIV_EXPR
:
14768 case CEIL_DIV_EXPR
:
14769 case FLOOR_DIV_EXPR
:
14770 case ROUND_DIV_EXPR
:
14771 return RECURSE (op0
) && RECURSE (op1
);
14773 case TRUNC_MOD_EXPR
:
14774 return RECURSE (op0
);
14776 case FLOOR_MOD_EXPR
:
14777 return RECURSE (op1
);
14779 case CEIL_MOD_EXPR
:
14780 case ROUND_MOD_EXPR
:
14782 return tree_simple_nonnegative_warnv_p (code
, type
);
14785 /* We don't know sign of `t', so be conservative and return false. */
14789 /* Return true if T is known to be non-negative. If the return
14790 value is based on the assumption that signed overflow is undefined,
14791 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14792 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14795 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14797 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14800 switch (TREE_CODE (t
))
14803 return tree_int_cst_sgn (t
) >= 0;
14806 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
14809 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
14812 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
14815 /* Limit the depth of recursion to avoid quadratic behavior.
14816 This is expected to catch almost all occurrences in practice.
14817 If this code misses important cases that unbounded recursion
14818 would not, passes that need this information could be revised
14819 to provide it through dataflow propagation. */
14820 return (!name_registered_for_update_p (t
)
14821 && depth
< param_max_ssa_name_query_depth
14822 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
14823 strict_overflow_p
, depth
));
14826 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
14830 /* Return true if T is known to be non-negative. If the return
14831 value is based on the assumption that signed overflow is undefined,
14832 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14833 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14836 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
14837 bool *strict_overflow_p
, int depth
)
14858 case CFN_BUILT_IN_BSWAP16
:
14859 case CFN_BUILT_IN_BSWAP32
:
14860 case CFN_BUILT_IN_BSWAP64
:
14861 case CFN_BUILT_IN_BSWAP128
:
14867 /* sqrt(-0.0) is -0.0. */
14868 if (!HONOR_SIGNED_ZEROS (type
))
14870 return RECURSE (arg0
);
14898 CASE_CFN_NEARBYINT
:
14899 CASE_CFN_NEARBYINT_FN
:
14904 CASE_CFN_ROUNDEVEN
:
14905 CASE_CFN_ROUNDEVEN_FN
:
14910 CASE_CFN_SIGNIFICAND
:
14915 /* True if the 1st argument is nonnegative. */
14916 return RECURSE (arg0
);
14920 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14921 things. In the presence of sNaNs, we're only guaranteed to be
14922 non-negative if both operands are non-negative. In the presence
14923 of qNaNs, we're non-negative if either operand is non-negative
14924 and can't be a qNaN, or if both operands are non-negative. */
14925 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
14926 tree_expr_maybe_signaling_nan_p (arg1
))
14927 return RECURSE (arg0
) && RECURSE (arg1
);
14928 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
14931 && !tree_expr_maybe_nan_p (arg1
));
14935 /* True if the 1st AND 2nd arguments are nonnegative. */
14936 return RECURSE (arg0
) && RECURSE (arg1
);
14939 CASE_CFN_COPYSIGN_FN
:
14940 /* True if the 2nd argument is nonnegative. */
14941 return RECURSE (arg1
);
14944 /* True if the 1st argument is nonnegative or the second
14945 argument is an even integer. */
14946 if (TREE_CODE (arg1
) == INTEGER_CST
14947 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
14949 return RECURSE (arg0
);
14952 /* True if the 1st argument is nonnegative or the second
14953 argument is an even integer valued real. */
14954 if (TREE_CODE (arg1
) == REAL_CST
)
14959 c
= TREE_REAL_CST (arg1
);
14960 n
= real_to_integer (&c
);
14963 REAL_VALUE_TYPE cint
;
14964 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
14965 if (real_identical (&c
, &cint
))
14969 return RECURSE (arg0
);
14974 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
14977 /* Return true if T is known to be non-negative. If the return
14978 value is based on the assumption that signed overflow is undefined,
14979 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14980 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14983 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14985 enum tree_code code
= TREE_CODE (t
);
14986 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14993 tree temp
= TARGET_EXPR_SLOT (t
);
14994 t
= TARGET_EXPR_INITIAL (t
);
14996 /* If the initializer is non-void, then it's a normal expression
14997 that will be assigned to the slot. */
14998 if (!VOID_TYPE_P (t
))
14999 return RECURSE (t
);
15001 /* Otherwise, the initializer sets the slot in some way. One common
15002 way is an assignment statement at the end of the initializer. */
15005 if (TREE_CODE (t
) == BIND_EXPR
)
15006 t
= expr_last (BIND_EXPR_BODY (t
));
15007 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
15008 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
15009 t
= expr_last (TREE_OPERAND (t
, 0));
15010 else if (TREE_CODE (t
) == STATEMENT_LIST
)
15015 if (TREE_CODE (t
) == MODIFY_EXPR
15016 && TREE_OPERAND (t
, 0) == temp
)
15017 return RECURSE (TREE_OPERAND (t
, 1));
15024 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
15025 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
15027 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
15028 get_call_combined_fn (t
),
15031 strict_overflow_p
, depth
);
15033 case COMPOUND_EXPR
:
15035 return RECURSE (TREE_OPERAND (t
, 1));
15038 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
15041 return RECURSE (TREE_OPERAND (t
, 0));
15044 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15049 #undef tree_expr_nonnegative_warnv_p
15051 /* Return true if T is known to be non-negative. If the return
15052 value is based on the assumption that signed overflow is undefined,
15053 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15054 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15057 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15059 enum tree_code code
;
15060 if (t
== error_mark_node
)
15063 code
= TREE_CODE (t
);
15064 switch (TREE_CODE_CLASS (code
))
15067 case tcc_comparison
:
15068 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15070 TREE_OPERAND (t
, 0),
15071 TREE_OPERAND (t
, 1),
15072 strict_overflow_p
, depth
);
15075 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15077 TREE_OPERAND (t
, 0),
15078 strict_overflow_p
, depth
);
15081 case tcc_declaration
:
15082 case tcc_reference
:
15083 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15091 case TRUTH_AND_EXPR
:
15092 case TRUTH_OR_EXPR
:
15093 case TRUTH_XOR_EXPR
:
15094 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15096 TREE_OPERAND (t
, 0),
15097 TREE_OPERAND (t
, 1),
15098 strict_overflow_p
, depth
);
15099 case TRUTH_NOT_EXPR
:
15100 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15102 TREE_OPERAND (t
, 0),
15103 strict_overflow_p
, depth
);
15110 case WITH_SIZE_EXPR
:
15112 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15115 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15119 /* Return true if `t' is known to be non-negative. Handle warnings
15120 about undefined signed overflow. */
15123 tree_expr_nonnegative_p (tree t
)
15125 bool ret
, strict_overflow_p
;
15127 strict_overflow_p
= false;
15128 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15129 if (strict_overflow_p
)
15130 fold_overflow_warning (("assuming signed overflow does not occur when "
15131 "determining that expression is always "
15133 WARN_STRICT_OVERFLOW_MISC
);
15138 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15139 For floating point we further ensure that T is not denormal.
15140 Similar logic is present in nonzero_address in rtlanal.h.
15142 If the return value is based on the assumption that signed overflow
15143 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15144 change *STRICT_OVERFLOW_P. */
15147 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15148 bool *strict_overflow_p
)
15153 return tree_expr_nonzero_warnv_p (op0
,
15154 strict_overflow_p
);
15158 tree inner_type
= TREE_TYPE (op0
);
15159 tree outer_type
= type
;
15161 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15162 && tree_expr_nonzero_warnv_p (op0
,
15163 strict_overflow_p
));
15167 case NON_LVALUE_EXPR
:
15168 return tree_expr_nonzero_warnv_p (op0
,
15169 strict_overflow_p
);
15178 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15179 For floating point we further ensure that T is not denormal.
15180 Similar logic is present in nonzero_address in rtlanal.h.
15182 If the return value is based on the assumption that signed overflow
15183 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15184 change *STRICT_OVERFLOW_P. */
15187 tree_binary_nonzero_warnv_p (enum tree_code code
,
15190 tree op1
, bool *strict_overflow_p
)
15192 bool sub_strict_overflow_p
;
15195 case POINTER_PLUS_EXPR
:
15197 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15199 /* With the presence of negative values it is hard
15200 to say something. */
15201 sub_strict_overflow_p
= false;
15202 if (!tree_expr_nonnegative_warnv_p (op0
,
15203 &sub_strict_overflow_p
)
15204 || !tree_expr_nonnegative_warnv_p (op1
,
15205 &sub_strict_overflow_p
))
15207 /* One of operands must be positive and the other non-negative. */
15208 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15209 overflows, on a twos-complement machine the sum of two
15210 nonnegative numbers can never be zero. */
15211 return (tree_expr_nonzero_warnv_p (op0
,
15213 || tree_expr_nonzero_warnv_p (op1
,
15214 strict_overflow_p
));
15219 if (TYPE_OVERFLOW_UNDEFINED (type
))
15221 if (tree_expr_nonzero_warnv_p (op0
,
15223 && tree_expr_nonzero_warnv_p (op1
,
15224 strict_overflow_p
))
15226 *strict_overflow_p
= true;
15233 sub_strict_overflow_p
= false;
15234 if (tree_expr_nonzero_warnv_p (op0
,
15235 &sub_strict_overflow_p
)
15236 && tree_expr_nonzero_warnv_p (op1
,
15237 &sub_strict_overflow_p
))
15239 if (sub_strict_overflow_p
)
15240 *strict_overflow_p
= true;
15245 sub_strict_overflow_p
= false;
15246 if (tree_expr_nonzero_warnv_p (op0
,
15247 &sub_strict_overflow_p
))
15249 if (sub_strict_overflow_p
)
15250 *strict_overflow_p
= true;
15252 /* When both operands are nonzero, then MAX must be too. */
15253 if (tree_expr_nonzero_warnv_p (op1
,
15254 strict_overflow_p
))
15257 /* MAX where operand 0 is positive is positive. */
15258 return tree_expr_nonnegative_warnv_p (op0
,
15259 strict_overflow_p
);
15261 /* MAX where operand 1 is positive is positive. */
15262 else if (tree_expr_nonzero_warnv_p (op1
,
15263 &sub_strict_overflow_p
)
15264 && tree_expr_nonnegative_warnv_p (op1
,
15265 &sub_strict_overflow_p
))
15267 if (sub_strict_overflow_p
)
15268 *strict_overflow_p
= true;
15274 return (tree_expr_nonzero_warnv_p (op1
,
15276 || tree_expr_nonzero_warnv_p (op0
,
15277 strict_overflow_p
));
15286 /* Return true when T is an address and is known to be nonzero.
15287 For floating point we further ensure that T is not denormal.
15288 Similar logic is present in nonzero_address in rtlanal.h.
15290 If the return value is based on the assumption that signed overflow
15291 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15292 change *STRICT_OVERFLOW_P. */
15295 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15297 bool sub_strict_overflow_p
;
15298 switch (TREE_CODE (t
))
15301 return !integer_zerop (t
);
15305 tree base
= TREE_OPERAND (t
, 0);
15307 if (!DECL_P (base
))
15308 base
= get_base_address (base
);
15310 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15311 base
= TARGET_EXPR_SLOT (base
);
15316 /* For objects in symbol table check if we know they are non-zero.
15317 Don't do anything for variables and functions before symtab is built;
15318 it is quite possible that they will be declared weak later. */
15319 int nonzero_addr
= maybe_nonzero_address (base
);
15320 if (nonzero_addr
>= 0)
15321 return nonzero_addr
;
15323 /* Constants are never weak. */
15324 if (CONSTANT_CLASS_P (base
))
15331 sub_strict_overflow_p
= false;
15332 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15333 &sub_strict_overflow_p
)
15334 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15335 &sub_strict_overflow_p
))
15337 if (sub_strict_overflow_p
)
15338 *strict_overflow_p
= true;
15344 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15346 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15354 #define integer_valued_real_p(X) \
15355 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15357 #define RECURSE(X) \
15358 ((integer_valued_real_p) (X, depth + 1))
15360 /* Return true if the floating point result of (CODE OP0) has an
15361 integer value. We also allow +Inf, -Inf and NaN to be considered
15362 integer values. Return false for signaling NaN.
15364 DEPTH is the current nesting depth of the query. */
15367 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15375 return RECURSE (op0
);
15379 tree type
= TREE_TYPE (op0
);
15380 if (TREE_CODE (type
) == INTEGER_TYPE
)
15382 if (TREE_CODE (type
) == REAL_TYPE
)
15383 return RECURSE (op0
);
15393 /* Return true if the floating point result of (CODE OP0 OP1) has an
15394 integer value. We also allow +Inf, -Inf and NaN to be considered
15395 integer values. Return false for signaling NaN.
15397 DEPTH is the current nesting depth of the query. */
15400 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15409 return RECURSE (op0
) && RECURSE (op1
);
15417 /* Return true if the floating point result of calling FNDECL with arguments
15418 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15419 considered integer values. Return false for signaling NaN. If FNDECL
15420 takes fewer than 2 arguments, the remaining ARGn are null.
15422 DEPTH is the current nesting depth of the query. */
15425 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15433 CASE_CFN_NEARBYINT
:
15434 CASE_CFN_NEARBYINT_FN
:
15439 CASE_CFN_ROUNDEVEN
:
15440 CASE_CFN_ROUNDEVEN_FN
:
15449 return RECURSE (arg0
) && RECURSE (arg1
);
15457 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15458 has an integer value. We also allow +Inf, -Inf and NaN to be
15459 considered integer values. Return false for signaling NaN.
15461 DEPTH is the current nesting depth of the query. */
15464 integer_valued_real_single_p (tree t
, int depth
)
15466 switch (TREE_CODE (t
))
15469 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15472 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15475 /* Limit the depth of recursion to avoid quadratic behavior.
15476 This is expected to catch almost all occurrences in practice.
15477 If this code misses important cases that unbounded recursion
15478 would not, passes that need this information could be revised
15479 to provide it through dataflow propagation. */
15480 return (!name_registered_for_update_p (t
)
15481 && depth
< param_max_ssa_name_query_depth
15482 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15491 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15492 has an integer value. We also allow +Inf, -Inf and NaN to be
15493 considered integer values. Return false for signaling NaN.
15495 DEPTH is the current nesting depth of the query. */
15498 integer_valued_real_invalid_p (tree t
, int depth
)
15500 switch (TREE_CODE (t
))
15502 case COMPOUND_EXPR
:
15505 return RECURSE (TREE_OPERAND (t
, 1));
15508 return RECURSE (TREE_OPERAND (t
, 0));
15517 #undef integer_valued_real_p
15519 /* Return true if the floating point expression T has an integer value.
15520 We also allow +Inf, -Inf and NaN to be considered integer values.
15521 Return false for signaling NaN.
15523 DEPTH is the current nesting depth of the query. */
15526 integer_valued_real_p (tree t
, int depth
)
15528 if (t
== error_mark_node
)
15531 STRIP_ANY_LOCATION_WRAPPER (t
);
15533 tree_code code
= TREE_CODE (t
);
15534 switch (TREE_CODE_CLASS (code
))
15537 case tcc_comparison
:
15538 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15539 TREE_OPERAND (t
, 1), depth
);
15542 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15545 case tcc_declaration
:
15546 case tcc_reference
:
15547 return integer_valued_real_single_p (t
, depth
);
15557 return integer_valued_real_single_p (t
, depth
);
15561 tree arg0
= (call_expr_nargs (t
) > 0
15562 ? CALL_EXPR_ARG (t
, 0)
15564 tree arg1
= (call_expr_nargs (t
) > 1
15565 ? CALL_EXPR_ARG (t
, 1)
15567 return integer_valued_real_call_p (get_call_combined_fn (t
),
15568 arg0
, arg1
, depth
);
15572 return integer_valued_real_invalid_p (t
, depth
);
15576 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15577 attempt to fold the expression to a constant without modifying TYPE,
15580 If the expression could be simplified to a constant, then return
15581 the constant. If the expression would not be simplified to a
15582 constant, then return NULL_TREE. */
15585 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15587 tree tem
= fold_binary (code
, type
, op0
, op1
);
15588 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15591 /* Given the components of a unary expression CODE, TYPE and OP0,
15592 attempt to fold the expression to a constant without modifying
15595 If the expression could be simplified to a constant, then return
15596 the constant. If the expression would not be simplified to a
15597 constant, then return NULL_TREE. */
15600 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15602 tree tem
= fold_unary (code
, type
, op0
);
15603 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15606 /* If EXP represents referencing an element in a constant string
15607 (either via pointer arithmetic or array indexing), return the
15608 tree representing the value accessed, otherwise return NULL. */
15611 fold_read_from_constant_string (tree exp
)
15613 if ((TREE_CODE (exp
) == INDIRECT_REF
15614 || TREE_CODE (exp
) == ARRAY_REF
)
15615 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15617 tree exp1
= TREE_OPERAND (exp
, 0);
15620 location_t loc
= EXPR_LOCATION (exp
);
15622 if (TREE_CODE (exp
) == INDIRECT_REF
)
15623 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15626 tree low_bound
= array_ref_low_bound (exp
);
15627 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15629 /* Optimize the special-case of a zero lower bound.
15631 We convert the low_bound to sizetype to avoid some problems
15632 with constant folding. (E.g. suppose the lower bound is 1,
15633 and its mode is QI. Without the conversion,l (ARRAY
15634 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15635 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15636 if (! integer_zerop (low_bound
))
15637 index
= size_diffop_loc (loc
, index
,
15638 fold_convert_loc (loc
, sizetype
, low_bound
));
15643 scalar_int_mode char_mode
;
15645 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15646 && TREE_CODE (string
) == STRING_CST
15647 && tree_fits_uhwi_p (index
)
15648 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15649 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15651 && GET_MODE_SIZE (char_mode
) == 1)
15652 return build_int_cst_type (TREE_TYPE (exp
),
15653 (TREE_STRING_POINTER (string
)
15654 [TREE_INT_CST_LOW (index
)]));
15659 /* Folds a read from vector element at IDX of vector ARG. */
15662 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15664 unsigned HOST_WIDE_INT i
;
15665 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15666 && known_ge (idx
, 0u)
15667 && idx
.is_constant (&i
))
15669 if (TREE_CODE (arg
) == VECTOR_CST
)
15670 return VECTOR_CST_ELT (arg
, i
);
15671 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15673 if (CONSTRUCTOR_NELTS (arg
)
15674 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg
, 0)->value
)))
15676 if (i
>= CONSTRUCTOR_NELTS (arg
))
15677 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
15678 return CONSTRUCTOR_ELT (arg
, i
)->value
;
15684 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15685 an integer constant, real, or fixed-point constant.
15687 TYPE is the type of the result. */
15690 fold_negate_const (tree arg0
, tree type
)
15692 tree t
= NULL_TREE
;
15694 switch (TREE_CODE (arg0
))
15697 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15702 FIXED_VALUE_TYPE f
;
15703 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
15704 &(TREE_FIXED_CST (arg0
)), NULL
,
15705 TYPE_SATURATING (type
));
15706 t
= build_fixed (type
, f
);
15707 /* Propagate overflow flags. */
15708 if (overflow_p
| TREE_OVERFLOW (arg0
))
15709 TREE_OVERFLOW (t
) = 1;
15714 if (poly_int_tree_p (arg0
))
15716 wi::overflow_type overflow
;
15717 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
15718 t
= force_fit_type (type
, res
, 1,
15719 (overflow
&& ! TYPE_UNSIGNED (type
))
15720 || TREE_OVERFLOW (arg0
));
15724 gcc_unreachable ();
15730 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15731 an integer constant or real constant.
15733 TYPE is the type of the result. */
15736 fold_abs_const (tree arg0
, tree type
)
15738 tree t
= NULL_TREE
;
15740 switch (TREE_CODE (arg0
))
15744 /* If the value is unsigned or non-negative, then the absolute value
15745 is the same as the ordinary value. */
15746 wide_int val
= wi::to_wide (arg0
);
15747 wi::overflow_type overflow
= wi::OVF_NONE
;
15748 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
15751 /* If the value is negative, then the absolute value is
15754 val
= wi::neg (val
, &overflow
);
15756 /* Force to the destination type, set TREE_OVERFLOW for signed
15758 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
15763 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
15764 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15770 gcc_unreachable ();
15776 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15777 constant. TYPE is the type of the result. */
15780 fold_not_const (const_tree arg0
, tree type
)
15782 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
15784 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
15787 /* Given CODE, a relational operator, the target type, TYPE and two
15788 constant operands OP0 and OP1, return the result of the
15789 relational operation. If the result is not a compile time
15790 constant, then return NULL_TREE. */
15793 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
15795 int result
, invert
;
15797 /* From here on, the only cases we handle are when the result is
15798 known to be a constant. */
15800 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
15802 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
15803 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
15805 /* Handle the cases where either operand is a NaN. */
15806 if (real_isnan (c0
) || real_isnan (c1
))
15816 case UNORDERED_EXPR
:
15830 if (flag_trapping_math
)
15836 gcc_unreachable ();
15839 return constant_boolean_node (result
, type
);
15842 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
15845 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
15847 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
15848 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
15849 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
15852 /* Handle equality/inequality of complex constants. */
15853 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
15855 tree rcond
= fold_relational_const (code
, type
,
15856 TREE_REALPART (op0
),
15857 TREE_REALPART (op1
));
15858 tree icond
= fold_relational_const (code
, type
,
15859 TREE_IMAGPART (op0
),
15860 TREE_IMAGPART (op1
));
15861 if (code
== EQ_EXPR
)
15862 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
15863 else if (code
== NE_EXPR
)
15864 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
15869 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
15871 if (!VECTOR_TYPE_P (type
))
15873 /* Have vector comparison with scalar boolean result. */
15874 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
15875 && known_eq (VECTOR_CST_NELTS (op0
),
15876 VECTOR_CST_NELTS (op1
)));
15877 unsigned HOST_WIDE_INT nunits
;
15878 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
15880 for (unsigned i
= 0; i
< nunits
; i
++)
15882 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15883 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15884 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
15885 if (tmp
== NULL_TREE
)
15887 if (integer_zerop (tmp
))
15888 return constant_boolean_node (code
== NE_EXPR
, type
);
15890 return constant_boolean_node (code
== EQ_EXPR
, type
);
15892 tree_vector_builder elts
;
15893 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
15895 unsigned int count
= elts
.encoded_nelts ();
15896 for (unsigned i
= 0; i
< count
; i
++)
15898 tree elem_type
= TREE_TYPE (type
);
15899 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15900 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15902 tree tem
= fold_relational_const (code
, elem_type
,
15905 if (tem
== NULL_TREE
)
15908 elts
.quick_push (build_int_cst (elem_type
,
15909 integer_zerop (tem
) ? 0 : -1));
15912 return elts
.build ();
15915 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15917 To compute GT, swap the arguments and do LT.
15918 To compute GE, do LT and invert the result.
15919 To compute LE, swap the arguments, do LT and invert the result.
15920 To compute NE, do EQ and invert the result.
15922 Therefore, the code below must handle only EQ and LT. */
15924 if (code
== LE_EXPR
|| code
== GT_EXPR
)
15926 std::swap (op0
, op1
);
15927 code
= swap_tree_comparison (code
);
15930 /* Note that it is safe to invert for real values here because we
15931 have already handled the one case that it matters. */
15934 if (code
== NE_EXPR
|| code
== GE_EXPR
)
15937 code
= invert_tree_comparison (code
, false);
15940 /* Compute a result for LT or EQ if args permit;
15941 Otherwise return T. */
15942 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
15944 if (code
== EQ_EXPR
)
15945 result
= tree_int_cst_equal (op0
, op1
);
15947 result
= tree_int_cst_lt (op0
, op1
);
15954 return constant_boolean_node (result
, type
);
15957 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15958 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15962 fold_build_cleanup_point_expr (tree type
, tree expr
)
15964 /* If the expression does not have side effects then we don't have to wrap
15965 it with a cleanup point expression. */
15966 if (!TREE_SIDE_EFFECTS (expr
))
15969 /* If the expression is a return, check to see if the expression inside the
15970 return has no side effects or the right hand side of the modify expression
15971 inside the return. If either don't have side effects set we don't need to
15972 wrap the expression in a cleanup point expression. Note we don't check the
15973 left hand side of the modify because it should always be a return decl. */
15974 if (TREE_CODE (expr
) == RETURN_EXPR
)
15976 tree op
= TREE_OPERAND (expr
, 0);
15977 if (!op
|| !TREE_SIDE_EFFECTS (op
))
15979 op
= TREE_OPERAND (op
, 1);
15980 if (!TREE_SIDE_EFFECTS (op
))
15984 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
15987 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15988 of an indirection through OP0, or NULL_TREE if no simplification is
15992 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
15996 poly_uint64 const_op01
;
15999 subtype
= TREE_TYPE (sub
);
16000 if (!POINTER_TYPE_P (subtype
)
16001 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
16004 if (TREE_CODE (sub
) == ADDR_EXPR
)
16006 tree op
= TREE_OPERAND (sub
, 0);
16007 tree optype
= TREE_TYPE (op
);
16009 /* *&CONST_DECL -> to the value of the const decl. */
16010 if (TREE_CODE (op
) == CONST_DECL
)
16011 return DECL_INITIAL (op
);
16012 /* *&p => p; make sure to handle *&"str"[cst] here. */
16013 if (type
== optype
)
16015 tree fop
= fold_read_from_constant_string (op
);
16021 /* *(foo *)&fooarray => fooarray[0] */
16022 else if (TREE_CODE (optype
) == ARRAY_TYPE
16023 && type
== TREE_TYPE (optype
)
16024 && (!in_gimple_form
16025 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16027 tree type_domain
= TYPE_DOMAIN (optype
);
16028 tree min_val
= size_zero_node
;
16029 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16030 min_val
= TYPE_MIN_VALUE (type_domain
);
16032 && TREE_CODE (min_val
) != INTEGER_CST
)
16034 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
16035 NULL_TREE
, NULL_TREE
);
16037 /* *(foo *)&complexfoo => __real__ complexfoo */
16038 else if (TREE_CODE (optype
) == COMPLEX_TYPE
16039 && type
== TREE_TYPE (optype
))
16040 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
16041 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16042 else if (VECTOR_TYPE_P (optype
)
16043 && type
== TREE_TYPE (optype
))
16045 tree part_width
= TYPE_SIZE (type
);
16046 tree index
= bitsize_int (0);
16047 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
16052 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16053 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
16055 tree op00
= TREE_OPERAND (sub
, 0);
16056 tree op01
= TREE_OPERAND (sub
, 1);
16059 if (TREE_CODE (op00
) == ADDR_EXPR
)
16062 op00
= TREE_OPERAND (op00
, 0);
16063 op00type
= TREE_TYPE (op00
);
16065 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16066 if (VECTOR_TYPE_P (op00type
)
16067 && type
== TREE_TYPE (op00type
)
16068 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16069 but we want to treat offsets with MSB set as negative.
16070 For the code below negative offsets are invalid and
16071 TYPE_SIZE of the element is something unsigned, so
16072 check whether op01 fits into poly_int64, which implies
16073 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16074 then just use poly_uint64 because we want to treat the
16075 value as unsigned. */
16076 && tree_fits_poly_int64_p (op01
))
16078 tree part_width
= TYPE_SIZE (type
);
16079 poly_uint64 max_offset
16080 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
16081 * TYPE_VECTOR_SUBPARTS (op00type
));
16082 if (known_lt (const_op01
, max_offset
))
16084 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
16085 return fold_build3_loc (loc
,
16086 BIT_FIELD_REF
, type
, op00
,
16087 part_width
, index
);
16090 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16091 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16092 && type
== TREE_TYPE (op00type
))
16094 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
16096 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16098 /* ((foo *)&fooarray)[1] => fooarray[1] */
16099 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16100 && type
== TREE_TYPE (op00type
))
16102 tree type_domain
= TYPE_DOMAIN (op00type
);
16103 tree min_val
= size_zero_node
;
16104 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16105 min_val
= TYPE_MIN_VALUE (type_domain
);
16106 poly_uint64 type_size
, index
;
16107 if (poly_int_tree_p (min_val
)
16108 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
16109 && multiple_p (const_op01
, type_size
, &index
))
16111 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
16112 op01
= wide_int_to_tree (sizetype
, off
);
16113 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16114 NULL_TREE
, NULL_TREE
);
16120 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16121 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16122 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16123 && (!in_gimple_form
16124 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16127 tree min_val
= size_zero_node
;
16128 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16129 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16130 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16131 min_val
= TYPE_MIN_VALUE (type_domain
);
16133 && TREE_CODE (min_val
) != INTEGER_CST
)
16135 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16142 /* Builds an expression for an indirection through T, simplifying some
16146 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16148 tree type
= TREE_TYPE (TREE_TYPE (t
));
16149 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16154 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16157 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16160 fold_indirect_ref_loc (location_t loc
, tree t
)
16162 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16170 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16171 whose result is ignored. The type of the returned tree need not be
16172 the same as the original expression. */
16175 fold_ignored_result (tree t
)
16177 if (!TREE_SIDE_EFFECTS (t
))
16178 return integer_zero_node
;
16181 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16184 t
= TREE_OPERAND (t
, 0);
16188 case tcc_comparison
:
16189 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16190 t
= TREE_OPERAND (t
, 0);
16191 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16192 t
= TREE_OPERAND (t
, 1);
16197 case tcc_expression
:
16198 switch (TREE_CODE (t
))
16200 case COMPOUND_EXPR
:
16201 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16203 t
= TREE_OPERAND (t
, 0);
16207 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16208 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16210 t
= TREE_OPERAND (t
, 0);
16223 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16226 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16228 tree div
= NULL_TREE
;
16233 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16234 have to do anything. Only do this when we are not given a const,
16235 because in that case, this check is more expensive than just
16237 if (TREE_CODE (value
) != INTEGER_CST
)
16239 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16241 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16245 /* If divisor is a power of two, simplify this to bit manipulation. */
16246 if (pow2_or_zerop (divisor
))
16248 if (TREE_CODE (value
) == INTEGER_CST
)
16250 wide_int val
= wi::to_wide (value
);
16253 if ((val
& (divisor
- 1)) == 0)
16256 overflow_p
= TREE_OVERFLOW (value
);
16257 val
+= divisor
- 1;
16258 val
&= (int) -divisor
;
16262 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16268 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16269 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16270 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16271 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16277 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16278 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16279 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16285 /* Likewise, but round down. */
16288 round_down_loc (location_t loc
, tree value
, int divisor
)
16290 tree div
= NULL_TREE
;
16292 gcc_assert (divisor
> 0);
16296 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16297 have to do anything. Only do this when we are not given a const,
16298 because in that case, this check is more expensive than just
16300 if (TREE_CODE (value
) != INTEGER_CST
)
16302 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16304 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16308 /* If divisor is a power of two, simplify this to bit manipulation. */
16309 if (pow2_or_zerop (divisor
))
16313 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16314 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16319 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16320 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16321 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16327 /* Returns the pointer to the base of the object addressed by EXP and
16328 extracts the information about the offset of the access, storing it
16329 to PBITPOS and POFFSET. */
16332 split_address_to_core_and_offset (tree exp
,
16333 poly_int64_pod
*pbitpos
, tree
*poffset
)
16337 int unsignedp
, reversep
, volatilep
;
16338 poly_int64 bitsize
;
16339 location_t loc
= EXPR_LOCATION (exp
);
16341 if (TREE_CODE (exp
) == ADDR_EXPR
)
16343 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16344 poffset
, &mode
, &unsignedp
, &reversep
,
16346 core
= build_fold_addr_expr_loc (loc
, core
);
16348 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16350 core
= TREE_OPERAND (exp
, 0);
16353 *poffset
= TREE_OPERAND (exp
, 1);
16354 if (poly_int_tree_p (*poffset
))
16356 poly_offset_int tem
16357 = wi::sext (wi::to_poly_offset (*poffset
),
16358 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16359 tem
<<= LOG2_BITS_PER_UNIT
;
16360 if (tem
.to_shwi (pbitpos
))
16361 *poffset
= NULL_TREE
;
16368 *poffset
= NULL_TREE
;
16374 /* Returns true if addresses of E1 and E2 differ by a constant, false
16375 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16378 ptr_difference_const (tree e1
, tree e2
, poly_int64_pod
*diff
)
16381 poly_int64 bitpos1
, bitpos2
;
16382 tree toffset1
, toffset2
, tdiff
, type
;
16384 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16385 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16387 poly_int64 bytepos1
, bytepos2
;
16388 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16389 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16390 || !operand_equal_p (core1
, core2
, 0))
16393 if (toffset1
&& toffset2
)
16395 type
= TREE_TYPE (toffset1
);
16396 if (type
!= TREE_TYPE (toffset2
))
16397 toffset2
= fold_convert (type
, toffset2
);
16399 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16400 if (!cst_and_fits_in_hwi (tdiff
))
16403 *diff
= int_cst_value (tdiff
);
16405 else if (toffset1
|| toffset2
)
16407 /* If only one of the offsets is non-constant, the difference cannot
16414 *diff
+= bytepos1
- bytepos2
;
16418 /* Return OFF converted to a pointer offset type suitable as offset for
16419 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16421 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16423 if (ptrofftype_p (TREE_TYPE (off
)))
16425 return fold_convert_loc (loc
, sizetype
, off
);
16428 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16430 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16432 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16433 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16436 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16438 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16440 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16441 ptr
, size_int (off
));
16444 /* Return a pointer to a NUL-terminated string containing the sequence
16445 of bytes corresponding to the representation of the object referred to
16446 by SRC (or a subsequence of such bytes within it if SRC is a reference
16447 to an initialized constant array plus some constant offset).
16448 Set *STRSIZE the number of bytes in the constant sequence including
16449 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16450 where A is the array that stores the constant sequence that SRC points
16451 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16452 need not point to a string or even an array of characters but may point
16453 to an object of any type. */
16456 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16458 /* The offset into the array A storing the string, and A's byte size. */
16466 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16468 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16472 unsigned HOST_WIDE_INT offset
= 0;
16473 if (offset_node
!= NULL_TREE
)
16475 if (!tree_fits_uhwi_p (offset_node
))
16478 offset
= tree_to_uhwi (offset_node
);
16481 if (!tree_fits_uhwi_p (mem_size
))
16484 /* ARRAY_SIZE is the byte size of the array the constant sequence
16485 is stored in and equal to sizeof A. INIT_BYTES is the number
16486 of bytes in the constant sequence used to initialize the array,
16487 including any embedded NULs as well as the terminating NUL (for
16488 strings), but not including any trailing zeros/NULs past
16489 the terminating one appended implicitly to a string literal to
16490 zero out the remainder of the array it's stored in. For example,
16492 const char a[7] = "abc\0d";
16493 n = strlen (a + 1);
16494 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16495 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16496 is equal to strlen (A) + 1. */
16497 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16498 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16499 const char *string
= TREE_STRING_POINTER (src
);
16501 /* Ideally this would turn into a gcc_checking_assert over time. */
16502 if (init_bytes
> array_size
)
16503 init_bytes
= array_size
;
16505 if (init_bytes
== 0 || offset
>= array_size
)
16510 /* Compute and store the number of characters from the beginning
16511 of the substring at OFFSET to the end, including the terminating
16512 nul. Offsets past the initial length refer to null strings. */
16513 if (offset
< init_bytes
)
16514 *strsize
= init_bytes
- offset
;
16520 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16521 /* Support only properly NUL-terminated single byte strings. */
16522 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16524 if (string
[init_bytes
- 1] != '\0')
16528 return offset
< init_bytes
? string
+ offset
: "";
16531 /* Return a pointer to a NUL-terminated string corresponding to
16532 the expression STR referencing a constant string, possibly
16533 involving a constant offset. Return null if STR either doesn't
16534 reference a constant string or if it involves a nonconstant
16538 c_getstr (tree str
)
16540 return getbyterep (str
, NULL
);
16543 /* Given a tree T, compute which bits in T may be nonzero. */
16546 tree_nonzero_bits (const_tree t
)
16548 switch (TREE_CODE (t
))
16551 return wi::to_wide (t
);
16553 return get_nonzero_bits (t
);
16554 case NON_LVALUE_EXPR
:
16556 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16558 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16559 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16562 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16563 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16565 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16566 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16568 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16569 TYPE_PRECISION (TREE_TYPE (t
)),
16570 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16572 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16574 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16575 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16576 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16577 return wi::bit_or (nzbits1
, nzbits2
);
16581 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16583 tree type
= TREE_TYPE (t
);
16584 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16585 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16586 TYPE_PRECISION (type
));
16587 return wi::neg_p (arg1
)
16588 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16589 : wi::lshift (nzbits
, arg1
);
16593 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16595 tree type
= TREE_TYPE (t
);
16596 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16597 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16598 TYPE_PRECISION (type
));
16599 return wi::neg_p (arg1
)
16600 ? wi::lshift (nzbits
, -arg1
)
16601 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16608 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16611 /* Helper function for address compare simplifications in match.pd.
16612 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16613 TYPE is the type of comparison operands.
16614 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16615 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16616 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16617 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16618 and 2 if unknown. */
16621 address_compare (tree_code code
, tree type
, tree op0
, tree op1
,
16622 tree
&base0
, tree
&base1
, poly_int64
&off0
, poly_int64
&off1
,
16625 gcc_checking_assert (TREE_CODE (op0
) == ADDR_EXPR
);
16626 gcc_checking_assert (TREE_CODE (op1
) == ADDR_EXPR
);
16627 base0
= get_addr_base_and_unit_offset (TREE_OPERAND (op0
, 0), &off0
);
16628 base1
= get_addr_base_and_unit_offset (TREE_OPERAND (op1
, 0), &off1
);
16629 if (base0
&& TREE_CODE (base0
) == MEM_REF
)
16631 off0
+= mem_ref_offset (base0
).force_shwi ();
16632 base0
= TREE_OPERAND (base0
, 0);
16634 if (base1
&& TREE_CODE (base1
) == MEM_REF
)
16636 off1
+= mem_ref_offset (base1
).force_shwi ();
16637 base1
= TREE_OPERAND (base1
, 0);
16639 if (base0
== NULL_TREE
|| base1
== NULL_TREE
)
16643 /* Punt in GENERIC on variables with value expressions;
16644 the value expressions might point to fields/elements
16645 of other vars etc. */
16647 && ((VAR_P (base0
) && DECL_HAS_VALUE_EXPR_P (base0
))
16648 || (VAR_P (base1
) && DECL_HAS_VALUE_EXPR_P (base1
))))
16650 else if (decl_in_symtab_p (base0
) && decl_in_symtab_p (base1
))
16652 symtab_node
*node0
= symtab_node::get_create (base0
);
16653 symtab_node
*node1
= symtab_node::get_create (base1
);
16654 equal
= node0
->equal_address_to (node1
);
16656 else if ((DECL_P (base0
)
16657 || TREE_CODE (base0
) == SSA_NAME
16658 || TREE_CODE (base0
) == STRING_CST
)
16660 || TREE_CODE (base1
) == SSA_NAME
16661 || TREE_CODE (base1
) == STRING_CST
))
16662 equal
= (base0
== base1
);
16663 /* Assume different STRING_CSTs with the same content will be
16666 && TREE_CODE (base0
) == STRING_CST
16667 && TREE_CODE (base1
) == STRING_CST
16668 && TREE_STRING_LENGTH (base0
) == TREE_STRING_LENGTH (base1
)
16669 && memcmp (TREE_STRING_POINTER (base0
), TREE_STRING_POINTER (base1
),
16670 TREE_STRING_LENGTH (base0
)) == 0)
16674 if (code
== EQ_EXPR
16676 /* If the offsets are equal we can ignore overflow. */
16677 || known_eq (off0
, off1
)
16678 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
16679 /* Or if we compare using pointers to decls or strings. */
16680 || (POINTER_TYPE_P (type
)
16681 && (DECL_P (base0
) || TREE_CODE (base0
) == STRING_CST
)))
16687 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
16690 /* At this point we know (or assume) the two pointers point at
16691 different objects. */
16692 HOST_WIDE_INT ioff0
= -1, ioff1
= -1;
16693 off0
.is_constant (&ioff0
);
16694 off1
.is_constant (&ioff1
);
16695 /* Punt on non-zero offsets from functions. */
16696 if ((TREE_CODE (base0
) == FUNCTION_DECL
&& ioff0
)
16697 || (TREE_CODE (base1
) == FUNCTION_DECL
&& ioff1
))
16699 /* Or if the bases are neither decls nor string literals. */
16700 if (!DECL_P (base0
) && TREE_CODE (base0
) != STRING_CST
)
16702 if (!DECL_P (base1
) && TREE_CODE (base1
) != STRING_CST
)
16704 /* For initializers, assume addresses of different functions are
16706 if (folding_initializer
16707 && TREE_CODE (base0
) == FUNCTION_DECL
16708 && TREE_CODE (base1
) == FUNCTION_DECL
)
16711 /* Compute whether one address points to the start of one
16712 object and another one to the end of another one. */
16713 poly_int64 size0
= 0, size1
= 0;
16714 if (TREE_CODE (base0
) == STRING_CST
)
16716 if (ioff0
< 0 || ioff0
> TREE_STRING_LENGTH (base0
))
16719 size0
= TREE_STRING_LENGTH (base0
);
16721 else if (TREE_CODE (base0
) == FUNCTION_DECL
)
16725 tree sz0
= DECL_SIZE_UNIT (base0
);
16726 if (!tree_fits_poly_int64_p (sz0
))
16729 size0
= tree_to_poly_int64 (sz0
);
16731 if (TREE_CODE (base1
) == STRING_CST
)
16733 if (ioff1
< 0 || ioff1
> TREE_STRING_LENGTH (base1
))
16736 size1
= TREE_STRING_LENGTH (base1
);
16738 else if (TREE_CODE (base1
) == FUNCTION_DECL
)
16742 tree sz1
= DECL_SIZE_UNIT (base1
);
16743 if (!tree_fits_poly_int64_p (sz1
))
16746 size1
= tree_to_poly_int64 (sz1
);
16750 /* If one offset is pointing (or could be) to the beginning of one
16751 object and the other is pointing to one past the last byte of the
16752 other object, punt. */
16753 if (maybe_eq (off0
, 0) && maybe_eq (off1
, size1
))
16755 else if (maybe_eq (off1
, 0) && maybe_eq (off0
, size0
))
16757 /* If both offsets are the same, there are some cases we know that are
16758 ok. Either if we know they aren't zero, or if we know both sizes
16761 && known_eq (off0
, off1
)
16762 && (known_ne (off0
, 0)
16763 || (known_ne (size0
, 0) && known_ne (size1
, 0))))
16767 /* At this point, equal is 2 if either one or both pointers are out of
16768 bounds of their object, or one points to start of its object and the
16769 other points to end of its object. This is unspecified behavior
16770 e.g. in C++. Otherwise equal is 0. */
16771 if (folding_cxx_constexpr
&& equal
)
16774 /* When both pointers point to string literals, even when equal is 0,
16775 due to tail merging of string literals the pointers might be the same. */
16776 if (TREE_CODE (base0
) == STRING_CST
&& TREE_CODE (base1
) == STRING_CST
)
16780 || ioff0
> TREE_STRING_LENGTH (base0
)
16781 || ioff1
> TREE_STRING_LENGTH (base1
))
16784 /* If the bytes in the string literals starting at the pointers
16785 differ, the pointers need to be different. */
16786 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
,
16787 TREE_STRING_POINTER (base1
) + ioff1
,
16788 MIN (TREE_STRING_LENGTH (base0
) - ioff0
,
16789 TREE_STRING_LENGTH (base1
) - ioff1
)) == 0)
16791 HOST_WIDE_INT ioffmin
= MIN (ioff0
, ioff1
);
16792 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
- ioffmin
,
16793 TREE_STRING_POINTER (base1
) + ioff1
- ioffmin
,
16795 /* If even the bytes in the string literal before the
16796 pointers are the same, the string literals could be
16803 if (folding_cxx_constexpr
)
16806 /* If this is a pointer comparison, ignore for now even
16807 valid equalities where one pointer is the offset zero
16808 of one object and the other to one past end of another one. */
16809 if (!INTEGRAL_TYPE_P (type
))
16812 /* Assume that string literals can't be adjacent to variables
16813 (automatic or global). */
16814 if (TREE_CODE (base0
) == STRING_CST
|| TREE_CODE (base1
) == STRING_CST
)
16817 /* Assume that automatic variables can't be adjacent to global
16819 if (is_global_var (base0
) != is_global_var (base1
))
16825 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16827 ctor_single_nonzero_element (const_tree t
)
16829 unsigned HOST_WIDE_INT idx
;
16830 constructor_elt
*ce
;
16831 tree elt
= NULL_TREE
;
16833 if (TREE_CODE (t
) != CONSTRUCTOR
)
16835 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
16836 if (!integer_zerop (ce
->value
) && !real_zerop (ce
->value
))
16847 namespace selftest
{
16849 /* Helper functions for writing tests of folding trees. */
16851 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16854 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
16857 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
16860 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16861 wrapping WRAPPED_EXPR. */
16864 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
16867 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
16868 ASSERT_NE (wrapped_expr
, result
);
16869 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
16870 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
16873 /* Verify that various arithmetic binary operations are folded
16877 test_arithmetic_folding ()
16879 tree type
= integer_type_node
;
16880 tree x
= create_tmp_var_raw (type
, "x");
16881 tree zero
= build_zero_cst (type
);
16882 tree one
= build_int_cst (type
, 1);
16885 /* 1 <-- (0 + 1) */
16886 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
16888 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
16891 /* (nonlvalue)x <-- (x + 0) */
16892 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
16896 /* 0 <-- (x - x) */
16897 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
16899 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
16902 /* Multiplication. */
16903 /* 0 <-- (x * 0) */
16904 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
16907 /* (nonlvalue)x <-- (x * 1) */
16908 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
16912 /* Verify that various binary operations on vectors are folded
16916 test_vector_folding ()
16918 tree inner_type
= integer_type_node
;
16919 tree type
= build_vector_type (inner_type
, 4);
16920 tree zero
= build_zero_cst (type
);
16921 tree one
= build_one_cst (type
);
16922 tree index
= build_index_vector (type
, 0, 1);
16924 /* Verify equality tests that return a scalar boolean result. */
16925 tree res_type
= boolean_type_node
;
16926 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
16927 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
16928 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
16929 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
16930 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
16931 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16933 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
16935 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16939 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16942 test_vec_duplicate_folding ()
16944 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
16945 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
16946 /* This will be 1 if VEC_MODE isn't a vector mode. */
16947 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
16949 tree type
= build_vector_type (ssizetype
, nunits
);
16950 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
16951 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
16952 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
16955 /* Run all of the selftests within this file. */
16958 fold_const_cc_tests ()
16960 test_arithmetic_folding ();
16961 test_vector_folding ();
16962 test_vec_duplicate_folding ();
16965 } // namespace selftest
16967 #endif /* CHECKING_P */