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
) == MINUS_EXPR
)
802 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
804 tree op0
= TREE_OPERAND (in
, 0);
805 tree op1
= TREE_OPERAND (in
, 1);
806 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
807 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
809 /* First see if either of the operands is a literal, then a constant. */
810 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
811 || TREE_CODE (op0
) == FIXED_CST
)
812 *litp
= op0
, op0
= 0;
813 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
814 || TREE_CODE (op1
) == FIXED_CST
)
815 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
817 if (op0
!= 0 && TREE_CONSTANT (op0
))
818 *conp
= op0
, op0
= 0;
819 else if (op1
!= 0 && TREE_CONSTANT (op1
))
820 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
822 /* If we haven't dealt with either operand, this is not a case we can
823 decompose. Otherwise, VAR is either of the ones remaining, if any. */
824 if (op0
!= 0 && op1
!= 0)
829 var
= op1
, neg_var_p
= neg1_p
;
831 /* Now do any needed negations. */
833 *minus_litp
= *litp
, *litp
= 0;
835 *conp
= negate_expr (*conp
);
836 if (neg_var_p
&& var
)
838 /* Convert to TYPE before negating. */
839 var
= fold_convert_loc (loc
, type
, var
);
840 var
= negate_expr (var
);
843 else if (TREE_CONSTANT (in
))
845 else if (TREE_CODE (in
) == BIT_NOT_EXPR
846 && code
== PLUS_EXPR
)
848 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
849 when IN is constant. */
850 *minus_litp
= build_one_cst (TREE_TYPE (in
));
851 var
= negate_expr (TREE_OPERAND (in
, 0));
859 *minus_litp
= *litp
, *litp
= 0;
860 else if (*minus_litp
)
861 *litp
= *minus_litp
, *minus_litp
= 0;
862 *conp
= negate_expr (*conp
);
865 /* Convert to TYPE before negating. */
866 var
= fold_convert_loc (loc
, type
, var
);
867 var
= negate_expr (var
);
874 /* Re-associate trees split by the above function. T1 and T2 are
875 either expressions to associate or null. Return the new
876 expression, if any. LOC is the location of the new expression. If
877 we build an operation, do it in TYPE and with CODE. */
880 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
887 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
888 try to fold this since we will have infinite recursion. But do
889 deal with any NEGATE_EXPRs. */
890 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
891 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
893 if (code
== PLUS_EXPR
)
895 if (TREE_CODE (t1
) == NEGATE_EXPR
)
896 return build2_loc (loc
, MINUS_EXPR
, type
,
897 fold_convert_loc (loc
, type
, t2
),
898 fold_convert_loc (loc
, type
,
899 TREE_OPERAND (t1
, 0)));
900 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
901 return build2_loc (loc
, MINUS_EXPR
, type
,
902 fold_convert_loc (loc
, type
, t1
),
903 fold_convert_loc (loc
, type
,
904 TREE_OPERAND (t2
, 0)));
905 else if (integer_zerop (t2
))
906 return fold_convert_loc (loc
, type
, t1
);
908 else if (code
== MINUS_EXPR
)
910 if (integer_zerop (t2
))
911 return fold_convert_loc (loc
, type
, t1
);
914 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
915 fold_convert_loc (loc
, type
, t2
));
918 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
919 fold_convert_loc (loc
, type
, t2
));
922 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
923 for use in int_const_binop, size_binop and size_diffop. */
926 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
928 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
930 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
945 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
946 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
947 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
951 /* Combine two integer constants ARG1 and ARG2 under operation CODE
952 to produce a new constant. Return NULL_TREE if we don't know how
953 to evaluate CODE at compile-time. */
956 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
961 tree type
= TREE_TYPE (arg1
);
962 signop sign
= TYPE_SIGN (type
);
963 bool overflow
= false;
965 wide_int arg2
= wi::to_wide (parg2
, TYPE_PRECISION (type
));
970 res
= wi::bit_or (arg1
, arg2
);
974 res
= wi::bit_xor (arg1
, arg2
);
978 res
= wi::bit_and (arg1
, arg2
);
983 if (wi::neg_p (arg2
))
986 if (code
== RSHIFT_EXPR
)
992 if (code
== RSHIFT_EXPR
)
993 /* It's unclear from the C standard whether shifts can overflow.
994 The following code ignores overflow; perhaps a C standard
995 interpretation ruling is needed. */
996 res
= wi::rshift (arg1
, arg2
, sign
);
998 res
= wi::lshift (arg1
, arg2
);
1003 if (wi::neg_p (arg2
))
1006 if (code
== RROTATE_EXPR
)
1007 code
= LROTATE_EXPR
;
1009 code
= RROTATE_EXPR
;
1012 if (code
== RROTATE_EXPR
)
1013 res
= wi::rrotate (arg1
, arg2
);
1015 res
= wi::lrotate (arg1
, arg2
);
1019 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1023 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1027 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1030 case MULT_HIGHPART_EXPR
:
1031 res
= wi::mul_high (arg1
, arg2
, sign
);
1034 case TRUNC_DIV_EXPR
:
1035 case EXACT_DIV_EXPR
:
1038 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1041 case FLOOR_DIV_EXPR
:
1044 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1050 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1053 case ROUND_DIV_EXPR
:
1056 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1059 case TRUNC_MOD_EXPR
:
1062 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1065 case FLOOR_MOD_EXPR
:
1068 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1074 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1077 case ROUND_MOD_EXPR
:
1080 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1084 res
= wi::min (arg1
, arg2
, sign
);
1088 res
= wi::max (arg1
, arg2
, sign
);
1095 t
= force_fit_type (type
, res
, overflowable
,
1096 (((sign
== SIGNED
|| overflowable
== -1)
1098 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1104 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1106 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1109 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1110 constant. We assume ARG1 and ARG2 have the same data type, or at least
1111 are the same kind of constant and the same machine mode. Return zero if
1112 combining the constants is not allowed in the current operating mode. */
1115 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1117 /* Sanity check for the recursive cases. */
1124 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1126 if (code
== POINTER_PLUS_EXPR
)
1127 return int_const_binop (PLUS_EXPR
,
1128 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1130 return int_const_binop (code
, arg1
, arg2
);
1133 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1138 REAL_VALUE_TYPE value
;
1139 REAL_VALUE_TYPE result
;
1143 /* The following codes are handled by real_arithmetic. */
1158 d1
= TREE_REAL_CST (arg1
);
1159 d2
= TREE_REAL_CST (arg2
);
1161 type
= TREE_TYPE (arg1
);
1162 mode
= TYPE_MODE (type
);
1164 /* Don't perform operation if we honor signaling NaNs and
1165 either operand is a signaling NaN. */
1166 if (HONOR_SNANS (mode
)
1167 && (REAL_VALUE_ISSIGNALING_NAN (d1
)
1168 || REAL_VALUE_ISSIGNALING_NAN (d2
)))
1171 /* Don't perform operation if it would raise a division
1172 by zero exception. */
1173 if (code
== RDIV_EXPR
1174 && real_equal (&d2
, &dconst0
)
1175 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1178 /* If either operand is a NaN, just return it. Otherwise, set up
1179 for floating-point trap; we return an overflow. */
1180 if (REAL_VALUE_ISNAN (d1
))
1182 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1185 t
= build_real (type
, d1
);
1188 else if (REAL_VALUE_ISNAN (d2
))
1190 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1193 t
= build_real (type
, d2
);
1197 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1198 real_convert (&result
, mode
, &value
);
1200 /* Don't constant fold this floating point operation if
1201 the result has overflowed and flag_trapping_math. */
1202 if (flag_trapping_math
1203 && MODE_HAS_INFINITIES (mode
)
1204 && REAL_VALUE_ISINF (result
)
1205 && !REAL_VALUE_ISINF (d1
)
1206 && !REAL_VALUE_ISINF (d2
))
1209 /* Don't constant fold this floating point operation if the
1210 result may dependent upon the run-time rounding mode and
1211 flag_rounding_math is set, or if GCC's software emulation
1212 is unable to accurately represent the result. */
1213 if ((flag_rounding_math
1214 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1215 && (inexact
|| !real_identical (&result
, &value
)))
1218 t
= build_real (type
, result
);
1220 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1224 if (TREE_CODE (arg1
) == FIXED_CST
)
1226 FIXED_VALUE_TYPE f1
;
1227 FIXED_VALUE_TYPE f2
;
1228 FIXED_VALUE_TYPE result
;
1233 /* The following codes are handled by fixed_arithmetic. */
1239 case TRUNC_DIV_EXPR
:
1240 if (TREE_CODE (arg2
) != FIXED_CST
)
1242 f2
= TREE_FIXED_CST (arg2
);
1248 if (TREE_CODE (arg2
) != INTEGER_CST
)
1251 f2
.data
.high
= w2
.elt (1);
1252 f2
.data
.low
= w2
.elt (0);
1261 f1
= TREE_FIXED_CST (arg1
);
1262 type
= TREE_TYPE (arg1
);
1263 sat_p
= TYPE_SATURATING (type
);
1264 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1265 t
= build_fixed (type
, result
);
1266 /* Propagate overflow flags. */
1267 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1268 TREE_OVERFLOW (t
) = 1;
1272 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1274 tree type
= TREE_TYPE (arg1
);
1275 tree r1
= TREE_REALPART (arg1
);
1276 tree i1
= TREE_IMAGPART (arg1
);
1277 tree r2
= TREE_REALPART (arg2
);
1278 tree i2
= TREE_IMAGPART (arg2
);
1285 real
= const_binop (code
, r1
, r2
);
1286 imag
= const_binop (code
, i1
, i2
);
1290 if (COMPLEX_FLOAT_TYPE_P (type
))
1291 return do_mpc_arg2 (arg1
, arg2
, type
,
1292 /* do_nonfinite= */ folding_initializer
,
1295 real
= const_binop (MINUS_EXPR
,
1296 const_binop (MULT_EXPR
, r1
, r2
),
1297 const_binop (MULT_EXPR
, i1
, i2
));
1298 imag
= const_binop (PLUS_EXPR
,
1299 const_binop (MULT_EXPR
, r1
, i2
),
1300 const_binop (MULT_EXPR
, i1
, r2
));
1304 if (COMPLEX_FLOAT_TYPE_P (type
))
1305 return do_mpc_arg2 (arg1
, arg2
, type
,
1306 /* do_nonfinite= */ folding_initializer
,
1309 case TRUNC_DIV_EXPR
:
1311 case FLOOR_DIV_EXPR
:
1312 case ROUND_DIV_EXPR
:
1313 if (flag_complex_method
== 0)
1315 /* Keep this algorithm in sync with
1316 tree-complex.c:expand_complex_div_straight().
1318 Expand complex division to scalars, straightforward algorithm.
1319 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1323 = const_binop (PLUS_EXPR
,
1324 const_binop (MULT_EXPR
, r2
, r2
),
1325 const_binop (MULT_EXPR
, i2
, i2
));
1327 = const_binop (PLUS_EXPR
,
1328 const_binop (MULT_EXPR
, r1
, r2
),
1329 const_binop (MULT_EXPR
, i1
, i2
));
1331 = const_binop (MINUS_EXPR
,
1332 const_binop (MULT_EXPR
, i1
, r2
),
1333 const_binop (MULT_EXPR
, r1
, i2
));
1335 real
= const_binop (code
, t1
, magsquared
);
1336 imag
= const_binop (code
, t2
, magsquared
);
1340 /* Keep this algorithm in sync with
1341 tree-complex.c:expand_complex_div_wide().
1343 Expand complex division to scalars, modified algorithm to minimize
1344 overflow with wide input ranges. */
1345 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1346 fold_abs_const (r2
, TREE_TYPE (type
)),
1347 fold_abs_const (i2
, TREE_TYPE (type
)));
1349 if (integer_nonzerop (compare
))
1351 /* In the TRUE branch, we compute
1353 div = (br * ratio) + bi;
1354 tr = (ar * ratio) + ai;
1355 ti = (ai * ratio) - ar;
1358 tree ratio
= const_binop (code
, r2
, i2
);
1359 tree div
= const_binop (PLUS_EXPR
, i2
,
1360 const_binop (MULT_EXPR
, r2
, ratio
));
1361 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1362 real
= const_binop (PLUS_EXPR
, real
, i1
);
1363 real
= const_binop (code
, real
, div
);
1365 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1366 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1367 imag
= const_binop (code
, imag
, div
);
1371 /* In the FALSE branch, we compute
1373 divisor = (d * ratio) + c;
1374 tr = (b * ratio) + a;
1375 ti = b - (a * ratio);
1378 tree ratio
= const_binop (code
, i2
, r2
);
1379 tree div
= const_binop (PLUS_EXPR
, r2
,
1380 const_binop (MULT_EXPR
, i2
, ratio
));
1382 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1383 real
= const_binop (PLUS_EXPR
, real
, r1
);
1384 real
= const_binop (code
, real
, div
);
1386 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1387 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1388 imag
= const_binop (code
, imag
, div
);
1398 return build_complex (type
, real
, imag
);
1401 if (TREE_CODE (arg1
) == VECTOR_CST
1402 && TREE_CODE (arg2
) == VECTOR_CST
)
1404 tree type
= TREE_TYPE (arg1
);
1405 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1406 tree
*elts
= XALLOCAVEC (tree
, count
);
1408 for (i
= 0; i
< count
; i
++)
1410 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1411 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1413 elts
[i
] = const_binop (code
, elem1
, elem2
);
1415 /* It is possible that const_binop cannot handle the given
1416 code and return NULL_TREE */
1417 if (elts
[i
] == NULL_TREE
)
1421 return build_vector (type
, elts
);
1424 /* Shifts allow a scalar offset for a vector. */
1425 if (TREE_CODE (arg1
) == VECTOR_CST
1426 && TREE_CODE (arg2
) == INTEGER_CST
)
1428 tree type
= TREE_TYPE (arg1
);
1429 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1430 tree
*elts
= XALLOCAVEC (tree
, count
);
1432 for (i
= 0; i
< count
; i
++)
1434 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1436 elts
[i
] = const_binop (code
, elem1
, arg2
);
1438 /* It is possible that const_binop cannot handle the given
1439 code and return NULL_TREE. */
1440 if (elts
[i
] == NULL_TREE
)
1444 return build_vector (type
, elts
);
1449 /* Overload that adds a TYPE parameter to be able to dispatch
1450 to fold_relational_const. */
1453 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1455 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1456 return fold_relational_const (code
, type
, arg1
, arg2
);
1458 /* ??? Until we make the const_binop worker take the type of the
1459 result as argument put those cases that need it here. */
1463 if ((TREE_CODE (arg1
) == REAL_CST
1464 && TREE_CODE (arg2
) == REAL_CST
)
1465 || (TREE_CODE (arg1
) == INTEGER_CST
1466 && TREE_CODE (arg2
) == INTEGER_CST
))
1467 return build_complex (type
, arg1
, arg2
);
1470 case VEC_PACK_TRUNC_EXPR
:
1471 case VEC_PACK_FIX_TRUNC_EXPR
:
1473 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1476 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1477 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1478 if (TREE_CODE (arg1
) != VECTOR_CST
1479 || TREE_CODE (arg2
) != VECTOR_CST
)
1482 elts
= XALLOCAVEC (tree
, nelts
);
1483 if (!vec_cst_ctor_to_array (arg1
, elts
)
1484 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1487 for (i
= 0; i
< nelts
; i
++)
1489 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1490 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1491 TREE_TYPE (type
), elts
[i
]);
1492 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1496 return build_vector (type
, elts
);
1499 case VEC_WIDEN_MULT_LO_EXPR
:
1500 case VEC_WIDEN_MULT_HI_EXPR
:
1501 case VEC_WIDEN_MULT_EVEN_EXPR
:
1502 case VEC_WIDEN_MULT_ODD_EXPR
:
1504 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1505 unsigned int out
, ofs
, scale
;
1508 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1509 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1510 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1513 elts
= XALLOCAVEC (tree
, nelts
* 4);
1514 if (!vec_cst_ctor_to_array (arg1
, elts
)
1515 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1518 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1519 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1520 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1521 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1522 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1524 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1527 for (out
= 0; out
< nelts
; out
++)
1529 unsigned int in1
= (out
<< scale
) + ofs
;
1530 unsigned int in2
= in1
+ nelts
* 2;
1533 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1534 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1536 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1538 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1539 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1543 return build_vector (type
, elts
);
1549 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1552 /* Make sure type and arg0 have the same saturating flag. */
1553 gcc_checking_assert (TYPE_SATURATING (type
)
1554 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1556 return const_binop (code
, arg1
, arg2
);
1559 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1560 Return zero if computing the constants is not possible. */
1563 const_unop (enum tree_code code
, tree type
, tree arg0
)
1565 /* Don't perform the operation, other than NEGATE and ABS, if
1566 flag_signaling_nans is on and the operand is a signaling NaN. */
1567 if (TREE_CODE (arg0
) == REAL_CST
1568 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
1569 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0
))
1570 && code
!= NEGATE_EXPR
1571 && code
!= ABS_EXPR
)
1578 case FIX_TRUNC_EXPR
:
1579 case FIXED_CONVERT_EXPR
:
1580 return fold_convert_const (code
, type
, arg0
);
1582 case ADDR_SPACE_CONVERT_EXPR
:
1583 /* If the source address is 0, and the source address space
1584 cannot have a valid object at 0, fold to dest type null. */
1585 if (integer_zerop (arg0
)
1586 && !(targetm
.addr_space
.zero_address_valid
1587 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
))))))
1588 return fold_convert_const (code
, type
, arg0
);
1591 case VIEW_CONVERT_EXPR
:
1592 return fold_view_convert_expr (type
, arg0
);
1596 /* Can't call fold_negate_const directly here as that doesn't
1597 handle all cases and we might not be able to negate some
1599 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1600 if (tem
&& CONSTANT_CLASS_P (tem
))
1606 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1607 return fold_abs_const (arg0
, type
);
1611 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1613 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1615 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1620 if (TREE_CODE (arg0
) == INTEGER_CST
)
1621 return fold_not_const (arg0
, type
);
1622 /* Perform BIT_NOT_EXPR on each element individually. */
1623 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1627 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1629 elements
= XALLOCAVEC (tree
, count
);
1630 for (i
= 0; i
< count
; i
++)
1632 elem
= VECTOR_CST_ELT (arg0
, i
);
1633 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1634 if (elem
== NULL_TREE
)
1639 return build_vector (type
, elements
);
1643 case TRUTH_NOT_EXPR
:
1644 if (TREE_CODE (arg0
) == INTEGER_CST
)
1645 return constant_boolean_node (integer_zerop (arg0
), type
);
1649 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1650 return fold_convert (type
, TREE_REALPART (arg0
));
1654 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1655 return fold_convert (type
, TREE_IMAGPART (arg0
));
1658 case VEC_UNPACK_LO_EXPR
:
1659 case VEC_UNPACK_HI_EXPR
:
1660 case VEC_UNPACK_FLOAT_LO_EXPR
:
1661 case VEC_UNPACK_FLOAT_HI_EXPR
:
1663 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1665 enum tree_code subcode
;
1667 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1668 if (TREE_CODE (arg0
) != VECTOR_CST
)
1671 elts
= XALLOCAVEC (tree
, nelts
* 2);
1672 if (!vec_cst_ctor_to_array (arg0
, elts
))
1675 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1676 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1679 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1682 subcode
= FLOAT_EXPR
;
1684 for (i
= 0; i
< nelts
; i
++)
1686 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1687 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1691 return build_vector (type
, elts
);
1694 case REDUC_MIN_EXPR
:
1695 case REDUC_MAX_EXPR
:
1696 case REDUC_PLUS_EXPR
:
1698 unsigned int nelts
, i
;
1700 enum tree_code subcode
;
1702 if (TREE_CODE (arg0
) != VECTOR_CST
)
1704 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1706 elts
= XALLOCAVEC (tree
, nelts
);
1707 if (!vec_cst_ctor_to_array (arg0
, elts
))
1712 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1713 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1714 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1715 default: gcc_unreachable ();
1718 for (i
= 1; i
< nelts
; i
++)
1720 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1721 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1735 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1736 indicates which particular sizetype to create. */
1739 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1741 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1744 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1745 is a tree code. The type of the result is taken from the operands.
1746 Both must be equivalent integer types, ala int_binop_types_match_p.
1747 If the operands are constant, so is the result. */
1750 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1752 tree type
= TREE_TYPE (arg0
);
1754 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1755 return error_mark_node
;
1757 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1760 /* Handle the special case of two integer constants faster. */
1761 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1763 /* And some specific cases even faster than that. */
1764 if (code
== PLUS_EXPR
)
1766 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1768 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1771 else if (code
== MINUS_EXPR
)
1773 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1776 else if (code
== MULT_EXPR
)
1778 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1782 /* Handle general case of two integer constants. For sizetype
1783 constant calculations we always want to know about overflow,
1784 even in the unsigned case. */
1785 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1788 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1791 /* Given two values, either both of sizetype or both of bitsizetype,
1792 compute the difference between the two values. Return the value
1793 in signed type corresponding to the type of the operands. */
1796 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1798 tree type
= TREE_TYPE (arg0
);
1801 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1804 /* If the type is already signed, just do the simple thing. */
1805 if (!TYPE_UNSIGNED (type
))
1806 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1808 if (type
== sizetype
)
1810 else if (type
== bitsizetype
)
1811 ctype
= sbitsizetype
;
1813 ctype
= signed_type_for (type
);
1815 /* If either operand is not a constant, do the conversions to the signed
1816 type and subtract. The hardware will do the right thing with any
1817 overflow in the subtraction. */
1818 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1819 return size_binop_loc (loc
, MINUS_EXPR
,
1820 fold_convert_loc (loc
, ctype
, arg0
),
1821 fold_convert_loc (loc
, ctype
, arg1
));
1823 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1824 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1825 overflow) and negate (which can't either). Special-case a result
1826 of zero while we're here. */
1827 if (tree_int_cst_equal (arg0
, arg1
))
1828 return build_int_cst (ctype
, 0);
1829 else if (tree_int_cst_lt (arg1
, arg0
))
1830 return fold_convert_loc (loc
, ctype
,
1831 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1833 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1834 fold_convert_loc (loc
, ctype
,
1835 size_binop_loc (loc
,
1840 /* A subroutine of fold_convert_const handling conversions of an
1841 INTEGER_CST to another integer type. */
1844 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1846 /* Given an integer constant, make new constant with new type,
1847 appropriately sign-extended or truncated. Use widest_int
1848 so that any extension is done according ARG1's type. */
1849 return force_fit_type (type
, wi::to_widest (arg1
),
1850 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1851 TREE_OVERFLOW (arg1
));
1854 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1855 to an integer type. */
1858 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1860 bool overflow
= false;
1863 /* The following code implements the floating point to integer
1864 conversion rules required by the Java Language Specification,
1865 that IEEE NaNs are mapped to zero and values that overflow
1866 the target precision saturate, i.e. values greater than
1867 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1868 are mapped to INT_MIN. These semantics are allowed by the
1869 C and C++ standards that simply state that the behavior of
1870 FP-to-integer conversion is unspecified upon overflow. */
1874 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1878 case FIX_TRUNC_EXPR
:
1879 real_trunc (&r
, VOIDmode
, &x
);
1886 /* If R is NaN, return zero and show we have an overflow. */
1887 if (REAL_VALUE_ISNAN (r
))
1890 val
= wi::zero (TYPE_PRECISION (type
));
1893 /* See if R is less than the lower bound or greater than the
1898 tree lt
= TYPE_MIN_VALUE (type
);
1899 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1900 if (real_less (&r
, &l
))
1909 tree ut
= TYPE_MAX_VALUE (type
);
1912 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1913 if (real_less (&u
, &r
))
1922 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1924 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1928 /* A subroutine of fold_convert_const handling conversions of a
1929 FIXED_CST to an integer type. */
1932 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1935 double_int temp
, temp_trunc
;
1938 /* Right shift FIXED_CST to temp by fbit. */
1939 temp
= TREE_FIXED_CST (arg1
).data
;
1940 mode
= TREE_FIXED_CST (arg1
).mode
;
1941 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1943 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1944 HOST_BITS_PER_DOUBLE_INT
,
1945 SIGNED_FIXED_POINT_MODE_P (mode
));
1947 /* Left shift temp to temp_trunc by fbit. */
1948 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1949 HOST_BITS_PER_DOUBLE_INT
,
1950 SIGNED_FIXED_POINT_MODE_P (mode
));
1954 temp
= double_int_zero
;
1955 temp_trunc
= double_int_zero
;
1958 /* If FIXED_CST is negative, we need to round the value toward 0.
1959 By checking if the fractional bits are not zero to add 1 to temp. */
1960 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1961 && temp_trunc
.is_negative ()
1962 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1963 temp
+= double_int_one
;
1965 /* Given a fixed-point constant, make new constant with new type,
1966 appropriately sign-extended or truncated. */
1967 t
= force_fit_type (type
, temp
, -1,
1968 (temp
.is_negative ()
1969 && (TYPE_UNSIGNED (type
)
1970 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1971 | TREE_OVERFLOW (arg1
));
1976 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1977 to another floating point type. */
1980 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1982 REAL_VALUE_TYPE value
;
1985 /* Don't perform the operation if flag_signaling_nans is on
1986 and the operand is a signaling NaN. */
1987 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
1988 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1
)))
1991 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1992 t
= build_real (type
, value
);
1994 /* If converting an infinity or NAN to a representation that doesn't
1995 have one, set the overflow bit so that we can produce some kind of
1996 error message at the appropriate point if necessary. It's not the
1997 most user-friendly message, but it's better than nothing. */
1998 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
1999 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
2000 TREE_OVERFLOW (t
) = 1;
2001 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
2002 && !MODE_HAS_NANS (TYPE_MODE (type
)))
2003 TREE_OVERFLOW (t
) = 1;
2004 /* Regular overflow, conversion produced an infinity in a mode that
2005 can't represent them. */
2006 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
2007 && REAL_VALUE_ISINF (value
)
2008 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
2009 TREE_OVERFLOW (t
) = 1;
2011 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2015 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2016 to a floating point type. */
2019 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2021 REAL_VALUE_TYPE value
;
2024 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
2025 t
= build_real (type
, value
);
2027 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2031 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2032 to another fixed-point type. */
2035 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2037 FIXED_VALUE_TYPE value
;
2041 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2042 TYPE_SATURATING (type
));
2043 t
= build_fixed (type
, value
);
2045 /* Propagate overflow flags. */
2046 if (overflow_p
| TREE_OVERFLOW (arg1
))
2047 TREE_OVERFLOW (t
) = 1;
2051 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2052 to a fixed-point type. */
2055 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2057 FIXED_VALUE_TYPE value
;
2062 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2064 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2065 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2066 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? HOST_WIDE_INT_M1
: 0;
2068 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2070 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2071 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2072 TYPE_SATURATING (type
));
2073 t
= build_fixed (type
, value
);
2075 /* Propagate overflow flags. */
2076 if (overflow_p
| TREE_OVERFLOW (arg1
))
2077 TREE_OVERFLOW (t
) = 1;
2081 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2082 to a fixed-point type. */
2085 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2087 FIXED_VALUE_TYPE value
;
2091 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2092 &TREE_REAL_CST (arg1
),
2093 TYPE_SATURATING (type
));
2094 t
= build_fixed (type
, value
);
2096 /* Propagate overflow flags. */
2097 if (overflow_p
| TREE_OVERFLOW (arg1
))
2098 TREE_OVERFLOW (t
) = 1;
2102 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2103 type TYPE. If no simplification can be done return NULL_TREE. */
2106 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2108 if (TREE_TYPE (arg1
) == type
)
2111 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2112 || TREE_CODE (type
) == OFFSET_TYPE
)
2114 if (TREE_CODE (arg1
) == INTEGER_CST
)
2115 return fold_convert_const_int_from_int (type
, arg1
);
2116 else if (TREE_CODE (arg1
) == REAL_CST
)
2117 return fold_convert_const_int_from_real (code
, type
, arg1
);
2118 else if (TREE_CODE (arg1
) == FIXED_CST
)
2119 return fold_convert_const_int_from_fixed (type
, arg1
);
2121 else if (TREE_CODE (type
) == REAL_TYPE
)
2123 if (TREE_CODE (arg1
) == INTEGER_CST
)
2124 return build_real_from_int_cst (type
, arg1
);
2125 else if (TREE_CODE (arg1
) == REAL_CST
)
2126 return fold_convert_const_real_from_real (type
, arg1
);
2127 else if (TREE_CODE (arg1
) == FIXED_CST
)
2128 return fold_convert_const_real_from_fixed (type
, arg1
);
2130 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2132 if (TREE_CODE (arg1
) == FIXED_CST
)
2133 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2134 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2135 return fold_convert_const_fixed_from_int (type
, arg1
);
2136 else if (TREE_CODE (arg1
) == REAL_CST
)
2137 return fold_convert_const_fixed_from_real (type
, arg1
);
2139 else if (TREE_CODE (type
) == VECTOR_TYPE
)
2141 if (TREE_CODE (arg1
) == VECTOR_CST
2142 && TYPE_VECTOR_SUBPARTS (type
) == VECTOR_CST_NELTS (arg1
))
2144 int len
= TYPE_VECTOR_SUBPARTS (type
);
2145 tree elttype
= TREE_TYPE (type
);
2146 tree
*v
= XALLOCAVEC (tree
, len
);
2147 for (int i
= 0; i
< len
; ++i
)
2149 tree elt
= VECTOR_CST_ELT (arg1
, i
);
2150 tree cvt
= fold_convert_const (code
, elttype
, elt
);
2151 if (cvt
== NULL_TREE
)
2155 return build_vector (type
, v
);
2161 /* Construct a vector of zero elements of vector type TYPE. */
2164 build_zero_vector (tree type
)
2168 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2169 return build_vector_from_val (type
, t
);
2172 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2175 fold_convertible_p (const_tree type
, const_tree arg
)
2177 tree orig
= TREE_TYPE (arg
);
2182 if (TREE_CODE (arg
) == ERROR_MARK
2183 || TREE_CODE (type
) == ERROR_MARK
2184 || TREE_CODE (orig
) == ERROR_MARK
)
2187 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2190 switch (TREE_CODE (type
))
2192 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2193 case POINTER_TYPE
: case REFERENCE_TYPE
:
2195 return (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2196 || TREE_CODE (orig
) == OFFSET_TYPE
);
2199 case FIXED_POINT_TYPE
:
2202 return TREE_CODE (type
) == TREE_CODE (orig
);
2209 /* Convert expression ARG to type TYPE. Used by the middle-end for
2210 simple conversions in preference to calling the front-end's convert. */
2213 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2215 tree orig
= TREE_TYPE (arg
);
2221 if (TREE_CODE (arg
) == ERROR_MARK
2222 || TREE_CODE (type
) == ERROR_MARK
2223 || TREE_CODE (orig
) == ERROR_MARK
)
2224 return error_mark_node
;
2226 switch (TREE_CODE (type
))
2229 case REFERENCE_TYPE
:
2230 /* Handle conversions between pointers to different address spaces. */
2231 if (POINTER_TYPE_P (orig
)
2232 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2233 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2234 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2237 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2239 if (TREE_CODE (arg
) == INTEGER_CST
)
2241 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2242 if (tem
!= NULL_TREE
)
2245 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2246 || TREE_CODE (orig
) == OFFSET_TYPE
)
2247 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2248 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2249 return fold_convert_loc (loc
, type
,
2250 fold_build1_loc (loc
, REALPART_EXPR
,
2251 TREE_TYPE (orig
), arg
));
2252 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2253 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2254 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2257 if (TREE_CODE (arg
) == INTEGER_CST
)
2259 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2260 if (tem
!= NULL_TREE
)
2263 else if (TREE_CODE (arg
) == REAL_CST
)
2265 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2266 if (tem
!= NULL_TREE
)
2269 else if (TREE_CODE (arg
) == FIXED_CST
)
2271 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2272 if (tem
!= NULL_TREE
)
2276 switch (TREE_CODE (orig
))
2279 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2280 case POINTER_TYPE
: case REFERENCE_TYPE
:
2281 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2284 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2286 case FIXED_POINT_TYPE
:
2287 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2290 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2291 return fold_convert_loc (loc
, type
, tem
);
2297 case FIXED_POINT_TYPE
:
2298 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2299 || TREE_CODE (arg
) == REAL_CST
)
2301 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2302 if (tem
!= NULL_TREE
)
2303 goto fold_convert_exit
;
2306 switch (TREE_CODE (orig
))
2308 case FIXED_POINT_TYPE
:
2313 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2316 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2317 return fold_convert_loc (loc
, type
, tem
);
2324 switch (TREE_CODE (orig
))
2327 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2328 case POINTER_TYPE
: case REFERENCE_TYPE
:
2330 case FIXED_POINT_TYPE
:
2331 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2332 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2333 fold_convert_loc (loc
, TREE_TYPE (type
),
2334 integer_zero_node
));
2339 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2341 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2342 TREE_OPERAND (arg
, 0));
2343 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2344 TREE_OPERAND (arg
, 1));
2345 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2348 arg
= save_expr (arg
);
2349 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2350 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2351 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2352 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2353 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2361 if (integer_zerop (arg
))
2362 return build_zero_vector (type
);
2363 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2364 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2365 || TREE_CODE (orig
) == VECTOR_TYPE
);
2366 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2369 tem
= fold_ignored_result (arg
);
2370 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2373 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2374 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2378 protected_set_expr_location_unshare (tem
, loc
);
2382 /* Return false if expr can be assumed not to be an lvalue, true
2386 maybe_lvalue_p (const_tree x
)
2388 /* We only need to wrap lvalue tree codes. */
2389 switch (TREE_CODE (x
))
2402 case ARRAY_RANGE_REF
:
2408 case PREINCREMENT_EXPR
:
2409 case PREDECREMENT_EXPR
:
2411 case TRY_CATCH_EXPR
:
2412 case WITH_CLEANUP_EXPR
:
2421 /* Assume the worst for front-end tree codes. */
2422 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2430 /* Return an expr equal to X but certainly not valid as an lvalue. */
2433 non_lvalue_loc (location_t loc
, tree x
)
2435 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2440 if (! maybe_lvalue_p (x
))
2442 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2445 /* When pedantic, return an expr equal to X but certainly not valid as a
2446 pedantic lvalue. Otherwise, return X. */
2449 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2451 return protected_set_expr_location_unshare (x
, loc
);
2454 /* Given a tree comparison code, return the code that is the logical inverse.
2455 It is generally not safe to do this for floating-point comparisons, except
2456 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2457 ERROR_MARK in this case. */
2460 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2462 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2463 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2473 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2475 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2477 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2479 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2493 return UNORDERED_EXPR
;
2494 case UNORDERED_EXPR
:
2495 return ORDERED_EXPR
;
2501 /* Similar, but return the comparison that results if the operands are
2502 swapped. This is safe for floating-point. */
2505 swap_tree_comparison (enum tree_code code
)
2512 case UNORDERED_EXPR
:
2538 /* Convert a comparison tree code from an enum tree_code representation
2539 into a compcode bit-based encoding. This function is the inverse of
2540 compcode_to_comparison. */
2542 static enum comparison_code
2543 comparison_to_compcode (enum tree_code code
)
2560 return COMPCODE_ORD
;
2561 case UNORDERED_EXPR
:
2562 return COMPCODE_UNORD
;
2564 return COMPCODE_UNLT
;
2566 return COMPCODE_UNEQ
;
2568 return COMPCODE_UNLE
;
2570 return COMPCODE_UNGT
;
2572 return COMPCODE_LTGT
;
2574 return COMPCODE_UNGE
;
2580 /* Convert a compcode bit-based encoding of a comparison operator back
2581 to GCC's enum tree_code representation. This function is the
2582 inverse of comparison_to_compcode. */
2584 static enum tree_code
2585 compcode_to_comparison (enum comparison_code code
)
2602 return ORDERED_EXPR
;
2603 case COMPCODE_UNORD
:
2604 return UNORDERED_EXPR
;
2622 /* Return a tree for the comparison which is the combination of
2623 doing the AND or OR (depending on CODE) of the two operations LCODE
2624 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2625 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2626 if this makes the transformation invalid. */
2629 combine_comparisons (location_t loc
,
2630 enum tree_code code
, enum tree_code lcode
,
2631 enum tree_code rcode
, tree truth_type
,
2632 tree ll_arg
, tree lr_arg
)
2634 bool honor_nans
= HONOR_NANS (ll_arg
);
2635 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2636 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2641 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2642 compcode
= lcompcode
& rcompcode
;
2645 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2646 compcode
= lcompcode
| rcompcode
;
2655 /* Eliminate unordered comparisons, as well as LTGT and ORD
2656 which are not used unless the mode has NaNs. */
2657 compcode
&= ~COMPCODE_UNORD
;
2658 if (compcode
== COMPCODE_LTGT
)
2659 compcode
= COMPCODE_NE
;
2660 else if (compcode
== COMPCODE_ORD
)
2661 compcode
= COMPCODE_TRUE
;
2663 else if (flag_trapping_math
)
2665 /* Check that the original operation and the optimized ones will trap
2666 under the same condition. */
2667 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2668 && (lcompcode
!= COMPCODE_EQ
)
2669 && (lcompcode
!= COMPCODE_ORD
);
2670 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2671 && (rcompcode
!= COMPCODE_EQ
)
2672 && (rcompcode
!= COMPCODE_ORD
);
2673 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2674 && (compcode
!= COMPCODE_EQ
)
2675 && (compcode
!= COMPCODE_ORD
);
2677 /* In a short-circuited boolean expression the LHS might be
2678 such that the RHS, if evaluated, will never trap. For
2679 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2680 if neither x nor y is NaN. (This is a mixed blessing: for
2681 example, the expression above will never trap, hence
2682 optimizing it to x < y would be invalid). */
2683 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2684 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2687 /* If the comparison was short-circuited, and only the RHS
2688 trapped, we may now generate a spurious trap. */
2690 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2693 /* If we changed the conditions that cause a trap, we lose. */
2694 if ((ltrap
|| rtrap
) != trap
)
2698 if (compcode
== COMPCODE_TRUE
)
2699 return constant_boolean_node (true, truth_type
);
2700 else if (compcode
== COMPCODE_FALSE
)
2701 return constant_boolean_node (false, truth_type
);
2704 enum tree_code tcode
;
2706 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2707 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2711 /* Return nonzero if two operands (typically of the same tree node)
2712 are necessarily equal. FLAGS modifies behavior as follows:
2714 If OEP_ONLY_CONST is set, only return nonzero for constants.
2715 This function tests whether the operands are indistinguishable;
2716 it does not test whether they are equal using C's == operation.
2717 The distinction is important for IEEE floating point, because
2718 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2719 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2721 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2722 even though it may hold multiple values during a function.
2723 This is because a GCC tree node guarantees that nothing else is
2724 executed between the evaluation of its "operands" (which may often
2725 be evaluated in arbitrary order). Hence if the operands themselves
2726 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2727 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2728 unset means assuming isochronic (or instantaneous) tree equivalence.
2729 Unless comparing arbitrary expression trees, such as from different
2730 statements, this flag can usually be left unset.
2732 If OEP_PURE_SAME is set, then pure functions with identical arguments
2733 are considered the same. It is used when the caller has other ways
2734 to ensure that global memory is unchanged in between.
2736 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2737 not values of expressions.
2739 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2740 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2742 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2743 any operand with side effect. This is unnecesarily conservative in the
2744 case we know that arg0 and arg1 are in disjoint code paths (such as in
2745 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2746 addresses with TREE_CONSTANT flag set so we know that &var == &var
2747 even if var is volatile. */
2750 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2752 /* When checking, verify at the outermost operand_equal_p call that
2753 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2755 if (flag_checking
&& !(flags
& OEP_NO_HASH_CHECK
))
2757 if (operand_equal_p (arg0
, arg1
, flags
| OEP_NO_HASH_CHECK
))
2761 inchash::hash
hstate0 (0), hstate1 (0);
2762 inchash::add_expr (arg0
, hstate0
, flags
| OEP_HASH_CHECK
);
2763 inchash::add_expr (arg1
, hstate1
, flags
| OEP_HASH_CHECK
);
2764 hashval_t h0
= hstate0
.end ();
2765 hashval_t h1
= hstate1
.end ();
2766 gcc_assert (h0
== h1
);
2774 /* If either is ERROR_MARK, they aren't equal. */
2775 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2776 || TREE_TYPE (arg0
) == error_mark_node
2777 || TREE_TYPE (arg1
) == error_mark_node
)
2780 /* Similar, if either does not have a type (like a released SSA name),
2781 they aren't equal. */
2782 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2785 /* We cannot consider pointers to different address space equal. */
2786 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2787 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2788 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2789 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2792 /* Check equality of integer constants before bailing out due to
2793 precision differences. */
2794 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2796 /* Address of INTEGER_CST is not defined; check that we did not forget
2797 to drop the OEP_ADDRESS_OF flags. */
2798 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2799 return tree_int_cst_equal (arg0
, arg1
);
2802 if (!(flags
& OEP_ADDRESS_OF
))
2804 /* If both types don't have the same signedness, then we can't consider
2805 them equal. We must check this before the STRIP_NOPS calls
2806 because they may change the signedness of the arguments. As pointers
2807 strictly don't have a signedness, require either two pointers or
2808 two non-pointers as well. */
2809 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2810 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2811 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2814 /* If both types don't have the same precision, then it is not safe
2816 if (element_precision (TREE_TYPE (arg0
))
2817 != element_precision (TREE_TYPE (arg1
)))
2824 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2825 sanity check once the issue is solved. */
2827 /* Addresses of conversions and SSA_NAMEs (and many other things)
2828 are not defined. Check that we did not forget to drop the
2829 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2830 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2831 && TREE_CODE (arg0
) != SSA_NAME
);
2834 /* In case both args are comparisons but with different comparison
2835 code, try to swap the comparison operands of one arg to produce
2836 a match and compare that variant. */
2837 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2838 && COMPARISON_CLASS_P (arg0
)
2839 && COMPARISON_CLASS_P (arg1
))
2841 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2843 if (TREE_CODE (arg0
) == swap_code
)
2844 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2845 TREE_OPERAND (arg1
, 1), flags
)
2846 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2847 TREE_OPERAND (arg1
, 0), flags
);
2850 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2852 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2853 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2855 else if (flags
& OEP_ADDRESS_OF
)
2857 /* If we are interested in comparing addresses ignore
2858 MEM_REF wrappings of the base that can appear just for
2860 if (TREE_CODE (arg0
) == MEM_REF
2862 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2863 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2864 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2866 else if (TREE_CODE (arg1
) == MEM_REF
2868 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2869 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2870 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2878 /* When not checking adddresses, this is needed for conversions and for
2879 COMPONENT_REF. Might as well play it safe and always test this. */
2880 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2881 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2882 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2883 && !(flags
& OEP_ADDRESS_OF
)))
2886 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2887 We don't care about side effects in that case because the SAVE_EXPR
2888 takes care of that for us. In all other cases, two expressions are
2889 equal if they have no side effects. If we have two identical
2890 expressions with side effects that should be treated the same due
2891 to the only side effects being identical SAVE_EXPR's, that will
2892 be detected in the recursive calls below.
2893 If we are taking an invariant address of two identical objects
2894 they are necessarily equal as well. */
2895 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2896 && (TREE_CODE (arg0
) == SAVE_EXPR
2897 || (flags
& OEP_MATCH_SIDE_EFFECTS
)
2898 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2901 /* Next handle constant cases, those for which we can return 1 even
2902 if ONLY_CONST is set. */
2903 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2904 switch (TREE_CODE (arg0
))
2907 return tree_int_cst_equal (arg0
, arg1
);
2910 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2911 TREE_FIXED_CST (arg1
));
2914 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2918 if (!HONOR_SIGNED_ZEROS (arg0
))
2920 /* If we do not distinguish between signed and unsigned zero,
2921 consider them equal. */
2922 if (real_zerop (arg0
) && real_zerop (arg1
))
2931 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2934 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2936 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2937 VECTOR_CST_ELT (arg1
, i
), flags
))
2944 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2946 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2950 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2951 && ! memcmp (TREE_STRING_POINTER (arg0
),
2952 TREE_STRING_POINTER (arg1
),
2953 TREE_STRING_LENGTH (arg0
)));
2956 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
2957 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2958 flags
| OEP_ADDRESS_OF
2959 | OEP_MATCH_SIDE_EFFECTS
);
2961 /* In GIMPLE empty constructors are allowed in initializers of
2963 return !CONSTRUCTOR_NELTS (arg0
) && !CONSTRUCTOR_NELTS (arg1
);
2968 if (flags
& OEP_ONLY_CONST
)
2971 /* Define macros to test an operand from arg0 and arg1 for equality and a
2972 variant that allows null and views null as being different from any
2973 non-null value. In the latter case, if either is null, the both
2974 must be; otherwise, do the normal comparison. */
2975 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2976 TREE_OPERAND (arg1, N), flags)
2978 #define OP_SAME_WITH_NULL(N) \
2979 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2980 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2982 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2985 /* Two conversions are equal only if signedness and modes match. */
2986 switch (TREE_CODE (arg0
))
2989 case FIX_TRUNC_EXPR
:
2990 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2991 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
3001 case tcc_comparison
:
3003 if (OP_SAME (0) && OP_SAME (1))
3006 /* For commutative ops, allow the other order. */
3007 return (commutative_tree_code (TREE_CODE (arg0
))
3008 && operand_equal_p (TREE_OPERAND (arg0
, 0),
3009 TREE_OPERAND (arg1
, 1), flags
)
3010 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3011 TREE_OPERAND (arg1
, 0), flags
));
3014 /* If either of the pointer (or reference) expressions we are
3015 dereferencing contain a side effect, these cannot be equal,
3016 but their addresses can be. */
3017 if ((flags
& OEP_MATCH_SIDE_EFFECTS
) == 0
3018 && (TREE_SIDE_EFFECTS (arg0
)
3019 || TREE_SIDE_EFFECTS (arg1
)))
3022 switch (TREE_CODE (arg0
))
3025 if (!(flags
& OEP_ADDRESS_OF
)
3026 && (TYPE_ALIGN (TREE_TYPE (arg0
))
3027 != TYPE_ALIGN (TREE_TYPE (arg1
))))
3029 flags
&= ~OEP_ADDRESS_OF
;
3033 /* Require the same offset. */
3034 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3035 TYPE_SIZE (TREE_TYPE (arg1
)),
3036 flags
& ~OEP_ADDRESS_OF
))
3041 case VIEW_CONVERT_EXPR
:
3044 case TARGET_MEM_REF
:
3046 if (!(flags
& OEP_ADDRESS_OF
))
3048 /* Require equal access sizes */
3049 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
3050 && (!TYPE_SIZE (TREE_TYPE (arg0
))
3051 || !TYPE_SIZE (TREE_TYPE (arg1
))
3052 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
3053 TYPE_SIZE (TREE_TYPE (arg1
)),
3056 /* Verify that access happens in similar types. */
3057 if (!types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
)))
3059 /* Verify that accesses are TBAA compatible. */
3060 if (!alias_ptr_types_compatible_p
3061 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3062 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
3063 || (MR_DEPENDENCE_CLIQUE (arg0
)
3064 != MR_DEPENDENCE_CLIQUE (arg1
))
3065 || (MR_DEPENDENCE_BASE (arg0
)
3066 != MR_DEPENDENCE_BASE (arg1
)))
3068 /* Verify that alignment is compatible. */
3069 if (TYPE_ALIGN (TREE_TYPE (arg0
))
3070 != TYPE_ALIGN (TREE_TYPE (arg1
)))
3073 flags
&= ~OEP_ADDRESS_OF
;
3074 return (OP_SAME (0) && OP_SAME (1)
3075 /* TARGET_MEM_REF require equal extra operands. */
3076 && (TREE_CODE (arg0
) != TARGET_MEM_REF
3077 || (OP_SAME_WITH_NULL (2)
3078 && OP_SAME_WITH_NULL (3)
3079 && OP_SAME_WITH_NULL (4))));
3082 case ARRAY_RANGE_REF
:
3085 flags
&= ~OEP_ADDRESS_OF
;
3086 /* Compare the array index by value if it is constant first as we
3087 may have different types but same value here. */
3088 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3089 TREE_OPERAND (arg1
, 1))
3091 && OP_SAME_WITH_NULL (2)
3092 && OP_SAME_WITH_NULL (3)
3093 /* Compare low bound and element size as with OEP_ADDRESS_OF
3094 we have to account for the offset of the ref. */
3095 && (TREE_TYPE (TREE_OPERAND (arg0
, 0))
3096 == TREE_TYPE (TREE_OPERAND (arg1
, 0))
3097 || (operand_equal_p (array_ref_low_bound
3098 (CONST_CAST_TREE (arg0
)),
3100 (CONST_CAST_TREE (arg1
)), flags
)
3101 && operand_equal_p (array_ref_element_size
3102 (CONST_CAST_TREE (arg0
)),
3103 array_ref_element_size
3104 (CONST_CAST_TREE (arg1
)),
3108 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3109 may be NULL when we're called to compare MEM_EXPRs. */
3110 if (!OP_SAME_WITH_NULL (0)
3113 flags
&= ~OEP_ADDRESS_OF
;
3114 return OP_SAME_WITH_NULL (2);
3119 flags
&= ~OEP_ADDRESS_OF
;
3120 return OP_SAME (1) && OP_SAME (2);
3126 case tcc_expression
:
3127 switch (TREE_CODE (arg0
))
3130 /* Be sure we pass right ADDRESS_OF flag. */
3131 gcc_checking_assert (!(flags
& OEP_ADDRESS_OF
));
3132 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3133 TREE_OPERAND (arg1
, 0),
3134 flags
| OEP_ADDRESS_OF
);
3136 case TRUTH_NOT_EXPR
:
3139 case TRUTH_ANDIF_EXPR
:
3140 case TRUTH_ORIF_EXPR
:
3141 return OP_SAME (0) && OP_SAME (1);
3144 case WIDEN_MULT_PLUS_EXPR
:
3145 case WIDEN_MULT_MINUS_EXPR
:
3148 /* The multiplcation operands are commutative. */
3151 case TRUTH_AND_EXPR
:
3153 case TRUTH_XOR_EXPR
:
3154 if (OP_SAME (0) && OP_SAME (1))
3157 /* Otherwise take into account this is a commutative operation. */
3158 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3159 TREE_OPERAND (arg1
, 1), flags
)
3160 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3161 TREE_OPERAND (arg1
, 0), flags
));
3164 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3166 flags
&= ~OEP_ADDRESS_OF
;
3171 case BIT_INSERT_EXPR
:
3172 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3177 case PREDECREMENT_EXPR
:
3178 case PREINCREMENT_EXPR
:
3179 case POSTDECREMENT_EXPR
:
3180 case POSTINCREMENT_EXPR
:
3181 if (flags
& OEP_LEXICOGRAPHIC
)
3182 return OP_SAME (0) && OP_SAME (1);
3185 case CLEANUP_POINT_EXPR
:
3187 if (flags
& OEP_LEXICOGRAPHIC
)
3196 switch (TREE_CODE (arg0
))
3199 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3200 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3201 /* If not both CALL_EXPRs are either internal or normal function
3202 functions, then they are not equal. */
3204 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3206 /* If the CALL_EXPRs call different internal functions, then they
3208 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3213 /* If the CALL_EXPRs call different functions, then they are not
3215 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3220 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3222 unsigned int cef
= call_expr_flags (arg0
);
3223 if (flags
& OEP_PURE_SAME
)
3224 cef
&= ECF_CONST
| ECF_PURE
;
3227 if (!cef
&& !(flags
& OEP_LEXICOGRAPHIC
))
3231 /* Now see if all the arguments are the same. */
3233 const_call_expr_arg_iterator iter0
, iter1
;
3235 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3236 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3238 a0
= next_const_call_expr_arg (&iter0
),
3239 a1
= next_const_call_expr_arg (&iter1
))
3240 if (! operand_equal_p (a0
, a1
, flags
))
3243 /* If we get here and both argument lists are exhausted
3244 then the CALL_EXPRs are equal. */
3245 return ! (a0
|| a1
);
3251 case tcc_declaration
:
3252 /* Consider __builtin_sqrt equal to sqrt. */
3253 return (TREE_CODE (arg0
) == FUNCTION_DECL
3254 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3255 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3256 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3258 case tcc_exceptional
:
3259 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3261 /* In GIMPLE constructors are used only to build vectors from
3262 elements. Individual elements in the constructor must be
3263 indexed in increasing order and form an initial sequence.
3265 We make no effort to compare constructors in generic.
3266 (see sem_variable::equals in ipa-icf which can do so for
3268 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3269 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3272 /* Be sure that vectors constructed have the same representation.
3273 We only tested element precision and modes to match.
3274 Vectors may be BLKmode and thus also check that the number of
3276 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3277 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3280 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3281 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3282 unsigned int len
= vec_safe_length (v0
);
3284 if (len
!= vec_safe_length (v1
))
3287 for (unsigned int i
= 0; i
< len
; i
++)
3289 constructor_elt
*c0
= &(*v0
)[i
];
3290 constructor_elt
*c1
= &(*v1
)[i
];
3292 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3293 /* In GIMPLE the indexes can be either NULL or matching i.
3294 Double check this so we won't get false
3295 positives for GENERIC. */
3297 && (TREE_CODE (c0
->index
) != INTEGER_CST
3298 || !compare_tree_int (c0
->index
, i
)))
3300 && (TREE_CODE (c1
->index
) != INTEGER_CST
3301 || !compare_tree_int (c1
->index
, i
))))
3306 else if (TREE_CODE (arg0
) == STATEMENT_LIST
3307 && (flags
& OEP_LEXICOGRAPHIC
))
3309 /* Compare the STATEMENT_LISTs. */
3310 tree_stmt_iterator tsi1
, tsi2
;
3311 tree body1
= CONST_CAST_TREE (arg0
);
3312 tree body2
= CONST_CAST_TREE (arg1
);
3313 for (tsi1
= tsi_start (body1
), tsi2
= tsi_start (body2
); ;
3314 tsi_next (&tsi1
), tsi_next (&tsi2
))
3316 /* The lists don't have the same number of statements. */
3317 if (tsi_end_p (tsi1
) ^ tsi_end_p (tsi2
))
3319 if (tsi_end_p (tsi1
) && tsi_end_p (tsi2
))
3321 if (!operand_equal_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
),
3329 switch (TREE_CODE (arg0
))
3332 if (flags
& OEP_LEXICOGRAPHIC
)
3333 return OP_SAME_WITH_NULL (0);
3344 #undef OP_SAME_WITH_NULL
3347 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3348 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3350 When in doubt, return 0. */
3353 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3355 int unsignedp1
, unsignedpo
;
3356 tree primarg0
, primarg1
, primother
;
3357 unsigned int correct_width
;
3359 if (operand_equal_p (arg0
, arg1
, 0))
3362 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3363 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3366 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3367 and see if the inner values are the same. This removes any
3368 signedness comparison, which doesn't matter here. */
3369 primarg0
= arg0
, primarg1
= arg1
;
3370 STRIP_NOPS (primarg0
);
3371 STRIP_NOPS (primarg1
);
3372 if (operand_equal_p (primarg0
, primarg1
, 0))
3375 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3376 actual comparison operand, ARG0.
3378 First throw away any conversions to wider types
3379 already present in the operands. */
3381 primarg1
= get_narrower (arg1
, &unsignedp1
);
3382 primother
= get_narrower (other
, &unsignedpo
);
3384 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3385 if (unsignedp1
== unsignedpo
3386 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3387 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3389 tree type
= TREE_TYPE (arg0
);
3391 /* Make sure shorter operand is extended the right way
3392 to match the longer operand. */
3393 primarg1
= fold_convert (signed_or_unsigned_type_for
3394 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3396 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3403 /* See if ARG is an expression that is either a comparison or is performing
3404 arithmetic on comparisons. The comparisons must only be comparing
3405 two different values, which will be stored in *CVAL1 and *CVAL2; if
3406 they are nonzero it means that some operands have already been found.
3407 No variables may be used anywhere else in the expression except in the
3408 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3409 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3411 If this is true, return 1. Otherwise, return zero. */
3414 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3416 enum tree_code code
= TREE_CODE (arg
);
3417 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3419 /* We can handle some of the tcc_expression cases here. */
3420 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3422 else if (tclass
== tcc_expression
3423 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3424 || code
== COMPOUND_EXPR
))
3425 tclass
= tcc_binary
;
3427 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3428 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3430 /* If we've already found a CVAL1 or CVAL2, this expression is
3431 two complex to handle. */
3432 if (*cval1
|| *cval2
)
3442 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3445 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3446 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3447 cval1
, cval2
, save_p
));
3452 case tcc_expression
:
3453 if (code
== COND_EXPR
)
3454 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3455 cval1
, cval2
, save_p
)
3456 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3457 cval1
, cval2
, save_p
)
3458 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3459 cval1
, cval2
, save_p
));
3462 case tcc_comparison
:
3463 /* First see if we can handle the first operand, then the second. For
3464 the second operand, we know *CVAL1 can't be zero. It must be that
3465 one side of the comparison is each of the values; test for the
3466 case where this isn't true by failing if the two operands
3469 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3470 TREE_OPERAND (arg
, 1), 0))
3474 *cval1
= TREE_OPERAND (arg
, 0);
3475 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3477 else if (*cval2
== 0)
3478 *cval2
= TREE_OPERAND (arg
, 0);
3479 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3484 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3486 else if (*cval2
== 0)
3487 *cval2
= TREE_OPERAND (arg
, 1);
3488 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3500 /* ARG is a tree that is known to contain just arithmetic operations and
3501 comparisons. Evaluate the operations in the tree substituting NEW0 for
3502 any occurrence of OLD0 as an operand of a comparison and likewise for
3506 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3507 tree old1
, tree new1
)
3509 tree type
= TREE_TYPE (arg
);
3510 enum tree_code code
= TREE_CODE (arg
);
3511 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3513 /* We can handle some of the tcc_expression cases here. */
3514 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3516 else if (tclass
== tcc_expression
3517 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3518 tclass
= tcc_binary
;
3523 return fold_build1_loc (loc
, code
, type
,
3524 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3525 old0
, new0
, old1
, new1
));
3528 return fold_build2_loc (loc
, code
, type
,
3529 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3530 old0
, new0
, old1
, new1
),
3531 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3532 old0
, new0
, old1
, new1
));
3534 case tcc_expression
:
3538 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3542 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3546 return fold_build3_loc (loc
, code
, type
,
3547 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3548 old0
, new0
, old1
, new1
),
3549 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3550 old0
, new0
, old1
, new1
),
3551 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3552 old0
, new0
, old1
, new1
));
3556 /* Fall through - ??? */
3558 case tcc_comparison
:
3560 tree arg0
= TREE_OPERAND (arg
, 0);
3561 tree arg1
= TREE_OPERAND (arg
, 1);
3563 /* We need to check both for exact equality and tree equality. The
3564 former will be true if the operand has a side-effect. In that
3565 case, we know the operand occurred exactly once. */
3567 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3569 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3572 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3574 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3577 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3585 /* Return a tree for the case when the result of an expression is RESULT
3586 converted to TYPE and OMITTED was previously an operand of the expression
3587 but is now not needed (e.g., we folded OMITTED * 0).
3589 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3590 the conversion of RESULT to TYPE. */
3593 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3595 tree t
= fold_convert_loc (loc
, type
, result
);
3597 /* If the resulting operand is an empty statement, just return the omitted
3598 statement casted to void. */
3599 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3600 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3601 fold_ignored_result (omitted
));
3603 if (TREE_SIDE_EFFECTS (omitted
))
3604 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3605 fold_ignored_result (omitted
), t
);
3607 return non_lvalue_loc (loc
, t
);
3610 /* Return a tree for the case when the result of an expression is RESULT
3611 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3612 of the expression but are now not needed.
3614 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3615 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3616 evaluated before OMITTED2. Otherwise, if neither has side effects,
3617 just do the conversion of RESULT to TYPE. */
3620 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3621 tree omitted1
, tree omitted2
)
3623 tree t
= fold_convert_loc (loc
, type
, result
);
3625 if (TREE_SIDE_EFFECTS (omitted2
))
3626 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3627 if (TREE_SIDE_EFFECTS (omitted1
))
3628 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3630 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3634 /* Return a simplified tree node for the truth-negation of ARG. This
3635 never alters ARG itself. We assume that ARG is an operation that
3636 returns a truth value (0 or 1).
3638 FIXME: one would think we would fold the result, but it causes
3639 problems with the dominator optimizer. */
3642 fold_truth_not_expr (location_t loc
, tree arg
)
3644 tree type
= TREE_TYPE (arg
);
3645 enum tree_code code
= TREE_CODE (arg
);
3646 location_t loc1
, loc2
;
3648 /* If this is a comparison, we can simply invert it, except for
3649 floating-point non-equality comparisons, in which case we just
3650 enclose a TRUTH_NOT_EXPR around what we have. */
3652 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3654 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3655 if (FLOAT_TYPE_P (op_type
)
3656 && flag_trapping_math
3657 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3658 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3661 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3662 if (code
== ERROR_MARK
)
3665 tree ret
= build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3666 TREE_OPERAND (arg
, 1));
3667 if (TREE_NO_WARNING (arg
))
3668 TREE_NO_WARNING (ret
) = 1;
3675 return constant_boolean_node (integer_zerop (arg
), type
);
3677 case TRUTH_AND_EXPR
:
3678 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3679 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3680 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3681 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3682 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3685 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3686 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3687 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3688 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3689 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3691 case TRUTH_XOR_EXPR
:
3692 /* Here we can invert either operand. We invert the first operand
3693 unless the second operand is a TRUTH_NOT_EXPR in which case our
3694 result is the XOR of the first operand with the inside of the
3695 negation of the second operand. */
3697 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3698 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3699 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3701 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3702 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3703 TREE_OPERAND (arg
, 1));
3705 case TRUTH_ANDIF_EXPR
:
3706 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3707 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3708 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3709 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3710 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3712 case TRUTH_ORIF_EXPR
:
3713 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3714 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3715 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3716 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3717 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3719 case TRUTH_NOT_EXPR
:
3720 return TREE_OPERAND (arg
, 0);
3724 tree arg1
= TREE_OPERAND (arg
, 1);
3725 tree arg2
= TREE_OPERAND (arg
, 2);
3727 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3728 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3730 /* A COND_EXPR may have a throw as one operand, which
3731 then has void type. Just leave void operands
3733 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3734 VOID_TYPE_P (TREE_TYPE (arg1
))
3735 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3736 VOID_TYPE_P (TREE_TYPE (arg2
))
3737 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3741 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3742 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3743 TREE_OPERAND (arg
, 0),
3744 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3746 case NON_LVALUE_EXPR
:
3747 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3748 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3751 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3752 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3757 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3758 return build1_loc (loc
, TREE_CODE (arg
), type
,
3759 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3762 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3764 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3767 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3769 case CLEANUP_POINT_EXPR
:
3770 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3771 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3772 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3779 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3780 assume that ARG is an operation that returns a truth value (0 or 1
3781 for scalars, 0 or -1 for vectors). Return the folded expression if
3782 folding is successful. Otherwise, return NULL_TREE. */
3785 fold_invert_truthvalue (location_t loc
, tree arg
)
3787 tree type
= TREE_TYPE (arg
);
3788 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3794 /* Return a simplified tree node for the truth-negation of ARG. This
3795 never alters ARG itself. We assume that ARG is an operation that
3796 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3799 invert_truthvalue_loc (location_t loc
, tree arg
)
3801 if (TREE_CODE (arg
) == ERROR_MARK
)
3804 tree type
= TREE_TYPE (arg
);
3805 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3811 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3812 with code CODE. This optimization is unsafe. */
3814 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3815 tree arg0
, tree arg1
)
3817 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3818 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3820 /* (A / C) +- (B / C) -> (A +- B) / C. */
3822 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3823 TREE_OPERAND (arg1
, 1), 0))
3824 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3825 fold_build2_loc (loc
, code
, type
,
3826 TREE_OPERAND (arg0
, 0),
3827 TREE_OPERAND (arg1
, 0)),
3828 TREE_OPERAND (arg0
, 1));
3830 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3831 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3832 TREE_OPERAND (arg1
, 0), 0)
3833 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3834 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3836 REAL_VALUE_TYPE r0
, r1
;
3837 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3838 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3840 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3842 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3843 real_arithmetic (&r0
, code
, &r0
, &r1
);
3844 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3845 TREE_OPERAND (arg0
, 0),
3846 build_real (type
, r0
));
3852 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3853 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3854 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3855 is the original memory reference used to preserve the alias set of
3859 make_bit_field_ref (location_t loc
, tree inner
, tree orig_inner
, tree type
,
3860 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
,
3861 int unsignedp
, int reversep
)
3863 tree result
, bftype
;
3865 alias_set_type iset
= get_alias_set (orig_inner
);
3866 if (iset
== 0 && get_alias_set (inner
) != iset
)
3867 inner
= fold_build2 (MEM_REF
, TREE_TYPE (inner
),
3868 build_fold_addr_expr (inner
),
3869 build_int_cst (ptr_type_node
, 0));
3871 if (bitpos
== 0 && !reversep
)
3873 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3874 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3875 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3876 && tree_fits_shwi_p (size
)
3877 && tree_to_shwi (size
) == bitsize
)
3878 return fold_convert_loc (loc
, type
, inner
);
3882 if (TYPE_PRECISION (bftype
) != bitsize
3883 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3884 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3886 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3887 size_int (bitsize
), bitsize_int (bitpos
));
3888 REF_REVERSE_STORAGE_ORDER (result
) = reversep
;
3891 result
= fold_convert_loc (loc
, type
, result
);
3896 /* Optimize a bit-field compare.
3898 There are two cases: First is a compare against a constant and the
3899 second is a comparison of two items where the fields are at the same
3900 bit position relative to the start of a chunk (byte, halfword, word)
3901 large enough to contain it. In these cases we can avoid the shift
3902 implicit in bitfield extractions.
3904 For constants, we emit a compare of the shifted constant with the
3905 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3906 compared. For two fields at the same position, we do the ANDs with the
3907 similar mask and compare the result of the ANDs.
3909 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3910 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3911 are the left and right operands of the comparison, respectively.
3913 If the optimization described above can be done, we return the resulting
3914 tree. Otherwise we return zero. */
3917 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3918 tree compare_type
, tree lhs
, tree rhs
)
3920 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3921 tree type
= TREE_TYPE (lhs
);
3923 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3924 machine_mode lmode
, rmode
, nmode
;
3925 int lunsignedp
, runsignedp
;
3926 int lreversep
, rreversep
;
3927 int lvolatilep
= 0, rvolatilep
= 0;
3928 tree linner
, rinner
= NULL_TREE
;
3932 /* Get all the information about the extractions being done. If the bit size
3933 if the same as the size of the underlying object, we aren't doing an
3934 extraction at all and so can do nothing. We also don't want to
3935 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3936 then will no longer be able to replace it. */
3937 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3938 &lunsignedp
, &lreversep
, &lvolatilep
);
3939 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3940 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3944 rreversep
= lreversep
;
3947 /* If this is not a constant, we can only do something if bit positions,
3948 sizes, signedness and storage order are the same. */
3950 = get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3951 &runsignedp
, &rreversep
, &rvolatilep
);
3953 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3954 || lunsignedp
!= runsignedp
|| lreversep
!= rreversep
|| offset
!= 0
3955 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3959 /* Honor the C++ memory model and mimic what RTL expansion does. */
3960 unsigned HOST_WIDE_INT bitstart
= 0;
3961 unsigned HOST_WIDE_INT bitend
= 0;
3962 if (TREE_CODE (lhs
) == COMPONENT_REF
)
3964 get_bit_range (&bitstart
, &bitend
, lhs
, &lbitpos
, &offset
);
3965 if (offset
!= NULL_TREE
)
3969 /* See if we can find a mode to refer to this field. We should be able to,
3970 but fail if we can't. */
3971 nmode
= get_best_mode (lbitsize
, lbitpos
, bitstart
, bitend
,
3972 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3973 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3974 TYPE_ALIGN (TREE_TYPE (rinner
))),
3976 if (nmode
== VOIDmode
)
3979 /* Set signed and unsigned types of the precision of this mode for the
3981 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3983 /* Compute the bit position and size for the new reference and our offset
3984 within it. If the new reference is the same size as the original, we
3985 won't optimize anything, so return zero. */
3986 nbitsize
= GET_MODE_BITSIZE (nmode
);
3987 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3989 if (nbitsize
== lbitsize
)
3992 if (lreversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
3993 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3995 /* Make the mask to be used against the extracted field. */
3996 mask
= build_int_cst_type (unsigned_type
, -1);
3997 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3998 mask
= const_binop (RSHIFT_EXPR
, mask
,
3999 size_int (nbitsize
- lbitsize
- lbitpos
));
4002 /* If not comparing with constant, just rework the comparison
4004 return fold_build2_loc (loc
, code
, compare_type
,
4005 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4006 make_bit_field_ref (loc
, linner
, lhs
,
4011 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4012 make_bit_field_ref (loc
, rinner
, rhs
,
4018 /* Otherwise, we are handling the constant case. See if the constant is too
4019 big for the field. Warn and return a tree for 0 (false) if so. We do
4020 this not only for its own sake, but to avoid having to test for this
4021 error case below. If we didn't, we might generate wrong code.
4023 For unsigned fields, the constant shifted right by the field length should
4024 be all zero. For signed fields, the high-order bits should agree with
4029 if (wi::lrshift (rhs
, lbitsize
) != 0)
4031 warning (0, "comparison is always %d due to width of bit-field",
4033 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4038 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
4039 if (tem
!= 0 && tem
!= -1)
4041 warning (0, "comparison is always %d due to width of bit-field",
4043 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
4047 /* Single-bit compares should always be against zero. */
4048 if (lbitsize
== 1 && ! integer_zerop (rhs
))
4050 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
4051 rhs
= build_int_cst (type
, 0);
4054 /* Make a new bitfield reference, shift the constant over the
4055 appropriate number of bits and mask it with the computed mask
4056 (in case this was a signed field). If we changed it, make a new one. */
4057 lhs
= make_bit_field_ref (loc
, linner
, lhs
, unsigned_type
,
4058 nbitsize
, nbitpos
, 1, lreversep
);
4060 rhs
= const_binop (BIT_AND_EXPR
,
4061 const_binop (LSHIFT_EXPR
,
4062 fold_convert_loc (loc
, unsigned_type
, rhs
),
4063 size_int (lbitpos
)),
4066 lhs
= build2_loc (loc
, code
, compare_type
,
4067 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
4071 /* Subroutine for fold_truth_andor_1: decode a field reference.
4073 If EXP is a comparison reference, we return the innermost reference.
4075 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4076 set to the starting bit number.
4078 If the innermost field can be completely contained in a mode-sized
4079 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4081 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4082 otherwise it is not changed.
4084 *PUNSIGNEDP is set to the signedness of the field.
4086 *PREVERSEP is set to the storage order of the field.
4088 *PMASK is set to the mask used. This is either contained in a
4089 BIT_AND_EXPR or derived from the width of the field.
4091 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4093 Return 0 if this is not a component reference or is one that we can't
4094 do anything with. */
4097 decode_field_reference (location_t loc
, tree
*exp_
, HOST_WIDE_INT
*pbitsize
,
4098 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
4099 int *punsignedp
, int *preversep
, int *pvolatilep
,
4100 tree
*pmask
, tree
*pand_mask
)
4103 tree outer_type
= 0;
4105 tree mask
, inner
, offset
;
4107 unsigned int precision
;
4109 /* All the optimizations using this function assume integer fields.
4110 There are problems with FP fields since the type_for_size call
4111 below can fail for, e.g., XFmode. */
4112 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
4115 /* We are interested in the bare arrangement of bits, so strip everything
4116 that doesn't affect the machine mode. However, record the type of the
4117 outermost expression if it may matter below. */
4118 if (CONVERT_EXPR_P (exp
)
4119 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
4120 outer_type
= TREE_TYPE (exp
);
4123 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
4125 and_mask
= TREE_OPERAND (exp
, 1);
4126 exp
= TREE_OPERAND (exp
, 0);
4127 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
4128 if (TREE_CODE (and_mask
) != INTEGER_CST
)
4132 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
4133 punsignedp
, preversep
, pvolatilep
);
4134 if ((inner
== exp
&& and_mask
== 0)
4135 || *pbitsize
< 0 || offset
!= 0
4136 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
4141 /* If the number of bits in the reference is the same as the bitsize of
4142 the outer type, then the outer type gives the signedness. Otherwise
4143 (in case of a small bitfield) the signedness is unchanged. */
4144 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
4145 *punsignedp
= TYPE_UNSIGNED (outer_type
);
4147 /* Compute the mask to access the bitfield. */
4148 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
4149 precision
= TYPE_PRECISION (unsigned_type
);
4151 mask
= build_int_cst_type (unsigned_type
, -1);
4153 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4154 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
4156 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4158 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
4159 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
4162 *pand_mask
= and_mask
;
4166 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4167 bit positions and MASK is SIGNED. */
4170 all_ones_mask_p (const_tree mask
, unsigned int size
)
4172 tree type
= TREE_TYPE (mask
);
4173 unsigned int precision
= TYPE_PRECISION (type
);
4175 /* If this function returns true when the type of the mask is
4176 UNSIGNED, then there will be errors. In particular see
4177 gcc.c-torture/execute/990326-1.c. There does not appear to be
4178 any documentation paper trail as to why this is so. But the pre
4179 wide-int worked with that restriction and it has been preserved
4181 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
4184 return wi::mask (size
, false, precision
) == mask
;
4187 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4188 represents the sign bit of EXP's type. If EXP represents a sign
4189 or zero extension, also test VAL against the unextended type.
4190 The return value is the (sub)expression whose sign bit is VAL,
4191 or NULL_TREE otherwise. */
4194 sign_bit_p (tree exp
, const_tree val
)
4199 /* Tree EXP must have an integral type. */
4200 t
= TREE_TYPE (exp
);
4201 if (! INTEGRAL_TYPE_P (t
))
4204 /* Tree VAL must be an integer constant. */
4205 if (TREE_CODE (val
) != INTEGER_CST
4206 || TREE_OVERFLOW (val
))
4209 width
= TYPE_PRECISION (t
);
4210 if (wi::only_sign_bit_p (val
, width
))
4213 /* Handle extension from a narrower type. */
4214 if (TREE_CODE (exp
) == NOP_EXPR
4215 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4216 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4221 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4222 to be evaluated unconditionally. */
4225 simple_operand_p (const_tree exp
)
4227 /* Strip any conversions that don't change the machine mode. */
4230 return (CONSTANT_CLASS_P (exp
)
4231 || TREE_CODE (exp
) == SSA_NAME
4233 && ! TREE_ADDRESSABLE (exp
)
4234 && ! TREE_THIS_VOLATILE (exp
)
4235 && ! DECL_NONLOCAL (exp
)
4236 /* Don't regard global variables as simple. They may be
4237 allocated in ways unknown to the compiler (shared memory,
4238 #pragma weak, etc). */
4239 && ! TREE_PUBLIC (exp
)
4240 && ! DECL_EXTERNAL (exp
)
4241 /* Weakrefs are not safe to be read, since they can be NULL.
4242 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4243 have DECL_WEAK flag set. */
4244 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4245 /* Loading a static variable is unduly expensive, but global
4246 registers aren't expensive. */
4247 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4250 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4251 to be evaluated unconditionally.
4252 I addition to simple_operand_p, we assume that comparisons, conversions,
4253 and logic-not operations are simple, if their operands are simple, too. */
4256 simple_operand_p_2 (tree exp
)
4258 enum tree_code code
;
4260 if (TREE_SIDE_EFFECTS (exp
)
4261 || tree_could_trap_p (exp
))
4264 while (CONVERT_EXPR_P (exp
))
4265 exp
= TREE_OPERAND (exp
, 0);
4267 code
= TREE_CODE (exp
);
4269 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4270 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4271 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4273 if (code
== TRUTH_NOT_EXPR
)
4274 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4276 return simple_operand_p (exp
);
4280 /* The following functions are subroutines to fold_range_test and allow it to
4281 try to change a logical combination of comparisons into a range test.
4284 X == 2 || X == 3 || X == 4 || X == 5
4288 (unsigned) (X - 2) <= 3
4290 We describe each set of comparisons as being either inside or outside
4291 a range, using a variable named like IN_P, and then describe the
4292 range with a lower and upper bound. If one of the bounds is omitted,
4293 it represents either the highest or lowest value of the type.
4295 In the comments below, we represent a range by two numbers in brackets
4296 preceded by a "+" to designate being inside that range, or a "-" to
4297 designate being outside that range, so the condition can be inverted by
4298 flipping the prefix. An omitted bound is represented by a "-". For
4299 example, "- [-, 10]" means being outside the range starting at the lowest
4300 possible value and ending at 10, in other words, being greater than 10.
4301 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4304 We set up things so that the missing bounds are handled in a consistent
4305 manner so neither a missing bound nor "true" and "false" need to be
4306 handled using a special case. */
4308 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4309 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4310 and UPPER1_P are nonzero if the respective argument is an upper bound
4311 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4312 must be specified for a comparison. ARG1 will be converted to ARG0's
4313 type if both are specified. */
4316 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4317 tree arg1
, int upper1_p
)
4323 /* If neither arg represents infinity, do the normal operation.
4324 Else, if not a comparison, return infinity. Else handle the special
4325 comparison rules. Note that most of the cases below won't occur, but
4326 are handled for consistency. */
4328 if (arg0
!= 0 && arg1
!= 0)
4330 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4331 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4333 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4336 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4339 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4340 for neither. In real maths, we cannot assume open ended ranges are
4341 the same. But, this is computer arithmetic, where numbers are finite.
4342 We can therefore make the transformation of any unbounded range with
4343 the value Z, Z being greater than any representable number. This permits
4344 us to treat unbounded ranges as equal. */
4345 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4346 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4350 result
= sgn0
== sgn1
;
4353 result
= sgn0
!= sgn1
;
4356 result
= sgn0
< sgn1
;
4359 result
= sgn0
<= sgn1
;
4362 result
= sgn0
> sgn1
;
4365 result
= sgn0
>= sgn1
;
4371 return constant_boolean_node (result
, type
);
4374 /* Helper routine for make_range. Perform one step for it, return
4375 new expression if the loop should continue or NULL_TREE if it should
4379 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4380 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4381 bool *strict_overflow_p
)
4383 tree arg0_type
= TREE_TYPE (arg0
);
4384 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4385 int in_p
= *p_in_p
, n_in_p
;
4389 case TRUTH_NOT_EXPR
:
4390 /* We can only do something if the range is testing for zero. */
4391 if (low
== NULL_TREE
|| high
== NULL_TREE
4392 || ! integer_zerop (low
) || ! integer_zerop (high
))
4397 case EQ_EXPR
: case NE_EXPR
:
4398 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4399 /* We can only do something if the range is testing for zero
4400 and if the second operand is an integer constant. Note that
4401 saying something is "in" the range we make is done by
4402 complementing IN_P since it will set in the initial case of
4403 being not equal to zero; "out" is leaving it alone. */
4404 if (low
== NULL_TREE
|| high
== NULL_TREE
4405 || ! integer_zerop (low
) || ! integer_zerop (high
)
4406 || TREE_CODE (arg1
) != INTEGER_CST
)
4411 case NE_EXPR
: /* - [c, c] */
4414 case EQ_EXPR
: /* + [c, c] */
4415 in_p
= ! in_p
, low
= high
= arg1
;
4417 case GT_EXPR
: /* - [-, c] */
4418 low
= 0, high
= arg1
;
4420 case GE_EXPR
: /* + [c, -] */
4421 in_p
= ! in_p
, low
= arg1
, high
= 0;
4423 case LT_EXPR
: /* - [c, -] */
4424 low
= arg1
, high
= 0;
4426 case LE_EXPR
: /* + [-, c] */
4427 in_p
= ! in_p
, low
= 0, high
= arg1
;
4433 /* If this is an unsigned comparison, we also know that EXP is
4434 greater than or equal to zero. We base the range tests we make
4435 on that fact, so we record it here so we can parse existing
4436 range tests. We test arg0_type since often the return type
4437 of, e.g. EQ_EXPR, is boolean. */
4438 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4440 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4442 build_int_cst (arg0_type
, 0),
4446 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4448 /* If the high bound is missing, but we have a nonzero low
4449 bound, reverse the range so it goes from zero to the low bound
4451 if (high
== 0 && low
&& ! integer_zerop (low
))
4454 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4455 build_int_cst (TREE_TYPE (low
), 1), 0);
4456 low
= build_int_cst (arg0_type
, 0);
4466 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4467 low and high are non-NULL, then normalize will DTRT. */
4468 if (!TYPE_UNSIGNED (arg0_type
)
4469 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4471 if (low
== NULL_TREE
)
4472 low
= TYPE_MIN_VALUE (arg0_type
);
4473 if (high
== NULL_TREE
)
4474 high
= TYPE_MAX_VALUE (arg0_type
);
4477 /* (-x) IN [a,b] -> x in [-b, -a] */
4478 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4479 build_int_cst (exp_type
, 0),
4481 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4482 build_int_cst (exp_type
, 0),
4484 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4490 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4491 build_int_cst (exp_type
, 1));
4495 if (TREE_CODE (arg1
) != INTEGER_CST
)
4498 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4499 move a constant to the other side. */
4500 if (!TYPE_UNSIGNED (arg0_type
)
4501 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4504 /* If EXP is signed, any overflow in the computation is undefined,
4505 so we don't worry about it so long as our computations on
4506 the bounds don't overflow. For unsigned, overflow is defined
4507 and this is exactly the right thing. */
4508 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4509 arg0_type
, low
, 0, arg1
, 0);
4510 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4511 arg0_type
, high
, 1, arg1
, 0);
4512 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4513 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4516 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4517 *strict_overflow_p
= true;
4520 /* Check for an unsigned range which has wrapped around the maximum
4521 value thus making n_high < n_low, and normalize it. */
4522 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4524 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4525 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4526 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4527 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4529 /* If the range is of the form +/- [ x+1, x ], we won't
4530 be able to normalize it. But then, it represents the
4531 whole range or the empty set, so make it
4533 if (tree_int_cst_equal (n_low
, low
)
4534 && tree_int_cst_equal (n_high
, high
))
4540 low
= n_low
, high
= n_high
;
4548 case NON_LVALUE_EXPR
:
4549 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4552 if (! INTEGRAL_TYPE_P (arg0_type
)
4553 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4554 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4557 n_low
= low
, n_high
= high
;
4560 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4563 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4565 /* If we're converting arg0 from an unsigned type, to exp,
4566 a signed type, we will be doing the comparison as unsigned.
4567 The tests above have already verified that LOW and HIGH
4570 So we have to ensure that we will handle large unsigned
4571 values the same way that the current signed bounds treat
4574 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4578 /* For fixed-point modes, we need to pass the saturating flag
4579 as the 2nd parameter. */
4580 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4582 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4583 TYPE_SATURATING (arg0_type
));
4586 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4588 /* A range without an upper bound is, naturally, unbounded.
4589 Since convert would have cropped a very large value, use
4590 the max value for the destination type. */
4592 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4593 : TYPE_MAX_VALUE (arg0_type
);
4595 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4596 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4597 fold_convert_loc (loc
, arg0_type
,
4599 build_int_cst (arg0_type
, 1));
4601 /* If the low bound is specified, "and" the range with the
4602 range for which the original unsigned value will be
4606 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4607 1, fold_convert_loc (loc
, arg0_type
,
4612 in_p
= (n_in_p
== in_p
);
4616 /* Otherwise, "or" the range with the range of the input
4617 that will be interpreted as negative. */
4618 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4619 1, fold_convert_loc (loc
, arg0_type
,
4624 in_p
= (in_p
!= n_in_p
);
4638 /* Given EXP, a logical expression, set the range it is testing into
4639 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4640 actually being tested. *PLOW and *PHIGH will be made of the same
4641 type as the returned expression. If EXP is not a comparison, we
4642 will most likely not be returning a useful value and range. Set
4643 *STRICT_OVERFLOW_P to true if the return value is only valid
4644 because signed overflow is undefined; otherwise, do not change
4645 *STRICT_OVERFLOW_P. */
4648 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4649 bool *strict_overflow_p
)
4651 enum tree_code code
;
4652 tree arg0
, arg1
= NULL_TREE
;
4653 tree exp_type
, nexp
;
4656 location_t loc
= EXPR_LOCATION (exp
);
4658 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4659 and see if we can refine the range. Some of the cases below may not
4660 happen, but it doesn't seem worth worrying about this. We "continue"
4661 the outer loop when we've changed something; otherwise we "break"
4662 the switch, which will "break" the while. */
4665 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4669 code
= TREE_CODE (exp
);
4670 exp_type
= TREE_TYPE (exp
);
4673 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4675 if (TREE_OPERAND_LENGTH (exp
) > 0)
4676 arg0
= TREE_OPERAND (exp
, 0);
4677 if (TREE_CODE_CLASS (code
) == tcc_binary
4678 || TREE_CODE_CLASS (code
) == tcc_comparison
4679 || (TREE_CODE_CLASS (code
) == tcc_expression
4680 && TREE_OPERAND_LENGTH (exp
) > 1))
4681 arg1
= TREE_OPERAND (exp
, 1);
4683 if (arg0
== NULL_TREE
)
4686 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4687 &high
, &in_p
, strict_overflow_p
);
4688 if (nexp
== NULL_TREE
)
4693 /* If EXP is a constant, we can evaluate whether this is true or false. */
4694 if (TREE_CODE (exp
) == INTEGER_CST
)
4696 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4698 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4704 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4708 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4709 type, TYPE, return an expression to test if EXP is in (or out of, depending
4710 on IN_P) the range. Return 0 if the test couldn't be created. */
4713 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4714 tree low
, tree high
)
4716 tree etype
= TREE_TYPE (exp
), value
;
4718 /* Disable this optimization for function pointer expressions
4719 on targets that require function pointer canonicalization. */
4720 if (targetm
.have_canonicalize_funcptr_for_compare ()
4721 && TREE_CODE (etype
) == POINTER_TYPE
4722 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4727 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4729 return invert_truthvalue_loc (loc
, value
);
4734 if (low
== 0 && high
== 0)
4735 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4738 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4739 fold_convert_loc (loc
, etype
, high
));
4742 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4743 fold_convert_loc (loc
, etype
, low
));
4745 if (operand_equal_p (low
, high
, 0))
4746 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4747 fold_convert_loc (loc
, etype
, low
));
4749 if (integer_zerop (low
))
4751 if (! TYPE_UNSIGNED (etype
))
4753 etype
= unsigned_type_for (etype
);
4754 high
= fold_convert_loc (loc
, etype
, high
);
4755 exp
= fold_convert_loc (loc
, etype
, exp
);
4757 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4760 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4761 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4763 int prec
= TYPE_PRECISION (etype
);
4765 if (wi::mask (prec
- 1, false, prec
) == high
)
4767 if (TYPE_UNSIGNED (etype
))
4769 tree signed_etype
= signed_type_for (etype
);
4770 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4772 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4774 etype
= signed_etype
;
4775 exp
= fold_convert_loc (loc
, etype
, exp
);
4777 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4778 build_int_cst (etype
, 0));
4782 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4783 This requires wrap-around arithmetics for the type of the expression.
4784 First make sure that arithmetics in this type is valid, then make sure
4785 that it wraps around. */
4786 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4787 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4788 TYPE_UNSIGNED (etype
));
4790 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4792 tree utype
, minv
, maxv
;
4794 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4795 for the type in question, as we rely on this here. */
4796 utype
= unsigned_type_for (etype
);
4797 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4798 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4799 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4800 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4802 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4809 high
= fold_convert_loc (loc
, etype
, high
);
4810 low
= fold_convert_loc (loc
, etype
, low
);
4811 exp
= fold_convert_loc (loc
, etype
, exp
);
4813 value
= const_binop (MINUS_EXPR
, high
, low
);
4816 if (POINTER_TYPE_P (etype
))
4818 if (value
!= 0 && !TREE_OVERFLOW (value
))
4820 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4821 return build_range_check (loc
, type
,
4822 fold_build_pointer_plus_loc (loc
, exp
, low
),
4823 1, build_int_cst (etype
, 0), value
);
4828 if (value
!= 0 && !TREE_OVERFLOW (value
))
4829 return build_range_check (loc
, type
,
4830 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4831 1, build_int_cst (etype
, 0), value
);
4836 /* Return the predecessor of VAL in its type, handling the infinite case. */
4839 range_predecessor (tree val
)
4841 tree type
= TREE_TYPE (val
);
4843 if (INTEGRAL_TYPE_P (type
)
4844 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4847 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4848 build_int_cst (TREE_TYPE (val
), 1), 0);
4851 /* Return the successor of VAL in its type, handling the infinite case. */
4854 range_successor (tree val
)
4856 tree type
= TREE_TYPE (val
);
4858 if (INTEGRAL_TYPE_P (type
)
4859 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4862 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4863 build_int_cst (TREE_TYPE (val
), 1), 0);
4866 /* Given two ranges, see if we can merge them into one. Return 1 if we
4867 can, 0 if we can't. Set the output range into the specified parameters. */
4870 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4871 tree high0
, int in1_p
, tree low1
, tree high1
)
4879 int lowequal
= ((low0
== 0 && low1
== 0)
4880 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4881 low0
, 0, low1
, 0)));
4882 int highequal
= ((high0
== 0 && high1
== 0)
4883 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4884 high0
, 1, high1
, 1)));
4886 /* Make range 0 be the range that starts first, or ends last if they
4887 start at the same value. Swap them if it isn't. */
4888 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4891 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4892 high1
, 1, high0
, 1))))
4894 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4895 tem
= low0
, low0
= low1
, low1
= tem
;
4896 tem
= high0
, high0
= high1
, high1
= tem
;
4899 /* Now flag two cases, whether the ranges are disjoint or whether the
4900 second range is totally subsumed in the first. Note that the tests
4901 below are simplified by the ones above. */
4902 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4903 high0
, 1, low1
, 0));
4904 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4905 high1
, 1, high0
, 1));
4907 /* We now have four cases, depending on whether we are including or
4908 excluding the two ranges. */
4911 /* If they don't overlap, the result is false. If the second range
4912 is a subset it is the result. Otherwise, the range is from the start
4913 of the second to the end of the first. */
4915 in_p
= 0, low
= high
= 0;
4917 in_p
= 1, low
= low1
, high
= high1
;
4919 in_p
= 1, low
= low1
, high
= high0
;
4922 else if (in0_p
&& ! in1_p
)
4924 /* If they don't overlap, the result is the first range. If they are
4925 equal, the result is false. If the second range is a subset of the
4926 first, and the ranges begin at the same place, we go from just after
4927 the end of the second range to the end of the first. If the second
4928 range is not a subset of the first, or if it is a subset and both
4929 ranges end at the same place, the range starts at the start of the
4930 first range and ends just before the second range.
4931 Otherwise, we can't describe this as a single range. */
4933 in_p
= 1, low
= low0
, high
= high0
;
4934 else if (lowequal
&& highequal
)
4935 in_p
= 0, low
= high
= 0;
4936 else if (subset
&& lowequal
)
4938 low
= range_successor (high1
);
4943 /* We are in the weird situation where high0 > high1 but
4944 high1 has no successor. Punt. */
4948 else if (! subset
|| highequal
)
4951 high
= range_predecessor (low1
);
4955 /* low0 < low1 but low1 has no predecessor. Punt. */
4963 else if (! in0_p
&& in1_p
)
4965 /* If they don't overlap, the result is the second range. If the second
4966 is a subset of the first, the result is false. Otherwise,
4967 the range starts just after the first range and ends at the
4968 end of the second. */
4970 in_p
= 1, low
= low1
, high
= high1
;
4971 else if (subset
|| highequal
)
4972 in_p
= 0, low
= high
= 0;
4975 low
= range_successor (high0
);
4980 /* high1 > high0 but high0 has no successor. Punt. */
4988 /* The case where we are excluding both ranges. Here the complex case
4989 is if they don't overlap. In that case, the only time we have a
4990 range is if they are adjacent. If the second is a subset of the
4991 first, the result is the first. Otherwise, the range to exclude
4992 starts at the beginning of the first range and ends at the end of the
4996 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4997 range_successor (high0
),
4999 in_p
= 0, low
= low0
, high
= high1
;
5002 /* Canonicalize - [min, x] into - [-, x]. */
5003 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
5004 switch (TREE_CODE (TREE_TYPE (low0
)))
5007 if (TYPE_PRECISION (TREE_TYPE (low0
))
5008 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
5012 if (tree_int_cst_equal (low0
,
5013 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
5017 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
5018 && integer_zerop (low0
))
5025 /* Canonicalize - [x, max] into - [x, -]. */
5026 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
5027 switch (TREE_CODE (TREE_TYPE (high1
)))
5030 if (TYPE_PRECISION (TREE_TYPE (high1
))
5031 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
5035 if (tree_int_cst_equal (high1
,
5036 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
5040 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
5041 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
5043 build_int_cst (TREE_TYPE (high1
), 1),
5051 /* The ranges might be also adjacent between the maximum and
5052 minimum values of the given type. For
5053 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5054 return + [x + 1, y - 1]. */
5055 if (low0
== 0 && high1
== 0)
5057 low
= range_successor (high0
);
5058 high
= range_predecessor (low1
);
5059 if (low
== 0 || high
== 0)
5069 in_p
= 0, low
= low0
, high
= high0
;
5071 in_p
= 0, low
= low0
, high
= high1
;
5074 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
5079 /* Subroutine of fold, looking inside expressions of the form
5080 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5081 of the COND_EXPR. This function is being used also to optimize
5082 A op B ? C : A, by reversing the comparison first.
5084 Return a folded expression whose code is not a COND_EXPR
5085 anymore, or NULL_TREE if no folding opportunity is found. */
5088 fold_cond_expr_with_comparison (location_t loc
, tree type
,
5089 tree arg0
, tree arg1
, tree arg2
)
5091 enum tree_code comp_code
= TREE_CODE (arg0
);
5092 tree arg00
= TREE_OPERAND (arg0
, 0);
5093 tree arg01
= TREE_OPERAND (arg0
, 1);
5094 tree arg1_type
= TREE_TYPE (arg1
);
5100 /* If we have A op 0 ? A : -A, consider applying the following
5103 A == 0? A : -A same as -A
5104 A != 0? A : -A same as A
5105 A >= 0? A : -A same as abs (A)
5106 A > 0? A : -A same as abs (A)
5107 A <= 0? A : -A same as -abs (A)
5108 A < 0? A : -A same as -abs (A)
5110 None of these transformations work for modes with signed
5111 zeros. If A is +/-0, the first two transformations will
5112 change the sign of the result (from +0 to -0, or vice
5113 versa). The last four will fix the sign of the result,
5114 even though the original expressions could be positive or
5115 negative, depending on the sign of A.
5117 Note that all these transformations are correct if A is
5118 NaN, since the two alternatives (A and -A) are also NaNs. */
5119 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5120 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
5121 ? real_zerop (arg01
)
5122 : integer_zerop (arg01
))
5123 && ((TREE_CODE (arg2
) == NEGATE_EXPR
5124 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
5125 /* In the case that A is of the form X-Y, '-A' (arg2) may
5126 have already been folded to Y-X, check for that. */
5127 || (TREE_CODE (arg1
) == MINUS_EXPR
5128 && TREE_CODE (arg2
) == MINUS_EXPR
5129 && operand_equal_p (TREE_OPERAND (arg1
, 0),
5130 TREE_OPERAND (arg2
, 1), 0)
5131 && operand_equal_p (TREE_OPERAND (arg1
, 1),
5132 TREE_OPERAND (arg2
, 0), 0))))
5137 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
5138 return fold_convert_loc (loc
, type
, negate_expr (tem
));
5141 return fold_convert_loc (loc
, type
, arg1
);
5144 if (flag_trapping_math
)
5149 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5151 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5152 return fold_convert_loc (loc
, type
, tem
);
5155 if (flag_trapping_math
)
5160 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
5162 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
5163 return negate_expr (fold_convert_loc (loc
, type
, tem
));
5165 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5169 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5170 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5171 both transformations are correct when A is NaN: A != 0
5172 is then true, and A == 0 is false. */
5174 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5175 && integer_zerop (arg01
) && integer_zerop (arg2
))
5177 if (comp_code
== NE_EXPR
)
5178 return fold_convert_loc (loc
, type
, arg1
);
5179 else if (comp_code
== EQ_EXPR
)
5180 return build_zero_cst (type
);
5183 /* Try some transformations of A op B ? A : B.
5185 A == B? A : B same as B
5186 A != B? A : B same as A
5187 A >= B? A : B same as max (A, B)
5188 A > B? A : B same as max (B, A)
5189 A <= B? A : B same as min (A, B)
5190 A < B? A : B same as min (B, A)
5192 As above, these transformations don't work in the presence
5193 of signed zeros. For example, if A and B are zeros of
5194 opposite sign, the first two transformations will change
5195 the sign of the result. In the last four, the original
5196 expressions give different results for (A=+0, B=-0) and
5197 (A=-0, B=+0), but the transformed expressions do not.
5199 The first two transformations are correct if either A or B
5200 is a NaN. In the first transformation, the condition will
5201 be false, and B will indeed be chosen. In the case of the
5202 second transformation, the condition A != B will be true,
5203 and A will be chosen.
5205 The conversions to max() and min() are not correct if B is
5206 a number and A is not. The conditions in the original
5207 expressions will be false, so all four give B. The min()
5208 and max() versions would give a NaN instead. */
5209 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5210 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
5211 /* Avoid these transformations if the COND_EXPR may be used
5212 as an lvalue in the C++ front-end. PR c++/19199. */
5214 || VECTOR_TYPE_P (type
)
5215 || (! lang_GNU_CXX ()
5216 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5217 || ! maybe_lvalue_p (arg1
)
5218 || ! maybe_lvalue_p (arg2
)))
5220 tree comp_op0
= arg00
;
5221 tree comp_op1
= arg01
;
5222 tree comp_type
= TREE_TYPE (comp_op0
);
5227 return fold_convert_loc (loc
, type
, arg2
);
5229 return fold_convert_loc (loc
, type
, arg1
);
5234 /* In C++ a ?: expression can be an lvalue, so put the
5235 operand which will be used if they are equal first
5236 so that we can convert this back to the
5237 corresponding COND_EXPR. */
5238 if (!HONOR_NANS (arg1
))
5240 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5241 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5242 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5243 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5244 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5245 comp_op1
, comp_op0
);
5246 return fold_convert_loc (loc
, type
, tem
);
5253 if (!HONOR_NANS (arg1
))
5255 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5256 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5257 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5258 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5259 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5260 comp_op1
, comp_op0
);
5261 return fold_convert_loc (loc
, type
, tem
);
5265 if (!HONOR_NANS (arg1
))
5266 return fold_convert_loc (loc
, type
, arg2
);
5269 if (!HONOR_NANS (arg1
))
5270 return fold_convert_loc (loc
, type
, arg1
);
5273 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5283 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5284 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5285 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5289 /* EXP is some logical combination of boolean tests. See if we can
5290 merge it into some range test. Return the new tree if so. */
5293 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5296 int or_op
= (code
== TRUTH_ORIF_EXPR
5297 || code
== TRUTH_OR_EXPR
);
5298 int in0_p
, in1_p
, in_p
;
5299 tree low0
, low1
, low
, high0
, high1
, high
;
5300 bool strict_overflow_p
= false;
5302 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5303 "when simplifying range test");
5305 if (!INTEGRAL_TYPE_P (type
))
5308 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5309 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5311 /* If this is an OR operation, invert both sides; we will invert
5312 again at the end. */
5314 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5316 /* If both expressions are the same, if we can merge the ranges, and we
5317 can build the range test, return it or it inverted. If one of the
5318 ranges is always true or always false, consider it to be the same
5319 expression as the other. */
5320 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5321 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5323 && 0 != (tem
= (build_range_check (loc
, type
,
5325 : rhs
!= 0 ? rhs
: integer_zero_node
,
5328 if (strict_overflow_p
)
5329 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5330 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5333 /* On machines where the branch cost is expensive, if this is a
5334 short-circuited branch and the underlying object on both sides
5335 is the same, make a non-short-circuit operation. */
5336 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5337 && lhs
!= 0 && rhs
!= 0
5338 && (code
== TRUTH_ANDIF_EXPR
5339 || code
== TRUTH_ORIF_EXPR
)
5340 && operand_equal_p (lhs
, rhs
, 0))
5342 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5343 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5344 which cases we can't do this. */
5345 if (simple_operand_p (lhs
))
5346 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5347 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5350 else if (!lang_hooks
.decls
.global_bindings_p ()
5351 && !CONTAINS_PLACEHOLDER_P (lhs
))
5353 tree common
= save_expr (lhs
);
5355 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5356 or_op
? ! in0_p
: in0_p
,
5358 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5359 or_op
? ! in1_p
: in1_p
,
5362 if (strict_overflow_p
)
5363 fold_overflow_warning (warnmsg
,
5364 WARN_STRICT_OVERFLOW_COMPARISON
);
5365 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5366 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5375 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5376 bit value. Arrange things so the extra bits will be set to zero if and
5377 only if C is signed-extended to its full width. If MASK is nonzero,
5378 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5381 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5383 tree type
= TREE_TYPE (c
);
5384 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5387 if (p
== modesize
|| unsignedp
)
5390 /* We work by getting just the sign bit into the low-order bit, then
5391 into the high-order bit, then sign-extend. We then XOR that value
5393 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5395 /* We must use a signed type in order to get an arithmetic right shift.
5396 However, we must also avoid introducing accidental overflows, so that
5397 a subsequent call to integer_zerop will work. Hence we must
5398 do the type conversion here. At this point, the constant is either
5399 zero or one, and the conversion to a signed type can never overflow.
5400 We could get an overflow if this conversion is done anywhere else. */
5401 if (TYPE_UNSIGNED (type
))
5402 temp
= fold_convert (signed_type_for (type
), temp
);
5404 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5405 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5407 temp
= const_binop (BIT_AND_EXPR
, temp
,
5408 fold_convert (TREE_TYPE (c
), mask
));
5409 /* If necessary, convert the type back to match the type of C. */
5410 if (TYPE_UNSIGNED (type
))
5411 temp
= fold_convert (type
, temp
);
5413 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5416 /* For an expression that has the form
5420 we can drop one of the inner expressions and simplify to
5424 LOC is the location of the resulting expression. OP is the inner
5425 logical operation; the left-hand side in the examples above, while CMPOP
5426 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5427 removing a condition that guards another, as in
5428 (A != NULL && A->...) || A == NULL
5429 which we must not transform. If RHS_ONLY is true, only eliminate the
5430 right-most operand of the inner logical operation. */
5433 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5436 tree type
= TREE_TYPE (cmpop
);
5437 enum tree_code code
= TREE_CODE (cmpop
);
5438 enum tree_code truthop_code
= TREE_CODE (op
);
5439 tree lhs
= TREE_OPERAND (op
, 0);
5440 tree rhs
= TREE_OPERAND (op
, 1);
5441 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5442 enum tree_code rhs_code
= TREE_CODE (rhs
);
5443 enum tree_code lhs_code
= TREE_CODE (lhs
);
5444 enum tree_code inv_code
;
5446 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5449 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5452 if (rhs_code
== truthop_code
)
5454 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5455 if (newrhs
!= NULL_TREE
)
5458 rhs_code
= TREE_CODE (rhs
);
5461 if (lhs_code
== truthop_code
&& !rhs_only
)
5463 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5464 if (newlhs
!= NULL_TREE
)
5467 lhs_code
= TREE_CODE (lhs
);
5471 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5472 if (inv_code
== rhs_code
5473 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5474 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5476 if (!rhs_only
&& inv_code
== lhs_code
5477 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5478 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5480 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5481 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5486 /* Find ways of folding logical expressions of LHS and RHS:
5487 Try to merge two comparisons to the same innermost item.
5488 Look for range tests like "ch >= '0' && ch <= '9'".
5489 Look for combinations of simple terms on machines with expensive branches
5490 and evaluate the RHS unconditionally.
5492 For example, if we have p->a == 2 && p->b == 4 and we can make an
5493 object large enough to span both A and B, we can do this with a comparison
5494 against the object ANDed with the a mask.
5496 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5497 operations to do this with one comparison.
5499 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5500 function and the one above.
5502 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5503 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5505 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5508 We return the simplified tree or 0 if no optimization is possible. */
5511 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5514 /* If this is the "or" of two comparisons, we can do something if
5515 the comparisons are NE_EXPR. If this is the "and", we can do something
5516 if the comparisons are EQ_EXPR. I.e.,
5517 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5519 WANTED_CODE is this operation code. For single bit fields, we can
5520 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5521 comparison for one-bit fields. */
5523 enum tree_code wanted_code
;
5524 enum tree_code lcode
, rcode
;
5525 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5526 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5527 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5528 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5529 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5530 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5531 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5532 int ll_reversep
, lr_reversep
, rl_reversep
, rr_reversep
;
5533 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5534 machine_mode lnmode
, rnmode
;
5535 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5536 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5537 tree l_const
, r_const
;
5538 tree lntype
, rntype
, result
;
5539 HOST_WIDE_INT first_bit
, end_bit
;
5542 /* Start by getting the comparison codes. Fail if anything is volatile.
5543 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5544 it were surrounded with a NE_EXPR. */
5546 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5549 lcode
= TREE_CODE (lhs
);
5550 rcode
= TREE_CODE (rhs
);
5552 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5554 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5555 build_int_cst (TREE_TYPE (lhs
), 0));
5559 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5561 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5562 build_int_cst (TREE_TYPE (rhs
), 0));
5566 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5567 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5570 ll_arg
= TREE_OPERAND (lhs
, 0);
5571 lr_arg
= TREE_OPERAND (lhs
, 1);
5572 rl_arg
= TREE_OPERAND (rhs
, 0);
5573 rr_arg
= TREE_OPERAND (rhs
, 1);
5575 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5576 if (simple_operand_p (ll_arg
)
5577 && simple_operand_p (lr_arg
))
5579 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5580 && operand_equal_p (lr_arg
, rr_arg
, 0))
5582 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5583 truth_type
, ll_arg
, lr_arg
);
5587 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5588 && operand_equal_p (lr_arg
, rl_arg
, 0))
5590 result
= combine_comparisons (loc
, code
, lcode
,
5591 swap_tree_comparison (rcode
),
5592 truth_type
, ll_arg
, lr_arg
);
5598 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5599 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5601 /* If the RHS can be evaluated unconditionally and its operands are
5602 simple, it wins to evaluate the RHS unconditionally on machines
5603 with expensive branches. In this case, this isn't a comparison
5604 that can be merged. */
5606 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5608 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5609 && simple_operand_p (rl_arg
)
5610 && simple_operand_p (rr_arg
))
5612 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5613 if (code
== TRUTH_OR_EXPR
5614 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5615 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5616 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5617 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5618 return build2_loc (loc
, NE_EXPR
, truth_type
,
5619 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5621 build_int_cst (TREE_TYPE (ll_arg
), 0));
5623 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5624 if (code
== TRUTH_AND_EXPR
5625 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5626 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5627 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5628 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5629 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5630 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5632 build_int_cst (TREE_TYPE (ll_arg
), 0));
5635 /* See if the comparisons can be merged. Then get all the parameters for
5638 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5639 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5642 ll_reversep
= lr_reversep
= rl_reversep
= rr_reversep
= 0;
5644 ll_inner
= decode_field_reference (loc
, &ll_arg
,
5645 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5646 &ll_unsignedp
, &ll_reversep
, &volatilep
,
5647 &ll_mask
, &ll_and_mask
);
5648 lr_inner
= decode_field_reference (loc
, &lr_arg
,
5649 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5650 &lr_unsignedp
, &lr_reversep
, &volatilep
,
5651 &lr_mask
, &lr_and_mask
);
5652 rl_inner
= decode_field_reference (loc
, &rl_arg
,
5653 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5654 &rl_unsignedp
, &rl_reversep
, &volatilep
,
5655 &rl_mask
, &rl_and_mask
);
5656 rr_inner
= decode_field_reference (loc
, &rr_arg
,
5657 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5658 &rr_unsignedp
, &rr_reversep
, &volatilep
,
5659 &rr_mask
, &rr_and_mask
);
5661 /* It must be true that the inner operation on the lhs of each
5662 comparison must be the same if we are to be able to do anything.
5663 Then see if we have constants. If not, the same must be true for
5666 || ll_reversep
!= rl_reversep
5667 || ll_inner
== 0 || rl_inner
== 0
5668 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5671 if (TREE_CODE (lr_arg
) == INTEGER_CST
5672 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5674 l_const
= lr_arg
, r_const
= rr_arg
;
5675 lr_reversep
= ll_reversep
;
5677 else if (lr_reversep
!= rr_reversep
5678 || lr_inner
== 0 || rr_inner
== 0
5679 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5682 l_const
= r_const
= 0;
5684 /* If either comparison code is not correct for our logical operation,
5685 fail. However, we can convert a one-bit comparison against zero into
5686 the opposite comparison against that bit being set in the field. */
5688 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5689 if (lcode
!= wanted_code
)
5691 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5693 /* Make the left operand unsigned, since we are only interested
5694 in the value of one bit. Otherwise we are doing the wrong
5703 /* This is analogous to the code for l_const above. */
5704 if (rcode
!= wanted_code
)
5706 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5715 /* See if we can find a mode that contains both fields being compared on
5716 the left. If we can't, fail. Otherwise, update all constants and masks
5717 to be relative to a field of that size. */
5718 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5719 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5720 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5721 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5723 if (lnmode
== VOIDmode
)
5726 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5727 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5728 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5729 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5731 if (ll_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5733 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5734 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5737 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5738 size_int (xll_bitpos
));
5739 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5740 size_int (xrl_bitpos
));
5744 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5745 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5746 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5747 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5748 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5751 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5753 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5758 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5759 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5760 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5761 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5762 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5765 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5767 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5771 /* If the right sides are not constant, do the same for it. Also,
5772 disallow this optimization if a size or signedness mismatch occurs
5773 between the left and right sides. */
5776 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5777 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5778 /* Make sure the two fields on the right
5779 correspond to the left without being swapped. */
5780 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5783 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5784 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5785 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5786 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5788 if (rnmode
== VOIDmode
)
5791 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5792 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5793 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5794 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5796 if (lr_reversep
? !BYTES_BIG_ENDIAN
: BYTES_BIG_ENDIAN
)
5798 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5799 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5802 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5804 size_int (xlr_bitpos
));
5805 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5807 size_int (xrr_bitpos
));
5809 /* Make a mask that corresponds to both fields being compared.
5810 Do this for both items being compared. If the operands are the
5811 same size and the bits being compared are in the same position
5812 then we can do this by masking both and comparing the masked
5814 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5815 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5816 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5818 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5819 lntype
, lnbitsize
, lnbitpos
,
5820 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5821 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5822 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5824 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
,
5825 rntype
, rnbitsize
, rnbitpos
,
5826 lr_unsignedp
|| rr_unsignedp
, lr_reversep
);
5827 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5828 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5830 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5833 /* There is still another way we can do something: If both pairs of
5834 fields being compared are adjacent, we may be able to make a wider
5835 field containing them both.
5837 Note that we still must mask the lhs/rhs expressions. Furthermore,
5838 the mask must be shifted to account for the shift done by
5839 make_bit_field_ref. */
5840 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5841 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5842 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5843 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5847 lhs
= make_bit_field_ref (loc
, ll_inner
, ll_arg
, lntype
,
5848 ll_bitsize
+ rl_bitsize
,
5849 MIN (ll_bitpos
, rl_bitpos
),
5850 ll_unsignedp
, ll_reversep
);
5851 rhs
= make_bit_field_ref (loc
, lr_inner
, lr_arg
, rntype
,
5852 lr_bitsize
+ rr_bitsize
,
5853 MIN (lr_bitpos
, rr_bitpos
),
5854 lr_unsignedp
, lr_reversep
);
5856 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5857 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5858 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5859 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5861 /* Convert to the smaller type before masking out unwanted bits. */
5863 if (lntype
!= rntype
)
5865 if (lnbitsize
> rnbitsize
)
5867 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5868 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5871 else if (lnbitsize
< rnbitsize
)
5873 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5874 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5879 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5880 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5882 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5883 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5885 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5891 /* Handle the case of comparisons with constants. If there is something in
5892 common between the masks, those bits of the constants must be the same.
5893 If not, the condition is always false. Test for this to avoid generating
5894 incorrect code below. */
5895 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5896 if (! integer_zerop (result
)
5897 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5898 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5900 if (wanted_code
== NE_EXPR
)
5902 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5903 return constant_boolean_node (true, truth_type
);
5907 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5908 return constant_boolean_node (false, truth_type
);
5912 /* Construct the expression we will return. First get the component
5913 reference we will make. Unless the mask is all ones the width of
5914 that field, perform the mask operation. Then compare with the
5916 result
= make_bit_field_ref (loc
, ll_inner
, ll_arg
,
5917 lntype
, lnbitsize
, lnbitpos
,
5918 ll_unsignedp
|| rl_unsignedp
, ll_reversep
);
5920 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5921 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5922 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5924 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5925 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5928 /* T is an integer expression that is being multiplied, divided, or taken a
5929 modulus (CODE says which and what kind of divide or modulus) by a
5930 constant C. See if we can eliminate that operation by folding it with
5931 other operations already in T. WIDE_TYPE, if non-null, is a type that
5932 should be used for the computation if wider than our type.
5934 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5935 (X * 2) + (Y * 4). We must, however, be assured that either the original
5936 expression would not overflow or that overflow is undefined for the type
5937 in the language in question.
5939 If we return a non-null expression, it is an equivalent form of the
5940 original computation, but need not be in the original type.
5942 We set *STRICT_OVERFLOW_P to true if the return values depends on
5943 signed overflow being undefined. Otherwise we do not change
5944 *STRICT_OVERFLOW_P. */
5947 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5948 bool *strict_overflow_p
)
5950 /* To avoid exponential search depth, refuse to allow recursion past
5951 three levels. Beyond that (1) it's highly unlikely that we'll find
5952 something interesting and (2) we've probably processed it before
5953 when we built the inner expression. */
5962 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
5969 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5970 bool *strict_overflow_p
)
5972 tree type
= TREE_TYPE (t
);
5973 enum tree_code tcode
= TREE_CODE (t
);
5974 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
5975 > GET_MODE_SIZE (TYPE_MODE (type
)))
5976 ? wide_type
: type
);
5978 int same_p
= tcode
== code
;
5979 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
5980 bool sub_strict_overflow_p
;
5982 /* Don't deal with constants of zero here; they confuse the code below. */
5983 if (integer_zerop (c
))
5986 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
5987 op0
= TREE_OPERAND (t
, 0);
5989 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
5990 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
5992 /* Note that we need not handle conditional operations here since fold
5993 already handles those cases. So just do arithmetic here. */
5997 /* For a constant, we can always simplify if we are a multiply
5998 or (for divide and modulus) if it is a multiple of our constant. */
5999 if (code
== MULT_EXPR
6000 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6002 tree tem
= const_binop (code
, fold_convert (ctype
, t
),
6003 fold_convert (ctype
, c
));
6004 /* If the multiplication overflowed, we lost information on it.
6005 See PR68142 and PR69845. */
6006 if (TREE_OVERFLOW (tem
))
6012 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6013 /* If op0 is an expression ... */
6014 if ((COMPARISON_CLASS_P (op0
)
6015 || UNARY_CLASS_P (op0
)
6016 || BINARY_CLASS_P (op0
)
6017 || VL_EXP_CLASS_P (op0
)
6018 || EXPRESSION_CLASS_P (op0
))
6019 /* ... and has wrapping overflow, and its type is smaller
6020 than ctype, then we cannot pass through as widening. */
6021 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6022 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6023 && (TYPE_PRECISION (ctype
)
6024 > TYPE_PRECISION (TREE_TYPE (op0
))))
6025 /* ... or this is a truncation (t is narrower than op0),
6026 then we cannot pass through this narrowing. */
6027 || (TYPE_PRECISION (type
)
6028 < TYPE_PRECISION (TREE_TYPE (op0
)))
6029 /* ... or signedness changes for division or modulus,
6030 then we cannot pass through this conversion. */
6031 || (code
!= MULT_EXPR
6032 && (TYPE_UNSIGNED (ctype
)
6033 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6034 /* ... or has undefined overflow while the converted to
6035 type has not, we cannot do the operation in the inner type
6036 as that would introduce undefined overflow. */
6037 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6038 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6039 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6042 /* Pass the constant down and see if we can make a simplification. If
6043 we can, replace this expression with the inner simplification for
6044 possible later conversion to our or some other type. */
6045 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6046 && TREE_CODE (t2
) == INTEGER_CST
6047 && !TREE_OVERFLOW (t2
)
6048 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6050 ? ctype
: NULL_TREE
,
6051 strict_overflow_p
))))
6056 /* If widening the type changes it from signed to unsigned, then we
6057 must avoid building ABS_EXPR itself as unsigned. */
6058 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6060 tree cstype
= (*signed_type_for
) (ctype
);
6061 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6064 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6065 return fold_convert (ctype
, t1
);
6069 /* If the constant is negative, we cannot simplify this. */
6070 if (tree_int_cst_sgn (c
) == -1)
6074 /* For division and modulus, type can't be unsigned, as e.g.
6075 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6076 For signed types, even with wrapping overflow, this is fine. */
6077 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6079 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6081 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6084 case MIN_EXPR
: case MAX_EXPR
:
6085 /* If widening the type changes the signedness, then we can't perform
6086 this optimization as that changes the result. */
6087 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6090 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6091 sub_strict_overflow_p
= false;
6092 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6093 &sub_strict_overflow_p
)) != 0
6094 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6095 &sub_strict_overflow_p
)) != 0)
6097 if (tree_int_cst_sgn (c
) < 0)
6098 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6099 if (sub_strict_overflow_p
)
6100 *strict_overflow_p
= true;
6101 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6102 fold_convert (ctype
, t2
));
6106 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6107 /* If the second operand is constant, this is a multiplication
6108 or floor division, by a power of two, so we can treat it that
6109 way unless the multiplier or divisor overflows. Signed
6110 left-shift overflow is implementation-defined rather than
6111 undefined in C90, so do not convert signed left shift into
6113 if (TREE_CODE (op1
) == INTEGER_CST
6114 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6115 /* const_binop may not detect overflow correctly,
6116 so check for it explicitly here. */
6117 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6118 && 0 != (t1
= fold_convert (ctype
,
6119 const_binop (LSHIFT_EXPR
,
6122 && !TREE_OVERFLOW (t1
))
6123 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6124 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6126 fold_convert (ctype
, op0
),
6128 c
, code
, wide_type
, strict_overflow_p
);
6131 case PLUS_EXPR
: case MINUS_EXPR
:
6132 /* See if we can eliminate the operation on both sides. If we can, we
6133 can return a new PLUS or MINUS. If we can't, the only remaining
6134 cases where we can do anything are if the second operand is a
6136 sub_strict_overflow_p
= false;
6137 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6138 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6139 if (t1
!= 0 && t2
!= 0
6140 && (code
== MULT_EXPR
6141 /* If not multiplication, we can only do this if both operands
6142 are divisible by c. */
6143 || (multiple_of_p (ctype
, op0
, c
)
6144 && multiple_of_p (ctype
, op1
, c
))))
6146 if (sub_strict_overflow_p
)
6147 *strict_overflow_p
= true;
6148 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6149 fold_convert (ctype
, t2
));
6152 /* If this was a subtraction, negate OP1 and set it to be an addition.
6153 This simplifies the logic below. */
6154 if (tcode
== MINUS_EXPR
)
6156 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6157 /* If OP1 was not easily negatable, the constant may be OP0. */
6158 if (TREE_CODE (op0
) == INTEGER_CST
)
6160 std::swap (op0
, op1
);
6165 if (TREE_CODE (op1
) != INTEGER_CST
)
6168 /* If either OP1 or C are negative, this optimization is not safe for
6169 some of the division and remainder types while for others we need
6170 to change the code. */
6171 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6173 if (code
== CEIL_DIV_EXPR
)
6174 code
= FLOOR_DIV_EXPR
;
6175 else if (code
== FLOOR_DIV_EXPR
)
6176 code
= CEIL_DIV_EXPR
;
6177 else if (code
!= MULT_EXPR
6178 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6182 /* If it's a multiply or a division/modulus operation of a multiple
6183 of our constant, do the operation and verify it doesn't overflow. */
6184 if (code
== MULT_EXPR
6185 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6187 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6188 fold_convert (ctype
, c
));
6189 /* We allow the constant to overflow with wrapping semantics. */
6191 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6197 /* If we have an unsigned type, we cannot widen the operation since it
6198 will change the result if the original computation overflowed. */
6199 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6202 /* If we were able to eliminate our operation from the first side,
6203 apply our operation to the second side and reform the PLUS. */
6204 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6205 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6207 /* The last case is if we are a multiply. In that case, we can
6208 apply the distributive law to commute the multiply and addition
6209 if the multiplication of the constants doesn't overflow
6210 and overflow is defined. With undefined overflow
6211 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6212 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6213 return fold_build2 (tcode
, ctype
,
6214 fold_build2 (code
, ctype
,
6215 fold_convert (ctype
, op0
),
6216 fold_convert (ctype
, c
)),
6222 /* We have a special case here if we are doing something like
6223 (C * 8) % 4 since we know that's zero. */
6224 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6225 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6226 /* If the multiplication can overflow we cannot optimize this. */
6227 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6228 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6229 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6231 *strict_overflow_p
= true;
6232 return omit_one_operand (type
, integer_zero_node
, op0
);
6235 /* ... fall through ... */
6237 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6238 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6239 /* If we can extract our operation from the LHS, do so and return a
6240 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6241 do something only if the second operand is a constant. */
6243 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6244 strict_overflow_p
)) != 0)
6245 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6246 fold_convert (ctype
, op1
));
6247 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6248 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6249 strict_overflow_p
)) != 0)
6250 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6251 fold_convert (ctype
, t1
));
6252 else if (TREE_CODE (op1
) != INTEGER_CST
)
6255 /* If these are the same operation types, we can associate them
6256 assuming no overflow. */
6259 bool overflow_p
= false;
6260 bool overflow_mul_p
;
6261 signop sign
= TYPE_SIGN (ctype
);
6262 unsigned prec
= TYPE_PRECISION (ctype
);
6263 wide_int mul
= wi::mul (wi::to_wide (op1
, prec
),
6264 wi::to_wide (c
, prec
),
6265 sign
, &overflow_mul_p
);
6266 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6268 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6271 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6272 wide_int_to_tree (ctype
, mul
));
6275 /* If these operations "cancel" each other, we have the main
6276 optimizations of this pass, which occur when either constant is a
6277 multiple of the other, in which case we replace this with either an
6278 operation or CODE or TCODE.
6280 If we have an unsigned type, we cannot do this since it will change
6281 the result if the original computation overflowed. */
6282 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6283 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6284 || (tcode
== MULT_EXPR
6285 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6286 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6287 && code
!= MULT_EXPR
)))
6289 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6291 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6292 *strict_overflow_p
= true;
6293 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6294 fold_convert (ctype
,
6295 const_binop (TRUNC_DIV_EXPR
,
6298 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6300 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6301 *strict_overflow_p
= true;
6302 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6303 fold_convert (ctype
,
6304 const_binop (TRUNC_DIV_EXPR
,
6317 /* Return a node which has the indicated constant VALUE (either 0 or
6318 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6319 and is of the indicated TYPE. */
6322 constant_boolean_node (bool value
, tree type
)
6324 if (type
== integer_type_node
)
6325 return value
? integer_one_node
: integer_zero_node
;
6326 else if (type
== boolean_type_node
)
6327 return value
? boolean_true_node
: boolean_false_node
;
6328 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6329 return build_vector_from_val (type
,
6330 build_int_cst (TREE_TYPE (type
),
6333 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6337 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6338 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6339 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6340 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6341 COND is the first argument to CODE; otherwise (as in the example
6342 given here), it is the second argument. TYPE is the type of the
6343 original expression. Return NULL_TREE if no simplification is
6347 fold_binary_op_with_conditional_arg (location_t loc
,
6348 enum tree_code code
,
6349 tree type
, tree op0
, tree op1
,
6350 tree cond
, tree arg
, int cond_first_p
)
6352 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6353 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6354 tree test
, true_value
, false_value
;
6355 tree lhs
= NULL_TREE
;
6356 tree rhs
= NULL_TREE
;
6357 enum tree_code cond_code
= COND_EXPR
;
6359 if (TREE_CODE (cond
) == COND_EXPR
6360 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6362 test
= TREE_OPERAND (cond
, 0);
6363 true_value
= TREE_OPERAND (cond
, 1);
6364 false_value
= TREE_OPERAND (cond
, 2);
6365 /* If this operand throws an expression, then it does not make
6366 sense to try to perform a logical or arithmetic operation
6368 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6370 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6373 else if (!(TREE_CODE (type
) != VECTOR_TYPE
6374 && TREE_CODE (TREE_TYPE (cond
)) == VECTOR_TYPE
))
6376 tree testtype
= TREE_TYPE (cond
);
6378 true_value
= constant_boolean_node (true, testtype
);
6379 false_value
= constant_boolean_node (false, testtype
);
6382 /* Detect the case of mixing vector and scalar types - bail out. */
6385 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6386 cond_code
= VEC_COND_EXPR
;
6388 /* This transformation is only worthwhile if we don't have to wrap ARG
6389 in a SAVE_EXPR and the operation can be simplified without recursing
6390 on at least one of the branches once its pushed inside the COND_EXPR. */
6391 if (!TREE_CONSTANT (arg
)
6392 && (TREE_SIDE_EFFECTS (arg
)
6393 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6394 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6397 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6400 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6402 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6404 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6408 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6410 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6412 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6415 /* Check that we have simplified at least one of the branches. */
6416 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6419 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6423 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6425 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6426 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6427 ADDEND is the same as X.
6429 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6430 and finite. The problematic cases are when X is zero, and its mode
6431 has signed zeros. In the case of rounding towards -infinity,
6432 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6433 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6436 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6438 if (!real_zerop (addend
))
6441 /* Don't allow the fold with -fsignaling-nans. */
6442 if (HONOR_SNANS (element_mode (type
)))
6445 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6446 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6449 /* In a vector or complex, we would need to check the sign of all zeros. */
6450 if (TREE_CODE (addend
) != REAL_CST
)
6453 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6454 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6457 /* The mode has signed zeros, and we have to honor their sign.
6458 In this situation, there is only one case we can return true for.
6459 X - 0 is the same as X unless rounding towards -infinity is
6461 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6464 /* Subroutine of fold() that optimizes comparisons of a division by
6465 a nonzero integer constant against an integer constant, i.e.
6468 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6469 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6470 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6472 The function returns the constant folded tree if a simplification
6473 can be made, and NULL_TREE otherwise. */
6476 fold_div_compare (location_t loc
,
6477 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6479 tree prod
, tmp
, hi
, lo
;
6480 tree arg00
= TREE_OPERAND (arg0
, 0);
6481 tree arg01
= TREE_OPERAND (arg0
, 1);
6482 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6483 bool neg_overflow
= false;
6486 /* We have to do this the hard way to detect unsigned overflow.
6487 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6488 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6489 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6490 neg_overflow
= false;
6492 if (sign
== UNSIGNED
)
6494 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6495 build_int_cst (TREE_TYPE (arg01
), 1));
6498 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6499 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6500 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6501 -1, overflow
| TREE_OVERFLOW (prod
));
6503 else if (tree_int_cst_sgn (arg01
) >= 0)
6505 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6506 build_int_cst (TREE_TYPE (arg01
), 1));
6507 switch (tree_int_cst_sgn (arg1
))
6510 neg_overflow
= true;
6511 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6516 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6521 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6531 /* A negative divisor reverses the relational operators. */
6532 code
= swap_tree_comparison (code
);
6534 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6535 build_int_cst (TREE_TYPE (arg01
), 1));
6536 switch (tree_int_cst_sgn (arg1
))
6539 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6544 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6549 neg_overflow
= true;
6550 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6562 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6563 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6564 if (TREE_OVERFLOW (hi
))
6565 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6566 if (TREE_OVERFLOW (lo
))
6567 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6568 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6571 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6572 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6573 if (TREE_OVERFLOW (hi
))
6574 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6575 if (TREE_OVERFLOW (lo
))
6576 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6577 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6580 if (TREE_OVERFLOW (lo
))
6582 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6583 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6585 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6588 if (TREE_OVERFLOW (hi
))
6590 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6591 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6593 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6596 if (TREE_OVERFLOW (hi
))
6598 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6599 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6601 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6604 if (TREE_OVERFLOW (lo
))
6606 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6607 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6609 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6619 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6620 equality/inequality test, then return a simplified form of the test
6621 using a sign testing. Otherwise return NULL. TYPE is the desired
6625 fold_single_bit_test_into_sign_test (location_t loc
,
6626 enum tree_code code
, tree arg0
, tree arg1
,
6629 /* If this is testing a single bit, we can optimize the test. */
6630 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6631 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6632 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6634 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6635 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6636 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6638 if (arg00
!= NULL_TREE
6639 /* This is only a win if casting to a signed type is cheap,
6640 i.e. when arg00's type is not a partial mode. */
6641 && TYPE_PRECISION (TREE_TYPE (arg00
))
6642 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6644 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6645 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6647 fold_convert_loc (loc
, stype
, arg00
),
6648 build_int_cst (stype
, 0));
6655 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6656 equality/inequality test, then return a simplified form of
6657 the test using shifts and logical operations. Otherwise return
6658 NULL. TYPE is the desired result type. */
6661 fold_single_bit_test (location_t loc
, enum tree_code code
,
6662 tree arg0
, tree arg1
, tree result_type
)
6664 /* If this is testing a single bit, we can optimize the test. */
6665 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6666 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6667 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6669 tree inner
= TREE_OPERAND (arg0
, 0);
6670 tree type
= TREE_TYPE (arg0
);
6671 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6672 machine_mode operand_mode
= TYPE_MODE (type
);
6674 tree signed_type
, unsigned_type
, intermediate_type
;
6677 /* First, see if we can fold the single bit test into a sign-bit
6679 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6684 /* Otherwise we have (A & C) != 0 where C is a single bit,
6685 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6686 Similarly for (A & C) == 0. */
6688 /* If INNER is a right shift of a constant and it plus BITNUM does
6689 not overflow, adjust BITNUM and INNER. */
6690 if (TREE_CODE (inner
) == RSHIFT_EXPR
6691 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6692 && bitnum
< TYPE_PRECISION (type
)
6693 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6694 TYPE_PRECISION (type
) - bitnum
))
6696 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6697 inner
= TREE_OPERAND (inner
, 0);
6700 /* If we are going to be able to omit the AND below, we must do our
6701 operations as unsigned. If we must use the AND, we have a choice.
6702 Normally unsigned is faster, but for some machines signed is. */
6703 ops_unsigned
= (load_extend_op (operand_mode
) == SIGN_EXTEND
6704 && !flag_syntax_only
) ? 0 : 1;
6706 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6707 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6708 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6709 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6712 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6713 inner
, size_int (bitnum
));
6715 one
= build_int_cst (intermediate_type
, 1);
6717 if (code
== EQ_EXPR
)
6718 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6720 /* Put the AND last so it can combine with more things. */
6721 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6723 /* Make sure to return the proper type. */
6724 inner
= fold_convert_loc (loc
, result_type
, inner
);
6731 /* Test whether it is preferable two swap two operands, ARG0 and
6732 ARG1, for example because ARG0 is an integer constant and ARG1
6736 tree_swap_operands_p (const_tree arg0
, const_tree arg1
)
6738 if (CONSTANT_CLASS_P (arg1
))
6740 if (CONSTANT_CLASS_P (arg0
))
6746 if (TREE_CONSTANT (arg1
))
6748 if (TREE_CONSTANT (arg0
))
6751 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6752 for commutative and comparison operators. Ensuring a canonical
6753 form allows the optimizers to find additional redundancies without
6754 having to explicitly check for both orderings. */
6755 if (TREE_CODE (arg0
) == SSA_NAME
6756 && TREE_CODE (arg1
) == SSA_NAME
6757 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6760 /* Put SSA_NAMEs last. */
6761 if (TREE_CODE (arg1
) == SSA_NAME
)
6763 if (TREE_CODE (arg0
) == SSA_NAME
)
6766 /* Put variables last. */
6776 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6777 means A >= Y && A != MAX, but in this case we know that
6778 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6781 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6783 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6785 if (TREE_CODE (bound
) == LT_EXPR
)
6786 a
= TREE_OPERAND (bound
, 0);
6787 else if (TREE_CODE (bound
) == GT_EXPR
)
6788 a
= TREE_OPERAND (bound
, 1);
6792 typea
= TREE_TYPE (a
);
6793 if (!INTEGRAL_TYPE_P (typea
)
6794 && !POINTER_TYPE_P (typea
))
6797 if (TREE_CODE (ineq
) == LT_EXPR
)
6799 a1
= TREE_OPERAND (ineq
, 1);
6800 y
= TREE_OPERAND (ineq
, 0);
6802 else if (TREE_CODE (ineq
) == GT_EXPR
)
6804 a1
= TREE_OPERAND (ineq
, 0);
6805 y
= TREE_OPERAND (ineq
, 1);
6810 if (TREE_TYPE (a1
) != typea
)
6813 if (POINTER_TYPE_P (typea
))
6815 /* Convert the pointer types into integer before taking the difference. */
6816 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6817 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6818 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6821 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6823 if (!diff
|| !integer_onep (diff
))
6826 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6829 /* Fold a sum or difference of at least one multiplication.
6830 Returns the folded tree or NULL if no simplification could be made. */
6833 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6834 tree arg0
, tree arg1
)
6836 tree arg00
, arg01
, arg10
, arg11
;
6837 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6839 /* (A * C) +- (B * C) -> (A+-B) * C.
6840 (A * C) +- A -> A * (C+-1).
6841 We are most concerned about the case where C is a constant,
6842 but other combinations show up during loop reduction. Since
6843 it is not difficult, try all four possibilities. */
6845 if (TREE_CODE (arg0
) == MULT_EXPR
)
6847 arg00
= TREE_OPERAND (arg0
, 0);
6848 arg01
= TREE_OPERAND (arg0
, 1);
6850 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6852 arg00
= build_one_cst (type
);
6857 /* We cannot generate constant 1 for fract. */
6858 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6861 arg01
= build_one_cst (type
);
6863 if (TREE_CODE (arg1
) == MULT_EXPR
)
6865 arg10
= TREE_OPERAND (arg1
, 0);
6866 arg11
= TREE_OPERAND (arg1
, 1);
6868 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6870 arg10
= build_one_cst (type
);
6871 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6872 the purpose of this canonicalization. */
6873 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
6874 && negate_expr_p (arg1
)
6875 && code
== PLUS_EXPR
)
6877 arg11
= negate_expr (arg1
);
6885 /* We cannot generate constant 1 for fract. */
6886 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6889 arg11
= build_one_cst (type
);
6893 if (operand_equal_p (arg01
, arg11
, 0))
6894 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
6895 else if (operand_equal_p (arg00
, arg10
, 0))
6896 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
6897 else if (operand_equal_p (arg00
, arg11
, 0))
6898 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
6899 else if (operand_equal_p (arg01
, arg10
, 0))
6900 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
6902 /* No identical multiplicands; see if we can find a common
6903 power-of-two factor in non-power-of-two multiplies. This
6904 can help in multi-dimensional array access. */
6905 else if (tree_fits_shwi_p (arg01
)
6906 && tree_fits_shwi_p (arg11
))
6908 HOST_WIDE_INT int01
, int11
, tmp
;
6911 int01
= tree_to_shwi (arg01
);
6912 int11
= tree_to_shwi (arg11
);
6914 /* Move min of absolute values to int11. */
6915 if (absu_hwi (int01
) < absu_hwi (int11
))
6917 tmp
= int01
, int01
= int11
, int11
= tmp
;
6918 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
6925 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
6926 /* The remainder should not be a constant, otherwise we
6927 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6928 increased the number of multiplications necessary. */
6929 && TREE_CODE (arg10
) != INTEGER_CST
)
6931 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
6932 build_int_cst (TREE_TYPE (arg00
),
6937 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
6942 return fold_build2_loc (loc
, MULT_EXPR
, type
,
6943 fold_build2_loc (loc
, code
, type
,
6944 fold_convert_loc (loc
, type
, alt0
),
6945 fold_convert_loc (loc
, type
, alt1
)),
6946 fold_convert_loc (loc
, type
, same
));
6951 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6952 specified by EXPR into the buffer PTR of length LEN bytes.
6953 Return the number of bytes placed in the buffer, or zero
6957 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
6959 tree type
= TREE_TYPE (expr
);
6960 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
6961 int byte
, offset
, word
, words
;
6962 unsigned char value
;
6964 if ((off
== -1 && total_bytes
> len
)
6965 || off
>= total_bytes
)
6969 words
= total_bytes
/ UNITS_PER_WORD
;
6971 for (byte
= 0; byte
< total_bytes
; byte
++)
6973 int bitpos
= byte
* BITS_PER_UNIT
;
6974 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6976 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
6978 if (total_bytes
> UNITS_PER_WORD
)
6980 word
= byte
/ UNITS_PER_WORD
;
6981 if (WORDS_BIG_ENDIAN
)
6982 word
= (words
- 1) - word
;
6983 offset
= word
* UNITS_PER_WORD
;
6984 if (BYTES_BIG_ENDIAN
)
6985 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
6987 offset
+= byte
% UNITS_PER_WORD
;
6990 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
6992 && offset
- off
< len
)
6993 ptr
[offset
- off
] = value
;
6995 return MIN (len
, total_bytes
- off
);
6999 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7000 specified by EXPR into the buffer PTR of length LEN bytes.
7001 Return the number of bytes placed in the buffer, or zero
7005 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7007 tree type
= TREE_TYPE (expr
);
7008 machine_mode mode
= TYPE_MODE (type
);
7009 int total_bytes
= GET_MODE_SIZE (mode
);
7010 FIXED_VALUE_TYPE value
;
7011 tree i_value
, i_type
;
7013 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7016 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7018 if (NULL_TREE
== i_type
7019 || TYPE_PRECISION (i_type
) != total_bytes
)
7022 value
= TREE_FIXED_CST (expr
);
7023 i_value
= double_int_to_tree (i_type
, value
.data
);
7025 return native_encode_int (i_value
, ptr
, len
, off
);
7029 /* Subroutine of native_encode_expr. Encode the REAL_CST
7030 specified by EXPR into the buffer PTR of length LEN bytes.
7031 Return the number of bytes placed in the buffer, or zero
7035 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7037 tree type
= TREE_TYPE (expr
);
7038 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7039 int byte
, offset
, word
, words
, bitpos
;
7040 unsigned char value
;
7042 /* There are always 32 bits in each long, no matter the size of
7043 the hosts long. We handle floating point representations with
7047 if ((off
== -1 && total_bytes
> len
)
7048 || off
>= total_bytes
)
7052 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7054 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7056 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7057 bitpos
+= BITS_PER_UNIT
)
7059 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7060 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7062 if (UNITS_PER_WORD
< 4)
7064 word
= byte
/ UNITS_PER_WORD
;
7065 if (WORDS_BIG_ENDIAN
)
7066 word
= (words
- 1) - word
;
7067 offset
= word
* UNITS_PER_WORD
;
7068 if (BYTES_BIG_ENDIAN
)
7069 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7071 offset
+= byte
% UNITS_PER_WORD
;
7076 if (BYTES_BIG_ENDIAN
)
7078 /* Reverse bytes within each long, or within the entire float
7079 if it's smaller than a long (for HFmode). */
7080 offset
= MIN (3, total_bytes
- 1) - offset
;
7081 gcc_assert (offset
>= 0);
7084 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7086 && offset
- off
< len
)
7087 ptr
[offset
- off
] = value
;
7089 return MIN (len
, total_bytes
- off
);
7092 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7093 specified by EXPR into the buffer PTR of length LEN bytes.
7094 Return the number of bytes placed in the buffer, or zero
7098 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7103 part
= TREE_REALPART (expr
);
7104 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7108 part
= TREE_IMAGPART (expr
);
7110 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7111 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7115 return rsize
+ isize
;
7119 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7120 specified by EXPR into the buffer PTR of length LEN bytes.
7121 Return the number of bytes placed in the buffer, or zero
7125 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7132 count
= VECTOR_CST_NELTS (expr
);
7133 itype
= TREE_TYPE (TREE_TYPE (expr
));
7134 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7135 for (i
= 0; i
< count
; i
++)
7142 elem
= VECTOR_CST_ELT (expr
, i
);
7143 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7144 if ((off
== -1 && res
!= size
)
7157 /* Subroutine of native_encode_expr. Encode the STRING_CST
7158 specified by EXPR into the buffer PTR of length LEN bytes.
7159 Return the number of bytes placed in the buffer, or zero
7163 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7165 tree type
= TREE_TYPE (expr
);
7166 HOST_WIDE_INT total_bytes
;
7168 if (TREE_CODE (type
) != ARRAY_TYPE
7169 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7170 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7171 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7173 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7174 if ((off
== -1 && total_bytes
> len
)
7175 || off
>= total_bytes
)
7179 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7182 if (off
< TREE_STRING_LENGTH (expr
))
7184 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7185 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7187 memset (ptr
+ written
, 0,
7188 MIN (total_bytes
- written
, len
- written
));
7191 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7192 return MIN (total_bytes
- off
, len
);
7196 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7197 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7198 buffer PTR of length LEN bytes. If OFF is not -1 then start
7199 the encoding at byte offset OFF and encode at most LEN bytes.
7200 Return the number of bytes placed in the buffer, or zero upon failure. */
7203 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7205 /* We don't support starting at negative offset and -1 is special. */
7209 switch (TREE_CODE (expr
))
7212 return native_encode_int (expr
, ptr
, len
, off
);
7215 return native_encode_real (expr
, ptr
, len
, off
);
7218 return native_encode_fixed (expr
, ptr
, len
, off
);
7221 return native_encode_complex (expr
, ptr
, len
, off
);
7224 return native_encode_vector (expr
, ptr
, len
, off
);
7227 return native_encode_string (expr
, ptr
, len
, off
);
7235 /* Subroutine of native_interpret_expr. Interpret the contents of
7236 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7237 If the buffer cannot be interpreted, return NULL_TREE. */
7240 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7242 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7244 if (total_bytes
> len
7245 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7248 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7250 return wide_int_to_tree (type
, result
);
7254 /* Subroutine of native_interpret_expr. Interpret the contents of
7255 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7256 If the buffer cannot be interpreted, return NULL_TREE. */
7259 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7261 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7263 FIXED_VALUE_TYPE fixed_value
;
7265 if (total_bytes
> len
7266 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7269 result
= double_int::from_buffer (ptr
, total_bytes
);
7270 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7272 return build_fixed (type
, fixed_value
);
7276 /* Subroutine of native_interpret_expr. Interpret the contents of
7277 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7278 If the buffer cannot be interpreted, return NULL_TREE. */
7281 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7283 machine_mode mode
= TYPE_MODE (type
);
7284 int total_bytes
= GET_MODE_SIZE (mode
);
7285 unsigned char value
;
7286 /* There are always 32 bits in each long, no matter the size of
7287 the hosts long. We handle floating point representations with
7292 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7293 if (total_bytes
> len
|| total_bytes
> 24)
7295 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7297 memset (tmp
, 0, sizeof (tmp
));
7298 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7299 bitpos
+= BITS_PER_UNIT
)
7301 /* Both OFFSET and BYTE index within a long;
7302 bitpos indexes the whole float. */
7303 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7304 if (UNITS_PER_WORD
< 4)
7306 int word
= byte
/ UNITS_PER_WORD
;
7307 if (WORDS_BIG_ENDIAN
)
7308 word
= (words
- 1) - word
;
7309 offset
= word
* UNITS_PER_WORD
;
7310 if (BYTES_BIG_ENDIAN
)
7311 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7313 offset
+= byte
% UNITS_PER_WORD
;
7318 if (BYTES_BIG_ENDIAN
)
7320 /* Reverse bytes within each long, or within the entire float
7321 if it's smaller than a long (for HFmode). */
7322 offset
= MIN (3, total_bytes
- 1) - offset
;
7323 gcc_assert (offset
>= 0);
7326 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7328 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7331 real_from_target (&r
, tmp
, mode
);
7332 return build_real (type
, r
);
7336 /* Subroutine of native_interpret_expr. Interpret the contents of
7337 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7338 If the buffer cannot be interpreted, return NULL_TREE. */
7341 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7343 tree etype
, rpart
, ipart
;
7346 etype
= TREE_TYPE (type
);
7347 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7350 rpart
= native_interpret_expr (etype
, ptr
, size
);
7353 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7356 return build_complex (type
, rpart
, ipart
);
7360 /* Subroutine of native_interpret_expr. Interpret the contents of
7361 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7362 If the buffer cannot be interpreted, return NULL_TREE. */
7365 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7371 etype
= TREE_TYPE (type
);
7372 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7373 count
= TYPE_VECTOR_SUBPARTS (type
);
7374 if (size
* count
> len
)
7377 elements
= XALLOCAVEC (tree
, count
);
7378 for (i
= count
- 1; i
>= 0; i
--)
7380 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7385 return build_vector (type
, elements
);
7389 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7390 the buffer PTR of length LEN as a constant of type TYPE. For
7391 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7392 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7393 return NULL_TREE. */
7396 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7398 switch (TREE_CODE (type
))
7404 case REFERENCE_TYPE
:
7405 return native_interpret_int (type
, ptr
, len
);
7408 return native_interpret_real (type
, ptr
, len
);
7410 case FIXED_POINT_TYPE
:
7411 return native_interpret_fixed (type
, ptr
, len
);
7414 return native_interpret_complex (type
, ptr
, len
);
7417 return native_interpret_vector (type
, ptr
, len
);
7424 /* Returns true if we can interpret the contents of a native encoding
7428 can_native_interpret_type_p (tree type
)
7430 switch (TREE_CODE (type
))
7436 case REFERENCE_TYPE
:
7437 case FIXED_POINT_TYPE
:
7447 /* Return true iff a constant of type TYPE is accepted by
7448 native_encode_expr. */
7451 can_native_encode_type_p (tree type
)
7453 switch (TREE_CODE (type
))
7457 case FIXED_POINT_TYPE
:
7467 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7468 TYPE at compile-time. If we're unable to perform the conversion
7469 return NULL_TREE. */
7472 fold_view_convert_expr (tree type
, tree expr
)
7474 /* We support up to 512-bit values (for V8DFmode). */
7475 unsigned char buffer
[64];
7478 /* Check that the host and target are sane. */
7479 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7482 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7486 return native_interpret_expr (type
, buffer
, len
);
7489 /* Build an expression for the address of T. Folds away INDIRECT_REF
7490 to avoid confusing the gimplify process. */
7493 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7495 /* The size of the object is not relevant when talking about its address. */
7496 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7497 t
= TREE_OPERAND (t
, 0);
7499 if (TREE_CODE (t
) == INDIRECT_REF
)
7501 t
= TREE_OPERAND (t
, 0);
7503 if (TREE_TYPE (t
) != ptrtype
)
7504 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7506 else if (TREE_CODE (t
) == MEM_REF
7507 && integer_zerop (TREE_OPERAND (t
, 1)))
7508 return TREE_OPERAND (t
, 0);
7509 else if (TREE_CODE (t
) == MEM_REF
7510 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7511 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7512 TREE_OPERAND (t
, 0),
7513 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7514 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7516 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7518 if (TREE_TYPE (t
) != ptrtype
)
7519 t
= fold_convert_loc (loc
, ptrtype
, t
);
7522 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7527 /* Build an expression for the address of T. */
7530 build_fold_addr_expr_loc (location_t loc
, tree t
)
7532 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7534 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7537 /* Fold a unary expression of code CODE and type TYPE with operand
7538 OP0. Return the folded expression if folding is successful.
7539 Otherwise, return NULL_TREE. */
7542 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7546 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7548 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7549 && TREE_CODE_LENGTH (code
) == 1);
7554 if (CONVERT_EXPR_CODE_P (code
)
7555 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7557 /* Don't use STRIP_NOPS, because signedness of argument type
7559 STRIP_SIGN_NOPS (arg0
);
7563 /* Strip any conversions that don't change the mode. This
7564 is safe for every expression, except for a comparison
7565 expression because its signedness is derived from its
7568 Note that this is done as an internal manipulation within
7569 the constant folder, in order to find the simplest
7570 representation of the arguments so that their form can be
7571 studied. In any cases, the appropriate type conversions
7572 should be put back in the tree that will get out of the
7577 if (CONSTANT_CLASS_P (arg0
))
7579 tree tem
= const_unop (code
, type
, arg0
);
7582 if (TREE_TYPE (tem
) != type
)
7583 tem
= fold_convert_loc (loc
, type
, tem
);
7589 tem
= generic_simplify (loc
, code
, type
, op0
);
7593 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7595 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7596 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7597 fold_build1_loc (loc
, code
, type
,
7598 fold_convert_loc (loc
, TREE_TYPE (op0
),
7599 TREE_OPERAND (arg0
, 1))));
7600 else if (TREE_CODE (arg0
) == COND_EXPR
)
7602 tree arg01
= TREE_OPERAND (arg0
, 1);
7603 tree arg02
= TREE_OPERAND (arg0
, 2);
7604 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7605 arg01
= fold_build1_loc (loc
, code
, type
,
7606 fold_convert_loc (loc
,
7607 TREE_TYPE (op0
), arg01
));
7608 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7609 arg02
= fold_build1_loc (loc
, code
, type
,
7610 fold_convert_loc (loc
,
7611 TREE_TYPE (op0
), arg02
));
7612 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7615 /* If this was a conversion, and all we did was to move into
7616 inside the COND_EXPR, bring it back out. But leave it if
7617 it is a conversion from integer to integer and the
7618 result precision is no wider than a word since such a
7619 conversion is cheap and may be optimized away by combine,
7620 while it couldn't if it were outside the COND_EXPR. Then return
7621 so we don't get into an infinite recursion loop taking the
7622 conversion out and then back in. */
7624 if ((CONVERT_EXPR_CODE_P (code
)
7625 || code
== NON_LVALUE_EXPR
)
7626 && TREE_CODE (tem
) == COND_EXPR
7627 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7628 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7629 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7630 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7631 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7632 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7633 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7635 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7636 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7637 || flag_syntax_only
))
7638 tem
= build1_loc (loc
, code
, type
,
7640 TREE_TYPE (TREE_OPERAND
7641 (TREE_OPERAND (tem
, 1), 0)),
7642 TREE_OPERAND (tem
, 0),
7643 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7644 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7652 case NON_LVALUE_EXPR
:
7653 if (!maybe_lvalue_p (op0
))
7654 return fold_convert_loc (loc
, type
, op0
);
7659 case FIX_TRUNC_EXPR
:
7660 if (COMPARISON_CLASS_P (op0
))
7662 /* If we have (type) (a CMP b) and type is an integral type, return
7663 new expression involving the new type. Canonicalize
7664 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7666 Do not fold the result as that would not simplify further, also
7667 folding again results in recursions. */
7668 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7669 return build2_loc (loc
, TREE_CODE (op0
), type
,
7670 TREE_OPERAND (op0
, 0),
7671 TREE_OPERAND (op0
, 1));
7672 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7673 && TREE_CODE (type
) != VECTOR_TYPE
)
7674 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7675 constant_boolean_node (true, type
),
7676 constant_boolean_node (false, type
));
7679 /* Handle (T *)&A.B.C for A being of type T and B and C
7680 living at offset zero. This occurs frequently in
7681 C++ upcasting and then accessing the base. */
7682 if (TREE_CODE (op0
) == ADDR_EXPR
7683 && POINTER_TYPE_P (type
)
7684 && handled_component_p (TREE_OPERAND (op0
, 0)))
7686 HOST_WIDE_INT bitsize
, bitpos
;
7689 int unsignedp
, reversep
, volatilep
;
7691 = get_inner_reference (TREE_OPERAND (op0
, 0), &bitsize
, &bitpos
,
7692 &offset
, &mode
, &unsignedp
, &reversep
,
7694 /* If the reference was to a (constant) zero offset, we can use
7695 the address of the base if it has the same base type
7696 as the result type and the pointer type is unqualified. */
7697 if (! offset
&& bitpos
== 0
7698 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7699 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7700 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7701 return fold_convert_loc (loc
, type
,
7702 build_fold_addr_expr_loc (loc
, base
));
7705 if (TREE_CODE (op0
) == MODIFY_EXPR
7706 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7707 /* Detect assigning a bitfield. */
7708 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7710 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7712 /* Don't leave an assignment inside a conversion
7713 unless assigning a bitfield. */
7714 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7715 /* First do the assignment, then return converted constant. */
7716 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7717 TREE_NO_WARNING (tem
) = 1;
7718 TREE_USED (tem
) = 1;
7722 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7723 constants (if x has signed type, the sign bit cannot be set
7724 in c). This folds extension into the BIT_AND_EXPR.
7725 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7726 very likely don't have maximal range for their precision and this
7727 transformation effectively doesn't preserve non-maximal ranges. */
7728 if (TREE_CODE (type
) == INTEGER_TYPE
7729 && TREE_CODE (op0
) == BIT_AND_EXPR
7730 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7732 tree and_expr
= op0
;
7733 tree and0
= TREE_OPERAND (and_expr
, 0);
7734 tree and1
= TREE_OPERAND (and_expr
, 1);
7737 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7738 || (TYPE_PRECISION (type
)
7739 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7741 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7742 <= HOST_BITS_PER_WIDE_INT
7743 && tree_fits_uhwi_p (and1
))
7745 unsigned HOST_WIDE_INT cst
;
7747 cst
= tree_to_uhwi (and1
);
7748 cst
&= HOST_WIDE_INT_M1U
7749 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7750 change
= (cst
== 0);
7752 && !flag_syntax_only
7753 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0
)))
7756 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7757 and0
= fold_convert_loc (loc
, uns
, and0
);
7758 and1
= fold_convert_loc (loc
, uns
, and1
);
7763 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7764 TREE_OVERFLOW (and1
));
7765 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7766 fold_convert_loc (loc
, type
, and0
), tem
);
7770 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7771 cast (T1)X will fold away. We assume that this happens when X itself
7773 if (POINTER_TYPE_P (type
)
7774 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7775 && CONVERT_EXPR_P (TREE_OPERAND (arg0
, 0)))
7777 tree arg00
= TREE_OPERAND (arg0
, 0);
7778 tree arg01
= TREE_OPERAND (arg0
, 1);
7780 return fold_build_pointer_plus_loc
7781 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7784 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7785 of the same precision, and X is an integer type not narrower than
7786 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7787 if (INTEGRAL_TYPE_P (type
)
7788 && TREE_CODE (op0
) == BIT_NOT_EXPR
7789 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7790 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7791 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7793 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7794 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7795 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7796 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7797 fold_convert_loc (loc
, type
, tem
));
7800 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7801 type of X and Y (integer types only). */
7802 if (INTEGRAL_TYPE_P (type
)
7803 && TREE_CODE (op0
) == MULT_EXPR
7804 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7805 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7807 /* Be careful not to introduce new overflows. */
7809 if (TYPE_OVERFLOW_WRAPS (type
))
7812 mult_type
= unsigned_type_for (type
);
7814 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7816 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7817 fold_convert_loc (loc
, mult_type
,
7818 TREE_OPERAND (op0
, 0)),
7819 fold_convert_loc (loc
, mult_type
,
7820 TREE_OPERAND (op0
, 1)));
7821 return fold_convert_loc (loc
, type
, tem
);
7827 case VIEW_CONVERT_EXPR
:
7828 if (TREE_CODE (op0
) == MEM_REF
)
7830 if (TYPE_ALIGN (TREE_TYPE (op0
)) != TYPE_ALIGN (type
))
7831 type
= build_aligned_type (type
, TYPE_ALIGN (TREE_TYPE (op0
)));
7832 tem
= fold_build2_loc (loc
, MEM_REF
, type
,
7833 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7834 REF_REVERSE_STORAGE_ORDER (tem
) = REF_REVERSE_STORAGE_ORDER (op0
);
7841 tem
= fold_negate_expr (loc
, arg0
);
7843 return fold_convert_loc (loc
, type
, tem
);
7847 /* Convert fabs((double)float) into (double)fabsf(float). */
7848 if (TREE_CODE (arg0
) == NOP_EXPR
7849 && TREE_CODE (type
) == REAL_TYPE
)
7851 tree targ0
= strip_float_extensions (arg0
);
7853 return fold_convert_loc (loc
, type
,
7854 fold_build1_loc (loc
, ABS_EXPR
,
7861 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7862 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7863 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7864 fold_convert_loc (loc
, type
,
7865 TREE_OPERAND (arg0
, 0)))))
7866 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
7867 fold_convert_loc (loc
, type
,
7868 TREE_OPERAND (arg0
, 1)));
7869 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7870 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7871 fold_convert_loc (loc
, type
,
7872 TREE_OPERAND (arg0
, 1)))))
7873 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
7874 fold_convert_loc (loc
, type
,
7875 TREE_OPERAND (arg0
, 0)), tem
);
7879 case TRUTH_NOT_EXPR
:
7880 /* Note that the operand of this must be an int
7881 and its values must be 0 or 1.
7882 ("true" is a fixed value perhaps depending on the language,
7883 but we don't handle values other than 1 correctly yet.) */
7884 tem
= fold_truth_not_expr (loc
, arg0
);
7887 return fold_convert_loc (loc
, type
, tem
);
7890 /* Fold *&X to X if X is an lvalue. */
7891 if (TREE_CODE (op0
) == ADDR_EXPR
)
7893 tree op00
= TREE_OPERAND (op0
, 0);
7895 || TREE_CODE (op00
) == PARM_DECL
7896 || TREE_CODE (op00
) == RESULT_DECL
)
7897 && !TREE_READONLY (op00
))
7904 } /* switch (code) */
7908 /* If the operation was a conversion do _not_ mark a resulting constant
7909 with TREE_OVERFLOW if the original constant was not. These conversions
7910 have implementation defined behavior and retaining the TREE_OVERFLOW
7911 flag here would confuse later passes such as VRP. */
7913 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
7914 tree type
, tree op0
)
7916 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
7918 && TREE_CODE (res
) == INTEGER_CST
7919 && TREE_CODE (op0
) == INTEGER_CST
7920 && CONVERT_EXPR_CODE_P (code
))
7921 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
7926 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7927 operands OP0 and OP1. LOC is the location of the resulting expression.
7928 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7929 Return the folded expression if folding is successful. Otherwise,
7930 return NULL_TREE. */
7932 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
7933 tree arg0
, tree arg1
, tree op0
, tree op1
)
7937 /* We only do these simplifications if we are optimizing. */
7941 /* Check for things like (A || B) && (A || C). We can convert this
7942 to A || (B && C). Note that either operator can be any of the four
7943 truth and/or operations and the transformation will still be
7944 valid. Also note that we only care about order for the
7945 ANDIF and ORIF operators. If B contains side effects, this
7946 might change the truth-value of A. */
7947 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
7948 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
7949 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
7950 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
7951 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
7952 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
7954 tree a00
= TREE_OPERAND (arg0
, 0);
7955 tree a01
= TREE_OPERAND (arg0
, 1);
7956 tree a10
= TREE_OPERAND (arg1
, 0);
7957 tree a11
= TREE_OPERAND (arg1
, 1);
7958 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
7959 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
7960 && (code
== TRUTH_AND_EXPR
7961 || code
== TRUTH_OR_EXPR
));
7963 if (operand_equal_p (a00
, a10
, 0))
7964 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7965 fold_build2_loc (loc
, code
, type
, a01
, a11
));
7966 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
7967 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7968 fold_build2_loc (loc
, code
, type
, a01
, a10
));
7969 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
7970 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
7971 fold_build2_loc (loc
, code
, type
, a00
, a11
));
7973 /* This case if tricky because we must either have commutative
7974 operators or else A10 must not have side-effects. */
7976 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
7977 && operand_equal_p (a01
, a11
, 0))
7978 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
7979 fold_build2_loc (loc
, code
, type
, a00
, a10
),
7983 /* See if we can build a range comparison. */
7984 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
7987 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
7988 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
7990 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
7992 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
7995 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
7996 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
7998 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8000 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8003 /* Check for the possibility of merging component references. If our
8004 lhs is another similar operation, try to merge its rhs with our
8005 rhs. Then try to merge our lhs and rhs. */
8006 if (TREE_CODE (arg0
) == code
8007 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8008 TREE_OPERAND (arg0
, 1), arg1
)))
8009 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8011 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8014 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8015 && (code
== TRUTH_AND_EXPR
8016 || code
== TRUTH_ANDIF_EXPR
8017 || code
== TRUTH_OR_EXPR
8018 || code
== TRUTH_ORIF_EXPR
))
8020 enum tree_code ncode
, icode
;
8022 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8023 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8024 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8026 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8027 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8028 We don't want to pack more than two leafs to a non-IF AND/OR
8030 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8031 equal to IF-CODE, then we don't want to add right-hand operand.
8032 If the inner right-hand side of left-hand operand has
8033 side-effects, or isn't simple, then we can't add to it,
8034 as otherwise we might destroy if-sequence. */
8035 if (TREE_CODE (arg0
) == icode
8036 && simple_operand_p_2 (arg1
)
8037 /* Needed for sequence points to handle trappings, and
8039 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8041 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8043 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8046 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8047 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8048 else if (TREE_CODE (arg1
) == icode
8049 && simple_operand_p_2 (arg0
)
8050 /* Needed for sequence points to handle trappings, and
8052 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8054 tem
= fold_build2_loc (loc
, ncode
, type
,
8055 arg0
, TREE_OPERAND (arg1
, 0));
8056 return fold_build2_loc (loc
, icode
, type
, tem
,
8057 TREE_OPERAND (arg1
, 1));
8059 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8061 For sequence point consistancy, we need to check for trapping,
8062 and side-effects. */
8063 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8064 && simple_operand_p_2 (arg1
))
8065 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8071 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8072 by changing CODE to reduce the magnitude of constants involved in
8073 ARG0 of the comparison.
8074 Returns a canonicalized comparison tree if a simplification was
8075 possible, otherwise returns NULL_TREE.
8076 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8077 valid if signed overflow is undefined. */
8080 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8081 tree arg0
, tree arg1
,
8082 bool *strict_overflow_p
)
8084 enum tree_code code0
= TREE_CODE (arg0
);
8085 tree t
, cst0
= NULL_TREE
;
8088 /* Match A +- CST code arg1. We can change this only if overflow
8090 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8091 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8092 /* In principle pointers also have undefined overflow behavior,
8093 but that causes problems elsewhere. */
8094 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8095 && (code0
== MINUS_EXPR
8096 || code0
== PLUS_EXPR
)
8097 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8100 /* Identify the constant in arg0 and its sign. */
8101 cst0
= TREE_OPERAND (arg0
, 1);
8102 sgn0
= tree_int_cst_sgn (cst0
);
8104 /* Overflowed constants and zero will cause problems. */
8105 if (integer_zerop (cst0
)
8106 || TREE_OVERFLOW (cst0
))
8109 /* See if we can reduce the magnitude of the constant in
8110 arg0 by changing the comparison code. */
8111 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8113 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8115 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8116 else if (code
== GT_EXPR
8117 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8119 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8120 else if (code
== LE_EXPR
8121 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8123 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8124 else if (code
== GE_EXPR
8125 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8129 *strict_overflow_p
= true;
8131 /* Now build the constant reduced in magnitude. But not if that
8132 would produce one outside of its types range. */
8133 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8135 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8136 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8138 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8139 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8142 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8143 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8144 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8145 t
= fold_convert (TREE_TYPE (arg1
), t
);
8147 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8150 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8151 overflow further. Try to decrease the magnitude of constants involved
8152 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8153 and put sole constants at the second argument position.
8154 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8157 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8158 tree arg0
, tree arg1
)
8161 bool strict_overflow_p
;
8162 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8163 "when reducing constant in comparison");
8165 /* Try canonicalization by simplifying arg0. */
8166 strict_overflow_p
= false;
8167 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8168 &strict_overflow_p
);
8171 if (strict_overflow_p
)
8172 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8176 /* Try canonicalization by simplifying arg1 using the swapped
8178 code
= swap_tree_comparison (code
);
8179 strict_overflow_p
= false;
8180 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8181 &strict_overflow_p
);
8182 if (t
&& strict_overflow_p
)
8183 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8187 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8188 space. This is used to avoid issuing overflow warnings for
8189 expressions like &p->x which can not wrap. */
8192 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8194 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8201 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8202 if (offset
== NULL_TREE
)
8203 wi_offset
= wi::zero (precision
);
8204 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8210 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8211 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8215 if (!wi::fits_uhwi_p (total
))
8218 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8222 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8224 if (TREE_CODE (base
) == ADDR_EXPR
)
8226 HOST_WIDE_INT base_size
;
8228 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8229 if (base_size
> 0 && size
< base_size
)
8233 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8236 /* Return a positive integer when the symbol DECL is known to have
8237 a nonzero address, zero when it's known not to (e.g., it's a weak
8238 symbol), and a negative integer when the symbol is not yet in the
8239 symbol table and so whether or not its address is zero is unknown.
8240 For function local objects always return positive integer. */
8242 maybe_nonzero_address (tree decl
)
8244 if (DECL_P (decl
) && decl_in_symtab_p (decl
))
8245 if (struct symtab_node
*symbol
= symtab_node::get_create (decl
))
8246 return symbol
->nonzero_address ();
8248 /* Function local objects are never NULL. */
8250 && (DECL_CONTEXT (decl
)
8251 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
8252 && auto_var_in_fn_p (decl
, DECL_CONTEXT (decl
))))
8258 /* Subroutine of fold_binary. This routine performs all of the
8259 transformations that are common to the equality/inequality
8260 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8261 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8262 fold_binary should call fold_binary. Fold a comparison with
8263 tree code CODE and type TYPE with operands OP0 and OP1. Return
8264 the folded comparison or NULL_TREE. */
8267 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8270 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8271 tree arg0
, arg1
, tem
;
8276 STRIP_SIGN_NOPS (arg0
);
8277 STRIP_SIGN_NOPS (arg1
);
8279 /* For comparisons of pointers we can decompose it to a compile time
8280 comparison of the base objects and the offsets into the object.
8281 This requires at least one operand being an ADDR_EXPR or a
8282 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8283 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8284 && (TREE_CODE (arg0
) == ADDR_EXPR
8285 || TREE_CODE (arg1
) == ADDR_EXPR
8286 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8287 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8289 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8290 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8292 int volatilep
, reversep
, unsignedp
;
8293 bool indirect_base0
= false, indirect_base1
= false;
8295 /* Get base and offset for the access. Strip ADDR_EXPR for
8296 get_inner_reference, but put it back by stripping INDIRECT_REF
8297 off the base object if possible. indirect_baseN will be true
8298 if baseN is not an address but refers to the object itself. */
8300 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8303 = get_inner_reference (TREE_OPERAND (arg0
, 0),
8304 &bitsize
, &bitpos0
, &offset0
, &mode
,
8305 &unsignedp
, &reversep
, &volatilep
);
8306 if (TREE_CODE (base0
) == INDIRECT_REF
)
8307 base0
= TREE_OPERAND (base0
, 0);
8309 indirect_base0
= true;
8311 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8313 base0
= TREE_OPERAND (arg0
, 0);
8314 STRIP_SIGN_NOPS (base0
);
8315 if (TREE_CODE (base0
) == ADDR_EXPR
)
8318 = get_inner_reference (TREE_OPERAND (base0
, 0),
8319 &bitsize
, &bitpos0
, &offset0
, &mode
,
8320 &unsignedp
, &reversep
, &volatilep
);
8321 if (TREE_CODE (base0
) == INDIRECT_REF
)
8322 base0
= TREE_OPERAND (base0
, 0);
8324 indirect_base0
= true;
8326 if (offset0
== NULL_TREE
|| integer_zerop (offset0
))
8327 offset0
= TREE_OPERAND (arg0
, 1);
8329 offset0
= size_binop (PLUS_EXPR
, offset0
,
8330 TREE_OPERAND (arg0
, 1));
8331 if (TREE_CODE (offset0
) == INTEGER_CST
)
8333 offset_int tem
= wi::sext (wi::to_offset (offset0
),
8334 TYPE_PRECISION (sizetype
));
8335 tem
<<= LOG2_BITS_PER_UNIT
;
8337 if (wi::fits_shwi_p (tem
))
8339 bitpos0
= tem
.to_shwi ();
8340 offset0
= NULL_TREE
;
8346 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8349 = get_inner_reference (TREE_OPERAND (arg1
, 0),
8350 &bitsize
, &bitpos1
, &offset1
, &mode
,
8351 &unsignedp
, &reversep
, &volatilep
);
8352 if (TREE_CODE (base1
) == INDIRECT_REF
)
8353 base1
= TREE_OPERAND (base1
, 0);
8355 indirect_base1
= true;
8357 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8359 base1
= TREE_OPERAND (arg1
, 0);
8360 STRIP_SIGN_NOPS (base1
);
8361 if (TREE_CODE (base1
) == ADDR_EXPR
)
8364 = get_inner_reference (TREE_OPERAND (base1
, 0),
8365 &bitsize
, &bitpos1
, &offset1
, &mode
,
8366 &unsignedp
, &reversep
, &volatilep
);
8367 if (TREE_CODE (base1
) == INDIRECT_REF
)
8368 base1
= TREE_OPERAND (base1
, 0);
8370 indirect_base1
= true;
8372 if (offset1
== NULL_TREE
|| integer_zerop (offset1
))
8373 offset1
= TREE_OPERAND (arg1
, 1);
8375 offset1
= size_binop (PLUS_EXPR
, offset1
,
8376 TREE_OPERAND (arg1
, 1));
8377 if (TREE_CODE (offset1
) == INTEGER_CST
)
8379 offset_int tem
= wi::sext (wi::to_offset (offset1
),
8380 TYPE_PRECISION (sizetype
));
8381 tem
<<= LOG2_BITS_PER_UNIT
;
8383 if (wi::fits_shwi_p (tem
))
8385 bitpos1
= tem
.to_shwi ();
8386 offset1
= NULL_TREE
;
8391 /* If we have equivalent bases we might be able to simplify. */
8392 if (indirect_base0
== indirect_base1
8393 && operand_equal_p (base0
, base1
,
8394 indirect_base0
? OEP_ADDRESS_OF
: 0))
8396 /* We can fold this expression to a constant if the non-constant
8397 offset parts are equal. */
8398 if ((offset0
== offset1
8399 || (offset0
&& offset1
8400 && operand_equal_p (offset0
, offset1
, 0)))
8403 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8404 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8408 && bitpos0
!= bitpos1
8409 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8410 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8411 fold_overflow_warning (("assuming pointer wraparound does not "
8412 "occur when comparing P +- C1 with "
8414 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8419 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8421 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8423 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8425 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8427 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8429 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8433 /* We can simplify the comparison to a comparison of the variable
8434 offset parts if the constant offset parts are equal.
8435 Be careful to use signed sizetype here because otherwise we
8436 mess with array offsets in the wrong way. This is possible
8437 because pointer arithmetic is restricted to retain within an
8438 object and overflow on pointer differences is undefined as of
8439 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8440 else if (bitpos0
== bitpos1
8443 && (DECL_P (base0
) || CONSTANT_CLASS_P (base0
)))
8444 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8446 /* By converting to signed sizetype we cover middle-end pointer
8447 arithmetic which operates on unsigned pointer types of size
8448 type size and ARRAY_REF offsets which are properly sign or
8449 zero extended from their type in case it is narrower than
8451 if (offset0
== NULL_TREE
)
8452 offset0
= build_int_cst (ssizetype
, 0);
8454 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8455 if (offset1
== NULL_TREE
)
8456 offset1
= build_int_cst (ssizetype
, 0);
8458 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8461 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8462 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8463 fold_overflow_warning (("assuming pointer wraparound does not "
8464 "occur when comparing P +- C1 with "
8466 WARN_STRICT_OVERFLOW_COMPARISON
);
8468 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8471 /* For equal offsets we can simplify to a comparison of the
8473 else if (bitpos0
== bitpos1
8475 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8477 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8478 && ((offset0
== offset1
)
8479 || (offset0
&& offset1
8480 && operand_equal_p (offset0
, offset1
, 0))))
8483 base0
= build_fold_addr_expr_loc (loc
, base0
);
8485 base1
= build_fold_addr_expr_loc (loc
, base1
);
8486 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8488 /* Comparison between an ordinary (non-weak) symbol and a null
8489 pointer can be eliminated since such symbols must have a non
8490 null address. In C, relational expressions between pointers
8491 to objects and null pointers are undefined. The results
8492 below follow the C++ rules with the additional property that
8493 every object pointer compares greater than a null pointer.
8495 else if (((DECL_P (base0
)
8496 && maybe_nonzero_address (base0
) > 0
8497 /* Avoid folding references to struct members at offset 0 to
8498 prevent tests like '&ptr->firstmember == 0' from getting
8499 eliminated. When ptr is null, although the -> expression
8500 is strictly speaking invalid, GCC retains it as a matter
8501 of QoI. See PR c/44555. */
8502 && (offset0
== NULL_TREE
&& bitpos0
!= 0))
8503 || CONSTANT_CLASS_P (base0
))
8505 /* The caller guarantees that when one of the arguments is
8506 constant (i.e., null in this case) it is second. */
8507 && integer_zerop (arg1
))
8514 return constant_boolean_node (false, type
);
8518 return constant_boolean_node (true, type
);
8525 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8526 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8527 the resulting offset is smaller in absolute value than the
8528 original one and has the same sign. */
8529 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8530 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8531 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8532 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8533 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8534 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8535 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8536 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8538 tree const1
= TREE_OPERAND (arg0
, 1);
8539 tree const2
= TREE_OPERAND (arg1
, 1);
8540 tree variable1
= TREE_OPERAND (arg0
, 0);
8541 tree variable2
= TREE_OPERAND (arg1
, 0);
8543 const char * const warnmsg
= G_("assuming signed overflow does not "
8544 "occur when combining constants around "
8547 /* Put the constant on the side where it doesn't overflow and is
8548 of lower absolute value and of same sign than before. */
8549 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8550 ? MINUS_EXPR
: PLUS_EXPR
,
8552 if (!TREE_OVERFLOW (cst
)
8553 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8554 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8556 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8557 return fold_build2_loc (loc
, code
, type
,
8559 fold_build2_loc (loc
, TREE_CODE (arg1
),
8564 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8565 ? MINUS_EXPR
: PLUS_EXPR
,
8567 if (!TREE_OVERFLOW (cst
)
8568 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8569 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8571 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8572 return fold_build2_loc (loc
, code
, type
,
8573 fold_build2_loc (loc
, TREE_CODE (arg0
),
8580 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8584 /* If we are comparing an expression that just has comparisons
8585 of two integer values, arithmetic expressions of those comparisons,
8586 and constants, we can simplify it. There are only three cases
8587 to check: the two values can either be equal, the first can be
8588 greater, or the second can be greater. Fold the expression for
8589 those three values. Since each value must be 0 or 1, we have
8590 eight possibilities, each of which corresponds to the constant 0
8591 or 1 or one of the six possible comparisons.
8593 This handles common cases like (a > b) == 0 but also handles
8594 expressions like ((x > y) - (y > x)) > 0, which supposedly
8595 occur in macroized code. */
8597 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8599 tree cval1
= 0, cval2
= 0;
8602 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8603 /* Don't handle degenerate cases here; they should already
8604 have been handled anyway. */
8605 && cval1
!= 0 && cval2
!= 0
8606 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8607 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8608 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8609 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8610 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8611 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8612 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8614 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8615 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8617 /* We can't just pass T to eval_subst in case cval1 or cval2
8618 was the same as ARG1. */
8621 = fold_build2_loc (loc
, code
, type
,
8622 eval_subst (loc
, arg0
, cval1
, maxval
,
8626 = fold_build2_loc (loc
, code
, type
,
8627 eval_subst (loc
, arg0
, cval1
, maxval
,
8631 = fold_build2_loc (loc
, code
, type
,
8632 eval_subst (loc
, arg0
, cval1
, minval
,
8636 /* All three of these results should be 0 or 1. Confirm they are.
8637 Then use those values to select the proper code to use. */
8639 if (TREE_CODE (high_result
) == INTEGER_CST
8640 && TREE_CODE (equal_result
) == INTEGER_CST
8641 && TREE_CODE (low_result
) == INTEGER_CST
)
8643 /* Make a 3-bit mask with the high-order bit being the
8644 value for `>', the next for '=', and the low for '<'. */
8645 switch ((integer_onep (high_result
) * 4)
8646 + (integer_onep (equal_result
) * 2)
8647 + integer_onep (low_result
))
8651 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8672 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8677 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8678 SET_EXPR_LOCATION (tem
, loc
);
8681 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8686 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8687 into a single range test. */
8688 if (TREE_CODE (arg0
) == TRUNC_DIV_EXPR
8689 && TREE_CODE (arg1
) == INTEGER_CST
8690 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8691 && !integer_zerop (TREE_OPERAND (arg0
, 1))
8692 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8693 && !TREE_OVERFLOW (arg1
))
8695 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
8696 if (tem
!= NULL_TREE
)
8704 /* Subroutine of fold_binary. Optimize complex multiplications of the
8705 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8706 argument EXPR represents the expression "z" of type TYPE. */
8709 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8711 tree itype
= TREE_TYPE (type
);
8712 tree rpart
, ipart
, tem
;
8714 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8716 rpart
= TREE_OPERAND (expr
, 0);
8717 ipart
= TREE_OPERAND (expr
, 1);
8719 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8721 rpart
= TREE_REALPART (expr
);
8722 ipart
= TREE_IMAGPART (expr
);
8726 expr
= save_expr (expr
);
8727 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8728 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8731 rpart
= save_expr (rpart
);
8732 ipart
= save_expr (ipart
);
8733 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8734 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8735 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8736 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8737 build_zero_cst (itype
));
8741 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8742 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8745 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
8747 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
8749 if (TREE_CODE (arg
) == VECTOR_CST
)
8751 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8752 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8754 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8756 constructor_elt
*elt
;
8758 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8759 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8762 elts
[i
] = elt
->value
;
8766 for (; i
< nelts
; i
++)
8768 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8772 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8773 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8774 NULL_TREE otherwise. */
8777 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
8779 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8781 bool need_ctor
= false;
8783 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8784 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8785 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8786 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8789 elts
= XALLOCAVEC (tree
, nelts
* 3);
8790 if (!vec_cst_ctor_to_array (arg0
, elts
)
8791 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
8794 for (i
= 0; i
< nelts
; i
++)
8796 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
8798 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
8803 vec
<constructor_elt
, va_gc
> *v
;
8804 vec_alloc (v
, nelts
);
8805 for (i
= 0; i
< nelts
; i
++)
8806 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
8807 return build_constructor (type
, v
);
8810 return build_vector (type
, &elts
[2 * nelts
]);
8813 /* Try to fold a pointer difference of type TYPE two address expressions of
8814 array references AREF0 and AREF1 using location LOC. Return a
8815 simplified expression for the difference or NULL_TREE. */
8818 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8819 tree aref0
, tree aref1
)
8821 tree base0
= TREE_OPERAND (aref0
, 0);
8822 tree base1
= TREE_OPERAND (aref1
, 0);
8823 tree base_offset
= build_int_cst (type
, 0);
8825 /* If the bases are array references as well, recurse. If the bases
8826 are pointer indirections compute the difference of the pointers.
8827 If the bases are equal, we are set. */
8828 if ((TREE_CODE (base0
) == ARRAY_REF
8829 && TREE_CODE (base1
) == ARRAY_REF
8831 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8832 || (INDIRECT_REF_P (base0
)
8833 && INDIRECT_REF_P (base1
)
8835 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8836 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8838 TREE_OPERAND (base1
, 0)))))
8839 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8841 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8842 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8843 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8844 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
8845 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8847 fold_build2_loc (loc
, MULT_EXPR
, type
,
8853 /* If the real or vector real constant CST of type TYPE has an exact
8854 inverse, return it, else return NULL. */
8857 exact_inverse (tree type
, tree cst
)
8860 tree unit_type
, *elts
;
8862 unsigned vec_nelts
, i
;
8864 switch (TREE_CODE (cst
))
8867 r
= TREE_REAL_CST (cst
);
8869 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8870 return build_real (type
, r
);
8875 vec_nelts
= VECTOR_CST_NELTS (cst
);
8876 elts
= XALLOCAVEC (tree
, vec_nelts
);
8877 unit_type
= TREE_TYPE (type
);
8878 mode
= TYPE_MODE (unit_type
);
8880 for (i
= 0; i
< vec_nelts
; i
++)
8882 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
8883 if (!exact_real_inverse (mode
, &r
))
8885 elts
[i
] = build_real (unit_type
, r
);
8888 return build_vector (type
, elts
);
8895 /* Mask out the tz least significant bits of X of type TYPE where
8896 tz is the number of trailing zeroes in Y. */
8898 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
8900 int tz
= wi::ctz (y
);
8902 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
8906 /* Return true when T is an address and is known to be nonzero.
8907 For floating point we further ensure that T is not denormal.
8908 Similar logic is present in nonzero_address in rtlanal.h.
8910 If the return value is based on the assumption that signed overflow
8911 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8912 change *STRICT_OVERFLOW_P. */
8915 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
8917 tree type
= TREE_TYPE (t
);
8918 enum tree_code code
;
8920 /* Doing something useful for floating point would need more work. */
8921 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
8924 code
= TREE_CODE (t
);
8925 switch (TREE_CODE_CLASS (code
))
8928 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8931 case tcc_comparison
:
8932 return tree_binary_nonzero_warnv_p (code
, type
,
8933 TREE_OPERAND (t
, 0),
8934 TREE_OPERAND (t
, 1),
8937 case tcc_declaration
:
8939 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8947 case TRUTH_NOT_EXPR
:
8948 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8951 case TRUTH_AND_EXPR
:
8953 case TRUTH_XOR_EXPR
:
8954 return tree_binary_nonzero_warnv_p (code
, type
,
8955 TREE_OPERAND (t
, 0),
8956 TREE_OPERAND (t
, 1),
8964 case WITH_SIZE_EXPR
:
8966 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8971 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
8975 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
8980 tree fndecl
= get_callee_fndecl (t
);
8981 if (!fndecl
) return false;
8982 if (flag_delete_null_pointer_checks
&& !flag_check_new
8983 && DECL_IS_OPERATOR_NEW (fndecl
)
8984 && !TREE_NOTHROW (fndecl
))
8986 if (flag_delete_null_pointer_checks
8987 && lookup_attribute ("returns_nonnull",
8988 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
8990 return alloca_call_p (t
);
8999 /* Return true when T is an address and is known to be nonzero.
9000 Handle warnings about undefined signed overflow. */
9003 tree_expr_nonzero_p (tree t
)
9005 bool ret
, strict_overflow_p
;
9007 strict_overflow_p
= false;
9008 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9009 if (strict_overflow_p
)
9010 fold_overflow_warning (("assuming signed overflow does not occur when "
9011 "determining that expression is always "
9013 WARN_STRICT_OVERFLOW_MISC
);
9017 /* Return true if T is known not to be equal to an integer W. */
9020 expr_not_equal_to (tree t
, const wide_int
&w
)
9022 wide_int min
, max
, nz
;
9023 value_range_type rtype
;
9024 switch (TREE_CODE (t
))
9027 return wi::ne_p (t
, w
);
9030 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
9032 rtype
= get_range_info (t
, &min
, &max
);
9033 if (rtype
== VR_RANGE
)
9035 if (wi::lt_p (max
, w
, TYPE_SIGN (TREE_TYPE (t
))))
9037 if (wi::lt_p (w
, min
, TYPE_SIGN (TREE_TYPE (t
))))
9040 else if (rtype
== VR_ANTI_RANGE
9041 && wi::le_p (min
, w
, TYPE_SIGN (TREE_TYPE (t
)))
9042 && wi::le_p (w
, max
, TYPE_SIGN (TREE_TYPE (t
))))
9044 /* If T has some known zero bits and W has any of those bits set,
9045 then T is known not to be equal to W. */
9046 if (wi::ne_p (wi::zext (wi::bit_and_not (w
, get_nonzero_bits (t
)),
9047 TYPE_PRECISION (TREE_TYPE (t
))), 0))
9056 /* Fold a binary expression of code CODE and type TYPE with operands
9057 OP0 and OP1. LOC is the location of the resulting expression.
9058 Return the folded expression if folding is successful. Otherwise,
9059 return NULL_TREE. */
9062 fold_binary_loc (location_t loc
,
9063 enum tree_code code
, tree type
, tree op0
, tree op1
)
9065 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9066 tree arg0
, arg1
, tem
;
9067 tree t1
= NULL_TREE
;
9068 bool strict_overflow_p
;
9071 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9072 && TREE_CODE_LENGTH (code
) == 2
9074 && op1
!= NULL_TREE
);
9079 /* Strip any conversions that don't change the mode. This is
9080 safe for every expression, except for a comparison expression
9081 because its signedness is derived from its operands. So, in
9082 the latter case, only strip conversions that don't change the
9083 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9086 Note that this is done as an internal manipulation within the
9087 constant folder, in order to find the simplest representation
9088 of the arguments so that their form can be studied. In any
9089 cases, the appropriate type conversions should be put back in
9090 the tree that will get out of the constant folder. */
9092 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9094 STRIP_SIGN_NOPS (arg0
);
9095 STRIP_SIGN_NOPS (arg1
);
9103 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9104 constant but we can't do arithmetic on them. */
9105 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9107 tem
= const_binop (code
, type
, arg0
, arg1
);
9108 if (tem
!= NULL_TREE
)
9110 if (TREE_TYPE (tem
) != type
)
9111 tem
= fold_convert_loc (loc
, type
, tem
);
9116 /* If this is a commutative operation, and ARG0 is a constant, move it
9117 to ARG1 to reduce the number of tests below. */
9118 if (commutative_tree_code (code
)
9119 && tree_swap_operands_p (arg0
, arg1
))
9120 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9122 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9123 to ARG1 to reduce the number of tests below. */
9124 if (kind
== tcc_comparison
9125 && tree_swap_operands_p (arg0
, arg1
))
9126 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9128 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9132 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9134 First check for cases where an arithmetic operation is applied to a
9135 compound, conditional, or comparison operation. Push the arithmetic
9136 operation inside the compound or conditional to see if any folding
9137 can then be done. Convert comparison to conditional for this purpose.
9138 The also optimizes non-constant cases that used to be done in
9141 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9142 one of the operands is a comparison and the other is a comparison, a
9143 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9144 code below would make the expression more complex. Change it to a
9145 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9146 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9148 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9149 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9150 && TREE_CODE (type
) != VECTOR_TYPE
9151 && ((truth_value_p (TREE_CODE (arg0
))
9152 && (truth_value_p (TREE_CODE (arg1
))
9153 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9154 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9155 || (truth_value_p (TREE_CODE (arg1
))
9156 && (truth_value_p (TREE_CODE (arg0
))
9157 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9158 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9160 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9161 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9164 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9165 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9167 if (code
== EQ_EXPR
)
9168 tem
= invert_truthvalue_loc (loc
, tem
);
9170 return fold_convert_loc (loc
, type
, tem
);
9173 if (TREE_CODE_CLASS (code
) == tcc_binary
9174 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9176 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9178 tem
= fold_build2_loc (loc
, code
, type
,
9179 fold_convert_loc (loc
, TREE_TYPE (op0
),
9180 TREE_OPERAND (arg0
, 1)), op1
);
9181 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9184 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
9186 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9187 fold_convert_loc (loc
, TREE_TYPE (op1
),
9188 TREE_OPERAND (arg1
, 1)));
9189 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9193 if (TREE_CODE (arg0
) == COND_EXPR
9194 || TREE_CODE (arg0
) == VEC_COND_EXPR
9195 || COMPARISON_CLASS_P (arg0
))
9197 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9199 /*cond_first_p=*/1);
9200 if (tem
!= NULL_TREE
)
9204 if (TREE_CODE (arg1
) == COND_EXPR
9205 || TREE_CODE (arg1
) == VEC_COND_EXPR
9206 || COMPARISON_CLASS_P (arg1
))
9208 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9210 /*cond_first_p=*/0);
9211 if (tem
!= NULL_TREE
)
9219 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9220 if (TREE_CODE (arg0
) == ADDR_EXPR
9221 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9223 tree iref
= TREE_OPERAND (arg0
, 0);
9224 return fold_build2 (MEM_REF
, type
,
9225 TREE_OPERAND (iref
, 0),
9226 int_const_binop (PLUS_EXPR
, arg1
,
9227 TREE_OPERAND (iref
, 1)));
9230 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9231 if (TREE_CODE (arg0
) == ADDR_EXPR
9232 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9235 HOST_WIDE_INT coffset
;
9236 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9240 return fold_build2 (MEM_REF
, type
,
9241 build_fold_addr_expr (base
),
9242 int_const_binop (PLUS_EXPR
, arg1
,
9243 size_int (coffset
)));
9248 case POINTER_PLUS_EXPR
:
9249 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9250 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9251 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9252 return fold_convert_loc (loc
, type
,
9253 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9254 fold_convert_loc (loc
, sizetype
,
9256 fold_convert_loc (loc
, sizetype
,
9262 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9264 /* X + (X / CST) * -CST is X % CST. */
9265 if (TREE_CODE (arg1
) == MULT_EXPR
9266 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9267 && operand_equal_p (arg0
,
9268 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9270 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9271 tree cst1
= TREE_OPERAND (arg1
, 1);
9272 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9274 if (sum
&& integer_zerop (sum
))
9275 return fold_convert_loc (loc
, type
,
9276 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9277 TREE_TYPE (arg0
), arg0
,
9282 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9283 one. Make sure the type is not saturating and has the signedness of
9284 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9285 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9286 if ((TREE_CODE (arg0
) == MULT_EXPR
9287 || TREE_CODE (arg1
) == MULT_EXPR
)
9288 && !TYPE_SATURATING (type
)
9289 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9290 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9291 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9293 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9298 if (! FLOAT_TYPE_P (type
))
9300 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9301 (plus (plus (mult) (mult)) (foo)) so that we can
9302 take advantage of the factoring cases below. */
9303 if (ANY_INTEGRAL_TYPE_P (type
)
9304 && TYPE_OVERFLOW_WRAPS (type
)
9305 && (((TREE_CODE (arg0
) == PLUS_EXPR
9306 || TREE_CODE (arg0
) == MINUS_EXPR
)
9307 && TREE_CODE (arg1
) == MULT_EXPR
)
9308 || ((TREE_CODE (arg1
) == PLUS_EXPR
9309 || TREE_CODE (arg1
) == MINUS_EXPR
)
9310 && TREE_CODE (arg0
) == MULT_EXPR
)))
9312 tree parg0
, parg1
, parg
, marg
;
9313 enum tree_code pcode
;
9315 if (TREE_CODE (arg1
) == MULT_EXPR
)
9316 parg
= arg0
, marg
= arg1
;
9318 parg
= arg1
, marg
= arg0
;
9319 pcode
= TREE_CODE (parg
);
9320 parg0
= TREE_OPERAND (parg
, 0);
9321 parg1
= TREE_OPERAND (parg
, 1);
9325 if (TREE_CODE (parg0
) == MULT_EXPR
9326 && TREE_CODE (parg1
) != MULT_EXPR
)
9327 return fold_build2_loc (loc
, pcode
, type
,
9328 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9329 fold_convert_loc (loc
, type
,
9331 fold_convert_loc (loc
, type
,
9333 fold_convert_loc (loc
, type
, parg1
));
9334 if (TREE_CODE (parg0
) != MULT_EXPR
9335 && TREE_CODE (parg1
) == MULT_EXPR
)
9337 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9338 fold_convert_loc (loc
, type
, parg0
),
9339 fold_build2_loc (loc
, pcode
, type
,
9340 fold_convert_loc (loc
, type
, marg
),
9341 fold_convert_loc (loc
, type
,
9347 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9348 to __complex__ ( x, y ). This is not the same for SNaNs or
9349 if signed zeros are involved. */
9350 if (!HONOR_SNANS (element_mode (arg0
))
9351 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9352 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9354 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9355 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9356 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9357 bool arg0rz
= false, arg0iz
= false;
9358 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9359 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9361 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9362 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9363 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9365 tree rp
= arg1r
? arg1r
9366 : build1 (REALPART_EXPR
, rtype
, arg1
);
9367 tree ip
= arg0i
? arg0i
9368 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9369 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9371 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9373 tree rp
= arg0r
? arg0r
9374 : build1 (REALPART_EXPR
, rtype
, arg0
);
9375 tree ip
= arg1i
? arg1i
9376 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9377 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9382 if (flag_unsafe_math_optimizations
9383 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9384 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9385 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9388 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9389 We associate floats only if the user has specified
9390 -fassociative-math. */
9391 if (flag_associative_math
9392 && TREE_CODE (arg1
) == PLUS_EXPR
9393 && TREE_CODE (arg0
) != MULT_EXPR
)
9395 tree tree10
= TREE_OPERAND (arg1
, 0);
9396 tree tree11
= TREE_OPERAND (arg1
, 1);
9397 if (TREE_CODE (tree11
) == MULT_EXPR
9398 && TREE_CODE (tree10
) == MULT_EXPR
)
9401 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9402 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9405 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9406 We associate floats only if the user has specified
9407 -fassociative-math. */
9408 if (flag_associative_math
9409 && TREE_CODE (arg0
) == PLUS_EXPR
9410 && TREE_CODE (arg1
) != MULT_EXPR
)
9412 tree tree00
= TREE_OPERAND (arg0
, 0);
9413 tree tree01
= TREE_OPERAND (arg0
, 1);
9414 if (TREE_CODE (tree01
) == MULT_EXPR
9415 && TREE_CODE (tree00
) == MULT_EXPR
)
9418 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9419 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9425 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9426 is a rotate of A by C1 bits. */
9427 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9428 is a rotate of A by B bits. */
9430 enum tree_code code0
, code1
;
9432 code0
= TREE_CODE (arg0
);
9433 code1
= TREE_CODE (arg1
);
9434 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9435 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9436 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9437 TREE_OPERAND (arg1
, 0), 0)
9438 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9439 TYPE_UNSIGNED (rtype
))
9440 /* Only create rotates in complete modes. Other cases are not
9441 expanded properly. */
9442 && (element_precision (rtype
)
9443 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9445 tree tree01
, tree11
;
9446 enum tree_code code01
, code11
;
9448 tree01
= TREE_OPERAND (arg0
, 1);
9449 tree11
= TREE_OPERAND (arg1
, 1);
9450 STRIP_NOPS (tree01
);
9451 STRIP_NOPS (tree11
);
9452 code01
= TREE_CODE (tree01
);
9453 code11
= TREE_CODE (tree11
);
9454 if (code01
== INTEGER_CST
9455 && code11
== INTEGER_CST
9456 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9457 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9459 tem
= build2_loc (loc
, LROTATE_EXPR
,
9460 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9461 TREE_OPERAND (arg0
, 0),
9462 code0
== LSHIFT_EXPR
9463 ? TREE_OPERAND (arg0
, 1)
9464 : TREE_OPERAND (arg1
, 1));
9465 return fold_convert_loc (loc
, type
, tem
);
9467 else if (code11
== MINUS_EXPR
)
9469 tree tree110
, tree111
;
9470 tree110
= TREE_OPERAND (tree11
, 0);
9471 tree111
= TREE_OPERAND (tree11
, 1);
9472 STRIP_NOPS (tree110
);
9473 STRIP_NOPS (tree111
);
9474 if (TREE_CODE (tree110
) == INTEGER_CST
9475 && 0 == compare_tree_int (tree110
,
9477 (TREE_TYPE (TREE_OPERAND
9479 && operand_equal_p (tree01
, tree111
, 0))
9481 fold_convert_loc (loc
, type
,
9482 build2 ((code0
== LSHIFT_EXPR
9485 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9486 TREE_OPERAND (arg0
, 0),
9487 TREE_OPERAND (arg0
, 1)));
9489 else if (code01
== MINUS_EXPR
)
9491 tree tree010
, tree011
;
9492 tree010
= TREE_OPERAND (tree01
, 0);
9493 tree011
= TREE_OPERAND (tree01
, 1);
9494 STRIP_NOPS (tree010
);
9495 STRIP_NOPS (tree011
);
9496 if (TREE_CODE (tree010
) == INTEGER_CST
9497 && 0 == compare_tree_int (tree010
,
9499 (TREE_TYPE (TREE_OPERAND
9501 && operand_equal_p (tree11
, tree011
, 0))
9502 return fold_convert_loc
9504 build2 ((code0
!= LSHIFT_EXPR
9507 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9508 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9514 /* In most languages, can't associate operations on floats through
9515 parentheses. Rather than remember where the parentheses were, we
9516 don't associate floats at all, unless the user has specified
9518 And, we need to make sure type is not saturating. */
9520 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9521 && !TYPE_SATURATING (type
))
9523 tree var0
, con0
, lit0
, minus_lit0
;
9524 tree var1
, con1
, lit1
, minus_lit1
;
9528 /* Split both trees into variables, constants, and literals. Then
9529 associate each group together, the constants with literals,
9530 then the result with variables. This increases the chances of
9531 literals being recombined later and of generating relocatable
9532 expressions for the sum of a constant and literal. */
9533 var0
= split_tree (loc
, arg0
, type
, code
,
9534 &con0
, &lit0
, &minus_lit0
, 0);
9535 var1
= split_tree (loc
, arg1
, type
, code
,
9536 &con1
, &lit1
, &minus_lit1
, code
== MINUS_EXPR
);
9538 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9539 if (code
== MINUS_EXPR
)
9542 /* With undefined overflow prefer doing association in a type
9543 which wraps on overflow, if that is one of the operand types. */
9544 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9545 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9547 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9548 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9549 atype
= TREE_TYPE (arg0
);
9550 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9551 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9552 atype
= TREE_TYPE (arg1
);
9553 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9556 /* With undefined overflow we can only associate constants with one
9557 variable, and constants whose association doesn't overflow. */
9558 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9559 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9565 bool one_neg
= false;
9567 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9569 tmp0
= TREE_OPERAND (tmp0
, 0);
9572 if (CONVERT_EXPR_P (tmp0
)
9573 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9574 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9575 <= TYPE_PRECISION (atype
)))
9576 tmp0
= TREE_OPERAND (tmp0
, 0);
9577 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9579 tmp1
= TREE_OPERAND (tmp1
, 0);
9582 if (CONVERT_EXPR_P (tmp1
)
9583 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9584 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9585 <= TYPE_PRECISION (atype
)))
9586 tmp1
= TREE_OPERAND (tmp1
, 0);
9587 /* The only case we can still associate with two variables
9588 is if they cancel out. */
9590 || !operand_equal_p (tmp0
, tmp1
, 0))
9595 /* Only do something if we found more than two objects. Otherwise,
9596 nothing has changed and we risk infinite recursion. */
9598 && (2 < ((var0
!= 0) + (var1
!= 0)
9599 + (con0
!= 0) + (con1
!= 0)
9600 + (lit0
!= 0) + (lit1
!= 0)
9601 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9603 bool any_overflows
= false;
9604 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
9605 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
9606 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
9607 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
9608 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9609 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9610 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9611 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9614 /* Preserve the MINUS_EXPR if the negative part of the literal is
9615 greater than the positive part. Otherwise, the multiplicative
9616 folding code (i.e extract_muldiv) may be fooled in case
9617 unsigned constants are subtracted, like in the following
9618 example: ((X*2 + 4) - 8U)/2. */
9619 if (minus_lit0
&& lit0
)
9621 if (TREE_CODE (lit0
) == INTEGER_CST
9622 && TREE_CODE (minus_lit0
) == INTEGER_CST
9623 && tree_int_cst_lt (lit0
, minus_lit0
))
9625 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9631 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9637 /* Don't introduce overflows through reassociation. */
9639 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
9640 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
9647 fold_convert_loc (loc
, type
,
9648 associate_trees (loc
, var0
, minus_lit0
,
9649 MINUS_EXPR
, atype
));
9652 con0
= associate_trees (loc
, con0
, minus_lit0
,
9655 fold_convert_loc (loc
, type
,
9656 associate_trees (loc
, var0
, con0
,
9661 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9663 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9671 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9672 if (TREE_CODE (arg0
) == NEGATE_EXPR
9673 && negate_expr_p (op1
))
9674 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9676 fold_convert_loc (loc
, type
,
9677 TREE_OPERAND (arg0
, 0)));
9679 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9680 __complex__ ( x, -y ). This is not the same for SNaNs or if
9681 signed zeros are involved. */
9682 if (!HONOR_SNANS (element_mode (arg0
))
9683 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9684 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9686 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9687 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9688 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9689 bool arg0rz
= false, arg0iz
= false;
9690 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9691 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9693 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9694 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9695 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9697 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9699 : build1 (REALPART_EXPR
, rtype
, arg1
));
9700 tree ip
= arg0i
? arg0i
9701 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9702 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9704 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9706 tree rp
= arg0r
? arg0r
9707 : build1 (REALPART_EXPR
, rtype
, arg0
);
9708 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9710 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9711 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9716 /* A - B -> A + (-B) if B is easily negatable. */
9717 if (negate_expr_p (op1
)
9718 && ! TYPE_OVERFLOW_SANITIZED (type
)
9719 && ((FLOAT_TYPE_P (type
)
9720 /* Avoid this transformation if B is a positive REAL_CST. */
9721 && (TREE_CODE (op1
) != REAL_CST
9722 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1
))))
9723 || INTEGRAL_TYPE_P (type
)))
9724 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9725 fold_convert_loc (loc
, type
, arg0
),
9728 /* Fold &a[i] - &a[j] to i-j. */
9729 if (TREE_CODE (arg0
) == ADDR_EXPR
9730 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9731 && TREE_CODE (arg1
) == ADDR_EXPR
9732 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9734 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9735 TREE_OPERAND (arg0
, 0),
9736 TREE_OPERAND (arg1
, 0));
9741 if (FLOAT_TYPE_P (type
)
9742 && flag_unsafe_math_optimizations
9743 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9744 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9745 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9748 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9749 one. Make sure the type is not saturating and has the signedness of
9750 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9751 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9752 if ((TREE_CODE (arg0
) == MULT_EXPR
9753 || TREE_CODE (arg1
) == MULT_EXPR
)
9754 && !TYPE_SATURATING (type
)
9755 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9756 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9757 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9759 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9767 if (! FLOAT_TYPE_P (type
))
9769 /* Transform x * -C into -x * C if x is easily negatable. */
9770 if (TREE_CODE (op1
) == INTEGER_CST
9771 && tree_int_cst_sgn (op1
) == -1
9772 && negate_expr_p (op0
)
9773 && (tem
= negate_expr (op1
)) != op1
9774 && ! TREE_OVERFLOW (tem
))
9775 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9776 fold_convert_loc (loc
, type
,
9777 negate_expr (op0
)), tem
);
9779 strict_overflow_p
= false;
9780 if (TREE_CODE (arg1
) == INTEGER_CST
9781 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9782 &strict_overflow_p
)))
9784 if (strict_overflow_p
)
9785 fold_overflow_warning (("assuming signed overflow does not "
9786 "occur when simplifying "
9788 WARN_STRICT_OVERFLOW_MISC
);
9789 return fold_convert_loc (loc
, type
, tem
);
9792 /* Optimize z * conj(z) for integer complex numbers. */
9793 if (TREE_CODE (arg0
) == CONJ_EXPR
9794 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9795 return fold_mult_zconjz (loc
, type
, arg1
);
9796 if (TREE_CODE (arg1
) == CONJ_EXPR
9797 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9798 return fold_mult_zconjz (loc
, type
, arg0
);
9802 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9803 This is not the same for NaNs or if signed zeros are
9805 if (!HONOR_NANS (arg0
)
9806 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9807 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9808 && TREE_CODE (arg1
) == COMPLEX_CST
9809 && real_zerop (TREE_REALPART (arg1
)))
9811 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9812 if (real_onep (TREE_IMAGPART (arg1
)))
9814 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9815 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9817 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9818 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9820 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9821 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9822 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9826 /* Optimize z * conj(z) for floating point complex numbers.
9827 Guarded by flag_unsafe_math_optimizations as non-finite
9828 imaginary components don't produce scalar results. */
9829 if (flag_unsafe_math_optimizations
9830 && TREE_CODE (arg0
) == CONJ_EXPR
9831 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9832 return fold_mult_zconjz (loc
, type
, arg1
);
9833 if (flag_unsafe_math_optimizations
9834 && TREE_CODE (arg1
) == CONJ_EXPR
9835 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9836 return fold_mult_zconjz (loc
, type
, arg0
);
9841 /* Canonicalize (X & C1) | C2. */
9842 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9843 && TREE_CODE (arg1
) == INTEGER_CST
9844 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9846 int width
= TYPE_PRECISION (type
), w
;
9847 wide_int c1
= TREE_OPERAND (arg0
, 1);
9850 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9851 if ((c1
& c2
) == c1
)
9852 return omit_one_operand_loc (loc
, type
, arg1
,
9853 TREE_OPERAND (arg0
, 0));
9855 wide_int msk
= wi::mask (width
, false,
9856 TYPE_PRECISION (TREE_TYPE (arg1
)));
9858 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9859 if (msk
.and_not (c1
| c2
) == 0)
9860 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9861 TREE_OPERAND (arg0
, 0), arg1
);
9863 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9864 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9865 mode which allows further optimizations. */
9868 wide_int c3
= c1
.and_not (c2
);
9869 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
9871 wide_int mask
= wi::mask (w
, false,
9872 TYPE_PRECISION (type
));
9873 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
9881 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9882 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9883 TREE_OPERAND (arg0
, 0),
9884 wide_int_to_tree (type
,
9889 /* See if this can be simplified into a rotate first. If that
9890 is unsuccessful continue in the association code. */
9894 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9895 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9896 && INTEGRAL_TYPE_P (type
)
9897 && integer_onep (TREE_OPERAND (arg0
, 1))
9898 && integer_onep (arg1
))
9899 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
9900 build_zero_cst (TREE_TYPE (arg0
)));
9902 /* See if this can be simplified into a rotate first. If that
9903 is unsuccessful continue in the association code. */
9907 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9908 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9909 && INTEGRAL_TYPE_P (type
)
9910 && integer_onep (TREE_OPERAND (arg0
, 1))
9911 && integer_onep (arg1
))
9914 tem
= TREE_OPERAND (arg0
, 0);
9915 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9916 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9918 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9919 build_zero_cst (TREE_TYPE (tem
)));
9921 /* Fold ~X & 1 as (X & 1) == 0. */
9922 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9923 && INTEGRAL_TYPE_P (type
)
9924 && integer_onep (arg1
))
9927 tem
= TREE_OPERAND (arg0
, 0);
9928 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9929 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9931 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9932 build_zero_cst (TREE_TYPE (tem
)));
9934 /* Fold !X & 1 as X == 0. */
9935 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
9936 && integer_onep (arg1
))
9938 tem
= TREE_OPERAND (arg0
, 0);
9939 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
9940 build_zero_cst (TREE_TYPE (tem
)));
9943 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9944 multiple of 1 << CST. */
9945 if (TREE_CODE (arg1
) == INTEGER_CST
)
9947 wide_int cst1
= arg1
;
9948 wide_int ncst1
= -cst1
;
9949 if ((cst1
& ncst1
) == ncst1
9950 && multiple_of_p (type
, arg0
,
9951 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
9952 return fold_convert_loc (loc
, type
, arg0
);
9955 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
9957 if (TREE_CODE (arg1
) == INTEGER_CST
9958 && TREE_CODE (arg0
) == MULT_EXPR
9959 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9961 wide_int warg1
= arg1
;
9962 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
9965 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
9967 else if (masked
!= warg1
)
9969 /* Avoid the transform if arg1 is a mask of some
9970 mode which allows further optimizations. */
9971 int pop
= wi::popcount (warg1
);
9972 if (!(pop
>= BITS_PER_UNIT
9974 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
9975 return fold_build2_loc (loc
, code
, type
, op0
,
9976 wide_int_to_tree (type
, masked
));
9980 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
9981 ((A & N) + B) & M -> (A + B) & M
9982 Similarly if (N & M) == 0,
9983 ((A | N) + B) & M -> (A + B) & M
9984 and for - instead of + (or unary - instead of +)
9985 and/or ^ instead of |.
9986 If B is constant and (B & M) == 0, fold into A & M. */
9987 if (TREE_CODE (arg1
) == INTEGER_CST
)
9989 wide_int cst1
= arg1
;
9990 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
9991 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9992 && (TREE_CODE (arg0
) == PLUS_EXPR
9993 || TREE_CODE (arg0
) == MINUS_EXPR
9994 || TREE_CODE (arg0
) == NEGATE_EXPR
)
9995 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
9996 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10002 /* Now we know that arg0 is (C + D) or (C - D) or
10003 -C and arg1 (M) is == (1LL << cst) - 1.
10004 Store C into PMOP[0] and D into PMOP[1]. */
10005 pmop
[0] = TREE_OPERAND (arg0
, 0);
10007 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10009 pmop
[1] = TREE_OPERAND (arg0
, 1);
10013 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10016 for (; which
>= 0; which
--)
10017 switch (TREE_CODE (pmop
[which
]))
10022 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10025 cst0
= TREE_OPERAND (pmop
[which
], 1);
10027 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10032 else if (cst0
!= 0)
10034 /* If C or D is of the form (A & N) where
10035 (N & M) == M, or of the form (A | N) or
10036 (A ^ N) where (N & M) == 0, replace it with A. */
10037 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10040 /* If C or D is a N where (N & M) == 0, it can be
10041 omitted (assumed 0). */
10042 if ((TREE_CODE (arg0
) == PLUS_EXPR
10043 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10044 && (cst1
& pmop
[which
]) == 0)
10045 pmop
[which
] = NULL
;
10051 /* Only build anything new if we optimized one or both arguments
10053 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10054 || (TREE_CODE (arg0
) != NEGATE_EXPR
10055 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10057 tree utype
= TREE_TYPE (arg0
);
10058 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10060 /* Perform the operations in a type that has defined
10061 overflow behavior. */
10062 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10063 if (pmop
[0] != NULL
)
10064 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10065 if (pmop
[1] != NULL
)
10066 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10069 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10070 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10071 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10073 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10074 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10076 else if (pmop
[0] != NULL
)
10078 else if (pmop
[1] != NULL
)
10081 return build_int_cst (type
, 0);
10083 else if (pmop
[0] == NULL
)
10084 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10086 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10088 /* TEM is now the new binary +, - or unary - replacement. */
10089 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10090 fold_convert_loc (loc
, utype
, arg1
));
10091 return fold_convert_loc (loc
, type
, tem
);
10096 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10097 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10098 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10100 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10102 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10105 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10111 /* Don't touch a floating-point divide by zero unless the mode
10112 of the constant can represent infinity. */
10113 if (TREE_CODE (arg1
) == REAL_CST
10114 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10115 && real_zerop (arg1
))
10118 /* (-A) / (-B) -> A / B */
10119 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10120 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10121 TREE_OPERAND (arg0
, 0),
10122 negate_expr (arg1
));
10123 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10124 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10125 negate_expr (arg0
),
10126 TREE_OPERAND (arg1
, 0));
10129 case TRUNC_DIV_EXPR
:
10132 case FLOOR_DIV_EXPR
:
10133 /* Simplify A / (B << N) where A and B are positive and B is
10134 a power of 2, to A >> (N + log2(B)). */
10135 strict_overflow_p
= false;
10136 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10137 && (TYPE_UNSIGNED (type
)
10138 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10140 tree sval
= TREE_OPERAND (arg1
, 0);
10141 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10143 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10144 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10145 wi::exact_log2 (sval
));
10147 if (strict_overflow_p
)
10148 fold_overflow_warning (("assuming signed overflow does not "
10149 "occur when simplifying A / (B << N)"),
10150 WARN_STRICT_OVERFLOW_MISC
);
10152 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10154 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10155 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10161 case ROUND_DIV_EXPR
:
10162 case CEIL_DIV_EXPR
:
10163 case EXACT_DIV_EXPR
:
10164 if (integer_zerop (arg1
))
10167 /* Convert -A / -B to A / B when the type is signed and overflow is
10169 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10170 && TREE_CODE (arg0
) == NEGATE_EXPR
10171 && negate_expr_p (op1
))
10173 if (INTEGRAL_TYPE_P (type
))
10174 fold_overflow_warning (("assuming signed overflow does not occur "
10175 "when distributing negation across "
10177 WARN_STRICT_OVERFLOW_MISC
);
10178 return fold_build2_loc (loc
, code
, type
,
10179 fold_convert_loc (loc
, type
,
10180 TREE_OPERAND (arg0
, 0)),
10181 negate_expr (op1
));
10183 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10184 && TREE_CODE (arg1
) == NEGATE_EXPR
10185 && negate_expr_p (op0
))
10187 if (INTEGRAL_TYPE_P (type
))
10188 fold_overflow_warning (("assuming signed overflow does not occur "
10189 "when distributing negation across "
10191 WARN_STRICT_OVERFLOW_MISC
);
10192 return fold_build2_loc (loc
, code
, type
,
10194 fold_convert_loc (loc
, type
,
10195 TREE_OPERAND (arg1
, 0)));
10198 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10199 operation, EXACT_DIV_EXPR.
10201 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10202 At one time others generated faster code, it's not clear if they do
10203 after the last round to changes to the DIV code in expmed.c. */
10204 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10205 && multiple_of_p (type
, arg0
, arg1
))
10206 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10207 fold_convert (type
, arg0
),
10208 fold_convert (type
, arg1
));
10210 strict_overflow_p
= false;
10211 if (TREE_CODE (arg1
) == INTEGER_CST
10212 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10213 &strict_overflow_p
)))
10215 if (strict_overflow_p
)
10216 fold_overflow_warning (("assuming signed overflow does not occur "
10217 "when simplifying division"),
10218 WARN_STRICT_OVERFLOW_MISC
);
10219 return fold_convert_loc (loc
, type
, tem
);
10224 case CEIL_MOD_EXPR
:
10225 case FLOOR_MOD_EXPR
:
10226 case ROUND_MOD_EXPR
:
10227 case TRUNC_MOD_EXPR
:
10228 strict_overflow_p
= false;
10229 if (TREE_CODE (arg1
) == INTEGER_CST
10230 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10231 &strict_overflow_p
)))
10233 if (strict_overflow_p
)
10234 fold_overflow_warning (("assuming signed overflow does not occur "
10235 "when simplifying modulus"),
10236 WARN_STRICT_OVERFLOW_MISC
);
10237 return fold_convert_loc (loc
, type
, tem
);
10246 /* Since negative shift count is not well-defined,
10247 don't try to compute it in the compiler. */
10248 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10251 prec
= element_precision (type
);
10253 /* If we have a rotate of a bit operation with the rotate count and
10254 the second operand of the bit operation both constant,
10255 permute the two operations. */
10256 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10257 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10258 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10259 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10260 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10262 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10263 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10264 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10265 fold_build2_loc (loc
, code
, type
,
10267 fold_build2_loc (loc
, code
, type
,
10271 /* Two consecutive rotates adding up to the some integer
10272 multiple of the precision of the type can be ignored. */
10273 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10274 && TREE_CODE (arg0
) == RROTATE_EXPR
10275 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10276 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10278 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10286 case TRUTH_ANDIF_EXPR
:
10287 /* Note that the operands of this must be ints
10288 and their values must be 0 or 1.
10289 ("true" is a fixed value perhaps depending on the language.) */
10290 /* If first arg is constant zero, return it. */
10291 if (integer_zerop (arg0
))
10292 return fold_convert_loc (loc
, type
, arg0
);
10294 case TRUTH_AND_EXPR
:
10295 /* If either arg is constant true, drop it. */
10296 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10297 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10298 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10299 /* Preserve sequence points. */
10300 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10301 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10302 /* If second arg is constant zero, result is zero, but first arg
10303 must be evaluated. */
10304 if (integer_zerop (arg1
))
10305 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10306 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10307 case will be handled here. */
10308 if (integer_zerop (arg0
))
10309 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10311 /* !X && X is always false. */
10312 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10313 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10314 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10315 /* X && !X is always false. */
10316 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10317 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10318 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10320 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10321 means A >= Y && A != MAX, but in this case we know that
10324 if (!TREE_SIDE_EFFECTS (arg0
)
10325 && !TREE_SIDE_EFFECTS (arg1
))
10327 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10328 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10329 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10331 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10332 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10333 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10336 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10342 case TRUTH_ORIF_EXPR
:
10343 /* Note that the operands of this must be ints
10344 and their values must be 0 or true.
10345 ("true" is a fixed value perhaps depending on the language.) */
10346 /* If first arg is constant true, return it. */
10347 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10348 return fold_convert_loc (loc
, type
, arg0
);
10350 case TRUTH_OR_EXPR
:
10351 /* If either arg is constant zero, drop it. */
10352 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10353 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10354 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10355 /* Preserve sequence points. */
10356 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10357 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10358 /* If second arg is constant true, result is true, but we must
10359 evaluate first arg. */
10360 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10361 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10362 /* Likewise for first arg, but note this only occurs here for
10364 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10365 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10367 /* !X || X is always true. */
10368 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10369 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10370 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10371 /* X || !X is always true. */
10372 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10373 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10374 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10376 /* (X && !Y) || (!X && Y) is X ^ Y */
10377 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10378 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10380 tree a0
, a1
, l0
, l1
, n0
, n1
;
10382 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10383 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10385 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10386 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10388 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10389 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10391 if ((operand_equal_p (n0
, a0
, 0)
10392 && operand_equal_p (n1
, a1
, 0))
10393 || (operand_equal_p (n0
, a1
, 0)
10394 && operand_equal_p (n1
, a0
, 0)))
10395 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10398 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10404 case TRUTH_XOR_EXPR
:
10405 /* If the second arg is constant zero, drop it. */
10406 if (integer_zerop (arg1
))
10407 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10408 /* If the second arg is constant true, this is a logical inversion. */
10409 if (integer_onep (arg1
))
10411 tem
= invert_truthvalue_loc (loc
, arg0
);
10412 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10414 /* Identical arguments cancel to zero. */
10415 if (operand_equal_p (arg0
, arg1
, 0))
10416 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10418 /* !X ^ X is always true. */
10419 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10420 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10421 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10423 /* X ^ !X is always true. */
10424 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10425 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10426 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10435 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10436 if (tem
!= NULL_TREE
)
10439 /* bool_var != 1 becomes !bool_var. */
10440 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10441 && code
== NE_EXPR
)
10442 return fold_convert_loc (loc
, type
,
10443 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10444 TREE_TYPE (arg0
), arg0
));
10446 /* bool_var == 0 becomes !bool_var. */
10447 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10448 && code
== EQ_EXPR
)
10449 return fold_convert_loc (loc
, type
,
10450 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10451 TREE_TYPE (arg0
), arg0
));
10453 /* !exp != 0 becomes !exp */
10454 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10455 && code
== NE_EXPR
)
10456 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10458 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10459 if ((TREE_CODE (arg0
) == PLUS_EXPR
10460 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10461 || TREE_CODE (arg0
) == MINUS_EXPR
)
10462 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10465 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10466 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10468 tree val
= TREE_OPERAND (arg0
, 1);
10469 val
= fold_build2_loc (loc
, code
, type
, val
,
10470 build_int_cst (TREE_TYPE (val
), 0));
10471 return omit_two_operands_loc (loc
, type
, val
,
10472 TREE_OPERAND (arg0
, 0), arg1
);
10475 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10476 if ((TREE_CODE (arg1
) == PLUS_EXPR
10477 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
10478 || TREE_CODE (arg1
) == MINUS_EXPR
)
10479 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10482 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10483 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
10485 tree val
= TREE_OPERAND (arg1
, 1);
10486 val
= fold_build2_loc (loc
, code
, type
, val
,
10487 build_int_cst (TREE_TYPE (val
), 0));
10488 return omit_two_operands_loc (loc
, type
, val
,
10489 TREE_OPERAND (arg1
, 0), arg0
);
10492 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10493 if (TREE_CODE (arg0
) == MINUS_EXPR
10494 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
10495 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10498 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
10499 return omit_two_operands_loc (loc
, type
,
10501 ? boolean_true_node
: boolean_false_node
,
10502 TREE_OPERAND (arg0
, 1), arg1
);
10504 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10505 if (TREE_CODE (arg1
) == MINUS_EXPR
10506 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == INTEGER_CST
10507 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1
,
10510 && wi::extract_uhwi (TREE_OPERAND (arg1
, 0), 0, 1) == 1)
10511 return omit_two_operands_loc (loc
, type
,
10513 ? boolean_true_node
: boolean_false_node
,
10514 TREE_OPERAND (arg1
, 1), arg0
);
10516 /* If this is an EQ or NE comparison with zero and ARG0 is
10517 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10518 two operations, but the latter can be done in one less insn
10519 on machines that have only two-operand insns or on which a
10520 constant cannot be the first operand. */
10521 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10522 && integer_zerop (arg1
))
10524 tree arg00
= TREE_OPERAND (arg0
, 0);
10525 tree arg01
= TREE_OPERAND (arg0
, 1);
10526 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10527 && integer_onep (TREE_OPERAND (arg00
, 0)))
10529 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10530 arg01
, TREE_OPERAND (arg00
, 1));
10531 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10532 build_int_cst (TREE_TYPE (arg0
), 1));
10533 return fold_build2_loc (loc
, code
, type
,
10534 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10537 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10538 && integer_onep (TREE_OPERAND (arg01
, 0)))
10540 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10541 arg00
, TREE_OPERAND (arg01
, 1));
10542 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10543 build_int_cst (TREE_TYPE (arg0
), 1));
10544 return fold_build2_loc (loc
, code
, type
,
10545 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10550 /* If this is an NE or EQ comparison of zero against the result of a
10551 signed MOD operation whose second operand is a power of 2, make
10552 the MOD operation unsigned since it is simpler and equivalent. */
10553 if (integer_zerop (arg1
)
10554 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10555 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10556 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10557 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10558 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10559 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10561 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10562 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10563 fold_convert_loc (loc
, newtype
,
10564 TREE_OPERAND (arg0
, 0)),
10565 fold_convert_loc (loc
, newtype
,
10566 TREE_OPERAND (arg0
, 1)));
10568 return fold_build2_loc (loc
, code
, type
, newmod
,
10569 fold_convert_loc (loc
, newtype
, arg1
));
10572 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10573 C1 is a valid shift constant, and C2 is a power of two, i.e.
10575 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10576 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10577 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10579 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10580 && integer_zerop (arg1
))
10582 tree itype
= TREE_TYPE (arg0
);
10583 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10584 prec
= TYPE_PRECISION (itype
);
10586 /* Check for a valid shift count. */
10587 if (wi::ltu_p (arg001
, prec
))
10589 tree arg01
= TREE_OPERAND (arg0
, 1);
10590 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10591 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10592 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10593 can be rewritten as (X & (C2 << C1)) != 0. */
10594 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10596 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10597 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10598 return fold_build2_loc (loc
, code
, type
, tem
,
10599 fold_convert_loc (loc
, itype
, arg1
));
10601 /* Otherwise, for signed (arithmetic) shifts,
10602 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10603 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10604 else if (!TYPE_UNSIGNED (itype
))
10605 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10606 arg000
, build_int_cst (itype
, 0));
10607 /* Otherwise, of unsigned (logical) shifts,
10608 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10609 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10611 return omit_one_operand_loc (loc
, type
,
10612 code
== EQ_EXPR
? integer_one_node
10613 : integer_zero_node
,
10618 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10619 Similarly for NE_EXPR. */
10620 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10621 && TREE_CODE (arg1
) == INTEGER_CST
10622 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10624 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
10625 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
10626 TREE_OPERAND (arg0
, 1));
10628 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10629 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
10631 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
10632 if (integer_nonzerop (dandnotc
))
10633 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
10636 /* If this is a comparison of a field, we may be able to simplify it. */
10637 if ((TREE_CODE (arg0
) == COMPONENT_REF
10638 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10639 /* Handle the constant case even without -O
10640 to make sure the warnings are given. */
10641 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10643 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10648 /* Optimize comparisons of strlen vs zero to a compare of the
10649 first character of the string vs zero. To wit,
10650 strlen(ptr) == 0 => *ptr == 0
10651 strlen(ptr) != 0 => *ptr != 0
10652 Other cases should reduce to one of these two (or a constant)
10653 due to the return value of strlen being unsigned. */
10654 if (TREE_CODE (arg0
) == CALL_EXPR
10655 && integer_zerop (arg1
))
10657 tree fndecl
= get_callee_fndecl (arg0
);
10660 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10661 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10662 && call_expr_nargs (arg0
) == 1
10663 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10665 tree iref
= build_fold_indirect_ref_loc (loc
,
10666 CALL_EXPR_ARG (arg0
, 0));
10667 return fold_build2_loc (loc
, code
, type
, iref
,
10668 build_int_cst (TREE_TYPE (iref
), 0));
10672 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10673 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10674 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10675 && integer_zerop (arg1
)
10676 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10678 tree arg00
= TREE_OPERAND (arg0
, 0);
10679 tree arg01
= TREE_OPERAND (arg0
, 1);
10680 tree itype
= TREE_TYPE (arg00
);
10681 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10683 if (TYPE_UNSIGNED (itype
))
10685 itype
= signed_type_for (itype
);
10686 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10688 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10689 type
, arg00
, build_zero_cst (itype
));
10693 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10694 (X & C) == 0 when C is a single bit. */
10695 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10696 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10697 && integer_zerop (arg1
)
10698 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10700 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10701 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10702 TREE_OPERAND (arg0
, 1));
10703 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10705 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10709 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10710 constant C is a power of two, i.e. a single bit. */
10711 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10712 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10713 && integer_zerop (arg1
)
10714 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10715 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10716 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10718 tree arg00
= TREE_OPERAND (arg0
, 0);
10719 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10720 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10723 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10724 when is C is a power of two, i.e. a single bit. */
10725 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10726 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10727 && integer_zerop (arg1
)
10728 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10729 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10730 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10732 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10733 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10734 arg000
, TREE_OPERAND (arg0
, 1));
10735 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10736 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10739 if (integer_zerop (arg1
)
10740 && tree_expr_nonzero_p (arg0
))
10742 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10743 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10746 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10747 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10748 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10750 tree arg00
= TREE_OPERAND (arg0
, 0);
10751 tree arg01
= TREE_OPERAND (arg0
, 1);
10752 tree arg10
= TREE_OPERAND (arg1
, 0);
10753 tree arg11
= TREE_OPERAND (arg1
, 1);
10754 tree itype
= TREE_TYPE (arg0
);
10756 if (operand_equal_p (arg01
, arg11
, 0))
10757 return fold_build2_loc (loc
, code
, type
,
10758 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10759 fold_build2_loc (loc
,
10760 BIT_XOR_EXPR
, itype
,
10763 build_zero_cst (itype
));
10765 if (operand_equal_p (arg01
, arg10
, 0))
10766 return fold_build2_loc (loc
, code
, type
,
10767 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10768 fold_build2_loc (loc
,
10769 BIT_XOR_EXPR
, itype
,
10772 build_zero_cst (itype
));
10774 if (operand_equal_p (arg00
, arg11
, 0))
10775 return fold_build2_loc (loc
, code
, type
,
10776 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10777 fold_build2_loc (loc
,
10778 BIT_XOR_EXPR
, itype
,
10781 build_zero_cst (itype
));
10783 if (operand_equal_p (arg00
, arg10
, 0))
10784 return fold_build2_loc (loc
, code
, type
,
10785 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10786 fold_build2_loc (loc
,
10787 BIT_XOR_EXPR
, itype
,
10790 build_zero_cst (itype
));
10793 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10794 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10796 tree arg00
= TREE_OPERAND (arg0
, 0);
10797 tree arg01
= TREE_OPERAND (arg0
, 1);
10798 tree arg10
= TREE_OPERAND (arg1
, 0);
10799 tree arg11
= TREE_OPERAND (arg1
, 1);
10800 tree itype
= TREE_TYPE (arg0
);
10802 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10803 operand_equal_p guarantees no side-effects so we don't need
10804 to use omit_one_operand on Z. */
10805 if (operand_equal_p (arg01
, arg11
, 0))
10806 return fold_build2_loc (loc
, code
, type
, arg00
,
10807 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10809 if (operand_equal_p (arg01
, arg10
, 0))
10810 return fold_build2_loc (loc
, code
, type
, arg00
,
10811 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10813 if (operand_equal_p (arg00
, arg11
, 0))
10814 return fold_build2_loc (loc
, code
, type
, arg01
,
10815 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10817 if (operand_equal_p (arg00
, arg10
, 0))
10818 return fold_build2_loc (loc
, code
, type
, arg01
,
10819 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10822 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10823 if (TREE_CODE (arg01
) == INTEGER_CST
10824 && TREE_CODE (arg11
) == INTEGER_CST
)
10826 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10827 fold_convert_loc (loc
, itype
, arg11
));
10828 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10829 return fold_build2_loc (loc
, code
, type
, tem
,
10830 fold_convert_loc (loc
, itype
, arg10
));
10834 /* Attempt to simplify equality/inequality comparisons of complex
10835 values. Only lower the comparison if the result is known or
10836 can be simplified to a single scalar comparison. */
10837 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10838 || TREE_CODE (arg0
) == COMPLEX_CST
)
10839 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10840 || TREE_CODE (arg1
) == COMPLEX_CST
))
10842 tree real0
, imag0
, real1
, imag1
;
10845 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10847 real0
= TREE_OPERAND (arg0
, 0);
10848 imag0
= TREE_OPERAND (arg0
, 1);
10852 real0
= TREE_REALPART (arg0
);
10853 imag0
= TREE_IMAGPART (arg0
);
10856 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
10858 real1
= TREE_OPERAND (arg1
, 0);
10859 imag1
= TREE_OPERAND (arg1
, 1);
10863 real1
= TREE_REALPART (arg1
);
10864 imag1
= TREE_IMAGPART (arg1
);
10867 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
10868 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
10870 if (integer_zerop (rcond
))
10872 if (code
== EQ_EXPR
)
10873 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10875 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
10879 if (code
== NE_EXPR
)
10880 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10882 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
10886 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
10887 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
10889 if (integer_zerop (icond
))
10891 if (code
== EQ_EXPR
)
10892 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10894 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
10898 if (code
== NE_EXPR
)
10899 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10901 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
10912 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10913 if (tem
!= NULL_TREE
)
10916 /* Transform comparisons of the form X +- C CMP X. */
10917 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10918 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10919 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
10920 && !HONOR_SNANS (arg0
))
10921 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10922 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
10924 tree arg01
= TREE_OPERAND (arg0
, 1);
10925 enum tree_code code0
= TREE_CODE (arg0
);
10928 if (TREE_CODE (arg01
) == REAL_CST
)
10929 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
10931 is_positive
= tree_int_cst_sgn (arg01
);
10933 /* (X - c) > X becomes false. */
10934 if (code
== GT_EXPR
10935 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10936 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10938 if (TREE_CODE (arg01
) == INTEGER_CST
10939 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10940 fold_overflow_warning (("assuming signed overflow does not "
10941 "occur when assuming that (X - c) > X "
10942 "is always false"),
10943 WARN_STRICT_OVERFLOW_ALL
);
10944 return constant_boolean_node (0, type
);
10947 /* Likewise (X + c) < X becomes false. */
10948 if (code
== LT_EXPR
10949 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10950 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10952 if (TREE_CODE (arg01
) == INTEGER_CST
10953 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10954 fold_overflow_warning (("assuming signed overflow does not "
10955 "occur when assuming that "
10956 "(X + c) < X is always false"),
10957 WARN_STRICT_OVERFLOW_ALL
);
10958 return constant_boolean_node (0, type
);
10961 /* Convert (X - c) <= X to true. */
10962 if (!HONOR_NANS (arg1
)
10964 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10965 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10967 if (TREE_CODE (arg01
) == INTEGER_CST
10968 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10969 fold_overflow_warning (("assuming signed overflow does not "
10970 "occur when assuming that "
10971 "(X - c) <= X is always true"),
10972 WARN_STRICT_OVERFLOW_ALL
);
10973 return constant_boolean_node (1, type
);
10976 /* Convert (X + c) >= X to true. */
10977 if (!HONOR_NANS (arg1
)
10979 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
10980 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
10982 if (TREE_CODE (arg01
) == INTEGER_CST
10983 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10984 fold_overflow_warning (("assuming signed overflow does not "
10985 "occur when assuming that "
10986 "(X + c) >= X is always true"),
10987 WARN_STRICT_OVERFLOW_ALL
);
10988 return constant_boolean_node (1, type
);
10991 if (TREE_CODE (arg01
) == INTEGER_CST
)
10993 /* Convert X + c > X and X - c < X to true for integers. */
10994 if (code
== GT_EXPR
10995 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
10996 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
10998 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10999 fold_overflow_warning (("assuming signed overflow does "
11000 "not occur when assuming that "
11001 "(X + c) > X is always true"),
11002 WARN_STRICT_OVERFLOW_ALL
);
11003 return constant_boolean_node (1, type
);
11006 if (code
== LT_EXPR
11007 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11008 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11010 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11011 fold_overflow_warning (("assuming signed overflow does "
11012 "not occur when assuming that "
11013 "(X - c) < X is always true"),
11014 WARN_STRICT_OVERFLOW_ALL
);
11015 return constant_boolean_node (1, type
);
11018 /* Convert X + c <= X and X - c >= X to false for integers. */
11019 if (code
== LE_EXPR
11020 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11021 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11023 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11024 fold_overflow_warning (("assuming signed overflow does "
11025 "not occur when assuming that "
11026 "(X + c) <= X is always false"),
11027 WARN_STRICT_OVERFLOW_ALL
);
11028 return constant_boolean_node (0, type
);
11031 if (code
== GE_EXPR
11032 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11033 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11035 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11036 fold_overflow_warning (("assuming signed overflow does "
11037 "not occur when assuming that "
11038 "(X - c) >= X is always false"),
11039 WARN_STRICT_OVERFLOW_ALL
);
11040 return constant_boolean_node (0, type
);
11045 /* If we are comparing an ABS_EXPR with a constant, we can
11046 convert all the cases into explicit comparisons, but they may
11047 well not be faster than doing the ABS and one comparison.
11048 But ABS (X) <= C is a range comparison, which becomes a subtraction
11049 and a comparison, and is probably faster. */
11050 if (code
== LE_EXPR
11051 && TREE_CODE (arg1
) == INTEGER_CST
11052 && TREE_CODE (arg0
) == ABS_EXPR
11053 && ! TREE_SIDE_EFFECTS (arg0
)
11054 && (0 != (tem
= negate_expr (arg1
)))
11055 && TREE_CODE (tem
) == INTEGER_CST
11056 && !TREE_OVERFLOW (tem
))
11057 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11058 build2 (GE_EXPR
, type
,
11059 TREE_OPERAND (arg0
, 0), tem
),
11060 build2 (LE_EXPR
, type
,
11061 TREE_OPERAND (arg0
, 0), arg1
));
11063 /* Convert ABS_EXPR<x> >= 0 to true. */
11064 strict_overflow_p
= false;
11065 if (code
== GE_EXPR
11066 && (integer_zerop (arg1
)
11067 || (! HONOR_NANS (arg0
)
11068 && real_zerop (arg1
)))
11069 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11071 if (strict_overflow_p
)
11072 fold_overflow_warning (("assuming signed overflow does not occur "
11073 "when simplifying comparison of "
11074 "absolute value and zero"),
11075 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11076 return omit_one_operand_loc (loc
, type
,
11077 constant_boolean_node (true, type
),
11081 /* Convert ABS_EXPR<x> < 0 to false. */
11082 strict_overflow_p
= false;
11083 if (code
== LT_EXPR
11084 && (integer_zerop (arg1
) || real_zerop (arg1
))
11085 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11087 if (strict_overflow_p
)
11088 fold_overflow_warning (("assuming signed overflow does not occur "
11089 "when simplifying comparison of "
11090 "absolute value and zero"),
11091 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11092 return omit_one_operand_loc (loc
, type
,
11093 constant_boolean_node (false, type
),
11097 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11098 and similarly for >= into !=. */
11099 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11100 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11101 && TREE_CODE (arg1
) == LSHIFT_EXPR
11102 && integer_onep (TREE_OPERAND (arg1
, 0)))
11103 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11104 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11105 TREE_OPERAND (arg1
, 1)),
11106 build_zero_cst (TREE_TYPE (arg0
)));
11108 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11109 otherwise Y might be >= # of bits in X's type and thus e.g.
11110 (unsigned char) (1 << Y) for Y 15 might be 0.
11111 If the cast is widening, then 1 << Y should have unsigned type,
11112 otherwise if Y is number of bits in the signed shift type minus 1,
11113 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11114 31 might be 0xffffffff80000000. */
11115 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11116 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11117 && CONVERT_EXPR_P (arg1
)
11118 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11119 && (element_precision (TREE_TYPE (arg1
))
11120 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11121 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11122 || (element_precision (TREE_TYPE (arg1
))
11123 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11124 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11126 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11127 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11128 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11129 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11130 build_zero_cst (TREE_TYPE (arg0
)));
11135 case UNORDERED_EXPR
:
11143 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11145 tree targ0
= strip_float_extensions (arg0
);
11146 tree targ1
= strip_float_extensions (arg1
);
11147 tree newtype
= TREE_TYPE (targ0
);
11149 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11150 newtype
= TREE_TYPE (targ1
);
11152 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11153 return fold_build2_loc (loc
, code
, type
,
11154 fold_convert_loc (loc
, newtype
, targ0
),
11155 fold_convert_loc (loc
, newtype
, targ1
));
11160 case COMPOUND_EXPR
:
11161 /* When pedantic, a compound expression can be neither an lvalue
11162 nor an integer constant expression. */
11163 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11165 /* Don't let (0, 0) be null pointer constant. */
11166 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11167 : fold_convert_loc (loc
, type
, arg1
);
11168 return pedantic_non_lvalue_loc (loc
, tem
);
11171 /* An ASSERT_EXPR should never be passed to fold_binary. */
11172 gcc_unreachable ();
11176 } /* switch (code) */
11179 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11180 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11184 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11186 switch (TREE_CODE (*tp
))
11192 *walk_subtrees
= 0;
11201 /* Return whether the sub-tree ST contains a label which is accessible from
11202 outside the sub-tree. */
11205 contains_label_p (tree st
)
11208 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11211 /* Fold a ternary expression of code CODE and type TYPE with operands
11212 OP0, OP1, and OP2. Return the folded expression if folding is
11213 successful. Otherwise, return NULL_TREE. */
11216 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11217 tree op0
, tree op1
, tree op2
)
11220 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11221 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11223 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11224 && TREE_CODE_LENGTH (code
) == 3);
11226 /* If this is a commutative operation, and OP0 is a constant, move it
11227 to OP1 to reduce the number of tests below. */
11228 if (commutative_ternary_tree_code (code
)
11229 && tree_swap_operands_p (op0
, op1
))
11230 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11232 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11236 /* Strip any conversions that don't change the mode. This is safe
11237 for every expression, except for a comparison expression because
11238 its signedness is derived from its operands. So, in the latter
11239 case, only strip conversions that don't change the signedness.
11241 Note that this is done as an internal manipulation within the
11242 constant folder, in order to find the simplest representation of
11243 the arguments so that their form can be studied. In any cases,
11244 the appropriate type conversions should be put back in the tree
11245 that will get out of the constant folder. */
11266 case COMPONENT_REF
:
11267 if (TREE_CODE (arg0
) == CONSTRUCTOR
11268 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11270 unsigned HOST_WIDE_INT idx
;
11272 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11279 case VEC_COND_EXPR
:
11280 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11281 so all simple results must be passed through pedantic_non_lvalue. */
11282 if (TREE_CODE (arg0
) == INTEGER_CST
)
11284 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11285 tem
= integer_zerop (arg0
) ? op2
: op1
;
11286 /* Only optimize constant conditions when the selected branch
11287 has the same type as the COND_EXPR. This avoids optimizing
11288 away "c ? x : throw", where the throw has a void type.
11289 Avoid throwing away that operand which contains label. */
11290 if ((!TREE_SIDE_EFFECTS (unused_op
)
11291 || !contains_label_p (unused_op
))
11292 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11293 || VOID_TYPE_P (type
)))
11294 return pedantic_non_lvalue_loc (loc
, tem
);
11297 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11299 if ((TREE_CODE (arg1
) == VECTOR_CST
11300 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11301 && (TREE_CODE (arg2
) == VECTOR_CST
11302 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11304 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
11305 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
11306 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
11307 for (i
= 0; i
< nelts
; i
++)
11309 tree val
= VECTOR_CST_ELT (arg0
, i
);
11310 if (integer_all_onesp (val
))
11312 else if (integer_zerop (val
))
11313 sel
[i
] = nelts
+ i
;
11314 else /* Currently unreachable. */
11317 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11318 if (t
!= NULL_TREE
)
11323 /* If we have A op B ? A : C, we may be able to convert this to a
11324 simpler expression, depending on the operation and the values
11325 of B and C. Signed zeros prevent all of these transformations,
11326 for reasons given above each one.
11328 Also try swapping the arguments and inverting the conditional. */
11329 if (COMPARISON_CLASS_P (arg0
)
11330 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11331 arg1
, TREE_OPERAND (arg0
, 1))
11332 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11334 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11339 if (COMPARISON_CLASS_P (arg0
)
11340 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11342 TREE_OPERAND (arg0
, 1))
11343 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11345 location_t loc0
= expr_location_or (arg0
, loc
);
11346 tem
= fold_invert_truthvalue (loc0
, arg0
);
11347 if (tem
&& COMPARISON_CLASS_P (tem
))
11349 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11355 /* If the second operand is simpler than the third, swap them
11356 since that produces better jump optimization results. */
11357 if (truth_value_p (TREE_CODE (arg0
))
11358 && tree_swap_operands_p (op1
, op2
))
11360 location_t loc0
= expr_location_or (arg0
, loc
);
11361 /* See if this can be inverted. If it can't, possibly because
11362 it was a floating-point inequality comparison, don't do
11364 tem
= fold_invert_truthvalue (loc0
, arg0
);
11366 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11369 /* Convert A ? 1 : 0 to simply A. */
11370 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11371 : (integer_onep (op1
)
11372 && !VECTOR_TYPE_P (type
)))
11373 && integer_zerop (op2
)
11374 /* If we try to convert OP0 to our type, the
11375 call to fold will try to move the conversion inside
11376 a COND, which will recurse. In that case, the COND_EXPR
11377 is probably the best choice, so leave it alone. */
11378 && type
== TREE_TYPE (arg0
))
11379 return pedantic_non_lvalue_loc (loc
, arg0
);
11381 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11382 over COND_EXPR in cases such as floating point comparisons. */
11383 if (integer_zerop (op1
)
11384 && code
== COND_EXPR
11385 && integer_onep (op2
)
11386 && !VECTOR_TYPE_P (type
)
11387 && truth_value_p (TREE_CODE (arg0
)))
11388 return pedantic_non_lvalue_loc (loc
,
11389 fold_convert_loc (loc
, type
,
11390 invert_truthvalue_loc (loc
,
11393 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11394 if (TREE_CODE (arg0
) == LT_EXPR
11395 && integer_zerop (TREE_OPERAND (arg0
, 1))
11396 && integer_zerop (op2
)
11397 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11399 /* sign_bit_p looks through both zero and sign extensions,
11400 but for this optimization only sign extensions are
11402 tree tem2
= TREE_OPERAND (arg0
, 0);
11403 while (tem
!= tem2
)
11405 if (TREE_CODE (tem2
) != NOP_EXPR
11406 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11411 tem2
= TREE_OPERAND (tem2
, 0);
11413 /* sign_bit_p only checks ARG1 bits within A's precision.
11414 If <sign bit of A> has wider type than A, bits outside
11415 of A's precision in <sign bit of A> need to be checked.
11416 If they are all 0, this optimization needs to be done
11417 in unsigned A's type, if they are all 1 in signed A's type,
11418 otherwise this can't be done. */
11420 && TYPE_PRECISION (TREE_TYPE (tem
))
11421 < TYPE_PRECISION (TREE_TYPE (arg1
))
11422 && TYPE_PRECISION (TREE_TYPE (tem
))
11423 < TYPE_PRECISION (type
))
11425 int inner_width
, outer_width
;
11428 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11429 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11430 if (outer_width
> TYPE_PRECISION (type
))
11431 outer_width
= TYPE_PRECISION (type
);
11433 wide_int mask
= wi::shifted_mask
11434 (inner_width
, outer_width
- inner_width
, false,
11435 TYPE_PRECISION (TREE_TYPE (arg1
)));
11437 wide_int common
= mask
& arg1
;
11438 if (common
== mask
)
11440 tem_type
= signed_type_for (TREE_TYPE (tem
));
11441 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11443 else if (common
== 0)
11445 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11446 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11454 fold_convert_loc (loc
, type
,
11455 fold_build2_loc (loc
, BIT_AND_EXPR
,
11456 TREE_TYPE (tem
), tem
,
11457 fold_convert_loc (loc
,
11462 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11463 already handled above. */
11464 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11465 && integer_onep (TREE_OPERAND (arg0
, 1))
11466 && integer_zerop (op2
)
11467 && integer_pow2p (arg1
))
11469 tree tem
= TREE_OPERAND (arg0
, 0);
11471 if (TREE_CODE (tem
) == RSHIFT_EXPR
11472 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11473 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
11474 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11475 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11476 TREE_OPERAND (tem
, 0), arg1
);
11479 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11480 is probably obsolete because the first operand should be a
11481 truth value (that's why we have the two cases above), but let's
11482 leave it in until we can confirm this for all front-ends. */
11483 if (integer_zerop (op2
)
11484 && TREE_CODE (arg0
) == NE_EXPR
11485 && integer_zerop (TREE_OPERAND (arg0
, 1))
11486 && integer_pow2p (arg1
)
11487 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11488 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11489 arg1
, OEP_ONLY_CONST
))
11490 return pedantic_non_lvalue_loc (loc
,
11491 fold_convert_loc (loc
, type
,
11492 TREE_OPERAND (arg0
, 0)));
11494 /* Disable the transformations below for vectors, since
11495 fold_binary_op_with_conditional_arg may undo them immediately,
11496 yielding an infinite loop. */
11497 if (code
== VEC_COND_EXPR
)
11500 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11501 if (integer_zerop (op2
)
11502 && truth_value_p (TREE_CODE (arg0
))
11503 && truth_value_p (TREE_CODE (arg1
))
11504 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11505 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11506 : TRUTH_ANDIF_EXPR
,
11507 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
11509 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11510 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11511 && truth_value_p (TREE_CODE (arg0
))
11512 && truth_value_p (TREE_CODE (arg1
))
11513 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11515 location_t loc0
= expr_location_or (arg0
, loc
);
11516 /* Only perform transformation if ARG0 is easily inverted. */
11517 tem
= fold_invert_truthvalue (loc0
, arg0
);
11519 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11522 type
, fold_convert_loc (loc
, type
, tem
),
11526 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11527 if (integer_zerop (arg1
)
11528 && truth_value_p (TREE_CODE (arg0
))
11529 && truth_value_p (TREE_CODE (op2
))
11530 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11532 location_t loc0
= expr_location_or (arg0
, loc
);
11533 /* Only perform transformation if ARG0 is easily inverted. */
11534 tem
= fold_invert_truthvalue (loc0
, arg0
);
11536 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11537 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11538 type
, fold_convert_loc (loc
, type
, tem
),
11542 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11543 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11544 && truth_value_p (TREE_CODE (arg0
))
11545 && truth_value_p (TREE_CODE (op2
))
11546 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11547 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11548 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11549 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11554 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11555 of fold_ternary on them. */
11556 gcc_unreachable ();
11558 case BIT_FIELD_REF
:
11559 if (TREE_CODE (arg0
) == VECTOR_CST
11560 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11561 || (TREE_CODE (type
) == VECTOR_TYPE
11562 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11564 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11565 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11566 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11567 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11570 && (idx
% width
) == 0
11571 && (n
% width
) == 0
11572 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11577 if (TREE_CODE (arg0
) == VECTOR_CST
)
11580 return VECTOR_CST_ELT (arg0
, idx
);
11582 tree
*vals
= XALLOCAVEC (tree
, n
);
11583 for (unsigned i
= 0; i
< n
; ++i
)
11584 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
11585 return build_vector (type
, vals
);
11590 /* On constants we can use native encode/interpret to constant
11591 fold (nearly) all BIT_FIELD_REFs. */
11592 if (CONSTANT_CLASS_P (arg0
)
11593 && can_native_interpret_type_p (type
)
11594 && BITS_PER_UNIT
== 8)
11596 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11597 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11598 /* Limit us to a reasonable amount of work. To relax the
11599 other limitations we need bit-shifting of the buffer
11600 and rounding up the size. */
11601 if (bitpos
% BITS_PER_UNIT
== 0
11602 && bitsize
% BITS_PER_UNIT
== 0
11603 && bitsize
<= MAX_BITSIZE_MODE_ANY_MODE
)
11605 unsigned char b
[MAX_BITSIZE_MODE_ANY_MODE
/ BITS_PER_UNIT
];
11606 unsigned HOST_WIDE_INT len
11607 = native_encode_expr (arg0
, b
, bitsize
/ BITS_PER_UNIT
,
11608 bitpos
/ BITS_PER_UNIT
);
11610 && len
* BITS_PER_UNIT
>= bitsize
)
11612 tree v
= native_interpret_expr (type
, b
,
11613 bitsize
/ BITS_PER_UNIT
);
11623 /* For integers we can decompose the FMA if possible. */
11624 if (TREE_CODE (arg0
) == INTEGER_CST
11625 && TREE_CODE (arg1
) == INTEGER_CST
)
11626 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11627 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11628 if (integer_zerop (arg2
))
11629 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11631 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11633 case VEC_PERM_EXPR
:
11634 if (TREE_CODE (arg2
) == VECTOR_CST
)
11636 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
11637 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
11638 unsigned char *sel2
= sel
+ nelts
;
11639 bool need_mask_canon
= false;
11640 bool need_mask_canon2
= false;
11641 bool all_in_vec0
= true;
11642 bool all_in_vec1
= true;
11643 bool maybe_identity
= true;
11644 bool single_arg
= (op0
== op1
);
11645 bool changed
= false;
11647 mask2
= 2 * nelts
- 1;
11648 mask
= single_arg
? (nelts
- 1) : mask2
;
11649 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
11650 for (i
= 0; i
< nelts
; i
++)
11652 tree val
= VECTOR_CST_ELT (arg2
, i
);
11653 if (TREE_CODE (val
) != INTEGER_CST
)
11656 /* Make sure that the perm value is in an acceptable
11659 need_mask_canon
|= wi::gtu_p (t
, mask
);
11660 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11661 sel
[i
] = t
.to_uhwi () & mask
;
11662 sel2
[i
] = t
.to_uhwi () & mask2
;
11664 if (sel
[i
] < nelts
)
11665 all_in_vec1
= false;
11667 all_in_vec0
= false;
11669 if ((sel
[i
] & (nelts
-1)) != i
)
11670 maybe_identity
= false;
11673 if (maybe_identity
)
11683 else if (all_in_vec1
)
11686 for (i
= 0; i
< nelts
; i
++)
11688 need_mask_canon
= true;
11691 if ((TREE_CODE (op0
) == VECTOR_CST
11692 || TREE_CODE (op0
) == CONSTRUCTOR
)
11693 && (TREE_CODE (op1
) == VECTOR_CST
11694 || TREE_CODE (op1
) == CONSTRUCTOR
))
11696 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11697 if (t
!= NULL_TREE
)
11701 if (op0
== op1
&& !single_arg
)
11704 /* Some targets are deficient and fail to expand a single
11705 argument permutation while still allowing an equivalent
11706 2-argument version. */
11707 if (need_mask_canon
&& arg2
== op2
11708 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
11709 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
11711 need_mask_canon
= need_mask_canon2
;
11715 if (need_mask_canon
&& arg2
== op2
)
11717 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
11718 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11719 for (i
= 0; i
< nelts
; i
++)
11720 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
11721 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11726 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11730 case BIT_INSERT_EXPR
:
11731 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11732 if (TREE_CODE (arg0
) == INTEGER_CST
11733 && TREE_CODE (arg1
) == INTEGER_CST
)
11735 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11736 unsigned bitsize
= TYPE_PRECISION (TREE_TYPE (arg1
));
11737 wide_int tem
= wi::bit_and (arg0
,
11738 wi::shifted_mask (bitpos
, bitsize
, true,
11739 TYPE_PRECISION (type
)));
11741 = wi::lshift (wi::zext (wi::to_wide (arg1
, TYPE_PRECISION (type
)),
11743 return wide_int_to_tree (type
, wi::bit_or (tem
, tem2
));
11745 else if (TREE_CODE (arg0
) == VECTOR_CST
11746 && CONSTANT_CLASS_P (arg1
)
11747 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0
)),
11750 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11751 unsigned HOST_WIDE_INT elsize
11752 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1
)));
11753 if (bitpos
% elsize
== 0)
11755 unsigned k
= bitpos
/ elsize
;
11756 if (operand_equal_p (VECTOR_CST_ELT (arg0
, k
), arg1
, 0))
11760 tree
*elts
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
11761 memcpy (elts
, VECTOR_CST_ELTS (arg0
),
11762 sizeof (tree
) * TYPE_VECTOR_SUBPARTS (type
));
11764 return build_vector (type
, elts
);
11772 } /* switch (code) */
11775 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11776 of an array (or vector). */
11779 get_array_ctor_element_at_index (tree ctor
, offset_int access_index
)
11781 tree index_type
= NULL_TREE
;
11782 offset_int low_bound
= 0;
11784 if (TREE_CODE (TREE_TYPE (ctor
)) == ARRAY_TYPE
)
11786 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ctor
));
11787 if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
11789 /* Static constructors for variably sized objects makes no sense. */
11790 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type
)) == INTEGER_CST
);
11791 index_type
= TREE_TYPE (TYPE_MIN_VALUE (domain_type
));
11792 low_bound
= wi::to_offset (TYPE_MIN_VALUE (domain_type
));
11797 access_index
= wi::ext (access_index
, TYPE_PRECISION (index_type
),
11798 TYPE_SIGN (index_type
));
11800 offset_int index
= low_bound
- 1;
11802 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11803 TYPE_SIGN (index_type
));
11805 offset_int max_index
;
11806 unsigned HOST_WIDE_INT cnt
;
11809 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), cnt
, cfield
, cval
)
11811 /* Array constructor might explicitly set index, or specify a range,
11812 or leave index NULL meaning that it is next index after previous
11816 if (TREE_CODE (cfield
) == INTEGER_CST
)
11817 max_index
= index
= wi::to_offset (cfield
);
11820 gcc_assert (TREE_CODE (cfield
) == RANGE_EXPR
);
11821 index
= wi::to_offset (TREE_OPERAND (cfield
, 0));
11822 max_index
= wi::to_offset (TREE_OPERAND (cfield
, 1));
11829 index
= wi::ext (index
, TYPE_PRECISION (index_type
),
11830 TYPE_SIGN (index_type
));
11834 /* Do we have match? */
11835 if (wi::cmpu (access_index
, index
) >= 0
11836 && wi::cmpu (access_index
, max_index
) <= 0)
11842 /* Perform constant folding and related simplification of EXPR.
11843 The related simplifications include x*1 => x, x*0 => 0, etc.,
11844 and application of the associative law.
11845 NOP_EXPR conversions may be removed freely (as long as we
11846 are careful not to change the type of the overall expression).
11847 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11848 but we can constant-fold them if they have constant operands. */
11850 #ifdef ENABLE_FOLD_CHECKING
11851 # define fold(x) fold_1 (x)
11852 static tree
fold_1 (tree
);
11858 const tree t
= expr
;
11859 enum tree_code code
= TREE_CODE (t
);
11860 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11862 location_t loc
= EXPR_LOCATION (expr
);
11864 /* Return right away if a constant. */
11865 if (kind
== tcc_constant
)
11868 /* CALL_EXPR-like objects with variable numbers of operands are
11869 treated specially. */
11870 if (kind
== tcc_vl_exp
)
11872 if (code
== CALL_EXPR
)
11874 tem
= fold_call_expr (loc
, expr
, false);
11875 return tem
? tem
: expr
;
11880 if (IS_EXPR_CODE_CLASS (kind
))
11882 tree type
= TREE_TYPE (t
);
11883 tree op0
, op1
, op2
;
11885 switch (TREE_CODE_LENGTH (code
))
11888 op0
= TREE_OPERAND (t
, 0);
11889 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11890 return tem
? tem
: expr
;
11892 op0
= TREE_OPERAND (t
, 0);
11893 op1
= TREE_OPERAND (t
, 1);
11894 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11895 return tem
? tem
: expr
;
11897 op0
= TREE_OPERAND (t
, 0);
11898 op1
= TREE_OPERAND (t
, 1);
11899 op2
= TREE_OPERAND (t
, 2);
11900 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11901 return tem
? tem
: expr
;
11911 tree op0
= TREE_OPERAND (t
, 0);
11912 tree op1
= TREE_OPERAND (t
, 1);
11914 if (TREE_CODE (op1
) == INTEGER_CST
11915 && TREE_CODE (op0
) == CONSTRUCTOR
11916 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11918 tree val
= get_array_ctor_element_at_index (op0
,
11919 wi::to_offset (op1
));
11927 /* Return a VECTOR_CST if possible. */
11930 tree type
= TREE_TYPE (t
);
11931 if (TREE_CODE (type
) != VECTOR_TYPE
)
11936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
11937 if (! CONSTANT_CLASS_P (val
))
11940 return build_vector_from_ctor (type
, CONSTRUCTOR_ELTS (t
));
11944 return fold (DECL_INITIAL (t
));
11948 } /* switch (code) */
11951 #ifdef ENABLE_FOLD_CHECKING
11954 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
11955 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
11956 static void fold_check_failed (const_tree
, const_tree
);
11957 void print_fold_checksum (const_tree
);
11959 /* When --enable-checking=fold, compute a digest of expr before
11960 and after actual fold call to see if fold did not accidentally
11961 change original expr. */
11967 struct md5_ctx ctx
;
11968 unsigned char checksum_before
[16], checksum_after
[16];
11969 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
11971 md5_init_ctx (&ctx
);
11972 fold_checksum_tree (expr
, &ctx
, &ht
);
11973 md5_finish_ctx (&ctx
, checksum_before
);
11976 ret
= fold_1 (expr
);
11978 md5_init_ctx (&ctx
);
11979 fold_checksum_tree (expr
, &ctx
, &ht
);
11980 md5_finish_ctx (&ctx
, checksum_after
);
11982 if (memcmp (checksum_before
, checksum_after
, 16))
11983 fold_check_failed (expr
, ret
);
11989 print_fold_checksum (const_tree expr
)
11991 struct md5_ctx ctx
;
11992 unsigned char checksum
[16], cnt
;
11993 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
11995 md5_init_ctx (&ctx
);
11996 fold_checksum_tree (expr
, &ctx
, &ht
);
11997 md5_finish_ctx (&ctx
, checksum
);
11998 for (cnt
= 0; cnt
< 16; ++cnt
)
11999 fprintf (stderr
, "%02x", checksum
[cnt
]);
12000 putc ('\n', stderr
);
12004 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12006 internal_error ("fold check: original tree changed by fold");
12010 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12011 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12013 const tree_node
**slot
;
12014 enum tree_code code
;
12015 union tree_node buf
;
12021 slot
= ht
->find_slot (expr
, INSERT
);
12025 code
= TREE_CODE (expr
);
12026 if (TREE_CODE_CLASS (code
) == tcc_declaration
12027 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12029 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12030 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12031 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12032 buf
.decl_with_vis
.symtab_node
= NULL
;
12033 expr
= (tree
) &buf
;
12035 else if (TREE_CODE_CLASS (code
) == tcc_type
12036 && (TYPE_POINTER_TO (expr
)
12037 || TYPE_REFERENCE_TO (expr
)
12038 || TYPE_CACHED_VALUES_P (expr
)
12039 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12040 || TYPE_NEXT_VARIANT (expr
)
12041 || TYPE_ALIAS_SET_KNOWN_P (expr
)))
12043 /* Allow these fields to be modified. */
12045 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12046 expr
= tmp
= (tree
) &buf
;
12047 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12048 TYPE_POINTER_TO (tmp
) = NULL
;
12049 TYPE_REFERENCE_TO (tmp
) = NULL
;
12050 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12051 TYPE_ALIAS_SET (tmp
) = -1;
12052 if (TYPE_CACHED_VALUES_P (tmp
))
12054 TYPE_CACHED_VALUES_P (tmp
) = 0;
12055 TYPE_CACHED_VALUES (tmp
) = NULL
;
12058 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12059 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12060 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12061 if (TREE_CODE_CLASS (code
) != tcc_type
12062 && TREE_CODE_CLASS (code
) != tcc_declaration
12063 && code
!= TREE_LIST
12064 && code
!= SSA_NAME
12065 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12066 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12067 switch (TREE_CODE_CLASS (code
))
12073 md5_process_bytes (TREE_STRING_POINTER (expr
),
12074 TREE_STRING_LENGTH (expr
), ctx
);
12077 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12078 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12081 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12082 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12088 case tcc_exceptional
:
12092 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12093 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12094 expr
= TREE_CHAIN (expr
);
12095 goto recursive_label
;
12098 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12099 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12105 case tcc_expression
:
12106 case tcc_reference
:
12107 case tcc_comparison
:
12110 case tcc_statement
:
12112 len
= TREE_OPERAND_LENGTH (expr
);
12113 for (i
= 0; i
< len
; ++i
)
12114 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12116 case tcc_declaration
:
12117 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12118 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12119 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12121 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12122 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12123 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12124 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12125 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12128 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12130 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12132 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12133 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12135 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12139 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12140 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12141 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12142 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12143 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12144 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12145 if (INTEGRAL_TYPE_P (expr
)
12146 || SCALAR_FLOAT_TYPE_P (expr
))
12148 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12149 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12151 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12152 if (TREE_CODE (expr
) == RECORD_TYPE
12153 || TREE_CODE (expr
) == UNION_TYPE
12154 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12155 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12156 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12163 /* Helper function for outputting the checksum of a tree T. When
12164 debugging with gdb, you can "define mynext" to be "next" followed
12165 by "call debug_fold_checksum (op0)", then just trace down till the
12168 DEBUG_FUNCTION
void
12169 debug_fold_checksum (const_tree t
)
12172 unsigned char checksum
[16];
12173 struct md5_ctx ctx
;
12174 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12176 md5_init_ctx (&ctx
);
12177 fold_checksum_tree (t
, &ctx
, &ht
);
12178 md5_finish_ctx (&ctx
, checksum
);
12181 for (i
= 0; i
< 16; i
++)
12182 fprintf (stderr
, "%d ", checksum
[i
]);
12184 fprintf (stderr
, "\n");
12189 /* Fold a unary tree expression with code CODE of type TYPE with an
12190 operand OP0. LOC is the location of the resulting expression.
12191 Return a folded expression if successful. Otherwise, return a tree
12192 expression with code CODE of type TYPE with an operand OP0. */
12195 fold_build1_stat_loc (location_t loc
,
12196 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12199 #ifdef ENABLE_FOLD_CHECKING
12200 unsigned char checksum_before
[16], checksum_after
[16];
12201 struct md5_ctx ctx
;
12202 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12204 md5_init_ctx (&ctx
);
12205 fold_checksum_tree (op0
, &ctx
, &ht
);
12206 md5_finish_ctx (&ctx
, checksum_before
);
12210 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12212 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12214 #ifdef ENABLE_FOLD_CHECKING
12215 md5_init_ctx (&ctx
);
12216 fold_checksum_tree (op0
, &ctx
, &ht
);
12217 md5_finish_ctx (&ctx
, checksum_after
);
12219 if (memcmp (checksum_before
, checksum_after
, 16))
12220 fold_check_failed (op0
, tem
);
12225 /* Fold a binary tree expression with code CODE of type TYPE with
12226 operands OP0 and OP1. LOC is the location of the resulting
12227 expression. Return a folded expression if successful. Otherwise,
12228 return a tree expression with code CODE of type TYPE with operands
12232 fold_build2_stat_loc (location_t loc
,
12233 enum tree_code code
, tree type
, tree op0
, tree op1
12237 #ifdef ENABLE_FOLD_CHECKING
12238 unsigned char checksum_before_op0
[16],
12239 checksum_before_op1
[16],
12240 checksum_after_op0
[16],
12241 checksum_after_op1
[16];
12242 struct md5_ctx ctx
;
12243 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12245 md5_init_ctx (&ctx
);
12246 fold_checksum_tree (op0
, &ctx
, &ht
);
12247 md5_finish_ctx (&ctx
, checksum_before_op0
);
12250 md5_init_ctx (&ctx
);
12251 fold_checksum_tree (op1
, &ctx
, &ht
);
12252 md5_finish_ctx (&ctx
, checksum_before_op1
);
12256 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12258 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12260 #ifdef ENABLE_FOLD_CHECKING
12261 md5_init_ctx (&ctx
);
12262 fold_checksum_tree (op0
, &ctx
, &ht
);
12263 md5_finish_ctx (&ctx
, checksum_after_op0
);
12266 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12267 fold_check_failed (op0
, tem
);
12269 md5_init_ctx (&ctx
);
12270 fold_checksum_tree (op1
, &ctx
, &ht
);
12271 md5_finish_ctx (&ctx
, checksum_after_op1
);
12273 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12274 fold_check_failed (op1
, tem
);
12279 /* Fold a ternary tree expression with code CODE of type TYPE with
12280 operands OP0, OP1, and OP2. Return a folded expression if
12281 successful. Otherwise, return a tree expression with code CODE of
12282 type TYPE with operands OP0, OP1, and OP2. */
12285 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
12286 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12289 #ifdef ENABLE_FOLD_CHECKING
12290 unsigned char checksum_before_op0
[16],
12291 checksum_before_op1
[16],
12292 checksum_before_op2
[16],
12293 checksum_after_op0
[16],
12294 checksum_after_op1
[16],
12295 checksum_after_op2
[16];
12296 struct md5_ctx ctx
;
12297 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12299 md5_init_ctx (&ctx
);
12300 fold_checksum_tree (op0
, &ctx
, &ht
);
12301 md5_finish_ctx (&ctx
, checksum_before_op0
);
12304 md5_init_ctx (&ctx
);
12305 fold_checksum_tree (op1
, &ctx
, &ht
);
12306 md5_finish_ctx (&ctx
, checksum_before_op1
);
12309 md5_init_ctx (&ctx
);
12310 fold_checksum_tree (op2
, &ctx
, &ht
);
12311 md5_finish_ctx (&ctx
, checksum_before_op2
);
12315 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12316 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12318 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12320 #ifdef ENABLE_FOLD_CHECKING
12321 md5_init_ctx (&ctx
);
12322 fold_checksum_tree (op0
, &ctx
, &ht
);
12323 md5_finish_ctx (&ctx
, checksum_after_op0
);
12326 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12327 fold_check_failed (op0
, tem
);
12329 md5_init_ctx (&ctx
);
12330 fold_checksum_tree (op1
, &ctx
, &ht
);
12331 md5_finish_ctx (&ctx
, checksum_after_op1
);
12334 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12335 fold_check_failed (op1
, tem
);
12337 md5_init_ctx (&ctx
);
12338 fold_checksum_tree (op2
, &ctx
, &ht
);
12339 md5_finish_ctx (&ctx
, checksum_after_op2
);
12341 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12342 fold_check_failed (op2
, tem
);
12347 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12348 arguments in ARGARRAY, and a null static chain.
12349 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12350 of type TYPE from the given operands as constructed by build_call_array. */
12353 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12354 int nargs
, tree
*argarray
)
12357 #ifdef ENABLE_FOLD_CHECKING
12358 unsigned char checksum_before_fn
[16],
12359 checksum_before_arglist
[16],
12360 checksum_after_fn
[16],
12361 checksum_after_arglist
[16];
12362 struct md5_ctx ctx
;
12363 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12366 md5_init_ctx (&ctx
);
12367 fold_checksum_tree (fn
, &ctx
, &ht
);
12368 md5_finish_ctx (&ctx
, checksum_before_fn
);
12371 md5_init_ctx (&ctx
);
12372 for (i
= 0; i
< nargs
; i
++)
12373 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12374 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12378 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12380 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12382 #ifdef ENABLE_FOLD_CHECKING
12383 md5_init_ctx (&ctx
);
12384 fold_checksum_tree (fn
, &ctx
, &ht
);
12385 md5_finish_ctx (&ctx
, checksum_after_fn
);
12388 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12389 fold_check_failed (fn
, tem
);
12391 md5_init_ctx (&ctx
);
12392 for (i
= 0; i
< nargs
; i
++)
12393 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12394 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12396 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12397 fold_check_failed (NULL_TREE
, tem
);
12402 /* Perform constant folding and related simplification of initializer
12403 expression EXPR. These behave identically to "fold_buildN" but ignore
12404 potential run-time traps and exceptions that fold must preserve. */
12406 #define START_FOLD_INIT \
12407 int saved_signaling_nans = flag_signaling_nans;\
12408 int saved_trapping_math = flag_trapping_math;\
12409 int saved_rounding_math = flag_rounding_math;\
12410 int saved_trapv = flag_trapv;\
12411 int saved_folding_initializer = folding_initializer;\
12412 flag_signaling_nans = 0;\
12413 flag_trapping_math = 0;\
12414 flag_rounding_math = 0;\
12416 folding_initializer = 1;
12418 #define END_FOLD_INIT \
12419 flag_signaling_nans = saved_signaling_nans;\
12420 flag_trapping_math = saved_trapping_math;\
12421 flag_rounding_math = saved_rounding_math;\
12422 flag_trapv = saved_trapv;\
12423 folding_initializer = saved_folding_initializer;
12426 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12427 tree type
, tree op
)
12432 result
= fold_build1_loc (loc
, code
, type
, op
);
12439 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12440 tree type
, tree op0
, tree op1
)
12445 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12452 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12453 int nargs
, tree
*argarray
)
12458 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12464 #undef START_FOLD_INIT
12465 #undef END_FOLD_INIT
12467 /* Determine if first argument is a multiple of second argument. Return 0 if
12468 it is not, or we cannot easily determined it to be.
12470 An example of the sort of thing we care about (at this point; this routine
12471 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12472 fold cases do now) is discovering that
12474 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12480 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12482 This code also handles discovering that
12484 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12486 is a multiple of 8 so we don't have to worry about dealing with a
12487 possible remainder.
12489 Note that we *look* inside a SAVE_EXPR only to determine how it was
12490 calculated; it is not safe for fold to do much of anything else with the
12491 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12492 at run time. For example, the latter example above *cannot* be implemented
12493 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12494 evaluation time of the original SAVE_EXPR is not necessarily the same at
12495 the time the new expression is evaluated. The only optimization of this
12496 sort that would be valid is changing
12498 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12502 SAVE_EXPR (I) * SAVE_EXPR (J)
12504 (where the same SAVE_EXPR (J) is used in the original and the
12505 transformed version). */
12508 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12513 if (operand_equal_p (top
, bottom
, 0))
12516 if (TREE_CODE (type
) != INTEGER_TYPE
)
12519 switch (TREE_CODE (top
))
12522 /* Bitwise and provides a power of two multiple. If the mask is
12523 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12524 if (!integer_pow2p (bottom
))
12529 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12530 || multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12533 /* It is impossible to prove if op0 - op1 is multiple of bottom
12534 precisely, so be conservative here checking if both op0 and op1
12535 are multiple of bottom. Note we check the second operand first
12536 since it's usually simpler. */
12537 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12538 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12541 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12542 as op0 - 3 if the expression has unsigned type. For example,
12543 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12544 op1
= TREE_OPERAND (top
, 1);
12545 if (TYPE_UNSIGNED (type
)
12546 && TREE_CODE (op1
) == INTEGER_CST
&& tree_int_cst_sign_bit (op1
))
12547 op1
= fold_build1 (NEGATE_EXPR
, type
, op1
);
12548 return (multiple_of_p (type
, op1
, bottom
)
12549 && multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
));
12552 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12554 op1
= TREE_OPERAND (top
, 1);
12555 /* const_binop may not detect overflow correctly,
12556 so check for it explicitly here. */
12557 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12558 && 0 != (t1
= fold_convert (type
,
12559 const_binop (LSHIFT_EXPR
,
12562 && !TREE_OVERFLOW (t1
))
12563 return multiple_of_p (type
, t1
, bottom
);
12568 /* Can't handle conversions from non-integral or wider integral type. */
12569 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12570 || (TYPE_PRECISION (type
)
12571 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12577 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12580 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12581 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12584 if (TREE_CODE (bottom
) != INTEGER_CST
12585 || integer_zerop (bottom
)
12586 || (TYPE_UNSIGNED (type
)
12587 && (tree_int_cst_sgn (top
) < 0
12588 || tree_int_cst_sgn (bottom
) < 0)))
12590 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12594 if (TREE_CODE (bottom
) == INTEGER_CST
12595 && (stmt
= SSA_NAME_DEF_STMT (top
)) != NULL
12596 && gimple_code (stmt
) == GIMPLE_ASSIGN
)
12598 enum tree_code code
= gimple_assign_rhs_code (stmt
);
12600 /* Check for special cases to see if top is defined as multiple
12603 top = (X & ~(bottom - 1) ; bottom is power of 2
12609 if (code
== BIT_AND_EXPR
12610 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12611 && TREE_CODE (op2
) == INTEGER_CST
12612 && integer_pow2p (bottom
)
12613 && wi::multiple_of_p (wi::to_widest (op2
),
12614 wi::to_widest (bottom
), UNSIGNED
))
12617 op1
= gimple_assign_rhs1 (stmt
);
12618 if (code
== MINUS_EXPR
12619 && (op2
= gimple_assign_rhs2 (stmt
)) != NULL_TREE
12620 && TREE_CODE (op2
) == SSA_NAME
12621 && (stmt
= SSA_NAME_DEF_STMT (op2
)) != NULL
12622 && gimple_code (stmt
) == GIMPLE_ASSIGN
12623 && (code
= gimple_assign_rhs_code (stmt
)) == TRUNC_MOD_EXPR
12624 && operand_equal_p (op1
, gimple_assign_rhs1 (stmt
), 0)
12625 && operand_equal_p (bottom
, gimple_assign_rhs2 (stmt
), 0))
12636 #define tree_expr_nonnegative_warnv_p(X, Y) \
12637 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12639 #define RECURSE(X) \
12640 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12642 /* Return true if CODE or TYPE is known to be non-negative. */
12645 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12647 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12648 && truth_value_p (code
))
12649 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12650 have a signed:1 type (where the value is -1 and 0). */
12655 /* Return true if (CODE OP0) is known to be non-negative. If the return
12656 value is based on the assumption that signed overflow is undefined,
12657 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12658 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12661 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12662 bool *strict_overflow_p
, int depth
)
12664 if (TYPE_UNSIGNED (type
))
12670 /* We can't return 1 if flag_wrapv is set because
12671 ABS_EXPR<INT_MIN> = INT_MIN. */
12672 if (!ANY_INTEGRAL_TYPE_P (type
))
12674 if (TYPE_OVERFLOW_UNDEFINED (type
))
12676 *strict_overflow_p
= true;
12681 case NON_LVALUE_EXPR
:
12683 case FIX_TRUNC_EXPR
:
12684 return RECURSE (op0
);
12688 tree inner_type
= TREE_TYPE (op0
);
12689 tree outer_type
= type
;
12691 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12693 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12694 return RECURSE (op0
);
12695 if (INTEGRAL_TYPE_P (inner_type
))
12697 if (TYPE_UNSIGNED (inner_type
))
12699 return RECURSE (op0
);
12702 else if (INTEGRAL_TYPE_P (outer_type
))
12704 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12705 return RECURSE (op0
);
12706 if (INTEGRAL_TYPE_P (inner_type
))
12707 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12708 && TYPE_UNSIGNED (inner_type
);
12714 return tree_simple_nonnegative_warnv_p (code
, type
);
12717 /* We don't know sign of `t', so be conservative and return false. */
12721 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12722 value is based on the assumption that signed overflow is undefined,
12723 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12724 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12727 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12728 tree op1
, bool *strict_overflow_p
,
12731 if (TYPE_UNSIGNED (type
))
12736 case POINTER_PLUS_EXPR
:
12738 if (FLOAT_TYPE_P (type
))
12739 return RECURSE (op0
) && RECURSE (op1
);
12741 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12742 both unsigned and at least 2 bits shorter than the result. */
12743 if (TREE_CODE (type
) == INTEGER_TYPE
12744 && TREE_CODE (op0
) == NOP_EXPR
12745 && TREE_CODE (op1
) == NOP_EXPR
)
12747 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12748 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12749 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12750 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12752 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12753 TYPE_PRECISION (inner2
)) + 1;
12754 return prec
< TYPE_PRECISION (type
);
12760 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12762 /* x * x is always non-negative for floating point x
12763 or without overflow. */
12764 if (operand_equal_p (op0
, op1
, 0)
12765 || (RECURSE (op0
) && RECURSE (op1
)))
12767 if (ANY_INTEGRAL_TYPE_P (type
)
12768 && TYPE_OVERFLOW_UNDEFINED (type
))
12769 *strict_overflow_p
= true;
12774 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12775 both unsigned and their total bits is shorter than the result. */
12776 if (TREE_CODE (type
) == INTEGER_TYPE
12777 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12778 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12780 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12781 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12783 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12784 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12787 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12788 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12790 if (TREE_CODE (op0
) == INTEGER_CST
)
12791 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12793 if (TREE_CODE (op1
) == INTEGER_CST
)
12794 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12796 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12797 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12799 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12800 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12801 : TYPE_PRECISION (inner0
);
12803 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12804 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12805 : TYPE_PRECISION (inner1
);
12807 return precision0
+ precision1
< TYPE_PRECISION (type
);
12814 return RECURSE (op0
) || RECURSE (op1
);
12820 case TRUNC_DIV_EXPR
:
12821 case CEIL_DIV_EXPR
:
12822 case FLOOR_DIV_EXPR
:
12823 case ROUND_DIV_EXPR
:
12824 return RECURSE (op0
) && RECURSE (op1
);
12826 case TRUNC_MOD_EXPR
:
12827 return RECURSE (op0
);
12829 case FLOOR_MOD_EXPR
:
12830 return RECURSE (op1
);
12832 case CEIL_MOD_EXPR
:
12833 case ROUND_MOD_EXPR
:
12835 return tree_simple_nonnegative_warnv_p (code
, type
);
12838 /* We don't know sign of `t', so be conservative and return false. */
12842 /* Return true if T is known to be non-negative. If the return
12843 value is based on the assumption that signed overflow is undefined,
12844 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12845 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12848 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12850 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12853 switch (TREE_CODE (t
))
12856 return tree_int_cst_sgn (t
) >= 0;
12859 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12862 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12865 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12868 /* Limit the depth of recursion to avoid quadratic behavior.
12869 This is expected to catch almost all occurrences in practice.
12870 If this code misses important cases that unbounded recursion
12871 would not, passes that need this information could be revised
12872 to provide it through dataflow propagation. */
12873 return (!name_registered_for_update_p (t
)
12874 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12875 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12876 strict_overflow_p
, depth
));
12879 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12883 /* Return true if T is known to be non-negative. If the return
12884 value is based on the assumption that signed overflow is undefined,
12885 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12886 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12889 tree_call_nonnegative_warnv_p (tree type
, combined_fn fn
, tree arg0
, tree arg1
,
12890 bool *strict_overflow_p
, int depth
)
12911 case CFN_BUILT_IN_BSWAP32
:
12912 case CFN_BUILT_IN_BSWAP64
:
12917 /* sqrt(-0.0) is -0.0. */
12918 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
12920 return RECURSE (arg0
);
12946 CASE_CFN_NEARBYINT
:
12953 CASE_CFN_SIGNIFICAND
:
12957 /* True if the 1st argument is nonnegative. */
12958 return RECURSE (arg0
);
12961 /* True if the 1st OR 2nd arguments are nonnegative. */
12962 return RECURSE (arg0
) || RECURSE (arg1
);
12965 /* True if the 1st AND 2nd arguments are nonnegative. */
12966 return RECURSE (arg0
) && RECURSE (arg1
);
12969 /* True if the 2nd argument is nonnegative. */
12970 return RECURSE (arg1
);
12973 /* True if the 1st argument is nonnegative or the second
12974 argument is an even integer. */
12975 if (TREE_CODE (arg1
) == INTEGER_CST
12976 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
12978 return RECURSE (arg0
);
12981 /* True if the 1st argument is nonnegative or the second
12982 argument is an even integer valued real. */
12983 if (TREE_CODE (arg1
) == REAL_CST
)
12988 c
= TREE_REAL_CST (arg1
);
12989 n
= real_to_integer (&c
);
12992 REAL_VALUE_TYPE cint
;
12993 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
12994 if (real_identical (&c
, &cint
))
12998 return RECURSE (arg0
);
13003 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
13006 /* Return true if T is known to be non-negative. If the return
13007 value is based on the assumption that signed overflow is undefined,
13008 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13009 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13012 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13014 enum tree_code code
= TREE_CODE (t
);
13015 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13022 tree temp
= TARGET_EXPR_SLOT (t
);
13023 t
= TARGET_EXPR_INITIAL (t
);
13025 /* If the initializer is non-void, then it's a normal expression
13026 that will be assigned to the slot. */
13027 if (!VOID_TYPE_P (t
))
13028 return RECURSE (t
);
13030 /* Otherwise, the initializer sets the slot in some way. One common
13031 way is an assignment statement at the end of the initializer. */
13034 if (TREE_CODE (t
) == BIND_EXPR
)
13035 t
= expr_last (BIND_EXPR_BODY (t
));
13036 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13037 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13038 t
= expr_last (TREE_OPERAND (t
, 0));
13039 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13044 if (TREE_CODE (t
) == MODIFY_EXPR
13045 && TREE_OPERAND (t
, 0) == temp
)
13046 return RECURSE (TREE_OPERAND (t
, 1));
13053 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13054 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13056 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13057 get_call_combined_fn (t
),
13060 strict_overflow_p
, depth
);
13062 case COMPOUND_EXPR
:
13064 return RECURSE (TREE_OPERAND (t
, 1));
13067 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13070 return RECURSE (TREE_OPERAND (t
, 0));
13073 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13078 #undef tree_expr_nonnegative_warnv_p
13080 /* Return true if T is known to be non-negative. If the return
13081 value is based on the assumption that signed overflow is undefined,
13082 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13083 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13086 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13088 enum tree_code code
;
13089 if (t
== error_mark_node
)
13092 code
= TREE_CODE (t
);
13093 switch (TREE_CODE_CLASS (code
))
13096 case tcc_comparison
:
13097 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13099 TREE_OPERAND (t
, 0),
13100 TREE_OPERAND (t
, 1),
13101 strict_overflow_p
, depth
);
13104 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13106 TREE_OPERAND (t
, 0),
13107 strict_overflow_p
, depth
);
13110 case tcc_declaration
:
13111 case tcc_reference
:
13112 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13120 case TRUTH_AND_EXPR
:
13121 case TRUTH_OR_EXPR
:
13122 case TRUTH_XOR_EXPR
:
13123 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13125 TREE_OPERAND (t
, 0),
13126 TREE_OPERAND (t
, 1),
13127 strict_overflow_p
, depth
);
13128 case TRUTH_NOT_EXPR
:
13129 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13131 TREE_OPERAND (t
, 0),
13132 strict_overflow_p
, depth
);
13139 case WITH_SIZE_EXPR
:
13141 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13144 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13148 /* Return true if `t' is known to be non-negative. Handle warnings
13149 about undefined signed overflow. */
13152 tree_expr_nonnegative_p (tree t
)
13154 bool ret
, strict_overflow_p
;
13156 strict_overflow_p
= false;
13157 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13158 if (strict_overflow_p
)
13159 fold_overflow_warning (("assuming signed overflow does not occur when "
13160 "determining that expression is always "
13162 WARN_STRICT_OVERFLOW_MISC
);
13167 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13168 For floating point we further ensure that T is not denormal.
13169 Similar logic is present in nonzero_address in rtlanal.h.
13171 If the return value is based on the assumption that signed overflow
13172 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13173 change *STRICT_OVERFLOW_P. */
13176 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13177 bool *strict_overflow_p
)
13182 return tree_expr_nonzero_warnv_p (op0
,
13183 strict_overflow_p
);
13187 tree inner_type
= TREE_TYPE (op0
);
13188 tree outer_type
= type
;
13190 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13191 && tree_expr_nonzero_warnv_p (op0
,
13192 strict_overflow_p
));
13196 case NON_LVALUE_EXPR
:
13197 return tree_expr_nonzero_warnv_p (op0
,
13198 strict_overflow_p
);
13207 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13208 For floating point we further ensure that T is not denormal.
13209 Similar logic is present in nonzero_address in rtlanal.h.
13211 If the return value is based on the assumption that signed overflow
13212 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13213 change *STRICT_OVERFLOW_P. */
13216 tree_binary_nonzero_warnv_p (enum tree_code code
,
13219 tree op1
, bool *strict_overflow_p
)
13221 bool sub_strict_overflow_p
;
13224 case POINTER_PLUS_EXPR
:
13226 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13228 /* With the presence of negative values it is hard
13229 to say something. */
13230 sub_strict_overflow_p
= false;
13231 if (!tree_expr_nonnegative_warnv_p (op0
,
13232 &sub_strict_overflow_p
)
13233 || !tree_expr_nonnegative_warnv_p (op1
,
13234 &sub_strict_overflow_p
))
13236 /* One of operands must be positive and the other non-negative. */
13237 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13238 overflows, on a twos-complement machine the sum of two
13239 nonnegative numbers can never be zero. */
13240 return (tree_expr_nonzero_warnv_p (op0
,
13242 || tree_expr_nonzero_warnv_p (op1
,
13243 strict_overflow_p
));
13248 if (TYPE_OVERFLOW_UNDEFINED (type
))
13250 if (tree_expr_nonzero_warnv_p (op0
,
13252 && tree_expr_nonzero_warnv_p (op1
,
13253 strict_overflow_p
))
13255 *strict_overflow_p
= true;
13262 sub_strict_overflow_p
= false;
13263 if (tree_expr_nonzero_warnv_p (op0
,
13264 &sub_strict_overflow_p
)
13265 && tree_expr_nonzero_warnv_p (op1
,
13266 &sub_strict_overflow_p
))
13268 if (sub_strict_overflow_p
)
13269 *strict_overflow_p
= true;
13274 sub_strict_overflow_p
= false;
13275 if (tree_expr_nonzero_warnv_p (op0
,
13276 &sub_strict_overflow_p
))
13278 if (sub_strict_overflow_p
)
13279 *strict_overflow_p
= true;
13281 /* When both operands are nonzero, then MAX must be too. */
13282 if (tree_expr_nonzero_warnv_p (op1
,
13283 strict_overflow_p
))
13286 /* MAX where operand 0 is positive is positive. */
13287 return tree_expr_nonnegative_warnv_p (op0
,
13288 strict_overflow_p
);
13290 /* MAX where operand 1 is positive is positive. */
13291 else if (tree_expr_nonzero_warnv_p (op1
,
13292 &sub_strict_overflow_p
)
13293 && tree_expr_nonnegative_warnv_p (op1
,
13294 &sub_strict_overflow_p
))
13296 if (sub_strict_overflow_p
)
13297 *strict_overflow_p
= true;
13303 return (tree_expr_nonzero_warnv_p (op1
,
13305 || tree_expr_nonzero_warnv_p (op0
,
13306 strict_overflow_p
));
13315 /* Return true when T is an address and is known to be nonzero.
13316 For floating point we further ensure that T is not denormal.
13317 Similar logic is present in nonzero_address in rtlanal.h.
13319 If the return value is based on the assumption that signed overflow
13320 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13321 change *STRICT_OVERFLOW_P. */
13324 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13326 bool sub_strict_overflow_p
;
13327 switch (TREE_CODE (t
))
13330 return !integer_zerop (t
);
13334 tree base
= TREE_OPERAND (t
, 0);
13336 if (!DECL_P (base
))
13337 base
= get_base_address (base
);
13339 if (base
&& TREE_CODE (base
) == TARGET_EXPR
)
13340 base
= TARGET_EXPR_SLOT (base
);
13345 /* For objects in symbol table check if we know they are non-zero.
13346 Don't do anything for variables and functions before symtab is built;
13347 it is quite possible that they will be declared weak later. */
13348 int nonzero_addr
= maybe_nonzero_address (base
);
13349 if (nonzero_addr
>= 0)
13350 return nonzero_addr
;
13352 /* Constants are never weak. */
13353 if (CONSTANT_CLASS_P (base
))
13360 sub_strict_overflow_p
= false;
13361 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13362 &sub_strict_overflow_p
)
13363 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13364 &sub_strict_overflow_p
))
13366 if (sub_strict_overflow_p
)
13367 *strict_overflow_p
= true;
13378 #define integer_valued_real_p(X) \
13379 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13381 #define RECURSE(X) \
13382 ((integer_valued_real_p) (X, depth + 1))
13384 /* Return true if the floating point result of (CODE OP0) has an
13385 integer value. We also allow +Inf, -Inf and NaN to be considered
13386 integer values. Return false for signaling NaN.
13388 DEPTH is the current nesting depth of the query. */
13391 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13399 return RECURSE (op0
);
13403 tree type
= TREE_TYPE (op0
);
13404 if (TREE_CODE (type
) == INTEGER_TYPE
)
13406 if (TREE_CODE (type
) == REAL_TYPE
)
13407 return RECURSE (op0
);
13417 /* Return true if the floating point result of (CODE OP0 OP1) has an
13418 integer value. We also allow +Inf, -Inf and NaN to be considered
13419 integer values. Return false for signaling NaN.
13421 DEPTH is the current nesting depth of the query. */
13424 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13433 return RECURSE (op0
) && RECURSE (op1
);
13441 /* Return true if the floating point result of calling FNDECL with arguments
13442 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13443 considered integer values. Return false for signaling NaN. If FNDECL
13444 takes fewer than 2 arguments, the remaining ARGn are null.
13446 DEPTH is the current nesting depth of the query. */
13449 integer_valued_real_call_p (combined_fn fn
, tree arg0
, tree arg1
, int depth
)
13455 CASE_CFN_NEARBYINT
:
13463 return RECURSE (arg0
) && RECURSE (arg1
);
13471 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13472 has an integer value. We also allow +Inf, -Inf and NaN to be
13473 considered integer values. Return false for signaling NaN.
13475 DEPTH is the current nesting depth of the query. */
13478 integer_valued_real_single_p (tree t
, int depth
)
13480 switch (TREE_CODE (t
))
13483 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13486 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13489 /* Limit the depth of recursion to avoid quadratic behavior.
13490 This is expected to catch almost all occurrences in practice.
13491 If this code misses important cases that unbounded recursion
13492 would not, passes that need this information could be revised
13493 to provide it through dataflow propagation. */
13494 return (!name_registered_for_update_p (t
)
13495 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13496 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13505 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13506 has an integer value. We also allow +Inf, -Inf and NaN to be
13507 considered integer values. Return false for signaling NaN.
13509 DEPTH is the current nesting depth of the query. */
13512 integer_valued_real_invalid_p (tree t
, int depth
)
13514 switch (TREE_CODE (t
))
13516 case COMPOUND_EXPR
:
13519 return RECURSE (TREE_OPERAND (t
, 1));
13522 return RECURSE (TREE_OPERAND (t
, 0));
13531 #undef integer_valued_real_p
13533 /* Return true if the floating point expression T has an integer value.
13534 We also allow +Inf, -Inf and NaN to be considered integer values.
13535 Return false for signaling NaN.
13537 DEPTH is the current nesting depth of the query. */
13540 integer_valued_real_p (tree t
, int depth
)
13542 if (t
== error_mark_node
)
13545 tree_code code
= TREE_CODE (t
);
13546 switch (TREE_CODE_CLASS (code
))
13549 case tcc_comparison
:
13550 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13551 TREE_OPERAND (t
, 1), depth
);
13554 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13557 case tcc_declaration
:
13558 case tcc_reference
:
13559 return integer_valued_real_single_p (t
, depth
);
13569 return integer_valued_real_single_p (t
, depth
);
13573 tree arg0
= (call_expr_nargs (t
) > 0
13574 ? CALL_EXPR_ARG (t
, 0)
13576 tree arg1
= (call_expr_nargs (t
) > 1
13577 ? CALL_EXPR_ARG (t
, 1)
13579 return integer_valued_real_call_p (get_call_combined_fn (t
),
13580 arg0
, arg1
, depth
);
13584 return integer_valued_real_invalid_p (t
, depth
);
13588 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13589 attempt to fold the expression to a constant without modifying TYPE,
13592 If the expression could be simplified to a constant, then return
13593 the constant. If the expression would not be simplified to a
13594 constant, then return NULL_TREE. */
13597 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13599 tree tem
= fold_binary (code
, type
, op0
, op1
);
13600 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13603 /* Given the components of a unary expression CODE, TYPE and OP0,
13604 attempt to fold the expression to a constant without modifying
13607 If the expression could be simplified to a constant, then return
13608 the constant. If the expression would not be simplified to a
13609 constant, then return NULL_TREE. */
13612 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13614 tree tem
= fold_unary (code
, type
, op0
);
13615 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13618 /* If EXP represents referencing an element in a constant string
13619 (either via pointer arithmetic or array indexing), return the
13620 tree representing the value accessed, otherwise return NULL. */
13623 fold_read_from_constant_string (tree exp
)
13625 if ((TREE_CODE (exp
) == INDIRECT_REF
13626 || TREE_CODE (exp
) == ARRAY_REF
)
13627 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13629 tree exp1
= TREE_OPERAND (exp
, 0);
13632 location_t loc
= EXPR_LOCATION (exp
);
13634 if (TREE_CODE (exp
) == INDIRECT_REF
)
13635 string
= string_constant (exp1
, &index
);
13638 tree low_bound
= array_ref_low_bound (exp
);
13639 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13641 /* Optimize the special-case of a zero lower bound.
13643 We convert the low_bound to sizetype to avoid some problems
13644 with constant folding. (E.g. suppose the lower bound is 1,
13645 and its mode is QI. Without the conversion,l (ARRAY
13646 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13647 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13648 if (! integer_zerop (low_bound
))
13649 index
= size_diffop_loc (loc
, index
,
13650 fold_convert_loc (loc
, sizetype
, low_bound
));
13656 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13657 && TREE_CODE (string
) == STRING_CST
13658 && TREE_CODE (index
) == INTEGER_CST
13659 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13660 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
13662 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
13663 return build_int_cst_type (TREE_TYPE (exp
),
13664 (TREE_STRING_POINTER (string
)
13665 [TREE_INT_CST_LOW (index
)]));
13670 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13671 an integer constant, real, or fixed-point constant.
13673 TYPE is the type of the result. */
13676 fold_negate_const (tree arg0
, tree type
)
13678 tree t
= NULL_TREE
;
13680 switch (TREE_CODE (arg0
))
13685 wide_int val
= wi::neg (arg0
, &overflow
);
13686 t
= force_fit_type (type
, val
, 1,
13687 (overflow
| TREE_OVERFLOW (arg0
))
13688 && !TYPE_UNSIGNED (type
));
13693 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13698 FIXED_VALUE_TYPE f
;
13699 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13700 &(TREE_FIXED_CST (arg0
)), NULL
,
13701 TYPE_SATURATING (type
));
13702 t
= build_fixed (type
, f
);
13703 /* Propagate overflow flags. */
13704 if (overflow_p
| TREE_OVERFLOW (arg0
))
13705 TREE_OVERFLOW (t
) = 1;
13710 gcc_unreachable ();
13716 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13717 an integer constant or real constant.
13719 TYPE is the type of the result. */
13722 fold_abs_const (tree arg0
, tree type
)
13724 tree t
= NULL_TREE
;
13726 switch (TREE_CODE (arg0
))
13730 /* If the value is unsigned or non-negative, then the absolute value
13731 is the same as the ordinary value. */
13732 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13735 /* If the value is negative, then the absolute value is
13740 wide_int val
= wi::neg (arg0
, &overflow
);
13741 t
= force_fit_type (type
, val
, -1,
13742 overflow
| TREE_OVERFLOW (arg0
));
13748 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13749 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13755 gcc_unreachable ();
13761 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13762 constant. TYPE is the type of the result. */
13765 fold_not_const (const_tree arg0
, tree type
)
13767 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13769 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13772 /* Given CODE, a relational operator, the target type, TYPE and two
13773 constant operands OP0 and OP1, return the result of the
13774 relational operation. If the result is not a compile time
13775 constant, then return NULL_TREE. */
13778 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13780 int result
, invert
;
13782 /* From here on, the only cases we handle are when the result is
13783 known to be a constant. */
13785 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13787 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13788 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13790 /* Handle the cases where either operand is a NaN. */
13791 if (real_isnan (c0
) || real_isnan (c1
))
13801 case UNORDERED_EXPR
:
13815 if (flag_trapping_math
)
13821 gcc_unreachable ();
13824 return constant_boolean_node (result
, type
);
13827 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13830 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13832 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13833 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13834 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13837 /* Handle equality/inequality of complex constants. */
13838 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13840 tree rcond
= fold_relational_const (code
, type
,
13841 TREE_REALPART (op0
),
13842 TREE_REALPART (op1
));
13843 tree icond
= fold_relational_const (code
, type
,
13844 TREE_IMAGPART (op0
),
13845 TREE_IMAGPART (op1
));
13846 if (code
== EQ_EXPR
)
13847 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13848 else if (code
== NE_EXPR
)
13849 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13854 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13856 if (!VECTOR_TYPE_P (type
))
13858 /* Have vector comparison with scalar boolean result. */
13859 gcc_assert ((code
== EQ_EXPR
|| code
== NE_EXPR
)
13860 && VECTOR_CST_NELTS (op0
) == VECTOR_CST_NELTS (op1
));
13861 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (op0
); i
++)
13863 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13864 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13865 tree tmp
= fold_relational_const (code
, type
, elem0
, elem1
);
13866 if (tmp
== NULL_TREE
)
13868 if (integer_zerop (tmp
))
13869 return constant_boolean_node (false, type
);
13871 return constant_boolean_node (true, type
);
13873 unsigned count
= VECTOR_CST_NELTS (op0
);
13874 tree
*elts
= XALLOCAVEC (tree
, count
);
13875 gcc_assert (VECTOR_CST_NELTS (op1
) == count
13876 && TYPE_VECTOR_SUBPARTS (type
) == count
);
13878 for (unsigned i
= 0; i
< count
; i
++)
13880 tree elem_type
= TREE_TYPE (type
);
13881 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13882 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13884 tree tem
= fold_relational_const (code
, elem_type
,
13887 if (tem
== NULL_TREE
)
13890 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
13893 return build_vector (type
, elts
);
13896 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13898 To compute GT, swap the arguments and do LT.
13899 To compute GE, do LT and invert the result.
13900 To compute LE, swap the arguments, do LT and invert the result.
13901 To compute NE, do EQ and invert the result.
13903 Therefore, the code below must handle only EQ and LT. */
13905 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13907 std::swap (op0
, op1
);
13908 code
= swap_tree_comparison (code
);
13911 /* Note that it is safe to invert for real values here because we
13912 have already handled the one case that it matters. */
13915 if (code
== NE_EXPR
|| code
== GE_EXPR
)
13918 code
= invert_tree_comparison (code
, false);
13921 /* Compute a result for LT or EQ if args permit;
13922 Otherwise return T. */
13923 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
13925 if (code
== EQ_EXPR
)
13926 result
= tree_int_cst_equal (op0
, op1
);
13928 result
= tree_int_cst_lt (op0
, op1
);
13935 return constant_boolean_node (result
, type
);
13938 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13939 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13943 fold_build_cleanup_point_expr (tree type
, tree expr
)
13945 /* If the expression does not have side effects then we don't have to wrap
13946 it with a cleanup point expression. */
13947 if (!TREE_SIDE_EFFECTS (expr
))
13950 /* If the expression is a return, check to see if the expression inside the
13951 return has no side effects or the right hand side of the modify expression
13952 inside the return. If either don't have side effects set we don't need to
13953 wrap the expression in a cleanup point expression. Note we don't check the
13954 left hand side of the modify because it should always be a return decl. */
13955 if (TREE_CODE (expr
) == RETURN_EXPR
)
13957 tree op
= TREE_OPERAND (expr
, 0);
13958 if (!op
|| !TREE_SIDE_EFFECTS (op
))
13960 op
= TREE_OPERAND (op
, 1);
13961 if (!TREE_SIDE_EFFECTS (op
))
13965 return build1_loc (EXPR_LOCATION (expr
), CLEANUP_POINT_EXPR
, type
, expr
);
13968 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13969 of an indirection through OP0, or NULL_TREE if no simplification is
13973 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
13979 subtype
= TREE_TYPE (sub
);
13980 if (!POINTER_TYPE_P (subtype
))
13983 if (TREE_CODE (sub
) == ADDR_EXPR
)
13985 tree op
= TREE_OPERAND (sub
, 0);
13986 tree optype
= TREE_TYPE (op
);
13987 /* *&CONST_DECL -> to the value of the const decl. */
13988 if (TREE_CODE (op
) == CONST_DECL
)
13989 return DECL_INITIAL (op
);
13990 /* *&p => p; make sure to handle *&"str"[cst] here. */
13991 if (type
== optype
)
13993 tree fop
= fold_read_from_constant_string (op
);
13999 /* *(foo *)&fooarray => fooarray[0] */
14000 else if (TREE_CODE (optype
) == ARRAY_TYPE
14001 && type
== TREE_TYPE (optype
)
14002 && (!in_gimple_form
14003 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14005 tree type_domain
= TYPE_DOMAIN (optype
);
14006 tree min_val
= size_zero_node
;
14007 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14008 min_val
= TYPE_MIN_VALUE (type_domain
);
14010 && TREE_CODE (min_val
) != INTEGER_CST
)
14012 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
14013 NULL_TREE
, NULL_TREE
);
14015 /* *(foo *)&complexfoo => __real__ complexfoo */
14016 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14017 && type
== TREE_TYPE (optype
))
14018 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14019 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14020 else if (TREE_CODE (optype
) == VECTOR_TYPE
14021 && type
== TREE_TYPE (optype
))
14023 tree part_width
= TYPE_SIZE (type
);
14024 tree index
= bitsize_int (0);
14025 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14029 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14030 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14032 tree op00
= TREE_OPERAND (sub
, 0);
14033 tree op01
= TREE_OPERAND (sub
, 1);
14036 if (TREE_CODE (op00
) == ADDR_EXPR
)
14039 op00
= TREE_OPERAND (op00
, 0);
14040 op00type
= TREE_TYPE (op00
);
14042 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14043 if (TREE_CODE (op00type
) == VECTOR_TYPE
14044 && type
== TREE_TYPE (op00type
))
14046 tree part_width
= TYPE_SIZE (type
);
14047 unsigned HOST_WIDE_INT max_offset
14048 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
14049 * TYPE_VECTOR_SUBPARTS (op00type
));
14050 if (tree_int_cst_sign_bit (op01
) == 0
14051 && compare_tree_int (op01
, max_offset
) == -1)
14053 unsigned HOST_WIDE_INT offset
= tree_to_uhwi (op01
);
14054 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14055 tree index
= bitsize_int (indexi
);
14056 return fold_build3_loc (loc
,
14057 BIT_FIELD_REF
, type
, op00
,
14058 part_width
, index
);
14061 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14062 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14063 && type
== TREE_TYPE (op00type
))
14065 tree size
= TYPE_SIZE_UNIT (type
);
14066 if (tree_int_cst_equal (size
, op01
))
14067 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14069 /* ((foo *)&fooarray)[1] => fooarray[1] */
14070 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14071 && type
== TREE_TYPE (op00type
))
14073 tree type_domain
= TYPE_DOMAIN (op00type
);
14074 tree min_val
= size_zero_node
;
14075 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14076 min_val
= TYPE_MIN_VALUE (type_domain
);
14077 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
14078 TYPE_SIZE_UNIT (type
));
14079 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
14080 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14081 NULL_TREE
, NULL_TREE
);
14086 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14087 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14088 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14089 && (!in_gimple_form
14090 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14093 tree min_val
= size_zero_node
;
14094 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14095 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14096 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14097 min_val
= TYPE_MIN_VALUE (type_domain
);
14099 && TREE_CODE (min_val
) != INTEGER_CST
)
14101 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14108 /* Builds an expression for an indirection through T, simplifying some
14112 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14114 tree type
= TREE_TYPE (TREE_TYPE (t
));
14115 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14120 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14123 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14126 fold_indirect_ref_loc (location_t loc
, tree t
)
14128 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14136 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14137 whose result is ignored. The type of the returned tree need not be
14138 the same as the original expression. */
14141 fold_ignored_result (tree t
)
14143 if (!TREE_SIDE_EFFECTS (t
))
14144 return integer_zero_node
;
14147 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14150 t
= TREE_OPERAND (t
, 0);
14154 case tcc_comparison
:
14155 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14156 t
= TREE_OPERAND (t
, 0);
14157 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14158 t
= TREE_OPERAND (t
, 1);
14163 case tcc_expression
:
14164 switch (TREE_CODE (t
))
14166 case COMPOUND_EXPR
:
14167 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14169 t
= TREE_OPERAND (t
, 0);
14173 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14174 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14176 t
= TREE_OPERAND (t
, 0);
14189 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14192 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14194 tree div
= NULL_TREE
;
14199 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14200 have to do anything. Only do this when we are not given a const,
14201 because in that case, this check is more expensive than just
14203 if (TREE_CODE (value
) != INTEGER_CST
)
14205 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14207 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14211 /* If divisor is a power of two, simplify this to bit manipulation. */
14212 if (pow2_or_zerop (divisor
))
14214 if (TREE_CODE (value
) == INTEGER_CST
)
14216 wide_int val
= value
;
14219 if ((val
& (divisor
- 1)) == 0)
14222 overflow_p
= TREE_OVERFLOW (value
);
14223 val
+= divisor
- 1;
14224 val
&= - (int) divisor
;
14228 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14234 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14235 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14236 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14237 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14243 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14244 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14245 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14251 /* Likewise, but round down. */
14254 round_down_loc (location_t loc
, tree value
, int divisor
)
14256 tree div
= NULL_TREE
;
14258 gcc_assert (divisor
> 0);
14262 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14263 have to do anything. Only do this when we are not given a const,
14264 because in that case, this check is more expensive than just
14266 if (TREE_CODE (value
) != INTEGER_CST
)
14268 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14270 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14274 /* If divisor is a power of two, simplify this to bit manipulation. */
14275 if (pow2_or_zerop (divisor
))
14279 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14280 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14285 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14286 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14287 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14293 /* Returns the pointer to the base of the object addressed by EXP and
14294 extracts the information about the offset of the access, storing it
14295 to PBITPOS and POFFSET. */
14298 split_address_to_core_and_offset (tree exp
,
14299 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14303 int unsignedp
, reversep
, volatilep
;
14304 HOST_WIDE_INT bitsize
;
14305 location_t loc
= EXPR_LOCATION (exp
);
14307 if (TREE_CODE (exp
) == ADDR_EXPR
)
14309 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14310 poffset
, &mode
, &unsignedp
, &reversep
,
14312 core
= build_fold_addr_expr_loc (loc
, core
);
14318 *poffset
= NULL_TREE
;
14324 /* Returns true if addresses of E1 and E2 differ by a constant, false
14325 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14328 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14331 HOST_WIDE_INT bitpos1
, bitpos2
;
14332 tree toffset1
, toffset2
, tdiff
, type
;
14334 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14335 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14337 if (bitpos1
% BITS_PER_UNIT
!= 0
14338 || bitpos2
% BITS_PER_UNIT
!= 0
14339 || !operand_equal_p (core1
, core2
, 0))
14342 if (toffset1
&& toffset2
)
14344 type
= TREE_TYPE (toffset1
);
14345 if (type
!= TREE_TYPE (toffset2
))
14346 toffset2
= fold_convert (type
, toffset2
);
14348 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14349 if (!cst_and_fits_in_hwi (tdiff
))
14352 *diff
= int_cst_value (tdiff
);
14354 else if (toffset1
|| toffset2
)
14356 /* If only one of the offsets is non-constant, the difference cannot
14363 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14367 /* Return OFF converted to a pointer offset type suitable as offset for
14368 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14370 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14372 return fold_convert_loc (loc
, sizetype
, off
);
14375 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14377 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14379 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14380 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14383 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14385 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14387 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14388 ptr
, size_int (off
));
14391 /* Return a char pointer for a C string if it is a string constant
14392 or sum of string constant and integer constant. We only support
14393 string constants properly terminated with '\0' character.
14394 If STRLEN is a valid pointer, length (including terminating character)
14395 of returned string is stored to the argument. */
14398 c_getstr (tree src
, unsigned HOST_WIDE_INT
*strlen
)
14405 src
= string_constant (src
, &offset_node
);
14409 unsigned HOST_WIDE_INT offset
= 0;
14410 if (offset_node
!= NULL_TREE
)
14412 if (!tree_fits_uhwi_p (offset_node
))
14415 offset
= tree_to_uhwi (offset_node
);
14418 unsigned HOST_WIDE_INT string_length
= TREE_STRING_LENGTH (src
);
14419 const char *string
= TREE_STRING_POINTER (src
);
14421 /* Support only properly null-terminated strings. */
14422 if (string_length
== 0
14423 || string
[string_length
- 1] != '\0'
14424 || offset
>= string_length
)
14428 *strlen
= string_length
- offset
;
14429 return string
+ offset
;
14434 namespace selftest
{
14436 /* Helper functions for writing tests of folding trees. */
14438 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14441 assert_binop_folds_to_const (tree lhs
, enum tree_code code
, tree rhs
,
14444 ASSERT_EQ (constant
, fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
));
14447 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14448 wrapping WRAPPED_EXPR. */
14451 assert_binop_folds_to_nonlvalue (tree lhs
, enum tree_code code
, tree rhs
,
14454 tree result
= fold_build2 (code
, TREE_TYPE (lhs
), lhs
, rhs
);
14455 ASSERT_NE (wrapped_expr
, result
);
14456 ASSERT_EQ (NON_LVALUE_EXPR
, TREE_CODE (result
));
14457 ASSERT_EQ (wrapped_expr
, TREE_OPERAND (result
, 0));
14460 /* Verify that various arithmetic binary operations are folded
14464 test_arithmetic_folding ()
14466 tree type
= integer_type_node
;
14467 tree x
= create_tmp_var_raw (type
, "x");
14468 tree zero
= build_zero_cst (type
);
14469 tree one
= build_int_cst (type
, 1);
14472 /* 1 <-- (0 + 1) */
14473 assert_binop_folds_to_const (zero
, PLUS_EXPR
, one
,
14475 assert_binop_folds_to_const (one
, PLUS_EXPR
, zero
,
14478 /* (nonlvalue)x <-- (x + 0) */
14479 assert_binop_folds_to_nonlvalue (x
, PLUS_EXPR
, zero
,
14483 /* 0 <-- (x - x) */
14484 assert_binop_folds_to_const (x
, MINUS_EXPR
, x
,
14486 assert_binop_folds_to_nonlvalue (x
, MINUS_EXPR
, zero
,
14489 /* Multiplication. */
14490 /* 0 <-- (x * 0) */
14491 assert_binop_folds_to_const (x
, MULT_EXPR
, zero
,
14494 /* (nonlvalue)x <-- (x * 1) */
14495 assert_binop_folds_to_nonlvalue (x
, MULT_EXPR
, one
,
14499 /* Verify that various binary operations on vectors are folded
14503 test_vector_folding ()
14505 tree inner_type
= integer_type_node
;
14506 tree type
= build_vector_type (inner_type
, 4);
14507 tree zero
= build_zero_cst (type
);
14508 tree one
= build_one_cst (type
);
14510 /* Verify equality tests that return a scalar boolean result. */
14511 tree res_type
= boolean_type_node
;
14512 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, one
)));
14513 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR
, res_type
, zero
, zero
)));
14514 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, zero
, one
)));
14515 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR
, res_type
, one
, one
)));
14518 /* Run all of the selftests within this file. */
14521 fold_const_c_tests ()
14523 test_arithmetic_folding ();
14524 test_vector_folding ();
14527 } // namespace selftest
14529 #endif /* CHECKING_P */