1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
45 #include "coretypes.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
57 #include "diagnostic-core.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
64 #include "tree-iterator.h"
67 #include "langhooks.h"
72 #include "generic-match.h"
73 #include "gimple-iterator.h"
74 #include "gimple-fold.h"
75 #include "tree-into-ssa.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
80 #include "tree-ssanames.h"
82 #include "stringpool.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 #include "gimple-range.h"
89 /* Nonzero if we are folding constants inside an initializer or a C++
90 manifestly-constant-evaluated context; zero otherwise.
91 Should be used when folding in initializer enables additional
93 int folding_initializer
= 0;
95 /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
97 Should be used when certain constructs shouldn't be optimized
98 during folding in that context. */
99 bool folding_cxx_constexpr
= false;
101 /* The following constants represent a bit based encoding of GCC's
102 comparison operators. This encoding simplifies transformations
103 on relational comparison operators, such as AND and OR. */
104 enum comparison_code
{
123 static bool negate_expr_p (tree
);
124 static tree
negate_expr (tree
);
125 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
126 static enum comparison_code
comparison_to_compcode (enum tree_code
);
127 static enum tree_code
compcode_to_comparison (enum comparison_code
);
128 static bool twoval_comparison_p (tree
, tree
*, tree
*);
129 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
130 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
132 static bool simple_operand_p (const_tree
);
133 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
134 static tree
range_predecessor (tree
);
135 static tree
range_successor (tree
);
136 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
137 static tree
fold_cond_expr_with_comparison (location_t
, tree
, enum tree_code
,
138 tree
, tree
, tree
, tree
);
139 static tree
unextend (tree
, int, int, tree
);
140 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
141 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
142 static tree
fold_binary_op_with_conditional_arg (location_t
,
143 enum tree_code
, tree
,
146 static tree
fold_negate_const (tree
, tree
);
147 static tree
fold_not_const (const_tree
, tree
);
148 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
149 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
150 static tree
fold_view_convert_expr (tree
, tree
);
151 static tree
fold_negate_expr (location_t
, tree
);
153 /* This is a helper function to detect min/max for some operands of COND_EXPR.
154 The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
156 minmax_from_comparison (tree_code cmp
, tree exp0
, tree exp1
, tree exp2
, tree exp3
)
158 enum tree_code code
= ERROR_MARK
;
160 if (HONOR_NANS (exp0
) || HONOR_SIGNED_ZEROS (exp0
))
163 if (!operand_equal_p (exp0
, exp2
))
166 if (TREE_CODE (exp3
) == INTEGER_CST
&& TREE_CODE (exp1
) == INTEGER_CST
)
168 if (wi::to_widest (exp1
) == (wi::to_widest (exp3
) - 1))
170 /* X <= Y - 1 equals to X < Y. */
173 /* X > Y - 1 equals to X >= Y. */
177 if (wi::to_widest (exp1
) == (wi::to_widest (exp3
) + 1))
179 /* X < Y + 1 equals to X <= Y. */
182 /* X >= Y + 1 equals to X > Y. */
187 if (code
!= ERROR_MARK
188 || operand_equal_p (exp1
, exp3
))
190 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
192 if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
198 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
199 Otherwise, return LOC. */
202 expr_location_or (tree t
, location_t loc
)
204 location_t tloc
= EXPR_LOCATION (t
);
205 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
208 /* Similar to protected_set_expr_location, but never modify x in place,
209 if location can and needs to be set, unshare it. */
212 protected_set_expr_location_unshare (tree x
, location_t loc
)
214 if (CAN_HAVE_LOCATION_P (x
)
215 && EXPR_LOCATION (x
) != loc
216 && !(TREE_CODE (x
) == SAVE_EXPR
217 || TREE_CODE (x
) == TARGET_EXPR
218 || TREE_CODE (x
) == BIND_EXPR
))
221 SET_EXPR_LOCATION (x
, loc
);
226 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
227 division and returns the quotient. Otherwise returns
231 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
235 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
237 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
242 /* This is nonzero if we should defer warnings about undefined
243 overflow. This facility exists because these warnings are a
244 special case. The code to estimate loop iterations does not want
245 to issue any warnings, since it works with expressions which do not
246 occur in user code. Various bits of cleanup code call fold(), but
247 only use the result if it has certain characteristics (e.g., is a
248 constant); that code only wants to issue a warning if the result is
251 static int fold_deferring_overflow_warnings
;
253 /* If a warning about undefined overflow is deferred, this is the
254 warning. Note that this may cause us to turn two warnings into
255 one, but that is fine since it is sufficient to only give one
256 warning per expression. */
258 static const char* fold_deferred_overflow_warning
;
260 /* If a warning about undefined overflow is deferred, this is the
261 level at which the warning should be emitted. */
263 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
265 /* Start deferring overflow warnings. We could use a stack here to
266 permit nested calls, but at present it is not necessary. */
269 fold_defer_overflow_warnings (void)
271 ++fold_deferring_overflow_warnings
;
274 /* Stop deferring overflow warnings. If there is a pending warning,
275 and ISSUE is true, then issue the warning if appropriate. STMT is
276 the statement with which the warning should be associated (used for
277 location information); STMT may be NULL. CODE is the level of the
278 warning--a warn_strict_overflow_code value. This function will use
279 the smaller of CODE and the deferred code when deciding whether to
280 issue the warning. CODE may be zero to mean to always use the
284 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
289 gcc_assert (fold_deferring_overflow_warnings
> 0);
290 --fold_deferring_overflow_warnings
;
291 if (fold_deferring_overflow_warnings
> 0)
293 if (fold_deferred_overflow_warning
!= NULL
295 && code
< (int) fold_deferred_overflow_code
)
296 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
300 warnmsg
= fold_deferred_overflow_warning
;
301 fold_deferred_overflow_warning
= NULL
;
303 if (!issue
|| warnmsg
== NULL
)
306 if (warning_suppressed_p (stmt
, OPT_Wstrict_overflow
))
309 /* Use the smallest code level when deciding to issue the
311 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
312 code
= fold_deferred_overflow_code
;
314 if (!issue_strict_overflow_warning (code
))
318 locus
= input_location
;
320 locus
= gimple_location (stmt
);
321 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
324 /* Stop deferring overflow warnings, ignoring any deferred
328 fold_undefer_and_ignore_overflow_warnings (void)
330 fold_undefer_overflow_warnings (false, NULL
, 0);
333 /* Whether we are deferring overflow warnings. */
336 fold_deferring_overflow_warnings_p (void)
338 return fold_deferring_overflow_warnings
> 0;
341 /* This is called when we fold something based on the fact that signed
342 overflow is undefined. */
345 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
347 if (fold_deferring_overflow_warnings
> 0)
349 if (fold_deferred_overflow_warning
== NULL
350 || wc
< fold_deferred_overflow_code
)
352 fold_deferred_overflow_warning
= gmsgid
;
353 fold_deferred_overflow_code
= wc
;
356 else if (issue_strict_overflow_warning (wc
))
357 warning (OPT_Wstrict_overflow
, gmsgid
);
360 /* Return true if the built-in mathematical function specified by CODE
361 is odd, i.e. -f(x) == f(-x). */
364 negate_mathfn_p (combined_fn fn
)
404 CASE_CFN_ROUNDEVEN_FN
:
422 CASE_CFN_NEARBYINT_FN
:
425 return !flag_rounding_math
;
433 /* Check whether we may negate an integer constant T without causing
437 may_negate_without_overflow_p (const_tree t
)
441 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
443 type
= TREE_TYPE (t
);
444 if (TYPE_UNSIGNED (type
))
447 return !wi::only_sign_bit_p (wi::to_wide (t
));
450 /* Determine whether an expression T can be cheaply negated using
451 the function negate_expr without introducing undefined overflow. */
454 negate_expr_p (tree t
)
461 type
= TREE_TYPE (t
);
464 switch (TREE_CODE (t
))
467 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
470 /* Check that -CST will not overflow type. */
471 return may_negate_without_overflow_p (t
);
473 return (INTEGRAL_TYPE_P (type
)
474 && TYPE_OVERFLOW_WRAPS (type
));
480 return !TYPE_OVERFLOW_SANITIZED (type
);
483 /* We want to canonicalize to positive real constants. Pretend
484 that only negative ones can be easily negated. */
485 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
488 return negate_expr_p (TREE_REALPART (t
))
489 && negate_expr_p (TREE_IMAGPART (t
));
493 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
496 /* Steps don't prevent negation. */
497 unsigned int count
= vector_cst_encoded_nelts (t
);
498 for (unsigned int i
= 0; i
< count
; ++i
)
499 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t
, i
)))
506 return negate_expr_p (TREE_OPERAND (t
, 0))
507 && negate_expr_p (TREE_OPERAND (t
, 1));
510 return negate_expr_p (TREE_OPERAND (t
, 0));
513 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
)
514 || HONOR_SIGNED_ZEROS (type
)
515 || (ANY_INTEGRAL_TYPE_P (type
)
516 && ! TYPE_OVERFLOW_WRAPS (type
)))
518 /* -(A + B) -> (-B) - A. */
519 if (negate_expr_p (TREE_OPERAND (t
, 1)))
521 /* -(A + B) -> (-A) - B. */
522 return negate_expr_p (TREE_OPERAND (t
, 0));
525 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
526 return !HONOR_SIGN_DEPENDENT_ROUNDING (type
)
527 && !HONOR_SIGNED_ZEROS (type
)
528 && (! ANY_INTEGRAL_TYPE_P (type
)
529 || TYPE_OVERFLOW_WRAPS (type
));
532 if (TYPE_UNSIGNED (type
))
534 /* INT_MIN/n * n doesn't overflow while negating one operand it does
535 if n is a (negative) power of two. */
536 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
537 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
538 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
540 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 0))))) != 1)
541 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
543 (wi::abs (wi::to_wide (TREE_OPERAND (t
, 1))))) != 1)))
549 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t
))
550 return negate_expr_p (TREE_OPERAND (t
, 1))
551 || negate_expr_p (TREE_OPERAND (t
, 0));
557 if (TYPE_UNSIGNED (type
))
559 /* In general we can't negate A in A / B, because if A is INT_MIN and
560 B is not 1 we change the sign of the result. */
561 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
562 && negate_expr_p (TREE_OPERAND (t
, 0)))
564 /* In general we can't negate B in A / B, because if A is INT_MIN and
565 B is 1, we may turn this into INT_MIN / -1 which is undefined
566 and actually traps on some architectures. */
567 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
568 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
569 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
570 && ! integer_onep (TREE_OPERAND (t
, 1))))
571 return negate_expr_p (TREE_OPERAND (t
, 1));
575 /* Negate -((double)float) as (double)(-float). */
576 if (SCALAR_FLOAT_TYPE_P (type
))
578 tree tem
= strip_float_extensions (t
);
580 return negate_expr_p (tem
);
585 /* Negate -f(x) as f(-x). */
586 if (negate_mathfn_p (get_call_combined_fn (t
)))
587 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
591 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
592 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
594 tree op1
= TREE_OPERAND (t
, 1);
595 if (wi::to_wide (op1
) == element_precision (type
) - 1)
606 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
607 simplification is possible.
608 If negate_expr_p would return true for T, NULL_TREE will never be
612 fold_negate_expr_1 (location_t loc
, tree t
)
614 tree type
= TREE_TYPE (t
);
617 switch (TREE_CODE (t
))
619 /* Convert - (~A) to A + 1. */
621 if (INTEGRAL_TYPE_P (type
))
622 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
623 build_one_cst (type
));
627 tem
= fold_negate_const (t
, type
);
628 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
629 || (ANY_INTEGRAL_TYPE_P (type
)
630 && !TYPE_OVERFLOW_TRAPS (type
)
631 && TYPE_OVERFLOW_WRAPS (type
))
632 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
639 tem
= fold_negate_const (t
, type
);
644 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
645 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
647 return build_complex (type
, rpart
, ipart
);
653 tree_vector_builder elts
;
654 elts
.new_unary_operation (type
, t
, true);
655 unsigned int count
= elts
.encoded_nelts ();
656 for (unsigned int i
= 0; i
< count
; ++i
)
658 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
659 if (elt
== NULL_TREE
)
661 elts
.quick_push (elt
);
664 return elts
.build ();
668 if (negate_expr_p (t
))
669 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
670 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
671 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
675 if (negate_expr_p (t
))
676 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
677 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
681 if (!TYPE_OVERFLOW_SANITIZED (type
))
682 return TREE_OPERAND (t
, 0);
686 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
687 && !HONOR_SIGNED_ZEROS (type
))
689 /* -(A + B) -> (-B) - A. */
690 if (negate_expr_p (TREE_OPERAND (t
, 1)))
692 tem
= negate_expr (TREE_OPERAND (t
, 1));
693 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
694 tem
, TREE_OPERAND (t
, 0));
697 /* -(A + B) -> (-A) - B. */
698 if (negate_expr_p (TREE_OPERAND (t
, 0)))
700 tem
= negate_expr (TREE_OPERAND (t
, 0));
701 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
702 tem
, TREE_OPERAND (t
, 1));
708 /* - (A - B) -> B - A */
709 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type
)
710 && !HONOR_SIGNED_ZEROS (type
))
711 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
712 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
716 if (TYPE_UNSIGNED (type
))
722 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type
))
724 tem
= TREE_OPERAND (t
, 1);
725 if (negate_expr_p (tem
))
726 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
727 TREE_OPERAND (t
, 0), negate_expr (tem
));
728 tem
= TREE_OPERAND (t
, 0);
729 if (negate_expr_p (tem
))
730 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
731 negate_expr (tem
), TREE_OPERAND (t
, 1));
738 if (TYPE_UNSIGNED (type
))
740 /* In general we can't negate A in A / B, because if A is INT_MIN and
741 B is not 1 we change the sign of the result. */
742 if (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
743 && negate_expr_p (TREE_OPERAND (t
, 0)))
744 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
745 negate_expr (TREE_OPERAND (t
, 0)),
746 TREE_OPERAND (t
, 1));
747 /* In general we can't negate B in A / B, because if A is INT_MIN and
748 B is 1, we may turn this into INT_MIN / -1 which is undefined
749 and actually traps on some architectures. */
750 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t
))
751 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
752 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
753 && ! integer_onep (TREE_OPERAND (t
, 1))))
754 && negate_expr_p (TREE_OPERAND (t
, 1)))
755 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
757 negate_expr (TREE_OPERAND (t
, 1)));
761 /* Convert -((double)float) into (double)(-float). */
762 if (SCALAR_FLOAT_TYPE_P (type
))
764 tem
= strip_float_extensions (t
);
765 if (tem
!= t
&& negate_expr_p (tem
))
766 return fold_convert_loc (loc
, type
, negate_expr (tem
));
771 /* Negate -f(x) as f(-x). */
772 if (negate_mathfn_p (get_call_combined_fn (t
))
773 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
777 fndecl
= get_callee_fndecl (t
);
778 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
779 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
784 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
785 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
787 tree op1
= TREE_OPERAND (t
, 1);
788 if (wi::to_wide (op1
) == element_precision (type
) - 1)
790 tree ntype
= TYPE_UNSIGNED (type
)
791 ? signed_type_for (type
)
792 : unsigned_type_for (type
);
793 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
794 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
795 return fold_convert_loc (loc
, type
, temp
);
807 /* A wrapper for fold_negate_expr_1. */
810 fold_negate_expr (location_t loc
, tree t
)
812 tree type
= TREE_TYPE (t
);
814 tree tem
= fold_negate_expr_1 (loc
, t
);
815 if (tem
== NULL_TREE
)
817 return fold_convert_loc (loc
, type
, tem
);
820 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
821 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
833 loc
= EXPR_LOCATION (t
);
834 type
= TREE_TYPE (t
);
837 tem
= fold_negate_expr (loc
, t
);
839 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
840 return fold_convert_loc (loc
, type
, tem
);
843 /* Split a tree IN into a constant, literal and variable parts that could be
844 combined with CODE to make IN. "constant" means an expression with
845 TREE_CONSTANT but that isn't an actual constant. CODE must be a
846 commutative arithmetic operation. Store the constant part into *CONP,
847 the literal in *LITP and return the variable part. If a part isn't
848 present, set it to null. If the tree does not decompose in this way,
849 return the entire tree as the variable part and the other parts as null.
851 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
852 case, we negate an operand that was subtracted. Except if it is a
853 literal for which we use *MINUS_LITP instead.
855 If NEGATE_P is true, we are negating all of IN, again except a literal
856 for which we use *MINUS_LITP instead. If a variable part is of pointer
857 type, it is negated after converting to TYPE. This prevents us from
858 generating illegal MINUS pointer expression. LOC is the location of
859 the converted variable part.
861 If IN is itself a literal or constant, return it as appropriate.
863 Note that we do not guarantee that any of the three values will be the
864 same type as IN, but they will have the same signedness and mode. */
867 split_tree (tree in
, tree type
, enum tree_code code
,
868 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
869 tree
*litp
, tree
*minus_litp
, int negate_p
)
878 /* Strip any conversions that don't change the machine mode or signedness. */
879 STRIP_SIGN_NOPS (in
);
881 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
882 || TREE_CODE (in
) == FIXED_CST
)
884 else if (TREE_CODE (in
) == code
885 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
886 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
887 /* We can associate addition and subtraction together (even
888 though the C standard doesn't say so) for integers because
889 the value is not affected. For reals, the value might be
890 affected, so we can't. */
891 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
892 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
893 || (code
== MINUS_EXPR
894 && (TREE_CODE (in
) == PLUS_EXPR
895 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
897 tree op0
= TREE_OPERAND (in
, 0);
898 tree op1
= TREE_OPERAND (in
, 1);
899 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
900 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
902 /* First see if either of the operands is a literal, then a constant. */
903 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
904 || TREE_CODE (op0
) == FIXED_CST
)
905 *litp
= op0
, op0
= 0;
906 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
907 || TREE_CODE (op1
) == FIXED_CST
)
908 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
910 if (op0
!= 0 && TREE_CONSTANT (op0
))
911 *conp
= op0
, op0
= 0;
912 else if (op1
!= 0 && TREE_CONSTANT (op1
))
913 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
915 /* If we haven't dealt with either operand, this is not a case we can
916 decompose. Otherwise, VAR is either of the ones remaining, if any. */
917 if (op0
!= 0 && op1
!= 0)
922 var
= op1
, neg_var_p
= neg1_p
;
924 /* Now do any needed negations. */
926 *minus_litp
= *litp
, *litp
= 0;
927 if (neg_conp_p
&& *conp
)
928 *minus_conp
= *conp
, *conp
= 0;
929 if (neg_var_p
&& var
)
930 *minus_varp
= var
, var
= 0;
932 else if (TREE_CONSTANT (in
))
934 else if (TREE_CODE (in
) == BIT_NOT_EXPR
935 && code
== PLUS_EXPR
)
937 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
938 when IN is constant. */
939 *litp
= build_minus_one_cst (type
);
940 *minus_varp
= TREE_OPERAND (in
, 0);
948 *minus_litp
= *litp
, *litp
= 0;
949 else if (*minus_litp
)
950 *litp
= *minus_litp
, *minus_litp
= 0;
952 *minus_conp
= *conp
, *conp
= 0;
953 else if (*minus_conp
)
954 *conp
= *minus_conp
, *minus_conp
= 0;
956 *minus_varp
= var
, var
= 0;
957 else if (*minus_varp
)
958 var
= *minus_varp
, *minus_varp
= 0;
962 && TREE_OVERFLOW_P (*litp
))
963 *litp
= drop_tree_overflow (*litp
);
965 && TREE_OVERFLOW_P (*minus_litp
))
966 *minus_litp
= drop_tree_overflow (*minus_litp
);
971 /* Re-associate trees split by the above function. T1 and T2 are
972 either expressions to associate or null. Return the new
973 expression, if any. LOC is the location of the new expression. If
974 we build an operation, do it in TYPE and with CODE. */
977 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
981 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
987 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
988 try to fold this since we will have infinite recursion. But do
989 deal with any NEGATE_EXPRs. */
990 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
991 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
992 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
994 if (code
== PLUS_EXPR
)
996 if (TREE_CODE (t1
) == NEGATE_EXPR
)
997 return build2_loc (loc
, MINUS_EXPR
, type
,
998 fold_convert_loc (loc
, type
, t2
),
999 fold_convert_loc (loc
, type
,
1000 TREE_OPERAND (t1
, 0)));
1001 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
1002 return build2_loc (loc
, MINUS_EXPR
, type
,
1003 fold_convert_loc (loc
, type
, t1
),
1004 fold_convert_loc (loc
, type
,
1005 TREE_OPERAND (t2
, 0)));
1006 else if (integer_zerop (t2
))
1007 return fold_convert_loc (loc
, type
, t1
);
1009 else if (code
== MINUS_EXPR
)
1011 if (integer_zerop (t2
))
1012 return fold_convert_loc (loc
, type
, t1
);
1015 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
1016 fold_convert_loc (loc
, type
, t2
));
1019 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
1020 fold_convert_loc (loc
, type
, t2
));
1023 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1024 for use in int_const_binop, size_binop and size_diffop. */
1027 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
1029 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
1031 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
1046 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
1047 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
1048 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
1051 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1052 a new constant in RES. Return FALSE if we don't know how to
1053 evaluate CODE at compile-time. */
1056 wide_int_binop (wide_int
&res
,
1057 enum tree_code code
, const wide_int
&arg1
, const wide_int
&arg2
,
1058 signop sign
, wi::overflow_type
*overflow
)
1061 *overflow
= wi::OVF_NONE
;
1065 res
= wi::bit_or (arg1
, arg2
);
1069 res
= wi::bit_xor (arg1
, arg2
);
1073 res
= wi::bit_and (arg1
, arg2
);
1077 if (wi::neg_p (arg2
))
1079 res
= wi::lshift (arg1
, arg2
);
1083 if (wi::neg_p (arg2
))
1085 /* It's unclear from the C standard whether shifts can overflow.
1086 The following code ignores overflow; perhaps a C standard
1087 interpretation ruling is needed. */
1088 res
= wi::rshift (arg1
, arg2
, sign
);
1093 if (wi::neg_p (arg2
))
1096 if (code
== RROTATE_EXPR
)
1097 code
= LROTATE_EXPR
;
1099 code
= RROTATE_EXPR
;
1104 if (code
== RROTATE_EXPR
)
1105 res
= wi::rrotate (arg1
, tmp
);
1107 res
= wi::lrotate (arg1
, tmp
);
1111 res
= wi::add (arg1
, arg2
, sign
, overflow
);
1115 res
= wi::sub (arg1
, arg2
, sign
, overflow
);
1119 res
= wi::mul (arg1
, arg2
, sign
, overflow
);
1122 case MULT_HIGHPART_EXPR
:
1123 res
= wi::mul_high (arg1
, arg2
, sign
);
1126 case TRUNC_DIV_EXPR
:
1127 case EXACT_DIV_EXPR
:
1130 res
= wi::div_trunc (arg1
, arg2
, sign
, overflow
);
1133 case FLOOR_DIV_EXPR
:
1136 res
= wi::div_floor (arg1
, arg2
, sign
, overflow
);
1142 res
= wi::div_ceil (arg1
, arg2
, sign
, overflow
);
1145 case ROUND_DIV_EXPR
:
1148 res
= wi::div_round (arg1
, arg2
, sign
, overflow
);
1151 case TRUNC_MOD_EXPR
:
1154 res
= wi::mod_trunc (arg1
, arg2
, sign
, overflow
);
1157 case FLOOR_MOD_EXPR
:
1160 res
= wi::mod_floor (arg1
, arg2
, sign
, overflow
);
1166 res
= wi::mod_ceil (arg1
, arg2
, sign
, overflow
);
1169 case ROUND_MOD_EXPR
:
1172 res
= wi::mod_round (arg1
, arg2
, sign
, overflow
);
1176 res
= wi::min (arg1
, arg2
, sign
);
1180 res
= wi::max (arg1
, arg2
, sign
);
1189 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1190 produce a new constant in RES. Return FALSE if we don't know how
1191 to evaluate CODE at compile-time. */
1194 poly_int_binop (poly_wide_int
&res
, enum tree_code code
,
1195 const_tree arg1
, const_tree arg2
,
1196 signop sign
, wi::overflow_type
*overflow
)
1198 gcc_assert (NUM_POLY_INT_COEFFS
!= 1);
1199 gcc_assert (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
));
1203 res
= wi::add (wi::to_poly_wide (arg1
),
1204 wi::to_poly_wide (arg2
), sign
, overflow
);
1208 res
= wi::sub (wi::to_poly_wide (arg1
),
1209 wi::to_poly_wide (arg2
), sign
, overflow
);
1213 if (TREE_CODE (arg2
) == INTEGER_CST
)
1214 res
= wi::mul (wi::to_poly_wide (arg1
),
1215 wi::to_wide (arg2
), sign
, overflow
);
1216 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1217 res
= wi::mul (wi::to_poly_wide (arg2
),
1218 wi::to_wide (arg1
), sign
, overflow
);
1224 if (TREE_CODE (arg2
) == INTEGER_CST
)
1225 res
= wi::to_poly_wide (arg1
) << wi::to_wide (arg2
);
1231 if (TREE_CODE (arg2
) != INTEGER_CST
1232 || !can_ior_p (wi::to_poly_wide (arg1
), wi::to_wide (arg2
),
1243 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1244 produce a new constant. Return NULL_TREE if we don't know how to
1245 evaluate CODE at compile-time. */
1248 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
,
1251 poly_wide_int poly_res
;
1252 tree type
= TREE_TYPE (arg1
);
1253 signop sign
= TYPE_SIGN (type
);
1254 wi::overflow_type overflow
= wi::OVF_NONE
;
1256 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1258 wide_int warg1
= wi::to_wide (arg1
), res
;
1259 wide_int warg2
= wi::to_wide (arg2
, TYPE_PRECISION (type
));
1260 if (!wide_int_binop (res
, code
, warg1
, warg2
, sign
, &overflow
))
1264 else if (!poly_int_tree_p (arg1
)
1265 || !poly_int_tree_p (arg2
)
1266 || !poly_int_binop (poly_res
, code
, arg1
, arg2
, sign
, &overflow
))
1268 return force_fit_type (type
, poly_res
, overflowable
,
1269 (((sign
== SIGNED
|| overflowable
== -1)
1271 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
)));
1274 /* Return true if binary operation OP distributes over addition in operand
1275 OPNO, with the other operand being held constant. OPNO counts from 1. */
1278 distributes_over_addition_p (tree_code op
, int opno
)
1295 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1296 constant. We assume ARG1 and ARG2 have the same data type, or at least
1297 are the same kind of constant and the same machine mode. Return zero if
1298 combining the constants is not allowed in the current operating mode. */
1301 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1303 /* Sanity check for the recursive cases. */
1310 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1312 if (code
== POINTER_PLUS_EXPR
)
1313 return int_const_binop (PLUS_EXPR
,
1314 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1316 return int_const_binop (code
, arg1
, arg2
);
1319 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1324 REAL_VALUE_TYPE value
;
1325 REAL_VALUE_TYPE result
;
1329 /* The following codes are handled by real_arithmetic. */
1344 d1
= TREE_REAL_CST (arg1
);
1345 d2
= TREE_REAL_CST (arg2
);
1347 type
= TREE_TYPE (arg1
);
1348 mode
= TYPE_MODE (type
);
1350 /* Don't perform operation if we honor signaling NaNs and
1351 either operand is a signaling NaN. */
1352 if (HONOR_SNANS (mode
)
1353 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1354 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1357 /* Don't perform operation if it would raise a division
1358 by zero exception. */
1359 if (code
== RDIV_EXPR
1360 && real_equal (&d2
, &dconst0
)
1361 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1364 /* If either operand is a NaN, just return it. Otherwise, set up
1365 for floating-point trap; we return an overflow. */
1366 if (REAL_VALUE_ISNAN (d1
))
1368 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1371 t
= build_real (type
, d1
);
1374 else if (REAL_VALUE_ISNAN (d2
))
1376 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1379 t
= build_real (type
, d2
);
1383 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1384 real_convert (&result
, mode
, &value
);
1386 /* Don't constant fold this floating point operation if
1387 both operands are not NaN but the result is NaN, and
1388 flag_trapping_math. Such operations should raise an
1389 invalid operation exception. */
1390 if (flag_trapping_math
1391 && MODE_HAS_NANS (mode
)
1392 && REAL_VALUE_ISNAN (result
)
1393 && !REAL_VALUE_ISNAN (d1
)
1394 && !REAL_VALUE_ISNAN (d2
))
1397 /* Don't constant fold this floating point operation if
1398 the result has overflowed and flag_trapping_math. */
1399 if (flag_trapping_math
1400 && MODE_HAS_INFINITIES (mode
)
1401 && REAL_VALUE_ISINF (result
)
1402 && !REAL_VALUE_ISINF (d1
)
1403 && !REAL_VALUE_ISINF (d2
))
1406 /* Don't constant fold this floating point operation if the
1407 result may dependent upon the run-time rounding mode and
1408 flag_rounding_math is set, or if GCC's software emulation
1409 is unable to accurately represent the result. */
1410 if ((flag_rounding_math
1411 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1412 && (inexact
|| !real_identical (&result
, &value
)))
1415 t
= build_real (type
, result
);
1417 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1421 if (TREE_CODE (arg1
) == FIXED_CST
)
1423 FIXED_VALUE_TYPE f1
;
1424 FIXED_VALUE_TYPE f2
;
1425 FIXED_VALUE_TYPE result
;
1430 /* The following codes are handled by fixed_arithmetic. */
1436 case TRUNC_DIV_EXPR
:
1437 if (TREE_CODE (arg2
) != FIXED_CST
)
1439 f2
= TREE_FIXED_CST (arg2
);
1445 if (TREE_CODE (arg2
) != INTEGER_CST
)
1447 wi::tree_to_wide_ref w2
= wi::to_wide (arg2
);
1448 f2
.data
.high
= w2
.elt (1);
1449 f2
.data
.low
= w2
.ulow ();
1458 f1
= TREE_FIXED_CST (arg1
);
1459 type
= TREE_TYPE (arg1
);
1460 sat_p
= TYPE_SATURATING (type
);
1461 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1462 t
= build_fixed (type
, result
);
1463 /* Propagate overflow flags. */
1464 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1465 TREE_OVERFLOW (t
) = 1;
1469 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1471 tree type
= TREE_TYPE (arg1
);
1472 tree r1
= TREE_REALPART (arg1
);
1473 tree i1
= TREE_IMAGPART (arg1
);
1474 tree r2
= TREE_REALPART (arg2
);
1475 tree i2
= TREE_IMAGPART (arg2
);
1482 real
= const_binop (code
, r1
, r2
);
1483 imag
= const_binop (code
, i1
, i2
);
1487 if (COMPLEX_FLOAT_TYPE_P (type
))
1488 return do_mpc_arg2 (arg1
, arg2
, type
,
1489 /* do_nonfinite= */ folding_initializer
,
1492 real
= const_binop (MINUS_EXPR
,
1493 const_binop (MULT_EXPR
, r1
, r2
),
1494 const_binop (MULT_EXPR
, i1
, i2
));
1495 imag
= const_binop (PLUS_EXPR
,
1496 const_binop (MULT_EXPR
, r1
, i2
),
1497 const_binop (MULT_EXPR
, i1
, r2
));
1501 if (COMPLEX_FLOAT_TYPE_P (type
))
1502 return do_mpc_arg2 (arg1
, arg2
, type
,
1503 /* do_nonfinite= */ folding_initializer
,
1506 case TRUNC_DIV_EXPR
:
1508 case FLOOR_DIV_EXPR
:
1509 case ROUND_DIV_EXPR
:
1510 if (flag_complex_method
== 0)
1512 /* Keep this algorithm in sync with
1513 tree-complex.cc:expand_complex_div_straight().
1515 Expand complex division to scalars, straightforward algorithm.
1516 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1520 = const_binop (PLUS_EXPR
,
1521 const_binop (MULT_EXPR
, r2
, r2
),
1522 const_binop (MULT_EXPR
, i2
, i2
));
1524 = const_binop (PLUS_EXPR
,
1525 const_binop (MULT_EXPR
, r1
, r2
),
1526 const_binop (MULT_EXPR
, i1
, i2
));
1528 = const_binop (MINUS_EXPR
,
1529 const_binop (MULT_EXPR
, i1
, r2
),
1530 const_binop (MULT_EXPR
, r1
, i2
));
1532 real
= const_binop (code
, t1
, magsquared
);
1533 imag
= const_binop (code
, t2
, magsquared
);
1537 /* Keep this algorithm in sync with
1538 tree-complex.cc:expand_complex_div_wide().
1540 Expand complex division to scalars, modified algorithm to minimize
1541 overflow with wide input ranges. */
1542 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1543 fold_abs_const (r2
, TREE_TYPE (type
)),
1544 fold_abs_const (i2
, TREE_TYPE (type
)));
1546 if (integer_nonzerop (compare
))
1548 /* In the TRUE branch, we compute
1550 div = (br * ratio) + bi;
1551 tr = (ar * ratio) + ai;
1552 ti = (ai * ratio) - ar;
1555 tree ratio
= const_binop (code
, r2
, i2
);
1556 tree div
= const_binop (PLUS_EXPR
, i2
,
1557 const_binop (MULT_EXPR
, r2
, ratio
));
1558 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1559 real
= const_binop (PLUS_EXPR
, real
, i1
);
1560 real
= const_binop (code
, real
, div
);
1562 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1563 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1564 imag
= const_binop (code
, imag
, div
);
1568 /* In the FALSE branch, we compute
1570 divisor = (d * ratio) + c;
1571 tr = (b * ratio) + a;
1572 ti = b - (a * ratio);
1575 tree ratio
= const_binop (code
, i2
, r2
);
1576 tree div
= const_binop (PLUS_EXPR
, r2
,
1577 const_binop (MULT_EXPR
, i2
, ratio
));
1579 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1580 real
= const_binop (PLUS_EXPR
, real
, r1
);
1581 real
= const_binop (code
, real
, div
);
1583 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1584 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1585 imag
= const_binop (code
, imag
, div
);
1595 return build_complex (type
, real
, imag
);
1598 if (TREE_CODE (arg1
) == VECTOR_CST
1599 && TREE_CODE (arg2
) == VECTOR_CST
1600 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)),
1601 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
))))
1603 tree type
= TREE_TYPE (arg1
);
1605 if (VECTOR_CST_STEPPED_P (arg1
)
1606 && VECTOR_CST_STEPPED_P (arg2
))
1607 /* We can operate directly on the encoding if:
1609 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1611 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1613 Addition and subtraction are the supported operators
1614 for which this is true. */
1615 step_ok_p
= (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
1616 else if (VECTOR_CST_STEPPED_P (arg1
))
1617 /* We can operate directly on stepped encodings if:
1621 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1623 which is true if (x -> x op c) distributes over addition. */
1624 step_ok_p
= distributes_over_addition_p (code
, 1);
1626 /* Similarly in reverse. */
1627 step_ok_p
= distributes_over_addition_p (code
, 2);
1628 tree_vector_builder elts
;
1629 if (!elts
.new_binary_operation (type
, arg1
, arg2
, step_ok_p
))
1631 unsigned int count
= elts
.encoded_nelts ();
1632 for (unsigned int i
= 0; i
< count
; ++i
)
1634 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1635 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1637 tree elt
= const_binop (code
, elem1
, elem2
);
1639 /* It is possible that const_binop cannot handle the given
1640 code and return NULL_TREE */
1641 if (elt
== NULL_TREE
)
1643 elts
.quick_push (elt
);
1646 return elts
.build ();
1649 /* Shifts allow a scalar offset for a vector. */
1650 if (TREE_CODE (arg1
) == VECTOR_CST
1651 && TREE_CODE (arg2
) == INTEGER_CST
)
1653 tree type
= TREE_TYPE (arg1
);
1654 bool step_ok_p
= distributes_over_addition_p (code
, 1);
1655 tree_vector_builder elts
;
1656 if (!elts
.new_unary_operation (type
, arg1
, step_ok_p
))
1658 unsigned int count
= elts
.encoded_nelts ();
1659 for (unsigned int i
= 0; i
< count
; ++i
)
1661 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1663 tree elt
= const_binop (code
, elem1
, arg2
);
1665 /* It is possible that const_binop cannot handle the given
1666 code and return NULL_TREE. */
1667 if (elt
== NULL_TREE
)
1669 elts
.quick_push (elt
);
1672 return elts
.build ();
1677 /* Overload that adds a TYPE parameter to be able to dispatch
1678 to fold_relational_const. */
1681 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1683 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1684 return fold_relational_const (code
, type
, arg1
, arg2
);
1686 /* ??? Until we make the const_binop worker take the type of the
1687 result as argument put those cases that need it here. */
1690 case VEC_SERIES_EXPR
:
1691 if (CONSTANT_CLASS_P (arg1
)
1692 && CONSTANT_CLASS_P (arg2
))
1693 return build_vec_series (type
, arg1
, arg2
);
1697 if ((TREE_CODE (arg1
) == REAL_CST
1698 && TREE_CODE (arg2
) == REAL_CST
)
1699 || (TREE_CODE (arg1
) == INTEGER_CST
1700 && TREE_CODE (arg2
) == INTEGER_CST
))
1701 return build_complex (type
, arg1
, arg2
);
1704 case POINTER_DIFF_EXPR
:
1705 if (poly_int_tree_p (arg1
) && poly_int_tree_p (arg2
))
1707 poly_offset_int res
= (wi::to_poly_offset (arg1
)
1708 - wi::to_poly_offset (arg2
));
1709 return force_fit_type (type
, res
, 1,
1710 TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1714 case VEC_PACK_TRUNC_EXPR
:
1715 case VEC_PACK_FIX_TRUNC_EXPR
:
1716 case VEC_PACK_FLOAT_EXPR
:
1718 unsigned int HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1720 if (TREE_CODE (arg1
) != VECTOR_CST
1721 || TREE_CODE (arg2
) != VECTOR_CST
)
1724 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1727 out_nelts
= in_nelts
* 2;
1728 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1729 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1731 tree_vector_builder
elts (type
, out_nelts
, 1);
1732 for (i
= 0; i
< out_nelts
; i
++)
1734 tree elt
= (i
< in_nelts
1735 ? VECTOR_CST_ELT (arg1
, i
)
1736 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1737 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1739 : code
== VEC_PACK_FLOAT_EXPR
1740 ? FLOAT_EXPR
: FIX_TRUNC_EXPR
,
1741 TREE_TYPE (type
), elt
);
1742 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1744 elts
.quick_push (elt
);
1747 return elts
.build ();
1750 case VEC_WIDEN_MULT_LO_EXPR
:
1751 case VEC_WIDEN_MULT_HI_EXPR
:
1752 case VEC_WIDEN_MULT_EVEN_EXPR
:
1753 case VEC_WIDEN_MULT_ODD_EXPR
:
1755 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, out
, ofs
, scale
;
1757 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1760 if (!VECTOR_CST_NELTS (arg1
).is_constant (&in_nelts
))
1762 out_nelts
= in_nelts
/ 2;
1763 gcc_assert (known_eq (in_nelts
, VECTOR_CST_NELTS (arg2
))
1764 && known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1766 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1767 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1768 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1769 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1770 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1772 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1775 tree_vector_builder
elts (type
, out_nelts
, 1);
1776 for (out
= 0; out
< out_nelts
; out
++)
1778 unsigned int in
= (out
<< scale
) + ofs
;
1779 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1780 VECTOR_CST_ELT (arg1
, in
));
1781 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1782 VECTOR_CST_ELT (arg2
, in
));
1784 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1786 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1787 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1789 elts
.quick_push (elt
);
1792 return elts
.build ();
1798 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1801 /* Make sure type and arg0 have the same saturating flag. */
1802 gcc_checking_assert (TYPE_SATURATING (type
)
1803 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1805 return const_binop (code
, arg1
, arg2
);
1808 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1809 Return zero if computing the constants is not possible. */
1812 const_unop (enum tree_code code
, tree type
, tree arg0
)
1814 /* Don't perform the operation, other than NEGATE and ABS, if
1815 flag_signaling_nans is on and the operand is a signaling NaN. */
1816 if (TREE_CODE (arg0
) == REAL_CST
1817 && HONOR_SNANS (arg0
)
1818 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1819 && code
!= NEGATE_EXPR
1821 && code
!= ABSU_EXPR
)
1828 case FIX_TRUNC_EXPR
:
1829 case FIXED_CONVERT_EXPR
:
1830 return fold_convert_const (code
, type
, arg0
);
1832 case ADDR_SPACE_CONVERT_EXPR
:
1833 /* If the source address is 0, and the source address space
1834 cannot have a valid object at 0, fold to dest type null. */
1835 if (integer_zerop (arg0
)
1836 && !(targetm
.addr_space
.zero_address_valid
1837 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1838 return fold_convert_const (code
, type
, arg0
);
1841 case VIEW_CONVERT_EXPR
:
1842 return fold_view_convert_expr (type
, arg0
);
1846 /* Can't call fold_negate_const directly here as that doesn't
1847 handle all cases and we might not be able to negate some
1849 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1850 if (tem
&& CONSTANT_CLASS_P (tem
))
1857 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1858 return fold_abs_const (arg0
, type
);
1862 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1864 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1866 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1871 if (TREE_CODE (arg0
) == INTEGER_CST
)
1872 return fold_not_const (arg0
, type
);
1873 else if (POLY_INT_CST_P (arg0
))
1874 return wide_int_to_tree (type
, -poly_int_cst_value (arg0
));
1875 /* Perform BIT_NOT_EXPR on each element individually. */
1876 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1880 /* This can cope with stepped encodings because ~x == -1 - x. */
1881 tree_vector_builder elements
;
1882 elements
.new_unary_operation (type
, arg0
, true);
1883 unsigned int i
, count
= elements
.encoded_nelts ();
1884 for (i
= 0; i
< count
; ++i
)
1886 elem
= VECTOR_CST_ELT (arg0
, i
);
1887 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1888 if (elem
== NULL_TREE
)
1890 elements
.quick_push (elem
);
1893 return elements
.build ();
1897 case TRUTH_NOT_EXPR
:
1898 if (TREE_CODE (arg0
) == INTEGER_CST
)
1899 return constant_boolean_node (integer_zerop (arg0
), type
);
1903 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1904 return fold_convert (type
, TREE_REALPART (arg0
));
1908 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1909 return fold_convert (type
, TREE_IMAGPART (arg0
));
1912 case VEC_UNPACK_LO_EXPR
:
1913 case VEC_UNPACK_HI_EXPR
:
1914 case VEC_UNPACK_FLOAT_LO_EXPR
:
1915 case VEC_UNPACK_FLOAT_HI_EXPR
:
1916 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
1917 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
1919 unsigned HOST_WIDE_INT out_nelts
, in_nelts
, i
;
1920 enum tree_code subcode
;
1922 if (TREE_CODE (arg0
) != VECTOR_CST
)
1925 if (!VECTOR_CST_NELTS (arg0
).is_constant (&in_nelts
))
1927 out_nelts
= in_nelts
/ 2;
1928 gcc_assert (known_eq (out_nelts
, TYPE_VECTOR_SUBPARTS (type
)));
1930 unsigned int offset
= 0;
1931 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1932 || code
== VEC_UNPACK_FLOAT_LO_EXPR
1933 || code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
))
1936 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1938 else if (code
== VEC_UNPACK_FLOAT_LO_EXPR
1939 || code
== VEC_UNPACK_FLOAT_HI_EXPR
)
1940 subcode
= FLOAT_EXPR
;
1942 subcode
= FIX_TRUNC_EXPR
;
1944 tree_vector_builder
elts (type
, out_nelts
, 1);
1945 for (i
= 0; i
< out_nelts
; i
++)
1947 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1948 VECTOR_CST_ELT (arg0
, i
+ offset
));
1949 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1951 elts
.quick_push (elt
);
1954 return elts
.build ();
1957 case VEC_DUPLICATE_EXPR
:
1958 if (CONSTANT_CLASS_P (arg0
))
1959 return build_vector_from_val (type
, arg0
);
1969 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1970 indicates which particular sizetype to create. */
1973 size_int_kind (poly_int64 number
, enum size_type_kind kind
)
1975 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1978 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1979 is a tree code. The type of the result is taken from the operands.
1980 Both must be equivalent integer types, ala int_binop_types_match_p.
1981 If the operands are constant, so is the result. */
1984 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1986 tree type
= TREE_TYPE (arg0
);
1988 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1989 return error_mark_node
;
1991 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1994 /* Handle the special case of two poly_int constants faster. */
1995 if (poly_int_tree_p (arg0
) && poly_int_tree_p (arg1
))
1997 /* And some specific cases even faster than that. */
1998 if (code
== PLUS_EXPR
)
2000 if (integer_zerop (arg0
)
2001 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
2003 if (integer_zerop (arg1
)
2004 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
2007 else if (code
== MINUS_EXPR
)
2009 if (integer_zerop (arg1
)
2010 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1
)))
2013 else if (code
== MULT_EXPR
)
2015 if (integer_onep (arg0
)
2016 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0
)))
2020 /* Handle general case of two integer constants. For sizetype
2021 constant calculations we always want to know about overflow,
2022 even in the unsigned case. */
2023 tree res
= int_const_binop (code
, arg0
, arg1
, -1);
2024 if (res
!= NULL_TREE
)
2028 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
2031 /* Given two values, either both of sizetype or both of bitsizetype,
2032 compute the difference between the two values. Return the value
2033 in signed type corresponding to the type of the operands. */
2036 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
2038 tree type
= TREE_TYPE (arg0
);
2041 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
2044 /* If the type is already signed, just do the simple thing. */
2045 if (!TYPE_UNSIGNED (type
))
2046 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
2048 if (type
== sizetype
)
2050 else if (type
== bitsizetype
)
2051 ctype
= sbitsizetype
;
2053 ctype
= signed_type_for (type
);
2055 /* If either operand is not a constant, do the conversions to the signed
2056 type and subtract. The hardware will do the right thing with any
2057 overflow in the subtraction. */
2058 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
2059 return size_binop_loc (loc
, MINUS_EXPR
,
2060 fold_convert_loc (loc
, ctype
, arg0
),
2061 fold_convert_loc (loc
, ctype
, arg1
));
2063 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2064 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2065 overflow) and negate (which can't either). Special-case a result
2066 of zero while we're here. */
2067 if (tree_int_cst_equal (arg0
, arg1
))
2068 return build_int_cst (ctype
, 0);
2069 else if (tree_int_cst_lt (arg1
, arg0
))
2070 return fold_convert_loc (loc
, ctype
,
2071 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
2073 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
2074 fold_convert_loc (loc
, ctype
,
2075 size_binop_loc (loc
,
2080 /* A subroutine of fold_convert_const handling conversions of an
2081 INTEGER_CST to another integer type. */
2084 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
2086 /* Given an integer constant, make new constant with new type,
2087 appropriately sign-extended or truncated. Use widest_int
2088 so that any extension is done according ARG1's type. */
2089 return force_fit_type (type
, wi::to_widest (arg1
),
2090 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
2091 TREE_OVERFLOW (arg1
));
2094 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2095 to an integer type. */
2098 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
2100 bool overflow
= false;
2103 /* The following code implements the floating point to integer
2104 conversion rules required by the Java Language Specification,
2105 that IEEE NaNs are mapped to zero and values that overflow
2106 the target precision saturate, i.e. values greater than
2107 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2108 are mapped to INT_MIN. These semantics are allowed by the
2109 C and C++ standards that simply state that the behavior of
2110 FP-to-integer conversion is unspecified upon overflow. */
2114 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
2118 case FIX_TRUNC_EXPR
:
2119 real_trunc (&r
, VOIDmode
, &x
);
2126 /* If R is NaN, return zero and show we have an overflow. */
2127 if (REAL_VALUE_ISNAN (r
))
2130 val
= wi::zero (TYPE_PRECISION (type
));
2133 /* See if R is less than the lower bound or greater than the
2138 tree lt
= TYPE_MIN_VALUE (type
);
2139 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
2140 if (real_less (&r
, &l
))
2143 val
= wi::to_wide (lt
);
2149 tree ut
= TYPE_MAX_VALUE (type
);
2152 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
2153 if (real_less (&u
, &r
))
2156 val
= wi::to_wide (ut
);
2162 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
2164 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
2168 /* A subroutine of fold_convert_const handling conversions of a
2169 FIXED_CST to an integer type. */
2172 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
2175 double_int temp
, temp_trunc
;
2178 /* Right shift FIXED_CST to temp by fbit. */
2179 temp
= TREE_FIXED_CST (arg1
).data
;
2180 mode
= TREE_FIXED_CST (arg1
).mode
;
2181 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
2183 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
2184 HOST_BITS_PER_DOUBLE_INT
,
2185 SIGNED_FIXED_POINT_MODE_P (mode
));
2187 /* Left shift temp to temp_trunc by fbit. */
2188 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
2189 HOST_BITS_PER_DOUBLE_INT
,
2190 SIGNED_FIXED_POINT_MODE_P (mode
));
2194 temp
= double_int_zero
;
2195 temp_trunc
= double_int_zero
;
2198 /* If FIXED_CST is negative, we need to round the value toward 0.
2199 By checking if the fractional bits are not zero to add 1 to temp. */
2200 if (SIGNED_FIXED_POINT_MODE_P (mode
)
2201 && temp_trunc
.is_negative ()
2202 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
2203 temp
+= double_int_one
;
2205 /* Given a fixed-point constant, make new constant with new type,
2206 appropriately sign-extended or truncated. */
2207 t
= force_fit_type (type
, temp
, -1,
2208 (temp
.is_negative ()
2209 && (TYPE_UNSIGNED (type
)
2210 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
2211 | TREE_OVERFLOW (arg1
));
2216 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2217 to another floating point type. */
2220 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
2222 REAL_VALUE_TYPE value
;
2225 /* If the underlying modes are the same, simply treat it as
2226 copy and rebuild with TREE_REAL_CST information and the
2228 if (TYPE_MODE (type
) == TYPE_MODE (TREE_TYPE (arg1
)))
2230 t
= build_real (type
, TREE_REAL_CST (arg1
));
2234 /* Don't perform the operation if flag_signaling_nans is on
2235 and the operand is a signaling NaN. */
2236 if (HONOR_SNANS (arg1
)
2237 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2240 /* With flag_rounding_math we should respect the current rounding mode
2241 unless the conversion is exact. */
2242 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1
)
2243 && !exact_real_truncate (TYPE_MODE (type
), &TREE_REAL_CST (arg1
)))
2246 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2247 t
= build_real (type
, value
);
2249 /* If converting an infinity or NAN to a representation that doesn't
2250 have one, set the overflow bit so that we can produce some kind of
2251 error message at the appropriate point if necessary. It's not the
2252 most user-friendly message, but it's better than nothing. */
2253 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2254 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2255 TREE_OVERFLOW (t
) = 1;
2256 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2257 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2258 TREE_OVERFLOW (t
) = 1;
2259 /* Regular overflow, conversion produced an infinity in a mode that
2260 can't represent them. */
2261 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2262 && REAL_VALUE_ISINF (value
)
2263 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2264 TREE_OVERFLOW (t
) = 1;
2266 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2270 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2271 to a floating point type. */
2274 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2276 REAL_VALUE_TYPE value
;
2279 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2280 &TREE_FIXED_CST (arg1
));
2281 t
= build_real (type
, value
);
2283 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2287 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2288 to another fixed-point type. */
2291 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2293 FIXED_VALUE_TYPE value
;
2297 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2298 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2299 t
= build_fixed (type
, value
);
2301 /* Propagate overflow flags. */
2302 if (overflow_p
| TREE_OVERFLOW (arg1
))
2303 TREE_OVERFLOW (t
) = 1;
2307 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2308 to a fixed-point type. */
2311 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2313 FIXED_VALUE_TYPE value
;
2318 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2320 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2321 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2322 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2324 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2326 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2327 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2328 TYPE_SATURATING (type
));
2329 t
= build_fixed (type
, value
);
2331 /* Propagate overflow flags. */
2332 if (overflow_p
| TREE_OVERFLOW (arg1
))
2333 TREE_OVERFLOW (t
) = 1;
2337 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2338 to a fixed-point type. */
2341 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2343 FIXED_VALUE_TYPE value
;
2347 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2348 &TREE_REAL_CST (arg1
),
2349 TYPE_SATURATING (type
));
2350 t
= build_fixed (type
, value
);
2352 /* Propagate overflow flags. */
2353 if (overflow_p
| TREE_OVERFLOW (arg1
))
2354 TREE_OVERFLOW (t
) = 1;
2358 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2359 type TYPE. If no simplification can be done return NULL_TREE. */
2362 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2364 tree arg_type
= TREE_TYPE (arg1
);
2365 if (arg_type
== type
)
2368 /* We can't widen types, since the runtime value could overflow the
2369 original type before being extended to the new type. */
2370 if (POLY_INT_CST_P (arg1
)
2371 && (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2372 && TYPE_PRECISION (type
) <= TYPE_PRECISION (arg_type
))
2373 return build_poly_int_cst (type
,
2374 poly_wide_int::from (poly_int_cst_value (arg1
),
2375 TYPE_PRECISION (type
),
2376 TYPE_SIGN (arg_type
)));
2378 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2379 || TREE_CODE (type
) == OFFSET_TYPE
)
2381 if (TREE_CODE (arg1
) == INTEGER_CST
)
2382 return fold_convert_const_int_from_int (type
, arg1
);
2383 else if (TREE_CODE (arg1
) == REAL_CST
)
2384 return fold_convert_const_int_from_real (code
, type
, arg1
);
2385 else if (TREE_CODE (arg1
) == FIXED_CST
)
2386 return fold_convert_const_int_from_fixed (type
, arg1
);
2388 else if (SCALAR_FLOAT_TYPE_P (type
))
2390 if (TREE_CODE (arg1
) == INTEGER_CST
)
2392 tree res
= build_real_from_int_cst (type
, arg1
);
2393 /* Avoid the folding if flag_rounding_math is on and the
2394 conversion is not exact. */
2395 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
2398 wide_int w
= real_to_integer (&TREE_REAL_CST (res
), &fail
,
2399 TYPE_PRECISION (TREE_TYPE (arg1
)));
2400 if (fail
|| wi::ne_p (w
, wi::to_wide (arg1
)))
2405 else if (TREE_CODE (arg1
) == REAL_CST
)
2406 return fold_convert_const_real_from_real (type
, arg1
);
2407 else if (TREE_CODE (arg1
) == FIXED_CST
)
2408 return fold_convert_const_real_from_fixed (type
, arg1
);
2410 else if (FIXED_POINT_TYPE_P (type
))
2412 if (TREE_CODE (arg1
) == FIXED_CST
)
2413 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2414 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2415 return fold_convert_const_fixed_from_int (type
, arg1
);
2416 else if (TREE_CODE (arg1
) == REAL_CST
)
2417 return fold_convert_const_fixed_from_real (type
, arg1
);
2419 else if (VECTOR_TYPE_P (type
))
2421 if (TREE_CODE (arg1
) == VECTOR_CST
2422 && known_eq (TYPE_VECTOR_SUBPARTS (type
), VECTOR_CST_NELTS (arg1
)))
2424 tree elttype
= TREE_TYPE (type
);
2425 tree arg1_elttype
= TREE_TYPE (TREE_TYPE (arg1
));
2426 /* We can't handle steps directly when extending, since the
2427 values need to wrap at the original precision first. */
2429 = (INTEGRAL_TYPE_P (elttype
)
2430 && INTEGRAL_TYPE_P (arg1_elttype
)
2431 && TYPE_PRECISION (elttype
) <= TYPE_PRECISION (arg1_elttype
));
2432 tree_vector_builder v
;
2433 if (!v
.new_unary_operation (type
, arg1
, step_ok_p
))
2435 unsigned int len
= v
.encoded_nelts ();
2436 for (unsigned int i
= 0; i
< len
; ++i
)
2438 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2439 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2440 if (cvt
== NULL_TREE
)
2450 /* Construct a vector of zero elements of vector type TYPE. */
2453 build_zero_vector (tree type
)
2457 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2458 return build_vector_from_val (type
, t
);
2461 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2464 fold_convertible_p (const_tree type
, const_tree arg
)
2466 const_tree orig
= TREE_TYPE (arg
);
2471 if (TREE_CODE (arg
) == ERROR_MARK
2472 || TREE_CODE (type
) == ERROR_MARK
2473 || TREE_CODE (orig
) == ERROR_MARK
)
2476 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2479 switch (TREE_CODE (type
))
2481 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2482 case POINTER_TYPE
: case REFERENCE_TYPE
:
2484 return (INTEGRAL_TYPE_P (orig
)
2485 || (POINTER_TYPE_P (orig
)
2486 && TYPE_PRECISION (type
) <= TYPE_PRECISION (orig
))
2487 || TREE_CODE (orig
) == OFFSET_TYPE
);
2490 case FIXED_POINT_TYPE
:
2492 return TREE_CODE (type
) == TREE_CODE (orig
);
2495 return (VECTOR_TYPE_P (orig
)
2496 && known_eq (TYPE_VECTOR_SUBPARTS (type
),
2497 TYPE_VECTOR_SUBPARTS (orig
))
2498 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2505 /* Convert expression ARG to type TYPE. Used by the middle-end for
2506 simple conversions in preference to calling the front-end's convert. */
2509 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2511 tree orig
= TREE_TYPE (arg
);
2517 if (TREE_CODE (arg
) == ERROR_MARK
2518 || TREE_CODE (type
) == ERROR_MARK
2519 || TREE_CODE (orig
) == ERROR_MARK
)
2520 return error_mark_node
;
2522 switch (TREE_CODE (type
))
2525 case REFERENCE_TYPE
:
2526 /* Handle conversions between pointers to different address spaces. */
2527 if (POINTER_TYPE_P (orig
)
2528 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2529 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2530 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2533 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2535 if (TREE_CODE (arg
) == INTEGER_CST
)
2537 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2538 if (tem
!= NULL_TREE
)
2541 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2542 || TREE_CODE (orig
) == OFFSET_TYPE
)
2543 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2544 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2545 return fold_convert_loc (loc
, type
,
2546 fold_build1_loc (loc
, REALPART_EXPR
,
2547 TREE_TYPE (orig
), arg
));
2548 gcc_assert (VECTOR_TYPE_P (orig
)
2549 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2550 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2553 if (TREE_CODE (arg
) == INTEGER_CST
)
2555 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2556 if (tem
!= NULL_TREE
)
2559 else if (TREE_CODE (arg
) == REAL_CST
)
2561 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2562 if (tem
!= NULL_TREE
)
2565 else if (TREE_CODE (arg
) == FIXED_CST
)
2567 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2568 if (tem
!= NULL_TREE
)
2572 switch (TREE_CODE (orig
))
2575 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2576 case POINTER_TYPE
: case REFERENCE_TYPE
:
2577 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2580 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2582 case FIXED_POINT_TYPE
:
2583 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2586 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2587 return fold_convert_loc (loc
, type
, tem
);
2593 case FIXED_POINT_TYPE
:
2594 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2595 || TREE_CODE (arg
) == REAL_CST
)
2597 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2598 if (tem
!= NULL_TREE
)
2599 goto fold_convert_exit
;
2602 switch (TREE_CODE (orig
))
2604 case FIXED_POINT_TYPE
:
2609 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2612 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2613 return fold_convert_loc (loc
, type
, tem
);
2620 switch (TREE_CODE (orig
))
2623 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2624 case POINTER_TYPE
: case REFERENCE_TYPE
:
2626 case FIXED_POINT_TYPE
:
2627 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2628 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2629 fold_convert_loc (loc
, TREE_TYPE (type
),
2630 integer_zero_node
));
2635 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2637 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2638 TREE_OPERAND (arg
, 0));
2639 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2640 TREE_OPERAND (arg
, 1));
2641 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2644 arg
= save_expr (arg
);
2645 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2646 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2647 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2648 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2649 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2657 if (integer_zerop (arg
))
2658 return build_zero_vector (type
);
2659 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2660 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2661 || VECTOR_TYPE_P (orig
));
2662 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2665 tem
= fold_ignored_result (arg
);
2666 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2669 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2670 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2674 tem
= protected_set_expr_location_unshare (tem
, loc
);
2678 /* Return false if expr can be assumed not to be an lvalue, true
2682 maybe_lvalue_p (const_tree x
)
2684 /* We only need to wrap lvalue tree codes. */
2685 switch (TREE_CODE (x
))
2693 case COMPOUND_LITERAL_EXPR
:
2699 case ARRAY_RANGE_REF
:
2705 case PREINCREMENT_EXPR
:
2706 case PREDECREMENT_EXPR
:
2708 case TRY_CATCH_EXPR
:
2709 case WITH_CLEANUP_EXPR
:
2715 case VIEW_CONVERT_EXPR
:
2719 /* Assume the worst for front-end tree codes. */
2720 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2728 /* Return an expr equal to X but certainly not valid as an lvalue. */
2731 non_lvalue_loc (location_t loc
, tree x
)
2733 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2738 if (! maybe_lvalue_p (x
))
2740 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2743 /* Given a tree comparison code, return the code that is the logical inverse.
2744 It is generally not safe to do this for floating-point comparisons, except
2745 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2746 ERROR_MARK in this case. */
2749 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2751 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2752 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2762 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2764 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2766 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2768 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2782 return UNORDERED_EXPR
;
2783 case UNORDERED_EXPR
:
2784 return ORDERED_EXPR
;
2790 /* Similar, but return the comparison that results if the operands are
2791 swapped. This is safe for floating-point. */
2794 swap_tree_comparison (enum tree_code code
)
2801 case UNORDERED_EXPR
:
2827 /* Convert a comparison tree code from an enum tree_code representation
2828 into a compcode bit-based encoding. This function is the inverse of
2829 compcode_to_comparison. */
2831 static enum comparison_code
2832 comparison_to_compcode (enum tree_code code
)
2849 return COMPCODE_ORD
;
2850 case UNORDERED_EXPR
:
2851 return COMPCODE_UNORD
;
2853 return COMPCODE_UNLT
;
2855 return COMPCODE_UNEQ
;
2857 return COMPCODE_UNLE
;
2859 return COMPCODE_UNGT
;
2861 return COMPCODE_LTGT
;
2863 return COMPCODE_UNGE
;
2869 /* Convert a compcode bit-based encoding of a comparison operator back
2870 to GCC's enum tree_code representation. This function is the
2871 inverse of comparison_to_compcode. */
2873 static enum tree_code
2874 compcode_to_comparison (enum comparison_code code
)
2891 return ORDERED_EXPR
;
2892 case COMPCODE_UNORD
:
2893 return UNORDERED_EXPR
;
2911 /* Return true if COND1 tests the opposite condition of COND2. */
2914 inverse_conditions_p (const_tree cond1
, const_tree cond2
)
2916 return (COMPARISON_CLASS_P (cond1
)
2917 && COMPARISON_CLASS_P (cond2
)
2918 && (invert_tree_comparison
2920 HONOR_NANS (TREE_OPERAND (cond1
, 0))) == TREE_CODE (cond2
))
2921 && operand_equal_p (TREE_OPERAND (cond1
, 0),
2922 TREE_OPERAND (cond2
, 0), 0)
2923 && operand_equal_p (TREE_OPERAND (cond1
, 1),
2924 TREE_OPERAND (cond2
, 1), 0));
2927 /* Return a tree for the comparison which is the combination of
2928 doing the AND or OR (depending on CODE) of the two operations LCODE
2929 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2930 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2931 if this makes the transformation invalid. */
2934 combine_comparisons (location_t loc
,
2935 enum tree_code code
, enum tree_code lcode
,
2936 enum tree_code rcode
, tree truth_type
,
2937 tree ll_arg
, tree lr_arg
)
2939 bool honor_nans
= HONOR_NANS (ll_arg
);
2940 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2941 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2946 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2947 compcode
= lcompcode
& rcompcode
;
2950 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2951 compcode
= lcompcode
| rcompcode
;
2960 /* Eliminate unordered comparisons, as well as LTGT and ORD
2961 which are not used unless the mode has NaNs. */
2962 compcode
&= ~COMPCODE_UNORD
;
2963 if (compcode
== COMPCODE_LTGT
)
2964 compcode
= COMPCODE_NE
;
2965 else if (compcode
== COMPCODE_ORD
)
2966 compcode
= COMPCODE_TRUE
;
2968 else if (flag_trapping_math
)
2970 /* Check that the original operation and the optimized ones will trap
2971 under the same condition. */
2972 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2973 && (lcompcode
!= COMPCODE_EQ
)
2974 && (lcompcode
!= COMPCODE_ORD
);
2975 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2976 && (rcompcode
!= COMPCODE_EQ
)
2977 && (rcompcode
!= COMPCODE_ORD
);
2978 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2979 && (compcode
!= COMPCODE_EQ
)
2980 && (compcode
!= COMPCODE_ORD
);
2982 /* In a short-circuited boolean expression the LHS might be
2983 such that the RHS, if evaluated, will never trap. For
2984 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2985 if neither x nor y is NaN. (This is a mixed blessing: for
2986 example, the expression above will never trap, hence
2987 optimizing it to x < y would be invalid). */
2988 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2989 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2992 /* If the comparison was short-circuited, and only the RHS
2993 trapped, we may now generate a spurious trap. */
2995 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2998 /* If we changed the conditions that cause a trap, we lose. */
2999 if ((ltrap
|| rtrap
) != trap
)
3003 if (compcode
== COMPCODE_TRUE
)
3004 return constant_boolean_node (true, truth_type
);
3005 else if (compcode
== COMPCODE_FALSE
)
3006 return constant_boolean_node (false, truth_type
);
3009 enum tree_code tcode
;
3011 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
3012 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
3016 /* Return nonzero if two operands (typically of the same tree node)
3017 are necessarily equal. FLAGS modifies behavior as follows:
3019 If OEP_ONLY_CONST is set, only return nonzero for constants.
3020 This function tests whether the operands are indistinguishable;
3021 it does not test whether they are equal using C's == operation.
3022 The distinction is important for IEEE floating point, because
3023 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3024 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3026 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3027 even though it may hold multiple values during a function.
3028 This is because a GCC tree node guarantees that nothing else is
3029 executed between the evaluation of its "operands" (which may often
3030 be evaluated in arbitrary order). Hence if the operands themselves
3031 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3032 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3033 unset means assuming isochronic (or instantaneous) tree equivalence.
3034 Unless comparing arbitrary expression trees, such as from different
3035 statements, this flag can usually be left unset.
3037 If OEP_PURE_SAME is set, then pure functions with identical arguments
3038 are considered the same. It is used when the caller has other ways
3039 to ensure that global memory is unchanged in between.
3041 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3042 not values of expressions.
3044 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3045 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3047 If OEP_BITWISE is set, then require the values to be bitwise identical
3048 rather than simply numerically equal. Do not take advantage of things
3049 like math-related flags or undefined behavior; only return true for
3050 values that are provably bitwise identical in all circumstances.
3052 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3053 any operand with side effect. This is unnecesarily conservative in the
3054 case we know that arg0 and arg1 are in disjoint code paths (such as in
3055 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3056 addresses with TREE_CONSTANT flag set so we know that &var == &var
3057 even if var is volatile. */
3060 operand_compare::operand_equal_p (const_tree arg0
, const_tree arg1
,
3064 if (verify_hash_value (arg0
, arg1
, flags
, &r
))
3067 STRIP_ANY_LOCATION_WRAPPER (arg0
);
3068 STRIP_ANY_LOCATION_WRAPPER (arg1
);
3070 /* If either is ERROR_MARK, they aren't equal. */
3071 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
3072 || TREE_TYPE (arg0
) == error_mark_node
3073 || TREE_TYPE (arg1
) == error_mark_node
)
3076 /* Similar, if either does not have a type (like a template id),
3077 they aren't equal. */
3078 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
3081 /* Bitwise identity makes no sense if the values have different layouts. */
3082 if ((flags
& OEP_BITWISE
)
3083 && !tree_nop_conversion_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3086 /* We cannot consider pointers to different address space equal. */
3087 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
3088 && POINTER_TYPE_P (TREE_TYPE (arg1
))
3089 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
3090 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
3093 /* Check equality of integer constants before bailing out due to
3094 precision differences. */
3095 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3097 /* Address of INTEGER_CST is not defined; check that we did not forget
3098 to drop the OEP_ADDRESS_OF flags. */
3099 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3100 return tree_int_cst_equal (arg0
, arg1
);
3103 if (!(flags
& OEP_ADDRESS_OF
))
3105 /* If both types don't have the same signedness, then we can't consider
3106 them equal. We must check this before the STRIP_NOPS calls
3107 because they may change the signedness of the arguments. As pointers
3108 strictly don't have a signedness, require either two pointers or
3109 two non-pointers as well. */
3110 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
3111 || POINTER_TYPE_P (TREE_TYPE (arg0
))
3112 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
3115 /* If both types don't have the same precision, then it is not safe
3117 if (element_precision (TREE_TYPE (arg0
))
3118 != element_precision (TREE_TYPE (arg1
)))
3125 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3126 sanity check once the issue is solved. */
3128 /* Addresses of conversions and SSA_NAMEs (and many other things)
3129 are not defined. Check that we did not forget to drop the
3130 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3131 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
3132 && TREE_CODE (arg0
) != SSA_NAME
);
3135 /* In case both args are comparisons but with different comparison
3136 code, try to swap the comparison operands of one arg to produce
3137 a match and compare that variant. */
3138 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3139 && COMPARISON_CLASS_P (arg0
)
3140 && COMPARISON_CLASS_P (arg1
))
3142 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
3144 if (TREE_CODE (arg0
) == swap_code
)
3145 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3146 TREE_OPERAND (arg1
, 1), flags
)
3147 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3148 TREE_OPERAND (arg1
, 0), flags
);
3151 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
3153 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3154 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
3156 else if (flags
& OEP_ADDRESS_OF
)
3158 /* If we are interested in comparing addresses ignore
3159 MEM_REF wrappings of the base that can appear just for
3161 if (TREE_CODE (arg0
) == MEM_REF
3163 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
3164 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
3165 && integer_zerop (TREE_OPERAND (arg0
, 1)))
3167 else if (TREE_CODE (arg1
) == MEM_REF
3169 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
3170 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
3171 && integer_zerop (TREE_OPERAND (arg1
, 1)))
3179 /* When not checking adddresses, this is needed for conversions and for
3180 COMPONENT_REF. Might as well play it safe and always test this. */
3181 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
3182 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
3183 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
3184 && !(flags
& OEP_ADDRESS_OF
)))
3187 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3188 We don't care about side effects in that case because the SAVE_EXPR
3189 takes care of that for us. In all other cases, two expressions are
3190 equal if they have no side effects. If we have two identical
3191 expressions with side effects that should be treated the same due
3192 to the only side effects being identical SAVE_EXPR's, that will
3193 be detected in the recursive calls below.
3194 If we are taking an invariant address of two identical objects
3195 they are necessarily equal as well. */
3196 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
3197 && (TREE_CODE (arg0
) == SAVE_EXPR
3198 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
3199 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
3202 /* Next handle constant cases, those for which we can return 1 even
3203 if ONLY_CONST is set. */
3204 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
3205 switch (TREE_CODE (arg0
))
3208 return tree_int_cst_equal (arg0
, arg1
);
3211 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
3212 TREE_FIXED_CST (arg1
));
3215 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
3218 if (!(flags
& OEP_BITWISE
) && !HONOR_SIGNED_ZEROS (arg0
))
3220 /* If we do not distinguish between signed and unsigned zero,
3221 consider them equal. */
3222 if (real_zerop (arg0
) && real_zerop (arg1
))
3229 if (VECTOR_CST_LOG2_NPATTERNS (arg0
)
3230 != VECTOR_CST_LOG2_NPATTERNS (arg1
))
3233 if (VECTOR_CST_NELTS_PER_PATTERN (arg0
)
3234 != VECTOR_CST_NELTS_PER_PATTERN (arg1
))
3237 unsigned int count
= vector_cst_encoded_nelts (arg0
);
3238 for (unsigned int i
= 0; i
< count
; ++i
)
3239 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0
, i
),
3240 VECTOR_CST_ENCODED_ELT (arg1
, i
), flags
))
3246 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
3248 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
3252 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
3253 && ! memcmp (TREE_STRING_POINTER (arg0
),
3254 TREE_STRING_POINTER (arg1
),
3255 TREE_STRING_LENGTH (arg0
)));
3258 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3259 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
3260 flags
| OEP_ADDRESS_OF
3261 | OEP_MATCH_SIDE_EFFECTS
);
3263 /* In GIMPLE empty constructors are allowed in initializers of
3265 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
3270 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3271 two instances of undefined behavior will give identical results. */
3272 if (flags
& (OEP_ONLY_CONST
| OEP_BITWISE
))
3275 /* Define macros to test an operand from arg0 and arg1 for equality and a
3276 variant that allows null and views null as being different from any
3277 non-null value. In the latter case, if either is null, the both
3278 must be; otherwise, do the normal comparison. */
3279 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3280 TREE_OPERAND (arg1, N), flags)
3282 #define OP_SAME_WITH_NULL(N) \
3283 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3284 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3286 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
3289 /* Two conversions are equal only if signedness and modes match. */
3290 switch (TREE_CODE (arg0
))
3293 case FIX_TRUNC_EXPR
:
3294 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3295 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3305 case tcc_comparison
:
3307 if (OP_SAME (0) && OP_SAME (1))
3310 /* For commutative ops, allow the other order. */
3311 return (commutative_tree_code (TREE_CODE (arg0
))
3312 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3313 TREE_OPERAND (arg1
, 1), flags
)
3314 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3315 TREE_OPERAND (arg1
, 0), flags
));
3318 /* If either of the pointer (or reference) expressions we are
3319 dereferencing contain a side effect, these cannot be equal,
3320 but their addresses can be. */
3321 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3322 && (TREE_SIDE_EFFECTS (arg0
)
3323 || TREE_SIDE_EFFECTS (arg1
)))
3326 switch (TREE_CODE (arg0
))
3329 if (!(flags
& OEP_ADDRESS_OF
))
3331 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3332 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3334 /* Verify that the access types are compatible. */
3335 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0
))
3336 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)))
3339 flags
&= ~OEP_ADDRESS_OF
;
3343 /* Require the same offset. */
3344 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3345 TYPE_SIZE (TREE_TYPE (arg1
)),
3346 flags
& ~OEP_ADDRESS_OF
))
3351 case VIEW_CONVERT_EXPR
:
3354 case TARGET_MEM_REF
:
3356 if (!(flags
& OEP_ADDRESS_OF
))
3358 /* Require equal access sizes */
3359 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3360 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3361 || !TYPE_SIZE (TREE_TYPE (arg1
))
3362 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3363 TYPE_SIZE (TREE_TYPE (arg1
)),
3366 /* Verify that access happens in similar types. */
3367 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3369 /* Verify that accesses are TBAA compatible. */
3370 if (!alias_ptr_types_compatible_p
3371 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3372 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3373 || (MR_DEPENDENCE_CLIQUE (arg0
)
3374 != MR_DEPENDENCE_CLIQUE (arg1
))
3375 || (MR_DEPENDENCE_BASE (arg0
)
3376 != MR_DEPENDENCE_BASE (arg1
)))
3378 /* Verify that alignment is compatible. */
3379 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3380 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3383 flags
&= ~OEP_ADDRESS_OF
;
3384 return (OP_SAME (0) && OP_SAME (1)
3385 /* TARGET_MEM_REF require equal extra operands. */
3386 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3387 || (OP_SAME_WITH_NULL (2)
3388 && OP_SAME_WITH_NULL (3)
3389 && OP_SAME_WITH_NULL (4))));
3392 case ARRAY_RANGE_REF
:
3395 flags
&= ~OEP_ADDRESS_OF
;
3396 /* Compare the array index by value if it is constant first as we
3397 may have different types but same value here. */
3398 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3399 TREE_OPERAND (arg1
, 1))
3401 && OP_SAME_WITH_NULL (2)
3402 && OP_SAME_WITH_NULL (3)
3403 /* Compare low bound and element size as with OEP_ADDRESS_OF
3404 we have to account for the offset of the ref. */
3405 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3406 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3407 || (operand_equal_p (array_ref_low_bound
3408 (CONST_CAST_TREE (arg0
)),
3410 (CONST_CAST_TREE (arg1
)), flags
)
3411 && operand_equal_p (array_ref_element_size
3412 (CONST_CAST_TREE (arg0
)),
3413 array_ref_element_size
3414 (CONST_CAST_TREE (arg1
)),
3418 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3419 may be NULL when we're called to compare MEM_EXPRs. */
3420 if (!OP_SAME_WITH_NULL (0))
3423 bool compare_address
= flags
& OEP_ADDRESS_OF
;
3425 /* Most of time we only need to compare FIELD_DECLs for equality.
3426 However when determining address look into actual offsets.
3427 These may match for unions and unshared record types. */
3428 flags
&= ~OEP_ADDRESS_OF
;
3432 && (flags
& OEP_ADDRESS_OF_SAME_FIELD
) == 0)
3434 tree field0
= TREE_OPERAND (arg0
, 1);
3435 tree field1
= TREE_OPERAND (arg1
, 1);
3437 /* Non-FIELD_DECL operands can appear in C++ templates. */
3438 if (TREE_CODE (field0
) != FIELD_DECL
3439 || TREE_CODE (field1
) != FIELD_DECL
3440 || !operand_equal_p (DECL_FIELD_OFFSET (field0
),
3441 DECL_FIELD_OFFSET (field1
), flags
)
3442 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0
),
3443 DECL_FIELD_BIT_OFFSET (field1
),
3451 return OP_SAME_WITH_NULL (2);
3456 flags
&= ~OEP_ADDRESS_OF
;
3457 return OP_SAME (1) && OP_SAME (2);
3463 case tcc_expression
:
3464 switch (TREE_CODE (arg0
))
3467 /* Be sure we pass right ADDRESS_OF flag. */
3468 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3469 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3470 TREE_OPERAND (arg1
, 0),
3471 flags
| OEP_ADDRESS_OF
);
3473 case TRUTH_NOT_EXPR
:
3476 case TRUTH_ANDIF_EXPR
:
3477 case TRUTH_ORIF_EXPR
:
3478 return OP_SAME (0) && OP_SAME (1);
3480 case WIDEN_MULT_PLUS_EXPR
:
3481 case WIDEN_MULT_MINUS_EXPR
:
3484 /* The multiplcation operands are commutative. */
3487 case TRUTH_AND_EXPR
:
3489 case TRUTH_XOR_EXPR
:
3490 if (OP_SAME (0) && OP_SAME (1))
3493 /* Otherwise take into account this is a commutative operation. */
3494 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3495 TREE_OPERAND (arg1
, 1), flags
)
3496 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3497 TREE_OPERAND (arg1
, 0), flags
));
3500 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3502 flags
&= ~OEP_ADDRESS_OF
;
3505 case BIT_INSERT_EXPR
:
3506 /* BIT_INSERT_EXPR has an implict operand as the type precision
3507 of op1. Need to check to make sure they are the same. */
3508 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3509 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3510 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3511 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3517 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3522 case PREDECREMENT_EXPR
:
3523 case PREINCREMENT_EXPR
:
3524 case POSTDECREMENT_EXPR
:
3525 case POSTINCREMENT_EXPR
:
3526 if (flags
& OEP_LEXICOGRAPHIC
)
3527 return OP_SAME (0) && OP_SAME (1);
3530 case CLEANUP_POINT_EXPR
:
3533 if (flags
& OEP_LEXICOGRAPHIC
)
3538 /* Virtual table reference. */
3539 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0
),
3540 OBJ_TYPE_REF_EXPR (arg1
), flags
))
3542 flags
&= ~OEP_ADDRESS_OF
;
3543 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0
))
3544 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1
)))
3546 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0
),
3547 OBJ_TYPE_REF_OBJECT (arg1
), flags
))
3549 if (virtual_method_call_p (arg0
))
3551 if (!virtual_method_call_p (arg1
))
3553 return types_same_for_odr (obj_type_ref_class (arg0
),
3554 obj_type_ref_class (arg1
));
3563 switch (TREE_CODE (arg0
))
3566 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3567 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3568 /* If not both CALL_EXPRs are either internal or normal function
3569 functions, then they are not equal. */
3571 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3573 /* If the CALL_EXPRs call different internal functions, then they
3575 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3580 /* If the CALL_EXPRs call different functions, then they are not
3582 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3587 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3589 unsigned int cef
= call_expr_flags (arg0
);
3590 if (flags
& OEP_PURE_SAME
)
3591 cef
&= ECF_CONST
| ECF_PURE
;
3594 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3598 /* Now see if all the arguments are the same. */
3600 const_call_expr_arg_iterator iter0
, iter1
;
3602 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3603 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3605 a0
= next_const_call_expr_arg (&iter0
),
3606 a1
= next_const_call_expr_arg (&iter1
))
3607 if (! operand_equal_p (a0
, a1
, flags
))
3610 /* If we get here and both argument lists are exhausted
3611 then the CALL_EXPRs are equal. */
3612 return ! (a0
|| a1
);
3618 case tcc_declaration
:
3619 /* Consider __builtin_sqrt equal to sqrt. */
3620 if (TREE_CODE (arg0
) == FUNCTION_DECL
)
3621 return (fndecl_built_in_p (arg0
) && fndecl_built_in_p (arg1
)
3622 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3623 && (DECL_UNCHECKED_FUNCTION_CODE (arg0
)
3624 == DECL_UNCHECKED_FUNCTION_CODE (arg1
)));
3627 && (flags
& OEP_DECL_NAME
)
3628 && (flags
& OEP_LEXICOGRAPHIC
))
3630 /* Consider decls with the same name equal. The caller needs
3631 to make sure they refer to the same entity (such as a function
3632 formal parameter). */
3633 tree a0name
= DECL_NAME (arg0
);
3634 tree a1name
= DECL_NAME (arg1
);
3635 const char *a0ns
= a0name
? IDENTIFIER_POINTER (a0name
) : NULL
;
3636 const char *a1ns
= a1name
? IDENTIFIER_POINTER (a1name
) : NULL
;
3637 return a0ns
&& a1ns
&& strcmp (a0ns
, a1ns
) == 0;
3641 case tcc_exceptional
:
3642 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3644 if (CONSTRUCTOR_NO_CLEARING (arg0
) != CONSTRUCTOR_NO_CLEARING (arg1
))
3647 /* In GIMPLE constructors are used only to build vectors from
3648 elements. Individual elements in the constructor must be
3649 indexed in increasing order and form an initial sequence.
3651 We make no effort to compare constructors in generic.
3652 (see sem_variable::equals in ipa-icf which can do so for
3654 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3655 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3658 /* Be sure that vectors constructed have the same representation.
3659 We only tested element precision and modes to match.
3660 Vectors may be BLKmode and thus also check that the number of
3662 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)),
3663 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
))))
3666 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3667 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3668 unsigned int len
= vec_safe_length (v0
);
3670 if (len
!= vec_safe_length (v1
))
3673 for (unsigned int i
= 0; i
< len
; i
++)
3675 constructor_elt
*c0
= &(*v0
)[i
];
3676 constructor_elt
*c1
= &(*v1
)[i
];
3678 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3679 /* In GIMPLE the indexes can be either NULL or matching i.
3680 Double check this so we won't get false
3681 positives for GENERIC. */
3683 && (TREE_CODE (c0
->index
) != INTEGER_CST
3684 || compare_tree_int (c0
->index
, i
)))
3686 && (TREE_CODE (c1
->index
) != INTEGER_CST
3687 || compare_tree_int (c1
->index
, i
))))
3692 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3693 && (flags
& OEP_LEXICOGRAPHIC
))
3695 /* Compare the STATEMENT_LISTs. */
3696 tree_stmt_iterator tsi1
, tsi2
;
3697 tree body1
= CONST_CAST_TREE (arg0
);
3698 tree body2
= CONST_CAST_TREE (arg1
);
3699 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3700 tsi_next (&tsi1
), tsi_next (&tsi2
))
3702 /* The lists don't have the same number of statements. */
3703 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3705 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3707 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3708 flags
& (OEP_LEXICOGRAPHIC
3709 | OEP_NO_HASH_CHECK
)))
3716 switch (TREE_CODE (arg0
))
3719 if (flags
& OEP_LEXICOGRAPHIC
)
3720 return OP_SAME_WITH_NULL (0);
3722 case DEBUG_BEGIN_STMT
:
3723 if (flags
& OEP_LEXICOGRAPHIC
)
3735 #undef OP_SAME_WITH_NULL
3738 /* Generate a hash value for an expression. This can be used iteratively
3739 by passing a previous result as the HSTATE argument. */
3742 operand_compare::hash_operand (const_tree t
, inchash::hash
&hstate
,
3746 enum tree_code code
;
3747 enum tree_code_class tclass
;
3749 if (t
== NULL_TREE
|| t
== error_mark_node
)
3751 hstate
.merge_hash (0);
3755 STRIP_ANY_LOCATION_WRAPPER (t
);
3757 if (!(flags
& OEP_ADDRESS_OF
))
3760 code
= TREE_CODE (t
);
3764 /* Alas, constants aren't shared, so we can't rely on pointer
3767 hstate
.merge_hash (0);
3770 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3771 for (i
= 0; i
< TREE_INT_CST_EXT_NUNITS (t
); i
++)
3772 hstate
.add_hwi (TREE_INT_CST_ELT (t
, i
));
3777 if (!HONOR_SIGNED_ZEROS (t
) && real_zerop (t
))
3780 val2
= real_hash (TREE_REAL_CST_PTR (t
));
3781 hstate
.merge_hash (val2
);
3786 unsigned int val2
= fixed_hash (TREE_FIXED_CST_PTR (t
));
3787 hstate
.merge_hash (val2
);
3791 hstate
.add ((const void *) TREE_STRING_POINTER (t
),
3792 TREE_STRING_LENGTH (t
));
3795 hash_operand (TREE_REALPART (t
), hstate
, flags
);
3796 hash_operand (TREE_IMAGPART (t
), hstate
, flags
);
3800 hstate
.add_int (VECTOR_CST_NPATTERNS (t
));
3801 hstate
.add_int (VECTOR_CST_NELTS_PER_PATTERN (t
));
3802 unsigned int count
= vector_cst_encoded_nelts (t
);
3803 for (unsigned int i
= 0; i
< count
; ++i
)
3804 hash_operand (VECTOR_CST_ENCODED_ELT (t
, i
), hstate
, flags
);
3808 /* We can just compare by pointer. */
3809 hstate
.add_hwi (SSA_NAME_VERSION (t
));
3811 case PLACEHOLDER_EXPR
:
3812 /* The node itself doesn't matter. */
3819 /* A list of expressions, for a CALL_EXPR or as the elements of a
3821 for (; t
; t
= TREE_CHAIN (t
))
3822 hash_operand (TREE_VALUE (t
), hstate
, flags
);
3826 unsigned HOST_WIDE_INT idx
;
3828 flags
&= ~OEP_ADDRESS_OF
;
3829 hstate
.add_int (CONSTRUCTOR_NO_CLEARING (t
));
3830 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), idx
, field
, value
)
3832 /* In GIMPLE the indexes can be either NULL or matching i. */
3833 if (field
== NULL_TREE
)
3834 field
= bitsize_int (idx
);
3835 hash_operand (field
, hstate
, flags
);
3836 hash_operand (value
, hstate
, flags
);
3840 case STATEMENT_LIST
:
3842 tree_stmt_iterator i
;
3843 for (i
= tsi_start (CONST_CAST_TREE (t
));
3844 !tsi_end_p (i
); tsi_next (&i
))
3845 hash_operand (tsi_stmt (i
), hstate
, flags
);
3849 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
3850 hash_operand (TREE_VEC_ELT (t
, i
), hstate
, flags
);
3852 case IDENTIFIER_NODE
:
3853 hstate
.add_object (IDENTIFIER_HASH_VALUE (t
));
3856 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3857 Otherwise nodes that compare equal according to operand_equal_p might
3858 get different hash codes. However, don't do this for machine specific
3859 or front end builtins, since the function code is overloaded in those
3861 if (DECL_BUILT_IN_CLASS (t
) == BUILT_IN_NORMAL
3862 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t
)))
3864 t
= builtin_decl_explicit (DECL_FUNCTION_CODE (t
));
3865 code
= TREE_CODE (t
);
3869 if (POLY_INT_CST_P (t
))
3871 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
3872 hstate
.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t
, i
)));
3875 tclass
= TREE_CODE_CLASS (code
);
3877 if (tclass
== tcc_declaration
)
3879 /* DECL's have a unique ID */
3880 hstate
.add_hwi (DECL_UID (t
));
3882 else if (tclass
== tcc_comparison
&& !commutative_tree_code (code
))
3884 /* For comparisons that can be swapped, use the lower
3886 enum tree_code ccode
= swap_tree_comparison (code
);
3889 hstate
.add_object (ccode
);
3890 hash_operand (TREE_OPERAND (t
, ccode
!= code
), hstate
, flags
);
3891 hash_operand (TREE_OPERAND (t
, ccode
== code
), hstate
, flags
);
3893 else if (CONVERT_EXPR_CODE_P (code
))
3895 /* NOP_EXPR and CONVERT_EXPR are considered equal by
3897 enum tree_code ccode
= NOP_EXPR
;
3898 hstate
.add_object (ccode
);
3900 /* Don't hash the type, that can lead to having nodes which
3901 compare equal according to operand_equal_p, but which
3902 have different hash codes. Make sure to include signedness
3903 in the hash computation. */
3904 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
3905 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3907 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
3908 else if (code
== MEM_REF
3909 && (flags
& OEP_ADDRESS_OF
) != 0
3910 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
3911 && DECL_P (TREE_OPERAND (TREE_OPERAND (t
, 0), 0))
3912 && integer_zerop (TREE_OPERAND (t
, 1)))
3913 hash_operand (TREE_OPERAND (TREE_OPERAND (t
, 0), 0),
3915 /* Don't ICE on FE specific trees, or their arguments etc.
3916 during operand_equal_p hash verification. */
3917 else if (!IS_EXPR_CODE_CLASS (tclass
))
3918 gcc_assert (flags
& OEP_HASH_CHECK
);
3921 unsigned int sflags
= flags
;
3923 hstate
.add_object (code
);
3928 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3929 flags
|= OEP_ADDRESS_OF
;
3935 case TARGET_MEM_REF
:
3936 flags
&= ~OEP_ADDRESS_OF
;
3941 if (sflags
& OEP_ADDRESS_OF
)
3943 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
3944 hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t
, 1)),
3945 hstate
, flags
& ~OEP_ADDRESS_OF
);
3946 hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t
, 1)),
3947 hstate
, flags
& ~OEP_ADDRESS_OF
);
3952 case ARRAY_RANGE_REF
:
3954 sflags
&= ~OEP_ADDRESS_OF
;
3958 flags
&= ~OEP_ADDRESS_OF
;
3961 case WIDEN_MULT_PLUS_EXPR
:
3962 case WIDEN_MULT_MINUS_EXPR
:
3964 /* The multiplication operands are commutative. */
3965 inchash::hash one
, two
;
3966 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
3967 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
3968 hstate
.add_commutative (one
, two
);
3969 hash_operand (TREE_OPERAND (t
, 2), two
, flags
);
3974 if (CALL_EXPR_FN (t
) == NULL_TREE
)
3975 hstate
.add_int (CALL_EXPR_IFN (t
));
3979 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
3980 Usually different TARGET_EXPRs just should use
3981 different temporaries in their slots. */
3982 hash_operand (TARGET_EXPR_SLOT (t
), hstate
, flags
);
3986 /* Virtual table reference. */
3987 inchash::add_expr (OBJ_TYPE_REF_EXPR (t
), hstate
, flags
);
3988 flags
&= ~OEP_ADDRESS_OF
;
3989 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t
), hstate
, flags
);
3990 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t
), hstate
, flags
);
3991 if (!virtual_method_call_p (t
))
3993 if (tree c
= obj_type_ref_class (t
))
3995 c
= TYPE_NAME (TYPE_MAIN_VARIANT (c
));
3996 /* We compute mangled names only when free_lang_data is run.
3997 In that case we can hash precisely. */
3998 if (TREE_CODE (c
) == TYPE_DECL
3999 && DECL_ASSEMBLER_NAME_SET_P (c
))
4001 (IDENTIFIER_HASH_VALUE
4002 (DECL_ASSEMBLER_NAME (c
)));
4009 /* Don't hash the type, that can lead to having nodes which
4010 compare equal according to operand_equal_p, but which
4011 have different hash codes. */
4012 if (code
== NON_LVALUE_EXPR
)
4014 /* Make sure to include signness in the hash computation. */
4015 hstate
.add_int (TYPE_UNSIGNED (TREE_TYPE (t
)));
4016 hash_operand (TREE_OPERAND (t
, 0), hstate
, flags
);
4019 else if (commutative_tree_code (code
))
4021 /* It's a commutative expression. We want to hash it the same
4022 however it appears. We do this by first hashing both operands
4023 and then rehashing based on the order of their independent
4025 inchash::hash one
, two
;
4026 hash_operand (TREE_OPERAND (t
, 0), one
, flags
);
4027 hash_operand (TREE_OPERAND (t
, 1), two
, flags
);
4028 hstate
.add_commutative (one
, two
);
4031 for (i
= TREE_OPERAND_LENGTH (t
) - 1; i
>= 0; --i
)
4032 hash_operand (TREE_OPERAND (t
, i
), hstate
,
4033 i
== 0 ? flags
: sflags
);
4040 operand_compare::verify_hash_value (const_tree arg0
, const_tree arg1
,
4041 unsigned int flags
, bool *ret
)
4043 /* When checking and unless comparing DECL names, verify that if
4044 the outermost operand_equal_p call returns non-zero then ARG0
4045 and ARG1 have the same hash value. */
4046 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
4048 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
4050 if (arg0
!= arg1
&& !(flags
& OEP_DECL_NAME
))
4052 inchash::hash
hstate0 (0), hstate1 (0);
4053 hash_operand (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
4054 hash_operand (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
4055 hashval_t h0
= hstate0
.end ();
4056 hashval_t h1
= hstate1
.end ();
4057 gcc_assert (h0
== h1
);
4071 static operand_compare default_compare_instance
;
4073 /* Conveinece wrapper around operand_compare class because usually we do
4074 not need to play with the valueizer. */
4077 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
4079 return default_compare_instance
.operand_equal_p (arg0
, arg1
, flags
);
4085 /* Generate a hash value for an expression. This can be used iteratively
4086 by passing a previous result as the HSTATE argument.
4088 This function is intended to produce the same hash for expressions which
4089 would compare equal using operand_equal_p. */
4091 add_expr (const_tree t
, inchash::hash
&hstate
, unsigned int flags
)
4093 default_compare_instance
.hash_operand (t
, hstate
, flags
);
4098 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4099 with a different signedness or a narrower precision. */
4102 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
4104 if (operand_equal_p (arg0
, arg1
, 0))
4107 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
4108 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
4111 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4112 and see if the inner values are the same. This removes any
4113 signedness comparison, which doesn't matter here. */
4118 if (operand_equal_p (op0
, op1
, 0))
4121 /* Discard a single widening conversion from ARG1 and see if the inner
4122 value is the same as ARG0. */
4123 if (CONVERT_EXPR_P (arg1
)
4124 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4125 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
4126 < TYPE_PRECISION (TREE_TYPE (arg1
))
4127 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
4133 /* See if ARG is an expression that is either a comparison or is performing
4134 arithmetic on comparisons. The comparisons must only be comparing
4135 two different values, which will be stored in *CVAL1 and *CVAL2; if
4136 they are nonzero it means that some operands have already been found.
4137 No variables may be used anywhere else in the expression except in the
4140 If this is true, return 1. Otherwise, return zero. */
4143 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
)
4145 enum tree_code code
= TREE_CODE (arg
);
4146 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4148 /* We can handle some of the tcc_expression cases here. */
4149 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4151 else if (tclass
== tcc_expression
4152 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
4153 || code
== COMPOUND_EXPR
))
4154 tclass
= tcc_binary
;
4159 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
4162 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4163 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
4168 case tcc_expression
:
4169 if (code
== COND_EXPR
)
4170 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
4171 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
4172 && twoval_comparison_p (TREE_OPERAND (arg
, 2), cval1
, cval2
));
4175 case tcc_comparison
:
4176 /* First see if we can handle the first operand, then the second. For
4177 the second operand, we know *CVAL1 can't be zero. It must be that
4178 one side of the comparison is each of the values; test for the
4179 case where this isn't true by failing if the two operands
4182 if (operand_equal_p (TREE_OPERAND (arg
, 0),
4183 TREE_OPERAND (arg
, 1), 0))
4187 *cval1
= TREE_OPERAND (arg
, 0);
4188 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
4190 else if (*cval2
== 0)
4191 *cval2
= TREE_OPERAND (arg
, 0);
4192 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
4197 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
4199 else if (*cval2
== 0)
4200 *cval2
= TREE_OPERAND (arg
, 1);
4201 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
4213 /* ARG is a tree that is known to contain just arithmetic operations and
4214 comparisons. Evaluate the operations in the tree substituting NEW0 for
4215 any occurrence of OLD0 as an operand of a comparison and likewise for
4219 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
4220 tree old1
, tree new1
)
4222 tree type
= TREE_TYPE (arg
);
4223 enum tree_code code
= TREE_CODE (arg
);
4224 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
4226 /* We can handle some of the tcc_expression cases here. */
4227 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
4229 else if (tclass
== tcc_expression
4230 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
4231 tclass
= tcc_binary
;
4236 return fold_build1_loc (loc
, code
, type
,
4237 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4238 old0
, new0
, old1
, new1
));
4241 return fold_build2_loc (loc
, code
, type
,
4242 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4243 old0
, new0
, old1
, new1
),
4244 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4245 old0
, new0
, old1
, new1
));
4247 case tcc_expression
:
4251 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
4255 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
4259 return fold_build3_loc (loc
, code
, type
,
4260 eval_subst (loc
, TREE_OPERAND (arg
, 0),
4261 old0
, new0
, old1
, new1
),
4262 eval_subst (loc
, TREE_OPERAND (arg
, 1),
4263 old0
, new0
, old1
, new1
),
4264 eval_subst (loc
, TREE_OPERAND (arg
, 2),
4265 old0
, new0
, old1
, new1
));
4269 /* Fall through - ??? */
4271 case tcc_comparison
:
4273 tree arg0
= TREE_OPERAND (arg
, 0);
4274 tree arg1
= TREE_OPERAND (arg
, 1);
4276 /* We need to check both for exact equality and tree equality. The
4277 former will be true if the operand has a side-effect. In that
4278 case, we know the operand occurred exactly once. */
4280 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
4282 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
4285 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
4287 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
4290 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
4298 /* Return a tree for the case when the result of an expression is RESULT
4299 converted to TYPE and OMITTED was previously an operand of the expression
4300 but is now not needed (e.g., we folded OMITTED * 0).
4302 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4303 the conversion of RESULT to TYPE. */
4306 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
4308 tree t
= fold_convert_loc (loc
, type
, result
);
4310 /* If the resulting operand is an empty statement, just return the omitted
4311 statement casted to void. */
4312 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
4313 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
4314 fold_ignored_result (omitted
));
4316 if (TREE_SIDE_EFFECTS (omitted
))
4317 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4318 fold_ignored_result (omitted
), t
);
4320 return non_lvalue_loc (loc
, t
);
4323 /* Return a tree for the case when the result of an expression is RESULT
4324 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4325 of the expression but are now not needed.
4327 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4328 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4329 evaluated before OMITTED2. Otherwise, if neither has side effects,
4330 just do the conversion of RESULT to TYPE. */
4333 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
4334 tree omitted1
, tree omitted2
)
4336 tree t
= fold_convert_loc (loc
, type
, result
);
4338 if (TREE_SIDE_EFFECTS (omitted2
))
4339 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
4340 if (TREE_SIDE_EFFECTS (omitted1
))
4341 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
4343 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
4347 /* Return a simplified tree node for the truth-negation of ARG. This
4348 never alters ARG itself. We assume that ARG is an operation that
4349 returns a truth value (0 or 1).
4351 FIXME: one would think we would fold the result, but it causes
4352 problems with the dominator optimizer. */
4355 fold_truth_not_expr (location_t loc
, tree arg
)
4357 tree type
= TREE_TYPE (arg
);
4358 enum tree_code code
= TREE_CODE (arg
);
4359 location_t loc1
, loc2
;
4361 /* If this is a comparison, we can simply invert it, except for
4362 floating-point non-equality comparisons, in which case we just
4363 enclose a TRUTH_NOT_EXPR around what we have. */
4365 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4367 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
4368 if (FLOAT_TYPE_P (op_type
)
4369 && flag_trapping_math
4370 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
4371 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
4374 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
4375 if (code
== ERROR_MARK
)
4378 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
4379 TREE_OPERAND (arg
, 1));
4380 copy_warning (ret
, arg
);
4387 return constant_boolean_node (integer_zerop (arg
), type
);
4389 case TRUTH_AND_EXPR
:
4390 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4391 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4392 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
4393 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4394 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4397 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4398 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4399 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
4400 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4401 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4403 case TRUTH_XOR_EXPR
:
4404 /* Here we can invert either operand. We invert the first operand
4405 unless the second operand is a TRUTH_NOT_EXPR in which case our
4406 result is the XOR of the first operand with the inside of the
4407 negation of the second operand. */
4409 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
4410 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
4411 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
4413 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
4414 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
4415 TREE_OPERAND (arg
, 1));
4417 case TRUTH_ANDIF_EXPR
:
4418 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4419 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4420 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
4421 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4422 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4424 case TRUTH_ORIF_EXPR
:
4425 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4426 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4427 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
4428 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
4429 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
4431 case TRUTH_NOT_EXPR
:
4432 return TREE_OPERAND (arg
, 0);
4436 tree arg1
= TREE_OPERAND (arg
, 1);
4437 tree arg2
= TREE_OPERAND (arg
, 2);
4439 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4440 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
4442 /* A COND_EXPR may have a throw as one operand, which
4443 then has void type. Just leave void operands
4445 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
4446 VOID_TYPE_P (TREE_TYPE (arg1
))
4447 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
4448 VOID_TYPE_P (TREE_TYPE (arg2
))
4449 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
4453 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
4454 return build2_loc (loc
, COMPOUND_EXPR
, type
,
4455 TREE_OPERAND (arg
, 0),
4456 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
4458 case NON_LVALUE_EXPR
:
4459 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4460 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
4463 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4464 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4469 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4470 return build1_loc (loc
, TREE_CODE (arg
), type
,
4471 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4474 if (!integer_onep (TREE_OPERAND (arg
, 1)))
4476 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
4479 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
4481 case CLEANUP_POINT_EXPR
:
4482 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
4483 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
4484 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
4491 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4492 assume that ARG is an operation that returns a truth value (0 or 1
4493 for scalars, 0 or -1 for vectors). Return the folded expression if
4494 folding is successful. Otherwise, return NULL_TREE. */
4497 fold_invert_truthvalue (location_t loc
, tree arg
)
4499 tree type
= TREE_TYPE (arg
);
4500 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
4506 /* Return a simplified tree node for the truth-negation of ARG. This
4507 never alters ARG itself. We assume that ARG is an operation that
4508 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4511 invert_truthvalue_loc (location_t loc
, tree arg
)
4513 if (TREE_CODE (arg
) == ERROR_MARK
)
4516 tree type
= TREE_TYPE (arg
);
4517 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
4523 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4524 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4525 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4526 is the original memory reference used to preserve the alias set of
4530 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
4531 HOST_WIDE_INT bitsize
, poly_int64 bitpos
,
4532 int unsignedp
, int reversep
)
4534 tree result
, bftype
;
4536 /* Attempt not to lose the access path if possible. */
4537 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
4539 tree ninner
= TREE_OPERAND (orig_inner
, 0);
4541 poly_int64 nbitsize
, nbitpos
;
4543 int nunsignedp
, nreversep
, nvolatilep
= 0;
4544 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
4545 &noffset
, &nmode
, &nunsignedp
,
4546 &nreversep
, &nvolatilep
);
4548 && noffset
== NULL_TREE
4549 && known_subrange_p (bitpos
, bitsize
, nbitpos
, nbitsize
)
4559 alias_set_type iset
= get_alias_set (orig_inner
);
4560 if (iset
== 0 && get_alias_set (inner
) != iset
)
4561 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
4562 build_fold_addr_expr (inner
),
4563 build_int_cst (ptr_type_node
, 0));
4565 if (known_eq (bitpos
, 0) && !reversep
)
4567 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
4568 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
4569 || POINTER_TYPE_P (TREE_TYPE (inner
)))
4570 && tree_fits_shwi_p (size
)
4571 && tree_to_shwi (size
) == bitsize
)
4572 return fold_convert_loc (loc
, type
, inner
);
4576 if (TYPE_PRECISION (bftype
) != bitsize
4577 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
4578 bftype
= build_nonstandard_integer_type (bitsize
, 0);
4580 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
4581 bitsize_int (bitsize
), bitsize_int (bitpos
));
4582 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
4585 result
= fold_convert_loc (loc
, type
, result
);
4590 /* Optimize a bit-field compare.
4592 There are two cases: First is a compare against a constant and the
4593 second is a comparison of two items where the fields are at the same
4594 bit position relative to the start of a chunk (byte, halfword, word)
4595 large enough to contain it. In these cases we can avoid the shift
4596 implicit in bitfield extractions.
4598 For constants, we emit a compare of the shifted constant with the
4599 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4600 compared. For two fields at the same position, we do the ANDs with the
4601 similar mask and compare the result of the ANDs.
4603 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4604 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4605 are the left and right operands of the comparison, respectively.
4607 If the optimization described above can be done, we return the resulting
4608 tree. Otherwise we return zero. */
4611 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
4612 tree compare_type
, tree lhs
, tree rhs
)
4614 poly_int64 plbitpos
, plbitsize
, rbitpos
, rbitsize
;
4615 HOST_WIDE_INT lbitpos
, lbitsize
, nbitpos
, nbitsize
;
4616 tree type
= TREE_TYPE (lhs
);
4618 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
4619 machine_mode lmode
, rmode
;
4620 scalar_int_mode nmode
;
4621 int lunsignedp
, runsignedp
;
4622 int lreversep
, rreversep
;
4623 int lvolatilep
= 0, rvolatilep
= 0;
4624 tree linner
, rinner
= NULL_TREE
;
4628 /* Get all the information about the extractions being done. If the bit size
4629 is the same as the size of the underlying object, we aren't doing an
4630 extraction at all and so can do nothing. We also don't want to
4631 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4632 then will no longer be able to replace it. */
4633 linner
= get_inner_reference (lhs
, &plbitsize
, &plbitpos
, &offset
, &lmode
,
4634 &lunsignedp
, &lreversep
, &lvolatilep
);
4636 || !known_size_p (plbitsize
)
4637 || !plbitsize
.is_constant (&lbitsize
)
4638 || !plbitpos
.is_constant (&lbitpos
)
4639 || known_eq (lbitsize
, GET_MODE_BITSIZE (lmode
))
4641 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
4646 rreversep
= lreversep
;
4649 /* If this is not a constant, we can only do something if bit positions,
4650 sizes, signedness and storage order are the same. */
4652 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
4653 &runsignedp
, &rreversep
, &rvolatilep
);
4656 || maybe_ne (lbitpos
, rbitpos
)
4657 || maybe_ne (lbitsize
, rbitsize
)
4658 || lunsignedp
!= runsignedp
4659 || lreversep
!= rreversep
4661 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
4666 /* Honor the C++ memory model and mimic what RTL expansion does. */
4667 poly_uint64 bitstart
= 0;
4668 poly_uint64 bitend
= 0;
4669 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4671 get_bit_range (&bitstart
, &bitend
, lhs
, &plbitpos
, &offset
);
4672 if (!plbitpos
.is_constant (&lbitpos
) || offset
!= NULL_TREE
)
4676 /* See if we can find a mode to refer to this field. We should be able to,
4677 but fail if we can't. */
4678 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4679 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4680 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4681 TYPE_ALIGN (TREE_TYPE (rinner
))),
4682 BITS_PER_WORD
, false, &nmode
))
4685 /* Set signed and unsigned types of the precision of this mode for the
4687 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4689 /* Compute the bit position and size for the new reference and our offset
4690 within it. If the new reference is the same size as the original, we
4691 won't optimize anything, so return zero. */
4692 nbitsize
= GET_MODE_BITSIZE (nmode
);
4693 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4695 if (nbitsize
== lbitsize
)
4698 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4699 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4701 /* Make the mask to be used against the extracted field. */
4702 mask
= build_int_cst_type (unsigned_type
, -1);
4703 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4704 mask
= const_binop (RSHIFT_EXPR
, mask
,
4705 size_int (nbitsize
- lbitsize
- lbitpos
));
4712 /* If not comparing with constant, just rework the comparison
4714 tree t1
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4715 nbitsize
, nbitpos
, 1, lreversep
);
4716 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t1
, mask
);
4717 tree t2
= make_bit_field_ref (loc
, rinner
, rhs
, unsigned_type
,
4718 nbitsize
, nbitpos
, 1, rreversep
);
4719 t2
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
, t2
, mask
);
4720 return fold_build2_loc (loc
, code
, compare_type
, t1
, t2
);
4723 /* Otherwise, we are handling the constant case. See if the constant is too
4724 big for the field. Warn and return a tree for 0 (false) if so. We do
4725 this not only for its own sake, but to avoid having to test for this
4726 error case below. If we didn't, we might generate wrong code.
4728 For unsigned fields, the constant shifted right by the field length should
4729 be all zero. For signed fields, the high-order bits should agree with
4734 if (wi::lrshift (wi::to_wide (rhs
), lbitsize
) != 0)
4736 warning (0, "comparison is always %d due to width of bit-field",
4738 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4743 wide_int tem
= wi::arshift (wi::to_wide (rhs
), lbitsize
- 1);
4744 if (tem
!= 0 && tem
!= -1)
4746 warning (0, "comparison is always %d due to width of bit-field",
4748 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4755 /* Single-bit compares should always be against zero. */
4756 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4758 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4759 rhs
= build_int_cst (type
, 0);
4762 /* Make a new bitfield reference, shift the constant over the
4763 appropriate number of bits and mask it with the computed mask
4764 (in case this was a signed field). If we changed it, make a new one. */
4765 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4766 nbitsize
, nbitpos
, 1, lreversep
);
4768 rhs
= const_binop (BIT_AND_EXPR
,
4769 const_binop (LSHIFT_EXPR
,
4770 fold_convert_loc (loc
, unsigned_type
, rhs
),
4771 size_int (lbitpos
)),
4774 lhs
= build2_loc (loc
, code
, compare_type
,
4775 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4779 /* Subroutine for fold_truth_andor_1: decode a field reference.
4781 If EXP is a comparison reference, we return the innermost reference.
4783 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4784 set to the starting bit number.
4786 If the innermost field can be completely contained in a mode-sized
4787 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4789 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4790 otherwise it is not changed.
4792 *PUNSIGNEDP is set to the signedness of the field.
4794 *PREVERSEP is set to the storage order of the field.
4796 *PMASK is set to the mask used. This is either contained in a
4797 BIT_AND_EXPR or derived from the width of the field.
4799 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4801 Return 0 if this is not a component reference or is one that we can't
4802 do anything with. */
4805 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4806 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4807 int *punsignedp
, int *preversep
, int *pvolatilep
,
4808 tree
*pmask
, tree
*pand_mask
)
4811 tree outer_type
= 0;
4813 tree mask
, inner
, offset
;
4815 unsigned int precision
;
4817 /* All the optimizations using this function assume integer fields.
4818 There are problems with FP fields since the type_for_size call
4819 below can fail for, e.g., XFmode. */
4820 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4823 /* We are interested in the bare arrangement of bits, so strip everything
4824 that doesn't affect the machine mode. However, record the type of the
4825 outermost expression if it may matter below. */
4826 if (CONVERT_EXPR_P (exp
)
4827 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4828 outer_type
= TREE_TYPE (exp
);
4831 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4833 and_mask
= TREE_OPERAND (exp
, 1);
4834 exp
= TREE_OPERAND (exp
, 0);
4835 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4836 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4840 poly_int64 poly_bitsize
, poly_bitpos
;
4841 inner
= get_inner_reference (exp
, &poly_bitsize
, &poly_bitpos
, &offset
,
4842 pmode
, punsignedp
, preversep
, pvolatilep
);
4843 if ((inner
== exp
&& and_mask
== 0)
4844 || !poly_bitsize
.is_constant (pbitsize
)
4845 || !poly_bitpos
.is_constant (pbitpos
)
4848 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4849 /* Reject out-of-bound accesses (PR79731). */
4850 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4851 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4852 *pbitpos
+ *pbitsize
) < 0))
4855 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4856 if (unsigned_type
== NULL_TREE
)
4861 /* If the number of bits in the reference is the same as the bitsize of
4862 the outer type, then the outer type gives the signedness. Otherwise
4863 (in case of a small bitfield) the signedness is unchanged. */
4864 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4865 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4867 /* Compute the mask to access the bitfield. */
4868 precision
= TYPE_PRECISION (unsigned_type
);
4870 mask
= build_int_cst_type (unsigned_type
, -1);
4872 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4873 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4875 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4877 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4878 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4881 *pand_mask
= and_mask
;
4885 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4886 bit positions and MASK is SIGNED. */
4889 all_ones_mask_p (const_tree mask
, unsigned int size
)
4891 tree type
= TREE_TYPE (mask
);
4892 unsigned int precision
= TYPE_PRECISION (type
);
4894 /* If this function returns true when the type of the mask is
4895 UNSIGNED, then there will be errors. In particular see
4896 gcc.c-torture/execute/990326-1.c. There does not appear to be
4897 any documentation paper trail as to why this is so. But the pre
4898 wide-int worked with that restriction and it has been preserved
4900 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4903 return wi::mask (size
, false, precision
) == wi::to_wide (mask
);
4906 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4907 represents the sign bit of EXP's type. If EXP represents a sign
4908 or zero extension, also test VAL against the unextended type.
4909 The return value is the (sub)expression whose sign bit is VAL,
4910 or NULL_TREE otherwise. */
4913 sign_bit_p (tree exp
, const_tree val
)
4918 /* Tree EXP must have an integral type. */
4919 t
= TREE_TYPE (exp
);
4920 if (! INTEGRAL_TYPE_P (t
))
4923 /* Tree VAL must be an integer constant. */
4924 if (TREE_CODE (val
) != INTEGER_CST
4925 || TREE_OVERFLOW (val
))
4928 width
= TYPE_PRECISION (t
);
4929 if (wi::only_sign_bit_p (wi::to_wide (val
), width
))
4932 /* Handle extension from a narrower type. */
4933 if (TREE_CODE (exp
) == NOP_EXPR
4934 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4935 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4940 /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4941 operand is simple enough to be evaluated unconditionally. */
4944 simple_operand_p (const_tree exp
)
4946 /* Strip any conversions that don't change the machine mode. */
4949 return (CONSTANT_CLASS_P (exp
)
4950 || TREE_CODE (exp
) == SSA_NAME
4952 && ! TREE_ADDRESSABLE (exp
)
4953 && ! TREE_THIS_VOLATILE (exp
)
4954 && ! DECL_NONLOCAL (exp
)
4955 /* Don't regard global variables as simple. They may be
4956 allocated in ways unknown to the compiler (shared memory,
4957 #pragma weak, etc). */
4958 && ! TREE_PUBLIC (exp
)
4959 && ! DECL_EXTERNAL (exp
)
4960 /* Weakrefs are not safe to be read, since they can be NULL.
4961 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4962 have DECL_WEAK flag set. */
4963 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4964 /* Loading a static variable is unduly expensive, but global
4965 registers aren't expensive. */
4966 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4969 /* Determine if an operand is simple enough to be evaluated unconditionally.
4970 In addition to simple_operand_p, we assume that comparisons, conversions,
4971 and logic-not operations are simple, if their operands are simple, too. */
4974 simple_condition_p (tree exp
)
4976 enum tree_code code
;
4978 if (TREE_SIDE_EFFECTS (exp
) || generic_expr_could_trap_p (exp
))
4981 while (CONVERT_EXPR_P (exp
))
4982 exp
= TREE_OPERAND (exp
, 0);
4984 code
= TREE_CODE (exp
);
4986 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4987 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4988 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4990 if (code
== TRUTH_NOT_EXPR
)
4991 return simple_condition_p (TREE_OPERAND (exp
, 0));
4993 return simple_operand_p (exp
);
4997 /* The following functions are subroutines to fold_range_test and allow it to
4998 try to change a logical combination of comparisons into a range test.
5001 X == 2 || X == 3 || X == 4 || X == 5
5005 (unsigned) (X - 2) <= 3
5007 We describe each set of comparisons as being either inside or outside
5008 a range, using a variable named like IN_P, and then describe the
5009 range with a lower and upper bound. If one of the bounds is omitted,
5010 it represents either the highest or lowest value of the type.
5012 In the comments below, we represent a range by two numbers in brackets
5013 preceded by a "+" to designate being inside that range, or a "-" to
5014 designate being outside that range, so the condition can be inverted by
5015 flipping the prefix. An omitted bound is represented by a "-". For
5016 example, "- [-, 10]" means being outside the range starting at the lowest
5017 possible value and ending at 10, in other words, being greater than 10.
5018 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5021 We set up things so that the missing bounds are handled in a consistent
5022 manner so neither a missing bound nor "true" and "false" need to be
5023 handled using a special case. */
5025 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5026 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5027 and UPPER1_P are nonzero if the respective argument is an upper bound
5028 and zero for a lower. TYPE, if nonzero, is the type of the result; it
5029 must be specified for a comparison. ARG1 will be converted to ARG0's
5030 type if both are specified. */
5033 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
5034 tree arg1
, int upper1_p
)
5040 /* If neither arg represents infinity, do the normal operation.
5041 Else, if not a comparison, return infinity. Else handle the special
5042 comparison rules. Note that most of the cases below won't occur, but
5043 are handled for consistency. */
5045 if (arg0
!= 0 && arg1
!= 0)
5047 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
5048 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
5050 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
5053 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5056 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5057 for neither. In real maths, we cannot assume open ended ranges are
5058 the same. But, this is computer arithmetic, where numbers are finite.
5059 We can therefore make the transformation of any unbounded range with
5060 the value Z, Z being greater than any representable number. This permits
5061 us to treat unbounded ranges as equal. */
5062 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
5063 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
5067 result
= sgn0
== sgn1
;
5070 result
= sgn0
!= sgn1
;
5073 result
= sgn0
< sgn1
;
5076 result
= sgn0
<= sgn1
;
5079 result
= sgn0
> sgn1
;
5082 result
= sgn0
>= sgn1
;
5088 return constant_boolean_node (result
, type
);
5091 /* Helper routine for make_range. Perform one step for it, return
5092 new expression if the loop should continue or NULL_TREE if it should
5096 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
5097 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
5098 bool *strict_overflow_p
)
5100 tree arg0_type
= TREE_TYPE (arg0
);
5101 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
5102 int in_p
= *p_in_p
, n_in_p
;
5106 case TRUTH_NOT_EXPR
:
5107 /* We can only do something if the range is testing for zero. */
5108 if (low
== NULL_TREE
|| high
== NULL_TREE
5109 || ! integer_zerop (low
) || ! integer_zerop (high
))
5114 case EQ_EXPR
: case NE_EXPR
:
5115 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
5116 /* We can only do something if the range is testing for zero
5117 and if the second operand is an integer constant. Note that
5118 saying something is "in" the range we make is done by
5119 complementing IN_P since it will set in the initial case of
5120 being not equal to zero; "out" is leaving it alone. */
5121 if (low
== NULL_TREE
|| high
== NULL_TREE
5122 || ! integer_zerop (low
) || ! integer_zerop (high
)
5123 || TREE_CODE (arg1
) != INTEGER_CST
)
5128 case NE_EXPR
: /* - [c, c] */
5131 case EQ_EXPR
: /* + [c, c] */
5132 in_p
= ! in_p
, low
= high
= arg1
;
5134 case GT_EXPR
: /* - [-, c] */
5135 low
= 0, high
= arg1
;
5137 case GE_EXPR
: /* + [c, -] */
5138 in_p
= ! in_p
, low
= arg1
, high
= 0;
5140 case LT_EXPR
: /* - [c, -] */
5141 low
= arg1
, high
= 0;
5143 case LE_EXPR
: /* + [-, c] */
5144 in_p
= ! in_p
, low
= 0, high
= arg1
;
5150 /* If this is an unsigned comparison, we also know that EXP is
5151 greater than or equal to zero. We base the range tests we make
5152 on that fact, so we record it here so we can parse existing
5153 range tests. We test arg0_type since often the return type
5154 of, e.g. EQ_EXPR, is boolean. */
5155 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
5157 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
5159 build_int_cst (arg0_type
, 0),
5163 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
5165 /* If the high bound is missing, but we have a nonzero low
5166 bound, reverse the range so it goes from zero to the low bound
5168 if (high
== 0 && low
&& ! integer_zerop (low
))
5171 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
5172 build_int_cst (TREE_TYPE (low
), 1), 0);
5173 low
= build_int_cst (arg0_type
, 0);
5183 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5184 low and high are non-NULL, then normalize will DTRT. */
5185 if (!TYPE_UNSIGNED (arg0_type
)
5186 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5188 if (low
== NULL_TREE
)
5189 low
= TYPE_MIN_VALUE (arg0_type
);
5190 if (high
== NULL_TREE
)
5191 high
= TYPE_MAX_VALUE (arg0_type
);
5194 /* (-x) IN [a,b] -> x in [-b, -a] */
5195 n_low
= range_binop (MINUS_EXPR
, exp_type
,
5196 build_int_cst (exp_type
, 0),
5198 n_high
= range_binop (MINUS_EXPR
, exp_type
,
5199 build_int_cst (exp_type
, 0),
5201 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
5207 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
5208 build_int_cst (exp_type
, 1));
5212 if (TREE_CODE (arg1
) != INTEGER_CST
)
5215 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5216 move a constant to the other side. */
5217 if (!TYPE_UNSIGNED (arg0_type
)
5218 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5221 /* If EXP is signed, any overflow in the computation is undefined,
5222 so we don't worry about it so long as our computations on
5223 the bounds don't overflow. For unsigned, overflow is defined
5224 and this is exactly the right thing. */
5225 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5226 arg0_type
, low
, 0, arg1
, 0);
5227 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
5228 arg0_type
, high
, 1, arg1
, 0);
5229 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
5230 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
5233 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
5234 *strict_overflow_p
= true;
5237 /* Check for an unsigned range which has wrapped around the maximum
5238 value thus making n_high < n_low, and normalize it. */
5239 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
5241 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
5242 build_int_cst (TREE_TYPE (n_high
), 1), 0);
5243 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
5244 build_int_cst (TREE_TYPE (n_low
), 1), 0);
5246 /* If the range is of the form +/- [ x+1, x ], we won't
5247 be able to normalize it. But then, it represents the
5248 whole range or the empty set, so make it
5250 if (tree_int_cst_equal (n_low
, low
)
5251 && tree_int_cst_equal (n_high
, high
))
5257 low
= n_low
, high
= n_high
;
5265 case NON_LVALUE_EXPR
:
5266 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
5269 if (! INTEGRAL_TYPE_P (arg0_type
)
5270 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
5271 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
5274 n_low
= low
, n_high
= high
;
5277 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
5280 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
5282 /* If we're converting arg0 from an unsigned type, to exp,
5283 a signed type, we will be doing the comparison as unsigned.
5284 The tests above have already verified that LOW and HIGH
5287 So we have to ensure that we will handle large unsigned
5288 values the same way that the current signed bounds treat
5291 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
5295 /* For fixed-point modes, we need to pass the saturating flag
5296 as the 2nd parameter. */
5297 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
5299 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
5300 TYPE_SATURATING (arg0_type
));
5303 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
5305 /* A range without an upper bound is, naturally, unbounded.
5306 Since convert would have cropped a very large value, use
5307 the max value for the destination type. */
5309 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
5310 : TYPE_MAX_VALUE (arg0_type
);
5312 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
5313 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
5314 fold_convert_loc (loc
, arg0_type
,
5316 build_int_cst (arg0_type
, 1));
5318 /* If the low bound is specified, "and" the range with the
5319 range for which the original unsigned value will be
5323 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
5324 1, fold_convert_loc (loc
, arg0_type
,
5329 in_p
= (n_in_p
== in_p
);
5333 /* Otherwise, "or" the range with the range of the input
5334 that will be interpreted as negative. */
5335 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
5336 1, fold_convert_loc (loc
, arg0_type
,
5341 in_p
= (in_p
!= n_in_p
);
5345 /* Otherwise, if we are converting arg0 from signed type, to exp,
5346 an unsigned type, we will do the comparison as signed. If
5347 high is non-NULL, we punt above if it doesn't fit in the signed
5348 type, so if we get through here, +[-, high] or +[low, high] are
5349 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5350 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5351 high is not, the +[low, -] range is equivalent to union of
5352 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5353 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5354 low being 0, which should be treated as [-, -]. */
5355 else if (TYPE_UNSIGNED (exp_type
)
5356 && !TYPE_UNSIGNED (arg0_type
)
5360 if (integer_zerop (low
))
5364 n_high
= fold_build2_loc (loc
, PLUS_EXPR
, arg0_type
,
5365 n_low
, build_int_cst (arg0_type
, -1));
5366 n_low
= build_zero_cst (arg0_type
);
5381 /* Given EXP, a logical expression, set the range it is testing into
5382 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5383 actually being tested. *PLOW and *PHIGH will be made of the same
5384 type as the returned expression. If EXP is not a comparison, we
5385 will most likely not be returning a useful value and range. Set
5386 *STRICT_OVERFLOW_P to true if the return value is only valid
5387 because signed overflow is undefined; otherwise, do not change
5388 *STRICT_OVERFLOW_P. */
5391 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
5392 bool *strict_overflow_p
)
5394 enum tree_code code
;
5395 tree arg0
, arg1
= NULL_TREE
;
5396 tree exp_type
, nexp
;
5399 location_t loc
= EXPR_LOCATION (exp
);
5401 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5402 and see if we can refine the range. Some of the cases below may not
5403 happen, but it doesn't seem worth worrying about this. We "continue"
5404 the outer loop when we've changed something; otherwise we "break"
5405 the switch, which will "break" the while. */
5408 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
5412 code
= TREE_CODE (exp
);
5413 exp_type
= TREE_TYPE (exp
);
5416 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
5418 if (TREE_OPERAND_LENGTH (exp
) > 0)
5419 arg0
= TREE_OPERAND (exp
, 0);
5420 if (TREE_CODE_CLASS (code
) == tcc_binary
5421 || TREE_CODE_CLASS (code
) == tcc_comparison
5422 || (TREE_CODE_CLASS (code
) == tcc_expression
5423 && TREE_OPERAND_LENGTH (exp
) > 1))
5424 arg1
= TREE_OPERAND (exp
, 1);
5426 if (arg0
== NULL_TREE
)
5429 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
5430 &high
, &in_p
, strict_overflow_p
);
5431 if (nexp
== NULL_TREE
)
5436 /* If EXP is a constant, we can evaluate whether this is true or false. */
5437 if (TREE_CODE (exp
) == INTEGER_CST
)
5439 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
5441 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5447 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5451 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5452 a bitwise check i.e. when
5453 LOW == 0xXX...X00...0
5454 HIGH == 0xXX...X11...1
5455 Return corresponding mask in MASK and stem in VALUE. */
5458 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
5461 if (TREE_CODE (low
) != INTEGER_CST
5462 || TREE_CODE (high
) != INTEGER_CST
)
5465 unsigned prec
= TYPE_PRECISION (type
);
5466 wide_int lo
= wi::to_wide (low
, prec
);
5467 wide_int hi
= wi::to_wide (high
, prec
);
5469 wide_int end_mask
= lo
^ hi
;
5470 if ((end_mask
& (end_mask
+ 1)) != 0
5471 || (lo
& end_mask
) != 0)
5474 wide_int stem_mask
= ~end_mask
;
5475 wide_int stem
= lo
& stem_mask
;
5476 if (stem
!= (hi
& stem_mask
))
5479 *mask
= wide_int_to_tree (type
, stem_mask
);
5480 *value
= wide_int_to_tree (type
, stem
);
5485 /* Helper routine for build_range_check and match.pd. Return the type to
5486 perform the check or NULL if it shouldn't be optimized. */
5489 range_check_type (tree etype
)
5491 /* First make sure that arithmetics in this type is valid, then make sure
5492 that it wraps around. */
5493 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
5494 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
), 1);
5496 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_UNSIGNED (etype
))
5498 tree utype
, minv
, maxv
;
5500 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5501 for the type in question, as we rely on this here. */
5502 utype
= unsigned_type_for (etype
);
5503 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
5504 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
5505 build_int_cst (TREE_TYPE (maxv
), 1), 1);
5506 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
5508 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
5514 else if (POINTER_TYPE_P (etype
) || TREE_CODE (etype
) == OFFSET_TYPE
)
5515 etype
= unsigned_type_for (etype
);
5519 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5520 type, TYPE, return an expression to test if EXP is in (or out of, depending
5521 on IN_P) the range. Return 0 if the test couldn't be created. */
5524 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
5525 tree low
, tree high
)
5527 tree etype
= TREE_TYPE (exp
), mask
, value
;
5529 /* Disable this optimization for function pointer expressions
5530 on targets that require function pointer canonicalization. */
5531 if (targetm
.have_canonicalize_funcptr_for_compare ()
5532 && POINTER_TYPE_P (etype
)
5533 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype
)))
5538 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
5540 return invert_truthvalue_loc (loc
, value
);
5545 if (low
== 0 && high
== 0)
5546 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
5549 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
5550 fold_convert_loc (loc
, etype
, high
));
5553 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
5554 fold_convert_loc (loc
, etype
, low
));
5556 if (operand_equal_p (low
, high
, 0))
5557 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
5558 fold_convert_loc (loc
, etype
, low
));
5560 if (TREE_CODE (exp
) == BIT_AND_EXPR
5561 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
5562 return fold_build2_loc (loc
, EQ_EXPR
, type
,
5563 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
5567 if (integer_zerop (low
))
5569 if (! TYPE_UNSIGNED (etype
))
5571 etype
= unsigned_type_for (etype
);
5572 high
= fold_convert_loc (loc
, etype
, high
);
5573 exp
= fold_convert_loc (loc
, etype
, exp
);
5575 return build_range_check (loc
, type
, exp
, 1, 0, high
);
5578 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5579 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
5581 int prec
= TYPE_PRECISION (etype
);
5583 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
5585 if (TYPE_UNSIGNED (etype
))
5587 tree signed_etype
= signed_type_for (etype
);
5588 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
5590 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
5592 etype
= signed_etype
;
5593 exp
= fold_convert_loc (loc
, etype
, exp
);
5595 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
5596 build_int_cst (etype
, 0));
5600 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5601 This requires wrap-around arithmetics for the type of the expression. */
5602 etype
= range_check_type (etype
);
5603 if (etype
== NULL_TREE
)
5606 high
= fold_convert_loc (loc
, etype
, high
);
5607 low
= fold_convert_loc (loc
, etype
, low
);
5608 exp
= fold_convert_loc (loc
, etype
, exp
);
5610 value
= const_binop (MINUS_EXPR
, high
, low
);
5612 if (value
!= 0 && !TREE_OVERFLOW (value
))
5613 return build_range_check (loc
, type
,
5614 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
5615 1, build_int_cst (etype
, 0), value
);
5620 /* Return the predecessor of VAL in its type, handling the infinite case. */
5623 range_predecessor (tree val
)
5625 tree type
= TREE_TYPE (val
);
5627 if (INTEGRAL_TYPE_P (type
)
5628 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
5631 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
5632 build_int_cst (TREE_TYPE (val
), 1), 0);
5635 /* Return the successor of VAL in its type, handling the infinite case. */
5638 range_successor (tree val
)
5640 tree type
= TREE_TYPE (val
);
5642 if (INTEGRAL_TYPE_P (type
)
5643 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
5646 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
5647 build_int_cst (TREE_TYPE (val
), 1), 0);
5650 /* Given two ranges, see if we can merge them into one. Return 1 if we
5651 can, 0 if we can't. Set the output range into the specified parameters. */
5654 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
5655 tree high0
, int in1_p
, tree low1
, tree high1
)
5663 int lowequal
= ((low0
== 0 && low1
== 0)
5664 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5665 low0
, 0, low1
, 0)));
5666 int highequal
= ((high0
== 0 && high1
== 0)
5667 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5668 high0
, 1, high1
, 1)));
5670 /* Make range 0 be the range that starts first, or ends last if they
5671 start at the same value. Swap them if it isn't. */
5672 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5675 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
5676 high1
, 1, high0
, 1))))
5678 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
5679 tem
= low0
, low0
= low1
, low1
= tem
;
5680 tem
= high0
, high0
= high1
, high1
= tem
;
5683 /* If the second range is != high1 where high1 is the type maximum of
5684 the type, try first merging with < high1 range. */
5687 && TREE_CODE (low1
) == INTEGER_CST
5688 && (TREE_CODE (TREE_TYPE (low1
)) == INTEGER_TYPE
5689 || (TREE_CODE (TREE_TYPE (low1
)) == ENUMERAL_TYPE
5690 && known_eq (TYPE_PRECISION (TREE_TYPE (low1
)),
5691 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1
))))))
5692 && operand_equal_p (low1
, high1
, 0))
5694 if (tree_int_cst_equal (low1
, TYPE_MAX_VALUE (TREE_TYPE (low1
)))
5695 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5696 !in1_p
, NULL_TREE
, range_predecessor (low1
)))
5698 /* Similarly for the second range != low1 where low1 is the type minimum
5699 of the type, try first merging with > low1 range. */
5700 if (tree_int_cst_equal (low1
, TYPE_MIN_VALUE (TREE_TYPE (low1
)))
5701 && merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
,
5702 !in1_p
, range_successor (low1
), NULL_TREE
))
5706 /* Now flag two cases, whether the ranges are disjoint or whether the
5707 second range is totally subsumed in the first. Note that the tests
5708 below are simplified by the ones above. */
5709 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
5710 high0
, 1, low1
, 0));
5711 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
5712 high1
, 1, high0
, 1));
5714 /* We now have four cases, depending on whether we are including or
5715 excluding the two ranges. */
5718 /* If they don't overlap, the result is false. If the second range
5719 is a subset it is the result. Otherwise, the range is from the start
5720 of the second to the end of the first. */
5722 in_p
= 0, low
= high
= 0;
5724 in_p
= 1, low
= low1
, high
= high1
;
5726 in_p
= 1, low
= low1
, high
= high0
;
5729 else if (in0_p
&& ! in1_p
)
5731 /* If they don't overlap, the result is the first range. If they are
5732 equal, the result is false. If the second range is a subset of the
5733 first, and the ranges begin at the same place, we go from just after
5734 the end of the second range to the end of the first. If the second
5735 range is not a subset of the first, or if it is a subset and both
5736 ranges end at the same place, the range starts at the start of the
5737 first range and ends just before the second range.
5738 Otherwise, we can't describe this as a single range. */
5740 in_p
= 1, low
= low0
, high
= high0
;
5741 else if (lowequal
&& highequal
)
5742 in_p
= 0, low
= high
= 0;
5743 else if (subset
&& lowequal
)
5745 low
= range_successor (high1
);
5750 /* We are in the weird situation where high0 > high1 but
5751 high1 has no successor. Punt. */
5755 else if (! subset
|| highequal
)
5758 high
= range_predecessor (low1
);
5762 /* low0 < low1 but low1 has no predecessor. Punt. */
5770 else if (! in0_p
&& in1_p
)
5772 /* If they don't overlap, the result is the second range. If the second
5773 is a subset of the first, the result is false. Otherwise,
5774 the range starts just after the first range and ends at the
5775 end of the second. */
5777 in_p
= 1, low
= low1
, high
= high1
;
5778 else if (subset
|| highequal
)
5779 in_p
= 0, low
= high
= 0;
5782 low
= range_successor (high0
);
5787 /* high1 > high0 but high0 has no successor. Punt. */
5795 /* The case where we are excluding both ranges. Here the complex case
5796 is if they don't overlap. In that case, the only time we have a
5797 range is if they are adjacent. If the second is a subset of the
5798 first, the result is the first. Otherwise, the range to exclude
5799 starts at the beginning of the first range and ends at the end of the
5803 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5804 range_successor (high0
),
5806 in_p
= 0, low
= low0
, high
= high1
;
5809 /* Canonicalize - [min, x] into - [-, x]. */
5810 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5811 switch (TREE_CODE (TREE_TYPE (low0
)))
5814 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0
)),
5816 (TYPE_MODE (TREE_TYPE (low0
)))))
5820 if (tree_int_cst_equal (low0
,
5821 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5825 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5826 && integer_zerop (low0
))
5833 /* Canonicalize - [x, max] into - [x, -]. */
5834 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5835 switch (TREE_CODE (TREE_TYPE (high1
)))
5838 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1
)),
5840 (TYPE_MODE (TREE_TYPE (high1
)))))
5844 if (tree_int_cst_equal (high1
,
5845 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5849 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5850 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5852 build_int_cst (TREE_TYPE (high1
), 1),
5860 /* The ranges might be also adjacent between the maximum and
5861 minimum values of the given type. For
5862 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5863 return + [x + 1, y - 1]. */
5864 if (low0
== 0 && high1
== 0)
5866 low
= range_successor (high0
);
5867 high
= range_predecessor (low1
);
5868 if (low
== 0 || high
== 0)
5878 in_p
= 0, low
= low0
, high
= high0
;
5880 in_p
= 0, low
= low0
, high
= high1
;
5883 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5888 /* Subroutine of fold, looking inside expressions of the form
5889 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5890 are the three operands of the COND_EXPR. This function is
5891 being used also to optimize A op B ? C : A, by reversing the
5894 Return a folded expression whose code is not a COND_EXPR
5895 anymore, or NULL_TREE if no folding opportunity is found. */
5898 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5899 enum tree_code comp_code
,
5900 tree arg00
, tree arg01
, tree arg1
, tree arg2
)
5902 tree arg1_type
= TREE_TYPE (arg1
);
5908 /* If we have A op 0 ? A : -A, consider applying the following
5911 A == 0? A : -A same as -A
5912 A != 0? A : -A same as A
5913 A >= 0? A : -A same as abs (A)
5914 A > 0? A : -A same as abs (A)
5915 A <= 0? A : -A same as -abs (A)
5916 A < 0? A : -A same as -abs (A)
5918 None of these transformations work for modes with signed
5919 zeros. If A is +/-0, the first two transformations will
5920 change the sign of the result (from +0 to -0, or vice
5921 versa). The last four will fix the sign of the result,
5922 even though the original expressions could be positive or
5923 negative, depending on the sign of A.
5925 Note that all these transformations are correct if A is
5926 NaN, since the two alternatives (A and -A) are also NaNs. */
5927 if (!HONOR_SIGNED_ZEROS (type
)
5928 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5929 ? real_zerop (arg01
)
5930 : integer_zerop (arg01
))
5931 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5932 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5933 /* In the case that A is of the form X-Y, '-A' (arg2) may
5934 have already been folded to Y-X, check for that. */
5935 || (TREE_CODE (arg1
) == MINUS_EXPR
5936 && TREE_CODE (arg2
) == MINUS_EXPR
5937 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5938 TREE_OPERAND (arg2
, 1), 0)
5939 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5940 TREE_OPERAND (arg2
, 0), 0))))
5945 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5946 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5949 return fold_convert_loc (loc
, type
, arg1
);
5952 if (flag_trapping_math
)
5957 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5959 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5960 return fold_convert_loc (loc
, type
, tem
);
5963 if (flag_trapping_math
)
5968 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5970 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
5971 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
5973 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
5974 is not, invokes UB both in abs and in the negation of it.
5975 So, use ABSU_EXPR instead. */
5976 tree utype
= unsigned_type_for (TREE_TYPE (arg1
));
5977 tem
= fold_build1_loc (loc
, ABSU_EXPR
, utype
, arg1
);
5978 tem
= negate_expr (tem
);
5979 return fold_convert_loc (loc
, type
, tem
);
5983 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5984 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5987 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5991 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5992 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5993 both transformations are correct when A is NaN: A != 0
5994 is then true, and A == 0 is false. */
5996 if (!HONOR_SIGNED_ZEROS (type
)
5997 && integer_zerop (arg01
) && integer_zerop (arg2
))
5999 if (comp_code
== NE_EXPR
)
6000 return fold_convert_loc (loc
, type
, arg1
);
6001 else if (comp_code
== EQ_EXPR
)
6002 return build_zero_cst (type
);
6005 /* Try some transformations of A op B ? A : B.
6007 A == B? A : B same as B
6008 A != B? A : B same as A
6009 A >= B? A : B same as max (A, B)
6010 A > B? A : B same as max (B, A)
6011 A <= B? A : B same as min (A, B)
6012 A < B? A : B same as min (B, A)
6014 As above, these transformations don't work in the presence
6015 of signed zeros. For example, if A and B are zeros of
6016 opposite sign, the first two transformations will change
6017 the sign of the result. In the last four, the original
6018 expressions give different results for (A=+0, B=-0) and
6019 (A=-0, B=+0), but the transformed expressions do not.
6021 The first two transformations are correct if either A or B
6022 is a NaN. In the first transformation, the condition will
6023 be false, and B will indeed be chosen. In the case of the
6024 second transformation, the condition A != B will be true,
6025 and A will be chosen.
6027 The conversions to max() and min() are not correct if B is
6028 a number and A is not. The conditions in the original
6029 expressions will be false, so all four give B. The min()
6030 and max() versions would give a NaN instead. */
6031 if (!HONOR_SIGNED_ZEROS (type
)
6032 && operand_equal_for_comparison_p (arg01
, arg2
)
6033 /* Avoid these transformations if the COND_EXPR may be used
6034 as an lvalue in the C++ front-end. PR c++/19199. */
6036 || VECTOR_TYPE_P (type
)
6037 || (! lang_GNU_CXX ()
6038 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
6039 || ! maybe_lvalue_p (arg1
)
6040 || ! maybe_lvalue_p (arg2
)))
6042 tree comp_op0
= arg00
;
6043 tree comp_op1
= arg01
;
6044 tree comp_type
= TREE_TYPE (comp_op0
);
6049 return fold_convert_loc (loc
, type
, arg2
);
6051 return fold_convert_loc (loc
, type
, arg1
);
6056 /* In C++ a ?: expression can be an lvalue, so put the
6057 operand which will be used if they are equal first
6058 so that we can convert this back to the
6059 corresponding COND_EXPR. */
6060 if (!HONOR_NANS (arg1
))
6062 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6063 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6064 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
6065 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
6066 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
6067 comp_op1
, comp_op0
);
6068 return fold_convert_loc (loc
, type
, tem
);
6075 if (!HONOR_NANS (arg1
))
6077 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
6078 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
6079 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
6080 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
6081 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
6082 comp_op1
, comp_op0
);
6083 return fold_convert_loc (loc
, type
, tem
);
6087 if (!HONOR_NANS (arg1
))
6088 return fold_convert_loc (loc
, type
, arg2
);
6091 if (!HONOR_NANS (arg1
))
6092 return fold_convert_loc (loc
, type
, arg1
);
6095 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
6105 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6106 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6107 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6111 /* EXP is some logical combination of boolean tests. See if we can
6112 merge it into some range test. Return the new tree if so. */
6115 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
6118 int or_op
= (code
== TRUTH_ORIF_EXPR
6119 || code
== TRUTH_OR_EXPR
);
6120 int in0_p
, in1_p
, in_p
;
6121 tree low0
, low1
, low
, high0
, high1
, high
;
6122 bool strict_overflow_p
= false;
6124 const char * const warnmsg
= G_("assuming signed overflow does not occur "
6125 "when simplifying range test");
6127 if (!INTEGRAL_TYPE_P (type
))
6130 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
6131 /* If op0 is known true or false and this is a short-circuiting
6132 operation we must not merge with op1 since that makes side-effects
6133 unconditional. So special-case this. */
6135 && ((code
== TRUTH_ORIF_EXPR
&& in0_p
)
6136 || (code
== TRUTH_ANDIF_EXPR
&& !in0_p
)))
6138 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
6140 /* If this is an OR operation, invert both sides; we will invert
6141 again at the end. */
6143 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
6145 /* If both expressions are the same, if we can merge the ranges, and we
6146 can build the range test, return it or it inverted. If one of the
6147 ranges is always true or always false, consider it to be the same
6148 expression as the other. */
6149 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
6150 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
6152 && (tem
= (build_range_check (loc
, type
,
6154 : rhs
!= 0 ? rhs
: integer_zero_node
,
6155 in_p
, low
, high
))) != 0)
6157 if (strict_overflow_p
)
6158 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
6159 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
6162 /* On machines where the branch cost is expensive, if this is a
6163 short-circuited branch and the underlying object on both sides
6164 is the same, make a non-short-circuit operation. */
6165 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
6166 if (param_logical_op_non_short_circuit
!= -1)
6167 logical_op_non_short_circuit
6168 = param_logical_op_non_short_circuit
;
6169 if (logical_op_non_short_circuit
6170 && !sanitize_coverage_p ()
6171 && lhs
!= 0 && rhs
!= 0
6172 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6173 && operand_equal_p (lhs
, rhs
, 0))
6175 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6176 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6177 which cases we can't do this. */
6178 if (simple_operand_p (lhs
))
6179 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6180 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6183 else if (!lang_hooks
.decls
.global_bindings_p ()
6184 && !CONTAINS_PLACEHOLDER_P (lhs
))
6186 tree common
= save_expr (lhs
);
6188 if ((lhs
= build_range_check (loc
, type
, common
,
6189 or_op
? ! in0_p
: in0_p
,
6191 && (rhs
= build_range_check (loc
, type
, common
,
6192 or_op
? ! in1_p
: in1_p
,
6195 if (strict_overflow_p
)
6196 fold_overflow_warning (warnmsg
,
6197 WARN_STRICT_OVERFLOW_COMPARISON
);
6198 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
6199 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
6208 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6209 bit value. Arrange things so the extra bits will be set to zero if and
6210 only if C is signed-extended to its full width. If MASK is nonzero,
6211 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6214 unextend (tree c
, int p
, int unsignedp
, tree mask
)
6216 tree type
= TREE_TYPE (c
);
6217 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
6220 if (p
== modesize
|| unsignedp
)
6223 /* We work by getting just the sign bit into the low-order bit, then
6224 into the high-order bit, then sign-extend. We then XOR that value
6226 temp
= build_int_cst (TREE_TYPE (c
),
6227 wi::extract_uhwi (wi::to_wide (c
), p
- 1, 1));
6229 /* We must use a signed type in order to get an arithmetic right shift.
6230 However, we must also avoid introducing accidental overflows, so that
6231 a subsequent call to integer_zerop will work. Hence we must
6232 do the type conversion here. At this point, the constant is either
6233 zero or one, and the conversion to a signed type can never overflow.
6234 We could get an overflow if this conversion is done anywhere else. */
6235 if (TYPE_UNSIGNED (type
))
6236 temp
= fold_convert (signed_type_for (type
), temp
);
6238 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
6239 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
6241 temp
= const_binop (BIT_AND_EXPR
, temp
,
6242 fold_convert (TREE_TYPE (c
), mask
));
6243 /* If necessary, convert the type back to match the type of C. */
6244 if (TYPE_UNSIGNED (type
))
6245 temp
= fold_convert (type
, temp
);
6247 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
6250 /* For an expression that has the form
6254 we can drop one of the inner expressions and simplify to
6258 LOC is the location of the resulting expression. OP is the inner
6259 logical operation; the left-hand side in the examples above, while CMPOP
6260 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6261 removing a condition that guards another, as in
6262 (A != NULL && A->...) || A == NULL
6263 which we must not transform. If RHS_ONLY is true, only eliminate the
6264 right-most operand of the inner logical operation. */
6267 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
6270 tree type
= TREE_TYPE (cmpop
);
6271 enum tree_code code
= TREE_CODE (cmpop
);
6272 enum tree_code truthop_code
= TREE_CODE (op
);
6273 tree lhs
= TREE_OPERAND (op
, 0);
6274 tree rhs
= TREE_OPERAND (op
, 1);
6275 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
6276 enum tree_code rhs_code
= TREE_CODE (rhs
);
6277 enum tree_code lhs_code
= TREE_CODE (lhs
);
6278 enum tree_code inv_code
;
6280 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
6283 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
6286 if (rhs_code
== truthop_code
)
6288 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
6289 if (newrhs
!= NULL_TREE
)
6292 rhs_code
= TREE_CODE (rhs
);
6295 if (lhs_code
== truthop_code
&& !rhs_only
)
6297 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
6298 if (newlhs
!= NULL_TREE
)
6301 lhs_code
= TREE_CODE (lhs
);
6305 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
6306 if (inv_code
== rhs_code
6307 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6308 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6310 if (!rhs_only
&& inv_code
== lhs_code
6311 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
6312 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
6314 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
6315 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
6320 /* Find ways of folding logical expressions of LHS and RHS:
6321 Try to merge two comparisons to the same innermost item.
6322 Look for range tests like "ch >= '0' && ch <= '9'".
6323 Look for combinations of simple terms on machines with expensive branches
6324 and evaluate the RHS unconditionally.
6326 For example, if we have p->a == 2 && p->b == 4 and we can make an
6327 object large enough to span both A and B, we can do this with a comparison
6328 against the object ANDed with the a mask.
6330 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6331 operations to do this with one comparison.
6333 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6334 function and the one above.
6336 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6337 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6339 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6342 We return the simplified tree or 0 if no optimization is possible. */
6345 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
6348 /* If this is the "or" of two comparisons, we can do something if
6349 the comparisons are NE_EXPR. If this is the "and", we can do something
6350 if the comparisons are EQ_EXPR. I.e.,
6351 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6353 WANTED_CODE is this operation code. For single bit fields, we can
6354 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6355 comparison for one-bit fields. */
6357 enum tree_code wanted_code
;
6358 enum tree_code lcode
, rcode
;
6359 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
6360 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
6361 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
6362 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
6363 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
6364 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
6365 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
6366 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
6367 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
6368 scalar_int_mode lnmode
, rnmode
;
6369 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
6370 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
6371 tree l_const
, r_const
;
6372 tree lntype
, rntype
, result
;
6373 HOST_WIDE_INT first_bit
, end_bit
;
6376 /* Start by getting the comparison codes. Fail if anything is volatile.
6377 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6378 it were surrounded with a NE_EXPR. */
6380 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
6383 lcode
= TREE_CODE (lhs
);
6384 rcode
= TREE_CODE (rhs
);
6386 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
6388 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
6389 build_int_cst (TREE_TYPE (lhs
), 0));
6393 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
6395 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
6396 build_int_cst (TREE_TYPE (rhs
), 0));
6400 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
6401 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
6404 ll_arg
= TREE_OPERAND (lhs
, 0);
6405 lr_arg
= TREE_OPERAND (lhs
, 1);
6406 rl_arg
= TREE_OPERAND (rhs
, 0);
6407 rr_arg
= TREE_OPERAND (rhs
, 1);
6409 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6410 if (simple_operand_p (ll_arg
)
6411 && simple_operand_p (lr_arg
))
6413 if (operand_equal_p (ll_arg
, rl_arg
, 0)
6414 && operand_equal_p (lr_arg
, rr_arg
, 0))
6416 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
6417 truth_type
, ll_arg
, lr_arg
);
6421 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
6422 && operand_equal_p (lr_arg
, rl_arg
, 0))
6424 result
= combine_comparisons (loc
, code
, lcode
,
6425 swap_tree_comparison (rcode
),
6426 truth_type
, ll_arg
, lr_arg
);
6432 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
6433 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
6435 /* If the RHS can be evaluated unconditionally and its operands are
6436 simple, it wins to evaluate the RHS unconditionally on machines
6437 with expensive branches. In this case, this isn't a comparison
6438 that can be merged. */
6440 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
6442 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
6443 && simple_operand_p (rl_arg
)
6444 && simple_operand_p (rr_arg
))
6446 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6447 if (code
== TRUTH_OR_EXPR
6448 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
6449 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
6450 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6451 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6452 return build2_loc (loc
, NE_EXPR
, truth_type
,
6453 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6455 build_int_cst (TREE_TYPE (ll_arg
), 0));
6457 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6458 if (code
== TRUTH_AND_EXPR
6459 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
6460 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
6461 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
6462 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
6463 return build2_loc (loc
, EQ_EXPR
, truth_type
,
6464 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
6466 build_int_cst (TREE_TYPE (ll_arg
), 0));
6469 /* See if the comparisons can be merged. Then get all the parameters for
6472 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
6473 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
6476 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
6478 ll_inner
= decode_field_reference (loc
, &ll_arg
,
6479 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
6480 &ll_unsignedp
, &ll_reversep
, &volatilep
,
6481 &ll_mask
, &ll_and_mask
);
6482 lr_inner
= decode_field_reference (loc
, &lr_arg
,
6483 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
6484 &lr_unsignedp
, &lr_reversep
, &volatilep
,
6485 &lr_mask
, &lr_and_mask
);
6486 rl_inner
= decode_field_reference (loc
, &rl_arg
,
6487 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
6488 &rl_unsignedp
, &rl_reversep
, &volatilep
,
6489 &rl_mask
, &rl_and_mask
);
6490 rr_inner
= decode_field_reference (loc
, &rr_arg
,
6491 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
6492 &rr_unsignedp
, &rr_reversep
, &volatilep
,
6493 &rr_mask
, &rr_and_mask
);
6495 /* It must be true that the inner operation on the lhs of each
6496 comparison must be the same if we are to be able to do anything.
6497 Then see if we have constants. If not, the same must be true for
6500 || ll_reversep
!= rl_reversep
6501 || ll_inner
== 0 || rl_inner
== 0
6502 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
6505 if (TREE_CODE (lr_arg
) == INTEGER_CST
6506 && TREE_CODE (rr_arg
) == INTEGER_CST
)
6508 l_const
= lr_arg
, r_const
= rr_arg
;
6509 lr_reversep
= ll_reversep
;
6511 else if (lr_reversep
!= rr_reversep
6512 || lr_inner
== 0 || rr_inner
== 0
6513 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
6516 l_const
= r_const
= 0;
6518 /* If either comparison code is not correct for our logical operation,
6519 fail. However, we can convert a one-bit comparison against zero into
6520 the opposite comparison against that bit being set in the field. */
6522 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
6523 if (lcode
!= wanted_code
)
6525 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
6527 /* Make the left operand unsigned, since we are only interested
6528 in the value of one bit. Otherwise we are doing the wrong
6537 /* This is analogous to the code for l_const above. */
6538 if (rcode
!= wanted_code
)
6540 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
6549 /* See if we can find a mode that contains both fields being compared on
6550 the left. If we can't, fail. Otherwise, update all constants and masks
6551 to be relative to a field of that size. */
6552 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
6553 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
6554 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6555 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
6556 volatilep
, &lnmode
))
6559 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
6560 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
6561 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
6562 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
6564 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6566 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
6567 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
6570 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
6571 size_int (xll_bitpos
));
6572 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
6573 size_int (xrl_bitpos
));
6574 if (ll_mask
== NULL_TREE
|| rl_mask
== NULL_TREE
)
6579 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
6580 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
6581 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
6582 if (l_const
== NULL_TREE
)
6584 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
6585 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6588 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6590 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6595 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
6596 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
6597 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
6598 if (r_const
== NULL_TREE
)
6600 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
6601 fold_build1_loc (loc
, BIT_NOT_EXPR
,
6604 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
6606 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
6610 /* If the right sides are not constant, do the same for it. Also,
6611 disallow this optimization if a size, signedness or storage order
6612 mismatch occurs between the left and right sides. */
6615 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
6616 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
6617 || ll_reversep
!= lr_reversep
6618 /* Make sure the two fields on the right
6619 correspond to the left without being swapped. */
6620 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
6623 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
6624 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
6625 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
6626 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
6627 volatilep
, &rnmode
))
6630 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
6631 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
6632 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
6633 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
6635 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
6637 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
6638 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
6641 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6643 size_int (xlr_bitpos
));
6644 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
6646 size_int (xrr_bitpos
));
6647 if (lr_mask
== NULL_TREE
|| rr_mask
== NULL_TREE
)
6650 /* Make a mask that corresponds to both fields being compared.
6651 Do this for both items being compared. If the operands are the
6652 same size and the bits being compared are in the same position
6653 then we can do this by masking both and comparing the masked
6655 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6656 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
6657 if (lnbitsize
== rnbitsize
6658 && xll_bitpos
== xlr_bitpos
6662 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6663 lntype
, lnbitsize
, lnbitpos
,
6664 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6665 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6666 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
6668 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
6669 rntype
, rnbitsize
, rnbitpos
,
6670 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
6671 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
6672 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
6674 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6677 /* There is still another way we can do something: If both pairs of
6678 fields being compared are adjacent, we may be able to make a wider
6679 field containing them both.
6681 Note that we still must mask the lhs/rhs expressions. Furthermore,
6682 the mask must be shifted to account for the shift done by
6683 make_bit_field_ref. */
6684 if (((ll_bitsize
+ ll_bitpos
== rl_bitpos
6685 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
6686 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
6687 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
6695 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
6696 ll_bitsize
+ rl_bitsize
,
6697 MIN (ll_bitpos
, rl_bitpos
),
6698 ll_unsignedp
, ll_reversep
);
6699 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
6700 lr_bitsize
+ rr_bitsize
,
6701 MIN (lr_bitpos
, rr_bitpos
),
6702 lr_unsignedp
, lr_reversep
);
6704 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
6705 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
6706 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
6707 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
6708 if (ll_mask
== NULL_TREE
|| lr_mask
== NULL_TREE
)
6711 /* Convert to the smaller type before masking out unwanted bits. */
6713 if (lntype
!= rntype
)
6715 if (lnbitsize
> rnbitsize
)
6717 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
6718 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
6721 else if (lnbitsize
< rnbitsize
)
6723 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
6724 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
6729 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
6730 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
6732 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
6733 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
6735 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
6741 /* Handle the case of comparisons with constants. If there is something in
6742 common between the masks, those bits of the constants must be the same.
6743 If not, the condition is always false. Test for this to avoid generating
6744 incorrect code below. */
6745 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
6746 if (! integer_zerop (result
)
6747 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
6748 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
6750 if (wanted_code
== NE_EXPR
)
6752 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6753 return constant_boolean_node (true, truth_type
);
6757 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6758 return constant_boolean_node (false, truth_type
);
6765 /* Construct the expression we will return. First get the component
6766 reference we will make. Unless the mask is all ones the width of
6767 that field, perform the mask operation. Then compare with the
6769 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
6770 lntype
, lnbitsize
, lnbitpos
,
6771 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
6773 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
6774 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
6775 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
6777 return build2_loc (loc
, wanted_code
, truth_type
, result
,
6778 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
6781 /* T is an integer expression that is being multiplied, divided, or taken a
6782 modulus (CODE says which and what kind of divide or modulus) by a
6783 constant C. See if we can eliminate that operation by folding it with
6784 other operations already in T. WIDE_TYPE, if non-null, is a type that
6785 should be used for the computation if wider than our type.
6787 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6788 (X * 2) + (Y * 4). We must, however, be assured that either the original
6789 expression would not overflow or that overflow is undefined for the type
6790 in the language in question.
6792 If we return a non-null expression, it is an equivalent form of the
6793 original computation, but need not be in the original type.
6795 We set *STRICT_OVERFLOW_P to true if the return values depends on
6796 signed overflow being undefined. Otherwise we do not change
6797 *STRICT_OVERFLOW_P. */
6800 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6801 bool *strict_overflow_p
)
6803 /* To avoid exponential search depth, refuse to allow recursion past
6804 three levels. Beyond that (1) it's highly unlikely that we'll find
6805 something interesting and (2) we've probably processed it before
6806 when we built the inner expression. */
6815 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6822 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6823 bool *strict_overflow_p
)
6825 tree type
= TREE_TYPE (t
);
6826 enum tree_code tcode
= TREE_CODE (t
);
6827 tree ctype
= (wide_type
!= 0
6828 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6829 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6830 ? wide_type
: type
);
6832 int same_p
= tcode
== code
;
6833 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6834 bool sub_strict_overflow_p
;
6836 /* Don't deal with constants of zero here; they confuse the code below. */
6837 if (integer_zerop (c
))
6840 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6841 op0
= TREE_OPERAND (t
, 0);
6843 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6844 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6846 /* Note that we need not handle conditional operations here since fold
6847 already handles those cases. So just do arithmetic here. */
6851 /* For a constant, we can always simplify if we are a multiply
6852 or (for divide and modulus) if it is a multiple of our constant. */
6853 if (code
== MULT_EXPR
6854 || wi::multiple_of_p (wi::to_wide (t
), wi::to_wide (c
),
6857 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6858 fold_convert (ctype
, c
));
6859 /* If the multiplication overflowed, we lost information on it.
6860 See PR68142 and PR69845. */
6861 if (TREE_OVERFLOW (tem
))
6867 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6868 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
6870 /* If op0 is an expression ... */
6871 if ((COMPARISON_CLASS_P (op0
)
6872 || UNARY_CLASS_P (op0
)
6873 || BINARY_CLASS_P (op0
)
6874 || VL_EXP_CLASS_P (op0
)
6875 || EXPRESSION_CLASS_P (op0
))
6876 /* ... and has wrapping overflow, and its type is smaller
6877 than ctype, then we cannot pass through as widening. */
6878 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
6879 && (TYPE_PRECISION (ctype
)
6880 > TYPE_PRECISION (TREE_TYPE (op0
))))
6881 /* ... or this is a truncation (t is narrower than op0),
6882 then we cannot pass through this narrowing. */
6883 || (TYPE_PRECISION (type
)
6884 < TYPE_PRECISION (TREE_TYPE (op0
)))
6885 /* ... or signedness changes for division or modulus,
6886 then we cannot pass through this conversion. */
6887 || (code
!= MULT_EXPR
6888 && (TYPE_UNSIGNED (ctype
)
6889 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6890 /* ... or has undefined overflow while the converted to
6891 type has not, we cannot do the operation in the inner type
6892 as that would introduce undefined overflow. */
6893 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
6894 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6897 /* Pass the constant down and see if we can make a simplification. If
6898 we can, replace this expression with the inner simplification for
6899 possible later conversion to our or some other type. */
6900 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6901 && TREE_CODE (t2
) == INTEGER_CST
6902 && !TREE_OVERFLOW (t2
)
6903 && (t1
= extract_muldiv (op0
, t2
, code
,
6904 code
== MULT_EXPR
? ctype
: NULL_TREE
,
6905 strict_overflow_p
)) != 0)
6910 /* If widening the type changes it from signed to unsigned, then we
6911 must avoid building ABS_EXPR itself as unsigned. */
6912 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6914 tree cstype
= (*signed_type_for
) (ctype
);
6915 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6918 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6919 return fold_convert (ctype
, t1
);
6923 /* If the constant is negative, we cannot simplify this. */
6924 if (tree_int_cst_sgn (c
) == -1)
6928 /* For division and modulus, type can't be unsigned, as e.g.
6929 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6930 For signed types, even with wrapping overflow, this is fine. */
6931 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6933 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6935 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6938 case MIN_EXPR
: case MAX_EXPR
:
6939 /* If widening the type changes the signedness, then we can't perform
6940 this optimization as that changes the result. */
6941 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6944 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6945 sub_strict_overflow_p
= false;
6946 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6947 &sub_strict_overflow_p
)) != 0
6948 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6949 &sub_strict_overflow_p
)) != 0)
6951 if (tree_int_cst_sgn (c
) < 0)
6952 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6953 if (sub_strict_overflow_p
)
6954 *strict_overflow_p
= true;
6955 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6956 fold_convert (ctype
, t2
));
6960 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6961 /* If the second operand is constant, this is a multiplication
6962 or floor division, by a power of two, so we can treat it that
6963 way unless the multiplier or divisor overflows. Signed
6964 left-shift overflow is implementation-defined rather than
6965 undefined in C90, so do not convert signed left shift into
6967 if (TREE_CODE (op1
) == INTEGER_CST
6968 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6969 /* const_binop may not detect overflow correctly,
6970 so check for it explicitly here. */
6971 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)),
6973 && (t1
= fold_convert (ctype
,
6974 const_binop (LSHIFT_EXPR
, size_one_node
,
6976 && !TREE_OVERFLOW (t1
))
6977 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6978 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6980 fold_convert (ctype
, op0
),
6982 c
, code
, wide_type
, strict_overflow_p
);
6985 case PLUS_EXPR
: case MINUS_EXPR
:
6986 /* See if we can eliminate the operation on both sides. If we can, we
6987 can return a new PLUS or MINUS. If we can't, the only remaining
6988 cases where we can do anything are if the second operand is a
6990 sub_strict_overflow_p
= false;
6991 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6992 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6993 if (t1
!= 0 && t2
!= 0
6994 && TYPE_OVERFLOW_WRAPS (ctype
)
6995 && (code
== MULT_EXPR
6996 /* If not multiplication, we can only do this if both operands
6997 are divisible by c. */
6998 || (multiple_of_p (ctype
, op0
, c
)
6999 && multiple_of_p (ctype
, op1
, c
))))
7001 if (sub_strict_overflow_p
)
7002 *strict_overflow_p
= true;
7003 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7004 fold_convert (ctype
, t2
));
7007 /* If this was a subtraction, negate OP1 and set it to be an addition.
7008 This simplifies the logic below. */
7009 if (tcode
== MINUS_EXPR
)
7011 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
7012 /* If OP1 was not easily negatable, the constant may be OP0. */
7013 if (TREE_CODE (op0
) == INTEGER_CST
)
7015 std::swap (op0
, op1
);
7020 if (TREE_CODE (op1
) != INTEGER_CST
)
7023 /* If either OP1 or C are negative, this optimization is not safe for
7024 some of the division and remainder types while for others we need
7025 to change the code. */
7026 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
7028 if (code
== CEIL_DIV_EXPR
)
7029 code
= FLOOR_DIV_EXPR
;
7030 else if (code
== FLOOR_DIV_EXPR
)
7031 code
= CEIL_DIV_EXPR
;
7032 else if (code
!= MULT_EXPR
7033 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
7037 /* If it's a multiply or a division/modulus operation of a multiple
7038 of our constant, do the operation and verify it doesn't overflow. */
7039 if (code
== MULT_EXPR
7040 || wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7043 op1
= const_binop (code
, fold_convert (ctype
, op1
),
7044 fold_convert (ctype
, c
));
7045 /* We allow the constant to overflow with wrapping semantics. */
7047 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
7053 /* If we have an unsigned type, we cannot widen the operation since it
7054 will change the result if the original computation overflowed. */
7055 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
7058 /* The last case is if we are a multiply. In that case, we can
7059 apply the distributive law to commute the multiply and addition
7060 if the multiplication of the constants doesn't overflow
7061 and overflow is defined. With undefined overflow
7062 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
7063 But fold_plusminus_mult_expr would factor back any power-of-two
7064 value so do not distribute in the first place in this case. */
7065 if (code
== MULT_EXPR
7066 && TYPE_OVERFLOW_WRAPS (ctype
)
7067 && !(tree_fits_shwi_p (c
) && pow2p_hwi (absu_hwi (tree_to_shwi (c
)))))
7068 return fold_build2 (tcode
, ctype
,
7069 fold_build2 (code
, ctype
,
7070 fold_convert (ctype
, op0
),
7071 fold_convert (ctype
, c
)),
7077 /* We have a special case here if we are doing something like
7078 (C * 8) % 4 since we know that's zero. */
7079 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
7080 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
7081 /* If the multiplication can overflow we cannot optimize this. */
7082 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
7083 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
7084 && wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7087 *strict_overflow_p
= true;
7088 return omit_one_operand (type
, integer_zero_node
, op0
);
7091 /* ... fall through ... */
7093 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
7094 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
7095 /* If we can extract our operation from the LHS, do so and return a
7096 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7097 do something only if the second operand is a constant. */
7099 && TYPE_OVERFLOW_WRAPS (ctype
)
7100 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
7101 strict_overflow_p
)) != 0)
7102 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
7103 fold_convert (ctype
, op1
));
7104 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
7105 && TYPE_OVERFLOW_WRAPS (ctype
)
7106 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
7107 strict_overflow_p
)) != 0)
7108 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7109 fold_convert (ctype
, t1
));
7110 else if (TREE_CODE (op1
) != INTEGER_CST
)
7113 /* If these are the same operation types, we can associate them
7114 assuming no overflow. */
7117 bool overflow_p
= false;
7118 wi::overflow_type overflow_mul
;
7119 signop sign
= TYPE_SIGN (ctype
);
7120 unsigned prec
= TYPE_PRECISION (ctype
);
7121 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
7122 wi::to_wide (c
, prec
),
7123 sign
, &overflow_mul
);
7124 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
7126 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
7129 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7130 wide_int_to_tree (ctype
, mul
));
7133 /* If these operations "cancel" each other, we have the main
7134 optimizations of this pass, which occur when either constant is a
7135 multiple of the other, in which case we replace this with either an
7136 operation or CODE or TCODE.
7138 If we have an unsigned type, we cannot do this since it will change
7139 the result if the original computation overflowed. */
7140 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
7141 && !TYPE_OVERFLOW_SANITIZED (ctype
)
7142 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
7143 || (tcode
== MULT_EXPR
7144 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
7145 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
7146 && code
!= MULT_EXPR
)))
7148 if (wi::multiple_of_p (wi::to_wide (op1
), wi::to_wide (c
),
7151 *strict_overflow_p
= true;
7152 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
7153 fold_convert (ctype
,
7154 const_binop (TRUNC_DIV_EXPR
,
7157 else if (wi::multiple_of_p (wi::to_wide (c
), wi::to_wide (op1
),
7160 *strict_overflow_p
= true;
7161 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
7162 fold_convert (ctype
,
7163 const_binop (TRUNC_DIV_EXPR
,
7176 /* Return a node which has the indicated constant VALUE (either 0 or
7177 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7178 and is of the indicated TYPE. */
7181 constant_boolean_node (bool value
, tree type
)
7183 if (type
== integer_type_node
)
7184 return value
? integer_one_node
: integer_zero_node
;
7185 else if (type
== boolean_type_node
)
7186 return value
? boolean_true_node
: boolean_false_node
;
7187 else if (VECTOR_TYPE_P (type
))
7188 return build_vector_from_val (type
,
7189 build_int_cst (TREE_TYPE (type
),
7192 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
7196 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7197 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7198 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7199 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7200 COND is the first argument to CODE; otherwise (as in the example
7201 given here), it is the second argument. TYPE is the type of the
7202 original expression. Return NULL_TREE if no simplification is
7206 fold_binary_op_with_conditional_arg (location_t loc
,
7207 enum tree_code code
,
7208 tree type
, tree op0
, tree op1
,
7209 tree cond
, tree arg
, int cond_first_p
)
7211 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
7212 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
7213 tree test
, true_value
, false_value
;
7214 tree lhs
= NULL_TREE
;
7215 tree rhs
= NULL_TREE
;
7216 enum tree_code cond_code
= COND_EXPR
;
7218 /* Do not move possibly trapping operations into the conditional as this
7219 pessimizes code and causes gimplification issues when applied late. */
7220 if (operation_could_trap_p (code
, FLOAT_TYPE_P (type
),
7221 ANY_INTEGRAL_TYPE_P (type
)
7222 && TYPE_OVERFLOW_TRAPS (type
), op1
))
7225 if (TREE_CODE (cond
) == COND_EXPR
7226 || TREE_CODE (cond
) == VEC_COND_EXPR
)
7228 test
= TREE_OPERAND (cond
, 0);
7229 true_value
= TREE_OPERAND (cond
, 1);
7230 false_value
= TREE_OPERAND (cond
, 2);
7231 /* If this operand throws an expression, then it does not make
7232 sense to try to perform a logical or arithmetic operation
7234 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
7236 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
7239 else if (!(TREE_CODE (type
) != VECTOR_TYPE
7240 && VECTOR_TYPE_P (TREE_TYPE (cond
))))
7242 tree testtype
= TREE_TYPE (cond
);
7244 true_value
= constant_boolean_node (true, testtype
);
7245 false_value
= constant_boolean_node (false, testtype
);
7248 /* Detect the case of mixing vector and scalar types - bail out. */
7251 if (VECTOR_TYPE_P (TREE_TYPE (test
)))
7252 cond_code
= VEC_COND_EXPR
;
7254 /* This transformation is only worthwhile if we don't have to wrap ARG
7255 in a SAVE_EXPR and the operation can be simplified without recursing
7256 on at least one of the branches once its pushed inside the COND_EXPR. */
7257 if (!TREE_CONSTANT (arg
)
7258 && (TREE_SIDE_EFFECTS (arg
)
7259 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
7260 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
7263 arg
= fold_convert_loc (loc
, arg_type
, arg
);
7266 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
7268 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
7270 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
7274 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
7276 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
7278 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
7281 /* Check that we have simplified at least one of the branches. */
7282 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
7285 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
7289 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7291 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7292 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7293 if ARG - ZERO_ARG is the same as X.
7295 If ARG is NULL, check for any value of type TYPE.
7297 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7298 and finite. The problematic cases are when X is zero, and its mode
7299 has signed zeros. In the case of rounding towards -infinity,
7300 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7301 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7304 fold_real_zero_addition_p (const_tree type
, const_tree arg
,
7305 const_tree zero_arg
, int negate
)
7307 if (!real_zerop (zero_arg
))
7310 /* Don't allow the fold with -fsignaling-nans. */
7311 if (arg
? tree_expr_maybe_signaling_nan_p (arg
) : HONOR_SNANS (type
))
7314 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7315 if (!HONOR_SIGNED_ZEROS (type
))
7318 /* There is no case that is safe for all rounding modes. */
7319 if (HONOR_SIGN_DEPENDENT_ROUNDING (type
))
7322 /* In a vector or complex, we would need to check the sign of all zeros. */
7323 if (TREE_CODE (zero_arg
) == VECTOR_CST
)
7324 zero_arg
= uniform_vector_p (zero_arg
);
7325 if (!zero_arg
|| TREE_CODE (zero_arg
) != REAL_CST
)
7328 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7329 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg
)))
7332 /* The mode has signed zeros, and we have to honor their sign.
7333 In this situation, there are only two cases we can return true for.
7334 (i) X - 0 is the same as X with default rounding.
7335 (ii) X + 0 is X when X can't possibly be -0.0. */
7336 return negate
|| (arg
&& !tree_expr_maybe_real_minus_zero_p (arg
));
7339 /* Subroutine of match.pd that optimizes comparisons of a division by
7340 a nonzero integer constant against an integer constant, i.e.
7343 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7344 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7347 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
7348 tree
*hi
, bool *neg_overflow
)
7350 tree prod
, tmp
, type
= TREE_TYPE (c1
);
7351 signop sign
= TYPE_SIGN (type
);
7352 wi::overflow_type overflow
;
7354 /* We have to do this the hard way to detect unsigned overflow.
7355 prod = int_const_binop (MULT_EXPR, c1, c2); */
7356 wide_int val
= wi::mul (wi::to_wide (c1
), wi::to_wide (c2
), sign
, &overflow
);
7357 prod
= force_fit_type (type
, val
, -1, overflow
);
7358 *neg_overflow
= false;
7360 if (sign
== UNSIGNED
)
7362 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7365 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7366 val
= wi::add (wi::to_wide (prod
), wi::to_wide (tmp
), sign
, &overflow
);
7367 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
7369 else if (tree_int_cst_sgn (c1
) >= 0)
7371 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
7372 switch (tree_int_cst_sgn (c2
))
7375 *neg_overflow
= true;
7376 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7381 *lo
= fold_negate_const (tmp
, type
);
7386 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7396 /* A negative divisor reverses the relational operators. */
7397 code
= swap_tree_comparison (code
);
7399 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
7400 switch (tree_int_cst_sgn (c2
))
7403 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
7408 *hi
= fold_negate_const (tmp
, type
);
7413 *neg_overflow
= true;
7414 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
7423 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
7426 if (TREE_OVERFLOW (*lo
)
7427 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
7429 if (TREE_OVERFLOW (*hi
)
7430 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
7436 /* Test whether it is preferable to swap two operands, ARG0 and
7437 ARG1, for example because ARG0 is an integer constant and ARG1
7441 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
7443 if (CONSTANT_CLASS_P (arg1
))
7445 if (CONSTANT_CLASS_P (arg0
))
7451 if (TREE_CONSTANT (arg1
))
7453 if (TREE_CONSTANT (arg0
))
7456 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7457 for commutative and comparison operators. Ensuring a canonical
7458 form allows the optimizers to find additional redundancies without
7459 having to explicitly check for both orderings. */
7460 if (TREE_CODE (arg0
) == SSA_NAME
7461 && TREE_CODE (arg1
) == SSA_NAME
7462 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
7465 /* Put SSA_NAMEs last. */
7466 if (TREE_CODE (arg1
) == SSA_NAME
)
7468 if (TREE_CODE (arg0
) == SSA_NAME
)
7471 /* Put variables last. */
7481 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7482 means A >= Y && A != MAX, but in this case we know that
7483 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7486 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7488 tree a
, typea
, type
= TREE_TYPE (bound
), a1
, diff
, y
;
7490 if (TREE_CODE (bound
) == LT_EXPR
)
7491 a
= TREE_OPERAND (bound
, 0);
7492 else if (TREE_CODE (bound
) == GT_EXPR
)
7493 a
= TREE_OPERAND (bound
, 1);
7497 typea
= TREE_TYPE (a
);
7498 if (!INTEGRAL_TYPE_P (typea
)
7499 && !POINTER_TYPE_P (typea
))
7502 if (TREE_CODE (ineq
) == LT_EXPR
)
7504 a1
= TREE_OPERAND (ineq
, 1);
7505 y
= TREE_OPERAND (ineq
, 0);
7507 else if (TREE_CODE (ineq
) == GT_EXPR
)
7509 a1
= TREE_OPERAND (ineq
, 0);
7510 y
= TREE_OPERAND (ineq
, 1);
7515 if (TREE_TYPE (a1
) != typea
)
7518 if (POINTER_TYPE_P (typea
))
7520 /* Convert the pointer types into integer before taking the difference. */
7521 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7522 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7523 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7526 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7528 if (!diff
|| !integer_onep (diff
))
7531 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7534 /* Fold a sum or difference of at least one multiplication.
7535 Returns the folded tree or NULL if no simplification could be made. */
7538 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7539 tree arg0
, tree arg1
)
7541 tree arg00
, arg01
, arg10
, arg11
;
7542 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7544 /* (A * C) +- (B * C) -> (A+-B) * C.
7545 (A * C) +- A -> A * (C+-1).
7546 We are most concerned about the case where C is a constant,
7547 but other combinations show up during loop reduction. Since
7548 it is not difficult, try all four possibilities. */
7550 if (TREE_CODE (arg0
) == MULT_EXPR
)
7552 arg00
= TREE_OPERAND (arg0
, 0);
7553 arg01
= TREE_OPERAND (arg0
, 1);
7555 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7557 arg00
= build_one_cst (type
);
7562 /* We cannot generate constant 1 for fract. */
7563 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7566 arg01
= build_one_cst (type
);
7568 if (TREE_CODE (arg1
) == MULT_EXPR
)
7570 arg10
= TREE_OPERAND (arg1
, 0);
7571 arg11
= TREE_OPERAND (arg1
, 1);
7573 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7575 arg10
= build_one_cst (type
);
7576 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7577 the purpose of this canonicalization. */
7578 if (wi::neg_p (wi::to_wide (arg1
), TYPE_SIGN (TREE_TYPE (arg1
)))
7579 && negate_expr_p (arg1
)
7580 && code
== PLUS_EXPR
)
7582 arg11
= negate_expr (arg1
);
7590 /* We cannot generate constant 1 for fract. */
7591 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7594 arg11
= build_one_cst (type
);
7598 /* Prefer factoring a common non-constant. */
7599 if (operand_equal_p (arg00
, arg10
, 0))
7600 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7601 else if (operand_equal_p (arg01
, arg11
, 0))
7602 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7603 else if (operand_equal_p (arg00
, arg11
, 0))
7604 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7605 else if (operand_equal_p (arg01
, arg10
, 0))
7606 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7608 /* No identical multiplicands; see if we can find a common
7609 power-of-two factor in non-power-of-two multiplies. This
7610 can help in multi-dimensional array access. */
7611 else if (tree_fits_shwi_p (arg01
) && tree_fits_shwi_p (arg11
))
7613 HOST_WIDE_INT int01
= tree_to_shwi (arg01
);
7614 HOST_WIDE_INT int11
= tree_to_shwi (arg11
);
7619 /* Move min of absolute values to int11. */
7620 if (absu_hwi (int01
) < absu_hwi (int11
))
7622 tmp
= int01
, int01
= int11
, int11
= tmp
;
7623 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7630 const unsigned HOST_WIDE_INT factor
= absu_hwi (int11
);
7632 && pow2p_hwi (factor
)
7633 && (int01
& (factor
- 1)) == 0
7634 /* The remainder should not be a constant, otherwise we
7635 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7636 increased the number of multiplications necessary. */
7637 && TREE_CODE (arg10
) != INTEGER_CST
)
7639 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7640 build_int_cst (TREE_TYPE (arg00
),
7645 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7652 if (! ANY_INTEGRAL_TYPE_P (type
)
7653 || TYPE_OVERFLOW_WRAPS (type
)
7654 /* We are neither factoring zero nor minus one. */
7655 || TREE_CODE (same
) == INTEGER_CST
)
7656 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7657 fold_build2_loc (loc
, code
, type
,
7658 fold_convert_loc (loc
, type
, alt0
),
7659 fold_convert_loc (loc
, type
, alt1
)),
7660 fold_convert_loc (loc
, type
, same
));
7662 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7663 same may be minus one and thus the multiplication may overflow. Perform
7664 the sum operation in an unsigned type. */
7665 tree utype
= unsigned_type_for (type
);
7666 tree tem
= fold_build2_loc (loc
, code
, utype
,
7667 fold_convert_loc (loc
, utype
, alt0
),
7668 fold_convert_loc (loc
, utype
, alt1
));
7669 /* If the sum evaluated to a constant that is not -INF the multiplication
7671 if (TREE_CODE (tem
) == INTEGER_CST
7672 && (wi::to_wide (tem
)
7673 != wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
7674 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7675 fold_convert (type
, tem
), same
);
7677 /* Do not resort to unsigned multiplication because
7678 we lose the no-overflow property of the expression. */
7682 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7683 specified by EXPR into the buffer PTR of length LEN bytes.
7684 Return the number of bytes placed in the buffer, or zero
7688 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7690 tree type
= TREE_TYPE (expr
);
7691 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7692 int byte
, offset
, word
, words
;
7693 unsigned char value
;
7695 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7702 return MIN (len
, total_bytes
- off
);
7704 words
= total_bytes
/ UNITS_PER_WORD
;
7706 for (byte
= 0; byte
< total_bytes
; byte
++)
7708 int bitpos
= byte
* BITS_PER_UNIT
;
7709 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7711 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7713 if (total_bytes
> UNITS_PER_WORD
)
7715 word
= byte
/ UNITS_PER_WORD
;
7716 if (WORDS_BIG_ENDIAN
)
7717 word
= (words
- 1) - word
;
7718 offset
= word
* UNITS_PER_WORD
;
7719 if (BYTES_BIG_ENDIAN
)
7720 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7722 offset
+= byte
% UNITS_PER_WORD
;
7725 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7726 if (offset
>= off
&& offset
- off
< len
)
7727 ptr
[offset
- off
] = value
;
7729 return MIN (len
, total_bytes
- off
);
7733 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7734 specified by EXPR into the buffer PTR of length LEN bytes.
7735 Return the number of bytes placed in the buffer, or zero
7739 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7741 tree type
= TREE_TYPE (expr
);
7742 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7743 int total_bytes
= GET_MODE_SIZE (mode
);
7744 FIXED_VALUE_TYPE value
;
7745 tree i_value
, i_type
;
7747 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7750 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7752 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7755 value
= TREE_FIXED_CST (expr
);
7756 i_value
= double_int_to_tree (i_type
, value
.data
);
7758 return native_encode_int (i_value
, ptr
, len
, off
);
7762 /* Subroutine of native_encode_expr. Encode the REAL_CST
7763 specified by EXPR into the buffer PTR of length LEN bytes.
7764 Return the number of bytes placed in the buffer, or zero
7768 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7770 tree type
= TREE_TYPE (expr
);
7771 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7772 int byte
, offset
, word
, words
, bitpos
;
7773 unsigned char value
;
7775 /* There are always 32 bits in each long, no matter the size of
7776 the hosts long. We handle floating point representations with
7780 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7787 return MIN (len
, total_bytes
- off
);
7789 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7791 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7793 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7794 bitpos
+= BITS_PER_UNIT
)
7796 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7797 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7799 if (UNITS_PER_WORD
< 4)
7801 word
= byte
/ UNITS_PER_WORD
;
7802 if (WORDS_BIG_ENDIAN
)
7803 word
= (words
- 1) - word
;
7804 offset
= word
* UNITS_PER_WORD
;
7805 if (BYTES_BIG_ENDIAN
)
7806 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7808 offset
+= byte
% UNITS_PER_WORD
;
7813 if (BYTES_BIG_ENDIAN
)
7815 /* Reverse bytes within each long, or within the entire float
7816 if it's smaller than a long (for HFmode). */
7817 offset
= MIN (3, total_bytes
- 1) - offset
;
7818 gcc_assert (offset
>= 0);
7821 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7823 && offset
- off
< len
)
7824 ptr
[offset
- off
] = value
;
7826 return MIN (len
, total_bytes
- off
);
7829 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7830 specified by EXPR into the buffer PTR of length LEN bytes.
7831 Return the number of bytes placed in the buffer, or zero
7835 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7840 part
= TREE_REALPART (expr
);
7841 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7842 if (off
== -1 && rsize
== 0)
7844 part
= TREE_IMAGPART (expr
);
7846 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7847 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7849 if (off
== -1 && isize
!= rsize
)
7851 return rsize
+ isize
;
7854 /* Like native_encode_vector, but only encode the first COUNT elements.
7855 The other arguments are as for native_encode_vector. */
7858 native_encode_vector_part (const_tree expr
, unsigned char *ptr
, int len
,
7859 int off
, unsigned HOST_WIDE_INT count
)
7861 tree itype
= TREE_TYPE (TREE_TYPE (expr
));
7862 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr
))
7863 && TYPE_PRECISION (itype
) <= BITS_PER_UNIT
)
7865 /* This is the only case in which elements can be smaller than a byte.
7866 Element 0 is always in the lsb of the containing byte. */
7867 unsigned int elt_bits
= TYPE_PRECISION (itype
);
7868 int total_bytes
= CEIL (elt_bits
* count
, BITS_PER_UNIT
);
7869 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7875 /* Zero the buffer and then set bits later where necessary. */
7876 int extract_bytes
= MIN (len
, total_bytes
- off
);
7878 memset (ptr
, 0, extract_bytes
);
7880 unsigned int elts_per_byte
= BITS_PER_UNIT
/ elt_bits
;
7881 unsigned int first_elt
= off
* elts_per_byte
;
7882 unsigned int extract_elts
= extract_bytes
* elts_per_byte
;
7883 for (unsigned int i
= 0; i
< extract_elts
; ++i
)
7885 tree elt
= VECTOR_CST_ELT (expr
, first_elt
+ i
);
7886 if (TREE_CODE (elt
) != INTEGER_CST
)
7889 if (ptr
&& wi::extract_uhwi (wi::to_wide (elt
), 0, 1))
7891 unsigned int bit
= i
* elt_bits
;
7892 ptr
[bit
/ BITS_PER_UNIT
] |= 1 << (bit
% BITS_PER_UNIT
);
7895 return extract_bytes
;
7899 int size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7900 for (unsigned HOST_WIDE_INT i
= 0; i
< count
; i
++)
7907 tree elem
= VECTOR_CST_ELT (expr
, i
);
7908 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7910 if ((off
== -1 && res
!= size
) || res
== 0)
7914 return (off
== -1 && i
< count
- 1) ? 0 : offset
;
7921 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7922 specified by EXPR into the buffer PTR of length LEN bytes.
7923 Return the number of bytes placed in the buffer, or zero
7927 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7929 unsigned HOST_WIDE_INT count
;
7930 if (!VECTOR_CST_NELTS (expr
).is_constant (&count
))
7932 return native_encode_vector_part (expr
, ptr
, len
, off
, count
);
7936 /* Subroutine of native_encode_expr. Encode the STRING_CST
7937 specified by EXPR into the buffer PTR of length LEN bytes.
7938 Return the number of bytes placed in the buffer, or zero
7942 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7944 tree type
= TREE_TYPE (expr
);
7946 /* Wide-char strings are encoded in target byte-order so native
7947 encoding them is trivial. */
7948 if (BITS_PER_UNIT
!= CHAR_BIT
7949 || TREE_CODE (type
) != ARRAY_TYPE
7950 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7951 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7954 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
7955 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7959 len
= MIN (total_bytes
- off
, len
);
7965 if (off
< TREE_STRING_LENGTH (expr
))
7967 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7968 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7970 memset (ptr
+ written
, 0, len
- written
);
7976 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7977 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7978 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7979 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7980 sufficient to encode the entire EXPR, or if OFF is out of bounds.
7981 Otherwise, start at byte offset OFF and encode at most LEN bytes.
7982 Return the number of bytes placed in the buffer, or zero upon failure. */
7985 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7987 /* We don't support starting at negative offset and -1 is special. */
7991 switch (TREE_CODE (expr
))
7994 return native_encode_int (expr
, ptr
, len
, off
);
7997 return native_encode_real (expr
, ptr
, len
, off
);
8000 return native_encode_fixed (expr
, ptr
, len
, off
);
8003 return native_encode_complex (expr
, ptr
, len
, off
);
8006 return native_encode_vector (expr
, ptr
, len
, off
);
8009 return native_encode_string (expr
, ptr
, len
, off
);
8016 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8017 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8018 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8019 machine modes, we can't just use build_nonstandard_integer_type. */
8022 find_bitfield_repr_type (int fieldsize
, int len
)
8025 for (int pass
= 0; pass
< 2; pass
++)
8027 enum mode_class mclass
= pass
? MODE_PARTIAL_INT
: MODE_INT
;
8028 FOR_EACH_MODE_IN_CLASS (mode
, mclass
)
8029 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8030 && known_eq (GET_MODE_PRECISION (mode
),
8031 GET_MODE_BITSIZE (mode
))
8032 && known_le (GET_MODE_SIZE (mode
), len
))
8034 tree ret
= lang_hooks
.types
.type_for_mode (mode
, 1);
8035 if (ret
&& TYPE_MODE (ret
) == mode
)
8040 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
8041 if (int_n_enabled_p
[i
]
8042 && int_n_data
[i
].bitsize
>= (unsigned) (BITS_PER_UNIT
* fieldsize
)
8043 && int_n_trees
[i
].unsigned_type
)
8045 tree ret
= int_n_trees
[i
].unsigned_type
;
8046 mode
= TYPE_MODE (ret
);
8047 if (known_ge (GET_MODE_SIZE (mode
), fieldsize
)
8048 && known_eq (GET_MODE_PRECISION (mode
),
8049 GET_MODE_BITSIZE (mode
))
8050 && known_le (GET_MODE_SIZE (mode
), len
))
8057 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8058 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8059 to be non-NULL and OFF zero), then in addition to filling the
8060 bytes pointed by PTR with the value also clear any bits pointed
8061 by MASK that are known to be initialized, keep them as is for
8062 e.g. uninitialized padding bits or uninitialized fields. */
8065 native_encode_initializer (tree init
, unsigned char *ptr
, int len
,
8066 int off
, unsigned char *mask
)
8070 /* We don't support starting at negative offset and -1 is special. */
8071 if (off
< -1 || init
== NULL_TREE
)
8074 gcc_assert (mask
== NULL
|| (off
== 0 && ptr
));
8077 switch (TREE_CODE (init
))
8079 case VIEW_CONVERT_EXPR
:
8080 case NON_LVALUE_EXPR
:
8081 return native_encode_initializer (TREE_OPERAND (init
, 0), ptr
, len
, off
,
8084 r
= native_encode_expr (init
, ptr
, len
, off
);
8086 memset (mask
, 0, r
);
8089 tree type
= TREE_TYPE (init
);
8090 HOST_WIDE_INT total_bytes
= int_size_in_bytes (type
);
8091 if (total_bytes
< 0)
8093 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
8095 int o
= off
== -1 ? 0 : off
;
8096 if (TREE_CODE (type
) == ARRAY_TYPE
)
8099 unsigned HOST_WIDE_INT cnt
;
8100 HOST_WIDE_INT curpos
= 0, fieldsize
, valueinit
= -1;
8101 constructor_elt
*ce
;
8103 if (!TYPE_DOMAIN (type
)
8104 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
8107 fieldsize
= int_size_in_bytes (TREE_TYPE (type
));
8111 min_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
8113 memset (ptr
, '\0', MIN (total_bytes
- off
, len
));
8115 for (cnt
= 0; ; cnt
++)
8117 tree val
= NULL_TREE
, index
= NULL_TREE
;
8118 HOST_WIDE_INT pos
= curpos
, count
= 0;
8120 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8125 else if (mask
== NULL
8126 || CONSTRUCTOR_NO_CLEARING (init
)
8127 || curpos
>= total_bytes
)
8132 if (index
&& TREE_CODE (index
) == RANGE_EXPR
)
8134 if (TREE_CODE (TREE_OPERAND (index
, 0)) != INTEGER_CST
8135 || TREE_CODE (TREE_OPERAND (index
, 1)) != INTEGER_CST
)
8138 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 0))
8139 - wi::to_offset (min_index
),
8140 TYPE_PRECISION (sizetype
));
8142 if (!wi::fits_shwi_p (pos
))
8144 pos
= wpos
.to_shwi ();
8146 = wi::sext (wi::to_offset (TREE_OPERAND (index
, 1))
8147 - wi::to_offset (TREE_OPERAND (index
, 0)),
8148 TYPE_PRECISION (sizetype
));
8149 if (!wi::fits_shwi_p (wcount
))
8151 count
= wcount
.to_shwi ();
8155 if (TREE_CODE (index
) != INTEGER_CST
)
8158 = wi::sext (wi::to_offset (index
)
8159 - wi::to_offset (min_index
),
8160 TYPE_PRECISION (sizetype
));
8162 if (!wi::fits_shwi_p (wpos
))
8164 pos
= wpos
.to_shwi ();
8167 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
) && curpos
!= pos
)
8169 if (valueinit
== -1)
8171 tree zero
= build_zero_cst (TREE_TYPE (type
));
8172 r
= native_encode_initializer (zero
, ptr
+ curpos
,
8175 if (TREE_CODE (zero
) == CONSTRUCTOR
)
8180 curpos
+= fieldsize
;
8182 while (curpos
!= pos
)
8184 memcpy (ptr
+ curpos
, ptr
+ valueinit
, fieldsize
);
8185 memcpy (mask
+ curpos
, mask
+ valueinit
, fieldsize
);
8186 curpos
+= fieldsize
;
8196 && (curpos
+ fieldsize
8197 <= (HOST_WIDE_INT
) off
+ len
)))
8202 memcpy (ptr
+ (curpos
- o
), ptr
+ (pos
- o
),
8205 memcpy (mask
+ curpos
, mask
+ pos
, fieldsize
);
8207 else if (!native_encode_initializer (val
,
8224 else if (curpos
+ fieldsize
> off
8225 && curpos
< (HOST_WIDE_INT
) off
+ len
)
8227 /* Partial overlap. */
8228 unsigned char *p
= NULL
;
8231 gcc_assert (mask
== NULL
);
8235 p
= ptr
+ curpos
- off
;
8236 l
= MIN ((HOST_WIDE_INT
) off
+ len
- curpos
,
8245 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8248 curpos
+= fieldsize
;
8250 while (count
-- != 0);
8252 return MIN (total_bytes
- off
, len
);
8254 else if (TREE_CODE (type
) == RECORD_TYPE
8255 || TREE_CODE (type
) == UNION_TYPE
)
8257 unsigned HOST_WIDE_INT cnt
;
8258 constructor_elt
*ce
;
8259 tree fld_base
= TYPE_FIELDS (type
);
8260 tree to_free
= NULL_TREE
;
8262 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
|| mask
== NULL
);
8264 memset (ptr
, '\0', MIN (total_bytes
- o
, len
));
8265 for (cnt
= 0; ; cnt
++)
8267 tree val
= NULL_TREE
, field
= NULL_TREE
;
8268 HOST_WIDE_INT pos
= 0, fieldsize
;
8269 unsigned HOST_WIDE_INT bpos
= 0, epos
= 0;
8274 to_free
= NULL_TREE
;
8277 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init
), cnt
, &ce
))
8281 if (field
== NULL_TREE
)
8284 pos
= int_byte_position (field
);
8285 if (off
!= -1 && (HOST_WIDE_INT
) off
+ len
<= pos
)
8288 else if (mask
== NULL
8289 || CONSTRUCTOR_NO_CLEARING (init
))
8294 if (mask
&& !CONSTRUCTOR_NO_CLEARING (init
))
8297 for (fld
= fld_base
; fld
; fld
= DECL_CHAIN (fld
))
8299 if (TREE_CODE (fld
) != FIELD_DECL
)
8303 if (DECL_PADDING_P (fld
))
8305 if (DECL_SIZE_UNIT (fld
) == NULL_TREE
8306 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld
)))
8308 if (integer_zerop (DECL_SIZE_UNIT (fld
)))
8312 if (fld
== NULL_TREE
)
8318 fld_base
= DECL_CHAIN (fld
);
8323 pos
= int_byte_position (field
);
8324 val
= build_zero_cst (TREE_TYPE (fld
));
8325 if (TREE_CODE (val
) == CONSTRUCTOR
)
8330 if (TREE_CODE (TREE_TYPE (field
)) == ARRAY_TYPE
8331 && TYPE_DOMAIN (TREE_TYPE (field
))
8332 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field
))))
8334 if (mask
|| off
!= -1)
8336 if (val
== NULL_TREE
)
8338 if (TREE_CODE (TREE_TYPE (val
)) != ARRAY_TYPE
)
8340 fieldsize
= int_size_in_bytes (TREE_TYPE (val
));
8342 || (int) fieldsize
!= fieldsize
8343 || (pos
+ fieldsize
) > INT_MAX
)
8345 if (pos
+ fieldsize
> total_bytes
)
8347 if (ptr
!= NULL
&& total_bytes
< len
)
8348 memset (ptr
+ total_bytes
, '\0',
8349 MIN (pos
+ fieldsize
, len
) - total_bytes
);
8350 total_bytes
= pos
+ fieldsize
;
8355 if (DECL_SIZE_UNIT (field
) == NULL_TREE
8356 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field
)))
8358 fieldsize
= tree_to_shwi (DECL_SIZE_UNIT (field
));
8363 /* Prepare to deal with integral bit-fields and filter out other
8364 bit-fields that do not start and end on a byte boundary. */
8365 if (DECL_BIT_FIELD (field
))
8367 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8369 bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8370 if (INTEGRAL_TYPE_P (TREE_TYPE (field
)))
8372 bpos
%= BITS_PER_UNIT
;
8373 fieldsize
= TYPE_PRECISION (TREE_TYPE (field
)) + bpos
;
8374 epos
= fieldsize
% BITS_PER_UNIT
;
8375 fieldsize
+= BITS_PER_UNIT
- 1;
8376 fieldsize
/= BITS_PER_UNIT
;
8378 else if (bpos
% BITS_PER_UNIT
8379 || DECL_SIZE (field
) == NULL_TREE
8380 || !tree_fits_shwi_p (DECL_SIZE (field
))
8381 || tree_to_shwi (DECL_SIZE (field
)) % BITS_PER_UNIT
)
8385 if (off
!= -1 && pos
+ fieldsize
<= off
)
8388 if (val
== NULL_TREE
)
8391 if (DECL_BIT_FIELD (field
)
8392 && INTEGRAL_TYPE_P (TREE_TYPE (field
)))
8394 /* FIXME: Handle PDP endian. */
8395 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
8398 if (TREE_CODE (val
) != INTEGER_CST
)
8401 tree repr
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8402 tree repr_type
= NULL_TREE
;
8403 HOST_WIDE_INT rpos
= 0;
8404 if (repr
&& INTEGRAL_TYPE_P (TREE_TYPE (repr
)))
8406 rpos
= int_byte_position (repr
);
8407 repr_type
= TREE_TYPE (repr
);
8411 repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8412 if (repr_type
== NULL_TREE
)
8414 HOST_WIDE_INT repr_size
= int_size_in_bytes (repr_type
);
8415 gcc_assert (repr_size
> 0 && repr_size
<= len
);
8416 if (pos
+ repr_size
<= o
+ len
)
8420 rpos
= o
+ len
- repr_size
;
8421 gcc_assert (rpos
<= pos
);
8427 wide_int w
= wi::to_wide (val
, TYPE_PRECISION (repr_type
));
8428 int diff
= (TYPE_PRECISION (repr_type
)
8429 - TYPE_PRECISION (TREE_TYPE (field
)));
8430 HOST_WIDE_INT bitoff
= (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8431 if (!BYTES_BIG_ENDIAN
)
8432 w
= wi::lshift (w
, bitoff
);
8434 w
= wi::lshift (w
, diff
- bitoff
);
8435 val
= wide_int_to_tree (repr_type
, w
);
8437 unsigned char buf
[MAX_BITSIZE_MODE_ANY_INT
8438 / BITS_PER_UNIT
+ 1];
8439 int l
= native_encode_int (val
, buf
, sizeof buf
, 0);
8440 if (l
* BITS_PER_UNIT
!= TYPE_PRECISION (repr_type
))
8446 /* If the bitfield does not start at byte boundary, handle
8447 the partial byte at the start. */
8449 && (off
== -1 || (pos
>= off
&& len
>= 1)))
8451 if (!BYTES_BIG_ENDIAN
)
8453 int msk
= (1 << bpos
) - 1;
8454 buf
[pos
- rpos
] &= ~msk
;
8455 buf
[pos
- rpos
] |= ptr
[pos
- o
] & msk
;
8458 if (fieldsize
> 1 || epos
== 0)
8461 mask
[pos
] &= (msk
| ~((1 << epos
) - 1));
8466 int msk
= (1 << (BITS_PER_UNIT
- bpos
)) - 1;
8467 buf
[pos
- rpos
] &= msk
;
8468 buf
[pos
- rpos
] |= ptr
[pos
- o
] & ~msk
;
8471 if (fieldsize
> 1 || epos
== 0)
8475 | ((1 << (BITS_PER_UNIT
- epos
))
8480 /* If the bitfield does not end at byte boundary, handle
8481 the partial byte at the end. */
8484 || pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
))
8486 if (!BYTES_BIG_ENDIAN
)
8488 int msk
= (1 << epos
) - 1;
8489 buf
[pos
- rpos
+ fieldsize
- 1] &= msk
;
8490 buf
[pos
- rpos
+ fieldsize
- 1]
8491 |= ptr
[pos
+ fieldsize
- 1 - o
] & ~msk
;
8492 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8493 mask
[pos
+ fieldsize
- 1] &= ~msk
;
8497 int msk
= (1 << (BITS_PER_UNIT
- epos
)) - 1;
8498 buf
[pos
- rpos
+ fieldsize
- 1] &= ~msk
;
8499 buf
[pos
- rpos
+ fieldsize
- 1]
8500 |= ptr
[pos
+ fieldsize
- 1 - o
] & msk
;
8501 if (mask
&& (fieldsize
> 1 || bpos
== 0))
8502 mask
[pos
+ fieldsize
- 1] &= msk
;
8507 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8509 memcpy (ptr
+ pos
- o
, buf
+ (pos
- rpos
), fieldsize
);
8510 if (mask
&& (fieldsize
> (bpos
!= 0) + (epos
!= 0)))
8511 memset (mask
+ pos
+ (bpos
!= 0), 0,
8512 fieldsize
- (bpos
!= 0) - (epos
!= 0));
8516 /* Partial overlap. */
8517 HOST_WIDE_INT fsz
= fieldsize
;
8518 gcc_assert (mask
== NULL
);
8524 if (pos
+ fsz
> (HOST_WIDE_INT
) off
+ len
)
8525 fsz
= (HOST_WIDE_INT
) off
+ len
- pos
;
8526 memcpy (ptr
+ pos
- off
, buf
+ (pos
- rpos
), fsz
);
8533 && (pos
+ fieldsize
<= (HOST_WIDE_INT
) off
+ len
)))
8535 int fldsize
= fieldsize
;
8538 tree fld
= DECL_CHAIN (field
);
8541 if (TREE_CODE (fld
) == FIELD_DECL
)
8543 fld
= DECL_CHAIN (fld
);
8545 if (fld
== NULL_TREE
)
8546 fldsize
= len
- pos
;
8548 r
= native_encode_initializer (val
, ptr
? ptr
+ pos
- o
8552 mask
? mask
+ pos
: NULL
);
8556 && fldsize
!= fieldsize
8558 && pos
+ r
> total_bytes
)
8559 total_bytes
= pos
+ r
;
8563 /* Partial overlap. */
8564 unsigned char *p
= NULL
;
8567 gcc_assert (mask
== NULL
);
8571 p
= ptr
+ pos
- off
;
8572 l
= MIN ((HOST_WIDE_INT
) off
+ len
- pos
,
8581 if (!native_encode_initializer (val
, p
, l
, no
, NULL
))
8585 return MIN (total_bytes
- off
, len
);
8592 /* Subroutine of native_interpret_expr. Interpret the contents of
8593 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8594 If the buffer cannot be interpreted, return NULL_TREE. */
8597 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
8599 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
8601 if (total_bytes
> len
8602 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8605 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
8607 return wide_int_to_tree (type
, result
);
8611 /* Subroutine of native_interpret_expr. Interpret the contents of
8612 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8613 If the buffer cannot be interpreted, return NULL_TREE. */
8616 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
8618 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
8619 int total_bytes
= GET_MODE_SIZE (mode
);
8621 FIXED_VALUE_TYPE fixed_value
;
8623 if (total_bytes
> len
8624 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
8627 result
= double_int::from_buffer (ptr
, total_bytes
);
8628 fixed_value
= fixed_from_double_int (result
, mode
);
8630 return build_fixed (type
, fixed_value
);
8634 /* Subroutine of native_interpret_expr. Interpret the contents of
8635 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8636 If the buffer cannot be interpreted, return NULL_TREE. */
8639 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
8641 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8642 int total_bytes
= GET_MODE_SIZE (mode
);
8643 unsigned char value
;
8644 /* There are always 32 bits in each long, no matter the size of
8645 the hosts long. We handle floating point representations with
8650 if (total_bytes
> len
|| total_bytes
> 24)
8652 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
8654 memset (tmp
, 0, sizeof (tmp
));
8655 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
8656 bitpos
+= BITS_PER_UNIT
)
8658 /* Both OFFSET and BYTE index within a long;
8659 bitpos indexes the whole float. */
8660 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
8661 if (UNITS_PER_WORD
< 4)
8663 int word
= byte
/ UNITS_PER_WORD
;
8664 if (WORDS_BIG_ENDIAN
)
8665 word
= (words
- 1) - word
;
8666 offset
= word
* UNITS_PER_WORD
;
8667 if (BYTES_BIG_ENDIAN
)
8668 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
8670 offset
+= byte
% UNITS_PER_WORD
;
8675 if (BYTES_BIG_ENDIAN
)
8677 /* Reverse bytes within each long, or within the entire float
8678 if it's smaller than a long (for HFmode). */
8679 offset
= MIN (3, total_bytes
- 1) - offset
;
8680 gcc_assert (offset
>= 0);
8683 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
8685 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
8688 real_from_target (&r
, tmp
, mode
);
8689 return build_real (type
, r
);
8693 /* Subroutine of native_interpret_expr. Interpret the contents of
8694 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8695 If the buffer cannot be interpreted, return NULL_TREE. */
8698 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
8700 tree etype
, rpart
, ipart
;
8703 etype
= TREE_TYPE (type
);
8704 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8707 rpart
= native_interpret_expr (etype
, ptr
, size
);
8710 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
8713 return build_complex (type
, rpart
, ipart
);
8716 /* Read a vector of type TYPE from the target memory image given by BYTES,
8717 which contains LEN bytes. The vector is known to be encodable using
8718 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8720 Return the vector on success, otherwise return null. */
8723 native_interpret_vector_part (tree type
, const unsigned char *bytes
,
8724 unsigned int len
, unsigned int npatterns
,
8725 unsigned int nelts_per_pattern
)
8727 tree elt_type
= TREE_TYPE (type
);
8728 if (VECTOR_BOOLEAN_TYPE_P (type
)
8729 && TYPE_PRECISION (elt_type
) <= BITS_PER_UNIT
)
8731 /* This is the only case in which elements can be smaller than a byte.
8732 Element 0 is always in the lsb of the containing byte. */
8733 unsigned int elt_bits
= TYPE_PRECISION (elt_type
);
8734 if (elt_bits
* npatterns
* nelts_per_pattern
> len
* BITS_PER_UNIT
)
8737 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8738 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8740 unsigned int bit_index
= i
* elt_bits
;
8741 unsigned int byte_index
= bit_index
/ BITS_PER_UNIT
;
8742 unsigned int lsb
= bit_index
% BITS_PER_UNIT
;
8743 builder
.quick_push (bytes
[byte_index
] & (1 << lsb
)
8744 ? build_all_ones_cst (elt_type
)
8745 : build_zero_cst (elt_type
));
8747 return builder
.build ();
8750 unsigned int elt_bytes
= tree_to_uhwi (TYPE_SIZE_UNIT (elt_type
));
8751 if (elt_bytes
* npatterns
* nelts_per_pattern
> len
)
8754 tree_vector_builder
builder (type
, npatterns
, nelts_per_pattern
);
8755 for (unsigned int i
= 0; i
< builder
.encoded_nelts (); ++i
)
8757 tree elt
= native_interpret_expr (elt_type
, bytes
, elt_bytes
);
8760 builder
.quick_push (elt
);
8763 return builder
.build ();
8766 /* Subroutine of native_interpret_expr. Interpret the contents of
8767 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8768 If the buffer cannot be interpreted, return NULL_TREE. */
8771 native_interpret_vector (tree type
, const unsigned char *ptr
, unsigned int len
)
8775 unsigned HOST_WIDE_INT count
;
8777 etype
= TREE_TYPE (type
);
8778 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
8779 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&count
)
8780 || size
* count
> len
)
8783 return native_interpret_vector_part (type
, ptr
, len
, count
, 1);
8787 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8788 the buffer PTR of length LEN as a constant of type TYPE. For
8789 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8790 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8791 return NULL_TREE. */
8794 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
8796 switch (TREE_CODE (type
))
8802 case REFERENCE_TYPE
:
8804 return native_interpret_int (type
, ptr
, len
);
8807 if (tree ret
= native_interpret_real (type
, ptr
, len
))
8809 /* For floating point values in composite modes, punt if this
8810 folding doesn't preserve bit representation. As the mode doesn't
8811 have fixed precision while GCC pretends it does, there could be
8812 valid values that GCC can't really represent accurately.
8813 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8814 bit combinationations which GCC doesn't preserve. */
8815 unsigned char buf
[24 * 2];
8816 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
8817 int total_bytes
= GET_MODE_SIZE (mode
);
8818 memcpy (buf
+ 24, ptr
, total_bytes
);
8819 clear_type_padding_in_mask (type
, buf
+ 24);
8820 if (native_encode_expr (ret
, buf
, total_bytes
, 0) != total_bytes
8821 || memcmp (buf
+ 24, buf
, total_bytes
) != 0)
8827 case FIXED_POINT_TYPE
:
8828 return native_interpret_fixed (type
, ptr
, len
);
8831 return native_interpret_complex (type
, ptr
, len
);
8834 return native_interpret_vector (type
, ptr
, len
);
8841 /* Returns true if we can interpret the contents of a native encoding
8845 can_native_interpret_type_p (tree type
)
8847 switch (TREE_CODE (type
))
8853 case REFERENCE_TYPE
:
8854 case FIXED_POINT_TYPE
:
8865 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8866 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8869 native_interpret_aggregate (tree type
, const unsigned char *ptr
, int off
,
8872 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
8873 if (TREE_CODE (type
) == ARRAY_TYPE
)
8875 HOST_WIDE_INT eltsz
= int_size_in_bytes (TREE_TYPE (type
));
8876 if (eltsz
< 0 || eltsz
> len
|| TYPE_DOMAIN (type
) == NULL_TREE
)
8879 HOST_WIDE_INT cnt
= 0;
8880 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
8882 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
8884 cnt
= tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) + 1;
8888 HOST_WIDE_INT pos
= 0;
8889 for (HOST_WIDE_INT i
= 0; i
< cnt
; i
++, pos
+= eltsz
)
8892 if (pos
>= len
|| pos
+ eltsz
> len
)
8894 if (can_native_interpret_type_p (TREE_TYPE (type
)))
8896 v
= native_interpret_expr (TREE_TYPE (type
),
8897 ptr
+ off
+ pos
, eltsz
);
8901 else if (TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
8902 || TREE_CODE (TREE_TYPE (type
)) == ARRAY_TYPE
)
8903 v
= native_interpret_aggregate (TREE_TYPE (type
), ptr
, off
+ pos
,
8907 CONSTRUCTOR_APPEND_ELT (elts
, size_int (i
), v
);
8909 return build_constructor (type
, elts
);
8911 if (TREE_CODE (type
) != RECORD_TYPE
)
8913 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
8915 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_PADDING_P (field
))
8918 HOST_WIDE_INT bitoff
= 0, pos
= 0, sz
= 0;
8921 if (DECL_BIT_FIELD (field
))
8923 fld
= DECL_BIT_FIELD_REPRESENTATIVE (field
);
8924 if (fld
&& INTEGRAL_TYPE_P (TREE_TYPE (fld
)))
8926 poly_int64 bitoffset
;
8927 poly_uint64 field_offset
, fld_offset
;
8928 if (poly_int_tree_p (DECL_FIELD_OFFSET (field
), &field_offset
)
8929 && poly_int_tree_p (DECL_FIELD_OFFSET (fld
), &fld_offset
))
8930 bitoffset
= (field_offset
- fld_offset
) * BITS_PER_UNIT
;
8933 bitoffset
+= (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
))
8934 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld
)));
8935 diff
= (TYPE_PRECISION (TREE_TYPE (fld
))
8936 - TYPE_PRECISION (TREE_TYPE (field
)));
8937 if (!bitoffset
.is_constant (&bitoff
)
8944 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field
)))
8946 int fieldsize
= TYPE_PRECISION (TREE_TYPE (field
));
8947 int bpos
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field
));
8948 bpos
%= BITS_PER_UNIT
;
8950 fieldsize
+= BITS_PER_UNIT
- 1;
8951 fieldsize
/= BITS_PER_UNIT
;
8952 tree repr_type
= find_bitfield_repr_type (fieldsize
, len
);
8953 if (repr_type
== NULL_TREE
)
8955 sz
= int_size_in_bytes (repr_type
);
8956 if (sz
< 0 || sz
> len
)
8958 pos
= int_byte_position (field
);
8959 if (pos
< 0 || pos
> len
|| pos
+ fieldsize
> len
)
8962 if (pos
+ sz
<= len
)
8967 gcc_assert (rpos
<= pos
);
8969 bitoff
= (HOST_WIDE_INT
) (pos
- rpos
) * BITS_PER_UNIT
+ bpos
;
8971 diff
= (TYPE_PRECISION (repr_type
)
8972 - TYPE_PRECISION (TREE_TYPE (field
)));
8973 v
= native_interpret_expr (repr_type
, ptr
+ off
+ pos
, sz
);
8982 sz
= int_size_in_bytes (TREE_TYPE (fld
));
8983 if (sz
< 0 || sz
> len
)
8985 tree byte_pos
= byte_position (fld
);
8986 if (!tree_fits_shwi_p (byte_pos
))
8988 pos
= tree_to_shwi (byte_pos
);
8989 if (pos
< 0 || pos
> len
|| pos
+ sz
> len
)
8992 if (fld
== NULL_TREE
)
8993 /* Already handled above. */;
8994 else if (can_native_interpret_type_p (TREE_TYPE (fld
)))
8996 v
= native_interpret_expr (TREE_TYPE (fld
),
8997 ptr
+ off
+ pos
, sz
);
9001 else if (TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
9002 || TREE_CODE (TREE_TYPE (fld
)) == ARRAY_TYPE
)
9003 v
= native_interpret_aggregate (TREE_TYPE (fld
), ptr
, off
+ pos
, sz
);
9008 if (TREE_CODE (v
) != INTEGER_CST
)
9011 /* FIXME: Figure out how to handle PDP endian bitfields. */
9012 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
9014 if (!BYTES_BIG_ENDIAN
)
9015 v
= wide_int_to_tree (TREE_TYPE (field
),
9016 wi::lrshift (wi::to_wide (v
), bitoff
));
9018 v
= wide_int_to_tree (TREE_TYPE (field
),
9019 wi::lrshift (wi::to_wide (v
),
9022 CONSTRUCTOR_APPEND_ELT (elts
, field
, v
);
9024 return build_constructor (type
, elts
);
9027 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9028 or extracted constant positions and/or sizes aren't byte aligned. */
9030 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9031 bits between adjacent elements. AMNT should be within
9034 00011111|11100000 << 2 = 01111111|10000000
9035 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9038 shift_bytes_in_array_left (unsigned char *ptr
, unsigned int sz
,
9044 unsigned char carry_over
= 0U;
9045 unsigned char carry_mask
= (~0U) << (unsigned char) (BITS_PER_UNIT
- amnt
);
9046 unsigned char clear_mask
= (~0U) << amnt
;
9048 for (unsigned int i
= 0; i
< sz
; i
++)
9050 unsigned prev_carry_over
= carry_over
;
9051 carry_over
= (ptr
[i
] & carry_mask
) >> (BITS_PER_UNIT
- amnt
);
9056 ptr
[i
] &= clear_mask
;
9057 ptr
[i
] |= prev_carry_over
;
9062 /* Like shift_bytes_in_array_left but for big-endian.
9063 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9064 bits between adjacent elements. AMNT should be within
9067 00011111|11100000 >> 2 = 00000111|11111000
9068 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9071 shift_bytes_in_array_right (unsigned char *ptr
, unsigned int sz
,
9077 unsigned char carry_over
= 0U;
9078 unsigned char carry_mask
= ~(~0U << amnt
);
9080 for (unsigned int i
= 0; i
< sz
; i
++)
9082 unsigned prev_carry_over
= carry_over
;
9083 carry_over
= ptr
[i
] & carry_mask
;
9085 carry_over
<<= (unsigned char) BITS_PER_UNIT
- amnt
;
9087 ptr
[i
] |= prev_carry_over
;
9091 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9092 directly on the VECTOR_CST encoding, in a way that works for variable-
9093 length vectors. Return the resulting VECTOR_CST on success or null
9097 fold_view_convert_vector_encoding (tree type
, tree expr
)
9099 tree expr_type
= TREE_TYPE (expr
);
9100 poly_uint64 type_bits
, expr_bits
;
9101 if (!poly_int_tree_p (TYPE_SIZE (type
), &type_bits
)
9102 || !poly_int_tree_p (TYPE_SIZE (expr_type
), &expr_bits
))
9105 poly_uint64 type_units
= TYPE_VECTOR_SUBPARTS (type
);
9106 poly_uint64 expr_units
= TYPE_VECTOR_SUBPARTS (expr_type
);
9107 unsigned int type_elt_bits
= vector_element_size (type_bits
, type_units
);
9108 unsigned int expr_elt_bits
= vector_element_size (expr_bits
, expr_units
);
9110 /* We can only preserve the semantics of a stepped pattern if the new
9111 vector element is an integer of the same size. */
9112 if (VECTOR_CST_STEPPED_P (expr
)
9113 && (!INTEGRAL_TYPE_P (type
) || type_elt_bits
!= expr_elt_bits
))
9116 /* The number of bits needed to encode one element from every pattern
9117 of the original vector. */
9118 unsigned int expr_sequence_bits
9119 = VECTOR_CST_NPATTERNS (expr
) * expr_elt_bits
;
9121 /* The number of bits needed to encode one element from every pattern
9123 unsigned int type_sequence_bits
9124 = least_common_multiple (expr_sequence_bits
, type_elt_bits
);
9126 /* Don't try to read more bytes than are available, which can happen
9127 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9128 The general VIEW_CONVERT handling can cope with that case, so there's
9129 no point complicating things here. */
9130 unsigned int nelts_per_pattern
= VECTOR_CST_NELTS_PER_PATTERN (expr
);
9131 unsigned int buffer_bytes
= CEIL (nelts_per_pattern
* type_sequence_bits
,
9133 unsigned int buffer_bits
= buffer_bytes
* BITS_PER_UNIT
;
9134 if (known_gt (buffer_bits
, expr_bits
))
9137 /* Get enough bytes of EXPR to form the new encoding. */
9138 auto_vec
<unsigned char, 128> buffer (buffer_bytes
);
9139 buffer
.quick_grow (buffer_bytes
);
9140 if (native_encode_vector_part (expr
, buffer
.address (), buffer_bytes
, 0,
9141 buffer_bits
/ expr_elt_bits
)
9142 != (int) buffer_bytes
)
9145 /* Reencode the bytes as TYPE. */
9146 unsigned int type_npatterns
= type_sequence_bits
/ type_elt_bits
;
9147 return native_interpret_vector_part (type
, &buffer
[0], buffer
.length (),
9148 type_npatterns
, nelts_per_pattern
);
9151 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9152 TYPE at compile-time. If we're unable to perform the conversion
9153 return NULL_TREE. */
9156 fold_view_convert_expr (tree type
, tree expr
)
9158 /* We support up to 512-bit values (for V8DFmode). */
9159 unsigned char buffer
[64];
9162 /* Check that the host and target are sane. */
9163 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
9166 if (VECTOR_TYPE_P (type
) && TREE_CODE (expr
) == VECTOR_CST
)
9167 if (tree res
= fold_view_convert_vector_encoding (type
, expr
))
9170 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
9174 return native_interpret_expr (type
, buffer
, len
);
9177 /* Build an expression for the address of T. Folds away INDIRECT_REF
9178 to avoid confusing the gimplify process. */
9181 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
9183 /* The size of the object is not relevant when talking about its address. */
9184 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
9185 t
= TREE_OPERAND (t
, 0);
9187 if (INDIRECT_REF_P (t
))
9189 t
= TREE_OPERAND (t
, 0);
9191 if (TREE_TYPE (t
) != ptrtype
)
9192 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
9194 else if (TREE_CODE (t
) == MEM_REF
9195 && integer_zerop (TREE_OPERAND (t
, 1)))
9197 t
= TREE_OPERAND (t
, 0);
9199 if (TREE_TYPE (t
) != ptrtype
)
9200 t
= fold_convert_loc (loc
, ptrtype
, t
);
9202 else if (TREE_CODE (t
) == MEM_REF
9203 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
9204 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
9205 TREE_OPERAND (t
, 0),
9206 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
9207 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
9209 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
9211 if (TREE_TYPE (t
) != ptrtype
)
9212 t
= fold_convert_loc (loc
, ptrtype
, t
);
9215 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
9220 /* Build an expression for the address of T. */
9223 build_fold_addr_expr_loc (location_t loc
, tree t
)
9225 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
9227 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
9230 /* Fold a unary expression of code CODE and type TYPE with operand
9231 OP0. Return the folded expression if folding is successful.
9232 Otherwise, return NULL_TREE. */
9235 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
9239 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9241 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9242 && TREE_CODE_LENGTH (code
) == 1);
9247 if (CONVERT_EXPR_CODE_P (code
)
9248 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
9250 /* Don't use STRIP_NOPS, because signedness of argument type
9252 STRIP_SIGN_NOPS (arg0
);
9256 /* Strip any conversions that don't change the mode. This
9257 is safe for every expression, except for a comparison
9258 expression because its signedness is derived from its
9261 Note that this is done as an internal manipulation within
9262 the constant folder, in order to find the simplest
9263 representation of the arguments so that their form can be
9264 studied. In any cases, the appropriate type conversions
9265 should be put back in the tree that will get out of the
9270 if (CONSTANT_CLASS_P (arg0
))
9272 tree tem
= const_unop (code
, type
, arg0
);
9275 if (TREE_TYPE (tem
) != type
)
9276 tem
= fold_convert_loc (loc
, type
, tem
);
9282 tem
= generic_simplify (loc
, code
, type
, op0
);
9286 if (TREE_CODE_CLASS (code
) == tcc_unary
)
9288 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9289 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9290 fold_build1_loc (loc
, code
, type
,
9291 fold_convert_loc (loc
, TREE_TYPE (op0
),
9292 TREE_OPERAND (arg0
, 1))));
9293 else if (TREE_CODE (arg0
) == COND_EXPR
)
9295 tree arg01
= TREE_OPERAND (arg0
, 1);
9296 tree arg02
= TREE_OPERAND (arg0
, 2);
9297 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
9298 arg01
= fold_build1_loc (loc
, code
, type
,
9299 fold_convert_loc (loc
,
9300 TREE_TYPE (op0
), arg01
));
9301 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
9302 arg02
= fold_build1_loc (loc
, code
, type
,
9303 fold_convert_loc (loc
,
9304 TREE_TYPE (op0
), arg02
));
9305 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9308 /* If this was a conversion, and all we did was to move into
9309 inside the COND_EXPR, bring it back out. But leave it if
9310 it is a conversion from integer to integer and the
9311 result precision is no wider than a word since such a
9312 conversion is cheap and may be optimized away by combine,
9313 while it couldn't if it were outside the COND_EXPR. Then return
9314 so we don't get into an infinite recursion loop taking the
9315 conversion out and then back in. */
9317 if ((CONVERT_EXPR_CODE_P (code
)
9318 || code
== NON_LVALUE_EXPR
)
9319 && TREE_CODE (tem
) == COND_EXPR
9320 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
9321 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
9322 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 1)))
9323 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem
, 2)))
9324 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
9325 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
9326 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9328 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
9329 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
9330 || flag_syntax_only
))
9331 tem
= build1_loc (loc
, code
, type
,
9333 TREE_TYPE (TREE_OPERAND
9334 (TREE_OPERAND (tem
, 1), 0)),
9335 TREE_OPERAND (tem
, 0),
9336 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
9337 TREE_OPERAND (TREE_OPERAND (tem
, 2),
9345 case NON_LVALUE_EXPR
:
9346 if (!maybe_lvalue_p (op0
))
9347 return fold_convert_loc (loc
, type
, op0
);
9352 case FIX_TRUNC_EXPR
:
9353 if (COMPARISON_CLASS_P (op0
))
9355 /* If we have (type) (a CMP b) and type is an integral type, return
9356 new expression involving the new type. Canonicalize
9357 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9359 Do not fold the result as that would not simplify further, also
9360 folding again results in recursions. */
9361 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
9362 return build2_loc (loc
, TREE_CODE (op0
), type
,
9363 TREE_OPERAND (op0
, 0),
9364 TREE_OPERAND (op0
, 1));
9365 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
9366 && TREE_CODE (type
) != VECTOR_TYPE
)
9367 return build3_loc (loc
, COND_EXPR
, type
, op0
,
9368 constant_boolean_node (true, type
),
9369 constant_boolean_node (false, type
));
9372 /* Handle (T *)&A.B.C for A being of type T and B and C
9373 living at offset zero. This occurs frequently in
9374 C++ upcasting and then accessing the base. */
9375 if (TREE_CODE (op0
) == ADDR_EXPR
9376 && POINTER_TYPE_P (type
)
9377 && handled_component_p (TREE_OPERAND (op0
, 0)))
9379 poly_int64 bitsize
, bitpos
;
9382 int unsignedp
, reversep
, volatilep
;
9384 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
9385 &offset
, &mode
, &unsignedp
, &reversep
,
9387 /* If the reference was to a (constant) zero offset, we can use
9388 the address of the base if it has the same base type
9389 as the result type and the pointer type is unqualified. */
9391 && known_eq (bitpos
, 0)
9392 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
9393 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
9394 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
9395 return fold_convert_loc (loc
, type
,
9396 build_fold_addr_expr_loc (loc
, base
));
9399 if (TREE_CODE (op0
) == MODIFY_EXPR
9400 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
9401 /* Detect assigning a bitfield. */
9402 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
9404 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
9406 /* Don't leave an assignment inside a conversion
9407 unless assigning a bitfield. */
9408 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
9409 /* First do the assignment, then return converted constant. */
9410 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
9411 suppress_warning (tem
/* What warning? */);
9412 TREE_USED (tem
) = 1;
9416 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9417 constants (if x has signed type, the sign bit cannot be set
9418 in c). This folds extension into the BIT_AND_EXPR.
9419 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9420 very likely don't have maximal range for their precision and this
9421 transformation effectively doesn't preserve non-maximal ranges. */
9422 if (TREE_CODE (type
) == INTEGER_TYPE
9423 && TREE_CODE (op0
) == BIT_AND_EXPR
9424 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
9426 tree and_expr
= op0
;
9427 tree and0
= TREE_OPERAND (and_expr
, 0);
9428 tree and1
= TREE_OPERAND (and_expr
, 1);
9431 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
9432 || (TYPE_PRECISION (type
)
9433 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
9435 else if (TYPE_PRECISION (TREE_TYPE (and1
))
9436 <= HOST_BITS_PER_WIDE_INT
9437 && tree_fits_uhwi_p (and1
))
9439 unsigned HOST_WIDE_INT cst
;
9441 cst
= tree_to_uhwi (and1
);
9442 cst
&= HOST_WIDE_INT_M1U
9443 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
9444 change
= (cst
== 0);
9446 && !flag_syntax_only
9447 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
9450 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
9451 and0
= fold_convert_loc (loc
, uns
, and0
);
9452 and1
= fold_convert_loc (loc
, uns
, and1
);
9457 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
9458 TREE_OVERFLOW (and1
));
9459 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9460 fold_convert_loc (loc
, type
, and0
), tem
);
9464 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9465 cast (T1)X will fold away. We assume that this happens when X itself
9467 if (POINTER_TYPE_P (type
)
9468 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9469 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
9471 tree arg00
= TREE_OPERAND (arg0
, 0);
9472 tree arg01
= TREE_OPERAND (arg0
, 1);
9474 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9475 when the pointed type needs higher alignment than
9476 the p+ first operand's pointed type. */
9478 && sanitize_flags_p (SANITIZE_ALIGNMENT
)
9479 && (min_align_of_type (TREE_TYPE (type
))
9480 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00
)))))
9483 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9484 when type is a reference type and arg00's type is not,
9485 because arg00 could be validly nullptr and if arg01 doesn't return,
9486 we don't want false positive binding of reference to nullptr. */
9487 if (TREE_CODE (type
) == REFERENCE_TYPE
9489 && sanitize_flags_p (SANITIZE_NULL
)
9490 && TREE_CODE (TREE_TYPE (arg00
)) != REFERENCE_TYPE
)
9493 arg00
= fold_convert_loc (loc
, type
, arg00
);
9494 return fold_build_pointer_plus_loc (loc
, arg00
, arg01
);
9497 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9498 of the same precision, and X is an integer type not narrower than
9499 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9500 if (INTEGRAL_TYPE_P (type
)
9501 && TREE_CODE (op0
) == BIT_NOT_EXPR
9502 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9503 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
9504 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
9506 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
9507 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9508 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
9509 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
9510 fold_convert_loc (loc
, type
, tem
));
9513 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9514 type of X and Y (integer types only). */
9515 if (INTEGRAL_TYPE_P (type
)
9516 && TREE_CODE (op0
) == MULT_EXPR
9517 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9518 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
))
9519 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
9520 || !sanitize_flags_p (SANITIZE_SI_OVERFLOW
)))
9522 /* Be careful not to introduce new overflows. */
9524 if (TYPE_OVERFLOW_WRAPS (type
))
9527 mult_type
= unsigned_type_for (type
);
9529 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
9531 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
9532 fold_convert_loc (loc
, mult_type
,
9533 TREE_OPERAND (op0
, 0)),
9534 fold_convert_loc (loc
, mult_type
,
9535 TREE_OPERAND (op0
, 1)));
9536 return fold_convert_loc (loc
, type
, tem
);
9542 case VIEW_CONVERT_EXPR
:
9543 if (TREE_CODE (op0
) == MEM_REF
)
9545 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
9546 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
9547 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
9548 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
9549 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
9556 tem
= fold_negate_expr (loc
, arg0
);
9558 return fold_convert_loc (loc
, type
, tem
);
9562 /* Convert fabs((double)float) into (double)fabsf(float). */
9563 if (TREE_CODE (arg0
) == NOP_EXPR
9564 && TREE_CODE (type
) == REAL_TYPE
)
9566 tree targ0
= strip_float_extensions (arg0
);
9568 return fold_convert_loc (loc
, type
,
9569 fold_build1_loc (loc
, ABS_EXPR
,
9576 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9577 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9578 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9579 fold_convert_loc (loc
, type
,
9580 TREE_OPERAND (arg0
, 0)))))
9581 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
9582 fold_convert_loc (loc
, type
,
9583 TREE_OPERAND (arg0
, 1)));
9584 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9585 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
9586 fold_convert_loc (loc
, type
,
9587 TREE_OPERAND (arg0
, 1)))))
9588 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
9589 fold_convert_loc (loc
, type
,
9590 TREE_OPERAND (arg0
, 0)), tem
);
9594 case TRUTH_NOT_EXPR
:
9595 /* Note that the operand of this must be an int
9596 and its values must be 0 or 1.
9597 ("true" is a fixed value perhaps depending on the language,
9598 but we don't handle values other than 1 correctly yet.) */
9599 tem
= fold_truth_not_expr (loc
, arg0
);
9602 return fold_convert_loc (loc
, type
, tem
);
9605 /* Fold *&X to X if X is an lvalue. */
9606 if (TREE_CODE (op0
) == ADDR_EXPR
)
9608 tree op00
= TREE_OPERAND (op0
, 0);
9610 || TREE_CODE (op00
) == PARM_DECL
9611 || TREE_CODE (op00
) == RESULT_DECL
)
9612 && !TREE_READONLY (op00
))
9619 } /* switch (code) */
9623 /* If the operation was a conversion do _not_ mark a resulting constant
9624 with TREE_OVERFLOW if the original constant was not. These conversions
9625 have implementation defined behavior and retaining the TREE_OVERFLOW
9626 flag here would confuse later passes such as VRP. */
9628 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
9629 tree type
, tree op0
)
9631 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
9633 && TREE_CODE (res
) == INTEGER_CST
9634 && TREE_CODE (op0
) == INTEGER_CST
9635 && CONVERT_EXPR_CODE_P (code
))
9636 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
9641 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9642 operands OP0 and OP1. LOC is the location of the resulting expression.
9643 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9644 Return the folded expression if folding is successful. Otherwise,
9645 return NULL_TREE. */
9647 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
9648 tree arg0
, tree arg1
, tree op0
, tree op1
)
9652 /* We only do these simplifications if we are optimizing. */
9656 /* Check for things like (A || B) && (A || C). We can convert this
9657 to A || (B && C). Note that either operator can be any of the four
9658 truth and/or operations and the transformation will still be
9659 valid. Also note that we only care about order for the
9660 ANDIF and ORIF operators. If B contains side effects, this
9661 might change the truth-value of A. */
9662 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9663 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
9664 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
9665 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
9666 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
9667 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
9669 tree a00
= TREE_OPERAND (arg0
, 0);
9670 tree a01
= TREE_OPERAND (arg0
, 1);
9671 tree a10
= TREE_OPERAND (arg1
, 0);
9672 tree a11
= TREE_OPERAND (arg1
, 1);
9673 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
9674 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
9675 && (code
== TRUTH_AND_EXPR
9676 || code
== TRUTH_OR_EXPR
));
9678 if (operand_equal_p (a00
, a10
, 0))
9679 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9680 fold_build2_loc (loc
, code
, type
, a01
, a11
));
9681 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
9682 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
9683 fold_build2_loc (loc
, code
, type
, a01
, a10
));
9684 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
9685 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
9686 fold_build2_loc (loc
, code
, type
, a00
, a11
));
9688 /* This case if tricky because we must either have commutative
9689 operators or else A10 must not have side-effects. */
9691 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
9692 && operand_equal_p (a01
, a11
, 0))
9693 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
9694 fold_build2_loc (loc
, code
, type
, a00
, a10
),
9698 /* See if we can build a range comparison. */
9699 if ((tem
= fold_range_test (loc
, code
, type
, op0
, op1
)) != 0)
9702 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
9703 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
9705 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
9707 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
9710 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
9711 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
9713 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
9715 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
9718 /* Check for the possibility of merging component references. If our
9719 lhs is another similar operation, try to merge its rhs with our
9720 rhs. Then try to merge our lhs and rhs. */
9721 if (TREE_CODE (arg0
) == code
9722 && (tem
= fold_truth_andor_1 (loc
, code
, type
,
9723 TREE_OPERAND (arg0
, 1), arg1
)) != 0)
9724 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9726 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
9729 bool logical_op_non_short_circuit
= LOGICAL_OP_NON_SHORT_CIRCUIT
;
9730 if (param_logical_op_non_short_circuit
!= -1)
9731 logical_op_non_short_circuit
9732 = param_logical_op_non_short_circuit
;
9733 if (logical_op_non_short_circuit
9734 && !sanitize_coverage_p ()
9735 && (code
== TRUTH_AND_EXPR
9736 || code
== TRUTH_ANDIF_EXPR
9737 || code
== TRUTH_OR_EXPR
9738 || code
== TRUTH_ORIF_EXPR
))
9740 enum tree_code ncode
, icode
;
9742 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
9743 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
9744 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
9746 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9747 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9748 We don't want to pack more than two leafs to a non-IF AND/OR
9750 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9751 equal to IF-CODE, then we don't want to add right-hand operand.
9752 If the inner right-hand side of left-hand operand has
9753 side-effects, or isn't simple, then we can't add to it,
9754 as otherwise we might destroy if-sequence. */
9755 if (TREE_CODE (arg0
) == icode
9756 && simple_condition_p (arg1
)
9757 /* Needed for sequence points to handle trappings, and
9759 && simple_condition_p (TREE_OPERAND (arg0
, 1)))
9761 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
9763 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
9766 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9767 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9768 else if (TREE_CODE (arg1
) == icode
9769 && simple_condition_p (arg0
)
9770 /* Needed for sequence points to handle trappings, and
9772 && simple_condition_p (TREE_OPERAND (arg1
, 0)))
9774 tem
= fold_build2_loc (loc
, ncode
, type
,
9775 arg0
, TREE_OPERAND (arg1
, 0));
9776 return fold_build2_loc (loc
, icode
, type
, tem
,
9777 TREE_OPERAND (arg1
, 1));
9779 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9781 For sequence point consistancy, we need to check for trapping,
9782 and side-effects. */
9783 else if (code
== icode
&& simple_condition_p (arg0
)
9784 && simple_condition_p (arg1
))
9785 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
9791 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9792 by changing CODE to reduce the magnitude of constants involved in
9793 ARG0 of the comparison.
9794 Returns a canonicalized comparison tree if a simplification was
9795 possible, otherwise returns NULL_TREE.
9796 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9797 valid if signed overflow is undefined. */
9800 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
9801 tree arg0
, tree arg1
,
9802 bool *strict_overflow_p
)
9804 enum tree_code code0
= TREE_CODE (arg0
);
9805 tree t
, cst0
= NULL_TREE
;
9808 /* Match A +- CST code arg1. We can change this only if overflow
9810 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9811 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
9812 /* In principle pointers also have undefined overflow behavior,
9813 but that causes problems elsewhere. */
9814 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
9815 && (code0
== MINUS_EXPR
9816 || code0
== PLUS_EXPR
)
9817 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
9820 /* Identify the constant in arg0 and its sign. */
9821 cst0
= TREE_OPERAND (arg0
, 1);
9822 sgn0
= tree_int_cst_sgn (cst0
);
9824 /* Overflowed constants and zero will cause problems. */
9825 if (integer_zerop (cst0
)
9826 || TREE_OVERFLOW (cst0
))
9829 /* See if we can reduce the magnitude of the constant in
9830 arg0 by changing the comparison code. */
9831 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9833 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9835 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9836 else if (code
== GT_EXPR
9837 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9839 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9840 else if (code
== LE_EXPR
9841 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
9843 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9844 else if (code
== GE_EXPR
9845 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
9849 *strict_overflow_p
= true;
9851 /* Now build the constant reduced in magnitude. But not if that
9852 would produce one outside of its types range. */
9853 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
9855 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
9856 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
9858 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
9859 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
9862 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
9863 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
9864 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
9865 t
= fold_convert (TREE_TYPE (arg1
), t
);
9867 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
9870 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9871 overflow further. Try to decrease the magnitude of constants involved
9872 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9873 and put sole constants at the second argument position.
9874 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9877 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
9878 tree arg0
, tree arg1
)
9881 bool strict_overflow_p
;
9882 const char * const warnmsg
= G_("assuming signed overflow does not occur "
9883 "when reducing constant in comparison");
9885 /* Try canonicalization by simplifying arg0. */
9886 strict_overflow_p
= false;
9887 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
9888 &strict_overflow_p
);
9891 if (strict_overflow_p
)
9892 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9896 /* Try canonicalization by simplifying arg1 using the swapped
9898 code
= swap_tree_comparison (code
);
9899 strict_overflow_p
= false;
9900 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
9901 &strict_overflow_p
);
9902 if (t
&& strict_overflow_p
)
9903 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
9907 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9908 space. This is used to avoid issuing overflow warnings for
9909 expressions like &p->x which cannot wrap. */
9912 pointer_may_wrap_p (tree base
, tree offset
, poly_int64 bitpos
)
9914 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
9917 if (maybe_lt (bitpos
, 0))
9920 poly_wide_int wi_offset
;
9921 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
9922 if (offset
== NULL_TREE
)
9923 wi_offset
= wi::zero (precision
);
9924 else if (!poly_int_tree_p (offset
) || TREE_OVERFLOW (offset
))
9927 wi_offset
= wi::to_poly_wide (offset
);
9929 wi::overflow_type overflow
;
9930 poly_wide_int units
= wi::shwi (bits_to_bytes_round_down (bitpos
),
9932 poly_wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
9936 poly_uint64 total_hwi
, size
;
9937 if (!total
.to_uhwi (&total_hwi
)
9938 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base
))),
9940 || known_eq (size
, 0U))
9943 if (known_le (total_hwi
, size
))
9946 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9948 if (TREE_CODE (base
) == ADDR_EXPR
9949 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base
, 0))),
9951 && maybe_ne (size
, 0U)
9952 && known_le (total_hwi
, size
))
9958 /* Return a positive integer when the symbol DECL is known to have
9959 a nonzero address, zero when it's known not to (e.g., it's a weak
9960 symbol), and a negative integer when the symbol is not yet in the
9961 symbol table and so whether or not its address is zero is unknown.
9962 For function local objects always return positive integer. */
9964 maybe_nonzero_address (tree decl
)
9966 /* Normally, don't do anything for variables and functions before symtab is
9967 built; it is quite possible that DECL will be declared weak later.
9968 But if folding_initializer, we need a constant answer now, so create
9969 the symtab entry and prevent later weak declaration. */
9970 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
9971 if (struct symtab_node
*symbol
9972 = (folding_initializer
9973 ? symtab_node::get_create (decl
)
9974 : symtab_node::get (decl
)))
9975 return symbol
->nonzero_address ();
9977 /* Function local objects are never NULL. */
9979 && (DECL_CONTEXT (decl
)
9980 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
9981 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
9987 /* Subroutine of fold_binary. This routine performs all of the
9988 transformations that are common to the equality/inequality
9989 operators (EQ_EXPR and NE_EXPR) and the ordering operators
9990 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9991 fold_binary should call fold_binary. Fold a comparison with
9992 tree code CODE and type TYPE with operands OP0 and OP1. Return
9993 the folded comparison or NULL_TREE. */
9996 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
9999 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
10000 tree arg0
, arg1
, tem
;
10005 STRIP_SIGN_NOPS (arg0
);
10006 STRIP_SIGN_NOPS (arg1
);
10008 /* For comparisons of pointers we can decompose it to a compile time
10009 comparison of the base objects and the offsets into the object.
10010 This requires at least one operand being an ADDR_EXPR or a
10011 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10012 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
10013 && (TREE_CODE (arg0
) == ADDR_EXPR
10014 || TREE_CODE (arg1
) == ADDR_EXPR
10015 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10016 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
10018 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
10019 poly_int64 bitsize
, bitpos0
= 0, bitpos1
= 0;
10021 int volatilep
, reversep
, unsignedp
;
10022 bool indirect_base0
= false, indirect_base1
= false;
10024 /* Get base and offset for the access. Strip ADDR_EXPR for
10025 get_inner_reference, but put it back by stripping INDIRECT_REF
10026 off the base object if possible. indirect_baseN will be true
10027 if baseN is not an address but refers to the object itself. */
10029 if (TREE_CODE (arg0
) == ADDR_EXPR
)
10032 = get_inner_reference (TREE_OPERAND (arg0
, 0),
10033 &bitsize
, &bitpos0
, &offset0
, &mode
,
10034 &unsignedp
, &reversep
, &volatilep
);
10035 if (INDIRECT_REF_P (base0
))
10036 base0
= TREE_OPERAND (base0
, 0);
10038 indirect_base0
= true;
10040 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10042 base0
= TREE_OPERAND (arg0
, 0);
10043 STRIP_SIGN_NOPS (base0
);
10044 if (TREE_CODE (base0
) == ADDR_EXPR
)
10047 = get_inner_reference (TREE_OPERAND (base0
, 0),
10048 &bitsize
, &bitpos0
, &offset0
, &mode
,
10049 &unsignedp
, &reversep
, &volatilep
);
10050 if (INDIRECT_REF_P (base0
))
10051 base0
= TREE_OPERAND (base0
, 0);
10053 indirect_base0
= true;
10055 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
10056 offset0
= TREE_OPERAND (arg0
, 1);
10058 offset0
= size_binop (PLUS_EXPR
, offset0
,
10059 TREE_OPERAND (arg0
, 1));
10060 if (poly_int_tree_p (offset0
))
10062 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset0
),
10063 TYPE_PRECISION (sizetype
));
10064 tem
<<= LOG2_BITS_PER_UNIT
;
10066 if (tem
.to_shwi (&bitpos0
))
10067 offset0
= NULL_TREE
;
10072 if (TREE_CODE (arg1
) == ADDR_EXPR
)
10075 = get_inner_reference (TREE_OPERAND (arg1
, 0),
10076 &bitsize
, &bitpos1
, &offset1
, &mode
,
10077 &unsignedp
, &reversep
, &volatilep
);
10078 if (INDIRECT_REF_P (base1
))
10079 base1
= TREE_OPERAND (base1
, 0);
10081 indirect_base1
= true;
10083 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10085 base1
= TREE_OPERAND (arg1
, 0);
10086 STRIP_SIGN_NOPS (base1
);
10087 if (TREE_CODE (base1
) == ADDR_EXPR
)
10090 = get_inner_reference (TREE_OPERAND (base1
, 0),
10091 &bitsize
, &bitpos1
, &offset1
, &mode
,
10092 &unsignedp
, &reversep
, &volatilep
);
10093 if (INDIRECT_REF_P (base1
))
10094 base1
= TREE_OPERAND (base1
, 0);
10096 indirect_base1
= true;
10098 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
10099 offset1
= TREE_OPERAND (arg1
, 1);
10101 offset1
= size_binop (PLUS_EXPR
, offset1
,
10102 TREE_OPERAND (arg1
, 1));
10103 if (poly_int_tree_p (offset1
))
10105 poly_offset_int tem
= wi::sext (wi::to_poly_offset (offset1
),
10106 TYPE_PRECISION (sizetype
));
10107 tem
<<= LOG2_BITS_PER_UNIT
;
10109 if (tem
.to_shwi (&bitpos1
))
10110 offset1
= NULL_TREE
;
10114 /* If we have equivalent bases we might be able to simplify. */
10115 if (indirect_base0
== indirect_base1
10116 && operand_equal_p (base0
, base1
,
10117 indirect_base0
? OEP_ADDRESS_OF
: 0))
10119 /* We can fold this expression to a constant if the non-constant
10120 offset parts are equal. */
10121 if ((offset0
== offset1
10122 || (offset0
&& offset1
10123 && operand_equal_p (offset0
, offset1
, 0)))
10126 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10127 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10130 && maybe_ne (bitpos0
, bitpos1
)
10131 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10132 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10133 fold_overflow_warning (("assuming pointer wraparound does not "
10134 "occur when comparing P +- C1 with "
10136 WARN_STRICT_OVERFLOW_CONDITIONAL
);
10141 if (known_eq (bitpos0
, bitpos1
))
10142 return constant_boolean_node (true, type
);
10143 if (known_ne (bitpos0
, bitpos1
))
10144 return constant_boolean_node (false, type
);
10147 if (known_ne (bitpos0
, bitpos1
))
10148 return constant_boolean_node (true, type
);
10149 if (known_eq (bitpos0
, bitpos1
))
10150 return constant_boolean_node (false, type
);
10153 if (known_lt (bitpos0
, bitpos1
))
10154 return constant_boolean_node (true, type
);
10155 if (known_ge (bitpos0
, bitpos1
))
10156 return constant_boolean_node (false, type
);
10159 if (known_le (bitpos0
, bitpos1
))
10160 return constant_boolean_node (true, type
);
10161 if (known_gt (bitpos0
, bitpos1
))
10162 return constant_boolean_node (false, type
);
10165 if (known_ge (bitpos0
, bitpos1
))
10166 return constant_boolean_node (true, type
);
10167 if (known_lt (bitpos0
, bitpos1
))
10168 return constant_boolean_node (false, type
);
10171 if (known_gt (bitpos0
, bitpos1
))
10172 return constant_boolean_node (true, type
);
10173 if (known_le (bitpos0
, bitpos1
))
10174 return constant_boolean_node (false, type
);
10179 /* We can simplify the comparison to a comparison of the variable
10180 offset parts if the constant offset parts are equal.
10181 Be careful to use signed sizetype here because otherwise we
10182 mess with array offsets in the wrong way. This is possible
10183 because pointer arithmetic is restricted to retain within an
10184 object and overflow on pointer differences is undefined as of
10185 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10186 else if (known_eq (bitpos0
, bitpos1
)
10189 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
10190 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
10192 /* By converting to signed sizetype we cover middle-end pointer
10193 arithmetic which operates on unsigned pointer types of size
10194 type size and ARRAY_REF offsets which are properly sign or
10195 zero extended from their type in case it is narrower than
10197 if (offset0
== NULL_TREE
)
10198 offset0
= build_int_cst (ssizetype
, 0);
10200 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
10201 if (offset1
== NULL_TREE
)
10202 offset1
= build_int_cst (ssizetype
, 0);
10204 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
10207 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
10208 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
10209 fold_overflow_warning (("assuming pointer wraparound does not "
10210 "occur when comparing P +- C1 with "
10212 WARN_STRICT_OVERFLOW_COMPARISON
);
10214 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
10217 /* For equal offsets we can simplify to a comparison of the
10219 else if (known_eq (bitpos0
, bitpos1
)
10221 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
10223 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
10224 && ((offset0
== offset1
)
10225 || (offset0
&& offset1
10226 && operand_equal_p (offset0
, offset1
, 0))))
10228 if (indirect_base0
)
10229 base0
= build_fold_addr_expr_loc (loc
, base0
);
10230 if (indirect_base1
)
10231 base1
= build_fold_addr_expr_loc (loc
, base1
);
10232 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
10234 /* Comparison between an ordinary (non-weak) symbol and a null
10235 pointer can be eliminated since such symbols must have a non
10236 null address. In C, relational expressions between pointers
10237 to objects and null pointers are undefined. The results
10238 below follow the C++ rules with the additional property that
10239 every object pointer compares greater than a null pointer.
10241 else if (((DECL_P (base0
)
10242 && maybe_nonzero_address (base0
) > 0
10243 /* Avoid folding references to struct members at offset 0 to
10244 prevent tests like '&ptr->firstmember == 0' from getting
10245 eliminated. When ptr is null, although the -> expression
10246 is strictly speaking invalid, GCC retains it as a matter
10247 of QoI. See PR c/44555. */
10248 && (offset0
== NULL_TREE
&& known_ne (bitpos0
, 0)))
10249 || CONSTANT_CLASS_P (base0
))
10251 /* The caller guarantees that when one of the arguments is
10252 constant (i.e., null in this case) it is second. */
10253 && integer_zerop (arg1
))
10260 return constant_boolean_node (false, type
);
10264 return constant_boolean_node (true, type
);
10266 gcc_unreachable ();
10271 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10272 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10273 the resulting offset is smaller in absolute value than the
10274 original one and has the same sign. */
10275 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10276 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
10277 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10278 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10279 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
10280 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
10281 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10282 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
10284 tree const1
= TREE_OPERAND (arg0
, 1);
10285 tree const2
= TREE_OPERAND (arg1
, 1);
10286 tree variable1
= TREE_OPERAND (arg0
, 0);
10287 tree variable2
= TREE_OPERAND (arg1
, 0);
10289 const char * const warnmsg
= G_("assuming signed overflow does not "
10290 "occur when combining constants around "
10293 /* Put the constant on the side where it doesn't overflow and is
10294 of lower absolute value and of same sign than before. */
10295 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10296 ? MINUS_EXPR
: PLUS_EXPR
,
10298 if (!TREE_OVERFLOW (cst
)
10299 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
10300 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
10302 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10303 return fold_build2_loc (loc
, code
, type
,
10305 fold_build2_loc (loc
, TREE_CODE (arg1
),
10310 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
10311 ? MINUS_EXPR
: PLUS_EXPR
,
10313 if (!TREE_OVERFLOW (cst
)
10314 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
10315 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
10317 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
10318 return fold_build2_loc (loc
, code
, type
,
10319 fold_build2_loc (loc
, TREE_CODE (arg0
),
10326 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
10330 /* If we are comparing an expression that just has comparisons
10331 of two integer values, arithmetic expressions of those comparisons,
10332 and constants, we can simplify it. There are only three cases
10333 to check: the two values can either be equal, the first can be
10334 greater, or the second can be greater. Fold the expression for
10335 those three values. Since each value must be 0 or 1, we have
10336 eight possibilities, each of which corresponds to the constant 0
10337 or 1 or one of the six possible comparisons.
10339 This handles common cases like (a > b) == 0 but also handles
10340 expressions like ((x > y) - (y > x)) > 0, which supposedly
10341 occur in macroized code. */
10343 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
10345 tree cval1
= 0, cval2
= 0;
10347 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
10348 /* Don't handle degenerate cases here; they should already
10349 have been handled anyway. */
10350 && cval1
!= 0 && cval2
!= 0
10351 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
10352 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
10353 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
10354 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
10355 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
10356 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
10357 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
10359 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
10360 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
10362 /* We can't just pass T to eval_subst in case cval1 or cval2
10363 was the same as ARG1. */
10366 = fold_build2_loc (loc
, code
, type
,
10367 eval_subst (loc
, arg0
, cval1
, maxval
,
10371 = fold_build2_loc (loc
, code
, type
,
10372 eval_subst (loc
, arg0
, cval1
, maxval
,
10376 = fold_build2_loc (loc
, code
, type
,
10377 eval_subst (loc
, arg0
, cval1
, minval
,
10381 /* All three of these results should be 0 or 1. Confirm they are.
10382 Then use those values to select the proper code to use. */
10384 if (TREE_CODE (high_result
) == INTEGER_CST
10385 && TREE_CODE (equal_result
) == INTEGER_CST
10386 && TREE_CODE (low_result
) == INTEGER_CST
)
10388 /* Make a 3-bit mask with the high-order bit being the
10389 value for `>', the next for '=', and the low for '<'. */
10390 switch ((integer_onep (high_result
) * 4)
10391 + (integer_onep (equal_result
) * 2)
10392 + integer_onep (low_result
))
10395 /* Always false. */
10396 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10417 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10420 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
10429 /* Subroutine of fold_binary. Optimize complex multiplications of the
10430 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10431 argument EXPR represents the expression "z" of type TYPE. */
10434 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
10436 tree itype
= TREE_TYPE (type
);
10437 tree rpart
, ipart
, tem
;
10439 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
10441 rpart
= TREE_OPERAND (expr
, 0);
10442 ipart
= TREE_OPERAND (expr
, 1);
10444 else if (TREE_CODE (expr
) == COMPLEX_CST
)
10446 rpart
= TREE_REALPART (expr
);
10447 ipart
= TREE_IMAGPART (expr
);
10451 expr
= save_expr (expr
);
10452 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
10453 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
10456 rpart
= save_expr (rpart
);
10457 ipart
= save_expr (ipart
);
10458 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
10459 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
10460 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
10461 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
10462 build_zero_cst (itype
));
10466 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10467 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10468 true if successful. */
10471 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
10473 unsigned HOST_WIDE_INT i
, nunits
;
10475 if (TREE_CODE (arg
) == VECTOR_CST
10476 && VECTOR_CST_NELTS (arg
).is_constant (&nunits
))
10478 for (i
= 0; i
< nunits
; ++i
)
10479 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
10481 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
10483 constructor_elt
*elt
;
10485 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
10486 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
10489 elts
[i
] = elt
->value
;
10493 for (; i
< nelts
; i
++)
10495 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
10499 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10500 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10501 NULL_TREE otherwise. */
10504 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const vec_perm_indices
&sel
)
10507 unsigned HOST_WIDE_INT nelts
;
10508 bool need_ctor
= false;
10510 if (!sel
.length ().is_constant (&nelts
))
10512 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type
), nelts
)
10513 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)), nelts
)
10514 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)), nelts
));
10515 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
10516 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
10519 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
10520 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
10521 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
10524 tree_vector_builder
out_elts (type
, nelts
, 1);
10525 for (i
= 0; i
< nelts
; i
++)
10527 HOST_WIDE_INT index
;
10528 if (!sel
[i
].is_constant (&index
))
10530 if (!CONSTANT_CLASS_P (in_elts
[index
]))
10532 out_elts
.quick_push (unshare_expr (in_elts
[index
]));
10537 vec
<constructor_elt
, va_gc
> *v
;
10538 vec_alloc (v
, nelts
);
10539 for (i
= 0; i
< nelts
; i
++)
10540 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
10541 return build_constructor (type
, v
);
10544 return out_elts
.build ();
10547 /* Try to fold a pointer difference of type TYPE two address expressions of
10548 array references AREF0 and AREF1 using location LOC. Return a
10549 simplified expression for the difference or NULL_TREE. */
10552 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
10553 tree aref0
, tree aref1
,
10554 bool use_pointer_diff
)
10556 tree base0
= TREE_OPERAND (aref0
, 0);
10557 tree base1
= TREE_OPERAND (aref1
, 0);
10558 tree base_offset
= build_int_cst (type
, 0);
10560 /* If the bases are array references as well, recurse. If the bases
10561 are pointer indirections compute the difference of the pointers.
10562 If the bases are equal, we are set. */
10563 if ((TREE_CODE (base0
) == ARRAY_REF
10564 && TREE_CODE (base1
) == ARRAY_REF
10566 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
,
10567 use_pointer_diff
)))
10568 || (INDIRECT_REF_P (base0
)
10569 && INDIRECT_REF_P (base1
)
10572 ? fold_binary_loc (loc
, POINTER_DIFF_EXPR
, type
,
10573 TREE_OPERAND (base0
, 0),
10574 TREE_OPERAND (base1
, 0))
10575 : fold_binary_loc (loc
, MINUS_EXPR
, type
,
10576 fold_convert (type
,
10577 TREE_OPERAND (base0
, 0)),
10578 fold_convert (type
,
10579 TREE_OPERAND (base1
, 0)))))
10580 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
10582 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
10583 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
10584 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
10585 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
10586 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10588 fold_build2_loc (loc
, MULT_EXPR
, type
,
10594 /* If the real or vector real constant CST of type TYPE has an exact
10595 inverse, return it, else return NULL. */
10598 exact_inverse (tree type
, tree cst
)
10604 switch (TREE_CODE (cst
))
10607 r
= TREE_REAL_CST (cst
);
10609 if (exact_real_inverse (TYPE_MODE (type
), &r
))
10610 return build_real (type
, r
);
10616 unit_type
= TREE_TYPE (type
);
10617 mode
= TYPE_MODE (unit_type
);
10619 tree_vector_builder elts
;
10620 if (!elts
.new_unary_operation (type
, cst
, false))
10622 unsigned int count
= elts
.encoded_nelts ();
10623 for (unsigned int i
= 0; i
< count
; ++i
)
10625 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
10626 if (!exact_real_inverse (mode
, &r
))
10628 elts
.quick_push (build_real (unit_type
, r
));
10631 return elts
.build ();
10639 /* Mask out the tz least significant bits of X of type TYPE where
10640 tz is the number of trailing zeroes in Y. */
10642 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
10644 int tz
= wi::ctz (y
);
10646 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
10650 /* Return true when T is an address and is known to be nonzero.
10651 For floating point we further ensure that T is not denormal.
10652 Similar logic is present in nonzero_address in rtlanal.h.
10654 If the return value is based on the assumption that signed overflow
10655 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10656 change *STRICT_OVERFLOW_P. */
10659 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
10661 tree type
= TREE_TYPE (t
);
10662 enum tree_code code
;
10664 /* Doing something useful for floating point would need more work. */
10665 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
10668 code
= TREE_CODE (t
);
10669 switch (TREE_CODE_CLASS (code
))
10672 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10673 strict_overflow_p
);
10675 case tcc_comparison
:
10676 return tree_binary_nonzero_warnv_p (code
, type
,
10677 TREE_OPERAND (t
, 0),
10678 TREE_OPERAND (t
, 1),
10679 strict_overflow_p
);
10681 case tcc_declaration
:
10682 case tcc_reference
:
10683 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10691 case TRUTH_NOT_EXPR
:
10692 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
10693 strict_overflow_p
);
10695 case TRUTH_AND_EXPR
:
10696 case TRUTH_OR_EXPR
:
10697 case TRUTH_XOR_EXPR
:
10698 return tree_binary_nonzero_warnv_p (code
, type
,
10699 TREE_OPERAND (t
, 0),
10700 TREE_OPERAND (t
, 1),
10701 strict_overflow_p
);
10707 case WITH_SIZE_EXPR
:
10709 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
10711 case COMPOUND_EXPR
:
10714 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
10715 strict_overflow_p
);
10718 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
10719 strict_overflow_p
);
10723 tree fndecl
= get_callee_fndecl (t
);
10724 if (!fndecl
) return false;
10725 if (flag_delete_null_pointer_checks
&& !flag_check_new
10726 && DECL_IS_OPERATOR_NEW_P (fndecl
)
10727 && !TREE_NOTHROW (fndecl
))
10729 if (flag_delete_null_pointer_checks
10730 && lookup_attribute ("returns_nonnull",
10731 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
10733 return alloca_call_p (t
);
10742 /* Return true when T is an address and is known to be nonzero.
10743 Handle warnings about undefined signed overflow. */
10746 tree_expr_nonzero_p (tree t
)
10748 bool ret
, strict_overflow_p
;
10750 strict_overflow_p
= false;
10751 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
10752 if (strict_overflow_p
)
10753 fold_overflow_warning (("assuming signed overflow does not occur when "
10754 "determining that expression is always "
10756 WARN_STRICT_OVERFLOW_MISC
);
10760 /* Return true if T is known not to be equal to an integer W. */
10763 expr_not_equal_to (tree t
, const wide_int
&w
)
10766 switch (TREE_CODE (t
))
10769 return wi::to_wide (t
) != w
;
10772 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
10776 get_range_query (cfun
)->range_of_expr (vr
, t
);
10778 get_global_range_query ()->range_of_expr (vr
, t
);
10780 if (!vr
.undefined_p () && !vr
.contains_p (w
))
10782 /* If T has some known zero bits and W has any of those bits set,
10783 then T is known not to be equal to W. */
10784 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
10785 TYPE_PRECISION (TREE_TYPE (t
))), 0))
10794 /* Fold a binary expression of code CODE and type TYPE with operands
10795 OP0 and OP1. LOC is the location of the resulting expression.
10796 Return the folded expression if folding is successful. Otherwise,
10797 return NULL_TREE. */
10800 fold_binary_loc (location_t loc
, enum tree_code code
, tree type
,
10801 tree op0
, tree op1
)
10803 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
10804 tree arg0
, arg1
, tem
;
10805 tree t1
= NULL_TREE
;
10806 bool strict_overflow_p
;
10809 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
10810 && TREE_CODE_LENGTH (code
) == 2
10811 && op0
!= NULL_TREE
10812 && op1
!= NULL_TREE
);
10817 /* Strip any conversions that don't change the mode. This is
10818 safe for every expression, except for a comparison expression
10819 because its signedness is derived from its operands. So, in
10820 the latter case, only strip conversions that don't change the
10821 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10824 Note that this is done as an internal manipulation within the
10825 constant folder, in order to find the simplest representation
10826 of the arguments so that their form can be studied. In any
10827 cases, the appropriate type conversions should be put back in
10828 the tree that will get out of the constant folder. */
10830 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
10832 STRIP_SIGN_NOPS (arg0
);
10833 STRIP_SIGN_NOPS (arg1
);
10841 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10842 constant but we can't do arithmetic on them. */
10843 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
10845 tem
= const_binop (code
, type
, arg0
, arg1
);
10846 if (tem
!= NULL_TREE
)
10848 if (TREE_TYPE (tem
) != type
)
10849 tem
= fold_convert_loc (loc
, type
, tem
);
10854 /* If this is a commutative operation, and ARG0 is a constant, move it
10855 to ARG1 to reduce the number of tests below. */
10856 if (commutative_tree_code (code
)
10857 && tree_swap_operands_p (arg0
, arg1
))
10858 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
10860 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10861 to ARG1 to reduce the number of tests below. */
10862 if (kind
== tcc_comparison
10863 && tree_swap_operands_p (arg0
, arg1
))
10864 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
10866 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
10870 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10872 First check for cases where an arithmetic operation is applied to a
10873 compound, conditional, or comparison operation. Push the arithmetic
10874 operation inside the compound or conditional to see if any folding
10875 can then be done. Convert comparison to conditional for this purpose.
10876 The also optimizes non-constant cases that used to be done in
10879 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10880 one of the operands is a comparison and the other is a comparison, a
10881 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10882 code below would make the expression more complex. Change it to a
10883 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10884 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10886 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
10887 || code
== EQ_EXPR
|| code
== NE_EXPR
)
10888 && !VECTOR_TYPE_P (TREE_TYPE (arg0
))
10889 && ((truth_value_p (TREE_CODE (arg0
))
10890 && (truth_value_p (TREE_CODE (arg1
))
10891 || (TREE_CODE (arg1
) == BIT_AND_EXPR
10892 && integer_onep (TREE_OPERAND (arg1
, 1)))))
10893 || (truth_value_p (TREE_CODE (arg1
))
10894 && (truth_value_p (TREE_CODE (arg0
))
10895 || (TREE_CODE (arg0
) == BIT_AND_EXPR
10896 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
10898 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
10899 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
10902 fold_convert_loc (loc
, boolean_type_node
, arg0
),
10903 fold_convert_loc (loc
, boolean_type_node
, arg1
));
10905 if (code
== EQ_EXPR
)
10906 tem
= invert_truthvalue_loc (loc
, tem
);
10908 return fold_convert_loc (loc
, type
, tem
);
10911 if (TREE_CODE_CLASS (code
) == tcc_binary
10912 || TREE_CODE_CLASS (code
) == tcc_comparison
)
10914 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
10916 tem
= fold_build2_loc (loc
, code
, type
,
10917 fold_convert_loc (loc
, TREE_TYPE (op0
),
10918 TREE_OPERAND (arg0
, 1)), op1
);
10919 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10922 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
10924 tem
= fold_build2_loc (loc
, code
, type
, op0
,
10925 fold_convert_loc (loc
, TREE_TYPE (op1
),
10926 TREE_OPERAND (arg1
, 1)));
10927 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
10931 if (TREE_CODE (arg0
) == COND_EXPR
10932 || TREE_CODE (arg0
) == VEC_COND_EXPR
10933 || COMPARISON_CLASS_P (arg0
))
10935 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10937 /*cond_first_p=*/1);
10938 if (tem
!= NULL_TREE
)
10942 if (TREE_CODE (arg1
) == COND_EXPR
10943 || TREE_CODE (arg1
) == VEC_COND_EXPR
10944 || COMPARISON_CLASS_P (arg1
))
10946 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10948 /*cond_first_p=*/0);
10949 if (tem
!= NULL_TREE
)
10957 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10958 if (TREE_CODE (arg0
) == ADDR_EXPR
10959 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
10961 tree iref
= TREE_OPERAND (arg0
, 0);
10962 return fold_build2 (MEM_REF
, type
,
10963 TREE_OPERAND (iref
, 0),
10964 int_const_binop (PLUS_EXPR
, arg1
,
10965 TREE_OPERAND (iref
, 1)));
10968 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10969 if (TREE_CODE (arg0
) == ADDR_EXPR
10970 && handled_component_p (TREE_OPERAND (arg0
, 0)))
10973 poly_int64 coffset
;
10974 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
10978 return fold_build2 (MEM_REF
, type
,
10979 build1 (ADDR_EXPR
, TREE_TYPE (arg0
), base
),
10980 int_const_binop (PLUS_EXPR
, arg1
,
10981 size_int (coffset
)));
10986 case POINTER_PLUS_EXPR
:
10987 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10988 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10989 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
10990 return fold_convert_loc (loc
, type
,
10991 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
10992 fold_convert_loc (loc
, sizetype
,
10994 fold_convert_loc (loc
, sizetype
,
11000 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
11002 /* X + (X / CST) * -CST is X % CST. */
11003 if (TREE_CODE (arg1
) == MULT_EXPR
11004 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
11005 && operand_equal_p (arg0
,
11006 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
11008 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
11009 tree cst1
= TREE_OPERAND (arg1
, 1);
11010 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
11012 if (sum
&& integer_zerop (sum
))
11013 return fold_convert_loc (loc
, type
,
11014 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
11015 TREE_TYPE (arg0
), arg0
,
11020 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11021 one. Make sure the type is not saturating and has the signedness of
11022 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11023 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11024 if ((TREE_CODE (arg0
) == MULT_EXPR
11025 || TREE_CODE (arg1
) == MULT_EXPR
)
11026 && !TYPE_SATURATING (type
)
11027 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11028 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11029 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11031 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11036 if (! FLOAT_TYPE_P (type
))
11038 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11039 (plus (plus (mult) (mult)) (foo)) so that we can
11040 take advantage of the factoring cases below. */
11041 if (ANY_INTEGRAL_TYPE_P (type
)
11042 && TYPE_OVERFLOW_WRAPS (type
)
11043 && (((TREE_CODE (arg0
) == PLUS_EXPR
11044 || TREE_CODE (arg0
) == MINUS_EXPR
)
11045 && TREE_CODE (arg1
) == MULT_EXPR
)
11046 || ((TREE_CODE (arg1
) == PLUS_EXPR
11047 || TREE_CODE (arg1
) == MINUS_EXPR
)
11048 && TREE_CODE (arg0
) == MULT_EXPR
)))
11050 tree parg0
, parg1
, parg
, marg
;
11051 enum tree_code pcode
;
11053 if (TREE_CODE (arg1
) == MULT_EXPR
)
11054 parg
= arg0
, marg
= arg1
;
11056 parg
= arg1
, marg
= arg0
;
11057 pcode
= TREE_CODE (parg
);
11058 parg0
= TREE_OPERAND (parg
, 0);
11059 parg1
= TREE_OPERAND (parg
, 1);
11060 STRIP_NOPS (parg0
);
11061 STRIP_NOPS (parg1
);
11063 if (TREE_CODE (parg0
) == MULT_EXPR
11064 && TREE_CODE (parg1
) != MULT_EXPR
)
11065 return fold_build2_loc (loc
, pcode
, type
,
11066 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11067 fold_convert_loc (loc
, type
,
11069 fold_convert_loc (loc
, type
,
11071 fold_convert_loc (loc
, type
, parg1
));
11072 if (TREE_CODE (parg0
) != MULT_EXPR
11073 && TREE_CODE (parg1
) == MULT_EXPR
)
11075 fold_build2_loc (loc
, PLUS_EXPR
, type
,
11076 fold_convert_loc (loc
, type
, parg0
),
11077 fold_build2_loc (loc
, pcode
, type
,
11078 fold_convert_loc (loc
, type
, marg
),
11079 fold_convert_loc (loc
, type
,
11085 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11086 to __complex__ ( x, y ). This is not the same for SNaNs or
11087 if signed zeros are involved. */
11088 if (!HONOR_SNANS (arg0
)
11089 && !HONOR_SIGNED_ZEROS (arg0
)
11090 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11092 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11093 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11094 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11095 bool arg0rz
= false, arg0iz
= false;
11096 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11097 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11099 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11100 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11101 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11103 tree rp
= arg1r
? arg1r
11104 : build1 (REALPART_EXPR
, rtype
, arg1
);
11105 tree ip
= arg0i
? arg0i
11106 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11107 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11109 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11111 tree rp
= arg0r
? arg0r
11112 : build1 (REALPART_EXPR
, rtype
, arg0
);
11113 tree ip
= arg1i
? arg1i
11114 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
11115 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11120 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11121 We associate floats only if the user has specified
11122 -fassociative-math. */
11123 if (flag_associative_math
11124 && TREE_CODE (arg1
) == PLUS_EXPR
11125 && TREE_CODE (arg0
) != MULT_EXPR
)
11127 tree tree10
= TREE_OPERAND (arg1
, 0);
11128 tree tree11
= TREE_OPERAND (arg1
, 1);
11129 if (TREE_CODE (tree11
) == MULT_EXPR
11130 && TREE_CODE (tree10
) == MULT_EXPR
)
11133 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
11134 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
11137 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11138 We associate floats only if the user has specified
11139 -fassociative-math. */
11140 if (flag_associative_math
11141 && TREE_CODE (arg0
) == PLUS_EXPR
11142 && TREE_CODE (arg1
) != MULT_EXPR
)
11144 tree tree00
= TREE_OPERAND (arg0
, 0);
11145 tree tree01
= TREE_OPERAND (arg0
, 1);
11146 if (TREE_CODE (tree01
) == MULT_EXPR
11147 && TREE_CODE (tree00
) == MULT_EXPR
)
11150 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
11151 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
11157 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11158 is a rotate of A by C1 bits. */
11159 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11160 is a rotate of A by B bits.
11161 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11162 though in this case CODE must be | and not + or ^, otherwise
11163 it doesn't return A when B is 0. */
11165 enum tree_code code0
, code1
;
11167 code0
= TREE_CODE (arg0
);
11168 code1
= TREE_CODE (arg1
);
11169 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
11170 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
11171 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11172 TREE_OPERAND (arg1
, 0), 0)
11173 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
11174 TYPE_UNSIGNED (rtype
))
11175 /* Only create rotates in complete modes. Other cases are not
11176 expanded properly. */
11177 && (element_precision (rtype
)
11178 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
11180 tree tree01
, tree11
;
11181 tree orig_tree01
, orig_tree11
;
11182 enum tree_code code01
, code11
;
11184 tree01
= orig_tree01
= TREE_OPERAND (arg0
, 1);
11185 tree11
= orig_tree11
= TREE_OPERAND (arg1
, 1);
11186 STRIP_NOPS (tree01
);
11187 STRIP_NOPS (tree11
);
11188 code01
= TREE_CODE (tree01
);
11189 code11
= TREE_CODE (tree11
);
11190 if (code11
!= MINUS_EXPR
11191 && (code01
== MINUS_EXPR
|| code01
== BIT_AND_EXPR
))
11193 std::swap (code0
, code1
);
11194 std::swap (code01
, code11
);
11195 std::swap (tree01
, tree11
);
11196 std::swap (orig_tree01
, orig_tree11
);
11198 if (code01
== INTEGER_CST
11199 && code11
== INTEGER_CST
11200 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
11201 == element_precision (rtype
)))
11203 tem
= build2_loc (loc
, LROTATE_EXPR
,
11204 rtype
, TREE_OPERAND (arg0
, 0),
11205 code0
== LSHIFT_EXPR
11206 ? orig_tree01
: orig_tree11
);
11207 return fold_convert_loc (loc
, type
, tem
);
11209 else if (code11
== MINUS_EXPR
)
11211 tree tree110
, tree111
;
11212 tree110
= TREE_OPERAND (tree11
, 0);
11213 tree111
= TREE_OPERAND (tree11
, 1);
11214 STRIP_NOPS (tree110
);
11215 STRIP_NOPS (tree111
);
11216 if (TREE_CODE (tree110
) == INTEGER_CST
11217 && compare_tree_int (tree110
,
11218 element_precision (rtype
)) == 0
11219 && operand_equal_p (tree01
, tree111
, 0))
11221 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11222 ? LROTATE_EXPR
: RROTATE_EXPR
),
11223 rtype
, TREE_OPERAND (arg0
, 0),
11225 return fold_convert_loc (loc
, type
, tem
);
11228 else if (code
== BIT_IOR_EXPR
11229 && code11
== BIT_AND_EXPR
11230 && pow2p_hwi (element_precision (rtype
)))
11232 tree tree110
, tree111
;
11233 tree110
= TREE_OPERAND (tree11
, 0);
11234 tree111
= TREE_OPERAND (tree11
, 1);
11235 STRIP_NOPS (tree110
);
11236 STRIP_NOPS (tree111
);
11237 if (TREE_CODE (tree110
) == NEGATE_EXPR
11238 && TREE_CODE (tree111
) == INTEGER_CST
11239 && compare_tree_int (tree111
,
11240 element_precision (rtype
) - 1) == 0
11241 && operand_equal_p (tree01
, TREE_OPERAND (tree110
, 0), 0))
11243 tem
= build2_loc (loc
, (code0
== LSHIFT_EXPR
11244 ? LROTATE_EXPR
: RROTATE_EXPR
),
11245 rtype
, TREE_OPERAND (arg0
, 0),
11247 return fold_convert_loc (loc
, type
, tem
);
11254 /* In most languages, can't associate operations on floats through
11255 parentheses. Rather than remember where the parentheses were, we
11256 don't associate floats at all, unless the user has specified
11257 -fassociative-math.
11258 And, we need to make sure type is not saturating. */
11260 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
11261 && !TYPE_SATURATING (type
)
11262 && !TYPE_OVERFLOW_SANITIZED (type
))
11264 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
11265 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
11269 /* Split both trees into variables, constants, and literals. Then
11270 associate each group together, the constants with literals,
11271 then the result with variables. This increases the chances of
11272 literals being recombined later and of generating relocatable
11273 expressions for the sum of a constant and literal. */
11274 var0
= split_tree (arg0
, type
, code
,
11275 &minus_var0
, &con0
, &minus_con0
,
11276 &lit0
, &minus_lit0
, 0);
11277 var1
= split_tree (arg1
, type
, code
,
11278 &minus_var1
, &con1
, &minus_con1
,
11279 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
11281 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11282 if (code
== MINUS_EXPR
)
11285 /* With undefined overflow prefer doing association in a type
11286 which wraps on overflow, if that is one of the operand types. */
11287 if ((POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
11288 && !TYPE_OVERFLOW_WRAPS (type
))
11290 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11291 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11292 atype
= TREE_TYPE (arg0
);
11293 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
11294 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
11295 atype
= TREE_TYPE (arg1
);
11296 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
11299 /* With undefined overflow we can only associate constants with one
11300 variable, and constants whose association doesn't overflow. */
11301 if ((POINTER_TYPE_P (atype
) || INTEGRAL_TYPE_P (atype
))
11302 && !TYPE_OVERFLOW_WRAPS (atype
))
11304 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
11306 /* ??? If split_tree would handle NEGATE_EXPR we could
11307 simply reject these cases and the allowed cases would
11308 be the var0/minus_var1 ones. */
11309 tree tmp0
= var0
? var0
: minus_var0
;
11310 tree tmp1
= var1
? var1
: minus_var1
;
11311 bool one_neg
= false;
11313 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
11315 tmp0
= TREE_OPERAND (tmp0
, 0);
11316 one_neg
= !one_neg
;
11318 if (CONVERT_EXPR_P (tmp0
)
11319 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11320 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
11321 <= TYPE_PRECISION (atype
)))
11322 tmp0
= TREE_OPERAND (tmp0
, 0);
11323 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
11325 tmp1
= TREE_OPERAND (tmp1
, 0);
11326 one_neg
= !one_neg
;
11328 if (CONVERT_EXPR_P (tmp1
)
11329 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11330 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
11331 <= TYPE_PRECISION (atype
)))
11332 tmp1
= TREE_OPERAND (tmp1
, 0);
11333 /* The only case we can still associate with two variables
11334 is if they cancel out. */
11336 || !operand_equal_p (tmp0
, tmp1
, 0))
11339 else if ((var0
&& minus_var1
11340 && ! operand_equal_p (var0
, minus_var1
, 0))
11341 || (minus_var0
&& var1
11342 && ! operand_equal_p (minus_var0
, var1
, 0)))
11346 /* Only do something if we found more than two objects. Otherwise,
11347 nothing has changed and we risk infinite recursion. */
11349 && ((var0
!= 0) + (var1
!= 0)
11350 + (minus_var0
!= 0) + (minus_var1
!= 0)
11351 + (con0
!= 0) + (con1
!= 0)
11352 + (minus_con0
!= 0) + (minus_con1
!= 0)
11353 + (lit0
!= 0) + (lit1
!= 0)
11354 + (minus_lit0
!= 0) + (minus_lit1
!= 0)) > 2)
11356 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
11357 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
11359 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
11360 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
11362 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
11363 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
11366 if (minus_var0
&& var0
)
11368 var0
= associate_trees (loc
, var0
, minus_var0
,
11369 MINUS_EXPR
, atype
);
11372 if (minus_con0
&& con0
)
11374 con0
= associate_trees (loc
, con0
, minus_con0
,
11375 MINUS_EXPR
, atype
);
11379 /* Preserve the MINUS_EXPR if the negative part of the literal is
11380 greater than the positive part. Otherwise, the multiplicative
11381 folding code (i.e extract_muldiv) may be fooled in case
11382 unsigned constants are subtracted, like in the following
11383 example: ((X*2 + 4) - 8U)/2. */
11384 if (minus_lit0
&& lit0
)
11386 if (TREE_CODE (lit0
) == INTEGER_CST
11387 && TREE_CODE (minus_lit0
) == INTEGER_CST
11388 && tree_int_cst_lt (lit0
, minus_lit0
)
11389 /* But avoid ending up with only negated parts. */
11392 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
11393 MINUS_EXPR
, atype
);
11398 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
11399 MINUS_EXPR
, atype
);
11404 /* Don't introduce overflows through reassociation. */
11405 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
11406 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
11409 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11410 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
11412 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
11416 /* Eliminate minus_con0. */
11420 con0
= associate_trees (loc
, con0
, minus_con0
,
11421 MINUS_EXPR
, atype
);
11423 var0
= associate_trees (loc
, var0
, minus_con0
,
11424 MINUS_EXPR
, atype
);
11426 gcc_unreachable ();
11430 /* Eliminate minus_var0. */
11434 con0
= associate_trees (loc
, con0
, minus_var0
,
11435 MINUS_EXPR
, atype
);
11437 gcc_unreachable ();
11442 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
11449 case POINTER_DIFF_EXPR
:
11451 /* Fold &a[i] - &a[j] to i-j. */
11452 if (TREE_CODE (arg0
) == ADDR_EXPR
11453 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
11454 && TREE_CODE (arg1
) == ADDR_EXPR
11455 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
11457 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
11458 TREE_OPERAND (arg0
, 0),
11459 TREE_OPERAND (arg1
, 0),
11461 == POINTER_DIFF_EXPR
);
11466 /* Further transformations are not for pointers. */
11467 if (code
== POINTER_DIFF_EXPR
)
11470 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11471 if (TREE_CODE (arg0
) == NEGATE_EXPR
11472 && negate_expr_p (op1
)
11473 /* If arg0 is e.g. unsigned int and type is int, then this could
11474 introduce UB, because if A is INT_MIN at runtime, the original
11475 expression can be well defined while the latter is not.
11477 && !(ANY_INTEGRAL_TYPE_P (type
)
11478 && TYPE_OVERFLOW_UNDEFINED (type
)
11479 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11480 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
11481 return fold_build2_loc (loc
, MINUS_EXPR
, type
, negate_expr (op1
),
11482 fold_convert_loc (loc
, type
,
11483 TREE_OPERAND (arg0
, 0)));
11485 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11486 __complex__ ( x, -y ). This is not the same for SNaNs or if
11487 signed zeros are involved. */
11488 if (!HONOR_SNANS (arg0
)
11489 && !HONOR_SIGNED_ZEROS (arg0
)
11490 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11492 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11493 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
11494 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
11495 bool arg0rz
= false, arg0iz
= false;
11496 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
11497 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
11499 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
11500 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
11501 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
11503 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11505 : build1 (REALPART_EXPR
, rtype
, arg1
));
11506 tree ip
= arg0i
? arg0i
11507 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
11508 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11510 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
11512 tree rp
= arg0r
? arg0r
11513 : build1 (REALPART_EXPR
, rtype
, arg0
);
11514 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
11516 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
11517 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
11522 /* A - B -> A + (-B) if B is easily negatable. */
11523 if (negate_expr_p (op1
)
11524 && ! TYPE_OVERFLOW_SANITIZED (type
)
11525 && ((FLOAT_TYPE_P (type
)
11526 /* Avoid this transformation if B is a positive REAL_CST. */
11527 && (TREE_CODE (op1
) != REAL_CST
11528 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
11529 || INTEGRAL_TYPE_P (type
)))
11530 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11531 fold_convert_loc (loc
, type
, arg0
),
11532 negate_expr (op1
));
11534 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11535 one. Make sure the type is not saturating and has the signedness of
11536 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11537 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11538 if ((TREE_CODE (arg0
) == MULT_EXPR
11539 || TREE_CODE (arg1
) == MULT_EXPR
)
11540 && !TYPE_SATURATING (type
)
11541 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
11542 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
11543 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
11545 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
11553 if (! FLOAT_TYPE_P (type
))
11555 /* Transform x * -C into -x * C if x is easily negatable. */
11556 if (TREE_CODE (op1
) == INTEGER_CST
11557 && tree_int_cst_sgn (op1
) == -1
11558 && negate_expr_p (op0
)
11559 && negate_expr_p (op1
)
11560 && (tem
= negate_expr (op1
)) != op1
11561 && ! TREE_OVERFLOW (tem
))
11562 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11563 fold_convert_loc (loc
, type
,
11564 negate_expr (op0
)), tem
);
11566 strict_overflow_p
= false;
11567 if (TREE_CODE (arg1
) == INTEGER_CST
11568 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11569 &strict_overflow_p
)) != 0)
11571 if (strict_overflow_p
)
11572 fold_overflow_warning (("assuming signed overflow does not "
11573 "occur when simplifying "
11575 WARN_STRICT_OVERFLOW_MISC
);
11576 return fold_convert_loc (loc
, type
, tem
);
11579 /* Optimize z * conj(z) for integer complex numbers. */
11580 if (TREE_CODE (arg0
) == CONJ_EXPR
11581 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11582 return fold_mult_zconjz (loc
, type
, arg1
);
11583 if (TREE_CODE (arg1
) == CONJ_EXPR
11584 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11585 return fold_mult_zconjz (loc
, type
, arg0
);
11589 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11590 This is not the same for NaNs or if signed zeros are
11592 if (!HONOR_NANS (arg0
)
11593 && !HONOR_SIGNED_ZEROS (arg0
)
11594 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11595 && TREE_CODE (arg1
) == COMPLEX_CST
11596 && real_zerop (TREE_REALPART (arg1
)))
11598 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11599 if (real_onep (TREE_IMAGPART (arg1
)))
11601 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11602 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11604 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11605 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11607 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11608 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11609 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11613 /* Optimize z * conj(z) for floating point complex numbers.
11614 Guarded by flag_unsafe_math_optimizations as non-finite
11615 imaginary components don't produce scalar results. */
11616 if (flag_unsafe_math_optimizations
11617 && TREE_CODE (arg0
) == CONJ_EXPR
11618 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11619 return fold_mult_zconjz (loc
, type
, arg1
);
11620 if (flag_unsafe_math_optimizations
11621 && TREE_CODE (arg1
) == CONJ_EXPR
11622 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11623 return fold_mult_zconjz (loc
, type
, arg0
);
11628 /* Canonicalize (X & C1) | C2. */
11629 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11630 && TREE_CODE (arg1
) == INTEGER_CST
11631 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11633 int width
= TYPE_PRECISION (type
), w
;
11634 wide_int c1
= wi::to_wide (TREE_OPERAND (arg0
, 1));
11635 wide_int c2
= wi::to_wide (arg1
);
11637 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11638 if ((c1
& c2
) == c1
)
11639 return omit_one_operand_loc (loc
, type
, arg1
,
11640 TREE_OPERAND (arg0
, 0));
11642 wide_int msk
= wi::mask (width
, false,
11643 TYPE_PRECISION (TREE_TYPE (arg1
)));
11645 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11646 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
11648 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11649 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11652 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11653 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11654 mode which allows further optimizations. */
11657 wide_int c3
= wi::bit_and_not (c1
, c2
);
11658 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
11660 wide_int mask
= wi::mask (w
, false,
11661 TYPE_PRECISION (type
));
11662 if (((c1
| c2
) & mask
) == mask
11663 && wi::bit_and_not (c1
, mask
) == 0)
11672 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11673 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
11674 wide_int_to_tree (type
, c3
));
11675 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
11679 /* See if this can be simplified into a rotate first. If that
11680 is unsuccessful continue in the association code. */
11684 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11685 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11686 && INTEGRAL_TYPE_P (type
)
11687 && integer_onep (TREE_OPERAND (arg0
, 1))
11688 && integer_onep (arg1
))
11689 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11690 build_zero_cst (TREE_TYPE (arg0
)));
11692 /* See if this can be simplified into a rotate first. If that
11693 is unsuccessful continue in the association code. */
11697 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11698 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11699 && INTEGRAL_TYPE_P (type
)
11700 && integer_onep (TREE_OPERAND (arg0
, 1))
11701 && integer_onep (arg1
))
11704 tem
= TREE_OPERAND (arg0
, 0);
11705 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11706 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11708 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11709 build_zero_cst (TREE_TYPE (tem
)));
11711 /* Fold ~X & 1 as (X & 1) == 0. */
11712 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11713 && INTEGRAL_TYPE_P (type
)
11714 && integer_onep (arg1
))
11717 tem
= TREE_OPERAND (arg0
, 0);
11718 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11719 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11721 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11722 build_zero_cst (TREE_TYPE (tem
)));
11724 /* Fold !X & 1 as X == 0. */
11725 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11726 && integer_onep (arg1
))
11728 tem
= TREE_OPERAND (arg0
, 0);
11729 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11730 build_zero_cst (TREE_TYPE (tem
)));
11733 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11734 multiple of 1 << CST. */
11735 if (TREE_CODE (arg1
) == INTEGER_CST
)
11737 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
11738 wide_int ncst1
= -cst1
;
11739 if ((cst1
& ncst1
) == ncst1
11740 && multiple_of_p (type
, arg0
,
11741 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11742 return fold_convert_loc (loc
, type
, arg0
);
11745 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11747 if (TREE_CODE (arg1
) == INTEGER_CST
11748 && TREE_CODE (arg0
) == MULT_EXPR
11749 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11751 wi::tree_to_wide_ref warg1
= wi::to_wide (arg1
);
11753 = mask_with_tz (type
, warg1
, wi::to_wide (TREE_OPERAND (arg0
, 1)));
11756 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11758 else if (masked
!= warg1
)
11760 /* Avoid the transform if arg1 is a mask of some
11761 mode which allows further optimizations. */
11762 int pop
= wi::popcount (warg1
);
11763 if (!(pop
>= BITS_PER_UNIT
11765 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
11766 return fold_build2_loc (loc
, code
, type
, op0
,
11767 wide_int_to_tree (type
, masked
));
11771 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11772 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11773 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11775 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11777 wide_int mask
= wide_int::from (wi::to_wide (arg1
), prec
, UNSIGNED
);
11780 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11786 /* Don't touch a floating-point divide by zero unless the mode
11787 of the constant can represent infinity. */
11788 if (TREE_CODE (arg1
) == REAL_CST
11789 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
11790 && real_zerop (arg1
))
11793 /* (-A) / (-B) -> A / B */
11794 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
11795 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11796 TREE_OPERAND (arg0
, 0),
11797 negate_expr (arg1
));
11798 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11799 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11800 negate_expr (arg0
),
11801 TREE_OPERAND (arg1
, 0));
11804 case TRUNC_DIV_EXPR
:
11807 case FLOOR_DIV_EXPR
:
11808 /* Simplify A / (B << N) where A and B are positive and B is
11809 a power of 2, to A >> (N + log2(B)). */
11810 strict_overflow_p
= false;
11811 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11812 && (TYPE_UNSIGNED (type
)
11813 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
11815 tree sval
= TREE_OPERAND (arg1
, 0);
11816 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
11818 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
11819 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
11820 wi::exact_log2 (wi::to_wide (sval
)));
11822 if (strict_overflow_p
)
11823 fold_overflow_warning (("assuming signed overflow does not "
11824 "occur when simplifying A / (B << N)"),
11825 WARN_STRICT_OVERFLOW_MISC
);
11827 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
11829 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11830 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
11836 case ROUND_DIV_EXPR
:
11837 case CEIL_DIV_EXPR
:
11838 case EXACT_DIV_EXPR
:
11839 if (integer_zerop (arg1
))
11842 /* Convert -A / -B to A / B when the type is signed and overflow is
11844 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11845 && TREE_CODE (op0
) == NEGATE_EXPR
11846 && negate_expr_p (op1
))
11848 if (ANY_INTEGRAL_TYPE_P (type
))
11849 fold_overflow_warning (("assuming signed overflow does not occur "
11850 "when distributing negation across "
11852 WARN_STRICT_OVERFLOW_MISC
);
11853 return fold_build2_loc (loc
, code
, type
,
11854 fold_convert_loc (loc
, type
,
11855 TREE_OPERAND (arg0
, 0)),
11856 negate_expr (op1
));
11858 if ((!ANY_INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11859 && TREE_CODE (arg1
) == NEGATE_EXPR
11860 && negate_expr_p (op0
))
11862 if (ANY_INTEGRAL_TYPE_P (type
))
11863 fold_overflow_warning (("assuming signed overflow does not occur "
11864 "when distributing negation across "
11866 WARN_STRICT_OVERFLOW_MISC
);
11867 return fold_build2_loc (loc
, code
, type
,
11869 fold_convert_loc (loc
, type
,
11870 TREE_OPERAND (arg1
, 0)));
11873 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11874 operation, EXACT_DIV_EXPR.
11876 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11877 At one time others generated faster code, it's not clear if they do
11878 after the last round to changes to the DIV code in expmed.cc. */
11879 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
11880 && multiple_of_p (type
, arg0
, arg1
))
11881 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
11882 fold_convert (type
, arg0
),
11883 fold_convert (type
, arg1
));
11885 strict_overflow_p
= false;
11886 if (TREE_CODE (arg1
) == INTEGER_CST
11887 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11888 &strict_overflow_p
)) != 0)
11890 if (strict_overflow_p
)
11891 fold_overflow_warning (("assuming signed overflow does not occur "
11892 "when simplifying division"),
11893 WARN_STRICT_OVERFLOW_MISC
);
11894 return fold_convert_loc (loc
, type
, tem
);
11899 case CEIL_MOD_EXPR
:
11900 case FLOOR_MOD_EXPR
:
11901 case ROUND_MOD_EXPR
:
11902 case TRUNC_MOD_EXPR
:
11903 strict_overflow_p
= false;
11904 if (TREE_CODE (arg1
) == INTEGER_CST
11905 && (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11906 &strict_overflow_p
)) != 0)
11908 if (strict_overflow_p
)
11909 fold_overflow_warning (("assuming signed overflow does not occur "
11910 "when simplifying modulus"),
11911 WARN_STRICT_OVERFLOW_MISC
);
11912 return fold_convert_loc (loc
, type
, tem
);
11921 /* Since negative shift count is not well-defined,
11922 don't try to compute it in the compiler. */
11923 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
11926 prec
= element_precision (type
);
11928 /* If we have a rotate of a bit operation with the rotate count and
11929 the second operand of the bit operation both constant,
11930 permute the two operations. */
11931 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11932 && (TREE_CODE (arg0
) == BIT_AND_EXPR
11933 || TREE_CODE (arg0
) == BIT_IOR_EXPR
11934 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
11935 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11937 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11938 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11939 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
11940 fold_build2_loc (loc
, code
, type
,
11942 fold_build2_loc (loc
, code
, type
,
11946 /* Two consecutive rotates adding up to the some integer
11947 multiple of the precision of the type can be ignored. */
11948 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11949 && TREE_CODE (arg0
) == RROTATE_EXPR
11950 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11951 && wi::umod_trunc (wi::to_wide (arg1
)
11952 + wi::to_wide (TREE_OPERAND (arg0
, 1)),
11954 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11962 case TRUTH_ANDIF_EXPR
:
11963 /* Note that the operands of this must be ints
11964 and their values must be 0 or 1.
11965 ("true" is a fixed value perhaps depending on the language.) */
11966 /* If first arg is constant zero, return it. */
11967 if (integer_zerop (arg0
))
11968 return fold_convert_loc (loc
, type
, arg0
);
11970 case TRUTH_AND_EXPR
:
11971 /* If either arg is constant true, drop it. */
11972 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11973 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11974 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
11975 /* Preserve sequence points. */
11976 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
11977 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11978 /* If second arg is constant zero, result is zero, but first arg
11979 must be evaluated. */
11980 if (integer_zerop (arg1
))
11981 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11982 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11983 case will be handled here. */
11984 if (integer_zerop (arg0
))
11985 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
11987 /* !X && X is always false. */
11988 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11989 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11990 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
11991 /* X && !X is always false. */
11992 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11993 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11994 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11996 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11997 means A >= Y && A != MAX, but in this case we know that
12000 if (!TREE_SIDE_EFFECTS (arg0
)
12001 && !TREE_SIDE_EFFECTS (arg1
))
12003 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12004 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12005 return fold_convert (type
,
12006 fold_build2_loc (loc
, code
, TREE_TYPE (arg1
),
12009 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12010 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12011 return fold_convert (type
,
12012 fold_build2_loc (loc
, code
, TREE_TYPE (arg0
),
12016 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12022 case TRUTH_ORIF_EXPR
:
12023 /* Note that the operands of this must be ints
12024 and their values must be 0 or true.
12025 ("true" is a fixed value perhaps depending on the language.) */
12026 /* If first arg is constant true, return it. */
12027 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12028 return fold_convert_loc (loc
, type
, arg0
);
12030 case TRUTH_OR_EXPR
:
12031 /* If either arg is constant zero, drop it. */
12032 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12033 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12034 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12035 /* Preserve sequence points. */
12036 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12037 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12038 /* If second arg is constant true, result is true, but we must
12039 evaluate first arg. */
12040 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12041 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12042 /* Likewise for first arg, but note this only occurs here for
12044 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12045 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12047 /* !X || X is always true. */
12048 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12049 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12050 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12051 /* X || !X is always true. */
12052 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12053 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12054 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12056 /* (X && !Y) || (!X && Y) is X ^ Y */
12057 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12058 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12060 tree a0
, a1
, l0
, l1
, n0
, n1
;
12062 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12063 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12065 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12066 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12068 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12069 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12071 if ((operand_equal_p (n0
, a0
, 0)
12072 && operand_equal_p (n1
, a1
, 0))
12073 || (operand_equal_p (n0
, a1
, 0)
12074 && operand_equal_p (n1
, a0
, 0)))
12075 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12078 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12084 case TRUTH_XOR_EXPR
:
12085 /* If the second arg is constant zero, drop it. */
12086 if (integer_zerop (arg1
))
12087 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12088 /* If the second arg is constant true, this is a logical inversion. */
12089 if (integer_onep (arg1
))
12091 tem
= invert_truthvalue_loc (loc
, arg0
);
12092 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12094 /* Identical arguments cancel to zero. */
12095 if (operand_equal_p (arg0
, arg1
, 0))
12096 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12098 /* !X ^ X is always true. */
12099 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12100 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12101 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12103 /* X ^ !X is always true. */
12104 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12105 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12106 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12115 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12116 if (tem
!= NULL_TREE
)
12119 /* bool_var != 1 becomes !bool_var. */
12120 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12121 && code
== NE_EXPR
)
12122 return fold_convert_loc (loc
, type
,
12123 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12124 TREE_TYPE (arg0
), arg0
));
12126 /* bool_var == 0 becomes !bool_var. */
12127 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12128 && code
== EQ_EXPR
)
12129 return fold_convert_loc (loc
, type
,
12130 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12131 TREE_TYPE (arg0
), arg0
));
12133 /* !exp != 0 becomes !exp */
12134 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12135 && code
== NE_EXPR
)
12136 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12138 /* If this is an EQ or NE comparison with zero and ARG0 is
12139 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12140 two operations, but the latter can be done in one less insn
12141 on machines that have only two-operand insns or on which a
12142 constant cannot be the first operand. */
12143 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12144 && integer_zerop (arg1
))
12146 tree arg00
= TREE_OPERAND (arg0
, 0);
12147 tree arg01
= TREE_OPERAND (arg0
, 1);
12148 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12149 && integer_onep (TREE_OPERAND (arg00
, 0)))
12151 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12152 arg01
, TREE_OPERAND (arg00
, 1));
12153 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12154 build_one_cst (TREE_TYPE (arg0
)));
12155 return fold_build2_loc (loc
, code
, type
,
12156 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12159 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12160 && integer_onep (TREE_OPERAND (arg01
, 0)))
12162 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12163 arg00
, TREE_OPERAND (arg01
, 1));
12164 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12165 build_one_cst (TREE_TYPE (arg0
)));
12166 return fold_build2_loc (loc
, code
, type
,
12167 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12172 /* If this is a comparison of a field, we may be able to simplify it. */
12173 if ((TREE_CODE (arg0
) == COMPONENT_REF
12174 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12175 /* Handle the constant case even without -O
12176 to make sure the warnings are given. */
12177 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12179 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12184 /* Optimize comparisons of strlen vs zero to a compare of the
12185 first character of the string vs zero. To wit,
12186 strlen(ptr) == 0 => *ptr == 0
12187 strlen(ptr) != 0 => *ptr != 0
12188 Other cases should reduce to one of these two (or a constant)
12189 due to the return value of strlen being unsigned. */
12190 if (TREE_CODE (arg0
) == CALL_EXPR
&& integer_zerop (arg1
))
12192 tree fndecl
= get_callee_fndecl (arg0
);
12195 && fndecl_built_in_p (fndecl
, BUILT_IN_STRLEN
)
12196 && call_expr_nargs (arg0
) == 1
12197 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0)))
12201 = build_pointer_type (build_qualified_type (char_type_node
,
12203 tree ptr
= fold_convert_loc (loc
, ptrtype
,
12204 CALL_EXPR_ARG (arg0
, 0));
12205 tree iref
= build_fold_indirect_ref_loc (loc
, ptr
);
12206 return fold_build2_loc (loc
, code
, type
, iref
,
12207 build_int_cst (TREE_TYPE (iref
), 0));
12211 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12212 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12213 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12214 && integer_zerop (arg1
)
12215 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12217 tree arg00
= TREE_OPERAND (arg0
, 0);
12218 tree arg01
= TREE_OPERAND (arg0
, 1);
12219 tree itype
= TREE_TYPE (arg00
);
12220 if (wi::to_wide (arg01
) == element_precision (itype
) - 1)
12222 if (TYPE_UNSIGNED (itype
))
12224 itype
= signed_type_for (itype
);
12225 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12227 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12228 type
, arg00
, build_zero_cst (itype
));
12232 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12233 (X & C) == 0 when C is a single bit. */
12234 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12235 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12236 && integer_zerop (arg1
)
12237 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12239 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12240 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12241 TREE_OPERAND (arg0
, 1));
12242 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12244 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12248 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12249 constant C is a power of two, i.e. a single bit. */
12250 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12251 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12252 && integer_zerop (arg1
)
12253 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12254 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12255 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12257 tree arg00
= TREE_OPERAND (arg0
, 0);
12258 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12259 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12262 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12263 when is C is a power of two, i.e. a single bit. */
12264 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12265 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12266 && integer_zerop (arg1
)
12267 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12268 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12269 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12271 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12272 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12273 arg000
, TREE_OPERAND (arg0
, 1));
12274 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12275 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12278 if (integer_zerop (arg1
)
12279 && tree_expr_nonzero_p (arg0
))
12281 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
12282 return omit_one_operand_loc (loc
, type
, res
, arg0
);
12285 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12286 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12288 tree arg00
= TREE_OPERAND (arg0
, 0);
12289 tree arg01
= TREE_OPERAND (arg0
, 1);
12290 tree arg10
= TREE_OPERAND (arg1
, 0);
12291 tree arg11
= TREE_OPERAND (arg1
, 1);
12292 tree itype
= TREE_TYPE (arg0
);
12294 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12295 operand_equal_p guarantees no side-effects so we don't need
12296 to use omit_one_operand on Z. */
12297 if (operand_equal_p (arg01
, arg11
, 0))
12298 return fold_build2_loc (loc
, code
, type
, arg00
,
12299 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12301 if (operand_equal_p (arg01
, arg10
, 0))
12302 return fold_build2_loc (loc
, code
, type
, arg00
,
12303 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12305 if (operand_equal_p (arg00
, arg11
, 0))
12306 return fold_build2_loc (loc
, code
, type
, arg01
,
12307 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12309 if (operand_equal_p (arg00
, arg10
, 0))
12310 return fold_build2_loc (loc
, code
, type
, arg01
,
12311 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12314 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12315 if (TREE_CODE (arg01
) == INTEGER_CST
12316 && TREE_CODE (arg11
) == INTEGER_CST
)
12318 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12319 fold_convert_loc (loc
, itype
, arg11
));
12320 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12321 return fold_build2_loc (loc
, code
, type
, tem
,
12322 fold_convert_loc (loc
, itype
, arg10
));
12326 /* Attempt to simplify equality/inequality comparisons of complex
12327 values. Only lower the comparison if the result is known or
12328 can be simplified to a single scalar comparison. */
12329 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12330 || TREE_CODE (arg0
) == COMPLEX_CST
)
12331 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12332 || TREE_CODE (arg1
) == COMPLEX_CST
))
12334 tree real0
, imag0
, real1
, imag1
;
12337 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12339 real0
= TREE_OPERAND (arg0
, 0);
12340 imag0
= TREE_OPERAND (arg0
, 1);
12344 real0
= TREE_REALPART (arg0
);
12345 imag0
= TREE_IMAGPART (arg0
);
12348 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12350 real1
= TREE_OPERAND (arg1
, 0);
12351 imag1
= TREE_OPERAND (arg1
, 1);
12355 real1
= TREE_REALPART (arg1
);
12356 imag1
= TREE_IMAGPART (arg1
);
12359 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12360 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12362 if (integer_zerop (rcond
))
12364 if (code
== EQ_EXPR
)
12365 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12367 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12371 if (code
== NE_EXPR
)
12372 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12374 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12378 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12379 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12381 if (integer_zerop (icond
))
12383 if (code
== EQ_EXPR
)
12384 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12386 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12390 if (code
== NE_EXPR
)
12391 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12393 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12404 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12405 if (tem
!= NULL_TREE
)
12408 /* Transform comparisons of the form X +- C CMP X. */
12409 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12410 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12411 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12412 && !HONOR_SNANS (arg0
))
12414 tree arg01
= TREE_OPERAND (arg0
, 1);
12415 enum tree_code code0
= TREE_CODE (arg0
);
12416 int is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12418 /* (X - c) > X becomes false. */
12419 if (code
== GT_EXPR
12420 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12421 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12422 return constant_boolean_node (0, type
);
12424 /* Likewise (X + c) < X becomes false. */
12425 if (code
== LT_EXPR
12426 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12427 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12428 return constant_boolean_node (0, type
);
12430 /* Convert (X - c) <= X to true. */
12431 if (!HONOR_NANS (arg1
)
12433 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12434 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12435 return constant_boolean_node (1, type
);
12437 /* Convert (X + c) >= X to true. */
12438 if (!HONOR_NANS (arg1
)
12440 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12441 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12442 return constant_boolean_node (1, type
);
12445 /* If we are comparing an ABS_EXPR with a constant, we can
12446 convert all the cases into explicit comparisons, but they may
12447 well not be faster than doing the ABS and one comparison.
12448 But ABS (X) <= C is a range comparison, which becomes a subtraction
12449 and a comparison, and is probably faster. */
12450 if (code
== LE_EXPR
12451 && TREE_CODE (arg1
) == INTEGER_CST
12452 && TREE_CODE (arg0
) == ABS_EXPR
12453 && ! TREE_SIDE_EFFECTS (arg0
)
12454 && (tem
= negate_expr (arg1
)) != 0
12455 && TREE_CODE (tem
) == INTEGER_CST
12456 && !TREE_OVERFLOW (tem
))
12457 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12458 build2 (GE_EXPR
, type
,
12459 TREE_OPERAND (arg0
, 0), tem
),
12460 build2 (LE_EXPR
, type
,
12461 TREE_OPERAND (arg0
, 0), arg1
));
12463 /* Convert ABS_EXPR<x> >= 0 to true. */
12464 strict_overflow_p
= false;
12465 if (code
== GE_EXPR
12466 && (integer_zerop (arg1
)
12467 || (! HONOR_NANS (arg0
)
12468 && real_zerop (arg1
)))
12469 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12471 if (strict_overflow_p
)
12472 fold_overflow_warning (("assuming signed overflow does not occur "
12473 "when simplifying comparison of "
12474 "absolute value and zero"),
12475 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12476 return omit_one_operand_loc (loc
, type
,
12477 constant_boolean_node (true, type
),
12481 /* Convert ABS_EXPR<x> < 0 to false. */
12482 strict_overflow_p
= false;
12483 if (code
== LT_EXPR
12484 && (integer_zerop (arg1
) || real_zerop (arg1
))
12485 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12487 if (strict_overflow_p
)
12488 fold_overflow_warning (("assuming signed overflow does not occur "
12489 "when simplifying comparison of "
12490 "absolute value and zero"),
12491 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12492 return omit_one_operand_loc (loc
, type
,
12493 constant_boolean_node (false, type
),
12497 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12498 and similarly for >= into !=. */
12499 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12500 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12501 && TREE_CODE (arg1
) == LSHIFT_EXPR
12502 && integer_onep (TREE_OPERAND (arg1
, 0)))
12503 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12504 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12505 TREE_OPERAND (arg1
, 1)),
12506 build_zero_cst (TREE_TYPE (arg0
)));
12508 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12509 otherwise Y might be >= # of bits in X's type and thus e.g.
12510 (unsigned char) (1 << Y) for Y 15 might be 0.
12511 If the cast is widening, then 1 << Y should have unsigned type,
12512 otherwise if Y is number of bits in the signed shift type minus 1,
12513 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12514 31 might be 0xffffffff80000000. */
12515 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12516 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12517 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0
)))
12518 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12519 && CONVERT_EXPR_P (arg1
)
12520 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12521 && (element_precision (TREE_TYPE (arg1
))
12522 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12523 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12524 || (element_precision (TREE_TYPE (arg1
))
12525 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12526 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12528 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12529 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12530 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12531 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12532 build_zero_cst (TREE_TYPE (arg0
)));
12537 case UNORDERED_EXPR
:
12545 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12547 tree targ0
= strip_float_extensions (arg0
);
12548 tree targ1
= strip_float_extensions (arg1
);
12549 tree newtype
= TREE_TYPE (targ0
);
12551 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
12552 newtype
= TREE_TYPE (targ1
);
12554 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
12555 return fold_build2_loc (loc
, code
, type
,
12556 fold_convert_loc (loc
, newtype
, targ0
),
12557 fold_convert_loc (loc
, newtype
, targ1
));
12562 case COMPOUND_EXPR
:
12563 /* When pedantic, a compound expression can be neither an lvalue
12564 nor an integer constant expression. */
12565 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12567 /* Don't let (0, 0) be null pointer constant. */
12568 tem
= integer_zerop (arg1
) ? build1_loc (loc
, NOP_EXPR
, type
, arg1
)
12569 : fold_convert_loc (loc
, type
, arg1
);
12574 } /* switch (code) */
12577 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12578 ((A & N) + B) & M -> (A + B) & M
12579 Similarly if (N & M) == 0,
12580 ((A | N) + B) & M -> (A + B) & M
12581 and for - instead of + (or unary - instead of +)
12582 and/or ^ instead of |.
12583 If B is constant and (B & M) == 0, fold into A & M.
12585 This function is a helper for match.pd patterns. Return non-NULL
12586 type in which the simplified operation should be performed only
12587 if any optimization is possible.
12589 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12590 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12591 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12594 fold_bit_and_mask (tree type
, tree arg1
, enum tree_code code
,
12595 tree arg00
, enum tree_code code00
, tree arg000
, tree arg001
,
12596 tree arg01
, enum tree_code code01
, tree arg010
, tree arg011
,
12599 gcc_assert (TREE_CODE (arg1
) == INTEGER_CST
);
12600 gcc_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== NEGATE_EXPR
);
12601 wi::tree_to_wide_ref cst1
= wi::to_wide (arg1
);
12603 || (cst1
& (cst1
+ 1)) != 0
12604 || !INTEGRAL_TYPE_P (type
)
12605 || (!TYPE_OVERFLOW_WRAPS (type
)
12606 && TREE_CODE (type
) != INTEGER_TYPE
)
12607 || (wi::max_value (type
) & cst1
) != cst1
)
12610 enum tree_code codes
[2] = { code00
, code01
};
12611 tree arg0xx
[4] = { arg000
, arg001
, arg010
, arg011
};
12615 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12616 arg1 (M) is == (1LL << cst) - 1.
12617 Store C into PMOP[0] and D into PMOP[1]. */
12620 which
= code
!= NEGATE_EXPR
;
12622 for (; which
>= 0; which
--)
12623 switch (codes
[which
])
12628 gcc_assert (TREE_CODE (arg0xx
[2 * which
+ 1]) == INTEGER_CST
);
12629 cst0
= wi::to_wide (arg0xx
[2 * which
+ 1]) & cst1
;
12630 if (codes
[which
] == BIT_AND_EXPR
)
12635 else if (cst0
!= 0)
12637 /* If C or D is of the form (A & N) where
12638 (N & M) == M, or of the form (A | N) or
12639 (A ^ N) where (N & M) == 0, replace it with A. */
12640 pmop
[which
] = arg0xx
[2 * which
];
12643 if (TREE_CODE (pmop
[which
]) != INTEGER_CST
)
12645 /* If C or D is a N where (N & M) == 0, it can be
12646 omitted (replaced with 0). */
12647 if ((code
== PLUS_EXPR
12648 || (code
== MINUS_EXPR
&& which
== 0))
12649 && (cst1
& wi::to_wide (pmop
[which
])) == 0)
12650 pmop
[which
] = build_int_cst (type
, 0);
12651 /* Similarly, with C - N where (-N & M) == 0. */
12652 if (code
== MINUS_EXPR
12654 && (cst1
& -wi::to_wide (pmop
[which
])) == 0)
12655 pmop
[which
] = build_int_cst (type
, 0);
12658 gcc_unreachable ();
12661 /* Only build anything new if we optimized one or both arguments above. */
12662 if (pmop
[0] == arg00
&& pmop
[1] == arg01
)
12665 if (TYPE_OVERFLOW_WRAPS (type
))
12668 return unsigned_type_for (type
);
12671 /* Used by contains_label_[p1]. */
12673 struct contains_label_data
12675 hash_set
<tree
> *pset
;
12676 bool inside_switch_p
;
12679 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12680 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12681 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12684 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data
)
12686 contains_label_data
*d
= (contains_label_data
*) data
;
12687 switch (TREE_CODE (*tp
))
12692 case CASE_LABEL_EXPR
:
12693 if (!d
->inside_switch_p
)
12698 if (!d
->inside_switch_p
)
12700 if (walk_tree (&SWITCH_COND (*tp
), contains_label_1
, data
, d
->pset
))
12702 d
->inside_switch_p
= true;
12703 if (walk_tree (&SWITCH_BODY (*tp
), contains_label_1
, data
, d
->pset
))
12705 d
->inside_switch_p
= false;
12706 *walk_subtrees
= 0;
12711 *walk_subtrees
= 0;
12719 /* Return whether the sub-tree ST contains a label which is accessible from
12720 outside the sub-tree. */
12723 contains_label_p (tree st
)
12725 hash_set
<tree
> pset
;
12726 contains_label_data data
= { &pset
, false };
12727 return walk_tree (&st
, contains_label_1
, &data
, &pset
) != NULL_TREE
;
12730 /* Fold a ternary expression of code CODE and type TYPE with operands
12731 OP0, OP1, and OP2. Return the folded expression if folding is
12732 successful. Otherwise, return NULL_TREE. */
12735 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
12736 tree op0
, tree op1
, tree op2
)
12739 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
12740 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
12742 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
12743 && TREE_CODE_LENGTH (code
) == 3);
12745 /* If this is a commutative operation, and OP0 is a constant, move it
12746 to OP1 to reduce the number of tests below. */
12747 if (commutative_ternary_tree_code (code
)
12748 && tree_swap_operands_p (op0
, op1
))
12749 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
12751 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
12755 /* Strip any conversions that don't change the mode. This is safe
12756 for every expression, except for a comparison expression because
12757 its signedness is derived from its operands. So, in the latter
12758 case, only strip conversions that don't change the signedness.
12760 Note that this is done as an internal manipulation within the
12761 constant folder, in order to find the simplest representation of
12762 the arguments so that their form can be studied. In any cases,
12763 the appropriate type conversions should be put back in the tree
12764 that will get out of the constant folder. */
12785 case COMPONENT_REF
:
12786 if (TREE_CODE (arg0
) == CONSTRUCTOR
12787 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
12789 unsigned HOST_WIDE_INT idx
;
12791 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
12798 case VEC_COND_EXPR
:
12799 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12800 so all simple results must be passed through pedantic_non_lvalue. */
12801 if (TREE_CODE (arg0
) == INTEGER_CST
)
12803 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
12804 tem
= integer_zerop (arg0
) ? op2
: op1
;
12805 /* Only optimize constant conditions when the selected branch
12806 has the same type as the COND_EXPR. This avoids optimizing
12807 away "c ? x : throw", where the throw has a void type.
12808 Avoid throwing away that operand which contains label. */
12809 if ((!TREE_SIDE_EFFECTS (unused_op
)
12810 || !contains_label_p (unused_op
))
12811 && (! VOID_TYPE_P (TREE_TYPE (tem
))
12812 || VOID_TYPE_P (type
)))
12813 return protected_set_expr_location_unshare (tem
, loc
);
12816 else if (TREE_CODE (arg0
) == VECTOR_CST
)
12818 unsigned HOST_WIDE_INT nelts
;
12819 if ((TREE_CODE (arg1
) == VECTOR_CST
12820 || TREE_CODE (arg1
) == CONSTRUCTOR
)
12821 && (TREE_CODE (arg2
) == VECTOR_CST
12822 || TREE_CODE (arg2
) == CONSTRUCTOR
)
12823 && TYPE_VECTOR_SUBPARTS (type
).is_constant (&nelts
))
12825 vec_perm_builder
sel (nelts
, nelts
, 1);
12826 for (unsigned int i
= 0; i
< nelts
; i
++)
12828 tree val
= VECTOR_CST_ELT (arg0
, i
);
12829 if (integer_all_onesp (val
))
12830 sel
.quick_push (i
);
12831 else if (integer_zerop (val
))
12832 sel
.quick_push (nelts
+ i
);
12833 else /* Currently unreachable. */
12836 vec_perm_indices
indices (sel
, 2, nelts
);
12837 tree t
= fold_vec_perm (type
, arg1
, arg2
, indices
);
12838 if (t
!= NULL_TREE
)
12843 /* If we have A op B ? A : C, we may be able to convert this to a
12844 simpler expression, depending on the operation and the values
12845 of B and C. Signed zeros prevent all of these transformations,
12846 for reasons given above each one.
12848 Also try swapping the arguments and inverting the conditional. */
12849 if (COMPARISON_CLASS_P (arg0
)
12850 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op1
)
12851 && !HONOR_SIGNED_ZEROS (op1
))
12853 tem
= fold_cond_expr_with_comparison (loc
, type
, TREE_CODE (arg0
),
12854 TREE_OPERAND (arg0
, 0),
12855 TREE_OPERAND (arg0
, 1),
12861 if (COMPARISON_CLASS_P (arg0
)
12862 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
12863 && !HONOR_SIGNED_ZEROS (op2
))
12865 enum tree_code comp_code
= TREE_CODE (arg0
);
12866 tree arg00
= TREE_OPERAND (arg0
, 0);
12867 tree arg01
= TREE_OPERAND (arg0
, 1);
12868 comp_code
= invert_tree_comparison (comp_code
, HONOR_NANS (arg00
));
12869 if (comp_code
!= ERROR_MARK
)
12870 tem
= fold_cond_expr_with_comparison (loc
, type
, comp_code
,
12878 /* If the second operand is simpler than the third, swap them
12879 since that produces better jump optimization results. */
12880 if (truth_value_p (TREE_CODE (arg0
))
12881 && tree_swap_operands_p (op1
, op2
))
12883 location_t loc0
= expr_location_or (arg0
, loc
);
12884 /* See if this can be inverted. If it can't, possibly because
12885 it was a floating-point inequality comparison, don't do
12887 tem
= fold_invert_truthvalue (loc0
, arg0
);
12889 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
12892 /* Convert A ? 1 : 0 to simply A. */
12893 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
12894 : (integer_onep (op1
)
12895 && !VECTOR_TYPE_P (type
)))
12896 && integer_zerop (op2
)
12897 /* If we try to convert OP0 to our type, the
12898 call to fold will try to move the conversion inside
12899 a COND, which will recurse. In that case, the COND_EXPR
12900 is probably the best choice, so leave it alone. */
12901 && type
== TREE_TYPE (arg0
))
12902 return protected_set_expr_location_unshare (arg0
, loc
);
12904 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12905 over COND_EXPR in cases such as floating point comparisons. */
12906 if (integer_zerop (op1
)
12907 && code
== COND_EXPR
12908 && integer_onep (op2
)
12909 && !VECTOR_TYPE_P (type
)
12910 && truth_value_p (TREE_CODE (arg0
)))
12911 return fold_convert_loc (loc
, type
,
12912 invert_truthvalue_loc (loc
, arg0
));
12914 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12915 if (TREE_CODE (arg0
) == LT_EXPR
12916 && integer_zerop (TREE_OPERAND (arg0
, 1))
12917 && integer_zerop (op2
)
12918 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
12920 /* sign_bit_p looks through both zero and sign extensions,
12921 but for this optimization only sign extensions are
12923 tree tem2
= TREE_OPERAND (arg0
, 0);
12924 while (tem
!= tem2
)
12926 if (TREE_CODE (tem2
) != NOP_EXPR
12927 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
12932 tem2
= TREE_OPERAND (tem2
, 0);
12934 /* sign_bit_p only checks ARG1 bits within A's precision.
12935 If <sign bit of A> has wider type than A, bits outside
12936 of A's precision in <sign bit of A> need to be checked.
12937 If they are all 0, this optimization needs to be done
12938 in unsigned A's type, if they are all 1 in signed A's type,
12939 otherwise this can't be done. */
12941 && TYPE_PRECISION (TREE_TYPE (tem
))
12942 < TYPE_PRECISION (TREE_TYPE (arg1
))
12943 && TYPE_PRECISION (TREE_TYPE (tem
))
12944 < TYPE_PRECISION (type
))
12946 int inner_width
, outer_width
;
12949 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
12950 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
12951 if (outer_width
> TYPE_PRECISION (type
))
12952 outer_width
= TYPE_PRECISION (type
);
12954 wide_int mask
= wi::shifted_mask
12955 (inner_width
, outer_width
- inner_width
, false,
12956 TYPE_PRECISION (TREE_TYPE (arg1
)));
12958 wide_int common
= mask
& wi::to_wide (arg1
);
12959 if (common
== mask
)
12961 tem_type
= signed_type_for (TREE_TYPE (tem
));
12962 tem
= fold_convert_loc (loc
, tem_type
, tem
);
12964 else if (common
== 0)
12966 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
12967 tem
= fold_convert_loc (loc
, tem_type
, tem
);
12975 fold_convert_loc (loc
, type
,
12976 fold_build2_loc (loc
, BIT_AND_EXPR
,
12977 TREE_TYPE (tem
), tem
,
12978 fold_convert_loc (loc
,
12983 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12984 already handled above. */
12985 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12986 && integer_onep (TREE_OPERAND (arg0
, 1))
12987 && integer_zerop (op2
)
12988 && integer_pow2p (arg1
))
12990 tree tem
= TREE_OPERAND (arg0
, 0);
12992 if (TREE_CODE (tem
) == RSHIFT_EXPR
12993 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
12994 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
12995 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
12996 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
12997 fold_convert_loc (loc
, type
,
12998 TREE_OPERAND (tem
, 0)),
13002 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13003 is probably obsolete because the first operand should be a
13004 truth value (that's why we have the two cases above), but let's
13005 leave it in until we can confirm this for all front-ends. */
13006 if (integer_zerop (op2
)
13007 && TREE_CODE (arg0
) == NE_EXPR
13008 && integer_zerop (TREE_OPERAND (arg0
, 1))
13009 && integer_pow2p (arg1
)
13010 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13011 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13012 arg1
, OEP_ONLY_CONST
)
13013 /* operand_equal_p compares just value, not precision, so e.g.
13014 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13015 second operand 32-bit -128, which is not a power of two (or vice
13017 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)))
13018 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
13020 /* Disable the transformations below for vectors, since
13021 fold_binary_op_with_conditional_arg may undo them immediately,
13022 yielding an infinite loop. */
13023 if (code
== VEC_COND_EXPR
)
13026 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13027 if (integer_zerop (op2
)
13028 && truth_value_p (TREE_CODE (arg0
))
13029 && truth_value_p (TREE_CODE (arg1
))
13030 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13031 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13032 : TRUTH_ANDIF_EXPR
,
13033 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
13035 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13036 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13037 && truth_value_p (TREE_CODE (arg0
))
13038 && truth_value_p (TREE_CODE (arg1
))
13039 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13041 location_t loc0
= expr_location_or (arg0
, loc
);
13042 /* Only perform transformation if ARG0 is easily inverted. */
13043 tem
= fold_invert_truthvalue (loc0
, arg0
);
13045 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13048 type
, fold_convert_loc (loc
, type
, tem
),
13052 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13053 if (integer_zerop (arg1
)
13054 && truth_value_p (TREE_CODE (arg0
))
13055 && truth_value_p (TREE_CODE (op2
))
13056 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13058 location_t loc0
= expr_location_or (arg0
, loc
);
13059 /* Only perform transformation if ARG0 is easily inverted. */
13060 tem
= fold_invert_truthvalue (loc0
, arg0
);
13062 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13063 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13064 type
, fold_convert_loc (loc
, type
, tem
),
13068 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13069 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13070 && truth_value_p (TREE_CODE (arg0
))
13071 && truth_value_p (TREE_CODE (op2
))
13072 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13073 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13074 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13075 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13080 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13081 of fold_ternary on them. */
13082 gcc_unreachable ();
13084 case BIT_FIELD_REF
:
13085 if (TREE_CODE (arg0
) == VECTOR_CST
13086 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13087 || (VECTOR_TYPE_P (type
)
13088 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
))))
13089 && tree_fits_uhwi_p (op1
)
13090 && tree_fits_uhwi_p (op2
))
13092 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13093 unsigned HOST_WIDE_INT width
13094 = (TREE_CODE (eltype
) == BOOLEAN_TYPE
13095 ? TYPE_PRECISION (eltype
) : tree_to_uhwi (TYPE_SIZE (eltype
)));
13096 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13097 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13100 && (idx
% width
) == 0
13101 && (n
% width
) == 0
13102 && known_le ((idx
+ n
) / width
,
13103 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))))
13108 if (TREE_CODE (arg0
) == VECTOR_CST
)
13112 tem
= VECTOR_CST_ELT (arg0
, idx
);
13113 if (VECTOR_TYPE_P (type
))
13114 tem
= fold_build1 (VIEW_CONVERT_EXPR
, type
, tem
);
13118 tree_vector_builder
vals (type
, n
, 1);
13119 for (unsigned i
= 0; i
< n
; ++i
)
13120 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
13121 return vals
.build ();
13126 /* On constants we can use native encode/interpret to constant
13127 fold (nearly) all BIT_FIELD_REFs. */
13128 if (CONSTANT_CLASS_P (arg0
)
13129 && can_native_interpret_type_p (type
)
13130 && BITS_PER_UNIT
== 8
13131 && tree_fits_uhwi_p (op1
)
13132 && tree_fits_uhwi_p (op2
))
13134 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13135 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13136 /* Limit us to a reasonable amount of work. To relax the
13137 other limitations we need bit-shifting of the buffer
13138 and rounding up the size. */
13139 if (bitpos
% BITS_PER_UNIT
== 0
13140 && bitsize
% BITS_PER_UNIT
== 0
13141 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
13143 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
13144 unsigned HOST_WIDE_INT len
13145 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
13146 bitpos
/ BITS_PER_UNIT
);
13148 && len
* BITS_PER_UNIT
>= bitsize
)
13150 tree v
= native_interpret_expr (type
, b
,
13151 bitsize
/ BITS_PER_UNIT
);
13160 case VEC_PERM_EXPR
:
13161 /* Perform constant folding of BIT_INSERT_EXPR. */
13162 if (TREE_CODE (arg2
) == VECTOR_CST
13163 && TREE_CODE (op0
) == VECTOR_CST
13164 && TREE_CODE (op1
) == VECTOR_CST
)
13166 /* Build a vector of integers from the tree mask. */
13167 vec_perm_builder builder
;
13168 if (!tree_to_vec_perm_builder (&builder
, arg2
))
13171 /* Create a vec_perm_indices for the integer vector. */
13172 poly_uint64 nelts
= TYPE_VECTOR_SUBPARTS (type
);
13173 bool single_arg
= (op0
== op1
);
13174 vec_perm_indices
sel (builder
, single_arg
? 1 : 2, nelts
);
13175 return fold_vec_perm (type
, op0
, op1
, sel
);
13179 case BIT_INSERT_EXPR
:
13180 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13181 if (TREE_CODE (arg0
) == INTEGER_CST
13182 && TREE_CODE (arg1
) == INTEGER_CST
)
13184 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13185 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
13186 wide_int tem
= (wi::to_wide (arg0
)
13187 & wi::shifted_mask (bitpos
, bitsize
, true,
13188 TYPE_PRECISION (type
)));
13190 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
13192 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
13194 else if (TREE_CODE (arg0
) == VECTOR_CST
13195 && CONSTANT_CLASS_P (arg1
)
13196 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
13199 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13200 unsigned HOST_WIDE_INT elsize
13201 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
13202 if (bitpos
% elsize
== 0)
13204 unsigned k
= bitpos
/ elsize
;
13205 unsigned HOST_WIDE_INT nelts
;
13206 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
13208 else if (VECTOR_CST_NELTS (arg0
).is_constant (&nelts
))
13210 tree_vector_builder
elts (type
, nelts
, 1);
13211 elts
.quick_grow (nelts
);
13212 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; ++i
)
13213 elts
[i
] = (i
== k
? arg1
: VECTOR_CST_ELT (arg0
, i
));
13214 return elts
.build ();
13222 } /* switch (code) */
13225 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13226 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13227 constructor element index of the value returned. If the element is
13228 not found NULL_TREE is returned and *CTOR_IDX is updated to
13229 the index of the element after the ACCESS_INDEX position (which
13230 may be outside of the CTOR array). */
13233 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
,
13234 unsigned *ctor_idx
)
13236 tree index_type
= NULL_TREE
;
13237 signop index_sgn
= UNSIGNED
;
13238 offset_int low_bound
= 0;
13240 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
13242 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
13243 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
13245 /* Static constructors for variably sized objects makes no sense. */
13246 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
13247 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
13248 /* ??? When it is obvious that the range is signed, treat it so. */
13249 if (TYPE_UNSIGNED (index_type
)
13250 && TYPE_MAX_VALUE (domain_type
)
13251 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type
),
13252 TYPE_MIN_VALUE (domain_type
)))
13254 index_sgn
= SIGNED
;
13256 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type
)),
13261 index_sgn
= TYPE_SIGN (index_type
);
13262 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
13268 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
13271 offset_int index
= low_bound
;
13273 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13275 offset_int max_index
= index
;
13278 bool first_p
= true;
13280 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
13282 /* Array constructor might explicitly set index, or specify a range,
13283 or leave index NULL meaning that it is next index after previous
13287 if (TREE_CODE (cfield
) == INTEGER_CST
)
13289 = offset_int::from (wi::to_wide (cfield
), index_sgn
);
13292 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
13293 index
= offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 0)),
13296 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield
, 1)),
13298 gcc_checking_assert (wi::le_p (index
, max_index
, index_sgn
));
13303 index
= max_index
+ 1;
13305 index
= wi::ext (index
, TYPE_PRECISION (index_type
), index_sgn
);
13306 gcc_checking_assert (wi::gt_p (index
, max_index
, index_sgn
));
13312 /* Do we have match? */
13313 if (wi::cmp (access_index
, index
, index_sgn
) >= 0)
13315 if (wi::cmp (access_index
, max_index
, index_sgn
) <= 0)
13322 else if (in_gimple_form
)
13323 /* We're past the element we search for. Note during parsing
13324 the elements might not be sorted.
13325 ??? We should use a binary search and a flag on the
13326 CONSTRUCTOR as to whether elements are sorted in declaration
13335 /* Perform constant folding and related simplification of EXPR.
13336 The related simplifications include x*1 => x, x*0 => 0, etc.,
13337 and application of the associative law.
13338 NOP_EXPR conversions may be removed freely (as long as we
13339 are careful not to change the type of the overall expression).
13340 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13341 but we can constant-fold them if they have constant operands. */
13343 #ifdef ENABLE_FOLD_CHECKING
13344 # define fold(x) fold_1 (x)
13345 static tree
fold_1 (tree
);
13351 const tree t
= expr
;
13352 enum tree_code code
= TREE_CODE (t
);
13353 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13355 location_t loc
= EXPR_LOCATION (expr
);
13357 /* Return right away if a constant. */
13358 if (kind
== tcc_constant
)
13361 /* CALL_EXPR-like objects with variable numbers of operands are
13362 treated specially. */
13363 if (kind
== tcc_vl_exp
)
13365 if (code
== CALL_EXPR
)
13367 tem
= fold_call_expr (loc
, expr
, false);
13368 return tem
? tem
: expr
;
13373 if (IS_EXPR_CODE_CLASS (kind
))
13375 tree type
= TREE_TYPE (t
);
13376 tree op0
, op1
, op2
;
13378 switch (TREE_CODE_LENGTH (code
))
13381 op0
= TREE_OPERAND (t
, 0);
13382 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13383 return tem
? tem
: expr
;
13385 op0
= TREE_OPERAND (t
, 0);
13386 op1
= TREE_OPERAND (t
, 1);
13387 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13388 return tem
? tem
: expr
;
13390 op0
= TREE_OPERAND (t
, 0);
13391 op1
= TREE_OPERAND (t
, 1);
13392 op2
= TREE_OPERAND (t
, 2);
13393 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13394 return tem
? tem
: expr
;
13404 tree op0
= TREE_OPERAND (t
, 0);
13405 tree op1
= TREE_OPERAND (t
, 1);
13407 if (TREE_CODE (op1
) == INTEGER_CST
13408 && TREE_CODE (op0
) == CONSTRUCTOR
13409 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13411 tree val
= get_array_ctor_element_at_index (op0
,
13412 wi::to_offset (op1
));
13420 /* Return a VECTOR_CST if possible. */
13423 tree type
= TREE_TYPE (t
);
13424 if (TREE_CODE (type
) != VECTOR_TYPE
)
13429 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
13430 if (! CONSTANT_CLASS_P (val
))
13433 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
13437 return fold (DECL_INITIAL (t
));
13441 } /* switch (code) */
13444 #ifdef ENABLE_FOLD_CHECKING
13447 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13448 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13449 static void fold_check_failed (const_tree
, const_tree
);
13450 void print_fold_checksum (const_tree
);
13452 /* When --enable-checking=fold, compute a digest of expr before
13453 and after actual fold call to see if fold did not accidentally
13454 change original expr. */
13460 struct md5_ctx ctx
;
13461 unsigned char checksum_before
[16], checksum_after
[16];
13462 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13464 md5_init_ctx (&ctx
);
13465 fold_checksum_tree (expr
, &ctx
, &ht
);
13466 md5_finish_ctx (&ctx
, checksum_before
);
13469 ret
= fold_1 (expr
);
13471 md5_init_ctx (&ctx
);
13472 fold_checksum_tree (expr
, &ctx
, &ht
);
13473 md5_finish_ctx (&ctx
, checksum_after
);
13475 if (memcmp (checksum_before
, checksum_after
, 16))
13476 fold_check_failed (expr
, ret
);
13482 print_fold_checksum (const_tree expr
)
13484 struct md5_ctx ctx
;
13485 unsigned char checksum
[16], cnt
;
13486 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13488 md5_init_ctx (&ctx
);
13489 fold_checksum_tree (expr
, &ctx
, &ht
);
13490 md5_finish_ctx (&ctx
, checksum
);
13491 for (cnt
= 0; cnt
< 16; ++cnt
)
13492 fprintf (stderr
, "%02x", checksum
[cnt
]);
13493 putc ('\n', stderr
);
13497 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13499 internal_error ("fold check: original tree changed by fold");
13503 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13504 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13506 const tree_node
**slot
;
13507 enum tree_code code
;
13508 union tree_node
*buf
;
13514 slot
= ht
->find_slot (expr
, INSERT
);
13518 code
= TREE_CODE (expr
);
13519 if (TREE_CODE_CLASS (code
) == tcc_declaration
13520 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13522 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13523 size_t sz
= tree_size (expr
);
13524 buf
= XALLOCAVAR (union tree_node
, sz
);
13525 memcpy ((char *) buf
, expr
, sz
);
13526 SET_DECL_ASSEMBLER_NAME ((tree
) buf
, NULL
);
13527 buf
->decl_with_vis
.symtab_node
= NULL
;
13528 buf
->base
.nowarning_flag
= 0;
13531 else if (TREE_CODE_CLASS (code
) == tcc_type
13532 && (TYPE_POINTER_TO (expr
)
13533 || TYPE_REFERENCE_TO (expr
)
13534 || TYPE_CACHED_VALUES_P (expr
)
13535 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13536 || TYPE_NEXT_VARIANT (expr
)
13537 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
13539 /* Allow these fields to be modified. */
13541 size_t sz
= tree_size (expr
);
13542 buf
= XALLOCAVAR (union tree_node
, sz
);
13543 memcpy ((char *) buf
, expr
, sz
);
13544 expr
= tmp
= (tree
) buf
;
13545 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13546 TYPE_POINTER_TO (tmp
) = NULL
;
13547 TYPE_REFERENCE_TO (tmp
) = NULL
;
13548 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13549 TYPE_ALIAS_SET (tmp
) = -1;
13550 if (TYPE_CACHED_VALUES_P (tmp
))
13552 TYPE_CACHED_VALUES_P (tmp
) = 0;
13553 TYPE_CACHED_VALUES (tmp
) = NULL
;
13556 else if (warning_suppressed_p (expr
) && (DECL_P (expr
) || EXPR_P (expr
)))
13558 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13559 that and change builtins.cc etc. instead - see PR89543. */
13560 size_t sz
= tree_size (expr
);
13561 buf
= XALLOCAVAR (union tree_node
, sz
);
13562 memcpy ((char *) buf
, expr
, sz
);
13563 buf
->base
.nowarning_flag
= 0;
13566 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13567 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13568 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13569 if (TREE_CODE_CLASS (code
) != tcc_type
13570 && TREE_CODE_CLASS (code
) != tcc_declaration
13571 && code
!= TREE_LIST
13572 && code
!= SSA_NAME
13573 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13574 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13575 switch (TREE_CODE_CLASS (code
))
13581 md5_process_bytes (TREE_STRING_POINTER (expr
),
13582 TREE_STRING_LENGTH (expr
), ctx
);
13585 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13586 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13589 len
= vector_cst_encoded_nelts (expr
);
13590 for (i
= 0; i
< len
; ++i
)
13591 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr
, i
), ctx
, ht
);
13597 case tcc_exceptional
:
13601 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13602 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13603 expr
= TREE_CHAIN (expr
);
13604 goto recursive_label
;
13607 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13608 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13614 case tcc_expression
:
13615 case tcc_reference
:
13616 case tcc_comparison
:
13619 case tcc_statement
:
13621 len
= TREE_OPERAND_LENGTH (expr
);
13622 for (i
= 0; i
< len
; ++i
)
13623 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13625 case tcc_declaration
:
13626 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13627 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13628 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13630 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13631 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13632 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13633 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13634 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13637 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13639 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13641 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13642 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13644 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13648 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13649 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13650 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
13651 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
13652 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
13653 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
13654 if (INTEGRAL_TYPE_P (expr
)
13655 || SCALAR_FLOAT_TYPE_P (expr
))
13657 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
13658 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
13660 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
13661 if (RECORD_OR_UNION_TYPE_P (expr
))
13662 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
13663 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
13670 /* Helper function for outputting the checksum of a tree T. When
13671 debugging with gdb, you can "define mynext" to be "next" followed
13672 by "call debug_fold_checksum (op0)", then just trace down till the
13675 DEBUG_FUNCTION
void
13676 debug_fold_checksum (const_tree t
)
13679 unsigned char checksum
[16];
13680 struct md5_ctx ctx
;
13681 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13683 md5_init_ctx (&ctx
);
13684 fold_checksum_tree (t
, &ctx
, &ht
);
13685 md5_finish_ctx (&ctx
, checksum
);
13688 for (i
= 0; i
< 16; i
++)
13689 fprintf (stderr
, "%d ", checksum
[i
]);
13691 fprintf (stderr
, "\n");
13696 /* Fold a unary tree expression with code CODE of type TYPE with an
13697 operand OP0. LOC is the location of the resulting expression.
13698 Return a folded expression if successful. Otherwise, return a tree
13699 expression with code CODE of type TYPE with an operand OP0. */
13702 fold_build1_loc (location_t loc
,
13703 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
13706 #ifdef ENABLE_FOLD_CHECKING
13707 unsigned char checksum_before
[16], checksum_after
[16];
13708 struct md5_ctx ctx
;
13709 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13711 md5_init_ctx (&ctx
);
13712 fold_checksum_tree (op0
, &ctx
, &ht
);
13713 md5_finish_ctx (&ctx
, checksum_before
);
13717 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13719 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
13721 #ifdef ENABLE_FOLD_CHECKING
13722 md5_init_ctx (&ctx
);
13723 fold_checksum_tree (op0
, &ctx
, &ht
);
13724 md5_finish_ctx (&ctx
, checksum_after
);
13726 if (memcmp (checksum_before
, checksum_after
, 16))
13727 fold_check_failed (op0
, tem
);
13732 /* Fold a binary tree expression with code CODE of type TYPE with
13733 operands OP0 and OP1. LOC is the location of the resulting
13734 expression. Return a folded expression if successful. Otherwise,
13735 return a tree expression with code CODE of type TYPE with operands
13739 fold_build2_loc (location_t loc
,
13740 enum tree_code code
, tree type
, tree op0
, tree op1
13744 #ifdef ENABLE_FOLD_CHECKING
13745 unsigned char checksum_before_op0
[16],
13746 checksum_before_op1
[16],
13747 checksum_after_op0
[16],
13748 checksum_after_op1
[16];
13749 struct md5_ctx ctx
;
13750 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13752 md5_init_ctx (&ctx
);
13753 fold_checksum_tree (op0
, &ctx
, &ht
);
13754 md5_finish_ctx (&ctx
, checksum_before_op0
);
13757 md5_init_ctx (&ctx
);
13758 fold_checksum_tree (op1
, &ctx
, &ht
);
13759 md5_finish_ctx (&ctx
, checksum_before_op1
);
13763 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13765 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
13767 #ifdef ENABLE_FOLD_CHECKING
13768 md5_init_ctx (&ctx
);
13769 fold_checksum_tree (op0
, &ctx
, &ht
);
13770 md5_finish_ctx (&ctx
, checksum_after_op0
);
13773 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13774 fold_check_failed (op0
, tem
);
13776 md5_init_ctx (&ctx
);
13777 fold_checksum_tree (op1
, &ctx
, &ht
);
13778 md5_finish_ctx (&ctx
, checksum_after_op1
);
13780 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13781 fold_check_failed (op1
, tem
);
13786 /* Fold a ternary tree expression with code CODE of type TYPE with
13787 operands OP0, OP1, and OP2. Return a folded expression if
13788 successful. Otherwise, return a tree expression with code CODE of
13789 type TYPE with operands OP0, OP1, and OP2. */
13792 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
13793 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
13796 #ifdef ENABLE_FOLD_CHECKING
13797 unsigned char checksum_before_op0
[16],
13798 checksum_before_op1
[16],
13799 checksum_before_op2
[16],
13800 checksum_after_op0
[16],
13801 checksum_after_op1
[16],
13802 checksum_after_op2
[16];
13803 struct md5_ctx ctx
;
13804 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13806 md5_init_ctx (&ctx
);
13807 fold_checksum_tree (op0
, &ctx
, &ht
);
13808 md5_finish_ctx (&ctx
, checksum_before_op0
);
13811 md5_init_ctx (&ctx
);
13812 fold_checksum_tree (op1
, &ctx
, &ht
);
13813 md5_finish_ctx (&ctx
, checksum_before_op1
);
13816 md5_init_ctx (&ctx
);
13817 fold_checksum_tree (op2
, &ctx
, &ht
);
13818 md5_finish_ctx (&ctx
, checksum_before_op2
);
13822 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
13823 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13825 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
13827 #ifdef ENABLE_FOLD_CHECKING
13828 md5_init_ctx (&ctx
);
13829 fold_checksum_tree (op0
, &ctx
, &ht
);
13830 md5_finish_ctx (&ctx
, checksum_after_op0
);
13833 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
13834 fold_check_failed (op0
, tem
);
13836 md5_init_ctx (&ctx
);
13837 fold_checksum_tree (op1
, &ctx
, &ht
);
13838 md5_finish_ctx (&ctx
, checksum_after_op1
);
13841 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
13842 fold_check_failed (op1
, tem
);
13844 md5_init_ctx (&ctx
);
13845 fold_checksum_tree (op2
, &ctx
, &ht
);
13846 md5_finish_ctx (&ctx
, checksum_after_op2
);
13848 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
13849 fold_check_failed (op2
, tem
);
13854 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13855 arguments in ARGARRAY, and a null static chain.
13856 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13857 of type TYPE from the given operands as constructed by build_call_array. */
13860 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
13861 int nargs
, tree
*argarray
)
13864 #ifdef ENABLE_FOLD_CHECKING
13865 unsigned char checksum_before_fn
[16],
13866 checksum_before_arglist
[16],
13867 checksum_after_fn
[16],
13868 checksum_after_arglist
[16];
13869 struct md5_ctx ctx
;
13870 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13873 md5_init_ctx (&ctx
);
13874 fold_checksum_tree (fn
, &ctx
, &ht
);
13875 md5_finish_ctx (&ctx
, checksum_before_fn
);
13878 md5_init_ctx (&ctx
);
13879 for (i
= 0; i
< nargs
; i
++)
13880 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13881 md5_finish_ctx (&ctx
, checksum_before_arglist
);
13885 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
13887 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13889 #ifdef ENABLE_FOLD_CHECKING
13890 md5_init_ctx (&ctx
);
13891 fold_checksum_tree (fn
, &ctx
, &ht
);
13892 md5_finish_ctx (&ctx
, checksum_after_fn
);
13895 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
13896 fold_check_failed (fn
, tem
);
13898 md5_init_ctx (&ctx
);
13899 for (i
= 0; i
< nargs
; i
++)
13900 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
13901 md5_finish_ctx (&ctx
, checksum_after_arglist
);
13903 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
13904 fold_check_failed (NULL_TREE
, tem
);
13909 /* Perform constant folding and related simplification of initializer
13910 expression EXPR. These behave identically to "fold_buildN" but ignore
13911 potential run-time traps and exceptions that fold must preserve. */
13913 #define START_FOLD_INIT \
13914 int saved_signaling_nans = flag_signaling_nans;\
13915 int saved_trapping_math = flag_trapping_math;\
13916 int saved_rounding_math = flag_rounding_math;\
13917 int saved_trapv = flag_trapv;\
13918 int saved_folding_initializer = folding_initializer;\
13919 flag_signaling_nans = 0;\
13920 flag_trapping_math = 0;\
13921 flag_rounding_math = 0;\
13923 folding_initializer = 1;
13925 #define END_FOLD_INIT \
13926 flag_signaling_nans = saved_signaling_nans;\
13927 flag_trapping_math = saved_trapping_math;\
13928 flag_rounding_math = saved_rounding_math;\
13929 flag_trapv = saved_trapv;\
13930 folding_initializer = saved_folding_initializer;
13933 fold_init (tree expr
)
13938 result
= fold (expr
);
13945 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
13946 tree type
, tree op
)
13951 result
= fold_build1_loc (loc
, code
, type
, op
);
13958 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
13959 tree type
, tree op0
, tree op1
)
13964 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
13971 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
13972 int nargs
, tree
*argarray
)
13977 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
13984 fold_binary_initializer_loc (location_t loc
, tree_code code
, tree type
,
13985 tree lhs
, tree rhs
)
13990 result
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
13996 #undef START_FOLD_INIT
13997 #undef END_FOLD_INIT
13999 /* Determine if first argument is a multiple of second argument. Return 0 if
14000 it is not, or we cannot easily determined it to be.
14002 An example of the sort of thing we care about (at this point; this routine
14003 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14004 fold cases do now) is discovering that
14006 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14012 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14014 This code also handles discovering that
14016 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14018 is a multiple of 8 so we don't have to worry about dealing with a
14019 possible remainder.
14021 Note that we *look* inside a SAVE_EXPR only to determine how it was
14022 calculated; it is not safe for fold to do much of anything else with the
14023 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14024 at run time. For example, the latter example above *cannot* be implemented
14025 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14026 evaluation time of the original SAVE_EXPR is not necessarily the same at
14027 the time the new expression is evaluated. The only optimization of this
14028 sort that would be valid is changing
14030 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14034 SAVE_EXPR (I) * SAVE_EXPR (J)
14036 (where the same SAVE_EXPR (J) is used in the original and the
14037 transformed version).
14039 NOWRAP specifies whether all outer operations in TYPE should
14040 be considered not wrapping. Any type conversion within TOP acts
14041 as a barrier and we will fall back to NOWRAP being false.
14042 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14043 as not wrapping even though they are generally using unsigned arithmetic. */
14046 multiple_of_p (tree type
, const_tree top
, const_tree bottom
, bool nowrap
)
14051 if (operand_equal_p (top
, bottom
, 0))
14054 if (TREE_CODE (type
) != INTEGER_TYPE
)
14057 switch (TREE_CODE (top
))
14060 /* Bitwise and provides a power of two multiple. If the mask is
14061 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14062 if (!integer_pow2p (bottom
))
14064 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14065 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14068 /* If the multiplication can wrap we cannot recurse further unless
14069 the bottom is a power of two which is where wrapping does not
14072 && !TYPE_OVERFLOW_UNDEFINED (type
)
14073 && !integer_pow2p (bottom
))
14075 if (TREE_CODE (bottom
) == INTEGER_CST
)
14077 op1
= TREE_OPERAND (top
, 0);
14078 op2
= TREE_OPERAND (top
, 1);
14079 if (TREE_CODE (op1
) == INTEGER_CST
)
14080 std::swap (op1
, op2
);
14081 if (TREE_CODE (op2
) == INTEGER_CST
)
14083 if (multiple_of_p (type
, op2
, bottom
, nowrap
))
14085 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14086 if (multiple_of_p (type
, bottom
, op2
, nowrap
))
14088 widest_int w
= wi::sdiv_trunc (wi::to_widest (bottom
),
14089 wi::to_widest (op2
));
14090 if (wi::fits_to_tree_p (w
, TREE_TYPE (bottom
)))
14092 op2
= wide_int_to_tree (TREE_TYPE (bottom
), w
);
14093 return multiple_of_p (type
, op1
, op2
, nowrap
);
14096 return multiple_of_p (type
, op1
, bottom
, nowrap
);
14099 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14100 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14103 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14104 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14106 op1
= TREE_OPERAND (top
, 1);
14107 if (wi::to_widest (op1
) < TYPE_PRECISION (type
))
14110 = wi::one (TYPE_PRECISION (type
)) << wi::to_wide (op1
);
14111 return multiple_of_p (type
,
14112 wide_int_to_tree (type
, mul_op
), bottom
,
14120 /* If the addition or subtraction can wrap we cannot recurse further
14121 unless bottom is a power of two which is where wrapping does not
14124 && !TYPE_OVERFLOW_UNDEFINED (type
)
14125 && !integer_pow2p (bottom
))
14128 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14129 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14130 but 0xfffffffd is not. */
14131 op1
= TREE_OPERAND (top
, 1);
14132 if (TREE_CODE (top
) == PLUS_EXPR
14134 && TYPE_UNSIGNED (type
)
14135 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
14136 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
14138 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14139 precisely, so be conservative here checking if both op0 and op1
14140 are multiple of bottom. Note we check the second operand first
14141 since it's usually simpler. */
14142 return (multiple_of_p (type
, op1
, bottom
, nowrap
)
14143 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
));
14146 /* Can't handle conversions from non-integral or wider integral type. */
14147 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14148 || (TYPE_PRECISION (type
)
14149 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14151 /* NOWRAP only extends to operations in the outermost type so
14152 make sure to strip it off here. */
14153 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top
, 0)),
14154 TREE_OPERAND (top
, 0), bottom
, false);
14157 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
, nowrap
);
14160 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
, nowrap
)
14161 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
, nowrap
));
14164 if (TREE_CODE (bottom
) != INTEGER_CST
|| integer_zerop (bottom
))
14166 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14170 if (TREE_CODE (bottom
) == INTEGER_CST
14171 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
14172 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
14174 enum tree_code code
= gimple_assign_rhs_code (stmt
);
14176 /* Check for special cases to see if top is defined as multiple
14179 top = (X & ~(bottom - 1) ; bottom is power of 2
14185 if (code
== BIT_AND_EXPR
14186 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14187 && TREE_CODE (op2
) == INTEGER_CST
14188 && integer_pow2p (bottom
)
14189 && wi::multiple_of_p (wi::to_widest (op2
),
14190 wi::to_widest (bottom
), UNSIGNED
))
14193 op1
= gimple_assign_rhs1 (stmt
);
14194 if (code
== MINUS_EXPR
14195 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
14196 && TREE_CODE (op2
) == SSA_NAME
14197 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
14198 && gimple_code (stmt
) == GIMPLE_ASSIGN
14199 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
14200 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
14201 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
14208 if (POLY_INT_CST_P (top
) && poly_int_tree_p (bottom
))
14209 return multiple_p (wi::to_poly_widest (top
),
14210 wi::to_poly_widest (bottom
));
14216 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14217 This function returns true for integer expressions, and returns
14218 false if uncertain. */
14221 tree_expr_finite_p (const_tree x
)
14223 machine_mode mode
= element_mode (x
);
14224 if (!HONOR_NANS (mode
) && !HONOR_INFINITIES (mode
))
14226 switch (TREE_CODE (x
))
14229 return real_isfinite (TREE_REAL_CST_PTR (x
));
14231 return tree_expr_finite_p (TREE_REALPART (x
))
14232 && tree_expr_finite_p (TREE_IMAGPART (x
));
14237 case NON_LVALUE_EXPR
:
14240 return tree_expr_finite_p (TREE_OPERAND (x
, 0));
14243 return tree_expr_finite_p (TREE_OPERAND (x
, 0))
14244 && tree_expr_finite_p (TREE_OPERAND (x
, 1));
14246 return tree_expr_finite_p (TREE_OPERAND (x
, 1))
14247 && tree_expr_finite_p (TREE_OPERAND (x
, 2));
14249 switch (get_call_combined_fn (x
))
14253 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0));
14258 return tree_expr_finite_p (CALL_EXPR_ARG (x
, 0))
14259 && tree_expr_finite_p (CALL_EXPR_ARG (x
, 1));
14269 /* Return true if expression X evaluates to an infinity.
14270 This function returns false for integer expressions. */
14273 tree_expr_infinite_p (const_tree x
)
14275 if (!HONOR_INFINITIES (x
))
14277 switch (TREE_CODE (x
))
14280 return real_isinf (TREE_REAL_CST_PTR (x
));
14283 case NON_LVALUE_EXPR
:
14285 return tree_expr_infinite_p (TREE_OPERAND (x
, 0));
14287 return tree_expr_infinite_p (TREE_OPERAND (x
, 1))
14288 && tree_expr_infinite_p (TREE_OPERAND (x
, 2));
14294 /* Return true if expression X could evaluate to an infinity.
14295 This function returns false for integer expressions, and returns
14296 true if uncertain. */
14299 tree_expr_maybe_infinite_p (const_tree x
)
14301 if (!HONOR_INFINITIES (x
))
14303 switch (TREE_CODE (x
))
14306 return real_isinf (TREE_REAL_CST_PTR (x
));
14311 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 0));
14313 return tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 1))
14314 || tree_expr_maybe_infinite_p (TREE_OPERAND (x
, 2));
14320 /* Return true if expression X evaluates to a signaling NaN.
14321 This function returns false for integer expressions. */
14324 tree_expr_signaling_nan_p (const_tree x
)
14326 if (!HONOR_SNANS (x
))
14328 switch (TREE_CODE (x
))
14331 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14332 case NON_LVALUE_EXPR
:
14334 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 0));
14336 return tree_expr_signaling_nan_p (TREE_OPERAND (x
, 1))
14337 && tree_expr_signaling_nan_p (TREE_OPERAND (x
, 2));
14343 /* Return true if expression X could evaluate to a signaling NaN.
14344 This function returns false for integer expressions, and returns
14345 true if uncertain. */
14348 tree_expr_maybe_signaling_nan_p (const_tree x
)
14350 if (!HONOR_SNANS (x
))
14352 switch (TREE_CODE (x
))
14355 return real_issignaling_nan (TREE_REAL_CST_PTR (x
));
14361 case NON_LVALUE_EXPR
:
14363 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0));
14366 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 0))
14367 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1));
14369 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 1))
14370 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x
, 2));
14372 switch (get_call_combined_fn (x
))
14376 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0));
14381 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 0))
14382 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x
, 1));
14391 /* Return true if expression X evaluates to a NaN.
14392 This function returns false for integer expressions. */
14395 tree_expr_nan_p (const_tree x
)
14397 if (!HONOR_NANS (x
))
14399 switch (TREE_CODE (x
))
14402 return real_isnan (TREE_REAL_CST_PTR (x
));
14403 case NON_LVALUE_EXPR
:
14405 return tree_expr_nan_p (TREE_OPERAND (x
, 0));
14407 return tree_expr_nan_p (TREE_OPERAND (x
, 1))
14408 && tree_expr_nan_p (TREE_OPERAND (x
, 2));
14414 /* Return true if expression X could evaluate to a NaN.
14415 This function returns false for integer expressions, and returns
14416 true if uncertain. */
14419 tree_expr_maybe_nan_p (const_tree x
)
14421 if (!HONOR_NANS (x
))
14423 switch (TREE_CODE (x
))
14426 return real_isnan (TREE_REAL_CST_PTR (x
));
14432 return !tree_expr_finite_p (TREE_OPERAND (x
, 0))
14433 || !tree_expr_finite_p (TREE_OPERAND (x
, 1));
14437 case NON_LVALUE_EXPR
:
14439 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0));
14442 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 0))
14443 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1));
14445 return tree_expr_maybe_nan_p (TREE_OPERAND (x
, 1))
14446 || tree_expr_maybe_nan_p (TREE_OPERAND (x
, 2));
14448 switch (get_call_combined_fn (x
))
14452 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0));
14457 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 0))
14458 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x
, 1));
14467 /* Return true if expression X could evaluate to -0.0.
14468 This function returns true if uncertain. */
14471 tree_expr_maybe_real_minus_zero_p (const_tree x
)
14473 if (!HONOR_SIGNED_ZEROS (x
))
14475 switch (TREE_CODE (x
))
14478 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x
));
14483 case NON_LVALUE_EXPR
:
14485 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 0));
14487 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 1))
14488 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x
, 2));
14490 switch (get_call_combined_fn (x
))
14501 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14502 * but currently those predicates require tree and not const_tree. */
14506 #define tree_expr_nonnegative_warnv_p(X, Y) \
14507 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14509 #define RECURSE(X) \
14510 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14512 /* Return true if CODE or TYPE is known to be non-negative. */
14515 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14517 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14518 && truth_value_p (code
))
14519 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14520 have a signed:1 type (where the value is -1 and 0). */
14525 /* Return true if (CODE OP0) is known to be non-negative. If the return
14526 value is based on the assumption that signed overflow is undefined,
14527 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14528 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14531 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14532 bool *strict_overflow_p
, int depth
)
14534 if (TYPE_UNSIGNED (type
))
14540 /* We can't return 1 if flag_wrapv is set because
14541 ABS_EXPR<INT_MIN> = INT_MIN. */
14542 if (!ANY_INTEGRAL_TYPE_P (type
))
14544 if (TYPE_OVERFLOW_UNDEFINED (type
))
14546 *strict_overflow_p
= true;
14551 case NON_LVALUE_EXPR
:
14553 case FIX_TRUNC_EXPR
:
14554 return RECURSE (op0
);
14558 tree inner_type
= TREE_TYPE (op0
);
14559 tree outer_type
= type
;
14561 if (SCALAR_FLOAT_TYPE_P (outer_type
))
14563 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14564 return RECURSE (op0
);
14565 if (INTEGRAL_TYPE_P (inner_type
))
14567 if (TYPE_UNSIGNED (inner_type
))
14569 return RECURSE (op0
);
14572 else if (INTEGRAL_TYPE_P (outer_type
))
14574 if (SCALAR_FLOAT_TYPE_P (inner_type
))
14575 return RECURSE (op0
);
14576 if (INTEGRAL_TYPE_P (inner_type
))
14577 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14578 && TYPE_UNSIGNED (inner_type
);
14584 return tree_simple_nonnegative_warnv_p (code
, type
);
14587 /* We don't know sign of `t', so be conservative and return false. */
14591 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14592 value is based on the assumption that signed overflow is undefined,
14593 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14594 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14597 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14598 tree op1
, bool *strict_overflow_p
,
14601 if (TYPE_UNSIGNED (type
))
14606 case POINTER_PLUS_EXPR
:
14608 if (FLOAT_TYPE_P (type
))
14609 return RECURSE (op0
) && RECURSE (op1
);
14611 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14612 both unsigned and at least 2 bits shorter than the result. */
14613 if (TREE_CODE (type
) == INTEGER_TYPE
14614 && TREE_CODE (op0
) == NOP_EXPR
14615 && TREE_CODE (op1
) == NOP_EXPR
)
14617 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14618 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14619 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14620 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14622 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14623 TYPE_PRECISION (inner2
)) + 1;
14624 return prec
< TYPE_PRECISION (type
);
14630 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14632 /* x * x is always non-negative for floating point x
14633 or without overflow. */
14634 if (operand_equal_p (op0
, op1
, 0)
14635 || (RECURSE (op0
) && RECURSE (op1
)))
14637 if (ANY_INTEGRAL_TYPE_P (type
)
14638 && TYPE_OVERFLOW_UNDEFINED (type
))
14639 *strict_overflow_p
= true;
14644 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14645 both unsigned and their total bits is shorter than the result. */
14646 if (TREE_CODE (type
) == INTEGER_TYPE
14647 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14648 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
14650 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
14651 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
14653 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
14654 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
14657 bool unsigned0
= TYPE_UNSIGNED (inner0
);
14658 bool unsigned1
= TYPE_UNSIGNED (inner1
);
14660 if (TREE_CODE (op0
) == INTEGER_CST
)
14661 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
14663 if (TREE_CODE (op1
) == INTEGER_CST
)
14664 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
14666 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
14667 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
14669 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
14670 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
14671 : TYPE_PRECISION (inner0
);
14673 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
14674 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
14675 : TYPE_PRECISION (inner1
);
14677 return precision0
+ precision1
< TYPE_PRECISION (type
);
14683 return RECURSE (op0
) || RECURSE (op1
);
14686 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14688 if (tree_expr_maybe_nan_p (op0
) || tree_expr_maybe_nan_p (op1
))
14689 return RECURSE (op0
) && RECURSE (op1
);
14690 return RECURSE (op0
) || RECURSE (op1
);
14696 case TRUNC_DIV_EXPR
:
14697 case CEIL_DIV_EXPR
:
14698 case FLOOR_DIV_EXPR
:
14699 case ROUND_DIV_EXPR
:
14700 return RECURSE (op0
) && RECURSE (op1
);
14702 case TRUNC_MOD_EXPR
:
14703 return RECURSE (op0
);
14705 case FLOOR_MOD_EXPR
:
14706 return RECURSE (op1
);
14708 case CEIL_MOD_EXPR
:
14709 case ROUND_MOD_EXPR
:
14711 return tree_simple_nonnegative_warnv_p (code
, type
);
14714 /* We don't know sign of `t', so be conservative and return false. */
14718 /* Return true if T is known to be non-negative. If the return
14719 value is based on the assumption that signed overflow is undefined,
14720 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14721 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14724 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14726 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14729 switch (TREE_CODE (t
))
14732 return tree_int_cst_sgn (t
) >= 0;
14735 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
14738 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
14741 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
14744 /* Limit the depth of recursion to avoid quadratic behavior.
14745 This is expected to catch almost all occurrences in practice.
14746 If this code misses important cases that unbounded recursion
14747 would not, passes that need this information could be revised
14748 to provide it through dataflow propagation. */
14749 return (!name_registered_for_update_p (t
)
14750 && depth
< param_max_ssa_name_query_depth
14751 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
14752 strict_overflow_p
, depth
));
14755 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
14759 /* Return true if T is known to be non-negative. If the return
14760 value is based on the assumption that signed overflow is undefined,
14761 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14762 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14765 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
14766 bool *strict_overflow_p
, int depth
)
14797 case CFN_BUILT_IN_BSWAP16
:
14798 case CFN_BUILT_IN_BSWAP32
:
14799 case CFN_BUILT_IN_BSWAP64
:
14800 case CFN_BUILT_IN_BSWAP128
:
14806 /* sqrt(-0.0) is -0.0. */
14807 if (!HONOR_SIGNED_ZEROS (type
))
14809 return RECURSE (arg0
);
14841 CASE_CFN_LLRINT_FN
:
14843 CASE_CFN_LLROUND_FN
:
14847 CASE_CFN_LROUND_FN
:
14850 CASE_CFN_NEARBYINT
:
14851 CASE_CFN_NEARBYINT_FN
:
14856 CASE_CFN_ROUNDEVEN
:
14857 CASE_CFN_ROUNDEVEN_FN
:
14860 CASE_CFN_SCALBLN_FN
:
14862 CASE_CFN_SCALBN_FN
:
14864 CASE_CFN_SIGNIFICAND
:
14871 /* True if the 1st argument is nonnegative. */
14872 return RECURSE (arg0
);
14876 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14877 things. In the presence of sNaNs, we're only guaranteed to be
14878 non-negative if both operands are non-negative. In the presence
14879 of qNaNs, we're non-negative if either operand is non-negative
14880 and can't be a qNaN, or if both operands are non-negative. */
14881 if (tree_expr_maybe_signaling_nan_p (arg0
) ||
14882 tree_expr_maybe_signaling_nan_p (arg1
))
14883 return RECURSE (arg0
) && RECURSE (arg1
);
14884 return RECURSE (arg0
) ? (!tree_expr_maybe_nan_p (arg0
)
14887 && !tree_expr_maybe_nan_p (arg1
));
14891 /* True if the 1st AND 2nd arguments are nonnegative. */
14892 return RECURSE (arg0
) && RECURSE (arg1
);
14895 CASE_CFN_COPYSIGN_FN
:
14896 /* True if the 2nd argument is nonnegative. */
14897 return RECURSE (arg1
);
14900 /* True if the 1st argument is nonnegative or the second
14901 argument is an even integer. */
14902 if (TREE_CODE (arg1
) == INTEGER_CST
14903 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
14905 return RECURSE (arg0
);
14909 /* True if the 1st argument is nonnegative or the second
14910 argument is an even integer valued real. */
14911 if (TREE_CODE (arg1
) == REAL_CST
)
14916 c
= TREE_REAL_CST (arg1
);
14917 n
= real_to_integer (&c
);
14920 REAL_VALUE_TYPE cint
;
14921 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
14922 if (real_identical (&c
, &cint
))
14926 return RECURSE (arg0
);
14931 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
14934 /* Return true if T is known to be non-negative. If the return
14935 value is based on the assumption that signed overflow is undefined,
14936 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14937 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14940 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
14942 enum tree_code code
= TREE_CODE (t
);
14943 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14950 tree temp
= TARGET_EXPR_SLOT (t
);
14951 t
= TARGET_EXPR_INITIAL (t
);
14953 /* If the initializer is non-void, then it's a normal expression
14954 that will be assigned to the slot. */
14955 if (!VOID_TYPE_P (TREE_TYPE (t
)))
14956 return RECURSE (t
);
14958 /* Otherwise, the initializer sets the slot in some way. One common
14959 way is an assignment statement at the end of the initializer. */
14962 if (TREE_CODE (t
) == BIND_EXPR
)
14963 t
= expr_last (BIND_EXPR_BODY (t
));
14964 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
14965 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
14966 t
= expr_last (TREE_OPERAND (t
, 0));
14967 else if (TREE_CODE (t
) == STATEMENT_LIST
)
14972 if (TREE_CODE (t
) == MODIFY_EXPR
14973 && TREE_OPERAND (t
, 0) == temp
)
14974 return RECURSE (TREE_OPERAND (t
, 1));
14981 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
14982 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
14984 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
14985 get_call_combined_fn (t
),
14988 strict_overflow_p
, depth
);
14990 case COMPOUND_EXPR
:
14992 return RECURSE (TREE_OPERAND (t
, 1));
14995 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
14998 return RECURSE (TREE_OPERAND (t
, 0));
15001 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
15006 #undef tree_expr_nonnegative_warnv_p
15008 /* Return true if T is known to be non-negative. If the return
15009 value is based on the assumption that signed overflow is undefined,
15010 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15011 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15014 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
15016 enum tree_code code
;
15017 if (t
== error_mark_node
)
15020 code
= TREE_CODE (t
);
15021 switch (TREE_CODE_CLASS (code
))
15024 case tcc_comparison
:
15025 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15027 TREE_OPERAND (t
, 0),
15028 TREE_OPERAND (t
, 1),
15029 strict_overflow_p
, depth
);
15032 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15034 TREE_OPERAND (t
, 0),
15035 strict_overflow_p
, depth
);
15038 case tcc_declaration
:
15039 case tcc_reference
:
15040 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15048 case TRUTH_AND_EXPR
:
15049 case TRUTH_OR_EXPR
:
15050 case TRUTH_XOR_EXPR
:
15051 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15053 TREE_OPERAND (t
, 0),
15054 TREE_OPERAND (t
, 1),
15055 strict_overflow_p
, depth
);
15056 case TRUTH_NOT_EXPR
:
15057 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15059 TREE_OPERAND (t
, 0),
15060 strict_overflow_p
, depth
);
15066 case WITH_SIZE_EXPR
:
15068 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15071 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
15075 /* Return true if `t' is known to be non-negative. Handle warnings
15076 about undefined signed overflow. */
15079 tree_expr_nonnegative_p (tree t
)
15081 bool ret
, strict_overflow_p
;
15083 strict_overflow_p
= false;
15084 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15085 if (strict_overflow_p
)
15086 fold_overflow_warning (("assuming signed overflow does not occur when "
15087 "determining that expression is always "
15089 WARN_STRICT_OVERFLOW_MISC
);
15094 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15095 For floating point we further ensure that T is not denormal.
15096 Similar logic is present in nonzero_address in rtlanal.h.
15098 If the return value is based on the assumption that signed overflow
15099 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15100 change *STRICT_OVERFLOW_P. */
15103 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15104 bool *strict_overflow_p
)
15109 return tree_expr_nonzero_warnv_p (op0
,
15110 strict_overflow_p
);
15114 tree inner_type
= TREE_TYPE (op0
);
15115 tree outer_type
= type
;
15117 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15118 && tree_expr_nonzero_warnv_p (op0
,
15119 strict_overflow_p
));
15123 case NON_LVALUE_EXPR
:
15124 return tree_expr_nonzero_warnv_p (op0
,
15125 strict_overflow_p
);
15134 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15135 For floating point we further ensure that T is not denormal.
15136 Similar logic is present in nonzero_address in rtlanal.h.
15138 If the return value is based on the assumption that signed overflow
15139 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15140 change *STRICT_OVERFLOW_P. */
15143 tree_binary_nonzero_warnv_p (enum tree_code code
,
15146 tree op1
, bool *strict_overflow_p
)
15148 bool sub_strict_overflow_p
;
15151 case POINTER_PLUS_EXPR
:
15153 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
15155 /* With the presence of negative values it is hard
15156 to say something. */
15157 sub_strict_overflow_p
= false;
15158 if (!tree_expr_nonnegative_warnv_p (op0
,
15159 &sub_strict_overflow_p
)
15160 || !tree_expr_nonnegative_warnv_p (op1
,
15161 &sub_strict_overflow_p
))
15163 /* One of operands must be positive and the other non-negative. */
15164 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15165 overflows, on a twos-complement machine the sum of two
15166 nonnegative numbers can never be zero. */
15167 return (tree_expr_nonzero_warnv_p (op0
,
15169 || tree_expr_nonzero_warnv_p (op1
,
15170 strict_overflow_p
));
15175 if (TYPE_OVERFLOW_UNDEFINED (type
))
15177 if (tree_expr_nonzero_warnv_p (op0
,
15179 && tree_expr_nonzero_warnv_p (op1
,
15180 strict_overflow_p
))
15182 *strict_overflow_p
= true;
15189 sub_strict_overflow_p
= false;
15190 if (tree_expr_nonzero_warnv_p (op0
,
15191 &sub_strict_overflow_p
)
15192 && tree_expr_nonzero_warnv_p (op1
,
15193 &sub_strict_overflow_p
))
15195 if (sub_strict_overflow_p
)
15196 *strict_overflow_p
= true;
15201 sub_strict_overflow_p
= false;
15202 if (tree_expr_nonzero_warnv_p (op0
,
15203 &sub_strict_overflow_p
))
15205 if (sub_strict_overflow_p
)
15206 *strict_overflow_p
= true;
15208 /* When both operands are nonzero, then MAX must be too. */
15209 if (tree_expr_nonzero_warnv_p (op1
,
15210 strict_overflow_p
))
15213 /* MAX where operand 0 is positive is positive. */
15214 return tree_expr_nonnegative_warnv_p (op0
,
15215 strict_overflow_p
);
15217 /* MAX where operand 1 is positive is positive. */
15218 else if (tree_expr_nonzero_warnv_p (op1
,
15219 &sub_strict_overflow_p
)
15220 && tree_expr_nonnegative_warnv_p (op1
,
15221 &sub_strict_overflow_p
))
15223 if (sub_strict_overflow_p
)
15224 *strict_overflow_p
= true;
15230 return (tree_expr_nonzero_warnv_p (op1
,
15232 || tree_expr_nonzero_warnv_p (op0
,
15233 strict_overflow_p
));
15242 /* Return true when T is an address and is known to be nonzero.
15243 For floating point we further ensure that T is not denormal.
15244 Similar logic is present in nonzero_address in rtlanal.h.
15246 If the return value is based on the assumption that signed overflow
15247 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15248 change *STRICT_OVERFLOW_P. */
15251 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15253 bool sub_strict_overflow_p
;
15254 switch (TREE_CODE (t
))
15257 return !integer_zerop (t
);
15261 tree base
= TREE_OPERAND (t
, 0);
15263 if (!DECL_P (base
))
15264 base
= get_base_address (base
);
15266 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
15267 base
= TARGET_EXPR_SLOT (base
);
15272 /* For objects in symbol table check if we know they are non-zero.
15273 Don't do anything for variables and functions before symtab is built;
15274 it is quite possible that they will be declared weak later. */
15275 int nonzero_addr
= maybe_nonzero_address (base
);
15276 if (nonzero_addr
>= 0)
15277 return nonzero_addr
;
15279 /* Constants are never weak. */
15280 if (CONSTANT_CLASS_P (base
))
15287 sub_strict_overflow_p
= false;
15288 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15289 &sub_strict_overflow_p
)
15290 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15291 &sub_strict_overflow_p
))
15293 if (sub_strict_overflow_p
)
15294 *strict_overflow_p
= true;
15300 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
15302 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
15310 #define integer_valued_real_p(X) \
15311 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15313 #define RECURSE(X) \
15314 ((integer_valued_real_p) (X, depth + 1))
15316 /* Return true if the floating point result of (CODE OP0) has an
15317 integer value. We also allow +Inf, -Inf and NaN to be considered
15318 integer values. Return false for signaling NaN.
15320 DEPTH is the current nesting depth of the query. */
15323 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
15331 return RECURSE (op0
);
15335 tree type
= TREE_TYPE (op0
);
15336 if (TREE_CODE (type
) == INTEGER_TYPE
)
15338 if (SCALAR_FLOAT_TYPE_P (type
))
15339 return RECURSE (op0
);
15349 /* Return true if the floating point result of (CODE OP0 OP1) has an
15350 integer value. We also allow +Inf, -Inf and NaN to be considered
15351 integer values. Return false for signaling NaN.
15353 DEPTH is the current nesting depth of the query. */
15356 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
15365 return RECURSE (op0
) && RECURSE (op1
);
15373 /* Return true if the floating point result of calling FNDECL with arguments
15374 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15375 considered integer values. Return false for signaling NaN. If FNDECL
15376 takes fewer than 2 arguments, the remaining ARGn are null.
15378 DEPTH is the current nesting depth of the query. */
15381 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
15389 CASE_CFN_NEARBYINT
:
15390 CASE_CFN_NEARBYINT_FN
:
15395 CASE_CFN_ROUNDEVEN
:
15396 CASE_CFN_ROUNDEVEN_FN
:
15405 return RECURSE (arg0
) && RECURSE (arg1
);
15413 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15414 has an integer value. We also allow +Inf, -Inf and NaN to be
15415 considered integer values. Return false for signaling NaN.
15417 DEPTH is the current nesting depth of the query. */
15420 integer_valued_real_single_p (tree t
, int depth
)
15422 switch (TREE_CODE (t
))
15425 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
15428 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
15431 /* Limit the depth of recursion to avoid quadratic behavior.
15432 This is expected to catch almost all occurrences in practice.
15433 If this code misses important cases that unbounded recursion
15434 would not, passes that need this information could be revised
15435 to provide it through dataflow propagation. */
15436 return (!name_registered_for_update_p (t
)
15437 && depth
< param_max_ssa_name_query_depth
15438 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
15447 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15448 has an integer value. We also allow +Inf, -Inf and NaN to be
15449 considered integer values. Return false for signaling NaN.
15451 DEPTH is the current nesting depth of the query. */
15454 integer_valued_real_invalid_p (tree t
, int depth
)
15456 switch (TREE_CODE (t
))
15458 case COMPOUND_EXPR
:
15461 return RECURSE (TREE_OPERAND (t
, 1));
15464 return RECURSE (TREE_OPERAND (t
, 0));
15473 #undef integer_valued_real_p
15475 /* Return true if the floating point expression T has an integer value.
15476 We also allow +Inf, -Inf and NaN to be considered integer values.
15477 Return false for signaling NaN.
15479 DEPTH is the current nesting depth of the query. */
15482 integer_valued_real_p (tree t
, int depth
)
15484 if (t
== error_mark_node
)
15487 STRIP_ANY_LOCATION_WRAPPER (t
);
15489 tree_code code
= TREE_CODE (t
);
15490 switch (TREE_CODE_CLASS (code
))
15493 case tcc_comparison
:
15494 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
15495 TREE_OPERAND (t
, 1), depth
);
15498 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
15501 case tcc_declaration
:
15502 case tcc_reference
:
15503 return integer_valued_real_single_p (t
, depth
);
15513 return integer_valued_real_single_p (t
, depth
);
15517 tree arg0
= (call_expr_nargs (t
) > 0
15518 ? CALL_EXPR_ARG (t
, 0)
15520 tree arg1
= (call_expr_nargs (t
) > 1
15521 ? CALL_EXPR_ARG (t
, 1)
15523 return integer_valued_real_call_p (get_call_combined_fn (t
),
15524 arg0
, arg1
, depth
);
15528 return integer_valued_real_invalid_p (t
, depth
);
15532 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15533 attempt to fold the expression to a constant without modifying TYPE,
15536 If the expression could be simplified to a constant, then return
15537 the constant. If the expression would not be simplified to a
15538 constant, then return NULL_TREE. */
15541 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15543 tree tem
= fold_binary (code
, type
, op0
, op1
);
15544 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15547 /* Given the components of a unary expression CODE, TYPE and OP0,
15548 attempt to fold the expression to a constant without modifying
15551 If the expression could be simplified to a constant, then return
15552 the constant. If the expression would not be simplified to a
15553 constant, then return NULL_TREE. */
15556 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15558 tree tem
= fold_unary (code
, type
, op0
);
15559 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15562 /* If EXP represents referencing an element in a constant string
15563 (either via pointer arithmetic or array indexing), return the
15564 tree representing the value accessed, otherwise return NULL. */
15567 fold_read_from_constant_string (tree exp
)
15569 if ((INDIRECT_REF_P (exp
)
15570 || TREE_CODE (exp
) == ARRAY_REF
)
15571 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15573 tree exp1
= TREE_OPERAND (exp
, 0);
15576 location_t loc
= EXPR_LOCATION (exp
);
15578 if (INDIRECT_REF_P (exp
))
15579 string
= string_constant (exp1
, &index
, NULL
, NULL
);
15582 tree low_bound
= array_ref_low_bound (exp
);
15583 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15585 /* Optimize the special-case of a zero lower bound.
15587 We convert the low_bound to sizetype to avoid some problems
15588 with constant folding. (E.g. suppose the lower bound is 1,
15589 and its mode is QI. Without the conversion,l (ARRAY
15590 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15591 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15592 if (! integer_zerop (low_bound
))
15593 index
= size_diffop_loc (loc
, index
,
15594 fold_convert_loc (loc
, sizetype
, low_bound
));
15599 scalar_int_mode char_mode
;
15601 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15602 && TREE_CODE (string
) == STRING_CST
15603 && tree_fits_uhwi_p (index
)
15604 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15605 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
15607 && GET_MODE_SIZE (char_mode
) == 1)
15608 return build_int_cst_type (TREE_TYPE (exp
),
15609 (TREE_STRING_POINTER (string
)
15610 [TREE_INT_CST_LOW (index
)]));
15615 /* Folds a read from vector element at IDX of vector ARG. */
15618 fold_read_from_vector (tree arg
, poly_uint64 idx
)
15620 unsigned HOST_WIDE_INT i
;
15621 if (known_lt (idx
, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)))
15622 && known_ge (idx
, 0u)
15623 && idx
.is_constant (&i
))
15625 if (TREE_CODE (arg
) == VECTOR_CST
)
15626 return VECTOR_CST_ELT (arg
, i
);
15627 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
15629 if (CONSTRUCTOR_NELTS (arg
)
15630 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg
, 0)->value
)))
15632 if (i
>= CONSTRUCTOR_NELTS (arg
))
15633 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg
)));
15634 return CONSTRUCTOR_ELT (arg
, i
)->value
;
15640 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15641 an integer constant, real, or fixed-point constant.
15643 TYPE is the type of the result. */
15646 fold_negate_const (tree arg0
, tree type
)
15648 tree t
= NULL_TREE
;
15650 switch (TREE_CODE (arg0
))
15653 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15658 FIXED_VALUE_TYPE f
;
15659 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
15660 &(TREE_FIXED_CST (arg0
)), NULL
,
15661 TYPE_SATURATING (type
));
15662 t
= build_fixed (type
, f
);
15663 /* Propagate overflow flags. */
15664 if (overflow_p
| TREE_OVERFLOW (arg0
))
15665 TREE_OVERFLOW (t
) = 1;
15670 if (poly_int_tree_p (arg0
))
15672 wi::overflow_type overflow
;
15673 poly_wide_int res
= wi::neg (wi::to_poly_wide (arg0
), &overflow
);
15674 t
= force_fit_type (type
, res
, 1,
15675 (overflow
&& ! TYPE_UNSIGNED (type
))
15676 || TREE_OVERFLOW (arg0
));
15680 gcc_unreachable ();
15686 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15687 an integer constant or real constant.
15689 TYPE is the type of the result. */
15692 fold_abs_const (tree arg0
, tree type
)
15694 tree t
= NULL_TREE
;
15696 switch (TREE_CODE (arg0
))
15700 /* If the value is unsigned or non-negative, then the absolute value
15701 is the same as the ordinary value. */
15702 wide_int val
= wi::to_wide (arg0
);
15703 wi::overflow_type overflow
= wi::OVF_NONE
;
15704 if (!wi::neg_p (val
, TYPE_SIGN (TREE_TYPE (arg0
))))
15707 /* If the value is negative, then the absolute value is
15710 val
= wi::neg (val
, &overflow
);
15712 /* Force to the destination type, set TREE_OVERFLOW for signed
15714 t
= force_fit_type (type
, val
, 1, overflow
| TREE_OVERFLOW (arg0
));
15719 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
15720 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15726 gcc_unreachable ();
15732 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15733 constant. TYPE is the type of the result. */
15736 fold_not_const (const_tree arg0
, tree type
)
15738 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
15740 return force_fit_type (type
, ~wi::to_wide (arg0
), 0, TREE_OVERFLOW (arg0
));
15743 /* Given CODE, a relational operator, the target type, TYPE and two
15744 constant operands OP0 and OP1, return the result of the
15745 relational operation. If the result is not a compile time
15746 constant, then return NULL_TREE. */
15749 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
15751 int result
, invert
;
15753 /* From here on, the only cases we handle are when the result is
15754 known to be a constant. */
15756 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
15758 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
15759 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
15761 /* Handle the cases where either operand is a NaN. */
15762 if (real_isnan (c0
) || real_isnan (c1
))
15772 case UNORDERED_EXPR
:
15786 if (flag_trapping_math
)
15792 gcc_unreachable ();
15795 return constant_boolean_node (result
, type
);
15798 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
15801 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
15803 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
15804 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
15805 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
15808 /* Handle equality/inequality of complex constants. */
15809 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
15811 tree rcond
= fold_relational_const (code
, type
,
15812 TREE_REALPART (op0
),
15813 TREE_REALPART (op1
));
15814 tree icond
= fold_relational_const (code
, type
,
15815 TREE_IMAGPART (op0
),
15816 TREE_IMAGPART (op1
));
15817 if (code
== EQ_EXPR
)
15818 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
15819 else if (code
== NE_EXPR
)
15820 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
15825 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
15827 if (!VECTOR_TYPE_P (type
))
15829 /* Have vector comparison with scalar boolean result. */
15830 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
15831 && known_eq (VECTOR_CST_NELTS (op0
),
15832 VECTOR_CST_NELTS (op1
)));
15833 unsigned HOST_WIDE_INT nunits
;
15834 if (!VECTOR_CST_NELTS (op0
).is_constant (&nunits
))
15836 for (unsigned i
= 0; i
< nunits
; i
++)
15838 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15839 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15840 tree tmp
= fold_relational_const (EQ_EXPR
, type
, elem0
, elem1
);
15841 if (tmp
== NULL_TREE
)
15843 if (integer_zerop (tmp
))
15844 return constant_boolean_node (code
== NE_EXPR
, type
);
15846 return constant_boolean_node (code
== EQ_EXPR
, type
);
15848 tree_vector_builder elts
;
15849 if (!elts
.new_binary_operation (type
, op0
, op1
, false))
15851 unsigned int count
= elts
.encoded_nelts ();
15852 for (unsigned i
= 0; i
< count
; i
++)
15854 tree elem_type
= TREE_TYPE (type
);
15855 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15856 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15858 tree tem
= fold_relational_const (code
, elem_type
,
15861 if (tem
== NULL_TREE
)
15864 elts
.quick_push (build_int_cst (elem_type
,
15865 integer_zerop (tem
) ? 0 : -1));
15868 return elts
.build ();
15871 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15873 To compute GT, swap the arguments and do LT.
15874 To compute GE, do LT and invert the result.
15875 To compute LE, swap the arguments, do LT and invert the result.
15876 To compute NE, do EQ and invert the result.
15878 Therefore, the code below must handle only EQ and LT. */
15880 if (code
== LE_EXPR
|| code
== GT_EXPR
)
15882 std::swap (op0
, op1
);
15883 code
= swap_tree_comparison (code
);
15886 /* Note that it is safe to invert for real values here because we
15887 have already handled the one case that it matters. */
15890 if (code
== NE_EXPR
|| code
== GE_EXPR
)
15893 code
= invert_tree_comparison (code
, false);
15896 /* Compute a result for LT or EQ if args permit;
15897 Otherwise return T. */
15898 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
15900 if (code
== EQ_EXPR
)
15901 result
= tree_int_cst_equal (op0
, op1
);
15903 result
= tree_int_cst_lt (op0
, op1
);
15910 return constant_boolean_node (result
, type
);
15913 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15914 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15918 fold_build_cleanup_point_expr (tree type
, tree expr
)
15920 /* If the expression does not have side effects then we don't have to wrap
15921 it with a cleanup point expression. */
15922 if (!TREE_SIDE_EFFECTS (expr
))
15925 /* If the expression is a return, check to see if the expression inside the
15926 return has no side effects or the right hand side of the modify expression
15927 inside the return. If either don't have side effects set we don't need to
15928 wrap the expression in a cleanup point expression. Note we don't check the
15929 left hand side of the modify because it should always be a return decl. */
15930 if (TREE_CODE (expr
) == RETURN_EXPR
)
15932 tree op
= TREE_OPERAND (expr
, 0);
15933 if (!op
|| !TREE_SIDE_EFFECTS (op
))
15935 op
= TREE_OPERAND (op
, 1);
15936 if (!TREE_SIDE_EFFECTS (op
))
15940 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
15943 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15944 of an indirection through OP0, or NULL_TREE if no simplification is
15948 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
15952 poly_uint64 const_op01
;
15955 subtype
= TREE_TYPE (sub
);
15956 if (!POINTER_TYPE_P (subtype
)
15957 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
15960 if (TREE_CODE (sub
) == ADDR_EXPR
)
15962 tree op
= TREE_OPERAND (sub
, 0);
15963 tree optype
= TREE_TYPE (op
);
15965 /* *&CONST_DECL -> to the value of the const decl. */
15966 if (TREE_CODE (op
) == CONST_DECL
)
15967 return DECL_INITIAL (op
);
15968 /* *&p => p; make sure to handle *&"str"[cst] here. */
15969 if (type
== optype
)
15971 tree fop
= fold_read_from_constant_string (op
);
15977 /* *(foo *)&fooarray => fooarray[0] */
15978 else if (TREE_CODE (optype
) == ARRAY_TYPE
15979 && type
== TREE_TYPE (optype
)
15980 && (!in_gimple_form
15981 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
15983 tree type_domain
= TYPE_DOMAIN (optype
);
15984 tree min_val
= size_zero_node
;
15985 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15986 min_val
= TYPE_MIN_VALUE (type_domain
);
15988 && TREE_CODE (min_val
) != INTEGER_CST
)
15990 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
15991 NULL_TREE
, NULL_TREE
);
15993 /* *(foo *)&complexfoo => __real__ complexfoo */
15994 else if (TREE_CODE (optype
) == COMPLEX_TYPE
15995 && type
== TREE_TYPE (optype
))
15996 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
15997 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15998 else if (VECTOR_TYPE_P (optype
)
15999 && type
== TREE_TYPE (optype
))
16001 tree part_width
= TYPE_SIZE (type
);
16002 tree index
= bitsize_int (0);
16003 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
16008 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16009 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
16011 tree op00
= TREE_OPERAND (sub
, 0);
16012 tree op01
= TREE_OPERAND (sub
, 1);
16015 if (TREE_CODE (op00
) == ADDR_EXPR
)
16018 op00
= TREE_OPERAND (op00
, 0);
16019 op00type
= TREE_TYPE (op00
);
16021 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16022 if (VECTOR_TYPE_P (op00type
)
16023 && type
== TREE_TYPE (op00type
)
16024 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16025 but we want to treat offsets with MSB set as negative.
16026 For the code below negative offsets are invalid and
16027 TYPE_SIZE of the element is something unsigned, so
16028 check whether op01 fits into poly_int64, which implies
16029 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16030 then just use poly_uint64 because we want to treat the
16031 value as unsigned. */
16032 && tree_fits_poly_int64_p (op01
))
16034 tree part_width
= TYPE_SIZE (type
);
16035 poly_uint64 max_offset
16036 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
16037 * TYPE_VECTOR_SUBPARTS (op00type
));
16038 if (known_lt (const_op01
, max_offset
))
16040 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
16041 return fold_build3_loc (loc
,
16042 BIT_FIELD_REF
, type
, op00
,
16043 part_width
, index
);
16046 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16047 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16048 && type
== TREE_TYPE (op00type
))
16050 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
16052 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16054 /* ((foo *)&fooarray)[1] => fooarray[1] */
16055 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16056 && type
== TREE_TYPE (op00type
))
16058 tree type_domain
= TYPE_DOMAIN (op00type
);
16059 tree min_val
= size_zero_node
;
16060 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16061 min_val
= TYPE_MIN_VALUE (type_domain
);
16062 poly_uint64 type_size
, index
;
16063 if (poly_int_tree_p (min_val
)
16064 && poly_int_tree_p (TYPE_SIZE_UNIT (type
), &type_size
)
16065 && multiple_p (const_op01
, type_size
, &index
))
16067 poly_offset_int off
= index
+ wi::to_poly_offset (min_val
);
16068 op01
= wide_int_to_tree (sizetype
, off
);
16069 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16070 NULL_TREE
, NULL_TREE
);
16076 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16077 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16078 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16079 && (!in_gimple_form
16080 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16083 tree min_val
= size_zero_node
;
16084 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16085 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16086 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16087 min_val
= TYPE_MIN_VALUE (type_domain
);
16089 && TREE_CODE (min_val
) != INTEGER_CST
)
16091 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16098 /* Builds an expression for an indirection through T, simplifying some
16102 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16104 tree type
= TREE_TYPE (TREE_TYPE (t
));
16105 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16110 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16113 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16116 fold_indirect_ref_loc (location_t loc
, tree t
)
16118 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16126 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16127 whose result is ignored. The type of the returned tree need not be
16128 the same as the original expression. */
16131 fold_ignored_result (tree t
)
16133 if (!TREE_SIDE_EFFECTS (t
))
16134 return integer_zero_node
;
16137 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16140 t
= TREE_OPERAND (t
, 0);
16144 case tcc_comparison
:
16145 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16146 t
= TREE_OPERAND (t
, 0);
16147 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16148 t
= TREE_OPERAND (t
, 1);
16153 case tcc_expression
:
16154 switch (TREE_CODE (t
))
16156 case COMPOUND_EXPR
:
16157 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16159 t
= TREE_OPERAND (t
, 0);
16163 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16164 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16166 t
= TREE_OPERAND (t
, 0);
16179 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16182 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
16184 tree div
= NULL_TREE
;
16189 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16190 have to do anything. Only do this when we are not given a const,
16191 because in that case, this check is more expensive than just
16193 if (TREE_CODE (value
) != INTEGER_CST
)
16195 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16197 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16201 /* If divisor is a power of two, simplify this to bit manipulation. */
16202 if (pow2_or_zerop (divisor
))
16204 if (TREE_CODE (value
) == INTEGER_CST
)
16206 wide_int val
= wi::to_wide (value
);
16209 if ((val
& (divisor
- 1)) == 0)
16212 overflow_p
= TREE_OVERFLOW (value
);
16213 val
+= divisor
- 1;
16214 val
&= (int) -divisor
;
16218 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16224 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16225 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16226 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
16227 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16233 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16234 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16235 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16241 /* Likewise, but round down. */
16244 round_down_loc (location_t loc
, tree value
, int divisor
)
16246 tree div
= NULL_TREE
;
16248 gcc_assert (divisor
> 0);
16252 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16253 have to do anything. Only do this when we are not given a const,
16254 because in that case, this check is more expensive than just
16256 if (TREE_CODE (value
) != INTEGER_CST
)
16258 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16260 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16264 /* If divisor is a power of two, simplify this to bit manipulation. */
16265 if (pow2_or_zerop (divisor
))
16269 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16270 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16275 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16276 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16277 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16283 /* Returns the pointer to the base of the object addressed by EXP and
16284 extracts the information about the offset of the access, storing it
16285 to PBITPOS and POFFSET. */
16288 split_address_to_core_and_offset (tree exp
,
16289 poly_int64_pod
*pbitpos
, tree
*poffset
)
16293 int unsignedp
, reversep
, volatilep
;
16294 poly_int64 bitsize
;
16295 location_t loc
= EXPR_LOCATION (exp
);
16297 if (TREE_CODE (exp
) == SSA_NAME
)
16298 if (gassign
*def
= dyn_cast
<gassign
*> (SSA_NAME_DEF_STMT (exp
)))
16299 if (gimple_assign_rhs_code (def
) == ADDR_EXPR
)
16300 exp
= gimple_assign_rhs1 (def
);
16302 if (TREE_CODE (exp
) == ADDR_EXPR
)
16304 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16305 poffset
, &mode
, &unsignedp
, &reversep
,
16307 core
= build_fold_addr_expr_loc (loc
, core
);
16309 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
16311 core
= TREE_OPERAND (exp
, 0);
16314 *poffset
= TREE_OPERAND (exp
, 1);
16315 if (poly_int_tree_p (*poffset
))
16317 poly_offset_int tem
16318 = wi::sext (wi::to_poly_offset (*poffset
),
16319 TYPE_PRECISION (TREE_TYPE (*poffset
)));
16320 tem
<<= LOG2_BITS_PER_UNIT
;
16321 if (tem
.to_shwi (pbitpos
))
16322 *poffset
= NULL_TREE
;
16329 *poffset
= NULL_TREE
;
16335 /* Returns true if addresses of E1 and E2 differ by a constant, false
16336 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16339 ptr_difference_const (tree e1
, tree e2
, poly_int64_pod
*diff
)
16342 poly_int64 bitpos1
, bitpos2
;
16343 tree toffset1
, toffset2
, tdiff
, type
;
16345 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16346 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16348 poly_int64 bytepos1
, bytepos2
;
16349 if (!multiple_p (bitpos1
, BITS_PER_UNIT
, &bytepos1
)
16350 || !multiple_p (bitpos2
, BITS_PER_UNIT
, &bytepos2
)
16351 || !operand_equal_p (core1
, core2
, 0))
16354 if (toffset1
&& toffset2
)
16356 type
= TREE_TYPE (toffset1
);
16357 if (type
!= TREE_TYPE (toffset2
))
16358 toffset2
= fold_convert (type
, toffset2
);
16360 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16361 if (!cst_and_fits_in_hwi (tdiff
))
16364 *diff
= int_cst_value (tdiff
);
16366 else if (toffset1
|| toffset2
)
16368 /* If only one of the offsets is non-constant, the difference cannot
16375 *diff
+= bytepos1
- bytepos2
;
16379 /* Return OFF converted to a pointer offset type suitable as offset for
16380 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16382 convert_to_ptrofftype_loc (location_t loc
, tree off
)
16384 if (ptrofftype_p (TREE_TYPE (off
)))
16386 return fold_convert_loc (loc
, sizetype
, off
);
16389 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16391 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16393 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16394 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16397 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16399 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16401 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16402 ptr
, size_int (off
));
16405 /* Return a pointer to a NUL-terminated string containing the sequence
16406 of bytes corresponding to the representation of the object referred to
16407 by SRC (or a subsequence of such bytes within it if SRC is a reference
16408 to an initialized constant array plus some constant offset).
16409 Set *STRSIZE the number of bytes in the constant sequence including
16410 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16411 where A is the array that stores the constant sequence that SRC points
16412 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16413 need not point to a string or even an array of characters but may point
16414 to an object of any type. */
16417 getbyterep (tree src
, unsigned HOST_WIDE_INT
*strsize
)
16419 /* The offset into the array A storing the string, and A's byte size. */
16427 src
= byte_representation (src
, &offset_node
, &mem_size
, NULL
);
16429 src
= string_constant (src
, &offset_node
, &mem_size
, NULL
);
16433 unsigned HOST_WIDE_INT offset
= 0;
16434 if (offset_node
!= NULL_TREE
)
16436 if (!tree_fits_uhwi_p (offset_node
))
16439 offset
= tree_to_uhwi (offset_node
);
16442 if (!tree_fits_uhwi_p (mem_size
))
16445 /* ARRAY_SIZE is the byte size of the array the constant sequence
16446 is stored in and equal to sizeof A. INIT_BYTES is the number
16447 of bytes in the constant sequence used to initialize the array,
16448 including any embedded NULs as well as the terminating NUL (for
16449 strings), but not including any trailing zeros/NULs past
16450 the terminating one appended implicitly to a string literal to
16451 zero out the remainder of the array it's stored in. For example,
16453 const char a[7] = "abc\0d";
16454 n = strlen (a + 1);
16455 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16456 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16457 is equal to strlen (A) + 1. */
16458 const unsigned HOST_WIDE_INT array_size
= tree_to_uhwi (mem_size
);
16459 unsigned HOST_WIDE_INT init_bytes
= TREE_STRING_LENGTH (src
);
16460 const char *string
= TREE_STRING_POINTER (src
);
16462 /* Ideally this would turn into a gcc_checking_assert over time. */
16463 if (init_bytes
> array_size
)
16464 init_bytes
= array_size
;
16466 if (init_bytes
== 0 || offset
>= array_size
)
16471 /* Compute and store the number of characters from the beginning
16472 of the substring at OFFSET to the end, including the terminating
16473 nul. Offsets past the initial length refer to null strings. */
16474 if (offset
< init_bytes
)
16475 *strsize
= init_bytes
- offset
;
16481 tree eltype
= TREE_TYPE (TREE_TYPE (src
));
16482 /* Support only properly NUL-terminated single byte strings. */
16483 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype
)) != 1)
16485 if (string
[init_bytes
- 1] != '\0')
16489 return offset
< init_bytes
? string
+ offset
: "";
16492 /* Return a pointer to a NUL-terminated string corresponding to
16493 the expression STR referencing a constant string, possibly
16494 involving a constant offset. Return null if STR either doesn't
16495 reference a constant string or if it involves a nonconstant
16499 c_getstr (tree str
)
16501 return getbyterep (str
, NULL
);
16504 /* Given a tree T, compute which bits in T may be nonzero. */
16507 tree_nonzero_bits (const_tree t
)
16509 switch (TREE_CODE (t
))
16512 return wi::to_wide (t
);
16514 return get_nonzero_bits (t
);
16515 case NON_LVALUE_EXPR
:
16517 return tree_nonzero_bits (TREE_OPERAND (t
, 0));
16519 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16520 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16523 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16524 tree_nonzero_bits (TREE_OPERAND (t
, 1)));
16526 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t
, 1)),
16527 tree_nonzero_bits (TREE_OPERAND (t
, 2)));
16529 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t
, 0)),
16530 TYPE_PRECISION (TREE_TYPE (t
)),
16531 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t
, 0))));
16533 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
16535 wide_int nzbits1
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16536 wide_int nzbits2
= tree_nonzero_bits (TREE_OPERAND (t
, 1));
16537 if (wi::bit_and (nzbits1
, nzbits2
) == 0)
16538 return wi::bit_or (nzbits1
, nzbits2
);
16542 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16544 tree type
= TREE_TYPE (t
);
16545 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16546 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16547 TYPE_PRECISION (type
));
16548 return wi::neg_p (arg1
)
16549 ? wi::rshift (nzbits
, -arg1
, TYPE_SIGN (type
))
16550 : wi::lshift (nzbits
, arg1
);
16554 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
16556 tree type
= TREE_TYPE (t
);
16557 wide_int nzbits
= tree_nonzero_bits (TREE_OPERAND (t
, 0));
16558 wide_int arg1
= wi::to_wide (TREE_OPERAND (t
, 1),
16559 TYPE_PRECISION (type
));
16560 return wi::neg_p (arg1
)
16561 ? wi::lshift (nzbits
, -arg1
)
16562 : wi::rshift (nzbits
, arg1
, TYPE_SIGN (type
));
16569 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t
)));
16572 /* Helper function for address compare simplifications in match.pd.
16573 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16574 TYPE is the type of comparison operands.
16575 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16576 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16577 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16578 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16579 and 2 if unknown. */
16582 address_compare (tree_code code
, tree type
, tree op0
, tree op1
,
16583 tree
&base0
, tree
&base1
, poly_int64
&off0
, poly_int64
&off1
,
16586 if (TREE_CODE (op0
) == SSA_NAME
)
16587 op0
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0
));
16588 if (TREE_CODE (op1
) == SSA_NAME
)
16589 op1
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1
));
16590 gcc_checking_assert (TREE_CODE (op0
) == ADDR_EXPR
);
16591 gcc_checking_assert (TREE_CODE (op1
) == ADDR_EXPR
);
16592 base0
= get_addr_base_and_unit_offset (TREE_OPERAND (op0
, 0), &off0
);
16593 base1
= get_addr_base_and_unit_offset (TREE_OPERAND (op1
, 0), &off1
);
16594 if (base0
&& TREE_CODE (base0
) == MEM_REF
)
16596 off0
+= mem_ref_offset (base0
).force_shwi ();
16597 base0
= TREE_OPERAND (base0
, 0);
16599 if (base1
&& TREE_CODE (base1
) == MEM_REF
)
16601 off1
+= mem_ref_offset (base1
).force_shwi ();
16602 base1
= TREE_OPERAND (base1
, 0);
16604 if (base0
== NULL_TREE
|| base1
== NULL_TREE
)
16608 /* Punt in GENERIC on variables with value expressions;
16609 the value expressions might point to fields/elements
16610 of other vars etc. */
16612 && ((VAR_P (base0
) && DECL_HAS_VALUE_EXPR_P (base0
))
16613 || (VAR_P (base1
) && DECL_HAS_VALUE_EXPR_P (base1
))))
16615 else if (decl_in_symtab_p (base0
) && decl_in_symtab_p (base1
))
16617 symtab_node
*node0
= symtab_node::get_create (base0
);
16618 symtab_node
*node1
= symtab_node::get_create (base1
);
16619 equal
= node0
->equal_address_to (node1
);
16621 else if ((DECL_P (base0
)
16622 || TREE_CODE (base0
) == SSA_NAME
16623 || TREE_CODE (base0
) == STRING_CST
)
16625 || TREE_CODE (base1
) == SSA_NAME
16626 || TREE_CODE (base1
) == STRING_CST
))
16627 equal
= (base0
== base1
);
16628 /* Assume different STRING_CSTs with the same content will be
16631 && TREE_CODE (base0
) == STRING_CST
16632 && TREE_CODE (base1
) == STRING_CST
16633 && TREE_STRING_LENGTH (base0
) == TREE_STRING_LENGTH (base1
)
16634 && memcmp (TREE_STRING_POINTER (base0
), TREE_STRING_POINTER (base1
),
16635 TREE_STRING_LENGTH (base0
)) == 0)
16639 if (code
== EQ_EXPR
16641 /* If the offsets are equal we can ignore overflow. */
16642 || known_eq (off0
, off1
)
16643 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
16644 /* Or if we compare using pointers to decls or strings. */
16645 || (POINTER_TYPE_P (type
)
16646 && (DECL_P (base0
) || TREE_CODE (base0
) == STRING_CST
)))
16652 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
16655 /* At this point we know (or assume) the two pointers point at
16656 different objects. */
16657 HOST_WIDE_INT ioff0
= -1, ioff1
= -1;
16658 off0
.is_constant (&ioff0
);
16659 off1
.is_constant (&ioff1
);
16660 /* Punt on non-zero offsets from functions. */
16661 if ((TREE_CODE (base0
) == FUNCTION_DECL
&& ioff0
)
16662 || (TREE_CODE (base1
) == FUNCTION_DECL
&& ioff1
))
16664 /* Or if the bases are neither decls nor string literals. */
16665 if (!DECL_P (base0
) && TREE_CODE (base0
) != STRING_CST
)
16667 if (!DECL_P (base1
) && TREE_CODE (base1
) != STRING_CST
)
16669 /* For initializers, assume addresses of different functions are
16671 if (folding_initializer
16672 && TREE_CODE (base0
) == FUNCTION_DECL
16673 && TREE_CODE (base1
) == FUNCTION_DECL
)
16676 /* Compute whether one address points to the start of one
16677 object and another one to the end of another one. */
16678 poly_int64 size0
= 0, size1
= 0;
16679 if (TREE_CODE (base0
) == STRING_CST
)
16681 if (ioff0
< 0 || ioff0
> TREE_STRING_LENGTH (base0
))
16684 size0
= TREE_STRING_LENGTH (base0
);
16686 else if (TREE_CODE (base0
) == FUNCTION_DECL
)
16690 tree sz0
= DECL_SIZE_UNIT (base0
);
16691 if (!tree_fits_poly_int64_p (sz0
))
16694 size0
= tree_to_poly_int64 (sz0
);
16696 if (TREE_CODE (base1
) == STRING_CST
)
16698 if (ioff1
< 0 || ioff1
> TREE_STRING_LENGTH (base1
))
16701 size1
= TREE_STRING_LENGTH (base1
);
16703 else if (TREE_CODE (base1
) == FUNCTION_DECL
)
16707 tree sz1
= DECL_SIZE_UNIT (base1
);
16708 if (!tree_fits_poly_int64_p (sz1
))
16711 size1
= tree_to_poly_int64 (sz1
);
16715 /* If one offset is pointing (or could be) to the beginning of one
16716 object and the other is pointing to one past the last byte of the
16717 other object, punt. */
16718 if (maybe_eq (off0
, 0) && maybe_eq (off1
, size1
))
16720 else if (maybe_eq (off1
, 0) && maybe_eq (off0
, size0
))
16722 /* If both offsets are the same, there are some cases we know that are
16723 ok. Either if we know they aren't zero, or if we know both sizes
16726 && known_eq (off0
, off1
)
16727 && (known_ne (off0
, 0)
16728 || (known_ne (size0
, 0) && known_ne (size1
, 0))))
16732 /* At this point, equal is 2 if either one or both pointers are out of
16733 bounds of their object, or one points to start of its object and the
16734 other points to end of its object. This is unspecified behavior
16735 e.g. in C++. Otherwise equal is 0. */
16736 if (folding_cxx_constexpr
&& equal
)
16739 /* When both pointers point to string literals, even when equal is 0,
16740 due to tail merging of string literals the pointers might be the same. */
16741 if (TREE_CODE (base0
) == STRING_CST
&& TREE_CODE (base1
) == STRING_CST
)
16745 || ioff0
> TREE_STRING_LENGTH (base0
)
16746 || ioff1
> TREE_STRING_LENGTH (base1
))
16749 /* If the bytes in the string literals starting at the pointers
16750 differ, the pointers need to be different. */
16751 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
,
16752 TREE_STRING_POINTER (base1
) + ioff1
,
16753 MIN (TREE_STRING_LENGTH (base0
) - ioff0
,
16754 TREE_STRING_LENGTH (base1
) - ioff1
)) == 0)
16756 HOST_WIDE_INT ioffmin
= MIN (ioff0
, ioff1
);
16757 if (memcmp (TREE_STRING_POINTER (base0
) + ioff0
- ioffmin
,
16758 TREE_STRING_POINTER (base1
) + ioff1
- ioffmin
,
16760 /* If even the bytes in the string literal before the
16761 pointers are the same, the string literals could be
16768 if (folding_cxx_constexpr
)
16771 /* If this is a pointer comparison, ignore for now even
16772 valid equalities where one pointer is the offset zero
16773 of one object and the other to one past end of another one. */
16774 if (!INTEGRAL_TYPE_P (type
))
16777 /* Assume that string literals can't be adjacent to variables
16778 (automatic or global). */
16779 if (TREE_CODE (base0
) == STRING_CST
|| TREE_CODE (base1
) == STRING_CST
)
16782 /* Assume that automatic variables can't be adjacent to global
16784 if (is_global_var (base0
) != is_global_var (base1
))
16790 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16792 ctor_single_nonzero_element (const_tree t
)
16794 unsigned HOST_WIDE_INT idx
;
16795 constructor_elt
*ce
;
16796 tree elt
= NULL_TREE
;
16798 if (TREE_CODE (t
) != CONSTRUCTOR
)
16800 for (idx
= 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t
), idx
, &ce
); idx
++)
16801 if (!integer_zerop (ce
->value
) && !real_zerop (ce
->value
))
16812 namespace selftest
{
16814 /* Helper functions for writing tests of folding trees. */
16816 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16819 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
16822 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
16825 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16826 wrapping WRAPPED_EXPR. */
16829 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
16832 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
16833 ASSERT_NE (wrapped_expr
, result
);
16834 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
16835 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
16838 /* Verify that various arithmetic binary operations are folded
16842 test_arithmetic_folding ()
16844 tree type
= integer_type_node
;
16845 tree x
= create_tmp_var_raw (type
, "x");
16846 tree zero
= build_zero_cst (type
);
16847 tree one
= build_int_cst (type
, 1);
16850 /* 1 <-- (0 + 1) */
16851 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
16853 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
16856 /* (nonlvalue)x <-- (x + 0) */
16857 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
16861 /* 0 <-- (x - x) */
16862 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
16864 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
16867 /* Multiplication. */
16868 /* 0 <-- (x * 0) */
16869 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
16872 /* (nonlvalue)x <-- (x * 1) */
16873 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
16877 /* Verify that various binary operations on vectors are folded
16881 test_vector_folding ()
16883 tree inner_type
= integer_type_node
;
16884 tree type
= build_vector_type (inner_type
, 4);
16885 tree zero
= build_zero_cst (type
);
16886 tree one
= build_one_cst (type
);
16887 tree index
= build_index_vector (type
, 0, 1);
16889 /* Verify equality tests that return a scalar boolean result. */
16890 tree res_type
= boolean_type_node
;
16891 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
16892 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
16893 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
16894 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
16895 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, index
, one
)));
16896 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16898 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
,
16900 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
,
16904 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16907 test_vec_duplicate_folding ()
16909 scalar_int_mode int_mode
= SCALAR_INT_TYPE_MODE (ssizetype
);
16910 machine_mode vec_mode
= targetm
.vectorize
.preferred_simd_mode (int_mode
);
16911 /* This will be 1 if VEC_MODE isn't a vector mode. */
16912 poly_uint64 nunits
= GET_MODE_NUNITS (vec_mode
);
16914 tree type
= build_vector_type (ssizetype
, nunits
);
16915 tree dup5_expr
= fold_unary (VEC_DUPLICATE_EXPR
, type
, ssize_int (5));
16916 tree dup5_cst
= build_vector_from_val (type
, ssize_int (5));
16917 ASSERT_TRUE (operand_equal_p (dup5_expr
, dup5_cst
, 0));
16920 /* Run all of the selftests within this file. */
16923 fold_const_cc_tests ()
16925 test_arithmetic_folding ();
16926 test_vector_folding ();
16927 test_vec_duplicate_folding ();
16930 } // namespace selftest
16932 #endif /* CHECKING_P */