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"
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
85 /* Nonzero if we are folding constants inside an initializer; zero
87 int folding_initializer
= 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code
{
111 static bool negate_expr_p (tree
);
112 static tree
negate_expr (tree
);
113 static tree
split_tree (location_t
, tree
, tree
, enum tree_code
,
114 tree
*, tree
*, tree
*, int);
115 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
116 static enum comparison_code
comparison_to_compcode (enum tree_code
);
117 static enum tree_code
compcode_to_comparison (enum comparison_code
);
118 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
119 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
120 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
121 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
123 static int simple_operand_p (const_tree
);
124 static bool simple_operand_p_2 (tree
);
125 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
126 static tree
range_predecessor (tree
);
127 static tree
range_successor (tree
);
128 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
129 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
130 static tree
unextend (tree
, int, int, tree
);
131 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
132 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
133 static tree
fold_binary_op_with_conditional_arg (location_t
,
134 enum tree_code
, tree
,
137 static tree
fold_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
138 static bool reorder_operands_p (const_tree
, const_tree
);
139 static tree
fold_negate_const (tree
, tree
);
140 static tree
fold_not_const (const_tree
, tree
);
141 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
142 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
143 static tree
fold_view_convert_expr (tree
, tree
);
144 static bool vec_cst_ctor_to_array (tree
, tree
*);
147 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
148 Otherwise, return LOC. */
151 expr_location_or (tree t
, location_t loc
)
153 location_t tloc
= EXPR_LOCATION (t
);
154 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
157 /* Similar to protected_set_expr_location, but never modify x in place,
158 if location can and needs to be set, unshare it. */
161 protected_set_expr_location_unshare (tree x
, location_t loc
)
163 if (CAN_HAVE_LOCATION_P (x
)
164 && EXPR_LOCATION (x
) != loc
165 && !(TREE_CODE (x
) == SAVE_EXPR
166 || TREE_CODE (x
) == TARGET_EXPR
167 || TREE_CODE (x
) == BIND_EXPR
))
170 SET_EXPR_LOCATION (x
, loc
);
175 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
176 division and returns the quotient. Otherwise returns
180 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
184 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
186 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
191 /* This is nonzero if we should defer warnings about undefined
192 overflow. This facility exists because these warnings are a
193 special case. The code to estimate loop iterations does not want
194 to issue any warnings, since it works with expressions which do not
195 occur in user code. Various bits of cleanup code call fold(), but
196 only use the result if it has certain characteristics (e.g., is a
197 constant); that code only wants to issue a warning if the result is
200 static int fold_deferring_overflow_warnings
;
202 /* If a warning about undefined overflow is deferred, this is the
203 warning. Note that this may cause us to turn two warnings into
204 one, but that is fine since it is sufficient to only give one
205 warning per expression. */
207 static const char* fold_deferred_overflow_warning
;
209 /* If a warning about undefined overflow is deferred, this is the
210 level at which the warning should be emitted. */
212 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
214 /* Start deferring overflow warnings. We could use a stack here to
215 permit nested calls, but at present it is not necessary. */
218 fold_defer_overflow_warnings (void)
220 ++fold_deferring_overflow_warnings
;
223 /* Stop deferring overflow warnings. If there is a pending warning,
224 and ISSUE is true, then issue the warning if appropriate. STMT is
225 the statement with which the warning should be associated (used for
226 location information); STMT may be NULL. CODE is the level of the
227 warning--a warn_strict_overflow_code value. This function will use
228 the smaller of CODE and the deferred code when deciding whether to
229 issue the warning. CODE may be zero to mean to always use the
233 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
238 gcc_assert (fold_deferring_overflow_warnings
> 0);
239 --fold_deferring_overflow_warnings
;
240 if (fold_deferring_overflow_warnings
> 0)
242 if (fold_deferred_overflow_warning
!= NULL
244 && code
< (int) fold_deferred_overflow_code
)
245 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
249 warnmsg
= fold_deferred_overflow_warning
;
250 fold_deferred_overflow_warning
= NULL
;
252 if (!issue
|| warnmsg
== NULL
)
255 if (gimple_no_warning_p (stmt
))
258 /* Use the smallest code level when deciding to issue the
260 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
261 code
= fold_deferred_overflow_code
;
263 if (!issue_strict_overflow_warning (code
))
267 locus
= input_location
;
269 locus
= gimple_location (stmt
);
270 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
273 /* Stop deferring overflow warnings, ignoring any deferred
277 fold_undefer_and_ignore_overflow_warnings (void)
279 fold_undefer_overflow_warnings (false, NULL
, 0);
282 /* Whether we are deferring overflow warnings. */
285 fold_deferring_overflow_warnings_p (void)
287 return fold_deferring_overflow_warnings
> 0;
290 /* This is called when we fold something based on the fact that signed
291 overflow is undefined. */
294 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
296 if (fold_deferring_overflow_warnings
> 0)
298 if (fold_deferred_overflow_warning
== NULL
299 || wc
< fold_deferred_overflow_code
)
301 fold_deferred_overflow_warning
= gmsgid
;
302 fold_deferred_overflow_code
= wc
;
305 else if (issue_strict_overflow_warning (wc
))
306 warning (OPT_Wstrict_overflow
, gmsgid
);
309 /* Return true if the built-in mathematical function specified by CODE
310 is odd, i.e. -f(x) == f(-x). */
313 negate_mathfn_p (combined_fn fn
)
346 return !flag_rounding_math
;
354 /* Check whether we may negate an integer constant T without causing
358 may_negate_without_overflow_p (const_tree t
)
362 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
364 type
= TREE_TYPE (t
);
365 if (TYPE_UNSIGNED (type
))
368 return !wi::only_sign_bit_p (t
);
371 /* Determine whether an expression T can be cheaply negated using
372 the function negate_expr without introducing undefined overflow. */
375 negate_expr_p (tree t
)
382 type
= TREE_TYPE (t
);
385 switch (TREE_CODE (t
))
388 if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
391 /* Check that -CST will not overflow type. */
392 return may_negate_without_overflow_p (t
);
394 return (INTEGRAL_TYPE_P (type
)
395 && TYPE_OVERFLOW_WRAPS (type
));
401 return !TYPE_OVERFLOW_SANITIZED (type
);
404 /* We want to canonicalize to positive real constants. Pretend
405 that only negative ones can be easily negated. */
406 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
409 return negate_expr_p (TREE_REALPART (t
))
410 && negate_expr_p (TREE_IMAGPART (t
));
414 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
417 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
419 for (i
= 0; i
< count
; i
++)
420 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
427 return negate_expr_p (TREE_OPERAND (t
, 0))
428 && negate_expr_p (TREE_OPERAND (t
, 1));
431 return negate_expr_p (TREE_OPERAND (t
, 0));
434 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
435 || HONOR_SIGNED_ZEROS (element_mode (type
))
436 || (INTEGRAL_TYPE_P (type
)
437 && ! TYPE_OVERFLOW_WRAPS (type
)))
439 /* -(A + B) -> (-B) - A. */
440 if (negate_expr_p (TREE_OPERAND (t
, 1))
441 && reorder_operands_p (TREE_OPERAND (t
, 0),
442 TREE_OPERAND (t
, 1)))
444 /* -(A + B) -> (-A) - B. */
445 return negate_expr_p (TREE_OPERAND (t
, 0));
448 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
449 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
450 && !HONOR_SIGNED_ZEROS (element_mode (type
))
451 && (! INTEGRAL_TYPE_P (type
)
452 || TYPE_OVERFLOW_WRAPS (type
))
453 && reorder_operands_p (TREE_OPERAND (t
, 0),
454 TREE_OPERAND (t
, 1));
457 if (TYPE_UNSIGNED (type
))
459 /* INT_MIN/n * n doesn't overflow while negating one operand it does
460 if n is a power of two. */
461 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
462 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
463 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
464 && ! integer_pow2p (TREE_OPERAND (t
, 0)))
465 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
466 && ! integer_pow2p (TREE_OPERAND (t
, 1)))))
472 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
473 return negate_expr_p (TREE_OPERAND (t
, 1))
474 || negate_expr_p (TREE_OPERAND (t
, 0));
480 if (TYPE_UNSIGNED (type
))
482 if (negate_expr_p (TREE_OPERAND (t
, 0)))
484 /* In general we can't negate B in A / B, because if A is INT_MIN and
485 B is 1, we may turn this into INT_MIN / -1 which is undefined
486 and actually traps on some architectures. */
487 if (! INTEGRAL_TYPE_P (TREE_TYPE (t
))
488 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
489 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
490 && ! integer_onep (TREE_OPERAND (t
, 1))))
491 return negate_expr_p (TREE_OPERAND (t
, 1));
495 /* Negate -((double)float) as (double)(-float). */
496 if (TREE_CODE (type
) == REAL_TYPE
)
498 tree tem
= strip_float_extensions (t
);
500 return negate_expr_p (tem
);
505 /* Negate -f(x) as f(-x). */
506 if (negate_mathfn_p (get_call_combined_fn (t
)))
507 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
511 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
512 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
514 tree op1
= TREE_OPERAND (t
, 1);
515 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
526 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
527 simplification is possible.
528 If negate_expr_p would return true for T, NULL_TREE will never be
532 fold_negate_expr (location_t loc
, tree t
)
534 tree type
= TREE_TYPE (t
);
537 switch (TREE_CODE (t
))
539 /* Convert - (~A) to A + 1. */
541 if (INTEGRAL_TYPE_P (type
))
542 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
543 build_one_cst (type
));
547 tem
= fold_negate_const (t
, type
);
548 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
549 || (ANY_INTEGRAL_TYPE_P (type
)
550 && !TYPE_OVERFLOW_TRAPS (type
)
551 && TYPE_OVERFLOW_WRAPS (type
))
552 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
557 tem
= fold_negate_const (t
, type
);
561 tem
= fold_negate_const (t
, type
);
566 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
567 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
569 return build_complex (type
, rpart
, ipart
);
575 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
576 tree
*elts
= XALLOCAVEC (tree
, count
);
578 for (i
= 0; i
< count
; i
++)
580 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
581 if (elts
[i
] == NULL_TREE
)
585 return build_vector (type
, elts
);
589 if (negate_expr_p (t
))
590 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
591 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
592 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
596 if (negate_expr_p (t
))
597 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
598 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
602 if (!TYPE_OVERFLOW_SANITIZED (type
))
603 return TREE_OPERAND (t
, 0);
607 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
608 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
610 /* -(A + B) -> (-B) - A. */
611 if (negate_expr_p (TREE_OPERAND (t
, 1))
612 && reorder_operands_p (TREE_OPERAND (t
, 0),
613 TREE_OPERAND (t
, 1)))
615 tem
= negate_expr (TREE_OPERAND (t
, 1));
616 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
617 tem
, TREE_OPERAND (t
, 0));
620 /* -(A + B) -> (-A) - B. */
621 if (negate_expr_p (TREE_OPERAND (t
, 0)))
623 tem
= negate_expr (TREE_OPERAND (t
, 0));
624 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
625 tem
, TREE_OPERAND (t
, 1));
631 /* - (A - B) -> B - A */
632 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
633 && !HONOR_SIGNED_ZEROS (element_mode (type
))
634 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
635 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
636 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
640 if (TYPE_UNSIGNED (type
))
646 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
648 tem
= TREE_OPERAND (t
, 1);
649 if (negate_expr_p (tem
))
650 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
651 TREE_OPERAND (t
, 0), negate_expr (tem
));
652 tem
= TREE_OPERAND (t
, 0);
653 if (negate_expr_p (tem
))
654 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
655 negate_expr (tem
), TREE_OPERAND (t
, 1));
662 if (TYPE_UNSIGNED (type
))
664 if (negate_expr_p (TREE_OPERAND (t
, 0)))
665 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
666 negate_expr (TREE_OPERAND (t
, 0)),
667 TREE_OPERAND (t
, 1));
668 /* In general we can't negate B in A / B, because if A is INT_MIN and
669 B is 1, we may turn this into INT_MIN / -1 which is undefined
670 and actually traps on some architectures. */
671 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t
))
672 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
673 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
674 && ! integer_onep (TREE_OPERAND (t
, 1))))
675 && negate_expr_p (TREE_OPERAND (t
, 1)))
676 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
678 negate_expr (TREE_OPERAND (t
, 1)));
682 /* Convert -((double)float) into (double)(-float). */
683 if (TREE_CODE (type
) == REAL_TYPE
)
685 tem
= strip_float_extensions (t
);
686 if (tem
!= t
&& negate_expr_p (tem
))
687 return fold_convert_loc (loc
, type
, negate_expr (tem
));
692 /* Negate -f(x) as f(-x). */
693 if (negate_mathfn_p (get_call_combined_fn (t
))
694 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
698 fndecl
= get_callee_fndecl (t
);
699 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
700 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
705 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
706 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
708 tree op1
= TREE_OPERAND (t
, 1);
709 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
711 tree ntype
= TYPE_UNSIGNED (type
)
712 ? signed_type_for (type
)
713 : unsigned_type_for (type
);
714 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
715 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
716 return fold_convert_loc (loc
, type
, temp
);
728 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
729 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
741 loc
= EXPR_LOCATION (t
);
742 type
= TREE_TYPE (t
);
745 tem
= fold_negate_expr (loc
, t
);
747 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
748 return fold_convert_loc (loc
, type
, tem
);
751 /* Split a tree IN into a constant, literal and variable parts that could be
752 combined with CODE to make IN. "constant" means an expression with
753 TREE_CONSTANT but that isn't an actual constant. CODE must be a
754 commutative arithmetic operation. Store the constant part into *CONP,
755 the literal in *LITP and return the variable part. If a part isn't
756 present, set it to null. If the tree does not decompose in this way,
757 return the entire tree as the variable part and the other parts as null.
759 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
760 case, we negate an operand that was subtracted. Except if it is a
761 literal for which we use *MINUS_LITP instead.
763 If NEGATE_P is true, we are negating all of IN, again except a literal
764 for which we use *MINUS_LITP instead. If a variable part is of pointer
765 type, it is negated after converting to TYPE. This prevents us from
766 generating illegal MINUS pointer expression. LOC is the location of
767 the converted variable part.
769 If IN is itself a literal or constant, return it as appropriate.
771 Note that we do not guarantee that any of the three values will be the
772 same type as IN, but they will have the same signedness and mode. */
775 split_tree (location_t loc
, tree in
, tree type
, enum tree_code code
,
776 tree
*conp
, tree
*litp
, tree
*minus_litp
, int negate_p
)
784 /* Strip any conversions that don't change the machine mode or signedness. */
785 STRIP_SIGN_NOPS (in
);
787 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
788 || TREE_CODE (in
) == FIXED_CST
)
790 else if (TREE_CODE (in
) == code
791 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
792 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
793 /* We can associate addition and subtraction together (even
794 though the C standard doesn't say so) for integers because
795 the value is not affected. For reals, the value might be
796 affected, so we can't. */
797 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
798 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
800 tree op0
= TREE_OPERAND (in
, 0);
801 tree op1
= TREE_OPERAND (in
, 1);
802 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
803 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
805 /* First see if either of the operands is a literal, then a constant. */
806 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
807 || TREE_CODE (op0
) == FIXED_CST
)
808 *litp
= op0
, op0
= 0;
809 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
810 || TREE_CODE (op1
) == FIXED_CST
)
811 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
813 if (op0
!= 0 && TREE_CONSTANT (op0
))
814 *conp
= op0
, op0
= 0;
815 else if (op1
!= 0 && TREE_CONSTANT (op1
))
816 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
818 /* If we haven't dealt with either operand, this is not a case we can
819 decompose. Otherwise, VAR is either of the ones remaining, if any. */
820 if (op0
!= 0 && op1
!= 0)
825 var
= op1
, neg_var_p
= neg1_p
;
827 /* Now do any needed negations. */
829 *minus_litp
= *litp
, *litp
= 0;
831 *conp
= negate_expr (*conp
);
832 if (neg_var_p
&& var
)
834 /* Convert to TYPE before negating. */
835 var
= fold_convert_loc (loc
, type
, var
);
836 var
= negate_expr (var
);
839 else if (TREE_CODE (in
) == BIT_NOT_EXPR
840 && code
== PLUS_EXPR
)
842 /* -X - 1 is folded to ~X, undo that here. */
843 *minus_litp
= build_one_cst (TREE_TYPE (in
));
844 var
= negate_expr (TREE_OPERAND (in
, 0));
846 else if (TREE_CONSTANT (in
))
854 *minus_litp
= *litp
, *litp
= 0;
855 else if (*minus_litp
)
856 *litp
= *minus_litp
, *minus_litp
= 0;
857 *conp
= negate_expr (*conp
);
860 /* Convert to TYPE before negating. */
861 var
= fold_convert_loc (loc
, type
, var
);
862 var
= negate_expr (var
);
869 /* Re-associate trees split by the above function. T1 and T2 are
870 either expressions to associate or null. Return the new
871 expression, if any. LOC is the location of the new expression. If
872 we build an operation, do it in TYPE and with CODE. */
875 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
882 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
883 try to fold this since we will have infinite recursion. But do
884 deal with any NEGATE_EXPRs. */
885 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
886 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
888 if (code
== PLUS_EXPR
)
890 if (TREE_CODE (t1
) == NEGATE_EXPR
)
891 return build2_loc (loc
, MINUS_EXPR
, type
,
892 fold_convert_loc (loc
, type
, t2
),
893 fold_convert_loc (loc
, type
,
894 TREE_OPERAND (t1
, 0)));
895 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
896 return build2_loc (loc
, MINUS_EXPR
, type
,
897 fold_convert_loc (loc
, type
, t1
),
898 fold_convert_loc (loc
, type
,
899 TREE_OPERAND (t2
, 0)));
900 else if (integer_zerop (t2
))
901 return fold_convert_loc (loc
, type
, t1
);
903 else if (code
== MINUS_EXPR
)
905 if (integer_zerop (t2
))
906 return fold_convert_loc (loc
, type
, t1
);
909 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
910 fold_convert_loc (loc
, type
, t2
));
913 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
914 fold_convert_loc (loc
, type
, t2
));
917 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
918 for use in int_const_binop, size_binop and size_diffop. */
921 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
923 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
925 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
940 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
941 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
942 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
946 /* Combine two integer constants ARG1 and ARG2 under operation CODE
947 to produce a new constant. Return NULL_TREE if we don't know how
948 to evaluate CODE at compile-time. */
951 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
956 tree type
= TREE_TYPE (arg1
);
957 signop sign
= TYPE_SIGN (type
);
958 bool overflow
= false;
960 wide_int arg2
= wi::to_wide (parg2
, TYPE_PRECISION (type
));
965 res
= wi::bit_or (arg1
, arg2
);
969 res
= wi::bit_xor (arg1
, arg2
);
973 res
= wi::bit_and (arg1
, arg2
);
978 if (wi::neg_p (arg2
))
981 if (code
== RSHIFT_EXPR
)
987 if (code
== RSHIFT_EXPR
)
988 /* It's unclear from the C standard whether shifts can overflow.
989 The following code ignores overflow; perhaps a C standard
990 interpretation ruling is needed. */
991 res
= wi::rshift (arg1
, arg2
, sign
);
993 res
= wi::lshift (arg1
, arg2
);
998 if (wi::neg_p (arg2
))
1001 if (code
== RROTATE_EXPR
)
1002 code
= LROTATE_EXPR
;
1004 code
= RROTATE_EXPR
;
1007 if (code
== RROTATE_EXPR
)
1008 res
= wi::rrotate (arg1
, arg2
);
1010 res
= wi::lrotate (arg1
, arg2
);
1014 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1018 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1022 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1025 case MULT_HIGHPART_EXPR
:
1026 res
= wi::mul_high (arg1
, arg2
, sign
);
1029 case TRUNC_DIV_EXPR
:
1030 case EXACT_DIV_EXPR
:
1033 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1036 case FLOOR_DIV_EXPR
:
1039 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1045 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1048 case ROUND_DIV_EXPR
:
1051 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1054 case TRUNC_MOD_EXPR
:
1057 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1060 case FLOOR_MOD_EXPR
:
1063 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1069 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1072 case ROUND_MOD_EXPR
:
1075 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1079 res
= wi::min (arg1
, arg2
, sign
);
1083 res
= wi::max (arg1
, arg2
, sign
);
1090 t
= force_fit_type (type
, res
, overflowable
,
1091 (((sign
== SIGNED
|| overflowable
== -1)
1093 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1099 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1101 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1104 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1105 constant. We assume ARG1 and ARG2 have the same data type, or at least
1106 are the same kind of constant and the same machine mode. Return zero if
1107 combining the constants is not allowed in the current operating mode. */
1110 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1112 /* Sanity check for the recursive cases. */
1119 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1121 if (code
== POINTER_PLUS_EXPR
)
1122 return int_const_binop (PLUS_EXPR
,
1123 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1125 return int_const_binop (code
, arg1
, arg2
);
1128 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1133 REAL_VALUE_TYPE value
;
1134 REAL_VALUE_TYPE result
;
1138 /* The following codes are handled by real_arithmetic. */
1153 d1
= TREE_REAL_CST (arg1
);
1154 d2
= TREE_REAL_CST (arg2
);
1156 type
= TREE_TYPE (arg1
);
1157 mode
= TYPE_MODE (type
);
1159 /* Don't perform operation if we honor signaling NaNs and
1160 either operand is a signaling NaN. */
1161 if (HONOR_SNANS (mode
)
1162 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1163 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1166 /* Don't perform operation if it would raise a division
1167 by zero exception. */
1168 if (code
== RDIV_EXPR
1169 && real_equal (&d2
, &dconst0
)
1170 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1173 /* If either operand is a NaN, just return it. Otherwise, set up
1174 for floating-point trap; we return an overflow. */
1175 if (REAL_VALUE_ISNAN (d1
))
1177 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1180 t
= build_real (type
, d1
);
1183 else if (REAL_VALUE_ISNAN (d2
))
1185 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1188 t
= build_real (type
, d2
);
1192 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1193 real_convert (&result
, mode
, &value
);
1195 /* Don't constant fold this floating point operation if
1196 the result has overflowed and flag_trapping_math. */
1197 if (flag_trapping_math
1198 && MODE_HAS_INFINITIES (mode
)
1199 && REAL_VALUE_ISINF (result
)
1200 && !REAL_VALUE_ISINF (d1
)
1201 && !REAL_VALUE_ISINF (d2
))
1204 /* Don't constant fold this floating point operation if the
1205 result may dependent upon the run-time rounding mode and
1206 flag_rounding_math is set, or if GCC's software emulation
1207 is unable to accurately represent the result. */
1208 if ((flag_rounding_math
1209 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1210 && (inexact
|| !real_identical (&result
, &value
)))
1213 t
= build_real (type
, result
);
1215 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1219 if (TREE_CODE (arg1
) == FIXED_CST
)
1221 FIXED_VALUE_TYPE f1
;
1222 FIXED_VALUE_TYPE f2
;
1223 FIXED_VALUE_TYPE result
;
1228 /* The following codes are handled by fixed_arithmetic. */
1234 case TRUNC_DIV_EXPR
:
1235 if (TREE_CODE (arg2
) != FIXED_CST
)
1237 f2
= TREE_FIXED_CST (arg2
);
1243 if (TREE_CODE (arg2
) != INTEGER_CST
)
1246 f2
.data
.high
= w2
.elt (1);
1247 f2
.data
.low
= w2
.elt (0);
1256 f1
= TREE_FIXED_CST (arg1
);
1257 type
= TREE_TYPE (arg1
);
1258 sat_p
= TYPE_SATURATING (type
);
1259 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1260 t
= build_fixed (type
, result
);
1261 /* Propagate overflow flags. */
1262 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1263 TREE_OVERFLOW (t
) = 1;
1267 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1269 tree type
= TREE_TYPE (arg1
);
1270 tree r1
= TREE_REALPART (arg1
);
1271 tree i1
= TREE_IMAGPART (arg1
);
1272 tree r2
= TREE_REALPART (arg2
);
1273 tree i2
= TREE_IMAGPART (arg2
);
1280 real
= const_binop (code
, r1
, r2
);
1281 imag
= const_binop (code
, i1
, i2
);
1285 if (COMPLEX_FLOAT_TYPE_P (type
))
1286 return do_mpc_arg2 (arg1
, arg2
, type
,
1287 /* do_nonfinite= */ folding_initializer
,
1290 real
= const_binop (MINUS_EXPR
,
1291 const_binop (MULT_EXPR
, r1
, r2
),
1292 const_binop (MULT_EXPR
, i1
, i2
));
1293 imag
= const_binop (PLUS_EXPR
,
1294 const_binop (MULT_EXPR
, r1
, i2
),
1295 const_binop (MULT_EXPR
, i1
, r2
));
1299 if (COMPLEX_FLOAT_TYPE_P (type
))
1300 return do_mpc_arg2 (arg1
, arg2
, type
,
1301 /* do_nonfinite= */ folding_initializer
,
1304 case TRUNC_DIV_EXPR
:
1306 case FLOOR_DIV_EXPR
:
1307 case ROUND_DIV_EXPR
:
1308 if (flag_complex_method
== 0)
1310 /* Keep this algorithm in sync with
1311 tree-complex.c:expand_complex_div_straight().
1313 Expand complex division to scalars, straightforward algorithm.
1314 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1318 = const_binop (PLUS_EXPR
,
1319 const_binop (MULT_EXPR
, r2
, r2
),
1320 const_binop (MULT_EXPR
, i2
, i2
));
1322 = const_binop (PLUS_EXPR
,
1323 const_binop (MULT_EXPR
, r1
, r2
),
1324 const_binop (MULT_EXPR
, i1
, i2
));
1326 = const_binop (MINUS_EXPR
,
1327 const_binop (MULT_EXPR
, i1
, r2
),
1328 const_binop (MULT_EXPR
, r1
, i2
));
1330 real
= const_binop (code
, t1
, magsquared
);
1331 imag
= const_binop (code
, t2
, magsquared
);
1335 /* Keep this algorithm in sync with
1336 tree-complex.c:expand_complex_div_wide().
1338 Expand complex division to scalars, modified algorithm to minimize
1339 overflow with wide input ranges. */
1340 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1341 fold_abs_const (r2
, TREE_TYPE (type
)),
1342 fold_abs_const (i2
, TREE_TYPE (type
)));
1344 if (integer_nonzerop (compare
))
1346 /* In the TRUE branch, we compute
1348 div = (br * ratio) + bi;
1349 tr = (ar * ratio) + ai;
1350 ti = (ai * ratio) - ar;
1353 tree ratio
= const_binop (code
, r2
, i2
);
1354 tree div
= const_binop (PLUS_EXPR
, i2
,
1355 const_binop (MULT_EXPR
, r2
, ratio
));
1356 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1357 real
= const_binop (PLUS_EXPR
, real
, i1
);
1358 real
= const_binop (code
, real
, div
);
1360 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1361 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1362 imag
= const_binop (code
, imag
, div
);
1366 /* In the FALSE branch, we compute
1368 divisor = (d * ratio) + c;
1369 tr = (b * ratio) + a;
1370 ti = b - (a * ratio);
1373 tree ratio
= const_binop (code
, i2
, r2
);
1374 tree div
= const_binop (PLUS_EXPR
, r2
,
1375 const_binop (MULT_EXPR
, i2
, ratio
));
1377 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1378 real
= const_binop (PLUS_EXPR
, real
, r1
);
1379 real
= const_binop (code
, real
, div
);
1381 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1382 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1383 imag
= const_binop (code
, imag
, div
);
1393 return build_complex (type
, real
, imag
);
1396 if (TREE_CODE (arg1
) == VECTOR_CST
1397 && TREE_CODE (arg2
) == VECTOR_CST
)
1399 tree type
= TREE_TYPE (arg1
);
1400 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1401 tree
*elts
= XALLOCAVEC (tree
, count
);
1403 for (i
= 0; i
< count
; i
++)
1405 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1406 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1408 elts
[i
] = const_binop (code
, elem1
, elem2
);
1410 /* It is possible that const_binop cannot handle the given
1411 code and return NULL_TREE */
1412 if (elts
[i
] == NULL_TREE
)
1416 return build_vector (type
, elts
);
1419 /* Shifts allow a scalar offset for a vector. */
1420 if (TREE_CODE (arg1
) == VECTOR_CST
1421 && TREE_CODE (arg2
) == INTEGER_CST
)
1423 tree type
= TREE_TYPE (arg1
);
1424 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1425 tree
*elts
= XALLOCAVEC (tree
, count
);
1427 for (i
= 0; i
< count
; i
++)
1429 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1431 elts
[i
] = const_binop (code
, elem1
, arg2
);
1433 /* It is possible that const_binop cannot handle the given
1434 code and return NULL_TREE. */
1435 if (elts
[i
] == NULL_TREE
)
1439 return build_vector (type
, elts
);
1444 /* Overload that adds a TYPE parameter to be able to dispatch
1445 to fold_relational_const. */
1448 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1450 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1451 return fold_relational_const (code
, type
, arg1
, arg2
);
1453 /* ??? Until we make the const_binop worker take the type of the
1454 result as argument put those cases that need it here. */
1458 if ((TREE_CODE (arg1
) == REAL_CST
1459 && TREE_CODE (arg2
) == REAL_CST
)
1460 || (TREE_CODE (arg1
) == INTEGER_CST
1461 && TREE_CODE (arg2
) == INTEGER_CST
))
1462 return build_complex (type
, arg1
, arg2
);
1465 case VEC_PACK_TRUNC_EXPR
:
1466 case VEC_PACK_FIX_TRUNC_EXPR
:
1468 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1471 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1472 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1473 if (TREE_CODE (arg1
) != VECTOR_CST
1474 || TREE_CODE (arg2
) != VECTOR_CST
)
1477 elts
= XALLOCAVEC (tree
, nelts
);
1478 if (!vec_cst_ctor_to_array (arg1
, elts
)
1479 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1482 for (i
= 0; i
< nelts
; i
++)
1484 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1485 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1486 TREE_TYPE (type
), elts
[i
]);
1487 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1491 return build_vector (type
, elts
);
1494 case VEC_WIDEN_MULT_LO_EXPR
:
1495 case VEC_WIDEN_MULT_HI_EXPR
:
1496 case VEC_WIDEN_MULT_EVEN_EXPR
:
1497 case VEC_WIDEN_MULT_ODD_EXPR
:
1499 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1500 unsigned int out
, ofs
, scale
;
1503 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1504 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1505 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1508 elts
= XALLOCAVEC (tree
, nelts
* 4);
1509 if (!vec_cst_ctor_to_array (arg1
, elts
)
1510 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1513 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1514 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1515 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1516 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1517 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1519 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1522 for (out
= 0; out
< nelts
; out
++)
1524 unsigned int in1
= (out
<< scale
) + ofs
;
1525 unsigned int in2
= in1
+ nelts
* 2;
1528 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1529 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1531 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1533 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1534 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1538 return build_vector (type
, elts
);
1544 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1547 /* Make sure type and arg0 have the same saturating flag. */
1548 gcc_checking_assert (TYPE_SATURATING (type
)
1549 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1551 return const_binop (code
, arg1
, arg2
);
1554 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1555 Return zero if computing the constants is not possible. */
1558 const_unop (enum tree_code code
, tree type
, tree arg0
)
1560 /* Don't perform the operation, other than NEGATE and ABS, if
1561 flag_signaling_nans is on and the operand is a signaling NaN. */
1562 if (TREE_CODE (arg0
) == REAL_CST
1563 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1564 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1565 && code
!= NEGATE_EXPR
1566 && code
!= ABS_EXPR
)
1573 case FIX_TRUNC_EXPR
:
1574 case FIXED_CONVERT_EXPR
:
1575 return fold_convert_const (code
, type
, arg0
);
1577 case ADDR_SPACE_CONVERT_EXPR
:
1578 /* If the source address is 0, and the source address space
1579 cannot have a valid object at 0, fold to dest type null. */
1580 if (integer_zerop (arg0
)
1581 && !(targetm
.addr_space
.zero_address_valid
1582 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1583 return fold_convert_const (code
, type
, arg0
);
1586 case VIEW_CONVERT_EXPR
:
1587 return fold_view_convert_expr (type
, arg0
);
1591 /* Can't call fold_negate_const directly here as that doesn't
1592 handle all cases and we might not be able to negate some
1594 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1595 if (tem
&& CONSTANT_CLASS_P (tem
))
1601 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1602 return fold_abs_const (arg0
, type
);
1606 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1608 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1610 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1615 if (TREE_CODE (arg0
) == INTEGER_CST
)
1616 return fold_not_const (arg0
, type
);
1617 /* Perform BIT_NOT_EXPR on each element individually. */
1618 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1622 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1624 elements
= XALLOCAVEC (tree
, count
);
1625 for (i
= 0; i
< count
; i
++)
1627 elem
= VECTOR_CST_ELT (arg0
, i
);
1628 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1629 if (elem
== NULL_TREE
)
1634 return build_vector (type
, elements
);
1638 case TRUTH_NOT_EXPR
:
1639 if (TREE_CODE (arg0
) == INTEGER_CST
)
1640 return constant_boolean_node (integer_zerop (arg0
), type
);
1644 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1645 return fold_convert (type
, TREE_REALPART (arg0
));
1649 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1650 return fold_convert (type
, TREE_IMAGPART (arg0
));
1653 case VEC_UNPACK_LO_EXPR
:
1654 case VEC_UNPACK_HI_EXPR
:
1655 case VEC_UNPACK_FLOAT_LO_EXPR
:
1656 case VEC_UNPACK_FLOAT_HI_EXPR
:
1658 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1660 enum tree_code subcode
;
1662 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1663 if (TREE_CODE (arg0
) != VECTOR_CST
)
1666 elts
= XALLOCAVEC (tree
, nelts
* 2);
1667 if (!vec_cst_ctor_to_array (arg0
, elts
))
1670 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1671 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1674 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1677 subcode
= FLOAT_EXPR
;
1679 for (i
= 0; i
< nelts
; i
++)
1681 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1682 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1686 return build_vector (type
, elts
);
1689 case REDUC_MIN_EXPR
:
1690 case REDUC_MAX_EXPR
:
1691 case REDUC_PLUS_EXPR
:
1693 unsigned int nelts
, i
;
1695 enum tree_code subcode
;
1697 if (TREE_CODE (arg0
) != VECTOR_CST
)
1699 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1701 elts
= XALLOCAVEC (tree
, nelts
);
1702 if (!vec_cst_ctor_to_array (arg0
, elts
))
1707 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1708 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1709 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1710 default: gcc_unreachable ();
1713 for (i
= 1; i
< nelts
; i
++)
1715 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1716 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1730 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1731 indicates which particular sizetype to create. */
1734 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1736 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1739 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1740 is a tree code. The type of the result is taken from the operands.
1741 Both must be equivalent integer types, ala int_binop_types_match_p.
1742 If the operands are constant, so is the result. */
1745 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1747 tree type
= TREE_TYPE (arg0
);
1749 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1750 return error_mark_node
;
1752 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1755 /* Handle the special case of two integer constants faster. */
1756 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1758 /* And some specific cases even faster than that. */
1759 if (code
== PLUS_EXPR
)
1761 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1763 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1766 else if (code
== MINUS_EXPR
)
1768 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1771 else if (code
== MULT_EXPR
)
1773 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1777 /* Handle general case of two integer constants. For sizetype
1778 constant calculations we always want to know about overflow,
1779 even in the unsigned case. */
1780 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1783 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1786 /* Given two values, either both of sizetype or both of bitsizetype,
1787 compute the difference between the two values. Return the value
1788 in signed type corresponding to the type of the operands. */
1791 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1793 tree type
= TREE_TYPE (arg0
);
1796 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1799 /* If the type is already signed, just do the simple thing. */
1800 if (!TYPE_UNSIGNED (type
))
1801 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1803 if (type
== sizetype
)
1805 else if (type
== bitsizetype
)
1806 ctype
= sbitsizetype
;
1808 ctype
= signed_type_for (type
);
1810 /* If either operand is not a constant, do the conversions to the signed
1811 type and subtract. The hardware will do the right thing with any
1812 overflow in the subtraction. */
1813 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1814 return size_binop_loc (loc
, MINUS_EXPR
,
1815 fold_convert_loc (loc
, ctype
, arg0
),
1816 fold_convert_loc (loc
, ctype
, arg1
));
1818 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1819 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1820 overflow) and negate (which can't either). Special-case a result
1821 of zero while we're here. */
1822 if (tree_int_cst_equal (arg0
, arg1
))
1823 return build_int_cst (ctype
, 0);
1824 else if (tree_int_cst_lt (arg1
, arg0
))
1825 return fold_convert_loc (loc
, ctype
,
1826 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1828 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1829 fold_convert_loc (loc
, ctype
,
1830 size_binop_loc (loc
,
1835 /* A subroutine of fold_convert_const handling conversions of an
1836 INTEGER_CST to another integer type. */
1839 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1841 /* Given an integer constant, make new constant with new type,
1842 appropriately sign-extended or truncated. Use widest_int
1843 so that any extension is done according ARG1's type. */
1844 return force_fit_type (type
, wi::to_widest (arg1
),
1845 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1846 TREE_OVERFLOW (arg1
));
1849 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1850 to an integer type. */
1853 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1855 bool overflow
= false;
1858 /* The following code implements the floating point to integer
1859 conversion rules required by the Java Language Specification,
1860 that IEEE NaNs are mapped to zero and values that overflow
1861 the target precision saturate, i.e. values greater than
1862 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1863 are mapped to INT_MIN. These semantics are allowed by the
1864 C and C++ standards that simply state that the behavior of
1865 FP-to-integer conversion is unspecified upon overflow. */
1869 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1873 case FIX_TRUNC_EXPR
:
1874 real_trunc (&r
, VOIDmode
, &x
);
1881 /* If R is NaN, return zero and show we have an overflow. */
1882 if (REAL_VALUE_ISNAN (r
))
1885 val
= wi::zero (TYPE_PRECISION (type
));
1888 /* See if R is less than the lower bound or greater than the
1893 tree lt
= TYPE_MIN_VALUE (type
);
1894 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1895 if (real_less (&r
, &l
))
1904 tree ut
= TYPE_MAX_VALUE (type
);
1907 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1908 if (real_less (&u
, &r
))
1917 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1919 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1923 /* A subroutine of fold_convert_const handling conversions of a
1924 FIXED_CST to an integer type. */
1927 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1930 double_int temp
, temp_trunc
;
1933 /* Right shift FIXED_CST to temp by fbit. */
1934 temp
= TREE_FIXED_CST (arg1
).data
;
1935 mode
= TREE_FIXED_CST (arg1
).mode
;
1936 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1938 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1939 HOST_BITS_PER_DOUBLE_INT
,
1940 SIGNED_FIXED_POINT_MODE_P (mode
));
1942 /* Left shift temp to temp_trunc by fbit. */
1943 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1944 HOST_BITS_PER_DOUBLE_INT
,
1945 SIGNED_FIXED_POINT_MODE_P (mode
));
1949 temp
= double_int_zero
;
1950 temp_trunc
= double_int_zero
;
1953 /* If FIXED_CST is negative, we need to round the value toward 0.
1954 By checking if the fractional bits are not zero to add 1 to temp. */
1955 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1956 && temp_trunc
.is_negative ()
1957 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1958 temp
+= double_int_one
;
1960 /* Given a fixed-point constant, make new constant with new type,
1961 appropriately sign-extended or truncated. */
1962 t
= force_fit_type (type
, temp
, -1,
1963 (temp
.is_negative ()
1964 && (TYPE_UNSIGNED (type
)
1965 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1966 | TREE_OVERFLOW (arg1
));
1971 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1972 to another floating point type. */
1975 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1977 REAL_VALUE_TYPE value
;
1980 /* Don't perform the operation if flag_signaling_nans is on
1981 and the operand is a signaling NaN. */
1982 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
1983 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
1986 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1987 t
= build_real (type
, value
);
1989 /* If converting an infinity or NAN to a representation that doesn't
1990 have one, set the overflow bit so that we can produce some kind of
1991 error message at the appropriate point if necessary. It's not the
1992 most user-friendly message, but it's better than nothing. */
1993 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
1994 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
1995 TREE_OVERFLOW (t
) = 1;
1996 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
1997 && !MODE_HAS_NANS (TYPE_MODE (type
)))
1998 TREE_OVERFLOW (t
) = 1;
1999 /* Regular overflow, conversion produced an infinity in a mode that
2000 can't represent them. */
2001 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2002 && REAL_VALUE_ISINF (value
)
2003 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2004 TREE_OVERFLOW (t
) = 1;
2006 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2010 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2011 to a floating point type. */
2014 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2016 REAL_VALUE_TYPE value
;
2019 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
2020 t
= build_real (type
, value
);
2022 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2026 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2027 to another fixed-point type. */
2030 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2032 FIXED_VALUE_TYPE value
;
2036 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2037 TYPE_SATURATING (type
));
2038 t
= build_fixed (type
, value
);
2040 /* Propagate overflow flags. */
2041 if (overflow_p
| TREE_OVERFLOW (arg1
))
2042 TREE_OVERFLOW (t
) = 1;
2046 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2047 to a fixed-point type. */
2050 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2052 FIXED_VALUE_TYPE value
;
2057 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2059 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2060 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2061 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? (HOST_WIDE_INT
) -1 : 0;
2063 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2065 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2066 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2067 TYPE_SATURATING (type
));
2068 t
= build_fixed (type
, value
);
2070 /* Propagate overflow flags. */
2071 if (overflow_p
| TREE_OVERFLOW (arg1
))
2072 TREE_OVERFLOW (t
) = 1;
2076 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2077 to a fixed-point type. */
2080 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2082 FIXED_VALUE_TYPE value
;
2086 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2087 &TREE_REAL_CST (arg1
),
2088 TYPE_SATURATING (type
));
2089 t
= build_fixed (type
, value
);
2091 /* Propagate overflow flags. */
2092 if (overflow_p
| TREE_OVERFLOW (arg1
))
2093 TREE_OVERFLOW (t
) = 1;
2097 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2098 type TYPE. If no simplification can be done return NULL_TREE. */
2101 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2103 if (TREE_TYPE (arg1
) == type
)
2106 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2107 || TREE_CODE (type
) == OFFSET_TYPE
)
2109 if (TREE_CODE (arg1
) == INTEGER_CST
)
2110 return fold_convert_const_int_from_int (type
, arg1
);
2111 else if (TREE_CODE (arg1
) == REAL_CST
)
2112 return fold_convert_const_int_from_real (code
, type
, arg1
);
2113 else if (TREE_CODE (arg1
) == FIXED_CST
)
2114 return fold_convert_const_int_from_fixed (type
, arg1
);
2116 else if (TREE_CODE (type
) == REAL_TYPE
)
2118 if (TREE_CODE (arg1
) == INTEGER_CST
)
2119 return build_real_from_int_cst (type
, arg1
);
2120 else if (TREE_CODE (arg1
) == REAL_CST
)
2121 return fold_convert_const_real_from_real (type
, arg1
);
2122 else if (TREE_CODE (arg1
) == FIXED_CST
)
2123 return fold_convert_const_real_from_fixed (type
, arg1
);
2125 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2127 if (TREE_CODE (arg1
) == FIXED_CST
)
2128 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2129 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2130 return fold_convert_const_fixed_from_int (type
, arg1
);
2131 else if (TREE_CODE (arg1
) == REAL_CST
)
2132 return fold_convert_const_fixed_from_real (type
, arg1
);
2134 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2136 if (TREE_CODE (arg1
) == VECTOR_CST
2137 && TYPE_VECTOR_SUBPARTS (type
) == VECTOR_CST_NELTS (arg1
))
2139 int len
= TYPE_VECTOR_SUBPARTS (type
);
2140 tree elttype
= TREE_TYPE (type
);
2141 tree
*v
= XALLOCAVEC (tree
, len
);
2142 for (int i
= 0; i
< len
; ++i
)
2144 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2145 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2146 if (cvt
== NULL_TREE
)
2150 return build_vector (type
, v
);
2156 /* Construct a vector of zero elements of vector type TYPE. */
2159 build_zero_vector (tree type
)
2163 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2164 return build_vector_from_val (type
, t
);
2167 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2170 fold_convertible_p (const_tree type
, const_tree arg
)
2172 tree orig
= TREE_TYPE (arg
);
2177 if (TREE_CODE (arg
) == ERROR_MARK
2178 || TREE_CODE (type
) == ERROR_MARK
2179 || TREE_CODE (orig
) == ERROR_MARK
)
2182 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2185 switch (TREE_CODE (type
))
2187 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2188 case POINTER_TYPE
: case REFERENCE_TYPE
:
2190 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2191 || TREE_CODE (orig
) == OFFSET_TYPE
);
2194 case FIXED_POINT_TYPE
:
2198 return TREE_CODE (type
) == TREE_CODE (orig
);
2205 /* Convert expression ARG to type TYPE. Used by the middle-end for
2206 simple conversions in preference to calling the front-end's convert. */
2209 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2211 tree orig
= TREE_TYPE (arg
);
2217 if (TREE_CODE (arg
) == ERROR_MARK
2218 || TREE_CODE (type
) == ERROR_MARK
2219 || TREE_CODE (orig
) == ERROR_MARK
)
2220 return error_mark_node
;
2222 switch (TREE_CODE (type
))
2225 case REFERENCE_TYPE
:
2226 /* Handle conversions between pointers to different address spaces. */
2227 if (POINTER_TYPE_P (orig
)
2228 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2229 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2230 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2233 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2235 if (TREE_CODE (arg
) == INTEGER_CST
)
2237 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2238 if (tem
!= NULL_TREE
)
2241 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2242 || TREE_CODE (orig
) == OFFSET_TYPE
)
2243 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2244 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2245 return fold_convert_loc (loc
, type
,
2246 fold_build1_loc (loc
, REALPART_EXPR
,
2247 TREE_TYPE (orig
), arg
));
2248 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2249 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2250 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2253 if (TREE_CODE (arg
) == INTEGER_CST
)
2255 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2256 if (tem
!= NULL_TREE
)
2259 else if (TREE_CODE (arg
) == REAL_CST
)
2261 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2262 if (tem
!= NULL_TREE
)
2265 else if (TREE_CODE (arg
) == FIXED_CST
)
2267 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2268 if (tem
!= NULL_TREE
)
2272 switch (TREE_CODE (orig
))
2275 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2276 case POINTER_TYPE
: case REFERENCE_TYPE
:
2277 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2280 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2282 case FIXED_POINT_TYPE
:
2283 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2286 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2287 return fold_convert_loc (loc
, type
, tem
);
2293 case FIXED_POINT_TYPE
:
2294 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2295 || TREE_CODE (arg
) == REAL_CST
)
2297 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2298 if (tem
!= NULL_TREE
)
2299 goto fold_convert_exit
;
2302 switch (TREE_CODE (orig
))
2304 case FIXED_POINT_TYPE
:
2309 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2312 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2313 return fold_convert_loc (loc
, type
, tem
);
2320 switch (TREE_CODE (orig
))
2323 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2324 case POINTER_TYPE
: case REFERENCE_TYPE
:
2326 case FIXED_POINT_TYPE
:
2327 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2328 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2329 fold_convert_loc (loc
, TREE_TYPE (type
),
2330 integer_zero_node
));
2335 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2337 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2338 TREE_OPERAND (arg
, 0));
2339 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2340 TREE_OPERAND (arg
, 1));
2341 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2344 arg
= save_expr (arg
);
2345 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2346 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2347 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2348 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2349 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2357 if (integer_zerop (arg
))
2358 return build_zero_vector (type
);
2359 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2360 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2361 || TREE_CODE (orig
) == VECTOR_TYPE
);
2362 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2365 tem
= fold_ignored_result (arg
);
2366 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2369 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2370 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2374 protected_set_expr_location_unshare (tem
, loc
);
2378 /* Return false if expr can be assumed not to be an lvalue, true
2382 maybe_lvalue_p (const_tree x
)
2384 /* We only need to wrap lvalue tree codes. */
2385 switch (TREE_CODE (x
))
2398 case ARRAY_RANGE_REF
:
2404 case PREINCREMENT_EXPR
:
2405 case PREDECREMENT_EXPR
:
2407 case TRY_CATCH_EXPR
:
2408 case WITH_CLEANUP_EXPR
:
2417 /* Assume the worst for front-end tree codes. */
2418 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2426 /* Return an expr equal to X but certainly not valid as an lvalue. */
2429 non_lvalue_loc (location_t loc
, tree x
)
2431 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2436 if (! maybe_lvalue_p (x
))
2438 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2441 /* When pedantic, return an expr equal to X but certainly not valid as a
2442 pedantic lvalue. Otherwise, return X. */
2445 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2447 return protected_set_expr_location_unshare (x
, loc
);
2450 /* Given a tree comparison code, return the code that is the logical inverse.
2451 It is generally not safe to do this for floating-point comparisons, except
2452 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2453 ERROR_MARK in this case. */
2456 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2458 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2459 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2469 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2471 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2473 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2475 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2489 return UNORDERED_EXPR
;
2490 case UNORDERED_EXPR
:
2491 return ORDERED_EXPR
;
2497 /* Similar, but return the comparison that results if the operands are
2498 swapped. This is safe for floating-point. */
2501 swap_tree_comparison (enum tree_code code
)
2508 case UNORDERED_EXPR
:
2534 /* Convert a comparison tree code from an enum tree_code representation
2535 into a compcode bit-based encoding. This function is the inverse of
2536 compcode_to_comparison. */
2538 static enum comparison_code
2539 comparison_to_compcode (enum tree_code code
)
2556 return COMPCODE_ORD
;
2557 case UNORDERED_EXPR
:
2558 return COMPCODE_UNORD
;
2560 return COMPCODE_UNLT
;
2562 return COMPCODE_UNEQ
;
2564 return COMPCODE_UNLE
;
2566 return COMPCODE_UNGT
;
2568 return COMPCODE_LTGT
;
2570 return COMPCODE_UNGE
;
2576 /* Convert a compcode bit-based encoding of a comparison operator back
2577 to GCC's enum tree_code representation. This function is the
2578 inverse of comparison_to_compcode. */
2580 static enum tree_code
2581 compcode_to_comparison (enum comparison_code code
)
2598 return ORDERED_EXPR
;
2599 case COMPCODE_UNORD
:
2600 return UNORDERED_EXPR
;
2618 /* Return a tree for the comparison which is the combination of
2619 doing the AND or OR (depending on CODE) of the two operations LCODE
2620 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2621 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2622 if this makes the transformation invalid. */
2625 combine_comparisons (location_t loc
,
2626 enum tree_code code
, enum tree_code lcode
,
2627 enum tree_code rcode
, tree truth_type
,
2628 tree ll_arg
, tree lr_arg
)
2630 bool honor_nans
= HONOR_NANS (ll_arg
);
2631 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2632 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2637 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2638 compcode
= lcompcode
& rcompcode
;
2641 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2642 compcode
= lcompcode
| rcompcode
;
2651 /* Eliminate unordered comparisons, as well as LTGT and ORD
2652 which are not used unless the mode has NaNs. */
2653 compcode
&= ~COMPCODE_UNORD
;
2654 if (compcode
== COMPCODE_LTGT
)
2655 compcode
= COMPCODE_NE
;
2656 else if (compcode
== COMPCODE_ORD
)
2657 compcode
= COMPCODE_TRUE
;
2659 else if (flag_trapping_math
)
2661 /* Check that the original operation and the optimized ones will trap
2662 under the same condition. */
2663 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2664 && (lcompcode
!= COMPCODE_EQ
)
2665 && (lcompcode
!= COMPCODE_ORD
);
2666 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2667 && (rcompcode
!= COMPCODE_EQ
)
2668 && (rcompcode
!= COMPCODE_ORD
);
2669 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2670 && (compcode
!= COMPCODE_EQ
)
2671 && (compcode
!= COMPCODE_ORD
);
2673 /* In a short-circuited boolean expression the LHS might be
2674 such that the RHS, if evaluated, will never trap. For
2675 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2676 if neither x nor y is NaN. (This is a mixed blessing: for
2677 example, the expression above will never trap, hence
2678 optimizing it to x < y would be invalid). */
2679 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2680 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2683 /* If the comparison was short-circuited, and only the RHS
2684 trapped, we may now generate a spurious trap. */
2686 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2689 /* If we changed the conditions that cause a trap, we lose. */
2690 if ((ltrap
|| rtrap
) != trap
)
2694 if (compcode
== COMPCODE_TRUE
)
2695 return constant_boolean_node (true, truth_type
);
2696 else if (compcode
== COMPCODE_FALSE
)
2697 return constant_boolean_node (false, truth_type
);
2700 enum tree_code tcode
;
2702 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2703 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2707 /* Return nonzero if two operands (typically of the same tree node)
2708 are necessarily equal. FLAGS modifies behavior as follows:
2710 If OEP_ONLY_CONST is set, only return nonzero for constants.
2711 This function tests whether the operands are indistinguishable;
2712 it does not test whether they are equal using C's == operation.
2713 The distinction is important for IEEE floating point, because
2714 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2715 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2717 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2718 even though it may hold multiple values during a function.
2719 This is because a GCC tree node guarantees that nothing else is
2720 executed between the evaluation of its "operands" (which may often
2721 be evaluated in arbitrary order). Hence if the operands themselves
2722 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2723 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2724 unset means assuming isochronic (or instantaneous) tree equivalence.
2725 Unless comparing arbitrary expression trees, such as from different
2726 statements, this flag can usually be left unset.
2728 If OEP_PURE_SAME is set, then pure functions with identical arguments
2729 are considered the same. It is used when the caller has other ways
2730 to ensure that global memory is unchanged in between.
2732 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2733 not values of expressions.
2735 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2736 any operand with side effect. This is unnecesarily conservative in the
2737 case we know that arg0 and arg1 are in disjoint code paths (such as in
2738 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2739 addresses with TREE_CONSTANT flag set so we know that &var == &var
2740 even if var is volatile. */
2743 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2745 /* When checking, verify at the outermost operand_equal_p call that
2746 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2748 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
2750 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
2754 inchash::hash
hstate0 (0), hstate1 (0);
2755 inchash::add_expr (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
2756 inchash::add_expr (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
2757 hashval_t h0
= hstate0
.end ();
2758 hashval_t h1
= hstate1
.end ();
2759 gcc_assert (h0
== h1
);
2767 /* If either is ERROR_MARK, they aren't equal. */
2768 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2769 || TREE_TYPE (arg0
) == error_mark_node
2770 || TREE_TYPE (arg1
) == error_mark_node
)
2773 /* Similar, if either does not have a type (like a released SSA name),
2774 they aren't equal. */
2775 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2778 /* We cannot consider pointers to different address space equal. */
2779 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2780 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2781 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2782 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2785 /* Check equality of integer constants before bailing out due to
2786 precision differences. */
2787 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2789 /* Address of INTEGER_CST is not defined; check that we did not forget
2790 to drop the OEP_ADDRESS_OF flags. */
2791 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2792 return tree_int_cst_equal (arg0
, arg1
);
2795 if (!(flags
& OEP_ADDRESS_OF
))
2797 /* If both types don't have the same signedness, then we can't consider
2798 them equal. We must check this before the STRIP_NOPS calls
2799 because they may change the signedness of the arguments. As pointers
2800 strictly don't have a signedness, require either two pointers or
2801 two non-pointers as well. */
2802 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2803 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2804 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2807 /* If both types don't have the same precision, then it is not safe
2809 if (element_precision (TREE_TYPE (arg0
))
2810 != element_precision (TREE_TYPE (arg1
)))
2817 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2818 sanity check once the issue is solved. */
2820 /* Addresses of conversions and SSA_NAMEs (and many other things)
2821 are not defined. Check that we did not forget to drop the
2822 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2823 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2824 && TREE_CODE (arg0
) != SSA_NAME
);
2827 /* In case both args are comparisons but with different comparison
2828 code, try to swap the comparison operands of one arg to produce
2829 a match and compare that variant. */
2830 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2831 && COMPARISON_CLASS_P (arg0
)
2832 && COMPARISON_CLASS_P (arg1
))
2834 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2836 if (TREE_CODE (arg0
) == swap_code
)
2837 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2838 TREE_OPERAND (arg1
, 1), flags
)
2839 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2840 TREE_OPERAND (arg1
, 0), flags
);
2843 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2845 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2846 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2848 else if (flags
& OEP_ADDRESS_OF
)
2850 /* If we are interested in comparing addresses ignore
2851 MEM_REF wrappings of the base that can appear just for
2853 if (TREE_CODE (arg0
) == MEM_REF
2855 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2856 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2857 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2859 else if (TREE_CODE (arg1
) == MEM_REF
2861 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2862 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2863 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2871 /* When not checking adddresses, this is needed for conversions and for
2872 COMPONENT_REF. Might as well play it safe and always test this. */
2873 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2874 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2875 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2876 && !(flags
& OEP_ADDRESS_OF
)))
2879 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2880 We don't care about side effects in that case because the SAVE_EXPR
2881 takes care of that for us. In all other cases, two expressions are
2882 equal if they have no side effects. If we have two identical
2883 expressions with side effects that should be treated the same due
2884 to the only side effects being identical SAVE_EXPR's, that will
2885 be detected in the recursive calls below.
2886 If we are taking an invariant address of two identical objects
2887 they are necessarily equal as well. */
2888 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2889 && (TREE_CODE (arg0
) == SAVE_EXPR
2890 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
2891 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2894 /* Next handle constant cases, those for which we can return 1 even
2895 if ONLY_CONST is set. */
2896 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2897 switch (TREE_CODE (arg0
))
2900 return tree_int_cst_equal (arg0
, arg1
);
2903 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2904 TREE_FIXED_CST (arg1
));
2907 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2911 if (!HONOR_SIGNED_ZEROS (arg0
))
2913 /* If we do not distinguish between signed and unsigned zero,
2914 consider them equal. */
2915 if (real_zerop (arg0
) && real_zerop (arg1
))
2924 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2927 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2929 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2930 VECTOR_CST_ELT (arg1
, i
), flags
))
2937 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2939 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2943 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2944 && ! memcmp (TREE_STRING_POINTER (arg0
),
2945 TREE_STRING_POINTER (arg1
),
2946 TREE_STRING_LENGTH (arg0
)));
2949 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2950 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2951 flags
| OEP_ADDRESS_OF
2952 | OEP_MATCH_SIDE_EFFECTS
);
2954 /* In GIMPLE empty constructors are allowed in initializers of
2956 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0
))
2957 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1
)));
2962 if (flags
& OEP_ONLY_CONST
)
2965 /* Define macros to test an operand from arg0 and arg1 for equality and a
2966 variant that allows null and views null as being different from any
2967 non-null value. In the latter case, if either is null, the both
2968 must be; otherwise, do the normal comparison. */
2969 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2970 TREE_OPERAND (arg1, N), flags)
2972 #define OP_SAME_WITH_NULL(N) \
2973 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2974 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2976 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2979 /* Two conversions are equal only if signedness and modes match. */
2980 switch (TREE_CODE (arg0
))
2983 case FIX_TRUNC_EXPR
:
2984 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2985 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
2995 case tcc_comparison
:
2997 if (OP_SAME (0) && OP_SAME (1))
3000 /* For commutative ops, allow the other order. */
3001 return (commutative_tree_code (TREE_CODE (arg0
))
3002 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3003 TREE_OPERAND (arg1
, 1), flags
)
3004 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3005 TREE_OPERAND (arg1
, 0), flags
));
3008 /* If either of the pointer (or reference) expressions we are
3009 dereferencing contain a side effect, these cannot be equal,
3010 but their addresses can be. */
3011 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3012 && (TREE_SIDE_EFFECTS (arg0
)
3013 || TREE_SIDE_EFFECTS (arg1
)))
3016 switch (TREE_CODE (arg0
))
3019 if (!(flags
& OEP_ADDRESS_OF
)
3020 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3021 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3023 flags
&= ~OEP_ADDRESS_OF
;
3027 /* Require the same offset. */
3028 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3029 TYPE_SIZE (TREE_TYPE (arg1
)),
3030 flags
& ~OEP_ADDRESS_OF
))
3035 case VIEW_CONVERT_EXPR
:
3038 case TARGET_MEM_REF
:
3040 if (!(flags
& OEP_ADDRESS_OF
))
3042 /* Require equal access sizes */
3043 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3044 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3045 || !TYPE_SIZE (TREE_TYPE (arg1
))
3046 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3047 TYPE_SIZE (TREE_TYPE (arg1
)),
3050 /* Verify that access happens in similar types. */
3051 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3053 /* Verify that accesses are TBAA compatible. */
3054 if (!alias_ptr_types_compatible_p
3055 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3056 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3057 || (MR_DEPENDENCE_CLIQUE (arg0
)
3058 != MR_DEPENDENCE_CLIQUE (arg1
))
3059 || (MR_DEPENDENCE_BASE (arg0
)
3060 != MR_DEPENDENCE_BASE (arg1
)))
3062 /* Verify that alignment is compatible. */
3063 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3064 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3067 flags
&= ~OEP_ADDRESS_OF
;
3068 return (OP_SAME (0) && OP_SAME (1)
3069 /* TARGET_MEM_REF require equal extra operands. */
3070 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3071 || (OP_SAME_WITH_NULL (2)
3072 && OP_SAME_WITH_NULL (3)
3073 && OP_SAME_WITH_NULL (4))));
3076 case ARRAY_RANGE_REF
:
3079 flags
&= ~OEP_ADDRESS_OF
;
3080 /* Compare the array index by value if it is constant first as we
3081 may have different types but same value here. */
3082 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3083 TREE_OPERAND (arg1
, 1))
3085 && OP_SAME_WITH_NULL (2)
3086 && OP_SAME_WITH_NULL (3)
3087 /* Compare low bound and element size as with OEP_ADDRESS_OF
3088 we have to account for the offset of the ref. */
3089 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3090 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3091 || (operand_equal_p (array_ref_low_bound
3092 (CONST_CAST_TREE (arg0
)),
3094 (CONST_CAST_TREE (arg1
)), flags
)
3095 && operand_equal_p (array_ref_element_size
3096 (CONST_CAST_TREE (arg0
)),
3097 array_ref_element_size
3098 (CONST_CAST_TREE (arg1
)),
3102 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3103 may be NULL when we're called to compare MEM_EXPRs. */
3104 if (!OP_SAME_WITH_NULL (0)
3107 flags
&= ~OEP_ADDRESS_OF
;
3108 return OP_SAME_WITH_NULL (2);
3113 flags
&= ~OEP_ADDRESS_OF
;
3114 return OP_SAME (1) && OP_SAME (2);
3120 case tcc_expression
:
3121 switch (TREE_CODE (arg0
))
3124 /* Be sure we pass right ADDRESS_OF flag. */
3125 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3126 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3127 TREE_OPERAND (arg1
, 0),
3128 flags
| OEP_ADDRESS_OF
);
3130 case TRUTH_NOT_EXPR
:
3133 case TRUTH_ANDIF_EXPR
:
3134 case TRUTH_ORIF_EXPR
:
3135 return OP_SAME (0) && OP_SAME (1);
3138 case WIDEN_MULT_PLUS_EXPR
:
3139 case WIDEN_MULT_MINUS_EXPR
:
3142 /* The multiplcation operands are commutative. */
3145 case TRUTH_AND_EXPR
:
3147 case TRUTH_XOR_EXPR
:
3148 if (OP_SAME (0) && OP_SAME (1))
3151 /* Otherwise take into account this is a commutative operation. */
3152 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3153 TREE_OPERAND (arg1
, 1), flags
)
3154 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3155 TREE_OPERAND (arg1
, 0), flags
));
3158 if (! OP_SAME (1) || ! OP_SAME (2))
3160 flags
&= ~OEP_ADDRESS_OF
;
3165 case BIT_INSERT_EXPR
:
3166 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3173 switch (TREE_CODE (arg0
))
3176 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3177 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3178 /* If not both CALL_EXPRs are either internal or normal function
3179 functions, then they are not equal. */
3181 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3183 /* If the CALL_EXPRs call different internal functions, then they
3185 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3190 /* If the CALL_EXPRs call different functions, then they are not
3192 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3197 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3199 unsigned int cef
= call_expr_flags (arg0
);
3200 if (flags
& OEP_PURE_SAME
)
3201 cef
&= ECF_CONST
| ECF_PURE
;
3208 /* Now see if all the arguments are the same. */
3210 const_call_expr_arg_iterator iter0
, iter1
;
3212 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3213 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3215 a0
= next_const_call_expr_arg (&iter0
),
3216 a1
= next_const_call_expr_arg (&iter1
))
3217 if (! operand_equal_p (a0
, a1
, flags
))
3220 /* If we get here and both argument lists are exhausted
3221 then the CALL_EXPRs are equal. */
3222 return ! (a0
|| a1
);
3228 case tcc_declaration
:
3229 /* Consider __builtin_sqrt equal to sqrt. */
3230 return (TREE_CODE (arg0
) == FUNCTION_DECL
3231 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3232 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3233 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3235 case tcc_exceptional
:
3236 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3238 /* In GIMPLE constructors are used only to build vectors from
3239 elements. Individual elements in the constructor must be
3240 indexed in increasing order and form an initial sequence.
3242 We make no effort to compare constructors in generic.
3243 (see sem_variable::equals in ipa-icf which can do so for
3245 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3246 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3249 /* Be sure that vectors constructed have the same representation.
3250 We only tested element precision and modes to match.
3251 Vectors may be BLKmode and thus also check that the number of
3253 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3254 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3257 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3258 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3259 unsigned int len
= vec_safe_length (v0
);
3261 if (len
!= vec_safe_length (v1
))
3264 for (unsigned int i
= 0; i
< len
; i
++)
3266 constructor_elt
*c0
= &(*v0
)[i
];
3267 constructor_elt
*c1
= &(*v1
)[i
];
3269 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3270 /* In GIMPLE the indexes can be either NULL or matching i.
3271 Double check this so we won't get false
3272 positives for GENERIC. */
3274 && (TREE_CODE (c0
->index
) != INTEGER_CST
3275 || !compare_tree_int (c0
->index
, i
)))
3277 && (TREE_CODE (c1
->index
) != INTEGER_CST
3278 || !compare_tree_int (c1
->index
, i
))))
3290 #undef OP_SAME_WITH_NULL
3293 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3294 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3296 When in doubt, return 0. */
3299 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3301 int unsignedp1
, unsignedpo
;
3302 tree primarg0
, primarg1
, primother
;
3303 unsigned int correct_width
;
3305 if (operand_equal_p (arg0
, arg1
, 0))
3308 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3309 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3312 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3313 and see if the inner values are the same. This removes any
3314 signedness comparison, which doesn't matter here. */
3315 primarg0
= arg0
, primarg1
= arg1
;
3316 STRIP_NOPS (primarg0
);
3317 STRIP_NOPS (primarg1
);
3318 if (operand_equal_p (primarg0
, primarg1
, 0))
3321 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3322 actual comparison operand, ARG0.
3324 First throw away any conversions to wider types
3325 already present in the operands. */
3327 primarg1
= get_narrower (arg1
, &unsignedp1
);
3328 primother
= get_narrower (other
, &unsignedpo
);
3330 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3331 if (unsignedp1
== unsignedpo
3332 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3333 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3335 tree type
= TREE_TYPE (arg0
);
3337 /* Make sure shorter operand is extended the right way
3338 to match the longer operand. */
3339 primarg1
= fold_convert (signed_or_unsigned_type_for
3340 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3342 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3349 /* See if ARG is an expression that is either a comparison or is performing
3350 arithmetic on comparisons. The comparisons must only be comparing
3351 two different values, which will be stored in *CVAL1 and *CVAL2; if
3352 they are nonzero it means that some operands have already been found.
3353 No variables may be used anywhere else in the expression except in the
3354 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3355 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3357 If this is true, return 1. Otherwise, return zero. */
3360 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3362 enum tree_code code
= TREE_CODE (arg
);
3363 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3365 /* We can handle some of the tcc_expression cases here. */
3366 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3368 else if (tclass
== tcc_expression
3369 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3370 || code
== COMPOUND_EXPR
))
3371 tclass
= tcc_binary
;
3373 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3374 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3376 /* If we've already found a CVAL1 or CVAL2, this expression is
3377 two complex to handle. */
3378 if (*cval1
|| *cval2
)
3388 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3391 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3392 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3393 cval1
, cval2
, save_p
));
3398 case tcc_expression
:
3399 if (code
== COND_EXPR
)
3400 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3401 cval1
, cval2
, save_p
)
3402 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3403 cval1
, cval2
, save_p
)
3404 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3405 cval1
, cval2
, save_p
));
3408 case tcc_comparison
:
3409 /* First see if we can handle the first operand, then the second. For
3410 the second operand, we know *CVAL1 can't be zero. It must be that
3411 one side of the comparison is each of the values; test for the
3412 case where this isn't true by failing if the two operands
3415 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3416 TREE_OPERAND (arg
, 1), 0))
3420 *cval1
= TREE_OPERAND (arg
, 0);
3421 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3423 else if (*cval2
== 0)
3424 *cval2
= TREE_OPERAND (arg
, 0);
3425 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3430 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3432 else if (*cval2
== 0)
3433 *cval2
= TREE_OPERAND (arg
, 1);
3434 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3446 /* ARG is a tree that is known to contain just arithmetic operations and
3447 comparisons. Evaluate the operations in the tree substituting NEW0 for
3448 any occurrence of OLD0 as an operand of a comparison and likewise for
3452 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3453 tree old1
, tree new1
)
3455 tree type
= TREE_TYPE (arg
);
3456 enum tree_code code
= TREE_CODE (arg
);
3457 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3459 /* We can handle some of the tcc_expression cases here. */
3460 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3462 else if (tclass
== tcc_expression
3463 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3464 tclass
= tcc_binary
;
3469 return fold_build1_loc (loc
, code
, type
,
3470 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3471 old0
, new0
, old1
, new1
));
3474 return fold_build2_loc (loc
, code
, type
,
3475 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3476 old0
, new0
, old1
, new1
),
3477 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3478 old0
, new0
, old1
, new1
));
3480 case tcc_expression
:
3484 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3488 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3492 return fold_build3_loc (loc
, code
, type
,
3493 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3494 old0
, new0
, old1
, new1
),
3495 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3496 old0
, new0
, old1
, new1
),
3497 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3498 old0
, new0
, old1
, new1
));
3502 /* Fall through - ??? */
3504 case tcc_comparison
:
3506 tree arg0
= TREE_OPERAND (arg
, 0);
3507 tree arg1
= TREE_OPERAND (arg
, 1);
3509 /* We need to check both for exact equality and tree equality. The
3510 former will be true if the operand has a side-effect. In that
3511 case, we know the operand occurred exactly once. */
3513 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3515 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3518 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3520 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3523 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3531 /* Return a tree for the case when the result of an expression is RESULT
3532 converted to TYPE and OMITTED was previously an operand of the expression
3533 but is now not needed (e.g., we folded OMITTED * 0).
3535 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3536 the conversion of RESULT to TYPE. */
3539 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3541 tree t
= fold_convert_loc (loc
, type
, result
);
3543 /* If the resulting operand is an empty statement, just return the omitted
3544 statement casted to void. */
3545 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3546 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3547 fold_ignored_result (omitted
));
3549 if (TREE_SIDE_EFFECTS (omitted
))
3550 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3551 fold_ignored_result (omitted
), t
);
3553 return non_lvalue_loc (loc
, t
);
3556 /* Return a tree for the case when the result of an expression is RESULT
3557 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3558 of the expression but are now not needed.
3560 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3561 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3562 evaluated before OMITTED2. Otherwise, if neither has side effects,
3563 just do the conversion of RESULT to TYPE. */
3566 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3567 tree omitted1
, tree omitted2
)
3569 tree t
= fold_convert_loc (loc
, type
, result
);
3571 if (TREE_SIDE_EFFECTS (omitted2
))
3572 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3573 if (TREE_SIDE_EFFECTS (omitted1
))
3574 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3576 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3580 /* Return a simplified tree node for the truth-negation of ARG. This
3581 never alters ARG itself. We assume that ARG is an operation that
3582 returns a truth value (0 or 1).
3584 FIXME: one would think we would fold the result, but it causes
3585 problems with the dominator optimizer. */
3588 fold_truth_not_expr (location_t loc
, tree arg
)
3590 tree type
= TREE_TYPE (arg
);
3591 enum tree_code code
= TREE_CODE (arg
);
3592 location_t loc1
, loc2
;
3594 /* If this is a comparison, we can simply invert it, except for
3595 floating-point non-equality comparisons, in which case we just
3596 enclose a TRUTH_NOT_EXPR around what we have. */
3598 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3600 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3601 if (FLOAT_TYPE_P (op_type
)
3602 && flag_trapping_math
3603 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3604 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3607 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3608 if (code
== ERROR_MARK
)
3611 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3612 TREE_OPERAND (arg
, 1));
3613 if (TREE_NO_WARNING (arg
))
3614 TREE_NO_WARNING (ret
) = 1;
3621 return constant_boolean_node (integer_zerop (arg
), type
);
3623 case TRUTH_AND_EXPR
:
3624 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3625 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3626 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3627 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3628 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3631 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3632 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3633 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3634 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3635 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3637 case TRUTH_XOR_EXPR
:
3638 /* Here we can invert either operand. We invert the first operand
3639 unless the second operand is a TRUTH_NOT_EXPR in which case our
3640 result is the XOR of the first operand with the inside of the
3641 negation of the second operand. */
3643 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3644 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3645 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3647 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3648 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3649 TREE_OPERAND (arg
, 1));
3651 case TRUTH_ANDIF_EXPR
:
3652 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3653 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3654 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3655 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3656 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3658 case TRUTH_ORIF_EXPR
:
3659 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3660 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3661 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3662 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3663 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3665 case TRUTH_NOT_EXPR
:
3666 return TREE_OPERAND (arg
, 0);
3670 tree arg1
= TREE_OPERAND (arg
, 1);
3671 tree arg2
= TREE_OPERAND (arg
, 2);
3673 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3674 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3676 /* A COND_EXPR may have a throw as one operand, which
3677 then has void type. Just leave void operands
3679 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3680 VOID_TYPE_P (TREE_TYPE (arg1
))
3681 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3682 VOID_TYPE_P (TREE_TYPE (arg2
))
3683 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3687 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3688 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3689 TREE_OPERAND (arg
, 0),
3690 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3692 case NON_LVALUE_EXPR
:
3693 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3694 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3697 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3698 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3700 /* ... fall through ... */
3703 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3704 return build1_loc (loc
, TREE_CODE (arg
), type
,
3705 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3708 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3710 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3713 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3715 case CLEANUP_POINT_EXPR
:
3716 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3717 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3718 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3725 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3726 assume that ARG is an operation that returns a truth value (0 or 1
3727 for scalars, 0 or -1 for vectors). Return the folded expression if
3728 folding is successful. Otherwise, return NULL_TREE. */
3731 fold_invert_truthvalue (location_t loc
, tree arg
)
3733 tree type
= TREE_TYPE (arg
);
3734 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3740 /* Return a simplified tree node for the truth-negation of ARG. This
3741 never alters ARG itself. We assume that ARG is an operation that
3742 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3745 invert_truthvalue_loc (location_t loc
, tree arg
)
3747 if (TREE_CODE (arg
) == ERROR_MARK
)
3750 tree type
= TREE_TYPE (arg
);
3751 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3757 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3758 with code CODE. This optimization is unsafe. */
3760 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3761 tree arg0
, tree arg1
)
3763 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3764 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3766 /* (A / C) +- (B / C) -> (A +- B) / C. */
3768 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3769 TREE_OPERAND (arg1
, 1), 0))
3770 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3771 fold_build2_loc (loc
, code
, type
,
3772 TREE_OPERAND (arg0
, 0),
3773 TREE_OPERAND (arg1
, 0)),
3774 TREE_OPERAND (arg0
, 1));
3776 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3777 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3778 TREE_OPERAND (arg1
, 0), 0)
3779 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3780 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3782 REAL_VALUE_TYPE r0
, r1
;
3783 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3784 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3786 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3788 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3789 real_arithmetic (&r0
, code
, &r0
, &r1
);
3790 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3791 TREE_OPERAND (arg0
, 0),
3792 build_real (type
, r0
));
3798 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3799 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3800 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3801 is the original memory reference used to preserve the alias set of
3805 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
3806 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
3807 int unsignedp
, int reversep
)
3809 tree result
, bftype
;
3811 if (get_alias_set (inner
) != get_alias_set (orig_inner
))
3812 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
3813 build_fold_addr_expr (inner
),
3815 (reference_alias_ptr_type (orig_inner
), 0));
3817 if (bitpos
== 0 && !reversep
)
3819 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3820 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3821 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3822 && tree_fits_shwi_p (size
)
3823 && tree_to_shwi (size
) == bitsize
)
3824 return fold_convert_loc (loc
, type
, inner
);
3828 if (TYPE_PRECISION (bftype
) != bitsize
3829 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3830 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3832 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3833 size_int (bitsize
), bitsize_int (bitpos
));
3834 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
3837 result
= fold_convert_loc (loc
, type
, result
);
3842 /* Optimize a bit-field compare.
3844 There are two cases: First is a compare against a constant and the
3845 second is a comparison of two items where the fields are at the same
3846 bit position relative to the start of a chunk (byte, halfword, word)
3847 large enough to contain it. In these cases we can avoid the shift
3848 implicit in bitfield extractions.
3850 For constants, we emit a compare of the shifted constant with the
3851 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3852 compared. For two fields at the same position, we do the ANDs with the
3853 similar mask and compare the result of the ANDs.
3855 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3856 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3857 are the left and right operands of the comparison, respectively.
3859 If the optimization described above can be done, we return the resulting
3860 tree. Otherwise we return zero. */
3863 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3864 tree compare_type
, tree lhs
, tree rhs
)
3866 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3867 tree type
= TREE_TYPE (lhs
);
3869 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3870 machine_mode lmode
, rmode
, nmode
;
3871 int lunsignedp
, runsignedp
;
3872 int lreversep
, rreversep
;
3873 int lvolatilep
= 0, rvolatilep
= 0;
3874 tree linner
, rinner
= NULL_TREE
;
3878 /* Get all the information about the extractions being done. If the bit size
3879 if the same as the size of the underlying object, we aren't doing an
3880 extraction at all and so can do nothing. We also don't want to
3881 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3882 then will no longer be able to replace it. */
3883 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3884 &lunsignedp
, &lreversep
, &lvolatilep
, false);
3885 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3886 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3890 rreversep
= lreversep
;
3893 /* If this is not a constant, we can only do something if bit positions,
3894 sizes, signedness and storage order are the same. */
3896 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3897 &runsignedp
, &rreversep
, &rvolatilep
, false);
3899 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3900 || lunsignedp
!= runsignedp
|| lreversep
!= rreversep
|| offset
!= 0
3901 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3905 /* Honor the C++ memory model and mimic what RTL expansion does. */
3906 unsigned HOST_WIDE_INT bitstart
= 0;
3907 unsigned HOST_WIDE_INT bitend
= 0;
3908 if (TREE_CODE (lhs
) == COMPONENT_REF
)
3910 get_bit_range (&bitstart
, &bitend
, lhs
, &lbitpos
, &offset
);
3911 if (offset
!= NULL_TREE
)
3915 /* See if we can find a mode to refer to this field. We should be able to,
3916 but fail if we can't. */
3917 nmode
= get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
3918 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3919 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3920 TYPE_ALIGN (TREE_TYPE (rinner
))),
3922 if (nmode
== VOIDmode
)
3925 /* Set signed and unsigned types of the precision of this mode for the
3927 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3929 /* Compute the bit position and size for the new reference and our offset
3930 within it. If the new reference is the same size as the original, we
3931 won't optimize anything, so return zero. */
3932 nbitsize
= GET_MODE_BITSIZE (nmode
);
3933 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3935 if (nbitsize
== lbitsize
)
3938 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
3939 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3941 /* Make the mask to be used against the extracted field. */
3942 mask
= build_int_cst_type (unsigned_type
, -1);
3943 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3944 mask
= const_binop (RSHIFT_EXPR
, mask
,
3945 size_int (nbitsize
- lbitsize
- lbitpos
));
3948 /* If not comparing with constant, just rework the comparison
3950 return fold_build2_loc (loc
, code
, compare_type
,
3951 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3952 make_bit_field_ref (loc
, linner
, lhs
,
3957 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3958 make_bit_field_ref (loc
, rinner
, rhs
,
3964 /* Otherwise, we are handling the constant case. See if the constant is too
3965 big for the field. Warn and return a tree for 0 (false) if so. We do
3966 this not only for its own sake, but to avoid having to test for this
3967 error case below. If we didn't, we might generate wrong code.
3969 For unsigned fields, the constant shifted right by the field length should
3970 be all zero. For signed fields, the high-order bits should agree with
3975 if (wi::lrshift (rhs
, lbitsize
) != 0)
3977 warning (0, "comparison is always %d due to width of bit-field",
3979 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3984 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
3985 if (tem
!= 0 && tem
!= -1)
3987 warning (0, "comparison is always %d due to width of bit-field",
3989 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3993 /* Single-bit compares should always be against zero. */
3994 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3996 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3997 rhs
= build_int_cst (type
, 0);
4000 /* Make a new bitfield reference, shift the constant over the
4001 appropriate number of bits and mask it with the computed mask
4002 (in case this was a signed field). If we changed it, make a new one. */
4003 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4004 nbitsize
, nbitpos
, 1, lreversep
);
4006 rhs
= const_binop (BIT_AND_EXPR
,
4007 const_binop (LSHIFT_EXPR
,
4008 fold_convert_loc (loc
, unsigned_type
, rhs
),
4009 size_int (lbitpos
)),
4012 lhs
= build2_loc (loc
, code
, compare_type
,
4013 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4017 /* Subroutine for fold_truth_andor_1: decode a field reference.
4019 If EXP is a comparison reference, we return the innermost reference.
4021 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4022 set to the starting bit number.
4024 If the innermost field can be completely contained in a mode-sized
4025 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4027 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4028 otherwise it is not changed.
4030 *PUNSIGNEDP is set to the signedness of the field.
4032 *PREVERSEP is set to the storage order of the field.
4034 *PMASK is set to the mask used. This is either contained in a
4035 BIT_AND_EXPR or derived from the width of the field.
4037 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4039 Return 0 if this is not a component reference or is one that we can't
4040 do anything with. */
4043 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4044 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4045 int *punsignedp
, int *preversep
, int *pvolatilep
,
4046 tree
*pmask
, tree
*pand_mask
)
4049 tree outer_type
= 0;
4051 tree mask
, inner
, offset
;
4053 unsigned int precision
;
4055 /* All the optimizations using this function assume integer fields.
4056 There are problems with FP fields since the type_for_size call
4057 below can fail for, e.g., XFmode. */
4058 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4061 /* We are interested in the bare arrangement of bits, so strip everything
4062 that doesn't affect the machine mode. However, record the type of the
4063 outermost expression if it may matter below. */
4064 if (CONVERT_EXPR_P (exp
)
4065 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4066 outer_type
= TREE_TYPE (exp
);
4069 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4071 and_mask
= TREE_OPERAND (exp
, 1);
4072 exp
= TREE_OPERAND (exp
, 0);
4073 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4074 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4078 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
4079 punsignedp
, preversep
, pvolatilep
, false);
4080 if ((inner
== exp
&& and_mask
== 0)
4081 || *pbitsize
< 0 || offset
!= 0
4082 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
4087 /* If the number of bits in the reference is the same as the bitsize of
4088 the outer type, then the outer type gives the signedness. Otherwise
4089 (in case of a small bitfield) the signedness is unchanged. */
4090 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4091 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4093 /* Compute the mask to access the bitfield. */
4094 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4095 precision
= TYPE_PRECISION (unsigned_type
);
4097 mask
= build_int_cst_type (unsigned_type
, -1);
4099 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4100 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4102 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4104 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4105 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4108 *pand_mask
= and_mask
;
4112 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4113 bit positions and MASK is SIGNED. */
4116 all_ones_mask_p (const_tree mask
, unsigned int size
)
4118 tree type
= TREE_TYPE (mask
);
4119 unsigned int precision
= TYPE_PRECISION (type
);
4121 /* If this function returns true when the type of the mask is
4122 UNSIGNED, then there will be errors. In particular see
4123 gcc.c-torture/execute/990326-1.c. There does not appear to be
4124 any documentation paper trail as to why this is so. But the pre
4125 wide-int worked with that restriction and it has been preserved
4127 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4130 return wi::mask (size
, false, precision
) == mask
;
4133 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4134 represents the sign bit of EXP's type. If EXP represents a sign
4135 or zero extension, also test VAL against the unextended type.
4136 The return value is the (sub)expression whose sign bit is VAL,
4137 or NULL_TREE otherwise. */
4140 sign_bit_p (tree exp
, const_tree val
)
4145 /* Tree EXP must have an integral type. */
4146 t
= TREE_TYPE (exp
);
4147 if (! INTEGRAL_TYPE_P (t
))
4150 /* Tree VAL must be an integer constant. */
4151 if (TREE_CODE (val
) != INTEGER_CST
4152 || TREE_OVERFLOW (val
))
4155 width
= TYPE_PRECISION (t
);
4156 if (wi::only_sign_bit_p (val
, width
))
4159 /* Handle extension from a narrower type. */
4160 if (TREE_CODE (exp
) == NOP_EXPR
4161 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4162 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4167 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4168 to be evaluated unconditionally. */
4171 simple_operand_p (const_tree exp
)
4173 /* Strip any conversions that don't change the machine mode. */
4176 return (CONSTANT_CLASS_P (exp
)
4177 || TREE_CODE (exp
) == SSA_NAME
4179 && ! TREE_ADDRESSABLE (exp
)
4180 && ! TREE_THIS_VOLATILE (exp
)
4181 && ! DECL_NONLOCAL (exp
)
4182 /* Don't regard global variables as simple. They may be
4183 allocated in ways unknown to the compiler (shared memory,
4184 #pragma weak, etc). */
4185 && ! TREE_PUBLIC (exp
)
4186 && ! DECL_EXTERNAL (exp
)
4187 /* Weakrefs are not safe to be read, since they can be NULL.
4188 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4189 have DECL_WEAK flag set. */
4190 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4191 /* Loading a static variable is unduly expensive, but global
4192 registers aren't expensive. */
4193 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4196 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4197 to be evaluated unconditionally.
4198 I addition to simple_operand_p, we assume that comparisons, conversions,
4199 and logic-not operations are simple, if their operands are simple, too. */
4202 simple_operand_p_2 (tree exp
)
4204 enum tree_code code
;
4206 if (TREE_SIDE_EFFECTS (exp
)
4207 || tree_could_trap_p (exp
))
4210 while (CONVERT_EXPR_P (exp
))
4211 exp
= TREE_OPERAND (exp
, 0);
4213 code
= TREE_CODE (exp
);
4215 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4216 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4217 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4219 if (code
== TRUTH_NOT_EXPR
)
4220 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4222 return simple_operand_p (exp
);
4226 /* The following functions are subroutines to fold_range_test and allow it to
4227 try to change a logical combination of comparisons into a range test.
4230 X == 2 || X == 3 || X == 4 || X == 5
4234 (unsigned) (X - 2) <= 3
4236 We describe each set of comparisons as being either inside or outside
4237 a range, using a variable named like IN_P, and then describe the
4238 range with a lower and upper bound. If one of the bounds is omitted,
4239 it represents either the highest or lowest value of the type.
4241 In the comments below, we represent a range by two numbers in brackets
4242 preceded by a "+" to designate being inside that range, or a "-" to
4243 designate being outside that range, so the condition can be inverted by
4244 flipping the prefix. An omitted bound is represented by a "-". For
4245 example, "- [-, 10]" means being outside the range starting at the lowest
4246 possible value and ending at 10, in other words, being greater than 10.
4247 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4250 We set up things so that the missing bounds are handled in a consistent
4251 manner so neither a missing bound nor "true" and "false" need to be
4252 handled using a special case. */
4254 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4255 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4256 and UPPER1_P are nonzero if the respective argument is an upper bound
4257 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4258 must be specified for a comparison. ARG1 will be converted to ARG0's
4259 type if both are specified. */
4262 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4263 tree arg1
, int upper1_p
)
4269 /* If neither arg represents infinity, do the normal operation.
4270 Else, if not a comparison, return infinity. Else handle the special
4271 comparison rules. Note that most of the cases below won't occur, but
4272 are handled for consistency. */
4274 if (arg0
!= 0 && arg1
!= 0)
4276 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4277 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4279 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4282 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4285 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4286 for neither. In real maths, we cannot assume open ended ranges are
4287 the same. But, this is computer arithmetic, where numbers are finite.
4288 We can therefore make the transformation of any unbounded range with
4289 the value Z, Z being greater than any representable number. This permits
4290 us to treat unbounded ranges as equal. */
4291 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4292 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4296 result
= sgn0
== sgn1
;
4299 result
= sgn0
!= sgn1
;
4302 result
= sgn0
< sgn1
;
4305 result
= sgn0
<= sgn1
;
4308 result
= sgn0
> sgn1
;
4311 result
= sgn0
>= sgn1
;
4317 return constant_boolean_node (result
, type
);
4320 /* Helper routine for make_range. Perform one step for it, return
4321 new expression if the loop should continue or NULL_TREE if it should
4325 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4326 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4327 bool *strict_overflow_p
)
4329 tree arg0_type
= TREE_TYPE (arg0
);
4330 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4331 int in_p
= *p_in_p
, n_in_p
;
4335 case TRUTH_NOT_EXPR
:
4336 /* We can only do something if the range is testing for zero. */
4337 if (low
== NULL_TREE
|| high
== NULL_TREE
4338 || ! integer_zerop (low
) || ! integer_zerop (high
))
4343 case EQ_EXPR
: case NE_EXPR
:
4344 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4345 /* We can only do something if the range is testing for zero
4346 and if the second operand is an integer constant. Note that
4347 saying something is "in" the range we make is done by
4348 complementing IN_P since it will set in the initial case of
4349 being not equal to zero; "out" is leaving it alone. */
4350 if (low
== NULL_TREE
|| high
== NULL_TREE
4351 || ! integer_zerop (low
) || ! integer_zerop (high
)
4352 || TREE_CODE (arg1
) != INTEGER_CST
)
4357 case NE_EXPR
: /* - [c, c] */
4360 case EQ_EXPR
: /* + [c, c] */
4361 in_p
= ! in_p
, low
= high
= arg1
;
4363 case GT_EXPR
: /* - [-, c] */
4364 low
= 0, high
= arg1
;
4366 case GE_EXPR
: /* + [c, -] */
4367 in_p
= ! in_p
, low
= arg1
, high
= 0;
4369 case LT_EXPR
: /* - [c, -] */
4370 low
= arg1
, high
= 0;
4372 case LE_EXPR
: /* + [-, c] */
4373 in_p
= ! in_p
, low
= 0, high
= arg1
;
4379 /* If this is an unsigned comparison, we also know that EXP is
4380 greater than or equal to zero. We base the range tests we make
4381 on that fact, so we record it here so we can parse existing
4382 range tests. We test arg0_type since often the return type
4383 of, e.g. EQ_EXPR, is boolean. */
4384 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4386 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4388 build_int_cst (arg0_type
, 0),
4392 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4394 /* If the high bound is missing, but we have a nonzero low
4395 bound, reverse the range so it goes from zero to the low bound
4397 if (high
== 0 && low
&& ! integer_zerop (low
))
4400 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4401 build_int_cst (TREE_TYPE (low
), 1), 0);
4402 low
= build_int_cst (arg0_type
, 0);
4412 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4413 low and high are non-NULL, then normalize will DTRT. */
4414 if (!TYPE_UNSIGNED (arg0_type
)
4415 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4417 if (low
== NULL_TREE
)
4418 low
= TYPE_MIN_VALUE (arg0_type
);
4419 if (high
== NULL_TREE
)
4420 high
= TYPE_MAX_VALUE (arg0_type
);
4423 /* (-x) IN [a,b] -> x in [-b, -a] */
4424 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4425 build_int_cst (exp_type
, 0),
4427 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4428 build_int_cst (exp_type
, 0),
4430 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4436 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4437 build_int_cst (exp_type
, 1));
4441 if (TREE_CODE (arg1
) != INTEGER_CST
)
4444 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4445 move a constant to the other side. */
4446 if (!TYPE_UNSIGNED (arg0_type
)
4447 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4450 /* If EXP is signed, any overflow in the computation is undefined,
4451 so we don't worry about it so long as our computations on
4452 the bounds don't overflow. For unsigned, overflow is defined
4453 and this is exactly the right thing. */
4454 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4455 arg0_type
, low
, 0, arg1
, 0);
4456 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4457 arg0_type
, high
, 1, arg1
, 0);
4458 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4459 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4462 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4463 *strict_overflow_p
= true;
4466 /* Check for an unsigned range which has wrapped around the maximum
4467 value thus making n_high < n_low, and normalize it. */
4468 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4470 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4471 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4472 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4473 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4475 /* If the range is of the form +/- [ x+1, x ], we won't
4476 be able to normalize it. But then, it represents the
4477 whole range or the empty set, so make it
4479 if (tree_int_cst_equal (n_low
, low
)
4480 && tree_int_cst_equal (n_high
, high
))
4486 low
= n_low
, high
= n_high
;
4494 case NON_LVALUE_EXPR
:
4495 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4498 if (! INTEGRAL_TYPE_P (arg0_type
)
4499 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4500 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4503 n_low
= low
, n_high
= high
;
4506 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4509 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4511 /* If we're converting arg0 from an unsigned type, to exp,
4512 a signed type, we will be doing the comparison as unsigned.
4513 The tests above have already verified that LOW and HIGH
4516 So we have to ensure that we will handle large unsigned
4517 values the same way that the current signed bounds treat
4520 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4524 /* For fixed-point modes, we need to pass the saturating flag
4525 as the 2nd parameter. */
4526 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4528 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4529 TYPE_SATURATING (arg0_type
));
4532 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4534 /* A range without an upper bound is, naturally, unbounded.
4535 Since convert would have cropped a very large value, use
4536 the max value for the destination type. */
4538 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4539 : TYPE_MAX_VALUE (arg0_type
);
4541 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4542 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4543 fold_convert_loc (loc
, arg0_type
,
4545 build_int_cst (arg0_type
, 1));
4547 /* If the low bound is specified, "and" the range with the
4548 range for which the original unsigned value will be
4552 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4553 1, fold_convert_loc (loc
, arg0_type
,
4558 in_p
= (n_in_p
== in_p
);
4562 /* Otherwise, "or" the range with the range of the input
4563 that will be interpreted as negative. */
4564 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4565 1, fold_convert_loc (loc
, arg0_type
,
4570 in_p
= (in_p
!= n_in_p
);
4584 /* Given EXP, a logical expression, set the range it is testing into
4585 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4586 actually being tested. *PLOW and *PHIGH will be made of the same
4587 type as the returned expression. If EXP is not a comparison, we
4588 will most likely not be returning a useful value and range. Set
4589 *STRICT_OVERFLOW_P to true if the return value is only valid
4590 because signed overflow is undefined; otherwise, do not change
4591 *STRICT_OVERFLOW_P. */
4594 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4595 bool *strict_overflow_p
)
4597 enum tree_code code
;
4598 tree arg0
, arg1
= NULL_TREE
;
4599 tree exp_type
, nexp
;
4602 location_t loc
= EXPR_LOCATION (exp
);
4604 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4605 and see if we can refine the range. Some of the cases below may not
4606 happen, but it doesn't seem worth worrying about this. We "continue"
4607 the outer loop when we've changed something; otherwise we "break"
4608 the switch, which will "break" the while. */
4611 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4615 code
= TREE_CODE (exp
);
4616 exp_type
= TREE_TYPE (exp
);
4619 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4621 if (TREE_OPERAND_LENGTH (exp
) > 0)
4622 arg0
= TREE_OPERAND (exp
, 0);
4623 if (TREE_CODE_CLASS (code
) == tcc_binary
4624 || TREE_CODE_CLASS (code
) == tcc_comparison
4625 || (TREE_CODE_CLASS (code
) == tcc_expression
4626 && TREE_OPERAND_LENGTH (exp
) > 1))
4627 arg1
= TREE_OPERAND (exp
, 1);
4629 if (arg0
== NULL_TREE
)
4632 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4633 &high
, &in_p
, strict_overflow_p
);
4634 if (nexp
== NULL_TREE
)
4639 /* If EXP is a constant, we can evaluate whether this is true or false. */
4640 if (TREE_CODE (exp
) == INTEGER_CST
)
4642 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4644 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4650 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4654 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4655 type, TYPE, return an expression to test if EXP is in (or out of, depending
4656 on IN_P) the range. Return 0 if the test couldn't be created. */
4659 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4660 tree low
, tree high
)
4662 tree etype
= TREE_TYPE (exp
), value
;
4664 /* Disable this optimization for function pointer expressions
4665 on targets that require function pointer canonicalization. */
4666 if (targetm
.have_canonicalize_funcptr_for_compare ()
4667 && TREE_CODE (etype
) == POINTER_TYPE
4668 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4673 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4675 return invert_truthvalue_loc (loc
, value
);
4680 if (low
== 0 && high
== 0)
4681 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4684 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4685 fold_convert_loc (loc
, etype
, high
));
4688 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4689 fold_convert_loc (loc
, etype
, low
));
4691 if (operand_equal_p (low
, high
, 0))
4692 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4693 fold_convert_loc (loc
, etype
, low
));
4695 if (integer_zerop (low
))
4697 if (! TYPE_UNSIGNED (etype
))
4699 etype
= unsigned_type_for (etype
);
4700 high
= fold_convert_loc (loc
, etype
, high
);
4701 exp
= fold_convert_loc (loc
, etype
, exp
);
4703 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4706 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4707 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4709 int prec
= TYPE_PRECISION (etype
);
4711 if (wi::mask (prec
- 1, false, prec
) == high
)
4713 if (TYPE_UNSIGNED (etype
))
4715 tree signed_etype
= signed_type_for (etype
);
4716 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4718 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4720 etype
= signed_etype
;
4721 exp
= fold_convert_loc (loc
, etype
, exp
);
4723 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4724 build_int_cst (etype
, 0));
4728 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4729 This requires wrap-around arithmetics for the type of the expression.
4730 First make sure that arithmetics in this type is valid, then make sure
4731 that it wraps around. */
4732 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4733 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4734 TYPE_UNSIGNED (etype
));
4736 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4738 tree utype
, minv
, maxv
;
4740 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4741 for the type in question, as we rely on this here. */
4742 utype
= unsigned_type_for (etype
);
4743 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4744 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4745 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4746 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4748 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4755 high
= fold_convert_loc (loc
, etype
, high
);
4756 low
= fold_convert_loc (loc
, etype
, low
);
4757 exp
= fold_convert_loc (loc
, etype
, exp
);
4759 value
= const_binop (MINUS_EXPR
, high
, low
);
4762 if (POINTER_TYPE_P (etype
))
4764 if (value
!= 0 && !TREE_OVERFLOW (value
))
4766 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4767 return build_range_check (loc
, type
,
4768 fold_build_pointer_plus_loc (loc
, exp
, low
),
4769 1, build_int_cst (etype
, 0), value
);
4774 if (value
!= 0 && !TREE_OVERFLOW (value
))
4775 return build_range_check (loc
, type
,
4776 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4777 1, build_int_cst (etype
, 0), value
);
4782 /* Return the predecessor of VAL in its type, handling the infinite case. */
4785 range_predecessor (tree val
)
4787 tree type
= TREE_TYPE (val
);
4789 if (INTEGRAL_TYPE_P (type
)
4790 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4793 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4794 build_int_cst (TREE_TYPE (val
), 1), 0);
4797 /* Return the successor of VAL in its type, handling the infinite case. */
4800 range_successor (tree val
)
4802 tree type
= TREE_TYPE (val
);
4804 if (INTEGRAL_TYPE_P (type
)
4805 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4808 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4809 build_int_cst (TREE_TYPE (val
), 1), 0);
4812 /* Given two ranges, see if we can merge them into one. Return 1 if we
4813 can, 0 if we can't. Set the output range into the specified parameters. */
4816 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4817 tree high0
, int in1_p
, tree low1
, tree high1
)
4825 int lowequal
= ((low0
== 0 && low1
== 0)
4826 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4827 low0
, 0, low1
, 0)));
4828 int highequal
= ((high0
== 0 && high1
== 0)
4829 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4830 high0
, 1, high1
, 1)));
4832 /* Make range 0 be the range that starts first, or ends last if they
4833 start at the same value. Swap them if it isn't. */
4834 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4837 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4838 high1
, 1, high0
, 1))))
4840 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4841 tem
= low0
, low0
= low1
, low1
= tem
;
4842 tem
= high0
, high0
= high1
, high1
= tem
;
4845 /* Now flag two cases, whether the ranges are disjoint or whether the
4846 second range is totally subsumed in the first. Note that the tests
4847 below are simplified by the ones above. */
4848 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4849 high0
, 1, low1
, 0));
4850 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4851 high1
, 1, high0
, 1));
4853 /* We now have four cases, depending on whether we are including or
4854 excluding the two ranges. */
4857 /* If they don't overlap, the result is false. If the second range
4858 is a subset it is the result. Otherwise, the range is from the start
4859 of the second to the end of the first. */
4861 in_p
= 0, low
= high
= 0;
4863 in_p
= 1, low
= low1
, high
= high1
;
4865 in_p
= 1, low
= low1
, high
= high0
;
4868 else if (in0_p
&& ! in1_p
)
4870 /* If they don't overlap, the result is the first range. If they are
4871 equal, the result is false. If the second range is a subset of the
4872 first, and the ranges begin at the same place, we go from just after
4873 the end of the second range to the end of the first. If the second
4874 range is not a subset of the first, or if it is a subset and both
4875 ranges end at the same place, the range starts at the start of the
4876 first range and ends just before the second range.
4877 Otherwise, we can't describe this as a single range. */
4879 in_p
= 1, low
= low0
, high
= high0
;
4880 else if (lowequal
&& highequal
)
4881 in_p
= 0, low
= high
= 0;
4882 else if (subset
&& lowequal
)
4884 low
= range_successor (high1
);
4889 /* We are in the weird situation where high0 > high1 but
4890 high1 has no successor. Punt. */
4894 else if (! subset
|| highequal
)
4897 high
= range_predecessor (low1
);
4901 /* low0 < low1 but low1 has no predecessor. Punt. */
4909 else if (! in0_p
&& in1_p
)
4911 /* If they don't overlap, the result is the second range. If the second
4912 is a subset of the first, the result is false. Otherwise,
4913 the range starts just after the first range and ends at the
4914 end of the second. */
4916 in_p
= 1, low
= low1
, high
= high1
;
4917 else if (subset
|| highequal
)
4918 in_p
= 0, low
= high
= 0;
4921 low
= range_successor (high0
);
4926 /* high1 > high0 but high0 has no successor. Punt. */
4934 /* The case where we are excluding both ranges. Here the complex case
4935 is if they don't overlap. In that case, the only time we have a
4936 range is if they are adjacent. If the second is a subset of the
4937 first, the result is the first. Otherwise, the range to exclude
4938 starts at the beginning of the first range and ends at the end of the
4942 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4943 range_successor (high0
),
4945 in_p
= 0, low
= low0
, high
= high1
;
4948 /* Canonicalize - [min, x] into - [-, x]. */
4949 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
4950 switch (TREE_CODE (TREE_TYPE (low0
)))
4953 if (TYPE_PRECISION (TREE_TYPE (low0
))
4954 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
4958 if (tree_int_cst_equal (low0
,
4959 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
4963 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
4964 && integer_zerop (low0
))
4971 /* Canonicalize - [x, max] into - [x, -]. */
4972 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
4973 switch (TREE_CODE (TREE_TYPE (high1
)))
4976 if (TYPE_PRECISION (TREE_TYPE (high1
))
4977 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
4981 if (tree_int_cst_equal (high1
,
4982 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
4986 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
4987 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
4989 build_int_cst (TREE_TYPE (high1
), 1),
4997 /* The ranges might be also adjacent between the maximum and
4998 minimum values of the given type. For
4999 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5000 return + [x + 1, y - 1]. */
5001 if (low0
== 0 && high1
== 0)
5003 low
= range_successor (high0
);
5004 high
= range_predecessor (low1
);
5005 if (low
== 0 || high
== 0)
5015 in_p
= 0, low
= low0
, high
= high0
;
5017 in_p
= 0, low
= low0
, high
= high1
;
5020 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5025 /* Subroutine of fold, looking inside expressions of the form
5026 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5027 of the COND_EXPR. This function is being used also to optimize
5028 A op B ? C : A, by reversing the comparison first.
5030 Return a folded expression whose code is not a COND_EXPR
5031 anymore, or NULL_TREE if no folding opportunity is found. */
5034 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5035 tree arg0
, tree arg1
, tree arg2
)
5037 enum tree_code comp_code
= TREE_CODE (arg0
);
5038 tree arg00
= TREE_OPERAND (arg0
, 0);
5039 tree arg01
= TREE_OPERAND (arg0
, 1);
5040 tree arg1_type
= TREE_TYPE (arg1
);
5046 /* If we have A op 0 ? A : -A, consider applying the following
5049 A == 0? A : -A same as -A
5050 A != 0? A : -A same as A
5051 A >= 0? A : -A same as abs (A)
5052 A > 0? A : -A same as abs (A)
5053 A <= 0? A : -A same as -abs (A)
5054 A < 0? A : -A same as -abs (A)
5056 None of these transformations work for modes with signed
5057 zeros. If A is +/-0, the first two transformations will
5058 change the sign of the result (from +0 to -0, or vice
5059 versa). The last four will fix the sign of the result,
5060 even though the original expressions could be positive or
5061 negative, depending on the sign of A.
5063 Note that all these transformations are correct if A is
5064 NaN, since the two alternatives (A and -A) are also NaNs. */
5065 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5066 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5067 ? real_zerop (arg01
)
5068 : integer_zerop (arg01
))
5069 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5070 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5071 /* In the case that A is of the form X-Y, '-A' (arg2) may
5072 have already been folded to Y-X, check for that. */
5073 || (TREE_CODE (arg1
) == MINUS_EXPR
5074 && TREE_CODE (arg2
) == MINUS_EXPR
5075 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5076 TREE_OPERAND (arg2
, 1), 0)
5077 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5078 TREE_OPERAND (arg2
, 0), 0))))
5083 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5084 return pedantic_non_lvalue_loc (loc
,
5085 fold_convert_loc (loc
, type
,
5086 negate_expr (tem
)));
5089 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5092 if (flag_trapping_math
)
5097 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5099 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5100 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5103 if (flag_trapping_math
)
5107 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5109 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5110 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5112 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5116 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5117 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5118 both transformations are correct when A is NaN: A != 0
5119 is then true, and A == 0 is false. */
5121 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5122 && integer_zerop (arg01
) && integer_zerop (arg2
))
5124 if (comp_code
== NE_EXPR
)
5125 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5126 else if (comp_code
== EQ_EXPR
)
5127 return build_zero_cst (type
);
5130 /* Try some transformations of A op B ? A : B.
5132 A == B? A : B same as B
5133 A != B? A : B same as A
5134 A >= B? A : B same as max (A, B)
5135 A > B? A : B same as max (B, A)
5136 A <= B? A : B same as min (A, B)
5137 A < B? A : B same as min (B, A)
5139 As above, these transformations don't work in the presence
5140 of signed zeros. For example, if A and B are zeros of
5141 opposite sign, the first two transformations will change
5142 the sign of the result. In the last four, the original
5143 expressions give different results for (A=+0, B=-0) and
5144 (A=-0, B=+0), but the transformed expressions do not.
5146 The first two transformations are correct if either A or B
5147 is a NaN. In the first transformation, the condition will
5148 be false, and B will indeed be chosen. In the case of the
5149 second transformation, the condition A != B will be true,
5150 and A will be chosen.
5152 The conversions to max() and min() are not correct if B is
5153 a number and A is not. The conditions in the original
5154 expressions will be false, so all four give B. The min()
5155 and max() versions would give a NaN instead. */
5156 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5157 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
5158 /* Avoid these transformations if the COND_EXPR may be used
5159 as an lvalue in the C++ front-end. PR c++/19199. */
5161 || VECTOR_TYPE_P (type
)
5162 || (! lang_GNU_CXX ()
5163 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5164 || ! maybe_lvalue_p (arg1
)
5165 || ! maybe_lvalue_p (arg2
)))
5167 tree comp_op0
= arg00
;
5168 tree comp_op1
= arg01
;
5169 tree comp_type
= TREE_TYPE (comp_op0
);
5171 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5172 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
5182 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg2
));
5184 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5189 /* In C++ a ?: expression can be an lvalue, so put the
5190 operand which will be used if they are equal first
5191 so that we can convert this back to the
5192 corresponding COND_EXPR. */
5193 if (!HONOR_NANS (arg1
))
5195 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5196 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5197 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5198 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5199 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5200 comp_op1
, comp_op0
);
5201 return pedantic_non_lvalue_loc (loc
,
5202 fold_convert_loc (loc
, type
, tem
));
5209 if (!HONOR_NANS (arg1
))
5211 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5212 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5213 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5214 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5215 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5216 comp_op1
, comp_op0
);
5217 return pedantic_non_lvalue_loc (loc
,
5218 fold_convert_loc (loc
, type
, tem
));
5222 if (!HONOR_NANS (arg1
))
5223 return pedantic_non_lvalue_loc (loc
,
5224 fold_convert_loc (loc
, type
, arg2
));
5227 if (!HONOR_NANS (arg1
))
5228 return pedantic_non_lvalue_loc (loc
,
5229 fold_convert_loc (loc
, type
, arg1
));
5232 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5237 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5238 we might still be able to simplify this. For example,
5239 if C1 is one less or one more than C2, this might have started
5240 out as a MIN or MAX and been transformed by this function.
5241 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5243 if (INTEGRAL_TYPE_P (type
)
5244 && TREE_CODE (arg01
) == INTEGER_CST
5245 && TREE_CODE (arg2
) == INTEGER_CST
)
5249 if (TREE_CODE (arg1
) == INTEGER_CST
)
5251 /* We can replace A with C1 in this case. */
5252 arg1
= fold_convert_loc (loc
, type
, arg01
);
5253 return fold_build3_loc (loc
, COND_EXPR
, type
, arg0
, arg1
, arg2
);
5256 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5257 MIN_EXPR, to preserve the signedness of the comparison. */
5258 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5260 && operand_equal_p (arg01
,
5261 const_binop (PLUS_EXPR
, arg2
,
5262 build_int_cst (type
, 1)),
5265 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5266 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5268 return pedantic_non_lvalue_loc (loc
,
5269 fold_convert_loc (loc
, type
, tem
));
5274 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5276 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5278 && operand_equal_p (arg01
,
5279 const_binop (MINUS_EXPR
, arg2
,
5280 build_int_cst (type
, 1)),
5283 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5284 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5286 return pedantic_non_lvalue_loc (loc
,
5287 fold_convert_loc (loc
, type
, tem
));
5292 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5293 MAX_EXPR, to preserve the signedness of the comparison. */
5294 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5296 && operand_equal_p (arg01
,
5297 const_binop (MINUS_EXPR
, arg2
,
5298 build_int_cst (type
, 1)),
5301 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5302 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5304 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5309 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5310 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5312 && operand_equal_p (arg01
,
5313 const_binop (PLUS_EXPR
, arg2
,
5314 build_int_cst (type
, 1)),
5317 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5318 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5320 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5334 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5335 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5336 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5340 /* EXP is some logical combination of boolean tests. See if we can
5341 merge it into some range test. Return the new tree if so. */
5344 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5347 int or_op
= (code
== TRUTH_ORIF_EXPR
5348 || code
== TRUTH_OR_EXPR
);
5349 int in0_p
, in1_p
, in_p
;
5350 tree low0
, low1
, low
, high0
, high1
, high
;
5351 bool strict_overflow_p
= false;
5353 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5354 "when simplifying range test");
5356 if (!INTEGRAL_TYPE_P (type
))
5359 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5360 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5362 /* If this is an OR operation, invert both sides; we will invert
5363 again at the end. */
5365 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5367 /* If both expressions are the same, if we can merge the ranges, and we
5368 can build the range test, return it or it inverted. If one of the
5369 ranges is always true or always false, consider it to be the same
5370 expression as the other. */
5371 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5372 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5374 && 0 != (tem
= (build_range_check (loc
, type
,
5376 : rhs
!= 0 ? rhs
: integer_zero_node
,
5379 if (strict_overflow_p
)
5380 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5381 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5384 /* On machines where the branch cost is expensive, if this is a
5385 short-circuited branch and the underlying object on both sides
5386 is the same, make a non-short-circuit operation. */
5387 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5388 && lhs
!= 0 && rhs
!= 0
5389 && (code
== TRUTH_ANDIF_EXPR
5390 || code
== TRUTH_ORIF_EXPR
)
5391 && operand_equal_p (lhs
, rhs
, 0))
5393 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5394 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5395 which cases we can't do this. */
5396 if (simple_operand_p (lhs
))
5397 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5398 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5401 else if (!lang_hooks
.decls
.global_bindings_p ()
5402 && !CONTAINS_PLACEHOLDER_P (lhs
))
5404 tree common
= save_expr (lhs
);
5406 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5407 or_op
? ! in0_p
: in0_p
,
5409 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5410 or_op
? ! in1_p
: in1_p
,
5413 if (strict_overflow_p
)
5414 fold_overflow_warning (warnmsg
,
5415 WARN_STRICT_OVERFLOW_COMPARISON
);
5416 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5417 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5426 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5427 bit value. Arrange things so the extra bits will be set to zero if and
5428 only if C is signed-extended to its full width. If MASK is nonzero,
5429 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5432 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5434 tree type
= TREE_TYPE (c
);
5435 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5438 if (p
== modesize
|| unsignedp
)
5441 /* We work by getting just the sign bit into the low-order bit, then
5442 into the high-order bit, then sign-extend. We then XOR that value
5444 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5446 /* We must use a signed type in order to get an arithmetic right shift.
5447 However, we must also avoid introducing accidental overflows, so that
5448 a subsequent call to integer_zerop will work. Hence we must
5449 do the type conversion here. At this point, the constant is either
5450 zero or one, and the conversion to a signed type can never overflow.
5451 We could get an overflow if this conversion is done anywhere else. */
5452 if (TYPE_UNSIGNED (type
))
5453 temp
= fold_convert (signed_type_for (type
), temp
);
5455 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5456 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5458 temp
= const_binop (BIT_AND_EXPR
, temp
,
5459 fold_convert (TREE_TYPE (c
), mask
));
5460 /* If necessary, convert the type back to match the type of C. */
5461 if (TYPE_UNSIGNED (type
))
5462 temp
= fold_convert (type
, temp
);
5464 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5467 /* For an expression that has the form
5471 we can drop one of the inner expressions and simplify to
5475 LOC is the location of the resulting expression. OP is the inner
5476 logical operation; the left-hand side in the examples above, while CMPOP
5477 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5478 removing a condition that guards another, as in
5479 (A != NULL && A->...) || A == NULL
5480 which we must not transform. If RHS_ONLY is true, only eliminate the
5481 right-most operand of the inner logical operation. */
5484 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5487 tree type
= TREE_TYPE (cmpop
);
5488 enum tree_code code
= TREE_CODE (cmpop
);
5489 enum tree_code truthop_code
= TREE_CODE (op
);
5490 tree lhs
= TREE_OPERAND (op
, 0);
5491 tree rhs
= TREE_OPERAND (op
, 1);
5492 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5493 enum tree_code rhs_code
= TREE_CODE (rhs
);
5494 enum tree_code lhs_code
= TREE_CODE (lhs
);
5495 enum tree_code inv_code
;
5497 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5500 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5503 if (rhs_code
== truthop_code
)
5505 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5506 if (newrhs
!= NULL_TREE
)
5509 rhs_code
= TREE_CODE (rhs
);
5512 if (lhs_code
== truthop_code
&& !rhs_only
)
5514 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5515 if (newlhs
!= NULL_TREE
)
5518 lhs_code
= TREE_CODE (lhs
);
5522 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5523 if (inv_code
== rhs_code
5524 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5525 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5527 if (!rhs_only
&& inv_code
== lhs_code
5528 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5529 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5531 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5532 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5537 /* Find ways of folding logical expressions of LHS and RHS:
5538 Try to merge two comparisons to the same innermost item.
5539 Look for range tests like "ch >= '0' && ch <= '9'".
5540 Look for combinations of simple terms on machines with expensive branches
5541 and evaluate the RHS unconditionally.
5543 For example, if we have p->a == 2 && p->b == 4 and we can make an
5544 object large enough to span both A and B, we can do this with a comparison
5545 against the object ANDed with the a mask.
5547 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5548 operations to do this with one comparison.
5550 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5551 function and the one above.
5553 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5554 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5556 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5559 We return the simplified tree or 0 if no optimization is possible. */
5562 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5565 /* If this is the "or" of two comparisons, we can do something if
5566 the comparisons are NE_EXPR. If this is the "and", we can do something
5567 if the comparisons are EQ_EXPR. I.e.,
5568 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5570 WANTED_CODE is this operation code. For single bit fields, we can
5571 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5572 comparison for one-bit fields. */
5574 enum tree_code wanted_code
;
5575 enum tree_code lcode
, rcode
;
5576 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5577 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5578 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5579 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5580 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5581 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5582 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5583 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5584 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5585 machine_mode lnmode
, rnmode
;
5586 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5587 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5588 tree l_const
, r_const
;
5589 tree lntype
, rntype
, result
;
5590 HOST_WIDE_INT first_bit
, end_bit
;
5593 /* Start by getting the comparison codes. Fail if anything is volatile.
5594 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5595 it were surrounded with a NE_EXPR. */
5597 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5600 lcode
= TREE_CODE (lhs
);
5601 rcode
= TREE_CODE (rhs
);
5603 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5605 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5606 build_int_cst (TREE_TYPE (lhs
), 0));
5610 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5612 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5613 build_int_cst (TREE_TYPE (rhs
), 0));
5617 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5618 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5621 ll_arg
= TREE_OPERAND (lhs
, 0);
5622 lr_arg
= TREE_OPERAND (lhs
, 1);
5623 rl_arg
= TREE_OPERAND (rhs
, 0);
5624 rr_arg
= TREE_OPERAND (rhs
, 1);
5626 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5627 if (simple_operand_p (ll_arg
)
5628 && simple_operand_p (lr_arg
))
5630 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5631 && operand_equal_p (lr_arg
, rr_arg
, 0))
5633 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5634 truth_type
, ll_arg
, lr_arg
);
5638 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5639 && operand_equal_p (lr_arg
, rl_arg
, 0))
5641 result
= combine_comparisons (loc
, code
, lcode
,
5642 swap_tree_comparison (rcode
),
5643 truth_type
, ll_arg
, lr_arg
);
5649 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5650 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5652 /* If the RHS can be evaluated unconditionally and its operands are
5653 simple, it wins to evaluate the RHS unconditionally on machines
5654 with expensive branches. In this case, this isn't a comparison
5655 that can be merged. */
5657 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5659 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5660 && simple_operand_p (rl_arg
)
5661 && simple_operand_p (rr_arg
))
5663 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5664 if (code
== TRUTH_OR_EXPR
5665 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5666 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5667 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5668 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5669 return build2_loc (loc
, NE_EXPR
, truth_type
,
5670 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5672 build_int_cst (TREE_TYPE (ll_arg
), 0));
5674 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5675 if (code
== TRUTH_AND_EXPR
5676 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5677 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5678 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5679 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5680 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5681 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5683 build_int_cst (TREE_TYPE (ll_arg
), 0));
5686 /* See if the comparisons can be merged. Then get all the parameters for
5689 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5690 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5693 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5695 ll_inner
= decode_field_reference (loc
, &ll_arg
,
5696 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5697 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5698 &ll_mask
, &ll_and_mask
);
5699 lr_inner
= decode_field_reference (loc
, &lr_arg
,
5700 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5701 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5702 &lr_mask
, &lr_and_mask
);
5703 rl_inner
= decode_field_reference (loc
, &rl_arg
,
5704 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5705 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5706 &rl_mask
, &rl_and_mask
);
5707 rr_inner
= decode_field_reference (loc
, &rr_arg
,
5708 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5709 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5710 &rr_mask
, &rr_and_mask
);
5712 /* It must be true that the inner operation on the lhs of each
5713 comparison must be the same if we are to be able to do anything.
5714 Then see if we have constants. If not, the same must be true for
5717 || ll_reversep
!= rl_reversep
5718 || ll_inner
== 0 || rl_inner
== 0
5719 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5722 if (TREE_CODE (lr_arg
) == INTEGER_CST
5723 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5725 l_const
= lr_arg
, r_const
= rr_arg
;
5726 lr_reversep
= ll_reversep
;
5728 else if (lr_reversep
!= rr_reversep
5729 || lr_inner
== 0 || rr_inner
== 0
5730 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5733 l_const
= r_const
= 0;
5735 /* If either comparison code is not correct for our logical operation,
5736 fail. However, we can convert a one-bit comparison against zero into
5737 the opposite comparison against that bit being set in the field. */
5739 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5740 if (lcode
!= wanted_code
)
5742 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5744 /* Make the left operand unsigned, since we are only interested
5745 in the value of one bit. Otherwise we are doing the wrong
5754 /* This is analogous to the code for l_const above. */
5755 if (rcode
!= wanted_code
)
5757 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5766 /* See if we can find a mode that contains both fields being compared on
5767 the left. If we can't, fail. Otherwise, update all constants and masks
5768 to be relative to a field of that size. */
5769 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5770 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5771 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5772 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5774 if (lnmode
== VOIDmode
)
5777 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5778 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5779 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5780 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5782 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5784 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5785 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5788 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5789 size_int (xll_bitpos
));
5790 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5791 size_int (xrl_bitpos
));
5795 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5796 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5797 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5798 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5799 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5802 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5804 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5809 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5810 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5811 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5812 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5813 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5816 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5818 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5822 /* If the right sides are not constant, do the same for it. Also,
5823 disallow this optimization if a size or signedness mismatch occurs
5824 between the left and right sides. */
5827 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5828 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5829 /* Make sure the two fields on the right
5830 correspond to the left without being swapped. */
5831 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5834 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5835 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5836 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5837 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5839 if (rnmode
== VOIDmode
)
5842 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5843 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5844 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5845 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5847 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5849 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5850 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5853 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5855 size_int (xlr_bitpos
));
5856 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5858 size_int (xrr_bitpos
));
5860 /* Make a mask that corresponds to both fields being compared.
5861 Do this for both items being compared. If the operands are the
5862 same size and the bits being compared are in the same position
5863 then we can do this by masking both and comparing the masked
5865 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5866 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5867 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5869 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5870 lntype
, lnbitsize
, lnbitpos
,
5871 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5872 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5873 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5875 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
5876 rntype
, rnbitsize
, rnbitpos
,
5877 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
5878 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5879 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5881 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5884 /* There is still another way we can do something: If both pairs of
5885 fields being compared are adjacent, we may be able to make a wider
5886 field containing them both.
5888 Note that we still must mask the lhs/rhs expressions. Furthermore,
5889 the mask must be shifted to account for the shift done by
5890 make_bit_field_ref. */
5891 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5892 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5893 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5894 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5898 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
5899 ll_bitsize
+ rl_bitsize
,
5900 MIN (ll_bitpos
, rl_bitpos
),
5901 ll_unsignedp
, ll_reversep
);
5902 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
5903 lr_bitsize
+ rr_bitsize
,
5904 MIN (lr_bitpos
, rr_bitpos
),
5905 lr_unsignedp
, lr_reversep
);
5907 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5908 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5909 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5910 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5912 /* Convert to the smaller type before masking out unwanted bits. */
5914 if (lntype
!= rntype
)
5916 if (lnbitsize
> rnbitsize
)
5918 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5919 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5922 else if (lnbitsize
< rnbitsize
)
5924 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5925 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5930 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5931 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5933 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5934 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5936 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5942 /* Handle the case of comparisons with constants. If there is something in
5943 common between the masks, those bits of the constants must be the same.
5944 If not, the condition is always false. Test for this to avoid generating
5945 incorrect code below. */
5946 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5947 if (! integer_zerop (result
)
5948 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5949 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5951 if (wanted_code
== NE_EXPR
)
5953 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5954 return constant_boolean_node (true, truth_type
);
5958 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5959 return constant_boolean_node (false, truth_type
);
5963 /* Construct the expression we will return. First get the component
5964 reference we will make. Unless the mask is all ones the width of
5965 that field, perform the mask operation. Then compare with the
5967 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5968 lntype
, lnbitsize
, lnbitpos
,
5969 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5971 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5972 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5973 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5975 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5976 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5979 /* T is an integer expression that is being multiplied, divided, or taken a
5980 modulus (CODE says which and what kind of divide or modulus) by a
5981 constant C. See if we can eliminate that operation by folding it with
5982 other operations already in T. WIDE_TYPE, if non-null, is a type that
5983 should be used for the computation if wider than our type.
5985 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5986 (X * 2) + (Y * 4). We must, however, be assured that either the original
5987 expression would not overflow or that overflow is undefined for the type
5988 in the language in question.
5990 If we return a non-null expression, it is an equivalent form of the
5991 original computation, but need not be in the original type.
5993 We set *STRICT_OVERFLOW_P to true if the return values depends on
5994 signed overflow being undefined. Otherwise we do not change
5995 *STRICT_OVERFLOW_P. */
5998 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5999 bool *strict_overflow_p
)
6001 /* To avoid exponential search depth, refuse to allow recursion past
6002 three levels. Beyond that (1) it's highly unlikely that we'll find
6003 something interesting and (2) we've probably processed it before
6004 when we built the inner expression. */
6013 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6020 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6021 bool *strict_overflow_p
)
6023 tree type
= TREE_TYPE (t
);
6024 enum tree_code tcode
= TREE_CODE (t
);
6025 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
6026 > GET_MODE_SIZE (TYPE_MODE (type
)))
6027 ? wide_type
: type
);
6029 int same_p
= tcode
== code
;
6030 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6031 bool sub_strict_overflow_p
;
6033 /* Don't deal with constants of zero here; they confuse the code below. */
6034 if (integer_zerop (c
))
6037 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6038 op0
= TREE_OPERAND (t
, 0);
6040 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6041 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6043 /* Note that we need not handle conditional operations here since fold
6044 already handles those cases. So just do arithmetic here. */
6048 /* For a constant, we can always simplify if we are a multiply
6049 or (for divide and modulus) if it is a multiple of our constant. */
6050 if (code
== MULT_EXPR
6051 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6053 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6054 fold_convert (ctype
, c
));
6055 /* If the multiplication overflowed, we lost information on it.
6056 See PR68142 and PR69845. */
6057 if (TREE_OVERFLOW (tem
))
6063 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6064 /* If op0 is an expression ... */
6065 if ((COMPARISON_CLASS_P (op0
)
6066 || UNARY_CLASS_P (op0
)
6067 || BINARY_CLASS_P (op0
)
6068 || VL_EXP_CLASS_P (op0
)
6069 || EXPRESSION_CLASS_P (op0
))
6070 /* ... and has wrapping overflow, and its type is smaller
6071 than ctype, then we cannot pass through as widening. */
6072 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6073 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6074 && (TYPE_PRECISION (ctype
)
6075 > TYPE_PRECISION (TREE_TYPE (op0
))))
6076 /* ... or this is a truncation (t is narrower than op0),
6077 then we cannot pass through this narrowing. */
6078 || (TYPE_PRECISION (type
)
6079 < TYPE_PRECISION (TREE_TYPE (op0
)))
6080 /* ... or signedness changes for division or modulus,
6081 then we cannot pass through this conversion. */
6082 || (code
!= MULT_EXPR
6083 && (TYPE_UNSIGNED (ctype
)
6084 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6085 /* ... or has undefined overflow while the converted to
6086 type has not, we cannot do the operation in the inner type
6087 as that would introduce undefined overflow. */
6088 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6089 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6090 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6093 /* Pass the constant down and see if we can make a simplification. If
6094 we can, replace this expression with the inner simplification for
6095 possible later conversion to our or some other type. */
6096 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6097 && TREE_CODE (t2
) == INTEGER_CST
6098 && !TREE_OVERFLOW (t2
)
6099 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6101 ? ctype
: NULL_TREE
,
6102 strict_overflow_p
))))
6107 /* If widening the type changes it from signed to unsigned, then we
6108 must avoid building ABS_EXPR itself as unsigned. */
6109 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6111 tree cstype
= (*signed_type_for
) (ctype
);
6112 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6115 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6116 return fold_convert (ctype
, t1
);
6120 /* If the constant is negative, we cannot simplify this. */
6121 if (tree_int_cst_sgn (c
) == -1)
6125 /* For division and modulus, type can't be unsigned, as e.g.
6126 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6127 For signed types, even with wrapping overflow, this is fine. */
6128 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6130 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6132 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6135 case MIN_EXPR
: case MAX_EXPR
:
6136 /* If widening the type changes the signedness, then we can't perform
6137 this optimization as that changes the result. */
6138 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6141 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6142 sub_strict_overflow_p
= false;
6143 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6144 &sub_strict_overflow_p
)) != 0
6145 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6146 &sub_strict_overflow_p
)) != 0)
6148 if (tree_int_cst_sgn (c
) < 0)
6149 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6150 if (sub_strict_overflow_p
)
6151 *strict_overflow_p
= true;
6152 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6153 fold_convert (ctype
, t2
));
6157 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6158 /* If the second operand is constant, this is a multiplication
6159 or floor division, by a power of two, so we can treat it that
6160 way unless the multiplier or divisor overflows. Signed
6161 left-shift overflow is implementation-defined rather than
6162 undefined in C90, so do not convert signed left shift into
6164 if (TREE_CODE (op1
) == INTEGER_CST
6165 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6166 /* const_binop may not detect overflow correctly,
6167 so check for it explicitly here. */
6168 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6169 && 0 != (t1
= fold_convert (ctype
,
6170 const_binop (LSHIFT_EXPR
,
6173 && !TREE_OVERFLOW (t1
))
6174 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6175 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6177 fold_convert (ctype
, op0
),
6179 c
, code
, wide_type
, strict_overflow_p
);
6182 case PLUS_EXPR
: case MINUS_EXPR
:
6183 /* See if we can eliminate the operation on both sides. If we can, we
6184 can return a new PLUS or MINUS. If we can't, the only remaining
6185 cases where we can do anything are if the second operand is a
6187 sub_strict_overflow_p
= false;
6188 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6189 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6190 if (t1
!= 0 && t2
!= 0
6191 && (code
== MULT_EXPR
6192 /* If not multiplication, we can only do this if both operands
6193 are divisible by c. */
6194 || (multiple_of_p (ctype
, op0
, c
)
6195 && multiple_of_p (ctype
, op1
, c
))))
6197 if (sub_strict_overflow_p
)
6198 *strict_overflow_p
= true;
6199 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6200 fold_convert (ctype
, t2
));
6203 /* If this was a subtraction, negate OP1 and set it to be an addition.
6204 This simplifies the logic below. */
6205 if (tcode
== MINUS_EXPR
)
6207 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6208 /* If OP1 was not easily negatable, the constant may be OP0. */
6209 if (TREE_CODE (op0
) == INTEGER_CST
)
6211 std::swap (op0
, op1
);
6216 if (TREE_CODE (op1
) != INTEGER_CST
)
6219 /* If either OP1 or C are negative, this optimization is not safe for
6220 some of the division and remainder types while for others we need
6221 to change the code. */
6222 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6224 if (code
== CEIL_DIV_EXPR
)
6225 code
= FLOOR_DIV_EXPR
;
6226 else if (code
== FLOOR_DIV_EXPR
)
6227 code
= CEIL_DIV_EXPR
;
6228 else if (code
!= MULT_EXPR
6229 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6233 /* If it's a multiply or a division/modulus operation of a multiple
6234 of our constant, do the operation and verify it doesn't overflow. */
6235 if (code
== MULT_EXPR
6236 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6238 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6239 fold_convert (ctype
, c
));
6240 /* We allow the constant to overflow with wrapping semantics. */
6242 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6248 /* If we have an unsigned type, we cannot widen the operation since it
6249 will change the result if the original computation overflowed. */
6250 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6253 /* If we were able to eliminate our operation from the first side,
6254 apply our operation to the second side and reform the PLUS. */
6255 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6256 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6258 /* The last case is if we are a multiply. In that case, we can
6259 apply the distributive law to commute the multiply and addition
6260 if the multiplication of the constants doesn't overflow
6261 and overflow is defined. With undefined overflow
6262 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6263 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6264 return fold_build2 (tcode
, ctype
,
6265 fold_build2 (code
, ctype
,
6266 fold_convert (ctype
, op0
),
6267 fold_convert (ctype
, c
)),
6273 /* We have a special case here if we are doing something like
6274 (C * 8) % 4 since we know that's zero. */
6275 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6276 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6277 /* If the multiplication can overflow we cannot optimize this. */
6278 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6279 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6280 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6282 *strict_overflow_p
= true;
6283 return omit_one_operand (type
, integer_zero_node
, op0
);
6286 /* ... fall through ... */
6288 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6289 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6290 /* If we can extract our operation from the LHS, do so and return a
6291 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6292 do something only if the second operand is a constant. */
6294 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6295 strict_overflow_p
)) != 0)
6296 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6297 fold_convert (ctype
, op1
));
6298 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6299 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6300 strict_overflow_p
)) != 0)
6301 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6302 fold_convert (ctype
, t1
));
6303 else if (TREE_CODE (op1
) != INTEGER_CST
)
6306 /* If these are the same operation types, we can associate them
6307 assuming no overflow. */
6310 bool overflow_p
= false;
6311 bool overflow_mul_p
;
6312 signop sign
= TYPE_SIGN (ctype
);
6313 unsigned prec
= TYPE_PRECISION (ctype
);
6314 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6315 wi::to_wide (c
, prec
),
6316 sign
, &overflow_mul_p
);
6317 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6319 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6322 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6323 wide_int_to_tree (ctype
, mul
));
6326 /* If these operations "cancel" each other, we have the main
6327 optimizations of this pass, which occur when either constant is a
6328 multiple of the other, in which case we replace this with either an
6329 operation or CODE or TCODE.
6331 If we have an unsigned type, we cannot do this since it will change
6332 the result if the original computation overflowed. */
6333 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6334 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6335 || (tcode
== MULT_EXPR
6336 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6337 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6338 && code
!= MULT_EXPR
)))
6340 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6342 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6343 *strict_overflow_p
= true;
6344 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6345 fold_convert (ctype
,
6346 const_binop (TRUNC_DIV_EXPR
,
6349 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6351 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6352 *strict_overflow_p
= true;
6353 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6354 fold_convert (ctype
,
6355 const_binop (TRUNC_DIV_EXPR
,
6368 /* Return a node which has the indicated constant VALUE (either 0 or
6369 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6370 and is of the indicated TYPE. */
6373 constant_boolean_node (bool value
, tree type
)
6375 if (type
== integer_type_node
)
6376 return value
? integer_one_node
: integer_zero_node
;
6377 else if (type
== boolean_type_node
)
6378 return value
? boolean_true_node
: boolean_false_node
;
6379 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6380 return build_vector_from_val (type
,
6381 build_int_cst (TREE_TYPE (type
),
6384 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6388 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6389 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6390 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6391 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6392 COND is the first argument to CODE; otherwise (as in the example
6393 given here), it is the second argument. TYPE is the type of the
6394 original expression. Return NULL_TREE if no simplification is
6398 fold_binary_op_with_conditional_arg (location_t loc
,
6399 enum tree_code code
,
6400 tree type
, tree op0
, tree op1
,
6401 tree cond
, tree arg
, int cond_first_p
)
6403 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6404 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6405 tree test
, true_value
, false_value
;
6406 tree lhs
= NULL_TREE
;
6407 tree rhs
= NULL_TREE
;
6408 enum tree_code cond_code
= COND_EXPR
;
6410 if (TREE_CODE (cond
) == COND_EXPR
6411 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6413 test
= TREE_OPERAND (cond
, 0);
6414 true_value
= TREE_OPERAND (cond
, 1);
6415 false_value
= TREE_OPERAND (cond
, 2);
6416 /* If this operand throws an expression, then it does not make
6417 sense to try to perform a logical or arithmetic operation
6419 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6421 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6424 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6425 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6427 tree testtype
= TREE_TYPE (cond
);
6429 true_value
= constant_boolean_node (true, testtype
);
6430 false_value
= constant_boolean_node (false, testtype
);
6433 /* Detect the case of mixing vector and scalar types - bail out. */
6436 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6437 cond_code
= VEC_COND_EXPR
;
6439 /* This transformation is only worthwhile if we don't have to wrap ARG
6440 in a SAVE_EXPR and the operation can be simplified without recursing
6441 on at least one of the branches once its pushed inside the COND_EXPR. */
6442 if (!TREE_CONSTANT (arg
)
6443 && (TREE_SIDE_EFFECTS (arg
)
6444 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6445 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6448 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6451 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6453 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6455 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6459 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6461 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6463 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6466 /* Check that we have simplified at least one of the branches. */
6467 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6470 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6474 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6476 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6477 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6478 ADDEND is the same as X.
6480 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6481 and finite. The problematic cases are when X is zero, and its mode
6482 has signed zeros. In the case of rounding towards -infinity,
6483 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6484 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6487 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6489 if (!real_zerop (addend
))
6492 /* Don't allow the fold with -fsignaling-nans. */
6493 if (HONOR_SNANS (element_mode (type
)))
6496 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6497 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6500 /* In a vector or complex, we would need to check the sign of all zeros. */
6501 if (TREE_CODE (addend
) != REAL_CST
)
6504 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6505 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6508 /* The mode has signed zeros, and we have to honor their sign.
6509 In this situation, there is only one case we can return true for.
6510 X - 0 is the same as X unless rounding towards -infinity is
6512 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6515 /* Subroutine of fold() that optimizes comparisons of a division by
6516 a nonzero integer constant against an integer constant, i.e.
6519 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6520 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6521 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6523 The function returns the constant folded tree if a simplification
6524 can be made, and NULL_TREE otherwise. */
6527 fold_div_compare (location_t loc
,
6528 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6530 tree prod
, tmp
, hi
, lo
;
6531 tree arg00
= TREE_OPERAND (arg0
, 0);
6532 tree arg01
= TREE_OPERAND (arg0
, 1);
6533 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6534 bool neg_overflow
= false;
6537 /* We have to do this the hard way to detect unsigned overflow.
6538 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6539 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6540 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6541 neg_overflow
= false;
6543 if (sign
== UNSIGNED
)
6545 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6546 build_int_cst (TREE_TYPE (arg01
), 1));
6549 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6550 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6551 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6552 -1, overflow
| TREE_OVERFLOW (prod
));
6554 else if (tree_int_cst_sgn (arg01
) >= 0)
6556 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6557 build_int_cst (TREE_TYPE (arg01
), 1));
6558 switch (tree_int_cst_sgn (arg1
))
6561 neg_overflow
= true;
6562 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6567 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6572 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6582 /* A negative divisor reverses the relational operators. */
6583 code
= swap_tree_comparison (code
);
6585 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6586 build_int_cst (TREE_TYPE (arg01
), 1));
6587 switch (tree_int_cst_sgn (arg1
))
6590 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6595 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6600 neg_overflow
= true;
6601 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6613 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6614 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6615 if (TREE_OVERFLOW (hi
))
6616 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6617 if (TREE_OVERFLOW (lo
))
6618 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6619 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6622 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6623 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6624 if (TREE_OVERFLOW (hi
))
6625 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6626 if (TREE_OVERFLOW (lo
))
6627 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6628 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6631 if (TREE_OVERFLOW (lo
))
6633 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6634 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6636 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6639 if (TREE_OVERFLOW (hi
))
6641 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6642 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6644 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6647 if (TREE_OVERFLOW (hi
))
6649 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6650 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6652 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6655 if (TREE_OVERFLOW (lo
))
6657 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6658 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6660 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6670 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6671 equality/inequality test, then return a simplified form of the test
6672 using a sign testing. Otherwise return NULL. TYPE is the desired
6676 fold_single_bit_test_into_sign_test (location_t loc
,
6677 enum tree_code code
, tree arg0
, tree arg1
,
6680 /* If this is testing a single bit, we can optimize the test. */
6681 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6682 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6683 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6685 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6686 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6687 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6689 if (arg00
!= NULL_TREE
6690 /* This is only a win if casting to a signed type is cheap,
6691 i.e. when arg00's type is not a partial mode. */
6692 && TYPE_PRECISION (TREE_TYPE (arg00
))
6693 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6695 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6696 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6698 fold_convert_loc (loc
, stype
, arg00
),
6699 build_int_cst (stype
, 0));
6706 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6707 equality/inequality test, then return a simplified form of
6708 the test using shifts and logical operations. Otherwise return
6709 NULL. TYPE is the desired result type. */
6712 fold_single_bit_test (location_t loc
, enum tree_code code
,
6713 tree arg0
, tree arg1
, tree result_type
)
6715 /* If this is testing a single bit, we can optimize the test. */
6716 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6717 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6718 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6720 tree inner
= TREE_OPERAND (arg0
, 0);
6721 tree type
= TREE_TYPE (arg0
);
6722 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6723 machine_mode operand_mode
= TYPE_MODE (type
);
6725 tree signed_type
, unsigned_type
, intermediate_type
;
6728 /* First, see if we can fold the single bit test into a sign-bit
6730 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6735 /* Otherwise we have (A & C) != 0 where C is a single bit,
6736 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6737 Similarly for (A & C) == 0. */
6739 /* If INNER is a right shift of a constant and it plus BITNUM does
6740 not overflow, adjust BITNUM and INNER. */
6741 if (TREE_CODE (inner
) == RSHIFT_EXPR
6742 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6743 && bitnum
< TYPE_PRECISION (type
)
6744 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6745 TYPE_PRECISION (type
) - bitnum
))
6747 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6748 inner
= TREE_OPERAND (inner
, 0);
6751 /* If we are going to be able to omit the AND below, we must do our
6752 operations as unsigned. If we must use the AND, we have a choice.
6753 Normally unsigned is faster, but for some machines signed is. */
6754 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
6755 && !flag_syntax_only
) ? 0 : 1;
6757 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6758 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6759 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6760 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6763 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6764 inner
, size_int (bitnum
));
6766 one
= build_int_cst (intermediate_type
, 1);
6768 if (code
== EQ_EXPR
)
6769 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6771 /* Put the AND last so it can combine with more things. */
6772 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6774 /* Make sure to return the proper type. */
6775 inner
= fold_convert_loc (loc
, result_type
, inner
);
6782 /* Check whether we are allowed to reorder operands arg0 and arg1,
6783 such that the evaluation of arg1 occurs before arg0. */
6786 reorder_operands_p (const_tree arg0
, const_tree arg1
)
6788 if (! flag_evaluation_order
)
6790 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
6792 return ! TREE_SIDE_EFFECTS (arg0
)
6793 && ! TREE_SIDE_EFFECTS (arg1
);
6796 /* Test whether it is preferable two swap two operands, ARG0 and
6797 ARG1, for example because ARG0 is an integer constant and ARG1
6798 isn't. If REORDER is true, only recommend swapping if we can
6799 evaluate the operands in reverse order. */
6802 tree_swap_operands_p (const_tree arg0
, const_tree arg1
, bool reorder
)
6804 if (CONSTANT_CLASS_P (arg1
))
6806 if (CONSTANT_CLASS_P (arg0
))
6812 if (TREE_CONSTANT (arg1
))
6814 if (TREE_CONSTANT (arg0
))
6817 if (reorder
&& flag_evaluation_order
6818 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
6821 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6822 for commutative and comparison operators. Ensuring a canonical
6823 form allows the optimizers to find additional redundancies without
6824 having to explicitly check for both orderings. */
6825 if (TREE_CODE (arg0
) == SSA_NAME
6826 && TREE_CODE (arg1
) == SSA_NAME
6827 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6830 /* Put SSA_NAMEs last. */
6831 if (TREE_CODE (arg1
) == SSA_NAME
)
6833 if (TREE_CODE (arg0
) == SSA_NAME
)
6836 /* Put variables last. */
6846 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6847 means A >= Y && A != MAX, but in this case we know that
6848 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6851 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6853 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6855 if (TREE_CODE (bound
) == LT_EXPR
)
6856 a
= TREE_OPERAND (bound
, 0);
6857 else if (TREE_CODE (bound
) == GT_EXPR
)
6858 a
= TREE_OPERAND (bound
, 1);
6862 typea
= TREE_TYPE (a
);
6863 if (!INTEGRAL_TYPE_P (typea
)
6864 && !POINTER_TYPE_P (typea
))
6867 if (TREE_CODE (ineq
) == LT_EXPR
)
6869 a1
= TREE_OPERAND (ineq
, 1);
6870 y
= TREE_OPERAND (ineq
, 0);
6872 else if (TREE_CODE (ineq
) == GT_EXPR
)
6874 a1
= TREE_OPERAND (ineq
, 0);
6875 y
= TREE_OPERAND (ineq
, 1);
6880 if (TREE_TYPE (a1
) != typea
)
6883 if (POINTER_TYPE_P (typea
))
6885 /* Convert the pointer types into integer before taking the difference. */
6886 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6887 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6888 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6891 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6893 if (!diff
|| !integer_onep (diff
))
6896 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6899 /* Fold a sum or difference of at least one multiplication.
6900 Returns the folded tree or NULL if no simplification could be made. */
6903 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6904 tree arg0
, tree arg1
)
6906 tree arg00
, arg01
, arg10
, arg11
;
6907 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6909 /* (A * C) +- (B * C) -> (A+-B) * C.
6910 (A * C) +- A -> A * (C+-1).
6911 We are most concerned about the case where C is a constant,
6912 but other combinations show up during loop reduction. Since
6913 it is not difficult, try all four possibilities. */
6915 if (TREE_CODE (arg0
) == MULT_EXPR
)
6917 arg00
= TREE_OPERAND (arg0
, 0);
6918 arg01
= TREE_OPERAND (arg0
, 1);
6920 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6922 arg00
= build_one_cst (type
);
6927 /* We cannot generate constant 1 for fract. */
6928 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6931 arg01
= build_one_cst (type
);
6933 if (TREE_CODE (arg1
) == MULT_EXPR
)
6935 arg10
= TREE_OPERAND (arg1
, 0);
6936 arg11
= TREE_OPERAND (arg1
, 1);
6938 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6940 arg10
= build_one_cst (type
);
6941 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6942 the purpose of this canonicalization. */
6943 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
6944 && negate_expr_p (arg1
)
6945 && code
== PLUS_EXPR
)
6947 arg11
= negate_expr (arg1
);
6955 /* We cannot generate constant 1 for fract. */
6956 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6959 arg11
= build_one_cst (type
);
6963 if (operand_equal_p (arg01
, arg11
, 0))
6964 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
6965 else if (operand_equal_p (arg00
, arg10
, 0))
6966 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
6967 else if (operand_equal_p (arg00
, arg11
, 0))
6968 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
6969 else if (operand_equal_p (arg01
, arg10
, 0))
6970 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
6972 /* No identical multiplicands; see if we can find a common
6973 power-of-two factor in non-power-of-two multiplies. This
6974 can help in multi-dimensional array access. */
6975 else if (tree_fits_shwi_p (arg01
)
6976 && tree_fits_shwi_p (arg11
))
6978 HOST_WIDE_INT int01
, int11
, tmp
;
6981 int01
= tree_to_shwi (arg01
);
6982 int11
= tree_to_shwi (arg11
);
6984 /* Move min of absolute values to int11. */
6985 if (absu_hwi (int01
) < absu_hwi (int11
))
6987 tmp
= int01
, int01
= int11
, int11
= tmp
;
6988 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
6995 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
6996 /* The remainder should not be a constant, otherwise we
6997 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6998 increased the number of multiplications necessary. */
6999 && TREE_CODE (arg10
) != INTEGER_CST
)
7001 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7002 build_int_cst (TREE_TYPE (arg00
),
7007 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7012 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7013 fold_build2_loc (loc
, code
, type
,
7014 fold_convert_loc (loc
, type
, alt0
),
7015 fold_convert_loc (loc
, type
, alt1
)),
7016 fold_convert_loc (loc
, type
, same
));
7021 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7022 specified by EXPR into the buffer PTR of length LEN bytes.
7023 Return the number of bytes placed in the buffer, or zero
7027 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7029 tree type
= TREE_TYPE (expr
);
7030 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7031 int byte
, offset
, word
, words
;
7032 unsigned char value
;
7034 if ((off
== -1 && total_bytes
> len
)
7035 || off
>= total_bytes
)
7039 words
= total_bytes
/ UNITS_PER_WORD
;
7041 for (byte
= 0; byte
< total_bytes
; byte
++)
7043 int bitpos
= byte
* BITS_PER_UNIT
;
7044 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7046 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7048 if (total_bytes
> UNITS_PER_WORD
)
7050 word
= byte
/ UNITS_PER_WORD
;
7051 if (WORDS_BIG_ENDIAN
)
7052 word
= (words
- 1) - word
;
7053 offset
= word
* UNITS_PER_WORD
;
7054 if (BYTES_BIG_ENDIAN
)
7055 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7057 offset
+= byte
% UNITS_PER_WORD
;
7060 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7062 && offset
- off
< len
)
7063 ptr
[offset
- off
] = value
;
7065 return MIN (len
, total_bytes
- off
);
7069 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7070 specified by EXPR into the buffer PTR of length LEN bytes.
7071 Return the number of bytes placed in the buffer, or zero
7075 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7077 tree type
= TREE_TYPE (expr
);
7078 machine_mode mode
= TYPE_MODE (type
);
7079 int total_bytes
= GET_MODE_SIZE (mode
);
7080 FIXED_VALUE_TYPE value
;
7081 tree i_value
, i_type
;
7083 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7086 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7088 if (NULL_TREE
== i_type
7089 || TYPE_PRECISION (i_type
) != total_bytes
)
7092 value
= TREE_FIXED_CST (expr
);
7093 i_value
= double_int_to_tree (i_type
, value
.data
);
7095 return native_encode_int (i_value
, ptr
, len
, off
);
7099 /* Subroutine of native_encode_expr. Encode the REAL_CST
7100 specified by EXPR into the buffer PTR of length LEN bytes.
7101 Return the number of bytes placed in the buffer, or zero
7105 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7107 tree type
= TREE_TYPE (expr
);
7108 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7109 int byte
, offset
, word
, words
, bitpos
;
7110 unsigned char value
;
7112 /* There are always 32 bits in each long, no matter the size of
7113 the hosts long. We handle floating point representations with
7117 if ((off
== -1 && total_bytes
> len
)
7118 || off
>= total_bytes
)
7122 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7124 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7126 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7127 bitpos
+= BITS_PER_UNIT
)
7129 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7130 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7132 if (UNITS_PER_WORD
< 4)
7134 word
= byte
/ UNITS_PER_WORD
;
7135 if (WORDS_BIG_ENDIAN
)
7136 word
= (words
- 1) - word
;
7137 offset
= word
* UNITS_PER_WORD
;
7138 if (BYTES_BIG_ENDIAN
)
7139 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7141 offset
+= byte
% UNITS_PER_WORD
;
7144 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7145 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7147 && offset
- off
< len
)
7148 ptr
[offset
- off
] = value
;
7150 return MIN (len
, total_bytes
- off
);
7153 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7154 specified by EXPR into the buffer PTR of length LEN bytes.
7155 Return the number of bytes placed in the buffer, or zero
7159 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7164 part
= TREE_REALPART (expr
);
7165 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7169 part
= TREE_IMAGPART (expr
);
7171 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7172 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7176 return rsize
+ isize
;
7180 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7181 specified by EXPR into the buffer PTR of length LEN bytes.
7182 Return the number of bytes placed in the buffer, or zero
7186 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7193 count
= VECTOR_CST_NELTS (expr
);
7194 itype
= TREE_TYPE (TREE_TYPE (expr
));
7195 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7196 for (i
= 0; i
< count
; i
++)
7203 elem
= VECTOR_CST_ELT (expr
, i
);
7204 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7205 if ((off
== -1 && res
!= size
)
7218 /* Subroutine of native_encode_expr. Encode the STRING_CST
7219 specified by EXPR into the buffer PTR of length LEN bytes.
7220 Return the number of bytes placed in the buffer, or zero
7224 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7226 tree type
= TREE_TYPE (expr
);
7227 HOST_WIDE_INT total_bytes
;
7229 if (TREE_CODE (type
) != ARRAY_TYPE
7230 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7231 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7232 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7234 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7235 if ((off
== -1 && total_bytes
> len
)
7236 || off
>= total_bytes
)
7240 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7243 if (off
< TREE_STRING_LENGTH (expr
))
7245 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7246 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7248 memset (ptr
+ written
, 0,
7249 MIN (total_bytes
- written
, len
- written
));
7252 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7253 return MIN (total_bytes
- off
, len
);
7257 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7258 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7259 buffer PTR of length LEN bytes. If OFF is not -1 then start
7260 the encoding at byte offset OFF and encode at most LEN bytes.
7261 Return the number of bytes placed in the buffer, or zero upon failure. */
7264 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7266 /* We don't support starting at negative offset and -1 is special. */
7270 switch (TREE_CODE (expr
))
7273 return native_encode_int (expr
, ptr
, len
, off
);
7276 return native_encode_real (expr
, ptr
, len
, off
);
7279 return native_encode_fixed (expr
, ptr
, len
, off
);
7282 return native_encode_complex (expr
, ptr
, len
, off
);
7285 return native_encode_vector (expr
, ptr
, len
, off
);
7288 return native_encode_string (expr
, ptr
, len
, off
);
7296 /* Subroutine of native_interpret_expr. Interpret the contents of
7297 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7298 If the buffer cannot be interpreted, return NULL_TREE. */
7301 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7303 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7305 if (total_bytes
> len
7306 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7309 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7311 return wide_int_to_tree (type
, result
);
7315 /* Subroutine of native_interpret_expr. Interpret the contents of
7316 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7317 If the buffer cannot be interpreted, return NULL_TREE. */
7320 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7322 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7324 FIXED_VALUE_TYPE fixed_value
;
7326 if (total_bytes
> len
7327 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7330 result
= double_int::from_buffer (ptr
, total_bytes
);
7331 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7333 return build_fixed (type
, fixed_value
);
7337 /* Subroutine of native_interpret_expr. Interpret the contents of
7338 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7339 If the buffer cannot be interpreted, return NULL_TREE. */
7342 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7344 machine_mode mode
= TYPE_MODE (type
);
7345 int total_bytes
= GET_MODE_SIZE (mode
);
7346 unsigned char value
;
7347 /* There are always 32 bits in each long, no matter the size of
7348 the hosts long. We handle floating point representations with
7353 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7354 if (total_bytes
> len
|| total_bytes
> 24)
7356 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7358 memset (tmp
, 0, sizeof (tmp
));
7359 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7360 bitpos
+= BITS_PER_UNIT
)
7362 /* Both OFFSET and BYTE index within a long;
7363 bitpos indexes the whole float. */
7364 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7365 if (UNITS_PER_WORD
< 4)
7367 int word
= byte
/ UNITS_PER_WORD
;
7368 if (WORDS_BIG_ENDIAN
)
7369 word
= (words
- 1) - word
;
7370 offset
= word
* UNITS_PER_WORD
;
7371 if (BYTES_BIG_ENDIAN
)
7372 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7374 offset
+= byte
% UNITS_PER_WORD
;
7379 if (BYTES_BIG_ENDIAN
)
7381 /* Reverse bytes within each long, or within the entire float
7382 if it's smaller than a long (for HFmode). */
7383 offset
= MIN (3, total_bytes
- 1) - offset
;
7384 gcc_assert (offset
>= 0);
7387 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7389 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7392 real_from_target (&r
, tmp
, mode
);
7393 return build_real (type
, r
);
7397 /* Subroutine of native_interpret_expr. Interpret the contents of
7398 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7399 If the buffer cannot be interpreted, return NULL_TREE. */
7402 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7404 tree etype
, rpart
, ipart
;
7407 etype
= TREE_TYPE (type
);
7408 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7411 rpart
= native_interpret_expr (etype
, ptr
, size
);
7414 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7417 return build_complex (type
, rpart
, ipart
);
7421 /* Subroutine of native_interpret_expr. Interpret the contents of
7422 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7423 If the buffer cannot be interpreted, return NULL_TREE. */
7426 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7432 etype
= TREE_TYPE (type
);
7433 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7434 count
= TYPE_VECTOR_SUBPARTS (type
);
7435 if (size
* count
> len
)
7438 elements
= XALLOCAVEC (tree
, count
);
7439 for (i
= count
- 1; i
>= 0; i
--)
7441 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7446 return build_vector (type
, elements
);
7450 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7451 the buffer PTR of length LEN as a constant of type TYPE. For
7452 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7453 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7454 return NULL_TREE. */
7457 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7459 switch (TREE_CODE (type
))
7465 case REFERENCE_TYPE
:
7466 return native_interpret_int (type
, ptr
, len
);
7469 return native_interpret_real (type
, ptr
, len
);
7471 case FIXED_POINT_TYPE
:
7472 return native_interpret_fixed (type
, ptr
, len
);
7475 return native_interpret_complex (type
, ptr
, len
);
7478 return native_interpret_vector (type
, ptr
, len
);
7485 /* Returns true if we can interpret the contents of a native encoding
7489 can_native_interpret_type_p (tree type
)
7491 switch (TREE_CODE (type
))
7497 case REFERENCE_TYPE
:
7498 case FIXED_POINT_TYPE
:
7508 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7509 TYPE at compile-time. If we're unable to perform the conversion
7510 return NULL_TREE. */
7513 fold_view_convert_expr (tree type
, tree expr
)
7515 /* We support up to 512-bit values (for V8DFmode). */
7516 unsigned char buffer
[64];
7519 /* Check that the host and target are sane. */
7520 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7523 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7527 return native_interpret_expr (type
, buffer
, len
);
7530 /* Build an expression for the address of T. Folds away INDIRECT_REF
7531 to avoid confusing the gimplify process. */
7534 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7536 /* The size of the object is not relevant when talking about its address. */
7537 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7538 t
= TREE_OPERAND (t
, 0);
7540 if (TREE_CODE (t
) == INDIRECT_REF
)
7542 t
= TREE_OPERAND (t
, 0);
7544 if (TREE_TYPE (t
) != ptrtype
)
7545 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7547 else if (TREE_CODE (t
) == MEM_REF
7548 && integer_zerop (TREE_OPERAND (t
, 1)))
7549 return TREE_OPERAND (t
, 0);
7550 else if (TREE_CODE (t
) == MEM_REF
7551 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7552 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7553 TREE_OPERAND (t
, 0),
7554 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7555 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7557 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7559 if (TREE_TYPE (t
) != ptrtype
)
7560 t
= fold_convert_loc (loc
, ptrtype
, t
);
7563 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7568 /* Build an expression for the address of T. */
7571 build_fold_addr_expr_loc (location_t loc
, tree t
)
7573 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7575 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7578 /* Fold a unary expression of code CODE and type TYPE with operand
7579 OP0. Return the folded expression if folding is successful.
7580 Otherwise, return NULL_TREE. */
7583 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7587 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7589 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7590 && TREE_CODE_LENGTH (code
) == 1);
7595 if (CONVERT_EXPR_CODE_P (code
)
7596 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7598 /* Don't use STRIP_NOPS, because signedness of argument type
7600 STRIP_SIGN_NOPS (arg0
);
7604 /* Strip any conversions that don't change the mode. This
7605 is safe for every expression, except for a comparison
7606 expression because its signedness is derived from its
7609 Note that this is done as an internal manipulation within
7610 the constant folder, in order to find the simplest
7611 representation of the arguments so that their form can be
7612 studied. In any cases, the appropriate type conversions
7613 should be put back in the tree that will get out of the
7618 if (CONSTANT_CLASS_P (arg0
))
7620 tree tem
= const_unop (code
, type
, arg0
);
7623 if (TREE_TYPE (tem
) != type
)
7624 tem
= fold_convert_loc (loc
, type
, tem
);
7630 tem
= generic_simplify (loc
, code
, type
, op0
);
7634 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7636 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7637 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7638 fold_build1_loc (loc
, code
, type
,
7639 fold_convert_loc (loc
, TREE_TYPE (op0
),
7640 TREE_OPERAND (arg0
, 1))));
7641 else if (TREE_CODE (arg0
) == COND_EXPR
)
7643 tree arg01
= TREE_OPERAND (arg0
, 1);
7644 tree arg02
= TREE_OPERAND (arg0
, 2);
7645 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7646 arg01
= fold_build1_loc (loc
, code
, type
,
7647 fold_convert_loc (loc
,
7648 TREE_TYPE (op0
), arg01
));
7649 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7650 arg02
= fold_build1_loc (loc
, code
, type
,
7651 fold_convert_loc (loc
,
7652 TREE_TYPE (op0
), arg02
));
7653 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7656 /* If this was a conversion, and all we did was to move into
7657 inside the COND_EXPR, bring it back out. But leave it if
7658 it is a conversion from integer to integer and the
7659 result precision is no wider than a word since such a
7660 conversion is cheap and may be optimized away by combine,
7661 while it couldn't if it were outside the COND_EXPR. Then return
7662 so we don't get into an infinite recursion loop taking the
7663 conversion out and then back in. */
7665 if ((CONVERT_EXPR_CODE_P (code
)
7666 || code
== NON_LVALUE_EXPR
)
7667 && TREE_CODE (tem
) == COND_EXPR
7668 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7669 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7670 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7671 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7672 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7673 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7674 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7676 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7677 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7678 || flag_syntax_only
))
7679 tem
= build1_loc (loc
, code
, type
,
7681 TREE_TYPE (TREE_OPERAND
7682 (TREE_OPERAND (tem
, 1), 0)),
7683 TREE_OPERAND (tem
, 0),
7684 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7685 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7693 case NON_LVALUE_EXPR
:
7694 if (!maybe_lvalue_p (op0
))
7695 return fold_convert_loc (loc
, type
, op0
);
7700 case FIX_TRUNC_EXPR
:
7701 if (COMPARISON_CLASS_P (op0
))
7703 /* If we have (type) (a CMP b) and type is an integral type, return
7704 new expression involving the new type. Canonicalize
7705 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7707 Do not fold the result as that would not simplify further, also
7708 folding again results in recursions. */
7709 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7710 return build2_loc (loc
, TREE_CODE (op0
), type
,
7711 TREE_OPERAND (op0
, 0),
7712 TREE_OPERAND (op0
, 1));
7713 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7714 && TREE_CODE (type
) != VECTOR_TYPE
)
7715 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7716 constant_boolean_node (true, type
),
7717 constant_boolean_node (false, type
));
7720 /* Handle (T *)&A.B.C for A being of type T and B and C
7721 living at offset zero. This occurs frequently in
7722 C++ upcasting and then accessing the base. */
7723 if (TREE_CODE (op0
) == ADDR_EXPR
7724 && POINTER_TYPE_P (type
)
7725 && handled_component_p (TREE_OPERAND (op0
, 0)))
7727 HOST_WIDE_INT bitsize
, bitpos
;
7730 int unsignedp
, reversep
, volatilep
;
7732 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
7733 &offset
, &mode
, &unsignedp
, &reversep
,
7735 /* If the reference was to a (constant) zero offset, we can use
7736 the address of the base if it has the same base type
7737 as the result type and the pointer type is unqualified. */
7738 if (! offset
&& bitpos
== 0
7739 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7740 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7741 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7742 return fold_convert_loc (loc
, type
,
7743 build_fold_addr_expr_loc (loc
, base
));
7746 if (TREE_CODE (op0
) == MODIFY_EXPR
7747 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7748 /* Detect assigning a bitfield. */
7749 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7751 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7753 /* Don't leave an assignment inside a conversion
7754 unless assigning a bitfield. */
7755 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7756 /* First do the assignment, then return converted constant. */
7757 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7758 TREE_NO_WARNING (tem
) = 1;
7759 TREE_USED (tem
) = 1;
7763 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7764 constants (if x has signed type, the sign bit cannot be set
7765 in c). This folds extension into the BIT_AND_EXPR.
7766 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7767 very likely don't have maximal range for their precision and this
7768 transformation effectively doesn't preserve non-maximal ranges. */
7769 if (TREE_CODE (type
) == INTEGER_TYPE
7770 && TREE_CODE (op0
) == BIT_AND_EXPR
7771 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7773 tree and_expr
= op0
;
7774 tree and0
= TREE_OPERAND (and_expr
, 0);
7775 tree and1
= TREE_OPERAND (and_expr
, 1);
7778 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7779 || (TYPE_PRECISION (type
)
7780 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7782 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7783 <= HOST_BITS_PER_WIDE_INT
7784 && tree_fits_uhwi_p (and1
))
7786 unsigned HOST_WIDE_INT cst
;
7788 cst
= tree_to_uhwi (and1
);
7789 cst
&= HOST_WIDE_INT_M1U
7790 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7791 change
= (cst
== 0);
7793 && !flag_syntax_only
7794 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
7797 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7798 and0
= fold_convert_loc (loc
, uns
, and0
);
7799 and1
= fold_convert_loc (loc
, uns
, and1
);
7804 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7805 TREE_OVERFLOW (and1
));
7806 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7807 fold_convert_loc (loc
, type
, and0
), tem
);
7811 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7812 cast (T1)X will fold away. We assume that this happens when X itself
7814 if (POINTER_TYPE_P (type
)
7815 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7816 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
7818 tree arg00
= TREE_OPERAND (arg0
, 0);
7819 tree arg01
= TREE_OPERAND (arg0
, 1);
7821 return fold_build_pointer_plus_loc
7822 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7825 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7826 of the same precision, and X is an integer type not narrower than
7827 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7828 if (INTEGRAL_TYPE_P (type
)
7829 && TREE_CODE (op0
) == BIT_NOT_EXPR
7830 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7831 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7832 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7834 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7835 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7836 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7837 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7838 fold_convert_loc (loc
, type
, tem
));
7841 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7842 type of X and Y (integer types only). */
7843 if (INTEGRAL_TYPE_P (type
)
7844 && TREE_CODE (op0
) == MULT_EXPR
7845 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7846 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7848 /* Be careful not to introduce new overflows. */
7850 if (TYPE_OVERFLOW_WRAPS (type
))
7853 mult_type
= unsigned_type_for (type
);
7855 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7857 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7858 fold_convert_loc (loc
, mult_type
,
7859 TREE_OPERAND (op0
, 0)),
7860 fold_convert_loc (loc
, mult_type
,
7861 TREE_OPERAND (op0
, 1)));
7862 return fold_convert_loc (loc
, type
, tem
);
7868 case VIEW_CONVERT_EXPR
:
7869 if (TREE_CODE (op0
) == MEM_REF
)
7871 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
7872 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
7873 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
7874 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7875 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
7882 tem
= fold_negate_expr (loc
, arg0
);
7884 return fold_convert_loc (loc
, type
, tem
);
7888 /* Convert fabs((double)float) into (double)fabsf(float). */
7889 if (TREE_CODE (arg0
) == NOP_EXPR
7890 && TREE_CODE (type
) == REAL_TYPE
)
7892 tree targ0
= strip_float_extensions (arg0
);
7894 return fold_convert_loc (loc
, type
,
7895 fold_build1_loc (loc
, ABS_EXPR
,
7902 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7903 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7904 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7905 fold_convert_loc (loc
, type
,
7906 TREE_OPERAND (arg0
, 0)))))
7907 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
7908 fold_convert_loc (loc
, type
,
7909 TREE_OPERAND (arg0
, 1)));
7910 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7911 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7912 fold_convert_loc (loc
, type
,
7913 TREE_OPERAND (arg0
, 1)))))
7914 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
7915 fold_convert_loc (loc
, type
,
7916 TREE_OPERAND (arg0
, 0)), tem
);
7920 case TRUTH_NOT_EXPR
:
7921 /* Note that the operand of this must be an int
7922 and its values must be 0 or 1.
7923 ("true" is a fixed value perhaps depending on the language,
7924 but we don't handle values other than 1 correctly yet.) */
7925 tem
= fold_truth_not_expr (loc
, arg0
);
7928 return fold_convert_loc (loc
, type
, tem
);
7931 /* Fold *&X to X if X is an lvalue. */
7932 if (TREE_CODE (op0
) == ADDR_EXPR
)
7934 tree op00
= TREE_OPERAND (op0
, 0);
7935 if ((TREE_CODE (op00
) == VAR_DECL
7936 || TREE_CODE (op00
) == PARM_DECL
7937 || TREE_CODE (op00
) == RESULT_DECL
)
7938 && !TREE_READONLY (op00
))
7945 } /* switch (code) */
7949 /* If the operation was a conversion do _not_ mark a resulting constant
7950 with TREE_OVERFLOW if the original constant was not. These conversions
7951 have implementation defined behavior and retaining the TREE_OVERFLOW
7952 flag here would confuse later passes such as VRP. */
7954 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
7955 tree type
, tree op0
)
7957 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
7959 && TREE_CODE (res
) == INTEGER_CST
7960 && TREE_CODE (op0
) == INTEGER_CST
7961 && CONVERT_EXPR_CODE_P (code
))
7962 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
7967 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7968 operands OP0 and OP1. LOC is the location of the resulting expression.
7969 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7970 Return the folded expression if folding is successful. Otherwise,
7971 return NULL_TREE. */
7973 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
7974 tree arg0
, tree arg1
, tree op0
, tree op1
)
7978 /* We only do these simplifications if we are optimizing. */
7982 /* Check for things like (A || B) && (A || C). We can convert this
7983 to A || (B && C). Note that either operator can be any of the four
7984 truth and/or operations and the transformation will still be
7985 valid. Also note that we only care about order for the
7986 ANDIF and ORIF operators. If B contains side effects, this
7987 might change the truth-value of A. */
7988 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
7989 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
7990 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
7991 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
7992 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
7993 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
7995 tree a00
= TREE_OPERAND (arg0
, 0);
7996 tree a01
= TREE_OPERAND (arg0
, 1);
7997 tree a10
= TREE_OPERAND (arg1
, 0);
7998 tree a11
= TREE_OPERAND (arg1
, 1);
7999 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
8000 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
8001 && (code
== TRUTH_AND_EXPR
8002 || code
== TRUTH_OR_EXPR
));
8004 if (operand_equal_p (a00
, a10
, 0))
8005 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8006 fold_build2_loc (loc
, code
, type
, a01
, a11
));
8007 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
8008 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8009 fold_build2_loc (loc
, code
, type
, a01
, a10
));
8010 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
8011 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
8012 fold_build2_loc (loc
, code
, type
, a00
, a11
));
8014 /* This case if tricky because we must either have commutative
8015 operators or else A10 must not have side-effects. */
8017 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
8018 && operand_equal_p (a01
, a11
, 0))
8019 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
8020 fold_build2_loc (loc
, code
, type
, a00
, a10
),
8024 /* See if we can build a range comparison. */
8025 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
8028 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
8029 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8031 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8033 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8036 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8037 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8039 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8041 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8044 /* Check for the possibility of merging component references. If our
8045 lhs is another similar operation, try to merge its rhs with our
8046 rhs. Then try to merge our lhs and rhs. */
8047 if (TREE_CODE (arg0
) == code
8048 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8049 TREE_OPERAND (arg0
, 1), arg1
)))
8050 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8052 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8055 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8056 && (code
== TRUTH_AND_EXPR
8057 || code
== TRUTH_ANDIF_EXPR
8058 || code
== TRUTH_OR_EXPR
8059 || code
== TRUTH_ORIF_EXPR
))
8061 enum tree_code ncode
, icode
;
8063 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8064 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8065 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8067 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8068 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8069 We don't want to pack more than two leafs to a non-IF AND/OR
8071 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8072 equal to IF-CODE, then we don't want to add right-hand operand.
8073 If the inner right-hand side of left-hand operand has
8074 side-effects, or isn't simple, then we can't add to it,
8075 as otherwise we might destroy if-sequence. */
8076 if (TREE_CODE (arg0
) == icode
8077 && simple_operand_p_2 (arg1
)
8078 /* Needed for sequence points to handle trappings, and
8080 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8082 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8084 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8087 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8088 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8089 else if (TREE_CODE (arg1
) == icode
8090 && simple_operand_p_2 (arg0
)
8091 /* Needed for sequence points to handle trappings, and
8093 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8095 tem
= fold_build2_loc (loc
, ncode
, type
,
8096 arg0
, TREE_OPERAND (arg1
, 0));
8097 return fold_build2_loc (loc
, icode
, type
, tem
,
8098 TREE_OPERAND (arg1
, 1));
8100 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8102 For sequence point consistancy, we need to check for trapping,
8103 and side-effects. */
8104 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8105 && simple_operand_p_2 (arg1
))
8106 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8112 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8113 by changing CODE to reduce the magnitude of constants involved in
8114 ARG0 of the comparison.
8115 Returns a canonicalized comparison tree if a simplification was
8116 possible, otherwise returns NULL_TREE.
8117 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8118 valid if signed overflow is undefined. */
8121 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8122 tree arg0
, tree arg1
,
8123 bool *strict_overflow_p
)
8125 enum tree_code code0
= TREE_CODE (arg0
);
8126 tree t
, cst0
= NULL_TREE
;
8129 /* Match A +- CST code arg1. We can change this only if overflow
8131 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8132 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8133 /* In principle pointers also have undefined overflow behavior,
8134 but that causes problems elsewhere. */
8135 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8136 && (code0
== MINUS_EXPR
8137 || code0
== PLUS_EXPR
)
8138 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8141 /* Identify the constant in arg0 and its sign. */
8142 cst0
= TREE_OPERAND (arg0
, 1);
8143 sgn0
= tree_int_cst_sgn (cst0
);
8145 /* Overflowed constants and zero will cause problems. */
8146 if (integer_zerop (cst0
)
8147 || TREE_OVERFLOW (cst0
))
8150 /* See if we can reduce the magnitude of the constant in
8151 arg0 by changing the comparison code. */
8152 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8154 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8156 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8157 else if (code
== GT_EXPR
8158 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8160 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8161 else if (code
== LE_EXPR
8162 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8164 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8165 else if (code
== GE_EXPR
8166 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8170 *strict_overflow_p
= true;
8172 /* Now build the constant reduced in magnitude. But not if that
8173 would produce one outside of its types range. */
8174 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8176 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8177 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8179 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8180 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8183 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8184 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8185 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8186 t
= fold_convert (TREE_TYPE (arg1
), t
);
8188 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8191 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8192 overflow further. Try to decrease the magnitude of constants involved
8193 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8194 and put sole constants at the second argument position.
8195 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8198 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8199 tree arg0
, tree arg1
)
8202 bool strict_overflow_p
;
8203 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8204 "when reducing constant in comparison");
8206 /* Try canonicalization by simplifying arg0. */
8207 strict_overflow_p
= false;
8208 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8209 &strict_overflow_p
);
8212 if (strict_overflow_p
)
8213 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8217 /* Try canonicalization by simplifying arg1 using the swapped
8219 code
= swap_tree_comparison (code
);
8220 strict_overflow_p
= false;
8221 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8222 &strict_overflow_p
);
8223 if (t
&& strict_overflow_p
)
8224 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8228 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8229 space. This is used to avoid issuing overflow warnings for
8230 expressions like &p->x which can not wrap. */
8233 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8235 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8242 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8243 if (offset
== NULL_TREE
)
8244 wi_offset
= wi::zero (precision
);
8245 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8251 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8252 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8256 if (!wi::fits_uhwi_p (total
))
8259 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8263 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8265 if (TREE_CODE (base
) == ADDR_EXPR
)
8267 HOST_WIDE_INT base_size
;
8269 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8270 if (base_size
> 0 && size
< base_size
)
8274 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8277 /* Return a positive integer when the symbol DECL is known to have
8278 a nonzero address, zero when it's known not to (e.g., it's a weak
8279 symbol), and a negative integer when the symbol is not yet in the
8280 symbol table and so whether or not its address is zero is unknown. */
8282 maybe_nonzero_address (tree decl
)
8284 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
8285 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
8286 return symbol
->nonzero_address ();
8291 /* Subroutine of fold_binary. This routine performs all of the
8292 transformations that are common to the equality/inequality
8293 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8294 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8295 fold_binary should call fold_binary. Fold a comparison with
8296 tree code CODE and type TYPE with operands OP0 and OP1. Return
8297 the folded comparison or NULL_TREE. */
8300 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8303 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8304 tree arg0
, arg1
, tem
;
8309 STRIP_SIGN_NOPS (arg0
);
8310 STRIP_SIGN_NOPS (arg1
);
8312 /* For comparisons of pointers we can decompose it to a compile time
8313 comparison of the base objects and the offsets into the object.
8314 This requires at least one operand being an ADDR_EXPR or a
8315 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8316 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8317 && (TREE_CODE (arg0
) == ADDR_EXPR
8318 || TREE_CODE (arg1
) == ADDR_EXPR
8319 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8320 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8322 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8323 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8325 int volatilep
, reversep
, unsignedp
;
8326 bool indirect_base0
= false, indirect_base1
= false;
8328 /* Get base and offset for the access. Strip ADDR_EXPR for
8329 get_inner_reference, but put it back by stripping INDIRECT_REF
8330 off the base object if possible. indirect_baseN will be true
8331 if baseN is not an address but refers to the object itself. */
8333 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8336 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8337 &bitsize
, &bitpos0
, &offset0
, &mode
,
8338 &unsignedp
, &reversep
, &volatilep
, false);
8339 if (TREE_CODE (base0
) == INDIRECT_REF
)
8340 base0
= TREE_OPERAND (base0
, 0);
8342 indirect_base0
= true;
8344 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8346 base0
= TREE_OPERAND (arg0
, 0);
8347 STRIP_SIGN_NOPS (base0
);
8348 if (TREE_CODE (base0
) == ADDR_EXPR
)
8351 = get_inner_reference (TREE_OPERAND (base0
, 0),
8352 &bitsize
, &bitpos0
, &offset0
, &mode
,
8353 &unsignedp
, &reversep
, &volatilep
,
8355 if (TREE_CODE (base0
) == INDIRECT_REF
)
8356 base0
= TREE_OPERAND (base0
, 0);
8358 indirect_base0
= true;
8360 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8361 offset0
= TREE_OPERAND (arg0
, 1);
8363 offset0
= size_binop (PLUS_EXPR
, offset0
,
8364 TREE_OPERAND (arg0
, 1));
8365 if (TREE_CODE (offset0
) == INTEGER_CST
)
8367 offset_int tem
= wi::sext (wi::to_offset (offset0
),
8368 TYPE_PRECISION (sizetype
));
8369 tem
<<= LOG2_BITS_PER_UNIT
;
8371 if (wi::fits_shwi_p (tem
))
8373 bitpos0
= tem
.to_shwi ();
8374 offset0
= NULL_TREE
;
8380 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8383 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8384 &bitsize
, &bitpos1
, &offset1
, &mode
,
8385 &unsignedp
, &reversep
, &volatilep
, false);
8386 if (TREE_CODE (base1
) == INDIRECT_REF
)
8387 base1
= TREE_OPERAND (base1
, 0);
8389 indirect_base1
= true;
8391 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8393 base1
= TREE_OPERAND (arg1
, 0);
8394 STRIP_SIGN_NOPS (base1
);
8395 if (TREE_CODE (base1
) == ADDR_EXPR
)
8398 = get_inner_reference (TREE_OPERAND (base1
, 0),
8399 &bitsize
, &bitpos1
, &offset1
, &mode
,
8400 &unsignedp
, &reversep
, &volatilep
,
8402 if (TREE_CODE (base1
) == INDIRECT_REF
)
8403 base1
= TREE_OPERAND (base1
, 0);
8405 indirect_base1
= true;
8407 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8408 offset1
= TREE_OPERAND (arg1
, 1);
8410 offset1
= size_binop (PLUS_EXPR
, offset1
,
8411 TREE_OPERAND (arg1
, 1));
8412 if (TREE_CODE (offset1
) == INTEGER_CST
)
8414 offset_int tem
= wi::sext (wi::to_offset (offset1
),
8415 TYPE_PRECISION (sizetype
));
8416 tem
<<= LOG2_BITS_PER_UNIT
;
8418 if (wi::fits_shwi_p (tem
))
8420 bitpos1
= tem
.to_shwi ();
8421 offset1
= NULL_TREE
;
8426 /* If we have equivalent bases we might be able to simplify. */
8427 if (indirect_base0
== indirect_base1
8428 && operand_equal_p (base0
, base1
,
8429 indirect_base0
? OEP_ADDRESS_OF
: 0))
8431 /* We can fold this expression to a constant if the non-constant
8432 offset parts are equal. */
8433 if ((offset0
== offset1
8434 || (offset0
&& offset1
8435 && operand_equal_p (offset0
, offset1
, 0)))
8438 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8439 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8443 && bitpos0
!= bitpos1
8444 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8445 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8446 fold_overflow_warning (("assuming pointer wraparound does not "
8447 "occur when comparing P +- C1 with "
8449 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8454 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8456 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8458 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8460 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8462 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8464 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8468 /* We can simplify the comparison to a comparison of the variable
8469 offset parts if the constant offset parts are equal.
8470 Be careful to use signed sizetype here because otherwise we
8471 mess with array offsets in the wrong way. This is possible
8472 because pointer arithmetic is restricted to retain within an
8473 object and overflow on pointer differences is undefined as of
8474 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8475 else if (bitpos0
== bitpos1
8478 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8479 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8481 /* By converting to signed sizetype we cover middle-end pointer
8482 arithmetic which operates on unsigned pointer types of size
8483 type size and ARRAY_REF offsets which are properly sign or
8484 zero extended from their type in case it is narrower than
8486 if (offset0
== NULL_TREE
)
8487 offset0
= build_int_cst (ssizetype
, 0);
8489 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8490 if (offset1
== NULL_TREE
)
8491 offset1
= build_int_cst (ssizetype
, 0);
8493 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8496 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8497 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8498 fold_overflow_warning (("assuming pointer wraparound does not "
8499 "occur when comparing P +- C1 with "
8501 WARN_STRICT_OVERFLOW_COMPARISON
);
8503 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8506 /* For equal offsets we can simplify to a comparison of the
8508 else if (bitpos0
== bitpos1
8510 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8512 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8513 && ((offset0
== offset1
)
8514 || (offset0
&& offset1
8515 && operand_equal_p (offset0
, offset1
, 0))))
8518 base0
= build_fold_addr_expr_loc (loc
, base0
);
8520 base1
= build_fold_addr_expr_loc (loc
, base1
);
8521 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8523 /* Comparison between an ordinary (non-weak) symbol and a null
8524 pointer can be eliminated since such symbols must have a non
8525 null address. In C, relational expressions between pointers
8526 to objects and null pointers are undefined. The results
8527 below follow the C++ rules with the additional property that
8528 every object pointer compares greater than a null pointer.
8530 else if (DECL_P (base0
)
8531 && maybe_nonzero_address (base0
) > 0
8532 /* Avoid folding references to struct members at offset 0 to
8533 prevent tests like '&ptr->firstmember == 0' from getting
8534 eliminated. When ptr is null, although the -> expression
8535 is strictly speaking invalid, GCC retains it as a matter
8536 of QoI. See PR c/44555. */
8537 && (offset0
== NULL_TREE
&& bitpos0
!= 0)
8538 /* The caller guarantees that when one of the arguments is
8539 constant (i.e., null in this case) it is second. */
8540 && integer_zerop (arg1
))
8547 return constant_boolean_node (false, type
);
8551 return constant_boolean_node (true, type
);
8558 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8559 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8560 the resulting offset is smaller in absolute value than the
8561 original one and has the same sign. */
8562 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8563 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8564 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8565 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8566 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8567 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8568 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8569 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8571 tree const1
= TREE_OPERAND (arg0
, 1);
8572 tree const2
= TREE_OPERAND (arg1
, 1);
8573 tree variable1
= TREE_OPERAND (arg0
, 0);
8574 tree variable2
= TREE_OPERAND (arg1
, 0);
8576 const char * const warnmsg
= G_("assuming signed overflow does not "
8577 "occur when combining constants around "
8580 /* Put the constant on the side where it doesn't overflow and is
8581 of lower absolute value and of same sign than before. */
8582 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8583 ? MINUS_EXPR
: PLUS_EXPR
,
8585 if (!TREE_OVERFLOW (cst
)
8586 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8587 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8589 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8590 return fold_build2_loc (loc
, code
, type
,
8592 fold_build2_loc (loc
, TREE_CODE (arg1
),
8597 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8598 ? MINUS_EXPR
: PLUS_EXPR
,
8600 if (!TREE_OVERFLOW (cst
)
8601 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8602 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8604 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8605 return fold_build2_loc (loc
, code
, type
,
8606 fold_build2_loc (loc
, TREE_CODE (arg0
),
8613 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8617 /* If we are comparing an expression that just has comparisons
8618 of two integer values, arithmetic expressions of those comparisons,
8619 and constants, we can simplify it. There are only three cases
8620 to check: the two values can either be equal, the first can be
8621 greater, or the second can be greater. Fold the expression for
8622 those three values. Since each value must be 0 or 1, we have
8623 eight possibilities, each of which corresponds to the constant 0
8624 or 1 or one of the six possible comparisons.
8626 This handles common cases like (a > b) == 0 but also handles
8627 expressions like ((x > y) - (y > x)) > 0, which supposedly
8628 occur in macroized code. */
8630 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8632 tree cval1
= 0, cval2
= 0;
8635 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8636 /* Don't handle degenerate cases here; they should already
8637 have been handled anyway. */
8638 && cval1
!= 0 && cval2
!= 0
8639 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8640 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8641 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8642 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8643 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8644 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8645 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8647 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8648 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8650 /* We can't just pass T to eval_subst in case cval1 or cval2
8651 was the same as ARG1. */
8654 = fold_build2_loc (loc
, code
, type
,
8655 eval_subst (loc
, arg0
, cval1
, maxval
,
8659 = fold_build2_loc (loc
, code
, type
,
8660 eval_subst (loc
, arg0
, cval1
, maxval
,
8664 = fold_build2_loc (loc
, code
, type
,
8665 eval_subst (loc
, arg0
, cval1
, minval
,
8669 /* All three of these results should be 0 or 1. Confirm they are.
8670 Then use those values to select the proper code to use. */
8672 if (TREE_CODE (high_result
) == INTEGER_CST
8673 && TREE_CODE (equal_result
) == INTEGER_CST
8674 && TREE_CODE (low_result
) == INTEGER_CST
)
8676 /* Make a 3-bit mask with the high-order bit being the
8677 value for `>', the next for '=', and the low for '<'. */
8678 switch ((integer_onep (high_result
) * 4)
8679 + (integer_onep (equal_result
) * 2)
8680 + integer_onep (low_result
))
8684 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8705 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8710 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8711 SET_EXPR_LOCATION (tem
, loc
);
8714 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8719 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8720 into a single range test. */
8721 if ((TREE_CODE (arg0
) == TRUNC_DIV_EXPR
8722 || TREE_CODE (arg0
) == EXACT_DIV_EXPR
)
8723 && TREE_CODE (arg1
) == INTEGER_CST
8724 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8725 && !integer_zerop (TREE_OPERAND (arg0
, 1))
8726 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8727 && !TREE_OVERFLOW (arg1
))
8729 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
8730 if (tem
!= NULL_TREE
)
8738 /* Subroutine of fold_binary. Optimize complex multiplications of the
8739 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8740 argument EXPR represents the expression "z" of type TYPE. */
8743 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8745 tree itype
= TREE_TYPE (type
);
8746 tree rpart
, ipart
, tem
;
8748 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8750 rpart
= TREE_OPERAND (expr
, 0);
8751 ipart
= TREE_OPERAND (expr
, 1);
8753 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8755 rpart
= TREE_REALPART (expr
);
8756 ipart
= TREE_IMAGPART (expr
);
8760 expr
= save_expr (expr
);
8761 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8762 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8765 rpart
= save_expr (rpart
);
8766 ipart
= save_expr (ipart
);
8767 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8768 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8769 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8770 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8771 build_zero_cst (itype
));
8775 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8776 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8779 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
8781 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
8783 if (TREE_CODE (arg
) == VECTOR_CST
)
8785 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8786 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8788 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8790 constructor_elt
*elt
;
8792 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8793 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8796 elts
[i
] = elt
->value
;
8800 for (; i
< nelts
; i
++)
8802 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8806 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8807 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8808 NULL_TREE otherwise. */
8811 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
8813 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8815 bool need_ctor
= false;
8817 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8818 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8819 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8820 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8823 elts
= XALLOCAVEC (tree
, nelts
* 3);
8824 if (!vec_cst_ctor_to_array (arg0
, elts
)
8825 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
8828 for (i
= 0; i
< nelts
; i
++)
8830 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
8832 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
8837 vec
<constructor_elt
, va_gc
> *v
;
8838 vec_alloc (v
, nelts
);
8839 for (i
= 0; i
< nelts
; i
++)
8840 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
8841 return build_constructor (type
, v
);
8844 return build_vector (type
, &elts
[2 * nelts
]);
8847 /* Try to fold a pointer difference of type TYPE two address expressions of
8848 array references AREF0 and AREF1 using location LOC. Return a
8849 simplified expression for the difference or NULL_TREE. */
8852 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8853 tree aref0
, tree aref1
)
8855 tree base0
= TREE_OPERAND (aref0
, 0);
8856 tree base1
= TREE_OPERAND (aref1
, 0);
8857 tree base_offset
= build_int_cst (type
, 0);
8859 /* If the bases are array references as well, recurse. If the bases
8860 are pointer indirections compute the difference of the pointers.
8861 If the bases are equal, we are set. */
8862 if ((TREE_CODE (base0
) == ARRAY_REF
8863 && TREE_CODE (base1
) == ARRAY_REF
8865 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8866 || (INDIRECT_REF_P (base0
)
8867 && INDIRECT_REF_P (base1
)
8869 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8870 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8872 TREE_OPERAND (base1
, 0)))))
8873 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8875 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8876 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8877 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8878 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
8879 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8881 fold_build2_loc (loc
, MULT_EXPR
, type
,
8887 /* If the real or vector real constant CST of type TYPE has an exact
8888 inverse, return it, else return NULL. */
8891 exact_inverse (tree type
, tree cst
)
8894 tree unit_type
, *elts
;
8896 unsigned vec_nelts
, i
;
8898 switch (TREE_CODE (cst
))
8901 r
= TREE_REAL_CST (cst
);
8903 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8904 return build_real (type
, r
);
8909 vec_nelts
= VECTOR_CST_NELTS (cst
);
8910 elts
= XALLOCAVEC (tree
, vec_nelts
);
8911 unit_type
= TREE_TYPE (type
);
8912 mode
= TYPE_MODE (unit_type
);
8914 for (i
= 0; i
< vec_nelts
; i
++)
8916 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
8917 if (!exact_real_inverse (mode
, &r
))
8919 elts
[i
] = build_real (unit_type
, r
);
8922 return build_vector (type
, elts
);
8929 /* Mask out the tz least significant bits of X of type TYPE where
8930 tz is the number of trailing zeroes in Y. */
8932 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
8934 int tz
= wi::ctz (y
);
8936 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
8940 /* Return true when T is an address and is known to be nonzero.
8941 For floating point we further ensure that T is not denormal.
8942 Similar logic is present in nonzero_address in rtlanal.h.
8944 If the return value is based on the assumption that signed overflow
8945 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8946 change *STRICT_OVERFLOW_P. */
8949 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
8951 tree type
= TREE_TYPE (t
);
8952 enum tree_code code
;
8954 /* Doing something useful for floating point would need more work. */
8955 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
8958 code
= TREE_CODE (t
);
8959 switch (TREE_CODE_CLASS (code
))
8962 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8965 case tcc_comparison
:
8966 return tree_binary_nonzero_warnv_p (code
, type
,
8967 TREE_OPERAND (t
, 0),
8968 TREE_OPERAND (t
, 1),
8971 case tcc_declaration
:
8973 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8981 case TRUTH_NOT_EXPR
:
8982 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8985 case TRUTH_AND_EXPR
:
8987 case TRUTH_XOR_EXPR
:
8988 return tree_binary_nonzero_warnv_p (code
, type
,
8989 TREE_OPERAND (t
, 0),
8990 TREE_OPERAND (t
, 1),
8998 case WITH_SIZE_EXPR
:
9000 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9005 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9009 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9014 tree fndecl
= get_callee_fndecl (t
);
9015 if (!fndecl
) return false;
9016 if (flag_delete_null_pointer_checks
&& !flag_check_new
9017 && DECL_IS_OPERATOR_NEW (fndecl
)
9018 && !TREE_NOTHROW (fndecl
))
9020 if (flag_delete_null_pointer_checks
9021 && lookup_attribute ("returns_nonnull",
9022 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9024 return alloca_call_p (t
);
9033 /* Return true when T is an address and is known to be nonzero.
9034 Handle warnings about undefined signed overflow. */
9037 tree_expr_nonzero_p (tree t
)
9039 bool ret
, strict_overflow_p
;
9041 strict_overflow_p
= false;
9042 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9043 if (strict_overflow_p
)
9044 fold_overflow_warning (("assuming signed overflow does not occur when "
9045 "determining that expression is always "
9047 WARN_STRICT_OVERFLOW_MISC
);
9051 /* Return true if T is known not to be equal to an integer W. */
9054 expr_not_equal_to (tree t
, const wide_int
&w
)
9056 wide_int min
, max
, nz
;
9057 value_range_type rtype
;
9058 switch (TREE_CODE (t
))
9061 return wi::ne_p (t
, w
);
9064 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9066 rtype
= get_range_info (t
, &min
, &max
);
9067 if (rtype
== VR_RANGE
)
9069 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9071 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9074 else if (rtype
== VR_ANTI_RANGE
9075 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9076 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9078 /* If T has some known zero bits and W has any of those bits set,
9079 then T is known not to be equal to W. */
9080 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9081 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9090 /* Fold a binary expression of code CODE and type TYPE with operands
9091 OP0 and OP1. LOC is the location of the resulting expression.
9092 Return the folded expression if folding is successful. Otherwise,
9093 return NULL_TREE. */
9096 fold_binary_loc (location_t loc
,
9097 enum tree_code code
, tree type
, tree op0
, tree op1
)
9099 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9100 tree arg0
, arg1
, tem
;
9101 tree t1
= NULL_TREE
;
9102 bool strict_overflow_p
;
9105 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9106 && TREE_CODE_LENGTH (code
) == 2
9108 && op1
!= NULL_TREE
);
9113 /* Strip any conversions that don't change the mode. This is
9114 safe for every expression, except for a comparison expression
9115 because its signedness is derived from its operands. So, in
9116 the latter case, only strip conversions that don't change the
9117 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9120 Note that this is done as an internal manipulation within the
9121 constant folder, in order to find the simplest representation
9122 of the arguments so that their form can be studied. In any
9123 cases, the appropriate type conversions should be put back in
9124 the tree that will get out of the constant folder. */
9126 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9128 STRIP_SIGN_NOPS (arg0
);
9129 STRIP_SIGN_NOPS (arg1
);
9137 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9138 constant but we can't do arithmetic on them. */
9139 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9141 tem
= const_binop (code
, type
, arg0
, arg1
);
9142 if (tem
!= NULL_TREE
)
9144 if (TREE_TYPE (tem
) != type
)
9145 tem
= fold_convert_loc (loc
, type
, tem
);
9150 /* If this is a commutative operation, and ARG0 is a constant, move it
9151 to ARG1 to reduce the number of tests below. */
9152 if (commutative_tree_code (code
)
9153 && tree_swap_operands_p (arg0
, arg1
, true))
9154 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9156 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9157 to ARG1 to reduce the number of tests below. */
9158 if (kind
== tcc_comparison
9159 && tree_swap_operands_p (arg0
, arg1
, true))
9160 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9162 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9166 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9168 First check for cases where an arithmetic operation is applied to a
9169 compound, conditional, or comparison operation. Push the arithmetic
9170 operation inside the compound or conditional to see if any folding
9171 can then be done. Convert comparison to conditional for this purpose.
9172 The also optimizes non-constant cases that used to be done in
9175 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9176 one of the operands is a comparison and the other is a comparison, a
9177 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9178 code below would make the expression more complex. Change it to a
9179 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9180 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9182 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9183 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9184 && TREE_CODE (type
) != VECTOR_TYPE
9185 && ((truth_value_p (TREE_CODE (arg0
))
9186 && (truth_value_p (TREE_CODE (arg1
))
9187 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9188 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9189 || (truth_value_p (TREE_CODE (arg1
))
9190 && (truth_value_p (TREE_CODE (arg0
))
9191 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9192 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9194 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9195 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9198 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9199 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9201 if (code
== EQ_EXPR
)
9202 tem
= invert_truthvalue_loc (loc
, tem
);
9204 return fold_convert_loc (loc
, type
, tem
);
9207 if (TREE_CODE_CLASS (code
) == tcc_binary
9208 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9210 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9212 tem
= fold_build2_loc (loc
, code
, type
,
9213 fold_convert_loc (loc
, TREE_TYPE (op0
),
9214 TREE_OPERAND (arg0
, 1)), op1
);
9215 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9218 if (TREE_CODE (arg1
) == COMPOUND_EXPR
9219 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
9221 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9222 fold_convert_loc (loc
, TREE_TYPE (op1
),
9223 TREE_OPERAND (arg1
, 1)));
9224 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9228 if (TREE_CODE (arg0
) == COND_EXPR
9229 || TREE_CODE (arg0
) == VEC_COND_EXPR
9230 || COMPARISON_CLASS_P (arg0
))
9232 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9234 /*cond_first_p=*/1);
9235 if (tem
!= NULL_TREE
)
9239 if (TREE_CODE (arg1
) == COND_EXPR
9240 || TREE_CODE (arg1
) == VEC_COND_EXPR
9241 || COMPARISON_CLASS_P (arg1
))
9243 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9245 /*cond_first_p=*/0);
9246 if (tem
!= NULL_TREE
)
9254 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9255 if (TREE_CODE (arg0
) == ADDR_EXPR
9256 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9258 tree iref
= TREE_OPERAND (arg0
, 0);
9259 return fold_build2 (MEM_REF
, type
,
9260 TREE_OPERAND (iref
, 0),
9261 int_const_binop (PLUS_EXPR
, arg1
,
9262 TREE_OPERAND (iref
, 1)));
9265 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9266 if (TREE_CODE (arg0
) == ADDR_EXPR
9267 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9270 HOST_WIDE_INT coffset
;
9271 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9275 return fold_build2 (MEM_REF
, type
,
9276 build_fold_addr_expr (base
),
9277 int_const_binop (PLUS_EXPR
, arg1
,
9278 size_int (coffset
)));
9283 case POINTER_PLUS_EXPR
:
9284 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9285 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9286 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9287 return fold_convert_loc (loc
, type
,
9288 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9289 fold_convert_loc (loc
, sizetype
,
9291 fold_convert_loc (loc
, sizetype
,
9297 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9299 /* X + (X / CST) * -CST is X % CST. */
9300 if (TREE_CODE (arg1
) == MULT_EXPR
9301 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9302 && operand_equal_p (arg0
,
9303 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9305 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9306 tree cst1
= TREE_OPERAND (arg1
, 1);
9307 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9309 if (sum
&& integer_zerop (sum
))
9310 return fold_convert_loc (loc
, type
,
9311 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9312 TREE_TYPE (arg0
), arg0
,
9317 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9318 one. Make sure the type is not saturating and has the signedness of
9319 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9320 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9321 if ((TREE_CODE (arg0
) == MULT_EXPR
9322 || TREE_CODE (arg1
) == MULT_EXPR
)
9323 && !TYPE_SATURATING (type
)
9324 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9325 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9326 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9328 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9333 if (! FLOAT_TYPE_P (type
))
9335 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9336 (plus (plus (mult) (mult)) (foo)) so that we can
9337 take advantage of the factoring cases below. */
9338 if (ANY_INTEGRAL_TYPE_P (type
)
9339 && TYPE_OVERFLOW_WRAPS (type
)
9340 && (((TREE_CODE (arg0
) == PLUS_EXPR
9341 || TREE_CODE (arg0
) == MINUS_EXPR
)
9342 && TREE_CODE (arg1
) == MULT_EXPR
)
9343 || ((TREE_CODE (arg1
) == PLUS_EXPR
9344 || TREE_CODE (arg1
) == MINUS_EXPR
)
9345 && TREE_CODE (arg0
) == MULT_EXPR
)))
9347 tree parg0
, parg1
, parg
, marg
;
9348 enum tree_code pcode
;
9350 if (TREE_CODE (arg1
) == MULT_EXPR
)
9351 parg
= arg0
, marg
= arg1
;
9353 parg
= arg1
, marg
= arg0
;
9354 pcode
= TREE_CODE (parg
);
9355 parg0
= TREE_OPERAND (parg
, 0);
9356 parg1
= TREE_OPERAND (parg
, 1);
9360 if (TREE_CODE (parg0
) == MULT_EXPR
9361 && TREE_CODE (parg1
) != MULT_EXPR
)
9362 return fold_build2_loc (loc
, pcode
, type
,
9363 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9364 fold_convert_loc (loc
, type
,
9366 fold_convert_loc (loc
, type
,
9368 fold_convert_loc (loc
, type
, parg1
));
9369 if (TREE_CODE (parg0
) != MULT_EXPR
9370 && TREE_CODE (parg1
) == MULT_EXPR
)
9372 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9373 fold_convert_loc (loc
, type
, parg0
),
9374 fold_build2_loc (loc
, pcode
, type
,
9375 fold_convert_loc (loc
, type
, marg
),
9376 fold_convert_loc (loc
, type
,
9382 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9383 to __complex__ ( x, y ). This is not the same for SNaNs or
9384 if signed zeros are involved. */
9385 if (!HONOR_SNANS (element_mode (arg0
))
9386 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9387 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9389 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9390 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9391 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9392 bool arg0rz
= false, arg0iz
= false;
9393 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9394 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9396 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9397 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9398 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9400 tree rp
= arg1r
? arg1r
9401 : build1 (REALPART_EXPR
, rtype
, arg1
);
9402 tree ip
= arg0i
? arg0i
9403 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9404 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9406 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9408 tree rp
= arg0r
? arg0r
9409 : build1 (REALPART_EXPR
, rtype
, arg0
);
9410 tree ip
= arg1i
? arg1i
9411 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9412 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9417 if (flag_unsafe_math_optimizations
9418 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9419 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9420 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9423 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9424 We associate floats only if the user has specified
9425 -fassociative-math. */
9426 if (flag_associative_math
9427 && TREE_CODE (arg1
) == PLUS_EXPR
9428 && TREE_CODE (arg0
) != MULT_EXPR
)
9430 tree tree10
= TREE_OPERAND (arg1
, 0);
9431 tree tree11
= TREE_OPERAND (arg1
, 1);
9432 if (TREE_CODE (tree11
) == MULT_EXPR
9433 && TREE_CODE (tree10
) == MULT_EXPR
)
9436 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9437 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9440 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9441 We associate floats only if the user has specified
9442 -fassociative-math. */
9443 if (flag_associative_math
9444 && TREE_CODE (arg0
) == PLUS_EXPR
9445 && TREE_CODE (arg1
) != MULT_EXPR
)
9447 tree tree00
= TREE_OPERAND (arg0
, 0);
9448 tree tree01
= TREE_OPERAND (arg0
, 1);
9449 if (TREE_CODE (tree01
) == MULT_EXPR
9450 && TREE_CODE (tree00
) == MULT_EXPR
)
9453 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9454 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9460 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9461 is a rotate of A by C1 bits. */
9462 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9463 is a rotate of A by B bits. */
9465 enum tree_code code0
, code1
;
9467 code0
= TREE_CODE (arg0
);
9468 code1
= TREE_CODE (arg1
);
9469 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9470 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9471 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9472 TREE_OPERAND (arg1
, 0), 0)
9473 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9474 TYPE_UNSIGNED (rtype
))
9475 /* Only create rotates in complete modes. Other cases are not
9476 expanded properly. */
9477 && (element_precision (rtype
)
9478 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9480 tree tree01
, tree11
;
9481 enum tree_code code01
, code11
;
9483 tree01
= TREE_OPERAND (arg0
, 1);
9484 tree11
= TREE_OPERAND (arg1
, 1);
9485 STRIP_NOPS (tree01
);
9486 STRIP_NOPS (tree11
);
9487 code01
= TREE_CODE (tree01
);
9488 code11
= TREE_CODE (tree11
);
9489 if (code01
== INTEGER_CST
9490 && code11
== INTEGER_CST
9491 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9492 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9494 tem
= build2_loc (loc
, LROTATE_EXPR
,
9495 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9496 TREE_OPERAND (arg0
, 0),
9497 code0
== LSHIFT_EXPR
9498 ? TREE_OPERAND (arg0
, 1)
9499 : TREE_OPERAND (arg1
, 1));
9500 return fold_convert_loc (loc
, type
, tem
);
9502 else if (code11
== MINUS_EXPR
)
9504 tree tree110
, tree111
;
9505 tree110
= TREE_OPERAND (tree11
, 0);
9506 tree111
= TREE_OPERAND (tree11
, 1);
9507 STRIP_NOPS (tree110
);
9508 STRIP_NOPS (tree111
);
9509 if (TREE_CODE (tree110
) == INTEGER_CST
9510 && 0 == compare_tree_int (tree110
,
9512 (TREE_TYPE (TREE_OPERAND
9514 && operand_equal_p (tree01
, tree111
, 0))
9516 fold_convert_loc (loc
, type
,
9517 build2 ((code0
== LSHIFT_EXPR
9520 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9521 TREE_OPERAND (arg0
, 0),
9522 TREE_OPERAND (arg0
, 1)));
9524 else if (code01
== MINUS_EXPR
)
9526 tree tree010
, tree011
;
9527 tree010
= TREE_OPERAND (tree01
, 0);
9528 tree011
= TREE_OPERAND (tree01
, 1);
9529 STRIP_NOPS (tree010
);
9530 STRIP_NOPS (tree011
);
9531 if (TREE_CODE (tree010
) == INTEGER_CST
9532 && 0 == compare_tree_int (tree010
,
9534 (TREE_TYPE (TREE_OPERAND
9536 && operand_equal_p (tree11
, tree011
, 0))
9537 return fold_convert_loc
9539 build2 ((code0
!= LSHIFT_EXPR
9542 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9543 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9549 /* In most languages, can't associate operations on floats through
9550 parentheses. Rather than remember where the parentheses were, we
9551 don't associate floats at all, unless the user has specified
9553 And, we need to make sure type is not saturating. */
9555 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9556 && !TYPE_SATURATING (type
))
9558 tree var0
, con0
, lit0
, minus_lit0
;
9559 tree var1
, con1
, lit1
, minus_lit1
;
9563 /* Split both trees into variables, constants, and literals. Then
9564 associate each group together, the constants with literals,
9565 then the result with variables. This increases the chances of
9566 literals being recombined later and of generating relocatable
9567 expressions for the sum of a constant and literal. */
9568 var0
= split_tree (loc
, arg0
, type
, code
,
9569 &con0
, &lit0
, &minus_lit0
, 0);
9570 var1
= split_tree (loc
, arg1
, type
, code
,
9571 &con1
, &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9573 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9574 if (code
== MINUS_EXPR
)
9577 /* With undefined overflow prefer doing association in a type
9578 which wraps on overflow, if that is one of the operand types. */
9579 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9580 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9582 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9583 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9584 atype
= TREE_TYPE (arg0
);
9585 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9586 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9587 atype
= TREE_TYPE (arg1
);
9588 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9591 /* With undefined overflow we can only associate constants with one
9592 variable, and constants whose association doesn't overflow. */
9593 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9594 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9600 bool one_neg
= false;
9602 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9604 tmp0
= TREE_OPERAND (tmp0
, 0);
9607 if (CONVERT_EXPR_P (tmp0
)
9608 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9609 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9610 <= TYPE_PRECISION (atype
)))
9611 tmp0
= TREE_OPERAND (tmp0
, 0);
9612 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9614 tmp1
= TREE_OPERAND (tmp1
, 0);
9617 if (CONVERT_EXPR_P (tmp1
)
9618 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9619 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9620 <= TYPE_PRECISION (atype
)))
9621 tmp1
= TREE_OPERAND (tmp1
, 0);
9622 /* The only case we can still associate with two variables
9623 is if they cancel out. */
9625 || !operand_equal_p (tmp0
, tmp1
, 0))
9630 /* Only do something if we found more than two objects. Otherwise,
9631 nothing has changed and we risk infinite recursion. */
9633 && (2 < ((var0
!= 0) + (var1
!= 0)
9634 + (con0
!= 0) + (con1
!= 0)
9635 + (lit0
!= 0) + (lit1
!= 0)
9636 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9638 bool any_overflows
= false;
9639 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
9640 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
9641 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
9642 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
9643 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9644 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9645 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9646 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9649 /* Preserve the MINUS_EXPR if the negative part of the literal is
9650 greater than the positive part. Otherwise, the multiplicative
9651 folding code (i.e extract_muldiv) may be fooled in case
9652 unsigned constants are subtracted, like in the following
9653 example: ((X*2 + 4) - 8U)/2. */
9654 if (minus_lit0
&& lit0
)
9656 if (TREE_CODE (lit0
) == INTEGER_CST
9657 && TREE_CODE (minus_lit0
) == INTEGER_CST
9658 && tree_int_cst_lt (lit0
, minus_lit0
))
9660 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9666 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9672 /* Don't introduce overflows through reassociation. */
9674 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
9675 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
9682 fold_convert_loc (loc
, type
,
9683 associate_trees (loc
, var0
, minus_lit0
,
9684 MINUS_EXPR
, atype
));
9687 con0
= associate_trees (loc
, con0
, minus_lit0
,
9690 fold_convert_loc (loc
, type
,
9691 associate_trees (loc
, var0
, con0
,
9696 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9698 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9706 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9707 if (TREE_CODE (arg0
) == NEGATE_EXPR
9708 && negate_expr_p (op1
)
9709 && reorder_operands_p (arg0
, arg1
))
9710 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9712 fold_convert_loc (loc
, type
,
9713 TREE_OPERAND (arg0
, 0)));
9715 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9716 __complex__ ( x, -y ). This is not the same for SNaNs or if
9717 signed zeros are involved. */
9718 if (!HONOR_SNANS (element_mode (arg0
))
9719 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9720 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9722 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9723 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9724 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9725 bool arg0rz
= false, arg0iz
= false;
9726 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9727 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9729 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9730 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9731 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9733 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9735 : build1 (REALPART_EXPR
, rtype
, arg1
));
9736 tree ip
= arg0i
? arg0i
9737 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9738 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9740 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9742 tree rp
= arg0r
? arg0r
9743 : build1 (REALPART_EXPR
, rtype
, arg0
);
9744 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9746 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9747 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9752 /* A - B -> A + (-B) if B is easily negatable. */
9753 if (negate_expr_p (op1
)
9754 && ! TYPE_OVERFLOW_SANITIZED (type
)
9755 && ((FLOAT_TYPE_P (type
)
9756 /* Avoid this transformation if B is a positive REAL_CST. */
9757 && (TREE_CODE (op1
) != REAL_CST
9758 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9759 || INTEGRAL_TYPE_P (type
)))
9760 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9761 fold_convert_loc (loc
, type
, arg0
),
9764 /* Fold &a[i] - &a[j] to i-j. */
9765 if (TREE_CODE (arg0
) == ADDR_EXPR
9766 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9767 && TREE_CODE (arg1
) == ADDR_EXPR
9768 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9770 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9771 TREE_OPERAND (arg0
, 0),
9772 TREE_OPERAND (arg1
, 0));
9777 if (FLOAT_TYPE_P (type
)
9778 && flag_unsafe_math_optimizations
9779 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9780 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9781 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9784 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9785 one. Make sure the type is not saturating and has the signedness of
9786 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9787 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9788 if ((TREE_CODE (arg0
) == MULT_EXPR
9789 || TREE_CODE (arg1
) == MULT_EXPR
)
9790 && !TYPE_SATURATING (type
)
9791 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9792 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9793 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9795 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9803 if (! FLOAT_TYPE_P (type
))
9805 /* Transform x * -C into -x * C if x is easily negatable. */
9806 if (TREE_CODE (op1
) == INTEGER_CST
9807 && tree_int_cst_sgn (op1
) == -1
9808 && negate_expr_p (op0
)
9809 && (tem
= negate_expr (op1
)) != op1
9810 && ! TREE_OVERFLOW (tem
))
9811 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9812 fold_convert_loc (loc
, type
,
9813 negate_expr (op0
)), tem
);
9815 strict_overflow_p
= false;
9816 if (TREE_CODE (arg1
) == INTEGER_CST
9817 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9818 &strict_overflow_p
)))
9820 if (strict_overflow_p
)
9821 fold_overflow_warning (("assuming signed overflow does not "
9822 "occur when simplifying "
9824 WARN_STRICT_OVERFLOW_MISC
);
9825 return fold_convert_loc (loc
, type
, tem
);
9828 /* Optimize z * conj(z) for integer complex numbers. */
9829 if (TREE_CODE (arg0
) == CONJ_EXPR
9830 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9831 return fold_mult_zconjz (loc
, type
, arg1
);
9832 if (TREE_CODE (arg1
) == CONJ_EXPR
9833 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9834 return fold_mult_zconjz (loc
, type
, arg0
);
9838 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9839 This is not the same for NaNs or if signed zeros are
9841 if (!HONOR_NANS (arg0
)
9842 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9843 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9844 && TREE_CODE (arg1
) == COMPLEX_CST
9845 && real_zerop (TREE_REALPART (arg1
)))
9847 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9848 if (real_onep (TREE_IMAGPART (arg1
)))
9850 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9851 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9853 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9854 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9856 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9857 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9858 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9862 /* Optimize z * conj(z) for floating point complex numbers.
9863 Guarded by flag_unsafe_math_optimizations as non-finite
9864 imaginary components don't produce scalar results. */
9865 if (flag_unsafe_math_optimizations
9866 && TREE_CODE (arg0
) == CONJ_EXPR
9867 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9868 return fold_mult_zconjz (loc
, type
, arg1
);
9869 if (flag_unsafe_math_optimizations
9870 && TREE_CODE (arg1
) == CONJ_EXPR
9871 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9872 return fold_mult_zconjz (loc
, type
, arg0
);
9877 /* Canonicalize (X & C1) | C2. */
9878 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9879 && TREE_CODE (arg1
) == INTEGER_CST
9880 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9882 int width
= TYPE_PRECISION (type
), w
;
9883 wide_int c1
= TREE_OPERAND (arg0
, 1);
9886 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9887 if ((c1
& c2
) == c1
)
9888 return omit_one_operand_loc (loc
, type
, arg1
,
9889 TREE_OPERAND (arg0
, 0));
9891 wide_int msk
= wi::mask (width
, false,
9892 TYPE_PRECISION (TREE_TYPE (arg1
)));
9894 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9895 if (msk
.and_not (c1
| c2
) == 0)
9896 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9897 TREE_OPERAND (arg0
, 0), arg1
);
9899 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9900 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9901 mode which allows further optimizations. */
9904 wide_int c3
= c1
.and_not (c2
);
9905 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
9907 wide_int mask
= wi::mask (w
, false,
9908 TYPE_PRECISION (type
));
9909 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
9917 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9918 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9919 TREE_OPERAND (arg0
, 0),
9920 wide_int_to_tree (type
,
9925 /* See if this can be simplified into a rotate first. If that
9926 is unsuccessful continue in the association code. */
9930 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9931 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9932 && INTEGRAL_TYPE_P (type
)
9933 && integer_onep (TREE_OPERAND (arg0
, 1))
9934 && integer_onep (arg1
))
9935 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
9936 build_zero_cst (TREE_TYPE (arg0
)));
9938 /* See if this can be simplified into a rotate first. If that
9939 is unsuccessful continue in the association code. */
9943 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9944 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9945 && INTEGRAL_TYPE_P (type
)
9946 && integer_onep (TREE_OPERAND (arg0
, 1))
9947 && integer_onep (arg1
))
9950 tem
= TREE_OPERAND (arg0
, 0);
9951 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9952 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9954 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9955 build_zero_cst (TREE_TYPE (tem
)));
9957 /* Fold ~X & 1 as (X & 1) == 0. */
9958 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9959 && INTEGRAL_TYPE_P (type
)
9960 && integer_onep (arg1
))
9963 tem
= TREE_OPERAND (arg0
, 0);
9964 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9965 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9967 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9968 build_zero_cst (TREE_TYPE (tem
)));
9970 /* Fold !X & 1 as X == 0. */
9971 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
9972 && integer_onep (arg1
))
9974 tem
= TREE_OPERAND (arg0
, 0);
9975 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
9976 build_zero_cst (TREE_TYPE (tem
)));
9979 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9980 multiple of 1 << CST. */
9981 if (TREE_CODE (arg1
) == INTEGER_CST
)
9983 wide_int cst1
= arg1
;
9984 wide_int ncst1
= -cst1
;
9985 if ((cst1
& ncst1
) == ncst1
9986 && multiple_of_p (type
, arg0
,
9987 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
9988 return fold_convert_loc (loc
, type
, arg0
);
9991 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9993 if (TREE_CODE (arg1
) == INTEGER_CST
9994 && TREE_CODE (arg0
) == MULT_EXPR
9995 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9997 wide_int warg1
= arg1
;
9998 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
10001 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10003 else if (masked
!= warg1
)
10005 /* Avoid the transform if arg1 is a mask of some
10006 mode which allows further optimizations. */
10007 int pop
= wi::popcount (warg1
);
10008 if (!(pop
>= BITS_PER_UNIT
10009 && exact_log2 (pop
) != -1
10010 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10011 return fold_build2_loc (loc
, code
, type
, op0
,
10012 wide_int_to_tree (type
, masked
));
10016 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10017 ((A & N) + B) & M -> (A + B) & M
10018 Similarly if (N & M) == 0,
10019 ((A | N) + B) & M -> (A + B) & M
10020 and for - instead of + (or unary - instead of +)
10021 and/or ^ instead of |.
10022 If B is constant and (B & M) == 0, fold into A & M. */
10023 if (TREE_CODE (arg1
) == INTEGER_CST
)
10025 wide_int cst1
= arg1
;
10026 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10027 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10028 && (TREE_CODE (arg0
) == PLUS_EXPR
10029 || TREE_CODE (arg0
) == MINUS_EXPR
10030 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10031 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10032 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10038 /* Now we know that arg0 is (C + D) or (C - D) or
10039 -C and arg1 (M) is == (1LL << cst) - 1.
10040 Store C into PMOP[0] and D into PMOP[1]. */
10041 pmop
[0] = TREE_OPERAND (arg0
, 0);
10043 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10045 pmop
[1] = TREE_OPERAND (arg0
, 1);
10049 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10052 for (; which
>= 0; which
--)
10053 switch (TREE_CODE (pmop
[which
]))
10058 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10061 cst0
= TREE_OPERAND (pmop
[which
], 1);
10063 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10068 else if (cst0
!= 0)
10070 /* If C or D is of the form (A & N) where
10071 (N & M) == M, or of the form (A | N) or
10072 (A ^ N) where (N & M) == 0, replace it with A. */
10073 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10076 /* If C or D is a N where (N & M) == 0, it can be
10077 omitted (assumed 0). */
10078 if ((TREE_CODE (arg0
) == PLUS_EXPR
10079 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10080 && (cst1
& pmop
[which
]) == 0)
10081 pmop
[which
] = NULL
;
10087 /* Only build anything new if we optimized one or both arguments
10089 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10090 || (TREE_CODE (arg0
) != NEGATE_EXPR
10091 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10093 tree utype
= TREE_TYPE (arg0
);
10094 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10096 /* Perform the operations in a type that has defined
10097 overflow behavior. */
10098 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10099 if (pmop
[0] != NULL
)
10100 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10101 if (pmop
[1] != NULL
)
10102 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10105 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10106 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10107 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10109 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10110 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10112 else if (pmop
[0] != NULL
)
10114 else if (pmop
[1] != NULL
)
10117 return build_int_cst (type
, 0);
10119 else if (pmop
[0] == NULL
)
10120 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10122 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10124 /* TEM is now the new binary +, - or unary - replacement. */
10125 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10126 fold_convert_loc (loc
, utype
, arg1
));
10127 return fold_convert_loc (loc
, type
, tem
);
10132 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10133 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10134 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10136 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10138 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10141 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10147 /* Don't touch a floating-point divide by zero unless the mode
10148 of the constant can represent infinity. */
10149 if (TREE_CODE (arg1
) == REAL_CST
10150 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10151 && real_zerop (arg1
))
10154 /* (-A) / (-B) -> A / B */
10155 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10156 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10157 TREE_OPERAND (arg0
, 0),
10158 negate_expr (arg1
));
10159 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10160 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10161 negate_expr (arg0
),
10162 TREE_OPERAND (arg1
, 0));
10165 case TRUNC_DIV_EXPR
:
10168 case FLOOR_DIV_EXPR
:
10169 /* Simplify A / (B << N) where A and B are positive and B is
10170 a power of 2, to A >> (N + log2(B)). */
10171 strict_overflow_p
= false;
10172 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10173 && (TYPE_UNSIGNED (type
)
10174 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10176 tree sval
= TREE_OPERAND (arg1
, 0);
10177 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10179 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10180 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10181 wi::exact_log2 (sval
));
10183 if (strict_overflow_p
)
10184 fold_overflow_warning (("assuming signed overflow does not "
10185 "occur when simplifying A / (B << N)"),
10186 WARN_STRICT_OVERFLOW_MISC
);
10188 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10190 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10191 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10197 case ROUND_DIV_EXPR
:
10198 case CEIL_DIV_EXPR
:
10199 case EXACT_DIV_EXPR
:
10200 if (integer_zerop (arg1
))
10203 /* Convert -A / -B to A / B when the type is signed and overflow is
10205 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10206 && TREE_CODE (arg0
) == NEGATE_EXPR
10207 && negate_expr_p (op1
))
10209 if (INTEGRAL_TYPE_P (type
))
10210 fold_overflow_warning (("assuming signed overflow does not occur "
10211 "when distributing negation across "
10213 WARN_STRICT_OVERFLOW_MISC
);
10214 return fold_build2_loc (loc
, code
, type
,
10215 fold_convert_loc (loc
, type
,
10216 TREE_OPERAND (arg0
, 0)),
10217 negate_expr (op1
));
10219 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10220 && TREE_CODE (arg1
) == NEGATE_EXPR
10221 && negate_expr_p (op0
))
10223 if (INTEGRAL_TYPE_P (type
))
10224 fold_overflow_warning (("assuming signed overflow does not occur "
10225 "when distributing negation across "
10227 WARN_STRICT_OVERFLOW_MISC
);
10228 return fold_build2_loc (loc
, code
, type
,
10230 fold_convert_loc (loc
, type
,
10231 TREE_OPERAND (arg1
, 0)));
10234 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10235 operation, EXACT_DIV_EXPR.
10237 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10238 At one time others generated faster code, it's not clear if they do
10239 after the last round to changes to the DIV code in expmed.c. */
10240 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10241 && multiple_of_p (type
, arg0
, arg1
))
10242 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10243 fold_convert (type
, arg0
),
10244 fold_convert (type
, arg1
));
10246 strict_overflow_p
= false;
10247 if (TREE_CODE (arg1
) == INTEGER_CST
10248 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10249 &strict_overflow_p
)))
10251 if (strict_overflow_p
)
10252 fold_overflow_warning (("assuming signed overflow does not occur "
10253 "when simplifying division"),
10254 WARN_STRICT_OVERFLOW_MISC
);
10255 return fold_convert_loc (loc
, type
, tem
);
10260 case CEIL_MOD_EXPR
:
10261 case FLOOR_MOD_EXPR
:
10262 case ROUND_MOD_EXPR
:
10263 case TRUNC_MOD_EXPR
:
10264 strict_overflow_p
= false;
10265 if (TREE_CODE (arg1
) == INTEGER_CST
10266 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10267 &strict_overflow_p
)))
10269 if (strict_overflow_p
)
10270 fold_overflow_warning (("assuming signed overflow does not occur "
10271 "when simplifying modulus"),
10272 WARN_STRICT_OVERFLOW_MISC
);
10273 return fold_convert_loc (loc
, type
, tem
);
10282 /* Since negative shift count is not well-defined,
10283 don't try to compute it in the compiler. */
10284 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10287 prec
= element_precision (type
);
10289 /* If we have a rotate of a bit operation with the rotate count and
10290 the second operand of the bit operation both constant,
10291 permute the two operations. */
10292 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10293 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10294 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10295 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10296 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10297 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10298 fold_build2_loc (loc
, code
, type
,
10299 TREE_OPERAND (arg0
, 0), arg1
),
10300 fold_build2_loc (loc
, code
, type
,
10301 TREE_OPERAND (arg0
, 1), arg1
));
10303 /* Two consecutive rotates adding up to the some integer
10304 multiple of the precision of the type can be ignored. */
10305 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10306 && TREE_CODE (arg0
) == RROTATE_EXPR
10307 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10308 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10310 return TREE_OPERAND (arg0
, 0);
10318 case TRUTH_ANDIF_EXPR
:
10319 /* Note that the operands of this must be ints
10320 and their values must be 0 or 1.
10321 ("true" is a fixed value perhaps depending on the language.) */
10322 /* If first arg is constant zero, return it. */
10323 if (integer_zerop (arg0
))
10324 return fold_convert_loc (loc
, type
, arg0
);
10325 case TRUTH_AND_EXPR
:
10326 /* If either arg is constant true, drop it. */
10327 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10328 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10329 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10330 /* Preserve sequence points. */
10331 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10332 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10333 /* If second arg is constant zero, result is zero, but first arg
10334 must be evaluated. */
10335 if (integer_zerop (arg1
))
10336 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10337 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10338 case will be handled here. */
10339 if (integer_zerop (arg0
))
10340 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10342 /* !X && X is always false. */
10343 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10344 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10345 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10346 /* X && !X is always false. */
10347 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10348 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10349 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10351 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10352 means A >= Y && A != MAX, but in this case we know that
10355 if (!TREE_SIDE_EFFECTS (arg0
)
10356 && !TREE_SIDE_EFFECTS (arg1
))
10358 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10359 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10360 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10362 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10363 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10364 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10367 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10373 case TRUTH_ORIF_EXPR
:
10374 /* Note that the operands of this must be ints
10375 and their values must be 0 or true.
10376 ("true" is a fixed value perhaps depending on the language.) */
10377 /* If first arg is constant true, return it. */
10378 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10379 return fold_convert_loc (loc
, type
, arg0
);
10380 case TRUTH_OR_EXPR
:
10381 /* If either arg is constant zero, drop it. */
10382 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10383 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10384 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10385 /* Preserve sequence points. */
10386 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10387 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10388 /* If second arg is constant true, result is true, but we must
10389 evaluate first arg. */
10390 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10391 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10392 /* Likewise for first arg, but note this only occurs here for
10394 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10395 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10397 /* !X || X is always true. */
10398 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10399 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10400 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10401 /* X || !X is always true. */
10402 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10403 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10404 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10406 /* (X && !Y) || (!X && Y) is X ^ Y */
10407 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10408 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10410 tree a0
, a1
, l0
, l1
, n0
, n1
;
10412 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10413 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10415 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10416 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10418 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10419 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10421 if ((operand_equal_p (n0
, a0
, 0)
10422 && operand_equal_p (n1
, a1
, 0))
10423 || (operand_equal_p (n0
, a1
, 0)
10424 && operand_equal_p (n1
, a0
, 0)))
10425 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10428 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10434 case TRUTH_XOR_EXPR
:
10435 /* If the second arg is constant zero, drop it. */
10436 if (integer_zerop (arg1
))
10437 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10438 /* If the second arg is constant true, this is a logical inversion. */
10439 if (integer_onep (arg1
))
10441 tem
= invert_truthvalue_loc (loc
, arg0
);
10442 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10444 /* Identical arguments cancel to zero. */
10445 if (operand_equal_p (arg0
, arg1
, 0))
10446 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10448 /* !X ^ X is always true. */
10449 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10450 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10451 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10453 /* X ^ !X is always true. */
10454 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10455 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10456 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10465 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10466 if (tem
!= NULL_TREE
)
10469 /* bool_var != 1 becomes !bool_var. */
10470 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10471 && code
== NE_EXPR
)
10472 return fold_convert_loc (loc
, type
,
10473 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10474 TREE_TYPE (arg0
), arg0
));
10476 /* bool_var == 0 becomes !bool_var. */
10477 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10478 && code
== EQ_EXPR
)
10479 return fold_convert_loc (loc
, type
,
10480 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10481 TREE_TYPE (arg0
), arg0
));
10483 /* !exp != 0 becomes !exp */
10484 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10485 && code
== NE_EXPR
)
10486 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10488 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10489 if ((TREE_CODE (arg0
) == PLUS_EXPR
10490 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10491 || TREE_CODE (arg0
) == MINUS_EXPR
)
10492 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10495 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10496 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10498 tree val
= TREE_OPERAND (arg0
, 1);
10499 val
= fold_build2_loc (loc
, code
, type
, val
,
10500 build_int_cst (TREE_TYPE (val
), 0));
10501 return omit_two_operands_loc (loc
, type
, val
,
10502 TREE_OPERAND (arg0
, 0), arg1
);
10505 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10506 if ((TREE_CODE (arg1
) == PLUS_EXPR
10507 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
10508 || TREE_CODE (arg1
) == MINUS_EXPR
)
10509 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10512 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10513 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
10515 tree val
= TREE_OPERAND (arg1
, 1);
10516 val
= fold_build2_loc (loc
, code
, type
, val
,
10517 build_int_cst (TREE_TYPE (val
), 0));
10518 return omit_two_operands_loc (loc
, type
, val
,
10519 TREE_OPERAND (arg1
, 0), arg0
);
10522 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10523 if (TREE_CODE (arg0
) == MINUS_EXPR
10524 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
10525 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10528 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
10529 return omit_two_operands_loc (loc
, type
,
10531 ? boolean_true_node
: boolean_false_node
,
10532 TREE_OPERAND (arg0
, 1), arg1
);
10534 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10535 if (TREE_CODE (arg1
) == MINUS_EXPR
10536 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == INTEGER_CST
10537 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10540 && wi::extract_uhwi (TREE_OPERAND (arg1
, 0), 0, 1) == 1)
10541 return omit_two_operands_loc (loc
, type
,
10543 ? boolean_true_node
: boolean_false_node
,
10544 TREE_OPERAND (arg1
, 1), arg0
);
10546 /* If this is an EQ or NE comparison with zero and ARG0 is
10547 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10548 two operations, but the latter can be done in one less insn
10549 on machines that have only two-operand insns or on which a
10550 constant cannot be the first operand. */
10551 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10552 && integer_zerop (arg1
))
10554 tree arg00
= TREE_OPERAND (arg0
, 0);
10555 tree arg01
= TREE_OPERAND (arg0
, 1);
10556 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10557 && integer_onep (TREE_OPERAND (arg00
, 0)))
10559 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10560 arg01
, TREE_OPERAND (arg00
, 1));
10561 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10562 build_int_cst (TREE_TYPE (arg0
), 1));
10563 return fold_build2_loc (loc
, code
, type
,
10564 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10567 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10568 && integer_onep (TREE_OPERAND (arg01
, 0)))
10570 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10571 arg00
, TREE_OPERAND (arg01
, 1));
10572 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10573 build_int_cst (TREE_TYPE (arg0
), 1));
10574 return fold_build2_loc (loc
, code
, type
,
10575 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10580 /* If this is an NE or EQ comparison of zero against the result of a
10581 signed MOD operation whose second operand is a power of 2, make
10582 the MOD operation unsigned since it is simpler and equivalent. */
10583 if (integer_zerop (arg1
)
10584 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10585 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10586 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10587 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10588 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10589 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10591 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10592 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10593 fold_convert_loc (loc
, newtype
,
10594 TREE_OPERAND (arg0
, 0)),
10595 fold_convert_loc (loc
, newtype
,
10596 TREE_OPERAND (arg0
, 1)));
10598 return fold_build2_loc (loc
, code
, type
, newmod
,
10599 fold_convert_loc (loc
, newtype
, arg1
));
10602 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10603 C1 is a valid shift constant, and C2 is a power of two, i.e.
10605 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10606 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10607 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10609 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10610 && integer_zerop (arg1
))
10612 tree itype
= TREE_TYPE (arg0
);
10613 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10614 prec
= TYPE_PRECISION (itype
);
10616 /* Check for a valid shift count. */
10617 if (wi::ltu_p (arg001
, prec
))
10619 tree arg01
= TREE_OPERAND (arg0
, 1);
10620 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10621 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10622 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10623 can be rewritten as (X & (C2 << C1)) != 0. */
10624 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10626 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10627 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10628 return fold_build2_loc (loc
, code
, type
, tem
,
10629 fold_convert_loc (loc
, itype
, arg1
));
10631 /* Otherwise, for signed (arithmetic) shifts,
10632 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10633 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10634 else if (!TYPE_UNSIGNED (itype
))
10635 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10636 arg000
, build_int_cst (itype
, 0));
10637 /* Otherwise, of unsigned (logical) shifts,
10638 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10639 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10641 return omit_one_operand_loc (loc
, type
,
10642 code
== EQ_EXPR
? integer_one_node
10643 : integer_zero_node
,
10648 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10649 Similarly for NE_EXPR. */
10650 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10651 && TREE_CODE (arg1
) == INTEGER_CST
10652 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10654 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
10655 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
10656 TREE_OPERAND (arg0
, 1));
10658 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10659 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
10661 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
10662 if (integer_nonzerop (dandnotc
))
10663 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
10666 /* If this is a comparison of a field, we may be able to simplify it. */
10667 if ((TREE_CODE (arg0
) == COMPONENT_REF
10668 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10669 /* Handle the constant case even without -O
10670 to make sure the warnings are given. */
10671 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10673 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10678 /* Optimize comparisons of strlen vs zero to a compare of the
10679 first character of the string vs zero. To wit,
10680 strlen(ptr) == 0 => *ptr == 0
10681 strlen(ptr) != 0 => *ptr != 0
10682 Other cases should reduce to one of these two (or a constant)
10683 due to the return value of strlen being unsigned. */
10684 if (TREE_CODE (arg0
) == CALL_EXPR
10685 && integer_zerop (arg1
))
10687 tree fndecl
= get_callee_fndecl (arg0
);
10690 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10691 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10692 && call_expr_nargs (arg0
) == 1
10693 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10695 tree iref
= build_fold_indirect_ref_loc (loc
,
10696 CALL_EXPR_ARG (arg0
, 0));
10697 return fold_build2_loc (loc
, code
, type
, iref
,
10698 build_int_cst (TREE_TYPE (iref
), 0));
10702 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10703 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10704 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10705 && integer_zerop (arg1
)
10706 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10708 tree arg00
= TREE_OPERAND (arg0
, 0);
10709 tree arg01
= TREE_OPERAND (arg0
, 1);
10710 tree itype
= TREE_TYPE (arg00
);
10711 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10713 if (TYPE_UNSIGNED (itype
))
10715 itype
= signed_type_for (itype
);
10716 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10718 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10719 type
, arg00
, build_zero_cst (itype
));
10723 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10724 (X & C) == 0 when C is a single bit. */
10725 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10726 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10727 && integer_zerop (arg1
)
10728 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10730 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10731 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10732 TREE_OPERAND (arg0
, 1));
10733 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10735 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10739 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10740 constant C is a power of two, i.e. a single bit. */
10741 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10742 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10743 && integer_zerop (arg1
)
10744 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10745 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10746 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10748 tree arg00
= TREE_OPERAND (arg0
, 0);
10749 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10750 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10753 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10754 when is C is a power of two, i.e. a single bit. */
10755 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10756 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10757 && integer_zerop (arg1
)
10758 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10759 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10760 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10762 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10763 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10764 arg000
, TREE_OPERAND (arg0
, 1));
10765 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10766 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10769 if (integer_zerop (arg1
)
10770 && tree_expr_nonzero_p (arg0
))
10772 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10773 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10776 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10777 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10778 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10780 tree arg00
= TREE_OPERAND (arg0
, 0);
10781 tree arg01
= TREE_OPERAND (arg0
, 1);
10782 tree arg10
= TREE_OPERAND (arg1
, 0);
10783 tree arg11
= TREE_OPERAND (arg1
, 1);
10784 tree itype
= TREE_TYPE (arg0
);
10786 if (operand_equal_p (arg01
, arg11
, 0))
10787 return fold_build2_loc (loc
, code
, type
,
10788 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10789 fold_build2_loc (loc
,
10790 BIT_XOR_EXPR
, itype
,
10793 build_zero_cst (itype
));
10795 if (operand_equal_p (arg01
, arg10
, 0))
10796 return fold_build2_loc (loc
, code
, type
,
10797 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10798 fold_build2_loc (loc
,
10799 BIT_XOR_EXPR
, itype
,
10802 build_zero_cst (itype
));
10804 if (operand_equal_p (arg00
, arg11
, 0))
10805 return fold_build2_loc (loc
, code
, type
,
10806 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10807 fold_build2_loc (loc
,
10808 BIT_XOR_EXPR
, itype
,
10811 build_zero_cst (itype
));
10813 if (operand_equal_p (arg00
, arg10
, 0))
10814 return fold_build2_loc (loc
, code
, type
,
10815 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10816 fold_build2_loc (loc
,
10817 BIT_XOR_EXPR
, itype
,
10820 build_zero_cst (itype
));
10823 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10824 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10826 tree arg00
= TREE_OPERAND (arg0
, 0);
10827 tree arg01
= TREE_OPERAND (arg0
, 1);
10828 tree arg10
= TREE_OPERAND (arg1
, 0);
10829 tree arg11
= TREE_OPERAND (arg1
, 1);
10830 tree itype
= TREE_TYPE (arg0
);
10832 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10833 operand_equal_p guarantees no side-effects so we don't need
10834 to use omit_one_operand on Z. */
10835 if (operand_equal_p (arg01
, arg11
, 0))
10836 return fold_build2_loc (loc
, code
, type
, arg00
,
10837 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10839 if (operand_equal_p (arg01
, arg10
, 0))
10840 return fold_build2_loc (loc
, code
, type
, arg00
,
10841 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10843 if (operand_equal_p (arg00
, arg11
, 0))
10844 return fold_build2_loc (loc
, code
, type
, arg01
,
10845 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10847 if (operand_equal_p (arg00
, arg10
, 0))
10848 return fold_build2_loc (loc
, code
, type
, arg01
,
10849 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10852 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10853 if (TREE_CODE (arg01
) == INTEGER_CST
10854 && TREE_CODE (arg11
) == INTEGER_CST
)
10856 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10857 fold_convert_loc (loc
, itype
, arg11
));
10858 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10859 return fold_build2_loc (loc
, code
, type
, tem
,
10860 fold_convert_loc (loc
, itype
, arg10
));
10864 /* Attempt to simplify equality/inequality comparisons of complex
10865 values. Only lower the comparison if the result is known or
10866 can be simplified to a single scalar comparison. */
10867 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10868 || TREE_CODE (arg0
) == COMPLEX_CST
)
10869 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10870 || TREE_CODE (arg1
) == COMPLEX_CST
))
10872 tree real0
, imag0
, real1
, imag1
;
10875 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10877 real0
= TREE_OPERAND (arg0
, 0);
10878 imag0
= TREE_OPERAND (arg0
, 1);
10882 real0
= TREE_REALPART (arg0
);
10883 imag0
= TREE_IMAGPART (arg0
);
10886 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
10888 real1
= TREE_OPERAND (arg1
, 0);
10889 imag1
= TREE_OPERAND (arg1
, 1);
10893 real1
= TREE_REALPART (arg1
);
10894 imag1
= TREE_IMAGPART (arg1
);
10897 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
10898 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
10900 if (integer_zerop (rcond
))
10902 if (code
== EQ_EXPR
)
10903 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10905 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
10909 if (code
== NE_EXPR
)
10910 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10912 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
10916 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
10917 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
10919 if (integer_zerop (icond
))
10921 if (code
== EQ_EXPR
)
10922 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10924 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
10928 if (code
== NE_EXPR
)
10929 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10931 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
10942 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10943 if (tem
!= NULL_TREE
)
10946 /* Transform comparisons of the form X +- C CMP X. */
10947 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10948 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10949 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
10950 && !HONOR_SNANS (arg0
))
10951 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10952 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
10954 tree arg01
= TREE_OPERAND (arg0
, 1);
10955 enum tree_code code0
= TREE_CODE (arg0
);
10958 if (TREE_CODE (arg01
) == REAL_CST
)
10959 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
10961 is_positive
= tree_int_cst_sgn (arg01
);
10963 /* (X - c) > X becomes false. */
10964 if (code
== GT_EXPR
10965 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10966 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10968 if (TREE_CODE (arg01
) == INTEGER_CST
10969 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10970 fold_overflow_warning (("assuming signed overflow does not "
10971 "occur when assuming that (X - c) > X "
10972 "is always false"),
10973 WARN_STRICT_OVERFLOW_ALL
);
10974 return constant_boolean_node (0, type
);
10977 /* Likewise (X + c) < X becomes false. */
10978 if (code
== LT_EXPR
10979 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10980 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10982 if (TREE_CODE (arg01
) == INTEGER_CST
10983 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10984 fold_overflow_warning (("assuming signed overflow does not "
10985 "occur when assuming that "
10986 "(X + c) < X is always false"),
10987 WARN_STRICT_OVERFLOW_ALL
);
10988 return constant_boolean_node (0, type
);
10991 /* Convert (X - c) <= X to true. */
10992 if (!HONOR_NANS (arg1
)
10994 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10995 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10997 if (TREE_CODE (arg01
) == INTEGER_CST
10998 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10999 fold_overflow_warning (("assuming signed overflow does not "
11000 "occur when assuming that "
11001 "(X - c) <= X is always true"),
11002 WARN_STRICT_OVERFLOW_ALL
);
11003 return constant_boolean_node (1, type
);
11006 /* Convert (X + c) >= X to true. */
11007 if (!HONOR_NANS (arg1
)
11009 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11010 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11012 if (TREE_CODE (arg01
) == INTEGER_CST
11013 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11014 fold_overflow_warning (("assuming signed overflow does not "
11015 "occur when assuming that "
11016 "(X + c) >= X is always true"),
11017 WARN_STRICT_OVERFLOW_ALL
);
11018 return constant_boolean_node (1, type
);
11021 if (TREE_CODE (arg01
) == INTEGER_CST
)
11023 /* Convert X + c > X and X - c < X to true for integers. */
11024 if (code
== GT_EXPR
11025 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11026 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11028 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11029 fold_overflow_warning (("assuming signed overflow does "
11030 "not occur when assuming that "
11031 "(X + c) > X is always true"),
11032 WARN_STRICT_OVERFLOW_ALL
);
11033 return constant_boolean_node (1, type
);
11036 if (code
== LT_EXPR
11037 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11038 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11040 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11041 fold_overflow_warning (("assuming signed overflow does "
11042 "not occur when assuming that "
11043 "(X - c) < X is always true"),
11044 WARN_STRICT_OVERFLOW_ALL
);
11045 return constant_boolean_node (1, type
);
11048 /* Convert X + c <= X and X - c >= X to false for integers. */
11049 if (code
== LE_EXPR
11050 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11051 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11053 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11054 fold_overflow_warning (("assuming signed overflow does "
11055 "not occur when assuming that "
11056 "(X + c) <= X is always false"),
11057 WARN_STRICT_OVERFLOW_ALL
);
11058 return constant_boolean_node (0, type
);
11061 if (code
== GE_EXPR
11062 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11063 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11065 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11066 fold_overflow_warning (("assuming signed overflow does "
11067 "not occur when assuming that "
11068 "(X - c) >= X is always false"),
11069 WARN_STRICT_OVERFLOW_ALL
);
11070 return constant_boolean_node (0, type
);
11075 /* If we are comparing an ABS_EXPR with a constant, we can
11076 convert all the cases into explicit comparisons, but they may
11077 well not be faster than doing the ABS and one comparison.
11078 But ABS (X) <= C is a range comparison, which becomes a subtraction
11079 and a comparison, and is probably faster. */
11080 if (code
== LE_EXPR
11081 && TREE_CODE (arg1
) == INTEGER_CST
11082 && TREE_CODE (arg0
) == ABS_EXPR
11083 && ! TREE_SIDE_EFFECTS (arg0
)
11084 && (0 != (tem
= negate_expr (arg1
)))
11085 && TREE_CODE (tem
) == INTEGER_CST
11086 && !TREE_OVERFLOW (tem
))
11087 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11088 build2 (GE_EXPR
, type
,
11089 TREE_OPERAND (arg0
, 0), tem
),
11090 build2 (LE_EXPR
, type
,
11091 TREE_OPERAND (arg0
, 0), arg1
));
11093 /* Convert ABS_EXPR<x> >= 0 to true. */
11094 strict_overflow_p
= false;
11095 if (code
== GE_EXPR
11096 && (integer_zerop (arg1
)
11097 || (! HONOR_NANS (arg0
)
11098 && real_zerop (arg1
)))
11099 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11101 if (strict_overflow_p
)
11102 fold_overflow_warning (("assuming signed overflow does not occur "
11103 "when simplifying comparison of "
11104 "absolute value and zero"),
11105 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11106 return omit_one_operand_loc (loc
, type
,
11107 constant_boolean_node (true, type
),
11111 /* Convert ABS_EXPR<x> < 0 to false. */
11112 strict_overflow_p
= false;
11113 if (code
== LT_EXPR
11114 && (integer_zerop (arg1
) || real_zerop (arg1
))
11115 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11117 if (strict_overflow_p
)
11118 fold_overflow_warning (("assuming signed overflow does not occur "
11119 "when simplifying comparison of "
11120 "absolute value and zero"),
11121 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11122 return omit_one_operand_loc (loc
, type
,
11123 constant_boolean_node (false, type
),
11127 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11128 and similarly for >= into !=. */
11129 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11130 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11131 && TREE_CODE (arg1
) == LSHIFT_EXPR
11132 && integer_onep (TREE_OPERAND (arg1
, 0)))
11133 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11134 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11135 TREE_OPERAND (arg1
, 1)),
11136 build_zero_cst (TREE_TYPE (arg0
)));
11138 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11139 otherwise Y might be >= # of bits in X's type and thus e.g.
11140 (unsigned char) (1 << Y) for Y 15 might be 0.
11141 If the cast is widening, then 1 << Y should have unsigned type,
11142 otherwise if Y is number of bits in the signed shift type minus 1,
11143 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11144 31 might be 0xffffffff80000000. */
11145 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11146 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11147 && CONVERT_EXPR_P (arg1
)
11148 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11149 && (element_precision (TREE_TYPE (arg1
))
11150 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11151 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11152 || (element_precision (TREE_TYPE (arg1
))
11153 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11154 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11156 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11157 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11158 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11159 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11160 build_zero_cst (TREE_TYPE (arg0
)));
11165 case UNORDERED_EXPR
:
11173 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11175 tree targ0
= strip_float_extensions (arg0
);
11176 tree targ1
= strip_float_extensions (arg1
);
11177 tree newtype
= TREE_TYPE (targ0
);
11179 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11180 newtype
= TREE_TYPE (targ1
);
11182 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11183 return fold_build2_loc (loc
, code
, type
,
11184 fold_convert_loc (loc
, newtype
, targ0
),
11185 fold_convert_loc (loc
, newtype
, targ1
));
11190 case COMPOUND_EXPR
:
11191 /* When pedantic, a compound expression can be neither an lvalue
11192 nor an integer constant expression. */
11193 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11195 /* Don't let (0, 0) be null pointer constant. */
11196 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11197 : fold_convert_loc (loc
, type
, arg1
);
11198 return pedantic_non_lvalue_loc (loc
, tem
);
11201 /* An ASSERT_EXPR should never be passed to fold_binary. */
11202 gcc_unreachable ();
11206 } /* switch (code) */
11209 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11210 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11214 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11216 switch (TREE_CODE (*tp
))
11222 *walk_subtrees
= 0;
11224 /* ... fall through ... */
11231 /* Return whether the sub-tree ST contains a label which is accessible from
11232 outside the sub-tree. */
11235 contains_label_p (tree st
)
11238 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11241 /* Fold a ternary expression of code CODE and type TYPE with operands
11242 OP0, OP1, and OP2. Return the folded expression if folding is
11243 successful. Otherwise, return NULL_TREE. */
11246 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11247 tree op0
, tree op1
, tree op2
)
11250 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11251 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11253 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11254 && TREE_CODE_LENGTH (code
) == 3);
11256 /* If this is a commutative operation, and OP0 is a constant, move it
11257 to OP1 to reduce the number of tests below. */
11258 if (commutative_ternary_tree_code (code
)
11259 && tree_swap_operands_p (op0
, op1
, true))
11260 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11262 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11266 /* Strip any conversions that don't change the mode. This is safe
11267 for every expression, except for a comparison expression because
11268 its signedness is derived from its operands. So, in the latter
11269 case, only strip conversions that don't change the signedness.
11271 Note that this is done as an internal manipulation within the
11272 constant folder, in order to find the simplest representation of
11273 the arguments so that their form can be studied. In any cases,
11274 the appropriate type conversions should be put back in the tree
11275 that will get out of the constant folder. */
11296 case COMPONENT_REF
:
11297 if (TREE_CODE (arg0
) == CONSTRUCTOR
11298 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11300 unsigned HOST_WIDE_INT idx
;
11302 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11309 case VEC_COND_EXPR
:
11310 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11311 so all simple results must be passed through pedantic_non_lvalue. */
11312 if (TREE_CODE (arg0
) == INTEGER_CST
)
11314 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11315 tem
= integer_zerop (arg0
) ? op2
: op1
;
11316 /* Only optimize constant conditions when the selected branch
11317 has the same type as the COND_EXPR. This avoids optimizing
11318 away "c ? x : throw", where the throw has a void type.
11319 Avoid throwing away that operand which contains label. */
11320 if ((!TREE_SIDE_EFFECTS (unused_op
)
11321 || !contains_label_p (unused_op
))
11322 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11323 || VOID_TYPE_P (type
)))
11324 return pedantic_non_lvalue_loc (loc
, tem
);
11327 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11329 if ((TREE_CODE (arg1
) == VECTOR_CST
11330 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11331 && (TREE_CODE (arg2
) == VECTOR_CST
11332 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11334 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
11335 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
11336 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
11337 for (i
= 0; i
< nelts
; i
++)
11339 tree val
= VECTOR_CST_ELT (arg0
, i
);
11340 if (integer_all_onesp (val
))
11342 else if (integer_zerop (val
))
11343 sel
[i
] = nelts
+ i
;
11344 else /* Currently unreachable. */
11347 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11348 if (t
!= NULL_TREE
)
11353 /* If we have A op B ? A : C, we may be able to convert this to a
11354 simpler expression, depending on the operation and the values
11355 of B and C. Signed zeros prevent all of these transformations,
11356 for reasons given above each one.
11358 Also try swapping the arguments and inverting the conditional. */
11359 if (COMPARISON_CLASS_P (arg0
)
11360 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11361 arg1
, TREE_OPERAND (arg0
, 1))
11362 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11364 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11369 if (COMPARISON_CLASS_P (arg0
)
11370 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11372 TREE_OPERAND (arg0
, 1))
11373 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11375 location_t loc0
= expr_location_or (arg0
, loc
);
11376 tem
= fold_invert_truthvalue (loc0
, arg0
);
11377 if (tem
&& COMPARISON_CLASS_P (tem
))
11379 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11385 /* If the second operand is simpler than the third, swap them
11386 since that produces better jump optimization results. */
11387 if (truth_value_p (TREE_CODE (arg0
))
11388 && tree_swap_operands_p (op1
, op2
, false))
11390 location_t loc0
= expr_location_or (arg0
, loc
);
11391 /* See if this can be inverted. If it can't, possibly because
11392 it was a floating-point inequality comparison, don't do
11394 tem
= fold_invert_truthvalue (loc0
, arg0
);
11396 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11399 /* Convert A ? 1 : 0 to simply A. */
11400 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11401 : (integer_onep (op1
)
11402 && !VECTOR_TYPE_P (type
)))
11403 && integer_zerop (op2
)
11404 /* If we try to convert OP0 to our type, the
11405 call to fold will try to move the conversion inside
11406 a COND, which will recurse. In that case, the COND_EXPR
11407 is probably the best choice, so leave it alone. */
11408 && type
== TREE_TYPE (arg0
))
11409 return pedantic_non_lvalue_loc (loc
, arg0
);
11411 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11412 over COND_EXPR in cases such as floating point comparisons. */
11413 if (integer_zerop (op1
)
11414 && code
== COND_EXPR
11415 && integer_onep (op2
)
11416 && !VECTOR_TYPE_P (type
)
11417 && truth_value_p (TREE_CODE (arg0
)))
11418 return pedantic_non_lvalue_loc (loc
,
11419 fold_convert_loc (loc
, type
,
11420 invert_truthvalue_loc (loc
,
11423 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11424 if (TREE_CODE (arg0
) == LT_EXPR
11425 && integer_zerop (TREE_OPERAND (arg0
, 1))
11426 && integer_zerop (op2
)
11427 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11429 /* sign_bit_p looks through both zero and sign extensions,
11430 but for this optimization only sign extensions are
11432 tree tem2
= TREE_OPERAND (arg0
, 0);
11433 while (tem
!= tem2
)
11435 if (TREE_CODE (tem2
) != NOP_EXPR
11436 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11441 tem2
= TREE_OPERAND (tem2
, 0);
11443 /* sign_bit_p only checks ARG1 bits within A's precision.
11444 If <sign bit of A> has wider type than A, bits outside
11445 of A's precision in <sign bit of A> need to be checked.
11446 If they are all 0, this optimization needs to be done
11447 in unsigned A's type, if they are all 1 in signed A's type,
11448 otherwise this can't be done. */
11450 && TYPE_PRECISION (TREE_TYPE (tem
))
11451 < TYPE_PRECISION (TREE_TYPE (arg1
))
11452 && TYPE_PRECISION (TREE_TYPE (tem
))
11453 < TYPE_PRECISION (type
))
11455 int inner_width
, outer_width
;
11458 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11459 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11460 if (outer_width
> TYPE_PRECISION (type
))
11461 outer_width
= TYPE_PRECISION (type
);
11463 wide_int mask
= wi::shifted_mask
11464 (inner_width
, outer_width
- inner_width
, false,
11465 TYPE_PRECISION (TREE_TYPE (arg1
)));
11467 wide_int common
= mask
& arg1
;
11468 if (common
== mask
)
11470 tem_type
= signed_type_for (TREE_TYPE (tem
));
11471 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11473 else if (common
== 0)
11475 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11476 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11484 fold_convert_loc (loc
, type
,
11485 fold_build2_loc (loc
, BIT_AND_EXPR
,
11486 TREE_TYPE (tem
), tem
,
11487 fold_convert_loc (loc
,
11492 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11493 already handled above. */
11494 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11495 && integer_onep (TREE_OPERAND (arg0
, 1))
11496 && integer_zerop (op2
)
11497 && integer_pow2p (arg1
))
11499 tree tem
= TREE_OPERAND (arg0
, 0);
11501 if (TREE_CODE (tem
) == RSHIFT_EXPR
11502 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11503 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
11504 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11505 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11506 TREE_OPERAND (tem
, 0), arg1
);
11509 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11510 is probably obsolete because the first operand should be a
11511 truth value (that's why we have the two cases above), but let's
11512 leave it in until we can confirm this for all front-ends. */
11513 if (integer_zerop (op2
)
11514 && TREE_CODE (arg0
) == NE_EXPR
11515 && integer_zerop (TREE_OPERAND (arg0
, 1))
11516 && integer_pow2p (arg1
)
11517 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11518 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11519 arg1
, OEP_ONLY_CONST
))
11520 return pedantic_non_lvalue_loc (loc
,
11521 fold_convert_loc (loc
, type
,
11522 TREE_OPERAND (arg0
, 0)));
11524 /* Disable the transformations below for vectors, since
11525 fold_binary_op_with_conditional_arg may undo them immediately,
11526 yielding an infinite loop. */
11527 if (code
== VEC_COND_EXPR
)
11530 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11531 if (integer_zerop (op2
)
11532 && truth_value_p (TREE_CODE (arg0
))
11533 && truth_value_p (TREE_CODE (arg1
))
11534 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11535 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11536 : TRUTH_ANDIF_EXPR
,
11537 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
11539 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11540 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11541 && truth_value_p (TREE_CODE (arg0
))
11542 && truth_value_p (TREE_CODE (arg1
))
11543 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11545 location_t loc0
= expr_location_or (arg0
, loc
);
11546 /* Only perform transformation if ARG0 is easily inverted. */
11547 tem
= fold_invert_truthvalue (loc0
, arg0
);
11549 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11552 type
, fold_convert_loc (loc
, type
, tem
),
11556 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11557 if (integer_zerop (arg1
)
11558 && truth_value_p (TREE_CODE (arg0
))
11559 && truth_value_p (TREE_CODE (op2
))
11560 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11562 location_t loc0
= expr_location_or (arg0
, loc
);
11563 /* Only perform transformation if ARG0 is easily inverted. */
11564 tem
= fold_invert_truthvalue (loc0
, arg0
);
11566 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11567 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11568 type
, fold_convert_loc (loc
, type
, tem
),
11572 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11573 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11574 && truth_value_p (TREE_CODE (arg0
))
11575 && truth_value_p (TREE_CODE (op2
))
11576 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11577 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11578 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11579 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11584 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11585 of fold_ternary on them. */
11586 gcc_unreachable ();
11588 case BIT_FIELD_REF
:
11589 if (TREE_CODE (arg0
) == VECTOR_CST
11590 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11591 || (TREE_CODE (type
) == VECTOR_TYPE
11592 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11594 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11595 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11596 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11597 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11600 && (idx
% width
) == 0
11601 && (n
% width
) == 0
11602 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11607 if (TREE_CODE (arg0
) == VECTOR_CST
)
11610 return VECTOR_CST_ELT (arg0
, idx
);
11612 tree
*vals
= XALLOCAVEC (tree
, n
);
11613 for (unsigned i
= 0; i
< n
; ++i
)
11614 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
11615 return build_vector (type
, vals
);
11620 /* On constants we can use native encode/interpret to constant
11621 fold (nearly) all BIT_FIELD_REFs. */
11622 if (CONSTANT_CLASS_P (arg0
)
11623 && can_native_interpret_type_p (type
)
11624 && BITS_PER_UNIT
== 8)
11626 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11627 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11628 /* Limit us to a reasonable amount of work. To relax the
11629 other limitations we need bit-shifting of the buffer
11630 and rounding up the size. */
11631 if (bitpos
% BITS_PER_UNIT
== 0
11632 && bitsize
% BITS_PER_UNIT
== 0
11633 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
11635 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
11636 unsigned HOST_WIDE_INT len
11637 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
11638 bitpos
/ BITS_PER_UNIT
);
11640 && len
* BITS_PER_UNIT
>= bitsize
)
11642 tree v
= native_interpret_expr (type
, b
,
11643 bitsize
/ BITS_PER_UNIT
);
11653 /* For integers we can decompose the FMA if possible. */
11654 if (TREE_CODE (arg0
) == INTEGER_CST
11655 && TREE_CODE (arg1
) == INTEGER_CST
)
11656 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11657 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11658 if (integer_zerop (arg2
))
11659 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11661 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11663 case VEC_PERM_EXPR
:
11664 if (TREE_CODE (arg2
) == VECTOR_CST
)
11666 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
11667 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
11668 unsigned char *sel2
= sel
+ nelts
;
11669 bool need_mask_canon
= false;
11670 bool need_mask_canon2
= false;
11671 bool all_in_vec0
= true;
11672 bool all_in_vec1
= true;
11673 bool maybe_identity
= true;
11674 bool single_arg
= (op0
== op1
);
11675 bool changed
= false;
11677 mask2
= 2 * nelts
- 1;
11678 mask
= single_arg
? (nelts
- 1) : mask2
;
11679 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
11680 for (i
= 0; i
< nelts
; i
++)
11682 tree val
= VECTOR_CST_ELT (arg2
, i
);
11683 if (TREE_CODE (val
) != INTEGER_CST
)
11686 /* Make sure that the perm value is in an acceptable
11689 need_mask_canon
|= wi::gtu_p (t
, mask
);
11690 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11691 sel
[i
] = t
.to_uhwi () & mask
;
11692 sel2
[i
] = t
.to_uhwi () & mask2
;
11694 if (sel
[i
] < nelts
)
11695 all_in_vec1
= false;
11697 all_in_vec0
= false;
11699 if ((sel
[i
] & (nelts
-1)) != i
)
11700 maybe_identity
= false;
11703 if (maybe_identity
)
11713 else if (all_in_vec1
)
11716 for (i
= 0; i
< nelts
; i
++)
11718 need_mask_canon
= true;
11721 if ((TREE_CODE (op0
) == VECTOR_CST
11722 || TREE_CODE (op0
) == CONSTRUCTOR
)
11723 && (TREE_CODE (op1
) == VECTOR_CST
11724 || TREE_CODE (op1
) == CONSTRUCTOR
))
11726 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11727 if (t
!= NULL_TREE
)
11731 if (op0
== op1
&& !single_arg
)
11734 /* Some targets are deficient and fail to expand a single
11735 argument permutation while still allowing an equivalent
11736 2-argument version. */
11737 if (need_mask_canon
&& arg2
== op2
11738 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
11739 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
11741 need_mask_canon
= need_mask_canon2
;
11745 if (need_mask_canon
&& arg2
== op2
)
11747 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
11748 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11749 for (i
= 0; i
< nelts
; i
++)
11750 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
11751 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11756 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11760 case BIT_INSERT_EXPR
:
11761 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11762 if (TREE_CODE (arg0
) == INTEGER_CST
11763 && TREE_CODE (arg1
) == INTEGER_CST
)
11765 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11766 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
11767 wide_int tem
= wi::bit_and (arg0
,
11768 wi::shifted_mask (bitpos
, bitsize
, true,
11769 TYPE_PRECISION (type
)));
11771 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
11773 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
11775 else if (TREE_CODE (arg0
) == VECTOR_CST
11776 && CONSTANT_CLASS_P (arg1
)
11777 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
11780 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11781 unsigned HOST_WIDE_INT elsize
11782 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
11783 if (bitpos
% elsize
== 0)
11785 unsigned k
= bitpos
/ elsize
;
11786 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
11790 tree
*elts
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
11791 memcpy (elts
, VECTOR_CST_ELTS (arg0
),
11792 sizeof (tree
) * TYPE_VECTOR_SUBPARTS (type
));
11794 return build_vector (type
, elts
);
11802 } /* switch (code) */
11805 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11806 of an array (or vector). */
11809 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
11811 tree index_type
= NULL_TREE
;
11812 offset_int low_bound
= 0;
11814 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
11816 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
11817 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
11819 /* Static constructors for variably sized objects makes no sense. */
11820 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
11821 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
11822 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
11827 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
11828 TYPE_SIGN (index_type
));
11830 offset_int index
= low_bound
- 1;
11832 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11833 TYPE_SIGN (index_type
));
11835 offset_int max_index
;
11836 unsigned HOST_WIDE_INT cnt
;
11839 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
11841 /* Array constructor might explicitly set index, or specify a range,
11842 or leave index NULL meaning that it is next index after previous
11846 if (TREE_CODE (cfield
) == INTEGER_CST
)
11847 max_index
= index
= wi::to_offset (cfield
);
11850 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
11851 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
11852 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
11859 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11860 TYPE_SIGN (index_type
));
11864 /* Do we have match? */
11865 if (wi::cmpu (access_index
, index
) >= 0
11866 && wi::cmpu (access_index
, max_index
) <= 0)
11872 /* Perform constant folding and related simplification of EXPR.
11873 The related simplifications include x*1 => x, x*0 => 0, etc.,
11874 and application of the associative law.
11875 NOP_EXPR conversions may be removed freely (as long as we
11876 are careful not to change the type of the overall expression).
11877 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11878 but we can constant-fold them if they have constant operands. */
11880 #ifdef ENABLE_FOLD_CHECKING
11881 # define fold(x) fold_1 (x)
11882 static tree
fold_1 (tree
);
11888 const tree t
= expr
;
11889 enum tree_code code
= TREE_CODE (t
);
11890 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11892 location_t loc
= EXPR_LOCATION (expr
);
11894 /* Return right away if a constant. */
11895 if (kind
== tcc_constant
)
11898 /* CALL_EXPR-like objects with variable numbers of operands are
11899 treated specially. */
11900 if (kind
== tcc_vl_exp
)
11902 if (code
== CALL_EXPR
)
11904 tem
= fold_call_expr (loc
, expr
, false);
11905 return tem
? tem
: expr
;
11910 if (IS_EXPR_CODE_CLASS (kind
))
11912 tree type
= TREE_TYPE (t
);
11913 tree op0
, op1
, op2
;
11915 switch (TREE_CODE_LENGTH (code
))
11918 op0
= TREE_OPERAND (t
, 0);
11919 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11920 return tem
? tem
: expr
;
11922 op0
= TREE_OPERAND (t
, 0);
11923 op1
= TREE_OPERAND (t
, 1);
11924 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11925 return tem
? tem
: expr
;
11927 op0
= TREE_OPERAND (t
, 0);
11928 op1
= TREE_OPERAND (t
, 1);
11929 op2
= TREE_OPERAND (t
, 2);
11930 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11931 return tem
? tem
: expr
;
11941 tree op0
= TREE_OPERAND (t
, 0);
11942 tree op1
= TREE_OPERAND (t
, 1);
11944 if (TREE_CODE (op1
) == INTEGER_CST
11945 && TREE_CODE (op0
) == CONSTRUCTOR
11946 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11948 tree val
= get_array_ctor_element_at_index (op0
,
11949 wi::to_offset (op1
));
11957 /* Return a VECTOR_CST if possible. */
11960 tree type
= TREE_TYPE (t
);
11961 if (TREE_CODE (type
) != VECTOR_TYPE
)
11966 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
11967 if (! CONSTANT_CLASS_P (val
))
11970 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
11974 return fold (DECL_INITIAL (t
));
11978 } /* switch (code) */
11981 #ifdef ENABLE_FOLD_CHECKING
11984 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
11985 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
11986 static void fold_check_failed (const_tree
, const_tree
);
11987 void print_fold_checksum (const_tree
);
11989 /* When --enable-checking=fold, compute a digest of expr before
11990 and after actual fold call to see if fold did not accidentally
11991 change original expr. */
11997 struct md5_ctx ctx
;
11998 unsigned char checksum_before
[16], checksum_after
[16];
11999 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12001 md5_init_ctx (&ctx
);
12002 fold_checksum_tree (expr
, &ctx
, &ht
);
12003 md5_finish_ctx (&ctx
, checksum_before
);
12006 ret
= fold_1 (expr
);
12008 md5_init_ctx (&ctx
);
12009 fold_checksum_tree (expr
, &ctx
, &ht
);
12010 md5_finish_ctx (&ctx
, checksum_after
);
12012 if (memcmp (checksum_before
, checksum_after
, 16))
12013 fold_check_failed (expr
, ret
);
12019 print_fold_checksum (const_tree expr
)
12021 struct md5_ctx ctx
;
12022 unsigned char checksum
[16], cnt
;
12023 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12025 md5_init_ctx (&ctx
);
12026 fold_checksum_tree (expr
, &ctx
, &ht
);
12027 md5_finish_ctx (&ctx
, checksum
);
12028 for (cnt
= 0; cnt
< 16; ++cnt
)
12029 fprintf (stderr
, "%02x", checksum
[cnt
]);
12030 putc ('\n', stderr
);
12034 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12036 internal_error ("fold check: original tree changed by fold");
12040 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12041 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12043 const tree_node
**slot
;
12044 enum tree_code code
;
12045 union tree_node buf
;
12051 slot
= ht
->find_slot (expr
, INSERT
);
12055 code
= TREE_CODE (expr
);
12056 if (TREE_CODE_CLASS (code
) == tcc_declaration
12057 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12059 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12060 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12061 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12062 buf
.decl_with_vis
.symtab_node
= NULL
;
12063 expr
= (tree
) &buf
;
12065 else if (TREE_CODE_CLASS (code
) == tcc_type
12066 && (TYPE_POINTER_TO (expr
)
12067 || TYPE_REFERENCE_TO (expr
)
12068 || TYPE_CACHED_VALUES_P (expr
)
12069 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12070 || TYPE_NEXT_VARIANT (expr
)
12071 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
12073 /* Allow these fields to be modified. */
12075 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12076 expr
= tmp
= (tree
) &buf
;
12077 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12078 TYPE_POINTER_TO (tmp
) = NULL
;
12079 TYPE_REFERENCE_TO (tmp
) = NULL
;
12080 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12081 TYPE_ALIAS_SET (tmp
) = -1;
12082 if (TYPE_CACHED_VALUES_P (tmp
))
12084 TYPE_CACHED_VALUES_P (tmp
) = 0;
12085 TYPE_CACHED_VALUES (tmp
) = NULL
;
12088 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12089 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12090 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12091 if (TREE_CODE_CLASS (code
) != tcc_type
12092 && TREE_CODE_CLASS (code
) != tcc_declaration
12093 && code
!= TREE_LIST
12094 && code
!= SSA_NAME
12095 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12096 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12097 switch (TREE_CODE_CLASS (code
))
12103 md5_process_bytes (TREE_STRING_POINTER (expr
),
12104 TREE_STRING_LENGTH (expr
), ctx
);
12107 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12108 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12111 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12112 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12118 case tcc_exceptional
:
12122 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12123 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12124 expr
= TREE_CHAIN (expr
);
12125 goto recursive_label
;
12128 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12129 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12135 case tcc_expression
:
12136 case tcc_reference
:
12137 case tcc_comparison
:
12140 case tcc_statement
:
12142 len
= TREE_OPERAND_LENGTH (expr
);
12143 for (i
= 0; i
< len
; ++i
)
12144 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12146 case tcc_declaration
:
12147 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12148 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12149 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12151 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12152 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12153 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12154 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12155 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12158 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12160 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12162 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12163 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12165 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12169 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12170 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12171 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12172 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12173 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12174 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12175 if (INTEGRAL_TYPE_P (expr
)
12176 || SCALAR_FLOAT_TYPE_P (expr
))
12178 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12179 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12181 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12182 if (TREE_CODE (expr
) == RECORD_TYPE
12183 || TREE_CODE (expr
) == UNION_TYPE
12184 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12185 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12186 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12193 /* Helper function for outputting the checksum of a tree T. When
12194 debugging with gdb, you can "define mynext" to be "next" followed
12195 by "call debug_fold_checksum (op0)", then just trace down till the
12198 DEBUG_FUNCTION
void
12199 debug_fold_checksum (const_tree t
)
12202 unsigned char checksum
[16];
12203 struct md5_ctx ctx
;
12204 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12206 md5_init_ctx (&ctx
);
12207 fold_checksum_tree (t
, &ctx
, &ht
);
12208 md5_finish_ctx (&ctx
, checksum
);
12211 for (i
= 0; i
< 16; i
++)
12212 fprintf (stderr
, "%d ", checksum
[i
]);
12214 fprintf (stderr
, "\n");
12219 /* Fold a unary tree expression with code CODE of type TYPE with an
12220 operand OP0. LOC is the location of the resulting expression.
12221 Return a folded expression if successful. Otherwise, return a tree
12222 expression with code CODE of type TYPE with an operand OP0. */
12225 fold_build1_stat_loc (location_t loc
,
12226 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12229 #ifdef ENABLE_FOLD_CHECKING
12230 unsigned char checksum_before
[16], checksum_after
[16];
12231 struct md5_ctx ctx
;
12232 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12234 md5_init_ctx (&ctx
);
12235 fold_checksum_tree (op0
, &ctx
, &ht
);
12236 md5_finish_ctx (&ctx
, checksum_before
);
12240 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12242 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12244 #ifdef ENABLE_FOLD_CHECKING
12245 md5_init_ctx (&ctx
);
12246 fold_checksum_tree (op0
, &ctx
, &ht
);
12247 md5_finish_ctx (&ctx
, checksum_after
);
12249 if (memcmp (checksum_before
, checksum_after
, 16))
12250 fold_check_failed (op0
, tem
);
12255 /* Fold a binary tree expression with code CODE of type TYPE with
12256 operands OP0 and OP1. LOC is the location of the resulting
12257 expression. Return a folded expression if successful. Otherwise,
12258 return a tree expression with code CODE of type TYPE with operands
12262 fold_build2_stat_loc (location_t loc
,
12263 enum tree_code code
, tree type
, tree op0
, tree op1
12267 #ifdef ENABLE_FOLD_CHECKING
12268 unsigned char checksum_before_op0
[16],
12269 checksum_before_op1
[16],
12270 checksum_after_op0
[16],
12271 checksum_after_op1
[16];
12272 struct md5_ctx ctx
;
12273 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12275 md5_init_ctx (&ctx
);
12276 fold_checksum_tree (op0
, &ctx
, &ht
);
12277 md5_finish_ctx (&ctx
, checksum_before_op0
);
12280 md5_init_ctx (&ctx
);
12281 fold_checksum_tree (op1
, &ctx
, &ht
);
12282 md5_finish_ctx (&ctx
, checksum_before_op1
);
12286 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12288 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12290 #ifdef ENABLE_FOLD_CHECKING
12291 md5_init_ctx (&ctx
);
12292 fold_checksum_tree (op0
, &ctx
, &ht
);
12293 md5_finish_ctx (&ctx
, checksum_after_op0
);
12296 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12297 fold_check_failed (op0
, tem
);
12299 md5_init_ctx (&ctx
);
12300 fold_checksum_tree (op1
, &ctx
, &ht
);
12301 md5_finish_ctx (&ctx
, checksum_after_op1
);
12303 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12304 fold_check_failed (op1
, tem
);
12309 /* Fold a ternary tree expression with code CODE of type TYPE with
12310 operands OP0, OP1, and OP2. Return a folded expression if
12311 successful. Otherwise, return a tree expression with code CODE of
12312 type TYPE with operands OP0, OP1, and OP2. */
12315 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
12316 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12319 #ifdef ENABLE_FOLD_CHECKING
12320 unsigned char checksum_before_op0
[16],
12321 checksum_before_op1
[16],
12322 checksum_before_op2
[16],
12323 checksum_after_op0
[16],
12324 checksum_after_op1
[16],
12325 checksum_after_op2
[16];
12326 struct md5_ctx ctx
;
12327 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12329 md5_init_ctx (&ctx
);
12330 fold_checksum_tree (op0
, &ctx
, &ht
);
12331 md5_finish_ctx (&ctx
, checksum_before_op0
);
12334 md5_init_ctx (&ctx
);
12335 fold_checksum_tree (op1
, &ctx
, &ht
);
12336 md5_finish_ctx (&ctx
, checksum_before_op1
);
12339 md5_init_ctx (&ctx
);
12340 fold_checksum_tree (op2
, &ctx
, &ht
);
12341 md5_finish_ctx (&ctx
, checksum_before_op2
);
12345 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12346 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12348 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12350 #ifdef ENABLE_FOLD_CHECKING
12351 md5_init_ctx (&ctx
);
12352 fold_checksum_tree (op0
, &ctx
, &ht
);
12353 md5_finish_ctx (&ctx
, checksum_after_op0
);
12356 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12357 fold_check_failed (op0
, tem
);
12359 md5_init_ctx (&ctx
);
12360 fold_checksum_tree (op1
, &ctx
, &ht
);
12361 md5_finish_ctx (&ctx
, checksum_after_op1
);
12364 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12365 fold_check_failed (op1
, tem
);
12367 md5_init_ctx (&ctx
);
12368 fold_checksum_tree (op2
, &ctx
, &ht
);
12369 md5_finish_ctx (&ctx
, checksum_after_op2
);
12371 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12372 fold_check_failed (op2
, tem
);
12377 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12378 arguments in ARGARRAY, and a null static chain.
12379 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12380 of type TYPE from the given operands as constructed by build_call_array. */
12383 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12384 int nargs
, tree
*argarray
)
12387 #ifdef ENABLE_FOLD_CHECKING
12388 unsigned char checksum_before_fn
[16],
12389 checksum_before_arglist
[16],
12390 checksum_after_fn
[16],
12391 checksum_after_arglist
[16];
12392 struct md5_ctx ctx
;
12393 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12396 md5_init_ctx (&ctx
);
12397 fold_checksum_tree (fn
, &ctx
, &ht
);
12398 md5_finish_ctx (&ctx
, checksum_before_fn
);
12401 md5_init_ctx (&ctx
);
12402 for (i
= 0; i
< nargs
; i
++)
12403 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12404 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12408 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12410 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12412 #ifdef ENABLE_FOLD_CHECKING
12413 md5_init_ctx (&ctx
);
12414 fold_checksum_tree (fn
, &ctx
, &ht
);
12415 md5_finish_ctx (&ctx
, checksum_after_fn
);
12418 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12419 fold_check_failed (fn
, tem
);
12421 md5_init_ctx (&ctx
);
12422 for (i
= 0; i
< nargs
; i
++)
12423 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12424 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12426 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12427 fold_check_failed (NULL_TREE
, tem
);
12432 /* Perform constant folding and related simplification of initializer
12433 expression EXPR. These behave identically to "fold_buildN" but ignore
12434 potential run-time traps and exceptions that fold must preserve. */
12436 #define START_FOLD_INIT \
12437 int saved_signaling_nans = flag_signaling_nans;\
12438 int saved_trapping_math = flag_trapping_math;\
12439 int saved_rounding_math = flag_rounding_math;\
12440 int saved_trapv = flag_trapv;\
12441 int saved_folding_initializer = folding_initializer;\
12442 flag_signaling_nans = 0;\
12443 flag_trapping_math = 0;\
12444 flag_rounding_math = 0;\
12446 folding_initializer = 1;
12448 #define END_FOLD_INIT \
12449 flag_signaling_nans = saved_signaling_nans;\
12450 flag_trapping_math = saved_trapping_math;\
12451 flag_rounding_math = saved_rounding_math;\
12452 flag_trapv = saved_trapv;\
12453 folding_initializer = saved_folding_initializer;
12456 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12457 tree type
, tree op
)
12462 result
= fold_build1_loc (loc
, code
, type
, op
);
12469 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12470 tree type
, tree op0
, tree op1
)
12475 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12482 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12483 int nargs
, tree
*argarray
)
12488 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12494 #undef START_FOLD_INIT
12495 #undef END_FOLD_INIT
12497 /* Determine if first argument is a multiple of second argument. Return 0 if
12498 it is not, or we cannot easily determined it to be.
12500 An example of the sort of thing we care about (at this point; this routine
12501 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12502 fold cases do now) is discovering that
12504 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12510 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12512 This code also handles discovering that
12514 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12516 is a multiple of 8 so we don't have to worry about dealing with a
12517 possible remainder.
12519 Note that we *look* inside a SAVE_EXPR only to determine how it was
12520 calculated; it is not safe for fold to do much of anything else with the
12521 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12522 at run time. For example, the latter example above *cannot* be implemented
12523 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12524 evaluation time of the original SAVE_EXPR is not necessarily the same at
12525 the time the new expression is evaluated. The only optimization of this
12526 sort that would be valid is changing
12528 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12532 SAVE_EXPR (I) * SAVE_EXPR (J)
12534 (where the same SAVE_EXPR (J) is used in the original and the
12535 transformed version). */
12538 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12540 if (operand_equal_p (top
, bottom
, 0))
12543 if (TREE_CODE (type
) != INTEGER_TYPE
)
12546 switch (TREE_CODE (top
))
12549 /* Bitwise and provides a power of two multiple. If the mask is
12550 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12551 if (!integer_pow2p (bottom
))
12556 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12557 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12561 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12562 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12565 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12569 op1
= TREE_OPERAND (top
, 1);
12570 /* const_binop may not detect overflow correctly,
12571 so check for it explicitly here. */
12572 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12573 && 0 != (t1
= fold_convert (type
,
12574 const_binop (LSHIFT_EXPR
,
12577 && !TREE_OVERFLOW (t1
))
12578 return multiple_of_p (type
, t1
, bottom
);
12583 /* Can't handle conversions from non-integral or wider integral type. */
12584 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12585 || (TYPE_PRECISION (type
)
12586 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12589 /* .. fall through ... */
12592 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12595 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12596 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12599 if (TREE_CODE (bottom
) != INTEGER_CST
12600 || integer_zerop (bottom
)
12601 || (TYPE_UNSIGNED (type
)
12602 && (tree_int_cst_sgn (top
) < 0
12603 || tree_int_cst_sgn (bottom
) < 0)))
12605 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12613 #define tree_expr_nonnegative_warnv_p(X, Y) \
12614 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12616 #define RECURSE(X) \
12617 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12619 /* Return true if CODE or TYPE is known to be non-negative. */
12622 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12624 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12625 && truth_value_p (code
))
12626 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12627 have a signed:1 type (where the value is -1 and 0). */
12632 /* Return true if (CODE OP0) is known to be non-negative. If the return
12633 value is based on the assumption that signed overflow is undefined,
12634 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12635 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12638 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12639 bool *strict_overflow_p
, int depth
)
12641 if (TYPE_UNSIGNED (type
))
12647 /* We can't return 1 if flag_wrapv is set because
12648 ABS_EXPR<INT_MIN> = INT_MIN. */
12649 if (!ANY_INTEGRAL_TYPE_P (type
))
12651 if (TYPE_OVERFLOW_UNDEFINED (type
))
12653 *strict_overflow_p
= true;
12658 case NON_LVALUE_EXPR
:
12660 case FIX_TRUNC_EXPR
:
12661 return RECURSE (op0
);
12665 tree inner_type
= TREE_TYPE (op0
);
12666 tree outer_type
= type
;
12668 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12670 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12671 return RECURSE (op0
);
12672 if (INTEGRAL_TYPE_P (inner_type
))
12674 if (TYPE_UNSIGNED (inner_type
))
12676 return RECURSE (op0
);
12679 else if (INTEGRAL_TYPE_P (outer_type
))
12681 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12682 return RECURSE (op0
);
12683 if (INTEGRAL_TYPE_P (inner_type
))
12684 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12685 && TYPE_UNSIGNED (inner_type
);
12691 return tree_simple_nonnegative_warnv_p (code
, type
);
12694 /* We don't know sign of `t', so be conservative and return false. */
12698 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12699 value is based on the assumption that signed overflow is undefined,
12700 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12701 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12704 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12705 tree op1
, bool *strict_overflow_p
,
12708 if (TYPE_UNSIGNED (type
))
12713 case POINTER_PLUS_EXPR
:
12715 if (FLOAT_TYPE_P (type
))
12716 return RECURSE (op0
) && RECURSE (op1
);
12718 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12719 both unsigned and at least 2 bits shorter than the result. */
12720 if (TREE_CODE (type
) == INTEGER_TYPE
12721 && TREE_CODE (op0
) == NOP_EXPR
12722 && TREE_CODE (op1
) == NOP_EXPR
)
12724 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12725 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12726 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12727 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12729 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12730 TYPE_PRECISION (inner2
)) + 1;
12731 return prec
< TYPE_PRECISION (type
);
12737 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12739 /* x * x is always non-negative for floating point x
12740 or without overflow. */
12741 if (operand_equal_p (op0
, op1
, 0)
12742 || (RECURSE (op0
) && RECURSE (op1
)))
12744 if (ANY_INTEGRAL_TYPE_P (type
)
12745 && TYPE_OVERFLOW_UNDEFINED (type
))
12746 *strict_overflow_p
= true;
12751 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12752 both unsigned and their total bits is shorter than the result. */
12753 if (TREE_CODE (type
) == INTEGER_TYPE
12754 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12755 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12757 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12758 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12760 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12761 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12764 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12765 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12767 if (TREE_CODE (op0
) == INTEGER_CST
)
12768 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12770 if (TREE_CODE (op1
) == INTEGER_CST
)
12771 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12773 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12774 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12776 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12777 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12778 : TYPE_PRECISION (inner0
);
12780 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12781 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12782 : TYPE_PRECISION (inner1
);
12784 return precision0
+ precision1
< TYPE_PRECISION (type
);
12791 return RECURSE (op0
) || RECURSE (op1
);
12797 case TRUNC_DIV_EXPR
:
12798 case CEIL_DIV_EXPR
:
12799 case FLOOR_DIV_EXPR
:
12800 case ROUND_DIV_EXPR
:
12801 return RECURSE (op0
) && RECURSE (op1
);
12803 case TRUNC_MOD_EXPR
:
12804 return RECURSE (op0
);
12806 case FLOOR_MOD_EXPR
:
12807 return RECURSE (op1
);
12809 case CEIL_MOD_EXPR
:
12810 case ROUND_MOD_EXPR
:
12812 return tree_simple_nonnegative_warnv_p (code
, type
);
12815 /* We don't know sign of `t', so be conservative and return false. */
12819 /* Return true if T 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_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12827 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12830 switch (TREE_CODE (t
))
12833 return tree_int_cst_sgn (t
) >= 0;
12836 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12839 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12842 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12845 /* Limit the depth of recursion to avoid quadratic behavior.
12846 This is expected to catch almost all occurrences in practice.
12847 If this code misses important cases that unbounded recursion
12848 would not, passes that need this information could be revised
12849 to provide it through dataflow propagation. */
12850 return (!name_registered_for_update_p (t
)
12851 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12852 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12853 strict_overflow_p
, depth
));
12856 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12860 /* Return true if T is known to be non-negative. If the return
12861 value is based on the assumption that signed overflow is undefined,
12862 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12863 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12866 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
12867 bool *strict_overflow_p
, int depth
)
12888 case CFN_BUILT_IN_BSWAP32
:
12889 case CFN_BUILT_IN_BSWAP64
:
12894 /* sqrt(-0.0) is -0.0. */
12895 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
12897 return RECURSE (arg0
);
12923 CASE_CFN_NEARBYINT
:
12930 CASE_CFN_SIGNIFICAND
:
12934 /* True if the 1st argument is nonnegative. */
12935 return RECURSE (arg0
);
12938 /* True if the 1st OR 2nd arguments are nonnegative. */
12939 return RECURSE (arg0
) || RECURSE (arg1
);
12942 /* True if the 1st AND 2nd arguments are nonnegative. */
12943 return RECURSE (arg0
) && RECURSE (arg1
);
12946 /* True if the 2nd argument is nonnegative. */
12947 return RECURSE (arg1
);
12950 /* True if the 1st argument is nonnegative or the second
12951 argument is an even integer. */
12952 if (TREE_CODE (arg1
) == INTEGER_CST
12953 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
12955 return RECURSE (arg0
);
12958 /* True if the 1st argument is nonnegative or the second
12959 argument is an even integer valued real. */
12960 if (TREE_CODE (arg1
) == REAL_CST
)
12965 c
= TREE_REAL_CST (arg1
);
12966 n
= real_to_integer (&c
);
12969 REAL_VALUE_TYPE cint
;
12970 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
12971 if (real_identical (&c
, &cint
))
12975 return RECURSE (arg0
);
12980 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
12983 /* Return true if T is known to be non-negative. If the return
12984 value is based on the assumption that signed overflow is undefined,
12985 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12986 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12989 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12991 enum tree_code code
= TREE_CODE (t
);
12992 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12999 tree temp
= TARGET_EXPR_SLOT (t
);
13000 t
= TARGET_EXPR_INITIAL (t
);
13002 /* If the initializer is non-void, then it's a normal expression
13003 that will be assigned to the slot. */
13004 if (!VOID_TYPE_P (t
))
13005 return RECURSE (t
);
13007 /* Otherwise, the initializer sets the slot in some way. One common
13008 way is an assignment statement at the end of the initializer. */
13011 if (TREE_CODE (t
) == BIND_EXPR
)
13012 t
= expr_last (BIND_EXPR_BODY (t
));
13013 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13014 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13015 t
= expr_last (TREE_OPERAND (t
, 0));
13016 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13021 if (TREE_CODE (t
) == MODIFY_EXPR
13022 && TREE_OPERAND (t
, 0) == temp
)
13023 return RECURSE (TREE_OPERAND (t
, 1));
13030 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13031 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13033 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13034 get_call_combined_fn (t
),
13037 strict_overflow_p
, depth
);
13039 case COMPOUND_EXPR
:
13041 return RECURSE (TREE_OPERAND (t
, 1));
13044 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13047 return RECURSE (TREE_OPERAND (t
, 0));
13050 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13055 #undef tree_expr_nonnegative_warnv_p
13057 /* Return true if T is known to be non-negative. If the return
13058 value is based on the assumption that signed overflow is undefined,
13059 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13060 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13063 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13065 enum tree_code code
;
13066 if (t
== error_mark_node
)
13069 code
= TREE_CODE (t
);
13070 switch (TREE_CODE_CLASS (code
))
13073 case tcc_comparison
:
13074 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13076 TREE_OPERAND (t
, 0),
13077 TREE_OPERAND (t
, 1),
13078 strict_overflow_p
, depth
);
13081 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13083 TREE_OPERAND (t
, 0),
13084 strict_overflow_p
, depth
);
13087 case tcc_declaration
:
13088 case tcc_reference
:
13089 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13097 case TRUTH_AND_EXPR
:
13098 case TRUTH_OR_EXPR
:
13099 case TRUTH_XOR_EXPR
:
13100 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13102 TREE_OPERAND (t
, 0),
13103 TREE_OPERAND (t
, 1),
13104 strict_overflow_p
, depth
);
13105 case TRUTH_NOT_EXPR
:
13106 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13108 TREE_OPERAND (t
, 0),
13109 strict_overflow_p
, depth
);
13116 case WITH_SIZE_EXPR
:
13118 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13121 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13125 /* Return true if `t' is known to be non-negative. Handle warnings
13126 about undefined signed overflow. */
13129 tree_expr_nonnegative_p (tree t
)
13131 bool ret
, strict_overflow_p
;
13133 strict_overflow_p
= false;
13134 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13135 if (strict_overflow_p
)
13136 fold_overflow_warning (("assuming signed overflow does not occur when "
13137 "determining that expression is always "
13139 WARN_STRICT_OVERFLOW_MISC
);
13144 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13145 For floating point we further ensure that T is not denormal.
13146 Similar logic is present in nonzero_address in rtlanal.h.
13148 If the return value is based on the assumption that signed overflow
13149 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13150 change *STRICT_OVERFLOW_P. */
13153 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13154 bool *strict_overflow_p
)
13159 return tree_expr_nonzero_warnv_p (op0
,
13160 strict_overflow_p
);
13164 tree inner_type
= TREE_TYPE (op0
);
13165 tree outer_type
= type
;
13167 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13168 && tree_expr_nonzero_warnv_p (op0
,
13169 strict_overflow_p
));
13173 case NON_LVALUE_EXPR
:
13174 return tree_expr_nonzero_warnv_p (op0
,
13175 strict_overflow_p
);
13184 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13185 For floating point we further ensure that T is not denormal.
13186 Similar logic is present in nonzero_address in rtlanal.h.
13188 If the return value is based on the assumption that signed overflow
13189 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13190 change *STRICT_OVERFLOW_P. */
13193 tree_binary_nonzero_warnv_p (enum tree_code code
,
13196 tree op1
, bool *strict_overflow_p
)
13198 bool sub_strict_overflow_p
;
13201 case POINTER_PLUS_EXPR
:
13203 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13205 /* With the presence of negative values it is hard
13206 to say something. */
13207 sub_strict_overflow_p
= false;
13208 if (!tree_expr_nonnegative_warnv_p (op0
,
13209 &sub_strict_overflow_p
)
13210 || !tree_expr_nonnegative_warnv_p (op1
,
13211 &sub_strict_overflow_p
))
13213 /* One of operands must be positive and the other non-negative. */
13214 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13215 overflows, on a twos-complement machine the sum of two
13216 nonnegative numbers can never be zero. */
13217 return (tree_expr_nonzero_warnv_p (op0
,
13219 || tree_expr_nonzero_warnv_p (op1
,
13220 strict_overflow_p
));
13225 if (TYPE_OVERFLOW_UNDEFINED (type
))
13227 if (tree_expr_nonzero_warnv_p (op0
,
13229 && tree_expr_nonzero_warnv_p (op1
,
13230 strict_overflow_p
))
13232 *strict_overflow_p
= true;
13239 sub_strict_overflow_p
= false;
13240 if (tree_expr_nonzero_warnv_p (op0
,
13241 &sub_strict_overflow_p
)
13242 && tree_expr_nonzero_warnv_p (op1
,
13243 &sub_strict_overflow_p
))
13245 if (sub_strict_overflow_p
)
13246 *strict_overflow_p
= true;
13251 sub_strict_overflow_p
= false;
13252 if (tree_expr_nonzero_warnv_p (op0
,
13253 &sub_strict_overflow_p
))
13255 if (sub_strict_overflow_p
)
13256 *strict_overflow_p
= true;
13258 /* When both operands are nonzero, then MAX must be too. */
13259 if (tree_expr_nonzero_warnv_p (op1
,
13260 strict_overflow_p
))
13263 /* MAX where operand 0 is positive is positive. */
13264 return tree_expr_nonnegative_warnv_p (op0
,
13265 strict_overflow_p
);
13267 /* MAX where operand 1 is positive is positive. */
13268 else if (tree_expr_nonzero_warnv_p (op1
,
13269 &sub_strict_overflow_p
)
13270 && tree_expr_nonnegative_warnv_p (op1
,
13271 &sub_strict_overflow_p
))
13273 if (sub_strict_overflow_p
)
13274 *strict_overflow_p
= true;
13280 return (tree_expr_nonzero_warnv_p (op1
,
13282 || tree_expr_nonzero_warnv_p (op0
,
13283 strict_overflow_p
));
13292 /* Return true when T is an address and is known to be nonzero.
13293 For floating point we further ensure that T is not denormal.
13294 Similar logic is present in nonzero_address in rtlanal.h.
13296 If the return value is based on the assumption that signed overflow
13297 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13298 change *STRICT_OVERFLOW_P. */
13301 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13303 bool sub_strict_overflow_p
;
13304 switch (TREE_CODE (t
))
13307 return !integer_zerop (t
);
13311 tree base
= TREE_OPERAND (t
, 0);
13313 if (!DECL_P (base
))
13314 base
= get_base_address (base
);
13316 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
13317 base
= TARGET_EXPR_SLOT (base
);
13322 /* For objects in symbol table check if we know they are non-zero.
13323 Don't do anything for variables and functions before symtab is built;
13324 it is quite possible that they will be declared weak later. */
13325 int nonzero_addr
= maybe_nonzero_address (base
);
13326 if (nonzero_addr
>= 0)
13327 return nonzero_addr
;
13329 /* Function local objects are never NULL. */
13331 && (DECL_CONTEXT (base
)
13332 && TREE_CODE (DECL_CONTEXT (base
)) == FUNCTION_DECL
13333 && auto_var_in_fn_p (base
, DECL_CONTEXT (base
))))
13336 /* Constants are never weak. */
13337 if (CONSTANT_CLASS_P (base
))
13344 sub_strict_overflow_p
= false;
13345 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13346 &sub_strict_overflow_p
)
13347 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13348 &sub_strict_overflow_p
))
13350 if (sub_strict_overflow_p
)
13351 *strict_overflow_p
= true;
13362 #define integer_valued_real_p(X) \
13363 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13365 #define RECURSE(X) \
13366 ((integer_valued_real_p) (X, depth + 1))
13368 /* Return true if the floating point result of (CODE OP0) has an
13369 integer value. We also allow +Inf, -Inf and NaN to be considered
13370 integer values. Return false for signaling NaN.
13372 DEPTH is the current nesting depth of the query. */
13375 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13383 return RECURSE (op0
);
13387 tree type
= TREE_TYPE (op0
);
13388 if (TREE_CODE (type
) == INTEGER_TYPE
)
13390 if (TREE_CODE (type
) == REAL_TYPE
)
13391 return RECURSE (op0
);
13401 /* Return true if the floating point result of (CODE OP0 OP1) has an
13402 integer value. We also allow +Inf, -Inf and NaN to be considered
13403 integer values. Return false for signaling NaN.
13405 DEPTH is the current nesting depth of the query. */
13408 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13417 return RECURSE (op0
) && RECURSE (op1
);
13425 /* Return true if the floating point result of calling FNDECL with arguments
13426 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13427 considered integer values. Return false for signaling NaN. If FNDECL
13428 takes fewer than 2 arguments, the remaining ARGn are null.
13430 DEPTH is the current nesting depth of the query. */
13433 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13439 CASE_CFN_NEARBYINT
:
13447 return RECURSE (arg0
) && RECURSE (arg1
);
13455 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13456 has an integer value. We also allow +Inf, -Inf and NaN to be
13457 considered integer values. Return false for signaling NaN.
13459 DEPTH is the current nesting depth of the query. */
13462 integer_valued_real_single_p (tree t
, int depth
)
13464 switch (TREE_CODE (t
))
13467 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13470 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13473 /* Limit the depth of recursion to avoid quadratic behavior.
13474 This is expected to catch almost all occurrences in practice.
13475 If this code misses important cases that unbounded recursion
13476 would not, passes that need this information could be revised
13477 to provide it through dataflow propagation. */
13478 return (!name_registered_for_update_p (t
)
13479 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13480 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13489 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13490 has an integer value. We also allow +Inf, -Inf and NaN to be
13491 considered integer values. Return false for signaling NaN.
13493 DEPTH is the current nesting depth of the query. */
13496 integer_valued_real_invalid_p (tree t
, int depth
)
13498 switch (TREE_CODE (t
))
13500 case COMPOUND_EXPR
:
13503 return RECURSE (TREE_OPERAND (t
, 1));
13506 return RECURSE (TREE_OPERAND (t
, 0));
13515 #undef integer_valued_real_p
13517 /* Return true if the floating point expression T has an integer value.
13518 We also allow +Inf, -Inf and NaN to be considered integer values.
13519 Return false for signaling NaN.
13521 DEPTH is the current nesting depth of the query. */
13524 integer_valued_real_p (tree t
, int depth
)
13526 if (t
== error_mark_node
)
13529 tree_code code
= TREE_CODE (t
);
13530 switch (TREE_CODE_CLASS (code
))
13533 case tcc_comparison
:
13534 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13535 TREE_OPERAND (t
, 1), depth
);
13538 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13541 case tcc_declaration
:
13542 case tcc_reference
:
13543 return integer_valued_real_single_p (t
, depth
);
13553 return integer_valued_real_single_p (t
, depth
);
13557 tree arg0
= (call_expr_nargs (t
) > 0
13558 ? CALL_EXPR_ARG (t
, 0)
13560 tree arg1
= (call_expr_nargs (t
) > 1
13561 ? CALL_EXPR_ARG (t
, 1)
13563 return integer_valued_real_call_p (get_call_combined_fn (t
),
13564 arg0
, arg1
, depth
);
13568 return integer_valued_real_invalid_p (t
, depth
);
13572 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13573 attempt to fold the expression to a constant without modifying TYPE,
13576 If the expression could be simplified to a constant, then return
13577 the constant. If the expression would not be simplified to a
13578 constant, then return NULL_TREE. */
13581 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13583 tree tem
= fold_binary (code
, type
, op0
, op1
);
13584 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13587 /* Given the components of a unary expression CODE, TYPE and OP0,
13588 attempt to fold the expression to a constant without modifying
13591 If the expression could be simplified to a constant, then return
13592 the constant. If the expression would not be simplified to a
13593 constant, then return NULL_TREE. */
13596 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13598 tree tem
= fold_unary (code
, type
, op0
);
13599 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13602 /* If EXP represents referencing an element in a constant string
13603 (either via pointer arithmetic or array indexing), return the
13604 tree representing the value accessed, otherwise return NULL. */
13607 fold_read_from_constant_string (tree exp
)
13609 if ((TREE_CODE (exp
) == INDIRECT_REF
13610 || TREE_CODE (exp
) == ARRAY_REF
)
13611 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13613 tree exp1
= TREE_OPERAND (exp
, 0);
13616 location_t loc
= EXPR_LOCATION (exp
);
13618 if (TREE_CODE (exp
) == INDIRECT_REF
)
13619 string
= string_constant (exp1
, &index
);
13622 tree low_bound
= array_ref_low_bound (exp
);
13623 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13625 /* Optimize the special-case of a zero lower bound.
13627 We convert the low_bound to sizetype to avoid some problems
13628 with constant folding. (E.g. suppose the lower bound is 1,
13629 and its mode is QI. Without the conversion,l (ARRAY
13630 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13631 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13632 if (! integer_zerop (low_bound
))
13633 index
= size_diffop_loc (loc
, index
,
13634 fold_convert_loc (loc
, sizetype
, low_bound
));
13640 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13641 && TREE_CODE (string
) == STRING_CST
13642 && TREE_CODE (index
) == INTEGER_CST
13643 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13644 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
13646 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
13647 return build_int_cst_type (TREE_TYPE (exp
),
13648 (TREE_STRING_POINTER (string
)
13649 [TREE_INT_CST_LOW (index
)]));
13654 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13655 an integer constant, real, or fixed-point constant.
13657 TYPE is the type of the result. */
13660 fold_negate_const (tree arg0
, tree type
)
13662 tree t
= NULL_TREE
;
13664 switch (TREE_CODE (arg0
))
13669 wide_int val
= wi::neg (arg0
, &overflow
);
13670 t
= force_fit_type (type
, val
, 1,
13671 (overflow
| TREE_OVERFLOW (arg0
))
13672 && !TYPE_UNSIGNED (type
));
13677 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13682 FIXED_VALUE_TYPE f
;
13683 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13684 &(TREE_FIXED_CST (arg0
)), NULL
,
13685 TYPE_SATURATING (type
));
13686 t
= build_fixed (type
, f
);
13687 /* Propagate overflow flags. */
13688 if (overflow_p
| TREE_OVERFLOW (arg0
))
13689 TREE_OVERFLOW (t
) = 1;
13694 gcc_unreachable ();
13700 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13701 an integer constant or real constant.
13703 TYPE is the type of the result. */
13706 fold_abs_const (tree arg0
, tree type
)
13708 tree t
= NULL_TREE
;
13710 switch (TREE_CODE (arg0
))
13714 /* If the value is unsigned or non-negative, then the absolute value
13715 is the same as the ordinary value. */
13716 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13719 /* If the value is negative, then the absolute value is
13724 wide_int val
= wi::neg (arg0
, &overflow
);
13725 t
= force_fit_type (type
, val
, -1,
13726 overflow
| TREE_OVERFLOW (arg0
));
13732 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13733 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13739 gcc_unreachable ();
13745 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13746 constant. TYPE is the type of the result. */
13749 fold_not_const (const_tree arg0
, tree type
)
13751 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13753 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13756 /* Given CODE, a relational operator, the target type, TYPE and two
13757 constant operands OP0 and OP1, return the result of the
13758 relational operation. If the result is not a compile time
13759 constant, then return NULL_TREE. */
13762 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13764 int result
, invert
;
13766 /* From here on, the only cases we handle are when the result is
13767 known to be a constant. */
13769 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13771 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13772 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13774 /* Handle the cases where either operand is a NaN. */
13775 if (real_isnan (c0
) || real_isnan (c1
))
13785 case UNORDERED_EXPR
:
13799 if (flag_trapping_math
)
13805 gcc_unreachable ();
13808 return constant_boolean_node (result
, type
);
13811 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13814 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13816 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13817 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13818 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13821 /* Handle equality/inequality of complex constants. */
13822 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13824 tree rcond
= fold_relational_const (code
, type
,
13825 TREE_REALPART (op0
),
13826 TREE_REALPART (op1
));
13827 tree icond
= fold_relational_const (code
, type
,
13828 TREE_IMAGPART (op0
),
13829 TREE_IMAGPART (op1
));
13830 if (code
== EQ_EXPR
)
13831 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13832 else if (code
== NE_EXPR
)
13833 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13838 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13840 if (!VECTOR_TYPE_P (type
))
13842 /* Have vector comparison with scalar boolean result. */
13843 bool result
= true;
13844 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
13845 && VECTOR_CST_NELTS (op0
) == VECTOR_CST_NELTS (op1
));
13846 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (op0
); i
++)
13848 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13849 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13850 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
13851 result
&= integer_onep (tmp
);
13853 if (code
== NE_EXPR
)
13855 return constant_boolean_node (result
, type
);
13857 unsigned count
= VECTOR_CST_NELTS (op0
);
13858 tree
*elts
= XALLOCAVEC (tree
, count
);
13859 gcc_assert (VECTOR_CST_NELTS (op1
) == count
13860 && TYPE_VECTOR_SUBPARTS (type
) == count
);
13862 for (unsigned i
= 0; i
< count
; i
++)
13864 tree elem_type
= TREE_TYPE (type
);
13865 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13866 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13868 tree tem
= fold_relational_const (code
, elem_type
,
13871 if (tem
== NULL_TREE
)
13874 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
13877 return build_vector (type
, elts
);
13880 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13882 To compute GT, swap the arguments and do LT.
13883 To compute GE, do LT and invert the result.
13884 To compute LE, swap the arguments, do LT and invert the result.
13885 To compute NE, do EQ and invert the result.
13887 Therefore, the code below must handle only EQ and LT. */
13889 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13891 std::swap (op0
, op1
);
13892 code
= swap_tree_comparison (code
);
13895 /* Note that it is safe to invert for real values here because we
13896 have already handled the one case that it matters. */
13899 if (code
== NE_EXPR
|| code
== GE_EXPR
)
13902 code
= invert_tree_comparison (code
, false);
13905 /* Compute a result for LT or EQ if args permit;
13906 Otherwise return T. */
13907 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
13909 if (code
== EQ_EXPR
)
13910 result
= tree_int_cst_equal (op0
, op1
);
13912 result
= tree_int_cst_lt (op0
, op1
);
13919 return constant_boolean_node (result
, type
);
13922 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13923 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13927 fold_build_cleanup_point_expr (tree type
, tree expr
)
13929 /* If the expression does not have side effects then we don't have to wrap
13930 it with a cleanup point expression. */
13931 if (!TREE_SIDE_EFFECTS (expr
))
13934 /* If the expression is a return, check to see if the expression inside the
13935 return has no side effects or the right hand side of the modify expression
13936 inside the return. If either don't have side effects set we don't need to
13937 wrap the expression in a cleanup point expression. Note we don't check the
13938 left hand side of the modify because it should always be a return decl. */
13939 if (TREE_CODE (expr
) == RETURN_EXPR
)
13941 tree op
= TREE_OPERAND (expr
, 0);
13942 if (!op
|| !TREE_SIDE_EFFECTS (op
))
13944 op
= TREE_OPERAND (op
, 1);
13945 if (!TREE_SIDE_EFFECTS (op
))
13949 return build1 (CLEANUP_POINT_EXPR
, type
, expr
);
13952 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13953 of an indirection through OP0, or NULL_TREE if no simplification is
13957 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
13963 subtype
= TREE_TYPE (sub
);
13964 if (!POINTER_TYPE_P (subtype
))
13967 if (TREE_CODE (sub
) == ADDR_EXPR
)
13969 tree op
= TREE_OPERAND (sub
, 0);
13970 tree optype
= TREE_TYPE (op
);
13971 /* *&CONST_DECL -> to the value of the const decl. */
13972 if (TREE_CODE (op
) == CONST_DECL
)
13973 return DECL_INITIAL (op
);
13974 /* *&p => p; make sure to handle *&"str"[cst] here. */
13975 if (type
== optype
)
13977 tree fop
= fold_read_from_constant_string (op
);
13983 /* *(foo *)&fooarray => fooarray[0] */
13984 else if (TREE_CODE (optype
) == ARRAY_TYPE
13985 && type
== TREE_TYPE (optype
)
13986 && (!in_gimple_form
13987 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
13989 tree type_domain
= TYPE_DOMAIN (optype
);
13990 tree min_val
= size_zero_node
;
13991 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
13992 min_val
= TYPE_MIN_VALUE (type_domain
);
13994 && TREE_CODE (min_val
) != INTEGER_CST
)
13996 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
13997 NULL_TREE
, NULL_TREE
);
13999 /* *(foo *)&complexfoo => __real__ complexfoo */
14000 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14001 && type
== TREE_TYPE (optype
))
14002 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14003 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14004 else if (TREE_CODE (optype
) == VECTOR_TYPE
14005 && type
== TREE_TYPE (optype
))
14007 tree part_width
= TYPE_SIZE (type
);
14008 tree index
= bitsize_int (0);
14009 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14013 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14014 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14016 tree op00
= TREE_OPERAND (sub
, 0);
14017 tree op01
= TREE_OPERAND (sub
, 1);
14020 if (TREE_CODE (op00
) == ADDR_EXPR
)
14023 op00
= TREE_OPERAND (op00
, 0);
14024 op00type
= TREE_TYPE (op00
);
14026 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14027 if (TREE_CODE (op00type
) == VECTOR_TYPE
14028 && type
== TREE_TYPE (op00type
))
14030 tree part_width
= TYPE_SIZE (type
);
14031 unsigned HOST_WIDE_INT max_offset
14032 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14033 * TYPE_VECTOR_SUBPARTS (op00type
));
14034 if (tree_int_cst_sign_bit (op01
) == 0
14035 && compare_tree_int (op01
, max_offset
) == -1)
14037 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (op01
);
14038 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14039 tree index
= bitsize_int (indexi
);
14040 return fold_build3_loc (loc
,
14041 BIT_FIELD_REF
, type
, op00
,
14042 part_width
, index
);
14045 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14046 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14047 && type
== TREE_TYPE (op00type
))
14049 tree size
= TYPE_SIZE_UNIT (type
);
14050 if (tree_int_cst_equal (size
, op01
))
14051 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14053 /* ((foo *)&fooarray)[1] => fooarray[1] */
14054 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14055 && type
== TREE_TYPE (op00type
))
14057 tree type_domain
= TYPE_DOMAIN (op00type
);
14058 tree min_val
= size_zero_node
;
14059 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14060 min_val
= TYPE_MIN_VALUE (type_domain
);
14061 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
14062 TYPE_SIZE_UNIT (type
));
14063 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
14064 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14065 NULL_TREE
, NULL_TREE
);
14070 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14071 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14072 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14073 && (!in_gimple_form
14074 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14077 tree min_val
= size_zero_node
;
14078 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14079 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14080 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14081 min_val
= TYPE_MIN_VALUE (type_domain
);
14083 && TREE_CODE (min_val
) != INTEGER_CST
)
14085 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14092 /* Builds an expression for an indirection through T, simplifying some
14096 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14098 tree type
= TREE_TYPE (TREE_TYPE (t
));
14099 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14104 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14107 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14110 fold_indirect_ref_loc (location_t loc
, tree t
)
14112 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14120 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14121 whose result is ignored. The type of the returned tree need not be
14122 the same as the original expression. */
14125 fold_ignored_result (tree t
)
14127 if (!TREE_SIDE_EFFECTS (t
))
14128 return integer_zero_node
;
14131 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14134 t
= TREE_OPERAND (t
, 0);
14138 case tcc_comparison
:
14139 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14140 t
= TREE_OPERAND (t
, 0);
14141 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14142 t
= TREE_OPERAND (t
, 1);
14147 case tcc_expression
:
14148 switch (TREE_CODE (t
))
14150 case COMPOUND_EXPR
:
14151 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14153 t
= TREE_OPERAND (t
, 0);
14157 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14158 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14160 t
= TREE_OPERAND (t
, 0);
14173 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14176 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14178 tree div
= NULL_TREE
;
14183 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14184 have to do anything. Only do this when we are not given a const,
14185 because in that case, this check is more expensive than just
14187 if (TREE_CODE (value
) != INTEGER_CST
)
14189 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14191 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14195 /* If divisor is a power of two, simplify this to bit manipulation. */
14196 if (divisor
== (divisor
& -divisor
))
14198 if (TREE_CODE (value
) == INTEGER_CST
)
14200 wide_int val
= value
;
14203 if ((val
& (divisor
- 1)) == 0)
14206 overflow_p
= TREE_OVERFLOW (value
);
14207 val
+= divisor
- 1;
14208 val
&= - (int) divisor
;
14212 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14218 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14219 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14220 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14221 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14227 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14228 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14229 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14235 /* Likewise, but round down. */
14238 round_down_loc (location_t loc
, tree value
, int divisor
)
14240 tree div
= NULL_TREE
;
14242 gcc_assert (divisor
> 0);
14246 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14247 have to do anything. Only do this when we are not given a const,
14248 because in that case, this check is more expensive than just
14250 if (TREE_CODE (value
) != INTEGER_CST
)
14252 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14254 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14258 /* If divisor is a power of two, simplify this to bit manipulation. */
14259 if (divisor
== (divisor
& -divisor
))
14263 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14264 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14269 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14270 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14271 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14277 /* Returns the pointer to the base of the object addressed by EXP and
14278 extracts the information about the offset of the access, storing it
14279 to PBITPOS and POFFSET. */
14282 split_address_to_core_and_offset (tree exp
,
14283 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14287 int unsignedp
, reversep
, volatilep
;
14288 HOST_WIDE_INT bitsize
;
14289 location_t loc
= EXPR_LOCATION (exp
);
14291 if (TREE_CODE (exp
) == ADDR_EXPR
)
14293 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14294 poffset
, &mode
, &unsignedp
, &reversep
,
14295 &volatilep
, false);
14296 core
= build_fold_addr_expr_loc (loc
, core
);
14302 *poffset
= NULL_TREE
;
14308 /* Returns true if addresses of E1 and E2 differ by a constant, false
14309 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14312 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14315 HOST_WIDE_INT bitpos1
, bitpos2
;
14316 tree toffset1
, toffset2
, tdiff
, type
;
14318 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14319 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14321 if (bitpos1
% BITS_PER_UNIT
!= 0
14322 || bitpos2
% BITS_PER_UNIT
!= 0
14323 || !operand_equal_p (core1
, core2
, 0))
14326 if (toffset1
&& toffset2
)
14328 type
= TREE_TYPE (toffset1
);
14329 if (type
!= TREE_TYPE (toffset2
))
14330 toffset2
= fold_convert (type
, toffset2
);
14332 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14333 if (!cst_and_fits_in_hwi (tdiff
))
14336 *diff
= int_cst_value (tdiff
);
14338 else if (toffset1
|| toffset2
)
14340 /* If only one of the offsets is non-constant, the difference cannot
14347 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14351 /* Return OFF converted to a pointer offset type suitable as offset for
14352 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14354 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14356 return fold_convert_loc (loc
, sizetype
, off
);
14359 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14361 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14363 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14364 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14367 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14369 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14371 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14372 ptr
, size_int (off
));
14375 /* Return a char pointer for a C string if it is a string constant
14376 or sum of string constant and integer constant. */
14379 c_getstr (tree src
)
14383 src
= string_constant (src
, &offset_node
);
14387 if (offset_node
== 0)
14388 return TREE_STRING_POINTER (src
);
14389 else if (!tree_fits_uhwi_p (offset_node
)
14390 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
14393 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);
14398 namespace selftest
{
14400 /* Helper functions for writing tests of folding trees. */
14402 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14405 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
14408 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
14411 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14412 wrapping WRAPPED_EXPR. */
14415 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
14418 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
14419 ASSERT_NE (wrapped_expr
, result
);
14420 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
14421 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
14424 /* Verify that various arithmetic binary operations are folded
14428 test_arithmetic_folding ()
14430 tree type
= integer_type_node
;
14431 tree x
= create_tmp_var_raw (type
, "x");
14432 tree zero
= build_zero_cst (type
);
14433 tree one
= build_int_cst (type
, 1);
14436 /* 1 <-- (0 + 1) */
14437 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
14439 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
14442 /* (nonlvalue)x <-- (x + 0) */
14443 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
14447 /* 0 <-- (x - x) */
14448 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
14450 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
14453 /* Multiplication. */
14454 /* 0 <-- (x * 0) */
14455 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
14458 /* (nonlvalue)x <-- (x * 1) */
14459 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
14463 /* Run all of the selftests within this file. */
14466 fold_const_c_tests ()
14468 test_arithmetic_folding ();
14471 } // namespace selftest
14473 #endif /* CHECKING_P */