1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2013 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"
49 #include "stor-layout.h"
51 #include "tree-iterator.h"
57 #include "diagnostic-core.h"
59 #include "langhooks.h"
61 #include "basic-block.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
65 #include "gimple-expr.h"
70 #include "hash-table.h" /* Required for ENABLE_FOLD_CHECKING. */
72 /* Nonzero if we are folding constants inside an initializer; zero
74 int folding_initializer
= 0;
76 /* The following constants represent a bit based encoding of GCC's
77 comparison operators. This encoding simplifies transformations
78 on relational comparison operators, such as AND and OR. */
79 enum comparison_code
{
98 static bool negate_mathfn_p (enum built_in_function
);
99 static bool negate_expr_p (tree
);
100 static tree
negate_expr (tree
);
101 static tree
split_tree (tree
, enum tree_code
, tree
*, tree
*, tree
*, int);
102 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
103 static tree
const_binop (enum tree_code
, tree
, tree
);
104 static enum comparison_code
comparison_to_compcode (enum tree_code
);
105 static enum tree_code
compcode_to_comparison (enum comparison_code
);
106 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
107 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
108 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
109 static tree
pedantic_omit_one_operand_loc (location_t
, tree
, tree
, tree
);
110 static tree
distribute_bit_expr (location_t
, enum tree_code
, tree
, tree
, tree
);
111 static tree
make_bit_field_ref (location_t
, tree
, tree
,
112 HOST_WIDE_INT
, HOST_WIDE_INT
, int);
113 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
115 static tree
decode_field_reference (location_t
, tree
, HOST_WIDE_INT
*,
117 enum machine_mode
*, int *, int *,
119 static tree
sign_bit_p (tree
, const_tree
);
120 static int simple_operand_p (const_tree
);
121 static bool simple_operand_p_2 (tree
);
122 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
123 static tree
range_predecessor (tree
);
124 static tree
range_successor (tree
);
125 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
126 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
127 static tree
unextend (tree
, int, int, tree
);
128 static tree
optimize_minmax_comparison (location_t
, enum tree_code
,
130 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
131 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
132 static tree
fold_binary_op_with_conditional_arg (location_t
,
133 enum tree_code
, tree
,
136 static tree
fold_mathfn_compare (location_t
,
137 enum built_in_function
, enum tree_code
,
139 static tree
fold_inf_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
140 static tree
fold_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
141 static bool reorder_operands_p (const_tree
, const_tree
);
142 static tree
fold_negate_const (tree
, tree
);
143 static tree
fold_not_const (const_tree
, tree
);
144 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
145 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
147 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
148 Otherwise, return LOC. */
151 expr_location_or (tree t
, location_t loc
)
153 location_t tloc
= EXPR_LOCATION (t
);
154 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
157 /* Similar to protected_set_expr_location, but never modify x in place,
158 if location can and needs to be set, unshare it. */
161 protected_set_expr_location_unshare (tree x
, location_t loc
)
163 if (CAN_HAVE_LOCATION_P (x
)
164 && EXPR_LOCATION (x
) != loc
165 && !(TREE_CODE (x
) == SAVE_EXPR
166 || TREE_CODE (x
) == TARGET_EXPR
167 || TREE_CODE (x
) == BIND_EXPR
))
170 SET_EXPR_LOCATION (x
, loc
);
175 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
176 division and returns the quotient. Otherwise returns
180 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
184 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
186 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
191 /* This is nonzero if we should defer warnings about undefined
192 overflow. This facility exists because these warnings are a
193 special case. The code to estimate loop iterations does not want
194 to issue any warnings, since it works with expressions which do not
195 occur in user code. Various bits of cleanup code call fold(), but
196 only use the result if it has certain characteristics (e.g., is a
197 constant); that code only wants to issue a warning if the result is
200 static int fold_deferring_overflow_warnings
;
202 /* If a warning about undefined overflow is deferred, this is the
203 warning. Note that this may cause us to turn two warnings into
204 one, but that is fine since it is sufficient to only give one
205 warning per expression. */
207 static const char* fold_deferred_overflow_warning
;
209 /* If a warning about undefined overflow is deferred, this is the
210 level at which the warning should be emitted. */
212 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
214 /* Start deferring overflow warnings. We could use a stack here to
215 permit nested calls, but at present it is not necessary. */
218 fold_defer_overflow_warnings (void)
220 ++fold_deferring_overflow_warnings
;
223 /* Stop deferring overflow warnings. If there is a pending warning,
224 and ISSUE is true, then issue the warning if appropriate. STMT is
225 the statement with which the warning should be associated (used for
226 location information); STMT may be NULL. CODE is the level of the
227 warning--a warn_strict_overflow_code value. This function will use
228 the smaller of CODE and the deferred code when deciding whether to
229 issue the warning. CODE may be zero to mean to always use the
233 fold_undefer_overflow_warnings (bool issue
, const_gimple stmt
, int code
)
238 gcc_assert (fold_deferring_overflow_warnings
> 0);
239 --fold_deferring_overflow_warnings
;
240 if (fold_deferring_overflow_warnings
> 0)
242 if (fold_deferred_overflow_warning
!= NULL
244 && code
< (int) fold_deferred_overflow_code
)
245 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
249 warnmsg
= fold_deferred_overflow_warning
;
250 fold_deferred_overflow_warning
= NULL
;
252 if (!issue
|| warnmsg
== NULL
)
255 if (gimple_no_warning_p (stmt
))
258 /* Use the smallest code level when deciding to issue the
260 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
261 code
= fold_deferred_overflow_code
;
263 if (!issue_strict_overflow_warning (code
))
267 locus
= input_location
;
269 locus
= gimple_location (stmt
);
270 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
273 /* Stop deferring overflow warnings, ignoring any deferred
277 fold_undefer_and_ignore_overflow_warnings (void)
279 fold_undefer_overflow_warnings (false, NULL
, 0);
282 /* Whether we are deferring overflow warnings. */
285 fold_deferring_overflow_warnings_p (void)
287 return fold_deferring_overflow_warnings
> 0;
290 /* This is called when we fold something based on the fact that signed
291 overflow is undefined. */
294 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
296 if (fold_deferring_overflow_warnings
> 0)
298 if (fold_deferred_overflow_warning
== NULL
299 || wc
< fold_deferred_overflow_code
)
301 fold_deferred_overflow_warning
= gmsgid
;
302 fold_deferred_overflow_code
= wc
;
305 else if (issue_strict_overflow_warning (wc
))
306 warning (OPT_Wstrict_overflow
, gmsgid
);
309 /* Return true if the built-in mathematical function specified by CODE
310 is odd, i.e. -f(x) == f(-x). */
313 negate_mathfn_p (enum built_in_function code
)
317 CASE_FLT_FN (BUILT_IN_ASIN
):
318 CASE_FLT_FN (BUILT_IN_ASINH
):
319 CASE_FLT_FN (BUILT_IN_ATAN
):
320 CASE_FLT_FN (BUILT_IN_ATANH
):
321 CASE_FLT_FN (BUILT_IN_CASIN
):
322 CASE_FLT_FN (BUILT_IN_CASINH
):
323 CASE_FLT_FN (BUILT_IN_CATAN
):
324 CASE_FLT_FN (BUILT_IN_CATANH
):
325 CASE_FLT_FN (BUILT_IN_CBRT
):
326 CASE_FLT_FN (BUILT_IN_CPROJ
):
327 CASE_FLT_FN (BUILT_IN_CSIN
):
328 CASE_FLT_FN (BUILT_IN_CSINH
):
329 CASE_FLT_FN (BUILT_IN_CTAN
):
330 CASE_FLT_FN (BUILT_IN_CTANH
):
331 CASE_FLT_FN (BUILT_IN_ERF
):
332 CASE_FLT_FN (BUILT_IN_LLROUND
):
333 CASE_FLT_FN (BUILT_IN_LROUND
):
334 CASE_FLT_FN (BUILT_IN_ROUND
):
335 CASE_FLT_FN (BUILT_IN_SIN
):
336 CASE_FLT_FN (BUILT_IN_SINH
):
337 CASE_FLT_FN (BUILT_IN_TAN
):
338 CASE_FLT_FN (BUILT_IN_TANH
):
339 CASE_FLT_FN (BUILT_IN_TRUNC
):
342 CASE_FLT_FN (BUILT_IN_LLRINT
):
343 CASE_FLT_FN (BUILT_IN_LRINT
):
344 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
345 CASE_FLT_FN (BUILT_IN_RINT
):
346 return !flag_rounding_math
;
354 /* Check whether we may negate an integer constant T without causing
358 may_negate_without_overflow_p (const_tree t
)
362 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
364 type
= TREE_TYPE (t
);
365 if (TYPE_UNSIGNED (type
))
368 return !wi::only_sign_bit_p (t
);
371 /* Determine whether an expression T can be cheaply negated using
372 the function negate_expr without introducing undefined overflow. */
375 negate_expr_p (tree t
)
382 type
= TREE_TYPE (t
);
385 switch (TREE_CODE (t
))
388 if (TYPE_OVERFLOW_WRAPS (type
))
391 /* Check that -CST will not overflow type. */
392 return may_negate_without_overflow_p (t
);
394 return (INTEGRAL_TYPE_P (type
)
395 && TYPE_OVERFLOW_WRAPS (type
));
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 (TYPE_MODE (type
))
433 || HONOR_SIGNED_ZEROS (TYPE_MODE (type
)))
435 /* -(A + B) -> (-B) - A. */
436 if (negate_expr_p (TREE_OPERAND (t
, 1))
437 && reorder_operands_p (TREE_OPERAND (t
, 0),
438 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 (TYPE_MODE (type
))
446 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
447 && reorder_operands_p (TREE_OPERAND (t
, 0),
448 TREE_OPERAND (t
, 1));
451 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
457 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t
))))
458 return negate_expr_p (TREE_OPERAND (t
, 1))
459 || negate_expr_p (TREE_OPERAND (t
, 0));
467 /* In general we can't negate A / B, because if A is INT_MIN and
468 B is 1, we may turn this into INT_MIN / -1 which is undefined
469 and actually traps on some architectures. But if overflow is
470 undefined, we can negate, because - (INT_MIN / 1) is an
472 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
474 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
)))
476 /* If overflow is undefined then we have to be careful because
477 we ask whether it's ok to associate the negate with the
478 division which is not ok for example for
479 -((a - b) / c) where (-(a - b)) / c may invoke undefined
480 overflow because of negating INT_MIN. So do not use
481 negate_expr_p here but open-code the two important cases. */
482 if (TREE_CODE (TREE_OPERAND (t
, 0)) == NEGATE_EXPR
483 || (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
484 && may_negate_without_overflow_p (TREE_OPERAND (t
, 0))))
487 else if (negate_expr_p (TREE_OPERAND (t
, 0)))
489 return negate_expr_p (TREE_OPERAND (t
, 1));
492 /* Negate -((double)float) as (double)(-float). */
493 if (TREE_CODE (type
) == REAL_TYPE
)
495 tree tem
= strip_float_extensions (t
);
497 return negate_expr_p (tem
);
502 /* Negate -f(x) as f(-x). */
503 if (negate_mathfn_p (builtin_mathfn_code (t
)))
504 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
508 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
509 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
511 tree op1
= TREE_OPERAND (t
, 1);
512 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
523 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
524 simplification is possible.
525 If negate_expr_p would return true for T, NULL_TREE will never be
529 fold_negate_expr (location_t loc
, tree t
)
531 tree type
= TREE_TYPE (t
);
534 switch (TREE_CODE (t
))
536 /* Convert - (~A) to A + 1. */
538 if (INTEGRAL_TYPE_P (type
))
539 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
540 build_one_cst (type
));
544 tem
= fold_negate_const (t
, type
);
545 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
546 || !TYPE_OVERFLOW_TRAPS (type
))
551 tem
= fold_negate_const (t
, type
);
552 /* Two's complement FP formats, such as c4x, may overflow. */
553 if (!TREE_OVERFLOW (tem
) || !flag_trapping_math
)
558 tem
= fold_negate_const (t
, type
);
563 tree rpart
= negate_expr (TREE_REALPART (t
));
564 tree ipart
= negate_expr (TREE_IMAGPART (t
));
566 if ((TREE_CODE (rpart
) == REAL_CST
567 && TREE_CODE (ipart
) == REAL_CST
)
568 || (TREE_CODE (rpart
) == INTEGER_CST
569 && TREE_CODE (ipart
) == INTEGER_CST
))
570 return build_complex (type
, rpart
, ipart
);
576 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
577 tree
*elts
= XALLOCAVEC (tree
, count
);
579 for (i
= 0; i
< count
; i
++)
581 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
582 if (elts
[i
] == NULL_TREE
)
586 return build_vector (type
, elts
);
590 if (negate_expr_p (t
))
591 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
592 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
593 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
597 if (negate_expr_p (t
))
598 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
599 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
603 return TREE_OPERAND (t
, 0);
606 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type
))
607 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type
)))
609 /* -(A + B) -> (-B) - A. */
610 if (negate_expr_p (TREE_OPERAND (t
, 1))
611 && reorder_operands_p (TREE_OPERAND (t
, 0),
612 TREE_OPERAND (t
, 1)))
614 tem
= negate_expr (TREE_OPERAND (t
, 1));
615 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
616 tem
, TREE_OPERAND (t
, 0));
619 /* -(A + B) -> (-A) - B. */
620 if (negate_expr_p (TREE_OPERAND (t
, 0)))
622 tem
= negate_expr (TREE_OPERAND (t
, 0));
623 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
624 tem
, TREE_OPERAND (t
, 1));
630 /* - (A - B) -> B - A */
631 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type
))
632 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
633 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
634 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
635 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
639 if (TYPE_UNSIGNED (type
))
645 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type
)))
647 tem
= TREE_OPERAND (t
, 1);
648 if (negate_expr_p (tem
))
649 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
650 TREE_OPERAND (t
, 0), negate_expr (tem
));
651 tem
= TREE_OPERAND (t
, 0);
652 if (negate_expr_p (tem
))
653 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
654 negate_expr (tem
), TREE_OPERAND (t
, 1));
663 /* In general we can't negate A / B, because if A is INT_MIN and
664 B is 1, we may turn this into INT_MIN / -1 which is undefined
665 and actually traps on some architectures. But if overflow is
666 undefined, we can negate, because - (INT_MIN / 1) is an
668 if (!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
670 const char * const warnmsg
= G_("assuming signed overflow does not "
671 "occur when negating a division");
672 tem
= TREE_OPERAND (t
, 1);
673 if (negate_expr_p (tem
))
675 if (INTEGRAL_TYPE_P (type
)
676 && (TREE_CODE (tem
) != INTEGER_CST
677 || integer_onep (tem
)))
678 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MISC
);
679 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
680 TREE_OPERAND (t
, 0), negate_expr (tem
));
682 /* If overflow is undefined then we have to be careful because
683 we ask whether it's ok to associate the negate with the
684 division which is not ok for example for
685 -((a - b) / c) where (-(a - b)) / c may invoke undefined
686 overflow because of negating INT_MIN. So do not use
687 negate_expr_p here but open-code the two important cases. */
688 tem
= TREE_OPERAND (t
, 0);
689 if ((INTEGRAL_TYPE_P (type
)
690 && (TREE_CODE (tem
) == NEGATE_EXPR
691 || (TREE_CODE (tem
) == INTEGER_CST
692 && may_negate_without_overflow_p (tem
))))
693 || !INTEGRAL_TYPE_P (type
))
694 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
695 negate_expr (tem
), TREE_OPERAND (t
, 1));
700 /* Convert -((double)float) into (double)(-float). */
701 if (TREE_CODE (type
) == REAL_TYPE
)
703 tem
= strip_float_extensions (t
);
704 if (tem
!= t
&& negate_expr_p (tem
))
705 return fold_convert_loc (loc
, type
, negate_expr (tem
));
710 /* Negate -f(x) as f(-x). */
711 if (negate_mathfn_p (builtin_mathfn_code (t
))
712 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
716 fndecl
= get_callee_fndecl (t
);
717 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
718 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
723 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
724 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
726 tree op1
= TREE_OPERAND (t
, 1);
727 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
729 tree ntype
= TYPE_UNSIGNED (type
)
730 ? signed_type_for (type
)
731 : unsigned_type_for (type
);
732 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
733 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
734 return fold_convert_loc (loc
, type
, temp
);
746 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
747 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
759 loc
= EXPR_LOCATION (t
);
760 type
= TREE_TYPE (t
);
763 tem
= fold_negate_expr (loc
, t
);
765 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
766 return fold_convert_loc (loc
, type
, tem
);
769 /* Split a tree IN into a constant, literal and variable parts that could be
770 combined with CODE to make IN. "constant" means an expression with
771 TREE_CONSTANT but that isn't an actual constant. CODE must be a
772 commutative arithmetic operation. Store the constant part into *CONP,
773 the literal in *LITP and return the variable part. If a part isn't
774 present, set it to null. If the tree does not decompose in this way,
775 return the entire tree as the variable part and the other parts as null.
777 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
778 case, we negate an operand that was subtracted. Except if it is a
779 literal for which we use *MINUS_LITP instead.
781 If NEGATE_P is true, we are negating all of IN, again except a literal
782 for which we use *MINUS_LITP instead.
784 If IN is itself a literal or constant, return it as appropriate.
786 Note that we do not guarantee that any of the three values will be the
787 same type as IN, but they will have the same signedness and mode. */
790 split_tree (tree in
, enum tree_code code
, tree
*conp
, tree
*litp
,
791 tree
*minus_litp
, int negate_p
)
799 /* Strip any conversions that don't change the machine mode or signedness. */
800 STRIP_SIGN_NOPS (in
);
802 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
803 || TREE_CODE (in
) == FIXED_CST
)
805 else if (TREE_CODE (in
) == code
806 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
807 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
808 /* We can associate addition and subtraction together (even
809 though the C standard doesn't say so) for integers because
810 the value is not affected. For reals, the value might be
811 affected, so we can't. */
812 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
813 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
815 tree op0
= TREE_OPERAND (in
, 0);
816 tree op1
= TREE_OPERAND (in
, 1);
817 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
818 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
822 || TREE_CODE (op0
) == FIXED_CST
)
823 *litp
= op0
, op0
= 0;
824 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
825 || TREE_CODE (op1
) == FIXED_CST
)
826 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
828 if (op0
!= 0 && TREE_CONSTANT (op0
))
829 *conp
= op0
, op0
= 0;
830 else if (op1
!= 0 && TREE_CONSTANT (op1
))
831 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0
!= 0 && op1
!= 0)
840 var
= op1
, neg_var_p
= neg1_p
;
842 /* Now do any needed negations. */
844 *minus_litp
= *litp
, *litp
= 0;
846 *conp
= negate_expr (*conp
);
848 var
= negate_expr (var
);
850 else if (TREE_CODE (in
) == BIT_NOT_EXPR
851 && code
== PLUS_EXPR
)
853 /* -X - 1 is folded to ~X, undo that here. */
854 *minus_litp
= build_one_cst (TREE_TYPE (in
));
855 var
= negate_expr (TREE_OPERAND (in
, 0));
857 else if (TREE_CONSTANT (in
))
865 *minus_litp
= *litp
, *litp
= 0;
866 else if (*minus_litp
)
867 *litp
= *minus_litp
, *minus_litp
= 0;
868 *conp
= negate_expr (*conp
);
869 var
= negate_expr (var
);
875 /* Re-associate trees split by the above function. T1 and T2 are
876 either expressions to associate or null. Return the new
877 expression, if any. LOC is the location of the new expression. If
878 we build an operation, do it in TYPE and with CODE. */
881 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
888 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
889 try to fold this since we will have infinite recursion. But do
890 deal with any NEGATE_EXPRs. */
891 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
892 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
894 if (code
== PLUS_EXPR
)
896 if (TREE_CODE (t1
) == NEGATE_EXPR
)
897 return build2_loc (loc
, MINUS_EXPR
, type
,
898 fold_convert_loc (loc
, type
, t2
),
899 fold_convert_loc (loc
, type
,
900 TREE_OPERAND (t1
, 0)));
901 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
902 return build2_loc (loc
, MINUS_EXPR
, type
,
903 fold_convert_loc (loc
, type
, t1
),
904 fold_convert_loc (loc
, type
,
905 TREE_OPERAND (t2
, 0)));
906 else if (integer_zerop (t2
))
907 return fold_convert_loc (loc
, type
, t1
);
909 else if (code
== MINUS_EXPR
)
911 if (integer_zerop (t2
))
912 return fold_convert_loc (loc
, type
, t1
);
915 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
916 fold_convert_loc (loc
, type
, t2
));
919 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
920 fold_convert_loc (loc
, type
, t2
));
923 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
924 for use in int_const_binop, size_binop and size_diffop. */
927 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
929 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
931 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
946 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
947 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
948 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
952 /* Combine two integer constants ARG1 and ARG2 under operation CODE
953 to produce a new constant. Return NULL_TREE if we don't know how
954 to evaluate CODE at compile-time. */
957 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
962 tree type
= TREE_TYPE (arg1
);
963 signop sign
= TYPE_SIGN (type
);
964 bool overflow
= false;
966 wide_int arg2
= wide_int::from (parg2
, TYPE_PRECISION (type
),
967 TYPE_SIGN (TREE_TYPE (parg2
)));
972 res
= wi::bit_or (arg1
, arg2
);
976 res
= wi::bit_xor (arg1
, arg2
);
980 res
= wi::bit_and (arg1
, arg2
);
985 if (wi::neg_p (arg2
))
988 if (code
== RSHIFT_EXPR
)
994 if (code
== RSHIFT_EXPR
)
995 /* It's unclear from the C standard whether shifts can overflow.
996 The following code ignores overflow; perhaps a C standard
997 interpretation ruling is needed. */
998 res
= wi::rshift (arg1
, arg2
, sign
);
1000 res
= wi::lshift (arg1
, arg2
);
1005 if (wi::neg_p (arg2
))
1008 if (code
== RROTATE_EXPR
)
1009 code
= LROTATE_EXPR
;
1011 code
= RROTATE_EXPR
;
1014 if (code
== RROTATE_EXPR
)
1015 res
= wi::rrotate (arg1
, arg2
);
1017 res
= wi::lrotate (arg1
, arg2
);
1021 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1025 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1029 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1032 case MULT_HIGHPART_EXPR
:
1033 res
= wi::mul_high (arg1
, arg2
, sign
);
1036 case TRUNC_DIV_EXPR
:
1037 case EXACT_DIV_EXPR
:
1040 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1043 case FLOOR_DIV_EXPR
:
1046 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1052 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1055 case ROUND_DIV_EXPR
:
1058 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1061 case TRUNC_MOD_EXPR
:
1064 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1067 case FLOOR_MOD_EXPR
:
1070 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1076 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1079 case ROUND_MOD_EXPR
:
1082 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1086 res
= wi::min (arg1
, arg2
, sign
);
1090 res
= wi::max (arg1
, arg2
, sign
);
1097 t
= force_fit_type (type
, res
, overflowable
,
1098 (((sign
== SIGNED
|| overflowable
== -1)
1100 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1106 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1108 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1111 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1112 constant. We assume ARG1 and ARG2 have the same data type, or at least
1113 are the same kind of constant and the same machine mode. Return zero if
1114 combining the constants is not allowed in the current operating mode. */
1117 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1119 /* Sanity check for the recursive cases. */
1126 if (TREE_CODE (arg1
) == INTEGER_CST
)
1127 return int_const_binop (code
, arg1
, arg2
);
1129 if (TREE_CODE (arg1
) == REAL_CST
)
1131 enum machine_mode mode
;
1134 REAL_VALUE_TYPE value
;
1135 REAL_VALUE_TYPE result
;
1139 /* The following codes are handled by real_arithmetic. */
1154 d1
= TREE_REAL_CST (arg1
);
1155 d2
= TREE_REAL_CST (arg2
);
1157 type
= TREE_TYPE (arg1
);
1158 mode
= TYPE_MODE (type
);
1160 /* Don't perform operation if we honor signaling NaNs and
1161 either operand is a NaN. */
1162 if (HONOR_SNANS (mode
)
1163 && (REAL_VALUE_ISNAN (d1
) || REAL_VALUE_ISNAN (d2
)))
1166 /* Don't perform operation if it would raise a division
1167 by zero exception. */
1168 if (code
== RDIV_EXPR
1169 && REAL_VALUES_EQUAL (d2
, dconst0
)
1170 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1173 /* If either operand is a NaN, just return it. Otherwise, set up
1174 for floating-point trap; we return an overflow. */
1175 if (REAL_VALUE_ISNAN (d1
))
1177 else if (REAL_VALUE_ISNAN (d2
))
1180 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1181 real_convert (&result
, mode
, &value
);
1183 /* Don't constant fold this floating point operation if
1184 the result has overflowed and flag_trapping_math. */
1185 if (flag_trapping_math
1186 && MODE_HAS_INFINITIES (mode
)
1187 && REAL_VALUE_ISINF (result
)
1188 && !REAL_VALUE_ISINF (d1
)
1189 && !REAL_VALUE_ISINF (d2
))
1192 /* Don't constant fold this floating point operation if the
1193 result may dependent upon the run-time rounding mode and
1194 flag_rounding_math is set, or if GCC's software emulation
1195 is unable to accurately represent the result. */
1196 if ((flag_rounding_math
1197 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1198 && (inexact
|| !real_identical (&result
, &value
)))
1201 t
= build_real (type
, result
);
1203 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1207 if (TREE_CODE (arg1
) == FIXED_CST
)
1209 FIXED_VALUE_TYPE f1
;
1210 FIXED_VALUE_TYPE f2
;
1211 FIXED_VALUE_TYPE result
;
1216 /* The following codes are handled by fixed_arithmetic. */
1222 case TRUNC_DIV_EXPR
:
1223 f2
= TREE_FIXED_CST (arg2
);
1230 f2
.data
.high
= w2
.elt (1);
1231 f2
.data
.low
= w2
.elt (0);
1240 f1
= TREE_FIXED_CST (arg1
);
1241 type
= TREE_TYPE (arg1
);
1242 sat_p
= TYPE_SATURATING (type
);
1243 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1244 t
= build_fixed (type
, result
);
1245 /* Propagate overflow flags. */
1246 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1247 TREE_OVERFLOW (t
) = 1;
1251 if (TREE_CODE (arg1
) == COMPLEX_CST
)
1253 tree type
= TREE_TYPE (arg1
);
1254 tree r1
= TREE_REALPART (arg1
);
1255 tree i1
= TREE_IMAGPART (arg1
);
1256 tree r2
= TREE_REALPART (arg2
);
1257 tree i2
= TREE_IMAGPART (arg2
);
1264 real
= const_binop (code
, r1
, r2
);
1265 imag
= const_binop (code
, i1
, i2
);
1269 if (COMPLEX_FLOAT_TYPE_P (type
))
1270 return do_mpc_arg2 (arg1
, arg2
, type
,
1271 /* do_nonfinite= */ folding_initializer
,
1274 real
= const_binop (MINUS_EXPR
,
1275 const_binop (MULT_EXPR
, r1
, r2
),
1276 const_binop (MULT_EXPR
, i1
, i2
));
1277 imag
= const_binop (PLUS_EXPR
,
1278 const_binop (MULT_EXPR
, r1
, i2
),
1279 const_binop (MULT_EXPR
, i1
, r2
));
1283 if (COMPLEX_FLOAT_TYPE_P (type
))
1284 return do_mpc_arg2 (arg1
, arg2
, type
,
1285 /* do_nonfinite= */ folding_initializer
,
1288 case TRUNC_DIV_EXPR
:
1290 case FLOOR_DIV_EXPR
:
1291 case ROUND_DIV_EXPR
:
1292 if (flag_complex_method
== 0)
1294 /* Keep this algorithm in sync with
1295 tree-complex.c:expand_complex_div_straight().
1297 Expand complex division to scalars, straightforward algorithm.
1298 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1302 = const_binop (PLUS_EXPR
,
1303 const_binop (MULT_EXPR
, r2
, r2
),
1304 const_binop (MULT_EXPR
, i2
, i2
));
1306 = const_binop (PLUS_EXPR
,
1307 const_binop (MULT_EXPR
, r1
, r2
),
1308 const_binop (MULT_EXPR
, i1
, i2
));
1310 = const_binop (MINUS_EXPR
,
1311 const_binop (MULT_EXPR
, i1
, r2
),
1312 const_binop (MULT_EXPR
, r1
, i2
));
1314 real
= const_binop (code
, t1
, magsquared
);
1315 imag
= const_binop (code
, t2
, magsquared
);
1319 /* Keep this algorithm in sync with
1320 tree-complex.c:expand_complex_div_wide().
1322 Expand complex division to scalars, modified algorithm to minimize
1323 overflow with wide input ranges. */
1324 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1325 fold_abs_const (r2
, TREE_TYPE (type
)),
1326 fold_abs_const (i2
, TREE_TYPE (type
)));
1328 if (integer_nonzerop (compare
))
1330 /* In the TRUE branch, we compute
1332 div = (br * ratio) + bi;
1333 tr = (ar * ratio) + ai;
1334 ti = (ai * ratio) - ar;
1337 tree ratio
= const_binop (code
, r2
, i2
);
1338 tree div
= const_binop (PLUS_EXPR
, i2
,
1339 const_binop (MULT_EXPR
, r2
, ratio
));
1340 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1341 real
= const_binop (PLUS_EXPR
, real
, i1
);
1342 real
= const_binop (code
, real
, div
);
1344 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1345 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1346 imag
= const_binop (code
, imag
, div
);
1350 /* In the FALSE branch, we compute
1352 divisor = (d * ratio) + c;
1353 tr = (b * ratio) + a;
1354 ti = b - (a * ratio);
1357 tree ratio
= const_binop (code
, i2
, r2
);
1358 tree div
= const_binop (PLUS_EXPR
, r2
,
1359 const_binop (MULT_EXPR
, i2
, ratio
));
1361 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1362 real
= const_binop (PLUS_EXPR
, real
, r1
);
1363 real
= const_binop (code
, real
, div
);
1365 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1366 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1367 imag
= const_binop (code
, imag
, div
);
1377 return build_complex (type
, real
, imag
);
1380 if (TREE_CODE (arg1
) == VECTOR_CST
1381 && TREE_CODE (arg2
) == VECTOR_CST
)
1383 tree type
= TREE_TYPE (arg1
);
1384 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1385 tree
*elts
= XALLOCAVEC (tree
, count
);
1387 for (i
= 0; i
< count
; i
++)
1389 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1390 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1392 elts
[i
] = const_binop (code
, elem1
, elem2
);
1394 /* It is possible that const_binop cannot handle the given
1395 code and return NULL_TREE */
1396 if (elts
[i
] == NULL_TREE
)
1400 return build_vector (type
, elts
);
1403 /* Shifts allow a scalar offset for a vector. */
1404 if (TREE_CODE (arg1
) == VECTOR_CST
1405 && TREE_CODE (arg2
) == INTEGER_CST
)
1407 tree type
= TREE_TYPE (arg1
);
1408 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1409 tree
*elts
= XALLOCAVEC (tree
, count
);
1411 if (code
== VEC_LSHIFT_EXPR
1412 || code
== VEC_RSHIFT_EXPR
)
1414 if (!tree_fits_uhwi_p (arg2
))
1417 unsigned HOST_WIDE_INT shiftc
= tree_to_uhwi (arg2
);
1418 unsigned HOST_WIDE_INT outerc
= tree_to_uhwi (TYPE_SIZE (type
));
1419 unsigned HOST_WIDE_INT innerc
1420 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type
)));
1421 if (shiftc
>= outerc
|| (shiftc
% innerc
) != 0)
1423 int offset
= shiftc
/ innerc
;
1424 /* The direction of VEC_[LR]SHIFT_EXPR is endian dependent.
1425 For reductions, compiler emits VEC_RSHIFT_EXPR always,
1426 for !BYTES_BIG_ENDIAN picks first vector element, but
1427 for BYTES_BIG_ENDIAN last element from the vector. */
1428 if ((code
== VEC_RSHIFT_EXPR
) ^ (!BYTES_BIG_ENDIAN
))
1430 tree zero
= build_zero_cst (TREE_TYPE (type
));
1431 for (i
= 0; i
< count
; i
++)
1433 if (i
+ offset
< 0 || i
+ offset
>= count
)
1436 elts
[i
] = VECTOR_CST_ELT (arg1
, i
+ offset
);
1440 for (i
= 0; i
< count
; i
++)
1442 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1444 elts
[i
] = const_binop (code
, elem1
, arg2
);
1446 /* It is possible that const_binop cannot handle the given
1447 code and return NULL_TREE */
1448 if (elts
[i
] == NULL_TREE
)
1452 return build_vector (type
, elts
);
1457 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1458 indicates which particular sizetype to create. */
1461 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1463 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1466 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1467 is a tree code. The type of the result is taken from the operands.
1468 Both must be equivalent integer types, ala int_binop_types_match_p.
1469 If the operands are constant, so is the result. */
1472 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1474 tree type
= TREE_TYPE (arg0
);
1476 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1477 return error_mark_node
;
1479 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1482 /* Handle the special case of two integer constants faster. */
1483 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1485 /* And some specific cases even faster than that. */
1486 if (code
== PLUS_EXPR
)
1488 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1490 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1493 else if (code
== MINUS_EXPR
)
1495 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1498 else if (code
== MULT_EXPR
)
1500 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1504 /* Handle general case of two integer constants. For sizetype
1505 constant calculations we always want to know about overflow,
1506 even in the unsigned case. */
1507 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1510 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1513 /* Given two values, either both of sizetype or both of bitsizetype,
1514 compute the difference between the two values. Return the value
1515 in signed type corresponding to the type of the operands. */
1518 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1520 tree type
= TREE_TYPE (arg0
);
1523 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1526 /* If the type is already signed, just do the simple thing. */
1527 if (!TYPE_UNSIGNED (type
))
1528 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1530 if (type
== sizetype
)
1532 else if (type
== bitsizetype
)
1533 ctype
= sbitsizetype
;
1535 ctype
= signed_type_for (type
);
1537 /* If either operand is not a constant, do the conversions to the signed
1538 type and subtract. The hardware will do the right thing with any
1539 overflow in the subtraction. */
1540 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1541 return size_binop_loc (loc
, MINUS_EXPR
,
1542 fold_convert_loc (loc
, ctype
, arg0
),
1543 fold_convert_loc (loc
, ctype
, arg1
));
1545 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1546 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1547 overflow) and negate (which can't either). Special-case a result
1548 of zero while we're here. */
1549 if (tree_int_cst_equal (arg0
, arg1
))
1550 return build_int_cst (ctype
, 0);
1551 else if (tree_int_cst_lt (arg1
, arg0
))
1552 return fold_convert_loc (loc
, ctype
,
1553 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1555 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1556 fold_convert_loc (loc
, ctype
,
1557 size_binop_loc (loc
,
1562 /* A subroutine of fold_convert_const handling conversions of an
1563 INTEGER_CST to another integer type. */
1566 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1568 /* Given an integer constant, make new constant with new type,
1569 appropriately sign-extended or truncated. Use widest_int
1570 so that any extension is done according ARG1's type. */
1571 return force_fit_type (type
, wi::to_widest (arg1
),
1572 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1573 TREE_OVERFLOW (arg1
));
1576 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1577 to an integer type. */
1580 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1582 bool overflow
= false;
1585 /* The following code implements the floating point to integer
1586 conversion rules required by the Java Language Specification,
1587 that IEEE NaNs are mapped to zero and values that overflow
1588 the target precision saturate, i.e. values greater than
1589 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1590 are mapped to INT_MIN. These semantics are allowed by the
1591 C and C++ standards that simply state that the behavior of
1592 FP-to-integer conversion is unspecified upon overflow. */
1596 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1600 case FIX_TRUNC_EXPR
:
1601 real_trunc (&r
, VOIDmode
, &x
);
1608 /* If R is NaN, return zero and show we have an overflow. */
1609 if (REAL_VALUE_ISNAN (r
))
1612 val
= wi::zero (TYPE_PRECISION (type
));
1615 /* See if R is less than the lower bound or greater than the
1620 tree lt
= TYPE_MIN_VALUE (type
);
1621 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1622 if (REAL_VALUES_LESS (r
, l
))
1631 tree ut
= TYPE_MAX_VALUE (type
);
1634 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1635 if (REAL_VALUES_LESS (u
, r
))
1644 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1646 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1650 /* A subroutine of fold_convert_const handling conversions of a
1651 FIXED_CST to an integer type. */
1654 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1657 double_int temp
, temp_trunc
;
1660 /* Right shift FIXED_CST to temp by fbit. */
1661 temp
= TREE_FIXED_CST (arg1
).data
;
1662 mode
= TREE_FIXED_CST (arg1
).mode
;
1663 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1665 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1666 HOST_BITS_PER_DOUBLE_INT
,
1667 SIGNED_FIXED_POINT_MODE_P (mode
));
1669 /* Left shift temp to temp_trunc by fbit. */
1670 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1671 HOST_BITS_PER_DOUBLE_INT
,
1672 SIGNED_FIXED_POINT_MODE_P (mode
));
1676 temp
= double_int_zero
;
1677 temp_trunc
= double_int_zero
;
1680 /* If FIXED_CST is negative, we need to round the value toward 0.
1681 By checking if the fractional bits are not zero to add 1 to temp. */
1682 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1683 && temp_trunc
.is_negative ()
1684 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1685 temp
+= double_int_one
;
1687 /* Given a fixed-point constant, make new constant with new type,
1688 appropriately sign-extended or truncated. */
1689 t
= force_fit_type (type
, temp
, -1,
1690 (temp
.is_negative ()
1691 && (TYPE_UNSIGNED (type
)
1692 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1693 | TREE_OVERFLOW (arg1
));
1698 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1699 to another floating point type. */
1702 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1704 REAL_VALUE_TYPE value
;
1707 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1708 t
= build_real (type
, value
);
1710 /* If converting an infinity or NAN to a representation that doesn't
1711 have one, set the overflow bit so that we can produce some kind of
1712 error message at the appropriate point if necessary. It's not the
1713 most user-friendly message, but it's better than nothing. */
1714 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
1715 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
1716 TREE_OVERFLOW (t
) = 1;
1717 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
1718 && !MODE_HAS_NANS (TYPE_MODE (type
)))
1719 TREE_OVERFLOW (t
) = 1;
1720 /* Regular overflow, conversion produced an infinity in a mode that
1721 can't represent them. */
1722 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
1723 && REAL_VALUE_ISINF (value
)
1724 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
1725 TREE_OVERFLOW (t
) = 1;
1727 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
1731 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1732 to a floating point type. */
1735 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
1737 REAL_VALUE_TYPE value
;
1740 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
1741 t
= build_real (type
, value
);
1743 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
1747 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1748 to another fixed-point type. */
1751 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
1753 FIXED_VALUE_TYPE value
;
1757 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
1758 TYPE_SATURATING (type
));
1759 t
= build_fixed (type
, value
);
1761 /* Propagate overflow flags. */
1762 if (overflow_p
| TREE_OVERFLOW (arg1
))
1763 TREE_OVERFLOW (t
) = 1;
1767 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1768 to a fixed-point type. */
1771 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
1773 FIXED_VALUE_TYPE value
;
1778 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
1780 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
1781 if (TREE_INT_CST_NUNITS (arg1
) == 1)
1782 di
.high
= (HOST_WIDE_INT
)di
.low
< 0 ? (HOST_WIDE_INT
)-1 : 0;
1784 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
1786 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
1787 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
1788 TYPE_SATURATING (type
));
1789 t
= build_fixed (type
, value
);
1791 /* Propagate overflow flags. */
1792 if (overflow_p
| TREE_OVERFLOW (arg1
))
1793 TREE_OVERFLOW (t
) = 1;
1797 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1798 to a fixed-point type. */
1801 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
1803 FIXED_VALUE_TYPE value
;
1807 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
1808 &TREE_REAL_CST (arg1
),
1809 TYPE_SATURATING (type
));
1810 t
= build_fixed (type
, value
);
1812 /* Propagate overflow flags. */
1813 if (overflow_p
| TREE_OVERFLOW (arg1
))
1814 TREE_OVERFLOW (t
) = 1;
1818 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1819 type TYPE. If no simplification can be done return NULL_TREE. */
1822 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
1824 if (TREE_TYPE (arg1
) == type
)
1827 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
1828 || TREE_CODE (type
) == OFFSET_TYPE
)
1830 if (TREE_CODE (arg1
) == INTEGER_CST
)
1831 return fold_convert_const_int_from_int (type
, arg1
);
1832 else if (TREE_CODE (arg1
) == REAL_CST
)
1833 return fold_convert_const_int_from_real (code
, type
, arg1
);
1834 else if (TREE_CODE (arg1
) == FIXED_CST
)
1835 return fold_convert_const_int_from_fixed (type
, arg1
);
1837 else if (TREE_CODE (type
) == REAL_TYPE
)
1839 if (TREE_CODE (arg1
) == INTEGER_CST
)
1840 return build_real_from_int_cst (type
, arg1
);
1841 else if (TREE_CODE (arg1
) == REAL_CST
)
1842 return fold_convert_const_real_from_real (type
, arg1
);
1843 else if (TREE_CODE (arg1
) == FIXED_CST
)
1844 return fold_convert_const_real_from_fixed (type
, arg1
);
1846 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
1848 if (TREE_CODE (arg1
) == FIXED_CST
)
1849 return fold_convert_const_fixed_from_fixed (type
, arg1
);
1850 else if (TREE_CODE (arg1
) == INTEGER_CST
)
1851 return fold_convert_const_fixed_from_int (type
, arg1
);
1852 else if (TREE_CODE (arg1
) == REAL_CST
)
1853 return fold_convert_const_fixed_from_real (type
, arg1
);
1858 /* Construct a vector of zero elements of vector type TYPE. */
1861 build_zero_vector (tree type
)
1865 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
1866 return build_vector_from_val (type
, t
);
1869 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1872 fold_convertible_p (const_tree type
, const_tree arg
)
1874 tree orig
= TREE_TYPE (arg
);
1879 if (TREE_CODE (arg
) == ERROR_MARK
1880 || TREE_CODE (type
) == ERROR_MARK
1881 || TREE_CODE (orig
) == ERROR_MARK
)
1884 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
1887 switch (TREE_CODE (type
))
1889 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
1890 case POINTER_TYPE
: case REFERENCE_TYPE
:
1892 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
1893 || TREE_CODE (orig
) == OFFSET_TYPE
)
1895 return (TREE_CODE (orig
) == VECTOR_TYPE
1896 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
1899 case FIXED_POINT_TYPE
:
1903 return TREE_CODE (type
) == TREE_CODE (orig
);
1910 /* Convert expression ARG to type TYPE. Used by the middle-end for
1911 simple conversions in preference to calling the front-end's convert. */
1914 fold_convert_loc (location_t loc
, tree type
, tree arg
)
1916 tree orig
= TREE_TYPE (arg
);
1922 if (TREE_CODE (arg
) == ERROR_MARK
1923 || TREE_CODE (type
) == ERROR_MARK
1924 || TREE_CODE (orig
) == ERROR_MARK
)
1925 return error_mark_node
;
1927 switch (TREE_CODE (type
))
1930 case REFERENCE_TYPE
:
1931 /* Handle conversions between pointers to different address spaces. */
1932 if (POINTER_TYPE_P (orig
)
1933 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
1934 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
1935 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
1938 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
1940 if (TREE_CODE (arg
) == INTEGER_CST
)
1942 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
1943 if (tem
!= NULL_TREE
)
1946 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
1947 || TREE_CODE (orig
) == OFFSET_TYPE
)
1948 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
1949 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
1950 return fold_convert_loc (loc
, type
,
1951 fold_build1_loc (loc
, REALPART_EXPR
,
1952 TREE_TYPE (orig
), arg
));
1953 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
1954 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
1955 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
1958 if (TREE_CODE (arg
) == INTEGER_CST
)
1960 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
1961 if (tem
!= NULL_TREE
)
1964 else if (TREE_CODE (arg
) == REAL_CST
)
1966 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
1967 if (tem
!= NULL_TREE
)
1970 else if (TREE_CODE (arg
) == FIXED_CST
)
1972 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
1973 if (tem
!= NULL_TREE
)
1977 switch (TREE_CODE (orig
))
1980 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
1981 case POINTER_TYPE
: case REFERENCE_TYPE
:
1982 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
1985 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
1987 case FIXED_POINT_TYPE
:
1988 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
1991 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
1992 return fold_convert_loc (loc
, type
, tem
);
1998 case FIXED_POINT_TYPE
:
1999 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2000 || TREE_CODE (arg
) == REAL_CST
)
2002 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2003 if (tem
!= NULL_TREE
)
2004 goto fold_convert_exit
;
2007 switch (TREE_CODE (orig
))
2009 case FIXED_POINT_TYPE
:
2014 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2017 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2018 return fold_convert_loc (loc
, type
, tem
);
2025 switch (TREE_CODE (orig
))
2028 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2029 case POINTER_TYPE
: case REFERENCE_TYPE
:
2031 case FIXED_POINT_TYPE
:
2032 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2033 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2034 fold_convert_loc (loc
, TREE_TYPE (type
),
2035 integer_zero_node
));
2040 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2042 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2043 TREE_OPERAND (arg
, 0));
2044 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2045 TREE_OPERAND (arg
, 1));
2046 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2049 arg
= save_expr (arg
);
2050 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2051 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2052 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2053 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2054 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2062 if (integer_zerop (arg
))
2063 return build_zero_vector (type
);
2064 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2065 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2066 || TREE_CODE (orig
) == VECTOR_TYPE
);
2067 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2070 tem
= fold_ignored_result (arg
);
2071 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2074 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2075 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2079 protected_set_expr_location_unshare (tem
, loc
);
2083 /* Return false if expr can be assumed not to be an lvalue, true
2087 maybe_lvalue_p (const_tree x
)
2089 /* We only need to wrap lvalue tree codes. */
2090 switch (TREE_CODE (x
))
2103 case ARRAY_RANGE_REF
:
2109 case PREINCREMENT_EXPR
:
2110 case PREDECREMENT_EXPR
:
2112 case TRY_CATCH_EXPR
:
2113 case WITH_CLEANUP_EXPR
:
2122 /* Assume the worst for front-end tree codes. */
2123 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2131 /* Return an expr equal to X but certainly not valid as an lvalue. */
2134 non_lvalue_loc (location_t loc
, tree x
)
2136 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2141 if (! maybe_lvalue_p (x
))
2143 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2146 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2147 Zero means allow extended lvalues. */
2149 int pedantic_lvalues
;
2151 /* When pedantic, return an expr equal to X but certainly not valid as a
2152 pedantic lvalue. Otherwise, return X. */
2155 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2157 if (pedantic_lvalues
)
2158 return non_lvalue_loc (loc
, x
);
2160 return protected_set_expr_location_unshare (x
, loc
);
2163 /* Given a tree comparison code, return the code that is the logical inverse.
2164 It is generally not safe to do this for floating-point comparisons, except
2165 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2166 ERROR_MARK in this case. */
2169 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2171 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2172 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2182 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2184 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2186 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2188 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2202 return UNORDERED_EXPR
;
2203 case UNORDERED_EXPR
:
2204 return ORDERED_EXPR
;
2210 /* Similar, but return the comparison that results if the operands are
2211 swapped. This is safe for floating-point. */
2214 swap_tree_comparison (enum tree_code code
)
2221 case UNORDERED_EXPR
:
2247 /* Convert a comparison tree code from an enum tree_code representation
2248 into a compcode bit-based encoding. This function is the inverse of
2249 compcode_to_comparison. */
2251 static enum comparison_code
2252 comparison_to_compcode (enum tree_code code
)
2269 return COMPCODE_ORD
;
2270 case UNORDERED_EXPR
:
2271 return COMPCODE_UNORD
;
2273 return COMPCODE_UNLT
;
2275 return COMPCODE_UNEQ
;
2277 return COMPCODE_UNLE
;
2279 return COMPCODE_UNGT
;
2281 return COMPCODE_LTGT
;
2283 return COMPCODE_UNGE
;
2289 /* Convert a compcode bit-based encoding of a comparison operator back
2290 to GCC's enum tree_code representation. This function is the
2291 inverse of comparison_to_compcode. */
2293 static enum tree_code
2294 compcode_to_comparison (enum comparison_code code
)
2311 return ORDERED_EXPR
;
2312 case COMPCODE_UNORD
:
2313 return UNORDERED_EXPR
;
2331 /* Return a tree for the comparison which is the combination of
2332 doing the AND or OR (depending on CODE) of the two operations LCODE
2333 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2334 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2335 if this makes the transformation invalid. */
2338 combine_comparisons (location_t loc
,
2339 enum tree_code code
, enum tree_code lcode
,
2340 enum tree_code rcode
, tree truth_type
,
2341 tree ll_arg
, tree lr_arg
)
2343 bool honor_nans
= HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg
)));
2344 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2345 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2350 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2351 compcode
= lcompcode
& rcompcode
;
2354 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2355 compcode
= lcompcode
| rcompcode
;
2364 /* Eliminate unordered comparisons, as well as LTGT and ORD
2365 which are not used unless the mode has NaNs. */
2366 compcode
&= ~COMPCODE_UNORD
;
2367 if (compcode
== COMPCODE_LTGT
)
2368 compcode
= COMPCODE_NE
;
2369 else if (compcode
== COMPCODE_ORD
)
2370 compcode
= COMPCODE_TRUE
;
2372 else if (flag_trapping_math
)
2374 /* Check that the original operation and the optimized ones will trap
2375 under the same condition. */
2376 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2377 && (lcompcode
!= COMPCODE_EQ
)
2378 && (lcompcode
!= COMPCODE_ORD
);
2379 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2380 && (rcompcode
!= COMPCODE_EQ
)
2381 && (rcompcode
!= COMPCODE_ORD
);
2382 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2383 && (compcode
!= COMPCODE_EQ
)
2384 && (compcode
!= COMPCODE_ORD
);
2386 /* In a short-circuited boolean expression the LHS might be
2387 such that the RHS, if evaluated, will never trap. For
2388 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2389 if neither x nor y is NaN. (This is a mixed blessing: for
2390 example, the expression above will never trap, hence
2391 optimizing it to x < y would be invalid). */
2392 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2393 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2396 /* If the comparison was short-circuited, and only the RHS
2397 trapped, we may now generate a spurious trap. */
2399 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2402 /* If we changed the conditions that cause a trap, we lose. */
2403 if ((ltrap
|| rtrap
) != trap
)
2407 if (compcode
== COMPCODE_TRUE
)
2408 return constant_boolean_node (true, truth_type
);
2409 else if (compcode
== COMPCODE_FALSE
)
2410 return constant_boolean_node (false, truth_type
);
2413 enum tree_code tcode
;
2415 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2416 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2420 /* Return nonzero if two operands (typically of the same tree node)
2421 are necessarily equal. If either argument has side-effects this
2422 function returns zero. FLAGS modifies behavior as follows:
2424 If OEP_ONLY_CONST is set, only return nonzero for constants.
2425 This function tests whether the operands are indistinguishable;
2426 it does not test whether they are equal using C's == operation.
2427 The distinction is important for IEEE floating point, because
2428 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2429 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2431 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2432 even though it may hold multiple values during a function.
2433 This is because a GCC tree node guarantees that nothing else is
2434 executed between the evaluation of its "operands" (which may often
2435 be evaluated in arbitrary order). Hence if the operands themselves
2436 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2437 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2438 unset means assuming isochronic (or instantaneous) tree equivalence.
2439 Unless comparing arbitrary expression trees, such as from different
2440 statements, this flag can usually be left unset.
2442 If OEP_PURE_SAME is set, then pure functions with identical arguments
2443 are considered the same. It is used when the caller has other ways
2444 to ensure that global memory is unchanged in between. */
2447 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2449 /* If either is ERROR_MARK, they aren't equal. */
2450 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2451 || TREE_TYPE (arg0
) == error_mark_node
2452 || TREE_TYPE (arg1
) == error_mark_node
)
2455 /* Similar, if either does not have a type (like a released SSA name),
2456 they aren't equal. */
2457 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2460 /* Check equality of integer constants before bailing out due to
2461 precision differences. */
2462 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2463 return tree_int_cst_equal (arg0
, arg1
);
2465 /* If both types don't have the same signedness, then we can't consider
2466 them equal. We must check this before the STRIP_NOPS calls
2467 because they may change the signedness of the arguments. As pointers
2468 strictly don't have a signedness, require either two pointers or
2469 two non-pointers as well. */
2470 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2471 || POINTER_TYPE_P (TREE_TYPE (arg0
)) != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2474 /* We cannot consider pointers to different address space equal. */
2475 if (POINTER_TYPE_P (TREE_TYPE (arg0
)) && POINTER_TYPE_P (TREE_TYPE (arg1
))
2476 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2477 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2480 /* If both types don't have the same precision, then it is not safe
2482 if (element_precision (TREE_TYPE (arg0
))
2483 != element_precision (TREE_TYPE (arg1
)))
2489 /* In case both args are comparisons but with different comparison
2490 code, try to swap the comparison operands of one arg to produce
2491 a match and compare that variant. */
2492 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2493 && COMPARISON_CLASS_P (arg0
)
2494 && COMPARISON_CLASS_P (arg1
))
2496 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2498 if (TREE_CODE (arg0
) == swap_code
)
2499 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2500 TREE_OPERAND (arg1
, 1), flags
)
2501 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2502 TREE_OPERAND (arg1
, 0), flags
);
2505 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2506 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2507 && !(CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
)))
2510 /* This is needed for conversions and for COMPONENT_REF.
2511 Might as well play it safe and always test this. */
2512 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2513 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2514 || TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
)))
2517 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2518 We don't care about side effects in that case because the SAVE_EXPR
2519 takes care of that for us. In all other cases, two expressions are
2520 equal if they have no side effects. If we have two identical
2521 expressions with side effects that should be treated the same due
2522 to the only side effects being identical SAVE_EXPR's, that will
2523 be detected in the recursive calls below.
2524 If we are taking an invariant address of two identical objects
2525 they are necessarily equal as well. */
2526 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2527 && (TREE_CODE (arg0
) == SAVE_EXPR
2528 || (flags
& OEP_CONSTANT_ADDRESS_OF
)
2529 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2532 /* Next handle constant cases, those for which we can return 1 even
2533 if ONLY_CONST is set. */
2534 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2535 switch (TREE_CODE (arg0
))
2538 return tree_int_cst_equal (arg0
, arg1
);
2541 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2542 TREE_FIXED_CST (arg1
));
2545 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0
),
2546 TREE_REAL_CST (arg1
)))
2550 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
))))
2552 /* If we do not distinguish between signed and unsigned zero,
2553 consider them equal. */
2554 if (real_zerop (arg0
) && real_zerop (arg1
))
2563 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2566 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2568 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2569 VECTOR_CST_ELT (arg1
, i
), flags
))
2576 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2578 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2582 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2583 && ! memcmp (TREE_STRING_POINTER (arg0
),
2584 TREE_STRING_POINTER (arg1
),
2585 TREE_STRING_LENGTH (arg0
)));
2588 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2589 TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
)
2590 ? OEP_CONSTANT_ADDRESS_OF
: 0);
2595 if (flags
& OEP_ONLY_CONST
)
2598 /* Define macros to test an operand from arg0 and arg1 for equality and a
2599 variant that allows null and views null as being different from any
2600 non-null value. In the latter case, if either is null, the both
2601 must be; otherwise, do the normal comparison. */
2602 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2603 TREE_OPERAND (arg1, N), flags)
2605 #define OP_SAME_WITH_NULL(N) \
2606 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2607 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2609 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2612 /* Two conversions are equal only if signedness and modes match. */
2613 switch (TREE_CODE (arg0
))
2616 case FIX_TRUNC_EXPR
:
2617 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2618 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
2628 case tcc_comparison
:
2630 if (OP_SAME (0) && OP_SAME (1))
2633 /* For commutative ops, allow the other order. */
2634 return (commutative_tree_code (TREE_CODE (arg0
))
2635 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2636 TREE_OPERAND (arg1
, 1), flags
)
2637 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2638 TREE_OPERAND (arg1
, 0), flags
));
2641 /* If either of the pointer (or reference) expressions we are
2642 dereferencing contain a side effect, these cannot be equal,
2643 but their addresses can be. */
2644 if ((flags
& OEP_CONSTANT_ADDRESS_OF
) == 0
2645 && (TREE_SIDE_EFFECTS (arg0
)
2646 || TREE_SIDE_EFFECTS (arg1
)))
2649 switch (TREE_CODE (arg0
))
2652 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2659 case TARGET_MEM_REF
:
2660 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2661 /* Require equal extra operands and then fall through to MEM_REF
2662 handling of the two common operands. */
2663 if (!OP_SAME_WITH_NULL (2)
2664 || !OP_SAME_WITH_NULL (3)
2665 || !OP_SAME_WITH_NULL (4))
2669 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2670 /* Require equal access sizes, and similar pointer types.
2671 We can have incomplete types for array references of
2672 variable-sized arrays from the Fortran frontend
2673 though. Also verify the types are compatible. */
2674 return ((TYPE_SIZE (TREE_TYPE (arg0
)) == TYPE_SIZE (TREE_TYPE (arg1
))
2675 || (TYPE_SIZE (TREE_TYPE (arg0
))
2676 && TYPE_SIZE (TREE_TYPE (arg1
))
2677 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
2678 TYPE_SIZE (TREE_TYPE (arg1
)), flags
)))
2679 && types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
))
2680 && alias_ptr_types_compatible_p
2681 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
2682 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
2683 && OP_SAME (0) && OP_SAME (1));
2686 case ARRAY_RANGE_REF
:
2687 /* Operands 2 and 3 may be null.
2688 Compare the array index by value if it is constant first as we
2689 may have different types but same value here. */
2692 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2693 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
2694 TREE_OPERAND (arg1
, 1))
2696 && OP_SAME_WITH_NULL (2)
2697 && OP_SAME_WITH_NULL (3));
2700 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2701 may be NULL when we're called to compare MEM_EXPRs. */
2702 if (!OP_SAME_WITH_NULL (0)
2705 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2706 return OP_SAME_WITH_NULL (2);
2711 flags
&= ~OEP_CONSTANT_ADDRESS_OF
;
2712 return OP_SAME (1) && OP_SAME (2);
2718 case tcc_expression
:
2719 switch (TREE_CODE (arg0
))
2722 case TRUTH_NOT_EXPR
:
2725 case TRUTH_ANDIF_EXPR
:
2726 case TRUTH_ORIF_EXPR
:
2727 return OP_SAME (0) && OP_SAME (1);
2730 case WIDEN_MULT_PLUS_EXPR
:
2731 case WIDEN_MULT_MINUS_EXPR
:
2734 /* The multiplcation operands are commutative. */
2737 case TRUTH_AND_EXPR
:
2739 case TRUTH_XOR_EXPR
:
2740 if (OP_SAME (0) && OP_SAME (1))
2743 /* Otherwise take into account this is a commutative operation. */
2744 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
2745 TREE_OPERAND (arg1
, 1), flags
)
2746 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2747 TREE_OPERAND (arg1
, 0), flags
));
2752 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2759 switch (TREE_CODE (arg0
))
2762 /* If the CALL_EXPRs call different functions, then they
2763 clearly can not be equal. */
2764 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
2769 unsigned int cef
= call_expr_flags (arg0
);
2770 if (flags
& OEP_PURE_SAME
)
2771 cef
&= ECF_CONST
| ECF_PURE
;
2778 /* Now see if all the arguments are the same. */
2780 const_call_expr_arg_iterator iter0
, iter1
;
2782 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
2783 a1
= first_const_call_expr_arg (arg1
, &iter1
);
2785 a0
= next_const_call_expr_arg (&iter0
),
2786 a1
= next_const_call_expr_arg (&iter1
))
2787 if (! operand_equal_p (a0
, a1
, flags
))
2790 /* If we get here and both argument lists are exhausted
2791 then the CALL_EXPRs are equal. */
2792 return ! (a0
|| a1
);
2798 case tcc_declaration
:
2799 /* Consider __builtin_sqrt equal to sqrt. */
2800 return (TREE_CODE (arg0
) == FUNCTION_DECL
2801 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
2802 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
2803 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
2810 #undef OP_SAME_WITH_NULL
2813 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2814 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2816 When in doubt, return 0. */
2819 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
2821 int unsignedp1
, unsignedpo
;
2822 tree primarg0
, primarg1
, primother
;
2823 unsigned int correct_width
;
2825 if (operand_equal_p (arg0
, arg1
, 0))
2828 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
2829 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
2832 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2833 and see if the inner values are the same. This removes any
2834 signedness comparison, which doesn't matter here. */
2835 primarg0
= arg0
, primarg1
= arg1
;
2836 STRIP_NOPS (primarg0
);
2837 STRIP_NOPS (primarg1
);
2838 if (operand_equal_p (primarg0
, primarg1
, 0))
2841 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2842 actual comparison operand, ARG0.
2844 First throw away any conversions to wider types
2845 already present in the operands. */
2847 primarg1
= get_narrower (arg1
, &unsignedp1
);
2848 primother
= get_narrower (other
, &unsignedpo
);
2850 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
2851 if (unsignedp1
== unsignedpo
2852 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
2853 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
2855 tree type
= TREE_TYPE (arg0
);
2857 /* Make sure shorter operand is extended the right way
2858 to match the longer operand. */
2859 primarg1
= fold_convert (signed_or_unsigned_type_for
2860 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
2862 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
2869 /* See if ARG is an expression that is either a comparison or is performing
2870 arithmetic on comparisons. The comparisons must only be comparing
2871 two different values, which will be stored in *CVAL1 and *CVAL2; if
2872 they are nonzero it means that some operands have already been found.
2873 No variables may be used anywhere else in the expression except in the
2874 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2875 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2877 If this is true, return 1. Otherwise, return zero. */
2880 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
2882 enum tree_code code
= TREE_CODE (arg
);
2883 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
2885 /* We can handle some of the tcc_expression cases here. */
2886 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
2888 else if (tclass
== tcc_expression
2889 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
2890 || code
== COMPOUND_EXPR
))
2891 tclass
= tcc_binary
;
2893 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
2894 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
2896 /* If we've already found a CVAL1 or CVAL2, this expression is
2897 two complex to handle. */
2898 if (*cval1
|| *cval2
)
2908 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
2911 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
2912 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2913 cval1
, cval2
, save_p
));
2918 case tcc_expression
:
2919 if (code
== COND_EXPR
)
2920 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
2921 cval1
, cval2
, save_p
)
2922 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2923 cval1
, cval2
, save_p
)
2924 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
2925 cval1
, cval2
, save_p
));
2928 case tcc_comparison
:
2929 /* First see if we can handle the first operand, then the second. For
2930 the second operand, we know *CVAL1 can't be zero. It must be that
2931 one side of the comparison is each of the values; test for the
2932 case where this isn't true by failing if the two operands
2935 if (operand_equal_p (TREE_OPERAND (arg
, 0),
2936 TREE_OPERAND (arg
, 1), 0))
2940 *cval1
= TREE_OPERAND (arg
, 0);
2941 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
2943 else if (*cval2
== 0)
2944 *cval2
= TREE_OPERAND (arg
, 0);
2945 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
2950 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
2952 else if (*cval2
== 0)
2953 *cval2
= TREE_OPERAND (arg
, 1);
2954 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
2966 /* ARG is a tree that is known to contain just arithmetic operations and
2967 comparisons. Evaluate the operations in the tree substituting NEW0 for
2968 any occurrence of OLD0 as an operand of a comparison and likewise for
2972 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
2973 tree old1
, tree new1
)
2975 tree type
= TREE_TYPE (arg
);
2976 enum tree_code code
= TREE_CODE (arg
);
2977 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
2979 /* We can handle some of the tcc_expression cases here. */
2980 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
2982 else if (tclass
== tcc_expression
2983 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2984 tclass
= tcc_binary
;
2989 return fold_build1_loc (loc
, code
, type
,
2990 eval_subst (loc
, TREE_OPERAND (arg
, 0),
2991 old0
, new0
, old1
, new1
));
2994 return fold_build2_loc (loc
, code
, type
,
2995 eval_subst (loc
, TREE_OPERAND (arg
, 0),
2996 old0
, new0
, old1
, new1
),
2997 eval_subst (loc
, TREE_OPERAND (arg
, 1),
2998 old0
, new0
, old1
, new1
));
3000 case tcc_expression
:
3004 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3008 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3012 return fold_build3_loc (loc
, code
, type
,
3013 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3014 old0
, new0
, old1
, new1
),
3015 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3016 old0
, new0
, old1
, new1
),
3017 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3018 old0
, new0
, old1
, new1
));
3022 /* Fall through - ??? */
3024 case tcc_comparison
:
3026 tree arg0
= TREE_OPERAND (arg
, 0);
3027 tree arg1
= TREE_OPERAND (arg
, 1);
3029 /* We need to check both for exact equality and tree equality. The
3030 former will be true if the operand has a side-effect. In that
3031 case, we know the operand occurred exactly once. */
3033 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3035 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3038 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3040 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3043 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3051 /* Return a tree for the case when the result of an expression is RESULT
3052 converted to TYPE and OMITTED was previously an operand of the expression
3053 but is now not needed (e.g., we folded OMITTED * 0).
3055 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3056 the conversion of RESULT to TYPE. */
3059 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3061 tree t
= fold_convert_loc (loc
, type
, result
);
3063 /* If the resulting operand is an empty statement, just return the omitted
3064 statement casted to void. */
3065 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3066 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3067 fold_ignored_result (omitted
));
3069 if (TREE_SIDE_EFFECTS (omitted
))
3070 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3071 fold_ignored_result (omitted
), t
);
3073 return non_lvalue_loc (loc
, t
);
3076 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3079 pedantic_omit_one_operand_loc (location_t loc
, tree type
, tree result
,
3082 tree t
= fold_convert_loc (loc
, type
, result
);
3084 /* If the resulting operand is an empty statement, just return the omitted
3085 statement casted to void. */
3086 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3087 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3088 fold_ignored_result (omitted
));
3090 if (TREE_SIDE_EFFECTS (omitted
))
3091 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3092 fold_ignored_result (omitted
), t
);
3094 return pedantic_non_lvalue_loc (loc
, t
);
3097 /* Return a tree for the case when the result of an expression is RESULT
3098 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3099 of the expression but are now not needed.
3101 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3102 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3103 evaluated before OMITTED2. Otherwise, if neither has side effects,
3104 just do the conversion of RESULT to TYPE. */
3107 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3108 tree omitted1
, tree omitted2
)
3110 tree t
= fold_convert_loc (loc
, type
, result
);
3112 if (TREE_SIDE_EFFECTS (omitted2
))
3113 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3114 if (TREE_SIDE_EFFECTS (omitted1
))
3115 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3117 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3121 /* Return a simplified tree node for the truth-negation of ARG. This
3122 never alters ARG itself. We assume that ARG is an operation that
3123 returns a truth value (0 or 1).
3125 FIXME: one would think we would fold the result, but it causes
3126 problems with the dominator optimizer. */
3129 fold_truth_not_expr (location_t loc
, tree arg
)
3131 tree type
= TREE_TYPE (arg
);
3132 enum tree_code code
= TREE_CODE (arg
);
3133 location_t loc1
, loc2
;
3135 /* If this is a comparison, we can simply invert it, except for
3136 floating-point non-equality comparisons, in which case we just
3137 enclose a TRUTH_NOT_EXPR around what we have. */
3139 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3141 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3142 if (FLOAT_TYPE_P (op_type
)
3143 && flag_trapping_math
3144 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3145 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3148 code
= invert_tree_comparison (code
, HONOR_NANS (TYPE_MODE (op_type
)));
3149 if (code
== ERROR_MARK
)
3152 return build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3153 TREE_OPERAND (arg
, 1));
3159 return constant_boolean_node (integer_zerop (arg
), type
);
3161 case TRUTH_AND_EXPR
:
3162 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3163 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3164 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3165 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3166 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3169 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3170 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3171 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3172 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3173 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3175 case TRUTH_XOR_EXPR
:
3176 /* Here we can invert either operand. We invert the first operand
3177 unless the second operand is a TRUTH_NOT_EXPR in which case our
3178 result is the XOR of the first operand with the inside of the
3179 negation of the second operand. */
3181 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3182 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3183 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3185 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3186 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3187 TREE_OPERAND (arg
, 1));
3189 case TRUTH_ANDIF_EXPR
:
3190 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3191 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3192 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3193 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3194 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3196 case TRUTH_ORIF_EXPR
:
3197 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3198 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3199 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3200 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3201 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3203 case TRUTH_NOT_EXPR
:
3204 return TREE_OPERAND (arg
, 0);
3208 tree arg1
= TREE_OPERAND (arg
, 1);
3209 tree arg2
= TREE_OPERAND (arg
, 2);
3211 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3212 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3214 /* A COND_EXPR may have a throw as one operand, which
3215 then has void type. Just leave void operands
3217 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3218 VOID_TYPE_P (TREE_TYPE (arg1
))
3219 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3220 VOID_TYPE_P (TREE_TYPE (arg2
))
3221 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3225 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3226 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3227 TREE_OPERAND (arg
, 0),
3228 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3230 case NON_LVALUE_EXPR
:
3231 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3232 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3235 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3236 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3238 /* ... fall through ... */
3241 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3242 return build1_loc (loc
, TREE_CODE (arg
), type
,
3243 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3246 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3248 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3251 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3253 case CLEANUP_POINT_EXPR
:
3254 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3255 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3256 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3263 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3264 assume that ARG is an operation that returns a truth value (0 or 1
3265 for scalars, 0 or -1 for vectors). Return the folded expression if
3266 folding is successful. Otherwise, return NULL_TREE. */
3269 fold_invert_truthvalue (location_t loc
, tree arg
)
3271 tree type
= TREE_TYPE (arg
);
3272 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3278 /* Return a simplified tree node for the truth-negation of ARG. This
3279 never alters ARG itself. We assume that ARG is an operation that
3280 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3283 invert_truthvalue_loc (location_t loc
, tree arg
)
3285 if (TREE_CODE (arg
) == ERROR_MARK
)
3288 tree type
= TREE_TYPE (arg
);
3289 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3295 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3296 operands are another bit-wise operation with a common input. If so,
3297 distribute the bit operations to save an operation and possibly two if
3298 constants are involved. For example, convert
3299 (A | B) & (A | C) into A | (B & C)
3300 Further simplification will occur if B and C are constants.
3302 If this optimization cannot be done, 0 will be returned. */
3305 distribute_bit_expr (location_t loc
, enum tree_code code
, tree type
,
3306 tree arg0
, tree arg1
)
3311 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3312 || TREE_CODE (arg0
) == code
3313 || (TREE_CODE (arg0
) != BIT_AND_EXPR
3314 && TREE_CODE (arg0
) != BIT_IOR_EXPR
))
3317 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0))
3319 common
= TREE_OPERAND (arg0
, 0);
3320 left
= TREE_OPERAND (arg0
, 1);
3321 right
= TREE_OPERAND (arg1
, 1);
3323 else if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1), 0))
3325 common
= TREE_OPERAND (arg0
, 0);
3326 left
= TREE_OPERAND (arg0
, 1);
3327 right
= TREE_OPERAND (arg1
, 0);
3329 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 0), 0))
3331 common
= TREE_OPERAND (arg0
, 1);
3332 left
= TREE_OPERAND (arg0
, 0);
3333 right
= TREE_OPERAND (arg1
, 1);
3335 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1), 0))
3337 common
= TREE_OPERAND (arg0
, 1);
3338 left
= TREE_OPERAND (arg0
, 0);
3339 right
= TREE_OPERAND (arg1
, 0);
3344 common
= fold_convert_loc (loc
, type
, common
);
3345 left
= fold_convert_loc (loc
, type
, left
);
3346 right
= fold_convert_loc (loc
, type
, right
);
3347 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, common
,
3348 fold_build2_loc (loc
, code
, type
, left
, right
));
3351 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3352 with code CODE. This optimization is unsafe. */
3354 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3355 tree arg0
, tree arg1
)
3357 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3358 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3360 /* (A / C) +- (B / C) -> (A +- B) / C. */
3362 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3363 TREE_OPERAND (arg1
, 1), 0))
3364 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3365 fold_build2_loc (loc
, code
, type
,
3366 TREE_OPERAND (arg0
, 0),
3367 TREE_OPERAND (arg1
, 0)),
3368 TREE_OPERAND (arg0
, 1));
3370 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3371 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3372 TREE_OPERAND (arg1
, 0), 0)
3373 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3374 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3376 REAL_VALUE_TYPE r0
, r1
;
3377 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3378 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3380 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3382 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3383 real_arithmetic (&r0
, code
, &r0
, &r1
);
3384 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3385 TREE_OPERAND (arg0
, 0),
3386 build_real (type
, r0
));
3392 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3393 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3396 make_bit_field_ref (location_t loc
, tree inner
, tree type
,
3397 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
, int unsignedp
)
3399 tree result
, bftype
;
3403 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3404 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3405 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3406 && tree_fits_shwi_p (size
)
3407 && tree_to_shwi (size
) == bitsize
)
3408 return fold_convert_loc (loc
, type
, inner
);
3412 if (TYPE_PRECISION (bftype
) != bitsize
3413 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3414 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3416 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3417 size_int (bitsize
), bitsize_int (bitpos
));
3420 result
= fold_convert_loc (loc
, type
, result
);
3425 /* Optimize a bit-field compare.
3427 There are two cases: First is a compare against a constant and the
3428 second is a comparison of two items where the fields are at the same
3429 bit position relative to the start of a chunk (byte, halfword, word)
3430 large enough to contain it. In these cases we can avoid the shift
3431 implicit in bitfield extractions.
3433 For constants, we emit a compare of the shifted constant with the
3434 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3435 compared. For two fields at the same position, we do the ANDs with the
3436 similar mask and compare the result of the ANDs.
3438 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3439 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3440 are the left and right operands of the comparison, respectively.
3442 If the optimization described above can be done, we return the resulting
3443 tree. Otherwise we return zero. */
3446 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3447 tree compare_type
, tree lhs
, tree rhs
)
3449 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3450 tree type
= TREE_TYPE (lhs
);
3451 tree signed_type
, unsigned_type
;
3452 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3453 enum machine_mode lmode
, rmode
, nmode
;
3454 int lunsignedp
, runsignedp
;
3455 int lvolatilep
= 0, rvolatilep
= 0;
3456 tree linner
, rinner
= NULL_TREE
;
3460 /* Get all the information about the extractions being done. If the bit size
3461 if the same as the size of the underlying object, we aren't doing an
3462 extraction at all and so can do nothing. We also don't want to
3463 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3464 then will no longer be able to replace it. */
3465 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3466 &lunsignedp
, &lvolatilep
, false);
3467 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3468 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3473 /* If this is not a constant, we can only do something if bit positions,
3474 sizes, and signedness are the same. */
3475 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3476 &runsignedp
, &rvolatilep
, false);
3478 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3479 || lunsignedp
!= runsignedp
|| offset
!= 0
3480 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3484 /* See if we can find a mode to refer to this field. We should be able to,
3485 but fail if we can't. */
3486 nmode
= get_best_mode (lbitsize
, lbitpos
, 0, 0,
3487 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3488 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3489 TYPE_ALIGN (TREE_TYPE (rinner
))),
3491 if (nmode
== VOIDmode
)
3494 /* Set signed and unsigned types of the precision of this mode for the
3496 signed_type
= lang_hooks
.types
.type_for_mode (nmode
, 0);
3497 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3499 /* Compute the bit position and size for the new reference and our offset
3500 within it. If the new reference is the same size as the original, we
3501 won't optimize anything, so return zero. */
3502 nbitsize
= GET_MODE_BITSIZE (nmode
);
3503 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3505 if (nbitsize
== lbitsize
)
3508 if (BYTES_BIG_ENDIAN
)
3509 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3511 /* Make the mask to be used against the extracted field. */
3512 mask
= build_int_cst_type (unsigned_type
, -1);
3513 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3514 mask
= const_binop (RSHIFT_EXPR
, mask
,
3515 size_int (nbitsize
- lbitsize
- lbitpos
));
3518 /* If not comparing with constant, just rework the comparison
3520 return fold_build2_loc (loc
, code
, compare_type
,
3521 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3522 make_bit_field_ref (loc
, linner
,
3527 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3528 make_bit_field_ref (loc
, rinner
,
3534 /* Otherwise, we are handling the constant case. See if the constant is too
3535 big for the field. Warn and return a tree of for 0 (false) if so. We do
3536 this not only for its own sake, but to avoid having to test for this
3537 error case below. If we didn't, we might generate wrong code.
3539 For unsigned fields, the constant shifted right by the field length should
3540 be all zero. For signed fields, the high-order bits should agree with
3545 if (! integer_zerop (const_binop (RSHIFT_EXPR
,
3546 fold_convert_loc (loc
,
3547 unsigned_type
, rhs
),
3548 size_int (lbitsize
))))
3550 warning (0, "comparison is always %d due to width of bit-field",
3552 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3557 tree tem
= const_binop (RSHIFT_EXPR
,
3558 fold_convert_loc (loc
, signed_type
, rhs
),
3559 size_int (lbitsize
- 1));
3560 if (! integer_zerop (tem
) && ! integer_all_onesp (tem
))
3562 warning (0, "comparison is always %d due to width of bit-field",
3564 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3568 /* Single-bit compares should always be against zero. */
3569 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3571 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3572 rhs
= build_int_cst (type
, 0);
3575 /* Make a new bitfield reference, shift the constant over the
3576 appropriate number of bits and mask it with the computed mask
3577 (in case this was a signed field). If we changed it, make a new one. */
3578 lhs
= make_bit_field_ref (loc
, linner
, unsigned_type
, nbitsize
, nbitpos
, 1);
3580 rhs
= const_binop (BIT_AND_EXPR
,
3581 const_binop (LSHIFT_EXPR
,
3582 fold_convert_loc (loc
, unsigned_type
, rhs
),
3583 size_int (lbitpos
)),
3586 lhs
= build2_loc (loc
, code
, compare_type
,
3587 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
3591 /* Subroutine for fold_truth_andor_1: decode a field reference.
3593 If EXP is a comparison reference, we return the innermost reference.
3595 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3596 set to the starting bit number.
3598 If the innermost field can be completely contained in a mode-sized
3599 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3601 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3602 otherwise it is not changed.
3604 *PUNSIGNEDP is set to the signedness of the field.
3606 *PMASK is set to the mask used. This is either contained in a
3607 BIT_AND_EXPR or derived from the width of the field.
3609 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3611 Return 0 if this is not a component reference or is one that we can't
3612 do anything with. */
3615 decode_field_reference (location_t loc
, tree exp
, HOST_WIDE_INT
*pbitsize
,
3616 HOST_WIDE_INT
*pbitpos
, enum machine_mode
*pmode
,
3617 int *punsignedp
, int *pvolatilep
,
3618 tree
*pmask
, tree
*pand_mask
)
3620 tree outer_type
= 0;
3622 tree mask
, inner
, offset
;
3624 unsigned int precision
;
3626 /* All the optimizations using this function assume integer fields.
3627 There are problems with FP fields since the type_for_size call
3628 below can fail for, e.g., XFmode. */
3629 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
3632 /* We are interested in the bare arrangement of bits, so strip everything
3633 that doesn't affect the machine mode. However, record the type of the
3634 outermost expression if it may matter below. */
3635 if (CONVERT_EXPR_P (exp
)
3636 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
3637 outer_type
= TREE_TYPE (exp
);
3640 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
3642 and_mask
= TREE_OPERAND (exp
, 1);
3643 exp
= TREE_OPERAND (exp
, 0);
3644 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
3645 if (TREE_CODE (and_mask
) != INTEGER_CST
)
3649 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
3650 punsignedp
, pvolatilep
, false);
3651 if ((inner
== exp
&& and_mask
== 0)
3652 || *pbitsize
< 0 || offset
!= 0
3653 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
3656 /* If the number of bits in the reference is the same as the bitsize of
3657 the outer type, then the outer type gives the signedness. Otherwise
3658 (in case of a small bitfield) the signedness is unchanged. */
3659 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
3660 *punsignedp
= TYPE_UNSIGNED (outer_type
);
3662 /* Compute the mask to access the bitfield. */
3663 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
3664 precision
= TYPE_PRECISION (unsigned_type
);
3666 mask
= build_int_cst_type (unsigned_type
, -1);
3668 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3669 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3671 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3673 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3674 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
3677 *pand_mask
= and_mask
;
3681 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3682 bit positions and MASK is SIGNED. */
3685 all_ones_mask_p (const_tree mask
, unsigned int size
)
3687 tree type
= TREE_TYPE (mask
);
3688 unsigned int precision
= TYPE_PRECISION (type
);
3690 /* If this function returns true when the type of the mask is
3691 UNSIGNED, then there will be errors. In particular see
3692 gcc.c-torture/execute/990326-1.c. There does not appear to be
3693 any documentation paper trail as to why this is so. But the pre
3694 wide-int worked with that restriction and it has been preserved
3696 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
3699 return wi::mask (size
, false, precision
) == mask
;
3702 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3703 represents the sign bit of EXP's type. If EXP represents a sign
3704 or zero extension, also test VAL against the unextended type.
3705 The return value is the (sub)expression whose sign bit is VAL,
3706 or NULL_TREE otherwise. */
3709 sign_bit_p (tree exp
, const_tree val
)
3714 /* Tree EXP must have an integral type. */
3715 t
= TREE_TYPE (exp
);
3716 if (! INTEGRAL_TYPE_P (t
))
3719 /* Tree VAL must be an integer constant. */
3720 if (TREE_CODE (val
) != INTEGER_CST
3721 || TREE_OVERFLOW (val
))
3724 width
= TYPE_PRECISION (t
);
3725 if (wi::only_sign_bit_p (val
, width
))
3728 /* Handle extension from a narrower type. */
3729 if (TREE_CODE (exp
) == NOP_EXPR
3730 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
3731 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
3736 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3737 to be evaluated unconditionally. */
3740 simple_operand_p (const_tree exp
)
3742 /* Strip any conversions that don't change the machine mode. */
3745 return (CONSTANT_CLASS_P (exp
)
3746 || TREE_CODE (exp
) == SSA_NAME
3748 && ! TREE_ADDRESSABLE (exp
)
3749 && ! TREE_THIS_VOLATILE (exp
)
3750 && ! DECL_NONLOCAL (exp
)
3751 /* Don't regard global variables as simple. They may be
3752 allocated in ways unknown to the compiler (shared memory,
3753 #pragma weak, etc). */
3754 && ! TREE_PUBLIC (exp
)
3755 && ! DECL_EXTERNAL (exp
)
3756 /* Weakrefs are not safe to be read, since they can be NULL.
3757 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3758 have DECL_WEAK flag set. */
3759 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
3760 /* Loading a static variable is unduly expensive, but global
3761 registers aren't expensive. */
3762 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
3765 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3766 to be evaluated unconditionally.
3767 I addition to simple_operand_p, we assume that comparisons, conversions,
3768 and logic-not operations are simple, if their operands are simple, too. */
3771 simple_operand_p_2 (tree exp
)
3773 enum tree_code code
;
3775 if (TREE_SIDE_EFFECTS (exp
)
3776 || tree_could_trap_p (exp
))
3779 while (CONVERT_EXPR_P (exp
))
3780 exp
= TREE_OPERAND (exp
, 0);
3782 code
= TREE_CODE (exp
);
3784 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3785 return (simple_operand_p (TREE_OPERAND (exp
, 0))
3786 && simple_operand_p (TREE_OPERAND (exp
, 1)));
3788 if (code
== TRUTH_NOT_EXPR
)
3789 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
3791 return simple_operand_p (exp
);
3795 /* The following functions are subroutines to fold_range_test and allow it to
3796 try to change a logical combination of comparisons into a range test.
3799 X == 2 || X == 3 || X == 4 || X == 5
3803 (unsigned) (X - 2) <= 3
3805 We describe each set of comparisons as being either inside or outside
3806 a range, using a variable named like IN_P, and then describe the
3807 range with a lower and upper bound. If one of the bounds is omitted,
3808 it represents either the highest or lowest value of the type.
3810 In the comments below, we represent a range by two numbers in brackets
3811 preceded by a "+" to designate being inside that range, or a "-" to
3812 designate being outside that range, so the condition can be inverted by
3813 flipping the prefix. An omitted bound is represented by a "-". For
3814 example, "- [-, 10]" means being outside the range starting at the lowest
3815 possible value and ending at 10, in other words, being greater than 10.
3816 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3819 We set up things so that the missing bounds are handled in a consistent
3820 manner so neither a missing bound nor "true" and "false" need to be
3821 handled using a special case. */
3823 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3824 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3825 and UPPER1_P are nonzero if the respective argument is an upper bound
3826 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3827 must be specified for a comparison. ARG1 will be converted to ARG0's
3828 type if both are specified. */
3831 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
3832 tree arg1
, int upper1_p
)
3838 /* If neither arg represents infinity, do the normal operation.
3839 Else, if not a comparison, return infinity. Else handle the special
3840 comparison rules. Note that most of the cases below won't occur, but
3841 are handled for consistency. */
3843 if (arg0
!= 0 && arg1
!= 0)
3845 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
3846 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
3848 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
3851 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
3854 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3855 for neither. In real maths, we cannot assume open ended ranges are
3856 the same. But, this is computer arithmetic, where numbers are finite.
3857 We can therefore make the transformation of any unbounded range with
3858 the value Z, Z being greater than any representable number. This permits
3859 us to treat unbounded ranges as equal. */
3860 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
3861 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
3865 result
= sgn0
== sgn1
;
3868 result
= sgn0
!= sgn1
;
3871 result
= sgn0
< sgn1
;
3874 result
= sgn0
<= sgn1
;
3877 result
= sgn0
> sgn1
;
3880 result
= sgn0
>= sgn1
;
3886 return constant_boolean_node (result
, type
);
3889 /* Helper routine for make_range. Perform one step for it, return
3890 new expression if the loop should continue or NULL_TREE if it should
3894 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
3895 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
3896 bool *strict_overflow_p
)
3898 tree arg0_type
= TREE_TYPE (arg0
);
3899 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
3900 int in_p
= *p_in_p
, n_in_p
;
3904 case TRUTH_NOT_EXPR
:
3905 /* We can only do something if the range is testing for zero. */
3906 if (low
== NULL_TREE
|| high
== NULL_TREE
3907 || ! integer_zerop (low
) || ! integer_zerop (high
))
3912 case EQ_EXPR
: case NE_EXPR
:
3913 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
3914 /* We can only do something if the range is testing for zero
3915 and if the second operand is an integer constant. Note that
3916 saying something is "in" the range we make is done by
3917 complementing IN_P since it will set in the initial case of
3918 being not equal to zero; "out" is leaving it alone. */
3919 if (low
== NULL_TREE
|| high
== NULL_TREE
3920 || ! integer_zerop (low
) || ! integer_zerop (high
)
3921 || TREE_CODE (arg1
) != INTEGER_CST
)
3926 case NE_EXPR
: /* - [c, c] */
3929 case EQ_EXPR
: /* + [c, c] */
3930 in_p
= ! in_p
, low
= high
= arg1
;
3932 case GT_EXPR
: /* - [-, c] */
3933 low
= 0, high
= arg1
;
3935 case GE_EXPR
: /* + [c, -] */
3936 in_p
= ! in_p
, low
= arg1
, high
= 0;
3938 case LT_EXPR
: /* - [c, -] */
3939 low
= arg1
, high
= 0;
3941 case LE_EXPR
: /* + [-, c] */
3942 in_p
= ! in_p
, low
= 0, high
= arg1
;
3948 /* If this is an unsigned comparison, we also know that EXP is
3949 greater than or equal to zero. We base the range tests we make
3950 on that fact, so we record it here so we can parse existing
3951 range tests. We test arg0_type since often the return type
3952 of, e.g. EQ_EXPR, is boolean. */
3953 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
3955 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
3957 build_int_cst (arg0_type
, 0),
3961 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
3963 /* If the high bound is missing, but we have a nonzero low
3964 bound, reverse the range so it goes from zero to the low bound
3966 if (high
== 0 && low
&& ! integer_zerop (low
))
3969 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
3970 build_int_cst (TREE_TYPE (low
), 1), 0);
3971 low
= build_int_cst (arg0_type
, 0);
3981 /* If flag_wrapv and ARG0_TYPE is signed, make sure
3982 low and high are non-NULL, then normalize will DTRT. */
3983 if (!TYPE_UNSIGNED (arg0_type
)
3984 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
3986 if (low
== NULL_TREE
)
3987 low
= TYPE_MIN_VALUE (arg0_type
);
3988 if (high
== NULL_TREE
)
3989 high
= TYPE_MAX_VALUE (arg0_type
);
3992 /* (-x) IN [a,b] -> x in [-b, -a] */
3993 n_low
= range_binop (MINUS_EXPR
, exp_type
,
3994 build_int_cst (exp_type
, 0),
3996 n_high
= range_binop (MINUS_EXPR
, exp_type
,
3997 build_int_cst (exp_type
, 0),
3999 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4005 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4006 build_int_cst (exp_type
, 1));
4010 if (TREE_CODE (arg1
) != INTEGER_CST
)
4013 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4014 move a constant to the other side. */
4015 if (!TYPE_UNSIGNED (arg0_type
)
4016 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4019 /* If EXP is signed, any overflow in the computation is undefined,
4020 so we don't worry about it so long as our computations on
4021 the bounds don't overflow. For unsigned, overflow is defined
4022 and this is exactly the right thing. */
4023 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4024 arg0_type
, low
, 0, arg1
, 0);
4025 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4026 arg0_type
, high
, 1, arg1
, 0);
4027 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4028 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4031 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4032 *strict_overflow_p
= true;
4035 /* Check for an unsigned range which has wrapped around the maximum
4036 value thus making n_high < n_low, and normalize it. */
4037 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4039 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4040 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4041 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4042 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4044 /* If the range is of the form +/- [ x+1, x ], we won't
4045 be able to normalize it. But then, it represents the
4046 whole range or the empty set, so make it
4048 if (tree_int_cst_equal (n_low
, low
)
4049 && tree_int_cst_equal (n_high
, high
))
4055 low
= n_low
, high
= n_high
;
4063 case NON_LVALUE_EXPR
:
4064 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4067 if (! INTEGRAL_TYPE_P (arg0_type
)
4068 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4069 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4072 n_low
= low
, n_high
= high
;
4075 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4078 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4080 /* If we're converting arg0 from an unsigned type, to exp,
4081 a signed type, we will be doing the comparison as unsigned.
4082 The tests above have already verified that LOW and HIGH
4085 So we have to ensure that we will handle large unsigned
4086 values the same way that the current signed bounds treat
4089 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4093 /* For fixed-point modes, we need to pass the saturating flag
4094 as the 2nd parameter. */
4095 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4097 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4098 TYPE_SATURATING (arg0_type
));
4101 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4103 /* A range without an upper bound is, naturally, unbounded.
4104 Since convert would have cropped a very large value, use
4105 the max value for the destination type. */
4107 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4108 : TYPE_MAX_VALUE (arg0_type
);
4110 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4111 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4112 fold_convert_loc (loc
, arg0_type
,
4114 build_int_cst (arg0_type
, 1));
4116 /* If the low bound is specified, "and" the range with the
4117 range for which the original unsigned value will be
4121 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4122 1, fold_convert_loc (loc
, arg0_type
,
4127 in_p
= (n_in_p
== in_p
);
4131 /* Otherwise, "or" the range with the range of the input
4132 that will be interpreted as negative. */
4133 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4134 1, fold_convert_loc (loc
, arg0_type
,
4139 in_p
= (in_p
!= n_in_p
);
4153 /* Given EXP, a logical expression, set the range it is testing into
4154 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4155 actually being tested. *PLOW and *PHIGH will be made of the same
4156 type as the returned expression. If EXP is not a comparison, we
4157 will most likely not be returning a useful value and range. Set
4158 *STRICT_OVERFLOW_P to true if the return value is only valid
4159 because signed overflow is undefined; otherwise, do not change
4160 *STRICT_OVERFLOW_P. */
4163 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4164 bool *strict_overflow_p
)
4166 enum tree_code code
;
4167 tree arg0
, arg1
= NULL_TREE
;
4168 tree exp_type
, nexp
;
4171 location_t loc
= EXPR_LOCATION (exp
);
4173 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4174 and see if we can refine the range. Some of the cases below may not
4175 happen, but it doesn't seem worth worrying about this. We "continue"
4176 the outer loop when we've changed something; otherwise we "break"
4177 the switch, which will "break" the while. */
4180 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4184 code
= TREE_CODE (exp
);
4185 exp_type
= TREE_TYPE (exp
);
4188 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4190 if (TREE_OPERAND_LENGTH (exp
) > 0)
4191 arg0
= TREE_OPERAND (exp
, 0);
4192 if (TREE_CODE_CLASS (code
) == tcc_binary
4193 || TREE_CODE_CLASS (code
) == tcc_comparison
4194 || (TREE_CODE_CLASS (code
) == tcc_expression
4195 && TREE_OPERAND_LENGTH (exp
) > 1))
4196 arg1
= TREE_OPERAND (exp
, 1);
4198 if (arg0
== NULL_TREE
)
4201 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4202 &high
, &in_p
, strict_overflow_p
);
4203 if (nexp
== NULL_TREE
)
4208 /* If EXP is a constant, we can evaluate whether this is true or false. */
4209 if (TREE_CODE (exp
) == INTEGER_CST
)
4211 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4213 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4219 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4223 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4224 type, TYPE, return an expression to test if EXP is in (or out of, depending
4225 on IN_P) the range. Return 0 if the test couldn't be created. */
4228 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4229 tree low
, tree high
)
4231 tree etype
= TREE_TYPE (exp
), value
;
4233 #ifdef HAVE_canonicalize_funcptr_for_compare
4234 /* Disable this optimization for function pointer expressions
4235 on targets that require function pointer canonicalization. */
4236 if (HAVE_canonicalize_funcptr_for_compare
4237 && TREE_CODE (etype
) == POINTER_TYPE
4238 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4244 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4246 return invert_truthvalue_loc (loc
, value
);
4251 if (low
== 0 && high
== 0)
4252 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4255 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4256 fold_convert_loc (loc
, etype
, high
));
4259 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4260 fold_convert_loc (loc
, etype
, low
));
4262 if (operand_equal_p (low
, high
, 0))
4263 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4264 fold_convert_loc (loc
, etype
, low
));
4266 if (integer_zerop (low
))
4268 if (! TYPE_UNSIGNED (etype
))
4270 etype
= unsigned_type_for (etype
);
4271 high
= fold_convert_loc (loc
, etype
, high
);
4272 exp
= fold_convert_loc (loc
, etype
, exp
);
4274 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4277 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4278 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4280 int prec
= TYPE_PRECISION (etype
);
4281 wide_int osb
= wi::set_bit_in_zero (prec
- 1, prec
) - 1;
4285 if (TYPE_UNSIGNED (etype
))
4287 tree signed_etype
= signed_type_for (etype
);
4288 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4290 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4292 etype
= signed_etype
;
4293 exp
= fold_convert_loc (loc
, etype
, exp
);
4295 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4296 build_int_cst (etype
, 0));
4300 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4301 This requires wrap-around arithmetics for the type of the expression.
4302 First make sure that arithmetics in this type is valid, then make sure
4303 that it wraps around. */
4304 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4305 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4306 TYPE_UNSIGNED (etype
));
4308 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4310 tree utype
, minv
, maxv
;
4312 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4313 for the type in question, as we rely on this here. */
4314 utype
= unsigned_type_for (etype
);
4315 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4316 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4317 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4318 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4320 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4327 high
= fold_convert_loc (loc
, etype
, high
);
4328 low
= fold_convert_loc (loc
, etype
, low
);
4329 exp
= fold_convert_loc (loc
, etype
, exp
);
4331 value
= const_binop (MINUS_EXPR
, high
, low
);
4334 if (POINTER_TYPE_P (etype
))
4336 if (value
!= 0 && !TREE_OVERFLOW (value
))
4338 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4339 return build_range_check (loc
, type
,
4340 fold_build_pointer_plus_loc (loc
, exp
, low
),
4341 1, build_int_cst (etype
, 0), value
);
4346 if (value
!= 0 && !TREE_OVERFLOW (value
))
4347 return build_range_check (loc
, type
,
4348 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4349 1, build_int_cst (etype
, 0), value
);
4354 /* Return the predecessor of VAL in its type, handling the infinite case. */
4357 range_predecessor (tree val
)
4359 tree type
= TREE_TYPE (val
);
4361 if (INTEGRAL_TYPE_P (type
)
4362 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4365 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4366 build_int_cst (TREE_TYPE (val
), 1), 0);
4369 /* Return the successor of VAL in its type, handling the infinite case. */
4372 range_successor (tree val
)
4374 tree type
= TREE_TYPE (val
);
4376 if (INTEGRAL_TYPE_P (type
)
4377 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4380 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4381 build_int_cst (TREE_TYPE (val
), 1), 0);
4384 /* Given two ranges, see if we can merge them into one. Return 1 if we
4385 can, 0 if we can't. Set the output range into the specified parameters. */
4388 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4389 tree high0
, int in1_p
, tree low1
, tree high1
)
4397 int lowequal
= ((low0
== 0 && low1
== 0)
4398 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4399 low0
, 0, low1
, 0)));
4400 int highequal
= ((high0
== 0 && high1
== 0)
4401 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4402 high0
, 1, high1
, 1)));
4404 /* Make range 0 be the range that starts first, or ends last if they
4405 start at the same value. Swap them if it isn't. */
4406 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4409 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4410 high1
, 1, high0
, 1))))
4412 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4413 tem
= low0
, low0
= low1
, low1
= tem
;
4414 tem
= high0
, high0
= high1
, high1
= tem
;
4417 /* Now flag two cases, whether the ranges are disjoint or whether the
4418 second range is totally subsumed in the first. Note that the tests
4419 below are simplified by the ones above. */
4420 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4421 high0
, 1, low1
, 0));
4422 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4423 high1
, 1, high0
, 1));
4425 /* We now have four cases, depending on whether we are including or
4426 excluding the two ranges. */
4429 /* If they don't overlap, the result is false. If the second range
4430 is a subset it is the result. Otherwise, the range is from the start
4431 of the second to the end of the first. */
4433 in_p
= 0, low
= high
= 0;
4435 in_p
= 1, low
= low1
, high
= high1
;
4437 in_p
= 1, low
= low1
, high
= high0
;
4440 else if (in0_p
&& ! in1_p
)
4442 /* If they don't overlap, the result is the first range. If they are
4443 equal, the result is false. If the second range is a subset of the
4444 first, and the ranges begin at the same place, we go from just after
4445 the end of the second range to the end of the first. If the second
4446 range is not a subset of the first, or if it is a subset and both
4447 ranges end at the same place, the range starts at the start of the
4448 first range and ends just before the second range.
4449 Otherwise, we can't describe this as a single range. */
4451 in_p
= 1, low
= low0
, high
= high0
;
4452 else if (lowequal
&& highequal
)
4453 in_p
= 0, low
= high
= 0;
4454 else if (subset
&& lowequal
)
4456 low
= range_successor (high1
);
4461 /* We are in the weird situation where high0 > high1 but
4462 high1 has no successor. Punt. */
4466 else if (! subset
|| highequal
)
4469 high
= range_predecessor (low1
);
4473 /* low0 < low1 but low1 has no predecessor. Punt. */
4481 else if (! in0_p
&& in1_p
)
4483 /* If they don't overlap, the result is the second range. If the second
4484 is a subset of the first, the result is false. Otherwise,
4485 the range starts just after the first range and ends at the
4486 end of the second. */
4488 in_p
= 1, low
= low1
, high
= high1
;
4489 else if (subset
|| highequal
)
4490 in_p
= 0, low
= high
= 0;
4493 low
= range_successor (high0
);
4498 /* high1 > high0 but high0 has no successor. Punt. */
4506 /* The case where we are excluding both ranges. Here the complex case
4507 is if they don't overlap. In that case, the only time we have a
4508 range is if they are adjacent. If the second is a subset of the
4509 first, the result is the first. Otherwise, the range to exclude
4510 starts at the beginning of the first range and ends at the end of the
4514 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4515 range_successor (high0
),
4517 in_p
= 0, low
= low0
, high
= high1
;
4520 /* Canonicalize - [min, x] into - [-, x]. */
4521 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
4522 switch (TREE_CODE (TREE_TYPE (low0
)))
4525 if (TYPE_PRECISION (TREE_TYPE (low0
))
4526 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
4530 if (tree_int_cst_equal (low0
,
4531 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
4535 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
4536 && integer_zerop (low0
))
4543 /* Canonicalize - [x, max] into - [x, -]. */
4544 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
4545 switch (TREE_CODE (TREE_TYPE (high1
)))
4548 if (TYPE_PRECISION (TREE_TYPE (high1
))
4549 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
4553 if (tree_int_cst_equal (high1
,
4554 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
4558 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
4559 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
4561 build_int_cst (TREE_TYPE (high1
), 1),
4569 /* The ranges might be also adjacent between the maximum and
4570 minimum values of the given type. For
4571 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4572 return + [x + 1, y - 1]. */
4573 if (low0
== 0 && high1
== 0)
4575 low
= range_successor (high0
);
4576 high
= range_predecessor (low1
);
4577 if (low
== 0 || high
== 0)
4587 in_p
= 0, low
= low0
, high
= high0
;
4589 in_p
= 0, low
= low0
, high
= high1
;
4592 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4597 /* Subroutine of fold, looking inside expressions of the form
4598 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4599 of the COND_EXPR. This function is being used also to optimize
4600 A op B ? C : A, by reversing the comparison first.
4602 Return a folded expression whose code is not a COND_EXPR
4603 anymore, or NULL_TREE if no folding opportunity is found. */
4606 fold_cond_expr_with_comparison (location_t loc
, tree type
,
4607 tree arg0
, tree arg1
, tree arg2
)
4609 enum tree_code comp_code
= TREE_CODE (arg0
);
4610 tree arg00
= TREE_OPERAND (arg0
, 0);
4611 tree arg01
= TREE_OPERAND (arg0
, 1);
4612 tree arg1_type
= TREE_TYPE (arg1
);
4618 /* If we have A op 0 ? A : -A, consider applying the following
4621 A == 0? A : -A same as -A
4622 A != 0? A : -A same as A
4623 A >= 0? A : -A same as abs (A)
4624 A > 0? A : -A same as abs (A)
4625 A <= 0? A : -A same as -abs (A)
4626 A < 0? A : -A same as -abs (A)
4628 None of these transformations work for modes with signed
4629 zeros. If A is +/-0, the first two transformations will
4630 change the sign of the result (from +0 to -0, or vice
4631 versa). The last four will fix the sign of the result,
4632 even though the original expressions could be positive or
4633 negative, depending on the sign of A.
4635 Note that all these transformations are correct if A is
4636 NaN, since the two alternatives (A and -A) are also NaNs. */
4637 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
4638 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
4639 ? real_zerop (arg01
)
4640 : integer_zerop (arg01
))
4641 && ((TREE_CODE (arg2
) == NEGATE_EXPR
4642 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
4643 /* In the case that A is of the form X-Y, '-A' (arg2) may
4644 have already been folded to Y-X, check for that. */
4645 || (TREE_CODE (arg1
) == MINUS_EXPR
4646 && TREE_CODE (arg2
) == MINUS_EXPR
4647 && operand_equal_p (TREE_OPERAND (arg1
, 0),
4648 TREE_OPERAND (arg2
, 1), 0)
4649 && operand_equal_p (TREE_OPERAND (arg1
, 1),
4650 TREE_OPERAND (arg2
, 0), 0))))
4655 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
4656 return pedantic_non_lvalue_loc (loc
,
4657 fold_convert_loc (loc
, type
,
4658 negate_expr (tem
)));
4661 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4664 if (flag_trapping_math
)
4669 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4670 arg1
= fold_convert_loc (loc
, signed_type_for
4671 (TREE_TYPE (arg1
)), arg1
);
4672 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4673 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
4676 if (flag_trapping_math
)
4680 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4681 arg1
= fold_convert_loc (loc
, signed_type_for
4682 (TREE_TYPE (arg1
)), arg1
);
4683 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4684 return negate_expr (fold_convert_loc (loc
, type
, tem
));
4686 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
4690 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4691 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4692 both transformations are correct when A is NaN: A != 0
4693 is then true, and A == 0 is false. */
4695 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
4696 && integer_zerop (arg01
) && integer_zerop (arg2
))
4698 if (comp_code
== NE_EXPR
)
4699 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4700 else if (comp_code
== EQ_EXPR
)
4701 return build_zero_cst (type
);
4704 /* Try some transformations of A op B ? A : B.
4706 A == B? A : B same as B
4707 A != B? A : B same as A
4708 A >= B? A : B same as max (A, B)
4709 A > B? A : B same as max (B, A)
4710 A <= B? A : B same as min (A, B)
4711 A < B? A : B same as min (B, A)
4713 As above, these transformations don't work in the presence
4714 of signed zeros. For example, if A and B are zeros of
4715 opposite sign, the first two transformations will change
4716 the sign of the result. In the last four, the original
4717 expressions give different results for (A=+0, B=-0) and
4718 (A=-0, B=+0), but the transformed expressions do not.
4720 The first two transformations are correct if either A or B
4721 is a NaN. In the first transformation, the condition will
4722 be false, and B will indeed be chosen. In the case of the
4723 second transformation, the condition A != B will be true,
4724 and A will be chosen.
4726 The conversions to max() and min() are not correct if B is
4727 a number and A is not. The conditions in the original
4728 expressions will be false, so all four give B. The min()
4729 and max() versions would give a NaN instead. */
4730 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
4731 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
4732 /* Avoid these transformations if the COND_EXPR may be used
4733 as an lvalue in the C++ front-end. PR c++/19199. */
4735 || VECTOR_TYPE_P (type
)
4736 || (strcmp (lang_hooks
.name
, "GNU C++") != 0
4737 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
4738 || ! maybe_lvalue_p (arg1
)
4739 || ! maybe_lvalue_p (arg2
)))
4741 tree comp_op0
= arg00
;
4742 tree comp_op1
= arg01
;
4743 tree comp_type
= TREE_TYPE (comp_op0
);
4745 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4746 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
4756 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg2
));
4758 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4763 /* In C++ a ?: expression can be an lvalue, so put the
4764 operand which will be used if they are equal first
4765 so that we can convert this back to the
4766 corresponding COND_EXPR. */
4767 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
4769 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
4770 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
4771 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
4772 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
4773 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
4774 comp_op1
, comp_op0
);
4775 return pedantic_non_lvalue_loc (loc
,
4776 fold_convert_loc (loc
, type
, tem
));
4783 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
4785 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
4786 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
4787 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
4788 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
4789 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
4790 comp_op1
, comp_op0
);
4791 return pedantic_non_lvalue_loc (loc
,
4792 fold_convert_loc (loc
, type
, tem
));
4796 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
4797 return pedantic_non_lvalue_loc (loc
,
4798 fold_convert_loc (loc
, type
, arg2
));
4801 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
))))
4802 return pedantic_non_lvalue_loc (loc
,
4803 fold_convert_loc (loc
, type
, arg1
));
4806 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
4811 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4812 we might still be able to simplify this. For example,
4813 if C1 is one less or one more than C2, this might have started
4814 out as a MIN or MAX and been transformed by this function.
4815 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4817 if (INTEGRAL_TYPE_P (type
)
4818 && TREE_CODE (arg01
) == INTEGER_CST
4819 && TREE_CODE (arg2
) == INTEGER_CST
)
4823 if (TREE_CODE (arg1
) == INTEGER_CST
)
4825 /* We can replace A with C1 in this case. */
4826 arg1
= fold_convert_loc (loc
, type
, arg01
);
4827 return fold_build3_loc (loc
, COND_EXPR
, type
, arg0
, arg1
, arg2
);
4830 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4831 MIN_EXPR, to preserve the signedness of the comparison. */
4832 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
4834 && operand_equal_p (arg01
,
4835 const_binop (PLUS_EXPR
, arg2
,
4836 build_int_cst (type
, 1)),
4839 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
4840 fold_convert_loc (loc
, TREE_TYPE (arg00
),
4842 return pedantic_non_lvalue_loc (loc
,
4843 fold_convert_loc (loc
, type
, tem
));
4848 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4850 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
4852 && operand_equal_p (arg01
,
4853 const_binop (MINUS_EXPR
, arg2
,
4854 build_int_cst (type
, 1)),
4857 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
4858 fold_convert_loc (loc
, TREE_TYPE (arg00
),
4860 return pedantic_non_lvalue_loc (loc
,
4861 fold_convert_loc (loc
, type
, tem
));
4866 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4867 MAX_EXPR, to preserve the signedness of the comparison. */
4868 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
4870 && operand_equal_p (arg01
,
4871 const_binop (MINUS_EXPR
, arg2
,
4872 build_int_cst (type
, 1)),
4875 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
4876 fold_convert_loc (loc
, TREE_TYPE (arg00
),
4878 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
4883 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4884 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
4886 && operand_equal_p (arg01
,
4887 const_binop (PLUS_EXPR
, arg2
,
4888 build_int_cst (type
, 1)),
4891 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
4892 fold_convert_loc (loc
, TREE_TYPE (arg00
),
4894 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
4908 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4909 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4910 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4914 /* EXP is some logical combination of boolean tests. See if we can
4915 merge it into some range test. Return the new tree if so. */
4918 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
4921 int or_op
= (code
== TRUTH_ORIF_EXPR
4922 || code
== TRUTH_OR_EXPR
);
4923 int in0_p
, in1_p
, in_p
;
4924 tree low0
, low1
, low
, high0
, high1
, high
;
4925 bool strict_overflow_p
= false;
4927 const char * const warnmsg
= G_("assuming signed overflow does not occur "
4928 "when simplifying range test");
4930 if (!INTEGRAL_TYPE_P (type
))
4933 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
4934 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
4936 /* If this is an OR operation, invert both sides; we will invert
4937 again at the end. */
4939 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
4941 /* If both expressions are the same, if we can merge the ranges, and we
4942 can build the range test, return it or it inverted. If one of the
4943 ranges is always true or always false, consider it to be the same
4944 expression as the other. */
4945 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
4946 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
4948 && 0 != (tem
= (build_range_check (loc
, type
,
4950 : rhs
!= 0 ? rhs
: integer_zero_node
,
4953 if (strict_overflow_p
)
4954 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
4955 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
4958 /* On machines where the branch cost is expensive, if this is a
4959 short-circuited branch and the underlying object on both sides
4960 is the same, make a non-short-circuit operation. */
4961 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4962 && lhs
!= 0 && rhs
!= 0
4963 && (code
== TRUTH_ANDIF_EXPR
4964 || code
== TRUTH_ORIF_EXPR
)
4965 && operand_equal_p (lhs
, rhs
, 0))
4967 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4968 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4969 which cases we can't do this. */
4970 if (simple_operand_p (lhs
))
4971 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
4972 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
4975 else if (!lang_hooks
.decls
.global_bindings_p ()
4976 && !CONTAINS_PLACEHOLDER_P (lhs
))
4978 tree common
= save_expr (lhs
);
4980 if (0 != (lhs
= build_range_check (loc
, type
, common
,
4981 or_op
? ! in0_p
: in0_p
,
4983 && (0 != (rhs
= build_range_check (loc
, type
, common
,
4984 or_op
? ! in1_p
: in1_p
,
4987 if (strict_overflow_p
)
4988 fold_overflow_warning (warnmsg
,
4989 WARN_STRICT_OVERFLOW_COMPARISON
);
4990 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
4991 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5000 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5001 bit value. Arrange things so the extra bits will be set to zero if and
5002 only if C is signed-extended to its full width. If MASK is nonzero,
5003 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5006 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5008 tree type
= TREE_TYPE (c
);
5009 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5012 if (p
== modesize
|| unsignedp
)
5015 /* We work by getting just the sign bit into the low-order bit, then
5016 into the high-order bit, then sign-extend. We then XOR that value
5018 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5020 /* We must use a signed type in order to get an arithmetic right shift.
5021 However, we must also avoid introducing accidental overflows, so that
5022 a subsequent call to integer_zerop will work. Hence we must
5023 do the type conversion here. At this point, the constant is either
5024 zero or one, and the conversion to a signed type can never overflow.
5025 We could get an overflow if this conversion is done anywhere else. */
5026 if (TYPE_UNSIGNED (type
))
5027 temp
= fold_convert (signed_type_for (type
), temp
);
5029 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5030 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5032 temp
= const_binop (BIT_AND_EXPR
, temp
,
5033 fold_convert (TREE_TYPE (c
), mask
));
5034 /* If necessary, convert the type back to match the type of C. */
5035 if (TYPE_UNSIGNED (type
))
5036 temp
= fold_convert (type
, temp
);
5038 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5041 /* For an expression that has the form
5045 we can drop one of the inner expressions and simplify to
5049 LOC is the location of the resulting expression. OP is the inner
5050 logical operation; the left-hand side in the examples above, while CMPOP
5051 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5052 removing a condition that guards another, as in
5053 (A != NULL && A->...) || A == NULL
5054 which we must not transform. If RHS_ONLY is true, only eliminate the
5055 right-most operand of the inner logical operation. */
5058 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5061 tree type
= TREE_TYPE (cmpop
);
5062 enum tree_code code
= TREE_CODE (cmpop
);
5063 enum tree_code truthop_code
= TREE_CODE (op
);
5064 tree lhs
= TREE_OPERAND (op
, 0);
5065 tree rhs
= TREE_OPERAND (op
, 1);
5066 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5067 enum tree_code rhs_code
= TREE_CODE (rhs
);
5068 enum tree_code lhs_code
= TREE_CODE (lhs
);
5069 enum tree_code inv_code
;
5071 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5074 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5077 if (rhs_code
== truthop_code
)
5079 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5080 if (newrhs
!= NULL_TREE
)
5083 rhs_code
= TREE_CODE (rhs
);
5086 if (lhs_code
== truthop_code
&& !rhs_only
)
5088 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5089 if (newlhs
!= NULL_TREE
)
5092 lhs_code
= TREE_CODE (lhs
);
5096 inv_code
= invert_tree_comparison (code
, HONOR_NANS (TYPE_MODE (type
)));
5097 if (inv_code
== rhs_code
5098 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5099 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5101 if (!rhs_only
&& inv_code
== lhs_code
5102 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5103 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5105 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5106 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5111 /* Find ways of folding logical expressions of LHS and RHS:
5112 Try to merge two comparisons to the same innermost item.
5113 Look for range tests like "ch >= '0' && ch <= '9'".
5114 Look for combinations of simple terms on machines with expensive branches
5115 and evaluate the RHS unconditionally.
5117 For example, if we have p->a == 2 && p->b == 4 and we can make an
5118 object large enough to span both A and B, we can do this with a comparison
5119 against the object ANDed with the a mask.
5121 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5122 operations to do this with one comparison.
5124 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5125 function and the one above.
5127 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5128 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5130 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5133 We return the simplified tree or 0 if no optimization is possible. */
5136 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5139 /* If this is the "or" of two comparisons, we can do something if
5140 the comparisons are NE_EXPR. If this is the "and", we can do something
5141 if the comparisons are EQ_EXPR. I.e.,
5142 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5144 WANTED_CODE is this operation code. For single bit fields, we can
5145 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5146 comparison for one-bit fields. */
5148 enum tree_code wanted_code
;
5149 enum tree_code lcode
, rcode
;
5150 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5151 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5152 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5153 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5154 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5155 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5156 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5157 enum machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5158 enum machine_mode lnmode
, rnmode
;
5159 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5160 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5161 tree l_const
, r_const
;
5162 tree lntype
, rntype
, result
;
5163 HOST_WIDE_INT first_bit
, end_bit
;
5166 /* Start by getting the comparison codes. Fail if anything is volatile.
5167 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5168 it were surrounded with a NE_EXPR. */
5170 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5173 lcode
= TREE_CODE (lhs
);
5174 rcode
= TREE_CODE (rhs
);
5176 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5178 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5179 build_int_cst (TREE_TYPE (lhs
), 0));
5183 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5185 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5186 build_int_cst (TREE_TYPE (rhs
), 0));
5190 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5191 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5194 ll_arg
= TREE_OPERAND (lhs
, 0);
5195 lr_arg
= TREE_OPERAND (lhs
, 1);
5196 rl_arg
= TREE_OPERAND (rhs
, 0);
5197 rr_arg
= TREE_OPERAND (rhs
, 1);
5199 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5200 if (simple_operand_p (ll_arg
)
5201 && simple_operand_p (lr_arg
))
5203 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5204 && operand_equal_p (lr_arg
, rr_arg
, 0))
5206 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5207 truth_type
, ll_arg
, lr_arg
);
5211 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5212 && operand_equal_p (lr_arg
, rl_arg
, 0))
5214 result
= combine_comparisons (loc
, code
, lcode
,
5215 swap_tree_comparison (rcode
),
5216 truth_type
, ll_arg
, lr_arg
);
5222 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5223 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5225 /* If the RHS can be evaluated unconditionally and its operands are
5226 simple, it wins to evaluate the RHS unconditionally on machines
5227 with expensive branches. In this case, this isn't a comparison
5228 that can be merged. */
5230 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5232 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5233 && simple_operand_p (rl_arg
)
5234 && simple_operand_p (rr_arg
))
5236 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5237 if (code
== TRUTH_OR_EXPR
5238 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5239 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5240 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5241 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5242 return build2_loc (loc
, NE_EXPR
, truth_type
,
5243 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5245 build_int_cst (TREE_TYPE (ll_arg
), 0));
5247 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5248 if (code
== TRUTH_AND_EXPR
5249 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5250 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5251 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5252 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5253 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5254 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5256 build_int_cst (TREE_TYPE (ll_arg
), 0));
5259 /* See if the comparisons can be merged. Then get all the parameters for
5262 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5263 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5267 ll_inner
= decode_field_reference (loc
, ll_arg
,
5268 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5269 &ll_unsignedp
, &volatilep
, &ll_mask
,
5271 lr_inner
= decode_field_reference (loc
, lr_arg
,
5272 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5273 &lr_unsignedp
, &volatilep
, &lr_mask
,
5275 rl_inner
= decode_field_reference (loc
, rl_arg
,
5276 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5277 &rl_unsignedp
, &volatilep
, &rl_mask
,
5279 rr_inner
= decode_field_reference (loc
, rr_arg
,
5280 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5281 &rr_unsignedp
, &volatilep
, &rr_mask
,
5284 /* It must be true that the inner operation on the lhs of each
5285 comparison must be the same if we are to be able to do anything.
5286 Then see if we have constants. If not, the same must be true for
5288 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
5289 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5292 if (TREE_CODE (lr_arg
) == INTEGER_CST
5293 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5294 l_const
= lr_arg
, r_const
= rr_arg
;
5295 else if (lr_inner
== 0 || rr_inner
== 0
5296 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5299 l_const
= r_const
= 0;
5301 /* If either comparison code is not correct for our logical operation,
5302 fail. However, we can convert a one-bit comparison against zero into
5303 the opposite comparison against that bit being set in the field. */
5305 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5306 if (lcode
!= wanted_code
)
5308 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5310 /* Make the left operand unsigned, since we are only interested
5311 in the value of one bit. Otherwise we are doing the wrong
5320 /* This is analogous to the code for l_const above. */
5321 if (rcode
!= wanted_code
)
5323 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5332 /* See if we can find a mode that contains both fields being compared on
5333 the left. If we can't, fail. Otherwise, update all constants and masks
5334 to be relative to a field of that size. */
5335 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5336 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5337 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5338 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5340 if (lnmode
== VOIDmode
)
5343 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5344 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5345 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5346 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5348 if (BYTES_BIG_ENDIAN
)
5350 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5351 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5354 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5355 size_int (xll_bitpos
));
5356 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5357 size_int (xrl_bitpos
));
5361 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5362 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5363 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5364 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5365 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5368 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5370 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5375 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5376 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5377 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5378 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5379 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5382 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5384 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5388 /* If the right sides are not constant, do the same for it. Also,
5389 disallow this optimization if a size or signedness mismatch occurs
5390 between the left and right sides. */
5393 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5394 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5395 /* Make sure the two fields on the right
5396 correspond to the left without being swapped. */
5397 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5400 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5401 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5402 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5403 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5405 if (rnmode
== VOIDmode
)
5408 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5409 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5410 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5411 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5413 if (BYTES_BIG_ENDIAN
)
5415 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5416 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5419 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5421 size_int (xlr_bitpos
));
5422 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5424 size_int (xrr_bitpos
));
5426 /* Make a mask that corresponds to both fields being compared.
5427 Do this for both items being compared. If the operands are the
5428 same size and the bits being compared are in the same position
5429 then we can do this by masking both and comparing the masked
5431 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5432 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5433 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5435 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5436 ll_unsignedp
|| rl_unsignedp
);
5437 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5438 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5440 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
, rnbitsize
, rnbitpos
,
5441 lr_unsignedp
|| rr_unsignedp
);
5442 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5443 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5445 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5448 /* There is still another way we can do something: If both pairs of
5449 fields being compared are adjacent, we may be able to make a wider
5450 field containing them both.
5452 Note that we still must mask the lhs/rhs expressions. Furthermore,
5453 the mask must be shifted to account for the shift done by
5454 make_bit_field_ref. */
5455 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5456 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5457 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5458 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5462 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
,
5463 ll_bitsize
+ rl_bitsize
,
5464 MIN (ll_bitpos
, rl_bitpos
), ll_unsignedp
);
5465 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
,
5466 lr_bitsize
+ rr_bitsize
,
5467 MIN (lr_bitpos
, rr_bitpos
), lr_unsignedp
);
5469 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5470 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5471 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5472 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5474 /* Convert to the smaller type before masking out unwanted bits. */
5476 if (lntype
!= rntype
)
5478 if (lnbitsize
> rnbitsize
)
5480 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5481 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5484 else if (lnbitsize
< rnbitsize
)
5486 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5487 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5492 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5493 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5495 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5496 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5498 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5504 /* Handle the case of comparisons with constants. If there is something in
5505 common between the masks, those bits of the constants must be the same.
5506 If not, the condition is always false. Test for this to avoid generating
5507 incorrect code below. */
5508 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5509 if (! integer_zerop (result
)
5510 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5511 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5513 if (wanted_code
== NE_EXPR
)
5515 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5516 return constant_boolean_node (true, truth_type
);
5520 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5521 return constant_boolean_node (false, truth_type
);
5525 /* Construct the expression we will return. First get the component
5526 reference we will make. Unless the mask is all ones the width of
5527 that field, perform the mask operation. Then compare with the
5529 result
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5530 ll_unsignedp
|| rl_unsignedp
);
5532 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5533 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5534 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5536 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5537 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5540 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5544 optimize_minmax_comparison (location_t loc
, enum tree_code code
, tree type
,
5548 enum tree_code op_code
;
5551 int consts_equal
, consts_lt
;
5554 STRIP_SIGN_NOPS (arg0
);
5556 op_code
= TREE_CODE (arg0
);
5557 minmax_const
= TREE_OPERAND (arg0
, 1);
5558 comp_const
= fold_convert_loc (loc
, TREE_TYPE (arg0
), op1
);
5559 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
5560 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
5561 inner
= TREE_OPERAND (arg0
, 0);
5563 /* If something does not permit us to optimize, return the original tree. */
5564 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
5565 || TREE_CODE (comp_const
) != INTEGER_CST
5566 || TREE_OVERFLOW (comp_const
)
5567 || TREE_CODE (minmax_const
) != INTEGER_CST
5568 || TREE_OVERFLOW (minmax_const
))
5571 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5572 and GT_EXPR, doing the rest with recursive calls using logical
5576 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
5579 = optimize_minmax_comparison (loc
,
5580 invert_tree_comparison (code
, false),
5583 return invert_truthvalue_loc (loc
, tem
);
5589 fold_build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
5590 optimize_minmax_comparison
5591 (loc
, EQ_EXPR
, type
, arg0
, comp_const
),
5592 optimize_minmax_comparison
5593 (loc
, GT_EXPR
, type
, arg0
, comp_const
));
5596 if (op_code
== MAX_EXPR
&& consts_equal
)
5597 /* MAX (X, 0) == 0 -> X <= 0 */
5598 return fold_build2_loc (loc
, LE_EXPR
, type
, inner
, comp_const
);
5600 else if (op_code
== MAX_EXPR
&& consts_lt
)
5601 /* MAX (X, 0) == 5 -> X == 5 */
5602 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5604 else if (op_code
== MAX_EXPR
)
5605 /* MAX (X, 0) == -1 -> false */
5606 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5608 else if (consts_equal
)
5609 /* MIN (X, 0) == 0 -> X >= 0 */
5610 return fold_build2_loc (loc
, GE_EXPR
, type
, inner
, comp_const
);
5613 /* MIN (X, 0) == 5 -> false */
5614 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5617 /* MIN (X, 0) == -1 -> X == -1 */
5618 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5621 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
5622 /* MAX (X, 0) > 0 -> X > 0
5623 MAX (X, 0) > 5 -> X > 5 */
5624 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5626 else if (op_code
== MAX_EXPR
)
5627 /* MAX (X, 0) > -1 -> true */
5628 return omit_one_operand_loc (loc
, type
, integer_one_node
, inner
);
5630 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
5631 /* MIN (X, 0) > 0 -> false
5632 MIN (X, 0) > 5 -> false */
5633 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5636 /* MIN (X, 0) > -1 -> X > -1 */
5637 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5644 /* T is an integer expression that is being multiplied, divided, or taken a
5645 modulus (CODE says which and what kind of divide or modulus) by a
5646 constant C. See if we can eliminate that operation by folding it with
5647 other operations already in T. WIDE_TYPE, if non-null, is a type that
5648 should be used for the computation if wider than our type.
5650 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5651 (X * 2) + (Y * 4). We must, however, be assured that either the original
5652 expression would not overflow or that overflow is undefined for the type
5653 in the language in question.
5655 If we return a non-null expression, it is an equivalent form of the
5656 original computation, but need not be in the original type.
5658 We set *STRICT_OVERFLOW_P to true if the return values depends on
5659 signed overflow being undefined. Otherwise we do not change
5660 *STRICT_OVERFLOW_P. */
5663 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5664 bool *strict_overflow_p
)
5666 /* To avoid exponential search depth, refuse to allow recursion past
5667 three levels. Beyond that (1) it's highly unlikely that we'll find
5668 something interesting and (2) we've probably processed it before
5669 when we built the inner expression. */
5678 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
5685 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5686 bool *strict_overflow_p
)
5688 tree type
= TREE_TYPE (t
);
5689 enum tree_code tcode
= TREE_CODE (t
);
5690 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
5691 > GET_MODE_SIZE (TYPE_MODE (type
)))
5692 ? wide_type
: type
);
5694 int same_p
= tcode
== code
;
5695 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
5696 bool sub_strict_overflow_p
;
5698 /* Don't deal with constants of zero here; they confuse the code below. */
5699 if (integer_zerop (c
))
5702 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
5703 op0
= TREE_OPERAND (t
, 0);
5705 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
5706 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
5708 /* Note that we need not handle conditional operations here since fold
5709 already handles those cases. So just do arithmetic here. */
5713 /* For a constant, we can always simplify if we are a multiply
5714 or (for divide and modulus) if it is a multiple of our constant. */
5715 if (code
== MULT_EXPR
5716 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, t
, c
)))
5717 return const_binop (code
, fold_convert (ctype
, t
),
5718 fold_convert (ctype
, c
));
5721 CASE_CONVERT
: case NON_LVALUE_EXPR
:
5722 /* If op0 is an expression ... */
5723 if ((COMPARISON_CLASS_P (op0
)
5724 || UNARY_CLASS_P (op0
)
5725 || BINARY_CLASS_P (op0
)
5726 || VL_EXP_CLASS_P (op0
)
5727 || EXPRESSION_CLASS_P (op0
))
5728 /* ... and has wrapping overflow, and its type is smaller
5729 than ctype, then we cannot pass through as widening. */
5730 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
))
5731 && (TYPE_PRECISION (ctype
)
5732 > TYPE_PRECISION (TREE_TYPE (op0
))))
5733 /* ... or this is a truncation (t is narrower than op0),
5734 then we cannot pass through this narrowing. */
5735 || (TYPE_PRECISION (type
)
5736 < TYPE_PRECISION (TREE_TYPE (op0
)))
5737 /* ... or signedness changes for division or modulus,
5738 then we cannot pass through this conversion. */
5739 || (code
!= MULT_EXPR
5740 && (TYPE_UNSIGNED (ctype
)
5741 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
5742 /* ... or has undefined overflow while the converted to
5743 type has not, we cannot do the operation in the inner type
5744 as that would introduce undefined overflow. */
5745 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))
5746 && !TYPE_OVERFLOW_UNDEFINED (type
))))
5749 /* Pass the constant down and see if we can make a simplification. If
5750 we can, replace this expression with the inner simplification for
5751 possible later conversion to our or some other type. */
5752 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
5753 && TREE_CODE (t2
) == INTEGER_CST
5754 && !TREE_OVERFLOW (t2
)
5755 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
5757 ? ctype
: NULL_TREE
,
5758 strict_overflow_p
))))
5763 /* If widening the type changes it from signed to unsigned, then we
5764 must avoid building ABS_EXPR itself as unsigned. */
5765 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
5767 tree cstype
= (*signed_type_for
) (ctype
);
5768 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
5771 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
5772 return fold_convert (ctype
, t1
);
5776 /* If the constant is negative, we cannot simplify this. */
5777 if (tree_int_cst_sgn (c
) == -1)
5781 /* For division and modulus, type can't be unsigned, as e.g.
5782 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5783 For signed types, even with wrapping overflow, this is fine. */
5784 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
5786 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
5788 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
5791 case MIN_EXPR
: case MAX_EXPR
:
5792 /* If widening the type changes the signedness, then we can't perform
5793 this optimization as that changes the result. */
5794 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
5797 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5798 sub_strict_overflow_p
= false;
5799 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
5800 &sub_strict_overflow_p
)) != 0
5801 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
5802 &sub_strict_overflow_p
)) != 0)
5804 if (tree_int_cst_sgn (c
) < 0)
5805 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
5806 if (sub_strict_overflow_p
)
5807 *strict_overflow_p
= true;
5808 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
5809 fold_convert (ctype
, t2
));
5813 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
5814 /* If the second operand is constant, this is a multiplication
5815 or floor division, by a power of two, so we can treat it that
5816 way unless the multiplier or divisor overflows. Signed
5817 left-shift overflow is implementation-defined rather than
5818 undefined in C90, so do not convert signed left shift into
5820 if (TREE_CODE (op1
) == INTEGER_CST
5821 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
5822 /* const_binop may not detect overflow correctly,
5823 so check for it explicitly here. */
5824 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
5825 && 0 != (t1
= fold_convert (ctype
,
5826 const_binop (LSHIFT_EXPR
,
5829 && !TREE_OVERFLOW (t1
))
5830 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
5831 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
5833 fold_convert (ctype
, op0
),
5835 c
, code
, wide_type
, strict_overflow_p
);
5838 case PLUS_EXPR
: case MINUS_EXPR
:
5839 /* See if we can eliminate the operation on both sides. If we can, we
5840 can return a new PLUS or MINUS. If we can't, the only remaining
5841 cases where we can do anything are if the second operand is a
5843 sub_strict_overflow_p
= false;
5844 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
5845 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
5846 if (t1
!= 0 && t2
!= 0
5847 && (code
== MULT_EXPR
5848 /* If not multiplication, we can only do this if both operands
5849 are divisible by c. */
5850 || (multiple_of_p (ctype
, op0
, c
)
5851 && multiple_of_p (ctype
, op1
, c
))))
5853 if (sub_strict_overflow_p
)
5854 *strict_overflow_p
= true;
5855 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
5856 fold_convert (ctype
, t2
));
5859 /* If this was a subtraction, negate OP1 and set it to be an addition.
5860 This simplifies the logic below. */
5861 if (tcode
== MINUS_EXPR
)
5863 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
5864 /* If OP1 was not easily negatable, the constant may be OP0. */
5865 if (TREE_CODE (op0
) == INTEGER_CST
)
5876 if (TREE_CODE (op1
) != INTEGER_CST
)
5879 /* If either OP1 or C are negative, this optimization is not safe for
5880 some of the division and remainder types while for others we need
5881 to change the code. */
5882 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
5884 if (code
== CEIL_DIV_EXPR
)
5885 code
= FLOOR_DIV_EXPR
;
5886 else if (code
== FLOOR_DIV_EXPR
)
5887 code
= CEIL_DIV_EXPR
;
5888 else if (code
!= MULT_EXPR
5889 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
5893 /* If it's a multiply or a division/modulus operation of a multiple
5894 of our constant, do the operation and verify it doesn't overflow. */
5895 if (code
== MULT_EXPR
5896 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
)))
5898 op1
= const_binop (code
, fold_convert (ctype
, op1
),
5899 fold_convert (ctype
, c
));
5900 /* We allow the constant to overflow with wrapping semantics. */
5902 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
5908 /* If we have an unsigned type, we cannot widen the operation since it
5909 will change the result if the original computation overflowed. */
5910 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
5913 /* If we were able to eliminate our operation from the first side,
5914 apply our operation to the second side and reform the PLUS. */
5915 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
5916 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
5918 /* The last case is if we are a multiply. In that case, we can
5919 apply the distributive law to commute the multiply and addition
5920 if the multiplication of the constants doesn't overflow
5921 and overflow is defined. With undefined overflow
5922 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
5923 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
5924 return fold_build2 (tcode
, ctype
,
5925 fold_build2 (code
, ctype
,
5926 fold_convert (ctype
, op0
),
5927 fold_convert (ctype
, c
)),
5933 /* We have a special case here if we are doing something like
5934 (C * 8) % 4 since we know that's zero. */
5935 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
5936 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
5937 /* If the multiplication can overflow we cannot optimize this. */
5938 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
5939 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
5940 && integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
)))
5942 *strict_overflow_p
= true;
5943 return omit_one_operand (type
, integer_zero_node
, op0
);
5946 /* ... fall through ... */
5948 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
5949 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
5950 /* If we can extract our operation from the LHS, do so and return a
5951 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5952 do something only if the second operand is a constant. */
5954 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
5955 strict_overflow_p
)) != 0)
5956 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
5957 fold_convert (ctype
, op1
));
5958 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
5959 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
5960 strict_overflow_p
)) != 0)
5961 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
5962 fold_convert (ctype
, t1
));
5963 else if (TREE_CODE (op1
) != INTEGER_CST
)
5966 /* If these are the same operation types, we can associate them
5967 assuming no overflow. */
5970 bool overflow_p
= false;
5971 bool overflow_mul_p
;
5972 signop sign
= TYPE_SIGN (ctype
);
5973 wide_int mul
= wi::mul (op1
, c
, sign
, &overflow_mul_p
);
5974 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
5976 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
5979 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
5980 wide_int_to_tree (ctype
, mul
));
5983 /* If these operations "cancel" each other, we have the main
5984 optimizations of this pass, which occur when either constant is a
5985 multiple of the other, in which case we replace this with either an
5986 operation or CODE or TCODE.
5988 If we have an unsigned type, we cannot do this since it will change
5989 the result if the original computation overflowed. */
5990 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
5991 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
5992 || (tcode
== MULT_EXPR
5993 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
5994 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
5995 && code
!= MULT_EXPR
)))
5997 if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
)))
5999 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6000 *strict_overflow_p
= true;
6001 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6002 fold_convert (ctype
,
6003 const_binop (TRUNC_DIV_EXPR
,
6006 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, c
, op1
)))
6008 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6009 *strict_overflow_p
= true;
6010 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6011 fold_convert (ctype
,
6012 const_binop (TRUNC_DIV_EXPR
,
6025 /* Return a node which has the indicated constant VALUE (either 0 or
6026 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6027 and is of the indicated TYPE. */
6030 constant_boolean_node (bool value
, tree type
)
6032 if (type
== integer_type_node
)
6033 return value
? integer_one_node
: integer_zero_node
;
6034 else if (type
== boolean_type_node
)
6035 return value
? boolean_true_node
: boolean_false_node
;
6036 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6037 return build_vector_from_val (type
,
6038 build_int_cst (TREE_TYPE (type
),
6041 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6045 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6046 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6047 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6048 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6049 COND is the first argument to CODE; otherwise (as in the example
6050 given here), it is the second argument. TYPE is the type of the
6051 original expression. Return NULL_TREE if no simplification is
6055 fold_binary_op_with_conditional_arg (location_t loc
,
6056 enum tree_code code
,
6057 tree type
, tree op0
, tree op1
,
6058 tree cond
, tree arg
, int cond_first_p
)
6060 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6061 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6062 tree test
, true_value
, false_value
;
6063 tree lhs
= NULL_TREE
;
6064 tree rhs
= NULL_TREE
;
6065 enum tree_code cond_code
= COND_EXPR
;
6067 if (TREE_CODE (cond
) == COND_EXPR
6068 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6070 test
= TREE_OPERAND (cond
, 0);
6071 true_value
= TREE_OPERAND (cond
, 1);
6072 false_value
= TREE_OPERAND (cond
, 2);
6073 /* If this operand throws an expression, then it does not make
6074 sense to try to perform a logical or arithmetic operation
6076 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6078 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6083 tree testtype
= TREE_TYPE (cond
);
6085 true_value
= constant_boolean_node (true, testtype
);
6086 false_value
= constant_boolean_node (false, testtype
);
6089 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6090 cond_code
= VEC_COND_EXPR
;
6092 /* This transformation is only worthwhile if we don't have to wrap ARG
6093 in a SAVE_EXPR and the operation can be simplified without recursing
6094 on at least one of the branches once its pushed inside the COND_EXPR. */
6095 if (!TREE_CONSTANT (arg
)
6096 && (TREE_SIDE_EFFECTS (arg
)
6097 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6098 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6101 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6104 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6106 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6108 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6112 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6114 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6116 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6119 /* Check that we have simplified at least one of the branches. */
6120 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6123 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6127 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6129 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6130 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6131 ADDEND is the same as X.
6133 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6134 and finite. The problematic cases are when X is zero, and its mode
6135 has signed zeros. In the case of rounding towards -infinity,
6136 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6137 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6140 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6142 if (!real_zerop (addend
))
6145 /* Don't allow the fold with -fsignaling-nans. */
6146 if (HONOR_SNANS (TYPE_MODE (type
)))
6149 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6150 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
)))
6153 /* In a vector or complex, we would need to check the sign of all zeros. */
6154 if (TREE_CODE (addend
) != REAL_CST
)
6157 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6158 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6161 /* The mode has signed zeros, and we have to honor their sign.
6162 In this situation, there is only one case we can return true for.
6163 X - 0 is the same as X unless rounding towards -infinity is
6165 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type
));
6168 /* Subroutine of fold() that checks comparisons of built-in math
6169 functions against real constants.
6171 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6172 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6173 is the type of the result and ARG0 and ARG1 are the operands of the
6174 comparison. ARG1 must be a TREE_REAL_CST.
6176 The function returns the constant folded tree if a simplification
6177 can be made, and NULL_TREE otherwise. */
6180 fold_mathfn_compare (location_t loc
,
6181 enum built_in_function fcode
, enum tree_code code
,
6182 tree type
, tree arg0
, tree arg1
)
6186 if (BUILTIN_SQRT_P (fcode
))
6188 tree arg
= CALL_EXPR_ARG (arg0
, 0);
6189 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (arg0
));
6191 c
= TREE_REAL_CST (arg1
);
6192 if (REAL_VALUE_NEGATIVE (c
))
6194 /* sqrt(x) < y is always false, if y is negative. */
6195 if (code
== EQ_EXPR
|| code
== LT_EXPR
|| code
== LE_EXPR
)
6196 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
6198 /* sqrt(x) > y is always true, if y is negative and we
6199 don't care about NaNs, i.e. negative values of x. */
6200 if (code
== NE_EXPR
|| !HONOR_NANS (mode
))
6201 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
6203 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6204 return fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6205 build_real (TREE_TYPE (arg
), dconst0
));
6207 else if (code
== GT_EXPR
|| code
== GE_EXPR
)
6211 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
6212 real_convert (&c2
, mode
, &c2
);
6214 if (REAL_VALUE_ISINF (c2
))
6216 /* sqrt(x) > y is x == +Inf, when y is very large. */
6217 if (HONOR_INFINITIES (mode
))
6218 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg
,
6219 build_real (TREE_TYPE (arg
), c2
));
6221 /* sqrt(x) > y is always false, when y is very large
6222 and we don't care about infinities. */
6223 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
6226 /* sqrt(x) > c is the same as x > c*c. */
6227 return fold_build2_loc (loc
, code
, type
, arg
,
6228 build_real (TREE_TYPE (arg
), c2
));
6230 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
6234 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
6235 real_convert (&c2
, mode
, &c2
);
6237 if (REAL_VALUE_ISINF (c2
))
6239 /* sqrt(x) < y is always true, when y is a very large
6240 value and we don't care about NaNs or Infinities. */
6241 if (! HONOR_NANS (mode
) && ! HONOR_INFINITIES (mode
))
6242 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
6244 /* sqrt(x) < y is x != +Inf when y is very large and we
6245 don't care about NaNs. */
6246 if (! HONOR_NANS (mode
))
6247 return fold_build2_loc (loc
, NE_EXPR
, type
, arg
,
6248 build_real (TREE_TYPE (arg
), c2
));
6250 /* sqrt(x) < y is x >= 0 when y is very large and we
6251 don't care about Infinities. */
6252 if (! HONOR_INFINITIES (mode
))
6253 return fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6254 build_real (TREE_TYPE (arg
), dconst0
));
6256 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6257 arg
= save_expr (arg
);
6258 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
6259 fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6260 build_real (TREE_TYPE (arg
),
6262 fold_build2_loc (loc
, NE_EXPR
, type
, arg
,
6263 build_real (TREE_TYPE (arg
),
6267 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6268 if (! HONOR_NANS (mode
))
6269 return fold_build2_loc (loc
, code
, type
, arg
,
6270 build_real (TREE_TYPE (arg
), c2
));
6272 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6273 arg
= save_expr (arg
);
6274 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
6275 fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6276 build_real (TREE_TYPE (arg
),
6278 fold_build2_loc (loc
, code
, type
, arg
,
6279 build_real (TREE_TYPE (arg
),
6287 /* Subroutine of fold() that optimizes comparisons against Infinities,
6288 either +Inf or -Inf.
6290 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6291 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6292 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6294 The function returns the constant folded tree if a simplification
6295 can be made, and NULL_TREE otherwise. */
6298 fold_inf_compare (location_t loc
, enum tree_code code
, tree type
,
6299 tree arg0
, tree arg1
)
6301 enum machine_mode mode
;
6302 REAL_VALUE_TYPE max
;
6306 mode
= TYPE_MODE (TREE_TYPE (arg0
));
6308 /* For negative infinity swap the sense of the comparison. */
6309 neg
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
));
6311 code
= swap_tree_comparison (code
);
6316 /* x > +Inf is always false, if with ignore sNANs. */
6317 if (HONOR_SNANS (mode
))
6319 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
6322 /* x <= +Inf is always true, if we don't case about NaNs. */
6323 if (! HONOR_NANS (mode
))
6324 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
6326 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6327 arg0
= save_expr (arg0
);
6328 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
, arg0
);
6332 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6333 real_maxval (&max
, neg
, mode
);
6334 return fold_build2_loc (loc
, neg
? LT_EXPR
: GT_EXPR
, type
,
6335 arg0
, build_real (TREE_TYPE (arg0
), max
));
6338 /* x < +Inf is always equal to x <= DBL_MAX. */
6339 real_maxval (&max
, neg
, mode
);
6340 return fold_build2_loc (loc
, neg
? GE_EXPR
: LE_EXPR
, type
,
6341 arg0
, build_real (TREE_TYPE (arg0
), max
));
6344 /* x != +Inf is always equal to !(x > DBL_MAX). */
6345 real_maxval (&max
, neg
, mode
);
6346 if (! HONOR_NANS (mode
))
6347 return fold_build2_loc (loc
, neg
? GE_EXPR
: LE_EXPR
, type
,
6348 arg0
, build_real (TREE_TYPE (arg0
), max
));
6350 temp
= fold_build2_loc (loc
, neg
? LT_EXPR
: GT_EXPR
, type
,
6351 arg0
, build_real (TREE_TYPE (arg0
), max
));
6352 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, temp
);
6361 /* Subroutine of fold() that optimizes comparisons of a division by
6362 a nonzero integer constant against an integer constant, i.e.
6365 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6366 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6367 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6369 The function returns the constant folded tree if a simplification
6370 can be made, and NULL_TREE otherwise. */
6373 fold_div_compare (location_t loc
,
6374 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6376 tree prod
, tmp
, hi
, lo
;
6377 tree arg00
= TREE_OPERAND (arg0
, 0);
6378 tree arg01
= TREE_OPERAND (arg0
, 1);
6379 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6380 bool neg_overflow
= false;
6383 /* We have to do this the hard way to detect unsigned overflow.
6384 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6385 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6386 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6387 neg_overflow
= false;
6389 if (sign
== UNSIGNED
)
6391 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6392 build_int_cst (TREE_TYPE (arg01
), 1));
6395 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6396 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6397 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6398 -1, overflow
| TREE_OVERFLOW (prod
));
6400 else if (tree_int_cst_sgn (arg01
) >= 0)
6402 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6403 build_int_cst (TREE_TYPE (arg01
), 1));
6404 switch (tree_int_cst_sgn (arg1
))
6407 neg_overflow
= true;
6408 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6413 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6418 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6428 /* A negative divisor reverses the relational operators. */
6429 code
= swap_tree_comparison (code
);
6431 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6432 build_int_cst (TREE_TYPE (arg01
), 1));
6433 switch (tree_int_cst_sgn (arg1
))
6436 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6441 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6446 neg_overflow
= true;
6447 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6459 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6460 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6461 if (TREE_OVERFLOW (hi
))
6462 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6463 if (TREE_OVERFLOW (lo
))
6464 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6465 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6468 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6469 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6470 if (TREE_OVERFLOW (hi
))
6471 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6472 if (TREE_OVERFLOW (lo
))
6473 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6474 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6477 if (TREE_OVERFLOW (lo
))
6479 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6480 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6482 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6485 if (TREE_OVERFLOW (hi
))
6487 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6488 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6490 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6493 if (TREE_OVERFLOW (hi
))
6495 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6496 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6498 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6501 if (TREE_OVERFLOW (lo
))
6503 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6504 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6506 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6516 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6517 equality/inequality test, then return a simplified form of the test
6518 using a sign testing. Otherwise return NULL. TYPE is the desired
6522 fold_single_bit_test_into_sign_test (location_t loc
,
6523 enum tree_code code
, tree arg0
, tree arg1
,
6526 /* If this is testing a single bit, we can optimize the test. */
6527 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6528 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6529 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6531 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6532 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6533 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6535 if (arg00
!= NULL_TREE
6536 /* This is only a win if casting to a signed type is cheap,
6537 i.e. when arg00's type is not a partial mode. */
6538 && TYPE_PRECISION (TREE_TYPE (arg00
))
6539 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6541 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6542 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6544 fold_convert_loc (loc
, stype
, arg00
),
6545 build_int_cst (stype
, 0));
6552 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6553 equality/inequality test, then return a simplified form of
6554 the test using shifts and logical operations. Otherwise return
6555 NULL. TYPE is the desired result type. */
6558 fold_single_bit_test (location_t loc
, enum tree_code code
,
6559 tree arg0
, tree arg1
, tree result_type
)
6561 /* If this is testing a single bit, we can optimize the test. */
6562 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6563 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6564 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6566 tree inner
= TREE_OPERAND (arg0
, 0);
6567 tree type
= TREE_TYPE (arg0
);
6568 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6569 enum machine_mode operand_mode
= TYPE_MODE (type
);
6571 tree signed_type
, unsigned_type
, intermediate_type
;
6574 /* First, see if we can fold the single bit test into a sign-bit
6576 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6581 /* Otherwise we have (A & C) != 0 where C is a single bit,
6582 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6583 Similarly for (A & C) == 0. */
6585 /* If INNER is a right shift of a constant and it plus BITNUM does
6586 not overflow, adjust BITNUM and INNER. */
6587 if (TREE_CODE (inner
) == RSHIFT_EXPR
6588 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6589 && wi::ltu_p (wi::to_widest (TREE_OPERAND (inner
, 1)) + bitnum
,
6590 TYPE_PRECISION (type
)))
6592 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6593 inner
= TREE_OPERAND (inner
, 0);
6596 /* If we are going to be able to omit the AND below, we must do our
6597 operations as unsigned. If we must use the AND, we have a choice.
6598 Normally unsigned is faster, but for some machines signed is. */
6599 #ifdef LOAD_EXTEND_OP
6600 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
6601 && !flag_syntax_only
) ? 0 : 1;
6606 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6607 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6608 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6609 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6612 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6613 inner
, size_int (bitnum
));
6615 one
= build_int_cst (intermediate_type
, 1);
6617 if (code
== EQ_EXPR
)
6618 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6620 /* Put the AND last so it can combine with more things. */
6621 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6623 /* Make sure to return the proper type. */
6624 inner
= fold_convert_loc (loc
, result_type
, inner
);
6631 /* Check whether we are allowed to reorder operands arg0 and arg1,
6632 such that the evaluation of arg1 occurs before arg0. */
6635 reorder_operands_p (const_tree arg0
, const_tree arg1
)
6637 if (! flag_evaluation_order
)
6639 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
6641 return ! TREE_SIDE_EFFECTS (arg0
)
6642 && ! TREE_SIDE_EFFECTS (arg1
);
6645 /* Test whether it is preferable two swap two operands, ARG0 and
6646 ARG1, for example because ARG0 is an integer constant and ARG1
6647 isn't. If REORDER is true, only recommend swapping if we can
6648 evaluate the operands in reverse order. */
6651 tree_swap_operands_p (const_tree arg0
, const_tree arg1
, bool reorder
)
6653 STRIP_SIGN_NOPS (arg0
);
6654 STRIP_SIGN_NOPS (arg1
);
6656 if (TREE_CODE (arg1
) == INTEGER_CST
)
6658 if (TREE_CODE (arg0
) == INTEGER_CST
)
6661 if (TREE_CODE (arg1
) == REAL_CST
)
6663 if (TREE_CODE (arg0
) == REAL_CST
)
6666 if (TREE_CODE (arg1
) == FIXED_CST
)
6668 if (TREE_CODE (arg0
) == FIXED_CST
)
6671 if (TREE_CODE (arg1
) == COMPLEX_CST
)
6673 if (TREE_CODE (arg0
) == COMPLEX_CST
)
6676 if (TREE_CONSTANT (arg1
))
6678 if (TREE_CONSTANT (arg0
))
6681 if (optimize_function_for_size_p (cfun
))
6684 if (reorder
&& flag_evaluation_order
6685 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
6688 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6689 for commutative and comparison operators. Ensuring a canonical
6690 form allows the optimizers to find additional redundancies without
6691 having to explicitly check for both orderings. */
6692 if (TREE_CODE (arg0
) == SSA_NAME
6693 && TREE_CODE (arg1
) == SSA_NAME
6694 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6697 /* Put SSA_NAMEs last. */
6698 if (TREE_CODE (arg1
) == SSA_NAME
)
6700 if (TREE_CODE (arg0
) == SSA_NAME
)
6703 /* Put variables last. */
6712 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6713 ARG0 is extended to a wider type. */
6716 fold_widened_comparison (location_t loc
, enum tree_code code
,
6717 tree type
, tree arg0
, tree arg1
)
6719 tree arg0_unw
= get_unwidened (arg0
, NULL_TREE
);
6721 tree shorter_type
, outer_type
;
6725 if (arg0_unw
== arg0
)
6727 shorter_type
= TREE_TYPE (arg0_unw
);
6729 #ifdef HAVE_canonicalize_funcptr_for_compare
6730 /* Disable this optimization if we're casting a function pointer
6731 type on targets that require function pointer canonicalization. */
6732 if (HAVE_canonicalize_funcptr_for_compare
6733 && TREE_CODE (shorter_type
) == POINTER_TYPE
6734 && TREE_CODE (TREE_TYPE (shorter_type
)) == FUNCTION_TYPE
)
6738 if (TYPE_PRECISION (TREE_TYPE (arg0
)) <= TYPE_PRECISION (shorter_type
))
6741 arg1_unw
= get_unwidened (arg1
, NULL_TREE
);
6743 /* If possible, express the comparison in the shorter mode. */
6744 if ((code
== EQ_EXPR
|| code
== NE_EXPR
6745 || TYPE_UNSIGNED (TREE_TYPE (arg0
)) == TYPE_UNSIGNED (shorter_type
))
6746 && (TREE_TYPE (arg1_unw
) == shorter_type
6747 || ((TYPE_PRECISION (shorter_type
)
6748 >= TYPE_PRECISION (TREE_TYPE (arg1_unw
)))
6749 && (TYPE_UNSIGNED (shorter_type
)
6750 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw
))))
6751 || (TREE_CODE (arg1_unw
) == INTEGER_CST
6752 && (TREE_CODE (shorter_type
) == INTEGER_TYPE
6753 || TREE_CODE (shorter_type
) == BOOLEAN_TYPE
)
6754 && int_fits_type_p (arg1_unw
, shorter_type
))))
6755 return fold_build2_loc (loc
, code
, type
, arg0_unw
,
6756 fold_convert_loc (loc
, shorter_type
, arg1_unw
));
6758 if (TREE_CODE (arg1_unw
) != INTEGER_CST
6759 || TREE_CODE (shorter_type
) != INTEGER_TYPE
6760 || !int_fits_type_p (arg1_unw
, shorter_type
))
6763 /* If we are comparing with the integer that does not fit into the range
6764 of the shorter type, the result is known. */
6765 outer_type
= TREE_TYPE (arg1_unw
);
6766 min
= lower_bound_in_type (outer_type
, shorter_type
);
6767 max
= upper_bound_in_type (outer_type
, shorter_type
);
6769 above
= integer_nonzerop (fold_relational_const (LT_EXPR
, type
,
6771 below
= integer_nonzerop (fold_relational_const (LT_EXPR
, type
,
6778 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
6783 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
6789 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
6791 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
6796 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
6798 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
6807 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6808 ARG0 just the signedness is changed. */
6811 fold_sign_changed_comparison (location_t loc
, enum tree_code code
, tree type
,
6812 tree arg0
, tree arg1
)
6815 tree inner_type
, outer_type
;
6817 if (!CONVERT_EXPR_P (arg0
))
6820 outer_type
= TREE_TYPE (arg0
);
6821 arg0_inner
= TREE_OPERAND (arg0
, 0);
6822 inner_type
= TREE_TYPE (arg0_inner
);
6824 #ifdef HAVE_canonicalize_funcptr_for_compare
6825 /* Disable this optimization if we're casting a function pointer
6826 type on targets that require function pointer canonicalization. */
6827 if (HAVE_canonicalize_funcptr_for_compare
6828 && TREE_CODE (inner_type
) == POINTER_TYPE
6829 && TREE_CODE (TREE_TYPE (inner_type
)) == FUNCTION_TYPE
)
6833 if (TYPE_PRECISION (inner_type
) != TYPE_PRECISION (outer_type
))
6836 if (TREE_CODE (arg1
) != INTEGER_CST
6837 && !(CONVERT_EXPR_P (arg1
)
6838 && TREE_TYPE (TREE_OPERAND (arg1
, 0)) == inner_type
))
6841 if (TYPE_UNSIGNED (inner_type
) != TYPE_UNSIGNED (outer_type
)
6846 if (POINTER_TYPE_P (inner_type
) != POINTER_TYPE_P (outer_type
))
6849 if (TREE_CODE (arg1
) == INTEGER_CST
)
6850 arg1
= force_fit_type (inner_type
, wi::to_widest (arg1
), 0,
6851 TREE_OVERFLOW (arg1
));
6853 arg1
= fold_convert_loc (loc
, inner_type
, arg1
);
6855 return fold_build2_loc (loc
, code
, type
, arg0_inner
, arg1
);
6858 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6859 step of the array. Reconstructs s and delta in the case of s *
6860 delta being an integer constant (and thus already folded). ADDR is
6861 the address. MULT is the multiplicative expression. If the
6862 function succeeds, the new address expression is returned.
6863 Otherwise NULL_TREE is returned. LOC is the location of the
6864 resulting expression. */
6867 try_move_mult_to_index (location_t loc
, tree addr
, tree op1
)
6869 tree s
, delta
, step
;
6870 tree ref
= TREE_OPERAND (addr
, 0), pref
;
6875 /* Strip the nops that might be added when converting op1 to sizetype. */
6878 /* Canonicalize op1 into a possibly non-constant delta
6879 and an INTEGER_CST s. */
6880 if (TREE_CODE (op1
) == MULT_EXPR
)
6882 tree arg0
= TREE_OPERAND (op1
, 0), arg1
= TREE_OPERAND (op1
, 1);
6887 if (TREE_CODE (arg0
) == INTEGER_CST
)
6892 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6900 else if (TREE_CODE (op1
) == INTEGER_CST
)
6907 /* Simulate we are delta * 1. */
6909 s
= integer_one_node
;
6912 /* Handle &x.array the same as we would handle &x.array[0]. */
6913 if (TREE_CODE (ref
) == COMPONENT_REF
6914 && TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
)
6918 /* Remember if this was a multi-dimensional array. */
6919 if (TREE_CODE (TREE_OPERAND (ref
, 0)) == ARRAY_REF
)
6922 domain
= TYPE_DOMAIN (TREE_TYPE (ref
));
6925 itype
= TREE_TYPE (domain
);
6927 step
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref
)));
6928 if (TREE_CODE (step
) != INTEGER_CST
)
6933 if (! tree_int_cst_equal (step
, s
))
6938 /* Try if delta is a multiple of step. */
6939 tree tmp
= div_if_zero_remainder (op1
, step
);
6945 /* Only fold here if we can verify we do not overflow one
6946 dimension of a multi-dimensional array. */
6951 if (!TYPE_MIN_VALUE (domain
)
6952 || !TYPE_MAX_VALUE (domain
)
6953 || TREE_CODE (TYPE_MAX_VALUE (domain
)) != INTEGER_CST
)
6956 tmp
= fold_binary_loc (loc
, PLUS_EXPR
, itype
,
6957 fold_convert_loc (loc
, itype
,
6958 TYPE_MIN_VALUE (domain
)),
6959 fold_convert_loc (loc
, itype
, delta
));
6960 if (TREE_CODE (tmp
) != INTEGER_CST
6961 || tree_int_cst_lt (TYPE_MAX_VALUE (domain
), tmp
))
6965 /* We found a suitable component reference. */
6967 pref
= TREE_OPERAND (addr
, 0);
6968 ret
= copy_node (pref
);
6969 SET_EXPR_LOCATION (ret
, loc
);
6971 ret
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (TREE_TYPE (ref
)), ret
,
6973 (loc
, PLUS_EXPR
, itype
,
6974 fold_convert_loc (loc
, itype
,
6976 (TYPE_DOMAIN (TREE_TYPE (ref
)))),
6977 fold_convert_loc (loc
, itype
, delta
)),
6978 NULL_TREE
, NULL_TREE
);
6979 return build_fold_addr_expr_loc (loc
, ret
);
6984 for (;; ref
= TREE_OPERAND (ref
, 0))
6986 if (TREE_CODE (ref
) == ARRAY_REF
)
6990 /* Remember if this was a multi-dimensional array. */
6991 if (TREE_CODE (TREE_OPERAND (ref
, 0)) == ARRAY_REF
)
6994 domain
= TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref
, 0)));
6997 itype
= TREE_TYPE (domain
);
6999 step
= array_ref_element_size (ref
);
7000 if (TREE_CODE (step
) != INTEGER_CST
)
7005 if (! tree_int_cst_equal (step
, s
))
7010 /* Try if delta is a multiple of step. */
7011 tree tmp
= div_if_zero_remainder (op1
, step
);
7017 /* Only fold here if we can verify we do not overflow one
7018 dimension of a multi-dimensional array. */
7023 if (TREE_CODE (TREE_OPERAND (ref
, 1)) != INTEGER_CST
7024 || !TYPE_MAX_VALUE (domain
)
7025 || TREE_CODE (TYPE_MAX_VALUE (domain
)) != INTEGER_CST
)
7028 tmp
= fold_binary_loc (loc
, PLUS_EXPR
, itype
,
7029 fold_convert_loc (loc
, itype
,
7030 TREE_OPERAND (ref
, 1)),
7031 fold_convert_loc (loc
, itype
, delta
));
7033 || TREE_CODE (tmp
) != INTEGER_CST
7034 || tree_int_cst_lt (TYPE_MAX_VALUE (domain
), tmp
))
7043 if (!handled_component_p (ref
))
7047 /* We found the suitable array reference. So copy everything up to it,
7048 and replace the index. */
7050 pref
= TREE_OPERAND (addr
, 0);
7051 ret
= copy_node (pref
);
7052 SET_EXPR_LOCATION (ret
, loc
);
7057 pref
= TREE_OPERAND (pref
, 0);
7058 TREE_OPERAND (pos
, 0) = copy_node (pref
);
7059 pos
= TREE_OPERAND (pos
, 0);
7062 TREE_OPERAND (pos
, 1)
7063 = fold_build2_loc (loc
, PLUS_EXPR
, itype
,
7064 fold_convert_loc (loc
, itype
, TREE_OPERAND (pos
, 1)),
7065 fold_convert_loc (loc
, itype
, delta
));
7066 return fold_build1_loc (loc
, ADDR_EXPR
, TREE_TYPE (addr
), ret
);
7070 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7071 means A >= Y && A != MAX, but in this case we know that
7072 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7075 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7077 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
7079 if (TREE_CODE (bound
) == LT_EXPR
)
7080 a
= TREE_OPERAND (bound
, 0);
7081 else if (TREE_CODE (bound
) == GT_EXPR
)
7082 a
= TREE_OPERAND (bound
, 1);
7086 typea
= TREE_TYPE (a
);
7087 if (!INTEGRAL_TYPE_P (typea
)
7088 && !POINTER_TYPE_P (typea
))
7091 if (TREE_CODE (ineq
) == LT_EXPR
)
7093 a1
= TREE_OPERAND (ineq
, 1);
7094 y
= TREE_OPERAND (ineq
, 0);
7096 else if (TREE_CODE (ineq
) == GT_EXPR
)
7098 a1
= TREE_OPERAND (ineq
, 0);
7099 y
= TREE_OPERAND (ineq
, 1);
7104 if (TREE_TYPE (a1
) != typea
)
7107 if (POINTER_TYPE_P (typea
))
7109 /* Convert the pointer types into integer before taking the difference. */
7110 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7111 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7112 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7115 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7117 if (!diff
|| !integer_onep (diff
))
7120 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7123 /* Fold a sum or difference of at least one multiplication.
7124 Returns the folded tree or NULL if no simplification could be made. */
7127 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7128 tree arg0
, tree arg1
)
7130 tree arg00
, arg01
, arg10
, arg11
;
7131 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7133 /* (A * C) +- (B * C) -> (A+-B) * C.
7134 (A * C) +- A -> A * (C+-1).
7135 We are most concerned about the case where C is a constant,
7136 but other combinations show up during loop reduction. Since
7137 it is not difficult, try all four possibilities. */
7139 if (TREE_CODE (arg0
) == MULT_EXPR
)
7141 arg00
= TREE_OPERAND (arg0
, 0);
7142 arg01
= TREE_OPERAND (arg0
, 1);
7144 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7146 arg00
= build_one_cst (type
);
7151 /* We cannot generate constant 1 for fract. */
7152 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7155 arg01
= build_one_cst (type
);
7157 if (TREE_CODE (arg1
) == MULT_EXPR
)
7159 arg10
= TREE_OPERAND (arg1
, 0);
7160 arg11
= TREE_OPERAND (arg1
, 1);
7162 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7164 arg10
= build_one_cst (type
);
7165 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7166 the purpose of this canonicalization. */
7167 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
7168 && negate_expr_p (arg1
)
7169 && code
== PLUS_EXPR
)
7171 arg11
= negate_expr (arg1
);
7179 /* We cannot generate constant 1 for fract. */
7180 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7183 arg11
= build_one_cst (type
);
7187 if (operand_equal_p (arg01
, arg11
, 0))
7188 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7189 else if (operand_equal_p (arg00
, arg10
, 0))
7190 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7191 else if (operand_equal_p (arg00
, arg11
, 0))
7192 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7193 else if (operand_equal_p (arg01
, arg10
, 0))
7194 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7196 /* No identical multiplicands; see if we can find a common
7197 power-of-two factor in non-power-of-two multiplies. This
7198 can help in multi-dimensional array access. */
7199 else if (tree_fits_shwi_p (arg01
)
7200 && tree_fits_shwi_p (arg11
))
7202 HOST_WIDE_INT int01
, int11
, tmp
;
7205 int01
= tree_to_shwi (arg01
);
7206 int11
= tree_to_shwi (arg11
);
7208 /* Move min of absolute values to int11. */
7209 if (absu_hwi (int01
) < absu_hwi (int11
))
7211 tmp
= int01
, int01
= int11
, int11
= tmp
;
7212 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7219 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
7220 /* The remainder should not be a constant, otherwise we
7221 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7222 increased the number of multiplications necessary. */
7223 && TREE_CODE (arg10
) != INTEGER_CST
)
7225 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7226 build_int_cst (TREE_TYPE (arg00
),
7231 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7236 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7237 fold_build2_loc (loc
, code
, type
,
7238 fold_convert_loc (loc
, type
, alt0
),
7239 fold_convert_loc (loc
, type
, alt1
)),
7240 fold_convert_loc (loc
, type
, same
));
7245 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7246 specified by EXPR into the buffer PTR of length LEN bytes.
7247 Return the number of bytes placed in the buffer, or zero
7251 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
)
7253 tree type
= TREE_TYPE (expr
);
7254 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7255 int byte
, offset
, word
, words
;
7256 unsigned char value
;
7258 if (total_bytes
> len
)
7260 words
= total_bytes
/ UNITS_PER_WORD
;
7262 for (byte
= 0; byte
< total_bytes
; byte
++)
7264 int bitpos
= byte
* BITS_PER_UNIT
;
7265 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7267 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7269 if (total_bytes
> UNITS_PER_WORD
)
7271 word
= byte
/ UNITS_PER_WORD
;
7272 if (WORDS_BIG_ENDIAN
)
7273 word
= (words
- 1) - word
;
7274 offset
= word
* UNITS_PER_WORD
;
7275 if (BYTES_BIG_ENDIAN
)
7276 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7278 offset
+= byte
% UNITS_PER_WORD
;
7281 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7282 ptr
[offset
] = value
;
7288 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7289 specified by EXPR into the buffer PTR of length LEN bytes.
7290 Return the number of bytes placed in the buffer, or zero
7294 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
)
7296 tree type
= TREE_TYPE (expr
);
7297 enum machine_mode mode
= TYPE_MODE (type
);
7298 int total_bytes
= GET_MODE_SIZE (mode
);
7299 FIXED_VALUE_TYPE value
;
7300 tree i_value
, i_type
;
7302 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7305 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7307 if (NULL_TREE
== i_type
7308 || TYPE_PRECISION (i_type
) != total_bytes
)
7311 value
= TREE_FIXED_CST (expr
);
7312 i_value
= double_int_to_tree (i_type
, value
.data
);
7314 return native_encode_int (i_value
, ptr
, len
);
7318 /* Subroutine of native_encode_expr. Encode the REAL_CST
7319 specified by EXPR into the buffer PTR of length LEN bytes.
7320 Return the number of bytes placed in the buffer, or zero
7324 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
)
7326 tree type
= TREE_TYPE (expr
);
7327 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7328 int byte
, offset
, word
, words
, bitpos
;
7329 unsigned char value
;
7331 /* There are always 32 bits in each long, no matter the size of
7332 the hosts long. We handle floating point representations with
7336 if (total_bytes
> len
)
7338 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7340 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7342 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7343 bitpos
+= BITS_PER_UNIT
)
7345 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7346 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7348 if (UNITS_PER_WORD
< 4)
7350 word
= byte
/ UNITS_PER_WORD
;
7351 if (WORDS_BIG_ENDIAN
)
7352 word
= (words
- 1) - word
;
7353 offset
= word
* UNITS_PER_WORD
;
7354 if (BYTES_BIG_ENDIAN
)
7355 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7357 offset
+= byte
% UNITS_PER_WORD
;
7360 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7361 ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)] = value
;
7366 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7367 specified by EXPR into the buffer PTR of length LEN bytes.
7368 Return the number of bytes placed in the buffer, or zero
7372 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
)
7377 part
= TREE_REALPART (expr
);
7378 rsize
= native_encode_expr (part
, ptr
, len
);
7381 part
= TREE_IMAGPART (expr
);
7382 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
);
7385 return rsize
+ isize
;
7389 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7390 specified by EXPR into the buffer PTR of length LEN bytes.
7391 Return the number of bytes placed in the buffer, or zero
7395 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
)
7402 count
= VECTOR_CST_NELTS (expr
);
7403 itype
= TREE_TYPE (TREE_TYPE (expr
));
7404 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7405 for (i
= 0; i
< count
; i
++)
7407 elem
= VECTOR_CST_ELT (expr
, i
);
7408 if (native_encode_expr (elem
, ptr
+offset
, len
-offset
) != size
)
7416 /* Subroutine of native_encode_expr. Encode the STRING_CST
7417 specified by EXPR into the buffer PTR of length LEN bytes.
7418 Return the number of bytes placed in the buffer, or zero
7422 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
)
7424 tree type
= TREE_TYPE (expr
);
7425 HOST_WIDE_INT total_bytes
;
7427 if (TREE_CODE (type
) != ARRAY_TYPE
7428 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7429 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7430 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7432 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7433 if (total_bytes
> len
)
7435 if (TREE_STRING_LENGTH (expr
) < total_bytes
)
7437 memcpy (ptr
, TREE_STRING_POINTER (expr
), TREE_STRING_LENGTH (expr
));
7438 memset (ptr
+ TREE_STRING_LENGTH (expr
), 0,
7439 total_bytes
- TREE_STRING_LENGTH (expr
));
7442 memcpy (ptr
, TREE_STRING_POINTER (expr
), total_bytes
);
7447 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7448 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7449 buffer PTR of length LEN bytes. Return the number of bytes
7450 placed in the buffer, or zero upon failure. */
7453 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
)
7455 switch (TREE_CODE (expr
))
7458 return native_encode_int (expr
, ptr
, len
);
7461 return native_encode_real (expr
, ptr
, len
);
7464 return native_encode_fixed (expr
, ptr
, len
);
7467 return native_encode_complex (expr
, ptr
, len
);
7470 return native_encode_vector (expr
, ptr
, len
);
7473 return native_encode_string (expr
, ptr
, len
);
7481 /* Subroutine of native_interpret_expr. Interpret the contents of
7482 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7483 If the buffer cannot be interpreted, return NULL_TREE. */
7486 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7488 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7490 if (total_bytes
> len
7491 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7494 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7496 return wide_int_to_tree (type
, result
);
7500 /* Subroutine of native_interpret_expr. Interpret the contents of
7501 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7502 If the buffer cannot be interpreted, return NULL_TREE. */
7505 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7507 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7509 FIXED_VALUE_TYPE fixed_value
;
7511 if (total_bytes
> len
7512 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7515 result
= double_int::from_buffer (ptr
, total_bytes
);
7516 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7518 return build_fixed (type
, fixed_value
);
7522 /* Subroutine of native_interpret_expr. Interpret the contents of
7523 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7524 If the buffer cannot be interpreted, return NULL_TREE. */
7527 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7529 enum machine_mode mode
= TYPE_MODE (type
);
7530 int total_bytes
= GET_MODE_SIZE (mode
);
7531 int byte
, offset
, word
, words
, bitpos
;
7532 unsigned char value
;
7533 /* There are always 32 bits in each long, no matter the size of
7534 the hosts long. We handle floating point representations with
7539 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7540 if (total_bytes
> len
|| total_bytes
> 24)
7542 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7544 memset (tmp
, 0, sizeof (tmp
));
7545 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7546 bitpos
+= BITS_PER_UNIT
)
7548 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7549 if (UNITS_PER_WORD
< 4)
7551 word
= byte
/ UNITS_PER_WORD
;
7552 if (WORDS_BIG_ENDIAN
)
7553 word
= (words
- 1) - word
;
7554 offset
= word
* UNITS_PER_WORD
;
7555 if (BYTES_BIG_ENDIAN
)
7556 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7558 offset
+= byte
% UNITS_PER_WORD
;
7561 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7562 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7564 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7567 real_from_target (&r
, tmp
, mode
);
7568 return build_real (type
, r
);
7572 /* Subroutine of native_interpret_expr. Interpret the contents of
7573 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7574 If the buffer cannot be interpreted, return NULL_TREE. */
7577 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7579 tree etype
, rpart
, ipart
;
7582 etype
= TREE_TYPE (type
);
7583 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7586 rpart
= native_interpret_expr (etype
, ptr
, size
);
7589 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7592 return build_complex (type
, rpart
, ipart
);
7596 /* Subroutine of native_interpret_expr. Interpret the contents of
7597 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7598 If the buffer cannot be interpreted, return NULL_TREE. */
7601 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7607 etype
= TREE_TYPE (type
);
7608 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7609 count
= TYPE_VECTOR_SUBPARTS (type
);
7610 if (size
* count
> len
)
7613 elements
= XALLOCAVEC (tree
, count
);
7614 for (i
= count
- 1; i
>= 0; i
--)
7616 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7621 return build_vector (type
, elements
);
7625 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7626 the buffer PTR of length LEN as a constant of type TYPE. For
7627 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7628 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7629 return NULL_TREE. */
7632 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7634 switch (TREE_CODE (type
))
7640 case REFERENCE_TYPE
:
7641 return native_interpret_int (type
, ptr
, len
);
7644 return native_interpret_real (type
, ptr
, len
);
7646 case FIXED_POINT_TYPE
:
7647 return native_interpret_fixed (type
, ptr
, len
);
7650 return native_interpret_complex (type
, ptr
, len
);
7653 return native_interpret_vector (type
, ptr
, len
);
7660 /* Returns true if we can interpret the contents of a native encoding
7664 can_native_interpret_type_p (tree type
)
7666 switch (TREE_CODE (type
))
7672 case REFERENCE_TYPE
:
7673 case FIXED_POINT_TYPE
:
7683 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7684 TYPE at compile-time. If we're unable to perform the conversion
7685 return NULL_TREE. */
7688 fold_view_convert_expr (tree type
, tree expr
)
7690 /* We support up to 512-bit values (for V8DFmode). */
7691 unsigned char buffer
[64];
7694 /* Check that the host and target are sane. */
7695 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7698 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7702 return native_interpret_expr (type
, buffer
, len
);
7705 /* Build an expression for the address of T. Folds away INDIRECT_REF
7706 to avoid confusing the gimplify process. */
7709 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7711 /* The size of the object is not relevant when talking about its address. */
7712 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7713 t
= TREE_OPERAND (t
, 0);
7715 if (TREE_CODE (t
) == INDIRECT_REF
)
7717 t
= TREE_OPERAND (t
, 0);
7719 if (TREE_TYPE (t
) != ptrtype
)
7720 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7722 else if (TREE_CODE (t
) == MEM_REF
7723 && integer_zerop (TREE_OPERAND (t
, 1)))
7724 return TREE_OPERAND (t
, 0);
7725 else if (TREE_CODE (t
) == MEM_REF
7726 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7727 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7728 TREE_OPERAND (t
, 0),
7729 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7730 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7732 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7734 if (TREE_TYPE (t
) != ptrtype
)
7735 t
= fold_convert_loc (loc
, ptrtype
, t
);
7738 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7743 /* Build an expression for the address of T. */
7746 build_fold_addr_expr_loc (location_t loc
, tree t
)
7748 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7750 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7753 static bool vec_cst_ctor_to_array (tree
, tree
*);
7755 /* Fold a unary expression of code CODE and type TYPE with operand
7756 OP0. Return the folded expression if folding is successful.
7757 Otherwise, return NULL_TREE. */
7760 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7764 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7766 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7767 && TREE_CODE_LENGTH (code
) == 1);
7772 if (CONVERT_EXPR_CODE_P (code
)
7773 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7775 /* Don't use STRIP_NOPS, because signedness of argument type
7777 STRIP_SIGN_NOPS (arg0
);
7781 /* Strip any conversions that don't change the mode. This
7782 is safe for every expression, except for a comparison
7783 expression because its signedness is derived from its
7786 Note that this is done as an internal manipulation within
7787 the constant folder, in order to find the simplest
7788 representation of the arguments so that their form can be
7789 studied. In any cases, the appropriate type conversions
7790 should be put back in the tree that will get out of the
7796 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7798 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7799 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7800 fold_build1_loc (loc
, code
, type
,
7801 fold_convert_loc (loc
, TREE_TYPE (op0
),
7802 TREE_OPERAND (arg0
, 1))));
7803 else if (TREE_CODE (arg0
) == COND_EXPR
)
7805 tree arg01
= TREE_OPERAND (arg0
, 1);
7806 tree arg02
= TREE_OPERAND (arg0
, 2);
7807 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7808 arg01
= fold_build1_loc (loc
, code
, type
,
7809 fold_convert_loc (loc
,
7810 TREE_TYPE (op0
), arg01
));
7811 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7812 arg02
= fold_build1_loc (loc
, code
, type
,
7813 fold_convert_loc (loc
,
7814 TREE_TYPE (op0
), arg02
));
7815 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7818 /* If this was a conversion, and all we did was to move into
7819 inside the COND_EXPR, bring it back out. But leave it if
7820 it is a conversion from integer to integer and the
7821 result precision is no wider than a word since such a
7822 conversion is cheap and may be optimized away by combine,
7823 while it couldn't if it were outside the COND_EXPR. Then return
7824 so we don't get into an infinite recursion loop taking the
7825 conversion out and then back in. */
7827 if ((CONVERT_EXPR_CODE_P (code
)
7828 || code
== NON_LVALUE_EXPR
)
7829 && TREE_CODE (tem
) == COND_EXPR
7830 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7831 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7832 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7833 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7834 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7835 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7836 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7838 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7839 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7840 || flag_syntax_only
))
7841 tem
= build1_loc (loc
, code
, type
,
7843 TREE_TYPE (TREE_OPERAND
7844 (TREE_OPERAND (tem
, 1), 0)),
7845 TREE_OPERAND (tem
, 0),
7846 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7847 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7856 /* Re-association barriers around constants and other re-association
7857 barriers can be removed. */
7858 if (CONSTANT_CLASS_P (op0
)
7859 || TREE_CODE (op0
) == PAREN_EXPR
)
7860 return fold_convert_loc (loc
, type
, op0
);
7865 case FIX_TRUNC_EXPR
:
7866 if (TREE_TYPE (op0
) == type
)
7869 if (COMPARISON_CLASS_P (op0
))
7871 /* If we have (type) (a CMP b) and type is an integral type, return
7872 new expression involving the new type. Canonicalize
7873 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7875 Do not fold the result as that would not simplify further, also
7876 folding again results in recursions. */
7877 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7878 return build2_loc (loc
, TREE_CODE (op0
), type
,
7879 TREE_OPERAND (op0
, 0),
7880 TREE_OPERAND (op0
, 1));
7881 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7882 && TREE_CODE (type
) != VECTOR_TYPE
)
7883 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7884 constant_boolean_node (true, type
),
7885 constant_boolean_node (false, type
));
7888 /* Handle cases of two conversions in a row. */
7889 if (CONVERT_EXPR_P (op0
))
7891 tree inside_type
= TREE_TYPE (TREE_OPERAND (op0
, 0));
7892 tree inter_type
= TREE_TYPE (op0
);
7893 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
7894 int inside_ptr
= POINTER_TYPE_P (inside_type
);
7895 int inside_float
= FLOAT_TYPE_P (inside_type
);
7896 int inside_vec
= TREE_CODE (inside_type
) == VECTOR_TYPE
;
7897 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
7898 int inside_unsignedp
= TYPE_UNSIGNED (inside_type
);
7899 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
7900 int inter_ptr
= POINTER_TYPE_P (inter_type
);
7901 int inter_float
= FLOAT_TYPE_P (inter_type
);
7902 int inter_vec
= TREE_CODE (inter_type
) == VECTOR_TYPE
;
7903 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
7904 int inter_unsignedp
= TYPE_UNSIGNED (inter_type
);
7905 int final_int
= INTEGRAL_TYPE_P (type
);
7906 int final_ptr
= POINTER_TYPE_P (type
);
7907 int final_float
= FLOAT_TYPE_P (type
);
7908 int final_vec
= TREE_CODE (type
) == VECTOR_TYPE
;
7909 unsigned int final_prec
= TYPE_PRECISION (type
);
7910 int final_unsignedp
= TYPE_UNSIGNED (type
);
7912 /* In addition to the cases of two conversions in a row
7913 handled below, if we are converting something to its own
7914 type via an object of identical or wider precision, neither
7915 conversion is needed. */
7916 if (TYPE_MAIN_VARIANT (inside_type
) == TYPE_MAIN_VARIANT (type
)
7917 && (((inter_int
|| inter_ptr
) && final_int
)
7918 || (inter_float
&& final_float
))
7919 && inter_prec
>= final_prec
)
7920 return fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 0));
7922 /* Likewise, if the intermediate and initial types are either both
7923 float or both integer, we don't need the middle conversion if the
7924 former is wider than the latter and doesn't change the signedness
7925 (for integers). Avoid this if the final type is a pointer since
7926 then we sometimes need the middle conversion. Likewise if the
7927 final type has a precision not equal to the size of its mode. */
7928 if (((inter_int
&& inside_int
)
7929 || (inter_float
&& inside_float
)
7930 || (inter_vec
&& inside_vec
))
7931 && inter_prec
>= inside_prec
7932 && (inter_float
|| inter_vec
7933 || inter_unsignedp
== inside_unsignedp
)
7934 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
7935 && TYPE_MODE (type
) == TYPE_MODE (inter_type
))
7937 && (! final_vec
|| inter_prec
== inside_prec
))
7938 return fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 0));
7940 /* If we have a sign-extension of a zero-extended value, we can
7941 replace that by a single zero-extension. Likewise if the
7942 final conversion does not change precision we can drop the
7943 intermediate conversion. */
7944 if (inside_int
&& inter_int
&& final_int
7945 && ((inside_prec
< inter_prec
&& inter_prec
< final_prec
7946 && inside_unsignedp
&& !inter_unsignedp
)
7947 || final_prec
== inter_prec
))
7948 return fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 0));
7950 /* Two conversions in a row are not needed unless:
7951 - some conversion is floating-point (overstrict for now), or
7952 - some conversion is a vector (overstrict for now), or
7953 - the intermediate type is narrower than both initial and
7955 - the intermediate type and innermost type differ in signedness,
7956 and the outermost type is wider than the intermediate, or
7957 - the initial type is a pointer type and the precisions of the
7958 intermediate and final types differ, or
7959 - the final type is a pointer type and the precisions of the
7960 initial and intermediate types differ. */
7961 if (! inside_float
&& ! inter_float
&& ! final_float
7962 && ! inside_vec
&& ! inter_vec
&& ! final_vec
7963 && (inter_prec
>= inside_prec
|| inter_prec
>= final_prec
)
7964 && ! (inside_int
&& inter_int
7965 && inter_unsignedp
!= inside_unsignedp
7966 && inter_prec
< final_prec
)
7967 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
7968 == (final_unsignedp
&& final_prec
> inter_prec
))
7969 && ! (inside_ptr
&& inter_prec
!= final_prec
)
7970 && ! (final_ptr
&& inside_prec
!= inter_prec
)
7971 && ! (final_prec
!= GET_MODE_PRECISION (TYPE_MODE (type
))
7972 && TYPE_MODE (type
) == TYPE_MODE (inter_type
)))
7973 return fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 0));
7976 /* Handle (T *)&A.B.C for A being of type T and B and C
7977 living at offset zero. This occurs frequently in
7978 C++ upcasting and then accessing the base. */
7979 if (TREE_CODE (op0
) == ADDR_EXPR
7980 && POINTER_TYPE_P (type
)
7981 && handled_component_p (TREE_OPERAND (op0
, 0)))
7983 HOST_WIDE_INT bitsize
, bitpos
;
7985 enum machine_mode mode
;
7986 int unsignedp
, volatilep
;
7987 tree base
= TREE_OPERAND (op0
, 0);
7988 base
= get_inner_reference (base
, &bitsize
, &bitpos
, &offset
,
7989 &mode
, &unsignedp
, &volatilep
, false);
7990 /* If the reference was to a (constant) zero offset, we can use
7991 the address of the base if it has the same base type
7992 as the result type and the pointer type is unqualified. */
7993 if (! offset
&& bitpos
== 0
7994 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7995 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7996 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7997 return fold_convert_loc (loc
, type
,
7998 build_fold_addr_expr_loc (loc
, base
));
8001 if (TREE_CODE (op0
) == MODIFY_EXPR
8002 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
8003 /* Detect assigning a bitfield. */
8004 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
8006 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
8008 /* Don't leave an assignment inside a conversion
8009 unless assigning a bitfield. */
8010 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
8011 /* First do the assignment, then return converted constant. */
8012 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
8013 TREE_NO_WARNING (tem
) = 1;
8014 TREE_USED (tem
) = 1;
8018 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8019 constants (if x has signed type, the sign bit cannot be set
8020 in c). This folds extension into the BIT_AND_EXPR.
8021 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8022 very likely don't have maximal range for their precision and this
8023 transformation effectively doesn't preserve non-maximal ranges. */
8024 if (TREE_CODE (type
) == INTEGER_TYPE
8025 && TREE_CODE (op0
) == BIT_AND_EXPR
8026 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
8028 tree and_expr
= op0
;
8029 tree and0
= TREE_OPERAND (and_expr
, 0);
8030 tree and1
= TREE_OPERAND (and_expr
, 1);
8033 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
8034 || (TYPE_PRECISION (type
)
8035 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
8037 else if (TYPE_PRECISION (TREE_TYPE (and1
))
8038 <= HOST_BITS_PER_WIDE_INT
8039 && tree_fits_uhwi_p (and1
))
8041 unsigned HOST_WIDE_INT cst
;
8043 cst
= tree_to_uhwi (and1
);
8044 cst
&= HOST_WIDE_INT_M1U
8045 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
8046 change
= (cst
== 0);
8047 #ifdef LOAD_EXTEND_OP
8049 && !flag_syntax_only
8050 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
8053 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
8054 and0
= fold_convert_loc (loc
, uns
, and0
);
8055 and1
= fold_convert_loc (loc
, uns
, and1
);
8061 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
8062 TREE_OVERFLOW (and1
));
8063 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
8064 fold_convert_loc (loc
, type
, and0
), tem
);
8068 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8069 when one of the new casts will fold away. Conservatively we assume
8070 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8071 if (POINTER_TYPE_P (type
)
8072 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8073 && (!TYPE_RESTRICT (type
) || TYPE_RESTRICT (TREE_TYPE (arg0
)))
8074 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8075 || TREE_CODE (TREE_OPERAND (arg0
, 0)) == NOP_EXPR
8076 || TREE_CODE (TREE_OPERAND (arg0
, 1)) == NOP_EXPR
))
8078 tree arg00
= TREE_OPERAND (arg0
, 0);
8079 tree arg01
= TREE_OPERAND (arg0
, 1);
8081 return fold_build_pointer_plus_loc
8082 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
8085 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8086 of the same precision, and X is an integer type not narrower than
8087 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8088 if (INTEGRAL_TYPE_P (type
)
8089 && TREE_CODE (op0
) == BIT_NOT_EXPR
8090 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8091 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
8092 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
8094 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
8095 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
8096 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
8097 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
8098 fold_convert_loc (loc
, type
, tem
));
8101 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8102 type of X and Y (integer types only). */
8103 if (INTEGRAL_TYPE_P (type
)
8104 && TREE_CODE (op0
) == MULT_EXPR
8105 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8106 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
8108 /* Be careful not to introduce new overflows. */
8110 if (TYPE_OVERFLOW_WRAPS (type
))
8113 mult_type
= unsigned_type_for (type
);
8115 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
8117 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
8118 fold_convert_loc (loc
, mult_type
,
8119 TREE_OPERAND (op0
, 0)),
8120 fold_convert_loc (loc
, mult_type
,
8121 TREE_OPERAND (op0
, 1)));
8122 return fold_convert_loc (loc
, type
, tem
);
8126 tem
= fold_convert_const (code
, type
, op0
);
8127 return tem
? tem
: NULL_TREE
;
8129 case ADDR_SPACE_CONVERT_EXPR
:
8130 if (integer_zerop (arg0
))
8131 return fold_convert_const (code
, type
, arg0
);
8134 case FIXED_CONVERT_EXPR
:
8135 tem
= fold_convert_const (code
, type
, arg0
);
8136 return tem
? tem
: NULL_TREE
;
8138 case VIEW_CONVERT_EXPR
:
8139 if (TREE_TYPE (op0
) == type
)
8141 if (TREE_CODE (op0
) == VIEW_CONVERT_EXPR
)
8142 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
,
8143 type
, TREE_OPERAND (op0
, 0));
8144 if (TREE_CODE (op0
) == MEM_REF
)
8145 return fold_build2_loc (loc
, MEM_REF
, type
,
8146 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
8148 /* For integral conversions with the same precision or pointer
8149 conversions use a NOP_EXPR instead. */
8150 if ((INTEGRAL_TYPE_P (type
)
8151 || POINTER_TYPE_P (type
))
8152 && (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8153 || POINTER_TYPE_P (TREE_TYPE (op0
)))
8154 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
8155 return fold_convert_loc (loc
, type
, op0
);
8157 /* Strip inner integral conversions that do not change the precision. */
8158 if (CONVERT_EXPR_P (op0
)
8159 && (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8160 || POINTER_TYPE_P (TREE_TYPE (op0
)))
8161 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0
, 0)))
8162 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0
, 0))))
8163 && (TYPE_PRECISION (TREE_TYPE (op0
))
8164 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
8165 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
,
8166 type
, TREE_OPERAND (op0
, 0));
8168 return fold_view_convert_expr (type
, op0
);
8171 tem
= fold_negate_expr (loc
, arg0
);
8173 return fold_convert_loc (loc
, type
, tem
);
8177 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
8178 return fold_abs_const (arg0
, type
);
8179 else if (TREE_CODE (arg0
) == NEGATE_EXPR
)
8180 return fold_build1_loc (loc
, ABS_EXPR
, type
, TREE_OPERAND (arg0
, 0));
8181 /* Convert fabs((double)float) into (double)fabsf(float). */
8182 else if (TREE_CODE (arg0
) == NOP_EXPR
8183 && TREE_CODE (type
) == REAL_TYPE
)
8185 tree targ0
= strip_float_extensions (arg0
);
8187 return fold_convert_loc (loc
, type
,
8188 fold_build1_loc (loc
, ABS_EXPR
,
8192 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8193 else if (TREE_CODE (arg0
) == ABS_EXPR
)
8195 else if (tree_expr_nonnegative_p (arg0
))
8198 /* Strip sign ops from argument. */
8199 if (TREE_CODE (type
) == REAL_TYPE
)
8201 tem
= fold_strip_sign_ops (arg0
);
8203 return fold_build1_loc (loc
, ABS_EXPR
, type
,
8204 fold_convert_loc (loc
, type
, tem
));
8209 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8210 return fold_convert_loc (loc
, type
, arg0
);
8211 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8213 tree itype
= TREE_TYPE (type
);
8214 tree rpart
= fold_convert_loc (loc
, itype
, TREE_OPERAND (arg0
, 0));
8215 tree ipart
= fold_convert_loc (loc
, itype
, TREE_OPERAND (arg0
, 1));
8216 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
,
8217 negate_expr (ipart
));
8219 if (TREE_CODE (arg0
) == COMPLEX_CST
)
8221 tree itype
= TREE_TYPE (type
);
8222 tree rpart
= fold_convert_loc (loc
, itype
, TREE_REALPART (arg0
));
8223 tree ipart
= fold_convert_loc (loc
, itype
, TREE_IMAGPART (arg0
));
8224 return build_complex (type
, rpart
, negate_expr (ipart
));
8226 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8227 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
8231 if (TREE_CODE (arg0
) == INTEGER_CST
)
8232 return fold_not_const (arg0
, type
);
8233 else if (TREE_CODE (arg0
) == BIT_NOT_EXPR
)
8234 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
8235 /* Convert ~ (-A) to A - 1. */
8236 else if (INTEGRAL_TYPE_P (type
) && TREE_CODE (arg0
) == NEGATE_EXPR
)
8237 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
8238 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0)),
8239 build_int_cst (type
, 1));
8240 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8241 else if (INTEGRAL_TYPE_P (type
)
8242 && ((TREE_CODE (arg0
) == MINUS_EXPR
8243 && integer_onep (TREE_OPERAND (arg0
, 1)))
8244 || (TREE_CODE (arg0
) == PLUS_EXPR
8245 && integer_all_onesp (TREE_OPERAND (arg0
, 1)))))
8246 return fold_build1_loc (loc
, NEGATE_EXPR
, type
,
8247 fold_convert_loc (loc
, type
,
8248 TREE_OPERAND (arg0
, 0)));
8249 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8250 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8251 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8252 fold_convert_loc (loc
, type
,
8253 TREE_OPERAND (arg0
, 0)))))
8254 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
8255 fold_convert_loc (loc
, type
,
8256 TREE_OPERAND (arg0
, 1)));
8257 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8258 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8259 fold_convert_loc (loc
, type
,
8260 TREE_OPERAND (arg0
, 1)))))
8261 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
8262 fold_convert_loc (loc
, type
,
8263 TREE_OPERAND (arg0
, 0)), tem
);
8264 /* Perform BIT_NOT_EXPR on each element individually. */
8265 else if (TREE_CODE (arg0
) == VECTOR_CST
)
8269 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
8271 elements
= XALLOCAVEC (tree
, count
);
8272 for (i
= 0; i
< count
; i
++)
8274 elem
= VECTOR_CST_ELT (arg0
, i
);
8275 elem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
8276 if (elem
== NULL_TREE
)
8281 return build_vector (type
, elements
);
8283 else if (COMPARISON_CLASS_P (arg0
)
8284 && (VECTOR_TYPE_P (type
)
8285 || (INTEGRAL_TYPE_P (type
) && TYPE_PRECISION (type
) == 1)))
8287 tree op_type
= TREE_TYPE (TREE_OPERAND (arg0
, 0));
8288 enum tree_code subcode
= invert_tree_comparison (TREE_CODE (arg0
),
8289 HONOR_NANS (TYPE_MODE (op_type
)));
8290 if (subcode
!= ERROR_MARK
)
8291 return build2_loc (loc
, subcode
, type
, TREE_OPERAND (arg0
, 0),
8292 TREE_OPERAND (arg0
, 1));
8298 case TRUTH_NOT_EXPR
:
8299 /* Note that the operand of this must be an int
8300 and its values must be 0 or 1.
8301 ("true" is a fixed value perhaps depending on the language,
8302 but we don't handle values other than 1 correctly yet.) */
8303 tem
= fold_truth_not_expr (loc
, arg0
);
8306 return fold_convert_loc (loc
, type
, tem
);
8309 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8310 return fold_convert_loc (loc
, type
, arg0
);
8311 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8312 return omit_one_operand_loc (loc
, type
, TREE_OPERAND (arg0
, 0),
8313 TREE_OPERAND (arg0
, 1));
8314 if (TREE_CODE (arg0
) == COMPLEX_CST
)
8315 return fold_convert_loc (loc
, type
, TREE_REALPART (arg0
));
8316 if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8318 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8319 tem
= fold_build2_loc (loc
, TREE_CODE (arg0
), itype
,
8320 fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8321 TREE_OPERAND (arg0
, 0)),
8322 fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8323 TREE_OPERAND (arg0
, 1)));
8324 return fold_convert_loc (loc
, type
, tem
);
8326 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8328 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8329 tem
= fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8330 TREE_OPERAND (arg0
, 0));
8331 return fold_convert_loc (loc
, type
, tem
);
8333 if (TREE_CODE (arg0
) == CALL_EXPR
)
8335 tree fn
= get_callee_fndecl (arg0
);
8336 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8337 switch (DECL_FUNCTION_CODE (fn
))
8339 CASE_FLT_FN (BUILT_IN_CEXPI
):
8340 fn
= mathfn_built_in (type
, BUILT_IN_COS
);
8342 return build_call_expr_loc (loc
, fn
, 1, CALL_EXPR_ARG (arg0
, 0));
8352 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8353 return build_zero_cst (type
);
8354 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8355 return omit_one_operand_loc (loc
, type
, TREE_OPERAND (arg0
, 1),
8356 TREE_OPERAND (arg0
, 0));
8357 if (TREE_CODE (arg0
) == COMPLEX_CST
)
8358 return fold_convert_loc (loc
, type
, TREE_IMAGPART (arg0
));
8359 if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8361 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8362 tem
= fold_build2_loc (loc
, TREE_CODE (arg0
), itype
,
8363 fold_build1_loc (loc
, IMAGPART_EXPR
, itype
,
8364 TREE_OPERAND (arg0
, 0)),
8365 fold_build1_loc (loc
, IMAGPART_EXPR
, itype
,
8366 TREE_OPERAND (arg0
, 1)));
8367 return fold_convert_loc (loc
, type
, tem
);
8369 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8371 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8372 tem
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, TREE_OPERAND (arg0
, 0));
8373 return fold_convert_loc (loc
, type
, negate_expr (tem
));
8375 if (TREE_CODE (arg0
) == CALL_EXPR
)
8377 tree fn
= get_callee_fndecl (arg0
);
8378 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8379 switch (DECL_FUNCTION_CODE (fn
))
8381 CASE_FLT_FN (BUILT_IN_CEXPI
):
8382 fn
= mathfn_built_in (type
, BUILT_IN_SIN
);
8384 return build_call_expr_loc (loc
, fn
, 1, CALL_EXPR_ARG (arg0
, 0));
8394 /* Fold *&X to X if X is an lvalue. */
8395 if (TREE_CODE (op0
) == ADDR_EXPR
)
8397 tree op00
= TREE_OPERAND (op0
, 0);
8398 if ((TREE_CODE (op00
) == VAR_DECL
8399 || TREE_CODE (op00
) == PARM_DECL
8400 || TREE_CODE (op00
) == RESULT_DECL
)
8401 && !TREE_READONLY (op00
))
8406 case VEC_UNPACK_LO_EXPR
:
8407 case VEC_UNPACK_HI_EXPR
:
8408 case VEC_UNPACK_FLOAT_LO_EXPR
:
8409 case VEC_UNPACK_FLOAT_HI_EXPR
:
8411 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8413 enum tree_code subcode
;
8415 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
8416 if (TREE_CODE (arg0
) != VECTOR_CST
)
8419 elts
= XALLOCAVEC (tree
, nelts
* 2);
8420 if (!vec_cst_ctor_to_array (arg0
, elts
))
8423 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
8424 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
8427 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
8430 subcode
= FLOAT_EXPR
;
8432 for (i
= 0; i
< nelts
; i
++)
8434 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
8435 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
8439 return build_vector (type
, elts
);
8442 case REDUC_MIN_EXPR
:
8443 case REDUC_MAX_EXPR
:
8444 case REDUC_PLUS_EXPR
:
8446 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8448 enum tree_code subcode
;
8450 if (TREE_CODE (op0
) != VECTOR_CST
)
8453 elts
= XALLOCAVEC (tree
, nelts
);
8454 if (!vec_cst_ctor_to_array (op0
, elts
))
8459 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
8460 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
8461 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
8462 default: gcc_unreachable ();
8465 for (i
= 1; i
< nelts
; i
++)
8467 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
8468 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
8470 elts
[i
] = build_zero_cst (TREE_TYPE (type
));
8473 return build_vector (type
, elts
);
8478 } /* switch (code) */
8482 /* If the operation was a conversion do _not_ mark a resulting constant
8483 with TREE_OVERFLOW if the original constant was not. These conversions
8484 have implementation defined behavior and retaining the TREE_OVERFLOW
8485 flag here would confuse later passes such as VRP. */
8487 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
8488 tree type
, tree op0
)
8490 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
8492 && TREE_CODE (res
) == INTEGER_CST
8493 && TREE_CODE (op0
) == INTEGER_CST
8494 && CONVERT_EXPR_CODE_P (code
))
8495 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
8500 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8501 operands OP0 and OP1. LOC is the location of the resulting expression.
8502 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8503 Return the folded expression if folding is successful. Otherwise,
8504 return NULL_TREE. */
8506 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
8507 tree arg0
, tree arg1
, tree op0
, tree op1
)
8511 /* We only do these simplifications if we are optimizing. */
8515 /* Check for things like (A || B) && (A || C). We can convert this
8516 to A || (B && C). Note that either operator can be any of the four
8517 truth and/or operations and the transformation will still be
8518 valid. Also note that we only care about order for the
8519 ANDIF and ORIF operators. If B contains side effects, this
8520 might change the truth-value of A. */
8521 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8522 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
8523 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
8524 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
8525 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
8526 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
8528 tree a00
= TREE_OPERAND (arg0
, 0);
8529 tree a01
= TREE_OPERAND (arg0
, 1);
8530 tree a10
= TREE_OPERAND (arg1
, 0);
8531 tree a11
= TREE_OPERAND (arg1
, 1);
8532 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
8533 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
8534 && (code
== TRUTH_AND_EXPR
8535 || code
== TRUTH_OR_EXPR
));
8537 if (operand_equal_p (a00
, a10
, 0))
8538 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8539 fold_build2_loc (loc
, code
, type
, a01
, a11
));
8540 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
8541 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8542 fold_build2_loc (loc
, code
, type
, a01
, a10
));
8543 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
8544 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
8545 fold_build2_loc (loc
, code
, type
, a00
, a11
));
8547 /* This case if tricky because we must either have commutative
8548 operators or else A10 must not have side-effects. */
8550 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
8551 && operand_equal_p (a01
, a11
, 0))
8552 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
8553 fold_build2_loc (loc
, code
, type
, a00
, a10
),
8557 /* See if we can build a range comparison. */
8558 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
8561 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
8562 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8564 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8566 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8569 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8570 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8572 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8574 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8577 /* Check for the possibility of merging component references. If our
8578 lhs is another similar operation, try to merge its rhs with our
8579 rhs. Then try to merge our lhs and rhs. */
8580 if (TREE_CODE (arg0
) == code
8581 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8582 TREE_OPERAND (arg0
, 1), arg1
)))
8583 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8585 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8588 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8589 && (code
== TRUTH_AND_EXPR
8590 || code
== TRUTH_ANDIF_EXPR
8591 || code
== TRUTH_OR_EXPR
8592 || code
== TRUTH_ORIF_EXPR
))
8594 enum tree_code ncode
, icode
;
8596 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8597 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8598 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8600 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8601 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8602 We don't want to pack more than two leafs to a non-IF AND/OR
8604 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8605 equal to IF-CODE, then we don't want to add right-hand operand.
8606 If the inner right-hand side of left-hand operand has
8607 side-effects, or isn't simple, then we can't add to it,
8608 as otherwise we might destroy if-sequence. */
8609 if (TREE_CODE (arg0
) == icode
8610 && simple_operand_p_2 (arg1
)
8611 /* Needed for sequence points to handle trappings, and
8613 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8615 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8617 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8620 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8621 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8622 else if (TREE_CODE (arg1
) == icode
8623 && simple_operand_p_2 (arg0
)
8624 /* Needed for sequence points to handle trappings, and
8626 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8628 tem
= fold_build2_loc (loc
, ncode
, type
,
8629 arg0
, TREE_OPERAND (arg1
, 0));
8630 return fold_build2_loc (loc
, icode
, type
, tem
,
8631 TREE_OPERAND (arg1
, 1));
8633 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8635 For sequence point consistancy, we need to check for trapping,
8636 and side-effects. */
8637 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8638 && simple_operand_p_2 (arg1
))
8639 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8645 /* Fold a binary expression of code CODE and type TYPE with operands
8646 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8647 Return the folded expression if folding is successful. Otherwise,
8648 return NULL_TREE. */
8651 fold_minmax (location_t loc
, enum tree_code code
, tree type
, tree op0
, tree op1
)
8653 enum tree_code compl_code
;
8655 if (code
== MIN_EXPR
)
8656 compl_code
= MAX_EXPR
;
8657 else if (code
== MAX_EXPR
)
8658 compl_code
= MIN_EXPR
;
8662 /* MIN (MAX (a, b), b) == b. */
8663 if (TREE_CODE (op0
) == compl_code
8664 && operand_equal_p (TREE_OPERAND (op0
, 1), op1
, 0))
8665 return omit_one_operand_loc (loc
, type
, op1
, TREE_OPERAND (op0
, 0));
8667 /* MIN (MAX (b, a), b) == b. */
8668 if (TREE_CODE (op0
) == compl_code
8669 && operand_equal_p (TREE_OPERAND (op0
, 0), op1
, 0)
8670 && reorder_operands_p (TREE_OPERAND (op0
, 1), op1
))
8671 return omit_one_operand_loc (loc
, type
, op1
, TREE_OPERAND (op0
, 1));
8673 /* MIN (a, MAX (a, b)) == a. */
8674 if (TREE_CODE (op1
) == compl_code
8675 && operand_equal_p (op0
, TREE_OPERAND (op1
, 0), 0)
8676 && reorder_operands_p (op0
, TREE_OPERAND (op1
, 1)))
8677 return omit_one_operand_loc (loc
, type
, op0
, TREE_OPERAND (op1
, 1));
8679 /* MIN (a, MAX (b, a)) == a. */
8680 if (TREE_CODE (op1
) == compl_code
8681 && operand_equal_p (op0
, TREE_OPERAND (op1
, 1), 0)
8682 && reorder_operands_p (op0
, TREE_OPERAND (op1
, 0)))
8683 return omit_one_operand_loc (loc
, type
, op0
, TREE_OPERAND (op1
, 0));
8688 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8689 by changing CODE to reduce the magnitude of constants involved in
8690 ARG0 of the comparison.
8691 Returns a canonicalized comparison tree if a simplification was
8692 possible, otherwise returns NULL_TREE.
8693 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8694 valid if signed overflow is undefined. */
8697 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8698 tree arg0
, tree arg1
,
8699 bool *strict_overflow_p
)
8701 enum tree_code code0
= TREE_CODE (arg0
);
8702 tree t
, cst0
= NULL_TREE
;
8706 /* Match A +- CST code arg1 and CST code arg1. We can change the
8707 first form only if overflow is undefined. */
8708 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8709 /* In principle pointers also have undefined overflow behavior,
8710 but that causes problems elsewhere. */
8711 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8712 && (code0
== MINUS_EXPR
8713 || code0
== PLUS_EXPR
)
8714 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
8715 || code0
== INTEGER_CST
))
8718 /* Identify the constant in arg0 and its sign. */
8719 if (code0
== INTEGER_CST
)
8722 cst0
= TREE_OPERAND (arg0
, 1);
8723 sgn0
= tree_int_cst_sgn (cst0
);
8725 /* Overflowed constants and zero will cause problems. */
8726 if (integer_zerop (cst0
)
8727 || TREE_OVERFLOW (cst0
))
8730 /* See if we can reduce the magnitude of the constant in
8731 arg0 by changing the comparison code. */
8732 if (code0
== INTEGER_CST
)
8734 /* CST <= arg1 -> CST-1 < arg1. */
8735 if (code
== LE_EXPR
&& sgn0
== 1)
8737 /* -CST < arg1 -> -CST-1 <= arg1. */
8738 else if (code
== LT_EXPR
&& sgn0
== -1)
8740 /* CST > arg1 -> CST-1 >= arg1. */
8741 else if (code
== GT_EXPR
&& sgn0
== 1)
8743 /* -CST >= arg1 -> -CST-1 > arg1. */
8744 else if (code
== GE_EXPR
&& sgn0
== -1)
8748 /* arg1 code' CST' might be more canonical. */
8753 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8755 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8757 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8758 else if (code
== GT_EXPR
8759 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8761 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8762 else if (code
== LE_EXPR
8763 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8765 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8766 else if (code
== GE_EXPR
8767 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8771 *strict_overflow_p
= true;
8774 /* Now build the constant reduced in magnitude. But not if that
8775 would produce one outside of its types range. */
8776 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8778 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8779 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8781 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8782 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8783 /* We cannot swap the comparison here as that would cause us to
8784 endlessly recurse. */
8787 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8788 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8789 if (code0
!= INTEGER_CST
)
8790 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8791 t
= fold_convert (TREE_TYPE (arg1
), t
);
8793 /* If swapping might yield to a more canonical form, do so. */
8795 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, arg1
, t
);
8797 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8800 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8801 overflow further. Try to decrease the magnitude of constants involved
8802 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8803 and put sole constants at the second argument position.
8804 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8807 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8808 tree arg0
, tree arg1
)
8811 bool strict_overflow_p
;
8812 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8813 "when reducing constant in comparison");
8815 /* Try canonicalization by simplifying arg0. */
8816 strict_overflow_p
= false;
8817 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8818 &strict_overflow_p
);
8821 if (strict_overflow_p
)
8822 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8826 /* Try canonicalization by simplifying arg1 using the swapped
8828 code
= swap_tree_comparison (code
);
8829 strict_overflow_p
= false;
8830 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8831 &strict_overflow_p
);
8832 if (t
&& strict_overflow_p
)
8833 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8837 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8838 space. This is used to avoid issuing overflow warnings for
8839 expressions like &p->x which can not wrap. */
8842 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8844 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8851 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8852 if (offset
== NULL_TREE
)
8853 wi_offset
= wi::zero (precision
);
8854 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8860 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8861 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8865 if (!wi::fits_uhwi_p (total
))
8868 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8872 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8874 if (TREE_CODE (base
) == ADDR_EXPR
)
8876 HOST_WIDE_INT base_size
;
8878 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8879 if (base_size
> 0 && size
< base_size
)
8883 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8886 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8887 kind INTEGER_CST. This makes sure to properly sign-extend the
8890 static HOST_WIDE_INT
8891 size_low_cst (const_tree t
)
8893 HOST_WIDE_INT w
= TREE_INT_CST_ELT (t
, 0);
8894 int prec
= TYPE_PRECISION (TREE_TYPE (t
));
8895 if (prec
< HOST_BITS_PER_WIDE_INT
)
8896 return sext_hwi (w
, prec
);
8900 /* Subroutine of fold_binary. This routine performs all of the
8901 transformations that are common to the equality/inequality
8902 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8903 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8904 fold_binary should call fold_binary. Fold a comparison with
8905 tree code CODE and type TYPE with operands OP0 and OP1. Return
8906 the folded comparison or NULL_TREE. */
8909 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8912 tree arg0
, arg1
, tem
;
8917 STRIP_SIGN_NOPS (arg0
);
8918 STRIP_SIGN_NOPS (arg1
);
8920 tem
= fold_relational_const (code
, type
, arg0
, arg1
);
8921 if (tem
!= NULL_TREE
)
8924 /* If one arg is a real or integer constant, put it last. */
8925 if (tree_swap_operands_p (arg0
, arg1
, true))
8926 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
8928 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8929 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8930 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8931 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
8933 && (TREE_CODE (arg1
) == INTEGER_CST
8934 && !TREE_OVERFLOW (arg1
)))
8936 tree const1
= TREE_OPERAND (arg0
, 1);
8938 tree variable
= TREE_OPERAND (arg0
, 0);
8941 lhs_add
= TREE_CODE (arg0
) != PLUS_EXPR
;
8943 lhs
= fold_build2_loc (loc
, lhs_add
? PLUS_EXPR
: MINUS_EXPR
,
8944 TREE_TYPE (arg1
), const2
, const1
);
8946 /* If the constant operation overflowed this can be
8947 simplified as a comparison against INT_MAX/INT_MIN. */
8948 if (TREE_CODE (lhs
) == INTEGER_CST
8949 && TREE_OVERFLOW (lhs
))
8951 int const1_sgn
= tree_int_cst_sgn (const1
);
8952 enum tree_code code2
= code
;
8954 /* Get the sign of the constant on the lhs if the
8955 operation were VARIABLE + CONST1. */
8956 if (TREE_CODE (arg0
) == MINUS_EXPR
)
8957 const1_sgn
= -const1_sgn
;
8959 /* The sign of the constant determines if we overflowed
8960 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8961 Canonicalize to the INT_MIN overflow by swapping the comparison
8963 if (const1_sgn
== -1)
8964 code2
= swap_tree_comparison (code
);
8966 /* We now can look at the canonicalized case
8967 VARIABLE + 1 CODE2 INT_MIN
8968 and decide on the result. */
8969 if (code2
== LT_EXPR
8971 || code2
== EQ_EXPR
)
8972 return omit_one_operand_loc (loc
, type
, boolean_false_node
, variable
);
8973 else if (code2
== NE_EXPR
8975 || code2
== GT_EXPR
)
8976 return omit_one_operand_loc (loc
, type
, boolean_true_node
, variable
);
8979 if (TREE_CODE (lhs
) == TREE_CODE (arg1
)
8980 && (TREE_CODE (lhs
) != INTEGER_CST
8981 || !TREE_OVERFLOW (lhs
)))
8983 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
8984 fold_overflow_warning ("assuming signed overflow does not occur "
8985 "when changing X +- C1 cmp C2 to "
8987 WARN_STRICT_OVERFLOW_COMPARISON
);
8988 return fold_build2_loc (loc
, code
, type
, variable
, lhs
);
8992 /* For comparisons of pointers we can decompose it to a compile time
8993 comparison of the base objects and the offsets into the object.
8994 This requires at least one operand being an ADDR_EXPR or a
8995 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8996 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8997 && (TREE_CODE (arg0
) == ADDR_EXPR
8998 || TREE_CODE (arg1
) == ADDR_EXPR
8999 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
9000 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
9002 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
9003 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
9004 enum machine_mode mode
;
9005 int volatilep
, unsignedp
;
9006 bool indirect_base0
= false, indirect_base1
= false;
9008 /* Get base and offset for the access. Strip ADDR_EXPR for
9009 get_inner_reference, but put it back by stripping INDIRECT_REF
9010 off the base object if possible. indirect_baseN will be true
9011 if baseN is not an address but refers to the object itself. */
9013 if (TREE_CODE (arg0
) == ADDR_EXPR
)
9015 base0
= get_inner_reference (TREE_OPERAND (arg0
, 0),
9016 &bitsize
, &bitpos0
, &offset0
, &mode
,
9017 &unsignedp
, &volatilep
, false);
9018 if (TREE_CODE (base0
) == INDIRECT_REF
)
9019 base0
= TREE_OPERAND (base0
, 0);
9021 indirect_base0
= true;
9023 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
9025 base0
= TREE_OPERAND (arg0
, 0);
9026 STRIP_SIGN_NOPS (base0
);
9027 if (TREE_CODE (base0
) == ADDR_EXPR
)
9029 base0
= TREE_OPERAND (base0
, 0);
9030 indirect_base0
= true;
9032 offset0
= TREE_OPERAND (arg0
, 1);
9033 if (tree_fits_shwi_p (offset0
))
9035 HOST_WIDE_INT off
= size_low_cst (offset0
);
9036 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
9038 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
9040 bitpos0
= off
* BITS_PER_UNIT
;
9041 offset0
= NULL_TREE
;
9047 if (TREE_CODE (arg1
) == ADDR_EXPR
)
9049 base1
= get_inner_reference (TREE_OPERAND (arg1
, 0),
9050 &bitsize
, &bitpos1
, &offset1
, &mode
,
9051 &unsignedp
, &volatilep
, false);
9052 if (TREE_CODE (base1
) == INDIRECT_REF
)
9053 base1
= TREE_OPERAND (base1
, 0);
9055 indirect_base1
= true;
9057 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
9059 base1
= TREE_OPERAND (arg1
, 0);
9060 STRIP_SIGN_NOPS (base1
);
9061 if (TREE_CODE (base1
) == ADDR_EXPR
)
9063 base1
= TREE_OPERAND (base1
, 0);
9064 indirect_base1
= true;
9066 offset1
= TREE_OPERAND (arg1
, 1);
9067 if (tree_fits_shwi_p (offset1
))
9069 HOST_WIDE_INT off
= size_low_cst (offset1
);
9070 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
9072 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
9074 bitpos1
= off
* BITS_PER_UNIT
;
9075 offset1
= NULL_TREE
;
9080 /* A local variable can never be pointed to by
9081 the default SSA name of an incoming parameter. */
9082 if ((TREE_CODE (arg0
) == ADDR_EXPR
9084 && TREE_CODE (base0
) == VAR_DECL
9085 && auto_var_in_fn_p (base0
, current_function_decl
)
9087 && TREE_CODE (base1
) == SSA_NAME
9088 && SSA_NAME_IS_DEFAULT_DEF (base1
)
9089 && TREE_CODE (SSA_NAME_VAR (base1
)) == PARM_DECL
)
9090 || (TREE_CODE (arg1
) == ADDR_EXPR
9092 && TREE_CODE (base1
) == VAR_DECL
9093 && auto_var_in_fn_p (base1
, current_function_decl
)
9095 && TREE_CODE (base0
) == SSA_NAME
9096 && SSA_NAME_IS_DEFAULT_DEF (base0
)
9097 && TREE_CODE (SSA_NAME_VAR (base0
)) == PARM_DECL
))
9099 if (code
== NE_EXPR
)
9100 return constant_boolean_node (1, type
);
9101 else if (code
== EQ_EXPR
)
9102 return constant_boolean_node (0, type
);
9104 /* If we have equivalent bases we might be able to simplify. */
9105 else if (indirect_base0
== indirect_base1
9106 && operand_equal_p (base0
, base1
, 0))
9108 /* We can fold this expression to a constant if the non-constant
9109 offset parts are equal. */
9110 if ((offset0
== offset1
9111 || (offset0
&& offset1
9112 && operand_equal_p (offset0
, offset1
, 0)))
9115 || (indirect_base0
&& DECL_P (base0
))
9116 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
9121 && bitpos0
!= bitpos1
9122 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
9123 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
9124 fold_overflow_warning (("assuming pointer wraparound does not "
9125 "occur when comparing P +- C1 with "
9127 WARN_STRICT_OVERFLOW_CONDITIONAL
);
9132 return constant_boolean_node (bitpos0
== bitpos1
, type
);
9134 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
9136 return constant_boolean_node (bitpos0
< bitpos1
, type
);
9138 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
9140 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
9142 return constant_boolean_node (bitpos0
> bitpos1
, type
);
9146 /* We can simplify the comparison to a comparison of the variable
9147 offset parts if the constant offset parts are equal.
9148 Be careful to use signed sizetype here because otherwise we
9149 mess with array offsets in the wrong way. This is possible
9150 because pointer arithmetic is restricted to retain within an
9151 object and overflow on pointer differences is undefined as of
9152 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9153 else if (bitpos0
== bitpos1
9154 && ((code
== EQ_EXPR
|| code
== NE_EXPR
)
9155 || (indirect_base0
&& DECL_P (base0
))
9156 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
9158 /* By converting to signed sizetype we cover middle-end pointer
9159 arithmetic which operates on unsigned pointer types of size
9160 type size and ARRAY_REF offsets which are properly sign or
9161 zero extended from their type in case it is narrower than
9163 if (offset0
== NULL_TREE
)
9164 offset0
= build_int_cst (ssizetype
, 0);
9166 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
9167 if (offset1
== NULL_TREE
)
9168 offset1
= build_int_cst (ssizetype
, 0);
9170 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
9174 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
9175 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
9176 fold_overflow_warning (("assuming pointer wraparound does not "
9177 "occur when comparing P +- C1 with "
9179 WARN_STRICT_OVERFLOW_COMPARISON
);
9181 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
9184 /* For non-equal bases we can simplify if they are addresses
9185 of local binding decls or constants. */
9186 else if (indirect_base0
&& indirect_base1
9187 /* We know that !operand_equal_p (base0, base1, 0)
9188 because the if condition was false. But make
9189 sure two decls are not the same. */
9191 && TREE_CODE (arg0
) == ADDR_EXPR
9192 && TREE_CODE (arg1
) == ADDR_EXPR
9193 && (((TREE_CODE (base0
) == VAR_DECL
9194 || TREE_CODE (base0
) == PARM_DECL
)
9195 && (targetm
.binds_local_p (base0
)
9196 || CONSTANT_CLASS_P (base1
)))
9197 || CONSTANT_CLASS_P (base0
))
9198 && (((TREE_CODE (base1
) == VAR_DECL
9199 || TREE_CODE (base1
) == PARM_DECL
)
9200 && (targetm
.binds_local_p (base1
)
9201 || CONSTANT_CLASS_P (base0
)))
9202 || CONSTANT_CLASS_P (base1
)))
9204 if (code
== EQ_EXPR
)
9205 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
9207 else if (code
== NE_EXPR
)
9208 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
9211 /* For equal offsets we can simplify to a comparison of the
9213 else if (bitpos0
== bitpos1
9215 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
9217 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
9218 && ((offset0
== offset1
)
9219 || (offset0
&& offset1
9220 && operand_equal_p (offset0
, offset1
, 0))))
9223 base0
= build_fold_addr_expr_loc (loc
, base0
);
9225 base1
= build_fold_addr_expr_loc (loc
, base1
);
9226 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
9230 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9231 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9232 the resulting offset is smaller in absolute value than the
9234 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
9235 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
9236 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9237 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
9238 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
9239 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
9240 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
9242 tree const1
= TREE_OPERAND (arg0
, 1);
9243 tree const2
= TREE_OPERAND (arg1
, 1);
9244 tree variable1
= TREE_OPERAND (arg0
, 0);
9245 tree variable2
= TREE_OPERAND (arg1
, 0);
9247 const char * const warnmsg
= G_("assuming signed overflow does not "
9248 "occur when combining constants around "
9251 /* Put the constant on the side where it doesn't overflow and is
9252 of lower absolute value than before. */
9253 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9254 ? MINUS_EXPR
: PLUS_EXPR
,
9256 if (!TREE_OVERFLOW (cst
)
9257 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
))
9259 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
9260 return fold_build2_loc (loc
, code
, type
,
9262 fold_build2_loc (loc
,
9263 TREE_CODE (arg1
), TREE_TYPE (arg1
),
9267 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9268 ? MINUS_EXPR
: PLUS_EXPR
,
9270 if (!TREE_OVERFLOW (cst
)
9271 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
))
9273 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
9274 return fold_build2_loc (loc
, code
, type
,
9275 fold_build2_loc (loc
, TREE_CODE (arg0
), TREE_TYPE (arg0
),
9281 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9282 signed arithmetic case. That form is created by the compiler
9283 often enough for folding it to be of value. One example is in
9284 computing loop trip counts after Operator Strength Reduction. */
9285 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
9286 && TREE_CODE (arg0
) == MULT_EXPR
9287 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9288 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
9289 && integer_zerop (arg1
))
9291 tree const1
= TREE_OPERAND (arg0
, 1);
9292 tree const2
= arg1
; /* zero */
9293 tree variable1
= TREE_OPERAND (arg0
, 0);
9294 enum tree_code cmp_code
= code
;
9296 /* Handle unfolded multiplication by zero. */
9297 if (integer_zerop (const1
))
9298 return fold_build2_loc (loc
, cmp_code
, type
, const1
, const2
);
9300 fold_overflow_warning (("assuming signed overflow does not occur when "
9301 "eliminating multiplication in comparison "
9303 WARN_STRICT_OVERFLOW_COMPARISON
);
9305 /* If const1 is negative we swap the sense of the comparison. */
9306 if (tree_int_cst_sgn (const1
) < 0)
9307 cmp_code
= swap_tree_comparison (cmp_code
);
9309 return fold_build2_loc (loc
, cmp_code
, type
, variable1
, const2
);
9312 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
9316 if (FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9318 tree targ0
= strip_float_extensions (arg0
);
9319 tree targ1
= strip_float_extensions (arg1
);
9320 tree newtype
= TREE_TYPE (targ0
);
9322 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
9323 newtype
= TREE_TYPE (targ1
);
9325 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9326 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
9327 return fold_build2_loc (loc
, code
, type
,
9328 fold_convert_loc (loc
, newtype
, targ0
),
9329 fold_convert_loc (loc
, newtype
, targ1
));
9331 /* (-a) CMP (-b) -> b CMP a */
9332 if (TREE_CODE (arg0
) == NEGATE_EXPR
9333 && TREE_CODE (arg1
) == NEGATE_EXPR
)
9334 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg1
, 0),
9335 TREE_OPERAND (arg0
, 0));
9337 if (TREE_CODE (arg1
) == REAL_CST
)
9339 REAL_VALUE_TYPE cst
;
9340 cst
= TREE_REAL_CST (arg1
);
9342 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9343 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
9344 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
,
9345 TREE_OPERAND (arg0
, 0),
9346 build_real (TREE_TYPE (arg1
),
9347 real_value_negate (&cst
)));
9349 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9350 /* a CMP (-0) -> a CMP 0 */
9351 if (REAL_VALUE_MINUS_ZERO (cst
))
9352 return fold_build2_loc (loc
, code
, type
, arg0
,
9353 build_real (TREE_TYPE (arg1
), dconst0
));
9355 /* x != NaN is always true, other ops are always false. */
9356 if (REAL_VALUE_ISNAN (cst
)
9357 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
))))
9359 tem
= (code
== NE_EXPR
) ? integer_one_node
: integer_zero_node
;
9360 return omit_one_operand_loc (loc
, type
, tem
, arg0
);
9363 /* Fold comparisons against infinity. */
9364 if (REAL_VALUE_ISINF (cst
)
9365 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
))))
9367 tem
= fold_inf_compare (loc
, code
, type
, arg0
, arg1
);
9368 if (tem
!= NULL_TREE
)
9373 /* If this is a comparison of a real constant with a PLUS_EXPR
9374 or a MINUS_EXPR of a real constant, we can convert it into a
9375 comparison with a revised real constant as long as no overflow
9376 occurs when unsafe_math_optimizations are enabled. */
9377 if (flag_unsafe_math_optimizations
9378 && TREE_CODE (arg1
) == REAL_CST
9379 && (TREE_CODE (arg0
) == PLUS_EXPR
9380 || TREE_CODE (arg0
) == MINUS_EXPR
)
9381 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
9382 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
9383 ? MINUS_EXPR
: PLUS_EXPR
,
9384 arg1
, TREE_OPERAND (arg0
, 1)))
9385 && !TREE_OVERFLOW (tem
))
9386 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9388 /* Likewise, we can simplify a comparison of a real constant with
9389 a MINUS_EXPR whose first operand is also a real constant, i.e.
9390 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9391 floating-point types only if -fassociative-math is set. */
9392 if (flag_associative_math
9393 && TREE_CODE (arg1
) == REAL_CST
9394 && TREE_CODE (arg0
) == MINUS_EXPR
9395 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
9396 && 0 != (tem
= const_binop (MINUS_EXPR
, TREE_OPERAND (arg0
, 0),
9398 && !TREE_OVERFLOW (tem
))
9399 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
,
9400 TREE_OPERAND (arg0
, 1), tem
);
9402 /* Fold comparisons against built-in math functions. */
9403 if (TREE_CODE (arg1
) == REAL_CST
9404 && flag_unsafe_math_optimizations
9405 && ! flag_errno_math
)
9407 enum built_in_function fcode
= builtin_mathfn_code (arg0
);
9409 if (fcode
!= END_BUILTINS
)
9411 tem
= fold_mathfn_compare (loc
, fcode
, code
, type
, arg0
, arg1
);
9412 if (tem
!= NULL_TREE
)
9418 if (TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
9419 && CONVERT_EXPR_P (arg0
))
9421 /* If we are widening one operand of an integer comparison,
9422 see if the other operand is similarly being widened. Perhaps we
9423 can do the comparison in the narrower type. */
9424 tem
= fold_widened_comparison (loc
, code
, type
, arg0
, arg1
);
9428 /* Or if we are changing signedness. */
9429 tem
= fold_sign_changed_comparison (loc
, code
, type
, arg0
, arg1
);
9434 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9435 constant, we can simplify it. */
9436 if (TREE_CODE (arg1
) == INTEGER_CST
9437 && (TREE_CODE (arg0
) == MIN_EXPR
9438 || TREE_CODE (arg0
) == MAX_EXPR
)
9439 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9441 tem
= optimize_minmax_comparison (loc
, code
, type
, op0
, op1
);
9446 /* Simplify comparison of something with itself. (For IEEE
9447 floating-point, we can only do some of these simplifications.) */
9448 if (operand_equal_p (arg0
, arg1
, 0))
9453 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
9454 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9455 return constant_boolean_node (1, type
);
9460 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
9461 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9462 return constant_boolean_node (1, type
);
9463 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
, arg1
);
9466 /* For NE, we can only do this simplification if integer
9467 or we don't honor IEEE floating point NaNs. */
9468 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
9469 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9471 /* ... fall through ... */
9474 return constant_boolean_node (0, type
);
9480 /* If we are comparing an expression that just has comparisons
9481 of two integer values, arithmetic expressions of those comparisons,
9482 and constants, we can simplify it. There are only three cases
9483 to check: the two values can either be equal, the first can be
9484 greater, or the second can be greater. Fold the expression for
9485 those three values. Since each value must be 0 or 1, we have
9486 eight possibilities, each of which corresponds to the constant 0
9487 or 1 or one of the six possible comparisons.
9489 This handles common cases like (a > b) == 0 but also handles
9490 expressions like ((x > y) - (y > x)) > 0, which supposedly
9491 occur in macroized code. */
9493 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
9495 tree cval1
= 0, cval2
= 0;
9498 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
9499 /* Don't handle degenerate cases here; they should already
9500 have been handled anyway. */
9501 && cval1
!= 0 && cval2
!= 0
9502 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
9503 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
9504 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
9505 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
9506 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
9507 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
9508 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
9510 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
9511 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
9513 /* We can't just pass T to eval_subst in case cval1 or cval2
9514 was the same as ARG1. */
9517 = fold_build2_loc (loc
, code
, type
,
9518 eval_subst (loc
, arg0
, cval1
, maxval
,
9522 = fold_build2_loc (loc
, code
, type
,
9523 eval_subst (loc
, arg0
, cval1
, maxval
,
9527 = fold_build2_loc (loc
, code
, type
,
9528 eval_subst (loc
, arg0
, cval1
, minval
,
9532 /* All three of these results should be 0 or 1. Confirm they are.
9533 Then use those values to select the proper code to use. */
9535 if (TREE_CODE (high_result
) == INTEGER_CST
9536 && TREE_CODE (equal_result
) == INTEGER_CST
9537 && TREE_CODE (low_result
) == INTEGER_CST
)
9539 /* Make a 3-bit mask with the high-order bit being the
9540 value for `>', the next for '=', and the low for '<'. */
9541 switch ((integer_onep (high_result
) * 4)
9542 + (integer_onep (equal_result
) * 2)
9543 + integer_onep (low_result
))
9547 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
9568 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
9573 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
9574 SET_EXPR_LOCATION (tem
, loc
);
9577 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
9582 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9583 into a single range test. */
9584 if ((TREE_CODE (arg0
) == TRUNC_DIV_EXPR
9585 || TREE_CODE (arg0
) == EXACT_DIV_EXPR
)
9586 && TREE_CODE (arg1
) == INTEGER_CST
9587 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9588 && !integer_zerop (TREE_OPERAND (arg0
, 1))
9589 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
9590 && !TREE_OVERFLOW (arg1
))
9592 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
9593 if (tem
!= NULL_TREE
)
9597 /* Fold ~X op ~Y as Y op X. */
9598 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9599 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
9601 tree cmp_type
= TREE_TYPE (TREE_OPERAND (arg0
, 0));
9602 return fold_build2_loc (loc
, code
, type
,
9603 fold_convert_loc (loc
, cmp_type
,
9604 TREE_OPERAND (arg1
, 0)),
9605 TREE_OPERAND (arg0
, 0));
9608 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9609 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9610 && (TREE_CODE (arg1
) == INTEGER_CST
|| TREE_CODE (arg1
) == VECTOR_CST
))
9612 tree cmp_type
= TREE_TYPE (TREE_OPERAND (arg0
, 0));
9613 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
,
9614 TREE_OPERAND (arg0
, 0),
9615 fold_build1_loc (loc
, BIT_NOT_EXPR
, cmp_type
,
9616 fold_convert_loc (loc
, cmp_type
, arg1
)));
9623 /* Subroutine of fold_binary. Optimize complex multiplications of the
9624 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9625 argument EXPR represents the expression "z" of type TYPE. */
9628 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
9630 tree itype
= TREE_TYPE (type
);
9631 tree rpart
, ipart
, tem
;
9633 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
9635 rpart
= TREE_OPERAND (expr
, 0);
9636 ipart
= TREE_OPERAND (expr
, 1);
9638 else if (TREE_CODE (expr
) == COMPLEX_CST
)
9640 rpart
= TREE_REALPART (expr
);
9641 ipart
= TREE_IMAGPART (expr
);
9645 expr
= save_expr (expr
);
9646 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
9647 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
9650 rpart
= save_expr (rpart
);
9651 ipart
= save_expr (ipart
);
9652 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
9653 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
9654 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
9655 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
9656 build_zero_cst (itype
));
9660 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9661 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9662 guarantees that P and N have the same least significant log2(M) bits.
9663 N is not otherwise constrained. In particular, N is not normalized to
9664 0 <= N < M as is common. In general, the precise value of P is unknown.
9665 M is chosen as large as possible such that constant N can be determined.
9667 Returns M and sets *RESIDUE to N.
9669 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9670 account. This is not always possible due to PR 35705.
9673 static unsigned HOST_WIDE_INT
9674 get_pointer_modulus_and_residue (tree expr
, unsigned HOST_WIDE_INT
*residue
,
9675 bool allow_func_align
)
9677 enum tree_code code
;
9681 code
= TREE_CODE (expr
);
9682 if (code
== ADDR_EXPR
)
9684 unsigned int bitalign
;
9685 get_object_alignment_1 (TREE_OPERAND (expr
, 0), &bitalign
, residue
);
9686 *residue
/= BITS_PER_UNIT
;
9687 return bitalign
/ BITS_PER_UNIT
;
9689 else if (code
== POINTER_PLUS_EXPR
)
9692 unsigned HOST_WIDE_INT modulus
;
9693 enum tree_code inner_code
;
9695 op0
= TREE_OPERAND (expr
, 0);
9697 modulus
= get_pointer_modulus_and_residue (op0
, residue
,
9700 op1
= TREE_OPERAND (expr
, 1);
9702 inner_code
= TREE_CODE (op1
);
9703 if (inner_code
== INTEGER_CST
)
9705 *residue
+= TREE_INT_CST_LOW (op1
);
9708 else if (inner_code
== MULT_EXPR
)
9710 op1
= TREE_OPERAND (op1
, 1);
9711 if (TREE_CODE (op1
) == INTEGER_CST
)
9713 unsigned HOST_WIDE_INT align
;
9715 /* Compute the greatest power-of-2 divisor of op1. */
9716 align
= TREE_INT_CST_LOW (op1
);
9719 /* If align is non-zero and less than *modulus, replace
9720 *modulus with align., If align is 0, then either op1 is 0
9721 or the greatest power-of-2 divisor of op1 doesn't fit in an
9722 unsigned HOST_WIDE_INT. In either case, no additional
9723 constraint is imposed. */
9725 modulus
= MIN (modulus
, align
);
9732 /* If we get here, we were unable to determine anything useful about the
9737 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9738 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9741 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
9743 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
9745 if (TREE_CODE (arg
) == VECTOR_CST
)
9747 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
9748 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
9750 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
9752 constructor_elt
*elt
;
9754 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
9755 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
9758 elts
[i
] = elt
->value
;
9762 for (; i
< nelts
; i
++)
9764 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
9768 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9769 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9770 NULL_TREE otherwise. */
9773 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
9775 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
9777 bool need_ctor
= false;
9779 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
9780 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
9781 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
9782 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
9785 elts
= XALLOCAVEC (tree
, nelts
* 3);
9786 if (!vec_cst_ctor_to_array (arg0
, elts
)
9787 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
9790 for (i
= 0; i
< nelts
; i
++)
9792 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
9794 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
9799 vec
<constructor_elt
, va_gc
> *v
;
9800 vec_alloc (v
, nelts
);
9801 for (i
= 0; i
< nelts
; i
++)
9802 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
9803 return build_constructor (type
, v
);
9806 return build_vector (type
, &elts
[2 * nelts
]);
9809 /* Try to fold a pointer difference of type TYPE two address expressions of
9810 array references AREF0 and AREF1 using location LOC. Return a
9811 simplified expression for the difference or NULL_TREE. */
9814 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
9815 tree aref0
, tree aref1
)
9817 tree base0
= TREE_OPERAND (aref0
, 0);
9818 tree base1
= TREE_OPERAND (aref1
, 0);
9819 tree base_offset
= build_int_cst (type
, 0);
9821 /* If the bases are array references as well, recurse. If the bases
9822 are pointer indirections compute the difference of the pointers.
9823 If the bases are equal, we are set. */
9824 if ((TREE_CODE (base0
) == ARRAY_REF
9825 && TREE_CODE (base1
) == ARRAY_REF
9827 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
9828 || (INDIRECT_REF_P (base0
)
9829 && INDIRECT_REF_P (base1
)
9830 && (base_offset
= fold_binary_loc (loc
, MINUS_EXPR
, type
,
9831 TREE_OPERAND (base0
, 0),
9832 TREE_OPERAND (base1
, 0))))
9833 || operand_equal_p (base0
, base1
, 0))
9835 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
9836 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
9837 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
9838 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
9839 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9841 fold_build2_loc (loc
, MULT_EXPR
, type
,
9847 /* If the real or vector real constant CST of type TYPE has an exact
9848 inverse, return it, else return NULL. */
9851 exact_inverse (tree type
, tree cst
)
9854 tree unit_type
, *elts
;
9855 enum machine_mode mode
;
9856 unsigned vec_nelts
, i
;
9858 switch (TREE_CODE (cst
))
9861 r
= TREE_REAL_CST (cst
);
9863 if (exact_real_inverse (TYPE_MODE (type
), &r
))
9864 return build_real (type
, r
);
9869 vec_nelts
= VECTOR_CST_NELTS (cst
);
9870 elts
= XALLOCAVEC (tree
, vec_nelts
);
9871 unit_type
= TREE_TYPE (type
);
9872 mode
= TYPE_MODE (unit_type
);
9874 for (i
= 0; i
< vec_nelts
; i
++)
9876 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
9877 if (!exact_real_inverse (mode
, &r
))
9879 elts
[i
] = build_real (unit_type
, r
);
9882 return build_vector (type
, elts
);
9889 /* Mask out the tz least significant bits of X of type TYPE where
9890 tz is the number of trailing zeroes in Y. */
9892 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
9894 int tz
= wi::ctz (y
);
9896 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
9900 /* Return true when T is an address and is known to be nonzero.
9901 For floating point we further ensure that T is not denormal.
9902 Similar logic is present in nonzero_address in rtlanal.h.
9904 If the return value is based on the assumption that signed overflow
9905 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9906 change *STRICT_OVERFLOW_P. */
9909 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
9911 tree type
= TREE_TYPE (t
);
9912 enum tree_code code
;
9914 /* Doing something useful for floating point would need more work. */
9915 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
9918 code
= TREE_CODE (t
);
9919 switch (TREE_CODE_CLASS (code
))
9922 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9925 case tcc_comparison
:
9926 return tree_binary_nonzero_warnv_p (code
, type
,
9927 TREE_OPERAND (t
, 0),
9928 TREE_OPERAND (t
, 1),
9931 case tcc_declaration
:
9933 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9941 case TRUTH_NOT_EXPR
:
9942 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9945 case TRUTH_AND_EXPR
:
9947 case TRUTH_XOR_EXPR
:
9948 return tree_binary_nonzero_warnv_p (code
, type
,
9949 TREE_OPERAND (t
, 0),
9950 TREE_OPERAND (t
, 1),
9958 case WITH_SIZE_EXPR
:
9960 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9965 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9969 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9974 tree fndecl
= get_callee_fndecl (t
);
9975 if (!fndecl
) return false;
9976 if (flag_delete_null_pointer_checks
&& !flag_check_new
9977 && DECL_IS_OPERATOR_NEW (fndecl
)
9978 && !TREE_NOTHROW (fndecl
))
9980 if (flag_delete_null_pointer_checks
9981 && lookup_attribute ("returns_nonnull",
9982 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9984 return alloca_call_p (t
);
9993 /* Return true when T is an address and is known to be nonzero.
9994 Handle warnings about undefined signed overflow. */
9997 tree_expr_nonzero_p (tree t
)
9999 bool ret
, strict_overflow_p
;
10001 strict_overflow_p
= false;
10002 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
10003 if (strict_overflow_p
)
10004 fold_overflow_warning (("assuming signed overflow does not occur when "
10005 "determining that expression is always "
10007 WARN_STRICT_OVERFLOW_MISC
);
10011 /* Fold a binary expression of code CODE and type TYPE with operands
10012 OP0 and OP1. LOC is the location of the resulting expression.
10013 Return the folded expression if folding is successful. Otherwise,
10014 return NULL_TREE. */
10017 fold_binary_loc (location_t loc
,
10018 enum tree_code code
, tree type
, tree op0
, tree op1
)
10020 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
10021 tree arg0
, arg1
, tem
;
10022 tree t1
= NULL_TREE
;
10023 bool strict_overflow_p
;
10026 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
10027 && TREE_CODE_LENGTH (code
) == 2
10028 && op0
!= NULL_TREE
10029 && op1
!= NULL_TREE
);
10034 /* Strip any conversions that don't change the mode. This is
10035 safe for every expression, except for a comparison expression
10036 because its signedness is derived from its operands. So, in
10037 the latter case, only strip conversions that don't change the
10038 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10041 Note that this is done as an internal manipulation within the
10042 constant folder, in order to find the simplest representation
10043 of the arguments so that their form can be studied. In any
10044 cases, the appropriate type conversions should be put back in
10045 the tree that will get out of the constant folder. */
10047 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
10049 STRIP_SIGN_NOPS (arg0
);
10050 STRIP_SIGN_NOPS (arg1
);
10058 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10059 constant but we can't do arithmetic on them. */
10060 if ((TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
10061 || (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
10062 || (TREE_CODE (arg0
) == FIXED_CST
&& TREE_CODE (arg1
) == FIXED_CST
)
10063 || (TREE_CODE (arg0
) == FIXED_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
10064 || (TREE_CODE (arg0
) == COMPLEX_CST
&& TREE_CODE (arg1
) == COMPLEX_CST
)
10065 || (TREE_CODE (arg0
) == VECTOR_CST
&& TREE_CODE (arg1
) == VECTOR_CST
)
10066 || (TREE_CODE (arg0
) == VECTOR_CST
&& TREE_CODE (arg1
) == INTEGER_CST
))
10068 if (kind
== tcc_binary
)
10070 /* Make sure type and arg0 have the same saturating flag. */
10071 gcc_assert (TYPE_SATURATING (type
)
10072 == TYPE_SATURATING (TREE_TYPE (arg0
)));
10073 tem
= const_binop (code
, arg0
, arg1
);
10075 else if (kind
== tcc_comparison
)
10076 tem
= fold_relational_const (code
, type
, arg0
, arg1
);
10080 if (tem
!= NULL_TREE
)
10082 if (TREE_TYPE (tem
) != type
)
10083 tem
= fold_convert_loc (loc
, type
, tem
);
10088 /* If this is a commutative operation, and ARG0 is a constant, move it
10089 to ARG1 to reduce the number of tests below. */
10090 if (commutative_tree_code (code
)
10091 && tree_swap_operands_p (arg0
, arg1
, true))
10092 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
10094 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10096 First check for cases where an arithmetic operation is applied to a
10097 compound, conditional, or comparison operation. Push the arithmetic
10098 operation inside the compound or conditional to see if any folding
10099 can then be done. Convert comparison to conditional for this purpose.
10100 The also optimizes non-constant cases that used to be done in
10103 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10104 one of the operands is a comparison and the other is a comparison, a
10105 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10106 code below would make the expression more complex. Change it to a
10107 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10108 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10110 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
10111 || code
== EQ_EXPR
|| code
== NE_EXPR
)
10112 && TREE_CODE (type
) != VECTOR_TYPE
10113 && ((truth_value_p (TREE_CODE (arg0
))
10114 && (truth_value_p (TREE_CODE (arg1
))
10115 || (TREE_CODE (arg1
) == BIT_AND_EXPR
10116 && integer_onep (TREE_OPERAND (arg1
, 1)))))
10117 || (truth_value_p (TREE_CODE (arg1
))
10118 && (truth_value_p (TREE_CODE (arg0
))
10119 || (TREE_CODE (arg0
) == BIT_AND_EXPR
10120 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
10122 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
10123 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
10126 fold_convert_loc (loc
, boolean_type_node
, arg0
),
10127 fold_convert_loc (loc
, boolean_type_node
, arg1
));
10129 if (code
== EQ_EXPR
)
10130 tem
= invert_truthvalue_loc (loc
, tem
);
10132 return fold_convert_loc (loc
, type
, tem
);
10135 if (TREE_CODE_CLASS (code
) == tcc_binary
10136 || TREE_CODE_CLASS (code
) == tcc_comparison
)
10138 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
10140 tem
= fold_build2_loc (loc
, code
, type
,
10141 fold_convert_loc (loc
, TREE_TYPE (op0
),
10142 TREE_OPERAND (arg0
, 1)), op1
);
10143 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10146 if (TREE_CODE (arg1
) == COMPOUND_EXPR
10147 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
10149 tem
= fold_build2_loc (loc
, code
, type
, op0
,
10150 fold_convert_loc (loc
, TREE_TYPE (op1
),
10151 TREE_OPERAND (arg1
, 1)));
10152 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
10156 if (TREE_CODE (arg0
) == COND_EXPR
10157 || TREE_CODE (arg0
) == VEC_COND_EXPR
10158 || COMPARISON_CLASS_P (arg0
))
10160 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10162 /*cond_first_p=*/1);
10163 if (tem
!= NULL_TREE
)
10167 if (TREE_CODE (arg1
) == COND_EXPR
10168 || TREE_CODE (arg1
) == VEC_COND_EXPR
10169 || COMPARISON_CLASS_P (arg1
))
10171 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
10173 /*cond_first_p=*/0);
10174 if (tem
!= NULL_TREE
)
10182 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10183 if (TREE_CODE (arg0
) == ADDR_EXPR
10184 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
10186 tree iref
= TREE_OPERAND (arg0
, 0);
10187 return fold_build2 (MEM_REF
, type
,
10188 TREE_OPERAND (iref
, 0),
10189 int_const_binop (PLUS_EXPR
, arg1
,
10190 TREE_OPERAND (iref
, 1)));
10193 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10194 if (TREE_CODE (arg0
) == ADDR_EXPR
10195 && handled_component_p (TREE_OPERAND (arg0
, 0)))
10198 HOST_WIDE_INT coffset
;
10199 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
10203 return fold_build2 (MEM_REF
, type
,
10204 build_fold_addr_expr (base
),
10205 int_const_binop (PLUS_EXPR
, arg1
,
10206 size_int (coffset
)));
10211 case POINTER_PLUS_EXPR
:
10212 /* 0 +p index -> (type)index */
10213 if (integer_zerop (arg0
))
10214 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10216 /* PTR +p 0 -> PTR */
10217 if (integer_zerop (arg1
))
10218 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10220 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10221 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10222 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
10223 return fold_convert_loc (loc
, type
,
10224 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
10225 fold_convert_loc (loc
, sizetype
,
10227 fold_convert_loc (loc
, sizetype
,
10230 /* (PTR +p B) +p A -> PTR +p (B + A) */
10231 if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10234 tree arg01
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (arg0
, 1));
10235 tree arg00
= TREE_OPERAND (arg0
, 0);
10236 inner
= fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
10237 arg01
, fold_convert_loc (loc
, sizetype
, arg1
));
10238 return fold_convert_loc (loc
, type
,
10239 fold_build_pointer_plus_loc (loc
,
10243 /* PTR_CST +p CST -> CST1 */
10244 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
10245 return fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
,
10246 fold_convert_loc (loc
, type
, arg1
));
10248 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10249 of the array. Loop optimizer sometimes produce this type of
10251 if (TREE_CODE (arg0
) == ADDR_EXPR
)
10253 tem
= try_move_mult_to_index (loc
, arg0
,
10254 fold_convert_loc (loc
,
10257 return fold_convert_loc (loc
, type
, tem
);
10263 /* A + (-B) -> A - B */
10264 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
10265 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
10266 fold_convert_loc (loc
, type
, arg0
),
10267 fold_convert_loc (loc
, type
,
10268 TREE_OPERAND (arg1
, 0)));
10269 /* (-A) + B -> B - A */
10270 if (TREE_CODE (arg0
) == NEGATE_EXPR
10271 && reorder_operands_p (TREE_OPERAND (arg0
, 0), arg1
))
10272 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
10273 fold_convert_loc (loc
, type
, arg1
),
10274 fold_convert_loc (loc
, type
,
10275 TREE_OPERAND (arg0
, 0)));
10277 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
10279 /* Convert ~A + 1 to -A. */
10280 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
10281 && integer_onep (arg1
))
10282 return fold_build1_loc (loc
, NEGATE_EXPR
, type
,
10283 fold_convert_loc (loc
, type
,
10284 TREE_OPERAND (arg0
, 0)));
10286 /* ~X + X is -1. */
10287 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
10288 && !TYPE_OVERFLOW_TRAPS (type
))
10290 tree tem
= TREE_OPERAND (arg0
, 0);
10293 if (operand_equal_p (tem
, arg1
, 0))
10295 t1
= build_all_ones_cst (type
);
10296 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
10300 /* X + ~X is -1. */
10301 if (TREE_CODE (arg1
) == BIT_NOT_EXPR
10302 && !TYPE_OVERFLOW_TRAPS (type
))
10304 tree tem
= TREE_OPERAND (arg1
, 0);
10307 if (operand_equal_p (arg0
, tem
, 0))
10309 t1
= build_all_ones_cst (type
);
10310 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
10314 /* X + (X / CST) * -CST is X % CST. */
10315 if (TREE_CODE (arg1
) == MULT_EXPR
10316 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
10317 && operand_equal_p (arg0
,
10318 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
10320 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
10321 tree cst1
= TREE_OPERAND (arg1
, 1);
10322 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
10324 if (sum
&& integer_zerop (sum
))
10325 return fold_convert_loc (loc
, type
,
10326 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
10327 TREE_TYPE (arg0
), arg0
,
10332 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10333 one. Make sure the type is not saturating and has the signedness of
10334 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10335 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10336 if ((TREE_CODE (arg0
) == MULT_EXPR
10337 || TREE_CODE (arg1
) == MULT_EXPR
)
10338 && !TYPE_SATURATING (type
)
10339 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
10340 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
10341 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
10343 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
10348 if (! FLOAT_TYPE_P (type
))
10350 if (integer_zerop (arg1
))
10351 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10353 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10354 with a constant, and the two constants have no bits in common,
10355 we should treat this as a BIT_IOR_EXPR since this may produce more
10356 simplifications. */
10357 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10358 && TREE_CODE (arg1
) == BIT_AND_EXPR
10359 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10360 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10361 && integer_zerop (const_binop (BIT_AND_EXPR
,
10362 TREE_OPERAND (arg0
, 1),
10363 TREE_OPERAND (arg1
, 1))))
10365 code
= BIT_IOR_EXPR
;
10369 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10370 (plus (plus (mult) (mult)) (foo)) so that we can
10371 take advantage of the factoring cases below. */
10372 if (TYPE_OVERFLOW_WRAPS (type
)
10373 && (((TREE_CODE (arg0
) == PLUS_EXPR
10374 || TREE_CODE (arg0
) == MINUS_EXPR
)
10375 && TREE_CODE (arg1
) == MULT_EXPR
)
10376 || ((TREE_CODE (arg1
) == PLUS_EXPR
10377 || TREE_CODE (arg1
) == MINUS_EXPR
)
10378 && TREE_CODE (arg0
) == MULT_EXPR
)))
10380 tree parg0
, parg1
, parg
, marg
;
10381 enum tree_code pcode
;
10383 if (TREE_CODE (arg1
) == MULT_EXPR
)
10384 parg
= arg0
, marg
= arg1
;
10386 parg
= arg1
, marg
= arg0
;
10387 pcode
= TREE_CODE (parg
);
10388 parg0
= TREE_OPERAND (parg
, 0);
10389 parg1
= TREE_OPERAND (parg
, 1);
10390 STRIP_NOPS (parg0
);
10391 STRIP_NOPS (parg1
);
10393 if (TREE_CODE (parg0
) == MULT_EXPR
10394 && TREE_CODE (parg1
) != MULT_EXPR
)
10395 return fold_build2_loc (loc
, pcode
, type
,
10396 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10397 fold_convert_loc (loc
, type
,
10399 fold_convert_loc (loc
, type
,
10401 fold_convert_loc (loc
, type
, parg1
));
10402 if (TREE_CODE (parg0
) != MULT_EXPR
10403 && TREE_CODE (parg1
) == MULT_EXPR
)
10405 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10406 fold_convert_loc (loc
, type
, parg0
),
10407 fold_build2_loc (loc
, pcode
, type
,
10408 fold_convert_loc (loc
, type
, marg
),
10409 fold_convert_loc (loc
, type
,
10415 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10416 if (fold_real_zero_addition_p (TREE_TYPE (arg0
), arg1
, 0))
10417 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10419 /* Likewise if the operands are reversed. */
10420 if (fold_real_zero_addition_p (TREE_TYPE (arg1
), arg0
, 0))
10421 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10423 /* Convert X + -C into X - C. */
10424 if (TREE_CODE (arg1
) == REAL_CST
10425 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
)))
10427 tem
= fold_negate_const (arg1
, type
);
10428 if (!TREE_OVERFLOW (arg1
) || !flag_trapping_math
)
10429 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
10430 fold_convert_loc (loc
, type
, arg0
),
10431 fold_convert_loc (loc
, type
, tem
));
10434 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10435 to __complex__ ( x, y ). This is not the same for SNaNs or
10436 if signed zeros are involved. */
10437 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
10438 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
10439 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
10441 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10442 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
10443 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
10444 bool arg0rz
= false, arg0iz
= false;
10445 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
10446 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
10448 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
10449 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
10450 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
10452 tree rp
= arg1r
? arg1r
10453 : build1 (REALPART_EXPR
, rtype
, arg1
);
10454 tree ip
= arg0i
? arg0i
10455 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
10456 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10458 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
10460 tree rp
= arg0r
? arg0r
10461 : build1 (REALPART_EXPR
, rtype
, arg0
);
10462 tree ip
= arg1i
? arg1i
10463 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
10464 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10469 if (flag_unsafe_math_optimizations
10470 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
10471 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
10472 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
10475 /* Convert x+x into x*2.0. */
10476 if (operand_equal_p (arg0
, arg1
, 0)
10477 && SCALAR_FLOAT_TYPE_P (type
))
10478 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
,
10479 build_real (type
, dconst2
));
10481 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10482 We associate floats only if the user has specified
10483 -fassociative-math. */
10484 if (flag_associative_math
10485 && TREE_CODE (arg1
) == PLUS_EXPR
10486 && TREE_CODE (arg0
) != MULT_EXPR
)
10488 tree tree10
= TREE_OPERAND (arg1
, 0);
10489 tree tree11
= TREE_OPERAND (arg1
, 1);
10490 if (TREE_CODE (tree11
) == MULT_EXPR
10491 && TREE_CODE (tree10
) == MULT_EXPR
)
10494 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
10495 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
10498 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10499 We associate floats only if the user has specified
10500 -fassociative-math. */
10501 if (flag_associative_math
10502 && TREE_CODE (arg0
) == PLUS_EXPR
10503 && TREE_CODE (arg1
) != MULT_EXPR
)
10505 tree tree00
= TREE_OPERAND (arg0
, 0);
10506 tree tree01
= TREE_OPERAND (arg0
, 1);
10507 if (TREE_CODE (tree01
) == MULT_EXPR
10508 && TREE_CODE (tree00
) == MULT_EXPR
)
10511 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
10512 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
10518 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10519 is a rotate of A by C1 bits. */
10520 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10521 is a rotate of A by B bits. */
10523 enum tree_code code0
, code1
;
10525 code0
= TREE_CODE (arg0
);
10526 code1
= TREE_CODE (arg1
);
10527 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
10528 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
10529 && operand_equal_p (TREE_OPERAND (arg0
, 0),
10530 TREE_OPERAND (arg1
, 0), 0)
10531 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10532 TYPE_UNSIGNED (rtype
))
10533 /* Only create rotates in complete modes. Other cases are not
10534 expanded properly. */
10535 && (element_precision (rtype
)
10536 == element_precision (TYPE_MODE (rtype
))))
10538 tree tree01
, tree11
;
10539 enum tree_code code01
, code11
;
10541 tree01
= TREE_OPERAND (arg0
, 1);
10542 tree11
= TREE_OPERAND (arg1
, 1);
10543 STRIP_NOPS (tree01
);
10544 STRIP_NOPS (tree11
);
10545 code01
= TREE_CODE (tree01
);
10546 code11
= TREE_CODE (tree11
);
10547 if (code01
== INTEGER_CST
10548 && code11
== INTEGER_CST
10549 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
10550 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
10552 tem
= build2_loc (loc
, LROTATE_EXPR
,
10553 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10554 TREE_OPERAND (arg0
, 0),
10555 code0
== LSHIFT_EXPR
? tree01
: tree11
);
10556 return fold_convert_loc (loc
, type
, tem
);
10558 else if (code11
== MINUS_EXPR
)
10560 tree tree110
, tree111
;
10561 tree110
= TREE_OPERAND (tree11
, 0);
10562 tree111
= TREE_OPERAND (tree11
, 1);
10563 STRIP_NOPS (tree110
);
10564 STRIP_NOPS (tree111
);
10565 if (TREE_CODE (tree110
) == INTEGER_CST
10566 && 0 == compare_tree_int (tree110
,
10568 (TREE_TYPE (TREE_OPERAND
10570 && operand_equal_p (tree01
, tree111
, 0))
10572 fold_convert_loc (loc
, type
,
10573 build2 ((code0
== LSHIFT_EXPR
10576 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10577 TREE_OPERAND (arg0
, 0), tree01
));
10579 else if (code01
== MINUS_EXPR
)
10581 tree tree010
, tree011
;
10582 tree010
= TREE_OPERAND (tree01
, 0);
10583 tree011
= TREE_OPERAND (tree01
, 1);
10584 STRIP_NOPS (tree010
);
10585 STRIP_NOPS (tree011
);
10586 if (TREE_CODE (tree010
) == INTEGER_CST
10587 && 0 == compare_tree_int (tree010
,
10589 (TREE_TYPE (TREE_OPERAND
10591 && operand_equal_p (tree11
, tree011
, 0))
10592 return fold_convert_loc
10594 build2 ((code0
!= LSHIFT_EXPR
10597 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10598 TREE_OPERAND (arg0
, 0), tree11
));
10604 /* In most languages, can't associate operations on floats through
10605 parentheses. Rather than remember where the parentheses were, we
10606 don't associate floats at all, unless the user has specified
10607 -fassociative-math.
10608 And, we need to make sure type is not saturating. */
10610 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
10611 && !TYPE_SATURATING (type
))
10613 tree var0
, con0
, lit0
, minus_lit0
;
10614 tree var1
, con1
, lit1
, minus_lit1
;
10618 /* Split both trees into variables, constants, and literals. Then
10619 associate each group together, the constants with literals,
10620 then the result with variables. This increases the chances of
10621 literals being recombined later and of generating relocatable
10622 expressions for the sum of a constant and literal. */
10623 var0
= split_tree (arg0
, code
, &con0
, &lit0
, &minus_lit0
, 0);
10624 var1
= split_tree (arg1
, code
, &con1
, &lit1
, &minus_lit1
,
10625 code
== MINUS_EXPR
);
10627 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10628 if (code
== MINUS_EXPR
)
10631 /* With undefined overflow prefer doing association in a type
10632 which wraps on overflow, if that is one of the operand types. */
10633 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
10634 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
10636 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10637 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10638 atype
= TREE_TYPE (arg0
);
10639 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10640 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
10641 atype
= TREE_TYPE (arg1
);
10642 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
10645 /* With undefined overflow we can only associate constants with one
10646 variable, and constants whose association doesn't overflow. */
10647 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
10648 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
10655 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
10656 tmp0
= TREE_OPERAND (tmp0
, 0);
10657 if (CONVERT_EXPR_P (tmp0
)
10658 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
10659 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
10660 <= TYPE_PRECISION (atype
)))
10661 tmp0
= TREE_OPERAND (tmp0
, 0);
10662 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
10663 tmp1
= TREE_OPERAND (tmp1
, 0);
10664 if (CONVERT_EXPR_P (tmp1
)
10665 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
10666 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
10667 <= TYPE_PRECISION (atype
)))
10668 tmp1
= TREE_OPERAND (tmp1
, 0);
10669 /* The only case we can still associate with two variables
10670 is if they are the same, modulo negation and bit-pattern
10671 preserving conversions. */
10672 if (!operand_equal_p (tmp0
, tmp1
, 0))
10677 /* Only do something if we found more than two objects. Otherwise,
10678 nothing has changed and we risk infinite recursion. */
10680 && (2 < ((var0
!= 0) + (var1
!= 0)
10681 + (con0
!= 0) + (con1
!= 0)
10682 + (lit0
!= 0) + (lit1
!= 0)
10683 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
10685 bool any_overflows
= false;
10686 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
10687 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
10688 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
10689 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
10690 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
10691 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
10692 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
10693 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
10696 /* Preserve the MINUS_EXPR if the negative part of the literal is
10697 greater than the positive part. Otherwise, the multiplicative
10698 folding code (i.e extract_muldiv) may be fooled in case
10699 unsigned constants are subtracted, like in the following
10700 example: ((X*2 + 4) - 8U)/2. */
10701 if (minus_lit0
&& lit0
)
10703 if (TREE_CODE (lit0
) == INTEGER_CST
10704 && TREE_CODE (minus_lit0
) == INTEGER_CST
10705 && tree_int_cst_lt (lit0
, minus_lit0
))
10707 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
10708 MINUS_EXPR
, atype
);
10713 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
10714 MINUS_EXPR
, atype
);
10719 /* Don't introduce overflows through reassociation. */
10721 && ((lit0
&& TREE_OVERFLOW (lit0
))
10722 || (minus_lit0
&& TREE_OVERFLOW (minus_lit0
))))
10729 fold_convert_loc (loc
, type
,
10730 associate_trees (loc
, var0
, minus_lit0
,
10731 MINUS_EXPR
, atype
));
10734 con0
= associate_trees (loc
, con0
, minus_lit0
,
10735 MINUS_EXPR
, atype
);
10737 fold_convert_loc (loc
, type
,
10738 associate_trees (loc
, var0
, con0
,
10739 PLUS_EXPR
, atype
));
10743 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
10745 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
10753 /* Pointer simplifications for subtraction, simple reassociations. */
10754 if (POINTER_TYPE_P (TREE_TYPE (arg1
)) && POINTER_TYPE_P (TREE_TYPE (arg0
)))
10756 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10757 if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10758 && TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10760 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10761 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10762 tree arg10
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10763 tree arg11
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10764 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10765 fold_build2_loc (loc
, MINUS_EXPR
, type
,
10767 fold_build2_loc (loc
, MINUS_EXPR
, type
,
10770 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10771 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10773 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10774 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10775 tree tmp
= fold_binary_loc (loc
, MINUS_EXPR
, type
, arg00
,
10776 fold_convert_loc (loc
, type
, arg1
));
10778 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tmp
, arg01
);
10781 /* A - (-B) -> A + B */
10782 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
10783 return fold_build2_loc (loc
, PLUS_EXPR
, type
, op0
,
10784 fold_convert_loc (loc
, type
,
10785 TREE_OPERAND (arg1
, 0)));
10786 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10787 if (TREE_CODE (arg0
) == NEGATE_EXPR
10788 && negate_expr_p (arg1
)
10789 && reorder_operands_p (arg0
, arg1
))
10790 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
10791 fold_convert_loc (loc
, type
,
10792 negate_expr (arg1
)),
10793 fold_convert_loc (loc
, type
,
10794 TREE_OPERAND (arg0
, 0)));
10795 /* Convert -A - 1 to ~A. */
10796 if (TREE_CODE (type
) != COMPLEX_TYPE
10797 && TREE_CODE (arg0
) == NEGATE_EXPR
10798 && integer_onep (arg1
)
10799 && !TYPE_OVERFLOW_TRAPS (type
))
10800 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
10801 fold_convert_loc (loc
, type
,
10802 TREE_OPERAND (arg0
, 0)));
10804 /* Convert -1 - A to ~A. */
10805 if (TREE_CODE (type
) != COMPLEX_TYPE
10806 && integer_all_onesp (arg0
))
10807 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, op1
);
10810 /* X - (X / Y) * Y is X % Y. */
10811 if ((INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
10812 && TREE_CODE (arg1
) == MULT_EXPR
10813 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
10814 && operand_equal_p (arg0
,
10815 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0)
10816 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1),
10817 TREE_OPERAND (arg1
, 1), 0))
10819 fold_convert_loc (loc
, type
,
10820 fold_build2_loc (loc
, TRUNC_MOD_EXPR
, TREE_TYPE (arg0
),
10821 arg0
, TREE_OPERAND (arg1
, 1)));
10823 if (! FLOAT_TYPE_P (type
))
10825 if (integer_zerop (arg0
))
10826 return negate_expr (fold_convert_loc (loc
, type
, arg1
));
10827 if (integer_zerop (arg1
))
10828 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10830 /* Fold A - (A & B) into ~B & A. */
10831 if (!TREE_SIDE_EFFECTS (arg0
)
10832 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10834 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0))
10836 tree arg10
= fold_convert_loc (loc
, type
,
10837 TREE_OPERAND (arg1
, 0));
10838 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10839 fold_build1_loc (loc
, BIT_NOT_EXPR
,
10841 fold_convert_loc (loc
, type
, arg0
));
10843 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10845 tree arg11
= fold_convert_loc (loc
,
10846 type
, TREE_OPERAND (arg1
, 1));
10847 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10848 fold_build1_loc (loc
, BIT_NOT_EXPR
,
10850 fold_convert_loc (loc
, type
, arg0
));
10854 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10855 any power of 2 minus 1. */
10856 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10857 && TREE_CODE (arg1
) == BIT_AND_EXPR
10858 && operand_equal_p (TREE_OPERAND (arg0
, 0),
10859 TREE_OPERAND (arg1
, 0), 0))
10861 tree mask0
= TREE_OPERAND (arg0
, 1);
10862 tree mask1
= TREE_OPERAND (arg1
, 1);
10863 tree tem
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, mask0
);
10865 if (operand_equal_p (tem
, mask1
, 0))
10867 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
10868 TREE_OPERAND (arg0
, 0), mask1
);
10869 return fold_build2_loc (loc
, MINUS_EXPR
, type
, tem
, mask1
);
10874 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10875 else if (fold_real_zero_addition_p (TREE_TYPE (arg0
), arg1
, 1))
10876 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10878 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10879 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10880 (-ARG1 + ARG0) reduces to -ARG1. */
10881 else if (fold_real_zero_addition_p (TREE_TYPE (arg1
), arg0
, 0))
10882 return negate_expr (fold_convert_loc (loc
, type
, arg1
));
10884 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10885 __complex__ ( x, -y ). This is not the same for SNaNs or if
10886 signed zeros are involved. */
10887 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
10888 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
10889 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
10891 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10892 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
10893 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
10894 bool arg0rz
= false, arg0iz
= false;
10895 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
10896 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
10898 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
10899 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
10900 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
10902 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
10904 : build1 (REALPART_EXPR
, rtype
, arg1
));
10905 tree ip
= arg0i
? arg0i
10906 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
10907 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10909 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
10911 tree rp
= arg0r
? arg0r
10912 : build1 (REALPART_EXPR
, rtype
, arg0
);
10913 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
10915 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
10916 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10921 /* Fold &x - &x. This can happen from &x.foo - &x.
10922 This is unsafe for certain floats even in non-IEEE formats.
10923 In IEEE, it is unsafe because it does wrong for NaNs.
10924 Also note that operand_equal_p is always false if an operand
10927 if ((!FLOAT_TYPE_P (type
) || !HONOR_NANS (TYPE_MODE (type
)))
10928 && operand_equal_p (arg0
, arg1
, 0))
10929 return build_zero_cst (type
);
10931 /* A - B -> A + (-B) if B is easily negatable. */
10932 if (negate_expr_p (arg1
)
10933 && ((FLOAT_TYPE_P (type
)
10934 /* Avoid this transformation if B is a positive REAL_CST. */
10935 && (TREE_CODE (arg1
) != REAL_CST
10936 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
))))
10937 || INTEGRAL_TYPE_P (type
)))
10938 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10939 fold_convert_loc (loc
, type
, arg0
),
10940 fold_convert_loc (loc
, type
,
10941 negate_expr (arg1
)));
10943 /* Try folding difference of addresses. */
10945 HOST_WIDE_INT diff
;
10947 if ((TREE_CODE (arg0
) == ADDR_EXPR
10948 || TREE_CODE (arg1
) == ADDR_EXPR
)
10949 && ptr_difference_const (arg0
, arg1
, &diff
))
10950 return build_int_cst_type (type
, diff
);
10953 /* Fold &a[i] - &a[j] to i-j. */
10954 if (TREE_CODE (arg0
) == ADDR_EXPR
10955 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
10956 && TREE_CODE (arg1
) == ADDR_EXPR
10957 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
10959 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
10960 TREE_OPERAND (arg0
, 0),
10961 TREE_OPERAND (arg1
, 0));
10966 if (FLOAT_TYPE_P (type
)
10967 && flag_unsafe_math_optimizations
10968 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
10969 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
10970 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
10973 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10974 one. Make sure the type is not saturating and has the signedness of
10975 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10976 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10977 if ((TREE_CODE (arg0
) == MULT_EXPR
10978 || TREE_CODE (arg1
) == MULT_EXPR
)
10979 && !TYPE_SATURATING (type
)
10980 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
10981 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
10982 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
10984 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
10992 /* (-A) * (-B) -> A * B */
10993 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10994 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10995 fold_convert_loc (loc
, type
,
10996 TREE_OPERAND (arg0
, 0)),
10997 fold_convert_loc (loc
, type
,
10998 negate_expr (arg1
)));
10999 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11000 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11001 fold_convert_loc (loc
, type
,
11002 negate_expr (arg0
)),
11003 fold_convert_loc (loc
, type
,
11004 TREE_OPERAND (arg1
, 0)));
11006 if (! FLOAT_TYPE_P (type
))
11008 if (integer_zerop (arg1
))
11009 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11010 if (integer_onep (arg1
))
11011 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11012 /* Transform x * -1 into -x. Make sure to do the negation
11013 on the original operand with conversions not stripped
11014 because we can only strip non-sign-changing conversions. */
11015 if (integer_minus_onep (arg1
))
11016 return fold_convert_loc (loc
, type
, negate_expr (op0
));
11017 /* Transform x * -C into -x * C if x is easily negatable. */
11018 if (TREE_CODE (arg1
) == INTEGER_CST
11019 && tree_int_cst_sgn (arg1
) == -1
11020 && negate_expr_p (arg0
)
11021 && (tem
= negate_expr (arg1
)) != arg1
11022 && !TREE_OVERFLOW (tem
))
11023 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11024 fold_convert_loc (loc
, type
,
11025 negate_expr (arg0
)),
11028 /* (a * (1 << b)) is (a << b) */
11029 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11030 && integer_onep (TREE_OPERAND (arg1
, 0)))
11031 return fold_build2_loc (loc
, LSHIFT_EXPR
, type
, op0
,
11032 TREE_OPERAND (arg1
, 1));
11033 if (TREE_CODE (arg0
) == LSHIFT_EXPR
11034 && integer_onep (TREE_OPERAND (arg0
, 0)))
11035 return fold_build2_loc (loc
, LSHIFT_EXPR
, type
, op1
,
11036 TREE_OPERAND (arg0
, 1));
11038 /* (A + A) * C -> A * 2 * C */
11039 if (TREE_CODE (arg0
) == PLUS_EXPR
11040 && TREE_CODE (arg1
) == INTEGER_CST
11041 && operand_equal_p (TREE_OPERAND (arg0
, 0),
11042 TREE_OPERAND (arg0
, 1), 0))
11043 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11044 omit_one_operand_loc (loc
, type
,
11045 TREE_OPERAND (arg0
, 0),
11046 TREE_OPERAND (arg0
, 1)),
11047 fold_build2_loc (loc
, MULT_EXPR
, type
,
11048 build_int_cst (type
, 2) , arg1
));
11050 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
11051 sign-changing only. */
11052 if (TREE_CODE (arg1
) == INTEGER_CST
11053 && TREE_CODE (arg0
) == EXACT_DIV_EXPR
11054 && operand_equal_p (arg1
, TREE_OPERAND (arg0
, 1), 0))
11055 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11057 strict_overflow_p
= false;
11058 if (TREE_CODE (arg1
) == INTEGER_CST
11059 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11060 &strict_overflow_p
)))
11062 if (strict_overflow_p
)
11063 fold_overflow_warning (("assuming signed overflow does not "
11064 "occur when simplifying "
11066 WARN_STRICT_OVERFLOW_MISC
);
11067 return fold_convert_loc (loc
, type
, tem
);
11070 /* Optimize z * conj(z) for integer complex numbers. */
11071 if (TREE_CODE (arg0
) == CONJ_EXPR
11072 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11073 return fold_mult_zconjz (loc
, type
, arg1
);
11074 if (TREE_CODE (arg1
) == CONJ_EXPR
11075 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11076 return fold_mult_zconjz (loc
, type
, arg0
);
11080 /* Maybe fold x * 0 to 0. The expressions aren't the same
11081 when x is NaN, since x * 0 is also NaN. Nor are they the
11082 same in modes with signed zeros, since multiplying a
11083 negative value by 0 gives -0, not +0. */
11084 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
)))
11085 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
11086 && real_zerop (arg1
))
11087 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11088 /* In IEEE floating point, x*1 is not equivalent to x for snans.
11089 Likewise for complex arithmetic with signed zeros. */
11090 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
11091 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
11092 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11093 && real_onep (arg1
))
11094 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11096 /* Transform x * -1.0 into -x. */
11097 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
11098 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
11099 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
11100 && real_minus_onep (arg1
))
11101 return fold_convert_loc (loc
, type
, negate_expr (arg0
));
11103 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
11104 the result for floating point types due to rounding so it is applied
11105 only if -fassociative-math was specify. */
11106 if (flag_associative_math
11107 && TREE_CODE (arg0
) == RDIV_EXPR
11108 && TREE_CODE (arg1
) == REAL_CST
11109 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
)
11111 tree tem
= const_binop (MULT_EXPR
, TREE_OPERAND (arg0
, 0),
11114 return fold_build2_loc (loc
, RDIV_EXPR
, type
, tem
,
11115 TREE_OPERAND (arg0
, 1));
11118 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
11119 if (operand_equal_p (arg0
, arg1
, 0))
11121 tree tem
= fold_strip_sign_ops (arg0
);
11122 if (tem
!= NULL_TREE
)
11124 tem
= fold_convert_loc (loc
, type
, tem
);
11125 return fold_build2_loc (loc
, MULT_EXPR
, type
, tem
, tem
);
11129 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11130 This is not the same for NaNs or if signed zeros are
11132 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
)))
11133 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0
)))
11134 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
11135 && TREE_CODE (arg1
) == COMPLEX_CST
11136 && real_zerop (TREE_REALPART (arg1
)))
11138 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
11139 if (real_onep (TREE_IMAGPART (arg1
)))
11141 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11142 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
11144 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
11145 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
11147 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
11148 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
11149 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
11153 /* Optimize z * conj(z) for floating point complex numbers.
11154 Guarded by flag_unsafe_math_optimizations as non-finite
11155 imaginary components don't produce scalar results. */
11156 if (flag_unsafe_math_optimizations
11157 && TREE_CODE (arg0
) == CONJ_EXPR
11158 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11159 return fold_mult_zconjz (loc
, type
, arg1
);
11160 if (flag_unsafe_math_optimizations
11161 && TREE_CODE (arg1
) == CONJ_EXPR
11162 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11163 return fold_mult_zconjz (loc
, type
, arg0
);
11165 if (flag_unsafe_math_optimizations
)
11167 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
11168 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
11170 /* Optimizations of root(...)*root(...). */
11171 if (fcode0
== fcode1
&& BUILTIN_ROOT_P (fcode0
))
11174 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11175 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
11177 /* Optimize sqrt(x)*sqrt(x) as x. */
11178 if (BUILTIN_SQRT_P (fcode0
)
11179 && operand_equal_p (arg00
, arg10
, 0)
11180 && ! HONOR_SNANS (TYPE_MODE (type
)))
11183 /* Optimize root(x)*root(y) as root(x*y). */
11184 rootfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11185 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg00
, arg10
);
11186 return build_call_expr_loc (loc
, rootfn
, 1, arg
);
11189 /* Optimize expN(x)*expN(y) as expN(x+y). */
11190 if (fcode0
== fcode1
&& BUILTIN_EXPONENT_P (fcode0
))
11192 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11193 tree arg
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
11194 CALL_EXPR_ARG (arg0
, 0),
11195 CALL_EXPR_ARG (arg1
, 0));
11196 return build_call_expr_loc (loc
, expfn
, 1, arg
);
11199 /* Optimizations of pow(...)*pow(...). */
11200 if ((fcode0
== BUILT_IN_POW
&& fcode1
== BUILT_IN_POW
)
11201 || (fcode0
== BUILT_IN_POWF
&& fcode1
== BUILT_IN_POWF
)
11202 || (fcode0
== BUILT_IN_POWL
&& fcode1
== BUILT_IN_POWL
))
11204 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11205 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
11206 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
11207 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
11209 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
11210 if (operand_equal_p (arg01
, arg11
, 0))
11212 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11213 tree arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
11215 return build_call_expr_loc (loc
, powfn
, 2, arg
, arg01
);
11218 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
11219 if (operand_equal_p (arg00
, arg10
, 0))
11221 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11222 tree arg
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
11224 return build_call_expr_loc (loc
, powfn
, 2, arg00
, arg
);
11228 /* Optimize tan(x)*cos(x) as sin(x). */
11229 if (((fcode0
== BUILT_IN_TAN
&& fcode1
== BUILT_IN_COS
)
11230 || (fcode0
== BUILT_IN_TANF
&& fcode1
== BUILT_IN_COSF
)
11231 || (fcode0
== BUILT_IN_TANL
&& fcode1
== BUILT_IN_COSL
)
11232 || (fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_TAN
)
11233 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_TANF
)
11234 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_TANL
))
11235 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
11236 CALL_EXPR_ARG (arg1
, 0), 0))
11238 tree sinfn
= mathfn_built_in (type
, BUILT_IN_SIN
);
11240 if (sinfn
!= NULL_TREE
)
11241 return build_call_expr_loc (loc
, sinfn
, 1,
11242 CALL_EXPR_ARG (arg0
, 0));
11245 /* Optimize x*pow(x,c) as pow(x,c+1). */
11246 if (fcode1
== BUILT_IN_POW
11247 || fcode1
== BUILT_IN_POWF
11248 || fcode1
== BUILT_IN_POWL
)
11250 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
11251 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
11252 if (TREE_CODE (arg11
) == REAL_CST
11253 && !TREE_OVERFLOW (arg11
)
11254 && operand_equal_p (arg0
, arg10
, 0))
11256 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
11260 c
= TREE_REAL_CST (arg11
);
11261 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
11262 arg
= build_real (type
, c
);
11263 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
11267 /* Optimize pow(x,c)*x as pow(x,c+1). */
11268 if (fcode0
== BUILT_IN_POW
11269 || fcode0
== BUILT_IN_POWF
11270 || fcode0
== BUILT_IN_POWL
)
11272 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11273 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
11274 if (TREE_CODE (arg01
) == REAL_CST
11275 && !TREE_OVERFLOW (arg01
)
11276 && operand_equal_p (arg1
, arg00
, 0))
11278 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11282 c
= TREE_REAL_CST (arg01
);
11283 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
11284 arg
= build_real (type
, c
);
11285 return build_call_expr_loc (loc
, powfn
, 2, arg1
, arg
);
11289 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
11290 if (!in_gimple_form
11292 && operand_equal_p (arg0
, arg1
, 0))
11294 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
11298 tree arg
= build_real (type
, dconst2
);
11299 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
11308 if (integer_all_onesp (arg1
))
11309 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11310 if (integer_zerop (arg1
))
11311 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11312 if (operand_equal_p (arg0
, arg1
, 0))
11313 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11315 /* ~X | X is -1. */
11316 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11317 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11319 t1
= build_zero_cst (type
);
11320 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11321 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
11324 /* X | ~X is -1. */
11325 if (TREE_CODE (arg1
) == BIT_NOT_EXPR
11326 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11328 t1
= build_zero_cst (type
);
11329 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11330 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
11333 /* Canonicalize (X & C1) | C2. */
11334 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11335 && TREE_CODE (arg1
) == INTEGER_CST
11336 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11338 int width
= TYPE_PRECISION (type
), w
;
11339 bool try_simplify
= true;
11340 wide_int c1
= TREE_OPERAND (arg0
, 1);
11341 wide_int c2
= arg1
;
11343 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11344 if ((c1
& c2
) == c1
)
11345 return omit_one_operand_loc (loc
, type
, arg1
,
11346 TREE_OPERAND (arg0
, 0));
11348 wide_int msk
= wi::mask (width
, false,
11349 TYPE_PRECISION (TREE_TYPE (arg1
)));
11351 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11352 if (msk
.and_not (c1
| c2
) == 0)
11353 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
11354 TREE_OPERAND (arg0
, 0), arg1
);
11356 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11357 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11358 mode which allows further optimizations. */
11361 wide_int c3
= c1
.and_not (c2
);
11362 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
11364 wide_int mask
= wi::mask (width
- w
, false,
11365 TYPE_PRECISION (type
));
11366 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
11373 /* If X is a tree of the form (Y * K1) & K2, this might conflict
11374 with that optimization from the BIT_AND_EXPR optimizations.
11375 This could end up in an infinite recursion. */
11376 if (TREE_CODE (TREE_OPERAND (arg0
, 0)) == MULT_EXPR
11377 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
11380 tree t
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
11381 wide_int masked
= mask_with_tz (type
, c3
, t
);
11383 try_simplify
= (masked
!= c1
);
11386 if (try_simplify
&& c3
!= c1
)
11387 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
11388 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11389 TREE_OPERAND (arg0
, 0),
11390 wide_int_to_tree (type
,
11395 /* (X & Y) | Y is (X, Y). */
11396 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11397 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11398 return omit_one_operand_loc (loc
, type
, arg1
, TREE_OPERAND (arg0
, 0));
11399 /* (X & Y) | X is (Y, X). */
11400 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11401 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11402 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11403 return omit_one_operand_loc (loc
, type
, arg1
, TREE_OPERAND (arg0
, 1));
11404 /* X | (X & Y) is (Y, X). */
11405 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11406 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0)
11407 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 1)))
11408 return omit_one_operand_loc (loc
, type
, arg0
, TREE_OPERAND (arg1
, 1));
11409 /* X | (Y & X) is (Y, X). */
11410 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11411 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11412 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11413 return omit_one_operand_loc (loc
, type
, arg0
, TREE_OPERAND (arg1
, 0));
11415 /* (X & ~Y) | (~X & Y) is X ^ Y */
11416 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11417 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
11419 tree a0
, a1
, l0
, l1
, n0
, n1
;
11421 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11422 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11424 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11425 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11427 n0
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, l0
);
11428 n1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, l1
);
11430 if ((operand_equal_p (n0
, a0
, 0)
11431 && operand_equal_p (n1
, a1
, 0))
11432 || (operand_equal_p (n0
, a1
, 0)
11433 && operand_equal_p (n1
, a0
, 0)))
11434 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, l0
, n1
);
11437 t1
= distribute_bit_expr (loc
, code
, type
, arg0
, arg1
);
11438 if (t1
!= NULL_TREE
)
11441 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11443 This results in more efficient code for machines without a NAND
11444 instruction. Combine will canonicalize to the first form
11445 which will allow use of NAND instructions provided by the
11446 backend if they exist. */
11447 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11448 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11451 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
11452 build2 (BIT_AND_EXPR
, type
,
11453 fold_convert_loc (loc
, type
,
11454 TREE_OPERAND (arg0
, 0)),
11455 fold_convert_loc (loc
, type
,
11456 TREE_OPERAND (arg1
, 0))));
11459 /* See if this can be simplified into a rotate first. If that
11460 is unsuccessful continue in the association code. */
11464 if (integer_zerop (arg1
))
11465 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11466 if (integer_all_onesp (arg1
))
11467 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, op0
);
11468 if (operand_equal_p (arg0
, arg1
, 0))
11469 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11471 /* ~X ^ X is -1. */
11472 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11473 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11475 t1
= build_zero_cst (type
);
11476 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11477 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
11480 /* X ^ ~X is -1. */
11481 if (TREE_CODE (arg1
) == BIT_NOT_EXPR
11482 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11484 t1
= build_zero_cst (type
);
11485 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11486 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
11489 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11490 with a constant, and the two constants have no bits in common,
11491 we should treat this as a BIT_IOR_EXPR since this may produce more
11492 simplifications. */
11493 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11494 && TREE_CODE (arg1
) == BIT_AND_EXPR
11495 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11496 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
11497 && integer_zerop (const_binop (BIT_AND_EXPR
,
11498 TREE_OPERAND (arg0
, 1),
11499 TREE_OPERAND (arg1
, 1))))
11501 code
= BIT_IOR_EXPR
;
11505 /* (X | Y) ^ X -> Y & ~ X*/
11506 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11507 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11509 tree t2
= TREE_OPERAND (arg0
, 1);
11510 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg1
),
11512 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11513 fold_convert_loc (loc
, type
, t2
),
11514 fold_convert_loc (loc
, type
, t1
));
11518 /* (Y | X) ^ X -> Y & ~ X*/
11519 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11520 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11522 tree t2
= TREE_OPERAND (arg0
, 0);
11523 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg1
),
11525 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11526 fold_convert_loc (loc
, type
, t2
),
11527 fold_convert_loc (loc
, type
, t1
));
11531 /* X ^ (X | Y) -> Y & ~ X*/
11532 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11533 && operand_equal_p (TREE_OPERAND (arg1
, 0), arg0
, 0))
11535 tree t2
= TREE_OPERAND (arg1
, 1);
11536 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg0
),
11538 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11539 fold_convert_loc (loc
, type
, t2
),
11540 fold_convert_loc (loc
, type
, t1
));
11544 /* X ^ (Y | X) -> Y & ~ X*/
11545 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11546 && operand_equal_p (TREE_OPERAND (arg1
, 1), arg0
, 0))
11548 tree t2
= TREE_OPERAND (arg1
, 0);
11549 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg0
),
11551 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11552 fold_convert_loc (loc
, type
, t2
),
11553 fold_convert_loc (loc
, type
, t1
));
11557 /* Convert ~X ^ ~Y to X ^ Y. */
11558 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11559 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11560 return fold_build2_loc (loc
, code
, type
,
11561 fold_convert_loc (loc
, type
,
11562 TREE_OPERAND (arg0
, 0)),
11563 fold_convert_loc (loc
, type
,
11564 TREE_OPERAND (arg1
, 0)));
11566 /* Convert ~X ^ C to X ^ ~C. */
11567 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11568 && TREE_CODE (arg1
) == INTEGER_CST
)
11569 return fold_build2_loc (loc
, code
, type
,
11570 fold_convert_loc (loc
, type
,
11571 TREE_OPERAND (arg0
, 0)),
11572 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, arg1
));
11574 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11575 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11576 && integer_onep (TREE_OPERAND (arg0
, 1))
11577 && integer_onep (arg1
))
11578 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11579 build_zero_cst (TREE_TYPE (arg0
)));
11581 /* Fold (X & Y) ^ Y as ~X & Y. */
11582 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11583 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11585 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11586 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11587 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11588 fold_convert_loc (loc
, type
, arg1
));
11590 /* Fold (X & Y) ^ X as ~Y & X. */
11591 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11592 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11593 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11595 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11596 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11597 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11598 fold_convert_loc (loc
, type
, arg1
));
11600 /* Fold X ^ (X & Y) as X & ~Y. */
11601 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11602 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11604 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11605 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11606 fold_convert_loc (loc
, type
, arg0
),
11607 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
11609 /* Fold X ^ (Y & X) as ~Y & X. */
11610 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11611 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11612 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11614 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11615 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11616 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11617 fold_convert_loc (loc
, type
, arg0
));
11620 /* See if this can be simplified into a rotate first. If that
11621 is unsuccessful continue in the association code. */
11625 if (integer_all_onesp (arg1
))
11626 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11627 if (integer_zerop (arg1
))
11628 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11629 if (operand_equal_p (arg0
, arg1
, 0))
11630 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11632 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11633 if ((TREE_CODE (arg0
) == BIT_NOT_EXPR
11634 || TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11635 || (TREE_CODE (arg0
) == EQ_EXPR
11636 && integer_zerop (TREE_OPERAND (arg0
, 1))))
11637 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11638 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
11640 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11641 if ((TREE_CODE (arg1
) == BIT_NOT_EXPR
11642 || TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11643 || (TREE_CODE (arg1
) == EQ_EXPR
11644 && integer_zerop (TREE_OPERAND (arg1
, 1))))
11645 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11646 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11648 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11649 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11650 && TREE_CODE (arg1
) == INTEGER_CST
11651 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11653 tree tmp1
= fold_convert_loc (loc
, type
, arg1
);
11654 tree tmp2
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11655 tree tmp3
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11656 tmp2
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tmp2
, tmp1
);
11657 tmp3
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tmp3
, tmp1
);
11659 fold_convert_loc (loc
, type
,
11660 fold_build2_loc (loc
, BIT_IOR_EXPR
,
11661 type
, tmp2
, tmp3
));
11664 /* (X | Y) & Y is (X, Y). */
11665 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11666 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11667 return omit_one_operand_loc (loc
, type
, arg1
, TREE_OPERAND (arg0
, 0));
11668 /* (X | Y) & X is (Y, X). */
11669 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11670 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11671 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11672 return omit_one_operand_loc (loc
, type
, arg1
, TREE_OPERAND (arg0
, 1));
11673 /* X & (X | Y) is (Y, X). */
11674 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11675 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0)
11676 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 1)))
11677 return omit_one_operand_loc (loc
, type
, arg0
, TREE_OPERAND (arg1
, 1));
11678 /* X & (Y | X) is (Y, X). */
11679 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11680 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11681 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11682 return omit_one_operand_loc (loc
, type
, arg0
, TREE_OPERAND (arg1
, 0));
11684 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11685 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11686 && integer_onep (TREE_OPERAND (arg0
, 1))
11687 && integer_onep (arg1
))
11690 tem
= TREE_OPERAND (arg0
, 0);
11691 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11692 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11694 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11695 build_zero_cst (TREE_TYPE (tem
)));
11697 /* Fold ~X & 1 as (X & 1) == 0. */
11698 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11699 && integer_onep (arg1
))
11702 tem
= TREE_OPERAND (arg0
, 0);
11703 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11704 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11706 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11707 build_zero_cst (TREE_TYPE (tem
)));
11709 /* Fold !X & 1 as X == 0. */
11710 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11711 && integer_onep (arg1
))
11713 tem
= TREE_OPERAND (arg0
, 0);
11714 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11715 build_zero_cst (TREE_TYPE (tem
)));
11718 /* Fold (X ^ Y) & Y as ~X & Y. */
11719 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11720 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11722 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11723 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11724 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11725 fold_convert_loc (loc
, type
, arg1
));
11727 /* Fold (X ^ Y) & X as ~Y & X. */
11728 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11729 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11730 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11732 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11733 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11734 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11735 fold_convert_loc (loc
, type
, arg1
));
11737 /* Fold X & (X ^ Y) as X & ~Y. */
11738 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
11739 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11741 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11742 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11743 fold_convert_loc (loc
, type
, arg0
),
11744 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
11746 /* Fold X & (Y ^ X) as ~Y & X. */
11747 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
11748 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11749 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11751 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11752 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11753 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11754 fold_convert_loc (loc
, type
, arg0
));
11757 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11758 multiple of 1 << CST. */
11759 if (TREE_CODE (arg1
) == INTEGER_CST
)
11761 wide_int cst1
= arg1
;
11762 wide_int ncst1
= -cst1
;
11763 if ((cst1
& ncst1
) == ncst1
11764 && multiple_of_p (type
, arg0
,
11765 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11766 return fold_convert_loc (loc
, type
, arg0
);
11769 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11771 if (TREE_CODE (arg1
) == INTEGER_CST
11772 && TREE_CODE (arg0
) == MULT_EXPR
11773 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11775 wide_int masked
= mask_with_tz (type
, arg1
, TREE_OPERAND (arg0
, 1));
11778 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11780 else if (masked
!= arg1
)
11781 return fold_build2_loc (loc
, code
, type
, op0
,
11782 wide_int_to_tree (type
, masked
));
11785 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11786 ((A & N) + B) & M -> (A + B) & M
11787 Similarly if (N & M) == 0,
11788 ((A | N) + B) & M -> (A + B) & M
11789 and for - instead of + (or unary - instead of +)
11790 and/or ^ instead of |.
11791 If B is constant and (B & M) == 0, fold into A & M. */
11792 if (TREE_CODE (arg1
) == INTEGER_CST
)
11794 wide_int cst1
= arg1
;
11795 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
11796 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11797 && (TREE_CODE (arg0
) == PLUS_EXPR
11798 || TREE_CODE (arg0
) == MINUS_EXPR
11799 || TREE_CODE (arg0
) == NEGATE_EXPR
)
11800 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
11801 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
11807 /* Now we know that arg0 is (C + D) or (C - D) or
11808 -C and arg1 (M) is == (1LL << cst) - 1.
11809 Store C into PMOP[0] and D into PMOP[1]. */
11810 pmop
[0] = TREE_OPERAND (arg0
, 0);
11812 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
11814 pmop
[1] = TREE_OPERAND (arg0
, 1);
11818 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
11821 for (; which
>= 0; which
--)
11822 switch (TREE_CODE (pmop
[which
]))
11827 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
11830 cst0
= TREE_OPERAND (pmop
[which
], 1);
11832 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
11837 else if (cst0
!= 0)
11839 /* If C or D is of the form (A & N) where
11840 (N & M) == M, or of the form (A | N) or
11841 (A ^ N) where (N & M) == 0, replace it with A. */
11842 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
11845 /* If C or D is a N where (N & M) == 0, it can be
11846 omitted (assumed 0). */
11847 if ((TREE_CODE (arg0
) == PLUS_EXPR
11848 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
11849 && (cst1
& pmop
[which
]) == 0)
11850 pmop
[which
] = NULL
;
11856 /* Only build anything new if we optimized one or both arguments
11858 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
11859 || (TREE_CODE (arg0
) != NEGATE_EXPR
11860 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
11862 tree utype
= TREE_TYPE (arg0
);
11863 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11865 /* Perform the operations in a type that has defined
11866 overflow behavior. */
11867 utype
= unsigned_type_for (TREE_TYPE (arg0
));
11868 if (pmop
[0] != NULL
)
11869 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
11870 if (pmop
[1] != NULL
)
11871 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
11874 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
11875 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
11876 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
11878 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
11879 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
11881 else if (pmop
[0] != NULL
)
11883 else if (pmop
[1] != NULL
)
11886 return build_int_cst (type
, 0);
11888 else if (pmop
[0] == NULL
)
11889 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
11891 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
11893 /* TEM is now the new binary +, - or unary - replacement. */
11894 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
11895 fold_convert_loc (loc
, utype
, arg1
));
11896 return fold_convert_loc (loc
, type
, tem
);
11901 t1
= distribute_bit_expr (loc
, code
, type
, arg0
, arg1
);
11902 if (t1
!= NULL_TREE
)
11904 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11905 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11906 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11908 prec
= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11910 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
11913 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11916 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11918 This results in more efficient code for machines without a NOR
11919 instruction. Combine will canonicalize to the first form
11920 which will allow use of NOR instructions provided by the
11921 backend if they exist. */
11922 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11923 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11925 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
11926 build2 (BIT_IOR_EXPR
, type
,
11927 fold_convert_loc (loc
, type
,
11928 TREE_OPERAND (arg0
, 0)),
11929 fold_convert_loc (loc
, type
,
11930 TREE_OPERAND (arg1
, 0))));
11933 /* If arg0 is derived from the address of an object or function, we may
11934 be able to fold this expression using the object or function's
11936 if (POINTER_TYPE_P (TREE_TYPE (arg0
)) && tree_fits_uhwi_p (arg1
))
11938 unsigned HOST_WIDE_INT modulus
, residue
;
11939 unsigned HOST_WIDE_INT low
= tree_to_uhwi (arg1
);
11941 modulus
= get_pointer_modulus_and_residue (arg0
, &residue
,
11942 integer_onep (arg1
));
11944 /* This works because modulus is a power of 2. If this weren't the
11945 case, we'd have to replace it by its greatest power-of-2
11946 divisor: modulus & -modulus. */
11948 return build_int_cst (type
, residue
& low
);
11951 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11952 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11953 if the new mask might be further optimized. */
11954 if ((TREE_CODE (arg0
) == LSHIFT_EXPR
11955 || TREE_CODE (arg0
) == RSHIFT_EXPR
)
11956 && TYPE_PRECISION (TREE_TYPE (arg0
)) <= HOST_BITS_PER_WIDE_INT
11957 && TREE_CODE (arg1
) == INTEGER_CST
11958 && tree_fits_uhwi_p (TREE_OPERAND (arg0
, 1))
11959 && tree_to_uhwi (TREE_OPERAND (arg0
, 1)) > 0
11960 && (tree_to_uhwi (TREE_OPERAND (arg0
, 1))
11961 < TYPE_PRECISION (TREE_TYPE (arg0
))))
11963 unsigned int shiftc
= tree_to_uhwi (TREE_OPERAND (arg0
, 1));
11964 unsigned HOST_WIDE_INT mask
= TREE_INT_CST_LOW (arg1
);
11965 unsigned HOST_WIDE_INT newmask
, zerobits
= 0;
11966 tree shift_type
= TREE_TYPE (arg0
);
11968 if (TREE_CODE (arg0
) == LSHIFT_EXPR
)
11969 zerobits
= ((((unsigned HOST_WIDE_INT
) 1) << shiftc
) - 1);
11970 else if (TREE_CODE (arg0
) == RSHIFT_EXPR
11971 && TYPE_PRECISION (TREE_TYPE (arg0
))
11972 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg0
))))
11974 prec
= TYPE_PRECISION (TREE_TYPE (arg0
));
11975 tree arg00
= TREE_OPERAND (arg0
, 0);
11976 /* See if more bits can be proven as zero because of
11978 if (TREE_CODE (arg00
) == NOP_EXPR
11979 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00
, 0))))
11981 tree inner_type
= TREE_TYPE (TREE_OPERAND (arg00
, 0));
11982 if (TYPE_PRECISION (inner_type
)
11983 == GET_MODE_PRECISION (TYPE_MODE (inner_type
))
11984 && TYPE_PRECISION (inner_type
) < prec
)
11986 prec
= TYPE_PRECISION (inner_type
);
11987 /* See if we can shorten the right shift. */
11989 shift_type
= inner_type
;
11992 zerobits
= ~(unsigned HOST_WIDE_INT
) 0;
11993 zerobits
>>= HOST_BITS_PER_WIDE_INT
- shiftc
;
11994 zerobits
<<= prec
- shiftc
;
11995 /* For arithmetic shift if sign bit could be set, zerobits
11996 can contain actually sign bits, so no transformation is
11997 possible, unless MASK masks them all away. In that
11998 case the shift needs to be converted into logical shift. */
11999 if (!TYPE_UNSIGNED (TREE_TYPE (arg0
))
12000 && prec
== TYPE_PRECISION (TREE_TYPE (arg0
)))
12002 if ((mask
& zerobits
) == 0)
12003 shift_type
= unsigned_type_for (TREE_TYPE (arg0
));
12009 /* ((X << 16) & 0xff00) is (X, 0). */
12010 if ((mask
& zerobits
) == mask
)
12011 return omit_one_operand_loc (loc
, type
,
12012 build_int_cst (type
, 0), arg0
);
12014 newmask
= mask
| zerobits
;
12015 if (newmask
!= mask
&& (newmask
& (newmask
+ 1)) == 0)
12017 /* Only do the transformation if NEWMASK is some integer
12019 for (prec
= BITS_PER_UNIT
;
12020 prec
< HOST_BITS_PER_WIDE_INT
; prec
<<= 1)
12021 if (newmask
== (((unsigned HOST_WIDE_INT
) 1) << prec
) - 1)
12023 if (prec
< HOST_BITS_PER_WIDE_INT
12024 || newmask
== ~(unsigned HOST_WIDE_INT
) 0)
12028 if (shift_type
!= TREE_TYPE (arg0
))
12030 tem
= fold_build2_loc (loc
, TREE_CODE (arg0
), shift_type
,
12031 fold_convert_loc (loc
, shift_type
,
12032 TREE_OPERAND (arg0
, 0)),
12033 TREE_OPERAND (arg0
, 1));
12034 tem
= fold_convert_loc (loc
, type
, tem
);
12038 newmaskt
= build_int_cst_type (TREE_TYPE (op1
), newmask
);
12039 if (!tree_int_cst_equal (newmaskt
, arg1
))
12040 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
, tem
, newmaskt
);
12048 /* Don't touch a floating-point divide by zero unless the mode
12049 of the constant can represent infinity. */
12050 if (TREE_CODE (arg1
) == REAL_CST
12051 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
12052 && real_zerop (arg1
))
12055 /* Optimize A / A to 1.0 if we don't care about
12056 NaNs or Infinities. Skip the transformation
12057 for non-real operands. */
12058 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0
))
12059 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
)))
12060 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0
)))
12061 && operand_equal_p (arg0
, arg1
, 0))
12063 tree r
= build_real (TREE_TYPE (arg0
), dconst1
);
12065 return omit_two_operands_loc (loc
, type
, r
, arg0
, arg1
);
12068 /* The complex version of the above A / A optimization. */
12069 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
12070 && operand_equal_p (arg0
, arg1
, 0))
12072 tree elem_type
= TREE_TYPE (TREE_TYPE (arg0
));
12073 if (! HONOR_NANS (TYPE_MODE (elem_type
))
12074 && ! HONOR_INFINITIES (TYPE_MODE (elem_type
)))
12076 tree r
= build_real (elem_type
, dconst1
);
12077 /* omit_two_operands will call fold_convert for us. */
12078 return omit_two_operands_loc (loc
, type
, r
, arg0
, arg1
);
12082 /* (-A) / (-B) -> A / B */
12083 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
12084 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12085 TREE_OPERAND (arg0
, 0),
12086 negate_expr (arg1
));
12087 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
12088 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12089 negate_expr (arg0
),
12090 TREE_OPERAND (arg1
, 0));
12092 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
12093 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
12094 && real_onep (arg1
))
12095 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12097 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
12098 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
12099 && real_minus_onep (arg1
))
12100 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
,
12101 negate_expr (arg0
)));
12103 /* If ARG1 is a constant, we can convert this to a multiply by the
12104 reciprocal. This does not have the same rounding properties,
12105 so only do this if -freciprocal-math. We can actually
12106 always safely do it if ARG1 is a power of two, but it's hard to
12107 tell if it is or not in a portable manner. */
12109 && (TREE_CODE (arg1
) == REAL_CST
12110 || (TREE_CODE (arg1
) == COMPLEX_CST
12111 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg1
)))
12112 || (TREE_CODE (arg1
) == VECTOR_CST
12113 && VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg1
)))))
12115 if (flag_reciprocal_math
12116 && 0 != (tem
= const_binop (code
, build_one_cst (type
), arg1
)))
12117 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, tem
);
12118 /* Find the reciprocal if optimizing and the result is exact.
12119 TODO: Complex reciprocal not implemented. */
12120 if (TREE_CODE (arg1
) != COMPLEX_CST
)
12122 tree inverse
= exact_inverse (TREE_TYPE (arg0
), arg1
);
12125 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, inverse
);
12128 /* Convert A/B/C to A/(B*C). */
12129 if (flag_reciprocal_math
12130 && TREE_CODE (arg0
) == RDIV_EXPR
)
12131 return fold_build2_loc (loc
, RDIV_EXPR
, type
, TREE_OPERAND (arg0
, 0),
12132 fold_build2_loc (loc
, MULT_EXPR
, type
,
12133 TREE_OPERAND (arg0
, 1), arg1
));
12135 /* Convert A/(B/C) to (A/B)*C. */
12136 if (flag_reciprocal_math
12137 && TREE_CODE (arg1
) == RDIV_EXPR
)
12138 return fold_build2_loc (loc
, MULT_EXPR
, type
,
12139 fold_build2_loc (loc
, RDIV_EXPR
, type
, arg0
,
12140 TREE_OPERAND (arg1
, 0)),
12141 TREE_OPERAND (arg1
, 1));
12143 /* Convert C1/(X*C2) into (C1/C2)/X. */
12144 if (flag_reciprocal_math
12145 && TREE_CODE (arg1
) == MULT_EXPR
12146 && TREE_CODE (arg0
) == REAL_CST
12147 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
12149 tree tem
= const_binop (RDIV_EXPR
, arg0
,
12150 TREE_OPERAND (arg1
, 1));
12152 return fold_build2_loc (loc
, RDIV_EXPR
, type
, tem
,
12153 TREE_OPERAND (arg1
, 0));
12156 if (flag_unsafe_math_optimizations
)
12158 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
12159 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
12161 /* Optimize sin(x)/cos(x) as tan(x). */
12162 if (((fcode0
== BUILT_IN_SIN
&& fcode1
== BUILT_IN_COS
)
12163 || (fcode0
== BUILT_IN_SINF
&& fcode1
== BUILT_IN_COSF
)
12164 || (fcode0
== BUILT_IN_SINL
&& fcode1
== BUILT_IN_COSL
))
12165 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
12166 CALL_EXPR_ARG (arg1
, 0), 0))
12168 tree tanfn
= mathfn_built_in (type
, BUILT_IN_TAN
);
12170 if (tanfn
!= NULL_TREE
)
12171 return build_call_expr_loc (loc
, tanfn
, 1, CALL_EXPR_ARG (arg0
, 0));
12174 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
12175 if (((fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_SIN
)
12176 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_SINF
)
12177 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_SINL
))
12178 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
12179 CALL_EXPR_ARG (arg1
, 0), 0))
12181 tree tanfn
= mathfn_built_in (type
, BUILT_IN_TAN
);
12183 if (tanfn
!= NULL_TREE
)
12185 tree tmp
= build_call_expr_loc (loc
, tanfn
, 1,
12186 CALL_EXPR_ARG (arg0
, 0));
12187 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12188 build_real (type
, dconst1
), tmp
);
12192 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
12193 NaNs or Infinities. */
12194 if (((fcode0
== BUILT_IN_SIN
&& fcode1
== BUILT_IN_TAN
)
12195 || (fcode0
== BUILT_IN_SINF
&& fcode1
== BUILT_IN_TANF
)
12196 || (fcode0
== BUILT_IN_SINL
&& fcode1
== BUILT_IN_TANL
)))
12198 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
12199 tree arg01
= CALL_EXPR_ARG (arg1
, 0);
12201 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00
)))
12202 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00
)))
12203 && operand_equal_p (arg00
, arg01
, 0))
12205 tree cosfn
= mathfn_built_in (type
, BUILT_IN_COS
);
12207 if (cosfn
!= NULL_TREE
)
12208 return build_call_expr_loc (loc
, cosfn
, 1, arg00
);
12212 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
12213 NaNs or Infinities. */
12214 if (((fcode0
== BUILT_IN_TAN
&& fcode1
== BUILT_IN_SIN
)
12215 || (fcode0
== BUILT_IN_TANF
&& fcode1
== BUILT_IN_SINF
)
12216 || (fcode0
== BUILT_IN_TANL
&& fcode1
== BUILT_IN_SINL
)))
12218 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
12219 tree arg01
= CALL_EXPR_ARG (arg1
, 0);
12221 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00
)))
12222 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00
)))
12223 && operand_equal_p (arg00
, arg01
, 0))
12225 tree cosfn
= mathfn_built_in (type
, BUILT_IN_COS
);
12227 if (cosfn
!= NULL_TREE
)
12229 tree tmp
= build_call_expr_loc (loc
, cosfn
, 1, arg00
);
12230 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
12231 build_real (type
, dconst1
),
12237 /* Optimize pow(x,c)/x as pow(x,c-1). */
12238 if (fcode0
== BUILT_IN_POW
12239 || fcode0
== BUILT_IN_POWF
12240 || fcode0
== BUILT_IN_POWL
)
12242 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
12243 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
12244 if (TREE_CODE (arg01
) == REAL_CST
12245 && !TREE_OVERFLOW (arg01
)
12246 && operand_equal_p (arg1
, arg00
, 0))
12248 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
12252 c
= TREE_REAL_CST (arg01
);
12253 real_arithmetic (&c
, MINUS_EXPR
, &c
, &dconst1
);
12254 arg
= build_real (type
, c
);
12255 return build_call_expr_loc (loc
, powfn
, 2, arg1
, arg
);
12259 /* Optimize a/root(b/c) into a*root(c/b). */
12260 if (BUILTIN_ROOT_P (fcode1
))
12262 tree rootarg
= CALL_EXPR_ARG (arg1
, 0);
12264 if (TREE_CODE (rootarg
) == RDIV_EXPR
)
12266 tree rootfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
12267 tree b
= TREE_OPERAND (rootarg
, 0);
12268 tree c
= TREE_OPERAND (rootarg
, 1);
12270 tree tmp
= fold_build2_loc (loc
, RDIV_EXPR
, type
, c
, b
);
12272 tmp
= build_call_expr_loc (loc
, rootfn
, 1, tmp
);
12273 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, tmp
);
12277 /* Optimize x/expN(y) into x*expN(-y). */
12278 if (BUILTIN_EXPONENT_P (fcode1
))
12280 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
12281 tree arg
= negate_expr (CALL_EXPR_ARG (arg1
, 0));
12282 arg1
= build_call_expr_loc (loc
,
12284 fold_convert_loc (loc
, type
, arg
));
12285 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
12288 /* Optimize x/pow(y,z) into x*pow(y,-z). */
12289 if (fcode1
== BUILT_IN_POW
12290 || fcode1
== BUILT_IN_POWF
12291 || fcode1
== BUILT_IN_POWL
)
12293 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
12294 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
12295 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
12296 tree neg11
= fold_convert_loc (loc
, type
,
12297 negate_expr (arg11
));
12298 arg1
= build_call_expr_loc (loc
, powfn
, 2, arg10
, neg11
);
12299 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
12304 case TRUNC_DIV_EXPR
:
12305 /* Optimize (X & (-A)) / A where A is a power of 2,
12307 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12308 && !TYPE_UNSIGNED (type
) && TREE_CODE (arg1
) == INTEGER_CST
12309 && integer_pow2p (arg1
) && tree_int_cst_sgn (arg1
) > 0)
12311 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (arg1
),
12312 arg1
, TREE_OPERAND (arg0
, 1));
12313 if (sum
&& integer_zerop (sum
)) {
12314 tree pow2
= build_int_cst (integer_type_node
,
12315 wi::exact_log2 (arg1
));
12316 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
12317 TREE_OPERAND (arg0
, 0), pow2
);
12323 case FLOOR_DIV_EXPR
:
12324 /* Simplify A / (B << N) where A and B are positive and B is
12325 a power of 2, to A >> (N + log2(B)). */
12326 strict_overflow_p
= false;
12327 if (TREE_CODE (arg1
) == LSHIFT_EXPR
12328 && (TYPE_UNSIGNED (type
)
12329 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
12331 tree sval
= TREE_OPERAND (arg1
, 0);
12332 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
12334 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
12335 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
12336 wi::exact_log2 (sval
));
12338 if (strict_overflow_p
)
12339 fold_overflow_warning (("assuming signed overflow does not "
12340 "occur when simplifying A / (B << N)"),
12341 WARN_STRICT_OVERFLOW_MISC
);
12343 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
12345 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
12346 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
12350 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12351 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
12352 if (INTEGRAL_TYPE_P (type
)
12353 && TYPE_UNSIGNED (type
)
12354 && code
== FLOOR_DIV_EXPR
)
12355 return fold_build2_loc (loc
, TRUNC_DIV_EXPR
, type
, op0
, op1
);
12359 case ROUND_DIV_EXPR
:
12360 case CEIL_DIV_EXPR
:
12361 case EXACT_DIV_EXPR
:
12362 if (integer_onep (arg1
))
12363 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12364 if (integer_zerop (arg1
))
12366 /* X / -1 is -X. */
12367 if (!TYPE_UNSIGNED (type
)
12368 && TREE_CODE (arg1
) == INTEGER_CST
12369 && wi::eq_p (arg1
, -1))
12370 return fold_convert_loc (loc
, type
, negate_expr (arg0
));
12372 /* Convert -A / -B to A / B when the type is signed and overflow is
12374 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12375 && TREE_CODE (arg0
) == NEGATE_EXPR
12376 && negate_expr_p (arg1
))
12378 if (INTEGRAL_TYPE_P (type
))
12379 fold_overflow_warning (("assuming signed overflow does not occur "
12380 "when distributing negation across "
12382 WARN_STRICT_OVERFLOW_MISC
);
12383 return fold_build2_loc (loc
, code
, type
,
12384 fold_convert_loc (loc
, type
,
12385 TREE_OPERAND (arg0
, 0)),
12386 fold_convert_loc (loc
, type
,
12387 negate_expr (arg1
)));
12389 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12390 && TREE_CODE (arg1
) == NEGATE_EXPR
12391 && negate_expr_p (arg0
))
12393 if (INTEGRAL_TYPE_P (type
))
12394 fold_overflow_warning (("assuming signed overflow does not occur "
12395 "when distributing negation across "
12397 WARN_STRICT_OVERFLOW_MISC
);
12398 return fold_build2_loc (loc
, code
, type
,
12399 fold_convert_loc (loc
, type
,
12400 negate_expr (arg0
)),
12401 fold_convert_loc (loc
, type
,
12402 TREE_OPERAND (arg1
, 0)));
12405 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12406 operation, EXACT_DIV_EXPR.
12408 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12409 At one time others generated faster code, it's not clear if they do
12410 after the last round to changes to the DIV code in expmed.c. */
12411 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
12412 && multiple_of_p (type
, arg0
, arg1
))
12413 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
, arg0
, arg1
);
12415 strict_overflow_p
= false;
12416 if (TREE_CODE (arg1
) == INTEGER_CST
12417 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12418 &strict_overflow_p
)))
12420 if (strict_overflow_p
)
12421 fold_overflow_warning (("assuming signed overflow does not occur "
12422 "when simplifying division"),
12423 WARN_STRICT_OVERFLOW_MISC
);
12424 return fold_convert_loc (loc
, type
, tem
);
12429 case CEIL_MOD_EXPR
:
12430 case FLOOR_MOD_EXPR
:
12431 case ROUND_MOD_EXPR
:
12432 case TRUNC_MOD_EXPR
:
12433 /* X % 1 is always zero, but be sure to preserve any side
12435 if (integer_onep (arg1
))
12436 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12438 /* X % 0, return X % 0 unchanged so that we can get the
12439 proper warnings and errors. */
12440 if (integer_zerop (arg1
))
12443 /* 0 % X is always zero, but be sure to preserve any side
12444 effects in X. Place this after checking for X == 0. */
12445 if (integer_zerop (arg0
))
12446 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12448 /* X % -1 is zero. */
12449 if (!TYPE_UNSIGNED (type
)
12450 && TREE_CODE (arg1
) == INTEGER_CST
12451 && wi::eq_p (arg1
, -1))
12452 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12454 /* X % -C is the same as X % C. */
12455 if (code
== TRUNC_MOD_EXPR
12456 && TYPE_SIGN (type
) == SIGNED
12457 && TREE_CODE (arg1
) == INTEGER_CST
12458 && !TREE_OVERFLOW (arg1
)
12459 && wi::neg_p (arg1
)
12460 && !TYPE_OVERFLOW_TRAPS (type
)
12461 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
12462 && !sign_bit_p (arg1
, arg1
))
12463 return fold_build2_loc (loc
, code
, type
,
12464 fold_convert_loc (loc
, type
, arg0
),
12465 fold_convert_loc (loc
, type
,
12466 negate_expr (arg1
)));
12468 /* X % -Y is the same as X % Y. */
12469 if (code
== TRUNC_MOD_EXPR
12470 && !TYPE_UNSIGNED (type
)
12471 && TREE_CODE (arg1
) == NEGATE_EXPR
12472 && !TYPE_OVERFLOW_TRAPS (type
))
12473 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, arg0
),
12474 fold_convert_loc (loc
, type
,
12475 TREE_OPERAND (arg1
, 0)));
12477 strict_overflow_p
= false;
12478 if (TREE_CODE (arg1
) == INTEGER_CST
12479 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
12480 &strict_overflow_p
)))
12482 if (strict_overflow_p
)
12483 fold_overflow_warning (("assuming signed overflow does not occur "
12484 "when simplifying modulus"),
12485 WARN_STRICT_OVERFLOW_MISC
);
12486 return fold_convert_loc (loc
, type
, tem
);
12489 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12490 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
12491 if ((code
== TRUNC_MOD_EXPR
|| code
== FLOOR_MOD_EXPR
)
12492 && (TYPE_UNSIGNED (type
)
12493 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
12496 /* Also optimize A % (C << N) where C is a power of 2,
12497 to A & ((C << N) - 1). */
12498 if (TREE_CODE (arg1
) == LSHIFT_EXPR
)
12499 c
= TREE_OPERAND (arg1
, 0);
12501 if (integer_pow2p (c
) && tree_int_cst_sgn (c
) > 0)
12504 = fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (arg1
), arg1
,
12505 build_int_cst (TREE_TYPE (arg1
), 1));
12506 if (strict_overflow_p
)
12507 fold_overflow_warning (("assuming signed overflow does not "
12508 "occur when simplifying "
12509 "X % (power of two)"),
12510 WARN_STRICT_OVERFLOW_MISC
);
12511 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
12512 fold_convert_loc (loc
, type
, arg0
),
12513 fold_convert_loc (loc
, type
, mask
));
12521 if (integer_all_onesp (arg0
))
12522 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12526 /* Optimize -1 >> x for arithmetic right shifts. */
12527 if (integer_all_onesp (arg0
) && !TYPE_UNSIGNED (type
)
12528 && tree_expr_nonnegative_p (arg1
))
12529 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12530 /* ... fall through ... */
12534 if (integer_zerop (arg1
))
12535 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12536 if (integer_zerop (arg0
))
12537 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12539 /* Prefer vector1 << scalar to vector1 << vector2
12540 if vector2 is uniform. */
12541 if (VECTOR_TYPE_P (TREE_TYPE (arg1
))
12542 && (tem
= uniform_vector_p (arg1
)) != NULL_TREE
)
12543 return fold_build2_loc (loc
, code
, type
, op0
, tem
);
12545 /* Since negative shift count is not well-defined,
12546 don't try to compute it in the compiler. */
12547 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
12550 prec
= element_precision (type
);
12552 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
12553 if (TREE_CODE (op0
) == code
&& tree_fits_uhwi_p (arg1
)
12554 && tree_to_uhwi (arg1
) < prec
12555 && tree_fits_uhwi_p (TREE_OPERAND (arg0
, 1))
12556 && tree_to_uhwi (TREE_OPERAND (arg0
, 1)) < prec
)
12558 unsigned int low
= (tree_to_uhwi (TREE_OPERAND (arg0
, 1))
12559 + tree_to_uhwi (arg1
));
12561 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12562 being well defined. */
12565 if (code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
12567 else if (TYPE_UNSIGNED (type
) || code
== LSHIFT_EXPR
)
12568 return omit_one_operand_loc (loc
, type
, build_zero_cst (type
),
12569 TREE_OPERAND (arg0
, 0));
12574 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
12575 build_int_cst (TREE_TYPE (arg1
), low
));
12578 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12579 into x & ((unsigned)-1 >> c) for unsigned types. */
12580 if (((code
== LSHIFT_EXPR
&& TREE_CODE (arg0
) == RSHIFT_EXPR
)
12581 || (TYPE_UNSIGNED (type
)
12582 && code
== RSHIFT_EXPR
&& TREE_CODE (arg0
) == LSHIFT_EXPR
))
12583 && tree_fits_uhwi_p (arg1
)
12584 && tree_to_uhwi (arg1
) < prec
12585 && tree_fits_uhwi_p (TREE_OPERAND (arg0
, 1))
12586 && tree_to_uhwi (TREE_OPERAND (arg0
, 1)) < prec
)
12588 HOST_WIDE_INT low0
= tree_to_uhwi (TREE_OPERAND (arg0
, 1));
12589 HOST_WIDE_INT low1
= tree_to_uhwi (arg1
);
12595 arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12597 lshift
= build_minus_one_cst (type
);
12598 lshift
= const_binop (code
, lshift
, arg1
);
12600 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
, arg00
, lshift
);
12604 /* Rewrite an LROTATE_EXPR by a constant into an
12605 RROTATE_EXPR by a new constant. */
12606 if (code
== LROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
)
12608 tree tem
= build_int_cst (TREE_TYPE (arg1
), prec
);
12609 tem
= const_binop (MINUS_EXPR
, tem
, arg1
);
12610 return fold_build2_loc (loc
, RROTATE_EXPR
, type
, op0
, tem
);
12613 /* If we have a rotate of a bit operation with the rotate count and
12614 the second operand of the bit operation both constant,
12615 permute the two operations. */
12616 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12617 && (TREE_CODE (arg0
) == BIT_AND_EXPR
12618 || TREE_CODE (arg0
) == BIT_IOR_EXPR
12619 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
12620 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12621 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
12622 fold_build2_loc (loc
, code
, type
,
12623 TREE_OPERAND (arg0
, 0), arg1
),
12624 fold_build2_loc (loc
, code
, type
,
12625 TREE_OPERAND (arg0
, 1), arg1
));
12627 /* Two consecutive rotates adding up to the some integer
12628 multiple of the precision of the type can be ignored. */
12629 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
12630 && TREE_CODE (arg0
) == RROTATE_EXPR
12631 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12632 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
12634 return TREE_OPERAND (arg0
, 0);
12636 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12637 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12638 if the latter can be further optimized. */
12639 if ((code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
12640 && TREE_CODE (arg0
) == BIT_AND_EXPR
12641 && TREE_CODE (arg1
) == INTEGER_CST
12642 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12644 tree mask
= fold_build2_loc (loc
, code
, type
,
12645 fold_convert_loc (loc
, type
,
12646 TREE_OPERAND (arg0
, 1)),
12648 tree shift
= fold_build2_loc (loc
, code
, type
,
12649 fold_convert_loc (loc
, type
,
12650 TREE_OPERAND (arg0
, 0)),
12652 tem
= fold_binary_loc (loc
, BIT_AND_EXPR
, type
, shift
, mask
);
12660 if (operand_equal_p (arg0
, arg1
, 0))
12661 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12662 if (INTEGRAL_TYPE_P (type
)
12663 && operand_equal_p (arg1
, TYPE_MIN_VALUE (type
), OEP_ONLY_CONST
))
12664 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12665 tem
= fold_minmax (loc
, MIN_EXPR
, type
, arg0
, arg1
);
12671 if (operand_equal_p (arg0
, arg1
, 0))
12672 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12673 if (INTEGRAL_TYPE_P (type
)
12674 && TYPE_MAX_VALUE (type
)
12675 && operand_equal_p (arg1
, TYPE_MAX_VALUE (type
), OEP_ONLY_CONST
))
12676 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12677 tem
= fold_minmax (loc
, MAX_EXPR
, type
, arg0
, arg1
);
12682 case TRUTH_ANDIF_EXPR
:
12683 /* Note that the operands of this must be ints
12684 and their values must be 0 or 1.
12685 ("true" is a fixed value perhaps depending on the language.) */
12686 /* If first arg is constant zero, return it. */
12687 if (integer_zerop (arg0
))
12688 return fold_convert_loc (loc
, type
, arg0
);
12689 case TRUTH_AND_EXPR
:
12690 /* If either arg is constant true, drop it. */
12691 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12692 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12693 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
12694 /* Preserve sequence points. */
12695 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12696 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12697 /* If second arg is constant zero, result is zero, but first arg
12698 must be evaluated. */
12699 if (integer_zerop (arg1
))
12700 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12701 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12702 case will be handled here. */
12703 if (integer_zerop (arg0
))
12704 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12706 /* !X && X is always false. */
12707 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12708 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12709 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
12710 /* X && !X is always false. */
12711 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12712 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12713 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12715 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12716 means A >= Y && A != MAX, but in this case we know that
12719 if (!TREE_SIDE_EFFECTS (arg0
)
12720 && !TREE_SIDE_EFFECTS (arg1
))
12722 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
12723 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
12724 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
12726 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
12727 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
12728 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
12731 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12737 case TRUTH_ORIF_EXPR
:
12738 /* Note that the operands of this must be ints
12739 and their values must be 0 or true.
12740 ("true" is a fixed value perhaps depending on the language.) */
12741 /* If first arg is constant true, return it. */
12742 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12743 return fold_convert_loc (loc
, type
, arg0
);
12744 case TRUTH_OR_EXPR
:
12745 /* If either arg is constant zero, drop it. */
12746 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
12747 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
12748 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
12749 /* Preserve sequence points. */
12750 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
12751 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12752 /* If second arg is constant true, result is true, but we must
12753 evaluate first arg. */
12754 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
12755 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
12756 /* Likewise for first arg, but note this only occurs here for
12758 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
12759 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
12761 /* !X || X is always true. */
12762 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12763 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12764 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12765 /* X || !X is always true. */
12766 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12767 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12768 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12770 /* (X && !Y) || (!X && Y) is X ^ Y */
12771 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12772 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12774 tree a0
, a1
, l0
, l1
, n0
, n1
;
12776 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12777 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12779 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12780 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12782 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12783 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12785 if ((operand_equal_p (n0
, a0
, 0)
12786 && operand_equal_p (n1
, a1
, 0))
12787 || (operand_equal_p (n0
, a1
, 0)
12788 && operand_equal_p (n1
, a0
, 0)))
12789 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12792 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12798 case TRUTH_XOR_EXPR
:
12799 /* If the second arg is constant zero, drop it. */
12800 if (integer_zerop (arg1
))
12801 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12802 /* If the second arg is constant true, this is a logical inversion. */
12803 if (integer_onep (arg1
))
12805 tem
= invert_truthvalue_loc (loc
, arg0
);
12806 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12808 /* Identical arguments cancel to zero. */
12809 if (operand_equal_p (arg0
, arg1
, 0))
12810 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12812 /* !X ^ X is always true. */
12813 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12814 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12815 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12817 /* X ^ !X is always true. */
12818 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12819 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12820 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12829 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12830 if (tem
!= NULL_TREE
)
12833 /* bool_var != 0 becomes bool_var. */
12834 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12835 && code
== NE_EXPR
)
12836 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12838 /* bool_var == 1 becomes bool_var. */
12839 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12840 && code
== EQ_EXPR
)
12841 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12843 /* bool_var != 1 becomes !bool_var. */
12844 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12845 && code
== NE_EXPR
)
12846 return fold_convert_loc (loc
, type
,
12847 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12848 TREE_TYPE (arg0
), arg0
));
12850 /* bool_var == 0 becomes !bool_var. */
12851 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12852 && code
== EQ_EXPR
)
12853 return fold_convert_loc (loc
, type
,
12854 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12855 TREE_TYPE (arg0
), arg0
));
12857 /* !exp != 0 becomes !exp */
12858 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12859 && code
== NE_EXPR
)
12860 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12862 /* If this is an equality comparison of the address of two non-weak,
12863 unaliased symbols neither of which are extern (since we do not
12864 have access to attributes for externs), then we know the result. */
12865 if (TREE_CODE (arg0
) == ADDR_EXPR
12866 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0
, 0))
12867 && ! DECL_WEAK (TREE_OPERAND (arg0
, 0))
12868 && ! lookup_attribute ("alias",
12869 DECL_ATTRIBUTES (TREE_OPERAND (arg0
, 0)))
12870 && ! DECL_EXTERNAL (TREE_OPERAND (arg0
, 0))
12871 && TREE_CODE (arg1
) == ADDR_EXPR
12872 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1
, 0))
12873 && ! DECL_WEAK (TREE_OPERAND (arg1
, 0))
12874 && ! lookup_attribute ("alias",
12875 DECL_ATTRIBUTES (TREE_OPERAND (arg1
, 0)))
12876 && ! DECL_EXTERNAL (TREE_OPERAND (arg1
, 0)))
12878 /* We know that we're looking at the address of two
12879 non-weak, unaliased, static _DECL nodes.
12881 It is both wasteful and incorrect to call operand_equal_p
12882 to compare the two ADDR_EXPR nodes. It is wasteful in that
12883 all we need to do is test pointer equality for the arguments
12884 to the two ADDR_EXPR nodes. It is incorrect to use
12885 operand_equal_p as that function is NOT equivalent to a
12886 C equality test. It can in fact return false for two
12887 objects which would test as equal using the C equality
12889 bool equal
= TREE_OPERAND (arg0
, 0) == TREE_OPERAND (arg1
, 0);
12890 return constant_boolean_node (equal
12891 ? code
== EQ_EXPR
: code
!= EQ_EXPR
,
12895 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12896 a MINUS_EXPR of a constant, we can convert it into a comparison with
12897 a revised constant as long as no overflow occurs. */
12898 if (TREE_CODE (arg1
) == INTEGER_CST
12899 && (TREE_CODE (arg0
) == PLUS_EXPR
12900 || TREE_CODE (arg0
) == MINUS_EXPR
)
12901 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12902 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
12903 ? MINUS_EXPR
: PLUS_EXPR
,
12904 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12906 TREE_OPERAND (arg0
, 1)))
12907 && !TREE_OVERFLOW (tem
))
12908 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
12910 /* Similarly for a NEGATE_EXPR. */
12911 if (TREE_CODE (arg0
) == NEGATE_EXPR
12912 && TREE_CODE (arg1
) == INTEGER_CST
12913 && 0 != (tem
= negate_expr (fold_convert_loc (loc
, TREE_TYPE (arg0
),
12915 && TREE_CODE (tem
) == INTEGER_CST
12916 && !TREE_OVERFLOW (tem
))
12917 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
12919 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12920 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12921 && TREE_CODE (arg1
) == INTEGER_CST
12922 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12923 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
12924 fold_build2_loc (loc
, BIT_XOR_EXPR
, TREE_TYPE (arg0
),
12925 fold_convert_loc (loc
,
12928 TREE_OPERAND (arg0
, 1)));
12930 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12931 if ((TREE_CODE (arg0
) == PLUS_EXPR
12932 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
12933 || TREE_CODE (arg0
) == MINUS_EXPR
)
12934 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
12937 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12938 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
12940 tree val
= TREE_OPERAND (arg0
, 1);
12941 return omit_two_operands_loc (loc
, type
,
12942 fold_build2_loc (loc
, code
, type
,
12944 build_int_cst (TREE_TYPE (val
),
12946 TREE_OPERAND (arg0
, 0), arg1
);
12949 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12950 if (TREE_CODE (arg0
) == MINUS_EXPR
12951 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
12952 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
12955 && wi::bit_and (TREE_OPERAND (arg0
, 0), 1) == 1)
12957 return omit_two_operands_loc (loc
, type
,
12959 ? boolean_true_node
: boolean_false_node
,
12960 TREE_OPERAND (arg0
, 1), arg1
);
12963 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12964 for !=. Don't do this for ordered comparisons due to overflow. */
12965 if (TREE_CODE (arg0
) == MINUS_EXPR
12966 && integer_zerop (arg1
))
12967 return fold_build2_loc (loc
, code
, type
,
12968 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
12970 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12971 if (TREE_CODE (arg0
) == ABS_EXPR
12972 && (integer_zerop (arg1
) || real_zerop (arg1
)))
12973 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), arg1
);
12975 /* If this is an EQ or NE comparison with zero and ARG0 is
12976 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12977 two operations, but the latter can be done in one less insn
12978 on machines that have only two-operand insns or on which a
12979 constant cannot be the first operand. */
12980 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12981 && integer_zerop (arg1
))
12983 tree arg00
= TREE_OPERAND (arg0
, 0);
12984 tree arg01
= TREE_OPERAND (arg0
, 1);
12985 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12986 && integer_onep (TREE_OPERAND (arg00
, 0)))
12988 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12989 arg01
, TREE_OPERAND (arg00
, 1));
12990 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12991 build_int_cst (TREE_TYPE (arg0
), 1));
12992 return fold_build2_loc (loc
, code
, type
,
12993 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
12996 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12997 && integer_onep (TREE_OPERAND (arg01
, 0)))
12999 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
13000 arg00
, TREE_OPERAND (arg01
, 1));
13001 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
13002 build_int_cst (TREE_TYPE (arg0
), 1));
13003 return fold_build2_loc (loc
, code
, type
,
13004 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
13009 /* If this is an NE or EQ comparison of zero against the result of a
13010 signed MOD operation whose second operand is a power of 2, make
13011 the MOD operation unsigned since it is simpler and equivalent. */
13012 if (integer_zerop (arg1
)
13013 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
13014 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
13015 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
13016 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
13017 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
13018 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
13020 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
13021 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
13022 fold_convert_loc (loc
, newtype
,
13023 TREE_OPERAND (arg0
, 0)),
13024 fold_convert_loc (loc
, newtype
,
13025 TREE_OPERAND (arg0
, 1)));
13027 return fold_build2_loc (loc
, code
, type
, newmod
,
13028 fold_convert_loc (loc
, newtype
, arg1
));
13031 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
13032 C1 is a valid shift constant, and C2 is a power of two, i.e.
13034 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13035 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
13036 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
13038 && integer_pow2p (TREE_OPERAND (arg0
, 1))
13039 && integer_zerop (arg1
))
13041 tree itype
= TREE_TYPE (arg0
);
13042 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
13043 prec
= TYPE_PRECISION (itype
);
13045 /* Check for a valid shift count. */
13046 if (wi::ltu_p (arg001
, prec
))
13048 tree arg01
= TREE_OPERAND (arg0
, 1);
13049 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
13050 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
13051 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
13052 can be rewritten as (X & (C2 << C1)) != 0. */
13053 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
13055 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
13056 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
13057 return fold_build2_loc (loc
, code
, type
, tem
,
13058 fold_convert_loc (loc
, itype
, arg1
));
13060 /* Otherwise, for signed (arithmetic) shifts,
13061 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
13062 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
13063 else if (!TYPE_UNSIGNED (itype
))
13064 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
13065 arg000
, build_int_cst (itype
, 0));
13066 /* Otherwise, of unsigned (logical) shifts,
13067 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
13068 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
13070 return omit_one_operand_loc (loc
, type
,
13071 code
== EQ_EXPR
? integer_one_node
13072 : integer_zero_node
,
13077 /* If we have (A & C) == C where C is a power of 2, convert this into
13078 (A & C) != 0. Similarly for NE_EXPR. */
13079 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13080 && integer_pow2p (TREE_OPERAND (arg0
, 1))
13081 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
13082 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
13083 arg0
, fold_convert_loc (loc
, TREE_TYPE (arg0
),
13084 integer_zero_node
));
13086 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
13087 bit, then fold the expression into A < 0 or A >= 0. */
13088 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
, type
);
13092 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
13093 Similarly for NE_EXPR. */
13094 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13095 && TREE_CODE (arg1
) == INTEGER_CST
13096 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
13098 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
13099 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
13100 TREE_OPERAND (arg0
, 1));
13102 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
13103 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
13105 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
13106 if (integer_nonzerop (dandnotc
))
13107 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
13110 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
13111 Similarly for NE_EXPR. */
13112 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
13113 && TREE_CODE (arg1
) == INTEGER_CST
13114 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
13116 tree notd
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg1
), arg1
);
13118 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
13119 TREE_OPERAND (arg0
, 1),
13120 fold_convert_loc (loc
, TREE_TYPE (arg0
), notd
));
13121 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
13122 if (integer_nonzerop (candnotd
))
13123 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
13126 /* If this is a comparison of a field, we may be able to simplify it. */
13127 if ((TREE_CODE (arg0
) == COMPONENT_REF
13128 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
13129 /* Handle the constant case even without -O
13130 to make sure the warnings are given. */
13131 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
13133 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
13138 /* Optimize comparisons of strlen vs zero to a compare of the
13139 first character of the string vs zero. To wit,
13140 strlen(ptr) == 0 => *ptr == 0
13141 strlen(ptr) != 0 => *ptr != 0
13142 Other cases should reduce to one of these two (or a constant)
13143 due to the return value of strlen being unsigned. */
13144 if (TREE_CODE (arg0
) == CALL_EXPR
13145 && integer_zerop (arg1
))
13147 tree fndecl
= get_callee_fndecl (arg0
);
13150 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
13151 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
13152 && call_expr_nargs (arg0
) == 1
13153 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
13155 tree iref
= build_fold_indirect_ref_loc (loc
,
13156 CALL_EXPR_ARG (arg0
, 0));
13157 return fold_build2_loc (loc
, code
, type
, iref
,
13158 build_int_cst (TREE_TYPE (iref
), 0));
13162 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
13163 of X. Similarly fold (X >> C) == 0 into X >= 0. */
13164 if (TREE_CODE (arg0
) == RSHIFT_EXPR
13165 && integer_zerop (arg1
)
13166 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
13168 tree arg00
= TREE_OPERAND (arg0
, 0);
13169 tree arg01
= TREE_OPERAND (arg0
, 1);
13170 tree itype
= TREE_TYPE (arg00
);
13171 if (wi::eq_p (arg01
, TYPE_PRECISION (itype
) - 1))
13173 if (TYPE_UNSIGNED (itype
))
13175 itype
= signed_type_for (itype
);
13176 arg00
= fold_convert_loc (loc
, itype
, arg00
);
13178 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
13179 type
, arg00
, build_zero_cst (itype
));
13183 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
13184 if (integer_zerop (arg1
)
13185 && TREE_CODE (arg0
) == BIT_XOR_EXPR
)
13186 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
13187 TREE_OPERAND (arg0
, 1));
13189 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
13190 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
13191 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
13192 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
13193 build_zero_cst (TREE_TYPE (arg0
)));
13194 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
13195 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
13196 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
13197 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
13198 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 1),
13199 build_zero_cst (TREE_TYPE (arg0
)));
13201 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
13202 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
13203 && TREE_CODE (arg1
) == INTEGER_CST
13204 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
13205 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
13206 fold_build2_loc (loc
, BIT_XOR_EXPR
, TREE_TYPE (arg1
),
13207 TREE_OPERAND (arg0
, 1), arg1
));
13209 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
13210 (X & C) == 0 when C is a single bit. */
13211 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13212 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
13213 && integer_zerop (arg1
)
13214 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
13216 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
13217 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
13218 TREE_OPERAND (arg0
, 1));
13219 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
13221 fold_convert_loc (loc
, TREE_TYPE (arg0
),
13225 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13226 constant C is a power of two, i.e. a single bit. */
13227 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
13228 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13229 && integer_zerop (arg1
)
13230 && integer_pow2p (TREE_OPERAND (arg0
, 1))
13231 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13232 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
13234 tree arg00
= TREE_OPERAND (arg0
, 0);
13235 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
13236 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
13239 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13240 when is C is a power of two, i.e. a single bit. */
13241 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13242 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
13243 && integer_zerop (arg1
)
13244 && integer_pow2p (TREE_OPERAND (arg0
, 1))
13245 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13246 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
13248 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
13249 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
13250 arg000
, TREE_OPERAND (arg0
, 1));
13251 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
13252 tem
, build_int_cst (TREE_TYPE (tem
), 0));
13255 if (integer_zerop (arg1
)
13256 && tree_expr_nonzero_p (arg0
))
13258 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
13259 return omit_one_operand_loc (loc
, type
, res
, arg0
);
13262 /* Fold -X op -Y as X op Y, where op is eq/ne. */
13263 if (TREE_CODE (arg0
) == NEGATE_EXPR
13264 && TREE_CODE (arg1
) == NEGATE_EXPR
)
13265 return fold_build2_loc (loc
, code
, type
,
13266 TREE_OPERAND (arg0
, 0),
13267 fold_convert_loc (loc
, TREE_TYPE (arg0
),
13268 TREE_OPERAND (arg1
, 0)));
13270 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
13271 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13272 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
13274 tree arg00
= TREE_OPERAND (arg0
, 0);
13275 tree arg01
= TREE_OPERAND (arg0
, 1);
13276 tree arg10
= TREE_OPERAND (arg1
, 0);
13277 tree arg11
= TREE_OPERAND (arg1
, 1);
13278 tree itype
= TREE_TYPE (arg0
);
13280 if (operand_equal_p (arg01
, arg11
, 0))
13281 return fold_build2_loc (loc
, code
, type
,
13282 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
13283 fold_build2_loc (loc
,
13284 BIT_XOR_EXPR
, itype
,
13287 build_zero_cst (itype
));
13289 if (operand_equal_p (arg01
, arg10
, 0))
13290 return fold_build2_loc (loc
, code
, type
,
13291 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
13292 fold_build2_loc (loc
,
13293 BIT_XOR_EXPR
, itype
,
13296 build_zero_cst (itype
));
13298 if (operand_equal_p (arg00
, arg11
, 0))
13299 return fold_build2_loc (loc
, code
, type
,
13300 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
13301 fold_build2_loc (loc
,
13302 BIT_XOR_EXPR
, itype
,
13305 build_zero_cst (itype
));
13307 if (operand_equal_p (arg00
, arg10
, 0))
13308 return fold_build2_loc (loc
, code
, type
,
13309 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
13310 fold_build2_loc (loc
,
13311 BIT_XOR_EXPR
, itype
,
13314 build_zero_cst (itype
));
13317 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
13318 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
13320 tree arg00
= TREE_OPERAND (arg0
, 0);
13321 tree arg01
= TREE_OPERAND (arg0
, 1);
13322 tree arg10
= TREE_OPERAND (arg1
, 0);
13323 tree arg11
= TREE_OPERAND (arg1
, 1);
13324 tree itype
= TREE_TYPE (arg0
);
13326 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13327 operand_equal_p guarantees no side-effects so we don't need
13328 to use omit_one_operand on Z. */
13329 if (operand_equal_p (arg01
, arg11
, 0))
13330 return fold_build2_loc (loc
, code
, type
, arg00
,
13331 fold_convert_loc (loc
, TREE_TYPE (arg00
),
13333 if (operand_equal_p (arg01
, arg10
, 0))
13334 return fold_build2_loc (loc
, code
, type
, arg00
,
13335 fold_convert_loc (loc
, TREE_TYPE (arg00
),
13337 if (operand_equal_p (arg00
, arg11
, 0))
13338 return fold_build2_loc (loc
, code
, type
, arg01
,
13339 fold_convert_loc (loc
, TREE_TYPE (arg01
),
13341 if (operand_equal_p (arg00
, arg10
, 0))
13342 return fold_build2_loc (loc
, code
, type
, arg01
,
13343 fold_convert_loc (loc
, TREE_TYPE (arg01
),
13346 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
13347 if (TREE_CODE (arg01
) == INTEGER_CST
13348 && TREE_CODE (arg11
) == INTEGER_CST
)
13350 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
13351 fold_convert_loc (loc
, itype
, arg11
));
13352 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
13353 return fold_build2_loc (loc
, code
, type
, tem
,
13354 fold_convert_loc (loc
, itype
, arg10
));
13358 /* Attempt to simplify equality/inequality comparisons of complex
13359 values. Only lower the comparison if the result is known or
13360 can be simplified to a single scalar comparison. */
13361 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
13362 || TREE_CODE (arg0
) == COMPLEX_CST
)
13363 && (TREE_CODE (arg1
) == COMPLEX_EXPR
13364 || TREE_CODE (arg1
) == COMPLEX_CST
))
13366 tree real0
, imag0
, real1
, imag1
;
13369 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
13371 real0
= TREE_OPERAND (arg0
, 0);
13372 imag0
= TREE_OPERAND (arg0
, 1);
13376 real0
= TREE_REALPART (arg0
);
13377 imag0
= TREE_IMAGPART (arg0
);
13380 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
13382 real1
= TREE_OPERAND (arg1
, 0);
13383 imag1
= TREE_OPERAND (arg1
, 1);
13387 real1
= TREE_REALPART (arg1
);
13388 imag1
= TREE_IMAGPART (arg1
);
13391 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
13392 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
13394 if (integer_zerop (rcond
))
13396 if (code
== EQ_EXPR
)
13397 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
13399 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
13403 if (code
== NE_EXPR
)
13404 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
13406 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
13410 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
13411 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
13413 if (integer_zerop (icond
))
13415 if (code
== EQ_EXPR
)
13416 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
13418 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
13422 if (code
== NE_EXPR
)
13423 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
13425 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
13436 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
13437 if (tem
!= NULL_TREE
)
13440 /* Transform comparisons of the form X +- C CMP X. */
13441 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
13442 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
13443 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
13444 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
))))
13445 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
13446 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
13448 tree arg01
= TREE_OPERAND (arg0
, 1);
13449 enum tree_code code0
= TREE_CODE (arg0
);
13452 if (TREE_CODE (arg01
) == REAL_CST
)
13453 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
13455 is_positive
= tree_int_cst_sgn (arg01
);
13457 /* (X - c) > X becomes false. */
13458 if (code
== GT_EXPR
13459 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
13460 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
13462 if (TREE_CODE (arg01
) == INTEGER_CST
13463 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13464 fold_overflow_warning (("assuming signed overflow does not "
13465 "occur when assuming that (X - c) > X "
13466 "is always false"),
13467 WARN_STRICT_OVERFLOW_ALL
);
13468 return constant_boolean_node (0, type
);
13471 /* Likewise (X + c) < X becomes false. */
13472 if (code
== LT_EXPR
13473 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
13474 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
13476 if (TREE_CODE (arg01
) == INTEGER_CST
13477 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13478 fold_overflow_warning (("assuming signed overflow does not "
13479 "occur when assuming that "
13480 "(X + c) < X is always false"),
13481 WARN_STRICT_OVERFLOW_ALL
);
13482 return constant_boolean_node (0, type
);
13485 /* Convert (X - c) <= X to true. */
13486 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
)))
13488 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
13489 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
13491 if (TREE_CODE (arg01
) == INTEGER_CST
13492 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13493 fold_overflow_warning (("assuming signed overflow does not "
13494 "occur when assuming that "
13495 "(X - c) <= X is always true"),
13496 WARN_STRICT_OVERFLOW_ALL
);
13497 return constant_boolean_node (1, type
);
13500 /* Convert (X + c) >= X to true. */
13501 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1
)))
13503 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
13504 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
13506 if (TREE_CODE (arg01
) == INTEGER_CST
13507 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13508 fold_overflow_warning (("assuming signed overflow does not "
13509 "occur when assuming that "
13510 "(X + c) >= X is always true"),
13511 WARN_STRICT_OVERFLOW_ALL
);
13512 return constant_boolean_node (1, type
);
13515 if (TREE_CODE (arg01
) == INTEGER_CST
)
13517 /* Convert X + c > X and X - c < X to true for integers. */
13518 if (code
== GT_EXPR
13519 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
13520 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
13522 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13523 fold_overflow_warning (("assuming signed overflow does "
13524 "not occur when assuming that "
13525 "(X + c) > X is always true"),
13526 WARN_STRICT_OVERFLOW_ALL
);
13527 return constant_boolean_node (1, type
);
13530 if (code
== LT_EXPR
13531 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
13532 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
13534 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13535 fold_overflow_warning (("assuming signed overflow does "
13536 "not occur when assuming that "
13537 "(X - c) < X is always true"),
13538 WARN_STRICT_OVERFLOW_ALL
);
13539 return constant_boolean_node (1, type
);
13542 /* Convert X + c <= X and X - c >= X to false for integers. */
13543 if (code
== LE_EXPR
13544 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
13545 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
13547 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13548 fold_overflow_warning (("assuming signed overflow does "
13549 "not occur when assuming that "
13550 "(X + c) <= X is always false"),
13551 WARN_STRICT_OVERFLOW_ALL
);
13552 return constant_boolean_node (0, type
);
13555 if (code
== GE_EXPR
13556 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
13557 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
13559 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
13560 fold_overflow_warning (("assuming signed overflow does "
13561 "not occur when assuming that "
13562 "(X - c) >= X is always false"),
13563 WARN_STRICT_OVERFLOW_ALL
);
13564 return constant_boolean_node (0, type
);
13569 /* Comparisons with the highest or lowest possible integer of
13570 the specified precision will have known values. */
13572 tree arg1_type
= TREE_TYPE (arg1
);
13573 unsigned int prec
= TYPE_PRECISION (arg1_type
);
13575 if (TREE_CODE (arg1
) == INTEGER_CST
13576 && (INTEGRAL_TYPE_P (arg1_type
) || POINTER_TYPE_P (arg1_type
)))
13578 wide_int max
= wi::max_value (arg1_type
);
13579 wide_int signed_max
= wi::max_value (prec
, SIGNED
);
13580 wide_int min
= wi::min_value (arg1_type
);
13582 if (wi::eq_p (arg1
, max
))
13586 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
13589 return fold_build2_loc (loc
, EQ_EXPR
, type
, op0
, op1
);
13592 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
13595 return fold_build2_loc (loc
, NE_EXPR
, type
, op0
, op1
);
13597 /* The GE_EXPR and LT_EXPR cases above are not normally
13598 reached because of previous transformations. */
13603 else if (wi::eq_p (arg1
, max
- 1))
13607 arg1
= const_binop (PLUS_EXPR
, arg1
,
13608 build_int_cst (TREE_TYPE (arg1
), 1));
13609 return fold_build2_loc (loc
, EQ_EXPR
, type
,
13610 fold_convert_loc (loc
,
13611 TREE_TYPE (arg1
), arg0
),
13614 arg1
= const_binop (PLUS_EXPR
, arg1
,
13615 build_int_cst (TREE_TYPE (arg1
), 1));
13616 return fold_build2_loc (loc
, NE_EXPR
, type
,
13617 fold_convert_loc (loc
, TREE_TYPE (arg1
),
13623 else if (wi::eq_p (arg1
, min
))
13627 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
13630 return fold_build2_loc (loc
, EQ_EXPR
, type
, op0
, op1
);
13633 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
13636 return fold_build2_loc (loc
, NE_EXPR
, type
, op0
, op1
);
13641 else if (wi::eq_p (arg1
, min
+ 1))
13645 arg1
= const_binop (MINUS_EXPR
, arg1
,
13646 build_int_cst (TREE_TYPE (arg1
), 1));
13647 return fold_build2_loc (loc
, NE_EXPR
, type
,
13648 fold_convert_loc (loc
,
13649 TREE_TYPE (arg1
), arg0
),
13652 arg1
= const_binop (MINUS_EXPR
, arg1
,
13653 build_int_cst (TREE_TYPE (arg1
), 1));
13654 return fold_build2_loc (loc
, EQ_EXPR
, type
,
13655 fold_convert_loc (loc
, TREE_TYPE (arg1
),
13662 else if (wi::eq_p (arg1
, signed_max
)
13663 && TYPE_UNSIGNED (arg1_type
)
13664 /* We will flip the signedness of the comparison operator
13665 associated with the mode of arg1, so the sign bit is
13666 specified by this mode. Check that arg1 is the signed
13667 max associated with this sign bit. */
13668 && prec
== GET_MODE_PRECISION (TYPE_MODE (arg1_type
))
13669 /* signed_type does not work on pointer types. */
13670 && INTEGRAL_TYPE_P (arg1_type
))
13672 /* The following case also applies to X < signed_max+1
13673 and X >= signed_max+1 because previous transformations. */
13674 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13676 tree st
= signed_type_for (arg1_type
);
13677 return fold_build2_loc (loc
,
13678 code
== LE_EXPR
? GE_EXPR
: LT_EXPR
,
13679 type
, fold_convert_loc (loc
, st
, arg0
),
13680 build_int_cst (st
, 0));
13686 /* If we are comparing an ABS_EXPR with a constant, we can
13687 convert all the cases into explicit comparisons, but they may
13688 well not be faster than doing the ABS and one comparison.
13689 But ABS (X) <= C is a range comparison, which becomes a subtraction
13690 and a comparison, and is probably faster. */
13691 if (code
== LE_EXPR
13692 && TREE_CODE (arg1
) == INTEGER_CST
13693 && TREE_CODE (arg0
) == ABS_EXPR
13694 && ! TREE_SIDE_EFFECTS (arg0
)
13695 && (0 != (tem
= negate_expr (arg1
)))
13696 && TREE_CODE (tem
) == INTEGER_CST
13697 && !TREE_OVERFLOW (tem
))
13698 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
13699 build2 (GE_EXPR
, type
,
13700 TREE_OPERAND (arg0
, 0), tem
),
13701 build2 (LE_EXPR
, type
,
13702 TREE_OPERAND (arg0
, 0), arg1
));
13704 /* Convert ABS_EXPR<x> >= 0 to true. */
13705 strict_overflow_p
= false;
13706 if (code
== GE_EXPR
13707 && (integer_zerop (arg1
)
13708 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
)))
13709 && real_zerop (arg1
)))
13710 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
13712 if (strict_overflow_p
)
13713 fold_overflow_warning (("assuming signed overflow does not occur "
13714 "when simplifying comparison of "
13715 "absolute value and zero"),
13716 WARN_STRICT_OVERFLOW_CONDITIONAL
);
13717 return omit_one_operand_loc (loc
, type
,
13718 constant_boolean_node (true, type
),
13722 /* Convert ABS_EXPR<x> < 0 to false. */
13723 strict_overflow_p
= false;
13724 if (code
== LT_EXPR
13725 && (integer_zerop (arg1
) || real_zerop (arg1
))
13726 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
13728 if (strict_overflow_p
)
13729 fold_overflow_warning (("assuming signed overflow does not occur "
13730 "when simplifying comparison of "
13731 "absolute value and zero"),
13732 WARN_STRICT_OVERFLOW_CONDITIONAL
);
13733 return omit_one_operand_loc (loc
, type
,
13734 constant_boolean_node (false, type
),
13738 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13739 and similarly for >= into !=. */
13740 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
13741 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
13742 && TREE_CODE (arg1
) == LSHIFT_EXPR
13743 && integer_onep (TREE_OPERAND (arg1
, 0)))
13744 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
13745 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
13746 TREE_OPERAND (arg1
, 1)),
13747 build_zero_cst (TREE_TYPE (arg0
)));
13749 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
13750 otherwise Y might be >= # of bits in X's type and thus e.g.
13751 (unsigned char) (1 << Y) for Y 15 might be 0.
13752 If the cast is widening, then 1 << Y should have unsigned type,
13753 otherwise if Y is number of bits in the signed shift type minus 1,
13754 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
13755 31 might be 0xffffffff80000000. */
13756 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
13757 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
13758 && CONVERT_EXPR_P (arg1
)
13759 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
13760 && (TYPE_PRECISION (TREE_TYPE (arg1
))
13761 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
13762 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
13763 || (TYPE_PRECISION (TREE_TYPE (arg1
))
13764 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
13765 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
13767 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
13768 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
13769 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
13770 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
13771 build_zero_cst (TREE_TYPE (arg0
)));
13776 case UNORDERED_EXPR
:
13784 if (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
13786 t1
= fold_relational_const (code
, type
, arg0
, arg1
);
13787 if (t1
!= NULL_TREE
)
13791 /* If the first operand is NaN, the result is constant. */
13792 if (TREE_CODE (arg0
) == REAL_CST
13793 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
))
13794 && (code
!= LTGT_EXPR
|| ! flag_trapping_math
))
13796 t1
= (code
== ORDERED_EXPR
|| code
== LTGT_EXPR
)
13797 ? integer_zero_node
13798 : integer_one_node
;
13799 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
13802 /* If the second operand is NaN, the result is constant. */
13803 if (TREE_CODE (arg1
) == REAL_CST
13804 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
13805 && (code
!= LTGT_EXPR
|| ! flag_trapping_math
))
13807 t1
= (code
== ORDERED_EXPR
|| code
== LTGT_EXPR
)
13808 ? integer_zero_node
13809 : integer_one_node
;
13810 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
13813 /* Simplify unordered comparison of something with itself. */
13814 if ((code
== UNLE_EXPR
|| code
== UNGE_EXPR
|| code
== UNEQ_EXPR
)
13815 && operand_equal_p (arg0
, arg1
, 0))
13816 return constant_boolean_node (1, type
);
13818 if (code
== LTGT_EXPR
13819 && !flag_trapping_math
13820 && operand_equal_p (arg0
, arg1
, 0))
13821 return constant_boolean_node (0, type
);
13823 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13825 tree targ0
= strip_float_extensions (arg0
);
13826 tree targ1
= strip_float_extensions (arg1
);
13827 tree newtype
= TREE_TYPE (targ0
);
13829 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
13830 newtype
= TREE_TYPE (targ1
);
13832 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
13833 return fold_build2_loc (loc
, code
, type
,
13834 fold_convert_loc (loc
, newtype
, targ0
),
13835 fold_convert_loc (loc
, newtype
, targ1
));
13840 case COMPOUND_EXPR
:
13841 /* When pedantic, a compound expression can be neither an lvalue
13842 nor an integer constant expression. */
13843 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
13845 /* Don't let (0, 0) be null pointer constant. */
13846 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
13847 : fold_convert_loc (loc
, type
, arg1
);
13848 return pedantic_non_lvalue_loc (loc
, tem
);
13851 if ((TREE_CODE (arg0
) == REAL_CST
13852 && TREE_CODE (arg1
) == REAL_CST
)
13853 || (TREE_CODE (arg0
) == INTEGER_CST
13854 && TREE_CODE (arg1
) == INTEGER_CST
))
13855 return build_complex (type
, arg0
, arg1
);
13856 if (TREE_CODE (arg0
) == REALPART_EXPR
13857 && TREE_CODE (arg1
) == IMAGPART_EXPR
13858 && TREE_TYPE (TREE_OPERAND (arg0
, 0)) == type
13859 && operand_equal_p (TREE_OPERAND (arg0
, 0),
13860 TREE_OPERAND (arg1
, 0), 0))
13861 return omit_one_operand_loc (loc
, type
, TREE_OPERAND (arg0
, 0),
13862 TREE_OPERAND (arg1
, 0));
13866 /* An ASSERT_EXPR should never be passed to fold_binary. */
13867 gcc_unreachable ();
13869 case VEC_PACK_TRUNC_EXPR
:
13870 case VEC_PACK_FIX_TRUNC_EXPR
:
13872 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
13875 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
/ 2
13876 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2);
13877 if (TREE_CODE (arg0
) != VECTOR_CST
|| TREE_CODE (arg1
) != VECTOR_CST
)
13880 elts
= XALLOCAVEC (tree
, nelts
);
13881 if (!vec_cst_ctor_to_array (arg0
, elts
)
13882 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
/ 2))
13885 for (i
= 0; i
< nelts
; i
++)
13887 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
13888 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
13889 TREE_TYPE (type
), elts
[i
]);
13890 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
13894 return build_vector (type
, elts
);
13897 case VEC_WIDEN_MULT_LO_EXPR
:
13898 case VEC_WIDEN_MULT_HI_EXPR
:
13899 case VEC_WIDEN_MULT_EVEN_EXPR
:
13900 case VEC_WIDEN_MULT_ODD_EXPR
:
13902 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
13903 unsigned int out
, ofs
, scale
;
13906 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2
13907 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2);
13908 if (TREE_CODE (arg0
) != VECTOR_CST
|| TREE_CODE (arg1
) != VECTOR_CST
)
13911 elts
= XALLOCAVEC (tree
, nelts
* 4);
13912 if (!vec_cst_ctor_to_array (arg0
, elts
)
13913 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
* 2))
13916 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
13917 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
13918 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
13919 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
13920 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
13921 scale
= 1, ofs
= 0;
13922 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
13923 scale
= 1, ofs
= 1;
13925 for (out
= 0; out
< nelts
; out
++)
13927 unsigned int in1
= (out
<< scale
) + ofs
;
13928 unsigned int in2
= in1
+ nelts
* 2;
13931 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
13932 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
13934 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
13936 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
13937 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
13941 return build_vector (type
, elts
);
13946 } /* switch (code) */
13949 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13950 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13954 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
13956 switch (TREE_CODE (*tp
))
13962 *walk_subtrees
= 0;
13964 /* ... fall through ... */
13971 /* Return whether the sub-tree ST contains a label which is accessible from
13972 outside the sub-tree. */
13975 contains_label_p (tree st
)
13978 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
13981 /* Fold a ternary expression of code CODE and type TYPE with operands
13982 OP0, OP1, and OP2. Return the folded expression if folding is
13983 successful. Otherwise, return NULL_TREE. */
13986 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
13987 tree op0
, tree op1
, tree op2
)
13990 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
13991 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13993 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
13994 && TREE_CODE_LENGTH (code
) == 3);
13996 /* Strip any conversions that don't change the mode. This is safe
13997 for every expression, except for a comparison expression because
13998 its signedness is derived from its operands. So, in the latter
13999 case, only strip conversions that don't change the signedness.
14001 Note that this is done as an internal manipulation within the
14002 constant folder, in order to find the simplest representation of
14003 the arguments so that their form can be studied. In any cases,
14004 the appropriate type conversions should be put back in the tree
14005 that will get out of the constant folder. */
14026 case COMPONENT_REF
:
14027 if (TREE_CODE (arg0
) == CONSTRUCTOR
14028 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
14030 unsigned HOST_WIDE_INT idx
;
14032 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
14039 case VEC_COND_EXPR
:
14040 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
14041 so all simple results must be passed through pedantic_non_lvalue. */
14042 if (TREE_CODE (arg0
) == INTEGER_CST
)
14044 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
14045 tem
= integer_zerop (arg0
) ? op2
: op1
;
14046 /* Only optimize constant conditions when the selected branch
14047 has the same type as the COND_EXPR. This avoids optimizing
14048 away "c ? x : throw", where the throw has a void type.
14049 Avoid throwing away that operand which contains label. */
14050 if ((!TREE_SIDE_EFFECTS (unused_op
)
14051 || !contains_label_p (unused_op
))
14052 && (! VOID_TYPE_P (TREE_TYPE (tem
))
14053 || VOID_TYPE_P (type
)))
14054 return pedantic_non_lvalue_loc (loc
, tem
);
14057 else if (TREE_CODE (arg0
) == VECTOR_CST
)
14059 if (integer_all_onesp (arg0
))
14060 return pedantic_omit_one_operand_loc (loc
, type
, arg1
, arg2
);
14061 if (integer_zerop (arg0
))
14062 return pedantic_omit_one_operand_loc (loc
, type
, arg2
, arg1
);
14064 if ((TREE_CODE (arg1
) == VECTOR_CST
14065 || TREE_CODE (arg1
) == CONSTRUCTOR
)
14066 && (TREE_CODE (arg2
) == VECTOR_CST
14067 || TREE_CODE (arg2
) == CONSTRUCTOR
))
14069 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
14070 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
14071 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
14072 for (i
= 0; i
< nelts
; i
++)
14074 tree val
= VECTOR_CST_ELT (arg0
, i
);
14075 if (integer_all_onesp (val
))
14077 else if (integer_zerop (val
))
14078 sel
[i
] = nelts
+ i
;
14079 else /* Currently unreachable. */
14082 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
14083 if (t
!= NULL_TREE
)
14088 if (operand_equal_p (arg1
, op2
, 0))
14089 return pedantic_omit_one_operand_loc (loc
, type
, arg1
, arg0
);
14091 /* If we have A op B ? A : C, we may be able to convert this to a
14092 simpler expression, depending on the operation and the values
14093 of B and C. Signed zeros prevent all of these transformations,
14094 for reasons given above each one.
14096 Also try swapping the arguments and inverting the conditional. */
14097 if (COMPARISON_CLASS_P (arg0
)
14098 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
14099 arg1
, TREE_OPERAND (arg0
, 1))
14100 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1
))))
14102 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
14107 if (COMPARISON_CLASS_P (arg0
)
14108 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
14110 TREE_OPERAND (arg0
, 1))
14111 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2
))))
14113 location_t loc0
= expr_location_or (arg0
, loc
);
14114 tem
= fold_invert_truthvalue (loc0
, arg0
);
14115 if (tem
&& COMPARISON_CLASS_P (tem
))
14117 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
14123 /* If the second operand is simpler than the third, swap them
14124 since that produces better jump optimization results. */
14125 if (truth_value_p (TREE_CODE (arg0
))
14126 && tree_swap_operands_p (op1
, op2
, false))
14128 location_t loc0
= expr_location_or (arg0
, loc
);
14129 /* See if this can be inverted. If it can't, possibly because
14130 it was a floating-point inequality comparison, don't do
14132 tem
= fold_invert_truthvalue (loc0
, arg0
);
14134 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
14137 /* Convert A ? 1 : 0 to simply A. */
14138 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
14139 : (integer_onep (op1
)
14140 && !VECTOR_TYPE_P (type
)))
14141 && integer_zerop (op2
)
14142 /* If we try to convert OP0 to our type, the
14143 call to fold will try to move the conversion inside
14144 a COND, which will recurse. In that case, the COND_EXPR
14145 is probably the best choice, so leave it alone. */
14146 && type
== TREE_TYPE (arg0
))
14147 return pedantic_non_lvalue_loc (loc
, arg0
);
14149 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
14150 over COND_EXPR in cases such as floating point comparisons. */
14151 if (integer_zerop (op1
)
14152 && (code
== VEC_COND_EXPR
? integer_all_onesp (op2
)
14153 : (integer_onep (op2
)
14154 && !VECTOR_TYPE_P (type
)))
14155 && truth_value_p (TREE_CODE (arg0
)))
14156 return pedantic_non_lvalue_loc (loc
,
14157 fold_convert_loc (loc
, type
,
14158 invert_truthvalue_loc (loc
,
14161 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
14162 if (TREE_CODE (arg0
) == LT_EXPR
14163 && integer_zerop (TREE_OPERAND (arg0
, 1))
14164 && integer_zerop (op2
)
14165 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
14167 /* sign_bit_p looks through both zero and sign extensions,
14168 but for this optimization only sign extensions are
14170 tree tem2
= TREE_OPERAND (arg0
, 0);
14171 while (tem
!= tem2
)
14173 if (TREE_CODE (tem2
) != NOP_EXPR
14174 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
14179 tem2
= TREE_OPERAND (tem2
, 0);
14181 /* sign_bit_p only checks ARG1 bits within A's precision.
14182 If <sign bit of A> has wider type than A, bits outside
14183 of A's precision in <sign bit of A> need to be checked.
14184 If they are all 0, this optimization needs to be done
14185 in unsigned A's type, if they are all 1 in signed A's type,
14186 otherwise this can't be done. */
14188 && TYPE_PRECISION (TREE_TYPE (tem
))
14189 < TYPE_PRECISION (TREE_TYPE (arg1
))
14190 && TYPE_PRECISION (TREE_TYPE (tem
))
14191 < TYPE_PRECISION (type
))
14193 int inner_width
, outer_width
;
14196 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
14197 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
14198 if (outer_width
> TYPE_PRECISION (type
))
14199 outer_width
= TYPE_PRECISION (type
);
14201 wide_int mask
= wi::shifted_mask
14202 (inner_width
, outer_width
- inner_width
, false,
14203 TYPE_PRECISION (TREE_TYPE (arg1
)));
14205 wide_int common
= mask
& arg1
;
14206 if (common
== mask
)
14208 tem_type
= signed_type_for (TREE_TYPE (tem
));
14209 tem
= fold_convert_loc (loc
, tem_type
, tem
);
14211 else if (common
== 0)
14213 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
14214 tem
= fold_convert_loc (loc
, tem_type
, tem
);
14222 fold_convert_loc (loc
, type
,
14223 fold_build2_loc (loc
, BIT_AND_EXPR
,
14224 TREE_TYPE (tem
), tem
,
14225 fold_convert_loc (loc
,
14230 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
14231 already handled above. */
14232 if (TREE_CODE (arg0
) == BIT_AND_EXPR
14233 && integer_onep (TREE_OPERAND (arg0
, 1))
14234 && integer_zerop (op2
)
14235 && integer_pow2p (arg1
))
14237 tree tem
= TREE_OPERAND (arg0
, 0);
14239 if (TREE_CODE (tem
) == RSHIFT_EXPR
14240 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
14241 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
14242 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
14243 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
14244 TREE_OPERAND (tem
, 0), arg1
);
14247 /* A & N ? N : 0 is simply A & N if N is a power of two. This
14248 is probably obsolete because the first operand should be a
14249 truth value (that's why we have the two cases above), but let's
14250 leave it in until we can confirm this for all front-ends. */
14251 if (integer_zerop (op2
)
14252 && TREE_CODE (arg0
) == NE_EXPR
14253 && integer_zerop (TREE_OPERAND (arg0
, 1))
14254 && integer_pow2p (arg1
)
14255 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
14256 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
14257 arg1
, OEP_ONLY_CONST
))
14258 return pedantic_non_lvalue_loc (loc
,
14259 fold_convert_loc (loc
, type
,
14260 TREE_OPERAND (arg0
, 0)));
14262 /* Disable the transformations below for vectors, since
14263 fold_binary_op_with_conditional_arg may undo them immediately,
14264 yielding an infinite loop. */
14265 if (code
== VEC_COND_EXPR
)
14268 /* Convert A ? B : 0 into A && B if A and B are truth values. */
14269 if (integer_zerop (op2
)
14270 && truth_value_p (TREE_CODE (arg0
))
14271 && truth_value_p (TREE_CODE (arg1
))
14272 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
14273 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
14274 : TRUTH_ANDIF_EXPR
,
14275 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
14277 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
14278 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
14279 && truth_value_p (TREE_CODE (arg0
))
14280 && truth_value_p (TREE_CODE (arg1
))
14281 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
14283 location_t loc0
= expr_location_or (arg0
, loc
);
14284 /* Only perform transformation if ARG0 is easily inverted. */
14285 tem
= fold_invert_truthvalue (loc0
, arg0
);
14287 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
14290 type
, fold_convert_loc (loc
, type
, tem
),
14294 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
14295 if (integer_zerop (arg1
)
14296 && truth_value_p (TREE_CODE (arg0
))
14297 && truth_value_p (TREE_CODE (op2
))
14298 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
14300 location_t loc0
= expr_location_or (arg0
, loc
);
14301 /* Only perform transformation if ARG0 is easily inverted. */
14302 tem
= fold_invert_truthvalue (loc0
, arg0
);
14304 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
14305 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
14306 type
, fold_convert_loc (loc
, type
, tem
),
14310 /* Convert A ? 1 : B into A || B if A and B are truth values. */
14311 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
14312 && truth_value_p (TREE_CODE (arg0
))
14313 && truth_value_p (TREE_CODE (op2
))
14314 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
14315 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
14316 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
14317 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
14322 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
14323 of fold_ternary on them. */
14324 gcc_unreachable ();
14326 case BIT_FIELD_REF
:
14327 if ((TREE_CODE (arg0
) == VECTOR_CST
14328 || (TREE_CODE (arg0
) == CONSTRUCTOR
14329 && TREE_CODE (TREE_TYPE (arg0
)) == VECTOR_TYPE
))
14330 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
14331 || (TREE_CODE (type
) == VECTOR_TYPE
14332 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
14334 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
14335 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
14336 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
14337 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
14340 && (idx
% width
) == 0
14341 && (n
% width
) == 0
14342 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
14347 if (TREE_CODE (arg0
) == VECTOR_CST
)
14350 return VECTOR_CST_ELT (arg0
, idx
);
14352 tree
*vals
= XALLOCAVEC (tree
, n
);
14353 for (unsigned i
= 0; i
< n
; ++i
)
14354 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
14355 return build_vector (type
, vals
);
14358 /* Constructor elements can be subvectors. */
14359 unsigned HOST_WIDE_INT k
= 1;
14360 if (CONSTRUCTOR_NELTS (arg0
) != 0)
14362 tree cons_elem
= TREE_TYPE (CONSTRUCTOR_ELT (arg0
, 0)->value
);
14363 if (TREE_CODE (cons_elem
) == VECTOR_TYPE
)
14364 k
= TYPE_VECTOR_SUBPARTS (cons_elem
);
14367 /* We keep an exact subset of the constructor elements. */
14368 if ((idx
% k
) == 0 && (n
% k
) == 0)
14370 if (CONSTRUCTOR_NELTS (arg0
) == 0)
14371 return build_constructor (type
, NULL
);
14376 if (idx
< CONSTRUCTOR_NELTS (arg0
))
14377 return CONSTRUCTOR_ELT (arg0
, idx
)->value
;
14378 return build_zero_cst (type
);
14381 vec
<constructor_elt
, va_gc
> *vals
;
14382 vec_alloc (vals
, n
);
14383 for (unsigned i
= 0;
14384 i
< n
&& idx
+ i
< CONSTRUCTOR_NELTS (arg0
);
14386 CONSTRUCTOR_APPEND_ELT (vals
, NULL_TREE
,
14388 (arg0
, idx
+ i
)->value
);
14389 return build_constructor (type
, vals
);
14391 /* The bitfield references a single constructor element. */
14392 else if (idx
+ n
<= (idx
/ k
+ 1) * k
)
14394 if (CONSTRUCTOR_NELTS (arg0
) <= idx
/ k
)
14395 return build_zero_cst (type
);
14397 return CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
;
14399 return fold_build3_loc (loc
, code
, type
,
14400 CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
, op1
,
14401 build_int_cst (TREE_TYPE (op2
), (idx
% k
) * width
));
14406 /* A bit-field-ref that referenced the full argument can be stripped. */
14407 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
14408 && TYPE_PRECISION (TREE_TYPE (arg0
)) == tree_to_uhwi (arg1
)
14409 && integer_zerop (op2
))
14410 return fold_convert_loc (loc
, type
, arg0
);
14412 /* On constants we can use native encode/interpret to constant
14413 fold (nearly) all BIT_FIELD_REFs. */
14414 if (CONSTANT_CLASS_P (arg0
)
14415 && can_native_interpret_type_p (type
)
14416 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)))
14417 /* This limitation should not be necessary, we just need to
14418 round this up to mode size. */
14419 && tree_to_uhwi (op1
) % BITS_PER_UNIT
== 0
14420 /* Need bit-shifting of the buffer to relax the following. */
14421 && tree_to_uhwi (op2
) % BITS_PER_UNIT
== 0)
14423 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
14424 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
14425 unsigned HOST_WIDE_INT clen
;
14426 clen
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)));
14427 /* ??? We cannot tell native_encode_expr to start at
14428 some random byte only. So limit us to a reasonable amount
14432 unsigned char *b
= XALLOCAVEC (unsigned char, clen
);
14433 unsigned HOST_WIDE_INT len
= native_encode_expr (arg0
, b
, clen
);
14435 && len
* BITS_PER_UNIT
>= bitpos
+ bitsize
)
14437 tree v
= native_interpret_expr (type
,
14438 b
+ bitpos
/ BITS_PER_UNIT
,
14439 bitsize
/ BITS_PER_UNIT
);
14449 /* For integers we can decompose the FMA if possible. */
14450 if (TREE_CODE (arg0
) == INTEGER_CST
14451 && TREE_CODE (arg1
) == INTEGER_CST
)
14452 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
14453 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
14454 if (integer_zerop (arg2
))
14455 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
14457 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
14459 case VEC_PERM_EXPR
:
14460 if (TREE_CODE (arg2
) == VECTOR_CST
)
14462 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
;
14463 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
14464 bool need_mask_canon
= false;
14465 bool all_in_vec0
= true;
14466 bool all_in_vec1
= true;
14467 bool maybe_identity
= true;
14468 bool single_arg
= (op0
== op1
);
14469 bool changed
= false;
14471 mask
= single_arg
? (nelts
- 1) : (2 * nelts
- 1);
14472 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
14473 for (i
= 0; i
< nelts
; i
++)
14475 tree val
= VECTOR_CST_ELT (arg2
, i
);
14476 if (TREE_CODE (val
) != INTEGER_CST
)
14479 /* Make sure that the perm value is in an acceptable
14482 if (wi::gtu_p (t
, mask
))
14484 need_mask_canon
= true;
14485 sel
[i
] = t
.to_uhwi () & mask
;
14488 sel
[i
] = t
.to_uhwi ();
14490 if (sel
[i
] < nelts
)
14491 all_in_vec1
= false;
14493 all_in_vec0
= false;
14495 if ((sel
[i
] & (nelts
-1)) != i
)
14496 maybe_identity
= false;
14499 if (maybe_identity
)
14509 else if (all_in_vec1
)
14512 for (i
= 0; i
< nelts
; i
++)
14514 need_mask_canon
= true;
14517 if ((TREE_CODE (op0
) == VECTOR_CST
14518 || TREE_CODE (op0
) == CONSTRUCTOR
)
14519 && (TREE_CODE (op1
) == VECTOR_CST
14520 || TREE_CODE (op1
) == CONSTRUCTOR
))
14522 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
14523 if (t
!= NULL_TREE
)
14527 if (op0
== op1
&& !single_arg
)
14530 if (need_mask_canon
&& arg2
== op2
)
14532 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
14533 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
14534 for (i
= 0; i
< nelts
; i
++)
14535 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
14536 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
14541 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
14547 } /* switch (code) */
14550 /* Perform constant folding and related simplification of EXPR.
14551 The related simplifications include x*1 => x, x*0 => 0, etc.,
14552 and application of the associative law.
14553 NOP_EXPR conversions may be removed freely (as long as we
14554 are careful not to change the type of the overall expression).
14555 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14556 but we can constant-fold them if they have constant operands. */
14558 #ifdef ENABLE_FOLD_CHECKING
14559 # define fold(x) fold_1 (x)
14560 static tree
fold_1 (tree
);
14566 const tree t
= expr
;
14567 enum tree_code code
= TREE_CODE (t
);
14568 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
14570 location_t loc
= EXPR_LOCATION (expr
);
14572 /* Return right away if a constant. */
14573 if (kind
== tcc_constant
)
14576 /* CALL_EXPR-like objects with variable numbers of operands are
14577 treated specially. */
14578 if (kind
== tcc_vl_exp
)
14580 if (code
== CALL_EXPR
)
14582 tem
= fold_call_expr (loc
, expr
, false);
14583 return tem
? tem
: expr
;
14588 if (IS_EXPR_CODE_CLASS (kind
))
14590 tree type
= TREE_TYPE (t
);
14591 tree op0
, op1
, op2
;
14593 switch (TREE_CODE_LENGTH (code
))
14596 op0
= TREE_OPERAND (t
, 0);
14597 tem
= fold_unary_loc (loc
, code
, type
, op0
);
14598 return tem
? tem
: expr
;
14600 op0
= TREE_OPERAND (t
, 0);
14601 op1
= TREE_OPERAND (t
, 1);
14602 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
14603 return tem
? tem
: expr
;
14605 op0
= TREE_OPERAND (t
, 0);
14606 op1
= TREE_OPERAND (t
, 1);
14607 op2
= TREE_OPERAND (t
, 2);
14608 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
14609 return tem
? tem
: expr
;
14619 tree op0
= TREE_OPERAND (t
, 0);
14620 tree op1
= TREE_OPERAND (t
, 1);
14622 if (TREE_CODE (op1
) == INTEGER_CST
14623 && TREE_CODE (op0
) == CONSTRUCTOR
14624 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
14626 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (op0
);
14627 unsigned HOST_WIDE_INT end
= vec_safe_length (elts
);
14628 unsigned HOST_WIDE_INT begin
= 0;
14630 /* Find a matching index by means of a binary search. */
14631 while (begin
!= end
)
14633 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
14634 tree index
= (*elts
)[middle
].index
;
14636 if (TREE_CODE (index
) == INTEGER_CST
14637 && tree_int_cst_lt (index
, op1
))
14638 begin
= middle
+ 1;
14639 else if (TREE_CODE (index
) == INTEGER_CST
14640 && tree_int_cst_lt (op1
, index
))
14642 else if (TREE_CODE (index
) == RANGE_EXPR
14643 && tree_int_cst_lt (TREE_OPERAND (index
, 1), op1
))
14644 begin
= middle
+ 1;
14645 else if (TREE_CODE (index
) == RANGE_EXPR
14646 && tree_int_cst_lt (op1
, TREE_OPERAND (index
, 0)))
14649 return (*elts
)[middle
].value
;
14656 /* Return a VECTOR_CST if possible. */
14659 tree type
= TREE_TYPE (t
);
14660 if (TREE_CODE (type
) != VECTOR_TYPE
)
14663 tree
*vec
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
14664 unsigned HOST_WIDE_INT idx
, pos
= 0;
14667 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, value
)
14669 if (!CONSTANT_CLASS_P (value
))
14671 if (TREE_CODE (value
) == VECTOR_CST
)
14673 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (value
); ++i
)
14674 vec
[pos
++] = VECTOR_CST_ELT (value
, i
);
14677 vec
[pos
++] = value
;
14679 for (; pos
< TYPE_VECTOR_SUBPARTS (type
); ++pos
)
14680 vec
[pos
] = build_zero_cst (TREE_TYPE (type
));
14682 return build_vector (type
, vec
);
14686 return fold (DECL_INITIAL (t
));
14690 } /* switch (code) */
14693 #ifdef ENABLE_FOLD_CHECKING
14696 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
14697 hash_table
<pointer_hash
<tree_node
> >);
14698 static void fold_check_failed (const_tree
, const_tree
);
14699 void print_fold_checksum (const_tree
);
14701 /* When --enable-checking=fold, compute a digest of expr before
14702 and after actual fold call to see if fold did not accidentally
14703 change original expr. */
14709 struct md5_ctx ctx
;
14710 unsigned char checksum_before
[16], checksum_after
[16];
14711 hash_table
<pointer_hash
<tree_node
> > ht
;
14714 md5_init_ctx (&ctx
);
14715 fold_checksum_tree (expr
, &ctx
, ht
);
14716 md5_finish_ctx (&ctx
, checksum_before
);
14719 ret
= fold_1 (expr
);
14721 md5_init_ctx (&ctx
);
14722 fold_checksum_tree (expr
, &ctx
, ht
);
14723 md5_finish_ctx (&ctx
, checksum_after
);
14726 if (memcmp (checksum_before
, checksum_after
, 16))
14727 fold_check_failed (expr
, ret
);
14733 print_fold_checksum (const_tree expr
)
14735 struct md5_ctx ctx
;
14736 unsigned char checksum
[16], cnt
;
14737 hash_table
<pointer_hash
<tree_node
> > ht
;
14740 md5_init_ctx (&ctx
);
14741 fold_checksum_tree (expr
, &ctx
, ht
);
14742 md5_finish_ctx (&ctx
, checksum
);
14744 for (cnt
= 0; cnt
< 16; ++cnt
)
14745 fprintf (stderr
, "%02x", checksum
[cnt
]);
14746 putc ('\n', stderr
);
14750 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
14752 internal_error ("fold check: original tree changed by fold");
14756 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
14757 hash_table
<pointer_hash
<tree_node
> > ht
)
14760 enum tree_code code
;
14761 union tree_node buf
;
14767 slot
= ht
.find_slot (expr
, INSERT
);
14770 *slot
= CONST_CAST_TREE (expr
);
14771 code
= TREE_CODE (expr
);
14772 if (TREE_CODE_CLASS (code
) == tcc_declaration
14773 && DECL_ASSEMBLER_NAME_SET_P (expr
))
14775 /* Allow DECL_ASSEMBLER_NAME to be modified. */
14776 memcpy ((char *) &buf
, expr
, tree_size (expr
));
14777 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
14778 expr
= (tree
) &buf
;
14780 else if (TREE_CODE_CLASS (code
) == tcc_type
14781 && (TYPE_POINTER_TO (expr
)
14782 || TYPE_REFERENCE_TO (expr
)
14783 || TYPE_CACHED_VALUES_P (expr
)
14784 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
14785 || TYPE_NEXT_VARIANT (expr
)))
14787 /* Allow these fields to be modified. */
14789 memcpy ((char *) &buf
, expr
, tree_size (expr
));
14790 expr
= tmp
= (tree
) &buf
;
14791 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
14792 TYPE_POINTER_TO (tmp
) = NULL
;
14793 TYPE_REFERENCE_TO (tmp
) = NULL
;
14794 TYPE_NEXT_VARIANT (tmp
) = NULL
;
14795 if (TYPE_CACHED_VALUES_P (tmp
))
14797 TYPE_CACHED_VALUES_P (tmp
) = 0;
14798 TYPE_CACHED_VALUES (tmp
) = NULL
;
14801 md5_process_bytes (expr
, tree_size (expr
), ctx
);
14802 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
14803 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
14804 if (TREE_CODE_CLASS (code
) != tcc_type
14805 && TREE_CODE_CLASS (code
) != tcc_declaration
14806 && code
!= TREE_LIST
14807 && code
!= SSA_NAME
14808 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
14809 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
14810 switch (TREE_CODE_CLASS (code
))
14816 md5_process_bytes (TREE_STRING_POINTER (expr
),
14817 TREE_STRING_LENGTH (expr
), ctx
);
14820 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
14821 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
14824 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
14825 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
14831 case tcc_exceptional
:
14835 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
14836 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
14837 expr
= TREE_CHAIN (expr
);
14838 goto recursive_label
;
14841 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
14842 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
14848 case tcc_expression
:
14849 case tcc_reference
:
14850 case tcc_comparison
:
14853 case tcc_statement
:
14855 len
= TREE_OPERAND_LENGTH (expr
);
14856 for (i
= 0; i
< len
; ++i
)
14857 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
14859 case tcc_declaration
:
14860 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
14861 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
14862 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
14864 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
14865 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
14866 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
14867 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
14868 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
14870 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_WITH_VIS
))
14871 fold_checksum_tree (DECL_SECTION_NAME (expr
), ctx
, ht
);
14873 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
14875 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
14876 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
14877 fold_checksum_tree (DECL_ARGUMENT_FLD (expr
), ctx
, ht
);
14881 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
14882 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
14883 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
14884 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
14885 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
14886 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
14887 if (INTEGRAL_TYPE_P (expr
)
14888 || SCALAR_FLOAT_TYPE_P (expr
))
14890 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
14891 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
14893 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
14894 if (TREE_CODE (expr
) == RECORD_TYPE
14895 || TREE_CODE (expr
) == UNION_TYPE
14896 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
14897 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
14898 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
14905 /* Helper function for outputting the checksum of a tree T. When
14906 debugging with gdb, you can "define mynext" to be "next" followed
14907 by "call debug_fold_checksum (op0)", then just trace down till the
14910 DEBUG_FUNCTION
void
14911 debug_fold_checksum (const_tree t
)
14914 unsigned char checksum
[16];
14915 struct md5_ctx ctx
;
14916 hash_table
<pointer_hash
<tree_node
> > ht
;
14919 md5_init_ctx (&ctx
);
14920 fold_checksum_tree (t
, &ctx
, ht
);
14921 md5_finish_ctx (&ctx
, checksum
);
14924 for (i
= 0; i
< 16; i
++)
14925 fprintf (stderr
, "%d ", checksum
[i
]);
14927 fprintf (stderr
, "\n");
14932 /* Fold a unary tree expression with code CODE of type TYPE with an
14933 operand OP0. LOC is the location of the resulting expression.
14934 Return a folded expression if successful. Otherwise, return a tree
14935 expression with code CODE of type TYPE with an operand OP0. */
14938 fold_build1_stat_loc (location_t loc
,
14939 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
14942 #ifdef ENABLE_FOLD_CHECKING
14943 unsigned char checksum_before
[16], checksum_after
[16];
14944 struct md5_ctx ctx
;
14945 hash_table
<pointer_hash
<tree_node
> > ht
;
14948 md5_init_ctx (&ctx
);
14949 fold_checksum_tree (op0
, &ctx
, ht
);
14950 md5_finish_ctx (&ctx
, checksum_before
);
14954 tem
= fold_unary_loc (loc
, code
, type
, op0
);
14956 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
14958 #ifdef ENABLE_FOLD_CHECKING
14959 md5_init_ctx (&ctx
);
14960 fold_checksum_tree (op0
, &ctx
, ht
);
14961 md5_finish_ctx (&ctx
, checksum_after
);
14964 if (memcmp (checksum_before
, checksum_after
, 16))
14965 fold_check_failed (op0
, tem
);
14970 /* Fold a binary tree expression with code CODE of type TYPE with
14971 operands OP0 and OP1. LOC is the location of the resulting
14972 expression. Return a folded expression if successful. Otherwise,
14973 return a tree expression with code CODE of type TYPE with operands
14977 fold_build2_stat_loc (location_t loc
,
14978 enum tree_code code
, tree type
, tree op0
, tree op1
14982 #ifdef ENABLE_FOLD_CHECKING
14983 unsigned char checksum_before_op0
[16],
14984 checksum_before_op1
[16],
14985 checksum_after_op0
[16],
14986 checksum_after_op1
[16];
14987 struct md5_ctx ctx
;
14988 hash_table
<pointer_hash
<tree_node
> > ht
;
14991 md5_init_ctx (&ctx
);
14992 fold_checksum_tree (op0
, &ctx
, ht
);
14993 md5_finish_ctx (&ctx
, checksum_before_op0
);
14996 md5_init_ctx (&ctx
);
14997 fold_checksum_tree (op1
, &ctx
, ht
);
14998 md5_finish_ctx (&ctx
, checksum_before_op1
);
15002 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
15004 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
15006 #ifdef ENABLE_FOLD_CHECKING
15007 md5_init_ctx (&ctx
);
15008 fold_checksum_tree (op0
, &ctx
, ht
);
15009 md5_finish_ctx (&ctx
, checksum_after_op0
);
15012 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
15013 fold_check_failed (op0
, tem
);
15015 md5_init_ctx (&ctx
);
15016 fold_checksum_tree (op1
, &ctx
, ht
);
15017 md5_finish_ctx (&ctx
, checksum_after_op1
);
15020 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
15021 fold_check_failed (op1
, tem
);
15026 /* Fold a ternary tree expression with code CODE of type TYPE with
15027 operands OP0, OP1, and OP2. Return a folded expression if
15028 successful. Otherwise, return a tree expression with code CODE of
15029 type TYPE with operands OP0, OP1, and OP2. */
15032 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
15033 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
15036 #ifdef ENABLE_FOLD_CHECKING
15037 unsigned char checksum_before_op0
[16],
15038 checksum_before_op1
[16],
15039 checksum_before_op2
[16],
15040 checksum_after_op0
[16],
15041 checksum_after_op1
[16],
15042 checksum_after_op2
[16];
15043 struct md5_ctx ctx
;
15044 hash_table
<pointer_hash
<tree_node
> > ht
;
15047 md5_init_ctx (&ctx
);
15048 fold_checksum_tree (op0
, &ctx
, ht
);
15049 md5_finish_ctx (&ctx
, checksum_before_op0
);
15052 md5_init_ctx (&ctx
);
15053 fold_checksum_tree (op1
, &ctx
, ht
);
15054 md5_finish_ctx (&ctx
, checksum_before_op1
);
15057 md5_init_ctx (&ctx
);
15058 fold_checksum_tree (op2
, &ctx
, ht
);
15059 md5_finish_ctx (&ctx
, checksum_before_op2
);
15063 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
15064 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
15066 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
15068 #ifdef ENABLE_FOLD_CHECKING
15069 md5_init_ctx (&ctx
);
15070 fold_checksum_tree (op0
, &ctx
, ht
);
15071 md5_finish_ctx (&ctx
, checksum_after_op0
);
15074 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
15075 fold_check_failed (op0
, tem
);
15077 md5_init_ctx (&ctx
);
15078 fold_checksum_tree (op1
, &ctx
, ht
);
15079 md5_finish_ctx (&ctx
, checksum_after_op1
);
15082 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
15083 fold_check_failed (op1
, tem
);
15085 md5_init_ctx (&ctx
);
15086 fold_checksum_tree (op2
, &ctx
, ht
);
15087 md5_finish_ctx (&ctx
, checksum_after_op2
);
15090 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
15091 fold_check_failed (op2
, tem
);
15096 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
15097 arguments in ARGARRAY, and a null static chain.
15098 Return a folded expression if successful. Otherwise, return a CALL_EXPR
15099 of type TYPE from the given operands as constructed by build_call_array. */
15102 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
15103 int nargs
, tree
*argarray
)
15106 #ifdef ENABLE_FOLD_CHECKING
15107 unsigned char checksum_before_fn
[16],
15108 checksum_before_arglist
[16],
15109 checksum_after_fn
[16],
15110 checksum_after_arglist
[16];
15111 struct md5_ctx ctx
;
15112 hash_table
<pointer_hash
<tree_node
> > ht
;
15116 md5_init_ctx (&ctx
);
15117 fold_checksum_tree (fn
, &ctx
, ht
);
15118 md5_finish_ctx (&ctx
, checksum_before_fn
);
15121 md5_init_ctx (&ctx
);
15122 for (i
= 0; i
< nargs
; i
++)
15123 fold_checksum_tree (argarray
[i
], &ctx
, ht
);
15124 md5_finish_ctx (&ctx
, checksum_before_arglist
);
15128 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
15130 #ifdef ENABLE_FOLD_CHECKING
15131 md5_init_ctx (&ctx
);
15132 fold_checksum_tree (fn
, &ctx
, ht
);
15133 md5_finish_ctx (&ctx
, checksum_after_fn
);
15136 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
15137 fold_check_failed (fn
, tem
);
15139 md5_init_ctx (&ctx
);
15140 for (i
= 0; i
< nargs
; i
++)
15141 fold_checksum_tree (argarray
[i
], &ctx
, ht
);
15142 md5_finish_ctx (&ctx
, checksum_after_arglist
);
15145 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
15146 fold_check_failed (NULL_TREE
, tem
);
15151 /* Perform constant folding and related simplification of initializer
15152 expression EXPR. These behave identically to "fold_buildN" but ignore
15153 potential run-time traps and exceptions that fold must preserve. */
15155 #define START_FOLD_INIT \
15156 int saved_signaling_nans = flag_signaling_nans;\
15157 int saved_trapping_math = flag_trapping_math;\
15158 int saved_rounding_math = flag_rounding_math;\
15159 int saved_trapv = flag_trapv;\
15160 int saved_folding_initializer = folding_initializer;\
15161 flag_signaling_nans = 0;\
15162 flag_trapping_math = 0;\
15163 flag_rounding_math = 0;\
15165 folding_initializer = 1;
15167 #define END_FOLD_INIT \
15168 flag_signaling_nans = saved_signaling_nans;\
15169 flag_trapping_math = saved_trapping_math;\
15170 flag_rounding_math = saved_rounding_math;\
15171 flag_trapv = saved_trapv;\
15172 folding_initializer = saved_folding_initializer;
15175 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
15176 tree type
, tree op
)
15181 result
= fold_build1_loc (loc
, code
, type
, op
);
15188 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
15189 tree type
, tree op0
, tree op1
)
15194 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
15201 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
15202 int nargs
, tree
*argarray
)
15207 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
15213 #undef START_FOLD_INIT
15214 #undef END_FOLD_INIT
15216 /* Determine if first argument is a multiple of second argument. Return 0 if
15217 it is not, or we cannot easily determined it to be.
15219 An example of the sort of thing we care about (at this point; this routine
15220 could surely be made more general, and expanded to do what the *_DIV_EXPR's
15221 fold cases do now) is discovering that
15223 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15229 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
15231 This code also handles discovering that
15233 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15235 is a multiple of 8 so we don't have to worry about dealing with a
15236 possible remainder.
15238 Note that we *look* inside a SAVE_EXPR only to determine how it was
15239 calculated; it is not safe for fold to do much of anything else with the
15240 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
15241 at run time. For example, the latter example above *cannot* be implemented
15242 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
15243 evaluation time of the original SAVE_EXPR is not necessarily the same at
15244 the time the new expression is evaluated. The only optimization of this
15245 sort that would be valid is changing
15247 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
15251 SAVE_EXPR (I) * SAVE_EXPR (J)
15253 (where the same SAVE_EXPR (J) is used in the original and the
15254 transformed version). */
15257 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
15259 if (operand_equal_p (top
, bottom
, 0))
15262 if (TREE_CODE (type
) != INTEGER_TYPE
)
15265 switch (TREE_CODE (top
))
15268 /* Bitwise and provides a power of two multiple. If the mask is
15269 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
15270 if (!integer_pow2p (bottom
))
15275 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
15276 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
15280 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
15281 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
15284 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
15288 op1
= TREE_OPERAND (top
, 1);
15289 /* const_binop may not detect overflow correctly,
15290 so check for it explicitly here. */
15291 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
15292 && 0 != (t1
= fold_convert (type
,
15293 const_binop (LSHIFT_EXPR
,
15296 && !TREE_OVERFLOW (t1
))
15297 return multiple_of_p (type
, t1
, bottom
);
15302 /* Can't handle conversions from non-integral or wider integral type. */
15303 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
15304 || (TYPE_PRECISION (type
)
15305 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
15308 /* .. fall through ... */
15311 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
15314 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
15315 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
15318 if (TREE_CODE (bottom
) != INTEGER_CST
15319 || integer_zerop (bottom
)
15320 || (TYPE_UNSIGNED (type
)
15321 && (tree_int_cst_sgn (top
) < 0
15322 || tree_int_cst_sgn (bottom
) < 0)))
15324 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR
,
15332 /* Return true if CODE or TYPE is known to be non-negative. */
15335 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
15337 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
15338 && truth_value_p (code
))
15339 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
15340 have a signed:1 type (where the value is -1 and 0). */
15345 /* Return true if (CODE OP0) is known to be non-negative. If the return
15346 value is based on the assumption that signed overflow is undefined,
15347 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15348 *STRICT_OVERFLOW_P. */
15351 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
15352 bool *strict_overflow_p
)
15354 if (TYPE_UNSIGNED (type
))
15360 /* We can't return 1 if flag_wrapv is set because
15361 ABS_EXPR<INT_MIN> = INT_MIN. */
15362 if (!INTEGRAL_TYPE_P (type
))
15364 if (TYPE_OVERFLOW_UNDEFINED (type
))
15366 *strict_overflow_p
= true;
15371 case NON_LVALUE_EXPR
:
15373 case FIX_TRUNC_EXPR
:
15374 return tree_expr_nonnegative_warnv_p (op0
,
15375 strict_overflow_p
);
15379 tree inner_type
= TREE_TYPE (op0
);
15380 tree outer_type
= type
;
15382 if (TREE_CODE (outer_type
) == REAL_TYPE
)
15384 if (TREE_CODE (inner_type
) == REAL_TYPE
)
15385 return tree_expr_nonnegative_warnv_p (op0
,
15386 strict_overflow_p
);
15387 if (INTEGRAL_TYPE_P (inner_type
))
15389 if (TYPE_UNSIGNED (inner_type
))
15391 return tree_expr_nonnegative_warnv_p (op0
,
15392 strict_overflow_p
);
15395 else if (INTEGRAL_TYPE_P (outer_type
))
15397 if (TREE_CODE (inner_type
) == REAL_TYPE
)
15398 return tree_expr_nonnegative_warnv_p (op0
,
15399 strict_overflow_p
);
15400 if (INTEGRAL_TYPE_P (inner_type
))
15401 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
15402 && TYPE_UNSIGNED (inner_type
);
15408 return tree_simple_nonnegative_warnv_p (code
, type
);
15411 /* We don't know sign of `t', so be conservative and return false. */
15415 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
15416 value is based on the assumption that signed overflow is undefined,
15417 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15418 *STRICT_OVERFLOW_P. */
15421 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
15422 tree op1
, bool *strict_overflow_p
)
15424 if (TYPE_UNSIGNED (type
))
15429 case POINTER_PLUS_EXPR
:
15431 if (FLOAT_TYPE_P (type
))
15432 return (tree_expr_nonnegative_warnv_p (op0
,
15434 && tree_expr_nonnegative_warnv_p (op1
,
15435 strict_overflow_p
));
15437 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
15438 both unsigned and at least 2 bits shorter than the result. */
15439 if (TREE_CODE (type
) == INTEGER_TYPE
15440 && TREE_CODE (op0
) == NOP_EXPR
15441 && TREE_CODE (op1
) == NOP_EXPR
)
15443 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
15444 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
15445 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
15446 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
15448 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
15449 TYPE_PRECISION (inner2
)) + 1;
15450 return prec
< TYPE_PRECISION (type
);
15456 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
15458 /* x * x is always non-negative for floating point x
15459 or without overflow. */
15460 if (operand_equal_p (op0
, op1
, 0)
15461 || (tree_expr_nonnegative_warnv_p (op0
, strict_overflow_p
)
15462 && tree_expr_nonnegative_warnv_p (op1
, strict_overflow_p
)))
15464 if (TYPE_OVERFLOW_UNDEFINED (type
))
15465 *strict_overflow_p
= true;
15470 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
15471 both unsigned and their total bits is shorter than the result. */
15472 if (TREE_CODE (type
) == INTEGER_TYPE
15473 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
15474 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
15476 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
15477 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
15479 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
15480 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
15483 bool unsigned0
= TYPE_UNSIGNED (inner0
);
15484 bool unsigned1
= TYPE_UNSIGNED (inner1
);
15486 if (TREE_CODE (op0
) == INTEGER_CST
)
15487 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
15489 if (TREE_CODE (op1
) == INTEGER_CST
)
15490 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
15492 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
15493 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
15495 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
15496 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
15497 : TYPE_PRECISION (inner0
);
15499 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
15500 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
15501 : TYPE_PRECISION (inner1
);
15503 return precision0
+ precision1
< TYPE_PRECISION (type
);
15510 return (tree_expr_nonnegative_warnv_p (op0
,
15512 || tree_expr_nonnegative_warnv_p (op1
,
15513 strict_overflow_p
));
15519 case TRUNC_DIV_EXPR
:
15520 case CEIL_DIV_EXPR
:
15521 case FLOOR_DIV_EXPR
:
15522 case ROUND_DIV_EXPR
:
15523 return (tree_expr_nonnegative_warnv_p (op0
,
15525 && tree_expr_nonnegative_warnv_p (op1
,
15526 strict_overflow_p
));
15528 case TRUNC_MOD_EXPR
:
15529 case CEIL_MOD_EXPR
:
15530 case FLOOR_MOD_EXPR
:
15531 case ROUND_MOD_EXPR
:
15532 return tree_expr_nonnegative_warnv_p (op0
,
15533 strict_overflow_p
);
15535 return tree_simple_nonnegative_warnv_p (code
, type
);
15538 /* We don't know sign of `t', so be conservative and return false. */
15542 /* Return true if T is known to be non-negative. If the return
15543 value is based on the assumption that signed overflow is undefined,
15544 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15545 *STRICT_OVERFLOW_P. */
15548 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
15550 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15553 switch (TREE_CODE (t
))
15556 return tree_int_cst_sgn (t
) >= 0;
15559 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
15562 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
15565 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
15567 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 2),
15568 strict_overflow_p
));
15570 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
),
15573 /* We don't know sign of `t', so be conservative and return false. */
15577 /* Return true if T is known to be non-negative. If the return
15578 value is based on the assumption that signed overflow is undefined,
15579 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15580 *STRICT_OVERFLOW_P. */
15583 tree_call_nonnegative_warnv_p (tree type
, tree fndecl
,
15584 tree arg0
, tree arg1
, bool *strict_overflow_p
)
15586 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
15587 switch (DECL_FUNCTION_CODE (fndecl
))
15589 CASE_FLT_FN (BUILT_IN_ACOS
):
15590 CASE_FLT_FN (BUILT_IN_ACOSH
):
15591 CASE_FLT_FN (BUILT_IN_CABS
):
15592 CASE_FLT_FN (BUILT_IN_COSH
):
15593 CASE_FLT_FN (BUILT_IN_ERFC
):
15594 CASE_FLT_FN (BUILT_IN_EXP
):
15595 CASE_FLT_FN (BUILT_IN_EXP10
):
15596 CASE_FLT_FN (BUILT_IN_EXP2
):
15597 CASE_FLT_FN (BUILT_IN_FABS
):
15598 CASE_FLT_FN (BUILT_IN_FDIM
):
15599 CASE_FLT_FN (BUILT_IN_HYPOT
):
15600 CASE_FLT_FN (BUILT_IN_POW10
):
15601 CASE_INT_FN (BUILT_IN_FFS
):
15602 CASE_INT_FN (BUILT_IN_PARITY
):
15603 CASE_INT_FN (BUILT_IN_POPCOUNT
):
15604 CASE_INT_FN (BUILT_IN_CLZ
):
15605 CASE_INT_FN (BUILT_IN_CLRSB
):
15606 case BUILT_IN_BSWAP32
:
15607 case BUILT_IN_BSWAP64
:
15611 CASE_FLT_FN (BUILT_IN_SQRT
):
15612 /* sqrt(-0.0) is -0.0. */
15613 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type
)))
15615 return tree_expr_nonnegative_warnv_p (arg0
,
15616 strict_overflow_p
);
15618 CASE_FLT_FN (BUILT_IN_ASINH
):
15619 CASE_FLT_FN (BUILT_IN_ATAN
):
15620 CASE_FLT_FN (BUILT_IN_ATANH
):
15621 CASE_FLT_FN (BUILT_IN_CBRT
):
15622 CASE_FLT_FN (BUILT_IN_CEIL
):
15623 CASE_FLT_FN (BUILT_IN_ERF
):
15624 CASE_FLT_FN (BUILT_IN_EXPM1
):
15625 CASE_FLT_FN (BUILT_IN_FLOOR
):
15626 CASE_FLT_FN (BUILT_IN_FMOD
):
15627 CASE_FLT_FN (BUILT_IN_FREXP
):
15628 CASE_FLT_FN (BUILT_IN_ICEIL
):
15629 CASE_FLT_FN (BUILT_IN_IFLOOR
):
15630 CASE_FLT_FN (BUILT_IN_IRINT
):
15631 CASE_FLT_FN (BUILT_IN_IROUND
):
15632 CASE_FLT_FN (BUILT_IN_LCEIL
):
15633 CASE_FLT_FN (BUILT_IN_LDEXP
):
15634 CASE_FLT_FN (BUILT_IN_LFLOOR
):
15635 CASE_FLT_FN (BUILT_IN_LLCEIL
):
15636 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
15637 CASE_FLT_FN (BUILT_IN_LLRINT
):
15638 CASE_FLT_FN (BUILT_IN_LLROUND
):
15639 CASE_FLT_FN (BUILT_IN_LRINT
):
15640 CASE_FLT_FN (BUILT_IN_LROUND
):
15641 CASE_FLT_FN (BUILT_IN_MODF
):
15642 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
15643 CASE_FLT_FN (BUILT_IN_RINT
):
15644 CASE_FLT_FN (BUILT_IN_ROUND
):
15645 CASE_FLT_FN (BUILT_IN_SCALB
):
15646 CASE_FLT_FN (BUILT_IN_SCALBLN
):
15647 CASE_FLT_FN (BUILT_IN_SCALBN
):
15648 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
15649 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
15650 CASE_FLT_FN (BUILT_IN_SINH
):
15651 CASE_FLT_FN (BUILT_IN_TANH
):
15652 CASE_FLT_FN (BUILT_IN_TRUNC
):
15653 /* True if the 1st argument is nonnegative. */
15654 return tree_expr_nonnegative_warnv_p (arg0
,
15655 strict_overflow_p
);
15657 CASE_FLT_FN (BUILT_IN_FMAX
):
15658 /* True if the 1st OR 2nd arguments are nonnegative. */
15659 return (tree_expr_nonnegative_warnv_p (arg0
,
15661 || (tree_expr_nonnegative_warnv_p (arg1
,
15662 strict_overflow_p
)));
15664 CASE_FLT_FN (BUILT_IN_FMIN
):
15665 /* True if the 1st AND 2nd arguments are nonnegative. */
15666 return (tree_expr_nonnegative_warnv_p (arg0
,
15668 && (tree_expr_nonnegative_warnv_p (arg1
,
15669 strict_overflow_p
)));
15671 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
15672 /* True if the 2nd argument is nonnegative. */
15673 return tree_expr_nonnegative_warnv_p (arg1
,
15674 strict_overflow_p
);
15676 CASE_FLT_FN (BUILT_IN_POWI
):
15677 /* True if the 1st argument is nonnegative or the second
15678 argument is an even integer. */
15679 if (TREE_CODE (arg1
) == INTEGER_CST
15680 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
15682 return tree_expr_nonnegative_warnv_p (arg0
,
15683 strict_overflow_p
);
15685 CASE_FLT_FN (BUILT_IN_POW
):
15686 /* True if the 1st argument is nonnegative or the second
15687 argument is an even integer valued real. */
15688 if (TREE_CODE (arg1
) == REAL_CST
)
15693 c
= TREE_REAL_CST (arg1
);
15694 n
= real_to_integer (&c
);
15697 REAL_VALUE_TYPE cint
;
15698 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
15699 if (real_identical (&c
, &cint
))
15703 return tree_expr_nonnegative_warnv_p (arg0
,
15704 strict_overflow_p
);
15709 return tree_simple_nonnegative_warnv_p (CALL_EXPR
,
15713 /* Return true if T is known to be non-negative. If the return
15714 value is based on the assumption that signed overflow is undefined,
15715 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15716 *STRICT_OVERFLOW_P. */
15719 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
15721 enum tree_code code
= TREE_CODE (t
);
15722 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
15729 tree temp
= TARGET_EXPR_SLOT (t
);
15730 t
= TARGET_EXPR_INITIAL (t
);
15732 /* If the initializer is non-void, then it's a normal expression
15733 that will be assigned to the slot. */
15734 if (!VOID_TYPE_P (t
))
15735 return tree_expr_nonnegative_warnv_p (t
, strict_overflow_p
);
15737 /* Otherwise, the initializer sets the slot in some way. One common
15738 way is an assignment statement at the end of the initializer. */
15741 if (TREE_CODE (t
) == BIND_EXPR
)
15742 t
= expr_last (BIND_EXPR_BODY (t
));
15743 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
15744 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
15745 t
= expr_last (TREE_OPERAND (t
, 0));
15746 else if (TREE_CODE (t
) == STATEMENT_LIST
)
15751 if (TREE_CODE (t
) == MODIFY_EXPR
15752 && TREE_OPERAND (t
, 0) == temp
)
15753 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
15754 strict_overflow_p
);
15761 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
15762 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
15764 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
15765 get_callee_fndecl (t
),
15768 strict_overflow_p
);
15770 case COMPOUND_EXPR
:
15772 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
15773 strict_overflow_p
);
15775 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t
, 1)),
15776 strict_overflow_p
);
15778 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 0),
15779 strict_overflow_p
);
15782 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
),
15786 /* We don't know sign of `t', so be conservative and return false. */
15790 /* Return true if T is known to be non-negative. If the return
15791 value is based on the assumption that signed overflow is undefined,
15792 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15793 *STRICT_OVERFLOW_P. */
15796 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
15798 enum tree_code code
;
15799 if (t
== error_mark_node
)
15802 code
= TREE_CODE (t
);
15803 switch (TREE_CODE_CLASS (code
))
15806 case tcc_comparison
:
15807 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15809 TREE_OPERAND (t
, 0),
15810 TREE_OPERAND (t
, 1),
15811 strict_overflow_p
);
15814 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15816 TREE_OPERAND (t
, 0),
15817 strict_overflow_p
);
15820 case tcc_declaration
:
15821 case tcc_reference
:
15822 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
);
15830 case TRUTH_AND_EXPR
:
15831 case TRUTH_OR_EXPR
:
15832 case TRUTH_XOR_EXPR
:
15833 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
15835 TREE_OPERAND (t
, 0),
15836 TREE_OPERAND (t
, 1),
15837 strict_overflow_p
);
15838 case TRUTH_NOT_EXPR
:
15839 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
15841 TREE_OPERAND (t
, 0),
15842 strict_overflow_p
);
15849 case WITH_SIZE_EXPR
:
15851 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
);
15854 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
);
15858 /* Return true if `t' is known to be non-negative. Handle warnings
15859 about undefined signed overflow. */
15862 tree_expr_nonnegative_p (tree t
)
15864 bool ret
, strict_overflow_p
;
15866 strict_overflow_p
= false;
15867 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
15868 if (strict_overflow_p
)
15869 fold_overflow_warning (("assuming signed overflow does not occur when "
15870 "determining that expression is always "
15872 WARN_STRICT_OVERFLOW_MISC
);
15877 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15878 For floating point we further ensure that T is not denormal.
15879 Similar logic is present in nonzero_address in rtlanal.h.
15881 If the return value is based on the assumption that signed overflow
15882 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15883 change *STRICT_OVERFLOW_P. */
15886 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
15887 bool *strict_overflow_p
)
15892 return tree_expr_nonzero_warnv_p (op0
,
15893 strict_overflow_p
);
15897 tree inner_type
= TREE_TYPE (op0
);
15898 tree outer_type
= type
;
15900 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
15901 && tree_expr_nonzero_warnv_p (op0
,
15902 strict_overflow_p
));
15906 case NON_LVALUE_EXPR
:
15907 return tree_expr_nonzero_warnv_p (op0
,
15908 strict_overflow_p
);
15917 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15918 For floating point we further ensure that T is not denormal.
15919 Similar logic is present in nonzero_address in rtlanal.h.
15921 If the return value is based on the assumption that signed overflow
15922 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15923 change *STRICT_OVERFLOW_P. */
15926 tree_binary_nonzero_warnv_p (enum tree_code code
,
15929 tree op1
, bool *strict_overflow_p
)
15931 bool sub_strict_overflow_p
;
15934 case POINTER_PLUS_EXPR
:
15936 if (TYPE_OVERFLOW_UNDEFINED (type
))
15938 /* With the presence of negative values it is hard
15939 to say something. */
15940 sub_strict_overflow_p
= false;
15941 if (!tree_expr_nonnegative_warnv_p (op0
,
15942 &sub_strict_overflow_p
)
15943 || !tree_expr_nonnegative_warnv_p (op1
,
15944 &sub_strict_overflow_p
))
15946 /* One of operands must be positive and the other non-negative. */
15947 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15948 overflows, on a twos-complement machine the sum of two
15949 nonnegative numbers can never be zero. */
15950 return (tree_expr_nonzero_warnv_p (op0
,
15952 || tree_expr_nonzero_warnv_p (op1
,
15953 strict_overflow_p
));
15958 if (TYPE_OVERFLOW_UNDEFINED (type
))
15960 if (tree_expr_nonzero_warnv_p (op0
,
15962 && tree_expr_nonzero_warnv_p (op1
,
15963 strict_overflow_p
))
15965 *strict_overflow_p
= true;
15972 sub_strict_overflow_p
= false;
15973 if (tree_expr_nonzero_warnv_p (op0
,
15974 &sub_strict_overflow_p
)
15975 && tree_expr_nonzero_warnv_p (op1
,
15976 &sub_strict_overflow_p
))
15978 if (sub_strict_overflow_p
)
15979 *strict_overflow_p
= true;
15984 sub_strict_overflow_p
= false;
15985 if (tree_expr_nonzero_warnv_p (op0
,
15986 &sub_strict_overflow_p
))
15988 if (sub_strict_overflow_p
)
15989 *strict_overflow_p
= true;
15991 /* When both operands are nonzero, then MAX must be too. */
15992 if (tree_expr_nonzero_warnv_p (op1
,
15993 strict_overflow_p
))
15996 /* MAX where operand 0 is positive is positive. */
15997 return tree_expr_nonnegative_warnv_p (op0
,
15998 strict_overflow_p
);
16000 /* MAX where operand 1 is positive is positive. */
16001 else if (tree_expr_nonzero_warnv_p (op1
,
16002 &sub_strict_overflow_p
)
16003 && tree_expr_nonnegative_warnv_p (op1
,
16004 &sub_strict_overflow_p
))
16006 if (sub_strict_overflow_p
)
16007 *strict_overflow_p
= true;
16013 return (tree_expr_nonzero_warnv_p (op1
,
16015 || tree_expr_nonzero_warnv_p (op0
,
16016 strict_overflow_p
));
16025 /* Return true when T is an address and is known to be nonzero.
16026 For floating point we further ensure that T is not denormal.
16027 Similar logic is present in nonzero_address in rtlanal.h.
16029 If the return value is based on the assumption that signed overflow
16030 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
16031 change *STRICT_OVERFLOW_P. */
16034 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
16036 bool sub_strict_overflow_p
;
16037 switch (TREE_CODE (t
))
16040 return !integer_zerop (t
);
16044 tree base
= TREE_OPERAND (t
, 0);
16045 if (!DECL_P (base
))
16046 base
= get_base_address (base
);
16051 /* Weak declarations may link to NULL. Other things may also be NULL
16052 so protect with -fdelete-null-pointer-checks; but not variables
16053 allocated on the stack. */
16055 && (flag_delete_null_pointer_checks
16056 || (DECL_CONTEXT (base
)
16057 && TREE_CODE (DECL_CONTEXT (base
)) == FUNCTION_DECL
16058 && auto_var_in_fn_p (base
, DECL_CONTEXT (base
)))))
16059 return !VAR_OR_FUNCTION_DECL_P (base
) || !DECL_WEAK (base
);
16061 /* Constants are never weak. */
16062 if (CONSTANT_CLASS_P (base
))
16069 sub_strict_overflow_p
= false;
16070 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
16071 &sub_strict_overflow_p
)
16072 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
16073 &sub_strict_overflow_p
))
16075 if (sub_strict_overflow_p
)
16076 *strict_overflow_p
= true;
16087 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
16088 attempt to fold the expression to a constant without modifying TYPE,
16091 If the expression could be simplified to a constant, then return
16092 the constant. If the expression would not be simplified to a
16093 constant, then return NULL_TREE. */
16096 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
16098 tree tem
= fold_binary (code
, type
, op0
, op1
);
16099 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
16102 /* Given the components of a unary expression CODE, TYPE and OP0,
16103 attempt to fold the expression to a constant without modifying
16106 If the expression could be simplified to a constant, then return
16107 the constant. If the expression would not be simplified to a
16108 constant, then return NULL_TREE. */
16111 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
16113 tree tem
= fold_unary (code
, type
, op0
);
16114 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
16117 /* If EXP represents referencing an element in a constant string
16118 (either via pointer arithmetic or array indexing), return the
16119 tree representing the value accessed, otherwise return NULL. */
16122 fold_read_from_constant_string (tree exp
)
16124 if ((TREE_CODE (exp
) == INDIRECT_REF
16125 || TREE_CODE (exp
) == ARRAY_REF
)
16126 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
16128 tree exp1
= TREE_OPERAND (exp
, 0);
16131 location_t loc
= EXPR_LOCATION (exp
);
16133 if (TREE_CODE (exp
) == INDIRECT_REF
)
16134 string
= string_constant (exp1
, &index
);
16137 tree low_bound
= array_ref_low_bound (exp
);
16138 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
16140 /* Optimize the special-case of a zero lower bound.
16142 We convert the low_bound to sizetype to avoid some problems
16143 with constant folding. (E.g. suppose the lower bound is 1,
16144 and its mode is QI. Without the conversion,l (ARRAY
16145 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
16146 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
16147 if (! integer_zerop (low_bound
))
16148 index
= size_diffop_loc (loc
, index
,
16149 fold_convert_loc (loc
, sizetype
, low_bound
));
16155 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
16156 && TREE_CODE (string
) == STRING_CST
16157 && TREE_CODE (index
) == INTEGER_CST
16158 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
16159 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
16161 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
16162 return build_int_cst_type (TREE_TYPE (exp
),
16163 (TREE_STRING_POINTER (string
)
16164 [TREE_INT_CST_LOW (index
)]));
16169 /* Return the tree for neg (ARG0) when ARG0 is known to be either
16170 an integer constant, real, or fixed-point constant.
16172 TYPE is the type of the result. */
16175 fold_negate_const (tree arg0
, tree type
)
16177 tree t
= NULL_TREE
;
16179 switch (TREE_CODE (arg0
))
16184 wide_int val
= wi::neg (arg0
, &overflow
);
16185 t
= force_fit_type (type
, val
, 1,
16186 (overflow
| TREE_OVERFLOW (arg0
))
16187 && !TYPE_UNSIGNED (type
));
16192 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16197 FIXED_VALUE_TYPE f
;
16198 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
16199 &(TREE_FIXED_CST (arg0
)), NULL
,
16200 TYPE_SATURATING (type
));
16201 t
= build_fixed (type
, f
);
16202 /* Propagate overflow flags. */
16203 if (overflow_p
| TREE_OVERFLOW (arg0
))
16204 TREE_OVERFLOW (t
) = 1;
16209 gcc_unreachable ();
16215 /* Return the tree for abs (ARG0) when ARG0 is known to be either
16216 an integer constant or real constant.
16218 TYPE is the type of the result. */
16221 fold_abs_const (tree arg0
, tree type
)
16223 tree t
= NULL_TREE
;
16225 switch (TREE_CODE (arg0
))
16229 /* If the value is unsigned or non-negative, then the absolute value
16230 is the same as the ordinary value. */
16231 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
16234 /* If the value is negative, then the absolute value is
16239 wide_int val
= wi::neg (arg0
, &overflow
);
16240 t
= force_fit_type (type
, val
, -1,
16241 overflow
| TREE_OVERFLOW (arg0
));
16247 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
16248 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
16254 gcc_unreachable ();
16260 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
16261 constant. TYPE is the type of the result. */
16264 fold_not_const (const_tree arg0
, tree type
)
16266 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
16268 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
16271 /* Given CODE, a relational operator, the target type, TYPE and two
16272 constant operands OP0 and OP1, return the result of the
16273 relational operation. If the result is not a compile time
16274 constant, then return NULL_TREE. */
16277 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
16279 int result
, invert
;
16281 /* From here on, the only cases we handle are when the result is
16282 known to be a constant. */
16284 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
16286 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
16287 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
16289 /* Handle the cases where either operand is a NaN. */
16290 if (real_isnan (c0
) || real_isnan (c1
))
16300 case UNORDERED_EXPR
:
16314 if (flag_trapping_math
)
16320 gcc_unreachable ();
16323 return constant_boolean_node (result
, type
);
16326 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
16329 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
16331 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
16332 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
16333 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
16336 /* Handle equality/inequality of complex constants. */
16337 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
16339 tree rcond
= fold_relational_const (code
, type
,
16340 TREE_REALPART (op0
),
16341 TREE_REALPART (op1
));
16342 tree icond
= fold_relational_const (code
, type
,
16343 TREE_IMAGPART (op0
),
16344 TREE_IMAGPART (op1
));
16345 if (code
== EQ_EXPR
)
16346 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
16347 else if (code
== NE_EXPR
)
16348 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
16353 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
16355 unsigned count
= VECTOR_CST_NELTS (op0
);
16356 tree
*elts
= XALLOCAVEC (tree
, count
);
16357 gcc_assert (VECTOR_CST_NELTS (op1
) == count
16358 && TYPE_VECTOR_SUBPARTS (type
) == count
);
16360 for (unsigned i
= 0; i
< count
; i
++)
16362 tree elem_type
= TREE_TYPE (type
);
16363 tree elem0
= VECTOR_CST_ELT (op0
, i
);
16364 tree elem1
= VECTOR_CST_ELT (op1
, i
);
16366 tree tem
= fold_relational_const (code
, elem_type
,
16369 if (tem
== NULL_TREE
)
16372 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
16375 return build_vector (type
, elts
);
16378 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16380 To compute GT, swap the arguments and do LT.
16381 To compute GE, do LT and invert the result.
16382 To compute LE, swap the arguments, do LT and invert the result.
16383 To compute NE, do EQ and invert the result.
16385 Therefore, the code below must handle only EQ and LT. */
16387 if (code
== LE_EXPR
|| code
== GT_EXPR
)
16392 code
= swap_tree_comparison (code
);
16395 /* Note that it is safe to invert for real values here because we
16396 have already handled the one case that it matters. */
16399 if (code
== NE_EXPR
|| code
== GE_EXPR
)
16402 code
= invert_tree_comparison (code
, false);
16405 /* Compute a result for LT or EQ if args permit;
16406 Otherwise return T. */
16407 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
16409 if (code
== EQ_EXPR
)
16410 result
= tree_int_cst_equal (op0
, op1
);
16412 result
= INT_CST_LT (op0
, op1
);
16419 return constant_boolean_node (result
, type
);
16422 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16423 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16427 fold_build_cleanup_point_expr (tree type
, tree expr
)
16429 /* If the expression does not have side effects then we don't have to wrap
16430 it with a cleanup point expression. */
16431 if (!TREE_SIDE_EFFECTS (expr
))
16434 /* If the expression is a return, check to see if the expression inside the
16435 return has no side effects or the right hand side of the modify expression
16436 inside the return. If either don't have side effects set we don't need to
16437 wrap the expression in a cleanup point expression. Note we don't check the
16438 left hand side of the modify because it should always be a return decl. */
16439 if (TREE_CODE (expr
) == RETURN_EXPR
)
16441 tree op
= TREE_OPERAND (expr
, 0);
16442 if (!op
|| !TREE_SIDE_EFFECTS (op
))
16444 op
= TREE_OPERAND (op
, 1);
16445 if (!TREE_SIDE_EFFECTS (op
))
16449 return build1 (CLEANUP_POINT_EXPR
, type
, expr
);
16452 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16453 of an indirection through OP0, or NULL_TREE if no simplification is
16457 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
16463 subtype
= TREE_TYPE (sub
);
16464 if (!POINTER_TYPE_P (subtype
))
16467 if (TREE_CODE (sub
) == ADDR_EXPR
)
16469 tree op
= TREE_OPERAND (sub
, 0);
16470 tree optype
= TREE_TYPE (op
);
16471 /* *&CONST_DECL -> to the value of the const decl. */
16472 if (TREE_CODE (op
) == CONST_DECL
)
16473 return DECL_INITIAL (op
);
16474 /* *&p => p; make sure to handle *&"str"[cst] here. */
16475 if (type
== optype
)
16477 tree fop
= fold_read_from_constant_string (op
);
16483 /* *(foo *)&fooarray => fooarray[0] */
16484 else if (TREE_CODE (optype
) == ARRAY_TYPE
16485 && type
== TREE_TYPE (optype
)
16486 && (!in_gimple_form
16487 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16489 tree type_domain
= TYPE_DOMAIN (optype
);
16490 tree min_val
= size_zero_node
;
16491 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16492 min_val
= TYPE_MIN_VALUE (type_domain
);
16494 && TREE_CODE (min_val
) != INTEGER_CST
)
16496 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
16497 NULL_TREE
, NULL_TREE
);
16499 /* *(foo *)&complexfoo => __real__ complexfoo */
16500 else if (TREE_CODE (optype
) == COMPLEX_TYPE
16501 && type
== TREE_TYPE (optype
))
16502 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
16503 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16504 else if (TREE_CODE (optype
) == VECTOR_TYPE
16505 && type
== TREE_TYPE (optype
))
16507 tree part_width
= TYPE_SIZE (type
);
16508 tree index
= bitsize_int (0);
16509 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
16513 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
16514 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
16516 tree op00
= TREE_OPERAND (sub
, 0);
16517 tree op01
= TREE_OPERAND (sub
, 1);
16520 if (TREE_CODE (op00
) == ADDR_EXPR
)
16523 op00
= TREE_OPERAND (op00
, 0);
16524 op00type
= TREE_TYPE (op00
);
16526 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16527 if (TREE_CODE (op00type
) == VECTOR_TYPE
16528 && type
== TREE_TYPE (op00type
))
16530 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
16531 tree part_width
= TYPE_SIZE (type
);
16532 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
16533 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
16534 tree index
= bitsize_int (indexi
);
16536 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
16537 return fold_build3_loc (loc
,
16538 BIT_FIELD_REF
, type
, op00
,
16539 part_width
, index
);
16542 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16543 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
16544 && type
== TREE_TYPE (op00type
))
16546 tree size
= TYPE_SIZE_UNIT (type
);
16547 if (tree_int_cst_equal (size
, op01
))
16548 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
16550 /* ((foo *)&fooarray)[1] => fooarray[1] */
16551 else if (TREE_CODE (op00type
) == ARRAY_TYPE
16552 && type
== TREE_TYPE (op00type
))
16554 tree type_domain
= TYPE_DOMAIN (op00type
);
16555 tree min_val
= size_zero_node
;
16556 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16557 min_val
= TYPE_MIN_VALUE (type_domain
);
16558 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
16559 TYPE_SIZE_UNIT (type
));
16560 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
16561 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
16562 NULL_TREE
, NULL_TREE
);
16567 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16568 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
16569 && type
== TREE_TYPE (TREE_TYPE (subtype
))
16570 && (!in_gimple_form
16571 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
16574 tree min_val
= size_zero_node
;
16575 sub
= build_fold_indirect_ref_loc (loc
, sub
);
16576 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
16577 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
16578 min_val
= TYPE_MIN_VALUE (type_domain
);
16580 && TREE_CODE (min_val
) != INTEGER_CST
)
16582 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
16589 /* Builds an expression for an indirection through T, simplifying some
16593 build_fold_indirect_ref_loc (location_t loc
, tree t
)
16595 tree type
= TREE_TYPE (TREE_TYPE (t
));
16596 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
16601 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
16604 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16607 fold_indirect_ref_loc (location_t loc
, tree t
)
16609 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
16617 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16618 whose result is ignored. The type of the returned tree need not be
16619 the same as the original expression. */
16622 fold_ignored_result (tree t
)
16624 if (!TREE_SIDE_EFFECTS (t
))
16625 return integer_zero_node
;
16628 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
16631 t
= TREE_OPERAND (t
, 0);
16635 case tcc_comparison
:
16636 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16637 t
= TREE_OPERAND (t
, 0);
16638 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
16639 t
= TREE_OPERAND (t
, 1);
16644 case tcc_expression
:
16645 switch (TREE_CODE (t
))
16647 case COMPOUND_EXPR
:
16648 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
16650 t
= TREE_OPERAND (t
, 0);
16654 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
16655 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
16657 t
= TREE_OPERAND (t
, 0);
16670 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16673 round_up_loc (location_t loc
, tree value
, int divisor
)
16675 tree div
= NULL_TREE
;
16677 gcc_assert (divisor
> 0);
16681 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16682 have to do anything. Only do this when we are not given a const,
16683 because in that case, this check is more expensive than just
16685 if (TREE_CODE (value
) != INTEGER_CST
)
16687 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16689 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16693 /* If divisor is a power of two, simplify this to bit manipulation. */
16694 if (divisor
== (divisor
& -divisor
))
16696 if (TREE_CODE (value
) == INTEGER_CST
)
16698 wide_int val
= value
;
16701 if ((val
& (divisor
- 1)) == 0)
16704 overflow_p
= TREE_OVERFLOW (value
);
16705 val
&= ~(divisor
- 1);
16710 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
16716 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
16717 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
16718 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16719 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16725 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16726 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
16727 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16733 /* Likewise, but round down. */
16736 round_down_loc (location_t loc
, tree value
, int divisor
)
16738 tree div
= NULL_TREE
;
16740 gcc_assert (divisor
> 0);
16744 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16745 have to do anything. Only do this when we are not given a const,
16746 because in that case, this check is more expensive than just
16748 if (TREE_CODE (value
) != INTEGER_CST
)
16750 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16752 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
16756 /* If divisor is a power of two, simplify this to bit manipulation. */
16757 if (divisor
== (divisor
& -divisor
))
16761 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
16762 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
16767 div
= build_int_cst (TREE_TYPE (value
), divisor
);
16768 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
16769 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
16775 /* Returns the pointer to the base of the object addressed by EXP and
16776 extracts the information about the offset of the access, storing it
16777 to PBITPOS and POFFSET. */
16780 split_address_to_core_and_offset (tree exp
,
16781 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
16784 enum machine_mode mode
;
16785 int unsignedp
, volatilep
;
16786 HOST_WIDE_INT bitsize
;
16787 location_t loc
= EXPR_LOCATION (exp
);
16789 if (TREE_CODE (exp
) == ADDR_EXPR
)
16791 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
16792 poffset
, &mode
, &unsignedp
, &volatilep
,
16794 core
= build_fold_addr_expr_loc (loc
, core
);
16800 *poffset
= NULL_TREE
;
16806 /* Returns true if addresses of E1 and E2 differ by a constant, false
16807 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16810 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
16813 HOST_WIDE_INT bitpos1
, bitpos2
;
16814 tree toffset1
, toffset2
, tdiff
, type
;
16816 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
16817 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
16819 if (bitpos1
% BITS_PER_UNIT
!= 0
16820 || bitpos2
% BITS_PER_UNIT
!= 0
16821 || !operand_equal_p (core1
, core2
, 0))
16824 if (toffset1
&& toffset2
)
16826 type
= TREE_TYPE (toffset1
);
16827 if (type
!= TREE_TYPE (toffset2
))
16828 toffset2
= fold_convert (type
, toffset2
);
16830 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
16831 if (!cst_and_fits_in_hwi (tdiff
))
16834 *diff
= int_cst_value (tdiff
);
16836 else if (toffset1
|| toffset2
)
16838 /* If only one of the offsets is non-constant, the difference cannot
16845 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
16849 /* Simplify the floating point expression EXP when the sign of the
16850 result is not significant. Return NULL_TREE if no simplification
16854 fold_strip_sign_ops (tree exp
)
16857 location_t loc
= EXPR_LOCATION (exp
);
16859 switch (TREE_CODE (exp
))
16863 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 0));
16864 return arg0
? arg0
: TREE_OPERAND (exp
, 0);
16868 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp
))))
16870 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 0));
16871 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
16872 if (arg0
!= NULL_TREE
|| arg1
!= NULL_TREE
)
16873 return fold_build2_loc (loc
, TREE_CODE (exp
), TREE_TYPE (exp
),
16874 arg0
? arg0
: TREE_OPERAND (exp
, 0),
16875 arg1
? arg1
: TREE_OPERAND (exp
, 1));
16878 case COMPOUND_EXPR
:
16879 arg0
= TREE_OPERAND (exp
, 0);
16880 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
16882 return fold_build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (exp
), arg0
, arg1
);
16886 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
16887 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 2));
16889 return fold_build3_loc (loc
,
16890 COND_EXPR
, TREE_TYPE (exp
), TREE_OPERAND (exp
, 0),
16891 arg0
? arg0
: TREE_OPERAND (exp
, 1),
16892 arg1
? arg1
: TREE_OPERAND (exp
, 2));
16897 const enum built_in_function fcode
= builtin_mathfn_code (exp
);
16900 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
16901 /* Strip copysign function call, return the 1st argument. */
16902 arg0
= CALL_EXPR_ARG (exp
, 0);
16903 arg1
= CALL_EXPR_ARG (exp
, 1);
16904 return omit_one_operand_loc (loc
, TREE_TYPE (exp
), arg0
, arg1
);
16907 /* Strip sign ops from the argument of "odd" math functions. */
16908 if (negate_mathfn_p (fcode
))
16910 arg0
= fold_strip_sign_ops (CALL_EXPR_ARG (exp
, 0));
16912 return build_call_expr_loc (loc
, get_callee_fndecl (exp
), 1, arg0
);