1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 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"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
56 #include "diagnostic-core.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
63 #include "tree-iterator.h"
66 #include "langhooks.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
74 #include "tree-into-ssa.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
80 #ifndef LOAD_EXTEND_OP
81 #define LOAD_EXTEND_OP(M) UNKNOWN
84 /* Nonzero if we are folding constants inside an initializer; zero
86 int folding_initializer
= 0;
88 /* The following constants represent a bit based encoding of GCC's
89 comparison operators. This encoding simplifies transformations
90 on relational comparison operators, such as AND and OR. */
91 enum comparison_code
{
110 static bool negate_expr_p (tree
);
111 static tree
negate_expr (tree
);
112 static tree
split_tree (location_t
, tree
, tree
, enum tree_code
,
113 tree
*, tree
*, tree
*, int);
114 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
115 static enum comparison_code
comparison_to_compcode (enum tree_code
);
116 static enum tree_code
compcode_to_comparison (enum comparison_code
);
117 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
118 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
119 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
120 static tree
make_bit_field_ref (location_t
, tree
, tree
,
121 HOST_WIDE_INT
, HOST_WIDE_INT
, int, int);
122 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
124 static tree
decode_field_reference (location_t
, tree
, HOST_WIDE_INT
*,
126 machine_mode
*, int *, int *, int *,
128 static int simple_operand_p (const_tree
);
129 static bool simple_operand_p_2 (tree
);
130 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
131 static tree
range_predecessor (tree
);
132 static tree
range_successor (tree
);
133 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
134 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
135 static tree
unextend (tree
, int, int, tree
);
136 static tree
optimize_minmax_comparison (location_t
, enum tree_code
,
138 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
139 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
140 static tree
fold_binary_op_with_conditional_arg (location_t
,
141 enum tree_code
, tree
,
144 static tree
fold_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
145 static bool reorder_operands_p (const_tree
, const_tree
);
146 static tree
fold_negate_const (tree
, tree
);
147 static tree
fold_not_const (const_tree
, tree
);
148 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
149 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
150 static tree
fold_view_convert_expr (tree
, tree
);
151 static bool vec_cst_ctor_to_array (tree
, tree
*);
154 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
155 Otherwise, return LOC. */
158 expr_location_or (tree t
, location_t loc
)
160 location_t tloc
= EXPR_LOCATION (t
);
161 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
164 /* Similar to protected_set_expr_location, but never modify x in place,
165 if location can and needs to be set, unshare it. */
168 protected_set_expr_location_unshare (tree x
, location_t loc
)
170 if (CAN_HAVE_LOCATION_P (x
)
171 && EXPR_LOCATION (x
) != loc
172 && !(TREE_CODE (x
) == SAVE_EXPR
173 || TREE_CODE (x
) == TARGET_EXPR
174 || TREE_CODE (x
) == BIND_EXPR
))
177 SET_EXPR_LOCATION (x
, loc
);
182 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
183 division and returns the quotient. Otherwise returns
187 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
191 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
193 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
198 /* This is nonzero if we should defer warnings about undefined
199 overflow. This facility exists because these warnings are a
200 special case. The code to estimate loop iterations does not want
201 to issue any warnings, since it works with expressions which do not
202 occur in user code. Various bits of cleanup code call fold(), but
203 only use the result if it has certain characteristics (e.g., is a
204 constant); that code only wants to issue a warning if the result is
207 static int fold_deferring_overflow_warnings
;
209 /* If a warning about undefined overflow is deferred, this is the
210 warning. Note that this may cause us to turn two warnings into
211 one, but that is fine since it is sufficient to only give one
212 warning per expression. */
214 static const char* fold_deferred_overflow_warning
;
216 /* If a warning about undefined overflow is deferred, this is the
217 level at which the warning should be emitted. */
219 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
221 /* Start deferring overflow warnings. We could use a stack here to
222 permit nested calls, but at present it is not necessary. */
225 fold_defer_overflow_warnings (void)
227 ++fold_deferring_overflow_warnings
;
230 /* Stop deferring overflow warnings. If there is a pending warning,
231 and ISSUE is true, then issue the warning if appropriate. STMT is
232 the statement with which the warning should be associated (used for
233 location information); STMT may be NULL. CODE is the level of the
234 warning--a warn_strict_overflow_code value. This function will use
235 the smaller of CODE and the deferred code when deciding whether to
236 issue the warning. CODE may be zero to mean to always use the
240 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
245 gcc_assert (fold_deferring_overflow_warnings
> 0);
246 --fold_deferring_overflow_warnings
;
247 if (fold_deferring_overflow_warnings
> 0)
249 if (fold_deferred_overflow_warning
!= NULL
251 && code
< (int) fold_deferred_overflow_code
)
252 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
256 warnmsg
= fold_deferred_overflow_warning
;
257 fold_deferred_overflow_warning
= NULL
;
259 if (!issue
|| warnmsg
== NULL
)
262 if (gimple_no_warning_p (stmt
))
265 /* Use the smallest code level when deciding to issue the
267 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
268 code
= fold_deferred_overflow_code
;
270 if (!issue_strict_overflow_warning (code
))
274 locus
= input_location
;
276 locus
= gimple_location (stmt
);
277 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
280 /* Stop deferring overflow warnings, ignoring any deferred
284 fold_undefer_and_ignore_overflow_warnings (void)
286 fold_undefer_overflow_warnings (false, NULL
, 0);
289 /* Whether we are deferring overflow warnings. */
292 fold_deferring_overflow_warnings_p (void)
294 return fold_deferring_overflow_warnings
> 0;
297 /* This is called when we fold something based on the fact that signed
298 overflow is undefined. */
301 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
303 if (fold_deferring_overflow_warnings
> 0)
305 if (fold_deferred_overflow_warning
== NULL
306 || wc
< fold_deferred_overflow_code
)
308 fold_deferred_overflow_warning
= gmsgid
;
309 fold_deferred_overflow_code
= wc
;
312 else if (issue_strict_overflow_warning (wc
))
313 warning (OPT_Wstrict_overflow
, gmsgid
);
316 /* Return true if the built-in mathematical function specified by CODE
317 is odd, i.e. -f(x) == f(-x). */
320 negate_mathfn_p (combined_fn fn
)
353 return !flag_rounding_math
;
361 /* Check whether we may negate an integer constant T without causing
365 may_negate_without_overflow_p (const_tree t
)
369 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
371 type
= TREE_TYPE (t
);
372 if (TYPE_UNSIGNED (type
))
375 return !wi::only_sign_bit_p (t
);
378 /* Determine whether an expression T can be cheaply negated using
379 the function negate_expr without introducing undefined overflow. */
382 negate_expr_p (tree t
)
389 type
= TREE_TYPE (t
);
392 switch (TREE_CODE (t
))
395 if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
398 /* Check that -CST will not overflow type. */
399 return may_negate_without_overflow_p (t
);
401 return (INTEGRAL_TYPE_P (type
)
402 && TYPE_OVERFLOW_WRAPS (type
));
408 return !TYPE_OVERFLOW_SANITIZED (type
);
411 /* We want to canonicalize to positive real constants. Pretend
412 that only negative ones can be easily negated. */
413 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
416 return negate_expr_p (TREE_REALPART (t
))
417 && negate_expr_p (TREE_IMAGPART (t
));
421 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
424 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
426 for (i
= 0; i
< count
; i
++)
427 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
434 return negate_expr_p (TREE_OPERAND (t
, 0))
435 && negate_expr_p (TREE_OPERAND (t
, 1));
438 return negate_expr_p (TREE_OPERAND (t
, 0));
441 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
442 || HONOR_SIGNED_ZEROS (element_mode (type
))
443 || (INTEGRAL_TYPE_P (type
)
444 && ! TYPE_OVERFLOW_WRAPS (type
)))
446 /* -(A + B) -> (-B) - A. */
447 if (negate_expr_p (TREE_OPERAND (t
, 1))
448 && reorder_operands_p (TREE_OPERAND (t
, 0),
449 TREE_OPERAND (t
, 1)))
451 /* -(A + B) -> (-A) - B. */
452 return negate_expr_p (TREE_OPERAND (t
, 0));
455 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
456 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
457 && !HONOR_SIGNED_ZEROS (element_mode (type
))
458 && (! INTEGRAL_TYPE_P (type
)
459 || TYPE_OVERFLOW_WRAPS (type
))
460 && reorder_operands_p (TREE_OPERAND (t
, 0),
461 TREE_OPERAND (t
, 1));
464 if (TYPE_UNSIGNED (type
))
466 /* INT_MIN/n * n doesn't overflow while negating one operand it does
467 if n is a power of two. */
468 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
469 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
470 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
471 && ! integer_pow2p (TREE_OPERAND (t
, 0)))
472 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
473 && ! integer_pow2p (TREE_OPERAND (t
, 1)))))
479 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
480 return negate_expr_p (TREE_OPERAND (t
, 1))
481 || negate_expr_p (TREE_OPERAND (t
, 0));
487 if (TYPE_UNSIGNED (type
))
489 if (negate_expr_p (TREE_OPERAND (t
, 0)))
491 /* In general we can't negate B in A / B, because if A is INT_MIN and
492 B is 1, we may turn this into INT_MIN / -1 which is undefined
493 and actually traps on some architectures. */
494 if (! INTEGRAL_TYPE_P (TREE_TYPE (t
))
495 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
496 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
497 && ! integer_onep (TREE_OPERAND (t
, 1))))
498 return negate_expr_p (TREE_OPERAND (t
, 1));
502 /* Negate -((double)float) as (double)(-float). */
503 if (TREE_CODE (type
) == REAL_TYPE
)
505 tree tem
= strip_float_extensions (t
);
507 return negate_expr_p (tem
);
512 /* Negate -f(x) as f(-x). */
513 if (negate_mathfn_p (get_call_combined_fn (t
)))
514 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
518 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
519 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
521 tree op1
= TREE_OPERAND (t
, 1);
522 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
533 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
534 simplification is possible.
535 If negate_expr_p would return true for T, NULL_TREE will never be
539 fold_negate_expr (location_t loc
, tree t
)
541 tree type
= TREE_TYPE (t
);
544 switch (TREE_CODE (t
))
546 /* Convert - (~A) to A + 1. */
548 if (INTEGRAL_TYPE_P (type
))
549 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
550 build_one_cst (type
));
554 tem
= fold_negate_const (t
, type
);
555 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
556 || (ANY_INTEGRAL_TYPE_P (type
)
557 && !TYPE_OVERFLOW_TRAPS (type
)
558 && TYPE_OVERFLOW_WRAPS (type
))
559 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
564 tem
= fold_negate_const (t
, type
);
568 tem
= fold_negate_const (t
, type
);
573 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
574 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
576 return build_complex (type
, rpart
, ipart
);
582 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
583 tree
*elts
= XALLOCAVEC (tree
, count
);
585 for (i
= 0; i
< count
; i
++)
587 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
588 if (elts
[i
] == NULL_TREE
)
592 return build_vector (type
, elts
);
596 if (negate_expr_p (t
))
597 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
598 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
599 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
603 if (negate_expr_p (t
))
604 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
605 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
609 if (!TYPE_OVERFLOW_SANITIZED (type
))
610 return TREE_OPERAND (t
, 0);
614 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
615 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
617 /* -(A + B) -> (-B) - A. */
618 if (negate_expr_p (TREE_OPERAND (t
, 1))
619 && reorder_operands_p (TREE_OPERAND (t
, 0),
620 TREE_OPERAND (t
, 1)))
622 tem
= negate_expr (TREE_OPERAND (t
, 1));
623 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
624 tem
, TREE_OPERAND (t
, 0));
627 /* -(A + B) -> (-A) - B. */
628 if (negate_expr_p (TREE_OPERAND (t
, 0)))
630 tem
= negate_expr (TREE_OPERAND (t
, 0));
631 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
632 tem
, TREE_OPERAND (t
, 1));
638 /* - (A - B) -> B - A */
639 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
640 && !HONOR_SIGNED_ZEROS (element_mode (type
))
641 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
642 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
643 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
647 if (TYPE_UNSIGNED (type
))
653 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
655 tem
= TREE_OPERAND (t
, 1);
656 if (negate_expr_p (tem
))
657 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
658 TREE_OPERAND (t
, 0), negate_expr (tem
));
659 tem
= TREE_OPERAND (t
, 0);
660 if (negate_expr_p (tem
))
661 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
662 negate_expr (tem
), TREE_OPERAND (t
, 1));
669 if (TYPE_UNSIGNED (type
))
671 if (negate_expr_p (TREE_OPERAND (t
, 0)))
672 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
673 negate_expr (TREE_OPERAND (t
, 0)),
674 TREE_OPERAND (t
, 1));
675 /* In general we can't negate B in A / B, because if A is INT_MIN and
676 B is 1, we may turn this into INT_MIN / -1 which is undefined
677 and actually traps on some architectures. */
678 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t
))
679 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
680 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
681 && ! integer_onep (TREE_OPERAND (t
, 1))))
682 && negate_expr_p (TREE_OPERAND (t
, 1)))
683 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
685 negate_expr (TREE_OPERAND (t
, 1)));
689 /* Convert -((double)float) into (double)(-float). */
690 if (TREE_CODE (type
) == REAL_TYPE
)
692 tem
= strip_float_extensions (t
);
693 if (tem
!= t
&& negate_expr_p (tem
))
694 return fold_convert_loc (loc
, type
, negate_expr (tem
));
699 /* Negate -f(x) as f(-x). */
700 if (negate_mathfn_p (get_call_combined_fn (t
))
701 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
705 fndecl
= get_callee_fndecl (t
);
706 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
707 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
712 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
713 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
715 tree op1
= TREE_OPERAND (t
, 1);
716 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
718 tree ntype
= TYPE_UNSIGNED (type
)
719 ? signed_type_for (type
)
720 : unsigned_type_for (type
);
721 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
722 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
723 return fold_convert_loc (loc
, type
, temp
);
735 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
736 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
748 loc
= EXPR_LOCATION (t
);
749 type
= TREE_TYPE (t
);
752 tem
= fold_negate_expr (loc
, t
);
754 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
755 return fold_convert_loc (loc
, type
, tem
);
758 /* Split a tree IN into a constant, literal and variable parts that could be
759 combined with CODE to make IN. "constant" means an expression with
760 TREE_CONSTANT but that isn't an actual constant. CODE must be a
761 commutative arithmetic operation. Store the constant part into *CONP,
762 the literal in *LITP and return the variable part. If a part isn't
763 present, set it to null. If the tree does not decompose in this way,
764 return the entire tree as the variable part and the other parts as null.
766 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
767 case, we negate an operand that was subtracted. Except if it is a
768 literal for which we use *MINUS_LITP instead.
770 If NEGATE_P is true, we are negating all of IN, again except a literal
771 for which we use *MINUS_LITP instead. If a variable part is of pointer
772 type, it is negated after converting to TYPE. This prevents us from
773 generating illegal MINUS pointer expression. LOC is the location of
774 the converted variable part.
776 If IN is itself a literal or constant, return it as appropriate.
778 Note that we do not guarantee that any of the three values will be the
779 same type as IN, but they will have the same signedness and mode. */
782 split_tree (location_t loc
, tree in
, tree type
, enum tree_code code
,
783 tree
*conp
, tree
*litp
, tree
*minus_litp
, int negate_p
)
791 /* Strip any conversions that don't change the machine mode or signedness. */
792 STRIP_SIGN_NOPS (in
);
794 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
795 || TREE_CODE (in
) == FIXED_CST
)
797 else if (TREE_CODE (in
) == code
798 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
799 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
800 /* We can associate addition and subtraction together (even
801 though the C standard doesn't say so) for integers because
802 the value is not affected. For reals, the value might be
803 affected, so we can't. */
804 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
805 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == 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;
838 *conp
= negate_expr (*conp
);
841 /* Convert to TYPE before negating a pointer type expr. */
842 if (var
&& POINTER_TYPE_P (TREE_TYPE (var
)))
843 var
= fold_convert_loc (loc
, type
, var
);
844 var
= negate_expr (var
);
847 else if (TREE_CODE (in
) == BIT_NOT_EXPR
848 && code
== PLUS_EXPR
)
850 /* -X - 1 is folded to ~X, undo that here. */
851 *minus_litp
= build_one_cst (TREE_TYPE (in
));
852 var
= negate_expr (TREE_OPERAND (in
, 0));
854 else if (TREE_CONSTANT (in
))
862 *minus_litp
= *litp
, *litp
= 0;
863 else if (*minus_litp
)
864 *litp
= *minus_litp
, *minus_litp
= 0;
865 *conp
= negate_expr (*conp
);
866 /* Convert to TYPE before negating a pointer type expr. */
867 if (var
&& POINTER_TYPE_P (TREE_TYPE (var
)))
868 var
= fold_convert_loc (loc
, type
, var
);
869 var
= negate_expr (var
);
875 /* Re-associate trees split by the above function. T1 and T2 are
876 either expressions to associate or null. Return the new
877 expression, if any. LOC is the location of the new expression. If
878 we build an operation, do it in TYPE and with CODE. */
881 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
888 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
889 try to fold this since we will have infinite recursion. But do
890 deal with any NEGATE_EXPRs. */
891 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
892 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
894 if (code
== PLUS_EXPR
)
896 if (TREE_CODE (t1
) == NEGATE_EXPR
)
897 return build2_loc (loc
, MINUS_EXPR
, type
,
898 fold_convert_loc (loc
, type
, t2
),
899 fold_convert_loc (loc
, type
,
900 TREE_OPERAND (t1
, 0)));
901 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
902 return build2_loc (loc
, MINUS_EXPR
, type
,
903 fold_convert_loc (loc
, type
, t1
),
904 fold_convert_loc (loc
, type
,
905 TREE_OPERAND (t2
, 0)));
906 else if (integer_zerop (t2
))
907 return fold_convert_loc (loc
, type
, t1
);
909 else if (code
== MINUS_EXPR
)
911 if (integer_zerop (t2
))
912 return fold_convert_loc (loc
, type
, t1
);
915 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
916 fold_convert_loc (loc
, type
, t2
));
919 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
920 fold_convert_loc (loc
, type
, t2
));
923 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
924 for use in int_const_binop, size_binop and size_diffop. */
927 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
929 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
931 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
946 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
947 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
948 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
952 /* Combine two integer constants ARG1 and ARG2 under operation CODE
953 to produce a new constant. Return NULL_TREE if we don't know how
954 to evaluate CODE at compile-time. */
957 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
962 tree type
= TREE_TYPE (arg1
);
963 signop sign
= TYPE_SIGN (type
);
964 bool overflow
= false;
966 wide_int arg2
= wide_int::from (parg2
, TYPE_PRECISION (type
),
967 TYPE_SIGN (TREE_TYPE (parg2
)));
972 res
= wi::bit_or (arg1
, arg2
);
976 res
= wi::bit_xor (arg1
, arg2
);
980 res
= wi::bit_and (arg1
, arg2
);
985 if (wi::neg_p (arg2
))
988 if (code
== RSHIFT_EXPR
)
994 if (code
== RSHIFT_EXPR
)
995 /* It's unclear from the C standard whether shifts can overflow.
996 The following code ignores overflow; perhaps a C standard
997 interpretation ruling is needed. */
998 res
= wi::rshift (arg1
, arg2
, sign
);
1000 res
= wi::lshift (arg1
, arg2
);
1005 if (wi::neg_p (arg2
))
1008 if (code
== RROTATE_EXPR
)
1009 code
= LROTATE_EXPR
;
1011 code
= RROTATE_EXPR
;
1014 if (code
== RROTATE_EXPR
)
1015 res
= wi::rrotate (arg1
, arg2
);
1017 res
= wi::lrotate (arg1
, arg2
);
1021 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1025 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1029 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1032 case MULT_HIGHPART_EXPR
:
1033 res
= wi::mul_high (arg1
, arg2
, sign
);
1036 case TRUNC_DIV_EXPR
:
1037 case EXACT_DIV_EXPR
:
1040 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1043 case FLOOR_DIV_EXPR
:
1046 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1052 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1055 case ROUND_DIV_EXPR
:
1058 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1061 case TRUNC_MOD_EXPR
:
1064 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1067 case FLOOR_MOD_EXPR
:
1070 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1076 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1079 case ROUND_MOD_EXPR
:
1082 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1086 res
= wi::min (arg1
, arg2
, sign
);
1090 res
= wi::max (arg1
, arg2
, sign
);
1097 t
= force_fit_type (type
, res
, overflowable
,
1098 (((sign
== SIGNED
|| overflowable
== -1)
1100 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1106 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1108 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1111 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1112 constant. We assume ARG1 and ARG2 have the same data type, or at least
1113 are the same kind of constant and the same machine mode. Return zero if
1114 combining the constants is not allowed in the current operating mode. */
1117 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1119 /* Sanity check for the recursive cases. */
1126 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1128 if (code
== POINTER_PLUS_EXPR
)
1129 return int_const_binop (PLUS_EXPR
,
1130 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1132 return int_const_binop (code
, arg1
, arg2
);
1135 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1140 REAL_VALUE_TYPE value
;
1141 REAL_VALUE_TYPE result
;
1145 /* The following codes are handled by real_arithmetic. */
1160 d1
= TREE_REAL_CST (arg1
);
1161 d2
= TREE_REAL_CST (arg2
);
1163 type
= TREE_TYPE (arg1
);
1164 mode
= TYPE_MODE (type
);
1166 /* Don't perform operation if we honor signaling NaNs and
1167 either operand is a signaling NaN. */
1168 if (HONOR_SNANS (mode
)
1169 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1170 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1173 /* Don't perform operation if it would raise a division
1174 by zero exception. */
1175 if (code
== RDIV_EXPR
1176 && real_equal (&d2
, &dconst0
)
1177 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1180 /* If either operand is a NaN, just return it. Otherwise, set up
1181 for floating-point trap; we return an overflow. */
1182 if (REAL_VALUE_ISNAN (d1
))
1184 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1187 t
= build_real (type
, d1
);
1190 else if (REAL_VALUE_ISNAN (d2
))
1192 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1195 t
= build_real (type
, d2
);
1199 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1200 real_convert (&result
, mode
, &value
);
1202 /* Don't constant fold this floating point operation if
1203 the result has overflowed and flag_trapping_math. */
1204 if (flag_trapping_math
1205 && MODE_HAS_INFINITIES (mode
)
1206 && REAL_VALUE_ISINF (result
)
1207 && !REAL_VALUE_ISINF (d1
)
1208 && !REAL_VALUE_ISINF (d2
))
1211 /* Don't constant fold this floating point operation if the
1212 result may dependent upon the run-time rounding mode and
1213 flag_rounding_math is set, or if GCC's software emulation
1214 is unable to accurately represent the result. */
1215 if ((flag_rounding_math
1216 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1217 && (inexact
|| !real_identical (&result
, &value
)))
1220 t
= build_real (type
, result
);
1222 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1226 if (TREE_CODE (arg1
) == FIXED_CST
)
1228 FIXED_VALUE_TYPE f1
;
1229 FIXED_VALUE_TYPE f2
;
1230 FIXED_VALUE_TYPE result
;
1235 /* The following codes are handled by fixed_arithmetic. */
1241 case TRUNC_DIV_EXPR
:
1242 if (TREE_CODE (arg2
) != FIXED_CST
)
1244 f2
= TREE_FIXED_CST (arg2
);
1250 if (TREE_CODE (arg2
) != INTEGER_CST
)
1253 f2
.data
.high
= w2
.elt (1);
1254 f2
.data
.low
= w2
.elt (0);
1263 f1
= TREE_FIXED_CST (arg1
);
1264 type
= TREE_TYPE (arg1
);
1265 sat_p
= TYPE_SATURATING (type
);
1266 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1267 t
= build_fixed (type
, result
);
1268 /* Propagate overflow flags. */
1269 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1270 TREE_OVERFLOW (t
) = 1;
1274 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1276 tree type
= TREE_TYPE (arg1
);
1277 tree r1
= TREE_REALPART (arg1
);
1278 tree i1
= TREE_IMAGPART (arg1
);
1279 tree r2
= TREE_REALPART (arg2
);
1280 tree i2
= TREE_IMAGPART (arg2
);
1287 real
= const_binop (code
, r1
, r2
);
1288 imag
= const_binop (code
, i1
, i2
);
1292 if (COMPLEX_FLOAT_TYPE_P (type
))
1293 return do_mpc_arg2 (arg1
, arg2
, type
,
1294 /* do_nonfinite= */ folding_initializer
,
1297 real
= const_binop (MINUS_EXPR
,
1298 const_binop (MULT_EXPR
, r1
, r2
),
1299 const_binop (MULT_EXPR
, i1
, i2
));
1300 imag
= const_binop (PLUS_EXPR
,
1301 const_binop (MULT_EXPR
, r1
, i2
),
1302 const_binop (MULT_EXPR
, i1
, r2
));
1306 if (COMPLEX_FLOAT_TYPE_P (type
))
1307 return do_mpc_arg2 (arg1
, arg2
, type
,
1308 /* do_nonfinite= */ folding_initializer
,
1311 case TRUNC_DIV_EXPR
:
1313 case FLOOR_DIV_EXPR
:
1314 case ROUND_DIV_EXPR
:
1315 if (flag_complex_method
== 0)
1317 /* Keep this algorithm in sync with
1318 tree-complex.c:expand_complex_div_straight().
1320 Expand complex division to scalars, straightforward algorithm.
1321 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1325 = const_binop (PLUS_EXPR
,
1326 const_binop (MULT_EXPR
, r2
, r2
),
1327 const_binop (MULT_EXPR
, i2
, i2
));
1329 = const_binop (PLUS_EXPR
,
1330 const_binop (MULT_EXPR
, r1
, r2
),
1331 const_binop (MULT_EXPR
, i1
, i2
));
1333 = const_binop (MINUS_EXPR
,
1334 const_binop (MULT_EXPR
, i1
, r2
),
1335 const_binop (MULT_EXPR
, r1
, i2
));
1337 real
= const_binop (code
, t1
, magsquared
);
1338 imag
= const_binop (code
, t2
, magsquared
);
1342 /* Keep this algorithm in sync with
1343 tree-complex.c:expand_complex_div_wide().
1345 Expand complex division to scalars, modified algorithm to minimize
1346 overflow with wide input ranges. */
1347 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1348 fold_abs_const (r2
, TREE_TYPE (type
)),
1349 fold_abs_const (i2
, TREE_TYPE (type
)));
1351 if (integer_nonzerop (compare
))
1353 /* In the TRUE branch, we compute
1355 div = (br * ratio) + bi;
1356 tr = (ar * ratio) + ai;
1357 ti = (ai * ratio) - ar;
1360 tree ratio
= const_binop (code
, r2
, i2
);
1361 tree div
= const_binop (PLUS_EXPR
, i2
,
1362 const_binop (MULT_EXPR
, r2
, ratio
));
1363 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1364 real
= const_binop (PLUS_EXPR
, real
, i1
);
1365 real
= const_binop (code
, real
, div
);
1367 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1368 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1369 imag
= const_binop (code
, imag
, div
);
1373 /* In the FALSE branch, we compute
1375 divisor = (d * ratio) + c;
1376 tr = (b * ratio) + a;
1377 ti = b - (a * ratio);
1380 tree ratio
= const_binop (code
, i2
, r2
);
1381 tree div
= const_binop (PLUS_EXPR
, r2
,
1382 const_binop (MULT_EXPR
, i2
, ratio
));
1384 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1385 real
= const_binop (PLUS_EXPR
, real
, r1
);
1386 real
= const_binop (code
, real
, div
);
1388 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1389 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1390 imag
= const_binop (code
, imag
, div
);
1400 return build_complex (type
, real
, imag
);
1403 if (TREE_CODE (arg1
) == VECTOR_CST
1404 && TREE_CODE (arg2
) == VECTOR_CST
)
1406 tree type
= TREE_TYPE (arg1
);
1407 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1408 tree
*elts
= XALLOCAVEC (tree
, count
);
1410 for (i
= 0; i
< count
; i
++)
1412 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1413 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1415 elts
[i
] = const_binop (code
, elem1
, elem2
);
1417 /* It is possible that const_binop cannot handle the given
1418 code and return NULL_TREE */
1419 if (elts
[i
] == NULL_TREE
)
1423 return build_vector (type
, elts
);
1426 /* Shifts allow a scalar offset for a vector. */
1427 if (TREE_CODE (arg1
) == VECTOR_CST
1428 && TREE_CODE (arg2
) == INTEGER_CST
)
1430 tree type
= TREE_TYPE (arg1
);
1431 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1432 tree
*elts
= XALLOCAVEC (tree
, count
);
1434 for (i
= 0; i
< count
; i
++)
1436 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1438 elts
[i
] = const_binop (code
, elem1
, arg2
);
1440 /* It is possible that const_binop cannot handle the given
1441 code and return NULL_TREE. */
1442 if (elts
[i
] == NULL_TREE
)
1446 return build_vector (type
, elts
);
1451 /* Overload that adds a TYPE parameter to be able to dispatch
1452 to fold_relational_const. */
1455 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1457 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1458 return fold_relational_const (code
, type
, arg1
, arg2
);
1460 /* ??? Until we make the const_binop worker take the type of the
1461 result as argument put those cases that need it here. */
1465 if ((TREE_CODE (arg1
) == REAL_CST
1466 && TREE_CODE (arg2
) == REAL_CST
)
1467 || (TREE_CODE (arg1
) == INTEGER_CST
1468 && TREE_CODE (arg2
) == INTEGER_CST
))
1469 return build_complex (type
, arg1
, arg2
);
1472 case VEC_PACK_TRUNC_EXPR
:
1473 case VEC_PACK_FIX_TRUNC_EXPR
:
1475 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1478 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1479 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1480 if (TREE_CODE (arg1
) != VECTOR_CST
1481 || TREE_CODE (arg2
) != VECTOR_CST
)
1484 elts
= XALLOCAVEC (tree
, nelts
);
1485 if (!vec_cst_ctor_to_array (arg1
, elts
)
1486 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1489 for (i
= 0; i
< nelts
; i
++)
1491 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1492 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1493 TREE_TYPE (type
), elts
[i
]);
1494 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1498 return build_vector (type
, elts
);
1501 case VEC_WIDEN_MULT_LO_EXPR
:
1502 case VEC_WIDEN_MULT_HI_EXPR
:
1503 case VEC_WIDEN_MULT_EVEN_EXPR
:
1504 case VEC_WIDEN_MULT_ODD_EXPR
:
1506 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1507 unsigned int out
, ofs
, scale
;
1510 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1511 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1512 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1515 elts
= XALLOCAVEC (tree
, nelts
* 4);
1516 if (!vec_cst_ctor_to_array (arg1
, elts
)
1517 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1520 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1521 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1522 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1523 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1524 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1526 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1529 for (out
= 0; out
< nelts
; out
++)
1531 unsigned int in1
= (out
<< scale
) + ofs
;
1532 unsigned int in2
= in1
+ nelts
* 2;
1535 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1536 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1538 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1540 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1541 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1545 return build_vector (type
, elts
);
1551 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1554 /* Make sure type and arg0 have the same saturating flag. */
1555 gcc_checking_assert (TYPE_SATURATING (type
)
1556 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1558 return const_binop (code
, arg1
, arg2
);
1561 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1562 Return zero if computing the constants is not possible. */
1565 const_unop (enum tree_code code
, tree type
, tree arg0
)
1567 /* Don't perform the operation, other than NEGATE and ABS, if
1568 flag_signaling_nans is on and the operand is a signaling NaN. */
1569 if (TREE_CODE (arg0
) == REAL_CST
1570 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1571 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1572 && code
!= NEGATE_EXPR
1573 && code
!= ABS_EXPR
)
1580 case FIX_TRUNC_EXPR
:
1581 case FIXED_CONVERT_EXPR
:
1582 return fold_convert_const (code
, type
, arg0
);
1584 case ADDR_SPACE_CONVERT_EXPR
:
1585 /* If the source address is 0, and the source address space
1586 cannot have a valid object at 0, fold to dest type null. */
1587 if (integer_zerop (arg0
)
1588 && !(targetm
.addr_space
.zero_address_valid
1589 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1590 return fold_convert_const (code
, type
, arg0
);
1593 case VIEW_CONVERT_EXPR
:
1594 return fold_view_convert_expr (type
, arg0
);
1598 /* Can't call fold_negate_const directly here as that doesn't
1599 handle all cases and we might not be able to negate some
1601 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1602 if (tem
&& CONSTANT_CLASS_P (tem
))
1608 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1609 return fold_abs_const (arg0
, type
);
1613 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1615 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1617 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1622 if (TREE_CODE (arg0
) == INTEGER_CST
)
1623 return fold_not_const (arg0
, type
);
1624 /* Perform BIT_NOT_EXPR on each element individually. */
1625 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1629 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1631 elements
= XALLOCAVEC (tree
, count
);
1632 for (i
= 0; i
< count
; i
++)
1634 elem
= VECTOR_CST_ELT (arg0
, i
);
1635 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1636 if (elem
== NULL_TREE
)
1641 return build_vector (type
, elements
);
1645 case TRUTH_NOT_EXPR
:
1646 if (TREE_CODE (arg0
) == INTEGER_CST
)
1647 return constant_boolean_node (integer_zerop (arg0
), type
);
1651 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1652 return fold_convert (type
, TREE_REALPART (arg0
));
1656 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1657 return fold_convert (type
, TREE_IMAGPART (arg0
));
1660 case VEC_UNPACK_LO_EXPR
:
1661 case VEC_UNPACK_HI_EXPR
:
1662 case VEC_UNPACK_FLOAT_LO_EXPR
:
1663 case VEC_UNPACK_FLOAT_HI_EXPR
:
1665 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1667 enum tree_code subcode
;
1669 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1670 if (TREE_CODE (arg0
) != VECTOR_CST
)
1673 elts
= XALLOCAVEC (tree
, nelts
* 2);
1674 if (!vec_cst_ctor_to_array (arg0
, elts
))
1677 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1678 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1681 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1684 subcode
= FLOAT_EXPR
;
1686 for (i
= 0; i
< nelts
; i
++)
1688 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1689 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1693 return build_vector (type
, elts
);
1696 case REDUC_MIN_EXPR
:
1697 case REDUC_MAX_EXPR
:
1698 case REDUC_PLUS_EXPR
:
1700 unsigned int nelts
, i
;
1702 enum tree_code subcode
;
1704 if (TREE_CODE (arg0
) != VECTOR_CST
)
1706 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1708 elts
= XALLOCAVEC (tree
, nelts
);
1709 if (!vec_cst_ctor_to_array (arg0
, elts
))
1714 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1715 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1716 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1717 default: gcc_unreachable ();
1720 for (i
= 1; i
< nelts
; i
++)
1722 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1723 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1737 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1738 indicates which particular sizetype to create. */
1741 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1743 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1746 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1747 is a tree code. The type of the result is taken from the operands.
1748 Both must be equivalent integer types, ala int_binop_types_match_p.
1749 If the operands are constant, so is the result. */
1752 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1754 tree type
= TREE_TYPE (arg0
);
1756 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1757 return error_mark_node
;
1759 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1762 /* Handle the special case of two integer constants faster. */
1763 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1765 /* And some specific cases even faster than that. */
1766 if (code
== PLUS_EXPR
)
1768 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1770 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1773 else if (code
== MINUS_EXPR
)
1775 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1778 else if (code
== MULT_EXPR
)
1780 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1784 /* Handle general case of two integer constants. For sizetype
1785 constant calculations we always want to know about overflow,
1786 even in the unsigned case. */
1787 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1790 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1793 /* Given two values, either both of sizetype or both of bitsizetype,
1794 compute the difference between the two values. Return the value
1795 in signed type corresponding to the type of the operands. */
1798 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1800 tree type
= TREE_TYPE (arg0
);
1803 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1806 /* If the type is already signed, just do the simple thing. */
1807 if (!TYPE_UNSIGNED (type
))
1808 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1810 if (type
== sizetype
)
1812 else if (type
== bitsizetype
)
1813 ctype
= sbitsizetype
;
1815 ctype
= signed_type_for (type
);
1817 /* If either operand is not a constant, do the conversions to the signed
1818 type and subtract. The hardware will do the right thing with any
1819 overflow in the subtraction. */
1820 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1821 return size_binop_loc (loc
, MINUS_EXPR
,
1822 fold_convert_loc (loc
, ctype
, arg0
),
1823 fold_convert_loc (loc
, ctype
, arg1
));
1825 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1826 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1827 overflow) and negate (which can't either). Special-case a result
1828 of zero while we're here. */
1829 if (tree_int_cst_equal (arg0
, arg1
))
1830 return build_int_cst (ctype
, 0);
1831 else if (tree_int_cst_lt (arg1
, arg0
))
1832 return fold_convert_loc (loc
, ctype
,
1833 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1835 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1836 fold_convert_loc (loc
, ctype
,
1837 size_binop_loc (loc
,
1842 /* A subroutine of fold_convert_const handling conversions of an
1843 INTEGER_CST to another integer type. */
1846 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1848 /* Given an integer constant, make new constant with new type,
1849 appropriately sign-extended or truncated. Use widest_int
1850 so that any extension is done according ARG1's type. */
1851 return force_fit_type (type
, wi::to_widest (arg1
),
1852 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1853 TREE_OVERFLOW (arg1
));
1856 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1857 to an integer type. */
1860 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1862 bool overflow
= false;
1865 /* The following code implements the floating point to integer
1866 conversion rules required by the Java Language Specification,
1867 that IEEE NaNs are mapped to zero and values that overflow
1868 the target precision saturate, i.e. values greater than
1869 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1870 are mapped to INT_MIN. These semantics are allowed by the
1871 C and C++ standards that simply state that the behavior of
1872 FP-to-integer conversion is unspecified upon overflow. */
1876 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1880 case FIX_TRUNC_EXPR
:
1881 real_trunc (&r
, VOIDmode
, &x
);
1888 /* If R is NaN, return zero and show we have an overflow. */
1889 if (REAL_VALUE_ISNAN (r
))
1892 val
= wi::zero (TYPE_PRECISION (type
));
1895 /* See if R is less than the lower bound or greater than the
1900 tree lt
= TYPE_MIN_VALUE (type
);
1901 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1902 if (real_less (&r
, &l
))
1911 tree ut
= TYPE_MAX_VALUE (type
);
1914 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1915 if (real_less (&u
, &r
))
1924 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1926 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1930 /* A subroutine of fold_convert_const handling conversions of a
1931 FIXED_CST to an integer type. */
1934 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1937 double_int temp
, temp_trunc
;
1940 /* Right shift FIXED_CST to temp by fbit. */
1941 temp
= TREE_FIXED_CST (arg1
).data
;
1942 mode
= TREE_FIXED_CST (arg1
).mode
;
1943 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1945 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1946 HOST_BITS_PER_DOUBLE_INT
,
1947 SIGNED_FIXED_POINT_MODE_P (mode
));
1949 /* Left shift temp to temp_trunc by fbit. */
1950 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1951 HOST_BITS_PER_DOUBLE_INT
,
1952 SIGNED_FIXED_POINT_MODE_P (mode
));
1956 temp
= double_int_zero
;
1957 temp_trunc
= double_int_zero
;
1960 /* If FIXED_CST is negative, we need to round the value toward 0.
1961 By checking if the fractional bits are not zero to add 1 to temp. */
1962 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1963 && temp_trunc
.is_negative ()
1964 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1965 temp
+= double_int_one
;
1967 /* Given a fixed-point constant, make new constant with new type,
1968 appropriately sign-extended or truncated. */
1969 t
= force_fit_type (type
, temp
, -1,
1970 (temp
.is_negative ()
1971 && (TYPE_UNSIGNED (type
)
1972 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1973 | TREE_OVERFLOW (arg1
));
1978 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1979 to another floating point type. */
1982 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1984 REAL_VALUE_TYPE value
;
1987 /* Don't perform the operation if flag_signaling_nans is on
1988 and the operand is a signaling NaN. */
1989 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
1990 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
1993 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1994 t
= build_real (type
, value
);
1996 /* If converting an infinity or NAN to a representation that doesn't
1997 have one, set the overflow bit so that we can produce some kind of
1998 error message at the appropriate point if necessary. It's not the
1999 most user-friendly message, but it's better than nothing. */
2000 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2001 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2002 TREE_OVERFLOW (t
) = 1;
2003 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2004 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2005 TREE_OVERFLOW (t
) = 1;
2006 /* Regular overflow, conversion produced an infinity in a mode that
2007 can't represent them. */
2008 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2009 && REAL_VALUE_ISINF (value
)
2010 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2011 TREE_OVERFLOW (t
) = 1;
2013 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2017 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2018 to a floating point type. */
2021 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2023 REAL_VALUE_TYPE value
;
2026 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
2027 t
= build_real (type
, value
);
2029 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2033 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2034 to another fixed-point type. */
2037 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2039 FIXED_VALUE_TYPE value
;
2043 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2044 TYPE_SATURATING (type
));
2045 t
= build_fixed (type
, value
);
2047 /* Propagate overflow flags. */
2048 if (overflow_p
| TREE_OVERFLOW (arg1
))
2049 TREE_OVERFLOW (t
) = 1;
2053 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2054 to a fixed-point type. */
2057 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2059 FIXED_VALUE_TYPE value
;
2064 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2066 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2067 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2068 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? (HOST_WIDE_INT
) -1 : 0;
2070 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2072 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2073 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2074 TYPE_SATURATING (type
));
2075 t
= build_fixed (type
, value
);
2077 /* Propagate overflow flags. */
2078 if (overflow_p
| TREE_OVERFLOW (arg1
))
2079 TREE_OVERFLOW (t
) = 1;
2083 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2084 to a fixed-point type. */
2087 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2089 FIXED_VALUE_TYPE value
;
2093 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2094 &TREE_REAL_CST (arg1
),
2095 TYPE_SATURATING (type
));
2096 t
= build_fixed (type
, value
);
2098 /* Propagate overflow flags. */
2099 if (overflow_p
| TREE_OVERFLOW (arg1
))
2100 TREE_OVERFLOW (t
) = 1;
2104 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2105 type TYPE. If no simplification can be done return NULL_TREE. */
2108 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2110 if (TREE_TYPE (arg1
) == type
)
2113 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2114 || TREE_CODE (type
) == OFFSET_TYPE
)
2116 if (TREE_CODE (arg1
) == INTEGER_CST
)
2117 return fold_convert_const_int_from_int (type
, arg1
);
2118 else if (TREE_CODE (arg1
) == REAL_CST
)
2119 return fold_convert_const_int_from_real (code
, type
, arg1
);
2120 else if (TREE_CODE (arg1
) == FIXED_CST
)
2121 return fold_convert_const_int_from_fixed (type
, arg1
);
2123 else if (TREE_CODE (type
) == REAL_TYPE
)
2125 if (TREE_CODE (arg1
) == INTEGER_CST
)
2126 return build_real_from_int_cst (type
, arg1
);
2127 else if (TREE_CODE (arg1
) == REAL_CST
)
2128 return fold_convert_const_real_from_real (type
, arg1
);
2129 else if (TREE_CODE (arg1
) == FIXED_CST
)
2130 return fold_convert_const_real_from_fixed (type
, arg1
);
2132 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2134 if (TREE_CODE (arg1
) == FIXED_CST
)
2135 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2136 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2137 return fold_convert_const_fixed_from_int (type
, arg1
);
2138 else if (TREE_CODE (arg1
) == REAL_CST
)
2139 return fold_convert_const_fixed_from_real (type
, arg1
);
2141 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2143 if (TREE_CODE (arg1
) == VECTOR_CST
2144 && TYPE_VECTOR_SUBPARTS (type
) == VECTOR_CST_NELTS (arg1
))
2146 int len
= TYPE_VECTOR_SUBPARTS (type
);
2147 tree elttype
= TREE_TYPE (type
);
2148 tree
*v
= XALLOCAVEC (tree
, len
);
2149 for (int i
= 0; i
< len
; ++i
)
2151 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2152 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2153 if (cvt
== NULL_TREE
)
2157 return build_vector (type
, v
);
2163 /* Construct a vector of zero elements of vector type TYPE. */
2166 build_zero_vector (tree type
)
2170 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2171 return build_vector_from_val (type
, t
);
2174 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2177 fold_convertible_p (const_tree type
, const_tree arg
)
2179 tree orig
= TREE_TYPE (arg
);
2184 if (TREE_CODE (arg
) == ERROR_MARK
2185 || TREE_CODE (type
) == ERROR_MARK
2186 || TREE_CODE (orig
) == ERROR_MARK
)
2189 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2192 switch (TREE_CODE (type
))
2194 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2195 case POINTER_TYPE
: case REFERENCE_TYPE
:
2197 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2198 || TREE_CODE (orig
) == OFFSET_TYPE
);
2201 case FIXED_POINT_TYPE
:
2205 return TREE_CODE (type
) == TREE_CODE (orig
);
2212 /* Convert expression ARG to type TYPE. Used by the middle-end for
2213 simple conversions in preference to calling the front-end's convert. */
2216 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2218 tree orig
= TREE_TYPE (arg
);
2224 if (TREE_CODE (arg
) == ERROR_MARK
2225 || TREE_CODE (type
) == ERROR_MARK
2226 || TREE_CODE (orig
) == ERROR_MARK
)
2227 return error_mark_node
;
2229 switch (TREE_CODE (type
))
2232 case REFERENCE_TYPE
:
2233 /* Handle conversions between pointers to different address spaces. */
2234 if (POINTER_TYPE_P (orig
)
2235 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2236 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2237 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2240 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2242 if (TREE_CODE (arg
) == INTEGER_CST
)
2244 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2245 if (tem
!= NULL_TREE
)
2248 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2249 || TREE_CODE (orig
) == OFFSET_TYPE
)
2250 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2251 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2252 return fold_convert_loc (loc
, type
,
2253 fold_build1_loc (loc
, REALPART_EXPR
,
2254 TREE_TYPE (orig
), arg
));
2255 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2256 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2257 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2260 if (TREE_CODE (arg
) == INTEGER_CST
)
2262 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2263 if (tem
!= NULL_TREE
)
2266 else if (TREE_CODE (arg
) == REAL_CST
)
2268 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2269 if (tem
!= NULL_TREE
)
2272 else if (TREE_CODE (arg
) == FIXED_CST
)
2274 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2275 if (tem
!= NULL_TREE
)
2279 switch (TREE_CODE (orig
))
2282 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2283 case POINTER_TYPE
: case REFERENCE_TYPE
:
2284 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2287 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2289 case FIXED_POINT_TYPE
:
2290 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2293 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2294 return fold_convert_loc (loc
, type
, tem
);
2300 case FIXED_POINT_TYPE
:
2301 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2302 || TREE_CODE (arg
) == REAL_CST
)
2304 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2305 if (tem
!= NULL_TREE
)
2306 goto fold_convert_exit
;
2309 switch (TREE_CODE (orig
))
2311 case FIXED_POINT_TYPE
:
2316 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2319 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2320 return fold_convert_loc (loc
, type
, tem
);
2327 switch (TREE_CODE (orig
))
2330 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2331 case POINTER_TYPE
: case REFERENCE_TYPE
:
2333 case FIXED_POINT_TYPE
:
2334 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2335 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2336 fold_convert_loc (loc
, TREE_TYPE (type
),
2337 integer_zero_node
));
2342 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2344 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2345 TREE_OPERAND (arg
, 0));
2346 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2347 TREE_OPERAND (arg
, 1));
2348 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2351 arg
= save_expr (arg
);
2352 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2353 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2354 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2355 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2356 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2364 if (integer_zerop (arg
))
2365 return build_zero_vector (type
);
2366 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2367 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2368 || TREE_CODE (orig
) == VECTOR_TYPE
);
2369 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2372 tem
= fold_ignored_result (arg
);
2373 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2376 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2377 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2381 protected_set_expr_location_unshare (tem
, loc
);
2385 /* Return false if expr can be assumed not to be an lvalue, true
2389 maybe_lvalue_p (const_tree x
)
2391 /* We only need to wrap lvalue tree codes. */
2392 switch (TREE_CODE (x
))
2405 case ARRAY_RANGE_REF
:
2411 case PREINCREMENT_EXPR
:
2412 case PREDECREMENT_EXPR
:
2414 case TRY_CATCH_EXPR
:
2415 case WITH_CLEANUP_EXPR
:
2424 /* Assume the worst for front-end tree codes. */
2425 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2433 /* Return an expr equal to X but certainly not valid as an lvalue. */
2436 non_lvalue_loc (location_t loc
, tree x
)
2438 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2443 if (! maybe_lvalue_p (x
))
2445 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2448 /* When pedantic, return an expr equal to X but certainly not valid as a
2449 pedantic lvalue. Otherwise, return X. */
2452 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2454 return protected_set_expr_location_unshare (x
, loc
);
2457 /* Given a tree comparison code, return the code that is the logical inverse.
2458 It is generally not safe to do this for floating-point comparisons, except
2459 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2460 ERROR_MARK in this case. */
2463 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2465 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2466 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2476 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2478 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2480 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2482 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2496 return UNORDERED_EXPR
;
2497 case UNORDERED_EXPR
:
2498 return ORDERED_EXPR
;
2504 /* Similar, but return the comparison that results if the operands are
2505 swapped. This is safe for floating-point. */
2508 swap_tree_comparison (enum tree_code code
)
2515 case UNORDERED_EXPR
:
2541 /* Convert a comparison tree code from an enum tree_code representation
2542 into a compcode bit-based encoding. This function is the inverse of
2543 compcode_to_comparison. */
2545 static enum comparison_code
2546 comparison_to_compcode (enum tree_code code
)
2563 return COMPCODE_ORD
;
2564 case UNORDERED_EXPR
:
2565 return COMPCODE_UNORD
;
2567 return COMPCODE_UNLT
;
2569 return COMPCODE_UNEQ
;
2571 return COMPCODE_UNLE
;
2573 return COMPCODE_UNGT
;
2575 return COMPCODE_LTGT
;
2577 return COMPCODE_UNGE
;
2583 /* Convert a compcode bit-based encoding of a comparison operator back
2584 to GCC's enum tree_code representation. This function is the
2585 inverse of comparison_to_compcode. */
2587 static enum tree_code
2588 compcode_to_comparison (enum comparison_code code
)
2605 return ORDERED_EXPR
;
2606 case COMPCODE_UNORD
:
2607 return UNORDERED_EXPR
;
2625 /* Return a tree for the comparison which is the combination of
2626 doing the AND or OR (depending on CODE) of the two operations LCODE
2627 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2628 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2629 if this makes the transformation invalid. */
2632 combine_comparisons (location_t loc
,
2633 enum tree_code code
, enum tree_code lcode
,
2634 enum tree_code rcode
, tree truth_type
,
2635 tree ll_arg
, tree lr_arg
)
2637 bool honor_nans
= HONOR_NANS (ll_arg
);
2638 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2639 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2644 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2645 compcode
= lcompcode
& rcompcode
;
2648 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2649 compcode
= lcompcode
| rcompcode
;
2658 /* Eliminate unordered comparisons, as well as LTGT and ORD
2659 which are not used unless the mode has NaNs. */
2660 compcode
&= ~COMPCODE_UNORD
;
2661 if (compcode
== COMPCODE_LTGT
)
2662 compcode
= COMPCODE_NE
;
2663 else if (compcode
== COMPCODE_ORD
)
2664 compcode
= COMPCODE_TRUE
;
2666 else if (flag_trapping_math
)
2668 /* Check that the original operation and the optimized ones will trap
2669 under the same condition. */
2670 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2671 && (lcompcode
!= COMPCODE_EQ
)
2672 && (lcompcode
!= COMPCODE_ORD
);
2673 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2674 && (rcompcode
!= COMPCODE_EQ
)
2675 && (rcompcode
!= COMPCODE_ORD
);
2676 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2677 && (compcode
!= COMPCODE_EQ
)
2678 && (compcode
!= COMPCODE_ORD
);
2680 /* In a short-circuited boolean expression the LHS might be
2681 such that the RHS, if evaluated, will never trap. For
2682 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2683 if neither x nor y is NaN. (This is a mixed blessing: for
2684 example, the expression above will never trap, hence
2685 optimizing it to x < y would be invalid). */
2686 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2687 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2690 /* If the comparison was short-circuited, and only the RHS
2691 trapped, we may now generate a spurious trap. */
2693 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2696 /* If we changed the conditions that cause a trap, we lose. */
2697 if ((ltrap
|| rtrap
) != trap
)
2701 if (compcode
== COMPCODE_TRUE
)
2702 return constant_boolean_node (true, truth_type
);
2703 else if (compcode
== COMPCODE_FALSE
)
2704 return constant_boolean_node (false, truth_type
);
2707 enum tree_code tcode
;
2709 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2710 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2714 /* Return nonzero if two operands (typically of the same tree node)
2715 are necessarily equal. FLAGS modifies behavior as follows:
2717 If OEP_ONLY_CONST is set, only return nonzero for constants.
2718 This function tests whether the operands are indistinguishable;
2719 it does not test whether they are equal using C's == operation.
2720 The distinction is important for IEEE floating point, because
2721 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2722 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2724 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2725 even though it may hold multiple values during a function.
2726 This is because a GCC tree node guarantees that nothing else is
2727 executed between the evaluation of its "operands" (which may often
2728 be evaluated in arbitrary order). Hence if the operands themselves
2729 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2730 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2731 unset means assuming isochronic (or instantaneous) tree equivalence.
2732 Unless comparing arbitrary expression trees, such as from different
2733 statements, this flag can usually be left unset.
2735 If OEP_PURE_SAME is set, then pure functions with identical arguments
2736 are considered the same. It is used when the caller has other ways
2737 to ensure that global memory is unchanged in between.
2739 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2740 not values of expressions.
2742 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2743 any operand with side effect. This is unnecesarily conservative in the
2744 case we know that arg0 and arg1 are in disjoint code paths (such as in
2745 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2746 addresses with TREE_CONSTANT flag set so we know that &var == &var
2747 even if var is volatile. */
2750 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2752 /* If either is ERROR_MARK, they aren't equal. */
2753 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2754 || TREE_TYPE (arg0
) == error_mark_node
2755 || TREE_TYPE (arg1
) == error_mark_node
)
2758 /* Similar, if either does not have a type (like a released SSA name),
2759 they aren't equal. */
2760 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2763 /* We cannot consider pointers to different address space equal. */
2764 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2765 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2766 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2767 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2770 /* Check equality of integer constants before bailing out due to
2771 precision differences. */
2772 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2774 /* Address of INTEGER_CST is not defined; check that we did not forget
2775 to drop the OEP_ADDRESS_OF flags. */
2776 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2777 return tree_int_cst_equal (arg0
, arg1
);
2780 if (!(flags
& OEP_ADDRESS_OF
))
2782 /* If both types don't have the same signedness, then we can't consider
2783 them equal. We must check this before the STRIP_NOPS calls
2784 because they may change the signedness of the arguments. As pointers
2785 strictly don't have a signedness, require either two pointers or
2786 two non-pointers as well. */
2787 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2788 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2789 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2792 /* If both types don't have the same precision, then it is not safe
2794 if (element_precision (TREE_TYPE (arg0
))
2795 != element_precision (TREE_TYPE (arg1
)))
2802 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2803 sanity check once the issue is solved. */
2805 /* Addresses of conversions and SSA_NAMEs (and many other things)
2806 are not defined. Check that we did not forget to drop the
2807 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2808 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2809 && TREE_CODE (arg0
) != SSA_NAME
);
2812 /* In case both args are comparisons but with different comparison
2813 code, try to swap the comparison operands of one arg to produce
2814 a match and compare that variant. */
2815 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2816 && COMPARISON_CLASS_P (arg0
)
2817 && COMPARISON_CLASS_P (arg1
))
2819 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2821 if (TREE_CODE (arg0
) == swap_code
)
2822 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2823 TREE_OPERAND (arg1
, 1), flags
)
2824 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2825 TREE_OPERAND (arg1
, 0), flags
);
2828 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2830 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2831 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2833 else if (flags
& OEP_ADDRESS_OF
)
2835 /* If we are interested in comparing addresses ignore
2836 MEM_REF wrappings of the base that can appear just for
2838 if (TREE_CODE (arg0
) == MEM_REF
2840 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2841 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2842 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2844 else if (TREE_CODE (arg1
) == MEM_REF
2846 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2847 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2848 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2856 /* When not checking adddresses, this is needed for conversions and for
2857 COMPONENT_REF. Might as well play it safe and always test this. */
2858 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2859 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2860 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2861 && !(flags
& OEP_ADDRESS_OF
)))
2864 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2865 We don't care about side effects in that case because the SAVE_EXPR
2866 takes care of that for us. In all other cases, two expressions are
2867 equal if they have no side effects. If we have two identical
2868 expressions with side effects that should be treated the same due
2869 to the only side effects being identical SAVE_EXPR's, that will
2870 be detected in the recursive calls below.
2871 If we are taking an invariant address of two identical objects
2872 they are necessarily equal as well. */
2873 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2874 && (TREE_CODE (arg0
) == SAVE_EXPR
2875 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
2876 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2879 /* Next handle constant cases, those for which we can return 1 even
2880 if ONLY_CONST is set. */
2881 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2882 switch (TREE_CODE (arg0
))
2885 return tree_int_cst_equal (arg0
, arg1
);
2888 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2889 TREE_FIXED_CST (arg1
));
2892 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2896 if (!HONOR_SIGNED_ZEROS (arg0
))
2898 /* If we do not distinguish between signed and unsigned zero,
2899 consider them equal. */
2900 if (real_zerop (arg0
) && real_zerop (arg1
))
2909 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2912 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2914 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2915 VECTOR_CST_ELT (arg1
, i
), flags
))
2922 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2924 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2928 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2929 && ! memcmp (TREE_STRING_POINTER (arg0
),
2930 TREE_STRING_POINTER (arg1
),
2931 TREE_STRING_LENGTH (arg0
)));
2934 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2935 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2936 flags
| OEP_ADDRESS_OF
2937 | OEP_MATCH_SIDE_EFFECTS
);
2939 /* In GIMPLE empty constructors are allowed in initializers of
2941 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0
))
2942 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1
)));
2947 if (flags
& OEP_ONLY_CONST
)
2950 /* Define macros to test an operand from arg0 and arg1 for equality and a
2951 variant that allows null and views null as being different from any
2952 non-null value. In the latter case, if either is null, the both
2953 must be; otherwise, do the normal comparison. */
2954 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2955 TREE_OPERAND (arg1, N), flags)
2957 #define OP_SAME_WITH_NULL(N) \
2958 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2959 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2961 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2964 /* Two conversions are equal only if signedness and modes match. */
2965 switch (TREE_CODE (arg0
))
2968 case FIX_TRUNC_EXPR
:
2969 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2970 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
2980 case tcc_comparison
:
2982 if (OP_SAME (0) && OP_SAME (1))
2985 /* For commutative ops, allow the other order. */
2986 return (commutative_tree_code (TREE_CODE (arg0
))
2987 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2988 TREE_OPERAND (arg1
, 1), flags
)
2989 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2990 TREE_OPERAND (arg1
, 0), flags
));
2993 /* If either of the pointer (or reference) expressions we are
2994 dereferencing contain a side effect, these cannot be equal,
2995 but their addresses can be. */
2996 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
2997 && (TREE_SIDE_EFFECTS (arg0
)
2998 || TREE_SIDE_EFFECTS (arg1
)))
3001 switch (TREE_CODE (arg0
))
3004 if (!(flags
& OEP_ADDRESS_OF
)
3005 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3006 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3008 flags
&= ~OEP_ADDRESS_OF
;
3012 /* Require the same offset. */
3013 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3014 TYPE_SIZE (TREE_TYPE (arg1
)),
3015 flags
& ~OEP_ADDRESS_OF
))
3020 case VIEW_CONVERT_EXPR
:
3023 case TARGET_MEM_REF
:
3025 if (!(flags
& OEP_ADDRESS_OF
))
3027 /* Require equal access sizes */
3028 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3029 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3030 || !TYPE_SIZE (TREE_TYPE (arg1
))
3031 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3032 TYPE_SIZE (TREE_TYPE (arg1
)),
3035 /* Verify that access happens in similar types. */
3036 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3038 /* Verify that accesses are TBAA compatible. */
3039 if (!alias_ptr_types_compatible_p
3040 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3041 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3042 || (MR_DEPENDENCE_CLIQUE (arg0
)
3043 != MR_DEPENDENCE_CLIQUE (arg1
))
3044 || (MR_DEPENDENCE_BASE (arg0
)
3045 != MR_DEPENDENCE_BASE (arg1
)))
3047 /* Verify that alignment is compatible. */
3048 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3049 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3052 flags
&= ~OEP_ADDRESS_OF
;
3053 return (OP_SAME (0) && OP_SAME (1)
3054 /* TARGET_MEM_REF require equal extra operands. */
3055 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3056 || (OP_SAME_WITH_NULL (2)
3057 && OP_SAME_WITH_NULL (3)
3058 && OP_SAME_WITH_NULL (4))));
3061 case ARRAY_RANGE_REF
:
3064 flags
&= ~OEP_ADDRESS_OF
;
3065 /* Compare the array index by value if it is constant first as we
3066 may have different types but same value here. */
3067 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3068 TREE_OPERAND (arg1
, 1))
3070 && OP_SAME_WITH_NULL (2)
3071 && OP_SAME_WITH_NULL (3)
3072 /* Compare low bound and element size as with OEP_ADDRESS_OF
3073 we have to account for the offset of the ref. */
3074 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3075 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3076 || (operand_equal_p (array_ref_low_bound
3077 (CONST_CAST_TREE (arg0
)),
3079 (CONST_CAST_TREE (arg1
)), flags
)
3080 && operand_equal_p (array_ref_element_size
3081 (CONST_CAST_TREE (arg0
)),
3082 array_ref_element_size
3083 (CONST_CAST_TREE (arg1
)),
3087 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3088 may be NULL when we're called to compare MEM_EXPRs. */
3089 if (!OP_SAME_WITH_NULL (0)
3092 flags
&= ~OEP_ADDRESS_OF
;
3093 return OP_SAME_WITH_NULL (2);
3098 flags
&= ~OEP_ADDRESS_OF
;
3099 return OP_SAME (1) && OP_SAME (2);
3105 case tcc_expression
:
3106 switch (TREE_CODE (arg0
))
3109 /* Be sure we pass right ADDRESS_OF flag. */
3110 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3111 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3112 TREE_OPERAND (arg1
, 0),
3113 flags
| OEP_ADDRESS_OF
);
3115 case TRUTH_NOT_EXPR
:
3118 case TRUTH_ANDIF_EXPR
:
3119 case TRUTH_ORIF_EXPR
:
3120 return OP_SAME (0) && OP_SAME (1);
3123 case WIDEN_MULT_PLUS_EXPR
:
3124 case WIDEN_MULT_MINUS_EXPR
:
3127 /* The multiplcation operands are commutative. */
3130 case TRUTH_AND_EXPR
:
3132 case TRUTH_XOR_EXPR
:
3133 if (OP_SAME (0) && OP_SAME (1))
3136 /* Otherwise take into account this is a commutative operation. */
3137 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3138 TREE_OPERAND (arg1
, 1), flags
)
3139 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3140 TREE_OPERAND (arg1
, 0), flags
));
3143 if (! OP_SAME (1) || ! OP_SAME (2))
3145 flags
&= ~OEP_ADDRESS_OF
;
3150 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3157 switch (TREE_CODE (arg0
))
3160 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3161 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3162 /* If not both CALL_EXPRs are either internal or normal function
3163 functions, then they are not equal. */
3165 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3167 /* If the CALL_EXPRs call different internal functions, then they
3169 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3174 /* If the CALL_EXPRs call different functions, then they are not
3176 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3181 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3183 unsigned int cef
= call_expr_flags (arg0
);
3184 if (flags
& OEP_PURE_SAME
)
3185 cef
&= ECF_CONST
| ECF_PURE
;
3192 /* Now see if all the arguments are the same. */
3194 const_call_expr_arg_iterator iter0
, iter1
;
3196 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3197 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3199 a0
= next_const_call_expr_arg (&iter0
),
3200 a1
= next_const_call_expr_arg (&iter1
))
3201 if (! operand_equal_p (a0
, a1
, flags
))
3204 /* If we get here and both argument lists are exhausted
3205 then the CALL_EXPRs are equal. */
3206 return ! (a0
|| a1
);
3212 case tcc_declaration
:
3213 /* Consider __builtin_sqrt equal to sqrt. */
3214 return (TREE_CODE (arg0
) == FUNCTION_DECL
3215 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3216 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3217 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3219 case tcc_exceptional
:
3220 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3222 /* In GIMPLE constructors are used only to build vectors from
3223 elements. Individual elements in the constructor must be
3224 indexed in increasing order and form an initial sequence.
3226 We make no effort to compare constructors in generic.
3227 (see sem_variable::equals in ipa-icf which can do so for
3229 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3230 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3233 /* Be sure that vectors constructed have the same representation.
3234 We only tested element precision and modes to match.
3235 Vectors may be BLKmode and thus also check that the number of
3237 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3238 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3241 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3242 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3243 unsigned int len
= vec_safe_length (v0
);
3245 if (len
!= vec_safe_length (v1
))
3248 for (unsigned int i
= 0; i
< len
; i
++)
3250 constructor_elt
*c0
= &(*v0
)[i
];
3251 constructor_elt
*c1
= &(*v1
)[i
];
3253 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3254 /* In GIMPLE the indexes can be either NULL or matching i.
3255 Double check this so we won't get false
3256 positives for GENERIC. */
3258 && (TREE_CODE (c0
->index
) != INTEGER_CST
3259 || !compare_tree_int (c0
->index
, i
)))
3261 && (TREE_CODE (c1
->index
) != INTEGER_CST
3262 || !compare_tree_int (c1
->index
, i
))))
3274 #undef OP_SAME_WITH_NULL
3277 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3278 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3280 When in doubt, return 0. */
3283 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3285 int unsignedp1
, unsignedpo
;
3286 tree primarg0
, primarg1
, primother
;
3287 unsigned int correct_width
;
3289 if (operand_equal_p (arg0
, arg1
, 0))
3292 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3293 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3296 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3297 and see if the inner values are the same. This removes any
3298 signedness comparison, which doesn't matter here. */
3299 primarg0
= arg0
, primarg1
= arg1
;
3300 STRIP_NOPS (primarg0
);
3301 STRIP_NOPS (primarg1
);
3302 if (operand_equal_p (primarg0
, primarg1
, 0))
3305 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3306 actual comparison operand, ARG0.
3308 First throw away any conversions to wider types
3309 already present in the operands. */
3311 primarg1
= get_narrower (arg1
, &unsignedp1
);
3312 primother
= get_narrower (other
, &unsignedpo
);
3314 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3315 if (unsignedp1
== unsignedpo
3316 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3317 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3319 tree type
= TREE_TYPE (arg0
);
3321 /* Make sure shorter operand is extended the right way
3322 to match the longer operand. */
3323 primarg1
= fold_convert (signed_or_unsigned_type_for
3324 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3326 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3333 /* See if ARG is an expression that is either a comparison or is performing
3334 arithmetic on comparisons. The comparisons must only be comparing
3335 two different values, which will be stored in *CVAL1 and *CVAL2; if
3336 they are nonzero it means that some operands have already been found.
3337 No variables may be used anywhere else in the expression except in the
3338 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3339 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3341 If this is true, return 1. Otherwise, return zero. */
3344 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3346 enum tree_code code
= TREE_CODE (arg
);
3347 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3349 /* We can handle some of the tcc_expression cases here. */
3350 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3352 else if (tclass
== tcc_expression
3353 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3354 || code
== COMPOUND_EXPR
))
3355 tclass
= tcc_binary
;
3357 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3358 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3360 /* If we've already found a CVAL1 or CVAL2, this expression is
3361 two complex to handle. */
3362 if (*cval1
|| *cval2
)
3372 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3375 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3376 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3377 cval1
, cval2
, save_p
));
3382 case tcc_expression
:
3383 if (code
== COND_EXPR
)
3384 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3385 cval1
, cval2
, save_p
)
3386 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3387 cval1
, cval2
, save_p
)
3388 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3389 cval1
, cval2
, save_p
));
3392 case tcc_comparison
:
3393 /* First see if we can handle the first operand, then the second. For
3394 the second operand, we know *CVAL1 can't be zero. It must be that
3395 one side of the comparison is each of the values; test for the
3396 case where this isn't true by failing if the two operands
3399 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3400 TREE_OPERAND (arg
, 1), 0))
3404 *cval1
= TREE_OPERAND (arg
, 0);
3405 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3407 else if (*cval2
== 0)
3408 *cval2
= TREE_OPERAND (arg
, 0);
3409 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3414 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3416 else if (*cval2
== 0)
3417 *cval2
= TREE_OPERAND (arg
, 1);
3418 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3430 /* ARG is a tree that is known to contain just arithmetic operations and
3431 comparisons. Evaluate the operations in the tree substituting NEW0 for
3432 any occurrence of OLD0 as an operand of a comparison and likewise for
3436 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3437 tree old1
, tree new1
)
3439 tree type
= TREE_TYPE (arg
);
3440 enum tree_code code
= TREE_CODE (arg
);
3441 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3443 /* We can handle some of the tcc_expression cases here. */
3444 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3446 else if (tclass
== tcc_expression
3447 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3448 tclass
= tcc_binary
;
3453 return fold_build1_loc (loc
, code
, type
,
3454 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3455 old0
, new0
, old1
, new1
));
3458 return fold_build2_loc (loc
, code
, type
,
3459 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3460 old0
, new0
, old1
, new1
),
3461 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3462 old0
, new0
, old1
, new1
));
3464 case tcc_expression
:
3468 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3472 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3476 return fold_build3_loc (loc
, code
, type
,
3477 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3478 old0
, new0
, old1
, new1
),
3479 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3480 old0
, new0
, old1
, new1
),
3481 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3482 old0
, new0
, old1
, new1
));
3486 /* Fall through - ??? */
3488 case tcc_comparison
:
3490 tree arg0
= TREE_OPERAND (arg
, 0);
3491 tree arg1
= TREE_OPERAND (arg
, 1);
3493 /* We need to check both for exact equality and tree equality. The
3494 former will be true if the operand has a side-effect. In that
3495 case, we know the operand occurred exactly once. */
3497 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3499 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3502 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3504 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3507 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3515 /* Return a tree for the case when the result of an expression is RESULT
3516 converted to TYPE and OMITTED was previously an operand of the expression
3517 but is now not needed (e.g., we folded OMITTED * 0).
3519 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3520 the conversion of RESULT to TYPE. */
3523 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3525 tree t
= fold_convert_loc (loc
, type
, result
);
3527 /* If the resulting operand is an empty statement, just return the omitted
3528 statement casted to void. */
3529 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3530 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3531 fold_ignored_result (omitted
));
3533 if (TREE_SIDE_EFFECTS (omitted
))
3534 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3535 fold_ignored_result (omitted
), t
);
3537 return non_lvalue_loc (loc
, t
);
3540 /* Return a tree for the case when the result of an expression is RESULT
3541 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3542 of the expression but are now not needed.
3544 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3545 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3546 evaluated before OMITTED2. Otherwise, if neither has side effects,
3547 just do the conversion of RESULT to TYPE. */
3550 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3551 tree omitted1
, tree omitted2
)
3553 tree t
= fold_convert_loc (loc
, type
, result
);
3555 if (TREE_SIDE_EFFECTS (omitted2
))
3556 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3557 if (TREE_SIDE_EFFECTS (omitted1
))
3558 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3560 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3564 /* Return a simplified tree node for the truth-negation of ARG. This
3565 never alters ARG itself. We assume that ARG is an operation that
3566 returns a truth value (0 or 1).
3568 FIXME: one would think we would fold the result, but it causes
3569 problems with the dominator optimizer. */
3572 fold_truth_not_expr (location_t loc
, tree arg
)
3574 tree type
= TREE_TYPE (arg
);
3575 enum tree_code code
= TREE_CODE (arg
);
3576 location_t loc1
, loc2
;
3578 /* If this is a comparison, we can simply invert it, except for
3579 floating-point non-equality comparisons, in which case we just
3580 enclose a TRUTH_NOT_EXPR around what we have. */
3582 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3584 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3585 if (FLOAT_TYPE_P (op_type
)
3586 && flag_trapping_math
3587 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3588 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3591 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3592 if (code
== ERROR_MARK
)
3595 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3596 TREE_OPERAND (arg
, 1));
3597 if (TREE_NO_WARNING (arg
))
3598 TREE_NO_WARNING (ret
) = 1;
3605 return constant_boolean_node (integer_zerop (arg
), type
);
3607 case TRUTH_AND_EXPR
:
3608 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3609 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3610 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3611 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3612 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3615 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3616 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3617 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3618 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3619 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3621 case TRUTH_XOR_EXPR
:
3622 /* Here we can invert either operand. We invert the first operand
3623 unless the second operand is a TRUTH_NOT_EXPR in which case our
3624 result is the XOR of the first operand with the inside of the
3625 negation of the second operand. */
3627 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3628 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3629 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3631 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3632 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3633 TREE_OPERAND (arg
, 1));
3635 case TRUTH_ANDIF_EXPR
:
3636 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3637 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3638 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3639 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3640 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3642 case TRUTH_ORIF_EXPR
:
3643 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3644 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3645 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3646 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3647 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3649 case TRUTH_NOT_EXPR
:
3650 return TREE_OPERAND (arg
, 0);
3654 tree arg1
= TREE_OPERAND (arg
, 1);
3655 tree arg2
= TREE_OPERAND (arg
, 2);
3657 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3658 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3660 /* A COND_EXPR may have a throw as one operand, which
3661 then has void type. Just leave void operands
3663 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3664 VOID_TYPE_P (TREE_TYPE (arg1
))
3665 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3666 VOID_TYPE_P (TREE_TYPE (arg2
))
3667 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3671 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3672 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3673 TREE_OPERAND (arg
, 0),
3674 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3676 case NON_LVALUE_EXPR
:
3677 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3678 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3681 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3682 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3684 /* ... fall through ... */
3687 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3688 return build1_loc (loc
, TREE_CODE (arg
), type
,
3689 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3692 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3694 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3697 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3699 case CLEANUP_POINT_EXPR
:
3700 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3701 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3702 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3709 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3710 assume that ARG is an operation that returns a truth value (0 or 1
3711 for scalars, 0 or -1 for vectors). Return the folded expression if
3712 folding is successful. Otherwise, return NULL_TREE. */
3715 fold_invert_truthvalue (location_t loc
, tree arg
)
3717 tree type
= TREE_TYPE (arg
);
3718 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3724 /* Return a simplified tree node for the truth-negation of ARG. This
3725 never alters ARG itself. We assume that ARG is an operation that
3726 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3729 invert_truthvalue_loc (location_t loc
, tree arg
)
3731 if (TREE_CODE (arg
) == ERROR_MARK
)
3734 tree type
= TREE_TYPE (arg
);
3735 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3741 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3742 with code CODE. This optimization is unsafe. */
3744 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3745 tree arg0
, tree arg1
)
3747 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3748 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3750 /* (A / C) +- (B / C) -> (A +- B) / C. */
3752 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3753 TREE_OPERAND (arg1
, 1), 0))
3754 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3755 fold_build2_loc (loc
, code
, type
,
3756 TREE_OPERAND (arg0
, 0),
3757 TREE_OPERAND (arg1
, 0)),
3758 TREE_OPERAND (arg0
, 1));
3760 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3761 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3762 TREE_OPERAND (arg1
, 0), 0)
3763 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3764 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3766 REAL_VALUE_TYPE r0
, r1
;
3767 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3768 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3770 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3772 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3773 real_arithmetic (&r0
, code
, &r0
, &r1
);
3774 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3775 TREE_OPERAND (arg0
, 0),
3776 build_real (type
, r0
));
3782 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3783 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3784 and uses reverse storage order if REVERSEP is nonzero. */
3787 make_bit_field_ref (location_t loc
, tree inner
, tree type
,
3788 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
3789 int unsignedp
, int reversep
)
3791 tree result
, bftype
;
3793 if (bitpos
== 0 && !reversep
)
3795 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3796 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3797 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3798 && tree_fits_shwi_p (size
)
3799 && tree_to_shwi (size
) == bitsize
)
3800 return fold_convert_loc (loc
, type
, inner
);
3804 if (TYPE_PRECISION (bftype
) != bitsize
3805 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3806 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3808 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3809 size_int (bitsize
), bitsize_int (bitpos
));
3810 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
3813 result
= fold_convert_loc (loc
, type
, result
);
3818 /* Optimize a bit-field compare.
3820 There are two cases: First is a compare against a constant and the
3821 second is a comparison of two items where the fields are at the same
3822 bit position relative to the start of a chunk (byte, halfword, word)
3823 large enough to contain it. In these cases we can avoid the shift
3824 implicit in bitfield extractions.
3826 For constants, we emit a compare of the shifted constant with the
3827 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3828 compared. For two fields at the same position, we do the ANDs with the
3829 similar mask and compare the result of the ANDs.
3831 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3832 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3833 are the left and right operands of the comparison, respectively.
3835 If the optimization described above can be done, we return the resulting
3836 tree. Otherwise we return zero. */
3839 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3840 tree compare_type
, tree lhs
, tree rhs
)
3842 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3843 tree type
= TREE_TYPE (lhs
);
3845 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3846 machine_mode lmode
, rmode
, nmode
;
3847 int lunsignedp
, runsignedp
;
3848 int lreversep
, rreversep
;
3849 int lvolatilep
= 0, rvolatilep
= 0;
3850 tree linner
, rinner
= NULL_TREE
;
3854 /* Get all the information about the extractions being done. If the bit size
3855 if the same as the size of the underlying object, we aren't doing an
3856 extraction at all and so can do nothing. We also don't want to
3857 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3858 then will no longer be able to replace it. */
3859 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3860 &lunsignedp
, &lreversep
, &lvolatilep
, false);
3861 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3862 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3866 rreversep
= lreversep
;
3869 /* If this is not a constant, we can only do something if bit positions,
3870 sizes, signedness and storage order are the same. */
3872 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3873 &runsignedp
, &rreversep
, &rvolatilep
, false);
3875 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3876 || lunsignedp
!= runsignedp
|| lreversep
!= rreversep
|| offset
!= 0
3877 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3881 /* See if we can find a mode to refer to this field. We should be able to,
3882 but fail if we can't. */
3883 nmode
= get_best_mode (lbitsize
, lbitpos
, 0, 0,
3884 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3885 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3886 TYPE_ALIGN (TREE_TYPE (rinner
))),
3888 if (nmode
== VOIDmode
)
3891 /* Set signed and unsigned types of the precision of this mode for the
3893 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3895 /* Compute the bit position and size for the new reference and our offset
3896 within it. If the new reference is the same size as the original, we
3897 won't optimize anything, so return zero. */
3898 nbitsize
= GET_MODE_BITSIZE (nmode
);
3899 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3901 if (nbitsize
== lbitsize
)
3904 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
3905 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3907 /* Make the mask to be used against the extracted field. */
3908 mask
= build_int_cst_type (unsigned_type
, -1);
3909 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3910 mask
= const_binop (RSHIFT_EXPR
, mask
,
3911 size_int (nbitsize
- lbitsize
- lbitpos
));
3914 /* If not comparing with constant, just rework the comparison
3916 return fold_build2_loc (loc
, code
, compare_type
,
3917 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3918 make_bit_field_ref (loc
, linner
,
3923 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3924 make_bit_field_ref (loc
, rinner
,
3930 /* Otherwise, we are handling the constant case. See if the constant is too
3931 big for the field. Warn and return a tree for 0 (false) if so. We do
3932 this not only for its own sake, but to avoid having to test for this
3933 error case below. If we didn't, we might generate wrong code.
3935 For unsigned fields, the constant shifted right by the field length should
3936 be all zero. For signed fields, the high-order bits should agree with
3941 if (wi::lrshift (rhs
, lbitsize
) != 0)
3943 warning (0, "comparison is always %d due to width of bit-field",
3945 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3950 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
3951 if (tem
!= 0 && tem
!= -1)
3953 warning (0, "comparison is always %d due to width of bit-field",
3955 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3959 /* Single-bit compares should always be against zero. */
3960 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3962 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3963 rhs
= build_int_cst (type
, 0);
3966 /* Make a new bitfield reference, shift the constant over the
3967 appropriate number of bits and mask it with the computed mask
3968 (in case this was a signed field). If we changed it, make a new one. */
3969 lhs
= make_bit_field_ref (loc
, linner
, unsigned_type
, nbitsize
, nbitpos
, 1,
3972 rhs
= const_binop (BIT_AND_EXPR
,
3973 const_binop (LSHIFT_EXPR
,
3974 fold_convert_loc (loc
, unsigned_type
, rhs
),
3975 size_int (lbitpos
)),
3978 lhs
= build2_loc (loc
, code
, compare_type
,
3979 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
3983 /* Subroutine for fold_truth_andor_1: decode a field reference.
3985 If EXP is a comparison reference, we return the innermost reference.
3987 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3988 set to the starting bit number.
3990 If the innermost field can be completely contained in a mode-sized
3991 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3993 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3994 otherwise it is not changed.
3996 *PUNSIGNEDP is set to the signedness of the field.
3998 *PREVERSEP is set to the storage order of the field.
4000 *PMASK is set to the mask used. This is either contained in a
4001 BIT_AND_EXPR or derived from the width of the field.
4003 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4005 Return 0 if this is not a component reference or is one that we can't
4006 do anything with. */
4009 decode_field_reference (location_t loc
, tree exp
, HOST_WIDE_INT
*pbitsize
,
4010 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4011 int *punsignedp
, int *preversep
, int *pvolatilep
,
4012 tree
*pmask
, tree
*pand_mask
)
4014 tree outer_type
= 0;
4016 tree mask
, inner
, offset
;
4018 unsigned int precision
;
4020 /* All the optimizations using this function assume integer fields.
4021 There are problems with FP fields since the type_for_size call
4022 below can fail for, e.g., XFmode. */
4023 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4026 /* We are interested in the bare arrangement of bits, so strip everything
4027 that doesn't affect the machine mode. However, record the type of the
4028 outermost expression if it may matter below. */
4029 if (CONVERT_EXPR_P (exp
)
4030 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4031 outer_type
= TREE_TYPE (exp
);
4034 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4036 and_mask
= TREE_OPERAND (exp
, 1);
4037 exp
= TREE_OPERAND (exp
, 0);
4038 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4039 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4043 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
4044 punsignedp
, preversep
, pvolatilep
, false);
4045 if ((inner
== exp
&& and_mask
== 0)
4046 || *pbitsize
< 0 || offset
!= 0
4047 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
4050 /* If the number of bits in the reference is the same as the bitsize of
4051 the outer type, then the outer type gives the signedness. Otherwise
4052 (in case of a small bitfield) the signedness is unchanged. */
4053 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4054 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4056 /* Compute the mask to access the bitfield. */
4057 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4058 precision
= TYPE_PRECISION (unsigned_type
);
4060 mask
= build_int_cst_type (unsigned_type
, -1);
4062 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4063 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4065 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4067 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4068 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4071 *pand_mask
= and_mask
;
4075 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4076 bit positions and MASK is SIGNED. */
4079 all_ones_mask_p (const_tree mask
, unsigned int size
)
4081 tree type
= TREE_TYPE (mask
);
4082 unsigned int precision
= TYPE_PRECISION (type
);
4084 /* If this function returns true when the type of the mask is
4085 UNSIGNED, then there will be errors. In particular see
4086 gcc.c-torture/execute/990326-1.c. There does not appear to be
4087 any documentation paper trail as to why this is so. But the pre
4088 wide-int worked with that restriction and it has been preserved
4090 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4093 return wi::mask (size
, false, precision
) == mask
;
4096 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4097 represents the sign bit of EXP's type. If EXP represents a sign
4098 or zero extension, also test VAL against the unextended type.
4099 The return value is the (sub)expression whose sign bit is VAL,
4100 or NULL_TREE otherwise. */
4103 sign_bit_p (tree exp
, const_tree val
)
4108 /* Tree EXP must have an integral type. */
4109 t
= TREE_TYPE (exp
);
4110 if (! INTEGRAL_TYPE_P (t
))
4113 /* Tree VAL must be an integer constant. */
4114 if (TREE_CODE (val
) != INTEGER_CST
4115 || TREE_OVERFLOW (val
))
4118 width
= TYPE_PRECISION (t
);
4119 if (wi::only_sign_bit_p (val
, width
))
4122 /* Handle extension from a narrower type. */
4123 if (TREE_CODE (exp
) == NOP_EXPR
4124 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4125 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4130 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4131 to be evaluated unconditionally. */
4134 simple_operand_p (const_tree exp
)
4136 /* Strip any conversions that don't change the machine mode. */
4139 return (CONSTANT_CLASS_P (exp
)
4140 || TREE_CODE (exp
) == SSA_NAME
4142 && ! TREE_ADDRESSABLE (exp
)
4143 && ! TREE_THIS_VOLATILE (exp
)
4144 && ! DECL_NONLOCAL (exp
)
4145 /* Don't regard global variables as simple. They may be
4146 allocated in ways unknown to the compiler (shared memory,
4147 #pragma weak, etc). */
4148 && ! TREE_PUBLIC (exp
)
4149 && ! DECL_EXTERNAL (exp
)
4150 /* Weakrefs are not safe to be read, since they can be NULL.
4151 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4152 have DECL_WEAK flag set. */
4153 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4154 /* Loading a static variable is unduly expensive, but global
4155 registers aren't expensive. */
4156 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4159 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4160 to be evaluated unconditionally.
4161 I addition to simple_operand_p, we assume that comparisons, conversions,
4162 and logic-not operations are simple, if their operands are simple, too. */
4165 simple_operand_p_2 (tree exp
)
4167 enum tree_code code
;
4169 if (TREE_SIDE_EFFECTS (exp
)
4170 || tree_could_trap_p (exp
))
4173 while (CONVERT_EXPR_P (exp
))
4174 exp
= TREE_OPERAND (exp
, 0);
4176 code
= TREE_CODE (exp
);
4178 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4179 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4180 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4182 if (code
== TRUTH_NOT_EXPR
)
4183 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4185 return simple_operand_p (exp
);
4189 /* The following functions are subroutines to fold_range_test and allow it to
4190 try to change a logical combination of comparisons into a range test.
4193 X == 2 || X == 3 || X == 4 || X == 5
4197 (unsigned) (X - 2) <= 3
4199 We describe each set of comparisons as being either inside or outside
4200 a range, using a variable named like IN_P, and then describe the
4201 range with a lower and upper bound. If one of the bounds is omitted,
4202 it represents either the highest or lowest value of the type.
4204 In the comments below, we represent a range by two numbers in brackets
4205 preceded by a "+" to designate being inside that range, or a "-" to
4206 designate being outside that range, so the condition can be inverted by
4207 flipping the prefix. An omitted bound is represented by a "-". For
4208 example, "- [-, 10]" means being outside the range starting at the lowest
4209 possible value and ending at 10, in other words, being greater than 10.
4210 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4213 We set up things so that the missing bounds are handled in a consistent
4214 manner so neither a missing bound nor "true" and "false" need to be
4215 handled using a special case. */
4217 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4218 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4219 and UPPER1_P are nonzero if the respective argument is an upper bound
4220 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4221 must be specified for a comparison. ARG1 will be converted to ARG0's
4222 type if both are specified. */
4225 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4226 tree arg1
, int upper1_p
)
4232 /* If neither arg represents infinity, do the normal operation.
4233 Else, if not a comparison, return infinity. Else handle the special
4234 comparison rules. Note that most of the cases below won't occur, but
4235 are handled for consistency. */
4237 if (arg0
!= 0 && arg1
!= 0)
4239 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4240 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4242 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4245 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4248 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4249 for neither. In real maths, we cannot assume open ended ranges are
4250 the same. But, this is computer arithmetic, where numbers are finite.
4251 We can therefore make the transformation of any unbounded range with
4252 the value Z, Z being greater than any representable number. This permits
4253 us to treat unbounded ranges as equal. */
4254 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4255 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4259 result
= sgn0
== sgn1
;
4262 result
= sgn0
!= sgn1
;
4265 result
= sgn0
< sgn1
;
4268 result
= sgn0
<= sgn1
;
4271 result
= sgn0
> sgn1
;
4274 result
= sgn0
>= sgn1
;
4280 return constant_boolean_node (result
, type
);
4283 /* Helper routine for make_range. Perform one step for it, return
4284 new expression if the loop should continue or NULL_TREE if it should
4288 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4289 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4290 bool *strict_overflow_p
)
4292 tree arg0_type
= TREE_TYPE (arg0
);
4293 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4294 int in_p
= *p_in_p
, n_in_p
;
4298 case TRUTH_NOT_EXPR
:
4299 /* We can only do something if the range is testing for zero. */
4300 if (low
== NULL_TREE
|| high
== NULL_TREE
4301 || ! integer_zerop (low
) || ! integer_zerop (high
))
4306 case EQ_EXPR
: case NE_EXPR
:
4307 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4308 /* We can only do something if the range is testing for zero
4309 and if the second operand is an integer constant. Note that
4310 saying something is "in" the range we make is done by
4311 complementing IN_P since it will set in the initial case of
4312 being not equal to zero; "out" is leaving it alone. */
4313 if (low
== NULL_TREE
|| high
== NULL_TREE
4314 || ! integer_zerop (low
) || ! integer_zerop (high
)
4315 || TREE_CODE (arg1
) != INTEGER_CST
)
4320 case NE_EXPR
: /* - [c, c] */
4323 case EQ_EXPR
: /* + [c, c] */
4324 in_p
= ! in_p
, low
= high
= arg1
;
4326 case GT_EXPR
: /* - [-, c] */
4327 low
= 0, high
= arg1
;
4329 case GE_EXPR
: /* + [c, -] */
4330 in_p
= ! in_p
, low
= arg1
, high
= 0;
4332 case LT_EXPR
: /* - [c, -] */
4333 low
= arg1
, high
= 0;
4335 case LE_EXPR
: /* + [-, c] */
4336 in_p
= ! in_p
, low
= 0, high
= arg1
;
4342 /* If this is an unsigned comparison, we also know that EXP is
4343 greater than or equal to zero. We base the range tests we make
4344 on that fact, so we record it here so we can parse existing
4345 range tests. We test arg0_type since often the return type
4346 of, e.g. EQ_EXPR, is boolean. */
4347 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4349 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4351 build_int_cst (arg0_type
, 0),
4355 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4357 /* If the high bound is missing, but we have a nonzero low
4358 bound, reverse the range so it goes from zero to the low bound
4360 if (high
== 0 && low
&& ! integer_zerop (low
))
4363 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4364 build_int_cst (TREE_TYPE (low
), 1), 0);
4365 low
= build_int_cst (arg0_type
, 0);
4375 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4376 low and high are non-NULL, then normalize will DTRT. */
4377 if (!TYPE_UNSIGNED (arg0_type
)
4378 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4380 if (low
== NULL_TREE
)
4381 low
= TYPE_MIN_VALUE (arg0_type
);
4382 if (high
== NULL_TREE
)
4383 high
= TYPE_MAX_VALUE (arg0_type
);
4386 /* (-x) IN [a,b] -> x in [-b, -a] */
4387 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4388 build_int_cst (exp_type
, 0),
4390 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4391 build_int_cst (exp_type
, 0),
4393 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4399 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4400 build_int_cst (exp_type
, 1));
4404 if (TREE_CODE (arg1
) != INTEGER_CST
)
4407 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4408 move a constant to the other side. */
4409 if (!TYPE_UNSIGNED (arg0_type
)
4410 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4413 /* If EXP is signed, any overflow in the computation is undefined,
4414 so we don't worry about it so long as our computations on
4415 the bounds don't overflow. For unsigned, overflow is defined
4416 and this is exactly the right thing. */
4417 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4418 arg0_type
, low
, 0, arg1
, 0);
4419 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4420 arg0_type
, high
, 1, arg1
, 0);
4421 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4422 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4425 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4426 *strict_overflow_p
= true;
4429 /* Check for an unsigned range which has wrapped around the maximum
4430 value thus making n_high < n_low, and normalize it. */
4431 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4433 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4434 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4435 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4436 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4438 /* If the range is of the form +/- [ x+1, x ], we won't
4439 be able to normalize it. But then, it represents the
4440 whole range or the empty set, so make it
4442 if (tree_int_cst_equal (n_low
, low
)
4443 && tree_int_cst_equal (n_high
, high
))
4449 low
= n_low
, high
= n_high
;
4457 case NON_LVALUE_EXPR
:
4458 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4461 if (! INTEGRAL_TYPE_P (arg0_type
)
4462 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4463 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4466 n_low
= low
, n_high
= high
;
4469 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4472 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4474 /* If we're converting arg0 from an unsigned type, to exp,
4475 a signed type, we will be doing the comparison as unsigned.
4476 The tests above have already verified that LOW and HIGH
4479 So we have to ensure that we will handle large unsigned
4480 values the same way that the current signed bounds treat
4483 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4487 /* For fixed-point modes, we need to pass the saturating flag
4488 as the 2nd parameter. */
4489 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4491 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4492 TYPE_SATURATING (arg0_type
));
4495 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4497 /* A range without an upper bound is, naturally, unbounded.
4498 Since convert would have cropped a very large value, use
4499 the max value for the destination type. */
4501 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4502 : TYPE_MAX_VALUE (arg0_type
);
4504 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4505 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4506 fold_convert_loc (loc
, arg0_type
,
4508 build_int_cst (arg0_type
, 1));
4510 /* If the low bound is specified, "and" the range with the
4511 range for which the original unsigned value will be
4515 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4516 1, fold_convert_loc (loc
, arg0_type
,
4521 in_p
= (n_in_p
== in_p
);
4525 /* Otherwise, "or" the range with the range of the input
4526 that will be interpreted as negative. */
4527 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4528 1, fold_convert_loc (loc
, arg0_type
,
4533 in_p
= (in_p
!= n_in_p
);
4547 /* Given EXP, a logical expression, set the range it is testing into
4548 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4549 actually being tested. *PLOW and *PHIGH will be made of the same
4550 type as the returned expression. If EXP is not a comparison, we
4551 will most likely not be returning a useful value and range. Set
4552 *STRICT_OVERFLOW_P to true if the return value is only valid
4553 because signed overflow is undefined; otherwise, do not change
4554 *STRICT_OVERFLOW_P. */
4557 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4558 bool *strict_overflow_p
)
4560 enum tree_code code
;
4561 tree arg0
, arg1
= NULL_TREE
;
4562 tree exp_type
, nexp
;
4565 location_t loc
= EXPR_LOCATION (exp
);
4567 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4568 and see if we can refine the range. Some of the cases below may not
4569 happen, but it doesn't seem worth worrying about this. We "continue"
4570 the outer loop when we've changed something; otherwise we "break"
4571 the switch, which will "break" the while. */
4574 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4578 code
= TREE_CODE (exp
);
4579 exp_type
= TREE_TYPE (exp
);
4582 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4584 if (TREE_OPERAND_LENGTH (exp
) > 0)
4585 arg0
= TREE_OPERAND (exp
, 0);
4586 if (TREE_CODE_CLASS (code
) == tcc_binary
4587 || TREE_CODE_CLASS (code
) == tcc_comparison
4588 || (TREE_CODE_CLASS (code
) == tcc_expression
4589 && TREE_OPERAND_LENGTH (exp
) > 1))
4590 arg1
= TREE_OPERAND (exp
, 1);
4592 if (arg0
== NULL_TREE
)
4595 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4596 &high
, &in_p
, strict_overflow_p
);
4597 if (nexp
== NULL_TREE
)
4602 /* If EXP is a constant, we can evaluate whether this is true or false. */
4603 if (TREE_CODE (exp
) == INTEGER_CST
)
4605 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4607 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4613 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4617 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4618 type, TYPE, return an expression to test if EXP is in (or out of, depending
4619 on IN_P) the range. Return 0 if the test couldn't be created. */
4622 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4623 tree low
, tree high
)
4625 tree etype
= TREE_TYPE (exp
), value
;
4627 /* Disable this optimization for function pointer expressions
4628 on targets that require function pointer canonicalization. */
4629 if (targetm
.have_canonicalize_funcptr_for_compare ()
4630 && TREE_CODE (etype
) == POINTER_TYPE
4631 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4636 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4638 return invert_truthvalue_loc (loc
, value
);
4643 if (low
== 0 && high
== 0)
4644 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4647 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4648 fold_convert_loc (loc
, etype
, high
));
4651 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4652 fold_convert_loc (loc
, etype
, low
));
4654 if (operand_equal_p (low
, high
, 0))
4655 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4656 fold_convert_loc (loc
, etype
, low
));
4658 if (integer_zerop (low
))
4660 if (! TYPE_UNSIGNED (etype
))
4662 etype
= unsigned_type_for (etype
);
4663 high
= fold_convert_loc (loc
, etype
, high
);
4664 exp
= fold_convert_loc (loc
, etype
, exp
);
4666 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4669 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4670 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4672 int prec
= TYPE_PRECISION (etype
);
4674 if (wi::mask (prec
- 1, false, prec
) == high
)
4676 if (TYPE_UNSIGNED (etype
))
4678 tree signed_etype
= signed_type_for (etype
);
4679 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4681 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4683 etype
= signed_etype
;
4684 exp
= fold_convert_loc (loc
, etype
, exp
);
4686 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4687 build_int_cst (etype
, 0));
4691 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4692 This requires wrap-around arithmetics for the type of the expression.
4693 First make sure that arithmetics in this type is valid, then make sure
4694 that it wraps around. */
4695 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4696 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4697 TYPE_UNSIGNED (etype
));
4699 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4701 tree utype
, minv
, maxv
;
4703 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4704 for the type in question, as we rely on this here. */
4705 utype
= unsigned_type_for (etype
);
4706 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4707 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4708 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4709 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4711 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4718 high
= fold_convert_loc (loc
, etype
, high
);
4719 low
= fold_convert_loc (loc
, etype
, low
);
4720 exp
= fold_convert_loc (loc
, etype
, exp
);
4722 value
= const_binop (MINUS_EXPR
, high
, low
);
4725 if (POINTER_TYPE_P (etype
))
4727 if (value
!= 0 && !TREE_OVERFLOW (value
))
4729 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4730 return build_range_check (loc
, type
,
4731 fold_build_pointer_plus_loc (loc
, exp
, low
),
4732 1, build_int_cst (etype
, 0), value
);
4737 if (value
!= 0 && !TREE_OVERFLOW (value
))
4738 return build_range_check (loc
, type
,
4739 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4740 1, build_int_cst (etype
, 0), value
);
4745 /* Return the predecessor of VAL in its type, handling the infinite case. */
4748 range_predecessor (tree val
)
4750 tree type
= TREE_TYPE (val
);
4752 if (INTEGRAL_TYPE_P (type
)
4753 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4756 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4757 build_int_cst (TREE_TYPE (val
), 1), 0);
4760 /* Return the successor of VAL in its type, handling the infinite case. */
4763 range_successor (tree val
)
4765 tree type
= TREE_TYPE (val
);
4767 if (INTEGRAL_TYPE_P (type
)
4768 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4771 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4772 build_int_cst (TREE_TYPE (val
), 1), 0);
4775 /* Given two ranges, see if we can merge them into one. Return 1 if we
4776 can, 0 if we can't. Set the output range into the specified parameters. */
4779 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4780 tree high0
, int in1_p
, tree low1
, tree high1
)
4788 int lowequal
= ((low0
== 0 && low1
== 0)
4789 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4790 low0
, 0, low1
, 0)));
4791 int highequal
= ((high0
== 0 && high1
== 0)
4792 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4793 high0
, 1, high1
, 1)));
4795 /* Make range 0 be the range that starts first, or ends last if they
4796 start at the same value. Swap them if it isn't. */
4797 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4800 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4801 high1
, 1, high0
, 1))))
4803 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4804 tem
= low0
, low0
= low1
, low1
= tem
;
4805 tem
= high0
, high0
= high1
, high1
= tem
;
4808 /* Now flag two cases, whether the ranges are disjoint or whether the
4809 second range is totally subsumed in the first. Note that the tests
4810 below are simplified by the ones above. */
4811 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4812 high0
, 1, low1
, 0));
4813 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4814 high1
, 1, high0
, 1));
4816 /* We now have four cases, depending on whether we are including or
4817 excluding the two ranges. */
4820 /* If they don't overlap, the result is false. If the second range
4821 is a subset it is the result. Otherwise, the range is from the start
4822 of the second to the end of the first. */
4824 in_p
= 0, low
= high
= 0;
4826 in_p
= 1, low
= low1
, high
= high1
;
4828 in_p
= 1, low
= low1
, high
= high0
;
4831 else if (in0_p
&& ! in1_p
)
4833 /* If they don't overlap, the result is the first range. If they are
4834 equal, the result is false. If the second range is a subset of the
4835 first, and the ranges begin at the same place, we go from just after
4836 the end of the second range to the end of the first. If the second
4837 range is not a subset of the first, or if it is a subset and both
4838 ranges end at the same place, the range starts at the start of the
4839 first range and ends just before the second range.
4840 Otherwise, we can't describe this as a single range. */
4842 in_p
= 1, low
= low0
, high
= high0
;
4843 else if (lowequal
&& highequal
)
4844 in_p
= 0, low
= high
= 0;
4845 else if (subset
&& lowequal
)
4847 low
= range_successor (high1
);
4852 /* We are in the weird situation where high0 > high1 but
4853 high1 has no successor. Punt. */
4857 else if (! subset
|| highequal
)
4860 high
= range_predecessor (low1
);
4864 /* low0 < low1 but low1 has no predecessor. Punt. */
4872 else if (! in0_p
&& in1_p
)
4874 /* If they don't overlap, the result is the second range. If the second
4875 is a subset of the first, the result is false. Otherwise,
4876 the range starts just after the first range and ends at the
4877 end of the second. */
4879 in_p
= 1, low
= low1
, high
= high1
;
4880 else if (subset
|| highequal
)
4881 in_p
= 0, low
= high
= 0;
4884 low
= range_successor (high0
);
4889 /* high1 > high0 but high0 has no successor. Punt. */
4897 /* The case where we are excluding both ranges. Here the complex case
4898 is if they don't overlap. In that case, the only time we have a
4899 range is if they are adjacent. If the second is a subset of the
4900 first, the result is the first. Otherwise, the range to exclude
4901 starts at the beginning of the first range and ends at the end of the
4905 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4906 range_successor (high0
),
4908 in_p
= 0, low
= low0
, high
= high1
;
4911 /* Canonicalize - [min, x] into - [-, x]. */
4912 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
4913 switch (TREE_CODE (TREE_TYPE (low0
)))
4916 if (TYPE_PRECISION (TREE_TYPE (low0
))
4917 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
4921 if (tree_int_cst_equal (low0
,
4922 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
4926 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
4927 && integer_zerop (low0
))
4934 /* Canonicalize - [x, max] into - [x, -]. */
4935 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
4936 switch (TREE_CODE (TREE_TYPE (high1
)))
4939 if (TYPE_PRECISION (TREE_TYPE (high1
))
4940 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
4944 if (tree_int_cst_equal (high1
,
4945 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
4949 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
4950 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
4952 build_int_cst (TREE_TYPE (high1
), 1),
4960 /* The ranges might be also adjacent between the maximum and
4961 minimum values of the given type. For
4962 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4963 return + [x + 1, y - 1]. */
4964 if (low0
== 0 && high1
== 0)
4966 low
= range_successor (high0
);
4967 high
= range_predecessor (low1
);
4968 if (low
== 0 || high
== 0)
4978 in_p
= 0, low
= low0
, high
= high0
;
4980 in_p
= 0, low
= low0
, high
= high1
;
4983 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4988 /* Subroutine of fold, looking inside expressions of the form
4989 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4990 of the COND_EXPR. This function is being used also to optimize
4991 A op B ? C : A, by reversing the comparison first.
4993 Return a folded expression whose code is not a COND_EXPR
4994 anymore, or NULL_TREE if no folding opportunity is found. */
4997 fold_cond_expr_with_comparison (location_t loc
, tree type
,
4998 tree arg0
, tree arg1
, tree arg2
)
5000 enum tree_code comp_code
= TREE_CODE (arg0
);
5001 tree arg00
= TREE_OPERAND (arg0
, 0);
5002 tree arg01
= TREE_OPERAND (arg0
, 1);
5003 tree arg1_type
= TREE_TYPE (arg1
);
5009 /* If we have A op 0 ? A : -A, consider applying the following
5012 A == 0? A : -A same as -A
5013 A != 0? A : -A same as A
5014 A >= 0? A : -A same as abs (A)
5015 A > 0? A : -A same as abs (A)
5016 A <= 0? A : -A same as -abs (A)
5017 A < 0? A : -A same as -abs (A)
5019 None of these transformations work for modes with signed
5020 zeros. If A is +/-0, the first two transformations will
5021 change the sign of the result (from +0 to -0, or vice
5022 versa). The last four will fix the sign of the result,
5023 even though the original expressions could be positive or
5024 negative, depending on the sign of A.
5026 Note that all these transformations are correct if A is
5027 NaN, since the two alternatives (A and -A) are also NaNs. */
5028 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5029 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5030 ? real_zerop (arg01
)
5031 : integer_zerop (arg01
))
5032 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5033 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5034 /* In the case that A is of the form X-Y, '-A' (arg2) may
5035 have already been folded to Y-X, check for that. */
5036 || (TREE_CODE (arg1
) == MINUS_EXPR
5037 && TREE_CODE (arg2
) == MINUS_EXPR
5038 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5039 TREE_OPERAND (arg2
, 1), 0)
5040 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5041 TREE_OPERAND (arg2
, 0), 0))))
5046 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5047 return pedantic_non_lvalue_loc (loc
,
5048 fold_convert_loc (loc
, type
,
5049 negate_expr (tem
)));
5052 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5055 if (flag_trapping_math
)
5060 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5062 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5063 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5066 if (flag_trapping_math
)
5070 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5072 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5073 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5075 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5079 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5080 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5081 both transformations are correct when A is NaN: A != 0
5082 is then true, and A == 0 is false. */
5084 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5085 && integer_zerop (arg01
) && integer_zerop (arg2
))
5087 if (comp_code
== NE_EXPR
)
5088 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5089 else if (comp_code
== EQ_EXPR
)
5090 return build_zero_cst (type
);
5093 /* Try some transformations of A op B ? A : B.
5095 A == B? A : B same as B
5096 A != B? A : B same as A
5097 A >= B? A : B same as max (A, B)
5098 A > B? A : B same as max (B, A)
5099 A <= B? A : B same as min (A, B)
5100 A < B? A : B same as min (B, A)
5102 As above, these transformations don't work in the presence
5103 of signed zeros. For example, if A and B are zeros of
5104 opposite sign, the first two transformations will change
5105 the sign of the result. In the last four, the original
5106 expressions give different results for (A=+0, B=-0) and
5107 (A=-0, B=+0), but the transformed expressions do not.
5109 The first two transformations are correct if either A or B
5110 is a NaN. In the first transformation, the condition will
5111 be false, and B will indeed be chosen. In the case of the
5112 second transformation, the condition A != B will be true,
5113 and A will be chosen.
5115 The conversions to max() and min() are not correct if B is
5116 a number and A is not. The conditions in the original
5117 expressions will be false, so all four give B. The min()
5118 and max() versions would give a NaN instead. */
5119 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5120 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
5121 /* Avoid these transformations if the COND_EXPR may be used
5122 as an lvalue in the C++ front-end. PR c++/19199. */
5124 || VECTOR_TYPE_P (type
)
5125 || (! lang_GNU_CXX ()
5126 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5127 || ! maybe_lvalue_p (arg1
)
5128 || ! maybe_lvalue_p (arg2
)))
5130 tree comp_op0
= arg00
;
5131 tree comp_op1
= arg01
;
5132 tree comp_type
= TREE_TYPE (comp_op0
);
5134 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5135 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
5145 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg2
));
5147 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5152 /* In C++ a ?: expression can be an lvalue, so put the
5153 operand which will be used if they are equal first
5154 so that we can convert this back to the
5155 corresponding COND_EXPR. */
5156 if (!HONOR_NANS (arg1
))
5158 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5159 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5160 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5161 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5162 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5163 comp_op1
, comp_op0
);
5164 return pedantic_non_lvalue_loc (loc
,
5165 fold_convert_loc (loc
, type
, tem
));
5172 if (!HONOR_NANS (arg1
))
5174 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5175 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5176 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5177 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5178 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5179 comp_op1
, comp_op0
);
5180 return pedantic_non_lvalue_loc (loc
,
5181 fold_convert_loc (loc
, type
, tem
));
5185 if (!HONOR_NANS (arg1
))
5186 return pedantic_non_lvalue_loc (loc
,
5187 fold_convert_loc (loc
, type
, arg2
));
5190 if (!HONOR_NANS (arg1
))
5191 return pedantic_non_lvalue_loc (loc
,
5192 fold_convert_loc (loc
, type
, arg1
));
5195 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5200 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5201 we might still be able to simplify this. For example,
5202 if C1 is one less or one more than C2, this might have started
5203 out as a MIN or MAX and been transformed by this function.
5204 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5206 if (INTEGRAL_TYPE_P (type
)
5207 && TREE_CODE (arg01
) == INTEGER_CST
5208 && TREE_CODE (arg2
) == INTEGER_CST
)
5212 if (TREE_CODE (arg1
) == INTEGER_CST
)
5214 /* We can replace A with C1 in this case. */
5215 arg1
= fold_convert_loc (loc
, type
, arg01
);
5216 return fold_build3_loc (loc
, COND_EXPR
, type
, arg0
, arg1
, arg2
);
5219 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5220 MIN_EXPR, to preserve the signedness of the comparison. */
5221 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5223 && operand_equal_p (arg01
,
5224 const_binop (PLUS_EXPR
, arg2
,
5225 build_int_cst (type
, 1)),
5228 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5229 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5231 return pedantic_non_lvalue_loc (loc
,
5232 fold_convert_loc (loc
, type
, tem
));
5237 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5239 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5241 && operand_equal_p (arg01
,
5242 const_binop (MINUS_EXPR
, arg2
,
5243 build_int_cst (type
, 1)),
5246 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5247 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5249 return pedantic_non_lvalue_loc (loc
,
5250 fold_convert_loc (loc
, type
, tem
));
5255 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5256 MAX_EXPR, to preserve the signedness of the comparison. */
5257 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5259 && operand_equal_p (arg01
,
5260 const_binop (MINUS_EXPR
, arg2
,
5261 build_int_cst (type
, 1)),
5264 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5265 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5267 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5272 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5273 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5275 && operand_equal_p (arg01
,
5276 const_binop (PLUS_EXPR
, arg2
,
5277 build_int_cst (type
, 1)),
5280 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5281 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5283 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5297 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5298 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5299 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5303 /* EXP is some logical combination of boolean tests. See if we can
5304 merge it into some range test. Return the new tree if so. */
5307 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5310 int or_op
= (code
== TRUTH_ORIF_EXPR
5311 || code
== TRUTH_OR_EXPR
);
5312 int in0_p
, in1_p
, in_p
;
5313 tree low0
, low1
, low
, high0
, high1
, high
;
5314 bool strict_overflow_p
= false;
5316 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5317 "when simplifying range test");
5319 if (!INTEGRAL_TYPE_P (type
))
5322 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5323 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5325 /* If this is an OR operation, invert both sides; we will invert
5326 again at the end. */
5328 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5330 /* If both expressions are the same, if we can merge the ranges, and we
5331 can build the range test, return it or it inverted. If one of the
5332 ranges is always true or always false, consider it to be the same
5333 expression as the other. */
5334 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5335 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5337 && 0 != (tem
= (build_range_check (loc
, type
,
5339 : rhs
!= 0 ? rhs
: integer_zero_node
,
5342 if (strict_overflow_p
)
5343 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5344 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5347 /* On machines where the branch cost is expensive, if this is a
5348 short-circuited branch and the underlying object on both sides
5349 is the same, make a non-short-circuit operation. */
5350 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5351 && lhs
!= 0 && rhs
!= 0
5352 && (code
== TRUTH_ANDIF_EXPR
5353 || code
== TRUTH_ORIF_EXPR
)
5354 && operand_equal_p (lhs
, rhs
, 0))
5356 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5357 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5358 which cases we can't do this. */
5359 if (simple_operand_p (lhs
))
5360 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5361 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5364 else if (!lang_hooks
.decls
.global_bindings_p ()
5365 && !CONTAINS_PLACEHOLDER_P (lhs
))
5367 tree common
= save_expr (lhs
);
5369 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5370 or_op
? ! in0_p
: in0_p
,
5372 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5373 or_op
? ! in1_p
: in1_p
,
5376 if (strict_overflow_p
)
5377 fold_overflow_warning (warnmsg
,
5378 WARN_STRICT_OVERFLOW_COMPARISON
);
5379 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5380 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5389 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5390 bit value. Arrange things so the extra bits will be set to zero if and
5391 only if C is signed-extended to its full width. If MASK is nonzero,
5392 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5395 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5397 tree type
= TREE_TYPE (c
);
5398 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5401 if (p
== modesize
|| unsignedp
)
5404 /* We work by getting just the sign bit into the low-order bit, then
5405 into the high-order bit, then sign-extend. We then XOR that value
5407 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5409 /* We must use a signed type in order to get an arithmetic right shift.
5410 However, we must also avoid introducing accidental overflows, so that
5411 a subsequent call to integer_zerop will work. Hence we must
5412 do the type conversion here. At this point, the constant is either
5413 zero or one, and the conversion to a signed type can never overflow.
5414 We could get an overflow if this conversion is done anywhere else. */
5415 if (TYPE_UNSIGNED (type
))
5416 temp
= fold_convert (signed_type_for (type
), temp
);
5418 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5419 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5421 temp
= const_binop (BIT_AND_EXPR
, temp
,
5422 fold_convert (TREE_TYPE (c
), mask
));
5423 /* If necessary, convert the type back to match the type of C. */
5424 if (TYPE_UNSIGNED (type
))
5425 temp
= fold_convert (type
, temp
);
5427 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5430 /* For an expression that has the form
5434 we can drop one of the inner expressions and simplify to
5438 LOC is the location of the resulting expression. OP is the inner
5439 logical operation; the left-hand side in the examples above, while CMPOP
5440 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5441 removing a condition that guards another, as in
5442 (A != NULL && A->...) || A == NULL
5443 which we must not transform. If RHS_ONLY is true, only eliminate the
5444 right-most operand of the inner logical operation. */
5447 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5450 tree type
= TREE_TYPE (cmpop
);
5451 enum tree_code code
= TREE_CODE (cmpop
);
5452 enum tree_code truthop_code
= TREE_CODE (op
);
5453 tree lhs
= TREE_OPERAND (op
, 0);
5454 tree rhs
= TREE_OPERAND (op
, 1);
5455 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5456 enum tree_code rhs_code
= TREE_CODE (rhs
);
5457 enum tree_code lhs_code
= TREE_CODE (lhs
);
5458 enum tree_code inv_code
;
5460 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5463 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5466 if (rhs_code
== truthop_code
)
5468 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5469 if (newrhs
!= NULL_TREE
)
5472 rhs_code
= TREE_CODE (rhs
);
5475 if (lhs_code
== truthop_code
&& !rhs_only
)
5477 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5478 if (newlhs
!= NULL_TREE
)
5481 lhs_code
= TREE_CODE (lhs
);
5485 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5486 if (inv_code
== rhs_code
5487 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5488 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5490 if (!rhs_only
&& inv_code
== lhs_code
5491 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5492 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5494 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5495 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5500 /* Find ways of folding logical expressions of LHS and RHS:
5501 Try to merge two comparisons to the same innermost item.
5502 Look for range tests like "ch >= '0' && ch <= '9'".
5503 Look for combinations of simple terms on machines with expensive branches
5504 and evaluate the RHS unconditionally.
5506 For example, if we have p->a == 2 && p->b == 4 and we can make an
5507 object large enough to span both A and B, we can do this with a comparison
5508 against the object ANDed with the a mask.
5510 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5511 operations to do this with one comparison.
5513 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5514 function and the one above.
5516 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5517 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5519 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5522 We return the simplified tree or 0 if no optimization is possible. */
5525 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5528 /* If this is the "or" of two comparisons, we can do something if
5529 the comparisons are NE_EXPR. If this is the "and", we can do something
5530 if the comparisons are EQ_EXPR. I.e.,
5531 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5533 WANTED_CODE is this operation code. For single bit fields, we can
5534 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5535 comparison for one-bit fields. */
5537 enum tree_code wanted_code
;
5538 enum tree_code lcode
, rcode
;
5539 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5540 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5541 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5542 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5543 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5544 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5545 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5546 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5547 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5548 machine_mode lnmode
, rnmode
;
5549 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5550 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5551 tree l_const
, r_const
;
5552 tree lntype
, rntype
, result
;
5553 HOST_WIDE_INT first_bit
, end_bit
;
5556 /* Start by getting the comparison codes. Fail if anything is volatile.
5557 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5558 it were surrounded with a NE_EXPR. */
5560 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5563 lcode
= TREE_CODE (lhs
);
5564 rcode
= TREE_CODE (rhs
);
5566 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5568 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5569 build_int_cst (TREE_TYPE (lhs
), 0));
5573 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5575 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5576 build_int_cst (TREE_TYPE (rhs
), 0));
5580 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5581 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5584 ll_arg
= TREE_OPERAND (lhs
, 0);
5585 lr_arg
= TREE_OPERAND (lhs
, 1);
5586 rl_arg
= TREE_OPERAND (rhs
, 0);
5587 rr_arg
= TREE_OPERAND (rhs
, 1);
5589 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5590 if (simple_operand_p (ll_arg
)
5591 && simple_operand_p (lr_arg
))
5593 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5594 && operand_equal_p (lr_arg
, rr_arg
, 0))
5596 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5597 truth_type
, ll_arg
, lr_arg
);
5601 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5602 && operand_equal_p (lr_arg
, rl_arg
, 0))
5604 result
= combine_comparisons (loc
, code
, lcode
,
5605 swap_tree_comparison (rcode
),
5606 truth_type
, ll_arg
, lr_arg
);
5612 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5613 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5615 /* If the RHS can be evaluated unconditionally and its operands are
5616 simple, it wins to evaluate the RHS unconditionally on machines
5617 with expensive branches. In this case, this isn't a comparison
5618 that can be merged. */
5620 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5622 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5623 && simple_operand_p (rl_arg
)
5624 && simple_operand_p (rr_arg
))
5626 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5627 if (code
== TRUTH_OR_EXPR
5628 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5629 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5630 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5631 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5632 return build2_loc (loc
, NE_EXPR
, truth_type
,
5633 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5635 build_int_cst (TREE_TYPE (ll_arg
), 0));
5637 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5638 if (code
== TRUTH_AND_EXPR
5639 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5640 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5641 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5642 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5643 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5644 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5646 build_int_cst (TREE_TYPE (ll_arg
), 0));
5649 /* See if the comparisons can be merged. Then get all the parameters for
5652 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5653 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5656 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5658 ll_inner
= decode_field_reference (loc
, ll_arg
,
5659 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5660 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5661 &ll_mask
, &ll_and_mask
);
5662 lr_inner
= decode_field_reference (loc
, lr_arg
,
5663 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5664 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5665 &lr_mask
, &lr_and_mask
);
5666 rl_inner
= decode_field_reference (loc
, rl_arg
,
5667 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5668 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5669 &rl_mask
, &rl_and_mask
);
5670 rr_inner
= decode_field_reference (loc
, rr_arg
,
5671 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5672 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5673 &rr_mask
, &rr_and_mask
);
5675 /* It must be true that the inner operation on the lhs of each
5676 comparison must be the same if we are to be able to do anything.
5677 Then see if we have constants. If not, the same must be true for
5680 || ll_reversep
!= rl_reversep
5681 || ll_inner
== 0 || rl_inner
== 0
5682 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5685 if (TREE_CODE (lr_arg
) == INTEGER_CST
5686 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5688 l_const
= lr_arg
, r_const
= rr_arg
;
5689 lr_reversep
= ll_reversep
;
5691 else if (lr_reversep
!= rr_reversep
5692 || lr_inner
== 0 || rr_inner
== 0
5693 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5696 l_const
= r_const
= 0;
5698 /* If either comparison code is not correct for our logical operation,
5699 fail. However, we can convert a one-bit comparison against zero into
5700 the opposite comparison against that bit being set in the field. */
5702 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5703 if (lcode
!= wanted_code
)
5705 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5707 /* Make the left operand unsigned, since we are only interested
5708 in the value of one bit. Otherwise we are doing the wrong
5717 /* This is analogous to the code for l_const above. */
5718 if (rcode
!= wanted_code
)
5720 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5729 /* See if we can find a mode that contains both fields being compared on
5730 the left. If we can't, fail. Otherwise, update all constants and masks
5731 to be relative to a field of that size. */
5732 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5733 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5734 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5735 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5737 if (lnmode
== VOIDmode
)
5740 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5741 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5742 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5743 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5745 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5747 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5748 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5751 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5752 size_int (xll_bitpos
));
5753 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5754 size_int (xrl_bitpos
));
5758 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5759 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5760 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5761 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5762 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5765 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5767 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5772 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5773 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5774 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5775 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5776 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5779 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5781 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5785 /* If the right sides are not constant, do the same for it. Also,
5786 disallow this optimization if a size or signedness mismatch occurs
5787 between the left and right sides. */
5790 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5791 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5792 /* Make sure the two fields on the right
5793 correspond to the left without being swapped. */
5794 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5797 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5798 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5799 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5800 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5802 if (rnmode
== VOIDmode
)
5805 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5806 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5807 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5808 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5810 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5812 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5813 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5816 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5818 size_int (xlr_bitpos
));
5819 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5821 size_int (xrr_bitpos
));
5823 /* Make a mask that corresponds to both fields being compared.
5824 Do this for both items being compared. If the operands are the
5825 same size and the bits being compared are in the same position
5826 then we can do this by masking both and comparing the masked
5828 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5829 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5830 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5832 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5833 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5834 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5835 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5837 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
, rnbitsize
, rnbitpos
,
5838 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
5839 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5840 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5842 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5845 /* There is still another way we can do something: If both pairs of
5846 fields being compared are adjacent, we may be able to make a wider
5847 field containing them both.
5849 Note that we still must mask the lhs/rhs expressions. Furthermore,
5850 the mask must be shifted to account for the shift done by
5851 make_bit_field_ref. */
5852 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5853 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5854 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5855 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5859 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
,
5860 ll_bitsize
+ rl_bitsize
,
5861 MIN (ll_bitpos
, rl_bitpos
),
5862 ll_unsignedp
, ll_reversep
);
5863 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
,
5864 lr_bitsize
+ rr_bitsize
,
5865 MIN (lr_bitpos
, rr_bitpos
),
5866 lr_unsignedp
, lr_reversep
);
5868 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5869 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5870 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5871 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5873 /* Convert to the smaller type before masking out unwanted bits. */
5875 if (lntype
!= rntype
)
5877 if (lnbitsize
> rnbitsize
)
5879 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5880 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5883 else if (lnbitsize
< rnbitsize
)
5885 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5886 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5891 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5892 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5894 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5895 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5897 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5903 /* Handle the case of comparisons with constants. If there is something in
5904 common between the masks, those bits of the constants must be the same.
5905 If not, the condition is always false. Test for this to avoid generating
5906 incorrect code below. */
5907 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5908 if (! integer_zerop (result
)
5909 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5910 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5912 if (wanted_code
== NE_EXPR
)
5914 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5915 return constant_boolean_node (true, truth_type
);
5919 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5920 return constant_boolean_node (false, truth_type
);
5924 /* Construct the expression we will return. First get the component
5925 reference we will make. Unless the mask is all ones the width of
5926 that field, perform the mask operation. Then compare with the
5928 result
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5929 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5931 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5932 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5933 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5935 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5936 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5939 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5943 optimize_minmax_comparison (location_t loc
, enum tree_code code
, tree type
,
5947 enum tree_code op_code
;
5950 int consts_equal
, consts_lt
;
5953 STRIP_SIGN_NOPS (arg0
);
5955 op_code
= TREE_CODE (arg0
);
5956 minmax_const
= TREE_OPERAND (arg0
, 1);
5957 comp_const
= fold_convert_loc (loc
, TREE_TYPE (arg0
), op1
);
5958 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
5959 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
5960 inner
= TREE_OPERAND (arg0
, 0);
5962 /* If something does not permit us to optimize, return the original tree. */
5963 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
5964 || TREE_CODE (comp_const
) != INTEGER_CST
5965 || TREE_OVERFLOW (comp_const
)
5966 || TREE_CODE (minmax_const
) != INTEGER_CST
5967 || TREE_OVERFLOW (minmax_const
))
5970 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5971 and GT_EXPR, doing the rest with recursive calls using logical
5975 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
5978 = optimize_minmax_comparison (loc
,
5979 invert_tree_comparison (code
, false),
5982 return invert_truthvalue_loc (loc
, tem
);
5988 fold_build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
5989 optimize_minmax_comparison
5990 (loc
, EQ_EXPR
, type
, arg0
, comp_const
),
5991 optimize_minmax_comparison
5992 (loc
, GT_EXPR
, type
, arg0
, comp_const
));
5995 if (op_code
== MAX_EXPR
&& consts_equal
)
5996 /* MAX (X, 0) == 0 -> X <= 0 */
5997 return fold_build2_loc (loc
, LE_EXPR
, type
, inner
, comp_const
);
5999 else if (op_code
== MAX_EXPR
&& consts_lt
)
6000 /* MAX (X, 0) == 5 -> X == 5 */
6001 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
6003 else if (op_code
== MAX_EXPR
)
6004 /* MAX (X, 0) == -1 -> false */
6005 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
6007 else if (consts_equal
)
6008 /* MIN (X, 0) == 0 -> X >= 0 */
6009 return fold_build2_loc (loc
, GE_EXPR
, type
, inner
, comp_const
);
6012 /* MIN (X, 0) == 5 -> false */
6013 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
6016 /* MIN (X, 0) == -1 -> X == -1 */
6017 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
6020 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
6021 /* MAX (X, 0) > 0 -> X > 0
6022 MAX (X, 0) > 5 -> X > 5 */
6023 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
6025 else if (op_code
== MAX_EXPR
)
6026 /* MAX (X, 0) > -1 -> true */
6027 return omit_one_operand_loc (loc
, type
, integer_one_node
, inner
);
6029 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
6030 /* MIN (X, 0) > 0 -> false
6031 MIN (X, 0) > 5 -> false */
6032 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
6035 /* MIN (X, 0) > -1 -> X > -1 */
6036 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
6043 /* T is an integer expression that is being multiplied, divided, or taken a
6044 modulus (CODE says which and what kind of divide or modulus) by a
6045 constant C. See if we can eliminate that operation by folding it with
6046 other operations already in T. WIDE_TYPE, if non-null, is a type that
6047 should be used for the computation if wider than our type.
6049 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6050 (X * 2) + (Y * 4). We must, however, be assured that either the original
6051 expression would not overflow or that overflow is undefined for the type
6052 in the language in question.
6054 If we return a non-null expression, it is an equivalent form of the
6055 original computation, but need not be in the original type.
6057 We set *STRICT_OVERFLOW_P to true if the return values depends on
6058 signed overflow being undefined. Otherwise we do not change
6059 *STRICT_OVERFLOW_P. */
6062 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6063 bool *strict_overflow_p
)
6065 /* To avoid exponential search depth, refuse to allow recursion past
6066 three levels. Beyond that (1) it's highly unlikely that we'll find
6067 something interesting and (2) we've probably processed it before
6068 when we built the inner expression. */
6077 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6084 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6085 bool *strict_overflow_p
)
6087 tree type
= TREE_TYPE (t
);
6088 enum tree_code tcode
= TREE_CODE (t
);
6089 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
6090 > GET_MODE_SIZE (TYPE_MODE (type
)))
6091 ? wide_type
: type
);
6093 int same_p
= tcode
== code
;
6094 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6095 bool sub_strict_overflow_p
;
6097 /* Don't deal with constants of zero here; they confuse the code below. */
6098 if (integer_zerop (c
))
6101 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6102 op0
= TREE_OPERAND (t
, 0);
6104 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6105 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6107 /* Note that we need not handle conditional operations here since fold
6108 already handles those cases. So just do arithmetic here. */
6112 /* For a constant, we can always simplify if we are a multiply
6113 or (for divide and modulus) if it is a multiple of our constant. */
6114 if (code
== MULT_EXPR
6115 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6117 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6118 fold_convert (ctype
, c
));
6119 /* If the multiplication overflowed, we lost information on it.
6120 See PR68142 and PR69845. */
6121 if (TREE_OVERFLOW (tem
))
6127 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6128 /* If op0 is an expression ... */
6129 if ((COMPARISON_CLASS_P (op0
)
6130 || UNARY_CLASS_P (op0
)
6131 || BINARY_CLASS_P (op0
)
6132 || VL_EXP_CLASS_P (op0
)
6133 || EXPRESSION_CLASS_P (op0
))
6134 /* ... and has wrapping overflow, and its type is smaller
6135 than ctype, then we cannot pass through as widening. */
6136 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6137 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6138 && (TYPE_PRECISION (ctype
)
6139 > TYPE_PRECISION (TREE_TYPE (op0
))))
6140 /* ... or this is a truncation (t is narrower than op0),
6141 then we cannot pass through this narrowing. */
6142 || (TYPE_PRECISION (type
)
6143 < TYPE_PRECISION (TREE_TYPE (op0
)))
6144 /* ... or signedness changes for division or modulus,
6145 then we cannot pass through this conversion. */
6146 || (code
!= MULT_EXPR
6147 && (TYPE_UNSIGNED (ctype
)
6148 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6149 /* ... or has undefined overflow while the converted to
6150 type has not, we cannot do the operation in the inner type
6151 as that would introduce undefined overflow. */
6152 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6153 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6154 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6157 /* Pass the constant down and see if we can make a simplification. If
6158 we can, replace this expression with the inner simplification for
6159 possible later conversion to our or some other type. */
6160 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6161 && TREE_CODE (t2
) == INTEGER_CST
6162 && !TREE_OVERFLOW (t2
)
6163 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6165 ? ctype
: NULL_TREE
,
6166 strict_overflow_p
))))
6171 /* If widening the type changes it from signed to unsigned, then we
6172 must avoid building ABS_EXPR itself as unsigned. */
6173 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6175 tree cstype
= (*signed_type_for
) (ctype
);
6176 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6179 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6180 return fold_convert (ctype
, t1
);
6184 /* If the constant is negative, we cannot simplify this. */
6185 if (tree_int_cst_sgn (c
) == -1)
6189 /* For division and modulus, type can't be unsigned, as e.g.
6190 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6191 For signed types, even with wrapping overflow, this is fine. */
6192 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6194 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6196 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6199 case MIN_EXPR
: case MAX_EXPR
:
6200 /* If widening the type changes the signedness, then we can't perform
6201 this optimization as that changes the result. */
6202 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6205 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6206 sub_strict_overflow_p
= false;
6207 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6208 &sub_strict_overflow_p
)) != 0
6209 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6210 &sub_strict_overflow_p
)) != 0)
6212 if (tree_int_cst_sgn (c
) < 0)
6213 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6214 if (sub_strict_overflow_p
)
6215 *strict_overflow_p
= true;
6216 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6217 fold_convert (ctype
, t2
));
6221 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6222 /* If the second operand is constant, this is a multiplication
6223 or floor division, by a power of two, so we can treat it that
6224 way unless the multiplier or divisor overflows. Signed
6225 left-shift overflow is implementation-defined rather than
6226 undefined in C90, so do not convert signed left shift into
6228 if (TREE_CODE (op1
) == INTEGER_CST
6229 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6230 /* const_binop may not detect overflow correctly,
6231 so check for it explicitly here. */
6232 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6233 && 0 != (t1
= fold_convert (ctype
,
6234 const_binop (LSHIFT_EXPR
,
6237 && !TREE_OVERFLOW (t1
))
6238 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6239 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6241 fold_convert (ctype
, op0
),
6243 c
, code
, wide_type
, strict_overflow_p
);
6246 case PLUS_EXPR
: case MINUS_EXPR
:
6247 /* See if we can eliminate the operation on both sides. If we can, we
6248 can return a new PLUS or MINUS. If we can't, the only remaining
6249 cases where we can do anything are if the second operand is a
6251 sub_strict_overflow_p
= false;
6252 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6253 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6254 if (t1
!= 0 && t2
!= 0
6255 && (code
== MULT_EXPR
6256 /* If not multiplication, we can only do this if both operands
6257 are divisible by c. */
6258 || (multiple_of_p (ctype
, op0
, c
)
6259 && multiple_of_p (ctype
, op1
, c
))))
6261 if (sub_strict_overflow_p
)
6262 *strict_overflow_p
= true;
6263 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6264 fold_convert (ctype
, t2
));
6267 /* If this was a subtraction, negate OP1 and set it to be an addition.
6268 This simplifies the logic below. */
6269 if (tcode
== MINUS_EXPR
)
6271 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6272 /* If OP1 was not easily negatable, the constant may be OP0. */
6273 if (TREE_CODE (op0
) == INTEGER_CST
)
6275 std::swap (op0
, op1
);
6280 if (TREE_CODE (op1
) != INTEGER_CST
)
6283 /* If either OP1 or C are negative, this optimization is not safe for
6284 some of the division and remainder types while for others we need
6285 to change the code. */
6286 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6288 if (code
== CEIL_DIV_EXPR
)
6289 code
= FLOOR_DIV_EXPR
;
6290 else if (code
== FLOOR_DIV_EXPR
)
6291 code
= CEIL_DIV_EXPR
;
6292 else if (code
!= MULT_EXPR
6293 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6297 /* If it's a multiply or a division/modulus operation of a multiple
6298 of our constant, do the operation and verify it doesn't overflow. */
6299 if (code
== MULT_EXPR
6300 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6302 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6303 fold_convert (ctype
, c
));
6304 /* We allow the constant to overflow with wrapping semantics. */
6306 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6312 /* If we have an unsigned type, we cannot widen the operation since it
6313 will change the result if the original computation overflowed. */
6314 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6317 /* If we were able to eliminate our operation from the first side,
6318 apply our operation to the second side and reform the PLUS. */
6319 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6320 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6322 /* The last case is if we are a multiply. In that case, we can
6323 apply the distributive law to commute the multiply and addition
6324 if the multiplication of the constants doesn't overflow
6325 and overflow is defined. With undefined overflow
6326 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6327 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6328 return fold_build2 (tcode
, ctype
,
6329 fold_build2 (code
, ctype
,
6330 fold_convert (ctype
, op0
),
6331 fold_convert (ctype
, c
)),
6337 /* We have a special case here if we are doing something like
6338 (C * 8) % 4 since we know that's zero. */
6339 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6340 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6341 /* If the multiplication can overflow we cannot optimize this. */
6342 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6343 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6344 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6346 *strict_overflow_p
= true;
6347 return omit_one_operand (type
, integer_zero_node
, op0
);
6350 /* ... fall through ... */
6352 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6353 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6354 /* If we can extract our operation from the LHS, do so and return a
6355 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6356 do something only if the second operand is a constant. */
6358 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6359 strict_overflow_p
)) != 0)
6360 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6361 fold_convert (ctype
, op1
));
6362 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6363 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6364 strict_overflow_p
)) != 0)
6365 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6366 fold_convert (ctype
, t1
));
6367 else if (TREE_CODE (op1
) != INTEGER_CST
)
6370 /* If these are the same operation types, we can associate them
6371 assuming no overflow. */
6374 bool overflow_p
= false;
6375 bool overflow_mul_p
;
6376 signop sign
= TYPE_SIGN (ctype
);
6377 unsigned prec
= TYPE_PRECISION (ctype
);
6378 wide_int mul
= wi::mul (wide_int::from (op1
, prec
, sign
),
6379 wide_int::from (c
, prec
, sign
),
6380 sign
, &overflow_mul_p
);
6381 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6383 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6386 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6387 wide_int_to_tree (ctype
, mul
));
6390 /* If these operations "cancel" each other, we have the main
6391 optimizations of this pass, which occur when either constant is a
6392 multiple of the other, in which case we replace this with either an
6393 operation or CODE or TCODE.
6395 If we have an unsigned type, we cannot do this since it will change
6396 the result if the original computation overflowed. */
6397 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6398 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6399 || (tcode
== MULT_EXPR
6400 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6401 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6402 && code
!= MULT_EXPR
)))
6404 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6406 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6407 *strict_overflow_p
= true;
6408 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6409 fold_convert (ctype
,
6410 const_binop (TRUNC_DIV_EXPR
,
6413 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6415 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6416 *strict_overflow_p
= true;
6417 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6418 fold_convert (ctype
,
6419 const_binop (TRUNC_DIV_EXPR
,
6432 /* Return a node which has the indicated constant VALUE (either 0 or
6433 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6434 and is of the indicated TYPE. */
6437 constant_boolean_node (bool value
, tree type
)
6439 if (type
== integer_type_node
)
6440 return value
? integer_one_node
: integer_zero_node
;
6441 else if (type
== boolean_type_node
)
6442 return value
? boolean_true_node
: boolean_false_node
;
6443 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6444 return build_vector_from_val (type
,
6445 build_int_cst (TREE_TYPE (type
),
6448 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6452 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6453 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6454 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6455 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6456 COND is the first argument to CODE; otherwise (as in the example
6457 given here), it is the second argument. TYPE is the type of the
6458 original expression. Return NULL_TREE if no simplification is
6462 fold_binary_op_with_conditional_arg (location_t loc
,
6463 enum tree_code code
,
6464 tree type
, tree op0
, tree op1
,
6465 tree cond
, tree arg
, int cond_first_p
)
6467 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6468 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6469 tree test
, true_value
, false_value
;
6470 tree lhs
= NULL_TREE
;
6471 tree rhs
= NULL_TREE
;
6472 enum tree_code cond_code
= COND_EXPR
;
6474 if (TREE_CODE (cond
) == COND_EXPR
6475 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6477 test
= TREE_OPERAND (cond
, 0);
6478 true_value
= TREE_OPERAND (cond
, 1);
6479 false_value
= TREE_OPERAND (cond
, 2);
6480 /* If this operand throws an expression, then it does not make
6481 sense to try to perform a logical or arithmetic operation
6483 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6485 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6488 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6489 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6491 tree testtype
= TREE_TYPE (cond
);
6493 true_value
= constant_boolean_node (true, testtype
);
6494 false_value
= constant_boolean_node (false, testtype
);
6497 /* Detect the case of mixing vector and scalar types - bail out. */
6500 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6501 cond_code
= VEC_COND_EXPR
;
6503 /* This transformation is only worthwhile if we don't have to wrap ARG
6504 in a SAVE_EXPR and the operation can be simplified without recursing
6505 on at least one of the branches once its pushed inside the COND_EXPR. */
6506 if (!TREE_CONSTANT (arg
)
6507 && (TREE_SIDE_EFFECTS (arg
)
6508 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6509 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6512 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6515 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6517 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6519 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6523 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6525 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6527 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6530 /* Check that we have simplified at least one of the branches. */
6531 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6534 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6538 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6540 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6541 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6542 ADDEND is the same as X.
6544 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6545 and finite. The problematic cases are when X is zero, and its mode
6546 has signed zeros. In the case of rounding towards -infinity,
6547 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6548 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6551 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6553 if (!real_zerop (addend
))
6556 /* Don't allow the fold with -fsignaling-nans. */
6557 if (HONOR_SNANS (element_mode (type
)))
6560 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6561 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6564 /* In a vector or complex, we would need to check the sign of all zeros. */
6565 if (TREE_CODE (addend
) != REAL_CST
)
6568 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6569 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6572 /* The mode has signed zeros, and we have to honor their sign.
6573 In this situation, there is only one case we can return true for.
6574 X - 0 is the same as X unless rounding towards -infinity is
6576 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6579 /* Subroutine of fold() that optimizes comparisons of a division by
6580 a nonzero integer constant against an integer constant, i.e.
6583 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6584 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6585 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6587 The function returns the constant folded tree if a simplification
6588 can be made, and NULL_TREE otherwise. */
6591 fold_div_compare (location_t loc
,
6592 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6594 tree prod
, tmp
, hi
, lo
;
6595 tree arg00
= TREE_OPERAND (arg0
, 0);
6596 tree arg01
= TREE_OPERAND (arg0
, 1);
6597 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6598 bool neg_overflow
= false;
6601 /* We have to do this the hard way to detect unsigned overflow.
6602 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6603 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6604 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6605 neg_overflow
= false;
6607 if (sign
== UNSIGNED
)
6609 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6610 build_int_cst (TREE_TYPE (arg01
), 1));
6613 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6614 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6615 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6616 -1, overflow
| TREE_OVERFLOW (prod
));
6618 else if (tree_int_cst_sgn (arg01
) >= 0)
6620 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6621 build_int_cst (TREE_TYPE (arg01
), 1));
6622 switch (tree_int_cst_sgn (arg1
))
6625 neg_overflow
= true;
6626 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6631 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6636 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6646 /* A negative divisor reverses the relational operators. */
6647 code
= swap_tree_comparison (code
);
6649 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6650 build_int_cst (TREE_TYPE (arg01
), 1));
6651 switch (tree_int_cst_sgn (arg1
))
6654 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6659 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6664 neg_overflow
= true;
6665 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6677 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6678 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6679 if (TREE_OVERFLOW (hi
))
6680 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6681 if (TREE_OVERFLOW (lo
))
6682 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6683 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6686 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6687 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6688 if (TREE_OVERFLOW (hi
))
6689 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6690 if (TREE_OVERFLOW (lo
))
6691 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6692 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6695 if (TREE_OVERFLOW (lo
))
6697 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6698 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6700 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6703 if (TREE_OVERFLOW (hi
))
6705 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6706 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6708 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6711 if (TREE_OVERFLOW (hi
))
6713 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6714 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6716 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6719 if (TREE_OVERFLOW (lo
))
6721 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6722 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6724 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6734 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6735 equality/inequality test, then return a simplified form of the test
6736 using a sign testing. Otherwise return NULL. TYPE is the desired
6740 fold_single_bit_test_into_sign_test (location_t loc
,
6741 enum tree_code code
, tree arg0
, tree arg1
,
6744 /* If this is testing a single bit, we can optimize the test. */
6745 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6746 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6747 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6749 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6750 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6751 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6753 if (arg00
!= NULL_TREE
6754 /* This is only a win if casting to a signed type is cheap,
6755 i.e. when arg00's type is not a partial mode. */
6756 && TYPE_PRECISION (TREE_TYPE (arg00
))
6757 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6759 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6760 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6762 fold_convert_loc (loc
, stype
, arg00
),
6763 build_int_cst (stype
, 0));
6770 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6771 equality/inequality test, then return a simplified form of
6772 the test using shifts and logical operations. Otherwise return
6773 NULL. TYPE is the desired result type. */
6776 fold_single_bit_test (location_t loc
, enum tree_code code
,
6777 tree arg0
, tree arg1
, tree result_type
)
6779 /* If this is testing a single bit, we can optimize the test. */
6780 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6781 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6782 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6784 tree inner
= TREE_OPERAND (arg0
, 0);
6785 tree type
= TREE_TYPE (arg0
);
6786 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6787 machine_mode operand_mode
= TYPE_MODE (type
);
6789 tree signed_type
, unsigned_type
, intermediate_type
;
6792 /* First, see if we can fold the single bit test into a sign-bit
6794 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6799 /* Otherwise we have (A & C) != 0 where C is a single bit,
6800 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6801 Similarly for (A & C) == 0. */
6803 /* If INNER is a right shift of a constant and it plus BITNUM does
6804 not overflow, adjust BITNUM and INNER. */
6805 if (TREE_CODE (inner
) == RSHIFT_EXPR
6806 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6807 && bitnum
< TYPE_PRECISION (type
)
6808 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6809 TYPE_PRECISION (type
) - bitnum
))
6811 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6812 inner
= TREE_OPERAND (inner
, 0);
6815 /* If we are going to be able to omit the AND below, we must do our
6816 operations as unsigned. If we must use the AND, we have a choice.
6817 Normally unsigned is faster, but for some machines signed is. */
6818 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
6819 && !flag_syntax_only
) ? 0 : 1;
6821 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6822 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6823 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6824 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6827 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6828 inner
, size_int (bitnum
));
6830 one
= build_int_cst (intermediate_type
, 1);
6832 if (code
== EQ_EXPR
)
6833 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6835 /* Put the AND last so it can combine with more things. */
6836 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6838 /* Make sure to return the proper type. */
6839 inner
= fold_convert_loc (loc
, result_type
, inner
);
6846 /* Check whether we are allowed to reorder operands arg0 and arg1,
6847 such that the evaluation of arg1 occurs before arg0. */
6850 reorder_operands_p (const_tree arg0
, const_tree arg1
)
6852 if (! flag_evaluation_order
)
6854 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
6856 return ! TREE_SIDE_EFFECTS (arg0
)
6857 && ! TREE_SIDE_EFFECTS (arg1
);
6860 /* Test whether it is preferable two swap two operands, ARG0 and
6861 ARG1, for example because ARG0 is an integer constant and ARG1
6862 isn't. If REORDER is true, only recommend swapping if we can
6863 evaluate the operands in reverse order. */
6866 tree_swap_operands_p (const_tree arg0
, const_tree arg1
, bool reorder
)
6868 if (CONSTANT_CLASS_P (arg1
))
6870 if (CONSTANT_CLASS_P (arg0
))
6876 if (TREE_CONSTANT (arg1
))
6878 if (TREE_CONSTANT (arg0
))
6881 if (reorder
&& flag_evaluation_order
6882 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
6885 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6886 for commutative and comparison operators. Ensuring a canonical
6887 form allows the optimizers to find additional redundancies without
6888 having to explicitly check for both orderings. */
6889 if (TREE_CODE (arg0
) == SSA_NAME
6890 && TREE_CODE (arg1
) == SSA_NAME
6891 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6894 /* Put SSA_NAMEs last. */
6895 if (TREE_CODE (arg1
) == SSA_NAME
)
6897 if (TREE_CODE (arg0
) == SSA_NAME
)
6900 /* Put variables last. */
6910 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6911 means A >= Y && A != MAX, but in this case we know that
6912 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6915 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6917 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6919 if (TREE_CODE (bound
) == LT_EXPR
)
6920 a
= TREE_OPERAND (bound
, 0);
6921 else if (TREE_CODE (bound
) == GT_EXPR
)
6922 a
= TREE_OPERAND (bound
, 1);
6926 typea
= TREE_TYPE (a
);
6927 if (!INTEGRAL_TYPE_P (typea
)
6928 && !POINTER_TYPE_P (typea
))
6931 if (TREE_CODE (ineq
) == LT_EXPR
)
6933 a1
= TREE_OPERAND (ineq
, 1);
6934 y
= TREE_OPERAND (ineq
, 0);
6936 else if (TREE_CODE (ineq
) == GT_EXPR
)
6938 a1
= TREE_OPERAND (ineq
, 0);
6939 y
= TREE_OPERAND (ineq
, 1);
6944 if (TREE_TYPE (a1
) != typea
)
6947 if (POINTER_TYPE_P (typea
))
6949 /* Convert the pointer types into integer before taking the difference. */
6950 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6951 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6952 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6955 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6957 if (!diff
|| !integer_onep (diff
))
6960 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6963 /* Fold a sum or difference of at least one multiplication.
6964 Returns the folded tree or NULL if no simplification could be made. */
6967 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6968 tree arg0
, tree arg1
)
6970 tree arg00
, arg01
, arg10
, arg11
;
6971 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6973 /* (A * C) +- (B * C) -> (A+-B) * C.
6974 (A * C) +- A -> A * (C+-1).
6975 We are most concerned about the case where C is a constant,
6976 but other combinations show up during loop reduction. Since
6977 it is not difficult, try all four possibilities. */
6979 if (TREE_CODE (arg0
) == MULT_EXPR
)
6981 arg00
= TREE_OPERAND (arg0
, 0);
6982 arg01
= TREE_OPERAND (arg0
, 1);
6984 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6986 arg00
= build_one_cst (type
);
6991 /* We cannot generate constant 1 for fract. */
6992 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6995 arg01
= build_one_cst (type
);
6997 if (TREE_CODE (arg1
) == MULT_EXPR
)
6999 arg10
= TREE_OPERAND (arg1
, 0);
7000 arg11
= TREE_OPERAND (arg1
, 1);
7002 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7004 arg10
= build_one_cst (type
);
7005 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7006 the purpose of this canonicalization. */
7007 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
7008 && negate_expr_p (arg1
)
7009 && code
== PLUS_EXPR
)
7011 arg11
= negate_expr (arg1
);
7019 /* We cannot generate constant 1 for fract. */
7020 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7023 arg11
= build_one_cst (type
);
7027 if (operand_equal_p (arg01
, arg11
, 0))
7028 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7029 else if (operand_equal_p (arg00
, arg10
, 0))
7030 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7031 else if (operand_equal_p (arg00
, arg11
, 0))
7032 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7033 else if (operand_equal_p (arg01
, arg10
, 0))
7034 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7036 /* No identical multiplicands; see if we can find a common
7037 power-of-two factor in non-power-of-two multiplies. This
7038 can help in multi-dimensional array access. */
7039 else if (tree_fits_shwi_p (arg01
)
7040 && tree_fits_shwi_p (arg11
))
7042 HOST_WIDE_INT int01
, int11
, tmp
;
7045 int01
= tree_to_shwi (arg01
);
7046 int11
= tree_to_shwi (arg11
);
7048 /* Move min of absolute values to int11. */
7049 if (absu_hwi (int01
) < absu_hwi (int11
))
7051 tmp
= int01
, int01
= int11
, int11
= tmp
;
7052 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7059 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
7060 /* The remainder should not be a constant, otherwise we
7061 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7062 increased the number of multiplications necessary. */
7063 && TREE_CODE (arg10
) != INTEGER_CST
)
7065 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7066 build_int_cst (TREE_TYPE (arg00
),
7071 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7076 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7077 fold_build2_loc (loc
, code
, type
,
7078 fold_convert_loc (loc
, type
, alt0
),
7079 fold_convert_loc (loc
, type
, alt1
)),
7080 fold_convert_loc (loc
, type
, same
));
7085 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7086 specified by EXPR into the buffer PTR of length LEN bytes.
7087 Return the number of bytes placed in the buffer, or zero
7091 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7093 tree type
= TREE_TYPE (expr
);
7094 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7095 int byte
, offset
, word
, words
;
7096 unsigned char value
;
7098 if ((off
== -1 && total_bytes
> len
)
7099 || off
>= total_bytes
)
7103 words
= total_bytes
/ UNITS_PER_WORD
;
7105 for (byte
= 0; byte
< total_bytes
; byte
++)
7107 int bitpos
= byte
* BITS_PER_UNIT
;
7108 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7110 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7112 if (total_bytes
> UNITS_PER_WORD
)
7114 word
= byte
/ UNITS_PER_WORD
;
7115 if (WORDS_BIG_ENDIAN
)
7116 word
= (words
- 1) - word
;
7117 offset
= word
* UNITS_PER_WORD
;
7118 if (BYTES_BIG_ENDIAN
)
7119 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7121 offset
+= byte
% UNITS_PER_WORD
;
7124 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7126 && offset
- off
< len
)
7127 ptr
[offset
- off
] = value
;
7129 return MIN (len
, total_bytes
- off
);
7133 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7134 specified by EXPR into the buffer PTR of length LEN bytes.
7135 Return the number of bytes placed in the buffer, or zero
7139 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7141 tree type
= TREE_TYPE (expr
);
7142 machine_mode mode
= TYPE_MODE (type
);
7143 int total_bytes
= GET_MODE_SIZE (mode
);
7144 FIXED_VALUE_TYPE value
;
7145 tree i_value
, i_type
;
7147 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7150 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7152 if (NULL_TREE
== i_type
7153 || TYPE_PRECISION (i_type
) != total_bytes
)
7156 value
= TREE_FIXED_CST (expr
);
7157 i_value
= double_int_to_tree (i_type
, value
.data
);
7159 return native_encode_int (i_value
, ptr
, len
, off
);
7163 /* Subroutine of native_encode_expr. Encode the REAL_CST
7164 specified by EXPR into the buffer PTR of length LEN bytes.
7165 Return the number of bytes placed in the buffer, or zero
7169 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7171 tree type
= TREE_TYPE (expr
);
7172 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7173 int byte
, offset
, word
, words
, bitpos
;
7174 unsigned char value
;
7176 /* There are always 32 bits in each long, no matter the size of
7177 the hosts long. We handle floating point representations with
7181 if ((off
== -1 && total_bytes
> len
)
7182 || off
>= total_bytes
)
7186 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7188 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7190 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7191 bitpos
+= BITS_PER_UNIT
)
7193 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7194 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7196 if (UNITS_PER_WORD
< 4)
7198 word
= byte
/ UNITS_PER_WORD
;
7199 if (WORDS_BIG_ENDIAN
)
7200 word
= (words
- 1) - word
;
7201 offset
= word
* UNITS_PER_WORD
;
7202 if (BYTES_BIG_ENDIAN
)
7203 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7205 offset
+= byte
% UNITS_PER_WORD
;
7208 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7209 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7211 && offset
- off
< len
)
7212 ptr
[offset
- off
] = value
;
7214 return MIN (len
, total_bytes
- off
);
7217 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7218 specified by EXPR into the buffer PTR of length LEN bytes.
7219 Return the number of bytes placed in the buffer, or zero
7223 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7228 part
= TREE_REALPART (expr
);
7229 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7233 part
= TREE_IMAGPART (expr
);
7235 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7236 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7240 return rsize
+ isize
;
7244 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7245 specified by EXPR into the buffer PTR of length LEN bytes.
7246 Return the number of bytes placed in the buffer, or zero
7250 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7257 count
= VECTOR_CST_NELTS (expr
);
7258 itype
= TREE_TYPE (TREE_TYPE (expr
));
7259 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7260 for (i
= 0; i
< count
; i
++)
7267 elem
= VECTOR_CST_ELT (expr
, i
);
7268 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7269 if ((off
== -1 && res
!= size
)
7282 /* Subroutine of native_encode_expr. Encode the STRING_CST
7283 specified by EXPR into the buffer PTR of length LEN bytes.
7284 Return the number of bytes placed in the buffer, or zero
7288 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7290 tree type
= TREE_TYPE (expr
);
7291 HOST_WIDE_INT total_bytes
;
7293 if (TREE_CODE (type
) != ARRAY_TYPE
7294 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7295 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7296 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7298 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7299 if ((off
== -1 && total_bytes
> len
)
7300 || off
>= total_bytes
)
7304 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7307 if (off
< TREE_STRING_LENGTH (expr
))
7309 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7310 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7312 memset (ptr
+ written
, 0,
7313 MIN (total_bytes
- written
, len
- written
));
7316 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7317 return MIN (total_bytes
- off
, len
);
7321 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7322 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7323 buffer PTR of length LEN bytes. If OFF is not -1 then start
7324 the encoding at byte offset OFF and encode at most LEN bytes.
7325 Return the number of bytes placed in the buffer, or zero upon failure. */
7328 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7330 /* We don't support starting at negative offset and -1 is special. */
7334 switch (TREE_CODE (expr
))
7337 return native_encode_int (expr
, ptr
, len
, off
);
7340 return native_encode_real (expr
, ptr
, len
, off
);
7343 return native_encode_fixed (expr
, ptr
, len
, off
);
7346 return native_encode_complex (expr
, ptr
, len
, off
);
7349 return native_encode_vector (expr
, ptr
, len
, off
);
7352 return native_encode_string (expr
, ptr
, len
, off
);
7360 /* Subroutine of native_interpret_expr. Interpret the contents of
7361 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7362 If the buffer cannot be interpreted, return NULL_TREE. */
7365 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7367 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7369 if (total_bytes
> len
7370 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7373 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7375 return wide_int_to_tree (type
, result
);
7379 /* Subroutine of native_interpret_expr. Interpret the contents of
7380 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7381 If the buffer cannot be interpreted, return NULL_TREE. */
7384 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7386 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7388 FIXED_VALUE_TYPE fixed_value
;
7390 if (total_bytes
> len
7391 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7394 result
= double_int::from_buffer (ptr
, total_bytes
);
7395 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7397 return build_fixed (type
, fixed_value
);
7401 /* Subroutine of native_interpret_expr. Interpret the contents of
7402 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7403 If the buffer cannot be interpreted, return NULL_TREE. */
7406 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7408 machine_mode mode
= TYPE_MODE (type
);
7409 int total_bytes
= GET_MODE_SIZE (mode
);
7410 unsigned char value
;
7411 /* There are always 32 bits in each long, no matter the size of
7412 the hosts long. We handle floating point representations with
7417 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7418 if (total_bytes
> len
|| total_bytes
> 24)
7420 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7422 memset (tmp
, 0, sizeof (tmp
));
7423 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7424 bitpos
+= BITS_PER_UNIT
)
7426 /* Both OFFSET and BYTE index within a long;
7427 bitpos indexes the whole float. */
7428 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7429 if (UNITS_PER_WORD
< 4)
7431 int word
= byte
/ UNITS_PER_WORD
;
7432 if (WORDS_BIG_ENDIAN
)
7433 word
= (words
- 1) - word
;
7434 offset
= word
* UNITS_PER_WORD
;
7435 if (BYTES_BIG_ENDIAN
)
7436 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7438 offset
+= byte
% UNITS_PER_WORD
;
7443 if (BYTES_BIG_ENDIAN
)
7445 /* Reverse bytes within each long, or within the entire float
7446 if it's smaller than a long (for HFmode). */
7447 offset
= MIN (3, total_bytes
- 1) - offset
;
7448 gcc_assert (offset
>= 0);
7451 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7453 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7456 real_from_target (&r
, tmp
, mode
);
7457 return build_real (type
, r
);
7461 /* Subroutine of native_interpret_expr. Interpret the contents of
7462 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7463 If the buffer cannot be interpreted, return NULL_TREE. */
7466 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7468 tree etype
, rpart
, ipart
;
7471 etype
= TREE_TYPE (type
);
7472 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7475 rpart
= native_interpret_expr (etype
, ptr
, size
);
7478 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7481 return build_complex (type
, rpart
, ipart
);
7485 /* Subroutine of native_interpret_expr. Interpret the contents of
7486 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7487 If the buffer cannot be interpreted, return NULL_TREE. */
7490 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7496 etype
= TREE_TYPE (type
);
7497 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7498 count
= TYPE_VECTOR_SUBPARTS (type
);
7499 if (size
* count
> len
)
7502 elements
= XALLOCAVEC (tree
, count
);
7503 for (i
= count
- 1; i
>= 0; i
--)
7505 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7510 return build_vector (type
, elements
);
7514 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7515 the buffer PTR of length LEN as a constant of type TYPE. For
7516 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7517 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7518 return NULL_TREE. */
7521 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7523 switch (TREE_CODE (type
))
7529 case REFERENCE_TYPE
:
7530 return native_interpret_int (type
, ptr
, len
);
7533 return native_interpret_real (type
, ptr
, len
);
7535 case FIXED_POINT_TYPE
:
7536 return native_interpret_fixed (type
, ptr
, len
);
7539 return native_interpret_complex (type
, ptr
, len
);
7542 return native_interpret_vector (type
, ptr
, len
);
7549 /* Returns true if we can interpret the contents of a native encoding
7553 can_native_interpret_type_p (tree type
)
7555 switch (TREE_CODE (type
))
7561 case REFERENCE_TYPE
:
7562 case FIXED_POINT_TYPE
:
7572 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7573 TYPE at compile-time. If we're unable to perform the conversion
7574 return NULL_TREE. */
7577 fold_view_convert_expr (tree type
, tree expr
)
7579 /* We support up to 512-bit values (for V8DFmode). */
7580 unsigned char buffer
[64];
7583 /* Check that the host and target are sane. */
7584 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7587 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7591 return native_interpret_expr (type
, buffer
, len
);
7594 /* Build an expression for the address of T. Folds away INDIRECT_REF
7595 to avoid confusing the gimplify process. */
7598 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7600 /* The size of the object is not relevant when talking about its address. */
7601 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7602 t
= TREE_OPERAND (t
, 0);
7604 if (TREE_CODE (t
) == INDIRECT_REF
)
7606 t
= TREE_OPERAND (t
, 0);
7608 if (TREE_TYPE (t
) != ptrtype
)
7609 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7611 else if (TREE_CODE (t
) == MEM_REF
7612 && integer_zerop (TREE_OPERAND (t
, 1)))
7613 return TREE_OPERAND (t
, 0);
7614 else if (TREE_CODE (t
) == MEM_REF
7615 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7616 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7617 TREE_OPERAND (t
, 0),
7618 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7619 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7621 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7623 if (TREE_TYPE (t
) != ptrtype
)
7624 t
= fold_convert_loc (loc
, ptrtype
, t
);
7627 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7632 /* Build an expression for the address of T. */
7635 build_fold_addr_expr_loc (location_t loc
, tree t
)
7637 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7639 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7642 /* Fold a unary expression of code CODE and type TYPE with operand
7643 OP0. Return the folded expression if folding is successful.
7644 Otherwise, return NULL_TREE. */
7647 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7651 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7653 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7654 && TREE_CODE_LENGTH (code
) == 1);
7659 if (CONVERT_EXPR_CODE_P (code
)
7660 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7662 /* Don't use STRIP_NOPS, because signedness of argument type
7664 STRIP_SIGN_NOPS (arg0
);
7668 /* Strip any conversions that don't change the mode. This
7669 is safe for every expression, except for a comparison
7670 expression because its signedness is derived from its
7673 Note that this is done as an internal manipulation within
7674 the constant folder, in order to find the simplest
7675 representation of the arguments so that their form can be
7676 studied. In any cases, the appropriate type conversions
7677 should be put back in the tree that will get out of the
7682 if (CONSTANT_CLASS_P (arg0
))
7684 tree tem
= const_unop (code
, type
, arg0
);
7687 if (TREE_TYPE (tem
) != type
)
7688 tem
= fold_convert_loc (loc
, type
, tem
);
7694 tem
= generic_simplify (loc
, code
, type
, op0
);
7698 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7700 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7701 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7702 fold_build1_loc (loc
, code
, type
,
7703 fold_convert_loc (loc
, TREE_TYPE (op0
),
7704 TREE_OPERAND (arg0
, 1))));
7705 else if (TREE_CODE (arg0
) == COND_EXPR
)
7707 tree arg01
= TREE_OPERAND (arg0
, 1);
7708 tree arg02
= TREE_OPERAND (arg0
, 2);
7709 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7710 arg01
= fold_build1_loc (loc
, code
, type
,
7711 fold_convert_loc (loc
,
7712 TREE_TYPE (op0
), arg01
));
7713 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7714 arg02
= fold_build1_loc (loc
, code
, type
,
7715 fold_convert_loc (loc
,
7716 TREE_TYPE (op0
), arg02
));
7717 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7720 /* If this was a conversion, and all we did was to move into
7721 inside the COND_EXPR, bring it back out. But leave it if
7722 it is a conversion from integer to integer and the
7723 result precision is no wider than a word since such a
7724 conversion is cheap and may be optimized away by combine,
7725 while it couldn't if it were outside the COND_EXPR. Then return
7726 so we don't get into an infinite recursion loop taking the
7727 conversion out and then back in. */
7729 if ((CONVERT_EXPR_CODE_P (code
)
7730 || code
== NON_LVALUE_EXPR
)
7731 && TREE_CODE (tem
) == COND_EXPR
7732 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7733 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7734 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7735 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7736 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7737 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7738 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7740 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7741 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7742 || flag_syntax_only
))
7743 tem
= build1_loc (loc
, code
, type
,
7745 TREE_TYPE (TREE_OPERAND
7746 (TREE_OPERAND (tem
, 1), 0)),
7747 TREE_OPERAND (tem
, 0),
7748 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7749 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7757 case NON_LVALUE_EXPR
:
7758 if (!maybe_lvalue_p (op0
))
7759 return fold_convert_loc (loc
, type
, op0
);
7764 case FIX_TRUNC_EXPR
:
7765 if (COMPARISON_CLASS_P (op0
))
7767 /* If we have (type) (a CMP b) and type is an integral type, return
7768 new expression involving the new type. Canonicalize
7769 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7771 Do not fold the result as that would not simplify further, also
7772 folding again results in recursions. */
7773 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7774 return build2_loc (loc
, TREE_CODE (op0
), type
,
7775 TREE_OPERAND (op0
, 0),
7776 TREE_OPERAND (op0
, 1));
7777 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7778 && TREE_CODE (type
) != VECTOR_TYPE
)
7779 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7780 constant_boolean_node (true, type
),
7781 constant_boolean_node (false, type
));
7784 /* Handle (T *)&A.B.C for A being of type T and B and C
7785 living at offset zero. This occurs frequently in
7786 C++ upcasting and then accessing the base. */
7787 if (TREE_CODE (op0
) == ADDR_EXPR
7788 && POINTER_TYPE_P (type
)
7789 && handled_component_p (TREE_OPERAND (op0
, 0)))
7791 HOST_WIDE_INT bitsize
, bitpos
;
7794 int unsignedp
, reversep
, volatilep
;
7796 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
7797 &offset
, &mode
, &unsignedp
, &reversep
,
7799 /* If the reference was to a (constant) zero offset, we can use
7800 the address of the base if it has the same base type
7801 as the result type and the pointer type is unqualified. */
7802 if (! offset
&& bitpos
== 0
7803 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7804 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7805 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7806 return fold_convert_loc (loc
, type
,
7807 build_fold_addr_expr_loc (loc
, base
));
7810 if (TREE_CODE (op0
) == MODIFY_EXPR
7811 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7812 /* Detect assigning a bitfield. */
7813 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7815 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7817 /* Don't leave an assignment inside a conversion
7818 unless assigning a bitfield. */
7819 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7820 /* First do the assignment, then return converted constant. */
7821 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7822 TREE_NO_WARNING (tem
) = 1;
7823 TREE_USED (tem
) = 1;
7827 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7828 constants (if x has signed type, the sign bit cannot be set
7829 in c). This folds extension into the BIT_AND_EXPR.
7830 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7831 very likely don't have maximal range for their precision and this
7832 transformation effectively doesn't preserve non-maximal ranges. */
7833 if (TREE_CODE (type
) == INTEGER_TYPE
7834 && TREE_CODE (op0
) == BIT_AND_EXPR
7835 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7837 tree and_expr
= op0
;
7838 tree and0
= TREE_OPERAND (and_expr
, 0);
7839 tree and1
= TREE_OPERAND (and_expr
, 1);
7842 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7843 || (TYPE_PRECISION (type
)
7844 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7846 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7847 <= HOST_BITS_PER_WIDE_INT
7848 && tree_fits_uhwi_p (and1
))
7850 unsigned HOST_WIDE_INT cst
;
7852 cst
= tree_to_uhwi (and1
);
7853 cst
&= HOST_WIDE_INT_M1U
7854 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7855 change
= (cst
== 0);
7857 && !flag_syntax_only
7858 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
7861 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7862 and0
= fold_convert_loc (loc
, uns
, and0
);
7863 and1
= fold_convert_loc (loc
, uns
, and1
);
7868 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7869 TREE_OVERFLOW (and1
));
7870 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7871 fold_convert_loc (loc
, type
, and0
), tem
);
7875 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7876 cast (T1)X will fold away. We assume that this happens when X itself
7878 if (POINTER_TYPE_P (type
)
7879 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7880 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
7882 tree arg00
= TREE_OPERAND (arg0
, 0);
7883 tree arg01
= TREE_OPERAND (arg0
, 1);
7885 return fold_build_pointer_plus_loc
7886 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7889 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7890 of the same precision, and X is an integer type not narrower than
7891 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7892 if (INTEGRAL_TYPE_P (type
)
7893 && TREE_CODE (op0
) == BIT_NOT_EXPR
7894 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7895 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7896 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7898 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7899 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7900 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7901 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7902 fold_convert_loc (loc
, type
, tem
));
7905 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7906 type of X and Y (integer types only). */
7907 if (INTEGRAL_TYPE_P (type
)
7908 && TREE_CODE (op0
) == MULT_EXPR
7909 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7910 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7912 /* Be careful not to introduce new overflows. */
7914 if (TYPE_OVERFLOW_WRAPS (type
))
7917 mult_type
= unsigned_type_for (type
);
7919 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7921 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7922 fold_convert_loc (loc
, mult_type
,
7923 TREE_OPERAND (op0
, 0)),
7924 fold_convert_loc (loc
, mult_type
,
7925 TREE_OPERAND (op0
, 1)));
7926 return fold_convert_loc (loc
, type
, tem
);
7932 case VIEW_CONVERT_EXPR
:
7933 if (TREE_CODE (op0
) == MEM_REF
)
7935 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
7936 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7937 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
7944 tem
= fold_negate_expr (loc
, arg0
);
7946 return fold_convert_loc (loc
, type
, tem
);
7950 /* Convert fabs((double)float) into (double)fabsf(float). */
7951 if (TREE_CODE (arg0
) == NOP_EXPR
7952 && TREE_CODE (type
) == REAL_TYPE
)
7954 tree targ0
= strip_float_extensions (arg0
);
7956 return fold_convert_loc (loc
, type
,
7957 fold_build1_loc (loc
, ABS_EXPR
,
7964 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7965 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7966 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7967 fold_convert_loc (loc
, type
,
7968 TREE_OPERAND (arg0
, 0)))))
7969 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
7970 fold_convert_loc (loc
, type
,
7971 TREE_OPERAND (arg0
, 1)));
7972 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7973 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7974 fold_convert_loc (loc
, type
,
7975 TREE_OPERAND (arg0
, 1)))))
7976 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
7977 fold_convert_loc (loc
, type
,
7978 TREE_OPERAND (arg0
, 0)), tem
);
7982 case TRUTH_NOT_EXPR
:
7983 /* Note that the operand of this must be an int
7984 and its values must be 0 or 1.
7985 ("true" is a fixed value perhaps depending on the language,
7986 but we don't handle values other than 1 correctly yet.) */
7987 tem
= fold_truth_not_expr (loc
, arg0
);
7990 return fold_convert_loc (loc
, type
, tem
);
7993 /* Fold *&X to X if X is an lvalue. */
7994 if (TREE_CODE (op0
) == ADDR_EXPR
)
7996 tree op00
= TREE_OPERAND (op0
, 0);
7997 if ((TREE_CODE (op00
) == VAR_DECL
7998 || TREE_CODE (op00
) == PARM_DECL
7999 || TREE_CODE (op00
) == RESULT_DECL
)
8000 && !TREE_READONLY (op00
))
8007 } /* switch (code) */
8011 /* If the operation was a conversion do _not_ mark a resulting constant
8012 with TREE_OVERFLOW if the original constant was not. These conversions
8013 have implementation defined behavior and retaining the TREE_OVERFLOW
8014 flag here would confuse later passes such as VRP. */
8016 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
8017 tree type
, tree op0
)
8019 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
8021 && TREE_CODE (res
) == INTEGER_CST
8022 && TREE_CODE (op0
) == INTEGER_CST
8023 && CONVERT_EXPR_CODE_P (code
))
8024 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
8029 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8030 operands OP0 and OP1. LOC is the location of the resulting expression.
8031 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8032 Return the folded expression if folding is successful. Otherwise,
8033 return NULL_TREE. */
8035 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
8036 tree arg0
, tree arg1
, tree op0
, tree op1
)
8040 /* We only do these simplifications if we are optimizing. */
8044 /* Check for things like (A || B) && (A || C). We can convert this
8045 to A || (B && C). Note that either operator can be any of the four
8046 truth and/or operations and the transformation will still be
8047 valid. Also note that we only care about order for the
8048 ANDIF and ORIF operators. If B contains side effects, this
8049 might change the truth-value of A. */
8050 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8051 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
8052 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
8053 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
8054 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
8055 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
8057 tree a00
= TREE_OPERAND (arg0
, 0);
8058 tree a01
= TREE_OPERAND (arg0
, 1);
8059 tree a10
= TREE_OPERAND (arg1
, 0);
8060 tree a11
= TREE_OPERAND (arg1
, 1);
8061 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
8062 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
8063 && (code
== TRUTH_AND_EXPR
8064 || code
== TRUTH_OR_EXPR
));
8066 if (operand_equal_p (a00
, a10
, 0))
8067 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8068 fold_build2_loc (loc
, code
, type
, a01
, a11
));
8069 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
8070 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8071 fold_build2_loc (loc
, code
, type
, a01
, a10
));
8072 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
8073 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
8074 fold_build2_loc (loc
, code
, type
, a00
, a11
));
8076 /* This case if tricky because we must either have commutative
8077 operators or else A10 must not have side-effects. */
8079 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
8080 && operand_equal_p (a01
, a11
, 0))
8081 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
8082 fold_build2_loc (loc
, code
, type
, a00
, a10
),
8086 /* See if we can build a range comparison. */
8087 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
8090 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
8091 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8093 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8095 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8098 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8099 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8101 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8103 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8106 /* Check for the possibility of merging component references. If our
8107 lhs is another similar operation, try to merge its rhs with our
8108 rhs. Then try to merge our lhs and rhs. */
8109 if (TREE_CODE (arg0
) == code
8110 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8111 TREE_OPERAND (arg0
, 1), arg1
)))
8112 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8114 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8117 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8118 && (code
== TRUTH_AND_EXPR
8119 || code
== TRUTH_ANDIF_EXPR
8120 || code
== TRUTH_OR_EXPR
8121 || code
== TRUTH_ORIF_EXPR
))
8123 enum tree_code ncode
, icode
;
8125 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8126 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8127 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8129 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8130 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8131 We don't want to pack more than two leafs to a non-IF AND/OR
8133 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8134 equal to IF-CODE, then we don't want to add right-hand operand.
8135 If the inner right-hand side of left-hand operand has
8136 side-effects, or isn't simple, then we can't add to it,
8137 as otherwise we might destroy if-sequence. */
8138 if (TREE_CODE (arg0
) == icode
8139 && simple_operand_p_2 (arg1
)
8140 /* Needed for sequence points to handle trappings, and
8142 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8144 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8146 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8149 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8150 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8151 else if (TREE_CODE (arg1
) == icode
8152 && simple_operand_p_2 (arg0
)
8153 /* Needed for sequence points to handle trappings, and
8155 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8157 tem
= fold_build2_loc (loc
, ncode
, type
,
8158 arg0
, TREE_OPERAND (arg1
, 0));
8159 return fold_build2_loc (loc
, icode
, type
, tem
,
8160 TREE_OPERAND (arg1
, 1));
8162 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8164 For sequence point consistancy, we need to check for trapping,
8165 and side-effects. */
8166 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8167 && simple_operand_p_2 (arg1
))
8168 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8174 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8175 by changing CODE to reduce the magnitude of constants involved in
8176 ARG0 of the comparison.
8177 Returns a canonicalized comparison tree if a simplification was
8178 possible, otherwise returns NULL_TREE.
8179 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8180 valid if signed overflow is undefined. */
8183 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8184 tree arg0
, tree arg1
,
8185 bool *strict_overflow_p
)
8187 enum tree_code code0
= TREE_CODE (arg0
);
8188 tree t
, cst0
= NULL_TREE
;
8191 /* Match A +- CST code arg1. We can change this only if overflow
8193 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8194 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8195 /* In principle pointers also have undefined overflow behavior,
8196 but that causes problems elsewhere. */
8197 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8198 && (code0
== MINUS_EXPR
8199 || code0
== PLUS_EXPR
)
8200 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8203 /* Identify the constant in arg0 and its sign. */
8204 cst0
= TREE_OPERAND (arg0
, 1);
8205 sgn0
= tree_int_cst_sgn (cst0
);
8207 /* Overflowed constants and zero will cause problems. */
8208 if (integer_zerop (cst0
)
8209 || TREE_OVERFLOW (cst0
))
8212 /* See if we can reduce the magnitude of the constant in
8213 arg0 by changing the comparison code. */
8214 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8216 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8218 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8219 else if (code
== GT_EXPR
8220 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8222 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8223 else if (code
== LE_EXPR
8224 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8226 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8227 else if (code
== GE_EXPR
8228 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8232 *strict_overflow_p
= true;
8234 /* Now build the constant reduced in magnitude. But not if that
8235 would produce one outside of its types range. */
8236 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8238 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8239 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8241 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8242 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8245 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8246 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8247 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8248 t
= fold_convert (TREE_TYPE (arg1
), t
);
8250 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8253 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8254 overflow further. Try to decrease the magnitude of constants involved
8255 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8256 and put sole constants at the second argument position.
8257 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8260 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8261 tree arg0
, tree arg1
)
8264 bool strict_overflow_p
;
8265 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8266 "when reducing constant in comparison");
8268 /* Try canonicalization by simplifying arg0. */
8269 strict_overflow_p
= false;
8270 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8271 &strict_overflow_p
);
8274 if (strict_overflow_p
)
8275 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8279 /* Try canonicalization by simplifying arg1 using the swapped
8281 code
= swap_tree_comparison (code
);
8282 strict_overflow_p
= false;
8283 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8284 &strict_overflow_p
);
8285 if (t
&& strict_overflow_p
)
8286 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8290 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8291 space. This is used to avoid issuing overflow warnings for
8292 expressions like &p->x which can not wrap. */
8295 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8297 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8304 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8305 if (offset
== NULL_TREE
)
8306 wi_offset
= wi::zero (precision
);
8307 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8313 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8314 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8318 if (!wi::fits_uhwi_p (total
))
8321 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8325 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8327 if (TREE_CODE (base
) == ADDR_EXPR
)
8329 HOST_WIDE_INT base_size
;
8331 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8332 if (base_size
> 0 && size
< base_size
)
8336 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8339 /* Subroutine of fold_binary. This routine performs all of the
8340 transformations that are common to the equality/inequality
8341 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8342 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8343 fold_binary should call fold_binary. Fold a comparison with
8344 tree code CODE and type TYPE with operands OP0 and OP1. Return
8345 the folded comparison or NULL_TREE. */
8348 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8351 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8352 tree arg0
, arg1
, tem
;
8357 STRIP_SIGN_NOPS (arg0
);
8358 STRIP_SIGN_NOPS (arg1
);
8360 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8361 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8363 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8364 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
8365 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8366 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8367 && TREE_CODE (arg1
) == INTEGER_CST
8368 && !TREE_OVERFLOW (arg1
))
8370 const enum tree_code
8371 reverse_op
= TREE_CODE (arg0
) == PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
;
8372 tree const1
= TREE_OPERAND (arg0
, 1);
8373 tree const2
= fold_convert_loc (loc
, TREE_TYPE (const1
), arg1
);
8374 tree variable
= TREE_OPERAND (arg0
, 0);
8375 tree new_const
= int_const_binop (reverse_op
, const2
, const1
);
8377 /* If the constant operation overflowed this can be
8378 simplified as a comparison against INT_MAX/INT_MIN. */
8379 if (TREE_OVERFLOW (new_const
)
8380 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
8382 int const1_sgn
= tree_int_cst_sgn (const1
);
8383 enum tree_code code2
= code
;
8385 /* Get the sign of the constant on the lhs if the
8386 operation were VARIABLE + CONST1. */
8387 if (TREE_CODE (arg0
) == MINUS_EXPR
)
8388 const1_sgn
= -const1_sgn
;
8390 /* The sign of the constant determines if we overflowed
8391 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8392 Canonicalize to the INT_MIN overflow by swapping the comparison
8394 if (const1_sgn
== -1)
8395 code2
= swap_tree_comparison (code
);
8397 /* We now can look at the canonicalized case
8398 VARIABLE + 1 CODE2 INT_MIN
8399 and decide on the result. */
8406 omit_one_operand_loc (loc
, type
, boolean_false_node
, variable
);
8412 omit_one_operand_loc (loc
, type
, boolean_true_node
, variable
);
8421 fold_overflow_warning ("assuming signed overflow does not occur "
8422 "when changing X +- C1 cmp C2 to "
8424 WARN_STRICT_OVERFLOW_COMPARISON
);
8425 return fold_build2_loc (loc
, code
, type
, variable
, new_const
);
8429 /* For comparisons of pointers we can decompose it to a compile time
8430 comparison of the base objects and the offsets into the object.
8431 This requires at least one operand being an ADDR_EXPR or a
8432 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8433 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8434 && (TREE_CODE (arg0
) == ADDR_EXPR
8435 || TREE_CODE (arg1
) == ADDR_EXPR
8436 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8437 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8439 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8440 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8442 int volatilep
, reversep
, unsignedp
;
8443 bool indirect_base0
= false, indirect_base1
= false;
8445 /* Get base and offset for the access. Strip ADDR_EXPR for
8446 get_inner_reference, but put it back by stripping INDIRECT_REF
8447 off the base object if possible. indirect_baseN will be true
8448 if baseN is not an address but refers to the object itself. */
8450 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8453 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8454 &bitsize
, &bitpos0
, &offset0
, &mode
,
8455 &unsignedp
, &reversep
, &volatilep
, false);
8456 if (TREE_CODE (base0
) == INDIRECT_REF
)
8457 base0
= TREE_OPERAND (base0
, 0);
8459 indirect_base0
= true;
8461 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8463 base0
= TREE_OPERAND (arg0
, 0);
8464 STRIP_SIGN_NOPS (base0
);
8465 if (TREE_CODE (base0
) == ADDR_EXPR
)
8468 = get_inner_reference (TREE_OPERAND (base0
, 0),
8469 &bitsize
, &bitpos0
, &offset0
, &mode
,
8470 &unsignedp
, &reversep
, &volatilep
,
8472 if (TREE_CODE (base0
) == INDIRECT_REF
)
8473 base0
= TREE_OPERAND (base0
, 0);
8475 indirect_base0
= true;
8477 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8478 offset0
= TREE_OPERAND (arg0
, 1);
8480 offset0
= size_binop (PLUS_EXPR
, offset0
,
8481 TREE_OPERAND (arg0
, 1));
8482 if (TREE_CODE (offset0
) == INTEGER_CST
)
8484 offset_int tem
= wi::sext (wi::to_offset (offset0
),
8485 TYPE_PRECISION (sizetype
));
8486 tem
= wi::lshift (tem
, LOG2_BITS_PER_UNIT
);
8488 if (wi::fits_shwi_p (tem
))
8490 bitpos0
= tem
.to_shwi ();
8491 offset0
= NULL_TREE
;
8497 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8500 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8501 &bitsize
, &bitpos1
, &offset1
, &mode
,
8502 &unsignedp
, &reversep
, &volatilep
, false);
8503 if (TREE_CODE (base1
) == INDIRECT_REF
)
8504 base1
= TREE_OPERAND (base1
, 0);
8506 indirect_base1
= true;
8508 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8510 base1
= TREE_OPERAND (arg1
, 0);
8511 STRIP_SIGN_NOPS (base1
);
8512 if (TREE_CODE (base1
) == ADDR_EXPR
)
8515 = get_inner_reference (TREE_OPERAND (base1
, 0),
8516 &bitsize
, &bitpos1
, &offset1
, &mode
,
8517 &unsignedp
, &reversep
, &volatilep
,
8519 if (TREE_CODE (base1
) == INDIRECT_REF
)
8520 base1
= TREE_OPERAND (base1
, 0);
8522 indirect_base1
= true;
8524 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8525 offset1
= TREE_OPERAND (arg1
, 1);
8527 offset1
= size_binop (PLUS_EXPR
, offset1
,
8528 TREE_OPERAND (arg1
, 1));
8529 if (TREE_CODE (offset1
) == INTEGER_CST
)
8531 offset_int tem
= wi::sext (wi::to_offset (offset1
),
8532 TYPE_PRECISION (sizetype
));
8533 tem
= wi::lshift (tem
, LOG2_BITS_PER_UNIT
);
8535 if (wi::fits_shwi_p (tem
))
8537 bitpos1
= tem
.to_shwi ();
8538 offset1
= NULL_TREE
;
8543 /* If we have equivalent bases we might be able to simplify. */
8544 if (indirect_base0
== indirect_base1
8545 && operand_equal_p (base0
, base1
,
8546 indirect_base0
? OEP_ADDRESS_OF
: 0))
8548 /* We can fold this expression to a constant if the non-constant
8549 offset parts are equal. */
8550 if ((offset0
== offset1
8551 || (offset0
&& offset1
8552 && operand_equal_p (offset0
, offset1
, 0)))
8555 || (indirect_base0
&& DECL_P (base0
))
8556 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8560 && bitpos0
!= bitpos1
8561 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8562 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8563 fold_overflow_warning (("assuming pointer wraparound does not "
8564 "occur when comparing P +- C1 with "
8566 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8571 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8573 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8575 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8577 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8579 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8581 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8585 /* We can simplify the comparison to a comparison of the variable
8586 offset parts if the constant offset parts are equal.
8587 Be careful to use signed sizetype here because otherwise we
8588 mess with array offsets in the wrong way. This is possible
8589 because pointer arithmetic is restricted to retain within an
8590 object and overflow on pointer differences is undefined as of
8591 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8592 else if (bitpos0
== bitpos1
8594 || (indirect_base0
&& DECL_P (base0
))
8595 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8597 /* By converting to signed sizetype we cover middle-end pointer
8598 arithmetic which operates on unsigned pointer types of size
8599 type size and ARRAY_REF offsets which are properly sign or
8600 zero extended from their type in case it is narrower than
8602 if (offset0
== NULL_TREE
)
8603 offset0
= build_int_cst (ssizetype
, 0);
8605 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8606 if (offset1
== NULL_TREE
)
8607 offset1
= build_int_cst (ssizetype
, 0);
8609 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8612 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8613 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8614 fold_overflow_warning (("assuming pointer wraparound does not "
8615 "occur when comparing P +- C1 with "
8617 WARN_STRICT_OVERFLOW_COMPARISON
);
8619 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8622 /* For equal offsets we can simplify to a comparison of the
8624 else if (bitpos0
== bitpos1
8626 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8628 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8629 && ((offset0
== offset1
)
8630 || (offset0
&& offset1
8631 && operand_equal_p (offset0
, offset1
, 0))))
8634 base0
= build_fold_addr_expr_loc (loc
, base0
);
8636 base1
= build_fold_addr_expr_loc (loc
, base1
);
8637 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8641 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8642 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8643 the resulting offset is smaller in absolute value than the
8644 original one and has the same sign. */
8645 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8646 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8647 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8648 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8649 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8650 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8651 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8652 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8654 tree const1
= TREE_OPERAND (arg0
, 1);
8655 tree const2
= TREE_OPERAND (arg1
, 1);
8656 tree variable1
= TREE_OPERAND (arg0
, 0);
8657 tree variable2
= TREE_OPERAND (arg1
, 0);
8659 const char * const warnmsg
= G_("assuming signed overflow does not "
8660 "occur when combining constants around "
8663 /* Put the constant on the side where it doesn't overflow and is
8664 of lower absolute value and of same sign than before. */
8665 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8666 ? MINUS_EXPR
: PLUS_EXPR
,
8668 if (!TREE_OVERFLOW (cst
)
8669 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8670 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8672 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8673 return fold_build2_loc (loc
, code
, type
,
8675 fold_build2_loc (loc
, TREE_CODE (arg1
),
8680 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8681 ? MINUS_EXPR
: PLUS_EXPR
,
8683 if (!TREE_OVERFLOW (cst
)
8684 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8685 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8687 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8688 return fold_build2_loc (loc
, code
, type
,
8689 fold_build2_loc (loc
, TREE_CODE (arg0
),
8696 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8700 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8701 constant, we can simplify it. */
8702 if (TREE_CODE (arg1
) == INTEGER_CST
8703 && (TREE_CODE (arg0
) == MIN_EXPR
8704 || TREE_CODE (arg0
) == MAX_EXPR
)
8705 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
8707 tem
= optimize_minmax_comparison (loc
, code
, type
, op0
, op1
);
8712 /* If we are comparing an expression that just has comparisons
8713 of two integer values, arithmetic expressions of those comparisons,
8714 and constants, we can simplify it. There are only three cases
8715 to check: the two values can either be equal, the first can be
8716 greater, or the second can be greater. Fold the expression for
8717 those three values. Since each value must be 0 or 1, we have
8718 eight possibilities, each of which corresponds to the constant 0
8719 or 1 or one of the six possible comparisons.
8721 This handles common cases like (a > b) == 0 but also handles
8722 expressions like ((x > y) - (y > x)) > 0, which supposedly
8723 occur in macroized code. */
8725 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8727 tree cval1
= 0, cval2
= 0;
8730 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8731 /* Don't handle degenerate cases here; they should already
8732 have been handled anyway. */
8733 && cval1
!= 0 && cval2
!= 0
8734 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8735 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8736 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8737 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8738 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8739 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8740 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8742 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8743 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8745 /* We can't just pass T to eval_subst in case cval1 or cval2
8746 was the same as ARG1. */
8749 = fold_build2_loc (loc
, code
, type
,
8750 eval_subst (loc
, arg0
, cval1
, maxval
,
8754 = fold_build2_loc (loc
, code
, type
,
8755 eval_subst (loc
, arg0
, cval1
, maxval
,
8759 = fold_build2_loc (loc
, code
, type
,
8760 eval_subst (loc
, arg0
, cval1
, minval
,
8764 /* All three of these results should be 0 or 1. Confirm they are.
8765 Then use those values to select the proper code to use. */
8767 if (TREE_CODE (high_result
) == INTEGER_CST
8768 && TREE_CODE (equal_result
) == INTEGER_CST
8769 && TREE_CODE (low_result
) == INTEGER_CST
)
8771 /* Make a 3-bit mask with the high-order bit being the
8772 value for `>', the next for '=', and the low for '<'. */
8773 switch ((integer_onep (high_result
) * 4)
8774 + (integer_onep (equal_result
) * 2)
8775 + integer_onep (low_result
))
8779 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8800 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8805 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8806 SET_EXPR_LOCATION (tem
, loc
);
8809 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8814 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8815 into a single range test. */
8816 if ((TREE_CODE (arg0
) == TRUNC_DIV_EXPR
8817 || TREE_CODE (arg0
) == EXACT_DIV_EXPR
)
8818 && TREE_CODE (arg1
) == INTEGER_CST
8819 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8820 && !integer_zerop (TREE_OPERAND (arg0
, 1))
8821 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8822 && !TREE_OVERFLOW (arg1
))
8824 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
8825 if (tem
!= NULL_TREE
)
8833 /* Subroutine of fold_binary. Optimize complex multiplications of the
8834 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8835 argument EXPR represents the expression "z" of type TYPE. */
8838 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8840 tree itype
= TREE_TYPE (type
);
8841 tree rpart
, ipart
, tem
;
8843 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8845 rpart
= TREE_OPERAND (expr
, 0);
8846 ipart
= TREE_OPERAND (expr
, 1);
8848 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8850 rpart
= TREE_REALPART (expr
);
8851 ipart
= TREE_IMAGPART (expr
);
8855 expr
= save_expr (expr
);
8856 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8857 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8860 rpart
= save_expr (rpart
);
8861 ipart
= save_expr (ipart
);
8862 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8863 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8864 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8865 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8866 build_zero_cst (itype
));
8870 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8871 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8874 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
8876 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
8878 if (TREE_CODE (arg
) == VECTOR_CST
)
8880 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8881 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8883 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8885 constructor_elt
*elt
;
8887 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8888 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8891 elts
[i
] = elt
->value
;
8895 for (; i
< nelts
; i
++)
8897 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8901 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8902 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8903 NULL_TREE otherwise. */
8906 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
8908 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8910 bool need_ctor
= false;
8912 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8913 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8914 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8915 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8918 elts
= XALLOCAVEC (tree
, nelts
* 3);
8919 if (!vec_cst_ctor_to_array (arg0
, elts
)
8920 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
8923 for (i
= 0; i
< nelts
; i
++)
8925 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
8927 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
8932 vec
<constructor_elt
, va_gc
> *v
;
8933 vec_alloc (v
, nelts
);
8934 for (i
= 0; i
< nelts
; i
++)
8935 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
8936 return build_constructor (type
, v
);
8939 return build_vector (type
, &elts
[2 * nelts
]);
8942 /* Try to fold a pointer difference of type TYPE two address expressions of
8943 array references AREF0 and AREF1 using location LOC. Return a
8944 simplified expression for the difference or NULL_TREE. */
8947 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8948 tree aref0
, tree aref1
)
8950 tree base0
= TREE_OPERAND (aref0
, 0);
8951 tree base1
= TREE_OPERAND (aref1
, 0);
8952 tree base_offset
= build_int_cst (type
, 0);
8954 /* If the bases are array references as well, recurse. If the bases
8955 are pointer indirections compute the difference of the pointers.
8956 If the bases are equal, we are set. */
8957 if ((TREE_CODE (base0
) == ARRAY_REF
8958 && TREE_CODE (base1
) == ARRAY_REF
8960 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8961 || (INDIRECT_REF_P (base0
)
8962 && INDIRECT_REF_P (base1
)
8964 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8965 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8967 TREE_OPERAND (base1
, 0)))))
8968 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8970 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8971 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8972 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8973 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
8974 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8976 fold_build2_loc (loc
, MULT_EXPR
, type
,
8982 /* If the real or vector real constant CST of type TYPE has an exact
8983 inverse, return it, else return NULL. */
8986 exact_inverse (tree type
, tree cst
)
8989 tree unit_type
, *elts
;
8991 unsigned vec_nelts
, i
;
8993 switch (TREE_CODE (cst
))
8996 r
= TREE_REAL_CST (cst
);
8998 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8999 return build_real (type
, r
);
9004 vec_nelts
= VECTOR_CST_NELTS (cst
);
9005 elts
= XALLOCAVEC (tree
, vec_nelts
);
9006 unit_type
= TREE_TYPE (type
);
9007 mode
= TYPE_MODE (unit_type
);
9009 for (i
= 0; i
< vec_nelts
; i
++)
9011 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
9012 if (!exact_real_inverse (mode
, &r
))
9014 elts
[i
] = build_real (unit_type
, r
);
9017 return build_vector (type
, elts
);
9024 /* Mask out the tz least significant bits of X of type TYPE where
9025 tz is the number of trailing zeroes in Y. */
9027 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
9029 int tz
= wi::ctz (y
);
9031 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
9035 /* Return true when T is an address and is known to be nonzero.
9036 For floating point we further ensure that T is not denormal.
9037 Similar logic is present in nonzero_address in rtlanal.h.
9039 If the return value is based on the assumption that signed overflow
9040 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9041 change *STRICT_OVERFLOW_P. */
9044 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
9046 tree type
= TREE_TYPE (t
);
9047 enum tree_code code
;
9049 /* Doing something useful for floating point would need more work. */
9050 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
9053 code
= TREE_CODE (t
);
9054 switch (TREE_CODE_CLASS (code
))
9057 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9060 case tcc_comparison
:
9061 return tree_binary_nonzero_warnv_p (code
, type
,
9062 TREE_OPERAND (t
, 0),
9063 TREE_OPERAND (t
, 1),
9066 case tcc_declaration
:
9068 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9076 case TRUTH_NOT_EXPR
:
9077 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9080 case TRUTH_AND_EXPR
:
9082 case TRUTH_XOR_EXPR
:
9083 return tree_binary_nonzero_warnv_p (code
, type
,
9084 TREE_OPERAND (t
, 0),
9085 TREE_OPERAND (t
, 1),
9093 case WITH_SIZE_EXPR
:
9095 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9100 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9104 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9109 tree fndecl
= get_callee_fndecl (t
);
9110 if (!fndecl
) return false;
9111 if (flag_delete_null_pointer_checks
&& !flag_check_new
9112 && DECL_IS_OPERATOR_NEW (fndecl
)
9113 && !TREE_NOTHROW (fndecl
))
9115 if (flag_delete_null_pointer_checks
9116 && lookup_attribute ("returns_nonnull",
9117 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9119 return alloca_call_p (t
);
9128 /* Return true when T is an address and is known to be nonzero.
9129 Handle warnings about undefined signed overflow. */
9132 tree_expr_nonzero_p (tree t
)
9134 bool ret
, strict_overflow_p
;
9136 strict_overflow_p
= false;
9137 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9138 if (strict_overflow_p
)
9139 fold_overflow_warning (("assuming signed overflow does not occur when "
9140 "determining that expression is always "
9142 WARN_STRICT_OVERFLOW_MISC
);
9146 /* Return true if T is known not to be equal to an integer W. */
9149 expr_not_equal_to (tree t
, const wide_int
&w
)
9151 wide_int min
, max
, nz
;
9152 value_range_type rtype
;
9153 switch (TREE_CODE (t
))
9156 return wi::ne_p (t
, w
);
9159 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9161 rtype
= get_range_info (t
, &min
, &max
);
9162 if (rtype
== VR_RANGE
)
9164 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9166 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9169 else if (rtype
== VR_ANTI_RANGE
9170 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9171 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9173 /* If T has some known zero bits and W has any of those bits set,
9174 then T is known not to be equal to W. */
9175 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9176 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9185 /* Fold a binary expression of code CODE and type TYPE with operands
9186 OP0 and OP1. LOC is the location of the resulting expression.
9187 Return the folded expression if folding is successful. Otherwise,
9188 return NULL_TREE. */
9191 fold_binary_loc (location_t loc
,
9192 enum tree_code code
, tree type
, tree op0
, tree op1
)
9194 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9195 tree arg0
, arg1
, tem
;
9196 tree t1
= NULL_TREE
;
9197 bool strict_overflow_p
;
9200 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9201 && TREE_CODE_LENGTH (code
) == 2
9203 && op1
!= NULL_TREE
);
9208 /* Strip any conversions that don't change the mode. This is
9209 safe for every expression, except for a comparison expression
9210 because its signedness is derived from its operands. So, in
9211 the latter case, only strip conversions that don't change the
9212 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9215 Note that this is done as an internal manipulation within the
9216 constant folder, in order to find the simplest representation
9217 of the arguments so that their form can be studied. In any
9218 cases, the appropriate type conversions should be put back in
9219 the tree that will get out of the constant folder. */
9221 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9223 STRIP_SIGN_NOPS (arg0
);
9224 STRIP_SIGN_NOPS (arg1
);
9232 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9233 constant but we can't do arithmetic on them. */
9234 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9236 tem
= const_binop (code
, type
, arg0
, arg1
);
9237 if (tem
!= NULL_TREE
)
9239 if (TREE_TYPE (tem
) != type
)
9240 tem
= fold_convert_loc (loc
, type
, tem
);
9245 /* If this is a commutative operation, and ARG0 is a constant, move it
9246 to ARG1 to reduce the number of tests below. */
9247 if (commutative_tree_code (code
)
9248 && tree_swap_operands_p (arg0
, arg1
, true))
9249 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9251 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9252 to ARG1 to reduce the number of tests below. */
9253 if (kind
== tcc_comparison
9254 && tree_swap_operands_p (arg0
, arg1
, true))
9255 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9257 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9261 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9263 First check for cases where an arithmetic operation is applied to a
9264 compound, conditional, or comparison operation. Push the arithmetic
9265 operation inside the compound or conditional to see if any folding
9266 can then be done. Convert comparison to conditional for this purpose.
9267 The also optimizes non-constant cases that used to be done in
9270 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9271 one of the operands is a comparison and the other is a comparison, a
9272 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9273 code below would make the expression more complex. Change it to a
9274 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9275 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9277 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9278 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9279 && TREE_CODE (type
) != VECTOR_TYPE
9280 && ((truth_value_p (TREE_CODE (arg0
))
9281 && (truth_value_p (TREE_CODE (arg1
))
9282 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9283 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9284 || (truth_value_p (TREE_CODE (arg1
))
9285 && (truth_value_p (TREE_CODE (arg0
))
9286 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9287 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9289 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9290 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9293 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9294 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9296 if (code
== EQ_EXPR
)
9297 tem
= invert_truthvalue_loc (loc
, tem
);
9299 return fold_convert_loc (loc
, type
, tem
);
9302 if (TREE_CODE_CLASS (code
) == tcc_binary
9303 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9305 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9307 tem
= fold_build2_loc (loc
, code
, type
,
9308 fold_convert_loc (loc
, TREE_TYPE (op0
),
9309 TREE_OPERAND (arg0
, 1)), op1
);
9310 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9313 if (TREE_CODE (arg1
) == COMPOUND_EXPR
9314 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
9316 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9317 fold_convert_loc (loc
, TREE_TYPE (op1
),
9318 TREE_OPERAND (arg1
, 1)));
9319 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9323 if (TREE_CODE (arg0
) == COND_EXPR
9324 || TREE_CODE (arg0
) == VEC_COND_EXPR
9325 || COMPARISON_CLASS_P (arg0
))
9327 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9329 /*cond_first_p=*/1);
9330 if (tem
!= NULL_TREE
)
9334 if (TREE_CODE (arg1
) == COND_EXPR
9335 || TREE_CODE (arg1
) == VEC_COND_EXPR
9336 || COMPARISON_CLASS_P (arg1
))
9338 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9340 /*cond_first_p=*/0);
9341 if (tem
!= NULL_TREE
)
9349 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9350 if (TREE_CODE (arg0
) == ADDR_EXPR
9351 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9353 tree iref
= TREE_OPERAND (arg0
, 0);
9354 return fold_build2 (MEM_REF
, type
,
9355 TREE_OPERAND (iref
, 0),
9356 int_const_binop (PLUS_EXPR
, arg1
,
9357 TREE_OPERAND (iref
, 1)));
9360 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9361 if (TREE_CODE (arg0
) == ADDR_EXPR
9362 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9365 HOST_WIDE_INT coffset
;
9366 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9370 return fold_build2 (MEM_REF
, type
,
9371 build_fold_addr_expr (base
),
9372 int_const_binop (PLUS_EXPR
, arg1
,
9373 size_int (coffset
)));
9378 case POINTER_PLUS_EXPR
:
9379 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9380 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9381 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9382 return fold_convert_loc (loc
, type
,
9383 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9384 fold_convert_loc (loc
, sizetype
,
9386 fold_convert_loc (loc
, sizetype
,
9392 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9394 /* X + (X / CST) * -CST is X % CST. */
9395 if (TREE_CODE (arg1
) == MULT_EXPR
9396 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9397 && operand_equal_p (arg0
,
9398 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9400 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9401 tree cst1
= TREE_OPERAND (arg1
, 1);
9402 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9404 if (sum
&& integer_zerop (sum
))
9405 return fold_convert_loc (loc
, type
,
9406 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9407 TREE_TYPE (arg0
), arg0
,
9412 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9413 one. Make sure the type is not saturating and has the signedness of
9414 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9415 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9416 if ((TREE_CODE (arg0
) == MULT_EXPR
9417 || TREE_CODE (arg1
) == MULT_EXPR
)
9418 && !TYPE_SATURATING (type
)
9419 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9420 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9421 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9423 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9428 if (! FLOAT_TYPE_P (type
))
9430 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9431 (plus (plus (mult) (mult)) (foo)) so that we can
9432 take advantage of the factoring cases below. */
9433 if (ANY_INTEGRAL_TYPE_P (type
)
9434 && TYPE_OVERFLOW_WRAPS (type
)
9435 && (((TREE_CODE (arg0
) == PLUS_EXPR
9436 || TREE_CODE (arg0
) == MINUS_EXPR
)
9437 && TREE_CODE (arg1
) == MULT_EXPR
)
9438 || ((TREE_CODE (arg1
) == PLUS_EXPR
9439 || TREE_CODE (arg1
) == MINUS_EXPR
)
9440 && TREE_CODE (arg0
) == MULT_EXPR
)))
9442 tree parg0
, parg1
, parg
, marg
;
9443 enum tree_code pcode
;
9445 if (TREE_CODE (arg1
) == MULT_EXPR
)
9446 parg
= arg0
, marg
= arg1
;
9448 parg
= arg1
, marg
= arg0
;
9449 pcode
= TREE_CODE (parg
);
9450 parg0
= TREE_OPERAND (parg
, 0);
9451 parg1
= TREE_OPERAND (parg
, 1);
9455 if (TREE_CODE (parg0
) == MULT_EXPR
9456 && TREE_CODE (parg1
) != MULT_EXPR
)
9457 return fold_build2_loc (loc
, pcode
, type
,
9458 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9459 fold_convert_loc (loc
, type
,
9461 fold_convert_loc (loc
, type
,
9463 fold_convert_loc (loc
, type
, parg1
));
9464 if (TREE_CODE (parg0
) != MULT_EXPR
9465 && TREE_CODE (parg1
) == MULT_EXPR
)
9467 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9468 fold_convert_loc (loc
, type
, parg0
),
9469 fold_build2_loc (loc
, pcode
, type
,
9470 fold_convert_loc (loc
, type
, marg
),
9471 fold_convert_loc (loc
, type
,
9477 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9478 to __complex__ ( x, y ). This is not the same for SNaNs or
9479 if signed zeros are involved. */
9480 if (!HONOR_SNANS (element_mode (arg0
))
9481 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9482 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9484 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9485 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9486 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9487 bool arg0rz
= false, arg0iz
= false;
9488 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9489 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9491 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9492 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9493 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9495 tree rp
= arg1r
? arg1r
9496 : build1 (REALPART_EXPR
, rtype
, arg1
);
9497 tree ip
= arg0i
? arg0i
9498 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9499 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9501 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9503 tree rp
= arg0r
? arg0r
9504 : build1 (REALPART_EXPR
, rtype
, arg0
);
9505 tree ip
= arg1i
? arg1i
9506 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9507 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9512 if (flag_unsafe_math_optimizations
9513 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9514 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9515 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9518 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9519 We associate floats only if the user has specified
9520 -fassociative-math. */
9521 if (flag_associative_math
9522 && TREE_CODE (arg1
) == PLUS_EXPR
9523 && TREE_CODE (arg0
) != MULT_EXPR
)
9525 tree tree10
= TREE_OPERAND (arg1
, 0);
9526 tree tree11
= TREE_OPERAND (arg1
, 1);
9527 if (TREE_CODE (tree11
) == MULT_EXPR
9528 && TREE_CODE (tree10
) == MULT_EXPR
)
9531 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9532 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9535 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9536 We associate floats only if the user has specified
9537 -fassociative-math. */
9538 if (flag_associative_math
9539 && TREE_CODE (arg0
) == PLUS_EXPR
9540 && TREE_CODE (arg1
) != MULT_EXPR
)
9542 tree tree00
= TREE_OPERAND (arg0
, 0);
9543 tree tree01
= TREE_OPERAND (arg0
, 1);
9544 if (TREE_CODE (tree01
) == MULT_EXPR
9545 && TREE_CODE (tree00
) == MULT_EXPR
)
9548 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9549 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9555 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9556 is a rotate of A by C1 bits. */
9557 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9558 is a rotate of A by B bits. */
9560 enum tree_code code0
, code1
;
9562 code0
= TREE_CODE (arg0
);
9563 code1
= TREE_CODE (arg1
);
9564 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9565 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9566 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9567 TREE_OPERAND (arg1
, 0), 0)
9568 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9569 TYPE_UNSIGNED (rtype
))
9570 /* Only create rotates in complete modes. Other cases are not
9571 expanded properly. */
9572 && (element_precision (rtype
)
9573 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9575 tree tree01
, tree11
;
9576 enum tree_code code01
, code11
;
9578 tree01
= TREE_OPERAND (arg0
, 1);
9579 tree11
= TREE_OPERAND (arg1
, 1);
9580 STRIP_NOPS (tree01
);
9581 STRIP_NOPS (tree11
);
9582 code01
= TREE_CODE (tree01
);
9583 code11
= TREE_CODE (tree11
);
9584 if (code01
== INTEGER_CST
9585 && code11
== INTEGER_CST
9586 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9587 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9589 tem
= build2_loc (loc
, LROTATE_EXPR
,
9590 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9591 TREE_OPERAND (arg0
, 0),
9592 code0
== LSHIFT_EXPR
9593 ? TREE_OPERAND (arg0
, 1)
9594 : TREE_OPERAND (arg1
, 1));
9595 return fold_convert_loc (loc
, type
, tem
);
9597 else if (code11
== MINUS_EXPR
)
9599 tree tree110
, tree111
;
9600 tree110
= TREE_OPERAND (tree11
, 0);
9601 tree111
= TREE_OPERAND (tree11
, 1);
9602 STRIP_NOPS (tree110
);
9603 STRIP_NOPS (tree111
);
9604 if (TREE_CODE (tree110
) == INTEGER_CST
9605 && 0 == compare_tree_int (tree110
,
9607 (TREE_TYPE (TREE_OPERAND
9609 && operand_equal_p (tree01
, tree111
, 0))
9611 fold_convert_loc (loc
, type
,
9612 build2 ((code0
== LSHIFT_EXPR
9615 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9616 TREE_OPERAND (arg0
, 0),
9617 TREE_OPERAND (arg0
, 1)));
9619 else if (code01
== MINUS_EXPR
)
9621 tree tree010
, tree011
;
9622 tree010
= TREE_OPERAND (tree01
, 0);
9623 tree011
= TREE_OPERAND (tree01
, 1);
9624 STRIP_NOPS (tree010
);
9625 STRIP_NOPS (tree011
);
9626 if (TREE_CODE (tree010
) == INTEGER_CST
9627 && 0 == compare_tree_int (tree010
,
9629 (TREE_TYPE (TREE_OPERAND
9631 && operand_equal_p (tree11
, tree011
, 0))
9632 return fold_convert_loc
9634 build2 ((code0
!= LSHIFT_EXPR
9637 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9638 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9644 /* In most languages, can't associate operations on floats through
9645 parentheses. Rather than remember where the parentheses were, we
9646 don't associate floats at all, unless the user has specified
9648 And, we need to make sure type is not saturating. */
9650 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9651 && !TYPE_SATURATING (type
))
9653 tree var0
, con0
, lit0
, minus_lit0
;
9654 tree var1
, con1
, lit1
, minus_lit1
;
9658 /* Split both trees into variables, constants, and literals. Then
9659 associate each group together, the constants with literals,
9660 then the result with variables. This increases the chances of
9661 literals being recombined later and of generating relocatable
9662 expressions for the sum of a constant and literal. */
9663 var0
= split_tree (loc
, arg0
, type
, code
,
9664 &con0
, &lit0
, &minus_lit0
, 0);
9665 var1
= split_tree (loc
, arg1
, type
, code
,
9666 &con1
, &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9668 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9669 if (code
== MINUS_EXPR
)
9672 /* With undefined overflow prefer doing association in a type
9673 which wraps on overflow, if that is one of the operand types. */
9674 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9675 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9677 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9678 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9679 atype
= TREE_TYPE (arg0
);
9680 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9681 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9682 atype
= TREE_TYPE (arg1
);
9683 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9686 /* With undefined overflow we can only associate constants with one
9687 variable, and constants whose association doesn't overflow. */
9688 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9689 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9695 bool one_neg
= false;
9697 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9699 tmp0
= TREE_OPERAND (tmp0
, 0);
9702 if (CONVERT_EXPR_P (tmp0
)
9703 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9704 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9705 <= TYPE_PRECISION (atype
)))
9706 tmp0
= TREE_OPERAND (tmp0
, 0);
9707 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9709 tmp1
= TREE_OPERAND (tmp1
, 0);
9712 if (CONVERT_EXPR_P (tmp1
)
9713 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9714 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9715 <= TYPE_PRECISION (atype
)))
9716 tmp1
= TREE_OPERAND (tmp1
, 0);
9717 /* The only case we can still associate with two variables
9718 is if they cancel out. */
9720 || !operand_equal_p (tmp0
, tmp1
, 0))
9725 /* Only do something if we found more than two objects. Otherwise,
9726 nothing has changed and we risk infinite recursion. */
9728 && (2 < ((var0
!= 0) + (var1
!= 0)
9729 + (con0
!= 0) + (con1
!= 0)
9730 + (lit0
!= 0) + (lit1
!= 0)
9731 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9733 bool any_overflows
= false;
9734 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
9735 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
9736 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
9737 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
9738 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9739 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9740 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9741 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9744 /* Preserve the MINUS_EXPR if the negative part of the literal is
9745 greater than the positive part. Otherwise, the multiplicative
9746 folding code (i.e extract_muldiv) may be fooled in case
9747 unsigned constants are subtracted, like in the following
9748 example: ((X*2 + 4) - 8U)/2. */
9749 if (minus_lit0
&& lit0
)
9751 if (TREE_CODE (lit0
) == INTEGER_CST
9752 && TREE_CODE (minus_lit0
) == INTEGER_CST
9753 && tree_int_cst_lt (lit0
, minus_lit0
))
9755 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9761 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9767 /* Don't introduce overflows through reassociation. */
9769 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
9770 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
9777 fold_convert_loc (loc
, type
,
9778 associate_trees (loc
, var0
, minus_lit0
,
9779 MINUS_EXPR
, atype
));
9782 con0
= associate_trees (loc
, con0
, minus_lit0
,
9785 fold_convert_loc (loc
, type
,
9786 associate_trees (loc
, var0
, con0
,
9791 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9793 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9801 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9802 if (TREE_CODE (arg0
) == NEGATE_EXPR
9803 && negate_expr_p (op1
)
9804 && reorder_operands_p (arg0
, arg1
))
9805 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9807 fold_convert_loc (loc
, type
,
9808 TREE_OPERAND (arg0
, 0)));
9810 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9811 __complex__ ( x, -y ). This is not the same for SNaNs or if
9812 signed zeros are involved. */
9813 if (!HONOR_SNANS (element_mode (arg0
))
9814 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9815 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9817 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9818 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9819 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9820 bool arg0rz
= false, arg0iz
= false;
9821 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9822 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9824 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9825 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9826 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9828 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9830 : build1 (REALPART_EXPR
, rtype
, arg1
));
9831 tree ip
= arg0i
? arg0i
9832 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9833 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9835 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9837 tree rp
= arg0r
? arg0r
9838 : build1 (REALPART_EXPR
, rtype
, arg0
);
9839 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9841 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9842 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9847 /* A - B -> A + (-B) if B is easily negatable. */
9848 if (negate_expr_p (op1
)
9849 && ! TYPE_OVERFLOW_SANITIZED (type
)
9850 && ((FLOAT_TYPE_P (type
)
9851 /* Avoid this transformation if B is a positive REAL_CST. */
9852 && (TREE_CODE (op1
) != REAL_CST
9853 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9854 || INTEGRAL_TYPE_P (type
)))
9855 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9856 fold_convert_loc (loc
, type
, arg0
),
9859 /* Fold &a[i] - &a[j] to i-j. */
9860 if (TREE_CODE (arg0
) == ADDR_EXPR
9861 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9862 && TREE_CODE (arg1
) == ADDR_EXPR
9863 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9865 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9866 TREE_OPERAND (arg0
, 0),
9867 TREE_OPERAND (arg1
, 0));
9872 if (FLOAT_TYPE_P (type
)
9873 && flag_unsafe_math_optimizations
9874 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9875 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9876 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9879 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9880 one. Make sure the type is not saturating and has the signedness of
9881 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9882 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9883 if ((TREE_CODE (arg0
) == MULT_EXPR
9884 || TREE_CODE (arg1
) == MULT_EXPR
)
9885 && !TYPE_SATURATING (type
)
9886 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9887 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9888 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9890 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9898 if (! FLOAT_TYPE_P (type
))
9900 /* Transform x * -C into -x * C if x is easily negatable. */
9901 if (TREE_CODE (op1
) == INTEGER_CST
9902 && tree_int_cst_sgn (op1
) == -1
9903 && negate_expr_p (op0
)
9904 && (tem
= negate_expr (op1
)) != op1
9905 && ! TREE_OVERFLOW (tem
))
9906 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9907 fold_convert_loc (loc
, type
,
9908 negate_expr (op0
)), tem
);
9910 /* (A + A) * C -> A * 2 * C */
9911 if (TREE_CODE (arg0
) == PLUS_EXPR
9912 && TREE_CODE (arg1
) == INTEGER_CST
9913 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9914 TREE_OPERAND (arg0
, 1), 0))
9915 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9916 omit_one_operand_loc (loc
, type
,
9917 TREE_OPERAND (arg0
, 0),
9918 TREE_OPERAND (arg0
, 1)),
9919 fold_build2_loc (loc
, MULT_EXPR
, type
,
9920 build_int_cst (type
, 2) , arg1
));
9922 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9923 sign-changing only. */
9924 if (TREE_CODE (arg1
) == INTEGER_CST
9925 && TREE_CODE (arg0
) == EXACT_DIV_EXPR
9926 && operand_equal_p (arg1
, TREE_OPERAND (arg0
, 1), 0))
9927 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9929 strict_overflow_p
= false;
9930 if (TREE_CODE (arg1
) == INTEGER_CST
9931 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9932 &strict_overflow_p
)))
9934 if (strict_overflow_p
)
9935 fold_overflow_warning (("assuming signed overflow does not "
9936 "occur when simplifying "
9938 WARN_STRICT_OVERFLOW_MISC
);
9939 return fold_convert_loc (loc
, type
, tem
);
9942 /* Optimize z * conj(z) for integer complex numbers. */
9943 if (TREE_CODE (arg0
) == CONJ_EXPR
9944 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9945 return fold_mult_zconjz (loc
, type
, arg1
);
9946 if (TREE_CODE (arg1
) == CONJ_EXPR
9947 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9948 return fold_mult_zconjz (loc
, type
, arg0
);
9952 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9953 This is not the same for NaNs or if signed zeros are
9955 if (!HONOR_NANS (arg0
)
9956 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9957 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9958 && TREE_CODE (arg1
) == COMPLEX_CST
9959 && real_zerop (TREE_REALPART (arg1
)))
9961 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9962 if (real_onep (TREE_IMAGPART (arg1
)))
9964 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9965 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9967 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9968 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9970 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9971 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9972 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9976 /* Optimize z * conj(z) for floating point complex numbers.
9977 Guarded by flag_unsafe_math_optimizations as non-finite
9978 imaginary components don't produce scalar results. */
9979 if (flag_unsafe_math_optimizations
9980 && TREE_CODE (arg0
) == CONJ_EXPR
9981 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9982 return fold_mult_zconjz (loc
, type
, arg1
);
9983 if (flag_unsafe_math_optimizations
9984 && TREE_CODE (arg1
) == CONJ_EXPR
9985 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9986 return fold_mult_zconjz (loc
, type
, arg0
);
9988 if (flag_unsafe_math_optimizations
)
9991 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9994 && operand_equal_p (arg0
, arg1
, 0))
9996 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
10000 tree arg
= build_real (type
, dconst2
);
10001 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
10009 /* Canonicalize (X & C1) | C2. */
10010 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10011 && TREE_CODE (arg1
) == INTEGER_CST
10012 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10014 int width
= TYPE_PRECISION (type
), w
;
10015 wide_int c1
= TREE_OPERAND (arg0
, 1);
10016 wide_int c2
= arg1
;
10018 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10019 if ((c1
& c2
) == c1
)
10020 return omit_one_operand_loc (loc
, type
, arg1
,
10021 TREE_OPERAND (arg0
, 0));
10023 wide_int msk
= wi::mask (width
, false,
10024 TYPE_PRECISION (TREE_TYPE (arg1
)));
10026 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10027 if (msk
.and_not (c1
| c2
) == 0)
10028 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
10029 TREE_OPERAND (arg0
, 0), arg1
);
10031 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10032 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10033 mode which allows further optimizations. */
10036 wide_int c3
= c1
.and_not (c2
);
10037 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
10039 wide_int mask
= wi::mask (w
, false,
10040 TYPE_PRECISION (type
));
10041 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
10049 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
10050 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10051 TREE_OPERAND (arg0
, 0),
10052 wide_int_to_tree (type
,
10057 /* See if this can be simplified into a rotate first. If that
10058 is unsuccessful continue in the association code. */
10062 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10063 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10064 && INTEGRAL_TYPE_P (type
)
10065 && integer_onep (TREE_OPERAND (arg0
, 1))
10066 && integer_onep (arg1
))
10067 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
10068 build_zero_cst (TREE_TYPE (arg0
)));
10070 /* See if this can be simplified into a rotate first. If that
10071 is unsuccessful continue in the association code. */
10075 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10076 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10077 && INTEGRAL_TYPE_P (type
)
10078 && integer_onep (TREE_OPERAND (arg0
, 1))
10079 && integer_onep (arg1
))
10082 tem
= TREE_OPERAND (arg0
, 0);
10083 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
10084 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
10086 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
10087 build_zero_cst (TREE_TYPE (tem
)));
10089 /* Fold ~X & 1 as (X & 1) == 0. */
10090 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
10091 && INTEGRAL_TYPE_P (type
)
10092 && integer_onep (arg1
))
10095 tem
= TREE_OPERAND (arg0
, 0);
10096 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
10097 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
10099 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
10100 build_zero_cst (TREE_TYPE (tem
)));
10102 /* Fold !X & 1 as X == 0. */
10103 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10104 && integer_onep (arg1
))
10106 tem
= TREE_OPERAND (arg0
, 0);
10107 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
10108 build_zero_cst (TREE_TYPE (tem
)));
10111 /* Fold (X ^ Y) & Y as ~X & Y. */
10112 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10113 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
10115 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10116 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10117 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
10118 fold_convert_loc (loc
, type
, arg1
));
10120 /* Fold (X ^ Y) & X as ~Y & X. */
10121 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10122 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10123 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
10125 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10126 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10127 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
10128 fold_convert_loc (loc
, type
, arg1
));
10130 /* Fold X & (X ^ Y) as X & ~Y. */
10131 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
10132 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10134 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10135 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10136 fold_convert_loc (loc
, type
, arg0
),
10137 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
10139 /* Fold X & (Y ^ X) as ~Y & X. */
10140 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
10141 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
10142 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
10144 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10145 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10146 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
10147 fold_convert_loc (loc
, type
, arg0
));
10150 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10151 multiple of 1 << CST. */
10152 if (TREE_CODE (arg1
) == INTEGER_CST
)
10154 wide_int cst1
= arg1
;
10155 wide_int ncst1
= -cst1
;
10156 if ((cst1
& ncst1
) == ncst1
10157 && multiple_of_p (type
, arg0
,
10158 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
10159 return fold_convert_loc (loc
, type
, arg0
);
10162 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10164 if (TREE_CODE (arg1
) == INTEGER_CST
10165 && TREE_CODE (arg0
) == MULT_EXPR
10166 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10168 wide_int warg1
= arg1
;
10169 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
10172 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10174 else if (masked
!= warg1
)
10176 /* Avoid the transform if arg1 is a mask of some
10177 mode which allows further optimizations. */
10178 int pop
= wi::popcount (warg1
);
10179 if (!(pop
>= BITS_PER_UNIT
10180 && exact_log2 (pop
) != -1
10181 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10182 return fold_build2_loc (loc
, code
, type
, op0
,
10183 wide_int_to_tree (type
, masked
));
10187 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10188 ((A & N) + B) & M -> (A + B) & M
10189 Similarly if (N & M) == 0,
10190 ((A | N) + B) & M -> (A + B) & M
10191 and for - instead of + (or unary - instead of +)
10192 and/or ^ instead of |.
10193 If B is constant and (B & M) == 0, fold into A & M. */
10194 if (TREE_CODE (arg1
) == INTEGER_CST
)
10196 wide_int cst1
= arg1
;
10197 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10198 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10199 && (TREE_CODE (arg0
) == PLUS_EXPR
10200 || TREE_CODE (arg0
) == MINUS_EXPR
10201 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10202 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10203 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10209 /* Now we know that arg0 is (C + D) or (C - D) or
10210 -C and arg1 (M) is == (1LL << cst) - 1.
10211 Store C into PMOP[0] and D into PMOP[1]. */
10212 pmop
[0] = TREE_OPERAND (arg0
, 0);
10214 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10216 pmop
[1] = TREE_OPERAND (arg0
, 1);
10220 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10223 for (; which
>= 0; which
--)
10224 switch (TREE_CODE (pmop
[which
]))
10229 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10232 cst0
= TREE_OPERAND (pmop
[which
], 1);
10234 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10239 else if (cst0
!= 0)
10241 /* If C or D is of the form (A & N) where
10242 (N & M) == M, or of the form (A | N) or
10243 (A ^ N) where (N & M) == 0, replace it with A. */
10244 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10247 /* If C or D is a N where (N & M) == 0, it can be
10248 omitted (assumed 0). */
10249 if ((TREE_CODE (arg0
) == PLUS_EXPR
10250 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10251 && (cst1
& pmop
[which
]) == 0)
10252 pmop
[which
] = NULL
;
10258 /* Only build anything new if we optimized one or both arguments
10260 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10261 || (TREE_CODE (arg0
) != NEGATE_EXPR
10262 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10264 tree utype
= TREE_TYPE (arg0
);
10265 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10267 /* Perform the operations in a type that has defined
10268 overflow behavior. */
10269 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10270 if (pmop
[0] != NULL
)
10271 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10272 if (pmop
[1] != NULL
)
10273 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10276 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10277 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10278 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10280 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10281 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10283 else if (pmop
[0] != NULL
)
10285 else if (pmop
[1] != NULL
)
10288 return build_int_cst (type
, 0);
10290 else if (pmop
[0] == NULL
)
10291 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10293 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10295 /* TEM is now the new binary +, - or unary - replacement. */
10296 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10297 fold_convert_loc (loc
, utype
, arg1
));
10298 return fold_convert_loc (loc
, type
, tem
);
10303 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10304 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10305 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10307 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10309 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10312 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10318 /* Don't touch a floating-point divide by zero unless the mode
10319 of the constant can represent infinity. */
10320 if (TREE_CODE (arg1
) == REAL_CST
10321 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10322 && real_zerop (arg1
))
10325 /* (-A) / (-B) -> A / B */
10326 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10327 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10328 TREE_OPERAND (arg0
, 0),
10329 negate_expr (arg1
));
10330 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10331 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10332 negate_expr (arg0
),
10333 TREE_OPERAND (arg1
, 0));
10336 case TRUNC_DIV_EXPR
:
10339 case FLOOR_DIV_EXPR
:
10340 /* Simplify A / (B << N) where A and B are positive and B is
10341 a power of 2, to A >> (N + log2(B)). */
10342 strict_overflow_p
= false;
10343 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10344 && (TYPE_UNSIGNED (type
)
10345 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10347 tree sval
= TREE_OPERAND (arg1
, 0);
10348 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10350 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10351 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10352 wi::exact_log2 (sval
));
10354 if (strict_overflow_p
)
10355 fold_overflow_warning (("assuming signed overflow does not "
10356 "occur when simplifying A / (B << N)"),
10357 WARN_STRICT_OVERFLOW_MISC
);
10359 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10361 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10362 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10368 case ROUND_DIV_EXPR
:
10369 case CEIL_DIV_EXPR
:
10370 case EXACT_DIV_EXPR
:
10371 if (integer_zerop (arg1
))
10374 /* Convert -A / -B to A / B when the type is signed and overflow is
10376 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10377 && TREE_CODE (arg0
) == NEGATE_EXPR
10378 && negate_expr_p (op1
))
10380 if (INTEGRAL_TYPE_P (type
))
10381 fold_overflow_warning (("assuming signed overflow does not occur "
10382 "when distributing negation across "
10384 WARN_STRICT_OVERFLOW_MISC
);
10385 return fold_build2_loc (loc
, code
, type
,
10386 fold_convert_loc (loc
, type
,
10387 TREE_OPERAND (arg0
, 0)),
10388 negate_expr (op1
));
10390 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10391 && TREE_CODE (arg1
) == NEGATE_EXPR
10392 && negate_expr_p (op0
))
10394 if (INTEGRAL_TYPE_P (type
))
10395 fold_overflow_warning (("assuming signed overflow does not occur "
10396 "when distributing negation across "
10398 WARN_STRICT_OVERFLOW_MISC
);
10399 return fold_build2_loc (loc
, code
, type
,
10401 fold_convert_loc (loc
, type
,
10402 TREE_OPERAND (arg1
, 0)));
10405 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10406 operation, EXACT_DIV_EXPR.
10408 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10409 At one time others generated faster code, it's not clear if they do
10410 after the last round to changes to the DIV code in expmed.c. */
10411 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10412 && multiple_of_p (type
, arg0
, arg1
))
10413 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10414 fold_convert (type
, arg0
),
10415 fold_convert (type
, arg1
));
10417 strict_overflow_p
= false;
10418 if (TREE_CODE (arg1
) == INTEGER_CST
10419 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10420 &strict_overflow_p
)))
10422 if (strict_overflow_p
)
10423 fold_overflow_warning (("assuming signed overflow does not occur "
10424 "when simplifying division"),
10425 WARN_STRICT_OVERFLOW_MISC
);
10426 return fold_convert_loc (loc
, type
, tem
);
10431 case CEIL_MOD_EXPR
:
10432 case FLOOR_MOD_EXPR
:
10433 case ROUND_MOD_EXPR
:
10434 case TRUNC_MOD_EXPR
:
10435 strict_overflow_p
= false;
10436 if (TREE_CODE (arg1
) == INTEGER_CST
10437 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10438 &strict_overflow_p
)))
10440 if (strict_overflow_p
)
10441 fold_overflow_warning (("assuming signed overflow does not occur "
10442 "when simplifying modulus"),
10443 WARN_STRICT_OVERFLOW_MISC
);
10444 return fold_convert_loc (loc
, type
, tem
);
10453 /* Since negative shift count is not well-defined,
10454 don't try to compute it in the compiler. */
10455 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10458 prec
= element_precision (type
);
10460 /* If we have a rotate of a bit operation with the rotate count and
10461 the second operand of the bit operation both constant,
10462 permute the two operations. */
10463 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10464 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10465 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10466 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10467 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10468 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10469 fold_build2_loc (loc
, code
, type
,
10470 TREE_OPERAND (arg0
, 0), arg1
),
10471 fold_build2_loc (loc
, code
, type
,
10472 TREE_OPERAND (arg0
, 1), arg1
));
10474 /* Two consecutive rotates adding up to the some integer
10475 multiple of the precision of the type can be ignored. */
10476 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10477 && TREE_CODE (arg0
) == RROTATE_EXPR
10478 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10479 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10481 return TREE_OPERAND (arg0
, 0);
10489 case TRUTH_ANDIF_EXPR
:
10490 /* Note that the operands of this must be ints
10491 and their values must be 0 or 1.
10492 ("true" is a fixed value perhaps depending on the language.) */
10493 /* If first arg is constant zero, return it. */
10494 if (integer_zerop (arg0
))
10495 return fold_convert_loc (loc
, type
, arg0
);
10496 case TRUTH_AND_EXPR
:
10497 /* If either arg is constant true, drop it. */
10498 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10499 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10500 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10501 /* Preserve sequence points. */
10502 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10503 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10504 /* If second arg is constant zero, result is zero, but first arg
10505 must be evaluated. */
10506 if (integer_zerop (arg1
))
10507 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10508 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10509 case will be handled here. */
10510 if (integer_zerop (arg0
))
10511 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10513 /* !X && X is always false. */
10514 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10515 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10516 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10517 /* X && !X is always false. */
10518 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10519 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10520 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10522 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10523 means A >= Y && A != MAX, but in this case we know that
10526 if (!TREE_SIDE_EFFECTS (arg0
)
10527 && !TREE_SIDE_EFFECTS (arg1
))
10529 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10530 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10531 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10533 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10534 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10535 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10538 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10544 case TRUTH_ORIF_EXPR
:
10545 /* Note that the operands of this must be ints
10546 and their values must be 0 or true.
10547 ("true" is a fixed value perhaps depending on the language.) */
10548 /* If first arg is constant true, return it. */
10549 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10550 return fold_convert_loc (loc
, type
, arg0
);
10551 case TRUTH_OR_EXPR
:
10552 /* If either arg is constant zero, drop it. */
10553 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10554 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10555 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10556 /* Preserve sequence points. */
10557 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10558 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10559 /* If second arg is constant true, result is true, but we must
10560 evaluate first arg. */
10561 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10562 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10563 /* Likewise for first arg, but note this only occurs here for
10565 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10566 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10568 /* !X || X is always true. */
10569 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10570 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10571 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10572 /* X || !X is always true. */
10573 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10574 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10575 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10577 /* (X && !Y) || (!X && Y) is X ^ Y */
10578 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10579 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10581 tree a0
, a1
, l0
, l1
, n0
, n1
;
10583 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10584 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10586 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10587 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10589 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10590 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10592 if ((operand_equal_p (n0
, a0
, 0)
10593 && operand_equal_p (n1
, a1
, 0))
10594 || (operand_equal_p (n0
, a1
, 0)
10595 && operand_equal_p (n1
, a0
, 0)))
10596 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10599 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10605 case TRUTH_XOR_EXPR
:
10606 /* If the second arg is constant zero, drop it. */
10607 if (integer_zerop (arg1
))
10608 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10609 /* If the second arg is constant true, this is a logical inversion. */
10610 if (integer_onep (arg1
))
10612 tem
= invert_truthvalue_loc (loc
, arg0
);
10613 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10615 /* Identical arguments cancel to zero. */
10616 if (operand_equal_p (arg0
, arg1
, 0))
10617 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10619 /* !X ^ X is always true. */
10620 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10621 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10622 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10624 /* X ^ !X is always true. */
10625 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10626 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10627 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10636 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10637 if (tem
!= NULL_TREE
)
10640 /* bool_var != 1 becomes !bool_var. */
10641 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10642 && code
== NE_EXPR
)
10643 return fold_convert_loc (loc
, type
,
10644 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10645 TREE_TYPE (arg0
), arg0
));
10647 /* bool_var == 0 becomes !bool_var. */
10648 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10649 && code
== EQ_EXPR
)
10650 return fold_convert_loc (loc
, type
,
10651 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10652 TREE_TYPE (arg0
), arg0
));
10654 /* !exp != 0 becomes !exp */
10655 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10656 && code
== NE_EXPR
)
10657 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10659 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10660 if ((TREE_CODE (arg0
) == PLUS_EXPR
10661 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10662 || TREE_CODE (arg0
) == MINUS_EXPR
)
10663 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10666 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10667 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10669 tree val
= TREE_OPERAND (arg0
, 1);
10670 val
= fold_build2_loc (loc
, code
, type
, val
,
10671 build_int_cst (TREE_TYPE (val
), 0));
10672 return omit_two_operands_loc (loc
, type
, val
,
10673 TREE_OPERAND (arg0
, 0), arg1
);
10676 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10677 if ((TREE_CODE (arg1
) == PLUS_EXPR
10678 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
10679 || TREE_CODE (arg1
) == MINUS_EXPR
)
10680 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10683 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10684 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
10686 tree val
= TREE_OPERAND (arg1
, 1);
10687 val
= fold_build2_loc (loc
, code
, type
, val
,
10688 build_int_cst (TREE_TYPE (val
), 0));
10689 return omit_two_operands_loc (loc
, type
, val
,
10690 TREE_OPERAND (arg1
, 0), arg0
);
10693 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10694 if (TREE_CODE (arg0
) == MINUS_EXPR
10695 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
10696 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10699 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
10700 return omit_two_operands_loc (loc
, type
,
10702 ? boolean_true_node
: boolean_false_node
,
10703 TREE_OPERAND (arg0
, 1), arg1
);
10705 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10706 if (TREE_CODE (arg1
) == MINUS_EXPR
10707 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == INTEGER_CST
10708 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10711 && wi::extract_uhwi (TREE_OPERAND (arg1
, 0), 0, 1) == 1)
10712 return omit_two_operands_loc (loc
, type
,
10714 ? boolean_true_node
: boolean_false_node
,
10715 TREE_OPERAND (arg1
, 1), arg0
);
10717 /* If this is an EQ or NE comparison with zero and ARG0 is
10718 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10719 two operations, but the latter can be done in one less insn
10720 on machines that have only two-operand insns or on which a
10721 constant cannot be the first operand. */
10722 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10723 && integer_zerop (arg1
))
10725 tree arg00
= TREE_OPERAND (arg0
, 0);
10726 tree arg01
= TREE_OPERAND (arg0
, 1);
10727 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10728 && integer_onep (TREE_OPERAND (arg00
, 0)))
10730 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10731 arg01
, TREE_OPERAND (arg00
, 1));
10732 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10733 build_int_cst (TREE_TYPE (arg0
), 1));
10734 return fold_build2_loc (loc
, code
, type
,
10735 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10738 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10739 && integer_onep (TREE_OPERAND (arg01
, 0)))
10741 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10742 arg00
, TREE_OPERAND (arg01
, 1));
10743 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10744 build_int_cst (TREE_TYPE (arg0
), 1));
10745 return fold_build2_loc (loc
, code
, type
,
10746 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10751 /* If this is an NE or EQ comparison of zero against the result of a
10752 signed MOD operation whose second operand is a power of 2, make
10753 the MOD operation unsigned since it is simpler and equivalent. */
10754 if (integer_zerop (arg1
)
10755 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10756 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10757 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10758 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10759 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10760 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10762 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10763 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10764 fold_convert_loc (loc
, newtype
,
10765 TREE_OPERAND (arg0
, 0)),
10766 fold_convert_loc (loc
, newtype
,
10767 TREE_OPERAND (arg0
, 1)));
10769 return fold_build2_loc (loc
, code
, type
, newmod
,
10770 fold_convert_loc (loc
, newtype
, arg1
));
10773 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10774 C1 is a valid shift constant, and C2 is a power of two, i.e.
10776 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10777 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10778 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10780 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10781 && integer_zerop (arg1
))
10783 tree itype
= TREE_TYPE (arg0
);
10784 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10785 prec
= TYPE_PRECISION (itype
);
10787 /* Check for a valid shift count. */
10788 if (wi::ltu_p (arg001
, prec
))
10790 tree arg01
= TREE_OPERAND (arg0
, 1);
10791 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10792 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10793 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10794 can be rewritten as (X & (C2 << C1)) != 0. */
10795 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10797 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10798 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10799 return fold_build2_loc (loc
, code
, type
, tem
,
10800 fold_convert_loc (loc
, itype
, arg1
));
10802 /* Otherwise, for signed (arithmetic) shifts,
10803 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10804 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10805 else if (!TYPE_UNSIGNED (itype
))
10806 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10807 arg000
, build_int_cst (itype
, 0));
10808 /* Otherwise, of unsigned (logical) shifts,
10809 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10810 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10812 return omit_one_operand_loc (loc
, type
,
10813 code
== EQ_EXPR
? integer_one_node
10814 : integer_zero_node
,
10819 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10820 Similarly for NE_EXPR. */
10821 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10822 && TREE_CODE (arg1
) == INTEGER_CST
10823 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10825 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
10826 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
10827 TREE_OPERAND (arg0
, 1));
10829 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10830 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
10832 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
10833 if (integer_nonzerop (dandnotc
))
10834 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
10837 /* If this is a comparison of a field, we may be able to simplify it. */
10838 if ((TREE_CODE (arg0
) == COMPONENT_REF
10839 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10840 /* Handle the constant case even without -O
10841 to make sure the warnings are given. */
10842 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10844 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10849 /* Optimize comparisons of strlen vs zero to a compare of the
10850 first character of the string vs zero. To wit,
10851 strlen(ptr) == 0 => *ptr == 0
10852 strlen(ptr) != 0 => *ptr != 0
10853 Other cases should reduce to one of these two (or a constant)
10854 due to the return value of strlen being unsigned. */
10855 if (TREE_CODE (arg0
) == CALL_EXPR
10856 && integer_zerop (arg1
))
10858 tree fndecl
= get_callee_fndecl (arg0
);
10861 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10862 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10863 && call_expr_nargs (arg0
) == 1
10864 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10866 tree iref
= build_fold_indirect_ref_loc (loc
,
10867 CALL_EXPR_ARG (arg0
, 0));
10868 return fold_build2_loc (loc
, code
, type
, iref
,
10869 build_int_cst (TREE_TYPE (iref
), 0));
10873 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10874 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10875 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10876 && integer_zerop (arg1
)
10877 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10879 tree arg00
= TREE_OPERAND (arg0
, 0);
10880 tree arg01
= TREE_OPERAND (arg0
, 1);
10881 tree itype
= TREE_TYPE (arg00
);
10882 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10884 if (TYPE_UNSIGNED (itype
))
10886 itype
= signed_type_for (itype
);
10887 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10889 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10890 type
, arg00
, build_zero_cst (itype
));
10894 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10895 (X & C) == 0 when C is a single bit. */
10896 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10897 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10898 && integer_zerop (arg1
)
10899 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10901 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10902 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10903 TREE_OPERAND (arg0
, 1));
10904 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10906 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10910 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10911 constant C is a power of two, i.e. a single bit. */
10912 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10913 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10914 && integer_zerop (arg1
)
10915 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10916 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10917 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10919 tree arg00
= TREE_OPERAND (arg0
, 0);
10920 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10921 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10924 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10925 when is C is a power of two, i.e. a single bit. */
10926 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10927 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10928 && integer_zerop (arg1
)
10929 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10930 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10931 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10933 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10934 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10935 arg000
, TREE_OPERAND (arg0
, 1));
10936 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10937 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10940 if (integer_zerop (arg1
)
10941 && tree_expr_nonzero_p (arg0
))
10943 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10944 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10947 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10948 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10949 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10951 tree arg00
= TREE_OPERAND (arg0
, 0);
10952 tree arg01
= TREE_OPERAND (arg0
, 1);
10953 tree arg10
= TREE_OPERAND (arg1
, 0);
10954 tree arg11
= TREE_OPERAND (arg1
, 1);
10955 tree itype
= TREE_TYPE (arg0
);
10957 if (operand_equal_p (arg01
, arg11
, 0))
10958 return fold_build2_loc (loc
, code
, type
,
10959 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10960 fold_build2_loc (loc
,
10961 BIT_XOR_EXPR
, itype
,
10964 build_zero_cst (itype
));
10966 if (operand_equal_p (arg01
, arg10
, 0))
10967 return fold_build2_loc (loc
, code
, type
,
10968 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10969 fold_build2_loc (loc
,
10970 BIT_XOR_EXPR
, itype
,
10973 build_zero_cst (itype
));
10975 if (operand_equal_p (arg00
, arg11
, 0))
10976 return fold_build2_loc (loc
, code
, type
,
10977 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10978 fold_build2_loc (loc
,
10979 BIT_XOR_EXPR
, itype
,
10982 build_zero_cst (itype
));
10984 if (operand_equal_p (arg00
, arg10
, 0))
10985 return fold_build2_loc (loc
, code
, type
,
10986 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10987 fold_build2_loc (loc
,
10988 BIT_XOR_EXPR
, itype
,
10991 build_zero_cst (itype
));
10994 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10995 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10997 tree arg00
= TREE_OPERAND (arg0
, 0);
10998 tree arg01
= TREE_OPERAND (arg0
, 1);
10999 tree arg10
= TREE_OPERAND (arg1
, 0);
11000 tree arg11
= TREE_OPERAND (arg1
, 1);
11001 tree itype
= TREE_TYPE (arg0
);
11003 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
11004 operand_equal_p guarantees no side-effects so we don't need
11005 to use omit_one_operand on Z. */
11006 if (operand_equal_p (arg01
, arg11
, 0))
11007 return fold_build2_loc (loc
, code
, type
, arg00
,
11008 fold_convert_loc (loc
, TREE_TYPE (arg00
),
11010 if (operand_equal_p (arg01
, arg10
, 0))
11011 return fold_build2_loc (loc
, code
, type
, arg00
,
11012 fold_convert_loc (loc
, TREE_TYPE (arg00
),
11014 if (operand_equal_p (arg00
, arg11
, 0))
11015 return fold_build2_loc (loc
, code
, type
, arg01
,
11016 fold_convert_loc (loc
, TREE_TYPE (arg01
),
11018 if (operand_equal_p (arg00
, arg10
, 0))
11019 return fold_build2_loc (loc
, code
, type
, arg01
,
11020 fold_convert_loc (loc
, TREE_TYPE (arg01
),
11023 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11024 if (TREE_CODE (arg01
) == INTEGER_CST
11025 && TREE_CODE (arg11
) == INTEGER_CST
)
11027 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
11028 fold_convert_loc (loc
, itype
, arg11
));
11029 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
11030 return fold_build2_loc (loc
, code
, type
, tem
,
11031 fold_convert_loc (loc
, itype
, arg10
));
11035 /* Attempt to simplify equality/inequality comparisons of complex
11036 values. Only lower the comparison if the result is known or
11037 can be simplified to a single scalar comparison. */
11038 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
11039 || TREE_CODE (arg0
) == COMPLEX_CST
)
11040 && (TREE_CODE (arg1
) == COMPLEX_EXPR
11041 || TREE_CODE (arg1
) == COMPLEX_CST
))
11043 tree real0
, imag0
, real1
, imag1
;
11046 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
11048 real0
= TREE_OPERAND (arg0
, 0);
11049 imag0
= TREE_OPERAND (arg0
, 1);
11053 real0
= TREE_REALPART (arg0
);
11054 imag0
= TREE_IMAGPART (arg0
);
11057 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
11059 real1
= TREE_OPERAND (arg1
, 0);
11060 imag1
= TREE_OPERAND (arg1
, 1);
11064 real1
= TREE_REALPART (arg1
);
11065 imag1
= TREE_IMAGPART (arg1
);
11068 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
11069 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
11071 if (integer_zerop (rcond
))
11073 if (code
== EQ_EXPR
)
11074 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
11076 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
11080 if (code
== NE_EXPR
)
11081 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
11083 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
11087 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
11088 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
11090 if (integer_zerop (icond
))
11092 if (code
== EQ_EXPR
)
11093 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
11095 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
11099 if (code
== NE_EXPR
)
11100 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
11102 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
11113 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
11114 if (tem
!= NULL_TREE
)
11117 /* Transform comparisons of the form X +- C CMP X. */
11118 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
11119 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11120 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
11121 && !HONOR_SNANS (arg0
))
11122 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11123 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
11125 tree arg01
= TREE_OPERAND (arg0
, 1);
11126 enum tree_code code0
= TREE_CODE (arg0
);
11129 if (TREE_CODE (arg01
) == REAL_CST
)
11130 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
11132 is_positive
= tree_int_cst_sgn (arg01
);
11134 /* (X - c) > X becomes false. */
11135 if (code
== GT_EXPR
11136 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11137 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11139 if (TREE_CODE (arg01
) == INTEGER_CST
11140 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11141 fold_overflow_warning (("assuming signed overflow does not "
11142 "occur when assuming that (X - c) > X "
11143 "is always false"),
11144 WARN_STRICT_OVERFLOW_ALL
);
11145 return constant_boolean_node (0, type
);
11148 /* Likewise (X + c) < X becomes false. */
11149 if (code
== LT_EXPR
11150 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11151 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11153 if (TREE_CODE (arg01
) == INTEGER_CST
11154 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11155 fold_overflow_warning (("assuming signed overflow does not "
11156 "occur when assuming that "
11157 "(X + c) < X is always false"),
11158 WARN_STRICT_OVERFLOW_ALL
);
11159 return constant_boolean_node (0, type
);
11162 /* Convert (X - c) <= X to true. */
11163 if (!HONOR_NANS (arg1
)
11165 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11166 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11168 if (TREE_CODE (arg01
) == INTEGER_CST
11169 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11170 fold_overflow_warning (("assuming signed overflow does not "
11171 "occur when assuming that "
11172 "(X - c) <= X is always true"),
11173 WARN_STRICT_OVERFLOW_ALL
);
11174 return constant_boolean_node (1, type
);
11177 /* Convert (X + c) >= X to true. */
11178 if (!HONOR_NANS (arg1
)
11180 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11181 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11183 if (TREE_CODE (arg01
) == INTEGER_CST
11184 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11185 fold_overflow_warning (("assuming signed overflow does not "
11186 "occur when assuming that "
11187 "(X + c) >= X is always true"),
11188 WARN_STRICT_OVERFLOW_ALL
);
11189 return constant_boolean_node (1, type
);
11192 if (TREE_CODE (arg01
) == INTEGER_CST
)
11194 /* Convert X + c > X and X - c < X to true for integers. */
11195 if (code
== GT_EXPR
11196 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11197 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11199 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11200 fold_overflow_warning (("assuming signed overflow does "
11201 "not occur when assuming that "
11202 "(X + c) > X is always true"),
11203 WARN_STRICT_OVERFLOW_ALL
);
11204 return constant_boolean_node (1, type
);
11207 if (code
== LT_EXPR
11208 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11209 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11211 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11212 fold_overflow_warning (("assuming signed overflow does "
11213 "not occur when assuming that "
11214 "(X - c) < X is always true"),
11215 WARN_STRICT_OVERFLOW_ALL
);
11216 return constant_boolean_node (1, type
);
11219 /* Convert X + c <= X and X - c >= X to false for integers. */
11220 if (code
== LE_EXPR
11221 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11222 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11224 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11225 fold_overflow_warning (("assuming signed overflow does "
11226 "not occur when assuming that "
11227 "(X + c) <= X is always false"),
11228 WARN_STRICT_OVERFLOW_ALL
);
11229 return constant_boolean_node (0, type
);
11232 if (code
== GE_EXPR
11233 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11234 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11236 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11237 fold_overflow_warning (("assuming signed overflow does "
11238 "not occur when assuming that "
11239 "(X - c) >= X is always false"),
11240 WARN_STRICT_OVERFLOW_ALL
);
11241 return constant_boolean_node (0, type
);
11246 /* If we are comparing an ABS_EXPR with a constant, we can
11247 convert all the cases into explicit comparisons, but they may
11248 well not be faster than doing the ABS and one comparison.
11249 But ABS (X) <= C is a range comparison, which becomes a subtraction
11250 and a comparison, and is probably faster. */
11251 if (code
== LE_EXPR
11252 && TREE_CODE (arg1
) == INTEGER_CST
11253 && TREE_CODE (arg0
) == ABS_EXPR
11254 && ! TREE_SIDE_EFFECTS (arg0
)
11255 && (0 != (tem
= negate_expr (arg1
)))
11256 && TREE_CODE (tem
) == INTEGER_CST
11257 && !TREE_OVERFLOW (tem
))
11258 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11259 build2 (GE_EXPR
, type
,
11260 TREE_OPERAND (arg0
, 0), tem
),
11261 build2 (LE_EXPR
, type
,
11262 TREE_OPERAND (arg0
, 0), arg1
));
11264 /* Convert ABS_EXPR<x> >= 0 to true. */
11265 strict_overflow_p
= false;
11266 if (code
== GE_EXPR
11267 && (integer_zerop (arg1
)
11268 || (! HONOR_NANS (arg0
)
11269 && real_zerop (arg1
)))
11270 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11272 if (strict_overflow_p
)
11273 fold_overflow_warning (("assuming signed overflow does not occur "
11274 "when simplifying comparison of "
11275 "absolute value and zero"),
11276 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11277 return omit_one_operand_loc (loc
, type
,
11278 constant_boolean_node (true, type
),
11282 /* Convert ABS_EXPR<x> < 0 to false. */
11283 strict_overflow_p
= false;
11284 if (code
== LT_EXPR
11285 && (integer_zerop (arg1
) || real_zerop (arg1
))
11286 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11288 if (strict_overflow_p
)
11289 fold_overflow_warning (("assuming signed overflow does not occur "
11290 "when simplifying comparison of "
11291 "absolute value and zero"),
11292 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11293 return omit_one_operand_loc (loc
, type
,
11294 constant_boolean_node (false, type
),
11298 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11299 and similarly for >= into !=. */
11300 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11301 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11302 && TREE_CODE (arg1
) == LSHIFT_EXPR
11303 && integer_onep (TREE_OPERAND (arg1
, 0)))
11304 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11305 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11306 TREE_OPERAND (arg1
, 1)),
11307 build_zero_cst (TREE_TYPE (arg0
)));
11309 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11310 otherwise Y might be >= # of bits in X's type and thus e.g.
11311 (unsigned char) (1 << Y) for Y 15 might be 0.
11312 If the cast is widening, then 1 << Y should have unsigned type,
11313 otherwise if Y is number of bits in the signed shift type minus 1,
11314 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11315 31 might be 0xffffffff80000000. */
11316 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11317 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11318 && CONVERT_EXPR_P (arg1
)
11319 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11320 && (element_precision (TREE_TYPE (arg1
))
11321 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11322 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11323 || (element_precision (TREE_TYPE (arg1
))
11324 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11325 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11327 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11328 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11329 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11330 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11331 build_zero_cst (TREE_TYPE (arg0
)));
11336 case UNORDERED_EXPR
:
11344 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11346 tree targ0
= strip_float_extensions (arg0
);
11347 tree targ1
= strip_float_extensions (arg1
);
11348 tree newtype
= TREE_TYPE (targ0
);
11350 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11351 newtype
= TREE_TYPE (targ1
);
11353 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11354 return fold_build2_loc (loc
, code
, type
,
11355 fold_convert_loc (loc
, newtype
, targ0
),
11356 fold_convert_loc (loc
, newtype
, targ1
));
11361 case COMPOUND_EXPR
:
11362 /* When pedantic, a compound expression can be neither an lvalue
11363 nor an integer constant expression. */
11364 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11366 /* Don't let (0, 0) be null pointer constant. */
11367 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11368 : fold_convert_loc (loc
, type
, arg1
);
11369 return pedantic_non_lvalue_loc (loc
, tem
);
11372 /* An ASSERT_EXPR should never be passed to fold_binary. */
11373 gcc_unreachable ();
11377 } /* switch (code) */
11380 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11381 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11385 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11387 switch (TREE_CODE (*tp
))
11393 *walk_subtrees
= 0;
11395 /* ... fall through ... */
11402 /* Return whether the sub-tree ST contains a label which is accessible from
11403 outside the sub-tree. */
11406 contains_label_p (tree st
)
11409 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11412 /* Fold a ternary expression of code CODE and type TYPE with operands
11413 OP0, OP1, and OP2. Return the folded expression if folding is
11414 successful. Otherwise, return NULL_TREE. */
11417 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11418 tree op0
, tree op1
, tree op2
)
11421 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11422 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11424 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11425 && TREE_CODE_LENGTH (code
) == 3);
11427 /* If this is a commutative operation, and OP0 is a constant, move it
11428 to OP1 to reduce the number of tests below. */
11429 if (commutative_ternary_tree_code (code
)
11430 && tree_swap_operands_p (op0
, op1
, true))
11431 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11433 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11437 /* Strip any conversions that don't change the mode. This is safe
11438 for every expression, except for a comparison expression because
11439 its signedness is derived from its operands. So, in the latter
11440 case, only strip conversions that don't change the signedness.
11442 Note that this is done as an internal manipulation within the
11443 constant folder, in order to find the simplest representation of
11444 the arguments so that their form can be studied. In any cases,
11445 the appropriate type conversions should be put back in the tree
11446 that will get out of the constant folder. */
11467 case COMPONENT_REF
:
11468 if (TREE_CODE (arg0
) == CONSTRUCTOR
11469 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11471 unsigned HOST_WIDE_INT idx
;
11473 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11480 case VEC_COND_EXPR
:
11481 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11482 so all simple results must be passed through pedantic_non_lvalue. */
11483 if (TREE_CODE (arg0
) == INTEGER_CST
)
11485 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11486 tem
= integer_zerop (arg0
) ? op2
: op1
;
11487 /* Only optimize constant conditions when the selected branch
11488 has the same type as the COND_EXPR. This avoids optimizing
11489 away "c ? x : throw", where the throw has a void type.
11490 Avoid throwing away that operand which contains label. */
11491 if ((!TREE_SIDE_EFFECTS (unused_op
)
11492 || !contains_label_p (unused_op
))
11493 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11494 || VOID_TYPE_P (type
)))
11495 return pedantic_non_lvalue_loc (loc
, tem
);
11498 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11500 if ((TREE_CODE (arg1
) == VECTOR_CST
11501 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11502 && (TREE_CODE (arg2
) == VECTOR_CST
11503 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11505 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
11506 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
11507 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
11508 for (i
= 0; i
< nelts
; i
++)
11510 tree val
= VECTOR_CST_ELT (arg0
, i
);
11511 if (integer_all_onesp (val
))
11513 else if (integer_zerop (val
))
11514 sel
[i
] = nelts
+ i
;
11515 else /* Currently unreachable. */
11518 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11519 if (t
!= NULL_TREE
)
11524 /* If we have A op B ? A : C, we may be able to convert this to a
11525 simpler expression, depending on the operation and the values
11526 of B and C. Signed zeros prevent all of these transformations,
11527 for reasons given above each one.
11529 Also try swapping the arguments and inverting the conditional. */
11530 if (COMPARISON_CLASS_P (arg0
)
11531 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11532 arg1
, TREE_OPERAND (arg0
, 1))
11533 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11535 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11540 if (COMPARISON_CLASS_P (arg0
)
11541 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11543 TREE_OPERAND (arg0
, 1))
11544 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11546 location_t loc0
= expr_location_or (arg0
, loc
);
11547 tem
= fold_invert_truthvalue (loc0
, arg0
);
11548 if (tem
&& COMPARISON_CLASS_P (tem
))
11550 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11556 /* If the second operand is simpler than the third, swap them
11557 since that produces better jump optimization results. */
11558 if (truth_value_p (TREE_CODE (arg0
))
11559 && tree_swap_operands_p (op1
, op2
, false))
11561 location_t loc0
= expr_location_or (arg0
, loc
);
11562 /* See if this can be inverted. If it can't, possibly because
11563 it was a floating-point inequality comparison, don't do
11565 tem
= fold_invert_truthvalue (loc0
, arg0
);
11567 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11570 /* Convert A ? 1 : 0 to simply A. */
11571 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11572 : (integer_onep (op1
)
11573 && !VECTOR_TYPE_P (type
)))
11574 && integer_zerop (op2
)
11575 /* If we try to convert OP0 to our type, the
11576 call to fold will try to move the conversion inside
11577 a COND, which will recurse. In that case, the COND_EXPR
11578 is probably the best choice, so leave it alone. */
11579 && type
== TREE_TYPE (arg0
))
11580 return pedantic_non_lvalue_loc (loc
, arg0
);
11582 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11583 over COND_EXPR in cases such as floating point comparisons. */
11584 if (integer_zerop (op1
)
11585 && (code
== VEC_COND_EXPR
? integer_all_onesp (op2
)
11586 : (integer_onep (op2
)
11587 && !VECTOR_TYPE_P (type
)))
11588 && truth_value_p (TREE_CODE (arg0
)))
11589 return pedantic_non_lvalue_loc (loc
,
11590 fold_convert_loc (loc
, type
,
11591 invert_truthvalue_loc (loc
,
11594 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11595 if (TREE_CODE (arg0
) == LT_EXPR
11596 && integer_zerop (TREE_OPERAND (arg0
, 1))
11597 && integer_zerop (op2
)
11598 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11600 /* sign_bit_p looks through both zero and sign extensions,
11601 but for this optimization only sign extensions are
11603 tree tem2
= TREE_OPERAND (arg0
, 0);
11604 while (tem
!= tem2
)
11606 if (TREE_CODE (tem2
) != NOP_EXPR
11607 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11612 tem2
= TREE_OPERAND (tem2
, 0);
11614 /* sign_bit_p only checks ARG1 bits within A's precision.
11615 If <sign bit of A> has wider type than A, bits outside
11616 of A's precision in <sign bit of A> need to be checked.
11617 If they are all 0, this optimization needs to be done
11618 in unsigned A's type, if they are all 1 in signed A's type,
11619 otherwise this can't be done. */
11621 && TYPE_PRECISION (TREE_TYPE (tem
))
11622 < TYPE_PRECISION (TREE_TYPE (arg1
))
11623 && TYPE_PRECISION (TREE_TYPE (tem
))
11624 < TYPE_PRECISION (type
))
11626 int inner_width
, outer_width
;
11629 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11630 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11631 if (outer_width
> TYPE_PRECISION (type
))
11632 outer_width
= TYPE_PRECISION (type
);
11634 wide_int mask
= wi::shifted_mask
11635 (inner_width
, outer_width
- inner_width
, false,
11636 TYPE_PRECISION (TREE_TYPE (arg1
)));
11638 wide_int common
= mask
& arg1
;
11639 if (common
== mask
)
11641 tem_type
= signed_type_for (TREE_TYPE (tem
));
11642 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11644 else if (common
== 0)
11646 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11647 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11655 fold_convert_loc (loc
, type
,
11656 fold_build2_loc (loc
, BIT_AND_EXPR
,
11657 TREE_TYPE (tem
), tem
,
11658 fold_convert_loc (loc
,
11663 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11664 already handled above. */
11665 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11666 && integer_onep (TREE_OPERAND (arg0
, 1))
11667 && integer_zerop (op2
)
11668 && integer_pow2p (arg1
))
11670 tree tem
= TREE_OPERAND (arg0
, 0);
11672 if (TREE_CODE (tem
) == RSHIFT_EXPR
11673 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11674 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
11675 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11676 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11677 TREE_OPERAND (tem
, 0), arg1
);
11680 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11681 is probably obsolete because the first operand should be a
11682 truth value (that's why we have the two cases above), but let's
11683 leave it in until we can confirm this for all front-ends. */
11684 if (integer_zerop (op2
)
11685 && TREE_CODE (arg0
) == NE_EXPR
11686 && integer_zerop (TREE_OPERAND (arg0
, 1))
11687 && integer_pow2p (arg1
)
11688 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11689 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11690 arg1
, OEP_ONLY_CONST
))
11691 return pedantic_non_lvalue_loc (loc
,
11692 fold_convert_loc (loc
, type
,
11693 TREE_OPERAND (arg0
, 0)));
11695 /* Disable the transformations below for vectors, since
11696 fold_binary_op_with_conditional_arg may undo them immediately,
11697 yielding an infinite loop. */
11698 if (code
== VEC_COND_EXPR
)
11701 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11702 if (integer_zerop (op2
)
11703 && truth_value_p (TREE_CODE (arg0
))
11704 && truth_value_p (TREE_CODE (arg1
))
11705 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11706 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11707 : TRUTH_ANDIF_EXPR
,
11708 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
11710 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11711 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11712 && truth_value_p (TREE_CODE (arg0
))
11713 && truth_value_p (TREE_CODE (arg1
))
11714 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11716 location_t loc0
= expr_location_or (arg0
, loc
);
11717 /* Only perform transformation if ARG0 is easily inverted. */
11718 tem
= fold_invert_truthvalue (loc0
, arg0
);
11720 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11723 type
, fold_convert_loc (loc
, type
, tem
),
11727 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11728 if (integer_zerop (arg1
)
11729 && truth_value_p (TREE_CODE (arg0
))
11730 && truth_value_p (TREE_CODE (op2
))
11731 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11733 location_t loc0
= expr_location_or (arg0
, loc
);
11734 /* Only perform transformation if ARG0 is easily inverted. */
11735 tem
= fold_invert_truthvalue (loc0
, arg0
);
11737 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11738 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11739 type
, fold_convert_loc (loc
, type
, tem
),
11743 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11744 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11745 && truth_value_p (TREE_CODE (arg0
))
11746 && truth_value_p (TREE_CODE (op2
))
11747 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11748 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11749 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11750 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11755 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11756 of fold_ternary on them. */
11757 gcc_unreachable ();
11759 case BIT_FIELD_REF
:
11760 if ((TREE_CODE (arg0
) == VECTOR_CST
11761 || (TREE_CODE (arg0
) == CONSTRUCTOR
11762 && TREE_CODE (TREE_TYPE (arg0
)) == VECTOR_TYPE
))
11763 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11764 || (TREE_CODE (type
) == VECTOR_TYPE
11765 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11767 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11768 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11769 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11770 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11773 && (idx
% width
) == 0
11774 && (n
% width
) == 0
11775 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11780 if (TREE_CODE (arg0
) == VECTOR_CST
)
11783 return VECTOR_CST_ELT (arg0
, idx
);
11785 tree
*vals
= XALLOCAVEC (tree
, n
);
11786 for (unsigned i
= 0; i
< n
; ++i
)
11787 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
11788 return build_vector (type
, vals
);
11791 /* Constructor elements can be subvectors. */
11792 unsigned HOST_WIDE_INT k
= 1;
11793 if (CONSTRUCTOR_NELTS (arg0
) != 0)
11795 tree cons_elem
= TREE_TYPE (CONSTRUCTOR_ELT (arg0
, 0)->value
);
11796 if (TREE_CODE (cons_elem
) == VECTOR_TYPE
)
11797 k
= TYPE_VECTOR_SUBPARTS (cons_elem
);
11800 /* We keep an exact subset of the constructor elements. */
11801 if ((idx
% k
) == 0 && (n
% k
) == 0)
11803 if (CONSTRUCTOR_NELTS (arg0
) == 0)
11804 return build_constructor (type
, NULL
);
11809 if (idx
< CONSTRUCTOR_NELTS (arg0
))
11810 return CONSTRUCTOR_ELT (arg0
, idx
)->value
;
11811 return build_zero_cst (type
);
11814 vec
<constructor_elt
, va_gc
> *vals
;
11815 vec_alloc (vals
, n
);
11816 for (unsigned i
= 0;
11817 i
< n
&& idx
+ i
< CONSTRUCTOR_NELTS (arg0
);
11819 CONSTRUCTOR_APPEND_ELT (vals
, NULL_TREE
,
11821 (arg0
, idx
+ i
)->value
);
11822 return build_constructor (type
, vals
);
11824 /* The bitfield references a single constructor element. */
11825 else if (idx
+ n
<= (idx
/ k
+ 1) * k
)
11827 if (CONSTRUCTOR_NELTS (arg0
) <= idx
/ k
)
11828 return build_zero_cst (type
);
11830 return CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
;
11832 return fold_build3_loc (loc
, code
, type
,
11833 CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
, op1
,
11834 build_int_cst (TREE_TYPE (op2
), (idx
% k
) * width
));
11839 /* A bit-field-ref that referenced the full argument can be stripped. */
11840 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11841 && TYPE_PRECISION (TREE_TYPE (arg0
)) == tree_to_uhwi (arg1
)
11842 && integer_zerop (op2
))
11843 return fold_convert_loc (loc
, type
, arg0
);
11845 /* On constants we can use native encode/interpret to constant
11846 fold (nearly) all BIT_FIELD_REFs. */
11847 if (CONSTANT_CLASS_P (arg0
)
11848 && can_native_interpret_type_p (type
)
11849 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)))
11850 /* This limitation should not be necessary, we just need to
11851 round this up to mode size. */
11852 && tree_to_uhwi (op1
) % BITS_PER_UNIT
== 0
11853 /* Need bit-shifting of the buffer to relax the following. */
11854 && tree_to_uhwi (op2
) % BITS_PER_UNIT
== 0)
11856 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11857 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11858 unsigned HOST_WIDE_INT clen
;
11859 clen
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)));
11860 /* ??? We cannot tell native_encode_expr to start at
11861 some random byte only. So limit us to a reasonable amount
11865 unsigned char *b
= XALLOCAVEC (unsigned char, clen
);
11866 unsigned HOST_WIDE_INT len
= native_encode_expr (arg0
, b
, clen
);
11868 && len
* BITS_PER_UNIT
>= bitpos
+ bitsize
)
11870 tree v
= native_interpret_expr (type
,
11871 b
+ bitpos
/ BITS_PER_UNIT
,
11872 bitsize
/ BITS_PER_UNIT
);
11882 /* For integers we can decompose the FMA if possible. */
11883 if (TREE_CODE (arg0
) == INTEGER_CST
11884 && TREE_CODE (arg1
) == INTEGER_CST
)
11885 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11886 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11887 if (integer_zerop (arg2
))
11888 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11890 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11892 case VEC_PERM_EXPR
:
11893 if (TREE_CODE (arg2
) == VECTOR_CST
)
11895 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
11896 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
11897 unsigned char *sel2
= sel
+ nelts
;
11898 bool need_mask_canon
= false;
11899 bool need_mask_canon2
= false;
11900 bool all_in_vec0
= true;
11901 bool all_in_vec1
= true;
11902 bool maybe_identity
= true;
11903 bool single_arg
= (op0
== op1
);
11904 bool changed
= false;
11906 mask2
= 2 * nelts
- 1;
11907 mask
= single_arg
? (nelts
- 1) : mask2
;
11908 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
11909 for (i
= 0; i
< nelts
; i
++)
11911 tree val
= VECTOR_CST_ELT (arg2
, i
);
11912 if (TREE_CODE (val
) != INTEGER_CST
)
11915 /* Make sure that the perm value is in an acceptable
11918 need_mask_canon
|= wi::gtu_p (t
, mask
);
11919 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11920 sel
[i
] = t
.to_uhwi () & mask
;
11921 sel2
[i
] = t
.to_uhwi () & mask2
;
11923 if (sel
[i
] < nelts
)
11924 all_in_vec1
= false;
11926 all_in_vec0
= false;
11928 if ((sel
[i
] & (nelts
-1)) != i
)
11929 maybe_identity
= false;
11932 if (maybe_identity
)
11942 else if (all_in_vec1
)
11945 for (i
= 0; i
< nelts
; i
++)
11947 need_mask_canon
= true;
11950 if ((TREE_CODE (op0
) == VECTOR_CST
11951 || TREE_CODE (op0
) == CONSTRUCTOR
)
11952 && (TREE_CODE (op1
) == VECTOR_CST
11953 || TREE_CODE (op1
) == CONSTRUCTOR
))
11955 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11956 if (t
!= NULL_TREE
)
11960 if (op0
== op1
&& !single_arg
)
11963 /* Some targets are deficient and fail to expand a single
11964 argument permutation while still allowing an equivalent
11965 2-argument version. */
11966 if (need_mask_canon
&& arg2
== op2
11967 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
11968 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
11970 need_mask_canon
= need_mask_canon2
;
11974 if (need_mask_canon
&& arg2
== op2
)
11976 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
11977 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11978 for (i
= 0; i
< nelts
; i
++)
11979 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
11980 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11985 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11991 } /* switch (code) */
11994 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11995 of an array (or vector). */
11998 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
12000 tree index_type
= NULL_TREE
;
12001 offset_int low_bound
= 0;
12003 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
12005 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
12006 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
12008 /* Static constructors for variably sized objects makes no sense. */
12009 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
12010 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
12011 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
12016 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
12017 TYPE_SIGN (index_type
));
12019 offset_int index
= low_bound
- 1;
12021 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
12022 TYPE_SIGN (index_type
));
12024 offset_int max_index
;
12025 unsigned HOST_WIDE_INT cnt
;
12028 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
12030 /* Array constructor might explicitly set index, or specify a range,
12031 or leave index NULL meaning that it is next index after previous
12035 if (TREE_CODE (cfield
) == INTEGER_CST
)
12036 max_index
= index
= wi::to_offset (cfield
);
12039 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
12040 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
12041 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
12048 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
12049 TYPE_SIGN (index_type
));
12053 /* Do we have match? */
12054 if (wi::cmpu (access_index
, index
) >= 0
12055 && wi::cmpu (access_index
, max_index
) <= 0)
12061 /* Perform constant folding and related simplification of EXPR.
12062 The related simplifications include x*1 => x, x*0 => 0, etc.,
12063 and application of the associative law.
12064 NOP_EXPR conversions may be removed freely (as long as we
12065 are careful not to change the type of the overall expression).
12066 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12067 but we can constant-fold them if they have constant operands. */
12069 #ifdef ENABLE_FOLD_CHECKING
12070 # define fold(x) fold_1 (x)
12071 static tree
fold_1 (tree
);
12077 const tree t
= expr
;
12078 enum tree_code code
= TREE_CODE (t
);
12079 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
12081 location_t loc
= EXPR_LOCATION (expr
);
12083 /* Return right away if a constant. */
12084 if (kind
== tcc_constant
)
12087 /* CALL_EXPR-like objects with variable numbers of operands are
12088 treated specially. */
12089 if (kind
== tcc_vl_exp
)
12091 if (code
== CALL_EXPR
)
12093 tem
= fold_call_expr (loc
, expr
, false);
12094 return tem
? tem
: expr
;
12099 if (IS_EXPR_CODE_CLASS (kind
))
12101 tree type
= TREE_TYPE (t
);
12102 tree op0
, op1
, op2
;
12104 switch (TREE_CODE_LENGTH (code
))
12107 op0
= TREE_OPERAND (t
, 0);
12108 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12109 return tem
? tem
: expr
;
12111 op0
= TREE_OPERAND (t
, 0);
12112 op1
= TREE_OPERAND (t
, 1);
12113 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12114 return tem
? tem
: expr
;
12116 op0
= TREE_OPERAND (t
, 0);
12117 op1
= TREE_OPERAND (t
, 1);
12118 op2
= TREE_OPERAND (t
, 2);
12119 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12120 return tem
? tem
: expr
;
12130 tree op0
= TREE_OPERAND (t
, 0);
12131 tree op1
= TREE_OPERAND (t
, 1);
12133 if (TREE_CODE (op1
) == INTEGER_CST
12134 && TREE_CODE (op0
) == CONSTRUCTOR
12135 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
12137 tree val
= get_array_ctor_element_at_index (op0
,
12138 wi::to_offset (op1
));
12146 /* Return a VECTOR_CST if possible. */
12149 tree type
= TREE_TYPE (t
);
12150 if (TREE_CODE (type
) != VECTOR_TYPE
)
12155 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
12156 if (! CONSTANT_CLASS_P (val
))
12159 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
12163 return fold (DECL_INITIAL (t
));
12167 } /* switch (code) */
12170 #ifdef ENABLE_FOLD_CHECKING
12173 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
12174 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
12175 static void fold_check_failed (const_tree
, const_tree
);
12176 void print_fold_checksum (const_tree
);
12178 /* When --enable-checking=fold, compute a digest of expr before
12179 and after actual fold call to see if fold did not accidentally
12180 change original expr. */
12186 struct md5_ctx ctx
;
12187 unsigned char checksum_before
[16], checksum_after
[16];
12188 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12190 md5_init_ctx (&ctx
);
12191 fold_checksum_tree (expr
, &ctx
, &ht
);
12192 md5_finish_ctx (&ctx
, checksum_before
);
12195 ret
= fold_1 (expr
);
12197 md5_init_ctx (&ctx
);
12198 fold_checksum_tree (expr
, &ctx
, &ht
);
12199 md5_finish_ctx (&ctx
, checksum_after
);
12201 if (memcmp (checksum_before
, checksum_after
, 16))
12202 fold_check_failed (expr
, ret
);
12208 print_fold_checksum (const_tree expr
)
12210 struct md5_ctx ctx
;
12211 unsigned char checksum
[16], cnt
;
12212 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12214 md5_init_ctx (&ctx
);
12215 fold_checksum_tree (expr
, &ctx
, &ht
);
12216 md5_finish_ctx (&ctx
, checksum
);
12217 for (cnt
= 0; cnt
< 16; ++cnt
)
12218 fprintf (stderr
, "%02x", checksum
[cnt
]);
12219 putc ('\n', stderr
);
12223 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12225 internal_error ("fold check: original tree changed by fold");
12229 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12230 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12232 const tree_node
**slot
;
12233 enum tree_code code
;
12234 union tree_node buf
;
12240 slot
= ht
->find_slot (expr
, INSERT
);
12244 code
= TREE_CODE (expr
);
12245 if (TREE_CODE_CLASS (code
) == tcc_declaration
12246 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12248 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12249 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12250 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12251 buf
.decl_with_vis
.symtab_node
= NULL
;
12252 expr
= (tree
) &buf
;
12254 else if (TREE_CODE_CLASS (code
) == tcc_type
12255 && (TYPE_POINTER_TO (expr
)
12256 || TYPE_REFERENCE_TO (expr
)
12257 || TYPE_CACHED_VALUES_P (expr
)
12258 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12259 || TYPE_NEXT_VARIANT (expr
)))
12261 /* Allow these fields to be modified. */
12263 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12264 expr
= tmp
= (tree
) &buf
;
12265 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12266 TYPE_POINTER_TO (tmp
) = NULL
;
12267 TYPE_REFERENCE_TO (tmp
) = NULL
;
12268 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12269 if (TYPE_CACHED_VALUES_P (tmp
))
12271 TYPE_CACHED_VALUES_P (tmp
) = 0;
12272 TYPE_CACHED_VALUES (tmp
) = NULL
;
12275 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12276 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12277 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12278 if (TREE_CODE_CLASS (code
) != tcc_type
12279 && TREE_CODE_CLASS (code
) != tcc_declaration
12280 && code
!= TREE_LIST
12281 && code
!= SSA_NAME
12282 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12283 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12284 switch (TREE_CODE_CLASS (code
))
12290 md5_process_bytes (TREE_STRING_POINTER (expr
),
12291 TREE_STRING_LENGTH (expr
), ctx
);
12294 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12295 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12298 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12299 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12305 case tcc_exceptional
:
12309 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12310 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12311 expr
= TREE_CHAIN (expr
);
12312 goto recursive_label
;
12315 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12316 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12322 case tcc_expression
:
12323 case tcc_reference
:
12324 case tcc_comparison
:
12327 case tcc_statement
:
12329 len
= TREE_OPERAND_LENGTH (expr
);
12330 for (i
= 0; i
< len
; ++i
)
12331 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12333 case tcc_declaration
:
12334 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12335 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12336 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12338 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12339 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12340 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12341 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12342 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12345 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12347 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12349 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12350 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12352 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12356 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12357 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12358 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12359 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12360 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12361 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12362 if (INTEGRAL_TYPE_P (expr
)
12363 || SCALAR_FLOAT_TYPE_P (expr
))
12365 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12366 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12368 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12369 if (TREE_CODE (expr
) == RECORD_TYPE
12370 || TREE_CODE (expr
) == UNION_TYPE
12371 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12372 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12373 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12380 /* Helper function for outputting the checksum of a tree T. When
12381 debugging with gdb, you can "define mynext" to be "next" followed
12382 by "call debug_fold_checksum (op0)", then just trace down till the
12385 DEBUG_FUNCTION
void
12386 debug_fold_checksum (const_tree t
)
12389 unsigned char checksum
[16];
12390 struct md5_ctx ctx
;
12391 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12393 md5_init_ctx (&ctx
);
12394 fold_checksum_tree (t
, &ctx
, &ht
);
12395 md5_finish_ctx (&ctx
, checksum
);
12398 for (i
= 0; i
< 16; i
++)
12399 fprintf (stderr
, "%d ", checksum
[i
]);
12401 fprintf (stderr
, "\n");
12406 /* Fold a unary tree expression with code CODE of type TYPE with an
12407 operand OP0. LOC is the location of the resulting expression.
12408 Return a folded expression if successful. Otherwise, return a tree
12409 expression with code CODE of type TYPE with an operand OP0. */
12412 fold_build1_stat_loc (location_t loc
,
12413 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12416 #ifdef ENABLE_FOLD_CHECKING
12417 unsigned char checksum_before
[16], checksum_after
[16];
12418 struct md5_ctx ctx
;
12419 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12421 md5_init_ctx (&ctx
);
12422 fold_checksum_tree (op0
, &ctx
, &ht
);
12423 md5_finish_ctx (&ctx
, checksum_before
);
12427 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12429 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12431 #ifdef ENABLE_FOLD_CHECKING
12432 md5_init_ctx (&ctx
);
12433 fold_checksum_tree (op0
, &ctx
, &ht
);
12434 md5_finish_ctx (&ctx
, checksum_after
);
12436 if (memcmp (checksum_before
, checksum_after
, 16))
12437 fold_check_failed (op0
, tem
);
12442 /* Fold a binary tree expression with code CODE of type TYPE with
12443 operands OP0 and OP1. LOC is the location of the resulting
12444 expression. Return a folded expression if successful. Otherwise,
12445 return a tree expression with code CODE of type TYPE with operands
12449 fold_build2_stat_loc (location_t loc
,
12450 enum tree_code code
, tree type
, tree op0
, tree op1
12454 #ifdef ENABLE_FOLD_CHECKING
12455 unsigned char checksum_before_op0
[16],
12456 checksum_before_op1
[16],
12457 checksum_after_op0
[16],
12458 checksum_after_op1
[16];
12459 struct md5_ctx ctx
;
12460 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12462 md5_init_ctx (&ctx
);
12463 fold_checksum_tree (op0
, &ctx
, &ht
);
12464 md5_finish_ctx (&ctx
, checksum_before_op0
);
12467 md5_init_ctx (&ctx
);
12468 fold_checksum_tree (op1
, &ctx
, &ht
);
12469 md5_finish_ctx (&ctx
, checksum_before_op1
);
12473 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12475 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12477 #ifdef ENABLE_FOLD_CHECKING
12478 md5_init_ctx (&ctx
);
12479 fold_checksum_tree (op0
, &ctx
, &ht
);
12480 md5_finish_ctx (&ctx
, checksum_after_op0
);
12483 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12484 fold_check_failed (op0
, tem
);
12486 md5_init_ctx (&ctx
);
12487 fold_checksum_tree (op1
, &ctx
, &ht
);
12488 md5_finish_ctx (&ctx
, checksum_after_op1
);
12490 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12491 fold_check_failed (op1
, tem
);
12496 /* Fold a ternary tree expression with code CODE of type TYPE with
12497 operands OP0, OP1, and OP2. Return a folded expression if
12498 successful. Otherwise, return a tree expression with code CODE of
12499 type TYPE with operands OP0, OP1, and OP2. */
12502 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
12503 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12506 #ifdef ENABLE_FOLD_CHECKING
12507 unsigned char checksum_before_op0
[16],
12508 checksum_before_op1
[16],
12509 checksum_before_op2
[16],
12510 checksum_after_op0
[16],
12511 checksum_after_op1
[16],
12512 checksum_after_op2
[16];
12513 struct md5_ctx ctx
;
12514 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12516 md5_init_ctx (&ctx
);
12517 fold_checksum_tree (op0
, &ctx
, &ht
);
12518 md5_finish_ctx (&ctx
, checksum_before_op0
);
12521 md5_init_ctx (&ctx
);
12522 fold_checksum_tree (op1
, &ctx
, &ht
);
12523 md5_finish_ctx (&ctx
, checksum_before_op1
);
12526 md5_init_ctx (&ctx
);
12527 fold_checksum_tree (op2
, &ctx
, &ht
);
12528 md5_finish_ctx (&ctx
, checksum_before_op2
);
12532 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12533 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12535 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12537 #ifdef ENABLE_FOLD_CHECKING
12538 md5_init_ctx (&ctx
);
12539 fold_checksum_tree (op0
, &ctx
, &ht
);
12540 md5_finish_ctx (&ctx
, checksum_after_op0
);
12543 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12544 fold_check_failed (op0
, tem
);
12546 md5_init_ctx (&ctx
);
12547 fold_checksum_tree (op1
, &ctx
, &ht
);
12548 md5_finish_ctx (&ctx
, checksum_after_op1
);
12551 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12552 fold_check_failed (op1
, tem
);
12554 md5_init_ctx (&ctx
);
12555 fold_checksum_tree (op2
, &ctx
, &ht
);
12556 md5_finish_ctx (&ctx
, checksum_after_op2
);
12558 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12559 fold_check_failed (op2
, tem
);
12564 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12565 arguments in ARGARRAY, and a null static chain.
12566 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12567 of type TYPE from the given operands as constructed by build_call_array. */
12570 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12571 int nargs
, tree
*argarray
)
12574 #ifdef ENABLE_FOLD_CHECKING
12575 unsigned char checksum_before_fn
[16],
12576 checksum_before_arglist
[16],
12577 checksum_after_fn
[16],
12578 checksum_after_arglist
[16];
12579 struct md5_ctx ctx
;
12580 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12583 md5_init_ctx (&ctx
);
12584 fold_checksum_tree (fn
, &ctx
, &ht
);
12585 md5_finish_ctx (&ctx
, checksum_before_fn
);
12588 md5_init_ctx (&ctx
);
12589 for (i
= 0; i
< nargs
; i
++)
12590 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12591 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12595 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12597 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12599 #ifdef ENABLE_FOLD_CHECKING
12600 md5_init_ctx (&ctx
);
12601 fold_checksum_tree (fn
, &ctx
, &ht
);
12602 md5_finish_ctx (&ctx
, checksum_after_fn
);
12605 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12606 fold_check_failed (fn
, tem
);
12608 md5_init_ctx (&ctx
);
12609 for (i
= 0; i
< nargs
; i
++)
12610 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12611 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12613 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12614 fold_check_failed (NULL_TREE
, tem
);
12619 /* Perform constant folding and related simplification of initializer
12620 expression EXPR. These behave identically to "fold_buildN" but ignore
12621 potential run-time traps and exceptions that fold must preserve. */
12623 #define START_FOLD_INIT \
12624 int saved_signaling_nans = flag_signaling_nans;\
12625 int saved_trapping_math = flag_trapping_math;\
12626 int saved_rounding_math = flag_rounding_math;\
12627 int saved_trapv = flag_trapv;\
12628 int saved_folding_initializer = folding_initializer;\
12629 flag_signaling_nans = 0;\
12630 flag_trapping_math = 0;\
12631 flag_rounding_math = 0;\
12633 folding_initializer = 1;
12635 #define END_FOLD_INIT \
12636 flag_signaling_nans = saved_signaling_nans;\
12637 flag_trapping_math = saved_trapping_math;\
12638 flag_rounding_math = saved_rounding_math;\
12639 flag_trapv = saved_trapv;\
12640 folding_initializer = saved_folding_initializer;
12643 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12644 tree type
, tree op
)
12649 result
= fold_build1_loc (loc
, code
, type
, op
);
12656 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12657 tree type
, tree op0
, tree op1
)
12662 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12669 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12670 int nargs
, tree
*argarray
)
12675 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12681 #undef START_FOLD_INIT
12682 #undef END_FOLD_INIT
12684 /* Determine if first argument is a multiple of second argument. Return 0 if
12685 it is not, or we cannot easily determined it to be.
12687 An example of the sort of thing we care about (at this point; this routine
12688 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12689 fold cases do now) is discovering that
12691 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12697 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12699 This code also handles discovering that
12701 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12703 is a multiple of 8 so we don't have to worry about dealing with a
12704 possible remainder.
12706 Note that we *look* inside a SAVE_EXPR only to determine how it was
12707 calculated; it is not safe for fold to do much of anything else with the
12708 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12709 at run time. For example, the latter example above *cannot* be implemented
12710 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12711 evaluation time of the original SAVE_EXPR is not necessarily the same at
12712 the time the new expression is evaluated. The only optimization of this
12713 sort that would be valid is changing
12715 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12719 SAVE_EXPR (I) * SAVE_EXPR (J)
12721 (where the same SAVE_EXPR (J) is used in the original and the
12722 transformed version). */
12725 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12727 if (operand_equal_p (top
, bottom
, 0))
12730 if (TREE_CODE (type
) != INTEGER_TYPE
)
12733 switch (TREE_CODE (top
))
12736 /* Bitwise and provides a power of two multiple. If the mask is
12737 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12738 if (!integer_pow2p (bottom
))
12743 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12744 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12748 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12749 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12752 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12756 op1
= TREE_OPERAND (top
, 1);
12757 /* const_binop may not detect overflow correctly,
12758 so check for it explicitly here. */
12759 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12760 && 0 != (t1
= fold_convert (type
,
12761 const_binop (LSHIFT_EXPR
,
12764 && !TREE_OVERFLOW (t1
))
12765 return multiple_of_p (type
, t1
, bottom
);
12770 /* Can't handle conversions from non-integral or wider integral type. */
12771 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12772 || (TYPE_PRECISION (type
)
12773 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12776 /* .. fall through ... */
12779 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12782 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12783 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12786 if (TREE_CODE (bottom
) != INTEGER_CST
12787 || integer_zerop (bottom
)
12788 || (TYPE_UNSIGNED (type
)
12789 && (tree_int_cst_sgn (top
) < 0
12790 || tree_int_cst_sgn (bottom
) < 0)))
12792 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12800 #define tree_expr_nonnegative_warnv_p(X, Y) \
12801 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12803 #define RECURSE(X) \
12804 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12806 /* Return true if CODE or TYPE is known to be non-negative. */
12809 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12811 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12812 && truth_value_p (code
))
12813 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12814 have a signed:1 type (where the value is -1 and 0). */
12819 /* Return true if (CODE OP0) is known to be non-negative. If the return
12820 value is based on the assumption that signed overflow is undefined,
12821 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12822 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12825 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12826 bool *strict_overflow_p
, int depth
)
12828 if (TYPE_UNSIGNED (type
))
12834 /* We can't return 1 if flag_wrapv is set because
12835 ABS_EXPR<INT_MIN> = INT_MIN. */
12836 if (!ANY_INTEGRAL_TYPE_P (type
))
12838 if (TYPE_OVERFLOW_UNDEFINED (type
))
12840 *strict_overflow_p
= true;
12845 case NON_LVALUE_EXPR
:
12847 case FIX_TRUNC_EXPR
:
12848 return RECURSE (op0
);
12852 tree inner_type
= TREE_TYPE (op0
);
12853 tree outer_type
= type
;
12855 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12857 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12858 return RECURSE (op0
);
12859 if (INTEGRAL_TYPE_P (inner_type
))
12861 if (TYPE_UNSIGNED (inner_type
))
12863 return RECURSE (op0
);
12866 else if (INTEGRAL_TYPE_P (outer_type
))
12868 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12869 return RECURSE (op0
);
12870 if (INTEGRAL_TYPE_P (inner_type
))
12871 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12872 && TYPE_UNSIGNED (inner_type
);
12878 return tree_simple_nonnegative_warnv_p (code
, type
);
12881 /* We don't know sign of `t', so be conservative and return false. */
12885 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12886 value is based on the assumption that signed overflow is undefined,
12887 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12888 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12891 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12892 tree op1
, bool *strict_overflow_p
,
12895 if (TYPE_UNSIGNED (type
))
12900 case POINTER_PLUS_EXPR
:
12902 if (FLOAT_TYPE_P (type
))
12903 return RECURSE (op0
) && RECURSE (op1
);
12905 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12906 both unsigned and at least 2 bits shorter than the result. */
12907 if (TREE_CODE (type
) == INTEGER_TYPE
12908 && TREE_CODE (op0
) == NOP_EXPR
12909 && TREE_CODE (op1
) == NOP_EXPR
)
12911 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12912 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12913 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12914 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12916 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12917 TYPE_PRECISION (inner2
)) + 1;
12918 return prec
< TYPE_PRECISION (type
);
12924 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12926 /* x * x is always non-negative for floating point x
12927 or without overflow. */
12928 if (operand_equal_p (op0
, op1
, 0)
12929 || (RECURSE (op0
) && RECURSE (op1
)))
12931 if (ANY_INTEGRAL_TYPE_P (type
)
12932 && TYPE_OVERFLOW_UNDEFINED (type
))
12933 *strict_overflow_p
= true;
12938 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12939 both unsigned and their total bits is shorter than the result. */
12940 if (TREE_CODE (type
) == INTEGER_TYPE
12941 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12942 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12944 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12945 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12947 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12948 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12951 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12952 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12954 if (TREE_CODE (op0
) == INTEGER_CST
)
12955 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12957 if (TREE_CODE (op1
) == INTEGER_CST
)
12958 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12960 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12961 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12963 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12964 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12965 : TYPE_PRECISION (inner0
);
12967 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12968 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12969 : TYPE_PRECISION (inner1
);
12971 return precision0
+ precision1
< TYPE_PRECISION (type
);
12978 return RECURSE (op0
) || RECURSE (op1
);
12984 case TRUNC_DIV_EXPR
:
12985 case CEIL_DIV_EXPR
:
12986 case FLOOR_DIV_EXPR
:
12987 case ROUND_DIV_EXPR
:
12988 return RECURSE (op0
) && RECURSE (op1
);
12990 case TRUNC_MOD_EXPR
:
12991 return RECURSE (op0
);
12993 case FLOOR_MOD_EXPR
:
12994 return RECURSE (op1
);
12996 case CEIL_MOD_EXPR
:
12997 case ROUND_MOD_EXPR
:
12999 return tree_simple_nonnegative_warnv_p (code
, type
);
13002 /* We don't know sign of `t', so be conservative and return false. */
13006 /* Return true if T is known to be non-negative. If the return
13007 value is based on the assumption that signed overflow is undefined,
13008 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13009 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13012 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13014 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13017 switch (TREE_CODE (t
))
13020 return tree_int_cst_sgn (t
) >= 0;
13023 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
13026 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
13029 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13032 /* Limit the depth of recursion to avoid quadratic behavior.
13033 This is expected to catch almost all occurrences in practice.
13034 If this code misses important cases that unbounded recursion
13035 would not, passes that need this information could be revised
13036 to provide it through dataflow propagation. */
13037 return (!name_registered_for_update_p (t
)
13038 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13039 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
13040 strict_overflow_p
, depth
));
13043 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13047 /* Return true if T is known to be non-negative. If the return
13048 value is based on the assumption that signed overflow is undefined,
13049 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13050 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13053 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
13054 bool *strict_overflow_p
, int depth
)
13075 case CFN_BUILT_IN_BSWAP32
:
13076 case CFN_BUILT_IN_BSWAP64
:
13081 /* sqrt(-0.0) is -0.0. */
13082 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
13084 return RECURSE (arg0
);
13110 CASE_CFN_NEARBYINT
:
13117 CASE_CFN_SIGNIFICAND
:
13121 /* True if the 1st argument is nonnegative. */
13122 return RECURSE (arg0
);
13125 /* True if the 1st OR 2nd arguments are nonnegative. */
13126 return RECURSE (arg0
) || RECURSE (arg1
);
13129 /* True if the 1st AND 2nd arguments are nonnegative. */
13130 return RECURSE (arg0
) && RECURSE (arg1
);
13133 /* True if the 2nd argument is nonnegative. */
13134 return RECURSE (arg1
);
13137 /* True if the 1st argument is nonnegative or the second
13138 argument is an even integer. */
13139 if (TREE_CODE (arg1
) == INTEGER_CST
13140 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
13142 return RECURSE (arg0
);
13145 /* True if the 1st argument is nonnegative or the second
13146 argument is an even integer valued real. */
13147 if (TREE_CODE (arg1
) == REAL_CST
)
13152 c
= TREE_REAL_CST (arg1
);
13153 n
= real_to_integer (&c
);
13156 REAL_VALUE_TYPE cint
;
13157 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
13158 if (real_identical (&c
, &cint
))
13162 return RECURSE (arg0
);
13167 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
13170 /* Return true if T is known to be non-negative. If the return
13171 value is based on the assumption that signed overflow is undefined,
13172 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13173 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13176 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13178 enum tree_code code
= TREE_CODE (t
);
13179 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13186 tree temp
= TARGET_EXPR_SLOT (t
);
13187 t
= TARGET_EXPR_INITIAL (t
);
13189 /* If the initializer is non-void, then it's a normal expression
13190 that will be assigned to the slot. */
13191 if (!VOID_TYPE_P (t
))
13192 return RECURSE (t
);
13194 /* Otherwise, the initializer sets the slot in some way. One common
13195 way is an assignment statement at the end of the initializer. */
13198 if (TREE_CODE (t
) == BIND_EXPR
)
13199 t
= expr_last (BIND_EXPR_BODY (t
));
13200 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13201 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13202 t
= expr_last (TREE_OPERAND (t
, 0));
13203 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13208 if (TREE_CODE (t
) == MODIFY_EXPR
13209 && TREE_OPERAND (t
, 0) == temp
)
13210 return RECURSE (TREE_OPERAND (t
, 1));
13217 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13218 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13220 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13221 get_call_combined_fn (t
),
13224 strict_overflow_p
, depth
);
13226 case COMPOUND_EXPR
:
13228 return RECURSE (TREE_OPERAND (t
, 1));
13231 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13234 return RECURSE (TREE_OPERAND (t
, 0));
13237 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13242 #undef tree_expr_nonnegative_warnv_p
13244 /* Return true if T is known to be non-negative. If the return
13245 value is based on the assumption that signed overflow is undefined,
13246 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13247 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13250 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13252 enum tree_code code
;
13253 if (t
== error_mark_node
)
13256 code
= TREE_CODE (t
);
13257 switch (TREE_CODE_CLASS (code
))
13260 case tcc_comparison
:
13261 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13263 TREE_OPERAND (t
, 0),
13264 TREE_OPERAND (t
, 1),
13265 strict_overflow_p
, depth
);
13268 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13270 TREE_OPERAND (t
, 0),
13271 strict_overflow_p
, depth
);
13274 case tcc_declaration
:
13275 case tcc_reference
:
13276 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13284 case TRUTH_AND_EXPR
:
13285 case TRUTH_OR_EXPR
:
13286 case TRUTH_XOR_EXPR
:
13287 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13289 TREE_OPERAND (t
, 0),
13290 TREE_OPERAND (t
, 1),
13291 strict_overflow_p
, depth
);
13292 case TRUTH_NOT_EXPR
:
13293 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13295 TREE_OPERAND (t
, 0),
13296 strict_overflow_p
, depth
);
13303 case WITH_SIZE_EXPR
:
13305 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13308 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13312 /* Return true if `t' is known to be non-negative. Handle warnings
13313 about undefined signed overflow. */
13316 tree_expr_nonnegative_p (tree t
)
13318 bool ret
, strict_overflow_p
;
13320 strict_overflow_p
= false;
13321 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13322 if (strict_overflow_p
)
13323 fold_overflow_warning (("assuming signed overflow does not occur when "
13324 "determining that expression is always "
13326 WARN_STRICT_OVERFLOW_MISC
);
13331 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13332 For floating point we further ensure that T is not denormal.
13333 Similar logic is present in nonzero_address in rtlanal.h.
13335 If the return value is based on the assumption that signed overflow
13336 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13337 change *STRICT_OVERFLOW_P. */
13340 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13341 bool *strict_overflow_p
)
13346 return tree_expr_nonzero_warnv_p (op0
,
13347 strict_overflow_p
);
13351 tree inner_type
= TREE_TYPE (op0
);
13352 tree outer_type
= type
;
13354 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13355 && tree_expr_nonzero_warnv_p (op0
,
13356 strict_overflow_p
));
13360 case NON_LVALUE_EXPR
:
13361 return tree_expr_nonzero_warnv_p (op0
,
13362 strict_overflow_p
);
13371 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13372 For floating point we further ensure that T is not denormal.
13373 Similar logic is present in nonzero_address in rtlanal.h.
13375 If the return value is based on the assumption that signed overflow
13376 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13377 change *STRICT_OVERFLOW_P. */
13380 tree_binary_nonzero_warnv_p (enum tree_code code
,
13383 tree op1
, bool *strict_overflow_p
)
13385 bool sub_strict_overflow_p
;
13388 case POINTER_PLUS_EXPR
:
13390 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13392 /* With the presence of negative values it is hard
13393 to say something. */
13394 sub_strict_overflow_p
= false;
13395 if (!tree_expr_nonnegative_warnv_p (op0
,
13396 &sub_strict_overflow_p
)
13397 || !tree_expr_nonnegative_warnv_p (op1
,
13398 &sub_strict_overflow_p
))
13400 /* One of operands must be positive and the other non-negative. */
13401 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13402 overflows, on a twos-complement machine the sum of two
13403 nonnegative numbers can never be zero. */
13404 return (tree_expr_nonzero_warnv_p (op0
,
13406 || tree_expr_nonzero_warnv_p (op1
,
13407 strict_overflow_p
));
13412 if (TYPE_OVERFLOW_UNDEFINED (type
))
13414 if (tree_expr_nonzero_warnv_p (op0
,
13416 && tree_expr_nonzero_warnv_p (op1
,
13417 strict_overflow_p
))
13419 *strict_overflow_p
= true;
13426 sub_strict_overflow_p
= false;
13427 if (tree_expr_nonzero_warnv_p (op0
,
13428 &sub_strict_overflow_p
)
13429 && tree_expr_nonzero_warnv_p (op1
,
13430 &sub_strict_overflow_p
))
13432 if (sub_strict_overflow_p
)
13433 *strict_overflow_p
= true;
13438 sub_strict_overflow_p
= false;
13439 if (tree_expr_nonzero_warnv_p (op0
,
13440 &sub_strict_overflow_p
))
13442 if (sub_strict_overflow_p
)
13443 *strict_overflow_p
= true;
13445 /* When both operands are nonzero, then MAX must be too. */
13446 if (tree_expr_nonzero_warnv_p (op1
,
13447 strict_overflow_p
))
13450 /* MAX where operand 0 is positive is positive. */
13451 return tree_expr_nonnegative_warnv_p (op0
,
13452 strict_overflow_p
);
13454 /* MAX where operand 1 is positive is positive. */
13455 else if (tree_expr_nonzero_warnv_p (op1
,
13456 &sub_strict_overflow_p
)
13457 && tree_expr_nonnegative_warnv_p (op1
,
13458 &sub_strict_overflow_p
))
13460 if (sub_strict_overflow_p
)
13461 *strict_overflow_p
= true;
13467 return (tree_expr_nonzero_warnv_p (op1
,
13469 || tree_expr_nonzero_warnv_p (op0
,
13470 strict_overflow_p
));
13479 /* Return true when T is an address and is known to be nonzero.
13480 For floating point we further ensure that T is not denormal.
13481 Similar logic is present in nonzero_address in rtlanal.h.
13483 If the return value is based on the assumption that signed overflow
13484 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13485 change *STRICT_OVERFLOW_P. */
13488 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13490 bool sub_strict_overflow_p
;
13491 switch (TREE_CODE (t
))
13494 return !integer_zerop (t
);
13498 tree base
= TREE_OPERAND (t
, 0);
13500 if (!DECL_P (base
))
13501 base
= get_base_address (base
);
13506 /* For objects in symbol table check if we know they are non-zero.
13507 Don't do anything for variables and functions before symtab is built;
13508 it is quite possible that they will be declared weak later. */
13509 if (DECL_P (base
) && decl_in_symtab_p (base
))
13511 struct symtab_node
*symbol
;
13513 symbol
= symtab_node::get_create (base
);
13515 return symbol
->nonzero_address ();
13520 /* Function local objects are never NULL. */
13522 && (DECL_CONTEXT (base
)
13523 && TREE_CODE (DECL_CONTEXT (base
)) == FUNCTION_DECL
13524 && auto_var_in_fn_p (base
, DECL_CONTEXT (base
))))
13527 /* Constants are never weak. */
13528 if (CONSTANT_CLASS_P (base
))
13535 sub_strict_overflow_p
= false;
13536 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13537 &sub_strict_overflow_p
)
13538 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13539 &sub_strict_overflow_p
))
13541 if (sub_strict_overflow_p
)
13542 *strict_overflow_p
= true;
13553 #define integer_valued_real_p(X) \
13554 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13556 #define RECURSE(X) \
13557 ((integer_valued_real_p) (X, depth + 1))
13559 /* Return true if the floating point result of (CODE OP0) has an
13560 integer value. We also allow +Inf, -Inf and NaN to be considered
13561 integer values. Return false for signaling NaN.
13563 DEPTH is the current nesting depth of the query. */
13566 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13574 return RECURSE (op0
);
13578 tree type
= TREE_TYPE (op0
);
13579 if (TREE_CODE (type
) == INTEGER_TYPE
)
13581 if (TREE_CODE (type
) == REAL_TYPE
)
13582 return RECURSE (op0
);
13592 /* Return true if the floating point result of (CODE OP0 OP1) has an
13593 integer value. We also allow +Inf, -Inf and NaN to be considered
13594 integer values. Return false for signaling NaN.
13596 DEPTH is the current nesting depth of the query. */
13599 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13608 return RECURSE (op0
) && RECURSE (op1
);
13616 /* Return true if the floating point result of calling FNDECL with arguments
13617 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13618 considered integer values. Return false for signaling NaN. If FNDECL
13619 takes fewer than 2 arguments, the remaining ARGn are null.
13621 DEPTH is the current nesting depth of the query. */
13624 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13630 CASE_CFN_NEARBYINT
:
13638 return RECURSE (arg0
) && RECURSE (arg1
);
13646 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13647 has an integer value. We also allow +Inf, -Inf and NaN to be
13648 considered integer values. Return false for signaling NaN.
13650 DEPTH is the current nesting depth of the query. */
13653 integer_valued_real_single_p (tree t
, int depth
)
13655 switch (TREE_CODE (t
))
13658 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13661 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13664 /* Limit the depth of recursion to avoid quadratic behavior.
13665 This is expected to catch almost all occurrences in practice.
13666 If this code misses important cases that unbounded recursion
13667 would not, passes that need this information could be revised
13668 to provide it through dataflow propagation. */
13669 return (!name_registered_for_update_p (t
)
13670 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13671 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13680 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13681 has an integer value. We also allow +Inf, -Inf and NaN to be
13682 considered integer values. Return false for signaling NaN.
13684 DEPTH is the current nesting depth of the query. */
13687 integer_valued_real_invalid_p (tree t
, int depth
)
13689 switch (TREE_CODE (t
))
13691 case COMPOUND_EXPR
:
13694 return RECURSE (TREE_OPERAND (t
, 1));
13697 return RECURSE (TREE_OPERAND (t
, 0));
13706 #undef integer_valued_real_p
13708 /* Return true if the floating point expression T has an integer value.
13709 We also allow +Inf, -Inf and NaN to be considered integer values.
13710 Return false for signaling NaN.
13712 DEPTH is the current nesting depth of the query. */
13715 integer_valued_real_p (tree t
, int depth
)
13717 if (t
== error_mark_node
)
13720 tree_code code
= TREE_CODE (t
);
13721 switch (TREE_CODE_CLASS (code
))
13724 case tcc_comparison
:
13725 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13726 TREE_OPERAND (t
, 1), depth
);
13729 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13732 case tcc_declaration
:
13733 case tcc_reference
:
13734 return integer_valued_real_single_p (t
, depth
);
13744 return integer_valued_real_single_p (t
, depth
);
13748 tree arg0
= (call_expr_nargs (t
) > 0
13749 ? CALL_EXPR_ARG (t
, 0)
13751 tree arg1
= (call_expr_nargs (t
) > 1
13752 ? CALL_EXPR_ARG (t
, 1)
13754 return integer_valued_real_call_p (get_call_combined_fn (t
),
13755 arg0
, arg1
, depth
);
13759 return integer_valued_real_invalid_p (t
, depth
);
13763 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13764 attempt to fold the expression to a constant without modifying TYPE,
13767 If the expression could be simplified to a constant, then return
13768 the constant. If the expression would not be simplified to a
13769 constant, then return NULL_TREE. */
13772 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13774 tree tem
= fold_binary (code
, type
, op0
, op1
);
13775 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13778 /* Given the components of a unary expression CODE, TYPE and OP0,
13779 attempt to fold the expression to a constant without modifying
13782 If the expression could be simplified to a constant, then return
13783 the constant. If the expression would not be simplified to a
13784 constant, then return NULL_TREE. */
13787 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13789 tree tem
= fold_unary (code
, type
, op0
);
13790 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13793 /* If EXP represents referencing an element in a constant string
13794 (either via pointer arithmetic or array indexing), return the
13795 tree representing the value accessed, otherwise return NULL. */
13798 fold_read_from_constant_string (tree exp
)
13800 if ((TREE_CODE (exp
) == INDIRECT_REF
13801 || TREE_CODE (exp
) == ARRAY_REF
)
13802 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13804 tree exp1
= TREE_OPERAND (exp
, 0);
13807 location_t loc
= EXPR_LOCATION (exp
);
13809 if (TREE_CODE (exp
) == INDIRECT_REF
)
13810 string
= string_constant (exp1
, &index
);
13813 tree low_bound
= array_ref_low_bound (exp
);
13814 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13816 /* Optimize the special-case of a zero lower bound.
13818 We convert the low_bound to sizetype to avoid some problems
13819 with constant folding. (E.g. suppose the lower bound is 1,
13820 and its mode is QI. Without the conversion,l (ARRAY
13821 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13822 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13823 if (! integer_zerop (low_bound
))
13824 index
= size_diffop_loc (loc
, index
,
13825 fold_convert_loc (loc
, sizetype
, low_bound
));
13831 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13832 && TREE_CODE (string
) == STRING_CST
13833 && TREE_CODE (index
) == INTEGER_CST
13834 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13835 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
13837 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
13838 return build_int_cst_type (TREE_TYPE (exp
),
13839 (TREE_STRING_POINTER (string
)
13840 [TREE_INT_CST_LOW (index
)]));
13845 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13846 an integer constant, real, or fixed-point constant.
13848 TYPE is the type of the result. */
13851 fold_negate_const (tree arg0
, tree type
)
13853 tree t
= NULL_TREE
;
13855 switch (TREE_CODE (arg0
))
13860 wide_int val
= wi::neg (arg0
, &overflow
);
13861 t
= force_fit_type (type
, val
, 1,
13862 (overflow
| TREE_OVERFLOW (arg0
))
13863 && !TYPE_UNSIGNED (type
));
13868 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13873 FIXED_VALUE_TYPE f
;
13874 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13875 &(TREE_FIXED_CST (arg0
)), NULL
,
13876 TYPE_SATURATING (type
));
13877 t
= build_fixed (type
, f
);
13878 /* Propagate overflow flags. */
13879 if (overflow_p
| TREE_OVERFLOW (arg0
))
13880 TREE_OVERFLOW (t
) = 1;
13885 gcc_unreachable ();
13891 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13892 an integer constant or real constant.
13894 TYPE is the type of the result. */
13897 fold_abs_const (tree arg0
, tree type
)
13899 tree t
= NULL_TREE
;
13901 switch (TREE_CODE (arg0
))
13905 /* If the value is unsigned or non-negative, then the absolute value
13906 is the same as the ordinary value. */
13907 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13910 /* If the value is negative, then the absolute value is
13915 wide_int val
= wi::neg (arg0
, &overflow
);
13916 t
= force_fit_type (type
, val
, -1,
13917 overflow
| TREE_OVERFLOW (arg0
));
13923 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13924 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13930 gcc_unreachable ();
13936 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13937 constant. TYPE is the type of the result. */
13940 fold_not_const (const_tree arg0
, tree type
)
13942 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13944 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13947 /* Given CODE, a relational operator, the target type, TYPE and two
13948 constant operands OP0 and OP1, return the result of the
13949 relational operation. If the result is not a compile time
13950 constant, then return NULL_TREE. */
13953 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13955 int result
, invert
;
13957 /* From here on, the only cases we handle are when the result is
13958 known to be a constant. */
13960 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13962 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13963 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13965 /* Handle the cases where either operand is a NaN. */
13966 if (real_isnan (c0
) || real_isnan (c1
))
13976 case UNORDERED_EXPR
:
13990 if (flag_trapping_math
)
13996 gcc_unreachable ();
13999 return constant_boolean_node (result
, type
);
14002 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
14005 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
14007 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
14008 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
14009 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
14012 /* Handle equality/inequality of complex constants. */
14013 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
14015 tree rcond
= fold_relational_const (code
, type
,
14016 TREE_REALPART (op0
),
14017 TREE_REALPART (op1
));
14018 tree icond
= fold_relational_const (code
, type
,
14019 TREE_IMAGPART (op0
),
14020 TREE_IMAGPART (op1
));
14021 if (code
== EQ_EXPR
)
14022 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
14023 else if (code
== NE_EXPR
)
14024 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
14029 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
14031 if (!VECTOR_TYPE_P (type
))
14033 /* Have vector comparison with scalar boolean result. */
14034 bool result
= true;
14035 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
14036 && VECTOR_CST_NELTS (op0
) == VECTOR_CST_NELTS (op1
));
14037 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (op0
); i
++)
14039 tree elem0
= VECTOR_CST_ELT (op0
, i
);
14040 tree elem1
= VECTOR_CST_ELT (op1
, i
);
14041 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
14042 result
&= integer_onep (tmp
);
14044 if (code
== NE_EXPR
)
14046 return constant_boolean_node (result
, type
);
14048 unsigned count
= VECTOR_CST_NELTS (op0
);
14049 tree
*elts
= XALLOCAVEC (tree
, count
);
14050 gcc_assert (VECTOR_CST_NELTS (op1
) == count
14051 && TYPE_VECTOR_SUBPARTS (type
) == count
);
14053 for (unsigned i
= 0; i
< count
; i
++)
14055 tree elem_type
= TREE_TYPE (type
);
14056 tree elem0
= VECTOR_CST_ELT (op0
, i
);
14057 tree elem1
= VECTOR_CST_ELT (op1
, i
);
14059 tree tem
= fold_relational_const (code
, elem_type
,
14062 if (tem
== NULL_TREE
)
14065 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
14068 return build_vector (type
, elts
);
14071 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14073 To compute GT, swap the arguments and do LT.
14074 To compute GE, do LT and invert the result.
14075 To compute LE, swap the arguments, do LT and invert the result.
14076 To compute NE, do EQ and invert the result.
14078 Therefore, the code below must handle only EQ and LT. */
14080 if (code
== LE_EXPR
|| code
== GT_EXPR
)
14082 std::swap (op0
, op1
);
14083 code
= swap_tree_comparison (code
);
14086 /* Note that it is safe to invert for real values here because we
14087 have already handled the one case that it matters. */
14090 if (code
== NE_EXPR
|| code
== GE_EXPR
)
14093 code
= invert_tree_comparison (code
, false);
14096 /* Compute a result for LT or EQ if args permit;
14097 Otherwise return T. */
14098 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
14100 if (code
== EQ_EXPR
)
14101 result
= tree_int_cst_equal (op0
, op1
);
14103 result
= tree_int_cst_lt (op0
, op1
);
14110 return constant_boolean_node (result
, type
);
14113 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14114 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14118 fold_build_cleanup_point_expr (tree type
, tree expr
)
14120 /* If the expression does not have side effects then we don't have to wrap
14121 it with a cleanup point expression. */
14122 if (!TREE_SIDE_EFFECTS (expr
))
14125 /* If the expression is a return, check to see if the expression inside the
14126 return has no side effects or the right hand side of the modify expression
14127 inside the return. If either don't have side effects set we don't need to
14128 wrap the expression in a cleanup point expression. Note we don't check the
14129 left hand side of the modify because it should always be a return decl. */
14130 if (TREE_CODE (expr
) == RETURN_EXPR
)
14132 tree op
= TREE_OPERAND (expr
, 0);
14133 if (!op
|| !TREE_SIDE_EFFECTS (op
))
14135 op
= TREE_OPERAND (op
, 1);
14136 if (!TREE_SIDE_EFFECTS (op
))
14140 return build1 (CLEANUP_POINT_EXPR
, type
, expr
);
14143 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14144 of an indirection through OP0, or NULL_TREE if no simplification is
14148 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
14154 subtype
= TREE_TYPE (sub
);
14155 if (!POINTER_TYPE_P (subtype
))
14158 if (TREE_CODE (sub
) == ADDR_EXPR
)
14160 tree op
= TREE_OPERAND (sub
, 0);
14161 tree optype
= TREE_TYPE (op
);
14162 /* *&CONST_DECL -> to the value of the const decl. */
14163 if (TREE_CODE (op
) == CONST_DECL
)
14164 return DECL_INITIAL (op
);
14165 /* *&p => p; make sure to handle *&"str"[cst] here. */
14166 if (type
== optype
)
14168 tree fop
= fold_read_from_constant_string (op
);
14174 /* *(foo *)&fooarray => fooarray[0] */
14175 else if (TREE_CODE (optype
) == ARRAY_TYPE
14176 && type
== TREE_TYPE (optype
)
14177 && (!in_gimple_form
14178 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14180 tree type_domain
= TYPE_DOMAIN (optype
);
14181 tree min_val
= size_zero_node
;
14182 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14183 min_val
= TYPE_MIN_VALUE (type_domain
);
14185 && TREE_CODE (min_val
) != INTEGER_CST
)
14187 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
14188 NULL_TREE
, NULL_TREE
);
14190 /* *(foo *)&complexfoo => __real__ complexfoo */
14191 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14192 && type
== TREE_TYPE (optype
))
14193 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14194 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14195 else if (TREE_CODE (optype
) == VECTOR_TYPE
14196 && type
== TREE_TYPE (optype
))
14198 tree part_width
= TYPE_SIZE (type
);
14199 tree index
= bitsize_int (0);
14200 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14204 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14205 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14207 tree op00
= TREE_OPERAND (sub
, 0);
14208 tree op01
= TREE_OPERAND (sub
, 1);
14211 if (TREE_CODE (op00
) == ADDR_EXPR
)
14214 op00
= TREE_OPERAND (op00
, 0);
14215 op00type
= TREE_TYPE (op00
);
14217 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14218 if (TREE_CODE (op00type
) == VECTOR_TYPE
14219 && type
== TREE_TYPE (op00type
))
14221 tree part_width
= TYPE_SIZE (type
);
14222 unsigned HOST_WIDE_INT max_offset
14223 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14224 * TYPE_VECTOR_SUBPARTS (op00type
));
14225 if (tree_int_cst_sign_bit (op01
) == 0
14226 && compare_tree_int (op01
, max_offset
) == -1)
14228 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (op01
);
14229 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14230 tree index
= bitsize_int (indexi
);
14231 return fold_build3_loc (loc
,
14232 BIT_FIELD_REF
, type
, op00
,
14233 part_width
, index
);
14236 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14237 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14238 && type
== TREE_TYPE (op00type
))
14240 tree size
= TYPE_SIZE_UNIT (type
);
14241 if (tree_int_cst_equal (size
, op01
))
14242 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14244 /* ((foo *)&fooarray)[1] => fooarray[1] */
14245 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14246 && type
== TREE_TYPE (op00type
))
14248 tree type_domain
= TYPE_DOMAIN (op00type
);
14249 tree min_val
= size_zero_node
;
14250 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14251 min_val
= TYPE_MIN_VALUE (type_domain
);
14252 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
14253 TYPE_SIZE_UNIT (type
));
14254 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
14255 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14256 NULL_TREE
, NULL_TREE
);
14261 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14262 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14263 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14264 && (!in_gimple_form
14265 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14268 tree min_val
= size_zero_node
;
14269 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14270 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14271 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14272 min_val
= TYPE_MIN_VALUE (type_domain
);
14274 && TREE_CODE (min_val
) != INTEGER_CST
)
14276 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14283 /* Builds an expression for an indirection through T, simplifying some
14287 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14289 tree type
= TREE_TYPE (TREE_TYPE (t
));
14290 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14295 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14298 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14301 fold_indirect_ref_loc (location_t loc
, tree t
)
14303 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14311 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14312 whose result is ignored. The type of the returned tree need not be
14313 the same as the original expression. */
14316 fold_ignored_result (tree t
)
14318 if (!TREE_SIDE_EFFECTS (t
))
14319 return integer_zero_node
;
14322 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14325 t
= TREE_OPERAND (t
, 0);
14329 case tcc_comparison
:
14330 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14331 t
= TREE_OPERAND (t
, 0);
14332 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14333 t
= TREE_OPERAND (t
, 1);
14338 case tcc_expression
:
14339 switch (TREE_CODE (t
))
14341 case COMPOUND_EXPR
:
14342 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14344 t
= TREE_OPERAND (t
, 0);
14348 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14349 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14351 t
= TREE_OPERAND (t
, 0);
14364 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14367 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14369 tree div
= NULL_TREE
;
14374 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14375 have to do anything. Only do this when we are not given a const,
14376 because in that case, this check is more expensive than just
14378 if (TREE_CODE (value
) != INTEGER_CST
)
14380 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14382 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14386 /* If divisor is a power of two, simplify this to bit manipulation. */
14387 if (divisor
== (divisor
& -divisor
))
14389 if (TREE_CODE (value
) == INTEGER_CST
)
14391 wide_int val
= value
;
14394 if ((val
& (divisor
- 1)) == 0)
14397 overflow_p
= TREE_OVERFLOW (value
);
14398 val
+= divisor
- 1;
14399 val
&= - (int) divisor
;
14403 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14409 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14410 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14411 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14412 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14418 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14419 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14420 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14426 /* Likewise, but round down. */
14429 round_down_loc (location_t loc
, tree value
, int divisor
)
14431 tree div
= NULL_TREE
;
14433 gcc_assert (divisor
> 0);
14437 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14438 have to do anything. Only do this when we are not given a const,
14439 because in that case, this check is more expensive than just
14441 if (TREE_CODE (value
) != INTEGER_CST
)
14443 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14445 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14449 /* If divisor is a power of two, simplify this to bit manipulation. */
14450 if (divisor
== (divisor
& -divisor
))
14454 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14455 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14460 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14461 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14462 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14468 /* Returns the pointer to the base of the object addressed by EXP and
14469 extracts the information about the offset of the access, storing it
14470 to PBITPOS and POFFSET. */
14473 split_address_to_core_and_offset (tree exp
,
14474 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14478 int unsignedp
, reversep
, volatilep
;
14479 HOST_WIDE_INT bitsize
;
14480 location_t loc
= EXPR_LOCATION (exp
);
14482 if (TREE_CODE (exp
) == ADDR_EXPR
)
14484 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14485 poffset
, &mode
, &unsignedp
, &reversep
,
14486 &volatilep
, false);
14487 core
= build_fold_addr_expr_loc (loc
, core
);
14493 *poffset
= NULL_TREE
;
14499 /* Returns true if addresses of E1 and E2 differ by a constant, false
14500 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14503 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14506 HOST_WIDE_INT bitpos1
, bitpos2
;
14507 tree toffset1
, toffset2
, tdiff
, type
;
14509 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14510 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14512 if (bitpos1
% BITS_PER_UNIT
!= 0
14513 || bitpos2
% BITS_PER_UNIT
!= 0
14514 || !operand_equal_p (core1
, core2
, 0))
14517 if (toffset1
&& toffset2
)
14519 type
= TREE_TYPE (toffset1
);
14520 if (type
!= TREE_TYPE (toffset2
))
14521 toffset2
= fold_convert (type
, toffset2
);
14523 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14524 if (!cst_and_fits_in_hwi (tdiff
))
14527 *diff
= int_cst_value (tdiff
);
14529 else if (toffset1
|| toffset2
)
14531 /* If only one of the offsets is non-constant, the difference cannot
14538 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14542 /* Return OFF converted to a pointer offset type suitable as offset for
14543 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14545 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14547 return fold_convert_loc (loc
, sizetype
, off
);
14550 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14552 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14554 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14555 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14558 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14560 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14562 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14563 ptr
, size_int (off
));
14566 /* Return a char pointer for a C string if it is a string constant
14567 or sum of string constant and integer constant. */
14570 c_getstr (tree src
)
14574 src
= string_constant (src
, &offset_node
);
14578 if (offset_node
== 0)
14579 return TREE_STRING_POINTER (src
);
14580 else if (!tree_fits_uhwi_p (offset_node
)
14581 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
14584 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);