1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2018 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"
75 #include "tree-into-ssa.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
80 #include "tree-ssanames.h"
82 #include "stringpool.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 /* 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 int 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 int 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
)
343 return !flag_rounding_math
;
351 /* Check whether we may negate an integer constant T without causing
355 may_negate_without_overflow_p (const_tree t
)
359 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
361 type
= TREE_TYPE (t
);
362 if (TYPE_UNSIGNED (type
))
365 return !wi::only_sign_bit_p (wi::to_wide (t
));
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
372 negate_expr_p (tree t
)
379 type
= TREE_TYPE (t
);
382 switch (TREE_CODE (t
))
385 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t
);
391 return (INTEGRAL_TYPE_P (type
)
392 && TYPE_OVERFLOW_WRAPS (type
));
398 return !TYPE_OVERFLOW_SANITIZED (type
);
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
406 return negate_expr_p (TREE_REALPART (t
))
407 && negate_expr_p (TREE_IMAGPART (t
));
411 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
414 /* Steps don't prevent negation. */
415 unsigned int count
= vector_cst_encoded_nelts (t
);
416 for (unsigned int i
= 0; i
< count
; ++i
)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
424 return negate_expr_p (TREE_OPERAND (t
, 0))
425 && negate_expr_p (TREE_OPERAND (t
, 1));
428 return negate_expr_p (TREE_OPERAND (t
, 0));
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
432 || HONOR_SIGNED_ZEROS (element_mode (type
))
433 || (ANY_INTEGRAL_TYPE_P (type
)
434 && ! TYPE_OVERFLOW_WRAPS (type
)))
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t
, 1)))
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t
, 0));
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
445 && !HONOR_SIGNED_ZEROS (element_mode (type
))
446 && (! ANY_INTEGRAL_TYPE_P (type
)
447 || TYPE_OVERFLOW_WRAPS (type
));
450 if (TYPE_UNSIGNED (type
))
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
456 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
458 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
461 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
468 return negate_expr_p (TREE_OPERAND (t
, 1))
469 || negate_expr_p (TREE_OPERAND (t
, 0));
475 if (TYPE_UNSIGNED (type
))
477 /* In general we can't negate A in A / B, because if A is INT_MIN and
478 B is not 1 we change the sign of the result. */
479 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
480 && negate_expr_p (TREE_OPERAND (t
, 0)))
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
487 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t
, 1))))
489 return negate_expr_p (TREE_OPERAND (t
, 1));
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type
) == REAL_TYPE
)
496 tree tem
= strip_float_extensions (t
);
498 return negate_expr_p (tem
);
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t
)))
505 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
512 tree op1
= TREE_OPERAND (t
, 1);
513 if (wi::to_wide (op1
) == TYPE_PRECISION (type
) - 1)
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
530 fold_negate_expr_1 (location_t loc
, tree t
)
532 tree type
= TREE_TYPE (t
);
535 switch (TREE_CODE (t
))
537 /* Convert - (~A) to A + 1. */
539 if (INTEGRAL_TYPE_P (type
))
540 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
541 build_one_cst (type
));
545 tem
= fold_negate_const (t
, type
);
546 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
547 || (ANY_INTEGRAL_TYPE_P (type
)
548 && !TYPE_OVERFLOW_TRAPS (type
)
549 && TYPE_OVERFLOW_WRAPS (type
))
550 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
557 tem
= fold_negate_const (t
, type
);
562 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
563 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
565 return build_complex (type
, rpart
, ipart
);
571 tree_vector_builder elts
;
572 elts
.new_unary_operation (type
, t
, true);
573 unsigned int count
= elts
.encoded_nelts ();
574 for (unsigned int i
= 0; i
< count
; ++i
)
576 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
577 if (elt
== NULL_TREE
)
579 elts
.quick_push (elt
);
582 return elts
.build ();
586 if (negate_expr_p (t
))
587 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
588 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
589 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
593 if (negate_expr_p (t
))
594 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
595 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
599 if (!TYPE_OVERFLOW_SANITIZED (type
))
600 return TREE_OPERAND (t
, 0);
604 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
605 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
607 /* -(A + B) -> (-B) - A. */
608 if (negate_expr_p (TREE_OPERAND (t
, 1)))
610 tem
= negate_expr (TREE_OPERAND (t
, 1));
611 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
612 tem
, TREE_OPERAND (t
, 0));
615 /* -(A + B) -> (-A) - B. */
616 if (negate_expr_p (TREE_OPERAND (t
, 0)))
618 tem
= negate_expr (TREE_OPERAND (t
, 0));
619 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
620 tem
, TREE_OPERAND (t
, 1));
626 /* - (A - B) -> B - A */
627 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
628 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
629 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
630 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
634 if (TYPE_UNSIGNED (type
))
640 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
642 tem
= TREE_OPERAND (t
, 1);
643 if (negate_expr_p (tem
))
644 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
645 TREE_OPERAND (t
, 0), negate_expr (tem
));
646 tem
= TREE_OPERAND (t
, 0);
647 if (negate_expr_p (tem
))
648 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
649 negate_expr (tem
), TREE_OPERAND (t
, 1));
656 if (TYPE_UNSIGNED (type
))
658 /* In general we can't negate A in A / B, because if A is INT_MIN and
659 B is not 1 we change the sign of the result. */
660 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
661 && negate_expr_p (TREE_OPERAND (t
, 0)))
662 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
663 negate_expr (TREE_OPERAND (t
, 0)),
664 TREE_OPERAND (t
, 1));
665 /* In general we can't negate B in A / B, because if A is INT_MIN and
666 B is 1, we may turn this into INT_MIN / -1 which is undefined
667 and actually traps on some architectures. */
668 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
670 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
671 && ! integer_onep (TREE_OPERAND (t
, 1))))
672 && negate_expr_p (TREE_OPERAND (t
, 1)))
673 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
675 negate_expr (TREE_OPERAND (t
, 1)));
679 /* Convert -((double)float) into (double)(-float). */
680 if (TREE_CODE (type
) == REAL_TYPE
)
682 tem
= strip_float_extensions (t
);
683 if (tem
!= t
&& negate_expr_p (tem
))
684 return fold_convert_loc (loc
, type
, negate_expr (tem
));
689 /* Negate -f(x) as f(-x). */
690 if (negate_mathfn_p (get_call_combined_fn (t
))
691 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
695 fndecl
= get_callee_fndecl (t
);
696 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
697 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
702 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
703 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
705 tree op1
= TREE_OPERAND (t
, 1);
706 if (wi::to_wide (op1
) == TYPE_PRECISION (type
) - 1)
708 tree ntype
= TYPE_UNSIGNED (type
)
709 ? signed_type_for (type
)
710 : unsigned_type_for (type
);
711 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
712 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
713 return fold_convert_loc (loc
, type
, temp
);
725 /* A wrapper for fold_negate_expr_1. */
728 fold_negate_expr (location_t loc
, tree t
)
730 tree type
= TREE_TYPE (t
);
732 tree tem
= fold_negate_expr_1 (loc
, t
);
733 if (tem
== NULL_TREE
)
735 return fold_convert_loc (loc
, type
, tem
);
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
751 loc
= EXPR_LOCATION (t
);
752 type
= TREE_TYPE (t
);
755 tem
= fold_negate_expr (loc
, t
);
757 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
758 return fold_convert_loc (loc
, type
, tem
);
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead. If a variable part is of pointer
775 type, it is negated after converting to TYPE. This prevents us from
776 generating illegal MINUS pointer expression. LOC is the location of
777 the converted variable part.
779 If IN is itself a literal or constant, return it as appropriate.
781 Note that we do not guarantee that any of the three values will be the
782 same type as IN, but they will have the same signedness and mode. */
785 split_tree (tree in
, tree type
, enum tree_code code
,
786 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
787 tree
*litp
, tree
*minus_litp
, int negate_p
)
796 /* Strip any conversions that don't change the machine mode or signedness. */
797 STRIP_SIGN_NOPS (in
);
799 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
800 || TREE_CODE (in
) == FIXED_CST
)
802 else if (TREE_CODE (in
) == code
803 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
804 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
805 /* We can associate addition and subtraction together (even
806 though the C standard doesn't say so) for integers because
807 the value is not affected. For reals, the value might be
808 affected, so we can't. */
809 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
810 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
811 || (code
== MINUS_EXPR
812 && (TREE_CODE (in
) == PLUS_EXPR
813 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
815 tree op0
= TREE_OPERAND (in
, 0);
816 tree op1
= TREE_OPERAND (in
, 1);
817 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
818 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
822 || TREE_CODE (op0
) == FIXED_CST
)
823 *litp
= op0
, op0
= 0;
824 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
825 || TREE_CODE (op1
) == FIXED_CST
)
826 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
828 if (op0
!= 0 && TREE_CONSTANT (op0
))
829 *conp
= op0
, op0
= 0;
830 else if (op1
!= 0 && TREE_CONSTANT (op1
))
831 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0
!= 0 && op1
!= 0)
840 var
= op1
, neg_var_p
= neg1_p
;
842 /* Now do any needed negations. */
844 *minus_litp
= *litp
, *litp
= 0;
845 if (neg_conp_p
&& *conp
)
846 *minus_conp
= *conp
, *conp
= 0;
847 if (neg_var_p
&& var
)
848 *minus_varp
= var
, var
= 0;
850 else if (TREE_CONSTANT (in
))
852 else if (TREE_CODE (in
) == BIT_NOT_EXPR
853 && code
== PLUS_EXPR
)
855 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *litp
= build_minus_one_cst (type
);
858 *minus_varp
= TREE_OPERAND (in
, 0);
866 *minus_litp
= *litp
, *litp
= 0;
867 else if (*minus_litp
)
868 *litp
= *minus_litp
, *minus_litp
= 0;
870 *minus_conp
= *conp
, *conp
= 0;
871 else if (*minus_conp
)
872 *conp
= *minus_conp
, *minus_conp
= 0;
874 *minus_varp
= var
, var
= 0;
875 else if (*minus_varp
)
876 var
= *minus_varp
, *minus_varp
= 0;
880 && TREE_OVERFLOW_P (*litp
))
881 *litp
= drop_tree_overflow (*litp
);
883 && TREE_OVERFLOW_P (*minus_litp
))
884 *minus_litp
= drop_tree_overflow (*minus_litp
);
889 /* Re-associate trees split by the above function. T1 and T2 are
890 either expressions to associate or null. Return the new
891 expression, if any. LOC is the location of the new expression. If
892 we build an operation, do it in TYPE and with CODE. */
895 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
899 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
905 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
906 try to fold this since we will have infinite recursion. But do
907 deal with any NEGATE_EXPRs. */
908 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
909 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
910 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
912 if (code
== PLUS_EXPR
)
914 if (TREE_CODE (t1
) == NEGATE_EXPR
)
915 return build2_loc (loc
, MINUS_EXPR
, type
,
916 fold_convert_loc (loc
, type
, t2
),
917 fold_convert_loc (loc
, type
,
918 TREE_OPERAND (t1
, 0)));
919 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
920 return build2_loc (loc
, MINUS_EXPR
, type
,
921 fold_convert_loc (loc
, type
, t1
),
922 fold_convert_loc (loc
, type
,
923 TREE_OPERAND (t2
, 0)));
924 else if (integer_zerop (t2
))
925 return fold_convert_loc (loc
, type
, t1
);
927 else if (code
== MINUS_EXPR
)
929 if (integer_zerop (t2
))
930 return fold_convert_loc (loc
, type
, t1
);
933 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
934 fold_convert_loc (loc
, type
, t2
));
937 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
938 fold_convert_loc (loc
, type
, t2
));
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
945 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
947 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
949 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
964 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
965 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
966 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
969 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
972 int_const_binop_2 (enum tree_code code
, const_tree parg1
, const_tree parg2
,
977 tree type
= TREE_TYPE (parg1
);
978 signop sign
= TYPE_SIGN (type
);
979 bool overflow
= false;
981 wi::tree_to_wide_ref arg1
= wi::to_wide (parg1
);
982 wide_int arg2
= wi::to_wide (parg2
, TYPE_PRECISION (type
));
987 res
= wi::bit_or (arg1
, arg2
);
991 res
= wi::bit_xor (arg1
, arg2
);
995 res
= wi::bit_and (arg1
, arg2
);
1000 if (wi::neg_p (arg2
))
1003 if (code
== RSHIFT_EXPR
)
1009 if (code
== RSHIFT_EXPR
)
1010 /* It's unclear from the C standard whether shifts can overflow.
1011 The following code ignores overflow; perhaps a C standard
1012 interpretation ruling is needed. */
1013 res
= wi::rshift (arg1
, arg2
, sign
);
1015 res
= wi::lshift (arg1
, arg2
);
1020 if (wi::neg_p (arg2
))
1023 if (code
== RROTATE_EXPR
)
1024 code
= LROTATE_EXPR
;
1026 code
= RROTATE_EXPR
;
1029 if (code
== RROTATE_EXPR
)
1030 res
= wi::rrotate (arg1
, arg2
);
1032 res
= wi::lrotate (arg1
, arg2
);
1036 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1040 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1044 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1047 case MULT_HIGHPART_EXPR
:
1048 res
= wi::mul_high (arg1
, arg2
, sign
);
1051 case TRUNC_DIV_EXPR
:
1052 case EXACT_DIV_EXPR
:
1055 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1058 case FLOOR_DIV_EXPR
:
1061 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1067 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1070 case ROUND_DIV_EXPR
:
1073 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1076 case TRUNC_MOD_EXPR
:
1079 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1082 case FLOOR_MOD_EXPR
:
1085 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1091 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1094 case ROUND_MOD_EXPR
:
1097 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1101 res
= wi::min (arg1
, arg2
, sign
);
1105 res
= wi::max (arg1
, arg2
, sign
);
1112 t
= force_fit_type (type
, res
, overflowable
,
1113 (((sign
== SIGNED
|| overflowable
== -1)
1115 | TREE_OVERFLOW (parg1
) | TREE_OVERFLOW (parg2
)));
1120 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1121 to produce a new constant. Return NULL_TREE if we don't know how
1122 to evaluate CODE at compile-time. */
1125 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1128 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1129 return int_const_binop_2 (code
, arg1
, arg2
, overflowable
);
1131 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1133 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1137 tree type
= TREE_TYPE (arg1
);
1138 signop sign
= TYPE_SIGN (type
);
1142 res
= wi::add (wi::to_poly_wide (arg1
),
1143 wi::to_poly_wide (arg2
), sign
, &overflow
);
1147 res
= wi::sub (wi::to_poly_wide (arg1
),
1148 wi::to_poly_wide (arg2
), sign
, &overflow
);
1152 if (TREE_CODE (arg2
) == INTEGER_CST
)
1153 res
= wi::mul (wi::to_poly_wide (arg1
),
1154 wi::to_wide (arg2
), sign
, &overflow
);
1155 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1156 res
= wi::mul (wi::to_poly_wide (arg2
),
1157 wi::to_wide (arg1
), sign
, &overflow
);
1163 if (TREE_CODE (arg2
) == INTEGER_CST
)
1164 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1170 if (TREE_CODE (arg2
) != INTEGER_CST
1171 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1179 return force_fit_type (type
, res
, overflowable
,
1180 (((sign
== SIGNED
|| overflowable
== -1)
1182 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1189 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1191 return int_const_binop_1 (code
, arg1
, arg2
, 1);
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 (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1616 offset_int res
= wi::sub (wi::to_offset (arg1
),
1617 wi::to_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
:
1626 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1628 if (TREE_CODE (arg1
) != VECTOR_CST
1629 || TREE_CODE (arg2
) != VECTOR_CST
)
1632 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1635 out_nelts
= in_nelts
* 2;
1636 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1637 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1639 tree_vector_builder
elts (type
, out_nelts
, 1);
1640 for (i
= 0; i
< out_nelts
; i
++)
1642 tree elt
= (i
< in_nelts
1643 ? VECTOR_CST_ELT (arg1
, i
)
1644 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1645 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1646 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1647 TREE_TYPE (type
), elt
);
1648 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1650 elts
.quick_push (elt
);
1653 return elts
.build ();
1656 case VEC_WIDEN_MULT_LO_EXPR
:
1657 case VEC_WIDEN_MULT_HI_EXPR
:
1658 case VEC_WIDEN_MULT_EVEN_EXPR
:
1659 case VEC_WIDEN_MULT_ODD_EXPR
:
1661 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1663 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1666 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1668 out_nelts
= in_nelts
/ 2;
1669 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1670 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1672 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1673 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1674 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1675 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1676 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1678 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1681 tree_vector_builder
elts (type
, out_nelts
, 1);
1682 for (out
= 0; out
< out_nelts
; out
++)
1684 unsigned int in
= (out
<< scale
) + ofs
;
1685 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1686 VECTOR_CST_ELT (arg1
, in
));
1687 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1688 VECTOR_CST_ELT (arg2
, in
));
1690 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1692 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1693 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1695 elts
.quick_push (elt
);
1698 return elts
.build ();
1704 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1707 /* Make sure type and arg0 have the same saturating flag. */
1708 gcc_checking_assert (TYPE_SATURATING (type
)
1709 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1711 return const_binop (code
, arg1
, arg2
);
1714 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1715 Return zero if computing the constants is not possible. */
1718 const_unop (enum tree_code code
, tree type
, tree arg0
)
1720 /* Don't perform the operation, other than NEGATE and ABS, if
1721 flag_signaling_nans is on and the operand is a signaling NaN. */
1722 if (TREE_CODE (arg0
) == REAL_CST
1723 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1724 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1725 && code
!= NEGATE_EXPR
1726 && code
!= ABS_EXPR
)
1733 case FIX_TRUNC_EXPR
:
1734 case FIXED_CONVERT_EXPR
:
1735 return fold_convert_const (code
, type
, arg0
);
1737 case ADDR_SPACE_CONVERT_EXPR
:
1738 /* If the source address is 0, and the source address space
1739 cannot have a valid object at 0, fold to dest type null. */
1740 if (integer_zerop (arg0
)
1741 && !(targetm
.addr_space
.zero_address_valid
1742 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1743 return fold_convert_const (code
, type
, arg0
);
1746 case VIEW_CONVERT_EXPR
:
1747 return fold_view_convert_expr (type
, arg0
);
1751 /* Can't call fold_negate_const directly here as that doesn't
1752 handle all cases and we might not be able to negate some
1754 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1755 if (tem
&& CONSTANT_CLASS_P (tem
))
1761 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1762 return fold_abs_const (arg0
, type
);
1766 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1768 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1770 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1775 if (TREE_CODE (arg0
) == INTEGER_CST
)
1776 return fold_not_const (arg0
, type
);
1777 else if (POLY_INT_CST_P (arg0
))
1778 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1779 /* Perform BIT_NOT_EXPR on each element individually. */
1780 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1784 /* This can cope with stepped encodings because ~x == -1 - x. */
1785 tree_vector_builder elements
;
1786 elements
.new_unary_operation (type
, arg0
, true);
1787 unsigned int i
, count
= elements
.encoded_nelts ();
1788 for (i
= 0; i
< count
; ++i
)
1790 elem
= VECTOR_CST_ELT (arg0
, i
);
1791 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1792 if (elem
== NULL_TREE
)
1794 elements
.quick_push (elem
);
1797 return elements
.build ();
1801 case TRUTH_NOT_EXPR
:
1802 if (TREE_CODE (arg0
) == INTEGER_CST
)
1803 return constant_boolean_node (integer_zerop (arg0
), type
);
1807 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1808 return fold_convert (type
, TREE_REALPART (arg0
));
1812 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1813 return fold_convert (type
, TREE_IMAGPART (arg0
));
1816 case VEC_UNPACK_LO_EXPR
:
1817 case VEC_UNPACK_HI_EXPR
:
1818 case VEC_UNPACK_FLOAT_LO_EXPR
:
1819 case VEC_UNPACK_FLOAT_HI_EXPR
:
1821 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1822 enum tree_code subcode
;
1824 if (TREE_CODE (arg0
) != VECTOR_CST
)
1827 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1829 out_nelts
= in_nelts
/ 2;
1830 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1832 unsigned int offset
= 0;
1833 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1834 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1837 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1840 subcode
= FLOAT_EXPR
;
1842 tree_vector_builder
elts (type
, out_nelts
, 1);
1843 for (i
= 0; i
< out_nelts
; i
++)
1845 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1846 VECTOR_CST_ELT (arg0
, i
+ offset
));
1847 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1849 elts
.quick_push (elt
);
1852 return elts
.build ();
1855 case VEC_DUPLICATE_EXPR
:
1856 if (CONSTANT_CLASS_P (arg0
))
1857 return build_vector_from_val (type
, arg0
);
1867 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1868 indicates which particular sizetype to create. */
1871 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
1873 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1876 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1877 is a tree code. The type of the result is taken from the operands.
1878 Both must be equivalent integer types, ala int_binop_types_match_p.
1879 If the operands are constant, so is the result. */
1882 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1884 tree type
= TREE_TYPE (arg0
);
1886 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1887 return error_mark_node
;
1889 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1892 /* Handle the special case of two poly_int constants faster. */
1893 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
1895 /* And some specific cases even faster than that. */
1896 if (code
== PLUS_EXPR
)
1898 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1900 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1903 else if (code
== MINUS_EXPR
)
1905 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1908 else if (code
== MULT_EXPR
)
1910 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1914 /* Handle general case of two integer constants. For sizetype
1915 constant calculations we always want to know about overflow,
1916 even in the unsigned case. */
1917 tree res
= int_const_binop_1 (code
, arg0
, arg1
, -1);
1918 if (res
!= NULL_TREE
)
1922 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1925 /* Given two values, either both of sizetype or both of bitsizetype,
1926 compute the difference between the two values. Return the value
1927 in signed type corresponding to the type of the operands. */
1930 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1932 tree type
= TREE_TYPE (arg0
);
1935 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1938 /* If the type is already signed, just do the simple thing. */
1939 if (!TYPE_UNSIGNED (type
))
1940 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1942 if (type
== sizetype
)
1944 else if (type
== bitsizetype
)
1945 ctype
= sbitsizetype
;
1947 ctype
= signed_type_for (type
);
1949 /* If either operand is not a constant, do the conversions to the signed
1950 type and subtract. The hardware will do the right thing with any
1951 overflow in the subtraction. */
1952 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1953 return size_binop_loc (loc
, MINUS_EXPR
,
1954 fold_convert_loc (loc
, ctype
, arg0
),
1955 fold_convert_loc (loc
, ctype
, arg1
));
1957 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1958 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1959 overflow) and negate (which can't either). Special-case a result
1960 of zero while we're here. */
1961 if (tree_int_cst_equal (arg0
, arg1
))
1962 return build_int_cst (ctype
, 0);
1963 else if (tree_int_cst_lt (arg1
, arg0
))
1964 return fold_convert_loc (loc
, ctype
,
1965 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1967 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1968 fold_convert_loc (loc
, ctype
,
1969 size_binop_loc (loc
,
1974 /* A subroutine of fold_convert_const handling conversions of an
1975 INTEGER_CST to another integer type. */
1978 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1980 /* Given an integer constant, make new constant with new type,
1981 appropriately sign-extended or truncated. Use widest_int
1982 so that any extension is done according ARG1's type. */
1983 return force_fit_type (type
, wi::to_widest (arg1
),
1984 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1985 TREE_OVERFLOW (arg1
));
1988 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1989 to an integer type. */
1992 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1994 bool overflow
= false;
1997 /* The following code implements the floating point to integer
1998 conversion rules required by the Java Language Specification,
1999 that IEEE NaNs are mapped to zero and values that overflow
2000 the target precision saturate, i.e. values greater than
2001 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2002 are mapped to INT_MIN. These semantics are allowed by the
2003 C and C++ standards that simply state that the behavior of
2004 FP-to-integer conversion is unspecified upon overflow. */
2008 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2012 case FIX_TRUNC_EXPR
:
2013 real_trunc (&r
, VOIDmode
, &x
);
2020 /* If R is NaN, return zero and show we have an overflow. */
2021 if (REAL_VALUE_ISNAN (r
))
2024 val
= wi::zero (TYPE_PRECISION (type
));
2027 /* See if R is less than the lower bound or greater than the
2032 tree lt
= TYPE_MIN_VALUE (type
);
2033 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2034 if (real_less (&r
, &l
))
2037 val
= wi::to_wide (lt
);
2043 tree ut
= TYPE_MAX_VALUE (type
);
2046 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2047 if (real_less (&u
, &r
))
2050 val
= wi::to_wide (ut
);
2056 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2058 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2062 /* A subroutine of fold_convert_const handling conversions of a
2063 FIXED_CST to an integer type. */
2066 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2069 double_int temp
, temp_trunc
;
2072 /* Right shift FIXED_CST to temp by fbit. */
2073 temp
= TREE_FIXED_CST (arg1
).data
;
2074 mode
= TREE_FIXED_CST (arg1
).mode
;
2075 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2077 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2078 HOST_BITS_PER_DOUBLE_INT
,
2079 SIGNED_FIXED_POINT_MODE_P (mode
));
2081 /* Left shift temp to temp_trunc by fbit. */
2082 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2083 HOST_BITS_PER_DOUBLE_INT
,
2084 SIGNED_FIXED_POINT_MODE_P (mode
));
2088 temp
= double_int_zero
;
2089 temp_trunc
= double_int_zero
;
2092 /* If FIXED_CST is negative, we need to round the value toward 0.
2093 By checking if the fractional bits are not zero to add 1 to temp. */
2094 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2095 && temp_trunc
.is_negative ()
2096 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2097 temp
+= double_int_one
;
2099 /* Given a fixed-point constant, make new constant with new type,
2100 appropriately sign-extended or truncated. */
2101 t
= force_fit_type (type
, temp
, -1,
2102 (temp
.is_negative ()
2103 && (TYPE_UNSIGNED (type
)
2104 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2105 | TREE_OVERFLOW (arg1
));
2110 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2111 to another floating point type. */
2114 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2116 REAL_VALUE_TYPE value
;
2119 /* Don't perform the operation if flag_signaling_nans is on
2120 and the operand is a signaling NaN. */
2121 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
2122 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2125 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2126 t
= build_real (type
, value
);
2128 /* If converting an infinity or NAN to a representation that doesn't
2129 have one, set the overflow bit so that we can produce some kind of
2130 error message at the appropriate point if necessary. It's not the
2131 most user-friendly message, but it's better than nothing. */
2132 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2133 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2134 TREE_OVERFLOW (t
) = 1;
2135 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2136 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2137 TREE_OVERFLOW (t
) = 1;
2138 /* Regular overflow, conversion produced an infinity in a mode that
2139 can't represent them. */
2140 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2141 && REAL_VALUE_ISINF (value
)
2142 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2143 TREE_OVERFLOW (t
) = 1;
2145 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2149 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2150 to a floating point type. */
2153 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2155 REAL_VALUE_TYPE value
;
2158 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2159 &TREE_FIXED_CST (arg1
));
2160 t
= build_real (type
, value
);
2162 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2166 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2167 to another fixed-point type. */
2170 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2172 FIXED_VALUE_TYPE value
;
2176 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2177 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2178 t
= build_fixed (type
, value
);
2180 /* Propagate overflow flags. */
2181 if (overflow_p
| TREE_OVERFLOW (arg1
))
2182 TREE_OVERFLOW (t
) = 1;
2186 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2187 to a fixed-point type. */
2190 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2192 FIXED_VALUE_TYPE value
;
2197 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2199 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2200 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2201 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2203 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2205 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2206 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2207 TYPE_SATURATING (type
));
2208 t
= build_fixed (type
, value
);
2210 /* Propagate overflow flags. */
2211 if (overflow_p
| TREE_OVERFLOW (arg1
))
2212 TREE_OVERFLOW (t
) = 1;
2216 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2217 to a fixed-point type. */
2220 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2222 FIXED_VALUE_TYPE value
;
2226 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2227 &TREE_REAL_CST (arg1
),
2228 TYPE_SATURATING (type
));
2229 t
= build_fixed (type
, value
);
2231 /* Propagate overflow flags. */
2232 if (overflow_p
| TREE_OVERFLOW (arg1
))
2233 TREE_OVERFLOW (t
) = 1;
2237 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2238 type TYPE. If no simplification can be done return NULL_TREE. */
2241 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2243 tree arg_type
= TREE_TYPE (arg1
);
2244 if (arg_type
== type
)
2247 /* We can't widen types, since the runtime value could overflow the
2248 original type before being extended to the new type. */
2249 if (POLY_INT_CST_P (arg1
)
2250 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2251 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2252 return build_poly_int_cst (type
,
2253 poly_wide_int::from (poly_int_cst_value (arg1
),
2254 TYPE_PRECISION (type
),
2255 TYPE_SIGN (arg_type
)));
2257 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2258 || TREE_CODE (type
) == OFFSET_TYPE
)
2260 if (TREE_CODE (arg1
) == INTEGER_CST
)
2261 return fold_convert_const_int_from_int (type
, arg1
);
2262 else if (TREE_CODE (arg1
) == REAL_CST
)
2263 return fold_convert_const_int_from_real (code
, type
, arg1
);
2264 else if (TREE_CODE (arg1
) == FIXED_CST
)
2265 return fold_convert_const_int_from_fixed (type
, arg1
);
2267 else if (TREE_CODE (type
) == REAL_TYPE
)
2269 if (TREE_CODE (arg1
) == INTEGER_CST
)
2270 return build_real_from_int_cst (type
, arg1
);
2271 else if (TREE_CODE (arg1
) == REAL_CST
)
2272 return fold_convert_const_real_from_real (type
, arg1
);
2273 else if (TREE_CODE (arg1
) == FIXED_CST
)
2274 return fold_convert_const_real_from_fixed (type
, arg1
);
2276 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2278 if (TREE_CODE (arg1
) == FIXED_CST
)
2279 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2280 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2281 return fold_convert_const_fixed_from_int (type
, arg1
);
2282 else if (TREE_CODE (arg1
) == REAL_CST
)
2283 return fold_convert_const_fixed_from_real (type
, arg1
);
2285 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2287 if (TREE_CODE (arg1
) == VECTOR_CST
2288 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2290 tree elttype
= TREE_TYPE (type
);
2291 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2292 /* We can't handle steps directly when extending, since the
2293 values need to wrap at the original precision first. */
2295 = (INTEGRAL_TYPE_P (elttype
)
2296 && INTEGRAL_TYPE_P (arg1_elttype
)
2297 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2298 tree_vector_builder v
;
2299 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2301 unsigned int len
= v
.encoded_nelts ();
2302 for (unsigned int i
= 0; i
< len
; ++i
)
2304 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2305 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2306 if (cvt
== NULL_TREE
)
2316 /* Construct a vector of zero elements of vector type TYPE. */
2319 build_zero_vector (tree type
)
2323 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2324 return build_vector_from_val (type
, t
);
2327 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2330 fold_convertible_p (const_tree type
, const_tree arg
)
2332 tree orig
= TREE_TYPE (arg
);
2337 if (TREE_CODE (arg
) == ERROR_MARK
2338 || TREE_CODE (type
) == ERROR_MARK
2339 || TREE_CODE (orig
) == ERROR_MARK
)
2342 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2345 switch (TREE_CODE (type
))
2347 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2348 case POINTER_TYPE
: case REFERENCE_TYPE
:
2350 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2351 || TREE_CODE (orig
) == OFFSET_TYPE
);
2354 case FIXED_POINT_TYPE
:
2357 return TREE_CODE (type
) == TREE_CODE (orig
);
2364 /* Convert expression ARG to type TYPE. Used by the middle-end for
2365 simple conversions in preference to calling the front-end's convert. */
2368 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2370 tree orig
= TREE_TYPE (arg
);
2376 if (TREE_CODE (arg
) == ERROR_MARK
2377 || TREE_CODE (type
) == ERROR_MARK
2378 || TREE_CODE (orig
) == ERROR_MARK
)
2379 return error_mark_node
;
2381 switch (TREE_CODE (type
))
2384 case REFERENCE_TYPE
:
2385 /* Handle conversions between pointers to different address spaces. */
2386 if (POINTER_TYPE_P (orig
)
2387 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2388 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2389 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2392 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2394 if (TREE_CODE (arg
) == INTEGER_CST
)
2396 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2397 if (tem
!= NULL_TREE
)
2400 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2401 || TREE_CODE (orig
) == OFFSET_TYPE
)
2402 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2403 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2404 return fold_convert_loc (loc
, type
,
2405 fold_build1_loc (loc
, REALPART_EXPR
,
2406 TREE_TYPE (orig
), arg
));
2407 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2408 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2409 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2412 if (TREE_CODE (arg
) == INTEGER_CST
)
2414 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2415 if (tem
!= NULL_TREE
)
2418 else if (TREE_CODE (arg
) == REAL_CST
)
2420 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2421 if (tem
!= NULL_TREE
)
2424 else if (TREE_CODE (arg
) == FIXED_CST
)
2426 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2427 if (tem
!= NULL_TREE
)
2431 switch (TREE_CODE (orig
))
2434 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2435 case POINTER_TYPE
: case REFERENCE_TYPE
:
2436 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2439 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2441 case FIXED_POINT_TYPE
:
2442 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2445 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2446 return fold_convert_loc (loc
, type
, tem
);
2452 case FIXED_POINT_TYPE
:
2453 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2454 || TREE_CODE (arg
) == REAL_CST
)
2456 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2457 if (tem
!= NULL_TREE
)
2458 goto fold_convert_exit
;
2461 switch (TREE_CODE (orig
))
2463 case FIXED_POINT_TYPE
:
2468 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2471 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2472 return fold_convert_loc (loc
, type
, tem
);
2479 switch (TREE_CODE (orig
))
2482 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2483 case POINTER_TYPE
: case REFERENCE_TYPE
:
2485 case FIXED_POINT_TYPE
:
2486 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2487 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2488 fold_convert_loc (loc
, TREE_TYPE (type
),
2489 integer_zero_node
));
2494 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2496 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2497 TREE_OPERAND (arg
, 0));
2498 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2499 TREE_OPERAND (arg
, 1));
2500 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2503 arg
= save_expr (arg
);
2504 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2505 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2506 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2507 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2508 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2516 if (integer_zerop (arg
))
2517 return build_zero_vector (type
);
2518 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2519 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2520 || TREE_CODE (orig
) == VECTOR_TYPE
);
2521 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2524 tem
= fold_ignored_result (arg
);
2525 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2528 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2529 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2533 protected_set_expr_location_unshare (tem
, loc
);
2537 /* Return false if expr can be assumed not to be an lvalue, true
2541 maybe_lvalue_p (const_tree x
)
2543 /* We only need to wrap lvalue tree codes. */
2544 switch (TREE_CODE (x
))
2557 case ARRAY_RANGE_REF
:
2563 case PREINCREMENT_EXPR
:
2564 case PREDECREMENT_EXPR
:
2566 case TRY_CATCH_EXPR
:
2567 case WITH_CLEANUP_EXPR
:
2576 /* Assume the worst for front-end tree codes. */
2577 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2585 /* Return an expr equal to X but certainly not valid as an lvalue. */
2588 non_lvalue_loc (location_t loc
, tree x
)
2590 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2595 if (! maybe_lvalue_p (x
))
2597 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2600 /* When pedantic, return an expr equal to X but certainly not valid as a
2601 pedantic lvalue. Otherwise, return X. */
2604 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2606 return protected_set_expr_location_unshare (x
, loc
);
2609 /* Given a tree comparison code, return the code that is the logical inverse.
2610 It is generally not safe to do this for floating-point comparisons, except
2611 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2612 ERROR_MARK in this case. */
2615 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2617 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2618 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2628 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2630 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2632 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2634 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2648 return UNORDERED_EXPR
;
2649 case UNORDERED_EXPR
:
2650 return ORDERED_EXPR
;
2656 /* Similar, but return the comparison that results if the operands are
2657 swapped. This is safe for floating-point. */
2660 swap_tree_comparison (enum tree_code code
)
2667 case UNORDERED_EXPR
:
2693 /* Convert a comparison tree code from an enum tree_code representation
2694 into a compcode bit-based encoding. This function is the inverse of
2695 compcode_to_comparison. */
2697 static enum comparison_code
2698 comparison_to_compcode (enum tree_code code
)
2715 return COMPCODE_ORD
;
2716 case UNORDERED_EXPR
:
2717 return COMPCODE_UNORD
;
2719 return COMPCODE_UNLT
;
2721 return COMPCODE_UNEQ
;
2723 return COMPCODE_UNLE
;
2725 return COMPCODE_UNGT
;
2727 return COMPCODE_LTGT
;
2729 return COMPCODE_UNGE
;
2735 /* Convert a compcode bit-based encoding of a comparison operator back
2736 to GCC's enum tree_code representation. This function is the
2737 inverse of comparison_to_compcode. */
2739 static enum tree_code
2740 compcode_to_comparison (enum comparison_code code
)
2757 return ORDERED_EXPR
;
2758 case COMPCODE_UNORD
:
2759 return UNORDERED_EXPR
;
2777 /* Return a tree for the comparison which is the combination of
2778 doing the AND or OR (depending on CODE) of the two operations LCODE
2779 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2780 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2781 if this makes the transformation invalid. */
2784 combine_comparisons (location_t loc
,
2785 enum tree_code code
, enum tree_code lcode
,
2786 enum tree_code rcode
, tree truth_type
,
2787 tree ll_arg
, tree lr_arg
)
2789 bool honor_nans
= HONOR_NANS (ll_arg
);
2790 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2791 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2796 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2797 compcode
= lcompcode
& rcompcode
;
2800 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2801 compcode
= lcompcode
| rcompcode
;
2810 /* Eliminate unordered comparisons, as well as LTGT and ORD
2811 which are not used unless the mode has NaNs. */
2812 compcode
&= ~COMPCODE_UNORD
;
2813 if (compcode
== COMPCODE_LTGT
)
2814 compcode
= COMPCODE_NE
;
2815 else if (compcode
== COMPCODE_ORD
)
2816 compcode
= COMPCODE_TRUE
;
2818 else if (flag_trapping_math
)
2820 /* Check that the original operation and the optimized ones will trap
2821 under the same condition. */
2822 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2823 && (lcompcode
!= COMPCODE_EQ
)
2824 && (lcompcode
!= COMPCODE_ORD
);
2825 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2826 && (rcompcode
!= COMPCODE_EQ
)
2827 && (rcompcode
!= COMPCODE_ORD
);
2828 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2829 && (compcode
!= COMPCODE_EQ
)
2830 && (compcode
!= COMPCODE_ORD
);
2832 /* In a short-circuited boolean expression the LHS might be
2833 such that the RHS, if evaluated, will never trap. For
2834 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2835 if neither x nor y is NaN. (This is a mixed blessing: for
2836 example, the expression above will never trap, hence
2837 optimizing it to x < y would be invalid). */
2838 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2839 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2842 /* If the comparison was short-circuited, and only the RHS
2843 trapped, we may now generate a spurious trap. */
2845 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2848 /* If we changed the conditions that cause a trap, we lose. */
2849 if ((ltrap
|| rtrap
) != trap
)
2853 if (compcode
== COMPCODE_TRUE
)
2854 return constant_boolean_node (true, truth_type
);
2855 else if (compcode
== COMPCODE_FALSE
)
2856 return constant_boolean_node (false, truth_type
);
2859 enum tree_code tcode
;
2861 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2862 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2866 /* Return nonzero if two operands (typically of the same tree node)
2867 are necessarily equal. FLAGS modifies behavior as follows:
2869 If OEP_ONLY_CONST is set, only return nonzero for constants.
2870 This function tests whether the operands are indistinguishable;
2871 it does not test whether they are equal using C's == operation.
2872 The distinction is important for IEEE floating point, because
2873 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2874 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2876 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2877 even though it may hold multiple values during a function.
2878 This is because a GCC tree node guarantees that nothing else is
2879 executed between the evaluation of its "operands" (which may often
2880 be evaluated in arbitrary order). Hence if the operands themselves
2881 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2882 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2883 unset means assuming isochronic (or instantaneous) tree equivalence.
2884 Unless comparing arbitrary expression trees, such as from different
2885 statements, this flag can usually be left unset.
2887 If OEP_PURE_SAME is set, then pure functions with identical arguments
2888 are considered the same. It is used when the caller has other ways
2889 to ensure that global memory is unchanged in between.
2891 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2892 not values of expressions.
2894 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2895 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2897 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2898 any operand with side effect. This is unnecesarily conservative in the
2899 case we know that arg0 and arg1 are in disjoint code paths (such as in
2900 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2901 addresses with TREE_CONSTANT flag set so we know that &var == &var
2902 even if var is volatile. */
2905 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2907 /* When checking, verify at the outermost operand_equal_p call that
2908 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2910 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
2912 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
2916 inchash::hash
hstate0 (0), hstate1 (0);
2917 inchash::add_expr (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
2918 inchash::add_expr (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
2919 hashval_t h0
= hstate0
.end ();
2920 hashval_t h1
= hstate1
.end ();
2921 gcc_assert (h0
== h1
);
2929 /* If either is ERROR_MARK, they aren't equal. */
2930 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2931 || TREE_TYPE (arg0
) == error_mark_node
2932 || TREE_TYPE (arg1
) == error_mark_node
)
2935 /* Similar, if either does not have a type (like a released SSA name),
2936 they aren't equal. */
2937 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2940 /* We cannot consider pointers to different address space equal. */
2941 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2942 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2943 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2944 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2947 /* Check equality of integer constants before bailing out due to
2948 precision differences. */
2949 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2951 /* Address of INTEGER_CST is not defined; check that we did not forget
2952 to drop the OEP_ADDRESS_OF flags. */
2953 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2954 return tree_int_cst_equal (arg0
, arg1
);
2957 if (!(flags
& OEP_ADDRESS_OF
))
2959 /* If both types don't have the same signedness, then we can't consider
2960 them equal. We must check this before the STRIP_NOPS calls
2961 because they may change the signedness of the arguments. As pointers
2962 strictly don't have a signedness, require either two pointers or
2963 two non-pointers as well. */
2964 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2965 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2966 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2969 /* If both types don't have the same precision, then it is not safe
2971 if (element_precision (TREE_TYPE (arg0
))
2972 != element_precision (TREE_TYPE (arg1
)))
2979 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2980 sanity check once the issue is solved. */
2982 /* Addresses of conversions and SSA_NAMEs (and many other things)
2983 are not defined. Check that we did not forget to drop the
2984 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2985 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2986 && TREE_CODE (arg0
) != SSA_NAME
);
2989 /* In case both args are comparisons but with different comparison
2990 code, try to swap the comparison operands of one arg to produce
2991 a match and compare that variant. */
2992 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2993 && COMPARISON_CLASS_P (arg0
)
2994 && COMPARISON_CLASS_P (arg1
))
2996 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2998 if (TREE_CODE (arg0
) == swap_code
)
2999 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3000 TREE_OPERAND (arg1
, 1), flags
)
3001 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3002 TREE_OPERAND (arg1
, 0), flags
);
3005 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3007 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3008 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3010 else if (flags
& OEP_ADDRESS_OF
)
3012 /* If we are interested in comparing addresses ignore
3013 MEM_REF wrappings of the base that can appear just for
3015 if (TREE_CODE (arg0
) == MEM_REF
3017 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3018 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3019 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3021 else if (TREE_CODE (arg1
) == MEM_REF
3023 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3024 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3025 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3033 /* When not checking adddresses, this is needed for conversions and for
3034 COMPONENT_REF. Might as well play it safe and always test this. */
3035 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3036 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3037 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3038 && !(flags
& OEP_ADDRESS_OF
)))
3041 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3042 We don't care about side effects in that case because the SAVE_EXPR
3043 takes care of that for us. In all other cases, two expressions are
3044 equal if they have no side effects. If we have two identical
3045 expressions with side effects that should be treated the same due
3046 to the only side effects being identical SAVE_EXPR's, that will
3047 be detected in the recursive calls below.
3048 If we are taking an invariant address of two identical objects
3049 they are necessarily equal as well. */
3050 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3051 && (TREE_CODE (arg0
) == SAVE_EXPR
3052 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3053 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3056 /* Next handle constant cases, those for which we can return 1 even
3057 if ONLY_CONST is set. */
3058 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3059 switch (TREE_CODE (arg0
))
3062 return tree_int_cst_equal (arg0
, arg1
);
3065 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3066 TREE_FIXED_CST (arg1
));
3069 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3073 if (!HONOR_SIGNED_ZEROS (arg0
))
3075 /* If we do not distinguish between signed and unsigned zero,
3076 consider them equal. */
3077 if (real_zerop (arg0
) && real_zerop (arg1
))
3084 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3085 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3088 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3089 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3092 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3093 for (unsigned int i
= 0; i
< count
; ++i
)
3094 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3095 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3101 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3103 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3107 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3108 && ! memcmp (TREE_STRING_POINTER (arg0
),
3109 TREE_STRING_POINTER (arg1
),
3110 TREE_STRING_LENGTH (arg0
)));
3113 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3114 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3115 flags
| OEP_ADDRESS_OF
3116 | OEP_MATCH_SIDE_EFFECTS
);
3118 /* In GIMPLE empty constructors are allowed in initializers of
3120 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
3125 if (flags
& OEP_ONLY_CONST
)
3128 /* Define macros to test an operand from arg0 and arg1 for equality and a
3129 variant that allows null and views null as being different from any
3130 non-null value. In the latter case, if either is null, the both
3131 must be; otherwise, do the normal comparison. */
3132 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3133 TREE_OPERAND (arg1, N), flags)
3135 #define OP_SAME_WITH_NULL(N) \
3136 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3137 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3139 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3142 /* Two conversions are equal only if signedness and modes match. */
3143 switch (TREE_CODE (arg0
))
3146 case FIX_TRUNC_EXPR
:
3147 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3148 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3158 case tcc_comparison
:
3160 if (OP_SAME (0) && OP_SAME (1))
3163 /* For commutative ops, allow the other order. */
3164 return (commutative_tree_code (TREE_CODE (arg0
))
3165 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3166 TREE_OPERAND (arg1
, 1), flags
)
3167 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3168 TREE_OPERAND (arg1
, 0), flags
));
3171 /* If either of the pointer (or reference) expressions we are
3172 dereferencing contain a side effect, these cannot be equal,
3173 but their addresses can be. */
3174 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3175 && (TREE_SIDE_EFFECTS (arg0
)
3176 || TREE_SIDE_EFFECTS (arg1
)))
3179 switch (TREE_CODE (arg0
))
3182 if (!(flags
& OEP_ADDRESS_OF
)
3183 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3184 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3186 flags
&= ~OEP_ADDRESS_OF
;
3190 /* Require the same offset. */
3191 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3192 TYPE_SIZE (TREE_TYPE (arg1
)),
3193 flags
& ~OEP_ADDRESS_OF
))
3198 case VIEW_CONVERT_EXPR
:
3201 case TARGET_MEM_REF
:
3203 if (!(flags
& OEP_ADDRESS_OF
))
3205 /* Require equal access sizes */
3206 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3207 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3208 || !TYPE_SIZE (TREE_TYPE (arg1
))
3209 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3210 TYPE_SIZE (TREE_TYPE (arg1
)),
3213 /* Verify that access happens in similar types. */
3214 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3216 /* Verify that accesses are TBAA compatible. */
3217 if (!alias_ptr_types_compatible_p
3218 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3219 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3220 || (MR_DEPENDENCE_CLIQUE (arg0
)
3221 != MR_DEPENDENCE_CLIQUE (arg1
))
3222 || (MR_DEPENDENCE_BASE (arg0
)
3223 != MR_DEPENDENCE_BASE (arg1
)))
3225 /* Verify that alignment is compatible. */
3226 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3227 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3230 flags
&= ~OEP_ADDRESS_OF
;
3231 return (OP_SAME (0) && OP_SAME (1)
3232 /* TARGET_MEM_REF require equal extra operands. */
3233 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3234 || (OP_SAME_WITH_NULL (2)
3235 && OP_SAME_WITH_NULL (3)
3236 && OP_SAME_WITH_NULL (4))));
3239 case ARRAY_RANGE_REF
:
3242 flags
&= ~OEP_ADDRESS_OF
;
3243 /* Compare the array index by value if it is constant first as we
3244 may have different types but same value here. */
3245 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3246 TREE_OPERAND (arg1
, 1))
3248 && OP_SAME_WITH_NULL (2)
3249 && OP_SAME_WITH_NULL (3)
3250 /* Compare low bound and element size as with OEP_ADDRESS_OF
3251 we have to account for the offset of the ref. */
3252 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3253 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3254 || (operand_equal_p (array_ref_low_bound
3255 (CONST_CAST_TREE (arg0
)),
3257 (CONST_CAST_TREE (arg1
)), flags
)
3258 && operand_equal_p (array_ref_element_size
3259 (CONST_CAST_TREE (arg0
)),
3260 array_ref_element_size
3261 (CONST_CAST_TREE (arg1
)),
3265 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3266 may be NULL when we're called to compare MEM_EXPRs. */
3267 if (!OP_SAME_WITH_NULL (0)
3270 flags
&= ~OEP_ADDRESS_OF
;
3271 return OP_SAME_WITH_NULL (2);
3276 flags
&= ~OEP_ADDRESS_OF
;
3277 return OP_SAME (1) && OP_SAME (2);
3283 case tcc_expression
:
3284 switch (TREE_CODE (arg0
))
3287 /* Be sure we pass right ADDRESS_OF flag. */
3288 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3289 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3290 TREE_OPERAND (arg1
, 0),
3291 flags
| OEP_ADDRESS_OF
);
3293 case TRUTH_NOT_EXPR
:
3296 case TRUTH_ANDIF_EXPR
:
3297 case TRUTH_ORIF_EXPR
:
3298 return OP_SAME (0) && OP_SAME (1);
3300 case WIDEN_MULT_PLUS_EXPR
:
3301 case WIDEN_MULT_MINUS_EXPR
:
3304 /* The multiplcation operands are commutative. */
3307 case TRUTH_AND_EXPR
:
3309 case TRUTH_XOR_EXPR
:
3310 if (OP_SAME (0) && OP_SAME (1))
3313 /* Otherwise take into account this is a commutative operation. */
3314 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3315 TREE_OPERAND (arg1
, 1), flags
)
3316 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3317 TREE_OPERAND (arg1
, 0), flags
));
3320 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3322 flags
&= ~OEP_ADDRESS_OF
;
3325 case BIT_INSERT_EXPR
:
3326 /* BIT_INSERT_EXPR has an implict operand as the type precision
3327 of op1. Need to check to make sure they are the same. */
3328 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3329 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3330 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3331 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3337 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3342 case PREDECREMENT_EXPR
:
3343 case PREINCREMENT_EXPR
:
3344 case POSTDECREMENT_EXPR
:
3345 case POSTINCREMENT_EXPR
:
3346 if (flags
& OEP_LEXICOGRAPHIC
)
3347 return OP_SAME (0) && OP_SAME (1);
3350 case CLEANUP_POINT_EXPR
:
3352 if (flags
& OEP_LEXICOGRAPHIC
)
3361 switch (TREE_CODE (arg0
))
3364 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3365 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3366 /* If not both CALL_EXPRs are either internal or normal function
3367 functions, then they are not equal. */
3369 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3371 /* If the CALL_EXPRs call different internal functions, then they
3373 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3378 /* If the CALL_EXPRs call different functions, then they are not
3380 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3385 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3387 unsigned int cef
= call_expr_flags (arg0
);
3388 if (flags
& OEP_PURE_SAME
)
3389 cef
&= ECF_CONST
| ECF_PURE
;
3392 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3396 /* Now see if all the arguments are the same. */
3398 const_call_expr_arg_iterator iter0
, iter1
;
3400 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3401 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3403 a0
= next_const_call_expr_arg (&iter0
),
3404 a1
= next_const_call_expr_arg (&iter1
))
3405 if (! operand_equal_p (a0
, a1
, flags
))
3408 /* If we get here and both argument lists are exhausted
3409 then the CALL_EXPRs are equal. */
3410 return ! (a0
|| a1
);
3416 case tcc_declaration
:
3417 /* Consider __builtin_sqrt equal to sqrt. */
3418 return (TREE_CODE (arg0
) == FUNCTION_DECL
3419 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3420 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3421 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3423 case tcc_exceptional
:
3424 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3426 /* In GIMPLE constructors are used only to build vectors from
3427 elements. Individual elements in the constructor must be
3428 indexed in increasing order and form an initial sequence.
3430 We make no effort to compare constructors in generic.
3431 (see sem_variable::equals in ipa-icf which can do so for
3433 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3434 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3437 /* Be sure that vectors constructed have the same representation.
3438 We only tested element precision and modes to match.
3439 Vectors may be BLKmode and thus also check that the number of
3441 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3442 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3445 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3446 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3447 unsigned int len
= vec_safe_length (v0
);
3449 if (len
!= vec_safe_length (v1
))
3452 for (unsigned int i
= 0; i
< len
; i
++)
3454 constructor_elt
*c0
= &(*v0
)[i
];
3455 constructor_elt
*c1
= &(*v1
)[i
];
3457 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3458 /* In GIMPLE the indexes can be either NULL or matching i.
3459 Double check this so we won't get false
3460 positives for GENERIC. */
3462 && (TREE_CODE (c0
->index
) != INTEGER_CST
3463 || !compare_tree_int (c0
->index
, i
)))
3465 && (TREE_CODE (c1
->index
) != INTEGER_CST
3466 || !compare_tree_int (c1
->index
, i
))))
3471 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3472 && (flags
& OEP_LEXICOGRAPHIC
))
3474 /* Compare the STATEMENT_LISTs. */
3475 tree_stmt_iterator tsi1
, tsi2
;
3476 tree body1
= CONST_CAST_TREE (arg0
);
3477 tree body2
= CONST_CAST_TREE (arg1
);
3478 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3479 tsi_next (&tsi1
), tsi_next (&tsi2
))
3481 /* The lists don't have the same number of statements. */
3482 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3484 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3486 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3487 flags
& (OEP_LEXICOGRAPHIC
3488 | OEP_NO_HASH_CHECK
)))
3495 switch (TREE_CODE (arg0
))
3498 if (flags
& OEP_LEXICOGRAPHIC
)
3499 return OP_SAME_WITH_NULL (0);
3501 case DEBUG_BEGIN_STMT
:
3502 if (flags
& OEP_LEXICOGRAPHIC
)
3514 #undef OP_SAME_WITH_NULL
3517 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3518 with a different signedness or a narrower precision. */
3521 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
3523 if (operand_equal_p (arg0
, arg1
, 0))
3526 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3527 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3530 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3531 and see if the inner values are the same. This removes any
3532 signedness comparison, which doesn't matter here. */
3537 if (operand_equal_p (op0
, op1
, 0))
3540 /* Discard a single widening conversion from ARG1 and see if the inner
3541 value is the same as ARG0. */
3542 if (CONVERT_EXPR_P (arg1
)
3543 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
3544 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
3545 < TYPE_PRECISION (TREE_TYPE (arg1
))
3546 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
3552 /* See if ARG is an expression that is either a comparison or is performing
3553 arithmetic on comparisons. The comparisons must only be comparing
3554 two different values, which will be stored in *CVAL1 and *CVAL2; if
3555 they are nonzero it means that some operands have already been found.
3556 No variables may be used anywhere else in the expression except in the
3559 If this is true, return 1. Otherwise, return zero. */
3562 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
3564 enum tree_code code
= TREE_CODE (arg
);
3565 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3567 /* We can handle some of the tcc_expression cases here. */
3568 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3570 else if (tclass
== tcc_expression
3571 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3572 || code
== COMPOUND_EXPR
))
3573 tclass
= tcc_binary
;
3578 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
3581 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
3582 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
3587 case tcc_expression
:
3588 if (code
== COND_EXPR
)
3589 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
3590 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
3591 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
3594 case tcc_comparison
:
3595 /* First see if we can handle the first operand, then the second. For
3596 the second operand, we know *CVAL1 can't be zero. It must be that
3597 one side of the comparison is each of the values; test for the
3598 case where this isn't true by failing if the two operands
3601 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3602 TREE_OPERAND (arg
, 1), 0))
3606 *cval1
= TREE_OPERAND (arg
, 0);
3607 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3609 else if (*cval2
== 0)
3610 *cval2
= TREE_OPERAND (arg
, 0);
3611 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3616 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3618 else if (*cval2
== 0)
3619 *cval2
= TREE_OPERAND (arg
, 1);
3620 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3632 /* ARG is a tree that is known to contain just arithmetic operations and
3633 comparisons. Evaluate the operations in the tree substituting NEW0 for
3634 any occurrence of OLD0 as an operand of a comparison and likewise for
3638 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3639 tree old1
, tree new1
)
3641 tree type
= TREE_TYPE (arg
);
3642 enum tree_code code
= TREE_CODE (arg
);
3643 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3645 /* We can handle some of the tcc_expression cases here. */
3646 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3648 else if (tclass
== tcc_expression
3649 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3650 tclass
= tcc_binary
;
3655 return fold_build1_loc (loc
, code
, type
,
3656 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3657 old0
, new0
, old1
, new1
));
3660 return fold_build2_loc (loc
, code
, type
,
3661 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3662 old0
, new0
, old1
, new1
),
3663 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3664 old0
, new0
, old1
, new1
));
3666 case tcc_expression
:
3670 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3674 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3678 return fold_build3_loc (loc
, code
, type
,
3679 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3680 old0
, new0
, old1
, new1
),
3681 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3682 old0
, new0
, old1
, new1
),
3683 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3684 old0
, new0
, old1
, new1
));
3688 /* Fall through - ??? */
3690 case tcc_comparison
:
3692 tree arg0
= TREE_OPERAND (arg
, 0);
3693 tree arg1
= TREE_OPERAND (arg
, 1);
3695 /* We need to check both for exact equality and tree equality. The
3696 former will be true if the operand has a side-effect. In that
3697 case, we know the operand occurred exactly once. */
3699 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3701 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3704 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3706 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3709 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3717 /* Return a tree for the case when the result of an expression is RESULT
3718 converted to TYPE and OMITTED was previously an operand of the expression
3719 but is now not needed (e.g., we folded OMITTED * 0).
3721 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3722 the conversion of RESULT to TYPE. */
3725 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3727 tree t
= fold_convert_loc (loc
, type
, result
);
3729 /* If the resulting operand is an empty statement, just return the omitted
3730 statement casted to void. */
3731 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3732 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3733 fold_ignored_result (omitted
));
3735 if (TREE_SIDE_EFFECTS (omitted
))
3736 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3737 fold_ignored_result (omitted
), t
);
3739 return non_lvalue_loc (loc
, t
);
3742 /* Return a tree for the case when the result of an expression is RESULT
3743 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3744 of the expression but are now not needed.
3746 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3747 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3748 evaluated before OMITTED2. Otherwise, if neither has side effects,
3749 just do the conversion of RESULT to TYPE. */
3752 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3753 tree omitted1
, tree omitted2
)
3755 tree t
= fold_convert_loc (loc
, type
, result
);
3757 if (TREE_SIDE_EFFECTS (omitted2
))
3758 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3759 if (TREE_SIDE_EFFECTS (omitted1
))
3760 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3762 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3766 /* Return a simplified tree node for the truth-negation of ARG. This
3767 never alters ARG itself. We assume that ARG is an operation that
3768 returns a truth value (0 or 1).
3770 FIXME: one would think we would fold the result, but it causes
3771 problems with the dominator optimizer. */
3774 fold_truth_not_expr (location_t loc
, tree arg
)
3776 tree type
= TREE_TYPE (arg
);
3777 enum tree_code code
= TREE_CODE (arg
);
3778 location_t loc1
, loc2
;
3780 /* If this is a comparison, we can simply invert it, except for
3781 floating-point non-equality comparisons, in which case we just
3782 enclose a TRUTH_NOT_EXPR around what we have. */
3784 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3786 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3787 if (FLOAT_TYPE_P (op_type
)
3788 && flag_trapping_math
3789 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3790 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3793 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3794 if (code
== ERROR_MARK
)
3797 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3798 TREE_OPERAND (arg
, 1));
3799 if (TREE_NO_WARNING (arg
))
3800 TREE_NO_WARNING (ret
) = 1;
3807 return constant_boolean_node (integer_zerop (arg
), type
);
3809 case TRUTH_AND_EXPR
:
3810 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3811 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3812 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3813 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3814 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3817 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3818 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3819 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3820 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3821 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3823 case TRUTH_XOR_EXPR
:
3824 /* Here we can invert either operand. We invert the first operand
3825 unless the second operand is a TRUTH_NOT_EXPR in which case our
3826 result is the XOR of the first operand with the inside of the
3827 negation of the second operand. */
3829 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3830 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3831 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3833 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3834 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3835 TREE_OPERAND (arg
, 1));
3837 case TRUTH_ANDIF_EXPR
:
3838 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3839 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3840 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3841 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3842 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3844 case TRUTH_ORIF_EXPR
:
3845 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3846 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3847 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3848 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3849 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3851 case TRUTH_NOT_EXPR
:
3852 return TREE_OPERAND (arg
, 0);
3856 tree arg1
= TREE_OPERAND (arg
, 1);
3857 tree arg2
= TREE_OPERAND (arg
, 2);
3859 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3860 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3862 /* A COND_EXPR may have a throw as one operand, which
3863 then has void type. Just leave void operands
3865 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3866 VOID_TYPE_P (TREE_TYPE (arg1
))
3867 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3868 VOID_TYPE_P (TREE_TYPE (arg2
))
3869 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3873 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3874 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3875 TREE_OPERAND (arg
, 0),
3876 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3878 case NON_LVALUE_EXPR
:
3879 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3880 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3883 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3884 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3889 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3890 return build1_loc (loc
, TREE_CODE (arg
), type
,
3891 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3894 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3896 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3899 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3901 case CLEANUP_POINT_EXPR
:
3902 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3903 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3904 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3911 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3912 assume that ARG is an operation that returns a truth value (0 or 1
3913 for scalars, 0 or -1 for vectors). Return the folded expression if
3914 folding is successful. Otherwise, return NULL_TREE. */
3917 fold_invert_truthvalue (location_t loc
, tree arg
)
3919 tree type
= TREE_TYPE (arg
);
3920 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3926 /* Return a simplified tree node for the truth-negation of ARG. This
3927 never alters ARG itself. We assume that ARG is an operation that
3928 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3931 invert_truthvalue_loc (location_t loc
, tree arg
)
3933 if (TREE_CODE (arg
) == ERROR_MARK
)
3936 tree type
= TREE_TYPE (arg
);
3937 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3943 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3944 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3945 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3946 is the original memory reference used to preserve the alias set of
3950 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
3951 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
3952 int unsignedp
, int reversep
)
3954 tree result
, bftype
;
3956 /* Attempt not to lose the access path if possible. */
3957 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
3959 tree ninner
= TREE_OPERAND (orig_inner
, 0);
3961 poly_int64 nbitsize
, nbitpos
;
3963 int nunsignedp
, nreversep
, nvolatilep
= 0;
3964 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
3965 &noffset
, &nmode
, &nunsignedp
,
3966 &nreversep
, &nvolatilep
);
3968 && noffset
== NULL_TREE
3969 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
3979 alias_set_type iset
= get_alias_set (orig_inner
);
3980 if (iset
== 0 && get_alias_set (inner
) != iset
)
3981 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
3982 build_fold_addr_expr (inner
),
3983 build_int_cst (ptr_type_node
, 0));
3985 if (known_eq (bitpos
, 0) && !reversep
)
3987 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3988 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3989 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3990 && tree_fits_shwi_p (size
)
3991 && tree_to_shwi (size
) == bitsize
)
3992 return fold_convert_loc (loc
, type
, inner
);
3996 if (TYPE_PRECISION (bftype
) != bitsize
3997 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3998 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4000 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4001 bitsize_int (bitsize
), bitsize_int (bitpos
));
4002 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4005 result
= fold_convert_loc (loc
, type
, result
);
4010 /* Optimize a bit-field compare.
4012 There are two cases: First is a compare against a constant and the
4013 second is a comparison of two items where the fields are at the same
4014 bit position relative to the start of a chunk (byte, halfword, word)
4015 large enough to contain it. In these cases we can avoid the shift
4016 implicit in bitfield extractions.
4018 For constants, we emit a compare of the shifted constant with the
4019 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4020 compared. For two fields at the same position, we do the ANDs with the
4021 similar mask and compare the result of the ANDs.
4023 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4024 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4025 are the left and right operands of the comparison, respectively.
4027 If the optimization described above can be done, we return the resulting
4028 tree. Otherwise we return zero. */
4031 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4032 tree compare_type
, tree lhs
, tree rhs
)
4034 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4035 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4036 tree type
= TREE_TYPE (lhs
);
4038 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4039 machine_mode lmode
, rmode
;
4040 scalar_int_mode nmode
;
4041 int lunsignedp
, runsignedp
;
4042 int lreversep
, rreversep
;
4043 int lvolatilep
= 0, rvolatilep
= 0;
4044 tree linner
, rinner
= NULL_TREE
;
4048 /* Get all the information about the extractions being done. If the bit size
4049 is the same as the size of the underlying object, we aren't doing an
4050 extraction at all and so can do nothing. We also don't want to
4051 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4052 then will no longer be able to replace it. */
4053 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4054 &lunsignedp
, &lreversep
, &lvolatilep
);
4056 || !known_size_p (plbitsize
)
4057 || !plbitsize
.is_constant (&lbitsize
)
4058 || !plbitpos
.is_constant (&lbitpos
)
4059 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4061 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4066 rreversep
= lreversep
;
4069 /* If this is not a constant, we can only do something if bit positions,
4070 sizes, signedness and storage order are the same. */
4072 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4073 &runsignedp
, &rreversep
, &rvolatilep
);
4076 || maybe_ne (lbitpos
, rbitpos
)
4077 || maybe_ne (lbitsize
, rbitsize
)
4078 || lunsignedp
!= runsignedp
4079 || lreversep
!= rreversep
4081 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4086 /* Honor the C++ memory model and mimic what RTL expansion does. */
4087 poly_uint64 bitstart
= 0;
4088 poly_uint64 bitend
= 0;
4089 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4091 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4092 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4096 /* See if we can find a mode to refer to this field. We should be able to,
4097 but fail if we can't. */
4098 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4099 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4100 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4101 TYPE_ALIGN (TREE_TYPE (rinner
))),
4102 BITS_PER_WORD
, false, &nmode
))
4105 /* Set signed and unsigned types of the precision of this mode for the
4107 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4109 /* Compute the bit position and size for the new reference and our offset
4110 within it. If the new reference is the same size as the original, we
4111 won't optimize anything, so return zero. */
4112 nbitsize
= GET_MODE_BITSIZE (nmode
);
4113 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4115 if (nbitsize
== lbitsize
)
4118 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4119 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4121 /* Make the mask to be used against the extracted field. */
4122 mask
= build_int_cst_type (unsigned_type
, -1);
4123 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4124 mask
= const_binop (RSHIFT_EXPR
, mask
,
4125 size_int (nbitsize
- lbitsize
- lbitpos
));
4132 /* If not comparing with constant, just rework the comparison
4134 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4135 nbitsize
, nbitpos
, 1, lreversep
);
4136 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4137 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4138 nbitsize
, nbitpos
, 1, rreversep
);
4139 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4140 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4143 /* Otherwise, we are handling the constant case. See if the constant is too
4144 big for the field. Warn and return a tree for 0 (false) if so. We do
4145 this not only for its own sake, but to avoid having to test for this
4146 error case below. If we didn't, we might generate wrong code.
4148 For unsigned fields, the constant shifted right by the field length should
4149 be all zero. For signed fields, the high-order bits should agree with
4154 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4156 warning (0, "comparison is always %d due to width of bit-field",
4158 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4163 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4164 if (tem
!= 0 && tem
!= -1)
4166 warning (0, "comparison is always %d due to width of bit-field",
4168 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4175 /* Single-bit compares should always be against zero. */
4176 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4178 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4179 rhs
= build_int_cst (type
, 0);
4182 /* Make a new bitfield reference, shift the constant over the
4183 appropriate number of bits and mask it with the computed mask
4184 (in case this was a signed field). If we changed it, make a new one. */
4185 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4186 nbitsize
, nbitpos
, 1, lreversep
);
4188 rhs
= const_binop (BIT_AND_EXPR
,
4189 const_binop (LSHIFT_EXPR
,
4190 fold_convert_loc (loc
, unsigned_type
, rhs
),
4191 size_int (lbitpos
)),
4194 lhs
= build2_loc (loc
, code
, compare_type
,
4195 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4199 /* Subroutine for fold_truth_andor_1: decode a field reference.
4201 If EXP is a comparison reference, we return the innermost reference.
4203 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4204 set to the starting bit number.
4206 If the innermost field can be completely contained in a mode-sized
4207 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4209 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4210 otherwise it is not changed.
4212 *PUNSIGNEDP is set to the signedness of the field.
4214 *PREVERSEP is set to the storage order of the field.
4216 *PMASK is set to the mask used. This is either contained in a
4217 BIT_AND_EXPR or derived from the width of the field.
4219 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4221 Return 0 if this is not a component reference or is one that we can't
4222 do anything with. */
4225 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4226 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4227 int *punsignedp
, int *preversep
, int *pvolatilep
,
4228 tree
*pmask
, tree
*pand_mask
)
4231 tree outer_type
= 0;
4233 tree mask
, inner
, offset
;
4235 unsigned int precision
;
4237 /* All the optimizations using this function assume integer fields.
4238 There are problems with FP fields since the type_for_size call
4239 below can fail for, e.g., XFmode. */
4240 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4243 /* We are interested in the bare arrangement of bits, so strip everything
4244 that doesn't affect the machine mode. However, record the type of the
4245 outermost expression if it may matter below. */
4246 if (CONVERT_EXPR_P (exp
)
4247 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4248 outer_type
= TREE_TYPE (exp
);
4251 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4253 and_mask
= TREE_OPERAND (exp
, 1);
4254 exp
= TREE_OPERAND (exp
, 0);
4255 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4256 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4260 poly_int64 poly_bitsize
, poly_bitpos
;
4261 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4262 pmode
, punsignedp
, preversep
, pvolatilep
);
4263 if ((inner
== exp
&& and_mask
== 0)
4264 || !poly_bitsize
.is_constant (pbitsize
)
4265 || !poly_bitpos
.is_constant (pbitpos
)
4268 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4269 /* Reject out-of-bound accesses (PR79731). */
4270 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4271 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4272 *pbitpos
+ *pbitsize
) < 0))
4277 /* If the number of bits in the reference is the same as the bitsize of
4278 the outer type, then the outer type gives the signedness. Otherwise
4279 (in case of a small bitfield) the signedness is unchanged. */
4280 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4281 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4283 /* Compute the mask to access the bitfield. */
4284 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4285 precision
= TYPE_PRECISION (unsigned_type
);
4287 mask
= build_int_cst_type (unsigned_type
, -1);
4289 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4290 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4292 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4294 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4295 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4298 *pand_mask
= and_mask
;
4302 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4303 bit positions and MASK is SIGNED. */
4306 all_ones_mask_p (const_tree mask
, unsigned int size
)
4308 tree type
= TREE_TYPE (mask
);
4309 unsigned int precision
= TYPE_PRECISION (type
);
4311 /* If this function returns true when the type of the mask is
4312 UNSIGNED, then there will be errors. In particular see
4313 gcc.c-torture/execute/990326-1.c. There does not appear to be
4314 any documentation paper trail as to why this is so. But the pre
4315 wide-int worked with that restriction and it has been preserved
4317 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4320 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
4323 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4324 represents the sign bit of EXP's type. If EXP represents a sign
4325 or zero extension, also test VAL against the unextended type.
4326 The return value is the (sub)expression whose sign bit is VAL,
4327 or NULL_TREE otherwise. */
4330 sign_bit_p (tree exp
, const_tree val
)
4335 /* Tree EXP must have an integral type. */
4336 t
= TREE_TYPE (exp
);
4337 if (! INTEGRAL_TYPE_P (t
))
4340 /* Tree VAL must be an integer constant. */
4341 if (TREE_CODE (val
) != INTEGER_CST
4342 || TREE_OVERFLOW (val
))
4345 width
= TYPE_PRECISION (t
);
4346 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
4349 /* Handle extension from a narrower type. */
4350 if (TREE_CODE (exp
) == NOP_EXPR
4351 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4352 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4357 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4358 to be evaluated unconditionally. */
4361 simple_operand_p (const_tree exp
)
4363 /* Strip any conversions that don't change the machine mode. */
4366 return (CONSTANT_CLASS_P (exp
)
4367 || TREE_CODE (exp
) == SSA_NAME
4369 && ! TREE_ADDRESSABLE (exp
)
4370 && ! TREE_THIS_VOLATILE (exp
)
4371 && ! DECL_NONLOCAL (exp
)
4372 /* Don't regard global variables as simple. They may be
4373 allocated in ways unknown to the compiler (shared memory,
4374 #pragma weak, etc). */
4375 && ! TREE_PUBLIC (exp
)
4376 && ! DECL_EXTERNAL (exp
)
4377 /* Weakrefs are not safe to be read, since they can be NULL.
4378 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4379 have DECL_WEAK flag set. */
4380 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4381 /* Loading a static variable is unduly expensive, but global
4382 registers aren't expensive. */
4383 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4386 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4387 to be evaluated unconditionally.
4388 I addition to simple_operand_p, we assume that comparisons, conversions,
4389 and logic-not operations are simple, if their operands are simple, too. */
4392 simple_operand_p_2 (tree exp
)
4394 enum tree_code code
;
4396 if (TREE_SIDE_EFFECTS (exp
)
4397 || tree_could_trap_p (exp
))
4400 while (CONVERT_EXPR_P (exp
))
4401 exp
= TREE_OPERAND (exp
, 0);
4403 code
= TREE_CODE (exp
);
4405 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4406 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4407 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4409 if (code
== TRUTH_NOT_EXPR
)
4410 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4412 return simple_operand_p (exp
);
4416 /* The following functions are subroutines to fold_range_test and allow it to
4417 try to change a logical combination of comparisons into a range test.
4420 X == 2 || X == 3 || X == 4 || X == 5
4424 (unsigned) (X - 2) <= 3
4426 We describe each set of comparisons as being either inside or outside
4427 a range, using a variable named like IN_P, and then describe the
4428 range with a lower and upper bound. If one of the bounds is omitted,
4429 it represents either the highest or lowest value of the type.
4431 In the comments below, we represent a range by two numbers in brackets
4432 preceded by a "+" to designate being inside that range, or a "-" to
4433 designate being outside that range, so the condition can be inverted by
4434 flipping the prefix. An omitted bound is represented by a "-". For
4435 example, "- [-, 10]" means being outside the range starting at the lowest
4436 possible value and ending at 10, in other words, being greater than 10.
4437 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4440 We set up things so that the missing bounds are handled in a consistent
4441 manner so neither a missing bound nor "true" and "false" need to be
4442 handled using a special case. */
4444 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4445 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4446 and UPPER1_P are nonzero if the respective argument is an upper bound
4447 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4448 must be specified for a comparison. ARG1 will be converted to ARG0's
4449 type if both are specified. */
4452 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4453 tree arg1
, int upper1_p
)
4459 /* If neither arg represents infinity, do the normal operation.
4460 Else, if not a comparison, return infinity. Else handle the special
4461 comparison rules. Note that most of the cases below won't occur, but
4462 are handled for consistency. */
4464 if (arg0
!= 0 && arg1
!= 0)
4466 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4467 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4469 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4472 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4475 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4476 for neither. In real maths, we cannot assume open ended ranges are
4477 the same. But, this is computer arithmetic, where numbers are finite.
4478 We can therefore make the transformation of any unbounded range with
4479 the value Z, Z being greater than any representable number. This permits
4480 us to treat unbounded ranges as equal. */
4481 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4482 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4486 result
= sgn0
== sgn1
;
4489 result
= sgn0
!= sgn1
;
4492 result
= sgn0
< sgn1
;
4495 result
= sgn0
<= sgn1
;
4498 result
= sgn0
> sgn1
;
4501 result
= sgn0
>= sgn1
;
4507 return constant_boolean_node (result
, type
);
4510 /* Helper routine for make_range. Perform one step for it, return
4511 new expression if the loop should continue or NULL_TREE if it should
4515 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4516 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4517 bool *strict_overflow_p
)
4519 tree arg0_type
= TREE_TYPE (arg0
);
4520 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4521 int in_p
= *p_in_p
, n_in_p
;
4525 case TRUTH_NOT_EXPR
:
4526 /* We can only do something if the range is testing for zero. */
4527 if (low
== NULL_TREE
|| high
== NULL_TREE
4528 || ! integer_zerop (low
) || ! integer_zerop (high
))
4533 case EQ_EXPR
: case NE_EXPR
:
4534 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4535 /* We can only do something if the range is testing for zero
4536 and if the second operand is an integer constant. Note that
4537 saying something is "in" the range we make is done by
4538 complementing IN_P since it will set in the initial case of
4539 being not equal to zero; "out" is leaving it alone. */
4540 if (low
== NULL_TREE
|| high
== NULL_TREE
4541 || ! integer_zerop (low
) || ! integer_zerop (high
)
4542 || TREE_CODE (arg1
) != INTEGER_CST
)
4547 case NE_EXPR
: /* - [c, c] */
4550 case EQ_EXPR
: /* + [c, c] */
4551 in_p
= ! in_p
, low
= high
= arg1
;
4553 case GT_EXPR
: /* - [-, c] */
4554 low
= 0, high
= arg1
;
4556 case GE_EXPR
: /* + [c, -] */
4557 in_p
= ! in_p
, low
= arg1
, high
= 0;
4559 case LT_EXPR
: /* - [c, -] */
4560 low
= arg1
, high
= 0;
4562 case LE_EXPR
: /* + [-, c] */
4563 in_p
= ! in_p
, low
= 0, high
= arg1
;
4569 /* If this is an unsigned comparison, we also know that EXP is
4570 greater than or equal to zero. We base the range tests we make
4571 on that fact, so we record it here so we can parse existing
4572 range tests. We test arg0_type since often the return type
4573 of, e.g. EQ_EXPR, is boolean. */
4574 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4576 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4578 build_int_cst (arg0_type
, 0),
4582 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4584 /* If the high bound is missing, but we have a nonzero low
4585 bound, reverse the range so it goes from zero to the low bound
4587 if (high
== 0 && low
&& ! integer_zerop (low
))
4590 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4591 build_int_cst (TREE_TYPE (low
), 1), 0);
4592 low
= build_int_cst (arg0_type
, 0);
4602 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4603 low and high are non-NULL, then normalize will DTRT. */
4604 if (!TYPE_UNSIGNED (arg0_type
)
4605 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4607 if (low
== NULL_TREE
)
4608 low
= TYPE_MIN_VALUE (arg0_type
);
4609 if (high
== NULL_TREE
)
4610 high
= TYPE_MAX_VALUE (arg0_type
);
4613 /* (-x) IN [a,b] -> x in [-b, -a] */
4614 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4615 build_int_cst (exp_type
, 0),
4617 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4618 build_int_cst (exp_type
, 0),
4620 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4626 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4627 build_int_cst (exp_type
, 1));
4631 if (TREE_CODE (arg1
) != INTEGER_CST
)
4634 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4635 move a constant to the other side. */
4636 if (!TYPE_UNSIGNED (arg0_type
)
4637 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4640 /* If EXP is signed, any overflow in the computation is undefined,
4641 so we don't worry about it so long as our computations on
4642 the bounds don't overflow. For unsigned, overflow is defined
4643 and this is exactly the right thing. */
4644 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4645 arg0_type
, low
, 0, arg1
, 0);
4646 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4647 arg0_type
, high
, 1, arg1
, 0);
4648 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4649 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4652 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4653 *strict_overflow_p
= true;
4656 /* Check for an unsigned range which has wrapped around the maximum
4657 value thus making n_high < n_low, and normalize it. */
4658 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4660 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4661 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4662 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4663 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4665 /* If the range is of the form +/- [ x+1, x ], we won't
4666 be able to normalize it. But then, it represents the
4667 whole range or the empty set, so make it
4669 if (tree_int_cst_equal (n_low
, low
)
4670 && tree_int_cst_equal (n_high
, high
))
4676 low
= n_low
, high
= n_high
;
4684 case NON_LVALUE_EXPR
:
4685 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4688 if (! INTEGRAL_TYPE_P (arg0_type
)
4689 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4690 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4693 n_low
= low
, n_high
= high
;
4696 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4699 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4701 /* If we're converting arg0 from an unsigned type, to exp,
4702 a signed type, we will be doing the comparison as unsigned.
4703 The tests above have already verified that LOW and HIGH
4706 So we have to ensure that we will handle large unsigned
4707 values the same way that the current signed bounds treat
4710 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4714 /* For fixed-point modes, we need to pass the saturating flag
4715 as the 2nd parameter. */
4716 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4718 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4719 TYPE_SATURATING (arg0_type
));
4722 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4724 /* A range without an upper bound is, naturally, unbounded.
4725 Since convert would have cropped a very large value, use
4726 the max value for the destination type. */
4728 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4729 : TYPE_MAX_VALUE (arg0_type
);
4731 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4732 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4733 fold_convert_loc (loc
, arg0_type
,
4735 build_int_cst (arg0_type
, 1));
4737 /* If the low bound is specified, "and" the range with the
4738 range for which the original unsigned value will be
4742 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4743 1, fold_convert_loc (loc
, arg0_type
,
4748 in_p
= (n_in_p
== in_p
);
4752 /* Otherwise, "or" the range with the range of the input
4753 that will be interpreted as negative. */
4754 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4755 1, fold_convert_loc (loc
, arg0_type
,
4760 in_p
= (in_p
!= n_in_p
);
4774 /* Given EXP, a logical expression, set the range it is testing into
4775 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4776 actually being tested. *PLOW and *PHIGH will be made of the same
4777 type as the returned expression. If EXP is not a comparison, we
4778 will most likely not be returning a useful value and range. Set
4779 *STRICT_OVERFLOW_P to true if the return value is only valid
4780 because signed overflow is undefined; otherwise, do not change
4781 *STRICT_OVERFLOW_P. */
4784 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4785 bool *strict_overflow_p
)
4787 enum tree_code code
;
4788 tree arg0
, arg1
= NULL_TREE
;
4789 tree exp_type
, nexp
;
4792 location_t loc
= EXPR_LOCATION (exp
);
4794 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4795 and see if we can refine the range. Some of the cases below may not
4796 happen, but it doesn't seem worth worrying about this. We "continue"
4797 the outer loop when we've changed something; otherwise we "break"
4798 the switch, which will "break" the while. */
4801 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4805 code
= TREE_CODE (exp
);
4806 exp_type
= TREE_TYPE (exp
);
4809 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4811 if (TREE_OPERAND_LENGTH (exp
) > 0)
4812 arg0
= TREE_OPERAND (exp
, 0);
4813 if (TREE_CODE_CLASS (code
) == tcc_binary
4814 || TREE_CODE_CLASS (code
) == tcc_comparison
4815 || (TREE_CODE_CLASS (code
) == tcc_expression
4816 && TREE_OPERAND_LENGTH (exp
) > 1))
4817 arg1
= TREE_OPERAND (exp
, 1);
4819 if (arg0
== NULL_TREE
)
4822 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4823 &high
, &in_p
, strict_overflow_p
);
4824 if (nexp
== NULL_TREE
)
4829 /* If EXP is a constant, we can evaluate whether this is true or false. */
4830 if (TREE_CODE (exp
) == INTEGER_CST
)
4832 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4834 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4840 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4844 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4845 a bitwise check i.e. when
4846 LOW == 0xXX...X00...0
4847 HIGH == 0xXX...X11...1
4848 Return corresponding mask in MASK and stem in VALUE. */
4851 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
4854 if (TREE_CODE (low
) != INTEGER_CST
4855 || TREE_CODE (high
) != INTEGER_CST
)
4858 unsigned prec
= TYPE_PRECISION (type
);
4859 wide_int lo
= wi::to_wide (low
, prec
);
4860 wide_int hi
= wi::to_wide (high
, prec
);
4862 wide_int end_mask
= lo
^ hi
;
4863 if ((end_mask
& (end_mask
+ 1)) != 0
4864 || (lo
& end_mask
) != 0)
4867 wide_int stem_mask
= ~end_mask
;
4868 wide_int stem
= lo
& stem_mask
;
4869 if (stem
!= (hi
& stem_mask
))
4872 *mask
= wide_int_to_tree (type
, stem_mask
);
4873 *value
= wide_int_to_tree (type
, stem
);
4878 /* Helper routine for build_range_check and match.pd. Return the type to
4879 perform the check or NULL if it shouldn't be optimized. */
4882 range_check_type (tree etype
)
4884 /* First make sure that arithmetics in this type is valid, then make sure
4885 that it wraps around. */
4886 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4887 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4888 TYPE_UNSIGNED (etype
));
4890 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4892 tree utype
, minv
, maxv
;
4894 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4895 for the type in question, as we rely on this here. */
4896 utype
= unsigned_type_for (etype
);
4897 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
4898 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4899 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4900 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
4902 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4911 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4912 type, TYPE, return an expression to test if EXP is in (or out of, depending
4913 on IN_P) the range. Return 0 if the test couldn't be created. */
4916 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4917 tree low
, tree high
)
4919 tree etype
= TREE_TYPE (exp
), mask
, value
;
4921 /* Disable this optimization for function pointer expressions
4922 on targets that require function pointer canonicalization. */
4923 if (targetm
.have_canonicalize_funcptr_for_compare ()
4924 && TREE_CODE (etype
) == POINTER_TYPE
4925 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4930 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4932 return invert_truthvalue_loc (loc
, value
);
4937 if (low
== 0 && high
== 0)
4938 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4941 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4942 fold_convert_loc (loc
, etype
, high
));
4945 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4946 fold_convert_loc (loc
, etype
, low
));
4948 if (operand_equal_p (low
, high
, 0))
4949 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4950 fold_convert_loc (loc
, etype
, low
));
4952 if (TREE_CODE (exp
) == BIT_AND_EXPR
4953 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
4954 return fold_build2_loc (loc
, EQ_EXPR
, type
,
4955 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
4959 if (integer_zerop (low
))
4961 if (! TYPE_UNSIGNED (etype
))
4963 etype
= unsigned_type_for (etype
);
4964 high
= fold_convert_loc (loc
, etype
, high
);
4965 exp
= fold_convert_loc (loc
, etype
, exp
);
4967 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4970 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4971 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4973 int prec
= TYPE_PRECISION (etype
);
4975 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
4977 if (TYPE_UNSIGNED (etype
))
4979 tree signed_etype
= signed_type_for (etype
);
4980 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4982 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4984 etype
= signed_etype
;
4985 exp
= fold_convert_loc (loc
, etype
, exp
);
4987 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4988 build_int_cst (etype
, 0));
4992 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4993 This requires wrap-around arithmetics for the type of the expression. */
4994 etype
= range_check_type (etype
);
4995 if (etype
== NULL_TREE
)
4998 if (POINTER_TYPE_P (etype
))
4999 etype
= unsigned_type_for (etype
);
5001 high
= fold_convert_loc (loc
, etype
, high
);
5002 low
= fold_convert_loc (loc
, etype
, low
);
5003 exp
= fold_convert_loc (loc
, etype
, exp
);
5005 value
= const_binop (MINUS_EXPR
, high
, low
);
5007 if (value
!= 0 && !TREE_OVERFLOW (value
))
5008 return build_range_check (loc
, type
,
5009 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5010 1, build_int_cst (etype
, 0), value
);
5015 /* Return the predecessor of VAL in its type, handling the infinite case. */
5018 range_predecessor (tree val
)
5020 tree type
= TREE_TYPE (val
);
5022 if (INTEGRAL_TYPE_P (type
)
5023 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5026 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5027 build_int_cst (TREE_TYPE (val
), 1), 0);
5030 /* Return the successor of VAL in its type, handling the infinite case. */
5033 range_successor (tree val
)
5035 tree type
= TREE_TYPE (val
);
5037 if (INTEGRAL_TYPE_P (type
)
5038 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5041 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5042 build_int_cst (TREE_TYPE (val
), 1), 0);
5045 /* Given two ranges, see if we can merge them into one. Return 1 if we
5046 can, 0 if we can't. Set the output range into the specified parameters. */
5049 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5050 tree high0
, int in1_p
, tree low1
, tree high1
)
5058 int lowequal
= ((low0
== 0 && low1
== 0)
5059 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5060 low0
, 0, low1
, 0)));
5061 int highequal
= ((high0
== 0 && high1
== 0)
5062 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5063 high0
, 1, high1
, 1)));
5065 /* Make range 0 be the range that starts first, or ends last if they
5066 start at the same value. Swap them if it isn't. */
5067 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5070 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5071 high1
, 1, high0
, 1))))
5073 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5074 tem
= low0
, low0
= low1
, low1
= tem
;
5075 tem
= high0
, high0
= high1
, high1
= tem
;
5078 /* Now flag two cases, whether the ranges are disjoint or whether the
5079 second range is totally subsumed in the first. Note that the tests
5080 below are simplified by the ones above. */
5081 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5082 high0
, 1, low1
, 0));
5083 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5084 high1
, 1, high0
, 1));
5086 /* We now have four cases, depending on whether we are including or
5087 excluding the two ranges. */
5090 /* If they don't overlap, the result is false. If the second range
5091 is a subset it is the result. Otherwise, the range is from the start
5092 of the second to the end of the first. */
5094 in_p
= 0, low
= high
= 0;
5096 in_p
= 1, low
= low1
, high
= high1
;
5098 in_p
= 1, low
= low1
, high
= high0
;
5101 else if (in0_p
&& ! in1_p
)
5103 /* If they don't overlap, the result is the first range. If they are
5104 equal, the result is false. If the second range is a subset of the
5105 first, and the ranges begin at the same place, we go from just after
5106 the end of the second range to the end of the first. If the second
5107 range is not a subset of the first, or if it is a subset and both
5108 ranges end at the same place, the range starts at the start of the
5109 first range and ends just before the second range.
5110 Otherwise, we can't describe this as a single range. */
5112 in_p
= 1, low
= low0
, high
= high0
;
5113 else if (lowequal
&& highequal
)
5114 in_p
= 0, low
= high
= 0;
5115 else if (subset
&& lowequal
)
5117 low
= range_successor (high1
);
5122 /* We are in the weird situation where high0 > high1 but
5123 high1 has no successor. Punt. */
5127 else if (! subset
|| highequal
)
5130 high
= range_predecessor (low1
);
5134 /* low0 < low1 but low1 has no predecessor. Punt. */
5142 else if (! in0_p
&& in1_p
)
5144 /* If they don't overlap, the result is the second range. If the second
5145 is a subset of the first, the result is false. Otherwise,
5146 the range starts just after the first range and ends at the
5147 end of the second. */
5149 in_p
= 1, low
= low1
, high
= high1
;
5150 else if (subset
|| highequal
)
5151 in_p
= 0, low
= high
= 0;
5154 low
= range_successor (high0
);
5159 /* high1 > high0 but high0 has no successor. Punt. */
5167 /* The case where we are excluding both ranges. Here the complex case
5168 is if they don't overlap. In that case, the only time we have a
5169 range is if they are adjacent. If the second is a subset of the
5170 first, the result is the first. Otherwise, the range to exclude
5171 starts at the beginning of the first range and ends at the end of the
5175 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5176 range_successor (high0
),
5178 in_p
= 0, low
= low0
, high
= high1
;
5181 /* Canonicalize - [min, x] into - [-, x]. */
5182 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5183 switch (TREE_CODE (TREE_TYPE (low0
)))
5186 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5188 (TYPE_MODE (TREE_TYPE (low0
)))))
5192 if (tree_int_cst_equal (low0
,
5193 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5197 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5198 && integer_zerop (low0
))
5205 /* Canonicalize - [x, max] into - [x, -]. */
5206 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5207 switch (TREE_CODE (TREE_TYPE (high1
)))
5210 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5212 (TYPE_MODE (TREE_TYPE (high1
)))))
5216 if (tree_int_cst_equal (high1
,
5217 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5221 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5222 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5224 build_int_cst (TREE_TYPE (high1
), 1),
5232 /* The ranges might be also adjacent between the maximum and
5233 minimum values of the given type. For
5234 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5235 return + [x + 1, y - 1]. */
5236 if (low0
== 0 && high1
== 0)
5238 low
= range_successor (high0
);
5239 high
= range_predecessor (low1
);
5240 if (low
== 0 || high
== 0)
5250 in_p
= 0, low
= low0
, high
= high0
;
5252 in_p
= 0, low
= low0
, high
= high1
;
5255 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5260 /* Subroutine of fold, looking inside expressions of the form
5261 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5262 of the COND_EXPR. This function is being used also to optimize
5263 A op B ? C : A, by reversing the comparison first.
5265 Return a folded expression whose code is not a COND_EXPR
5266 anymore, or NULL_TREE if no folding opportunity is found. */
5269 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5270 tree arg0
, tree arg1
, tree arg2
)
5272 enum tree_code comp_code
= TREE_CODE (arg0
);
5273 tree arg00
= TREE_OPERAND (arg0
, 0);
5274 tree arg01
= TREE_OPERAND (arg0
, 1);
5275 tree arg1_type
= TREE_TYPE (arg1
);
5281 /* If we have A op 0 ? A : -A, consider applying the following
5284 A == 0? A : -A same as -A
5285 A != 0? A : -A same as A
5286 A >= 0? A : -A same as abs (A)
5287 A > 0? A : -A same as abs (A)
5288 A <= 0? A : -A same as -abs (A)
5289 A < 0? A : -A same as -abs (A)
5291 None of these transformations work for modes with signed
5292 zeros. If A is +/-0, the first two transformations will
5293 change the sign of the result (from +0 to -0, or vice
5294 versa). The last four will fix the sign of the result,
5295 even though the original expressions could be positive or
5296 negative, depending on the sign of A.
5298 Note that all these transformations are correct if A is
5299 NaN, since the two alternatives (A and -A) are also NaNs. */
5300 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5301 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5302 ? real_zerop (arg01
)
5303 : integer_zerop (arg01
))
5304 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5305 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5306 /* In the case that A is of the form X-Y, '-A' (arg2) may
5307 have already been folded to Y-X, check for that. */
5308 || (TREE_CODE (arg1
) == MINUS_EXPR
5309 && TREE_CODE (arg2
) == MINUS_EXPR
5310 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5311 TREE_OPERAND (arg2
, 1), 0)
5312 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5313 TREE_OPERAND (arg2
, 0), 0))))
5318 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5319 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5322 return fold_convert_loc (loc
, type
, arg1
);
5325 if (flag_trapping_math
)
5330 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5332 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5333 return fold_convert_loc (loc
, type
, tem
);
5336 if (flag_trapping_math
)
5341 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5343 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5344 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5346 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5350 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5351 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5352 both transformations are correct when A is NaN: A != 0
5353 is then true, and A == 0 is false. */
5355 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5356 && integer_zerop (arg01
) && integer_zerop (arg2
))
5358 if (comp_code
== NE_EXPR
)
5359 return fold_convert_loc (loc
, type
, arg1
);
5360 else if (comp_code
== EQ_EXPR
)
5361 return build_zero_cst (type
);
5364 /* Try some transformations of A op B ? A : B.
5366 A == B? A : B same as B
5367 A != B? A : B same as A
5368 A >= B? A : B same as max (A, B)
5369 A > B? A : B same as max (B, A)
5370 A <= B? A : B same as min (A, B)
5371 A < B? A : B same as min (B, A)
5373 As above, these transformations don't work in the presence
5374 of signed zeros. For example, if A and B are zeros of
5375 opposite sign, the first two transformations will change
5376 the sign of the result. In the last four, the original
5377 expressions give different results for (A=+0, B=-0) and
5378 (A=-0, B=+0), but the transformed expressions do not.
5380 The first two transformations are correct if either A or B
5381 is a NaN. In the first transformation, the condition will
5382 be false, and B will indeed be chosen. In the case of the
5383 second transformation, the condition A != B will be true,
5384 and A will be chosen.
5386 The conversions to max() and min() are not correct if B is
5387 a number and A is not. The conditions in the original
5388 expressions will be false, so all four give B. The min()
5389 and max() versions would give a NaN instead. */
5390 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5391 && operand_equal_for_comparison_p (arg01
, arg2
)
5392 /* Avoid these transformations if the COND_EXPR may be used
5393 as an lvalue in the C++ front-end. PR c++/19199. */
5395 || VECTOR_TYPE_P (type
)
5396 || (! lang_GNU_CXX ()
5397 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5398 || ! maybe_lvalue_p (arg1
)
5399 || ! maybe_lvalue_p (arg2
)))
5401 tree comp_op0
= arg00
;
5402 tree comp_op1
= arg01
;
5403 tree comp_type
= TREE_TYPE (comp_op0
);
5408 return fold_convert_loc (loc
, type
, arg2
);
5410 return fold_convert_loc (loc
, type
, arg1
);
5415 /* In C++ a ?: expression can be an lvalue, so put the
5416 operand which will be used if they are equal first
5417 so that we can convert this back to the
5418 corresponding COND_EXPR. */
5419 if (!HONOR_NANS (arg1
))
5421 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5422 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5423 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5424 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5425 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5426 comp_op1
, comp_op0
);
5427 return fold_convert_loc (loc
, type
, tem
);
5434 if (!HONOR_NANS (arg1
))
5436 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5437 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5438 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5439 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5440 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5441 comp_op1
, comp_op0
);
5442 return fold_convert_loc (loc
, type
, tem
);
5446 if (!HONOR_NANS (arg1
))
5447 return fold_convert_loc (loc
, type
, arg2
);
5450 if (!HONOR_NANS (arg1
))
5451 return fold_convert_loc (loc
, type
, arg1
);
5454 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5464 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5465 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5466 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5470 /* EXP is some logical combination of boolean tests. See if we can
5471 merge it into some range test. Return the new tree if so. */
5474 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5477 int or_op
= (code
== TRUTH_ORIF_EXPR
5478 || code
== TRUTH_OR_EXPR
);
5479 int in0_p
, in1_p
, in_p
;
5480 tree low0
, low1
, low
, high0
, high1
, high
;
5481 bool strict_overflow_p
= false;
5483 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5484 "when simplifying range test");
5486 if (!INTEGRAL_TYPE_P (type
))
5489 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5490 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5492 /* If this is an OR operation, invert both sides; we will invert
5493 again at the end. */
5495 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5497 /* If both expressions are the same, if we can merge the ranges, and we
5498 can build the range test, return it or it inverted. If one of the
5499 ranges is always true or always false, consider it to be the same
5500 expression as the other. */
5501 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5502 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5504 && (tem
= (build_range_check (loc
, type
,
5506 : rhs
!= 0 ? rhs
: integer_zero_node
,
5507 in_p
, low
, high
))) != 0)
5509 if (strict_overflow_p
)
5510 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5511 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5514 /* On machines where the branch cost is expensive, if this is a
5515 short-circuited branch and the underlying object on both sides
5516 is the same, make a non-short-circuit operation. */
5517 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5518 && !flag_sanitize_coverage
5519 && lhs
!= 0 && rhs
!= 0
5520 && (code
== TRUTH_ANDIF_EXPR
5521 || code
== TRUTH_ORIF_EXPR
)
5522 && operand_equal_p (lhs
, rhs
, 0))
5524 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5525 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5526 which cases we can't do this. */
5527 if (simple_operand_p (lhs
))
5528 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5529 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5532 else if (!lang_hooks
.decls
.global_bindings_p ()
5533 && !CONTAINS_PLACEHOLDER_P (lhs
))
5535 tree common
= save_expr (lhs
);
5537 if ((lhs
= build_range_check (loc
, type
, common
,
5538 or_op
? ! in0_p
: in0_p
,
5540 && (rhs
= build_range_check (loc
, type
, common
,
5541 or_op
? ! in1_p
: in1_p
,
5544 if (strict_overflow_p
)
5545 fold_overflow_warning (warnmsg
,
5546 WARN_STRICT_OVERFLOW_COMPARISON
);
5547 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5548 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5557 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5558 bit value. Arrange things so the extra bits will be set to zero if and
5559 only if C is signed-extended to its full width. If MASK is nonzero,
5560 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5563 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5565 tree type
= TREE_TYPE (c
);
5566 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
5569 if (p
== modesize
|| unsignedp
)
5572 /* We work by getting just the sign bit into the low-order bit, then
5573 into the high-order bit, then sign-extend. We then XOR that value
5575 temp
= build_int_cst (TREE_TYPE (c
),
5576 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
5578 /* We must use a signed type in order to get an arithmetic right shift.
5579 However, we must also avoid introducing accidental overflows, so that
5580 a subsequent call to integer_zerop will work. Hence we must
5581 do the type conversion here. At this point, the constant is either
5582 zero or one, and the conversion to a signed type can never overflow.
5583 We could get an overflow if this conversion is done anywhere else. */
5584 if (TYPE_UNSIGNED (type
))
5585 temp
= fold_convert (signed_type_for (type
), temp
);
5587 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5588 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5590 temp
= const_binop (BIT_AND_EXPR
, temp
,
5591 fold_convert (TREE_TYPE (c
), mask
));
5592 /* If necessary, convert the type back to match the type of C. */
5593 if (TYPE_UNSIGNED (type
))
5594 temp
= fold_convert (type
, temp
);
5596 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5599 /* For an expression that has the form
5603 we can drop one of the inner expressions and simplify to
5607 LOC is the location of the resulting expression. OP is the inner
5608 logical operation; the left-hand side in the examples above, while CMPOP
5609 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5610 removing a condition that guards another, as in
5611 (A != NULL && A->...) || A == NULL
5612 which we must not transform. If RHS_ONLY is true, only eliminate the
5613 right-most operand of the inner logical operation. */
5616 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5619 tree type
= TREE_TYPE (cmpop
);
5620 enum tree_code code
= TREE_CODE (cmpop
);
5621 enum tree_code truthop_code
= TREE_CODE (op
);
5622 tree lhs
= TREE_OPERAND (op
, 0);
5623 tree rhs
= TREE_OPERAND (op
, 1);
5624 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5625 enum tree_code rhs_code
= TREE_CODE (rhs
);
5626 enum tree_code lhs_code
= TREE_CODE (lhs
);
5627 enum tree_code inv_code
;
5629 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5632 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5635 if (rhs_code
== truthop_code
)
5637 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5638 if (newrhs
!= NULL_TREE
)
5641 rhs_code
= TREE_CODE (rhs
);
5644 if (lhs_code
== truthop_code
&& !rhs_only
)
5646 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5647 if (newlhs
!= NULL_TREE
)
5650 lhs_code
= TREE_CODE (lhs
);
5654 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5655 if (inv_code
== rhs_code
5656 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5657 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5659 if (!rhs_only
&& inv_code
== lhs_code
5660 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5661 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5663 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5664 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5669 /* Find ways of folding logical expressions of LHS and RHS:
5670 Try to merge two comparisons to the same innermost item.
5671 Look for range tests like "ch >= '0' && ch <= '9'".
5672 Look for combinations of simple terms on machines with expensive branches
5673 and evaluate the RHS unconditionally.
5675 For example, if we have p->a == 2 && p->b == 4 and we can make an
5676 object large enough to span both A and B, we can do this with a comparison
5677 against the object ANDed with the a mask.
5679 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5680 operations to do this with one comparison.
5682 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5683 function and the one above.
5685 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5686 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5688 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5691 We return the simplified tree or 0 if no optimization is possible. */
5694 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5697 /* If this is the "or" of two comparisons, we can do something if
5698 the comparisons are NE_EXPR. If this is the "and", we can do something
5699 if the comparisons are EQ_EXPR. I.e.,
5700 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5702 WANTED_CODE is this operation code. For single bit fields, we can
5703 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5704 comparison for one-bit fields. */
5706 enum tree_code wanted_code
;
5707 enum tree_code lcode
, rcode
;
5708 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5709 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5710 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5711 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5712 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5713 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5714 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5715 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5716 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5717 scalar_int_mode lnmode
, rnmode
;
5718 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5719 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5720 tree l_const
, r_const
;
5721 tree lntype
, rntype
, result
;
5722 HOST_WIDE_INT first_bit
, end_bit
;
5725 /* Start by getting the comparison codes. Fail if anything is volatile.
5726 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5727 it were surrounded with a NE_EXPR. */
5729 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5732 lcode
= TREE_CODE (lhs
);
5733 rcode
= TREE_CODE (rhs
);
5735 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5737 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5738 build_int_cst (TREE_TYPE (lhs
), 0));
5742 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5744 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5745 build_int_cst (TREE_TYPE (rhs
), 0));
5749 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5750 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5753 ll_arg
= TREE_OPERAND (lhs
, 0);
5754 lr_arg
= TREE_OPERAND (lhs
, 1);
5755 rl_arg
= TREE_OPERAND (rhs
, 0);
5756 rr_arg
= TREE_OPERAND (rhs
, 1);
5758 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5759 if (simple_operand_p (ll_arg
)
5760 && simple_operand_p (lr_arg
))
5762 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5763 && operand_equal_p (lr_arg
, rr_arg
, 0))
5765 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5766 truth_type
, ll_arg
, lr_arg
);
5770 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5771 && operand_equal_p (lr_arg
, rl_arg
, 0))
5773 result
= combine_comparisons (loc
, code
, lcode
,
5774 swap_tree_comparison (rcode
),
5775 truth_type
, ll_arg
, lr_arg
);
5781 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5782 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5784 /* If the RHS can be evaluated unconditionally and its operands are
5785 simple, it wins to evaluate the RHS unconditionally on machines
5786 with expensive branches. In this case, this isn't a comparison
5787 that can be merged. */
5789 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5791 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5792 && simple_operand_p (rl_arg
)
5793 && simple_operand_p (rr_arg
))
5795 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5796 if (code
== TRUTH_OR_EXPR
5797 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5798 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5799 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5800 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5801 return build2_loc (loc
, NE_EXPR
, truth_type
,
5802 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5804 build_int_cst (TREE_TYPE (ll_arg
), 0));
5806 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5807 if (code
== TRUTH_AND_EXPR
5808 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5809 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5810 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5811 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5812 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5813 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5815 build_int_cst (TREE_TYPE (ll_arg
), 0));
5818 /* See if the comparisons can be merged. Then get all the parameters for
5821 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5822 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5825 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5827 ll_inner
= decode_field_reference (loc
, &ll_arg
,
5828 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5829 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5830 &ll_mask
, &ll_and_mask
);
5831 lr_inner
= decode_field_reference (loc
, &lr_arg
,
5832 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5833 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5834 &lr_mask
, &lr_and_mask
);
5835 rl_inner
= decode_field_reference (loc
, &rl_arg
,
5836 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5837 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5838 &rl_mask
, &rl_and_mask
);
5839 rr_inner
= decode_field_reference (loc
, &rr_arg
,
5840 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5841 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5842 &rr_mask
, &rr_and_mask
);
5844 /* It must be true that the inner operation on the lhs of each
5845 comparison must be the same if we are to be able to do anything.
5846 Then see if we have constants. If not, the same must be true for
5849 || ll_reversep
!= rl_reversep
5850 || ll_inner
== 0 || rl_inner
== 0
5851 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5854 if (TREE_CODE (lr_arg
) == INTEGER_CST
5855 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5857 l_const
= lr_arg
, r_const
= rr_arg
;
5858 lr_reversep
= ll_reversep
;
5860 else if (lr_reversep
!= rr_reversep
5861 || lr_inner
== 0 || rr_inner
== 0
5862 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5865 l_const
= r_const
= 0;
5867 /* If either comparison code is not correct for our logical operation,
5868 fail. However, we can convert a one-bit comparison against zero into
5869 the opposite comparison against that bit being set in the field. */
5871 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5872 if (lcode
!= wanted_code
)
5874 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5876 /* Make the left operand unsigned, since we are only interested
5877 in the value of one bit. Otherwise we are doing the wrong
5886 /* This is analogous to the code for l_const above. */
5887 if (rcode
!= wanted_code
)
5889 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5898 /* See if we can find a mode that contains both fields being compared on
5899 the left. If we can't, fail. Otherwise, update all constants and masks
5900 to be relative to a field of that size. */
5901 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5902 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5903 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5904 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
5905 volatilep
, &lnmode
))
5908 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5909 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5910 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5911 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5913 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5915 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5916 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5919 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5920 size_int (xll_bitpos
));
5921 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5922 size_int (xrl_bitpos
));
5926 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5927 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5928 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5929 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5930 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5933 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5935 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5940 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5941 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5942 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5943 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5944 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5947 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5949 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5953 /* If the right sides are not constant, do the same for it. Also,
5954 disallow this optimization if a size or signedness mismatch occurs
5955 between the left and right sides. */
5958 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5959 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5960 /* Make sure the two fields on the right
5961 correspond to the left without being swapped. */
5962 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5965 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5966 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5967 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5968 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
5969 volatilep
, &rnmode
))
5972 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5973 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5974 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5975 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5977 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5979 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5980 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5983 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5985 size_int (xlr_bitpos
));
5986 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5988 size_int (xrr_bitpos
));
5990 /* Make a mask that corresponds to both fields being compared.
5991 Do this for both items being compared. If the operands are the
5992 same size and the bits being compared are in the same position
5993 then we can do this by masking both and comparing the masked
5995 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5996 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5997 if (lnbitsize
== rnbitsize
5998 && xll_bitpos
== xlr_bitpos
6002 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6003 lntype
, lnbitsize
, lnbitpos
,
6004 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6005 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6006 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6008 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6009 rntype
, rnbitsize
, rnbitpos
,
6010 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6011 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6012 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6014 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6017 /* There is still another way we can do something: If both pairs of
6018 fields being compared are adjacent, we may be able to make a wider
6019 field containing them both.
6021 Note that we still must mask the lhs/rhs expressions. Furthermore,
6022 the mask must be shifted to account for the shift done by
6023 make_bit_field_ref. */
6024 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6025 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6026 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6027 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6035 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6036 ll_bitsize
+ rl_bitsize
,
6037 MIN (ll_bitpos
, rl_bitpos
),
6038 ll_unsignedp
, ll_reversep
);
6039 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6040 lr_bitsize
+ rr_bitsize
,
6041 MIN (lr_bitpos
, rr_bitpos
),
6042 lr_unsignedp
, lr_reversep
);
6044 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6045 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6046 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6047 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6049 /* Convert to the smaller type before masking out unwanted bits. */
6051 if (lntype
!= rntype
)
6053 if (lnbitsize
> rnbitsize
)
6055 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6056 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6059 else if (lnbitsize
< rnbitsize
)
6061 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6062 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6067 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6068 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6070 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6071 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6073 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6079 /* Handle the case of comparisons with constants. If there is something in
6080 common between the masks, those bits of the constants must be the same.
6081 If not, the condition is always false. Test for this to avoid generating
6082 incorrect code below. */
6083 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6084 if (! integer_zerop (result
)
6085 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6086 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6088 if (wanted_code
== NE_EXPR
)
6090 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6091 return constant_boolean_node (true, truth_type
);
6095 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6096 return constant_boolean_node (false, truth_type
);
6103 /* Construct the expression we will return. First get the component
6104 reference we will make. Unless the mask is all ones the width of
6105 that field, perform the mask operation. Then compare with the
6107 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6108 lntype
, lnbitsize
, lnbitpos
,
6109 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6111 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6112 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6113 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6115 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6116 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6119 /* T is an integer expression that is being multiplied, divided, or taken a
6120 modulus (CODE says which and what kind of divide or modulus) by a
6121 constant C. See if we can eliminate that operation by folding it with
6122 other operations already in T. WIDE_TYPE, if non-null, is a type that
6123 should be used for the computation if wider than our type.
6125 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6126 (X * 2) + (Y * 4). We must, however, be assured that either the original
6127 expression would not overflow or that overflow is undefined for the type
6128 in the language in question.
6130 If we return a non-null expression, it is an equivalent form of the
6131 original computation, but need not be in the original type.
6133 We set *STRICT_OVERFLOW_P to true if the return values depends on
6134 signed overflow being undefined. Otherwise we do not change
6135 *STRICT_OVERFLOW_P. */
6138 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6139 bool *strict_overflow_p
)
6141 /* To avoid exponential search depth, refuse to allow recursion past
6142 three levels. Beyond that (1) it's highly unlikely that we'll find
6143 something interesting and (2) we've probably processed it before
6144 when we built the inner expression. */
6153 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6160 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6161 bool *strict_overflow_p
)
6163 tree type
= TREE_TYPE (t
);
6164 enum tree_code tcode
= TREE_CODE (t
);
6165 tree ctype
= (wide_type
!= 0
6166 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6167 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6168 ? wide_type
: type
);
6170 int same_p
= tcode
== code
;
6171 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6172 bool sub_strict_overflow_p
;
6174 /* Don't deal with constants of zero here; they confuse the code below. */
6175 if (integer_zerop (c
))
6178 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6179 op0
= TREE_OPERAND (t
, 0);
6181 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6182 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6184 /* Note that we need not handle conditional operations here since fold
6185 already handles those cases. So just do arithmetic here. */
6189 /* For a constant, we can always simplify if we are a multiply
6190 or (for divide and modulus) if it is a multiple of our constant. */
6191 if (code
== MULT_EXPR
6192 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6195 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6196 fold_convert (ctype
, c
));
6197 /* If the multiplication overflowed, we lost information on it.
6198 See PR68142 and PR69845. */
6199 if (TREE_OVERFLOW (tem
))
6205 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6206 /* If op0 is an expression ... */
6207 if ((COMPARISON_CLASS_P (op0
)
6208 || UNARY_CLASS_P (op0
)
6209 || BINARY_CLASS_P (op0
)
6210 || VL_EXP_CLASS_P (op0
)
6211 || EXPRESSION_CLASS_P (op0
))
6212 /* ... and has wrapping overflow, and its type is smaller
6213 than ctype, then we cannot pass through as widening. */
6214 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6215 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6216 && (TYPE_PRECISION (ctype
)
6217 > TYPE_PRECISION (TREE_TYPE (op0
))))
6218 /* ... or this is a truncation (t is narrower than op0),
6219 then we cannot pass through this narrowing. */
6220 || (TYPE_PRECISION (type
)
6221 < TYPE_PRECISION (TREE_TYPE (op0
)))
6222 /* ... or signedness changes for division or modulus,
6223 then we cannot pass through this conversion. */
6224 || (code
!= MULT_EXPR
6225 && (TYPE_UNSIGNED (ctype
)
6226 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6227 /* ... or has undefined overflow while the converted to
6228 type has not, we cannot do the operation in the inner type
6229 as that would introduce undefined overflow. */
6230 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6231 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6232 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6235 /* Pass the constant down and see if we can make a simplification. If
6236 we can, replace this expression with the inner simplification for
6237 possible later conversion to our or some other type. */
6238 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6239 && TREE_CODE (t2
) == INTEGER_CST
6240 && !TREE_OVERFLOW (t2
)
6241 && (t1
= extract_muldiv (op0
, t2
, code
,
6242 code
== MULT_EXPR
? ctype
: NULL_TREE
,
6243 strict_overflow_p
)) != 0)
6248 /* If widening the type changes it from signed to unsigned, then we
6249 must avoid building ABS_EXPR itself as unsigned. */
6250 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6252 tree cstype
= (*signed_type_for
) (ctype
);
6253 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6256 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6257 return fold_convert (ctype
, t1
);
6261 /* If the constant is negative, we cannot simplify this. */
6262 if (tree_int_cst_sgn (c
) == -1)
6266 /* For division and modulus, type can't be unsigned, as e.g.
6267 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6268 For signed types, even with wrapping overflow, this is fine. */
6269 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6271 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6273 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6276 case MIN_EXPR
: case MAX_EXPR
:
6277 /* If widening the type changes the signedness, then we can't perform
6278 this optimization as that changes the result. */
6279 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6282 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6283 sub_strict_overflow_p
= false;
6284 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6285 &sub_strict_overflow_p
)) != 0
6286 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6287 &sub_strict_overflow_p
)) != 0)
6289 if (tree_int_cst_sgn (c
) < 0)
6290 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6291 if (sub_strict_overflow_p
)
6292 *strict_overflow_p
= true;
6293 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6294 fold_convert (ctype
, t2
));
6298 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6299 /* If the second operand is constant, this is a multiplication
6300 or floor division, by a power of two, so we can treat it that
6301 way unless the multiplier or divisor overflows. Signed
6302 left-shift overflow is implementation-defined rather than
6303 undefined in C90, so do not convert signed left shift into
6305 if (TREE_CODE (op1
) == INTEGER_CST
6306 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6307 /* const_binop may not detect overflow correctly,
6308 so check for it explicitly here. */
6309 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
6311 && (t1
= fold_convert (ctype
,
6312 const_binop (LSHIFT_EXPR
, size_one_node
,
6314 && !TREE_OVERFLOW (t1
))
6315 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6316 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6318 fold_convert (ctype
, op0
),
6320 c
, code
, wide_type
, strict_overflow_p
);
6323 case PLUS_EXPR
: case MINUS_EXPR
:
6324 /* See if we can eliminate the operation on both sides. If we can, we
6325 can return a new PLUS or MINUS. If we can't, the only remaining
6326 cases where we can do anything are if the second operand is a
6328 sub_strict_overflow_p
= false;
6329 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6330 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6331 if (t1
!= 0 && t2
!= 0
6332 && TYPE_OVERFLOW_WRAPS (ctype
)
6333 && (code
== MULT_EXPR
6334 /* If not multiplication, we can only do this if both operands
6335 are divisible by c. */
6336 || (multiple_of_p (ctype
, op0
, c
)
6337 && multiple_of_p (ctype
, op1
, c
))))
6339 if (sub_strict_overflow_p
)
6340 *strict_overflow_p
= true;
6341 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6342 fold_convert (ctype
, t2
));
6345 /* If this was a subtraction, negate OP1 and set it to be an addition.
6346 This simplifies the logic below. */
6347 if (tcode
== MINUS_EXPR
)
6349 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6350 /* If OP1 was not easily negatable, the constant may be OP0. */
6351 if (TREE_CODE (op0
) == INTEGER_CST
)
6353 std::swap (op0
, op1
);
6358 if (TREE_CODE (op1
) != INTEGER_CST
)
6361 /* If either OP1 or C are negative, this optimization is not safe for
6362 some of the division and remainder types while for others we need
6363 to change the code. */
6364 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6366 if (code
== CEIL_DIV_EXPR
)
6367 code
= FLOOR_DIV_EXPR
;
6368 else if (code
== FLOOR_DIV_EXPR
)
6369 code
= CEIL_DIV_EXPR
;
6370 else if (code
!= MULT_EXPR
6371 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6375 /* If it's a multiply or a division/modulus operation of a multiple
6376 of our constant, do the operation and verify it doesn't overflow. */
6377 if (code
== MULT_EXPR
6378 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6381 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6382 fold_convert (ctype
, c
));
6383 /* We allow the constant to overflow with wrapping semantics. */
6385 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6391 /* If we have an unsigned type, we cannot widen the operation since it
6392 will change the result if the original computation overflowed. */
6393 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6396 /* The last case is if we are a multiply. In that case, we can
6397 apply the distributive law to commute the multiply and addition
6398 if the multiplication of the constants doesn't overflow
6399 and overflow is defined. With undefined overflow
6400 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6401 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6402 return fold_build2 (tcode
, ctype
,
6403 fold_build2 (code
, ctype
,
6404 fold_convert (ctype
, op0
),
6405 fold_convert (ctype
, c
)),
6411 /* We have a special case here if we are doing something like
6412 (C * 8) % 4 since we know that's zero. */
6413 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6414 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6415 /* If the multiplication can overflow we cannot optimize this. */
6416 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6417 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6418 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6421 *strict_overflow_p
= true;
6422 return omit_one_operand (type
, integer_zero_node
, op0
);
6425 /* ... fall through ... */
6427 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6428 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6429 /* If we can extract our operation from the LHS, do so and return a
6430 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6431 do something only if the second operand is a constant. */
6433 && TYPE_OVERFLOW_WRAPS (ctype
)
6434 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6435 strict_overflow_p
)) != 0)
6436 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6437 fold_convert (ctype
, op1
));
6438 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6439 && TYPE_OVERFLOW_WRAPS (ctype
)
6440 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6441 strict_overflow_p
)) != 0)
6442 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6443 fold_convert (ctype
, t1
));
6444 else if (TREE_CODE (op1
) != INTEGER_CST
)
6447 /* If these are the same operation types, we can associate them
6448 assuming no overflow. */
6451 bool overflow_p
= false;
6452 bool overflow_mul_p
;
6453 signop sign
= TYPE_SIGN (ctype
);
6454 unsigned prec
= TYPE_PRECISION (ctype
);
6455 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6456 wi::to_wide (c
, prec
),
6457 sign
, &overflow_mul_p
);
6458 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6460 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6463 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6464 wide_int_to_tree (ctype
, mul
));
6467 /* If these operations "cancel" each other, we have the main
6468 optimizations of this pass, which occur when either constant is a
6469 multiple of the other, in which case we replace this with either an
6470 operation or CODE or TCODE.
6472 If we have an unsigned type, we cannot do this since it will change
6473 the result if the original computation overflowed. */
6474 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6475 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6476 || (tcode
== MULT_EXPR
6477 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6478 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6479 && code
!= MULT_EXPR
)))
6481 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
6484 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6485 *strict_overflow_p
= true;
6486 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6487 fold_convert (ctype
,
6488 const_binop (TRUNC_DIV_EXPR
,
6491 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
6494 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6495 *strict_overflow_p
= true;
6496 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6497 fold_convert (ctype
,
6498 const_binop (TRUNC_DIV_EXPR
,
6511 /* Return a node which has the indicated constant VALUE (either 0 or
6512 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6513 and is of the indicated TYPE. */
6516 constant_boolean_node (bool value
, tree type
)
6518 if (type
== integer_type_node
)
6519 return value
? integer_one_node
: integer_zero_node
;
6520 else if (type
== boolean_type_node
)
6521 return value
? boolean_true_node
: boolean_false_node
;
6522 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6523 return build_vector_from_val (type
,
6524 build_int_cst (TREE_TYPE (type
),
6527 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6531 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6532 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6533 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6534 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6535 COND is the first argument to CODE; otherwise (as in the example
6536 given here), it is the second argument. TYPE is the type of the
6537 original expression. Return NULL_TREE if no simplification is
6541 fold_binary_op_with_conditional_arg (location_t loc
,
6542 enum tree_code code
,
6543 tree type
, tree op0
, tree op1
,
6544 tree cond
, tree arg
, int cond_first_p
)
6546 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6547 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6548 tree test
, true_value
, false_value
;
6549 tree lhs
= NULL_TREE
;
6550 tree rhs
= NULL_TREE
;
6551 enum tree_code cond_code
= COND_EXPR
;
6553 if (TREE_CODE (cond
) == COND_EXPR
6554 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6556 test
= TREE_OPERAND (cond
, 0);
6557 true_value
= TREE_OPERAND (cond
, 1);
6558 false_value
= TREE_OPERAND (cond
, 2);
6559 /* If this operand throws an expression, then it does not make
6560 sense to try to perform a logical or arithmetic operation
6562 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6564 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6567 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6568 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6570 tree testtype
= TREE_TYPE (cond
);
6572 true_value
= constant_boolean_node (true, testtype
);
6573 false_value
= constant_boolean_node (false, testtype
);
6576 /* Detect the case of mixing vector and scalar types - bail out. */
6579 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6580 cond_code
= VEC_COND_EXPR
;
6582 /* This transformation is only worthwhile if we don't have to wrap ARG
6583 in a SAVE_EXPR and the operation can be simplified without recursing
6584 on at least one of the branches once its pushed inside the COND_EXPR. */
6585 if (!TREE_CONSTANT (arg
)
6586 && (TREE_SIDE_EFFECTS (arg
)
6587 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6588 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6591 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6594 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6596 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6598 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6602 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6604 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6606 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6609 /* Check that we have simplified at least one of the branches. */
6610 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6613 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6617 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6619 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6620 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6621 ADDEND is the same as X.
6623 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6624 and finite. The problematic cases are when X is zero, and its mode
6625 has signed zeros. In the case of rounding towards -infinity,
6626 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6627 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6630 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6632 if (!real_zerop (addend
))
6635 /* Don't allow the fold with -fsignaling-nans. */
6636 if (HONOR_SNANS (element_mode (type
)))
6639 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6640 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6643 /* In a vector or complex, we would need to check the sign of all zeros. */
6644 if (TREE_CODE (addend
) != REAL_CST
)
6647 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6648 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6651 /* The mode has signed zeros, and we have to honor their sign.
6652 In this situation, there is only one case we can return true for.
6653 X - 0 is the same as X unless rounding towards -infinity is
6655 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6658 /* Subroutine of match.pd that optimizes comparisons of a division by
6659 a nonzero integer constant against an integer constant, i.e.
6662 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6663 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6666 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
6667 tree
*hi
, bool *neg_overflow
)
6669 tree prod
, tmp
, type
= TREE_TYPE (c1
);
6670 signop sign
= TYPE_SIGN (type
);
6673 /* We have to do this the hard way to detect unsigned overflow.
6674 prod = int_const_binop (MULT_EXPR, c1, c2); */
6675 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
6676 prod
= force_fit_type (type
, val
, -1, overflow
);
6677 *neg_overflow
= false;
6679 if (sign
== UNSIGNED
)
6681 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
6684 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6685 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
6686 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
6688 else if (tree_int_cst_sgn (c1
) >= 0)
6690 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
6691 switch (tree_int_cst_sgn (c2
))
6694 *neg_overflow
= true;
6695 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6700 *lo
= fold_negate_const (tmp
, type
);
6705 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6715 /* A negative divisor reverses the relational operators. */
6716 code
= swap_tree_comparison (code
);
6718 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
6719 switch (tree_int_cst_sgn (c2
))
6722 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6727 *hi
= fold_negate_const (tmp
, type
);
6732 *neg_overflow
= true;
6733 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6742 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
6745 if (TREE_OVERFLOW (*lo
)
6746 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
6748 if (TREE_OVERFLOW (*hi
)
6749 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
6756 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6757 equality/inequality test, then return a simplified form of the test
6758 using a sign testing. Otherwise return NULL. TYPE is the desired
6762 fold_single_bit_test_into_sign_test (location_t loc
,
6763 enum tree_code code
, tree arg0
, tree arg1
,
6766 /* If this is testing a single bit, we can optimize the test. */
6767 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6768 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6769 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6771 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6772 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6773 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6775 if (arg00
!= NULL_TREE
6776 /* This is only a win if casting to a signed type is cheap,
6777 i.e. when arg00's type is not a partial mode. */
6778 && type_has_mode_precision_p (TREE_TYPE (arg00
)))
6780 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6781 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6783 fold_convert_loc (loc
, stype
, arg00
),
6784 build_int_cst (stype
, 0));
6791 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6792 equality/inequality test, then return a simplified form of
6793 the test using shifts and logical operations. Otherwise return
6794 NULL. TYPE is the desired result type. */
6797 fold_single_bit_test (location_t loc
, enum tree_code code
,
6798 tree arg0
, tree arg1
, tree result_type
)
6800 /* If this is testing a single bit, we can optimize the test. */
6801 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6802 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6803 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6805 tree inner
= TREE_OPERAND (arg0
, 0);
6806 tree type
= TREE_TYPE (arg0
);
6807 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6808 scalar_int_mode operand_mode
= SCALAR_INT_TYPE_MODE (type
);
6810 tree signed_type
, unsigned_type
, intermediate_type
;
6813 /* First, see if we can fold the single bit test into a sign-bit
6815 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6820 /* Otherwise we have (A & C) != 0 where C is a single bit,
6821 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6822 Similarly for (A & C) == 0. */
6824 /* If INNER is a right shift of a constant and it plus BITNUM does
6825 not overflow, adjust BITNUM and INNER. */
6826 if (TREE_CODE (inner
) == RSHIFT_EXPR
6827 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6828 && bitnum
< TYPE_PRECISION (type
)
6829 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner
, 1)),
6830 TYPE_PRECISION (type
) - bitnum
))
6832 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6833 inner
= TREE_OPERAND (inner
, 0);
6836 /* If we are going to be able to omit the AND below, we must do our
6837 operations as unsigned. If we must use the AND, we have a choice.
6838 Normally unsigned is faster, but for some machines signed is. */
6839 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
6840 && !flag_syntax_only
) ? 0 : 1;
6842 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6843 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6844 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6845 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6848 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6849 inner
, size_int (bitnum
));
6851 one
= build_int_cst (intermediate_type
, 1);
6853 if (code
== EQ_EXPR
)
6854 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6856 /* Put the AND last so it can combine with more things. */
6857 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6859 /* Make sure to return the proper type. */
6860 inner
= fold_convert_loc (loc
, result_type
, inner
);
6867 /* Test whether it is preferable two swap two operands, ARG0 and
6868 ARG1, for example because ARG0 is an integer constant and ARG1
6872 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
6874 if (CONSTANT_CLASS_P (arg1
))
6876 if (CONSTANT_CLASS_P (arg0
))
6882 if (TREE_CONSTANT (arg1
))
6884 if (TREE_CONSTANT (arg0
))
6887 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6888 for commutative and comparison operators. Ensuring a canonical
6889 form allows the optimizers to find additional redundancies without
6890 having to explicitly check for both orderings. */
6891 if (TREE_CODE (arg0
) == SSA_NAME
6892 && TREE_CODE (arg1
) == SSA_NAME
6893 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6896 /* Put SSA_NAMEs last. */
6897 if (TREE_CODE (arg1
) == SSA_NAME
)
6899 if (TREE_CODE (arg0
) == SSA_NAME
)
6902 /* Put variables last. */
6912 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6913 means A >= Y && A != MAX, but in this case we know that
6914 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6917 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6919 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6921 if (TREE_CODE (bound
) == LT_EXPR
)
6922 a
= TREE_OPERAND (bound
, 0);
6923 else if (TREE_CODE (bound
) == GT_EXPR
)
6924 a
= TREE_OPERAND (bound
, 1);
6928 typea
= TREE_TYPE (a
);
6929 if (!INTEGRAL_TYPE_P (typea
)
6930 && !POINTER_TYPE_P (typea
))
6933 if (TREE_CODE (ineq
) == LT_EXPR
)
6935 a1
= TREE_OPERAND (ineq
, 1);
6936 y
= TREE_OPERAND (ineq
, 0);
6938 else if (TREE_CODE (ineq
) == GT_EXPR
)
6940 a1
= TREE_OPERAND (ineq
, 0);
6941 y
= TREE_OPERAND (ineq
, 1);
6946 if (TREE_TYPE (a1
) != typea
)
6949 if (POINTER_TYPE_P (typea
))
6951 /* Convert the pointer types into integer before taking the difference. */
6952 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6953 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6954 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6957 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6959 if (!diff
|| !integer_onep (diff
))
6962 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6965 /* Fold a sum or difference of at least one multiplication.
6966 Returns the folded tree or NULL if no simplification could be made. */
6969 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6970 tree arg0
, tree arg1
)
6972 tree arg00
, arg01
, arg10
, arg11
;
6973 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6975 /* (A * C) +- (B * C) -> (A+-B) * C.
6976 (A * C) +- A -> A * (C+-1).
6977 We are most concerned about the case where C is a constant,
6978 but other combinations show up during loop reduction. Since
6979 it is not difficult, try all four possibilities. */
6981 if (TREE_CODE (arg0
) == MULT_EXPR
)
6983 arg00
= TREE_OPERAND (arg0
, 0);
6984 arg01
= TREE_OPERAND (arg0
, 1);
6986 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6988 arg00
= build_one_cst (type
);
6993 /* We cannot generate constant 1 for fract. */
6994 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6997 arg01
= build_one_cst (type
);
6999 if (TREE_CODE (arg1
) == MULT_EXPR
)
7001 arg10
= TREE_OPERAND (arg1
, 0);
7002 arg11
= TREE_OPERAND (arg1
, 1);
7004 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7006 arg10
= build_one_cst (type
);
7007 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7008 the purpose of this canonicalization. */
7009 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7010 && negate_expr_p (arg1
)
7011 && code
== PLUS_EXPR
)
7013 arg11
= negate_expr (arg1
);
7021 /* We cannot generate constant 1 for fract. */
7022 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7025 arg11
= build_one_cst (type
);
7029 /* Prefer factoring a common non-constant. */
7030 if (operand_equal_p (arg00
, arg10
, 0))
7031 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7032 else if (operand_equal_p (arg01
, arg11
, 0))
7033 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7034 else if (operand_equal_p (arg00
, arg11
, 0))
7035 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7036 else if (operand_equal_p (arg01
, arg10
, 0))
7037 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7039 /* No identical multiplicands; see if we can find a common
7040 power-of-two factor in non-power-of-two multiplies. This
7041 can help in multi-dimensional array access. */
7042 else if (tree_fits_shwi_p (arg01
)
7043 && tree_fits_shwi_p (arg11
))
7045 HOST_WIDE_INT int01
, int11
, tmp
;
7048 int01
= tree_to_shwi (arg01
);
7049 int11
= tree_to_shwi (arg11
);
7051 /* Move min of absolute values to int11. */
7052 if (absu_hwi (int01
) < absu_hwi (int11
))
7054 tmp
= int01
, int01
= int11
, int11
= tmp
;
7055 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7062 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
7063 /* The remainder should not be a constant, otherwise we
7064 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7065 increased the number of multiplications necessary. */
7066 && TREE_CODE (arg10
) != INTEGER_CST
)
7068 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7069 build_int_cst (TREE_TYPE (arg00
),
7074 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7081 if (! INTEGRAL_TYPE_P (type
)
7082 || TYPE_OVERFLOW_WRAPS (type
)
7083 /* We are neither factoring zero nor minus one. */
7084 || TREE_CODE (same
) == INTEGER_CST
)
7085 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7086 fold_build2_loc (loc
, code
, type
,
7087 fold_convert_loc (loc
, type
, alt0
),
7088 fold_convert_loc (loc
, type
, alt1
)),
7089 fold_convert_loc (loc
, type
, same
));
7091 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7092 same may be minus one and thus the multiplication may overflow. Perform
7093 the sum operation in an unsigned type. */
7094 tree utype
= unsigned_type_for (type
);
7095 tree tem
= fold_build2_loc (loc
, code
, utype
,
7096 fold_convert_loc (loc
, utype
, alt0
),
7097 fold_convert_loc (loc
, utype
, alt1
));
7098 /* If the sum evaluated to a constant that is not -INF the multiplication
7100 if (TREE_CODE (tem
) == INTEGER_CST
7101 && (wi::to_wide (tem
)
7102 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7103 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7104 fold_convert (type
, tem
), same
);
7106 /* Do not resort to unsigned multiplication because
7107 we lose the no-overflow property of the expression. */
7111 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7112 specified by EXPR into the buffer PTR of length LEN bytes.
7113 Return the number of bytes placed in the buffer, or zero
7117 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7119 tree type
= TREE_TYPE (expr
);
7120 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7121 int byte
, offset
, word
, words
;
7122 unsigned char value
;
7124 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7131 return MIN (len
, total_bytes
- off
);
7133 words
= total_bytes
/ UNITS_PER_WORD
;
7135 for (byte
= 0; byte
< total_bytes
; byte
++)
7137 int bitpos
= byte
* BITS_PER_UNIT
;
7138 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7140 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7142 if (total_bytes
> UNITS_PER_WORD
)
7144 word
= byte
/ UNITS_PER_WORD
;
7145 if (WORDS_BIG_ENDIAN
)
7146 word
= (words
- 1) - word
;
7147 offset
= word
* UNITS_PER_WORD
;
7148 if (BYTES_BIG_ENDIAN
)
7149 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7151 offset
+= byte
% UNITS_PER_WORD
;
7154 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7155 if (offset
>= off
&& offset
- off
< len
)
7156 ptr
[offset
- off
] = value
;
7158 return MIN (len
, total_bytes
- off
);
7162 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7163 specified by EXPR into the buffer PTR of length LEN bytes.
7164 Return the number of bytes placed in the buffer, or zero
7168 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7170 tree type
= TREE_TYPE (expr
);
7171 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7172 int total_bytes
= GET_MODE_SIZE (mode
);
7173 FIXED_VALUE_TYPE value
;
7174 tree i_value
, i_type
;
7176 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7179 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7181 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7184 value
= TREE_FIXED_CST (expr
);
7185 i_value
= double_int_to_tree (i_type
, value
.data
);
7187 return native_encode_int (i_value
, ptr
, len
, off
);
7191 /* Subroutine of native_encode_expr. Encode the REAL_CST
7192 specified by EXPR into the buffer PTR of length LEN bytes.
7193 Return the number of bytes placed in the buffer, or zero
7197 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7199 tree type
= TREE_TYPE (expr
);
7200 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7201 int byte
, offset
, word
, words
, bitpos
;
7202 unsigned char value
;
7204 /* There are always 32 bits in each long, no matter the size of
7205 the hosts long. We handle floating point representations with
7209 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7216 return MIN (len
, total_bytes
- off
);
7218 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7220 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7222 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7223 bitpos
+= BITS_PER_UNIT
)
7225 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7226 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7228 if (UNITS_PER_WORD
< 4)
7230 word
= byte
/ UNITS_PER_WORD
;
7231 if (WORDS_BIG_ENDIAN
)
7232 word
= (words
- 1) - word
;
7233 offset
= word
* UNITS_PER_WORD
;
7234 if (BYTES_BIG_ENDIAN
)
7235 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7237 offset
+= byte
% UNITS_PER_WORD
;
7242 if (BYTES_BIG_ENDIAN
)
7244 /* Reverse bytes within each long, or within the entire float
7245 if it's smaller than a long (for HFmode). */
7246 offset
= MIN (3, total_bytes
- 1) - offset
;
7247 gcc_assert (offset
>= 0);
7250 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7252 && offset
- off
< len
)
7253 ptr
[offset
- off
] = value
;
7255 return MIN (len
, total_bytes
- off
);
7258 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7259 specified by EXPR into the buffer PTR of length LEN bytes.
7260 Return the number of bytes placed in the buffer, or zero
7264 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7269 part
= TREE_REALPART (expr
);
7270 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7271 if (off
== -1 && rsize
== 0)
7273 part
= TREE_IMAGPART (expr
);
7275 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7276 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7278 if (off
== -1 && isize
!= rsize
)
7280 return rsize
+ isize
;
7284 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7285 specified by EXPR into the buffer PTR of length LEN bytes.
7286 Return the number of bytes placed in the buffer, or zero
7290 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7292 unsigned HOST_WIDE_INT i
, count
;
7297 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
7299 itype
= TREE_TYPE (TREE_TYPE (expr
));
7300 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7301 for (i
= 0; i
< count
; i
++)
7308 elem
= VECTOR_CST_ELT (expr
, i
);
7309 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7311 if ((off
== -1 && res
!= size
) || res
== 0)
7315 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
7323 /* Subroutine of native_encode_expr. Encode the STRING_CST
7324 specified by EXPR into the buffer PTR of length LEN bytes.
7325 Return the number of bytes placed in the buffer, or zero
7329 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7331 tree type
= TREE_TYPE (expr
);
7333 /* Wide-char strings are encoded in target byte-order so native
7334 encoding them is trivial. */
7335 if (BITS_PER_UNIT
!= CHAR_BIT
7336 || TREE_CODE (type
) != ARRAY_TYPE
7337 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7338 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7341 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
7342 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7348 else if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7351 if (off
< TREE_STRING_LENGTH (expr
))
7353 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7354 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7356 memset (ptr
+ written
, 0,
7357 MIN (total_bytes
- written
, len
- written
));
7360 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7361 return MIN (total_bytes
- off
, len
);
7365 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7366 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7367 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7368 anything, just do a dry run. If OFF is not -1 then start
7369 the encoding at byte offset OFF and encode at most LEN bytes.
7370 Return the number of bytes placed in the buffer, or zero upon failure. */
7373 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7375 /* We don't support starting at negative offset and -1 is special. */
7379 switch (TREE_CODE (expr
))
7382 return native_encode_int (expr
, ptr
, len
, off
);
7385 return native_encode_real (expr
, ptr
, len
, off
);
7388 return native_encode_fixed (expr
, ptr
, len
, off
);
7391 return native_encode_complex (expr
, ptr
, len
, off
);
7394 return native_encode_vector (expr
, ptr
, len
, off
);
7397 return native_encode_string (expr
, ptr
, len
, off
);
7405 /* Subroutine of native_interpret_expr. Interpret the contents of
7406 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7407 If the buffer cannot be interpreted, return NULL_TREE. */
7410 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7412 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7414 if (total_bytes
> len
7415 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7418 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7420 return wide_int_to_tree (type
, result
);
7424 /* Subroutine of native_interpret_expr. Interpret the contents of
7425 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7426 If the buffer cannot be interpreted, return NULL_TREE. */
7429 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7431 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7432 int total_bytes
= GET_MODE_SIZE (mode
);
7434 FIXED_VALUE_TYPE fixed_value
;
7436 if (total_bytes
> len
7437 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7440 result
= double_int::from_buffer (ptr
, total_bytes
);
7441 fixed_value
= fixed_from_double_int (result
, mode
);
7443 return build_fixed (type
, fixed_value
);
7447 /* Subroutine of native_interpret_expr. Interpret the contents of
7448 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7449 If the buffer cannot be interpreted, return NULL_TREE. */
7452 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7454 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
7455 int total_bytes
= GET_MODE_SIZE (mode
);
7456 unsigned char value
;
7457 /* There are always 32 bits in each long, no matter the size of
7458 the hosts long. We handle floating point representations with
7463 if (total_bytes
> len
|| total_bytes
> 24)
7465 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7467 memset (tmp
, 0, sizeof (tmp
));
7468 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7469 bitpos
+= BITS_PER_UNIT
)
7471 /* Both OFFSET and BYTE index within a long;
7472 bitpos indexes the whole float. */
7473 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7474 if (UNITS_PER_WORD
< 4)
7476 int word
= byte
/ UNITS_PER_WORD
;
7477 if (WORDS_BIG_ENDIAN
)
7478 word
= (words
- 1) - word
;
7479 offset
= word
* UNITS_PER_WORD
;
7480 if (BYTES_BIG_ENDIAN
)
7481 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7483 offset
+= byte
% UNITS_PER_WORD
;
7488 if (BYTES_BIG_ENDIAN
)
7490 /* Reverse bytes within each long, or within the entire float
7491 if it's smaller than a long (for HFmode). */
7492 offset
= MIN (3, total_bytes
- 1) - offset
;
7493 gcc_assert (offset
>= 0);
7496 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7498 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7501 real_from_target (&r
, tmp
, mode
);
7502 return build_real (type
, r
);
7506 /* Subroutine of native_interpret_expr. Interpret the contents of
7507 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7508 If the buffer cannot be interpreted, return NULL_TREE. */
7511 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7513 tree etype
, rpart
, ipart
;
7516 etype
= TREE_TYPE (type
);
7517 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
7520 rpart
= native_interpret_expr (etype
, ptr
, size
);
7523 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7526 return build_complex (type
, rpart
, ipart
);
7530 /* Subroutine of native_interpret_expr. Interpret the contents of
7531 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7532 If the buffer cannot be interpreted, return NULL_TREE. */
7535 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
7538 unsigned int i
, size
;
7539 unsigned HOST_WIDE_INT count
;
7541 etype
= TREE_TYPE (type
);
7542 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
7543 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&count
)
7544 || size
* count
> len
)
7547 tree_vector_builder
elements (type
, count
, 1);
7548 for (i
= 0; i
< count
; ++i
)
7550 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7553 elements
.quick_push (elem
);
7555 return elements
.build ();
7559 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7560 the buffer PTR of length LEN as a constant of type TYPE. For
7561 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7562 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7563 return NULL_TREE. */
7566 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7568 switch (TREE_CODE (type
))
7574 case REFERENCE_TYPE
:
7575 return native_interpret_int (type
, ptr
, len
);
7578 return native_interpret_real (type
, ptr
, len
);
7580 case FIXED_POINT_TYPE
:
7581 return native_interpret_fixed (type
, ptr
, len
);
7584 return native_interpret_complex (type
, ptr
, len
);
7587 return native_interpret_vector (type
, ptr
, len
);
7594 /* Returns true if we can interpret the contents of a native encoding
7598 can_native_interpret_type_p (tree type
)
7600 switch (TREE_CODE (type
))
7606 case REFERENCE_TYPE
:
7607 case FIXED_POINT_TYPE
:
7618 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7619 TYPE at compile-time. If we're unable to perform the conversion
7620 return NULL_TREE. */
7623 fold_view_convert_expr (tree type
, tree expr
)
7625 /* We support up to 512-bit values (for V8DFmode). */
7626 unsigned char buffer
[64];
7629 /* Check that the host and target are sane. */
7630 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7633 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7637 return native_interpret_expr (type
, buffer
, len
);
7640 /* Build an expression for the address of T. Folds away INDIRECT_REF
7641 to avoid confusing the gimplify process. */
7644 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7646 /* The size of the object is not relevant when talking about its address. */
7647 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7648 t
= TREE_OPERAND (t
, 0);
7650 if (TREE_CODE (t
) == INDIRECT_REF
)
7652 t
= TREE_OPERAND (t
, 0);
7654 if (TREE_TYPE (t
) != ptrtype
)
7655 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7657 else if (TREE_CODE (t
) == MEM_REF
7658 && integer_zerop (TREE_OPERAND (t
, 1)))
7659 return TREE_OPERAND (t
, 0);
7660 else if (TREE_CODE (t
) == MEM_REF
7661 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7662 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7663 TREE_OPERAND (t
, 0),
7664 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7665 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7667 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7669 if (TREE_TYPE (t
) != ptrtype
)
7670 t
= fold_convert_loc (loc
, ptrtype
, t
);
7673 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7678 /* Build an expression for the address of T. */
7681 build_fold_addr_expr_loc (location_t loc
, tree t
)
7683 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7685 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7688 /* Fold a unary expression of code CODE and type TYPE with operand
7689 OP0. Return the folded expression if folding is successful.
7690 Otherwise, return NULL_TREE. */
7693 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7697 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7699 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7700 && TREE_CODE_LENGTH (code
) == 1);
7705 if (CONVERT_EXPR_CODE_P (code
)
7706 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7708 /* Don't use STRIP_NOPS, because signedness of argument type
7710 STRIP_SIGN_NOPS (arg0
);
7714 /* Strip any conversions that don't change the mode. This
7715 is safe for every expression, except for a comparison
7716 expression because its signedness is derived from its
7719 Note that this is done as an internal manipulation within
7720 the constant folder, in order to find the simplest
7721 representation of the arguments so that their form can be
7722 studied. In any cases, the appropriate type conversions
7723 should be put back in the tree that will get out of the
7728 if (CONSTANT_CLASS_P (arg0
))
7730 tree tem
= const_unop (code
, type
, arg0
);
7733 if (TREE_TYPE (tem
) != type
)
7734 tem
= fold_convert_loc (loc
, type
, tem
);
7740 tem
= generic_simplify (loc
, code
, type
, op0
);
7744 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7746 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7747 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7748 fold_build1_loc (loc
, code
, type
,
7749 fold_convert_loc (loc
, TREE_TYPE (op0
),
7750 TREE_OPERAND (arg0
, 1))));
7751 else if (TREE_CODE (arg0
) == COND_EXPR
)
7753 tree arg01
= TREE_OPERAND (arg0
, 1);
7754 tree arg02
= TREE_OPERAND (arg0
, 2);
7755 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7756 arg01
= fold_build1_loc (loc
, code
, type
,
7757 fold_convert_loc (loc
,
7758 TREE_TYPE (op0
), arg01
));
7759 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7760 arg02
= fold_build1_loc (loc
, code
, type
,
7761 fold_convert_loc (loc
,
7762 TREE_TYPE (op0
), arg02
));
7763 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7766 /* If this was a conversion, and all we did was to move into
7767 inside the COND_EXPR, bring it back out. But leave it if
7768 it is a conversion from integer to integer and the
7769 result precision is no wider than a word since such a
7770 conversion is cheap and may be optimized away by combine,
7771 while it couldn't if it were outside the COND_EXPR. Then return
7772 so we don't get into an infinite recursion loop taking the
7773 conversion out and then back in. */
7775 if ((CONVERT_EXPR_CODE_P (code
)
7776 || code
== NON_LVALUE_EXPR
)
7777 && TREE_CODE (tem
) == COND_EXPR
7778 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7779 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7780 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7781 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7782 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7783 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7784 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7786 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7787 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7788 || flag_syntax_only
))
7789 tem
= build1_loc (loc
, code
, type
,
7791 TREE_TYPE (TREE_OPERAND
7792 (TREE_OPERAND (tem
, 1), 0)),
7793 TREE_OPERAND (tem
, 0),
7794 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7795 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7803 case NON_LVALUE_EXPR
:
7804 if (!maybe_lvalue_p (op0
))
7805 return fold_convert_loc (loc
, type
, op0
);
7810 case FIX_TRUNC_EXPR
:
7811 if (COMPARISON_CLASS_P (op0
))
7813 /* If we have (type) (a CMP b) and type is an integral type, return
7814 new expression involving the new type. Canonicalize
7815 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7817 Do not fold the result as that would not simplify further, also
7818 folding again results in recursions. */
7819 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7820 return build2_loc (loc
, TREE_CODE (op0
), type
,
7821 TREE_OPERAND (op0
, 0),
7822 TREE_OPERAND (op0
, 1));
7823 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7824 && TREE_CODE (type
) != VECTOR_TYPE
)
7825 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7826 constant_boolean_node (true, type
),
7827 constant_boolean_node (false, type
));
7830 /* Handle (T *)&A.B.C for A being of type T and B and C
7831 living at offset zero. This occurs frequently in
7832 C++ upcasting and then accessing the base. */
7833 if (TREE_CODE (op0
) == ADDR_EXPR
7834 && POINTER_TYPE_P (type
)
7835 && handled_component_p (TREE_OPERAND (op0
, 0)))
7837 poly_int64 bitsize
, bitpos
;
7840 int unsignedp
, reversep
, volatilep
;
7842 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
7843 &offset
, &mode
, &unsignedp
, &reversep
,
7845 /* If the reference was to a (constant) zero offset, we can use
7846 the address of the base if it has the same base type
7847 as the result type and the pointer type is unqualified. */
7849 && known_eq (bitpos
, 0)
7850 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7851 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7852 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7853 return fold_convert_loc (loc
, type
,
7854 build_fold_addr_expr_loc (loc
, base
));
7857 if (TREE_CODE (op0
) == MODIFY_EXPR
7858 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7859 /* Detect assigning a bitfield. */
7860 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7862 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7864 /* Don't leave an assignment inside a conversion
7865 unless assigning a bitfield. */
7866 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7867 /* First do the assignment, then return converted constant. */
7868 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7869 TREE_NO_WARNING (tem
) = 1;
7870 TREE_USED (tem
) = 1;
7874 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7875 constants (if x has signed type, the sign bit cannot be set
7876 in c). This folds extension into the BIT_AND_EXPR.
7877 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7878 very likely don't have maximal range for their precision and this
7879 transformation effectively doesn't preserve non-maximal ranges. */
7880 if (TREE_CODE (type
) == INTEGER_TYPE
7881 && TREE_CODE (op0
) == BIT_AND_EXPR
7882 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7884 tree and_expr
= op0
;
7885 tree and0
= TREE_OPERAND (and_expr
, 0);
7886 tree and1
= TREE_OPERAND (and_expr
, 1);
7889 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7890 || (TYPE_PRECISION (type
)
7891 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7893 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7894 <= HOST_BITS_PER_WIDE_INT
7895 && tree_fits_uhwi_p (and1
))
7897 unsigned HOST_WIDE_INT cst
;
7899 cst
= tree_to_uhwi (and1
);
7900 cst
&= HOST_WIDE_INT_M1U
7901 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7902 change
= (cst
== 0);
7904 && !flag_syntax_only
7905 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
7908 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7909 and0
= fold_convert_loc (loc
, uns
, and0
);
7910 and1
= fold_convert_loc (loc
, uns
, and1
);
7915 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7916 TREE_OVERFLOW (and1
));
7917 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7918 fold_convert_loc (loc
, type
, and0
), tem
);
7922 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7923 cast (T1)X will fold away. We assume that this happens when X itself
7925 if (POINTER_TYPE_P (type
)
7926 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7927 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
7929 tree arg00
= TREE_OPERAND (arg0
, 0);
7930 tree arg01
= TREE_OPERAND (arg0
, 1);
7932 return fold_build_pointer_plus_loc
7933 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7936 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7937 of the same precision, and X is an integer type not narrower than
7938 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7939 if (INTEGRAL_TYPE_P (type
)
7940 && TREE_CODE (op0
) == BIT_NOT_EXPR
7941 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7942 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7943 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7945 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7946 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7947 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7948 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7949 fold_convert_loc (loc
, type
, tem
));
7952 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7953 type of X and Y (integer types only). */
7954 if (INTEGRAL_TYPE_P (type
)
7955 && TREE_CODE (op0
) == MULT_EXPR
7956 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7957 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7959 /* Be careful not to introduce new overflows. */
7961 if (TYPE_OVERFLOW_WRAPS (type
))
7964 mult_type
= unsigned_type_for (type
);
7966 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7968 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7969 fold_convert_loc (loc
, mult_type
,
7970 TREE_OPERAND (op0
, 0)),
7971 fold_convert_loc (loc
, mult_type
,
7972 TREE_OPERAND (op0
, 1)));
7973 return fold_convert_loc (loc
, type
, tem
);
7979 case VIEW_CONVERT_EXPR
:
7980 if (TREE_CODE (op0
) == MEM_REF
)
7982 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
7983 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
7984 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
7985 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7986 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
7993 tem
= fold_negate_expr (loc
, arg0
);
7995 return fold_convert_loc (loc
, type
, tem
);
7999 /* Convert fabs((double)float) into (double)fabsf(float). */
8000 if (TREE_CODE (arg0
) == NOP_EXPR
8001 && TREE_CODE (type
) == REAL_TYPE
)
8003 tree targ0
= strip_float_extensions (arg0
);
8005 return fold_convert_loc (loc
, type
,
8006 fold_build1_loc (loc
, ABS_EXPR
,
8013 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8014 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8015 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8016 fold_convert_loc (loc
, type
,
8017 TREE_OPERAND (arg0
, 0)))))
8018 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
8019 fold_convert_loc (loc
, type
,
8020 TREE_OPERAND (arg0
, 1)));
8021 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8022 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8023 fold_convert_loc (loc
, type
,
8024 TREE_OPERAND (arg0
, 1)))))
8025 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
8026 fold_convert_loc (loc
, type
,
8027 TREE_OPERAND (arg0
, 0)), tem
);
8031 case TRUTH_NOT_EXPR
:
8032 /* Note that the operand of this must be an int
8033 and its values must be 0 or 1.
8034 ("true" is a fixed value perhaps depending on the language,
8035 but we don't handle values other than 1 correctly yet.) */
8036 tem
= fold_truth_not_expr (loc
, arg0
);
8039 return fold_convert_loc (loc
, type
, tem
);
8042 /* Fold *&X to X if X is an lvalue. */
8043 if (TREE_CODE (op0
) == ADDR_EXPR
)
8045 tree op00
= TREE_OPERAND (op0
, 0);
8047 || TREE_CODE (op00
) == PARM_DECL
8048 || TREE_CODE (op00
) == RESULT_DECL
)
8049 && !TREE_READONLY (op00
))
8056 } /* switch (code) */
8060 /* If the operation was a conversion do _not_ mark a resulting constant
8061 with TREE_OVERFLOW if the original constant was not. These conversions
8062 have implementation defined behavior and retaining the TREE_OVERFLOW
8063 flag here would confuse later passes such as VRP. */
8065 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
8066 tree type
, tree op0
)
8068 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
8070 && TREE_CODE (res
) == INTEGER_CST
8071 && TREE_CODE (op0
) == INTEGER_CST
8072 && CONVERT_EXPR_CODE_P (code
))
8073 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
8078 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8079 operands OP0 and OP1. LOC is the location of the resulting expression.
8080 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8081 Return the folded expression if folding is successful. Otherwise,
8082 return NULL_TREE. */
8084 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
8085 tree arg0
, tree arg1
, tree op0
, tree op1
)
8089 /* We only do these simplifications if we are optimizing. */
8093 /* Check for things like (A || B) && (A || C). We can convert this
8094 to A || (B && C). Note that either operator can be any of the four
8095 truth and/or operations and the transformation will still be
8096 valid. Also note that we only care about order for the
8097 ANDIF and ORIF operators. If B contains side effects, this
8098 might change the truth-value of A. */
8099 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8100 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
8101 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
8102 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
8103 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
8104 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
8106 tree a00
= TREE_OPERAND (arg0
, 0);
8107 tree a01
= TREE_OPERAND (arg0
, 1);
8108 tree a10
= TREE_OPERAND (arg1
, 0);
8109 tree a11
= TREE_OPERAND (arg1
, 1);
8110 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
8111 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
8112 && (code
== TRUTH_AND_EXPR
8113 || code
== TRUTH_OR_EXPR
));
8115 if (operand_equal_p (a00
, a10
, 0))
8116 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8117 fold_build2_loc (loc
, code
, type
, a01
, a11
));
8118 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
8119 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8120 fold_build2_loc (loc
, code
, type
, a01
, a10
));
8121 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
8122 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
8123 fold_build2_loc (loc
, code
, type
, a00
, a11
));
8125 /* This case if tricky because we must either have commutative
8126 operators or else A10 must not have side-effects. */
8128 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
8129 && operand_equal_p (a01
, a11
, 0))
8130 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
8131 fold_build2_loc (loc
, code
, type
, a00
, a10
),
8135 /* See if we can build a range comparison. */
8136 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
8139 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
8140 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8142 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8144 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8147 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8148 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8150 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8152 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8155 /* Check for the possibility of merging component references. If our
8156 lhs is another similar operation, try to merge its rhs with our
8157 rhs. Then try to merge our lhs and rhs. */
8158 if (TREE_CODE (arg0
) == code
8159 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
8160 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
8161 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8163 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8166 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8167 && !flag_sanitize_coverage
8168 && (code
== TRUTH_AND_EXPR
8169 || code
== TRUTH_ANDIF_EXPR
8170 || code
== TRUTH_OR_EXPR
8171 || code
== TRUTH_ORIF_EXPR
))
8173 enum tree_code ncode
, icode
;
8175 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8176 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8177 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8179 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8180 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8181 We don't want to pack more than two leafs to a non-IF AND/OR
8183 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8184 equal to IF-CODE, then we don't want to add right-hand operand.
8185 If the inner right-hand side of left-hand operand has
8186 side-effects, or isn't simple, then we can't add to it,
8187 as otherwise we might destroy if-sequence. */
8188 if (TREE_CODE (arg0
) == icode
8189 && simple_operand_p_2 (arg1
)
8190 /* Needed for sequence points to handle trappings, and
8192 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8194 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8196 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8199 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8200 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8201 else if (TREE_CODE (arg1
) == icode
8202 && simple_operand_p_2 (arg0
)
8203 /* Needed for sequence points to handle trappings, and
8205 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8207 tem
= fold_build2_loc (loc
, ncode
, type
,
8208 arg0
, TREE_OPERAND (arg1
, 0));
8209 return fold_build2_loc (loc
, icode
, type
, tem
,
8210 TREE_OPERAND (arg1
, 1));
8212 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8214 For sequence point consistancy, we need to check for trapping,
8215 and side-effects. */
8216 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8217 && simple_operand_p_2 (arg1
))
8218 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8224 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8225 by changing CODE to reduce the magnitude of constants involved in
8226 ARG0 of the comparison.
8227 Returns a canonicalized comparison tree if a simplification was
8228 possible, otherwise returns NULL_TREE.
8229 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8230 valid if signed overflow is undefined. */
8233 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8234 tree arg0
, tree arg1
,
8235 bool *strict_overflow_p
)
8237 enum tree_code code0
= TREE_CODE (arg0
);
8238 tree t
, cst0
= NULL_TREE
;
8241 /* Match A +- CST code arg1. We can change this only if overflow
8243 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8244 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8245 /* In principle pointers also have undefined overflow behavior,
8246 but that causes problems elsewhere. */
8247 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8248 && (code0
== MINUS_EXPR
8249 || code0
== PLUS_EXPR
)
8250 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8253 /* Identify the constant in arg0 and its sign. */
8254 cst0
= TREE_OPERAND (arg0
, 1);
8255 sgn0
= tree_int_cst_sgn (cst0
);
8257 /* Overflowed constants and zero will cause problems. */
8258 if (integer_zerop (cst0
)
8259 || TREE_OVERFLOW (cst0
))
8262 /* See if we can reduce the magnitude of the constant in
8263 arg0 by changing the comparison code. */
8264 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8266 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8268 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8269 else if (code
== GT_EXPR
8270 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8272 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8273 else if (code
== LE_EXPR
8274 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8276 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8277 else if (code
== GE_EXPR
8278 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8282 *strict_overflow_p
= true;
8284 /* Now build the constant reduced in magnitude. But not if that
8285 would produce one outside of its types range. */
8286 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8288 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8289 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8291 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8292 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8295 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8296 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8297 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8298 t
= fold_convert (TREE_TYPE (arg1
), t
);
8300 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8303 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8304 overflow further. Try to decrease the magnitude of constants involved
8305 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8306 and put sole constants at the second argument position.
8307 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8310 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8311 tree arg0
, tree arg1
)
8314 bool strict_overflow_p
;
8315 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8316 "when reducing constant in comparison");
8318 /* Try canonicalization by simplifying arg0. */
8319 strict_overflow_p
= false;
8320 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8321 &strict_overflow_p
);
8324 if (strict_overflow_p
)
8325 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8329 /* Try canonicalization by simplifying arg1 using the swapped
8331 code
= swap_tree_comparison (code
);
8332 strict_overflow_p
= false;
8333 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8334 &strict_overflow_p
);
8335 if (t
&& strict_overflow_p
)
8336 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8340 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8341 space. This is used to avoid issuing overflow warnings for
8342 expressions like &p->x which can not wrap. */
8345 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
8347 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8350 if (maybe_lt (bitpos
, 0))
8353 poly_wide_int wi_offset
;
8354 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8355 if (offset
== NULL_TREE
)
8356 wi_offset
= wi::zero (precision
);
8357 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
8360 wi_offset
= wi::to_poly_wide (offset
);
8363 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
8365 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8369 poly_uint64 total_hwi
, size
;
8370 if (!total
.to_uhwi (&total_hwi
)
8371 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
8373 || known_eq (size
, 0U))
8376 if (known_le (total_hwi
, size
))
8379 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8381 if (TREE_CODE (base
) == ADDR_EXPR
8382 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
8384 && maybe_ne (size
, 0U)
8385 && known_le (total_hwi
, size
))
8391 /* Return a positive integer when the symbol DECL is known to have
8392 a nonzero address, zero when it's known not to (e.g., it's a weak
8393 symbol), and a negative integer when the symbol is not yet in the
8394 symbol table and so whether or not its address is zero is unknown.
8395 For function local objects always return positive integer. */
8397 maybe_nonzero_address (tree decl
)
8399 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
8400 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
8401 return symbol
->nonzero_address ();
8403 /* Function local objects are never NULL. */
8405 && (DECL_CONTEXT (decl
)
8406 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
8407 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
8413 /* Subroutine of fold_binary. This routine performs all of the
8414 transformations that are common to the equality/inequality
8415 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8416 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8417 fold_binary should call fold_binary. Fold a comparison with
8418 tree code CODE and type TYPE with operands OP0 and OP1. Return
8419 the folded comparison or NULL_TREE. */
8422 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8425 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8426 tree arg0
, arg1
, tem
;
8431 STRIP_SIGN_NOPS (arg0
);
8432 STRIP_SIGN_NOPS (arg1
);
8434 /* For comparisons of pointers we can decompose it to a compile time
8435 comparison of the base objects and the offsets into the object.
8436 This requires at least one operand being an ADDR_EXPR or a
8437 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8438 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8439 && (TREE_CODE (arg0
) == ADDR_EXPR
8440 || TREE_CODE (arg1
) == ADDR_EXPR
8441 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8442 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8444 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8445 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
8447 int volatilep
, reversep
, unsignedp
;
8448 bool indirect_base0
= false, indirect_base1
= false;
8450 /* Get base and offset for the access. Strip ADDR_EXPR for
8451 get_inner_reference, but put it back by stripping INDIRECT_REF
8452 off the base object if possible. indirect_baseN will be true
8453 if baseN is not an address but refers to the object itself. */
8455 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8458 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8459 &bitsize
, &bitpos0
, &offset0
, &mode
,
8460 &unsignedp
, &reversep
, &volatilep
);
8461 if (TREE_CODE (base0
) == INDIRECT_REF
)
8462 base0
= TREE_OPERAND (base0
, 0);
8464 indirect_base0
= true;
8466 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8468 base0
= TREE_OPERAND (arg0
, 0);
8469 STRIP_SIGN_NOPS (base0
);
8470 if (TREE_CODE (base0
) == ADDR_EXPR
)
8473 = get_inner_reference (TREE_OPERAND (base0
, 0),
8474 &bitsize
, &bitpos0
, &offset0
, &mode
,
8475 &unsignedp
, &reversep
, &volatilep
);
8476 if (TREE_CODE (base0
) == INDIRECT_REF
)
8477 base0
= TREE_OPERAND (base0
, 0);
8479 indirect_base0
= true;
8481 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8482 offset0
= TREE_OPERAND (arg0
, 1);
8484 offset0
= size_binop (PLUS_EXPR
, offset0
,
8485 TREE_OPERAND (arg0
, 1));
8486 if (poly_int_tree_p (offset0
))
8488 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
8489 TYPE_PRECISION (sizetype
));
8490 tem
<<= LOG2_BITS_PER_UNIT
;
8492 if (tem
.to_shwi (&bitpos0
))
8493 offset0
= NULL_TREE
;
8498 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8501 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8502 &bitsize
, &bitpos1
, &offset1
, &mode
,
8503 &unsignedp
, &reversep
, &volatilep
);
8504 if (TREE_CODE (base1
) == INDIRECT_REF
)
8505 base1
= TREE_OPERAND (base1
, 0);
8507 indirect_base1
= true;
8509 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8511 base1
= TREE_OPERAND (arg1
, 0);
8512 STRIP_SIGN_NOPS (base1
);
8513 if (TREE_CODE (base1
) == ADDR_EXPR
)
8516 = get_inner_reference (TREE_OPERAND (base1
, 0),
8517 &bitsize
, &bitpos1
, &offset1
, &mode
,
8518 &unsignedp
, &reversep
, &volatilep
);
8519 if (TREE_CODE (base1
) == INDIRECT_REF
)
8520 base1
= TREE_OPERAND (base1
, 0);
8522 indirect_base1
= true;
8524 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8525 offset1
= TREE_OPERAND (arg1
, 1);
8527 offset1
= size_binop (PLUS_EXPR
, offset1
,
8528 TREE_OPERAND (arg1
, 1));
8529 if (poly_int_tree_p (offset1
))
8531 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
8532 TYPE_PRECISION (sizetype
));
8533 tem
<<= LOG2_BITS_PER_UNIT
;
8535 if (tem
.to_shwi (&bitpos1
))
8536 offset1
= NULL_TREE
;
8540 /* If we have equivalent bases we might be able to simplify. */
8541 if (indirect_base0
== indirect_base1
8542 && operand_equal_p (base0
, base1
,
8543 indirect_base0
? OEP_ADDRESS_OF
: 0))
8545 /* We can fold this expression to a constant if the non-constant
8546 offset parts are equal. */
8547 if ((offset0
== offset1
8548 || (offset0
&& offset1
8549 && operand_equal_p (offset0
, offset1
, 0)))
8552 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8553 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
8556 && maybe_ne (bitpos0
, bitpos1
)
8557 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8558 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8559 fold_overflow_warning (("assuming pointer wraparound does not "
8560 "occur when comparing P +- C1 with "
8562 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8567 if (known_eq (bitpos0
, bitpos1
))
8568 return constant_boolean_node (true, type
);
8569 if (known_ne (bitpos0
, bitpos1
))
8570 return constant_boolean_node (false, type
);
8573 if (known_ne (bitpos0
, bitpos1
))
8574 return constant_boolean_node (true, type
);
8575 if (known_eq (bitpos0
, bitpos1
))
8576 return constant_boolean_node (false, type
);
8579 if (known_lt (bitpos0
, bitpos1
))
8580 return constant_boolean_node (true, type
);
8581 if (known_ge (bitpos0
, bitpos1
))
8582 return constant_boolean_node (false, type
);
8585 if (known_le (bitpos0
, bitpos1
))
8586 return constant_boolean_node (true, type
);
8587 if (known_gt (bitpos0
, bitpos1
))
8588 return constant_boolean_node (false, type
);
8591 if (known_ge (bitpos0
, bitpos1
))
8592 return constant_boolean_node (true, type
);
8593 if (known_lt (bitpos0
, bitpos1
))
8594 return constant_boolean_node (false, type
);
8597 if (known_gt (bitpos0
, bitpos1
))
8598 return constant_boolean_node (true, type
);
8599 if (known_le (bitpos0
, bitpos1
))
8600 return constant_boolean_node (false, type
);
8605 /* We can simplify the comparison to a comparison of the variable
8606 offset parts if the constant offset parts are equal.
8607 Be careful to use signed sizetype here because otherwise we
8608 mess with array offsets in the wrong way. This is possible
8609 because pointer arithmetic is restricted to retain within an
8610 object and overflow on pointer differences is undefined as of
8611 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8612 else if (known_eq (bitpos0
, bitpos1
)
8615 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8616 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
8618 /* By converting to signed sizetype we cover middle-end pointer
8619 arithmetic which operates on unsigned pointer types of size
8620 type size and ARRAY_REF offsets which are properly sign or
8621 zero extended from their type in case it is narrower than
8623 if (offset0
== NULL_TREE
)
8624 offset0
= build_int_cst (ssizetype
, 0);
8626 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8627 if (offset1
== NULL_TREE
)
8628 offset1
= build_int_cst (ssizetype
, 0);
8630 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8633 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8634 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8635 fold_overflow_warning (("assuming pointer wraparound does not "
8636 "occur when comparing P +- C1 with "
8638 WARN_STRICT_OVERFLOW_COMPARISON
);
8640 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8643 /* For equal offsets we can simplify to a comparison of the
8645 else if (known_eq (bitpos0
, bitpos1
)
8647 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8649 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8650 && ((offset0
== offset1
)
8651 || (offset0
&& offset1
8652 && operand_equal_p (offset0
, offset1
, 0))))
8655 base0
= build_fold_addr_expr_loc (loc
, base0
);
8657 base1
= build_fold_addr_expr_loc (loc
, base1
);
8658 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8660 /* Comparison between an ordinary (non-weak) symbol and a null
8661 pointer can be eliminated since such symbols must have a non
8662 null address. In C, relational expressions between pointers
8663 to objects and null pointers are undefined. The results
8664 below follow the C++ rules with the additional property that
8665 every object pointer compares greater than a null pointer.
8667 else if (((DECL_P (base0
)
8668 && maybe_nonzero_address (base0
) > 0
8669 /* Avoid folding references to struct members at offset 0 to
8670 prevent tests like '&ptr->firstmember == 0' from getting
8671 eliminated. When ptr is null, although the -> expression
8672 is strictly speaking invalid, GCC retains it as a matter
8673 of QoI. See PR c/44555. */
8674 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
8675 || CONSTANT_CLASS_P (base0
))
8677 /* The caller guarantees that when one of the arguments is
8678 constant (i.e., null in this case) it is second. */
8679 && integer_zerop (arg1
))
8686 return constant_boolean_node (false, type
);
8690 return constant_boolean_node (true, type
);
8697 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8698 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8699 the resulting offset is smaller in absolute value than the
8700 original one and has the same sign. */
8701 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8702 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8703 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8704 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8705 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8706 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8707 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8708 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8710 tree const1
= TREE_OPERAND (arg0
, 1);
8711 tree const2
= TREE_OPERAND (arg1
, 1);
8712 tree variable1
= TREE_OPERAND (arg0
, 0);
8713 tree variable2
= TREE_OPERAND (arg1
, 0);
8715 const char * const warnmsg
= G_("assuming signed overflow does not "
8716 "occur when combining constants around "
8719 /* Put the constant on the side where it doesn't overflow and is
8720 of lower absolute value and of same sign than before. */
8721 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8722 ? MINUS_EXPR
: PLUS_EXPR
,
8724 if (!TREE_OVERFLOW (cst
)
8725 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8726 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8728 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8729 return fold_build2_loc (loc
, code
, type
,
8731 fold_build2_loc (loc
, TREE_CODE (arg1
),
8736 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8737 ? MINUS_EXPR
: PLUS_EXPR
,
8739 if (!TREE_OVERFLOW (cst
)
8740 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8741 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8743 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8744 return fold_build2_loc (loc
, code
, type
,
8745 fold_build2_loc (loc
, TREE_CODE (arg0
),
8752 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8756 /* If we are comparing an expression that just has comparisons
8757 of two integer values, arithmetic expressions of those comparisons,
8758 and constants, we can simplify it. There are only three cases
8759 to check: the two values can either be equal, the first can be
8760 greater, or the second can be greater. Fold the expression for
8761 those three values. Since each value must be 0 or 1, we have
8762 eight possibilities, each of which corresponds to the constant 0
8763 or 1 or one of the six possible comparisons.
8765 This handles common cases like (a > b) == 0 but also handles
8766 expressions like ((x > y) - (y > x)) > 0, which supposedly
8767 occur in macroized code. */
8769 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8771 tree cval1
= 0, cval2
= 0;
8773 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
8774 /* Don't handle degenerate cases here; they should already
8775 have been handled anyway. */
8776 && cval1
!= 0 && cval2
!= 0
8777 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8778 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8779 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8780 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8781 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8782 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8783 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8785 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8786 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8788 /* We can't just pass T to eval_subst in case cval1 or cval2
8789 was the same as ARG1. */
8792 = fold_build2_loc (loc
, code
, type
,
8793 eval_subst (loc
, arg0
, cval1
, maxval
,
8797 = fold_build2_loc (loc
, code
, type
,
8798 eval_subst (loc
, arg0
, cval1
, maxval
,
8802 = fold_build2_loc (loc
, code
, type
,
8803 eval_subst (loc
, arg0
, cval1
, minval
,
8807 /* All three of these results should be 0 or 1. Confirm they are.
8808 Then use those values to select the proper code to use. */
8810 if (TREE_CODE (high_result
) == INTEGER_CST
8811 && TREE_CODE (equal_result
) == INTEGER_CST
8812 && TREE_CODE (low_result
) == INTEGER_CST
)
8814 /* Make a 3-bit mask with the high-order bit being the
8815 value for `>', the next for '=', and the low for '<'. */
8816 switch ((integer_onep (high_result
) * 4)
8817 + (integer_onep (equal_result
) * 2)
8818 + integer_onep (low_result
))
8822 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8843 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8846 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8855 /* Subroutine of fold_binary. Optimize complex multiplications of the
8856 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8857 argument EXPR represents the expression "z" of type TYPE. */
8860 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8862 tree itype
= TREE_TYPE (type
);
8863 tree rpart
, ipart
, tem
;
8865 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8867 rpart
= TREE_OPERAND (expr
, 0);
8868 ipart
= TREE_OPERAND (expr
, 1);
8870 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8872 rpart
= TREE_REALPART (expr
);
8873 ipart
= TREE_IMAGPART (expr
);
8877 expr
= save_expr (expr
);
8878 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8879 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8882 rpart
= save_expr (rpart
);
8883 ipart
= save_expr (ipart
);
8884 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8885 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8886 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8887 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8888 build_zero_cst (itype
));
8892 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8893 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8894 true if successful. */
8897 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
8899 unsigned HOST_WIDE_INT i
, nunits
;
8901 if (TREE_CODE (arg
) == VECTOR_CST
8902 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
8904 for (i
= 0; i
< nunits
; ++i
)
8905 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8907 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8909 constructor_elt
*elt
;
8911 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8912 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8915 elts
[i
] = elt
->value
;
8919 for (; i
< nelts
; i
++)
8921 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8925 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8926 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8927 NULL_TREE otherwise. */
8930 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
8933 unsigned HOST_WIDE_INT nelts
;
8934 bool need_ctor
= false;
8936 if (!sel
.length ().is_constant (&nelts
))
8938 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), nelts
)
8939 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)), nelts
)
8940 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)), nelts
));
8941 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8942 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8945 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
8946 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
8947 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
8950 tree_vector_builder
out_elts (type
, nelts
, 1);
8951 for (i
= 0; i
< nelts
; i
++)
8953 HOST_WIDE_INT index
;
8954 if (!sel
[i
].is_constant (&index
))
8956 if (!CONSTANT_CLASS_P (in_elts
[index
]))
8958 out_elts
.quick_push (unshare_expr (in_elts
[index
]));
8963 vec
<constructor_elt
, va_gc
> *v
;
8964 vec_alloc (v
, nelts
);
8965 for (i
= 0; i
< nelts
; i
++)
8966 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
8967 return build_constructor (type
, v
);
8970 return out_elts
.build ();
8973 /* Try to fold a pointer difference of type TYPE two address expressions of
8974 array references AREF0 and AREF1 using location LOC. Return a
8975 simplified expression for the difference or NULL_TREE. */
8978 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8979 tree aref0
, tree aref1
,
8980 bool use_pointer_diff
)
8982 tree base0
= TREE_OPERAND (aref0
, 0);
8983 tree base1
= TREE_OPERAND (aref1
, 0);
8984 tree base_offset
= build_int_cst (type
, 0);
8986 /* If the bases are array references as well, recurse. If the bases
8987 are pointer indirections compute the difference of the pointers.
8988 If the bases are equal, we are set. */
8989 if ((TREE_CODE (base0
) == ARRAY_REF
8990 && TREE_CODE (base1
) == ARRAY_REF
8992 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
8994 || (INDIRECT_REF_P (base0
)
8995 && INDIRECT_REF_P (base1
)
8998 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
8999 TREE_OPERAND (base0
, 0),
9000 TREE_OPERAND (base1
, 0))
9001 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
9003 TREE_OPERAND (base0
, 0)),
9005 TREE_OPERAND (base1
, 0)))))
9006 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
9008 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
9009 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
9010 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
9011 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
9012 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9014 fold_build2_loc (loc
, MULT_EXPR
, type
,
9020 /* If the real or vector real constant CST of type TYPE has an exact
9021 inverse, return it, else return NULL. */
9024 exact_inverse (tree type
, tree cst
)
9030 switch (TREE_CODE (cst
))
9033 r
= TREE_REAL_CST (cst
);
9035 if (exact_real_inverse (TYPE_MODE (type
), &r
))
9036 return build_real (type
, r
);
9042 unit_type
= TREE_TYPE (type
);
9043 mode
= TYPE_MODE (unit_type
);
9045 tree_vector_builder elts
;
9046 if (!elts
.new_unary_operation (type
, cst
, false))
9048 unsigned int count
= elts
.encoded_nelts ();
9049 for (unsigned int i
= 0; i
< count
; ++i
)
9051 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
9052 if (!exact_real_inverse (mode
, &r
))
9054 elts
.quick_push (build_real (unit_type
, r
));
9057 return elts
.build ();
9065 /* Mask out the tz least significant bits of X of type TYPE where
9066 tz is the number of trailing zeroes in Y. */
9068 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
9070 int tz
= wi::ctz (y
);
9072 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
9076 /* Return true when T is an address and is known to be nonzero.
9077 For floating point we further ensure that T is not denormal.
9078 Similar logic is present in nonzero_address in rtlanal.h.
9080 If the return value is based on the assumption that signed overflow
9081 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9082 change *STRICT_OVERFLOW_P. */
9085 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
9087 tree type
= TREE_TYPE (t
);
9088 enum tree_code code
;
9090 /* Doing something useful for floating point would need more work. */
9091 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
9094 code
= TREE_CODE (t
);
9095 switch (TREE_CODE_CLASS (code
))
9098 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9101 case tcc_comparison
:
9102 return tree_binary_nonzero_warnv_p (code
, type
,
9103 TREE_OPERAND (t
, 0),
9104 TREE_OPERAND (t
, 1),
9107 case tcc_declaration
:
9109 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9117 case TRUTH_NOT_EXPR
:
9118 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9121 case TRUTH_AND_EXPR
:
9123 case TRUTH_XOR_EXPR
:
9124 return tree_binary_nonzero_warnv_p (code
, type
,
9125 TREE_OPERAND (t
, 0),
9126 TREE_OPERAND (t
, 1),
9134 case WITH_SIZE_EXPR
:
9136 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9141 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9145 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9150 tree fndecl
= get_callee_fndecl (t
);
9151 if (!fndecl
) return false;
9152 if (flag_delete_null_pointer_checks
&& !flag_check_new
9153 && DECL_IS_OPERATOR_NEW (fndecl
)
9154 && !TREE_NOTHROW (fndecl
))
9156 if (flag_delete_null_pointer_checks
9157 && lookup_attribute ("returns_nonnull",
9158 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9160 return alloca_call_p (t
);
9169 /* Return true when T is an address and is known to be nonzero.
9170 Handle warnings about undefined signed overflow. */
9173 tree_expr_nonzero_p (tree t
)
9175 bool ret
, strict_overflow_p
;
9177 strict_overflow_p
= false;
9178 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9179 if (strict_overflow_p
)
9180 fold_overflow_warning (("assuming signed overflow does not occur when "
9181 "determining that expression is always "
9183 WARN_STRICT_OVERFLOW_MISC
);
9187 /* Return true if T is known not to be equal to an integer W. */
9190 expr_not_equal_to (tree t
, const wide_int
&w
)
9192 wide_int min
, max
, nz
;
9193 value_range_type rtype
;
9194 switch (TREE_CODE (t
))
9197 return wi::to_wide (t
) != w
;
9200 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9202 rtype
= get_range_info (t
, &min
, &max
);
9203 if (rtype
== VR_RANGE
)
9205 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9207 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9210 else if (rtype
== VR_ANTI_RANGE
9211 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9212 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9214 /* If T has some known zero bits and W has any of those bits set,
9215 then T is known not to be equal to W. */
9216 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9217 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9226 /* Fold a binary expression of code CODE and type TYPE with operands
9227 OP0 and OP1. LOC is the location of the resulting expression.
9228 Return the folded expression if folding is successful. Otherwise,
9229 return NULL_TREE. */
9232 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
9235 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9236 tree arg0
, arg1
, tem
;
9237 tree t1
= NULL_TREE
;
9238 bool strict_overflow_p
;
9241 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9242 && TREE_CODE_LENGTH (code
) == 2
9244 && op1
!= NULL_TREE
);
9249 /* Strip any conversions that don't change the mode. This is
9250 safe for every expression, except for a comparison expression
9251 because its signedness is derived from its operands. So, in
9252 the latter case, only strip conversions that don't change the
9253 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9256 Note that this is done as an internal manipulation within the
9257 constant folder, in order to find the simplest representation
9258 of the arguments so that their form can be studied. In any
9259 cases, the appropriate type conversions should be put back in
9260 the tree that will get out of the constant folder. */
9262 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9264 STRIP_SIGN_NOPS (arg0
);
9265 STRIP_SIGN_NOPS (arg1
);
9273 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9274 constant but we can't do arithmetic on them. */
9275 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9277 tem
= const_binop (code
, type
, arg0
, arg1
);
9278 if (tem
!= NULL_TREE
)
9280 if (TREE_TYPE (tem
) != type
)
9281 tem
= fold_convert_loc (loc
, type
, tem
);
9286 /* If this is a commutative operation, and ARG0 is a constant, move it
9287 to ARG1 to reduce the number of tests below. */
9288 if (commutative_tree_code (code
)
9289 && tree_swap_operands_p (arg0
, arg1
))
9290 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9292 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9293 to ARG1 to reduce the number of tests below. */
9294 if (kind
== tcc_comparison
9295 && tree_swap_operands_p (arg0
, arg1
))
9296 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9298 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9302 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9304 First check for cases where an arithmetic operation is applied to a
9305 compound, conditional, or comparison operation. Push the arithmetic
9306 operation inside the compound or conditional to see if any folding
9307 can then be done. Convert comparison to conditional for this purpose.
9308 The also optimizes non-constant cases that used to be done in
9311 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9312 one of the operands is a comparison and the other is a comparison, a
9313 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9314 code below would make the expression more complex. Change it to a
9315 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9316 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9318 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9319 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9320 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
9321 && ((truth_value_p (TREE_CODE (arg0
))
9322 && (truth_value_p (TREE_CODE (arg1
))
9323 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9324 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9325 || (truth_value_p (TREE_CODE (arg1
))
9326 && (truth_value_p (TREE_CODE (arg0
))
9327 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9328 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9330 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9331 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9334 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9335 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9337 if (code
== EQ_EXPR
)
9338 tem
= invert_truthvalue_loc (loc
, tem
);
9340 return fold_convert_loc (loc
, type
, tem
);
9343 if (TREE_CODE_CLASS (code
) == tcc_binary
9344 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9346 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9348 tem
= fold_build2_loc (loc
, code
, type
,
9349 fold_convert_loc (loc
, TREE_TYPE (op0
),
9350 TREE_OPERAND (arg0
, 1)), op1
);
9351 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9354 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
9356 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9357 fold_convert_loc (loc
, TREE_TYPE (op1
),
9358 TREE_OPERAND (arg1
, 1)));
9359 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9363 if (TREE_CODE (arg0
) == COND_EXPR
9364 || TREE_CODE (arg0
) == VEC_COND_EXPR
9365 || COMPARISON_CLASS_P (arg0
))
9367 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9369 /*cond_first_p=*/1);
9370 if (tem
!= NULL_TREE
)
9374 if (TREE_CODE (arg1
) == COND_EXPR
9375 || TREE_CODE (arg1
) == VEC_COND_EXPR
9376 || COMPARISON_CLASS_P (arg1
))
9378 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9380 /*cond_first_p=*/0);
9381 if (tem
!= NULL_TREE
)
9389 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9390 if (TREE_CODE (arg0
) == ADDR_EXPR
9391 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9393 tree iref
= TREE_OPERAND (arg0
, 0);
9394 return fold_build2 (MEM_REF
, type
,
9395 TREE_OPERAND (iref
, 0),
9396 int_const_binop (PLUS_EXPR
, arg1
,
9397 TREE_OPERAND (iref
, 1)));
9400 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9401 if (TREE_CODE (arg0
) == ADDR_EXPR
9402 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9406 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9410 return fold_build2 (MEM_REF
, type
,
9411 build_fold_addr_expr (base
),
9412 int_const_binop (PLUS_EXPR
, arg1
,
9413 size_int (coffset
)));
9418 case POINTER_PLUS_EXPR
:
9419 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9420 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9421 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9422 return fold_convert_loc (loc
, type
,
9423 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9424 fold_convert_loc (loc
, sizetype
,
9426 fold_convert_loc (loc
, sizetype
,
9432 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9434 /* X + (X / CST) * -CST is X % CST. */
9435 if (TREE_CODE (arg1
) == MULT_EXPR
9436 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9437 && operand_equal_p (arg0
,
9438 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9440 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9441 tree cst1
= TREE_OPERAND (arg1
, 1);
9442 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9444 if (sum
&& integer_zerop (sum
))
9445 return fold_convert_loc (loc
, type
,
9446 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9447 TREE_TYPE (arg0
), arg0
,
9452 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9453 one. Make sure the type is not saturating and has the signedness of
9454 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9455 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9456 if ((TREE_CODE (arg0
) == MULT_EXPR
9457 || TREE_CODE (arg1
) == MULT_EXPR
)
9458 && !TYPE_SATURATING (type
)
9459 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9460 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9461 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9463 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9468 if (! FLOAT_TYPE_P (type
))
9470 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9471 (plus (plus (mult) (mult)) (foo)) so that we can
9472 take advantage of the factoring cases below. */
9473 if (ANY_INTEGRAL_TYPE_P (type
)
9474 && TYPE_OVERFLOW_WRAPS (type
)
9475 && (((TREE_CODE (arg0
) == PLUS_EXPR
9476 || TREE_CODE (arg0
) == MINUS_EXPR
)
9477 && TREE_CODE (arg1
) == MULT_EXPR
)
9478 || ((TREE_CODE (arg1
) == PLUS_EXPR
9479 || TREE_CODE (arg1
) == MINUS_EXPR
)
9480 && TREE_CODE (arg0
) == MULT_EXPR
)))
9482 tree parg0
, parg1
, parg
, marg
;
9483 enum tree_code pcode
;
9485 if (TREE_CODE (arg1
) == MULT_EXPR
)
9486 parg
= arg0
, marg
= arg1
;
9488 parg
= arg1
, marg
= arg0
;
9489 pcode
= TREE_CODE (parg
);
9490 parg0
= TREE_OPERAND (parg
, 0);
9491 parg1
= TREE_OPERAND (parg
, 1);
9495 if (TREE_CODE (parg0
) == MULT_EXPR
9496 && TREE_CODE (parg1
) != MULT_EXPR
)
9497 return fold_build2_loc (loc
, pcode
, type
,
9498 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9499 fold_convert_loc (loc
, type
,
9501 fold_convert_loc (loc
, type
,
9503 fold_convert_loc (loc
, type
, parg1
));
9504 if (TREE_CODE (parg0
) != MULT_EXPR
9505 && TREE_CODE (parg1
) == MULT_EXPR
)
9507 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9508 fold_convert_loc (loc
, type
, parg0
),
9509 fold_build2_loc (loc
, pcode
, type
,
9510 fold_convert_loc (loc
, type
, marg
),
9511 fold_convert_loc (loc
, type
,
9517 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9518 to __complex__ ( x, y ). This is not the same for SNaNs or
9519 if signed zeros are involved. */
9520 if (!HONOR_SNANS (element_mode (arg0
))
9521 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9522 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9524 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9525 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9526 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9527 bool arg0rz
= false, arg0iz
= false;
9528 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9529 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9531 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9532 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9533 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9535 tree rp
= arg1r
? arg1r
9536 : build1 (REALPART_EXPR
, rtype
, arg1
);
9537 tree ip
= arg0i
? arg0i
9538 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9539 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9541 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9543 tree rp
= arg0r
? arg0r
9544 : build1 (REALPART_EXPR
, rtype
, arg0
);
9545 tree ip
= arg1i
? arg1i
9546 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9547 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9552 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9553 We associate floats only if the user has specified
9554 -fassociative-math. */
9555 if (flag_associative_math
9556 && TREE_CODE (arg1
) == PLUS_EXPR
9557 && TREE_CODE (arg0
) != MULT_EXPR
)
9559 tree tree10
= TREE_OPERAND (arg1
, 0);
9560 tree tree11
= TREE_OPERAND (arg1
, 1);
9561 if (TREE_CODE (tree11
) == MULT_EXPR
9562 && TREE_CODE (tree10
) == MULT_EXPR
)
9565 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9566 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9569 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9570 We associate floats only if the user has specified
9571 -fassociative-math. */
9572 if (flag_associative_math
9573 && TREE_CODE (arg0
) == PLUS_EXPR
9574 && TREE_CODE (arg1
) != MULT_EXPR
)
9576 tree tree00
= TREE_OPERAND (arg0
, 0);
9577 tree tree01
= TREE_OPERAND (arg0
, 1);
9578 if (TREE_CODE (tree01
) == MULT_EXPR
9579 && TREE_CODE (tree00
) == MULT_EXPR
)
9582 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9583 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9589 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9590 is a rotate of A by C1 bits. */
9591 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9592 is a rotate of A by B bits.
9593 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9594 though in this case CODE must be | and not + or ^, otherwise
9595 it doesn't return A when B is 0. */
9597 enum tree_code code0
, code1
;
9599 code0
= TREE_CODE (arg0
);
9600 code1
= TREE_CODE (arg1
);
9601 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9602 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9603 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9604 TREE_OPERAND (arg1
, 0), 0)
9605 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9606 TYPE_UNSIGNED (rtype
))
9607 /* Only create rotates in complete modes. Other cases are not
9608 expanded properly. */
9609 && (element_precision (rtype
)
9610 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9612 tree tree01
, tree11
;
9613 tree orig_tree01
, orig_tree11
;
9614 enum tree_code code01
, code11
;
9616 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
9617 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
9618 STRIP_NOPS (tree01
);
9619 STRIP_NOPS (tree11
);
9620 code01
= TREE_CODE (tree01
);
9621 code11
= TREE_CODE (tree11
);
9622 if (code11
!= MINUS_EXPR
9623 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
9625 std::swap (code0
, code1
);
9626 std::swap (code01
, code11
);
9627 std::swap (tree01
, tree11
);
9628 std::swap (orig_tree01
, orig_tree11
);
9630 if (code01
== INTEGER_CST
9631 && code11
== INTEGER_CST
9632 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9633 == element_precision (rtype
)))
9635 tem
= build2_loc (loc
, LROTATE_EXPR
,
9636 rtype
, TREE_OPERAND (arg0
, 0),
9637 code0
== LSHIFT_EXPR
9638 ? orig_tree01
: orig_tree11
);
9639 return fold_convert_loc (loc
, type
, tem
);
9641 else if (code11
== MINUS_EXPR
)
9643 tree tree110
, tree111
;
9644 tree110
= TREE_OPERAND (tree11
, 0);
9645 tree111
= TREE_OPERAND (tree11
, 1);
9646 STRIP_NOPS (tree110
);
9647 STRIP_NOPS (tree111
);
9648 if (TREE_CODE (tree110
) == INTEGER_CST
9649 && compare_tree_int (tree110
,
9650 element_precision (rtype
)) == 0
9651 && operand_equal_p (tree01
, tree111
, 0))
9653 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
9654 ? LROTATE_EXPR
: RROTATE_EXPR
),
9655 rtype
, TREE_OPERAND (arg0
, 0),
9657 return fold_convert_loc (loc
, type
, tem
);
9660 else if (code
== BIT_IOR_EXPR
9661 && code11
== BIT_AND_EXPR
9662 && pow2p_hwi (element_precision (rtype
)))
9664 tree tree110
, tree111
;
9665 tree110
= TREE_OPERAND (tree11
, 0);
9666 tree111
= TREE_OPERAND (tree11
, 1);
9667 STRIP_NOPS (tree110
);
9668 STRIP_NOPS (tree111
);
9669 if (TREE_CODE (tree110
) == NEGATE_EXPR
9670 && TREE_CODE (tree111
) == INTEGER_CST
9671 && compare_tree_int (tree111
,
9672 element_precision (rtype
) - 1) == 0
9673 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
9675 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
9676 ? LROTATE_EXPR
: RROTATE_EXPR
),
9677 rtype
, TREE_OPERAND (arg0
, 0),
9679 return fold_convert_loc (loc
, type
, tem
);
9686 /* In most languages, can't associate operations on floats through
9687 parentheses. Rather than remember where the parentheses were, we
9688 don't associate floats at all, unless the user has specified
9690 And, we need to make sure type is not saturating. */
9692 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9693 && !TYPE_SATURATING (type
))
9695 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
9696 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
9700 /* Split both trees into variables, constants, and literals. Then
9701 associate each group together, the constants with literals,
9702 then the result with variables. This increases the chances of
9703 literals being recombined later and of generating relocatable
9704 expressions for the sum of a constant and literal. */
9705 var0
= split_tree (arg0
, type
, code
,
9706 &minus_var0
, &con0
, &minus_con0
,
9707 &lit0
, &minus_lit0
, 0);
9708 var1
= split_tree (arg1
, type
, code
,
9709 &minus_var1
, &con1
, &minus_con1
,
9710 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9712 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9713 if (code
== MINUS_EXPR
)
9716 /* With undefined overflow prefer doing association in a type
9717 which wraps on overflow, if that is one of the operand types. */
9718 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
9719 && !TYPE_OVERFLOW_WRAPS (type
))
9721 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9722 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9723 atype
= TREE_TYPE (arg0
);
9724 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9725 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9726 atype
= TREE_TYPE (arg1
);
9727 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9730 /* With undefined overflow we can only associate constants with one
9731 variable, and constants whose association doesn't overflow. */
9732 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
9733 && !TYPE_OVERFLOW_WRAPS (atype
))
9735 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
9737 /* ??? If split_tree would handle NEGATE_EXPR we could
9738 simply reject these cases and the allowed cases would
9739 be the var0/minus_var1 ones. */
9740 tree tmp0
= var0
? var0
: minus_var0
;
9741 tree tmp1
= var1
? var1
: minus_var1
;
9742 bool one_neg
= false;
9744 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9746 tmp0
= TREE_OPERAND (tmp0
, 0);
9749 if (CONVERT_EXPR_P (tmp0
)
9750 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9751 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9752 <= TYPE_PRECISION (atype
)))
9753 tmp0
= TREE_OPERAND (tmp0
, 0);
9754 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9756 tmp1
= TREE_OPERAND (tmp1
, 0);
9759 if (CONVERT_EXPR_P (tmp1
)
9760 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9761 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9762 <= TYPE_PRECISION (atype
)))
9763 tmp1
= TREE_OPERAND (tmp1
, 0);
9764 /* The only case we can still associate with two variables
9765 is if they cancel out. */
9767 || !operand_equal_p (tmp0
, tmp1
, 0))
9770 else if ((var0
&& minus_var1
9771 && ! operand_equal_p (var0
, minus_var1
, 0))
9772 || (minus_var0
&& var1
9773 && ! operand_equal_p (minus_var0
, var1
, 0)))
9777 /* Only do something if we found more than two objects. Otherwise,
9778 nothing has changed and we risk infinite recursion. */
9780 && ((var0
!= 0) + (var1
!= 0)
9781 + (minus_var0
!= 0) + (minus_var1
!= 0)
9782 + (con0
!= 0) + (con1
!= 0)
9783 + (minus_con0
!= 0) + (minus_con1
!= 0)
9784 + (lit0
!= 0) + (lit1
!= 0)
9785 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
9787 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9788 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
9790 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9791 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
9793 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9794 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9797 if (minus_var0
&& var0
)
9799 var0
= associate_trees (loc
, var0
, minus_var0
,
9803 if (minus_con0
&& con0
)
9805 con0
= associate_trees (loc
, con0
, minus_con0
,
9810 /* Preserve the MINUS_EXPR if the negative part of the literal is
9811 greater than the positive part. Otherwise, the multiplicative
9812 folding code (i.e extract_muldiv) may be fooled in case
9813 unsigned constants are subtracted, like in the following
9814 example: ((X*2 + 4) - 8U)/2. */
9815 if (minus_lit0
&& lit0
)
9817 if (TREE_CODE (lit0
) == INTEGER_CST
9818 && TREE_CODE (minus_lit0
) == INTEGER_CST
9819 && tree_int_cst_lt (lit0
, minus_lit0
)
9820 /* But avoid ending up with only negated parts. */
9823 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9829 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9835 /* Don't introduce overflows through reassociation. */
9836 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
9837 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
9840 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9841 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9843 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
9847 /* Eliminate minus_con0. */
9851 con0
= associate_trees (loc
, con0
, minus_con0
,
9854 var0
= associate_trees (loc
, var0
, minus_con0
,
9861 /* Eliminate minus_var0. */
9865 con0
= associate_trees (loc
, con0
, minus_var0
,
9873 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9880 case POINTER_DIFF_EXPR
:
9882 /* Fold &a[i] - &a[j] to i-j. */
9883 if (TREE_CODE (arg0
) == ADDR_EXPR
9884 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9885 && TREE_CODE (arg1
) == ADDR_EXPR
9886 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9888 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9889 TREE_OPERAND (arg0
, 0),
9890 TREE_OPERAND (arg1
, 0),
9892 == POINTER_DIFF_EXPR
);
9897 /* Further transformations are not for pointers. */
9898 if (code
== POINTER_DIFF_EXPR
)
9901 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9902 if (TREE_CODE (arg0
) == NEGATE_EXPR
9903 && negate_expr_p (op1
)
9904 /* If arg0 is e.g. unsigned int and type is int, then this could
9905 introduce UB, because if A is INT_MIN at runtime, the original
9906 expression can be well defined while the latter is not.
9908 && !(ANY_INTEGRAL_TYPE_P (type
)
9909 && TYPE_OVERFLOW_UNDEFINED (type
)
9910 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9911 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
9912 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
9913 fold_convert_loc (loc
, type
,
9914 TREE_OPERAND (arg0
, 0)));
9916 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9917 __complex__ ( x, -y ). This is not the same for SNaNs or if
9918 signed zeros are involved. */
9919 if (!HONOR_SNANS (element_mode (arg0
))
9920 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9921 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9923 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9924 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9925 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9926 bool arg0rz
= false, arg0iz
= false;
9927 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9928 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9930 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9931 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9932 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9934 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9936 : build1 (REALPART_EXPR
, rtype
, arg1
));
9937 tree ip
= arg0i
? arg0i
9938 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9939 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9941 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9943 tree rp
= arg0r
? arg0r
9944 : build1 (REALPART_EXPR
, rtype
, arg0
);
9945 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9947 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9948 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9953 /* A - B -> A + (-B) if B is easily negatable. */
9954 if (negate_expr_p (op1
)
9955 && ! TYPE_OVERFLOW_SANITIZED (type
)
9956 && ((FLOAT_TYPE_P (type
)
9957 /* Avoid this transformation if B is a positive REAL_CST. */
9958 && (TREE_CODE (op1
) != REAL_CST
9959 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9960 || INTEGRAL_TYPE_P (type
)))
9961 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9962 fold_convert_loc (loc
, type
, arg0
),
9965 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9966 one. Make sure the type is not saturating and has the signedness of
9967 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9968 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9969 if ((TREE_CODE (arg0
) == MULT_EXPR
9970 || TREE_CODE (arg1
) == MULT_EXPR
)
9971 && !TYPE_SATURATING (type
)
9972 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9973 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9974 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9976 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9984 if (! FLOAT_TYPE_P (type
))
9986 /* Transform x * -C into -x * C if x is easily negatable. */
9987 if (TREE_CODE (op1
) == INTEGER_CST
9988 && tree_int_cst_sgn (op1
) == -1
9989 && negate_expr_p (op0
)
9990 && negate_expr_p (op1
)
9991 && (tem
= negate_expr (op1
)) != op1
9992 && ! TREE_OVERFLOW (tem
))
9993 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9994 fold_convert_loc (loc
, type
,
9995 negate_expr (op0
)), tem
);
9997 strict_overflow_p
= false;
9998 if (TREE_CODE (arg1
) == INTEGER_CST
9999 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10000 &strict_overflow_p
)) != 0)
10002 if (strict_overflow_p
)
10003 fold_overflow_warning (("assuming signed overflow does not "
10004 "occur when simplifying "
10006 WARN_STRICT_OVERFLOW_MISC
);
10007 return fold_convert_loc (loc
, type
, tem
);
10010 /* Optimize z * conj(z) for integer complex numbers. */
10011 if (TREE_CODE (arg0
) == CONJ_EXPR
10012 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10013 return fold_mult_zconjz (loc
, type
, arg1
);
10014 if (TREE_CODE (arg1
) == CONJ_EXPR
10015 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10016 return fold_mult_zconjz (loc
, type
, arg0
);
10020 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10021 This is not the same for NaNs or if signed zeros are
10023 if (!HONOR_NANS (arg0
)
10024 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
10025 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
10026 && TREE_CODE (arg1
) == COMPLEX_CST
10027 && real_zerop (TREE_REALPART (arg1
)))
10029 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10030 if (real_onep (TREE_IMAGPART (arg1
)))
10032 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
10033 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
10035 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
10036 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
10038 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
10039 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
10040 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
10044 /* Optimize z * conj(z) for floating point complex numbers.
10045 Guarded by flag_unsafe_math_optimizations as non-finite
10046 imaginary components don't produce scalar results. */
10047 if (flag_unsafe_math_optimizations
10048 && TREE_CODE (arg0
) == CONJ_EXPR
10049 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10050 return fold_mult_zconjz (loc
, type
, arg1
);
10051 if (flag_unsafe_math_optimizations
10052 && TREE_CODE (arg1
) == CONJ_EXPR
10053 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10054 return fold_mult_zconjz (loc
, type
, arg0
);
10059 /* Canonicalize (X & C1) | C2. */
10060 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10061 && TREE_CODE (arg1
) == INTEGER_CST
10062 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10064 int width
= TYPE_PRECISION (type
), w
;
10065 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
10066 wide_int c2
= wi::to_wide (arg1
);
10068 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10069 if ((c1
& c2
) == c1
)
10070 return omit_one_operand_loc (loc
, type
, arg1
,
10071 TREE_OPERAND (arg0
, 0));
10073 wide_int msk
= wi::mask (width
, false,
10074 TYPE_PRECISION (TREE_TYPE (arg1
)));
10076 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10077 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
10079 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10080 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
10083 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10084 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10085 mode which allows further optimizations. */
10088 wide_int c3
= wi::bit_and_not (c1
, c2
);
10089 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
10091 wide_int mask
= wi::mask (w
, false,
10092 TYPE_PRECISION (type
));
10093 if (((c1
| c2
) & mask
) == mask
10094 && wi::bit_and_not (c1
, mask
) == 0)
10103 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10104 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
10105 wide_int_to_tree (type
, c3
));
10106 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
10110 /* See if this can be simplified into a rotate first. If that
10111 is unsuccessful continue in the association code. */
10115 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10116 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10117 && INTEGRAL_TYPE_P (type
)
10118 && integer_onep (TREE_OPERAND (arg0
, 1))
10119 && integer_onep (arg1
))
10120 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
10121 build_zero_cst (TREE_TYPE (arg0
)));
10123 /* See if this can be simplified into a rotate first. If that
10124 is unsuccessful continue in the association code. */
10128 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10129 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10130 && INTEGRAL_TYPE_P (type
)
10131 && integer_onep (TREE_OPERAND (arg0
, 1))
10132 && integer_onep (arg1
))
10135 tem
= TREE_OPERAND (arg0
, 0);
10136 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
10137 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
10139 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
10140 build_zero_cst (TREE_TYPE (tem
)));
10142 /* Fold ~X & 1 as (X & 1) == 0. */
10143 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
10144 && INTEGRAL_TYPE_P (type
)
10145 && integer_onep (arg1
))
10148 tem
= TREE_OPERAND (arg0
, 0);
10149 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
10150 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
10152 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
10153 build_zero_cst (TREE_TYPE (tem
)));
10155 /* Fold !X & 1 as X == 0. */
10156 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10157 && integer_onep (arg1
))
10159 tem
= TREE_OPERAND (arg0
, 0);
10160 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
10161 build_zero_cst (TREE_TYPE (tem
)));
10164 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10165 multiple of 1 << CST. */
10166 if (TREE_CODE (arg1
) == INTEGER_CST
)
10168 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
10169 wide_int ncst1
= -cst1
;
10170 if ((cst1
& ncst1
) == ncst1
10171 && multiple_of_p (type
, arg0
,
10172 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
10173 return fold_convert_loc (loc
, type
, arg0
);
10176 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10178 if (TREE_CODE (arg1
) == INTEGER_CST
10179 && TREE_CODE (arg0
) == MULT_EXPR
10180 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10182 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
10184 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
10187 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10189 else if (masked
!= warg1
)
10191 /* Avoid the transform if arg1 is a mask of some
10192 mode which allows further optimizations. */
10193 int pop
= wi::popcount (warg1
);
10194 if (!(pop
>= BITS_PER_UNIT
10196 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10197 return fold_build2_loc (loc
, code
, type
, op0
,
10198 wide_int_to_tree (type
, masked
));
10202 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10203 ((A & N) + B) & M -> (A + B) & M
10204 Similarly if (N & M) == 0,
10205 ((A | N) + B) & M -> (A + B) & M
10206 and for - instead of + (or unary - instead of +)
10207 and/or ^ instead of |.
10208 If B is constant and (B & M) == 0, fold into A & M. */
10209 if (TREE_CODE (arg1
) == INTEGER_CST
)
10211 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
10212 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10213 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10214 && (TREE_CODE (arg0
) == PLUS_EXPR
10215 || TREE_CODE (arg0
) == MINUS_EXPR
10216 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10217 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10218 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10224 /* Now we know that arg0 is (C + D) or (C - D) or
10225 -C and arg1 (M) is == (1LL << cst) - 1.
10226 Store C into PMOP[0] and D into PMOP[1]. */
10227 pmop
[0] = TREE_OPERAND (arg0
, 0);
10229 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10231 pmop
[1] = TREE_OPERAND (arg0
, 1);
10235 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10238 for (; which
>= 0; which
--)
10239 switch (TREE_CODE (pmop
[which
]))
10244 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10247 cst0
= wi::to_wide (TREE_OPERAND (pmop
[which
], 1)) & cst1
;
10248 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10253 else if (cst0
!= 0)
10255 /* If C or D is of the form (A & N) where
10256 (N & M) == M, or of the form (A | N) or
10257 (A ^ N) where (N & M) == 0, replace it with A. */
10258 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10261 /* If C or D is a N where (N & M) == 0, it can be
10262 omitted (assumed 0). */
10263 if ((TREE_CODE (arg0
) == PLUS_EXPR
10264 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10265 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
10266 pmop
[which
] = NULL
;
10272 /* Only build anything new if we optimized one or both arguments
10274 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10275 || (TREE_CODE (arg0
) != NEGATE_EXPR
10276 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10278 tree utype
= TREE_TYPE (arg0
);
10279 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10281 /* Perform the operations in a type that has defined
10282 overflow behavior. */
10283 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10284 if (pmop
[0] != NULL
)
10285 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10286 if (pmop
[1] != NULL
)
10287 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10290 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10291 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10292 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10294 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10295 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10297 else if (pmop
[0] != NULL
)
10299 else if (pmop
[1] != NULL
)
10302 return build_int_cst (type
, 0);
10304 else if (pmop
[0] == NULL
)
10305 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10307 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10309 /* TEM is now the new binary +, - or unary - replacement. */
10310 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10311 fold_convert_loc (loc
, utype
, arg1
));
10312 return fold_convert_loc (loc
, type
, tem
);
10317 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10318 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10319 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10321 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10323 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
10326 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10332 /* Don't touch a floating-point divide by zero unless the mode
10333 of the constant can represent infinity. */
10334 if (TREE_CODE (arg1
) == REAL_CST
10335 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10336 && real_zerop (arg1
))
10339 /* (-A) / (-B) -> A / B */
10340 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10341 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10342 TREE_OPERAND (arg0
, 0),
10343 negate_expr (arg1
));
10344 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10345 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10346 negate_expr (arg0
),
10347 TREE_OPERAND (arg1
, 0));
10350 case TRUNC_DIV_EXPR
:
10353 case FLOOR_DIV_EXPR
:
10354 /* Simplify A / (B << N) where A and B are positive and B is
10355 a power of 2, to A >> (N + log2(B)). */
10356 strict_overflow_p
= false;
10357 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10358 && (TYPE_UNSIGNED (type
)
10359 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10361 tree sval
= TREE_OPERAND (arg1
, 0);
10362 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10364 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10365 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10366 wi::exact_log2 (wi::to_wide (sval
)));
10368 if (strict_overflow_p
)
10369 fold_overflow_warning (("assuming signed overflow does not "
10370 "occur when simplifying A / (B << N)"),
10371 WARN_STRICT_OVERFLOW_MISC
);
10373 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10375 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10376 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10382 case ROUND_DIV_EXPR
:
10383 case CEIL_DIV_EXPR
:
10384 case EXACT_DIV_EXPR
:
10385 if (integer_zerop (arg1
))
10388 /* Convert -A / -B to A / B when the type is signed and overflow is
10390 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10391 && TREE_CODE (op0
) == NEGATE_EXPR
10392 && negate_expr_p (op1
))
10394 if (INTEGRAL_TYPE_P (type
))
10395 fold_overflow_warning (("assuming signed overflow does not occur "
10396 "when distributing negation across "
10398 WARN_STRICT_OVERFLOW_MISC
);
10399 return fold_build2_loc (loc
, code
, type
,
10400 fold_convert_loc (loc
, type
,
10401 TREE_OPERAND (arg0
, 0)),
10402 negate_expr (op1
));
10404 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10405 && TREE_CODE (arg1
) == NEGATE_EXPR
10406 && negate_expr_p (op0
))
10408 if (INTEGRAL_TYPE_P (type
))
10409 fold_overflow_warning (("assuming signed overflow does not occur "
10410 "when distributing negation across "
10412 WARN_STRICT_OVERFLOW_MISC
);
10413 return fold_build2_loc (loc
, code
, type
,
10415 fold_convert_loc (loc
, type
,
10416 TREE_OPERAND (arg1
, 0)));
10419 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10420 operation, EXACT_DIV_EXPR.
10422 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10423 At one time others generated faster code, it's not clear if they do
10424 after the last round to changes to the DIV code in expmed.c. */
10425 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10426 && multiple_of_p (type
, arg0
, arg1
))
10427 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10428 fold_convert (type
, arg0
),
10429 fold_convert (type
, arg1
));
10431 strict_overflow_p
= false;
10432 if (TREE_CODE (arg1
) == INTEGER_CST
10433 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10434 &strict_overflow_p
)) != 0)
10436 if (strict_overflow_p
)
10437 fold_overflow_warning (("assuming signed overflow does not occur "
10438 "when simplifying division"),
10439 WARN_STRICT_OVERFLOW_MISC
);
10440 return fold_convert_loc (loc
, type
, tem
);
10445 case CEIL_MOD_EXPR
:
10446 case FLOOR_MOD_EXPR
:
10447 case ROUND_MOD_EXPR
:
10448 case TRUNC_MOD_EXPR
:
10449 strict_overflow_p
= false;
10450 if (TREE_CODE (arg1
) == INTEGER_CST
10451 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10452 &strict_overflow_p
)) != 0)
10454 if (strict_overflow_p
)
10455 fold_overflow_warning (("assuming signed overflow does not occur "
10456 "when simplifying modulus"),
10457 WARN_STRICT_OVERFLOW_MISC
);
10458 return fold_convert_loc (loc
, type
, tem
);
10467 /* Since negative shift count is not well-defined,
10468 don't try to compute it in the compiler. */
10469 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10472 prec
= element_precision (type
);
10474 /* If we have a rotate of a bit operation with the rotate count and
10475 the second operand of the bit operation both constant,
10476 permute the two operations. */
10477 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10478 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10479 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10480 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10481 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10483 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10484 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10485 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10486 fold_build2_loc (loc
, code
, type
,
10488 fold_build2_loc (loc
, code
, type
,
10492 /* Two consecutive rotates adding up to the some integer
10493 multiple of the precision of the type can be ignored. */
10494 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10495 && TREE_CODE (arg0
) == RROTATE_EXPR
10496 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10497 && wi::umod_trunc (wi::to_wide (arg1
)
10498 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
10500 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10508 case TRUTH_ANDIF_EXPR
:
10509 /* Note that the operands of this must be ints
10510 and their values must be 0 or 1.
10511 ("true" is a fixed value perhaps depending on the language.) */
10512 /* If first arg is constant zero, return it. */
10513 if (integer_zerop (arg0
))
10514 return fold_convert_loc (loc
, type
, arg0
);
10516 case TRUTH_AND_EXPR
:
10517 /* If either arg is constant true, drop it. */
10518 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10519 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10520 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10521 /* Preserve sequence points. */
10522 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10523 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10524 /* If second arg is constant zero, result is zero, but first arg
10525 must be evaluated. */
10526 if (integer_zerop (arg1
))
10527 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10528 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10529 case will be handled here. */
10530 if (integer_zerop (arg0
))
10531 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10533 /* !X && X is always false. */
10534 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10535 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10536 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10537 /* X && !X is always false. */
10538 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10539 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10540 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10542 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10543 means A >= Y && A != MAX, but in this case we know that
10546 if (!TREE_SIDE_EFFECTS (arg0
)
10547 && !TREE_SIDE_EFFECTS (arg1
))
10549 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10550 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10551 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10553 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10554 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10555 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10558 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10564 case TRUTH_ORIF_EXPR
:
10565 /* Note that the operands of this must be ints
10566 and their values must be 0 or true.
10567 ("true" is a fixed value perhaps depending on the language.) */
10568 /* If first arg is constant true, return it. */
10569 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10570 return fold_convert_loc (loc
, type
, arg0
);
10572 case TRUTH_OR_EXPR
:
10573 /* If either arg is constant zero, drop it. */
10574 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10575 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10576 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10577 /* Preserve sequence points. */
10578 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10579 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10580 /* If second arg is constant true, result is true, but we must
10581 evaluate first arg. */
10582 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10583 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10584 /* Likewise for first arg, but note this only occurs here for
10586 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10587 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10589 /* !X || X is always true. */
10590 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10591 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10592 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10593 /* X || !X is always true. */
10594 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10595 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10596 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10598 /* (X && !Y) || (!X && Y) is X ^ Y */
10599 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10600 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10602 tree a0
, a1
, l0
, l1
, n0
, n1
;
10604 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10605 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10607 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10608 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10610 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10611 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10613 if ((operand_equal_p (n0
, a0
, 0)
10614 && operand_equal_p (n1
, a1
, 0))
10615 || (operand_equal_p (n0
, a1
, 0)
10616 && operand_equal_p (n1
, a0
, 0)))
10617 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10620 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10626 case TRUTH_XOR_EXPR
:
10627 /* If the second arg is constant zero, drop it. */
10628 if (integer_zerop (arg1
))
10629 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10630 /* If the second arg is constant true, this is a logical inversion. */
10631 if (integer_onep (arg1
))
10633 tem
= invert_truthvalue_loc (loc
, arg0
);
10634 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10636 /* Identical arguments cancel to zero. */
10637 if (operand_equal_p (arg0
, arg1
, 0))
10638 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10640 /* !X ^ X is always true. */
10641 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10642 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10643 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10645 /* X ^ !X is always true. */
10646 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10647 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10648 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10657 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10658 if (tem
!= NULL_TREE
)
10661 /* bool_var != 1 becomes !bool_var. */
10662 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10663 && code
== NE_EXPR
)
10664 return fold_convert_loc (loc
, type
,
10665 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10666 TREE_TYPE (arg0
), arg0
));
10668 /* bool_var == 0 becomes !bool_var. */
10669 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10670 && code
== EQ_EXPR
)
10671 return fold_convert_loc (loc
, type
,
10672 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10673 TREE_TYPE (arg0
), arg0
));
10675 /* !exp != 0 becomes !exp */
10676 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10677 && code
== NE_EXPR
)
10678 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10680 /* If this is an EQ or NE comparison with zero and ARG0 is
10681 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10682 two operations, but the latter can be done in one less insn
10683 on machines that have only two-operand insns or on which a
10684 constant cannot be the first operand. */
10685 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10686 && integer_zerop (arg1
))
10688 tree arg00
= TREE_OPERAND (arg0
, 0);
10689 tree arg01
= TREE_OPERAND (arg0
, 1);
10690 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10691 && integer_onep (TREE_OPERAND (arg00
, 0)))
10693 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10694 arg01
, TREE_OPERAND (arg00
, 1));
10695 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10696 build_int_cst (TREE_TYPE (arg0
), 1));
10697 return fold_build2_loc (loc
, code
, type
,
10698 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10701 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10702 && integer_onep (TREE_OPERAND (arg01
, 0)))
10704 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10705 arg00
, TREE_OPERAND (arg01
, 1));
10706 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10707 build_int_cst (TREE_TYPE (arg0
), 1));
10708 return fold_build2_loc (loc
, code
, type
,
10709 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10714 /* If this is an NE or EQ comparison of zero against the result of a
10715 signed MOD operation whose second operand is a power of 2, make
10716 the MOD operation unsigned since it is simpler and equivalent. */
10717 if (integer_zerop (arg1
)
10718 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10719 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10720 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10721 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10722 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10723 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10725 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10726 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10727 fold_convert_loc (loc
, newtype
,
10728 TREE_OPERAND (arg0
, 0)),
10729 fold_convert_loc (loc
, newtype
,
10730 TREE_OPERAND (arg0
, 1)));
10732 return fold_build2_loc (loc
, code
, type
, newmod
,
10733 fold_convert_loc (loc
, newtype
, arg1
));
10736 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10737 C1 is a valid shift constant, and C2 is a power of two, i.e.
10739 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10740 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10741 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10743 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10744 && integer_zerop (arg1
))
10746 tree itype
= TREE_TYPE (arg0
);
10747 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10748 prec
= TYPE_PRECISION (itype
);
10750 /* Check for a valid shift count. */
10751 if (wi::ltu_p (wi::to_wide (arg001
), prec
))
10753 tree arg01
= TREE_OPERAND (arg0
, 1);
10754 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10755 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10756 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10757 can be rewritten as (X & (C2 << C1)) != 0. */
10758 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10760 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10761 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10762 return fold_build2_loc (loc
, code
, type
, tem
,
10763 fold_convert_loc (loc
, itype
, arg1
));
10765 /* Otherwise, for signed (arithmetic) shifts,
10766 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10767 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10768 else if (!TYPE_UNSIGNED (itype
))
10769 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10770 arg000
, build_int_cst (itype
, 0));
10771 /* Otherwise, of unsigned (logical) shifts,
10772 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10773 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10775 return omit_one_operand_loc (loc
, type
,
10776 code
== EQ_EXPR
? integer_one_node
10777 : integer_zero_node
,
10782 /* If this is a comparison of a field, we may be able to simplify it. */
10783 if ((TREE_CODE (arg0
) == COMPONENT_REF
10784 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10785 /* Handle the constant case even without -O
10786 to make sure the warnings are given. */
10787 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10789 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10794 /* Optimize comparisons of strlen vs zero to a compare of the
10795 first character of the string vs zero. To wit,
10796 strlen(ptr) == 0 => *ptr == 0
10797 strlen(ptr) != 0 => *ptr != 0
10798 Other cases should reduce to one of these two (or a constant)
10799 due to the return value of strlen being unsigned. */
10800 if (TREE_CODE (arg0
) == CALL_EXPR
10801 && integer_zerop (arg1
))
10803 tree fndecl
= get_callee_fndecl (arg0
);
10806 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10807 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10808 && call_expr_nargs (arg0
) == 1
10809 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10811 tree iref
= build_fold_indirect_ref_loc (loc
,
10812 CALL_EXPR_ARG (arg0
, 0));
10813 return fold_build2_loc (loc
, code
, type
, iref
,
10814 build_int_cst (TREE_TYPE (iref
), 0));
10818 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10819 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10820 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10821 && integer_zerop (arg1
)
10822 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10824 tree arg00
= TREE_OPERAND (arg0
, 0);
10825 tree arg01
= TREE_OPERAND (arg0
, 1);
10826 tree itype
= TREE_TYPE (arg00
);
10827 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
10829 if (TYPE_UNSIGNED (itype
))
10831 itype
= signed_type_for (itype
);
10832 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10834 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10835 type
, arg00
, build_zero_cst (itype
));
10839 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10840 (X & C) == 0 when C is a single bit. */
10841 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10842 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10843 && integer_zerop (arg1
)
10844 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10846 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10847 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10848 TREE_OPERAND (arg0
, 1));
10849 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10851 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10855 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10856 constant C is a power of two, i.e. a single bit. */
10857 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10858 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10859 && integer_zerop (arg1
)
10860 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10861 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10862 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10864 tree arg00
= TREE_OPERAND (arg0
, 0);
10865 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10866 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10869 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10870 when is C is a power of two, i.e. a single bit. */
10871 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10872 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10873 && integer_zerop (arg1
)
10874 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10875 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10876 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10878 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10879 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10880 arg000
, TREE_OPERAND (arg0
, 1));
10881 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10882 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10885 if (integer_zerop (arg1
)
10886 && tree_expr_nonzero_p (arg0
))
10888 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10889 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10892 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10893 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10894 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10896 tree arg00
= TREE_OPERAND (arg0
, 0);
10897 tree arg01
= TREE_OPERAND (arg0
, 1);
10898 tree arg10
= TREE_OPERAND (arg1
, 0);
10899 tree arg11
= TREE_OPERAND (arg1
, 1);
10900 tree itype
= TREE_TYPE (arg0
);
10902 if (operand_equal_p (arg01
, arg11
, 0))
10904 tem
= fold_convert_loc (loc
, itype
, arg10
);
10905 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10906 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg01
);
10907 return fold_build2_loc (loc
, code
, type
, tem
,
10908 build_zero_cst (itype
));
10910 if (operand_equal_p (arg01
, arg10
, 0))
10912 tem
= fold_convert_loc (loc
, itype
, arg11
);
10913 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10914 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg01
);
10915 return fold_build2_loc (loc
, code
, type
, tem
,
10916 build_zero_cst (itype
));
10918 if (operand_equal_p (arg00
, arg11
, 0))
10920 tem
= fold_convert_loc (loc
, itype
, arg10
);
10921 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
, tem
);
10922 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg00
);
10923 return fold_build2_loc (loc
, code
, type
, tem
,
10924 build_zero_cst (itype
));
10926 if (operand_equal_p (arg00
, arg10
, 0))
10928 tem
= fold_convert_loc (loc
, itype
, arg11
);
10929 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
, tem
);
10930 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg00
);
10931 return fold_build2_loc (loc
, code
, type
, tem
,
10932 build_zero_cst (itype
));
10936 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10937 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10939 tree arg00
= TREE_OPERAND (arg0
, 0);
10940 tree arg01
= TREE_OPERAND (arg0
, 1);
10941 tree arg10
= TREE_OPERAND (arg1
, 0);
10942 tree arg11
= TREE_OPERAND (arg1
, 1);
10943 tree itype
= TREE_TYPE (arg0
);
10945 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10946 operand_equal_p guarantees no side-effects so we don't need
10947 to use omit_one_operand on Z. */
10948 if (operand_equal_p (arg01
, arg11
, 0))
10949 return fold_build2_loc (loc
, code
, type
, arg00
,
10950 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10952 if (operand_equal_p (arg01
, arg10
, 0))
10953 return fold_build2_loc (loc
, code
, type
, arg00
,
10954 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10956 if (operand_equal_p (arg00
, arg11
, 0))
10957 return fold_build2_loc (loc
, code
, type
, arg01
,
10958 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10960 if (operand_equal_p (arg00
, arg10
, 0))
10961 return fold_build2_loc (loc
, code
, type
, arg01
,
10962 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10965 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10966 if (TREE_CODE (arg01
) == INTEGER_CST
10967 && TREE_CODE (arg11
) == INTEGER_CST
)
10969 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10970 fold_convert_loc (loc
, itype
, arg11
));
10971 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10972 return fold_build2_loc (loc
, code
, type
, tem
,
10973 fold_convert_loc (loc
, itype
, arg10
));
10977 /* Attempt to simplify equality/inequality comparisons of complex
10978 values. Only lower the comparison if the result is known or
10979 can be simplified to a single scalar comparison. */
10980 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10981 || TREE_CODE (arg0
) == COMPLEX_CST
)
10982 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10983 || TREE_CODE (arg1
) == COMPLEX_CST
))
10985 tree real0
, imag0
, real1
, imag1
;
10988 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10990 real0
= TREE_OPERAND (arg0
, 0);
10991 imag0
= TREE_OPERAND (arg0
, 1);
10995 real0
= TREE_REALPART (arg0
);
10996 imag0
= TREE_IMAGPART (arg0
);
10999 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
11001 real1
= TREE_OPERAND (arg1
, 0);
11002 imag1
= TREE_OPERAND (arg1
, 1);
11006 real1
= TREE_REALPART (arg1
);
11007 imag1
= TREE_IMAGPART (arg1
);
11010 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
11011 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
11013 if (integer_zerop (rcond
))
11015 if (code
== EQ_EXPR
)
11016 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
11018 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
11022 if (code
== NE_EXPR
)
11023 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
11025 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
11029 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
11030 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
11032 if (integer_zerop (icond
))
11034 if (code
== EQ_EXPR
)
11035 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
11037 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
11041 if (code
== NE_EXPR
)
11042 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
11044 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
11055 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
11056 if (tem
!= NULL_TREE
)
11059 /* Transform comparisons of the form X +- C CMP X. */
11060 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
11061 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11062 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
11063 && !HONOR_SNANS (arg0
))
11065 tree arg01
= TREE_OPERAND (arg0
, 1);
11066 enum tree_code code0
= TREE_CODE (arg0
);
11067 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
11069 /* (X - c) > X becomes false. */
11070 if (code
== GT_EXPR
11071 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11072 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11073 return constant_boolean_node (0, type
);
11075 /* Likewise (X + c) < X becomes false. */
11076 if (code
== LT_EXPR
11077 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11078 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11079 return constant_boolean_node (0, type
);
11081 /* Convert (X - c) <= X to true. */
11082 if (!HONOR_NANS (arg1
)
11084 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11085 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11086 return constant_boolean_node (1, type
);
11088 /* Convert (X + c) >= X to true. */
11089 if (!HONOR_NANS (arg1
)
11091 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11092 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11093 return constant_boolean_node (1, type
);
11096 /* If we are comparing an ABS_EXPR with a constant, we can
11097 convert all the cases into explicit comparisons, but they may
11098 well not be faster than doing the ABS and one comparison.
11099 But ABS (X) <= C is a range comparison, which becomes a subtraction
11100 and a comparison, and is probably faster. */
11101 if (code
== LE_EXPR
11102 && TREE_CODE (arg1
) == INTEGER_CST
11103 && TREE_CODE (arg0
) == ABS_EXPR
11104 && ! TREE_SIDE_EFFECTS (arg0
)
11105 && (tem
= negate_expr (arg1
)) != 0
11106 && TREE_CODE (tem
) == INTEGER_CST
11107 && !TREE_OVERFLOW (tem
))
11108 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11109 build2 (GE_EXPR
, type
,
11110 TREE_OPERAND (arg0
, 0), tem
),
11111 build2 (LE_EXPR
, type
,
11112 TREE_OPERAND (arg0
, 0), arg1
));
11114 /* Convert ABS_EXPR<x> >= 0 to true. */
11115 strict_overflow_p
= false;
11116 if (code
== GE_EXPR
11117 && (integer_zerop (arg1
)
11118 || (! HONOR_NANS (arg0
)
11119 && real_zerop (arg1
)))
11120 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11122 if (strict_overflow_p
)
11123 fold_overflow_warning (("assuming signed overflow does not occur "
11124 "when simplifying comparison of "
11125 "absolute value and zero"),
11126 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11127 return omit_one_operand_loc (loc
, type
,
11128 constant_boolean_node (true, type
),
11132 /* Convert ABS_EXPR<x> < 0 to false. */
11133 strict_overflow_p
= false;
11134 if (code
== LT_EXPR
11135 && (integer_zerop (arg1
) || real_zerop (arg1
))
11136 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11138 if (strict_overflow_p
)
11139 fold_overflow_warning (("assuming signed overflow does not occur "
11140 "when simplifying comparison of "
11141 "absolute value and zero"),
11142 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11143 return omit_one_operand_loc (loc
, type
,
11144 constant_boolean_node (false, type
),
11148 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11149 and similarly for >= into !=. */
11150 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11151 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11152 && TREE_CODE (arg1
) == LSHIFT_EXPR
11153 && integer_onep (TREE_OPERAND (arg1
, 0)))
11154 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11155 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11156 TREE_OPERAND (arg1
, 1)),
11157 build_zero_cst (TREE_TYPE (arg0
)));
11159 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11160 otherwise Y might be >= # of bits in X's type and thus e.g.
11161 (unsigned char) (1 << Y) for Y 15 might be 0.
11162 If the cast is widening, then 1 << Y should have unsigned type,
11163 otherwise if Y is number of bits in the signed shift type minus 1,
11164 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11165 31 might be 0xffffffff80000000. */
11166 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11167 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11168 && CONVERT_EXPR_P (arg1
)
11169 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11170 && (element_precision (TREE_TYPE (arg1
))
11171 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11172 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11173 || (element_precision (TREE_TYPE (arg1
))
11174 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11175 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11177 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11178 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11179 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11180 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11181 build_zero_cst (TREE_TYPE (arg0
)));
11186 case UNORDERED_EXPR
:
11194 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11196 tree targ0
= strip_float_extensions (arg0
);
11197 tree targ1
= strip_float_extensions (arg1
);
11198 tree newtype
= TREE_TYPE (targ0
);
11200 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11201 newtype
= TREE_TYPE (targ1
);
11203 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11204 return fold_build2_loc (loc
, code
, type
,
11205 fold_convert_loc (loc
, newtype
, targ0
),
11206 fold_convert_loc (loc
, newtype
, targ1
));
11211 case COMPOUND_EXPR
:
11212 /* When pedantic, a compound expression can be neither an lvalue
11213 nor an integer constant expression. */
11214 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11216 /* Don't let (0, 0) be null pointer constant. */
11217 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11218 : fold_convert_loc (loc
, type
, arg1
);
11219 return pedantic_non_lvalue_loc (loc
, tem
);
11222 /* An ASSERT_EXPR should never be passed to fold_binary. */
11223 gcc_unreachable ();
11227 } /* switch (code) */
11230 /* Used by contains_label_[p1]. */
11232 struct contains_label_data
11234 hash_set
<tree
> *pset
;
11235 bool inside_switch_p
;
11238 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11239 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11240 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11243 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
11245 contains_label_data
*d
= (contains_label_data
*) data
;
11246 switch (TREE_CODE (*tp
))
11251 case CASE_LABEL_EXPR
:
11252 if (!d
->inside_switch_p
)
11257 if (!d
->inside_switch_p
)
11259 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
11261 d
->inside_switch_p
= true;
11262 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
11264 d
->inside_switch_p
= false;
11265 *walk_subtrees
= 0;
11270 *walk_subtrees
= 0;
11278 /* Return whether the sub-tree ST contains a label which is accessible from
11279 outside the sub-tree. */
11282 contains_label_p (tree st
)
11284 hash_set
<tree
> pset
;
11285 contains_label_data data
= { &pset
, false };
11286 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
11289 /* Fold a ternary expression of code CODE and type TYPE with operands
11290 OP0, OP1, and OP2. Return the folded expression if folding is
11291 successful. Otherwise, return NULL_TREE. */
11294 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11295 tree op0
, tree op1
, tree op2
)
11298 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11299 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11301 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11302 && TREE_CODE_LENGTH (code
) == 3);
11304 /* If this is a commutative operation, and OP0 is a constant, move it
11305 to OP1 to reduce the number of tests below. */
11306 if (commutative_ternary_tree_code (code
)
11307 && tree_swap_operands_p (op0
, op1
))
11308 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11310 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11314 /* Strip any conversions that don't change the mode. This is safe
11315 for every expression, except for a comparison expression because
11316 its signedness is derived from its operands. So, in the latter
11317 case, only strip conversions that don't change the signedness.
11319 Note that this is done as an internal manipulation within the
11320 constant folder, in order to find the simplest representation of
11321 the arguments so that their form can be studied. In any cases,
11322 the appropriate type conversions should be put back in the tree
11323 that will get out of the constant folder. */
11344 case COMPONENT_REF
:
11345 if (TREE_CODE (arg0
) == CONSTRUCTOR
11346 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11348 unsigned HOST_WIDE_INT idx
;
11350 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11357 case VEC_COND_EXPR
:
11358 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11359 so all simple results must be passed through pedantic_non_lvalue. */
11360 if (TREE_CODE (arg0
) == INTEGER_CST
)
11362 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11363 tem
= integer_zerop (arg0
) ? op2
: op1
;
11364 /* Only optimize constant conditions when the selected branch
11365 has the same type as the COND_EXPR. This avoids optimizing
11366 away "c ? x : throw", where the throw has a void type.
11367 Avoid throwing away that operand which contains label. */
11368 if ((!TREE_SIDE_EFFECTS (unused_op
)
11369 || !contains_label_p (unused_op
))
11370 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11371 || VOID_TYPE_P (type
)))
11372 return pedantic_non_lvalue_loc (loc
, tem
);
11375 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11377 unsigned HOST_WIDE_INT nelts
;
11378 if ((TREE_CODE (arg1
) == VECTOR_CST
11379 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11380 && (TREE_CODE (arg2
) == VECTOR_CST
11381 || TREE_CODE (arg2
) == CONSTRUCTOR
)
11382 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
11384 vec_perm_builder
sel (nelts
, nelts
, 1);
11385 for (unsigned int i
= 0; i
< nelts
; i
++)
11387 tree val
= VECTOR_CST_ELT (arg0
, i
);
11388 if (integer_all_onesp (val
))
11389 sel
.quick_push (i
);
11390 else if (integer_zerop (val
))
11391 sel
.quick_push (nelts
+ i
);
11392 else /* Currently unreachable. */
11395 vec_perm_indices
indices (sel
, 2, nelts
);
11396 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
11397 if (t
!= NULL_TREE
)
11402 /* If we have A op B ? A : C, we may be able to convert this to a
11403 simpler expression, depending on the operation and the values
11404 of B and C. Signed zeros prevent all of these transformations,
11405 for reasons given above each one.
11407 Also try swapping the arguments and inverting the conditional. */
11408 if (COMPARISON_CLASS_P (arg0
)
11409 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
11410 && !HONOR_SIGNED_ZEROS (element_mode (op1
)))
11412 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11417 if (COMPARISON_CLASS_P (arg0
)
11418 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
11419 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11421 location_t loc0
= expr_location_or (arg0
, loc
);
11422 tem
= fold_invert_truthvalue (loc0
, arg0
);
11423 if (tem
&& COMPARISON_CLASS_P (tem
))
11425 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11431 /* If the second operand is simpler than the third, swap them
11432 since that produces better jump optimization results. */
11433 if (truth_value_p (TREE_CODE (arg0
))
11434 && tree_swap_operands_p (op1
, op2
))
11436 location_t loc0
= expr_location_or (arg0
, loc
);
11437 /* See if this can be inverted. If it can't, possibly because
11438 it was a floating-point inequality comparison, don't do
11440 tem
= fold_invert_truthvalue (loc0
, arg0
);
11442 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11445 /* Convert A ? 1 : 0 to simply A. */
11446 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11447 : (integer_onep (op1
)
11448 && !VECTOR_TYPE_P (type
)))
11449 && integer_zerop (op2
)
11450 /* If we try to convert OP0 to our type, the
11451 call to fold will try to move the conversion inside
11452 a COND, which will recurse. In that case, the COND_EXPR
11453 is probably the best choice, so leave it alone. */
11454 && type
== TREE_TYPE (arg0
))
11455 return pedantic_non_lvalue_loc (loc
, arg0
);
11457 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11458 over COND_EXPR in cases such as floating point comparisons. */
11459 if (integer_zerop (op1
)
11460 && code
== COND_EXPR
11461 && integer_onep (op2
)
11462 && !VECTOR_TYPE_P (type
)
11463 && truth_value_p (TREE_CODE (arg0
)))
11464 return pedantic_non_lvalue_loc (loc
,
11465 fold_convert_loc (loc
, type
,
11466 invert_truthvalue_loc (loc
,
11469 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11470 if (TREE_CODE (arg0
) == LT_EXPR
11471 && integer_zerop (TREE_OPERAND (arg0
, 1))
11472 && integer_zerop (op2
)
11473 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11475 /* sign_bit_p looks through both zero and sign extensions,
11476 but for this optimization only sign extensions are
11478 tree tem2
= TREE_OPERAND (arg0
, 0);
11479 while (tem
!= tem2
)
11481 if (TREE_CODE (tem2
) != NOP_EXPR
11482 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11487 tem2
= TREE_OPERAND (tem2
, 0);
11489 /* sign_bit_p only checks ARG1 bits within A's precision.
11490 If <sign bit of A> has wider type than A, bits outside
11491 of A's precision in <sign bit of A> need to be checked.
11492 If they are all 0, this optimization needs to be done
11493 in unsigned A's type, if they are all 1 in signed A's type,
11494 otherwise this can't be done. */
11496 && TYPE_PRECISION (TREE_TYPE (tem
))
11497 < TYPE_PRECISION (TREE_TYPE (arg1
))
11498 && TYPE_PRECISION (TREE_TYPE (tem
))
11499 < TYPE_PRECISION (type
))
11501 int inner_width
, outer_width
;
11504 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11505 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11506 if (outer_width
> TYPE_PRECISION (type
))
11507 outer_width
= TYPE_PRECISION (type
);
11509 wide_int mask
= wi::shifted_mask
11510 (inner_width
, outer_width
- inner_width
, false,
11511 TYPE_PRECISION (TREE_TYPE (arg1
)));
11513 wide_int common
= mask
& wi::to_wide (arg1
);
11514 if (common
== mask
)
11516 tem_type
= signed_type_for (TREE_TYPE (tem
));
11517 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11519 else if (common
== 0)
11521 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11522 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11530 fold_convert_loc (loc
, type
,
11531 fold_build2_loc (loc
, BIT_AND_EXPR
,
11532 TREE_TYPE (tem
), tem
,
11533 fold_convert_loc (loc
,
11538 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11539 already handled above. */
11540 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11541 && integer_onep (TREE_OPERAND (arg0
, 1))
11542 && integer_zerop (op2
)
11543 && integer_pow2p (arg1
))
11545 tree tem
= TREE_OPERAND (arg0
, 0);
11547 if (TREE_CODE (tem
) == RSHIFT_EXPR
11548 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11549 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
11550 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11551 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11552 fold_convert_loc (loc
, type
,
11553 TREE_OPERAND (tem
, 0)),
11557 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11558 is probably obsolete because the first operand should be a
11559 truth value (that's why we have the two cases above), but let's
11560 leave it in until we can confirm this for all front-ends. */
11561 if (integer_zerop (op2
)
11562 && TREE_CODE (arg0
) == NE_EXPR
11563 && integer_zerop (TREE_OPERAND (arg0
, 1))
11564 && integer_pow2p (arg1
)
11565 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11566 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11567 arg1
, OEP_ONLY_CONST
))
11568 return pedantic_non_lvalue_loc (loc
,
11569 fold_convert_loc (loc
, type
,
11570 TREE_OPERAND (arg0
, 0)));
11572 /* Disable the transformations below for vectors, since
11573 fold_binary_op_with_conditional_arg may undo them immediately,
11574 yielding an infinite loop. */
11575 if (code
== VEC_COND_EXPR
)
11578 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11579 if (integer_zerop (op2
)
11580 && truth_value_p (TREE_CODE (arg0
))
11581 && truth_value_p (TREE_CODE (arg1
))
11582 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11583 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11584 : TRUTH_ANDIF_EXPR
,
11585 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
11587 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11588 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11589 && truth_value_p (TREE_CODE (arg0
))
11590 && truth_value_p (TREE_CODE (arg1
))
11591 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11593 location_t loc0
= expr_location_or (arg0
, loc
);
11594 /* Only perform transformation if ARG0 is easily inverted. */
11595 tem
= fold_invert_truthvalue (loc0
, arg0
);
11597 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11600 type
, fold_convert_loc (loc
, type
, tem
),
11604 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11605 if (integer_zerop (arg1
)
11606 && truth_value_p (TREE_CODE (arg0
))
11607 && truth_value_p (TREE_CODE (op2
))
11608 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11610 location_t loc0
= expr_location_or (arg0
, loc
);
11611 /* Only perform transformation if ARG0 is easily inverted. */
11612 tem
= fold_invert_truthvalue (loc0
, arg0
);
11614 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11615 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11616 type
, fold_convert_loc (loc
, type
, tem
),
11620 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11621 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11622 && truth_value_p (TREE_CODE (arg0
))
11623 && truth_value_p (TREE_CODE (op2
))
11624 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11625 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11626 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11627 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11632 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11633 of fold_ternary on them. */
11634 gcc_unreachable ();
11636 case BIT_FIELD_REF
:
11637 if (TREE_CODE (arg0
) == VECTOR_CST
11638 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11639 || (VECTOR_TYPE_P (type
)
11640 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
11641 && tree_fits_uhwi_p (op1
)
11642 && tree_fits_uhwi_p (op2
))
11644 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11645 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11646 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11647 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11650 && (idx
% width
) == 0
11651 && (n
% width
) == 0
11652 && known_le ((idx
+ n
) / width
,
11653 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
11658 if (TREE_CODE (arg0
) == VECTOR_CST
)
11662 tem
= VECTOR_CST_ELT (arg0
, idx
);
11663 if (VECTOR_TYPE_P (type
))
11664 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
11668 tree_vector_builder
vals (type
, n
, 1);
11669 for (unsigned i
= 0; i
< n
; ++i
)
11670 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
11671 return vals
.build ();
11676 /* On constants we can use native encode/interpret to constant
11677 fold (nearly) all BIT_FIELD_REFs. */
11678 if (CONSTANT_CLASS_P (arg0
)
11679 && can_native_interpret_type_p (type
)
11680 && BITS_PER_UNIT
== 8
11681 && tree_fits_uhwi_p (op1
)
11682 && tree_fits_uhwi_p (op2
))
11684 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11685 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11686 /* Limit us to a reasonable amount of work. To relax the
11687 other limitations we need bit-shifting of the buffer
11688 and rounding up the size. */
11689 if (bitpos
% BITS_PER_UNIT
== 0
11690 && bitsize
% BITS_PER_UNIT
== 0
11691 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
11693 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
11694 unsigned HOST_WIDE_INT len
11695 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
11696 bitpos
/ BITS_PER_UNIT
);
11698 && len
* BITS_PER_UNIT
>= bitsize
)
11700 tree v
= native_interpret_expr (type
, b
,
11701 bitsize
/ BITS_PER_UNIT
);
11710 case VEC_PERM_EXPR
:
11711 if (TREE_CODE (arg2
) == VECTOR_CST
)
11713 /* Build a vector of integers from the tree mask. */
11714 vec_perm_builder builder
;
11715 if (!tree_to_vec_perm_builder (&builder
, arg2
))
11718 /* Create a vec_perm_indices for the integer vector. */
11719 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
11720 bool single_arg
= (op0
== op1
);
11721 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
11723 /* Check for cases that fold to OP0 or OP1 in their original
11725 if (sel
.series_p (0, 1, 0, 1))
11727 if (sel
.series_p (0, 1, nelts
, 1))
11732 if (sel
.all_from_input_p (0))
11734 else if (sel
.all_from_input_p (1))
11737 sel
.rotate_inputs (1);
11741 if ((TREE_CODE (op0
) == VECTOR_CST
11742 || TREE_CODE (op0
) == CONSTRUCTOR
)
11743 && (TREE_CODE (op1
) == VECTOR_CST
11744 || TREE_CODE (op1
) == CONSTRUCTOR
))
11746 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11747 if (t
!= NULL_TREE
)
11751 bool changed
= (op0
== op1
&& !single_arg
);
11753 /* Generate a canonical form of the selector. */
11754 if (arg2
== op2
&& sel
.encoding () != builder
)
11756 /* Some targets are deficient and fail to expand a single
11757 argument permutation while still allowing an equivalent
11758 2-argument version. */
11759 if (sel
.ninputs () == 2
11760 || can_vec_perm_const_p (TYPE_MODE (type
), sel
, false))
11761 op2
= vec_perm_indices_to_tree (TREE_TYPE (arg2
), sel
);
11764 vec_perm_indices
sel2 (builder
, 2, nelts
);
11765 if (can_vec_perm_const_p (TYPE_MODE (type
), sel2
, false))
11766 op2
= vec_perm_indices_to_tree (TREE_TYPE (arg2
), sel2
);
11768 /* Not directly supported with either encoding,
11769 so use the preferred form. */
11770 op2
= vec_perm_indices_to_tree (TREE_TYPE (arg2
), sel
);
11776 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11780 case BIT_INSERT_EXPR
:
11781 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11782 if (TREE_CODE (arg0
) == INTEGER_CST
11783 && TREE_CODE (arg1
) == INTEGER_CST
)
11785 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11786 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
11787 wide_int tem
= (wi::to_wide (arg0
)
11788 & wi::shifted_mask (bitpos
, bitsize
, true,
11789 TYPE_PRECISION (type
)));
11791 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
11793 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
11795 else if (TREE_CODE (arg0
) == VECTOR_CST
11796 && CONSTANT_CLASS_P (arg1
)
11797 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
11800 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11801 unsigned HOST_WIDE_INT elsize
11802 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
11803 if (bitpos
% elsize
== 0)
11805 unsigned k
= bitpos
/ elsize
;
11806 unsigned HOST_WIDE_INT nelts
;
11807 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
11809 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
11811 tree_vector_builder
elts (type
, nelts
, 1);
11812 elts
.quick_grow (nelts
);
11813 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
11814 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
11815 return elts
.build ();
11823 } /* switch (code) */
11826 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11827 of an array (or vector). */
11830 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
11832 tree index_type
= NULL_TREE
;
11833 offset_int low_bound
= 0;
11835 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
11837 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
11838 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
11840 /* Static constructors for variably sized objects makes no sense. */
11841 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
11842 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
11843 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
11848 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
11849 TYPE_SIGN (index_type
));
11851 offset_int index
= low_bound
- 1;
11853 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11854 TYPE_SIGN (index_type
));
11856 offset_int max_index
;
11857 unsigned HOST_WIDE_INT cnt
;
11860 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
11862 /* Array constructor might explicitly set index, or specify a range,
11863 or leave index NULL meaning that it is next index after previous
11867 if (TREE_CODE (cfield
) == INTEGER_CST
)
11868 max_index
= index
= wi::to_offset (cfield
);
11871 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
11872 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
11873 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
11880 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11881 TYPE_SIGN (index_type
));
11885 /* Do we have match? */
11886 if (wi::cmpu (access_index
, index
) >= 0
11887 && wi::cmpu (access_index
, max_index
) <= 0)
11893 /* Perform constant folding and related simplification of EXPR.
11894 The related simplifications include x*1 => x, x*0 => 0, etc.,
11895 and application of the associative law.
11896 NOP_EXPR conversions may be removed freely (as long as we
11897 are careful not to change the type of the overall expression).
11898 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11899 but we can constant-fold them if they have constant operands. */
11901 #ifdef ENABLE_FOLD_CHECKING
11902 # define fold(x) fold_1 (x)
11903 static tree
fold_1 (tree
);
11909 const tree t
= expr
;
11910 enum tree_code code
= TREE_CODE (t
);
11911 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11913 location_t loc
= EXPR_LOCATION (expr
);
11915 /* Return right away if a constant. */
11916 if (kind
== tcc_constant
)
11919 /* CALL_EXPR-like objects with variable numbers of operands are
11920 treated specially. */
11921 if (kind
== tcc_vl_exp
)
11923 if (code
== CALL_EXPR
)
11925 tem
= fold_call_expr (loc
, expr
, false);
11926 return tem
? tem
: expr
;
11931 if (IS_EXPR_CODE_CLASS (kind
))
11933 tree type
= TREE_TYPE (t
);
11934 tree op0
, op1
, op2
;
11936 switch (TREE_CODE_LENGTH (code
))
11939 op0
= TREE_OPERAND (t
, 0);
11940 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11941 return tem
? tem
: expr
;
11943 op0
= TREE_OPERAND (t
, 0);
11944 op1
= TREE_OPERAND (t
, 1);
11945 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11946 return tem
? tem
: expr
;
11948 op0
= TREE_OPERAND (t
, 0);
11949 op1
= TREE_OPERAND (t
, 1);
11950 op2
= TREE_OPERAND (t
, 2);
11951 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11952 return tem
? tem
: expr
;
11962 tree op0
= TREE_OPERAND (t
, 0);
11963 tree op1
= TREE_OPERAND (t
, 1);
11965 if (TREE_CODE (op1
) == INTEGER_CST
11966 && TREE_CODE (op0
) == CONSTRUCTOR
11967 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11969 tree val
= get_array_ctor_element_at_index (op0
,
11970 wi::to_offset (op1
));
11978 /* Return a VECTOR_CST if possible. */
11981 tree type
= TREE_TYPE (t
);
11982 if (TREE_CODE (type
) != VECTOR_TYPE
)
11987 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
11988 if (! CONSTANT_CLASS_P (val
))
11991 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
11995 return fold (DECL_INITIAL (t
));
11999 } /* switch (code) */
12002 #ifdef ENABLE_FOLD_CHECKING
12005 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
12006 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
12007 static void fold_check_failed (const_tree
, const_tree
);
12008 void print_fold_checksum (const_tree
);
12010 /* When --enable-checking=fold, compute a digest of expr before
12011 and after actual fold call to see if fold did not accidentally
12012 change original expr. */
12018 struct md5_ctx ctx
;
12019 unsigned char checksum_before
[16], checksum_after
[16];
12020 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12022 md5_init_ctx (&ctx
);
12023 fold_checksum_tree (expr
, &ctx
, &ht
);
12024 md5_finish_ctx (&ctx
, checksum_before
);
12027 ret
= fold_1 (expr
);
12029 md5_init_ctx (&ctx
);
12030 fold_checksum_tree (expr
, &ctx
, &ht
);
12031 md5_finish_ctx (&ctx
, checksum_after
);
12033 if (memcmp (checksum_before
, checksum_after
, 16))
12034 fold_check_failed (expr
, ret
);
12040 print_fold_checksum (const_tree expr
)
12042 struct md5_ctx ctx
;
12043 unsigned char checksum
[16], cnt
;
12044 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12046 md5_init_ctx (&ctx
);
12047 fold_checksum_tree (expr
, &ctx
, &ht
);
12048 md5_finish_ctx (&ctx
, checksum
);
12049 for (cnt
= 0; cnt
< 16; ++cnt
)
12050 fprintf (stderr
, "%02x", checksum
[cnt
]);
12051 putc ('\n', stderr
);
12055 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12057 internal_error ("fold check: original tree changed by fold");
12061 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12062 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12064 const tree_node
**slot
;
12065 enum tree_code code
;
12066 union tree_node buf
;
12072 slot
= ht
->find_slot (expr
, INSERT
);
12076 code
= TREE_CODE (expr
);
12077 if (TREE_CODE_CLASS (code
) == tcc_declaration
12078 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12080 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12081 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12082 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12083 buf
.decl_with_vis
.symtab_node
= NULL
;
12084 expr
= (tree
) &buf
;
12086 else if (TREE_CODE_CLASS (code
) == tcc_type
12087 && (TYPE_POINTER_TO (expr
)
12088 || TYPE_REFERENCE_TO (expr
)
12089 || TYPE_CACHED_VALUES_P (expr
)
12090 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12091 || TYPE_NEXT_VARIANT (expr
)
12092 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
12094 /* Allow these fields to be modified. */
12096 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12097 expr
= tmp
= (tree
) &buf
;
12098 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12099 TYPE_POINTER_TO (tmp
) = NULL
;
12100 TYPE_REFERENCE_TO (tmp
) = NULL
;
12101 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12102 TYPE_ALIAS_SET (tmp
) = -1;
12103 if (TYPE_CACHED_VALUES_P (tmp
))
12105 TYPE_CACHED_VALUES_P (tmp
) = 0;
12106 TYPE_CACHED_VALUES (tmp
) = NULL
;
12109 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12110 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12111 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12112 if (TREE_CODE_CLASS (code
) != tcc_type
12113 && TREE_CODE_CLASS (code
) != tcc_declaration
12114 && code
!= TREE_LIST
12115 && code
!= SSA_NAME
12116 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12117 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12118 switch (TREE_CODE_CLASS (code
))
12124 md5_process_bytes (TREE_STRING_POINTER (expr
),
12125 TREE_STRING_LENGTH (expr
), ctx
);
12128 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12129 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12132 len
= vector_cst_encoded_nelts (expr
);
12133 for (i
= 0; i
< len
; ++i
)
12134 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
12140 case tcc_exceptional
:
12144 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12145 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12146 expr
= TREE_CHAIN (expr
);
12147 goto recursive_label
;
12150 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12151 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12157 case tcc_expression
:
12158 case tcc_reference
:
12159 case tcc_comparison
:
12162 case tcc_statement
:
12164 len
= TREE_OPERAND_LENGTH (expr
);
12165 for (i
= 0; i
< len
; ++i
)
12166 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12168 case tcc_declaration
:
12169 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12170 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12171 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12173 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12174 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12175 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12176 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12177 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12180 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12182 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12184 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12185 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12187 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12191 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12192 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12193 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12194 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12195 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12196 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12197 if (INTEGRAL_TYPE_P (expr
)
12198 || SCALAR_FLOAT_TYPE_P (expr
))
12200 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12201 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12203 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12204 if (TREE_CODE (expr
) == RECORD_TYPE
12205 || TREE_CODE (expr
) == UNION_TYPE
12206 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12207 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12208 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12215 /* Helper function for outputting the checksum of a tree T. When
12216 debugging with gdb, you can "define mynext" to be "next" followed
12217 by "call debug_fold_checksum (op0)", then just trace down till the
12220 DEBUG_FUNCTION
void
12221 debug_fold_checksum (const_tree t
)
12224 unsigned char checksum
[16];
12225 struct md5_ctx ctx
;
12226 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12228 md5_init_ctx (&ctx
);
12229 fold_checksum_tree (t
, &ctx
, &ht
);
12230 md5_finish_ctx (&ctx
, checksum
);
12233 for (i
= 0; i
< 16; i
++)
12234 fprintf (stderr
, "%d ", checksum
[i
]);
12236 fprintf (stderr
, "\n");
12241 /* Fold a unary tree expression with code CODE of type TYPE with an
12242 operand OP0. LOC is the location of the resulting expression.
12243 Return a folded expression if successful. Otherwise, return a tree
12244 expression with code CODE of type TYPE with an operand OP0. */
12247 fold_build1_loc (location_t loc
,
12248 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12251 #ifdef ENABLE_FOLD_CHECKING
12252 unsigned char checksum_before
[16], checksum_after
[16];
12253 struct md5_ctx ctx
;
12254 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12256 md5_init_ctx (&ctx
);
12257 fold_checksum_tree (op0
, &ctx
, &ht
);
12258 md5_finish_ctx (&ctx
, checksum_before
);
12262 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12264 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12266 #ifdef ENABLE_FOLD_CHECKING
12267 md5_init_ctx (&ctx
);
12268 fold_checksum_tree (op0
, &ctx
, &ht
);
12269 md5_finish_ctx (&ctx
, checksum_after
);
12271 if (memcmp (checksum_before
, checksum_after
, 16))
12272 fold_check_failed (op0
, tem
);
12277 /* Fold a binary tree expression with code CODE of type TYPE with
12278 operands OP0 and OP1. LOC is the location of the resulting
12279 expression. Return a folded expression if successful. Otherwise,
12280 return a tree expression with code CODE of type TYPE with operands
12284 fold_build2_loc (location_t loc
,
12285 enum tree_code code
, tree type
, tree op0
, tree op1
12289 #ifdef ENABLE_FOLD_CHECKING
12290 unsigned char checksum_before_op0
[16],
12291 checksum_before_op1
[16],
12292 checksum_after_op0
[16],
12293 checksum_after_op1
[16];
12294 struct md5_ctx ctx
;
12295 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12297 md5_init_ctx (&ctx
);
12298 fold_checksum_tree (op0
, &ctx
, &ht
);
12299 md5_finish_ctx (&ctx
, checksum_before_op0
);
12302 md5_init_ctx (&ctx
);
12303 fold_checksum_tree (op1
, &ctx
, &ht
);
12304 md5_finish_ctx (&ctx
, checksum_before_op1
);
12308 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12310 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12312 #ifdef ENABLE_FOLD_CHECKING
12313 md5_init_ctx (&ctx
);
12314 fold_checksum_tree (op0
, &ctx
, &ht
);
12315 md5_finish_ctx (&ctx
, checksum_after_op0
);
12318 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12319 fold_check_failed (op0
, tem
);
12321 md5_init_ctx (&ctx
);
12322 fold_checksum_tree (op1
, &ctx
, &ht
);
12323 md5_finish_ctx (&ctx
, checksum_after_op1
);
12325 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12326 fold_check_failed (op1
, tem
);
12331 /* Fold a ternary tree expression with code CODE of type TYPE with
12332 operands OP0, OP1, and OP2. Return a folded expression if
12333 successful. Otherwise, return a tree expression with code CODE of
12334 type TYPE with operands OP0, OP1, and OP2. */
12337 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
12338 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12341 #ifdef ENABLE_FOLD_CHECKING
12342 unsigned char checksum_before_op0
[16],
12343 checksum_before_op1
[16],
12344 checksum_before_op2
[16],
12345 checksum_after_op0
[16],
12346 checksum_after_op1
[16],
12347 checksum_after_op2
[16];
12348 struct md5_ctx ctx
;
12349 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12351 md5_init_ctx (&ctx
);
12352 fold_checksum_tree (op0
, &ctx
, &ht
);
12353 md5_finish_ctx (&ctx
, checksum_before_op0
);
12356 md5_init_ctx (&ctx
);
12357 fold_checksum_tree (op1
, &ctx
, &ht
);
12358 md5_finish_ctx (&ctx
, checksum_before_op1
);
12361 md5_init_ctx (&ctx
);
12362 fold_checksum_tree (op2
, &ctx
, &ht
);
12363 md5_finish_ctx (&ctx
, checksum_before_op2
);
12367 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12368 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12370 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12372 #ifdef ENABLE_FOLD_CHECKING
12373 md5_init_ctx (&ctx
);
12374 fold_checksum_tree (op0
, &ctx
, &ht
);
12375 md5_finish_ctx (&ctx
, checksum_after_op0
);
12378 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12379 fold_check_failed (op0
, tem
);
12381 md5_init_ctx (&ctx
);
12382 fold_checksum_tree (op1
, &ctx
, &ht
);
12383 md5_finish_ctx (&ctx
, checksum_after_op1
);
12386 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12387 fold_check_failed (op1
, tem
);
12389 md5_init_ctx (&ctx
);
12390 fold_checksum_tree (op2
, &ctx
, &ht
);
12391 md5_finish_ctx (&ctx
, checksum_after_op2
);
12393 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12394 fold_check_failed (op2
, tem
);
12399 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12400 arguments in ARGARRAY, and a null static chain.
12401 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12402 of type TYPE from the given operands as constructed by build_call_array. */
12405 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12406 int nargs
, tree
*argarray
)
12409 #ifdef ENABLE_FOLD_CHECKING
12410 unsigned char checksum_before_fn
[16],
12411 checksum_before_arglist
[16],
12412 checksum_after_fn
[16],
12413 checksum_after_arglist
[16];
12414 struct md5_ctx ctx
;
12415 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12418 md5_init_ctx (&ctx
);
12419 fold_checksum_tree (fn
, &ctx
, &ht
);
12420 md5_finish_ctx (&ctx
, checksum_before_fn
);
12423 md5_init_ctx (&ctx
);
12424 for (i
= 0; i
< nargs
; i
++)
12425 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12426 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12430 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12432 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12434 #ifdef ENABLE_FOLD_CHECKING
12435 md5_init_ctx (&ctx
);
12436 fold_checksum_tree (fn
, &ctx
, &ht
);
12437 md5_finish_ctx (&ctx
, checksum_after_fn
);
12440 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12441 fold_check_failed (fn
, tem
);
12443 md5_init_ctx (&ctx
);
12444 for (i
= 0; i
< nargs
; i
++)
12445 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12446 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12448 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12449 fold_check_failed (NULL_TREE
, tem
);
12454 /* Perform constant folding and related simplification of initializer
12455 expression EXPR. These behave identically to "fold_buildN" but ignore
12456 potential run-time traps and exceptions that fold must preserve. */
12458 #define START_FOLD_INIT \
12459 int saved_signaling_nans = flag_signaling_nans;\
12460 int saved_trapping_math = flag_trapping_math;\
12461 int saved_rounding_math = flag_rounding_math;\
12462 int saved_trapv = flag_trapv;\
12463 int saved_folding_initializer = folding_initializer;\
12464 flag_signaling_nans = 0;\
12465 flag_trapping_math = 0;\
12466 flag_rounding_math = 0;\
12468 folding_initializer = 1;
12470 #define END_FOLD_INIT \
12471 flag_signaling_nans = saved_signaling_nans;\
12472 flag_trapping_math = saved_trapping_math;\
12473 flag_rounding_math = saved_rounding_math;\
12474 flag_trapv = saved_trapv;\
12475 folding_initializer = saved_folding_initializer;
12478 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12479 tree type
, tree op
)
12484 result
= fold_build1_loc (loc
, code
, type
, op
);
12491 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12492 tree type
, tree op0
, tree op1
)
12497 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12504 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12505 int nargs
, tree
*argarray
)
12510 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12516 #undef START_FOLD_INIT
12517 #undef END_FOLD_INIT
12519 /* Determine if first argument is a multiple of second argument. Return 0 if
12520 it is not, or we cannot easily determined it to be.
12522 An example of the sort of thing we care about (at this point; this routine
12523 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12524 fold cases do now) is discovering that
12526 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12532 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12534 This code also handles discovering that
12536 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12538 is a multiple of 8 so we don't have to worry about dealing with a
12539 possible remainder.
12541 Note that we *look* inside a SAVE_EXPR only to determine how it was
12542 calculated; it is not safe for fold to do much of anything else with the
12543 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12544 at run time. For example, the latter example above *cannot* be implemented
12545 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12546 evaluation time of the original SAVE_EXPR is not necessarily the same at
12547 the time the new expression is evaluated. The only optimization of this
12548 sort that would be valid is changing
12550 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12554 SAVE_EXPR (I) * SAVE_EXPR (J)
12556 (where the same SAVE_EXPR (J) is used in the original and the
12557 transformed version). */
12560 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12565 if (operand_equal_p (top
, bottom
, 0))
12568 if (TREE_CODE (type
) != INTEGER_TYPE
)
12571 switch (TREE_CODE (top
))
12574 /* Bitwise and provides a power of two multiple. If the mask is
12575 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12576 if (!integer_pow2p (bottom
))
12578 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12579 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12582 if (TREE_CODE (bottom
) == INTEGER_CST
)
12584 op1
= TREE_OPERAND (top
, 0);
12585 op2
= TREE_OPERAND (top
, 1);
12586 if (TREE_CODE (op1
) == INTEGER_CST
)
12587 std::swap (op1
, op2
);
12588 if (TREE_CODE (op2
) == INTEGER_CST
)
12590 if (multiple_of_p (type
, op2
, bottom
))
12592 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12593 if (multiple_of_p (type
, bottom
, op2
))
12595 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
12596 wi::to_widest (op2
));
12597 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
12599 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
12600 return multiple_of_p (type
, op1
, op2
);
12603 return multiple_of_p (type
, op1
, bottom
);
12606 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12607 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12610 /* It is impossible to prove if op0 - op1 is multiple of bottom
12611 precisely, so be conservative here checking if both op0 and op1
12612 are multiple of bottom. Note we check the second operand first
12613 since it's usually simpler. */
12614 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12615 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12618 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12619 as op0 - 3 if the expression has unsigned type. For example,
12620 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12621 op1
= TREE_OPERAND (top
, 1);
12622 if (TYPE_UNSIGNED (type
)
12623 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
12624 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
12625 return (multiple_of_p (type
, op1
, bottom
)
12626 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12629 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12631 op1
= TREE_OPERAND (top
, 1);
12632 /* const_binop may not detect overflow correctly,
12633 so check for it explicitly here. */
12634 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
12636 && (t1
= fold_convert (type
,
12637 const_binop (LSHIFT_EXPR
, size_one_node
,
12639 && !TREE_OVERFLOW (t1
))
12640 return multiple_of_p (type
, t1
, bottom
);
12645 /* Can't handle conversions from non-integral or wider integral type. */
12646 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12647 || (TYPE_PRECISION (type
)
12648 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12654 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12657 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12658 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12661 if (TREE_CODE (bottom
) != INTEGER_CST
12662 || integer_zerop (bottom
)
12663 || (TYPE_UNSIGNED (type
)
12664 && (tree_int_cst_sgn (top
) < 0
12665 || tree_int_cst_sgn (bottom
) < 0)))
12667 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12671 if (TREE_CODE (bottom
) == INTEGER_CST
12672 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
12673 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
12675 enum tree_code code
= gimple_assign_rhs_code (stmt
);
12677 /* Check for special cases to see if top is defined as multiple
12680 top = (X & ~(bottom - 1) ; bottom is power of 2
12686 if (code
== BIT_AND_EXPR
12687 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12688 && TREE_CODE (op2
) == INTEGER_CST
12689 && integer_pow2p (bottom
)
12690 && wi::multiple_of_p (wi::to_widest (op2
),
12691 wi::to_widest (bottom
), UNSIGNED
))
12694 op1
= gimple_assign_rhs1 (stmt
);
12695 if (code
== MINUS_EXPR
12696 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12697 && TREE_CODE (op2
) == SSA_NAME
12698 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
12699 && gimple_code (stmt
) == GIMPLE_ASSIGN
12700 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
12701 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
12702 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
12709 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
12710 return multiple_p (wi::to_poly_widest (top
),
12711 wi::to_poly_widest (bottom
));
12717 #define tree_expr_nonnegative_warnv_p(X, Y) \
12718 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12720 #define RECURSE(X) \
12721 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12723 /* Return true if CODE or TYPE is known to be non-negative. */
12726 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12728 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12729 && truth_value_p (code
))
12730 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12731 have a signed:1 type (where the value is -1 and 0). */
12736 /* Return true if (CODE OP0) is known to be non-negative. If the return
12737 value is based on the assumption that signed overflow is undefined,
12738 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12739 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12742 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12743 bool *strict_overflow_p
, int depth
)
12745 if (TYPE_UNSIGNED (type
))
12751 /* We can't return 1 if flag_wrapv is set because
12752 ABS_EXPR<INT_MIN> = INT_MIN. */
12753 if (!ANY_INTEGRAL_TYPE_P (type
))
12755 if (TYPE_OVERFLOW_UNDEFINED (type
))
12757 *strict_overflow_p
= true;
12762 case NON_LVALUE_EXPR
:
12764 case FIX_TRUNC_EXPR
:
12765 return RECURSE (op0
);
12769 tree inner_type
= TREE_TYPE (op0
);
12770 tree outer_type
= type
;
12772 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12774 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12775 return RECURSE (op0
);
12776 if (INTEGRAL_TYPE_P (inner_type
))
12778 if (TYPE_UNSIGNED (inner_type
))
12780 return RECURSE (op0
);
12783 else if (INTEGRAL_TYPE_P (outer_type
))
12785 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12786 return RECURSE (op0
);
12787 if (INTEGRAL_TYPE_P (inner_type
))
12788 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12789 && TYPE_UNSIGNED (inner_type
);
12795 return tree_simple_nonnegative_warnv_p (code
, type
);
12798 /* We don't know sign of `t', so be conservative and return false. */
12802 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12803 value is based on the assumption that signed overflow is undefined,
12804 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12805 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12808 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12809 tree op1
, bool *strict_overflow_p
,
12812 if (TYPE_UNSIGNED (type
))
12817 case POINTER_PLUS_EXPR
:
12819 if (FLOAT_TYPE_P (type
))
12820 return RECURSE (op0
) && RECURSE (op1
);
12822 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12823 both unsigned and at least 2 bits shorter than the result. */
12824 if (TREE_CODE (type
) == INTEGER_TYPE
12825 && TREE_CODE (op0
) == NOP_EXPR
12826 && TREE_CODE (op1
) == NOP_EXPR
)
12828 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12829 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12830 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12831 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12833 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12834 TYPE_PRECISION (inner2
)) + 1;
12835 return prec
< TYPE_PRECISION (type
);
12841 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12843 /* x * x is always non-negative for floating point x
12844 or without overflow. */
12845 if (operand_equal_p (op0
, op1
, 0)
12846 || (RECURSE (op0
) && RECURSE (op1
)))
12848 if (ANY_INTEGRAL_TYPE_P (type
)
12849 && TYPE_OVERFLOW_UNDEFINED (type
))
12850 *strict_overflow_p
= true;
12855 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12856 both unsigned and their total bits is shorter than the result. */
12857 if (TREE_CODE (type
) == INTEGER_TYPE
12858 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12859 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12861 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12862 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12864 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12865 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12868 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12869 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12871 if (TREE_CODE (op0
) == INTEGER_CST
)
12872 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12874 if (TREE_CODE (op1
) == INTEGER_CST
)
12875 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12877 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12878 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12880 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12881 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12882 : TYPE_PRECISION (inner0
);
12884 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12885 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12886 : TYPE_PRECISION (inner1
);
12888 return precision0
+ precision1
< TYPE_PRECISION (type
);
12895 return RECURSE (op0
) || RECURSE (op1
);
12901 case TRUNC_DIV_EXPR
:
12902 case CEIL_DIV_EXPR
:
12903 case FLOOR_DIV_EXPR
:
12904 case ROUND_DIV_EXPR
:
12905 return RECURSE (op0
) && RECURSE (op1
);
12907 case TRUNC_MOD_EXPR
:
12908 return RECURSE (op0
);
12910 case FLOOR_MOD_EXPR
:
12911 return RECURSE (op1
);
12913 case CEIL_MOD_EXPR
:
12914 case ROUND_MOD_EXPR
:
12916 return tree_simple_nonnegative_warnv_p (code
, type
);
12919 /* We don't know sign of `t', so be conservative and return false. */
12923 /* Return true if T is known to be non-negative. If the return
12924 value is based on the assumption that signed overflow is undefined,
12925 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12926 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12929 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12931 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12934 switch (TREE_CODE (t
))
12937 return tree_int_cst_sgn (t
) >= 0;
12940 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12943 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12946 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12949 /* Limit the depth of recursion to avoid quadratic behavior.
12950 This is expected to catch almost all occurrences in practice.
12951 If this code misses important cases that unbounded recursion
12952 would not, passes that need this information could be revised
12953 to provide it through dataflow propagation. */
12954 return (!name_registered_for_update_p (t
)
12955 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12956 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12957 strict_overflow_p
, depth
));
12960 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12964 /* Return true if T is known to be non-negative. If the return
12965 value is based on the assumption that signed overflow is undefined,
12966 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12967 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12970 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
12971 bool *strict_overflow_p
, int depth
)
12992 case CFN_BUILT_IN_BSWAP32
:
12993 case CFN_BUILT_IN_BSWAP64
:
12999 /* sqrt(-0.0) is -0.0. */
13000 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
13002 return RECURSE (arg0
);
13030 CASE_CFN_NEARBYINT
:
13031 CASE_CFN_NEARBYINT_FN
:
13040 CASE_CFN_SIGNIFICAND
:
13045 /* True if the 1st argument is nonnegative. */
13046 return RECURSE (arg0
);
13050 /* True if the 1st OR 2nd arguments are nonnegative. */
13051 return RECURSE (arg0
) || RECURSE (arg1
);
13055 /* True if the 1st AND 2nd arguments are nonnegative. */
13056 return RECURSE (arg0
) && RECURSE (arg1
);
13059 CASE_CFN_COPYSIGN_FN
:
13060 /* True if the 2nd argument is nonnegative. */
13061 return RECURSE (arg1
);
13064 /* True if the 1st argument is nonnegative or the second
13065 argument is an even integer. */
13066 if (TREE_CODE (arg1
) == INTEGER_CST
13067 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
13069 return RECURSE (arg0
);
13072 /* True if the 1st argument is nonnegative or the second
13073 argument is an even integer valued real. */
13074 if (TREE_CODE (arg1
) == REAL_CST
)
13079 c
= TREE_REAL_CST (arg1
);
13080 n
= real_to_integer (&c
);
13083 REAL_VALUE_TYPE cint
;
13084 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
13085 if (real_identical (&c
, &cint
))
13089 return RECURSE (arg0
);
13094 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
13097 /* Return true if T is known to be non-negative. If the return
13098 value is based on the assumption that signed overflow is undefined,
13099 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13100 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13103 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13105 enum tree_code code
= TREE_CODE (t
);
13106 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13113 tree temp
= TARGET_EXPR_SLOT (t
);
13114 t
= TARGET_EXPR_INITIAL (t
);
13116 /* If the initializer is non-void, then it's a normal expression
13117 that will be assigned to the slot. */
13118 if (!VOID_TYPE_P (t
))
13119 return RECURSE (t
);
13121 /* Otherwise, the initializer sets the slot in some way. One common
13122 way is an assignment statement at the end of the initializer. */
13125 if (TREE_CODE (t
) == BIND_EXPR
)
13126 t
= expr_last (BIND_EXPR_BODY (t
));
13127 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13128 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13129 t
= expr_last (TREE_OPERAND (t
, 0));
13130 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13135 if (TREE_CODE (t
) == MODIFY_EXPR
13136 && TREE_OPERAND (t
, 0) == temp
)
13137 return RECURSE (TREE_OPERAND (t
, 1));
13144 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13145 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13147 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13148 get_call_combined_fn (t
),
13151 strict_overflow_p
, depth
);
13153 case COMPOUND_EXPR
:
13155 return RECURSE (TREE_OPERAND (t
, 1));
13158 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13161 return RECURSE (TREE_OPERAND (t
, 0));
13164 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13169 #undef tree_expr_nonnegative_warnv_p
13171 /* Return true if T is known to be non-negative. If the return
13172 value is based on the assumption that signed overflow is undefined,
13173 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13174 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13177 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13179 enum tree_code code
;
13180 if (t
== error_mark_node
)
13183 code
= TREE_CODE (t
);
13184 switch (TREE_CODE_CLASS (code
))
13187 case tcc_comparison
:
13188 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13190 TREE_OPERAND (t
, 0),
13191 TREE_OPERAND (t
, 1),
13192 strict_overflow_p
, depth
);
13195 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13197 TREE_OPERAND (t
, 0),
13198 strict_overflow_p
, depth
);
13201 case tcc_declaration
:
13202 case tcc_reference
:
13203 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13211 case TRUTH_AND_EXPR
:
13212 case TRUTH_OR_EXPR
:
13213 case TRUTH_XOR_EXPR
:
13214 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13216 TREE_OPERAND (t
, 0),
13217 TREE_OPERAND (t
, 1),
13218 strict_overflow_p
, depth
);
13219 case TRUTH_NOT_EXPR
:
13220 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13222 TREE_OPERAND (t
, 0),
13223 strict_overflow_p
, depth
);
13230 case WITH_SIZE_EXPR
:
13232 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13235 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13239 /* Return true if `t' is known to be non-negative. Handle warnings
13240 about undefined signed overflow. */
13243 tree_expr_nonnegative_p (tree t
)
13245 bool ret
, strict_overflow_p
;
13247 strict_overflow_p
= false;
13248 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13249 if (strict_overflow_p
)
13250 fold_overflow_warning (("assuming signed overflow does not occur when "
13251 "determining that expression is always "
13253 WARN_STRICT_OVERFLOW_MISC
);
13258 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13259 For floating point we further ensure that T is not denormal.
13260 Similar logic is present in nonzero_address in rtlanal.h.
13262 If the return value is based on the assumption that signed overflow
13263 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13264 change *STRICT_OVERFLOW_P. */
13267 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13268 bool *strict_overflow_p
)
13273 return tree_expr_nonzero_warnv_p (op0
,
13274 strict_overflow_p
);
13278 tree inner_type
= TREE_TYPE (op0
);
13279 tree outer_type
= type
;
13281 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13282 && tree_expr_nonzero_warnv_p (op0
,
13283 strict_overflow_p
));
13287 case NON_LVALUE_EXPR
:
13288 return tree_expr_nonzero_warnv_p (op0
,
13289 strict_overflow_p
);
13298 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13299 For floating point we further ensure that T is not denormal.
13300 Similar logic is present in nonzero_address in rtlanal.h.
13302 If the return value is based on the assumption that signed overflow
13303 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13304 change *STRICT_OVERFLOW_P. */
13307 tree_binary_nonzero_warnv_p (enum tree_code code
,
13310 tree op1
, bool *strict_overflow_p
)
13312 bool sub_strict_overflow_p
;
13315 case POINTER_PLUS_EXPR
:
13317 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13319 /* With the presence of negative values it is hard
13320 to say something. */
13321 sub_strict_overflow_p
= false;
13322 if (!tree_expr_nonnegative_warnv_p (op0
,
13323 &sub_strict_overflow_p
)
13324 || !tree_expr_nonnegative_warnv_p (op1
,
13325 &sub_strict_overflow_p
))
13327 /* One of operands must be positive and the other non-negative. */
13328 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13329 overflows, on a twos-complement machine the sum of two
13330 nonnegative numbers can never be zero. */
13331 return (tree_expr_nonzero_warnv_p (op0
,
13333 || tree_expr_nonzero_warnv_p (op1
,
13334 strict_overflow_p
));
13339 if (TYPE_OVERFLOW_UNDEFINED (type
))
13341 if (tree_expr_nonzero_warnv_p (op0
,
13343 && tree_expr_nonzero_warnv_p (op1
,
13344 strict_overflow_p
))
13346 *strict_overflow_p
= true;
13353 sub_strict_overflow_p
= false;
13354 if (tree_expr_nonzero_warnv_p (op0
,
13355 &sub_strict_overflow_p
)
13356 && tree_expr_nonzero_warnv_p (op1
,
13357 &sub_strict_overflow_p
))
13359 if (sub_strict_overflow_p
)
13360 *strict_overflow_p
= true;
13365 sub_strict_overflow_p
= false;
13366 if (tree_expr_nonzero_warnv_p (op0
,
13367 &sub_strict_overflow_p
))
13369 if (sub_strict_overflow_p
)
13370 *strict_overflow_p
= true;
13372 /* When both operands are nonzero, then MAX must be too. */
13373 if (tree_expr_nonzero_warnv_p (op1
,
13374 strict_overflow_p
))
13377 /* MAX where operand 0 is positive is positive. */
13378 return tree_expr_nonnegative_warnv_p (op0
,
13379 strict_overflow_p
);
13381 /* MAX where operand 1 is positive is positive. */
13382 else if (tree_expr_nonzero_warnv_p (op1
,
13383 &sub_strict_overflow_p
)
13384 && tree_expr_nonnegative_warnv_p (op1
,
13385 &sub_strict_overflow_p
))
13387 if (sub_strict_overflow_p
)
13388 *strict_overflow_p
= true;
13394 return (tree_expr_nonzero_warnv_p (op1
,
13396 || tree_expr_nonzero_warnv_p (op0
,
13397 strict_overflow_p
));
13406 /* Return true when T is an address and is known to be nonzero.
13407 For floating point we further ensure that T is not denormal.
13408 Similar logic is present in nonzero_address in rtlanal.h.
13410 If the return value is based on the assumption that signed overflow
13411 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13412 change *STRICT_OVERFLOW_P. */
13415 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13417 bool sub_strict_overflow_p
;
13418 switch (TREE_CODE (t
))
13421 return !integer_zerop (t
);
13425 tree base
= TREE_OPERAND (t
, 0);
13427 if (!DECL_P (base
))
13428 base
= get_base_address (base
);
13430 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
13431 base
= TARGET_EXPR_SLOT (base
);
13436 /* For objects in symbol table check if we know they are non-zero.
13437 Don't do anything for variables and functions before symtab is built;
13438 it is quite possible that they will be declared weak later. */
13439 int nonzero_addr
= maybe_nonzero_address (base
);
13440 if (nonzero_addr
>= 0)
13441 return nonzero_addr
;
13443 /* Constants are never weak. */
13444 if (CONSTANT_CLASS_P (base
))
13451 sub_strict_overflow_p
= false;
13452 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13453 &sub_strict_overflow_p
)
13454 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13455 &sub_strict_overflow_p
))
13457 if (sub_strict_overflow_p
)
13458 *strict_overflow_p
= true;
13464 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
13466 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
13474 #define integer_valued_real_p(X) \
13475 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13477 #define RECURSE(X) \
13478 ((integer_valued_real_p) (X, depth + 1))
13480 /* Return true if the floating point result of (CODE OP0) has an
13481 integer value. We also allow +Inf, -Inf and NaN to be considered
13482 integer values. Return false for signaling NaN.
13484 DEPTH is the current nesting depth of the query. */
13487 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13495 return RECURSE (op0
);
13499 tree type
= TREE_TYPE (op0
);
13500 if (TREE_CODE (type
) == INTEGER_TYPE
)
13502 if (TREE_CODE (type
) == REAL_TYPE
)
13503 return RECURSE (op0
);
13513 /* Return true if the floating point result of (CODE OP0 OP1) has an
13514 integer value. We also allow +Inf, -Inf and NaN to be considered
13515 integer values. Return false for signaling NaN.
13517 DEPTH is the current nesting depth of the query. */
13520 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13529 return RECURSE (op0
) && RECURSE (op1
);
13537 /* Return true if the floating point result of calling FNDECL with arguments
13538 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13539 considered integer values. Return false for signaling NaN. If FNDECL
13540 takes fewer than 2 arguments, the remaining ARGn are null.
13542 DEPTH is the current nesting depth of the query. */
13545 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13553 CASE_CFN_NEARBYINT
:
13554 CASE_CFN_NEARBYINT_FN
:
13567 return RECURSE (arg0
) && RECURSE (arg1
);
13575 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13576 has an integer value. We also allow +Inf, -Inf and NaN to be
13577 considered integer values. Return false for signaling NaN.
13579 DEPTH is the current nesting depth of the query. */
13582 integer_valued_real_single_p (tree t
, int depth
)
13584 switch (TREE_CODE (t
))
13587 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13590 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13593 /* Limit the depth of recursion to avoid quadratic behavior.
13594 This is expected to catch almost all occurrences in practice.
13595 If this code misses important cases that unbounded recursion
13596 would not, passes that need this information could be revised
13597 to provide it through dataflow propagation. */
13598 return (!name_registered_for_update_p (t
)
13599 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13600 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13609 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13610 has an integer value. We also allow +Inf, -Inf and NaN to be
13611 considered integer values. Return false for signaling NaN.
13613 DEPTH is the current nesting depth of the query. */
13616 integer_valued_real_invalid_p (tree t
, int depth
)
13618 switch (TREE_CODE (t
))
13620 case COMPOUND_EXPR
:
13623 return RECURSE (TREE_OPERAND (t
, 1));
13626 return RECURSE (TREE_OPERAND (t
, 0));
13635 #undef integer_valued_real_p
13637 /* Return true if the floating point expression T has an integer value.
13638 We also allow +Inf, -Inf and NaN to be considered integer values.
13639 Return false for signaling NaN.
13641 DEPTH is the current nesting depth of the query. */
13644 integer_valued_real_p (tree t
, int depth
)
13646 if (t
== error_mark_node
)
13649 tree_code code
= TREE_CODE (t
);
13650 switch (TREE_CODE_CLASS (code
))
13653 case tcc_comparison
:
13654 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13655 TREE_OPERAND (t
, 1), depth
);
13658 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13661 case tcc_declaration
:
13662 case tcc_reference
:
13663 return integer_valued_real_single_p (t
, depth
);
13673 return integer_valued_real_single_p (t
, depth
);
13677 tree arg0
= (call_expr_nargs (t
) > 0
13678 ? CALL_EXPR_ARG (t
, 0)
13680 tree arg1
= (call_expr_nargs (t
) > 1
13681 ? CALL_EXPR_ARG (t
, 1)
13683 return integer_valued_real_call_p (get_call_combined_fn (t
),
13684 arg0
, arg1
, depth
);
13688 return integer_valued_real_invalid_p (t
, depth
);
13692 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13693 attempt to fold the expression to a constant without modifying TYPE,
13696 If the expression could be simplified to a constant, then return
13697 the constant. If the expression would not be simplified to a
13698 constant, then return NULL_TREE. */
13701 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13703 tree tem
= fold_binary (code
, type
, op0
, op1
);
13704 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13707 /* Given the components of a unary expression CODE, TYPE and OP0,
13708 attempt to fold the expression to a constant without modifying
13711 If the expression could be simplified to a constant, then return
13712 the constant. If the expression would not be simplified to a
13713 constant, then return NULL_TREE. */
13716 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13718 tree tem
= fold_unary (code
, type
, op0
);
13719 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13722 /* If EXP represents referencing an element in a constant string
13723 (either via pointer arithmetic or array indexing), return the
13724 tree representing the value accessed, otherwise return NULL. */
13727 fold_read_from_constant_string (tree exp
)
13729 if ((TREE_CODE (exp
) == INDIRECT_REF
13730 || TREE_CODE (exp
) == ARRAY_REF
)
13731 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13733 tree exp1
= TREE_OPERAND (exp
, 0);
13736 location_t loc
= EXPR_LOCATION (exp
);
13738 if (TREE_CODE (exp
) == INDIRECT_REF
)
13739 string
= string_constant (exp1
, &index
);
13742 tree low_bound
= array_ref_low_bound (exp
);
13743 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13745 /* Optimize the special-case of a zero lower bound.
13747 We convert the low_bound to sizetype to avoid some problems
13748 with constant folding. (E.g. suppose the lower bound is 1,
13749 and its mode is QI. Without the conversion,l (ARRAY
13750 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13751 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13752 if (! integer_zerop (low_bound
))
13753 index
= size_diffop_loc (loc
, index
,
13754 fold_convert_loc (loc
, sizetype
, low_bound
));
13759 scalar_int_mode char_mode
;
13761 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13762 && TREE_CODE (string
) == STRING_CST
13763 && TREE_CODE (index
) == INTEGER_CST
13764 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13765 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
13767 && GET_MODE_SIZE (char_mode
) == 1)
13768 return build_int_cst_type (TREE_TYPE (exp
),
13769 (TREE_STRING_POINTER (string
)
13770 [TREE_INT_CST_LOW (index
)]));
13775 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13776 an integer constant, real, or fixed-point constant.
13778 TYPE is the type of the result. */
13781 fold_negate_const (tree arg0
, tree type
)
13783 tree t
= NULL_TREE
;
13785 switch (TREE_CODE (arg0
))
13788 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13793 FIXED_VALUE_TYPE f
;
13794 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13795 &(TREE_FIXED_CST (arg0
)), NULL
,
13796 TYPE_SATURATING (type
));
13797 t
= build_fixed (type
, f
);
13798 /* Propagate overflow flags. */
13799 if (overflow_p
| TREE_OVERFLOW (arg0
))
13800 TREE_OVERFLOW (t
) = 1;
13805 if (poly_int_tree_p (arg0
))
13808 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
13809 t
= force_fit_type (type
, res
, 1,
13810 (overflow
&& ! TYPE_UNSIGNED (type
))
13811 || TREE_OVERFLOW (arg0
));
13815 gcc_unreachable ();
13821 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13822 an integer constant or real constant.
13824 TYPE is the type of the result. */
13827 fold_abs_const (tree arg0
, tree type
)
13829 tree t
= NULL_TREE
;
13831 switch (TREE_CODE (arg0
))
13835 /* If the value is unsigned or non-negative, then the absolute value
13836 is the same as the ordinary value. */
13837 if (!wi::neg_p (wi::to_wide (arg0
), TYPE_SIGN (type
)))
13840 /* If the value is negative, then the absolute value is
13845 wide_int val
= wi::neg (wi::to_wide (arg0
), &overflow
);
13846 t
= force_fit_type (type
, val
, -1,
13847 overflow
| TREE_OVERFLOW (arg0
));
13853 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13854 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13860 gcc_unreachable ();
13866 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13867 constant. TYPE is the type of the result. */
13870 fold_not_const (const_tree arg0
, tree type
)
13872 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13874 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
13877 /* Given CODE, a relational operator, the target type, TYPE and two
13878 constant operands OP0 and OP1, return the result of the
13879 relational operation. If the result is not a compile time
13880 constant, then return NULL_TREE. */
13883 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13885 int result
, invert
;
13887 /* From here on, the only cases we handle are when the result is
13888 known to be a constant. */
13890 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13892 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13893 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13895 /* Handle the cases where either operand is a NaN. */
13896 if (real_isnan (c0
) || real_isnan (c1
))
13906 case UNORDERED_EXPR
:
13920 if (flag_trapping_math
)
13926 gcc_unreachable ();
13929 return constant_boolean_node (result
, type
);
13932 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13935 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13937 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13938 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13939 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13942 /* Handle equality/inequality of complex constants. */
13943 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13945 tree rcond
= fold_relational_const (code
, type
,
13946 TREE_REALPART (op0
),
13947 TREE_REALPART (op1
));
13948 tree icond
= fold_relational_const (code
, type
,
13949 TREE_IMAGPART (op0
),
13950 TREE_IMAGPART (op1
));
13951 if (code
== EQ_EXPR
)
13952 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13953 else if (code
== NE_EXPR
)
13954 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13959 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13961 if (!VECTOR_TYPE_P (type
))
13963 /* Have vector comparison with scalar boolean result. */
13964 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
13965 && known_eq (VECTOR_CST_NELTS (op0
),
13966 VECTOR_CST_NELTS (op1
)));
13967 unsigned HOST_WIDE_INT nunits
;
13968 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
13970 for (unsigned i
= 0; i
< nunits
; i
++)
13972 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13973 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13974 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
13975 if (tmp
== NULL_TREE
)
13977 if (integer_zerop (tmp
))
13978 return constant_boolean_node (false, type
);
13980 return constant_boolean_node (true, type
);
13982 tree_vector_builder elts
;
13983 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
13985 unsigned int count
= elts
.encoded_nelts ();
13986 for (unsigned i
= 0; i
< count
; i
++)
13988 tree elem_type
= TREE_TYPE (type
);
13989 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13990 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13992 tree tem
= fold_relational_const (code
, elem_type
,
13995 if (tem
== NULL_TREE
)
13998 elts
.quick_push (build_int_cst (elem_type
,
13999 integer_zerop (tem
) ? 0 : -1));
14002 return elts
.build ();
14005 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14007 To compute GT, swap the arguments and do LT.
14008 To compute GE, do LT and invert the result.
14009 To compute LE, swap the arguments, do LT and invert the result.
14010 To compute NE, do EQ and invert the result.
14012 Therefore, the code below must handle only EQ and LT. */
14014 if (code
== LE_EXPR
|| code
== GT_EXPR
)
14016 std::swap (op0
, op1
);
14017 code
= swap_tree_comparison (code
);
14020 /* Note that it is safe to invert for real values here because we
14021 have already handled the one case that it matters. */
14024 if (code
== NE_EXPR
|| code
== GE_EXPR
)
14027 code
= invert_tree_comparison (code
, false);
14030 /* Compute a result for LT or EQ if args permit;
14031 Otherwise return T. */
14032 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
14034 if (code
== EQ_EXPR
)
14035 result
= tree_int_cst_equal (op0
, op1
);
14037 result
= tree_int_cst_lt (op0
, op1
);
14044 return constant_boolean_node (result
, type
);
14047 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14048 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14052 fold_build_cleanup_point_expr (tree type
, tree expr
)
14054 /* If the expression does not have side effects then we don't have to wrap
14055 it with a cleanup point expression. */
14056 if (!TREE_SIDE_EFFECTS (expr
))
14059 /* If the expression is a return, check to see if the expression inside the
14060 return has no side effects or the right hand side of the modify expression
14061 inside the return. If either don't have side effects set we don't need to
14062 wrap the expression in a cleanup point expression. Note we don't check the
14063 left hand side of the modify because it should always be a return decl. */
14064 if (TREE_CODE (expr
) == RETURN_EXPR
)
14066 tree op
= TREE_OPERAND (expr
, 0);
14067 if (!op
|| !TREE_SIDE_EFFECTS (op
))
14069 op
= TREE_OPERAND (op
, 1);
14070 if (!TREE_SIDE_EFFECTS (op
))
14074 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
14077 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14078 of an indirection through OP0, or NULL_TREE if no simplification is
14082 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
14086 poly_uint64 const_op01
;
14089 subtype
= TREE_TYPE (sub
);
14090 if (!POINTER_TYPE_P (subtype
)
14091 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
14094 if (TREE_CODE (sub
) == ADDR_EXPR
)
14096 tree op
= TREE_OPERAND (sub
, 0);
14097 tree optype
= TREE_TYPE (op
);
14099 /* *&CONST_DECL -> to the value of the const decl. */
14100 if (TREE_CODE (op
) == CONST_DECL
)
14101 return DECL_INITIAL (op
);
14102 /* *&p => p; make sure to handle *&"str"[cst] here. */
14103 if (type
== optype
)
14105 tree fop
= fold_read_from_constant_string (op
);
14111 /* *(foo *)&fooarray => fooarray[0] */
14112 else if (TREE_CODE (optype
) == ARRAY_TYPE
14113 && type
== TREE_TYPE (optype
)
14114 && (!in_gimple_form
14115 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14117 tree type_domain
= TYPE_DOMAIN (optype
);
14118 tree min_val
= size_zero_node
;
14119 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14120 min_val
= TYPE_MIN_VALUE (type_domain
);
14122 && TREE_CODE (min_val
) != INTEGER_CST
)
14124 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
14125 NULL_TREE
, NULL_TREE
);
14127 /* *(foo *)&complexfoo => __real__ complexfoo */
14128 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14129 && type
== TREE_TYPE (optype
))
14130 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14131 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14132 else if (VECTOR_TYPE_P (optype
)
14133 && type
== TREE_TYPE (optype
))
14135 tree part_width
= TYPE_SIZE (type
);
14136 tree index
= bitsize_int (0);
14137 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
14142 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14143 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
14145 tree op00
= TREE_OPERAND (sub
, 0);
14146 tree op01
= TREE_OPERAND (sub
, 1);
14149 if (TREE_CODE (op00
) == ADDR_EXPR
)
14152 op00
= TREE_OPERAND (op00
, 0);
14153 op00type
= TREE_TYPE (op00
);
14155 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14156 if (VECTOR_TYPE_P (op00type
)
14157 && type
== TREE_TYPE (op00type
)
14158 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14159 but we want to treat offsets with MSB set as negative.
14160 For the code below negative offsets are invalid and
14161 TYPE_SIZE of the element is something unsigned, so
14162 check whether op01 fits into poly_int64, which implies
14163 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14164 then just use poly_uint64 because we want to treat the
14165 value as unsigned. */
14166 && tree_fits_poly_int64_p (op01
))
14168 tree part_width
= TYPE_SIZE (type
);
14169 poly_uint64 max_offset
14170 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14171 * TYPE_VECTOR_SUBPARTS (op00type
));
14172 if (known_lt (const_op01
, max_offset
))
14174 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
14175 return fold_build3_loc (loc
,
14176 BIT_FIELD_REF
, type
, op00
,
14177 part_width
, index
);
14180 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14181 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14182 && type
== TREE_TYPE (op00type
))
14184 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
14186 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14188 /* ((foo *)&fooarray)[1] => fooarray[1] */
14189 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14190 && type
== TREE_TYPE (op00type
))
14192 tree type_domain
= TYPE_DOMAIN (op00type
);
14193 tree min_val
= size_zero_node
;
14194 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14195 min_val
= TYPE_MIN_VALUE (type_domain
);
14196 offset_int off
= wi::to_offset (op01
);
14197 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (type
));
14198 offset_int remainder
;
14199 off
= wi::divmod_trunc (off
, el_sz
, SIGNED
, &remainder
);
14200 if (remainder
== 0 && TREE_CODE (min_val
) == INTEGER_CST
)
14202 off
= off
+ wi::to_offset (min_val
);
14203 op01
= wide_int_to_tree (sizetype
, off
);
14204 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14205 NULL_TREE
, NULL_TREE
);
14211 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14212 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14213 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14214 && (!in_gimple_form
14215 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14218 tree min_val
= size_zero_node
;
14219 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14220 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14221 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14222 min_val
= TYPE_MIN_VALUE (type_domain
);
14224 && TREE_CODE (min_val
) != INTEGER_CST
)
14226 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14233 /* Builds an expression for an indirection through T, simplifying some
14237 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14239 tree type
= TREE_TYPE (TREE_TYPE (t
));
14240 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14245 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14248 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14251 fold_indirect_ref_loc (location_t loc
, tree t
)
14253 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14261 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14262 whose result is ignored. The type of the returned tree need not be
14263 the same as the original expression. */
14266 fold_ignored_result (tree t
)
14268 if (!TREE_SIDE_EFFECTS (t
))
14269 return integer_zero_node
;
14272 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14275 t
= TREE_OPERAND (t
, 0);
14279 case tcc_comparison
:
14280 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14281 t
= TREE_OPERAND (t
, 0);
14282 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14283 t
= TREE_OPERAND (t
, 1);
14288 case tcc_expression
:
14289 switch (TREE_CODE (t
))
14291 case COMPOUND_EXPR
:
14292 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14294 t
= TREE_OPERAND (t
, 0);
14298 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14299 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14301 t
= TREE_OPERAND (t
, 0);
14314 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14317 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14319 tree div
= NULL_TREE
;
14324 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14325 have to do anything. Only do this when we are not given a const,
14326 because in that case, this check is more expensive than just
14328 if (TREE_CODE (value
) != INTEGER_CST
)
14330 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14332 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14336 /* If divisor is a power of two, simplify this to bit manipulation. */
14337 if (pow2_or_zerop (divisor
))
14339 if (TREE_CODE (value
) == INTEGER_CST
)
14341 wide_int val
= wi::to_wide (value
);
14344 if ((val
& (divisor
- 1)) == 0)
14347 overflow_p
= TREE_OVERFLOW (value
);
14348 val
+= divisor
- 1;
14349 val
&= (int) -divisor
;
14353 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14359 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14360 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14361 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14362 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14368 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14369 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14370 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14376 /* Likewise, but round down. */
14379 round_down_loc (location_t loc
, tree value
, int divisor
)
14381 tree div
= NULL_TREE
;
14383 gcc_assert (divisor
> 0);
14387 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14388 have to do anything. Only do this when we are not given a const,
14389 because in that case, this check is more expensive than just
14391 if (TREE_CODE (value
) != INTEGER_CST
)
14393 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14395 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14399 /* If divisor is a power of two, simplify this to bit manipulation. */
14400 if (pow2_or_zerop (divisor
))
14404 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14405 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14410 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14411 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14412 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14418 /* Returns the pointer to the base of the object addressed by EXP and
14419 extracts the information about the offset of the access, storing it
14420 to PBITPOS and POFFSET. */
14423 split_address_to_core_and_offset (tree exp
,
14424 poly_int64_pod
*pbitpos
, tree
*poffset
)
14428 int unsignedp
, reversep
, volatilep
;
14429 poly_int64 bitsize
;
14430 location_t loc
= EXPR_LOCATION (exp
);
14432 if (TREE_CODE (exp
) == ADDR_EXPR
)
14434 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14435 poffset
, &mode
, &unsignedp
, &reversep
,
14437 core
= build_fold_addr_expr_loc (loc
, core
);
14439 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
14441 core
= TREE_OPERAND (exp
, 0);
14444 *poffset
= TREE_OPERAND (exp
, 1);
14445 if (poly_int_tree_p (*poffset
))
14447 poly_offset_int tem
14448 = wi::sext (wi::to_poly_offset (*poffset
),
14449 TYPE_PRECISION (TREE_TYPE (*poffset
)));
14450 tem
<<= LOG2_BITS_PER_UNIT
;
14451 if (tem
.to_shwi (pbitpos
))
14452 *poffset
= NULL_TREE
;
14459 *poffset
= NULL_TREE
;
14465 /* Returns true if addresses of E1 and E2 differ by a constant, false
14466 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14469 ptr_difference_const (tree e1
, tree e2
, poly_int64_pod
*diff
)
14472 poly_int64 bitpos1
, bitpos2
;
14473 tree toffset1
, toffset2
, tdiff
, type
;
14475 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14476 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14478 poly_int64 bytepos1
, bytepos2
;
14479 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
14480 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
14481 || !operand_equal_p (core1
, core2
, 0))
14484 if (toffset1
&& toffset2
)
14486 type
= TREE_TYPE (toffset1
);
14487 if (type
!= TREE_TYPE (toffset2
))
14488 toffset2
= fold_convert (type
, toffset2
);
14490 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14491 if (!cst_and_fits_in_hwi (tdiff
))
14494 *diff
= int_cst_value (tdiff
);
14496 else if (toffset1
|| toffset2
)
14498 /* If only one of the offsets is non-constant, the difference cannot
14505 *diff
+= bytepos1
- bytepos2
;
14509 /* Return OFF converted to a pointer offset type suitable as offset for
14510 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14512 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14514 return fold_convert_loc (loc
, sizetype
, off
);
14517 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14519 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14521 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14522 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14525 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14527 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14529 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14530 ptr
, size_int (off
));
14533 /* Return a char pointer for a C string if it is a string constant
14534 or sum of string constant and integer constant. We only support
14535 string constants properly terminated with '\0' character.
14536 If STRLEN is a valid pointer, length (including terminating character)
14537 of returned string is stored to the argument. */
14540 c_getstr (tree src
, unsigned HOST_WIDE_INT
*strlen
)
14547 src
= string_constant (src
, &offset_node
);
14551 unsigned HOST_WIDE_INT offset
= 0;
14552 if (offset_node
!= NULL_TREE
)
14554 if (!tree_fits_uhwi_p (offset_node
))
14557 offset
= tree_to_uhwi (offset_node
);
14560 unsigned HOST_WIDE_INT string_length
= TREE_STRING_LENGTH (src
);
14561 const char *string
= TREE_STRING_POINTER (src
);
14563 /* Support only properly null-terminated strings. */
14564 if (string_length
== 0
14565 || string
[string_length
- 1] != '\0'
14566 || offset
>= string_length
)
14570 *strlen
= string_length
- offset
;
14571 return string
+ offset
;
14576 namespace selftest
{
14578 /* Helper functions for writing tests of folding trees. */
14580 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14583 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
14586 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
14589 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14590 wrapping WRAPPED_EXPR. */
14593 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
14596 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
14597 ASSERT_NE (wrapped_expr
, result
);
14598 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
14599 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
14602 /* Verify that various arithmetic binary operations are folded
14606 test_arithmetic_folding ()
14608 tree type
= integer_type_node
;
14609 tree x
= create_tmp_var_raw (type
, "x");
14610 tree zero
= build_zero_cst (type
);
14611 tree one
= build_int_cst (type
, 1);
14614 /* 1 <-- (0 + 1) */
14615 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
14617 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
14620 /* (nonlvalue)x <-- (x + 0) */
14621 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
14625 /* 0 <-- (x - x) */
14626 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
14628 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
14631 /* Multiplication. */
14632 /* 0 <-- (x * 0) */
14633 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
14636 /* (nonlvalue)x <-- (x * 1) */
14637 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
14641 /* Verify that various binary operations on vectors are folded
14645 test_vector_folding ()
14647 tree inner_type
= integer_type_node
;
14648 tree type
= build_vector_type (inner_type
, 4);
14649 tree zero
= build_zero_cst (type
);
14650 tree one
= build_one_cst (type
);
14652 /* Verify equality tests that return a scalar boolean result. */
14653 tree res_type
= boolean_type_node
;
14654 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
14655 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
14656 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
14657 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
14660 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14663 test_vec_duplicate_folding ()
14665 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
14666 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
14667 /* This will be 1 if VEC_MODE isn't a vector mode. */
14668 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
14670 tree type
= build_vector_type (ssizetype
, nunits
);
14671 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
14672 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
14673 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
14676 /* Run all of the selftests within this file. */
14679 fold_const_c_tests ()
14681 test_arithmetic_folding ();
14682 test_vector_folding ();
14683 test_vec_duplicate_folding ();
14686 } // namespace selftest
14688 #endif /* CHECKING_P */