1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2021 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-fold.h"
74 #include "tree-into-ssa.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
79 #include "tree-ssanames.h"
81 #include "stringpool.h"
83 #include "tree-vector-builder.h"
84 #include "vec-perm-indices.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
89 int folding_initializer
= 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code
{
113 static bool negate_expr_p (tree
);
114 static tree
negate_expr (tree
);
115 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
116 static enum comparison_code
comparison_to_compcode (enum tree_code
);
117 static enum tree_code
compcode_to_comparison (enum comparison_code
);
118 static bool twoval_comparison_p (tree
, tree
*, tree
*);
119 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
120 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
122 static bool simple_operand_p (const_tree
);
123 static bool simple_operand_p_2 (tree
);
124 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
125 static tree
range_predecessor (tree
);
126 static tree
range_successor (tree
);
127 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
128 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
129 static tree
unextend (tree
, int, int, tree
);
130 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
131 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
132 static tree
fold_binary_op_with_conditional_arg (location_t
,
133 enum tree_code
, tree
,
136 static tree
fold_negate_const (tree
, tree
);
137 static tree
fold_not_const (const_tree
, tree
);
138 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
139 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
140 static tree
fold_view_convert_expr (tree
, tree
);
141 static tree
fold_negate_expr (location_t
, tree
);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
148 expr_location_or (tree t
, location_t loc
)
150 location_t tloc
= EXPR_LOCATION (t
);
151 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
158 protected_set_expr_location_unshare (tree x
, location_t loc
)
160 if (CAN_HAVE_LOCATION_P (x
)
161 && EXPR_LOCATION (x
) != loc
162 && !(TREE_CODE (x
) == SAVE_EXPR
163 || TREE_CODE (x
) == TARGET_EXPR
164 || TREE_CODE (x
) == BIND_EXPR
))
167 SET_EXPR_LOCATION (x
, loc
);
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
177 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
181 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
183 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
197 static int fold_deferring_overflow_warnings
;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning
;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings
;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
230 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
235 gcc_assert (fold_deferring_overflow_warnings
> 0);
236 --fold_deferring_overflow_warnings
;
237 if (fold_deferring_overflow_warnings
> 0)
239 if (fold_deferred_overflow_warning
!= NULL
241 && code
< (int) fold_deferred_overflow_code
)
242 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
246 warnmsg
= fold_deferred_overflow_warning
;
247 fold_deferred_overflow_warning
= NULL
;
249 if (!issue
|| warnmsg
== NULL
)
252 if (gimple_no_warning_p (stmt
))
255 /* Use the smallest code level when deciding to issue the
257 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
258 code
= fold_deferred_overflow_code
;
260 if (!issue_strict_overflow_warning (code
))
264 locus
= input_location
;
266 locus
= gimple_location (stmt
);
267 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
270 /* Stop deferring overflow warnings, ignoring any deferred
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL
, 0);
279 /* Whether we are deferring overflow warnings. */
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings
> 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
291 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
293 if (fold_deferring_overflow_warnings
> 0)
295 if (fold_deferred_overflow_warning
== NULL
296 || wc
< fold_deferred_overflow_code
)
298 fold_deferred_overflow_warning
= gmsgid
;
299 fold_deferred_overflow_code
= wc
;
302 else if (issue_strict_overflow_warning (wc
))
303 warning (OPT_Wstrict_overflow
, gmsgid
);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
310 negate_mathfn_p (combined_fn fn
)
333 CASE_CFN_ROUNDEVEN_FN
:
345 return !flag_rounding_math
;
353 /* Check whether we may negate an integer constant T without causing
357 may_negate_without_overflow_p (const_tree t
)
361 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
363 type
= TREE_TYPE (t
);
364 if (TYPE_UNSIGNED (type
))
367 return !wi::only_sign_bit_p (wi::to_wide (t
));
370 /* Determine whether an expression T can be cheaply negated using
371 the function negate_expr without introducing undefined overflow. */
374 negate_expr_p (tree t
)
381 type
= TREE_TYPE (t
);
384 switch (TREE_CODE (t
))
387 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
390 /* Check that -CST will not overflow type. */
391 return may_negate_without_overflow_p (t
);
393 return (INTEGRAL_TYPE_P (type
)
394 && TYPE_OVERFLOW_WRAPS (type
));
400 return !TYPE_OVERFLOW_SANITIZED (type
);
403 /* We want to canonicalize to positive real constants. Pretend
404 that only negative ones can be easily negated. */
405 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
408 return negate_expr_p (TREE_REALPART (t
))
409 && negate_expr_p (TREE_IMAGPART (t
));
413 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
416 /* Steps don't prevent negation. */
417 unsigned int count
= vector_cst_encoded_nelts (t
);
418 for (unsigned int i
= 0; i
< count
; ++i
)
419 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
426 return negate_expr_p (TREE_OPERAND (t
, 0))
427 && negate_expr_p (TREE_OPERAND (t
, 1));
430 return negate_expr_p (TREE_OPERAND (t
, 0));
433 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
434 || HONOR_SIGNED_ZEROS (element_mode (type
))
435 || (ANY_INTEGRAL_TYPE_P (type
)
436 && ! TYPE_OVERFLOW_WRAPS (type
)))
438 /* -(A + B) -> (-B) - A. */
439 if (negate_expr_p (TREE_OPERAND (t
, 1)))
441 /* -(A + B) -> (-A) - B. */
442 return negate_expr_p (TREE_OPERAND (t
, 0));
445 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
446 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
447 && !HONOR_SIGNED_ZEROS (element_mode (type
))
448 && (! ANY_INTEGRAL_TYPE_P (type
)
449 || TYPE_OVERFLOW_WRAPS (type
));
452 if (TYPE_UNSIGNED (type
))
454 /* INT_MIN/n * n doesn't overflow while negating one operand it does
455 if n is a (negative) power of two. */
456 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
457 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
458 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
460 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
461 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
463 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
469 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
470 return negate_expr_p (TREE_OPERAND (t
, 1))
471 || negate_expr_p (TREE_OPERAND (t
, 0));
477 if (TYPE_UNSIGNED (type
))
479 /* In general we can't negate A in A / B, because if A is INT_MIN and
480 B is not 1 we change the sign of the result. */
481 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
482 && negate_expr_p (TREE_OPERAND (t
, 0)))
484 /* In general we can't negate B in A / B, because if A is INT_MIN and
485 B is 1, we may turn this into INT_MIN / -1 which is undefined
486 and actually traps on some architectures. */
487 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
488 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
489 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
490 && ! integer_onep (TREE_OPERAND (t
, 1))))
491 return negate_expr_p (TREE_OPERAND (t
, 1));
495 /* Negate -((double)float) as (double)(-float). */
496 if (TREE_CODE (type
) == REAL_TYPE
)
498 tree tem
= strip_float_extensions (t
);
500 return negate_expr_p (tem
);
505 /* Negate -f(x) as f(-x). */
506 if (negate_mathfn_p (get_call_combined_fn (t
)))
507 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
511 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
512 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
514 tree op1
= TREE_OPERAND (t
, 1);
515 if (wi::to_wide (op1
) == TYPE_PRECISION (type
) - 1)
526 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
527 simplification is possible.
528 If negate_expr_p would return true for T, NULL_TREE will never be
532 fold_negate_expr_1 (location_t loc
, tree t
)
534 tree type
= TREE_TYPE (t
);
537 switch (TREE_CODE (t
))
539 /* Convert - (~A) to A + 1. */
541 if (INTEGRAL_TYPE_P (type
))
542 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
543 build_one_cst (type
));
547 tem
= fold_negate_const (t
, type
);
548 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
549 || (ANY_INTEGRAL_TYPE_P (type
)
550 && !TYPE_OVERFLOW_TRAPS (type
)
551 && TYPE_OVERFLOW_WRAPS (type
))
552 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
559 tem
= fold_negate_const (t
, type
);
564 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
565 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
567 return build_complex (type
, rpart
, ipart
);
573 tree_vector_builder elts
;
574 elts
.new_unary_operation (type
, t
, true);
575 unsigned int count
= elts
.encoded_nelts ();
576 for (unsigned int i
= 0; i
< count
; ++i
)
578 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
579 if (elt
== NULL_TREE
)
581 elts
.quick_push (elt
);
584 return elts
.build ();
588 if (negate_expr_p (t
))
589 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
590 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
591 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
595 if (negate_expr_p (t
))
596 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
597 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
601 if (!TYPE_OVERFLOW_SANITIZED (type
))
602 return TREE_OPERAND (t
, 0);
606 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
607 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
609 /* -(A + B) -> (-B) - A. */
610 if (negate_expr_p (TREE_OPERAND (t
, 1)))
612 tem
= negate_expr (TREE_OPERAND (t
, 1));
613 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
614 tem
, TREE_OPERAND (t
, 0));
617 /* -(A + B) -> (-A) - B. */
618 if (negate_expr_p (TREE_OPERAND (t
, 0)))
620 tem
= negate_expr (TREE_OPERAND (t
, 0));
621 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
622 tem
, TREE_OPERAND (t
, 1));
628 /* - (A - B) -> B - A */
629 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
630 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
631 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
632 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
636 if (TYPE_UNSIGNED (type
))
642 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
644 tem
= TREE_OPERAND (t
, 1);
645 if (negate_expr_p (tem
))
646 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
647 TREE_OPERAND (t
, 0), negate_expr (tem
));
648 tem
= TREE_OPERAND (t
, 0);
649 if (negate_expr_p (tem
))
650 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
651 negate_expr (tem
), TREE_OPERAND (t
, 1));
658 if (TYPE_UNSIGNED (type
))
660 /* In general we can't negate A in A / B, because if A is INT_MIN and
661 B is not 1 we change the sign of the result. */
662 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
663 && negate_expr_p (TREE_OPERAND (t
, 0)))
664 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
665 negate_expr (TREE_OPERAND (t
, 0)),
666 TREE_OPERAND (t
, 1));
667 /* In general we can't negate B in A / B, because if A is INT_MIN and
668 B is 1, we may turn this into INT_MIN / -1 which is undefined
669 and actually traps on some architectures. */
670 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
671 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
672 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
673 && ! integer_onep (TREE_OPERAND (t
, 1))))
674 && negate_expr_p (TREE_OPERAND (t
, 1)))
675 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
677 negate_expr (TREE_OPERAND (t
, 1)));
681 /* Convert -((double)float) into (double)(-float). */
682 if (TREE_CODE (type
) == REAL_TYPE
)
684 tem
= strip_float_extensions (t
);
685 if (tem
!= t
&& negate_expr_p (tem
))
686 return fold_convert_loc (loc
, type
, negate_expr (tem
));
691 /* Negate -f(x) as f(-x). */
692 if (negate_mathfn_p (get_call_combined_fn (t
))
693 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
697 fndecl
= get_callee_fndecl (t
);
698 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
699 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
704 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
705 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
707 tree op1
= TREE_OPERAND (t
, 1);
708 if (wi::to_wide (op1
) == TYPE_PRECISION (type
) - 1)
710 tree ntype
= TYPE_UNSIGNED (type
)
711 ? signed_type_for (type
)
712 : unsigned_type_for (type
);
713 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
714 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
715 return fold_convert_loc (loc
, type
, temp
);
727 /* A wrapper for fold_negate_expr_1. */
730 fold_negate_expr (location_t loc
, tree t
)
732 tree type
= TREE_TYPE (t
);
734 tree tem
= fold_negate_expr_1 (loc
, t
);
735 if (tem
== NULL_TREE
)
737 return fold_convert_loc (loc
, type
, tem
);
740 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
741 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
753 loc
= EXPR_LOCATION (t
);
754 type
= TREE_TYPE (t
);
757 tem
= fold_negate_expr (loc
, t
);
759 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
760 return fold_convert_loc (loc
, type
, tem
);
763 /* Split a tree IN into a constant, literal and variable parts that could be
764 combined with CODE to make IN. "constant" means an expression with
765 TREE_CONSTANT but that isn't an actual constant. CODE must be a
766 commutative arithmetic operation. Store the constant part into *CONP,
767 the literal in *LITP and return the variable part. If a part isn't
768 present, set it to null. If the tree does not decompose in this way,
769 return the entire tree as the variable part and the other parts as null.
771 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
772 case, we negate an operand that was subtracted. Except if it is a
773 literal for which we use *MINUS_LITP instead.
775 If NEGATE_P is true, we are negating all of IN, again except a literal
776 for which we use *MINUS_LITP instead. If a variable part is of pointer
777 type, it is negated after converting to TYPE. This prevents us from
778 generating illegal MINUS pointer expression. LOC is the location of
779 the converted variable part.
781 If IN is itself a literal or constant, return it as appropriate.
783 Note that we do not guarantee that any of the three values will be the
784 same type as IN, but they will have the same signedness and mode. */
787 split_tree (tree in
, tree type
, enum tree_code code
,
788 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
789 tree
*litp
, tree
*minus_litp
, int negate_p
)
798 /* Strip any conversions that don't change the machine mode or signedness. */
799 STRIP_SIGN_NOPS (in
);
801 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
802 || TREE_CODE (in
) == FIXED_CST
)
804 else if (TREE_CODE (in
) == code
805 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
806 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
807 /* We can associate addition and subtraction together (even
808 though the C standard doesn't say so) for integers because
809 the value is not affected. For reals, the value might be
810 affected, so we can't. */
811 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
812 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
813 || (code
== MINUS_EXPR
814 && (TREE_CODE (in
) == PLUS_EXPR
815 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
817 tree op0
= TREE_OPERAND (in
, 0);
818 tree op1
= TREE_OPERAND (in
, 1);
819 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
820 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
822 /* First see if either of the operands is a literal, then a constant. */
823 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
824 || TREE_CODE (op0
) == FIXED_CST
)
825 *litp
= op0
, op0
= 0;
826 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
827 || TREE_CODE (op1
) == FIXED_CST
)
828 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
830 if (op0
!= 0 && TREE_CONSTANT (op0
))
831 *conp
= op0
, op0
= 0;
832 else if (op1
!= 0 && TREE_CONSTANT (op1
))
833 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
835 /* If we haven't dealt with either operand, this is not a case we can
836 decompose. Otherwise, VAR is either of the ones remaining, if any. */
837 if (op0
!= 0 && op1
!= 0)
842 var
= op1
, neg_var_p
= neg1_p
;
844 /* Now do any needed negations. */
846 *minus_litp
= *litp
, *litp
= 0;
847 if (neg_conp_p
&& *conp
)
848 *minus_conp
= *conp
, *conp
= 0;
849 if (neg_var_p
&& var
)
850 *minus_varp
= var
, var
= 0;
852 else if (TREE_CONSTANT (in
))
854 else if (TREE_CODE (in
) == BIT_NOT_EXPR
855 && code
== PLUS_EXPR
)
857 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
858 when IN is constant. */
859 *litp
= build_minus_one_cst (type
);
860 *minus_varp
= TREE_OPERAND (in
, 0);
868 *minus_litp
= *litp
, *litp
= 0;
869 else if (*minus_litp
)
870 *litp
= *minus_litp
, *minus_litp
= 0;
872 *minus_conp
= *conp
, *conp
= 0;
873 else if (*minus_conp
)
874 *conp
= *minus_conp
, *minus_conp
= 0;
876 *minus_varp
= var
, var
= 0;
877 else if (*minus_varp
)
878 var
= *minus_varp
, *minus_varp
= 0;
882 && TREE_OVERFLOW_P (*litp
))
883 *litp
= drop_tree_overflow (*litp
);
885 && TREE_OVERFLOW_P (*minus_litp
))
886 *minus_litp
= drop_tree_overflow (*minus_litp
);
891 /* Re-associate trees split by the above function. T1 and T2 are
892 either expressions to associate or null. Return the new
893 expression, if any. LOC is the location of the new expression. If
894 we build an operation, do it in TYPE and with CODE. */
897 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
901 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
907 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
908 try to fold this since we will have infinite recursion. But do
909 deal with any NEGATE_EXPRs. */
910 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
911 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
912 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
914 if (code
== PLUS_EXPR
)
916 if (TREE_CODE (t1
) == NEGATE_EXPR
)
917 return build2_loc (loc
, MINUS_EXPR
, type
,
918 fold_convert_loc (loc
, type
, t2
),
919 fold_convert_loc (loc
, type
,
920 TREE_OPERAND (t1
, 0)));
921 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
922 return build2_loc (loc
, MINUS_EXPR
, type
,
923 fold_convert_loc (loc
, type
, t1
),
924 fold_convert_loc (loc
, type
,
925 TREE_OPERAND (t2
, 0)));
926 else if (integer_zerop (t2
))
927 return fold_convert_loc (loc
, type
, t1
);
929 else if (code
== MINUS_EXPR
)
931 if (integer_zerop (t2
))
932 return fold_convert_loc (loc
, type
, t1
);
935 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
936 fold_convert_loc (loc
, type
, t2
));
939 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
940 fold_convert_loc (loc
, type
, t2
));
943 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
944 for use in int_const_binop, size_binop and size_diffop. */
947 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
949 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
951 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
966 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
967 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
968 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
971 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
972 a new constant in RES. Return FALSE if we don't know how to
973 evaluate CODE at compile-time. */
976 wide_int_binop (wide_int
&res
,
977 enum tree_code code
, const wide_int
&arg1
, const wide_int
&arg2
,
978 signop sign
, wi::overflow_type
*overflow
)
981 *overflow
= wi::OVF_NONE
;
985 res
= wi::bit_or (arg1
, arg2
);
989 res
= wi::bit_xor (arg1
, arg2
);
993 res
= wi::bit_and (arg1
, arg2
);
997 if (wi::neg_p (arg2
))
999 res
= wi::lshift (arg1
, arg2
);
1003 if (wi::neg_p (arg2
))
1005 /* It's unclear from the C standard whether shifts can overflow.
1006 The following code ignores overflow; perhaps a C standard
1007 interpretation ruling is needed. */
1008 res
= wi::rshift (arg1
, arg2
, sign
);
1013 if (wi::neg_p (arg2
))
1016 if (code
== RROTATE_EXPR
)
1017 code
= LROTATE_EXPR
;
1019 code
= RROTATE_EXPR
;
1024 if (code
== RROTATE_EXPR
)
1025 res
= wi::rrotate (arg1
, tmp
);
1027 res
= wi::lrotate (arg1
, tmp
);
1031 res
= wi::add (arg1
, arg2
, sign
, overflow
);
1035 res
= wi::sub (arg1
, arg2
, sign
, overflow
);
1039 res
= wi::mul (arg1
, arg2
, sign
, overflow
);
1042 case MULT_HIGHPART_EXPR
:
1043 res
= wi::mul_high (arg1
, arg2
, sign
);
1046 case TRUNC_DIV_EXPR
:
1047 case EXACT_DIV_EXPR
:
1050 res
= wi::div_trunc (arg1
, arg2
, sign
, overflow
);
1053 case FLOOR_DIV_EXPR
:
1056 res
= wi::div_floor (arg1
, arg2
, sign
, overflow
);
1062 res
= wi::div_ceil (arg1
, arg2
, sign
, overflow
);
1065 case ROUND_DIV_EXPR
:
1068 res
= wi::div_round (arg1
, arg2
, sign
, overflow
);
1071 case TRUNC_MOD_EXPR
:
1074 res
= wi::mod_trunc (arg1
, arg2
, sign
, overflow
);
1077 case FLOOR_MOD_EXPR
:
1080 res
= wi::mod_floor (arg1
, arg2
, sign
, overflow
);
1086 res
= wi::mod_ceil (arg1
, arg2
, sign
, overflow
);
1089 case ROUND_MOD_EXPR
:
1092 res
= wi::mod_round (arg1
, arg2
, sign
, overflow
);
1096 res
= wi::min (arg1
, arg2
, sign
);
1100 res
= wi::max (arg1
, arg2
, sign
);
1109 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1110 produce a new constant in RES. Return FALSE if we don't know how
1111 to evaluate CODE at compile-time. */
1114 poly_int_binop (poly_wide_int
&res
, enum tree_code code
,
1115 const_tree arg1
, const_tree arg2
,
1116 signop sign
, wi::overflow_type
*overflow
)
1118 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1119 gcc_assert (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
));
1123 res
= wi::add (wi::to_poly_wide (arg1
),
1124 wi::to_poly_wide (arg2
), sign
, overflow
);
1128 res
= wi::sub (wi::to_poly_wide (arg1
),
1129 wi::to_poly_wide (arg2
), sign
, overflow
);
1133 if (TREE_CODE (arg2
) == INTEGER_CST
)
1134 res
= wi::mul (wi::to_poly_wide (arg1
),
1135 wi::to_wide (arg2
), sign
, overflow
);
1136 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1137 res
= wi::mul (wi::to_poly_wide (arg2
),
1138 wi::to_wide (arg1
), sign
, overflow
);
1144 if (TREE_CODE (arg2
) == INTEGER_CST
)
1145 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1151 if (TREE_CODE (arg2
) != INTEGER_CST
1152 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1163 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1164 produce a new constant. Return NULL_TREE if we don't know how to
1165 evaluate CODE at compile-time. */
1168 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1171 poly_wide_int poly_res
;
1172 tree type
= TREE_TYPE (arg1
);
1173 signop sign
= TYPE_SIGN (type
);
1174 wi::overflow_type overflow
= wi::OVF_NONE
;
1176 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1178 wide_int warg1
= wi::to_wide (arg1
), res
;
1179 wide_int warg2
= wi::to_wide (arg2
, TYPE_PRECISION (type
));
1180 if (!wide_int_binop (res
, code
, warg1
, warg2
, sign
, &overflow
))
1184 else if (!poly_int_tree_p (arg1
)
1185 || !poly_int_tree_p (arg2
)
1186 || !poly_int_binop (poly_res
, code
, arg1
, arg2
, sign
, &overflow
))
1188 return force_fit_type (type
, poly_res
, overflowable
,
1189 (((sign
== SIGNED
|| overflowable
== -1)
1191 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1194 /* Return true if binary operation OP distributes over addition in operand
1195 OPNO, with the other operand being held constant. OPNO counts from 1. */
1198 distributes_over_addition_p (tree_code op
, int opno
)
1215 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1216 constant. We assume ARG1 and ARG2 have the same data type, or at least
1217 are the same kind of constant and the same machine mode. Return zero if
1218 combining the constants is not allowed in the current operating mode. */
1221 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1223 /* Sanity check for the recursive cases. */
1230 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1232 if (code
== POINTER_PLUS_EXPR
)
1233 return int_const_binop (PLUS_EXPR
,
1234 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1236 return int_const_binop (code
, arg1
, arg2
);
1239 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1244 REAL_VALUE_TYPE value
;
1245 REAL_VALUE_TYPE result
;
1249 /* The following codes are handled by real_arithmetic. */
1264 d1
= TREE_REAL_CST (arg1
);
1265 d2
= TREE_REAL_CST (arg2
);
1267 type
= TREE_TYPE (arg1
);
1268 mode
= TYPE_MODE (type
);
1270 /* Don't perform operation if we honor signaling NaNs and
1271 either operand is a signaling NaN. */
1272 if (HONOR_SNANS (mode
)
1273 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1274 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1277 /* Don't perform operation if it would raise a division
1278 by zero exception. */
1279 if (code
== RDIV_EXPR
1280 && real_equal (&d2
, &dconst0
)
1281 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1284 /* If either operand is a NaN, just return it. Otherwise, set up
1285 for floating-point trap; we return an overflow. */
1286 if (REAL_VALUE_ISNAN (d1
))
1288 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1291 t
= build_real (type
, d1
);
1294 else if (REAL_VALUE_ISNAN (d2
))
1296 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1299 t
= build_real (type
, d2
);
1303 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1304 real_convert (&result
, mode
, &value
);
1306 /* Don't constant fold this floating point operation if
1307 the result has overflowed and flag_trapping_math. */
1308 if (flag_trapping_math
1309 && MODE_HAS_INFINITIES (mode
)
1310 && REAL_VALUE_ISINF (result
)
1311 && !REAL_VALUE_ISINF (d1
)
1312 && !REAL_VALUE_ISINF (d2
))
1315 /* Don't constant fold this floating point operation if the
1316 result may dependent upon the run-time rounding mode and
1317 flag_rounding_math is set, or if GCC's software emulation
1318 is unable to accurately represent the result. */
1319 if ((flag_rounding_math
1320 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1321 && (inexact
|| !real_identical (&result
, &value
)))
1324 t
= build_real (type
, result
);
1326 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1330 if (TREE_CODE (arg1
) == FIXED_CST
)
1332 FIXED_VALUE_TYPE f1
;
1333 FIXED_VALUE_TYPE f2
;
1334 FIXED_VALUE_TYPE result
;
1339 /* The following codes are handled by fixed_arithmetic. */
1345 case TRUNC_DIV_EXPR
:
1346 if (TREE_CODE (arg2
) != FIXED_CST
)
1348 f2
= TREE_FIXED_CST (arg2
);
1354 if (TREE_CODE (arg2
) != INTEGER_CST
)
1356 wi::tree_to_wide_ref w2
= wi::to_wide (arg2
);
1357 f2
.data
.high
= w2
.elt (1);
1358 f2
.data
.low
= w2
.ulow ();
1367 f1
= TREE_FIXED_CST (arg1
);
1368 type
= TREE_TYPE (arg1
);
1369 sat_p
= TYPE_SATURATING (type
);
1370 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1371 t
= build_fixed (type
, result
);
1372 /* Propagate overflow flags. */
1373 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1374 TREE_OVERFLOW (t
) = 1;
1378 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1380 tree type
= TREE_TYPE (arg1
);
1381 tree r1
= TREE_REALPART (arg1
);
1382 tree i1
= TREE_IMAGPART (arg1
);
1383 tree r2
= TREE_REALPART (arg2
);
1384 tree i2
= TREE_IMAGPART (arg2
);
1391 real
= const_binop (code
, r1
, r2
);
1392 imag
= const_binop (code
, i1
, i2
);
1396 if (COMPLEX_FLOAT_TYPE_P (type
))
1397 return do_mpc_arg2 (arg1
, arg2
, type
,
1398 /* do_nonfinite= */ folding_initializer
,
1401 real
= const_binop (MINUS_EXPR
,
1402 const_binop (MULT_EXPR
, r1
, r2
),
1403 const_binop (MULT_EXPR
, i1
, i2
));
1404 imag
= const_binop (PLUS_EXPR
,
1405 const_binop (MULT_EXPR
, r1
, i2
),
1406 const_binop (MULT_EXPR
, i1
, r2
));
1410 if (COMPLEX_FLOAT_TYPE_P (type
))
1411 return do_mpc_arg2 (arg1
, arg2
, type
,
1412 /* do_nonfinite= */ folding_initializer
,
1415 case TRUNC_DIV_EXPR
:
1417 case FLOOR_DIV_EXPR
:
1418 case ROUND_DIV_EXPR
:
1419 if (flag_complex_method
== 0)
1421 /* Keep this algorithm in sync with
1422 tree-complex.c:expand_complex_div_straight().
1424 Expand complex division to scalars, straightforward algorithm.
1425 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1429 = const_binop (PLUS_EXPR
,
1430 const_binop (MULT_EXPR
, r2
, r2
),
1431 const_binop (MULT_EXPR
, i2
, i2
));
1433 = const_binop (PLUS_EXPR
,
1434 const_binop (MULT_EXPR
, r1
, r2
),
1435 const_binop (MULT_EXPR
, i1
, i2
));
1437 = const_binop (MINUS_EXPR
,
1438 const_binop (MULT_EXPR
, i1
, r2
),
1439 const_binop (MULT_EXPR
, r1
, i2
));
1441 real
= const_binop (code
, t1
, magsquared
);
1442 imag
= const_binop (code
, t2
, magsquared
);
1446 /* Keep this algorithm in sync with
1447 tree-complex.c:expand_complex_div_wide().
1449 Expand complex division to scalars, modified algorithm to minimize
1450 overflow with wide input ranges. */
1451 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1452 fold_abs_const (r2
, TREE_TYPE (type
)),
1453 fold_abs_const (i2
, TREE_TYPE (type
)));
1455 if (integer_nonzerop (compare
))
1457 /* In the TRUE branch, we compute
1459 div = (br * ratio) + bi;
1460 tr = (ar * ratio) + ai;
1461 ti = (ai * ratio) - ar;
1464 tree ratio
= const_binop (code
, r2
, i2
);
1465 tree div
= const_binop (PLUS_EXPR
, i2
,
1466 const_binop (MULT_EXPR
, r2
, ratio
));
1467 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1468 real
= const_binop (PLUS_EXPR
, real
, i1
);
1469 real
= const_binop (code
, real
, div
);
1471 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1472 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1473 imag
= const_binop (code
, imag
, div
);
1477 /* In the FALSE branch, we compute
1479 divisor = (d * ratio) + c;
1480 tr = (b * ratio) + a;
1481 ti = b - (a * ratio);
1484 tree ratio
= const_binop (code
, i2
, r2
);
1485 tree div
= const_binop (PLUS_EXPR
, r2
,
1486 const_binop (MULT_EXPR
, i2
, ratio
));
1488 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1489 real
= const_binop (PLUS_EXPR
, real
, r1
);
1490 real
= const_binop (code
, real
, div
);
1492 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1493 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1494 imag
= const_binop (code
, imag
, div
);
1504 return build_complex (type
, real
, imag
);
1507 if (TREE_CODE (arg1
) == VECTOR_CST
1508 && TREE_CODE (arg2
) == VECTOR_CST
1509 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)),
1510 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
))))
1512 tree type
= TREE_TYPE (arg1
);
1514 if (VECTOR_CST_STEPPED_P (arg1
)
1515 && VECTOR_CST_STEPPED_P (arg2
))
1516 /* We can operate directly on the encoding if:
1518 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1520 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1522 Addition and subtraction are the supported operators
1523 for which this is true. */
1524 step_ok_p
= (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
1525 else if (VECTOR_CST_STEPPED_P (arg1
))
1526 /* We can operate directly on stepped encodings if:
1530 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1532 which is true if (x -> x op c) distributes over addition. */
1533 step_ok_p
= distributes_over_addition_p (code
, 1);
1535 /* Similarly in reverse. */
1536 step_ok_p
= distributes_over_addition_p (code
, 2);
1537 tree_vector_builder elts
;
1538 if (!elts
.new_binary_operation (type
, arg1
, arg2
, step_ok_p
))
1540 unsigned int count
= elts
.encoded_nelts ();
1541 for (unsigned int i
= 0; i
< count
; ++i
)
1543 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1544 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1546 tree elt
= const_binop (code
, elem1
, elem2
);
1548 /* It is possible that const_binop cannot handle the given
1549 code and return NULL_TREE */
1550 if (elt
== NULL_TREE
)
1552 elts
.quick_push (elt
);
1555 return elts
.build ();
1558 /* Shifts allow a scalar offset for a vector. */
1559 if (TREE_CODE (arg1
) == VECTOR_CST
1560 && TREE_CODE (arg2
) == INTEGER_CST
)
1562 tree type
= TREE_TYPE (arg1
);
1563 bool step_ok_p
= distributes_over_addition_p (code
, 1);
1564 tree_vector_builder elts
;
1565 if (!elts
.new_unary_operation (type
, arg1
, step_ok_p
))
1567 unsigned int count
= elts
.encoded_nelts ();
1568 for (unsigned int i
= 0; i
< count
; ++i
)
1570 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1572 tree elt
= const_binop (code
, elem1
, arg2
);
1574 /* It is possible that const_binop cannot handle the given
1575 code and return NULL_TREE. */
1576 if (elt
== NULL_TREE
)
1578 elts
.quick_push (elt
);
1581 return elts
.build ();
1586 /* Overload that adds a TYPE parameter to be able to dispatch
1587 to fold_relational_const. */
1590 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1592 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1593 return fold_relational_const (code
, type
, arg1
, arg2
);
1595 /* ??? Until we make the const_binop worker take the type of the
1596 result as argument put those cases that need it here. */
1599 case VEC_SERIES_EXPR
:
1600 if (CONSTANT_CLASS_P (arg1
)
1601 && CONSTANT_CLASS_P (arg2
))
1602 return build_vec_series (type
, arg1
, arg2
);
1606 if ((TREE_CODE (arg1
) == REAL_CST
1607 && TREE_CODE (arg2
) == REAL_CST
)
1608 || (TREE_CODE (arg1
) == INTEGER_CST
1609 && TREE_CODE (arg2
) == INTEGER_CST
))
1610 return build_complex (type
, arg1
, arg2
);
1613 case POINTER_DIFF_EXPR
:
1614 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1616 poly_offset_int res
= (wi::to_poly_offset (arg1
)
1617 - wi::to_poly_offset (arg2
));
1618 return force_fit_type (type
, res
, 1,
1619 TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1623 case VEC_PACK_TRUNC_EXPR
:
1624 case VEC_PACK_FIX_TRUNC_EXPR
:
1625 case VEC_PACK_FLOAT_EXPR
:
1627 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1629 if (TREE_CODE (arg1
) != VECTOR_CST
1630 || TREE_CODE (arg2
) != VECTOR_CST
)
1633 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1636 out_nelts
= in_nelts
* 2;
1637 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1638 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1640 tree_vector_builder
elts (type
, out_nelts
, 1);
1641 for (i
= 0; i
< out_nelts
; i
++)
1643 tree elt
= (i
< in_nelts
1644 ? VECTOR_CST_ELT (arg1
, i
)
1645 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1646 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1648 : code
== VEC_PACK_FLOAT_EXPR
1649 ? FLOAT_EXPR
: FIX_TRUNC_EXPR
,
1650 TREE_TYPE (type
), elt
);
1651 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1653 elts
.quick_push (elt
);
1656 return elts
.build ();
1659 case VEC_WIDEN_MULT_LO_EXPR
:
1660 case VEC_WIDEN_MULT_HI_EXPR
:
1661 case VEC_WIDEN_MULT_EVEN_EXPR
:
1662 case VEC_WIDEN_MULT_ODD_EXPR
:
1664 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1666 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1669 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1671 out_nelts
= in_nelts
/ 2;
1672 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1673 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1675 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1676 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1677 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1678 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1679 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1681 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1684 tree_vector_builder
elts (type
, out_nelts
, 1);
1685 for (out
= 0; out
< out_nelts
; out
++)
1687 unsigned int in
= (out
<< scale
) + ofs
;
1688 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1689 VECTOR_CST_ELT (arg1
, in
));
1690 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1691 VECTOR_CST_ELT (arg2
, in
));
1693 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1695 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1696 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1698 elts
.quick_push (elt
);
1701 return elts
.build ();
1707 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1710 /* Make sure type and arg0 have the same saturating flag. */
1711 gcc_checking_assert (TYPE_SATURATING (type
)
1712 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1714 return const_binop (code
, arg1
, arg2
);
1717 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1718 Return zero if computing the constants is not possible. */
1721 const_unop (enum tree_code code
, tree type
, tree arg0
)
1723 /* Don't perform the operation, other than NEGATE and ABS, if
1724 flag_signaling_nans is on and the operand is a signaling NaN. */
1725 if (TREE_CODE (arg0
) == REAL_CST
1726 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1727 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1728 && code
!= NEGATE_EXPR
1730 && code
!= ABSU_EXPR
)
1737 case FIX_TRUNC_EXPR
:
1738 case FIXED_CONVERT_EXPR
:
1739 return fold_convert_const (code
, type
, arg0
);
1741 case ADDR_SPACE_CONVERT_EXPR
:
1742 /* If the source address is 0, and the source address space
1743 cannot have a valid object at 0, fold to dest type null. */
1744 if (integer_zerop (arg0
)
1745 && !(targetm
.addr_space
.zero_address_valid
1746 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1747 return fold_convert_const (code
, type
, arg0
);
1750 case VIEW_CONVERT_EXPR
:
1751 return fold_view_convert_expr (type
, arg0
);
1755 /* Can't call fold_negate_const directly here as that doesn't
1756 handle all cases and we might not be able to negate some
1758 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1759 if (tem
&& CONSTANT_CLASS_P (tem
))
1766 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1767 return fold_abs_const (arg0
, type
);
1771 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1773 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1775 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1780 if (TREE_CODE (arg0
) == INTEGER_CST
)
1781 return fold_not_const (arg0
, type
);
1782 else if (POLY_INT_CST_P (arg0
))
1783 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1784 /* Perform BIT_NOT_EXPR on each element individually. */
1785 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1789 /* This can cope with stepped encodings because ~x == -1 - x. */
1790 tree_vector_builder elements
;
1791 elements
.new_unary_operation (type
, arg0
, true);
1792 unsigned int i
, count
= elements
.encoded_nelts ();
1793 for (i
= 0; i
< count
; ++i
)
1795 elem
= VECTOR_CST_ELT (arg0
, i
);
1796 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1797 if (elem
== NULL_TREE
)
1799 elements
.quick_push (elem
);
1802 return elements
.build ();
1806 case TRUTH_NOT_EXPR
:
1807 if (TREE_CODE (arg0
) == INTEGER_CST
)
1808 return constant_boolean_node (integer_zerop (arg0
), type
);
1812 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1813 return fold_convert (type
, TREE_REALPART (arg0
));
1817 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1818 return fold_convert (type
, TREE_IMAGPART (arg0
));
1821 case VEC_UNPACK_LO_EXPR
:
1822 case VEC_UNPACK_HI_EXPR
:
1823 case VEC_UNPACK_FLOAT_LO_EXPR
:
1824 case VEC_UNPACK_FLOAT_HI_EXPR
:
1825 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
1826 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
1828 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1829 enum tree_code subcode
;
1831 if (TREE_CODE (arg0
) != VECTOR_CST
)
1834 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1836 out_nelts
= in_nelts
/ 2;
1837 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1839 unsigned int offset
= 0;
1840 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1841 || code
== VEC_UNPACK_FLOAT_LO_EXPR
1842 || code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
))
1845 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1847 else if (code
== VEC_UNPACK_FLOAT_LO_EXPR
1848 || code
== VEC_UNPACK_FLOAT_HI_EXPR
)
1849 subcode
= FLOAT_EXPR
;
1851 subcode
= FIX_TRUNC_EXPR
;
1853 tree_vector_builder
elts (type
, out_nelts
, 1);
1854 for (i
= 0; i
< out_nelts
; i
++)
1856 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1857 VECTOR_CST_ELT (arg0
, i
+ offset
));
1858 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1860 elts
.quick_push (elt
);
1863 return elts
.build ();
1866 case VEC_DUPLICATE_EXPR
:
1867 if (CONSTANT_CLASS_P (arg0
))
1868 return build_vector_from_val (type
, arg0
);
1878 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1879 indicates which particular sizetype to create. */
1882 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
1884 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1887 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1888 is a tree code. The type of the result is taken from the operands.
1889 Both must be equivalent integer types, ala int_binop_types_match_p.
1890 If the operands are constant, so is the result. */
1893 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1895 tree type
= TREE_TYPE (arg0
);
1897 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1898 return error_mark_node
;
1900 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1903 /* Handle the special case of two poly_int constants faster. */
1904 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
1906 /* And some specific cases even faster than that. */
1907 if (code
== PLUS_EXPR
)
1909 if (integer_zerop (arg0
)
1910 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1912 if (integer_zerop (arg1
)
1913 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1916 else if (code
== MINUS_EXPR
)
1918 if (integer_zerop (arg1
)
1919 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
1922 else if (code
== MULT_EXPR
)
1924 if (integer_onep (arg0
)
1925 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
1929 /* Handle general case of two integer constants. For sizetype
1930 constant calculations we always want to know about overflow,
1931 even in the unsigned case. */
1932 tree res
= int_const_binop (code
, arg0
, arg1
, -1);
1933 if (res
!= NULL_TREE
)
1937 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1940 /* Given two values, either both of sizetype or both of bitsizetype,
1941 compute the difference between the two values. Return the value
1942 in signed type corresponding to the type of the operands. */
1945 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1947 tree type
= TREE_TYPE (arg0
);
1950 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1953 /* If the type is already signed, just do the simple thing. */
1954 if (!TYPE_UNSIGNED (type
))
1955 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1957 if (type
== sizetype
)
1959 else if (type
== bitsizetype
)
1960 ctype
= sbitsizetype
;
1962 ctype
= signed_type_for (type
);
1964 /* If either operand is not a constant, do the conversions to the signed
1965 type and subtract. The hardware will do the right thing with any
1966 overflow in the subtraction. */
1967 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1968 return size_binop_loc (loc
, MINUS_EXPR
,
1969 fold_convert_loc (loc
, ctype
, arg0
),
1970 fold_convert_loc (loc
, ctype
, arg1
));
1972 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1973 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1974 overflow) and negate (which can't either). Special-case a result
1975 of zero while we're here. */
1976 if (tree_int_cst_equal (arg0
, arg1
))
1977 return build_int_cst (ctype
, 0);
1978 else if (tree_int_cst_lt (arg1
, arg0
))
1979 return fold_convert_loc (loc
, ctype
,
1980 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1982 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1983 fold_convert_loc (loc
, ctype
,
1984 size_binop_loc (loc
,
1989 /* A subroutine of fold_convert_const handling conversions of an
1990 INTEGER_CST to another integer type. */
1993 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1995 /* Given an integer constant, make new constant with new type,
1996 appropriately sign-extended or truncated. Use widest_int
1997 so that any extension is done according ARG1's type. */
1998 return force_fit_type (type
, wi::to_widest (arg1
),
1999 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
2000 TREE_OVERFLOW (arg1
));
2003 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2004 to an integer type. */
2007 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
2009 bool overflow
= false;
2012 /* The following code implements the floating point to integer
2013 conversion rules required by the Java Language Specification,
2014 that IEEE NaNs are mapped to zero and values that overflow
2015 the target precision saturate, i.e. values greater than
2016 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2017 are mapped to INT_MIN. These semantics are allowed by the
2018 C and C++ standards that simply state that the behavior of
2019 FP-to-integer conversion is unspecified upon overflow. */
2023 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2027 case FIX_TRUNC_EXPR
:
2028 real_trunc (&r
, VOIDmode
, &x
);
2035 /* If R is NaN, return zero and show we have an overflow. */
2036 if (REAL_VALUE_ISNAN (r
))
2039 val
= wi::zero (TYPE_PRECISION (type
));
2042 /* See if R is less than the lower bound or greater than the
2047 tree lt
= TYPE_MIN_VALUE (type
);
2048 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2049 if (real_less (&r
, &l
))
2052 val
= wi::to_wide (lt
);
2058 tree ut
= TYPE_MAX_VALUE (type
);
2061 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2062 if (real_less (&u
, &r
))
2065 val
= wi::to_wide (ut
);
2071 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2073 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2077 /* A subroutine of fold_convert_const handling conversions of a
2078 FIXED_CST to an integer type. */
2081 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2084 double_int temp
, temp_trunc
;
2087 /* Right shift FIXED_CST to temp by fbit. */
2088 temp
= TREE_FIXED_CST (arg1
).data
;
2089 mode
= TREE_FIXED_CST (arg1
).mode
;
2090 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2092 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2093 HOST_BITS_PER_DOUBLE_INT
,
2094 SIGNED_FIXED_POINT_MODE_P (mode
));
2096 /* Left shift temp to temp_trunc by fbit. */
2097 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2098 HOST_BITS_PER_DOUBLE_INT
,
2099 SIGNED_FIXED_POINT_MODE_P (mode
));
2103 temp
= double_int_zero
;
2104 temp_trunc
= double_int_zero
;
2107 /* If FIXED_CST is negative, we need to round the value toward 0.
2108 By checking if the fractional bits are not zero to add 1 to temp. */
2109 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2110 && temp_trunc
.is_negative ()
2111 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2112 temp
+= double_int_one
;
2114 /* Given a fixed-point constant, make new constant with new type,
2115 appropriately sign-extended or truncated. */
2116 t
= force_fit_type (type
, temp
, -1,
2117 (temp
.is_negative ()
2118 && (TYPE_UNSIGNED (type
)
2119 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2120 | TREE_OVERFLOW (arg1
));
2125 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2126 to another floating point type. */
2129 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2131 REAL_VALUE_TYPE value
;
2134 /* Don't perform the operation if flag_signaling_nans is on
2135 and the operand is a signaling NaN. */
2136 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
2137 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2140 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2141 t
= build_real (type
, value
);
2143 /* If converting an infinity or NAN to a representation that doesn't
2144 have one, set the overflow bit so that we can produce some kind of
2145 error message at the appropriate point if necessary. It's not the
2146 most user-friendly message, but it's better than nothing. */
2147 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2148 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2149 TREE_OVERFLOW (t
) = 1;
2150 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2151 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2152 TREE_OVERFLOW (t
) = 1;
2153 /* Regular overflow, conversion produced an infinity in a mode that
2154 can't represent them. */
2155 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2156 && REAL_VALUE_ISINF (value
)
2157 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2158 TREE_OVERFLOW (t
) = 1;
2160 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2164 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2165 to a floating point type. */
2168 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2170 REAL_VALUE_TYPE value
;
2173 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2174 &TREE_FIXED_CST (arg1
));
2175 t
= build_real (type
, value
);
2177 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2181 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2182 to another fixed-point type. */
2185 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2187 FIXED_VALUE_TYPE value
;
2191 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2192 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2193 t
= build_fixed (type
, value
);
2195 /* Propagate overflow flags. */
2196 if (overflow_p
| TREE_OVERFLOW (arg1
))
2197 TREE_OVERFLOW (t
) = 1;
2201 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2202 to a fixed-point type. */
2205 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2207 FIXED_VALUE_TYPE value
;
2212 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2214 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2215 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2216 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2218 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2220 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2221 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2222 TYPE_SATURATING (type
));
2223 t
= build_fixed (type
, value
);
2225 /* Propagate overflow flags. */
2226 if (overflow_p
| TREE_OVERFLOW (arg1
))
2227 TREE_OVERFLOW (t
) = 1;
2231 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2232 to a fixed-point type. */
2235 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2237 FIXED_VALUE_TYPE value
;
2241 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2242 &TREE_REAL_CST (arg1
),
2243 TYPE_SATURATING (type
));
2244 t
= build_fixed (type
, value
);
2246 /* Propagate overflow flags. */
2247 if (overflow_p
| TREE_OVERFLOW (arg1
))
2248 TREE_OVERFLOW (t
) = 1;
2252 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2253 type TYPE. If no simplification can be done return NULL_TREE. */
2256 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2258 tree arg_type
= TREE_TYPE (arg1
);
2259 if (arg_type
== type
)
2262 /* We can't widen types, since the runtime value could overflow the
2263 original type before being extended to the new type. */
2264 if (POLY_INT_CST_P (arg1
)
2265 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2266 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2267 return build_poly_int_cst (type
,
2268 poly_wide_int::from (poly_int_cst_value (arg1
),
2269 TYPE_PRECISION (type
),
2270 TYPE_SIGN (arg_type
)));
2272 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2273 || TREE_CODE (type
) == OFFSET_TYPE
)
2275 if (TREE_CODE (arg1
) == INTEGER_CST
)
2276 return fold_convert_const_int_from_int (type
, arg1
);
2277 else if (TREE_CODE (arg1
) == REAL_CST
)
2278 return fold_convert_const_int_from_real (code
, type
, arg1
);
2279 else if (TREE_CODE (arg1
) == FIXED_CST
)
2280 return fold_convert_const_int_from_fixed (type
, arg1
);
2282 else if (TREE_CODE (type
) == REAL_TYPE
)
2284 if (TREE_CODE (arg1
) == INTEGER_CST
)
2285 return build_real_from_int_cst (type
, arg1
);
2286 else if (TREE_CODE (arg1
) == REAL_CST
)
2287 return fold_convert_const_real_from_real (type
, arg1
);
2288 else if (TREE_CODE (arg1
) == FIXED_CST
)
2289 return fold_convert_const_real_from_fixed (type
, arg1
);
2291 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2293 if (TREE_CODE (arg1
) == FIXED_CST
)
2294 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2295 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2296 return fold_convert_const_fixed_from_int (type
, arg1
);
2297 else if (TREE_CODE (arg1
) == REAL_CST
)
2298 return fold_convert_const_fixed_from_real (type
, arg1
);
2300 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2302 if (TREE_CODE (arg1
) == VECTOR_CST
2303 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2305 tree elttype
= TREE_TYPE (type
);
2306 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2307 /* We can't handle steps directly when extending, since the
2308 values need to wrap at the original precision first. */
2310 = (INTEGRAL_TYPE_P (elttype
)
2311 && INTEGRAL_TYPE_P (arg1_elttype
)
2312 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2313 tree_vector_builder v
;
2314 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2316 unsigned int len
= v
.encoded_nelts ();
2317 for (unsigned int i
= 0; i
< len
; ++i
)
2319 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2320 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2321 if (cvt
== NULL_TREE
)
2331 /* Construct a vector of zero elements of vector type TYPE. */
2334 build_zero_vector (tree type
)
2338 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2339 return build_vector_from_val (type
, t
);
2342 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2345 fold_convertible_p (const_tree type
, const_tree arg
)
2347 tree orig
= TREE_TYPE (arg
);
2352 if (TREE_CODE (arg
) == ERROR_MARK
2353 || TREE_CODE (type
) == ERROR_MARK
2354 || TREE_CODE (orig
) == ERROR_MARK
)
2357 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2360 switch (TREE_CODE (type
))
2362 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2363 case POINTER_TYPE
: case REFERENCE_TYPE
:
2365 return (INTEGRAL_TYPE_P (orig
)
2366 || (POINTER_TYPE_P (orig
)
2367 && TYPE_PRECISION (type
) <= TYPE_PRECISION (orig
))
2368 || TREE_CODE (orig
) == OFFSET_TYPE
);
2371 case FIXED_POINT_TYPE
:
2373 return TREE_CODE (type
) == TREE_CODE (orig
);
2376 return (VECTOR_TYPE_P (orig
)
2377 && known_eq (TYPE_VECTOR_SUBPARTS (type
),
2378 TYPE_VECTOR_SUBPARTS (orig
))
2379 && fold_convertible_p (TREE_TYPE (type
), TREE_TYPE (orig
)));
2386 /* Convert expression ARG to type TYPE. Used by the middle-end for
2387 simple conversions in preference to calling the front-end's convert. */
2390 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2392 tree orig
= TREE_TYPE (arg
);
2398 if (TREE_CODE (arg
) == ERROR_MARK
2399 || TREE_CODE (type
) == ERROR_MARK
2400 || TREE_CODE (orig
) == ERROR_MARK
)
2401 return error_mark_node
;
2403 switch (TREE_CODE (type
))
2406 case REFERENCE_TYPE
:
2407 /* Handle conversions between pointers to different address spaces. */
2408 if (POINTER_TYPE_P (orig
)
2409 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2410 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2411 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2414 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2416 if (TREE_CODE (arg
) == INTEGER_CST
)
2418 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2419 if (tem
!= NULL_TREE
)
2422 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2423 || TREE_CODE (orig
) == OFFSET_TYPE
)
2424 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2425 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2426 return fold_convert_loc (loc
, type
,
2427 fold_build1_loc (loc
, REALPART_EXPR
,
2428 TREE_TYPE (orig
), arg
));
2429 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2430 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2431 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2434 if (TREE_CODE (arg
) == INTEGER_CST
)
2436 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2437 if (tem
!= NULL_TREE
)
2440 else if (TREE_CODE (arg
) == REAL_CST
)
2442 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2443 if (tem
!= NULL_TREE
)
2446 else if (TREE_CODE (arg
) == FIXED_CST
)
2448 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2449 if (tem
!= NULL_TREE
)
2453 switch (TREE_CODE (orig
))
2456 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2457 case POINTER_TYPE
: case REFERENCE_TYPE
:
2458 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2461 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2463 case FIXED_POINT_TYPE
:
2464 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2467 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2468 return fold_convert_loc (loc
, type
, tem
);
2474 case FIXED_POINT_TYPE
:
2475 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2476 || TREE_CODE (arg
) == REAL_CST
)
2478 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2479 if (tem
!= NULL_TREE
)
2480 goto fold_convert_exit
;
2483 switch (TREE_CODE (orig
))
2485 case FIXED_POINT_TYPE
:
2490 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2493 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2494 return fold_convert_loc (loc
, type
, tem
);
2501 switch (TREE_CODE (orig
))
2504 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2505 case POINTER_TYPE
: case REFERENCE_TYPE
:
2507 case FIXED_POINT_TYPE
:
2508 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2509 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2510 fold_convert_loc (loc
, TREE_TYPE (type
),
2511 integer_zero_node
));
2516 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2518 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2519 TREE_OPERAND (arg
, 0));
2520 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2521 TREE_OPERAND (arg
, 1));
2522 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2525 arg
= save_expr (arg
);
2526 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2527 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2528 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2529 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2530 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2538 if (integer_zerop (arg
))
2539 return build_zero_vector (type
);
2540 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2541 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2542 || TREE_CODE (orig
) == VECTOR_TYPE
);
2543 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2546 tem
= fold_ignored_result (arg
);
2547 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2550 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2551 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2555 protected_set_expr_location_unshare (tem
, loc
);
2559 /* Return false if expr can be assumed not to be an lvalue, true
2563 maybe_lvalue_p (const_tree x
)
2565 /* We only need to wrap lvalue tree codes. */
2566 switch (TREE_CODE (x
))
2579 case ARRAY_RANGE_REF
:
2585 case PREINCREMENT_EXPR
:
2586 case PREDECREMENT_EXPR
:
2588 case TRY_CATCH_EXPR
:
2589 case WITH_CLEANUP_EXPR
:
2595 case VIEW_CONVERT_EXPR
:
2599 /* Assume the worst for front-end tree codes. */
2600 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2608 /* Return an expr equal to X but certainly not valid as an lvalue. */
2611 non_lvalue_loc (location_t loc
, tree x
)
2613 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2618 if (! maybe_lvalue_p (x
))
2620 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2623 /* When pedantic, return an expr equal to X but certainly not valid as a
2624 pedantic lvalue. Otherwise, return X. */
2627 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2629 return protected_set_expr_location_unshare (x
, loc
);
2632 /* Given a tree comparison code, return the code that is the logical inverse.
2633 It is generally not safe to do this for floating-point comparisons, except
2634 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2635 ERROR_MARK in this case. */
2638 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2640 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2641 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2651 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2653 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2655 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2657 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2671 return UNORDERED_EXPR
;
2672 case UNORDERED_EXPR
:
2673 return ORDERED_EXPR
;
2679 /* Similar, but return the comparison that results if the operands are
2680 swapped. This is safe for floating-point. */
2683 swap_tree_comparison (enum tree_code code
)
2690 case UNORDERED_EXPR
:
2716 /* Convert a comparison tree code from an enum tree_code representation
2717 into a compcode bit-based encoding. This function is the inverse of
2718 compcode_to_comparison. */
2720 static enum comparison_code
2721 comparison_to_compcode (enum tree_code code
)
2738 return COMPCODE_ORD
;
2739 case UNORDERED_EXPR
:
2740 return COMPCODE_UNORD
;
2742 return COMPCODE_UNLT
;
2744 return COMPCODE_UNEQ
;
2746 return COMPCODE_UNLE
;
2748 return COMPCODE_UNGT
;
2750 return COMPCODE_LTGT
;
2752 return COMPCODE_UNGE
;
2758 /* Convert a compcode bit-based encoding of a comparison operator back
2759 to GCC's enum tree_code representation. This function is the
2760 inverse of comparison_to_compcode. */
2762 static enum tree_code
2763 compcode_to_comparison (enum comparison_code code
)
2780 return ORDERED_EXPR
;
2781 case COMPCODE_UNORD
:
2782 return UNORDERED_EXPR
;
2800 /* Return true if COND1 tests the opposite condition of COND2. */
2803 inverse_conditions_p (const_tree cond1
, const_tree cond2
)
2805 return (COMPARISON_CLASS_P (cond1
)
2806 && COMPARISON_CLASS_P (cond2
)
2807 && (invert_tree_comparison
2809 HONOR_NANS (TREE_OPERAND (cond1
, 0))) == TREE_CODE (cond2
))
2810 && operand_equal_p (TREE_OPERAND (cond1
, 0),
2811 TREE_OPERAND (cond2
, 0), 0)
2812 && operand_equal_p (TREE_OPERAND (cond1
, 1),
2813 TREE_OPERAND (cond2
, 1), 0));
2816 /* Return a tree for the comparison which is the combination of
2817 doing the AND or OR (depending on CODE) of the two operations LCODE
2818 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2819 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2820 if this makes the transformation invalid. */
2823 combine_comparisons (location_t loc
,
2824 enum tree_code code
, enum tree_code lcode
,
2825 enum tree_code rcode
, tree truth_type
,
2826 tree ll_arg
, tree lr_arg
)
2828 bool honor_nans
= HONOR_NANS (ll_arg
);
2829 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2830 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2835 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2836 compcode
= lcompcode
& rcompcode
;
2839 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2840 compcode
= lcompcode
| rcompcode
;
2849 /* Eliminate unordered comparisons, as well as LTGT and ORD
2850 which are not used unless the mode has NaNs. */
2851 compcode
&= ~COMPCODE_UNORD
;
2852 if (compcode
== COMPCODE_LTGT
)
2853 compcode
= COMPCODE_NE
;
2854 else if (compcode
== COMPCODE_ORD
)
2855 compcode
= COMPCODE_TRUE
;
2857 else if (flag_trapping_math
)
2859 /* Check that the original operation and the optimized ones will trap
2860 under the same condition. */
2861 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2862 && (lcompcode
!= COMPCODE_EQ
)
2863 && (lcompcode
!= COMPCODE_ORD
);
2864 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2865 && (rcompcode
!= COMPCODE_EQ
)
2866 && (rcompcode
!= COMPCODE_ORD
);
2867 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2868 && (compcode
!= COMPCODE_EQ
)
2869 && (compcode
!= COMPCODE_ORD
);
2871 /* In a short-circuited boolean expression the LHS might be
2872 such that the RHS, if evaluated, will never trap. For
2873 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2874 if neither x nor y is NaN. (This is a mixed blessing: for
2875 example, the expression above will never trap, hence
2876 optimizing it to x < y would be invalid). */
2877 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2878 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2881 /* If the comparison was short-circuited, and only the RHS
2882 trapped, we may now generate a spurious trap. */
2884 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2887 /* If we changed the conditions that cause a trap, we lose. */
2888 if ((ltrap
|| rtrap
) != trap
)
2892 if (compcode
== COMPCODE_TRUE
)
2893 return constant_boolean_node (true, truth_type
);
2894 else if (compcode
== COMPCODE_FALSE
)
2895 return constant_boolean_node (false, truth_type
);
2898 enum tree_code tcode
;
2900 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2901 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2905 /* Return nonzero if two operands (typically of the same tree node)
2906 are necessarily equal. FLAGS modifies behavior as follows:
2908 If OEP_ONLY_CONST is set, only return nonzero for constants.
2909 This function tests whether the operands are indistinguishable;
2910 it does not test whether they are equal using C's == operation.
2911 The distinction is important for IEEE floating point, because
2912 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2913 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2915 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2916 even though it may hold multiple values during a function.
2917 This is because a GCC tree node guarantees that nothing else is
2918 executed between the evaluation of its "operands" (which may often
2919 be evaluated in arbitrary order). Hence if the operands themselves
2920 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2921 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2922 unset means assuming isochronic (or instantaneous) tree equivalence.
2923 Unless comparing arbitrary expression trees, such as from different
2924 statements, this flag can usually be left unset.
2926 If OEP_PURE_SAME is set, then pure functions with identical arguments
2927 are considered the same. It is used when the caller has other ways
2928 to ensure that global memory is unchanged in between.
2930 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2931 not values of expressions.
2933 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2934 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2936 If OEP_BITWISE is set, then require the values to be bitwise identical
2937 rather than simply numerically equal. Do not take advantage of things
2938 like math-related flags or undefined behavior; only return true for
2939 values that are provably bitwise identical in all circumstances.
2941 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2942 any operand with side effect. This is unnecesarily conservative in the
2943 case we know that arg0 and arg1 are in disjoint code paths (such as in
2944 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2945 addresses with TREE_CONSTANT flag set so we know that &var == &var
2946 even if var is volatile. */
2949 operand_compare::operand_equal_p (const_tree arg0
, const_tree arg1
,
2953 if (verify_hash_value (arg0
, arg1
, flags
, &r
))
2956 STRIP_ANY_LOCATION_WRAPPER (arg0
);
2957 STRIP_ANY_LOCATION_WRAPPER (arg1
);
2959 /* If either is ERROR_MARK, they aren't equal. */
2960 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2961 || TREE_TYPE (arg0
) == error_mark_node
2962 || TREE_TYPE (arg1
) == error_mark_node
)
2965 /* Similar, if either does not have a type (like a template id),
2966 they aren't equal. */
2967 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2970 /* Bitwise identity makes no sense if the values have different layouts. */
2971 if ((flags
& OEP_BITWISE
)
2972 && !tree_nop_conversion_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
2975 /* We cannot consider pointers to different address space equal. */
2976 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2977 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2978 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2979 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2982 /* Check equality of integer constants before bailing out due to
2983 precision differences. */
2984 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2986 /* Address of INTEGER_CST is not defined; check that we did not forget
2987 to drop the OEP_ADDRESS_OF flags. */
2988 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2989 return tree_int_cst_equal (arg0
, arg1
);
2992 if (!(flags
& OEP_ADDRESS_OF
))
2994 /* If both types don't have the same signedness, then we can't consider
2995 them equal. We must check this before the STRIP_NOPS calls
2996 because they may change the signedness of the arguments. As pointers
2997 strictly don't have a signedness, require either two pointers or
2998 two non-pointers as well. */
2999 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
3000 || POINTER_TYPE_P (TREE_TYPE (arg0
))
3001 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
3004 /* If both types don't have the same precision, then it is not safe
3006 if (element_precision (TREE_TYPE (arg0
))
3007 != element_precision (TREE_TYPE (arg1
)))
3014 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3015 sanity check once the issue is solved. */
3017 /* Addresses of conversions and SSA_NAMEs (and many other things)
3018 are not defined. Check that we did not forget to drop the
3019 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3020 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
3021 && TREE_CODE (arg0
) != SSA_NAME
);
3024 /* In case both args are comparisons but with different comparison
3025 code, try to swap the comparison operands of one arg to produce
3026 a match and compare that variant. */
3027 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3028 && COMPARISON_CLASS_P (arg0
)
3029 && COMPARISON_CLASS_P (arg1
))
3031 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
3033 if (TREE_CODE (arg0
) == swap_code
)
3034 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3035 TREE_OPERAND (arg1
, 1), flags
)
3036 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3037 TREE_OPERAND (arg1
, 0), flags
);
3040 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3042 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3043 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3045 else if (flags
& OEP_ADDRESS_OF
)
3047 /* If we are interested in comparing addresses ignore
3048 MEM_REF wrappings of the base that can appear just for
3050 if (TREE_CODE (arg0
) == MEM_REF
3052 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3053 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3054 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3056 else if (TREE_CODE (arg1
) == MEM_REF
3058 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3059 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3060 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3068 /* When not checking adddresses, this is needed for conversions and for
3069 COMPONENT_REF. Might as well play it safe and always test this. */
3070 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3071 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3072 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3073 && !(flags
& OEP_ADDRESS_OF
)))
3076 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3077 We don't care about side effects in that case because the SAVE_EXPR
3078 takes care of that for us. In all other cases, two expressions are
3079 equal if they have no side effects. If we have two identical
3080 expressions with side effects that should be treated the same due
3081 to the only side effects being identical SAVE_EXPR's, that will
3082 be detected in the recursive calls below.
3083 If we are taking an invariant address of two identical objects
3084 they are necessarily equal as well. */
3085 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3086 && (TREE_CODE (arg0
) == SAVE_EXPR
3087 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3088 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3091 /* Next handle constant cases, those for which we can return 1 even
3092 if ONLY_CONST is set. */
3093 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3094 switch (TREE_CODE (arg0
))
3097 return tree_int_cst_equal (arg0
, arg1
);
3100 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3101 TREE_FIXED_CST (arg1
));
3104 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3107 if (!(flags
& OEP_BITWISE
) && !HONOR_SIGNED_ZEROS (arg0
))
3109 /* If we do not distinguish between signed and unsigned zero,
3110 consider them equal. */
3111 if (real_zerop (arg0
) && real_zerop (arg1
))
3118 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3119 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3122 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3123 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3126 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3127 for (unsigned int i
= 0; i
< count
; ++i
)
3128 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3129 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3135 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3137 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3141 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3142 && ! memcmp (TREE_STRING_POINTER (arg0
),
3143 TREE_STRING_POINTER (arg1
),
3144 TREE_STRING_LENGTH (arg0
)));
3147 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3148 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3149 flags
| OEP_ADDRESS_OF
3150 | OEP_MATCH_SIDE_EFFECTS
);
3152 /* In GIMPLE empty constructors are allowed in initializers of
3154 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
3159 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3160 two instances of undefined behavior will give identical results. */
3161 if (flags
& (OEP_ONLY_CONST
| OEP_BITWISE
))
3164 /* Define macros to test an operand from arg0 and arg1 for equality and a
3165 variant that allows null and views null as being different from any
3166 non-null value. In the latter case, if either is null, the both
3167 must be; otherwise, do the normal comparison. */
3168 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3169 TREE_OPERAND (arg1, N), flags)
3171 #define OP_SAME_WITH_NULL(N) \
3172 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3173 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3175 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3178 /* Two conversions are equal only if signedness and modes match. */
3179 switch (TREE_CODE (arg0
))
3182 case FIX_TRUNC_EXPR
:
3183 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3184 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3194 case tcc_comparison
:
3196 if (OP_SAME (0) && OP_SAME (1))
3199 /* For commutative ops, allow the other order. */
3200 return (commutative_tree_code (TREE_CODE (arg0
))
3201 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3202 TREE_OPERAND (arg1
, 1), flags
)
3203 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3204 TREE_OPERAND (arg1
, 0), flags
));
3207 /* If either of the pointer (or reference) expressions we are
3208 dereferencing contain a side effect, these cannot be equal,
3209 but their addresses can be. */
3210 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3211 && (TREE_SIDE_EFFECTS (arg0
)
3212 || TREE_SIDE_EFFECTS (arg1
)))
3215 switch (TREE_CODE (arg0
))
3218 if (!(flags
& OEP_ADDRESS_OF
))
3220 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3221 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3223 /* Verify that the access types are compatible. */
3224 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0
))
3225 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)))
3228 flags
&= ~OEP_ADDRESS_OF
;
3232 /* Require the same offset. */
3233 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3234 TYPE_SIZE (TREE_TYPE (arg1
)),
3235 flags
& ~OEP_ADDRESS_OF
))
3240 case VIEW_CONVERT_EXPR
:
3243 case TARGET_MEM_REF
:
3245 if (!(flags
& OEP_ADDRESS_OF
))
3247 /* Require equal access sizes */
3248 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3249 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3250 || !TYPE_SIZE (TREE_TYPE (arg1
))
3251 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3252 TYPE_SIZE (TREE_TYPE (arg1
)),
3255 /* Verify that access happens in similar types. */
3256 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3258 /* Verify that accesses are TBAA compatible. */
3259 if (!alias_ptr_types_compatible_p
3260 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3261 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3262 || (MR_DEPENDENCE_CLIQUE (arg0
)
3263 != MR_DEPENDENCE_CLIQUE (arg1
))
3264 || (MR_DEPENDENCE_BASE (arg0
)
3265 != MR_DEPENDENCE_BASE (arg1
)))
3267 /* Verify that alignment is compatible. */
3268 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3269 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3272 flags
&= ~OEP_ADDRESS_OF
;
3273 return (OP_SAME (0) && OP_SAME (1)
3274 /* TARGET_MEM_REF require equal extra operands. */
3275 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3276 || (OP_SAME_WITH_NULL (2)
3277 && OP_SAME_WITH_NULL (3)
3278 && OP_SAME_WITH_NULL (4))));
3281 case ARRAY_RANGE_REF
:
3284 flags
&= ~OEP_ADDRESS_OF
;
3285 /* Compare the array index by value if it is constant first as we
3286 may have different types but same value here. */
3287 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3288 TREE_OPERAND (arg1
, 1))
3290 && OP_SAME_WITH_NULL (2)
3291 && OP_SAME_WITH_NULL (3)
3292 /* Compare low bound and element size as with OEP_ADDRESS_OF
3293 we have to account for the offset of the ref. */
3294 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3295 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3296 || (operand_equal_p (array_ref_low_bound
3297 (CONST_CAST_TREE (arg0
)),
3299 (CONST_CAST_TREE (arg1
)), flags
)
3300 && operand_equal_p (array_ref_element_size
3301 (CONST_CAST_TREE (arg0
)),
3302 array_ref_element_size
3303 (CONST_CAST_TREE (arg1
)),
3307 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3308 may be NULL when we're called to compare MEM_EXPRs. */
3309 if (!OP_SAME_WITH_NULL (0))
3312 bool compare_address
= flags
& OEP_ADDRESS_OF
;
3314 /* Most of time we only need to compare FIELD_DECLs for equality.
3315 However when determining address look into actual offsets.
3316 These may match for unions and unshared record types. */
3317 flags
&= ~OEP_ADDRESS_OF
;
3321 && (flags
& OEP_ADDRESS_OF_SAME_FIELD
) == 0)
3323 if (TREE_OPERAND (arg0
, 2)
3324 || TREE_OPERAND (arg1
, 2))
3325 return OP_SAME_WITH_NULL (2);
3326 tree field0
= TREE_OPERAND (arg0
, 1);
3327 tree field1
= TREE_OPERAND (arg1
, 1);
3329 if (!operand_equal_p (DECL_FIELD_OFFSET (field0
),
3330 DECL_FIELD_OFFSET (field1
), flags
)
3331 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0
),
3332 DECL_FIELD_BIT_OFFSET (field1
),
3340 return OP_SAME_WITH_NULL (2);
3345 flags
&= ~OEP_ADDRESS_OF
;
3346 return OP_SAME (1) && OP_SAME (2);
3352 case tcc_expression
:
3353 switch (TREE_CODE (arg0
))
3356 /* Be sure we pass right ADDRESS_OF flag. */
3357 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3358 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3359 TREE_OPERAND (arg1
, 0),
3360 flags
| OEP_ADDRESS_OF
);
3362 case TRUTH_NOT_EXPR
:
3365 case TRUTH_ANDIF_EXPR
:
3366 case TRUTH_ORIF_EXPR
:
3367 return OP_SAME (0) && OP_SAME (1);
3369 case WIDEN_MULT_PLUS_EXPR
:
3370 case WIDEN_MULT_MINUS_EXPR
:
3373 /* The multiplcation operands are commutative. */
3376 case TRUTH_AND_EXPR
:
3378 case TRUTH_XOR_EXPR
:
3379 if (OP_SAME (0) && OP_SAME (1))
3382 /* Otherwise take into account this is a commutative operation. */
3383 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3384 TREE_OPERAND (arg1
, 1), flags
)
3385 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3386 TREE_OPERAND (arg1
, 0), flags
));
3389 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3391 flags
&= ~OEP_ADDRESS_OF
;
3394 case BIT_INSERT_EXPR
:
3395 /* BIT_INSERT_EXPR has an implict operand as the type precision
3396 of op1. Need to check to make sure they are the same. */
3397 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3398 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3399 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3400 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3406 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3411 case PREDECREMENT_EXPR
:
3412 case PREINCREMENT_EXPR
:
3413 case POSTDECREMENT_EXPR
:
3414 case POSTINCREMENT_EXPR
:
3415 if (flags
& OEP_LEXICOGRAPHIC
)
3416 return OP_SAME (0) && OP_SAME (1);
3419 case CLEANUP_POINT_EXPR
:
3422 if (flags
& OEP_LEXICOGRAPHIC
)
3427 /* Virtual table reference. */
3428 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0
),
3429 OBJ_TYPE_REF_EXPR (arg1
), flags
))
3431 flags
&= ~OEP_ADDRESS_OF
;
3432 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0
))
3433 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1
)))
3435 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0
),
3436 OBJ_TYPE_REF_OBJECT (arg1
), flags
))
3438 if (virtual_method_call_p (arg0
))
3440 if (!virtual_method_call_p (arg1
))
3442 return types_same_for_odr (obj_type_ref_class (arg0
),
3443 obj_type_ref_class (arg1
));
3452 switch (TREE_CODE (arg0
))
3455 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3456 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3457 /* If not both CALL_EXPRs are either internal or normal function
3458 functions, then they are not equal. */
3460 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3462 /* If the CALL_EXPRs call different internal functions, then they
3464 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3469 /* If the CALL_EXPRs call different functions, then they are not
3471 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3476 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3478 unsigned int cef
= call_expr_flags (arg0
);
3479 if (flags
& OEP_PURE_SAME
)
3480 cef
&= ECF_CONST
| ECF_PURE
;
3483 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3487 /* Now see if all the arguments are the same. */
3489 const_call_expr_arg_iterator iter0
, iter1
;
3491 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3492 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3494 a0
= next_const_call_expr_arg (&iter0
),
3495 a1
= next_const_call_expr_arg (&iter1
))
3496 if (! operand_equal_p (a0
, a1
, flags
))
3499 /* If we get here and both argument lists are exhausted
3500 then the CALL_EXPRs are equal. */
3501 return ! (a0
|| a1
);
3507 case tcc_declaration
:
3508 /* Consider __builtin_sqrt equal to sqrt. */
3509 return (TREE_CODE (arg0
) == FUNCTION_DECL
3510 && fndecl_built_in_p (arg0
) && fndecl_built_in_p (arg1
)
3511 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3512 && (DECL_UNCHECKED_FUNCTION_CODE (arg0
)
3513 == DECL_UNCHECKED_FUNCTION_CODE (arg1
)));
3515 case tcc_exceptional
:
3516 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3518 if (CONSTRUCTOR_NO_CLEARING (arg0
) != CONSTRUCTOR_NO_CLEARING (arg1
))
3521 /* In GIMPLE constructors are used only to build vectors from
3522 elements. Individual elements in the constructor must be
3523 indexed in increasing order and form an initial sequence.
3525 We make no effort to compare constructors in generic.
3526 (see sem_variable::equals in ipa-icf which can do so for
3528 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3529 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3532 /* Be sure that vectors constructed have the same representation.
3533 We only tested element precision and modes to match.
3534 Vectors may be BLKmode and thus also check that the number of
3536 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3537 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3540 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3541 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3542 unsigned int len
= vec_safe_length (v0
);
3544 if (len
!= vec_safe_length (v1
))
3547 for (unsigned int i
= 0; i
< len
; i
++)
3549 constructor_elt
*c0
= &(*v0
)[i
];
3550 constructor_elt
*c1
= &(*v1
)[i
];
3552 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3553 /* In GIMPLE the indexes can be either NULL or matching i.
3554 Double check this so we won't get false
3555 positives for GENERIC. */
3557 && (TREE_CODE (c0
->index
) != INTEGER_CST
3558 || compare_tree_int (c0
->index
, i
)))
3560 && (TREE_CODE (c1
->index
) != INTEGER_CST
3561 || compare_tree_int (c1
->index
, i
))))
3566 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3567 && (flags
& OEP_LEXICOGRAPHIC
))
3569 /* Compare the STATEMENT_LISTs. */
3570 tree_stmt_iterator tsi1
, tsi2
;
3571 tree body1
= CONST_CAST_TREE (arg0
);
3572 tree body2
= CONST_CAST_TREE (arg1
);
3573 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3574 tsi_next (&tsi1
), tsi_next (&tsi2
))
3576 /* The lists don't have the same number of statements. */
3577 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3579 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3581 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3582 flags
& (OEP_LEXICOGRAPHIC
3583 | OEP_NO_HASH_CHECK
)))
3590 switch (TREE_CODE (arg0
))
3593 if (flags
& OEP_LEXICOGRAPHIC
)
3594 return OP_SAME_WITH_NULL (0);
3596 case DEBUG_BEGIN_STMT
:
3597 if (flags
& OEP_LEXICOGRAPHIC
)
3609 #undef OP_SAME_WITH_NULL
3612 /* Generate a hash value for an expression. This can be used iteratively
3613 by passing a previous result as the HSTATE argument. */
3616 operand_compare::hash_operand (const_tree t
, inchash::hash
&hstate
,
3620 enum tree_code code
;
3621 enum tree_code_class tclass
;
3623 if (t
== NULL_TREE
|| t
== error_mark_node
)
3625 hstate
.merge_hash (0);
3629 STRIP_ANY_LOCATION_WRAPPER (t
);
3631 if (!(flags
& OEP_ADDRESS_OF
))
3634 code
= TREE_CODE (t
);
3638 /* Alas, constants aren't shared, so we can't rely on pointer
3641 hstate
.merge_hash (0);
3644 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3645 for (i
= 0; i
< TREE_INT_CST_EXT_NUNITS (t
); i
++)
3646 hstate
.add_hwi (TREE_INT_CST_ELT (t
, i
));
3651 if (!HONOR_SIGNED_ZEROS (t
) && real_zerop (t
))
3654 val2
= real_hash (TREE_REAL_CST_PTR (t
));
3655 hstate
.merge_hash (val2
);
3660 unsigned int val2
= fixed_hash (TREE_FIXED_CST_PTR (t
));
3661 hstate
.merge_hash (val2
);
3665 hstate
.add ((const void *) TREE_STRING_POINTER (t
),
3666 TREE_STRING_LENGTH (t
));
3669 hash_operand (TREE_REALPART (t
), hstate
, flags
);
3670 hash_operand (TREE_IMAGPART (t
), hstate
, flags
);
3674 hstate
.add_int (VECTOR_CST_NPATTERNS (t
));
3675 hstate
.add_int (VECTOR_CST_NELTS_PER_PATTERN (t
));
3676 unsigned int count
= vector_cst_encoded_nelts (t
);
3677 for (unsigned int i
= 0; i
< count
; ++i
)
3678 hash_operand (VECTOR_CST_ENCODED_ELT (t
, i
), hstate
, flags
);
3682 /* We can just compare by pointer. */
3683 hstate
.add_hwi (SSA_NAME_VERSION (t
));
3685 case PLACEHOLDER_EXPR
:
3686 /* The node itself doesn't matter. */
3693 /* A list of expressions, for a CALL_EXPR or as the elements of a
3695 for (; t
; t
= TREE_CHAIN (t
))
3696 hash_operand (TREE_VALUE (t
), hstate
, flags
);
3700 unsigned HOST_WIDE_INT idx
;
3702 flags
&= ~OEP_ADDRESS_OF
;
3703 hstate
.add_int (CONSTRUCTOR_NO_CLEARING (t
));
3704 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), idx
, field
, value
)
3706 /* In GIMPLE the indexes can be either NULL or matching i. */
3707 if (field
== NULL_TREE
)
3708 field
= bitsize_int (idx
);
3709 hash_operand (field
, hstate
, flags
);
3710 hash_operand (value
, hstate
, flags
);
3714 case STATEMENT_LIST
:
3716 tree_stmt_iterator i
;
3717 for (i
= tsi_start (CONST_CAST_TREE (t
));
3718 !tsi_end_p (i
); tsi_next (&i
))
3719 hash_operand (tsi_stmt (i
), hstate
, flags
);
3723 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
3724 hash_operand (TREE_VEC_ELT (t
, i
), hstate
, flags
);
3726 case IDENTIFIER_NODE
:
3727 hstate
.add_object (IDENTIFIER_HASH_VALUE (t
));
3730 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3731 Otherwise nodes that compare equal according to operand_equal_p might
3732 get different hash codes. However, don't do this for machine specific
3733 or front end builtins, since the function code is overloaded in those
3735 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
3736 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t
)))
3738 t
= builtin_decl_explicit (DECL_FUNCTION_CODE (t
));
3739 code
= TREE_CODE (t
);
3743 if (POLY_INT_CST_P (t
))
3745 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
3746 hstate
.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t
, i
)));
3749 tclass
= TREE_CODE_CLASS (code
);
3751 if (tclass
== tcc_declaration
)
3753 /* DECL's have a unique ID */
3754 hstate
.add_hwi (DECL_UID (t
));
3756 else if (tclass
== tcc_comparison
&& !commutative_tree_code (code
))
3758 /* For comparisons that can be swapped, use the lower
3760 enum tree_code ccode
= swap_tree_comparison (code
);
3763 hstate
.add_object (ccode
);
3764 hash_operand (TREE_OPERAND (t
, ccode
!= code
), hstate
, flags
);
3765 hash_operand (TREE_OPERAND (t
, ccode
== code
), hstate
, flags
);
3767 else if (CONVERT_EXPR_CODE_P (code
))
3769 /* NOP_EXPR and CONVERT_EXPR are considered equal by
3771 enum tree_code ccode
= NOP_EXPR
;
3772 hstate
.add_object (ccode
);
3774 /* Don't hash the type, that can lead to having nodes which
3775 compare equal according to operand_equal_p, but which
3776 have different hash codes. Make sure to include signedness
3777 in the hash computation. */
3778 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3779 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3781 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
3782 else if (code
== MEM_REF
3783 && (flags
& OEP_ADDRESS_OF
) != 0
3784 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
3785 && DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0))
3786 && integer_zerop (TREE_OPERAND (t
, 1)))
3787 hash_operand (TREE_OPERAND (TREE_OPERAND (t
, 0), 0),
3789 /* Don't ICE on FE specific trees, or their arguments etc.
3790 during operand_equal_p hash verification. */
3791 else if (!IS_EXPR_CODE_CLASS (tclass
))
3792 gcc_assert (flags
& OEP_HASH_CHECK
);
3795 unsigned int sflags
= flags
;
3797 hstate
.add_object (code
);
3802 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3803 flags
|= OEP_ADDRESS_OF
;
3809 case TARGET_MEM_REF
:
3810 flags
&= ~OEP_ADDRESS_OF
;
3815 if (sflags
& OEP_ADDRESS_OF
)
3817 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3818 if (TREE_OPERAND (t
, 2))
3819 hash_operand (TREE_OPERAND (t
, 2), hstate
,
3820 flags
& ~OEP_ADDRESS_OF
);
3823 tree field
= TREE_OPERAND (t
, 1);
3824 hash_operand (DECL_FIELD_OFFSET (field
),
3825 hstate
, flags
& ~OEP_ADDRESS_OF
);
3826 hash_operand (DECL_FIELD_BIT_OFFSET (field
),
3827 hstate
, flags
& ~OEP_ADDRESS_OF
);
3833 case ARRAY_RANGE_REF
:
3835 sflags
&= ~OEP_ADDRESS_OF
;
3839 flags
&= ~OEP_ADDRESS_OF
;
3842 case WIDEN_MULT_PLUS_EXPR
:
3843 case WIDEN_MULT_MINUS_EXPR
:
3845 /* The multiplication operands are commutative. */
3846 inchash::hash one
, two
;
3847 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3848 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3849 hstate
.add_commutative (one
, two
);
3850 hash_operand (TREE_OPERAND (t
, 2), two
, flags
);
3855 if (CALL_EXPR_FN (t
) == NULL_TREE
)
3856 hstate
.add_int (CALL_EXPR_IFN (t
));
3860 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
3861 Usually different TARGET_EXPRs just should use
3862 different temporaries in their slots. */
3863 hash_operand (TARGET_EXPR_SLOT (t
), hstate
, flags
);
3867 /* Virtual table reference. */
3868 inchash::add_expr (OBJ_TYPE_REF_EXPR (t
), hstate
, flags
);
3869 flags
&= ~OEP_ADDRESS_OF
;
3870 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t
), hstate
, flags
);
3871 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t
), hstate
, flags
);
3872 if (!virtual_method_call_p (t
))
3874 if (tree c
= obj_type_ref_class (t
))
3876 c
= TYPE_NAME (TYPE_MAIN_VARIANT (c
));
3877 /* We compute mangled names only when free_lang_data is run.
3878 In that case we can hash precisely. */
3879 if (TREE_CODE (c
) == TYPE_DECL
3880 && DECL_ASSEMBLER_NAME_SET_P (c
))
3882 (IDENTIFIER_HASH_VALUE
3883 (DECL_ASSEMBLER_NAME (c
)));
3890 /* Don't hash the type, that can lead to having nodes which
3891 compare equal according to operand_equal_p, but which
3892 have different hash codes. */
3893 if (code
== NON_LVALUE_EXPR
)
3895 /* Make sure to include signness in the hash computation. */
3896 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3897 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3900 else if (commutative_tree_code (code
))
3902 /* It's a commutative expression. We want to hash it the same
3903 however it appears. We do this by first hashing both operands
3904 and then rehashing based on the order of their independent
3906 inchash::hash one
, two
;
3907 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3908 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3909 hstate
.add_commutative (one
, two
);
3912 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
3913 hash_operand (TREE_OPERAND (t
, i
), hstate
,
3914 i
== 0 ? flags
: sflags
);
3921 operand_compare::verify_hash_value (const_tree arg0
, const_tree arg1
,
3922 unsigned int flags
, bool *ret
)
3924 /* When checking, verify at the outermost operand_equal_p call that
3925 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
3927 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
3929 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
3933 inchash::hash
hstate0 (0), hstate1 (0);
3934 hash_operand (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
3935 hash_operand (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
3936 hashval_t h0
= hstate0
.end ();
3937 hashval_t h1
= hstate1
.end ();
3938 gcc_assert (h0
== h1
);
3952 static operand_compare default_compare_instance
;
3954 /* Conveinece wrapper around operand_compare class because usually we do
3955 not need to play with the valueizer. */
3958 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
3960 return default_compare_instance
.operand_equal_p (arg0
, arg1
, flags
);
3966 /* Generate a hash value for an expression. This can be used iteratively
3967 by passing a previous result as the HSTATE argument.
3969 This function is intended to produce the same hash for expressions which
3970 would compare equal using operand_equal_p. */
3972 add_expr (const_tree t
, inchash::hash
&hstate
, unsigned int flags
)
3974 default_compare_instance
.hash_operand (t
, hstate
, flags
);
3979 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3980 with a different signedness or a narrower precision. */
3983 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
3985 if (operand_equal_p (arg0
, arg1
, 0))
3988 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3989 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3992 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3993 and see if the inner values are the same. This removes any
3994 signedness comparison, which doesn't matter here. */
3999 if (operand_equal_p (op0
, op1
, 0))
4002 /* Discard a single widening conversion from ARG1 and see if the inner
4003 value is the same as ARG0. */
4004 if (CONVERT_EXPR_P (arg1
)
4005 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4006 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4007 < TYPE_PRECISION (TREE_TYPE (arg1
))
4008 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
4014 /* See if ARG is an expression that is either a comparison or is performing
4015 arithmetic on comparisons. The comparisons must only be comparing
4016 two different values, which will be stored in *CVAL1 and *CVAL2; if
4017 they are nonzero it means that some operands have already been found.
4018 No variables may be used anywhere else in the expression except in the
4021 If this is true, return 1. Otherwise, return zero. */
4024 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
4026 enum tree_code code
= TREE_CODE (arg
);
4027 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4029 /* We can handle some of the tcc_expression cases here. */
4030 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4032 else if (tclass
== tcc_expression
4033 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
4034 || code
== COMPOUND_EXPR
))
4035 tclass
= tcc_binary
;
4040 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
4043 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4044 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
4049 case tcc_expression
:
4050 if (code
== COND_EXPR
)
4051 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4052 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
4053 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
4056 case tcc_comparison
:
4057 /* First see if we can handle the first operand, then the second. For
4058 the second operand, we know *CVAL1 can't be zero. It must be that
4059 one side of the comparison is each of the values; test for the
4060 case where this isn't true by failing if the two operands
4063 if (operand_equal_p (TREE_OPERAND (arg
, 0),
4064 TREE_OPERAND (arg
, 1), 0))
4068 *cval1
= TREE_OPERAND (arg
, 0);
4069 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
4071 else if (*cval2
== 0)
4072 *cval2
= TREE_OPERAND (arg
, 0);
4073 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
4078 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
4080 else if (*cval2
== 0)
4081 *cval2
= TREE_OPERAND (arg
, 1);
4082 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
4094 /* ARG is a tree that is known to contain just arithmetic operations and
4095 comparisons. Evaluate the operations in the tree substituting NEW0 for
4096 any occurrence of OLD0 as an operand of a comparison and likewise for
4100 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
4101 tree old1
, tree new1
)
4103 tree type
= TREE_TYPE (arg
);
4104 enum tree_code code
= TREE_CODE (arg
);
4105 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4107 /* We can handle some of the tcc_expression cases here. */
4108 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4110 else if (tclass
== tcc_expression
4111 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
4112 tclass
= tcc_binary
;
4117 return fold_build1_loc (loc
, code
, type
,
4118 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4119 old0
, new0
, old1
, new1
));
4122 return fold_build2_loc (loc
, code
, type
,
4123 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4124 old0
, new0
, old1
, new1
),
4125 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4126 old0
, new0
, old1
, new1
));
4128 case tcc_expression
:
4132 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
4136 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
4140 return fold_build3_loc (loc
, code
, type
,
4141 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4142 old0
, new0
, old1
, new1
),
4143 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4144 old0
, new0
, old1
, new1
),
4145 eval_subst (loc
, TREE_OPERAND (arg
, 2),
4146 old0
, new0
, old1
, new1
));
4150 /* Fall through - ??? */
4152 case tcc_comparison
:
4154 tree arg0
= TREE_OPERAND (arg
, 0);
4155 tree arg1
= TREE_OPERAND (arg
, 1);
4157 /* We need to check both for exact equality and tree equality. The
4158 former will be true if the operand has a side-effect. In that
4159 case, we know the operand occurred exactly once. */
4161 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
4163 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
4166 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
4168 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
4171 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
4179 /* Return a tree for the case when the result of an expression is RESULT
4180 converted to TYPE and OMITTED was previously an operand of the expression
4181 but is now not needed (e.g., we folded OMITTED * 0).
4183 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4184 the conversion of RESULT to TYPE. */
4187 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
4189 tree t
= fold_convert_loc (loc
, type
, result
);
4191 /* If the resulting operand is an empty statement, just return the omitted
4192 statement casted to void. */
4193 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
4194 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
4195 fold_ignored_result (omitted
));
4197 if (TREE_SIDE_EFFECTS (omitted
))
4198 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4199 fold_ignored_result (omitted
), t
);
4201 return non_lvalue_loc (loc
, t
);
4204 /* Return a tree for the case when the result of an expression is RESULT
4205 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4206 of the expression but are now not needed.
4208 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4209 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4210 evaluated before OMITTED2. Otherwise, if neither has side effects,
4211 just do the conversion of RESULT to TYPE. */
4214 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
4215 tree omitted1
, tree omitted2
)
4217 tree t
= fold_convert_loc (loc
, type
, result
);
4219 if (TREE_SIDE_EFFECTS (omitted2
))
4220 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
4221 if (TREE_SIDE_EFFECTS (omitted1
))
4222 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
4224 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
4228 /* Return a simplified tree node for the truth-negation of ARG. This
4229 never alters ARG itself. We assume that ARG is an operation that
4230 returns a truth value (0 or 1).
4232 FIXME: one would think we would fold the result, but it causes
4233 problems with the dominator optimizer. */
4236 fold_truth_not_expr (location_t loc
, tree arg
)
4238 tree type
= TREE_TYPE (arg
);
4239 enum tree_code code
= TREE_CODE (arg
);
4240 location_t loc1
, loc2
;
4242 /* If this is a comparison, we can simply invert it, except for
4243 floating-point non-equality comparisons, in which case we just
4244 enclose a TRUTH_NOT_EXPR around what we have. */
4246 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4248 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4249 if (FLOAT_TYPE_P (op_type
)
4250 && flag_trapping_math
4251 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
4252 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
4255 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
4256 if (code
== ERROR_MARK
)
4259 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
4260 TREE_OPERAND (arg
, 1));
4261 if (TREE_NO_WARNING (arg
))
4262 TREE_NO_WARNING (ret
) = 1;
4269 return constant_boolean_node (integer_zerop (arg
), type
);
4271 case TRUTH_AND_EXPR
:
4272 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4273 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4274 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
4275 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4276 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4279 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4280 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4281 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
4282 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4283 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4285 case TRUTH_XOR_EXPR
:
4286 /* Here we can invert either operand. We invert the first operand
4287 unless the second operand is a TRUTH_NOT_EXPR in which case our
4288 result is the XOR of the first operand with the inside of the
4289 negation of the second operand. */
4291 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
4292 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
4293 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
4295 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
4296 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
4297 TREE_OPERAND (arg
, 1));
4299 case TRUTH_ANDIF_EXPR
:
4300 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4301 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4302 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
4303 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4304 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4306 case TRUTH_ORIF_EXPR
:
4307 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4308 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4309 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
4310 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4311 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4313 case TRUTH_NOT_EXPR
:
4314 return TREE_OPERAND (arg
, 0);
4318 tree arg1
= TREE_OPERAND (arg
, 1);
4319 tree arg2
= TREE_OPERAND (arg
, 2);
4321 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4322 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
4324 /* A COND_EXPR may have a throw as one operand, which
4325 then has void type. Just leave void operands
4327 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
4328 VOID_TYPE_P (TREE_TYPE (arg1
))
4329 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
4330 VOID_TYPE_P (TREE_TYPE (arg2
))
4331 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
4335 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4336 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4337 TREE_OPERAND (arg
, 0),
4338 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
4340 case NON_LVALUE_EXPR
:
4341 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4342 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
4345 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4346 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4351 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4352 return build1_loc (loc
, TREE_CODE (arg
), type
,
4353 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4356 if (!integer_onep (TREE_OPERAND (arg
, 1)))
4358 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
4361 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4363 case CLEANUP_POINT_EXPR
:
4364 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4365 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
4366 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4373 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4374 assume that ARG is an operation that returns a truth value (0 or 1
4375 for scalars, 0 or -1 for vectors). Return the folded expression if
4376 folding is successful. Otherwise, return NULL_TREE. */
4379 fold_invert_truthvalue (location_t loc
, tree arg
)
4381 tree type
= TREE_TYPE (arg
);
4382 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
4388 /* Return a simplified tree node for the truth-negation of ARG. This
4389 never alters ARG itself. We assume that ARG is an operation that
4390 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4393 invert_truthvalue_loc (location_t loc
, tree arg
)
4395 if (TREE_CODE (arg
) == ERROR_MARK
)
4398 tree type
= TREE_TYPE (arg
);
4399 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
4405 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4406 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4407 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4408 is the original memory reference used to preserve the alias set of
4412 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
4413 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
4414 int unsignedp
, int reversep
)
4416 tree result
, bftype
;
4418 /* Attempt not to lose the access path if possible. */
4419 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
4421 tree ninner
= TREE_OPERAND (orig_inner
, 0);
4423 poly_int64 nbitsize
, nbitpos
;
4425 int nunsignedp
, nreversep
, nvolatilep
= 0;
4426 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
4427 &noffset
, &nmode
, &nunsignedp
,
4428 &nreversep
, &nvolatilep
);
4430 && noffset
== NULL_TREE
4431 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
4441 alias_set_type iset
= get_alias_set (orig_inner
);
4442 if (iset
== 0 && get_alias_set (inner
) != iset
)
4443 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
4444 build_fold_addr_expr (inner
),
4445 build_int_cst (ptr_type_node
, 0));
4447 if (known_eq (bitpos
, 0) && !reversep
)
4449 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
4450 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
4451 || POINTER_TYPE_P (TREE_TYPE (inner
)))
4452 && tree_fits_shwi_p (size
)
4453 && tree_to_shwi (size
) == bitsize
)
4454 return fold_convert_loc (loc
, type
, inner
);
4458 if (TYPE_PRECISION (bftype
) != bitsize
4459 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
4460 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4462 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4463 bitsize_int (bitsize
), bitsize_int (bitpos
));
4464 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4467 result
= fold_convert_loc (loc
, type
, result
);
4472 /* Optimize a bit-field compare.
4474 There are two cases: First is a compare against a constant and the
4475 second is a comparison of two items where the fields are at the same
4476 bit position relative to the start of a chunk (byte, halfword, word)
4477 large enough to contain it. In these cases we can avoid the shift
4478 implicit in bitfield extractions.
4480 For constants, we emit a compare of the shifted constant with the
4481 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4482 compared. For two fields at the same position, we do the ANDs with the
4483 similar mask and compare the result of the ANDs.
4485 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4486 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4487 are the left and right operands of the comparison, respectively.
4489 If the optimization described above can be done, we return the resulting
4490 tree. Otherwise we return zero. */
4493 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4494 tree compare_type
, tree lhs
, tree rhs
)
4496 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4497 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4498 tree type
= TREE_TYPE (lhs
);
4500 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4501 machine_mode lmode
, rmode
;
4502 scalar_int_mode nmode
;
4503 int lunsignedp
, runsignedp
;
4504 int lreversep
, rreversep
;
4505 int lvolatilep
= 0, rvolatilep
= 0;
4506 tree linner
, rinner
= NULL_TREE
;
4510 /* Get all the information about the extractions being done. If the bit size
4511 is the same as the size of the underlying object, we aren't doing an
4512 extraction at all and so can do nothing. We also don't want to
4513 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4514 then will no longer be able to replace it. */
4515 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4516 &lunsignedp
, &lreversep
, &lvolatilep
);
4518 || !known_size_p (plbitsize
)
4519 || !plbitsize
.is_constant (&lbitsize
)
4520 || !plbitpos
.is_constant (&lbitpos
)
4521 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4523 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4528 rreversep
= lreversep
;
4531 /* If this is not a constant, we can only do something if bit positions,
4532 sizes, signedness and storage order are the same. */
4534 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4535 &runsignedp
, &rreversep
, &rvolatilep
);
4538 || maybe_ne (lbitpos
, rbitpos
)
4539 || maybe_ne (lbitsize
, rbitsize
)
4540 || lunsignedp
!= runsignedp
4541 || lreversep
!= rreversep
4543 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4548 /* Honor the C++ memory model and mimic what RTL expansion does. */
4549 poly_uint64 bitstart
= 0;
4550 poly_uint64 bitend
= 0;
4551 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4553 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4554 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4558 /* See if we can find a mode to refer to this field. We should be able to,
4559 but fail if we can't. */
4560 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4561 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4562 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4563 TYPE_ALIGN (TREE_TYPE (rinner
))),
4564 BITS_PER_WORD
, false, &nmode
))
4567 /* Set signed and unsigned types of the precision of this mode for the
4569 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4571 /* Compute the bit position and size for the new reference and our offset
4572 within it. If the new reference is the same size as the original, we
4573 won't optimize anything, so return zero. */
4574 nbitsize
= GET_MODE_BITSIZE (nmode
);
4575 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4577 if (nbitsize
== lbitsize
)
4580 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4581 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4583 /* Make the mask to be used against the extracted field. */
4584 mask
= build_int_cst_type (unsigned_type
, -1);
4585 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4586 mask
= const_binop (RSHIFT_EXPR
, mask
,
4587 size_int (nbitsize
- lbitsize
- lbitpos
));
4594 /* If not comparing with constant, just rework the comparison
4596 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4597 nbitsize
, nbitpos
, 1, lreversep
);
4598 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4599 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4600 nbitsize
, nbitpos
, 1, rreversep
);
4601 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4602 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4605 /* Otherwise, we are handling the constant case. See if the constant is too
4606 big for the field. Warn and return a tree for 0 (false) if so. We do
4607 this not only for its own sake, but to avoid having to test for this
4608 error case below. If we didn't, we might generate wrong code.
4610 For unsigned fields, the constant shifted right by the field length should
4611 be all zero. For signed fields, the high-order bits should agree with
4616 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4618 warning (0, "comparison is always %d due to width of bit-field",
4620 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4625 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4626 if (tem
!= 0 && tem
!= -1)
4628 warning (0, "comparison is always %d due to width of bit-field",
4630 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4637 /* Single-bit compares should always be against zero. */
4638 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4640 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4641 rhs
= build_int_cst (type
, 0);
4644 /* Make a new bitfield reference, shift the constant over the
4645 appropriate number of bits and mask it with the computed mask
4646 (in case this was a signed field). If we changed it, make a new one. */
4647 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4648 nbitsize
, nbitpos
, 1, lreversep
);
4650 rhs
= const_binop (BIT_AND_EXPR
,
4651 const_binop (LSHIFT_EXPR
,
4652 fold_convert_loc (loc
, unsigned_type
, rhs
),
4653 size_int (lbitpos
)),
4656 lhs
= build2_loc (loc
, code
, compare_type
,
4657 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4661 /* Subroutine for fold_truth_andor_1: decode a field reference.
4663 If EXP is a comparison reference, we return the innermost reference.
4665 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4666 set to the starting bit number.
4668 If the innermost field can be completely contained in a mode-sized
4669 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4671 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4672 otherwise it is not changed.
4674 *PUNSIGNEDP is set to the signedness of the field.
4676 *PREVERSEP is set to the storage order of the field.
4678 *PMASK is set to the mask used. This is either contained in a
4679 BIT_AND_EXPR or derived from the width of the field.
4681 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4683 Return 0 if this is not a component reference or is one that we can't
4684 do anything with. */
4687 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4688 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4689 int *punsignedp
, int *preversep
, int *pvolatilep
,
4690 tree
*pmask
, tree
*pand_mask
)
4693 tree outer_type
= 0;
4695 tree mask
, inner
, offset
;
4697 unsigned int precision
;
4699 /* All the optimizations using this function assume integer fields.
4700 There are problems with FP fields since the type_for_size call
4701 below can fail for, e.g., XFmode. */
4702 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4705 /* We are interested in the bare arrangement of bits, so strip everything
4706 that doesn't affect the machine mode. However, record the type of the
4707 outermost expression if it may matter below. */
4708 if (CONVERT_EXPR_P (exp
)
4709 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4710 outer_type
= TREE_TYPE (exp
);
4713 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4715 and_mask
= TREE_OPERAND (exp
, 1);
4716 exp
= TREE_OPERAND (exp
, 0);
4717 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4718 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4722 poly_int64 poly_bitsize
, poly_bitpos
;
4723 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4724 pmode
, punsignedp
, preversep
, pvolatilep
);
4725 if ((inner
== exp
&& and_mask
== 0)
4726 || !poly_bitsize
.is_constant (pbitsize
)
4727 || !poly_bitpos
.is_constant (pbitpos
)
4730 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4731 /* Reject out-of-bound accesses (PR79731). */
4732 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4733 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4734 *pbitpos
+ *pbitsize
) < 0))
4737 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4738 if (unsigned_type
== NULL_TREE
)
4743 /* If the number of bits in the reference is the same as the bitsize of
4744 the outer type, then the outer type gives the signedness. Otherwise
4745 (in case of a small bitfield) the signedness is unchanged. */
4746 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4747 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4749 /* Compute the mask to access the bitfield. */
4750 precision
= TYPE_PRECISION (unsigned_type
);
4752 mask
= build_int_cst_type (unsigned_type
, -1);
4754 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4755 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4757 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4759 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4760 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4763 *pand_mask
= and_mask
;
4767 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4768 bit positions and MASK is SIGNED. */
4771 all_ones_mask_p (const_tree mask
, unsigned int size
)
4773 tree type
= TREE_TYPE (mask
);
4774 unsigned int precision
= TYPE_PRECISION (type
);
4776 /* If this function returns true when the type of the mask is
4777 UNSIGNED, then there will be errors. In particular see
4778 gcc.c-torture/execute/990326-1.c. There does not appear to be
4779 any documentation paper trail as to why this is so. But the pre
4780 wide-int worked with that restriction and it has been preserved
4782 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4785 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
4788 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4789 represents the sign bit of EXP's type. If EXP represents a sign
4790 or zero extension, also test VAL against the unextended type.
4791 The return value is the (sub)expression whose sign bit is VAL,
4792 or NULL_TREE otherwise. */
4795 sign_bit_p (tree exp
, const_tree val
)
4800 /* Tree EXP must have an integral type. */
4801 t
= TREE_TYPE (exp
);
4802 if (! INTEGRAL_TYPE_P (t
))
4805 /* Tree VAL must be an integer constant. */
4806 if (TREE_CODE (val
) != INTEGER_CST
4807 || TREE_OVERFLOW (val
))
4810 width
= TYPE_PRECISION (t
);
4811 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
4814 /* Handle extension from a narrower type. */
4815 if (TREE_CODE (exp
) == NOP_EXPR
4816 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4817 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4822 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4823 to be evaluated unconditionally. */
4826 simple_operand_p (const_tree exp
)
4828 /* Strip any conversions that don't change the machine mode. */
4831 return (CONSTANT_CLASS_P (exp
)
4832 || TREE_CODE (exp
) == SSA_NAME
4834 && ! TREE_ADDRESSABLE (exp
)
4835 && ! TREE_THIS_VOLATILE (exp
)
4836 && ! DECL_NONLOCAL (exp
)
4837 /* Don't regard global variables as simple. They may be
4838 allocated in ways unknown to the compiler (shared memory,
4839 #pragma weak, etc). */
4840 && ! TREE_PUBLIC (exp
)
4841 && ! DECL_EXTERNAL (exp
)
4842 /* Weakrefs are not safe to be read, since they can be NULL.
4843 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4844 have DECL_WEAK flag set. */
4845 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4846 /* Loading a static variable is unduly expensive, but global
4847 registers aren't expensive. */
4848 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4851 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4852 to be evaluated unconditionally.
4853 I addition to simple_operand_p, we assume that comparisons, conversions,
4854 and logic-not operations are simple, if their operands are simple, too. */
4857 simple_operand_p_2 (tree exp
)
4859 enum tree_code code
;
4861 if (TREE_SIDE_EFFECTS (exp
) || generic_expr_could_trap_p (exp
))
4864 while (CONVERT_EXPR_P (exp
))
4865 exp
= TREE_OPERAND (exp
, 0);
4867 code
= TREE_CODE (exp
);
4869 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4870 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4871 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4873 if (code
== TRUTH_NOT_EXPR
)
4874 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4876 return simple_operand_p (exp
);
4880 /* The following functions are subroutines to fold_range_test and allow it to
4881 try to change a logical combination of comparisons into a range test.
4884 X == 2 || X == 3 || X == 4 || X == 5
4888 (unsigned) (X - 2) <= 3
4890 We describe each set of comparisons as being either inside or outside
4891 a range, using a variable named like IN_P, and then describe the
4892 range with a lower and upper bound. If one of the bounds is omitted,
4893 it represents either the highest or lowest value of the type.
4895 In the comments below, we represent a range by two numbers in brackets
4896 preceded by a "+" to designate being inside that range, or a "-" to
4897 designate being outside that range, so the condition can be inverted by
4898 flipping the prefix. An omitted bound is represented by a "-". For
4899 example, "- [-, 10]" means being outside the range starting at the lowest
4900 possible value and ending at 10, in other words, being greater than 10.
4901 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4904 We set up things so that the missing bounds are handled in a consistent
4905 manner so neither a missing bound nor "true" and "false" need to be
4906 handled using a special case. */
4908 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4909 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4910 and UPPER1_P are nonzero if the respective argument is an upper bound
4911 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4912 must be specified for a comparison. ARG1 will be converted to ARG0's
4913 type if both are specified. */
4916 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4917 tree arg1
, int upper1_p
)
4923 /* If neither arg represents infinity, do the normal operation.
4924 Else, if not a comparison, return infinity. Else handle the special
4925 comparison rules. Note that most of the cases below won't occur, but
4926 are handled for consistency. */
4928 if (arg0
!= 0 && arg1
!= 0)
4930 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4931 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4933 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4936 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4939 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4940 for neither. In real maths, we cannot assume open ended ranges are
4941 the same. But, this is computer arithmetic, where numbers are finite.
4942 We can therefore make the transformation of any unbounded range with
4943 the value Z, Z being greater than any representable number. This permits
4944 us to treat unbounded ranges as equal. */
4945 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4946 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4950 result
= sgn0
== sgn1
;
4953 result
= sgn0
!= sgn1
;
4956 result
= sgn0
< sgn1
;
4959 result
= sgn0
<= sgn1
;
4962 result
= sgn0
> sgn1
;
4965 result
= sgn0
>= sgn1
;
4971 return constant_boolean_node (result
, type
);
4974 /* Helper routine for make_range. Perform one step for it, return
4975 new expression if the loop should continue or NULL_TREE if it should
4979 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4980 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4981 bool *strict_overflow_p
)
4983 tree arg0_type
= TREE_TYPE (arg0
);
4984 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4985 int in_p
= *p_in_p
, n_in_p
;
4989 case TRUTH_NOT_EXPR
:
4990 /* We can only do something if the range is testing for zero. */
4991 if (low
== NULL_TREE
|| high
== NULL_TREE
4992 || ! integer_zerop (low
) || ! integer_zerop (high
))
4997 case EQ_EXPR
: case NE_EXPR
:
4998 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4999 /* We can only do something if the range is testing for zero
5000 and if the second operand is an integer constant. Note that
5001 saying something is "in" the range we make is done by
5002 complementing IN_P since it will set in the initial case of
5003 being not equal to zero; "out" is leaving it alone. */
5004 if (low
== NULL_TREE
|| high
== NULL_TREE
5005 || ! integer_zerop (low
) || ! integer_zerop (high
)
5006 || TREE_CODE (arg1
) != INTEGER_CST
)
5011 case NE_EXPR
: /* - [c, c] */
5014 case EQ_EXPR
: /* + [c, c] */
5015 in_p
= ! in_p
, low
= high
= arg1
;
5017 case GT_EXPR
: /* - [-, c] */
5018 low
= 0, high
= arg1
;
5020 case GE_EXPR
: /* + [c, -] */
5021 in_p
= ! in_p
, low
= arg1
, high
= 0;
5023 case LT_EXPR
: /* - [c, -] */
5024 low
= arg1
, high
= 0;
5026 case LE_EXPR
: /* + [-, c] */
5027 in_p
= ! in_p
, low
= 0, high
= arg1
;
5033 /* If this is an unsigned comparison, we also know that EXP is
5034 greater than or equal to zero. We base the range tests we make
5035 on that fact, so we record it here so we can parse existing
5036 range tests. We test arg0_type since often the return type
5037 of, e.g. EQ_EXPR, is boolean. */
5038 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
5040 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
5042 build_int_cst (arg0_type
, 0),
5046 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
5048 /* If the high bound is missing, but we have a nonzero low
5049 bound, reverse the range so it goes from zero to the low bound
5051 if (high
== 0 && low
&& ! integer_zerop (low
))
5054 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
5055 build_int_cst (TREE_TYPE (low
), 1), 0);
5056 low
= build_int_cst (arg0_type
, 0);
5066 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5067 low and high are non-NULL, then normalize will DTRT. */
5068 if (!TYPE_UNSIGNED (arg0_type
)
5069 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5071 if (low
== NULL_TREE
)
5072 low
= TYPE_MIN_VALUE (arg0_type
);
5073 if (high
== NULL_TREE
)
5074 high
= TYPE_MAX_VALUE (arg0_type
);
5077 /* (-x) IN [a,b] -> x in [-b, -a] */
5078 n_low
= range_binop (MINUS_EXPR
, exp_type
,
5079 build_int_cst (exp_type
, 0),
5081 n_high
= range_binop (MINUS_EXPR
, exp_type
,
5082 build_int_cst (exp_type
, 0),
5084 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
5090 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
5091 build_int_cst (exp_type
, 1));
5095 if (TREE_CODE (arg1
) != INTEGER_CST
)
5098 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5099 move a constant to the other side. */
5100 if (!TYPE_UNSIGNED (arg0_type
)
5101 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5104 /* If EXP is signed, any overflow in the computation is undefined,
5105 so we don't worry about it so long as our computations on
5106 the bounds don't overflow. For unsigned, overflow is defined
5107 and this is exactly the right thing. */
5108 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5109 arg0_type
, low
, 0, arg1
, 0);
5110 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5111 arg0_type
, high
, 1, arg1
, 0);
5112 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
5113 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
5116 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5117 *strict_overflow_p
= true;
5120 /* Check for an unsigned range which has wrapped around the maximum
5121 value thus making n_high < n_low, and normalize it. */
5122 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
5124 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
5125 build_int_cst (TREE_TYPE (n_high
), 1), 0);
5126 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
5127 build_int_cst (TREE_TYPE (n_low
), 1), 0);
5129 /* If the range is of the form +/- [ x+1, x ], we won't
5130 be able to normalize it. But then, it represents the
5131 whole range or the empty set, so make it
5133 if (tree_int_cst_equal (n_low
, low
)
5134 && tree_int_cst_equal (n_high
, high
))
5140 low
= n_low
, high
= n_high
;
5148 case NON_LVALUE_EXPR
:
5149 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
5152 if (! INTEGRAL_TYPE_P (arg0_type
)
5153 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
5154 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
5157 n_low
= low
, n_high
= high
;
5160 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
5163 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
5165 /* If we're converting arg0 from an unsigned type, to exp,
5166 a signed type, we will be doing the comparison as unsigned.
5167 The tests above have already verified that LOW and HIGH
5170 So we have to ensure that we will handle large unsigned
5171 values the same way that the current signed bounds treat
5174 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
5178 /* For fixed-point modes, we need to pass the saturating flag
5179 as the 2nd parameter. */
5180 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
5182 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
5183 TYPE_SATURATING (arg0_type
));
5186 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
5188 /* A range without an upper bound is, naturally, unbounded.
5189 Since convert would have cropped a very large value, use
5190 the max value for the destination type. */
5192 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
5193 : TYPE_MAX_VALUE (arg0_type
);
5195 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
5196 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
5197 fold_convert_loc (loc
, arg0_type
,
5199 build_int_cst (arg0_type
, 1));
5201 /* If the low bound is specified, "and" the range with the
5202 range for which the original unsigned value will be
5206 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
5207 1, fold_convert_loc (loc
, arg0_type
,
5212 in_p
= (n_in_p
== in_p
);
5216 /* Otherwise, "or" the range with the range of the input
5217 that will be interpreted as negative. */
5218 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
5219 1, fold_convert_loc (loc
, arg0_type
,
5224 in_p
= (in_p
!= n_in_p
);
5238 /* Given EXP, a logical expression, set the range it is testing into
5239 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5240 actually being tested. *PLOW and *PHIGH will be made of the same
5241 type as the returned expression. If EXP is not a comparison, we
5242 will most likely not be returning a useful value and range. Set
5243 *STRICT_OVERFLOW_P to true if the return value is only valid
5244 because signed overflow is undefined; otherwise, do not change
5245 *STRICT_OVERFLOW_P. */
5248 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
5249 bool *strict_overflow_p
)
5251 enum tree_code code
;
5252 tree arg0
, arg1
= NULL_TREE
;
5253 tree exp_type
, nexp
;
5256 location_t loc
= EXPR_LOCATION (exp
);
5258 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5259 and see if we can refine the range. Some of the cases below may not
5260 happen, but it doesn't seem worth worrying about this. We "continue"
5261 the outer loop when we've changed something; otherwise we "break"
5262 the switch, which will "break" the while. */
5265 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
5269 code
= TREE_CODE (exp
);
5270 exp_type
= TREE_TYPE (exp
);
5273 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
5275 if (TREE_OPERAND_LENGTH (exp
) > 0)
5276 arg0
= TREE_OPERAND (exp
, 0);
5277 if (TREE_CODE_CLASS (code
) == tcc_binary
5278 || TREE_CODE_CLASS (code
) == tcc_comparison
5279 || (TREE_CODE_CLASS (code
) == tcc_expression
5280 && TREE_OPERAND_LENGTH (exp
) > 1))
5281 arg1
= TREE_OPERAND (exp
, 1);
5283 if (arg0
== NULL_TREE
)
5286 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
5287 &high
, &in_p
, strict_overflow_p
);
5288 if (nexp
== NULL_TREE
)
5293 /* If EXP is a constant, we can evaluate whether this is true or false. */
5294 if (TREE_CODE (exp
) == INTEGER_CST
)
5296 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
5298 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5304 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5308 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5309 a bitwise check i.e. when
5310 LOW == 0xXX...X00...0
5311 HIGH == 0xXX...X11...1
5312 Return corresponding mask in MASK and stem in VALUE. */
5315 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
5318 if (TREE_CODE (low
) != INTEGER_CST
5319 || TREE_CODE (high
) != INTEGER_CST
)
5322 unsigned prec
= TYPE_PRECISION (type
);
5323 wide_int lo
= wi::to_wide (low
, prec
);
5324 wide_int hi
= wi::to_wide (high
, prec
);
5326 wide_int end_mask
= lo
^ hi
;
5327 if ((end_mask
& (end_mask
+ 1)) != 0
5328 || (lo
& end_mask
) != 0)
5331 wide_int stem_mask
= ~end_mask
;
5332 wide_int stem
= lo
& stem_mask
;
5333 if (stem
!= (hi
& stem_mask
))
5336 *mask
= wide_int_to_tree (type
, stem_mask
);
5337 *value
= wide_int_to_tree (type
, stem
);
5342 /* Helper routine for build_range_check and match.pd. Return the type to
5343 perform the check or NULL if it shouldn't be optimized. */
5346 range_check_type (tree etype
)
5348 /* First make sure that arithmetics in this type is valid, then make sure
5349 that it wraps around. */
5350 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
5351 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
), 1);
5353 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_UNSIGNED (etype
))
5355 tree utype
, minv
, maxv
;
5357 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5358 for the type in question, as we rely on this here. */
5359 utype
= unsigned_type_for (etype
);
5360 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
5361 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
5362 build_int_cst (TREE_TYPE (maxv
), 1), 1);
5363 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
5365 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
5371 else if (POINTER_TYPE_P (etype
))
5372 etype
= unsigned_type_for (etype
);
5376 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5377 type, TYPE, return an expression to test if EXP is in (or out of, depending
5378 on IN_P) the range. Return 0 if the test couldn't be created. */
5381 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
5382 tree low
, tree high
)
5384 tree etype
= TREE_TYPE (exp
), mask
, value
;
5386 /* Disable this optimization for function pointer expressions
5387 on targets that require function pointer canonicalization. */
5388 if (targetm
.have_canonicalize_funcptr_for_compare ()
5389 && POINTER_TYPE_P (etype
)
5390 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype
)))
5395 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
5397 return invert_truthvalue_loc (loc
, value
);
5402 if (low
== 0 && high
== 0)
5403 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
5406 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
5407 fold_convert_loc (loc
, etype
, high
));
5410 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
5411 fold_convert_loc (loc
, etype
, low
));
5413 if (operand_equal_p (low
, high
, 0))
5414 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
5415 fold_convert_loc (loc
, etype
, low
));
5417 if (TREE_CODE (exp
) == BIT_AND_EXPR
5418 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
5419 return fold_build2_loc (loc
, EQ_EXPR
, type
,
5420 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
5424 if (integer_zerop (low
))
5426 if (! TYPE_UNSIGNED (etype
))
5428 etype
= unsigned_type_for (etype
);
5429 high
= fold_convert_loc (loc
, etype
, high
);
5430 exp
= fold_convert_loc (loc
, etype
, exp
);
5432 return build_range_check (loc
, type
, exp
, 1, 0, high
);
5435 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5436 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
5438 int prec
= TYPE_PRECISION (etype
);
5440 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
5442 if (TYPE_UNSIGNED (etype
))
5444 tree signed_etype
= signed_type_for (etype
);
5445 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
5447 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
5449 etype
= signed_etype
;
5450 exp
= fold_convert_loc (loc
, etype
, exp
);
5452 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
5453 build_int_cst (etype
, 0));
5457 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5458 This requires wrap-around arithmetics for the type of the expression. */
5459 etype
= range_check_type (etype
);
5460 if (etype
== NULL_TREE
)
5463 high
= fold_convert_loc (loc
, etype
, high
);
5464 low
= fold_convert_loc (loc
, etype
, low
);
5465 exp
= fold_convert_loc (loc
, etype
, exp
);
5467 value
= const_binop (MINUS_EXPR
, high
, low
);
5469 if (value
!= 0 && !TREE_OVERFLOW (value
))
5470 return build_range_check (loc
, type
,
5471 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5472 1, build_int_cst (etype
, 0), value
);
5477 /* Return the predecessor of VAL in its type, handling the infinite case. */
5480 range_predecessor (tree val
)
5482 tree type
= TREE_TYPE (val
);
5484 if (INTEGRAL_TYPE_P (type
)
5485 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5488 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5489 build_int_cst (TREE_TYPE (val
), 1), 0);
5492 /* Return the successor of VAL in its type, handling the infinite case. */
5495 range_successor (tree val
)
5497 tree type
= TREE_TYPE (val
);
5499 if (INTEGRAL_TYPE_P (type
)
5500 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5503 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5504 build_int_cst (TREE_TYPE (val
), 1), 0);
5507 /* Given two ranges, see if we can merge them into one. Return 1 if we
5508 can, 0 if we can't. Set the output range into the specified parameters. */
5511 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5512 tree high0
, int in1_p
, tree low1
, tree high1
)
5520 int lowequal
= ((low0
== 0 && low1
== 0)
5521 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5522 low0
, 0, low1
, 0)));
5523 int highequal
= ((high0
== 0 && high1
== 0)
5524 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5525 high0
, 1, high1
, 1)));
5527 /* Make range 0 be the range that starts first, or ends last if they
5528 start at the same value. Swap them if it isn't. */
5529 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5532 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5533 high1
, 1, high0
, 1))))
5535 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5536 tem
= low0
, low0
= low1
, low1
= tem
;
5537 tem
= high0
, high0
= high1
, high1
= tem
;
5540 /* If the second range is != high1 where high1 is the type maximum of
5541 the type, try first merging with < high1 range. */
5544 && TREE_CODE (low1
) == INTEGER_CST
5545 && (TREE_CODE (TREE_TYPE (low1
)) == INTEGER_TYPE
5546 || (TREE_CODE (TREE_TYPE (low1
)) == ENUMERAL_TYPE
5547 && known_eq (TYPE_PRECISION (TREE_TYPE (low1
)),
5548 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1
))))))
5549 && operand_equal_p (low1
, high1
, 0))
5551 if (tree_int_cst_equal (low1
, TYPE_MAX_VALUE (TREE_TYPE (low1
)))
5552 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5553 !in1_p
, NULL_TREE
, range_predecessor (low1
)))
5555 /* Similarly for the second range != low1 where low1 is the type minimum
5556 of the type, try first merging with > low1 range. */
5557 if (tree_int_cst_equal (low1
, TYPE_MIN_VALUE (TREE_TYPE (low1
)))
5558 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5559 !in1_p
, range_successor (low1
), NULL_TREE
))
5563 /* Now flag two cases, whether the ranges are disjoint or whether the
5564 second range is totally subsumed in the first. Note that the tests
5565 below are simplified by the ones above. */
5566 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5567 high0
, 1, low1
, 0));
5568 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5569 high1
, 1, high0
, 1));
5571 /* We now have four cases, depending on whether we are including or
5572 excluding the two ranges. */
5575 /* If they don't overlap, the result is false. If the second range
5576 is a subset it is the result. Otherwise, the range is from the start
5577 of the second to the end of the first. */
5579 in_p
= 0, low
= high
= 0;
5581 in_p
= 1, low
= low1
, high
= high1
;
5583 in_p
= 1, low
= low1
, high
= high0
;
5586 else if (in0_p
&& ! in1_p
)
5588 /* If they don't overlap, the result is the first range. If they are
5589 equal, the result is false. If the second range is a subset of the
5590 first, and the ranges begin at the same place, we go from just after
5591 the end of the second range to the end of the first. If the second
5592 range is not a subset of the first, or if it is a subset and both
5593 ranges end at the same place, the range starts at the start of the
5594 first range and ends just before the second range.
5595 Otherwise, we can't describe this as a single range. */
5597 in_p
= 1, low
= low0
, high
= high0
;
5598 else if (lowequal
&& highequal
)
5599 in_p
= 0, low
= high
= 0;
5600 else if (subset
&& lowequal
)
5602 low
= range_successor (high1
);
5607 /* We are in the weird situation where high0 > high1 but
5608 high1 has no successor. Punt. */
5612 else if (! subset
|| highequal
)
5615 high
= range_predecessor (low1
);
5619 /* low0 < low1 but low1 has no predecessor. Punt. */
5627 else if (! in0_p
&& in1_p
)
5629 /* If they don't overlap, the result is the second range. If the second
5630 is a subset of the first, the result is false. Otherwise,
5631 the range starts just after the first range and ends at the
5632 end of the second. */
5634 in_p
= 1, low
= low1
, high
= high1
;
5635 else if (subset
|| highequal
)
5636 in_p
= 0, low
= high
= 0;
5639 low
= range_successor (high0
);
5644 /* high1 > high0 but high0 has no successor. Punt. */
5652 /* The case where we are excluding both ranges. Here the complex case
5653 is if they don't overlap. In that case, the only time we have a
5654 range is if they are adjacent. If the second is a subset of the
5655 first, the result is the first. Otherwise, the range to exclude
5656 starts at the beginning of the first range and ends at the end of the
5660 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5661 range_successor (high0
),
5663 in_p
= 0, low
= low0
, high
= high1
;
5666 /* Canonicalize - [min, x] into - [-, x]. */
5667 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5668 switch (TREE_CODE (TREE_TYPE (low0
)))
5671 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5673 (TYPE_MODE (TREE_TYPE (low0
)))))
5677 if (tree_int_cst_equal (low0
,
5678 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5682 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5683 && integer_zerop (low0
))
5690 /* Canonicalize - [x, max] into - [x, -]. */
5691 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5692 switch (TREE_CODE (TREE_TYPE (high1
)))
5695 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5697 (TYPE_MODE (TREE_TYPE (high1
)))))
5701 if (tree_int_cst_equal (high1
,
5702 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5706 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5707 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5709 build_int_cst (TREE_TYPE (high1
), 1),
5717 /* The ranges might be also adjacent between the maximum and
5718 minimum values of the given type. For
5719 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5720 return + [x + 1, y - 1]. */
5721 if (low0
== 0 && high1
== 0)
5723 low
= range_successor (high0
);
5724 high
= range_predecessor (low1
);
5725 if (low
== 0 || high
== 0)
5735 in_p
= 0, low
= low0
, high
= high0
;
5737 in_p
= 0, low
= low0
, high
= high1
;
5740 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5745 /* Subroutine of fold, looking inside expressions of the form
5746 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5747 of the COND_EXPR. This function is being used also to optimize
5748 A op B ? C : A, by reversing the comparison first.
5750 Return a folded expression whose code is not a COND_EXPR
5751 anymore, or NULL_TREE if no folding opportunity is found. */
5754 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5755 tree arg0
, tree arg1
, tree arg2
)
5757 enum tree_code comp_code
= TREE_CODE (arg0
);
5758 tree arg00
= TREE_OPERAND (arg0
, 0);
5759 tree arg01
= TREE_OPERAND (arg0
, 1);
5760 tree arg1_type
= TREE_TYPE (arg1
);
5766 /* If we have A op 0 ? A : -A, consider applying the following
5769 A == 0? A : -A same as -A
5770 A != 0? A : -A same as A
5771 A >= 0? A : -A same as abs (A)
5772 A > 0? A : -A same as abs (A)
5773 A <= 0? A : -A same as -abs (A)
5774 A < 0? A : -A same as -abs (A)
5776 None of these transformations work for modes with signed
5777 zeros. If A is +/-0, the first two transformations will
5778 change the sign of the result (from +0 to -0, or vice
5779 versa). The last four will fix the sign of the result,
5780 even though the original expressions could be positive or
5781 negative, depending on the sign of A.
5783 Note that all these transformations are correct if A is
5784 NaN, since the two alternatives (A and -A) are also NaNs. */
5785 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5786 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5787 ? real_zerop (arg01
)
5788 : integer_zerop (arg01
))
5789 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5790 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5791 /* In the case that A is of the form X-Y, '-A' (arg2) may
5792 have already been folded to Y-X, check for that. */
5793 || (TREE_CODE (arg1
) == MINUS_EXPR
5794 && TREE_CODE (arg2
) == MINUS_EXPR
5795 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5796 TREE_OPERAND (arg2
, 1), 0)
5797 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5798 TREE_OPERAND (arg2
, 0), 0))))
5803 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5804 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5807 return fold_convert_loc (loc
, type
, arg1
);
5810 if (flag_trapping_math
)
5815 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5817 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5818 return fold_convert_loc (loc
, type
, tem
);
5821 if (flag_trapping_math
)
5826 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5828 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
5829 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
5831 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
5832 is not, invokes UB both in abs and in the negation of it.
5833 So, use ABSU_EXPR instead. */
5834 tree utype
= unsigned_type_for (TREE_TYPE (arg1
));
5835 tem
= fold_build1_loc (loc
, ABSU_EXPR
, utype
, arg1
);
5836 tem
= negate_expr (tem
);
5837 return fold_convert_loc (loc
, type
, tem
);
5841 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5842 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5845 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5849 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5850 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5851 both transformations are correct when A is NaN: A != 0
5852 is then true, and A == 0 is false. */
5854 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5855 && integer_zerop (arg01
) && integer_zerop (arg2
))
5857 if (comp_code
== NE_EXPR
)
5858 return fold_convert_loc (loc
, type
, arg1
);
5859 else if (comp_code
== EQ_EXPR
)
5860 return build_zero_cst (type
);
5863 /* Try some transformations of A op B ? A : B.
5865 A == B? A : B same as B
5866 A != B? A : B same as A
5867 A >= B? A : B same as max (A, B)
5868 A > B? A : B same as max (B, A)
5869 A <= B? A : B same as min (A, B)
5870 A < B? A : B same as min (B, A)
5872 As above, these transformations don't work in the presence
5873 of signed zeros. For example, if A and B are zeros of
5874 opposite sign, the first two transformations will change
5875 the sign of the result. In the last four, the original
5876 expressions give different results for (A=+0, B=-0) and
5877 (A=-0, B=+0), but the transformed expressions do not.
5879 The first two transformations are correct if either A or B
5880 is a NaN. In the first transformation, the condition will
5881 be false, and B will indeed be chosen. In the case of the
5882 second transformation, the condition A != B will be true,
5883 and A will be chosen.
5885 The conversions to max() and min() are not correct if B is
5886 a number and A is not. The conditions in the original
5887 expressions will be false, so all four give B. The min()
5888 and max() versions would give a NaN instead. */
5889 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5890 && operand_equal_for_comparison_p (arg01
, arg2
)
5891 /* Avoid these transformations if the COND_EXPR may be used
5892 as an lvalue in the C++ front-end. PR c++/19199. */
5894 || VECTOR_TYPE_P (type
)
5895 || (! lang_GNU_CXX ()
5896 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5897 || ! maybe_lvalue_p (arg1
)
5898 || ! maybe_lvalue_p (arg2
)))
5900 tree comp_op0
= arg00
;
5901 tree comp_op1
= arg01
;
5902 tree comp_type
= TREE_TYPE (comp_op0
);
5907 return fold_convert_loc (loc
, type
, arg2
);
5909 return fold_convert_loc (loc
, type
, arg1
);
5914 /* In C++ a ?: expression can be an lvalue, so put the
5915 operand which will be used if they are equal first
5916 so that we can convert this back to the
5917 corresponding COND_EXPR. */
5918 if (!HONOR_NANS (arg1
))
5920 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5921 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5922 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5923 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5924 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5925 comp_op1
, comp_op0
);
5926 return fold_convert_loc (loc
, type
, tem
);
5933 if (!HONOR_NANS (arg1
))
5935 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5936 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5937 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5938 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5939 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5940 comp_op1
, comp_op0
);
5941 return fold_convert_loc (loc
, type
, tem
);
5945 if (!HONOR_NANS (arg1
))
5946 return fold_convert_loc (loc
, type
, arg2
);
5949 if (!HONOR_NANS (arg1
))
5950 return fold_convert_loc (loc
, type
, arg1
);
5953 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5963 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5964 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5965 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5969 /* EXP is some logical combination of boolean tests. See if we can
5970 merge it into some range test. Return the new tree if so. */
5973 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5976 int or_op
= (code
== TRUTH_ORIF_EXPR
5977 || code
== TRUTH_OR_EXPR
);
5978 int in0_p
, in1_p
, in_p
;
5979 tree low0
, low1
, low
, high0
, high1
, high
;
5980 bool strict_overflow_p
= false;
5982 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5983 "when simplifying range test");
5985 if (!INTEGRAL_TYPE_P (type
))
5988 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5989 /* If op0 is known true or false and this is a short-circuiting
5990 operation we must not merge with op1 since that makes side-effects
5991 unconditional. So special-case this. */
5993 && ((code
== TRUTH_ORIF_EXPR
&& in0_p
)
5994 || (code
== TRUTH_ANDIF_EXPR
&& !in0_p
)))
5996 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5998 /* If this is an OR operation, invert both sides; we will invert
5999 again at the end. */
6001 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
6003 /* If both expressions are the same, if we can merge the ranges, and we
6004 can build the range test, return it or it inverted. If one of the
6005 ranges is always true or always false, consider it to be the same
6006 expression as the other. */
6007 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
6008 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
6010 && (tem
= (build_range_check (loc
, type
,
6012 : rhs
!= 0 ? rhs
: integer_zero_node
,
6013 in_p
, low
, high
))) != 0)
6015 if (strict_overflow_p
)
6016 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
6017 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
6020 /* On machines where the branch cost is expensive, if this is a
6021 short-circuited branch and the underlying object on both sides
6022 is the same, make a non-short-circuit operation. */
6023 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
6024 if (param_logical_op_non_short_circuit
!= -1)
6025 logical_op_non_short_circuit
6026 = param_logical_op_non_short_circuit
;
6027 if (logical_op_non_short_circuit
6028 && !flag_sanitize_coverage
6029 && lhs
!= 0 && rhs
!= 0
6030 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6031 && operand_equal_p (lhs
, rhs
, 0))
6033 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6034 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6035 which cases we can't do this. */
6036 if (simple_operand_p (lhs
))
6037 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6038 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6041 else if (!lang_hooks
.decls
.global_bindings_p ()
6042 && !CONTAINS_PLACEHOLDER_P (lhs
))
6044 tree common
= save_expr (lhs
);
6046 if ((lhs
= build_range_check (loc
, type
, common
,
6047 or_op
? ! in0_p
: in0_p
,
6049 && (rhs
= build_range_check (loc
, type
, common
,
6050 or_op
? ! in1_p
: in1_p
,
6053 if (strict_overflow_p
)
6054 fold_overflow_warning (warnmsg
,
6055 WARN_STRICT_OVERFLOW_COMPARISON
);
6056 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6057 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6066 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6067 bit value. Arrange things so the extra bits will be set to zero if and
6068 only if C is signed-extended to its full width. If MASK is nonzero,
6069 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6072 unextend (tree c
, int p
, int unsignedp
, tree mask
)
6074 tree type
= TREE_TYPE (c
);
6075 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
6078 if (p
== modesize
|| unsignedp
)
6081 /* We work by getting just the sign bit into the low-order bit, then
6082 into the high-order bit, then sign-extend. We then XOR that value
6084 temp
= build_int_cst (TREE_TYPE (c
),
6085 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
6087 /* We must use a signed type in order to get an arithmetic right shift.
6088 However, we must also avoid introducing accidental overflows, so that
6089 a subsequent call to integer_zerop will work. Hence we must
6090 do the type conversion here. At this point, the constant is either
6091 zero or one, and the conversion to a signed type can never overflow.
6092 We could get an overflow if this conversion is done anywhere else. */
6093 if (TYPE_UNSIGNED (type
))
6094 temp
= fold_convert (signed_type_for (type
), temp
);
6096 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
6097 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
6099 temp
= const_binop (BIT_AND_EXPR
, temp
,
6100 fold_convert (TREE_TYPE (c
), mask
));
6101 /* If necessary, convert the type back to match the type of C. */
6102 if (TYPE_UNSIGNED (type
))
6103 temp
= fold_convert (type
, temp
);
6105 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
6108 /* For an expression that has the form
6112 we can drop one of the inner expressions and simplify to
6116 LOC is the location of the resulting expression. OP is the inner
6117 logical operation; the left-hand side in the examples above, while CMPOP
6118 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6119 removing a condition that guards another, as in
6120 (A != NULL && A->...) || A == NULL
6121 which we must not transform. If RHS_ONLY is true, only eliminate the
6122 right-most operand of the inner logical operation. */
6125 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
6128 tree type
= TREE_TYPE (cmpop
);
6129 enum tree_code code
= TREE_CODE (cmpop
);
6130 enum tree_code truthop_code
= TREE_CODE (op
);
6131 tree lhs
= TREE_OPERAND (op
, 0);
6132 tree rhs
= TREE_OPERAND (op
, 1);
6133 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
6134 enum tree_code rhs_code
= TREE_CODE (rhs
);
6135 enum tree_code lhs_code
= TREE_CODE (lhs
);
6136 enum tree_code inv_code
;
6138 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
6141 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
6144 if (rhs_code
== truthop_code
)
6146 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
6147 if (newrhs
!= NULL_TREE
)
6150 rhs_code
= TREE_CODE (rhs
);
6153 if (lhs_code
== truthop_code
&& !rhs_only
)
6155 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
6156 if (newlhs
!= NULL_TREE
)
6159 lhs_code
= TREE_CODE (lhs
);
6163 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
6164 if (inv_code
== rhs_code
6165 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6166 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6168 if (!rhs_only
&& inv_code
== lhs_code
6169 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6170 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6172 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
6173 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
6178 /* Find ways of folding logical expressions of LHS and RHS:
6179 Try to merge two comparisons to the same innermost item.
6180 Look for range tests like "ch >= '0' && ch <= '9'".
6181 Look for combinations of simple terms on machines with expensive branches
6182 and evaluate the RHS unconditionally.
6184 For example, if we have p->a == 2 && p->b == 4 and we can make an
6185 object large enough to span both A and B, we can do this with a comparison
6186 against the object ANDed with the a mask.
6188 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6189 operations to do this with one comparison.
6191 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6192 function and the one above.
6194 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6195 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6197 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6200 We return the simplified tree or 0 if no optimization is possible. */
6203 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
6206 /* If this is the "or" of two comparisons, we can do something if
6207 the comparisons are NE_EXPR. If this is the "and", we can do something
6208 if the comparisons are EQ_EXPR. I.e.,
6209 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6211 WANTED_CODE is this operation code. For single bit fields, we can
6212 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6213 comparison for one-bit fields. */
6215 enum tree_code wanted_code
;
6216 enum tree_code lcode
, rcode
;
6217 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
6218 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
6219 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
6220 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
6221 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
6222 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
6223 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
6224 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
6225 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
6226 scalar_int_mode lnmode
, rnmode
;
6227 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
6228 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
6229 tree l_const
, r_const
;
6230 tree lntype
, rntype
, result
;
6231 HOST_WIDE_INT first_bit
, end_bit
;
6234 /* Start by getting the comparison codes. Fail if anything is volatile.
6235 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6236 it were surrounded with a NE_EXPR. */
6238 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
6241 lcode
= TREE_CODE (lhs
);
6242 rcode
= TREE_CODE (rhs
);
6244 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
6246 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
6247 build_int_cst (TREE_TYPE (lhs
), 0));
6251 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
6253 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
6254 build_int_cst (TREE_TYPE (rhs
), 0));
6258 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
6259 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
6262 ll_arg
= TREE_OPERAND (lhs
, 0);
6263 lr_arg
= TREE_OPERAND (lhs
, 1);
6264 rl_arg
= TREE_OPERAND (rhs
, 0);
6265 rr_arg
= TREE_OPERAND (rhs
, 1);
6267 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6268 if (simple_operand_p (ll_arg
)
6269 && simple_operand_p (lr_arg
))
6271 if (operand_equal_p (ll_arg
, rl_arg
, 0)
6272 && operand_equal_p (lr_arg
, rr_arg
, 0))
6274 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
6275 truth_type
, ll_arg
, lr_arg
);
6279 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
6280 && operand_equal_p (lr_arg
, rl_arg
, 0))
6282 result
= combine_comparisons (loc
, code
, lcode
,
6283 swap_tree_comparison (rcode
),
6284 truth_type
, ll_arg
, lr_arg
);
6290 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
6291 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
6293 /* If the RHS can be evaluated unconditionally and its operands are
6294 simple, it wins to evaluate the RHS unconditionally on machines
6295 with expensive branches. In this case, this isn't a comparison
6296 that can be merged. */
6298 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
6300 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
6301 && simple_operand_p (rl_arg
)
6302 && simple_operand_p (rr_arg
))
6304 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6305 if (code
== TRUTH_OR_EXPR
6306 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
6307 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
6308 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6309 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6310 return build2_loc (loc
, NE_EXPR
, truth_type
,
6311 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6313 build_int_cst (TREE_TYPE (ll_arg
), 0));
6315 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6316 if (code
== TRUTH_AND_EXPR
6317 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
6318 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
6319 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6320 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6321 return build2_loc (loc
, EQ_EXPR
, truth_type
,
6322 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6324 build_int_cst (TREE_TYPE (ll_arg
), 0));
6327 /* See if the comparisons can be merged. Then get all the parameters for
6330 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
6331 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
6334 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
6336 ll_inner
= decode_field_reference (loc
, &ll_arg
,
6337 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
6338 &ll_unsignedp
, &ll_reversep
, &volatilep
,
6339 &ll_mask
, &ll_and_mask
);
6340 lr_inner
= decode_field_reference (loc
, &lr_arg
,
6341 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
6342 &lr_unsignedp
, &lr_reversep
, &volatilep
,
6343 &lr_mask
, &lr_and_mask
);
6344 rl_inner
= decode_field_reference (loc
, &rl_arg
,
6345 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
6346 &rl_unsignedp
, &rl_reversep
, &volatilep
,
6347 &rl_mask
, &rl_and_mask
);
6348 rr_inner
= decode_field_reference (loc
, &rr_arg
,
6349 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
6350 &rr_unsignedp
, &rr_reversep
, &volatilep
,
6351 &rr_mask
, &rr_and_mask
);
6353 /* It must be true that the inner operation on the lhs of each
6354 comparison must be the same if we are to be able to do anything.
6355 Then see if we have constants. If not, the same must be true for
6358 || ll_reversep
!= rl_reversep
6359 || ll_inner
== 0 || rl_inner
== 0
6360 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
6363 if (TREE_CODE (lr_arg
) == INTEGER_CST
6364 && TREE_CODE (rr_arg
) == INTEGER_CST
)
6366 l_const
= lr_arg
, r_const
= rr_arg
;
6367 lr_reversep
= ll_reversep
;
6369 else if (lr_reversep
!= rr_reversep
6370 || lr_inner
== 0 || rr_inner
== 0
6371 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
6374 l_const
= r_const
= 0;
6376 /* If either comparison code is not correct for our logical operation,
6377 fail. However, we can convert a one-bit comparison against zero into
6378 the opposite comparison against that bit being set in the field. */
6380 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
6381 if (lcode
!= wanted_code
)
6383 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
6385 /* Make the left operand unsigned, since we are only interested
6386 in the value of one bit. Otherwise we are doing the wrong
6395 /* This is analogous to the code for l_const above. */
6396 if (rcode
!= wanted_code
)
6398 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
6407 /* See if we can find a mode that contains both fields being compared on
6408 the left. If we can't, fail. Otherwise, update all constants and masks
6409 to be relative to a field of that size. */
6410 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
6411 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
6412 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6413 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
6414 volatilep
, &lnmode
))
6417 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
6418 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
6419 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
6420 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
6422 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6424 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
6425 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
6428 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
6429 size_int (xll_bitpos
));
6430 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
6431 size_int (xrl_bitpos
));
6435 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
6436 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
6437 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
6438 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
6439 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6442 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6444 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6449 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
6450 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
6451 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
6452 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
6453 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6456 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6458 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6462 /* If the right sides are not constant, do the same for it. Also,
6463 disallow this optimization if a size, signedness or storage order
6464 mismatch occurs between the left and right sides. */
6467 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
6468 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
6469 || ll_reversep
!= lr_reversep
6470 /* Make sure the two fields on the right
6471 correspond to the left without being swapped. */
6472 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
6475 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
6476 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
6477 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6478 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
6479 volatilep
, &rnmode
))
6482 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
6483 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
6484 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
6485 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
6487 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6489 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
6490 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
6493 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6495 size_int (xlr_bitpos
));
6496 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6498 size_int (xrr_bitpos
));
6500 /* Make a mask that corresponds to both fields being compared.
6501 Do this for both items being compared. If the operands are the
6502 same size and the bits being compared are in the same position
6503 then we can do this by masking both and comparing the masked
6505 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6506 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
6507 if (lnbitsize
== rnbitsize
6508 && xll_bitpos
== xlr_bitpos
6512 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6513 lntype
, lnbitsize
, lnbitpos
,
6514 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6515 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6516 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6518 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6519 rntype
, rnbitsize
, rnbitpos
,
6520 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6521 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6522 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6524 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6527 /* There is still another way we can do something: If both pairs of
6528 fields being compared are adjacent, we may be able to make a wider
6529 field containing them both.
6531 Note that we still must mask the lhs/rhs expressions. Furthermore,
6532 the mask must be shifted to account for the shift done by
6533 make_bit_field_ref. */
6534 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6535 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6536 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6537 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6545 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6546 ll_bitsize
+ rl_bitsize
,
6547 MIN (ll_bitpos
, rl_bitpos
),
6548 ll_unsignedp
, ll_reversep
);
6549 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6550 lr_bitsize
+ rr_bitsize
,
6551 MIN (lr_bitpos
, rr_bitpos
),
6552 lr_unsignedp
, lr_reversep
);
6554 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6555 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6556 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6557 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6559 /* Convert to the smaller type before masking out unwanted bits. */
6561 if (lntype
!= rntype
)
6563 if (lnbitsize
> rnbitsize
)
6565 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6566 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6569 else if (lnbitsize
< rnbitsize
)
6571 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6572 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6577 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6578 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6580 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6581 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6583 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6589 /* Handle the case of comparisons with constants. If there is something in
6590 common between the masks, those bits of the constants must be the same.
6591 If not, the condition is always false. Test for this to avoid generating
6592 incorrect code below. */
6593 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6594 if (! integer_zerop (result
)
6595 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6596 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6598 if (wanted_code
== NE_EXPR
)
6600 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6601 return constant_boolean_node (true, truth_type
);
6605 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6606 return constant_boolean_node (false, truth_type
);
6613 /* Construct the expression we will return. First get the component
6614 reference we will make. Unless the mask is all ones the width of
6615 that field, perform the mask operation. Then compare with the
6617 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6618 lntype
, lnbitsize
, lnbitpos
,
6619 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6621 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6622 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6623 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6625 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6626 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6629 /* T is an integer expression that is being multiplied, divided, or taken a
6630 modulus (CODE says which and what kind of divide or modulus) by a
6631 constant C. See if we can eliminate that operation by folding it with
6632 other operations already in T. WIDE_TYPE, if non-null, is a type that
6633 should be used for the computation if wider than our type.
6635 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6636 (X * 2) + (Y * 4). We must, however, be assured that either the original
6637 expression would not overflow or that overflow is undefined for the type
6638 in the language in question.
6640 If we return a non-null expression, it is an equivalent form of the
6641 original computation, but need not be in the original type.
6643 We set *STRICT_OVERFLOW_P to true if the return values depends on
6644 signed overflow being undefined. Otherwise we do not change
6645 *STRICT_OVERFLOW_P. */
6648 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6649 bool *strict_overflow_p
)
6651 /* To avoid exponential search depth, refuse to allow recursion past
6652 three levels. Beyond that (1) it's highly unlikely that we'll find
6653 something interesting and (2) we've probably processed it before
6654 when we built the inner expression. */
6663 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6670 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6671 bool *strict_overflow_p
)
6673 tree type
= TREE_TYPE (t
);
6674 enum tree_code tcode
= TREE_CODE (t
);
6675 tree ctype
= (wide_type
!= 0
6676 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6677 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6678 ? wide_type
: type
);
6680 int same_p
= tcode
== code
;
6681 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6682 bool sub_strict_overflow_p
;
6684 /* Don't deal with constants of zero here; they confuse the code below. */
6685 if (integer_zerop (c
))
6688 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6689 op0
= TREE_OPERAND (t
, 0);
6691 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6692 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6694 /* Note that we need not handle conditional operations here since fold
6695 already handles those cases. So just do arithmetic here. */
6699 /* For a constant, we can always simplify if we are a multiply
6700 or (for divide and modulus) if it is a multiple of our constant. */
6701 if (code
== MULT_EXPR
6702 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6705 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6706 fold_convert (ctype
, c
));
6707 /* If the multiplication overflowed, we lost information on it.
6708 See PR68142 and PR69845. */
6709 if (TREE_OVERFLOW (tem
))
6715 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6716 /* If op0 is an expression ... */
6717 if ((COMPARISON_CLASS_P (op0
)
6718 || UNARY_CLASS_P (op0
)
6719 || BINARY_CLASS_P (op0
)
6720 || VL_EXP_CLASS_P (op0
)
6721 || EXPRESSION_CLASS_P (op0
))
6722 /* ... and has wrapping overflow, and its type is smaller
6723 than ctype, then we cannot pass through as widening. */
6724 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6725 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6726 && (TYPE_PRECISION (ctype
)
6727 > TYPE_PRECISION (TREE_TYPE (op0
))))
6728 /* ... or this is a truncation (t is narrower than op0),
6729 then we cannot pass through this narrowing. */
6730 || (TYPE_PRECISION (type
)
6731 < TYPE_PRECISION (TREE_TYPE (op0
)))
6732 /* ... or signedness changes for division or modulus,
6733 then we cannot pass through this conversion. */
6734 || (code
!= MULT_EXPR
6735 && (TYPE_UNSIGNED (ctype
)
6736 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6737 /* ... or has undefined overflow while the converted to
6738 type has not, we cannot do the operation in the inner type
6739 as that would introduce undefined overflow. */
6740 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6741 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6742 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6745 /* Pass the constant down and see if we can make a simplification. If
6746 we can, replace this expression with the inner simplification for
6747 possible later conversion to our or some other type. */
6748 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6749 && TREE_CODE (t2
) == INTEGER_CST
6750 && !TREE_OVERFLOW (t2
)
6751 && (t1
= extract_muldiv (op0
, t2
, code
,
6752 code
== MULT_EXPR
? ctype
: NULL_TREE
,
6753 strict_overflow_p
)) != 0)
6758 /* If widening the type changes it from signed to unsigned, then we
6759 must avoid building ABS_EXPR itself as unsigned. */
6760 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6762 tree cstype
= (*signed_type_for
) (ctype
);
6763 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6766 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6767 return fold_convert (ctype
, t1
);
6771 /* If the constant is negative, we cannot simplify this. */
6772 if (tree_int_cst_sgn (c
) == -1)
6776 /* For division and modulus, type can't be unsigned, as e.g.
6777 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6778 For signed types, even with wrapping overflow, this is fine. */
6779 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6781 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6783 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6786 case MIN_EXPR
: case MAX_EXPR
:
6787 /* If widening the type changes the signedness, then we can't perform
6788 this optimization as that changes the result. */
6789 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6792 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6793 sub_strict_overflow_p
= false;
6794 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6795 &sub_strict_overflow_p
)) != 0
6796 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6797 &sub_strict_overflow_p
)) != 0)
6799 if (tree_int_cst_sgn (c
) < 0)
6800 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6801 if (sub_strict_overflow_p
)
6802 *strict_overflow_p
= true;
6803 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6804 fold_convert (ctype
, t2
));
6808 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6809 /* If the second operand is constant, this is a multiplication
6810 or floor division, by a power of two, so we can treat it that
6811 way unless the multiplier or divisor overflows. Signed
6812 left-shift overflow is implementation-defined rather than
6813 undefined in C90, so do not convert signed left shift into
6815 if (TREE_CODE (op1
) == INTEGER_CST
6816 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6817 /* const_binop may not detect overflow correctly,
6818 so check for it explicitly here. */
6819 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
6821 && (t1
= fold_convert (ctype
,
6822 const_binop (LSHIFT_EXPR
, size_one_node
,
6824 && !TREE_OVERFLOW (t1
))
6825 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6826 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6828 fold_convert (ctype
, op0
),
6830 c
, code
, wide_type
, strict_overflow_p
);
6833 case PLUS_EXPR
: case MINUS_EXPR
:
6834 /* See if we can eliminate the operation on both sides. If we can, we
6835 can return a new PLUS or MINUS. If we can't, the only remaining
6836 cases where we can do anything are if the second operand is a
6838 sub_strict_overflow_p
= false;
6839 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6840 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6841 if (t1
!= 0 && t2
!= 0
6842 && TYPE_OVERFLOW_WRAPS (ctype
)
6843 && (code
== MULT_EXPR
6844 /* If not multiplication, we can only do this if both operands
6845 are divisible by c. */
6846 || (multiple_of_p (ctype
, op0
, c
)
6847 && multiple_of_p (ctype
, op1
, c
))))
6849 if (sub_strict_overflow_p
)
6850 *strict_overflow_p
= true;
6851 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6852 fold_convert (ctype
, t2
));
6855 /* If this was a subtraction, negate OP1 and set it to be an addition.
6856 This simplifies the logic below. */
6857 if (tcode
== MINUS_EXPR
)
6859 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6860 /* If OP1 was not easily negatable, the constant may be OP0. */
6861 if (TREE_CODE (op0
) == INTEGER_CST
)
6863 std::swap (op0
, op1
);
6868 if (TREE_CODE (op1
) != INTEGER_CST
)
6871 /* If either OP1 or C are negative, this optimization is not safe for
6872 some of the division and remainder types while for others we need
6873 to change the code. */
6874 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6876 if (code
== CEIL_DIV_EXPR
)
6877 code
= FLOOR_DIV_EXPR
;
6878 else if (code
== FLOOR_DIV_EXPR
)
6879 code
= CEIL_DIV_EXPR
;
6880 else if (code
!= MULT_EXPR
6881 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6885 /* If it's a multiply or a division/modulus operation of a multiple
6886 of our constant, do the operation and verify it doesn't overflow. */
6887 if (code
== MULT_EXPR
6888 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6891 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6892 fold_convert (ctype
, c
));
6893 /* We allow the constant to overflow with wrapping semantics. */
6895 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6901 /* If we have an unsigned type, we cannot widen the operation since it
6902 will change the result if the original computation overflowed. */
6903 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6906 /* The last case is if we are a multiply. In that case, we can
6907 apply the distributive law to commute the multiply and addition
6908 if the multiplication of the constants doesn't overflow
6909 and overflow is defined. With undefined overflow
6910 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6911 But fold_plusminus_mult_expr would factor back any power-of-two
6912 value so do not distribute in the first place in this case. */
6913 if (code
== MULT_EXPR
6914 && TYPE_OVERFLOW_WRAPS (ctype
)
6915 && !(tree_fits_shwi_p (c
) && pow2p_hwi (absu_hwi (tree_to_shwi (c
)))))
6916 return fold_build2 (tcode
, ctype
,
6917 fold_build2 (code
, ctype
,
6918 fold_convert (ctype
, op0
),
6919 fold_convert (ctype
, c
)),
6925 /* We have a special case here if we are doing something like
6926 (C * 8) % 4 since we know that's zero. */
6927 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6928 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6929 /* If the multiplication can overflow we cannot optimize this. */
6930 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6931 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6932 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6935 *strict_overflow_p
= true;
6936 return omit_one_operand (type
, integer_zero_node
, op0
);
6939 /* ... fall through ... */
6941 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6942 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6943 /* If we can extract our operation from the LHS, do so and return a
6944 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6945 do something only if the second operand is a constant. */
6947 && TYPE_OVERFLOW_WRAPS (ctype
)
6948 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6949 strict_overflow_p
)) != 0)
6950 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6951 fold_convert (ctype
, op1
));
6952 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6953 && TYPE_OVERFLOW_WRAPS (ctype
)
6954 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6955 strict_overflow_p
)) != 0)
6956 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6957 fold_convert (ctype
, t1
));
6958 else if (TREE_CODE (op1
) != INTEGER_CST
)
6961 /* If these are the same operation types, we can associate them
6962 assuming no overflow. */
6965 bool overflow_p
= false;
6966 wi::overflow_type overflow_mul
;
6967 signop sign
= TYPE_SIGN (ctype
);
6968 unsigned prec
= TYPE_PRECISION (ctype
);
6969 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6970 wi::to_wide (c
, prec
),
6971 sign
, &overflow_mul
);
6972 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6974 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6977 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6978 wide_int_to_tree (ctype
, mul
));
6981 /* If these operations "cancel" each other, we have the main
6982 optimizations of this pass, which occur when either constant is a
6983 multiple of the other, in which case we replace this with either an
6984 operation or CODE or TCODE.
6986 If we have an unsigned type, we cannot do this since it will change
6987 the result if the original computation overflowed. */
6988 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6989 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6990 || (tcode
== MULT_EXPR
6991 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6992 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6993 && code
!= MULT_EXPR
)))
6995 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6998 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6999 *strict_overflow_p
= true;
7000 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7001 fold_convert (ctype
,
7002 const_binop (TRUNC_DIV_EXPR
,
7005 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
7008 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
7009 *strict_overflow_p
= true;
7010 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
7011 fold_convert (ctype
,
7012 const_binop (TRUNC_DIV_EXPR
,
7025 /* Return a node which has the indicated constant VALUE (either 0 or
7026 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7027 and is of the indicated TYPE. */
7030 constant_boolean_node (bool value
, tree type
)
7032 if (type
== integer_type_node
)
7033 return value
? integer_one_node
: integer_zero_node
;
7034 else if (type
== boolean_type_node
)
7035 return value
? boolean_true_node
: boolean_false_node
;
7036 else if (TREE_CODE (type
) == VECTOR_TYPE
)
7037 return build_vector_from_val (type
,
7038 build_int_cst (TREE_TYPE (type
),
7041 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
7045 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7046 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7047 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7048 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7049 COND is the first argument to CODE; otherwise (as in the example
7050 given here), it is the second argument. TYPE is the type of the
7051 original expression. Return NULL_TREE if no simplification is
7055 fold_binary_op_with_conditional_arg (location_t loc
,
7056 enum tree_code code
,
7057 tree type
, tree op0
, tree op1
,
7058 tree cond
, tree arg
, int cond_first_p
)
7060 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
7061 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
7062 tree test
, true_value
, false_value
;
7063 tree lhs
= NULL_TREE
;
7064 tree rhs
= NULL_TREE
;
7065 enum tree_code cond_code
= COND_EXPR
;
7067 /* Do not move possibly trapping operations into the conditional as this
7068 pessimizes code and causes gimplification issues when applied late. */
7069 if (operation_could_trap_p (code
, FLOAT_TYPE_P (type
),
7070 ANY_INTEGRAL_TYPE_P (type
)
7071 && TYPE_OVERFLOW_TRAPS (type
), op1
))
7074 if (TREE_CODE (cond
) == COND_EXPR
7075 || TREE_CODE (cond
) == VEC_COND_EXPR
)
7077 test
= TREE_OPERAND (cond
, 0);
7078 true_value
= TREE_OPERAND (cond
, 1);
7079 false_value
= TREE_OPERAND (cond
, 2);
7080 /* If this operand throws an expression, then it does not make
7081 sense to try to perform a logical or arithmetic operation
7083 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
7085 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
7088 else if (!(TREE_CODE (type
) != VECTOR_TYPE
7089 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
7091 tree testtype
= TREE_TYPE (cond
);
7093 true_value
= constant_boolean_node (true, testtype
);
7094 false_value
= constant_boolean_node (false, testtype
);
7097 /* Detect the case of mixing vector and scalar types - bail out. */
7100 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
7101 cond_code
= VEC_COND_EXPR
;
7103 /* This transformation is only worthwhile if we don't have to wrap ARG
7104 in a SAVE_EXPR and the operation can be simplified without recursing
7105 on at least one of the branches once its pushed inside the COND_EXPR. */
7106 if (!TREE_CONSTANT (arg
)
7107 && (TREE_SIDE_EFFECTS (arg
)
7108 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
7109 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
7112 arg
= fold_convert_loc (loc
, arg_type
, arg
);
7115 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
7117 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
7119 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
7123 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
7125 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
7127 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
7130 /* Check that we have simplified at least one of the branches. */
7131 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
7134 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
7138 /* Subroutine of fold() that checks for the addition of +/- 0.0.
7140 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
7141 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
7142 ADDEND is the same as X.
7144 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7145 and finite. The problematic cases are when X is zero, and its mode
7146 has signed zeros. In the case of rounding towards -infinity,
7147 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7148 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7151 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
7153 if (!real_zerop (addend
))
7156 /* Don't allow the fold with -fsignaling-nans. */
7157 if (HONOR_SNANS (type
))
7160 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7161 if (!HONOR_SIGNED_ZEROS (type
))
7164 /* There is no case that is safe for all rounding modes. */
7165 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
7168 /* In a vector or complex, we would need to check the sign of all zeros. */
7169 if (TREE_CODE (addend
) == VECTOR_CST
)
7170 addend
= uniform_vector_p (addend
);
7171 if (!addend
|| TREE_CODE (addend
) != REAL_CST
)
7174 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7175 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
7178 /* The mode has signed zeros, and we have to honor their sign.
7179 In this situation, there is only one case we can return true for.
7180 X - 0 is the same as X with default rounding. */
7184 /* Subroutine of match.pd that optimizes comparisons of a division by
7185 a nonzero integer constant against an integer constant, i.e.
7188 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7189 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7192 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
7193 tree
*hi
, bool *neg_overflow
)
7195 tree prod
, tmp
, type
= TREE_TYPE (c1
);
7196 signop sign
= TYPE_SIGN (type
);
7197 wi::overflow_type overflow
;
7199 /* We have to do this the hard way to detect unsigned overflow.
7200 prod = int_const_binop (MULT_EXPR, c1, c2); */
7201 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
7202 prod
= force_fit_type (type
, val
, -1, overflow
);
7203 *neg_overflow
= false;
7205 if (sign
== UNSIGNED
)
7207 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7210 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7211 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
7212 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
7214 else if (tree_int_cst_sgn (c1
) >= 0)
7216 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7217 switch (tree_int_cst_sgn (c2
))
7220 *neg_overflow
= true;
7221 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7226 *lo
= fold_negate_const (tmp
, type
);
7231 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7241 /* A negative divisor reverses the relational operators. */
7242 code
= swap_tree_comparison (code
);
7244 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
7245 switch (tree_int_cst_sgn (c2
))
7248 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7253 *hi
= fold_negate_const (tmp
, type
);
7258 *neg_overflow
= true;
7259 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7268 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
7271 if (TREE_OVERFLOW (*lo
)
7272 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
7274 if (TREE_OVERFLOW (*hi
)
7275 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
7282 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7283 equality/inequality test, then return a simplified form of the test
7284 using a sign testing. Otherwise return NULL. TYPE is the desired
7288 fold_single_bit_test_into_sign_test (location_t loc
,
7289 enum tree_code code
, tree arg0
, tree arg1
,
7292 /* If this is testing a single bit, we can optimize the test. */
7293 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7294 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7295 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7297 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7298 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7299 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
7301 if (arg00
!= NULL_TREE
7302 /* This is only a win if casting to a signed type is cheap,
7303 i.e. when arg00's type is not a partial mode. */
7304 && type_has_mode_precision_p (TREE_TYPE (arg00
)))
7306 tree stype
= signed_type_for (TREE_TYPE (arg00
));
7307 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
7309 fold_convert_loc (loc
, stype
, arg00
),
7310 build_int_cst (stype
, 0));
7317 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7318 equality/inequality test, then return a simplified form of
7319 the test using shifts and logical operations. Otherwise return
7320 NULL. TYPE is the desired result type. */
7323 fold_single_bit_test (location_t loc
, enum tree_code code
,
7324 tree arg0
, tree arg1
, tree result_type
)
7326 /* If this is testing a single bit, we can optimize the test. */
7327 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
7328 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
7329 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
7331 tree inner
= TREE_OPERAND (arg0
, 0);
7332 tree type
= TREE_TYPE (arg0
);
7333 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
7334 scalar_int_mode operand_mode
= SCALAR_INT_TYPE_MODE (type
);
7336 tree signed_type
, unsigned_type
, intermediate_type
;
7339 /* First, see if we can fold the single bit test into a sign-bit
7341 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
7346 /* Otherwise we have (A & C) != 0 where C is a single bit,
7347 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7348 Similarly for (A & C) == 0. */
7350 /* If INNER is a right shift of a constant and it plus BITNUM does
7351 not overflow, adjust BITNUM and INNER. */
7352 if (TREE_CODE (inner
) == RSHIFT_EXPR
7353 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
7354 && bitnum
< TYPE_PRECISION (type
)
7355 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner
, 1)),
7356 TYPE_PRECISION (type
) - bitnum
))
7358 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
7359 inner
= TREE_OPERAND (inner
, 0);
7362 /* If we are going to be able to omit the AND below, we must do our
7363 operations as unsigned. If we must use the AND, we have a choice.
7364 Normally unsigned is faster, but for some machines signed is. */
7365 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
7366 && !flag_syntax_only
) ? 0 : 1;
7368 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
7369 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
7370 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
7371 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
7374 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
7375 inner
, size_int (bitnum
));
7377 one
= build_int_cst (intermediate_type
, 1);
7379 if (code
== EQ_EXPR
)
7380 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
7382 /* Put the AND last so it can combine with more things. */
7383 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
7385 /* Make sure to return the proper type. */
7386 inner
= fold_convert_loc (loc
, result_type
, inner
);
7393 /* Test whether it is preferable two swap two operands, ARG0 and
7394 ARG1, for example because ARG0 is an integer constant and ARG1
7398 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
7400 if (CONSTANT_CLASS_P (arg1
))
7402 if (CONSTANT_CLASS_P (arg0
))
7408 if (TREE_CONSTANT (arg1
))
7410 if (TREE_CONSTANT (arg0
))
7413 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7414 for commutative and comparison operators. Ensuring a canonical
7415 form allows the optimizers to find additional redundancies without
7416 having to explicitly check for both orderings. */
7417 if (TREE_CODE (arg0
) == SSA_NAME
7418 && TREE_CODE (arg1
) == SSA_NAME
7419 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
7422 /* Put SSA_NAMEs last. */
7423 if (TREE_CODE (arg1
) == SSA_NAME
)
7425 if (TREE_CODE (arg0
) == SSA_NAME
)
7428 /* Put variables last. */
7438 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7439 means A >= Y && A != MAX, but in this case we know that
7440 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7443 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7445 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
7447 if (TREE_CODE (bound
) == LT_EXPR
)
7448 a
= TREE_OPERAND (bound
, 0);
7449 else if (TREE_CODE (bound
) == GT_EXPR
)
7450 a
= TREE_OPERAND (bound
, 1);
7454 typea
= TREE_TYPE (a
);
7455 if (!INTEGRAL_TYPE_P (typea
)
7456 && !POINTER_TYPE_P (typea
))
7459 if (TREE_CODE (ineq
) == LT_EXPR
)
7461 a1
= TREE_OPERAND (ineq
, 1);
7462 y
= TREE_OPERAND (ineq
, 0);
7464 else if (TREE_CODE (ineq
) == GT_EXPR
)
7466 a1
= TREE_OPERAND (ineq
, 0);
7467 y
= TREE_OPERAND (ineq
, 1);
7472 if (TREE_TYPE (a1
) != typea
)
7475 if (POINTER_TYPE_P (typea
))
7477 /* Convert the pointer types into integer before taking the difference. */
7478 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7479 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7480 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7483 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7485 if (!diff
|| !integer_onep (diff
))
7488 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7491 /* Fold a sum or difference of at least one multiplication.
7492 Returns the folded tree or NULL if no simplification could be made. */
7495 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7496 tree arg0
, tree arg1
)
7498 tree arg00
, arg01
, arg10
, arg11
;
7499 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7501 /* (A * C) +- (B * C) -> (A+-B) * C.
7502 (A * C) +- A -> A * (C+-1).
7503 We are most concerned about the case where C is a constant,
7504 but other combinations show up during loop reduction. Since
7505 it is not difficult, try all four possibilities. */
7507 if (TREE_CODE (arg0
) == MULT_EXPR
)
7509 arg00
= TREE_OPERAND (arg0
, 0);
7510 arg01
= TREE_OPERAND (arg0
, 1);
7512 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7514 arg00
= build_one_cst (type
);
7519 /* We cannot generate constant 1 for fract. */
7520 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7523 arg01
= build_one_cst (type
);
7525 if (TREE_CODE (arg1
) == MULT_EXPR
)
7527 arg10
= TREE_OPERAND (arg1
, 0);
7528 arg11
= TREE_OPERAND (arg1
, 1);
7530 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7532 arg10
= build_one_cst (type
);
7533 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7534 the purpose of this canonicalization. */
7535 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7536 && negate_expr_p (arg1
)
7537 && code
== PLUS_EXPR
)
7539 arg11
= negate_expr (arg1
);
7547 /* We cannot generate constant 1 for fract. */
7548 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7551 arg11
= build_one_cst (type
);
7555 /* Prefer factoring a common non-constant. */
7556 if (operand_equal_p (arg00
, arg10
, 0))
7557 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7558 else if (operand_equal_p (arg01
, arg11
, 0))
7559 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7560 else if (operand_equal_p (arg00
, arg11
, 0))
7561 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7562 else if (operand_equal_p (arg01
, arg10
, 0))
7563 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7565 /* No identical multiplicands; see if we can find a common
7566 power-of-two factor in non-power-of-two multiplies. This
7567 can help in multi-dimensional array access. */
7568 else if (tree_fits_shwi_p (arg01
) && tree_fits_shwi_p (arg11
))
7570 HOST_WIDE_INT int01
= tree_to_shwi (arg01
);
7571 HOST_WIDE_INT int11
= tree_to_shwi (arg11
);
7576 /* Move min of absolute values to int11. */
7577 if (absu_hwi (int01
) < absu_hwi (int11
))
7579 tmp
= int01
, int01
= int11
, int11
= tmp
;
7580 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7587 const unsigned HOST_WIDE_INT factor
= absu_hwi (int11
);
7589 && pow2p_hwi (factor
)
7590 && (int01
& (factor
- 1)) == 0
7591 /* The remainder should not be a constant, otherwise we
7592 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7593 increased the number of multiplications necessary. */
7594 && TREE_CODE (arg10
) != INTEGER_CST
)
7596 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7597 build_int_cst (TREE_TYPE (arg00
),
7602 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7609 if (! ANY_INTEGRAL_TYPE_P (type
)
7610 || TYPE_OVERFLOW_WRAPS (type
)
7611 /* We are neither factoring zero nor minus one. */
7612 || TREE_CODE (same
) == INTEGER_CST
)
7613 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7614 fold_build2_loc (loc
, code
, type
,
7615 fold_convert_loc (loc
, type
, alt0
),
7616 fold_convert_loc (loc
, type
, alt1
)),
7617 fold_convert_loc (loc
, type
, same
));
7619 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7620 same may be minus one and thus the multiplication may overflow. Perform
7621 the sum operation in an unsigned type. */
7622 tree utype
= unsigned_type_for (type
);
7623 tree tem
= fold_build2_loc (loc
, code
, utype
,
7624 fold_convert_loc (loc
, utype
, alt0
),
7625 fold_convert_loc (loc
, utype
, alt1
));
7626 /* If the sum evaluated to a constant that is not -INF the multiplication
7628 if (TREE_CODE (tem
) == INTEGER_CST
7629 && (wi::to_wide (tem
)
7630 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7631 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7632 fold_convert (type
, tem
), same
);
7634 /* Do not resort to unsigned multiplication because
7635 we lose the no-overflow property of the expression. */
7639 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7640 specified by EXPR into the buffer PTR of length LEN bytes.
7641 Return the number of bytes placed in the buffer, or zero
7645 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7647 tree type
= TREE_TYPE (expr
);
7648 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7649 int byte
, offset
, word
, words
;
7650 unsigned char value
;
7652 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7659 return MIN (len
, total_bytes
- off
);
7661 words
= total_bytes
/ UNITS_PER_WORD
;
7663 for (byte
= 0; byte
< total_bytes
; byte
++)
7665 int bitpos
= byte
* BITS_PER_UNIT
;
7666 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7668 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7670 if (total_bytes
> UNITS_PER_WORD
)
7672 word
= byte
/ UNITS_PER_WORD
;
7673 if (WORDS_BIG_ENDIAN
)
7674 word
= (words
- 1) - word
;
7675 offset
= word
* UNITS_PER_WORD
;
7676 if (BYTES_BIG_ENDIAN
)
7677 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7679 offset
+= byte
% UNITS_PER_WORD
;
7682 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7683 if (offset
>= off
&& offset
- off
< len
)
7684 ptr
[offset
- off
] = value
;
7686 return MIN (len
, total_bytes
- off
);
7690 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7691 specified by EXPR into the buffer PTR of length LEN bytes.
7692 Return the number of bytes placed in the buffer, or zero
7696 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7698 tree type
= TREE_TYPE (expr
);
7699 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7700 int total_bytes
= GET_MODE_SIZE (mode
);
7701 FIXED_VALUE_TYPE value
;
7702 tree i_value
, i_type
;
7704 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7707 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7709 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7712 value
= TREE_FIXED_CST (expr
);
7713 i_value
= double_int_to_tree (i_type
, value
.data
);
7715 return native_encode_int (i_value
, ptr
, len
, off
);
7719 /* Subroutine of native_encode_expr. Encode the REAL_CST
7720 specified by EXPR into the buffer PTR of length LEN bytes.
7721 Return the number of bytes placed in the buffer, or zero
7725 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7727 tree type
= TREE_TYPE (expr
);
7728 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7729 int byte
, offset
, word
, words
, bitpos
;
7730 unsigned char value
;
7732 /* There are always 32 bits in each long, no matter the size of
7733 the hosts long. We handle floating point representations with
7737 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7744 return MIN (len
, total_bytes
- off
);
7746 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7748 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7750 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7751 bitpos
+= BITS_PER_UNIT
)
7753 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7754 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7756 if (UNITS_PER_WORD
< 4)
7758 word
= byte
/ UNITS_PER_WORD
;
7759 if (WORDS_BIG_ENDIAN
)
7760 word
= (words
- 1) - word
;
7761 offset
= word
* UNITS_PER_WORD
;
7762 if (BYTES_BIG_ENDIAN
)
7763 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7765 offset
+= byte
% UNITS_PER_WORD
;
7770 if (BYTES_BIG_ENDIAN
)
7772 /* Reverse bytes within each long, or within the entire float
7773 if it's smaller than a long (for HFmode). */
7774 offset
= MIN (3, total_bytes
- 1) - offset
;
7775 gcc_assert (offset
>= 0);
7778 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7780 && offset
- off
< len
)
7781 ptr
[offset
- off
] = value
;
7783 return MIN (len
, total_bytes
- off
);
7786 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7787 specified by EXPR into the buffer PTR of length LEN bytes.
7788 Return the number of bytes placed in the buffer, or zero
7792 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7797 part
= TREE_REALPART (expr
);
7798 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7799 if (off
== -1 && rsize
== 0)
7801 part
= TREE_IMAGPART (expr
);
7803 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7804 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7806 if (off
== -1 && isize
!= rsize
)
7808 return rsize
+ isize
;
7811 /* Like native_encode_vector, but only encode the first COUNT elements.
7812 The other arguments are as for native_encode_vector. */
7815 native_encode_vector_part (const_tree expr
, unsigned char *ptr
, int len
,
7816 int off
, unsigned HOST_WIDE_INT count
)
7818 tree itype
= TREE_TYPE (TREE_TYPE (expr
));
7819 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr
))
7820 && TYPE_PRECISION (itype
) <= BITS_PER_UNIT
)
7822 /* This is the only case in which elements can be smaller than a byte.
7823 Element 0 is always in the lsb of the containing byte. */
7824 unsigned int elt_bits
= TYPE_PRECISION (itype
);
7825 int total_bytes
= CEIL (elt_bits
* count
, BITS_PER_UNIT
);
7826 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7832 /* Zero the buffer and then set bits later where necessary. */
7833 int extract_bytes
= MIN (len
, total_bytes
- off
);
7835 memset (ptr
, 0, extract_bytes
);
7837 unsigned int elts_per_byte
= BITS_PER_UNIT
/ elt_bits
;
7838 unsigned int first_elt
= off
* elts_per_byte
;
7839 unsigned int extract_elts
= extract_bytes
* elts_per_byte
;
7840 for (unsigned int i
= 0; i
< extract_elts
; ++i
)
7842 tree elt
= VECTOR_CST_ELT (expr
, first_elt
+ i
);
7843 if (TREE_CODE (elt
) != INTEGER_CST
)
7846 if (ptr
&& wi::extract_uhwi (wi::to_wide (elt
), 0, 1))
7848 unsigned int bit
= i
* elt_bits
;
7849 ptr
[bit
/ BITS_PER_UNIT
] |= 1 << (bit
% BITS_PER_UNIT
);
7852 return extract_bytes
;
7856 int size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7857 for (unsigned HOST_WIDE_INT i
= 0; i
< count
; i
++)
7864 tree elem
= VECTOR_CST_ELT (expr
, i
);
7865 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7867 if ((off
== -1 && res
!= size
) || res
== 0)
7871 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
7878 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7879 specified by EXPR into the buffer PTR of length LEN bytes.
7880 Return the number of bytes placed in the buffer, or zero
7884 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7886 unsigned HOST_WIDE_INT count
;
7887 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
7889 return native_encode_vector_part (expr
, ptr
, len
, off
, count
);
7893 /* Subroutine of native_encode_expr. Encode the STRING_CST
7894 specified by EXPR into the buffer PTR of length LEN bytes.
7895 Return the number of bytes placed in the buffer, or zero
7899 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7901 tree type
= TREE_TYPE (expr
);
7903 /* Wide-char strings are encoded in target byte-order so native
7904 encoding them is trivial. */
7905 if (BITS_PER_UNIT
!= CHAR_BIT
7906 || TREE_CODE (type
) != ARRAY_TYPE
7907 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7908 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7911 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
7912 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7916 len
= MIN (total_bytes
- off
, len
);
7922 if (off
< TREE_STRING_LENGTH (expr
))
7924 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7925 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7927 memset (ptr
+ written
, 0, len
- written
);
7933 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7934 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7935 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7936 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7937 sufficient to encode the entire EXPR, or if OFF is out of bounds.
7938 Otherwise, start at byte offset OFF and encode at most LEN bytes.
7939 Return the number of bytes placed in the buffer, or zero upon failure. */
7942 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7944 /* We don't support starting at negative offset and -1 is special. */
7948 switch (TREE_CODE (expr
))
7951 return native_encode_int (expr
, ptr
, len
, off
);
7954 return native_encode_real (expr
, ptr
, len
, off
);
7957 return native_encode_fixed (expr
, ptr
, len
, off
);
7960 return native_encode_complex (expr
, ptr
, len
, off
);
7963 return native_encode_vector (expr
, ptr
, len
, off
);
7966 return native_encode_string (expr
, ptr
, len
, off
);
7973 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7974 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7975 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7976 machine modes, we can't just use build_nonstandard_integer_type. */
7979 find_bitfield_repr_type (int fieldsize
, int len
)
7982 for (int pass
= 0; pass
< 2; pass
++)
7984 enum mode_class mclass
= pass
? MODE_PARTIAL_INT
: MODE_INT
;
7985 FOR_EACH_MODE_IN_CLASS (mode
, mclass
)
7986 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
7987 && known_eq (GET_MODE_PRECISION (mode
),
7988 GET_MODE_BITSIZE (mode
))
7989 && known_le (GET_MODE_SIZE (mode
), len
))
7991 tree ret
= lang_hooks
.types
.type_for_mode (mode
, 1);
7992 if (ret
&& TYPE_MODE (ret
) == mode
)
7997 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
7998 if (int_n_enabled_p
[i
]
7999 && int_n_data
[i
].bitsize
>= (unsigned) (BITS_PER_UNIT
* fieldsize
)
8000 && int_n_trees
[i
].unsigned_type
)
8002 tree ret
= int_n_trees
[i
].unsigned_type
;
8003 mode
= TYPE_MODE (ret
);
8004 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8005 && known_eq (GET_MODE_PRECISION (mode
),
8006 GET_MODE_BITSIZE (mode
))
8007 && known_le (GET_MODE_SIZE (mode
), len
))
8014 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8015 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8016 to be non-NULL and OFF zero), then in addition to filling the
8017 bytes pointed by PTR with the value also clear any bits pointed
8018 by MASK that are known to be initialized, keep them as is for
8019 e.g. uninitialized padding bits or uninitialized fields. */
8022 native_encode_initializer (tree init
, unsigned char *ptr
, int len
,
8023 int off
, unsigned char *mask
)
8027 /* We don't support starting at negative offset and -1 is special. */
8028 if (off
< -1 || init
== NULL_TREE
)
8031 gcc_assert (mask
== NULL
|| (off
== 0 && ptr
));
8034 switch (TREE_CODE (init
))
8036 case VIEW_CONVERT_EXPR
:
8037 case NON_LVALUE_EXPR
:
8038 return native_encode_initializer (TREE_OPERAND (init
, 0), ptr
, len
, off
,
8041 r
= native_encode_expr (init
, ptr
, len
, off
);
8043 memset (mask
, 0, r
);
8046 tree type
= TREE_TYPE (init
);
8047 HOST_WIDE_INT total_bytes
= int_size_in_bytes (type
);
8048 if (total_bytes
< 0)
8050 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8052 int o
= off
== -1 ? 0 : off
;
8053 if (TREE_CODE (type
) == ARRAY_TYPE
)
8056 unsigned HOST_WIDE_INT cnt
;
8057 HOST_WIDE_INT curpos
= 0, fieldsize
, valueinit
= -1;
8058 constructor_elt
*ce
;
8060 if (!TYPE_DOMAIN (type
)
8061 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
8064 fieldsize
= int_size_in_bytes (TREE_TYPE (type
));
8068 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
8070 memset (ptr
, '\0', MIN (total_bytes
- off
, len
));
8072 for (cnt
= 0; ; cnt
++)
8074 tree val
= NULL_TREE
, index
= NULL_TREE
;
8075 HOST_WIDE_INT pos
= curpos
, count
= 0;
8077 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8082 else if (mask
== NULL
8083 || CONSTRUCTOR_NO_CLEARING (init
)
8084 || curpos
>= total_bytes
)
8089 if (index
&& TREE_CODE (index
) == RANGE_EXPR
)
8091 if (TREE_CODE (TREE_OPERAND (index
, 0)) != INTEGER_CST
8092 || TREE_CODE (TREE_OPERAND (index
, 1)) != INTEGER_CST
)
8095 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 0))
8096 - wi::to_offset (min_index
),
8097 TYPE_PRECISION (sizetype
));
8099 if (!wi::fits_shwi_p (pos
))
8101 pos
= wpos
.to_shwi ();
8103 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 1))
8104 - wi::to_offset (TREE_OPERAND (index
, 0)),
8105 TYPE_PRECISION (sizetype
));
8106 if (!wi::fits_shwi_p (wcount
))
8108 count
= wcount
.to_shwi ();
8112 if (TREE_CODE (index
) != INTEGER_CST
)
8115 = wi::sext (wi::to_offset (index
)
8116 - wi::to_offset (min_index
),
8117 TYPE_PRECISION (sizetype
));
8119 if (!wi::fits_shwi_p (wpos
))
8121 pos
= wpos
.to_shwi ();
8124 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
) && curpos
!= pos
)
8126 if (valueinit
== -1)
8128 tree zero
= build_zero_cst (TREE_TYPE (type
));
8129 r
= native_encode_initializer (zero
, ptr
+ curpos
,
8132 if (TREE_CODE (zero
) == CONSTRUCTOR
)
8137 curpos
+= fieldsize
;
8139 while (curpos
!= pos
)
8141 memcpy (ptr
+ curpos
, ptr
+ valueinit
, fieldsize
);
8142 memcpy (mask
+ curpos
, mask
+ valueinit
, fieldsize
);
8143 curpos
+= fieldsize
;
8153 && (curpos
+ fieldsize
8154 <= (HOST_WIDE_INT
) off
+ len
)))
8159 memcpy (ptr
+ (curpos
- o
), ptr
+ (pos
- o
),
8162 memcpy (mask
+ curpos
, mask
+ pos
, fieldsize
);
8164 else if (!native_encode_initializer (val
,
8181 else if (curpos
+ fieldsize
> off
8182 && curpos
< (HOST_WIDE_INT
) off
+ len
)
8184 /* Partial overlap. */
8185 unsigned char *p
= NULL
;
8188 gcc_assert (mask
== NULL
);
8192 p
= ptr
+ curpos
- off
;
8193 l
= MIN ((HOST_WIDE_INT
) off
+ len
- curpos
,
8202 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8205 curpos
+= fieldsize
;
8207 while (count
-- != 0);
8209 return MIN (total_bytes
- off
, len
);
8211 else if (TREE_CODE (type
) == RECORD_TYPE
8212 || TREE_CODE (type
) == UNION_TYPE
)
8214 unsigned HOST_WIDE_INT cnt
;
8215 constructor_elt
*ce
;
8216 tree fld_base
= TYPE_FIELDS (type
);
8217 tree to_free
= NULL_TREE
;
8219 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
|| mask
== NULL
);
8221 memset (ptr
, '\0', MIN (total_bytes
- o
, len
));
8222 for (cnt
= 0; ; cnt
++)
8224 tree val
= NULL_TREE
, field
= NULL_TREE
;
8225 HOST_WIDE_INT pos
= 0, fieldsize
;
8226 unsigned HOST_WIDE_INT bpos
= 0, epos
= 0;
8231 to_free
= NULL_TREE
;
8234 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8238 if (field
== NULL_TREE
)
8241 pos
= int_byte_position (field
);
8242 if (off
!= -1 && (HOST_WIDE_INT
) off
+ len
<= pos
)
8245 else if (mask
== NULL
8246 || CONSTRUCTOR_NO_CLEARING (init
))
8251 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
))
8254 for (fld
= fld_base
; fld
; fld
= DECL_CHAIN (fld
))
8256 if (TREE_CODE (fld
) != FIELD_DECL
)
8260 if (DECL_PADDING_P (fld
))
8262 if (DECL_SIZE_UNIT (fld
) == NULL_TREE
8263 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld
)))
8265 if (integer_zerop (DECL_SIZE_UNIT (fld
)))
8269 if (fld
== NULL_TREE
)
8275 fld_base
= DECL_CHAIN (fld
);
8280 pos
= int_byte_position (field
);
8281 val
= build_zero_cst (TREE_TYPE (fld
));
8282 if (TREE_CODE (val
) == CONSTRUCTOR
)
8287 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
8288 && TYPE_DOMAIN (TREE_TYPE (field
))
8289 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field
))))
8291 if (mask
|| off
!= -1)
8293 if (val
== NULL_TREE
)
8295 if (TREE_CODE (TREE_TYPE (val
)) != ARRAY_TYPE
)
8297 fieldsize
= int_size_in_bytes (TREE_TYPE (val
));
8299 || (int) fieldsize
!= fieldsize
8300 || (pos
+ fieldsize
) > INT_MAX
)
8302 if (pos
+ fieldsize
> total_bytes
)
8304 if (ptr
!= NULL
&& total_bytes
< len
)
8305 memset (ptr
+ total_bytes
, '\0',
8306 MIN (pos
+ fieldsize
, len
) - total_bytes
);
8307 total_bytes
= pos
+ fieldsize
;
8312 if (DECL_SIZE_UNIT (field
) == NULL_TREE
8313 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field
)))
8315 fieldsize
= tree_to_shwi (DECL_SIZE_UNIT (field
));
8320 if (DECL_BIT_FIELD (field
))
8322 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8324 fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8325 bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8326 if (bpos
% BITS_PER_UNIT
)
8327 bpos
%= BITS_PER_UNIT
;
8331 epos
= fieldsize
% BITS_PER_UNIT
;
8332 fieldsize
+= BITS_PER_UNIT
- 1;
8333 fieldsize
/= BITS_PER_UNIT
;
8336 if (off
!= -1 && pos
+ fieldsize
<= off
)
8339 if (val
== NULL_TREE
)
8342 if (DECL_BIT_FIELD (field
))
8344 /* FIXME: Handle PDP endian. */
8345 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8348 if (TREE_CODE (val
) != INTEGER_CST
)
8351 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8352 tree repr_type
= NULL_TREE
;
8353 HOST_WIDE_INT rpos
= 0;
8354 if (repr
&& INTEGRAL_TYPE_P (TREE_TYPE (repr
)))
8356 rpos
= int_byte_position (repr
);
8357 repr_type
= TREE_TYPE (repr
);
8361 repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8362 if (repr_type
== NULL_TREE
)
8364 HOST_WIDE_INT repr_size
= int_size_in_bytes (repr_type
);
8365 gcc_assert (repr_size
> 0 && repr_size
<= len
);
8366 if (pos
+ repr_size
<= o
+ len
)
8370 rpos
= o
+ len
- repr_size
;
8371 gcc_assert (rpos
<= pos
);
8377 wide_int w
= wi::to_wide (val
, TYPE_PRECISION (repr_type
));
8378 int diff
= (TYPE_PRECISION (repr_type
)
8379 - TYPE_PRECISION (TREE_TYPE (field
)));
8380 HOST_WIDE_INT bitoff
= (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8381 if (!BYTES_BIG_ENDIAN
)
8382 w
= wi::lshift (w
, bitoff
);
8384 w
= wi::lshift (w
, diff
- bitoff
);
8385 val
= wide_int_to_tree (repr_type
, w
);
8387 unsigned char buf
[MAX_BITSIZE_MODE_ANY_INT
8388 / BITS_PER_UNIT
+ 1];
8389 int l
= native_encode_int (val
, buf
, sizeof buf
, 0);
8390 if (l
* BITS_PER_UNIT
!= TYPE_PRECISION (repr_type
))
8396 /* If the bitfield does not start at byte boundary, handle
8397 the partial byte at the start. */
8399 && (off
== -1 || (pos
>= off
&& len
>= 1)))
8401 if (!BYTES_BIG_ENDIAN
)
8403 int msk
= (1 << bpos
) - 1;
8404 buf
[pos
- rpos
] &= ~msk
;
8405 buf
[pos
- rpos
] |= ptr
[pos
- o
] & msk
;
8408 if (fieldsize
> 1 || epos
== 0)
8411 mask
[pos
] &= (msk
| ~((1 << epos
) - 1));
8416 int msk
= (1 << (BITS_PER_UNIT
- bpos
)) - 1;
8417 buf
[pos
- rpos
] &= msk
;
8418 buf
[pos
- rpos
] |= ptr
[pos
- o
] & ~msk
;
8421 if (fieldsize
> 1 || epos
== 0)
8425 | ((1 << (BITS_PER_UNIT
- epos
))
8430 /* If the bitfield does not end at byte boundary, handle
8431 the partial byte at the end. */
8434 || pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
))
8436 if (!BYTES_BIG_ENDIAN
)
8438 int msk
= (1 << epos
) - 1;
8439 buf
[pos
- rpos
+ fieldsize
- 1] &= msk
;
8440 buf
[pos
- rpos
+ fieldsize
- 1]
8441 |= ptr
[pos
+ fieldsize
- 1 - o
] & ~msk
;
8442 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8443 mask
[pos
+ fieldsize
- 1] &= ~msk
;
8447 int msk
= (1 << (BITS_PER_UNIT
- epos
)) - 1;
8448 buf
[pos
- rpos
+ fieldsize
- 1] &= ~msk
;
8449 buf
[pos
- rpos
+ fieldsize
- 1]
8450 |= ptr
[pos
+ fieldsize
- 1 - o
] & msk
;
8451 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8452 mask
[pos
+ fieldsize
- 1] &= msk
;
8457 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8459 memcpy (ptr
+ pos
- o
, buf
+ (pos
- rpos
), fieldsize
);
8460 if (mask
&& (fieldsize
> (bpos
!= 0) + (epos
!= 0)))
8461 memset (mask
+ pos
+ (bpos
!= 0), 0,
8462 fieldsize
- (bpos
!= 0) - (epos
!= 0));
8466 /* Partial overlap. */
8467 HOST_WIDE_INT fsz
= fieldsize
;
8468 gcc_assert (mask
== NULL
);
8474 if (pos
+ fsz
> (HOST_WIDE_INT
) off
+ len
)
8475 fsz
= (HOST_WIDE_INT
) off
+ len
- pos
;
8476 memcpy (ptr
+ pos
- off
, buf
+ (pos
- rpos
), fsz
);
8483 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8485 int fldsize
= fieldsize
;
8488 tree fld
= DECL_CHAIN (field
);
8491 if (TREE_CODE (fld
) == FIELD_DECL
)
8493 fld
= DECL_CHAIN (fld
);
8495 if (fld
== NULL_TREE
)
8496 fldsize
= len
- pos
;
8498 r
= native_encode_initializer (val
, ptr
? ptr
+ pos
- o
8502 mask
? mask
+ pos
: NULL
);
8506 && fldsize
!= fieldsize
8508 && pos
+ r
> total_bytes
)
8509 total_bytes
= pos
+ r
;
8513 /* Partial overlap. */
8514 unsigned char *p
= NULL
;
8517 gcc_assert (mask
== NULL
);
8521 p
= ptr
+ pos
- off
;
8522 l
= MIN ((HOST_WIDE_INT
) off
+ len
- pos
,
8531 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8535 return MIN (total_bytes
- off
, len
);
8542 /* Subroutine of native_interpret_expr. Interpret the contents of
8543 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8544 If the buffer cannot be interpreted, return NULL_TREE. */
8547 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
8549 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8551 if (total_bytes
> len
8552 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8555 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
8557 return wide_int_to_tree (type
, result
);
8561 /* Subroutine of native_interpret_expr. Interpret the contents of
8562 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8563 If the buffer cannot be interpreted, return NULL_TREE. */
8566 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
8568 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
8569 int total_bytes
= GET_MODE_SIZE (mode
);
8571 FIXED_VALUE_TYPE fixed_value
;
8573 if (total_bytes
> len
8574 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8577 result
= double_int::from_buffer (ptr
, total_bytes
);
8578 fixed_value
= fixed_from_double_int (result
, mode
);
8580 return build_fixed (type
, fixed_value
);
8584 /* Subroutine of native_interpret_expr. Interpret the contents of
8585 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8586 If the buffer cannot be interpreted, return NULL_TREE. */
8589 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
8591 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8592 int total_bytes
= GET_MODE_SIZE (mode
);
8593 unsigned char value
;
8594 /* There are always 32 bits in each long, no matter the size of
8595 the hosts long. We handle floating point representations with
8600 if (total_bytes
> len
|| total_bytes
> 24)
8602 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
8604 memset (tmp
, 0, sizeof (tmp
));
8605 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
8606 bitpos
+= BITS_PER_UNIT
)
8608 /* Both OFFSET and BYTE index within a long;
8609 bitpos indexes the whole float. */
8610 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
8611 if (UNITS_PER_WORD
< 4)
8613 int word
= byte
/ UNITS_PER_WORD
;
8614 if (WORDS_BIG_ENDIAN
)
8615 word
= (words
- 1) - word
;
8616 offset
= word
* UNITS_PER_WORD
;
8617 if (BYTES_BIG_ENDIAN
)
8618 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
8620 offset
+= byte
% UNITS_PER_WORD
;
8625 if (BYTES_BIG_ENDIAN
)
8627 /* Reverse bytes within each long, or within the entire float
8628 if it's smaller than a long (for HFmode). */
8629 offset
= MIN (3, total_bytes
- 1) - offset
;
8630 gcc_assert (offset
>= 0);
8633 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
8635 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
8638 real_from_target (&r
, tmp
, mode
);
8639 tree ret
= build_real (type
, r
);
8640 if (MODE_COMPOSITE_P (mode
))
8642 /* For floating point values in composite modes, punt if this folding
8643 doesn't preserve bit representation. As the mode doesn't have fixed
8644 precision while GCC pretends it does, there could be valid values that
8645 GCC can't really represent accurately. See PR95450. */
8646 unsigned char buf
[24];
8647 if (native_encode_expr (ret
, buf
, total_bytes
, 0) != total_bytes
8648 || memcmp (ptr
, buf
, total_bytes
) != 0)
8655 /* Subroutine of native_interpret_expr. Interpret the contents of
8656 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8657 If the buffer cannot be interpreted, return NULL_TREE. */
8660 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
8662 tree etype
, rpart
, ipart
;
8665 etype
= TREE_TYPE (type
);
8666 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8669 rpart
= native_interpret_expr (etype
, ptr
, size
);
8672 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
8675 return build_complex (type
, rpart
, ipart
);
8678 /* Read a vector of type TYPE from the target memory image given by BYTES,
8679 which contains LEN bytes. The vector is known to be encodable using
8680 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8682 Return the vector on success, otherwise return null. */
8685 native_interpret_vector_part (tree type
, const unsigned char *bytes
,
8686 unsigned int len
, unsigned int npatterns
,
8687 unsigned int nelts_per_pattern
)
8689 tree elt_type
= TREE_TYPE (type
);
8690 if (VECTOR_BOOLEAN_TYPE_P (type
)
8691 && TYPE_PRECISION (elt_type
) <= BITS_PER_UNIT
)
8693 /* This is the only case in which elements can be smaller than a byte.
8694 Element 0 is always in the lsb of the containing byte. */
8695 unsigned int elt_bits
= TYPE_PRECISION (elt_type
);
8696 if (elt_bits
* npatterns
* nelts_per_pattern
> len
* BITS_PER_UNIT
)
8699 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8700 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8702 unsigned int bit_index
= i
* elt_bits
;
8703 unsigned int byte_index
= bit_index
/ BITS_PER_UNIT
;
8704 unsigned int lsb
= bit_index
% BITS_PER_UNIT
;
8705 builder
.quick_push (bytes
[byte_index
] & (1 << lsb
)
8706 ? build_all_ones_cst (elt_type
)
8707 : build_zero_cst (elt_type
));
8709 return builder
.build ();
8712 unsigned int elt_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (elt_type
));
8713 if (elt_bytes
* npatterns
* nelts_per_pattern
> len
)
8716 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8717 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8719 tree elt
= native_interpret_expr (elt_type
, bytes
, elt_bytes
);
8722 builder
.quick_push (elt
);
8725 return builder
.build ();
8728 /* Subroutine of native_interpret_expr. Interpret the contents of
8729 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8730 If the buffer cannot be interpreted, return NULL_TREE. */
8733 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
8737 unsigned HOST_WIDE_INT count
;
8739 etype
= TREE_TYPE (type
);
8740 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8741 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&count
)
8742 || size
* count
> len
)
8745 return native_interpret_vector_part (type
, ptr
, len
, count
, 1);
8749 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8750 the buffer PTR of length LEN as a constant of type TYPE. For
8751 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8752 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8753 return NULL_TREE. */
8756 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
8758 switch (TREE_CODE (type
))
8764 case REFERENCE_TYPE
:
8765 return native_interpret_int (type
, ptr
, len
);
8768 return native_interpret_real (type
, ptr
, len
);
8770 case FIXED_POINT_TYPE
:
8771 return native_interpret_fixed (type
, ptr
, len
);
8774 return native_interpret_complex (type
, ptr
, len
);
8777 return native_interpret_vector (type
, ptr
, len
);
8784 /* Returns true if we can interpret the contents of a native encoding
8788 can_native_interpret_type_p (tree type
)
8790 switch (TREE_CODE (type
))
8796 case REFERENCE_TYPE
:
8797 case FIXED_POINT_TYPE
:
8807 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8808 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8811 native_interpret_aggregate (tree type
, const unsigned char *ptr
, int off
,
8814 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
8815 if (TREE_CODE (type
) == ARRAY_TYPE
)
8817 HOST_WIDE_INT eltsz
= int_size_in_bytes (TREE_TYPE (type
));
8818 if (eltsz
< 0 || eltsz
> len
|| TYPE_DOMAIN (type
) == NULL_TREE
)
8821 HOST_WIDE_INT cnt
= 0;
8822 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
8824 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
8826 cnt
= tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1;
8830 HOST_WIDE_INT pos
= 0;
8831 for (HOST_WIDE_INT i
= 0; i
< cnt
; i
++, pos
+= eltsz
)
8834 if (pos
>= len
|| pos
+ eltsz
> len
)
8836 if (can_native_interpret_type_p (TREE_TYPE (type
)))
8838 v
= native_interpret_expr (TREE_TYPE (type
),
8839 ptr
+ off
+ pos
, eltsz
);
8843 else if (TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
8844 || TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
8845 v
= native_interpret_aggregate (TREE_TYPE (type
), ptr
, off
+ pos
,
8849 CONSTRUCTOR_APPEND_ELT (elts
, size_int (i
), v
);
8851 return build_constructor (type
, elts
);
8853 if (TREE_CODE (type
) != RECORD_TYPE
)
8855 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8857 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_PADDING_P (field
))
8860 HOST_WIDE_INT bitoff
= 0, pos
= 0, sz
= 0;
8863 if (DECL_BIT_FIELD (field
))
8865 fld
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8866 if (fld
&& INTEGRAL_TYPE_P (TREE_TYPE (fld
)))
8868 poly_int64 bitoffset
;
8869 poly_uint64 field_offset
, fld_offset
;
8870 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
8871 && poly_int_tree_p (DECL_FIELD_OFFSET (fld
), &fld_offset
))
8872 bitoffset
= (field_offset
- fld_offset
) * BITS_PER_UNIT
;
8875 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
8876 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld
)));
8877 diff
= (TYPE_PRECISION (TREE_TYPE (fld
))
8878 - TYPE_PRECISION (TREE_TYPE (field
)));
8879 if (!bitoffset
.is_constant (&bitoff
)
8886 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8888 int fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8889 int bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8890 bpos
%= BITS_PER_UNIT
;
8892 fieldsize
+= BITS_PER_UNIT
- 1;
8893 fieldsize
/= BITS_PER_UNIT
;
8894 tree repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8895 if (repr_type
== NULL_TREE
)
8897 sz
= int_size_in_bytes (repr_type
);
8898 if (sz
< 0 || sz
> len
)
8900 pos
= int_byte_position (field
);
8901 if (pos
< 0 || pos
> len
|| pos
+ fieldsize
> len
)
8904 if (pos
+ sz
<= len
)
8909 gcc_assert (rpos
<= pos
);
8911 bitoff
= (HOST_WIDE_INT
) (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8913 diff
= (TYPE_PRECISION (repr_type
)
8914 - TYPE_PRECISION (TREE_TYPE (field
)));
8915 v
= native_interpret_expr (repr_type
, ptr
+ off
+ pos
, sz
);
8924 sz
= int_size_in_bytes (TREE_TYPE (fld
));
8925 if (sz
< 0 || sz
> len
)
8927 tree byte_pos
= byte_position (fld
);
8928 if (!tree_fits_shwi_p (byte_pos
))
8930 pos
= tree_to_shwi (byte_pos
);
8931 if (pos
< 0 || pos
> len
|| pos
+ sz
> len
)
8934 if (fld
== NULL_TREE
)
8935 /* Already handled above. */;
8936 else if (can_native_interpret_type_p (TREE_TYPE (fld
)))
8938 v
= native_interpret_expr (TREE_TYPE (fld
),
8939 ptr
+ off
+ pos
, sz
);
8943 else if (TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
8944 || TREE_CODE (TREE_TYPE (fld
)) == ARRAY_TYPE
)
8945 v
= native_interpret_aggregate (TREE_TYPE (fld
), ptr
, off
+ pos
, sz
);
8950 if (TREE_CODE (v
) != INTEGER_CST
)
8953 /* FIXME: Figure out how to handle PDP endian bitfields. */
8954 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8956 if (!BYTES_BIG_ENDIAN
)
8957 v
= wide_int_to_tree (TREE_TYPE (field
),
8958 wi::lrshift (wi::to_wide (v
), bitoff
));
8960 v
= wide_int_to_tree (TREE_TYPE (field
),
8961 wi::lrshift (wi::to_wide (v
),
8964 CONSTRUCTOR_APPEND_ELT (elts
, field
, v
);
8966 return build_constructor (type
, elts
);
8969 /* Routines for manipulation of native_encode_expr encoded data if the encoded
8970 or extracted constant positions and/or sizes aren't byte aligned. */
8972 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8973 bits between adjacent elements. AMNT should be within
8976 00011111|11100000 << 2 = 01111111|10000000
8977 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8980 shift_bytes_in_array_left (unsigned char *ptr
, unsigned int sz
,
8986 unsigned char carry_over
= 0U;
8987 unsigned char carry_mask
= (~0U) << (unsigned char) (BITS_PER_UNIT
- amnt
);
8988 unsigned char clear_mask
= (~0U) << amnt
;
8990 for (unsigned int i
= 0; i
< sz
; i
++)
8992 unsigned prev_carry_over
= carry_over
;
8993 carry_over
= (ptr
[i
] & carry_mask
) >> (BITS_PER_UNIT
- amnt
);
8998 ptr
[i
] &= clear_mask
;
8999 ptr
[i
] |= prev_carry_over
;
9004 /* Like shift_bytes_in_array_left but for big-endian.
9005 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9006 bits between adjacent elements. AMNT should be within
9009 00011111|11100000 >> 2 = 00000111|11111000
9010 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9013 shift_bytes_in_array_right (unsigned char *ptr
, unsigned int sz
,
9019 unsigned char carry_over
= 0U;
9020 unsigned char carry_mask
= ~(~0U << amnt
);
9022 for (unsigned int i
= 0; i
< sz
; i
++)
9024 unsigned prev_carry_over
= carry_over
;
9025 carry_over
= ptr
[i
] & carry_mask
;
9027 carry_over
<<= (unsigned char) BITS_PER_UNIT
- amnt
;
9029 ptr
[i
] |= prev_carry_over
;
9033 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9034 directly on the VECTOR_CST encoding, in a way that works for variable-
9035 length vectors. Return the resulting VECTOR_CST on success or null
9039 fold_view_convert_vector_encoding (tree type
, tree expr
)
9041 tree expr_type
= TREE_TYPE (expr
);
9042 poly_uint64 type_bits
, expr_bits
;
9043 if (!poly_int_tree_p (TYPE_SIZE (type
), &type_bits
)
9044 || !poly_int_tree_p (TYPE_SIZE (expr_type
), &expr_bits
))
9047 poly_uint64 type_units
= TYPE_VECTOR_SUBPARTS (type
);
9048 poly_uint64 expr_units
= TYPE_VECTOR_SUBPARTS (expr_type
);
9049 unsigned int type_elt_bits
= vector_element_size (type_bits
, type_units
);
9050 unsigned int expr_elt_bits
= vector_element_size (expr_bits
, expr_units
);
9052 /* We can only preserve the semantics of a stepped pattern if the new
9053 vector element is an integer of the same size. */
9054 if (VECTOR_CST_STEPPED_P (expr
)
9055 && (!INTEGRAL_TYPE_P (type
) || type_elt_bits
!= expr_elt_bits
))
9058 /* The number of bits needed to encode one element from every pattern
9059 of the original vector. */
9060 unsigned int expr_sequence_bits
9061 = VECTOR_CST_NPATTERNS (expr
) * expr_elt_bits
;
9063 /* The number of bits needed to encode one element from every pattern
9065 unsigned int type_sequence_bits
9066 = least_common_multiple (expr_sequence_bits
, type_elt_bits
);
9068 /* Don't try to read more bytes than are available, which can happen
9069 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9070 The general VIEW_CONVERT handling can cope with that case, so there's
9071 no point complicating things here. */
9072 unsigned int nelts_per_pattern
= VECTOR_CST_NELTS_PER_PATTERN (expr
);
9073 unsigned int buffer_bytes
= CEIL (nelts_per_pattern
* type_sequence_bits
,
9075 unsigned int buffer_bits
= buffer_bytes
* BITS_PER_UNIT
;
9076 if (known_gt (buffer_bits
, expr_bits
))
9079 /* Get enough bytes of EXPR to form the new encoding. */
9080 auto_vec
<unsigned char, 128> buffer (buffer_bytes
);
9081 buffer
.quick_grow (buffer_bytes
);
9082 if (native_encode_vector_part (expr
, buffer
.address (), buffer_bytes
, 0,
9083 buffer_bits
/ expr_elt_bits
)
9084 != (int) buffer_bytes
)
9087 /* Reencode the bytes as TYPE. */
9088 unsigned int type_npatterns
= type_sequence_bits
/ type_elt_bits
;
9089 return native_interpret_vector_part (type
, &buffer
[0], buffer
.length (),
9090 type_npatterns
, nelts_per_pattern
);
9093 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9094 TYPE at compile-time. If we're unable to perform the conversion
9095 return NULL_TREE. */
9098 fold_view_convert_expr (tree type
, tree expr
)
9100 /* We support up to 512-bit values (for V8DFmode). */
9101 unsigned char buffer
[64];
9104 /* Check that the host and target are sane. */
9105 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
9108 if (VECTOR_TYPE_P (type
) && TREE_CODE (expr
) == VECTOR_CST
)
9109 if (tree res
= fold_view_convert_vector_encoding (type
, expr
))
9112 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
9116 return native_interpret_expr (type
, buffer
, len
);
9119 /* Build an expression for the address of T. Folds away INDIRECT_REF
9120 to avoid confusing the gimplify process. */
9123 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
9125 /* The size of the object is not relevant when talking about its address. */
9126 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
9127 t
= TREE_OPERAND (t
, 0);
9129 if (TREE_CODE (t
) == INDIRECT_REF
)
9131 t
= TREE_OPERAND (t
, 0);
9133 if (TREE_TYPE (t
) != ptrtype
)
9134 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
9136 else if (TREE_CODE (t
) == MEM_REF
9137 && integer_zerop (TREE_OPERAND (t
, 1)))
9139 t
= TREE_OPERAND (t
, 0);
9141 if (TREE_TYPE (t
) != ptrtype
)
9142 t
= fold_convert_loc (loc
, ptrtype
, t
);
9144 else if (TREE_CODE (t
) == MEM_REF
9145 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
9146 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
9147 TREE_OPERAND (t
, 0),
9148 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
9149 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
9151 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
9153 if (TREE_TYPE (t
) != ptrtype
)
9154 t
= fold_convert_loc (loc
, ptrtype
, t
);
9157 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
9162 /* Build an expression for the address of T. */
9165 build_fold_addr_expr_loc (location_t loc
, tree t
)
9167 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
9169 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
9172 /* Fold a unary expression of code CODE and type TYPE with operand
9173 OP0. Return the folded expression if folding is successful.
9174 Otherwise, return NULL_TREE. */
9177 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
9181 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9183 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9184 && TREE_CODE_LENGTH (code
) == 1);
9189 if (CONVERT_EXPR_CODE_P (code
)
9190 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
9192 /* Don't use STRIP_NOPS, because signedness of argument type
9194 STRIP_SIGN_NOPS (arg0
);
9198 /* Strip any conversions that don't change the mode. This
9199 is safe for every expression, except for a comparison
9200 expression because its signedness is derived from its
9203 Note that this is done as an internal manipulation within
9204 the constant folder, in order to find the simplest
9205 representation of the arguments so that their form can be
9206 studied. In any cases, the appropriate type conversions
9207 should be put back in the tree that will get out of the
9212 if (CONSTANT_CLASS_P (arg0
))
9214 tree tem
= const_unop (code
, type
, arg0
);
9217 if (TREE_TYPE (tem
) != type
)
9218 tem
= fold_convert_loc (loc
, type
, tem
);
9224 tem
= generic_simplify (loc
, code
, type
, op0
);
9228 if (TREE_CODE_CLASS (code
) == tcc_unary
)
9230 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9231 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9232 fold_build1_loc (loc
, code
, type
,
9233 fold_convert_loc (loc
, TREE_TYPE (op0
),
9234 TREE_OPERAND (arg0
, 1))));
9235 else if (TREE_CODE (arg0
) == COND_EXPR
)
9237 tree arg01
= TREE_OPERAND (arg0
, 1);
9238 tree arg02
= TREE_OPERAND (arg0
, 2);
9239 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
9240 arg01
= fold_build1_loc (loc
, code
, type
,
9241 fold_convert_loc (loc
,
9242 TREE_TYPE (op0
), arg01
));
9243 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
9244 arg02
= fold_build1_loc (loc
, code
, type
,
9245 fold_convert_loc (loc
,
9246 TREE_TYPE (op0
), arg02
));
9247 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9250 /* If this was a conversion, and all we did was to move into
9251 inside the COND_EXPR, bring it back out. But leave it if
9252 it is a conversion from integer to integer and the
9253 result precision is no wider than a word since such a
9254 conversion is cheap and may be optimized away by combine,
9255 while it couldn't if it were outside the COND_EXPR. Then return
9256 so we don't get into an infinite recursion loop taking the
9257 conversion out and then back in. */
9259 if ((CONVERT_EXPR_CODE_P (code
)
9260 || code
== NON_LVALUE_EXPR
)
9261 && TREE_CODE (tem
) == COND_EXPR
9262 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
9263 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
9264 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
9265 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
9266 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
9267 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
9268 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9270 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
9271 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
9272 || flag_syntax_only
))
9273 tem
= build1_loc (loc
, code
, type
,
9275 TREE_TYPE (TREE_OPERAND
9276 (TREE_OPERAND (tem
, 1), 0)),
9277 TREE_OPERAND (tem
, 0),
9278 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
9279 TREE_OPERAND (TREE_OPERAND (tem
, 2),
9287 case NON_LVALUE_EXPR
:
9288 if (!maybe_lvalue_p (op0
))
9289 return fold_convert_loc (loc
, type
, op0
);
9294 case FIX_TRUNC_EXPR
:
9295 if (COMPARISON_CLASS_P (op0
))
9297 /* If we have (type) (a CMP b) and type is an integral type, return
9298 new expression involving the new type. Canonicalize
9299 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9301 Do not fold the result as that would not simplify further, also
9302 folding again results in recursions. */
9303 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
9304 return build2_loc (loc
, TREE_CODE (op0
), type
,
9305 TREE_OPERAND (op0
, 0),
9306 TREE_OPERAND (op0
, 1));
9307 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
9308 && TREE_CODE (type
) != VECTOR_TYPE
)
9309 return build3_loc (loc
, COND_EXPR
, type
, op0
,
9310 constant_boolean_node (true, type
),
9311 constant_boolean_node (false, type
));
9314 /* Handle (T *)&A.B.C for A being of type T and B and C
9315 living at offset zero. This occurs frequently in
9316 C++ upcasting and then accessing the base. */
9317 if (TREE_CODE (op0
) == ADDR_EXPR
9318 && POINTER_TYPE_P (type
)
9319 && handled_component_p (TREE_OPERAND (op0
, 0)))
9321 poly_int64 bitsize
, bitpos
;
9324 int unsignedp
, reversep
, volatilep
;
9326 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
9327 &offset
, &mode
, &unsignedp
, &reversep
,
9329 /* If the reference was to a (constant) zero offset, we can use
9330 the address of the base if it has the same base type
9331 as the result type and the pointer type is unqualified. */
9333 && known_eq (bitpos
, 0)
9334 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
9335 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
9336 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
9337 return fold_convert_loc (loc
, type
,
9338 build_fold_addr_expr_loc (loc
, base
));
9341 if (TREE_CODE (op0
) == MODIFY_EXPR
9342 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
9343 /* Detect assigning a bitfield. */
9344 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
9346 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
9348 /* Don't leave an assignment inside a conversion
9349 unless assigning a bitfield. */
9350 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
9351 /* First do the assignment, then return converted constant. */
9352 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
9353 TREE_NO_WARNING (tem
) = 1;
9354 TREE_USED (tem
) = 1;
9358 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9359 constants (if x has signed type, the sign bit cannot be set
9360 in c). This folds extension into the BIT_AND_EXPR.
9361 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9362 very likely don't have maximal range for their precision and this
9363 transformation effectively doesn't preserve non-maximal ranges. */
9364 if (TREE_CODE (type
) == INTEGER_TYPE
9365 && TREE_CODE (op0
) == BIT_AND_EXPR
9366 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
9368 tree and_expr
= op0
;
9369 tree and0
= TREE_OPERAND (and_expr
, 0);
9370 tree and1
= TREE_OPERAND (and_expr
, 1);
9373 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
9374 || (TYPE_PRECISION (type
)
9375 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
9377 else if (TYPE_PRECISION (TREE_TYPE (and1
))
9378 <= HOST_BITS_PER_WIDE_INT
9379 && tree_fits_uhwi_p (and1
))
9381 unsigned HOST_WIDE_INT cst
;
9383 cst
= tree_to_uhwi (and1
);
9384 cst
&= HOST_WIDE_INT_M1U
9385 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
9386 change
= (cst
== 0);
9388 && !flag_syntax_only
9389 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
9392 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
9393 and0
= fold_convert_loc (loc
, uns
, and0
);
9394 and1
= fold_convert_loc (loc
, uns
, and1
);
9399 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
9400 TREE_OVERFLOW (and1
));
9401 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9402 fold_convert_loc (loc
, type
, and0
), tem
);
9406 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9407 cast (T1)X will fold away. We assume that this happens when X itself
9409 if (POINTER_TYPE_P (type
)
9410 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9411 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
9413 tree arg00
= TREE_OPERAND (arg0
, 0);
9414 tree arg01
= TREE_OPERAND (arg0
, 1);
9416 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9417 when the pointed type needs higher alignment than
9418 the p+ first operand's pointed type. */
9420 && sanitize_flags_p (SANITIZE_ALIGNMENT
)
9421 && (min_align_of_type (TREE_TYPE (type
))
9422 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00
)))))
9425 arg00
= fold_convert_loc (loc
, type
, arg00
);
9426 return fold_build_pointer_plus_loc (loc
, arg00
, arg01
);
9429 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9430 of the same precision, and X is an integer type not narrower than
9431 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9432 if (INTEGRAL_TYPE_P (type
)
9433 && TREE_CODE (op0
) == BIT_NOT_EXPR
9434 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9435 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
9436 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
9438 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
9439 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9440 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
9441 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
9442 fold_convert_loc (loc
, type
, tem
));
9445 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9446 type of X and Y (integer types only). */
9447 if (INTEGRAL_TYPE_P (type
)
9448 && TREE_CODE (op0
) == MULT_EXPR
9449 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9450 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9452 /* Be careful not to introduce new overflows. */
9454 if (TYPE_OVERFLOW_WRAPS (type
))
9457 mult_type
= unsigned_type_for (type
);
9459 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9461 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
9462 fold_convert_loc (loc
, mult_type
,
9463 TREE_OPERAND (op0
, 0)),
9464 fold_convert_loc (loc
, mult_type
,
9465 TREE_OPERAND (op0
, 1)));
9466 return fold_convert_loc (loc
, type
, tem
);
9472 case VIEW_CONVERT_EXPR
:
9473 if (TREE_CODE (op0
) == MEM_REF
)
9475 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
9476 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
9477 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
9478 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
9479 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
9486 tem
= fold_negate_expr (loc
, arg0
);
9488 return fold_convert_loc (loc
, type
, tem
);
9492 /* Convert fabs((double)float) into (double)fabsf(float). */
9493 if (TREE_CODE (arg0
) == NOP_EXPR
9494 && TREE_CODE (type
) == REAL_TYPE
)
9496 tree targ0
= strip_float_extensions (arg0
);
9498 return fold_convert_loc (loc
, type
,
9499 fold_build1_loc (loc
, ABS_EXPR
,
9506 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9507 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9508 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9509 fold_convert_loc (loc
, type
,
9510 TREE_OPERAND (arg0
, 0)))))
9511 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
9512 fold_convert_loc (loc
, type
,
9513 TREE_OPERAND (arg0
, 1)));
9514 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9515 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9516 fold_convert_loc (loc
, type
,
9517 TREE_OPERAND (arg0
, 1)))))
9518 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
9519 fold_convert_loc (loc
, type
,
9520 TREE_OPERAND (arg0
, 0)), tem
);
9524 case TRUTH_NOT_EXPR
:
9525 /* Note that the operand of this must be an int
9526 and its values must be 0 or 1.
9527 ("true" is a fixed value perhaps depending on the language,
9528 but we don't handle values other than 1 correctly yet.) */
9529 tem
= fold_truth_not_expr (loc
, arg0
);
9532 return fold_convert_loc (loc
, type
, tem
);
9535 /* Fold *&X to X if X is an lvalue. */
9536 if (TREE_CODE (op0
) == ADDR_EXPR
)
9538 tree op00
= TREE_OPERAND (op0
, 0);
9540 || TREE_CODE (op00
) == PARM_DECL
9541 || TREE_CODE (op00
) == RESULT_DECL
)
9542 && !TREE_READONLY (op00
))
9549 } /* switch (code) */
9553 /* If the operation was a conversion do _not_ mark a resulting constant
9554 with TREE_OVERFLOW if the original constant was not. These conversions
9555 have implementation defined behavior and retaining the TREE_OVERFLOW
9556 flag here would confuse later passes such as VRP. */
9558 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
9559 tree type
, tree op0
)
9561 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
9563 && TREE_CODE (res
) == INTEGER_CST
9564 && TREE_CODE (op0
) == INTEGER_CST
9565 && CONVERT_EXPR_CODE_P (code
))
9566 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
9571 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9572 operands OP0 and OP1. LOC is the location of the resulting expression.
9573 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9574 Return the folded expression if folding is successful. Otherwise,
9575 return NULL_TREE. */
9577 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
9578 tree arg0
, tree arg1
, tree op0
, tree op1
)
9582 /* We only do these simplifications if we are optimizing. */
9586 /* Check for things like (A || B) && (A || C). We can convert this
9587 to A || (B && C). Note that either operator can be any of the four
9588 truth and/or operations and the transformation will still be
9589 valid. Also note that we only care about order for the
9590 ANDIF and ORIF operators. If B contains side effects, this
9591 might change the truth-value of A. */
9592 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9593 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
9594 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
9595 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
9596 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
9597 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
9599 tree a00
= TREE_OPERAND (arg0
, 0);
9600 tree a01
= TREE_OPERAND (arg0
, 1);
9601 tree a10
= TREE_OPERAND (arg1
, 0);
9602 tree a11
= TREE_OPERAND (arg1
, 1);
9603 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
9604 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
9605 && (code
== TRUTH_AND_EXPR
9606 || code
== TRUTH_OR_EXPR
));
9608 if (operand_equal_p (a00
, a10
, 0))
9609 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9610 fold_build2_loc (loc
, code
, type
, a01
, a11
));
9611 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
9612 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9613 fold_build2_loc (loc
, code
, type
, a01
, a10
));
9614 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
9615 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
9616 fold_build2_loc (loc
, code
, type
, a00
, a11
));
9618 /* This case if tricky because we must either have commutative
9619 operators or else A10 must not have side-effects. */
9621 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
9622 && operand_equal_p (a01
, a11
, 0))
9623 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
9624 fold_build2_loc (loc
, code
, type
, a00
, a10
),
9628 /* See if we can build a range comparison. */
9629 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
9632 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
9633 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
9635 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
9637 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
9640 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
9641 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
9643 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
9645 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
9648 /* Check for the possibility of merging component references. If our
9649 lhs is another similar operation, try to merge its rhs with our
9650 rhs. Then try to merge our lhs and rhs. */
9651 if (TREE_CODE (arg0
) == code
9652 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
9653 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
9654 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9656 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
9659 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
9660 if (param_logical_op_non_short_circuit
!= -1)
9661 logical_op_non_short_circuit
9662 = param_logical_op_non_short_circuit
;
9663 if (logical_op_non_short_circuit
9664 && !flag_sanitize_coverage
9665 && (code
== TRUTH_AND_EXPR
9666 || code
== TRUTH_ANDIF_EXPR
9667 || code
== TRUTH_OR_EXPR
9668 || code
== TRUTH_ORIF_EXPR
))
9670 enum tree_code ncode
, icode
;
9672 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
9673 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
9674 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
9676 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9677 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9678 We don't want to pack more than two leafs to a non-IF AND/OR
9680 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9681 equal to IF-CODE, then we don't want to add right-hand operand.
9682 If the inner right-hand side of left-hand operand has
9683 side-effects, or isn't simple, then we can't add to it,
9684 as otherwise we might destroy if-sequence. */
9685 if (TREE_CODE (arg0
) == icode
9686 && simple_operand_p_2 (arg1
)
9687 /* Needed for sequence points to handle trappings, and
9689 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
9691 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
9693 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
9696 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9697 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9698 else if (TREE_CODE (arg1
) == icode
9699 && simple_operand_p_2 (arg0
)
9700 /* Needed for sequence points to handle trappings, and
9702 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
9704 tem
= fold_build2_loc (loc
, ncode
, type
,
9705 arg0
, TREE_OPERAND (arg1
, 0));
9706 return fold_build2_loc (loc
, icode
, type
, tem
,
9707 TREE_OPERAND (arg1
, 1));
9709 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9711 For sequence point consistancy, we need to check for trapping,
9712 and side-effects. */
9713 else if (code
== icode
&& simple_operand_p_2 (arg0
)
9714 && simple_operand_p_2 (arg1
))
9715 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
9721 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9722 by changing CODE to reduce the magnitude of constants involved in
9723 ARG0 of the comparison.
9724 Returns a canonicalized comparison tree if a simplification was
9725 possible, otherwise returns NULL_TREE.
9726 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9727 valid if signed overflow is undefined. */
9730 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
9731 tree arg0
, tree arg1
,
9732 bool *strict_overflow_p
)
9734 enum tree_code code0
= TREE_CODE (arg0
);
9735 tree t
, cst0
= NULL_TREE
;
9738 /* Match A +- CST code arg1. We can change this only if overflow
9740 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9741 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
9742 /* In principle pointers also have undefined overflow behavior,
9743 but that causes problems elsewhere. */
9744 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
9745 && (code0
== MINUS_EXPR
9746 || code0
== PLUS_EXPR
)
9747 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
9750 /* Identify the constant in arg0 and its sign. */
9751 cst0
= TREE_OPERAND (arg0
, 1);
9752 sgn0
= tree_int_cst_sgn (cst0
);
9754 /* Overflowed constants and zero will cause problems. */
9755 if (integer_zerop (cst0
)
9756 || TREE_OVERFLOW (cst0
))
9759 /* See if we can reduce the magnitude of the constant in
9760 arg0 by changing the comparison code. */
9761 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9763 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9765 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9766 else if (code
== GT_EXPR
9767 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9769 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9770 else if (code
== LE_EXPR
9771 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9773 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9774 else if (code
== GE_EXPR
9775 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9779 *strict_overflow_p
= true;
9781 /* Now build the constant reduced in magnitude. But not if that
9782 would produce one outside of its types range. */
9783 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
9785 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
9786 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
9788 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
9789 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
9792 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
9793 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
9794 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
9795 t
= fold_convert (TREE_TYPE (arg1
), t
);
9797 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
9800 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9801 overflow further. Try to decrease the magnitude of constants involved
9802 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9803 and put sole constants at the second argument position.
9804 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9807 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
9808 tree arg0
, tree arg1
)
9811 bool strict_overflow_p
;
9812 const char * const warnmsg
= G_("assuming signed overflow does not occur "
9813 "when reducing constant in comparison");
9815 /* Try canonicalization by simplifying arg0. */
9816 strict_overflow_p
= false;
9817 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
9818 &strict_overflow_p
);
9821 if (strict_overflow_p
)
9822 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9826 /* Try canonicalization by simplifying arg1 using the swapped
9828 code
= swap_tree_comparison (code
);
9829 strict_overflow_p
= false;
9830 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
9831 &strict_overflow_p
);
9832 if (t
&& strict_overflow_p
)
9833 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9837 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9838 space. This is used to avoid issuing overflow warnings for
9839 expressions like &p->x which cannot wrap. */
9842 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
9844 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
9847 if (maybe_lt (bitpos
, 0))
9850 poly_wide_int wi_offset
;
9851 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
9852 if (offset
== NULL_TREE
)
9853 wi_offset
= wi::zero (precision
);
9854 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
9857 wi_offset
= wi::to_poly_wide (offset
);
9859 wi::overflow_type overflow
;
9860 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
9862 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
9866 poly_uint64 total_hwi
, size
;
9867 if (!total
.to_uhwi (&total_hwi
)
9868 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
9870 || known_eq (size
, 0U))
9873 if (known_le (total_hwi
, size
))
9876 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9878 if (TREE_CODE (base
) == ADDR_EXPR
9879 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
9881 && maybe_ne (size
, 0U)
9882 && known_le (total_hwi
, size
))
9888 /* Return a positive integer when the symbol DECL is known to have
9889 a nonzero address, zero when it's known not to (e.g., it's a weak
9890 symbol), and a negative integer when the symbol is not yet in the
9891 symbol table and so whether or not its address is zero is unknown.
9892 For function local objects always return positive integer. */
9894 maybe_nonzero_address (tree decl
)
9896 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
9897 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
9898 return symbol
->nonzero_address ();
9900 /* Function local objects are never NULL. */
9902 && (DECL_CONTEXT (decl
)
9903 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
9904 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
9910 /* Subroutine of fold_binary. This routine performs all of the
9911 transformations that are common to the equality/inequality
9912 operators (EQ_EXPR and NE_EXPR) and the ordering operators
9913 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9914 fold_binary should call fold_binary. Fold a comparison with
9915 tree code CODE and type TYPE with operands OP0 and OP1. Return
9916 the folded comparison or NULL_TREE. */
9919 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
9922 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
9923 tree arg0
, arg1
, tem
;
9928 STRIP_SIGN_NOPS (arg0
);
9929 STRIP_SIGN_NOPS (arg1
);
9931 /* For comparisons of pointers we can decompose it to a compile time
9932 comparison of the base objects and the offsets into the object.
9933 This requires at least one operand being an ADDR_EXPR or a
9934 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9935 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
9936 && (TREE_CODE (arg0
) == ADDR_EXPR
9937 || TREE_CODE (arg1
) == ADDR_EXPR
9938 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9939 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
9941 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
9942 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
9944 int volatilep
, reversep
, unsignedp
;
9945 bool indirect_base0
= false, indirect_base1
= false;
9947 /* Get base and offset for the access. Strip ADDR_EXPR for
9948 get_inner_reference, but put it back by stripping INDIRECT_REF
9949 off the base object if possible. indirect_baseN will be true
9950 if baseN is not an address but refers to the object itself. */
9952 if (TREE_CODE (arg0
) == ADDR_EXPR
)
9955 = get_inner_reference (TREE_OPERAND (arg0
, 0),
9956 &bitsize
, &bitpos0
, &offset0
, &mode
,
9957 &unsignedp
, &reversep
, &volatilep
);
9958 if (TREE_CODE (base0
) == INDIRECT_REF
)
9959 base0
= TREE_OPERAND (base0
, 0);
9961 indirect_base0
= true;
9963 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
9965 base0
= TREE_OPERAND (arg0
, 0);
9966 STRIP_SIGN_NOPS (base0
);
9967 if (TREE_CODE (base0
) == ADDR_EXPR
)
9970 = get_inner_reference (TREE_OPERAND (base0
, 0),
9971 &bitsize
, &bitpos0
, &offset0
, &mode
,
9972 &unsignedp
, &reversep
, &volatilep
);
9973 if (TREE_CODE (base0
) == INDIRECT_REF
)
9974 base0
= TREE_OPERAND (base0
, 0);
9976 indirect_base0
= true;
9978 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
9979 offset0
= TREE_OPERAND (arg0
, 1);
9981 offset0
= size_binop (PLUS_EXPR
, offset0
,
9982 TREE_OPERAND (arg0
, 1));
9983 if (poly_int_tree_p (offset0
))
9985 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
9986 TYPE_PRECISION (sizetype
));
9987 tem
<<= LOG2_BITS_PER_UNIT
;
9989 if (tem
.to_shwi (&bitpos0
))
9990 offset0
= NULL_TREE
;
9995 if (TREE_CODE (arg1
) == ADDR_EXPR
)
9998 = get_inner_reference (TREE_OPERAND (arg1
, 0),
9999 &bitsize
, &bitpos1
, &offset1
, &mode
,
10000 &unsignedp
, &reversep
, &volatilep
);
10001 if (TREE_CODE (base1
) == INDIRECT_REF
)
10002 base1
= TREE_OPERAND (base1
, 0);
10004 indirect_base1
= true;
10006 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10008 base1
= TREE_OPERAND (arg1
, 0);
10009 STRIP_SIGN_NOPS (base1
);
10010 if (TREE_CODE (base1
) == ADDR_EXPR
)
10013 = get_inner_reference (TREE_OPERAND (base1
, 0),
10014 &bitsize
, &bitpos1
, &offset1
, &mode
,
10015 &unsignedp
, &reversep
, &volatilep
);
10016 if (TREE_CODE (base1
) == INDIRECT_REF
)
10017 base1
= TREE_OPERAND (base1
, 0);
10019 indirect_base1
= true;
10021 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
10022 offset1
= TREE_OPERAND (arg1
, 1);
10024 offset1
= size_binop (PLUS_EXPR
, offset1
,
10025 TREE_OPERAND (arg1
, 1));
10026 if (poly_int_tree_p (offset1
))
10028 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
10029 TYPE_PRECISION (sizetype
));
10030 tem
<<= LOG2_BITS_PER_UNIT
;
10032 if (tem
.to_shwi (&bitpos1
))
10033 offset1
= NULL_TREE
;
10037 /* If we have equivalent bases we might be able to simplify. */
10038 if (indirect_base0
== indirect_base1
10039 && operand_equal_p (base0
, base1
,
10040 indirect_base0
? OEP_ADDRESS_OF
: 0))
10042 /* We can fold this expression to a constant if the non-constant
10043 offset parts are equal. */
10044 if ((offset0
== offset1
10045 || (offset0
&& offset1
10046 && operand_equal_p (offset0
, offset1
, 0)))
10049 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10050 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10053 && maybe_ne (bitpos0
, bitpos1
)
10054 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10055 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10056 fold_overflow_warning (("assuming pointer wraparound does not "
10057 "occur when comparing P +- C1 with "
10059 WARN_STRICT_OVERFLOW_CONDITIONAL
);
10064 if (known_eq (bitpos0
, bitpos1
))
10065 return constant_boolean_node (true, type
);
10066 if (known_ne (bitpos0
, bitpos1
))
10067 return constant_boolean_node (false, type
);
10070 if (known_ne (bitpos0
, bitpos1
))
10071 return constant_boolean_node (true, type
);
10072 if (known_eq (bitpos0
, bitpos1
))
10073 return constant_boolean_node (false, type
);
10076 if (known_lt (bitpos0
, bitpos1
))
10077 return constant_boolean_node (true, type
);
10078 if (known_ge (bitpos0
, bitpos1
))
10079 return constant_boolean_node (false, type
);
10082 if (known_le (bitpos0
, bitpos1
))
10083 return constant_boolean_node (true, type
);
10084 if (known_gt (bitpos0
, bitpos1
))
10085 return constant_boolean_node (false, type
);
10088 if (known_ge (bitpos0
, bitpos1
))
10089 return constant_boolean_node (true, type
);
10090 if (known_lt (bitpos0
, bitpos1
))
10091 return constant_boolean_node (false, type
);
10094 if (known_gt (bitpos0
, bitpos1
))
10095 return constant_boolean_node (true, type
);
10096 if (known_le (bitpos0
, bitpos1
))
10097 return constant_boolean_node (false, type
);
10102 /* We can simplify the comparison to a comparison of the variable
10103 offset parts if the constant offset parts are equal.
10104 Be careful to use signed sizetype here because otherwise we
10105 mess with array offsets in the wrong way. This is possible
10106 because pointer arithmetic is restricted to retain within an
10107 object and overflow on pointer differences is undefined as of
10108 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10109 else if (known_eq (bitpos0
, bitpos1
)
10112 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10113 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10115 /* By converting to signed sizetype we cover middle-end pointer
10116 arithmetic which operates on unsigned pointer types of size
10117 type size and ARRAY_REF offsets which are properly sign or
10118 zero extended from their type in case it is narrower than
10120 if (offset0
== NULL_TREE
)
10121 offset0
= build_int_cst (ssizetype
, 0);
10123 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
10124 if (offset1
== NULL_TREE
)
10125 offset1
= build_int_cst (ssizetype
, 0);
10127 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
10130 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10131 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10132 fold_overflow_warning (("assuming pointer wraparound does not "
10133 "occur when comparing P +- C1 with "
10135 WARN_STRICT_OVERFLOW_COMPARISON
);
10137 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
10140 /* For equal offsets we can simplify to a comparison of the
10142 else if (known_eq (bitpos0
, bitpos1
)
10144 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
10146 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
10147 && ((offset0
== offset1
)
10148 || (offset0
&& offset1
10149 && operand_equal_p (offset0
, offset1
, 0))))
10151 if (indirect_base0
)
10152 base0
= build_fold_addr_expr_loc (loc
, base0
);
10153 if (indirect_base1
)
10154 base1
= build_fold_addr_expr_loc (loc
, base1
);
10155 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
10157 /* Comparison between an ordinary (non-weak) symbol and a null
10158 pointer can be eliminated since such symbols must have a non
10159 null address. In C, relational expressions between pointers
10160 to objects and null pointers are undefined. The results
10161 below follow the C++ rules with the additional property that
10162 every object pointer compares greater than a null pointer.
10164 else if (((DECL_P (base0
)
10165 && maybe_nonzero_address (base0
) > 0
10166 /* Avoid folding references to struct members at offset 0 to
10167 prevent tests like '&ptr->firstmember == 0' from getting
10168 eliminated. When ptr is null, although the -> expression
10169 is strictly speaking invalid, GCC retains it as a matter
10170 of QoI. See PR c/44555. */
10171 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
10172 || CONSTANT_CLASS_P (base0
))
10174 /* The caller guarantees that when one of the arguments is
10175 constant (i.e., null in this case) it is second. */
10176 && integer_zerop (arg1
))
10183 return constant_boolean_node (false, type
);
10187 return constant_boolean_node (true, type
);
10189 gcc_unreachable ();
10194 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10195 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10196 the resulting offset is smaller in absolute value than the
10197 original one and has the same sign. */
10198 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10199 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
10200 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10201 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10202 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
10203 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
10204 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10205 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
10207 tree const1
= TREE_OPERAND (arg0
, 1);
10208 tree const2
= TREE_OPERAND (arg1
, 1);
10209 tree variable1
= TREE_OPERAND (arg0
, 0);
10210 tree variable2
= TREE_OPERAND (arg1
, 0);
10212 const char * const warnmsg
= G_("assuming signed overflow does not "
10213 "occur when combining constants around "
10216 /* Put the constant on the side where it doesn't overflow and is
10217 of lower absolute value and of same sign than before. */
10218 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10219 ? MINUS_EXPR
: PLUS_EXPR
,
10221 if (!TREE_OVERFLOW (cst
)
10222 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
10223 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
10225 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10226 return fold_build2_loc (loc
, code
, type
,
10228 fold_build2_loc (loc
, TREE_CODE (arg1
),
10233 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10234 ? MINUS_EXPR
: PLUS_EXPR
,
10236 if (!TREE_OVERFLOW (cst
)
10237 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
10238 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
10240 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10241 return fold_build2_loc (loc
, code
, type
,
10242 fold_build2_loc (loc
, TREE_CODE (arg0
),
10249 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
10253 /* If we are comparing an expression that just has comparisons
10254 of two integer values, arithmetic expressions of those comparisons,
10255 and constants, we can simplify it. There are only three cases
10256 to check: the two values can either be equal, the first can be
10257 greater, or the second can be greater. Fold the expression for
10258 those three values. Since each value must be 0 or 1, we have
10259 eight possibilities, each of which corresponds to the constant 0
10260 or 1 or one of the six possible comparisons.
10262 This handles common cases like (a > b) == 0 but also handles
10263 expressions like ((x > y) - (y > x)) > 0, which supposedly
10264 occur in macroized code. */
10266 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
10268 tree cval1
= 0, cval2
= 0;
10270 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
10271 /* Don't handle degenerate cases here; they should already
10272 have been handled anyway. */
10273 && cval1
!= 0 && cval2
!= 0
10274 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
10275 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
10276 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
10277 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
10278 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
10279 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
10280 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
10282 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
10283 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
10285 /* We can't just pass T to eval_subst in case cval1 or cval2
10286 was the same as ARG1. */
10289 = fold_build2_loc (loc
, code
, type
,
10290 eval_subst (loc
, arg0
, cval1
, maxval
,
10294 = fold_build2_loc (loc
, code
, type
,
10295 eval_subst (loc
, arg0
, cval1
, maxval
,
10299 = fold_build2_loc (loc
, code
, type
,
10300 eval_subst (loc
, arg0
, cval1
, minval
,
10304 /* All three of these results should be 0 or 1. Confirm they are.
10305 Then use those values to select the proper code to use. */
10307 if (TREE_CODE (high_result
) == INTEGER_CST
10308 && TREE_CODE (equal_result
) == INTEGER_CST
10309 && TREE_CODE (low_result
) == INTEGER_CST
)
10311 /* Make a 3-bit mask with the high-order bit being the
10312 value for `>', the next for '=', and the low for '<'. */
10313 switch ((integer_onep (high_result
) * 4)
10314 + (integer_onep (equal_result
) * 2)
10315 + integer_onep (low_result
))
10318 /* Always false. */
10319 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10340 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10343 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
10352 /* Subroutine of fold_binary. Optimize complex multiplications of the
10353 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10354 argument EXPR represents the expression "z" of type TYPE. */
10357 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
10359 tree itype
= TREE_TYPE (type
);
10360 tree rpart
, ipart
, tem
;
10362 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
10364 rpart
= TREE_OPERAND (expr
, 0);
10365 ipart
= TREE_OPERAND (expr
, 1);
10367 else if (TREE_CODE (expr
) == COMPLEX_CST
)
10369 rpart
= TREE_REALPART (expr
);
10370 ipart
= TREE_IMAGPART (expr
);
10374 expr
= save_expr (expr
);
10375 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
10376 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
10379 rpart
= save_expr (rpart
);
10380 ipart
= save_expr (ipart
);
10381 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
10382 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
10383 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
10384 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
10385 build_zero_cst (itype
));
10389 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10390 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10391 true if successful. */
10394 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
10396 unsigned HOST_WIDE_INT i
, nunits
;
10398 if (TREE_CODE (arg
) == VECTOR_CST
10399 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
10401 for (i
= 0; i
< nunits
; ++i
)
10402 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
10404 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
10406 constructor_elt
*elt
;
10408 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
10409 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
10412 elts
[i
] = elt
->value
;
10416 for (; i
< nelts
; i
++)
10418 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
10422 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10423 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10424 NULL_TREE otherwise. */
10427 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10430 unsigned HOST_WIDE_INT nelts
;
10431 bool need_ctor
= false;
10433 if (!sel
.length ().is_constant (&nelts
))
10435 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), nelts
)
10436 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)), nelts
)
10437 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)), nelts
));
10438 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10439 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10442 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10443 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10444 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10447 tree_vector_builder
out_elts (type
, nelts
, 1);
10448 for (i
= 0; i
< nelts
; i
++)
10450 HOST_WIDE_INT index
;
10451 if (!sel
[i
].is_constant (&index
))
10453 if (!CONSTANT_CLASS_P (in_elts
[index
]))
10455 out_elts
.quick_push (unshare_expr (in_elts
[index
]));
10460 vec
<constructor_elt
, va_gc
> *v
;
10461 vec_alloc (v
, nelts
);
10462 for (i
= 0; i
< nelts
; i
++)
10463 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
10464 return build_constructor (type
, v
);
10467 return out_elts
.build ();
10470 /* Try to fold a pointer difference of type TYPE two address expressions of
10471 array references AREF0 and AREF1 using location LOC. Return a
10472 simplified expression for the difference or NULL_TREE. */
10475 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10476 tree aref0
, tree aref1
,
10477 bool use_pointer_diff
)
10479 tree base0
= TREE_OPERAND (aref0
, 0);
10480 tree base1
= TREE_OPERAND (aref1
, 0);
10481 tree base_offset
= build_int_cst (type
, 0);
10483 /* If the bases are array references as well, recurse. If the bases
10484 are pointer indirections compute the difference of the pointers.
10485 If the bases are equal, we are set. */
10486 if ((TREE_CODE (base0
) == ARRAY_REF
10487 && TREE_CODE (base1
) == ARRAY_REF
10489 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10490 use_pointer_diff
)))
10491 || (INDIRECT_REF_P (base0
)
10492 && INDIRECT_REF_P (base1
)
10495 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10496 TREE_OPERAND (base0
, 0),
10497 TREE_OPERAND (base1
, 0))
10498 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10499 fold_convert (type
,
10500 TREE_OPERAND (base0
, 0)),
10501 fold_convert (type
,
10502 TREE_OPERAND (base1
, 0)))))
10503 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10505 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10506 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10507 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10508 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10509 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10511 fold_build2_loc (loc
, MULT_EXPR
, type
,
10517 /* If the real or vector real constant CST of type TYPE has an exact
10518 inverse, return it, else return NULL. */
10521 exact_inverse (tree type
, tree cst
)
10527 switch (TREE_CODE (cst
))
10530 r
= TREE_REAL_CST (cst
);
10532 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10533 return build_real (type
, r
);
10539 unit_type
= TREE_TYPE (type
);
10540 mode
= TYPE_MODE (unit_type
);
10542 tree_vector_builder elts
;
10543 if (!elts
.new_unary_operation (type
, cst
, false))
10545 unsigned int count
= elts
.encoded_nelts ();
10546 for (unsigned int i
= 0; i
< count
; ++i
)
10548 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
10549 if (!exact_real_inverse (mode
, &r
))
10551 elts
.quick_push (build_real (unit_type
, r
));
10554 return elts
.build ();
10562 /* Mask out the tz least significant bits of X of type TYPE where
10563 tz is the number of trailing zeroes in Y. */
10565 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
10567 int tz
= wi::ctz (y
);
10569 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
10573 /* Return true when T is an address and is known to be nonzero.
10574 For floating point we further ensure that T is not denormal.
10575 Similar logic is present in nonzero_address in rtlanal.h.
10577 If the return value is based on the assumption that signed overflow
10578 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10579 change *STRICT_OVERFLOW_P. */
10582 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
10584 tree type
= TREE_TYPE (t
);
10585 enum tree_code code
;
10587 /* Doing something useful for floating point would need more work. */
10588 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
10591 code
= TREE_CODE (t
);
10592 switch (TREE_CODE_CLASS (code
))
10595 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10596 strict_overflow_p
);
10598 case tcc_comparison
:
10599 return tree_binary_nonzero_warnv_p (code
, type
,
10600 TREE_OPERAND (t
, 0),
10601 TREE_OPERAND (t
, 1),
10602 strict_overflow_p
);
10604 case tcc_declaration
:
10605 case tcc_reference
:
10606 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10614 case TRUTH_NOT_EXPR
:
10615 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10616 strict_overflow_p
);
10618 case TRUTH_AND_EXPR
:
10619 case TRUTH_OR_EXPR
:
10620 case TRUTH_XOR_EXPR
:
10621 return tree_binary_nonzero_warnv_p (code
, type
,
10622 TREE_OPERAND (t
, 0),
10623 TREE_OPERAND (t
, 1),
10624 strict_overflow_p
);
10631 case WITH_SIZE_EXPR
:
10633 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10635 case COMPOUND_EXPR
:
10638 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
10639 strict_overflow_p
);
10642 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
10643 strict_overflow_p
);
10647 tree fndecl
= get_callee_fndecl (t
);
10648 if (!fndecl
) return false;
10649 if (flag_delete_null_pointer_checks
&& !flag_check_new
10650 && DECL_IS_OPERATOR_NEW_P (fndecl
)
10651 && !TREE_NOTHROW (fndecl
))
10653 if (flag_delete_null_pointer_checks
10654 && lookup_attribute ("returns_nonnull",
10655 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
10657 return alloca_call_p (t
);
10666 /* Return true when T is an address and is known to be nonzero.
10667 Handle warnings about undefined signed overflow. */
10670 tree_expr_nonzero_p (tree t
)
10672 bool ret
, strict_overflow_p
;
10674 strict_overflow_p
= false;
10675 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
10676 if (strict_overflow_p
)
10677 fold_overflow_warning (("assuming signed overflow does not occur when "
10678 "determining that expression is always "
10680 WARN_STRICT_OVERFLOW_MISC
);
10684 /* Return true if T is known not to be equal to an integer W. */
10687 expr_not_equal_to (tree t
, const wide_int
&w
)
10690 switch (TREE_CODE (t
))
10693 return wi::to_wide (t
) != w
;
10696 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
10698 get_range_info (t
, vr
);
10699 if (!vr
.undefined_p ()
10700 && !vr
.contains_p (wide_int_to_tree (TREE_TYPE (t
), w
)))
10702 /* If T has some known zero bits and W has any of those bits set,
10703 then T is known not to be equal to W. */
10704 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
10705 TYPE_PRECISION (TREE_TYPE (t
))), 0))
10714 /* Fold a binary expression of code CODE and type TYPE with operands
10715 OP0 and OP1. LOC is the location of the resulting expression.
10716 Return the folded expression if folding is successful. Otherwise,
10717 return NULL_TREE. */
10720 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
10721 tree op0
, tree op1
)
10723 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
10724 tree arg0
, arg1
, tem
;
10725 tree t1
= NULL_TREE
;
10726 bool strict_overflow_p
;
10729 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
10730 && TREE_CODE_LENGTH (code
) == 2
10731 && op0
!= NULL_TREE
10732 && op1
!= NULL_TREE
);
10737 /* Strip any conversions that don't change the mode. This is
10738 safe for every expression, except for a comparison expression
10739 because its signedness is derived from its operands. So, in
10740 the latter case, only strip conversions that don't change the
10741 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10744 Note that this is done as an internal manipulation within the
10745 constant folder, in order to find the simplest representation
10746 of the arguments so that their form can be studied. In any
10747 cases, the appropriate type conversions should be put back in
10748 the tree that will get out of the constant folder. */
10750 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
10752 STRIP_SIGN_NOPS (arg0
);
10753 STRIP_SIGN_NOPS (arg1
);
10761 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10762 constant but we can't do arithmetic on them. */
10763 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
10765 tem
= const_binop (code
, type
, arg0
, arg1
);
10766 if (tem
!= NULL_TREE
)
10768 if (TREE_TYPE (tem
) != type
)
10769 tem
= fold_convert_loc (loc
, type
, tem
);
10774 /* If this is a commutative operation, and ARG0 is a constant, move it
10775 to ARG1 to reduce the number of tests below. */
10776 if (commutative_tree_code (code
)
10777 && tree_swap_operands_p (arg0
, arg1
))
10778 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
10780 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10781 to ARG1 to reduce the number of tests below. */
10782 if (kind
== tcc_comparison
10783 && tree_swap_operands_p (arg0
, arg1
))
10784 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
10786 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
10790 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10792 First check for cases where an arithmetic operation is applied to a
10793 compound, conditional, or comparison operation. Push the arithmetic
10794 operation inside the compound or conditional to see if any folding
10795 can then be done. Convert comparison to conditional for this purpose.
10796 The also optimizes non-constant cases that used to be done in
10799 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10800 one of the operands is a comparison and the other is a comparison, a
10801 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10802 code below would make the expression more complex. Change it to a
10803 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10804 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10806 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
10807 || code
== EQ_EXPR
|| code
== NE_EXPR
)
10808 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
10809 && ((truth_value_p (TREE_CODE (arg0
))
10810 && (truth_value_p (TREE_CODE (arg1
))
10811 || (TREE_CODE (arg1
) == BIT_AND_EXPR
10812 && integer_onep (TREE_OPERAND (arg1
, 1)))))
10813 || (truth_value_p (TREE_CODE (arg1
))
10814 && (truth_value_p (TREE_CODE (arg0
))
10815 || (TREE_CODE (arg0
) == BIT_AND_EXPR
10816 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
10818 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
10819 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
10822 fold_convert_loc (loc
, boolean_type_node
, arg0
),
10823 fold_convert_loc (loc
, boolean_type_node
, arg1
));
10825 if (code
== EQ_EXPR
)
10826 tem
= invert_truthvalue_loc (loc
, tem
);
10828 return fold_convert_loc (loc
, type
, tem
);
10831 if (TREE_CODE_CLASS (code
) == tcc_binary
10832 || TREE_CODE_CLASS (code
) == tcc_comparison
)
10834 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
10836 tem
= fold_build2_loc (loc
, code
, type
,
10837 fold_convert_loc (loc
, TREE_TYPE (op0
),
10838 TREE_OPERAND (arg0
, 1)), op1
);
10839 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10842 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
10844 tem
= fold_build2_loc (loc
, code
, type
, op0
,
10845 fold_convert_loc (loc
, TREE_TYPE (op1
),
10846 TREE_OPERAND (arg1
, 1)));
10847 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
10851 if (TREE_CODE (arg0
) == COND_EXPR
10852 || TREE_CODE (arg0
) == VEC_COND_EXPR
10853 || COMPARISON_CLASS_P (arg0
))
10855 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10857 /*cond_first_p=*/1);
10858 if (tem
!= NULL_TREE
)
10862 if (TREE_CODE (arg1
) == COND_EXPR
10863 || TREE_CODE (arg1
) == VEC_COND_EXPR
10864 || COMPARISON_CLASS_P (arg1
))
10866 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10868 /*cond_first_p=*/0);
10869 if (tem
!= NULL_TREE
)
10877 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10878 if (TREE_CODE (arg0
) == ADDR_EXPR
10879 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
10881 tree iref
= TREE_OPERAND (arg0
, 0);
10882 return fold_build2 (MEM_REF
, type
,
10883 TREE_OPERAND (iref
, 0),
10884 int_const_binop (PLUS_EXPR
, arg1
,
10885 TREE_OPERAND (iref
, 1)));
10888 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10889 if (TREE_CODE (arg0
) == ADDR_EXPR
10890 && handled_component_p (TREE_OPERAND (arg0
, 0)))
10893 poly_int64 coffset
;
10894 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
10898 return fold_build2 (MEM_REF
, type
,
10899 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
10900 int_const_binop (PLUS_EXPR
, arg1
,
10901 size_int (coffset
)));
10906 case POINTER_PLUS_EXPR
:
10907 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10908 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10909 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
10910 return fold_convert_loc (loc
, type
,
10911 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
10912 fold_convert_loc (loc
, sizetype
,
10914 fold_convert_loc (loc
, sizetype
,
10920 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
10922 /* X + (X / CST) * -CST is X % CST. */
10923 if (TREE_CODE (arg1
) == MULT_EXPR
10924 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
10925 && operand_equal_p (arg0
,
10926 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
10928 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
10929 tree cst1
= TREE_OPERAND (arg1
, 1);
10930 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
10932 if (sum
&& integer_zerop (sum
))
10933 return fold_convert_loc (loc
, type
,
10934 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
10935 TREE_TYPE (arg0
), arg0
,
10940 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10941 one. Make sure the type is not saturating and has the signedness of
10942 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10943 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10944 if ((TREE_CODE (arg0
) == MULT_EXPR
10945 || TREE_CODE (arg1
) == MULT_EXPR
)
10946 && !TYPE_SATURATING (type
)
10947 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
10948 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
10949 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
10951 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
10956 if (! FLOAT_TYPE_P (type
))
10958 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10959 (plus (plus (mult) (mult)) (foo)) so that we can
10960 take advantage of the factoring cases below. */
10961 if (ANY_INTEGRAL_TYPE_P (type
)
10962 && TYPE_OVERFLOW_WRAPS (type
)
10963 && (((TREE_CODE (arg0
) == PLUS_EXPR
10964 || TREE_CODE (arg0
) == MINUS_EXPR
)
10965 && TREE_CODE (arg1
) == MULT_EXPR
)
10966 || ((TREE_CODE (arg1
) == PLUS_EXPR
10967 || TREE_CODE (arg1
) == MINUS_EXPR
)
10968 && TREE_CODE (arg0
) == MULT_EXPR
)))
10970 tree parg0
, parg1
, parg
, marg
;
10971 enum tree_code pcode
;
10973 if (TREE_CODE (arg1
) == MULT_EXPR
)
10974 parg
= arg0
, marg
= arg1
;
10976 parg
= arg1
, marg
= arg0
;
10977 pcode
= TREE_CODE (parg
);
10978 parg0
= TREE_OPERAND (parg
, 0);
10979 parg1
= TREE_OPERAND (parg
, 1);
10980 STRIP_NOPS (parg0
);
10981 STRIP_NOPS (parg1
);
10983 if (TREE_CODE (parg0
) == MULT_EXPR
10984 && TREE_CODE (parg1
) != MULT_EXPR
)
10985 return fold_build2_loc (loc
, pcode
, type
,
10986 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10987 fold_convert_loc (loc
, type
,
10989 fold_convert_loc (loc
, type
,
10991 fold_convert_loc (loc
, type
, parg1
));
10992 if (TREE_CODE (parg0
) != MULT_EXPR
10993 && TREE_CODE (parg1
) == MULT_EXPR
)
10995 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10996 fold_convert_loc (loc
, type
, parg0
),
10997 fold_build2_loc (loc
, pcode
, type
,
10998 fold_convert_loc (loc
, type
, marg
),
10999 fold_convert_loc (loc
, type
,
11005 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11006 to __complex__ ( x, y ). This is not the same for SNaNs or
11007 if signed zeros are involved. */
11008 if (!HONOR_SNANS (element_mode (arg0
))
11009 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
11010 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11012 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11013 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11014 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11015 bool arg0rz
= false, arg0iz
= false;
11016 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11017 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11019 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11020 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11021 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11023 tree rp
= arg1r
? arg1r
11024 : build1 (REALPART_EXPR
, rtype
, arg1
);
11025 tree ip
= arg0i
? arg0i
11026 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11027 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11029 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11031 tree rp
= arg0r
? arg0r
11032 : build1 (REALPART_EXPR
, rtype
, arg0
);
11033 tree ip
= arg1i
? arg1i
11034 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11035 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11040 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11041 We associate floats only if the user has specified
11042 -fassociative-math. */
11043 if (flag_associative_math
11044 && TREE_CODE (arg1
) == PLUS_EXPR
11045 && TREE_CODE (arg0
) != MULT_EXPR
)
11047 tree tree10
= TREE_OPERAND (arg1
, 0);
11048 tree tree11
= TREE_OPERAND (arg1
, 1);
11049 if (TREE_CODE (tree11
) == MULT_EXPR
11050 && TREE_CODE (tree10
) == MULT_EXPR
)
11053 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11054 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11057 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11058 We associate floats only if the user has specified
11059 -fassociative-math. */
11060 if (flag_associative_math
11061 && TREE_CODE (arg0
) == PLUS_EXPR
11062 && TREE_CODE (arg1
) != MULT_EXPR
)
11064 tree tree00
= TREE_OPERAND (arg0
, 0);
11065 tree tree01
= TREE_OPERAND (arg0
, 1);
11066 if (TREE_CODE (tree01
) == MULT_EXPR
11067 && TREE_CODE (tree00
) == MULT_EXPR
)
11070 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11071 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11077 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11078 is a rotate of A by C1 bits. */
11079 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11080 is a rotate of A by B bits.
11081 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11082 though in this case CODE must be | and not + or ^, otherwise
11083 it doesn't return A when B is 0. */
11085 enum tree_code code0
, code1
;
11087 code0
= TREE_CODE (arg0
);
11088 code1
= TREE_CODE (arg1
);
11089 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11090 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11091 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11092 TREE_OPERAND (arg1
, 0), 0)
11093 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11094 TYPE_UNSIGNED (rtype
))
11095 /* Only create rotates in complete modes. Other cases are not
11096 expanded properly. */
11097 && (element_precision (rtype
)
11098 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11100 tree tree01
, tree11
;
11101 tree orig_tree01
, orig_tree11
;
11102 enum tree_code code01
, code11
;
11104 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11105 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11106 STRIP_NOPS (tree01
);
11107 STRIP_NOPS (tree11
);
11108 code01
= TREE_CODE (tree01
);
11109 code11
= TREE_CODE (tree11
);
11110 if (code11
!= MINUS_EXPR
11111 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11113 std::swap (code0
, code1
);
11114 std::swap (code01
, code11
);
11115 std::swap (tree01
, tree11
);
11116 std::swap (orig_tree01
, orig_tree11
);
11118 if (code01
== INTEGER_CST
11119 && code11
== INTEGER_CST
11120 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11121 == element_precision (rtype
)))
11123 tem
= build2_loc (loc
, LROTATE_EXPR
,
11124 rtype
, TREE_OPERAND (arg0
, 0),
11125 code0
== LSHIFT_EXPR
11126 ? orig_tree01
: orig_tree11
);
11127 return fold_convert_loc (loc
, type
, tem
);
11129 else if (code11
== MINUS_EXPR
)
11131 tree tree110
, tree111
;
11132 tree110
= TREE_OPERAND (tree11
, 0);
11133 tree111
= TREE_OPERAND (tree11
, 1);
11134 STRIP_NOPS (tree110
);
11135 STRIP_NOPS (tree111
);
11136 if (TREE_CODE (tree110
) == INTEGER_CST
11137 && compare_tree_int (tree110
,
11138 element_precision (rtype
)) == 0
11139 && operand_equal_p (tree01
, tree111
, 0))
11141 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11142 ? LROTATE_EXPR
: RROTATE_EXPR
),
11143 rtype
, TREE_OPERAND (arg0
, 0),
11145 return fold_convert_loc (loc
, type
, tem
);
11148 else if (code
== BIT_IOR_EXPR
11149 && code11
== BIT_AND_EXPR
11150 && pow2p_hwi (element_precision (rtype
)))
11152 tree tree110
, tree111
;
11153 tree110
= TREE_OPERAND (tree11
, 0);
11154 tree111
= TREE_OPERAND (tree11
, 1);
11155 STRIP_NOPS (tree110
);
11156 STRIP_NOPS (tree111
);
11157 if (TREE_CODE (tree110
) == NEGATE_EXPR
11158 && TREE_CODE (tree111
) == INTEGER_CST
11159 && compare_tree_int (tree111
,
11160 element_precision (rtype
) - 1) == 0
11161 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11163 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11164 ? LROTATE_EXPR
: RROTATE_EXPR
),
11165 rtype
, TREE_OPERAND (arg0
, 0),
11167 return fold_convert_loc (loc
, type
, tem
);
11174 /* In most languages, can't associate operations on floats through
11175 parentheses. Rather than remember where the parentheses were, we
11176 don't associate floats at all, unless the user has specified
11177 -fassociative-math.
11178 And, we need to make sure type is not saturating. */
11180 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11181 && !TYPE_SATURATING (type
))
11183 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11184 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11188 /* Split both trees into variables, constants, and literals. Then
11189 associate each group together, the constants with literals,
11190 then the result with variables. This increases the chances of
11191 literals being recombined later and of generating relocatable
11192 expressions for the sum of a constant and literal. */
11193 var0
= split_tree (arg0
, type
, code
,
11194 &minus_var0
, &con0
, &minus_con0
,
11195 &lit0
, &minus_lit0
, 0);
11196 var1
= split_tree (arg1
, type
, code
,
11197 &minus_var1
, &con1
, &minus_con1
,
11198 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11200 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11201 if (code
== MINUS_EXPR
)
11204 /* With undefined overflow prefer doing association in a type
11205 which wraps on overflow, if that is one of the operand types. */
11206 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11207 && !TYPE_OVERFLOW_WRAPS (type
))
11209 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11210 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11211 atype
= TREE_TYPE (arg0
);
11212 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11213 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11214 atype
= TREE_TYPE (arg1
);
11215 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11218 /* With undefined overflow we can only associate constants with one
11219 variable, and constants whose association doesn't overflow. */
11220 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11221 && !TYPE_OVERFLOW_WRAPS (atype
))
11223 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11225 /* ??? If split_tree would handle NEGATE_EXPR we could
11226 simply reject these cases and the allowed cases would
11227 be the var0/minus_var1 ones. */
11228 tree tmp0
= var0
? var0
: minus_var0
;
11229 tree tmp1
= var1
? var1
: minus_var1
;
11230 bool one_neg
= false;
11232 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11234 tmp0
= TREE_OPERAND (tmp0
, 0);
11235 one_neg
= !one_neg
;
11237 if (CONVERT_EXPR_P (tmp0
)
11238 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11239 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11240 <= TYPE_PRECISION (atype
)))
11241 tmp0
= TREE_OPERAND (tmp0
, 0);
11242 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11244 tmp1
= TREE_OPERAND (tmp1
, 0);
11245 one_neg
= !one_neg
;
11247 if (CONVERT_EXPR_P (tmp1
)
11248 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11249 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11250 <= TYPE_PRECISION (atype
)))
11251 tmp1
= TREE_OPERAND (tmp1
, 0);
11252 /* The only case we can still associate with two variables
11253 is if they cancel out. */
11255 || !operand_equal_p (tmp0
, tmp1
, 0))
11258 else if ((var0
&& minus_var1
11259 && ! operand_equal_p (var0
, minus_var1
, 0))
11260 || (minus_var0
&& var1
11261 && ! operand_equal_p (minus_var0
, var1
, 0)))
11265 /* Only do something if we found more than two objects. Otherwise,
11266 nothing has changed and we risk infinite recursion. */
11268 && ((var0
!= 0) + (var1
!= 0)
11269 + (minus_var0
!= 0) + (minus_var1
!= 0)
11270 + (con0
!= 0) + (con1
!= 0)
11271 + (minus_con0
!= 0) + (minus_con1
!= 0)
11272 + (lit0
!= 0) + (lit1
!= 0)
11273 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11275 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11276 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11278 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11279 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11281 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11282 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11285 if (minus_var0
&& var0
)
11287 var0
= associate_trees (loc
, var0
, minus_var0
,
11288 MINUS_EXPR
, atype
);
11291 if (minus_con0
&& con0
)
11293 con0
= associate_trees (loc
, con0
, minus_con0
,
11294 MINUS_EXPR
, atype
);
11298 /* Preserve the MINUS_EXPR if the negative part of the literal is
11299 greater than the positive part. Otherwise, the multiplicative
11300 folding code (i.e extract_muldiv) may be fooled in case
11301 unsigned constants are subtracted, like in the following
11302 example: ((X*2 + 4) - 8U)/2. */
11303 if (minus_lit0
&& lit0
)
11305 if (TREE_CODE (lit0
) == INTEGER_CST
11306 && TREE_CODE (minus_lit0
) == INTEGER_CST
11307 && tree_int_cst_lt (lit0
, minus_lit0
)
11308 /* But avoid ending up with only negated parts. */
11311 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11312 MINUS_EXPR
, atype
);
11317 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11318 MINUS_EXPR
, atype
);
11323 /* Don't introduce overflows through reassociation. */
11324 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11325 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11328 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11329 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11331 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11335 /* Eliminate minus_con0. */
11339 con0
= associate_trees (loc
, con0
, minus_con0
,
11340 MINUS_EXPR
, atype
);
11342 var0
= associate_trees (loc
, var0
, minus_con0
,
11343 MINUS_EXPR
, atype
);
11345 gcc_unreachable ();
11349 /* Eliminate minus_var0. */
11353 con0
= associate_trees (loc
, con0
, minus_var0
,
11354 MINUS_EXPR
, atype
);
11356 gcc_unreachable ();
11361 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11368 case POINTER_DIFF_EXPR
:
11370 /* Fold &a[i] - &a[j] to i-j. */
11371 if (TREE_CODE (arg0
) == ADDR_EXPR
11372 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11373 && TREE_CODE (arg1
) == ADDR_EXPR
11374 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11376 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11377 TREE_OPERAND (arg0
, 0),
11378 TREE_OPERAND (arg1
, 0),
11380 == POINTER_DIFF_EXPR
);
11385 /* Further transformations are not for pointers. */
11386 if (code
== POINTER_DIFF_EXPR
)
11389 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11390 if (TREE_CODE (arg0
) == NEGATE_EXPR
11391 && negate_expr_p (op1
)
11392 /* If arg0 is e.g. unsigned int and type is int, then this could
11393 introduce UB, because if A is INT_MIN at runtime, the original
11394 expression can be well defined while the latter is not.
11396 && !(ANY_INTEGRAL_TYPE_P (type
)
11397 && TYPE_OVERFLOW_UNDEFINED (type
)
11398 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11399 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11400 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11401 fold_convert_loc (loc
, type
,
11402 TREE_OPERAND (arg0
, 0)));
11404 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11405 __complex__ ( x, -y ). This is not the same for SNaNs or if
11406 signed zeros are involved. */
11407 if (!HONOR_SNANS (element_mode (arg0
))
11408 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
11409 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11411 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11412 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11413 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11414 bool arg0rz
= false, arg0iz
= false;
11415 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11416 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11418 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11419 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11420 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11422 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11424 : build1 (REALPART_EXPR
, rtype
, arg1
));
11425 tree ip
= arg0i
? arg0i
11426 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11427 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11429 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11431 tree rp
= arg0r
? arg0r
11432 : build1 (REALPART_EXPR
, rtype
, arg0
);
11433 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11435 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11436 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11441 /* A - B -> A + (-B) if B is easily negatable. */
11442 if (negate_expr_p (op1
)
11443 && ! TYPE_OVERFLOW_SANITIZED (type
)
11444 && ((FLOAT_TYPE_P (type
)
11445 /* Avoid this transformation if B is a positive REAL_CST. */
11446 && (TREE_CODE (op1
) != REAL_CST
11447 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11448 || INTEGRAL_TYPE_P (type
)))
11449 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11450 fold_convert_loc (loc
, type
, arg0
),
11451 negate_expr (op1
));
11453 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11454 one. Make sure the type is not saturating and has the signedness of
11455 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11456 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11457 if ((TREE_CODE (arg0
) == MULT_EXPR
11458 || TREE_CODE (arg1
) == MULT_EXPR
)
11459 && !TYPE_SATURATING (type
)
11460 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11461 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11462 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11464 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11472 if (! FLOAT_TYPE_P (type
))
11474 /* Transform x * -C into -x * C if x is easily negatable. */
11475 if (TREE_CODE (op1
) == INTEGER_CST
11476 && tree_int_cst_sgn (op1
) == -1
11477 && negate_expr_p (op0
)
11478 && negate_expr_p (op1
)
11479 && (tem
= negate_expr (op1
)) != op1
11480 && ! TREE_OVERFLOW (tem
))
11481 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11482 fold_convert_loc (loc
, type
,
11483 negate_expr (op0
)), tem
);
11485 strict_overflow_p
= false;
11486 if (TREE_CODE (arg1
) == INTEGER_CST
11487 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11488 &strict_overflow_p
)) != 0)
11490 if (strict_overflow_p
)
11491 fold_overflow_warning (("assuming signed overflow does not "
11492 "occur when simplifying "
11494 WARN_STRICT_OVERFLOW_MISC
);
11495 return fold_convert_loc (loc
, type
, tem
);
11498 /* Optimize z * conj(z) for integer complex numbers. */
11499 if (TREE_CODE (arg0
) == CONJ_EXPR
11500 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11501 return fold_mult_zconjz (loc
, type
, arg1
);
11502 if (TREE_CODE (arg1
) == CONJ_EXPR
11503 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11504 return fold_mult_zconjz (loc
, type
, arg0
);
11508 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11509 This is not the same for NaNs or if signed zeros are
11511 if (!HONOR_NANS (arg0
)
11512 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
11513 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11514 && TREE_CODE (arg1
) == COMPLEX_CST
11515 && real_zerop (TREE_REALPART (arg1
)))
11517 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11518 if (real_onep (TREE_IMAGPART (arg1
)))
11520 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11521 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11523 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11524 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11526 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11527 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11528 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11532 /* Optimize z * conj(z) for floating point complex numbers.
11533 Guarded by flag_unsafe_math_optimizations as non-finite
11534 imaginary components don't produce scalar results. */
11535 if (flag_unsafe_math_optimizations
11536 && TREE_CODE (arg0
) == CONJ_EXPR
11537 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11538 return fold_mult_zconjz (loc
, type
, arg1
);
11539 if (flag_unsafe_math_optimizations
11540 && TREE_CODE (arg1
) == CONJ_EXPR
11541 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11542 return fold_mult_zconjz (loc
, type
, arg0
);
11547 /* Canonicalize (X & C1) | C2. */
11548 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11549 && TREE_CODE (arg1
) == INTEGER_CST
11550 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11552 int width
= TYPE_PRECISION (type
), w
;
11553 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
11554 wide_int c2
= wi::to_wide (arg1
);
11556 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11557 if ((c1
& c2
) == c1
)
11558 return omit_one_operand_loc (loc
, type
, arg1
,
11559 TREE_OPERAND (arg0
, 0));
11561 wide_int msk
= wi::mask (width
, false,
11562 TYPE_PRECISION (TREE_TYPE (arg1
)));
11564 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11565 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
11567 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11568 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11571 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11572 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11573 mode which allows further optimizations. */
11576 wide_int c3
= wi::bit_and_not (c1
, c2
);
11577 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
11579 wide_int mask
= wi::mask (w
, false,
11580 TYPE_PRECISION (type
));
11581 if (((c1
| c2
) & mask
) == mask
11582 && wi::bit_and_not (c1
, mask
) == 0)
11591 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11592 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
11593 wide_int_to_tree (type
, c3
));
11594 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11598 /* See if this can be simplified into a rotate first. If that
11599 is unsuccessful continue in the association code. */
11603 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11604 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11605 && INTEGRAL_TYPE_P (type
)
11606 && integer_onep (TREE_OPERAND (arg0
, 1))
11607 && integer_onep (arg1
))
11608 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11609 build_zero_cst (TREE_TYPE (arg0
)));
11611 /* See if this can be simplified into a rotate first. If that
11612 is unsuccessful continue in the association code. */
11616 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11617 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11618 && INTEGRAL_TYPE_P (type
)
11619 && integer_onep (TREE_OPERAND (arg0
, 1))
11620 && integer_onep (arg1
))
11623 tem
= TREE_OPERAND (arg0
, 0);
11624 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11625 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11627 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11628 build_zero_cst (TREE_TYPE (tem
)));
11630 /* Fold ~X & 1 as (X & 1) == 0. */
11631 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11632 && INTEGRAL_TYPE_P (type
)
11633 && integer_onep (arg1
))
11636 tem
= TREE_OPERAND (arg0
, 0);
11637 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11638 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11640 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11641 build_zero_cst (TREE_TYPE (tem
)));
11643 /* Fold !X & 1 as X == 0. */
11644 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11645 && integer_onep (arg1
))
11647 tem
= TREE_OPERAND (arg0
, 0);
11648 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11649 build_zero_cst (TREE_TYPE (tem
)));
11652 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11653 multiple of 1 << CST. */
11654 if (TREE_CODE (arg1
) == INTEGER_CST
)
11656 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
11657 wide_int ncst1
= -cst1
;
11658 if ((cst1
& ncst1
) == ncst1
11659 && multiple_of_p (type
, arg0
,
11660 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11661 return fold_convert_loc (loc
, type
, arg0
);
11664 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11666 if (TREE_CODE (arg1
) == INTEGER_CST
11667 && TREE_CODE (arg0
) == MULT_EXPR
11668 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11670 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
11672 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
11675 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11677 else if (masked
!= warg1
)
11679 /* Avoid the transform if arg1 is a mask of some
11680 mode which allows further optimizations. */
11681 int pop
= wi::popcount (warg1
);
11682 if (!(pop
>= BITS_PER_UNIT
11684 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
11685 return fold_build2_loc (loc
, code
, type
, op0
,
11686 wide_int_to_tree (type
, masked
));
11690 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11691 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11692 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11694 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11696 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
11699 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11705 /* Don't touch a floating-point divide by zero unless the mode
11706 of the constant can represent infinity. */
11707 if (TREE_CODE (arg1
) == REAL_CST
11708 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
11709 && real_zerop (arg1
))
11712 /* (-A) / (-B) -> A / B */
11713 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
11714 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11715 TREE_OPERAND (arg0
, 0),
11716 negate_expr (arg1
));
11717 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11718 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11719 negate_expr (arg0
),
11720 TREE_OPERAND (arg1
, 0));
11723 case TRUNC_DIV_EXPR
:
11726 case FLOOR_DIV_EXPR
:
11727 /* Simplify A / (B << N) where A and B are positive and B is
11728 a power of 2, to A >> (N + log2(B)). */
11729 strict_overflow_p
= false;
11730 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11731 && (TYPE_UNSIGNED (type
)
11732 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
11734 tree sval
= TREE_OPERAND (arg1
, 0);
11735 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
11737 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
11738 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
11739 wi::exact_log2 (wi::to_wide (sval
)));
11741 if (strict_overflow_p
)
11742 fold_overflow_warning (("assuming signed overflow does not "
11743 "occur when simplifying A / (B << N)"),
11744 WARN_STRICT_OVERFLOW_MISC
);
11746 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
11748 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11749 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
11755 case ROUND_DIV_EXPR
:
11756 case CEIL_DIV_EXPR
:
11757 case EXACT_DIV_EXPR
:
11758 if (integer_zerop (arg1
))
11761 /* Convert -A / -B to A / B when the type is signed and overflow is
11763 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11764 && TREE_CODE (op0
) == NEGATE_EXPR
11765 && negate_expr_p (op1
))
11767 if (ANY_INTEGRAL_TYPE_P (type
))
11768 fold_overflow_warning (("assuming signed overflow does not occur "
11769 "when distributing negation across "
11771 WARN_STRICT_OVERFLOW_MISC
);
11772 return fold_build2_loc (loc
, code
, type
,
11773 fold_convert_loc (loc
, type
,
11774 TREE_OPERAND (arg0
, 0)),
11775 negate_expr (op1
));
11777 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11778 && TREE_CODE (arg1
) == NEGATE_EXPR
11779 && negate_expr_p (op0
))
11781 if (ANY_INTEGRAL_TYPE_P (type
))
11782 fold_overflow_warning (("assuming signed overflow does not occur "
11783 "when distributing negation across "
11785 WARN_STRICT_OVERFLOW_MISC
);
11786 return fold_build2_loc (loc
, code
, type
,
11788 fold_convert_loc (loc
, type
,
11789 TREE_OPERAND (arg1
, 0)));
11792 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11793 operation, EXACT_DIV_EXPR.
11795 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11796 At one time others generated faster code, it's not clear if they do
11797 after the last round to changes to the DIV code in expmed.c. */
11798 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
11799 && multiple_of_p (type
, arg0
, arg1
))
11800 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
11801 fold_convert (type
, arg0
),
11802 fold_convert (type
, arg1
));
11804 strict_overflow_p
= false;
11805 if (TREE_CODE (arg1
) == INTEGER_CST
11806 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11807 &strict_overflow_p
)) != 0)
11809 if (strict_overflow_p
)
11810 fold_overflow_warning (("assuming signed overflow does not occur "
11811 "when simplifying division"),
11812 WARN_STRICT_OVERFLOW_MISC
);
11813 return fold_convert_loc (loc
, type
, tem
);
11818 case CEIL_MOD_EXPR
:
11819 case FLOOR_MOD_EXPR
:
11820 case ROUND_MOD_EXPR
:
11821 case TRUNC_MOD_EXPR
:
11822 strict_overflow_p
= false;
11823 if (TREE_CODE (arg1
) == INTEGER_CST
11824 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11825 &strict_overflow_p
)) != 0)
11827 if (strict_overflow_p
)
11828 fold_overflow_warning (("assuming signed overflow does not occur "
11829 "when simplifying modulus"),
11830 WARN_STRICT_OVERFLOW_MISC
);
11831 return fold_convert_loc (loc
, type
, tem
);
11840 /* Since negative shift count is not well-defined,
11841 don't try to compute it in the compiler. */
11842 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
11845 prec
= element_precision (type
);
11847 /* If we have a rotate of a bit operation with the rotate count and
11848 the second operand of the bit operation both constant,
11849 permute the two operations. */
11850 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11851 && (TREE_CODE (arg0
) == BIT_AND_EXPR
11852 || TREE_CODE (arg0
) == BIT_IOR_EXPR
11853 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
11854 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11856 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11857 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11858 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
11859 fold_build2_loc (loc
, code
, type
,
11861 fold_build2_loc (loc
, code
, type
,
11865 /* Two consecutive rotates adding up to the some integer
11866 multiple of the precision of the type can be ignored. */
11867 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11868 && TREE_CODE (arg0
) == RROTATE_EXPR
11869 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11870 && wi::umod_trunc (wi::to_wide (arg1
)
11871 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
11873 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11881 case TRUTH_ANDIF_EXPR
:
11882 /* Note that the operands of this must be ints
11883 and their values must be 0 or 1.
11884 ("true" is a fixed value perhaps depending on the language.) */
11885 /* If first arg is constant zero, return it. */
11886 if (integer_zerop (arg0
))
11887 return fold_convert_loc (loc
, type
, arg0
);
11889 case TRUTH_AND_EXPR
:
11890 /* If either arg is constant true, drop it. */
11891 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11892 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11893 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
11894 /* Preserve sequence points. */
11895 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
11896 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11897 /* If second arg is constant zero, result is zero, but first arg
11898 must be evaluated. */
11899 if (integer_zerop (arg1
))
11900 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11901 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11902 case will be handled here. */
11903 if (integer_zerop (arg0
))
11904 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
11906 /* !X && X is always false. */
11907 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11908 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11909 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
11910 /* X && !X is always false. */
11911 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11912 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11913 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11915 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11916 means A >= Y && A != MAX, but in this case we know that
11919 if (!TREE_SIDE_EFFECTS (arg0
)
11920 && !TREE_SIDE_EFFECTS (arg1
))
11922 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
11923 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
11924 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
11926 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
11927 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
11928 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
11931 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
11937 case TRUTH_ORIF_EXPR
:
11938 /* Note that the operands of this must be ints
11939 and their values must be 0 or true.
11940 ("true" is a fixed value perhaps depending on the language.) */
11941 /* If first arg is constant true, return it. */
11942 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11943 return fold_convert_loc (loc
, type
, arg0
);
11945 case TRUTH_OR_EXPR
:
11946 /* If either arg is constant zero, drop it. */
11947 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
11948 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11949 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
11950 /* Preserve sequence points. */
11951 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
11952 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11953 /* If second arg is constant true, result is true, but we must
11954 evaluate first arg. */
11955 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
11956 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11957 /* Likewise for first arg, but note this only occurs here for
11959 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11960 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
11962 /* !X || X is always true. */
11963 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11964 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11965 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
11966 /* X || !X is always true. */
11967 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11968 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11969 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
11971 /* (X && !Y) || (!X && Y) is X ^ Y */
11972 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
11973 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
11975 tree a0
, a1
, l0
, l1
, n0
, n1
;
11977 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11978 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11980 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11981 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11983 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
11984 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
11986 if ((operand_equal_p (n0
, a0
, 0)
11987 && operand_equal_p (n1
, a1
, 0))
11988 || (operand_equal_p (n0
, a1
, 0)
11989 && operand_equal_p (n1
, a0
, 0)))
11990 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
11993 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
11999 case TRUTH_XOR_EXPR
:
12000 /* If the second arg is constant zero, drop it. */
12001 if (integer_zerop (arg1
))
12002 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12003 /* If the second arg is constant true, this is a logical inversion. */
12004 if (integer_onep (arg1
))
12006 tem
= invert_truthvalue_loc (loc
, arg0
);
12007 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12009 /* Identical arguments cancel to zero. */
12010 if (operand_equal_p (arg0
, arg1
, 0))
12011 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12013 /* !X ^ X is always true. */
12014 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12015 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12016 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12018 /* X ^ !X is always true. */
12019 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12020 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12021 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12030 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12031 if (tem
!= NULL_TREE
)
12034 /* bool_var != 1 becomes !bool_var. */
12035 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12036 && code
== NE_EXPR
)
12037 return fold_convert_loc (loc
, type
,
12038 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12039 TREE_TYPE (arg0
), arg0
));
12041 /* bool_var == 0 becomes !bool_var. */
12042 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12043 && code
== EQ_EXPR
)
12044 return fold_convert_loc (loc
, type
,
12045 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12046 TREE_TYPE (arg0
), arg0
));
12048 /* !exp != 0 becomes !exp */
12049 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12050 && code
== NE_EXPR
)
12051 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12053 /* If this is an EQ or NE comparison with zero and ARG0 is
12054 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12055 two operations, but the latter can be done in one less insn
12056 on machines that have only two-operand insns or on which a
12057 constant cannot be the first operand. */
12058 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12059 && integer_zerop (arg1
))
12061 tree arg00
= TREE_OPERAND (arg0
, 0);
12062 tree arg01
= TREE_OPERAND (arg0
, 1);
12063 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12064 && integer_onep (TREE_OPERAND (arg00
, 0)))
12066 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12067 arg01
, TREE_OPERAND (arg00
, 1));
12068 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12069 build_one_cst (TREE_TYPE (arg0
)));
12070 return fold_build2_loc (loc
, code
, type
,
12071 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12074 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12075 && integer_onep (TREE_OPERAND (arg01
, 0)))
12077 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12078 arg00
, TREE_OPERAND (arg01
, 1));
12079 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12080 build_one_cst (TREE_TYPE (arg0
)));
12081 return fold_build2_loc (loc
, code
, type
,
12082 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12087 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12088 C1 is a valid shift constant, and C2 is a power of two, i.e.
12090 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12091 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12092 && integer_zerop (arg1
))
12094 tree arg00
= TREE_OPERAND (arg0
, 0);
12095 STRIP_NOPS (arg00
);
12096 if (TREE_CODE (arg00
) == RSHIFT_EXPR
12097 && TREE_CODE (TREE_OPERAND (arg00
, 1)) == INTEGER_CST
)
12099 tree itype
= TREE_TYPE (arg00
);
12100 tree arg001
= TREE_OPERAND (arg00
, 1);
12101 prec
= TYPE_PRECISION (itype
);
12103 /* Check for a valid shift count. */
12104 if (wi::ltu_p (wi::to_wide (arg001
), prec
))
12106 tree arg01
= TREE_OPERAND (arg0
, 1);
12107 tree arg000
= TREE_OPERAND (arg00
, 0);
12108 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
12109 /* If (C2 << C1) doesn't overflow, then
12110 ((X >> C1) & C2) != 0 can be rewritten as
12111 (X & (C2 << C1)) != 0. */
12112 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
12114 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
,
12116 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12118 return fold_build2_loc (loc
, code
, type
, tem
,
12119 fold_convert_loc (loc
, itype
, arg1
));
12121 /* Otherwise, for signed (arithmetic) shifts,
12122 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12123 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12124 else if (!TYPE_UNSIGNED (itype
))
12125 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
12128 build_int_cst (itype
, 0));
12129 /* Otherwise, of unsigned (logical) shifts,
12130 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12131 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12133 return omit_one_operand_loc (loc
, type
,
12134 code
== EQ_EXPR
? integer_one_node
12135 : integer_zero_node
,
12141 /* If this is a comparison of a field, we may be able to simplify it. */
12142 if ((TREE_CODE (arg0
) == COMPONENT_REF
12143 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12144 /* Handle the constant case even without -O
12145 to make sure the warnings are given. */
12146 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12148 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12153 /* Optimize comparisons of strlen vs zero to a compare of the
12154 first character of the string vs zero. To wit,
12155 strlen(ptr) == 0 => *ptr == 0
12156 strlen(ptr) != 0 => *ptr != 0
12157 Other cases should reduce to one of these two (or a constant)
12158 due to the return value of strlen being unsigned. */
12159 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12161 tree fndecl
= get_callee_fndecl (arg0
);
12164 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12165 && call_expr_nargs (arg0
) == 1
12166 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12170 = build_pointer_type (build_qualified_type (char_type_node
,
12172 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12173 CALL_EXPR_ARG (arg0
, 0));
12174 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12175 return fold_build2_loc (loc
, code
, type
, iref
,
12176 build_int_cst (TREE_TYPE (iref
), 0));
12180 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12181 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12182 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12183 && integer_zerop (arg1
)
12184 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12186 tree arg00
= TREE_OPERAND (arg0
, 0);
12187 tree arg01
= TREE_OPERAND (arg0
, 1);
12188 tree itype
= TREE_TYPE (arg00
);
12189 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12191 if (TYPE_UNSIGNED (itype
))
12193 itype
= signed_type_for (itype
);
12194 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12196 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12197 type
, arg00
, build_zero_cst (itype
));
12201 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12202 (X & C) == 0 when C is a single bit. */
12203 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12204 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12205 && integer_zerop (arg1
)
12206 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12208 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12209 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12210 TREE_OPERAND (arg0
, 1));
12211 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12213 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12217 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12218 constant C is a power of two, i.e. a single bit. */
12219 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12220 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12221 && integer_zerop (arg1
)
12222 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12223 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12224 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12226 tree arg00
= TREE_OPERAND (arg0
, 0);
12227 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12228 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12231 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12232 when is C is a power of two, i.e. a single bit. */
12233 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12234 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12235 && integer_zerop (arg1
)
12236 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12237 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12238 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12240 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12241 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12242 arg000
, TREE_OPERAND (arg0
, 1));
12243 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12244 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12247 if (integer_zerop (arg1
)
12248 && tree_expr_nonzero_p (arg0
))
12250 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
12251 return omit_one_operand_loc (loc
, type
, res
, arg0
);
12254 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12255 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12257 tree arg00
= TREE_OPERAND (arg0
, 0);
12258 tree arg01
= TREE_OPERAND (arg0
, 1);
12259 tree arg10
= TREE_OPERAND (arg1
, 0);
12260 tree arg11
= TREE_OPERAND (arg1
, 1);
12261 tree itype
= TREE_TYPE (arg0
);
12263 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12264 operand_equal_p guarantees no side-effects so we don't need
12265 to use omit_one_operand on Z. */
12266 if (operand_equal_p (arg01
, arg11
, 0))
12267 return fold_build2_loc (loc
, code
, type
, arg00
,
12268 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12270 if (operand_equal_p (arg01
, arg10
, 0))
12271 return fold_build2_loc (loc
, code
, type
, arg00
,
12272 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12274 if (operand_equal_p (arg00
, arg11
, 0))
12275 return fold_build2_loc (loc
, code
, type
, arg01
,
12276 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12278 if (operand_equal_p (arg00
, arg10
, 0))
12279 return fold_build2_loc (loc
, code
, type
, arg01
,
12280 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12283 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12284 if (TREE_CODE (arg01
) == INTEGER_CST
12285 && TREE_CODE (arg11
) == INTEGER_CST
)
12287 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12288 fold_convert_loc (loc
, itype
, arg11
));
12289 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12290 return fold_build2_loc (loc
, code
, type
, tem
,
12291 fold_convert_loc (loc
, itype
, arg10
));
12295 /* Attempt to simplify equality/inequality comparisons of complex
12296 values. Only lower the comparison if the result is known or
12297 can be simplified to a single scalar comparison. */
12298 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12299 || TREE_CODE (arg0
) == COMPLEX_CST
)
12300 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12301 || TREE_CODE (arg1
) == COMPLEX_CST
))
12303 tree real0
, imag0
, real1
, imag1
;
12306 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12308 real0
= TREE_OPERAND (arg0
, 0);
12309 imag0
= TREE_OPERAND (arg0
, 1);
12313 real0
= TREE_REALPART (arg0
);
12314 imag0
= TREE_IMAGPART (arg0
);
12317 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12319 real1
= TREE_OPERAND (arg1
, 0);
12320 imag1
= TREE_OPERAND (arg1
, 1);
12324 real1
= TREE_REALPART (arg1
);
12325 imag1
= TREE_IMAGPART (arg1
);
12328 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12329 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12331 if (integer_zerop (rcond
))
12333 if (code
== EQ_EXPR
)
12334 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12336 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12340 if (code
== NE_EXPR
)
12341 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12343 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12347 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12348 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12350 if (integer_zerop (icond
))
12352 if (code
== EQ_EXPR
)
12353 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12355 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12359 if (code
== NE_EXPR
)
12360 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12362 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12373 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12374 if (tem
!= NULL_TREE
)
12377 /* Transform comparisons of the form X +- C CMP X. */
12378 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12379 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12380 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12381 && !HONOR_SNANS (arg0
))
12383 tree arg01
= TREE_OPERAND (arg0
, 1);
12384 enum tree_code code0
= TREE_CODE (arg0
);
12385 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12387 /* (X - c) > X becomes false. */
12388 if (code
== GT_EXPR
12389 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12390 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12391 return constant_boolean_node (0, type
);
12393 /* Likewise (X + c) < X becomes false. */
12394 if (code
== LT_EXPR
12395 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12396 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12397 return constant_boolean_node (0, type
);
12399 /* Convert (X - c) <= X to true. */
12400 if (!HONOR_NANS (arg1
)
12402 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12403 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12404 return constant_boolean_node (1, type
);
12406 /* Convert (X + c) >= X to true. */
12407 if (!HONOR_NANS (arg1
)
12409 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12410 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12411 return constant_boolean_node (1, type
);
12414 /* If we are comparing an ABS_EXPR with a constant, we can
12415 convert all the cases into explicit comparisons, but they may
12416 well not be faster than doing the ABS and one comparison.
12417 But ABS (X) <= C is a range comparison, which becomes a subtraction
12418 and a comparison, and is probably faster. */
12419 if (code
== LE_EXPR
12420 && TREE_CODE (arg1
) == INTEGER_CST
12421 && TREE_CODE (arg0
) == ABS_EXPR
12422 && ! TREE_SIDE_EFFECTS (arg0
)
12423 && (tem
= negate_expr (arg1
)) != 0
12424 && TREE_CODE (tem
) == INTEGER_CST
12425 && !TREE_OVERFLOW (tem
))
12426 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12427 build2 (GE_EXPR
, type
,
12428 TREE_OPERAND (arg0
, 0), tem
),
12429 build2 (LE_EXPR
, type
,
12430 TREE_OPERAND (arg0
, 0), arg1
));
12432 /* Convert ABS_EXPR<x> >= 0 to true. */
12433 strict_overflow_p
= false;
12434 if (code
== GE_EXPR
12435 && (integer_zerop (arg1
)
12436 || (! HONOR_NANS (arg0
)
12437 && real_zerop (arg1
)))
12438 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12440 if (strict_overflow_p
)
12441 fold_overflow_warning (("assuming signed overflow does not occur "
12442 "when simplifying comparison of "
12443 "absolute value and zero"),
12444 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12445 return omit_one_operand_loc (loc
, type
,
12446 constant_boolean_node (true, type
),
12450 /* Convert ABS_EXPR<x> < 0 to false. */
12451 strict_overflow_p
= false;
12452 if (code
== LT_EXPR
12453 && (integer_zerop (arg1
) || real_zerop (arg1
))
12454 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12456 if (strict_overflow_p
)
12457 fold_overflow_warning (("assuming signed overflow does not occur "
12458 "when simplifying comparison of "
12459 "absolute value and zero"),
12460 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12461 return omit_one_operand_loc (loc
, type
,
12462 constant_boolean_node (false, type
),
12466 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12467 and similarly for >= into !=. */
12468 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12469 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12470 && TREE_CODE (arg1
) == LSHIFT_EXPR
12471 && integer_onep (TREE_OPERAND (arg1
, 0)))
12472 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12473 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12474 TREE_OPERAND (arg1
, 1)),
12475 build_zero_cst (TREE_TYPE (arg0
)));
12477 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12478 otherwise Y might be >= # of bits in X's type and thus e.g.
12479 (unsigned char) (1 << Y) for Y 15 might be 0.
12480 If the cast is widening, then 1 << Y should have unsigned type,
12481 otherwise if Y is number of bits in the signed shift type minus 1,
12482 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12483 31 might be 0xffffffff80000000. */
12484 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12485 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12486 && CONVERT_EXPR_P (arg1
)
12487 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12488 && (element_precision (TREE_TYPE (arg1
))
12489 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12490 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12491 || (element_precision (TREE_TYPE (arg1
))
12492 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12493 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12495 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12496 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12497 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12498 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12499 build_zero_cst (TREE_TYPE (arg0
)));
12504 case UNORDERED_EXPR
:
12512 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12514 tree targ0
= strip_float_extensions (arg0
);
12515 tree targ1
= strip_float_extensions (arg1
);
12516 tree newtype
= TREE_TYPE (targ0
);
12518 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
12519 newtype
= TREE_TYPE (targ1
);
12521 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
12522 return fold_build2_loc (loc
, code
, type
,
12523 fold_convert_loc (loc
, newtype
, targ0
),
12524 fold_convert_loc (loc
, newtype
, targ1
));
12529 case COMPOUND_EXPR
:
12530 /* When pedantic, a compound expression can be neither an lvalue
12531 nor an integer constant expression. */
12532 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12534 /* Don't let (0, 0) be null pointer constant. */
12535 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
12536 : fold_convert_loc (loc
, type
, arg1
);
12537 return pedantic_non_lvalue_loc (loc
, tem
);
12540 /* An ASSERT_EXPR should never be passed to fold_binary. */
12541 gcc_unreachable ();
12545 } /* switch (code) */
12548 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12549 ((A & N) + B) & M -> (A + B) & M
12550 Similarly if (N & M) == 0,
12551 ((A | N) + B) & M -> (A + B) & M
12552 and for - instead of + (or unary - instead of +)
12553 and/or ^ instead of |.
12554 If B is constant and (B & M) == 0, fold into A & M.
12556 This function is a helper for match.pd patterns. Return non-NULL
12557 type in which the simplified operation should be performed only
12558 if any optimization is possible.
12560 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12561 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12562 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12565 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12566 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12567 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12570 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12571 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12572 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12574 || (cst1
& (cst1
+ 1)) != 0
12575 || !INTEGRAL_TYPE_P (type
)
12576 || (!TYPE_OVERFLOW_WRAPS (type
)
12577 && TREE_CODE (type
) != INTEGER_TYPE
)
12578 || (wi::max_value (type
) & cst1
) != cst1
)
12581 enum tree_code codes
[2] = { code00
, code01
};
12582 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12586 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12587 arg1 (M) is == (1LL << cst) - 1.
12588 Store C into PMOP[0] and D into PMOP[1]. */
12591 which
= code
!= NEGATE_EXPR
;
12593 for (; which
>= 0; which
--)
12594 switch (codes
[which
])
12599 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12600 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12601 if (codes
[which
] == BIT_AND_EXPR
)
12606 else if (cst0
!= 0)
12608 /* If C or D is of the form (A & N) where
12609 (N & M) == M, or of the form (A | N) or
12610 (A ^ N) where (N & M) == 0, replace it with A. */
12611 pmop
[which
] = arg0xx
[2 * which
];
12614 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
12616 /* If C or D is a N where (N & M) == 0, it can be
12617 omitted (replaced with 0). */
12618 if ((code
== PLUS_EXPR
12619 || (code
== MINUS_EXPR
&& which
== 0))
12620 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
12621 pmop
[which
] = build_int_cst (type
, 0);
12622 /* Similarly, with C - N where (-N & M) == 0. */
12623 if (code
== MINUS_EXPR
12625 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
12626 pmop
[which
] = build_int_cst (type
, 0);
12629 gcc_unreachable ();
12632 /* Only build anything new if we optimized one or both arguments above. */
12633 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
12636 if (TYPE_OVERFLOW_WRAPS (type
))
12639 return unsigned_type_for (type
);
12642 /* Used by contains_label_[p1]. */
12644 struct contains_label_data
12646 hash_set
<tree
> *pset
;
12647 bool inside_switch_p
;
12650 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12651 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12652 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12655 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
12657 contains_label_data
*d
= (contains_label_data
*) data
;
12658 switch (TREE_CODE (*tp
))
12663 case CASE_LABEL_EXPR
:
12664 if (!d
->inside_switch_p
)
12669 if (!d
->inside_switch_p
)
12671 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
12673 d
->inside_switch_p
= true;
12674 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
12676 d
->inside_switch_p
= false;
12677 *walk_subtrees
= 0;
12682 *walk_subtrees
= 0;
12690 /* Return whether the sub-tree ST contains a label which is accessible from
12691 outside the sub-tree. */
12694 contains_label_p (tree st
)
12696 hash_set
<tree
> pset
;
12697 contains_label_data data
= { &pset
, false };
12698 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
12701 /* Fold a ternary expression of code CODE and type TYPE with operands
12702 OP0, OP1, and OP2. Return the folded expression if folding is
12703 successful. Otherwise, return NULL_TREE. */
12706 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
12707 tree op0
, tree op1
, tree op2
)
12710 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
12711 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
12713 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
12714 && TREE_CODE_LENGTH (code
) == 3);
12716 /* If this is a commutative operation, and OP0 is a constant, move it
12717 to OP1 to reduce the number of tests below. */
12718 if (commutative_ternary_tree_code (code
)
12719 && tree_swap_operands_p (op0
, op1
))
12720 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
12722 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
12726 /* Strip any conversions that don't change the mode. This is safe
12727 for every expression, except for a comparison expression because
12728 its signedness is derived from its operands. So, in the latter
12729 case, only strip conversions that don't change the signedness.
12731 Note that this is done as an internal manipulation within the
12732 constant folder, in order to find the simplest representation of
12733 the arguments so that their form can be studied. In any cases,
12734 the appropriate type conversions should be put back in the tree
12735 that will get out of the constant folder. */
12756 case COMPONENT_REF
:
12757 if (TREE_CODE (arg0
) == CONSTRUCTOR
12758 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
12760 unsigned HOST_WIDE_INT idx
;
12762 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
12769 case VEC_COND_EXPR
:
12770 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12771 so all simple results must be passed through pedantic_non_lvalue. */
12772 if (TREE_CODE (arg0
) == INTEGER_CST
)
12774 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
12775 tem
= integer_zerop (arg0
) ? op2
: op1
;
12776 /* Only optimize constant conditions when the selected branch
12777 has the same type as the COND_EXPR. This avoids optimizing
12778 away "c ? x : throw", where the throw has a void type.
12779 Avoid throwing away that operand which contains label. */
12780 if ((!TREE_SIDE_EFFECTS (unused_op
)
12781 || !contains_label_p (unused_op
))
12782 && (! VOID_TYPE_P (TREE_TYPE (tem
))
12783 || VOID_TYPE_P (type
)))
12784 return pedantic_non_lvalue_loc (loc
, tem
);
12787 else if (TREE_CODE (arg0
) == VECTOR_CST
)
12789 unsigned HOST_WIDE_INT nelts
;
12790 if ((TREE_CODE (arg1
) == VECTOR_CST
12791 || TREE_CODE (arg1
) == CONSTRUCTOR
)
12792 && (TREE_CODE (arg2
) == VECTOR_CST
12793 || TREE_CODE (arg2
) == CONSTRUCTOR
)
12794 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
12796 vec_perm_builder
sel (nelts
, nelts
, 1);
12797 for (unsigned int i
= 0; i
< nelts
; i
++)
12799 tree val
= VECTOR_CST_ELT (arg0
, i
);
12800 if (integer_all_onesp (val
))
12801 sel
.quick_push (i
);
12802 else if (integer_zerop (val
))
12803 sel
.quick_push (nelts
+ i
);
12804 else /* Currently unreachable. */
12807 vec_perm_indices
indices (sel
, 2, nelts
);
12808 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
12809 if (t
!= NULL_TREE
)
12814 /* If we have A op B ? A : C, we may be able to convert this to a
12815 simpler expression, depending on the operation and the values
12816 of B and C. Signed zeros prevent all of these transformations,
12817 for reasons given above each one.
12819 Also try swapping the arguments and inverting the conditional. */
12820 if (COMPARISON_CLASS_P (arg0
)
12821 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
12822 && !HONOR_SIGNED_ZEROS (element_mode (op1
)))
12824 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
12829 if (COMPARISON_CLASS_P (arg0
)
12830 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
12831 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
12833 location_t loc0
= expr_location_or (arg0
, loc
);
12834 tem
= fold_invert_truthvalue (loc0
, arg0
);
12835 if (tem
&& COMPARISON_CLASS_P (tem
))
12837 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
12843 /* If the second operand is simpler than the third, swap them
12844 since that produces better jump optimization results. */
12845 if (truth_value_p (TREE_CODE (arg0
))
12846 && tree_swap_operands_p (op1
, op2
))
12848 location_t loc0
= expr_location_or (arg0
, loc
);
12849 /* See if this can be inverted. If it can't, possibly because
12850 it was a floating-point inequality comparison, don't do
12852 tem
= fold_invert_truthvalue (loc0
, arg0
);
12854 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
12857 /* Convert A ? 1 : 0 to simply A. */
12858 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
12859 : (integer_onep (op1
)
12860 && !VECTOR_TYPE_P (type
)))
12861 && integer_zerop (op2
)
12862 /* If we try to convert OP0 to our type, the
12863 call to fold will try to move the conversion inside
12864 a COND, which will recurse. In that case, the COND_EXPR
12865 is probably the best choice, so leave it alone. */
12866 && type
== TREE_TYPE (arg0
))
12867 return pedantic_non_lvalue_loc (loc
, arg0
);
12869 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12870 over COND_EXPR in cases such as floating point comparisons. */
12871 if (integer_zerop (op1
)
12872 && code
== COND_EXPR
12873 && integer_onep (op2
)
12874 && !VECTOR_TYPE_P (type
)
12875 && truth_value_p (TREE_CODE (arg0
)))
12876 return pedantic_non_lvalue_loc (loc
,
12877 fold_convert_loc (loc
, type
,
12878 invert_truthvalue_loc (loc
,
12881 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12882 if (TREE_CODE (arg0
) == LT_EXPR
12883 && integer_zerop (TREE_OPERAND (arg0
, 1))
12884 && integer_zerop (op2
)
12885 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
12887 /* sign_bit_p looks through both zero and sign extensions,
12888 but for this optimization only sign extensions are
12890 tree tem2
= TREE_OPERAND (arg0
, 0);
12891 while (tem
!= tem2
)
12893 if (TREE_CODE (tem2
) != NOP_EXPR
12894 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
12899 tem2
= TREE_OPERAND (tem2
, 0);
12901 /* sign_bit_p only checks ARG1 bits within A's precision.
12902 If <sign bit of A> has wider type than A, bits outside
12903 of A's precision in <sign bit of A> need to be checked.
12904 If they are all 0, this optimization needs to be done
12905 in unsigned A's type, if they are all 1 in signed A's type,
12906 otherwise this can't be done. */
12908 && TYPE_PRECISION (TREE_TYPE (tem
))
12909 < TYPE_PRECISION (TREE_TYPE (arg1
))
12910 && TYPE_PRECISION (TREE_TYPE (tem
))
12911 < TYPE_PRECISION (type
))
12913 int inner_width
, outer_width
;
12916 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
12917 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
12918 if (outer_width
> TYPE_PRECISION (type
))
12919 outer_width
= TYPE_PRECISION (type
);
12921 wide_int mask
= wi::shifted_mask
12922 (inner_width
, outer_width
- inner_width
, false,
12923 TYPE_PRECISION (TREE_TYPE (arg1
)));
12925 wide_int common
= mask
& wi::to_wide (arg1
);
12926 if (common
== mask
)
12928 tem_type
= signed_type_for (TREE_TYPE (tem
));
12929 tem
= fold_convert_loc (loc
, tem_type
, tem
);
12931 else if (common
== 0)
12933 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
12934 tem
= fold_convert_loc (loc
, tem_type
, tem
);
12942 fold_convert_loc (loc
, type
,
12943 fold_build2_loc (loc
, BIT_AND_EXPR
,
12944 TREE_TYPE (tem
), tem
,
12945 fold_convert_loc (loc
,
12950 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12951 already handled above. */
12952 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12953 && integer_onep (TREE_OPERAND (arg0
, 1))
12954 && integer_zerop (op2
)
12955 && integer_pow2p (arg1
))
12957 tree tem
= TREE_OPERAND (arg0
, 0);
12959 if (TREE_CODE (tem
) == RSHIFT_EXPR
12960 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
12961 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
12962 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
12963 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
12964 fold_convert_loc (loc
, type
,
12965 TREE_OPERAND (tem
, 0)),
12969 /* A & N ? N : 0 is simply A & N if N is a power of two. This
12970 is probably obsolete because the first operand should be a
12971 truth value (that's why we have the two cases above), but let's
12972 leave it in until we can confirm this for all front-ends. */
12973 if (integer_zerop (op2
)
12974 && TREE_CODE (arg0
) == NE_EXPR
12975 && integer_zerop (TREE_OPERAND (arg0
, 1))
12976 && integer_pow2p (arg1
)
12977 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12978 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12979 arg1
, OEP_ONLY_CONST
)
12980 /* operand_equal_p compares just value, not precision, so e.g.
12981 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
12982 second operand 32-bit -128, which is not a power of two (or vice
12984 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
12985 return pedantic_non_lvalue_loc (loc
,
12986 fold_convert_loc (loc
, type
,
12987 TREE_OPERAND (arg0
,
12990 /* Disable the transformations below for vectors, since
12991 fold_binary_op_with_conditional_arg may undo them immediately,
12992 yielding an infinite loop. */
12993 if (code
== VEC_COND_EXPR
)
12996 /* Convert A ? B : 0 into A && B if A and B are truth values. */
12997 if (integer_zerop (op2
)
12998 && truth_value_p (TREE_CODE (arg0
))
12999 && truth_value_p (TREE_CODE (arg1
))
13000 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13001 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13002 : TRUTH_ANDIF_EXPR
,
13003 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13005 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13006 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13007 && truth_value_p (TREE_CODE (arg0
))
13008 && truth_value_p (TREE_CODE (arg1
))
13009 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13011 location_t loc0
= expr_location_or (arg0
, loc
);
13012 /* Only perform transformation if ARG0 is easily inverted. */
13013 tem
= fold_invert_truthvalue (loc0
, arg0
);
13015 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13018 type
, fold_convert_loc (loc
, type
, tem
),
13022 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13023 if (integer_zerop (arg1
)
13024 && truth_value_p (TREE_CODE (arg0
))
13025 && truth_value_p (TREE_CODE (op2
))
13026 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13028 location_t loc0
= expr_location_or (arg0
, loc
);
13029 /* Only perform transformation if ARG0 is easily inverted. */
13030 tem
= fold_invert_truthvalue (loc0
, arg0
);
13032 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13033 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13034 type
, fold_convert_loc (loc
, type
, tem
),
13038 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13039 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13040 && truth_value_p (TREE_CODE (arg0
))
13041 && truth_value_p (TREE_CODE (op2
))
13042 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13043 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13044 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13045 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13050 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13051 of fold_ternary on them. */
13052 gcc_unreachable ();
13054 case BIT_FIELD_REF
:
13055 if (TREE_CODE (arg0
) == VECTOR_CST
13056 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13057 || (VECTOR_TYPE_P (type
)
13058 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13059 && tree_fits_uhwi_p (op1
)
13060 && tree_fits_uhwi_p (op2
))
13062 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13063 unsigned HOST_WIDE_INT width
13064 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13065 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13066 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13067 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13070 && (idx
% width
) == 0
13071 && (n
% width
) == 0
13072 && known_le ((idx
+ n
) / width
,
13073 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13078 if (TREE_CODE (arg0
) == VECTOR_CST
)
13082 tem
= VECTOR_CST_ELT (arg0
, idx
);
13083 if (VECTOR_TYPE_P (type
))
13084 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13088 tree_vector_builder
vals (type
, n
, 1);
13089 for (unsigned i
= 0; i
< n
; ++i
)
13090 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13091 return vals
.build ();
13096 /* On constants we can use native encode/interpret to constant
13097 fold (nearly) all BIT_FIELD_REFs. */
13098 if (CONSTANT_CLASS_P (arg0
)
13099 && can_native_interpret_type_p (type
)
13100 && BITS_PER_UNIT
== 8
13101 && tree_fits_uhwi_p (op1
)
13102 && tree_fits_uhwi_p (op2
))
13104 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13105 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13106 /* Limit us to a reasonable amount of work. To relax the
13107 other limitations we need bit-shifting of the buffer
13108 and rounding up the size. */
13109 if (bitpos
% BITS_PER_UNIT
== 0
13110 && bitsize
% BITS_PER_UNIT
== 0
13111 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13113 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13114 unsigned HOST_WIDE_INT len
13115 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13116 bitpos
/ BITS_PER_UNIT
);
13118 && len
* BITS_PER_UNIT
>= bitsize
)
13120 tree v
= native_interpret_expr (type
, b
,
13121 bitsize
/ BITS_PER_UNIT
);
13130 case VEC_PERM_EXPR
:
13131 /* Perform constant folding of BIT_INSERT_EXPR. */
13132 if (TREE_CODE (arg2
) == VECTOR_CST
13133 && TREE_CODE (op0
) == VECTOR_CST
13134 && TREE_CODE (op1
) == VECTOR_CST
)
13136 /* Build a vector of integers from the tree mask. */
13137 vec_perm_builder builder
;
13138 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13141 /* Create a vec_perm_indices for the integer vector. */
13142 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13143 bool single_arg
= (op0
== op1
);
13144 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13145 return fold_vec_perm (type
, op0
, op1
, sel
);
13149 case BIT_INSERT_EXPR
:
13150 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13151 if (TREE_CODE (arg0
) == INTEGER_CST
13152 && TREE_CODE (arg1
) == INTEGER_CST
)
13154 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13155 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13156 wide_int tem
= (wi::to_wide (arg0
)
13157 & wi::shifted_mask (bitpos
, bitsize
, true,
13158 TYPE_PRECISION (type
)));
13160 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13162 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13164 else if (TREE_CODE (arg0
) == VECTOR_CST
13165 && CONSTANT_CLASS_P (arg1
)
13166 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13169 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13170 unsigned HOST_WIDE_INT elsize
13171 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13172 if (bitpos
% elsize
== 0)
13174 unsigned k
= bitpos
/ elsize
;
13175 unsigned HOST_WIDE_INT nelts
;
13176 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13178 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13180 tree_vector_builder
elts (type
, nelts
, 1);
13181 elts
.quick_grow (nelts
);
13182 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13183 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13184 return elts
.build ();
13192 } /* switch (code) */
13195 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13196 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13197 constructor element index of the value returned. If the element is
13198 not found NULL_TREE is returned and *CTOR_IDX is updated to
13199 the index of the element after the ACCESS_INDEX position (which
13200 may be outside of the CTOR array). */
13203 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13204 unsigned *ctor_idx
)
13206 tree index_type
= NULL_TREE
;
13207 signop index_sgn
= UNSIGNED
;
13208 offset_int low_bound
= 0;
13210 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13212 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13213 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13215 /* Static constructors for variably sized objects makes no sense. */
13216 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13217 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13218 /* ??? When it is obvious that the range is signed, treat it so. */
13219 if (TYPE_UNSIGNED (index_type
)
13220 && TYPE_MAX_VALUE (domain_type
)
13221 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13222 TYPE_MIN_VALUE (domain_type
)))
13224 index_sgn
= SIGNED
;
13226 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13231 index_sgn
= TYPE_SIGN (index_type
);
13232 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13238 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13241 offset_int index
= low_bound
;
13243 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13245 offset_int max_index
= index
;
13248 bool first_p
= true;
13250 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13252 /* Array constructor might explicitly set index, or specify a range,
13253 or leave index NULL meaning that it is next index after previous
13257 if (TREE_CODE (cfield
) == INTEGER_CST
)
13259 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13262 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13263 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13266 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13268 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13273 index
= max_index
+ 1;
13275 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13276 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13282 /* Do we have match? */
13283 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13285 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13292 else if (in_gimple_form
)
13293 /* We're past the element we search for. Note during parsing
13294 the elements might not be sorted.
13295 ??? We should use a binary search and a flag on the
13296 CONSTRUCTOR as to whether elements are sorted in declaration
13305 /* Perform constant folding and related simplification of EXPR.
13306 The related simplifications include x*1 => x, x*0 => 0, etc.,
13307 and application of the associative law.
13308 NOP_EXPR conversions may be removed freely (as long as we
13309 are careful not to change the type of the overall expression).
13310 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13311 but we can constant-fold them if they have constant operands. */
13313 #ifdef ENABLE_FOLD_CHECKING
13314 # define fold(x) fold_1 (x)
13315 static tree
fold_1 (tree
);
13321 const tree t
= expr
;
13322 enum tree_code code
= TREE_CODE (t
);
13323 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13325 location_t loc
= EXPR_LOCATION (expr
);
13327 /* Return right away if a constant. */
13328 if (kind
== tcc_constant
)
13331 /* CALL_EXPR-like objects with variable numbers of operands are
13332 treated specially. */
13333 if (kind
== tcc_vl_exp
)
13335 if (code
== CALL_EXPR
)
13337 tem
= fold_call_expr (loc
, expr
, false);
13338 return tem
? tem
: expr
;
13343 if (IS_EXPR_CODE_CLASS (kind
))
13345 tree type
= TREE_TYPE (t
);
13346 tree op0
, op1
, op2
;
13348 switch (TREE_CODE_LENGTH (code
))
13351 op0
= TREE_OPERAND (t
, 0);
13352 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13353 return tem
? tem
: expr
;
13355 op0
= TREE_OPERAND (t
, 0);
13356 op1
= TREE_OPERAND (t
, 1);
13357 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13358 return tem
? tem
: expr
;
13360 op0
= TREE_OPERAND (t
, 0);
13361 op1
= TREE_OPERAND (t
, 1);
13362 op2
= TREE_OPERAND (t
, 2);
13363 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13364 return tem
? tem
: expr
;
13374 tree op0
= TREE_OPERAND (t
, 0);
13375 tree op1
= TREE_OPERAND (t
, 1);
13377 if (TREE_CODE (op1
) == INTEGER_CST
13378 && TREE_CODE (op0
) == CONSTRUCTOR
13379 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13381 tree val
= get_array_ctor_element_at_index (op0
,
13382 wi::to_offset (op1
));
13390 /* Return a VECTOR_CST if possible. */
13393 tree type
= TREE_TYPE (t
);
13394 if (TREE_CODE (type
) != VECTOR_TYPE
)
13399 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13400 if (! CONSTANT_CLASS_P (val
))
13403 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13407 return fold (DECL_INITIAL (t
));
13411 } /* switch (code) */
13414 #ifdef ENABLE_FOLD_CHECKING
13417 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13418 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13419 static void fold_check_failed (const_tree
, const_tree
);
13420 void print_fold_checksum (const_tree
);
13422 /* When --enable-checking=fold, compute a digest of expr before
13423 and after actual fold call to see if fold did not accidentally
13424 change original expr. */
13430 struct md5_ctx ctx
;
13431 unsigned char checksum_before
[16], checksum_after
[16];
13432 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13434 md5_init_ctx (&ctx
);
13435 fold_checksum_tree (expr
, &ctx
, &ht
);
13436 md5_finish_ctx (&ctx
, checksum_before
);
13439 ret
= fold_1 (expr
);
13441 md5_init_ctx (&ctx
);
13442 fold_checksum_tree (expr
, &ctx
, &ht
);
13443 md5_finish_ctx (&ctx
, checksum_after
);
13445 if (memcmp (checksum_before
, checksum_after
, 16))
13446 fold_check_failed (expr
, ret
);
13452 print_fold_checksum (const_tree expr
)
13454 struct md5_ctx ctx
;
13455 unsigned char checksum
[16], cnt
;
13456 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13458 md5_init_ctx (&ctx
);
13459 fold_checksum_tree (expr
, &ctx
, &ht
);
13460 md5_finish_ctx (&ctx
, checksum
);
13461 for (cnt
= 0; cnt
< 16; ++cnt
)
13462 fprintf (stderr
, "%02x", checksum
[cnt
]);
13463 putc ('\n', stderr
);
13467 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13469 internal_error ("fold check: original tree changed by fold");
13473 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13474 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13476 const tree_node
**slot
;
13477 enum tree_code code
;
13478 union tree_node
*buf
;
13484 slot
= ht
->find_slot (expr
, INSERT
);
13488 code
= TREE_CODE (expr
);
13489 if (TREE_CODE_CLASS (code
) == tcc_declaration
13490 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13492 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13493 size_t sz
= tree_size (expr
);
13494 buf
= XALLOCAVAR (union tree_node
, sz
);
13495 memcpy ((char *) buf
, expr
, sz
);
13496 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13497 buf
->decl_with_vis
.symtab_node
= NULL
;
13498 buf
->base
.nowarning_flag
= 0;
13501 else if (TREE_CODE_CLASS (code
) == tcc_type
13502 && (TYPE_POINTER_TO (expr
)
13503 || TYPE_REFERENCE_TO (expr
)
13504 || TYPE_CACHED_VALUES_P (expr
)
13505 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13506 || TYPE_NEXT_VARIANT (expr
)
13507 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13509 /* Allow these fields to be modified. */
13511 size_t sz
= tree_size (expr
);
13512 buf
= XALLOCAVAR (union tree_node
, sz
);
13513 memcpy ((char *) buf
, expr
, sz
);
13514 expr
= tmp
= (tree
) buf
;
13515 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13516 TYPE_POINTER_TO (tmp
) = NULL
;
13517 TYPE_REFERENCE_TO (tmp
) = NULL
;
13518 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13519 TYPE_ALIAS_SET (tmp
) = -1;
13520 if (TYPE_CACHED_VALUES_P (tmp
))
13522 TYPE_CACHED_VALUES_P (tmp
) = 0;
13523 TYPE_CACHED_VALUES (tmp
) = NULL
;
13526 else if (TREE_NO_WARNING (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13528 /* Allow TREE_NO_WARNING to be set. Perhaps we shouldn't allow that
13529 and change builtins.c etc. instead - see PR89543. */
13530 size_t sz
= tree_size (expr
);
13531 buf
= XALLOCAVAR (union tree_node
, sz
);
13532 memcpy ((char *) buf
, expr
, sz
);
13533 buf
->base
.nowarning_flag
= 0;
13536 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13537 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13538 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13539 if (TREE_CODE_CLASS (code
) != tcc_type
13540 && TREE_CODE_CLASS (code
) != tcc_declaration
13541 && code
!= TREE_LIST
13542 && code
!= SSA_NAME
13543 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13544 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13545 switch (TREE_CODE_CLASS (code
))
13551 md5_process_bytes (TREE_STRING_POINTER (expr
),
13552 TREE_STRING_LENGTH (expr
), ctx
);
13555 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13556 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13559 len
= vector_cst_encoded_nelts (expr
);
13560 for (i
= 0; i
< len
; ++i
)
13561 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13567 case tcc_exceptional
:
13571 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13572 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13573 expr
= TREE_CHAIN (expr
);
13574 goto recursive_label
;
13577 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13578 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13584 case tcc_expression
:
13585 case tcc_reference
:
13586 case tcc_comparison
:
13589 case tcc_statement
:
13591 len
= TREE_OPERAND_LENGTH (expr
);
13592 for (i
= 0; i
< len
; ++i
)
13593 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13595 case tcc_declaration
:
13596 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13597 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13598 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13600 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13601 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13602 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13603 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13604 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13607 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13609 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13611 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13612 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13614 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13618 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13619 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13620 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
13621 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
13622 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
13623 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
13624 if (INTEGRAL_TYPE_P (expr
)
13625 || SCALAR_FLOAT_TYPE_P (expr
))
13627 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
13628 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
13630 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
13631 if (TREE_CODE (expr
) == RECORD_TYPE
13632 || TREE_CODE (expr
) == UNION_TYPE
13633 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
13634 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
13635 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
13642 /* Helper function for outputting the checksum of a tree T. When
13643 debugging with gdb, you can "define mynext" to be "next" followed
13644 by "call debug_fold_checksum (op0)", then just trace down till the
13647 DEBUG_FUNCTION
void
13648 debug_fold_checksum (const_tree t
)
13651 unsigned char checksum
[16];
13652 struct md5_ctx ctx
;
13653 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13655 md5_init_ctx (&ctx
);
13656 fold_checksum_tree (t
, &ctx
, &ht
);
13657 md5_finish_ctx (&ctx
, checksum
);
13660 for (i
= 0; i
< 16; i
++)
13661 fprintf (stderr
, "%d ", checksum
[i
]);
13663 fprintf (stderr
, "\n");
13668 /* Fold a unary tree expression with code CODE of type TYPE with an
13669 operand OP0. LOC is the location of the resulting expression.
13670 Return a folded expression if successful. Otherwise, return a tree
13671 expression with code CODE of type TYPE with an operand OP0. */
13674 fold_build1_loc (location_t loc
,
13675 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
13678 #ifdef ENABLE_FOLD_CHECKING
13679 unsigned char checksum_before
[16], checksum_after
[16];
13680 struct md5_ctx ctx
;
13681 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13683 md5_init_ctx (&ctx
);
13684 fold_checksum_tree (op0
, &ctx
, &ht
);
13685 md5_finish_ctx (&ctx
, checksum_before
);
13689 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13691 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
13693 #ifdef ENABLE_FOLD_CHECKING
13694 md5_init_ctx (&ctx
);
13695 fold_checksum_tree (op0
, &ctx
, &ht
);
13696 md5_finish_ctx (&ctx
, checksum_after
);
13698 if (memcmp (checksum_before
, checksum_after
, 16))
13699 fold_check_failed (op0
, tem
);
13704 /* Fold a binary tree expression with code CODE of type TYPE with
13705 operands OP0 and OP1. LOC is the location of the resulting
13706 expression. Return a folded expression if successful. Otherwise,
13707 return a tree expression with code CODE of type TYPE with operands
13711 fold_build2_loc (location_t loc
,
13712 enum tree_code code
, tree type
, tree op0
, tree op1
13716 #ifdef ENABLE_FOLD_CHECKING
13717 unsigned char checksum_before_op0
[16],
13718 checksum_before_op1
[16],
13719 checksum_after_op0
[16],
13720 checksum_after_op1
[16];
13721 struct md5_ctx ctx
;
13722 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13724 md5_init_ctx (&ctx
);
13725 fold_checksum_tree (op0
, &ctx
, &ht
);
13726 md5_finish_ctx (&ctx
, checksum_before_op0
);
13729 md5_init_ctx (&ctx
);
13730 fold_checksum_tree (op1
, &ctx
, &ht
);
13731 md5_finish_ctx (&ctx
, checksum_before_op1
);
13735 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13737 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
13739 #ifdef ENABLE_FOLD_CHECKING
13740 md5_init_ctx (&ctx
);
13741 fold_checksum_tree (op0
, &ctx
, &ht
);
13742 md5_finish_ctx (&ctx
, checksum_after_op0
);
13745 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13746 fold_check_failed (op0
, tem
);
13748 md5_init_ctx (&ctx
);
13749 fold_checksum_tree (op1
, &ctx
, &ht
);
13750 md5_finish_ctx (&ctx
, checksum_after_op1
);
13752 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13753 fold_check_failed (op1
, tem
);
13758 /* Fold a ternary tree expression with code CODE of type TYPE with
13759 operands OP0, OP1, and OP2. Return a folded expression if
13760 successful. Otherwise, return a tree expression with code CODE of
13761 type TYPE with operands OP0, OP1, and OP2. */
13764 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
13765 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
13768 #ifdef ENABLE_FOLD_CHECKING
13769 unsigned char checksum_before_op0
[16],
13770 checksum_before_op1
[16],
13771 checksum_before_op2
[16],
13772 checksum_after_op0
[16],
13773 checksum_after_op1
[16],
13774 checksum_after_op2
[16];
13775 struct md5_ctx ctx
;
13776 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13778 md5_init_ctx (&ctx
);
13779 fold_checksum_tree (op0
, &ctx
, &ht
);
13780 md5_finish_ctx (&ctx
, checksum_before_op0
);
13783 md5_init_ctx (&ctx
);
13784 fold_checksum_tree (op1
, &ctx
, &ht
);
13785 md5_finish_ctx (&ctx
, checksum_before_op1
);
13788 md5_init_ctx (&ctx
);
13789 fold_checksum_tree (op2
, &ctx
, &ht
);
13790 md5_finish_ctx (&ctx
, checksum_before_op2
);
13794 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
13795 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13797 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
13799 #ifdef ENABLE_FOLD_CHECKING
13800 md5_init_ctx (&ctx
);
13801 fold_checksum_tree (op0
, &ctx
, &ht
);
13802 md5_finish_ctx (&ctx
, checksum_after_op0
);
13805 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13806 fold_check_failed (op0
, tem
);
13808 md5_init_ctx (&ctx
);
13809 fold_checksum_tree (op1
, &ctx
, &ht
);
13810 md5_finish_ctx (&ctx
, checksum_after_op1
);
13813 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13814 fold_check_failed (op1
, tem
);
13816 md5_init_ctx (&ctx
);
13817 fold_checksum_tree (op2
, &ctx
, &ht
);
13818 md5_finish_ctx (&ctx
, checksum_after_op2
);
13820 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
13821 fold_check_failed (op2
, tem
);
13826 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13827 arguments in ARGARRAY, and a null static chain.
13828 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13829 of type TYPE from the given operands as constructed by build_call_array. */
13832 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
13833 int nargs
, tree
*argarray
)
13836 #ifdef ENABLE_FOLD_CHECKING
13837 unsigned char checksum_before_fn
[16],
13838 checksum_before_arglist
[16],
13839 checksum_after_fn
[16],
13840 checksum_after_arglist
[16];
13841 struct md5_ctx ctx
;
13842 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13845 md5_init_ctx (&ctx
);
13846 fold_checksum_tree (fn
, &ctx
, &ht
);
13847 md5_finish_ctx (&ctx
, checksum_before_fn
);
13850 md5_init_ctx (&ctx
);
13851 for (i
= 0; i
< nargs
; i
++)
13852 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13853 md5_finish_ctx (&ctx
, checksum_before_arglist
);
13857 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
13859 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13861 #ifdef ENABLE_FOLD_CHECKING
13862 md5_init_ctx (&ctx
);
13863 fold_checksum_tree (fn
, &ctx
, &ht
);
13864 md5_finish_ctx (&ctx
, checksum_after_fn
);
13867 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
13868 fold_check_failed (fn
, tem
);
13870 md5_init_ctx (&ctx
);
13871 for (i
= 0; i
< nargs
; i
++)
13872 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13873 md5_finish_ctx (&ctx
, checksum_after_arglist
);
13875 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
13876 fold_check_failed (NULL_TREE
, tem
);
13881 /* Perform constant folding and related simplification of initializer
13882 expression EXPR. These behave identically to "fold_buildN" but ignore
13883 potential run-time traps and exceptions that fold must preserve. */
13885 #define START_FOLD_INIT \
13886 int saved_signaling_nans = flag_signaling_nans;\
13887 int saved_trapping_math = flag_trapping_math;\
13888 int saved_rounding_math = flag_rounding_math;\
13889 int saved_trapv = flag_trapv;\
13890 int saved_folding_initializer = folding_initializer;\
13891 flag_signaling_nans = 0;\
13892 flag_trapping_math = 0;\
13893 flag_rounding_math = 0;\
13895 folding_initializer = 1;
13897 #define END_FOLD_INIT \
13898 flag_signaling_nans = saved_signaling_nans;\
13899 flag_trapping_math = saved_trapping_math;\
13900 flag_rounding_math = saved_rounding_math;\
13901 flag_trapv = saved_trapv;\
13902 folding_initializer = saved_folding_initializer;
13905 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
13906 tree type
, tree op
)
13911 result
= fold_build1_loc (loc
, code
, type
, op
);
13918 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
13919 tree type
, tree op0
, tree op1
)
13924 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
13931 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
13932 int nargs
, tree
*argarray
)
13937 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13943 #undef START_FOLD_INIT
13944 #undef END_FOLD_INIT
13946 /* Determine if first argument is a multiple of second argument. Return 0 if
13947 it is not, or we cannot easily determined it to be.
13949 An example of the sort of thing we care about (at this point; this routine
13950 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13951 fold cases do now) is discovering that
13953 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13959 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13961 This code also handles discovering that
13963 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13965 is a multiple of 8 so we don't have to worry about dealing with a
13966 possible remainder.
13968 Note that we *look* inside a SAVE_EXPR only to determine how it was
13969 calculated; it is not safe for fold to do much of anything else with the
13970 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13971 at run time. For example, the latter example above *cannot* be implemented
13972 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13973 evaluation time of the original SAVE_EXPR is not necessarily the same at
13974 the time the new expression is evaluated. The only optimization of this
13975 sort that would be valid is changing
13977 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13981 SAVE_EXPR (I) * SAVE_EXPR (J)
13983 (where the same SAVE_EXPR (J) is used in the original and the
13984 transformed version). */
13987 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
13992 if (operand_equal_p (top
, bottom
, 0))
13995 if (TREE_CODE (type
) != INTEGER_TYPE
)
13998 switch (TREE_CODE (top
))
14001 /* Bitwise and provides a power of two multiple. If the mask is
14002 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14003 if (!integer_pow2p (bottom
))
14005 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
14006 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
14009 if (TREE_CODE (bottom
) == INTEGER_CST
)
14011 op1
= TREE_OPERAND (top
, 0);
14012 op2
= TREE_OPERAND (top
, 1);
14013 if (TREE_CODE (op1
) == INTEGER_CST
)
14014 std::swap (op1
, op2
);
14015 if (TREE_CODE (op2
) == INTEGER_CST
)
14017 if (multiple_of_p (type
, op2
, bottom
))
14019 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14020 if (multiple_of_p (type
, bottom
, op2
))
14022 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14023 wi::to_widest (op2
));
14024 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14026 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14027 return multiple_of_p (type
, op1
, op2
);
14030 return multiple_of_p (type
, op1
, bottom
);
14033 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
14034 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
14037 /* It is impossible to prove if op0 - op1 is multiple of bottom
14038 precisely, so be conservative here checking if both op0 and op1
14039 are multiple of bottom. Note we check the second operand first
14040 since it's usually simpler. */
14041 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
14042 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
14045 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
14046 as op0 - 3 if the expression has unsigned type. For example,
14047 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
14048 op1
= TREE_OPERAND (top
, 1);
14049 if (TYPE_UNSIGNED (type
)
14050 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14051 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14052 return (multiple_of_p (type
, op1
, bottom
)
14053 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
14056 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14058 op1
= TREE_OPERAND (top
, 1);
14059 /* const_binop may not detect overflow correctly,
14060 so check for it explicitly here. */
14061 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
14063 && (t1
= fold_convert (type
,
14064 const_binop (LSHIFT_EXPR
, size_one_node
,
14066 && !TREE_OVERFLOW (t1
))
14067 return multiple_of_p (type
, t1
, bottom
);
14072 /* Can't handle conversions from non-integral or wider integral type. */
14073 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14074 || (TYPE_PRECISION (type
)
14075 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14081 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
14084 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
14085 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
14088 if (TREE_CODE (bottom
) != INTEGER_CST
14089 || integer_zerop (bottom
)
14090 || (TYPE_UNSIGNED (type
)
14091 && (tree_int_cst_sgn (top
) < 0
14092 || tree_int_cst_sgn (bottom
) < 0)))
14094 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14098 if (TREE_CODE (bottom
) == INTEGER_CST
14099 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14100 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14102 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14104 /* Check for special cases to see if top is defined as multiple
14107 top = (X & ~(bottom - 1) ; bottom is power of 2
14113 if (code
== BIT_AND_EXPR
14114 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14115 && TREE_CODE (op2
) == INTEGER_CST
14116 && integer_pow2p (bottom
)
14117 && wi::multiple_of_p (wi::to_widest (op2
),
14118 wi::to_widest (bottom
), UNSIGNED
))
14121 op1
= gimple_assign_rhs1 (stmt
);
14122 if (code
== MINUS_EXPR
14123 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14124 && TREE_CODE (op2
) == SSA_NAME
14125 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14126 && gimple_code (stmt
) == GIMPLE_ASSIGN
14127 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14128 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14129 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14136 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14137 return multiple_p (wi::to_poly_widest (top
),
14138 wi::to_poly_widest (bottom
));
14144 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14145 This function returns true for integer expressions, and returns
14146 false if uncertain. */
14149 tree_expr_finite_p (const_tree x
)
14151 machine_mode mode
= element_mode (x
);
14152 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14154 switch (TREE_CODE (x
))
14157 return real_isfinite (TREE_REAL_CST_PTR (x
));
14159 return tree_expr_finite_p (TREE_REALPART (x
))
14160 && tree_expr_finite_p (TREE_IMAGPART (x
));
14165 case NON_LVALUE_EXPR
:
14168 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14171 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14172 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14174 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14175 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14177 switch (get_call_combined_fn (x
))
14180 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14183 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14184 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14194 /* Return true if expression X evaluates to an infinity.
14195 This function returns false for integer expressions. */
14198 tree_expr_infinite_p (const_tree x
)
14200 if (!HONOR_INFINITIES (x
))
14202 switch (TREE_CODE (x
))
14205 return real_isinf (TREE_REAL_CST_PTR (x
));
14208 case NON_LVALUE_EXPR
:
14210 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14212 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14213 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14219 /* Return true if expression X could evaluate to an infinity.
14220 This function returns false for integer expressions, and returns
14221 true if uncertain. */
14224 tree_expr_maybe_infinite_p (const_tree x
)
14226 if (!HONOR_INFINITIES (x
))
14228 switch (TREE_CODE (x
))
14231 return real_isinf (TREE_REAL_CST_PTR (x
));
14236 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14238 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14239 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14245 /* Return true if expression X evaluates to a signaling NaN.
14246 This function returns false for integer expressions. */
14249 tree_expr_signaling_nan_p (const_tree x
)
14251 if (!HONOR_SNANS (x
))
14253 switch (TREE_CODE (x
))
14256 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14257 case NON_LVALUE_EXPR
:
14259 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14261 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14262 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14268 /* Return true if expression X could evaluate to a signaling NaN.
14269 This function returns false for integer expressions, and returns
14270 true if uncertain. */
14273 tree_expr_maybe_signaling_nan_p (const_tree x
)
14275 if (!HONOR_SNANS (x
))
14277 switch (TREE_CODE (x
))
14280 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14286 case NON_LVALUE_EXPR
:
14288 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14291 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14292 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14294 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14295 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14297 switch (get_call_combined_fn (x
))
14300 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14303 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14304 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14313 /* Return true if expression X evaluates to a NaN.
14314 This function returns false for integer expressions. */
14317 tree_expr_nan_p (const_tree x
)
14319 if (!HONOR_NANS (x
))
14321 switch (TREE_CODE (x
))
14324 return real_isnan (TREE_REAL_CST_PTR (x
));
14325 case NON_LVALUE_EXPR
:
14327 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14329 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14330 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14336 /* Return true if expression X could evaluate to a NaN.
14337 This function returns false for integer expressions, and returns
14338 true if uncertain. */
14341 tree_expr_maybe_nan_p (const_tree x
)
14343 if (!HONOR_NANS (x
))
14345 switch (TREE_CODE (x
))
14348 return real_isnan (TREE_REAL_CST_PTR (x
));
14354 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14355 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14359 case NON_LVALUE_EXPR
:
14361 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14364 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14365 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14367 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14368 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14370 switch (get_call_combined_fn (x
))
14373 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14376 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14377 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14386 #define tree_expr_nonnegative_warnv_p(X, Y) \
14387 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14389 #define RECURSE(X) \
14390 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14392 /* Return true if CODE or TYPE is known to be non-negative. */
14395 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14397 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14398 && truth_value_p (code
))
14399 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14400 have a signed:1 type (where the value is -1 and 0). */
14405 /* Return true if (CODE OP0) is known to be non-negative. If the return
14406 value is based on the assumption that signed overflow is undefined,
14407 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14408 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14411 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14412 bool *strict_overflow_p
, int depth
)
14414 if (TYPE_UNSIGNED (type
))
14420 /* We can't return 1 if flag_wrapv is set because
14421 ABS_EXPR<INT_MIN> = INT_MIN. */
14422 if (!ANY_INTEGRAL_TYPE_P (type
))
14424 if (TYPE_OVERFLOW_UNDEFINED (type
))
14426 *strict_overflow_p
= true;
14431 case NON_LVALUE_EXPR
:
14433 case FIX_TRUNC_EXPR
:
14434 return RECURSE (op0
);
14438 tree inner_type
= TREE_TYPE (op0
);
14439 tree outer_type
= type
;
14441 if (TREE_CODE (outer_type
) == REAL_TYPE
)
14443 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14444 return RECURSE (op0
);
14445 if (INTEGRAL_TYPE_P (inner_type
))
14447 if (TYPE_UNSIGNED (inner_type
))
14449 return RECURSE (op0
);
14452 else if (INTEGRAL_TYPE_P (outer_type
))
14454 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14455 return RECURSE (op0
);
14456 if (INTEGRAL_TYPE_P (inner_type
))
14457 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14458 && TYPE_UNSIGNED (inner_type
);
14464 return tree_simple_nonnegative_warnv_p (code
, type
);
14467 /* We don't know sign of `t', so be conservative and return false. */
14471 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14472 value is based on the assumption that signed overflow is undefined,
14473 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14474 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14477 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14478 tree op1
, bool *strict_overflow_p
,
14481 if (TYPE_UNSIGNED (type
))
14486 case POINTER_PLUS_EXPR
:
14488 if (FLOAT_TYPE_P (type
))
14489 return RECURSE (op0
) && RECURSE (op1
);
14491 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14492 both unsigned and at least 2 bits shorter than the result. */
14493 if (TREE_CODE (type
) == INTEGER_TYPE
14494 && TREE_CODE (op0
) == NOP_EXPR
14495 && TREE_CODE (op1
) == NOP_EXPR
)
14497 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14498 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14499 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14500 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14502 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14503 TYPE_PRECISION (inner2
)) + 1;
14504 return prec
< TYPE_PRECISION (type
);
14510 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14512 /* x * x is always non-negative for floating point x
14513 or without overflow. */
14514 if (operand_equal_p (op0
, op1
, 0)
14515 || (RECURSE (op0
) && RECURSE (op1
)))
14517 if (ANY_INTEGRAL_TYPE_P (type
)
14518 && TYPE_OVERFLOW_UNDEFINED (type
))
14519 *strict_overflow_p
= true;
14524 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14525 both unsigned and their total bits is shorter than the result. */
14526 if (TREE_CODE (type
) == INTEGER_TYPE
14527 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14528 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
14530 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
14531 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
14533 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
14534 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
14537 bool unsigned0
= TYPE_UNSIGNED (inner0
);
14538 bool unsigned1
= TYPE_UNSIGNED (inner1
);
14540 if (TREE_CODE (op0
) == INTEGER_CST
)
14541 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
14543 if (TREE_CODE (op1
) == INTEGER_CST
)
14544 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
14546 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
14547 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
14549 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
14550 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
14551 : TYPE_PRECISION (inner0
);
14553 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
14554 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
14555 : TYPE_PRECISION (inner1
);
14557 return precision0
+ precision1
< TYPE_PRECISION (type
);
14563 return RECURSE (op0
) || RECURSE (op1
);
14566 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14568 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
14569 return RECURSE (op0
) && RECURSE (op1
);
14570 return RECURSE (op0
) || RECURSE (op1
);
14576 case TRUNC_DIV_EXPR
:
14577 case CEIL_DIV_EXPR
:
14578 case FLOOR_DIV_EXPR
:
14579 case ROUND_DIV_EXPR
:
14580 return RECURSE (op0
) && RECURSE (op1
);
14582 case TRUNC_MOD_EXPR
:
14583 return RECURSE (op0
);
14585 case FLOOR_MOD_EXPR
:
14586 return RECURSE (op1
);
14588 case CEIL_MOD_EXPR
:
14589 case ROUND_MOD_EXPR
:
14591 return tree_simple_nonnegative_warnv_p (code
, type
);
14594 /* We don't know sign of `t', so be conservative and return false. */
14598 /* Return true if T is known to be non-negative. If the return
14599 value is based on the assumption that signed overflow is undefined,
14600 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14601 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14604 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14606 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14609 switch (TREE_CODE (t
))
14612 return tree_int_cst_sgn (t
) >= 0;
14615 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
14618 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
14621 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
14624 /* Limit the depth of recursion to avoid quadratic behavior.
14625 This is expected to catch almost all occurrences in practice.
14626 If this code misses important cases that unbounded recursion
14627 would not, passes that need this information could be revised
14628 to provide it through dataflow propagation. */
14629 return (!name_registered_for_update_p (t
)
14630 && depth
< param_max_ssa_name_query_depth
14631 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
14632 strict_overflow_p
, depth
));
14635 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
14639 /* Return true if T is known to be non-negative. If the return
14640 value is based on the assumption that signed overflow is undefined,
14641 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14642 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14645 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
14646 bool *strict_overflow_p
, int depth
)
14667 case CFN_BUILT_IN_BSWAP16
:
14668 case CFN_BUILT_IN_BSWAP32
:
14669 case CFN_BUILT_IN_BSWAP64
:
14670 case CFN_BUILT_IN_BSWAP128
:
14676 /* sqrt(-0.0) is -0.0. */
14677 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
14679 return RECURSE (arg0
);
14707 CASE_CFN_NEARBYINT
:
14708 CASE_CFN_NEARBYINT_FN
:
14713 CASE_CFN_ROUNDEVEN
:
14714 CASE_CFN_ROUNDEVEN_FN
:
14719 CASE_CFN_SIGNIFICAND
:
14724 /* True if the 1st argument is nonnegative. */
14725 return RECURSE (arg0
);
14729 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14730 things. In the presence of sNaNs, we're only guaranteed to be
14731 non-negative if both operands are non-negative. In the presence
14732 of qNaNs, we're non-negative if either operand is non-negative
14733 and can't be a qNaN, or if both operands are non-negative. */
14734 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
14735 tree_expr_maybe_signaling_nan_p (arg1
))
14736 return RECURSE (arg0
) && RECURSE (arg1
);
14737 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
14740 && !tree_expr_maybe_nan_p (arg1
));
14744 /* True if the 1st AND 2nd arguments are nonnegative. */
14745 return RECURSE (arg0
) && RECURSE (arg1
);
14748 CASE_CFN_COPYSIGN_FN
:
14749 /* True if the 2nd argument is nonnegative. */
14750 return RECURSE (arg1
);
14753 /* True if the 1st argument is nonnegative or the second
14754 argument is an even integer. */
14755 if (TREE_CODE (arg1
) == INTEGER_CST
14756 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
14758 return RECURSE (arg0
);
14761 /* True if the 1st argument is nonnegative or the second
14762 argument is an even integer valued real. */
14763 if (TREE_CODE (arg1
) == REAL_CST
)
14768 c
= TREE_REAL_CST (arg1
);
14769 n
= real_to_integer (&c
);
14772 REAL_VALUE_TYPE cint
;
14773 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
14774 if (real_identical (&c
, &cint
))
14778 return RECURSE (arg0
);
14783 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
14786 /* Return true if T is known to be non-negative. If the return
14787 value is based on the assumption that signed overflow is undefined,
14788 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14789 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14792 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14794 enum tree_code code
= TREE_CODE (t
);
14795 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14802 tree temp
= TARGET_EXPR_SLOT (t
);
14803 t
= TARGET_EXPR_INITIAL (t
);
14805 /* If the initializer is non-void, then it's a normal expression
14806 that will be assigned to the slot. */
14807 if (!VOID_TYPE_P (t
))
14808 return RECURSE (t
);
14810 /* Otherwise, the initializer sets the slot in some way. One common
14811 way is an assignment statement at the end of the initializer. */
14814 if (TREE_CODE (t
) == BIND_EXPR
)
14815 t
= expr_last (BIND_EXPR_BODY (t
));
14816 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
14817 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
14818 t
= expr_last (TREE_OPERAND (t
, 0));
14819 else if (TREE_CODE (t
) == STATEMENT_LIST
)
14824 if (TREE_CODE (t
) == MODIFY_EXPR
14825 && TREE_OPERAND (t
, 0) == temp
)
14826 return RECURSE (TREE_OPERAND (t
, 1));
14833 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
14834 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
14836 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
14837 get_call_combined_fn (t
),
14840 strict_overflow_p
, depth
);
14842 case COMPOUND_EXPR
:
14844 return RECURSE (TREE_OPERAND (t
, 1));
14847 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
14850 return RECURSE (TREE_OPERAND (t
, 0));
14853 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
14858 #undef tree_expr_nonnegative_warnv_p
14860 /* Return true if T is known to be non-negative. If the return
14861 value is based on the assumption that signed overflow is undefined,
14862 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14863 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14866 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14868 enum tree_code code
;
14869 if (t
== error_mark_node
)
14872 code
= TREE_CODE (t
);
14873 switch (TREE_CODE_CLASS (code
))
14876 case tcc_comparison
:
14877 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
14879 TREE_OPERAND (t
, 0),
14880 TREE_OPERAND (t
, 1),
14881 strict_overflow_p
, depth
);
14884 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
14886 TREE_OPERAND (t
, 0),
14887 strict_overflow_p
, depth
);
14890 case tcc_declaration
:
14891 case tcc_reference
:
14892 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
14900 case TRUTH_AND_EXPR
:
14901 case TRUTH_OR_EXPR
:
14902 case TRUTH_XOR_EXPR
:
14903 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
14905 TREE_OPERAND (t
, 0),
14906 TREE_OPERAND (t
, 1),
14907 strict_overflow_p
, depth
);
14908 case TRUTH_NOT_EXPR
:
14909 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
14911 TREE_OPERAND (t
, 0),
14912 strict_overflow_p
, depth
);
14919 case WITH_SIZE_EXPR
:
14921 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
14924 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
14928 /* Return true if `t' is known to be non-negative. Handle warnings
14929 about undefined signed overflow. */
14932 tree_expr_nonnegative_p (tree t
)
14934 bool ret
, strict_overflow_p
;
14936 strict_overflow_p
= false;
14937 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
14938 if (strict_overflow_p
)
14939 fold_overflow_warning (("assuming signed overflow does not occur when "
14940 "determining that expression is always "
14942 WARN_STRICT_OVERFLOW_MISC
);
14947 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14948 For floating point we further ensure that T is not denormal.
14949 Similar logic is present in nonzero_address in rtlanal.h.
14951 If the return value is based on the assumption that signed overflow
14952 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14953 change *STRICT_OVERFLOW_P. */
14956 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
14957 bool *strict_overflow_p
)
14962 return tree_expr_nonzero_warnv_p (op0
,
14963 strict_overflow_p
);
14967 tree inner_type
= TREE_TYPE (op0
);
14968 tree outer_type
= type
;
14970 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
14971 && tree_expr_nonzero_warnv_p (op0
,
14972 strict_overflow_p
));
14976 case NON_LVALUE_EXPR
:
14977 return tree_expr_nonzero_warnv_p (op0
,
14978 strict_overflow_p
);
14987 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14988 For floating point we further ensure that T is not denormal.
14989 Similar logic is present in nonzero_address in rtlanal.h.
14991 If the return value is based on the assumption that signed overflow
14992 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14993 change *STRICT_OVERFLOW_P. */
14996 tree_binary_nonzero_warnv_p (enum tree_code code
,
14999 tree op1
, bool *strict_overflow_p
)
15001 bool sub_strict_overflow_p
;
15004 case POINTER_PLUS_EXPR
:
15006 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15008 /* With the presence of negative values it is hard
15009 to say something. */
15010 sub_strict_overflow_p
= false;
15011 if (!tree_expr_nonnegative_warnv_p (op0
,
15012 &sub_strict_overflow_p
)
15013 || !tree_expr_nonnegative_warnv_p (op1
,
15014 &sub_strict_overflow_p
))
15016 /* One of operands must be positive and the other non-negative. */
15017 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15018 overflows, on a twos-complement machine the sum of two
15019 nonnegative numbers can never be zero. */
15020 return (tree_expr_nonzero_warnv_p (op0
,
15022 || tree_expr_nonzero_warnv_p (op1
,
15023 strict_overflow_p
));
15028 if (TYPE_OVERFLOW_UNDEFINED (type
))
15030 if (tree_expr_nonzero_warnv_p (op0
,
15032 && tree_expr_nonzero_warnv_p (op1
,
15033 strict_overflow_p
))
15035 *strict_overflow_p
= true;
15042 sub_strict_overflow_p
= false;
15043 if (tree_expr_nonzero_warnv_p (op0
,
15044 &sub_strict_overflow_p
)
15045 && tree_expr_nonzero_warnv_p (op1
,
15046 &sub_strict_overflow_p
))
15048 if (sub_strict_overflow_p
)
15049 *strict_overflow_p
= true;
15054 sub_strict_overflow_p
= false;
15055 if (tree_expr_nonzero_warnv_p (op0
,
15056 &sub_strict_overflow_p
))
15058 if (sub_strict_overflow_p
)
15059 *strict_overflow_p
= true;
15061 /* When both operands are nonzero, then MAX must be too. */
15062 if (tree_expr_nonzero_warnv_p (op1
,
15063 strict_overflow_p
))
15066 /* MAX where operand 0 is positive is positive. */
15067 return tree_expr_nonnegative_warnv_p (op0
,
15068 strict_overflow_p
);
15070 /* MAX where operand 1 is positive is positive. */
15071 else if (tree_expr_nonzero_warnv_p (op1
,
15072 &sub_strict_overflow_p
)
15073 && tree_expr_nonnegative_warnv_p (op1
,
15074 &sub_strict_overflow_p
))
15076 if (sub_strict_overflow_p
)
15077 *strict_overflow_p
= true;
15083 return (tree_expr_nonzero_warnv_p (op1
,
15085 || tree_expr_nonzero_warnv_p (op0
,
15086 strict_overflow_p
));
15095 /* Return true when T is an address and is known to be nonzero.
15096 For floating point we further ensure that T is not denormal.
15097 Similar logic is present in nonzero_address in rtlanal.h.
15099 If the return value is based on the assumption that signed overflow
15100 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15101 change *STRICT_OVERFLOW_P. */
15104 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15106 bool sub_strict_overflow_p
;
15107 switch (TREE_CODE (t
))
15110 return !integer_zerop (t
);
15114 tree base
= TREE_OPERAND (t
, 0);
15116 if (!DECL_P (base
))
15117 base
= get_base_address (base
);
15119 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15120 base
= TARGET_EXPR_SLOT (base
);
15125 /* For objects in symbol table check if we know they are non-zero.
15126 Don't do anything for variables and functions before symtab is built;
15127 it is quite possible that they will be declared weak later. */
15128 int nonzero_addr
= maybe_nonzero_address (base
);
15129 if (nonzero_addr
>= 0)
15130 return nonzero_addr
;
15132 /* Constants are never weak. */
15133 if (CONSTANT_CLASS_P (base
))
15140 sub_strict_overflow_p
= false;
15141 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15142 &sub_strict_overflow_p
)
15143 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15144 &sub_strict_overflow_p
))
15146 if (sub_strict_overflow_p
)
15147 *strict_overflow_p
= true;
15153 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15155 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15163 #define integer_valued_real_p(X) \
15164 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15166 #define RECURSE(X) \
15167 ((integer_valued_real_p) (X, depth + 1))
15169 /* Return true if the floating point result of (CODE OP0) has an
15170 integer value. We also allow +Inf, -Inf and NaN to be considered
15171 integer values. Return false for signaling NaN.
15173 DEPTH is the current nesting depth of the query. */
15176 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15184 return RECURSE (op0
);
15188 tree type
= TREE_TYPE (op0
);
15189 if (TREE_CODE (type
) == INTEGER_TYPE
)
15191 if (TREE_CODE (type
) == REAL_TYPE
)
15192 return RECURSE (op0
);
15202 /* Return true if the floating point result of (CODE OP0 OP1) has an
15203 integer value. We also allow +Inf, -Inf and NaN to be considered
15204 integer values. Return false for signaling NaN.
15206 DEPTH is the current nesting depth of the query. */
15209 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15218 return RECURSE (op0
) && RECURSE (op1
);
15226 /* Return true if the floating point result of calling FNDECL with arguments
15227 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15228 considered integer values. Return false for signaling NaN. If FNDECL
15229 takes fewer than 2 arguments, the remaining ARGn are null.
15231 DEPTH is the current nesting depth of the query. */
15234 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15242 CASE_CFN_NEARBYINT
:
15243 CASE_CFN_NEARBYINT_FN
:
15248 CASE_CFN_ROUNDEVEN
:
15249 CASE_CFN_ROUNDEVEN_FN
:
15258 return RECURSE (arg0
) && RECURSE (arg1
);
15266 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15267 has an integer value. We also allow +Inf, -Inf and NaN to be
15268 considered integer values. Return false for signaling NaN.
15270 DEPTH is the current nesting depth of the query. */
15273 integer_valued_real_single_p (tree t
, int depth
)
15275 switch (TREE_CODE (t
))
15278 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15281 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15284 /* Limit the depth of recursion to avoid quadratic behavior.
15285 This is expected to catch almost all occurrences in practice.
15286 If this code misses important cases that unbounded recursion
15287 would not, passes that need this information could be revised
15288 to provide it through dataflow propagation. */
15289 return (!name_registered_for_update_p (t
)
15290 && depth
< param_max_ssa_name_query_depth
15291 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15300 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15301 has an integer value. We also allow +Inf, -Inf and NaN to be
15302 considered integer values. Return false for signaling NaN.
15304 DEPTH is the current nesting depth of the query. */
15307 integer_valued_real_invalid_p (tree t
, int depth
)
15309 switch (TREE_CODE (t
))
15311 case COMPOUND_EXPR
:
15314 return RECURSE (TREE_OPERAND (t
, 1));
15317 return RECURSE (TREE_OPERAND (t
, 0));
15326 #undef integer_valued_real_p
15328 /* Return true if the floating point expression T has an integer value.
15329 We also allow +Inf, -Inf and NaN to be considered integer values.
15330 Return false for signaling NaN.
15332 DEPTH is the current nesting depth of the query. */
15335 integer_valued_real_p (tree t
, int depth
)
15337 if (t
== error_mark_node
)
15340 STRIP_ANY_LOCATION_WRAPPER (t
);
15342 tree_code code
= TREE_CODE (t
);
15343 switch (TREE_CODE_CLASS (code
))
15346 case tcc_comparison
:
15347 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15348 TREE_OPERAND (t
, 1), depth
);
15351 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15354 case tcc_declaration
:
15355 case tcc_reference
:
15356 return integer_valued_real_single_p (t
, depth
);
15366 return integer_valued_real_single_p (t
, depth
);
15370 tree arg0
= (call_expr_nargs (t
) > 0
15371 ? CALL_EXPR_ARG (t
, 0)
15373 tree arg1
= (call_expr_nargs (t
) > 1
15374 ? CALL_EXPR_ARG (t
, 1)
15376 return integer_valued_real_call_p (get_call_combined_fn (t
),
15377 arg0
, arg1
, depth
);
15381 return integer_valued_real_invalid_p (t
, depth
);
15385 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15386 attempt to fold the expression to a constant without modifying TYPE,
15389 If the expression could be simplified to a constant, then return
15390 the constant. If the expression would not be simplified to a
15391 constant, then return NULL_TREE. */
15394 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15396 tree tem
= fold_binary (code
, type
, op0
, op1
);
15397 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15400 /* Given the components of a unary expression CODE, TYPE and OP0,
15401 attempt to fold the expression to a constant without modifying
15404 If the expression could be simplified to a constant, then return
15405 the constant. If the expression would not be simplified to a
15406 constant, then return NULL_TREE. */
15409 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15411 tree tem
= fold_unary (code
, type
, op0
);
15412 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15415 /* If EXP represents referencing an element in a constant string
15416 (either via pointer arithmetic or array indexing), return the
15417 tree representing the value accessed, otherwise return NULL. */
15420 fold_read_from_constant_string (tree exp
)
15422 if ((TREE_CODE (exp
) == INDIRECT_REF
15423 || TREE_CODE (exp
) == ARRAY_REF
)
15424 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15426 tree exp1
= TREE_OPERAND (exp
, 0);
15429 location_t loc
= EXPR_LOCATION (exp
);
15431 if (TREE_CODE (exp
) == INDIRECT_REF
)
15432 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15435 tree low_bound
= array_ref_low_bound (exp
);
15436 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15438 /* Optimize the special-case of a zero lower bound.
15440 We convert the low_bound to sizetype to avoid some problems
15441 with constant folding. (E.g. suppose the lower bound is 1,
15442 and its mode is QI. Without the conversion,l (ARRAY
15443 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15444 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15445 if (! integer_zerop (low_bound
))
15446 index
= size_diffop_loc (loc
, index
,
15447 fold_convert_loc (loc
, sizetype
, low_bound
));
15452 scalar_int_mode char_mode
;
15454 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15455 && TREE_CODE (string
) == STRING_CST
15456 && tree_fits_uhwi_p (index
)
15457 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15458 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15460 && GET_MODE_SIZE (char_mode
) == 1)
15461 return build_int_cst_type (TREE_TYPE (exp
),
15462 (TREE_STRING_POINTER (string
)
15463 [TREE_INT_CST_LOW (index
)]));
15468 /* Folds a read from vector element at IDX of vector ARG. */
15471 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15473 unsigned HOST_WIDE_INT i
;
15474 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15475 && known_ge (idx
, 0u)
15476 && idx
.is_constant (&i
))
15478 if (TREE_CODE (arg
) == VECTOR_CST
)
15479 return VECTOR_CST_ELT (arg
, i
);
15480 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15482 if (i
>= CONSTRUCTOR_NELTS (arg
))
15483 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
15484 return CONSTRUCTOR_ELT (arg
, i
)->value
;
15490 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15491 an integer constant, real, or fixed-point constant.
15493 TYPE is the type of the result. */
15496 fold_negate_const (tree arg0
, tree type
)
15498 tree t
= NULL_TREE
;
15500 switch (TREE_CODE (arg0
))
15503 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15508 FIXED_VALUE_TYPE f
;
15509 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
15510 &(TREE_FIXED_CST (arg0
)), NULL
,
15511 TYPE_SATURATING (type
));
15512 t
= build_fixed (type
, f
);
15513 /* Propagate overflow flags. */
15514 if (overflow_p
| TREE_OVERFLOW (arg0
))
15515 TREE_OVERFLOW (t
) = 1;
15520 if (poly_int_tree_p (arg0
))
15522 wi::overflow_type overflow
;
15523 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
15524 t
= force_fit_type (type
, res
, 1,
15525 (overflow
&& ! TYPE_UNSIGNED (type
))
15526 || TREE_OVERFLOW (arg0
));
15530 gcc_unreachable ();
15536 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15537 an integer constant or real constant.
15539 TYPE is the type of the result. */
15542 fold_abs_const (tree arg0
, tree type
)
15544 tree t
= NULL_TREE
;
15546 switch (TREE_CODE (arg0
))
15550 /* If the value is unsigned or non-negative, then the absolute value
15551 is the same as the ordinary value. */
15552 wide_int val
= wi::to_wide (arg0
);
15553 wi::overflow_type overflow
= wi::OVF_NONE
;
15554 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
15557 /* If the value is negative, then the absolute value is
15560 val
= wi::neg (val
, &overflow
);
15562 /* Force to the destination type, set TREE_OVERFLOW for signed
15564 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
15569 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
15570 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15576 gcc_unreachable ();
15582 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15583 constant. TYPE is the type of the result. */
15586 fold_not_const (const_tree arg0
, tree type
)
15588 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
15590 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
15593 /* Given CODE, a relational operator, the target type, TYPE and two
15594 constant operands OP0 and OP1, return the result of the
15595 relational operation. If the result is not a compile time
15596 constant, then return NULL_TREE. */
15599 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
15601 int result
, invert
;
15603 /* From here on, the only cases we handle are when the result is
15604 known to be a constant. */
15606 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
15608 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
15609 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
15611 /* Handle the cases where either operand is a NaN. */
15612 if (real_isnan (c0
) || real_isnan (c1
))
15622 case UNORDERED_EXPR
:
15636 if (flag_trapping_math
)
15642 gcc_unreachable ();
15645 return constant_boolean_node (result
, type
);
15648 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
15651 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
15653 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
15654 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
15655 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
15658 /* Handle equality/inequality of complex constants. */
15659 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
15661 tree rcond
= fold_relational_const (code
, type
,
15662 TREE_REALPART (op0
),
15663 TREE_REALPART (op1
));
15664 tree icond
= fold_relational_const (code
, type
,
15665 TREE_IMAGPART (op0
),
15666 TREE_IMAGPART (op1
));
15667 if (code
== EQ_EXPR
)
15668 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
15669 else if (code
== NE_EXPR
)
15670 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
15675 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
15677 if (!VECTOR_TYPE_P (type
))
15679 /* Have vector comparison with scalar boolean result. */
15680 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
15681 && known_eq (VECTOR_CST_NELTS (op0
),
15682 VECTOR_CST_NELTS (op1
)));
15683 unsigned HOST_WIDE_INT nunits
;
15684 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
15686 for (unsigned i
= 0; i
< nunits
; i
++)
15688 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15689 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15690 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
15691 if (tmp
== NULL_TREE
)
15693 if (integer_zerop (tmp
))
15694 return constant_boolean_node (code
== NE_EXPR
, type
);
15696 return constant_boolean_node (code
== EQ_EXPR
, type
);
15698 tree_vector_builder elts
;
15699 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
15701 unsigned int count
= elts
.encoded_nelts ();
15702 for (unsigned i
= 0; i
< count
; i
++)
15704 tree elem_type
= TREE_TYPE (type
);
15705 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15706 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15708 tree tem
= fold_relational_const (code
, elem_type
,
15711 if (tem
== NULL_TREE
)
15714 elts
.quick_push (build_int_cst (elem_type
,
15715 integer_zerop (tem
) ? 0 : -1));
15718 return elts
.build ();
15721 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15723 To compute GT, swap the arguments and do LT.
15724 To compute GE, do LT and invert the result.
15725 To compute LE, swap the arguments, do LT and invert the result.
15726 To compute NE, do EQ and invert the result.
15728 Therefore, the code below must handle only EQ and LT. */
15730 if (code
== LE_EXPR
|| code
== GT_EXPR
)
15732 std::swap (op0
, op1
);
15733 code
= swap_tree_comparison (code
);
15736 /* Note that it is safe to invert for real values here because we
15737 have already handled the one case that it matters. */
15740 if (code
== NE_EXPR
|| code
== GE_EXPR
)
15743 code
= invert_tree_comparison (code
, false);
15746 /* Compute a result for LT or EQ if args permit;
15747 Otherwise return T. */
15748 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
15750 if (code
== EQ_EXPR
)
15751 result
= tree_int_cst_equal (op0
, op1
);
15753 result
= tree_int_cst_lt (op0
, op1
);
15760 return constant_boolean_node (result
, type
);
15763 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15764 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15768 fold_build_cleanup_point_expr (tree type
, tree expr
)
15770 /* If the expression does not have side effects then we don't have to wrap
15771 it with a cleanup point expression. */
15772 if (!TREE_SIDE_EFFECTS (expr
))
15775 /* If the expression is a return, check to see if the expression inside the
15776 return has no side effects or the right hand side of the modify expression
15777 inside the return. If either don't have side effects set we don't need to
15778 wrap the expression in a cleanup point expression. Note we don't check the
15779 left hand side of the modify because it should always be a return decl. */
15780 if (TREE_CODE (expr
) == RETURN_EXPR
)
15782 tree op
= TREE_OPERAND (expr
, 0);
15783 if (!op
|| !TREE_SIDE_EFFECTS (op
))
15785 op
= TREE_OPERAND (op
, 1);
15786 if (!TREE_SIDE_EFFECTS (op
))
15790 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
15793 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15794 of an indirection through OP0, or NULL_TREE if no simplification is
15798 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
15802 poly_uint64 const_op01
;
15805 subtype
= TREE_TYPE (sub
);
15806 if (!POINTER_TYPE_P (subtype
)
15807 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
15810 if (TREE_CODE (sub
) == ADDR_EXPR
)
15812 tree op
= TREE_OPERAND (sub
, 0);
15813 tree optype
= TREE_TYPE (op
);
15815 /* *&CONST_DECL -> to the value of the const decl. */
15816 if (TREE_CODE (op
) == CONST_DECL
)
15817 return DECL_INITIAL (op
);
15818 /* *&p => p; make sure to handle *&"str"[cst] here. */
15819 if (type
== optype
)
15821 tree fop
= fold_read_from_constant_string (op
);
15827 /* *(foo *)&fooarray => fooarray[0] */
15828 else if (TREE_CODE (optype
) == ARRAY_TYPE
15829 && type
== TREE_TYPE (optype
)
15830 && (!in_gimple_form
15831 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
15833 tree type_domain
= TYPE_DOMAIN (optype
);
15834 tree min_val
= size_zero_node
;
15835 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15836 min_val
= TYPE_MIN_VALUE (type_domain
);
15838 && TREE_CODE (min_val
) != INTEGER_CST
)
15840 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
15841 NULL_TREE
, NULL_TREE
);
15843 /* *(foo *)&complexfoo => __real__ complexfoo */
15844 else if (TREE_CODE (optype
) == COMPLEX_TYPE
15845 && type
== TREE_TYPE (optype
))
15846 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
15847 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15848 else if (VECTOR_TYPE_P (optype
)
15849 && type
== TREE_TYPE (optype
))
15851 tree part_width
= TYPE_SIZE (type
);
15852 tree index
= bitsize_int (0);
15853 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
15858 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
15859 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
15861 tree op00
= TREE_OPERAND (sub
, 0);
15862 tree op01
= TREE_OPERAND (sub
, 1);
15865 if (TREE_CODE (op00
) == ADDR_EXPR
)
15868 op00
= TREE_OPERAND (op00
, 0);
15869 op00type
= TREE_TYPE (op00
);
15871 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15872 if (VECTOR_TYPE_P (op00type
)
15873 && type
== TREE_TYPE (op00type
)
15874 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15875 but we want to treat offsets with MSB set as negative.
15876 For the code below negative offsets are invalid and
15877 TYPE_SIZE of the element is something unsigned, so
15878 check whether op01 fits into poly_int64, which implies
15879 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15880 then just use poly_uint64 because we want to treat the
15881 value as unsigned. */
15882 && tree_fits_poly_int64_p (op01
))
15884 tree part_width
= TYPE_SIZE (type
);
15885 poly_uint64 max_offset
15886 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
15887 * TYPE_VECTOR_SUBPARTS (op00type
));
15888 if (known_lt (const_op01
, max_offset
))
15890 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
15891 return fold_build3_loc (loc
,
15892 BIT_FIELD_REF
, type
, op00
,
15893 part_width
, index
);
15896 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15897 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
15898 && type
== TREE_TYPE (op00type
))
15900 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
15902 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
15904 /* ((foo *)&fooarray)[1] => fooarray[1] */
15905 else if (TREE_CODE (op00type
) == ARRAY_TYPE
15906 && type
== TREE_TYPE (op00type
))
15908 tree type_domain
= TYPE_DOMAIN (op00type
);
15909 tree min_val
= size_zero_node
;
15910 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15911 min_val
= TYPE_MIN_VALUE (type_domain
);
15912 poly_uint64 type_size
, index
;
15913 if (poly_int_tree_p (min_val
)
15914 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
15915 && multiple_p (const_op01
, type_size
, &index
))
15917 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
15918 op01
= wide_int_to_tree (sizetype
, off
);
15919 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
15920 NULL_TREE
, NULL_TREE
);
15926 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15927 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
15928 && type
== TREE_TYPE (TREE_TYPE (subtype
))
15929 && (!in_gimple_form
15930 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
15933 tree min_val
= size_zero_node
;
15934 sub
= build_fold_indirect_ref_loc (loc
, sub
);
15935 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
15936 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15937 min_val
= TYPE_MIN_VALUE (type_domain
);
15939 && TREE_CODE (min_val
) != INTEGER_CST
)
15941 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
15948 /* Builds an expression for an indirection through T, simplifying some
15952 build_fold_indirect_ref_loc (location_t loc
, tree t
)
15954 tree type
= TREE_TYPE (TREE_TYPE (t
));
15955 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
15960 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
15963 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15966 fold_indirect_ref_loc (location_t loc
, tree t
)
15968 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
15976 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15977 whose result is ignored. The type of the returned tree need not be
15978 the same as the original expression. */
15981 fold_ignored_result (tree t
)
15983 if (!TREE_SIDE_EFFECTS (t
))
15984 return integer_zero_node
;
15987 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
15990 t
= TREE_OPERAND (t
, 0);
15994 case tcc_comparison
:
15995 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
15996 t
= TREE_OPERAND (t
, 0);
15997 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
15998 t
= TREE_OPERAND (t
, 1);
16003 case tcc_expression
:
16004 switch (TREE_CODE (t
))
16006 case COMPOUND_EXPR
:
16007 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16009 t
= TREE_OPERAND (t
, 0);
16013 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16014 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16016 t
= TREE_OPERAND (t
, 0);
16029 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16032 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16034 tree div
= NULL_TREE
;
16039 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16040 have to do anything. Only do this when we are not given a const,
16041 because in that case, this check is more expensive than just
16043 if (TREE_CODE (value
) != INTEGER_CST
)
16045 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16047 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16051 /* If divisor is a power of two, simplify this to bit manipulation. */
16052 if (pow2_or_zerop (divisor
))
16054 if (TREE_CODE (value
) == INTEGER_CST
)
16056 wide_int val
= wi::to_wide (value
);
16059 if ((val
& (divisor
- 1)) == 0)
16062 overflow_p
= TREE_OVERFLOW (value
);
16063 val
+= divisor
- 1;
16064 val
&= (int) -divisor
;
16068 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16074 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16075 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16076 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16077 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16083 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16084 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16085 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16091 /* Likewise, but round down. */
16094 round_down_loc (location_t loc
, tree value
, int divisor
)
16096 tree div
= NULL_TREE
;
16098 gcc_assert (divisor
> 0);
16102 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16103 have to do anything. Only do this when we are not given a const,
16104 because in that case, this check is more expensive than just
16106 if (TREE_CODE (value
) != INTEGER_CST
)
16108 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16110 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16114 /* If divisor is a power of two, simplify this to bit manipulation. */
16115 if (pow2_or_zerop (divisor
))
16119 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16120 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16125 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16126 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16127 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16133 /* Returns the pointer to the base of the object addressed by EXP and
16134 extracts the information about the offset of the access, storing it
16135 to PBITPOS and POFFSET. */
16138 split_address_to_core_and_offset (tree exp
,
16139 poly_int64_pod
*pbitpos
, tree
*poffset
)
16143 int unsignedp
, reversep
, volatilep
;
16144 poly_int64 bitsize
;
16145 location_t loc
= EXPR_LOCATION (exp
);
16147 if (TREE_CODE (exp
) == ADDR_EXPR
)
16149 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16150 poffset
, &mode
, &unsignedp
, &reversep
,
16152 core
= build_fold_addr_expr_loc (loc
, core
);
16154 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16156 core
= TREE_OPERAND (exp
, 0);
16159 *poffset
= TREE_OPERAND (exp
, 1);
16160 if (poly_int_tree_p (*poffset
))
16162 poly_offset_int tem
16163 = wi::sext (wi::to_poly_offset (*poffset
),
16164 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16165 tem
<<= LOG2_BITS_PER_UNIT
;
16166 if (tem
.to_shwi (pbitpos
))
16167 *poffset
= NULL_TREE
;
16174 *poffset
= NULL_TREE
;
16180 /* Returns true if addresses of E1 and E2 differ by a constant, false
16181 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16184 ptr_difference_const (tree e1
, tree e2
, poly_int64_pod
*diff
)
16187 poly_int64 bitpos1
, bitpos2
;
16188 tree toffset1
, toffset2
, tdiff
, type
;
16190 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16191 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16193 poly_int64 bytepos1
, bytepos2
;
16194 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16195 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16196 || !operand_equal_p (core1
, core2
, 0))
16199 if (toffset1
&& toffset2
)
16201 type
= TREE_TYPE (toffset1
);
16202 if (type
!= TREE_TYPE (toffset2
))
16203 toffset2
= fold_convert (type
, toffset2
);
16205 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16206 if (!cst_and_fits_in_hwi (tdiff
))
16209 *diff
= int_cst_value (tdiff
);
16211 else if (toffset1
|| toffset2
)
16213 /* If only one of the offsets is non-constant, the difference cannot
16220 *diff
+= bytepos1
- bytepos2
;
16224 /* Return OFF converted to a pointer offset type suitable as offset for
16225 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16227 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16229 if (ptrofftype_p (TREE_TYPE (off
)))
16231 return fold_convert_loc (loc
, sizetype
, off
);
16234 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16236 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16238 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16239 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16242 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16244 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16246 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16247 ptr
, size_int (off
));
16250 /* Return a pointer to a NUL-terminated string containing the sequence
16251 of bytes corresponding to the representation of the object referred to
16252 by SRC (or a subsequence of such bytes within it if SRC is a reference
16253 to an initialized constant array plus some constant offset).
16254 Set *STRSIZE the number of bytes in the constant sequence including
16255 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16256 where A is the array that stores the constant sequence that SRC points
16257 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16258 need not point to a string or even an array of characters but may point
16259 to an object of any type. */
16262 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16264 /* The offset into the array A storing the string, and A's byte size. */
16272 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16274 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16278 unsigned HOST_WIDE_INT offset
= 0;
16279 if (offset_node
!= NULL_TREE
)
16281 if (!tree_fits_uhwi_p (offset_node
))
16284 offset
= tree_to_uhwi (offset_node
);
16287 if (!tree_fits_uhwi_p (mem_size
))
16290 /* ARRAY_SIZE is the byte size of the array the constant sequence
16291 is stored in and equal to sizeof A. INIT_BYTES is the number
16292 of bytes in the constant sequence used to initialize the array,
16293 including any embedded NULs as well as the terminating NUL (for
16294 strings), but not including any trailing zeros/NULs past
16295 the terminating one appended implicitly to a string literal to
16296 zero out the remainder of the array it's stored in. For example,
16298 const char a[7] = "abc\0d";
16299 n = strlen (a + 1);
16300 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16301 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16302 is equal to strlen (A) + 1. */
16303 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16304 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16305 const char *string
= TREE_STRING_POINTER (src
);
16307 /* Ideally this would turn into a gcc_checking_assert over time. */
16308 if (init_bytes
> array_size
)
16309 init_bytes
= array_size
;
16311 if (init_bytes
== 0 || offset
>= array_size
)
16316 /* Compute and store the number of characters from the beginning
16317 of the substring at OFFSET to the end, including the terminating
16318 nul. Offsets past the initial length refer to null strings. */
16319 if (offset
< init_bytes
)
16320 *strsize
= init_bytes
- offset
;
16326 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16327 /* Support only properly NUL-terminated single byte strings. */
16328 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16330 if (string
[init_bytes
- 1] != '\0')
16334 return offset
< init_bytes
? string
+ offset
: "";
16337 /* Return a pointer to a NUL-terminated string corresponding to
16338 the expression STR referencing a constant string, possibly
16339 involving a constant offset. Return null if STR either doesn't
16340 reference a constant string or if it involves a nonconstant
16344 c_getstr (tree str
)
16346 return getbyterep (str
, NULL
);
16349 /* Given a tree T, compute which bits in T may be nonzero. */
16352 tree_nonzero_bits (const_tree t
)
16354 switch (TREE_CODE (t
))
16357 return wi::to_wide (t
);
16359 return get_nonzero_bits (t
);
16360 case NON_LVALUE_EXPR
:
16362 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16364 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16365 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16368 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16369 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16371 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16372 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16374 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16375 TYPE_PRECISION (TREE_TYPE (t
)),
16376 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16378 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16380 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16381 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16382 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16383 return wi::bit_or (nzbits1
, nzbits2
);
16387 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16389 tree type
= TREE_TYPE (t
);
16390 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16391 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16392 TYPE_PRECISION (type
));
16393 return wi::neg_p (arg1
)
16394 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16395 : wi::lshift (nzbits
, arg1
);
16399 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16401 tree type
= TREE_TYPE (t
);
16402 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16403 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16404 TYPE_PRECISION (type
));
16405 return wi::neg_p (arg1
)
16406 ? wi::lshift (nzbits
, -arg1
)
16407 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16414 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16419 namespace selftest
{
16421 /* Helper functions for writing tests of folding trees. */
16423 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16426 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
16429 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
16432 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16433 wrapping WRAPPED_EXPR. */
16436 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
16439 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
16440 ASSERT_NE (wrapped_expr
, result
);
16441 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
16442 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
16445 /* Verify that various arithmetic binary operations are folded
16449 test_arithmetic_folding ()
16451 tree type
= integer_type_node
;
16452 tree x
= create_tmp_var_raw (type
, "x");
16453 tree zero
= build_zero_cst (type
);
16454 tree one
= build_int_cst (type
, 1);
16457 /* 1 <-- (0 + 1) */
16458 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
16460 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
16463 /* (nonlvalue)x <-- (x + 0) */
16464 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
16468 /* 0 <-- (x - x) */
16469 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
16471 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
16474 /* Multiplication. */
16475 /* 0 <-- (x * 0) */
16476 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
16479 /* (nonlvalue)x <-- (x * 1) */
16480 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
16484 /* Verify that various binary operations on vectors are folded
16488 test_vector_folding ()
16490 tree inner_type
= integer_type_node
;
16491 tree type
= build_vector_type (inner_type
, 4);
16492 tree zero
= build_zero_cst (type
);
16493 tree one
= build_one_cst (type
);
16494 tree index
= build_index_vector (type
, 0, 1);
16496 /* Verify equality tests that return a scalar boolean result. */
16497 tree res_type
= boolean_type_node
;
16498 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
16499 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
16500 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
16501 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
16502 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
16503 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16505 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
16507 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16511 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16514 test_vec_duplicate_folding ()
16516 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
16517 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
16518 /* This will be 1 if VEC_MODE isn't a vector mode. */
16519 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
16521 tree type
= build_vector_type (ssizetype
, nunits
);
16522 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
16523 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
16524 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
16527 /* Run all of the selftests within this file. */
16530 fold_const_c_tests ()
16532 test_arithmetic_folding ();
16533 test_vector_folding ();
16534 test_vec_duplicate_folding ();
16537 } // namespace selftest
16539 #endif /* CHECKING_P */