1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
45 #include "coretypes.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
57 #include "diagnostic-core.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
64 #include "tree-iterator.h"
67 #include "langhooks.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
75 #include "tree-into-ssa.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
80 #include "tree-ssanames.h"
83 /* Nonzero if we are folding constants inside an initializer; zero
85 int folding_initializer
= 0;
87 /* The following constants represent a bit based encoding of GCC's
88 comparison operators. This encoding simplifies transformations
89 on relational comparison operators, such as AND and OR. */
90 enum comparison_code
{
109 static bool negate_expr_p (tree
);
110 static tree
negate_expr (tree
);
111 static tree
split_tree (location_t
, tree
, tree
, enum tree_code
,
112 tree
*, tree
*, tree
*, int);
113 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
114 static enum comparison_code
comparison_to_compcode (enum tree_code
);
115 static enum tree_code
compcode_to_comparison (enum comparison_code
);
116 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
117 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
118 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
119 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
121 static int simple_operand_p (const_tree
);
122 static bool simple_operand_p_2 (tree
);
123 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
124 static tree
range_predecessor (tree
);
125 static tree
range_successor (tree
);
126 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
127 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
128 static tree
unextend (tree
, int, int, tree
);
129 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
130 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
131 static tree
fold_binary_op_with_conditional_arg (location_t
,
132 enum tree_code
, tree
,
135 static tree
fold_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
136 static tree
fold_negate_const (tree
, tree
);
137 static tree
fold_not_const (const_tree
, tree
);
138 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
139 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
140 static tree
fold_view_convert_expr (tree
, tree
);
141 static bool vec_cst_ctor_to_array (tree
, tree
*);
142 static tree
fold_negate_expr (location_t
, tree
);
145 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
146 Otherwise, return LOC. */
149 expr_location_or (tree t
, location_t loc
)
151 location_t tloc
= EXPR_LOCATION (t
);
152 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
155 /* Similar to protected_set_expr_location, but never modify x in place,
156 if location can and needs to be set, unshare it. */
159 protected_set_expr_location_unshare (tree x
, location_t loc
)
161 if (CAN_HAVE_LOCATION_P (x
)
162 && EXPR_LOCATION (x
) != loc
163 && !(TREE_CODE (x
) == SAVE_EXPR
164 || TREE_CODE (x
) == TARGET_EXPR
165 || TREE_CODE (x
) == BIND_EXPR
))
168 SET_EXPR_LOCATION (x
, loc
);
173 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
174 division and returns the quotient. Otherwise returns
178 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
182 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
184 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
189 /* This is nonzero if we should defer warnings about undefined
190 overflow. This facility exists because these warnings are a
191 special case. The code to estimate loop iterations does not want
192 to issue any warnings, since it works with expressions which do not
193 occur in user code. Various bits of cleanup code call fold(), but
194 only use the result if it has certain characteristics (e.g., is a
195 constant); that code only wants to issue a warning if the result is
198 static int fold_deferring_overflow_warnings
;
200 /* If a warning about undefined overflow is deferred, this is the
201 warning. Note that this may cause us to turn two warnings into
202 one, but that is fine since it is sufficient to only give one
203 warning per expression. */
205 static const char* fold_deferred_overflow_warning
;
207 /* If a warning about undefined overflow is deferred, this is the
208 level at which the warning should be emitted. */
210 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
212 /* Start deferring overflow warnings. We could use a stack here to
213 permit nested calls, but at present it is not necessary. */
216 fold_defer_overflow_warnings (void)
218 ++fold_deferring_overflow_warnings
;
221 /* Stop deferring overflow warnings. If there is a pending warning,
222 and ISSUE is true, then issue the warning if appropriate. STMT is
223 the statement with which the warning should be associated (used for
224 location information); STMT may be NULL. CODE is the level of the
225 warning--a warn_strict_overflow_code value. This function will use
226 the smaller of CODE and the deferred code when deciding whether to
227 issue the warning. CODE may be zero to mean to always use the
231 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
236 gcc_assert (fold_deferring_overflow_warnings
> 0);
237 --fold_deferring_overflow_warnings
;
238 if (fold_deferring_overflow_warnings
> 0)
240 if (fold_deferred_overflow_warning
!= NULL
242 && code
< (int) fold_deferred_overflow_code
)
243 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
247 warnmsg
= fold_deferred_overflow_warning
;
248 fold_deferred_overflow_warning
= NULL
;
250 if (!issue
|| warnmsg
== NULL
)
253 if (gimple_no_warning_p (stmt
))
256 /* Use the smallest code level when deciding to issue the
258 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
259 code
= fold_deferred_overflow_code
;
261 if (!issue_strict_overflow_warning (code
))
265 locus
= input_location
;
267 locus
= gimple_location (stmt
);
268 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
271 /* Stop deferring overflow warnings, ignoring any deferred
275 fold_undefer_and_ignore_overflow_warnings (void)
277 fold_undefer_overflow_warnings (false, NULL
, 0);
280 /* Whether we are deferring overflow warnings. */
283 fold_deferring_overflow_warnings_p (void)
285 return fold_deferring_overflow_warnings
> 0;
288 /* This is called when we fold something based on the fact that signed
289 overflow is undefined. */
292 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
294 if (fold_deferring_overflow_warnings
> 0)
296 if (fold_deferred_overflow_warning
== NULL
297 || wc
< fold_deferred_overflow_code
)
299 fold_deferred_overflow_warning
= gmsgid
;
300 fold_deferred_overflow_code
= wc
;
303 else if (issue_strict_overflow_warning (wc
))
304 warning (OPT_Wstrict_overflow
, gmsgid
);
307 /* Return true if the built-in mathematical function specified by CODE
308 is odd, i.e. -f(x) == f(-x). */
311 negate_mathfn_p (combined_fn fn
)
344 return !flag_rounding_math
;
352 /* Check whether we may negate an integer constant T without causing
356 may_negate_without_overflow_p (const_tree t
)
360 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
362 type
= TREE_TYPE (t
);
363 if (TYPE_UNSIGNED (type
))
366 return !wi::only_sign_bit_p (t
);
369 /* Determine whether an expression T can be cheaply negated using
370 the function negate_expr without introducing undefined overflow. */
373 negate_expr_p (tree t
)
380 type
= TREE_TYPE (t
);
383 switch (TREE_CODE (t
))
386 if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
389 /* Check that -CST will not overflow type. */
390 return may_negate_without_overflow_p (t
);
392 return (INTEGRAL_TYPE_P (type
)
393 && TYPE_OVERFLOW_WRAPS (type
));
399 return !TYPE_OVERFLOW_SANITIZED (type
);
402 /* We want to canonicalize to positive real constants. Pretend
403 that only negative ones can be easily negated. */
404 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
407 return negate_expr_p (TREE_REALPART (t
))
408 && negate_expr_p (TREE_IMAGPART (t
));
412 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
415 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
417 for (i
= 0; i
< count
; i
++)
418 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
425 return negate_expr_p (TREE_OPERAND (t
, 0))
426 && negate_expr_p (TREE_OPERAND (t
, 1));
429 return negate_expr_p (TREE_OPERAND (t
, 0));
432 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
433 || HONOR_SIGNED_ZEROS (element_mode (type
))
434 || (INTEGRAL_TYPE_P (type
)
435 && ! TYPE_OVERFLOW_WRAPS (type
)))
437 /* -(A + B) -> (-B) - A. */
438 if (negate_expr_p (TREE_OPERAND (t
, 1)))
440 /* -(A + B) -> (-A) - B. */
441 return negate_expr_p (TREE_OPERAND (t
, 0));
444 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
445 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
446 && !HONOR_SIGNED_ZEROS (element_mode (type
))
447 && (! INTEGRAL_TYPE_P (type
)
448 || TYPE_OVERFLOW_WRAPS (type
));
451 if (TYPE_UNSIGNED (type
))
453 /* INT_MIN/n * n doesn't overflow while negating one operand it does
454 if n is a (negative) power of two. */
455 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
456 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
457 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
458 && wi::popcount (wi::abs (TREE_OPERAND (t
, 0))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
460 && wi::popcount (wi::abs (TREE_OPERAND (t
, 1))) != 1)))
466 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
467 return negate_expr_p (TREE_OPERAND (t
, 1))
468 || negate_expr_p (TREE_OPERAND (t
, 0));
474 if (TYPE_UNSIGNED (type
))
476 if (negate_expr_p (TREE_OPERAND (t
, 0)))
478 /* In general we can't negate B in A / B, because if A is INT_MIN and
479 B is 1, we may turn this into INT_MIN / -1 which is undefined
480 and actually traps on some architectures. */
481 if (! INTEGRAL_TYPE_P (TREE_TYPE (t
))
482 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
483 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
484 && ! integer_onep (TREE_OPERAND (t
, 1))))
485 return negate_expr_p (TREE_OPERAND (t
, 1));
489 /* Negate -((double)float) as (double)(-float). */
490 if (TREE_CODE (type
) == REAL_TYPE
)
492 tree tem
= strip_float_extensions (t
);
494 return negate_expr_p (tem
);
499 /* Negate -f(x) as f(-x). */
500 if (negate_mathfn_p (get_call_combined_fn (t
)))
501 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
505 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
506 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
508 tree op1
= TREE_OPERAND (t
, 1);
509 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
520 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
521 simplification is possible.
522 If negate_expr_p would return true for T, NULL_TREE will never be
526 fold_negate_expr_1 (location_t loc
, tree t
)
528 tree type
= TREE_TYPE (t
);
531 switch (TREE_CODE (t
))
533 /* Convert - (~A) to A + 1. */
535 if (INTEGRAL_TYPE_P (type
))
536 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
537 build_one_cst (type
));
541 tem
= fold_negate_const (t
, type
);
542 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
543 || (ANY_INTEGRAL_TYPE_P (type
)
544 && !TYPE_OVERFLOW_TRAPS (type
)
545 && TYPE_OVERFLOW_WRAPS (type
))
546 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
551 tem
= fold_negate_const (t
, type
);
555 tem
= fold_negate_const (t
, type
);
560 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
561 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
563 return build_complex (type
, rpart
, ipart
);
569 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
570 tree
*elts
= XALLOCAVEC (tree
, count
);
572 for (i
= 0; i
< count
; i
++)
574 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
575 if (elts
[i
] == NULL_TREE
)
579 return build_vector (type
, elts
);
583 if (negate_expr_p (t
))
584 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
585 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
586 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
590 if (negate_expr_p (t
))
591 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
592 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
596 if (!TYPE_OVERFLOW_SANITIZED (type
))
597 return TREE_OPERAND (t
, 0);
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
602 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t
, 1)))
607 tem
= negate_expr (TREE_OPERAND (t
, 1));
608 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
609 tem
, TREE_OPERAND (t
, 0));
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t
, 0)))
615 tem
= negate_expr (TREE_OPERAND (t
, 0));
616 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
617 tem
, TREE_OPERAND (t
, 1));
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
625 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
626 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
627 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
631 if (TYPE_UNSIGNED (type
))
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
639 tem
= TREE_OPERAND (t
, 1);
640 if (negate_expr_p (tem
))
641 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
642 TREE_OPERAND (t
, 0), negate_expr (tem
));
643 tem
= TREE_OPERAND (t
, 0);
644 if (negate_expr_p (tem
))
645 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
646 negate_expr (tem
), TREE_OPERAND (t
, 1));
653 if (TYPE_UNSIGNED (type
))
655 if (negate_expr_p (TREE_OPERAND (t
, 0)))
656 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
657 negate_expr (TREE_OPERAND (t
, 0)),
658 TREE_OPERAND (t
, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t
))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
664 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t
, 1))))
666 && negate_expr_p (TREE_OPERAND (t
, 1)))
667 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
669 negate_expr (TREE_OPERAND (t
, 1)));
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type
) == REAL_TYPE
)
676 tem
= strip_float_extensions (t
);
677 if (tem
!= t
&& negate_expr_p (tem
))
678 return fold_convert_loc (loc
, type
, negate_expr (tem
));
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t
))
685 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
689 fndecl
= get_callee_fndecl (t
);
690 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
691 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
699 tree op1
= TREE_OPERAND (t
, 1);
700 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
702 tree ntype
= TYPE_UNSIGNED (type
)
703 ? signed_type_for (type
)
704 : unsigned_type_for (type
);
705 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
706 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
707 return fold_convert_loc (loc
, type
, temp
);
719 /* A wrapper for fold_negate_expr_1. */
722 fold_negate_expr (location_t loc
, tree t
)
724 tree type
= TREE_TYPE (t
);
726 tree tem
= fold_negate_expr_1 (loc
, t
);
727 if (tem
== NULL_TREE
)
729 return fold_convert_loc (loc
, type
, tem
);
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
745 loc
= EXPR_LOCATION (t
);
746 type
= TREE_TYPE (t
);
749 tem
= fold_negate_expr (loc
, t
);
751 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
752 return fold_convert_loc (loc
, type
, tem
);
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
773 If IN is itself a literal or constant, return it as appropriate.
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
779 split_tree (location_t loc
, tree in
, tree type
, enum tree_code code
,
780 tree
*conp
, tree
*litp
, tree
*minus_litp
, int negate_p
)
788 /* Strip any conversions that don't change the machine mode or signedness. */
789 STRIP_SIGN_NOPS (in
);
791 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
792 || TREE_CODE (in
) == FIXED_CST
)
794 else if (TREE_CODE (in
) == code
795 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
796 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
797 /* We can associate addition and subtraction together (even
798 though the C standard doesn't say so) for integers because
799 the value is not affected. For reals, the value might be
800 affected, so we can't. */
801 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == POINTER_PLUS_EXPR
)
802 || (code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
803 || (code
== MINUS_EXPR
804 && (TREE_CODE (in
) == PLUS_EXPR
805 || TREE_CODE (in
) == POINTER_PLUS_EXPR
)))))
807 tree op0
= TREE_OPERAND (in
, 0);
808 tree op1
= TREE_OPERAND (in
, 1);
809 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
810 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
814 || TREE_CODE (op0
) == FIXED_CST
)
815 *litp
= op0
, op0
= 0;
816 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
817 || TREE_CODE (op1
) == FIXED_CST
)
818 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
820 if (op0
!= 0 && TREE_CONSTANT (op0
))
821 *conp
= op0
, op0
= 0;
822 else if (op1
!= 0 && TREE_CONSTANT (op1
))
823 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0
!= 0 && op1
!= 0)
832 var
= op1
, neg_var_p
= neg1_p
;
834 /* Now do any needed negations. */
836 *minus_litp
= *litp
, *litp
= 0;
837 if (neg_conp_p
&& *conp
)
839 /* Convert to TYPE before negating. */
840 *conp
= fold_convert_loc (loc
, type
, *conp
);
841 *conp
= negate_expr (*conp
);
843 if (neg_var_p
&& var
)
845 /* Convert to TYPE before negating. */
846 var
= fold_convert_loc (loc
, type
, var
);
847 var
= negate_expr (var
);
850 else if (TREE_CONSTANT (in
))
852 else if (TREE_CODE (in
) == BIT_NOT_EXPR
853 && code
== PLUS_EXPR
)
855 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *minus_litp
= build_one_cst (TREE_TYPE (in
));
858 var
= negate_expr (TREE_OPERAND (in
, 0));
866 *minus_litp
= *litp
, *litp
= 0;
867 else if (*minus_litp
)
868 *litp
= *minus_litp
, *minus_litp
= 0;
871 /* Convert to TYPE before negating. */
872 *conp
= fold_convert_loc (loc
, type
, *conp
);
873 *conp
= negate_expr (*conp
);
877 /* Convert to TYPE before negating. */
878 var
= fold_convert_loc (loc
, type
, var
);
879 var
= negate_expr (var
);
886 /* Re-associate trees split by the above function. T1 and T2 are
887 either expressions to associate or null. Return the new
888 expression, if any. LOC is the location of the new expression. If
889 we build an operation, do it in TYPE and with CODE. */
892 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
903 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
905 if (code
== PLUS_EXPR
)
907 if (TREE_CODE (t1
) == NEGATE_EXPR
)
908 return build2_loc (loc
, MINUS_EXPR
, type
,
909 fold_convert_loc (loc
, type
, t2
),
910 fold_convert_loc (loc
, type
,
911 TREE_OPERAND (t1
, 0)));
912 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
913 return build2_loc (loc
, MINUS_EXPR
, type
,
914 fold_convert_loc (loc
, type
, t1
),
915 fold_convert_loc (loc
, type
,
916 TREE_OPERAND (t2
, 0)));
917 else if (integer_zerop (t2
))
918 return fold_convert_loc (loc
, type
, t1
);
920 else if (code
== MINUS_EXPR
)
922 if (integer_zerop (t2
))
923 return fold_convert_loc (loc
, type
, t1
);
926 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
927 fold_convert_loc (loc
, type
, t2
));
930 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
931 fold_convert_loc (loc
, type
, t2
));
934 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
935 for use in int_const_binop, size_binop and size_diffop. */
938 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
940 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
942 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
957 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
958 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
959 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
963 /* Combine two integer constants ARG1 and ARG2 under operation CODE
964 to produce a new constant. Return NULL_TREE if we don't know how
965 to evaluate CODE at compile-time. */
968 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
973 tree type
= TREE_TYPE (arg1
);
974 signop sign
= TYPE_SIGN (type
);
975 bool overflow
= false;
977 wide_int arg2
= wi::to_wide (parg2
, TYPE_PRECISION (type
));
982 res
= wi::bit_or (arg1
, arg2
);
986 res
= wi::bit_xor (arg1
, arg2
);
990 res
= wi::bit_and (arg1
, arg2
);
995 if (wi::neg_p (arg2
))
998 if (code
== RSHIFT_EXPR
)
1004 if (code
== RSHIFT_EXPR
)
1005 /* It's unclear from the C standard whether shifts can overflow.
1006 The following code ignores overflow; perhaps a C standard
1007 interpretation ruling is needed. */
1008 res
= wi::rshift (arg1
, arg2
, sign
);
1010 res
= wi::lshift (arg1
, arg2
);
1015 if (wi::neg_p (arg2
))
1018 if (code
== RROTATE_EXPR
)
1019 code
= LROTATE_EXPR
;
1021 code
= RROTATE_EXPR
;
1024 if (code
== RROTATE_EXPR
)
1025 res
= wi::rrotate (arg1
, arg2
);
1027 res
= wi::lrotate (arg1
, arg2
);
1031 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1035 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1039 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1042 case MULT_HIGHPART_EXPR
:
1043 res
= wi::mul_high (arg1
, arg2
, sign
);
1046 case TRUNC_DIV_EXPR
:
1047 case EXACT_DIV_EXPR
:
1050 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1053 case FLOOR_DIV_EXPR
:
1056 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1062 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1065 case ROUND_DIV_EXPR
:
1068 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1071 case TRUNC_MOD_EXPR
:
1074 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1077 case FLOOR_MOD_EXPR
:
1080 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1086 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1089 case ROUND_MOD_EXPR
:
1092 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1096 res
= wi::min (arg1
, arg2
, sign
);
1100 res
= wi::max (arg1
, arg2
, sign
);
1107 t
= force_fit_type (type
, res
, overflowable
,
1108 (((sign
== SIGNED
|| overflowable
== -1)
1110 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1116 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1118 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1121 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1122 constant. We assume ARG1 and ARG2 have the same data type, or at least
1123 are the same kind of constant and the same machine mode. Return zero if
1124 combining the constants is not allowed in the current operating mode. */
1127 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1129 /* Sanity check for the recursive cases. */
1136 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1138 if (code
== POINTER_PLUS_EXPR
)
1139 return int_const_binop (PLUS_EXPR
,
1140 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1142 return int_const_binop (code
, arg1
, arg2
);
1145 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1150 REAL_VALUE_TYPE value
;
1151 REAL_VALUE_TYPE result
;
1155 /* The following codes are handled by real_arithmetic. */
1170 d1
= TREE_REAL_CST (arg1
);
1171 d2
= TREE_REAL_CST (arg2
);
1173 type
= TREE_TYPE (arg1
);
1174 mode
= TYPE_MODE (type
);
1176 /* Don't perform operation if we honor signaling NaNs and
1177 either operand is a signaling NaN. */
1178 if (HONOR_SNANS (mode
)
1179 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1180 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1183 /* Don't perform operation if it would raise a division
1184 by zero exception. */
1185 if (code
== RDIV_EXPR
1186 && real_equal (&d2
, &dconst0
)
1187 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1190 /* If either operand is a NaN, just return it. Otherwise, set up
1191 for floating-point trap; we return an overflow. */
1192 if (REAL_VALUE_ISNAN (d1
))
1194 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1197 t
= build_real (type
, d1
);
1200 else if (REAL_VALUE_ISNAN (d2
))
1202 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1205 t
= build_real (type
, d2
);
1209 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1210 real_convert (&result
, mode
, &value
);
1212 /* Don't constant fold this floating point operation if
1213 the result has overflowed and flag_trapping_math. */
1214 if (flag_trapping_math
1215 && MODE_HAS_INFINITIES (mode
)
1216 && REAL_VALUE_ISINF (result
)
1217 && !REAL_VALUE_ISINF (d1
)
1218 && !REAL_VALUE_ISINF (d2
))
1221 /* Don't constant fold this floating point operation if the
1222 result may dependent upon the run-time rounding mode and
1223 flag_rounding_math is set, or if GCC's software emulation
1224 is unable to accurately represent the result. */
1225 if ((flag_rounding_math
1226 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1227 && (inexact
|| !real_identical (&result
, &value
)))
1230 t
= build_real (type
, result
);
1232 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1236 if (TREE_CODE (arg1
) == FIXED_CST
)
1238 FIXED_VALUE_TYPE f1
;
1239 FIXED_VALUE_TYPE f2
;
1240 FIXED_VALUE_TYPE result
;
1245 /* The following codes are handled by fixed_arithmetic. */
1251 case TRUNC_DIV_EXPR
:
1252 if (TREE_CODE (arg2
) != FIXED_CST
)
1254 f2
= TREE_FIXED_CST (arg2
);
1260 if (TREE_CODE (arg2
) != INTEGER_CST
)
1263 f2
.data
.high
= w2
.elt (1);
1264 f2
.data
.low
= w2
.ulow ();
1273 f1
= TREE_FIXED_CST (arg1
);
1274 type
= TREE_TYPE (arg1
);
1275 sat_p
= TYPE_SATURATING (type
);
1276 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1277 t
= build_fixed (type
, result
);
1278 /* Propagate overflow flags. */
1279 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1280 TREE_OVERFLOW (t
) = 1;
1284 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1286 tree type
= TREE_TYPE (arg1
);
1287 tree r1
= TREE_REALPART (arg1
);
1288 tree i1
= TREE_IMAGPART (arg1
);
1289 tree r2
= TREE_REALPART (arg2
);
1290 tree i2
= TREE_IMAGPART (arg2
);
1297 real
= const_binop (code
, r1
, r2
);
1298 imag
= const_binop (code
, i1
, i2
);
1302 if (COMPLEX_FLOAT_TYPE_P (type
))
1303 return do_mpc_arg2 (arg1
, arg2
, type
,
1304 /* do_nonfinite= */ folding_initializer
,
1307 real
= const_binop (MINUS_EXPR
,
1308 const_binop (MULT_EXPR
, r1
, r2
),
1309 const_binop (MULT_EXPR
, i1
, i2
));
1310 imag
= const_binop (PLUS_EXPR
,
1311 const_binop (MULT_EXPR
, r1
, i2
),
1312 const_binop (MULT_EXPR
, i1
, r2
));
1316 if (COMPLEX_FLOAT_TYPE_P (type
))
1317 return do_mpc_arg2 (arg1
, arg2
, type
,
1318 /* do_nonfinite= */ folding_initializer
,
1321 case TRUNC_DIV_EXPR
:
1323 case FLOOR_DIV_EXPR
:
1324 case ROUND_DIV_EXPR
:
1325 if (flag_complex_method
== 0)
1327 /* Keep this algorithm in sync with
1328 tree-complex.c:expand_complex_div_straight().
1330 Expand complex division to scalars, straightforward algorithm.
1331 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1335 = const_binop (PLUS_EXPR
,
1336 const_binop (MULT_EXPR
, r2
, r2
),
1337 const_binop (MULT_EXPR
, i2
, i2
));
1339 = const_binop (PLUS_EXPR
,
1340 const_binop (MULT_EXPR
, r1
, r2
),
1341 const_binop (MULT_EXPR
, i1
, i2
));
1343 = const_binop (MINUS_EXPR
,
1344 const_binop (MULT_EXPR
, i1
, r2
),
1345 const_binop (MULT_EXPR
, r1
, i2
));
1347 real
= const_binop (code
, t1
, magsquared
);
1348 imag
= const_binop (code
, t2
, magsquared
);
1352 /* Keep this algorithm in sync with
1353 tree-complex.c:expand_complex_div_wide().
1355 Expand complex division to scalars, modified algorithm to minimize
1356 overflow with wide input ranges. */
1357 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1358 fold_abs_const (r2
, TREE_TYPE (type
)),
1359 fold_abs_const (i2
, TREE_TYPE (type
)));
1361 if (integer_nonzerop (compare
))
1363 /* In the TRUE branch, we compute
1365 div = (br * ratio) + bi;
1366 tr = (ar * ratio) + ai;
1367 ti = (ai * ratio) - ar;
1370 tree ratio
= const_binop (code
, r2
, i2
);
1371 tree div
= const_binop (PLUS_EXPR
, i2
,
1372 const_binop (MULT_EXPR
, r2
, ratio
));
1373 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1374 real
= const_binop (PLUS_EXPR
, real
, i1
);
1375 real
= const_binop (code
, real
, div
);
1377 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1378 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1379 imag
= const_binop (code
, imag
, div
);
1383 /* In the FALSE branch, we compute
1385 divisor = (d * ratio) + c;
1386 tr = (b * ratio) + a;
1387 ti = b - (a * ratio);
1390 tree ratio
= const_binop (code
, i2
, r2
);
1391 tree div
= const_binop (PLUS_EXPR
, r2
,
1392 const_binop (MULT_EXPR
, i2
, ratio
));
1394 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1395 real
= const_binop (PLUS_EXPR
, real
, r1
);
1396 real
= const_binop (code
, real
, div
);
1398 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1399 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1400 imag
= const_binop (code
, imag
, div
);
1410 return build_complex (type
, real
, imag
);
1413 if (TREE_CODE (arg1
) == VECTOR_CST
1414 && TREE_CODE (arg2
) == VECTOR_CST
)
1416 tree type
= TREE_TYPE (arg1
);
1417 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1418 tree
*elts
= XALLOCAVEC (tree
, count
);
1420 for (i
= 0; i
< count
; i
++)
1422 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1423 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1425 elts
[i
] = const_binop (code
, elem1
, elem2
);
1427 /* It is possible that const_binop cannot handle the given
1428 code and return NULL_TREE */
1429 if (elts
[i
] == NULL_TREE
)
1433 return build_vector (type
, elts
);
1436 /* Shifts allow a scalar offset for a vector. */
1437 if (TREE_CODE (arg1
) == VECTOR_CST
1438 && TREE_CODE (arg2
) == INTEGER_CST
)
1440 tree type
= TREE_TYPE (arg1
);
1441 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1442 tree
*elts
= XALLOCAVEC (tree
, count
);
1444 for (i
= 0; i
< count
; i
++)
1446 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1448 elts
[i
] = const_binop (code
, elem1
, arg2
);
1450 /* It is possible that const_binop cannot handle the given
1451 code and return NULL_TREE. */
1452 if (elts
[i
] == NULL_TREE
)
1456 return build_vector (type
, elts
);
1461 /* Overload that adds a TYPE parameter to be able to dispatch
1462 to fold_relational_const. */
1465 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1467 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1468 return fold_relational_const (code
, type
, arg1
, arg2
);
1470 /* ??? Until we make the const_binop worker take the type of the
1471 result as argument put those cases that need it here. */
1475 if ((TREE_CODE (arg1
) == REAL_CST
1476 && TREE_CODE (arg2
) == REAL_CST
)
1477 || (TREE_CODE (arg1
) == INTEGER_CST
1478 && TREE_CODE (arg2
) == INTEGER_CST
))
1479 return build_complex (type
, arg1
, arg2
);
1482 case VEC_PACK_TRUNC_EXPR
:
1483 case VEC_PACK_FIX_TRUNC_EXPR
:
1485 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1488 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1489 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1490 if (TREE_CODE (arg1
) != VECTOR_CST
1491 || TREE_CODE (arg2
) != VECTOR_CST
)
1494 elts
= XALLOCAVEC (tree
, nelts
);
1495 if (!vec_cst_ctor_to_array (arg1
, elts
)
1496 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1499 for (i
= 0; i
< nelts
; i
++)
1501 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1502 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1503 TREE_TYPE (type
), elts
[i
]);
1504 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1508 return build_vector (type
, elts
);
1511 case VEC_WIDEN_MULT_LO_EXPR
:
1512 case VEC_WIDEN_MULT_HI_EXPR
:
1513 case VEC_WIDEN_MULT_EVEN_EXPR
:
1514 case VEC_WIDEN_MULT_ODD_EXPR
:
1516 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1517 unsigned int out
, ofs
, scale
;
1520 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1521 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1522 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1525 elts
= XALLOCAVEC (tree
, nelts
* 4);
1526 if (!vec_cst_ctor_to_array (arg1
, elts
)
1527 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1530 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1531 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1532 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1533 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1534 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1536 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1539 for (out
= 0; out
< nelts
; out
++)
1541 unsigned int in1
= (out
<< scale
) + ofs
;
1542 unsigned int in2
= in1
+ nelts
* 2;
1545 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1546 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1548 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1550 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1551 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1555 return build_vector (type
, elts
);
1561 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1564 /* Make sure type and arg0 have the same saturating flag. */
1565 gcc_checking_assert (TYPE_SATURATING (type
)
1566 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1568 return const_binop (code
, arg1
, arg2
);
1571 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1572 Return zero if computing the constants is not possible. */
1575 const_unop (enum tree_code code
, tree type
, tree arg0
)
1577 /* Don't perform the operation, other than NEGATE and ABS, if
1578 flag_signaling_nans is on and the operand is a signaling NaN. */
1579 if (TREE_CODE (arg0
) == REAL_CST
1580 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1581 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1582 && code
!= NEGATE_EXPR
1583 && code
!= ABS_EXPR
)
1590 case FIX_TRUNC_EXPR
:
1591 case FIXED_CONVERT_EXPR
:
1592 return fold_convert_const (code
, type
, arg0
);
1594 case ADDR_SPACE_CONVERT_EXPR
:
1595 /* If the source address is 0, and the source address space
1596 cannot have a valid object at 0, fold to dest type null. */
1597 if (integer_zerop (arg0
)
1598 && !(targetm
.addr_space
.zero_address_valid
1599 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1600 return fold_convert_const (code
, type
, arg0
);
1603 case VIEW_CONVERT_EXPR
:
1604 return fold_view_convert_expr (type
, arg0
);
1608 /* Can't call fold_negate_const directly here as that doesn't
1609 handle all cases and we might not be able to negate some
1611 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1612 if (tem
&& CONSTANT_CLASS_P (tem
))
1618 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1619 return fold_abs_const (arg0
, type
);
1623 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1625 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1627 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1632 if (TREE_CODE (arg0
) == INTEGER_CST
)
1633 return fold_not_const (arg0
, type
);
1634 /* Perform BIT_NOT_EXPR on each element individually. */
1635 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1639 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1641 elements
= XALLOCAVEC (tree
, count
);
1642 for (i
= 0; i
< count
; i
++)
1644 elem
= VECTOR_CST_ELT (arg0
, i
);
1645 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1646 if (elem
== NULL_TREE
)
1651 return build_vector (type
, elements
);
1655 case TRUTH_NOT_EXPR
:
1656 if (TREE_CODE (arg0
) == INTEGER_CST
)
1657 return constant_boolean_node (integer_zerop (arg0
), type
);
1661 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1662 return fold_convert (type
, TREE_REALPART (arg0
));
1666 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1667 return fold_convert (type
, TREE_IMAGPART (arg0
));
1670 case VEC_UNPACK_LO_EXPR
:
1671 case VEC_UNPACK_HI_EXPR
:
1672 case VEC_UNPACK_FLOAT_LO_EXPR
:
1673 case VEC_UNPACK_FLOAT_HI_EXPR
:
1675 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1677 enum tree_code subcode
;
1679 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1680 if (TREE_CODE (arg0
) != VECTOR_CST
)
1683 elts
= XALLOCAVEC (tree
, nelts
* 2);
1684 if (!vec_cst_ctor_to_array (arg0
, elts
))
1687 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1688 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1691 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1694 subcode
= FLOAT_EXPR
;
1696 for (i
= 0; i
< nelts
; i
++)
1698 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1699 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1703 return build_vector (type
, elts
);
1706 case REDUC_MIN_EXPR
:
1707 case REDUC_MAX_EXPR
:
1708 case REDUC_PLUS_EXPR
:
1710 unsigned int nelts
, i
;
1712 enum tree_code subcode
;
1714 if (TREE_CODE (arg0
) != VECTOR_CST
)
1716 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1718 elts
= XALLOCAVEC (tree
, nelts
);
1719 if (!vec_cst_ctor_to_array (arg0
, elts
))
1724 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1725 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1726 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1727 default: gcc_unreachable ();
1730 for (i
= 1; i
< nelts
; i
++)
1732 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1733 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1747 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1748 indicates which particular sizetype to create. */
1751 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1753 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1756 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1757 is a tree code. The type of the result is taken from the operands.
1758 Both must be equivalent integer types, ala int_binop_types_match_p.
1759 If the operands are constant, so is the result. */
1762 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1764 tree type
= TREE_TYPE (arg0
);
1766 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1767 return error_mark_node
;
1769 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1772 /* Handle the special case of two integer constants faster. */
1773 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1775 /* And some specific cases even faster than that. */
1776 if (code
== PLUS_EXPR
)
1778 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1780 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1783 else if (code
== MINUS_EXPR
)
1785 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1788 else if (code
== MULT_EXPR
)
1790 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1794 /* Handle general case of two integer constants. For sizetype
1795 constant calculations we always want to know about overflow,
1796 even in the unsigned case. */
1797 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1800 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1803 /* Given two values, either both of sizetype or both of bitsizetype,
1804 compute the difference between the two values. Return the value
1805 in signed type corresponding to the type of the operands. */
1808 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1810 tree type
= TREE_TYPE (arg0
);
1813 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1816 /* If the type is already signed, just do the simple thing. */
1817 if (!TYPE_UNSIGNED (type
))
1818 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1820 if (type
== sizetype
)
1822 else if (type
== bitsizetype
)
1823 ctype
= sbitsizetype
;
1825 ctype
= signed_type_for (type
);
1827 /* If either operand is not a constant, do the conversions to the signed
1828 type and subtract. The hardware will do the right thing with any
1829 overflow in the subtraction. */
1830 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1831 return size_binop_loc (loc
, MINUS_EXPR
,
1832 fold_convert_loc (loc
, ctype
, arg0
),
1833 fold_convert_loc (loc
, ctype
, arg1
));
1835 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1836 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1837 overflow) and negate (which can't either). Special-case a result
1838 of zero while we're here. */
1839 if (tree_int_cst_equal (arg0
, arg1
))
1840 return build_int_cst (ctype
, 0);
1841 else if (tree_int_cst_lt (arg1
, arg0
))
1842 return fold_convert_loc (loc
, ctype
,
1843 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1845 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1846 fold_convert_loc (loc
, ctype
,
1847 size_binop_loc (loc
,
1852 /* A subroutine of fold_convert_const handling conversions of an
1853 INTEGER_CST to another integer type. */
1856 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1858 /* Given an integer constant, make new constant with new type,
1859 appropriately sign-extended or truncated. Use widest_int
1860 so that any extension is done according ARG1's type. */
1861 return force_fit_type (type
, wi::to_widest (arg1
),
1862 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1863 TREE_OVERFLOW (arg1
));
1866 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1867 to an integer type. */
1870 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1872 bool overflow
= false;
1875 /* The following code implements the floating point to integer
1876 conversion rules required by the Java Language Specification,
1877 that IEEE NaNs are mapped to zero and values that overflow
1878 the target precision saturate, i.e. values greater than
1879 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1880 are mapped to INT_MIN. These semantics are allowed by the
1881 C and C++ standards that simply state that the behavior of
1882 FP-to-integer conversion is unspecified upon overflow. */
1886 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1890 case FIX_TRUNC_EXPR
:
1891 real_trunc (&r
, VOIDmode
, &x
);
1898 /* If R is NaN, return zero and show we have an overflow. */
1899 if (REAL_VALUE_ISNAN (r
))
1902 val
= wi::zero (TYPE_PRECISION (type
));
1905 /* See if R is less than the lower bound or greater than the
1910 tree lt
= TYPE_MIN_VALUE (type
);
1911 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1912 if (real_less (&r
, &l
))
1921 tree ut
= TYPE_MAX_VALUE (type
);
1924 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1925 if (real_less (&u
, &r
))
1934 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1936 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1940 /* A subroutine of fold_convert_const handling conversions of a
1941 FIXED_CST to an integer type. */
1944 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1947 double_int temp
, temp_trunc
;
1950 /* Right shift FIXED_CST to temp by fbit. */
1951 temp
= TREE_FIXED_CST (arg1
).data
;
1952 mode
= TREE_FIXED_CST (arg1
).mode
;
1953 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1955 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1956 HOST_BITS_PER_DOUBLE_INT
,
1957 SIGNED_FIXED_POINT_MODE_P (mode
));
1959 /* Left shift temp to temp_trunc by fbit. */
1960 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1961 HOST_BITS_PER_DOUBLE_INT
,
1962 SIGNED_FIXED_POINT_MODE_P (mode
));
1966 temp
= double_int_zero
;
1967 temp_trunc
= double_int_zero
;
1970 /* If FIXED_CST is negative, we need to round the value toward 0.
1971 By checking if the fractional bits are not zero to add 1 to temp. */
1972 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1973 && temp_trunc
.is_negative ()
1974 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1975 temp
+= double_int_one
;
1977 /* Given a fixed-point constant, make new constant with new type,
1978 appropriately sign-extended or truncated. */
1979 t
= force_fit_type (type
, temp
, -1,
1980 (temp
.is_negative ()
1981 && (TYPE_UNSIGNED (type
)
1982 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1983 | TREE_OVERFLOW (arg1
));
1988 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1989 to another floating point type. */
1992 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1994 REAL_VALUE_TYPE value
;
1997 /* Don't perform the operation if flag_signaling_nans is on
1998 and the operand is a signaling NaN. */
1999 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
2000 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
2003 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
2004 t
= build_real (type
, value
);
2006 /* If converting an infinity or NAN to a representation that doesn't
2007 have one, set the overflow bit so that we can produce some kind of
2008 error message at the appropriate point if necessary. It's not the
2009 most user-friendly message, but it's better than nothing. */
2010 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
2011 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2012 TREE_OVERFLOW (t
) = 1;
2013 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2014 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2015 TREE_OVERFLOW (t
) = 1;
2016 /* Regular overflow, conversion produced an infinity in a mode that
2017 can't represent them. */
2018 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2019 && REAL_VALUE_ISINF (value
)
2020 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2021 TREE_OVERFLOW (t
) = 1;
2023 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2027 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2028 to a floating point type. */
2031 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2033 REAL_VALUE_TYPE value
;
2036 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
2037 t
= build_real (type
, value
);
2039 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2043 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2044 to another fixed-point type. */
2047 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2049 FIXED_VALUE_TYPE value
;
2053 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2054 TYPE_SATURATING (type
));
2055 t
= build_fixed (type
, value
);
2057 /* Propagate overflow flags. */
2058 if (overflow_p
| TREE_OVERFLOW (arg1
))
2059 TREE_OVERFLOW (t
) = 1;
2063 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2064 to a fixed-point type. */
2067 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2069 FIXED_VALUE_TYPE value
;
2074 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2076 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2077 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2078 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2080 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2082 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2083 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2084 TYPE_SATURATING (type
));
2085 t
= build_fixed (type
, value
);
2087 /* Propagate overflow flags. */
2088 if (overflow_p
| TREE_OVERFLOW (arg1
))
2089 TREE_OVERFLOW (t
) = 1;
2093 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2094 to a fixed-point type. */
2097 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2099 FIXED_VALUE_TYPE value
;
2103 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2104 &TREE_REAL_CST (arg1
),
2105 TYPE_SATURATING (type
));
2106 t
= build_fixed (type
, value
);
2108 /* Propagate overflow flags. */
2109 if (overflow_p
| TREE_OVERFLOW (arg1
))
2110 TREE_OVERFLOW (t
) = 1;
2114 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2115 type TYPE. If no simplification can be done return NULL_TREE. */
2118 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2120 if (TREE_TYPE (arg1
) == type
)
2123 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2124 || TREE_CODE (type
) == OFFSET_TYPE
)
2126 if (TREE_CODE (arg1
) == INTEGER_CST
)
2127 return fold_convert_const_int_from_int (type
, arg1
);
2128 else if (TREE_CODE (arg1
) == REAL_CST
)
2129 return fold_convert_const_int_from_real (code
, type
, arg1
);
2130 else if (TREE_CODE (arg1
) == FIXED_CST
)
2131 return fold_convert_const_int_from_fixed (type
, arg1
);
2133 else if (TREE_CODE (type
) == REAL_TYPE
)
2135 if (TREE_CODE (arg1
) == INTEGER_CST
)
2136 return build_real_from_int_cst (type
, arg1
);
2137 else if (TREE_CODE (arg1
) == REAL_CST
)
2138 return fold_convert_const_real_from_real (type
, arg1
);
2139 else if (TREE_CODE (arg1
) == FIXED_CST
)
2140 return fold_convert_const_real_from_fixed (type
, arg1
);
2142 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2144 if (TREE_CODE (arg1
) == FIXED_CST
)
2145 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2146 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2147 return fold_convert_const_fixed_from_int (type
, arg1
);
2148 else if (TREE_CODE (arg1
) == REAL_CST
)
2149 return fold_convert_const_fixed_from_real (type
, arg1
);
2151 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2153 if (TREE_CODE (arg1
) == VECTOR_CST
2154 && TYPE_VECTOR_SUBPARTS (type
) == VECTOR_CST_NELTS (arg1
))
2156 int len
= TYPE_VECTOR_SUBPARTS (type
);
2157 tree elttype
= TREE_TYPE (type
);
2158 tree
*v
= XALLOCAVEC (tree
, len
);
2159 for (int i
= 0; i
< len
; ++i
)
2161 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2162 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2163 if (cvt
== NULL_TREE
)
2167 return build_vector (type
, v
);
2173 /* Construct a vector of zero elements of vector type TYPE. */
2176 build_zero_vector (tree type
)
2180 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2181 return build_vector_from_val (type
, t
);
2184 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2187 fold_convertible_p (const_tree type
, const_tree arg
)
2189 tree orig
= TREE_TYPE (arg
);
2194 if (TREE_CODE (arg
) == ERROR_MARK
2195 || TREE_CODE (type
) == ERROR_MARK
2196 || TREE_CODE (orig
) == ERROR_MARK
)
2199 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2202 switch (TREE_CODE (type
))
2204 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2205 case POINTER_TYPE
: case REFERENCE_TYPE
:
2207 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2208 || TREE_CODE (orig
) == OFFSET_TYPE
);
2211 case FIXED_POINT_TYPE
:
2214 return TREE_CODE (type
) == TREE_CODE (orig
);
2221 /* Convert expression ARG to type TYPE. Used by the middle-end for
2222 simple conversions in preference to calling the front-end's convert. */
2225 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2227 tree orig
= TREE_TYPE (arg
);
2233 if (TREE_CODE (arg
) == ERROR_MARK
2234 || TREE_CODE (type
) == ERROR_MARK
2235 || TREE_CODE (orig
) == ERROR_MARK
)
2236 return error_mark_node
;
2238 switch (TREE_CODE (type
))
2241 case REFERENCE_TYPE
:
2242 /* Handle conversions between pointers to different address spaces. */
2243 if (POINTER_TYPE_P (orig
)
2244 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2245 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2246 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2249 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2251 if (TREE_CODE (arg
) == INTEGER_CST
)
2253 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2254 if (tem
!= NULL_TREE
)
2257 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2258 || TREE_CODE (orig
) == OFFSET_TYPE
)
2259 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2260 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2261 return fold_convert_loc (loc
, type
,
2262 fold_build1_loc (loc
, REALPART_EXPR
,
2263 TREE_TYPE (orig
), arg
));
2264 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2265 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2266 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2269 if (TREE_CODE (arg
) == INTEGER_CST
)
2271 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2272 if (tem
!= NULL_TREE
)
2275 else if (TREE_CODE (arg
) == REAL_CST
)
2277 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2278 if (tem
!= NULL_TREE
)
2281 else if (TREE_CODE (arg
) == FIXED_CST
)
2283 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2284 if (tem
!= NULL_TREE
)
2288 switch (TREE_CODE (orig
))
2291 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2292 case POINTER_TYPE
: case REFERENCE_TYPE
:
2293 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2296 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2298 case FIXED_POINT_TYPE
:
2299 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2302 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2303 return fold_convert_loc (loc
, type
, tem
);
2309 case FIXED_POINT_TYPE
:
2310 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2311 || TREE_CODE (arg
) == REAL_CST
)
2313 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2314 if (tem
!= NULL_TREE
)
2315 goto fold_convert_exit
;
2318 switch (TREE_CODE (orig
))
2320 case FIXED_POINT_TYPE
:
2325 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2328 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2329 return fold_convert_loc (loc
, type
, tem
);
2336 switch (TREE_CODE (orig
))
2339 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2340 case POINTER_TYPE
: case REFERENCE_TYPE
:
2342 case FIXED_POINT_TYPE
:
2343 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2344 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2345 fold_convert_loc (loc
, TREE_TYPE (type
),
2346 integer_zero_node
));
2351 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2353 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2354 TREE_OPERAND (arg
, 0));
2355 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2356 TREE_OPERAND (arg
, 1));
2357 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2360 arg
= save_expr (arg
);
2361 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2362 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2363 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2364 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2365 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2373 if (integer_zerop (arg
))
2374 return build_zero_vector (type
);
2375 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2376 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2377 || TREE_CODE (orig
) == VECTOR_TYPE
);
2378 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2381 tem
= fold_ignored_result (arg
);
2382 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2385 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2386 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2390 protected_set_expr_location_unshare (tem
, loc
);
2394 /* Return false if expr can be assumed not to be an lvalue, true
2398 maybe_lvalue_p (const_tree x
)
2400 /* We only need to wrap lvalue tree codes. */
2401 switch (TREE_CODE (x
))
2414 case ARRAY_RANGE_REF
:
2420 case PREINCREMENT_EXPR
:
2421 case PREDECREMENT_EXPR
:
2423 case TRY_CATCH_EXPR
:
2424 case WITH_CLEANUP_EXPR
:
2433 /* Assume the worst for front-end tree codes. */
2434 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2442 /* Return an expr equal to X but certainly not valid as an lvalue. */
2445 non_lvalue_loc (location_t loc
, tree x
)
2447 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2452 if (! maybe_lvalue_p (x
))
2454 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2457 /* When pedantic, return an expr equal to X but certainly not valid as a
2458 pedantic lvalue. Otherwise, return X. */
2461 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2463 return protected_set_expr_location_unshare (x
, loc
);
2466 /* Given a tree comparison code, return the code that is the logical inverse.
2467 It is generally not safe to do this for floating-point comparisons, except
2468 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2469 ERROR_MARK in this case. */
2472 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2474 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2475 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2485 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2487 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2489 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2491 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2505 return UNORDERED_EXPR
;
2506 case UNORDERED_EXPR
:
2507 return ORDERED_EXPR
;
2513 /* Similar, but return the comparison that results if the operands are
2514 swapped. This is safe for floating-point. */
2517 swap_tree_comparison (enum tree_code code
)
2524 case UNORDERED_EXPR
:
2550 /* Convert a comparison tree code from an enum tree_code representation
2551 into a compcode bit-based encoding. This function is the inverse of
2552 compcode_to_comparison. */
2554 static enum comparison_code
2555 comparison_to_compcode (enum tree_code code
)
2572 return COMPCODE_ORD
;
2573 case UNORDERED_EXPR
:
2574 return COMPCODE_UNORD
;
2576 return COMPCODE_UNLT
;
2578 return COMPCODE_UNEQ
;
2580 return COMPCODE_UNLE
;
2582 return COMPCODE_UNGT
;
2584 return COMPCODE_LTGT
;
2586 return COMPCODE_UNGE
;
2592 /* Convert a compcode bit-based encoding of a comparison operator back
2593 to GCC's enum tree_code representation. This function is the
2594 inverse of comparison_to_compcode. */
2596 static enum tree_code
2597 compcode_to_comparison (enum comparison_code code
)
2614 return ORDERED_EXPR
;
2615 case COMPCODE_UNORD
:
2616 return UNORDERED_EXPR
;
2634 /* Return a tree for the comparison which is the combination of
2635 doing the AND or OR (depending on CODE) of the two operations LCODE
2636 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2637 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2638 if this makes the transformation invalid. */
2641 combine_comparisons (location_t loc
,
2642 enum tree_code code
, enum tree_code lcode
,
2643 enum tree_code rcode
, tree truth_type
,
2644 tree ll_arg
, tree lr_arg
)
2646 bool honor_nans
= HONOR_NANS (ll_arg
);
2647 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2648 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2653 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2654 compcode
= lcompcode
& rcompcode
;
2657 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2658 compcode
= lcompcode
| rcompcode
;
2667 /* Eliminate unordered comparisons, as well as LTGT and ORD
2668 which are not used unless the mode has NaNs. */
2669 compcode
&= ~COMPCODE_UNORD
;
2670 if (compcode
== COMPCODE_LTGT
)
2671 compcode
= COMPCODE_NE
;
2672 else if (compcode
== COMPCODE_ORD
)
2673 compcode
= COMPCODE_TRUE
;
2675 else if (flag_trapping_math
)
2677 /* Check that the original operation and the optimized ones will trap
2678 under the same condition. */
2679 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2680 && (lcompcode
!= COMPCODE_EQ
)
2681 && (lcompcode
!= COMPCODE_ORD
);
2682 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2683 && (rcompcode
!= COMPCODE_EQ
)
2684 && (rcompcode
!= COMPCODE_ORD
);
2685 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2686 && (compcode
!= COMPCODE_EQ
)
2687 && (compcode
!= COMPCODE_ORD
);
2689 /* In a short-circuited boolean expression the LHS might be
2690 such that the RHS, if evaluated, will never trap. For
2691 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2692 if neither x nor y is NaN. (This is a mixed blessing: for
2693 example, the expression above will never trap, hence
2694 optimizing it to x < y would be invalid). */
2695 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2696 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2699 /* If the comparison was short-circuited, and only the RHS
2700 trapped, we may now generate a spurious trap. */
2702 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2705 /* If we changed the conditions that cause a trap, we lose. */
2706 if ((ltrap
|| rtrap
) != trap
)
2710 if (compcode
== COMPCODE_TRUE
)
2711 return constant_boolean_node (true, truth_type
);
2712 else if (compcode
== COMPCODE_FALSE
)
2713 return constant_boolean_node (false, truth_type
);
2716 enum tree_code tcode
;
2718 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2719 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2723 /* Return nonzero if two operands (typically of the same tree node)
2724 are necessarily equal. FLAGS modifies behavior as follows:
2726 If OEP_ONLY_CONST is set, only return nonzero for constants.
2727 This function tests whether the operands are indistinguishable;
2728 it does not test whether they are equal using C's == operation.
2729 The distinction is important for IEEE floating point, because
2730 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2731 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2733 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2734 even though it may hold multiple values during a function.
2735 This is because a GCC tree node guarantees that nothing else is
2736 executed between the evaluation of its "operands" (which may often
2737 be evaluated in arbitrary order). Hence if the operands themselves
2738 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2739 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2740 unset means assuming isochronic (or instantaneous) tree equivalence.
2741 Unless comparing arbitrary expression trees, such as from different
2742 statements, this flag can usually be left unset.
2744 If OEP_PURE_SAME is set, then pure functions with identical arguments
2745 are considered the same. It is used when the caller has other ways
2746 to ensure that global memory is unchanged in between.
2748 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2749 not values of expressions.
2751 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2752 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2754 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2755 any operand with side effect. This is unnecesarily conservative in the
2756 case we know that arg0 and arg1 are in disjoint code paths (such as in
2757 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2758 addresses with TREE_CONSTANT flag set so we know that &var == &var
2759 even if var is volatile. */
2762 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2764 /* When checking, verify at the outermost operand_equal_p call that
2765 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2767 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
2769 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
2773 inchash::hash
hstate0 (0), hstate1 (0);
2774 inchash::add_expr (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
2775 inchash::add_expr (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
2776 hashval_t h0
= hstate0
.end ();
2777 hashval_t h1
= hstate1
.end ();
2778 gcc_assert (h0
== h1
);
2786 /* If either is ERROR_MARK, they aren't equal. */
2787 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2788 || TREE_TYPE (arg0
) == error_mark_node
2789 || TREE_TYPE (arg1
) == error_mark_node
)
2792 /* Similar, if either does not have a type (like a released SSA name),
2793 they aren't equal. */
2794 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2797 /* We cannot consider pointers to different address space equal. */
2798 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2799 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2800 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2801 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2804 /* Check equality of integer constants before bailing out due to
2805 precision differences. */
2806 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2808 /* Address of INTEGER_CST is not defined; check that we did not forget
2809 to drop the OEP_ADDRESS_OF flags. */
2810 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2811 return tree_int_cst_equal (arg0
, arg1
);
2814 if (!(flags
& OEP_ADDRESS_OF
))
2816 /* If both types don't have the same signedness, then we can't consider
2817 them equal. We must check this before the STRIP_NOPS calls
2818 because they may change the signedness of the arguments. As pointers
2819 strictly don't have a signedness, require either two pointers or
2820 two non-pointers as well. */
2821 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2822 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2823 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2826 /* If both types don't have the same precision, then it is not safe
2828 if (element_precision (TREE_TYPE (arg0
))
2829 != element_precision (TREE_TYPE (arg1
)))
2836 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2837 sanity check once the issue is solved. */
2839 /* Addresses of conversions and SSA_NAMEs (and many other things)
2840 are not defined. Check that we did not forget to drop the
2841 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2842 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2843 && TREE_CODE (arg0
) != SSA_NAME
);
2846 /* In case both args are comparisons but with different comparison
2847 code, try to swap the comparison operands of one arg to produce
2848 a match and compare that variant. */
2849 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2850 && COMPARISON_CLASS_P (arg0
)
2851 && COMPARISON_CLASS_P (arg1
))
2853 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2855 if (TREE_CODE (arg0
) == swap_code
)
2856 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2857 TREE_OPERAND (arg1
, 1), flags
)
2858 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2859 TREE_OPERAND (arg1
, 0), flags
);
2862 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2864 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2865 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2867 else if (flags
& OEP_ADDRESS_OF
)
2869 /* If we are interested in comparing addresses ignore
2870 MEM_REF wrappings of the base that can appear just for
2872 if (TREE_CODE (arg0
) == MEM_REF
2874 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2875 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2876 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2878 else if (TREE_CODE (arg1
) == MEM_REF
2880 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2881 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2882 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2890 /* When not checking adddresses, this is needed for conversions and for
2891 COMPONENT_REF. Might as well play it safe and always test this. */
2892 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2893 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2894 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2895 && !(flags
& OEP_ADDRESS_OF
)))
2898 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2899 We don't care about side effects in that case because the SAVE_EXPR
2900 takes care of that for us. In all other cases, two expressions are
2901 equal if they have no side effects. If we have two identical
2902 expressions with side effects that should be treated the same due
2903 to the only side effects being identical SAVE_EXPR's, that will
2904 be detected in the recursive calls below.
2905 If we are taking an invariant address of two identical objects
2906 they are necessarily equal as well. */
2907 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2908 && (TREE_CODE (arg0
) == SAVE_EXPR
2909 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
2910 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2913 /* Next handle constant cases, those for which we can return 1 even
2914 if ONLY_CONST is set. */
2915 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2916 switch (TREE_CODE (arg0
))
2919 return tree_int_cst_equal (arg0
, arg1
);
2922 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2923 TREE_FIXED_CST (arg1
));
2926 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2930 if (!HONOR_SIGNED_ZEROS (arg0
))
2932 /* If we do not distinguish between signed and unsigned zero,
2933 consider them equal. */
2934 if (real_zerop (arg0
) && real_zerop (arg1
))
2943 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2946 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2948 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2949 VECTOR_CST_ELT (arg1
, i
), flags
))
2956 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2958 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2962 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2963 && ! memcmp (TREE_STRING_POINTER (arg0
),
2964 TREE_STRING_POINTER (arg1
),
2965 TREE_STRING_LENGTH (arg0
)));
2968 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2969 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2970 flags
| OEP_ADDRESS_OF
2971 | OEP_MATCH_SIDE_EFFECTS
);
2973 /* In GIMPLE empty constructors are allowed in initializers of
2975 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
2980 if (flags
& OEP_ONLY_CONST
)
2983 /* Define macros to test an operand from arg0 and arg1 for equality and a
2984 variant that allows null and views null as being different from any
2985 non-null value. In the latter case, if either is null, the both
2986 must be; otherwise, do the normal comparison. */
2987 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2988 TREE_OPERAND (arg1, N), flags)
2990 #define OP_SAME_WITH_NULL(N) \
2991 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2992 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2994 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2997 /* Two conversions are equal only if signedness and modes match. */
2998 switch (TREE_CODE (arg0
))
3001 case FIX_TRUNC_EXPR
:
3002 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
3003 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3013 case tcc_comparison
:
3015 if (OP_SAME (0) && OP_SAME (1))
3018 /* For commutative ops, allow the other order. */
3019 return (commutative_tree_code (TREE_CODE (arg0
))
3020 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3021 TREE_OPERAND (arg1
, 1), flags
)
3022 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3023 TREE_OPERAND (arg1
, 0), flags
));
3026 /* If either of the pointer (or reference) expressions we are
3027 dereferencing contain a side effect, these cannot be equal,
3028 but their addresses can be. */
3029 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3030 && (TREE_SIDE_EFFECTS (arg0
)
3031 || TREE_SIDE_EFFECTS (arg1
)))
3034 switch (TREE_CODE (arg0
))
3037 if (!(flags
& OEP_ADDRESS_OF
)
3038 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3039 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3041 flags
&= ~OEP_ADDRESS_OF
;
3045 /* Require the same offset. */
3046 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3047 TYPE_SIZE (TREE_TYPE (arg1
)),
3048 flags
& ~OEP_ADDRESS_OF
))
3053 case VIEW_CONVERT_EXPR
:
3056 case TARGET_MEM_REF
:
3058 if (!(flags
& OEP_ADDRESS_OF
))
3060 /* Require equal access sizes */
3061 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3062 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3063 || !TYPE_SIZE (TREE_TYPE (arg1
))
3064 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3065 TYPE_SIZE (TREE_TYPE (arg1
)),
3068 /* Verify that access happens in similar types. */
3069 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3071 /* Verify that accesses are TBAA compatible. */
3072 if (!alias_ptr_types_compatible_p
3073 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3074 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3075 || (MR_DEPENDENCE_CLIQUE (arg0
)
3076 != MR_DEPENDENCE_CLIQUE (arg1
))
3077 || (MR_DEPENDENCE_BASE (arg0
)
3078 != MR_DEPENDENCE_BASE (arg1
)))
3080 /* Verify that alignment is compatible. */
3081 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3082 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3085 flags
&= ~OEP_ADDRESS_OF
;
3086 return (OP_SAME (0) && OP_SAME (1)
3087 /* TARGET_MEM_REF require equal extra operands. */
3088 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3089 || (OP_SAME_WITH_NULL (2)
3090 && OP_SAME_WITH_NULL (3)
3091 && OP_SAME_WITH_NULL (4))));
3094 case ARRAY_RANGE_REF
:
3097 flags
&= ~OEP_ADDRESS_OF
;
3098 /* Compare the array index by value if it is constant first as we
3099 may have different types but same value here. */
3100 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3101 TREE_OPERAND (arg1
, 1))
3103 && OP_SAME_WITH_NULL (2)
3104 && OP_SAME_WITH_NULL (3)
3105 /* Compare low bound and element size as with OEP_ADDRESS_OF
3106 we have to account for the offset of the ref. */
3107 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3108 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3109 || (operand_equal_p (array_ref_low_bound
3110 (CONST_CAST_TREE (arg0
)),
3112 (CONST_CAST_TREE (arg1
)), flags
)
3113 && operand_equal_p (array_ref_element_size
3114 (CONST_CAST_TREE (arg0
)),
3115 array_ref_element_size
3116 (CONST_CAST_TREE (arg1
)),
3120 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3121 may be NULL when we're called to compare MEM_EXPRs. */
3122 if (!OP_SAME_WITH_NULL (0)
3125 flags
&= ~OEP_ADDRESS_OF
;
3126 return OP_SAME_WITH_NULL (2);
3131 flags
&= ~OEP_ADDRESS_OF
;
3132 return OP_SAME (1) && OP_SAME (2);
3138 case tcc_expression
:
3139 switch (TREE_CODE (arg0
))
3142 /* Be sure we pass right ADDRESS_OF flag. */
3143 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3144 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3145 TREE_OPERAND (arg1
, 0),
3146 flags
| OEP_ADDRESS_OF
);
3148 case TRUTH_NOT_EXPR
:
3151 case TRUTH_ANDIF_EXPR
:
3152 case TRUTH_ORIF_EXPR
:
3153 return OP_SAME (0) && OP_SAME (1);
3156 case WIDEN_MULT_PLUS_EXPR
:
3157 case WIDEN_MULT_MINUS_EXPR
:
3160 /* The multiplcation operands are commutative. */
3163 case TRUTH_AND_EXPR
:
3165 case TRUTH_XOR_EXPR
:
3166 if (OP_SAME (0) && OP_SAME (1))
3169 /* Otherwise take into account this is a commutative operation. */
3170 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3171 TREE_OPERAND (arg1
, 1), flags
)
3172 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3173 TREE_OPERAND (arg1
, 0), flags
));
3176 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3178 flags
&= ~OEP_ADDRESS_OF
;
3183 case BIT_INSERT_EXPR
:
3184 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3189 case PREDECREMENT_EXPR
:
3190 case PREINCREMENT_EXPR
:
3191 case POSTDECREMENT_EXPR
:
3192 case POSTINCREMENT_EXPR
:
3193 if (flags
& OEP_LEXICOGRAPHIC
)
3194 return OP_SAME (0) && OP_SAME (1);
3197 case CLEANUP_POINT_EXPR
:
3199 if (flags
& OEP_LEXICOGRAPHIC
)
3208 switch (TREE_CODE (arg0
))
3211 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3212 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3213 /* If not both CALL_EXPRs are either internal or normal function
3214 functions, then they are not equal. */
3216 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3218 /* If the CALL_EXPRs call different internal functions, then they
3220 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3225 /* If the CALL_EXPRs call different functions, then they are not
3227 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3232 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3234 unsigned int cef
= call_expr_flags (arg0
);
3235 if (flags
& OEP_PURE_SAME
)
3236 cef
&= ECF_CONST
| ECF_PURE
;
3239 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3243 /* Now see if all the arguments are the same. */
3245 const_call_expr_arg_iterator iter0
, iter1
;
3247 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3248 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3250 a0
= next_const_call_expr_arg (&iter0
),
3251 a1
= next_const_call_expr_arg (&iter1
))
3252 if (! operand_equal_p (a0
, a1
, flags
))
3255 /* If we get here and both argument lists are exhausted
3256 then the CALL_EXPRs are equal. */
3257 return ! (a0
|| a1
);
3263 case tcc_declaration
:
3264 /* Consider __builtin_sqrt equal to sqrt. */
3265 return (TREE_CODE (arg0
) == FUNCTION_DECL
3266 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3267 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3268 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3270 case tcc_exceptional
:
3271 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3273 /* In GIMPLE constructors are used only to build vectors from
3274 elements. Individual elements in the constructor must be
3275 indexed in increasing order and form an initial sequence.
3277 We make no effort to compare constructors in generic.
3278 (see sem_variable::equals in ipa-icf which can do so for
3280 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3281 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3284 /* Be sure that vectors constructed have the same representation.
3285 We only tested element precision and modes to match.
3286 Vectors may be BLKmode and thus also check that the number of
3288 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3289 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3292 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3293 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3294 unsigned int len
= vec_safe_length (v0
);
3296 if (len
!= vec_safe_length (v1
))
3299 for (unsigned int i
= 0; i
< len
; i
++)
3301 constructor_elt
*c0
= &(*v0
)[i
];
3302 constructor_elt
*c1
= &(*v1
)[i
];
3304 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3305 /* In GIMPLE the indexes can be either NULL or matching i.
3306 Double check this so we won't get false
3307 positives for GENERIC. */
3309 && (TREE_CODE (c0
->index
) != INTEGER_CST
3310 || !compare_tree_int (c0
->index
, i
)))
3312 && (TREE_CODE (c1
->index
) != INTEGER_CST
3313 || !compare_tree_int (c1
->index
, i
))))
3318 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3319 && (flags
& OEP_LEXICOGRAPHIC
))
3321 /* Compare the STATEMENT_LISTs. */
3322 tree_stmt_iterator tsi1
, tsi2
;
3323 tree body1
= CONST_CAST_TREE (arg0
);
3324 tree body2
= CONST_CAST_TREE (arg1
);
3325 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3326 tsi_next (&tsi1
), tsi_next (&tsi2
))
3328 /* The lists don't have the same number of statements. */
3329 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3331 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3333 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3341 switch (TREE_CODE (arg0
))
3344 if (flags
& OEP_LEXICOGRAPHIC
)
3345 return OP_SAME_WITH_NULL (0);
3356 #undef OP_SAME_WITH_NULL
3359 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3360 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3362 When in doubt, return 0. */
3365 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3367 int unsignedp1
, unsignedpo
;
3368 tree primarg0
, primarg1
, primother
;
3369 unsigned int correct_width
;
3371 if (operand_equal_p (arg0
, arg1
, 0))
3374 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3375 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3378 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3379 and see if the inner values are the same. This removes any
3380 signedness comparison, which doesn't matter here. */
3381 primarg0
= arg0
, primarg1
= arg1
;
3382 STRIP_NOPS (primarg0
);
3383 STRIP_NOPS (primarg1
);
3384 if (operand_equal_p (primarg0
, primarg1
, 0))
3387 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3388 actual comparison operand, ARG0.
3390 First throw away any conversions to wider types
3391 already present in the operands. */
3393 primarg1
= get_narrower (arg1
, &unsignedp1
);
3394 primother
= get_narrower (other
, &unsignedpo
);
3396 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3397 if (unsignedp1
== unsignedpo
3398 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3399 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3401 tree type
= TREE_TYPE (arg0
);
3403 /* Make sure shorter operand is extended the right way
3404 to match the longer operand. */
3405 primarg1
= fold_convert (signed_or_unsigned_type_for
3406 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3408 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3415 /* See if ARG is an expression that is either a comparison or is performing
3416 arithmetic on comparisons. The comparisons must only be comparing
3417 two different values, which will be stored in *CVAL1 and *CVAL2; if
3418 they are nonzero it means that some operands have already been found.
3419 No variables may be used anywhere else in the expression except in the
3420 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3421 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3423 If this is true, return 1. Otherwise, return zero. */
3426 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3428 enum tree_code code
= TREE_CODE (arg
);
3429 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3431 /* We can handle some of the tcc_expression cases here. */
3432 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3434 else if (tclass
== tcc_expression
3435 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3436 || code
== COMPOUND_EXPR
))
3437 tclass
= tcc_binary
;
3439 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3440 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3442 /* If we've already found a CVAL1 or CVAL2, this expression is
3443 two complex to handle. */
3444 if (*cval1
|| *cval2
)
3454 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3457 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3458 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3459 cval1
, cval2
, save_p
));
3464 case tcc_expression
:
3465 if (code
== COND_EXPR
)
3466 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3467 cval1
, cval2
, save_p
)
3468 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3469 cval1
, cval2
, save_p
)
3470 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3471 cval1
, cval2
, save_p
));
3474 case tcc_comparison
:
3475 /* First see if we can handle the first operand, then the second. For
3476 the second operand, we know *CVAL1 can't be zero. It must be that
3477 one side of the comparison is each of the values; test for the
3478 case where this isn't true by failing if the two operands
3481 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3482 TREE_OPERAND (arg
, 1), 0))
3486 *cval1
= TREE_OPERAND (arg
, 0);
3487 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3489 else if (*cval2
== 0)
3490 *cval2
= TREE_OPERAND (arg
, 0);
3491 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3496 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3498 else if (*cval2
== 0)
3499 *cval2
= TREE_OPERAND (arg
, 1);
3500 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3512 /* ARG is a tree that is known to contain just arithmetic operations and
3513 comparisons. Evaluate the operations in the tree substituting NEW0 for
3514 any occurrence of OLD0 as an operand of a comparison and likewise for
3518 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3519 tree old1
, tree new1
)
3521 tree type
= TREE_TYPE (arg
);
3522 enum tree_code code
= TREE_CODE (arg
);
3523 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3525 /* We can handle some of the tcc_expression cases here. */
3526 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3528 else if (tclass
== tcc_expression
3529 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3530 tclass
= tcc_binary
;
3535 return fold_build1_loc (loc
, code
, type
,
3536 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3537 old0
, new0
, old1
, new1
));
3540 return fold_build2_loc (loc
, code
, type
,
3541 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3542 old0
, new0
, old1
, new1
),
3543 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3544 old0
, new0
, old1
, new1
));
3546 case tcc_expression
:
3550 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3554 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3558 return fold_build3_loc (loc
, code
, type
,
3559 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3560 old0
, new0
, old1
, new1
),
3561 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3562 old0
, new0
, old1
, new1
),
3563 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3564 old0
, new0
, old1
, new1
));
3568 /* Fall through - ??? */
3570 case tcc_comparison
:
3572 tree arg0
= TREE_OPERAND (arg
, 0);
3573 tree arg1
= TREE_OPERAND (arg
, 1);
3575 /* We need to check both for exact equality and tree equality. The
3576 former will be true if the operand has a side-effect. In that
3577 case, we know the operand occurred exactly once. */
3579 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3581 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3584 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3586 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3589 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3597 /* Return a tree for the case when the result of an expression is RESULT
3598 converted to TYPE and OMITTED was previously an operand of the expression
3599 but is now not needed (e.g., we folded OMITTED * 0).
3601 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3602 the conversion of RESULT to TYPE. */
3605 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3607 tree t
= fold_convert_loc (loc
, type
, result
);
3609 /* If the resulting operand is an empty statement, just return the omitted
3610 statement casted to void. */
3611 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3612 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3613 fold_ignored_result (omitted
));
3615 if (TREE_SIDE_EFFECTS (omitted
))
3616 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3617 fold_ignored_result (omitted
), t
);
3619 return non_lvalue_loc (loc
, t
);
3622 /* Return a tree for the case when the result of an expression is RESULT
3623 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3624 of the expression but are now not needed.
3626 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3627 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3628 evaluated before OMITTED2. Otherwise, if neither has side effects,
3629 just do the conversion of RESULT to TYPE. */
3632 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3633 tree omitted1
, tree omitted2
)
3635 tree t
= fold_convert_loc (loc
, type
, result
);
3637 if (TREE_SIDE_EFFECTS (omitted2
))
3638 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3639 if (TREE_SIDE_EFFECTS (omitted1
))
3640 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3642 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3646 /* Return a simplified tree node for the truth-negation of ARG. This
3647 never alters ARG itself. We assume that ARG is an operation that
3648 returns a truth value (0 or 1).
3650 FIXME: one would think we would fold the result, but it causes
3651 problems with the dominator optimizer. */
3654 fold_truth_not_expr (location_t loc
, tree arg
)
3656 tree type
= TREE_TYPE (arg
);
3657 enum tree_code code
= TREE_CODE (arg
);
3658 location_t loc1
, loc2
;
3660 /* If this is a comparison, we can simply invert it, except for
3661 floating-point non-equality comparisons, in which case we just
3662 enclose a TRUTH_NOT_EXPR around what we have. */
3664 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3666 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3667 if (FLOAT_TYPE_P (op_type
)
3668 && flag_trapping_math
3669 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3670 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3673 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3674 if (code
== ERROR_MARK
)
3677 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3678 TREE_OPERAND (arg
, 1));
3679 if (TREE_NO_WARNING (arg
))
3680 TREE_NO_WARNING (ret
) = 1;
3687 return constant_boolean_node (integer_zerop (arg
), type
);
3689 case TRUTH_AND_EXPR
:
3690 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3691 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3692 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3693 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3694 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3697 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3698 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3699 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3700 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3701 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3703 case TRUTH_XOR_EXPR
:
3704 /* Here we can invert either operand. We invert the first operand
3705 unless the second operand is a TRUTH_NOT_EXPR in which case our
3706 result is the XOR of the first operand with the inside of the
3707 negation of the second operand. */
3709 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3710 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3711 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3713 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3714 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3715 TREE_OPERAND (arg
, 1));
3717 case TRUTH_ANDIF_EXPR
:
3718 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3719 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3720 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3721 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3722 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3724 case TRUTH_ORIF_EXPR
:
3725 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3726 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3727 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3728 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3729 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3731 case TRUTH_NOT_EXPR
:
3732 return TREE_OPERAND (arg
, 0);
3736 tree arg1
= TREE_OPERAND (arg
, 1);
3737 tree arg2
= TREE_OPERAND (arg
, 2);
3739 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3740 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3742 /* A COND_EXPR may have a throw as one operand, which
3743 then has void type. Just leave void operands
3745 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3746 VOID_TYPE_P (TREE_TYPE (arg1
))
3747 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3748 VOID_TYPE_P (TREE_TYPE (arg2
))
3749 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3753 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3754 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3755 TREE_OPERAND (arg
, 0),
3756 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3758 case NON_LVALUE_EXPR
:
3759 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3760 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3763 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3764 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3769 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3770 return build1_loc (loc
, TREE_CODE (arg
), type
,
3771 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3774 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3776 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3779 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3781 case CLEANUP_POINT_EXPR
:
3782 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3783 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3784 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3791 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3792 assume that ARG is an operation that returns a truth value (0 or 1
3793 for scalars, 0 or -1 for vectors). Return the folded expression if
3794 folding is successful. Otherwise, return NULL_TREE. */
3797 fold_invert_truthvalue (location_t loc
, tree arg
)
3799 tree type
= TREE_TYPE (arg
);
3800 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3806 /* Return a simplified tree node for the truth-negation of ARG. This
3807 never alters ARG itself. We assume that ARG is an operation that
3808 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3811 invert_truthvalue_loc (location_t loc
, tree arg
)
3813 if (TREE_CODE (arg
) == ERROR_MARK
)
3816 tree type
= TREE_TYPE (arg
);
3817 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3823 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3824 with code CODE. This optimization is unsafe. */
3826 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3827 tree arg0
, tree arg1
)
3829 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3830 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3832 /* (A / C) +- (B / C) -> (A +- B) / C. */
3834 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3835 TREE_OPERAND (arg1
, 1), 0))
3836 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3837 fold_build2_loc (loc
, code
, type
,
3838 TREE_OPERAND (arg0
, 0),
3839 TREE_OPERAND (arg1
, 0)),
3840 TREE_OPERAND (arg0
, 1));
3842 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3843 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3844 TREE_OPERAND (arg1
, 0), 0)
3845 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3846 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3848 REAL_VALUE_TYPE r0
, r1
;
3849 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3850 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3852 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3854 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3855 real_arithmetic (&r0
, code
, &r0
, &r1
);
3856 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3857 TREE_OPERAND (arg0
, 0),
3858 build_real (type
, r0
));
3864 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3865 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3866 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3867 is the original memory reference used to preserve the alias set of
3871 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
3872 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
3873 int unsignedp
, int reversep
)
3875 tree result
, bftype
;
3877 /* Attempt not to lose the access path if possible. */
3878 if (TREE_CODE (orig_inner
) == COMPONENT_REF
)
3880 tree ninner
= TREE_OPERAND (orig_inner
, 0);
3882 HOST_WIDE_INT nbitsize
, nbitpos
;
3884 int nunsignedp
, nreversep
, nvolatilep
= 0;
3885 tree base
= get_inner_reference (ninner
, &nbitsize
, &nbitpos
,
3886 &noffset
, &nmode
, &nunsignedp
,
3887 &nreversep
, &nvolatilep
);
3889 && noffset
== NULL_TREE
3890 && nbitsize
>= bitsize
3891 && nbitpos
<= bitpos
3892 && bitpos
+ bitsize
<= nbitpos
+ nbitsize
3902 alias_set_type iset
= get_alias_set (orig_inner
);
3903 if (iset
== 0 && get_alias_set (inner
) != iset
)
3904 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
3905 build_fold_addr_expr (inner
),
3906 build_int_cst (ptr_type_node
, 0));
3908 if (bitpos
== 0 && !reversep
)
3910 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3911 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3912 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3913 && tree_fits_shwi_p (size
)
3914 && tree_to_shwi (size
) == bitsize
)
3915 return fold_convert_loc (loc
, type
, inner
);
3919 if (TYPE_PRECISION (bftype
) != bitsize
3920 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3921 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3923 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3924 size_int (bitsize
), bitsize_int (bitpos
));
3925 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
3928 result
= fold_convert_loc (loc
, type
, result
);
3933 /* Optimize a bit-field compare.
3935 There are two cases: First is a compare against a constant and the
3936 second is a comparison of two items where the fields are at the same
3937 bit position relative to the start of a chunk (byte, halfword, word)
3938 large enough to contain it. In these cases we can avoid the shift
3939 implicit in bitfield extractions.
3941 For constants, we emit a compare of the shifted constant with the
3942 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3943 compared. For two fields at the same position, we do the ANDs with the
3944 similar mask and compare the result of the ANDs.
3946 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3947 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3948 are the left and right operands of the comparison, respectively.
3950 If the optimization described above can be done, we return the resulting
3951 tree. Otherwise we return zero. */
3954 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3955 tree compare_type
, tree lhs
, tree rhs
)
3957 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3958 tree type
= TREE_TYPE (lhs
);
3960 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3961 machine_mode lmode
, rmode
, nmode
;
3962 int lunsignedp
, runsignedp
;
3963 int lreversep
, rreversep
;
3964 int lvolatilep
= 0, rvolatilep
= 0;
3965 tree linner
, rinner
= NULL_TREE
;
3969 /* Get all the information about the extractions being done. If the bit size
3970 if the same as the size of the underlying object, we aren't doing an
3971 extraction at all and so can do nothing. We also don't want to
3972 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3973 then will no longer be able to replace it. */
3974 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3975 &lunsignedp
, &lreversep
, &lvolatilep
);
3976 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3977 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3981 rreversep
= lreversep
;
3984 /* If this is not a constant, we can only do something if bit positions,
3985 sizes, signedness and storage order are the same. */
3987 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3988 &runsignedp
, &rreversep
, &rvolatilep
);
3990 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3991 || lunsignedp
!= runsignedp
|| lreversep
!= rreversep
|| offset
!= 0
3992 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3996 /* Honor the C++ memory model and mimic what RTL expansion does. */
3997 unsigned HOST_WIDE_INT bitstart
= 0;
3998 unsigned HOST_WIDE_INT bitend
= 0;
3999 if (TREE_CODE (lhs
) == COMPONENT_REF
)
4001 get_bit_range (&bitstart
, &bitend
, lhs
, &lbitpos
, &offset
);
4002 if (offset
!= NULL_TREE
)
4006 /* See if we can find a mode to refer to this field. We should be able to,
4007 but fail if we can't. */
4008 nmode
= get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
4009 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
4010 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
4011 TYPE_ALIGN (TREE_TYPE (rinner
))),
4013 if (nmode
== VOIDmode
)
4016 /* Set signed and unsigned types of the precision of this mode for the
4018 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
4020 /* Compute the bit position and size for the new reference and our offset
4021 within it. If the new reference is the same size as the original, we
4022 won't optimize anything, so return zero. */
4023 nbitsize
= GET_MODE_BITSIZE (nmode
);
4024 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
4026 if (nbitsize
== lbitsize
)
4029 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
4030 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
4032 /* Make the mask to be used against the extracted field. */
4033 mask
= build_int_cst_type (unsigned_type
, -1);
4034 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
4035 mask
= const_binop (RSHIFT_EXPR
, mask
,
4036 size_int (nbitsize
- lbitsize
- lbitpos
));
4039 /* If not comparing with constant, just rework the comparison
4041 return fold_build2_loc (loc
, code
, compare_type
,
4042 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4043 make_bit_field_ref (loc
, linner
, lhs
,
4048 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4049 make_bit_field_ref (loc
, rinner
, rhs
,
4055 /* Otherwise, we are handling the constant case. See if the constant is too
4056 big for the field. Warn and return a tree for 0 (false) if so. We do
4057 this not only for its own sake, but to avoid having to test for this
4058 error case below. If we didn't, we might generate wrong code.
4060 For unsigned fields, the constant shifted right by the field length should
4061 be all zero. For signed fields, the high-order bits should agree with
4066 if (wi::lrshift (rhs
, lbitsize
) != 0)
4068 warning (0, "comparison is always %d due to width of bit-field",
4070 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4075 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
4076 if (tem
!= 0 && tem
!= -1)
4078 warning (0, "comparison is always %d due to width of bit-field",
4080 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4084 /* Single-bit compares should always be against zero. */
4085 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4087 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4088 rhs
= build_int_cst (type
, 0);
4091 /* Make a new bitfield reference, shift the constant over the
4092 appropriate number of bits and mask it with the computed mask
4093 (in case this was a signed field). If we changed it, make a new one. */
4094 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4095 nbitsize
, nbitpos
, 1, lreversep
);
4097 rhs
= const_binop (BIT_AND_EXPR
,
4098 const_binop (LSHIFT_EXPR
,
4099 fold_convert_loc (loc
, unsigned_type
, rhs
),
4100 size_int (lbitpos
)),
4103 lhs
= build2_loc (loc
, code
, compare_type
,
4104 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4108 /* Subroutine for fold_truth_andor_1: decode a field reference.
4110 If EXP is a comparison reference, we return the innermost reference.
4112 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4113 set to the starting bit number.
4115 If the innermost field can be completely contained in a mode-sized
4116 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4118 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4119 otherwise it is not changed.
4121 *PUNSIGNEDP is set to the signedness of the field.
4123 *PREVERSEP is set to the storage order of the field.
4125 *PMASK is set to the mask used. This is either contained in a
4126 BIT_AND_EXPR or derived from the width of the field.
4128 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4130 Return 0 if this is not a component reference or is one that we can't
4131 do anything with. */
4134 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4135 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4136 int *punsignedp
, int *preversep
, int *pvolatilep
,
4137 tree
*pmask
, tree
*pand_mask
)
4140 tree outer_type
= 0;
4142 tree mask
, inner
, offset
;
4144 unsigned int precision
;
4146 /* All the optimizations using this function assume integer fields.
4147 There are problems with FP fields since the type_for_size call
4148 below can fail for, e.g., XFmode. */
4149 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4152 /* We are interested in the bare arrangement of bits, so strip everything
4153 that doesn't affect the machine mode. However, record the type of the
4154 outermost expression if it may matter below. */
4155 if (CONVERT_EXPR_P (exp
)
4156 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4157 outer_type
= TREE_TYPE (exp
);
4160 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4162 and_mask
= TREE_OPERAND (exp
, 1);
4163 exp
= TREE_OPERAND (exp
, 0);
4164 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4165 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4169 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
4170 punsignedp
, preversep
, pvolatilep
);
4171 if ((inner
== exp
&& and_mask
== 0)
4172 || *pbitsize
< 0 || offset
!= 0
4173 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
4174 /* Reject out-of-bound accesses (PR79731). */
4175 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner
))
4176 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner
)),
4177 *pbitpos
+ *pbitsize
) < 0))
4182 /* If the number of bits in the reference is the same as the bitsize of
4183 the outer type, then the outer type gives the signedness. Otherwise
4184 (in case of a small bitfield) the signedness is unchanged. */
4185 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4186 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4188 /* Compute the mask to access the bitfield. */
4189 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4190 precision
= TYPE_PRECISION (unsigned_type
);
4192 mask
= build_int_cst_type (unsigned_type
, -1);
4194 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4195 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4197 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4199 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4200 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4203 *pand_mask
= and_mask
;
4207 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4208 bit positions and MASK is SIGNED. */
4211 all_ones_mask_p (const_tree mask
, unsigned int size
)
4213 tree type
= TREE_TYPE (mask
);
4214 unsigned int precision
= TYPE_PRECISION (type
);
4216 /* If this function returns true when the type of the mask is
4217 UNSIGNED, then there will be errors. In particular see
4218 gcc.c-torture/execute/990326-1.c. There does not appear to be
4219 any documentation paper trail as to why this is so. But the pre
4220 wide-int worked with that restriction and it has been preserved
4222 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4225 return wi::mask (size
, false, precision
) == mask
;
4228 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4229 represents the sign bit of EXP's type. If EXP represents a sign
4230 or zero extension, also test VAL against the unextended type.
4231 The return value is the (sub)expression whose sign bit is VAL,
4232 or NULL_TREE otherwise. */
4235 sign_bit_p (tree exp
, const_tree val
)
4240 /* Tree EXP must have an integral type. */
4241 t
= TREE_TYPE (exp
);
4242 if (! INTEGRAL_TYPE_P (t
))
4245 /* Tree VAL must be an integer constant. */
4246 if (TREE_CODE (val
) != INTEGER_CST
4247 || TREE_OVERFLOW (val
))
4250 width
= TYPE_PRECISION (t
);
4251 if (wi::only_sign_bit_p (val
, width
))
4254 /* Handle extension from a narrower type. */
4255 if (TREE_CODE (exp
) == NOP_EXPR
4256 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4257 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4262 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4263 to be evaluated unconditionally. */
4266 simple_operand_p (const_tree exp
)
4268 /* Strip any conversions that don't change the machine mode. */
4271 return (CONSTANT_CLASS_P (exp
)
4272 || TREE_CODE (exp
) == SSA_NAME
4274 && ! TREE_ADDRESSABLE (exp
)
4275 && ! TREE_THIS_VOLATILE (exp
)
4276 && ! DECL_NONLOCAL (exp
)
4277 /* Don't regard global variables as simple. They may be
4278 allocated in ways unknown to the compiler (shared memory,
4279 #pragma weak, etc). */
4280 && ! TREE_PUBLIC (exp
)
4281 && ! DECL_EXTERNAL (exp
)
4282 /* Weakrefs are not safe to be read, since they can be NULL.
4283 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4284 have DECL_WEAK flag set. */
4285 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4286 /* Loading a static variable is unduly expensive, but global
4287 registers aren't expensive. */
4288 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4291 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4292 to be evaluated unconditionally.
4293 I addition to simple_operand_p, we assume that comparisons, conversions,
4294 and logic-not operations are simple, if their operands are simple, too. */
4297 simple_operand_p_2 (tree exp
)
4299 enum tree_code code
;
4301 if (TREE_SIDE_EFFECTS (exp
)
4302 || tree_could_trap_p (exp
))
4305 while (CONVERT_EXPR_P (exp
))
4306 exp
= TREE_OPERAND (exp
, 0);
4308 code
= TREE_CODE (exp
);
4310 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4311 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4312 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4314 if (code
== TRUTH_NOT_EXPR
)
4315 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4317 return simple_operand_p (exp
);
4321 /* The following functions are subroutines to fold_range_test and allow it to
4322 try to change a logical combination of comparisons into a range test.
4325 X == 2 || X == 3 || X == 4 || X == 5
4329 (unsigned) (X - 2) <= 3
4331 We describe each set of comparisons as being either inside or outside
4332 a range, using a variable named like IN_P, and then describe the
4333 range with a lower and upper bound. If one of the bounds is omitted,
4334 it represents either the highest or lowest value of the type.
4336 In the comments below, we represent a range by two numbers in brackets
4337 preceded by a "+" to designate being inside that range, or a "-" to
4338 designate being outside that range, so the condition can be inverted by
4339 flipping the prefix. An omitted bound is represented by a "-". For
4340 example, "- [-, 10]" means being outside the range starting at the lowest
4341 possible value and ending at 10, in other words, being greater than 10.
4342 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4345 We set up things so that the missing bounds are handled in a consistent
4346 manner so neither a missing bound nor "true" and "false" need to be
4347 handled using a special case. */
4349 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4350 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4351 and UPPER1_P are nonzero if the respective argument is an upper bound
4352 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4353 must be specified for a comparison. ARG1 will be converted to ARG0's
4354 type if both are specified. */
4357 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4358 tree arg1
, int upper1_p
)
4364 /* If neither arg represents infinity, do the normal operation.
4365 Else, if not a comparison, return infinity. Else handle the special
4366 comparison rules. Note that most of the cases below won't occur, but
4367 are handled for consistency. */
4369 if (arg0
!= 0 && arg1
!= 0)
4371 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4372 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4374 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4377 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4380 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4381 for neither. In real maths, we cannot assume open ended ranges are
4382 the same. But, this is computer arithmetic, where numbers are finite.
4383 We can therefore make the transformation of any unbounded range with
4384 the value Z, Z being greater than any representable number. This permits
4385 us to treat unbounded ranges as equal. */
4386 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4387 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4391 result
= sgn0
== sgn1
;
4394 result
= sgn0
!= sgn1
;
4397 result
= sgn0
< sgn1
;
4400 result
= sgn0
<= sgn1
;
4403 result
= sgn0
> sgn1
;
4406 result
= sgn0
>= sgn1
;
4412 return constant_boolean_node (result
, type
);
4415 /* Helper routine for make_range. Perform one step for it, return
4416 new expression if the loop should continue or NULL_TREE if it should
4420 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4421 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4422 bool *strict_overflow_p
)
4424 tree arg0_type
= TREE_TYPE (arg0
);
4425 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4426 int in_p
= *p_in_p
, n_in_p
;
4430 case TRUTH_NOT_EXPR
:
4431 /* We can only do something if the range is testing for zero. */
4432 if (low
== NULL_TREE
|| high
== NULL_TREE
4433 || ! integer_zerop (low
) || ! integer_zerop (high
))
4438 case EQ_EXPR
: case NE_EXPR
:
4439 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4440 /* We can only do something if the range is testing for zero
4441 and if the second operand is an integer constant. Note that
4442 saying something is "in" the range we make is done by
4443 complementing IN_P since it will set in the initial case of
4444 being not equal to zero; "out" is leaving it alone. */
4445 if (low
== NULL_TREE
|| high
== NULL_TREE
4446 || ! integer_zerop (low
) || ! integer_zerop (high
)
4447 || TREE_CODE (arg1
) != INTEGER_CST
)
4452 case NE_EXPR
: /* - [c, c] */
4455 case EQ_EXPR
: /* + [c, c] */
4456 in_p
= ! in_p
, low
= high
= arg1
;
4458 case GT_EXPR
: /* - [-, c] */
4459 low
= 0, high
= arg1
;
4461 case GE_EXPR
: /* + [c, -] */
4462 in_p
= ! in_p
, low
= arg1
, high
= 0;
4464 case LT_EXPR
: /* - [c, -] */
4465 low
= arg1
, high
= 0;
4467 case LE_EXPR
: /* + [-, c] */
4468 in_p
= ! in_p
, low
= 0, high
= arg1
;
4474 /* If this is an unsigned comparison, we also know that EXP is
4475 greater than or equal to zero. We base the range tests we make
4476 on that fact, so we record it here so we can parse existing
4477 range tests. We test arg0_type since often the return type
4478 of, e.g. EQ_EXPR, is boolean. */
4479 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4481 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4483 build_int_cst (arg0_type
, 0),
4487 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4489 /* If the high bound is missing, but we have a nonzero low
4490 bound, reverse the range so it goes from zero to the low bound
4492 if (high
== 0 && low
&& ! integer_zerop (low
))
4495 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4496 build_int_cst (TREE_TYPE (low
), 1), 0);
4497 low
= build_int_cst (arg0_type
, 0);
4507 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4508 low and high are non-NULL, then normalize will DTRT. */
4509 if (!TYPE_UNSIGNED (arg0_type
)
4510 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4512 if (low
== NULL_TREE
)
4513 low
= TYPE_MIN_VALUE (arg0_type
);
4514 if (high
== NULL_TREE
)
4515 high
= TYPE_MAX_VALUE (arg0_type
);
4518 /* (-x) IN [a,b] -> x in [-b, -a] */
4519 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4520 build_int_cst (exp_type
, 0),
4522 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4523 build_int_cst (exp_type
, 0),
4525 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4531 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4532 build_int_cst (exp_type
, 1));
4536 if (TREE_CODE (arg1
) != INTEGER_CST
)
4539 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4540 move a constant to the other side. */
4541 if (!TYPE_UNSIGNED (arg0_type
)
4542 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4545 /* If EXP is signed, any overflow in the computation is undefined,
4546 so we don't worry about it so long as our computations on
4547 the bounds don't overflow. For unsigned, overflow is defined
4548 and this is exactly the right thing. */
4549 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4550 arg0_type
, low
, 0, arg1
, 0);
4551 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4552 arg0_type
, high
, 1, arg1
, 0);
4553 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4554 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4557 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4558 *strict_overflow_p
= true;
4561 /* Check for an unsigned range which has wrapped around the maximum
4562 value thus making n_high < n_low, and normalize it. */
4563 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4565 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4566 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4567 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4568 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4570 /* If the range is of the form +/- [ x+1, x ], we won't
4571 be able to normalize it. But then, it represents the
4572 whole range or the empty set, so make it
4574 if (tree_int_cst_equal (n_low
, low
)
4575 && tree_int_cst_equal (n_high
, high
))
4581 low
= n_low
, high
= n_high
;
4589 case NON_LVALUE_EXPR
:
4590 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4593 if (! INTEGRAL_TYPE_P (arg0_type
)
4594 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4595 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4598 n_low
= low
, n_high
= high
;
4601 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4604 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4606 /* If we're converting arg0 from an unsigned type, to exp,
4607 a signed type, we will be doing the comparison as unsigned.
4608 The tests above have already verified that LOW and HIGH
4611 So we have to ensure that we will handle large unsigned
4612 values the same way that the current signed bounds treat
4615 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4619 /* For fixed-point modes, we need to pass the saturating flag
4620 as the 2nd parameter. */
4621 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4623 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4624 TYPE_SATURATING (arg0_type
));
4627 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4629 /* A range without an upper bound is, naturally, unbounded.
4630 Since convert would have cropped a very large value, use
4631 the max value for the destination type. */
4633 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4634 : TYPE_MAX_VALUE (arg0_type
);
4636 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4637 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4638 fold_convert_loc (loc
, arg0_type
,
4640 build_int_cst (arg0_type
, 1));
4642 /* If the low bound is specified, "and" the range with the
4643 range for which the original unsigned value will be
4647 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4648 1, fold_convert_loc (loc
, arg0_type
,
4653 in_p
= (n_in_p
== in_p
);
4657 /* Otherwise, "or" the range with the range of the input
4658 that will be interpreted as negative. */
4659 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4660 1, fold_convert_loc (loc
, arg0_type
,
4665 in_p
= (in_p
!= n_in_p
);
4679 /* Given EXP, a logical expression, set the range it is testing into
4680 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4681 actually being tested. *PLOW and *PHIGH will be made of the same
4682 type as the returned expression. If EXP is not a comparison, we
4683 will most likely not be returning a useful value and range. Set
4684 *STRICT_OVERFLOW_P to true if the return value is only valid
4685 because signed overflow is undefined; otherwise, do not change
4686 *STRICT_OVERFLOW_P. */
4689 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4690 bool *strict_overflow_p
)
4692 enum tree_code code
;
4693 tree arg0
, arg1
= NULL_TREE
;
4694 tree exp_type
, nexp
;
4697 location_t loc
= EXPR_LOCATION (exp
);
4699 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4700 and see if we can refine the range. Some of the cases below may not
4701 happen, but it doesn't seem worth worrying about this. We "continue"
4702 the outer loop when we've changed something; otherwise we "break"
4703 the switch, which will "break" the while. */
4706 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4710 code
= TREE_CODE (exp
);
4711 exp_type
= TREE_TYPE (exp
);
4714 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4716 if (TREE_OPERAND_LENGTH (exp
) > 0)
4717 arg0
= TREE_OPERAND (exp
, 0);
4718 if (TREE_CODE_CLASS (code
) == tcc_binary
4719 || TREE_CODE_CLASS (code
) == tcc_comparison
4720 || (TREE_CODE_CLASS (code
) == tcc_expression
4721 && TREE_OPERAND_LENGTH (exp
) > 1))
4722 arg1
= TREE_OPERAND (exp
, 1);
4724 if (arg0
== NULL_TREE
)
4727 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4728 &high
, &in_p
, strict_overflow_p
);
4729 if (nexp
== NULL_TREE
)
4734 /* If EXP is a constant, we can evaluate whether this is true or false. */
4735 if (TREE_CODE (exp
) == INTEGER_CST
)
4737 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4739 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4745 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4749 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4750 type, TYPE, return an expression to test if EXP is in (or out of, depending
4751 on IN_P) the range. Return 0 if the test couldn't be created. */
4754 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4755 tree low
, tree high
)
4757 tree etype
= TREE_TYPE (exp
), value
;
4759 /* Disable this optimization for function pointer expressions
4760 on targets that require function pointer canonicalization. */
4761 if (targetm
.have_canonicalize_funcptr_for_compare ()
4762 && TREE_CODE (etype
) == POINTER_TYPE
4763 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4768 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4770 return invert_truthvalue_loc (loc
, value
);
4775 if (low
== 0 && high
== 0)
4776 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4779 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4780 fold_convert_loc (loc
, etype
, high
));
4783 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4784 fold_convert_loc (loc
, etype
, low
));
4786 if (operand_equal_p (low
, high
, 0))
4787 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4788 fold_convert_loc (loc
, etype
, low
));
4790 if (integer_zerop (low
))
4792 if (! TYPE_UNSIGNED (etype
))
4794 etype
= unsigned_type_for (etype
);
4795 high
= fold_convert_loc (loc
, etype
, high
);
4796 exp
= fold_convert_loc (loc
, etype
, exp
);
4798 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4801 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4802 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4804 int prec
= TYPE_PRECISION (etype
);
4806 if (wi::mask (prec
- 1, false, prec
) == high
)
4808 if (TYPE_UNSIGNED (etype
))
4810 tree signed_etype
= signed_type_for (etype
);
4811 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4813 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4815 etype
= signed_etype
;
4816 exp
= fold_convert_loc (loc
, etype
, exp
);
4818 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4819 build_int_cst (etype
, 0));
4823 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4824 This requires wrap-around arithmetics for the type of the expression.
4825 First make sure that arithmetics in this type is valid, then make sure
4826 that it wraps around. */
4827 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4828 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4829 TYPE_UNSIGNED (etype
));
4831 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4833 tree utype
, minv
, maxv
;
4835 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4836 for the type in question, as we rely on this here. */
4837 utype
= unsigned_type_for (etype
);
4838 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4839 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4840 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4841 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4843 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4850 high
= fold_convert_loc (loc
, etype
, high
);
4851 low
= fold_convert_loc (loc
, etype
, low
);
4852 exp
= fold_convert_loc (loc
, etype
, exp
);
4854 value
= const_binop (MINUS_EXPR
, high
, low
);
4857 if (POINTER_TYPE_P (etype
))
4859 if (value
!= 0 && !TREE_OVERFLOW (value
))
4861 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4862 return build_range_check (loc
, type
,
4863 fold_build_pointer_plus_loc (loc
, exp
, low
),
4864 1, build_int_cst (etype
, 0), value
);
4869 if (value
!= 0 && !TREE_OVERFLOW (value
))
4870 return build_range_check (loc
, type
,
4871 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4872 1, build_int_cst (etype
, 0), value
);
4877 /* Return the predecessor of VAL in its type, handling the infinite case. */
4880 range_predecessor (tree val
)
4882 tree type
= TREE_TYPE (val
);
4884 if (INTEGRAL_TYPE_P (type
)
4885 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4888 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4889 build_int_cst (TREE_TYPE (val
), 1), 0);
4892 /* Return the successor of VAL in its type, handling the infinite case. */
4895 range_successor (tree val
)
4897 tree type
= TREE_TYPE (val
);
4899 if (INTEGRAL_TYPE_P (type
)
4900 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4903 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4904 build_int_cst (TREE_TYPE (val
), 1), 0);
4907 /* Given two ranges, see if we can merge them into one. Return 1 if we
4908 can, 0 if we can't. Set the output range into the specified parameters. */
4911 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4912 tree high0
, int in1_p
, tree low1
, tree high1
)
4920 int lowequal
= ((low0
== 0 && low1
== 0)
4921 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4922 low0
, 0, low1
, 0)));
4923 int highequal
= ((high0
== 0 && high1
== 0)
4924 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4925 high0
, 1, high1
, 1)));
4927 /* Make range 0 be the range that starts first, or ends last if they
4928 start at the same value. Swap them if it isn't. */
4929 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4932 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4933 high1
, 1, high0
, 1))))
4935 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4936 tem
= low0
, low0
= low1
, low1
= tem
;
4937 tem
= high0
, high0
= high1
, high1
= tem
;
4940 /* Now flag two cases, whether the ranges are disjoint or whether the
4941 second range is totally subsumed in the first. Note that the tests
4942 below are simplified by the ones above. */
4943 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4944 high0
, 1, low1
, 0));
4945 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4946 high1
, 1, high0
, 1));
4948 /* We now have four cases, depending on whether we are including or
4949 excluding the two ranges. */
4952 /* If they don't overlap, the result is false. If the second range
4953 is a subset it is the result. Otherwise, the range is from the start
4954 of the second to the end of the first. */
4956 in_p
= 0, low
= high
= 0;
4958 in_p
= 1, low
= low1
, high
= high1
;
4960 in_p
= 1, low
= low1
, high
= high0
;
4963 else if (in0_p
&& ! in1_p
)
4965 /* If they don't overlap, the result is the first range. If they are
4966 equal, the result is false. If the second range is a subset of the
4967 first, and the ranges begin at the same place, we go from just after
4968 the end of the second range to the end of the first. If the second
4969 range is not a subset of the first, or if it is a subset and both
4970 ranges end at the same place, the range starts at the start of the
4971 first range and ends just before the second range.
4972 Otherwise, we can't describe this as a single range. */
4974 in_p
= 1, low
= low0
, high
= high0
;
4975 else if (lowequal
&& highequal
)
4976 in_p
= 0, low
= high
= 0;
4977 else if (subset
&& lowequal
)
4979 low
= range_successor (high1
);
4984 /* We are in the weird situation where high0 > high1 but
4985 high1 has no successor. Punt. */
4989 else if (! subset
|| highequal
)
4992 high
= range_predecessor (low1
);
4996 /* low0 < low1 but low1 has no predecessor. Punt. */
5004 else if (! in0_p
&& in1_p
)
5006 /* If they don't overlap, the result is the second range. If the second
5007 is a subset of the first, the result is false. Otherwise,
5008 the range starts just after the first range and ends at the
5009 end of the second. */
5011 in_p
= 1, low
= low1
, high
= high1
;
5012 else if (subset
|| highequal
)
5013 in_p
= 0, low
= high
= 0;
5016 low
= range_successor (high0
);
5021 /* high1 > high0 but high0 has no successor. Punt. */
5029 /* The case where we are excluding both ranges. Here the complex case
5030 is if they don't overlap. In that case, the only time we have a
5031 range is if they are adjacent. If the second is a subset of the
5032 first, the result is the first. Otherwise, the range to exclude
5033 starts at the beginning of the first range and ends at the end of the
5037 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
5038 range_successor (high0
),
5040 in_p
= 0, low
= low0
, high
= high1
;
5043 /* Canonicalize - [min, x] into - [-, x]. */
5044 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5045 switch (TREE_CODE (TREE_TYPE (low0
)))
5048 if (TYPE_PRECISION (TREE_TYPE (low0
))
5049 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
5053 if (tree_int_cst_equal (low0
,
5054 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5058 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5059 && integer_zerop (low0
))
5066 /* Canonicalize - [x, max] into - [x, -]. */
5067 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5068 switch (TREE_CODE (TREE_TYPE (high1
)))
5071 if (TYPE_PRECISION (TREE_TYPE (high1
))
5072 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
5076 if (tree_int_cst_equal (high1
,
5077 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5081 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5082 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5084 build_int_cst (TREE_TYPE (high1
), 1),
5092 /* The ranges might be also adjacent between the maximum and
5093 minimum values of the given type. For
5094 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5095 return + [x + 1, y - 1]. */
5096 if (low0
== 0 && high1
== 0)
5098 low
= range_successor (high0
);
5099 high
= range_predecessor (low1
);
5100 if (low
== 0 || high
== 0)
5110 in_p
= 0, low
= low0
, high
= high0
;
5112 in_p
= 0, low
= low0
, high
= high1
;
5115 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5120 /* Subroutine of fold, looking inside expressions of the form
5121 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5122 of the COND_EXPR. This function is being used also to optimize
5123 A op B ? C : A, by reversing the comparison first.
5125 Return a folded expression whose code is not a COND_EXPR
5126 anymore, or NULL_TREE if no folding opportunity is found. */
5129 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5130 tree arg0
, tree arg1
, tree arg2
)
5132 enum tree_code comp_code
= TREE_CODE (arg0
);
5133 tree arg00
= TREE_OPERAND (arg0
, 0);
5134 tree arg01
= TREE_OPERAND (arg0
, 1);
5135 tree arg1_type
= TREE_TYPE (arg1
);
5141 /* If we have A op 0 ? A : -A, consider applying the following
5144 A == 0? A : -A same as -A
5145 A != 0? A : -A same as A
5146 A >= 0? A : -A same as abs (A)
5147 A > 0? A : -A same as abs (A)
5148 A <= 0? A : -A same as -abs (A)
5149 A < 0? A : -A same as -abs (A)
5151 None of these transformations work for modes with signed
5152 zeros. If A is +/-0, the first two transformations will
5153 change the sign of the result (from +0 to -0, or vice
5154 versa). The last four will fix the sign of the result,
5155 even though the original expressions could be positive or
5156 negative, depending on the sign of A.
5158 Note that all these transformations are correct if A is
5159 NaN, since the two alternatives (A and -A) are also NaNs. */
5160 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5161 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5162 ? real_zerop (arg01
)
5163 : integer_zerop (arg01
))
5164 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5165 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5166 /* In the case that A is of the form X-Y, '-A' (arg2) may
5167 have already been folded to Y-X, check for that. */
5168 || (TREE_CODE (arg1
) == MINUS_EXPR
5169 && TREE_CODE (arg2
) == MINUS_EXPR
5170 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5171 TREE_OPERAND (arg2
, 1), 0)
5172 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5173 TREE_OPERAND (arg2
, 0), 0))))
5178 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5179 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5182 return fold_convert_loc (loc
, type
, arg1
);
5185 if (flag_trapping_math
)
5190 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5192 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5193 return fold_convert_loc (loc
, type
, tem
);
5196 if (flag_trapping_math
)
5201 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5203 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5204 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5206 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5210 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5211 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5212 both transformations are correct when A is NaN: A != 0
5213 is then true, and A == 0 is false. */
5215 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5216 && integer_zerop (arg01
) && integer_zerop (arg2
))
5218 if (comp_code
== NE_EXPR
)
5219 return fold_convert_loc (loc
, type
, arg1
);
5220 else if (comp_code
== EQ_EXPR
)
5221 return build_zero_cst (type
);
5224 /* Try some transformations of A op B ? A : B.
5226 A == B? A : B same as B
5227 A != B? A : B same as A
5228 A >= B? A : B same as max (A, B)
5229 A > B? A : B same as max (B, A)
5230 A <= B? A : B same as min (A, B)
5231 A < B? A : B same as min (B, A)
5233 As above, these transformations don't work in the presence
5234 of signed zeros. For example, if A and B are zeros of
5235 opposite sign, the first two transformations will change
5236 the sign of the result. In the last four, the original
5237 expressions give different results for (A=+0, B=-0) and
5238 (A=-0, B=+0), but the transformed expressions do not.
5240 The first two transformations are correct if either A or B
5241 is a NaN. In the first transformation, the condition will
5242 be false, and B will indeed be chosen. In the case of the
5243 second transformation, the condition A != B will be true,
5244 and A will be chosen.
5246 The conversions to max() and min() are not correct if B is
5247 a number and A is not. The conditions in the original
5248 expressions will be false, so all four give B. The min()
5249 and max() versions would give a NaN instead. */
5250 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5251 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
5252 /* Avoid these transformations if the COND_EXPR may be used
5253 as an lvalue in the C++ front-end. PR c++/19199. */
5255 || VECTOR_TYPE_P (type
)
5256 || (! lang_GNU_CXX ()
5257 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5258 || ! maybe_lvalue_p (arg1
)
5259 || ! maybe_lvalue_p (arg2
)))
5261 tree comp_op0
= arg00
;
5262 tree comp_op1
= arg01
;
5263 tree comp_type
= TREE_TYPE (comp_op0
);
5268 return fold_convert_loc (loc
, type
, arg2
);
5270 return fold_convert_loc (loc
, type
, arg1
);
5275 /* In C++ a ?: expression can be an lvalue, so put the
5276 operand which will be used if they are equal first
5277 so that we can convert this back to the
5278 corresponding COND_EXPR. */
5279 if (!HONOR_NANS (arg1
))
5281 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5282 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5283 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5284 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5285 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5286 comp_op1
, comp_op0
);
5287 return fold_convert_loc (loc
, type
, tem
);
5294 if (!HONOR_NANS (arg1
))
5296 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5297 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5298 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5299 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5300 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5301 comp_op1
, comp_op0
);
5302 return fold_convert_loc (loc
, type
, tem
);
5306 if (!HONOR_NANS (arg1
))
5307 return fold_convert_loc (loc
, type
, arg2
);
5310 if (!HONOR_NANS (arg1
))
5311 return fold_convert_loc (loc
, type
, arg1
);
5314 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5324 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5325 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5326 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5330 /* EXP is some logical combination of boolean tests. See if we can
5331 merge it into some range test. Return the new tree if so. */
5334 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5337 int or_op
= (code
== TRUTH_ORIF_EXPR
5338 || code
== TRUTH_OR_EXPR
);
5339 int in0_p
, in1_p
, in_p
;
5340 tree low0
, low1
, low
, high0
, high1
, high
;
5341 bool strict_overflow_p
= false;
5343 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5344 "when simplifying range test");
5346 if (!INTEGRAL_TYPE_P (type
))
5349 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5350 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5352 /* If this is an OR operation, invert both sides; we will invert
5353 again at the end. */
5355 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5357 /* If both expressions are the same, if we can merge the ranges, and we
5358 can build the range test, return it or it inverted. If one of the
5359 ranges is always true or always false, consider it to be the same
5360 expression as the other. */
5361 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5362 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5364 && 0 != (tem
= (build_range_check (loc
, type
,
5366 : rhs
!= 0 ? rhs
: integer_zero_node
,
5369 if (strict_overflow_p
)
5370 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5371 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5374 /* On machines where the branch cost is expensive, if this is a
5375 short-circuited branch and the underlying object on both sides
5376 is the same, make a non-short-circuit operation. */
5377 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5378 && lhs
!= 0 && rhs
!= 0
5379 && (code
== TRUTH_ANDIF_EXPR
5380 || code
== TRUTH_ORIF_EXPR
)
5381 && operand_equal_p (lhs
, rhs
, 0))
5383 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5384 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5385 which cases we can't do this. */
5386 if (simple_operand_p (lhs
))
5387 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5388 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5391 else if (!lang_hooks
.decls
.global_bindings_p ()
5392 && !CONTAINS_PLACEHOLDER_P (lhs
))
5394 tree common
= save_expr (lhs
);
5396 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5397 or_op
? ! in0_p
: in0_p
,
5399 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5400 or_op
? ! in1_p
: in1_p
,
5403 if (strict_overflow_p
)
5404 fold_overflow_warning (warnmsg
,
5405 WARN_STRICT_OVERFLOW_COMPARISON
);
5406 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5407 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5416 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5417 bit value. Arrange things so the extra bits will be set to zero if and
5418 only if C is signed-extended to its full width. If MASK is nonzero,
5419 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5422 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5424 tree type
= TREE_TYPE (c
);
5425 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5428 if (p
== modesize
|| unsignedp
)
5431 /* We work by getting just the sign bit into the low-order bit, then
5432 into the high-order bit, then sign-extend. We then XOR that value
5434 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5436 /* We must use a signed type in order to get an arithmetic right shift.
5437 However, we must also avoid introducing accidental overflows, so that
5438 a subsequent call to integer_zerop will work. Hence we must
5439 do the type conversion here. At this point, the constant is either
5440 zero or one, and the conversion to a signed type can never overflow.
5441 We could get an overflow if this conversion is done anywhere else. */
5442 if (TYPE_UNSIGNED (type
))
5443 temp
= fold_convert (signed_type_for (type
), temp
);
5445 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5446 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5448 temp
= const_binop (BIT_AND_EXPR
, temp
,
5449 fold_convert (TREE_TYPE (c
), mask
));
5450 /* If necessary, convert the type back to match the type of C. */
5451 if (TYPE_UNSIGNED (type
))
5452 temp
= fold_convert (type
, temp
);
5454 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5457 /* For an expression that has the form
5461 we can drop one of the inner expressions and simplify to
5465 LOC is the location of the resulting expression. OP is the inner
5466 logical operation; the left-hand side in the examples above, while CMPOP
5467 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5468 removing a condition that guards another, as in
5469 (A != NULL && A->...) || A == NULL
5470 which we must not transform. If RHS_ONLY is true, only eliminate the
5471 right-most operand of the inner logical operation. */
5474 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5477 tree type
= TREE_TYPE (cmpop
);
5478 enum tree_code code
= TREE_CODE (cmpop
);
5479 enum tree_code truthop_code
= TREE_CODE (op
);
5480 tree lhs
= TREE_OPERAND (op
, 0);
5481 tree rhs
= TREE_OPERAND (op
, 1);
5482 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5483 enum tree_code rhs_code
= TREE_CODE (rhs
);
5484 enum tree_code lhs_code
= TREE_CODE (lhs
);
5485 enum tree_code inv_code
;
5487 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5490 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5493 if (rhs_code
== truthop_code
)
5495 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5496 if (newrhs
!= NULL_TREE
)
5499 rhs_code
= TREE_CODE (rhs
);
5502 if (lhs_code
== truthop_code
&& !rhs_only
)
5504 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5505 if (newlhs
!= NULL_TREE
)
5508 lhs_code
= TREE_CODE (lhs
);
5512 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5513 if (inv_code
== rhs_code
5514 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5515 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5517 if (!rhs_only
&& inv_code
== lhs_code
5518 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5519 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5521 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5522 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5527 /* Find ways of folding logical expressions of LHS and RHS:
5528 Try to merge two comparisons to the same innermost item.
5529 Look for range tests like "ch >= '0' && ch <= '9'".
5530 Look for combinations of simple terms on machines with expensive branches
5531 and evaluate the RHS unconditionally.
5533 For example, if we have p->a == 2 && p->b == 4 and we can make an
5534 object large enough to span both A and B, we can do this with a comparison
5535 against the object ANDed with the a mask.
5537 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5538 operations to do this with one comparison.
5540 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5541 function and the one above.
5543 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5544 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5546 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5549 We return the simplified tree or 0 if no optimization is possible. */
5552 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5555 /* If this is the "or" of two comparisons, we can do something if
5556 the comparisons are NE_EXPR. If this is the "and", we can do something
5557 if the comparisons are EQ_EXPR. I.e.,
5558 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5560 WANTED_CODE is this operation code. For single bit fields, we can
5561 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5562 comparison for one-bit fields. */
5564 enum tree_code wanted_code
;
5565 enum tree_code lcode
, rcode
;
5566 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5567 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5568 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5569 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5570 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5571 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5572 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5573 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5574 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5575 machine_mode lnmode
, rnmode
;
5576 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5577 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5578 tree l_const
, r_const
;
5579 tree lntype
, rntype
, result
;
5580 HOST_WIDE_INT first_bit
, end_bit
;
5583 /* Start by getting the comparison codes. Fail if anything is volatile.
5584 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5585 it were surrounded with a NE_EXPR. */
5587 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5590 lcode
= TREE_CODE (lhs
);
5591 rcode
= TREE_CODE (rhs
);
5593 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5595 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5596 build_int_cst (TREE_TYPE (lhs
), 0));
5600 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5602 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5603 build_int_cst (TREE_TYPE (rhs
), 0));
5607 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5608 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5611 ll_arg
= TREE_OPERAND (lhs
, 0);
5612 lr_arg
= TREE_OPERAND (lhs
, 1);
5613 rl_arg
= TREE_OPERAND (rhs
, 0);
5614 rr_arg
= TREE_OPERAND (rhs
, 1);
5616 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5617 if (simple_operand_p (ll_arg
)
5618 && simple_operand_p (lr_arg
))
5620 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5621 && operand_equal_p (lr_arg
, rr_arg
, 0))
5623 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5624 truth_type
, ll_arg
, lr_arg
);
5628 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5629 && operand_equal_p (lr_arg
, rl_arg
, 0))
5631 result
= combine_comparisons (loc
, code
, lcode
,
5632 swap_tree_comparison (rcode
),
5633 truth_type
, ll_arg
, lr_arg
);
5639 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5640 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5642 /* If the RHS can be evaluated unconditionally and its operands are
5643 simple, it wins to evaluate the RHS unconditionally on machines
5644 with expensive branches. In this case, this isn't a comparison
5645 that can be merged. */
5647 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5649 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5650 && simple_operand_p (rl_arg
)
5651 && simple_operand_p (rr_arg
))
5653 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5654 if (code
== TRUTH_OR_EXPR
5655 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5656 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5657 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5658 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5659 return build2_loc (loc
, NE_EXPR
, truth_type
,
5660 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5662 build_int_cst (TREE_TYPE (ll_arg
), 0));
5664 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5665 if (code
== TRUTH_AND_EXPR
5666 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5667 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5668 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5669 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5670 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5671 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5673 build_int_cst (TREE_TYPE (ll_arg
), 0));
5676 /* See if the comparisons can be merged. Then get all the parameters for
5679 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5680 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5683 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5685 ll_inner
= decode_field_reference (loc
, &ll_arg
,
5686 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5687 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5688 &ll_mask
, &ll_and_mask
);
5689 lr_inner
= decode_field_reference (loc
, &lr_arg
,
5690 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5691 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5692 &lr_mask
, &lr_and_mask
);
5693 rl_inner
= decode_field_reference (loc
, &rl_arg
,
5694 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5695 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5696 &rl_mask
, &rl_and_mask
);
5697 rr_inner
= decode_field_reference (loc
, &rr_arg
,
5698 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5699 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5700 &rr_mask
, &rr_and_mask
);
5702 /* It must be true that the inner operation on the lhs of each
5703 comparison must be the same if we are to be able to do anything.
5704 Then see if we have constants. If not, the same must be true for
5707 || ll_reversep
!= rl_reversep
5708 || ll_inner
== 0 || rl_inner
== 0
5709 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5712 if (TREE_CODE (lr_arg
) == INTEGER_CST
5713 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5715 l_const
= lr_arg
, r_const
= rr_arg
;
5716 lr_reversep
= ll_reversep
;
5718 else if (lr_reversep
!= rr_reversep
5719 || lr_inner
== 0 || rr_inner
== 0
5720 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5723 l_const
= r_const
= 0;
5725 /* If either comparison code is not correct for our logical operation,
5726 fail. However, we can convert a one-bit comparison against zero into
5727 the opposite comparison against that bit being set in the field. */
5729 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5730 if (lcode
!= wanted_code
)
5732 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5734 /* Make the left operand unsigned, since we are only interested
5735 in the value of one bit. Otherwise we are doing the wrong
5744 /* This is analogous to the code for l_const above. */
5745 if (rcode
!= wanted_code
)
5747 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5756 /* See if we can find a mode that contains both fields being compared on
5757 the left. If we can't, fail. Otherwise, update all constants and masks
5758 to be relative to a field of that size. */
5759 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5760 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5761 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5762 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5764 if (lnmode
== VOIDmode
)
5767 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5768 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5769 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5770 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5772 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5774 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5775 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5778 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5779 size_int (xll_bitpos
));
5780 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5781 size_int (xrl_bitpos
));
5785 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5786 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5787 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5788 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5789 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5792 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5794 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5799 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5800 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5801 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5802 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5803 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5806 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5808 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5812 /* If the right sides are not constant, do the same for it. Also,
5813 disallow this optimization if a size or signedness mismatch occurs
5814 between the left and right sides. */
5817 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5818 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5819 /* Make sure the two fields on the right
5820 correspond to the left without being swapped. */
5821 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5824 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5825 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5826 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5827 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5829 if (rnmode
== VOIDmode
)
5832 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5833 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5834 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5835 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5837 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5839 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5840 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5843 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5845 size_int (xlr_bitpos
));
5846 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5848 size_int (xrr_bitpos
));
5850 /* Make a mask that corresponds to both fields being compared.
5851 Do this for both items being compared. If the operands are the
5852 same size and the bits being compared are in the same position
5853 then we can do this by masking both and comparing the masked
5855 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5856 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5857 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5859 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5860 lntype
, lnbitsize
, lnbitpos
,
5861 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5862 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5863 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5865 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
5866 rntype
, rnbitsize
, rnbitpos
,
5867 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
5868 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5869 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5871 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5874 /* There is still another way we can do something: If both pairs of
5875 fields being compared are adjacent, we may be able to make a wider
5876 field containing them both.
5878 Note that we still must mask the lhs/rhs expressions. Furthermore,
5879 the mask must be shifted to account for the shift done by
5880 make_bit_field_ref. */
5881 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5882 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5883 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5884 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5888 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
5889 ll_bitsize
+ rl_bitsize
,
5890 MIN (ll_bitpos
, rl_bitpos
),
5891 ll_unsignedp
, ll_reversep
);
5892 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
5893 lr_bitsize
+ rr_bitsize
,
5894 MIN (lr_bitpos
, rr_bitpos
),
5895 lr_unsignedp
, lr_reversep
);
5897 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5898 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5899 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5900 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5902 /* Convert to the smaller type before masking out unwanted bits. */
5904 if (lntype
!= rntype
)
5906 if (lnbitsize
> rnbitsize
)
5908 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5909 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5912 else if (lnbitsize
< rnbitsize
)
5914 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5915 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5920 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5921 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5923 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5924 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5926 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5932 /* Handle the case of comparisons with constants. If there is something in
5933 common between the masks, those bits of the constants must be the same.
5934 If not, the condition is always false. Test for this to avoid generating
5935 incorrect code below. */
5936 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5937 if (! integer_zerop (result
)
5938 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5939 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5941 if (wanted_code
== NE_EXPR
)
5943 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5944 return constant_boolean_node (true, truth_type
);
5948 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5949 return constant_boolean_node (false, truth_type
);
5953 /* Construct the expression we will return. First get the component
5954 reference we will make. Unless the mask is all ones the width of
5955 that field, perform the mask operation. Then compare with the
5957 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5958 lntype
, lnbitsize
, lnbitpos
,
5959 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5961 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5962 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5963 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5965 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5966 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5969 /* T is an integer expression that is being multiplied, divided, or taken a
5970 modulus (CODE says which and what kind of divide or modulus) by a
5971 constant C. See if we can eliminate that operation by folding it with
5972 other operations already in T. WIDE_TYPE, if non-null, is a type that
5973 should be used for the computation if wider than our type.
5975 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5976 (X * 2) + (Y * 4). We must, however, be assured that either the original
5977 expression would not overflow or that overflow is undefined for the type
5978 in the language in question.
5980 If we return a non-null expression, it is an equivalent form of the
5981 original computation, but need not be in the original type.
5983 We set *STRICT_OVERFLOW_P to true if the return values depends on
5984 signed overflow being undefined. Otherwise we do not change
5985 *STRICT_OVERFLOW_P. */
5988 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5989 bool *strict_overflow_p
)
5991 /* To avoid exponential search depth, refuse to allow recursion past
5992 three levels. Beyond that (1) it's highly unlikely that we'll find
5993 something interesting and (2) we've probably processed it before
5994 when we built the inner expression. */
6003 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
6010 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
6011 bool *strict_overflow_p
)
6013 tree type
= TREE_TYPE (t
);
6014 enum tree_code tcode
= TREE_CODE (t
);
6015 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
6016 > GET_MODE_SIZE (TYPE_MODE (type
)))
6017 ? wide_type
: type
);
6019 int same_p
= tcode
== code
;
6020 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
6021 bool sub_strict_overflow_p
;
6023 /* Don't deal with constants of zero here; they confuse the code below. */
6024 if (integer_zerop (c
))
6027 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
6028 op0
= TREE_OPERAND (t
, 0);
6030 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6031 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6033 /* Note that we need not handle conditional operations here since fold
6034 already handles those cases. So just do arithmetic here. */
6038 /* For a constant, we can always simplify if we are a multiply
6039 or (for divide and modulus) if it is a multiple of our constant. */
6040 if (code
== MULT_EXPR
6041 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6043 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6044 fold_convert (ctype
, c
));
6045 /* If the multiplication overflowed, we lost information on it.
6046 See PR68142 and PR69845. */
6047 if (TREE_OVERFLOW (tem
))
6053 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6054 /* If op0 is an expression ... */
6055 if ((COMPARISON_CLASS_P (op0
)
6056 || UNARY_CLASS_P (op0
)
6057 || BINARY_CLASS_P (op0
)
6058 || VL_EXP_CLASS_P (op0
)
6059 || EXPRESSION_CLASS_P (op0
))
6060 /* ... and has wrapping overflow, and its type is smaller
6061 than ctype, then we cannot pass through as widening. */
6062 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6063 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6064 && (TYPE_PRECISION (ctype
)
6065 > TYPE_PRECISION (TREE_TYPE (op0
))))
6066 /* ... or this is a truncation (t is narrower than op0),
6067 then we cannot pass through this narrowing. */
6068 || (TYPE_PRECISION (type
)
6069 < TYPE_PRECISION (TREE_TYPE (op0
)))
6070 /* ... or signedness changes for division or modulus,
6071 then we cannot pass through this conversion. */
6072 || (code
!= MULT_EXPR
6073 && (TYPE_UNSIGNED (ctype
)
6074 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6075 /* ... or has undefined overflow while the converted to
6076 type has not, we cannot do the operation in the inner type
6077 as that would introduce undefined overflow. */
6078 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6079 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6080 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6083 /* Pass the constant down and see if we can make a simplification. If
6084 we can, replace this expression with the inner simplification for
6085 possible later conversion to our or some other type. */
6086 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6087 && TREE_CODE (t2
) == INTEGER_CST
6088 && !TREE_OVERFLOW (t2
)
6089 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6091 ? ctype
: NULL_TREE
,
6092 strict_overflow_p
))))
6097 /* If widening the type changes it from signed to unsigned, then we
6098 must avoid building ABS_EXPR itself as unsigned. */
6099 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6101 tree cstype
= (*signed_type_for
) (ctype
);
6102 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6105 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6106 return fold_convert (ctype
, t1
);
6110 /* If the constant is negative, we cannot simplify this. */
6111 if (tree_int_cst_sgn (c
) == -1)
6115 /* For division and modulus, type can't be unsigned, as e.g.
6116 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6117 For signed types, even with wrapping overflow, this is fine. */
6118 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6120 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6122 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6125 case MIN_EXPR
: case MAX_EXPR
:
6126 /* If widening the type changes the signedness, then we can't perform
6127 this optimization as that changes the result. */
6128 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6131 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6132 sub_strict_overflow_p
= false;
6133 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6134 &sub_strict_overflow_p
)) != 0
6135 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6136 &sub_strict_overflow_p
)) != 0)
6138 if (tree_int_cst_sgn (c
) < 0)
6139 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6140 if (sub_strict_overflow_p
)
6141 *strict_overflow_p
= true;
6142 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6143 fold_convert (ctype
, t2
));
6147 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6148 /* If the second operand is constant, this is a multiplication
6149 or floor division, by a power of two, so we can treat it that
6150 way unless the multiplier or divisor overflows. Signed
6151 left-shift overflow is implementation-defined rather than
6152 undefined in C90, so do not convert signed left shift into
6154 if (TREE_CODE (op1
) == INTEGER_CST
6155 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6156 /* const_binop may not detect overflow correctly,
6157 so check for it explicitly here. */
6158 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6159 && 0 != (t1
= fold_convert (ctype
,
6160 const_binop (LSHIFT_EXPR
,
6163 && !TREE_OVERFLOW (t1
))
6164 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6165 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6167 fold_convert (ctype
, op0
),
6169 c
, code
, wide_type
, strict_overflow_p
);
6172 case PLUS_EXPR
: case MINUS_EXPR
:
6173 /* See if we can eliminate the operation on both sides. If we can, we
6174 can return a new PLUS or MINUS. If we can't, the only remaining
6175 cases where we can do anything are if the second operand is a
6177 sub_strict_overflow_p
= false;
6178 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6179 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6180 if (t1
!= 0 && t2
!= 0
6181 && (code
== MULT_EXPR
6182 /* If not multiplication, we can only do this if both operands
6183 are divisible by c. */
6184 || (multiple_of_p (ctype
, op0
, c
)
6185 && multiple_of_p (ctype
, op1
, c
))))
6187 if (sub_strict_overflow_p
)
6188 *strict_overflow_p
= true;
6189 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6190 fold_convert (ctype
, t2
));
6193 /* If this was a subtraction, negate OP1 and set it to be an addition.
6194 This simplifies the logic below. */
6195 if (tcode
== MINUS_EXPR
)
6197 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6198 /* If OP1 was not easily negatable, the constant may be OP0. */
6199 if (TREE_CODE (op0
) == INTEGER_CST
)
6201 std::swap (op0
, op1
);
6206 if (TREE_CODE (op1
) != INTEGER_CST
)
6209 /* If either OP1 or C are negative, this optimization is not safe for
6210 some of the division and remainder types while for others we need
6211 to change the code. */
6212 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6214 if (code
== CEIL_DIV_EXPR
)
6215 code
= FLOOR_DIV_EXPR
;
6216 else if (code
== FLOOR_DIV_EXPR
)
6217 code
= CEIL_DIV_EXPR
;
6218 else if (code
!= MULT_EXPR
6219 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6223 /* If it's a multiply or a division/modulus operation of a multiple
6224 of our constant, do the operation and verify it doesn't overflow. */
6225 if (code
== MULT_EXPR
6226 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6228 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6229 fold_convert (ctype
, c
));
6230 /* We allow the constant to overflow with wrapping semantics. */
6232 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6238 /* If we have an unsigned type, we cannot widen the operation since it
6239 will change the result if the original computation overflowed. */
6240 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6243 /* If we were able to eliminate our operation from the first side,
6244 apply our operation to the second side and reform the PLUS. */
6245 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6246 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6248 /* The last case is if we are a multiply. In that case, we can
6249 apply the distributive law to commute the multiply and addition
6250 if the multiplication of the constants doesn't overflow
6251 and overflow is defined. With undefined overflow
6252 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6253 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6254 return fold_build2 (tcode
, ctype
,
6255 fold_build2 (code
, ctype
,
6256 fold_convert (ctype
, op0
),
6257 fold_convert (ctype
, c
)),
6263 /* We have a special case here if we are doing something like
6264 (C * 8) % 4 since we know that's zero. */
6265 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6266 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6267 /* If the multiplication can overflow we cannot optimize this. */
6268 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6269 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6270 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6272 *strict_overflow_p
= true;
6273 return omit_one_operand (type
, integer_zero_node
, op0
);
6276 /* ... fall through ... */
6278 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6279 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6280 /* If we can extract our operation from the LHS, do so and return a
6281 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6282 do something only if the second operand is a constant. */
6284 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6285 strict_overflow_p
)) != 0)
6286 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6287 fold_convert (ctype
, op1
));
6288 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6289 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6290 strict_overflow_p
)) != 0)
6291 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6292 fold_convert (ctype
, t1
));
6293 else if (TREE_CODE (op1
) != INTEGER_CST
)
6296 /* If these are the same operation types, we can associate them
6297 assuming no overflow. */
6300 bool overflow_p
= false;
6301 bool overflow_mul_p
;
6302 signop sign
= TYPE_SIGN (ctype
);
6303 unsigned prec
= TYPE_PRECISION (ctype
);
6304 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6305 wi::to_wide (c
, prec
),
6306 sign
, &overflow_mul_p
);
6307 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6309 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6312 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6313 wide_int_to_tree (ctype
, mul
));
6316 /* If these operations "cancel" each other, we have the main
6317 optimizations of this pass, which occur when either constant is a
6318 multiple of the other, in which case we replace this with either an
6319 operation or CODE or TCODE.
6321 If we have an unsigned type, we cannot do this since it will change
6322 the result if the original computation overflowed. */
6323 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6324 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6325 || (tcode
== MULT_EXPR
6326 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6327 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6328 && code
!= MULT_EXPR
)))
6330 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6332 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6333 *strict_overflow_p
= true;
6334 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6335 fold_convert (ctype
,
6336 const_binop (TRUNC_DIV_EXPR
,
6339 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6341 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6342 *strict_overflow_p
= true;
6343 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6344 fold_convert (ctype
,
6345 const_binop (TRUNC_DIV_EXPR
,
6358 /* Return a node which has the indicated constant VALUE (either 0 or
6359 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6360 and is of the indicated TYPE. */
6363 constant_boolean_node (bool value
, tree type
)
6365 if (type
== integer_type_node
)
6366 return value
? integer_one_node
: integer_zero_node
;
6367 else if (type
== boolean_type_node
)
6368 return value
? boolean_true_node
: boolean_false_node
;
6369 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6370 return build_vector_from_val (type
,
6371 build_int_cst (TREE_TYPE (type
),
6374 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6378 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6379 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6380 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6381 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6382 COND is the first argument to CODE; otherwise (as in the example
6383 given here), it is the second argument. TYPE is the type of the
6384 original expression. Return NULL_TREE if no simplification is
6388 fold_binary_op_with_conditional_arg (location_t loc
,
6389 enum tree_code code
,
6390 tree type
, tree op0
, tree op1
,
6391 tree cond
, tree arg
, int cond_first_p
)
6393 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6394 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6395 tree test
, true_value
, false_value
;
6396 tree lhs
= NULL_TREE
;
6397 tree rhs
= NULL_TREE
;
6398 enum tree_code cond_code
= COND_EXPR
;
6400 if (TREE_CODE (cond
) == COND_EXPR
6401 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6403 test
= TREE_OPERAND (cond
, 0);
6404 true_value
= TREE_OPERAND (cond
, 1);
6405 false_value
= TREE_OPERAND (cond
, 2);
6406 /* If this operand throws an expression, then it does not make
6407 sense to try to perform a logical or arithmetic operation
6409 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6411 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6414 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6415 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6417 tree testtype
= TREE_TYPE (cond
);
6419 true_value
= constant_boolean_node (true, testtype
);
6420 false_value
= constant_boolean_node (false, testtype
);
6423 /* Detect the case of mixing vector and scalar types - bail out. */
6426 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6427 cond_code
= VEC_COND_EXPR
;
6429 /* This transformation is only worthwhile if we don't have to wrap ARG
6430 in a SAVE_EXPR and the operation can be simplified without recursing
6431 on at least one of the branches once its pushed inside the COND_EXPR. */
6432 if (!TREE_CONSTANT (arg
)
6433 && (TREE_SIDE_EFFECTS (arg
)
6434 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6435 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6438 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6441 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6443 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6445 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6449 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6451 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6453 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6456 /* Check that we have simplified at least one of the branches. */
6457 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6460 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6464 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6466 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6467 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6468 ADDEND is the same as X.
6470 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6471 and finite. The problematic cases are when X is zero, and its mode
6472 has signed zeros. In the case of rounding towards -infinity,
6473 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6474 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6477 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6479 if (!real_zerop (addend
))
6482 /* Don't allow the fold with -fsignaling-nans. */
6483 if (HONOR_SNANS (element_mode (type
)))
6486 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6487 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6490 /* In a vector or complex, we would need to check the sign of all zeros. */
6491 if (TREE_CODE (addend
) != REAL_CST
)
6494 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6495 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6498 /* The mode has signed zeros, and we have to honor their sign.
6499 In this situation, there is only one case we can return true for.
6500 X - 0 is the same as X unless rounding towards -infinity is
6502 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6505 /* Subroutine of fold() that optimizes comparisons of a division by
6506 a nonzero integer constant against an integer constant, i.e.
6509 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6510 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6511 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6513 The function returns the constant folded tree if a simplification
6514 can be made, and NULL_TREE otherwise. */
6517 fold_div_compare (location_t loc
,
6518 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6520 tree prod
, tmp
, hi
, lo
;
6521 tree arg00
= TREE_OPERAND (arg0
, 0);
6522 tree arg01
= TREE_OPERAND (arg0
, 1);
6523 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6524 bool neg_overflow
= false;
6527 /* We have to do this the hard way to detect unsigned overflow.
6528 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6529 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6530 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6531 neg_overflow
= false;
6533 if (sign
== UNSIGNED
)
6535 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6536 build_int_cst (TREE_TYPE (arg01
), 1));
6539 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6540 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6541 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6542 -1, overflow
| TREE_OVERFLOW (prod
));
6544 else if (tree_int_cst_sgn (arg01
) >= 0)
6546 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6547 build_int_cst (TREE_TYPE (arg01
), 1));
6548 switch (tree_int_cst_sgn (arg1
))
6551 neg_overflow
= true;
6552 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6557 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6562 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6572 /* A negative divisor reverses the relational operators. */
6573 code
= swap_tree_comparison (code
);
6575 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6576 build_int_cst (TREE_TYPE (arg01
), 1));
6577 switch (tree_int_cst_sgn (arg1
))
6580 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6585 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6590 neg_overflow
= true;
6591 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6603 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6604 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6605 if (TREE_OVERFLOW (hi
))
6606 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6607 if (TREE_OVERFLOW (lo
))
6608 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6609 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6612 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6613 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6614 if (TREE_OVERFLOW (hi
))
6615 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6616 if (TREE_OVERFLOW (lo
))
6617 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6618 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6621 if (TREE_OVERFLOW (lo
))
6623 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6624 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6626 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6629 if (TREE_OVERFLOW (hi
))
6631 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6632 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6634 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6637 if (TREE_OVERFLOW (hi
))
6639 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6640 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6642 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6645 if (TREE_OVERFLOW (lo
))
6647 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6648 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6650 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6660 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6661 equality/inequality test, then return a simplified form of the test
6662 using a sign testing. Otherwise return NULL. TYPE is the desired
6666 fold_single_bit_test_into_sign_test (location_t loc
,
6667 enum tree_code code
, tree arg0
, tree arg1
,
6670 /* If this is testing a single bit, we can optimize the test. */
6671 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6672 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6673 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6675 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6676 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6677 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6679 if (arg00
!= NULL_TREE
6680 /* This is only a win if casting to a signed type is cheap,
6681 i.e. when arg00's type is not a partial mode. */
6682 && TYPE_PRECISION (TREE_TYPE (arg00
))
6683 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6685 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6686 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6688 fold_convert_loc (loc
, stype
, arg00
),
6689 build_int_cst (stype
, 0));
6696 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6697 equality/inequality test, then return a simplified form of
6698 the test using shifts and logical operations. Otherwise return
6699 NULL. TYPE is the desired result type. */
6702 fold_single_bit_test (location_t loc
, enum tree_code code
,
6703 tree arg0
, tree arg1
, tree result_type
)
6705 /* If this is testing a single bit, we can optimize the test. */
6706 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6707 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6708 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6710 tree inner
= TREE_OPERAND (arg0
, 0);
6711 tree type
= TREE_TYPE (arg0
);
6712 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6713 machine_mode operand_mode
= TYPE_MODE (type
);
6715 tree signed_type
, unsigned_type
, intermediate_type
;
6718 /* First, see if we can fold the single bit test into a sign-bit
6720 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6725 /* Otherwise we have (A & C) != 0 where C is a single bit,
6726 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6727 Similarly for (A & C) == 0. */
6729 /* If INNER is a right shift of a constant and it plus BITNUM does
6730 not overflow, adjust BITNUM and INNER. */
6731 if (TREE_CODE (inner
) == RSHIFT_EXPR
6732 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6733 && bitnum
< TYPE_PRECISION (type
)
6734 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6735 TYPE_PRECISION (type
) - bitnum
))
6737 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6738 inner
= TREE_OPERAND (inner
, 0);
6741 /* If we are going to be able to omit the AND below, we must do our
6742 operations as unsigned. If we must use the AND, we have a choice.
6743 Normally unsigned is faster, but for some machines signed is. */
6744 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
6745 && !flag_syntax_only
) ? 0 : 1;
6747 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6748 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6749 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6750 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6753 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6754 inner
, size_int (bitnum
));
6756 one
= build_int_cst (intermediate_type
, 1);
6758 if (code
== EQ_EXPR
)
6759 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6761 /* Put the AND last so it can combine with more things. */
6762 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6764 /* Make sure to return the proper type. */
6765 inner
= fold_convert_loc (loc
, result_type
, inner
);
6772 /* Test whether it is preferable two swap two operands, ARG0 and
6773 ARG1, for example because ARG0 is an integer constant and ARG1
6777 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
6779 if (CONSTANT_CLASS_P (arg1
))
6781 if (CONSTANT_CLASS_P (arg0
))
6787 if (TREE_CONSTANT (arg1
))
6789 if (TREE_CONSTANT (arg0
))
6792 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6793 for commutative and comparison operators. Ensuring a canonical
6794 form allows the optimizers to find additional redundancies without
6795 having to explicitly check for both orderings. */
6796 if (TREE_CODE (arg0
) == SSA_NAME
6797 && TREE_CODE (arg1
) == SSA_NAME
6798 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6801 /* Put SSA_NAMEs last. */
6802 if (TREE_CODE (arg1
) == SSA_NAME
)
6804 if (TREE_CODE (arg0
) == SSA_NAME
)
6807 /* Put variables last. */
6817 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6818 means A >= Y && A != MAX, but in this case we know that
6819 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6822 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6824 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6826 if (TREE_CODE (bound
) == LT_EXPR
)
6827 a
= TREE_OPERAND (bound
, 0);
6828 else if (TREE_CODE (bound
) == GT_EXPR
)
6829 a
= TREE_OPERAND (bound
, 1);
6833 typea
= TREE_TYPE (a
);
6834 if (!INTEGRAL_TYPE_P (typea
)
6835 && !POINTER_TYPE_P (typea
))
6838 if (TREE_CODE (ineq
) == LT_EXPR
)
6840 a1
= TREE_OPERAND (ineq
, 1);
6841 y
= TREE_OPERAND (ineq
, 0);
6843 else if (TREE_CODE (ineq
) == GT_EXPR
)
6845 a1
= TREE_OPERAND (ineq
, 0);
6846 y
= TREE_OPERAND (ineq
, 1);
6851 if (TREE_TYPE (a1
) != typea
)
6854 if (POINTER_TYPE_P (typea
))
6856 /* Convert the pointer types into integer before taking the difference. */
6857 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6858 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6859 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6862 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6864 if (!diff
|| !integer_onep (diff
))
6867 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6870 /* Fold a sum or difference of at least one multiplication.
6871 Returns the folded tree or NULL if no simplification could be made. */
6874 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6875 tree arg0
, tree arg1
)
6877 tree arg00
, arg01
, arg10
, arg11
;
6878 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6880 /* (A * C) +- (B * C) -> (A+-B) * C.
6881 (A * C) +- A -> A * (C+-1).
6882 We are most concerned about the case where C is a constant,
6883 but other combinations show up during loop reduction. Since
6884 it is not difficult, try all four possibilities. */
6886 if (TREE_CODE (arg0
) == MULT_EXPR
)
6888 arg00
= TREE_OPERAND (arg0
, 0);
6889 arg01
= TREE_OPERAND (arg0
, 1);
6891 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6893 arg00
= build_one_cst (type
);
6898 /* We cannot generate constant 1 for fract. */
6899 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6902 arg01
= build_one_cst (type
);
6904 if (TREE_CODE (arg1
) == MULT_EXPR
)
6906 arg10
= TREE_OPERAND (arg1
, 0);
6907 arg11
= TREE_OPERAND (arg1
, 1);
6909 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6911 arg10
= build_one_cst (type
);
6912 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6913 the purpose of this canonicalization. */
6914 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
6915 && negate_expr_p (arg1
)
6916 && code
== PLUS_EXPR
)
6918 arg11
= negate_expr (arg1
);
6926 /* We cannot generate constant 1 for fract. */
6927 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6930 arg11
= build_one_cst (type
);
6934 if (operand_equal_p (arg01
, arg11
, 0))
6935 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
6936 else if (operand_equal_p (arg00
, arg10
, 0))
6937 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
6938 else if (operand_equal_p (arg00
, arg11
, 0))
6939 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
6940 else if (operand_equal_p (arg01
, arg10
, 0))
6941 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
6943 /* No identical multiplicands; see if we can find a common
6944 power-of-two factor in non-power-of-two multiplies. This
6945 can help in multi-dimensional array access. */
6946 else if (tree_fits_shwi_p (arg01
)
6947 && tree_fits_shwi_p (arg11
))
6949 HOST_WIDE_INT int01
, int11
, tmp
;
6952 int01
= tree_to_shwi (arg01
);
6953 int11
= tree_to_shwi (arg11
);
6955 /* Move min of absolute values to int11. */
6956 if (absu_hwi (int01
) < absu_hwi (int11
))
6958 tmp
= int01
, int01
= int11
, int11
= tmp
;
6959 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
6966 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
6967 /* The remainder should not be a constant, otherwise we
6968 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6969 increased the number of multiplications necessary. */
6970 && TREE_CODE (arg10
) != INTEGER_CST
)
6972 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
6973 build_int_cst (TREE_TYPE (arg00
),
6978 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
6983 return fold_build2_loc (loc
, MULT_EXPR
, type
,
6984 fold_build2_loc (loc
, code
, type
,
6985 fold_convert_loc (loc
, type
, alt0
),
6986 fold_convert_loc (loc
, type
, alt1
)),
6987 fold_convert_loc (loc
, type
, same
));
6992 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6993 specified by EXPR into the buffer PTR of length LEN bytes.
6994 Return the number of bytes placed in the buffer, or zero
6998 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7000 tree type
= TREE_TYPE (expr
);
7001 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7002 int byte
, offset
, word
, words
;
7003 unsigned char value
;
7005 if ((off
== -1 && total_bytes
> len
)
7006 || off
>= total_bytes
)
7010 words
= total_bytes
/ UNITS_PER_WORD
;
7012 for (byte
= 0; byte
< total_bytes
; byte
++)
7014 int bitpos
= byte
* BITS_PER_UNIT
;
7015 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7017 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7019 if (total_bytes
> UNITS_PER_WORD
)
7021 word
= byte
/ UNITS_PER_WORD
;
7022 if (WORDS_BIG_ENDIAN
)
7023 word
= (words
- 1) - word
;
7024 offset
= word
* UNITS_PER_WORD
;
7025 if (BYTES_BIG_ENDIAN
)
7026 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7028 offset
+= byte
% UNITS_PER_WORD
;
7031 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7033 && offset
- off
< len
)
7034 ptr
[offset
- off
] = value
;
7036 return MIN (len
, total_bytes
- off
);
7040 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7041 specified by EXPR into the buffer PTR of length LEN bytes.
7042 Return the number of bytes placed in the buffer, or zero
7046 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7048 tree type
= TREE_TYPE (expr
);
7049 machine_mode mode
= TYPE_MODE (type
);
7050 int total_bytes
= GET_MODE_SIZE (mode
);
7051 FIXED_VALUE_TYPE value
;
7052 tree i_value
, i_type
;
7054 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7057 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7059 if (NULL_TREE
== i_type
7060 || TYPE_PRECISION (i_type
) != total_bytes
)
7063 value
= TREE_FIXED_CST (expr
);
7064 i_value
= double_int_to_tree (i_type
, value
.data
);
7066 return native_encode_int (i_value
, ptr
, len
, off
);
7070 /* Subroutine of native_encode_expr. Encode the REAL_CST
7071 specified by EXPR into the buffer PTR of length LEN bytes.
7072 Return the number of bytes placed in the buffer, or zero
7076 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7078 tree type
= TREE_TYPE (expr
);
7079 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7080 int byte
, offset
, word
, words
, bitpos
;
7081 unsigned char value
;
7083 /* There are always 32 bits in each long, no matter the size of
7084 the hosts long. We handle floating point representations with
7088 if ((off
== -1 && total_bytes
> len
)
7089 || off
>= total_bytes
)
7093 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7095 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7097 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7098 bitpos
+= BITS_PER_UNIT
)
7100 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7101 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7103 if (UNITS_PER_WORD
< 4)
7105 word
= byte
/ UNITS_PER_WORD
;
7106 if (WORDS_BIG_ENDIAN
)
7107 word
= (words
- 1) - word
;
7108 offset
= word
* UNITS_PER_WORD
;
7109 if (BYTES_BIG_ENDIAN
)
7110 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7112 offset
+= byte
% UNITS_PER_WORD
;
7117 if (BYTES_BIG_ENDIAN
)
7119 /* Reverse bytes within each long, or within the entire float
7120 if it's smaller than a long (for HFmode). */
7121 offset
= MIN (3, total_bytes
- 1) - offset
;
7122 gcc_assert (offset
>= 0);
7125 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7127 && offset
- off
< len
)
7128 ptr
[offset
- off
] = value
;
7130 return MIN (len
, total_bytes
- off
);
7133 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7134 specified by EXPR into the buffer PTR of length LEN bytes.
7135 Return the number of bytes placed in the buffer, or zero
7139 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7144 part
= TREE_REALPART (expr
);
7145 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7149 part
= TREE_IMAGPART (expr
);
7151 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7152 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7156 return rsize
+ isize
;
7160 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7161 specified by EXPR into the buffer PTR of length LEN bytes.
7162 Return the number of bytes placed in the buffer, or zero
7166 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7173 count
= VECTOR_CST_NELTS (expr
);
7174 itype
= TREE_TYPE (TREE_TYPE (expr
));
7175 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7176 for (i
= 0; i
< count
; i
++)
7183 elem
= VECTOR_CST_ELT (expr
, i
);
7184 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7185 if ((off
== -1 && res
!= size
)
7198 /* Subroutine of native_encode_expr. Encode the STRING_CST
7199 specified by EXPR into the buffer PTR of length LEN bytes.
7200 Return the number of bytes placed in the buffer, or zero
7204 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7206 tree type
= TREE_TYPE (expr
);
7207 HOST_WIDE_INT total_bytes
;
7209 if (TREE_CODE (type
) != ARRAY_TYPE
7210 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7211 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7212 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7214 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7215 if ((off
== -1 && total_bytes
> len
)
7216 || off
>= total_bytes
)
7220 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7223 if (off
< TREE_STRING_LENGTH (expr
))
7225 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7226 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7228 memset (ptr
+ written
, 0,
7229 MIN (total_bytes
- written
, len
- written
));
7232 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7233 return MIN (total_bytes
- off
, len
);
7237 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7238 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7239 buffer PTR of length LEN bytes. If OFF is not -1 then start
7240 the encoding at byte offset OFF and encode at most LEN bytes.
7241 Return the number of bytes placed in the buffer, or zero upon failure. */
7244 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7246 /* We don't support starting at negative offset and -1 is special. */
7250 switch (TREE_CODE (expr
))
7253 return native_encode_int (expr
, ptr
, len
, off
);
7256 return native_encode_real (expr
, ptr
, len
, off
);
7259 return native_encode_fixed (expr
, ptr
, len
, off
);
7262 return native_encode_complex (expr
, ptr
, len
, off
);
7265 return native_encode_vector (expr
, ptr
, len
, off
);
7268 return native_encode_string (expr
, ptr
, len
, off
);
7276 /* Subroutine of native_interpret_expr. Interpret the contents of
7277 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7278 If the buffer cannot be interpreted, return NULL_TREE. */
7281 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7283 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7285 if (total_bytes
> len
7286 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7289 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7291 return wide_int_to_tree (type
, result
);
7295 /* Subroutine of native_interpret_expr. Interpret the contents of
7296 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7297 If the buffer cannot be interpreted, return NULL_TREE. */
7300 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7302 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7304 FIXED_VALUE_TYPE fixed_value
;
7306 if (total_bytes
> len
7307 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7310 result
= double_int::from_buffer (ptr
, total_bytes
);
7311 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7313 return build_fixed (type
, fixed_value
);
7317 /* Subroutine of native_interpret_expr. Interpret the contents of
7318 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7319 If the buffer cannot be interpreted, return NULL_TREE. */
7322 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7324 machine_mode mode
= TYPE_MODE (type
);
7325 int total_bytes
= GET_MODE_SIZE (mode
);
7326 unsigned char value
;
7327 /* There are always 32 bits in each long, no matter the size of
7328 the hosts long. We handle floating point representations with
7333 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7334 if (total_bytes
> len
|| total_bytes
> 24)
7336 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7338 memset (tmp
, 0, sizeof (tmp
));
7339 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7340 bitpos
+= BITS_PER_UNIT
)
7342 /* Both OFFSET and BYTE index within a long;
7343 bitpos indexes the whole float. */
7344 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7345 if (UNITS_PER_WORD
< 4)
7347 int word
= byte
/ UNITS_PER_WORD
;
7348 if (WORDS_BIG_ENDIAN
)
7349 word
= (words
- 1) - word
;
7350 offset
= word
* UNITS_PER_WORD
;
7351 if (BYTES_BIG_ENDIAN
)
7352 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7354 offset
+= byte
% UNITS_PER_WORD
;
7359 if (BYTES_BIG_ENDIAN
)
7361 /* Reverse bytes within each long, or within the entire float
7362 if it's smaller than a long (for HFmode). */
7363 offset
= MIN (3, total_bytes
- 1) - offset
;
7364 gcc_assert (offset
>= 0);
7367 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7369 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7372 real_from_target (&r
, tmp
, mode
);
7373 return build_real (type
, r
);
7377 /* Subroutine of native_interpret_expr. Interpret the contents of
7378 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7379 If the buffer cannot be interpreted, return NULL_TREE. */
7382 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7384 tree etype
, rpart
, ipart
;
7387 etype
= TREE_TYPE (type
);
7388 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7391 rpart
= native_interpret_expr (etype
, ptr
, size
);
7394 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7397 return build_complex (type
, rpart
, ipart
);
7401 /* Subroutine of native_interpret_expr. Interpret the contents of
7402 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7403 If the buffer cannot be interpreted, return NULL_TREE. */
7406 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7412 etype
= TREE_TYPE (type
);
7413 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7414 count
= TYPE_VECTOR_SUBPARTS (type
);
7415 if (size
* count
> len
)
7418 elements
= XALLOCAVEC (tree
, count
);
7419 for (i
= count
- 1; i
>= 0; i
--)
7421 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7426 return build_vector (type
, elements
);
7430 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7431 the buffer PTR of length LEN as a constant of type TYPE. For
7432 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7433 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7434 return NULL_TREE. */
7437 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7439 switch (TREE_CODE (type
))
7445 case REFERENCE_TYPE
:
7446 return native_interpret_int (type
, ptr
, len
);
7449 return native_interpret_real (type
, ptr
, len
);
7451 case FIXED_POINT_TYPE
:
7452 return native_interpret_fixed (type
, ptr
, len
);
7455 return native_interpret_complex (type
, ptr
, len
);
7458 return native_interpret_vector (type
, ptr
, len
);
7465 /* Returns true if we can interpret the contents of a native encoding
7469 can_native_interpret_type_p (tree type
)
7471 switch (TREE_CODE (type
))
7477 case REFERENCE_TYPE
:
7478 case FIXED_POINT_TYPE
:
7488 /* Return true iff a constant of type TYPE is accepted by
7489 native_encode_expr. */
7492 can_native_encode_type_p (tree type
)
7494 switch (TREE_CODE (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);
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 above 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.
8281 For function local objects always return positive integer. */
8283 maybe_nonzero_address (tree decl
)
8285 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
8286 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
8287 return symbol
->nonzero_address ();
8289 /* Function local objects are never NULL. */
8291 && (DECL_CONTEXT (decl
)
8292 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
8293 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
8299 /* Subroutine of fold_binary. This routine performs all of the
8300 transformations that are common to the equality/inequality
8301 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8302 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8303 fold_binary should call fold_binary. Fold a comparison with
8304 tree code CODE and type TYPE with operands OP0 and OP1. Return
8305 the folded comparison or NULL_TREE. */
8308 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8311 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8312 tree arg0
, arg1
, tem
;
8317 STRIP_SIGN_NOPS (arg0
);
8318 STRIP_SIGN_NOPS (arg1
);
8320 /* For comparisons of pointers we can decompose it to a compile time
8321 comparison of the base objects and the offsets into the object.
8322 This requires at least one operand being an ADDR_EXPR or a
8323 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8324 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8325 && (TREE_CODE (arg0
) == ADDR_EXPR
8326 || TREE_CODE (arg1
) == ADDR_EXPR
8327 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8328 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8330 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8331 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8333 int volatilep
, reversep
, unsignedp
;
8334 bool indirect_base0
= false, indirect_base1
= false;
8336 /* Get base and offset for the access. Strip ADDR_EXPR for
8337 get_inner_reference, but put it back by stripping INDIRECT_REF
8338 off the base object if possible. indirect_baseN will be true
8339 if baseN is not an address but refers to the object itself. */
8341 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8344 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8345 &bitsize
, &bitpos0
, &offset0
, &mode
,
8346 &unsignedp
, &reversep
, &volatilep
);
8347 if (TREE_CODE (base0
) == INDIRECT_REF
)
8348 base0
= TREE_OPERAND (base0
, 0);
8350 indirect_base0
= true;
8352 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8354 base0
= TREE_OPERAND (arg0
, 0);
8355 STRIP_SIGN_NOPS (base0
);
8356 if (TREE_CODE (base0
) == ADDR_EXPR
)
8359 = get_inner_reference (TREE_OPERAND (base0
, 0),
8360 &bitsize
, &bitpos0
, &offset0
, &mode
,
8361 &unsignedp
, &reversep
, &volatilep
);
8362 if (TREE_CODE (base0
) == INDIRECT_REF
)
8363 base0
= TREE_OPERAND (base0
, 0);
8365 indirect_base0
= true;
8367 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8368 offset0
= TREE_OPERAND (arg0
, 1);
8370 offset0
= size_binop (PLUS_EXPR
, offset0
,
8371 TREE_OPERAND (arg0
, 1));
8372 if (TREE_CODE (offset0
) == INTEGER_CST
)
8374 offset_int tem
= wi::sext (wi::to_offset (offset0
),
8375 TYPE_PRECISION (sizetype
));
8376 tem
<<= LOG2_BITS_PER_UNIT
;
8378 if (wi::fits_shwi_p (tem
))
8380 bitpos0
= tem
.to_shwi ();
8381 offset0
= NULL_TREE
;
8387 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8390 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8391 &bitsize
, &bitpos1
, &offset1
, &mode
,
8392 &unsignedp
, &reversep
, &volatilep
);
8393 if (TREE_CODE (base1
) == INDIRECT_REF
)
8394 base1
= TREE_OPERAND (base1
, 0);
8396 indirect_base1
= true;
8398 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8400 base1
= TREE_OPERAND (arg1
, 0);
8401 STRIP_SIGN_NOPS (base1
);
8402 if (TREE_CODE (base1
) == ADDR_EXPR
)
8405 = get_inner_reference (TREE_OPERAND (base1
, 0),
8406 &bitsize
, &bitpos1
, &offset1
, &mode
,
8407 &unsignedp
, &reversep
, &volatilep
);
8408 if (TREE_CODE (base1
) == INDIRECT_REF
)
8409 base1
= TREE_OPERAND (base1
, 0);
8411 indirect_base1
= true;
8413 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8414 offset1
= TREE_OPERAND (arg1
, 1);
8416 offset1
= size_binop (PLUS_EXPR
, offset1
,
8417 TREE_OPERAND (arg1
, 1));
8418 if (TREE_CODE (offset1
) == INTEGER_CST
)
8420 offset_int tem
= wi::sext (wi::to_offset (offset1
),
8421 TYPE_PRECISION (sizetype
));
8422 tem
<<= LOG2_BITS_PER_UNIT
;
8424 if (wi::fits_shwi_p (tem
))
8426 bitpos1
= tem
.to_shwi ();
8427 offset1
= NULL_TREE
;
8432 /* If we have equivalent bases we might be able to simplify. */
8433 if (indirect_base0
== indirect_base1
8434 && operand_equal_p (base0
, base1
,
8435 indirect_base0
? OEP_ADDRESS_OF
: 0))
8437 /* We can fold this expression to a constant if the non-constant
8438 offset parts are equal. */
8439 if ((offset0
== offset1
8440 || (offset0
&& offset1
8441 && operand_equal_p (offset0
, offset1
, 0)))
8444 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8445 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8449 && bitpos0
!= bitpos1
8450 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8451 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8452 fold_overflow_warning (("assuming pointer wraparound does not "
8453 "occur when comparing P +- C1 with "
8455 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8460 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8462 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8464 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8466 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8468 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8470 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8474 /* We can simplify the comparison to a comparison of the variable
8475 offset parts if the constant offset parts are equal.
8476 Be careful to use signed sizetype here because otherwise we
8477 mess with array offsets in the wrong way. This is possible
8478 because pointer arithmetic is restricted to retain within an
8479 object and overflow on pointer differences is undefined as of
8480 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8481 else if (bitpos0
== bitpos1
8484 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8485 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8487 /* By converting to signed sizetype we cover middle-end pointer
8488 arithmetic which operates on unsigned pointer types of size
8489 type size and ARRAY_REF offsets which are properly sign or
8490 zero extended from their type in case it is narrower than
8492 if (offset0
== NULL_TREE
)
8493 offset0
= build_int_cst (ssizetype
, 0);
8495 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8496 if (offset1
== NULL_TREE
)
8497 offset1
= build_int_cst (ssizetype
, 0);
8499 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8502 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8503 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8504 fold_overflow_warning (("assuming pointer wraparound does not "
8505 "occur when comparing P +- C1 with "
8507 WARN_STRICT_OVERFLOW_COMPARISON
);
8509 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8512 /* For equal offsets we can simplify to a comparison of the
8514 else if (bitpos0
== bitpos1
8516 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8518 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8519 && ((offset0
== offset1
)
8520 || (offset0
&& offset1
8521 && operand_equal_p (offset0
, offset1
, 0))))
8524 base0
= build_fold_addr_expr_loc (loc
, base0
);
8526 base1
= build_fold_addr_expr_loc (loc
, base1
);
8527 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8529 /* Comparison between an ordinary (non-weak) symbol and a null
8530 pointer can be eliminated since such symbols must have a non
8531 null address. In C, relational expressions between pointers
8532 to objects and null pointers are undefined. The results
8533 below follow the C++ rules with the additional property that
8534 every object pointer compares greater than a null pointer.
8536 else if (((DECL_P (base0
)
8537 && maybe_nonzero_address (base0
) > 0
8538 /* Avoid folding references to struct members at offset 0 to
8539 prevent tests like '&ptr->firstmember == 0' from getting
8540 eliminated. When ptr is null, although the -> expression
8541 is strictly speaking invalid, GCC retains it as a matter
8542 of QoI. See PR c/44555. */
8543 && (offset0
== NULL_TREE
&& bitpos0
!= 0))
8544 || CONSTANT_CLASS_P (base0
))
8546 /* The caller guarantees that when one of the arguments is
8547 constant (i.e., null in this case) it is second. */
8548 && integer_zerop (arg1
))
8555 return constant_boolean_node (false, type
);
8559 return constant_boolean_node (true, type
);
8566 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8567 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8568 the resulting offset is smaller in absolute value than the
8569 original one and has the same sign. */
8570 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8571 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8572 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8573 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8574 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8575 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8576 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8577 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8579 tree const1
= TREE_OPERAND (arg0
, 1);
8580 tree const2
= TREE_OPERAND (arg1
, 1);
8581 tree variable1
= TREE_OPERAND (arg0
, 0);
8582 tree variable2
= TREE_OPERAND (arg1
, 0);
8584 const char * const warnmsg
= G_("assuming signed overflow does not "
8585 "occur when combining constants around "
8588 /* Put the constant on the side where it doesn't overflow and is
8589 of lower absolute value and of same sign than before. */
8590 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8591 ? MINUS_EXPR
: PLUS_EXPR
,
8593 if (!TREE_OVERFLOW (cst
)
8594 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8595 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8597 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8598 return fold_build2_loc (loc
, code
, type
,
8600 fold_build2_loc (loc
, TREE_CODE (arg1
),
8605 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8606 ? MINUS_EXPR
: PLUS_EXPR
,
8608 if (!TREE_OVERFLOW (cst
)
8609 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8610 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8612 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8613 return fold_build2_loc (loc
, code
, type
,
8614 fold_build2_loc (loc
, TREE_CODE (arg0
),
8621 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8625 /* If we are comparing an expression that just has comparisons
8626 of two integer values, arithmetic expressions of those comparisons,
8627 and constants, we can simplify it. There are only three cases
8628 to check: the two values can either be equal, the first can be
8629 greater, or the second can be greater. Fold the expression for
8630 those three values. Since each value must be 0 or 1, we have
8631 eight possibilities, each of which corresponds to the constant 0
8632 or 1 or one of the six possible comparisons.
8634 This handles common cases like (a > b) == 0 but also handles
8635 expressions like ((x > y) - (y > x)) > 0, which supposedly
8636 occur in macroized code. */
8638 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8640 tree cval1
= 0, cval2
= 0;
8643 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8644 /* Don't handle degenerate cases here; they should already
8645 have been handled anyway. */
8646 && cval1
!= 0 && cval2
!= 0
8647 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8648 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8649 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8650 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8651 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8652 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8653 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8655 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8656 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8658 /* We can't just pass T to eval_subst in case cval1 or cval2
8659 was the same as ARG1. */
8662 = fold_build2_loc (loc
, code
, type
,
8663 eval_subst (loc
, arg0
, cval1
, maxval
,
8667 = fold_build2_loc (loc
, code
, type
,
8668 eval_subst (loc
, arg0
, cval1
, maxval
,
8672 = fold_build2_loc (loc
, code
, type
,
8673 eval_subst (loc
, arg0
, cval1
, minval
,
8677 /* All three of these results should be 0 or 1. Confirm they are.
8678 Then use those values to select the proper code to use. */
8680 if (TREE_CODE (high_result
) == INTEGER_CST
8681 && TREE_CODE (equal_result
) == INTEGER_CST
8682 && TREE_CODE (low_result
) == INTEGER_CST
)
8684 /* Make a 3-bit mask with the high-order bit being the
8685 value for `>', the next for '=', and the low for '<'. */
8686 switch ((integer_onep (high_result
) * 4)
8687 + (integer_onep (equal_result
) * 2)
8688 + integer_onep (low_result
))
8692 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8713 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8718 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8719 protected_set_expr_location (tem
, loc
);
8722 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8727 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8728 into a single range test. */
8729 if (TREE_CODE (arg0
) == TRUNC_DIV_EXPR
8730 && TREE_CODE (arg1
) == INTEGER_CST
8731 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8732 && !integer_zerop (TREE_OPERAND (arg0
, 1))
8733 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8734 && !TREE_OVERFLOW (arg1
))
8736 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
8737 if (tem
!= NULL_TREE
)
8745 /* Subroutine of fold_binary. Optimize complex multiplications of the
8746 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8747 argument EXPR represents the expression "z" of type TYPE. */
8750 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8752 tree itype
= TREE_TYPE (type
);
8753 tree rpart
, ipart
, tem
;
8755 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8757 rpart
= TREE_OPERAND (expr
, 0);
8758 ipart
= TREE_OPERAND (expr
, 1);
8760 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8762 rpart
= TREE_REALPART (expr
);
8763 ipart
= TREE_IMAGPART (expr
);
8767 expr
= save_expr (expr
);
8768 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8769 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8772 rpart
= save_expr (rpart
);
8773 ipart
= save_expr (ipart
);
8774 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8775 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8776 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8777 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8778 build_zero_cst (itype
));
8782 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8783 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8786 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
8788 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
8790 if (TREE_CODE (arg
) == VECTOR_CST
)
8792 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8793 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8795 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8797 constructor_elt
*elt
;
8799 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8800 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8803 elts
[i
] = elt
->value
;
8807 for (; i
< nelts
; i
++)
8809 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8813 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8814 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8815 NULL_TREE otherwise. */
8818 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
8820 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8822 bool need_ctor
= false;
8824 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8825 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8826 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8827 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8830 elts
= XALLOCAVEC (tree
, nelts
* 3);
8831 if (!vec_cst_ctor_to_array (arg0
, elts
)
8832 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
8835 for (i
= 0; i
< nelts
; i
++)
8837 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
8839 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
8844 vec
<constructor_elt
, va_gc
> *v
;
8845 vec_alloc (v
, nelts
);
8846 for (i
= 0; i
< nelts
; i
++)
8847 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
8848 return build_constructor (type
, v
);
8851 return build_vector (type
, &elts
[2 * nelts
]);
8854 /* Try to fold a pointer difference of type TYPE two address expressions of
8855 array references AREF0 and AREF1 using location LOC. Return a
8856 simplified expression for the difference or NULL_TREE. */
8859 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8860 tree aref0
, tree aref1
)
8862 tree base0
= TREE_OPERAND (aref0
, 0);
8863 tree base1
= TREE_OPERAND (aref1
, 0);
8864 tree base_offset
= build_int_cst (type
, 0);
8866 /* If the bases are array references as well, recurse. If the bases
8867 are pointer indirections compute the difference of the pointers.
8868 If the bases are equal, we are set. */
8869 if ((TREE_CODE (base0
) == ARRAY_REF
8870 && TREE_CODE (base1
) == ARRAY_REF
8872 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8873 || (INDIRECT_REF_P (base0
)
8874 && INDIRECT_REF_P (base1
)
8876 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8877 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8879 TREE_OPERAND (base1
, 0)))))
8880 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8882 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8883 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8884 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8885 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
8886 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8888 fold_build2_loc (loc
, MULT_EXPR
, type
,
8894 /* If the real or vector real constant CST of type TYPE has an exact
8895 inverse, return it, else return NULL. */
8898 exact_inverse (tree type
, tree cst
)
8901 tree unit_type
, *elts
;
8903 unsigned vec_nelts
, i
;
8905 switch (TREE_CODE (cst
))
8908 r
= TREE_REAL_CST (cst
);
8910 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8911 return build_real (type
, r
);
8916 vec_nelts
= VECTOR_CST_NELTS (cst
);
8917 elts
= XALLOCAVEC (tree
, vec_nelts
);
8918 unit_type
= TREE_TYPE (type
);
8919 mode
= TYPE_MODE (unit_type
);
8921 for (i
= 0; i
< vec_nelts
; i
++)
8923 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
8924 if (!exact_real_inverse (mode
, &r
))
8926 elts
[i
] = build_real (unit_type
, r
);
8929 return build_vector (type
, elts
);
8936 /* Mask out the tz least significant bits of X of type TYPE where
8937 tz is the number of trailing zeroes in Y. */
8939 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
8941 int tz
= wi::ctz (y
);
8943 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
8947 /* Return true when T is an address and is known to be nonzero.
8948 For floating point we further ensure that T is not denormal.
8949 Similar logic is present in nonzero_address in rtlanal.h.
8951 If the return value is based on the assumption that signed overflow
8952 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8953 change *STRICT_OVERFLOW_P. */
8956 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
8958 tree type
= TREE_TYPE (t
);
8959 enum tree_code code
;
8961 /* Doing something useful for floating point would need more work. */
8962 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
8965 code
= TREE_CODE (t
);
8966 switch (TREE_CODE_CLASS (code
))
8969 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8972 case tcc_comparison
:
8973 return tree_binary_nonzero_warnv_p (code
, type
,
8974 TREE_OPERAND (t
, 0),
8975 TREE_OPERAND (t
, 1),
8978 case tcc_declaration
:
8980 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8988 case TRUTH_NOT_EXPR
:
8989 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8992 case TRUTH_AND_EXPR
:
8994 case TRUTH_XOR_EXPR
:
8995 return tree_binary_nonzero_warnv_p (code
, type
,
8996 TREE_OPERAND (t
, 0),
8997 TREE_OPERAND (t
, 1),
9005 case WITH_SIZE_EXPR
:
9007 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9012 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9016 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9021 tree fndecl
= get_callee_fndecl (t
);
9022 if (!fndecl
) return false;
9023 if (flag_delete_null_pointer_checks
&& !flag_check_new
9024 && DECL_IS_OPERATOR_NEW (fndecl
)
9025 && !TREE_NOTHROW (fndecl
))
9027 if (flag_delete_null_pointer_checks
9028 && lookup_attribute ("returns_nonnull",
9029 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9031 return alloca_call_p (t
);
9040 /* Return true when T is an address and is known to be nonzero.
9041 Handle warnings about undefined signed overflow. */
9044 tree_expr_nonzero_p (tree t
)
9046 bool ret
, strict_overflow_p
;
9048 strict_overflow_p
= false;
9049 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9050 if (strict_overflow_p
)
9051 fold_overflow_warning (("assuming signed overflow does not occur when "
9052 "determining that expression is always "
9054 WARN_STRICT_OVERFLOW_MISC
);
9058 /* Return true if T is known not to be equal to an integer W. */
9061 expr_not_equal_to (tree t
, const wide_int
&w
)
9063 wide_int min
, max
, nz
;
9064 value_range_type rtype
;
9065 switch (TREE_CODE (t
))
9068 return wi::ne_p (t
, w
);
9071 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9073 rtype
= get_range_info (t
, &min
, &max
);
9074 if (rtype
== VR_RANGE
)
9076 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9078 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9081 else if (rtype
== VR_ANTI_RANGE
9082 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9083 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9085 /* If T has some known zero bits and W has any of those bits set,
9086 then T is known not to be equal to W. */
9087 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9088 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9097 /* Fold a binary expression of code CODE and type TYPE with operands
9098 OP0 and OP1. LOC is the location of the resulting expression.
9099 Return the folded expression if folding is successful. Otherwise,
9100 return NULL_TREE. */
9103 fold_binary_loc (location_t loc
,
9104 enum tree_code code
, tree type
, tree op0
, tree op1
)
9106 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9107 tree arg0
, arg1
, tem
;
9108 tree t1
= NULL_TREE
;
9109 bool strict_overflow_p
;
9112 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9113 && TREE_CODE_LENGTH (code
) == 2
9115 && op1
!= NULL_TREE
);
9120 /* Strip any conversions that don't change the mode. This is
9121 safe for every expression, except for a comparison expression
9122 because its signedness is derived from its operands. So, in
9123 the latter case, only strip conversions that don't change the
9124 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9127 Note that this is done as an internal manipulation within the
9128 constant folder, in order to find the simplest representation
9129 of the arguments so that their form can be studied. In any
9130 cases, the appropriate type conversions should be put back in
9131 the tree that will get out of the constant folder. */
9133 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9135 STRIP_SIGN_NOPS (arg0
);
9136 STRIP_SIGN_NOPS (arg1
);
9144 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9145 constant but we can't do arithmetic on them. */
9146 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9148 tem
= const_binop (code
, type
, arg0
, arg1
);
9149 if (tem
!= NULL_TREE
)
9151 if (TREE_TYPE (tem
) != type
)
9152 tem
= fold_convert_loc (loc
, type
, tem
);
9157 /* If this is a commutative operation, and ARG0 is a constant, move it
9158 to ARG1 to reduce the number of tests below. */
9159 if (commutative_tree_code (code
)
9160 && tree_swap_operands_p (arg0
, arg1
))
9161 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9163 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9164 to ARG1 to reduce the number of tests below. */
9165 if (kind
== tcc_comparison
9166 && tree_swap_operands_p (arg0
, arg1
))
9167 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9169 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9173 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9175 First check for cases where an arithmetic operation is applied to a
9176 compound, conditional, or comparison operation. Push the arithmetic
9177 operation inside the compound or conditional to see if any folding
9178 can then be done. Convert comparison to conditional for this purpose.
9179 The also optimizes non-constant cases that used to be done in
9182 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9183 one of the operands is a comparison and the other is a comparison, a
9184 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9185 code below would make the expression more complex. Change it to a
9186 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9187 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9189 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9190 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9191 && TREE_CODE (type
) != VECTOR_TYPE
9192 && ((truth_value_p (TREE_CODE (arg0
))
9193 && (truth_value_p (TREE_CODE (arg1
))
9194 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9195 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9196 || (truth_value_p (TREE_CODE (arg1
))
9197 && (truth_value_p (TREE_CODE (arg0
))
9198 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9199 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9201 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9202 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9205 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9206 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9208 if (code
== EQ_EXPR
)
9209 tem
= invert_truthvalue_loc (loc
, tem
);
9211 return fold_convert_loc (loc
, type
, tem
);
9214 if (TREE_CODE_CLASS (code
) == tcc_binary
9215 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9217 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9219 tem
= fold_build2_loc (loc
, code
, type
,
9220 fold_convert_loc (loc
, TREE_TYPE (op0
),
9221 TREE_OPERAND (arg0
, 1)), op1
);
9222 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9225 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
9227 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9228 fold_convert_loc (loc
, TREE_TYPE (op1
),
9229 TREE_OPERAND (arg1
, 1)));
9230 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9234 if (TREE_CODE (arg0
) == COND_EXPR
9235 || TREE_CODE (arg0
) == VEC_COND_EXPR
9236 || COMPARISON_CLASS_P (arg0
))
9238 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9240 /*cond_first_p=*/1);
9241 if (tem
!= NULL_TREE
)
9245 if (TREE_CODE (arg1
) == COND_EXPR
9246 || TREE_CODE (arg1
) == VEC_COND_EXPR
9247 || COMPARISON_CLASS_P (arg1
))
9249 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9251 /*cond_first_p=*/0);
9252 if (tem
!= NULL_TREE
)
9260 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9261 if (TREE_CODE (arg0
) == ADDR_EXPR
9262 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9264 tree iref
= TREE_OPERAND (arg0
, 0);
9265 return fold_build2 (MEM_REF
, type
,
9266 TREE_OPERAND (iref
, 0),
9267 int_const_binop (PLUS_EXPR
, arg1
,
9268 TREE_OPERAND (iref
, 1)));
9271 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9272 if (TREE_CODE (arg0
) == ADDR_EXPR
9273 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9276 HOST_WIDE_INT coffset
;
9277 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9281 return fold_build2 (MEM_REF
, type
,
9282 build_fold_addr_expr (base
),
9283 int_const_binop (PLUS_EXPR
, arg1
,
9284 size_int (coffset
)));
9289 case POINTER_PLUS_EXPR
:
9290 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9291 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9292 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9293 return fold_convert_loc (loc
, type
,
9294 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9295 fold_convert_loc (loc
, sizetype
,
9297 fold_convert_loc (loc
, sizetype
,
9303 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9305 /* X + (X / CST) * -CST is X % CST. */
9306 if (TREE_CODE (arg1
) == MULT_EXPR
9307 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9308 && operand_equal_p (arg0
,
9309 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9311 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9312 tree cst1
= TREE_OPERAND (arg1
, 1);
9313 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9315 if (sum
&& integer_zerop (sum
))
9316 return fold_convert_loc (loc
, type
,
9317 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9318 TREE_TYPE (arg0
), arg0
,
9323 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9324 one. Make sure the type is not saturating and has the signedness of
9325 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9326 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9327 if ((TREE_CODE (arg0
) == MULT_EXPR
9328 || TREE_CODE (arg1
) == MULT_EXPR
)
9329 && !TYPE_SATURATING (type
)
9330 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9331 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9332 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9334 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9339 if (! FLOAT_TYPE_P (type
))
9341 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9342 (plus (plus (mult) (mult)) (foo)) so that we can
9343 take advantage of the factoring cases below. */
9344 if (ANY_INTEGRAL_TYPE_P (type
)
9345 && TYPE_OVERFLOW_WRAPS (type
)
9346 && (((TREE_CODE (arg0
) == PLUS_EXPR
9347 || TREE_CODE (arg0
) == MINUS_EXPR
)
9348 && TREE_CODE (arg1
) == MULT_EXPR
)
9349 || ((TREE_CODE (arg1
) == PLUS_EXPR
9350 || TREE_CODE (arg1
) == MINUS_EXPR
)
9351 && TREE_CODE (arg0
) == MULT_EXPR
)))
9353 tree parg0
, parg1
, parg
, marg
;
9354 enum tree_code pcode
;
9356 if (TREE_CODE (arg1
) == MULT_EXPR
)
9357 parg
= arg0
, marg
= arg1
;
9359 parg
= arg1
, marg
= arg0
;
9360 pcode
= TREE_CODE (parg
);
9361 parg0
= TREE_OPERAND (parg
, 0);
9362 parg1
= TREE_OPERAND (parg
, 1);
9366 if (TREE_CODE (parg0
) == MULT_EXPR
9367 && TREE_CODE (parg1
) != MULT_EXPR
)
9368 return fold_build2_loc (loc
, pcode
, type
,
9369 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9370 fold_convert_loc (loc
, type
,
9372 fold_convert_loc (loc
, type
,
9374 fold_convert_loc (loc
, type
, parg1
));
9375 if (TREE_CODE (parg0
) != MULT_EXPR
9376 && TREE_CODE (parg1
) == MULT_EXPR
)
9378 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9379 fold_convert_loc (loc
, type
, parg0
),
9380 fold_build2_loc (loc
, pcode
, type
,
9381 fold_convert_loc (loc
, type
, marg
),
9382 fold_convert_loc (loc
, type
,
9388 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9389 to __complex__ ( x, y ). This is not the same for SNaNs or
9390 if signed zeros are involved. */
9391 if (!HONOR_SNANS (element_mode (arg0
))
9392 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9393 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9395 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9396 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9397 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9398 bool arg0rz
= false, arg0iz
= false;
9399 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9400 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9402 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9403 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9404 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9406 tree rp
= arg1r
? arg1r
9407 : build1 (REALPART_EXPR
, rtype
, arg1
);
9408 tree ip
= arg0i
? arg0i
9409 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9410 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9412 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9414 tree rp
= arg0r
? arg0r
9415 : build1 (REALPART_EXPR
, rtype
, arg0
);
9416 tree ip
= arg1i
? arg1i
9417 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9418 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9423 if (flag_unsafe_math_optimizations
9424 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9425 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9426 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9429 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9430 We associate floats only if the user has specified
9431 -fassociative-math. */
9432 if (flag_associative_math
9433 && TREE_CODE (arg1
) == PLUS_EXPR
9434 && TREE_CODE (arg0
) != MULT_EXPR
)
9436 tree tree10
= TREE_OPERAND (arg1
, 0);
9437 tree tree11
= TREE_OPERAND (arg1
, 1);
9438 if (TREE_CODE (tree11
) == MULT_EXPR
9439 && TREE_CODE (tree10
) == MULT_EXPR
)
9442 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9443 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9446 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9447 We associate floats only if the user has specified
9448 -fassociative-math. */
9449 if (flag_associative_math
9450 && TREE_CODE (arg0
) == PLUS_EXPR
9451 && TREE_CODE (arg1
) != MULT_EXPR
)
9453 tree tree00
= TREE_OPERAND (arg0
, 0);
9454 tree tree01
= TREE_OPERAND (arg0
, 1);
9455 if (TREE_CODE (tree01
) == MULT_EXPR
9456 && TREE_CODE (tree00
) == MULT_EXPR
)
9459 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9460 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9466 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9467 is a rotate of A by C1 bits. */
9468 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9469 is a rotate of A by B bits. */
9471 enum tree_code code0
, code1
;
9473 code0
= TREE_CODE (arg0
);
9474 code1
= TREE_CODE (arg1
);
9475 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9476 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9477 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9478 TREE_OPERAND (arg1
, 0), 0)
9479 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9480 TYPE_UNSIGNED (rtype
))
9481 /* Only create rotates in complete modes. Other cases are not
9482 expanded properly. */
9483 && (element_precision (rtype
)
9484 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9486 tree tree01
, tree11
;
9487 enum tree_code code01
, code11
;
9489 tree01
= TREE_OPERAND (arg0
, 1);
9490 tree11
= TREE_OPERAND (arg1
, 1);
9491 STRIP_NOPS (tree01
);
9492 STRIP_NOPS (tree11
);
9493 code01
= TREE_CODE (tree01
);
9494 code11
= TREE_CODE (tree11
);
9495 if (code01
== INTEGER_CST
9496 && code11
== INTEGER_CST
9497 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9498 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9500 tem
= build2_loc (loc
, LROTATE_EXPR
,
9501 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9502 TREE_OPERAND (arg0
, 0),
9503 code0
== LSHIFT_EXPR
9504 ? TREE_OPERAND (arg0
, 1)
9505 : TREE_OPERAND (arg1
, 1));
9506 return fold_convert_loc (loc
, type
, tem
);
9508 else if (code11
== MINUS_EXPR
)
9510 tree tree110
, tree111
;
9511 tree110
= TREE_OPERAND (tree11
, 0);
9512 tree111
= TREE_OPERAND (tree11
, 1);
9513 STRIP_NOPS (tree110
);
9514 STRIP_NOPS (tree111
);
9515 if (TREE_CODE (tree110
) == INTEGER_CST
9516 && 0 == compare_tree_int (tree110
,
9518 (TREE_TYPE (TREE_OPERAND
9520 && operand_equal_p (tree01
, tree111
, 0))
9522 fold_convert_loc (loc
, type
,
9523 build2 ((code0
== LSHIFT_EXPR
9526 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9527 TREE_OPERAND (arg0
, 0),
9528 TREE_OPERAND (arg0
, 1)));
9530 else if (code01
== MINUS_EXPR
)
9532 tree tree010
, tree011
;
9533 tree010
= TREE_OPERAND (tree01
, 0);
9534 tree011
= TREE_OPERAND (tree01
, 1);
9535 STRIP_NOPS (tree010
);
9536 STRIP_NOPS (tree011
);
9537 if (TREE_CODE (tree010
) == INTEGER_CST
9538 && 0 == compare_tree_int (tree010
,
9540 (TREE_TYPE (TREE_OPERAND
9542 && operand_equal_p (tree11
, tree011
, 0))
9543 return fold_convert_loc
9545 build2 ((code0
!= LSHIFT_EXPR
9548 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9549 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9555 /* In most languages, can't associate operations on floats through
9556 parentheses. Rather than remember where the parentheses were, we
9557 don't associate floats at all, unless the user has specified
9559 And, we need to make sure type is not saturating. */
9561 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9562 && !TYPE_SATURATING (type
))
9564 tree var0
, con0
, lit0
, minus_lit0
;
9565 tree var1
, con1
, lit1
, minus_lit1
;
9569 /* Split both trees into variables, constants, and literals. Then
9570 associate each group together, the constants with literals,
9571 then the result with variables. This increases the chances of
9572 literals being recombined later and of generating relocatable
9573 expressions for the sum of a constant and literal. */
9574 var0
= split_tree (loc
, arg0
, type
, code
,
9575 &con0
, &lit0
, &minus_lit0
, 0);
9576 var1
= split_tree (loc
, arg1
, type
, code
,
9577 &con1
, &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9579 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9580 if (code
== MINUS_EXPR
)
9583 /* With undefined overflow prefer doing association in a type
9584 which wraps on overflow, if that is one of the operand types. */
9585 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9586 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9588 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9589 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9590 atype
= TREE_TYPE (arg0
);
9591 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9592 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9593 atype
= TREE_TYPE (arg1
);
9594 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9597 /* With undefined overflow we can only associate constants with one
9598 variable, and constants whose association doesn't overflow. */
9599 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9600 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9606 bool one_neg
= false;
9608 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9610 tmp0
= TREE_OPERAND (tmp0
, 0);
9613 if (CONVERT_EXPR_P (tmp0
)
9614 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9615 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9616 <= TYPE_PRECISION (atype
)))
9617 tmp0
= TREE_OPERAND (tmp0
, 0);
9618 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9620 tmp1
= TREE_OPERAND (tmp1
, 0);
9623 if (CONVERT_EXPR_P (tmp1
)
9624 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9625 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9626 <= TYPE_PRECISION (atype
)))
9627 tmp1
= TREE_OPERAND (tmp1
, 0);
9628 /* The only case we can still associate with two variables
9629 is if they cancel out. */
9631 || !operand_equal_p (tmp0
, tmp1
, 0))
9636 /* Only do something if we found more than two objects. Otherwise,
9637 nothing has changed and we risk infinite recursion. */
9639 && (2 < ((var0
!= 0) + (var1
!= 0)
9640 + (con0
!= 0) + (con1
!= 0)
9641 + (lit0
!= 0) + (lit1
!= 0)
9642 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9644 bool any_overflows
= false;
9645 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
9646 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
9647 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
9648 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
9649 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9650 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9651 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9652 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9655 /* Preserve the MINUS_EXPR if the negative part of the literal is
9656 greater than the positive part. Otherwise, the multiplicative
9657 folding code (i.e extract_muldiv) may be fooled in case
9658 unsigned constants are subtracted, like in the following
9659 example: ((X*2 + 4) - 8U)/2. */
9660 if (minus_lit0
&& lit0
)
9662 if (TREE_CODE (lit0
) == INTEGER_CST
9663 && TREE_CODE (minus_lit0
) == INTEGER_CST
9664 && tree_int_cst_lt (lit0
, minus_lit0
))
9666 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9672 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9678 /* Don't introduce overflows through reassociation. */
9680 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
9681 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
9688 fold_convert_loc (loc
, type
,
9689 associate_trees (loc
, var0
, minus_lit0
,
9690 MINUS_EXPR
, atype
));
9693 con0
= associate_trees (loc
, con0
, minus_lit0
,
9696 fold_convert_loc (loc
, type
,
9697 associate_trees (loc
, var0
, con0
,
9702 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9704 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9712 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9713 if (TREE_CODE (arg0
) == NEGATE_EXPR
9714 && negate_expr_p (op1
))
9715 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9717 fold_convert_loc (loc
, type
,
9718 TREE_OPERAND (arg0
, 0)));
9720 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9721 __complex__ ( x, -y ). This is not the same for SNaNs or if
9722 signed zeros are involved. */
9723 if (!HONOR_SNANS (element_mode (arg0
))
9724 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9725 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9727 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9728 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9729 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9730 bool arg0rz
= false, arg0iz
= false;
9731 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9732 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9734 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9735 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9736 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9738 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9740 : build1 (REALPART_EXPR
, rtype
, arg1
));
9741 tree ip
= arg0i
? arg0i
9742 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9743 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9745 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9747 tree rp
= arg0r
? arg0r
9748 : build1 (REALPART_EXPR
, rtype
, arg0
);
9749 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9751 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9752 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9757 /* A - B -> A + (-B) if B is easily negatable. */
9758 if (negate_expr_p (op1
)
9759 && ! TYPE_OVERFLOW_SANITIZED (type
)
9760 && ((FLOAT_TYPE_P (type
)
9761 /* Avoid this transformation if B is a positive REAL_CST. */
9762 && (TREE_CODE (op1
) != REAL_CST
9763 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9764 || INTEGRAL_TYPE_P (type
)))
9765 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9766 fold_convert_loc (loc
, type
, arg0
),
9769 /* Fold &a[i] - &a[j] to i-j. */
9770 if (TREE_CODE (arg0
) == ADDR_EXPR
9771 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9772 && TREE_CODE (arg1
) == ADDR_EXPR
9773 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9775 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9776 TREE_OPERAND (arg0
, 0),
9777 TREE_OPERAND (arg1
, 0));
9782 if (FLOAT_TYPE_P (type
)
9783 && flag_unsafe_math_optimizations
9784 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9785 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9786 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9789 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9790 one. Make sure the type is not saturating and has the signedness of
9791 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9792 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9793 if ((TREE_CODE (arg0
) == MULT_EXPR
9794 || TREE_CODE (arg1
) == MULT_EXPR
)
9795 && !TYPE_SATURATING (type
)
9796 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9797 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9798 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9800 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9808 if (! FLOAT_TYPE_P (type
))
9810 /* Transform x * -C into -x * C if x is easily negatable. */
9811 if (TREE_CODE (op1
) == INTEGER_CST
9812 && tree_int_cst_sgn (op1
) == -1
9813 && negate_expr_p (op0
)
9814 && (tem
= negate_expr (op1
)) != op1
9815 && ! TREE_OVERFLOW (tem
))
9816 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9817 fold_convert_loc (loc
, type
,
9818 negate_expr (op0
)), tem
);
9820 strict_overflow_p
= false;
9821 if (TREE_CODE (arg1
) == INTEGER_CST
9822 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9823 &strict_overflow_p
)))
9825 if (strict_overflow_p
)
9826 fold_overflow_warning (("assuming signed overflow does not "
9827 "occur when simplifying "
9829 WARN_STRICT_OVERFLOW_MISC
);
9830 return fold_convert_loc (loc
, type
, tem
);
9833 /* Optimize z * conj(z) for integer complex numbers. */
9834 if (TREE_CODE (arg0
) == CONJ_EXPR
9835 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9836 return fold_mult_zconjz (loc
, type
, arg1
);
9837 if (TREE_CODE (arg1
) == CONJ_EXPR
9838 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9839 return fold_mult_zconjz (loc
, type
, arg0
);
9843 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9844 This is not the same for NaNs or if signed zeros are
9846 if (!HONOR_NANS (arg0
)
9847 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9848 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9849 && TREE_CODE (arg1
) == COMPLEX_CST
9850 && real_zerop (TREE_REALPART (arg1
)))
9852 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9853 if (real_onep (TREE_IMAGPART (arg1
)))
9855 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9856 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9858 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9859 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9861 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9862 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9863 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9867 /* Optimize z * conj(z) for floating point complex numbers.
9868 Guarded by flag_unsafe_math_optimizations as non-finite
9869 imaginary components don't produce scalar results. */
9870 if (flag_unsafe_math_optimizations
9871 && TREE_CODE (arg0
) == CONJ_EXPR
9872 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9873 return fold_mult_zconjz (loc
, type
, arg1
);
9874 if (flag_unsafe_math_optimizations
9875 && TREE_CODE (arg1
) == CONJ_EXPR
9876 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9877 return fold_mult_zconjz (loc
, type
, arg0
);
9882 /* Canonicalize (X & C1) | C2. */
9883 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9884 && TREE_CODE (arg1
) == INTEGER_CST
9885 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9887 int width
= TYPE_PRECISION (type
), w
;
9888 wide_int c1
= TREE_OPERAND (arg0
, 1);
9891 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9892 if ((c1
& c2
) == c1
)
9893 return omit_one_operand_loc (loc
, type
, arg1
,
9894 TREE_OPERAND (arg0
, 0));
9896 wide_int msk
= wi::mask (width
, false,
9897 TYPE_PRECISION (TREE_TYPE (arg1
)));
9899 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9900 if (msk
.and_not (c1
| c2
) == 0)
9901 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9902 TREE_OPERAND (arg0
, 0), arg1
);
9904 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9905 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9906 mode which allows further optimizations. */
9909 wide_int c3
= c1
.and_not (c2
);
9910 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
9912 wide_int mask
= wi::mask (w
, false,
9913 TYPE_PRECISION (type
));
9914 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
9923 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9924 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
,
9925 wide_int_to_tree (type
, c3
));
9926 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
, tem
, arg1
);
9930 /* See if this can be simplified into a rotate first. If that
9931 is unsuccessful continue in the association code. */
9935 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9936 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9937 && INTEGRAL_TYPE_P (type
)
9938 && integer_onep (TREE_OPERAND (arg0
, 1))
9939 && integer_onep (arg1
))
9940 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
9941 build_zero_cst (TREE_TYPE (arg0
)));
9943 /* See if this can be simplified into a rotate first. If that
9944 is unsuccessful continue in the association code. */
9948 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9949 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9950 && INTEGRAL_TYPE_P (type
)
9951 && integer_onep (TREE_OPERAND (arg0
, 1))
9952 && integer_onep (arg1
))
9955 tem
= TREE_OPERAND (arg0
, 0);
9956 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9957 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9959 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9960 build_zero_cst (TREE_TYPE (tem
)));
9962 /* Fold ~X & 1 as (X & 1) == 0. */
9963 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9964 && INTEGRAL_TYPE_P (type
)
9965 && integer_onep (arg1
))
9968 tem
= TREE_OPERAND (arg0
, 0);
9969 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9970 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9972 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9973 build_zero_cst (TREE_TYPE (tem
)));
9975 /* Fold !X & 1 as X == 0. */
9976 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
9977 && integer_onep (arg1
))
9979 tem
= TREE_OPERAND (arg0
, 0);
9980 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
9981 build_zero_cst (TREE_TYPE (tem
)));
9984 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9985 multiple of 1 << CST. */
9986 if (TREE_CODE (arg1
) == INTEGER_CST
)
9988 wide_int cst1
= arg1
;
9989 wide_int ncst1
= -cst1
;
9990 if ((cst1
& ncst1
) == ncst1
9991 && multiple_of_p (type
, arg0
,
9992 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
9993 return fold_convert_loc (loc
, type
, arg0
);
9996 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9998 if (TREE_CODE (arg1
) == INTEGER_CST
9999 && TREE_CODE (arg0
) == MULT_EXPR
10000 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10002 wide_int warg1
= arg1
;
10003 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
10006 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10008 else if (masked
!= warg1
)
10010 /* Avoid the transform if arg1 is a mask of some
10011 mode which allows further optimizations. */
10012 int pop
= wi::popcount (warg1
);
10013 if (!(pop
>= BITS_PER_UNIT
10015 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10016 return fold_build2_loc (loc
, code
, type
, op0
,
10017 wide_int_to_tree (type
, masked
));
10021 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10022 ((A & N) + B) & M -> (A + B) & M
10023 Similarly if (N & M) == 0,
10024 ((A | N) + B) & M -> (A + B) & M
10025 and for - instead of + (or unary - instead of +)
10026 and/or ^ instead of |.
10027 If B is constant and (B & M) == 0, fold into A & M. */
10028 if (TREE_CODE (arg1
) == INTEGER_CST
)
10030 wide_int cst1
= arg1
;
10031 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10032 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10033 && (TREE_CODE (arg0
) == PLUS_EXPR
10034 || TREE_CODE (arg0
) == MINUS_EXPR
10035 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10036 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10037 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10043 /* Now we know that arg0 is (C + D) or (C - D) or
10044 -C and arg1 (M) is == (1LL << cst) - 1.
10045 Store C into PMOP[0] and D into PMOP[1]. */
10046 pmop
[0] = TREE_OPERAND (arg0
, 0);
10048 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10050 pmop
[1] = TREE_OPERAND (arg0
, 1);
10054 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10057 for (; which
>= 0; which
--)
10058 switch (TREE_CODE (pmop
[which
]))
10063 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10066 cst0
= TREE_OPERAND (pmop
[which
], 1);
10068 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10073 else if (cst0
!= 0)
10075 /* If C or D is of the form (A & N) where
10076 (N & M) == M, or of the form (A | N) or
10077 (A ^ N) where (N & M) == 0, replace it with A. */
10078 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10081 /* If C or D is a N where (N & M) == 0, it can be
10082 omitted (assumed 0). */
10083 if ((TREE_CODE (arg0
) == PLUS_EXPR
10084 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10085 && (cst1
& pmop
[which
]) == 0)
10086 pmop
[which
] = NULL
;
10092 /* Only build anything new if we optimized one or both arguments
10094 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10095 || (TREE_CODE (arg0
) != NEGATE_EXPR
10096 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10098 tree utype
= TREE_TYPE (arg0
);
10099 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10101 /* Perform the operations in a type that has defined
10102 overflow behavior. */
10103 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10104 if (pmop
[0] != NULL
)
10105 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10106 if (pmop
[1] != NULL
)
10107 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10110 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10111 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10112 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10114 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10115 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10117 else if (pmop
[0] != NULL
)
10119 else if (pmop
[1] != NULL
)
10122 return build_int_cst (type
, 0);
10124 else if (pmop
[0] == NULL
)
10125 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10127 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10129 /* TEM is now the new binary +, - or unary - replacement. */
10130 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10131 fold_convert_loc (loc
, utype
, arg1
));
10132 return fold_convert_loc (loc
, type
, tem
);
10137 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10138 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10139 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10141 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10143 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10146 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10152 /* Don't touch a floating-point divide by zero unless the mode
10153 of the constant can represent infinity. */
10154 if (TREE_CODE (arg1
) == REAL_CST
10155 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10156 && real_zerop (arg1
))
10159 /* (-A) / (-B) -> A / B */
10160 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10161 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10162 TREE_OPERAND (arg0
, 0),
10163 negate_expr (arg1
));
10164 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10165 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10166 negate_expr (arg0
),
10167 TREE_OPERAND (arg1
, 0));
10170 case TRUNC_DIV_EXPR
:
10173 case FLOOR_DIV_EXPR
:
10174 /* Simplify A / (B << N) where A and B are positive and B is
10175 a power of 2, to A >> (N + log2(B)). */
10176 strict_overflow_p
= false;
10177 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10178 && (TYPE_UNSIGNED (type
)
10179 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10181 tree sval
= TREE_OPERAND (arg1
, 0);
10182 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10184 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10185 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10186 wi::exact_log2 (sval
));
10188 if (strict_overflow_p
)
10189 fold_overflow_warning (("assuming signed overflow does not "
10190 "occur when simplifying A / (B << N)"),
10191 WARN_STRICT_OVERFLOW_MISC
);
10193 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10195 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10196 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10202 case ROUND_DIV_EXPR
:
10203 case CEIL_DIV_EXPR
:
10204 case EXACT_DIV_EXPR
:
10205 if (integer_zerop (arg1
))
10208 /* Convert -A / -B to A / B when the type is signed and overflow is
10210 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10211 && TREE_CODE (op0
) == NEGATE_EXPR
10212 && negate_expr_p (op1
))
10214 if (INTEGRAL_TYPE_P (type
))
10215 fold_overflow_warning (("assuming signed overflow does not occur "
10216 "when distributing negation across "
10218 WARN_STRICT_OVERFLOW_MISC
);
10219 return fold_build2_loc (loc
, code
, type
,
10220 fold_convert_loc (loc
, type
,
10221 TREE_OPERAND (arg0
, 0)),
10222 negate_expr (op1
));
10224 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10225 && TREE_CODE (arg1
) == NEGATE_EXPR
10226 && negate_expr_p (op0
))
10228 if (INTEGRAL_TYPE_P (type
))
10229 fold_overflow_warning (("assuming signed overflow does not occur "
10230 "when distributing negation across "
10232 WARN_STRICT_OVERFLOW_MISC
);
10233 return fold_build2_loc (loc
, code
, type
,
10235 fold_convert_loc (loc
, type
,
10236 TREE_OPERAND (arg1
, 0)));
10239 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10240 operation, EXACT_DIV_EXPR.
10242 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10243 At one time others generated faster code, it's not clear if they do
10244 after the last round to changes to the DIV code in expmed.c. */
10245 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10246 && multiple_of_p (type
, arg0
, arg1
))
10247 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10248 fold_convert (type
, arg0
),
10249 fold_convert (type
, arg1
));
10251 strict_overflow_p
= false;
10252 if (TREE_CODE (arg1
) == INTEGER_CST
10253 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10254 &strict_overflow_p
)))
10256 if (strict_overflow_p
)
10257 fold_overflow_warning (("assuming signed overflow does not occur "
10258 "when simplifying division"),
10259 WARN_STRICT_OVERFLOW_MISC
);
10260 return fold_convert_loc (loc
, type
, tem
);
10265 case CEIL_MOD_EXPR
:
10266 case FLOOR_MOD_EXPR
:
10267 case ROUND_MOD_EXPR
:
10268 case TRUNC_MOD_EXPR
:
10269 strict_overflow_p
= false;
10270 if (TREE_CODE (arg1
) == INTEGER_CST
10271 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10272 &strict_overflow_p
)))
10274 if (strict_overflow_p
)
10275 fold_overflow_warning (("assuming signed overflow does not occur "
10276 "when simplifying modulus"),
10277 WARN_STRICT_OVERFLOW_MISC
);
10278 return fold_convert_loc (loc
, type
, tem
);
10287 /* Since negative shift count is not well-defined,
10288 don't try to compute it in the compiler. */
10289 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10292 prec
= element_precision (type
);
10294 /* If we have a rotate of a bit operation with the rotate count and
10295 the second operand of the bit operation both constant,
10296 permute the two operations. */
10297 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10298 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10299 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10300 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10301 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10303 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10304 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10305 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10306 fold_build2_loc (loc
, code
, type
,
10308 fold_build2_loc (loc
, code
, type
,
10312 /* Two consecutive rotates adding up to the some integer
10313 multiple of the precision of the type can be ignored. */
10314 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10315 && TREE_CODE (arg0
) == RROTATE_EXPR
10316 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10317 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10319 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10327 case TRUTH_ANDIF_EXPR
:
10328 /* Note that the operands of this must be ints
10329 and their values must be 0 or 1.
10330 ("true" is a fixed value perhaps depending on the language.) */
10331 /* If first arg is constant zero, return it. */
10332 if (integer_zerop (arg0
))
10333 return fold_convert_loc (loc
, type
, arg0
);
10335 case TRUTH_AND_EXPR
:
10336 /* If either arg is constant true, drop it. */
10337 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10338 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10339 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10340 /* Preserve sequence points. */
10341 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10342 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10343 /* If second arg is constant zero, result is zero, but first arg
10344 must be evaluated. */
10345 if (integer_zerop (arg1
))
10346 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10347 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10348 case will be handled here. */
10349 if (integer_zerop (arg0
))
10350 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10352 /* !X && X is always false. */
10353 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10354 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10355 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10356 /* X && !X is always false. */
10357 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10358 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10359 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10361 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10362 means A >= Y && A != MAX, but in this case we know that
10365 if (!TREE_SIDE_EFFECTS (arg0
)
10366 && !TREE_SIDE_EFFECTS (arg1
))
10368 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10369 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10370 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10372 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10373 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10374 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10377 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10383 case TRUTH_ORIF_EXPR
:
10384 /* Note that the operands of this must be ints
10385 and their values must be 0 or true.
10386 ("true" is a fixed value perhaps depending on the language.) */
10387 /* If first arg is constant true, return it. */
10388 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10389 return fold_convert_loc (loc
, type
, arg0
);
10391 case TRUTH_OR_EXPR
:
10392 /* If either arg is constant zero, drop it. */
10393 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10394 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10395 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10396 /* Preserve sequence points. */
10397 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10398 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10399 /* If second arg is constant true, result is true, but we must
10400 evaluate first arg. */
10401 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10402 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10403 /* Likewise for first arg, but note this only occurs here for
10405 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10406 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10408 /* !X || X is always true. */
10409 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10410 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10411 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10412 /* X || !X is always true. */
10413 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10414 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10415 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10417 /* (X && !Y) || (!X && Y) is X ^ Y */
10418 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10419 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10421 tree a0
, a1
, l0
, l1
, n0
, n1
;
10423 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10424 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10426 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10427 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10429 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10430 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10432 if ((operand_equal_p (n0
, a0
, 0)
10433 && operand_equal_p (n1
, a1
, 0))
10434 || (operand_equal_p (n0
, a1
, 0)
10435 && operand_equal_p (n1
, a0
, 0)))
10436 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10439 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10445 case TRUTH_XOR_EXPR
:
10446 /* If the second arg is constant zero, drop it. */
10447 if (integer_zerop (arg1
))
10448 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10449 /* If the second arg is constant true, this is a logical inversion. */
10450 if (integer_onep (arg1
))
10452 tem
= invert_truthvalue_loc (loc
, arg0
);
10453 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10455 /* Identical arguments cancel to zero. */
10456 if (operand_equal_p (arg0
, arg1
, 0))
10457 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10459 /* !X ^ X is always true. */
10460 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10461 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10462 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10464 /* X ^ !X is always true. */
10465 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10466 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10467 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10476 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10477 if (tem
!= NULL_TREE
)
10480 /* bool_var != 1 becomes !bool_var. */
10481 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10482 && code
== NE_EXPR
)
10483 return fold_convert_loc (loc
, type
,
10484 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10485 TREE_TYPE (arg0
), arg0
));
10487 /* bool_var == 0 becomes !bool_var. */
10488 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10489 && code
== EQ_EXPR
)
10490 return fold_convert_loc (loc
, type
,
10491 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10492 TREE_TYPE (arg0
), arg0
));
10494 /* !exp != 0 becomes !exp */
10495 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10496 && code
== NE_EXPR
)
10497 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10499 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10500 if ((TREE_CODE (arg0
) == PLUS_EXPR
10501 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10502 || TREE_CODE (arg0
) == MINUS_EXPR
)
10503 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10506 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10507 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10509 tree val
= TREE_OPERAND (arg0
, 1);
10510 val
= fold_build2_loc (loc
, code
, type
, val
,
10511 build_int_cst (TREE_TYPE (val
), 0));
10512 return omit_two_operands_loc (loc
, type
, val
,
10513 TREE_OPERAND (arg0
, 0), arg1
);
10516 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10517 if ((TREE_CODE (arg1
) == PLUS_EXPR
10518 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
10519 || TREE_CODE (arg1
) == MINUS_EXPR
)
10520 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10523 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10524 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
10526 tree val
= TREE_OPERAND (arg1
, 1);
10527 val
= fold_build2_loc (loc
, code
, type
, val
,
10528 build_int_cst (TREE_TYPE (val
), 0));
10529 return omit_two_operands_loc (loc
, type
, val
,
10530 TREE_OPERAND (arg1
, 0), arg0
);
10533 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10534 if (TREE_CODE (arg0
) == MINUS_EXPR
10535 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
10536 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10539 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
10540 return omit_two_operands_loc (loc
, type
,
10542 ? boolean_true_node
: boolean_false_node
,
10543 TREE_OPERAND (arg0
, 1), arg1
);
10545 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10546 if (TREE_CODE (arg1
) == MINUS_EXPR
10547 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == INTEGER_CST
10548 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10551 && wi::extract_uhwi (TREE_OPERAND (arg1
, 0), 0, 1) == 1)
10552 return omit_two_operands_loc (loc
, type
,
10554 ? boolean_true_node
: boolean_false_node
,
10555 TREE_OPERAND (arg1
, 1), arg0
);
10557 /* If this is an EQ or NE comparison with zero and ARG0 is
10558 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10559 two operations, but the latter can be done in one less insn
10560 on machines that have only two-operand insns or on which a
10561 constant cannot be the first operand. */
10562 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10563 && integer_zerop (arg1
))
10565 tree arg00
= TREE_OPERAND (arg0
, 0);
10566 tree arg01
= TREE_OPERAND (arg0
, 1);
10567 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10568 && integer_onep (TREE_OPERAND (arg00
, 0)))
10570 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10571 arg01
, TREE_OPERAND (arg00
, 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
),
10578 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10579 && integer_onep (TREE_OPERAND (arg01
, 0)))
10581 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10582 arg00
, TREE_OPERAND (arg01
, 1));
10583 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10584 build_int_cst (TREE_TYPE (arg0
), 1));
10585 return fold_build2_loc (loc
, code
, type
,
10586 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10591 /* If this is an NE or EQ comparison of zero against the result of a
10592 signed MOD operation whose second operand is a power of 2, make
10593 the MOD operation unsigned since it is simpler and equivalent. */
10594 if (integer_zerop (arg1
)
10595 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10596 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10597 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10598 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10599 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10600 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10602 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10603 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10604 fold_convert_loc (loc
, newtype
,
10605 TREE_OPERAND (arg0
, 0)),
10606 fold_convert_loc (loc
, newtype
,
10607 TREE_OPERAND (arg0
, 1)));
10609 return fold_build2_loc (loc
, code
, type
, newmod
,
10610 fold_convert_loc (loc
, newtype
, arg1
));
10613 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10614 C1 is a valid shift constant, and C2 is a power of two, i.e.
10616 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10617 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10618 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10620 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10621 && integer_zerop (arg1
))
10623 tree itype
= TREE_TYPE (arg0
);
10624 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10625 prec
= TYPE_PRECISION (itype
);
10627 /* Check for a valid shift count. */
10628 if (wi::ltu_p (arg001
, prec
))
10630 tree arg01
= TREE_OPERAND (arg0
, 1);
10631 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10632 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10633 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10634 can be rewritten as (X & (C2 << C1)) != 0. */
10635 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10637 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10638 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10639 return fold_build2_loc (loc
, code
, type
, tem
,
10640 fold_convert_loc (loc
, itype
, arg1
));
10642 /* Otherwise, for signed (arithmetic) shifts,
10643 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10644 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10645 else if (!TYPE_UNSIGNED (itype
))
10646 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10647 arg000
, build_int_cst (itype
, 0));
10648 /* Otherwise, of unsigned (logical) shifts,
10649 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10650 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10652 return omit_one_operand_loc (loc
, type
,
10653 code
== EQ_EXPR
? integer_one_node
10654 : integer_zero_node
,
10659 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10660 Similarly for NE_EXPR. */
10661 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10662 && TREE_CODE (arg1
) == INTEGER_CST
10663 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10665 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
10666 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
10667 TREE_OPERAND (arg0
, 1));
10669 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10670 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
10672 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
10673 if (integer_nonzerop (dandnotc
))
10674 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
10677 /* If this is a comparison of a field, we may be able to simplify it. */
10678 if ((TREE_CODE (arg0
) == COMPONENT_REF
10679 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10680 /* Handle the constant case even without -O
10681 to make sure the warnings are given. */
10682 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10684 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10689 /* Optimize comparisons of strlen vs zero to a compare of the
10690 first character of the string vs zero. To wit,
10691 strlen(ptr) == 0 => *ptr == 0
10692 strlen(ptr) != 0 => *ptr != 0
10693 Other cases should reduce to one of these two (or a constant)
10694 due to the return value of strlen being unsigned. */
10695 if (TREE_CODE (arg0
) == CALL_EXPR
10696 && integer_zerop (arg1
))
10698 tree fndecl
= get_callee_fndecl (arg0
);
10701 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10702 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10703 && call_expr_nargs (arg0
) == 1
10704 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10706 tree iref
= build_fold_indirect_ref_loc (loc
,
10707 CALL_EXPR_ARG (arg0
, 0));
10708 return fold_build2_loc (loc
, code
, type
, iref
,
10709 build_int_cst (TREE_TYPE (iref
), 0));
10713 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10714 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10715 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10716 && integer_zerop (arg1
)
10717 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10719 tree arg00
= TREE_OPERAND (arg0
, 0);
10720 tree arg01
= TREE_OPERAND (arg0
, 1);
10721 tree itype
= TREE_TYPE (arg00
);
10722 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10724 if (TYPE_UNSIGNED (itype
))
10726 itype
= signed_type_for (itype
);
10727 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10729 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10730 type
, arg00
, build_zero_cst (itype
));
10734 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10735 (X & C) == 0 when C is a single bit. */
10736 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10737 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10738 && integer_zerop (arg1
)
10739 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10741 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10742 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10743 TREE_OPERAND (arg0
, 1));
10744 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10746 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10750 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10751 constant C is a power of two, i.e. a single bit. */
10752 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10753 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10754 && integer_zerop (arg1
)
10755 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10756 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10757 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10759 tree arg00
= TREE_OPERAND (arg0
, 0);
10760 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10761 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10764 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10765 when is C is a power of two, i.e. a single bit. */
10766 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10767 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10768 && integer_zerop (arg1
)
10769 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10770 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10771 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10773 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10774 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10775 arg000
, TREE_OPERAND (arg0
, 1));
10776 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10777 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10780 if (integer_zerop (arg1
)
10781 && tree_expr_nonzero_p (arg0
))
10783 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10784 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10787 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10788 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10789 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10791 tree arg00
= TREE_OPERAND (arg0
, 0);
10792 tree arg01
= TREE_OPERAND (arg0
, 1);
10793 tree arg10
= TREE_OPERAND (arg1
, 0);
10794 tree arg11
= TREE_OPERAND (arg1
, 1);
10795 tree itype
= TREE_TYPE (arg0
);
10797 if (operand_equal_p (arg01
, arg11
, 0))
10798 return fold_build2_loc (loc
, code
, type
,
10799 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10800 fold_build2_loc (loc
,
10801 BIT_XOR_EXPR
, itype
,
10804 build_zero_cst (itype
));
10806 if (operand_equal_p (arg01
, arg10
, 0))
10807 return fold_build2_loc (loc
, code
, type
,
10808 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10809 fold_build2_loc (loc
,
10810 BIT_XOR_EXPR
, itype
,
10813 build_zero_cst (itype
));
10815 if (operand_equal_p (arg00
, arg11
, 0))
10816 return fold_build2_loc (loc
, code
, type
,
10817 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10818 fold_build2_loc (loc
,
10819 BIT_XOR_EXPR
, itype
,
10822 build_zero_cst (itype
));
10824 if (operand_equal_p (arg00
, arg10
, 0))
10825 return fold_build2_loc (loc
, code
, type
,
10826 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10827 fold_build2_loc (loc
,
10828 BIT_XOR_EXPR
, itype
,
10831 build_zero_cst (itype
));
10834 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10835 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10837 tree arg00
= TREE_OPERAND (arg0
, 0);
10838 tree arg01
= TREE_OPERAND (arg0
, 1);
10839 tree arg10
= TREE_OPERAND (arg1
, 0);
10840 tree arg11
= TREE_OPERAND (arg1
, 1);
10841 tree itype
= TREE_TYPE (arg0
);
10843 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10844 operand_equal_p guarantees no side-effects so we don't need
10845 to use omit_one_operand on Z. */
10846 if (operand_equal_p (arg01
, arg11
, 0))
10847 return fold_build2_loc (loc
, code
, type
, arg00
,
10848 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10850 if (operand_equal_p (arg01
, arg10
, 0))
10851 return fold_build2_loc (loc
, code
, type
, arg00
,
10852 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10854 if (operand_equal_p (arg00
, arg11
, 0))
10855 return fold_build2_loc (loc
, code
, type
, arg01
,
10856 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10858 if (operand_equal_p (arg00
, arg10
, 0))
10859 return fold_build2_loc (loc
, code
, type
, arg01
,
10860 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10863 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10864 if (TREE_CODE (arg01
) == INTEGER_CST
10865 && TREE_CODE (arg11
) == INTEGER_CST
)
10867 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10868 fold_convert_loc (loc
, itype
, arg11
));
10869 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10870 return fold_build2_loc (loc
, code
, type
, tem
,
10871 fold_convert_loc (loc
, itype
, arg10
));
10875 /* Attempt to simplify equality/inequality comparisons of complex
10876 values. Only lower the comparison if the result is known or
10877 can be simplified to a single scalar comparison. */
10878 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10879 || TREE_CODE (arg0
) == COMPLEX_CST
)
10880 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10881 || TREE_CODE (arg1
) == COMPLEX_CST
))
10883 tree real0
, imag0
, real1
, imag1
;
10886 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10888 real0
= TREE_OPERAND (arg0
, 0);
10889 imag0
= TREE_OPERAND (arg0
, 1);
10893 real0
= TREE_REALPART (arg0
);
10894 imag0
= TREE_IMAGPART (arg0
);
10897 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
10899 real1
= TREE_OPERAND (arg1
, 0);
10900 imag1
= TREE_OPERAND (arg1
, 1);
10904 real1
= TREE_REALPART (arg1
);
10905 imag1
= TREE_IMAGPART (arg1
);
10908 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
10909 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
10911 if (integer_zerop (rcond
))
10913 if (code
== EQ_EXPR
)
10914 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10916 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
10920 if (code
== NE_EXPR
)
10921 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10923 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
10927 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
10928 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
10930 if (integer_zerop (icond
))
10932 if (code
== EQ_EXPR
)
10933 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10935 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
10939 if (code
== NE_EXPR
)
10940 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10942 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
10953 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10954 if (tem
!= NULL_TREE
)
10957 /* Transform comparisons of the form X +- C CMP X. */
10958 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10959 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10960 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
10961 && !HONOR_SNANS (arg0
))
10962 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10963 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
10965 tree arg01
= TREE_OPERAND (arg0
, 1);
10966 enum tree_code code0
= TREE_CODE (arg0
);
10969 if (TREE_CODE (arg01
) == REAL_CST
)
10970 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
10972 is_positive
= tree_int_cst_sgn (arg01
);
10974 /* (X - c) > X becomes false. */
10975 if (code
== GT_EXPR
10976 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10977 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10979 if (TREE_CODE (arg01
) == INTEGER_CST
10980 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10981 fold_overflow_warning (("assuming signed overflow does not "
10982 "occur when assuming that (X - c) > X "
10983 "is always false"),
10984 WARN_STRICT_OVERFLOW_ALL
);
10985 return constant_boolean_node (0, type
);
10988 /* Likewise (X + c) < X becomes false. */
10989 if (code
== LT_EXPR
10990 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10991 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10993 if (TREE_CODE (arg01
) == INTEGER_CST
10994 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10995 fold_overflow_warning (("assuming signed overflow does not "
10996 "occur when assuming that "
10997 "(X + c) < X is always false"),
10998 WARN_STRICT_OVERFLOW_ALL
);
10999 return constant_boolean_node (0, type
);
11002 /* Convert (X - c) <= X to true. */
11003 if (!HONOR_NANS (arg1
)
11005 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11006 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11008 if (TREE_CODE (arg01
) == INTEGER_CST
11009 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11010 fold_overflow_warning (("assuming signed overflow does not "
11011 "occur when assuming that "
11012 "(X - c) <= X is always true"),
11013 WARN_STRICT_OVERFLOW_ALL
);
11014 return constant_boolean_node (1, type
);
11017 /* Convert (X + c) >= X to true. */
11018 if (!HONOR_NANS (arg1
)
11020 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11021 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11023 if (TREE_CODE (arg01
) == INTEGER_CST
11024 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11025 fold_overflow_warning (("assuming signed overflow does not "
11026 "occur when assuming that "
11027 "(X + c) >= X is always true"),
11028 WARN_STRICT_OVERFLOW_ALL
);
11029 return constant_boolean_node (1, type
);
11032 if (TREE_CODE (arg01
) == INTEGER_CST
)
11034 /* Convert X + c > X and X - c < X to true for integers. */
11035 if (code
== GT_EXPR
11036 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11037 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11039 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11040 fold_overflow_warning (("assuming signed overflow does "
11041 "not occur when assuming that "
11042 "(X + c) > X is always true"),
11043 WARN_STRICT_OVERFLOW_ALL
);
11044 return constant_boolean_node (1, type
);
11047 if (code
== LT_EXPR
11048 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11049 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11051 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11052 fold_overflow_warning (("assuming signed overflow does "
11053 "not occur when assuming that "
11054 "(X - c) < X is always true"),
11055 WARN_STRICT_OVERFLOW_ALL
);
11056 return constant_boolean_node (1, type
);
11059 /* Convert X + c <= X and X - c >= X to false for integers. */
11060 if (code
== LE_EXPR
11061 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11062 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11064 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11065 fold_overflow_warning (("assuming signed overflow does "
11066 "not occur when assuming that "
11067 "(X + c) <= X is always false"),
11068 WARN_STRICT_OVERFLOW_ALL
);
11069 return constant_boolean_node (0, type
);
11072 if (code
== GE_EXPR
11073 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11074 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11076 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11077 fold_overflow_warning (("assuming signed overflow does "
11078 "not occur when assuming that "
11079 "(X - c) >= X is always false"),
11080 WARN_STRICT_OVERFLOW_ALL
);
11081 return constant_boolean_node (0, type
);
11086 /* If we are comparing an ABS_EXPR with a constant, we can
11087 convert all the cases into explicit comparisons, but they may
11088 well not be faster than doing the ABS and one comparison.
11089 But ABS (X) <= C is a range comparison, which becomes a subtraction
11090 and a comparison, and is probably faster. */
11091 if (code
== LE_EXPR
11092 && TREE_CODE (arg1
) == INTEGER_CST
11093 && TREE_CODE (arg0
) == ABS_EXPR
11094 && ! TREE_SIDE_EFFECTS (arg0
)
11095 && (0 != (tem
= negate_expr (arg1
)))
11096 && TREE_CODE (tem
) == INTEGER_CST
11097 && !TREE_OVERFLOW (tem
))
11098 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11099 build2 (GE_EXPR
, type
,
11100 TREE_OPERAND (arg0
, 0), tem
),
11101 build2 (LE_EXPR
, type
,
11102 TREE_OPERAND (arg0
, 0), arg1
));
11104 /* Convert ABS_EXPR<x> >= 0 to true. */
11105 strict_overflow_p
= false;
11106 if (code
== GE_EXPR
11107 && (integer_zerop (arg1
)
11108 || (! HONOR_NANS (arg0
)
11109 && real_zerop (arg1
)))
11110 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11112 if (strict_overflow_p
)
11113 fold_overflow_warning (("assuming signed overflow does not occur "
11114 "when simplifying comparison of "
11115 "absolute value and zero"),
11116 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11117 return omit_one_operand_loc (loc
, type
,
11118 constant_boolean_node (true, type
),
11122 /* Convert ABS_EXPR<x> < 0 to false. */
11123 strict_overflow_p
= false;
11124 if (code
== LT_EXPR
11125 && (integer_zerop (arg1
) || real_zerop (arg1
))
11126 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11128 if (strict_overflow_p
)
11129 fold_overflow_warning (("assuming signed overflow does not occur "
11130 "when simplifying comparison of "
11131 "absolute value and zero"),
11132 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11133 return omit_one_operand_loc (loc
, type
,
11134 constant_boolean_node (false, type
),
11138 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11139 and similarly for >= into !=. */
11140 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11141 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11142 && TREE_CODE (arg1
) == LSHIFT_EXPR
11143 && integer_onep (TREE_OPERAND (arg1
, 0)))
11144 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11145 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11146 TREE_OPERAND (arg1
, 1)),
11147 build_zero_cst (TREE_TYPE (arg0
)));
11149 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11150 otherwise Y might be >= # of bits in X's type and thus e.g.
11151 (unsigned char) (1 << Y) for Y 15 might be 0.
11152 If the cast is widening, then 1 << Y should have unsigned type,
11153 otherwise if Y is number of bits in the signed shift type minus 1,
11154 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11155 31 might be 0xffffffff80000000. */
11156 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11157 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11158 && CONVERT_EXPR_P (arg1
)
11159 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11160 && (element_precision (TREE_TYPE (arg1
))
11161 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11162 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11163 || (element_precision (TREE_TYPE (arg1
))
11164 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11165 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11167 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11168 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11169 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11170 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11171 build_zero_cst (TREE_TYPE (arg0
)));
11176 case UNORDERED_EXPR
:
11184 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11186 tree targ0
= strip_float_extensions (arg0
);
11187 tree targ1
= strip_float_extensions (arg1
);
11188 tree newtype
= TREE_TYPE (targ0
);
11190 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11191 newtype
= TREE_TYPE (targ1
);
11193 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11194 return fold_build2_loc (loc
, code
, type
,
11195 fold_convert_loc (loc
, newtype
, targ0
),
11196 fold_convert_loc (loc
, newtype
, targ1
));
11201 case COMPOUND_EXPR
:
11202 /* When pedantic, a compound expression can be neither an lvalue
11203 nor an integer constant expression. */
11204 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11206 /* Don't let (0, 0) be null pointer constant. */
11207 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11208 : fold_convert_loc (loc
, type
, arg1
);
11209 return pedantic_non_lvalue_loc (loc
, tem
);
11212 /* An ASSERT_EXPR should never be passed to fold_binary. */
11213 gcc_unreachable ();
11217 } /* switch (code) */
11220 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11221 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11225 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11227 switch (TREE_CODE (*tp
))
11233 *walk_subtrees
= 0;
11242 /* Return whether the sub-tree ST contains a label which is accessible from
11243 outside the sub-tree. */
11246 contains_label_p (tree st
)
11249 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11252 /* Fold a ternary expression of code CODE and type TYPE with operands
11253 OP0, OP1, and OP2. Return the folded expression if folding is
11254 successful. Otherwise, return NULL_TREE. */
11257 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11258 tree op0
, tree op1
, tree op2
)
11261 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11262 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11264 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11265 && TREE_CODE_LENGTH (code
) == 3);
11267 /* If this is a commutative operation, and OP0 is a constant, move it
11268 to OP1 to reduce the number of tests below. */
11269 if (commutative_ternary_tree_code (code
)
11270 && tree_swap_operands_p (op0
, op1
))
11271 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11273 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11277 /* Strip any conversions that don't change the mode. This is safe
11278 for every expression, except for a comparison expression because
11279 its signedness is derived from its operands. So, in the latter
11280 case, only strip conversions that don't change the signedness.
11282 Note that this is done as an internal manipulation within the
11283 constant folder, in order to find the simplest representation of
11284 the arguments so that their form can be studied. In any cases,
11285 the appropriate type conversions should be put back in the tree
11286 that will get out of the constant folder. */
11307 case COMPONENT_REF
:
11308 if (TREE_CODE (arg0
) == CONSTRUCTOR
11309 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11311 unsigned HOST_WIDE_INT idx
;
11313 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11320 case VEC_COND_EXPR
:
11321 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11322 so all simple results must be passed through pedantic_non_lvalue. */
11323 if (TREE_CODE (arg0
) == INTEGER_CST
)
11325 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11326 tem
= integer_zerop (arg0
) ? op2
: op1
;
11327 /* Only optimize constant conditions when the selected branch
11328 has the same type as the COND_EXPR. This avoids optimizing
11329 away "c ? x : throw", where the throw has a void type.
11330 Avoid throwing away that operand which contains label. */
11331 if ((!TREE_SIDE_EFFECTS (unused_op
)
11332 || !contains_label_p (unused_op
))
11333 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11334 || VOID_TYPE_P (type
)))
11335 return pedantic_non_lvalue_loc (loc
, tem
);
11338 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11340 if ((TREE_CODE (arg1
) == VECTOR_CST
11341 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11342 && (TREE_CODE (arg2
) == VECTOR_CST
11343 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11345 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
11346 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
11347 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
11348 for (i
= 0; i
< nelts
; i
++)
11350 tree val
= VECTOR_CST_ELT (arg0
, i
);
11351 if (integer_all_onesp (val
))
11353 else if (integer_zerop (val
))
11354 sel
[i
] = nelts
+ i
;
11355 else /* Currently unreachable. */
11358 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11359 if (t
!= NULL_TREE
)
11364 /* If we have A op B ? A : C, we may be able to convert this to a
11365 simpler expression, depending on the operation and the values
11366 of B and C. Signed zeros prevent all of these transformations,
11367 for reasons given above each one.
11369 Also try swapping the arguments and inverting the conditional. */
11370 if (COMPARISON_CLASS_P (arg0
)
11371 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11372 arg1
, TREE_OPERAND (arg0
, 1))
11373 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11375 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11380 if (COMPARISON_CLASS_P (arg0
)
11381 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11383 TREE_OPERAND (arg0
, 1))
11384 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11386 location_t loc0
= expr_location_or (arg0
, loc
);
11387 tem
= fold_invert_truthvalue (loc0
, arg0
);
11388 if (tem
&& COMPARISON_CLASS_P (tem
))
11390 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11396 /* If the second operand is simpler than the third, swap them
11397 since that produces better jump optimization results. */
11398 if (truth_value_p (TREE_CODE (arg0
))
11399 && tree_swap_operands_p (op1
, op2
))
11401 location_t loc0
= expr_location_or (arg0
, loc
);
11402 /* See if this can be inverted. If it can't, possibly because
11403 it was a floating-point inequality comparison, don't do
11405 tem
= fold_invert_truthvalue (loc0
, arg0
);
11407 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11410 /* Convert A ? 1 : 0 to simply A. */
11411 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11412 : (integer_onep (op1
)
11413 && !VECTOR_TYPE_P (type
)))
11414 && integer_zerop (op2
)
11415 /* If we try to convert OP0 to our type, the
11416 call to fold will try to move the conversion inside
11417 a COND, which will recurse. In that case, the COND_EXPR
11418 is probably the best choice, so leave it alone. */
11419 && type
== TREE_TYPE (arg0
))
11420 return pedantic_non_lvalue_loc (loc
, arg0
);
11422 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11423 over COND_EXPR in cases such as floating point comparisons. */
11424 if (integer_zerop (op1
)
11425 && code
== COND_EXPR
11426 && integer_onep (op2
)
11427 && !VECTOR_TYPE_P (type
)
11428 && truth_value_p (TREE_CODE (arg0
)))
11429 return pedantic_non_lvalue_loc (loc
,
11430 fold_convert_loc (loc
, type
,
11431 invert_truthvalue_loc (loc
,
11434 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11435 if (TREE_CODE (arg0
) == LT_EXPR
11436 && integer_zerop (TREE_OPERAND (arg0
, 1))
11437 && integer_zerop (op2
)
11438 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11440 /* sign_bit_p looks through both zero and sign extensions,
11441 but for this optimization only sign extensions are
11443 tree tem2
= TREE_OPERAND (arg0
, 0);
11444 while (tem
!= tem2
)
11446 if (TREE_CODE (tem2
) != NOP_EXPR
11447 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11452 tem2
= TREE_OPERAND (tem2
, 0);
11454 /* sign_bit_p only checks ARG1 bits within A's precision.
11455 If <sign bit of A> has wider type than A, bits outside
11456 of A's precision in <sign bit of A> need to be checked.
11457 If they are all 0, this optimization needs to be done
11458 in unsigned A's type, if they are all 1 in signed A's type,
11459 otherwise this can't be done. */
11461 && TYPE_PRECISION (TREE_TYPE (tem
))
11462 < TYPE_PRECISION (TREE_TYPE (arg1
))
11463 && TYPE_PRECISION (TREE_TYPE (tem
))
11464 < TYPE_PRECISION (type
))
11466 int inner_width
, outer_width
;
11469 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11470 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11471 if (outer_width
> TYPE_PRECISION (type
))
11472 outer_width
= TYPE_PRECISION (type
);
11474 wide_int mask
= wi::shifted_mask
11475 (inner_width
, outer_width
- inner_width
, false,
11476 TYPE_PRECISION (TREE_TYPE (arg1
)));
11478 wide_int common
= mask
& arg1
;
11479 if (common
== mask
)
11481 tem_type
= signed_type_for (TREE_TYPE (tem
));
11482 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11484 else if (common
== 0)
11486 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11487 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11495 fold_convert_loc (loc
, type
,
11496 fold_build2_loc (loc
, BIT_AND_EXPR
,
11497 TREE_TYPE (tem
), tem
,
11498 fold_convert_loc (loc
,
11503 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11504 already handled above. */
11505 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11506 && integer_onep (TREE_OPERAND (arg0
, 1))
11507 && integer_zerop (op2
)
11508 && integer_pow2p (arg1
))
11510 tree tem
= TREE_OPERAND (arg0
, 0);
11512 if (TREE_CODE (tem
) == RSHIFT_EXPR
11513 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11514 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
)
11515 == tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11516 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11517 fold_convert_loc (loc
, type
,
11518 TREE_OPERAND (tem
, 0)),
11522 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11523 is probably obsolete because the first operand should be a
11524 truth value (that's why we have the two cases above), but let's
11525 leave it in until we can confirm this for all front-ends. */
11526 if (integer_zerop (op2
)
11527 && TREE_CODE (arg0
) == NE_EXPR
11528 && integer_zerop (TREE_OPERAND (arg0
, 1))
11529 && integer_pow2p (arg1
)
11530 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11531 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11532 arg1
, OEP_ONLY_CONST
))
11533 return pedantic_non_lvalue_loc (loc
,
11534 fold_convert_loc (loc
, type
,
11535 TREE_OPERAND (arg0
, 0)));
11537 /* Disable the transformations below for vectors, since
11538 fold_binary_op_with_conditional_arg may undo them immediately,
11539 yielding an infinite loop. */
11540 if (code
== VEC_COND_EXPR
)
11543 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11544 if (integer_zerop (op2
)
11545 && truth_value_p (TREE_CODE (arg0
))
11546 && truth_value_p (TREE_CODE (arg1
))
11547 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11548 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11549 : TRUTH_ANDIF_EXPR
,
11550 type
, fold_convert_loc (loc
, type
, arg0
), op1
);
11552 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11553 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11554 && truth_value_p (TREE_CODE (arg0
))
11555 && truth_value_p (TREE_CODE (arg1
))
11556 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11558 location_t loc0
= expr_location_or (arg0
, loc
);
11559 /* Only perform transformation if ARG0 is easily inverted. */
11560 tem
= fold_invert_truthvalue (loc0
, arg0
);
11562 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11565 type
, fold_convert_loc (loc
, type
, tem
),
11569 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11570 if (integer_zerop (arg1
)
11571 && truth_value_p (TREE_CODE (arg0
))
11572 && truth_value_p (TREE_CODE (op2
))
11573 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11575 location_t loc0
= expr_location_or (arg0
, loc
);
11576 /* Only perform transformation if ARG0 is easily inverted. */
11577 tem
= fold_invert_truthvalue (loc0
, arg0
);
11579 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11580 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11581 type
, fold_convert_loc (loc
, type
, tem
),
11585 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11586 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11587 && truth_value_p (TREE_CODE (arg0
))
11588 && truth_value_p (TREE_CODE (op2
))
11589 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11590 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11591 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11592 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11597 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11598 of fold_ternary on them. */
11599 gcc_unreachable ();
11601 case BIT_FIELD_REF
:
11602 if (TREE_CODE (arg0
) == VECTOR_CST
11603 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11604 || (TREE_CODE (type
) == VECTOR_TYPE
11605 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11607 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11608 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11609 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11610 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11613 && (idx
% width
) == 0
11614 && (n
% width
) == 0
11615 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11620 if (TREE_CODE (arg0
) == VECTOR_CST
)
11623 return VECTOR_CST_ELT (arg0
, idx
);
11625 tree
*vals
= XALLOCAVEC (tree
, n
);
11626 for (unsigned i
= 0; i
< n
; ++i
)
11627 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
11628 return build_vector (type
, vals
);
11633 /* On constants we can use native encode/interpret to constant
11634 fold (nearly) all BIT_FIELD_REFs. */
11635 if (CONSTANT_CLASS_P (arg0
)
11636 && can_native_interpret_type_p (type
)
11637 && BITS_PER_UNIT
== 8)
11639 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11640 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11641 /* Limit us to a reasonable amount of work. To relax the
11642 other limitations we need bit-shifting of the buffer
11643 and rounding up the size. */
11644 if (bitpos
% BITS_PER_UNIT
== 0
11645 && bitsize
% BITS_PER_UNIT
== 0
11646 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
11648 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
11649 unsigned HOST_WIDE_INT len
11650 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
11651 bitpos
/ BITS_PER_UNIT
);
11653 && len
* BITS_PER_UNIT
>= bitsize
)
11655 tree v
= native_interpret_expr (type
, b
,
11656 bitsize
/ BITS_PER_UNIT
);
11666 /* For integers we can decompose the FMA if possible. */
11667 if (TREE_CODE (arg0
) == INTEGER_CST
11668 && TREE_CODE (arg1
) == INTEGER_CST
)
11669 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11670 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11671 if (integer_zerop (arg2
))
11672 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11674 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11676 case VEC_PERM_EXPR
:
11677 if (TREE_CODE (arg2
) == VECTOR_CST
)
11679 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
11680 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
11681 unsigned char *sel2
= sel
+ nelts
;
11682 bool need_mask_canon
= false;
11683 bool need_mask_canon2
= false;
11684 bool all_in_vec0
= true;
11685 bool all_in_vec1
= true;
11686 bool maybe_identity
= true;
11687 bool single_arg
= (op0
== op1
);
11688 bool changed
= false;
11690 mask2
= 2 * nelts
- 1;
11691 mask
= single_arg
? (nelts
- 1) : mask2
;
11692 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
11693 for (i
= 0; i
< nelts
; i
++)
11695 tree val
= VECTOR_CST_ELT (arg2
, i
);
11696 if (TREE_CODE (val
) != INTEGER_CST
)
11699 /* Make sure that the perm value is in an acceptable
11702 need_mask_canon
|= wi::gtu_p (t
, mask
);
11703 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11704 sel
[i
] = t
.to_uhwi () & mask
;
11705 sel2
[i
] = t
.to_uhwi () & mask2
;
11707 if (sel
[i
] < nelts
)
11708 all_in_vec1
= false;
11710 all_in_vec0
= false;
11712 if ((sel
[i
] & (nelts
-1)) != i
)
11713 maybe_identity
= false;
11716 if (maybe_identity
)
11726 else if (all_in_vec1
)
11729 for (i
= 0; i
< nelts
; i
++)
11731 need_mask_canon
= true;
11734 if ((TREE_CODE (op0
) == VECTOR_CST
11735 || TREE_CODE (op0
) == CONSTRUCTOR
)
11736 && (TREE_CODE (op1
) == VECTOR_CST
11737 || TREE_CODE (op1
) == CONSTRUCTOR
))
11739 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11740 if (t
!= NULL_TREE
)
11744 if (op0
== op1
&& !single_arg
)
11747 /* Some targets are deficient and fail to expand a single
11748 argument permutation while still allowing an equivalent
11749 2-argument version. */
11750 if (need_mask_canon
&& arg2
== op2
11751 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
11752 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
11754 need_mask_canon
= need_mask_canon2
;
11758 if (need_mask_canon
&& arg2
== op2
)
11760 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
11761 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11762 for (i
= 0; i
< nelts
; i
++)
11763 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
11764 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11769 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11773 case BIT_INSERT_EXPR
:
11774 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11775 if (TREE_CODE (arg0
) == INTEGER_CST
11776 && TREE_CODE (arg1
) == INTEGER_CST
)
11778 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11779 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
11780 wide_int tem
= wi::bit_and (arg0
,
11781 wi::shifted_mask (bitpos
, bitsize
, true,
11782 TYPE_PRECISION (type
)));
11784 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
11786 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
11788 else if (TREE_CODE (arg0
) == VECTOR_CST
11789 && CONSTANT_CLASS_P (arg1
)
11790 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
11793 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11794 unsigned HOST_WIDE_INT elsize
11795 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
11796 if (bitpos
% elsize
== 0)
11798 unsigned k
= bitpos
/ elsize
;
11799 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
11803 tree
*elts
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
11804 memcpy (elts
, VECTOR_CST_ELTS (arg0
),
11805 sizeof (tree
) * TYPE_VECTOR_SUBPARTS (type
));
11807 return build_vector (type
, elts
);
11815 } /* switch (code) */
11818 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11819 of an array (or vector). */
11822 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
11824 tree index_type
= NULL_TREE
;
11825 offset_int low_bound
= 0;
11827 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
11829 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
11830 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
11832 /* Static constructors for variably sized objects makes no sense. */
11833 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
11834 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
11835 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
11840 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
11841 TYPE_SIGN (index_type
));
11843 offset_int index
= low_bound
- 1;
11845 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11846 TYPE_SIGN (index_type
));
11848 offset_int max_index
;
11849 unsigned HOST_WIDE_INT cnt
;
11852 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
11854 /* Array constructor might explicitly set index, or specify a range,
11855 or leave index NULL meaning that it is next index after previous
11859 if (TREE_CODE (cfield
) == INTEGER_CST
)
11860 max_index
= index
= wi::to_offset (cfield
);
11863 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
11864 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
11865 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
11872 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11873 TYPE_SIGN (index_type
));
11877 /* Do we have match? */
11878 if (wi::cmpu (access_index
, index
) >= 0
11879 && wi::cmpu (access_index
, max_index
) <= 0)
11885 /* Perform constant folding and related simplification of EXPR.
11886 The related simplifications include x*1 => x, x*0 => 0, etc.,
11887 and application of the associative law.
11888 NOP_EXPR conversions may be removed freely (as long as we
11889 are careful not to change the type of the overall expression).
11890 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11891 but we can constant-fold them if they have constant operands. */
11893 #ifdef ENABLE_FOLD_CHECKING
11894 # define fold(x) fold_1 (x)
11895 static tree
fold_1 (tree
);
11901 const tree t
= expr
;
11902 enum tree_code code
= TREE_CODE (t
);
11903 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11905 location_t loc
= EXPR_LOCATION (expr
);
11907 /* Return right away if a constant. */
11908 if (kind
== tcc_constant
)
11911 /* CALL_EXPR-like objects with variable numbers of operands are
11912 treated specially. */
11913 if (kind
== tcc_vl_exp
)
11915 if (code
== CALL_EXPR
)
11917 tem
= fold_call_expr (loc
, expr
, false);
11918 return tem
? tem
: expr
;
11923 if (IS_EXPR_CODE_CLASS (kind
))
11925 tree type
= TREE_TYPE (t
);
11926 tree op0
, op1
, op2
;
11928 switch (TREE_CODE_LENGTH (code
))
11931 op0
= TREE_OPERAND (t
, 0);
11932 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11933 return tem
? tem
: expr
;
11935 op0
= TREE_OPERAND (t
, 0);
11936 op1
= TREE_OPERAND (t
, 1);
11937 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11938 return tem
? tem
: expr
;
11940 op0
= TREE_OPERAND (t
, 0);
11941 op1
= TREE_OPERAND (t
, 1);
11942 op2
= TREE_OPERAND (t
, 2);
11943 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11944 return tem
? tem
: expr
;
11954 tree op0
= TREE_OPERAND (t
, 0);
11955 tree op1
= TREE_OPERAND (t
, 1);
11957 if (TREE_CODE (op1
) == INTEGER_CST
11958 && TREE_CODE (op0
) == CONSTRUCTOR
11959 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11961 tree val
= get_array_ctor_element_at_index (op0
,
11962 wi::to_offset (op1
));
11970 /* Return a VECTOR_CST if possible. */
11973 tree type
= TREE_TYPE (t
);
11974 if (TREE_CODE (type
) != VECTOR_TYPE
)
11979 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
11980 if (! CONSTANT_CLASS_P (val
))
11983 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
11987 return fold (DECL_INITIAL (t
));
11991 } /* switch (code) */
11994 #ifdef ENABLE_FOLD_CHECKING
11997 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
11998 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
11999 static void fold_check_failed (const_tree
, const_tree
);
12000 void print_fold_checksum (const_tree
);
12002 /* When --enable-checking=fold, compute a digest of expr before
12003 and after actual fold call to see if fold did not accidentally
12004 change original expr. */
12010 struct md5_ctx ctx
;
12011 unsigned char checksum_before
[16], checksum_after
[16];
12012 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12014 md5_init_ctx (&ctx
);
12015 fold_checksum_tree (expr
, &ctx
, &ht
);
12016 md5_finish_ctx (&ctx
, checksum_before
);
12019 ret
= fold_1 (expr
);
12021 md5_init_ctx (&ctx
);
12022 fold_checksum_tree (expr
, &ctx
, &ht
);
12023 md5_finish_ctx (&ctx
, checksum_after
);
12025 if (memcmp (checksum_before
, checksum_after
, 16))
12026 fold_check_failed (expr
, ret
);
12032 print_fold_checksum (const_tree expr
)
12034 struct md5_ctx ctx
;
12035 unsigned char checksum
[16], cnt
;
12036 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12038 md5_init_ctx (&ctx
);
12039 fold_checksum_tree (expr
, &ctx
, &ht
);
12040 md5_finish_ctx (&ctx
, checksum
);
12041 for (cnt
= 0; cnt
< 16; ++cnt
)
12042 fprintf (stderr
, "%02x", checksum
[cnt
]);
12043 putc ('\n', stderr
);
12047 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12049 internal_error ("fold check: original tree changed by fold");
12053 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12054 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12056 const tree_node
**slot
;
12057 enum tree_code code
;
12058 union tree_node buf
;
12064 slot
= ht
->find_slot (expr
, INSERT
);
12068 code
= TREE_CODE (expr
);
12069 if (TREE_CODE_CLASS (code
) == tcc_declaration
12070 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12072 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12073 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12074 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12075 buf
.decl_with_vis
.symtab_node
= NULL
;
12076 expr
= (tree
) &buf
;
12078 else if (TREE_CODE_CLASS (code
) == tcc_type
12079 && (TYPE_POINTER_TO (expr
)
12080 || TYPE_REFERENCE_TO (expr
)
12081 || TYPE_CACHED_VALUES_P (expr
)
12082 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12083 || TYPE_NEXT_VARIANT (expr
)
12084 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
12086 /* Allow these fields to be modified. */
12088 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12089 expr
= tmp
= (tree
) &buf
;
12090 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12091 TYPE_POINTER_TO (tmp
) = NULL
;
12092 TYPE_REFERENCE_TO (tmp
) = NULL
;
12093 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12094 TYPE_ALIAS_SET (tmp
) = -1;
12095 if (TYPE_CACHED_VALUES_P (tmp
))
12097 TYPE_CACHED_VALUES_P (tmp
) = 0;
12098 TYPE_CACHED_VALUES (tmp
) = NULL
;
12101 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12102 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12103 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12104 if (TREE_CODE_CLASS (code
) != tcc_type
12105 && TREE_CODE_CLASS (code
) != tcc_declaration
12106 && code
!= TREE_LIST
12107 && code
!= SSA_NAME
12108 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12109 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12110 switch (TREE_CODE_CLASS (code
))
12116 md5_process_bytes (TREE_STRING_POINTER (expr
),
12117 TREE_STRING_LENGTH (expr
), ctx
);
12120 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12121 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12124 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12125 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12131 case tcc_exceptional
:
12135 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12136 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12137 expr
= TREE_CHAIN (expr
);
12138 goto recursive_label
;
12141 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12142 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12148 case tcc_expression
:
12149 case tcc_reference
:
12150 case tcc_comparison
:
12153 case tcc_statement
:
12155 len
= TREE_OPERAND_LENGTH (expr
);
12156 for (i
= 0; i
< len
; ++i
)
12157 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12159 case tcc_declaration
:
12160 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12161 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12162 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12164 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12165 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12166 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12167 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12168 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12171 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12173 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12175 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12176 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12178 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12182 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12183 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12184 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12185 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12186 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12187 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12188 if (INTEGRAL_TYPE_P (expr
)
12189 || SCALAR_FLOAT_TYPE_P (expr
))
12191 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12192 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12194 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12195 if (TREE_CODE (expr
) == RECORD_TYPE
12196 || TREE_CODE (expr
) == UNION_TYPE
12197 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12198 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12199 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12206 /* Helper function for outputting the checksum of a tree T. When
12207 debugging with gdb, you can "define mynext" to be "next" followed
12208 by "call debug_fold_checksum (op0)", then just trace down till the
12211 DEBUG_FUNCTION
void
12212 debug_fold_checksum (const_tree t
)
12215 unsigned char checksum
[16];
12216 struct md5_ctx ctx
;
12217 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12219 md5_init_ctx (&ctx
);
12220 fold_checksum_tree (t
, &ctx
, &ht
);
12221 md5_finish_ctx (&ctx
, checksum
);
12224 for (i
= 0; i
< 16; i
++)
12225 fprintf (stderr
, "%d ", checksum
[i
]);
12227 fprintf (stderr
, "\n");
12232 /* Fold a unary tree expression with code CODE of type TYPE with an
12233 operand OP0. LOC is the location of the resulting expression.
12234 Return a folded expression if successful. Otherwise, return a tree
12235 expression with code CODE of type TYPE with an operand OP0. */
12238 fold_build1_stat_loc (location_t loc
,
12239 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12242 #ifdef ENABLE_FOLD_CHECKING
12243 unsigned char checksum_before
[16], checksum_after
[16];
12244 struct md5_ctx ctx
;
12245 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12247 md5_init_ctx (&ctx
);
12248 fold_checksum_tree (op0
, &ctx
, &ht
);
12249 md5_finish_ctx (&ctx
, checksum_before
);
12253 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12255 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12257 #ifdef ENABLE_FOLD_CHECKING
12258 md5_init_ctx (&ctx
);
12259 fold_checksum_tree (op0
, &ctx
, &ht
);
12260 md5_finish_ctx (&ctx
, checksum_after
);
12262 if (memcmp (checksum_before
, checksum_after
, 16))
12263 fold_check_failed (op0
, tem
);
12268 /* Fold a binary tree expression with code CODE of type TYPE with
12269 operands OP0 and OP1. LOC is the location of the resulting
12270 expression. Return a folded expression if successful. Otherwise,
12271 return a tree expression with code CODE of type TYPE with operands
12275 fold_build2_stat_loc (location_t loc
,
12276 enum tree_code code
, tree type
, tree op0
, tree op1
12280 #ifdef ENABLE_FOLD_CHECKING
12281 unsigned char checksum_before_op0
[16],
12282 checksum_before_op1
[16],
12283 checksum_after_op0
[16],
12284 checksum_after_op1
[16];
12285 struct md5_ctx ctx
;
12286 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12288 md5_init_ctx (&ctx
);
12289 fold_checksum_tree (op0
, &ctx
, &ht
);
12290 md5_finish_ctx (&ctx
, checksum_before_op0
);
12293 md5_init_ctx (&ctx
);
12294 fold_checksum_tree (op1
, &ctx
, &ht
);
12295 md5_finish_ctx (&ctx
, checksum_before_op1
);
12299 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12301 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12303 #ifdef ENABLE_FOLD_CHECKING
12304 md5_init_ctx (&ctx
);
12305 fold_checksum_tree (op0
, &ctx
, &ht
);
12306 md5_finish_ctx (&ctx
, checksum_after_op0
);
12309 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12310 fold_check_failed (op0
, tem
);
12312 md5_init_ctx (&ctx
);
12313 fold_checksum_tree (op1
, &ctx
, &ht
);
12314 md5_finish_ctx (&ctx
, checksum_after_op1
);
12316 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12317 fold_check_failed (op1
, tem
);
12322 /* Fold a ternary tree expression with code CODE of type TYPE with
12323 operands OP0, OP1, and OP2. Return a folded expression if
12324 successful. Otherwise, return a tree expression with code CODE of
12325 type TYPE with operands OP0, OP1, and OP2. */
12328 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
12329 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12332 #ifdef ENABLE_FOLD_CHECKING
12333 unsigned char checksum_before_op0
[16],
12334 checksum_before_op1
[16],
12335 checksum_before_op2
[16],
12336 checksum_after_op0
[16],
12337 checksum_after_op1
[16],
12338 checksum_after_op2
[16];
12339 struct md5_ctx ctx
;
12340 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12342 md5_init_ctx (&ctx
);
12343 fold_checksum_tree (op0
, &ctx
, &ht
);
12344 md5_finish_ctx (&ctx
, checksum_before_op0
);
12347 md5_init_ctx (&ctx
);
12348 fold_checksum_tree (op1
, &ctx
, &ht
);
12349 md5_finish_ctx (&ctx
, checksum_before_op1
);
12352 md5_init_ctx (&ctx
);
12353 fold_checksum_tree (op2
, &ctx
, &ht
);
12354 md5_finish_ctx (&ctx
, checksum_before_op2
);
12358 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12359 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12361 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12363 #ifdef ENABLE_FOLD_CHECKING
12364 md5_init_ctx (&ctx
);
12365 fold_checksum_tree (op0
, &ctx
, &ht
);
12366 md5_finish_ctx (&ctx
, checksum_after_op0
);
12369 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12370 fold_check_failed (op0
, tem
);
12372 md5_init_ctx (&ctx
);
12373 fold_checksum_tree (op1
, &ctx
, &ht
);
12374 md5_finish_ctx (&ctx
, checksum_after_op1
);
12377 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12378 fold_check_failed (op1
, tem
);
12380 md5_init_ctx (&ctx
);
12381 fold_checksum_tree (op2
, &ctx
, &ht
);
12382 md5_finish_ctx (&ctx
, checksum_after_op2
);
12384 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12385 fold_check_failed (op2
, tem
);
12390 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12391 arguments in ARGARRAY, and a null static chain.
12392 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12393 of type TYPE from the given operands as constructed by build_call_array. */
12396 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12397 int nargs
, tree
*argarray
)
12400 #ifdef ENABLE_FOLD_CHECKING
12401 unsigned char checksum_before_fn
[16],
12402 checksum_before_arglist
[16],
12403 checksum_after_fn
[16],
12404 checksum_after_arglist
[16];
12405 struct md5_ctx ctx
;
12406 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12409 md5_init_ctx (&ctx
);
12410 fold_checksum_tree (fn
, &ctx
, &ht
);
12411 md5_finish_ctx (&ctx
, checksum_before_fn
);
12414 md5_init_ctx (&ctx
);
12415 for (i
= 0; i
< nargs
; i
++)
12416 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12417 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12421 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12423 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12425 #ifdef ENABLE_FOLD_CHECKING
12426 md5_init_ctx (&ctx
);
12427 fold_checksum_tree (fn
, &ctx
, &ht
);
12428 md5_finish_ctx (&ctx
, checksum_after_fn
);
12431 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12432 fold_check_failed (fn
, tem
);
12434 md5_init_ctx (&ctx
);
12435 for (i
= 0; i
< nargs
; i
++)
12436 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12437 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12439 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12440 fold_check_failed (NULL_TREE
, tem
);
12445 /* Perform constant folding and related simplification of initializer
12446 expression EXPR. These behave identically to "fold_buildN" but ignore
12447 potential run-time traps and exceptions that fold must preserve. */
12449 #define START_FOLD_INIT \
12450 int saved_signaling_nans = flag_signaling_nans;\
12451 int saved_trapping_math = flag_trapping_math;\
12452 int saved_rounding_math = flag_rounding_math;\
12453 int saved_trapv = flag_trapv;\
12454 int saved_folding_initializer = folding_initializer;\
12455 flag_signaling_nans = 0;\
12456 flag_trapping_math = 0;\
12457 flag_rounding_math = 0;\
12459 folding_initializer = 1;
12461 #define END_FOLD_INIT \
12462 flag_signaling_nans = saved_signaling_nans;\
12463 flag_trapping_math = saved_trapping_math;\
12464 flag_rounding_math = saved_rounding_math;\
12465 flag_trapv = saved_trapv;\
12466 folding_initializer = saved_folding_initializer;
12469 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12470 tree type
, tree op
)
12475 result
= fold_build1_loc (loc
, code
, type
, op
);
12482 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12483 tree type
, tree op0
, tree op1
)
12488 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12495 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12496 int nargs
, tree
*argarray
)
12501 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12507 #undef START_FOLD_INIT
12508 #undef END_FOLD_INIT
12510 /* Determine if first argument is a multiple of second argument. Return 0 if
12511 it is not, or we cannot easily determined it to be.
12513 An example of the sort of thing we care about (at this point; this routine
12514 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12515 fold cases do now) is discovering that
12517 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12523 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12525 This code also handles discovering that
12527 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12529 is a multiple of 8 so we don't have to worry about dealing with a
12530 possible remainder.
12532 Note that we *look* inside a SAVE_EXPR only to determine how it was
12533 calculated; it is not safe for fold to do much of anything else with the
12534 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12535 at run time. For example, the latter example above *cannot* be implemented
12536 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12537 evaluation time of the original SAVE_EXPR is not necessarily the same at
12538 the time the new expression is evaluated. The only optimization of this
12539 sort that would be valid is changing
12541 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12545 SAVE_EXPR (I) * SAVE_EXPR (J)
12547 (where the same SAVE_EXPR (J) is used in the original and the
12548 transformed version). */
12551 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12556 if (operand_equal_p (top
, bottom
, 0))
12559 if (TREE_CODE (type
) != INTEGER_TYPE
)
12562 switch (TREE_CODE (top
))
12565 /* Bitwise and provides a power of two multiple. If the mask is
12566 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12567 if (!integer_pow2p (bottom
))
12572 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12573 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12576 /* It is impossible to prove if op0 - op1 is multiple of bottom
12577 precisely, so be conservative here checking if both op0 and op1
12578 are multiple of bottom. Note we check the second operand first
12579 since it's usually simpler. */
12580 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12581 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12584 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12585 as op0 - 3 if the expression has unsigned type. For example,
12586 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12587 op1
= TREE_OPERAND (top
, 1);
12588 if (TYPE_UNSIGNED (type
)
12589 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
12590 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
12591 return (multiple_of_p (type
, op1
, bottom
)
12592 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12595 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12597 op1
= TREE_OPERAND (top
, 1);
12598 /* const_binop may not detect overflow correctly,
12599 so check for it explicitly here. */
12600 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12601 && 0 != (t1
= fold_convert (type
,
12602 const_binop (LSHIFT_EXPR
,
12605 && !TREE_OVERFLOW (t1
))
12606 return multiple_of_p (type
, t1
, bottom
);
12611 /* Can't handle conversions from non-integral or wider integral type. */
12612 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12613 || (TYPE_PRECISION (type
)
12614 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12620 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12623 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12624 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12627 if (TREE_CODE (bottom
) != INTEGER_CST
12628 || integer_zerop (bottom
)
12629 || (TYPE_UNSIGNED (type
)
12630 && (tree_int_cst_sgn (top
) < 0
12631 || tree_int_cst_sgn (bottom
) < 0)))
12633 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12637 if (TREE_CODE (bottom
) == INTEGER_CST
12638 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
12639 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
12641 enum tree_code code
= gimple_assign_rhs_code (stmt
);
12643 /* Check for special cases to see if top is defined as multiple
12646 top = (X & ~(bottom - 1) ; bottom is power of 2
12652 if (code
== BIT_AND_EXPR
12653 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12654 && TREE_CODE (op2
) == INTEGER_CST
12655 && integer_pow2p (bottom
)
12656 && wi::multiple_of_p (wi::to_widest (op2
),
12657 wi::to_widest (bottom
), UNSIGNED
))
12660 op1
= gimple_assign_rhs1 (stmt
);
12661 if (code
== MINUS_EXPR
12662 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12663 && TREE_CODE (op2
) == SSA_NAME
12664 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
12665 && gimple_code (stmt
) == GIMPLE_ASSIGN
12666 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
12667 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
12668 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
12679 #define tree_expr_nonnegative_warnv_p(X, Y) \
12680 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12682 #define RECURSE(X) \
12683 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12685 /* Return true if CODE or TYPE is known to be non-negative. */
12688 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12690 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12691 && truth_value_p (code
))
12692 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12693 have a signed:1 type (where the value is -1 and 0). */
12698 /* Return true if (CODE OP0) 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_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12705 bool *strict_overflow_p
, int depth
)
12707 if (TYPE_UNSIGNED (type
))
12713 /* We can't return 1 if flag_wrapv is set because
12714 ABS_EXPR<INT_MIN> = INT_MIN. */
12715 if (!ANY_INTEGRAL_TYPE_P (type
))
12717 if (TYPE_OVERFLOW_UNDEFINED (type
))
12719 *strict_overflow_p
= true;
12724 case NON_LVALUE_EXPR
:
12726 case FIX_TRUNC_EXPR
:
12727 return RECURSE (op0
);
12731 tree inner_type
= TREE_TYPE (op0
);
12732 tree outer_type
= type
;
12734 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12736 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12737 return RECURSE (op0
);
12738 if (INTEGRAL_TYPE_P (inner_type
))
12740 if (TYPE_UNSIGNED (inner_type
))
12742 return RECURSE (op0
);
12745 else if (INTEGRAL_TYPE_P (outer_type
))
12747 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12748 return RECURSE (op0
);
12749 if (INTEGRAL_TYPE_P (inner_type
))
12750 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12751 && TYPE_UNSIGNED (inner_type
);
12757 return tree_simple_nonnegative_warnv_p (code
, type
);
12760 /* We don't know sign of `t', so be conservative and return false. */
12764 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12765 value is based on the assumption that signed overflow is undefined,
12766 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12767 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12770 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12771 tree op1
, bool *strict_overflow_p
,
12774 if (TYPE_UNSIGNED (type
))
12779 case POINTER_PLUS_EXPR
:
12781 if (FLOAT_TYPE_P (type
))
12782 return RECURSE (op0
) && RECURSE (op1
);
12784 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12785 both unsigned and at least 2 bits shorter than the result. */
12786 if (TREE_CODE (type
) == INTEGER_TYPE
12787 && TREE_CODE (op0
) == NOP_EXPR
12788 && TREE_CODE (op1
) == NOP_EXPR
)
12790 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12791 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12792 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12793 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12795 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12796 TYPE_PRECISION (inner2
)) + 1;
12797 return prec
< TYPE_PRECISION (type
);
12803 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12805 /* x * x is always non-negative for floating point x
12806 or without overflow. */
12807 if (operand_equal_p (op0
, op1
, 0)
12808 || (RECURSE (op0
) && RECURSE (op1
)))
12810 if (ANY_INTEGRAL_TYPE_P (type
)
12811 && TYPE_OVERFLOW_UNDEFINED (type
))
12812 *strict_overflow_p
= true;
12817 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12818 both unsigned and their total bits is shorter than the result. */
12819 if (TREE_CODE (type
) == INTEGER_TYPE
12820 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12821 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12823 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12824 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12826 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12827 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12830 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12831 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12833 if (TREE_CODE (op0
) == INTEGER_CST
)
12834 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12836 if (TREE_CODE (op1
) == INTEGER_CST
)
12837 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12839 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12840 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12842 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12843 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12844 : TYPE_PRECISION (inner0
);
12846 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12847 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12848 : TYPE_PRECISION (inner1
);
12850 return precision0
+ precision1
< TYPE_PRECISION (type
);
12857 return RECURSE (op0
) || RECURSE (op1
);
12863 case TRUNC_DIV_EXPR
:
12864 case CEIL_DIV_EXPR
:
12865 case FLOOR_DIV_EXPR
:
12866 case ROUND_DIV_EXPR
:
12867 return RECURSE (op0
) && RECURSE (op1
);
12869 case TRUNC_MOD_EXPR
:
12870 return RECURSE (op0
);
12872 case FLOOR_MOD_EXPR
:
12873 return RECURSE (op1
);
12875 case CEIL_MOD_EXPR
:
12876 case ROUND_MOD_EXPR
:
12878 return tree_simple_nonnegative_warnv_p (code
, type
);
12881 /* We don't know sign of `t', so be conservative and return false. */
12885 /* Return true if T is known to be non-negative. If the return
12886 value is based on the assumption that signed overflow is undefined,
12887 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12888 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12891 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12893 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12896 switch (TREE_CODE (t
))
12899 return tree_int_cst_sgn (t
) >= 0;
12902 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12905 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12908 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12911 /* Limit the depth of recursion to avoid quadratic behavior.
12912 This is expected to catch almost all occurrences in practice.
12913 If this code misses important cases that unbounded recursion
12914 would not, passes that need this information could be revised
12915 to provide it through dataflow propagation. */
12916 return (!name_registered_for_update_p (t
)
12917 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12918 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12919 strict_overflow_p
, depth
));
12922 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12926 /* Return true if T is known to be non-negative. If the return
12927 value is based on the assumption that signed overflow is undefined,
12928 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12929 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12932 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
12933 bool *strict_overflow_p
, int depth
)
12954 case CFN_BUILT_IN_BSWAP32
:
12955 case CFN_BUILT_IN_BSWAP64
:
12960 /* sqrt(-0.0) is -0.0. */
12961 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
12963 return RECURSE (arg0
);
12989 CASE_CFN_NEARBYINT
:
12996 CASE_CFN_SIGNIFICAND
:
13000 /* True if the 1st argument is nonnegative. */
13001 return RECURSE (arg0
);
13004 /* True if the 1st OR 2nd arguments are nonnegative. */
13005 return RECURSE (arg0
) || RECURSE (arg1
);
13008 /* True if the 1st AND 2nd arguments are nonnegative. */
13009 return RECURSE (arg0
) && RECURSE (arg1
);
13012 /* True if the 2nd argument is nonnegative. */
13013 return RECURSE (arg1
);
13016 /* True if the 1st argument is nonnegative or the second
13017 argument is an even integer. */
13018 if (TREE_CODE (arg1
) == INTEGER_CST
13019 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
13021 return RECURSE (arg0
);
13024 /* True if the 1st argument is nonnegative or the second
13025 argument is an even integer valued real. */
13026 if (TREE_CODE (arg1
) == REAL_CST
)
13031 c
= TREE_REAL_CST (arg1
);
13032 n
= real_to_integer (&c
);
13035 REAL_VALUE_TYPE cint
;
13036 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
13037 if (real_identical (&c
, &cint
))
13041 return RECURSE (arg0
);
13046 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
13049 /* Return true if T is known to be non-negative. If the return
13050 value is based on the assumption that signed overflow is undefined,
13051 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13052 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13055 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13057 enum tree_code code
= TREE_CODE (t
);
13058 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13065 tree temp
= TARGET_EXPR_SLOT (t
);
13066 t
= TARGET_EXPR_INITIAL (t
);
13068 /* If the initializer is non-void, then it's a normal expression
13069 that will be assigned to the slot. */
13070 if (!VOID_TYPE_P (t
))
13071 return RECURSE (t
);
13073 /* Otherwise, the initializer sets the slot in some way. One common
13074 way is an assignment statement at the end of the initializer. */
13077 if (TREE_CODE (t
) == BIND_EXPR
)
13078 t
= expr_last (BIND_EXPR_BODY (t
));
13079 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13080 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13081 t
= expr_last (TREE_OPERAND (t
, 0));
13082 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13087 if (TREE_CODE (t
) == MODIFY_EXPR
13088 && TREE_OPERAND (t
, 0) == temp
)
13089 return RECURSE (TREE_OPERAND (t
, 1));
13096 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13097 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13099 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13100 get_call_combined_fn (t
),
13103 strict_overflow_p
, depth
);
13105 case COMPOUND_EXPR
:
13107 return RECURSE (TREE_OPERAND (t
, 1));
13110 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13113 return RECURSE (TREE_OPERAND (t
, 0));
13116 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13121 #undef tree_expr_nonnegative_warnv_p
13123 /* Return true if T is known to be non-negative. If the return
13124 value is based on the assumption that signed overflow is undefined,
13125 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13126 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13129 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13131 enum tree_code code
;
13132 if (t
== error_mark_node
)
13135 code
= TREE_CODE (t
);
13136 switch (TREE_CODE_CLASS (code
))
13139 case tcc_comparison
:
13140 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13142 TREE_OPERAND (t
, 0),
13143 TREE_OPERAND (t
, 1),
13144 strict_overflow_p
, depth
);
13147 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13149 TREE_OPERAND (t
, 0),
13150 strict_overflow_p
, depth
);
13153 case tcc_declaration
:
13154 case tcc_reference
:
13155 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13163 case TRUTH_AND_EXPR
:
13164 case TRUTH_OR_EXPR
:
13165 case TRUTH_XOR_EXPR
:
13166 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13168 TREE_OPERAND (t
, 0),
13169 TREE_OPERAND (t
, 1),
13170 strict_overflow_p
, depth
);
13171 case TRUTH_NOT_EXPR
:
13172 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13174 TREE_OPERAND (t
, 0),
13175 strict_overflow_p
, depth
);
13182 case WITH_SIZE_EXPR
:
13184 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13187 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13191 /* Return true if `t' is known to be non-negative. Handle warnings
13192 about undefined signed overflow. */
13195 tree_expr_nonnegative_p (tree t
)
13197 bool ret
, strict_overflow_p
;
13199 strict_overflow_p
= false;
13200 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13201 if (strict_overflow_p
)
13202 fold_overflow_warning (("assuming signed overflow does not occur when "
13203 "determining that expression is always "
13205 WARN_STRICT_OVERFLOW_MISC
);
13210 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13211 For floating point we further ensure that T is not denormal.
13212 Similar logic is present in nonzero_address in rtlanal.h.
13214 If the return value is based on the assumption that signed overflow
13215 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13216 change *STRICT_OVERFLOW_P. */
13219 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13220 bool *strict_overflow_p
)
13225 return tree_expr_nonzero_warnv_p (op0
,
13226 strict_overflow_p
);
13230 tree inner_type
= TREE_TYPE (op0
);
13231 tree outer_type
= type
;
13233 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13234 && tree_expr_nonzero_warnv_p (op0
,
13235 strict_overflow_p
));
13239 case NON_LVALUE_EXPR
:
13240 return tree_expr_nonzero_warnv_p (op0
,
13241 strict_overflow_p
);
13250 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13251 For floating point we further ensure that T is not denormal.
13252 Similar logic is present in nonzero_address in rtlanal.h.
13254 If the return value is based on the assumption that signed overflow
13255 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13256 change *STRICT_OVERFLOW_P. */
13259 tree_binary_nonzero_warnv_p (enum tree_code code
,
13262 tree op1
, bool *strict_overflow_p
)
13264 bool sub_strict_overflow_p
;
13267 case POINTER_PLUS_EXPR
:
13269 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13271 /* With the presence of negative values it is hard
13272 to say something. */
13273 sub_strict_overflow_p
= false;
13274 if (!tree_expr_nonnegative_warnv_p (op0
,
13275 &sub_strict_overflow_p
)
13276 || !tree_expr_nonnegative_warnv_p (op1
,
13277 &sub_strict_overflow_p
))
13279 /* One of operands must be positive and the other non-negative. */
13280 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13281 overflows, on a twos-complement machine the sum of two
13282 nonnegative numbers can never be zero. */
13283 return (tree_expr_nonzero_warnv_p (op0
,
13285 || tree_expr_nonzero_warnv_p (op1
,
13286 strict_overflow_p
));
13291 if (TYPE_OVERFLOW_UNDEFINED (type
))
13293 if (tree_expr_nonzero_warnv_p (op0
,
13295 && tree_expr_nonzero_warnv_p (op1
,
13296 strict_overflow_p
))
13298 *strict_overflow_p
= true;
13305 sub_strict_overflow_p
= false;
13306 if (tree_expr_nonzero_warnv_p (op0
,
13307 &sub_strict_overflow_p
)
13308 && tree_expr_nonzero_warnv_p (op1
,
13309 &sub_strict_overflow_p
))
13311 if (sub_strict_overflow_p
)
13312 *strict_overflow_p
= true;
13317 sub_strict_overflow_p
= false;
13318 if (tree_expr_nonzero_warnv_p (op0
,
13319 &sub_strict_overflow_p
))
13321 if (sub_strict_overflow_p
)
13322 *strict_overflow_p
= true;
13324 /* When both operands are nonzero, then MAX must be too. */
13325 if (tree_expr_nonzero_warnv_p (op1
,
13326 strict_overflow_p
))
13329 /* MAX where operand 0 is positive is positive. */
13330 return tree_expr_nonnegative_warnv_p (op0
,
13331 strict_overflow_p
);
13333 /* MAX where operand 1 is positive is positive. */
13334 else if (tree_expr_nonzero_warnv_p (op1
,
13335 &sub_strict_overflow_p
)
13336 && tree_expr_nonnegative_warnv_p (op1
,
13337 &sub_strict_overflow_p
))
13339 if (sub_strict_overflow_p
)
13340 *strict_overflow_p
= true;
13346 return (tree_expr_nonzero_warnv_p (op1
,
13348 || tree_expr_nonzero_warnv_p (op0
,
13349 strict_overflow_p
));
13358 /* Return true when T is an address and is known to be nonzero.
13359 For floating point we further ensure that T is not denormal.
13360 Similar logic is present in nonzero_address in rtlanal.h.
13362 If the return value is based on the assumption that signed overflow
13363 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13364 change *STRICT_OVERFLOW_P. */
13367 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13369 bool sub_strict_overflow_p
;
13370 switch (TREE_CODE (t
))
13373 return !integer_zerop (t
);
13377 tree base
= TREE_OPERAND (t
, 0);
13379 if (!DECL_P (base
))
13380 base
= get_base_address (base
);
13382 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
13383 base
= TARGET_EXPR_SLOT (base
);
13388 /* For objects in symbol table check if we know they are non-zero.
13389 Don't do anything for variables and functions before symtab is built;
13390 it is quite possible that they will be declared weak later. */
13391 int nonzero_addr
= maybe_nonzero_address (base
);
13392 if (nonzero_addr
>= 0)
13393 return nonzero_addr
;
13395 /* Constants are never weak. */
13396 if (CONSTANT_CLASS_P (base
))
13403 sub_strict_overflow_p
= false;
13404 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13405 &sub_strict_overflow_p
)
13406 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13407 &sub_strict_overflow_p
))
13409 if (sub_strict_overflow_p
)
13410 *strict_overflow_p
= true;
13416 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
13418 return expr_not_equal_to (t
, wi::zero (TYPE_PRECISION (TREE_TYPE (t
))));
13426 #define integer_valued_real_p(X) \
13427 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13429 #define RECURSE(X) \
13430 ((integer_valued_real_p) (X, depth + 1))
13432 /* Return true if the floating point result of (CODE OP0) has an
13433 integer value. We also allow +Inf, -Inf and NaN to be considered
13434 integer values. Return false for signaling NaN.
13436 DEPTH is the current nesting depth of the query. */
13439 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13447 return RECURSE (op0
);
13451 tree type
= TREE_TYPE (op0
);
13452 if (TREE_CODE (type
) == INTEGER_TYPE
)
13454 if (TREE_CODE (type
) == REAL_TYPE
)
13455 return RECURSE (op0
);
13465 /* Return true if the floating point result of (CODE OP0 OP1) has an
13466 integer value. We also allow +Inf, -Inf and NaN to be considered
13467 integer values. Return false for signaling NaN.
13469 DEPTH is the current nesting depth of the query. */
13472 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13481 return RECURSE (op0
) && RECURSE (op1
);
13489 /* Return true if the floating point result of calling FNDECL with arguments
13490 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13491 considered integer values. Return false for signaling NaN. If FNDECL
13492 takes fewer than 2 arguments, the remaining ARGn are null.
13494 DEPTH is the current nesting depth of the query. */
13497 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13503 CASE_CFN_NEARBYINT
:
13511 return RECURSE (arg0
) && RECURSE (arg1
);
13519 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13520 has an integer value. We also allow +Inf, -Inf and NaN to be
13521 considered integer values. Return false for signaling NaN.
13523 DEPTH is the current nesting depth of the query. */
13526 integer_valued_real_single_p (tree t
, int depth
)
13528 switch (TREE_CODE (t
))
13531 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13534 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13537 /* Limit the depth of recursion to avoid quadratic behavior.
13538 This is expected to catch almost all occurrences in practice.
13539 If this code misses important cases that unbounded recursion
13540 would not, passes that need this information could be revised
13541 to provide it through dataflow propagation. */
13542 return (!name_registered_for_update_p (t
)
13543 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13544 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13553 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13554 has an integer value. We also allow +Inf, -Inf and NaN to be
13555 considered integer values. Return false for signaling NaN.
13557 DEPTH is the current nesting depth of the query. */
13560 integer_valued_real_invalid_p (tree t
, int depth
)
13562 switch (TREE_CODE (t
))
13564 case COMPOUND_EXPR
:
13567 return RECURSE (TREE_OPERAND (t
, 1));
13570 return RECURSE (TREE_OPERAND (t
, 0));
13579 #undef integer_valued_real_p
13581 /* Return true if the floating point expression T has an integer value.
13582 We also allow +Inf, -Inf and NaN to be considered integer values.
13583 Return false for signaling NaN.
13585 DEPTH is the current nesting depth of the query. */
13588 integer_valued_real_p (tree t
, int depth
)
13590 if (t
== error_mark_node
)
13593 tree_code code
= TREE_CODE (t
);
13594 switch (TREE_CODE_CLASS (code
))
13597 case tcc_comparison
:
13598 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13599 TREE_OPERAND (t
, 1), depth
);
13602 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13605 case tcc_declaration
:
13606 case tcc_reference
:
13607 return integer_valued_real_single_p (t
, depth
);
13617 return integer_valued_real_single_p (t
, depth
);
13621 tree arg0
= (call_expr_nargs (t
) > 0
13622 ? CALL_EXPR_ARG (t
, 0)
13624 tree arg1
= (call_expr_nargs (t
) > 1
13625 ? CALL_EXPR_ARG (t
, 1)
13627 return integer_valued_real_call_p (get_call_combined_fn (t
),
13628 arg0
, arg1
, depth
);
13632 return integer_valued_real_invalid_p (t
, depth
);
13636 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13637 attempt to fold the expression to a constant without modifying TYPE,
13640 If the expression could be simplified to a constant, then return
13641 the constant. If the expression would not be simplified to a
13642 constant, then return NULL_TREE. */
13645 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13647 tree tem
= fold_binary (code
, type
, op0
, op1
);
13648 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13651 /* Given the components of a unary expression CODE, TYPE and OP0,
13652 attempt to fold the expression to a constant without modifying
13655 If the expression could be simplified to a constant, then return
13656 the constant. If the expression would not be simplified to a
13657 constant, then return NULL_TREE. */
13660 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13662 tree tem
= fold_unary (code
, type
, op0
);
13663 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13666 /* If EXP represents referencing an element in a constant string
13667 (either via pointer arithmetic or array indexing), return the
13668 tree representing the value accessed, otherwise return NULL. */
13671 fold_read_from_constant_string (tree exp
)
13673 if ((TREE_CODE (exp
) == INDIRECT_REF
13674 || TREE_CODE (exp
) == ARRAY_REF
)
13675 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13677 tree exp1
= TREE_OPERAND (exp
, 0);
13680 location_t loc
= EXPR_LOCATION (exp
);
13682 if (TREE_CODE (exp
) == INDIRECT_REF
)
13683 string
= string_constant (exp1
, &index
);
13686 tree low_bound
= array_ref_low_bound (exp
);
13687 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13689 /* Optimize the special-case of a zero lower bound.
13691 We convert the low_bound to sizetype to avoid some problems
13692 with constant folding. (E.g. suppose the lower bound is 1,
13693 and its mode is QI. Without the conversion,l (ARRAY
13694 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13695 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13696 if (! integer_zerop (low_bound
))
13697 index
= size_diffop_loc (loc
, index
,
13698 fold_convert_loc (loc
, sizetype
, low_bound
));
13704 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13705 && TREE_CODE (string
) == STRING_CST
13706 && TREE_CODE (index
) == INTEGER_CST
13707 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13708 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
13710 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
13711 return build_int_cst_type (TREE_TYPE (exp
),
13712 (TREE_STRING_POINTER (string
)
13713 [TREE_INT_CST_LOW (index
)]));
13718 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13719 an integer constant, real, or fixed-point constant.
13721 TYPE is the type of the result. */
13724 fold_negate_const (tree arg0
, tree type
)
13726 tree t
= NULL_TREE
;
13728 switch (TREE_CODE (arg0
))
13733 wide_int val
= wi::neg (arg0
, &overflow
);
13734 t
= force_fit_type (type
, val
, 1,
13735 (overflow
| TREE_OVERFLOW (arg0
))
13736 && !TYPE_UNSIGNED (type
));
13741 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13746 FIXED_VALUE_TYPE f
;
13747 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13748 &(TREE_FIXED_CST (arg0
)), NULL
,
13749 TYPE_SATURATING (type
));
13750 t
= build_fixed (type
, f
);
13751 /* Propagate overflow flags. */
13752 if (overflow_p
| TREE_OVERFLOW (arg0
))
13753 TREE_OVERFLOW (t
) = 1;
13758 gcc_unreachable ();
13764 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13765 an integer constant or real constant.
13767 TYPE is the type of the result. */
13770 fold_abs_const (tree arg0
, tree type
)
13772 tree t
= NULL_TREE
;
13774 switch (TREE_CODE (arg0
))
13778 /* If the value is unsigned or non-negative, then the absolute value
13779 is the same as the ordinary value. */
13780 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13783 /* If the value is negative, then the absolute value is
13788 wide_int val
= wi::neg (arg0
, &overflow
);
13789 t
= force_fit_type (type
, val
, -1,
13790 overflow
| TREE_OVERFLOW (arg0
));
13796 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13797 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13803 gcc_unreachable ();
13809 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13810 constant. TYPE is the type of the result. */
13813 fold_not_const (const_tree arg0
, tree type
)
13815 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13817 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13820 /* Given CODE, a relational operator, the target type, TYPE and two
13821 constant operands OP0 and OP1, return the result of the
13822 relational operation. If the result is not a compile time
13823 constant, then return NULL_TREE. */
13826 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13828 int result
, invert
;
13830 /* From here on, the only cases we handle are when the result is
13831 known to be a constant. */
13833 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13835 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13836 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13838 /* Handle the cases where either operand is a NaN. */
13839 if (real_isnan (c0
) || real_isnan (c1
))
13849 case UNORDERED_EXPR
:
13863 if (flag_trapping_math
)
13869 gcc_unreachable ();
13872 return constant_boolean_node (result
, type
);
13875 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13878 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13880 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13881 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13882 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13885 /* Handle equality/inequality of complex constants. */
13886 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13888 tree rcond
= fold_relational_const (code
, type
,
13889 TREE_REALPART (op0
),
13890 TREE_REALPART (op1
));
13891 tree icond
= fold_relational_const (code
, type
,
13892 TREE_IMAGPART (op0
),
13893 TREE_IMAGPART (op1
));
13894 if (code
== EQ_EXPR
)
13895 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13896 else if (code
== NE_EXPR
)
13897 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13902 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13904 if (!VECTOR_TYPE_P (type
))
13906 /* Have vector comparison with scalar boolean result. */
13907 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
13908 && VECTOR_CST_NELTS (op0
) == VECTOR_CST_NELTS (op1
));
13909 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (op0
); i
++)
13911 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13912 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13913 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
13914 if (tmp
== NULL_TREE
)
13916 if (integer_zerop (tmp
))
13917 return constant_boolean_node (false, type
);
13919 return constant_boolean_node (true, type
);
13921 unsigned count
= VECTOR_CST_NELTS (op0
);
13922 tree
*elts
= XALLOCAVEC (tree
, count
);
13923 gcc_assert (VECTOR_CST_NELTS (op1
) == count
13924 && TYPE_VECTOR_SUBPARTS (type
) == count
);
13926 for (unsigned i
= 0; i
< count
; i
++)
13928 tree elem_type
= TREE_TYPE (type
);
13929 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13930 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13932 tree tem
= fold_relational_const (code
, elem_type
,
13935 if (tem
== NULL_TREE
)
13938 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
13941 return build_vector (type
, elts
);
13944 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13946 To compute GT, swap the arguments and do LT.
13947 To compute GE, do LT and invert the result.
13948 To compute LE, swap the arguments, do LT and invert the result.
13949 To compute NE, do EQ and invert the result.
13951 Therefore, the code below must handle only EQ and LT. */
13953 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13955 std::swap (op0
, op1
);
13956 code
= swap_tree_comparison (code
);
13959 /* Note that it is safe to invert for real values here because we
13960 have already handled the one case that it matters. */
13963 if (code
== NE_EXPR
|| code
== GE_EXPR
)
13966 code
= invert_tree_comparison (code
, false);
13969 /* Compute a result for LT or EQ if args permit;
13970 Otherwise return T. */
13971 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
13973 if (code
== EQ_EXPR
)
13974 result
= tree_int_cst_equal (op0
, op1
);
13976 result
= tree_int_cst_lt (op0
, op1
);
13983 return constant_boolean_node (result
, type
);
13986 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13987 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13991 fold_build_cleanup_point_expr (tree type
, tree expr
)
13993 /* If the expression does not have side effects then we don't have to wrap
13994 it with a cleanup point expression. */
13995 if (!TREE_SIDE_EFFECTS (expr
))
13998 /* If the expression is a return, check to see if the expression inside the
13999 return has no side effects or the right hand side of the modify expression
14000 inside the return. If either don't have side effects set we don't need to
14001 wrap the expression in a cleanup point expression. Note we don't check the
14002 left hand side of the modify because it should always be a return decl. */
14003 if (TREE_CODE (expr
) == RETURN_EXPR
)
14005 tree op
= TREE_OPERAND (expr
, 0);
14006 if (!op
|| !TREE_SIDE_EFFECTS (op
))
14008 op
= TREE_OPERAND (op
, 1);
14009 if (!TREE_SIDE_EFFECTS (op
))
14013 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
14016 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14017 of an indirection through OP0, or NULL_TREE if no simplification is
14021 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
14027 subtype
= TREE_TYPE (sub
);
14028 if (!POINTER_TYPE_P (subtype
)
14029 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0
)))
14032 if (TREE_CODE (sub
) == ADDR_EXPR
)
14034 tree op
= TREE_OPERAND (sub
, 0);
14035 tree optype
= TREE_TYPE (op
);
14036 /* *&CONST_DECL -> to the value of the const decl. */
14037 if (TREE_CODE (op
) == CONST_DECL
)
14038 return DECL_INITIAL (op
);
14039 /* *&p => p; make sure to handle *&"str"[cst] here. */
14040 if (type
== optype
)
14042 tree fop
= fold_read_from_constant_string (op
);
14048 /* *(foo *)&fooarray => fooarray[0] */
14049 else if (TREE_CODE (optype
) == ARRAY_TYPE
14050 && type
== TREE_TYPE (optype
)
14051 && (!in_gimple_form
14052 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14054 tree type_domain
= TYPE_DOMAIN (optype
);
14055 tree min_val
= size_zero_node
;
14056 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14057 min_val
= TYPE_MIN_VALUE (type_domain
);
14059 && TREE_CODE (min_val
) != INTEGER_CST
)
14061 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
14062 NULL_TREE
, NULL_TREE
);
14064 /* *(foo *)&complexfoo => __real__ complexfoo */
14065 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14066 && type
== TREE_TYPE (optype
))
14067 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14068 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14069 else if (TREE_CODE (optype
) == VECTOR_TYPE
14070 && type
== TREE_TYPE (optype
))
14072 tree part_width
= TYPE_SIZE (type
);
14073 tree index
= bitsize_int (0);
14074 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14078 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14079 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14081 tree op00
= TREE_OPERAND (sub
, 0);
14082 tree op01
= TREE_OPERAND (sub
, 1);
14085 if (TREE_CODE (op00
) == ADDR_EXPR
)
14088 op00
= TREE_OPERAND (op00
, 0);
14089 op00type
= TREE_TYPE (op00
);
14091 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14092 if (TREE_CODE (op00type
) == VECTOR_TYPE
14093 && type
== TREE_TYPE (op00type
))
14095 tree part_width
= TYPE_SIZE (type
);
14096 unsigned HOST_WIDE_INT max_offset
14097 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14098 * TYPE_VECTOR_SUBPARTS (op00type
));
14099 if (tree_int_cst_sign_bit (op01
) == 0
14100 && compare_tree_int (op01
, max_offset
) == -1)
14102 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (op01
);
14103 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14104 tree index
= bitsize_int (indexi
);
14105 return fold_build3_loc (loc
,
14106 BIT_FIELD_REF
, type
, op00
,
14107 part_width
, index
);
14110 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14111 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14112 && type
== TREE_TYPE (op00type
))
14114 tree size
= TYPE_SIZE_UNIT (type
);
14115 if (tree_int_cst_equal (size
, op01
))
14116 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14118 /* ((foo *)&fooarray)[1] => fooarray[1] */
14119 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14120 && type
== TREE_TYPE (op00type
))
14122 tree type_domain
= TYPE_DOMAIN (op00type
);
14123 tree min_val
= size_zero_node
;
14124 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14125 min_val
= TYPE_MIN_VALUE (type_domain
);
14126 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
14127 TYPE_SIZE_UNIT (type
));
14128 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
14129 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14130 NULL_TREE
, NULL_TREE
);
14135 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14136 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14137 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14138 && (!in_gimple_form
14139 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14142 tree min_val
= size_zero_node
;
14143 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14144 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14145 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14146 min_val
= TYPE_MIN_VALUE (type_domain
);
14148 && TREE_CODE (min_val
) != INTEGER_CST
)
14150 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14157 /* Builds an expression for an indirection through T, simplifying some
14161 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14163 tree type
= TREE_TYPE (TREE_TYPE (t
));
14164 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14169 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14172 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14175 fold_indirect_ref_loc (location_t loc
, tree t
)
14177 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14185 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14186 whose result is ignored. The type of the returned tree need not be
14187 the same as the original expression. */
14190 fold_ignored_result (tree t
)
14192 if (!TREE_SIDE_EFFECTS (t
))
14193 return integer_zero_node
;
14196 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14199 t
= TREE_OPERAND (t
, 0);
14203 case tcc_comparison
:
14204 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14205 t
= TREE_OPERAND (t
, 0);
14206 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14207 t
= TREE_OPERAND (t
, 1);
14212 case tcc_expression
:
14213 switch (TREE_CODE (t
))
14215 case COMPOUND_EXPR
:
14216 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14218 t
= TREE_OPERAND (t
, 0);
14222 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14223 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14225 t
= TREE_OPERAND (t
, 0);
14238 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14241 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14243 tree div
= NULL_TREE
;
14248 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14249 have to do anything. Only do this when we are not given a const,
14250 because in that case, this check is more expensive than just
14252 if (TREE_CODE (value
) != INTEGER_CST
)
14254 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14256 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14260 /* If divisor is a power of two, simplify this to bit manipulation. */
14261 if (pow2_or_zerop (divisor
))
14263 if (TREE_CODE (value
) == INTEGER_CST
)
14265 wide_int val
= value
;
14268 if ((val
& (divisor
- 1)) == 0)
14271 overflow_p
= TREE_OVERFLOW (value
);
14272 val
+= divisor
- 1;
14273 val
&= (int) -divisor
;
14277 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14283 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14284 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14285 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14286 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14292 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14293 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14294 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14300 /* Likewise, but round down. */
14303 round_down_loc (location_t loc
, tree value
, int divisor
)
14305 tree div
= NULL_TREE
;
14307 gcc_assert (divisor
> 0);
14311 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14312 have to do anything. Only do this when we are not given a const,
14313 because in that case, this check is more expensive than just
14315 if (TREE_CODE (value
) != INTEGER_CST
)
14317 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14319 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14323 /* If divisor is a power of two, simplify this to bit manipulation. */
14324 if (pow2_or_zerop (divisor
))
14328 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14329 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14334 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14335 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14336 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14342 /* Returns the pointer to the base of the object addressed by EXP and
14343 extracts the information about the offset of the access, storing it
14344 to PBITPOS and POFFSET. */
14347 split_address_to_core_and_offset (tree exp
,
14348 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14352 int unsignedp
, reversep
, volatilep
;
14353 HOST_WIDE_INT bitsize
;
14354 location_t loc
= EXPR_LOCATION (exp
);
14356 if (TREE_CODE (exp
) == ADDR_EXPR
)
14358 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14359 poffset
, &mode
, &unsignedp
, &reversep
,
14361 core
= build_fold_addr_expr_loc (loc
, core
);
14363 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
14365 core
= TREE_OPERAND (exp
, 0);
14368 *poffset
= TREE_OPERAND (exp
, 1);
14369 if (TREE_CODE (*poffset
) == INTEGER_CST
)
14371 offset_int tem
= wi::sext (wi::to_offset (*poffset
),
14372 TYPE_PRECISION (TREE_TYPE (*poffset
)));
14373 tem
<<= LOG2_BITS_PER_UNIT
;
14374 if (wi::fits_shwi_p (tem
))
14376 *pbitpos
= tem
.to_shwi ();
14377 *poffset
= NULL_TREE
;
14385 *poffset
= NULL_TREE
;
14391 /* Returns true if addresses of E1 and E2 differ by a constant, false
14392 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14395 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14398 HOST_WIDE_INT bitpos1
, bitpos2
;
14399 tree toffset1
, toffset2
, tdiff
, type
;
14401 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14402 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14404 if (bitpos1
% BITS_PER_UNIT
!= 0
14405 || bitpos2
% BITS_PER_UNIT
!= 0
14406 || !operand_equal_p (core1
, core2
, 0))
14409 if (toffset1
&& toffset2
)
14411 type
= TREE_TYPE (toffset1
);
14412 if (type
!= TREE_TYPE (toffset2
))
14413 toffset2
= fold_convert (type
, toffset2
);
14415 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14416 if (!cst_and_fits_in_hwi (tdiff
))
14419 *diff
= int_cst_value (tdiff
);
14421 else if (toffset1
|| toffset2
)
14423 /* If only one of the offsets is non-constant, the difference cannot
14430 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14434 /* Return OFF converted to a pointer offset type suitable as offset for
14435 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14437 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14439 return fold_convert_loc (loc
, sizetype
, off
);
14442 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14444 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14446 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14447 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14450 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14452 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14454 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14455 ptr
, size_int (off
));
14458 /* Return a char pointer for a C string if it is a string constant
14459 or sum of string constant and integer constant. We only support
14460 string constants properly terminated with '\0' character.
14461 If STRLEN is a valid pointer, length (including terminating character)
14462 of returned string is stored to the argument. */
14465 c_getstr (tree src
, unsigned HOST_WIDE_INT
*strlen
)
14472 src
= string_constant (src
, &offset_node
);
14476 unsigned HOST_WIDE_INT offset
= 0;
14477 if (offset_node
!= NULL_TREE
)
14479 if (!tree_fits_uhwi_p (offset_node
))
14482 offset
= tree_to_uhwi (offset_node
);
14485 unsigned HOST_WIDE_INT string_length
= TREE_STRING_LENGTH (src
);
14486 const char *string
= TREE_STRING_POINTER (src
);
14488 /* Support only properly null-terminated strings. */
14489 if (string_length
== 0
14490 || string
[string_length
- 1] != '\0'
14491 || offset
>= string_length
)
14495 *strlen
= string_length
- offset
;
14496 return string
+ offset
;
14501 namespace selftest
{
14503 /* Helper functions for writing tests of folding trees. */
14505 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14508 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
14511 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
14514 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14515 wrapping WRAPPED_EXPR. */
14518 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
14521 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
14522 ASSERT_NE (wrapped_expr
, result
);
14523 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
14524 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
14527 /* Verify that various arithmetic binary operations are folded
14531 test_arithmetic_folding ()
14533 tree type
= integer_type_node
;
14534 tree x
= create_tmp_var_raw (type
, "x");
14535 tree zero
= build_zero_cst (type
);
14536 tree one
= build_int_cst (type
, 1);
14539 /* 1 <-- (0 + 1) */
14540 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
14542 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
14545 /* (nonlvalue)x <-- (x + 0) */
14546 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
14550 /* 0 <-- (x - x) */
14551 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
14553 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
14556 /* Multiplication. */
14557 /* 0 <-- (x * 0) */
14558 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
14561 /* (nonlvalue)x <-- (x * 1) */
14562 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
14566 /* Verify that various binary operations on vectors are folded
14570 test_vector_folding ()
14572 tree inner_type
= integer_type_node
;
14573 tree type
= build_vector_type (inner_type
, 4);
14574 tree zero
= build_zero_cst (type
);
14575 tree one
= build_one_cst (type
);
14577 /* Verify equality tests that return a scalar boolean result. */
14578 tree res_type
= boolean_type_node
;
14579 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
14580 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
14581 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
14582 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
14585 /* Run all of the selftests within this file. */
14588 fold_const_c_tests ()
14590 test_arithmetic_folding ();
14591 test_vector_folding ();
14594 } // namespace selftest
14596 #endif /* CHECKING_P */