1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
45 #include "coretypes.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
57 #include "diagnostic-core.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
64 #include "tree-iterator.h"
67 #include "langhooks.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
75 #include "tree-into-ssa.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
80 #include "tree-ssanames.h"
82 #include "stringpool.h"
85 /* Nonzero if we are folding constants inside an initializer; zero
87 int folding_initializer
= 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code
{
111 static bool negate_expr_p (tree
);
112 static tree
negate_expr (tree
);
113 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
114 static enum comparison_code
comparison_to_compcode (enum tree_code
);
115 static enum tree_code
compcode_to_comparison (enum comparison_code
);
116 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
117 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
118 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
120 static int simple_operand_p (const_tree
);
121 static bool simple_operand_p_2 (tree
);
122 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
123 static tree
range_predecessor (tree
);
124 static tree
range_successor (tree
);
125 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
126 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
127 static tree
unextend (tree
, int, int, tree
);
128 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
129 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
130 static tree
fold_binary_op_with_conditional_arg (location_t
,
131 enum tree_code
, tree
,
134 static tree
fold_negate_const (tree
, tree
);
135 static tree
fold_not_const (const_tree
, tree
);
136 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
137 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
138 static tree
fold_view_convert_expr (tree
, tree
);
139 static tree
fold_negate_expr (location_t
, tree
);
142 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
143 Otherwise, return LOC. */
146 expr_location_or (tree t
, location_t loc
)
148 location_t tloc
= EXPR_LOCATION (t
);
149 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
152 /* Similar to protected_set_expr_location, but never modify x in place,
153 if location can and needs to be set, unshare it. */
156 protected_set_expr_location_unshare (tree x
, location_t loc
)
158 if (CAN_HAVE_LOCATION_P (x
)
159 && EXPR_LOCATION (x
) != loc
160 && !(TREE_CODE (x
) == SAVE_EXPR
161 || TREE_CODE (x
) == TARGET_EXPR
162 || TREE_CODE (x
) == BIND_EXPR
))
165 SET_EXPR_LOCATION (x
, loc
);
170 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
171 division and returns the quotient. Otherwise returns
175 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
179 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
181 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
186 /* This is nonzero if we should defer warnings about undefined
187 overflow. This facility exists because these warnings are a
188 special case. The code to estimate loop iterations does not want
189 to issue any warnings, since it works with expressions which do not
190 occur in user code. Various bits of cleanup code call fold(), but
191 only use the result if it has certain characteristics (e.g., is a
192 constant); that code only wants to issue a warning if the result is
195 static int fold_deferring_overflow_warnings
;
197 /* If a warning about undefined overflow is deferred, this is the
198 warning. Note that this may cause us to turn two warnings into
199 one, but that is fine since it is sufficient to only give one
200 warning per expression. */
202 static const char* fold_deferred_overflow_warning
;
204 /* If a warning about undefined overflow is deferred, this is the
205 level at which the warning should be emitted. */
207 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
209 /* Start deferring overflow warnings. We could use a stack here to
210 permit nested calls, but at present it is not necessary. */
213 fold_defer_overflow_warnings (void)
215 ++fold_deferring_overflow_warnings
;
218 /* Stop deferring overflow warnings. If there is a pending warning,
219 and ISSUE is true, then issue the warning if appropriate. STMT is
220 the statement with which the warning should be associated (used for
221 location information); STMT may be NULL. CODE is the level of the
222 warning--a warn_strict_overflow_code value. This function will use
223 the smaller of CODE and the deferred code when deciding whether to
224 issue the warning. CODE may be zero to mean to always use the
228 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
233 gcc_assert (fold_deferring_overflow_warnings
> 0);
234 --fold_deferring_overflow_warnings
;
235 if (fold_deferring_overflow_warnings
> 0)
237 if (fold_deferred_overflow_warning
!= NULL
239 && code
< (int) fold_deferred_overflow_code
)
240 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
244 warnmsg
= fold_deferred_overflow_warning
;
245 fold_deferred_overflow_warning
= NULL
;
247 if (!issue
|| warnmsg
== NULL
)
250 if (gimple_no_warning_p (stmt
))
253 /* Use the smallest code level when deciding to issue the
255 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
256 code
= fold_deferred_overflow_code
;
258 if (!issue_strict_overflow_warning (code
))
262 locus
= input_location
;
264 locus
= gimple_location (stmt
);
265 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
268 /* Stop deferring overflow warnings, ignoring any deferred
272 fold_undefer_and_ignore_overflow_warnings (void)
274 fold_undefer_overflow_warnings (false, NULL
, 0);
277 /* Whether we are deferring overflow warnings. */
280 fold_deferring_overflow_warnings_p (void)
282 return fold_deferring_overflow_warnings
> 0;
285 /* This is called when we fold something based on the fact that signed
286 overflow is undefined. */
289 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
291 if (fold_deferring_overflow_warnings
> 0)
293 if (fold_deferred_overflow_warning
== NULL
294 || wc
< fold_deferred_overflow_code
)
296 fold_deferred_overflow_warning
= gmsgid
;
297 fold_deferred_overflow_code
= wc
;
300 else if (issue_strict_overflow_warning (wc
))
301 warning (OPT_Wstrict_overflow
, gmsgid
);
304 /* Return true if the built-in mathematical function specified by CODE
305 is odd, i.e. -f(x) == f(-x). */
308 negate_mathfn_p (combined_fn fn
)
341 return !flag_rounding_math
;
349 /* Check whether we may negate an integer constant T without causing
353 may_negate_without_overflow_p (const_tree t
)
357 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
359 type
= TREE_TYPE (t
);
360 if (TYPE_UNSIGNED (type
))
363 return !wi::only_sign_bit_p (t
);
366 /* Determine whether an expression T can be cheaply negated using
367 the function negate_expr without introducing undefined overflow. */
370 negate_expr_p (tree t
)
377 type
= TREE_TYPE (t
);
380 switch (TREE_CODE (t
))
383 if (INTEGRAL_TYPE_P (type
) && TYPE_UNSIGNED (type
))
386 /* Check that -CST will not overflow type. */
387 return may_negate_without_overflow_p (t
);
389 return (INTEGRAL_TYPE_P (type
)
390 && TYPE_OVERFLOW_WRAPS (type
));
396 return !TYPE_OVERFLOW_SANITIZED (type
);
399 /* We want to canonicalize to positive real constants. Pretend
400 that only negative ones can be easily negated. */
401 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
404 return negate_expr_p (TREE_REALPART (t
))
405 && negate_expr_p (TREE_IMAGPART (t
));
409 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
412 int count
= VECTOR_CST_NELTS (t
), i
;
414 for (i
= 0; i
< count
; i
++)
415 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
422 return negate_expr_p (TREE_OPERAND (t
, 0))
423 && negate_expr_p (TREE_OPERAND (t
, 1));
426 return negate_expr_p (TREE_OPERAND (t
, 0));
429 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
430 || HONOR_SIGNED_ZEROS (element_mode (type
))
431 || (INTEGRAL_TYPE_P (type
)
432 && ! TYPE_OVERFLOW_WRAPS (type
)))
434 /* -(A + B) -> (-B) - A. */
435 if (negate_expr_p (TREE_OPERAND (t
, 1)))
437 /* -(A + B) -> (-A) - B. */
438 return negate_expr_p (TREE_OPERAND (t
, 0));
441 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
443 && !HONOR_SIGNED_ZEROS (element_mode (type
))
444 && (! INTEGRAL_TYPE_P (type
)
445 || TYPE_OVERFLOW_WRAPS (type
));
448 if (TYPE_UNSIGNED (type
))
450 /* INT_MIN/n * n doesn't overflow while negating one operand it does
451 if n is a (negative) power of two. */
452 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
453 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
454 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
455 && wi::popcount (wi::abs (TREE_OPERAND (t
, 0))) != 1)
456 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
457 && wi::popcount (wi::abs (TREE_OPERAND (t
, 1))) != 1)))
463 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
464 return negate_expr_p (TREE_OPERAND (t
, 1))
465 || negate_expr_p (TREE_OPERAND (t
, 0));
471 if (TYPE_UNSIGNED (type
))
473 if (negate_expr_p (TREE_OPERAND (t
, 0)))
475 /* In general we can't negate B in A / B, because if A is INT_MIN and
476 B is 1, we may turn this into INT_MIN / -1 which is undefined
477 and actually traps on some architectures. */
478 if (! INTEGRAL_TYPE_P (TREE_TYPE (t
))
479 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
480 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
481 && ! integer_onep (TREE_OPERAND (t
, 1))))
482 return negate_expr_p (TREE_OPERAND (t
, 1));
486 /* Negate -((double)float) as (double)(-float). */
487 if (TREE_CODE (type
) == REAL_TYPE
)
489 tree tem
= strip_float_extensions (t
);
491 return negate_expr_p (tem
);
496 /* Negate -f(x) as f(-x). */
497 if (negate_mathfn_p (get_call_combined_fn (t
)))
498 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
502 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
503 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
505 tree op1
= TREE_OPERAND (t
, 1);
506 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
517 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
518 simplification is possible.
519 If negate_expr_p would return true for T, NULL_TREE will never be
523 fold_negate_expr_1 (location_t loc
, tree t
)
525 tree type
= TREE_TYPE (t
);
528 switch (TREE_CODE (t
))
530 /* Convert - (~A) to A + 1. */
532 if (INTEGRAL_TYPE_P (type
))
533 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
534 build_one_cst (type
));
538 tem
= fold_negate_const (t
, type
);
539 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
540 || (ANY_INTEGRAL_TYPE_P (type
)
541 && !TYPE_OVERFLOW_TRAPS (type
)
542 && TYPE_OVERFLOW_WRAPS (type
))
543 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
548 tem
= fold_negate_const (t
, type
);
552 tem
= fold_negate_const (t
, type
);
557 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
558 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
560 return build_complex (type
, rpart
, ipart
);
566 int count
= VECTOR_CST_NELTS (t
), i
;
568 auto_vec
<tree
, 32> elts (count
);
569 for (i
= 0; i
< count
; i
++)
571 tree elt
= fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
572 if (elt
== NULL_TREE
)
574 elts
.quick_push (elt
);
577 return build_vector (type
, elts
);
581 if (negate_expr_p (t
))
582 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
583 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
584 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
588 if (negate_expr_p (t
))
589 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
590 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
594 if (!TYPE_OVERFLOW_SANITIZED (type
))
595 return TREE_OPERAND (t
, 0);
599 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
600 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
602 /* -(A + B) -> (-B) - A. */
603 if (negate_expr_p (TREE_OPERAND (t
, 1)))
605 tem
= negate_expr (TREE_OPERAND (t
, 1));
606 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
607 tem
, TREE_OPERAND (t
, 0));
610 /* -(A + B) -> (-A) - B. */
611 if (negate_expr_p (TREE_OPERAND (t
, 0)))
613 tem
= negate_expr (TREE_OPERAND (t
, 0));
614 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
615 tem
, TREE_OPERAND (t
, 1));
621 /* - (A - B) -> B - A */
622 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
623 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
624 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
625 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
629 if (TYPE_UNSIGNED (type
))
635 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
637 tem
= TREE_OPERAND (t
, 1);
638 if (negate_expr_p (tem
))
639 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
640 TREE_OPERAND (t
, 0), negate_expr (tem
));
641 tem
= TREE_OPERAND (t
, 0);
642 if (negate_expr_p (tem
))
643 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
644 negate_expr (tem
), TREE_OPERAND (t
, 1));
651 if (TYPE_UNSIGNED (type
))
653 if (negate_expr_p (TREE_OPERAND (t
, 0)))
654 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
655 negate_expr (TREE_OPERAND (t
, 0)),
656 TREE_OPERAND (t
, 1));
657 /* In general we can't negate B in A / B, because if A is INT_MIN and
658 B is 1, we may turn this into INT_MIN / -1 which is undefined
659 and actually traps on some architectures. */
660 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t
))
661 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
662 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
663 && ! integer_onep (TREE_OPERAND (t
, 1))))
664 && negate_expr_p (TREE_OPERAND (t
, 1)))
665 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
667 negate_expr (TREE_OPERAND (t
, 1)));
671 /* Convert -((double)float) into (double)(-float). */
672 if (TREE_CODE (type
) == REAL_TYPE
)
674 tem
= strip_float_extensions (t
);
675 if (tem
!= t
&& negate_expr_p (tem
))
676 return fold_convert_loc (loc
, type
, negate_expr (tem
));
681 /* Negate -f(x) as f(-x). */
682 if (negate_mathfn_p (get_call_combined_fn (t
))
683 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
687 fndecl
= get_callee_fndecl (t
);
688 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
689 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
694 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
695 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
697 tree op1
= TREE_OPERAND (t
, 1);
698 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
700 tree ntype
= TYPE_UNSIGNED (type
)
701 ? signed_type_for (type
)
702 : unsigned_type_for (type
);
703 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
704 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
705 return fold_convert_loc (loc
, type
, temp
);
717 /* A wrapper for fold_negate_expr_1. */
720 fold_negate_expr (location_t loc
, tree t
)
722 tree type
= TREE_TYPE (t
);
724 tree tem
= fold_negate_expr_1 (loc
, t
);
725 if (tem
== NULL_TREE
)
727 return fold_convert_loc (loc
, type
, tem
);
730 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
731 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
743 loc
= EXPR_LOCATION (t
);
744 type
= TREE_TYPE (t
);
747 tem
= fold_negate_expr (loc
, t
);
749 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
750 return fold_convert_loc (loc
, type
, tem
);
753 /* Split a tree IN into a constant, literal and variable parts that could be
754 combined with CODE to make IN. "constant" means an expression with
755 TREE_CONSTANT but that isn't an actual constant. CODE must be a
756 commutative arithmetic operation. Store the constant part into *CONP,
757 the literal in *LITP and return the variable part. If a part isn't
758 present, set it to null. If the tree does not decompose in this way,
759 return the entire tree as the variable part and the other parts as null.
761 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
762 case, we negate an operand that was subtracted. Except if it is a
763 literal for which we use *MINUS_LITP instead.
765 If NEGATE_P is true, we are negating all of IN, again except a literal
766 for which we use *MINUS_LITP instead. If a variable part is of pointer
767 type, it is negated after converting to TYPE. This prevents us from
768 generating illegal MINUS pointer expression. LOC is the location of
769 the converted variable part.
771 If IN is itself a literal or constant, return it as appropriate.
773 Note that we do not guarantee that any of the three values will be the
774 same type as IN, but they will have the same signedness and mode. */
777 split_tree (tree in
, tree type
, enum tree_code code
,
778 tree
*minus_varp
, tree
*conp
, tree
*minus_conp
,
779 tree
*litp
, tree
*minus_litp
, int negate_p
)
788 /* Strip any conversions that don't change the machine mode or signedness. */
789 STRIP_SIGN_NOPS (in
);
791 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
792 || TREE_CODE (in
) == FIXED_CST
)
794 else if (TREE_CODE (in
) == code
795 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
796 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
797 /* We can associate addition and subtraction together (even
798 though the C standard doesn't say so) for integers because
799 the value is not affected. For reals, the value might be
800 affected, so we can't. */
801 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
802 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
803 || (code
== MINUS_EXPR
804 && (TREE_CODE (in
) == PLUS_EXPR
805 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
807 tree op0
= TREE_OPERAND (in
, 0);
808 tree op1
= TREE_OPERAND (in
, 1);
809 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
810 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
814 || TREE_CODE (op0
) == FIXED_CST
)
815 *litp
= op0
, op0
= 0;
816 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
817 || TREE_CODE (op1
) == FIXED_CST
)
818 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
820 if (op0
!= 0 && TREE_CONSTANT (op0
))
821 *conp
= op0
, op0
= 0;
822 else if (op1
!= 0 && TREE_CONSTANT (op1
))
823 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0
!= 0 && op1
!= 0)
832 var
= op1
, neg_var_p
= neg1_p
;
834 /* Now do any needed negations. */
836 *minus_litp
= *litp
, *litp
= 0;
837 if (neg_conp_p
&& *conp
)
838 *minus_conp
= *conp
, *conp
= 0;
839 if (neg_var_p
&& var
)
840 *minus_varp
= var
, var
= 0;
842 else if (TREE_CONSTANT (in
))
844 else if (TREE_CODE (in
) == BIT_NOT_EXPR
845 && code
== PLUS_EXPR
)
847 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
848 when IN is constant. */
849 *litp
= build_minus_one_cst (type
);
850 *minus_varp
= TREE_OPERAND (in
, 0);
858 *minus_litp
= *litp
, *litp
= 0;
859 else if (*minus_litp
)
860 *litp
= *minus_litp
, *minus_litp
= 0;
862 *minus_conp
= *conp
, *conp
= 0;
863 else if (*minus_conp
)
864 *conp
= *minus_conp
, *minus_conp
= 0;
866 *minus_varp
= var
, var
= 0;
867 else if (*minus_varp
)
868 var
= *minus_varp
, *minus_varp
= 0;
872 && TREE_OVERFLOW_P (*litp
))
873 *litp
= drop_tree_overflow (*litp
);
875 && TREE_OVERFLOW_P (*minus_litp
))
876 *minus_litp
= drop_tree_overflow (*minus_litp
);
881 /* Re-associate trees split by the above function. T1 and T2 are
882 either expressions to associate or null. Return the new
883 expression, if any. LOC is the location of the new expression. If
884 we build an operation, do it in TYPE and with CODE. */
887 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
891 gcc_assert (t2
== 0 || code
!= MINUS_EXPR
);
897 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
898 try to fold this since we will have infinite recursion. But do
899 deal with any NEGATE_EXPRs. */
900 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
901 || TREE_CODE (t1
) == PLUS_EXPR
|| TREE_CODE (t2
) == PLUS_EXPR
902 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
904 if (code
== PLUS_EXPR
)
906 if (TREE_CODE (t1
) == NEGATE_EXPR
)
907 return build2_loc (loc
, MINUS_EXPR
, type
,
908 fold_convert_loc (loc
, type
, t2
),
909 fold_convert_loc (loc
, type
,
910 TREE_OPERAND (t1
, 0)));
911 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
912 return build2_loc (loc
, MINUS_EXPR
, type
,
913 fold_convert_loc (loc
, type
, t1
),
914 fold_convert_loc (loc
, type
,
915 TREE_OPERAND (t2
, 0)));
916 else if (integer_zerop (t2
))
917 return fold_convert_loc (loc
, type
, t1
);
919 else if (code
== MINUS_EXPR
)
921 if (integer_zerop (t2
))
922 return fold_convert_loc (loc
, type
, t1
);
925 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
926 fold_convert_loc (loc
, type
, t2
));
929 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
930 fold_convert_loc (loc
, type
, t2
));
933 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
934 for use in int_const_binop, size_binop and size_diffop. */
937 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
939 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
941 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
956 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
957 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
958 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
962 /* Combine two integer constants ARG1 and ARG2 under operation CODE
963 to produce a new constant. Return NULL_TREE if we don't know how
964 to evaluate CODE at compile-time. */
967 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
972 tree type
= TREE_TYPE (arg1
);
973 signop sign
= TYPE_SIGN (type
);
974 bool overflow
= false;
976 wide_int arg2
= wi::to_wide (parg2
, TYPE_PRECISION (type
));
981 res
= wi::bit_or (arg1
, arg2
);
985 res
= wi::bit_xor (arg1
, arg2
);
989 res
= wi::bit_and (arg1
, arg2
);
994 if (wi::neg_p (arg2
))
997 if (code
== RSHIFT_EXPR
)
1003 if (code
== RSHIFT_EXPR
)
1004 /* It's unclear from the C standard whether shifts can overflow.
1005 The following code ignores overflow; perhaps a C standard
1006 interpretation ruling is needed. */
1007 res
= wi::rshift (arg1
, arg2
, sign
);
1009 res
= wi::lshift (arg1
, arg2
);
1014 if (wi::neg_p (arg2
))
1017 if (code
== RROTATE_EXPR
)
1018 code
= LROTATE_EXPR
;
1020 code
= RROTATE_EXPR
;
1023 if (code
== RROTATE_EXPR
)
1024 res
= wi::rrotate (arg1
, arg2
);
1026 res
= wi::lrotate (arg1
, arg2
);
1030 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1034 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1038 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1041 case MULT_HIGHPART_EXPR
:
1042 res
= wi::mul_high (arg1
, arg2
, sign
);
1045 case TRUNC_DIV_EXPR
:
1046 case EXACT_DIV_EXPR
:
1049 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1052 case FLOOR_DIV_EXPR
:
1055 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1061 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1064 case ROUND_DIV_EXPR
:
1067 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1070 case TRUNC_MOD_EXPR
:
1073 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1076 case FLOOR_MOD_EXPR
:
1079 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1085 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1088 case ROUND_MOD_EXPR
:
1091 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1095 res
= wi::min (arg1
, arg2
, sign
);
1099 res
= wi::max (arg1
, arg2
, sign
);
1106 t
= force_fit_type (type
, res
, overflowable
,
1107 (((sign
== SIGNED
|| overflowable
== -1)
1109 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1115 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1117 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1120 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1121 constant. We assume ARG1 and ARG2 have the same data type, or at least
1122 are the same kind of constant and the same machine mode. Return zero if
1123 combining the constants is not allowed in the current operating mode. */
1126 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1128 /* Sanity check for the recursive cases. */
1135 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1137 if (code
== POINTER_PLUS_EXPR
)
1138 return int_const_binop (PLUS_EXPR
,
1139 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1141 return int_const_binop (code
, arg1
, arg2
);
1144 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1149 REAL_VALUE_TYPE value
;
1150 REAL_VALUE_TYPE result
;
1154 /* The following codes are handled by real_arithmetic. */
1169 d1
= TREE_REAL_CST (arg1
);
1170 d2
= TREE_REAL_CST (arg2
);
1172 type
= TREE_TYPE (arg1
);
1173 mode
= TYPE_MODE (type
);
1175 /* Don't perform operation if we honor signaling NaNs and
1176 either operand is a signaling NaN. */
1177 if (HONOR_SNANS (mode
)
1178 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1179 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1182 /* Don't perform operation if it would raise a division
1183 by zero exception. */
1184 if (code
== RDIV_EXPR
1185 && real_equal (&d2
, &dconst0
)
1186 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1189 /* If either operand is a NaN, just return it. Otherwise, set up
1190 for floating-point trap; we return an overflow. */
1191 if (REAL_VALUE_ISNAN (d1
))
1193 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1196 t
= build_real (type
, d1
);
1199 else if (REAL_VALUE_ISNAN (d2
))
1201 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1204 t
= build_real (type
, d2
);
1208 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1209 real_convert (&result
, mode
, &value
);
1211 /* Don't constant fold this floating point operation if
1212 the result has overflowed and flag_trapping_math. */
1213 if (flag_trapping_math
1214 && MODE_HAS_INFINITIES (mode
)
1215 && REAL_VALUE_ISINF (result
)
1216 && !REAL_VALUE_ISINF (d1
)
1217 && !REAL_VALUE_ISINF (d2
))
1220 /* Don't constant fold this floating point operation if the
1221 result may dependent upon the run-time rounding mode and
1222 flag_rounding_math is set, or if GCC's software emulation
1223 is unable to accurately represent the result. */
1224 if ((flag_rounding_math
1225 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1226 && (inexact
|| !real_identical (&result
, &value
)))
1229 t
= build_real (type
, result
);
1231 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1235 if (TREE_CODE (arg1
) == FIXED_CST
)
1237 FIXED_VALUE_TYPE f1
;
1238 FIXED_VALUE_TYPE f2
;
1239 FIXED_VALUE_TYPE result
;
1244 /* The following codes are handled by fixed_arithmetic. */
1250 case TRUNC_DIV_EXPR
:
1251 if (TREE_CODE (arg2
) != FIXED_CST
)
1253 f2
= TREE_FIXED_CST (arg2
);
1259 if (TREE_CODE (arg2
) != INTEGER_CST
)
1262 f2
.data
.high
= w2
.elt (1);
1263 f2
.data
.low
= w2
.ulow ();
1272 f1
= TREE_FIXED_CST (arg1
);
1273 type
= TREE_TYPE (arg1
);
1274 sat_p
= TYPE_SATURATING (type
);
1275 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1276 t
= build_fixed (type
, result
);
1277 /* Propagate overflow flags. */
1278 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1279 TREE_OVERFLOW (t
) = 1;
1283 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1285 tree type
= TREE_TYPE (arg1
);
1286 tree r1
= TREE_REALPART (arg1
);
1287 tree i1
= TREE_IMAGPART (arg1
);
1288 tree r2
= TREE_REALPART (arg2
);
1289 tree i2
= TREE_IMAGPART (arg2
);
1296 real
= const_binop (code
, r1
, r2
);
1297 imag
= const_binop (code
, i1
, i2
);
1301 if (COMPLEX_FLOAT_TYPE_P (type
))
1302 return do_mpc_arg2 (arg1
, arg2
, type
,
1303 /* do_nonfinite= */ folding_initializer
,
1306 real
= const_binop (MINUS_EXPR
,
1307 const_binop (MULT_EXPR
, r1
, r2
),
1308 const_binop (MULT_EXPR
, i1
, i2
));
1309 imag
= const_binop (PLUS_EXPR
,
1310 const_binop (MULT_EXPR
, r1
, i2
),
1311 const_binop (MULT_EXPR
, i1
, r2
));
1315 if (COMPLEX_FLOAT_TYPE_P (type
))
1316 return do_mpc_arg2 (arg1
, arg2
, type
,
1317 /* do_nonfinite= */ folding_initializer
,
1320 case TRUNC_DIV_EXPR
:
1322 case FLOOR_DIV_EXPR
:
1323 case ROUND_DIV_EXPR
:
1324 if (flag_complex_method
== 0)
1326 /* Keep this algorithm in sync with
1327 tree-complex.c:expand_complex_div_straight().
1329 Expand complex division to scalars, straightforward algorithm.
1330 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1334 = const_binop (PLUS_EXPR
,
1335 const_binop (MULT_EXPR
, r2
, r2
),
1336 const_binop (MULT_EXPR
, i2
, i2
));
1338 = const_binop (PLUS_EXPR
,
1339 const_binop (MULT_EXPR
, r1
, r2
),
1340 const_binop (MULT_EXPR
, i1
, i2
));
1342 = const_binop (MINUS_EXPR
,
1343 const_binop (MULT_EXPR
, i1
, r2
),
1344 const_binop (MULT_EXPR
, r1
, i2
));
1346 real
= const_binop (code
, t1
, magsquared
);
1347 imag
= const_binop (code
, t2
, magsquared
);
1351 /* Keep this algorithm in sync with
1352 tree-complex.c:expand_complex_div_wide().
1354 Expand complex division to scalars, modified algorithm to minimize
1355 overflow with wide input ranges. */
1356 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1357 fold_abs_const (r2
, TREE_TYPE (type
)),
1358 fold_abs_const (i2
, TREE_TYPE (type
)));
1360 if (integer_nonzerop (compare
))
1362 /* In the TRUE branch, we compute
1364 div = (br * ratio) + bi;
1365 tr = (ar * ratio) + ai;
1366 ti = (ai * ratio) - ar;
1369 tree ratio
= const_binop (code
, r2
, i2
);
1370 tree div
= const_binop (PLUS_EXPR
, i2
,
1371 const_binop (MULT_EXPR
, r2
, ratio
));
1372 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1373 real
= const_binop (PLUS_EXPR
, real
, i1
);
1374 real
= const_binop (code
, real
, div
);
1376 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1377 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1378 imag
= const_binop (code
, imag
, div
);
1382 /* In the FALSE branch, we compute
1384 divisor = (d * ratio) + c;
1385 tr = (b * ratio) + a;
1386 ti = b - (a * ratio);
1389 tree ratio
= const_binop (code
, i2
, r2
);
1390 tree div
= const_binop (PLUS_EXPR
, r2
,
1391 const_binop (MULT_EXPR
, i2
, ratio
));
1393 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1394 real
= const_binop (PLUS_EXPR
, real
, r1
);
1395 real
= const_binop (code
, real
, div
);
1397 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1398 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1399 imag
= const_binop (code
, imag
, div
);
1409 return build_complex (type
, real
, imag
);
1412 if (TREE_CODE (arg1
) == VECTOR_CST
1413 && TREE_CODE (arg2
) == VECTOR_CST
)
1415 tree type
= TREE_TYPE (arg1
);
1416 int count
= VECTOR_CST_NELTS (arg1
), i
;
1418 auto_vec
<tree
, 32> elts (count
);
1419 for (i
= 0; i
< count
; i
++)
1421 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1422 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1424 tree elt
= const_binop (code
, elem1
, elem2
);
1426 /* It is possible that const_binop cannot handle the given
1427 code and return NULL_TREE */
1428 if (elt
== NULL_TREE
)
1430 elts
.quick_push (elt
);
1433 return build_vector (type
, elts
);
1436 /* Shifts allow a scalar offset for a vector. */
1437 if (TREE_CODE (arg1
) == VECTOR_CST
1438 && TREE_CODE (arg2
) == INTEGER_CST
)
1440 tree type
= TREE_TYPE (arg1
);
1441 int count
= VECTOR_CST_NELTS (arg1
), i
;
1443 auto_vec
<tree
, 32> elts (count
);
1444 for (i
= 0; i
< count
; i
++)
1446 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1448 tree elt
= const_binop (code
, elem1
, arg2
);
1450 /* It is possible that const_binop cannot handle the given
1451 code and return NULL_TREE. */
1452 if (elt
== NULL_TREE
)
1454 elts
.quick_push (elt
);
1457 return build_vector (type
, elts
);
1462 /* Overload that adds a TYPE parameter to be able to dispatch
1463 to fold_relational_const. */
1466 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1468 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1469 return fold_relational_const (code
, type
, arg1
, arg2
);
1471 /* ??? Until we make the const_binop worker take the type of the
1472 result as argument put those cases that need it here. */
1476 if ((TREE_CODE (arg1
) == REAL_CST
1477 && TREE_CODE (arg2
) == REAL_CST
)
1478 || (TREE_CODE (arg1
) == INTEGER_CST
1479 && TREE_CODE (arg2
) == INTEGER_CST
))
1480 return build_complex (type
, arg1
, arg2
);
1483 case VEC_PACK_TRUNC_EXPR
:
1484 case VEC_PACK_FIX_TRUNC_EXPR
:
1486 unsigned int out_nelts
, in_nelts
, i
;
1488 if (TREE_CODE (arg1
) != VECTOR_CST
1489 || TREE_CODE (arg2
) != VECTOR_CST
)
1492 in_nelts
= VECTOR_CST_NELTS (arg1
);
1493 out_nelts
= in_nelts
* 2;
1494 gcc_assert (in_nelts
== VECTOR_CST_NELTS (arg2
)
1495 && out_nelts
== TYPE_VECTOR_SUBPARTS (type
));
1497 auto_vec
<tree
, 32> elts (out_nelts
);
1498 for (i
= 0; i
< out_nelts
; i
++)
1500 tree elt
= (i
< in_nelts
1501 ? VECTOR_CST_ELT (arg1
, i
)
1502 : VECTOR_CST_ELT (arg2
, i
- in_nelts
));
1503 elt
= fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1504 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1505 TREE_TYPE (type
), elt
);
1506 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1508 elts
.quick_push (elt
);
1511 return build_vector (type
, elts
);
1514 case VEC_WIDEN_MULT_LO_EXPR
:
1515 case VEC_WIDEN_MULT_HI_EXPR
:
1516 case VEC_WIDEN_MULT_EVEN_EXPR
:
1517 case VEC_WIDEN_MULT_ODD_EXPR
:
1519 unsigned int out_nelts
, in_nelts
, out
, ofs
, scale
;
1521 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1524 in_nelts
= VECTOR_CST_NELTS (arg1
);
1525 out_nelts
= in_nelts
/ 2;
1526 gcc_assert (in_nelts
== VECTOR_CST_NELTS (arg2
)
1527 && out_nelts
== TYPE_VECTOR_SUBPARTS (type
));
1529 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1530 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? out_nelts
: 0;
1531 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1532 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : out_nelts
;
1533 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1535 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1538 auto_vec
<tree
, 32> elts (out_nelts
);
1539 for (out
= 0; out
< out_nelts
; out
++)
1541 unsigned int in
= (out
<< scale
) + ofs
;
1542 tree t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1543 VECTOR_CST_ELT (arg1
, in
));
1544 tree t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
),
1545 VECTOR_CST_ELT (arg2
, in
));
1547 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1549 tree elt
= const_binop (MULT_EXPR
, t1
, t2
);
1550 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1552 elts
.quick_push (elt
);
1555 return build_vector (type
, elts
);
1561 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1564 /* Make sure type and arg0 have the same saturating flag. */
1565 gcc_checking_assert (TYPE_SATURATING (type
)
1566 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1568 return const_binop (code
, arg1
, arg2
);
1571 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1572 Return zero if computing the constants is not possible. */
1575 const_unop (enum tree_code code
, tree type
, tree arg0
)
1577 /* Don't perform the operation, other than NEGATE and ABS, if
1578 flag_signaling_nans is on and the operand is a signaling NaN. */
1579 if (TREE_CODE (arg0
) == REAL_CST
1580 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1581 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1582 && code
!= NEGATE_EXPR
1583 && code
!= ABS_EXPR
)
1590 case FIX_TRUNC_EXPR
:
1591 case FIXED_CONVERT_EXPR
:
1592 return fold_convert_const (code
, type
, arg0
);
1594 case ADDR_SPACE_CONVERT_EXPR
:
1595 /* If the source address is 0, and the source address space
1596 cannot have a valid object at 0, fold to dest type null. */
1597 if (integer_zerop (arg0
)
1598 && !(targetm
.addr_space
.zero_address_valid
1599 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1600 return fold_convert_const (code
, type
, arg0
);
1603 case VIEW_CONVERT_EXPR
:
1604 return fold_view_convert_expr (type
, arg0
);
1608 /* Can't call fold_negate_const directly here as that doesn't
1609 handle all cases and we might not be able to negate some
1611 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1612 if (tem
&& CONSTANT_CLASS_P (tem
))
1618 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1619 return fold_abs_const (arg0
, type
);
1623 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1625 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1627 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1632 if (TREE_CODE (arg0
) == INTEGER_CST
)
1633 return fold_not_const (arg0
, type
);
1634 /* Perform BIT_NOT_EXPR on each element individually. */
1635 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1638 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1640 auto_vec
<tree
, 32> elements (count
);
1641 for (i
= 0; i
< count
; i
++)
1643 elem
= VECTOR_CST_ELT (arg0
, i
);
1644 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1645 if (elem
== NULL_TREE
)
1647 elements
.quick_push (elem
);
1650 return build_vector (type
, elements
);
1654 case TRUTH_NOT_EXPR
:
1655 if (TREE_CODE (arg0
) == INTEGER_CST
)
1656 return constant_boolean_node (integer_zerop (arg0
), type
);
1660 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1661 return fold_convert (type
, TREE_REALPART (arg0
));
1665 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1666 return fold_convert (type
, TREE_IMAGPART (arg0
));
1669 case VEC_UNPACK_LO_EXPR
:
1670 case VEC_UNPACK_HI_EXPR
:
1671 case VEC_UNPACK_FLOAT_LO_EXPR
:
1672 case VEC_UNPACK_FLOAT_HI_EXPR
:
1674 unsigned int out_nelts
, in_nelts
, i
;
1675 enum tree_code subcode
;
1677 if (TREE_CODE (arg0
) != VECTOR_CST
)
1680 in_nelts
= VECTOR_CST_NELTS (arg0
);
1681 out_nelts
= in_nelts
/ 2;
1682 gcc_assert (out_nelts
== TYPE_VECTOR_SUBPARTS (type
));
1684 unsigned int offset
= 0;
1685 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1686 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1689 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1692 subcode
= FLOAT_EXPR
;
1694 auto_vec
<tree
, 32> elts (out_nelts
);
1695 for (i
= 0; i
< out_nelts
; i
++)
1697 tree elt
= fold_convert_const (subcode
, TREE_TYPE (type
),
1698 VECTOR_CST_ELT (arg0
, i
+ offset
));
1699 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
1701 elts
.quick_push (elt
);
1704 return build_vector (type
, elts
);
1707 case REDUC_MIN_EXPR
:
1708 case REDUC_MAX_EXPR
:
1709 case REDUC_PLUS_EXPR
:
1711 unsigned int nelts
, i
;
1712 enum tree_code subcode
;
1714 if (TREE_CODE (arg0
) != VECTOR_CST
)
1716 nelts
= VECTOR_CST_NELTS (arg0
);
1720 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1721 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1722 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1723 default: gcc_unreachable ();
1726 tree res
= VECTOR_CST_ELT (arg0
, 0);
1727 for (i
= 1; i
< nelts
; i
++)
1729 res
= const_binop (subcode
, res
, VECTOR_CST_ELT (arg0
, i
));
1730 if (res
== NULL_TREE
|| !CONSTANT_CLASS_P (res
))
1744 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1745 indicates which particular sizetype to create. */
1748 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1750 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1753 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1754 is a tree code. The type of the result is taken from the operands.
1755 Both must be equivalent integer types, ala int_binop_types_match_p.
1756 If the operands are constant, so is the result. */
1759 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1761 tree type
= TREE_TYPE (arg0
);
1763 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1764 return error_mark_node
;
1766 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1769 /* Handle the special case of two integer constants faster. */
1770 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1772 /* And some specific cases even faster than that. */
1773 if (code
== PLUS_EXPR
)
1775 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1777 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1780 else if (code
== MINUS_EXPR
)
1782 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1785 else if (code
== MULT_EXPR
)
1787 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1791 /* Handle general case of two integer constants. For sizetype
1792 constant calculations we always want to know about overflow,
1793 even in the unsigned case. */
1794 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1797 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1800 /* Given two values, either both of sizetype or both of bitsizetype,
1801 compute the difference between the two values. Return the value
1802 in signed type corresponding to the type of the operands. */
1805 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1807 tree type
= TREE_TYPE (arg0
);
1810 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1813 /* If the type is already signed, just do the simple thing. */
1814 if (!TYPE_UNSIGNED (type
))
1815 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1817 if (type
== sizetype
)
1819 else if (type
== bitsizetype
)
1820 ctype
= sbitsizetype
;
1822 ctype
= signed_type_for (type
);
1824 /* If either operand is not a constant, do the conversions to the signed
1825 type and subtract. The hardware will do the right thing with any
1826 overflow in the subtraction. */
1827 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1828 return size_binop_loc (loc
, MINUS_EXPR
,
1829 fold_convert_loc (loc
, ctype
, arg0
),
1830 fold_convert_loc (loc
, ctype
, arg1
));
1832 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1833 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1834 overflow) and negate (which can't either). Special-case a result
1835 of zero while we're here. */
1836 if (tree_int_cst_equal (arg0
, arg1
))
1837 return build_int_cst (ctype
, 0);
1838 else if (tree_int_cst_lt (arg1
, arg0
))
1839 return fold_convert_loc (loc
, ctype
,
1840 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1842 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1843 fold_convert_loc (loc
, ctype
,
1844 size_binop_loc (loc
,
1849 /* A subroutine of fold_convert_const handling conversions of an
1850 INTEGER_CST to another integer type. */
1853 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1855 /* Given an integer constant, make new constant with new type,
1856 appropriately sign-extended or truncated. Use widest_int
1857 so that any extension is done according ARG1's type. */
1858 return force_fit_type (type
, wi::to_widest (arg1
),
1859 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1860 TREE_OVERFLOW (arg1
));
1863 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1864 to an integer type. */
1867 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1869 bool overflow
= false;
1872 /* The following code implements the floating point to integer
1873 conversion rules required by the Java Language Specification,
1874 that IEEE NaNs are mapped to zero and values that overflow
1875 the target precision saturate, i.e. values greater than
1876 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1877 are mapped to INT_MIN. These semantics are allowed by the
1878 C and C++ standards that simply state that the behavior of
1879 FP-to-integer conversion is unspecified upon overflow. */
1883 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1887 case FIX_TRUNC_EXPR
:
1888 real_trunc (&r
, VOIDmode
, &x
);
1895 /* If R is NaN, return zero and show we have an overflow. */
1896 if (REAL_VALUE_ISNAN (r
))
1899 val
= wi::zero (TYPE_PRECISION (type
));
1902 /* See if R is less than the lower bound or greater than the
1907 tree lt
= TYPE_MIN_VALUE (type
);
1908 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1909 if (real_less (&r
, &l
))
1918 tree ut
= TYPE_MAX_VALUE (type
);
1921 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1922 if (real_less (&u
, &r
))
1931 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1933 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1937 /* A subroutine of fold_convert_const handling conversions of a
1938 FIXED_CST to an integer type. */
1941 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1944 double_int temp
, temp_trunc
;
1947 /* Right shift FIXED_CST to temp by fbit. */
1948 temp
= TREE_FIXED_CST (arg1
).data
;
1949 mode
= TREE_FIXED_CST (arg1
).mode
;
1950 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1952 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1953 HOST_BITS_PER_DOUBLE_INT
,
1954 SIGNED_FIXED_POINT_MODE_P (mode
));
1956 /* Left shift temp to temp_trunc by fbit. */
1957 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1958 HOST_BITS_PER_DOUBLE_INT
,
1959 SIGNED_FIXED_POINT_MODE_P (mode
));
1963 temp
= double_int_zero
;
1964 temp_trunc
= double_int_zero
;
1967 /* If FIXED_CST is negative, we need to round the value toward 0.
1968 By checking if the fractional bits are not zero to add 1 to temp. */
1969 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1970 && temp_trunc
.is_negative ()
1971 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1972 temp
+= double_int_one
;
1974 /* Given a fixed-point constant, make new constant with new type,
1975 appropriately sign-extended or truncated. */
1976 t
= force_fit_type (type
, temp
, -1,
1977 (temp
.is_negative ()
1978 && (TYPE_UNSIGNED (type
)
1979 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1980 | TREE_OVERFLOW (arg1
));
1985 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1986 to another floating point type. */
1989 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1991 REAL_VALUE_TYPE value
;
1994 /* Don't perform the operation if flag_signaling_nans is on
1995 and the operand is a signaling NaN. */
1996 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
1997 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2000 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2001 t
= build_real (type
, value
);
2003 /* If converting an infinity or NAN to a representation that doesn't
2004 have one, set the overflow bit so that we can produce some kind of
2005 error message at the appropriate point if necessary. It's not the
2006 most user-friendly message, but it's better than nothing. */
2007 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2008 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2009 TREE_OVERFLOW (t
) = 1;
2010 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2011 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2012 TREE_OVERFLOW (t
) = 1;
2013 /* Regular overflow, conversion produced an infinity in a mode that
2014 can't represent them. */
2015 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2016 && REAL_VALUE_ISINF (value
)
2017 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2018 TREE_OVERFLOW (t
) = 1;
2020 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2024 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2025 to a floating point type. */
2028 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2030 REAL_VALUE_TYPE value
;
2033 real_convert_from_fixed (&value
, SCALAR_FLOAT_TYPE_MODE (type
),
2034 &TREE_FIXED_CST (arg1
));
2035 t
= build_real (type
, value
);
2037 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2041 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2042 to another fixed-point type. */
2045 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2047 FIXED_VALUE_TYPE value
;
2051 overflow_p
= fixed_convert (&value
, SCALAR_TYPE_MODE (type
),
2052 &TREE_FIXED_CST (arg1
), TYPE_SATURATING (type
));
2053 t
= build_fixed (type
, value
);
2055 /* Propagate overflow flags. */
2056 if (overflow_p
| TREE_OVERFLOW (arg1
))
2057 TREE_OVERFLOW (t
) = 1;
2061 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2062 to a fixed-point type. */
2065 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2067 FIXED_VALUE_TYPE value
;
2072 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2074 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2075 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2076 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2078 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2080 overflow_p
= fixed_convert_from_int (&value
, SCALAR_TYPE_MODE (type
), di
,
2081 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2082 TYPE_SATURATING (type
));
2083 t
= build_fixed (type
, value
);
2085 /* Propagate overflow flags. */
2086 if (overflow_p
| TREE_OVERFLOW (arg1
))
2087 TREE_OVERFLOW (t
) = 1;
2091 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2092 to a fixed-point type. */
2095 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2097 FIXED_VALUE_TYPE value
;
2101 overflow_p
= fixed_convert_from_real (&value
, SCALAR_TYPE_MODE (type
),
2102 &TREE_REAL_CST (arg1
),
2103 TYPE_SATURATING (type
));
2104 t
= build_fixed (type
, value
);
2106 /* Propagate overflow flags. */
2107 if (overflow_p
| TREE_OVERFLOW (arg1
))
2108 TREE_OVERFLOW (t
) = 1;
2112 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2113 type TYPE. If no simplification can be done return NULL_TREE. */
2116 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2118 if (TREE_TYPE (arg1
) == type
)
2121 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2122 || TREE_CODE (type
) == OFFSET_TYPE
)
2124 if (TREE_CODE (arg1
) == INTEGER_CST
)
2125 return fold_convert_const_int_from_int (type
, arg1
);
2126 else if (TREE_CODE (arg1
) == REAL_CST
)
2127 return fold_convert_const_int_from_real (code
, type
, arg1
);
2128 else if (TREE_CODE (arg1
) == FIXED_CST
)
2129 return fold_convert_const_int_from_fixed (type
, arg1
);
2131 else if (TREE_CODE (type
) == REAL_TYPE
)
2133 if (TREE_CODE (arg1
) == INTEGER_CST
)
2134 return build_real_from_int_cst (type
, arg1
);
2135 else if (TREE_CODE (arg1
) == REAL_CST
)
2136 return fold_convert_const_real_from_real (type
, arg1
);
2137 else if (TREE_CODE (arg1
) == FIXED_CST
)
2138 return fold_convert_const_real_from_fixed (type
, arg1
);
2140 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2142 if (TREE_CODE (arg1
) == FIXED_CST
)
2143 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2144 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2145 return fold_convert_const_fixed_from_int (type
, arg1
);
2146 else if (TREE_CODE (arg1
) == REAL_CST
)
2147 return fold_convert_const_fixed_from_real (type
, arg1
);
2149 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2151 if (TREE_CODE (arg1
) == VECTOR_CST
2152 && TYPE_VECTOR_SUBPARTS (type
) == VECTOR_CST_NELTS (arg1
))
2154 int len
= VECTOR_CST_NELTS (arg1
);
2155 tree elttype
= TREE_TYPE (type
);
2156 auto_vec
<tree
, 32> v (len
);
2157 for (int i
= 0; i
< len
; ++i
)
2159 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2160 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2161 if (cvt
== NULL_TREE
)
2165 return build_vector (type
, v
);
2171 /* Construct a vector of zero elements of vector type TYPE. */
2174 build_zero_vector (tree type
)
2178 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2179 return build_vector_from_val (type
, t
);
2182 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2185 fold_convertible_p (const_tree type
, const_tree arg
)
2187 tree orig
= TREE_TYPE (arg
);
2192 if (TREE_CODE (arg
) == ERROR_MARK
2193 || TREE_CODE (type
) == ERROR_MARK
2194 || TREE_CODE (orig
) == ERROR_MARK
)
2197 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2200 switch (TREE_CODE (type
))
2202 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2203 case POINTER_TYPE
: case REFERENCE_TYPE
:
2205 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2206 || TREE_CODE (orig
) == OFFSET_TYPE
);
2209 case FIXED_POINT_TYPE
:
2212 return TREE_CODE (type
) == TREE_CODE (orig
);
2219 /* Convert expression ARG to type TYPE. Used by the middle-end for
2220 simple conversions in preference to calling the front-end's convert. */
2223 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2225 tree orig
= TREE_TYPE (arg
);
2231 if (TREE_CODE (arg
) == ERROR_MARK
2232 || TREE_CODE (type
) == ERROR_MARK
2233 || TREE_CODE (orig
) == ERROR_MARK
)
2234 return error_mark_node
;
2236 switch (TREE_CODE (type
))
2239 case REFERENCE_TYPE
:
2240 /* Handle conversions between pointers to different address spaces. */
2241 if (POINTER_TYPE_P (orig
)
2242 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2243 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2244 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2247 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2249 if (TREE_CODE (arg
) == INTEGER_CST
)
2251 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2252 if (tem
!= NULL_TREE
)
2255 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2256 || TREE_CODE (orig
) == OFFSET_TYPE
)
2257 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2258 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2259 return fold_convert_loc (loc
, type
,
2260 fold_build1_loc (loc
, REALPART_EXPR
,
2261 TREE_TYPE (orig
), arg
));
2262 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2263 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2264 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2267 if (TREE_CODE (arg
) == INTEGER_CST
)
2269 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2270 if (tem
!= NULL_TREE
)
2273 else if (TREE_CODE (arg
) == REAL_CST
)
2275 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2276 if (tem
!= NULL_TREE
)
2279 else if (TREE_CODE (arg
) == FIXED_CST
)
2281 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2282 if (tem
!= NULL_TREE
)
2286 switch (TREE_CODE (orig
))
2289 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2290 case POINTER_TYPE
: case REFERENCE_TYPE
:
2291 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2294 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2296 case FIXED_POINT_TYPE
:
2297 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2300 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2301 return fold_convert_loc (loc
, type
, tem
);
2307 case FIXED_POINT_TYPE
:
2308 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2309 || TREE_CODE (arg
) == REAL_CST
)
2311 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2312 if (tem
!= NULL_TREE
)
2313 goto fold_convert_exit
;
2316 switch (TREE_CODE (orig
))
2318 case FIXED_POINT_TYPE
:
2323 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2326 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2327 return fold_convert_loc (loc
, type
, tem
);
2334 switch (TREE_CODE (orig
))
2337 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2338 case POINTER_TYPE
: case REFERENCE_TYPE
:
2340 case FIXED_POINT_TYPE
:
2341 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2342 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2343 fold_convert_loc (loc
, TREE_TYPE (type
),
2344 integer_zero_node
));
2349 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2351 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2352 TREE_OPERAND (arg
, 0));
2353 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2354 TREE_OPERAND (arg
, 1));
2355 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2358 arg
= save_expr (arg
);
2359 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2360 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2361 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2362 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2363 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2371 if (integer_zerop (arg
))
2372 return build_zero_vector (type
);
2373 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2374 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2375 || TREE_CODE (orig
) == VECTOR_TYPE
);
2376 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2379 tem
= fold_ignored_result (arg
);
2380 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2383 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2384 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2388 protected_set_expr_location_unshare (tem
, loc
);
2392 /* Return false if expr can be assumed not to be an lvalue, true
2396 maybe_lvalue_p (const_tree x
)
2398 /* We only need to wrap lvalue tree codes. */
2399 switch (TREE_CODE (x
))
2412 case ARRAY_RANGE_REF
:
2418 case PREINCREMENT_EXPR
:
2419 case PREDECREMENT_EXPR
:
2421 case TRY_CATCH_EXPR
:
2422 case WITH_CLEANUP_EXPR
:
2431 /* Assume the worst for front-end tree codes. */
2432 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2440 /* Return an expr equal to X but certainly not valid as an lvalue. */
2443 non_lvalue_loc (location_t loc
, tree x
)
2445 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2450 if (! maybe_lvalue_p (x
))
2452 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2455 /* When pedantic, return an expr equal to X but certainly not valid as a
2456 pedantic lvalue. Otherwise, return X. */
2459 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2461 return protected_set_expr_location_unshare (x
, loc
);
2464 /* Given a tree comparison code, return the code that is the logical inverse.
2465 It is generally not safe to do this for floating-point comparisons, except
2466 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2467 ERROR_MARK in this case. */
2470 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2472 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2473 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2483 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2485 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2487 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2489 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2503 return UNORDERED_EXPR
;
2504 case UNORDERED_EXPR
:
2505 return ORDERED_EXPR
;
2511 /* Similar, but return the comparison that results if the operands are
2512 swapped. This is safe for floating-point. */
2515 swap_tree_comparison (enum tree_code code
)
2522 case UNORDERED_EXPR
:
2548 /* Convert a comparison tree code from an enum tree_code representation
2549 into a compcode bit-based encoding. This function is the inverse of
2550 compcode_to_comparison. */
2552 static enum comparison_code
2553 comparison_to_compcode (enum tree_code code
)
2570 return COMPCODE_ORD
;
2571 case UNORDERED_EXPR
:
2572 return COMPCODE_UNORD
;
2574 return COMPCODE_UNLT
;
2576 return COMPCODE_UNEQ
;
2578 return COMPCODE_UNLE
;
2580 return COMPCODE_UNGT
;
2582 return COMPCODE_LTGT
;
2584 return COMPCODE_UNGE
;
2590 /* Convert a compcode bit-based encoding of a comparison operator back
2591 to GCC's enum tree_code representation. This function is the
2592 inverse of comparison_to_compcode. */
2594 static enum tree_code
2595 compcode_to_comparison (enum comparison_code code
)
2612 return ORDERED_EXPR
;
2613 case COMPCODE_UNORD
:
2614 return UNORDERED_EXPR
;
2632 /* Return a tree for the comparison which is the combination of
2633 doing the AND or OR (depending on CODE) of the two operations LCODE
2634 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2635 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2636 if this makes the transformation invalid. */
2639 combine_comparisons (location_t loc
,
2640 enum tree_code code
, enum tree_code lcode
,
2641 enum tree_code rcode
, tree truth_type
,
2642 tree ll_arg
, tree lr_arg
)
2644 bool honor_nans
= HONOR_NANS (ll_arg
);
2645 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2646 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2651 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2652 compcode
= lcompcode
& rcompcode
;
2655 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2656 compcode
= lcompcode
| rcompcode
;
2665 /* Eliminate unordered comparisons, as well as LTGT and ORD
2666 which are not used unless the mode has NaNs. */
2667 compcode
&= ~COMPCODE_UNORD
;
2668 if (compcode
== COMPCODE_LTGT
)
2669 compcode
= COMPCODE_NE
;
2670 else if (compcode
== COMPCODE_ORD
)
2671 compcode
= COMPCODE_TRUE
;
2673 else if (flag_trapping_math
)
2675 /* Check that the original operation and the optimized ones will trap
2676 under the same condition. */
2677 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2678 && (lcompcode
!= COMPCODE_EQ
)
2679 && (lcompcode
!= COMPCODE_ORD
);
2680 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2681 && (rcompcode
!= COMPCODE_EQ
)
2682 && (rcompcode
!= COMPCODE_ORD
);
2683 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2684 && (compcode
!= COMPCODE_EQ
)
2685 && (compcode
!= COMPCODE_ORD
);
2687 /* In a short-circuited boolean expression the LHS might be
2688 such that the RHS, if evaluated, will never trap. For
2689 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2690 if neither x nor y is NaN. (This is a mixed blessing: for
2691 example, the expression above will never trap, hence
2692 optimizing it to x < y would be invalid). */
2693 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2694 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2697 /* If the comparison was short-circuited, and only the RHS
2698 trapped, we may now generate a spurious trap. */
2700 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2703 /* If we changed the conditions that cause a trap, we lose. */
2704 if ((ltrap
|| rtrap
) != trap
)
2708 if (compcode
== COMPCODE_TRUE
)
2709 return constant_boolean_node (true, truth_type
);
2710 else if (compcode
== COMPCODE_FALSE
)
2711 return constant_boolean_node (false, truth_type
);
2714 enum tree_code tcode
;
2716 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2717 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2721 /* Return nonzero if two operands (typically of the same tree node)
2722 are necessarily equal. FLAGS modifies behavior as follows:
2724 If OEP_ONLY_CONST is set, only return nonzero for constants.
2725 This function tests whether the operands are indistinguishable;
2726 it does not test whether they are equal using C's == operation.
2727 The distinction is important for IEEE floating point, because
2728 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2729 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2731 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2732 even though it may hold multiple values during a function.
2733 This is because a GCC tree node guarantees that nothing else is
2734 executed between the evaluation of its "operands" (which may often
2735 be evaluated in arbitrary order). Hence if the operands themselves
2736 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2737 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2738 unset means assuming isochronic (or instantaneous) tree equivalence.
2739 Unless comparing arbitrary expression trees, such as from different
2740 statements, this flag can usually be left unset.
2742 If OEP_PURE_SAME is set, then pure functions with identical arguments
2743 are considered the same. It is used when the caller has other ways
2744 to ensure that global memory is unchanged in between.
2746 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2747 not values of expressions.
2749 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2750 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2752 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2753 any operand with side effect. This is unnecesarily conservative in the
2754 case we know that arg0 and arg1 are in disjoint code paths (such as in
2755 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2756 addresses with TREE_CONSTANT flag set so we know that &var == &var
2757 even if var is volatile. */
2760 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2762 /* When checking, verify at the outermost operand_equal_p call that
2763 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2765 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
2767 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
2771 inchash::hash
hstate0 (0), hstate1 (0);
2772 inchash::add_expr (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
2773 inchash::add_expr (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
2774 hashval_t h0
= hstate0
.end ();
2775 hashval_t h1
= hstate1
.end ();
2776 gcc_assert (h0
== h1
);
2784 /* If either is ERROR_MARK, they aren't equal. */
2785 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2786 || TREE_TYPE (arg0
) == error_mark_node
2787 || TREE_TYPE (arg1
) == error_mark_node
)
2790 /* Similar, if either does not have a type (like a released SSA name),
2791 they aren't equal. */
2792 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2795 /* We cannot consider pointers to different address space equal. */
2796 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2797 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2798 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2799 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2802 /* Check equality of integer constants before bailing out due to
2803 precision differences. */
2804 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2806 /* Address of INTEGER_CST is not defined; check that we did not forget
2807 to drop the OEP_ADDRESS_OF flags. */
2808 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2809 return tree_int_cst_equal (arg0
, arg1
);
2812 if (!(flags
& OEP_ADDRESS_OF
))
2814 /* If both types don't have the same signedness, then we can't consider
2815 them equal. We must check this before the STRIP_NOPS calls
2816 because they may change the signedness of the arguments. As pointers
2817 strictly don't have a signedness, require either two pointers or
2818 two non-pointers as well. */
2819 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2820 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2821 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2824 /* If both types don't have the same precision, then it is not safe
2826 if (element_precision (TREE_TYPE (arg0
))
2827 != element_precision (TREE_TYPE (arg1
)))
2834 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2835 sanity check once the issue is solved. */
2837 /* Addresses of conversions and SSA_NAMEs (and many other things)
2838 are not defined. Check that we did not forget to drop the
2839 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2840 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2841 && TREE_CODE (arg0
) != SSA_NAME
);
2844 /* In case both args are comparisons but with different comparison
2845 code, try to swap the comparison operands of one arg to produce
2846 a match and compare that variant. */
2847 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2848 && COMPARISON_CLASS_P (arg0
)
2849 && COMPARISON_CLASS_P (arg1
))
2851 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2853 if (TREE_CODE (arg0
) == swap_code
)
2854 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2855 TREE_OPERAND (arg1
, 1), flags
)
2856 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2857 TREE_OPERAND (arg1
, 0), flags
);
2860 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2862 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2863 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2865 else if (flags
& OEP_ADDRESS_OF
)
2867 /* If we are interested in comparing addresses ignore
2868 MEM_REF wrappings of the base that can appear just for
2870 if (TREE_CODE (arg0
) == MEM_REF
2872 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2873 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2874 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2876 else if (TREE_CODE (arg1
) == MEM_REF
2878 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2879 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2880 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2888 /* When not checking adddresses, this is needed for conversions and for
2889 COMPONENT_REF. Might as well play it safe and always test this. */
2890 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2891 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2892 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2893 && !(flags
& OEP_ADDRESS_OF
)))
2896 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2897 We don't care about side effects in that case because the SAVE_EXPR
2898 takes care of that for us. In all other cases, two expressions are
2899 equal if they have no side effects. If we have two identical
2900 expressions with side effects that should be treated the same due
2901 to the only side effects being identical SAVE_EXPR's, that will
2902 be detected in the recursive calls below.
2903 If we are taking an invariant address of two identical objects
2904 they are necessarily equal as well. */
2905 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2906 && (TREE_CODE (arg0
) == SAVE_EXPR
2907 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
2908 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2911 /* Next handle constant cases, those for which we can return 1 even
2912 if ONLY_CONST is set. */
2913 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2914 switch (TREE_CODE (arg0
))
2917 return tree_int_cst_equal (arg0
, arg1
);
2920 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2921 TREE_FIXED_CST (arg1
));
2924 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2928 if (!HONOR_SIGNED_ZEROS (arg0
))
2930 /* If we do not distinguish between signed and unsigned zero,
2931 consider them equal. */
2932 if (real_zerop (arg0
) && real_zerop (arg1
))
2941 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2944 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2946 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2947 VECTOR_CST_ELT (arg1
, i
), flags
))
2954 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2956 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2960 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2961 && ! memcmp (TREE_STRING_POINTER (arg0
),
2962 TREE_STRING_POINTER (arg1
),
2963 TREE_STRING_LENGTH (arg0
)));
2966 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2967 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2968 flags
| OEP_ADDRESS_OF
2969 | OEP_MATCH_SIDE_EFFECTS
);
2971 /* In GIMPLE empty constructors are allowed in initializers of
2973 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
2978 if (flags
& OEP_ONLY_CONST
)
2981 /* Define macros to test an operand from arg0 and arg1 for equality and a
2982 variant that allows null and views null as being different from any
2983 non-null value. In the latter case, if either is null, the both
2984 must be; otherwise, do the normal comparison. */
2985 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2986 TREE_OPERAND (arg1, N), flags)
2988 #define OP_SAME_WITH_NULL(N) \
2989 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2990 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2992 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2995 /* Two conversions are equal only if signedness and modes match. */
2996 switch (TREE_CODE (arg0
))
2999 case FIX_TRUNC_EXPR
:
3000 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3001 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3011 case tcc_comparison
:
3013 if (OP_SAME (0) && OP_SAME (1))
3016 /* For commutative ops, allow the other order. */
3017 return (commutative_tree_code (TREE_CODE (arg0
))
3018 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3019 TREE_OPERAND (arg1
, 1), flags
)
3020 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3021 TREE_OPERAND (arg1
, 0), flags
));
3024 /* If either of the pointer (or reference) expressions we are
3025 dereferencing contain a side effect, these cannot be equal,
3026 but their addresses can be. */
3027 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3028 && (TREE_SIDE_EFFECTS (arg0
)
3029 || TREE_SIDE_EFFECTS (arg1
)))
3032 switch (TREE_CODE (arg0
))
3035 if (!(flags
& OEP_ADDRESS_OF
)
3036 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3037 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3039 flags
&= ~OEP_ADDRESS_OF
;
3043 /* Require the same offset. */
3044 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3045 TYPE_SIZE (TREE_TYPE (arg1
)),
3046 flags
& ~OEP_ADDRESS_OF
))
3051 case VIEW_CONVERT_EXPR
:
3054 case TARGET_MEM_REF
:
3056 if (!(flags
& OEP_ADDRESS_OF
))
3058 /* Require equal access sizes */
3059 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3060 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3061 || !TYPE_SIZE (TREE_TYPE (arg1
))
3062 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3063 TYPE_SIZE (TREE_TYPE (arg1
)),
3066 /* Verify that access happens in similar types. */
3067 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3069 /* Verify that accesses are TBAA compatible. */
3070 if (!alias_ptr_types_compatible_p
3071 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3072 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3073 || (MR_DEPENDENCE_CLIQUE (arg0
)
3074 != MR_DEPENDENCE_CLIQUE (arg1
))
3075 || (MR_DEPENDENCE_BASE (arg0
)
3076 != MR_DEPENDENCE_BASE (arg1
)))
3078 /* Verify that alignment is compatible. */
3079 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3080 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3083 flags
&= ~OEP_ADDRESS_OF
;
3084 return (OP_SAME (0) && OP_SAME (1)
3085 /* TARGET_MEM_REF require equal extra operands. */
3086 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3087 || (OP_SAME_WITH_NULL (2)
3088 && OP_SAME_WITH_NULL (3)
3089 && OP_SAME_WITH_NULL (4))));
3092 case ARRAY_RANGE_REF
:
3095 flags
&= ~OEP_ADDRESS_OF
;
3096 /* Compare the array index by value if it is constant first as we
3097 may have different types but same value here. */
3098 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3099 TREE_OPERAND (arg1
, 1))
3101 && OP_SAME_WITH_NULL (2)
3102 && OP_SAME_WITH_NULL (3)
3103 /* Compare low bound and element size as with OEP_ADDRESS_OF
3104 we have to account for the offset of the ref. */
3105 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3106 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3107 || (operand_equal_p (array_ref_low_bound
3108 (CONST_CAST_TREE (arg0
)),
3110 (CONST_CAST_TREE (arg1
)), flags
)
3111 && operand_equal_p (array_ref_element_size
3112 (CONST_CAST_TREE (arg0
)),
3113 array_ref_element_size
3114 (CONST_CAST_TREE (arg1
)),
3118 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3119 may be NULL when we're called to compare MEM_EXPRs. */
3120 if (!OP_SAME_WITH_NULL (0)
3123 flags
&= ~OEP_ADDRESS_OF
;
3124 return OP_SAME_WITH_NULL (2);
3129 flags
&= ~OEP_ADDRESS_OF
;
3130 return OP_SAME (1) && OP_SAME (2);
3136 case tcc_expression
:
3137 switch (TREE_CODE (arg0
))
3140 /* Be sure we pass right ADDRESS_OF flag. */
3141 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3142 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3143 TREE_OPERAND (arg1
, 0),
3144 flags
| OEP_ADDRESS_OF
);
3146 case TRUTH_NOT_EXPR
:
3149 case TRUTH_ANDIF_EXPR
:
3150 case TRUTH_ORIF_EXPR
:
3151 return OP_SAME (0) && OP_SAME (1);
3154 case WIDEN_MULT_PLUS_EXPR
:
3155 case WIDEN_MULT_MINUS_EXPR
:
3158 /* The multiplcation operands are commutative. */
3161 case TRUTH_AND_EXPR
:
3163 case TRUTH_XOR_EXPR
:
3164 if (OP_SAME (0) && OP_SAME (1))
3167 /* Otherwise take into account this is a commutative operation. */
3168 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3169 TREE_OPERAND (arg1
, 1), flags
)
3170 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3171 TREE_OPERAND (arg1
, 0), flags
));
3174 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3176 flags
&= ~OEP_ADDRESS_OF
;
3179 case BIT_INSERT_EXPR
:
3180 /* BIT_INSERT_EXPR has an implict operand as the type precision
3181 of op1. Need to check to make sure they are the same. */
3182 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3183 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
3184 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
3185 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 1))))
3191 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3196 case PREDECREMENT_EXPR
:
3197 case PREINCREMENT_EXPR
:
3198 case POSTDECREMENT_EXPR
:
3199 case POSTINCREMENT_EXPR
:
3200 if (flags
& OEP_LEXICOGRAPHIC
)
3201 return OP_SAME (0) && OP_SAME (1);
3204 case CLEANUP_POINT_EXPR
:
3206 if (flags
& OEP_LEXICOGRAPHIC
)
3215 switch (TREE_CODE (arg0
))
3218 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3219 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3220 /* If not both CALL_EXPRs are either internal or normal function
3221 functions, then they are not equal. */
3223 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3225 /* If the CALL_EXPRs call different internal functions, then they
3227 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3232 /* If the CALL_EXPRs call different functions, then they are not
3234 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3239 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3241 unsigned int cef
= call_expr_flags (arg0
);
3242 if (flags
& OEP_PURE_SAME
)
3243 cef
&= ECF_CONST
| ECF_PURE
;
3246 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3250 /* Now see if all the arguments are the same. */
3252 const_call_expr_arg_iterator iter0
, iter1
;
3254 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3255 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3257 a0
= next_const_call_expr_arg (&iter0
),
3258 a1
= next_const_call_expr_arg (&iter1
))
3259 if (! operand_equal_p (a0
, a1
, flags
))
3262 /* If we get here and both argument lists are exhausted
3263 then the CALL_EXPRs are equal. */
3264 return ! (a0
|| a1
);
3270 case tcc_declaration
:
3271 /* Consider __builtin_sqrt equal to sqrt. */
3272 return (TREE_CODE (arg0
) == FUNCTION_DECL
3273 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3274 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3275 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3277 case tcc_exceptional
:
3278 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3280 /* In GIMPLE constructors are used only to build vectors from
3281 elements. Individual elements in the constructor must be
3282 indexed in increasing order and form an initial sequence.
3284 We make no effort to compare constructors in generic.
3285 (see sem_variable::equals in ipa-icf which can do so for
3287 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3288 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3291 /* Be sure that vectors constructed have the same representation.
3292 We only tested element precision and modes to match.
3293 Vectors may be BLKmode and thus also check that the number of
3295 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3296 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3299 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3300 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3301 unsigned int len
= vec_safe_length (v0
);
3303 if (len
!= vec_safe_length (v1
))
3306 for (unsigned int i
= 0; i
< len
; i
++)
3308 constructor_elt
*c0
= &(*v0
)[i
];
3309 constructor_elt
*c1
= &(*v1
)[i
];
3311 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3312 /* In GIMPLE the indexes can be either NULL or matching i.
3313 Double check this so we won't get false
3314 positives for GENERIC. */
3316 && (TREE_CODE (c0
->index
) != INTEGER_CST
3317 || !compare_tree_int (c0
->index
, i
)))
3319 && (TREE_CODE (c1
->index
) != INTEGER_CST
3320 || !compare_tree_int (c1
->index
, i
))))
3325 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3326 && (flags
& OEP_LEXICOGRAPHIC
))
3328 /* Compare the STATEMENT_LISTs. */
3329 tree_stmt_iterator tsi1
, tsi2
;
3330 tree body1
= CONST_CAST_TREE (arg0
);
3331 tree body2
= CONST_CAST_TREE (arg1
);
3332 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3333 tsi_next (&tsi1
), tsi_next (&tsi2
))
3335 /* The lists don't have the same number of statements. */
3336 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3338 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3340 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3348 switch (TREE_CODE (arg0
))
3351 if (flags
& OEP_LEXICOGRAPHIC
)
3352 return OP_SAME_WITH_NULL (0);
3363 #undef OP_SAME_WITH_NULL
3366 /* Similar to operand_equal_p, but strip nops first. */
3369 operand_equal_for_comparison_p (tree arg0
, tree arg1
)
3371 if (operand_equal_p (arg0
, arg1
, 0))
3374 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3375 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3378 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3379 and see if the inner values are the same. This removes any
3380 signedness comparison, which doesn't matter here. */
3383 if (operand_equal_p (arg0
, arg1
, 0))
3389 /* See if ARG is an expression that is either a comparison or is performing
3390 arithmetic on comparisons. The comparisons must only be comparing
3391 two different values, which will be stored in *CVAL1 and *CVAL2; if
3392 they are nonzero it means that some operands have already been found.
3393 No variables may be used anywhere else in the expression except in the
3394 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3395 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3397 If this is true, return 1. Otherwise, return zero. */
3400 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3402 enum tree_code code
= TREE_CODE (arg
);
3403 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3405 /* We can handle some of the tcc_expression cases here. */
3406 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3408 else if (tclass
== tcc_expression
3409 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3410 || code
== COMPOUND_EXPR
))
3411 tclass
= tcc_binary
;
3413 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3414 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3416 /* If we've already found a CVAL1 or CVAL2, this expression is
3417 two complex to handle. */
3418 if (*cval1
|| *cval2
)
3428 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3431 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3432 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3433 cval1
, cval2
, save_p
));
3438 case tcc_expression
:
3439 if (code
== COND_EXPR
)
3440 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3441 cval1
, cval2
, save_p
)
3442 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3443 cval1
, cval2
, save_p
)
3444 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3445 cval1
, cval2
, save_p
));
3448 case tcc_comparison
:
3449 /* First see if we can handle the first operand, then the second. For
3450 the second operand, we know *CVAL1 can't be zero. It must be that
3451 one side of the comparison is each of the values; test for the
3452 case where this isn't true by failing if the two operands
3455 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3456 TREE_OPERAND (arg
, 1), 0))
3460 *cval1
= TREE_OPERAND (arg
, 0);
3461 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3463 else if (*cval2
== 0)
3464 *cval2
= TREE_OPERAND (arg
, 0);
3465 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3470 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3472 else if (*cval2
== 0)
3473 *cval2
= TREE_OPERAND (arg
, 1);
3474 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3486 /* ARG is a tree that is known to contain just arithmetic operations and
3487 comparisons. Evaluate the operations in the tree substituting NEW0 for
3488 any occurrence of OLD0 as an operand of a comparison and likewise for
3492 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3493 tree old1
, tree new1
)
3495 tree type
= TREE_TYPE (arg
);
3496 enum tree_code code
= TREE_CODE (arg
);
3497 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3499 /* We can handle some of the tcc_expression cases here. */
3500 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3502 else if (tclass
== tcc_expression
3503 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3504 tclass
= tcc_binary
;
3509 return fold_build1_loc (loc
, code
, type
,
3510 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3511 old0
, new0
, old1
, new1
));
3514 return fold_build2_loc (loc
, code
, type
,
3515 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3516 old0
, new0
, old1
, new1
),
3517 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3518 old0
, new0
, old1
, new1
));
3520 case tcc_expression
:
3524 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3528 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3532 return fold_build3_loc (loc
, code
, type
,
3533 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3534 old0
, new0
, old1
, new1
),
3535 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3536 old0
, new0
, old1
, new1
),
3537 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3538 old0
, new0
, old1
, new1
));
3542 /* Fall through - ??? */
3544 case tcc_comparison
:
3546 tree arg0
= TREE_OPERAND (arg
, 0);
3547 tree arg1
= TREE_OPERAND (arg
, 1);
3549 /* We need to check both for exact equality and tree equality. The
3550 former will be true if the operand has a side-effect. In that
3551 case, we know the operand occurred exactly once. */
3553 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3555 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3558 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3560 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3563 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3571 /* Return a tree for the case when the result of an expression is RESULT
3572 converted to TYPE and OMITTED was previously an operand of the expression
3573 but is now not needed (e.g., we folded OMITTED * 0).
3575 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3576 the conversion of RESULT to TYPE. */
3579 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3581 tree t
= fold_convert_loc (loc
, type
, result
);
3583 /* If the resulting operand is an empty statement, just return the omitted
3584 statement casted to void. */
3585 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3586 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3587 fold_ignored_result (omitted
));
3589 if (TREE_SIDE_EFFECTS (omitted
))
3590 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3591 fold_ignored_result (omitted
), t
);
3593 return non_lvalue_loc (loc
, t
);
3596 /* Return a tree for the case when the result of an expression is RESULT
3597 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3598 of the expression but are now not needed.
3600 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3601 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3602 evaluated before OMITTED2. Otherwise, if neither has side effects,
3603 just do the conversion of RESULT to TYPE. */
3606 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3607 tree omitted1
, tree omitted2
)
3609 tree t
= fold_convert_loc (loc
, type
, result
);
3611 if (TREE_SIDE_EFFECTS (omitted2
))
3612 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3613 if (TREE_SIDE_EFFECTS (omitted1
))
3614 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3616 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3620 /* Return a simplified tree node for the truth-negation of ARG. This
3621 never alters ARG itself. We assume that ARG is an operation that
3622 returns a truth value (0 or 1).
3624 FIXME: one would think we would fold the result, but it causes
3625 problems with the dominator optimizer. */
3628 fold_truth_not_expr (location_t loc
, tree arg
)
3630 tree type
= TREE_TYPE (arg
);
3631 enum tree_code code
= TREE_CODE (arg
);
3632 location_t loc1
, loc2
;
3634 /* If this is a comparison, we can simply invert it, except for
3635 floating-point non-equality comparisons, in which case we just
3636 enclose a TRUTH_NOT_EXPR around what we have. */
3638 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3640 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3641 if (FLOAT_TYPE_P (op_type
)
3642 && flag_trapping_math
3643 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3644 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3647 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3648 if (code
== ERROR_MARK
)
3651 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3652 TREE_OPERAND (arg
, 1));
3653 if (TREE_NO_WARNING (arg
))
3654 TREE_NO_WARNING (ret
) = 1;
3661 return constant_boolean_node (integer_zerop (arg
), type
);
3663 case TRUTH_AND_EXPR
:
3664 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3665 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3666 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3667 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3668 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3671 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3672 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3673 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3674 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3675 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3677 case TRUTH_XOR_EXPR
:
3678 /* Here we can invert either operand. We invert the first operand
3679 unless the second operand is a TRUTH_NOT_EXPR in which case our
3680 result is the XOR of the first operand with the inside of the
3681 negation of the second operand. */
3683 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3684 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3685 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3687 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3688 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3689 TREE_OPERAND (arg
, 1));
3691 case TRUTH_ANDIF_EXPR
:
3692 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3693 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3694 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3695 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3696 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3698 case TRUTH_ORIF_EXPR
:
3699 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3700 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3701 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3702 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3703 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3705 case TRUTH_NOT_EXPR
:
3706 return TREE_OPERAND (arg
, 0);
3710 tree arg1
= TREE_OPERAND (arg
, 1);
3711 tree arg2
= TREE_OPERAND (arg
, 2);
3713 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3714 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3716 /* A COND_EXPR may have a throw as one operand, which
3717 then has void type. Just leave void operands
3719 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3720 VOID_TYPE_P (TREE_TYPE (arg1
))
3721 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3722 VOID_TYPE_P (TREE_TYPE (arg2
))
3723 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3727 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3728 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3729 TREE_OPERAND (arg
, 0),
3730 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3732 case NON_LVALUE_EXPR
:
3733 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3734 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3737 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3738 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3743 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3744 return build1_loc (loc
, TREE_CODE (arg
), type
,
3745 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3748 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3750 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3753 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3755 case CLEANUP_POINT_EXPR
:
3756 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3757 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3758 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3765 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3766 assume that ARG is an operation that returns a truth value (0 or 1
3767 for scalars, 0 or -1 for vectors). Return the folded expression if
3768 folding is successful. Otherwise, return NULL_TREE. */
3771 fold_invert_truthvalue (location_t loc
, tree arg
)
3773 tree type
= TREE_TYPE (arg
);
3774 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3780 /* Return a simplified tree node for the truth-negation of ARG. This
3781 never alters ARG itself. We assume that ARG is an operation that
3782 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3785 invert_truthvalue_loc (location_t loc
, tree arg
)
3787 if (TREE_CODE (arg
) == ERROR_MARK
)
3790 tree type
= TREE_TYPE (arg
);
3791 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3797 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3798 with code CODE. This optimization is unsafe. */
3800 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3801 tree arg0
, tree arg1
)
3803 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3804 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3806 /* (A / C) +- (B / C) -> (A +- B) / C. */
3808 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3809 TREE_OPERAND (arg1
, 1), 0))
3810 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3811 fold_build2_loc (loc
, code
, type
,
3812 TREE_OPERAND (arg0
, 0),
3813 TREE_OPERAND (arg1
, 0)),
3814 TREE_OPERAND (arg0
, 1));
3816 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3817 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3818 TREE_OPERAND (arg1
, 0), 0)
3819 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3820 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3822 REAL_VALUE_TYPE r0
, r1
;
3823 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3824 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3826 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3828 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3829 real_arithmetic (&r0
, code
, &r0
, &r1
);
3830 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3831 TREE_OPERAND (arg0
, 0),
3832 build_real (type
, r0
));
3838 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3839 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3840 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3841 is the original memory reference used to preserve the alias set of
3845 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
3846 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
3847 int unsignedp
, int reversep
)
3849 tree result
, bftype
;
3851 /* Attempt not to lose the access path if possible. */
3852 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
3854 tree ninner
= TREE_OPERAND (orig_inner
, 0);
3856 HOST_WIDE_INT nbitsize
, nbitpos
;
3858 int nunsignedp
, nreversep
, nvolatilep
= 0;
3859 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
3860 &noffset
, &nmode
, &nunsignedp
,
3861 &nreversep
, &nvolatilep
);
3863 && noffset
== NULL_TREE
3864 && nbitsize
>= bitsize
3865 && nbitpos
<= bitpos
3866 && bitpos
+ bitsize
<= nbitpos
+ nbitsize
3876 alias_set_type iset
= get_alias_set (orig_inner
);
3877 if (iset
== 0 && get_alias_set (inner
) != iset
)
3878 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
3879 build_fold_addr_expr (inner
),
3880 build_int_cst (ptr_type_node
, 0));
3882 if (bitpos
== 0 && !reversep
)
3884 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3885 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3886 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3887 && tree_fits_shwi_p (size
)
3888 && tree_to_shwi (size
) == bitsize
)
3889 return fold_convert_loc (loc
, type
, inner
);
3893 if (TYPE_PRECISION (bftype
) != bitsize
3894 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3895 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3897 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3898 bitsize_int (bitsize
), bitsize_int (bitpos
));
3899 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
3902 result
= fold_convert_loc (loc
, type
, result
);
3907 /* Optimize a bit-field compare.
3909 There are two cases: First is a compare against a constant and the
3910 second is a comparison of two items where the fields are at the same
3911 bit position relative to the start of a chunk (byte, halfword, word)
3912 large enough to contain it. In these cases we can avoid the shift
3913 implicit in bitfield extractions.
3915 For constants, we emit a compare of the shifted constant with the
3916 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3917 compared. For two fields at the same position, we do the ANDs with the
3918 similar mask and compare the result of the ANDs.
3920 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3921 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3922 are the left and right operands of the comparison, respectively.
3924 If the optimization described above can be done, we return the resulting
3925 tree. Otherwise we return zero. */
3928 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3929 tree compare_type
, tree lhs
, tree rhs
)
3931 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3932 tree type
= TREE_TYPE (lhs
);
3934 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3935 machine_mode lmode
, rmode
;
3936 scalar_int_mode nmode
;
3937 int lunsignedp
, runsignedp
;
3938 int lreversep
, rreversep
;
3939 int lvolatilep
= 0, rvolatilep
= 0;
3940 tree linner
, rinner
= NULL_TREE
;
3944 /* Get all the information about the extractions being done. If the bit size
3945 if the same as the size of the underlying object, we aren't doing an
3946 extraction at all and so can do nothing. We also don't want to
3947 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3948 then will no longer be able to replace it. */
3949 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3950 &lunsignedp
, &lreversep
, &lvolatilep
);
3951 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3952 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3956 rreversep
= lreversep
;
3959 /* If this is not a constant, we can only do something if bit positions,
3960 sizes, signedness and storage order are the same. */
3962 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3963 &runsignedp
, &rreversep
, &rvolatilep
);
3965 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3966 || lunsignedp
!= runsignedp
|| lreversep
!= rreversep
|| offset
!= 0
3967 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3971 /* Honor the C++ memory model and mimic what RTL expansion does. */
3972 unsigned HOST_WIDE_INT bitstart
= 0;
3973 unsigned HOST_WIDE_INT bitend
= 0;
3974 if (TREE_CODE (lhs
) == COMPONENT_REF
)
3976 get_bit_range (&bitstart
, &bitend
, lhs
, &lbitpos
, &offset
);
3977 if (offset
!= NULL_TREE
)
3981 /* See if we can find a mode to refer to this field. We should be able to,
3982 but fail if we can't. */
3983 if (!get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
3984 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3985 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3986 TYPE_ALIGN (TREE_TYPE (rinner
))),
3987 BITS_PER_WORD
, false, &nmode
))
3990 /* Set signed and unsigned types of the precision of this mode for the
3992 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3994 /* Compute the bit position and size for the new reference and our offset
3995 within it. If the new reference is the same size as the original, we
3996 won't optimize anything, so return zero. */
3997 nbitsize
= GET_MODE_BITSIZE (nmode
);
3998 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4000 if (nbitsize
== lbitsize
)
4003 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4004 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4006 /* Make the mask to be used against the extracted field. */
4007 mask
= build_int_cst_type (unsigned_type
, -1);
4008 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4009 mask
= const_binop (RSHIFT_EXPR
, mask
,
4010 size_int (nbitsize
- lbitsize
- lbitpos
));
4013 /* If not comparing with constant, just rework the comparison
4015 return fold_build2_loc (loc
, code
, compare_type
,
4016 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4017 make_bit_field_ref (loc
, linner
, lhs
,
4022 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4023 make_bit_field_ref (loc
, rinner
, rhs
,
4029 /* Otherwise, we are handling the constant case. See if the constant is too
4030 big for the field. Warn and return a tree for 0 (false) if so. We do
4031 this not only for its own sake, but to avoid having to test for this
4032 error case below. If we didn't, we might generate wrong code.
4034 For unsigned fields, the constant shifted right by the field length should
4035 be all zero. For signed fields, the high-order bits should agree with
4040 if (wi::lrshift (rhs
, lbitsize
) != 0)
4042 warning (0, "comparison is always %d due to width of bit-field",
4044 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4049 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
4050 if (tem
!= 0 && tem
!= -1)
4052 warning (0, "comparison is always %d due to width of bit-field",
4054 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4058 /* Single-bit compares should always be against zero. */
4059 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4061 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4062 rhs
= build_int_cst (type
, 0);
4065 /* Make a new bitfield reference, shift the constant over the
4066 appropriate number of bits and mask it with the computed mask
4067 (in case this was a signed field). If we changed it, make a new one. */
4068 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4069 nbitsize
, nbitpos
, 1, lreversep
);
4071 rhs
= const_binop (BIT_AND_EXPR
,
4072 const_binop (LSHIFT_EXPR
,
4073 fold_convert_loc (loc
, unsigned_type
, rhs
),
4074 size_int (lbitpos
)),
4077 lhs
= build2_loc (loc
, code
, compare_type
,
4078 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4082 /* Subroutine for fold_truth_andor_1: decode a field reference.
4084 If EXP is a comparison reference, we return the innermost reference.
4086 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4087 set to the starting bit number.
4089 If the innermost field can be completely contained in a mode-sized
4090 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4092 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4093 otherwise it is not changed.
4095 *PUNSIGNEDP is set to the signedness of the field.
4097 *PREVERSEP is set to the storage order of the field.
4099 *PMASK is set to the mask used. This is either contained in a
4100 BIT_AND_EXPR or derived from the width of the field.
4102 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4104 Return 0 if this is not a component reference or is one that we can't
4105 do anything with. */
4108 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4109 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4110 int *punsignedp
, int *preversep
, int *pvolatilep
,
4111 tree
*pmask
, tree
*pand_mask
)
4114 tree outer_type
= 0;
4116 tree mask
, inner
, offset
;
4118 unsigned int precision
;
4120 /* All the optimizations using this function assume integer fields.
4121 There are problems with FP fields since the type_for_size call
4122 below can fail for, e.g., XFmode. */
4123 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4126 /* We are interested in the bare arrangement of bits, so strip everything
4127 that doesn't affect the machine mode. However, record the type of the
4128 outermost expression if it may matter below. */
4129 if (CONVERT_EXPR_P (exp
)
4130 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4131 outer_type
= TREE_TYPE (exp
);
4134 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4136 and_mask
= TREE_OPERAND (exp
, 1);
4137 exp
= TREE_OPERAND (exp
, 0);
4138 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4139 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4143 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
4144 punsignedp
, preversep
, pvolatilep
);
4145 if ((inner
== exp
&& and_mask
== 0)
4146 || *pbitsize
< 0 || offset
!= 0
4147 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4148 /* Reject out-of-bound accesses (PR79731). */
4149 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4150 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4151 *pbitpos
+ *pbitsize
) < 0))
4156 /* If the number of bits in the reference is the same as the bitsize of
4157 the outer type, then the outer type gives the signedness. Otherwise
4158 (in case of a small bitfield) the signedness is unchanged. */
4159 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4160 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4162 /* Compute the mask to access the bitfield. */
4163 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4164 precision
= TYPE_PRECISION (unsigned_type
);
4166 mask
= build_int_cst_type (unsigned_type
, -1);
4168 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4169 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4171 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4173 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4174 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4177 *pand_mask
= and_mask
;
4181 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4182 bit positions and MASK is SIGNED. */
4185 all_ones_mask_p (const_tree mask
, unsigned int size
)
4187 tree type
= TREE_TYPE (mask
);
4188 unsigned int precision
= TYPE_PRECISION (type
);
4190 /* If this function returns true when the type of the mask is
4191 UNSIGNED, then there will be errors. In particular see
4192 gcc.c-torture/execute/990326-1.c. There does not appear to be
4193 any documentation paper trail as to why this is so. But the pre
4194 wide-int worked with that restriction and it has been preserved
4196 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4199 return wi::mask (size
, false, precision
) == mask
;
4202 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4203 represents the sign bit of EXP's type. If EXP represents a sign
4204 or zero extension, also test VAL against the unextended type.
4205 The return value is the (sub)expression whose sign bit is VAL,
4206 or NULL_TREE otherwise. */
4209 sign_bit_p (tree exp
, const_tree val
)
4214 /* Tree EXP must have an integral type. */
4215 t
= TREE_TYPE (exp
);
4216 if (! INTEGRAL_TYPE_P (t
))
4219 /* Tree VAL must be an integer constant. */
4220 if (TREE_CODE (val
) != INTEGER_CST
4221 || TREE_OVERFLOW (val
))
4224 width
= TYPE_PRECISION (t
);
4225 if (wi::only_sign_bit_p (val
, width
))
4228 /* Handle extension from a narrower type. */
4229 if (TREE_CODE (exp
) == NOP_EXPR
4230 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4231 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4236 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4237 to be evaluated unconditionally. */
4240 simple_operand_p (const_tree exp
)
4242 /* Strip any conversions that don't change the machine mode. */
4245 return (CONSTANT_CLASS_P (exp
)
4246 || TREE_CODE (exp
) == SSA_NAME
4248 && ! TREE_ADDRESSABLE (exp
)
4249 && ! TREE_THIS_VOLATILE (exp
)
4250 && ! DECL_NONLOCAL (exp
)
4251 /* Don't regard global variables as simple. They may be
4252 allocated in ways unknown to the compiler (shared memory,
4253 #pragma weak, etc). */
4254 && ! TREE_PUBLIC (exp
)
4255 && ! DECL_EXTERNAL (exp
)
4256 /* Weakrefs are not safe to be read, since they can be NULL.
4257 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4258 have DECL_WEAK flag set. */
4259 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4260 /* Loading a static variable is unduly expensive, but global
4261 registers aren't expensive. */
4262 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4265 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4266 to be evaluated unconditionally.
4267 I addition to simple_operand_p, we assume that comparisons, conversions,
4268 and logic-not operations are simple, if their operands are simple, too. */
4271 simple_operand_p_2 (tree exp
)
4273 enum tree_code code
;
4275 if (TREE_SIDE_EFFECTS (exp
)
4276 || tree_could_trap_p (exp
))
4279 while (CONVERT_EXPR_P (exp
))
4280 exp
= TREE_OPERAND (exp
, 0);
4282 code
= TREE_CODE (exp
);
4284 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4285 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4286 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4288 if (code
== TRUTH_NOT_EXPR
)
4289 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4291 return simple_operand_p (exp
);
4295 /* The following functions are subroutines to fold_range_test and allow it to
4296 try to change a logical combination of comparisons into a range test.
4299 X == 2 || X == 3 || X == 4 || X == 5
4303 (unsigned) (X - 2) <= 3
4305 We describe each set of comparisons as being either inside or outside
4306 a range, using a variable named like IN_P, and then describe the
4307 range with a lower and upper bound. If one of the bounds is omitted,
4308 it represents either the highest or lowest value of the type.
4310 In the comments below, we represent a range by two numbers in brackets
4311 preceded by a "+" to designate being inside that range, or a "-" to
4312 designate being outside that range, so the condition can be inverted by
4313 flipping the prefix. An omitted bound is represented by a "-". For
4314 example, "- [-, 10]" means being outside the range starting at the lowest
4315 possible value and ending at 10, in other words, being greater than 10.
4316 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4319 We set up things so that the missing bounds are handled in a consistent
4320 manner so neither a missing bound nor "true" and "false" need to be
4321 handled using a special case. */
4323 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4324 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4325 and UPPER1_P are nonzero if the respective argument is an upper bound
4326 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4327 must be specified for a comparison. ARG1 will be converted to ARG0's
4328 type if both are specified. */
4331 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4332 tree arg1
, int upper1_p
)
4338 /* If neither arg represents infinity, do the normal operation.
4339 Else, if not a comparison, return infinity. Else handle the special
4340 comparison rules. Note that most of the cases below won't occur, but
4341 are handled for consistency. */
4343 if (arg0
!= 0 && arg1
!= 0)
4345 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4346 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4348 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4351 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4354 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4355 for neither. In real maths, we cannot assume open ended ranges are
4356 the same. But, this is computer arithmetic, where numbers are finite.
4357 We can therefore make the transformation of any unbounded range with
4358 the value Z, Z being greater than any representable number. This permits
4359 us to treat unbounded ranges as equal. */
4360 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4361 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4365 result
= sgn0
== sgn1
;
4368 result
= sgn0
!= sgn1
;
4371 result
= sgn0
< sgn1
;
4374 result
= sgn0
<= sgn1
;
4377 result
= sgn0
> sgn1
;
4380 result
= sgn0
>= sgn1
;
4386 return constant_boolean_node (result
, type
);
4389 /* Helper routine for make_range. Perform one step for it, return
4390 new expression if the loop should continue or NULL_TREE if it should
4394 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4395 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4396 bool *strict_overflow_p
)
4398 tree arg0_type
= TREE_TYPE (arg0
);
4399 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4400 int in_p
= *p_in_p
, n_in_p
;
4404 case TRUTH_NOT_EXPR
:
4405 /* We can only do something if the range is testing for zero. */
4406 if (low
== NULL_TREE
|| high
== NULL_TREE
4407 || ! integer_zerop (low
) || ! integer_zerop (high
))
4412 case EQ_EXPR
: case NE_EXPR
:
4413 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4414 /* We can only do something if the range is testing for zero
4415 and if the second operand is an integer constant. Note that
4416 saying something is "in" the range we make is done by
4417 complementing IN_P since it will set in the initial case of
4418 being not equal to zero; "out" is leaving it alone. */
4419 if (low
== NULL_TREE
|| high
== NULL_TREE
4420 || ! integer_zerop (low
) || ! integer_zerop (high
)
4421 || TREE_CODE (arg1
) != INTEGER_CST
)
4426 case NE_EXPR
: /* - [c, c] */
4429 case EQ_EXPR
: /* + [c, c] */
4430 in_p
= ! in_p
, low
= high
= arg1
;
4432 case GT_EXPR
: /* - [-, c] */
4433 low
= 0, high
= arg1
;
4435 case GE_EXPR
: /* + [c, -] */
4436 in_p
= ! in_p
, low
= arg1
, high
= 0;
4438 case LT_EXPR
: /* - [c, -] */
4439 low
= arg1
, high
= 0;
4441 case LE_EXPR
: /* + [-, c] */
4442 in_p
= ! in_p
, low
= 0, high
= arg1
;
4448 /* If this is an unsigned comparison, we also know that EXP is
4449 greater than or equal to zero. We base the range tests we make
4450 on that fact, so we record it here so we can parse existing
4451 range tests. We test arg0_type since often the return type
4452 of, e.g. EQ_EXPR, is boolean. */
4453 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4455 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4457 build_int_cst (arg0_type
, 0),
4461 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4463 /* If the high bound is missing, but we have a nonzero low
4464 bound, reverse the range so it goes from zero to the low bound
4466 if (high
== 0 && low
&& ! integer_zerop (low
))
4469 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4470 build_int_cst (TREE_TYPE (low
), 1), 0);
4471 low
= build_int_cst (arg0_type
, 0);
4481 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4482 low and high are non-NULL, then normalize will DTRT. */
4483 if (!TYPE_UNSIGNED (arg0_type
)
4484 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4486 if (low
== NULL_TREE
)
4487 low
= TYPE_MIN_VALUE (arg0_type
);
4488 if (high
== NULL_TREE
)
4489 high
= TYPE_MAX_VALUE (arg0_type
);
4492 /* (-x) IN [a,b] -> x in [-b, -a] */
4493 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4494 build_int_cst (exp_type
, 0),
4496 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4497 build_int_cst (exp_type
, 0),
4499 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4505 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4506 build_int_cst (exp_type
, 1));
4510 if (TREE_CODE (arg1
) != INTEGER_CST
)
4513 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4514 move a constant to the other side. */
4515 if (!TYPE_UNSIGNED (arg0_type
)
4516 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4519 /* If EXP is signed, any overflow in the computation is undefined,
4520 so we don't worry about it so long as our computations on
4521 the bounds don't overflow. For unsigned, overflow is defined
4522 and this is exactly the right thing. */
4523 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4524 arg0_type
, low
, 0, arg1
, 0);
4525 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4526 arg0_type
, high
, 1, arg1
, 0);
4527 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4528 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4531 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4532 *strict_overflow_p
= true;
4535 /* Check for an unsigned range which has wrapped around the maximum
4536 value thus making n_high < n_low, and normalize it. */
4537 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4539 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4540 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4541 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4542 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4544 /* If the range is of the form +/- [ x+1, x ], we won't
4545 be able to normalize it. But then, it represents the
4546 whole range or the empty set, so make it
4548 if (tree_int_cst_equal (n_low
, low
)
4549 && tree_int_cst_equal (n_high
, high
))
4555 low
= n_low
, high
= n_high
;
4563 case NON_LVALUE_EXPR
:
4564 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4567 if (! INTEGRAL_TYPE_P (arg0_type
)
4568 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4569 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4572 n_low
= low
, n_high
= high
;
4575 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4578 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4580 /* If we're converting arg0 from an unsigned type, to exp,
4581 a signed type, we will be doing the comparison as unsigned.
4582 The tests above have already verified that LOW and HIGH
4585 So we have to ensure that we will handle large unsigned
4586 values the same way that the current signed bounds treat
4589 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4593 /* For fixed-point modes, we need to pass the saturating flag
4594 as the 2nd parameter. */
4595 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4597 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4598 TYPE_SATURATING (arg0_type
));
4601 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4603 /* A range without an upper bound is, naturally, unbounded.
4604 Since convert would have cropped a very large value, use
4605 the max value for the destination type. */
4607 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4608 : TYPE_MAX_VALUE (arg0_type
);
4610 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4611 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4612 fold_convert_loc (loc
, arg0_type
,
4614 build_int_cst (arg0_type
, 1));
4616 /* If the low bound is specified, "and" the range with the
4617 range for which the original unsigned value will be
4621 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4622 1, fold_convert_loc (loc
, arg0_type
,
4627 in_p
= (n_in_p
== in_p
);
4631 /* Otherwise, "or" the range with the range of the input
4632 that will be interpreted as negative. */
4633 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4634 1, fold_convert_loc (loc
, arg0_type
,
4639 in_p
= (in_p
!= n_in_p
);
4653 /* Given EXP, a logical expression, set the range it is testing into
4654 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4655 actually being tested. *PLOW and *PHIGH will be made of the same
4656 type as the returned expression. If EXP is not a comparison, we
4657 will most likely not be returning a useful value and range. Set
4658 *STRICT_OVERFLOW_P to true if the return value is only valid
4659 because signed overflow is undefined; otherwise, do not change
4660 *STRICT_OVERFLOW_P. */
4663 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4664 bool *strict_overflow_p
)
4666 enum tree_code code
;
4667 tree arg0
, arg1
= NULL_TREE
;
4668 tree exp_type
, nexp
;
4671 location_t loc
= EXPR_LOCATION (exp
);
4673 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4674 and see if we can refine the range. Some of the cases below may not
4675 happen, but it doesn't seem worth worrying about this. We "continue"
4676 the outer loop when we've changed something; otherwise we "break"
4677 the switch, which will "break" the while. */
4680 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4684 code
= TREE_CODE (exp
);
4685 exp_type
= TREE_TYPE (exp
);
4688 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4690 if (TREE_OPERAND_LENGTH (exp
) > 0)
4691 arg0
= TREE_OPERAND (exp
, 0);
4692 if (TREE_CODE_CLASS (code
) == tcc_binary
4693 || TREE_CODE_CLASS (code
) == tcc_comparison
4694 || (TREE_CODE_CLASS (code
) == tcc_expression
4695 && TREE_OPERAND_LENGTH (exp
) > 1))
4696 arg1
= TREE_OPERAND (exp
, 1);
4698 if (arg0
== NULL_TREE
)
4701 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4702 &high
, &in_p
, strict_overflow_p
);
4703 if (nexp
== NULL_TREE
)
4708 /* If EXP is a constant, we can evaluate whether this is true or false. */
4709 if (TREE_CODE (exp
) == INTEGER_CST
)
4711 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4713 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4719 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4723 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4724 a bitwise check i.e. when
4725 LOW == 0xXX...X00...0
4726 HIGH == 0xXX...X11...1
4727 Return corresponding mask in MASK and stem in VALUE. */
4730 maskable_range_p (const_tree low
, const_tree high
, tree type
, tree
*mask
,
4733 if (TREE_CODE (low
) != INTEGER_CST
4734 || TREE_CODE (high
) != INTEGER_CST
)
4737 unsigned prec
= TYPE_PRECISION (type
);
4738 wide_int lo
= wi::to_wide (low
, prec
);
4739 wide_int hi
= wi::to_wide (high
, prec
);
4741 wide_int end_mask
= lo
^ hi
;
4742 if ((end_mask
& (end_mask
+ 1)) != 0
4743 || (lo
& end_mask
) != 0)
4746 wide_int stem_mask
= ~end_mask
;
4747 wide_int stem
= lo
& stem_mask
;
4748 if (stem
!= (hi
& stem_mask
))
4751 *mask
= wide_int_to_tree (type
, stem_mask
);
4752 *value
= wide_int_to_tree (type
, stem
);
4757 /* Helper routine for build_range_check and match.pd. Return the type to
4758 perform the check or NULL if it shouldn't be optimized. */
4761 range_check_type (tree etype
)
4763 /* First make sure that arithmetics in this type is valid, then make sure
4764 that it wraps around. */
4765 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4766 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4767 TYPE_UNSIGNED (etype
));
4769 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4771 tree utype
, minv
, maxv
;
4773 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4774 for the type in question, as we rely on this here. */
4775 utype
= unsigned_type_for (etype
);
4776 maxv
= fold_convert (utype
, TYPE_MAX_VALUE (etype
));
4777 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4778 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4779 minv
= fold_convert (utype
, TYPE_MIN_VALUE (etype
));
4781 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4790 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4791 type, TYPE, return an expression to test if EXP is in (or out of, depending
4792 on IN_P) the range. Return 0 if the test couldn't be created. */
4795 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4796 tree low
, tree high
)
4798 tree etype
= TREE_TYPE (exp
), mask
, value
;
4800 /* Disable this optimization for function pointer expressions
4801 on targets that require function pointer canonicalization. */
4802 if (targetm
.have_canonicalize_funcptr_for_compare ()
4803 && TREE_CODE (etype
) == POINTER_TYPE
4804 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4809 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4811 return invert_truthvalue_loc (loc
, value
);
4816 if (low
== 0 && high
== 0)
4817 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4820 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4821 fold_convert_loc (loc
, etype
, high
));
4824 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4825 fold_convert_loc (loc
, etype
, low
));
4827 if (operand_equal_p (low
, high
, 0))
4828 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4829 fold_convert_loc (loc
, etype
, low
));
4831 if (TREE_CODE (exp
) == BIT_AND_EXPR
4832 && maskable_range_p (low
, high
, etype
, &mask
, &value
))
4833 return fold_build2_loc (loc
, EQ_EXPR
, type
,
4834 fold_build2_loc (loc
, BIT_AND_EXPR
, etype
,
4838 if (integer_zerop (low
))
4840 if (! TYPE_UNSIGNED (etype
))
4842 etype
= unsigned_type_for (etype
);
4843 high
= fold_convert_loc (loc
, etype
, high
);
4844 exp
= fold_convert_loc (loc
, etype
, exp
);
4846 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4849 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4850 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4852 int prec
= TYPE_PRECISION (etype
);
4854 if (wi::mask
<widest_int
> (prec
- 1, false) == wi::to_widest (high
))
4856 if (TYPE_UNSIGNED (etype
))
4858 tree signed_etype
= signed_type_for (etype
);
4859 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4861 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4863 etype
= signed_etype
;
4864 exp
= fold_convert_loc (loc
, etype
, exp
);
4866 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4867 build_int_cst (etype
, 0));
4871 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4872 This requires wrap-around arithmetics for the type of the expression. */
4873 etype
= range_check_type (etype
);
4874 if (etype
== NULL_TREE
)
4877 if (POINTER_TYPE_P (etype
))
4878 etype
= unsigned_type_for (etype
);
4880 high
= fold_convert_loc (loc
, etype
, high
);
4881 low
= fold_convert_loc (loc
, etype
, low
);
4882 exp
= fold_convert_loc (loc
, etype
, exp
);
4884 value
= const_binop (MINUS_EXPR
, high
, low
);
4886 if (value
!= 0 && !TREE_OVERFLOW (value
))
4887 return build_range_check (loc
, type
,
4888 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4889 1, build_int_cst (etype
, 0), value
);
4894 /* Return the predecessor of VAL in its type, handling the infinite case. */
4897 range_predecessor (tree val
)
4899 tree type
= TREE_TYPE (val
);
4901 if (INTEGRAL_TYPE_P (type
)
4902 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4905 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4906 build_int_cst (TREE_TYPE (val
), 1), 0);
4909 /* Return the successor of VAL in its type, handling the infinite case. */
4912 range_successor (tree val
)
4914 tree type
= TREE_TYPE (val
);
4916 if (INTEGRAL_TYPE_P (type
)
4917 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4920 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4921 build_int_cst (TREE_TYPE (val
), 1), 0);
4924 /* Given two ranges, see if we can merge them into one. Return 1 if we
4925 can, 0 if we can't. Set the output range into the specified parameters. */
4928 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4929 tree high0
, int in1_p
, tree low1
, tree high1
)
4937 int lowequal
= ((low0
== 0 && low1
== 0)
4938 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4939 low0
, 0, low1
, 0)));
4940 int highequal
= ((high0
== 0 && high1
== 0)
4941 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4942 high0
, 1, high1
, 1)));
4944 /* Make range 0 be the range that starts first, or ends last if they
4945 start at the same value. Swap them if it isn't. */
4946 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4949 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4950 high1
, 1, high0
, 1))))
4952 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4953 tem
= low0
, low0
= low1
, low1
= tem
;
4954 tem
= high0
, high0
= high1
, high1
= tem
;
4957 /* Now flag two cases, whether the ranges are disjoint or whether the
4958 second range is totally subsumed in the first. Note that the tests
4959 below are simplified by the ones above. */
4960 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4961 high0
, 1, low1
, 0));
4962 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4963 high1
, 1, high0
, 1));
4965 /* We now have four cases, depending on whether we are including or
4966 excluding the two ranges. */
4969 /* If they don't overlap, the result is false. If the second range
4970 is a subset it is the result. Otherwise, the range is from the start
4971 of the second to the end of the first. */
4973 in_p
= 0, low
= high
= 0;
4975 in_p
= 1, low
= low1
, high
= high1
;
4977 in_p
= 1, low
= low1
, high
= high0
;
4980 else if (in0_p
&& ! in1_p
)
4982 /* If they don't overlap, the result is the first range. If they are
4983 equal, the result is false. If the second range is a subset of the
4984 first, and the ranges begin at the same place, we go from just after
4985 the end of the second range to the end of the first. If the second
4986 range is not a subset of the first, or if it is a subset and both
4987 ranges end at the same place, the range starts at the start of the
4988 first range and ends just before the second range.
4989 Otherwise, we can't describe this as a single range. */
4991 in_p
= 1, low
= low0
, high
= high0
;
4992 else if (lowequal
&& highequal
)
4993 in_p
= 0, low
= high
= 0;
4994 else if (subset
&& lowequal
)
4996 low
= range_successor (high1
);
5001 /* We are in the weird situation where high0 > high1 but
5002 high1 has no successor. Punt. */
5006 else if (! subset
|| highequal
)
5009 high
= range_predecessor (low1
);
5013 /* low0 < low1 but low1 has no predecessor. Punt. */
5021 else if (! in0_p
&& in1_p
)
5023 /* If they don't overlap, the result is the second range. If the second
5024 is a subset of the first, the result is false. Otherwise,
5025 the range starts just after the first range and ends at the
5026 end of the second. */
5028 in_p
= 1, low
= low1
, high
= high1
;
5029 else if (subset
|| highequal
)
5030 in_p
= 0, low
= high
= 0;
5033 low
= range_successor (high0
);
5038 /* high1 > high0 but high0 has no successor. Punt. */
5046 /* The case where we are excluding both ranges. Here the complex case
5047 is if they don't overlap. In that case, the only time we have a
5048 range is if they are adjacent. If the second is a subset of the
5049 first, the result is the first. Otherwise, the range to exclude
5050 starts at the beginning of the first range and ends at the end of the
5054 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5055 range_successor (high0
),
5057 in_p
= 0, low
= low0
, high
= high1
;
5060 /* Canonicalize - [min, x] into - [-, x]. */
5061 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5062 switch (TREE_CODE (TREE_TYPE (low0
)))
5065 if (TYPE_PRECISION (TREE_TYPE (low0
))
5066 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
5070 if (tree_int_cst_equal (low0
,
5071 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5075 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5076 && integer_zerop (low0
))
5083 /* Canonicalize - [x, max] into - [x, -]. */
5084 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5085 switch (TREE_CODE (TREE_TYPE (high1
)))
5088 if (TYPE_PRECISION (TREE_TYPE (high1
))
5089 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
5093 if (tree_int_cst_equal (high1
,
5094 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5098 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5099 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5101 build_int_cst (TREE_TYPE (high1
), 1),
5109 /* The ranges might be also adjacent between the maximum and
5110 minimum values of the given type. For
5111 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5112 return + [x + 1, y - 1]. */
5113 if (low0
== 0 && high1
== 0)
5115 low
= range_successor (high0
);
5116 high
= range_predecessor (low1
);
5117 if (low
== 0 || high
== 0)
5127 in_p
= 0, low
= low0
, high
= high0
;
5129 in_p
= 0, low
= low0
, high
= high1
;
5132 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5137 /* Subroutine of fold, looking inside expressions of the form
5138 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5139 of the COND_EXPR. This function is being used also to optimize
5140 A op B ? C : A, by reversing the comparison first.
5142 Return a folded expression whose code is not a COND_EXPR
5143 anymore, or NULL_TREE if no folding opportunity is found. */
5146 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5147 tree arg0
, tree arg1
, tree arg2
)
5149 enum tree_code comp_code
= TREE_CODE (arg0
);
5150 tree arg00
= TREE_OPERAND (arg0
, 0);
5151 tree arg01
= TREE_OPERAND (arg0
, 1);
5152 tree arg1_type
= TREE_TYPE (arg1
);
5158 /* If we have A op 0 ? A : -A, consider applying the following
5161 A == 0? A : -A same as -A
5162 A != 0? A : -A same as A
5163 A >= 0? A : -A same as abs (A)
5164 A > 0? A : -A same as abs (A)
5165 A <= 0? A : -A same as -abs (A)
5166 A < 0? A : -A same as -abs (A)
5168 None of these transformations work for modes with signed
5169 zeros. If A is +/-0, the first two transformations will
5170 change the sign of the result (from +0 to -0, or vice
5171 versa). The last four will fix the sign of the result,
5172 even though the original expressions could be positive or
5173 negative, depending on the sign of A.
5175 Note that all these transformations are correct if A is
5176 NaN, since the two alternatives (A and -A) are also NaNs. */
5177 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5178 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5179 ? real_zerop (arg01
)
5180 : integer_zerop (arg01
))
5181 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5182 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5183 /* In the case that A is of the form X-Y, '-A' (arg2) may
5184 have already been folded to Y-X, check for that. */
5185 || (TREE_CODE (arg1
) == MINUS_EXPR
5186 && TREE_CODE (arg2
) == MINUS_EXPR
5187 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5188 TREE_OPERAND (arg2
, 1), 0)
5189 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5190 TREE_OPERAND (arg2
, 0), 0))))
5195 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5196 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5199 return fold_convert_loc (loc
, type
, arg1
);
5202 if (flag_trapping_math
)
5207 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5209 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5210 return fold_convert_loc (loc
, type
, tem
);
5213 if (flag_trapping_math
)
5218 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5220 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5221 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5223 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5227 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5228 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5229 both transformations are correct when A is NaN: A != 0
5230 is then true, and A == 0 is false. */
5232 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5233 && integer_zerop (arg01
) && integer_zerop (arg2
))
5235 if (comp_code
== NE_EXPR
)
5236 return fold_convert_loc (loc
, type
, arg1
);
5237 else if (comp_code
== EQ_EXPR
)
5238 return build_zero_cst (type
);
5241 /* Try some transformations of A op B ? A : B.
5243 A == B? A : B same as B
5244 A != B? A : B same as A
5245 A >= B? A : B same as max (A, B)
5246 A > B? A : B same as max (B, A)
5247 A <= B? A : B same as min (A, B)
5248 A < B? A : B same as min (B, A)
5250 As above, these transformations don't work in the presence
5251 of signed zeros. For example, if A and B are zeros of
5252 opposite sign, the first two transformations will change
5253 the sign of the result. In the last four, the original
5254 expressions give different results for (A=+0, B=-0) and
5255 (A=-0, B=+0), but the transformed expressions do not.
5257 The first two transformations are correct if either A or B
5258 is a NaN. In the first transformation, the condition will
5259 be false, and B will indeed be chosen. In the case of the
5260 second transformation, the condition A != B will be true,
5261 and A will be chosen.
5263 The conversions to max() and min() are not correct if B is
5264 a number and A is not. The conditions in the original
5265 expressions will be false, so all four give B. The min()
5266 and max() versions would give a NaN instead. */
5267 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5268 && operand_equal_for_comparison_p (arg01
, arg2
)
5269 /* Avoid these transformations if the COND_EXPR may be used
5270 as an lvalue in the C++ front-end. PR c++/19199. */
5272 || VECTOR_TYPE_P (type
)
5273 || (! lang_GNU_CXX ()
5274 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5275 || ! maybe_lvalue_p (arg1
)
5276 || ! maybe_lvalue_p (arg2
)))
5278 tree comp_op0
= arg00
;
5279 tree comp_op1
= arg01
;
5280 tree comp_type
= TREE_TYPE (comp_op0
);
5285 return fold_convert_loc (loc
, type
, arg2
);
5287 return fold_convert_loc (loc
, type
, arg1
);
5292 /* In C++ a ?: expression can be an lvalue, so put the
5293 operand which will be used if they are equal first
5294 so that we can convert this back to the
5295 corresponding COND_EXPR. */
5296 if (!HONOR_NANS (arg1
))
5298 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5299 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5300 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5301 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5302 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5303 comp_op1
, comp_op0
);
5304 return fold_convert_loc (loc
, type
, tem
);
5311 if (!HONOR_NANS (arg1
))
5313 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5314 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5315 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5316 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5317 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5318 comp_op1
, comp_op0
);
5319 return fold_convert_loc (loc
, type
, tem
);
5323 if (!HONOR_NANS (arg1
))
5324 return fold_convert_loc (loc
, type
, arg2
);
5327 if (!HONOR_NANS (arg1
))
5328 return fold_convert_loc (loc
, type
, arg1
);
5331 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5341 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5342 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5343 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5347 /* EXP is some logical combination of boolean tests. See if we can
5348 merge it into some range test. Return the new tree if so. */
5351 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5354 int or_op
= (code
== TRUTH_ORIF_EXPR
5355 || code
== TRUTH_OR_EXPR
);
5356 int in0_p
, in1_p
, in_p
;
5357 tree low0
, low1
, low
, high0
, high1
, high
;
5358 bool strict_overflow_p
= false;
5360 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5361 "when simplifying range test");
5363 if (!INTEGRAL_TYPE_P (type
))
5366 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5367 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5369 /* If this is an OR operation, invert both sides; we will invert
5370 again at the end. */
5372 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5374 /* If both expressions are the same, if we can merge the ranges, and we
5375 can build the range test, return it or it inverted. If one of the
5376 ranges is always true or always false, consider it to be the same
5377 expression as the other. */
5378 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5379 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5381 && 0 != (tem
= (build_range_check (loc
, type
,
5383 : rhs
!= 0 ? rhs
: integer_zero_node
,
5386 if (strict_overflow_p
)
5387 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5388 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5391 /* On machines where the branch cost is expensive, if this is a
5392 short-circuited branch and the underlying object on both sides
5393 is the same, make a non-short-circuit operation. */
5394 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5395 && !flag_sanitize_coverage
5396 && lhs
!= 0 && rhs
!= 0
5397 && (code
== TRUTH_ANDIF_EXPR
5398 || code
== TRUTH_ORIF_EXPR
)
5399 && operand_equal_p (lhs
, rhs
, 0))
5401 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5402 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5403 which cases we can't do this. */
5404 if (simple_operand_p (lhs
))
5405 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5406 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5409 else if (!lang_hooks
.decls
.global_bindings_p ()
5410 && !CONTAINS_PLACEHOLDER_P (lhs
))
5412 tree common
= save_expr (lhs
);
5414 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5415 or_op
? ! in0_p
: in0_p
,
5417 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5418 or_op
? ! in1_p
: in1_p
,
5421 if (strict_overflow_p
)
5422 fold_overflow_warning (warnmsg
,
5423 WARN_STRICT_OVERFLOW_COMPARISON
);
5424 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5425 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5434 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5435 bit value. Arrange things so the extra bits will be set to zero if and
5436 only if C is signed-extended to its full width. If MASK is nonzero,
5437 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5440 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5442 tree type
= TREE_TYPE (c
);
5443 int modesize
= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type
));
5446 if (p
== modesize
|| unsignedp
)
5449 /* We work by getting just the sign bit into the low-order bit, then
5450 into the high-order bit, then sign-extend. We then XOR that value
5452 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5454 /* We must use a signed type in order to get an arithmetic right shift.
5455 However, we must also avoid introducing accidental overflows, so that
5456 a subsequent call to integer_zerop will work. Hence we must
5457 do the type conversion here. At this point, the constant is either
5458 zero or one, and the conversion to a signed type can never overflow.
5459 We could get an overflow if this conversion is done anywhere else. */
5460 if (TYPE_UNSIGNED (type
))
5461 temp
= fold_convert (signed_type_for (type
), temp
);
5463 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5464 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5466 temp
= const_binop (BIT_AND_EXPR
, temp
,
5467 fold_convert (TREE_TYPE (c
), mask
));
5468 /* If necessary, convert the type back to match the type of C. */
5469 if (TYPE_UNSIGNED (type
))
5470 temp
= fold_convert (type
, temp
);
5472 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5475 /* For an expression that has the form
5479 we can drop one of the inner expressions and simplify to
5483 LOC is the location of the resulting expression. OP is the inner
5484 logical operation; the left-hand side in the examples above, while CMPOP
5485 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5486 removing a condition that guards another, as in
5487 (A != NULL && A->...) || A == NULL
5488 which we must not transform. If RHS_ONLY is true, only eliminate the
5489 right-most operand of the inner logical operation. */
5492 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5495 tree type
= TREE_TYPE (cmpop
);
5496 enum tree_code code
= TREE_CODE (cmpop
);
5497 enum tree_code truthop_code
= TREE_CODE (op
);
5498 tree lhs
= TREE_OPERAND (op
, 0);
5499 tree rhs
= TREE_OPERAND (op
, 1);
5500 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5501 enum tree_code rhs_code
= TREE_CODE (rhs
);
5502 enum tree_code lhs_code
= TREE_CODE (lhs
);
5503 enum tree_code inv_code
;
5505 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5508 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5511 if (rhs_code
== truthop_code
)
5513 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5514 if (newrhs
!= NULL_TREE
)
5517 rhs_code
= TREE_CODE (rhs
);
5520 if (lhs_code
== truthop_code
&& !rhs_only
)
5522 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5523 if (newlhs
!= NULL_TREE
)
5526 lhs_code
= TREE_CODE (lhs
);
5530 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5531 if (inv_code
== rhs_code
5532 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5533 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5535 if (!rhs_only
&& inv_code
== lhs_code
5536 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5537 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5539 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5540 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5545 /* Find ways of folding logical expressions of LHS and RHS:
5546 Try to merge two comparisons to the same innermost item.
5547 Look for range tests like "ch >= '0' && ch <= '9'".
5548 Look for combinations of simple terms on machines with expensive branches
5549 and evaluate the RHS unconditionally.
5551 For example, if we have p->a == 2 && p->b == 4 and we can make an
5552 object large enough to span both A and B, we can do this with a comparison
5553 against the object ANDed with the a mask.
5555 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5556 operations to do this with one comparison.
5558 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5559 function and the one above.
5561 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5562 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5564 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5567 We return the simplified tree or 0 if no optimization is possible. */
5570 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5573 /* If this is the "or" of two comparisons, we can do something if
5574 the comparisons are NE_EXPR. If this is the "and", we can do something
5575 if the comparisons are EQ_EXPR. I.e.,
5576 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5578 WANTED_CODE is this operation code. For single bit fields, we can
5579 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5580 comparison for one-bit fields. */
5582 enum tree_code wanted_code
;
5583 enum tree_code lcode
, rcode
;
5584 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5585 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5586 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5587 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5588 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5589 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5590 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5591 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5592 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5593 scalar_int_mode lnmode
, rnmode
;
5594 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5595 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5596 tree l_const
, r_const
;
5597 tree lntype
, rntype
, result
;
5598 HOST_WIDE_INT first_bit
, end_bit
;
5601 /* Start by getting the comparison codes. Fail if anything is volatile.
5602 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5603 it were surrounded with a NE_EXPR. */
5605 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5608 lcode
= TREE_CODE (lhs
);
5609 rcode
= TREE_CODE (rhs
);
5611 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5613 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5614 build_int_cst (TREE_TYPE (lhs
), 0));
5618 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5620 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5621 build_int_cst (TREE_TYPE (rhs
), 0));
5625 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5626 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5629 ll_arg
= TREE_OPERAND (lhs
, 0);
5630 lr_arg
= TREE_OPERAND (lhs
, 1);
5631 rl_arg
= TREE_OPERAND (rhs
, 0);
5632 rr_arg
= TREE_OPERAND (rhs
, 1);
5634 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5635 if (simple_operand_p (ll_arg
)
5636 && simple_operand_p (lr_arg
))
5638 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5639 && operand_equal_p (lr_arg
, rr_arg
, 0))
5641 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5642 truth_type
, ll_arg
, lr_arg
);
5646 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5647 && operand_equal_p (lr_arg
, rl_arg
, 0))
5649 result
= combine_comparisons (loc
, code
, lcode
,
5650 swap_tree_comparison (rcode
),
5651 truth_type
, ll_arg
, lr_arg
);
5657 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5658 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5660 /* If the RHS can be evaluated unconditionally and its operands are
5661 simple, it wins to evaluate the RHS unconditionally on machines
5662 with expensive branches. In this case, this isn't a comparison
5663 that can be merged. */
5665 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5667 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5668 && simple_operand_p (rl_arg
)
5669 && simple_operand_p (rr_arg
))
5671 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5672 if (code
== TRUTH_OR_EXPR
5673 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5674 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5675 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5676 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5677 return build2_loc (loc
, NE_EXPR
, truth_type
,
5678 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5680 build_int_cst (TREE_TYPE (ll_arg
), 0));
5682 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5683 if (code
== TRUTH_AND_EXPR
5684 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5685 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5686 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5687 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5688 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5689 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5691 build_int_cst (TREE_TYPE (ll_arg
), 0));
5694 /* See if the comparisons can be merged. Then get all the parameters for
5697 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5698 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5701 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5703 ll_inner
= decode_field_reference (loc
, &ll_arg
,
5704 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5705 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5706 &ll_mask
, &ll_and_mask
);
5707 lr_inner
= decode_field_reference (loc
, &lr_arg
,
5708 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5709 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5710 &lr_mask
, &lr_and_mask
);
5711 rl_inner
= decode_field_reference (loc
, &rl_arg
,
5712 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5713 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5714 &rl_mask
, &rl_and_mask
);
5715 rr_inner
= decode_field_reference (loc
, &rr_arg
,
5716 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5717 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5718 &rr_mask
, &rr_and_mask
);
5720 /* It must be true that the inner operation on the lhs of each
5721 comparison must be the same if we are to be able to do anything.
5722 Then see if we have constants. If not, the same must be true for
5725 || ll_reversep
!= rl_reversep
5726 || ll_inner
== 0 || rl_inner
== 0
5727 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5730 if (TREE_CODE (lr_arg
) == INTEGER_CST
5731 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5733 l_const
= lr_arg
, r_const
= rr_arg
;
5734 lr_reversep
= ll_reversep
;
5736 else if (lr_reversep
!= rr_reversep
5737 || lr_inner
== 0 || rr_inner
== 0
5738 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5741 l_const
= r_const
= 0;
5743 /* If either comparison code is not correct for our logical operation,
5744 fail. However, we can convert a one-bit comparison against zero into
5745 the opposite comparison against that bit being set in the field. */
5747 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5748 if (lcode
!= wanted_code
)
5750 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5752 /* Make the left operand unsigned, since we are only interested
5753 in the value of one bit. Otherwise we are doing the wrong
5762 /* This is analogous to the code for l_const above. */
5763 if (rcode
!= wanted_code
)
5765 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5774 /* See if we can find a mode that contains both fields being compared on
5775 the left. If we can't, fail. Otherwise, update all constants and masks
5776 to be relative to a field of that size. */
5777 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5778 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5779 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5780 TYPE_ALIGN (TREE_TYPE (ll_inner
)), BITS_PER_WORD
,
5781 volatilep
, &lnmode
))
5784 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5785 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5786 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5787 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5789 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5791 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5792 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5795 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5796 size_int (xll_bitpos
));
5797 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5798 size_int (xrl_bitpos
));
5802 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5803 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5804 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5805 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5806 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5809 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5811 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5816 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5817 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5818 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5819 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5820 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5823 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5825 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5829 /* If the right sides are not constant, do the same for it. Also,
5830 disallow this optimization if a size or signedness mismatch occurs
5831 between the left and right sides. */
5834 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5835 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5836 /* Make sure the two fields on the right
5837 correspond to the left without being swapped. */
5838 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5841 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5842 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5843 if (!get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5844 TYPE_ALIGN (TREE_TYPE (lr_inner
)), BITS_PER_WORD
,
5845 volatilep
, &rnmode
))
5848 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5849 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5850 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5851 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5853 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5855 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5856 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5859 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5861 size_int (xlr_bitpos
));
5862 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5864 size_int (xrr_bitpos
));
5866 /* Make a mask that corresponds to both fields being compared.
5867 Do this for both items being compared. If the operands are the
5868 same size and the bits being compared are in the same position
5869 then we can do this by masking both and comparing the masked
5871 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5872 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5873 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5875 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5876 lntype
, lnbitsize
, lnbitpos
,
5877 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5878 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5879 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5881 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
5882 rntype
, rnbitsize
, rnbitpos
,
5883 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
5884 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5885 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5887 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5890 /* There is still another way we can do something: If both pairs of
5891 fields being compared are adjacent, we may be able to make a wider
5892 field containing them both.
5894 Note that we still must mask the lhs/rhs expressions. Furthermore,
5895 the mask must be shifted to account for the shift done by
5896 make_bit_field_ref. */
5897 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5898 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5899 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5900 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5904 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
5905 ll_bitsize
+ rl_bitsize
,
5906 MIN (ll_bitpos
, rl_bitpos
),
5907 ll_unsignedp
, ll_reversep
);
5908 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
5909 lr_bitsize
+ rr_bitsize
,
5910 MIN (lr_bitpos
, rr_bitpos
),
5911 lr_unsignedp
, lr_reversep
);
5913 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5914 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5915 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5916 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5918 /* Convert to the smaller type before masking out unwanted bits. */
5920 if (lntype
!= rntype
)
5922 if (lnbitsize
> rnbitsize
)
5924 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5925 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5928 else if (lnbitsize
< rnbitsize
)
5930 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5931 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5936 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5937 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5939 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5940 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5942 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5948 /* Handle the case of comparisons with constants. If there is something in
5949 common between the masks, those bits of the constants must be the same.
5950 If not, the condition is always false. Test for this to avoid generating
5951 incorrect code below. */
5952 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5953 if (! integer_zerop (result
)
5954 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5955 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5957 if (wanted_code
== NE_EXPR
)
5959 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5960 return constant_boolean_node (true, truth_type
);
5964 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5965 return constant_boolean_node (false, truth_type
);
5969 /* Construct the expression we will return. First get the component
5970 reference we will make. Unless the mask is all ones the width of
5971 that field, perform the mask operation. Then compare with the
5973 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5974 lntype
, lnbitsize
, lnbitpos
,
5975 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5977 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5978 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5979 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5981 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5982 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5985 /* T is an integer expression that is being multiplied, divided, or taken a
5986 modulus (CODE says which and what kind of divide or modulus) by a
5987 constant C. See if we can eliminate that operation by folding it with
5988 other operations already in T. WIDE_TYPE, if non-null, is a type that
5989 should be used for the computation if wider than our type.
5991 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5992 (X * 2) + (Y * 4). We must, however, be assured that either the original
5993 expression would not overflow or that overflow is undefined for the type
5994 in the language in question.
5996 If we return a non-null expression, it is an equivalent form of the
5997 original computation, but need not be in the original type.
5999 We set *STRICT_OVERFLOW_P to true if the return values depends on
6000 signed overflow being undefined. Otherwise we do not change
6001 *STRICT_OVERFLOW_P. */
6004 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6005 bool *strict_overflow_p
)
6007 /* To avoid exponential search depth, refuse to allow recursion past
6008 three levels. Beyond that (1) it's highly unlikely that we'll find
6009 something interesting and (2) we've probably processed it before
6010 when we built the inner expression. */
6019 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6026 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6027 bool *strict_overflow_p
)
6029 tree type
= TREE_TYPE (t
);
6030 enum tree_code tcode
= TREE_CODE (t
);
6031 tree ctype
= (wide_type
!= 0
6032 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type
))
6033 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
)))
6034 ? wide_type
: type
);
6036 int same_p
= tcode
== code
;
6037 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6038 bool sub_strict_overflow_p
;
6040 /* Don't deal with constants of zero here; they confuse the code below. */
6041 if (integer_zerop (c
))
6044 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6045 op0
= TREE_OPERAND (t
, 0);
6047 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6048 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6050 /* Note that we need not handle conditional operations here since fold
6051 already handles those cases. So just do arithmetic here. */
6055 /* For a constant, we can always simplify if we are a multiply
6056 or (for divide and modulus) if it is a multiple of our constant. */
6057 if (code
== MULT_EXPR
6058 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6060 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6061 fold_convert (ctype
, c
));
6062 /* If the multiplication overflowed, we lost information on it.
6063 See PR68142 and PR69845. */
6064 if (TREE_OVERFLOW (tem
))
6070 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6071 /* If op0 is an expression ... */
6072 if ((COMPARISON_CLASS_P (op0
)
6073 || UNARY_CLASS_P (op0
)
6074 || BINARY_CLASS_P (op0
)
6075 || VL_EXP_CLASS_P (op0
)
6076 || EXPRESSION_CLASS_P (op0
))
6077 /* ... and has wrapping overflow, and its type is smaller
6078 than ctype, then we cannot pass through as widening. */
6079 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6080 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6081 && (TYPE_PRECISION (ctype
)
6082 > TYPE_PRECISION (TREE_TYPE (op0
))))
6083 /* ... or this is a truncation (t is narrower than op0),
6084 then we cannot pass through this narrowing. */
6085 || (TYPE_PRECISION (type
)
6086 < TYPE_PRECISION (TREE_TYPE (op0
)))
6087 /* ... or signedness changes for division or modulus,
6088 then we cannot pass through this conversion. */
6089 || (code
!= MULT_EXPR
6090 && (TYPE_UNSIGNED (ctype
)
6091 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6092 /* ... or has undefined overflow while the converted to
6093 type has not, we cannot do the operation in the inner type
6094 as that would introduce undefined overflow. */
6095 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6096 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6097 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6100 /* Pass the constant down and see if we can make a simplification. If
6101 we can, replace this expression with the inner simplification for
6102 possible later conversion to our or some other type. */
6103 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6104 && TREE_CODE (t2
) == INTEGER_CST
6105 && !TREE_OVERFLOW (t2
)
6106 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6108 ? ctype
: NULL_TREE
,
6109 strict_overflow_p
))))
6114 /* If widening the type changes it from signed to unsigned, then we
6115 must avoid building ABS_EXPR itself as unsigned. */
6116 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6118 tree cstype
= (*signed_type_for
) (ctype
);
6119 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6122 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6123 return fold_convert (ctype
, t1
);
6127 /* If the constant is negative, we cannot simplify this. */
6128 if (tree_int_cst_sgn (c
) == -1)
6132 /* For division and modulus, type can't be unsigned, as e.g.
6133 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6134 For signed types, even with wrapping overflow, this is fine. */
6135 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6137 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6139 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6142 case MIN_EXPR
: case MAX_EXPR
:
6143 /* If widening the type changes the signedness, then we can't perform
6144 this optimization as that changes the result. */
6145 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6148 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6149 sub_strict_overflow_p
= false;
6150 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6151 &sub_strict_overflow_p
)) != 0
6152 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6153 &sub_strict_overflow_p
)) != 0)
6155 if (tree_int_cst_sgn (c
) < 0)
6156 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6157 if (sub_strict_overflow_p
)
6158 *strict_overflow_p
= true;
6159 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6160 fold_convert (ctype
, t2
));
6164 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6165 /* If the second operand is constant, this is a multiplication
6166 or floor division, by a power of two, so we can treat it that
6167 way unless the multiplier or divisor overflows. Signed
6168 left-shift overflow is implementation-defined rather than
6169 undefined in C90, so do not convert signed left shift into
6171 if (TREE_CODE (op1
) == INTEGER_CST
6172 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6173 /* const_binop may not detect overflow correctly,
6174 so check for it explicitly here. */
6175 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6176 && 0 != (t1
= fold_convert (ctype
,
6177 const_binop (LSHIFT_EXPR
,
6180 && !TREE_OVERFLOW (t1
))
6181 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6182 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6184 fold_convert (ctype
, op0
),
6186 c
, code
, wide_type
, strict_overflow_p
);
6189 case PLUS_EXPR
: case MINUS_EXPR
:
6190 /* See if we can eliminate the operation on both sides. If we can, we
6191 can return a new PLUS or MINUS. If we can't, the only remaining
6192 cases where we can do anything are if the second operand is a
6194 sub_strict_overflow_p
= false;
6195 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6196 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6197 if (t1
!= 0 && t2
!= 0
6198 && TYPE_OVERFLOW_WRAPS (ctype
)
6199 && (code
== MULT_EXPR
6200 /* If not multiplication, we can only do this if both operands
6201 are divisible by c. */
6202 || (multiple_of_p (ctype
, op0
, c
)
6203 && multiple_of_p (ctype
, op1
, c
))))
6205 if (sub_strict_overflow_p
)
6206 *strict_overflow_p
= true;
6207 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6208 fold_convert (ctype
, t2
));
6211 /* If this was a subtraction, negate OP1 and set it to be an addition.
6212 This simplifies the logic below. */
6213 if (tcode
== MINUS_EXPR
)
6215 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6216 /* If OP1 was not easily negatable, the constant may be OP0. */
6217 if (TREE_CODE (op0
) == INTEGER_CST
)
6219 std::swap (op0
, op1
);
6224 if (TREE_CODE (op1
) != INTEGER_CST
)
6227 /* If either OP1 or C are negative, this optimization is not safe for
6228 some of the division and remainder types while for others we need
6229 to change the code. */
6230 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6232 if (code
== CEIL_DIV_EXPR
)
6233 code
= FLOOR_DIV_EXPR
;
6234 else if (code
== FLOOR_DIV_EXPR
)
6235 code
= CEIL_DIV_EXPR
;
6236 else if (code
!= MULT_EXPR
6237 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6241 /* If it's a multiply or a division/modulus operation of a multiple
6242 of our constant, do the operation and verify it doesn't overflow. */
6243 if (code
== MULT_EXPR
6244 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6246 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6247 fold_convert (ctype
, c
));
6248 /* We allow the constant to overflow with wrapping semantics. */
6250 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6256 /* If we have an unsigned type, we cannot widen the operation since it
6257 will change the result if the original computation overflowed. */
6258 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6261 /* The last case is if we are a multiply. In that case, we can
6262 apply the distributive law to commute the multiply and addition
6263 if the multiplication of the constants doesn't overflow
6264 and overflow is defined. With undefined overflow
6265 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6266 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6267 return fold_build2 (tcode
, ctype
,
6268 fold_build2 (code
, ctype
,
6269 fold_convert (ctype
, op0
),
6270 fold_convert (ctype
, c
)),
6276 /* We have a special case here if we are doing something like
6277 (C * 8) % 4 since we know that's zero. */
6278 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6279 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6280 /* If the multiplication can overflow we cannot optimize this. */
6281 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6282 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6283 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6285 *strict_overflow_p
= true;
6286 return omit_one_operand (type
, integer_zero_node
, op0
);
6289 /* ... fall through ... */
6291 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6292 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6293 /* If we can extract our operation from the LHS, do so and return a
6294 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6295 do something only if the second operand is a constant. */
6297 && TYPE_OVERFLOW_WRAPS (ctype
)
6298 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6299 strict_overflow_p
)) != 0)
6300 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6301 fold_convert (ctype
, op1
));
6302 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6303 && TYPE_OVERFLOW_WRAPS (ctype
)
6304 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6305 strict_overflow_p
)) != 0)
6306 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6307 fold_convert (ctype
, t1
));
6308 else if (TREE_CODE (op1
) != INTEGER_CST
)
6311 /* If these are the same operation types, we can associate them
6312 assuming no overflow. */
6315 bool overflow_p
= false;
6316 bool overflow_mul_p
;
6317 signop sign
= TYPE_SIGN (ctype
);
6318 unsigned prec
= TYPE_PRECISION (ctype
);
6319 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6320 wi::to_wide (c
, prec
),
6321 sign
, &overflow_mul_p
);
6322 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6324 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6327 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6328 wide_int_to_tree (ctype
, mul
));
6331 /* If these operations "cancel" each other, we have the main
6332 optimizations of this pass, which occur when either constant is a
6333 multiple of the other, in which case we replace this with either an
6334 operation or CODE or TCODE.
6336 If we have an unsigned type, we cannot do this since it will change
6337 the result if the original computation overflowed. */
6338 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6339 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6340 || (tcode
== MULT_EXPR
6341 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6342 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6343 && code
!= MULT_EXPR
)))
6345 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6347 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6348 *strict_overflow_p
= true;
6349 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6350 fold_convert (ctype
,
6351 const_binop (TRUNC_DIV_EXPR
,
6354 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6356 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6357 *strict_overflow_p
= true;
6358 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6359 fold_convert (ctype
,
6360 const_binop (TRUNC_DIV_EXPR
,
6373 /* Return a node which has the indicated constant VALUE (either 0 or
6374 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6375 and is of the indicated TYPE. */
6378 constant_boolean_node (bool value
, tree type
)
6380 if (type
== integer_type_node
)
6381 return value
? integer_one_node
: integer_zero_node
;
6382 else if (type
== boolean_type_node
)
6383 return value
? boolean_true_node
: boolean_false_node
;
6384 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6385 return build_vector_from_val (type
,
6386 build_int_cst (TREE_TYPE (type
),
6389 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6393 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6394 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6395 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6396 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6397 COND is the first argument to CODE; otherwise (as in the example
6398 given here), it is the second argument. TYPE is the type of the
6399 original expression. Return NULL_TREE if no simplification is
6403 fold_binary_op_with_conditional_arg (location_t loc
,
6404 enum tree_code code
,
6405 tree type
, tree op0
, tree op1
,
6406 tree cond
, tree arg
, int cond_first_p
)
6408 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6409 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6410 tree test
, true_value
, false_value
;
6411 tree lhs
= NULL_TREE
;
6412 tree rhs
= NULL_TREE
;
6413 enum tree_code cond_code
= COND_EXPR
;
6415 if (TREE_CODE (cond
) == COND_EXPR
6416 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6418 test
= TREE_OPERAND (cond
, 0);
6419 true_value
= TREE_OPERAND (cond
, 1);
6420 false_value
= TREE_OPERAND (cond
, 2);
6421 /* If this operand throws an expression, then it does not make
6422 sense to try to perform a logical or arithmetic operation
6424 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6426 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6429 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6430 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6432 tree testtype
= TREE_TYPE (cond
);
6434 true_value
= constant_boolean_node (true, testtype
);
6435 false_value
= constant_boolean_node (false, testtype
);
6438 /* Detect the case of mixing vector and scalar types - bail out. */
6441 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6442 cond_code
= VEC_COND_EXPR
;
6444 /* This transformation is only worthwhile if we don't have to wrap ARG
6445 in a SAVE_EXPR and the operation can be simplified without recursing
6446 on at least one of the branches once its pushed inside the COND_EXPR. */
6447 if (!TREE_CONSTANT (arg
)
6448 && (TREE_SIDE_EFFECTS (arg
)
6449 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6450 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6453 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6456 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6458 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6460 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6464 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6466 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6468 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6471 /* Check that we have simplified at least one of the branches. */
6472 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6475 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6479 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6481 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6482 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6483 ADDEND is the same as X.
6485 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6486 and finite. The problematic cases are when X is zero, and its mode
6487 has signed zeros. In the case of rounding towards -infinity,
6488 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6489 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6492 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6494 if (!real_zerop (addend
))
6497 /* Don't allow the fold with -fsignaling-nans. */
6498 if (HONOR_SNANS (element_mode (type
)))
6501 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6502 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6505 /* In a vector or complex, we would need to check the sign of all zeros. */
6506 if (TREE_CODE (addend
) != REAL_CST
)
6509 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6510 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6513 /* The mode has signed zeros, and we have to honor their sign.
6514 In this situation, there is only one case we can return true for.
6515 X - 0 is the same as X unless rounding towards -infinity is
6517 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6520 /* Subroutine of match.pd that optimizes comparisons of a division by
6521 a nonzero integer constant against an integer constant, i.e.
6524 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6525 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6528 fold_div_compare (enum tree_code code
, tree c1
, tree c2
, tree
*lo
,
6529 tree
*hi
, bool *neg_overflow
)
6531 tree prod
, tmp
, type
= TREE_TYPE (c1
);
6532 signop sign
= TYPE_SIGN (type
);
6535 /* We have to do this the hard way to detect unsigned overflow.
6536 prod = int_const_binop (MULT_EXPR, c1, c2); */
6537 wide_int val
= wi::mul (c1
, c2
, sign
, &overflow
);
6538 prod
= force_fit_type (type
, val
, -1, overflow
);
6539 *neg_overflow
= false;
6541 if (sign
== UNSIGNED
)
6543 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
6546 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6547 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6548 *hi
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (prod
));
6550 else if (tree_int_cst_sgn (c1
) >= 0)
6552 tmp
= int_const_binop (MINUS_EXPR
, c1
, build_int_cst (type
, 1));
6553 switch (tree_int_cst_sgn (c2
))
6556 *neg_overflow
= true;
6557 *lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6562 *lo
= fold_negate_const (tmp
, type
);
6567 *hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6577 /* A negative divisor reverses the relational operators. */
6578 code
= swap_tree_comparison (code
);
6580 tmp
= int_const_binop (PLUS_EXPR
, c1
, build_int_cst (type
, 1));
6581 switch (tree_int_cst_sgn (c2
))
6584 *hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6589 *hi
= fold_negate_const (tmp
, type
);
6594 *neg_overflow
= true;
6595 *lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6604 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
6607 if (TREE_OVERFLOW (*lo
)
6608 || operand_equal_p (*lo
, TYPE_MIN_VALUE (type
), 0))
6610 if (TREE_OVERFLOW (*hi
)
6611 || operand_equal_p (*hi
, TYPE_MAX_VALUE (type
), 0))
6618 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6619 equality/inequality test, then return a simplified form of the test
6620 using a sign testing. Otherwise return NULL. TYPE is the desired
6624 fold_single_bit_test_into_sign_test (location_t loc
,
6625 enum tree_code code
, tree arg0
, tree arg1
,
6628 /* If this is testing a single bit, we can optimize the test. */
6629 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6630 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6631 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6633 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6634 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6635 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6637 if (arg00
!= NULL_TREE
6638 /* This is only a win if casting to a signed type is cheap,
6639 i.e. when arg00's type is not a partial mode. */
6640 && type_has_mode_precision_p (TREE_TYPE (arg00
)))
6642 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6643 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6645 fold_convert_loc (loc
, stype
, arg00
),
6646 build_int_cst (stype
, 0));
6653 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6654 equality/inequality test, then return a simplified form of
6655 the test using shifts and logical operations. Otherwise return
6656 NULL. TYPE is the desired result type. */
6659 fold_single_bit_test (location_t loc
, enum tree_code code
,
6660 tree arg0
, tree arg1
, tree result_type
)
6662 /* If this is testing a single bit, we can optimize the test. */
6663 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6664 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6665 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6667 tree inner
= TREE_OPERAND (arg0
, 0);
6668 tree type
= TREE_TYPE (arg0
);
6669 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6670 scalar_int_mode operand_mode
= SCALAR_INT_TYPE_MODE (type
);
6672 tree signed_type
, unsigned_type
, intermediate_type
;
6675 /* First, see if we can fold the single bit test into a sign-bit
6677 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6682 /* Otherwise we have (A & C) != 0 where C is a single bit,
6683 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6684 Similarly for (A & C) == 0. */
6686 /* If INNER is a right shift of a constant and it plus BITNUM does
6687 not overflow, adjust BITNUM and INNER. */
6688 if (TREE_CODE (inner
) == RSHIFT_EXPR
6689 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6690 && bitnum
< TYPE_PRECISION (type
)
6691 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6692 TYPE_PRECISION (type
) - bitnum
))
6694 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6695 inner
= TREE_OPERAND (inner
, 0);
6698 /* If we are going to be able to omit the AND below, we must do our
6699 operations as unsigned. If we must use the AND, we have a choice.
6700 Normally unsigned is faster, but for some machines signed is. */
6701 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
6702 && !flag_syntax_only
) ? 0 : 1;
6704 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6705 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6706 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6707 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6710 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6711 inner
, size_int (bitnum
));
6713 one
= build_int_cst (intermediate_type
, 1);
6715 if (code
== EQ_EXPR
)
6716 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6718 /* Put the AND last so it can combine with more things. */
6719 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6721 /* Make sure to return the proper type. */
6722 inner
= fold_convert_loc (loc
, result_type
, inner
);
6729 /* Test whether it is preferable two swap two operands, ARG0 and
6730 ARG1, for example because ARG0 is an integer constant and ARG1
6734 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
6736 if (CONSTANT_CLASS_P (arg1
))
6738 if (CONSTANT_CLASS_P (arg0
))
6744 if (TREE_CONSTANT (arg1
))
6746 if (TREE_CONSTANT (arg0
))
6749 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6750 for commutative and comparison operators. Ensuring a canonical
6751 form allows the optimizers to find additional redundancies without
6752 having to explicitly check for both orderings. */
6753 if (TREE_CODE (arg0
) == SSA_NAME
6754 && TREE_CODE (arg1
) == SSA_NAME
6755 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6758 /* Put SSA_NAMEs last. */
6759 if (TREE_CODE (arg1
) == SSA_NAME
)
6761 if (TREE_CODE (arg0
) == SSA_NAME
)
6764 /* Put variables last. */
6774 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6775 means A >= Y && A != MAX, but in this case we know that
6776 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6779 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6781 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6783 if (TREE_CODE (bound
) == LT_EXPR
)
6784 a
= TREE_OPERAND (bound
, 0);
6785 else if (TREE_CODE (bound
) == GT_EXPR
)
6786 a
= TREE_OPERAND (bound
, 1);
6790 typea
= TREE_TYPE (a
);
6791 if (!INTEGRAL_TYPE_P (typea
)
6792 && !POINTER_TYPE_P (typea
))
6795 if (TREE_CODE (ineq
) == LT_EXPR
)
6797 a1
= TREE_OPERAND (ineq
, 1);
6798 y
= TREE_OPERAND (ineq
, 0);
6800 else if (TREE_CODE (ineq
) == GT_EXPR
)
6802 a1
= TREE_OPERAND (ineq
, 0);
6803 y
= TREE_OPERAND (ineq
, 1);
6808 if (TREE_TYPE (a1
) != typea
)
6811 if (POINTER_TYPE_P (typea
))
6813 /* Convert the pointer types into integer before taking the difference. */
6814 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6815 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6816 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6819 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6821 if (!diff
|| !integer_onep (diff
))
6824 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6827 /* Fold a sum or difference of at least one multiplication.
6828 Returns the folded tree or NULL if no simplification could be made. */
6831 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6832 tree arg0
, tree arg1
)
6834 tree arg00
, arg01
, arg10
, arg11
;
6835 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6837 /* (A * C) +- (B * C) -> (A+-B) * C.
6838 (A * C) +- A -> A * (C+-1).
6839 We are most concerned about the case where C is a constant,
6840 but other combinations show up during loop reduction. Since
6841 it is not difficult, try all four possibilities. */
6843 if (TREE_CODE (arg0
) == MULT_EXPR
)
6845 arg00
= TREE_OPERAND (arg0
, 0);
6846 arg01
= TREE_OPERAND (arg0
, 1);
6848 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6850 arg00
= build_one_cst (type
);
6855 /* We cannot generate constant 1 for fract. */
6856 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6859 arg01
= build_one_cst (type
);
6861 if (TREE_CODE (arg1
) == MULT_EXPR
)
6863 arg10
= TREE_OPERAND (arg1
, 0);
6864 arg11
= TREE_OPERAND (arg1
, 1);
6866 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6868 arg10
= build_one_cst (type
);
6869 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6870 the purpose of this canonicalization. */
6871 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
6872 && negate_expr_p (arg1
)
6873 && code
== PLUS_EXPR
)
6875 arg11
= negate_expr (arg1
);
6883 /* We cannot generate constant 1 for fract. */
6884 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6887 arg11
= build_one_cst (type
);
6891 /* Prefer factoring a common non-constant. */
6892 if (operand_equal_p (arg00
, arg10
, 0))
6893 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
6894 else if (operand_equal_p (arg01
, arg11
, 0))
6895 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
6896 else if (operand_equal_p (arg00
, arg11
, 0))
6897 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
6898 else if (operand_equal_p (arg01
, arg10
, 0))
6899 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
6901 /* No identical multiplicands; see if we can find a common
6902 power-of-two factor in non-power-of-two multiplies. This
6903 can help in multi-dimensional array access. */
6904 else if (tree_fits_shwi_p (arg01
)
6905 && tree_fits_shwi_p (arg11
))
6907 HOST_WIDE_INT int01
, int11
, tmp
;
6910 int01
= tree_to_shwi (arg01
);
6911 int11
= tree_to_shwi (arg11
);
6913 /* Move min of absolute values to int11. */
6914 if (absu_hwi (int01
) < absu_hwi (int11
))
6916 tmp
= int01
, int01
= int11
, int11
= tmp
;
6917 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
6924 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
6925 /* The remainder should not be a constant, otherwise we
6926 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6927 increased the number of multiplications necessary. */
6928 && TREE_CODE (arg10
) != INTEGER_CST
)
6930 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
6931 build_int_cst (TREE_TYPE (arg00
),
6936 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
6943 if (! INTEGRAL_TYPE_P (type
)
6944 || TYPE_OVERFLOW_WRAPS (type
)
6945 /* We are neither factoring zero nor minus one. */
6946 || TREE_CODE (same
) == INTEGER_CST
)
6947 return fold_build2_loc (loc
, MULT_EXPR
, type
,
6948 fold_build2_loc (loc
, code
, type
,
6949 fold_convert_loc (loc
, type
, alt0
),
6950 fold_convert_loc (loc
, type
, alt1
)),
6951 fold_convert_loc (loc
, type
, same
));
6953 /* Same may be zero and thus the operation 'code' may overflow. Likewise
6954 same may be minus one and thus the multiplication may overflow. Perform
6955 the operations in an unsigned type. */
6956 tree utype
= unsigned_type_for (type
);
6957 tree tem
= fold_build2_loc (loc
, code
, utype
,
6958 fold_convert_loc (loc
, utype
, alt0
),
6959 fold_convert_loc (loc
, utype
, alt1
));
6960 /* If the sum evaluated to a constant that is not -INF the multiplication
6962 if (TREE_CODE (tem
) == INTEGER_CST
6963 && ! wi::eq_p (tem
, wi::min_value (TYPE_PRECISION (utype
), SIGNED
)))
6964 return fold_build2_loc (loc
, MULT_EXPR
, type
,
6965 fold_convert (type
, tem
), same
);
6967 return fold_convert_loc (loc
, type
,
6968 fold_build2_loc (loc
, MULT_EXPR
, utype
, tem
,
6969 fold_convert_loc (loc
, utype
, same
)));
6972 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6973 specified by EXPR into the buffer PTR of length LEN bytes.
6974 Return the number of bytes placed in the buffer, or zero
6978 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
6980 tree type
= TREE_TYPE (expr
);
6981 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
6982 int byte
, offset
, word
, words
;
6983 unsigned char value
;
6985 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
6992 return MIN (len
, total_bytes
- off
);
6994 words
= total_bytes
/ UNITS_PER_WORD
;
6996 for (byte
= 0; byte
< total_bytes
; byte
++)
6998 int bitpos
= byte
* BITS_PER_UNIT
;
6999 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7001 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7003 if (total_bytes
> UNITS_PER_WORD
)
7005 word
= byte
/ UNITS_PER_WORD
;
7006 if (WORDS_BIG_ENDIAN
)
7007 word
= (words
- 1) - word
;
7008 offset
= word
* UNITS_PER_WORD
;
7009 if (BYTES_BIG_ENDIAN
)
7010 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7012 offset
+= byte
% UNITS_PER_WORD
;
7015 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7016 if (offset
>= off
&& offset
- off
< len
)
7017 ptr
[offset
- off
] = value
;
7019 return MIN (len
, total_bytes
- off
);
7023 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7024 specified by EXPR into the buffer PTR of length LEN bytes.
7025 Return the number of bytes placed in the buffer, or zero
7029 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7031 tree type
= TREE_TYPE (expr
);
7032 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7033 int total_bytes
= GET_MODE_SIZE (mode
);
7034 FIXED_VALUE_TYPE value
;
7035 tree i_value
, i_type
;
7037 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7040 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7042 if (NULL_TREE
== i_type
|| TYPE_PRECISION (i_type
) != total_bytes
)
7045 value
= TREE_FIXED_CST (expr
);
7046 i_value
= double_int_to_tree (i_type
, value
.data
);
7048 return native_encode_int (i_value
, ptr
, len
, off
);
7052 /* Subroutine of native_encode_expr. Encode the REAL_CST
7053 specified by EXPR into the buffer PTR of length LEN bytes.
7054 Return the number of bytes placed in the buffer, or zero
7058 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7060 tree type
= TREE_TYPE (expr
);
7061 int total_bytes
= GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type
));
7062 int byte
, offset
, word
, words
, bitpos
;
7063 unsigned char value
;
7065 /* There are always 32 bits in each long, no matter the size of
7066 the hosts long. We handle floating point representations with
7070 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7077 return MIN (len
, total_bytes
- off
);
7079 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7081 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7083 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7084 bitpos
+= BITS_PER_UNIT
)
7086 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7087 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7089 if (UNITS_PER_WORD
< 4)
7091 word
= byte
/ UNITS_PER_WORD
;
7092 if (WORDS_BIG_ENDIAN
)
7093 word
= (words
- 1) - word
;
7094 offset
= word
* UNITS_PER_WORD
;
7095 if (BYTES_BIG_ENDIAN
)
7096 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7098 offset
+= byte
% UNITS_PER_WORD
;
7103 if (BYTES_BIG_ENDIAN
)
7105 /* Reverse bytes within each long, or within the entire float
7106 if it's smaller than a long (for HFmode). */
7107 offset
= MIN (3, total_bytes
- 1) - offset
;
7108 gcc_assert (offset
>= 0);
7111 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7113 && offset
- off
< len
)
7114 ptr
[offset
- off
] = value
;
7116 return MIN (len
, total_bytes
- off
);
7119 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7120 specified by EXPR into the buffer PTR of length LEN bytes.
7121 Return the number of bytes placed in the buffer, or zero
7125 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7130 part
= TREE_REALPART (expr
);
7131 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7132 if (off
== -1 && rsize
== 0)
7134 part
= TREE_IMAGPART (expr
);
7136 off
= MAX (0, off
- GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part
))));
7137 isize
= native_encode_expr (part
, ptr
? ptr
+ rsize
: NULL
,
7139 if (off
== -1 && isize
!= rsize
)
7141 return rsize
+ isize
;
7145 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7146 specified by EXPR into the buffer PTR of length LEN bytes.
7147 Return the number of bytes placed in the buffer, or zero
7151 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7158 count
= VECTOR_CST_NELTS (expr
);
7159 itype
= TREE_TYPE (TREE_TYPE (expr
));
7160 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (itype
));
7161 for (i
= 0; i
< count
; i
++)
7168 elem
= VECTOR_CST_ELT (expr
, i
);
7169 int res
= native_encode_expr (elem
, ptr
? ptr
+ offset
: NULL
,
7171 if ((off
== -1 && res
!= size
) || res
== 0)
7183 /* Subroutine of native_encode_expr. Encode the STRING_CST
7184 specified by EXPR into the buffer PTR of length LEN bytes.
7185 Return the number of bytes placed in the buffer, or zero
7189 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7191 tree type
= TREE_TYPE (expr
);
7193 /* Wide-char strings are encoded in target byte-order so native
7194 encoding them is trivial. */
7195 if (BITS_PER_UNIT
!= CHAR_BIT
7196 || TREE_CODE (type
) != ARRAY_TYPE
7197 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7198 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7201 HOST_WIDE_INT total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr
)));
7202 if ((off
== -1 && total_bytes
> len
) || off
>= total_bytes
)
7208 else if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7211 if (off
< TREE_STRING_LENGTH (expr
))
7213 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7214 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7216 memset (ptr
+ written
, 0,
7217 MIN (total_bytes
- written
, len
- written
));
7220 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7221 return MIN (total_bytes
- off
, len
);
7225 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7226 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7227 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7228 anything, just do a dry run. If OFF is not -1 then start
7229 the encoding at byte offset OFF and encode at most LEN bytes.
7230 Return the number of bytes placed in the buffer, or zero upon failure. */
7233 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7235 /* We don't support starting at negative offset and -1 is special. */
7239 switch (TREE_CODE (expr
))
7242 return native_encode_int (expr
, ptr
, len
, off
);
7245 return native_encode_real (expr
, ptr
, len
, off
);
7248 return native_encode_fixed (expr
, ptr
, len
, off
);
7251 return native_encode_complex (expr
, ptr
, len
, off
);
7254 return native_encode_vector (expr
, ptr
, len
, off
);
7257 return native_encode_string (expr
, ptr
, len
, off
);
7265 /* Subroutine of native_interpret_expr. Interpret the contents of
7266 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7267 If the buffer cannot be interpreted, return NULL_TREE. */
7270 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7272 int total_bytes
= GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type
));
7274 if (total_bytes
> len
7275 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7278 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7280 return wide_int_to_tree (type
, result
);
7284 /* Subroutine of native_interpret_expr. Interpret the contents of
7285 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7286 If the buffer cannot be interpreted, return NULL_TREE. */
7289 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7291 scalar_mode mode
= SCALAR_TYPE_MODE (type
);
7292 int total_bytes
= GET_MODE_SIZE (mode
);
7294 FIXED_VALUE_TYPE fixed_value
;
7296 if (total_bytes
> len
7297 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7300 result
= double_int::from_buffer (ptr
, total_bytes
);
7301 fixed_value
= fixed_from_double_int (result
, mode
);
7303 return build_fixed (type
, fixed_value
);
7307 /* Subroutine of native_interpret_expr. Interpret the contents of
7308 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7309 If the buffer cannot be interpreted, return NULL_TREE. */
7312 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7314 scalar_float_mode mode
= SCALAR_FLOAT_TYPE_MODE (type
);
7315 int total_bytes
= GET_MODE_SIZE (mode
);
7316 unsigned char value
;
7317 /* There are always 32 bits in each long, no matter the size of
7318 the hosts long. We handle floating point representations with
7323 if (total_bytes
> len
|| total_bytes
> 24)
7325 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7327 memset (tmp
, 0, sizeof (tmp
));
7328 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7329 bitpos
+= BITS_PER_UNIT
)
7331 /* Both OFFSET and BYTE index within a long;
7332 bitpos indexes the whole float. */
7333 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7334 if (UNITS_PER_WORD
< 4)
7336 int word
= byte
/ UNITS_PER_WORD
;
7337 if (WORDS_BIG_ENDIAN
)
7338 word
= (words
- 1) - word
;
7339 offset
= word
* UNITS_PER_WORD
;
7340 if (BYTES_BIG_ENDIAN
)
7341 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7343 offset
+= byte
% UNITS_PER_WORD
;
7348 if (BYTES_BIG_ENDIAN
)
7350 /* Reverse bytes within each long, or within the entire float
7351 if it's smaller than a long (for HFmode). */
7352 offset
= MIN (3, total_bytes
- 1) - offset
;
7353 gcc_assert (offset
>= 0);
7356 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7358 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7361 real_from_target (&r
, tmp
, mode
);
7362 return build_real (type
, r
);
7366 /* Subroutine of native_interpret_expr. Interpret the contents of
7367 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7368 If the buffer cannot be interpreted, return NULL_TREE. */
7371 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7373 tree etype
, rpart
, ipart
;
7376 etype
= TREE_TYPE (type
);
7377 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
7380 rpart
= native_interpret_expr (etype
, ptr
, size
);
7383 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7386 return build_complex (type
, rpart
, ipart
);
7390 /* Subroutine of native_interpret_expr. Interpret the contents of
7391 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7392 If the buffer cannot be interpreted, return NULL_TREE. */
7395 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7400 etype
= TREE_TYPE (type
);
7401 size
= GET_MODE_SIZE (SCALAR_TYPE_MODE (etype
));
7402 count
= TYPE_VECTOR_SUBPARTS (type
);
7403 if (size
* count
> len
)
7406 auto_vec
<tree
, 32> elements (count
);
7407 for (i
= 0; i
< count
; ++i
)
7409 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7412 elements
.quick_push (elem
);
7414 return build_vector (type
, elements
);
7418 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7419 the buffer PTR of length LEN as a constant of type TYPE. For
7420 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7421 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7422 return NULL_TREE. */
7425 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7427 switch (TREE_CODE (type
))
7433 case REFERENCE_TYPE
:
7434 return native_interpret_int (type
, ptr
, len
);
7437 return native_interpret_real (type
, ptr
, len
);
7439 case FIXED_POINT_TYPE
:
7440 return native_interpret_fixed (type
, ptr
, len
);
7443 return native_interpret_complex (type
, ptr
, len
);
7446 return native_interpret_vector (type
, ptr
, len
);
7453 /* Returns true if we can interpret the contents of a native encoding
7457 can_native_interpret_type_p (tree type
)
7459 switch (TREE_CODE (type
))
7465 case REFERENCE_TYPE
:
7466 case FIXED_POINT_TYPE
:
7477 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7478 TYPE at compile-time. If we're unable to perform the conversion
7479 return NULL_TREE. */
7482 fold_view_convert_expr (tree type
, tree expr
)
7484 /* We support up to 512-bit values (for V8DFmode). */
7485 unsigned char buffer
[64];
7488 /* Check that the host and target are sane. */
7489 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7492 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7496 return native_interpret_expr (type
, buffer
, len
);
7499 /* Build an expression for the address of T. Folds away INDIRECT_REF
7500 to avoid confusing the gimplify process. */
7503 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7505 /* The size of the object is not relevant when talking about its address. */
7506 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7507 t
= TREE_OPERAND (t
, 0);
7509 if (TREE_CODE (t
) == INDIRECT_REF
)
7511 t
= TREE_OPERAND (t
, 0);
7513 if (TREE_TYPE (t
) != ptrtype
)
7514 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7516 else if (TREE_CODE (t
) == MEM_REF
7517 && integer_zerop (TREE_OPERAND (t
, 1)))
7518 return TREE_OPERAND (t
, 0);
7519 else if (TREE_CODE (t
) == MEM_REF
7520 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7521 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7522 TREE_OPERAND (t
, 0),
7523 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7524 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7526 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7528 if (TREE_TYPE (t
) != ptrtype
)
7529 t
= fold_convert_loc (loc
, ptrtype
, t
);
7532 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7537 /* Build an expression for the address of T. */
7540 build_fold_addr_expr_loc (location_t loc
, tree t
)
7542 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7544 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7547 /* Fold a unary expression of code CODE and type TYPE with operand
7548 OP0. Return the folded expression if folding is successful.
7549 Otherwise, return NULL_TREE. */
7552 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7556 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7558 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7559 && TREE_CODE_LENGTH (code
) == 1);
7564 if (CONVERT_EXPR_CODE_P (code
)
7565 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7567 /* Don't use STRIP_NOPS, because signedness of argument type
7569 STRIP_SIGN_NOPS (arg0
);
7573 /* Strip any conversions that don't change the mode. This
7574 is safe for every expression, except for a comparison
7575 expression because its signedness is derived from its
7578 Note that this is done as an internal manipulation within
7579 the constant folder, in order to find the simplest
7580 representation of the arguments so that their form can be
7581 studied. In any cases, the appropriate type conversions
7582 should be put back in the tree that will get out of the
7587 if (CONSTANT_CLASS_P (arg0
))
7589 tree tem
= const_unop (code
, type
, arg0
);
7592 if (TREE_TYPE (tem
) != type
)
7593 tem
= fold_convert_loc (loc
, type
, tem
);
7599 tem
= generic_simplify (loc
, code
, type
, op0
);
7603 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7605 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7606 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7607 fold_build1_loc (loc
, code
, type
,
7608 fold_convert_loc (loc
, TREE_TYPE (op0
),
7609 TREE_OPERAND (arg0
, 1))));
7610 else if (TREE_CODE (arg0
) == COND_EXPR
)
7612 tree arg01
= TREE_OPERAND (arg0
, 1);
7613 tree arg02
= TREE_OPERAND (arg0
, 2);
7614 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7615 arg01
= fold_build1_loc (loc
, code
, type
,
7616 fold_convert_loc (loc
,
7617 TREE_TYPE (op0
), arg01
));
7618 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7619 arg02
= fold_build1_loc (loc
, code
, type
,
7620 fold_convert_loc (loc
,
7621 TREE_TYPE (op0
), arg02
));
7622 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7625 /* If this was a conversion, and all we did was to move into
7626 inside the COND_EXPR, bring it back out. But leave it if
7627 it is a conversion from integer to integer and the
7628 result precision is no wider than a word since such a
7629 conversion is cheap and may be optimized away by combine,
7630 while it couldn't if it were outside the COND_EXPR. Then return
7631 so we don't get into an infinite recursion loop taking the
7632 conversion out and then back in. */
7634 if ((CONVERT_EXPR_CODE_P (code
)
7635 || code
== NON_LVALUE_EXPR
)
7636 && TREE_CODE (tem
) == COND_EXPR
7637 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7638 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7639 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7640 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7641 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7642 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7643 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7645 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7646 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7647 || flag_syntax_only
))
7648 tem
= build1_loc (loc
, code
, type
,
7650 TREE_TYPE (TREE_OPERAND
7651 (TREE_OPERAND (tem
, 1), 0)),
7652 TREE_OPERAND (tem
, 0),
7653 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7654 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7662 case NON_LVALUE_EXPR
:
7663 if (!maybe_lvalue_p (op0
))
7664 return fold_convert_loc (loc
, type
, op0
);
7669 case FIX_TRUNC_EXPR
:
7670 if (COMPARISON_CLASS_P (op0
))
7672 /* If we have (type) (a CMP b) and type is an integral type, return
7673 new expression involving the new type. Canonicalize
7674 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7676 Do not fold the result as that would not simplify further, also
7677 folding again results in recursions. */
7678 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7679 return build2_loc (loc
, TREE_CODE (op0
), type
,
7680 TREE_OPERAND (op0
, 0),
7681 TREE_OPERAND (op0
, 1));
7682 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7683 && TREE_CODE (type
) != VECTOR_TYPE
)
7684 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7685 constant_boolean_node (true, type
),
7686 constant_boolean_node (false, type
));
7689 /* Handle (T *)&A.B.C for A being of type T and B and C
7690 living at offset zero. This occurs frequently in
7691 C++ upcasting and then accessing the base. */
7692 if (TREE_CODE (op0
) == ADDR_EXPR
7693 && POINTER_TYPE_P (type
)
7694 && handled_component_p (TREE_OPERAND (op0
, 0)))
7696 HOST_WIDE_INT bitsize
, bitpos
;
7699 int unsignedp
, reversep
, volatilep
;
7701 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
7702 &offset
, &mode
, &unsignedp
, &reversep
,
7704 /* If the reference was to a (constant) zero offset, we can use
7705 the address of the base if it has the same base type
7706 as the result type and the pointer type is unqualified. */
7707 if (! offset
&& bitpos
== 0
7708 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7709 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7710 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7711 return fold_convert_loc (loc
, type
,
7712 build_fold_addr_expr_loc (loc
, base
));
7715 if (TREE_CODE (op0
) == MODIFY_EXPR
7716 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7717 /* Detect assigning a bitfield. */
7718 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7720 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7722 /* Don't leave an assignment inside a conversion
7723 unless assigning a bitfield. */
7724 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7725 /* First do the assignment, then return converted constant. */
7726 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7727 TREE_NO_WARNING (tem
) = 1;
7728 TREE_USED (tem
) = 1;
7732 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7733 constants (if x has signed type, the sign bit cannot be set
7734 in c). This folds extension into the BIT_AND_EXPR.
7735 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7736 very likely don't have maximal range for their precision and this
7737 transformation effectively doesn't preserve non-maximal ranges. */
7738 if (TREE_CODE (type
) == INTEGER_TYPE
7739 && TREE_CODE (op0
) == BIT_AND_EXPR
7740 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7742 tree and_expr
= op0
;
7743 tree and0
= TREE_OPERAND (and_expr
, 0);
7744 tree and1
= TREE_OPERAND (and_expr
, 1);
7747 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7748 || (TYPE_PRECISION (type
)
7749 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7751 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7752 <= HOST_BITS_PER_WIDE_INT
7753 && tree_fits_uhwi_p (and1
))
7755 unsigned HOST_WIDE_INT cst
;
7757 cst
= tree_to_uhwi (and1
);
7758 cst
&= HOST_WIDE_INT_M1U
7759 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7760 change
= (cst
== 0);
7762 && !flag_syntax_only
7763 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
7766 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7767 and0
= fold_convert_loc (loc
, uns
, and0
);
7768 and1
= fold_convert_loc (loc
, uns
, and1
);
7773 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7774 TREE_OVERFLOW (and1
));
7775 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7776 fold_convert_loc (loc
, type
, and0
), tem
);
7780 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7781 cast (T1)X will fold away. We assume that this happens when X itself
7783 if (POINTER_TYPE_P (type
)
7784 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7785 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
7787 tree arg00
= TREE_OPERAND (arg0
, 0);
7788 tree arg01
= TREE_OPERAND (arg0
, 1);
7790 return fold_build_pointer_plus_loc
7791 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7794 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7795 of the same precision, and X is an integer type not narrower than
7796 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7797 if (INTEGRAL_TYPE_P (type
)
7798 && TREE_CODE (op0
) == BIT_NOT_EXPR
7799 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7800 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7801 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7803 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7804 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7805 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7806 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7807 fold_convert_loc (loc
, type
, tem
));
7810 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7811 type of X and Y (integer types only). */
7812 if (INTEGRAL_TYPE_P (type
)
7813 && TREE_CODE (op0
) == MULT_EXPR
7814 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7815 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7817 /* Be careful not to introduce new overflows. */
7819 if (TYPE_OVERFLOW_WRAPS (type
))
7822 mult_type
= unsigned_type_for (type
);
7824 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7826 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7827 fold_convert_loc (loc
, mult_type
,
7828 TREE_OPERAND (op0
, 0)),
7829 fold_convert_loc (loc
, mult_type
,
7830 TREE_OPERAND (op0
, 1)));
7831 return fold_convert_loc (loc
, type
, tem
);
7837 case VIEW_CONVERT_EXPR
:
7838 if (TREE_CODE (op0
) == MEM_REF
)
7840 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
7841 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
7842 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
7843 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7844 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
7851 tem
= fold_negate_expr (loc
, arg0
);
7853 return fold_convert_loc (loc
, type
, tem
);
7857 /* Convert fabs((double)float) into (double)fabsf(float). */
7858 if (TREE_CODE (arg0
) == NOP_EXPR
7859 && TREE_CODE (type
) == REAL_TYPE
)
7861 tree targ0
= strip_float_extensions (arg0
);
7863 return fold_convert_loc (loc
, type
,
7864 fold_build1_loc (loc
, ABS_EXPR
,
7871 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7872 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7873 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7874 fold_convert_loc (loc
, type
,
7875 TREE_OPERAND (arg0
, 0)))))
7876 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
7877 fold_convert_loc (loc
, type
,
7878 TREE_OPERAND (arg0
, 1)));
7879 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7880 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7881 fold_convert_loc (loc
, type
,
7882 TREE_OPERAND (arg0
, 1)))))
7883 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
7884 fold_convert_loc (loc
, type
,
7885 TREE_OPERAND (arg0
, 0)), tem
);
7889 case TRUTH_NOT_EXPR
:
7890 /* Note that the operand of this must be an int
7891 and its values must be 0 or 1.
7892 ("true" is a fixed value perhaps depending on the language,
7893 but we don't handle values other than 1 correctly yet.) */
7894 tem
= fold_truth_not_expr (loc
, arg0
);
7897 return fold_convert_loc (loc
, type
, tem
);
7900 /* Fold *&X to X if X is an lvalue. */
7901 if (TREE_CODE (op0
) == ADDR_EXPR
)
7903 tree op00
= TREE_OPERAND (op0
, 0);
7905 || TREE_CODE (op00
) == PARM_DECL
7906 || TREE_CODE (op00
) == RESULT_DECL
)
7907 && !TREE_READONLY (op00
))
7914 } /* switch (code) */
7918 /* If the operation was a conversion do _not_ mark a resulting constant
7919 with TREE_OVERFLOW if the original constant was not. These conversions
7920 have implementation defined behavior and retaining the TREE_OVERFLOW
7921 flag here would confuse later passes such as VRP. */
7923 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
7924 tree type
, tree op0
)
7926 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
7928 && TREE_CODE (res
) == INTEGER_CST
7929 && TREE_CODE (op0
) == INTEGER_CST
7930 && CONVERT_EXPR_CODE_P (code
))
7931 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
7936 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7937 operands OP0 and OP1. LOC is the location of the resulting expression.
7938 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7939 Return the folded expression if folding is successful. Otherwise,
7940 return NULL_TREE. */
7942 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
7943 tree arg0
, tree arg1
, tree op0
, tree op1
)
7947 /* We only do these simplifications if we are optimizing. */
7951 /* Check for things like (A || B) && (A || C). We can convert this
7952 to A || (B && C). Note that either operator can be any of the four
7953 truth and/or operations and the transformation will still be
7954 valid. Also note that we only care about order for the
7955 ANDIF and ORIF operators. If B contains side effects, this
7956 might change the truth-value of A. */
7957 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
7958 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
7959 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
7960 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
7961 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
7962 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
7964 tree a00
= TREE_OPERAND (arg0
, 0);
7965 tree a01
= TREE_OPERAND (arg0
, 1);
7966 tree a10
= TREE_OPERAND (arg1
, 0);
7967 tree a11
= TREE_OPERAND (arg1
, 1);
7968 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
7969 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
7970 && (code
== TRUTH_AND_EXPR
7971 || code
== TRUTH_OR_EXPR
));
7973 if (operand_equal_p (a00
, a10
, 0))
7974 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7975 fold_build2_loc (loc
, code
, type
, a01
, a11
));
7976 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
7977 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7978 fold_build2_loc (loc
, code
, type
, a01
, a10
));
7979 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
7980 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
7981 fold_build2_loc (loc
, code
, type
, a00
, a11
));
7983 /* This case if tricky because we must either have commutative
7984 operators or else A10 must not have side-effects. */
7986 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
7987 && operand_equal_p (a01
, a11
, 0))
7988 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
7989 fold_build2_loc (loc
, code
, type
, a00
, a10
),
7993 /* See if we can build a range comparison. */
7994 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
7997 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
7998 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8000 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8002 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8005 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8006 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8008 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8010 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8013 /* Check for the possibility of merging component references. If our
8014 lhs is another similar operation, try to merge its rhs with our
8015 rhs. Then try to merge our lhs and rhs. */
8016 if (TREE_CODE (arg0
) == code
8017 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8018 TREE_OPERAND (arg0
, 1), arg1
)))
8019 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8021 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8024 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8025 && !flag_sanitize_coverage
8026 && (code
== TRUTH_AND_EXPR
8027 || code
== TRUTH_ANDIF_EXPR
8028 || code
== TRUTH_OR_EXPR
8029 || code
== TRUTH_ORIF_EXPR
))
8031 enum tree_code ncode
, icode
;
8033 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8034 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8035 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8037 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8038 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8039 We don't want to pack more than two leafs to a non-IF AND/OR
8041 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8042 equal to IF-CODE, then we don't want to add right-hand operand.
8043 If the inner right-hand side of left-hand operand has
8044 side-effects, or isn't simple, then we can't add to it,
8045 as otherwise we might destroy if-sequence. */
8046 if (TREE_CODE (arg0
) == icode
8047 && simple_operand_p_2 (arg1
)
8048 /* Needed for sequence points to handle trappings, and
8050 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8052 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8054 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8057 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8058 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8059 else if (TREE_CODE (arg1
) == icode
8060 && simple_operand_p_2 (arg0
)
8061 /* Needed for sequence points to handle trappings, and
8063 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8065 tem
= fold_build2_loc (loc
, ncode
, type
,
8066 arg0
, TREE_OPERAND (arg1
, 0));
8067 return fold_build2_loc (loc
, icode
, type
, tem
,
8068 TREE_OPERAND (arg1
, 1));
8070 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8072 For sequence point consistancy, we need to check for trapping,
8073 and side-effects. */
8074 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8075 && simple_operand_p_2 (arg1
))
8076 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8082 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8083 by changing CODE to reduce the magnitude of constants involved in
8084 ARG0 of the comparison.
8085 Returns a canonicalized comparison tree if a simplification was
8086 possible, otherwise returns NULL_TREE.
8087 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8088 valid if signed overflow is undefined. */
8091 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8092 tree arg0
, tree arg1
,
8093 bool *strict_overflow_p
)
8095 enum tree_code code0
= TREE_CODE (arg0
);
8096 tree t
, cst0
= NULL_TREE
;
8099 /* Match A +- CST code arg1. We can change this only if overflow
8101 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8102 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8103 /* In principle pointers also have undefined overflow behavior,
8104 but that causes problems elsewhere. */
8105 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8106 && (code0
== MINUS_EXPR
8107 || code0
== PLUS_EXPR
)
8108 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8111 /* Identify the constant in arg0 and its sign. */
8112 cst0
= TREE_OPERAND (arg0
, 1);
8113 sgn0
= tree_int_cst_sgn (cst0
);
8115 /* Overflowed constants and zero will cause problems. */
8116 if (integer_zerop (cst0
)
8117 || TREE_OVERFLOW (cst0
))
8120 /* See if we can reduce the magnitude of the constant in
8121 arg0 by changing the comparison code. */
8122 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8124 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8126 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8127 else if (code
== GT_EXPR
8128 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8130 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8131 else if (code
== LE_EXPR
8132 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8134 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8135 else if (code
== GE_EXPR
8136 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8140 *strict_overflow_p
= true;
8142 /* Now build the constant reduced in magnitude. But not if that
8143 would produce one outside of its types range. */
8144 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8146 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8147 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8149 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8150 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8153 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8154 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8155 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8156 t
= fold_convert (TREE_TYPE (arg1
), t
);
8158 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8161 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8162 overflow further. Try to decrease the magnitude of constants involved
8163 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8164 and put sole constants at the second argument position.
8165 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8168 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8169 tree arg0
, tree arg1
)
8172 bool strict_overflow_p
;
8173 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8174 "when reducing constant in comparison");
8176 /* Try canonicalization by simplifying arg0. */
8177 strict_overflow_p
= false;
8178 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8179 &strict_overflow_p
);
8182 if (strict_overflow_p
)
8183 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8187 /* Try canonicalization by simplifying arg1 using the swapped
8189 code
= swap_tree_comparison (code
);
8190 strict_overflow_p
= false;
8191 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8192 &strict_overflow_p
);
8193 if (t
&& strict_overflow_p
)
8194 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8198 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8199 space. This is used to avoid issuing overflow warnings for
8200 expressions like &p->x which can not wrap. */
8203 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8205 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8212 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8213 if (offset
== NULL_TREE
)
8214 wi_offset
= wi::zero (precision
);
8215 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8221 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8222 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8226 if (!wi::fits_uhwi_p (total
))
8229 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8233 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8235 if (TREE_CODE (base
) == ADDR_EXPR
)
8237 HOST_WIDE_INT base_size
;
8239 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8240 if (base_size
> 0 && size
< base_size
)
8244 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8247 /* Return a positive integer when the symbol DECL is known to have
8248 a nonzero address, zero when it's known not to (e.g., it's a weak
8249 symbol), and a negative integer when the symbol is not yet in the
8250 symbol table and so whether or not its address is zero is unknown.
8251 For function local objects always return positive integer. */
8253 maybe_nonzero_address (tree decl
)
8255 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
8256 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
8257 return symbol
->nonzero_address ();
8259 /* Function local objects are never NULL. */
8261 && (DECL_CONTEXT (decl
)
8262 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
8263 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
8269 /* Subroutine of fold_binary. This routine performs all of the
8270 transformations that are common to the equality/inequality
8271 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8272 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8273 fold_binary should call fold_binary. Fold a comparison with
8274 tree code CODE and type TYPE with operands OP0 and OP1. Return
8275 the folded comparison or NULL_TREE. */
8278 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8281 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8282 tree arg0
, arg1
, tem
;
8287 STRIP_SIGN_NOPS (arg0
);
8288 STRIP_SIGN_NOPS (arg1
);
8290 /* For comparisons of pointers we can decompose it to a compile time
8291 comparison of the base objects and the offsets into the object.
8292 This requires at least one operand being an ADDR_EXPR or a
8293 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8294 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8295 && (TREE_CODE (arg0
) == ADDR_EXPR
8296 || TREE_CODE (arg1
) == ADDR_EXPR
8297 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8298 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8300 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8301 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8303 int volatilep
, reversep
, unsignedp
;
8304 bool indirect_base0
= false, indirect_base1
= false;
8306 /* Get base and offset for the access. Strip ADDR_EXPR for
8307 get_inner_reference, but put it back by stripping INDIRECT_REF
8308 off the base object if possible. indirect_baseN will be true
8309 if baseN is not an address but refers to the object itself. */
8311 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8314 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8315 &bitsize
, &bitpos0
, &offset0
, &mode
,
8316 &unsignedp
, &reversep
, &volatilep
);
8317 if (TREE_CODE (base0
) == INDIRECT_REF
)
8318 base0
= TREE_OPERAND (base0
, 0);
8320 indirect_base0
= true;
8322 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8324 base0
= TREE_OPERAND (arg0
, 0);
8325 STRIP_SIGN_NOPS (base0
);
8326 if (TREE_CODE (base0
) == ADDR_EXPR
)
8329 = get_inner_reference (TREE_OPERAND (base0
, 0),
8330 &bitsize
, &bitpos0
, &offset0
, &mode
,
8331 &unsignedp
, &reversep
, &volatilep
);
8332 if (TREE_CODE (base0
) == INDIRECT_REF
)
8333 base0
= TREE_OPERAND (base0
, 0);
8335 indirect_base0
= true;
8337 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8338 offset0
= TREE_OPERAND (arg0
, 1);
8340 offset0
= size_binop (PLUS_EXPR
, offset0
,
8341 TREE_OPERAND (arg0
, 1));
8342 if (TREE_CODE (offset0
) == INTEGER_CST
)
8344 offset_int tem
= wi::sext (wi::to_offset (offset0
),
8345 TYPE_PRECISION (sizetype
));
8346 tem
<<= LOG2_BITS_PER_UNIT
;
8348 if (wi::fits_shwi_p (tem
))
8350 bitpos0
= tem
.to_shwi ();
8351 offset0
= NULL_TREE
;
8357 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8360 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8361 &bitsize
, &bitpos1
, &offset1
, &mode
,
8362 &unsignedp
, &reversep
, &volatilep
);
8363 if (TREE_CODE (base1
) == INDIRECT_REF
)
8364 base1
= TREE_OPERAND (base1
, 0);
8366 indirect_base1
= true;
8368 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8370 base1
= TREE_OPERAND (arg1
, 0);
8371 STRIP_SIGN_NOPS (base1
);
8372 if (TREE_CODE (base1
) == ADDR_EXPR
)
8375 = get_inner_reference (TREE_OPERAND (base1
, 0),
8376 &bitsize
, &bitpos1
, &offset1
, &mode
,
8377 &unsignedp
, &reversep
, &volatilep
);
8378 if (TREE_CODE (base1
) == INDIRECT_REF
)
8379 base1
= TREE_OPERAND (base1
, 0);
8381 indirect_base1
= true;
8383 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8384 offset1
= TREE_OPERAND (arg1
, 1);
8386 offset1
= size_binop (PLUS_EXPR
, offset1
,
8387 TREE_OPERAND (arg1
, 1));
8388 if (TREE_CODE (offset1
) == INTEGER_CST
)
8390 offset_int tem
= wi::sext (wi::to_offset (offset1
),
8391 TYPE_PRECISION (sizetype
));
8392 tem
<<= LOG2_BITS_PER_UNIT
;
8394 if (wi::fits_shwi_p (tem
))
8396 bitpos1
= tem
.to_shwi ();
8397 offset1
= NULL_TREE
;
8402 /* If we have equivalent bases we might be able to simplify. */
8403 if (indirect_base0
== indirect_base1
8404 && operand_equal_p (base0
, base1
,
8405 indirect_base0
? OEP_ADDRESS_OF
: 0))
8407 /* We can fold this expression to a constant if the non-constant
8408 offset parts are equal. */
8409 if (offset0
== offset1
8410 || (offset0
&& offset1
8411 && operand_equal_p (offset0
, offset1
, 0)))
8414 && bitpos0
!= bitpos1
8415 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8416 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8417 fold_overflow_warning (("assuming pointer wraparound does not "
8418 "occur when comparing P +- C1 with "
8420 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8425 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8427 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8429 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8431 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8433 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8435 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8439 /* We can simplify the comparison to a comparison of the variable
8440 offset parts if the constant offset parts are equal.
8441 Be careful to use signed sizetype here because otherwise we
8442 mess with array offsets in the wrong way. This is possible
8443 because pointer arithmetic is restricted to retain within an
8444 object and overflow on pointer differences is undefined as of
8445 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8446 else if (bitpos0
== bitpos1
)
8448 /* By converting to signed sizetype we cover middle-end pointer
8449 arithmetic which operates on unsigned pointer types of size
8450 type size and ARRAY_REF offsets which are properly sign or
8451 zero extended from their type in case it is narrower than
8453 if (offset0
== NULL_TREE
)
8454 offset0
= build_int_cst (ssizetype
, 0);
8456 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8457 if (offset1
== NULL_TREE
)
8458 offset1
= build_int_cst (ssizetype
, 0);
8460 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8463 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8464 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8465 fold_overflow_warning (("assuming pointer wraparound does not "
8466 "occur when comparing P +- C1 with "
8468 WARN_STRICT_OVERFLOW_COMPARISON
);
8470 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8473 /* For equal offsets we can simplify to a comparison of the
8475 else if (bitpos0
== bitpos1
8477 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8479 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8480 && ((offset0
== offset1
)
8481 || (offset0
&& offset1
8482 && operand_equal_p (offset0
, offset1
, 0))))
8485 base0
= build_fold_addr_expr_loc (loc
, base0
);
8487 base1
= build_fold_addr_expr_loc (loc
, base1
);
8488 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8490 /* Comparison between an ordinary (non-weak) symbol and a null
8491 pointer can be eliminated since such symbols must have a non
8492 null address. In C, relational expressions between pointers
8493 to objects and null pointers are undefined. The results
8494 below follow the C++ rules with the additional property that
8495 every object pointer compares greater than a null pointer.
8497 else if (((DECL_P (base0
)
8498 && maybe_nonzero_address (base0
) > 0
8499 /* Avoid folding references to struct members at offset 0 to
8500 prevent tests like '&ptr->firstmember == 0' from getting
8501 eliminated. When ptr is null, although the -> expression
8502 is strictly speaking invalid, GCC retains it as a matter
8503 of QoI. See PR c/44555. */
8504 && (offset0
== NULL_TREE
&& bitpos0
!= 0))
8505 || CONSTANT_CLASS_P (base0
))
8507 /* The caller guarantees that when one of the arguments is
8508 constant (i.e., null in this case) it is second. */
8509 && integer_zerop (arg1
))
8516 return constant_boolean_node (false, type
);
8520 return constant_boolean_node (true, type
);
8527 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8528 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8529 the resulting offset is smaller in absolute value than the
8530 original one and has the same sign. */
8531 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8532 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8533 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8534 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8535 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8536 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8537 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8538 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8540 tree const1
= TREE_OPERAND (arg0
, 1);
8541 tree const2
= TREE_OPERAND (arg1
, 1);
8542 tree variable1
= TREE_OPERAND (arg0
, 0);
8543 tree variable2
= TREE_OPERAND (arg1
, 0);
8545 const char * const warnmsg
= G_("assuming signed overflow does not "
8546 "occur when combining constants around "
8549 /* Put the constant on the side where it doesn't overflow and is
8550 of lower absolute value and of same sign than before. */
8551 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8552 ? MINUS_EXPR
: PLUS_EXPR
,
8554 if (!TREE_OVERFLOW (cst
)
8555 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8556 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8558 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8559 return fold_build2_loc (loc
, code
, type
,
8561 fold_build2_loc (loc
, TREE_CODE (arg1
),
8566 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8567 ? MINUS_EXPR
: PLUS_EXPR
,
8569 if (!TREE_OVERFLOW (cst
)
8570 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8571 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8573 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8574 return fold_build2_loc (loc
, code
, type
,
8575 fold_build2_loc (loc
, TREE_CODE (arg0
),
8582 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8586 /* If we are comparing an expression that just has comparisons
8587 of two integer values, arithmetic expressions of those comparisons,
8588 and constants, we can simplify it. There are only three cases
8589 to check: the two values can either be equal, the first can be
8590 greater, or the second can be greater. Fold the expression for
8591 those three values. Since each value must be 0 or 1, we have
8592 eight possibilities, each of which corresponds to the constant 0
8593 or 1 or one of the six possible comparisons.
8595 This handles common cases like (a > b) == 0 but also handles
8596 expressions like ((x > y) - (y > x)) > 0, which supposedly
8597 occur in macroized code. */
8599 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8601 tree cval1
= 0, cval2
= 0;
8604 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8605 /* Don't handle degenerate cases here; they should already
8606 have been handled anyway. */
8607 && cval1
!= 0 && cval2
!= 0
8608 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8609 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8610 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8611 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8612 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8613 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8614 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8616 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8617 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8619 /* We can't just pass T to eval_subst in case cval1 or cval2
8620 was the same as ARG1. */
8623 = fold_build2_loc (loc
, code
, type
,
8624 eval_subst (loc
, arg0
, cval1
, maxval
,
8628 = fold_build2_loc (loc
, code
, type
,
8629 eval_subst (loc
, arg0
, cval1
, maxval
,
8633 = fold_build2_loc (loc
, code
, type
,
8634 eval_subst (loc
, arg0
, cval1
, minval
,
8638 /* All three of these results should be 0 or 1. Confirm they are.
8639 Then use those values to select the proper code to use. */
8641 if (TREE_CODE (high_result
) == INTEGER_CST
8642 && TREE_CODE (equal_result
) == INTEGER_CST
8643 && TREE_CODE (low_result
) == INTEGER_CST
)
8645 /* Make a 3-bit mask with the high-order bit being the
8646 value for `>', the next for '=', and the low for '<'. */
8647 switch ((integer_onep (high_result
) * 4)
8648 + (integer_onep (equal_result
) * 2)
8649 + integer_onep (low_result
))
8653 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8674 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8679 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8680 protected_set_expr_location (tem
, loc
);
8683 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8692 /* Subroutine of fold_binary. Optimize complex multiplications of the
8693 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8694 argument EXPR represents the expression "z" of type TYPE. */
8697 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8699 tree itype
= TREE_TYPE (type
);
8700 tree rpart
, ipart
, tem
;
8702 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8704 rpart
= TREE_OPERAND (expr
, 0);
8705 ipart
= TREE_OPERAND (expr
, 1);
8707 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8709 rpart
= TREE_REALPART (expr
);
8710 ipart
= TREE_IMAGPART (expr
);
8714 expr
= save_expr (expr
);
8715 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8716 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8719 rpart
= save_expr (rpart
);
8720 ipart
= save_expr (ipart
);
8721 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8722 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8723 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8724 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8725 build_zero_cst (itype
));
8729 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8730 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8731 true if successful. */
8734 vec_cst_ctor_to_array (tree arg
, unsigned int nelts
, tree
*elts
)
8738 if (TREE_CODE (arg
) == VECTOR_CST
)
8740 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8741 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8743 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8745 constructor_elt
*elt
;
8747 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8748 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8751 elts
[i
] = elt
->value
;
8755 for (; i
< nelts
; i
++)
8757 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8761 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8762 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8763 NULL_TREE otherwise. */
8766 fold_vec_perm (tree type
, tree arg0
, tree arg1
, vec_perm_indices sel
)
8769 bool need_ctor
= false;
8771 unsigned int nelts
= sel
.length ();
8772 gcc_assert (TYPE_VECTOR_SUBPARTS (type
) == nelts
8773 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8774 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8775 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8776 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8779 tree
*in_elts
= XALLOCAVEC (tree
, nelts
* 2);
8780 if (!vec_cst_ctor_to_array (arg0
, nelts
, in_elts
)
8781 || !vec_cst_ctor_to_array (arg1
, nelts
, in_elts
+ nelts
))
8784 auto_vec
<tree
, 32> out_elts (nelts
);
8785 for (i
= 0; i
< nelts
; i
++)
8787 if (!CONSTANT_CLASS_P (in_elts
[sel
[i
]]))
8789 out_elts
.quick_push (unshare_expr (in_elts
[sel
[i
]]));
8794 vec
<constructor_elt
, va_gc
> *v
;
8795 vec_alloc (v
, nelts
);
8796 for (i
= 0; i
< nelts
; i
++)
8797 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, out_elts
[i
]);
8798 return build_constructor (type
, v
);
8801 return build_vector (type
, out_elts
);
8804 /* Try to fold a pointer difference of type TYPE two address expressions of
8805 array references AREF0 and AREF1 using location LOC. Return a
8806 simplified expression for the difference or NULL_TREE. */
8809 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8810 tree aref0
, tree aref1
)
8812 tree base0
= TREE_OPERAND (aref0
, 0);
8813 tree base1
= TREE_OPERAND (aref1
, 0);
8814 tree base_offset
= build_int_cst (type
, 0);
8816 /* If the bases are array references as well, recurse. If the bases
8817 are pointer indirections compute the difference of the pointers.
8818 If the bases are equal, we are set. */
8819 if ((TREE_CODE (base0
) == ARRAY_REF
8820 && TREE_CODE (base1
) == ARRAY_REF
8822 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8823 || (INDIRECT_REF_P (base0
)
8824 && INDIRECT_REF_P (base1
)
8826 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8827 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8829 TREE_OPERAND (base1
, 0)))))
8830 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8832 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8833 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8834 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8835 tree diff
= fold_build2_loc (loc
, MINUS_EXPR
, type
, op0
, op1
);
8836 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8838 fold_build2_loc (loc
, MULT_EXPR
, type
,
8844 /* If the real or vector real constant CST of type TYPE has an exact
8845 inverse, return it, else return NULL. */
8848 exact_inverse (tree type
, tree cst
)
8853 unsigned vec_nelts
, i
;
8855 switch (TREE_CODE (cst
))
8858 r
= TREE_REAL_CST (cst
);
8860 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8861 return build_real (type
, r
);
8867 vec_nelts
= VECTOR_CST_NELTS (cst
);
8868 unit_type
= TREE_TYPE (type
);
8869 mode
= TYPE_MODE (unit_type
);
8871 auto_vec
<tree
, 32> elts (vec_nelts
);
8872 for (i
= 0; i
< vec_nelts
; i
++)
8874 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
8875 if (!exact_real_inverse (mode
, &r
))
8877 elts
.quick_push (build_real (unit_type
, r
));
8880 return build_vector (type
, elts
);
8888 /* Mask out the tz least significant bits of X of type TYPE where
8889 tz is the number of trailing zeroes in Y. */
8891 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
8893 int tz
= wi::ctz (y
);
8895 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
8899 /* Return true when T is an address and is known to be nonzero.
8900 For floating point we further ensure that T is not denormal.
8901 Similar logic is present in nonzero_address in rtlanal.h.
8903 If the return value is based on the assumption that signed overflow
8904 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8905 change *STRICT_OVERFLOW_P. */
8908 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
8910 tree type
= TREE_TYPE (t
);
8911 enum tree_code code
;
8913 /* Doing something useful for floating point would need more work. */
8914 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
8917 code
= TREE_CODE (t
);
8918 switch (TREE_CODE_CLASS (code
))
8921 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8924 case tcc_comparison
:
8925 return tree_binary_nonzero_warnv_p (code
, type
,
8926 TREE_OPERAND (t
, 0),
8927 TREE_OPERAND (t
, 1),
8930 case tcc_declaration
:
8932 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8940 case TRUTH_NOT_EXPR
:
8941 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8944 case TRUTH_AND_EXPR
:
8946 case TRUTH_XOR_EXPR
:
8947 return tree_binary_nonzero_warnv_p (code
, type
,
8948 TREE_OPERAND (t
, 0),
8949 TREE_OPERAND (t
, 1),
8957 case WITH_SIZE_EXPR
:
8959 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8964 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
8968 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
8973 tree fndecl
= get_callee_fndecl (t
);
8974 if (!fndecl
) return false;
8975 if (flag_delete_null_pointer_checks
&& !flag_check_new
8976 && DECL_IS_OPERATOR_NEW (fndecl
)
8977 && !TREE_NOTHROW (fndecl
))
8979 if (flag_delete_null_pointer_checks
8980 && lookup_attribute ("returns_nonnull",
8981 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
8983 return alloca_call_p (t
);
8992 /* Return true when T is an address and is known to be nonzero.
8993 Handle warnings about undefined signed overflow. */
8996 tree_expr_nonzero_p (tree t
)
8998 bool ret
, strict_overflow_p
;
9000 strict_overflow_p
= false;
9001 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9002 if (strict_overflow_p
)
9003 fold_overflow_warning (("assuming signed overflow does not occur when "
9004 "determining that expression is always "
9006 WARN_STRICT_OVERFLOW_MISC
);
9010 /* Return true if T is known not to be equal to an integer W. */
9013 expr_not_equal_to (tree t
, const wide_int
&w
)
9015 wide_int min
, max
, nz
;
9016 value_range_type rtype
;
9017 switch (TREE_CODE (t
))
9020 return wi::ne_p (t
, w
);
9023 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9025 rtype
= get_range_info (t
, &min
, &max
);
9026 if (rtype
== VR_RANGE
)
9028 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9030 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9033 else if (rtype
== VR_ANTI_RANGE
9034 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9035 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9037 /* If T has some known zero bits and W has any of those bits set,
9038 then T is known not to be equal to W. */
9039 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9040 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9049 /* Fold a binary expression of code CODE and type TYPE with operands
9050 OP0 and OP1. LOC is the location of the resulting expression.
9051 Return the folded expression if folding is successful. Otherwise,
9052 return NULL_TREE. */
9055 fold_binary_loc (location_t loc
,
9056 enum tree_code code
, tree type
, tree op0
, tree op1
)
9058 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9059 tree arg0
, arg1
, tem
;
9060 tree t1
= NULL_TREE
;
9061 bool strict_overflow_p
;
9064 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9065 && TREE_CODE_LENGTH (code
) == 2
9067 && op1
!= NULL_TREE
);
9072 /* Strip any conversions that don't change the mode. This is
9073 safe for every expression, except for a comparison expression
9074 because its signedness is derived from its operands. So, in
9075 the latter case, only strip conversions that don't change the
9076 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9079 Note that this is done as an internal manipulation within the
9080 constant folder, in order to find the simplest representation
9081 of the arguments so that their form can be studied. In any
9082 cases, the appropriate type conversions should be put back in
9083 the tree that will get out of the constant folder. */
9085 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9087 STRIP_SIGN_NOPS (arg0
);
9088 STRIP_SIGN_NOPS (arg1
);
9096 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9097 constant but we can't do arithmetic on them. */
9098 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9100 tem
= const_binop (code
, type
, arg0
, arg1
);
9101 if (tem
!= NULL_TREE
)
9103 if (TREE_TYPE (tem
) != type
)
9104 tem
= fold_convert_loc (loc
, type
, tem
);
9109 /* If this is a commutative operation, and ARG0 is a constant, move it
9110 to ARG1 to reduce the number of tests below. */
9111 if (commutative_tree_code (code
)
9112 && tree_swap_operands_p (arg0
, arg1
))
9113 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9115 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9116 to ARG1 to reduce the number of tests below. */
9117 if (kind
== tcc_comparison
9118 && tree_swap_operands_p (arg0
, arg1
))
9119 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9121 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9125 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9127 First check for cases where an arithmetic operation is applied to a
9128 compound, conditional, or comparison operation. Push the arithmetic
9129 operation inside the compound or conditional to see if any folding
9130 can then be done. Convert comparison to conditional for this purpose.
9131 The also optimizes non-constant cases that used to be done in
9134 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9135 one of the operands is a comparison and the other is a comparison, a
9136 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9137 code below would make the expression more complex. Change it to a
9138 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9139 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9141 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9142 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9143 && TREE_CODE (type
) != VECTOR_TYPE
9144 && ((truth_value_p (TREE_CODE (arg0
))
9145 && (truth_value_p (TREE_CODE (arg1
))
9146 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9147 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9148 || (truth_value_p (TREE_CODE (arg1
))
9149 && (truth_value_p (TREE_CODE (arg0
))
9150 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9151 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9153 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9154 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9157 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9158 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9160 if (code
== EQ_EXPR
)
9161 tem
= invert_truthvalue_loc (loc
, tem
);
9163 return fold_convert_loc (loc
, type
, tem
);
9166 if (TREE_CODE_CLASS (code
) == tcc_binary
9167 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9169 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9171 tem
= fold_build2_loc (loc
, code
, type
,
9172 fold_convert_loc (loc
, TREE_TYPE (op0
),
9173 TREE_OPERAND (arg0
, 1)), op1
);
9174 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9177 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
9179 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9180 fold_convert_loc (loc
, TREE_TYPE (op1
),
9181 TREE_OPERAND (arg1
, 1)));
9182 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9186 if (TREE_CODE (arg0
) == COND_EXPR
9187 || TREE_CODE (arg0
) == VEC_COND_EXPR
9188 || COMPARISON_CLASS_P (arg0
))
9190 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9192 /*cond_first_p=*/1);
9193 if (tem
!= NULL_TREE
)
9197 if (TREE_CODE (arg1
) == COND_EXPR
9198 || TREE_CODE (arg1
) == VEC_COND_EXPR
9199 || COMPARISON_CLASS_P (arg1
))
9201 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9203 /*cond_first_p=*/0);
9204 if (tem
!= NULL_TREE
)
9212 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9213 if (TREE_CODE (arg0
) == ADDR_EXPR
9214 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9216 tree iref
= TREE_OPERAND (arg0
, 0);
9217 return fold_build2 (MEM_REF
, type
,
9218 TREE_OPERAND (iref
, 0),
9219 int_const_binop (PLUS_EXPR
, arg1
,
9220 TREE_OPERAND (iref
, 1)));
9223 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9224 if (TREE_CODE (arg0
) == ADDR_EXPR
9225 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9228 HOST_WIDE_INT coffset
;
9229 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9233 return fold_build2 (MEM_REF
, type
,
9234 build_fold_addr_expr (base
),
9235 int_const_binop (PLUS_EXPR
, arg1
,
9236 size_int (coffset
)));
9241 case POINTER_PLUS_EXPR
:
9242 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9243 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9244 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9245 return fold_convert_loc (loc
, type
,
9246 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9247 fold_convert_loc (loc
, sizetype
,
9249 fold_convert_loc (loc
, sizetype
,
9255 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9257 /* X + (X / CST) * -CST is X % CST. */
9258 if (TREE_CODE (arg1
) == MULT_EXPR
9259 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9260 && operand_equal_p (arg0
,
9261 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9263 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9264 tree cst1
= TREE_OPERAND (arg1
, 1);
9265 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9267 if (sum
&& integer_zerop (sum
))
9268 return fold_convert_loc (loc
, type
,
9269 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9270 TREE_TYPE (arg0
), arg0
,
9275 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9276 one. Make sure the type is not saturating and has the signedness of
9277 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9278 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9279 if ((TREE_CODE (arg0
) == MULT_EXPR
9280 || TREE_CODE (arg1
) == MULT_EXPR
)
9281 && !TYPE_SATURATING (type
)
9282 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9283 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9284 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9286 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9291 if (! FLOAT_TYPE_P (type
))
9293 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9294 (plus (plus (mult) (mult)) (foo)) so that we can
9295 take advantage of the factoring cases below. */
9296 if (ANY_INTEGRAL_TYPE_P (type
)
9297 && TYPE_OVERFLOW_WRAPS (type
)
9298 && (((TREE_CODE (arg0
) == PLUS_EXPR
9299 || TREE_CODE (arg0
) == MINUS_EXPR
)
9300 && TREE_CODE (arg1
) == MULT_EXPR
)
9301 || ((TREE_CODE (arg1
) == PLUS_EXPR
9302 || TREE_CODE (arg1
) == MINUS_EXPR
)
9303 && TREE_CODE (arg0
) == MULT_EXPR
)))
9305 tree parg0
, parg1
, parg
, marg
;
9306 enum tree_code pcode
;
9308 if (TREE_CODE (arg1
) == MULT_EXPR
)
9309 parg
= arg0
, marg
= arg1
;
9311 parg
= arg1
, marg
= arg0
;
9312 pcode
= TREE_CODE (parg
);
9313 parg0
= TREE_OPERAND (parg
, 0);
9314 parg1
= TREE_OPERAND (parg
, 1);
9318 if (TREE_CODE (parg0
) == MULT_EXPR
9319 && TREE_CODE (parg1
) != MULT_EXPR
)
9320 return fold_build2_loc (loc
, pcode
, type
,
9321 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9322 fold_convert_loc (loc
, type
,
9324 fold_convert_loc (loc
, type
,
9326 fold_convert_loc (loc
, type
, parg1
));
9327 if (TREE_CODE (parg0
) != MULT_EXPR
9328 && TREE_CODE (parg1
) == MULT_EXPR
)
9330 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9331 fold_convert_loc (loc
, type
, parg0
),
9332 fold_build2_loc (loc
, pcode
, type
,
9333 fold_convert_loc (loc
, type
, marg
),
9334 fold_convert_loc (loc
, type
,
9340 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9341 to __complex__ ( x, y ). This is not the same for SNaNs or
9342 if signed zeros are involved. */
9343 if (!HONOR_SNANS (element_mode (arg0
))
9344 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9345 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9347 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9348 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9349 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9350 bool arg0rz
= false, arg0iz
= false;
9351 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9352 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9354 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9355 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9356 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9358 tree rp
= arg1r
? arg1r
9359 : build1 (REALPART_EXPR
, rtype
, arg1
);
9360 tree ip
= arg0i
? arg0i
9361 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9362 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9364 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9366 tree rp
= arg0r
? arg0r
9367 : build1 (REALPART_EXPR
, rtype
, arg0
);
9368 tree ip
= arg1i
? arg1i
9369 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9370 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9375 if (flag_unsafe_math_optimizations
9376 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9377 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9378 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9381 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9382 We associate floats only if the user has specified
9383 -fassociative-math. */
9384 if (flag_associative_math
9385 && TREE_CODE (arg1
) == PLUS_EXPR
9386 && TREE_CODE (arg0
) != MULT_EXPR
)
9388 tree tree10
= TREE_OPERAND (arg1
, 0);
9389 tree tree11
= TREE_OPERAND (arg1
, 1);
9390 if (TREE_CODE (tree11
) == MULT_EXPR
9391 && TREE_CODE (tree10
) == MULT_EXPR
)
9394 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9395 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9398 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9399 We associate floats only if the user has specified
9400 -fassociative-math. */
9401 if (flag_associative_math
9402 && TREE_CODE (arg0
) == PLUS_EXPR
9403 && TREE_CODE (arg1
) != MULT_EXPR
)
9405 tree tree00
= TREE_OPERAND (arg0
, 0);
9406 tree tree01
= TREE_OPERAND (arg0
, 1);
9407 if (TREE_CODE (tree01
) == MULT_EXPR
9408 && TREE_CODE (tree00
) == MULT_EXPR
)
9411 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9412 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9418 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9419 is a rotate of A by C1 bits. */
9420 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9421 is a rotate of A by B bits. */
9423 enum tree_code code0
, code1
;
9425 code0
= TREE_CODE (arg0
);
9426 code1
= TREE_CODE (arg1
);
9427 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9428 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9429 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9430 TREE_OPERAND (arg1
, 0), 0)
9431 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9432 TYPE_UNSIGNED (rtype
))
9433 /* Only create rotates in complete modes. Other cases are not
9434 expanded properly. */
9435 && (element_precision (rtype
)
9436 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9438 tree tree01
, tree11
;
9439 enum tree_code code01
, code11
;
9441 tree01
= TREE_OPERAND (arg0
, 1);
9442 tree11
= TREE_OPERAND (arg1
, 1);
9443 STRIP_NOPS (tree01
);
9444 STRIP_NOPS (tree11
);
9445 code01
= TREE_CODE (tree01
);
9446 code11
= TREE_CODE (tree11
);
9447 if (code01
== INTEGER_CST
9448 && code11
== INTEGER_CST
9449 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9450 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9452 tem
= build2_loc (loc
, LROTATE_EXPR
,
9453 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9454 TREE_OPERAND (arg0
, 0),
9455 code0
== LSHIFT_EXPR
9456 ? TREE_OPERAND (arg0
, 1)
9457 : TREE_OPERAND (arg1
, 1));
9458 return fold_convert_loc (loc
, type
, tem
);
9460 else if (code11
== MINUS_EXPR
)
9462 tree tree110
, tree111
;
9463 tree110
= TREE_OPERAND (tree11
, 0);
9464 tree111
= TREE_OPERAND (tree11
, 1);
9465 STRIP_NOPS (tree110
);
9466 STRIP_NOPS (tree111
);
9467 if (TREE_CODE (tree110
) == INTEGER_CST
9468 && 0 == compare_tree_int (tree110
,
9470 (TREE_TYPE (TREE_OPERAND
9472 && operand_equal_p (tree01
, tree111
, 0))
9474 fold_convert_loc (loc
, type
,
9475 build2 ((code0
== LSHIFT_EXPR
9478 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9479 TREE_OPERAND (arg0
, 0),
9480 TREE_OPERAND (arg0
, 1)));
9482 else if (code01
== MINUS_EXPR
)
9484 tree tree010
, tree011
;
9485 tree010
= TREE_OPERAND (tree01
, 0);
9486 tree011
= TREE_OPERAND (tree01
, 1);
9487 STRIP_NOPS (tree010
);
9488 STRIP_NOPS (tree011
);
9489 if (TREE_CODE (tree010
) == INTEGER_CST
9490 && 0 == compare_tree_int (tree010
,
9492 (TREE_TYPE (TREE_OPERAND
9494 && operand_equal_p (tree11
, tree011
, 0))
9495 return fold_convert_loc
9497 build2 ((code0
!= LSHIFT_EXPR
9500 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9501 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9507 /* In most languages, can't associate operations on floats through
9508 parentheses. Rather than remember where the parentheses were, we
9509 don't associate floats at all, unless the user has specified
9511 And, we need to make sure type is not saturating. */
9513 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9514 && !TYPE_SATURATING (type
))
9516 tree var0
, minus_var0
, con0
, minus_con0
, lit0
, minus_lit0
;
9517 tree var1
, minus_var1
, con1
, minus_con1
, lit1
, minus_lit1
;
9521 /* Split both trees into variables, constants, and literals. Then
9522 associate each group together, the constants with literals,
9523 then the result with variables. This increases the chances of
9524 literals being recombined later and of generating relocatable
9525 expressions for the sum of a constant and literal. */
9526 var0
= split_tree (arg0
, type
, code
,
9527 &minus_var0
, &con0
, &minus_con0
,
9528 &lit0
, &minus_lit0
, 0);
9529 var1
= split_tree (arg1
, type
, code
,
9530 &minus_var1
, &con1
, &minus_con1
,
9531 &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9533 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9534 if (code
== MINUS_EXPR
)
9537 /* With undefined overflow prefer doing association in a type
9538 which wraps on overflow, if that is one of the operand types. */
9539 if (POINTER_TYPE_P (type
)
9540 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9542 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9543 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9544 atype
= TREE_TYPE (arg0
);
9545 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9546 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9547 atype
= TREE_TYPE (arg1
);
9548 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9551 /* With undefined overflow we can only associate constants with one
9552 variable, and constants whose association doesn't overflow. */
9553 if (POINTER_TYPE_P (atype
)
9554 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9556 if ((var0
&& var1
) || (minus_var0
&& minus_var1
))
9558 /* ??? If split_tree would handle NEGATE_EXPR we could
9559 simply reject these cases and the allowed cases would
9560 be the var0/minus_var1 ones. */
9561 tree tmp0
= var0
? var0
: minus_var0
;
9562 tree tmp1
= var1
? var1
: minus_var1
;
9563 bool one_neg
= false;
9565 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9567 tmp0
= TREE_OPERAND (tmp0
, 0);
9570 if (CONVERT_EXPR_P (tmp0
)
9571 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9572 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9573 <= TYPE_PRECISION (atype
)))
9574 tmp0
= TREE_OPERAND (tmp0
, 0);
9575 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9577 tmp1
= TREE_OPERAND (tmp1
, 0);
9580 if (CONVERT_EXPR_P (tmp1
)
9581 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9582 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9583 <= TYPE_PRECISION (atype
)))
9584 tmp1
= TREE_OPERAND (tmp1
, 0);
9585 /* The only case we can still associate with two variables
9586 is if they cancel out. */
9588 || !operand_equal_p (tmp0
, tmp1
, 0))
9591 else if ((var0
&& minus_var1
9592 && ! operand_equal_p (var0
, minus_var1
, 0))
9593 || (minus_var0
&& var1
9594 && ! operand_equal_p (minus_var0
, var1
, 0)))
9598 /* Only do something if we found more than two objects. Otherwise,
9599 nothing has changed and we risk infinite recursion. */
9601 && (2 < ((var0
!= 0) + (var1
!= 0)
9602 + (minus_var0
!= 0) + (minus_var1
!= 0)
9603 + (con0
!= 0) + (con1
!= 0)
9604 + (minus_con0
!= 0) + (minus_con1
!= 0)
9605 + (lit0
!= 0) + (lit1
!= 0)
9606 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9608 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9609 minus_var0
= associate_trees (loc
, minus_var0
, minus_var1
,
9611 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9612 minus_con0
= associate_trees (loc
, minus_con0
, minus_con1
,
9614 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9615 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9618 if (minus_var0
&& var0
)
9620 var0
= associate_trees (loc
, var0
, minus_var0
,
9624 if (minus_con0
&& con0
)
9626 con0
= associate_trees (loc
, con0
, minus_con0
,
9631 /* Preserve the MINUS_EXPR if the negative part of the literal is
9632 greater than the positive part. Otherwise, the multiplicative
9633 folding code (i.e extract_muldiv) may be fooled in case
9634 unsigned constants are subtracted, like in the following
9635 example: ((X*2 + 4) - 8U)/2. */
9636 if (minus_lit0
&& lit0
)
9638 if (TREE_CODE (lit0
) == INTEGER_CST
9639 && TREE_CODE (minus_lit0
) == INTEGER_CST
9640 && tree_int_cst_lt (lit0
, minus_lit0
)
9641 /* But avoid ending up with only negated parts. */
9644 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9650 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9656 /* Don't introduce overflows through reassociation. */
9657 if ((lit0
&& TREE_OVERFLOW_P (lit0
))
9658 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
)))
9661 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9662 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9664 minus_con0
= associate_trees (loc
, minus_con0
, minus_lit0
,
9668 /* Eliminate minus_con0. */
9672 con0
= associate_trees (loc
, con0
, minus_con0
,
9675 var0
= associate_trees (loc
, var0
, minus_con0
,
9682 /* Eliminate minus_var0. */
9686 con0
= associate_trees (loc
, con0
, minus_var0
,
9694 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9702 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9703 if (TREE_CODE (arg0
) == NEGATE_EXPR
9704 && negate_expr_p (op1
))
9705 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9707 fold_convert_loc (loc
, type
,
9708 TREE_OPERAND (arg0
, 0)));
9710 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9711 __complex__ ( x, -y ). This is not the same for SNaNs or if
9712 signed zeros are involved. */
9713 if (!HONOR_SNANS (element_mode (arg0
))
9714 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9715 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9717 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9718 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9719 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9720 bool arg0rz
= false, arg0iz
= false;
9721 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9722 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9724 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9725 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9726 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9728 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9730 : build1 (REALPART_EXPR
, rtype
, arg1
));
9731 tree ip
= arg0i
? arg0i
9732 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9733 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9735 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9737 tree rp
= arg0r
? arg0r
9738 : build1 (REALPART_EXPR
, rtype
, arg0
);
9739 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9741 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9742 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9747 /* A - B -> A + (-B) if B is easily negatable. */
9748 if (negate_expr_p (op1
)
9749 && ! TYPE_OVERFLOW_SANITIZED (type
)
9750 && ((FLOAT_TYPE_P (type
)
9751 /* Avoid this transformation if B is a positive REAL_CST. */
9752 && (TREE_CODE (op1
) != REAL_CST
9753 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9754 || INTEGRAL_TYPE_P (type
)))
9755 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9756 fold_convert_loc (loc
, type
, arg0
),
9759 /* Fold &a[i] - &a[j] to i-j. */
9760 if (TREE_CODE (arg0
) == ADDR_EXPR
9761 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9762 && TREE_CODE (arg1
) == ADDR_EXPR
9763 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9765 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9766 TREE_OPERAND (arg0
, 0),
9767 TREE_OPERAND (arg1
, 0));
9772 if (FLOAT_TYPE_P (type
)
9773 && flag_unsafe_math_optimizations
9774 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9775 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9776 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9779 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9780 one. Make sure the type is not saturating and has the signedness of
9781 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9782 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9783 if ((TREE_CODE (arg0
) == MULT_EXPR
9784 || TREE_CODE (arg1
) == MULT_EXPR
)
9785 && !TYPE_SATURATING (type
)
9786 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9787 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9788 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9790 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9798 if (! FLOAT_TYPE_P (type
))
9800 /* Transform x * -C into -x * C if x is easily negatable. */
9801 if (TREE_CODE (op1
) == INTEGER_CST
9802 && tree_int_cst_sgn (op1
) == -1
9803 && negate_expr_p (op0
)
9804 && negate_expr_p (op1
)
9805 && (tem
= negate_expr (op1
)) != op1
9806 && ! TREE_OVERFLOW (tem
))
9807 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9808 fold_convert_loc (loc
, type
,
9809 negate_expr (op0
)), tem
);
9811 strict_overflow_p
= false;
9812 if (TREE_CODE (arg1
) == INTEGER_CST
9813 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9814 &strict_overflow_p
)))
9816 if (strict_overflow_p
)
9817 fold_overflow_warning (("assuming signed overflow does not "
9818 "occur when simplifying "
9820 WARN_STRICT_OVERFLOW_MISC
);
9821 return fold_convert_loc (loc
, type
, tem
);
9824 /* Optimize z * conj(z) for integer complex numbers. */
9825 if (TREE_CODE (arg0
) == CONJ_EXPR
9826 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9827 return fold_mult_zconjz (loc
, type
, arg1
);
9828 if (TREE_CODE (arg1
) == CONJ_EXPR
9829 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9830 return fold_mult_zconjz (loc
, type
, arg0
);
9834 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9835 This is not the same for NaNs or if signed zeros are
9837 if (!HONOR_NANS (arg0
)
9838 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9839 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9840 && TREE_CODE (arg1
) == COMPLEX_CST
9841 && real_zerop (TREE_REALPART (arg1
)))
9843 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9844 if (real_onep (TREE_IMAGPART (arg1
)))
9846 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9847 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9849 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9850 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9852 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9853 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9854 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9858 /* Optimize z * conj(z) for floating point complex numbers.
9859 Guarded by flag_unsafe_math_optimizations as non-finite
9860 imaginary components don't produce scalar results. */
9861 if (flag_unsafe_math_optimizations
9862 && TREE_CODE (arg0
) == CONJ_EXPR
9863 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9864 return fold_mult_zconjz (loc
, type
, arg1
);
9865 if (flag_unsafe_math_optimizations
9866 && TREE_CODE (arg1
) == CONJ_EXPR
9867 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9868 return fold_mult_zconjz (loc
, type
, arg0
);
9873 /* Canonicalize (X & C1) | C2. */
9874 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9875 && TREE_CODE (arg1
) == INTEGER_CST
9876 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9878 int width
= TYPE_PRECISION (type
), w
;
9879 wide_int c1
= TREE_OPERAND (arg0
, 1);
9882 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9883 if ((c1
& c2
) == c1
)
9884 return omit_one_operand_loc (loc
, type
, arg1
,
9885 TREE_OPERAND (arg0
, 0));
9887 wide_int msk
= wi::mask (width
, false,
9888 TYPE_PRECISION (TREE_TYPE (arg1
)));
9890 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9891 if (wi::bit_and_not (msk
, c1
| c2
) == 0)
9893 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9894 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
9897 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9898 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9899 mode which allows further optimizations. */
9902 wide_int c3
= wi::bit_and_not (c1
, c2
);
9903 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
9905 wide_int mask
= wi::mask (w
, false,
9906 TYPE_PRECISION (type
));
9907 if (((c1
| c2
) & mask
) == mask
9908 && wi::bit_and_not (c1
, mask
) == 0)
9917 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9918 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
9919 wide_int_to_tree (type
, c3
));
9920 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
9924 /* See if this can be simplified into a rotate first. If that
9925 is unsuccessful continue in the association code. */
9929 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9930 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9931 && INTEGRAL_TYPE_P (type
)
9932 && integer_onep (TREE_OPERAND (arg0
, 1))
9933 && integer_onep (arg1
))
9934 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
9935 build_zero_cst (TREE_TYPE (arg0
)));
9937 /* See if this can be simplified into a rotate first. If that
9938 is unsuccessful continue in the association code. */
9942 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9943 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9944 && INTEGRAL_TYPE_P (type
)
9945 && integer_onep (TREE_OPERAND (arg0
, 1))
9946 && integer_onep (arg1
))
9949 tem
= TREE_OPERAND (arg0
, 0);
9950 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9951 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9953 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9954 build_zero_cst (TREE_TYPE (tem
)));
9956 /* Fold ~X & 1 as (X & 1) == 0. */
9957 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9958 && INTEGRAL_TYPE_P (type
)
9959 && integer_onep (arg1
))
9962 tem
= TREE_OPERAND (arg0
, 0);
9963 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9964 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9966 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9967 build_zero_cst (TREE_TYPE (tem
)));
9969 /* Fold !X & 1 as X == 0. */
9970 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
9971 && integer_onep (arg1
))
9973 tem
= TREE_OPERAND (arg0
, 0);
9974 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
9975 build_zero_cst (TREE_TYPE (tem
)));
9978 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9979 multiple of 1 << CST. */
9980 if (TREE_CODE (arg1
) == INTEGER_CST
)
9982 wide_int cst1
= arg1
;
9983 wide_int ncst1
= -cst1
;
9984 if ((cst1
& ncst1
) == ncst1
9985 && multiple_of_p (type
, arg0
,
9986 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
9987 return fold_convert_loc (loc
, type
, arg0
);
9990 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9992 if (TREE_CODE (arg1
) == INTEGER_CST
9993 && TREE_CODE (arg0
) == MULT_EXPR
9994 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9996 wide_int warg1
= arg1
;
9997 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
10000 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10002 else if (masked
!= warg1
)
10004 /* Avoid the transform if arg1 is a mask of some
10005 mode which allows further optimizations. */
10006 int pop
= wi::popcount (warg1
);
10007 if (!(pop
>= BITS_PER_UNIT
10009 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10010 return fold_build2_loc (loc
, code
, type
, op0
,
10011 wide_int_to_tree (type
, masked
));
10015 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10016 ((A & N) + B) & M -> (A + B) & M
10017 Similarly if (N & M) == 0,
10018 ((A | N) + B) & M -> (A + B) & M
10019 and for - instead of + (or unary - instead of +)
10020 and/or ^ instead of |.
10021 If B is constant and (B & M) == 0, fold into A & M. */
10022 if (TREE_CODE (arg1
) == INTEGER_CST
)
10024 wide_int cst1
= arg1
;
10025 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10026 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10027 && (TREE_CODE (arg0
) == PLUS_EXPR
10028 || TREE_CODE (arg0
) == MINUS_EXPR
10029 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10030 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10031 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10037 /* Now we know that arg0 is (C + D) or (C - D) or
10038 -C and arg1 (M) is == (1LL << cst) - 1.
10039 Store C into PMOP[0] and D into PMOP[1]. */
10040 pmop
[0] = TREE_OPERAND (arg0
, 0);
10042 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10044 pmop
[1] = TREE_OPERAND (arg0
, 1);
10048 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10051 for (; which
>= 0; which
--)
10052 switch (TREE_CODE (pmop
[which
]))
10057 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10060 cst0
= TREE_OPERAND (pmop
[which
], 1);
10062 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10067 else if (cst0
!= 0)
10069 /* If C or D is of the form (A & N) where
10070 (N & M) == M, or of the form (A | N) or
10071 (A ^ N) where (N & M) == 0, replace it with A. */
10072 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10075 /* If C or D is a N where (N & M) == 0, it can be
10076 omitted (assumed 0). */
10077 if ((TREE_CODE (arg0
) == PLUS_EXPR
10078 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10079 && (cst1
& pmop
[which
]) == 0)
10080 pmop
[which
] = NULL
;
10086 /* Only build anything new if we optimized one or both arguments
10088 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10089 || (TREE_CODE (arg0
) != NEGATE_EXPR
10090 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10092 tree utype
= TREE_TYPE (arg0
);
10093 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10095 /* Perform the operations in a type that has defined
10096 overflow behavior. */
10097 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10098 if (pmop
[0] != NULL
)
10099 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10100 if (pmop
[1] != NULL
)
10101 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10104 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10105 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10106 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10108 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10109 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10111 else if (pmop
[0] != NULL
)
10113 else if (pmop
[1] != NULL
)
10116 return build_int_cst (type
, 0);
10118 else if (pmop
[0] == NULL
)
10119 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10121 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10123 /* TEM is now the new binary +, - or unary - replacement. */
10124 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10125 fold_convert_loc (loc
, utype
, arg1
));
10126 return fold_convert_loc (loc
, type
, tem
);
10131 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10132 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10133 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10135 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10137 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10140 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10146 /* Don't touch a floating-point divide by zero unless the mode
10147 of the constant can represent infinity. */
10148 if (TREE_CODE (arg1
) == REAL_CST
10149 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10150 && real_zerop (arg1
))
10153 /* (-A) / (-B) -> A / B */
10154 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10155 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10156 TREE_OPERAND (arg0
, 0),
10157 negate_expr (arg1
));
10158 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10159 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10160 negate_expr (arg0
),
10161 TREE_OPERAND (arg1
, 0));
10164 case TRUNC_DIV_EXPR
:
10167 case FLOOR_DIV_EXPR
:
10168 /* Simplify A / (B << N) where A and B are positive and B is
10169 a power of 2, to A >> (N + log2(B)). */
10170 strict_overflow_p
= false;
10171 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10172 && (TYPE_UNSIGNED (type
)
10173 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10175 tree sval
= TREE_OPERAND (arg1
, 0);
10176 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10178 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10179 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10180 wi::exact_log2 (sval
));
10182 if (strict_overflow_p
)
10183 fold_overflow_warning (("assuming signed overflow does not "
10184 "occur when simplifying A / (B << N)"),
10185 WARN_STRICT_OVERFLOW_MISC
);
10187 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10189 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10190 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10196 case ROUND_DIV_EXPR
:
10197 case CEIL_DIV_EXPR
:
10198 case EXACT_DIV_EXPR
:
10199 if (integer_zerop (arg1
))
10202 /* Convert -A / -B to A / B when the type is signed and overflow is
10204 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10205 && TREE_CODE (op0
) == NEGATE_EXPR
10206 && negate_expr_p (op1
))
10208 if (INTEGRAL_TYPE_P (type
))
10209 fold_overflow_warning (("assuming signed overflow does not occur "
10210 "when distributing negation across "
10212 WARN_STRICT_OVERFLOW_MISC
);
10213 return fold_build2_loc (loc
, code
, type
,
10214 fold_convert_loc (loc
, type
,
10215 TREE_OPERAND (arg0
, 0)),
10216 negate_expr (op1
));
10218 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10219 && TREE_CODE (arg1
) == NEGATE_EXPR
10220 && negate_expr_p (op0
))
10222 if (INTEGRAL_TYPE_P (type
))
10223 fold_overflow_warning (("assuming signed overflow does not occur "
10224 "when distributing negation across "
10226 WARN_STRICT_OVERFLOW_MISC
);
10227 return fold_build2_loc (loc
, code
, type
,
10229 fold_convert_loc (loc
, type
,
10230 TREE_OPERAND (arg1
, 0)));
10233 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10234 operation, EXACT_DIV_EXPR.
10236 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10237 At one time others generated faster code, it's not clear if they do
10238 after the last round to changes to the DIV code in expmed.c. */
10239 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10240 && multiple_of_p (type
, arg0
, arg1
))
10241 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10242 fold_convert (type
, arg0
),
10243 fold_convert (type
, arg1
));
10245 strict_overflow_p
= false;
10246 if (TREE_CODE (arg1
) == INTEGER_CST
10247 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10248 &strict_overflow_p
)))
10250 if (strict_overflow_p
)
10251 fold_overflow_warning (("assuming signed overflow does not occur "
10252 "when simplifying division"),
10253 WARN_STRICT_OVERFLOW_MISC
);
10254 return fold_convert_loc (loc
, type
, tem
);
10259 case CEIL_MOD_EXPR
:
10260 case FLOOR_MOD_EXPR
:
10261 case ROUND_MOD_EXPR
:
10262 case TRUNC_MOD_EXPR
:
10263 strict_overflow_p
= false;
10264 if (TREE_CODE (arg1
) == INTEGER_CST
10265 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10266 &strict_overflow_p
)))
10268 if (strict_overflow_p
)
10269 fold_overflow_warning (("assuming signed overflow does not occur "
10270 "when simplifying modulus"),
10271 WARN_STRICT_OVERFLOW_MISC
);
10272 return fold_convert_loc (loc
, type
, tem
);
10281 /* Since negative shift count is not well-defined,
10282 don't try to compute it in the compiler. */
10283 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10286 prec
= element_precision (type
);
10288 /* If we have a rotate of a bit operation with the rotate count and
10289 the second operand of the bit operation both constant,
10290 permute the two operations. */
10291 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10292 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10293 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10294 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10295 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10297 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10298 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10299 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10300 fold_build2_loc (loc
, code
, type
,
10302 fold_build2_loc (loc
, code
, type
,
10306 /* Two consecutive rotates adding up to the some integer
10307 multiple of the precision of the type can be ignored. */
10308 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10309 && TREE_CODE (arg0
) == RROTATE_EXPR
10310 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10311 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10313 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10321 case TRUTH_ANDIF_EXPR
:
10322 /* Note that the operands of this must be ints
10323 and their values must be 0 or 1.
10324 ("true" is a fixed value perhaps depending on the language.) */
10325 /* If first arg is constant zero, return it. */
10326 if (integer_zerop (arg0
))
10327 return fold_convert_loc (loc
, type
, arg0
);
10329 case TRUTH_AND_EXPR
:
10330 /* If either arg is constant true, drop it. */
10331 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10332 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10333 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10334 /* Preserve sequence points. */
10335 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10336 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10337 /* If second arg is constant zero, result is zero, but first arg
10338 must be evaluated. */
10339 if (integer_zerop (arg1
))
10340 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10341 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10342 case will be handled here. */
10343 if (integer_zerop (arg0
))
10344 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10346 /* !X && X is always false. */
10347 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10348 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10349 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10350 /* X && !X is always false. */
10351 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10352 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10353 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10355 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10356 means A >= Y && A != MAX, but in this case we know that
10359 if (!TREE_SIDE_EFFECTS (arg0
)
10360 && !TREE_SIDE_EFFECTS (arg1
))
10362 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10363 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10364 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10366 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10367 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10368 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10371 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10377 case TRUTH_ORIF_EXPR
:
10378 /* Note that the operands of this must be ints
10379 and their values must be 0 or true.
10380 ("true" is a fixed value perhaps depending on the language.) */
10381 /* If first arg is constant true, return it. */
10382 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10383 return fold_convert_loc (loc
, type
, arg0
);
10385 case TRUTH_OR_EXPR
:
10386 /* If either arg is constant zero, drop it. */
10387 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10388 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10389 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10390 /* Preserve sequence points. */
10391 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10392 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10393 /* If second arg is constant true, result is true, but we must
10394 evaluate first arg. */
10395 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10396 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10397 /* Likewise for first arg, but note this only occurs here for
10399 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10400 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10402 /* !X || X is always true. */
10403 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10404 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10405 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10406 /* X || !X is always true. */
10407 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10408 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10409 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10411 /* (X && !Y) || (!X && Y) is X ^ Y */
10412 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10413 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10415 tree a0
, a1
, l0
, l1
, n0
, n1
;
10417 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10418 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10420 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10421 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10423 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10424 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10426 if ((operand_equal_p (n0
, a0
, 0)
10427 && operand_equal_p (n1
, a1
, 0))
10428 || (operand_equal_p (n0
, a1
, 0)
10429 && operand_equal_p (n1
, a0
, 0)))
10430 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10433 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10439 case TRUTH_XOR_EXPR
:
10440 /* If the second arg is constant zero, drop it. */
10441 if (integer_zerop (arg1
))
10442 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10443 /* If the second arg is constant true, this is a logical inversion. */
10444 if (integer_onep (arg1
))
10446 tem
= invert_truthvalue_loc (loc
, arg0
);
10447 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10449 /* Identical arguments cancel to zero. */
10450 if (operand_equal_p (arg0
, arg1
, 0))
10451 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10453 /* !X ^ X is always true. */
10454 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10455 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10456 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10458 /* X ^ !X is always true. */
10459 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10460 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10461 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10470 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10471 if (tem
!= NULL_TREE
)
10474 /* bool_var != 1 becomes !bool_var. */
10475 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10476 && code
== NE_EXPR
)
10477 return fold_convert_loc (loc
, type
,
10478 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10479 TREE_TYPE (arg0
), arg0
));
10481 /* bool_var == 0 becomes !bool_var. */
10482 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10483 && code
== EQ_EXPR
)
10484 return fold_convert_loc (loc
, type
,
10485 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10486 TREE_TYPE (arg0
), arg0
));
10488 /* !exp != 0 becomes !exp */
10489 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10490 && code
== NE_EXPR
)
10491 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10493 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10494 if ((TREE_CODE (arg0
) == PLUS_EXPR
10495 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10496 || TREE_CODE (arg0
) == MINUS_EXPR
)
10497 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10500 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10501 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10503 tree val
= TREE_OPERAND (arg0
, 1);
10504 val
= fold_build2_loc (loc
, code
, type
, val
,
10505 build_int_cst (TREE_TYPE (val
), 0));
10506 return omit_two_operands_loc (loc
, type
, val
,
10507 TREE_OPERAND (arg0
, 0), arg1
);
10510 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10511 if ((TREE_CODE (arg1
) == PLUS_EXPR
10512 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
10513 || TREE_CODE (arg1
) == MINUS_EXPR
)
10514 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10517 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10518 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
10520 tree val
= TREE_OPERAND (arg1
, 1);
10521 val
= fold_build2_loc (loc
, code
, type
, val
,
10522 build_int_cst (TREE_TYPE (val
), 0));
10523 return omit_two_operands_loc (loc
, type
, val
,
10524 TREE_OPERAND (arg1
, 0), arg0
);
10527 /* If this is an EQ or NE comparison with zero and ARG0 is
10528 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10529 two operations, but the latter can be done in one less insn
10530 on machines that have only two-operand insns or on which a
10531 constant cannot be the first operand. */
10532 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10533 && integer_zerop (arg1
))
10535 tree arg00
= TREE_OPERAND (arg0
, 0);
10536 tree arg01
= TREE_OPERAND (arg0
, 1);
10537 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10538 && integer_onep (TREE_OPERAND (arg00
, 0)))
10540 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10541 arg01
, TREE_OPERAND (arg00
, 1));
10542 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10543 build_int_cst (TREE_TYPE (arg0
), 1));
10544 return fold_build2_loc (loc
, code
, type
,
10545 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10548 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10549 && integer_onep (TREE_OPERAND (arg01
, 0)))
10551 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10552 arg00
, TREE_OPERAND (arg01
, 1));
10553 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10554 build_int_cst (TREE_TYPE (arg0
), 1));
10555 return fold_build2_loc (loc
, code
, type
,
10556 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10561 /* If this is an NE or EQ comparison of zero against the result of a
10562 signed MOD operation whose second operand is a power of 2, make
10563 the MOD operation unsigned since it is simpler and equivalent. */
10564 if (integer_zerop (arg1
)
10565 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10566 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10567 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10568 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10569 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10570 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10572 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10573 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10574 fold_convert_loc (loc
, newtype
,
10575 TREE_OPERAND (arg0
, 0)),
10576 fold_convert_loc (loc
, newtype
,
10577 TREE_OPERAND (arg0
, 1)));
10579 return fold_build2_loc (loc
, code
, type
, newmod
,
10580 fold_convert_loc (loc
, newtype
, arg1
));
10583 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10584 C1 is a valid shift constant, and C2 is a power of two, i.e.
10586 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10587 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10588 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10590 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10591 && integer_zerop (arg1
))
10593 tree itype
= TREE_TYPE (arg0
);
10594 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10595 prec
= TYPE_PRECISION (itype
);
10597 /* Check for a valid shift count. */
10598 if (wi::ltu_p (arg001
, prec
))
10600 tree arg01
= TREE_OPERAND (arg0
, 1);
10601 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10602 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10603 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10604 can be rewritten as (X & (C2 << C1)) != 0. */
10605 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10607 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10608 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10609 return fold_build2_loc (loc
, code
, type
, tem
,
10610 fold_convert_loc (loc
, itype
, arg1
));
10612 /* Otherwise, for signed (arithmetic) shifts,
10613 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10614 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10615 else if (!TYPE_UNSIGNED (itype
))
10616 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10617 arg000
, build_int_cst (itype
, 0));
10618 /* Otherwise, of unsigned (logical) shifts,
10619 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10620 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10622 return omit_one_operand_loc (loc
, type
,
10623 code
== EQ_EXPR
? integer_one_node
10624 : integer_zero_node
,
10629 /* If this is a comparison of a field, we may be able to simplify it. */
10630 if ((TREE_CODE (arg0
) == COMPONENT_REF
10631 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10632 /* Handle the constant case even without -O
10633 to make sure the warnings are given. */
10634 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10636 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10641 /* Optimize comparisons of strlen vs zero to a compare of the
10642 first character of the string vs zero. To wit,
10643 strlen(ptr) == 0 => *ptr == 0
10644 strlen(ptr) != 0 => *ptr != 0
10645 Other cases should reduce to one of these two (or a constant)
10646 due to the return value of strlen being unsigned. */
10647 if (TREE_CODE (arg0
) == CALL_EXPR
10648 && integer_zerop (arg1
))
10650 tree fndecl
= get_callee_fndecl (arg0
);
10653 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10654 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10655 && call_expr_nargs (arg0
) == 1
10656 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10658 tree iref
= build_fold_indirect_ref_loc (loc
,
10659 CALL_EXPR_ARG (arg0
, 0));
10660 return fold_build2_loc (loc
, code
, type
, iref
,
10661 build_int_cst (TREE_TYPE (iref
), 0));
10665 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10666 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10667 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10668 && integer_zerop (arg1
)
10669 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10671 tree arg00
= TREE_OPERAND (arg0
, 0);
10672 tree arg01
= TREE_OPERAND (arg0
, 1);
10673 tree itype
= TREE_TYPE (arg00
);
10674 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10676 if (TYPE_UNSIGNED (itype
))
10678 itype
= signed_type_for (itype
);
10679 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10681 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10682 type
, arg00
, build_zero_cst (itype
));
10686 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10687 (X & C) == 0 when C is a single bit. */
10688 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10689 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10690 && integer_zerop (arg1
)
10691 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10693 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10694 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10695 TREE_OPERAND (arg0
, 1));
10696 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10698 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10702 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10703 constant C is a power of two, i.e. a single bit. */
10704 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10705 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10706 && integer_zerop (arg1
)
10707 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10708 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10709 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10711 tree arg00
= TREE_OPERAND (arg0
, 0);
10712 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10713 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10716 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10717 when is C is a power of two, i.e. a single bit. */
10718 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10719 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10720 && integer_zerop (arg1
)
10721 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10722 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10723 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10725 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10726 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10727 arg000
, TREE_OPERAND (arg0
, 1));
10728 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10729 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10732 if (integer_zerop (arg1
)
10733 && tree_expr_nonzero_p (arg0
))
10735 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10736 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10739 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10740 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10741 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10743 tree arg00
= TREE_OPERAND (arg0
, 0);
10744 tree arg01
= TREE_OPERAND (arg0
, 1);
10745 tree arg10
= TREE_OPERAND (arg1
, 0);
10746 tree arg11
= TREE_OPERAND (arg1
, 1);
10747 tree itype
= TREE_TYPE (arg0
);
10749 if (operand_equal_p (arg01
, arg11
, 0))
10751 tem
= fold_convert_loc (loc
, itype
, arg10
);
10752 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10753 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg01
);
10754 return fold_build2_loc (loc
, code
, type
, tem
,
10755 build_zero_cst (itype
));
10757 if (operand_equal_p (arg01
, arg10
, 0))
10759 tem
= fold_convert_loc (loc
, itype
, arg11
);
10760 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10761 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg01
);
10762 return fold_build2_loc (loc
, code
, type
, tem
,
10763 build_zero_cst (itype
));
10765 if (operand_equal_p (arg00
, arg11
, 0))
10767 tem
= fold_convert_loc (loc
, itype
, arg10
);
10768 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
, tem
);
10769 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg00
);
10770 return fold_build2_loc (loc
, code
, type
, tem
,
10771 build_zero_cst (itype
));
10773 if (operand_equal_p (arg00
, arg10
, 0))
10775 tem
= fold_convert_loc (loc
, itype
, arg11
);
10776 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
, tem
);
10777 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, tem
, arg00
);
10778 return fold_build2_loc (loc
, code
, type
, tem
,
10779 build_zero_cst (itype
));
10783 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10784 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10786 tree arg00
= TREE_OPERAND (arg0
, 0);
10787 tree arg01
= TREE_OPERAND (arg0
, 1);
10788 tree arg10
= TREE_OPERAND (arg1
, 0);
10789 tree arg11
= TREE_OPERAND (arg1
, 1);
10790 tree itype
= TREE_TYPE (arg0
);
10792 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10793 operand_equal_p guarantees no side-effects so we don't need
10794 to use omit_one_operand on Z. */
10795 if (operand_equal_p (arg01
, arg11
, 0))
10796 return fold_build2_loc (loc
, code
, type
, arg00
,
10797 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10799 if (operand_equal_p (arg01
, arg10
, 0))
10800 return fold_build2_loc (loc
, code
, type
, arg00
,
10801 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10803 if (operand_equal_p (arg00
, arg11
, 0))
10804 return fold_build2_loc (loc
, code
, type
, arg01
,
10805 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10807 if (operand_equal_p (arg00
, arg10
, 0))
10808 return fold_build2_loc (loc
, code
, type
, arg01
,
10809 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10812 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10813 if (TREE_CODE (arg01
) == INTEGER_CST
10814 && TREE_CODE (arg11
) == INTEGER_CST
)
10816 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10817 fold_convert_loc (loc
, itype
, arg11
));
10818 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10819 return fold_build2_loc (loc
, code
, type
, tem
,
10820 fold_convert_loc (loc
, itype
, arg10
));
10824 /* Attempt to simplify equality/inequality comparisons of complex
10825 values. Only lower the comparison if the result is known or
10826 can be simplified to a single scalar comparison. */
10827 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10828 || TREE_CODE (arg0
) == COMPLEX_CST
)
10829 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10830 || TREE_CODE (arg1
) == COMPLEX_CST
))
10832 tree real0
, imag0
, real1
, imag1
;
10835 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10837 real0
= TREE_OPERAND (arg0
, 0);
10838 imag0
= TREE_OPERAND (arg0
, 1);
10842 real0
= TREE_REALPART (arg0
);
10843 imag0
= TREE_IMAGPART (arg0
);
10846 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
10848 real1
= TREE_OPERAND (arg1
, 0);
10849 imag1
= TREE_OPERAND (arg1
, 1);
10853 real1
= TREE_REALPART (arg1
);
10854 imag1
= TREE_IMAGPART (arg1
);
10857 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
10858 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
10860 if (integer_zerop (rcond
))
10862 if (code
== EQ_EXPR
)
10863 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10865 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
10869 if (code
== NE_EXPR
)
10870 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10872 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
10876 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
10877 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
10879 if (integer_zerop (icond
))
10881 if (code
== EQ_EXPR
)
10882 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10884 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
10888 if (code
== NE_EXPR
)
10889 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10891 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
10902 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10903 if (tem
!= NULL_TREE
)
10906 /* Transform comparisons of the form X +- C CMP X. */
10907 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10908 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10909 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
10910 && !HONOR_SNANS (arg0
))
10911 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10912 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
10914 tree arg01
= TREE_OPERAND (arg0
, 1);
10915 enum tree_code code0
= TREE_CODE (arg0
);
10918 if (TREE_CODE (arg01
) == REAL_CST
)
10919 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
10921 is_positive
= tree_int_cst_sgn (arg01
);
10923 /* (X - c) > X becomes false. */
10924 if (code
== GT_EXPR
10925 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10926 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10928 if (TREE_CODE (arg01
) == INTEGER_CST
10929 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10930 fold_overflow_warning (("assuming signed overflow does not "
10931 "occur when assuming that (X - c) > X "
10932 "is always false"),
10933 WARN_STRICT_OVERFLOW_ALL
);
10934 return constant_boolean_node (0, type
);
10937 /* Likewise (X + c) < X becomes false. */
10938 if (code
== LT_EXPR
10939 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10940 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10942 if (TREE_CODE (arg01
) == INTEGER_CST
10943 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10944 fold_overflow_warning (("assuming signed overflow does not "
10945 "occur when assuming that "
10946 "(X + c) < X is always false"),
10947 WARN_STRICT_OVERFLOW_ALL
);
10948 return constant_boolean_node (0, type
);
10951 /* Convert (X - c) <= X to true. */
10952 if (!HONOR_NANS (arg1
)
10954 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10955 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10957 if (TREE_CODE (arg01
) == INTEGER_CST
10958 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10959 fold_overflow_warning (("assuming signed overflow does not "
10960 "occur when assuming that "
10961 "(X - c) <= X is always true"),
10962 WARN_STRICT_OVERFLOW_ALL
);
10963 return constant_boolean_node (1, type
);
10966 /* Convert (X + c) >= X to true. */
10967 if (!HONOR_NANS (arg1
)
10969 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10970 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10972 if (TREE_CODE (arg01
) == INTEGER_CST
10973 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10974 fold_overflow_warning (("assuming signed overflow does not "
10975 "occur when assuming that "
10976 "(X + c) >= X is always true"),
10977 WARN_STRICT_OVERFLOW_ALL
);
10978 return constant_boolean_node (1, type
);
10981 if (TREE_CODE (arg01
) == INTEGER_CST
)
10983 /* Convert X + c > X and X - c < X to true for integers. */
10984 if (code
== GT_EXPR
10985 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
10986 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
10988 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10989 fold_overflow_warning (("assuming signed overflow does "
10990 "not occur when assuming that "
10991 "(X + c) > X is always true"),
10992 WARN_STRICT_OVERFLOW_ALL
);
10993 return constant_boolean_node (1, type
);
10996 if (code
== LT_EXPR
10997 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
10998 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11000 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11001 fold_overflow_warning (("assuming signed overflow does "
11002 "not occur when assuming that "
11003 "(X - c) < X is always true"),
11004 WARN_STRICT_OVERFLOW_ALL
);
11005 return constant_boolean_node (1, type
);
11008 /* Convert X + c <= X and X - c >= X to false for integers. */
11009 if (code
== LE_EXPR
11010 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11011 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11013 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11014 fold_overflow_warning (("assuming signed overflow does "
11015 "not occur when assuming that "
11016 "(X + c) <= X is always false"),
11017 WARN_STRICT_OVERFLOW_ALL
);
11018 return constant_boolean_node (0, type
);
11021 if (code
== GE_EXPR
11022 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11023 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11025 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11026 fold_overflow_warning (("assuming signed overflow does "
11027 "not occur when assuming that "
11028 "(X - c) >= X is always false"),
11029 WARN_STRICT_OVERFLOW_ALL
);
11030 return constant_boolean_node (0, type
);
11035 /* If we are comparing an ABS_EXPR with a constant, we can
11036 convert all the cases into explicit comparisons, but they may
11037 well not be faster than doing the ABS and one comparison.
11038 But ABS (X) <= C is a range comparison, which becomes a subtraction
11039 and a comparison, and is probably faster. */
11040 if (code
== LE_EXPR
11041 && TREE_CODE (arg1
) == INTEGER_CST
11042 && TREE_CODE (arg0
) == ABS_EXPR
11043 && ! TREE_SIDE_EFFECTS (arg0
)
11044 && (0 != (tem
= negate_expr (arg1
)))
11045 && TREE_CODE (tem
) == INTEGER_CST
11046 && !TREE_OVERFLOW (tem
))
11047 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11048 build2 (GE_EXPR
, type
,
11049 TREE_OPERAND (arg0
, 0), tem
),
11050 build2 (LE_EXPR
, type
,
11051 TREE_OPERAND (arg0
, 0), arg1
));
11053 /* Convert ABS_EXPR<x> >= 0 to true. */
11054 strict_overflow_p
= false;
11055 if (code
== GE_EXPR
11056 && (integer_zerop (arg1
)
11057 || (! HONOR_NANS (arg0
)
11058 && real_zerop (arg1
)))
11059 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11061 if (strict_overflow_p
)
11062 fold_overflow_warning (("assuming signed overflow does not occur "
11063 "when simplifying comparison of "
11064 "absolute value and zero"),
11065 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11066 return omit_one_operand_loc (loc
, type
,
11067 constant_boolean_node (true, type
),
11071 /* Convert ABS_EXPR<x> < 0 to false. */
11072 strict_overflow_p
= false;
11073 if (code
== LT_EXPR
11074 && (integer_zerop (arg1
) || real_zerop (arg1
))
11075 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11077 if (strict_overflow_p
)
11078 fold_overflow_warning (("assuming signed overflow does not occur "
11079 "when simplifying comparison of "
11080 "absolute value and zero"),
11081 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11082 return omit_one_operand_loc (loc
, type
,
11083 constant_boolean_node (false, type
),
11087 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11088 and similarly for >= into !=. */
11089 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11090 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11091 && TREE_CODE (arg1
) == LSHIFT_EXPR
11092 && integer_onep (TREE_OPERAND (arg1
, 0)))
11093 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11094 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11095 TREE_OPERAND (arg1
, 1)),
11096 build_zero_cst (TREE_TYPE (arg0
)));
11098 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11099 otherwise Y might be >= # of bits in X's type and thus e.g.
11100 (unsigned char) (1 << Y) for Y 15 might be 0.
11101 If the cast is widening, then 1 << Y should have unsigned type,
11102 otherwise if Y is number of bits in the signed shift type minus 1,
11103 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11104 31 might be 0xffffffff80000000. */
11105 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11106 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11107 && CONVERT_EXPR_P (arg1
)
11108 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11109 && (element_precision (TREE_TYPE (arg1
))
11110 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11111 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11112 || (element_precision (TREE_TYPE (arg1
))
11113 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11114 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11116 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11117 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11118 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11119 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11120 build_zero_cst (TREE_TYPE (arg0
)));
11125 case UNORDERED_EXPR
:
11133 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11135 tree targ0
= strip_float_extensions (arg0
);
11136 tree targ1
= strip_float_extensions (arg1
);
11137 tree newtype
= TREE_TYPE (targ0
);
11139 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11140 newtype
= TREE_TYPE (targ1
);
11142 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11143 return fold_build2_loc (loc
, code
, type
,
11144 fold_convert_loc (loc
, newtype
, targ0
),
11145 fold_convert_loc (loc
, newtype
, targ1
));
11150 case COMPOUND_EXPR
:
11151 /* When pedantic, a compound expression can be neither an lvalue
11152 nor an integer constant expression. */
11153 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11155 /* Don't let (0, 0) be null pointer constant. */
11156 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11157 : fold_convert_loc (loc
, type
, arg1
);
11158 return pedantic_non_lvalue_loc (loc
, tem
);
11161 /* An ASSERT_EXPR should never be passed to fold_binary. */
11162 gcc_unreachable ();
11166 } /* switch (code) */
11169 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11170 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11174 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11176 switch (TREE_CODE (*tp
))
11182 *walk_subtrees
= 0;
11191 /* Return whether the sub-tree ST contains a label which is accessible from
11192 outside the sub-tree. */
11195 contains_label_p (tree st
)
11198 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11201 /* Fold a ternary expression of code CODE and type TYPE with operands
11202 OP0, OP1, and OP2. Return the folded expression if folding is
11203 successful. Otherwise, return NULL_TREE. */
11206 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11207 tree op0
, tree op1
, tree op2
)
11210 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11211 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11213 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11214 && TREE_CODE_LENGTH (code
) == 3);
11216 /* If this is a commutative operation, and OP0 is a constant, move it
11217 to OP1 to reduce the number of tests below. */
11218 if (commutative_ternary_tree_code (code
)
11219 && tree_swap_operands_p (op0
, op1
))
11220 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11222 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11226 /* Strip any conversions that don't change the mode. This is safe
11227 for every expression, except for a comparison expression because
11228 its signedness is derived from its operands. So, in the latter
11229 case, only strip conversions that don't change the signedness.
11231 Note that this is done as an internal manipulation within the
11232 constant folder, in order to find the simplest representation of
11233 the arguments so that their form can be studied. In any cases,
11234 the appropriate type conversions should be put back in the tree
11235 that will get out of the constant folder. */
11256 case COMPONENT_REF
:
11257 if (TREE_CODE (arg0
) == CONSTRUCTOR
11258 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11260 unsigned HOST_WIDE_INT idx
;
11262 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11269 case VEC_COND_EXPR
:
11270 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11271 so all simple results must be passed through pedantic_non_lvalue. */
11272 if (TREE_CODE (arg0
) == INTEGER_CST
)
11274 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11275 tem
= integer_zerop (arg0
) ? op2
: op1
;
11276 /* Only optimize constant conditions when the selected branch
11277 has the same type as the COND_EXPR. This avoids optimizing
11278 away "c ? x : throw", where the throw has a void type.
11279 Avoid throwing away that operand which contains label. */
11280 if ((!TREE_SIDE_EFFECTS (unused_op
)
11281 || !contains_label_p (unused_op
))
11282 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11283 || VOID_TYPE_P (type
)))
11284 return pedantic_non_lvalue_loc (loc
, tem
);
11287 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11289 if ((TREE_CODE (arg1
) == VECTOR_CST
11290 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11291 && (TREE_CODE (arg2
) == VECTOR_CST
11292 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11294 unsigned int nelts
= VECTOR_CST_NELTS (arg0
), i
;
11295 gcc_assert (nelts
== TYPE_VECTOR_SUBPARTS (type
));
11296 auto_vec_perm_indices
sel (nelts
);
11297 for (i
= 0; i
< nelts
; i
++)
11299 tree val
= VECTOR_CST_ELT (arg0
, i
);
11300 if (integer_all_onesp (val
))
11301 sel
.quick_push (i
);
11302 else if (integer_zerop (val
))
11303 sel
.quick_push (nelts
+ i
);
11304 else /* Currently unreachable. */
11307 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11308 if (t
!= NULL_TREE
)
11313 /* If we have A op B ? A : C, we may be able to convert this to a
11314 simpler expression, depending on the operation and the values
11315 of B and C. Signed zeros prevent all of these transformations,
11316 for reasons given above each one.
11318 Also try swapping the arguments and inverting the conditional. */
11319 if (COMPARISON_CLASS_P (arg0
)
11320 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), arg1
)
11321 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11323 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11328 if (COMPARISON_CLASS_P (arg0
)
11329 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0), op2
)
11330 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11332 location_t loc0
= expr_location_or (arg0
, loc
);
11333 tem
= fold_invert_truthvalue (loc0
, arg0
);
11334 if (tem
&& COMPARISON_CLASS_P (tem
))
11336 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11342 /* If the second operand is simpler than the third, swap them
11343 since that produces better jump optimization results. */
11344 if (truth_value_p (TREE_CODE (arg0
))
11345 && tree_swap_operands_p (op1
, op2
))
11347 location_t loc0
= expr_location_or (arg0
, loc
);
11348 /* See if this can be inverted. If it can't, possibly because
11349 it was a floating-point inequality comparison, don't do
11351 tem
= fold_invert_truthvalue (loc0
, arg0
);
11353 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11356 /* Convert A ? 1 : 0 to simply A. */
11357 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11358 : (integer_onep (op1
)
11359 && !VECTOR_TYPE_P (type
)))
11360 && integer_zerop (op2
)
11361 /* If we try to convert OP0 to our type, the
11362 call to fold will try to move the conversion inside
11363 a COND, which will recurse. In that case, the COND_EXPR
11364 is probably the best choice, so leave it alone. */
11365 && type
== TREE_TYPE (arg0
))
11366 return pedantic_non_lvalue_loc (loc
, arg0
);
11368 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11369 over COND_EXPR in cases such as floating point comparisons. */
11370 if (integer_zerop (op1
)
11371 && code
== COND_EXPR
11372 && integer_onep (op2
)
11373 && !VECTOR_TYPE_P (type
)
11374 && truth_value_p (TREE_CODE (arg0
)))
11375 return pedantic_non_lvalue_loc (loc
,
11376 fold_convert_loc (loc
, type
,
11377 invert_truthvalue_loc (loc
,
11380 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11381 if (TREE_CODE (arg0
) == LT_EXPR
11382 && integer_zerop (TREE_OPERAND (arg0
, 1))
11383 && integer_zerop (op2
)
11384 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11386 /* sign_bit_p looks through both zero and sign extensions,
11387 but for this optimization only sign extensions are
11389 tree tem2
= TREE_OPERAND (arg0
, 0);
11390 while (tem
!= tem2
)
11392 if (TREE_CODE (tem2
) != NOP_EXPR
11393 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11398 tem2
= TREE_OPERAND (tem2
, 0);
11400 /* sign_bit_p only checks ARG1 bits within A's precision.
11401 If <sign bit of A> has wider type than A, bits outside
11402 of A's precision in <sign bit of A> need to be checked.
11403 If they are all 0, this optimization needs to be done
11404 in unsigned A's type, if they are all 1 in signed A's type,
11405 otherwise this can't be done. */
11407 && TYPE_PRECISION (TREE_TYPE (tem
))
11408 < TYPE_PRECISION (TREE_TYPE (arg1
))
11409 && TYPE_PRECISION (TREE_TYPE (tem
))
11410 < TYPE_PRECISION (type
))
11412 int inner_width
, outer_width
;
11415 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11416 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11417 if (outer_width
> TYPE_PRECISION (type
))
11418 outer_width
= TYPE_PRECISION (type
);
11420 wide_int mask
= wi::shifted_mask
11421 (inner_width
, outer_width
- inner_width
, false,
11422 TYPE_PRECISION (TREE_TYPE (arg1
)));
11424 wide_int common
= mask
& arg1
;
11425 if (common
== mask
)
11427 tem_type
= signed_type_for (TREE_TYPE (tem
));
11428 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11430 else if (common
== 0)
11432 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11433 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11441 fold_convert_loc (loc
, type
,
11442 fold_build2_loc (loc
, BIT_AND_EXPR
,
11443 TREE_TYPE (tem
), tem
,
11444 fold_convert_loc (loc
,
11449 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11450 already handled above. */
11451 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11452 && integer_onep (TREE_OPERAND (arg0
, 1))
11453 && integer_zerop (op2
)
11454 && integer_pow2p (arg1
))
11456 tree tem
= TREE_OPERAND (arg0
, 0);
11458 if (TREE_CODE (tem
) == RSHIFT_EXPR
11459 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11460 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
11461 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11462 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11463 fold_convert_loc (loc
, type
,
11464 TREE_OPERAND (tem
, 0)),
11468 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11469 is probably obsolete because the first operand should be a
11470 truth value (that's why we have the two cases above), but let's
11471 leave it in until we can confirm this for all front-ends. */
11472 if (integer_zerop (op2
)
11473 && TREE_CODE (arg0
) == NE_EXPR
11474 && integer_zerop (TREE_OPERAND (arg0
, 1))
11475 && integer_pow2p (arg1
)
11476 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11477 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11478 arg1
, OEP_ONLY_CONST
))
11479 return pedantic_non_lvalue_loc (loc
,
11480 fold_convert_loc (loc
, type
,
11481 TREE_OPERAND (arg0
, 0)));
11483 /* Disable the transformations below for vectors, since
11484 fold_binary_op_with_conditional_arg may undo them immediately,
11485 yielding an infinite loop. */
11486 if (code
== VEC_COND_EXPR
)
11489 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11490 if (integer_zerop (op2
)
11491 && truth_value_p (TREE_CODE (arg0
))
11492 && truth_value_p (TREE_CODE (arg1
))
11493 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11494 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11495 : TRUTH_ANDIF_EXPR
,
11496 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
11498 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11499 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11500 && truth_value_p (TREE_CODE (arg0
))
11501 && truth_value_p (TREE_CODE (arg1
))
11502 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11504 location_t loc0
= expr_location_or (arg0
, loc
);
11505 /* Only perform transformation if ARG0 is easily inverted. */
11506 tem
= fold_invert_truthvalue (loc0
, arg0
);
11508 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11511 type
, fold_convert_loc (loc
, type
, tem
),
11515 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11516 if (integer_zerop (arg1
)
11517 && truth_value_p (TREE_CODE (arg0
))
11518 && truth_value_p (TREE_CODE (op2
))
11519 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11521 location_t loc0
= expr_location_or (arg0
, loc
);
11522 /* Only perform transformation if ARG0 is easily inverted. */
11523 tem
= fold_invert_truthvalue (loc0
, arg0
);
11525 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11526 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11527 type
, fold_convert_loc (loc
, type
, tem
),
11531 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11532 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11533 && truth_value_p (TREE_CODE (arg0
))
11534 && truth_value_p (TREE_CODE (op2
))
11535 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11536 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11537 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11538 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11543 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11544 of fold_ternary on them. */
11545 gcc_unreachable ();
11547 case BIT_FIELD_REF
:
11548 if (TREE_CODE (arg0
) == VECTOR_CST
11549 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11550 || (TREE_CODE (type
) == VECTOR_TYPE
11551 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11553 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11554 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11555 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11556 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11559 && (idx
% width
) == 0
11560 && (n
% width
) == 0
11561 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11566 if (TREE_CODE (arg0
) == VECTOR_CST
)
11569 return VECTOR_CST_ELT (arg0
, idx
);
11571 auto_vec
<tree
, 32> vals (n
);
11572 for (unsigned i
= 0; i
< n
; ++i
)
11573 vals
.quick_push (VECTOR_CST_ELT (arg0
, idx
+ i
));
11574 return build_vector (type
, vals
);
11579 /* On constants we can use native encode/interpret to constant
11580 fold (nearly) all BIT_FIELD_REFs. */
11581 if (CONSTANT_CLASS_P (arg0
)
11582 && can_native_interpret_type_p (type
)
11583 && BITS_PER_UNIT
== 8)
11585 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11586 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11587 /* Limit us to a reasonable amount of work. To relax the
11588 other limitations we need bit-shifting of the buffer
11589 and rounding up the size. */
11590 if (bitpos
% BITS_PER_UNIT
== 0
11591 && bitsize
% BITS_PER_UNIT
== 0
11592 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
11594 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
11595 unsigned HOST_WIDE_INT len
11596 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
11597 bitpos
/ BITS_PER_UNIT
);
11599 && len
* BITS_PER_UNIT
>= bitsize
)
11601 tree v
= native_interpret_expr (type
, b
,
11602 bitsize
/ BITS_PER_UNIT
);
11612 /* For integers we can decompose the FMA if possible. */
11613 if (TREE_CODE (arg0
) == INTEGER_CST
11614 && TREE_CODE (arg1
) == INTEGER_CST
)
11615 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11616 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11617 if (integer_zerop (arg2
))
11618 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11620 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11622 case VEC_PERM_EXPR
:
11623 if (TREE_CODE (arg2
) == VECTOR_CST
)
11625 unsigned int nelts
= VECTOR_CST_NELTS (arg2
), i
, mask
, mask2
;
11626 bool need_mask_canon
= false;
11627 bool need_mask_canon2
= false;
11628 bool all_in_vec0
= true;
11629 bool all_in_vec1
= true;
11630 bool maybe_identity
= true;
11631 bool single_arg
= (op0
== op1
);
11632 bool changed
= false;
11634 mask2
= 2 * nelts
- 1;
11635 mask
= single_arg
? (nelts
- 1) : mask2
;
11636 gcc_assert (nelts
== TYPE_VECTOR_SUBPARTS (type
));
11637 auto_vec_perm_indices
sel (nelts
);
11638 auto_vec_perm_indices
sel2 (nelts
);
11639 for (i
= 0; i
< nelts
; i
++)
11641 tree val
= VECTOR_CST_ELT (arg2
, i
);
11642 if (TREE_CODE (val
) != INTEGER_CST
)
11645 /* Make sure that the perm value is in an acceptable
11648 need_mask_canon
|= wi::gtu_p (t
, mask
);
11649 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11650 unsigned int elt
= t
.to_uhwi () & mask
;
11651 unsigned int elt2
= t
.to_uhwi () & mask2
;
11654 all_in_vec1
= false;
11656 all_in_vec0
= false;
11658 if ((elt
& (nelts
- 1)) != i
)
11659 maybe_identity
= false;
11661 sel
.quick_push (elt
);
11662 sel2
.quick_push (elt2
);
11665 if (maybe_identity
)
11675 else if (all_in_vec1
)
11678 for (i
= 0; i
< nelts
; i
++)
11680 need_mask_canon
= true;
11683 if ((TREE_CODE (op0
) == VECTOR_CST
11684 || TREE_CODE (op0
) == CONSTRUCTOR
)
11685 && (TREE_CODE (op1
) == VECTOR_CST
11686 || TREE_CODE (op1
) == CONSTRUCTOR
))
11688 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11689 if (t
!= NULL_TREE
)
11693 if (op0
== op1
&& !single_arg
)
11696 /* Some targets are deficient and fail to expand a single
11697 argument permutation while still allowing an equivalent
11698 2-argument version. */
11699 if (need_mask_canon
&& arg2
== op2
11700 && !can_vec_perm_p (TYPE_MODE (type
), false, &sel
)
11701 && can_vec_perm_p (TYPE_MODE (type
), false, &sel2
))
11703 need_mask_canon
= need_mask_canon2
;
11707 if (need_mask_canon
&& arg2
== op2
)
11709 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11710 auto_vec
<tree
, 32> tsel (nelts
);
11711 for (i
= 0; i
< nelts
; i
++)
11712 tsel
.quick_push (build_int_cst (eltype
, sel
[i
]));
11713 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11718 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11722 case BIT_INSERT_EXPR
:
11723 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11724 if (TREE_CODE (arg0
) == INTEGER_CST
11725 && TREE_CODE (arg1
) == INTEGER_CST
)
11727 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11728 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
11729 wide_int tem
= wi::bit_and (arg0
,
11730 wi::shifted_mask (bitpos
, bitsize
, true,
11731 TYPE_PRECISION (type
)));
11733 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
11735 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
11737 else if (TREE_CODE (arg0
) == VECTOR_CST
11738 && CONSTANT_CLASS_P (arg1
)
11739 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
11742 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11743 unsigned HOST_WIDE_INT elsize
11744 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
11745 if (bitpos
% elsize
== 0)
11747 unsigned k
= bitpos
/ elsize
;
11748 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
11752 unsigned int nelts
= VECTOR_CST_NELTS (arg0
);
11753 auto_vec
<tree
, 32> elts (nelts
);
11754 elts
.quick_grow (nelts
);
11755 memcpy (&elts
[0], VECTOR_CST_ELTS (arg0
),
11756 sizeof (tree
) * nelts
);
11758 return build_vector (type
, elts
);
11766 } /* switch (code) */
11769 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11770 of an array (or vector). */
11773 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
11775 tree index_type
= NULL_TREE
;
11776 offset_int low_bound
= 0;
11778 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
11780 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
11781 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
11783 /* Static constructors for variably sized objects makes no sense. */
11784 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
11785 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
11786 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
11791 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
11792 TYPE_SIGN (index_type
));
11794 offset_int index
= low_bound
- 1;
11796 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11797 TYPE_SIGN (index_type
));
11799 offset_int max_index
;
11800 unsigned HOST_WIDE_INT cnt
;
11803 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
11805 /* Array constructor might explicitly set index, or specify a range,
11806 or leave index NULL meaning that it is next index after previous
11810 if (TREE_CODE (cfield
) == INTEGER_CST
)
11811 max_index
= index
= wi::to_offset (cfield
);
11814 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
11815 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
11816 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
11823 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11824 TYPE_SIGN (index_type
));
11828 /* Do we have match? */
11829 if (wi::cmpu (access_index
, index
) >= 0
11830 && wi::cmpu (access_index
, max_index
) <= 0)
11836 /* Perform constant folding and related simplification of EXPR.
11837 The related simplifications include x*1 => x, x*0 => 0, etc.,
11838 and application of the associative law.
11839 NOP_EXPR conversions may be removed freely (as long as we
11840 are careful not to change the type of the overall expression).
11841 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11842 but we can constant-fold them if they have constant operands. */
11844 #ifdef ENABLE_FOLD_CHECKING
11845 # define fold(x) fold_1 (x)
11846 static tree
fold_1 (tree
);
11852 const tree t
= expr
;
11853 enum tree_code code
= TREE_CODE (t
);
11854 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11856 location_t loc
= EXPR_LOCATION (expr
);
11858 /* Return right away if a constant. */
11859 if (kind
== tcc_constant
)
11862 /* CALL_EXPR-like objects with variable numbers of operands are
11863 treated specially. */
11864 if (kind
== tcc_vl_exp
)
11866 if (code
== CALL_EXPR
)
11868 tem
= fold_call_expr (loc
, expr
, false);
11869 return tem
? tem
: expr
;
11874 if (IS_EXPR_CODE_CLASS (kind
))
11876 tree type
= TREE_TYPE (t
);
11877 tree op0
, op1
, op2
;
11879 switch (TREE_CODE_LENGTH (code
))
11882 op0
= TREE_OPERAND (t
, 0);
11883 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11884 return tem
? tem
: expr
;
11886 op0
= TREE_OPERAND (t
, 0);
11887 op1
= TREE_OPERAND (t
, 1);
11888 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11889 return tem
? tem
: expr
;
11891 op0
= TREE_OPERAND (t
, 0);
11892 op1
= TREE_OPERAND (t
, 1);
11893 op2
= TREE_OPERAND (t
, 2);
11894 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11895 return tem
? tem
: expr
;
11905 tree op0
= TREE_OPERAND (t
, 0);
11906 tree op1
= TREE_OPERAND (t
, 1);
11908 if (TREE_CODE (op1
) == INTEGER_CST
11909 && TREE_CODE (op0
) == CONSTRUCTOR
11910 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11912 tree val
= get_array_ctor_element_at_index (op0
,
11913 wi::to_offset (op1
));
11921 /* Return a VECTOR_CST if possible. */
11924 tree type
= TREE_TYPE (t
);
11925 if (TREE_CODE (type
) != VECTOR_TYPE
)
11930 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
11931 if (! CONSTANT_CLASS_P (val
))
11934 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
11938 return fold (DECL_INITIAL (t
));
11942 } /* switch (code) */
11945 #ifdef ENABLE_FOLD_CHECKING
11948 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
11949 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
11950 static void fold_check_failed (const_tree
, const_tree
);
11951 void print_fold_checksum (const_tree
);
11953 /* When --enable-checking=fold, compute a digest of expr before
11954 and after actual fold call to see if fold did not accidentally
11955 change original expr. */
11961 struct md5_ctx ctx
;
11962 unsigned char checksum_before
[16], checksum_after
[16];
11963 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
11965 md5_init_ctx (&ctx
);
11966 fold_checksum_tree (expr
, &ctx
, &ht
);
11967 md5_finish_ctx (&ctx
, checksum_before
);
11970 ret
= fold_1 (expr
);
11972 md5_init_ctx (&ctx
);
11973 fold_checksum_tree (expr
, &ctx
, &ht
);
11974 md5_finish_ctx (&ctx
, checksum_after
);
11976 if (memcmp (checksum_before
, checksum_after
, 16))
11977 fold_check_failed (expr
, ret
);
11983 print_fold_checksum (const_tree expr
)
11985 struct md5_ctx ctx
;
11986 unsigned char checksum
[16], cnt
;
11987 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
11989 md5_init_ctx (&ctx
);
11990 fold_checksum_tree (expr
, &ctx
, &ht
);
11991 md5_finish_ctx (&ctx
, checksum
);
11992 for (cnt
= 0; cnt
< 16; ++cnt
)
11993 fprintf (stderr
, "%02x", checksum
[cnt
]);
11994 putc ('\n', stderr
);
11998 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12000 internal_error ("fold check: original tree changed by fold");
12004 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12005 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12007 const tree_node
**slot
;
12008 enum tree_code code
;
12009 union tree_node buf
;
12015 slot
= ht
->find_slot (expr
, INSERT
);
12019 code
= TREE_CODE (expr
);
12020 if (TREE_CODE_CLASS (code
) == tcc_declaration
12021 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12023 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12024 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12025 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12026 buf
.decl_with_vis
.symtab_node
= NULL
;
12027 expr
= (tree
) &buf
;
12029 else if (TREE_CODE_CLASS (code
) == tcc_type
12030 && (TYPE_POINTER_TO (expr
)
12031 || TYPE_REFERENCE_TO (expr
)
12032 || TYPE_CACHED_VALUES_P (expr
)
12033 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12034 || TYPE_NEXT_VARIANT (expr
)
12035 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
12037 /* Allow these fields to be modified. */
12039 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12040 expr
= tmp
= (tree
) &buf
;
12041 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12042 TYPE_POINTER_TO (tmp
) = NULL
;
12043 TYPE_REFERENCE_TO (tmp
) = NULL
;
12044 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12045 TYPE_ALIAS_SET (tmp
) = -1;
12046 if (TYPE_CACHED_VALUES_P (tmp
))
12048 TYPE_CACHED_VALUES_P (tmp
) = 0;
12049 TYPE_CACHED_VALUES (tmp
) = NULL
;
12052 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12053 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12054 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12055 if (TREE_CODE_CLASS (code
) != tcc_type
12056 && TREE_CODE_CLASS (code
) != tcc_declaration
12057 && code
!= TREE_LIST
12058 && code
!= SSA_NAME
12059 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12060 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12061 switch (TREE_CODE_CLASS (code
))
12067 md5_process_bytes (TREE_STRING_POINTER (expr
),
12068 TREE_STRING_LENGTH (expr
), ctx
);
12071 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12072 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12075 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12076 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12082 case tcc_exceptional
:
12086 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12087 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12088 expr
= TREE_CHAIN (expr
);
12089 goto recursive_label
;
12092 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12093 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12099 case tcc_expression
:
12100 case tcc_reference
:
12101 case tcc_comparison
:
12104 case tcc_statement
:
12106 len
= TREE_OPERAND_LENGTH (expr
);
12107 for (i
= 0; i
< len
; ++i
)
12108 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12110 case tcc_declaration
:
12111 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12112 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12113 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12115 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12116 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12117 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12118 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12119 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12122 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12124 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12126 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12127 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12129 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12133 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12134 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12135 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12136 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12137 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12138 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12139 if (INTEGRAL_TYPE_P (expr
)
12140 || SCALAR_FLOAT_TYPE_P (expr
))
12142 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12143 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12145 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12146 if (TREE_CODE (expr
) == RECORD_TYPE
12147 || TREE_CODE (expr
) == UNION_TYPE
12148 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12149 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12150 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12157 /* Helper function for outputting the checksum of a tree T. When
12158 debugging with gdb, you can "define mynext" to be "next" followed
12159 by "call debug_fold_checksum (op0)", then just trace down till the
12162 DEBUG_FUNCTION
void
12163 debug_fold_checksum (const_tree t
)
12166 unsigned char checksum
[16];
12167 struct md5_ctx ctx
;
12168 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12170 md5_init_ctx (&ctx
);
12171 fold_checksum_tree (t
, &ctx
, &ht
);
12172 md5_finish_ctx (&ctx
, checksum
);
12175 for (i
= 0; i
< 16; i
++)
12176 fprintf (stderr
, "%d ", checksum
[i
]);
12178 fprintf (stderr
, "\n");
12183 /* Fold a unary tree expression with code CODE of type TYPE with an
12184 operand OP0. LOC is the location of the resulting expression.
12185 Return a folded expression if successful. Otherwise, return a tree
12186 expression with code CODE of type TYPE with an operand OP0. */
12189 fold_build1_loc (location_t loc
,
12190 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12193 #ifdef ENABLE_FOLD_CHECKING
12194 unsigned char checksum_before
[16], checksum_after
[16];
12195 struct md5_ctx ctx
;
12196 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12198 md5_init_ctx (&ctx
);
12199 fold_checksum_tree (op0
, &ctx
, &ht
);
12200 md5_finish_ctx (&ctx
, checksum_before
);
12204 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12206 tem
= build1_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12208 #ifdef ENABLE_FOLD_CHECKING
12209 md5_init_ctx (&ctx
);
12210 fold_checksum_tree (op0
, &ctx
, &ht
);
12211 md5_finish_ctx (&ctx
, checksum_after
);
12213 if (memcmp (checksum_before
, checksum_after
, 16))
12214 fold_check_failed (op0
, tem
);
12219 /* Fold a binary tree expression with code CODE of type TYPE with
12220 operands OP0 and OP1. LOC is the location of the resulting
12221 expression. Return a folded expression if successful. Otherwise,
12222 return a tree expression with code CODE of type TYPE with operands
12226 fold_build2_loc (location_t loc
,
12227 enum tree_code code
, tree type
, tree op0
, tree op1
12231 #ifdef ENABLE_FOLD_CHECKING
12232 unsigned char checksum_before_op0
[16],
12233 checksum_before_op1
[16],
12234 checksum_after_op0
[16],
12235 checksum_after_op1
[16];
12236 struct md5_ctx ctx
;
12237 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12239 md5_init_ctx (&ctx
);
12240 fold_checksum_tree (op0
, &ctx
, &ht
);
12241 md5_finish_ctx (&ctx
, checksum_before_op0
);
12244 md5_init_ctx (&ctx
);
12245 fold_checksum_tree (op1
, &ctx
, &ht
);
12246 md5_finish_ctx (&ctx
, checksum_before_op1
);
12250 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12252 tem
= build2_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12254 #ifdef ENABLE_FOLD_CHECKING
12255 md5_init_ctx (&ctx
);
12256 fold_checksum_tree (op0
, &ctx
, &ht
);
12257 md5_finish_ctx (&ctx
, checksum_after_op0
);
12260 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12261 fold_check_failed (op0
, tem
);
12263 md5_init_ctx (&ctx
);
12264 fold_checksum_tree (op1
, &ctx
, &ht
);
12265 md5_finish_ctx (&ctx
, checksum_after_op1
);
12267 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12268 fold_check_failed (op1
, tem
);
12273 /* Fold a ternary tree expression with code CODE of type TYPE with
12274 operands OP0, OP1, and OP2. Return a folded expression if
12275 successful. Otherwise, return a tree expression with code CODE of
12276 type TYPE with operands OP0, OP1, and OP2. */
12279 fold_build3_loc (location_t loc
, enum tree_code code
, tree type
,
12280 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12283 #ifdef ENABLE_FOLD_CHECKING
12284 unsigned char checksum_before_op0
[16],
12285 checksum_before_op1
[16],
12286 checksum_before_op2
[16],
12287 checksum_after_op0
[16],
12288 checksum_after_op1
[16],
12289 checksum_after_op2
[16];
12290 struct md5_ctx ctx
;
12291 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12293 md5_init_ctx (&ctx
);
12294 fold_checksum_tree (op0
, &ctx
, &ht
);
12295 md5_finish_ctx (&ctx
, checksum_before_op0
);
12298 md5_init_ctx (&ctx
);
12299 fold_checksum_tree (op1
, &ctx
, &ht
);
12300 md5_finish_ctx (&ctx
, checksum_before_op1
);
12303 md5_init_ctx (&ctx
);
12304 fold_checksum_tree (op2
, &ctx
, &ht
);
12305 md5_finish_ctx (&ctx
, checksum_before_op2
);
12309 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12310 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12312 tem
= build3_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12314 #ifdef ENABLE_FOLD_CHECKING
12315 md5_init_ctx (&ctx
);
12316 fold_checksum_tree (op0
, &ctx
, &ht
);
12317 md5_finish_ctx (&ctx
, checksum_after_op0
);
12320 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12321 fold_check_failed (op0
, tem
);
12323 md5_init_ctx (&ctx
);
12324 fold_checksum_tree (op1
, &ctx
, &ht
);
12325 md5_finish_ctx (&ctx
, checksum_after_op1
);
12328 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12329 fold_check_failed (op1
, tem
);
12331 md5_init_ctx (&ctx
);
12332 fold_checksum_tree (op2
, &ctx
, &ht
);
12333 md5_finish_ctx (&ctx
, checksum_after_op2
);
12335 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12336 fold_check_failed (op2
, tem
);
12341 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12342 arguments in ARGARRAY, and a null static chain.
12343 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12344 of type TYPE from the given operands as constructed by build_call_array. */
12347 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12348 int nargs
, tree
*argarray
)
12351 #ifdef ENABLE_FOLD_CHECKING
12352 unsigned char checksum_before_fn
[16],
12353 checksum_before_arglist
[16],
12354 checksum_after_fn
[16],
12355 checksum_after_arglist
[16];
12356 struct md5_ctx ctx
;
12357 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12360 md5_init_ctx (&ctx
);
12361 fold_checksum_tree (fn
, &ctx
, &ht
);
12362 md5_finish_ctx (&ctx
, checksum_before_fn
);
12365 md5_init_ctx (&ctx
);
12366 for (i
= 0; i
< nargs
; i
++)
12367 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12368 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12372 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12374 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12376 #ifdef ENABLE_FOLD_CHECKING
12377 md5_init_ctx (&ctx
);
12378 fold_checksum_tree (fn
, &ctx
, &ht
);
12379 md5_finish_ctx (&ctx
, checksum_after_fn
);
12382 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12383 fold_check_failed (fn
, tem
);
12385 md5_init_ctx (&ctx
);
12386 for (i
= 0; i
< nargs
; i
++)
12387 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12388 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12390 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12391 fold_check_failed (NULL_TREE
, tem
);
12396 /* Perform constant folding and related simplification of initializer
12397 expression EXPR. These behave identically to "fold_buildN" but ignore
12398 potential run-time traps and exceptions that fold must preserve. */
12400 #define START_FOLD_INIT \
12401 int saved_signaling_nans = flag_signaling_nans;\
12402 int saved_trapping_math = flag_trapping_math;\
12403 int saved_rounding_math = flag_rounding_math;\
12404 int saved_trapv = flag_trapv;\
12405 int saved_folding_initializer = folding_initializer;\
12406 flag_signaling_nans = 0;\
12407 flag_trapping_math = 0;\
12408 flag_rounding_math = 0;\
12410 folding_initializer = 1;
12412 #define END_FOLD_INIT \
12413 flag_signaling_nans = saved_signaling_nans;\
12414 flag_trapping_math = saved_trapping_math;\
12415 flag_rounding_math = saved_rounding_math;\
12416 flag_trapv = saved_trapv;\
12417 folding_initializer = saved_folding_initializer;
12420 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12421 tree type
, tree op
)
12426 result
= fold_build1_loc (loc
, code
, type
, op
);
12433 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12434 tree type
, tree op0
, tree op1
)
12439 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12446 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12447 int nargs
, tree
*argarray
)
12452 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12458 #undef START_FOLD_INIT
12459 #undef END_FOLD_INIT
12461 /* Determine if first argument is a multiple of second argument. Return 0 if
12462 it is not, or we cannot easily determined it to be.
12464 An example of the sort of thing we care about (at this point; this routine
12465 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12466 fold cases do now) is discovering that
12468 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12474 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12476 This code also handles discovering that
12478 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12480 is a multiple of 8 so we don't have to worry about dealing with a
12481 possible remainder.
12483 Note that we *look* inside a SAVE_EXPR only to determine how it was
12484 calculated; it is not safe for fold to do much of anything else with the
12485 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12486 at run time. For example, the latter example above *cannot* be implemented
12487 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12488 evaluation time of the original SAVE_EXPR is not necessarily the same at
12489 the time the new expression is evaluated. The only optimization of this
12490 sort that would be valid is changing
12492 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12496 SAVE_EXPR (I) * SAVE_EXPR (J)
12498 (where the same SAVE_EXPR (J) is used in the original and the
12499 transformed version). */
12502 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12507 if (operand_equal_p (top
, bottom
, 0))
12510 if (TREE_CODE (type
) != INTEGER_TYPE
)
12513 switch (TREE_CODE (top
))
12516 /* Bitwise and provides a power of two multiple. If the mask is
12517 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12518 if (!integer_pow2p (bottom
))
12523 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12524 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12527 /* It is impossible to prove if op0 - op1 is multiple of bottom
12528 precisely, so be conservative here checking if both op0 and op1
12529 are multiple of bottom. Note we check the second operand first
12530 since it's usually simpler. */
12531 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12532 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12535 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12536 as op0 - 3 if the expression has unsigned type. For example,
12537 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12538 op1
= TREE_OPERAND (top
, 1);
12539 if (TYPE_UNSIGNED (type
)
12540 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
12541 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
12542 return (multiple_of_p (type
, op1
, bottom
)
12543 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12546 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12548 op1
= TREE_OPERAND (top
, 1);
12549 /* const_binop may not detect overflow correctly,
12550 so check for it explicitly here. */
12551 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12552 && 0 != (t1
= fold_convert (type
,
12553 const_binop (LSHIFT_EXPR
,
12556 && !TREE_OVERFLOW (t1
))
12557 return multiple_of_p (type
, t1
, bottom
);
12562 /* Can't handle conversions from non-integral or wider integral type. */
12563 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12564 || (TYPE_PRECISION (type
)
12565 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12571 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12574 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12575 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12578 if (TREE_CODE (bottom
) != INTEGER_CST
12579 || integer_zerop (bottom
)
12580 || (TYPE_UNSIGNED (type
)
12581 && (tree_int_cst_sgn (top
) < 0
12582 || tree_int_cst_sgn (bottom
) < 0)))
12584 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12588 if (TREE_CODE (bottom
) == INTEGER_CST
12589 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
12590 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
12592 enum tree_code code
= gimple_assign_rhs_code (stmt
);
12594 /* Check for special cases to see if top is defined as multiple
12597 top = (X & ~(bottom - 1) ; bottom is power of 2
12603 if (code
== BIT_AND_EXPR
12604 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12605 && TREE_CODE (op2
) == INTEGER_CST
12606 && integer_pow2p (bottom
)
12607 && wi::multiple_of_p (wi::to_widest (op2
),
12608 wi::to_widest (bottom
), UNSIGNED
))
12611 op1
= gimple_assign_rhs1 (stmt
);
12612 if (code
== MINUS_EXPR
12613 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12614 && TREE_CODE (op2
) == SSA_NAME
12615 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
12616 && gimple_code (stmt
) == GIMPLE_ASSIGN
12617 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
12618 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
12619 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
12630 #define tree_expr_nonnegative_warnv_p(X, Y) \
12631 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12633 #define RECURSE(X) \
12634 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12636 /* Return true if CODE or TYPE is known to be non-negative. */
12639 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12641 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12642 && truth_value_p (code
))
12643 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12644 have a signed:1 type (where the value is -1 and 0). */
12649 /* Return true if (CODE OP0) is known to be non-negative. If the return
12650 value is based on the assumption that signed overflow is undefined,
12651 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12652 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12655 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12656 bool *strict_overflow_p
, int depth
)
12658 if (TYPE_UNSIGNED (type
))
12664 /* We can't return 1 if flag_wrapv is set because
12665 ABS_EXPR<INT_MIN> = INT_MIN. */
12666 if (!ANY_INTEGRAL_TYPE_P (type
))
12668 if (TYPE_OVERFLOW_UNDEFINED (type
))
12670 *strict_overflow_p
= true;
12675 case NON_LVALUE_EXPR
:
12677 case FIX_TRUNC_EXPR
:
12678 return RECURSE (op0
);
12682 tree inner_type
= TREE_TYPE (op0
);
12683 tree outer_type
= type
;
12685 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12687 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12688 return RECURSE (op0
);
12689 if (INTEGRAL_TYPE_P (inner_type
))
12691 if (TYPE_UNSIGNED (inner_type
))
12693 return RECURSE (op0
);
12696 else if (INTEGRAL_TYPE_P (outer_type
))
12698 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12699 return RECURSE (op0
);
12700 if (INTEGRAL_TYPE_P (inner_type
))
12701 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12702 && TYPE_UNSIGNED (inner_type
);
12708 return tree_simple_nonnegative_warnv_p (code
, type
);
12711 /* We don't know sign of `t', so be conservative and return false. */
12715 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12716 value is based on the assumption that signed overflow is undefined,
12717 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12718 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12721 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12722 tree op1
, bool *strict_overflow_p
,
12725 if (TYPE_UNSIGNED (type
))
12730 case POINTER_PLUS_EXPR
:
12732 if (FLOAT_TYPE_P (type
))
12733 return RECURSE (op0
) && RECURSE (op1
);
12735 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12736 both unsigned and at least 2 bits shorter than the result. */
12737 if (TREE_CODE (type
) == INTEGER_TYPE
12738 && TREE_CODE (op0
) == NOP_EXPR
12739 && TREE_CODE (op1
) == NOP_EXPR
)
12741 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12742 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12743 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12744 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12746 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12747 TYPE_PRECISION (inner2
)) + 1;
12748 return prec
< TYPE_PRECISION (type
);
12754 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12756 /* x * x is always non-negative for floating point x
12757 or without overflow. */
12758 if (operand_equal_p (op0
, op1
, 0)
12759 || (RECURSE (op0
) && RECURSE (op1
)))
12761 if (ANY_INTEGRAL_TYPE_P (type
)
12762 && TYPE_OVERFLOW_UNDEFINED (type
))
12763 *strict_overflow_p
= true;
12768 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12769 both unsigned and their total bits is shorter than the result. */
12770 if (TREE_CODE (type
) == INTEGER_TYPE
12771 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12772 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12774 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12775 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12777 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12778 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12781 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12782 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12784 if (TREE_CODE (op0
) == INTEGER_CST
)
12785 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12787 if (TREE_CODE (op1
) == INTEGER_CST
)
12788 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12790 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12791 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12793 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12794 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12795 : TYPE_PRECISION (inner0
);
12797 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12798 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12799 : TYPE_PRECISION (inner1
);
12801 return precision0
+ precision1
< TYPE_PRECISION (type
);
12808 return RECURSE (op0
) || RECURSE (op1
);
12814 case TRUNC_DIV_EXPR
:
12815 case CEIL_DIV_EXPR
:
12816 case FLOOR_DIV_EXPR
:
12817 case ROUND_DIV_EXPR
:
12818 return RECURSE (op0
) && RECURSE (op1
);
12820 case TRUNC_MOD_EXPR
:
12821 return RECURSE (op0
);
12823 case FLOOR_MOD_EXPR
:
12824 return RECURSE (op1
);
12826 case CEIL_MOD_EXPR
:
12827 case ROUND_MOD_EXPR
:
12829 return tree_simple_nonnegative_warnv_p (code
, type
);
12832 /* We don't know sign of `t', so be conservative and return false. */
12836 /* Return true if T is known to be non-negative. If the return
12837 value is based on the assumption that signed overflow is undefined,
12838 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12839 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12842 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12844 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12847 switch (TREE_CODE (t
))
12850 return tree_int_cst_sgn (t
) >= 0;
12853 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12856 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12859 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12862 /* Limit the depth of recursion to avoid quadratic behavior.
12863 This is expected to catch almost all occurrences in practice.
12864 If this code misses important cases that unbounded recursion
12865 would not, passes that need this information could be revised
12866 to provide it through dataflow propagation. */
12867 return (!name_registered_for_update_p (t
)
12868 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12869 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12870 strict_overflow_p
, depth
));
12873 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12877 /* Return true if T is known to be non-negative. If the return
12878 value is based on the assumption that signed overflow is undefined,
12879 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12880 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12883 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
12884 bool *strict_overflow_p
, int depth
)
12905 case CFN_BUILT_IN_BSWAP32
:
12906 case CFN_BUILT_IN_BSWAP64
:
12911 /* sqrt(-0.0) is -0.0. */
12912 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
12914 return RECURSE (arg0
);
12940 CASE_CFN_NEARBYINT
:
12947 CASE_CFN_SIGNIFICAND
:
12951 /* True if the 1st argument is nonnegative. */
12952 return RECURSE (arg0
);
12955 /* True if the 1st OR 2nd arguments are nonnegative. */
12956 return RECURSE (arg0
) || RECURSE (arg1
);
12959 /* True if the 1st AND 2nd arguments are nonnegative. */
12960 return RECURSE (arg0
) && RECURSE (arg1
);
12963 /* True if the 2nd argument is nonnegative. */
12964 return RECURSE (arg1
);
12967 /* True if the 1st argument is nonnegative or the second
12968 argument is an even integer. */
12969 if (TREE_CODE (arg1
) == INTEGER_CST
12970 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
12972 return RECURSE (arg0
);
12975 /* True if the 1st argument is nonnegative or the second
12976 argument is an even integer valued real. */
12977 if (TREE_CODE (arg1
) == REAL_CST
)
12982 c
= TREE_REAL_CST (arg1
);
12983 n
= real_to_integer (&c
);
12986 REAL_VALUE_TYPE cint
;
12987 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
12988 if (real_identical (&c
, &cint
))
12992 return RECURSE (arg0
);
12997 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
13000 /* Return true if T is known to be non-negative. If the return
13001 value is based on the assumption that signed overflow is undefined,
13002 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13003 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13006 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13008 enum tree_code code
= TREE_CODE (t
);
13009 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13016 tree temp
= TARGET_EXPR_SLOT (t
);
13017 t
= TARGET_EXPR_INITIAL (t
);
13019 /* If the initializer is non-void, then it's a normal expression
13020 that will be assigned to the slot. */
13021 if (!VOID_TYPE_P (t
))
13022 return RECURSE (t
);
13024 /* Otherwise, the initializer sets the slot in some way. One common
13025 way is an assignment statement at the end of the initializer. */
13028 if (TREE_CODE (t
) == BIND_EXPR
)
13029 t
= expr_last (BIND_EXPR_BODY (t
));
13030 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13031 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13032 t
= expr_last (TREE_OPERAND (t
, 0));
13033 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13038 if (TREE_CODE (t
) == MODIFY_EXPR
13039 && TREE_OPERAND (t
, 0) == temp
)
13040 return RECURSE (TREE_OPERAND (t
, 1));
13047 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13048 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13050 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13051 get_call_combined_fn (t
),
13054 strict_overflow_p
, depth
);
13056 case COMPOUND_EXPR
:
13058 return RECURSE (TREE_OPERAND (t
, 1));
13061 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13064 return RECURSE (TREE_OPERAND (t
, 0));
13067 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13072 #undef tree_expr_nonnegative_warnv_p
13074 /* Return true if T is known to be non-negative. If the return
13075 value is based on the assumption that signed overflow is undefined,
13076 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13077 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13080 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13082 enum tree_code code
;
13083 if (t
== error_mark_node
)
13086 code
= TREE_CODE (t
);
13087 switch (TREE_CODE_CLASS (code
))
13090 case tcc_comparison
:
13091 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13093 TREE_OPERAND (t
, 0),
13094 TREE_OPERAND (t
, 1),
13095 strict_overflow_p
, depth
);
13098 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13100 TREE_OPERAND (t
, 0),
13101 strict_overflow_p
, depth
);
13104 case tcc_declaration
:
13105 case tcc_reference
:
13106 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13114 case TRUTH_AND_EXPR
:
13115 case TRUTH_OR_EXPR
:
13116 case TRUTH_XOR_EXPR
:
13117 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13119 TREE_OPERAND (t
, 0),
13120 TREE_OPERAND (t
, 1),
13121 strict_overflow_p
, depth
);
13122 case TRUTH_NOT_EXPR
:
13123 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13125 TREE_OPERAND (t
, 0),
13126 strict_overflow_p
, depth
);
13133 case WITH_SIZE_EXPR
:
13135 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13138 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13142 /* Return true if `t' is known to be non-negative. Handle warnings
13143 about undefined signed overflow. */
13146 tree_expr_nonnegative_p (tree t
)
13148 bool ret
, strict_overflow_p
;
13150 strict_overflow_p
= false;
13151 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13152 if (strict_overflow_p
)
13153 fold_overflow_warning (("assuming signed overflow does not occur when "
13154 "determining that expression is always "
13156 WARN_STRICT_OVERFLOW_MISC
);
13161 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13162 For floating point we further ensure that T is not denormal.
13163 Similar logic is present in nonzero_address in rtlanal.h.
13165 If the return value is based on the assumption that signed overflow
13166 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13167 change *STRICT_OVERFLOW_P. */
13170 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13171 bool *strict_overflow_p
)
13176 return tree_expr_nonzero_warnv_p (op0
,
13177 strict_overflow_p
);
13181 tree inner_type
= TREE_TYPE (op0
);
13182 tree outer_type
= type
;
13184 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13185 && tree_expr_nonzero_warnv_p (op0
,
13186 strict_overflow_p
));
13190 case NON_LVALUE_EXPR
:
13191 return tree_expr_nonzero_warnv_p (op0
,
13192 strict_overflow_p
);
13201 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13202 For floating point we further ensure that T is not denormal.
13203 Similar logic is present in nonzero_address in rtlanal.h.
13205 If the return value is based on the assumption that signed overflow
13206 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13207 change *STRICT_OVERFLOW_P. */
13210 tree_binary_nonzero_warnv_p (enum tree_code code
,
13213 tree op1
, bool *strict_overflow_p
)
13215 bool sub_strict_overflow_p
;
13218 case POINTER_PLUS_EXPR
:
13220 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13222 /* With the presence of negative values it is hard
13223 to say something. */
13224 sub_strict_overflow_p
= false;
13225 if (!tree_expr_nonnegative_warnv_p (op0
,
13226 &sub_strict_overflow_p
)
13227 || !tree_expr_nonnegative_warnv_p (op1
,
13228 &sub_strict_overflow_p
))
13230 /* One of operands must be positive and the other non-negative. */
13231 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13232 overflows, on a twos-complement machine the sum of two
13233 nonnegative numbers can never be zero. */
13234 return (tree_expr_nonzero_warnv_p (op0
,
13236 || tree_expr_nonzero_warnv_p (op1
,
13237 strict_overflow_p
));
13242 if (TYPE_OVERFLOW_UNDEFINED (type
))
13244 if (tree_expr_nonzero_warnv_p (op0
,
13246 && tree_expr_nonzero_warnv_p (op1
,
13247 strict_overflow_p
))
13249 *strict_overflow_p
= true;
13256 sub_strict_overflow_p
= false;
13257 if (tree_expr_nonzero_warnv_p (op0
,
13258 &sub_strict_overflow_p
)
13259 && tree_expr_nonzero_warnv_p (op1
,
13260 &sub_strict_overflow_p
))
13262 if (sub_strict_overflow_p
)
13263 *strict_overflow_p
= true;
13268 sub_strict_overflow_p
= false;
13269 if (tree_expr_nonzero_warnv_p (op0
,
13270 &sub_strict_overflow_p
))
13272 if (sub_strict_overflow_p
)
13273 *strict_overflow_p
= true;
13275 /* When both operands are nonzero, then MAX must be too. */
13276 if (tree_expr_nonzero_warnv_p (op1
,
13277 strict_overflow_p
))
13280 /* MAX where operand 0 is positive is positive. */
13281 return tree_expr_nonnegative_warnv_p (op0
,
13282 strict_overflow_p
);
13284 /* MAX where operand 1 is positive is positive. */
13285 else if (tree_expr_nonzero_warnv_p (op1
,
13286 &sub_strict_overflow_p
)
13287 && tree_expr_nonnegative_warnv_p (op1
,
13288 &sub_strict_overflow_p
))
13290 if (sub_strict_overflow_p
)
13291 *strict_overflow_p
= true;
13297 return (tree_expr_nonzero_warnv_p (op1
,
13299 || tree_expr_nonzero_warnv_p (op0
,
13300 strict_overflow_p
));
13309 /* Return true when T is an address and is known to be nonzero.
13310 For floating point we further ensure that T is not denormal.
13311 Similar logic is present in nonzero_address in rtlanal.h.
13313 If the return value is based on the assumption that signed overflow
13314 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13315 change *STRICT_OVERFLOW_P. */
13318 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13320 bool sub_strict_overflow_p
;
13321 switch (TREE_CODE (t
))
13324 return !integer_zerop (t
);
13328 tree base
= TREE_OPERAND (t
, 0);
13330 if (!DECL_P (base
))
13331 base
= get_base_address (base
);
13333 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
13334 base
= TARGET_EXPR_SLOT (base
);
13339 /* For objects in symbol table check if we know they are non-zero.
13340 Don't do anything for variables and functions before symtab is built;
13341 it is quite possible that they will be declared weak later. */
13342 int nonzero_addr
= maybe_nonzero_address (base
);
13343 if (nonzero_addr
>= 0)
13344 return nonzero_addr
;
13346 /* Constants are never weak. */
13347 if (CONSTANT_CLASS_P (base
))
13354 sub_strict_overflow_p
= false;
13355 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13356 &sub_strict_overflow_p
)
13357 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13358 &sub_strict_overflow_p
))
13360 if (sub_strict_overflow_p
)
13361 *strict_overflow_p
= true;
13367 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
13369 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
13377 #define integer_valued_real_p(X) \
13378 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13380 #define RECURSE(X) \
13381 ((integer_valued_real_p) (X, depth + 1))
13383 /* Return true if the floating point result of (CODE OP0) has an
13384 integer value. We also allow +Inf, -Inf and NaN to be considered
13385 integer values. Return false for signaling NaN.
13387 DEPTH is the current nesting depth of the query. */
13390 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13398 return RECURSE (op0
);
13402 tree type
= TREE_TYPE (op0
);
13403 if (TREE_CODE (type
) == INTEGER_TYPE
)
13405 if (TREE_CODE (type
) == REAL_TYPE
)
13406 return RECURSE (op0
);
13416 /* Return true if the floating point result of (CODE OP0 OP1) has an
13417 integer value. We also allow +Inf, -Inf and NaN to be considered
13418 integer values. Return false for signaling NaN.
13420 DEPTH is the current nesting depth of the query. */
13423 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13432 return RECURSE (op0
) && RECURSE (op1
);
13440 /* Return true if the floating point result of calling FNDECL with arguments
13441 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13442 considered integer values. Return false for signaling NaN. If FNDECL
13443 takes fewer than 2 arguments, the remaining ARGn are null.
13445 DEPTH is the current nesting depth of the query. */
13448 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13454 CASE_CFN_NEARBYINT
:
13462 return RECURSE (arg0
) && RECURSE (arg1
);
13470 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13471 has an integer value. We also allow +Inf, -Inf and NaN to be
13472 considered integer values. Return false for signaling NaN.
13474 DEPTH is the current nesting depth of the query. */
13477 integer_valued_real_single_p (tree t
, int depth
)
13479 switch (TREE_CODE (t
))
13482 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13485 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13488 /* Limit the depth of recursion to avoid quadratic behavior.
13489 This is expected to catch almost all occurrences in practice.
13490 If this code misses important cases that unbounded recursion
13491 would not, passes that need this information could be revised
13492 to provide it through dataflow propagation. */
13493 return (!name_registered_for_update_p (t
)
13494 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13495 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13504 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13505 has an integer value. We also allow +Inf, -Inf and NaN to be
13506 considered integer values. Return false for signaling NaN.
13508 DEPTH is the current nesting depth of the query. */
13511 integer_valued_real_invalid_p (tree t
, int depth
)
13513 switch (TREE_CODE (t
))
13515 case COMPOUND_EXPR
:
13518 return RECURSE (TREE_OPERAND (t
, 1));
13521 return RECURSE (TREE_OPERAND (t
, 0));
13530 #undef integer_valued_real_p
13532 /* Return true if the floating point expression T has an integer value.
13533 We also allow +Inf, -Inf and NaN to be considered integer values.
13534 Return false for signaling NaN.
13536 DEPTH is the current nesting depth of the query. */
13539 integer_valued_real_p (tree t
, int depth
)
13541 if (t
== error_mark_node
)
13544 tree_code code
= TREE_CODE (t
);
13545 switch (TREE_CODE_CLASS (code
))
13548 case tcc_comparison
:
13549 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13550 TREE_OPERAND (t
, 1), depth
);
13553 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13556 case tcc_declaration
:
13557 case tcc_reference
:
13558 return integer_valued_real_single_p (t
, depth
);
13568 return integer_valued_real_single_p (t
, depth
);
13572 tree arg0
= (call_expr_nargs (t
) > 0
13573 ? CALL_EXPR_ARG (t
, 0)
13575 tree arg1
= (call_expr_nargs (t
) > 1
13576 ? CALL_EXPR_ARG (t
, 1)
13578 return integer_valued_real_call_p (get_call_combined_fn (t
),
13579 arg0
, arg1
, depth
);
13583 return integer_valued_real_invalid_p (t
, depth
);
13587 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13588 attempt to fold the expression to a constant without modifying TYPE,
13591 If the expression could be simplified to a constant, then return
13592 the constant. If the expression would not be simplified to a
13593 constant, then return NULL_TREE. */
13596 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13598 tree tem
= fold_binary (code
, type
, op0
, op1
);
13599 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13602 /* Given the components of a unary expression CODE, TYPE and OP0,
13603 attempt to fold the expression to a constant without modifying
13606 If the expression could be simplified to a constant, then return
13607 the constant. If the expression would not be simplified to a
13608 constant, then return NULL_TREE. */
13611 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13613 tree tem
= fold_unary (code
, type
, op0
);
13614 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13617 /* If EXP represents referencing an element in a constant string
13618 (either via pointer arithmetic or array indexing), return the
13619 tree representing the value accessed, otherwise return NULL. */
13622 fold_read_from_constant_string (tree exp
)
13624 if ((TREE_CODE (exp
) == INDIRECT_REF
13625 || TREE_CODE (exp
) == ARRAY_REF
)
13626 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13628 tree exp1
= TREE_OPERAND (exp
, 0);
13631 location_t loc
= EXPR_LOCATION (exp
);
13633 if (TREE_CODE (exp
) == INDIRECT_REF
)
13634 string
= string_constant (exp1
, &index
);
13637 tree low_bound
= array_ref_low_bound (exp
);
13638 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13640 /* Optimize the special-case of a zero lower bound.
13642 We convert the low_bound to sizetype to avoid some problems
13643 with constant folding. (E.g. suppose the lower bound is 1,
13644 and its mode is QI. Without the conversion,l (ARRAY
13645 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13646 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13647 if (! integer_zerop (low_bound
))
13648 index
= size_diffop_loc (loc
, index
,
13649 fold_convert_loc (loc
, sizetype
, low_bound
));
13654 scalar_int_mode char_mode
;
13656 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13657 && TREE_CODE (string
) == STRING_CST
13658 && TREE_CODE (index
) == INTEGER_CST
13659 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13660 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))),
13662 && GET_MODE_SIZE (char_mode
) == 1)
13663 return build_int_cst_type (TREE_TYPE (exp
),
13664 (TREE_STRING_POINTER (string
)
13665 [TREE_INT_CST_LOW (index
)]));
13670 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13671 an integer constant, real, or fixed-point constant.
13673 TYPE is the type of the result. */
13676 fold_negate_const (tree arg0
, tree type
)
13678 tree t
= NULL_TREE
;
13680 switch (TREE_CODE (arg0
))
13685 wide_int val
= wi::neg (arg0
, &overflow
);
13686 t
= force_fit_type (type
, val
, 1,
13687 (overflow
&& ! TYPE_UNSIGNED (type
))
13688 || TREE_OVERFLOW (arg0
));
13693 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13698 FIXED_VALUE_TYPE f
;
13699 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13700 &(TREE_FIXED_CST (arg0
)), NULL
,
13701 TYPE_SATURATING (type
));
13702 t
= build_fixed (type
, f
);
13703 /* Propagate overflow flags. */
13704 if (overflow_p
| TREE_OVERFLOW (arg0
))
13705 TREE_OVERFLOW (t
) = 1;
13710 gcc_unreachable ();
13716 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13717 an integer constant or real constant.
13719 TYPE is the type of the result. */
13722 fold_abs_const (tree arg0
, tree type
)
13724 tree t
= NULL_TREE
;
13726 switch (TREE_CODE (arg0
))
13730 /* If the value is unsigned or non-negative, then the absolute value
13731 is the same as the ordinary value. */
13732 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13735 /* If the value is negative, then the absolute value is
13740 wide_int val
= wi::neg (arg0
, &overflow
);
13741 t
= force_fit_type (type
, val
, -1,
13742 overflow
| TREE_OVERFLOW (arg0
));
13748 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13749 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13755 gcc_unreachable ();
13761 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13762 constant. TYPE is the type of the result. */
13765 fold_not_const (const_tree arg0
, tree type
)
13767 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13769 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13772 /* Given CODE, a relational operator, the target type, TYPE and two
13773 constant operands OP0 and OP1, return the result of the
13774 relational operation. If the result is not a compile time
13775 constant, then return NULL_TREE. */
13778 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13780 int result
, invert
;
13782 /* From here on, the only cases we handle are when the result is
13783 known to be a constant. */
13785 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13787 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13788 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13790 /* Handle the cases where either operand is a NaN. */
13791 if (real_isnan (c0
) || real_isnan (c1
))
13801 case UNORDERED_EXPR
:
13815 if (flag_trapping_math
)
13821 gcc_unreachable ();
13824 return constant_boolean_node (result
, type
);
13827 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13830 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13832 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13833 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13834 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13837 /* Handle equality/inequality of complex constants. */
13838 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13840 tree rcond
= fold_relational_const (code
, type
,
13841 TREE_REALPART (op0
),
13842 TREE_REALPART (op1
));
13843 tree icond
= fold_relational_const (code
, type
,
13844 TREE_IMAGPART (op0
),
13845 TREE_IMAGPART (op1
));
13846 if (code
== EQ_EXPR
)
13847 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13848 else if (code
== NE_EXPR
)
13849 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13854 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13856 if (!VECTOR_TYPE_P (type
))
13858 /* Have vector comparison with scalar boolean result. */
13859 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
13860 && VECTOR_CST_NELTS (op0
) == VECTOR_CST_NELTS (op1
));
13861 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (op0
); i
++)
13863 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13864 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13865 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
13866 if (tmp
== NULL_TREE
)
13868 if (integer_zerop (tmp
))
13869 return constant_boolean_node (false, type
);
13871 return constant_boolean_node (true, type
);
13873 unsigned count
= VECTOR_CST_NELTS (op0
);
13874 gcc_assert (VECTOR_CST_NELTS (op1
) == count
13875 && TYPE_VECTOR_SUBPARTS (type
) == count
);
13877 auto_vec
<tree
, 32> elts (count
);
13878 for (unsigned i
= 0; i
< count
; i
++)
13880 tree elem_type
= TREE_TYPE (type
);
13881 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13882 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13884 tree tem
= fold_relational_const (code
, elem_type
,
13887 if (tem
== NULL_TREE
)
13890 elts
.quick_push (build_int_cst (elem_type
,
13891 integer_zerop (tem
) ? 0 : -1));
13894 return build_vector (type
, elts
);
13897 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13899 To compute GT, swap the arguments and do LT.
13900 To compute GE, do LT and invert the result.
13901 To compute LE, swap the arguments, do LT and invert the result.
13902 To compute NE, do EQ and invert the result.
13904 Therefore, the code below must handle only EQ and LT. */
13906 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13908 std::swap (op0
, op1
);
13909 code
= swap_tree_comparison (code
);
13912 /* Note that it is safe to invert for real values here because we
13913 have already handled the one case that it matters. */
13916 if (code
== NE_EXPR
|| code
== GE_EXPR
)
13919 code
= invert_tree_comparison (code
, false);
13922 /* Compute a result for LT or EQ if args permit;
13923 Otherwise return T. */
13924 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
13926 if (code
== EQ_EXPR
)
13927 result
= tree_int_cst_equal (op0
, op1
);
13929 result
= tree_int_cst_lt (op0
, op1
);
13936 return constant_boolean_node (result
, type
);
13939 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13940 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13944 fold_build_cleanup_point_expr (tree type
, tree expr
)
13946 /* If the expression does not have side effects then we don't have to wrap
13947 it with a cleanup point expression. */
13948 if (!TREE_SIDE_EFFECTS (expr
))
13951 /* If the expression is a return, check to see if the expression inside the
13952 return has no side effects or the right hand side of the modify expression
13953 inside the return. If either don't have side effects set we don't need to
13954 wrap the expression in a cleanup point expression. Note we don't check the
13955 left hand side of the modify because it should always be a return decl. */
13956 if (TREE_CODE (expr
) == RETURN_EXPR
)
13958 tree op
= TREE_OPERAND (expr
, 0);
13959 if (!op
|| !TREE_SIDE_EFFECTS (op
))
13961 op
= TREE_OPERAND (op
, 1);
13962 if (!TREE_SIDE_EFFECTS (op
))
13966 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
13969 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13970 of an indirection through OP0, or NULL_TREE if no simplification is
13974 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
13980 subtype
= TREE_TYPE (sub
);
13981 if (!POINTER_TYPE_P (subtype
)
13982 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
13985 if (TREE_CODE (sub
) == ADDR_EXPR
)
13987 tree op
= TREE_OPERAND (sub
, 0);
13988 tree optype
= TREE_TYPE (op
);
13989 /* *&CONST_DECL -> to the value of the const decl. */
13990 if (TREE_CODE (op
) == CONST_DECL
)
13991 return DECL_INITIAL (op
);
13992 /* *&p => p; make sure to handle *&"str"[cst] here. */
13993 if (type
== optype
)
13995 tree fop
= fold_read_from_constant_string (op
);
14001 /* *(foo *)&fooarray => fooarray[0] */
14002 else if (TREE_CODE (optype
) == ARRAY_TYPE
14003 && type
== TREE_TYPE (optype
)
14004 && (!in_gimple_form
14005 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14007 tree type_domain
= TYPE_DOMAIN (optype
);
14008 tree min_val
= size_zero_node
;
14009 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14010 min_val
= TYPE_MIN_VALUE (type_domain
);
14012 && TREE_CODE (min_val
) != INTEGER_CST
)
14014 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
14015 NULL_TREE
, NULL_TREE
);
14017 /* *(foo *)&complexfoo => __real__ complexfoo */
14018 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14019 && type
== TREE_TYPE (optype
))
14020 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14021 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14022 else if (TREE_CODE (optype
) == VECTOR_TYPE
14023 && type
== TREE_TYPE (optype
))
14025 tree part_width
= TYPE_SIZE (type
);
14026 tree index
= bitsize_int (0);
14027 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14031 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14032 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14034 tree op00
= TREE_OPERAND (sub
, 0);
14035 tree op01
= TREE_OPERAND (sub
, 1);
14038 if (TREE_CODE (op00
) == ADDR_EXPR
)
14041 op00
= TREE_OPERAND (op00
, 0);
14042 op00type
= TREE_TYPE (op00
);
14044 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14045 if (TREE_CODE (op00type
) == VECTOR_TYPE
14046 && type
== TREE_TYPE (op00type
))
14048 tree part_width
= TYPE_SIZE (type
);
14049 unsigned HOST_WIDE_INT max_offset
14050 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14051 * TYPE_VECTOR_SUBPARTS (op00type
));
14052 if (tree_int_cst_sign_bit (op01
) == 0
14053 && compare_tree_int (op01
, max_offset
) == -1)
14055 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (op01
);
14056 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14057 tree index
= bitsize_int (indexi
);
14058 return fold_build3_loc (loc
,
14059 BIT_FIELD_REF
, type
, op00
,
14060 part_width
, index
);
14063 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14064 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14065 && type
== TREE_TYPE (op00type
))
14067 tree size
= TYPE_SIZE_UNIT (type
);
14068 if (tree_int_cst_equal (size
, op01
))
14069 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14071 /* ((foo *)&fooarray)[1] => fooarray[1] */
14072 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14073 && type
== TREE_TYPE (op00type
))
14075 tree type_domain
= TYPE_DOMAIN (op00type
);
14076 tree min
= size_zero_node
;
14077 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14078 min
= TYPE_MIN_VALUE (type_domain
);
14079 offset_int off
= wi::to_offset (op01
);
14080 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (type
));
14081 offset_int remainder
;
14082 off
= wi::divmod_trunc (off
, el_sz
, SIGNED
, &remainder
);
14083 if (remainder
== 0 && TREE_CODE (min
) == INTEGER_CST
)
14085 off
= off
+ wi::to_offset (min
);
14086 op01
= wide_int_to_tree (sizetype
, off
);
14087 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14088 NULL_TREE
, NULL_TREE
);
14094 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14095 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14096 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14097 && (!in_gimple_form
14098 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14101 tree min_val
= size_zero_node
;
14102 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14103 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14104 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14105 min_val
= TYPE_MIN_VALUE (type_domain
);
14107 && TREE_CODE (min_val
) != INTEGER_CST
)
14109 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14116 /* Builds an expression for an indirection through T, simplifying some
14120 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14122 tree type
= TREE_TYPE (TREE_TYPE (t
));
14123 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14128 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14131 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14134 fold_indirect_ref_loc (location_t loc
, tree t
)
14136 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14144 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14145 whose result is ignored. The type of the returned tree need not be
14146 the same as the original expression. */
14149 fold_ignored_result (tree t
)
14151 if (!TREE_SIDE_EFFECTS (t
))
14152 return integer_zero_node
;
14155 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14158 t
= TREE_OPERAND (t
, 0);
14162 case tcc_comparison
:
14163 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14164 t
= TREE_OPERAND (t
, 0);
14165 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14166 t
= TREE_OPERAND (t
, 1);
14171 case tcc_expression
:
14172 switch (TREE_CODE (t
))
14174 case COMPOUND_EXPR
:
14175 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14177 t
= TREE_OPERAND (t
, 0);
14181 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14182 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14184 t
= TREE_OPERAND (t
, 0);
14197 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14200 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14202 tree div
= NULL_TREE
;
14207 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14208 have to do anything. Only do this when we are not given a const,
14209 because in that case, this check is more expensive than just
14211 if (TREE_CODE (value
) != INTEGER_CST
)
14213 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14215 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14219 /* If divisor is a power of two, simplify this to bit manipulation. */
14220 if (pow2_or_zerop (divisor
))
14222 if (TREE_CODE (value
) == INTEGER_CST
)
14224 wide_int val
= value
;
14227 if ((val
& (divisor
- 1)) == 0)
14230 overflow_p
= TREE_OVERFLOW (value
);
14231 val
+= divisor
- 1;
14232 val
&= (int) -divisor
;
14236 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14242 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14243 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14244 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14245 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14251 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14252 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14253 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14259 /* Likewise, but round down. */
14262 round_down_loc (location_t loc
, tree value
, int divisor
)
14264 tree div
= NULL_TREE
;
14266 gcc_assert (divisor
> 0);
14270 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14271 have to do anything. Only do this when we are not given a const,
14272 because in that case, this check is more expensive than just
14274 if (TREE_CODE (value
) != INTEGER_CST
)
14276 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14278 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14282 /* If divisor is a power of two, simplify this to bit manipulation. */
14283 if (pow2_or_zerop (divisor
))
14287 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14288 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14293 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14294 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14295 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14301 /* Returns the pointer to the base of the object addressed by EXP and
14302 extracts the information about the offset of the access, storing it
14303 to PBITPOS and POFFSET. */
14306 split_address_to_core_and_offset (tree exp
,
14307 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14311 int unsignedp
, reversep
, volatilep
;
14312 HOST_WIDE_INT bitsize
;
14313 location_t loc
= EXPR_LOCATION (exp
);
14315 if (TREE_CODE (exp
) == ADDR_EXPR
)
14317 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14318 poffset
, &mode
, &unsignedp
, &reversep
,
14320 core
= build_fold_addr_expr_loc (loc
, core
);
14322 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
14324 core
= TREE_OPERAND (exp
, 0);
14327 *poffset
= TREE_OPERAND (exp
, 1);
14328 if (TREE_CODE (*poffset
) == INTEGER_CST
)
14330 offset_int tem
= wi::sext (wi::to_offset (*poffset
),
14331 TYPE_PRECISION (TREE_TYPE (*poffset
)));
14332 tem
<<= LOG2_BITS_PER_UNIT
;
14333 if (wi::fits_shwi_p (tem
))
14335 *pbitpos
= tem
.to_shwi ();
14336 *poffset
= NULL_TREE
;
14344 *poffset
= NULL_TREE
;
14350 /* Returns true if addresses of E1 and E2 differ by a constant, false
14351 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14354 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14357 HOST_WIDE_INT bitpos1
, bitpos2
;
14358 tree toffset1
, toffset2
, tdiff
, type
;
14360 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14361 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14363 if (bitpos1
% BITS_PER_UNIT
!= 0
14364 || bitpos2
% BITS_PER_UNIT
!= 0
14365 || !operand_equal_p (core1
, core2
, 0))
14368 if (toffset1
&& toffset2
)
14370 type
= TREE_TYPE (toffset1
);
14371 if (type
!= TREE_TYPE (toffset2
))
14372 toffset2
= fold_convert (type
, toffset2
);
14374 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14375 if (!cst_and_fits_in_hwi (tdiff
))
14378 *diff
= int_cst_value (tdiff
);
14380 else if (toffset1
|| toffset2
)
14382 /* If only one of the offsets is non-constant, the difference cannot
14389 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14393 /* Return OFF converted to a pointer offset type suitable as offset for
14394 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14396 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14398 return fold_convert_loc (loc
, sizetype
, off
);
14401 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14403 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14405 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14406 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14409 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14411 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14413 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14414 ptr
, size_int (off
));
14417 /* Return a char pointer for a C string if it is a string constant
14418 or sum of string constant and integer constant. We only support
14419 string constants properly terminated with '\0' character.
14420 If STRLEN is a valid pointer, length (including terminating character)
14421 of returned string is stored to the argument. */
14424 c_getstr (tree src
, unsigned HOST_WIDE_INT
*strlen
)
14431 src
= string_constant (src
, &offset_node
);
14435 unsigned HOST_WIDE_INT offset
= 0;
14436 if (offset_node
!= NULL_TREE
)
14438 if (!tree_fits_uhwi_p (offset_node
))
14441 offset
= tree_to_uhwi (offset_node
);
14444 unsigned HOST_WIDE_INT string_length
= TREE_STRING_LENGTH (src
);
14445 const char *string
= TREE_STRING_POINTER (src
);
14447 /* Support only properly null-terminated strings. */
14448 if (string_length
== 0
14449 || string
[string_length
- 1] != '\0'
14450 || offset
>= string_length
)
14454 *strlen
= string_length
- offset
;
14455 return string
+ offset
;
14460 namespace selftest
{
14462 /* Helper functions for writing tests of folding trees. */
14464 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14467 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
14470 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
14473 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14474 wrapping WRAPPED_EXPR. */
14477 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
14480 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
14481 ASSERT_NE (wrapped_expr
, result
);
14482 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
14483 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
14486 /* Verify that various arithmetic binary operations are folded
14490 test_arithmetic_folding ()
14492 tree type
= integer_type_node
;
14493 tree x
= create_tmp_var_raw (type
, "x");
14494 tree zero
= build_zero_cst (type
);
14495 tree one
= build_int_cst (type
, 1);
14498 /* 1 <-- (0 + 1) */
14499 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
14501 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
14504 /* (nonlvalue)x <-- (x + 0) */
14505 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
14509 /* 0 <-- (x - x) */
14510 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
14512 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
14515 /* Multiplication. */
14516 /* 0 <-- (x * 0) */
14517 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
14520 /* (nonlvalue)x <-- (x * 1) */
14521 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
14525 /* Verify that various binary operations on vectors are folded
14529 test_vector_folding ()
14531 tree inner_type
= integer_type_node
;
14532 tree type
= build_vector_type (inner_type
, 4);
14533 tree zero
= build_zero_cst (type
);
14534 tree one
= build_one_cst (type
);
14536 /* Verify equality tests that return a scalar boolean result. */
14537 tree res_type
= boolean_type_node
;
14538 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
14539 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
14540 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
14541 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
14544 /* Run all of the selftests within this file. */
14547 fold_const_c_tests ()
14549 test_arithmetic_folding ();
14550 test_vector_folding ();
14553 } // namespace selftest
14555 #endif /* CHECKING_P */