1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2015 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"
51 #include "fold-const.h"
52 #include "stor-layout.h"
54 #include "tree-iterator.h"
57 #include "hard-reg-set.h"
59 #include "insn-config.h"
69 #include "diagnostic-core.h"
71 #include "langhooks.h"
74 #include "basic-block.h"
75 #include "tree-ssa-alias.h"
76 #include "internal-fn.h"
78 #include "gimple-expr.h"
84 #include "generic-match.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
89 int folding_initializer
= 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code
{
113 static bool negate_mathfn_p (enum built_in_function
);
114 static bool negate_expr_p (tree
);
115 static tree
negate_expr (tree
);
116 static tree
split_tree (tree
, enum tree_code
, tree
*, tree
*, tree
*, int);
117 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
118 static enum comparison_code
comparison_to_compcode (enum tree_code
);
119 static enum tree_code
compcode_to_comparison (enum comparison_code
);
120 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
121 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
122 static tree
eval_subst (location_t
, tree
, tree
, tree
, tree
, tree
);
123 static tree
distribute_bit_expr (location_t
, enum tree_code
, tree
, tree
, tree
);
124 static tree
make_bit_field_ref (location_t
, tree
, tree
,
125 HOST_WIDE_INT
, HOST_WIDE_INT
, int);
126 static tree
optimize_bit_field_compare (location_t
, enum tree_code
,
128 static tree
decode_field_reference (location_t
, tree
, HOST_WIDE_INT
*,
130 machine_mode
*, int *, int *,
132 static int simple_operand_p (const_tree
);
133 static bool simple_operand_p_2 (tree
);
134 static tree
range_binop (enum tree_code
, tree
, tree
, int, tree
, int);
135 static tree
range_predecessor (tree
);
136 static tree
range_successor (tree
);
137 static tree
fold_range_test (location_t
, enum tree_code
, tree
, tree
, tree
);
138 static tree
fold_cond_expr_with_comparison (location_t
, tree
, tree
, tree
, tree
);
139 static tree
unextend (tree
, int, int, tree
);
140 static tree
optimize_minmax_comparison (location_t
, enum tree_code
,
142 static tree
extract_muldiv (tree
, tree
, enum tree_code
, tree
, bool *);
143 static tree
extract_muldiv_1 (tree
, tree
, enum tree_code
, tree
, bool *);
144 static tree
fold_binary_op_with_conditional_arg (location_t
,
145 enum tree_code
, tree
,
148 static tree
fold_mathfn_compare (location_t
,
149 enum built_in_function
, enum tree_code
,
151 static tree
fold_inf_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
152 static tree
fold_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
153 static bool reorder_operands_p (const_tree
, const_tree
);
154 static tree
fold_negate_const (tree
, tree
);
155 static tree
fold_not_const (const_tree
, tree
);
156 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
157 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
158 static tree
fold_view_convert_expr (tree
, tree
);
159 static bool vec_cst_ctor_to_array (tree
, tree
*);
162 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
163 Otherwise, return LOC. */
166 expr_location_or (tree t
, location_t loc
)
168 location_t tloc
= EXPR_LOCATION (t
);
169 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
172 /* Similar to protected_set_expr_location, but never modify x in place,
173 if location can and needs to be set, unshare it. */
176 protected_set_expr_location_unshare (tree x
, location_t loc
)
178 if (CAN_HAVE_LOCATION_P (x
)
179 && EXPR_LOCATION (x
) != loc
180 && !(TREE_CODE (x
) == SAVE_EXPR
181 || TREE_CODE (x
) == TARGET_EXPR
182 || TREE_CODE (x
) == BIND_EXPR
))
185 SET_EXPR_LOCATION (x
, loc
);
190 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
191 division and returns the quotient. Otherwise returns
195 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
199 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
201 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
206 /* This is nonzero if we should defer warnings about undefined
207 overflow. This facility exists because these warnings are a
208 special case. The code to estimate loop iterations does not want
209 to issue any warnings, since it works with expressions which do not
210 occur in user code. Various bits of cleanup code call fold(), but
211 only use the result if it has certain characteristics (e.g., is a
212 constant); that code only wants to issue a warning if the result is
215 static int fold_deferring_overflow_warnings
;
217 /* If a warning about undefined overflow is deferred, this is the
218 warning. Note that this may cause us to turn two warnings into
219 one, but that is fine since it is sufficient to only give one
220 warning per expression. */
222 static const char* fold_deferred_overflow_warning
;
224 /* If a warning about undefined overflow is deferred, this is the
225 level at which the warning should be emitted. */
227 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
229 /* Start deferring overflow warnings. We could use a stack here to
230 permit nested calls, but at present it is not necessary. */
233 fold_defer_overflow_warnings (void)
235 ++fold_deferring_overflow_warnings
;
238 /* Stop deferring overflow warnings. If there is a pending warning,
239 and ISSUE is true, then issue the warning if appropriate. STMT is
240 the statement with which the warning should be associated (used for
241 location information); STMT may be NULL. CODE is the level of the
242 warning--a warn_strict_overflow_code value. This function will use
243 the smaller of CODE and the deferred code when deciding whether to
244 issue the warning. CODE may be zero to mean to always use the
248 fold_undefer_overflow_warnings (bool issue
, const_gimple stmt
, int code
)
253 gcc_assert (fold_deferring_overflow_warnings
> 0);
254 --fold_deferring_overflow_warnings
;
255 if (fold_deferring_overflow_warnings
> 0)
257 if (fold_deferred_overflow_warning
!= NULL
259 && code
< (int) fold_deferred_overflow_code
)
260 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
264 warnmsg
= fold_deferred_overflow_warning
;
265 fold_deferred_overflow_warning
= NULL
;
267 if (!issue
|| warnmsg
== NULL
)
270 if (gimple_no_warning_p (stmt
))
273 /* Use the smallest code level when deciding to issue the
275 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
276 code
= fold_deferred_overflow_code
;
278 if (!issue_strict_overflow_warning (code
))
282 locus
= input_location
;
284 locus
= gimple_location (stmt
);
285 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
288 /* Stop deferring overflow warnings, ignoring any deferred
292 fold_undefer_and_ignore_overflow_warnings (void)
294 fold_undefer_overflow_warnings (false, NULL
, 0);
297 /* Whether we are deferring overflow warnings. */
300 fold_deferring_overflow_warnings_p (void)
302 return fold_deferring_overflow_warnings
> 0;
305 /* This is called when we fold something based on the fact that signed
306 overflow is undefined. */
309 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
311 if (fold_deferring_overflow_warnings
> 0)
313 if (fold_deferred_overflow_warning
== NULL
314 || wc
< fold_deferred_overflow_code
)
316 fold_deferred_overflow_warning
= gmsgid
;
317 fold_deferred_overflow_code
= wc
;
320 else if (issue_strict_overflow_warning (wc
))
321 warning (OPT_Wstrict_overflow
, gmsgid
);
324 /* Return true if the built-in mathematical function specified by CODE
325 is odd, i.e. -f(x) == f(-x). */
328 negate_mathfn_p (enum built_in_function code
)
332 CASE_FLT_FN (BUILT_IN_ASIN
):
333 CASE_FLT_FN (BUILT_IN_ASINH
):
334 CASE_FLT_FN (BUILT_IN_ATAN
):
335 CASE_FLT_FN (BUILT_IN_ATANH
):
336 CASE_FLT_FN (BUILT_IN_CASIN
):
337 CASE_FLT_FN (BUILT_IN_CASINH
):
338 CASE_FLT_FN (BUILT_IN_CATAN
):
339 CASE_FLT_FN (BUILT_IN_CATANH
):
340 CASE_FLT_FN (BUILT_IN_CBRT
):
341 CASE_FLT_FN (BUILT_IN_CPROJ
):
342 CASE_FLT_FN (BUILT_IN_CSIN
):
343 CASE_FLT_FN (BUILT_IN_CSINH
):
344 CASE_FLT_FN (BUILT_IN_CTAN
):
345 CASE_FLT_FN (BUILT_IN_CTANH
):
346 CASE_FLT_FN (BUILT_IN_ERF
):
347 CASE_FLT_FN (BUILT_IN_LLROUND
):
348 CASE_FLT_FN (BUILT_IN_LROUND
):
349 CASE_FLT_FN (BUILT_IN_ROUND
):
350 CASE_FLT_FN (BUILT_IN_SIN
):
351 CASE_FLT_FN (BUILT_IN_SINH
):
352 CASE_FLT_FN (BUILT_IN_TAN
):
353 CASE_FLT_FN (BUILT_IN_TANH
):
354 CASE_FLT_FN (BUILT_IN_TRUNC
):
357 CASE_FLT_FN (BUILT_IN_LLRINT
):
358 CASE_FLT_FN (BUILT_IN_LRINT
):
359 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
360 CASE_FLT_FN (BUILT_IN_RINT
):
361 return !flag_rounding_math
;
369 /* Check whether we may negate an integer constant T without causing
373 may_negate_without_overflow_p (const_tree t
)
377 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
379 type
= TREE_TYPE (t
);
380 if (TYPE_UNSIGNED (type
))
383 return !wi::only_sign_bit_p (t
);
386 /* Determine whether an expression T can be cheaply negated using
387 the function negate_expr without introducing undefined overflow. */
390 negate_expr_p (tree t
)
397 type
= TREE_TYPE (t
);
400 switch (TREE_CODE (t
))
403 if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
406 /* Check that -CST will not overflow type. */
407 return may_negate_without_overflow_p (t
);
409 return (INTEGRAL_TYPE_P (type
)
410 && TYPE_OVERFLOW_WRAPS (type
));
416 return !TYPE_OVERFLOW_SANITIZED (type
);
419 /* We want to canonicalize to positive real constants. Pretend
420 that only negative ones can be easily negated. */
421 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
424 return negate_expr_p (TREE_REALPART (t
))
425 && negate_expr_p (TREE_IMAGPART (t
));
429 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
432 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
434 for (i
= 0; i
< count
; i
++)
435 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
442 return negate_expr_p (TREE_OPERAND (t
, 0))
443 && negate_expr_p (TREE_OPERAND (t
, 1));
446 return negate_expr_p (TREE_OPERAND (t
, 0));
449 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
450 || HONOR_SIGNED_ZEROS (element_mode (type
)))
452 /* -(A + B) -> (-B) - A. */
453 if (negate_expr_p (TREE_OPERAND (t
, 1))
454 && reorder_operands_p (TREE_OPERAND (t
, 0),
455 TREE_OPERAND (t
, 1)))
457 /* -(A + B) -> (-A) - B. */
458 return negate_expr_p (TREE_OPERAND (t
, 0));
461 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
462 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
463 && !HONOR_SIGNED_ZEROS (element_mode (type
))
464 && reorder_operands_p (TREE_OPERAND (t
, 0),
465 TREE_OPERAND (t
, 1));
468 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
474 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
475 return negate_expr_p (TREE_OPERAND (t
, 1))
476 || negate_expr_p (TREE_OPERAND (t
, 0));
482 /* In general we can't negate A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. But if overflow is
485 undefined, we can negate, because - (INT_MIN / 1) is an
487 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
489 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
)))
491 /* If overflow is undefined then we have to be careful because
492 we ask whether it's ok to associate the negate with the
493 division which is not ok for example for
494 -((a - b) / c) where (-(a - b)) / c may invoke undefined
495 overflow because of negating INT_MIN. So do not use
496 negate_expr_p here but open-code the two important cases. */
497 if (TREE_CODE (TREE_OPERAND (t
, 0)) == NEGATE_EXPR
498 || (TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
499 && may_negate_without_overflow_p (TREE_OPERAND (t
, 0))))
502 else if (negate_expr_p (TREE_OPERAND (t
, 0)))
504 return negate_expr_p (TREE_OPERAND (t
, 1));
507 /* Negate -((double)float) as (double)(-float). */
508 if (TREE_CODE (type
) == REAL_TYPE
)
510 tree tem
= strip_float_extensions (t
);
512 return negate_expr_p (tem
);
517 /* Negate -f(x) as f(-x). */
518 if (negate_mathfn_p (builtin_mathfn_code (t
)))
519 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
523 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
524 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
526 tree op1
= TREE_OPERAND (t
, 1);
527 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
538 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
539 simplification is possible.
540 If negate_expr_p would return true for T, NULL_TREE will never be
544 fold_negate_expr (location_t loc
, tree t
)
546 tree type
= TREE_TYPE (t
);
549 switch (TREE_CODE (t
))
551 /* Convert - (~A) to A + 1. */
553 if (INTEGRAL_TYPE_P (type
))
554 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
555 build_one_cst (type
));
559 tem
= fold_negate_const (t
, type
);
560 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
561 || (ANY_INTEGRAL_TYPE_P (type
)
562 && !TYPE_OVERFLOW_TRAPS (type
)
563 && TYPE_OVERFLOW_WRAPS (type
))
564 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
569 tem
= fold_negate_const (t
, type
);
573 tem
= fold_negate_const (t
, type
);
578 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
579 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
581 return build_complex (type
, rpart
, ipart
);
587 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
588 tree
*elts
= XALLOCAVEC (tree
, count
);
590 for (i
= 0; i
< count
; i
++)
592 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
593 if (elts
[i
] == NULL_TREE
)
597 return build_vector (type
, elts
);
601 if (negate_expr_p (t
))
602 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
603 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
604 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
608 if (negate_expr_p (t
))
609 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
610 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
614 if (!TYPE_OVERFLOW_SANITIZED (type
))
615 return TREE_OPERAND (t
, 0);
619 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
620 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
622 /* -(A + B) -> (-B) - A. */
623 if (negate_expr_p (TREE_OPERAND (t
, 1))
624 && reorder_operands_p (TREE_OPERAND (t
, 0),
625 TREE_OPERAND (t
, 1)))
627 tem
= negate_expr (TREE_OPERAND (t
, 1));
628 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
629 tem
, TREE_OPERAND (t
, 0));
632 /* -(A + B) -> (-A) - B. */
633 if (negate_expr_p (TREE_OPERAND (t
, 0)))
635 tem
= negate_expr (TREE_OPERAND (t
, 0));
636 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
637 tem
, TREE_OPERAND (t
, 1));
643 /* - (A - B) -> B - A */
644 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
645 && !HONOR_SIGNED_ZEROS (element_mode (type
))
646 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
647 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
648 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
652 if (TYPE_UNSIGNED (type
))
658 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
660 tem
= TREE_OPERAND (t
, 1);
661 if (negate_expr_p (tem
))
662 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
663 TREE_OPERAND (t
, 0), negate_expr (tem
));
664 tem
= TREE_OPERAND (t
, 0);
665 if (negate_expr_p (tem
))
666 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
667 negate_expr (tem
), TREE_OPERAND (t
, 1));
674 /* In general we can't negate A / B, because if A is INT_MIN and
675 B is 1, we may turn this into INT_MIN / -1 which is undefined
676 and actually traps on some architectures. But if overflow is
677 undefined, we can negate, because - (INT_MIN / 1) is an
679 if (!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
681 const char * const warnmsg
= G_("assuming signed overflow does not "
682 "occur when negating a division");
683 tem
= TREE_OPERAND (t
, 1);
684 if (negate_expr_p (tem
))
686 if (INTEGRAL_TYPE_P (type
)
687 && (TREE_CODE (tem
) != INTEGER_CST
688 || integer_onep (tem
)))
689 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MISC
);
690 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
691 TREE_OPERAND (t
, 0), negate_expr (tem
));
693 /* If overflow is undefined then we have to be careful because
694 we ask whether it's ok to associate the negate with the
695 division which is not ok for example for
696 -((a - b) / c) where (-(a - b)) / c may invoke undefined
697 overflow because of negating INT_MIN. So do not use
698 negate_expr_p here but open-code the two important cases. */
699 tem
= TREE_OPERAND (t
, 0);
700 if ((INTEGRAL_TYPE_P (type
)
701 && (TREE_CODE (tem
) == NEGATE_EXPR
702 || (TREE_CODE (tem
) == INTEGER_CST
703 && may_negate_without_overflow_p (tem
))))
704 || !INTEGRAL_TYPE_P (type
))
705 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
706 negate_expr (tem
), TREE_OPERAND (t
, 1));
711 /* Convert -((double)float) into (double)(-float). */
712 if (TREE_CODE (type
) == REAL_TYPE
)
714 tem
= strip_float_extensions (t
);
715 if (tem
!= t
&& negate_expr_p (tem
))
716 return fold_convert_loc (loc
, type
, negate_expr (tem
));
721 /* Negate -f(x) as f(-x). */
722 if (negate_mathfn_p (builtin_mathfn_code (t
))
723 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
727 fndecl
= get_callee_fndecl (t
);
728 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
729 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
734 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
735 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
737 tree op1
= TREE_OPERAND (t
, 1);
738 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
740 tree ntype
= TYPE_UNSIGNED (type
)
741 ? signed_type_for (type
)
742 : unsigned_type_for (type
);
743 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
744 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
745 return fold_convert_loc (loc
, type
, temp
);
757 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
758 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
770 loc
= EXPR_LOCATION (t
);
771 type
= TREE_TYPE (t
);
774 tem
= fold_negate_expr (loc
, t
);
776 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
777 return fold_convert_loc (loc
, type
, tem
);
780 /* Split a tree IN into a constant, literal and variable parts that could be
781 combined with CODE to make IN. "constant" means an expression with
782 TREE_CONSTANT but that isn't an actual constant. CODE must be a
783 commutative arithmetic operation. Store the constant part into *CONP,
784 the literal in *LITP and return the variable part. If a part isn't
785 present, set it to null. If the tree does not decompose in this way,
786 return the entire tree as the variable part and the other parts as null.
788 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
789 case, we negate an operand that was subtracted. Except if it is a
790 literal for which we use *MINUS_LITP instead.
792 If NEGATE_P is true, we are negating all of IN, again except a literal
793 for which we use *MINUS_LITP instead.
795 If IN is itself a literal or constant, return it as appropriate.
797 Note that we do not guarantee that any of the three values will be the
798 same type as IN, but they will have the same signedness and mode. */
801 split_tree (tree in
, enum tree_code code
, tree
*conp
, tree
*litp
,
802 tree
*minus_litp
, int negate_p
)
810 /* Strip any conversions that don't change the machine mode or signedness. */
811 STRIP_SIGN_NOPS (in
);
813 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
814 || TREE_CODE (in
) == FIXED_CST
)
816 else if (TREE_CODE (in
) == code
817 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
818 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
819 /* We can associate addition and subtraction together (even
820 though the C standard doesn't say so) for integers because
821 the value is not affected. For reals, the value might be
822 affected, so we can't. */
823 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
824 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
826 tree op0
= TREE_OPERAND (in
, 0);
827 tree op1
= TREE_OPERAND (in
, 1);
828 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
829 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
831 /* First see if either of the operands is a literal, then a constant. */
832 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
833 || TREE_CODE (op0
) == FIXED_CST
)
834 *litp
= op0
, op0
= 0;
835 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
836 || TREE_CODE (op1
) == FIXED_CST
)
837 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
839 if (op0
!= 0 && TREE_CONSTANT (op0
))
840 *conp
= op0
, op0
= 0;
841 else if (op1
!= 0 && TREE_CONSTANT (op1
))
842 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
844 /* If we haven't dealt with either operand, this is not a case we can
845 decompose. Otherwise, VAR is either of the ones remaining, if any. */
846 if (op0
!= 0 && op1
!= 0)
851 var
= op1
, neg_var_p
= neg1_p
;
853 /* Now do any needed negations. */
855 *minus_litp
= *litp
, *litp
= 0;
857 *conp
= negate_expr (*conp
);
859 var
= negate_expr (var
);
861 else if (TREE_CODE (in
) == BIT_NOT_EXPR
862 && code
== PLUS_EXPR
)
864 /* -X - 1 is folded to ~X, undo that here. */
865 *minus_litp
= build_one_cst (TREE_TYPE (in
));
866 var
= negate_expr (TREE_OPERAND (in
, 0));
868 else if (TREE_CONSTANT (in
))
876 *minus_litp
= *litp
, *litp
= 0;
877 else if (*minus_litp
)
878 *litp
= *minus_litp
, *minus_litp
= 0;
879 *conp
= negate_expr (*conp
);
880 var
= negate_expr (var
);
886 /* Re-associate trees split by the above function. T1 and T2 are
887 either expressions to associate or null. Return the new
888 expression, if any. LOC is the location of the new expression. If
889 we build an operation, do it in TYPE and with CODE. */
892 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
903 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
905 if (code
== PLUS_EXPR
)
907 if (TREE_CODE (t1
) == NEGATE_EXPR
)
908 return build2_loc (loc
, MINUS_EXPR
, type
,
909 fold_convert_loc (loc
, type
, t2
),
910 fold_convert_loc (loc
, type
,
911 TREE_OPERAND (t1
, 0)));
912 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
913 return build2_loc (loc
, MINUS_EXPR
, type
,
914 fold_convert_loc (loc
, type
, t1
),
915 fold_convert_loc (loc
, type
,
916 TREE_OPERAND (t2
, 0)));
917 else if (integer_zerop (t2
))
918 return fold_convert_loc (loc
, type
, t1
);
920 else if (code
== MINUS_EXPR
)
922 if (integer_zerop (t2
))
923 return fold_convert_loc (loc
, type
, t1
);
926 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
927 fold_convert_loc (loc
, type
, t2
));
930 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
931 fold_convert_loc (loc
, type
, t2
));
934 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
935 for use in int_const_binop, size_binop and size_diffop. */
938 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
940 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
942 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
957 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
958 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
959 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
963 /* Combine two integer constants ARG1 and ARG2 under operation CODE
964 to produce a new constant. Return NULL_TREE if we don't know how
965 to evaluate CODE at compile-time. */
968 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
973 tree type
= TREE_TYPE (arg1
);
974 signop sign
= TYPE_SIGN (type
);
975 bool overflow
= false;
977 wide_int arg2
= wide_int::from (parg2
, TYPE_PRECISION (type
),
978 TYPE_SIGN (TREE_TYPE (parg2
)));
983 res
= wi::bit_or (arg1
, arg2
);
987 res
= wi::bit_xor (arg1
, arg2
);
991 res
= wi::bit_and (arg1
, arg2
);
996 if (wi::neg_p (arg2
))
999 if (code
== RSHIFT_EXPR
)
1005 if (code
== RSHIFT_EXPR
)
1006 /* It's unclear from the C standard whether shifts can overflow.
1007 The following code ignores overflow; perhaps a C standard
1008 interpretation ruling is needed. */
1009 res
= wi::rshift (arg1
, arg2
, sign
);
1011 res
= wi::lshift (arg1
, arg2
);
1016 if (wi::neg_p (arg2
))
1019 if (code
== RROTATE_EXPR
)
1020 code
= LROTATE_EXPR
;
1022 code
= RROTATE_EXPR
;
1025 if (code
== RROTATE_EXPR
)
1026 res
= wi::rrotate (arg1
, arg2
);
1028 res
= wi::lrotate (arg1
, arg2
);
1032 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1036 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1040 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1043 case MULT_HIGHPART_EXPR
:
1044 res
= wi::mul_high (arg1
, arg2
, sign
);
1047 case TRUNC_DIV_EXPR
:
1048 case EXACT_DIV_EXPR
:
1051 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1054 case FLOOR_DIV_EXPR
:
1057 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1063 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1066 case ROUND_DIV_EXPR
:
1069 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1072 case TRUNC_MOD_EXPR
:
1075 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1078 case FLOOR_MOD_EXPR
:
1081 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1087 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1090 case ROUND_MOD_EXPR
:
1093 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1097 res
= wi::min (arg1
, arg2
, sign
);
1101 res
= wi::max (arg1
, arg2
, sign
);
1108 t
= force_fit_type (type
, res
, overflowable
,
1109 (((sign
== SIGNED
|| overflowable
== -1)
1111 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1117 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1119 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1122 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1123 constant. We assume ARG1 and ARG2 have the same data type, or at least
1124 are the same kind of constant and the same machine mode. Return zero if
1125 combining the constants is not allowed in the current operating mode. */
1128 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1130 /* Sanity check for the recursive cases. */
1137 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1139 if (code
== POINTER_PLUS_EXPR
)
1140 return int_const_binop (PLUS_EXPR
,
1141 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1143 return int_const_binop (code
, arg1
, arg2
);
1146 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1151 REAL_VALUE_TYPE value
;
1152 REAL_VALUE_TYPE result
;
1156 /* The following codes are handled by real_arithmetic. */
1171 d1
= TREE_REAL_CST (arg1
);
1172 d2
= TREE_REAL_CST (arg2
);
1174 type
= TREE_TYPE (arg1
);
1175 mode
= TYPE_MODE (type
);
1177 /* Don't perform operation if we honor signaling NaNs and
1178 either operand is a NaN. */
1179 if (HONOR_SNANS (mode
)
1180 && (REAL_VALUE_ISNAN (d1
) || REAL_VALUE_ISNAN (d2
)))
1183 /* Don't perform operation if it would raise a division
1184 by zero exception. */
1185 if (code
== RDIV_EXPR
1186 && REAL_VALUES_EQUAL (d2
, dconst0
)
1187 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1190 /* If either operand is a NaN, just return it. Otherwise, set up
1191 for floating-point trap; we return an overflow. */
1192 if (REAL_VALUE_ISNAN (d1
))
1194 else if (REAL_VALUE_ISNAN (d2
))
1197 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1198 real_convert (&result
, mode
, &value
);
1200 /* Don't constant fold this floating point operation if
1201 the result has overflowed and flag_trapping_math. */
1202 if (flag_trapping_math
1203 && MODE_HAS_INFINITIES (mode
)
1204 && REAL_VALUE_ISINF (result
)
1205 && !REAL_VALUE_ISINF (d1
)
1206 && !REAL_VALUE_ISINF (d2
))
1209 /* Don't constant fold this floating point operation if the
1210 result may dependent upon the run-time rounding mode and
1211 flag_rounding_math is set, or if GCC's software emulation
1212 is unable to accurately represent the result. */
1213 if ((flag_rounding_math
1214 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1215 && (inexact
|| !real_identical (&result
, &value
)))
1218 t
= build_real (type
, result
);
1220 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1224 if (TREE_CODE (arg1
) == FIXED_CST
)
1226 FIXED_VALUE_TYPE f1
;
1227 FIXED_VALUE_TYPE f2
;
1228 FIXED_VALUE_TYPE result
;
1233 /* The following codes are handled by fixed_arithmetic. */
1239 case TRUNC_DIV_EXPR
:
1240 if (TREE_CODE (arg2
) != FIXED_CST
)
1242 f2
= TREE_FIXED_CST (arg2
);
1248 if (TREE_CODE (arg2
) != INTEGER_CST
)
1251 f2
.data
.high
= w2
.elt (1);
1252 f2
.data
.low
= w2
.elt (0);
1261 f1
= TREE_FIXED_CST (arg1
);
1262 type
= TREE_TYPE (arg1
);
1263 sat_p
= TYPE_SATURATING (type
);
1264 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1265 t
= build_fixed (type
, result
);
1266 /* Propagate overflow flags. */
1267 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1268 TREE_OVERFLOW (t
) = 1;
1272 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1274 tree type
= TREE_TYPE (arg1
);
1275 tree r1
= TREE_REALPART (arg1
);
1276 tree i1
= TREE_IMAGPART (arg1
);
1277 tree r2
= TREE_REALPART (arg2
);
1278 tree i2
= TREE_IMAGPART (arg2
);
1285 real
= const_binop (code
, r1
, r2
);
1286 imag
= const_binop (code
, i1
, i2
);
1290 if (COMPLEX_FLOAT_TYPE_P (type
))
1291 return do_mpc_arg2 (arg1
, arg2
, type
,
1292 /* do_nonfinite= */ folding_initializer
,
1295 real
= const_binop (MINUS_EXPR
,
1296 const_binop (MULT_EXPR
, r1
, r2
),
1297 const_binop (MULT_EXPR
, i1
, i2
));
1298 imag
= const_binop (PLUS_EXPR
,
1299 const_binop (MULT_EXPR
, r1
, i2
),
1300 const_binop (MULT_EXPR
, i1
, r2
));
1304 if (COMPLEX_FLOAT_TYPE_P (type
))
1305 return do_mpc_arg2 (arg1
, arg2
, type
,
1306 /* do_nonfinite= */ folding_initializer
,
1309 case TRUNC_DIV_EXPR
:
1311 case FLOOR_DIV_EXPR
:
1312 case ROUND_DIV_EXPR
:
1313 if (flag_complex_method
== 0)
1315 /* Keep this algorithm in sync with
1316 tree-complex.c:expand_complex_div_straight().
1318 Expand complex division to scalars, straightforward algorithm.
1319 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1323 = const_binop (PLUS_EXPR
,
1324 const_binop (MULT_EXPR
, r2
, r2
),
1325 const_binop (MULT_EXPR
, i2
, i2
));
1327 = const_binop (PLUS_EXPR
,
1328 const_binop (MULT_EXPR
, r1
, r2
),
1329 const_binop (MULT_EXPR
, i1
, i2
));
1331 = const_binop (MINUS_EXPR
,
1332 const_binop (MULT_EXPR
, i1
, r2
),
1333 const_binop (MULT_EXPR
, r1
, i2
));
1335 real
= const_binop (code
, t1
, magsquared
);
1336 imag
= const_binop (code
, t2
, magsquared
);
1340 /* Keep this algorithm in sync with
1341 tree-complex.c:expand_complex_div_wide().
1343 Expand complex division to scalars, modified algorithm to minimize
1344 overflow with wide input ranges. */
1345 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1346 fold_abs_const (r2
, TREE_TYPE (type
)),
1347 fold_abs_const (i2
, TREE_TYPE (type
)));
1349 if (integer_nonzerop (compare
))
1351 /* In the TRUE branch, we compute
1353 div = (br * ratio) + bi;
1354 tr = (ar * ratio) + ai;
1355 ti = (ai * ratio) - ar;
1358 tree ratio
= const_binop (code
, r2
, i2
);
1359 tree div
= const_binop (PLUS_EXPR
, i2
,
1360 const_binop (MULT_EXPR
, r2
, ratio
));
1361 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1362 real
= const_binop (PLUS_EXPR
, real
, i1
);
1363 real
= const_binop (code
, real
, div
);
1365 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1366 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1367 imag
= const_binop (code
, imag
, div
);
1371 /* In the FALSE branch, we compute
1373 divisor = (d * ratio) + c;
1374 tr = (b * ratio) + a;
1375 ti = b - (a * ratio);
1378 tree ratio
= const_binop (code
, i2
, r2
);
1379 tree div
= const_binop (PLUS_EXPR
, r2
,
1380 const_binop (MULT_EXPR
, i2
, ratio
));
1382 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1383 real
= const_binop (PLUS_EXPR
, real
, r1
);
1384 real
= const_binop (code
, real
, div
);
1386 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1387 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1388 imag
= const_binop (code
, imag
, div
);
1398 return build_complex (type
, real
, imag
);
1401 if (TREE_CODE (arg1
) == VECTOR_CST
1402 && TREE_CODE (arg2
) == VECTOR_CST
)
1404 tree type
= TREE_TYPE (arg1
);
1405 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1406 tree
*elts
= XALLOCAVEC (tree
, count
);
1408 for (i
= 0; i
< count
; i
++)
1410 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1411 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1413 elts
[i
] = const_binop (code
, elem1
, elem2
);
1415 /* It is possible that const_binop cannot handle the given
1416 code and return NULL_TREE */
1417 if (elts
[i
] == NULL_TREE
)
1421 return build_vector (type
, elts
);
1424 /* Shifts allow a scalar offset for a vector. */
1425 if (TREE_CODE (arg1
) == VECTOR_CST
1426 && TREE_CODE (arg2
) == INTEGER_CST
)
1428 tree type
= TREE_TYPE (arg1
);
1429 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1430 tree
*elts
= XALLOCAVEC (tree
, count
);
1432 for (i
= 0; i
< count
; i
++)
1434 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1436 elts
[i
] = const_binop (code
, elem1
, arg2
);
1438 /* It is possible that const_binop cannot handle the given
1439 code and return NULL_TREE. */
1440 if (elts
[i
] == NULL_TREE
)
1444 return build_vector (type
, elts
);
1449 /* Overload that adds a TYPE parameter to be able to dispatch
1450 to fold_relational_const. */
1453 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1455 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1456 return fold_relational_const (code
, type
, arg1
, arg2
);
1458 /* ??? Until we make the const_binop worker take the type of the
1459 result as argument put those cases that need it here. */
1463 if ((TREE_CODE (arg1
) == REAL_CST
1464 && TREE_CODE (arg2
) == REAL_CST
)
1465 || (TREE_CODE (arg1
) == INTEGER_CST
1466 && TREE_CODE (arg2
) == INTEGER_CST
))
1467 return build_complex (type
, arg1
, arg2
);
1470 case VEC_PACK_TRUNC_EXPR
:
1471 case VEC_PACK_FIX_TRUNC_EXPR
:
1473 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1476 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1477 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1478 if (TREE_CODE (arg1
) != VECTOR_CST
1479 || TREE_CODE (arg2
) != VECTOR_CST
)
1482 elts
= XALLOCAVEC (tree
, nelts
);
1483 if (!vec_cst_ctor_to_array (arg1
, elts
)
1484 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1487 for (i
= 0; i
< nelts
; i
++)
1489 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1490 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1491 TREE_TYPE (type
), elts
[i
]);
1492 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1496 return build_vector (type
, elts
);
1499 case VEC_WIDEN_MULT_LO_EXPR
:
1500 case VEC_WIDEN_MULT_HI_EXPR
:
1501 case VEC_WIDEN_MULT_EVEN_EXPR
:
1502 case VEC_WIDEN_MULT_ODD_EXPR
:
1504 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1505 unsigned int out
, ofs
, scale
;
1508 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1509 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1510 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1513 elts
= XALLOCAVEC (tree
, nelts
* 4);
1514 if (!vec_cst_ctor_to_array (arg1
, elts
)
1515 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1518 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1519 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1520 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1521 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1522 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1524 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1527 for (out
= 0; out
< nelts
; out
++)
1529 unsigned int in1
= (out
<< scale
) + ofs
;
1530 unsigned int in2
= in1
+ nelts
* 2;
1533 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1534 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1536 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1538 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1539 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1543 return build_vector (type
, elts
);
1549 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1552 /* Make sure type and arg0 have the same saturating flag. */
1553 gcc_checking_assert (TYPE_SATURATING (type
)
1554 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1556 return const_binop (code
, arg1
, arg2
);
1559 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1560 Return zero if computing the constants is not possible. */
1563 const_unop (enum tree_code code
, tree type
, tree arg0
)
1569 case FIX_TRUNC_EXPR
:
1570 case FIXED_CONVERT_EXPR
:
1571 return fold_convert_const (code
, type
, arg0
);
1573 case ADDR_SPACE_CONVERT_EXPR
:
1574 if (integer_zerop (arg0
))
1575 return fold_convert_const (code
, type
, arg0
);
1578 case VIEW_CONVERT_EXPR
:
1579 return fold_view_convert_expr (type
, arg0
);
1583 /* Can't call fold_negate_const directly here as that doesn't
1584 handle all cases and we might not be able to negate some
1586 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1587 if (tem
&& CONSTANT_CLASS_P (tem
))
1593 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1594 return fold_abs_const (arg0
, type
);
1598 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1600 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1602 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1607 if (TREE_CODE (arg0
) == INTEGER_CST
)
1608 return fold_not_const (arg0
, type
);
1609 /* Perform BIT_NOT_EXPR on each element individually. */
1610 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1614 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1616 elements
= XALLOCAVEC (tree
, count
);
1617 for (i
= 0; i
< count
; i
++)
1619 elem
= VECTOR_CST_ELT (arg0
, i
);
1620 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1621 if (elem
== NULL_TREE
)
1626 return build_vector (type
, elements
);
1630 case TRUTH_NOT_EXPR
:
1631 if (TREE_CODE (arg0
) == INTEGER_CST
)
1632 return constant_boolean_node (integer_zerop (arg0
), type
);
1636 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1637 return fold_convert (type
, TREE_REALPART (arg0
));
1641 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1642 return fold_convert (type
, TREE_IMAGPART (arg0
));
1645 case VEC_UNPACK_LO_EXPR
:
1646 case VEC_UNPACK_HI_EXPR
:
1647 case VEC_UNPACK_FLOAT_LO_EXPR
:
1648 case VEC_UNPACK_FLOAT_HI_EXPR
:
1650 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1652 enum tree_code subcode
;
1654 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1655 if (TREE_CODE (arg0
) != VECTOR_CST
)
1658 elts
= XALLOCAVEC (tree
, nelts
* 2);
1659 if (!vec_cst_ctor_to_array (arg0
, elts
))
1662 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1663 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1666 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1669 subcode
= FLOAT_EXPR
;
1671 for (i
= 0; i
< nelts
; i
++)
1673 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1674 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1678 return build_vector (type
, elts
);
1681 case REDUC_MIN_EXPR
:
1682 case REDUC_MAX_EXPR
:
1683 case REDUC_PLUS_EXPR
:
1685 unsigned int nelts
, i
;
1687 enum tree_code subcode
;
1689 if (TREE_CODE (arg0
) != VECTOR_CST
)
1691 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1693 elts
= XALLOCAVEC (tree
, nelts
);
1694 if (!vec_cst_ctor_to_array (arg0
, elts
))
1699 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1700 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1701 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1702 default: gcc_unreachable ();
1705 for (i
= 1; i
< nelts
; i
++)
1707 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1708 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1722 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1723 indicates which particular sizetype to create. */
1726 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1728 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1731 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1732 is a tree code. The type of the result is taken from the operands.
1733 Both must be equivalent integer types, ala int_binop_types_match_p.
1734 If the operands are constant, so is the result. */
1737 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1739 tree type
= TREE_TYPE (arg0
);
1741 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1742 return error_mark_node
;
1744 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1747 /* Handle the special case of two integer constants faster. */
1748 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1750 /* And some specific cases even faster than that. */
1751 if (code
== PLUS_EXPR
)
1753 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1755 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1758 else if (code
== MINUS_EXPR
)
1760 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1763 else if (code
== MULT_EXPR
)
1765 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1769 /* Handle general case of two integer constants. For sizetype
1770 constant calculations we always want to know about overflow,
1771 even in the unsigned case. */
1772 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1775 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1778 /* Given two values, either both of sizetype or both of bitsizetype,
1779 compute the difference between the two values. Return the value
1780 in signed type corresponding to the type of the operands. */
1783 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1785 tree type
= TREE_TYPE (arg0
);
1788 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1791 /* If the type is already signed, just do the simple thing. */
1792 if (!TYPE_UNSIGNED (type
))
1793 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1795 if (type
== sizetype
)
1797 else if (type
== bitsizetype
)
1798 ctype
= sbitsizetype
;
1800 ctype
= signed_type_for (type
);
1802 /* If either operand is not a constant, do the conversions to the signed
1803 type and subtract. The hardware will do the right thing with any
1804 overflow in the subtraction. */
1805 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1806 return size_binop_loc (loc
, MINUS_EXPR
,
1807 fold_convert_loc (loc
, ctype
, arg0
),
1808 fold_convert_loc (loc
, ctype
, arg1
));
1810 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1811 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1812 overflow) and negate (which can't either). Special-case a result
1813 of zero while we're here. */
1814 if (tree_int_cst_equal (arg0
, arg1
))
1815 return build_int_cst (ctype
, 0);
1816 else if (tree_int_cst_lt (arg1
, arg0
))
1817 return fold_convert_loc (loc
, ctype
,
1818 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1820 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1821 fold_convert_loc (loc
, ctype
,
1822 size_binop_loc (loc
,
1827 /* A subroutine of fold_convert_const handling conversions of an
1828 INTEGER_CST to another integer type. */
1831 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1833 /* Given an integer constant, make new constant with new type,
1834 appropriately sign-extended or truncated. Use widest_int
1835 so that any extension is done according ARG1's type. */
1836 return force_fit_type (type
, wi::to_widest (arg1
),
1837 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1838 TREE_OVERFLOW (arg1
));
1841 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1842 to an integer type. */
1845 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1847 bool overflow
= false;
1850 /* The following code implements the floating point to integer
1851 conversion rules required by the Java Language Specification,
1852 that IEEE NaNs are mapped to zero and values that overflow
1853 the target precision saturate, i.e. values greater than
1854 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1855 are mapped to INT_MIN. These semantics are allowed by the
1856 C and C++ standards that simply state that the behavior of
1857 FP-to-integer conversion is unspecified upon overflow. */
1861 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1865 case FIX_TRUNC_EXPR
:
1866 real_trunc (&r
, VOIDmode
, &x
);
1873 /* If R is NaN, return zero and show we have an overflow. */
1874 if (REAL_VALUE_ISNAN (r
))
1877 val
= wi::zero (TYPE_PRECISION (type
));
1880 /* See if R is less than the lower bound or greater than the
1885 tree lt
= TYPE_MIN_VALUE (type
);
1886 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1887 if (REAL_VALUES_LESS (r
, l
))
1896 tree ut
= TYPE_MAX_VALUE (type
);
1899 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1900 if (REAL_VALUES_LESS (u
, r
))
1909 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1911 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1915 /* A subroutine of fold_convert_const handling conversions of a
1916 FIXED_CST to an integer type. */
1919 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1922 double_int temp
, temp_trunc
;
1925 /* Right shift FIXED_CST to temp by fbit. */
1926 temp
= TREE_FIXED_CST (arg1
).data
;
1927 mode
= TREE_FIXED_CST (arg1
).mode
;
1928 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1930 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1931 HOST_BITS_PER_DOUBLE_INT
,
1932 SIGNED_FIXED_POINT_MODE_P (mode
));
1934 /* Left shift temp to temp_trunc by fbit. */
1935 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1936 HOST_BITS_PER_DOUBLE_INT
,
1937 SIGNED_FIXED_POINT_MODE_P (mode
));
1941 temp
= double_int_zero
;
1942 temp_trunc
= double_int_zero
;
1945 /* If FIXED_CST is negative, we need to round the value toward 0.
1946 By checking if the fractional bits are not zero to add 1 to temp. */
1947 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1948 && temp_trunc
.is_negative ()
1949 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1950 temp
+= double_int_one
;
1952 /* Given a fixed-point constant, make new constant with new type,
1953 appropriately sign-extended or truncated. */
1954 t
= force_fit_type (type
, temp
, -1,
1955 (temp
.is_negative ()
1956 && (TYPE_UNSIGNED (type
)
1957 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1958 | TREE_OVERFLOW (arg1
));
1963 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1964 to another floating point type. */
1967 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1969 REAL_VALUE_TYPE value
;
1972 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1973 t
= build_real (type
, value
);
1975 /* If converting an infinity or NAN to a representation that doesn't
1976 have one, set the overflow bit so that we can produce some kind of
1977 error message at the appropriate point if necessary. It's not the
1978 most user-friendly message, but it's better than nothing. */
1979 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
1980 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
1981 TREE_OVERFLOW (t
) = 1;
1982 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
1983 && !MODE_HAS_NANS (TYPE_MODE (type
)))
1984 TREE_OVERFLOW (t
) = 1;
1985 /* Regular overflow, conversion produced an infinity in a mode that
1986 can't represent them. */
1987 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
1988 && REAL_VALUE_ISINF (value
)
1989 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
1990 TREE_OVERFLOW (t
) = 1;
1992 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
1996 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1997 to a floating point type. */
2000 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
2002 REAL_VALUE_TYPE value
;
2005 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
2006 t
= build_real (type
, value
);
2008 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
2012 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2013 to another fixed-point type. */
2016 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2018 FIXED_VALUE_TYPE value
;
2022 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2023 TYPE_SATURATING (type
));
2024 t
= build_fixed (type
, value
);
2026 /* Propagate overflow flags. */
2027 if (overflow_p
| TREE_OVERFLOW (arg1
))
2028 TREE_OVERFLOW (t
) = 1;
2032 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2033 to a fixed-point type. */
2036 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2038 FIXED_VALUE_TYPE value
;
2043 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2045 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2046 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2047 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? (HOST_WIDE_INT
) -1 : 0;
2049 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2051 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2052 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2053 TYPE_SATURATING (type
));
2054 t
= build_fixed (type
, value
);
2056 /* Propagate overflow flags. */
2057 if (overflow_p
| TREE_OVERFLOW (arg1
))
2058 TREE_OVERFLOW (t
) = 1;
2062 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2063 to a fixed-point type. */
2066 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2068 FIXED_VALUE_TYPE value
;
2072 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2073 &TREE_REAL_CST (arg1
),
2074 TYPE_SATURATING (type
));
2075 t
= build_fixed (type
, value
);
2077 /* Propagate overflow flags. */
2078 if (overflow_p
| TREE_OVERFLOW (arg1
))
2079 TREE_OVERFLOW (t
) = 1;
2083 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2084 type TYPE. If no simplification can be done return NULL_TREE. */
2087 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2089 if (TREE_TYPE (arg1
) == type
)
2092 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2093 || TREE_CODE (type
) == OFFSET_TYPE
)
2095 if (TREE_CODE (arg1
) == INTEGER_CST
)
2096 return fold_convert_const_int_from_int (type
, arg1
);
2097 else if (TREE_CODE (arg1
) == REAL_CST
)
2098 return fold_convert_const_int_from_real (code
, type
, arg1
);
2099 else if (TREE_CODE (arg1
) == FIXED_CST
)
2100 return fold_convert_const_int_from_fixed (type
, arg1
);
2102 else if (TREE_CODE (type
) == REAL_TYPE
)
2104 if (TREE_CODE (arg1
) == INTEGER_CST
)
2105 return build_real_from_int_cst (type
, arg1
);
2106 else if (TREE_CODE (arg1
) == REAL_CST
)
2107 return fold_convert_const_real_from_real (type
, arg1
);
2108 else if (TREE_CODE (arg1
) == FIXED_CST
)
2109 return fold_convert_const_real_from_fixed (type
, arg1
);
2111 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2113 if (TREE_CODE (arg1
) == FIXED_CST
)
2114 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2115 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2116 return fold_convert_const_fixed_from_int (type
, arg1
);
2117 else if (TREE_CODE (arg1
) == REAL_CST
)
2118 return fold_convert_const_fixed_from_real (type
, arg1
);
2123 /* Construct a vector of zero elements of vector type TYPE. */
2126 build_zero_vector (tree type
)
2130 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2131 return build_vector_from_val (type
, t
);
2134 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2137 fold_convertible_p (const_tree type
, const_tree arg
)
2139 tree orig
= TREE_TYPE (arg
);
2144 if (TREE_CODE (arg
) == ERROR_MARK
2145 || TREE_CODE (type
) == ERROR_MARK
2146 || TREE_CODE (orig
) == ERROR_MARK
)
2149 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2152 switch (TREE_CODE (type
))
2154 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2155 case POINTER_TYPE
: case REFERENCE_TYPE
:
2157 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2158 || TREE_CODE (orig
) == OFFSET_TYPE
)
2160 return (TREE_CODE (orig
) == VECTOR_TYPE
2161 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2164 case FIXED_POINT_TYPE
:
2168 return TREE_CODE (type
) == TREE_CODE (orig
);
2175 /* Convert expression ARG to type TYPE. Used by the middle-end for
2176 simple conversions in preference to calling the front-end's convert. */
2179 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2181 tree orig
= TREE_TYPE (arg
);
2187 if (TREE_CODE (arg
) == ERROR_MARK
2188 || TREE_CODE (type
) == ERROR_MARK
2189 || TREE_CODE (orig
) == ERROR_MARK
)
2190 return error_mark_node
;
2192 switch (TREE_CODE (type
))
2195 case REFERENCE_TYPE
:
2196 /* Handle conversions between pointers to different address spaces. */
2197 if (POINTER_TYPE_P (orig
)
2198 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2199 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2200 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2203 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2205 if (TREE_CODE (arg
) == INTEGER_CST
)
2207 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2208 if (tem
!= NULL_TREE
)
2211 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2212 || TREE_CODE (orig
) == OFFSET_TYPE
)
2213 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2214 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2215 return fold_convert_loc (loc
, type
,
2216 fold_build1_loc (loc
, REALPART_EXPR
,
2217 TREE_TYPE (orig
), arg
));
2218 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2219 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2220 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2223 if (TREE_CODE (arg
) == INTEGER_CST
)
2225 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2226 if (tem
!= NULL_TREE
)
2229 else if (TREE_CODE (arg
) == REAL_CST
)
2231 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2232 if (tem
!= NULL_TREE
)
2235 else if (TREE_CODE (arg
) == FIXED_CST
)
2237 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2238 if (tem
!= NULL_TREE
)
2242 switch (TREE_CODE (orig
))
2245 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2246 case POINTER_TYPE
: case REFERENCE_TYPE
:
2247 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2250 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2252 case FIXED_POINT_TYPE
:
2253 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2256 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2257 return fold_convert_loc (loc
, type
, tem
);
2263 case FIXED_POINT_TYPE
:
2264 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2265 || TREE_CODE (arg
) == REAL_CST
)
2267 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2268 if (tem
!= NULL_TREE
)
2269 goto fold_convert_exit
;
2272 switch (TREE_CODE (orig
))
2274 case FIXED_POINT_TYPE
:
2279 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2282 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2283 return fold_convert_loc (loc
, type
, tem
);
2290 switch (TREE_CODE (orig
))
2293 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2294 case POINTER_TYPE
: case REFERENCE_TYPE
:
2296 case FIXED_POINT_TYPE
:
2297 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2298 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2299 fold_convert_loc (loc
, TREE_TYPE (type
),
2300 integer_zero_node
));
2305 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2307 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2308 TREE_OPERAND (arg
, 0));
2309 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2310 TREE_OPERAND (arg
, 1));
2311 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2314 arg
= save_expr (arg
);
2315 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2316 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2317 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2318 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2319 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2327 if (integer_zerop (arg
))
2328 return build_zero_vector (type
);
2329 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2330 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2331 || TREE_CODE (orig
) == VECTOR_TYPE
);
2332 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2335 tem
= fold_ignored_result (arg
);
2336 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2339 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2340 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2344 protected_set_expr_location_unshare (tem
, loc
);
2348 /* Return false if expr can be assumed not to be an lvalue, true
2352 maybe_lvalue_p (const_tree x
)
2354 /* We only need to wrap lvalue tree codes. */
2355 switch (TREE_CODE (x
))
2368 case ARRAY_RANGE_REF
:
2374 case PREINCREMENT_EXPR
:
2375 case PREDECREMENT_EXPR
:
2377 case TRY_CATCH_EXPR
:
2378 case WITH_CLEANUP_EXPR
:
2387 /* Assume the worst for front-end tree codes. */
2388 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2396 /* Return an expr equal to X but certainly not valid as an lvalue. */
2399 non_lvalue_loc (location_t loc
, tree x
)
2401 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2406 if (! maybe_lvalue_p (x
))
2408 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2411 /* When pedantic, return an expr equal to X but certainly not valid as a
2412 pedantic lvalue. Otherwise, return X. */
2415 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2417 return protected_set_expr_location_unshare (x
, loc
);
2420 /* Given a tree comparison code, return the code that is the logical inverse.
2421 It is generally not safe to do this for floating-point comparisons, except
2422 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2423 ERROR_MARK in this case. */
2426 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2428 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2429 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2439 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2441 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2443 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2445 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2459 return UNORDERED_EXPR
;
2460 case UNORDERED_EXPR
:
2461 return ORDERED_EXPR
;
2467 /* Similar, but return the comparison that results if the operands are
2468 swapped. This is safe for floating-point. */
2471 swap_tree_comparison (enum tree_code code
)
2478 case UNORDERED_EXPR
:
2504 /* Convert a comparison tree code from an enum tree_code representation
2505 into a compcode bit-based encoding. This function is the inverse of
2506 compcode_to_comparison. */
2508 static enum comparison_code
2509 comparison_to_compcode (enum tree_code code
)
2526 return COMPCODE_ORD
;
2527 case UNORDERED_EXPR
:
2528 return COMPCODE_UNORD
;
2530 return COMPCODE_UNLT
;
2532 return COMPCODE_UNEQ
;
2534 return COMPCODE_UNLE
;
2536 return COMPCODE_UNGT
;
2538 return COMPCODE_LTGT
;
2540 return COMPCODE_UNGE
;
2546 /* Convert a compcode bit-based encoding of a comparison operator back
2547 to GCC's enum tree_code representation. This function is the
2548 inverse of comparison_to_compcode. */
2550 static enum tree_code
2551 compcode_to_comparison (enum comparison_code code
)
2568 return ORDERED_EXPR
;
2569 case COMPCODE_UNORD
:
2570 return UNORDERED_EXPR
;
2588 /* Return a tree for the comparison which is the combination of
2589 doing the AND or OR (depending on CODE) of the two operations LCODE
2590 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2591 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2592 if this makes the transformation invalid. */
2595 combine_comparisons (location_t loc
,
2596 enum tree_code code
, enum tree_code lcode
,
2597 enum tree_code rcode
, tree truth_type
,
2598 tree ll_arg
, tree lr_arg
)
2600 bool honor_nans
= HONOR_NANS (ll_arg
);
2601 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2602 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2607 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2608 compcode
= lcompcode
& rcompcode
;
2611 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2612 compcode
= lcompcode
| rcompcode
;
2621 /* Eliminate unordered comparisons, as well as LTGT and ORD
2622 which are not used unless the mode has NaNs. */
2623 compcode
&= ~COMPCODE_UNORD
;
2624 if (compcode
== COMPCODE_LTGT
)
2625 compcode
= COMPCODE_NE
;
2626 else if (compcode
== COMPCODE_ORD
)
2627 compcode
= COMPCODE_TRUE
;
2629 else if (flag_trapping_math
)
2631 /* Check that the original operation and the optimized ones will trap
2632 under the same condition. */
2633 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2634 && (lcompcode
!= COMPCODE_EQ
)
2635 && (lcompcode
!= COMPCODE_ORD
);
2636 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2637 && (rcompcode
!= COMPCODE_EQ
)
2638 && (rcompcode
!= COMPCODE_ORD
);
2639 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2640 && (compcode
!= COMPCODE_EQ
)
2641 && (compcode
!= COMPCODE_ORD
);
2643 /* In a short-circuited boolean expression the LHS might be
2644 such that the RHS, if evaluated, will never trap. For
2645 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2646 if neither x nor y is NaN. (This is a mixed blessing: for
2647 example, the expression above will never trap, hence
2648 optimizing it to x < y would be invalid). */
2649 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2650 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2653 /* If the comparison was short-circuited, and only the RHS
2654 trapped, we may now generate a spurious trap. */
2656 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2659 /* If we changed the conditions that cause a trap, we lose. */
2660 if ((ltrap
|| rtrap
) != trap
)
2664 if (compcode
== COMPCODE_TRUE
)
2665 return constant_boolean_node (true, truth_type
);
2666 else if (compcode
== COMPCODE_FALSE
)
2667 return constant_boolean_node (false, truth_type
);
2670 enum tree_code tcode
;
2672 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2673 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2677 /* Return nonzero if two operands (typically of the same tree node)
2678 are necessarily equal. If either argument has side-effects this
2679 function returns zero. FLAGS modifies behavior as follows:
2681 If OEP_ONLY_CONST is set, only return nonzero for constants.
2682 This function tests whether the operands are indistinguishable;
2683 it does not test whether they are equal using C's == operation.
2684 The distinction is important for IEEE floating point, because
2685 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2686 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2688 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2689 even though it may hold multiple values during a function.
2690 This is because a GCC tree node guarantees that nothing else is
2691 executed between the evaluation of its "operands" (which may often
2692 be evaluated in arbitrary order). Hence if the operands themselves
2693 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2694 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2695 unset means assuming isochronic (or instantaneous) tree equivalence.
2696 Unless comparing arbitrary expression trees, such as from different
2697 statements, this flag can usually be left unset.
2699 If OEP_PURE_SAME is set, then pure functions with identical arguments
2700 are considered the same. It is used when the caller has other ways
2701 to ensure that global memory is unchanged in between. */
2704 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2706 /* If either is ERROR_MARK, they aren't equal. */
2707 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2708 || TREE_TYPE (arg0
) == error_mark_node
2709 || TREE_TYPE (arg1
) == error_mark_node
)
2712 /* Similar, if either does not have a type (like a released SSA name),
2713 they aren't equal. */
2714 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2717 /* Check equality of integer constants before bailing out due to
2718 precision differences. */
2719 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2720 return tree_int_cst_equal (arg0
, arg1
);
2722 /* If both types don't have the same signedness, then we can't consider
2723 them equal. We must check this before the STRIP_NOPS calls
2724 because they may change the signedness of the arguments. As pointers
2725 strictly don't have a signedness, require either two pointers or
2726 two non-pointers as well. */
2727 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2728 || POINTER_TYPE_P (TREE_TYPE (arg0
)) != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2731 /* We cannot consider pointers to different address space equal. */
2732 if (POINTER_TYPE_P (TREE_TYPE (arg0
)) && POINTER_TYPE_P (TREE_TYPE (arg1
))
2733 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2734 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2737 /* If both types don't have the same precision, then it is not safe
2739 if (element_precision (TREE_TYPE (arg0
))
2740 != element_precision (TREE_TYPE (arg1
)))
2746 /* In case both args are comparisons but with different comparison
2747 code, try to swap the comparison operands of one arg to produce
2748 a match and compare that variant. */
2749 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2750 && COMPARISON_CLASS_P (arg0
)
2751 && COMPARISON_CLASS_P (arg1
))
2753 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2755 if (TREE_CODE (arg0
) == swap_code
)
2756 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2757 TREE_OPERAND (arg1
, 1), flags
)
2758 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2759 TREE_OPERAND (arg1
, 0), flags
);
2762 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2763 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2764 && !(CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
)))
2767 /* This is needed for conversions and for COMPONENT_REF.
2768 Might as well play it safe and always test this. */
2769 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2770 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2771 || TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
)))
2774 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2775 We don't care about side effects in that case because the SAVE_EXPR
2776 takes care of that for us. In all other cases, two expressions are
2777 equal if they have no side effects. If we have two identical
2778 expressions with side effects that should be treated the same due
2779 to the only side effects being identical SAVE_EXPR's, that will
2780 be detected in the recursive calls below.
2781 If we are taking an invariant address of two identical objects
2782 they are necessarily equal as well. */
2783 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2784 && (TREE_CODE (arg0
) == SAVE_EXPR
2785 || (flags
& OEP_CONSTANT_ADDRESS_OF
)
2786 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2789 /* Next handle constant cases, those for which we can return 1 even
2790 if ONLY_CONST is set. */
2791 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2792 switch (TREE_CODE (arg0
))
2795 return tree_int_cst_equal (arg0
, arg1
);
2798 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2799 TREE_FIXED_CST (arg1
));
2802 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0
),
2803 TREE_REAL_CST (arg1
)))
2807 if (!HONOR_SIGNED_ZEROS (arg0
))
2809 /* If we do not distinguish between signed and unsigned zero,
2810 consider them equal. */
2811 if (real_zerop (arg0
) && real_zerop (arg1
))
2820 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2823 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2825 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2826 VECTOR_CST_ELT (arg1
, i
), flags
))
2833 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2835 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2839 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2840 && ! memcmp (TREE_STRING_POINTER (arg0
),
2841 TREE_STRING_POINTER (arg1
),
2842 TREE_STRING_LENGTH (arg0
)));
2845 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2846 TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
)
2847 ? OEP_CONSTANT_ADDRESS_OF
| OEP_ADDRESS_OF
: 0);
2852 if (flags
& OEP_ONLY_CONST
)
2855 /* Define macros to test an operand from arg0 and arg1 for equality and a
2856 variant that allows null and views null as being different from any
2857 non-null value. In the latter case, if either is null, the both
2858 must be; otherwise, do the normal comparison. */
2859 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2860 TREE_OPERAND (arg1, N), flags)
2862 #define OP_SAME_WITH_NULL(N) \
2863 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2864 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2866 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2869 /* Two conversions are equal only if signedness and modes match. */
2870 switch (TREE_CODE (arg0
))
2873 case FIX_TRUNC_EXPR
:
2874 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2875 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
2885 case tcc_comparison
:
2887 if (OP_SAME (0) && OP_SAME (1))
2890 /* For commutative ops, allow the other order. */
2891 return (commutative_tree_code (TREE_CODE (arg0
))
2892 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2893 TREE_OPERAND (arg1
, 1), flags
)
2894 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2895 TREE_OPERAND (arg1
, 0), flags
));
2898 /* If either of the pointer (or reference) expressions we are
2899 dereferencing contain a side effect, these cannot be equal,
2900 but their addresses can be. */
2901 if ((flags
& OEP_CONSTANT_ADDRESS_OF
) == 0
2902 && (TREE_SIDE_EFFECTS (arg0
)
2903 || TREE_SIDE_EFFECTS (arg1
)))
2906 switch (TREE_CODE (arg0
))
2909 if (!(flags
& OEP_ADDRESS_OF
)
2910 && (TYPE_ALIGN (TREE_TYPE (arg0
))
2911 != TYPE_ALIGN (TREE_TYPE (arg1
))))
2913 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2920 case TARGET_MEM_REF
:
2922 /* Require equal access sizes, and similar pointer types.
2923 We can have incomplete types for array references of
2924 variable-sized arrays from the Fortran frontend
2925 though. Also verify the types are compatible. */
2926 if (!((TYPE_SIZE (TREE_TYPE (arg0
)) == TYPE_SIZE (TREE_TYPE (arg1
))
2927 || (TYPE_SIZE (TREE_TYPE (arg0
))
2928 && TYPE_SIZE (TREE_TYPE (arg1
))
2929 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
2930 TYPE_SIZE (TREE_TYPE (arg1
)), flags
)))
2931 && types_compatible_p (TREE_TYPE (arg0
), TREE_TYPE (arg1
))
2932 && ((flags
& OEP_ADDRESS_OF
)
2933 || (alias_ptr_types_compatible_p
2934 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
2935 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
2936 && (MR_DEPENDENCE_CLIQUE (arg0
)
2937 == MR_DEPENDENCE_CLIQUE (arg1
))
2938 && (MR_DEPENDENCE_BASE (arg0
)
2939 == MR_DEPENDENCE_BASE (arg1
))
2940 && (TYPE_ALIGN (TREE_TYPE (arg0
))
2941 == TYPE_ALIGN (TREE_TYPE (arg1
)))))))
2943 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2944 return (OP_SAME (0) && OP_SAME (1)
2945 /* TARGET_MEM_REF require equal extra operands. */
2946 && (TREE_CODE (arg0
) != TARGET_MEM_REF
2947 || (OP_SAME_WITH_NULL (2)
2948 && OP_SAME_WITH_NULL (3)
2949 && OP_SAME_WITH_NULL (4))));
2952 case ARRAY_RANGE_REF
:
2953 /* Operands 2 and 3 may be null.
2954 Compare the array index by value if it is constant first as we
2955 may have different types but same value here. */
2958 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2959 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
2960 TREE_OPERAND (arg1
, 1))
2962 && OP_SAME_WITH_NULL (2)
2963 && OP_SAME_WITH_NULL (3));
2966 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2967 may be NULL when we're called to compare MEM_EXPRs. */
2968 if (!OP_SAME_WITH_NULL (0)
2971 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2972 return OP_SAME_WITH_NULL (2);
2977 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2978 return OP_SAME (1) && OP_SAME (2);
2984 case tcc_expression
:
2985 switch (TREE_CODE (arg0
))
2988 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2989 TREE_OPERAND (arg1
, 0),
2990 flags
| OEP_ADDRESS_OF
);
2992 case TRUTH_NOT_EXPR
:
2995 case TRUTH_ANDIF_EXPR
:
2996 case TRUTH_ORIF_EXPR
:
2997 return OP_SAME (0) && OP_SAME (1);
3000 case WIDEN_MULT_PLUS_EXPR
:
3001 case WIDEN_MULT_MINUS_EXPR
:
3004 /* The multiplcation operands are commutative. */
3007 case TRUTH_AND_EXPR
:
3009 case TRUTH_XOR_EXPR
:
3010 if (OP_SAME (0) && OP_SAME (1))
3013 /* Otherwise take into account this is a commutative operation. */
3014 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3015 TREE_OPERAND (arg1
, 1), flags
)
3016 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3017 TREE_OPERAND (arg1
, 0), flags
));
3022 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3029 switch (TREE_CODE (arg0
))
3032 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3033 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3034 /* If not both CALL_EXPRs are either internal or normal function
3035 functions, then they are not equal. */
3037 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3039 /* If the CALL_EXPRs call different internal functions, then they
3041 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3046 /* If the CALL_EXPRs call different functions, then they are not
3048 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3054 unsigned int cef
= call_expr_flags (arg0
);
3055 if (flags
& OEP_PURE_SAME
)
3056 cef
&= ECF_CONST
| ECF_PURE
;
3063 /* Now see if all the arguments are the same. */
3065 const_call_expr_arg_iterator iter0
, iter1
;
3067 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3068 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3070 a0
= next_const_call_expr_arg (&iter0
),
3071 a1
= next_const_call_expr_arg (&iter1
))
3072 if (! operand_equal_p (a0
, a1
, flags
))
3075 /* If we get here and both argument lists are exhausted
3076 then the CALL_EXPRs are equal. */
3077 return ! (a0
|| a1
);
3083 case tcc_declaration
:
3084 /* Consider __builtin_sqrt equal to sqrt. */
3085 return (TREE_CODE (arg0
) == FUNCTION_DECL
3086 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3087 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3088 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3095 #undef OP_SAME_WITH_NULL
3098 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3099 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3101 When in doubt, return 0. */
3104 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3106 int unsignedp1
, unsignedpo
;
3107 tree primarg0
, primarg1
, primother
;
3108 unsigned int correct_width
;
3110 if (operand_equal_p (arg0
, arg1
, 0))
3113 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3114 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3117 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3118 and see if the inner values are the same. This removes any
3119 signedness comparison, which doesn't matter here. */
3120 primarg0
= arg0
, primarg1
= arg1
;
3121 STRIP_NOPS (primarg0
);
3122 STRIP_NOPS (primarg1
);
3123 if (operand_equal_p (primarg0
, primarg1
, 0))
3126 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3127 actual comparison operand, ARG0.
3129 First throw away any conversions to wider types
3130 already present in the operands. */
3132 primarg1
= get_narrower (arg1
, &unsignedp1
);
3133 primother
= get_narrower (other
, &unsignedpo
);
3135 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3136 if (unsignedp1
== unsignedpo
3137 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3138 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3140 tree type
= TREE_TYPE (arg0
);
3142 /* Make sure shorter operand is extended the right way
3143 to match the longer operand. */
3144 primarg1
= fold_convert (signed_or_unsigned_type_for
3145 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3147 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3154 /* See if ARG is an expression that is either a comparison or is performing
3155 arithmetic on comparisons. The comparisons must only be comparing
3156 two different values, which will be stored in *CVAL1 and *CVAL2; if
3157 they are nonzero it means that some operands have already been found.
3158 No variables may be used anywhere else in the expression except in the
3159 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3160 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3162 If this is true, return 1. Otherwise, return zero. */
3165 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3167 enum tree_code code
= TREE_CODE (arg
);
3168 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3170 /* We can handle some of the tcc_expression cases here. */
3171 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3173 else if (tclass
== tcc_expression
3174 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3175 || code
== COMPOUND_EXPR
))
3176 tclass
= tcc_binary
;
3178 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3179 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3181 /* If we've already found a CVAL1 or CVAL2, this expression is
3182 two complex to handle. */
3183 if (*cval1
|| *cval2
)
3193 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3196 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3197 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3198 cval1
, cval2
, save_p
));
3203 case tcc_expression
:
3204 if (code
== COND_EXPR
)
3205 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3206 cval1
, cval2
, save_p
)
3207 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3208 cval1
, cval2
, save_p
)
3209 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3210 cval1
, cval2
, save_p
));
3213 case tcc_comparison
:
3214 /* First see if we can handle the first operand, then the second. For
3215 the second operand, we know *CVAL1 can't be zero. It must be that
3216 one side of the comparison is each of the values; test for the
3217 case where this isn't true by failing if the two operands
3220 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3221 TREE_OPERAND (arg
, 1), 0))
3225 *cval1
= TREE_OPERAND (arg
, 0);
3226 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3228 else if (*cval2
== 0)
3229 *cval2
= TREE_OPERAND (arg
, 0);
3230 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3235 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3237 else if (*cval2
== 0)
3238 *cval2
= TREE_OPERAND (arg
, 1);
3239 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3251 /* ARG is a tree that is known to contain just arithmetic operations and
3252 comparisons. Evaluate the operations in the tree substituting NEW0 for
3253 any occurrence of OLD0 as an operand of a comparison and likewise for
3257 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3258 tree old1
, tree new1
)
3260 tree type
= TREE_TYPE (arg
);
3261 enum tree_code code
= TREE_CODE (arg
);
3262 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3264 /* We can handle some of the tcc_expression cases here. */
3265 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3267 else if (tclass
== tcc_expression
3268 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3269 tclass
= tcc_binary
;
3274 return fold_build1_loc (loc
, code
, type
,
3275 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3276 old0
, new0
, old1
, new1
));
3279 return fold_build2_loc (loc
, code
, type
,
3280 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3281 old0
, new0
, old1
, new1
),
3282 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3283 old0
, new0
, old1
, new1
));
3285 case tcc_expression
:
3289 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3293 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3297 return fold_build3_loc (loc
, code
, type
,
3298 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3299 old0
, new0
, old1
, new1
),
3300 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3301 old0
, new0
, old1
, new1
),
3302 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3303 old0
, new0
, old1
, new1
));
3307 /* Fall through - ??? */
3309 case tcc_comparison
:
3311 tree arg0
= TREE_OPERAND (arg
, 0);
3312 tree arg1
= TREE_OPERAND (arg
, 1);
3314 /* We need to check both for exact equality and tree equality. The
3315 former will be true if the operand has a side-effect. In that
3316 case, we know the operand occurred exactly once. */
3318 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3320 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3323 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3325 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3328 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3336 /* Return a tree for the case when the result of an expression is RESULT
3337 converted to TYPE and OMITTED was previously an operand of the expression
3338 but is now not needed (e.g., we folded OMITTED * 0).
3340 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3341 the conversion of RESULT to TYPE. */
3344 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3346 tree t
= fold_convert_loc (loc
, type
, result
);
3348 /* If the resulting operand is an empty statement, just return the omitted
3349 statement casted to void. */
3350 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3351 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3352 fold_ignored_result (omitted
));
3354 if (TREE_SIDE_EFFECTS (omitted
))
3355 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3356 fold_ignored_result (omitted
), t
);
3358 return non_lvalue_loc (loc
, t
);
3361 /* Return a tree for the case when the result of an expression is RESULT
3362 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3363 of the expression but are now not needed.
3365 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3366 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3367 evaluated before OMITTED2. Otherwise, if neither has side effects,
3368 just do the conversion of RESULT to TYPE. */
3371 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3372 tree omitted1
, tree omitted2
)
3374 tree t
= fold_convert_loc (loc
, type
, result
);
3376 if (TREE_SIDE_EFFECTS (omitted2
))
3377 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3378 if (TREE_SIDE_EFFECTS (omitted1
))
3379 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3381 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3385 /* Return a simplified tree node for the truth-negation of ARG. This
3386 never alters ARG itself. We assume that ARG is an operation that
3387 returns a truth value (0 or 1).
3389 FIXME: one would think we would fold the result, but it causes
3390 problems with the dominator optimizer. */
3393 fold_truth_not_expr (location_t loc
, tree arg
)
3395 tree type
= TREE_TYPE (arg
);
3396 enum tree_code code
= TREE_CODE (arg
);
3397 location_t loc1
, loc2
;
3399 /* If this is a comparison, we can simply invert it, except for
3400 floating-point non-equality comparisons, in which case we just
3401 enclose a TRUTH_NOT_EXPR around what we have. */
3403 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3405 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3406 if (FLOAT_TYPE_P (op_type
)
3407 && flag_trapping_math
3408 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3409 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3412 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3413 if (code
== ERROR_MARK
)
3416 return build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3417 TREE_OPERAND (arg
, 1));
3423 return constant_boolean_node (integer_zerop (arg
), type
);
3425 case TRUTH_AND_EXPR
:
3426 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3427 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3428 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3429 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3430 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3433 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3434 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3435 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3436 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3437 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3439 case TRUTH_XOR_EXPR
:
3440 /* Here we can invert either operand. We invert the first operand
3441 unless the second operand is a TRUTH_NOT_EXPR in which case our
3442 result is the XOR of the first operand with the inside of the
3443 negation of the second operand. */
3445 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3446 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3447 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3449 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3450 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3451 TREE_OPERAND (arg
, 1));
3453 case TRUTH_ANDIF_EXPR
:
3454 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3455 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3456 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3457 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3458 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3460 case TRUTH_ORIF_EXPR
:
3461 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3462 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3463 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3464 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3465 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3467 case TRUTH_NOT_EXPR
:
3468 return TREE_OPERAND (arg
, 0);
3472 tree arg1
= TREE_OPERAND (arg
, 1);
3473 tree arg2
= TREE_OPERAND (arg
, 2);
3475 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3476 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3478 /* A COND_EXPR may have a throw as one operand, which
3479 then has void type. Just leave void operands
3481 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3482 VOID_TYPE_P (TREE_TYPE (arg1
))
3483 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3484 VOID_TYPE_P (TREE_TYPE (arg2
))
3485 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3489 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3490 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3491 TREE_OPERAND (arg
, 0),
3492 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3494 case NON_LVALUE_EXPR
:
3495 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3496 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3499 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3500 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3502 /* ... fall through ... */
3505 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3506 return build1_loc (loc
, TREE_CODE (arg
), type
,
3507 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3510 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3512 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3515 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3517 case CLEANUP_POINT_EXPR
:
3518 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3519 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3520 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3527 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3528 assume that ARG is an operation that returns a truth value (0 or 1
3529 for scalars, 0 or -1 for vectors). Return the folded expression if
3530 folding is successful. Otherwise, return NULL_TREE. */
3533 fold_invert_truthvalue (location_t loc
, tree arg
)
3535 tree type
= TREE_TYPE (arg
);
3536 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3542 /* Return a simplified tree node for the truth-negation of ARG. This
3543 never alters ARG itself. We assume that ARG is an operation that
3544 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3547 invert_truthvalue_loc (location_t loc
, tree arg
)
3549 if (TREE_CODE (arg
) == ERROR_MARK
)
3552 tree type
= TREE_TYPE (arg
);
3553 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3559 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3560 operands are another bit-wise operation with a common input. If so,
3561 distribute the bit operations to save an operation and possibly two if
3562 constants are involved. For example, convert
3563 (A | B) & (A | C) into A | (B & C)
3564 Further simplification will occur if B and C are constants.
3566 If this optimization cannot be done, 0 will be returned. */
3569 distribute_bit_expr (location_t loc
, enum tree_code code
, tree type
,
3570 tree arg0
, tree arg1
)
3575 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
3576 || TREE_CODE (arg0
) == code
3577 || (TREE_CODE (arg0
) != BIT_AND_EXPR
3578 && TREE_CODE (arg0
) != BIT_IOR_EXPR
))
3581 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0))
3583 common
= TREE_OPERAND (arg0
, 0);
3584 left
= TREE_OPERAND (arg0
, 1);
3585 right
= TREE_OPERAND (arg1
, 1);
3587 else if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1), 0))
3589 common
= TREE_OPERAND (arg0
, 0);
3590 left
= TREE_OPERAND (arg0
, 1);
3591 right
= TREE_OPERAND (arg1
, 0);
3593 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 0), 0))
3595 common
= TREE_OPERAND (arg0
, 1);
3596 left
= TREE_OPERAND (arg0
, 0);
3597 right
= TREE_OPERAND (arg1
, 1);
3599 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1), 0))
3601 common
= TREE_OPERAND (arg0
, 1);
3602 left
= TREE_OPERAND (arg0
, 0);
3603 right
= TREE_OPERAND (arg1
, 0);
3608 common
= fold_convert_loc (loc
, type
, common
);
3609 left
= fold_convert_loc (loc
, type
, left
);
3610 right
= fold_convert_loc (loc
, type
, right
);
3611 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, common
,
3612 fold_build2_loc (loc
, code
, type
, left
, right
));
3615 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3616 with code CODE. This optimization is unsafe. */
3618 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3619 tree arg0
, tree arg1
)
3621 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3622 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3624 /* (A / C) +- (B / C) -> (A +- B) / C. */
3626 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3627 TREE_OPERAND (arg1
, 1), 0))
3628 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3629 fold_build2_loc (loc
, code
, type
,
3630 TREE_OPERAND (arg0
, 0),
3631 TREE_OPERAND (arg1
, 0)),
3632 TREE_OPERAND (arg0
, 1));
3634 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3635 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3636 TREE_OPERAND (arg1
, 0), 0)
3637 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3638 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3640 REAL_VALUE_TYPE r0
, r1
;
3641 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3642 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3644 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3646 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3647 real_arithmetic (&r0
, code
, &r0
, &r1
);
3648 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3649 TREE_OPERAND (arg0
, 0),
3650 build_real (type
, r0
));
3656 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3657 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3660 make_bit_field_ref (location_t loc
, tree inner
, tree type
,
3661 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
, int unsignedp
)
3663 tree result
, bftype
;
3667 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3668 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3669 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3670 && tree_fits_shwi_p (size
)
3671 && tree_to_shwi (size
) == bitsize
)
3672 return fold_convert_loc (loc
, type
, inner
);
3676 if (TYPE_PRECISION (bftype
) != bitsize
3677 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3678 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3680 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3681 size_int (bitsize
), bitsize_int (bitpos
));
3684 result
= fold_convert_loc (loc
, type
, result
);
3689 /* Optimize a bit-field compare.
3691 There are two cases: First is a compare against a constant and the
3692 second is a comparison of two items where the fields are at the same
3693 bit position relative to the start of a chunk (byte, halfword, word)
3694 large enough to contain it. In these cases we can avoid the shift
3695 implicit in bitfield extractions.
3697 For constants, we emit a compare of the shifted constant with the
3698 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3699 compared. For two fields at the same position, we do the ANDs with the
3700 similar mask and compare the result of the ANDs.
3702 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3703 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3704 are the left and right operands of the comparison, respectively.
3706 If the optimization described above can be done, we return the resulting
3707 tree. Otherwise we return zero. */
3710 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3711 tree compare_type
, tree lhs
, tree rhs
)
3713 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3714 tree type
= TREE_TYPE (lhs
);
3716 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3717 machine_mode lmode
, rmode
, nmode
;
3718 int lunsignedp
, runsignedp
;
3719 int lvolatilep
= 0, rvolatilep
= 0;
3720 tree linner
, rinner
= NULL_TREE
;
3724 /* Get all the information about the extractions being done. If the bit size
3725 if the same as the size of the underlying object, we aren't doing an
3726 extraction at all and so can do nothing. We also don't want to
3727 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3728 then will no longer be able to replace it. */
3729 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3730 &lunsignedp
, &lvolatilep
, false);
3731 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3732 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3737 /* If this is not a constant, we can only do something if bit positions,
3738 sizes, and signedness are the same. */
3739 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3740 &runsignedp
, &rvolatilep
, false);
3742 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3743 || lunsignedp
!= runsignedp
|| offset
!= 0
3744 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3748 /* See if we can find a mode to refer to this field. We should be able to,
3749 but fail if we can't. */
3750 nmode
= get_best_mode (lbitsize
, lbitpos
, 0, 0,
3751 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3752 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3753 TYPE_ALIGN (TREE_TYPE (rinner
))),
3755 if (nmode
== VOIDmode
)
3758 /* Set signed and unsigned types of the precision of this mode for the
3760 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3762 /* Compute the bit position and size for the new reference and our offset
3763 within it. If the new reference is the same size as the original, we
3764 won't optimize anything, so return zero. */
3765 nbitsize
= GET_MODE_BITSIZE (nmode
);
3766 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3768 if (nbitsize
== lbitsize
)
3771 if (BYTES_BIG_ENDIAN
)
3772 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3774 /* Make the mask to be used against the extracted field. */
3775 mask
= build_int_cst_type (unsigned_type
, -1);
3776 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3777 mask
= const_binop (RSHIFT_EXPR
, mask
,
3778 size_int (nbitsize
- lbitsize
- lbitpos
));
3781 /* If not comparing with constant, just rework the comparison
3783 return fold_build2_loc (loc
, code
, compare_type
,
3784 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3785 make_bit_field_ref (loc
, linner
,
3790 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3791 make_bit_field_ref (loc
, rinner
,
3797 /* Otherwise, we are handling the constant case. See if the constant is too
3798 big for the field. Warn and return a tree of for 0 (false) if so. We do
3799 this not only for its own sake, but to avoid having to test for this
3800 error case below. If we didn't, we might generate wrong code.
3802 For unsigned fields, the constant shifted right by the field length should
3803 be all zero. For signed fields, the high-order bits should agree with
3808 if (wi::lrshift (rhs
, lbitsize
) != 0)
3810 warning (0, "comparison is always %d due to width of bit-field",
3812 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3817 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
3818 if (tem
!= 0 && tem
!= -1)
3820 warning (0, "comparison is always %d due to width of bit-field",
3822 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3826 /* Single-bit compares should always be against zero. */
3827 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3829 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3830 rhs
= build_int_cst (type
, 0);
3833 /* Make a new bitfield reference, shift the constant over the
3834 appropriate number of bits and mask it with the computed mask
3835 (in case this was a signed field). If we changed it, make a new one. */
3836 lhs
= make_bit_field_ref (loc
, linner
, unsigned_type
, nbitsize
, nbitpos
, 1);
3838 rhs
= const_binop (BIT_AND_EXPR
,
3839 const_binop (LSHIFT_EXPR
,
3840 fold_convert_loc (loc
, unsigned_type
, rhs
),
3841 size_int (lbitpos
)),
3844 lhs
= build2_loc (loc
, code
, compare_type
,
3845 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
3849 /* Subroutine for fold_truth_andor_1: decode a field reference.
3851 If EXP is a comparison reference, we return the innermost reference.
3853 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3854 set to the starting bit number.
3856 If the innermost field can be completely contained in a mode-sized
3857 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3859 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3860 otherwise it is not changed.
3862 *PUNSIGNEDP is set to the signedness of the field.
3864 *PMASK is set to the mask used. This is either contained in a
3865 BIT_AND_EXPR or derived from the width of the field.
3867 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3869 Return 0 if this is not a component reference or is one that we can't
3870 do anything with. */
3873 decode_field_reference (location_t loc
, tree exp
, HOST_WIDE_INT
*pbitsize
,
3874 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
3875 int *punsignedp
, int *pvolatilep
,
3876 tree
*pmask
, tree
*pand_mask
)
3878 tree outer_type
= 0;
3880 tree mask
, inner
, offset
;
3882 unsigned int precision
;
3884 /* All the optimizations using this function assume integer fields.
3885 There are problems with FP fields since the type_for_size call
3886 below can fail for, e.g., XFmode. */
3887 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
3890 /* We are interested in the bare arrangement of bits, so strip everything
3891 that doesn't affect the machine mode. However, record the type of the
3892 outermost expression if it may matter below. */
3893 if (CONVERT_EXPR_P (exp
)
3894 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
3895 outer_type
= TREE_TYPE (exp
);
3898 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
3900 and_mask
= TREE_OPERAND (exp
, 1);
3901 exp
= TREE_OPERAND (exp
, 0);
3902 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
3903 if (TREE_CODE (and_mask
) != INTEGER_CST
)
3907 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
3908 punsignedp
, pvolatilep
, false);
3909 if ((inner
== exp
&& and_mask
== 0)
3910 || *pbitsize
< 0 || offset
!= 0
3911 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
3914 /* If the number of bits in the reference is the same as the bitsize of
3915 the outer type, then the outer type gives the signedness. Otherwise
3916 (in case of a small bitfield) the signedness is unchanged. */
3917 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
3918 *punsignedp
= TYPE_UNSIGNED (outer_type
);
3920 /* Compute the mask to access the bitfield. */
3921 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
3922 precision
= TYPE_PRECISION (unsigned_type
);
3924 mask
= build_int_cst_type (unsigned_type
, -1);
3926 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3927 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3929 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3931 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3932 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
3935 *pand_mask
= and_mask
;
3939 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3940 bit positions and MASK is SIGNED. */
3943 all_ones_mask_p (const_tree mask
, unsigned int size
)
3945 tree type
= TREE_TYPE (mask
);
3946 unsigned int precision
= TYPE_PRECISION (type
);
3948 /* If this function returns true when the type of the mask is
3949 UNSIGNED, then there will be errors. In particular see
3950 gcc.c-torture/execute/990326-1.c. There does not appear to be
3951 any documentation paper trail as to why this is so. But the pre
3952 wide-int worked with that restriction and it has been preserved
3954 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
3957 return wi::mask (size
, false, precision
) == mask
;
3960 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3961 represents the sign bit of EXP's type. If EXP represents a sign
3962 or zero extension, also test VAL against the unextended type.
3963 The return value is the (sub)expression whose sign bit is VAL,
3964 or NULL_TREE otherwise. */
3967 sign_bit_p (tree exp
, const_tree val
)
3972 /* Tree EXP must have an integral type. */
3973 t
= TREE_TYPE (exp
);
3974 if (! INTEGRAL_TYPE_P (t
))
3977 /* Tree VAL must be an integer constant. */
3978 if (TREE_CODE (val
) != INTEGER_CST
3979 || TREE_OVERFLOW (val
))
3982 width
= TYPE_PRECISION (t
);
3983 if (wi::only_sign_bit_p (val
, width
))
3986 /* Handle extension from a narrower type. */
3987 if (TREE_CODE (exp
) == NOP_EXPR
3988 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
3989 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
3994 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3995 to be evaluated unconditionally. */
3998 simple_operand_p (const_tree exp
)
4000 /* Strip any conversions that don't change the machine mode. */
4003 return (CONSTANT_CLASS_P (exp
)
4004 || TREE_CODE (exp
) == SSA_NAME
4006 && ! TREE_ADDRESSABLE (exp
)
4007 && ! TREE_THIS_VOLATILE (exp
)
4008 && ! DECL_NONLOCAL (exp
)
4009 /* Don't regard global variables as simple. They may be
4010 allocated in ways unknown to the compiler (shared memory,
4011 #pragma weak, etc). */
4012 && ! TREE_PUBLIC (exp
)
4013 && ! DECL_EXTERNAL (exp
)
4014 /* Weakrefs are not safe to be read, since they can be NULL.
4015 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4016 have DECL_WEAK flag set. */
4017 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4018 /* Loading a static variable is unduly expensive, but global
4019 registers aren't expensive. */
4020 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4023 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4024 to be evaluated unconditionally.
4025 I addition to simple_operand_p, we assume that comparisons, conversions,
4026 and logic-not operations are simple, if their operands are simple, too. */
4029 simple_operand_p_2 (tree exp
)
4031 enum tree_code code
;
4033 if (TREE_SIDE_EFFECTS (exp
)
4034 || tree_could_trap_p (exp
))
4037 while (CONVERT_EXPR_P (exp
))
4038 exp
= TREE_OPERAND (exp
, 0);
4040 code
= TREE_CODE (exp
);
4042 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4043 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4044 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4046 if (code
== TRUTH_NOT_EXPR
)
4047 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4049 return simple_operand_p (exp
);
4053 /* The following functions are subroutines to fold_range_test and allow it to
4054 try to change a logical combination of comparisons into a range test.
4057 X == 2 || X == 3 || X == 4 || X == 5
4061 (unsigned) (X - 2) <= 3
4063 We describe each set of comparisons as being either inside or outside
4064 a range, using a variable named like IN_P, and then describe the
4065 range with a lower and upper bound. If one of the bounds is omitted,
4066 it represents either the highest or lowest value of the type.
4068 In the comments below, we represent a range by two numbers in brackets
4069 preceded by a "+" to designate being inside that range, or a "-" to
4070 designate being outside that range, so the condition can be inverted by
4071 flipping the prefix. An omitted bound is represented by a "-". For
4072 example, "- [-, 10]" means being outside the range starting at the lowest
4073 possible value and ending at 10, in other words, being greater than 10.
4074 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4077 We set up things so that the missing bounds are handled in a consistent
4078 manner so neither a missing bound nor "true" and "false" need to be
4079 handled using a special case. */
4081 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4082 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4083 and UPPER1_P are nonzero if the respective argument is an upper bound
4084 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4085 must be specified for a comparison. ARG1 will be converted to ARG0's
4086 type if both are specified. */
4089 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4090 tree arg1
, int upper1_p
)
4096 /* If neither arg represents infinity, do the normal operation.
4097 Else, if not a comparison, return infinity. Else handle the special
4098 comparison rules. Note that most of the cases below won't occur, but
4099 are handled for consistency. */
4101 if (arg0
!= 0 && arg1
!= 0)
4103 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4104 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4106 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4109 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4112 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4113 for neither. In real maths, we cannot assume open ended ranges are
4114 the same. But, this is computer arithmetic, where numbers are finite.
4115 We can therefore make the transformation of any unbounded range with
4116 the value Z, Z being greater than any representable number. This permits
4117 us to treat unbounded ranges as equal. */
4118 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4119 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4123 result
= sgn0
== sgn1
;
4126 result
= sgn0
!= sgn1
;
4129 result
= sgn0
< sgn1
;
4132 result
= sgn0
<= sgn1
;
4135 result
= sgn0
> sgn1
;
4138 result
= sgn0
>= sgn1
;
4144 return constant_boolean_node (result
, type
);
4147 /* Helper routine for make_range. Perform one step for it, return
4148 new expression if the loop should continue or NULL_TREE if it should
4152 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4153 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4154 bool *strict_overflow_p
)
4156 tree arg0_type
= TREE_TYPE (arg0
);
4157 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4158 int in_p
= *p_in_p
, n_in_p
;
4162 case TRUTH_NOT_EXPR
:
4163 /* We can only do something if the range is testing for zero. */
4164 if (low
== NULL_TREE
|| high
== NULL_TREE
4165 || ! integer_zerop (low
) || ! integer_zerop (high
))
4170 case EQ_EXPR
: case NE_EXPR
:
4171 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4172 /* We can only do something if the range is testing for zero
4173 and if the second operand is an integer constant. Note that
4174 saying something is "in" the range we make is done by
4175 complementing IN_P since it will set in the initial case of
4176 being not equal to zero; "out" is leaving it alone. */
4177 if (low
== NULL_TREE
|| high
== NULL_TREE
4178 || ! integer_zerop (low
) || ! integer_zerop (high
)
4179 || TREE_CODE (arg1
) != INTEGER_CST
)
4184 case NE_EXPR
: /* - [c, c] */
4187 case EQ_EXPR
: /* + [c, c] */
4188 in_p
= ! in_p
, low
= high
= arg1
;
4190 case GT_EXPR
: /* - [-, c] */
4191 low
= 0, high
= arg1
;
4193 case GE_EXPR
: /* + [c, -] */
4194 in_p
= ! in_p
, low
= arg1
, high
= 0;
4196 case LT_EXPR
: /* - [c, -] */
4197 low
= arg1
, high
= 0;
4199 case LE_EXPR
: /* + [-, c] */
4200 in_p
= ! in_p
, low
= 0, high
= arg1
;
4206 /* If this is an unsigned comparison, we also know that EXP is
4207 greater than or equal to zero. We base the range tests we make
4208 on that fact, so we record it here so we can parse existing
4209 range tests. We test arg0_type since often the return type
4210 of, e.g. EQ_EXPR, is boolean. */
4211 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4213 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4215 build_int_cst (arg0_type
, 0),
4219 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4221 /* If the high bound is missing, but we have a nonzero low
4222 bound, reverse the range so it goes from zero to the low bound
4224 if (high
== 0 && low
&& ! integer_zerop (low
))
4227 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4228 build_int_cst (TREE_TYPE (low
), 1), 0);
4229 low
= build_int_cst (arg0_type
, 0);
4239 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4240 low and high are non-NULL, then normalize will DTRT. */
4241 if (!TYPE_UNSIGNED (arg0_type
)
4242 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4244 if (low
== NULL_TREE
)
4245 low
= TYPE_MIN_VALUE (arg0_type
);
4246 if (high
== NULL_TREE
)
4247 high
= TYPE_MAX_VALUE (arg0_type
);
4250 /* (-x) IN [a,b] -> x in [-b, -a] */
4251 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4252 build_int_cst (exp_type
, 0),
4254 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4255 build_int_cst (exp_type
, 0),
4257 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4263 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4264 build_int_cst (exp_type
, 1));
4268 if (TREE_CODE (arg1
) != INTEGER_CST
)
4271 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4272 move a constant to the other side. */
4273 if (!TYPE_UNSIGNED (arg0_type
)
4274 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4277 /* If EXP is signed, any overflow in the computation is undefined,
4278 so we don't worry about it so long as our computations on
4279 the bounds don't overflow. For unsigned, overflow is defined
4280 and this is exactly the right thing. */
4281 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4282 arg0_type
, low
, 0, arg1
, 0);
4283 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4284 arg0_type
, high
, 1, arg1
, 0);
4285 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4286 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4289 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4290 *strict_overflow_p
= true;
4293 /* Check for an unsigned range which has wrapped around the maximum
4294 value thus making n_high < n_low, and normalize it. */
4295 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4297 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4298 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4299 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4300 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4302 /* If the range is of the form +/- [ x+1, x ], we won't
4303 be able to normalize it. But then, it represents the
4304 whole range or the empty set, so make it
4306 if (tree_int_cst_equal (n_low
, low
)
4307 && tree_int_cst_equal (n_high
, high
))
4313 low
= n_low
, high
= n_high
;
4321 case NON_LVALUE_EXPR
:
4322 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4325 if (! INTEGRAL_TYPE_P (arg0_type
)
4326 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4327 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4330 n_low
= low
, n_high
= high
;
4333 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4336 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4338 /* If we're converting arg0 from an unsigned type, to exp,
4339 a signed type, we will be doing the comparison as unsigned.
4340 The tests above have already verified that LOW and HIGH
4343 So we have to ensure that we will handle large unsigned
4344 values the same way that the current signed bounds treat
4347 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4351 /* For fixed-point modes, we need to pass the saturating flag
4352 as the 2nd parameter. */
4353 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4355 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4356 TYPE_SATURATING (arg0_type
));
4359 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4361 /* A range without an upper bound is, naturally, unbounded.
4362 Since convert would have cropped a very large value, use
4363 the max value for the destination type. */
4365 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4366 : TYPE_MAX_VALUE (arg0_type
);
4368 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4369 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4370 fold_convert_loc (loc
, arg0_type
,
4372 build_int_cst (arg0_type
, 1));
4374 /* If the low bound is specified, "and" the range with the
4375 range for which the original unsigned value will be
4379 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4380 1, fold_convert_loc (loc
, arg0_type
,
4385 in_p
= (n_in_p
== in_p
);
4389 /* Otherwise, "or" the range with the range of the input
4390 that will be interpreted as negative. */
4391 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4392 1, fold_convert_loc (loc
, arg0_type
,
4397 in_p
= (in_p
!= n_in_p
);
4411 /* Given EXP, a logical expression, set the range it is testing into
4412 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4413 actually being tested. *PLOW and *PHIGH will be made of the same
4414 type as the returned expression. If EXP is not a comparison, we
4415 will most likely not be returning a useful value and range. Set
4416 *STRICT_OVERFLOW_P to true if the return value is only valid
4417 because signed overflow is undefined; otherwise, do not change
4418 *STRICT_OVERFLOW_P. */
4421 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4422 bool *strict_overflow_p
)
4424 enum tree_code code
;
4425 tree arg0
, arg1
= NULL_TREE
;
4426 tree exp_type
, nexp
;
4429 location_t loc
= EXPR_LOCATION (exp
);
4431 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4432 and see if we can refine the range. Some of the cases below may not
4433 happen, but it doesn't seem worth worrying about this. We "continue"
4434 the outer loop when we've changed something; otherwise we "break"
4435 the switch, which will "break" the while. */
4438 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4442 code
= TREE_CODE (exp
);
4443 exp_type
= TREE_TYPE (exp
);
4446 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4448 if (TREE_OPERAND_LENGTH (exp
) > 0)
4449 arg0
= TREE_OPERAND (exp
, 0);
4450 if (TREE_CODE_CLASS (code
) == tcc_binary
4451 || TREE_CODE_CLASS (code
) == tcc_comparison
4452 || (TREE_CODE_CLASS (code
) == tcc_expression
4453 && TREE_OPERAND_LENGTH (exp
) > 1))
4454 arg1
= TREE_OPERAND (exp
, 1);
4456 if (arg0
== NULL_TREE
)
4459 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4460 &high
, &in_p
, strict_overflow_p
);
4461 if (nexp
== NULL_TREE
)
4466 /* If EXP is a constant, we can evaluate whether this is true or false. */
4467 if (TREE_CODE (exp
) == INTEGER_CST
)
4469 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4471 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4477 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4481 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4482 type, TYPE, return an expression to test if EXP is in (or out of, depending
4483 on IN_P) the range. Return 0 if the test couldn't be created. */
4486 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4487 tree low
, tree high
)
4489 tree etype
= TREE_TYPE (exp
), value
;
4491 /* Disable this optimization for function pointer expressions
4492 on targets that require function pointer canonicalization. */
4493 if (targetm
.have_canonicalize_funcptr_for_compare ()
4494 && TREE_CODE (etype
) == POINTER_TYPE
4495 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4500 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4502 return invert_truthvalue_loc (loc
, value
);
4507 if (low
== 0 && high
== 0)
4508 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4511 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4512 fold_convert_loc (loc
, etype
, high
));
4515 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4516 fold_convert_loc (loc
, etype
, low
));
4518 if (operand_equal_p (low
, high
, 0))
4519 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4520 fold_convert_loc (loc
, etype
, low
));
4522 if (integer_zerop (low
))
4524 if (! TYPE_UNSIGNED (etype
))
4526 etype
= unsigned_type_for (etype
);
4527 high
= fold_convert_loc (loc
, etype
, high
);
4528 exp
= fold_convert_loc (loc
, etype
, exp
);
4530 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4533 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4534 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4536 int prec
= TYPE_PRECISION (etype
);
4538 if (wi::mask (prec
- 1, false, prec
) == high
)
4540 if (TYPE_UNSIGNED (etype
))
4542 tree signed_etype
= signed_type_for (etype
);
4543 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4545 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4547 etype
= signed_etype
;
4548 exp
= fold_convert_loc (loc
, etype
, exp
);
4550 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4551 build_int_cst (etype
, 0));
4555 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4556 This requires wrap-around arithmetics for the type of the expression.
4557 First make sure that arithmetics in this type is valid, then make sure
4558 that it wraps around. */
4559 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4560 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4561 TYPE_UNSIGNED (etype
));
4563 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4565 tree utype
, minv
, maxv
;
4567 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4568 for the type in question, as we rely on this here. */
4569 utype
= unsigned_type_for (etype
);
4570 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4571 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4572 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4573 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4575 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4582 high
= fold_convert_loc (loc
, etype
, high
);
4583 low
= fold_convert_loc (loc
, etype
, low
);
4584 exp
= fold_convert_loc (loc
, etype
, exp
);
4586 value
= const_binop (MINUS_EXPR
, high
, low
);
4589 if (POINTER_TYPE_P (etype
))
4591 if (value
!= 0 && !TREE_OVERFLOW (value
))
4593 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4594 return build_range_check (loc
, type
,
4595 fold_build_pointer_plus_loc (loc
, exp
, low
),
4596 1, build_int_cst (etype
, 0), value
);
4601 if (value
!= 0 && !TREE_OVERFLOW (value
))
4602 return build_range_check (loc
, type
,
4603 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4604 1, build_int_cst (etype
, 0), value
);
4609 /* Return the predecessor of VAL in its type, handling the infinite case. */
4612 range_predecessor (tree val
)
4614 tree type
= TREE_TYPE (val
);
4616 if (INTEGRAL_TYPE_P (type
)
4617 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4620 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4621 build_int_cst (TREE_TYPE (val
), 1), 0);
4624 /* Return the successor of VAL in its type, handling the infinite case. */
4627 range_successor (tree val
)
4629 tree type
= TREE_TYPE (val
);
4631 if (INTEGRAL_TYPE_P (type
)
4632 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4635 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4636 build_int_cst (TREE_TYPE (val
), 1), 0);
4639 /* Given two ranges, see if we can merge them into one. Return 1 if we
4640 can, 0 if we can't. Set the output range into the specified parameters. */
4643 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4644 tree high0
, int in1_p
, tree low1
, tree high1
)
4652 int lowequal
= ((low0
== 0 && low1
== 0)
4653 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4654 low0
, 0, low1
, 0)));
4655 int highequal
= ((high0
== 0 && high1
== 0)
4656 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4657 high0
, 1, high1
, 1)));
4659 /* Make range 0 be the range that starts first, or ends last if they
4660 start at the same value. Swap them if it isn't. */
4661 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4664 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4665 high1
, 1, high0
, 1))))
4667 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4668 tem
= low0
, low0
= low1
, low1
= tem
;
4669 tem
= high0
, high0
= high1
, high1
= tem
;
4672 /* Now flag two cases, whether the ranges are disjoint or whether the
4673 second range is totally subsumed in the first. Note that the tests
4674 below are simplified by the ones above. */
4675 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4676 high0
, 1, low1
, 0));
4677 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4678 high1
, 1, high0
, 1));
4680 /* We now have four cases, depending on whether we are including or
4681 excluding the two ranges. */
4684 /* If they don't overlap, the result is false. If the second range
4685 is a subset it is the result. Otherwise, the range is from the start
4686 of the second to the end of the first. */
4688 in_p
= 0, low
= high
= 0;
4690 in_p
= 1, low
= low1
, high
= high1
;
4692 in_p
= 1, low
= low1
, high
= high0
;
4695 else if (in0_p
&& ! in1_p
)
4697 /* If they don't overlap, the result is the first range. If they are
4698 equal, the result is false. If the second range is a subset of the
4699 first, and the ranges begin at the same place, we go from just after
4700 the end of the second range to the end of the first. If the second
4701 range is not a subset of the first, or if it is a subset and both
4702 ranges end at the same place, the range starts at the start of the
4703 first range and ends just before the second range.
4704 Otherwise, we can't describe this as a single range. */
4706 in_p
= 1, low
= low0
, high
= high0
;
4707 else if (lowequal
&& highequal
)
4708 in_p
= 0, low
= high
= 0;
4709 else if (subset
&& lowequal
)
4711 low
= range_successor (high1
);
4716 /* We are in the weird situation where high0 > high1 but
4717 high1 has no successor. Punt. */
4721 else if (! subset
|| highequal
)
4724 high
= range_predecessor (low1
);
4728 /* low0 < low1 but low1 has no predecessor. Punt. */
4736 else if (! in0_p
&& in1_p
)
4738 /* If they don't overlap, the result is the second range. If the second
4739 is a subset of the first, the result is false. Otherwise,
4740 the range starts just after the first range and ends at the
4741 end of the second. */
4743 in_p
= 1, low
= low1
, high
= high1
;
4744 else if (subset
|| highequal
)
4745 in_p
= 0, low
= high
= 0;
4748 low
= range_successor (high0
);
4753 /* high1 > high0 but high0 has no successor. Punt. */
4761 /* The case where we are excluding both ranges. Here the complex case
4762 is if they don't overlap. In that case, the only time we have a
4763 range is if they are adjacent. If the second is a subset of the
4764 first, the result is the first. Otherwise, the range to exclude
4765 starts at the beginning of the first range and ends at the end of the
4769 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4770 range_successor (high0
),
4772 in_p
= 0, low
= low0
, high
= high1
;
4775 /* Canonicalize - [min, x] into - [-, x]. */
4776 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
4777 switch (TREE_CODE (TREE_TYPE (low0
)))
4780 if (TYPE_PRECISION (TREE_TYPE (low0
))
4781 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
4785 if (tree_int_cst_equal (low0
,
4786 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
4790 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
4791 && integer_zerop (low0
))
4798 /* Canonicalize - [x, max] into - [x, -]. */
4799 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
4800 switch (TREE_CODE (TREE_TYPE (high1
)))
4803 if (TYPE_PRECISION (TREE_TYPE (high1
))
4804 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
4808 if (tree_int_cst_equal (high1
,
4809 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
4813 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
4814 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
4816 build_int_cst (TREE_TYPE (high1
), 1),
4824 /* The ranges might be also adjacent between the maximum and
4825 minimum values of the given type. For
4826 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4827 return + [x + 1, y - 1]. */
4828 if (low0
== 0 && high1
== 0)
4830 low
= range_successor (high0
);
4831 high
= range_predecessor (low1
);
4832 if (low
== 0 || high
== 0)
4842 in_p
= 0, low
= low0
, high
= high0
;
4844 in_p
= 0, low
= low0
, high
= high1
;
4847 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4852 /* Subroutine of fold, looking inside expressions of the form
4853 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4854 of the COND_EXPR. This function is being used also to optimize
4855 A op B ? C : A, by reversing the comparison first.
4857 Return a folded expression whose code is not a COND_EXPR
4858 anymore, or NULL_TREE if no folding opportunity is found. */
4861 fold_cond_expr_with_comparison (location_t loc
, tree type
,
4862 tree arg0
, tree arg1
, tree arg2
)
4864 enum tree_code comp_code
= TREE_CODE (arg0
);
4865 tree arg00
= TREE_OPERAND (arg0
, 0);
4866 tree arg01
= TREE_OPERAND (arg0
, 1);
4867 tree arg1_type
= TREE_TYPE (arg1
);
4873 /* If we have A op 0 ? A : -A, consider applying the following
4876 A == 0? A : -A same as -A
4877 A != 0? A : -A same as A
4878 A >= 0? A : -A same as abs (A)
4879 A > 0? A : -A same as abs (A)
4880 A <= 0? A : -A same as -abs (A)
4881 A < 0? A : -A same as -abs (A)
4883 None of these transformations work for modes with signed
4884 zeros. If A is +/-0, the first two transformations will
4885 change the sign of the result (from +0 to -0, or vice
4886 versa). The last four will fix the sign of the result,
4887 even though the original expressions could be positive or
4888 negative, depending on the sign of A.
4890 Note that all these transformations are correct if A is
4891 NaN, since the two alternatives (A and -A) are also NaNs. */
4892 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
4893 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
4894 ? real_zerop (arg01
)
4895 : integer_zerop (arg01
))
4896 && ((TREE_CODE (arg2
) == NEGATE_EXPR
4897 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
4898 /* In the case that A is of the form X-Y, '-A' (arg2) may
4899 have already been folded to Y-X, check for that. */
4900 || (TREE_CODE (arg1
) == MINUS_EXPR
4901 && TREE_CODE (arg2
) == MINUS_EXPR
4902 && operand_equal_p (TREE_OPERAND (arg1
, 0),
4903 TREE_OPERAND (arg2
, 1), 0)
4904 && operand_equal_p (TREE_OPERAND (arg1
, 1),
4905 TREE_OPERAND (arg2
, 0), 0))))
4910 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
4911 return pedantic_non_lvalue_loc (loc
,
4912 fold_convert_loc (loc
, type
,
4913 negate_expr (tem
)));
4916 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4919 if (flag_trapping_math
)
4924 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4925 arg1
= fold_convert_loc (loc
, signed_type_for
4926 (TREE_TYPE (arg1
)), arg1
);
4927 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4928 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
4931 if (flag_trapping_math
)
4935 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4936 arg1
= fold_convert_loc (loc
, signed_type_for
4937 (TREE_TYPE (arg1
)), arg1
);
4938 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4939 return negate_expr (fold_convert_loc (loc
, type
, tem
));
4941 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
4945 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4946 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4947 both transformations are correct when A is NaN: A != 0
4948 is then true, and A == 0 is false. */
4950 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
4951 && integer_zerop (arg01
) && integer_zerop (arg2
))
4953 if (comp_code
== NE_EXPR
)
4954 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4955 else if (comp_code
== EQ_EXPR
)
4956 return build_zero_cst (type
);
4959 /* Try some transformations of A op B ? A : B.
4961 A == B? A : B same as B
4962 A != B? A : B same as A
4963 A >= B? A : B same as max (A, B)
4964 A > B? A : B same as max (B, A)
4965 A <= B? A : B same as min (A, B)
4966 A < B? A : B same as min (B, A)
4968 As above, these transformations don't work in the presence
4969 of signed zeros. For example, if A and B are zeros of
4970 opposite sign, the first two transformations will change
4971 the sign of the result. In the last four, the original
4972 expressions give different results for (A=+0, B=-0) and
4973 (A=-0, B=+0), but the transformed expressions do not.
4975 The first two transformations are correct if either A or B
4976 is a NaN. In the first transformation, the condition will
4977 be false, and B will indeed be chosen. In the case of the
4978 second transformation, the condition A != B will be true,
4979 and A will be chosen.
4981 The conversions to max() and min() are not correct if B is
4982 a number and A is not. The conditions in the original
4983 expressions will be false, so all four give B. The min()
4984 and max() versions would give a NaN instead. */
4985 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
4986 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
4987 /* Avoid these transformations if the COND_EXPR may be used
4988 as an lvalue in the C++ front-end. PR c++/19199. */
4990 || VECTOR_TYPE_P (type
)
4991 || (! lang_GNU_CXX ()
4992 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
4993 || ! maybe_lvalue_p (arg1
)
4994 || ! maybe_lvalue_p (arg2
)))
4996 tree comp_op0
= arg00
;
4997 tree comp_op1
= arg01
;
4998 tree comp_type
= TREE_TYPE (comp_op0
);
5000 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5001 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
5011 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg2
));
5013 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5018 /* In C++ a ?: expression can be an lvalue, so put the
5019 operand which will be used if they are equal first
5020 so that we can convert this back to the
5021 corresponding COND_EXPR. */
5022 if (!HONOR_NANS (arg1
))
5024 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5025 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5026 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5027 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5028 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5029 comp_op1
, comp_op0
);
5030 return pedantic_non_lvalue_loc (loc
,
5031 fold_convert_loc (loc
, type
, tem
));
5038 if (!HONOR_NANS (arg1
))
5040 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5041 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5042 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5043 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5044 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5045 comp_op1
, comp_op0
);
5046 return pedantic_non_lvalue_loc (loc
,
5047 fold_convert_loc (loc
, type
, tem
));
5051 if (!HONOR_NANS (arg1
))
5052 return pedantic_non_lvalue_loc (loc
,
5053 fold_convert_loc (loc
, type
, arg2
));
5056 if (!HONOR_NANS (arg1
))
5057 return pedantic_non_lvalue_loc (loc
,
5058 fold_convert_loc (loc
, type
, arg1
));
5061 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5066 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5067 we might still be able to simplify this. For example,
5068 if C1 is one less or one more than C2, this might have started
5069 out as a MIN or MAX and been transformed by this function.
5070 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5072 if (INTEGRAL_TYPE_P (type
)
5073 && TREE_CODE (arg01
) == INTEGER_CST
5074 && TREE_CODE (arg2
) == INTEGER_CST
)
5078 if (TREE_CODE (arg1
) == INTEGER_CST
)
5080 /* We can replace A with C1 in this case. */
5081 arg1
= fold_convert_loc (loc
, type
, arg01
);
5082 return fold_build3_loc (loc
, COND_EXPR
, type
, arg0
, arg1
, arg2
);
5085 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5086 MIN_EXPR, to preserve the signedness of the comparison. */
5087 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5089 && operand_equal_p (arg01
,
5090 const_binop (PLUS_EXPR
, arg2
,
5091 build_int_cst (type
, 1)),
5094 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5095 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5097 return pedantic_non_lvalue_loc (loc
,
5098 fold_convert_loc (loc
, type
, tem
));
5103 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5105 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5107 && operand_equal_p (arg01
,
5108 const_binop (MINUS_EXPR
, arg2
,
5109 build_int_cst (type
, 1)),
5112 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5113 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5115 return pedantic_non_lvalue_loc (loc
,
5116 fold_convert_loc (loc
, type
, tem
));
5121 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5122 MAX_EXPR, to preserve the signedness of the comparison. */
5123 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5125 && operand_equal_p (arg01
,
5126 const_binop (MINUS_EXPR
, arg2
,
5127 build_int_cst (type
, 1)),
5130 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5131 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5133 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5138 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5139 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5141 && operand_equal_p (arg01
,
5142 const_binop (PLUS_EXPR
, arg2
,
5143 build_int_cst (type
, 1)),
5146 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5147 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5149 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5163 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5164 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5165 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5169 /* EXP is some logical combination of boolean tests. See if we can
5170 merge it into some range test. Return the new tree if so. */
5173 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5176 int or_op
= (code
== TRUTH_ORIF_EXPR
5177 || code
== TRUTH_OR_EXPR
);
5178 int in0_p
, in1_p
, in_p
;
5179 tree low0
, low1
, low
, high0
, high1
, high
;
5180 bool strict_overflow_p
= false;
5182 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5183 "when simplifying range test");
5185 if (!INTEGRAL_TYPE_P (type
))
5188 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5189 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5191 /* If this is an OR operation, invert both sides; we will invert
5192 again at the end. */
5194 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5196 /* If both expressions are the same, if we can merge the ranges, and we
5197 can build the range test, return it or it inverted. If one of the
5198 ranges is always true or always false, consider it to be the same
5199 expression as the other. */
5200 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5201 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5203 && 0 != (tem
= (build_range_check (loc
, type
,
5205 : rhs
!= 0 ? rhs
: integer_zero_node
,
5208 if (strict_overflow_p
)
5209 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5210 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5213 /* On machines where the branch cost is expensive, if this is a
5214 short-circuited branch and the underlying object on both sides
5215 is the same, make a non-short-circuit operation. */
5216 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5217 && lhs
!= 0 && rhs
!= 0
5218 && (code
== TRUTH_ANDIF_EXPR
5219 || code
== TRUTH_ORIF_EXPR
)
5220 && operand_equal_p (lhs
, rhs
, 0))
5222 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5223 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5224 which cases we can't do this. */
5225 if (simple_operand_p (lhs
))
5226 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5227 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5230 else if (!lang_hooks
.decls
.global_bindings_p ()
5231 && !CONTAINS_PLACEHOLDER_P (lhs
))
5233 tree common
= save_expr (lhs
);
5235 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5236 or_op
? ! in0_p
: in0_p
,
5238 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5239 or_op
? ! in1_p
: in1_p
,
5242 if (strict_overflow_p
)
5243 fold_overflow_warning (warnmsg
,
5244 WARN_STRICT_OVERFLOW_COMPARISON
);
5245 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5246 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5255 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5256 bit value. Arrange things so the extra bits will be set to zero if and
5257 only if C is signed-extended to its full width. If MASK is nonzero,
5258 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5261 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5263 tree type
= TREE_TYPE (c
);
5264 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5267 if (p
== modesize
|| unsignedp
)
5270 /* We work by getting just the sign bit into the low-order bit, then
5271 into the high-order bit, then sign-extend. We then XOR that value
5273 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5275 /* We must use a signed type in order to get an arithmetic right shift.
5276 However, we must also avoid introducing accidental overflows, so that
5277 a subsequent call to integer_zerop will work. Hence we must
5278 do the type conversion here. At this point, the constant is either
5279 zero or one, and the conversion to a signed type can never overflow.
5280 We could get an overflow if this conversion is done anywhere else. */
5281 if (TYPE_UNSIGNED (type
))
5282 temp
= fold_convert (signed_type_for (type
), temp
);
5284 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5285 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5287 temp
= const_binop (BIT_AND_EXPR
, temp
,
5288 fold_convert (TREE_TYPE (c
), mask
));
5289 /* If necessary, convert the type back to match the type of C. */
5290 if (TYPE_UNSIGNED (type
))
5291 temp
= fold_convert (type
, temp
);
5293 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5296 /* For an expression that has the form
5300 we can drop one of the inner expressions and simplify to
5304 LOC is the location of the resulting expression. OP is the inner
5305 logical operation; the left-hand side in the examples above, while CMPOP
5306 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5307 removing a condition that guards another, as in
5308 (A != NULL && A->...) || A == NULL
5309 which we must not transform. If RHS_ONLY is true, only eliminate the
5310 right-most operand of the inner logical operation. */
5313 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5316 tree type
= TREE_TYPE (cmpop
);
5317 enum tree_code code
= TREE_CODE (cmpop
);
5318 enum tree_code truthop_code
= TREE_CODE (op
);
5319 tree lhs
= TREE_OPERAND (op
, 0);
5320 tree rhs
= TREE_OPERAND (op
, 1);
5321 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5322 enum tree_code rhs_code
= TREE_CODE (rhs
);
5323 enum tree_code lhs_code
= TREE_CODE (lhs
);
5324 enum tree_code inv_code
;
5326 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5329 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5332 if (rhs_code
== truthop_code
)
5334 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5335 if (newrhs
!= NULL_TREE
)
5338 rhs_code
= TREE_CODE (rhs
);
5341 if (lhs_code
== truthop_code
&& !rhs_only
)
5343 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5344 if (newlhs
!= NULL_TREE
)
5347 lhs_code
= TREE_CODE (lhs
);
5351 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5352 if (inv_code
== rhs_code
5353 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5354 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5356 if (!rhs_only
&& inv_code
== lhs_code
5357 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5358 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5360 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5361 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5366 /* Find ways of folding logical expressions of LHS and RHS:
5367 Try to merge two comparisons to the same innermost item.
5368 Look for range tests like "ch >= '0' && ch <= '9'".
5369 Look for combinations of simple terms on machines with expensive branches
5370 and evaluate the RHS unconditionally.
5372 For example, if we have p->a == 2 && p->b == 4 and we can make an
5373 object large enough to span both A and B, we can do this with a comparison
5374 against the object ANDed with the a mask.
5376 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5377 operations to do this with one comparison.
5379 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5380 function and the one above.
5382 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5383 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5385 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5388 We return the simplified tree or 0 if no optimization is possible. */
5391 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5394 /* If this is the "or" of two comparisons, we can do something if
5395 the comparisons are NE_EXPR. If this is the "and", we can do something
5396 if the comparisons are EQ_EXPR. I.e.,
5397 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5399 WANTED_CODE is this operation code. For single bit fields, we can
5400 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5401 comparison for one-bit fields. */
5403 enum tree_code wanted_code
;
5404 enum tree_code lcode
, rcode
;
5405 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5406 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5407 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5408 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5409 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5410 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5411 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5412 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5413 machine_mode lnmode
, rnmode
;
5414 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5415 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5416 tree l_const
, r_const
;
5417 tree lntype
, rntype
, result
;
5418 HOST_WIDE_INT first_bit
, end_bit
;
5421 /* Start by getting the comparison codes. Fail if anything is volatile.
5422 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5423 it were surrounded with a NE_EXPR. */
5425 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5428 lcode
= TREE_CODE (lhs
);
5429 rcode
= TREE_CODE (rhs
);
5431 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5433 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5434 build_int_cst (TREE_TYPE (lhs
), 0));
5438 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5440 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5441 build_int_cst (TREE_TYPE (rhs
), 0));
5445 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5446 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5449 ll_arg
= TREE_OPERAND (lhs
, 0);
5450 lr_arg
= TREE_OPERAND (lhs
, 1);
5451 rl_arg
= TREE_OPERAND (rhs
, 0);
5452 rr_arg
= TREE_OPERAND (rhs
, 1);
5454 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5455 if (simple_operand_p (ll_arg
)
5456 && simple_operand_p (lr_arg
))
5458 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5459 && operand_equal_p (lr_arg
, rr_arg
, 0))
5461 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5462 truth_type
, ll_arg
, lr_arg
);
5466 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5467 && operand_equal_p (lr_arg
, rl_arg
, 0))
5469 result
= combine_comparisons (loc
, code
, lcode
,
5470 swap_tree_comparison (rcode
),
5471 truth_type
, ll_arg
, lr_arg
);
5477 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5478 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5480 /* If the RHS can be evaluated unconditionally and its operands are
5481 simple, it wins to evaluate the RHS unconditionally on machines
5482 with expensive branches. In this case, this isn't a comparison
5483 that can be merged. */
5485 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5487 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5488 && simple_operand_p (rl_arg
)
5489 && simple_operand_p (rr_arg
))
5491 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5492 if (code
== TRUTH_OR_EXPR
5493 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5494 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5495 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5496 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5497 return build2_loc (loc
, NE_EXPR
, truth_type
,
5498 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5500 build_int_cst (TREE_TYPE (ll_arg
), 0));
5502 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5503 if (code
== TRUTH_AND_EXPR
5504 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5505 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5506 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5507 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5508 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5509 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5511 build_int_cst (TREE_TYPE (ll_arg
), 0));
5514 /* See if the comparisons can be merged. Then get all the parameters for
5517 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5518 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5522 ll_inner
= decode_field_reference (loc
, ll_arg
,
5523 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5524 &ll_unsignedp
, &volatilep
, &ll_mask
,
5526 lr_inner
= decode_field_reference (loc
, lr_arg
,
5527 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5528 &lr_unsignedp
, &volatilep
, &lr_mask
,
5530 rl_inner
= decode_field_reference (loc
, rl_arg
,
5531 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5532 &rl_unsignedp
, &volatilep
, &rl_mask
,
5534 rr_inner
= decode_field_reference (loc
, rr_arg
,
5535 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5536 &rr_unsignedp
, &volatilep
, &rr_mask
,
5539 /* It must be true that the inner operation on the lhs of each
5540 comparison must be the same if we are to be able to do anything.
5541 Then see if we have constants. If not, the same must be true for
5543 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
5544 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5547 if (TREE_CODE (lr_arg
) == INTEGER_CST
5548 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5549 l_const
= lr_arg
, r_const
= rr_arg
;
5550 else if (lr_inner
== 0 || rr_inner
== 0
5551 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5554 l_const
= r_const
= 0;
5556 /* If either comparison code is not correct for our logical operation,
5557 fail. However, we can convert a one-bit comparison against zero into
5558 the opposite comparison against that bit being set in the field. */
5560 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5561 if (lcode
!= wanted_code
)
5563 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5565 /* Make the left operand unsigned, since we are only interested
5566 in the value of one bit. Otherwise we are doing the wrong
5575 /* This is analogous to the code for l_const above. */
5576 if (rcode
!= wanted_code
)
5578 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5587 /* See if we can find a mode that contains both fields being compared on
5588 the left. If we can't, fail. Otherwise, update all constants and masks
5589 to be relative to a field of that size. */
5590 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5591 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5592 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5593 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5595 if (lnmode
== VOIDmode
)
5598 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5599 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5600 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5601 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5603 if (BYTES_BIG_ENDIAN
)
5605 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5606 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5609 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5610 size_int (xll_bitpos
));
5611 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5612 size_int (xrl_bitpos
));
5616 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5617 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5618 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5619 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5620 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5623 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5625 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5630 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5631 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5632 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5633 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5634 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5637 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5639 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5643 /* If the right sides are not constant, do the same for it. Also,
5644 disallow this optimization if a size or signedness mismatch occurs
5645 between the left and right sides. */
5648 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5649 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5650 /* Make sure the two fields on the right
5651 correspond to the left without being swapped. */
5652 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5655 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5656 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5657 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5658 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5660 if (rnmode
== VOIDmode
)
5663 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5664 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5665 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5666 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5668 if (BYTES_BIG_ENDIAN
)
5670 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5671 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5674 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5676 size_int (xlr_bitpos
));
5677 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5679 size_int (xrr_bitpos
));
5681 /* Make a mask that corresponds to both fields being compared.
5682 Do this for both items being compared. If the operands are the
5683 same size and the bits being compared are in the same position
5684 then we can do this by masking both and comparing the masked
5686 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5687 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5688 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5690 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5691 ll_unsignedp
|| rl_unsignedp
);
5692 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5693 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5695 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
, rnbitsize
, rnbitpos
,
5696 lr_unsignedp
|| rr_unsignedp
);
5697 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5698 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5700 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5703 /* There is still another way we can do something: If both pairs of
5704 fields being compared are adjacent, we may be able to make a wider
5705 field containing them both.
5707 Note that we still must mask the lhs/rhs expressions. Furthermore,
5708 the mask must be shifted to account for the shift done by
5709 make_bit_field_ref. */
5710 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5711 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5712 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5713 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5717 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
,
5718 ll_bitsize
+ rl_bitsize
,
5719 MIN (ll_bitpos
, rl_bitpos
), ll_unsignedp
);
5720 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
,
5721 lr_bitsize
+ rr_bitsize
,
5722 MIN (lr_bitpos
, rr_bitpos
), lr_unsignedp
);
5724 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5725 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5726 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5727 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5729 /* Convert to the smaller type before masking out unwanted bits. */
5731 if (lntype
!= rntype
)
5733 if (lnbitsize
> rnbitsize
)
5735 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5736 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5739 else if (lnbitsize
< rnbitsize
)
5741 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5742 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5747 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5748 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5750 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5751 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5753 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5759 /* Handle the case of comparisons with constants. If there is something in
5760 common between the masks, those bits of the constants must be the same.
5761 If not, the condition is always false. Test for this to avoid generating
5762 incorrect code below. */
5763 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5764 if (! integer_zerop (result
)
5765 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5766 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5768 if (wanted_code
== NE_EXPR
)
5770 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5771 return constant_boolean_node (true, truth_type
);
5775 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5776 return constant_boolean_node (false, truth_type
);
5780 /* Construct the expression we will return. First get the component
5781 reference we will make. Unless the mask is all ones the width of
5782 that field, perform the mask operation. Then compare with the
5784 result
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5785 ll_unsignedp
|| rl_unsignedp
);
5787 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5788 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5789 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5791 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5792 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5795 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5799 optimize_minmax_comparison (location_t loc
, enum tree_code code
, tree type
,
5803 enum tree_code op_code
;
5806 int consts_equal
, consts_lt
;
5809 STRIP_SIGN_NOPS (arg0
);
5811 op_code
= TREE_CODE (arg0
);
5812 minmax_const
= TREE_OPERAND (arg0
, 1);
5813 comp_const
= fold_convert_loc (loc
, TREE_TYPE (arg0
), op1
);
5814 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
5815 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
5816 inner
= TREE_OPERAND (arg0
, 0);
5818 /* If something does not permit us to optimize, return the original tree. */
5819 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
5820 || TREE_CODE (comp_const
) != INTEGER_CST
5821 || TREE_OVERFLOW (comp_const
)
5822 || TREE_CODE (minmax_const
) != INTEGER_CST
5823 || TREE_OVERFLOW (minmax_const
))
5826 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5827 and GT_EXPR, doing the rest with recursive calls using logical
5831 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
5834 = optimize_minmax_comparison (loc
,
5835 invert_tree_comparison (code
, false),
5838 return invert_truthvalue_loc (loc
, tem
);
5844 fold_build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
5845 optimize_minmax_comparison
5846 (loc
, EQ_EXPR
, type
, arg0
, comp_const
),
5847 optimize_minmax_comparison
5848 (loc
, GT_EXPR
, type
, arg0
, comp_const
));
5851 if (op_code
== MAX_EXPR
&& consts_equal
)
5852 /* MAX (X, 0) == 0 -> X <= 0 */
5853 return fold_build2_loc (loc
, LE_EXPR
, type
, inner
, comp_const
);
5855 else if (op_code
== MAX_EXPR
&& consts_lt
)
5856 /* MAX (X, 0) == 5 -> X == 5 */
5857 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5859 else if (op_code
== MAX_EXPR
)
5860 /* MAX (X, 0) == -1 -> false */
5861 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5863 else if (consts_equal
)
5864 /* MIN (X, 0) == 0 -> X >= 0 */
5865 return fold_build2_loc (loc
, GE_EXPR
, type
, inner
, comp_const
);
5868 /* MIN (X, 0) == 5 -> false */
5869 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5872 /* MIN (X, 0) == -1 -> X == -1 */
5873 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5876 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
5877 /* MAX (X, 0) > 0 -> X > 0
5878 MAX (X, 0) > 5 -> X > 5 */
5879 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5881 else if (op_code
== MAX_EXPR
)
5882 /* MAX (X, 0) > -1 -> true */
5883 return omit_one_operand_loc (loc
, type
, integer_one_node
, inner
);
5885 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
5886 /* MIN (X, 0) > 0 -> false
5887 MIN (X, 0) > 5 -> false */
5888 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5891 /* MIN (X, 0) > -1 -> X > -1 */
5892 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5899 /* T is an integer expression that is being multiplied, divided, or taken a
5900 modulus (CODE says which and what kind of divide or modulus) by a
5901 constant C. See if we can eliminate that operation by folding it with
5902 other operations already in T. WIDE_TYPE, if non-null, is a type that
5903 should be used for the computation if wider than our type.
5905 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5906 (X * 2) + (Y * 4). We must, however, be assured that either the original
5907 expression would not overflow or that overflow is undefined for the type
5908 in the language in question.
5910 If we return a non-null expression, it is an equivalent form of the
5911 original computation, but need not be in the original type.
5913 We set *STRICT_OVERFLOW_P to true if the return values depends on
5914 signed overflow being undefined. Otherwise we do not change
5915 *STRICT_OVERFLOW_P. */
5918 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5919 bool *strict_overflow_p
)
5921 /* To avoid exponential search depth, refuse to allow recursion past
5922 three levels. Beyond that (1) it's highly unlikely that we'll find
5923 something interesting and (2) we've probably processed it before
5924 when we built the inner expression. */
5933 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
5940 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5941 bool *strict_overflow_p
)
5943 tree type
= TREE_TYPE (t
);
5944 enum tree_code tcode
= TREE_CODE (t
);
5945 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
5946 > GET_MODE_SIZE (TYPE_MODE (type
)))
5947 ? wide_type
: type
);
5949 int same_p
= tcode
== code
;
5950 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
5951 bool sub_strict_overflow_p
;
5953 /* Don't deal with constants of zero here; they confuse the code below. */
5954 if (integer_zerop (c
))
5957 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
5958 op0
= TREE_OPERAND (t
, 0);
5960 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
5961 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
5963 /* Note that we need not handle conditional operations here since fold
5964 already handles those cases. So just do arithmetic here. */
5968 /* For a constant, we can always simplify if we are a multiply
5969 or (for divide and modulus) if it is a multiple of our constant. */
5970 if (code
== MULT_EXPR
5971 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
5972 return const_binop (code
, fold_convert (ctype
, t
),
5973 fold_convert (ctype
, c
));
5976 CASE_CONVERT
: case NON_LVALUE_EXPR
:
5977 /* If op0 is an expression ... */
5978 if ((COMPARISON_CLASS_P (op0
)
5979 || UNARY_CLASS_P (op0
)
5980 || BINARY_CLASS_P (op0
)
5981 || VL_EXP_CLASS_P (op0
)
5982 || EXPRESSION_CLASS_P (op0
))
5983 /* ... and has wrapping overflow, and its type is smaller
5984 than ctype, then we cannot pass through as widening. */
5985 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
5986 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
5987 && (TYPE_PRECISION (ctype
)
5988 > TYPE_PRECISION (TREE_TYPE (op0
))))
5989 /* ... or this is a truncation (t is narrower than op0),
5990 then we cannot pass through this narrowing. */
5991 || (TYPE_PRECISION (type
)
5992 < TYPE_PRECISION (TREE_TYPE (op0
)))
5993 /* ... or signedness changes for division or modulus,
5994 then we cannot pass through this conversion. */
5995 || (code
!= MULT_EXPR
5996 && (TYPE_UNSIGNED (ctype
)
5997 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
5998 /* ... or has undefined overflow while the converted to
5999 type has not, we cannot do the operation in the inner type
6000 as that would introduce undefined overflow. */
6001 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6002 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6003 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6006 /* Pass the constant down and see if we can make a simplification. If
6007 we can, replace this expression with the inner simplification for
6008 possible later conversion to our or some other type. */
6009 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6010 && TREE_CODE (t2
) == INTEGER_CST
6011 && !TREE_OVERFLOW (t2
)
6012 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6014 ? ctype
: NULL_TREE
,
6015 strict_overflow_p
))))
6020 /* If widening the type changes it from signed to unsigned, then we
6021 must avoid building ABS_EXPR itself as unsigned. */
6022 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6024 tree cstype
= (*signed_type_for
) (ctype
);
6025 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6028 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6029 return fold_convert (ctype
, t1
);
6033 /* If the constant is negative, we cannot simplify this. */
6034 if (tree_int_cst_sgn (c
) == -1)
6038 /* For division and modulus, type can't be unsigned, as e.g.
6039 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6040 For signed types, even with wrapping overflow, this is fine. */
6041 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6043 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6045 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6048 case MIN_EXPR
: case MAX_EXPR
:
6049 /* If widening the type changes the signedness, then we can't perform
6050 this optimization as that changes the result. */
6051 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6054 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6055 sub_strict_overflow_p
= false;
6056 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6057 &sub_strict_overflow_p
)) != 0
6058 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6059 &sub_strict_overflow_p
)) != 0)
6061 if (tree_int_cst_sgn (c
) < 0)
6062 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6063 if (sub_strict_overflow_p
)
6064 *strict_overflow_p
= true;
6065 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6066 fold_convert (ctype
, t2
));
6070 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6071 /* If the second operand is constant, this is a multiplication
6072 or floor division, by a power of two, so we can treat it that
6073 way unless the multiplier or divisor overflows. Signed
6074 left-shift overflow is implementation-defined rather than
6075 undefined in C90, so do not convert signed left shift into
6077 if (TREE_CODE (op1
) == INTEGER_CST
6078 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6079 /* const_binop may not detect overflow correctly,
6080 so check for it explicitly here. */
6081 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6082 && 0 != (t1
= fold_convert (ctype
,
6083 const_binop (LSHIFT_EXPR
,
6086 && !TREE_OVERFLOW (t1
))
6087 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6088 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6090 fold_convert (ctype
, op0
),
6092 c
, code
, wide_type
, strict_overflow_p
);
6095 case PLUS_EXPR
: case MINUS_EXPR
:
6096 /* See if we can eliminate the operation on both sides. If we can, we
6097 can return a new PLUS or MINUS. If we can't, the only remaining
6098 cases where we can do anything are if the second operand is a
6100 sub_strict_overflow_p
= false;
6101 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6102 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6103 if (t1
!= 0 && t2
!= 0
6104 && (code
== MULT_EXPR
6105 /* If not multiplication, we can only do this if both operands
6106 are divisible by c. */
6107 || (multiple_of_p (ctype
, op0
, c
)
6108 && multiple_of_p (ctype
, op1
, c
))))
6110 if (sub_strict_overflow_p
)
6111 *strict_overflow_p
= true;
6112 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6113 fold_convert (ctype
, t2
));
6116 /* If this was a subtraction, negate OP1 and set it to be an addition.
6117 This simplifies the logic below. */
6118 if (tcode
== MINUS_EXPR
)
6120 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6121 /* If OP1 was not easily negatable, the constant may be OP0. */
6122 if (TREE_CODE (op0
) == INTEGER_CST
)
6124 std::swap (op0
, op1
);
6129 if (TREE_CODE (op1
) != INTEGER_CST
)
6132 /* If either OP1 or C are negative, this optimization is not safe for
6133 some of the division and remainder types while for others we need
6134 to change the code. */
6135 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6137 if (code
== CEIL_DIV_EXPR
)
6138 code
= FLOOR_DIV_EXPR
;
6139 else if (code
== FLOOR_DIV_EXPR
)
6140 code
= CEIL_DIV_EXPR
;
6141 else if (code
!= MULT_EXPR
6142 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6146 /* If it's a multiply or a division/modulus operation of a multiple
6147 of our constant, do the operation and verify it doesn't overflow. */
6148 if (code
== MULT_EXPR
6149 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6151 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6152 fold_convert (ctype
, c
));
6153 /* We allow the constant to overflow with wrapping semantics. */
6155 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6161 /* If we have an unsigned type, we cannot widen the operation since it
6162 will change the result if the original computation overflowed. */
6163 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6166 /* If we were able to eliminate our operation from the first side,
6167 apply our operation to the second side and reform the PLUS. */
6168 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6169 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6171 /* The last case is if we are a multiply. In that case, we can
6172 apply the distributive law to commute the multiply and addition
6173 if the multiplication of the constants doesn't overflow
6174 and overflow is defined. With undefined overflow
6175 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6176 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6177 return fold_build2 (tcode
, ctype
,
6178 fold_build2 (code
, ctype
,
6179 fold_convert (ctype
, op0
),
6180 fold_convert (ctype
, c
)),
6186 /* We have a special case here if we are doing something like
6187 (C * 8) % 4 since we know that's zero. */
6188 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6189 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6190 /* If the multiplication can overflow we cannot optimize this. */
6191 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6192 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6193 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6195 *strict_overflow_p
= true;
6196 return omit_one_operand (type
, integer_zero_node
, op0
);
6199 /* ... fall through ... */
6201 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6202 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6203 /* If we can extract our operation from the LHS, do so and return a
6204 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6205 do something only if the second operand is a constant. */
6207 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6208 strict_overflow_p
)) != 0)
6209 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6210 fold_convert (ctype
, op1
));
6211 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6212 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6213 strict_overflow_p
)) != 0)
6214 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6215 fold_convert (ctype
, t1
));
6216 else if (TREE_CODE (op1
) != INTEGER_CST
)
6219 /* If these are the same operation types, we can associate them
6220 assuming no overflow. */
6223 bool overflow_p
= false;
6224 bool overflow_mul_p
;
6225 signop sign
= TYPE_SIGN (ctype
);
6226 wide_int mul
= wi::mul (op1
, c
, sign
, &overflow_mul_p
);
6227 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6229 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6232 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6233 wide_int_to_tree (ctype
, mul
));
6236 /* If these operations "cancel" each other, we have the main
6237 optimizations of this pass, which occur when either constant is a
6238 multiple of the other, in which case we replace this with either an
6239 operation or CODE or TCODE.
6241 If we have an unsigned type, we cannot do this since it will change
6242 the result if the original computation overflowed. */
6243 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6244 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6245 || (tcode
== MULT_EXPR
6246 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6247 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6248 && code
!= MULT_EXPR
)))
6250 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6252 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6253 *strict_overflow_p
= true;
6254 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6255 fold_convert (ctype
,
6256 const_binop (TRUNC_DIV_EXPR
,
6259 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6261 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6262 *strict_overflow_p
= true;
6263 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6264 fold_convert (ctype
,
6265 const_binop (TRUNC_DIV_EXPR
,
6278 /* Return a node which has the indicated constant VALUE (either 0 or
6279 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6280 and is of the indicated TYPE. */
6283 constant_boolean_node (bool value
, tree type
)
6285 if (type
== integer_type_node
)
6286 return value
? integer_one_node
: integer_zero_node
;
6287 else if (type
== boolean_type_node
)
6288 return value
? boolean_true_node
: boolean_false_node
;
6289 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6290 return build_vector_from_val (type
,
6291 build_int_cst (TREE_TYPE (type
),
6294 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6298 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6299 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6300 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6301 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6302 COND is the first argument to CODE; otherwise (as in the example
6303 given here), it is the second argument. TYPE is the type of the
6304 original expression. Return NULL_TREE if no simplification is
6308 fold_binary_op_with_conditional_arg (location_t loc
,
6309 enum tree_code code
,
6310 tree type
, tree op0
, tree op1
,
6311 tree cond
, tree arg
, int cond_first_p
)
6313 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6314 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6315 tree test
, true_value
, false_value
;
6316 tree lhs
= NULL_TREE
;
6317 tree rhs
= NULL_TREE
;
6318 enum tree_code cond_code
= COND_EXPR
;
6320 if (TREE_CODE (cond
) == COND_EXPR
6321 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6323 test
= TREE_OPERAND (cond
, 0);
6324 true_value
= TREE_OPERAND (cond
, 1);
6325 false_value
= TREE_OPERAND (cond
, 2);
6326 /* If this operand throws an expression, then it does not make
6327 sense to try to perform a logical or arithmetic operation
6329 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6331 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6336 tree testtype
= TREE_TYPE (cond
);
6338 true_value
= constant_boolean_node (true, testtype
);
6339 false_value
= constant_boolean_node (false, testtype
);
6342 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6343 cond_code
= VEC_COND_EXPR
;
6345 /* This transformation is only worthwhile if we don't have to wrap ARG
6346 in a SAVE_EXPR and the operation can be simplified without recursing
6347 on at least one of the branches once its pushed inside the COND_EXPR. */
6348 if (!TREE_CONSTANT (arg
)
6349 && (TREE_SIDE_EFFECTS (arg
)
6350 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6351 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6354 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6357 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6359 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6361 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6365 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6367 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6369 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6372 /* Check that we have simplified at least one of the branches. */
6373 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6376 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6380 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6382 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6383 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6384 ADDEND is the same as X.
6386 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6387 and finite. The problematic cases are when X is zero, and its mode
6388 has signed zeros. In the case of rounding towards -infinity,
6389 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6390 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6393 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6395 if (!real_zerop (addend
))
6398 /* Don't allow the fold with -fsignaling-nans. */
6399 if (HONOR_SNANS (element_mode (type
)))
6402 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6403 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6406 /* In a vector or complex, we would need to check the sign of all zeros. */
6407 if (TREE_CODE (addend
) != REAL_CST
)
6410 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6411 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6414 /* The mode has signed zeros, and we have to honor their sign.
6415 In this situation, there is only one case we can return true for.
6416 X - 0 is the same as X unless rounding towards -infinity is
6418 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6421 /* Subroutine of fold() that checks comparisons of built-in math
6422 functions against real constants.
6424 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6425 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6426 is the type of the result and ARG0 and ARG1 are the operands of the
6427 comparison. ARG1 must be a TREE_REAL_CST.
6429 The function returns the constant folded tree if a simplification
6430 can be made, and NULL_TREE otherwise. */
6433 fold_mathfn_compare (location_t loc
,
6434 enum built_in_function fcode
, enum tree_code code
,
6435 tree type
, tree arg0
, tree arg1
)
6439 if (BUILTIN_SQRT_P (fcode
))
6441 tree arg
= CALL_EXPR_ARG (arg0
, 0);
6442 machine_mode mode
= TYPE_MODE (TREE_TYPE (arg0
));
6444 c
= TREE_REAL_CST (arg1
);
6445 if (REAL_VALUE_NEGATIVE (c
))
6447 /* sqrt(x) < y is always false, if y is negative. */
6448 if (code
== EQ_EXPR
|| code
== LT_EXPR
|| code
== LE_EXPR
)
6449 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
6451 /* sqrt(x) > y is always true, if y is negative and we
6452 don't care about NaNs, i.e. negative values of x. */
6453 if (code
== NE_EXPR
|| !HONOR_NANS (mode
))
6454 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
6456 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6457 return fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6458 build_real (TREE_TYPE (arg
), dconst0
));
6460 else if (code
== GT_EXPR
|| code
== GE_EXPR
)
6464 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
6465 real_convert (&c2
, mode
, &c2
);
6467 if (REAL_VALUE_ISINF (c2
))
6469 /* sqrt(x) > y is x == +Inf, when y is very large. */
6470 if (HONOR_INFINITIES (mode
))
6471 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg
,
6472 build_real (TREE_TYPE (arg
), c2
));
6474 /* sqrt(x) > y is always false, when y is very large
6475 and we don't care about infinities. */
6476 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
6479 /* sqrt(x) > c is the same as x > c*c. */
6480 return fold_build2_loc (loc
, code
, type
, arg
,
6481 build_real (TREE_TYPE (arg
), c2
));
6483 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
6487 REAL_ARITHMETIC (c2
, MULT_EXPR
, c
, c
);
6488 real_convert (&c2
, mode
, &c2
);
6490 if (REAL_VALUE_ISINF (c2
))
6492 /* sqrt(x) < y is always true, when y is a very large
6493 value and we don't care about NaNs or Infinities. */
6494 if (! HONOR_NANS (mode
) && ! HONOR_INFINITIES (mode
))
6495 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
6497 /* sqrt(x) < y is x != +Inf when y is very large and we
6498 don't care about NaNs. */
6499 if (! HONOR_NANS (mode
))
6500 return fold_build2_loc (loc
, NE_EXPR
, type
, arg
,
6501 build_real (TREE_TYPE (arg
), c2
));
6503 /* sqrt(x) < y is x >= 0 when y is very large and we
6504 don't care about Infinities. */
6505 if (! HONOR_INFINITIES (mode
))
6506 return fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6507 build_real (TREE_TYPE (arg
), dconst0
));
6509 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6510 arg
= save_expr (arg
);
6511 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
6512 fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6513 build_real (TREE_TYPE (arg
),
6515 fold_build2_loc (loc
, NE_EXPR
, type
, arg
,
6516 build_real (TREE_TYPE (arg
),
6520 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6521 if (! HONOR_NANS (mode
))
6522 return fold_build2_loc (loc
, code
, type
, arg
,
6523 build_real (TREE_TYPE (arg
), c2
));
6525 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6526 arg
= save_expr (arg
);
6527 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
6528 fold_build2_loc (loc
, GE_EXPR
, type
, arg
,
6529 build_real (TREE_TYPE (arg
),
6531 fold_build2_loc (loc
, code
, type
, arg
,
6532 build_real (TREE_TYPE (arg
),
6540 /* Subroutine of fold() that optimizes comparisons against Infinities,
6541 either +Inf or -Inf.
6543 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6544 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6545 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6547 The function returns the constant folded tree if a simplification
6548 can be made, and NULL_TREE otherwise. */
6551 fold_inf_compare (location_t loc
, enum tree_code code
, tree type
,
6552 tree arg0
, tree arg1
)
6555 REAL_VALUE_TYPE max
;
6559 mode
= TYPE_MODE (TREE_TYPE (arg0
));
6561 /* For negative infinity swap the sense of the comparison. */
6562 neg
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
));
6564 code
= swap_tree_comparison (code
);
6569 /* x > +Inf is always false, if with ignore sNANs. */
6570 if (HONOR_SNANS (mode
))
6572 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
6575 /* x <= +Inf is always true, if we don't case about NaNs. */
6576 if (! HONOR_NANS (mode
))
6577 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
6579 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6580 arg0
= save_expr (arg0
);
6581 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
, arg0
);
6585 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6586 real_maxval (&max
, neg
, mode
);
6587 return fold_build2_loc (loc
, neg
? LT_EXPR
: GT_EXPR
, type
,
6588 arg0
, build_real (TREE_TYPE (arg0
), max
));
6591 /* x < +Inf is always equal to x <= DBL_MAX. */
6592 real_maxval (&max
, neg
, mode
);
6593 return fold_build2_loc (loc
, neg
? GE_EXPR
: LE_EXPR
, type
,
6594 arg0
, build_real (TREE_TYPE (arg0
), max
));
6597 /* x != +Inf is always equal to !(x > DBL_MAX). */
6598 real_maxval (&max
, neg
, mode
);
6599 if (! HONOR_NANS (mode
))
6600 return fold_build2_loc (loc
, neg
? GE_EXPR
: LE_EXPR
, type
,
6601 arg0
, build_real (TREE_TYPE (arg0
), max
));
6603 temp
= fold_build2_loc (loc
, neg
? LT_EXPR
: GT_EXPR
, type
,
6604 arg0
, build_real (TREE_TYPE (arg0
), max
));
6605 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, temp
);
6614 /* Subroutine of fold() that optimizes comparisons of a division by
6615 a nonzero integer constant against an integer constant, i.e.
6618 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6619 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6620 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6622 The function returns the constant folded tree if a simplification
6623 can be made, and NULL_TREE otherwise. */
6626 fold_div_compare (location_t loc
,
6627 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6629 tree prod
, tmp
, hi
, lo
;
6630 tree arg00
= TREE_OPERAND (arg0
, 0);
6631 tree arg01
= TREE_OPERAND (arg0
, 1);
6632 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6633 bool neg_overflow
= false;
6636 /* We have to do this the hard way to detect unsigned overflow.
6637 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6638 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6639 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6640 neg_overflow
= false;
6642 if (sign
== UNSIGNED
)
6644 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6645 build_int_cst (TREE_TYPE (arg01
), 1));
6648 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6649 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6650 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6651 -1, overflow
| TREE_OVERFLOW (prod
));
6653 else if (tree_int_cst_sgn (arg01
) >= 0)
6655 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6656 build_int_cst (TREE_TYPE (arg01
), 1));
6657 switch (tree_int_cst_sgn (arg1
))
6660 neg_overflow
= true;
6661 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6666 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6671 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6681 /* A negative divisor reverses the relational operators. */
6682 code
= swap_tree_comparison (code
);
6684 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6685 build_int_cst (TREE_TYPE (arg01
), 1));
6686 switch (tree_int_cst_sgn (arg1
))
6689 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6694 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6699 neg_overflow
= true;
6700 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6712 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6713 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6714 if (TREE_OVERFLOW (hi
))
6715 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6716 if (TREE_OVERFLOW (lo
))
6717 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6718 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6721 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6722 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6723 if (TREE_OVERFLOW (hi
))
6724 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6725 if (TREE_OVERFLOW (lo
))
6726 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6727 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6730 if (TREE_OVERFLOW (lo
))
6732 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6733 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6735 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6738 if (TREE_OVERFLOW (hi
))
6740 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6741 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6743 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6746 if (TREE_OVERFLOW (hi
))
6748 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6749 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6751 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6754 if (TREE_OVERFLOW (lo
))
6756 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6757 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6759 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6769 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6770 equality/inequality test, then return a simplified form of the test
6771 using a sign testing. Otherwise return NULL. TYPE is the desired
6775 fold_single_bit_test_into_sign_test (location_t loc
,
6776 enum tree_code code
, tree arg0
, tree arg1
,
6779 /* If this is testing a single bit, we can optimize the test. */
6780 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6781 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6782 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6784 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6785 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6786 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6788 if (arg00
!= NULL_TREE
6789 /* This is only a win if casting to a signed type is cheap,
6790 i.e. when arg00's type is not a partial mode. */
6791 && TYPE_PRECISION (TREE_TYPE (arg00
))
6792 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6794 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6795 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6797 fold_convert_loc (loc
, stype
, arg00
),
6798 build_int_cst (stype
, 0));
6805 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6806 equality/inequality test, then return a simplified form of
6807 the test using shifts and logical operations. Otherwise return
6808 NULL. TYPE is the desired result type. */
6811 fold_single_bit_test (location_t loc
, enum tree_code code
,
6812 tree arg0
, tree arg1
, tree result_type
)
6814 /* If this is testing a single bit, we can optimize the test. */
6815 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6816 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6817 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6819 tree inner
= TREE_OPERAND (arg0
, 0);
6820 tree type
= TREE_TYPE (arg0
);
6821 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6822 machine_mode operand_mode
= TYPE_MODE (type
);
6824 tree signed_type
, unsigned_type
, intermediate_type
;
6827 /* First, see if we can fold the single bit test into a sign-bit
6829 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6834 /* Otherwise we have (A & C) != 0 where C is a single bit,
6835 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6836 Similarly for (A & C) == 0. */
6838 /* If INNER is a right shift of a constant and it plus BITNUM does
6839 not overflow, adjust BITNUM and INNER. */
6840 if (TREE_CODE (inner
) == RSHIFT_EXPR
6841 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6842 && bitnum
< TYPE_PRECISION (type
)
6843 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6844 TYPE_PRECISION (type
) - bitnum
))
6846 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6847 inner
= TREE_OPERAND (inner
, 0);
6850 /* If we are going to be able to omit the AND below, we must do our
6851 operations as unsigned. If we must use the AND, we have a choice.
6852 Normally unsigned is faster, but for some machines signed is. */
6853 #ifdef LOAD_EXTEND_OP
6854 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
6855 && !flag_syntax_only
) ? 0 : 1;
6860 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6861 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6862 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6863 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6866 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6867 inner
, size_int (bitnum
));
6869 one
= build_int_cst (intermediate_type
, 1);
6871 if (code
== EQ_EXPR
)
6872 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6874 /* Put the AND last so it can combine with more things. */
6875 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6877 /* Make sure to return the proper type. */
6878 inner
= fold_convert_loc (loc
, result_type
, inner
);
6885 /* Check whether we are allowed to reorder operands arg0 and arg1,
6886 such that the evaluation of arg1 occurs before arg0. */
6889 reorder_operands_p (const_tree arg0
, const_tree arg1
)
6891 if (! flag_evaluation_order
)
6893 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
6895 return ! TREE_SIDE_EFFECTS (arg0
)
6896 && ! TREE_SIDE_EFFECTS (arg1
);
6899 /* Test whether it is preferable two swap two operands, ARG0 and
6900 ARG1, for example because ARG0 is an integer constant and ARG1
6901 isn't. If REORDER is true, only recommend swapping if we can
6902 evaluate the operands in reverse order. */
6905 tree_swap_operands_p (const_tree arg0
, const_tree arg1
, bool reorder
)
6907 if (CONSTANT_CLASS_P (arg1
))
6909 if (CONSTANT_CLASS_P (arg0
))
6915 if (TREE_CONSTANT (arg1
))
6917 if (TREE_CONSTANT (arg0
))
6920 if (reorder
&& flag_evaluation_order
6921 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
6924 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6925 for commutative and comparison operators. Ensuring a canonical
6926 form allows the optimizers to find additional redundancies without
6927 having to explicitly check for both orderings. */
6928 if (TREE_CODE (arg0
) == SSA_NAME
6929 && TREE_CODE (arg1
) == SSA_NAME
6930 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6933 /* Put SSA_NAMEs last. */
6934 if (TREE_CODE (arg1
) == SSA_NAME
)
6936 if (TREE_CODE (arg0
) == SSA_NAME
)
6939 /* Put variables last. */
6948 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6949 ARG0 is extended to a wider type. */
6952 fold_widened_comparison (location_t loc
, enum tree_code code
,
6953 tree type
, tree arg0
, tree arg1
)
6955 tree arg0_unw
= get_unwidened (arg0
, NULL_TREE
);
6957 tree shorter_type
, outer_type
;
6961 if (arg0_unw
== arg0
)
6963 shorter_type
= TREE_TYPE (arg0_unw
);
6965 /* Disable this optimization if we're casting a function pointer
6966 type on targets that require function pointer canonicalization. */
6967 if (targetm
.have_canonicalize_funcptr_for_compare ()
6968 && TREE_CODE (shorter_type
) == POINTER_TYPE
6969 && TREE_CODE (TREE_TYPE (shorter_type
)) == FUNCTION_TYPE
)
6972 if (TYPE_PRECISION (TREE_TYPE (arg0
)) <= TYPE_PRECISION (shorter_type
))
6975 arg1_unw
= get_unwidened (arg1
, NULL_TREE
);
6977 /* If possible, express the comparison in the shorter mode. */
6978 if ((code
== EQ_EXPR
|| code
== NE_EXPR
6979 || TYPE_UNSIGNED (TREE_TYPE (arg0
)) == TYPE_UNSIGNED (shorter_type
))
6980 && (TREE_TYPE (arg1_unw
) == shorter_type
6981 || ((TYPE_PRECISION (shorter_type
)
6982 >= TYPE_PRECISION (TREE_TYPE (arg1_unw
)))
6983 && (TYPE_UNSIGNED (shorter_type
)
6984 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw
))))
6985 || (TREE_CODE (arg1_unw
) == INTEGER_CST
6986 && (TREE_CODE (shorter_type
) == INTEGER_TYPE
6987 || TREE_CODE (shorter_type
) == BOOLEAN_TYPE
)
6988 && int_fits_type_p (arg1_unw
, shorter_type
))))
6989 return fold_build2_loc (loc
, code
, type
, arg0_unw
,
6990 fold_convert_loc (loc
, shorter_type
, arg1_unw
));
6992 if (TREE_CODE (arg1_unw
) != INTEGER_CST
6993 || TREE_CODE (shorter_type
) != INTEGER_TYPE
6994 || !int_fits_type_p (arg1_unw
, shorter_type
))
6997 /* If we are comparing with the integer that does not fit into the range
6998 of the shorter type, the result is known. */
6999 outer_type
= TREE_TYPE (arg1_unw
);
7000 min
= lower_bound_in_type (outer_type
, shorter_type
);
7001 max
= upper_bound_in_type (outer_type
, shorter_type
);
7003 above
= integer_nonzerop (fold_relational_const (LT_EXPR
, type
,
7005 below
= integer_nonzerop (fold_relational_const (LT_EXPR
, type
,
7012 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
7017 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
7023 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
7025 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
7030 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
7032 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
7041 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7042 ARG0 just the signedness is changed. */
7045 fold_sign_changed_comparison (location_t loc
, enum tree_code code
, tree type
,
7046 tree arg0
, tree arg1
)
7049 tree inner_type
, outer_type
;
7051 if (!CONVERT_EXPR_P (arg0
))
7054 outer_type
= TREE_TYPE (arg0
);
7055 arg0_inner
= TREE_OPERAND (arg0
, 0);
7056 inner_type
= TREE_TYPE (arg0_inner
);
7058 /* Disable this optimization if we're casting a function pointer
7059 type on targets that require function pointer canonicalization. */
7060 if (targetm
.have_canonicalize_funcptr_for_compare ()
7061 && TREE_CODE (inner_type
) == POINTER_TYPE
7062 && TREE_CODE (TREE_TYPE (inner_type
)) == FUNCTION_TYPE
)
7065 if (TYPE_PRECISION (inner_type
) != TYPE_PRECISION (outer_type
))
7068 if (TREE_CODE (arg1
) != INTEGER_CST
7069 && !(CONVERT_EXPR_P (arg1
)
7070 && TREE_TYPE (TREE_OPERAND (arg1
, 0)) == inner_type
))
7073 if (TYPE_UNSIGNED (inner_type
) != TYPE_UNSIGNED (outer_type
)
7078 if (POINTER_TYPE_P (inner_type
) != POINTER_TYPE_P (outer_type
))
7081 if (TREE_CODE (arg1
) == INTEGER_CST
)
7082 arg1
= force_fit_type (inner_type
, wi::to_widest (arg1
), 0,
7083 TREE_OVERFLOW (arg1
));
7085 arg1
= fold_convert_loc (loc
, inner_type
, arg1
);
7087 return fold_build2_loc (loc
, code
, type
, arg0_inner
, arg1
);
7091 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7092 means A >= Y && A != MAX, but in this case we know that
7093 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7096 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
7098 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
7100 if (TREE_CODE (bound
) == LT_EXPR
)
7101 a
= TREE_OPERAND (bound
, 0);
7102 else if (TREE_CODE (bound
) == GT_EXPR
)
7103 a
= TREE_OPERAND (bound
, 1);
7107 typea
= TREE_TYPE (a
);
7108 if (!INTEGRAL_TYPE_P (typea
)
7109 && !POINTER_TYPE_P (typea
))
7112 if (TREE_CODE (ineq
) == LT_EXPR
)
7114 a1
= TREE_OPERAND (ineq
, 1);
7115 y
= TREE_OPERAND (ineq
, 0);
7117 else if (TREE_CODE (ineq
) == GT_EXPR
)
7119 a1
= TREE_OPERAND (ineq
, 0);
7120 y
= TREE_OPERAND (ineq
, 1);
7125 if (TREE_TYPE (a1
) != typea
)
7128 if (POINTER_TYPE_P (typea
))
7130 /* Convert the pointer types into integer before taking the difference. */
7131 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
7132 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
7133 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
7136 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
7138 if (!diff
|| !integer_onep (diff
))
7141 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
7144 /* Fold a sum or difference of at least one multiplication.
7145 Returns the folded tree or NULL if no simplification could be made. */
7148 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
7149 tree arg0
, tree arg1
)
7151 tree arg00
, arg01
, arg10
, arg11
;
7152 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
7154 /* (A * C) +- (B * C) -> (A+-B) * C.
7155 (A * C) +- A -> A * (C+-1).
7156 We are most concerned about the case where C is a constant,
7157 but other combinations show up during loop reduction. Since
7158 it is not difficult, try all four possibilities. */
7160 if (TREE_CODE (arg0
) == MULT_EXPR
)
7162 arg00
= TREE_OPERAND (arg0
, 0);
7163 arg01
= TREE_OPERAND (arg0
, 1);
7165 else if (TREE_CODE (arg0
) == INTEGER_CST
)
7167 arg00
= build_one_cst (type
);
7172 /* We cannot generate constant 1 for fract. */
7173 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7176 arg01
= build_one_cst (type
);
7178 if (TREE_CODE (arg1
) == MULT_EXPR
)
7180 arg10
= TREE_OPERAND (arg1
, 0);
7181 arg11
= TREE_OPERAND (arg1
, 1);
7183 else if (TREE_CODE (arg1
) == INTEGER_CST
)
7185 arg10
= build_one_cst (type
);
7186 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7187 the purpose of this canonicalization. */
7188 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
7189 && negate_expr_p (arg1
)
7190 && code
== PLUS_EXPR
)
7192 arg11
= negate_expr (arg1
);
7200 /* We cannot generate constant 1 for fract. */
7201 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
7204 arg11
= build_one_cst (type
);
7208 if (operand_equal_p (arg01
, arg11
, 0))
7209 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
7210 else if (operand_equal_p (arg00
, arg10
, 0))
7211 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
7212 else if (operand_equal_p (arg00
, arg11
, 0))
7213 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
7214 else if (operand_equal_p (arg01
, arg10
, 0))
7215 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
7217 /* No identical multiplicands; see if we can find a common
7218 power-of-two factor in non-power-of-two multiplies. This
7219 can help in multi-dimensional array access. */
7220 else if (tree_fits_shwi_p (arg01
)
7221 && tree_fits_shwi_p (arg11
))
7223 HOST_WIDE_INT int01
, int11
, tmp
;
7226 int01
= tree_to_shwi (arg01
);
7227 int11
= tree_to_shwi (arg11
);
7229 /* Move min of absolute values to int11. */
7230 if (absu_hwi (int01
) < absu_hwi (int11
))
7232 tmp
= int01
, int01
= int11
, int11
= tmp
;
7233 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
7240 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
7241 /* The remainder should not be a constant, otherwise we
7242 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7243 increased the number of multiplications necessary. */
7244 && TREE_CODE (arg10
) != INTEGER_CST
)
7246 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
7247 build_int_cst (TREE_TYPE (arg00
),
7252 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
7257 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7258 fold_build2_loc (loc
, code
, type
,
7259 fold_convert_loc (loc
, type
, alt0
),
7260 fold_convert_loc (loc
, type
, alt1
)),
7261 fold_convert_loc (loc
, type
, same
));
7266 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7267 specified by EXPR into the buffer PTR of length LEN bytes.
7268 Return the number of bytes placed in the buffer, or zero
7272 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7274 tree type
= TREE_TYPE (expr
);
7275 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7276 int byte
, offset
, word
, words
;
7277 unsigned char value
;
7279 if ((off
== -1 && total_bytes
> len
)
7280 || off
>= total_bytes
)
7284 words
= total_bytes
/ UNITS_PER_WORD
;
7286 for (byte
= 0; byte
< total_bytes
; byte
++)
7288 int bitpos
= byte
* BITS_PER_UNIT
;
7289 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7291 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
7293 if (total_bytes
> UNITS_PER_WORD
)
7295 word
= byte
/ UNITS_PER_WORD
;
7296 if (WORDS_BIG_ENDIAN
)
7297 word
= (words
- 1) - word
;
7298 offset
= word
* UNITS_PER_WORD
;
7299 if (BYTES_BIG_ENDIAN
)
7300 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7302 offset
+= byte
% UNITS_PER_WORD
;
7305 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7307 && offset
- off
< len
)
7308 ptr
[offset
- off
] = value
;
7310 return MIN (len
, total_bytes
- off
);
7314 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7315 specified by EXPR into the buffer PTR of length LEN bytes.
7316 Return the number of bytes placed in the buffer, or zero
7320 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7322 tree type
= TREE_TYPE (expr
);
7323 machine_mode mode
= TYPE_MODE (type
);
7324 int total_bytes
= GET_MODE_SIZE (mode
);
7325 FIXED_VALUE_TYPE value
;
7326 tree i_value
, i_type
;
7328 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7331 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7333 if (NULL_TREE
== i_type
7334 || TYPE_PRECISION (i_type
) != total_bytes
)
7337 value
= TREE_FIXED_CST (expr
);
7338 i_value
= double_int_to_tree (i_type
, value
.data
);
7340 return native_encode_int (i_value
, ptr
, len
, off
);
7344 /* Subroutine of native_encode_expr. Encode the REAL_CST
7345 specified by EXPR into the buffer PTR of length LEN bytes.
7346 Return the number of bytes placed in the buffer, or zero
7350 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7352 tree type
= TREE_TYPE (expr
);
7353 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7354 int byte
, offset
, word
, words
, bitpos
;
7355 unsigned char value
;
7357 /* There are always 32 bits in each long, no matter the size of
7358 the hosts long. We handle floating point representations with
7362 if ((off
== -1 && total_bytes
> len
)
7363 || off
>= total_bytes
)
7367 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7369 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7371 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7372 bitpos
+= BITS_PER_UNIT
)
7374 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7375 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7377 if (UNITS_PER_WORD
< 4)
7379 word
= byte
/ UNITS_PER_WORD
;
7380 if (WORDS_BIG_ENDIAN
)
7381 word
= (words
- 1) - word
;
7382 offset
= word
* UNITS_PER_WORD
;
7383 if (BYTES_BIG_ENDIAN
)
7384 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7386 offset
+= byte
% UNITS_PER_WORD
;
7389 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7390 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7392 && offset
- off
< len
)
7393 ptr
[offset
- off
] = value
;
7395 return MIN (len
, total_bytes
- off
);
7398 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7399 specified by EXPR into the buffer PTR of length LEN bytes.
7400 Return the number of bytes placed in the buffer, or zero
7404 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7409 part
= TREE_REALPART (expr
);
7410 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7414 part
= TREE_IMAGPART (expr
);
7416 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7417 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7421 return rsize
+ isize
;
7425 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7426 specified by EXPR into the buffer PTR of length LEN bytes.
7427 Return the number of bytes placed in the buffer, or zero
7431 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7438 count
= VECTOR_CST_NELTS (expr
);
7439 itype
= TREE_TYPE (TREE_TYPE (expr
));
7440 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7441 for (i
= 0; i
< count
; i
++)
7448 elem
= VECTOR_CST_ELT (expr
, i
);
7449 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7450 if ((off
== -1 && res
!= size
)
7463 /* Subroutine of native_encode_expr. Encode the STRING_CST
7464 specified by EXPR into the buffer PTR of length LEN bytes.
7465 Return the number of bytes placed in the buffer, or zero
7469 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7471 tree type
= TREE_TYPE (expr
);
7472 HOST_WIDE_INT total_bytes
;
7474 if (TREE_CODE (type
) != ARRAY_TYPE
7475 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7476 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7477 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7479 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7480 if ((off
== -1 && total_bytes
> len
)
7481 || off
>= total_bytes
)
7485 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7488 if (off
< TREE_STRING_LENGTH (expr
))
7490 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7491 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7493 memset (ptr
+ written
, 0,
7494 MIN (total_bytes
- written
, len
- written
));
7497 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7498 return MIN (total_bytes
- off
, len
);
7502 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7503 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7504 buffer PTR of length LEN bytes. If OFF is not -1 then start
7505 the encoding at byte offset OFF and encode at most LEN bytes.
7506 Return the number of bytes placed in the buffer, or zero upon failure. */
7509 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7511 switch (TREE_CODE (expr
))
7514 return native_encode_int (expr
, ptr
, len
, off
);
7517 return native_encode_real (expr
, ptr
, len
, off
);
7520 return native_encode_fixed (expr
, ptr
, len
, off
);
7523 return native_encode_complex (expr
, ptr
, len
, off
);
7526 return native_encode_vector (expr
, ptr
, len
, off
);
7529 return native_encode_string (expr
, ptr
, len
, off
);
7537 /* Subroutine of native_interpret_expr. Interpret the contents of
7538 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7539 If the buffer cannot be interpreted, return NULL_TREE. */
7542 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7544 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7546 if (total_bytes
> len
7547 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7550 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7552 return wide_int_to_tree (type
, result
);
7556 /* Subroutine of native_interpret_expr. Interpret the contents of
7557 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7558 If the buffer cannot be interpreted, return NULL_TREE. */
7561 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7563 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7565 FIXED_VALUE_TYPE fixed_value
;
7567 if (total_bytes
> len
7568 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7571 result
= double_int::from_buffer (ptr
, total_bytes
);
7572 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7574 return build_fixed (type
, fixed_value
);
7578 /* Subroutine of native_interpret_expr. Interpret the contents of
7579 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7580 If the buffer cannot be interpreted, return NULL_TREE. */
7583 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7585 machine_mode mode
= TYPE_MODE (type
);
7586 int total_bytes
= GET_MODE_SIZE (mode
);
7587 int byte
, offset
, word
, words
, bitpos
;
7588 unsigned char value
;
7589 /* There are always 32 bits in each long, no matter the size of
7590 the hosts long. We handle floating point representations with
7595 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7596 if (total_bytes
> len
|| total_bytes
> 24)
7598 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7600 memset (tmp
, 0, sizeof (tmp
));
7601 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7602 bitpos
+= BITS_PER_UNIT
)
7604 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7605 if (UNITS_PER_WORD
< 4)
7607 word
= byte
/ UNITS_PER_WORD
;
7608 if (WORDS_BIG_ENDIAN
)
7609 word
= (words
- 1) - word
;
7610 offset
= word
* UNITS_PER_WORD
;
7611 if (BYTES_BIG_ENDIAN
)
7612 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7614 offset
+= byte
% UNITS_PER_WORD
;
7617 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7618 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7620 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7623 real_from_target (&r
, tmp
, mode
);
7624 return build_real (type
, r
);
7628 /* Subroutine of native_interpret_expr. Interpret the contents of
7629 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7630 If the buffer cannot be interpreted, return NULL_TREE. */
7633 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7635 tree etype
, rpart
, ipart
;
7638 etype
= TREE_TYPE (type
);
7639 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7642 rpart
= native_interpret_expr (etype
, ptr
, size
);
7645 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7648 return build_complex (type
, rpart
, ipart
);
7652 /* Subroutine of native_interpret_expr. Interpret the contents of
7653 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7654 If the buffer cannot be interpreted, return NULL_TREE. */
7657 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7663 etype
= TREE_TYPE (type
);
7664 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7665 count
= TYPE_VECTOR_SUBPARTS (type
);
7666 if (size
* count
> len
)
7669 elements
= XALLOCAVEC (tree
, count
);
7670 for (i
= count
- 1; i
>= 0; i
--)
7672 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7677 return build_vector (type
, elements
);
7681 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7682 the buffer PTR of length LEN as a constant of type TYPE. For
7683 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7684 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7685 return NULL_TREE. */
7688 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7690 switch (TREE_CODE (type
))
7696 case REFERENCE_TYPE
:
7697 return native_interpret_int (type
, ptr
, len
);
7700 return native_interpret_real (type
, ptr
, len
);
7702 case FIXED_POINT_TYPE
:
7703 return native_interpret_fixed (type
, ptr
, len
);
7706 return native_interpret_complex (type
, ptr
, len
);
7709 return native_interpret_vector (type
, ptr
, len
);
7716 /* Returns true if we can interpret the contents of a native encoding
7720 can_native_interpret_type_p (tree type
)
7722 switch (TREE_CODE (type
))
7728 case REFERENCE_TYPE
:
7729 case FIXED_POINT_TYPE
:
7739 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7740 TYPE at compile-time. If we're unable to perform the conversion
7741 return NULL_TREE. */
7744 fold_view_convert_expr (tree type
, tree expr
)
7746 /* We support up to 512-bit values (for V8DFmode). */
7747 unsigned char buffer
[64];
7750 /* Check that the host and target are sane. */
7751 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7754 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7758 return native_interpret_expr (type
, buffer
, len
);
7761 /* Build an expression for the address of T. Folds away INDIRECT_REF
7762 to avoid confusing the gimplify process. */
7765 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7767 /* The size of the object is not relevant when talking about its address. */
7768 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7769 t
= TREE_OPERAND (t
, 0);
7771 if (TREE_CODE (t
) == INDIRECT_REF
)
7773 t
= TREE_OPERAND (t
, 0);
7775 if (TREE_TYPE (t
) != ptrtype
)
7776 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7778 else if (TREE_CODE (t
) == MEM_REF
7779 && integer_zerop (TREE_OPERAND (t
, 1)))
7780 return TREE_OPERAND (t
, 0);
7781 else if (TREE_CODE (t
) == MEM_REF
7782 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7783 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7784 TREE_OPERAND (t
, 0),
7785 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7786 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7788 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7790 if (TREE_TYPE (t
) != ptrtype
)
7791 t
= fold_convert_loc (loc
, ptrtype
, t
);
7794 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7799 /* Build an expression for the address of T. */
7802 build_fold_addr_expr_loc (location_t loc
, tree t
)
7804 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7806 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7809 /* Fold a unary expression of code CODE and type TYPE with operand
7810 OP0. Return the folded expression if folding is successful.
7811 Otherwise, return NULL_TREE. */
7814 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7818 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7820 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7821 && TREE_CODE_LENGTH (code
) == 1);
7826 if (CONVERT_EXPR_CODE_P (code
)
7827 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7829 /* Don't use STRIP_NOPS, because signedness of argument type
7831 STRIP_SIGN_NOPS (arg0
);
7835 /* Strip any conversions that don't change the mode. This
7836 is safe for every expression, except for a comparison
7837 expression because its signedness is derived from its
7840 Note that this is done as an internal manipulation within
7841 the constant folder, in order to find the simplest
7842 representation of the arguments so that their form can be
7843 studied. In any cases, the appropriate type conversions
7844 should be put back in the tree that will get out of the
7849 if (CONSTANT_CLASS_P (arg0
))
7851 tree tem
= const_unop (code
, type
, arg0
);
7854 if (TREE_TYPE (tem
) != type
)
7855 tem
= fold_convert_loc (loc
, type
, tem
);
7861 tem
= generic_simplify (loc
, code
, type
, op0
);
7865 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7867 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7868 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7869 fold_build1_loc (loc
, code
, type
,
7870 fold_convert_loc (loc
, TREE_TYPE (op0
),
7871 TREE_OPERAND (arg0
, 1))));
7872 else if (TREE_CODE (arg0
) == COND_EXPR
)
7874 tree arg01
= TREE_OPERAND (arg0
, 1);
7875 tree arg02
= TREE_OPERAND (arg0
, 2);
7876 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7877 arg01
= fold_build1_loc (loc
, code
, type
,
7878 fold_convert_loc (loc
,
7879 TREE_TYPE (op0
), arg01
));
7880 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7881 arg02
= fold_build1_loc (loc
, code
, type
,
7882 fold_convert_loc (loc
,
7883 TREE_TYPE (op0
), arg02
));
7884 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7887 /* If this was a conversion, and all we did was to move into
7888 inside the COND_EXPR, bring it back out. But leave it if
7889 it is a conversion from integer to integer and the
7890 result precision is no wider than a word since such a
7891 conversion is cheap and may be optimized away by combine,
7892 while it couldn't if it were outside the COND_EXPR. Then return
7893 so we don't get into an infinite recursion loop taking the
7894 conversion out and then back in. */
7896 if ((CONVERT_EXPR_CODE_P (code
)
7897 || code
== NON_LVALUE_EXPR
)
7898 && TREE_CODE (tem
) == COND_EXPR
7899 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7900 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7901 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7902 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7903 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7904 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7905 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7907 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7908 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7909 || flag_syntax_only
))
7910 tem
= build1_loc (loc
, code
, type
,
7912 TREE_TYPE (TREE_OPERAND
7913 (TREE_OPERAND (tem
, 1), 0)),
7914 TREE_OPERAND (tem
, 0),
7915 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7916 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7924 case NON_LVALUE_EXPR
:
7925 if (!maybe_lvalue_p (op0
))
7926 return fold_convert_loc (loc
, type
, op0
);
7931 case FIX_TRUNC_EXPR
:
7932 if (COMPARISON_CLASS_P (op0
))
7934 /* If we have (type) (a CMP b) and type is an integral type, return
7935 new expression involving the new type. Canonicalize
7936 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7938 Do not fold the result as that would not simplify further, also
7939 folding again results in recursions. */
7940 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7941 return build2_loc (loc
, TREE_CODE (op0
), type
,
7942 TREE_OPERAND (op0
, 0),
7943 TREE_OPERAND (op0
, 1));
7944 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7945 && TREE_CODE (type
) != VECTOR_TYPE
)
7946 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7947 constant_boolean_node (true, type
),
7948 constant_boolean_node (false, type
));
7951 /* Handle (T *)&A.B.C for A being of type T and B and C
7952 living at offset zero. This occurs frequently in
7953 C++ upcasting and then accessing the base. */
7954 if (TREE_CODE (op0
) == ADDR_EXPR
7955 && POINTER_TYPE_P (type
)
7956 && handled_component_p (TREE_OPERAND (op0
, 0)))
7958 HOST_WIDE_INT bitsize
, bitpos
;
7961 int unsignedp
, volatilep
;
7962 tree base
= TREE_OPERAND (op0
, 0);
7963 base
= get_inner_reference (base
, &bitsize
, &bitpos
, &offset
,
7964 &mode
, &unsignedp
, &volatilep
, false);
7965 /* If the reference was to a (constant) zero offset, we can use
7966 the address of the base if it has the same base type
7967 as the result type and the pointer type is unqualified. */
7968 if (! offset
&& bitpos
== 0
7969 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7970 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7971 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7972 return fold_convert_loc (loc
, type
,
7973 build_fold_addr_expr_loc (loc
, base
));
7976 if (TREE_CODE (op0
) == MODIFY_EXPR
7977 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7978 /* Detect assigning a bitfield. */
7979 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7981 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7983 /* Don't leave an assignment inside a conversion
7984 unless assigning a bitfield. */
7985 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7986 /* First do the assignment, then return converted constant. */
7987 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7988 TREE_NO_WARNING (tem
) = 1;
7989 TREE_USED (tem
) = 1;
7993 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7994 constants (if x has signed type, the sign bit cannot be set
7995 in c). This folds extension into the BIT_AND_EXPR.
7996 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7997 very likely don't have maximal range for their precision and this
7998 transformation effectively doesn't preserve non-maximal ranges. */
7999 if (TREE_CODE (type
) == INTEGER_TYPE
8000 && TREE_CODE (op0
) == BIT_AND_EXPR
8001 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
8003 tree and_expr
= op0
;
8004 tree and0
= TREE_OPERAND (and_expr
, 0);
8005 tree and1
= TREE_OPERAND (and_expr
, 1);
8008 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
8009 || (TYPE_PRECISION (type
)
8010 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
8012 else if (TYPE_PRECISION (TREE_TYPE (and1
))
8013 <= HOST_BITS_PER_WIDE_INT
8014 && tree_fits_uhwi_p (and1
))
8016 unsigned HOST_WIDE_INT cst
;
8018 cst
= tree_to_uhwi (and1
);
8019 cst
&= HOST_WIDE_INT_M1U
8020 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
8021 change
= (cst
== 0);
8022 #ifdef LOAD_EXTEND_OP
8024 && !flag_syntax_only
8025 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
8028 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
8029 and0
= fold_convert_loc (loc
, uns
, and0
);
8030 and1
= fold_convert_loc (loc
, uns
, and1
);
8036 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
8037 TREE_OVERFLOW (and1
));
8038 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
8039 fold_convert_loc (loc
, type
, and0
), tem
);
8043 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8044 when one of the new casts will fold away. Conservatively we assume
8045 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8046 if (POINTER_TYPE_P (type
)
8047 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8048 && (!TYPE_RESTRICT (type
) || TYPE_RESTRICT (TREE_TYPE (arg0
)))
8049 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8050 || TREE_CODE (TREE_OPERAND (arg0
, 0)) == NOP_EXPR
8051 || TREE_CODE (TREE_OPERAND (arg0
, 1)) == NOP_EXPR
))
8053 tree arg00
= TREE_OPERAND (arg0
, 0);
8054 tree arg01
= TREE_OPERAND (arg0
, 1);
8056 return fold_build_pointer_plus_loc
8057 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
8060 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8061 of the same precision, and X is an integer type not narrower than
8062 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8063 if (INTEGRAL_TYPE_P (type
)
8064 && TREE_CODE (op0
) == BIT_NOT_EXPR
8065 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8066 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
8067 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
8069 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
8070 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
8071 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
8072 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
8073 fold_convert_loc (loc
, type
, tem
));
8076 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8077 type of X and Y (integer types only). */
8078 if (INTEGRAL_TYPE_P (type
)
8079 && TREE_CODE (op0
) == MULT_EXPR
8080 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8081 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
8083 /* Be careful not to introduce new overflows. */
8085 if (TYPE_OVERFLOW_WRAPS (type
))
8088 mult_type
= unsigned_type_for (type
);
8090 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
8092 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
8093 fold_convert_loc (loc
, mult_type
,
8094 TREE_OPERAND (op0
, 0)),
8095 fold_convert_loc (loc
, mult_type
,
8096 TREE_OPERAND (op0
, 1)));
8097 return fold_convert_loc (loc
, type
, tem
);
8103 case VIEW_CONVERT_EXPR
:
8104 if (TREE_CODE (op0
) == MEM_REF
)
8105 return fold_build2_loc (loc
, MEM_REF
, type
,
8106 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
8111 tem
= fold_negate_expr (loc
, arg0
);
8113 return fold_convert_loc (loc
, type
, tem
);
8117 /* Convert fabs((double)float) into (double)fabsf(float). */
8118 if (TREE_CODE (arg0
) == NOP_EXPR
8119 && TREE_CODE (type
) == REAL_TYPE
)
8121 tree targ0
= strip_float_extensions (arg0
);
8123 return fold_convert_loc (loc
, type
,
8124 fold_build1_loc (loc
, ABS_EXPR
,
8128 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8129 else if (TREE_CODE (arg0
) == ABS_EXPR
)
8132 /* Strip sign ops from argument. */
8133 if (TREE_CODE (type
) == REAL_TYPE
)
8135 tem
= fold_strip_sign_ops (arg0
);
8137 return fold_build1_loc (loc
, ABS_EXPR
, type
,
8138 fold_convert_loc (loc
, type
, tem
));
8143 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8144 return fold_convert_loc (loc
, type
, arg0
);
8145 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
8147 tree itype
= TREE_TYPE (type
);
8148 tree rpart
= fold_convert_loc (loc
, itype
, TREE_OPERAND (arg0
, 0));
8149 tree ipart
= fold_convert_loc (loc
, itype
, TREE_OPERAND (arg0
, 1));
8150 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
,
8151 negate_expr (ipart
));
8153 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8154 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
8158 /* Convert ~ (-A) to A - 1. */
8159 if (INTEGRAL_TYPE_P (type
) && TREE_CODE (arg0
) == NEGATE_EXPR
)
8160 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
8161 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0)),
8162 build_int_cst (type
, 1));
8163 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8164 else if (INTEGRAL_TYPE_P (type
)
8165 && ((TREE_CODE (arg0
) == MINUS_EXPR
8166 && integer_onep (TREE_OPERAND (arg0
, 1)))
8167 || (TREE_CODE (arg0
) == PLUS_EXPR
8168 && integer_all_onesp (TREE_OPERAND (arg0
, 1)))))
8170 /* Perform the negation in ARG0's type and only then convert
8171 to TYPE as to avoid introducing undefined behavior. */
8172 tree t
= fold_build1_loc (loc
, NEGATE_EXPR
,
8173 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
8174 TREE_OPERAND (arg0
, 0));
8175 return fold_convert_loc (loc
, type
, t
);
8177 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8178 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8179 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8180 fold_convert_loc (loc
, type
,
8181 TREE_OPERAND (arg0
, 0)))))
8182 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
8183 fold_convert_loc (loc
, type
,
8184 TREE_OPERAND (arg0
, 1)));
8185 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
8186 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
8187 fold_convert_loc (loc
, type
,
8188 TREE_OPERAND (arg0
, 1)))))
8189 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
8190 fold_convert_loc (loc
, type
,
8191 TREE_OPERAND (arg0
, 0)), tem
);
8195 case TRUTH_NOT_EXPR
:
8196 /* Note that the operand of this must be an int
8197 and its values must be 0 or 1.
8198 ("true" is a fixed value perhaps depending on the language,
8199 but we don't handle values other than 1 correctly yet.) */
8200 tem
= fold_truth_not_expr (loc
, arg0
);
8203 return fold_convert_loc (loc
, type
, tem
);
8206 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8207 return fold_convert_loc (loc
, type
, arg0
);
8208 if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8210 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8211 tem
= fold_build2_loc (loc
, TREE_CODE (arg0
), itype
,
8212 fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8213 TREE_OPERAND (arg0
, 0)),
8214 fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8215 TREE_OPERAND (arg0
, 1)));
8216 return fold_convert_loc (loc
, type
, tem
);
8218 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8220 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8221 tem
= fold_build1_loc (loc
, REALPART_EXPR
, itype
,
8222 TREE_OPERAND (arg0
, 0));
8223 return fold_convert_loc (loc
, type
, tem
);
8225 if (TREE_CODE (arg0
) == CALL_EXPR
)
8227 tree fn
= get_callee_fndecl (arg0
);
8228 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8229 switch (DECL_FUNCTION_CODE (fn
))
8231 CASE_FLT_FN (BUILT_IN_CEXPI
):
8232 fn
= mathfn_built_in (type
, BUILT_IN_COS
);
8234 return build_call_expr_loc (loc
, fn
, 1, CALL_EXPR_ARG (arg0
, 0));
8244 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
8245 return build_zero_cst (type
);
8246 if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8248 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8249 tem
= fold_build2_loc (loc
, TREE_CODE (arg0
), itype
,
8250 fold_build1_loc (loc
, IMAGPART_EXPR
, itype
,
8251 TREE_OPERAND (arg0
, 0)),
8252 fold_build1_loc (loc
, IMAGPART_EXPR
, itype
,
8253 TREE_OPERAND (arg0
, 1)));
8254 return fold_convert_loc (loc
, type
, tem
);
8256 if (TREE_CODE (arg0
) == CONJ_EXPR
)
8258 tree itype
= TREE_TYPE (TREE_TYPE (arg0
));
8259 tem
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, TREE_OPERAND (arg0
, 0));
8260 return fold_convert_loc (loc
, type
, negate_expr (tem
));
8262 if (TREE_CODE (arg0
) == CALL_EXPR
)
8264 tree fn
= get_callee_fndecl (arg0
);
8265 if (fn
&& DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8266 switch (DECL_FUNCTION_CODE (fn
))
8268 CASE_FLT_FN (BUILT_IN_CEXPI
):
8269 fn
= mathfn_built_in (type
, BUILT_IN_SIN
);
8271 return build_call_expr_loc (loc
, fn
, 1, CALL_EXPR_ARG (arg0
, 0));
8281 /* Fold *&X to X if X is an lvalue. */
8282 if (TREE_CODE (op0
) == ADDR_EXPR
)
8284 tree op00
= TREE_OPERAND (op0
, 0);
8285 if ((TREE_CODE (op00
) == VAR_DECL
8286 || TREE_CODE (op00
) == PARM_DECL
8287 || TREE_CODE (op00
) == RESULT_DECL
)
8288 && !TREE_READONLY (op00
))
8295 } /* switch (code) */
8299 /* If the operation was a conversion do _not_ mark a resulting constant
8300 with TREE_OVERFLOW if the original constant was not. These conversions
8301 have implementation defined behavior and retaining the TREE_OVERFLOW
8302 flag here would confuse later passes such as VRP. */
8304 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
8305 tree type
, tree op0
)
8307 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
8309 && TREE_CODE (res
) == INTEGER_CST
8310 && TREE_CODE (op0
) == INTEGER_CST
8311 && CONVERT_EXPR_CODE_P (code
))
8312 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
8317 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8318 operands OP0 and OP1. LOC is the location of the resulting expression.
8319 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8320 Return the folded expression if folding is successful. Otherwise,
8321 return NULL_TREE. */
8323 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
8324 tree arg0
, tree arg1
, tree op0
, tree op1
)
8328 /* We only do these simplifications if we are optimizing. */
8332 /* Check for things like (A || B) && (A || C). We can convert this
8333 to A || (B && C). Note that either operator can be any of the four
8334 truth and/or operations and the transformation will still be
8335 valid. Also note that we only care about order for the
8336 ANDIF and ORIF operators. If B contains side effects, this
8337 might change the truth-value of A. */
8338 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8339 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
8340 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
8341 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
8342 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
8343 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
8345 tree a00
= TREE_OPERAND (arg0
, 0);
8346 tree a01
= TREE_OPERAND (arg0
, 1);
8347 tree a10
= TREE_OPERAND (arg1
, 0);
8348 tree a11
= TREE_OPERAND (arg1
, 1);
8349 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
8350 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
8351 && (code
== TRUTH_AND_EXPR
8352 || code
== TRUTH_OR_EXPR
));
8354 if (operand_equal_p (a00
, a10
, 0))
8355 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8356 fold_build2_loc (loc
, code
, type
, a01
, a11
));
8357 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
8358 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
8359 fold_build2_loc (loc
, code
, type
, a01
, a10
));
8360 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
8361 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
8362 fold_build2_loc (loc
, code
, type
, a00
, a11
));
8364 /* This case if tricky because we must either have commutative
8365 operators or else A10 must not have side-effects. */
8367 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
8368 && operand_equal_p (a01
, a11
, 0))
8369 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
8370 fold_build2_loc (loc
, code
, type
, a00
, a10
),
8374 /* See if we can build a range comparison. */
8375 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
8378 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
8379 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
8381 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
8383 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
8386 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
8387 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
8389 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
8391 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
8394 /* Check for the possibility of merging component references. If our
8395 lhs is another similar operation, try to merge its rhs with our
8396 rhs. Then try to merge our lhs and rhs. */
8397 if (TREE_CODE (arg0
) == code
8398 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
8399 TREE_OPERAND (arg0
, 1), arg1
)))
8400 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
8402 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8405 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8406 && (code
== TRUTH_AND_EXPR
8407 || code
== TRUTH_ANDIF_EXPR
8408 || code
== TRUTH_OR_EXPR
8409 || code
== TRUTH_ORIF_EXPR
))
8411 enum tree_code ncode
, icode
;
8413 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8414 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8415 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8417 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8418 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8419 We don't want to pack more than two leafs to a non-IF AND/OR
8421 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8422 equal to IF-CODE, then we don't want to add right-hand operand.
8423 If the inner right-hand side of left-hand operand has
8424 side-effects, or isn't simple, then we can't add to it,
8425 as otherwise we might destroy if-sequence. */
8426 if (TREE_CODE (arg0
) == icode
8427 && simple_operand_p_2 (arg1
)
8428 /* Needed for sequence points to handle trappings, and
8430 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8432 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8434 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8437 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8438 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8439 else if (TREE_CODE (arg1
) == icode
8440 && simple_operand_p_2 (arg0
)
8441 /* Needed for sequence points to handle trappings, and
8443 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8445 tem
= fold_build2_loc (loc
, ncode
, type
,
8446 arg0
, TREE_OPERAND (arg1
, 0));
8447 return fold_build2_loc (loc
, icode
, type
, tem
,
8448 TREE_OPERAND (arg1
, 1));
8450 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8452 For sequence point consistancy, we need to check for trapping,
8453 and side-effects. */
8454 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8455 && simple_operand_p_2 (arg1
))
8456 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8462 /* Fold a binary expression of code CODE and type TYPE with operands
8463 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8464 Return the folded expression if folding is successful. Otherwise,
8465 return NULL_TREE. */
8468 fold_minmax (location_t loc
, enum tree_code code
, tree type
, tree op0
, tree op1
)
8470 enum tree_code compl_code
;
8472 if (code
== MIN_EXPR
)
8473 compl_code
= MAX_EXPR
;
8474 else if (code
== MAX_EXPR
)
8475 compl_code
= MIN_EXPR
;
8479 /* MIN (MAX (a, b), b) == b. */
8480 if (TREE_CODE (op0
) == compl_code
8481 && operand_equal_p (TREE_OPERAND (op0
, 1), op1
, 0))
8482 return omit_one_operand_loc (loc
, type
, op1
, TREE_OPERAND (op0
, 0));
8484 /* MIN (MAX (b, a), b) == b. */
8485 if (TREE_CODE (op0
) == compl_code
8486 && operand_equal_p (TREE_OPERAND (op0
, 0), op1
, 0)
8487 && reorder_operands_p (TREE_OPERAND (op0
, 1), op1
))
8488 return omit_one_operand_loc (loc
, type
, op1
, TREE_OPERAND (op0
, 1));
8490 /* MIN (a, MAX (a, b)) == a. */
8491 if (TREE_CODE (op1
) == compl_code
8492 && operand_equal_p (op0
, TREE_OPERAND (op1
, 0), 0)
8493 && reorder_operands_p (op0
, TREE_OPERAND (op1
, 1)))
8494 return omit_one_operand_loc (loc
, type
, op0
, TREE_OPERAND (op1
, 1));
8496 /* MIN (a, MAX (b, a)) == a. */
8497 if (TREE_CODE (op1
) == compl_code
8498 && operand_equal_p (op0
, TREE_OPERAND (op1
, 1), 0)
8499 && reorder_operands_p (op0
, TREE_OPERAND (op1
, 0)))
8500 return omit_one_operand_loc (loc
, type
, op0
, TREE_OPERAND (op1
, 0));
8505 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8506 by changing CODE to reduce the magnitude of constants involved in
8507 ARG0 of the comparison.
8508 Returns a canonicalized comparison tree if a simplification was
8509 possible, otherwise returns NULL_TREE.
8510 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8511 valid if signed overflow is undefined. */
8514 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8515 tree arg0
, tree arg1
,
8516 bool *strict_overflow_p
)
8518 enum tree_code code0
= TREE_CODE (arg0
);
8519 tree t
, cst0
= NULL_TREE
;
8523 /* Match A +- CST code arg1 and CST code arg1. We can change the
8524 first form only if overflow is undefined. */
8525 if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8526 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8527 /* In principle pointers also have undefined overflow behavior,
8528 but that causes problems elsewhere. */
8529 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8530 && (code0
== MINUS_EXPR
8531 || code0
== PLUS_EXPR
)
8532 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
8533 || code0
== INTEGER_CST
))
8536 /* Identify the constant in arg0 and its sign. */
8537 if (code0
== INTEGER_CST
)
8540 cst0
= TREE_OPERAND (arg0
, 1);
8541 sgn0
= tree_int_cst_sgn (cst0
);
8543 /* Overflowed constants and zero will cause problems. */
8544 if (integer_zerop (cst0
)
8545 || TREE_OVERFLOW (cst0
))
8548 /* See if we can reduce the magnitude of the constant in
8549 arg0 by changing the comparison code. */
8550 if (code0
== INTEGER_CST
)
8552 /* CST <= arg1 -> CST-1 < arg1. */
8553 if (code
== LE_EXPR
&& sgn0
== 1)
8555 /* -CST < arg1 -> -CST-1 <= arg1. */
8556 else if (code
== LT_EXPR
&& sgn0
== -1)
8558 /* CST > arg1 -> CST-1 >= arg1. */
8559 else if (code
== GT_EXPR
&& sgn0
== 1)
8561 /* -CST >= arg1 -> -CST-1 > arg1. */
8562 else if (code
== GE_EXPR
&& sgn0
== -1)
8566 /* arg1 code' CST' might be more canonical. */
8571 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8573 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8575 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8576 else if (code
== GT_EXPR
8577 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8579 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8580 else if (code
== LE_EXPR
8581 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8583 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8584 else if (code
== GE_EXPR
8585 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8589 *strict_overflow_p
= true;
8592 /* Now build the constant reduced in magnitude. But not if that
8593 would produce one outside of its types range. */
8594 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8596 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8597 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8599 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8600 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8601 /* We cannot swap the comparison here as that would cause us to
8602 endlessly recurse. */
8605 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8606 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8607 if (code0
!= INTEGER_CST
)
8608 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8609 t
= fold_convert (TREE_TYPE (arg1
), t
);
8611 /* If swapping might yield to a more canonical form, do so. */
8613 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, arg1
, t
);
8615 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8618 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8619 overflow further. Try to decrease the magnitude of constants involved
8620 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8621 and put sole constants at the second argument position.
8622 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8625 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8626 tree arg0
, tree arg1
)
8629 bool strict_overflow_p
;
8630 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8631 "when reducing constant in comparison");
8633 /* Try canonicalization by simplifying arg0. */
8634 strict_overflow_p
= false;
8635 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8636 &strict_overflow_p
);
8639 if (strict_overflow_p
)
8640 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8644 /* Try canonicalization by simplifying arg1 using the swapped
8646 code
= swap_tree_comparison (code
);
8647 strict_overflow_p
= false;
8648 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8649 &strict_overflow_p
);
8650 if (t
&& strict_overflow_p
)
8651 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8655 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8656 space. This is used to avoid issuing overflow warnings for
8657 expressions like &p->x which can not wrap. */
8660 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8662 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8669 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8670 if (offset
== NULL_TREE
)
8671 wi_offset
= wi::zero (precision
);
8672 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8678 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8679 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8683 if (!wi::fits_uhwi_p (total
))
8686 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8690 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8692 if (TREE_CODE (base
) == ADDR_EXPR
)
8694 HOST_WIDE_INT base_size
;
8696 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8697 if (base_size
> 0 && size
< base_size
)
8701 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8704 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8705 kind INTEGER_CST. This makes sure to properly sign-extend the
8708 static HOST_WIDE_INT
8709 size_low_cst (const_tree t
)
8711 HOST_WIDE_INT w
= TREE_INT_CST_ELT (t
, 0);
8712 int prec
= TYPE_PRECISION (TREE_TYPE (t
));
8713 if (prec
< HOST_BITS_PER_WIDE_INT
)
8714 return sext_hwi (w
, prec
);
8718 /* Subroutine of fold_binary. This routine performs all of the
8719 transformations that are common to the equality/inequality
8720 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8721 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8722 fold_binary should call fold_binary. Fold a comparison with
8723 tree code CODE and type TYPE with operands OP0 and OP1. Return
8724 the folded comparison or NULL_TREE. */
8727 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8730 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8731 tree arg0
, arg1
, tem
;
8736 STRIP_SIGN_NOPS (arg0
);
8737 STRIP_SIGN_NOPS (arg1
);
8739 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8740 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8742 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8743 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
8744 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8745 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8746 && TREE_CODE (arg1
) == INTEGER_CST
8747 && !TREE_OVERFLOW (arg1
))
8749 const enum tree_code
8750 reverse_op
= TREE_CODE (arg0
) == PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
;
8751 tree const1
= TREE_OPERAND (arg0
, 1);
8752 tree const2
= fold_convert_loc (loc
, TREE_TYPE (const1
), arg1
);
8753 tree variable
= TREE_OPERAND (arg0
, 0);
8754 tree new_const
= int_const_binop (reverse_op
, const2
, const1
);
8756 /* If the constant operation overflowed this can be
8757 simplified as a comparison against INT_MAX/INT_MIN. */
8758 if (TREE_OVERFLOW (new_const
)
8759 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
8761 int const1_sgn
= tree_int_cst_sgn (const1
);
8762 enum tree_code code2
= code
;
8764 /* Get the sign of the constant on the lhs if the
8765 operation were VARIABLE + CONST1. */
8766 if (TREE_CODE (arg0
) == MINUS_EXPR
)
8767 const1_sgn
= -const1_sgn
;
8769 /* The sign of the constant determines if we overflowed
8770 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8771 Canonicalize to the INT_MIN overflow by swapping the comparison
8773 if (const1_sgn
== -1)
8774 code2
= swap_tree_comparison (code
);
8776 /* We now can look at the canonicalized case
8777 VARIABLE + 1 CODE2 INT_MIN
8778 and decide on the result. */
8785 omit_one_operand_loc (loc
, type
, boolean_false_node
, variable
);
8791 omit_one_operand_loc (loc
, type
, boolean_true_node
, variable
);
8800 fold_overflow_warning ("assuming signed overflow does not occur "
8801 "when changing X +- C1 cmp C2 to "
8803 WARN_STRICT_OVERFLOW_COMPARISON
);
8804 return fold_build2_loc (loc
, code
, type
, variable
, new_const
);
8808 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. */
8809 if (TREE_CODE (arg0
) == MINUS_EXPR
8811 && integer_zerop (arg1
))
8813 /* ??? The transformation is valid for the other operators if overflow
8814 is undefined for the type, but performing it here badly interacts
8815 with the transformation in fold_cond_expr_with_comparison which
8816 attempts to synthetize ABS_EXPR. */
8818 fold_overflow_warning ("assuming signed overflow does not occur "
8819 "when changing X - Y cmp 0 to X cmp Y",
8820 WARN_STRICT_OVERFLOW_COMPARISON
);
8821 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
8822 TREE_OPERAND (arg0
, 1));
8825 /* For comparisons of pointers we can decompose it to a compile time
8826 comparison of the base objects and the offsets into the object.
8827 This requires at least one operand being an ADDR_EXPR or a
8828 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8829 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8830 && (TREE_CODE (arg0
) == ADDR_EXPR
8831 || TREE_CODE (arg1
) == ADDR_EXPR
8832 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8833 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8835 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8836 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8838 int volatilep
, unsignedp
;
8839 bool indirect_base0
= false, indirect_base1
= false;
8841 /* Get base and offset for the access. Strip ADDR_EXPR for
8842 get_inner_reference, but put it back by stripping INDIRECT_REF
8843 off the base object if possible. indirect_baseN will be true
8844 if baseN is not an address but refers to the object itself. */
8846 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8848 base0
= get_inner_reference (TREE_OPERAND (arg0
, 0),
8849 &bitsize
, &bitpos0
, &offset0
, &mode
,
8850 &unsignedp
, &volatilep
, false);
8851 if (TREE_CODE (base0
) == INDIRECT_REF
)
8852 base0
= TREE_OPERAND (base0
, 0);
8854 indirect_base0
= true;
8856 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8858 base0
= TREE_OPERAND (arg0
, 0);
8859 STRIP_SIGN_NOPS (base0
);
8860 if (TREE_CODE (base0
) == ADDR_EXPR
)
8862 base0
= TREE_OPERAND (base0
, 0);
8863 indirect_base0
= true;
8865 offset0
= TREE_OPERAND (arg0
, 1);
8866 if (tree_fits_shwi_p (offset0
))
8868 HOST_WIDE_INT off
= size_low_cst (offset0
);
8869 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
8871 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
8873 bitpos0
= off
* BITS_PER_UNIT
;
8874 offset0
= NULL_TREE
;
8880 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8882 base1
= get_inner_reference (TREE_OPERAND (arg1
, 0),
8883 &bitsize
, &bitpos1
, &offset1
, &mode
,
8884 &unsignedp
, &volatilep
, false);
8885 if (TREE_CODE (base1
) == INDIRECT_REF
)
8886 base1
= TREE_OPERAND (base1
, 0);
8888 indirect_base1
= true;
8890 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8892 base1
= TREE_OPERAND (arg1
, 0);
8893 STRIP_SIGN_NOPS (base1
);
8894 if (TREE_CODE (base1
) == ADDR_EXPR
)
8896 base1
= TREE_OPERAND (base1
, 0);
8897 indirect_base1
= true;
8899 offset1
= TREE_OPERAND (arg1
, 1);
8900 if (tree_fits_shwi_p (offset1
))
8902 HOST_WIDE_INT off
= size_low_cst (offset1
);
8903 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
8905 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
8907 bitpos1
= off
* BITS_PER_UNIT
;
8908 offset1
= NULL_TREE
;
8913 /* A local variable can never be pointed to by
8914 the default SSA name of an incoming parameter. */
8915 if ((TREE_CODE (arg0
) == ADDR_EXPR
8917 && TREE_CODE (base0
) == VAR_DECL
8918 && auto_var_in_fn_p (base0
, current_function_decl
)
8920 && TREE_CODE (base1
) == SSA_NAME
8921 && SSA_NAME_IS_DEFAULT_DEF (base1
)
8922 && TREE_CODE (SSA_NAME_VAR (base1
)) == PARM_DECL
)
8923 || (TREE_CODE (arg1
) == ADDR_EXPR
8925 && TREE_CODE (base1
) == VAR_DECL
8926 && auto_var_in_fn_p (base1
, current_function_decl
)
8928 && TREE_CODE (base0
) == SSA_NAME
8929 && SSA_NAME_IS_DEFAULT_DEF (base0
)
8930 && TREE_CODE (SSA_NAME_VAR (base0
)) == PARM_DECL
))
8932 if (code
== NE_EXPR
)
8933 return constant_boolean_node (1, type
);
8934 else if (code
== EQ_EXPR
)
8935 return constant_boolean_node (0, type
);
8937 /* If we have equivalent bases we might be able to simplify. */
8938 else if (indirect_base0
== indirect_base1
8939 && operand_equal_p (base0
, base1
, 0))
8941 /* We can fold this expression to a constant if the non-constant
8942 offset parts are equal. */
8943 if ((offset0
== offset1
8944 || (offset0
&& offset1
8945 && operand_equal_p (offset0
, offset1
, 0)))
8948 || (indirect_base0
&& DECL_P (base0
))
8949 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8953 && bitpos0
!= bitpos1
8954 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8955 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8956 fold_overflow_warning (("assuming pointer wraparound does not "
8957 "occur when comparing P +- C1 with "
8959 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8964 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8966 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8968 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8970 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8972 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8974 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8978 /* We can simplify the comparison to a comparison of the variable
8979 offset parts if the constant offset parts are equal.
8980 Be careful to use signed sizetype here because otherwise we
8981 mess with array offsets in the wrong way. This is possible
8982 because pointer arithmetic is restricted to retain within an
8983 object and overflow on pointer differences is undefined as of
8984 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8985 else if (bitpos0
== bitpos1
8987 || (indirect_base0
&& DECL_P (base0
))
8988 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8990 /* By converting to signed sizetype we cover middle-end pointer
8991 arithmetic which operates on unsigned pointer types of size
8992 type size and ARRAY_REF offsets which are properly sign or
8993 zero extended from their type in case it is narrower than
8995 if (offset0
== NULL_TREE
)
8996 offset0
= build_int_cst (ssizetype
, 0);
8998 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8999 if (offset1
== NULL_TREE
)
9000 offset1
= build_int_cst (ssizetype
, 0);
9002 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
9005 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
9006 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
9007 fold_overflow_warning (("assuming pointer wraparound does not "
9008 "occur when comparing P +- C1 with "
9010 WARN_STRICT_OVERFLOW_COMPARISON
);
9012 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
9015 /* For non-equal bases we can simplify if they are addresses
9016 declarations with different addresses. */
9017 else if (indirect_base0
&& indirect_base1
9018 /* We know that !operand_equal_p (base0, base1, 0)
9019 because the if condition was false. But make
9020 sure two decls are not the same. */
9022 && TREE_CODE (arg0
) == ADDR_EXPR
9023 && TREE_CODE (arg1
) == ADDR_EXPR
9026 /* Watch for aliases. */
9027 && (!decl_in_symtab_p (base0
)
9028 || !decl_in_symtab_p (base1
)
9029 || !symtab_node::get_create (base0
)->equal_address_to
9030 (symtab_node::get_create (base1
))))
9032 if (code
== EQ_EXPR
)
9033 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
9035 else if (code
== NE_EXPR
)
9036 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
9039 /* For equal offsets we can simplify to a comparison of the
9041 else if (bitpos0
== bitpos1
9043 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
9045 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
9046 && ((offset0
== offset1
)
9047 || (offset0
&& offset1
9048 && operand_equal_p (offset0
, offset1
, 0))))
9051 base0
= build_fold_addr_expr_loc (loc
, base0
);
9053 base1
= build_fold_addr_expr_loc (loc
, base1
);
9054 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
9058 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9059 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9060 the resulting offset is smaller in absolute value than the
9061 original one and has the same sign. */
9062 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9063 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
9064 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
9065 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9066 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
9067 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
9068 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
9069 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
9071 tree const1
= TREE_OPERAND (arg0
, 1);
9072 tree const2
= TREE_OPERAND (arg1
, 1);
9073 tree variable1
= TREE_OPERAND (arg0
, 0);
9074 tree variable2
= TREE_OPERAND (arg1
, 0);
9076 const char * const warnmsg
= G_("assuming signed overflow does not "
9077 "occur when combining constants around "
9080 /* Put the constant on the side where it doesn't overflow and is
9081 of lower absolute value and of same sign than before. */
9082 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9083 ? MINUS_EXPR
: PLUS_EXPR
,
9085 if (!TREE_OVERFLOW (cst
)
9086 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
9087 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
9089 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
9090 return fold_build2_loc (loc
, code
, type
,
9092 fold_build2_loc (loc
, TREE_CODE (arg1
),
9097 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
9098 ? MINUS_EXPR
: PLUS_EXPR
,
9100 if (!TREE_OVERFLOW (cst
)
9101 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
9102 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
9104 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
9105 return fold_build2_loc (loc
, code
, type
,
9106 fold_build2_loc (loc
, TREE_CODE (arg0
),
9113 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9114 signed arithmetic case. That form is created by the compiler
9115 often enough for folding it to be of value. One example is in
9116 computing loop trip counts after Operator Strength Reduction. */
9117 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9118 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
9119 && TREE_CODE (arg0
) == MULT_EXPR
9120 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9121 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
9122 && integer_zerop (arg1
))
9124 tree const1
= TREE_OPERAND (arg0
, 1);
9125 tree const2
= arg1
; /* zero */
9126 tree variable1
= TREE_OPERAND (arg0
, 0);
9127 enum tree_code cmp_code
= code
;
9129 /* Handle unfolded multiplication by zero. */
9130 if (integer_zerop (const1
))
9131 return fold_build2_loc (loc
, cmp_code
, type
, const1
, const2
);
9133 fold_overflow_warning (("assuming signed overflow does not occur when "
9134 "eliminating multiplication in comparison "
9136 WARN_STRICT_OVERFLOW_COMPARISON
);
9138 /* If const1 is negative we swap the sense of the comparison. */
9139 if (tree_int_cst_sgn (const1
) < 0)
9140 cmp_code
= swap_tree_comparison (cmp_code
);
9142 return fold_build2_loc (loc
, cmp_code
, type
, variable1
, const2
);
9145 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
9149 if (FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9151 tree targ0
= strip_float_extensions (arg0
);
9152 tree targ1
= strip_float_extensions (arg1
);
9153 tree newtype
= TREE_TYPE (targ0
);
9155 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
9156 newtype
= TREE_TYPE (targ1
);
9158 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9159 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
9160 return fold_build2_loc (loc
, code
, type
,
9161 fold_convert_loc (loc
, newtype
, targ0
),
9162 fold_convert_loc (loc
, newtype
, targ1
));
9164 if (TREE_CODE (arg1
) == REAL_CST
)
9166 REAL_VALUE_TYPE cst
;
9167 cst
= TREE_REAL_CST (arg1
);
9169 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9170 /* a CMP (-0) -> a CMP 0 */
9171 if (REAL_VALUE_MINUS_ZERO (cst
))
9172 return fold_build2_loc (loc
, code
, type
, arg0
,
9173 build_real (TREE_TYPE (arg1
), dconst0
));
9175 /* x != NaN is always true, other ops are always false. */
9176 if (REAL_VALUE_ISNAN (cst
)
9177 && ! HONOR_SNANS (arg1
))
9179 tem
= (code
== NE_EXPR
) ? integer_one_node
: integer_zero_node
;
9180 return omit_one_operand_loc (loc
, type
, tem
, arg0
);
9183 /* Fold comparisons against infinity. */
9184 if (REAL_VALUE_ISINF (cst
)
9185 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
))))
9187 tem
= fold_inf_compare (loc
, code
, type
, arg0
, arg1
);
9188 if (tem
!= NULL_TREE
)
9193 /* If this is a comparison of a real constant with a PLUS_EXPR
9194 or a MINUS_EXPR of a real constant, we can convert it into a
9195 comparison with a revised real constant as long as no overflow
9196 occurs when unsafe_math_optimizations are enabled. */
9197 if (flag_unsafe_math_optimizations
9198 && TREE_CODE (arg1
) == REAL_CST
9199 && (TREE_CODE (arg0
) == PLUS_EXPR
9200 || TREE_CODE (arg0
) == MINUS_EXPR
)
9201 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
9202 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
9203 ? MINUS_EXPR
: PLUS_EXPR
,
9204 arg1
, TREE_OPERAND (arg0
, 1)))
9205 && !TREE_OVERFLOW (tem
))
9206 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
9208 /* Likewise, we can simplify a comparison of a real constant with
9209 a MINUS_EXPR whose first operand is also a real constant, i.e.
9210 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9211 floating-point types only if -fassociative-math is set. */
9212 if (flag_associative_math
9213 && TREE_CODE (arg1
) == REAL_CST
9214 && TREE_CODE (arg0
) == MINUS_EXPR
9215 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
9216 && 0 != (tem
= const_binop (MINUS_EXPR
, TREE_OPERAND (arg0
, 0),
9218 && !TREE_OVERFLOW (tem
))
9219 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
,
9220 TREE_OPERAND (arg0
, 1), tem
);
9222 /* Fold comparisons against built-in math functions. */
9223 if (TREE_CODE (arg1
) == REAL_CST
9224 && flag_unsafe_math_optimizations
9225 && ! flag_errno_math
)
9227 enum built_in_function fcode
= builtin_mathfn_code (arg0
);
9229 if (fcode
!= END_BUILTINS
)
9231 tem
= fold_mathfn_compare (loc
, fcode
, code
, type
, arg0
, arg1
);
9232 if (tem
!= NULL_TREE
)
9238 if (TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
9239 && CONVERT_EXPR_P (arg0
))
9241 /* If we are widening one operand of an integer comparison,
9242 see if the other operand is similarly being widened. Perhaps we
9243 can do the comparison in the narrower type. */
9244 tem
= fold_widened_comparison (loc
, code
, type
, arg0
, arg1
);
9248 /* Or if we are changing signedness. */
9249 tem
= fold_sign_changed_comparison (loc
, code
, type
, arg0
, arg1
);
9254 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9255 constant, we can simplify it. */
9256 if (TREE_CODE (arg1
) == INTEGER_CST
9257 && (TREE_CODE (arg0
) == MIN_EXPR
9258 || TREE_CODE (arg0
) == MAX_EXPR
)
9259 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9261 tem
= optimize_minmax_comparison (loc
, code
, type
, op0
, op1
);
9266 /* Simplify comparison of something with itself. (For IEEE
9267 floating-point, we can only do some of these simplifications.) */
9268 if (operand_equal_p (arg0
, arg1
, 0))
9273 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
9274 || ! HONOR_NANS (arg0
))
9275 return constant_boolean_node (1, type
);
9280 if (! FLOAT_TYPE_P (TREE_TYPE (arg0
))
9281 || ! HONOR_NANS (arg0
))
9282 return constant_boolean_node (1, type
);
9283 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
, arg1
);
9286 /* For NE, we can only do this simplification if integer
9287 or we don't honor IEEE floating point NaNs. */
9288 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
9289 && HONOR_NANS (arg0
))
9291 /* ... fall through ... */
9294 return constant_boolean_node (0, type
);
9300 /* If we are comparing an expression that just has comparisons
9301 of two integer values, arithmetic expressions of those comparisons,
9302 and constants, we can simplify it. There are only three cases
9303 to check: the two values can either be equal, the first can be
9304 greater, or the second can be greater. Fold the expression for
9305 those three values. Since each value must be 0 or 1, we have
9306 eight possibilities, each of which corresponds to the constant 0
9307 or 1 or one of the six possible comparisons.
9309 This handles common cases like (a > b) == 0 but also handles
9310 expressions like ((x > y) - (y > x)) > 0, which supposedly
9311 occur in macroized code. */
9313 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
9315 tree cval1
= 0, cval2
= 0;
9318 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
9319 /* Don't handle degenerate cases here; they should already
9320 have been handled anyway. */
9321 && cval1
!= 0 && cval2
!= 0
9322 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
9323 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
9324 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
9325 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
9326 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
9327 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
9328 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
9330 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
9331 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
9333 /* We can't just pass T to eval_subst in case cval1 or cval2
9334 was the same as ARG1. */
9337 = fold_build2_loc (loc
, code
, type
,
9338 eval_subst (loc
, arg0
, cval1
, maxval
,
9342 = fold_build2_loc (loc
, code
, type
,
9343 eval_subst (loc
, arg0
, cval1
, maxval
,
9347 = fold_build2_loc (loc
, code
, type
,
9348 eval_subst (loc
, arg0
, cval1
, minval
,
9352 /* All three of these results should be 0 or 1. Confirm they are.
9353 Then use those values to select the proper code to use. */
9355 if (TREE_CODE (high_result
) == INTEGER_CST
9356 && TREE_CODE (equal_result
) == INTEGER_CST
9357 && TREE_CODE (low_result
) == INTEGER_CST
)
9359 /* Make a 3-bit mask with the high-order bit being the
9360 value for `>', the next for '=', and the low for '<'. */
9361 switch ((integer_onep (high_result
) * 4)
9362 + (integer_onep (equal_result
) * 2)
9363 + integer_onep (low_result
))
9367 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
9388 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
9393 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
9394 SET_EXPR_LOCATION (tem
, loc
);
9397 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
9402 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9403 into a single range test. */
9404 if ((TREE_CODE (arg0
) == TRUNC_DIV_EXPR
9405 || TREE_CODE (arg0
) == EXACT_DIV_EXPR
)
9406 && TREE_CODE (arg1
) == INTEGER_CST
9407 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
9408 && !integer_zerop (TREE_OPERAND (arg0
, 1))
9409 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
9410 && !TREE_OVERFLOW (arg1
))
9412 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
9413 if (tem
!= NULL_TREE
)
9417 /* Fold ~X op ~Y as Y op X. */
9418 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9419 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
9421 tree cmp_type
= TREE_TYPE (TREE_OPERAND (arg0
, 0));
9422 return fold_build2_loc (loc
, code
, type
,
9423 fold_convert_loc (loc
, cmp_type
,
9424 TREE_OPERAND (arg1
, 0)),
9425 TREE_OPERAND (arg0
, 0));
9428 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9429 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9430 && (TREE_CODE (arg1
) == INTEGER_CST
|| TREE_CODE (arg1
) == VECTOR_CST
))
9432 tree cmp_type
= TREE_TYPE (TREE_OPERAND (arg0
, 0));
9433 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
,
9434 TREE_OPERAND (arg0
, 0),
9435 fold_build1_loc (loc
, BIT_NOT_EXPR
, cmp_type
,
9436 fold_convert_loc (loc
, cmp_type
, arg1
)));
9443 /* Subroutine of fold_binary. Optimize complex multiplications of the
9444 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9445 argument EXPR represents the expression "z" of type TYPE. */
9448 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
9450 tree itype
= TREE_TYPE (type
);
9451 tree rpart
, ipart
, tem
;
9453 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
9455 rpart
= TREE_OPERAND (expr
, 0);
9456 ipart
= TREE_OPERAND (expr
, 1);
9458 else if (TREE_CODE (expr
) == COMPLEX_CST
)
9460 rpart
= TREE_REALPART (expr
);
9461 ipart
= TREE_IMAGPART (expr
);
9465 expr
= save_expr (expr
);
9466 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
9467 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
9470 rpart
= save_expr (rpart
);
9471 ipart
= save_expr (ipart
);
9472 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
9473 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
9474 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
9475 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
9476 build_zero_cst (itype
));
9480 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9481 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9482 guarantees that P and N have the same least significant log2(M) bits.
9483 N is not otherwise constrained. In particular, N is not normalized to
9484 0 <= N < M as is common. In general, the precise value of P is unknown.
9485 M is chosen as large as possible such that constant N can be determined.
9487 Returns M and sets *RESIDUE to N.
9489 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9490 account. This is not always possible due to PR 35705.
9493 static unsigned HOST_WIDE_INT
9494 get_pointer_modulus_and_residue (tree expr
, unsigned HOST_WIDE_INT
*residue
,
9495 bool allow_func_align
)
9497 enum tree_code code
;
9501 code
= TREE_CODE (expr
);
9502 if (code
== ADDR_EXPR
)
9504 unsigned int bitalign
;
9505 get_object_alignment_1 (TREE_OPERAND (expr
, 0), &bitalign
, residue
);
9506 *residue
/= BITS_PER_UNIT
;
9507 return bitalign
/ BITS_PER_UNIT
;
9509 else if (code
== POINTER_PLUS_EXPR
)
9512 unsigned HOST_WIDE_INT modulus
;
9513 enum tree_code inner_code
;
9515 op0
= TREE_OPERAND (expr
, 0);
9517 modulus
= get_pointer_modulus_and_residue (op0
, residue
,
9520 op1
= TREE_OPERAND (expr
, 1);
9522 inner_code
= TREE_CODE (op1
);
9523 if (inner_code
== INTEGER_CST
)
9525 *residue
+= TREE_INT_CST_LOW (op1
);
9528 else if (inner_code
== MULT_EXPR
)
9530 op1
= TREE_OPERAND (op1
, 1);
9531 if (TREE_CODE (op1
) == INTEGER_CST
)
9533 unsigned HOST_WIDE_INT align
;
9535 /* Compute the greatest power-of-2 divisor of op1. */
9536 align
= TREE_INT_CST_LOW (op1
);
9539 /* If align is non-zero and less than *modulus, replace
9540 *modulus with align., If align is 0, then either op1 is 0
9541 or the greatest power-of-2 divisor of op1 doesn't fit in an
9542 unsigned HOST_WIDE_INT. In either case, no additional
9543 constraint is imposed. */
9545 modulus
= MIN (modulus
, align
);
9552 /* If we get here, we were unable to determine anything useful about the
9557 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9558 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9561 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
9563 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
9565 if (TREE_CODE (arg
) == VECTOR_CST
)
9567 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
9568 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
9570 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
9572 constructor_elt
*elt
;
9574 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
9575 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
9578 elts
[i
] = elt
->value
;
9582 for (; i
< nelts
; i
++)
9584 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
9588 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9589 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9590 NULL_TREE otherwise. */
9593 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
9595 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
9597 bool need_ctor
= false;
9599 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
9600 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
9601 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
9602 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
9605 elts
= XALLOCAVEC (tree
, nelts
* 3);
9606 if (!vec_cst_ctor_to_array (arg0
, elts
)
9607 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
9610 for (i
= 0; i
< nelts
; i
++)
9612 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
9614 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
9619 vec
<constructor_elt
, va_gc
> *v
;
9620 vec_alloc (v
, nelts
);
9621 for (i
= 0; i
< nelts
; i
++)
9622 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
9623 return build_constructor (type
, v
);
9626 return build_vector (type
, &elts
[2 * nelts
]);
9629 /* Try to fold a pointer difference of type TYPE two address expressions of
9630 array references AREF0 and AREF1 using location LOC. Return a
9631 simplified expression for the difference or NULL_TREE. */
9634 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
9635 tree aref0
, tree aref1
)
9637 tree base0
= TREE_OPERAND (aref0
, 0);
9638 tree base1
= TREE_OPERAND (aref1
, 0);
9639 tree base_offset
= build_int_cst (type
, 0);
9641 /* If the bases are array references as well, recurse. If the bases
9642 are pointer indirections compute the difference of the pointers.
9643 If the bases are equal, we are set. */
9644 if ((TREE_CODE (base0
) == ARRAY_REF
9645 && TREE_CODE (base1
) == ARRAY_REF
9647 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
9648 || (INDIRECT_REF_P (base0
)
9649 && INDIRECT_REF_P (base1
)
9650 && (base_offset
= fold_binary_loc (loc
, MINUS_EXPR
, type
,
9651 TREE_OPERAND (base0
, 0),
9652 TREE_OPERAND (base1
, 0))))
9653 || operand_equal_p (base0
, base1
, 0))
9655 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
9656 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
9657 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
9658 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
9659 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9661 fold_build2_loc (loc
, MULT_EXPR
, type
,
9667 /* If the real or vector real constant CST of type TYPE has an exact
9668 inverse, return it, else return NULL. */
9671 exact_inverse (tree type
, tree cst
)
9674 tree unit_type
, *elts
;
9676 unsigned vec_nelts
, i
;
9678 switch (TREE_CODE (cst
))
9681 r
= TREE_REAL_CST (cst
);
9683 if (exact_real_inverse (TYPE_MODE (type
), &r
))
9684 return build_real (type
, r
);
9689 vec_nelts
= VECTOR_CST_NELTS (cst
);
9690 elts
= XALLOCAVEC (tree
, vec_nelts
);
9691 unit_type
= TREE_TYPE (type
);
9692 mode
= TYPE_MODE (unit_type
);
9694 for (i
= 0; i
< vec_nelts
; i
++)
9696 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
9697 if (!exact_real_inverse (mode
, &r
))
9699 elts
[i
] = build_real (unit_type
, r
);
9702 return build_vector (type
, elts
);
9709 /* Mask out the tz least significant bits of X of type TYPE where
9710 tz is the number of trailing zeroes in Y. */
9712 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
9714 int tz
= wi::ctz (y
);
9716 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
9720 /* Return true when T is an address and is known to be nonzero.
9721 For floating point we further ensure that T is not denormal.
9722 Similar logic is present in nonzero_address in rtlanal.h.
9724 If the return value is based on the assumption that signed overflow
9725 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9726 change *STRICT_OVERFLOW_P. */
9729 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
9731 tree type
= TREE_TYPE (t
);
9732 enum tree_code code
;
9734 /* Doing something useful for floating point would need more work. */
9735 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
9738 code
= TREE_CODE (t
);
9739 switch (TREE_CODE_CLASS (code
))
9742 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9745 case tcc_comparison
:
9746 return tree_binary_nonzero_warnv_p (code
, type
,
9747 TREE_OPERAND (t
, 0),
9748 TREE_OPERAND (t
, 1),
9751 case tcc_declaration
:
9753 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9761 case TRUTH_NOT_EXPR
:
9762 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
9765 case TRUTH_AND_EXPR
:
9767 case TRUTH_XOR_EXPR
:
9768 return tree_binary_nonzero_warnv_p (code
, type
,
9769 TREE_OPERAND (t
, 0),
9770 TREE_OPERAND (t
, 1),
9778 case WITH_SIZE_EXPR
:
9780 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
9785 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
9789 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
9794 tree fndecl
= get_callee_fndecl (t
);
9795 if (!fndecl
) return false;
9796 if (flag_delete_null_pointer_checks
&& !flag_check_new
9797 && DECL_IS_OPERATOR_NEW (fndecl
)
9798 && !TREE_NOTHROW (fndecl
))
9800 if (flag_delete_null_pointer_checks
9801 && lookup_attribute ("returns_nonnull",
9802 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
9804 return alloca_call_p (t
);
9813 /* Return true when T is an address and is known to be nonzero.
9814 Handle warnings about undefined signed overflow. */
9817 tree_expr_nonzero_p (tree t
)
9819 bool ret
, strict_overflow_p
;
9821 strict_overflow_p
= false;
9822 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9823 if (strict_overflow_p
)
9824 fold_overflow_warning (("assuming signed overflow does not occur when "
9825 "determining that expression is always "
9827 WARN_STRICT_OVERFLOW_MISC
);
9831 /* Fold a binary expression of code CODE and type TYPE with operands
9832 OP0 and OP1. LOC is the location of the resulting expression.
9833 Return the folded expression if folding is successful. Otherwise,
9834 return NULL_TREE. */
9837 fold_binary_loc (location_t loc
,
9838 enum tree_code code
, tree type
, tree op0
, tree op1
)
9840 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9841 tree arg0
, arg1
, tem
;
9842 tree t1
= NULL_TREE
;
9843 bool strict_overflow_p
;
9846 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9847 && TREE_CODE_LENGTH (code
) == 2
9849 && op1
!= NULL_TREE
);
9854 /* Strip any conversions that don't change the mode. This is
9855 safe for every expression, except for a comparison expression
9856 because its signedness is derived from its operands. So, in
9857 the latter case, only strip conversions that don't change the
9858 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9861 Note that this is done as an internal manipulation within the
9862 constant folder, in order to find the simplest representation
9863 of the arguments so that their form can be studied. In any
9864 cases, the appropriate type conversions should be put back in
9865 the tree that will get out of the constant folder. */
9867 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9869 STRIP_SIGN_NOPS (arg0
);
9870 STRIP_SIGN_NOPS (arg1
);
9878 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9879 constant but we can't do arithmetic on them. */
9880 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9882 tem
= const_binop (code
, type
, arg0
, arg1
);
9883 if (tem
!= NULL_TREE
)
9885 if (TREE_TYPE (tem
) != type
)
9886 tem
= fold_convert_loc (loc
, type
, tem
);
9891 /* If this is a commutative operation, and ARG0 is a constant, move it
9892 to ARG1 to reduce the number of tests below. */
9893 if (commutative_tree_code (code
)
9894 && tree_swap_operands_p (arg0
, arg1
, true))
9895 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9897 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9898 to ARG1 to reduce the number of tests below. */
9899 if (kind
== tcc_comparison
9900 && tree_swap_operands_p (arg0
, arg1
, true))
9901 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9903 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9907 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9909 First check for cases where an arithmetic operation is applied to a
9910 compound, conditional, or comparison operation. Push the arithmetic
9911 operation inside the compound or conditional to see if any folding
9912 can then be done. Convert comparison to conditional for this purpose.
9913 The also optimizes non-constant cases that used to be done in
9916 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9917 one of the operands is a comparison and the other is a comparison, a
9918 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9919 code below would make the expression more complex. Change it to a
9920 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9921 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9923 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9924 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9925 && TREE_CODE (type
) != VECTOR_TYPE
9926 && ((truth_value_p (TREE_CODE (arg0
))
9927 && (truth_value_p (TREE_CODE (arg1
))
9928 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9929 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9930 || (truth_value_p (TREE_CODE (arg1
))
9931 && (truth_value_p (TREE_CODE (arg0
))
9932 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9933 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9935 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9936 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9939 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9940 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9942 if (code
== EQ_EXPR
)
9943 tem
= invert_truthvalue_loc (loc
, tem
);
9945 return fold_convert_loc (loc
, type
, tem
);
9948 if (TREE_CODE_CLASS (code
) == tcc_binary
9949 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9951 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9953 tem
= fold_build2_loc (loc
, code
, type
,
9954 fold_convert_loc (loc
, TREE_TYPE (op0
),
9955 TREE_OPERAND (arg0
, 1)), op1
);
9956 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9959 if (TREE_CODE (arg1
) == COMPOUND_EXPR
9960 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
9962 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9963 fold_convert_loc (loc
, TREE_TYPE (op1
),
9964 TREE_OPERAND (arg1
, 1)));
9965 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9969 if (TREE_CODE (arg0
) == COND_EXPR
9970 || TREE_CODE (arg0
) == VEC_COND_EXPR
9971 || COMPARISON_CLASS_P (arg0
))
9973 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9975 /*cond_first_p=*/1);
9976 if (tem
!= NULL_TREE
)
9980 if (TREE_CODE (arg1
) == COND_EXPR
9981 || TREE_CODE (arg1
) == VEC_COND_EXPR
9982 || COMPARISON_CLASS_P (arg1
))
9984 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9986 /*cond_first_p=*/0);
9987 if (tem
!= NULL_TREE
)
9995 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9996 if (TREE_CODE (arg0
) == ADDR_EXPR
9997 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9999 tree iref
= TREE_OPERAND (arg0
, 0);
10000 return fold_build2 (MEM_REF
, type
,
10001 TREE_OPERAND (iref
, 0),
10002 int_const_binop (PLUS_EXPR
, arg1
,
10003 TREE_OPERAND (iref
, 1)));
10006 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10007 if (TREE_CODE (arg0
) == ADDR_EXPR
10008 && handled_component_p (TREE_OPERAND (arg0
, 0)))
10011 HOST_WIDE_INT coffset
;
10012 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
10016 return fold_build2 (MEM_REF
, type
,
10017 build_fold_addr_expr (base
),
10018 int_const_binop (PLUS_EXPR
, arg1
,
10019 size_int (coffset
)));
10024 case POINTER_PLUS_EXPR
:
10025 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10026 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10027 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
10028 return fold_convert_loc (loc
, type
,
10029 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
10030 fold_convert_loc (loc
, sizetype
,
10032 fold_convert_loc (loc
, sizetype
,
10038 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
10040 /* X + (X / CST) * -CST is X % CST. */
10041 if (TREE_CODE (arg1
) == MULT_EXPR
10042 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
10043 && operand_equal_p (arg0
,
10044 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
10046 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
10047 tree cst1
= TREE_OPERAND (arg1
, 1);
10048 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
10050 if (sum
&& integer_zerop (sum
))
10051 return fold_convert_loc (loc
, type
,
10052 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
10053 TREE_TYPE (arg0
), arg0
,
10058 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10059 one. Make sure the type is not saturating and has the signedness of
10060 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10061 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10062 if ((TREE_CODE (arg0
) == MULT_EXPR
10063 || TREE_CODE (arg1
) == MULT_EXPR
)
10064 && !TYPE_SATURATING (type
)
10065 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
10066 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
10067 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
10069 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
10074 if (! FLOAT_TYPE_P (type
))
10076 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10077 with a constant, and the two constants have no bits in common,
10078 we should treat this as a BIT_IOR_EXPR since this may produce more
10079 simplifications. */
10080 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10081 && TREE_CODE (arg1
) == BIT_AND_EXPR
10082 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10083 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
10084 && wi::bit_and (TREE_OPERAND (arg0
, 1),
10085 TREE_OPERAND (arg1
, 1)) == 0)
10087 code
= BIT_IOR_EXPR
;
10091 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10092 (plus (plus (mult) (mult)) (foo)) so that we can
10093 take advantage of the factoring cases below. */
10094 if (ANY_INTEGRAL_TYPE_P (type
)
10095 && TYPE_OVERFLOW_WRAPS (type
)
10096 && (((TREE_CODE (arg0
) == PLUS_EXPR
10097 || TREE_CODE (arg0
) == MINUS_EXPR
)
10098 && TREE_CODE (arg1
) == MULT_EXPR
)
10099 || ((TREE_CODE (arg1
) == PLUS_EXPR
10100 || TREE_CODE (arg1
) == MINUS_EXPR
)
10101 && TREE_CODE (arg0
) == MULT_EXPR
)))
10103 tree parg0
, parg1
, parg
, marg
;
10104 enum tree_code pcode
;
10106 if (TREE_CODE (arg1
) == MULT_EXPR
)
10107 parg
= arg0
, marg
= arg1
;
10109 parg
= arg1
, marg
= arg0
;
10110 pcode
= TREE_CODE (parg
);
10111 parg0
= TREE_OPERAND (parg
, 0);
10112 parg1
= TREE_OPERAND (parg
, 1);
10113 STRIP_NOPS (parg0
);
10114 STRIP_NOPS (parg1
);
10116 if (TREE_CODE (parg0
) == MULT_EXPR
10117 && TREE_CODE (parg1
) != MULT_EXPR
)
10118 return fold_build2_loc (loc
, pcode
, type
,
10119 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10120 fold_convert_loc (loc
, type
,
10122 fold_convert_loc (loc
, type
,
10124 fold_convert_loc (loc
, type
, parg1
));
10125 if (TREE_CODE (parg0
) != MULT_EXPR
10126 && TREE_CODE (parg1
) == MULT_EXPR
)
10128 fold_build2_loc (loc
, PLUS_EXPR
, type
,
10129 fold_convert_loc (loc
, type
, parg0
),
10130 fold_build2_loc (loc
, pcode
, type
,
10131 fold_convert_loc (loc
, type
, marg
),
10132 fold_convert_loc (loc
, type
,
10138 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10139 to __complex__ ( x, y ). This is not the same for SNaNs or
10140 if signed zeros are involved. */
10141 if (!HONOR_SNANS (element_mode (arg0
))
10142 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
10143 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
10145 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10146 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
10147 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
10148 bool arg0rz
= false, arg0iz
= false;
10149 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
10150 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
10152 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
10153 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
10154 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
10156 tree rp
= arg1r
? arg1r
10157 : build1 (REALPART_EXPR
, rtype
, arg1
);
10158 tree ip
= arg0i
? arg0i
10159 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
10160 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10162 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
10164 tree rp
= arg0r
? arg0r
10165 : build1 (REALPART_EXPR
, rtype
, arg0
);
10166 tree ip
= arg1i
? arg1i
10167 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
10168 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10173 if (flag_unsafe_math_optimizations
10174 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
10175 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
10176 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
10179 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10180 We associate floats only if the user has specified
10181 -fassociative-math. */
10182 if (flag_associative_math
10183 && TREE_CODE (arg1
) == PLUS_EXPR
10184 && TREE_CODE (arg0
) != MULT_EXPR
)
10186 tree tree10
= TREE_OPERAND (arg1
, 0);
10187 tree tree11
= TREE_OPERAND (arg1
, 1);
10188 if (TREE_CODE (tree11
) == MULT_EXPR
10189 && TREE_CODE (tree10
) == MULT_EXPR
)
10192 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
10193 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
10196 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10197 We associate floats only if the user has specified
10198 -fassociative-math. */
10199 if (flag_associative_math
10200 && TREE_CODE (arg0
) == PLUS_EXPR
10201 && TREE_CODE (arg1
) != MULT_EXPR
)
10203 tree tree00
= TREE_OPERAND (arg0
, 0);
10204 tree tree01
= TREE_OPERAND (arg0
, 1);
10205 if (TREE_CODE (tree01
) == MULT_EXPR
10206 && TREE_CODE (tree00
) == MULT_EXPR
)
10209 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
10210 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
10216 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10217 is a rotate of A by C1 bits. */
10218 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10219 is a rotate of A by B bits. */
10221 enum tree_code code0
, code1
;
10223 code0
= TREE_CODE (arg0
);
10224 code1
= TREE_CODE (arg1
);
10225 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
10226 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
10227 && operand_equal_p (TREE_OPERAND (arg0
, 0),
10228 TREE_OPERAND (arg1
, 0), 0)
10229 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10230 TYPE_UNSIGNED (rtype
))
10231 /* Only create rotates in complete modes. Other cases are not
10232 expanded properly. */
10233 && (element_precision (rtype
)
10234 == element_precision (TYPE_MODE (rtype
))))
10236 tree tree01
, tree11
;
10237 enum tree_code code01
, code11
;
10239 tree01
= TREE_OPERAND (arg0
, 1);
10240 tree11
= TREE_OPERAND (arg1
, 1);
10241 STRIP_NOPS (tree01
);
10242 STRIP_NOPS (tree11
);
10243 code01
= TREE_CODE (tree01
);
10244 code11
= TREE_CODE (tree11
);
10245 if (code01
== INTEGER_CST
10246 && code11
== INTEGER_CST
10247 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
10248 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
10250 tem
= build2_loc (loc
, LROTATE_EXPR
,
10251 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10252 TREE_OPERAND (arg0
, 0),
10253 code0
== LSHIFT_EXPR
10254 ? TREE_OPERAND (arg0
, 1)
10255 : TREE_OPERAND (arg1
, 1));
10256 return fold_convert_loc (loc
, type
, tem
);
10258 else if (code11
== MINUS_EXPR
)
10260 tree tree110
, tree111
;
10261 tree110
= TREE_OPERAND (tree11
, 0);
10262 tree111
= TREE_OPERAND (tree11
, 1);
10263 STRIP_NOPS (tree110
);
10264 STRIP_NOPS (tree111
);
10265 if (TREE_CODE (tree110
) == INTEGER_CST
10266 && 0 == compare_tree_int (tree110
,
10268 (TREE_TYPE (TREE_OPERAND
10270 && operand_equal_p (tree01
, tree111
, 0))
10272 fold_convert_loc (loc
, type
,
10273 build2 ((code0
== LSHIFT_EXPR
10276 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10277 TREE_OPERAND (arg0
, 0),
10278 TREE_OPERAND (arg0
, 1)));
10280 else if (code01
== MINUS_EXPR
)
10282 tree tree010
, tree011
;
10283 tree010
= TREE_OPERAND (tree01
, 0);
10284 tree011
= TREE_OPERAND (tree01
, 1);
10285 STRIP_NOPS (tree010
);
10286 STRIP_NOPS (tree011
);
10287 if (TREE_CODE (tree010
) == INTEGER_CST
10288 && 0 == compare_tree_int (tree010
,
10290 (TREE_TYPE (TREE_OPERAND
10292 && operand_equal_p (tree11
, tree011
, 0))
10293 return fold_convert_loc
10295 build2 ((code0
!= LSHIFT_EXPR
10298 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
10299 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
10305 /* In most languages, can't associate operations on floats through
10306 parentheses. Rather than remember where the parentheses were, we
10307 don't associate floats at all, unless the user has specified
10308 -fassociative-math.
10309 And, we need to make sure type is not saturating. */
10311 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
10312 && !TYPE_SATURATING (type
))
10314 tree var0
, con0
, lit0
, minus_lit0
;
10315 tree var1
, con1
, lit1
, minus_lit1
;
10319 /* Split both trees into variables, constants, and literals. Then
10320 associate each group together, the constants with literals,
10321 then the result with variables. This increases the chances of
10322 literals being recombined later and of generating relocatable
10323 expressions for the sum of a constant and literal. */
10324 var0
= split_tree (arg0
, code
, &con0
, &lit0
, &minus_lit0
, 0);
10325 var1
= split_tree (arg1
, code
, &con1
, &lit1
, &minus_lit1
,
10326 code
== MINUS_EXPR
);
10328 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10329 if (code
== MINUS_EXPR
)
10332 /* With undefined overflow prefer doing association in a type
10333 which wraps on overflow, if that is one of the operand types. */
10334 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
10335 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
10337 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10338 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10339 atype
= TREE_TYPE (arg0
);
10340 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
10341 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
10342 atype
= TREE_TYPE (arg1
);
10343 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
10346 /* With undefined overflow we can only associate constants with one
10347 variable, and constants whose association doesn't overflow. */
10348 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
10349 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
10356 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
10357 tmp0
= TREE_OPERAND (tmp0
, 0);
10358 if (CONVERT_EXPR_P (tmp0
)
10359 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
10360 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
10361 <= TYPE_PRECISION (atype
)))
10362 tmp0
= TREE_OPERAND (tmp0
, 0);
10363 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
10364 tmp1
= TREE_OPERAND (tmp1
, 0);
10365 if (CONVERT_EXPR_P (tmp1
)
10366 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
10367 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
10368 <= TYPE_PRECISION (atype
)))
10369 tmp1
= TREE_OPERAND (tmp1
, 0);
10370 /* The only case we can still associate with two variables
10371 is if they are the same, modulo negation and bit-pattern
10372 preserving conversions. */
10373 if (!operand_equal_p (tmp0
, tmp1
, 0))
10378 /* Only do something if we found more than two objects. Otherwise,
10379 nothing has changed and we risk infinite recursion. */
10381 && (2 < ((var0
!= 0) + (var1
!= 0)
10382 + (con0
!= 0) + (con1
!= 0)
10383 + (lit0
!= 0) + (lit1
!= 0)
10384 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
10386 bool any_overflows
= false;
10387 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
10388 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
10389 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
10390 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
10391 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
10392 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
10393 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
10394 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
10397 /* Preserve the MINUS_EXPR if the negative part of the literal is
10398 greater than the positive part. Otherwise, the multiplicative
10399 folding code (i.e extract_muldiv) may be fooled in case
10400 unsigned constants are subtracted, like in the following
10401 example: ((X*2 + 4) - 8U)/2. */
10402 if (minus_lit0
&& lit0
)
10404 if (TREE_CODE (lit0
) == INTEGER_CST
10405 && TREE_CODE (minus_lit0
) == INTEGER_CST
10406 && tree_int_cst_lt (lit0
, minus_lit0
))
10408 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
10409 MINUS_EXPR
, atype
);
10414 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
10415 MINUS_EXPR
, atype
);
10420 /* Don't introduce overflows through reassociation. */
10422 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
10423 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
10430 fold_convert_loc (loc
, type
,
10431 associate_trees (loc
, var0
, minus_lit0
,
10432 MINUS_EXPR
, atype
));
10435 con0
= associate_trees (loc
, con0
, minus_lit0
,
10436 MINUS_EXPR
, atype
);
10438 fold_convert_loc (loc
, type
,
10439 associate_trees (loc
, var0
, con0
,
10440 PLUS_EXPR
, atype
));
10444 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
10446 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
10454 /* Pointer simplifications for subtraction, simple reassociations. */
10455 if (POINTER_TYPE_P (TREE_TYPE (arg1
)) && POINTER_TYPE_P (TREE_TYPE (arg0
)))
10457 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10458 if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10459 && TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10461 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10462 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10463 tree arg10
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10464 tree arg11
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10465 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10466 fold_build2_loc (loc
, MINUS_EXPR
, type
,
10468 fold_build2_loc (loc
, MINUS_EXPR
, type
,
10471 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10472 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
10474 tree arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10475 tree arg01
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10476 tree tmp
= fold_binary_loc (loc
, MINUS_EXPR
, type
, arg00
,
10477 fold_convert_loc (loc
, type
, arg1
));
10479 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tmp
, arg01
);
10481 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
10483 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
10485 tree arg10
= fold_convert_loc (loc
, type
,
10486 TREE_OPERAND (arg1
, 0));
10487 tree arg11
= fold_convert_loc (loc
, type
,
10488 TREE_OPERAND (arg1
, 1));
10489 tree tmp
= fold_binary_loc (loc
, MINUS_EXPR
, type
,
10490 fold_convert_loc (loc
, type
, arg0
),
10493 return fold_build2_loc (loc
, MINUS_EXPR
, type
, tmp
, arg11
);
10496 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10497 if (TREE_CODE (arg0
) == NEGATE_EXPR
10498 && negate_expr_p (arg1
)
10499 && reorder_operands_p (arg0
, arg1
))
10500 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
10501 fold_convert_loc (loc
, type
,
10502 negate_expr (arg1
)),
10503 fold_convert_loc (loc
, type
,
10504 TREE_OPERAND (arg0
, 0)));
10506 if (! FLOAT_TYPE_P (type
))
10508 /* Fold A - (A & B) into ~B & A. */
10509 if (!TREE_SIDE_EFFECTS (arg0
)
10510 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10512 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0))
10514 tree arg10
= fold_convert_loc (loc
, type
,
10515 TREE_OPERAND (arg1
, 0));
10516 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10517 fold_build1_loc (loc
, BIT_NOT_EXPR
,
10519 fold_convert_loc (loc
, type
, arg0
));
10521 if (operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10523 tree arg11
= fold_convert_loc (loc
,
10524 type
, TREE_OPERAND (arg1
, 1));
10525 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10526 fold_build1_loc (loc
, BIT_NOT_EXPR
,
10528 fold_convert_loc (loc
, type
, arg0
));
10532 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10533 any power of 2 minus 1. */
10534 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10535 && TREE_CODE (arg1
) == BIT_AND_EXPR
10536 && operand_equal_p (TREE_OPERAND (arg0
, 0),
10537 TREE_OPERAND (arg1
, 0), 0))
10539 tree mask0
= TREE_OPERAND (arg0
, 1);
10540 tree mask1
= TREE_OPERAND (arg1
, 1);
10541 tree tem
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, mask0
);
10543 if (operand_equal_p (tem
, mask1
, 0))
10545 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
10546 TREE_OPERAND (arg0
, 0), mask1
);
10547 return fold_build2_loc (loc
, MINUS_EXPR
, type
, tem
, mask1
);
10552 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10553 __complex__ ( x, -y ). This is not the same for SNaNs or if
10554 signed zeros are involved. */
10555 if (!HONOR_SNANS (element_mode (arg0
))
10556 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
10557 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
10559 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10560 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
10561 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
10562 bool arg0rz
= false, arg0iz
= false;
10563 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
10564 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
10566 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
10567 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
10568 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
10570 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
10572 : build1 (REALPART_EXPR
, rtype
, arg1
));
10573 tree ip
= arg0i
? arg0i
10574 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
10575 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10577 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
10579 tree rp
= arg0r
? arg0r
10580 : build1 (REALPART_EXPR
, rtype
, arg0
);
10581 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
10583 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
10584 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
10589 /* A - B -> A + (-B) if B is easily negatable. */
10590 if (negate_expr_p (arg1
)
10591 && !TYPE_OVERFLOW_SANITIZED (type
)
10592 && ((FLOAT_TYPE_P (type
)
10593 /* Avoid this transformation if B is a positive REAL_CST. */
10594 && (TREE_CODE (arg1
) != REAL_CST
10595 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
))))
10596 || INTEGRAL_TYPE_P (type
)))
10597 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
10598 fold_convert_loc (loc
, type
, arg0
),
10599 fold_convert_loc (loc
, type
,
10600 negate_expr (arg1
)));
10602 /* Fold &a[i] - &a[j] to i-j. */
10603 if (TREE_CODE (arg0
) == ADDR_EXPR
10604 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
10605 && TREE_CODE (arg1
) == ADDR_EXPR
10606 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
10608 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
10609 TREE_OPERAND (arg0
, 0),
10610 TREE_OPERAND (arg1
, 0));
10615 if (FLOAT_TYPE_P (type
)
10616 && flag_unsafe_math_optimizations
10617 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
10618 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
10619 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
10622 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10623 one. Make sure the type is not saturating and has the signedness of
10624 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10625 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10626 if ((TREE_CODE (arg0
) == MULT_EXPR
10627 || TREE_CODE (arg1
) == MULT_EXPR
)
10628 && !TYPE_SATURATING (type
)
10629 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
10630 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
10631 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
10633 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
10641 /* (-A) * (-B) -> A * B */
10642 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10643 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10644 fold_convert_loc (loc
, type
,
10645 TREE_OPERAND (arg0
, 0)),
10646 fold_convert_loc (loc
, type
,
10647 negate_expr (arg1
)));
10648 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10649 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10650 fold_convert_loc (loc
, type
,
10651 negate_expr (arg0
)),
10652 fold_convert_loc (loc
, type
,
10653 TREE_OPERAND (arg1
, 0)));
10655 if (! FLOAT_TYPE_P (type
))
10657 /* Transform x * -C into -x * C if x is easily negatable. */
10658 if (TREE_CODE (arg1
) == INTEGER_CST
10659 && tree_int_cst_sgn (arg1
) == -1
10660 && negate_expr_p (arg0
)
10661 && (tem
= negate_expr (arg1
)) != arg1
10662 && !TREE_OVERFLOW (tem
))
10663 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10664 fold_convert_loc (loc
, type
,
10665 negate_expr (arg0
)),
10668 /* (a * (1 << b)) is (a << b) */
10669 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10670 && integer_onep (TREE_OPERAND (arg1
, 0)))
10671 return fold_build2_loc (loc
, LSHIFT_EXPR
, type
, op0
,
10672 TREE_OPERAND (arg1
, 1));
10673 if (TREE_CODE (arg0
) == LSHIFT_EXPR
10674 && integer_onep (TREE_OPERAND (arg0
, 0)))
10675 return fold_build2_loc (loc
, LSHIFT_EXPR
, type
, op1
,
10676 TREE_OPERAND (arg0
, 1));
10678 /* (A + A) * C -> A * 2 * C */
10679 if (TREE_CODE (arg0
) == PLUS_EXPR
10680 && TREE_CODE (arg1
) == INTEGER_CST
10681 && operand_equal_p (TREE_OPERAND (arg0
, 0),
10682 TREE_OPERAND (arg0
, 1), 0))
10683 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10684 omit_one_operand_loc (loc
, type
,
10685 TREE_OPERAND (arg0
, 0),
10686 TREE_OPERAND (arg0
, 1)),
10687 fold_build2_loc (loc
, MULT_EXPR
, type
,
10688 build_int_cst (type
, 2) , arg1
));
10690 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10691 sign-changing only. */
10692 if (TREE_CODE (arg1
) == INTEGER_CST
10693 && TREE_CODE (arg0
) == EXACT_DIV_EXPR
10694 && operand_equal_p (arg1
, TREE_OPERAND (arg0
, 1), 0))
10695 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10697 strict_overflow_p
= false;
10698 if (TREE_CODE (arg1
) == INTEGER_CST
10699 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10700 &strict_overflow_p
)))
10702 if (strict_overflow_p
)
10703 fold_overflow_warning (("assuming signed overflow does not "
10704 "occur when simplifying "
10706 WARN_STRICT_OVERFLOW_MISC
);
10707 return fold_convert_loc (loc
, type
, tem
);
10710 /* Optimize z * conj(z) for integer complex numbers. */
10711 if (TREE_CODE (arg0
) == CONJ_EXPR
10712 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10713 return fold_mult_zconjz (loc
, type
, arg1
);
10714 if (TREE_CODE (arg1
) == CONJ_EXPR
10715 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10716 return fold_mult_zconjz (loc
, type
, arg0
);
10720 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10721 the result for floating point types due to rounding so it is applied
10722 only if -fassociative-math was specify. */
10723 if (flag_associative_math
10724 && TREE_CODE (arg0
) == RDIV_EXPR
10725 && TREE_CODE (arg1
) == REAL_CST
10726 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == REAL_CST
)
10728 tree tem
= const_binop (MULT_EXPR
, TREE_OPERAND (arg0
, 0),
10731 return fold_build2_loc (loc
, RDIV_EXPR
, type
, tem
,
10732 TREE_OPERAND (arg0
, 1));
10735 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10736 if (operand_equal_p (arg0
, arg1
, 0))
10738 tree tem
= fold_strip_sign_ops (arg0
);
10739 if (tem
!= NULL_TREE
)
10741 tem
= fold_convert_loc (loc
, type
, tem
);
10742 return fold_build2_loc (loc
, MULT_EXPR
, type
, tem
, tem
);
10746 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10747 This is not the same for NaNs or if signed zeros are
10749 if (!HONOR_NANS (arg0
)
10750 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
10751 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
10752 && TREE_CODE (arg1
) == COMPLEX_CST
10753 && real_zerop (TREE_REALPART (arg1
)))
10755 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
10756 if (real_onep (TREE_IMAGPART (arg1
)))
10758 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
10759 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
10761 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
10762 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
10764 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
10765 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
10766 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
10770 /* Optimize z * conj(z) for floating point complex numbers.
10771 Guarded by flag_unsafe_math_optimizations as non-finite
10772 imaginary components don't produce scalar results. */
10773 if (flag_unsafe_math_optimizations
10774 && TREE_CODE (arg0
) == CONJ_EXPR
10775 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10776 return fold_mult_zconjz (loc
, type
, arg1
);
10777 if (flag_unsafe_math_optimizations
10778 && TREE_CODE (arg1
) == CONJ_EXPR
10779 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10780 return fold_mult_zconjz (loc
, type
, arg0
);
10782 if (flag_unsafe_math_optimizations
)
10784 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
10785 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
10787 /* Optimizations of root(...)*root(...). */
10788 if (fcode0
== fcode1
&& BUILTIN_ROOT_P (fcode0
))
10791 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
10792 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
10794 /* Optimize sqrt(x)*sqrt(x) as x. */
10795 if (BUILTIN_SQRT_P (fcode0
)
10796 && operand_equal_p (arg00
, arg10
, 0)
10797 && ! HONOR_SNANS (element_mode (type
)))
10800 /* Optimize root(x)*root(y) as root(x*y). */
10801 rootfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
10802 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg00
, arg10
);
10803 return build_call_expr_loc (loc
, rootfn
, 1, arg
);
10806 /* Optimize expN(x)*expN(y) as expN(x+y). */
10807 if (fcode0
== fcode1
&& BUILTIN_EXPONENT_P (fcode0
))
10809 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
10810 tree arg
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
10811 CALL_EXPR_ARG (arg0
, 0),
10812 CALL_EXPR_ARG (arg1
, 0));
10813 return build_call_expr_loc (loc
, expfn
, 1, arg
);
10816 /* Optimizations of pow(...)*pow(...). */
10817 if ((fcode0
== BUILT_IN_POW
&& fcode1
== BUILT_IN_POW
)
10818 || (fcode0
== BUILT_IN_POWF
&& fcode1
== BUILT_IN_POWF
)
10819 || (fcode0
== BUILT_IN_POWL
&& fcode1
== BUILT_IN_POWL
))
10821 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
10822 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
10823 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
10824 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
10826 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10827 if (operand_equal_p (arg01
, arg11
, 0))
10829 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
10830 tree arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
10832 return build_call_expr_loc (loc
, powfn
, 2, arg
, arg01
);
10835 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10836 if (operand_equal_p (arg00
, arg10
, 0))
10838 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
10839 tree arg
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
10841 return build_call_expr_loc (loc
, powfn
, 2, arg00
, arg
);
10845 /* Optimize tan(x)*cos(x) as sin(x). */
10846 if (((fcode0
== BUILT_IN_TAN
&& fcode1
== BUILT_IN_COS
)
10847 || (fcode0
== BUILT_IN_TANF
&& fcode1
== BUILT_IN_COSF
)
10848 || (fcode0
== BUILT_IN_TANL
&& fcode1
== BUILT_IN_COSL
)
10849 || (fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_TAN
)
10850 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_TANF
)
10851 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_TANL
))
10852 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
10853 CALL_EXPR_ARG (arg1
, 0), 0))
10855 tree sinfn
= mathfn_built_in (type
, BUILT_IN_SIN
);
10857 if (sinfn
!= NULL_TREE
)
10858 return build_call_expr_loc (loc
, sinfn
, 1,
10859 CALL_EXPR_ARG (arg0
, 0));
10862 /* Optimize x*pow(x,c) as pow(x,c+1). */
10863 if (fcode1
== BUILT_IN_POW
10864 || fcode1
== BUILT_IN_POWF
10865 || fcode1
== BUILT_IN_POWL
)
10867 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
10868 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
10869 if (TREE_CODE (arg11
) == REAL_CST
10870 && !TREE_OVERFLOW (arg11
)
10871 && operand_equal_p (arg0
, arg10
, 0))
10873 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
10877 c
= TREE_REAL_CST (arg11
);
10878 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
10879 arg
= build_real (type
, c
);
10880 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
10884 /* Optimize pow(x,c)*x as pow(x,c+1). */
10885 if (fcode0
== BUILT_IN_POW
10886 || fcode0
== BUILT_IN_POWF
10887 || fcode0
== BUILT_IN_POWL
)
10889 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
10890 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
10891 if (TREE_CODE (arg01
) == REAL_CST
10892 && !TREE_OVERFLOW (arg01
)
10893 && operand_equal_p (arg1
, arg00
, 0))
10895 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
10899 c
= TREE_REAL_CST (arg01
);
10900 real_arithmetic (&c
, PLUS_EXPR
, &c
, &dconst1
);
10901 arg
= build_real (type
, c
);
10902 return build_call_expr_loc (loc
, powfn
, 2, arg1
, arg
);
10906 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10907 if (!in_gimple_form
10909 && operand_equal_p (arg0
, arg1
, 0))
10911 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
10915 tree arg
= build_real (type
, dconst2
);
10916 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
10925 /* Canonicalize (X & C1) | C2. */
10926 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10927 && TREE_CODE (arg1
) == INTEGER_CST
10928 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10930 int width
= TYPE_PRECISION (type
), w
;
10931 wide_int c1
= TREE_OPERAND (arg0
, 1);
10932 wide_int c2
= arg1
;
10934 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10935 if ((c1
& c2
) == c1
)
10936 return omit_one_operand_loc (loc
, type
, arg1
,
10937 TREE_OPERAND (arg0
, 0));
10939 wide_int msk
= wi::mask (width
, false,
10940 TYPE_PRECISION (TREE_TYPE (arg1
)));
10942 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10943 if (msk
.and_not (c1
| c2
) == 0)
10944 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
10945 TREE_OPERAND (arg0
, 0), arg1
);
10947 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10948 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10949 mode which allows further optimizations. */
10952 wide_int c3
= c1
.and_not (c2
);
10953 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
10955 wide_int mask
= wi::mask (w
, false,
10956 TYPE_PRECISION (type
));
10957 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
10965 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
10966 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
10967 TREE_OPERAND (arg0
, 0),
10968 wide_int_to_tree (type
,
10973 /* (X & ~Y) | (~X & Y) is X ^ Y */
10974 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10975 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10977 tree a0
, a1
, l0
, l1
, n0
, n1
;
10979 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10980 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10982 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10983 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10985 n0
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, l0
);
10986 n1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, l1
);
10988 if ((operand_equal_p (n0
, a0
, 0)
10989 && operand_equal_p (n1
, a1
, 0))
10990 || (operand_equal_p (n0
, a1
, 0)
10991 && operand_equal_p (n1
, a0
, 0)))
10992 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, l0
, n1
);
10995 t1
= distribute_bit_expr (loc
, code
, type
, arg0
, arg1
);
10996 if (t1
!= NULL_TREE
)
10999 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11001 This results in more efficient code for machines without a NAND
11002 instruction. Combine will canonicalize to the first form
11003 which will allow use of NAND instructions provided by the
11004 backend if they exist. */
11005 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11006 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11009 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
11010 build2 (BIT_AND_EXPR
, type
,
11011 fold_convert_loc (loc
, type
,
11012 TREE_OPERAND (arg0
, 0)),
11013 fold_convert_loc (loc
, type
,
11014 TREE_OPERAND (arg1
, 0))));
11017 /* See if this can be simplified into a rotate first. If that
11018 is unsuccessful continue in the association code. */
11022 /* ~X ^ X is -1. */
11023 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11024 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11026 t1
= build_zero_cst (type
);
11027 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11028 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
11031 /* X ^ ~X is -1. */
11032 if (TREE_CODE (arg1
) == BIT_NOT_EXPR
11033 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11035 t1
= build_zero_cst (type
);
11036 t1
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
, t1
);
11037 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
11040 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11041 with a constant, and the two constants have no bits in common,
11042 we should treat this as a BIT_IOR_EXPR since this may produce more
11043 simplifications. */
11044 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11045 && TREE_CODE (arg1
) == BIT_AND_EXPR
11046 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11047 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
11048 && wi::bit_and (TREE_OPERAND (arg0
, 1),
11049 TREE_OPERAND (arg1
, 1)) == 0)
11051 code
= BIT_IOR_EXPR
;
11055 /* (X | Y) ^ X -> Y & ~ X*/
11056 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11057 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11059 tree t2
= TREE_OPERAND (arg0
, 1);
11060 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg1
),
11062 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11063 fold_convert_loc (loc
, type
, t2
),
11064 fold_convert_loc (loc
, type
, t1
));
11068 /* (Y | X) ^ X -> Y & ~ X*/
11069 if (TREE_CODE (arg0
) == BIT_IOR_EXPR
11070 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11072 tree t2
= TREE_OPERAND (arg0
, 0);
11073 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg1
),
11075 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11076 fold_convert_loc (loc
, type
, t2
),
11077 fold_convert_loc (loc
, type
, t1
));
11081 /* X ^ (X | Y) -> Y & ~ X*/
11082 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11083 && operand_equal_p (TREE_OPERAND (arg1
, 0), arg0
, 0))
11085 tree t2
= TREE_OPERAND (arg1
, 1);
11086 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg0
),
11088 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11089 fold_convert_loc (loc
, type
, t2
),
11090 fold_convert_loc (loc
, type
, t1
));
11094 /* X ^ (Y | X) -> Y & ~ X*/
11095 if (TREE_CODE (arg1
) == BIT_IOR_EXPR
11096 && operand_equal_p (TREE_OPERAND (arg1
, 1), arg0
, 0))
11098 tree t2
= TREE_OPERAND (arg1
, 0);
11099 t1
= fold_build1_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (arg0
),
11101 t1
= fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11102 fold_convert_loc (loc
, type
, t2
),
11103 fold_convert_loc (loc
, type
, t1
));
11107 /* Convert ~X ^ ~Y to X ^ Y. */
11108 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11109 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11110 return fold_build2_loc (loc
, code
, type
,
11111 fold_convert_loc (loc
, type
,
11112 TREE_OPERAND (arg0
, 0)),
11113 fold_convert_loc (loc
, type
,
11114 TREE_OPERAND (arg1
, 0)));
11116 /* Convert ~X ^ C to X ^ ~C. */
11117 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11118 && TREE_CODE (arg1
) == INTEGER_CST
)
11119 return fold_build2_loc (loc
, code
, type
,
11120 fold_convert_loc (loc
, type
,
11121 TREE_OPERAND (arg0
, 0)),
11122 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, arg1
));
11124 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11125 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11126 && INTEGRAL_TYPE_P (type
)
11127 && integer_onep (TREE_OPERAND (arg0
, 1))
11128 && integer_onep (arg1
))
11129 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
11130 build_zero_cst (TREE_TYPE (arg0
)));
11132 /* Fold (X & Y) ^ Y as ~X & Y. */
11133 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11134 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11136 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11137 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11138 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11139 fold_convert_loc (loc
, type
, arg1
));
11141 /* Fold (X & Y) ^ X as ~Y & X. */
11142 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11143 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11144 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11146 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11147 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11148 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11149 fold_convert_loc (loc
, type
, arg1
));
11151 /* Fold X ^ (X & Y) as X & ~Y. */
11152 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11153 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11155 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11156 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11157 fold_convert_loc (loc
, type
, arg0
),
11158 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
11160 /* Fold X ^ (Y & X) as ~Y & X. */
11161 if (TREE_CODE (arg1
) == BIT_AND_EXPR
11162 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11163 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11165 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11166 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11167 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11168 fold_convert_loc (loc
, type
, arg0
));
11171 /* See if this can be simplified into a rotate first. If that
11172 is unsuccessful continue in the association code. */
11176 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11177 if ((TREE_CODE (arg0
) == BIT_NOT_EXPR
11178 || TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11179 || (TREE_CODE (arg0
) == EQ_EXPR
11180 && integer_zerop (TREE_OPERAND (arg0
, 1))))
11181 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11182 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
11184 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11185 if ((TREE_CODE (arg1
) == BIT_NOT_EXPR
11186 || TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11187 || (TREE_CODE (arg1
) == EQ_EXPR
11188 && integer_zerop (TREE_OPERAND (arg1
, 1))))
11189 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11190 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11192 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11193 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11194 && INTEGRAL_TYPE_P (type
)
11195 && integer_onep (TREE_OPERAND (arg0
, 1))
11196 && integer_onep (arg1
))
11199 tem
= TREE_OPERAND (arg0
, 0);
11200 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11201 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11203 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11204 build_zero_cst (TREE_TYPE (tem
)));
11206 /* Fold ~X & 1 as (X & 1) == 0. */
11207 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11208 && INTEGRAL_TYPE_P (type
)
11209 && integer_onep (arg1
))
11212 tem
= TREE_OPERAND (arg0
, 0);
11213 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
11214 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
11216 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
11217 build_zero_cst (TREE_TYPE (tem
)));
11219 /* Fold !X & 1 as X == 0. */
11220 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11221 && integer_onep (arg1
))
11223 tem
= TREE_OPERAND (arg0
, 0);
11224 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
11225 build_zero_cst (TREE_TYPE (tem
)));
11228 /* Fold (X ^ Y) & Y as ~X & Y. */
11229 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11230 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
11232 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11233 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11234 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11235 fold_convert_loc (loc
, type
, arg1
));
11237 /* Fold (X ^ Y) & X as ~Y & X. */
11238 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
11239 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
11240 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
11242 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
11243 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11244 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11245 fold_convert_loc (loc
, type
, arg1
));
11247 /* Fold X & (X ^ Y) as X & ~Y. */
11248 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
11249 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11251 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
11252 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11253 fold_convert_loc (loc
, type
, arg0
),
11254 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
11256 /* Fold X & (Y ^ X) as ~Y & X. */
11257 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
11258 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
11259 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
11261 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
11262 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11263 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
11264 fold_convert_loc (loc
, type
, arg0
));
11267 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11268 multiple of 1 << CST. */
11269 if (TREE_CODE (arg1
) == INTEGER_CST
)
11271 wide_int cst1
= arg1
;
11272 wide_int ncst1
= -cst1
;
11273 if ((cst1
& ncst1
) == ncst1
11274 && multiple_of_p (type
, arg0
,
11275 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
11276 return fold_convert_loc (loc
, type
, arg0
);
11279 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11281 if (TREE_CODE (arg1
) == INTEGER_CST
11282 && TREE_CODE (arg0
) == MULT_EXPR
11283 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11285 wide_int warg1
= arg1
;
11286 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
11289 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
11291 else if (masked
!= warg1
)
11293 /* Avoid the transform if arg1 is a mask of some
11294 mode which allows further optimizations. */
11295 int pop
= wi::popcount (warg1
);
11296 if (!(pop
>= BITS_PER_UNIT
11297 && exact_log2 (pop
) != -1
11298 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
11299 return fold_build2_loc (loc
, code
, type
, op0
,
11300 wide_int_to_tree (type
, masked
));
11304 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11305 ((A & N) + B) & M -> (A + B) & M
11306 Similarly if (N & M) == 0,
11307 ((A | N) + B) & M -> (A + B) & M
11308 and for - instead of + (or unary - instead of +)
11309 and/or ^ instead of |.
11310 If B is constant and (B & M) == 0, fold into A & M. */
11311 if (TREE_CODE (arg1
) == INTEGER_CST
)
11313 wide_int cst1
= arg1
;
11314 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
11315 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11316 && (TREE_CODE (arg0
) == PLUS_EXPR
11317 || TREE_CODE (arg0
) == MINUS_EXPR
11318 || TREE_CODE (arg0
) == NEGATE_EXPR
)
11319 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
11320 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
11326 /* Now we know that arg0 is (C + D) or (C - D) or
11327 -C and arg1 (M) is == (1LL << cst) - 1.
11328 Store C into PMOP[0] and D into PMOP[1]. */
11329 pmop
[0] = TREE_OPERAND (arg0
, 0);
11331 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
11333 pmop
[1] = TREE_OPERAND (arg0
, 1);
11337 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
11340 for (; which
>= 0; which
--)
11341 switch (TREE_CODE (pmop
[which
]))
11346 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
11349 cst0
= TREE_OPERAND (pmop
[which
], 1);
11351 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
11356 else if (cst0
!= 0)
11358 /* If C or D is of the form (A & N) where
11359 (N & M) == M, or of the form (A | N) or
11360 (A ^ N) where (N & M) == 0, replace it with A. */
11361 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
11364 /* If C or D is a N where (N & M) == 0, it can be
11365 omitted (assumed 0). */
11366 if ((TREE_CODE (arg0
) == PLUS_EXPR
11367 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
11368 && (cst1
& pmop
[which
]) == 0)
11369 pmop
[which
] = NULL
;
11375 /* Only build anything new if we optimized one or both arguments
11377 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
11378 || (TREE_CODE (arg0
) != NEGATE_EXPR
11379 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
11381 tree utype
= TREE_TYPE (arg0
);
11382 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
11384 /* Perform the operations in a type that has defined
11385 overflow behavior. */
11386 utype
= unsigned_type_for (TREE_TYPE (arg0
));
11387 if (pmop
[0] != NULL
)
11388 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
11389 if (pmop
[1] != NULL
)
11390 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
11393 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
11394 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
11395 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
11397 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
11398 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
11400 else if (pmop
[0] != NULL
)
11402 else if (pmop
[1] != NULL
)
11405 return build_int_cst (type
, 0);
11407 else if (pmop
[0] == NULL
)
11408 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
11410 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
11412 /* TEM is now the new binary +, - or unary - replacement. */
11413 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
11414 fold_convert_loc (loc
, utype
, arg1
));
11415 return fold_convert_loc (loc
, type
, tem
);
11420 t1
= distribute_bit_expr (loc
, code
, type
, arg0
, arg1
);
11421 if (t1
!= NULL_TREE
)
11423 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11424 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
11425 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
11427 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
11429 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
11432 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11435 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11437 This results in more efficient code for machines without a NOR
11438 instruction. Combine will canonicalize to the first form
11439 which will allow use of NOR instructions provided by the
11440 backend if they exist. */
11441 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
11442 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
11444 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
11445 build2 (BIT_IOR_EXPR
, type
,
11446 fold_convert_loc (loc
, type
,
11447 TREE_OPERAND (arg0
, 0)),
11448 fold_convert_loc (loc
, type
,
11449 TREE_OPERAND (arg1
, 0))));
11452 /* If arg0 is derived from the address of an object or function, we may
11453 be able to fold this expression using the object or function's
11455 if (POINTER_TYPE_P (TREE_TYPE (arg0
)) && tree_fits_uhwi_p (arg1
))
11457 unsigned HOST_WIDE_INT modulus
, residue
;
11458 unsigned HOST_WIDE_INT low
= tree_to_uhwi (arg1
);
11460 modulus
= get_pointer_modulus_and_residue (arg0
, &residue
,
11461 integer_onep (arg1
));
11463 /* This works because modulus is a power of 2. If this weren't the
11464 case, we'd have to replace it by its greatest power-of-2
11465 divisor: modulus & -modulus. */
11467 return build_int_cst (type
, residue
& low
);
11473 /* Don't touch a floating-point divide by zero unless the mode
11474 of the constant can represent infinity. */
11475 if (TREE_CODE (arg1
) == REAL_CST
11476 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
11477 && real_zerop (arg1
))
11480 /* (-A) / (-B) -> A / B */
11481 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
11482 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11483 TREE_OPERAND (arg0
, 0),
11484 negate_expr (arg1
));
11485 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
11486 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11487 negate_expr (arg0
),
11488 TREE_OPERAND (arg1
, 0));
11490 /* Convert A/B/C to A/(B*C). */
11491 if (flag_reciprocal_math
11492 && TREE_CODE (arg0
) == RDIV_EXPR
)
11493 return fold_build2_loc (loc
, RDIV_EXPR
, type
, TREE_OPERAND (arg0
, 0),
11494 fold_build2_loc (loc
, MULT_EXPR
, type
,
11495 TREE_OPERAND (arg0
, 1), arg1
));
11497 /* Convert A/(B/C) to (A/B)*C. */
11498 if (flag_reciprocal_math
11499 && TREE_CODE (arg1
) == RDIV_EXPR
)
11500 return fold_build2_loc (loc
, MULT_EXPR
, type
,
11501 fold_build2_loc (loc
, RDIV_EXPR
, type
, arg0
,
11502 TREE_OPERAND (arg1
, 0)),
11503 TREE_OPERAND (arg1
, 1));
11505 /* Convert C1/(X*C2) into (C1/C2)/X. */
11506 if (flag_reciprocal_math
11507 && TREE_CODE (arg1
) == MULT_EXPR
11508 && TREE_CODE (arg0
) == REAL_CST
11509 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
11511 tree tem
= const_binop (RDIV_EXPR
, arg0
,
11512 TREE_OPERAND (arg1
, 1));
11514 return fold_build2_loc (loc
, RDIV_EXPR
, type
, tem
,
11515 TREE_OPERAND (arg1
, 0));
11518 if (flag_unsafe_math_optimizations
)
11520 enum built_in_function fcode0
= builtin_mathfn_code (arg0
);
11521 enum built_in_function fcode1
= builtin_mathfn_code (arg1
);
11523 /* Optimize sin(x)/cos(x) as tan(x). */
11524 if (((fcode0
== BUILT_IN_SIN
&& fcode1
== BUILT_IN_COS
)
11525 || (fcode0
== BUILT_IN_SINF
&& fcode1
== BUILT_IN_COSF
)
11526 || (fcode0
== BUILT_IN_SINL
&& fcode1
== BUILT_IN_COSL
))
11527 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
11528 CALL_EXPR_ARG (arg1
, 0), 0))
11530 tree tanfn
= mathfn_built_in (type
, BUILT_IN_TAN
);
11532 if (tanfn
!= NULL_TREE
)
11533 return build_call_expr_loc (loc
, tanfn
, 1, CALL_EXPR_ARG (arg0
, 0));
11536 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11537 if (((fcode0
== BUILT_IN_COS
&& fcode1
== BUILT_IN_SIN
)
11538 || (fcode0
== BUILT_IN_COSF
&& fcode1
== BUILT_IN_SINF
)
11539 || (fcode0
== BUILT_IN_COSL
&& fcode1
== BUILT_IN_SINL
))
11540 && operand_equal_p (CALL_EXPR_ARG (arg0
, 0),
11541 CALL_EXPR_ARG (arg1
, 0), 0))
11543 tree tanfn
= mathfn_built_in (type
, BUILT_IN_TAN
);
11545 if (tanfn
!= NULL_TREE
)
11547 tree tmp
= build_call_expr_loc (loc
, tanfn
, 1,
11548 CALL_EXPR_ARG (arg0
, 0));
11549 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11550 build_real (type
, dconst1
), tmp
);
11554 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11555 NaNs or Infinities. */
11556 if (((fcode0
== BUILT_IN_SIN
&& fcode1
== BUILT_IN_TAN
)
11557 || (fcode0
== BUILT_IN_SINF
&& fcode1
== BUILT_IN_TANF
)
11558 || (fcode0
== BUILT_IN_SINL
&& fcode1
== BUILT_IN_TANL
)))
11560 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11561 tree arg01
= CALL_EXPR_ARG (arg1
, 0);
11563 if (! HONOR_NANS (arg00
)
11564 && ! HONOR_INFINITIES (element_mode (arg00
))
11565 && operand_equal_p (arg00
, arg01
, 0))
11567 tree cosfn
= mathfn_built_in (type
, BUILT_IN_COS
);
11569 if (cosfn
!= NULL_TREE
)
11570 return build_call_expr_loc (loc
, cosfn
, 1, arg00
);
11574 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11575 NaNs or Infinities. */
11576 if (((fcode0
== BUILT_IN_TAN
&& fcode1
== BUILT_IN_SIN
)
11577 || (fcode0
== BUILT_IN_TANF
&& fcode1
== BUILT_IN_SINF
)
11578 || (fcode0
== BUILT_IN_TANL
&& fcode1
== BUILT_IN_SINL
)))
11580 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11581 tree arg01
= CALL_EXPR_ARG (arg1
, 0);
11583 if (! HONOR_NANS (arg00
)
11584 && ! HONOR_INFINITIES (element_mode (arg00
))
11585 && operand_equal_p (arg00
, arg01
, 0))
11587 tree cosfn
= mathfn_built_in (type
, BUILT_IN_COS
);
11589 if (cosfn
!= NULL_TREE
)
11591 tree tmp
= build_call_expr_loc (loc
, cosfn
, 1, arg00
);
11592 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
11593 build_real (type
, dconst1
),
11599 /* Optimize pow(x,c)/x as pow(x,c-1). */
11600 if (fcode0
== BUILT_IN_POW
11601 || fcode0
== BUILT_IN_POWF
11602 || fcode0
== BUILT_IN_POWL
)
11604 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
11605 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
11606 if (TREE_CODE (arg01
) == REAL_CST
11607 && !TREE_OVERFLOW (arg01
)
11608 && operand_equal_p (arg1
, arg00
, 0))
11610 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
11614 c
= TREE_REAL_CST (arg01
);
11615 real_arithmetic (&c
, MINUS_EXPR
, &c
, &dconst1
);
11616 arg
= build_real (type
, c
);
11617 return build_call_expr_loc (loc
, powfn
, 2, arg1
, arg
);
11621 /* Optimize a/root(b/c) into a*root(c/b). */
11622 if (BUILTIN_ROOT_P (fcode1
))
11624 tree rootarg
= CALL_EXPR_ARG (arg1
, 0);
11626 if (TREE_CODE (rootarg
) == RDIV_EXPR
)
11628 tree rootfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
11629 tree b
= TREE_OPERAND (rootarg
, 0);
11630 tree c
= TREE_OPERAND (rootarg
, 1);
11632 tree tmp
= fold_build2_loc (loc
, RDIV_EXPR
, type
, c
, b
);
11634 tmp
= build_call_expr_loc (loc
, rootfn
, 1, tmp
);
11635 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, tmp
);
11639 /* Optimize x/expN(y) into x*expN(-y). */
11640 if (BUILTIN_EXPONENT_P (fcode1
))
11642 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
11643 tree arg
= negate_expr (CALL_EXPR_ARG (arg1
, 0));
11644 arg1
= build_call_expr_loc (loc
,
11646 fold_convert_loc (loc
, type
, arg
));
11647 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11650 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11651 if (fcode1
== BUILT_IN_POW
11652 || fcode1
== BUILT_IN_POWF
11653 || fcode1
== BUILT_IN_POWL
)
11655 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg1
), 0);
11656 tree arg10
= CALL_EXPR_ARG (arg1
, 0);
11657 tree arg11
= CALL_EXPR_ARG (arg1
, 1);
11658 tree neg11
= fold_convert_loc (loc
, type
,
11659 negate_expr (arg11
));
11660 arg1
= build_call_expr_loc (loc
, powfn
, 2, arg10
, neg11
);
11661 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11666 case TRUNC_DIV_EXPR
:
11667 /* Optimize (X & (-A)) / A where A is a power of 2,
11669 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11670 && !TYPE_UNSIGNED (type
) && TREE_CODE (arg1
) == INTEGER_CST
11671 && integer_pow2p (arg1
) && tree_int_cst_sgn (arg1
) > 0)
11673 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (arg1
),
11674 arg1
, TREE_OPERAND (arg0
, 1));
11675 if (sum
&& integer_zerop (sum
)) {
11676 tree pow2
= build_int_cst (integer_type_node
,
11677 wi::exact_log2 (arg1
));
11678 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11679 TREE_OPERAND (arg0
, 0), pow2
);
11685 case FLOOR_DIV_EXPR
:
11686 /* Simplify A / (B << N) where A and B are positive and B is
11687 a power of 2, to A >> (N + log2(B)). */
11688 strict_overflow_p
= false;
11689 if (TREE_CODE (arg1
) == LSHIFT_EXPR
11690 && (TYPE_UNSIGNED (type
)
11691 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
11693 tree sval
= TREE_OPERAND (arg1
, 0);
11694 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
11696 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
11697 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
11698 wi::exact_log2 (sval
));
11700 if (strict_overflow_p
)
11701 fold_overflow_warning (("assuming signed overflow does not "
11702 "occur when simplifying A / (B << N)"),
11703 WARN_STRICT_OVERFLOW_MISC
);
11705 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
11707 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
11708 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
11714 case ROUND_DIV_EXPR
:
11715 case CEIL_DIV_EXPR
:
11716 case EXACT_DIV_EXPR
:
11717 if (integer_zerop (arg1
))
11720 /* Convert -A / -B to A / B when the type is signed and overflow is
11722 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11723 && TREE_CODE (arg0
) == NEGATE_EXPR
11724 && negate_expr_p (arg1
))
11726 if (INTEGRAL_TYPE_P (type
))
11727 fold_overflow_warning (("assuming signed overflow does not occur "
11728 "when distributing negation across "
11730 WARN_STRICT_OVERFLOW_MISC
);
11731 return fold_build2_loc (loc
, code
, type
,
11732 fold_convert_loc (loc
, type
,
11733 TREE_OPERAND (arg0
, 0)),
11734 fold_convert_loc (loc
, type
,
11735 negate_expr (arg1
)));
11737 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
11738 && TREE_CODE (arg1
) == NEGATE_EXPR
11739 && negate_expr_p (arg0
))
11741 if (INTEGRAL_TYPE_P (type
))
11742 fold_overflow_warning (("assuming signed overflow does not occur "
11743 "when distributing negation across "
11745 WARN_STRICT_OVERFLOW_MISC
);
11746 return fold_build2_loc (loc
, code
, type
,
11747 fold_convert_loc (loc
, type
,
11748 negate_expr (arg0
)),
11749 fold_convert_loc (loc
, type
,
11750 TREE_OPERAND (arg1
, 0)));
11753 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11754 operation, EXACT_DIV_EXPR.
11756 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11757 At one time others generated faster code, it's not clear if they do
11758 after the last round to changes to the DIV code in expmed.c. */
11759 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
11760 && multiple_of_p (type
, arg0
, arg1
))
11761 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
, arg0
, arg1
);
11763 strict_overflow_p
= false;
11764 if (TREE_CODE (arg1
) == INTEGER_CST
11765 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11766 &strict_overflow_p
)))
11768 if (strict_overflow_p
)
11769 fold_overflow_warning (("assuming signed overflow does not occur "
11770 "when simplifying division"),
11771 WARN_STRICT_OVERFLOW_MISC
);
11772 return fold_convert_loc (loc
, type
, tem
);
11777 case CEIL_MOD_EXPR
:
11778 case FLOOR_MOD_EXPR
:
11779 case ROUND_MOD_EXPR
:
11780 case TRUNC_MOD_EXPR
:
11781 strict_overflow_p
= false;
11782 if (TREE_CODE (arg1
) == INTEGER_CST
11783 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
11784 &strict_overflow_p
)))
11786 if (strict_overflow_p
)
11787 fold_overflow_warning (("assuming signed overflow does not occur "
11788 "when simplifying modulus"),
11789 WARN_STRICT_OVERFLOW_MISC
);
11790 return fold_convert_loc (loc
, type
, tem
);
11799 /* Since negative shift count is not well-defined,
11800 don't try to compute it in the compiler. */
11801 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
11804 prec
= element_precision (type
);
11806 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11807 if (TREE_CODE (op0
) == code
&& tree_fits_uhwi_p (arg1
)
11808 && tree_to_uhwi (arg1
) < prec
11809 && tree_fits_uhwi_p (TREE_OPERAND (arg0
, 1))
11810 && tree_to_uhwi (TREE_OPERAND (arg0
, 1)) < prec
)
11812 unsigned int low
= (tree_to_uhwi (TREE_OPERAND (arg0
, 1))
11813 + tree_to_uhwi (arg1
));
11815 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11816 being well defined. */
11819 if (code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
11821 else if (TYPE_UNSIGNED (type
) || code
== LSHIFT_EXPR
)
11822 return omit_one_operand_loc (loc
, type
, build_zero_cst (type
),
11823 TREE_OPERAND (arg0
, 0));
11828 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
11829 build_int_cst (TREE_TYPE (arg1
), low
));
11832 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11833 into x & ((unsigned)-1 >> c) for unsigned types. */
11834 if (((code
== LSHIFT_EXPR
&& TREE_CODE (arg0
) == RSHIFT_EXPR
)
11835 || (TYPE_UNSIGNED (type
)
11836 && code
== RSHIFT_EXPR
&& TREE_CODE (arg0
) == LSHIFT_EXPR
))
11837 && tree_fits_uhwi_p (arg1
)
11838 && tree_to_uhwi (arg1
) < prec
11839 && tree_fits_uhwi_p (TREE_OPERAND (arg0
, 1))
11840 && tree_to_uhwi (TREE_OPERAND (arg0
, 1)) < prec
)
11842 HOST_WIDE_INT low0
= tree_to_uhwi (TREE_OPERAND (arg0
, 1));
11843 HOST_WIDE_INT low1
= tree_to_uhwi (arg1
);
11849 arg00
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
11851 lshift
= build_minus_one_cst (type
);
11852 lshift
= const_binop (code
, lshift
, arg1
);
11854 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
, arg00
, lshift
);
11858 /* If we have a rotate of a bit operation with the rotate count and
11859 the second operand of the bit operation both constant,
11860 permute the two operations. */
11861 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11862 && (TREE_CODE (arg0
) == BIT_AND_EXPR
11863 || TREE_CODE (arg0
) == BIT_IOR_EXPR
11864 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
11865 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11866 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
11867 fold_build2_loc (loc
, code
, type
,
11868 TREE_OPERAND (arg0
, 0), arg1
),
11869 fold_build2_loc (loc
, code
, type
,
11870 TREE_OPERAND (arg0
, 1), arg1
));
11872 /* Two consecutive rotates adding up to the some integer
11873 multiple of the precision of the type can be ignored. */
11874 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
11875 && TREE_CODE (arg0
) == RROTATE_EXPR
11876 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
11877 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
11879 return TREE_OPERAND (arg0
, 0);
11881 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11882 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11883 if the latter can be further optimized. */
11884 if ((code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
11885 && TREE_CODE (arg0
) == BIT_AND_EXPR
11886 && TREE_CODE (arg1
) == INTEGER_CST
11887 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
11889 tree mask
= fold_build2_loc (loc
, code
, type
,
11890 fold_convert_loc (loc
, type
,
11891 TREE_OPERAND (arg0
, 1)),
11893 tree shift
= fold_build2_loc (loc
, code
, type
,
11894 fold_convert_loc (loc
, type
,
11895 TREE_OPERAND (arg0
, 0)),
11897 tem
= fold_binary_loc (loc
, BIT_AND_EXPR
, type
, shift
, mask
);
11905 tem
= fold_minmax (loc
, MIN_EXPR
, type
, arg0
, arg1
);
11911 tem
= fold_minmax (loc
, MAX_EXPR
, type
, arg0
, arg1
);
11916 case TRUTH_ANDIF_EXPR
:
11917 /* Note that the operands of this must be ints
11918 and their values must be 0 or 1.
11919 ("true" is a fixed value perhaps depending on the language.) */
11920 /* If first arg is constant zero, return it. */
11921 if (integer_zerop (arg0
))
11922 return fold_convert_loc (loc
, type
, arg0
);
11923 case TRUTH_AND_EXPR
:
11924 /* If either arg is constant true, drop it. */
11925 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11926 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11927 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
11928 /* Preserve sequence points. */
11929 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
11930 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11931 /* If second arg is constant zero, result is zero, but first arg
11932 must be evaluated. */
11933 if (integer_zerop (arg1
))
11934 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11935 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11936 case will be handled here. */
11937 if (integer_zerop (arg0
))
11938 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
11940 /* !X && X is always false. */
11941 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11942 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11943 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
11944 /* X && !X is always false. */
11945 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
11946 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
11947 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
11949 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11950 means A >= Y && A != MAX, but in this case we know that
11953 if (!TREE_SIDE_EFFECTS (arg0
)
11954 && !TREE_SIDE_EFFECTS (arg1
))
11956 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
11957 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
11958 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
11960 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
11961 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
11962 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
11965 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
11971 case TRUTH_ORIF_EXPR
:
11972 /* Note that the operands of this must be ints
11973 and their values must be 0 or true.
11974 ("true" is a fixed value perhaps depending on the language.) */
11975 /* If first arg is constant true, return it. */
11976 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11977 return fold_convert_loc (loc
, type
, arg0
);
11978 case TRUTH_OR_EXPR
:
11979 /* If either arg is constant zero, drop it. */
11980 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
11981 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
11982 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
11983 /* Preserve sequence points. */
11984 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
11985 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
11986 /* If second arg is constant true, result is true, but we must
11987 evaluate first arg. */
11988 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
11989 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
11990 /* Likewise for first arg, but note this only occurs here for
11992 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
11993 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
11995 /* !X || X is always true. */
11996 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
11997 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
11998 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
11999 /* X || !X is always true. */
12000 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12001 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12002 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12004 /* (X && !Y) || (!X && Y) is X ^ Y */
12005 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
12006 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
12008 tree a0
, a1
, l0
, l1
, n0
, n1
;
12010 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
12011 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
12013 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
12014 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
12016 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
12017 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
12019 if ((operand_equal_p (n0
, a0
, 0)
12020 && operand_equal_p (n1
, a1
, 0))
12021 || (operand_equal_p (n0
, a1
, 0)
12022 && operand_equal_p (n1
, a0
, 0)))
12023 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
12026 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
12032 case TRUTH_XOR_EXPR
:
12033 /* If the second arg is constant zero, drop it. */
12034 if (integer_zerop (arg1
))
12035 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12036 /* If the second arg is constant true, this is a logical inversion. */
12037 if (integer_onep (arg1
))
12039 tem
= invert_truthvalue_loc (loc
, arg0
);
12040 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
12042 /* Identical arguments cancel to zero. */
12043 if (operand_equal_p (arg0
, arg1
, 0))
12044 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12046 /* !X ^ X is always true. */
12047 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
12048 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
12049 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
12051 /* X ^ !X is always true. */
12052 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
12053 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
12054 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12063 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12064 if (tem
!= NULL_TREE
)
12067 /* bool_var != 0 becomes bool_var. */
12068 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12069 && code
== NE_EXPR
)
12070 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12072 /* bool_var == 1 becomes bool_var. */
12073 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12074 && code
== EQ_EXPR
)
12075 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12077 /* bool_var != 1 becomes !bool_var. */
12078 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
12079 && code
== NE_EXPR
)
12080 return fold_convert_loc (loc
, type
,
12081 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12082 TREE_TYPE (arg0
), arg0
));
12084 /* bool_var == 0 becomes !bool_var. */
12085 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
12086 && code
== EQ_EXPR
)
12087 return fold_convert_loc (loc
, type
,
12088 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
12089 TREE_TYPE (arg0
), arg0
));
12091 /* !exp != 0 becomes !exp */
12092 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
12093 && code
== NE_EXPR
)
12094 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
12096 /* If this is an equality comparison of the address of two non-weak,
12097 unaliased symbols neither of which are extern (since we do not
12098 have access to attributes for externs), then we know the result. */
12099 if (TREE_CODE (arg0
) == ADDR_EXPR
12100 && DECL_P (TREE_OPERAND (arg0
, 0))
12101 && TREE_CODE (arg1
) == ADDR_EXPR
12102 && DECL_P (TREE_OPERAND (arg1
, 0)))
12106 if (decl_in_symtab_p (TREE_OPERAND (arg0
, 0))
12107 && decl_in_symtab_p (TREE_OPERAND (arg1
, 0)))
12108 equal
= symtab_node::get_create (TREE_OPERAND (arg0
, 0))
12109 ->equal_address_to (symtab_node::get_create
12110 (TREE_OPERAND (arg1
, 0)));
12112 equal
= TREE_OPERAND (arg0
, 0) == TREE_OPERAND (arg1
, 0);
12114 return constant_boolean_node (equal
12115 ? code
== EQ_EXPR
: code
!= EQ_EXPR
,
12119 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12120 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12121 && TREE_CODE (arg1
) == INTEGER_CST
12122 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12123 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0),
12124 fold_build2_loc (loc
, BIT_XOR_EXPR
, TREE_TYPE (arg0
),
12125 fold_convert_loc (loc
,
12128 TREE_OPERAND (arg0
, 1)));
12130 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12131 if ((TREE_CODE (arg0
) == PLUS_EXPR
12132 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
12133 || TREE_CODE (arg0
) == MINUS_EXPR
)
12134 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
12137 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
12138 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
12140 tree val
= TREE_OPERAND (arg0
, 1);
12141 return omit_two_operands_loc (loc
, type
,
12142 fold_build2_loc (loc
, code
, type
,
12144 build_int_cst (TREE_TYPE (val
),
12146 TREE_OPERAND (arg0
, 0), arg1
);
12149 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12150 if (TREE_CODE (arg0
) == MINUS_EXPR
12151 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
12152 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
12155 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
12157 return omit_two_operands_loc (loc
, type
,
12159 ? boolean_true_node
: boolean_false_node
,
12160 TREE_OPERAND (arg0
, 1), arg1
);
12163 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12164 if (TREE_CODE (arg0
) == ABS_EXPR
12165 && (integer_zerop (arg1
) || real_zerop (arg1
)))
12166 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), arg1
);
12168 /* If this is an EQ or NE comparison with zero and ARG0 is
12169 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12170 two operations, but the latter can be done in one less insn
12171 on machines that have only two-operand insns or on which a
12172 constant cannot be the first operand. */
12173 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12174 && integer_zerop (arg1
))
12176 tree arg00
= TREE_OPERAND (arg0
, 0);
12177 tree arg01
= TREE_OPERAND (arg0
, 1);
12178 if (TREE_CODE (arg00
) == LSHIFT_EXPR
12179 && integer_onep (TREE_OPERAND (arg00
, 0)))
12181 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
12182 arg01
, TREE_OPERAND (arg00
, 1));
12183 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12184 build_int_cst (TREE_TYPE (arg0
), 1));
12185 return fold_build2_loc (loc
, code
, type
,
12186 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
12189 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
12190 && integer_onep (TREE_OPERAND (arg01
, 0)))
12192 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
12193 arg00
, TREE_OPERAND (arg01
, 1));
12194 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
12195 build_int_cst (TREE_TYPE (arg0
), 1));
12196 return fold_build2_loc (loc
, code
, type
,
12197 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
12202 /* If this is an NE or EQ comparison of zero against the result of a
12203 signed MOD operation whose second operand is a power of 2, make
12204 the MOD operation unsigned since it is simpler and equivalent. */
12205 if (integer_zerop (arg1
)
12206 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
12207 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
12208 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
12209 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
12210 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
12211 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12213 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
12214 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
12215 fold_convert_loc (loc
, newtype
,
12216 TREE_OPERAND (arg0
, 0)),
12217 fold_convert_loc (loc
, newtype
,
12218 TREE_OPERAND (arg0
, 1)));
12220 return fold_build2_loc (loc
, code
, type
, newmod
,
12221 fold_convert_loc (loc
, newtype
, arg1
));
12224 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12225 C1 is a valid shift constant, and C2 is a power of two, i.e.
12227 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12228 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
12229 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
12231 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12232 && integer_zerop (arg1
))
12234 tree itype
= TREE_TYPE (arg0
);
12235 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
12236 prec
= TYPE_PRECISION (itype
);
12238 /* Check for a valid shift count. */
12239 if (wi::ltu_p (arg001
, prec
))
12241 tree arg01
= TREE_OPERAND (arg0
, 1);
12242 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12243 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
12244 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12245 can be rewritten as (X & (C2 << C1)) != 0. */
12246 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
12248 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
12249 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
12250 return fold_build2_loc (loc
, code
, type
, tem
,
12251 fold_convert_loc (loc
, itype
, arg1
));
12253 /* Otherwise, for signed (arithmetic) shifts,
12254 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12255 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12256 else if (!TYPE_UNSIGNED (itype
))
12257 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
12258 arg000
, build_int_cst (itype
, 0));
12259 /* Otherwise, of unsigned (logical) shifts,
12260 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12261 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12263 return omit_one_operand_loc (loc
, type
,
12264 code
== EQ_EXPR
? integer_one_node
12265 : integer_zero_node
,
12270 /* If we have (A & C) == C where C is a power of 2, convert this into
12271 (A & C) != 0. Similarly for NE_EXPR. */
12272 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12273 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12274 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
12275 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12276 arg0
, fold_convert_loc (loc
, TREE_TYPE (arg0
),
12277 integer_zero_node
));
12279 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12280 bit, then fold the expression into A < 0 or A >= 0. */
12281 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
, type
);
12285 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12286 Similarly for NE_EXPR. */
12287 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12288 && TREE_CODE (arg1
) == INTEGER_CST
12289 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12291 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
12292 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
12293 TREE_OPERAND (arg0
, 1));
12295 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12296 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
12298 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
12299 if (integer_nonzerop (dandnotc
))
12300 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
12303 /* If this is a comparison of a field, we may be able to simplify it. */
12304 if ((TREE_CODE (arg0
) == COMPONENT_REF
12305 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
12306 /* Handle the constant case even without -O
12307 to make sure the warnings are given. */
12308 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
12310 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
12315 /* Optimize comparisons of strlen vs zero to a compare of the
12316 first character of the string vs zero. To wit,
12317 strlen(ptr) == 0 => *ptr == 0
12318 strlen(ptr) != 0 => *ptr != 0
12319 Other cases should reduce to one of these two (or a constant)
12320 due to the return value of strlen being unsigned. */
12321 if (TREE_CODE (arg0
) == CALL_EXPR
12322 && integer_zerop (arg1
))
12324 tree fndecl
= get_callee_fndecl (arg0
);
12327 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
12328 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
12329 && call_expr_nargs (arg0
) == 1
12330 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
12332 tree iref
= build_fold_indirect_ref_loc (loc
,
12333 CALL_EXPR_ARG (arg0
, 0));
12334 return fold_build2_loc (loc
, code
, type
, iref
,
12335 build_int_cst (TREE_TYPE (iref
), 0));
12339 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12340 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12341 if (TREE_CODE (arg0
) == RSHIFT_EXPR
12342 && integer_zerop (arg1
)
12343 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
12345 tree arg00
= TREE_OPERAND (arg0
, 0);
12346 tree arg01
= TREE_OPERAND (arg0
, 1);
12347 tree itype
= TREE_TYPE (arg00
);
12348 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
12350 if (TYPE_UNSIGNED (itype
))
12352 itype
= signed_type_for (itype
);
12353 arg00
= fold_convert_loc (loc
, itype
, arg00
);
12355 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
12356 type
, arg00
, build_zero_cst (itype
));
12360 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12361 (X & C) == 0 when C is a single bit. */
12362 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12363 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
12364 && integer_zerop (arg1
)
12365 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
12367 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
12368 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
12369 TREE_OPERAND (arg0
, 1));
12370 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
12372 fold_convert_loc (loc
, TREE_TYPE (arg0
),
12376 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12377 constant C is a power of two, i.e. a single bit. */
12378 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12379 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
12380 && integer_zerop (arg1
)
12381 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12382 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12383 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12385 tree arg00
= TREE_OPERAND (arg0
, 0);
12386 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12387 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
12390 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12391 when is C is a power of two, i.e. a single bit. */
12392 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12393 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
12394 && integer_zerop (arg1
)
12395 && integer_pow2p (TREE_OPERAND (arg0
, 1))
12396 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
12397 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
12399 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
12400 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
12401 arg000
, TREE_OPERAND (arg0
, 1));
12402 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
12403 tem
, build_int_cst (TREE_TYPE (tem
), 0));
12406 if (integer_zerop (arg1
)
12407 && tree_expr_nonzero_p (arg0
))
12409 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
12410 return omit_one_operand_loc (loc
, type
, res
, arg0
);
12413 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12414 if (TREE_CODE (arg0
) == BIT_AND_EXPR
12415 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
12417 tree arg00
= TREE_OPERAND (arg0
, 0);
12418 tree arg01
= TREE_OPERAND (arg0
, 1);
12419 tree arg10
= TREE_OPERAND (arg1
, 0);
12420 tree arg11
= TREE_OPERAND (arg1
, 1);
12421 tree itype
= TREE_TYPE (arg0
);
12423 if (operand_equal_p (arg01
, arg11
, 0))
12424 return fold_build2_loc (loc
, code
, type
,
12425 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12426 fold_build2_loc (loc
,
12427 BIT_XOR_EXPR
, itype
,
12430 build_zero_cst (itype
));
12432 if (operand_equal_p (arg01
, arg10
, 0))
12433 return fold_build2_loc (loc
, code
, type
,
12434 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12435 fold_build2_loc (loc
,
12436 BIT_XOR_EXPR
, itype
,
12439 build_zero_cst (itype
));
12441 if (operand_equal_p (arg00
, arg11
, 0))
12442 return fold_build2_loc (loc
, code
, type
,
12443 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12444 fold_build2_loc (loc
,
12445 BIT_XOR_EXPR
, itype
,
12448 build_zero_cst (itype
));
12450 if (operand_equal_p (arg00
, arg10
, 0))
12451 return fold_build2_loc (loc
, code
, type
,
12452 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
12453 fold_build2_loc (loc
,
12454 BIT_XOR_EXPR
, itype
,
12457 build_zero_cst (itype
));
12460 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
12461 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
12463 tree arg00
= TREE_OPERAND (arg0
, 0);
12464 tree arg01
= TREE_OPERAND (arg0
, 1);
12465 tree arg10
= TREE_OPERAND (arg1
, 0);
12466 tree arg11
= TREE_OPERAND (arg1
, 1);
12467 tree itype
= TREE_TYPE (arg0
);
12469 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12470 operand_equal_p guarantees no side-effects so we don't need
12471 to use omit_one_operand on Z. */
12472 if (operand_equal_p (arg01
, arg11
, 0))
12473 return fold_build2_loc (loc
, code
, type
, arg00
,
12474 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12476 if (operand_equal_p (arg01
, arg10
, 0))
12477 return fold_build2_loc (loc
, code
, type
, arg00
,
12478 fold_convert_loc (loc
, TREE_TYPE (arg00
),
12480 if (operand_equal_p (arg00
, arg11
, 0))
12481 return fold_build2_loc (loc
, code
, type
, arg01
,
12482 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12484 if (operand_equal_p (arg00
, arg10
, 0))
12485 return fold_build2_loc (loc
, code
, type
, arg01
,
12486 fold_convert_loc (loc
, TREE_TYPE (arg01
),
12489 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12490 if (TREE_CODE (arg01
) == INTEGER_CST
12491 && TREE_CODE (arg11
) == INTEGER_CST
)
12493 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
12494 fold_convert_loc (loc
, itype
, arg11
));
12495 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
12496 return fold_build2_loc (loc
, code
, type
, tem
,
12497 fold_convert_loc (loc
, itype
, arg10
));
12501 /* Attempt to simplify equality/inequality comparisons of complex
12502 values. Only lower the comparison if the result is known or
12503 can be simplified to a single scalar comparison. */
12504 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
12505 || TREE_CODE (arg0
) == COMPLEX_CST
)
12506 && (TREE_CODE (arg1
) == COMPLEX_EXPR
12507 || TREE_CODE (arg1
) == COMPLEX_CST
))
12509 tree real0
, imag0
, real1
, imag1
;
12512 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
12514 real0
= TREE_OPERAND (arg0
, 0);
12515 imag0
= TREE_OPERAND (arg0
, 1);
12519 real0
= TREE_REALPART (arg0
);
12520 imag0
= TREE_IMAGPART (arg0
);
12523 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
12525 real1
= TREE_OPERAND (arg1
, 0);
12526 imag1
= TREE_OPERAND (arg1
, 1);
12530 real1
= TREE_REALPART (arg1
);
12531 imag1
= TREE_IMAGPART (arg1
);
12534 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
12535 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
12537 if (integer_zerop (rcond
))
12539 if (code
== EQ_EXPR
)
12540 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12542 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
12546 if (code
== NE_EXPR
)
12547 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12549 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
12553 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
12554 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
12556 if (integer_zerop (icond
))
12558 if (code
== EQ_EXPR
)
12559 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
12561 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
12565 if (code
== NE_EXPR
)
12566 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
12568 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
12579 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
12580 if (tem
!= NULL_TREE
)
12583 /* Transform comparisons of the form X +- C CMP X. */
12584 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
12585 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
12586 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
12587 && !HONOR_SNANS (arg0
))
12588 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
12589 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
12591 tree arg01
= TREE_OPERAND (arg0
, 1);
12592 enum tree_code code0
= TREE_CODE (arg0
);
12595 if (TREE_CODE (arg01
) == REAL_CST
)
12596 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
12598 is_positive
= tree_int_cst_sgn (arg01
);
12600 /* (X - c) > X becomes false. */
12601 if (code
== GT_EXPR
12602 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12603 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12605 if (TREE_CODE (arg01
) == INTEGER_CST
12606 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12607 fold_overflow_warning (("assuming signed overflow does not "
12608 "occur when assuming that (X - c) > X "
12609 "is always false"),
12610 WARN_STRICT_OVERFLOW_ALL
);
12611 return constant_boolean_node (0, type
);
12614 /* Likewise (X + c) < X becomes false. */
12615 if (code
== LT_EXPR
12616 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12617 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12619 if (TREE_CODE (arg01
) == INTEGER_CST
12620 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12621 fold_overflow_warning (("assuming signed overflow does not "
12622 "occur when assuming that "
12623 "(X + c) < X is always false"),
12624 WARN_STRICT_OVERFLOW_ALL
);
12625 return constant_boolean_node (0, type
);
12628 /* Convert (X - c) <= X to true. */
12629 if (!HONOR_NANS (arg1
)
12631 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
12632 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
12634 if (TREE_CODE (arg01
) == INTEGER_CST
12635 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12636 fold_overflow_warning (("assuming signed overflow does not "
12637 "occur when assuming that "
12638 "(X - c) <= X is always true"),
12639 WARN_STRICT_OVERFLOW_ALL
);
12640 return constant_boolean_node (1, type
);
12643 /* Convert (X + c) >= X to true. */
12644 if (!HONOR_NANS (arg1
)
12646 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
12647 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
12649 if (TREE_CODE (arg01
) == INTEGER_CST
12650 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12651 fold_overflow_warning (("assuming signed overflow does not "
12652 "occur when assuming that "
12653 "(X + c) >= X is always true"),
12654 WARN_STRICT_OVERFLOW_ALL
);
12655 return constant_boolean_node (1, type
);
12658 if (TREE_CODE (arg01
) == INTEGER_CST
)
12660 /* Convert X + c > X and X - c < X to true for integers. */
12661 if (code
== GT_EXPR
12662 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
12663 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
12665 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12666 fold_overflow_warning (("assuming signed overflow does "
12667 "not occur when assuming that "
12668 "(X + c) > X is always true"),
12669 WARN_STRICT_OVERFLOW_ALL
);
12670 return constant_boolean_node (1, type
);
12673 if (code
== LT_EXPR
12674 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
12675 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
12677 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12678 fold_overflow_warning (("assuming signed overflow does "
12679 "not occur when assuming that "
12680 "(X - c) < X is always true"),
12681 WARN_STRICT_OVERFLOW_ALL
);
12682 return constant_boolean_node (1, type
);
12685 /* Convert X + c <= X and X - c >= X to false for integers. */
12686 if (code
== LE_EXPR
12687 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
12688 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
12690 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12691 fold_overflow_warning (("assuming signed overflow does "
12692 "not occur when assuming that "
12693 "(X + c) <= X is always false"),
12694 WARN_STRICT_OVERFLOW_ALL
);
12695 return constant_boolean_node (0, type
);
12698 if (code
== GE_EXPR
12699 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
12700 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
12702 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
12703 fold_overflow_warning (("assuming signed overflow does "
12704 "not occur when assuming that "
12705 "(X - c) >= X is always false"),
12706 WARN_STRICT_OVERFLOW_ALL
);
12707 return constant_boolean_node (0, type
);
12712 /* Comparisons with the highest or lowest possible integer of
12713 the specified precision will have known values. */
12715 tree arg1_type
= TREE_TYPE (arg1
);
12716 unsigned int prec
= TYPE_PRECISION (arg1_type
);
12718 if (TREE_CODE (arg1
) == INTEGER_CST
12719 && (INTEGRAL_TYPE_P (arg1_type
) || POINTER_TYPE_P (arg1_type
)))
12721 wide_int max
= wi::max_value (arg1_type
);
12722 wide_int signed_max
= wi::max_value (prec
, SIGNED
);
12723 wide_int min
= wi::min_value (arg1_type
);
12725 if (wi::eq_p (arg1
, max
))
12729 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12732 return fold_build2_loc (loc
, EQ_EXPR
, type
, op0
, op1
);
12735 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12738 return fold_build2_loc (loc
, NE_EXPR
, type
, op0
, op1
);
12740 /* The GE_EXPR and LT_EXPR cases above are not normally
12741 reached because of previous transformations. */
12746 else if (wi::eq_p (arg1
, max
- 1))
12750 arg1
= const_binop (PLUS_EXPR
, arg1
,
12751 build_int_cst (TREE_TYPE (arg1
), 1));
12752 return fold_build2_loc (loc
, EQ_EXPR
, type
,
12753 fold_convert_loc (loc
,
12754 TREE_TYPE (arg1
), arg0
),
12757 arg1
= const_binop (PLUS_EXPR
, arg1
,
12758 build_int_cst (TREE_TYPE (arg1
), 1));
12759 return fold_build2_loc (loc
, NE_EXPR
, type
,
12760 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12766 else if (wi::eq_p (arg1
, min
))
12770 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
12773 return fold_build2_loc (loc
, EQ_EXPR
, type
, op0
, op1
);
12776 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
12779 return fold_build2_loc (loc
, NE_EXPR
, type
, op0
, op1
);
12784 else if (wi::eq_p (arg1
, min
+ 1))
12788 arg1
= const_binop (MINUS_EXPR
, arg1
,
12789 build_int_cst (TREE_TYPE (arg1
), 1));
12790 return fold_build2_loc (loc
, NE_EXPR
, type
,
12791 fold_convert_loc (loc
,
12792 TREE_TYPE (arg1
), arg0
),
12795 arg1
= const_binop (MINUS_EXPR
, arg1
,
12796 build_int_cst (TREE_TYPE (arg1
), 1));
12797 return fold_build2_loc (loc
, EQ_EXPR
, type
,
12798 fold_convert_loc (loc
, TREE_TYPE (arg1
),
12805 else if (wi::eq_p (arg1
, signed_max
)
12806 && TYPE_UNSIGNED (arg1_type
)
12807 /* We will flip the signedness of the comparison operator
12808 associated with the mode of arg1, so the sign bit is
12809 specified by this mode. Check that arg1 is the signed
12810 max associated with this sign bit. */
12811 && prec
== GET_MODE_PRECISION (TYPE_MODE (arg1_type
))
12812 /* signed_type does not work on pointer types. */
12813 && INTEGRAL_TYPE_P (arg1_type
))
12815 /* The following case also applies to X < signed_max+1
12816 and X >= signed_max+1 because previous transformations. */
12817 if (code
== LE_EXPR
|| code
== GT_EXPR
)
12819 tree st
= signed_type_for (arg1_type
);
12820 return fold_build2_loc (loc
,
12821 code
== LE_EXPR
? GE_EXPR
: LT_EXPR
,
12822 type
, fold_convert_loc (loc
, st
, arg0
),
12823 build_int_cst (st
, 0));
12829 /* If we are comparing an ABS_EXPR with a constant, we can
12830 convert all the cases into explicit comparisons, but they may
12831 well not be faster than doing the ABS and one comparison.
12832 But ABS (X) <= C is a range comparison, which becomes a subtraction
12833 and a comparison, and is probably faster. */
12834 if (code
== LE_EXPR
12835 && TREE_CODE (arg1
) == INTEGER_CST
12836 && TREE_CODE (arg0
) == ABS_EXPR
12837 && ! TREE_SIDE_EFFECTS (arg0
)
12838 && (0 != (tem
= negate_expr (arg1
)))
12839 && TREE_CODE (tem
) == INTEGER_CST
12840 && !TREE_OVERFLOW (tem
))
12841 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
12842 build2 (GE_EXPR
, type
,
12843 TREE_OPERAND (arg0
, 0), tem
),
12844 build2 (LE_EXPR
, type
,
12845 TREE_OPERAND (arg0
, 0), arg1
));
12847 /* Convert ABS_EXPR<x> >= 0 to true. */
12848 strict_overflow_p
= false;
12849 if (code
== GE_EXPR
12850 && (integer_zerop (arg1
)
12851 || (! HONOR_NANS (arg0
)
12852 && real_zerop (arg1
)))
12853 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12855 if (strict_overflow_p
)
12856 fold_overflow_warning (("assuming signed overflow does not occur "
12857 "when simplifying comparison of "
12858 "absolute value and zero"),
12859 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12860 return omit_one_operand_loc (loc
, type
,
12861 constant_boolean_node (true, type
),
12865 /* Convert ABS_EXPR<x> < 0 to false. */
12866 strict_overflow_p
= false;
12867 if (code
== LT_EXPR
12868 && (integer_zerop (arg1
) || real_zerop (arg1
))
12869 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
12871 if (strict_overflow_p
)
12872 fold_overflow_warning (("assuming signed overflow does not occur "
12873 "when simplifying comparison of "
12874 "absolute value and zero"),
12875 WARN_STRICT_OVERFLOW_CONDITIONAL
);
12876 return omit_one_operand_loc (loc
, type
,
12877 constant_boolean_node (false, type
),
12881 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12882 and similarly for >= into !=. */
12883 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12884 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12885 && TREE_CODE (arg1
) == LSHIFT_EXPR
12886 && integer_onep (TREE_OPERAND (arg1
, 0)))
12887 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12888 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12889 TREE_OPERAND (arg1
, 1)),
12890 build_zero_cst (TREE_TYPE (arg0
)));
12892 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12893 otherwise Y might be >= # of bits in X's type and thus e.g.
12894 (unsigned char) (1 << Y) for Y 15 might be 0.
12895 If the cast is widening, then 1 << Y should have unsigned type,
12896 otherwise if Y is number of bits in the signed shift type minus 1,
12897 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12898 31 might be 0xffffffff80000000. */
12899 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
12900 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
12901 && CONVERT_EXPR_P (arg1
)
12902 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
12903 && (element_precision (TREE_TYPE (arg1
))
12904 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
12905 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
12906 || (element_precision (TREE_TYPE (arg1
))
12907 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
12908 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
12910 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
12911 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
12912 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
12913 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
12914 build_zero_cst (TREE_TYPE (arg0
)));
12919 case UNORDERED_EXPR
:
12927 if (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
12929 t1
= fold_relational_const (code
, type
, arg0
, arg1
);
12930 if (t1
!= NULL_TREE
)
12934 /* If the first operand is NaN, the result is constant. */
12935 if (TREE_CODE (arg0
) == REAL_CST
12936 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
))
12937 && (code
!= LTGT_EXPR
|| ! flag_trapping_math
))
12939 t1
= (code
== ORDERED_EXPR
|| code
== LTGT_EXPR
)
12940 ? integer_zero_node
12941 : integer_one_node
;
12942 return omit_one_operand_loc (loc
, type
, t1
, arg1
);
12945 /* If the second operand is NaN, the result is constant. */
12946 if (TREE_CODE (arg1
) == REAL_CST
12947 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
12948 && (code
!= LTGT_EXPR
|| ! flag_trapping_math
))
12950 t1
= (code
== ORDERED_EXPR
|| code
== LTGT_EXPR
)
12951 ? integer_zero_node
12952 : integer_one_node
;
12953 return omit_one_operand_loc (loc
, type
, t1
, arg0
);
12956 /* Simplify unordered comparison of something with itself. */
12957 if ((code
== UNLE_EXPR
|| code
== UNGE_EXPR
|| code
== UNEQ_EXPR
)
12958 && operand_equal_p (arg0
, arg1
, 0))
12959 return constant_boolean_node (1, type
);
12961 if (code
== LTGT_EXPR
12962 && !flag_trapping_math
12963 && operand_equal_p (arg0
, arg1
, 0))
12964 return constant_boolean_node (0, type
);
12966 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12968 tree targ0
= strip_float_extensions (arg0
);
12969 tree targ1
= strip_float_extensions (arg1
);
12970 tree newtype
= TREE_TYPE (targ0
);
12972 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
12973 newtype
= TREE_TYPE (targ1
);
12975 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
12976 return fold_build2_loc (loc
, code
, type
,
12977 fold_convert_loc (loc
, newtype
, targ0
),
12978 fold_convert_loc (loc
, newtype
, targ1
));
12983 case COMPOUND_EXPR
:
12984 /* When pedantic, a compound expression can be neither an lvalue
12985 nor an integer constant expression. */
12986 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
12988 /* Don't let (0, 0) be null pointer constant. */
12989 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
12990 : fold_convert_loc (loc
, type
, arg1
);
12991 return pedantic_non_lvalue_loc (loc
, tem
);
12994 /* An ASSERT_EXPR should never be passed to fold_binary. */
12995 gcc_unreachable ();
12999 } /* switch (code) */
13002 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13003 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13007 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
13009 switch (TREE_CODE (*tp
))
13015 *walk_subtrees
= 0;
13017 /* ... fall through ... */
13024 /* Return whether the sub-tree ST contains a label which is accessible from
13025 outside the sub-tree. */
13028 contains_label_p (tree st
)
13031 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
13034 /* Fold a ternary expression of code CODE and type TYPE with operands
13035 OP0, OP1, and OP2. Return the folded expression if folding is
13036 successful. Otherwise, return NULL_TREE. */
13039 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
13040 tree op0
, tree op1
, tree op2
)
13043 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
13044 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13046 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
13047 && TREE_CODE_LENGTH (code
) == 3);
13049 /* If this is a commutative operation, and OP0 is a constant, move it
13050 to OP1 to reduce the number of tests below. */
13051 if (commutative_ternary_tree_code (code
)
13052 && tree_swap_operands_p (op0
, op1
, true))
13053 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
13055 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
13059 /* Strip any conversions that don't change the mode. This is safe
13060 for every expression, except for a comparison expression because
13061 its signedness is derived from its operands. So, in the latter
13062 case, only strip conversions that don't change the signedness.
13064 Note that this is done as an internal manipulation within the
13065 constant folder, in order to find the simplest representation of
13066 the arguments so that their form can be studied. In any cases,
13067 the appropriate type conversions should be put back in the tree
13068 that will get out of the constant folder. */
13089 case COMPONENT_REF
:
13090 if (TREE_CODE (arg0
) == CONSTRUCTOR
13091 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
13093 unsigned HOST_WIDE_INT idx
;
13095 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
13102 case VEC_COND_EXPR
:
13103 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13104 so all simple results must be passed through pedantic_non_lvalue. */
13105 if (TREE_CODE (arg0
) == INTEGER_CST
)
13107 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
13108 tem
= integer_zerop (arg0
) ? op2
: op1
;
13109 /* Only optimize constant conditions when the selected branch
13110 has the same type as the COND_EXPR. This avoids optimizing
13111 away "c ? x : throw", where the throw has a void type.
13112 Avoid throwing away that operand which contains label. */
13113 if ((!TREE_SIDE_EFFECTS (unused_op
)
13114 || !contains_label_p (unused_op
))
13115 && (! VOID_TYPE_P (TREE_TYPE (tem
))
13116 || VOID_TYPE_P (type
)))
13117 return pedantic_non_lvalue_loc (loc
, tem
);
13120 else if (TREE_CODE (arg0
) == VECTOR_CST
)
13122 if ((TREE_CODE (arg1
) == VECTOR_CST
13123 || TREE_CODE (arg1
) == CONSTRUCTOR
)
13124 && (TREE_CODE (arg2
) == VECTOR_CST
13125 || TREE_CODE (arg2
) == CONSTRUCTOR
))
13127 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
13128 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
13129 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
13130 for (i
= 0; i
< nelts
; i
++)
13132 tree val
= VECTOR_CST_ELT (arg0
, i
);
13133 if (integer_all_onesp (val
))
13135 else if (integer_zerop (val
))
13136 sel
[i
] = nelts
+ i
;
13137 else /* Currently unreachable. */
13140 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
13141 if (t
!= NULL_TREE
)
13146 /* If we have A op B ? A : C, we may be able to convert this to a
13147 simpler expression, depending on the operation and the values
13148 of B and C. Signed zeros prevent all of these transformations,
13149 for reasons given above each one.
13151 Also try swapping the arguments and inverting the conditional. */
13152 if (COMPARISON_CLASS_P (arg0
)
13153 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
13154 arg1
, TREE_OPERAND (arg0
, 1))
13155 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
13157 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
13162 if (COMPARISON_CLASS_P (arg0
)
13163 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
13165 TREE_OPERAND (arg0
, 1))
13166 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
13168 location_t loc0
= expr_location_or (arg0
, loc
);
13169 tem
= fold_invert_truthvalue (loc0
, arg0
);
13170 if (tem
&& COMPARISON_CLASS_P (tem
))
13172 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
13178 /* If the second operand is simpler than the third, swap them
13179 since that produces better jump optimization results. */
13180 if (truth_value_p (TREE_CODE (arg0
))
13181 && tree_swap_operands_p (op1
, op2
, false))
13183 location_t loc0
= expr_location_or (arg0
, loc
);
13184 /* See if this can be inverted. If it can't, possibly because
13185 it was a floating-point inequality comparison, don't do
13187 tem
= fold_invert_truthvalue (loc0
, arg0
);
13189 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
13192 /* Convert A ? 1 : 0 to simply A. */
13193 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
13194 : (integer_onep (op1
)
13195 && !VECTOR_TYPE_P (type
)))
13196 && integer_zerop (op2
)
13197 /* If we try to convert OP0 to our type, the
13198 call to fold will try to move the conversion inside
13199 a COND, which will recurse. In that case, the COND_EXPR
13200 is probably the best choice, so leave it alone. */
13201 && type
== TREE_TYPE (arg0
))
13202 return pedantic_non_lvalue_loc (loc
, arg0
);
13204 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13205 over COND_EXPR in cases such as floating point comparisons. */
13206 if (integer_zerop (op1
)
13207 && (code
== VEC_COND_EXPR
? integer_all_onesp (op2
)
13208 : (integer_onep (op2
)
13209 && !VECTOR_TYPE_P (type
)))
13210 && truth_value_p (TREE_CODE (arg0
)))
13211 return pedantic_non_lvalue_loc (loc
,
13212 fold_convert_loc (loc
, type
,
13213 invert_truthvalue_loc (loc
,
13216 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13217 if (TREE_CODE (arg0
) == LT_EXPR
13218 && integer_zerop (TREE_OPERAND (arg0
, 1))
13219 && integer_zerop (op2
)
13220 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
13222 /* sign_bit_p looks through both zero and sign extensions,
13223 but for this optimization only sign extensions are
13225 tree tem2
= TREE_OPERAND (arg0
, 0);
13226 while (tem
!= tem2
)
13228 if (TREE_CODE (tem2
) != NOP_EXPR
13229 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
13234 tem2
= TREE_OPERAND (tem2
, 0);
13236 /* sign_bit_p only checks ARG1 bits within A's precision.
13237 If <sign bit of A> has wider type than A, bits outside
13238 of A's precision in <sign bit of A> need to be checked.
13239 If they are all 0, this optimization needs to be done
13240 in unsigned A's type, if they are all 1 in signed A's type,
13241 otherwise this can't be done. */
13243 && TYPE_PRECISION (TREE_TYPE (tem
))
13244 < TYPE_PRECISION (TREE_TYPE (arg1
))
13245 && TYPE_PRECISION (TREE_TYPE (tem
))
13246 < TYPE_PRECISION (type
))
13248 int inner_width
, outer_width
;
13251 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
13252 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
13253 if (outer_width
> TYPE_PRECISION (type
))
13254 outer_width
= TYPE_PRECISION (type
);
13256 wide_int mask
= wi::shifted_mask
13257 (inner_width
, outer_width
- inner_width
, false,
13258 TYPE_PRECISION (TREE_TYPE (arg1
)));
13260 wide_int common
= mask
& arg1
;
13261 if (common
== mask
)
13263 tem_type
= signed_type_for (TREE_TYPE (tem
));
13264 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13266 else if (common
== 0)
13268 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
13269 tem
= fold_convert_loc (loc
, tem_type
, tem
);
13277 fold_convert_loc (loc
, type
,
13278 fold_build2_loc (loc
, BIT_AND_EXPR
,
13279 TREE_TYPE (tem
), tem
,
13280 fold_convert_loc (loc
,
13285 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13286 already handled above. */
13287 if (TREE_CODE (arg0
) == BIT_AND_EXPR
13288 && integer_onep (TREE_OPERAND (arg0
, 1))
13289 && integer_zerop (op2
)
13290 && integer_pow2p (arg1
))
13292 tree tem
= TREE_OPERAND (arg0
, 0);
13294 if (TREE_CODE (tem
) == RSHIFT_EXPR
13295 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
13296 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
13297 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
13298 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
13299 TREE_OPERAND (tem
, 0), arg1
);
13302 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13303 is probably obsolete because the first operand should be a
13304 truth value (that's why we have the two cases above), but let's
13305 leave it in until we can confirm this for all front-ends. */
13306 if (integer_zerop (op2
)
13307 && TREE_CODE (arg0
) == NE_EXPR
13308 && integer_zerop (TREE_OPERAND (arg0
, 1))
13309 && integer_pow2p (arg1
)
13310 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
13311 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
13312 arg1
, OEP_ONLY_CONST
))
13313 return pedantic_non_lvalue_loc (loc
,
13314 fold_convert_loc (loc
, type
,
13315 TREE_OPERAND (arg0
, 0)));
13317 /* Disable the transformations below for vectors, since
13318 fold_binary_op_with_conditional_arg may undo them immediately,
13319 yielding an infinite loop. */
13320 if (code
== VEC_COND_EXPR
)
13323 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13324 if (integer_zerop (op2
)
13325 && truth_value_p (TREE_CODE (arg0
))
13326 && truth_value_p (TREE_CODE (arg1
))
13327 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13328 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
13329 : TRUTH_ANDIF_EXPR
,
13330 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
13332 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13333 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
13334 && truth_value_p (TREE_CODE (arg0
))
13335 && truth_value_p (TREE_CODE (arg1
))
13336 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13338 location_t loc0
= expr_location_or (arg0
, loc
);
13339 /* Only perform transformation if ARG0 is easily inverted. */
13340 tem
= fold_invert_truthvalue (loc0
, arg0
);
13342 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13345 type
, fold_convert_loc (loc
, type
, tem
),
13349 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13350 if (integer_zerop (arg1
)
13351 && truth_value_p (TREE_CODE (arg0
))
13352 && truth_value_p (TREE_CODE (op2
))
13353 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13355 location_t loc0
= expr_location_or (arg0
, loc
);
13356 /* Only perform transformation if ARG0 is easily inverted. */
13357 tem
= fold_invert_truthvalue (loc0
, arg0
);
13359 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13360 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
13361 type
, fold_convert_loc (loc
, type
, tem
),
13365 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13366 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
13367 && truth_value_p (TREE_CODE (arg0
))
13368 && truth_value_p (TREE_CODE (op2
))
13369 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
13370 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
13371 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
13372 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
13377 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13378 of fold_ternary on them. */
13379 gcc_unreachable ();
13381 case BIT_FIELD_REF
:
13382 if ((TREE_CODE (arg0
) == VECTOR_CST
13383 || (TREE_CODE (arg0
) == CONSTRUCTOR
13384 && TREE_CODE (TREE_TYPE (arg0
)) == VECTOR_TYPE
))
13385 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
13386 || (TREE_CODE (type
) == VECTOR_TYPE
13387 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
13389 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
13390 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
13391 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
13392 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
13395 && (idx
% width
) == 0
13396 && (n
% width
) == 0
13397 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
13402 if (TREE_CODE (arg0
) == VECTOR_CST
)
13405 return VECTOR_CST_ELT (arg0
, idx
);
13407 tree
*vals
= XALLOCAVEC (tree
, n
);
13408 for (unsigned i
= 0; i
< n
; ++i
)
13409 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
13410 return build_vector (type
, vals
);
13413 /* Constructor elements can be subvectors. */
13414 unsigned HOST_WIDE_INT k
= 1;
13415 if (CONSTRUCTOR_NELTS (arg0
) != 0)
13417 tree cons_elem
= TREE_TYPE (CONSTRUCTOR_ELT (arg0
, 0)->value
);
13418 if (TREE_CODE (cons_elem
) == VECTOR_TYPE
)
13419 k
= TYPE_VECTOR_SUBPARTS (cons_elem
);
13422 /* We keep an exact subset of the constructor elements. */
13423 if ((idx
% k
) == 0 && (n
% k
) == 0)
13425 if (CONSTRUCTOR_NELTS (arg0
) == 0)
13426 return build_constructor (type
, NULL
);
13431 if (idx
< CONSTRUCTOR_NELTS (arg0
))
13432 return CONSTRUCTOR_ELT (arg0
, idx
)->value
;
13433 return build_zero_cst (type
);
13436 vec
<constructor_elt
, va_gc
> *vals
;
13437 vec_alloc (vals
, n
);
13438 for (unsigned i
= 0;
13439 i
< n
&& idx
+ i
< CONSTRUCTOR_NELTS (arg0
);
13441 CONSTRUCTOR_APPEND_ELT (vals
, NULL_TREE
,
13443 (arg0
, idx
+ i
)->value
);
13444 return build_constructor (type
, vals
);
13446 /* The bitfield references a single constructor element. */
13447 else if (idx
+ n
<= (idx
/ k
+ 1) * k
)
13449 if (CONSTRUCTOR_NELTS (arg0
) <= idx
/ k
)
13450 return build_zero_cst (type
);
13452 return CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
;
13454 return fold_build3_loc (loc
, code
, type
,
13455 CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
, op1
,
13456 build_int_cst (TREE_TYPE (op2
), (idx
% k
) * width
));
13461 /* A bit-field-ref that referenced the full argument can be stripped. */
13462 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
13463 && TYPE_PRECISION (TREE_TYPE (arg0
)) == tree_to_uhwi (arg1
)
13464 && integer_zerop (op2
))
13465 return fold_convert_loc (loc
, type
, arg0
);
13467 /* On constants we can use native encode/interpret to constant
13468 fold (nearly) all BIT_FIELD_REFs. */
13469 if (CONSTANT_CLASS_P (arg0
)
13470 && can_native_interpret_type_p (type
)
13471 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)))
13472 /* This limitation should not be necessary, we just need to
13473 round this up to mode size. */
13474 && tree_to_uhwi (op1
) % BITS_PER_UNIT
== 0
13475 /* Need bit-shifting of the buffer to relax the following. */
13476 && tree_to_uhwi (op2
) % BITS_PER_UNIT
== 0)
13478 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
13479 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
13480 unsigned HOST_WIDE_INT clen
;
13481 clen
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)));
13482 /* ??? We cannot tell native_encode_expr to start at
13483 some random byte only. So limit us to a reasonable amount
13487 unsigned char *b
= XALLOCAVEC (unsigned char, clen
);
13488 unsigned HOST_WIDE_INT len
= native_encode_expr (arg0
, b
, clen
);
13490 && len
* BITS_PER_UNIT
>= bitpos
+ bitsize
)
13492 tree v
= native_interpret_expr (type
,
13493 b
+ bitpos
/ BITS_PER_UNIT
,
13494 bitsize
/ BITS_PER_UNIT
);
13504 /* For integers we can decompose the FMA if possible. */
13505 if (TREE_CODE (arg0
) == INTEGER_CST
13506 && TREE_CODE (arg1
) == INTEGER_CST
)
13507 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
13508 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
13509 if (integer_zerop (arg2
))
13510 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
13512 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
13514 case VEC_PERM_EXPR
:
13515 if (TREE_CODE (arg2
) == VECTOR_CST
)
13517 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
13518 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
13519 unsigned char *sel2
= sel
+ nelts
;
13520 bool need_mask_canon
= false;
13521 bool need_mask_canon2
= false;
13522 bool all_in_vec0
= true;
13523 bool all_in_vec1
= true;
13524 bool maybe_identity
= true;
13525 bool single_arg
= (op0
== op1
);
13526 bool changed
= false;
13528 mask2
= 2 * nelts
- 1;
13529 mask
= single_arg
? (nelts
- 1) : mask2
;
13530 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
13531 for (i
= 0; i
< nelts
; i
++)
13533 tree val
= VECTOR_CST_ELT (arg2
, i
);
13534 if (TREE_CODE (val
) != INTEGER_CST
)
13537 /* Make sure that the perm value is in an acceptable
13540 need_mask_canon
|= wi::gtu_p (t
, mask
);
13541 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
13542 sel
[i
] = t
.to_uhwi () & mask
;
13543 sel2
[i
] = t
.to_uhwi () & mask2
;
13545 if (sel
[i
] < nelts
)
13546 all_in_vec1
= false;
13548 all_in_vec0
= false;
13550 if ((sel
[i
] & (nelts
-1)) != i
)
13551 maybe_identity
= false;
13554 if (maybe_identity
)
13564 else if (all_in_vec1
)
13567 for (i
= 0; i
< nelts
; i
++)
13569 need_mask_canon
= true;
13572 if ((TREE_CODE (op0
) == VECTOR_CST
13573 || TREE_CODE (op0
) == CONSTRUCTOR
)
13574 && (TREE_CODE (op1
) == VECTOR_CST
13575 || TREE_CODE (op1
) == CONSTRUCTOR
))
13577 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
13578 if (t
!= NULL_TREE
)
13582 if (op0
== op1
&& !single_arg
)
13585 /* Some targets are deficient and fail to expand a single
13586 argument permutation while still allowing an equivalent
13587 2-argument version. */
13588 if (need_mask_canon
&& arg2
== op2
13589 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
13590 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
13592 need_mask_canon
= need_mask_canon2
;
13596 if (need_mask_canon
&& arg2
== op2
)
13598 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
13599 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
13600 for (i
= 0; i
< nelts
; i
++)
13601 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
13602 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
13607 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
13613 } /* switch (code) */
13616 /* Perform constant folding and related simplification of EXPR.
13617 The related simplifications include x*1 => x, x*0 => 0, etc.,
13618 and application of the associative law.
13619 NOP_EXPR conversions may be removed freely (as long as we
13620 are careful not to change the type of the overall expression).
13621 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13622 but we can constant-fold them if they have constant operands. */
13624 #ifdef ENABLE_FOLD_CHECKING
13625 # define fold(x) fold_1 (x)
13626 static tree
fold_1 (tree
);
13632 const tree t
= expr
;
13633 enum tree_code code
= TREE_CODE (t
);
13634 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
13636 location_t loc
= EXPR_LOCATION (expr
);
13638 /* Return right away if a constant. */
13639 if (kind
== tcc_constant
)
13642 /* CALL_EXPR-like objects with variable numbers of operands are
13643 treated specially. */
13644 if (kind
== tcc_vl_exp
)
13646 if (code
== CALL_EXPR
)
13648 tem
= fold_call_expr (loc
, expr
, false);
13649 return tem
? tem
: expr
;
13654 if (IS_EXPR_CODE_CLASS (kind
))
13656 tree type
= TREE_TYPE (t
);
13657 tree op0
, op1
, op2
;
13659 switch (TREE_CODE_LENGTH (code
))
13662 op0
= TREE_OPERAND (t
, 0);
13663 tem
= fold_unary_loc (loc
, code
, type
, op0
);
13664 return tem
? tem
: expr
;
13666 op0
= TREE_OPERAND (t
, 0);
13667 op1
= TREE_OPERAND (t
, 1);
13668 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
13669 return tem
? tem
: expr
;
13671 op0
= TREE_OPERAND (t
, 0);
13672 op1
= TREE_OPERAND (t
, 1);
13673 op2
= TREE_OPERAND (t
, 2);
13674 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
13675 return tem
? tem
: expr
;
13685 tree op0
= TREE_OPERAND (t
, 0);
13686 tree op1
= TREE_OPERAND (t
, 1);
13688 if (TREE_CODE (op1
) == INTEGER_CST
13689 && TREE_CODE (op0
) == CONSTRUCTOR
13690 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
13692 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (op0
);
13693 unsigned HOST_WIDE_INT end
= vec_safe_length (elts
);
13694 unsigned HOST_WIDE_INT begin
= 0;
13696 /* Find a matching index by means of a binary search. */
13697 while (begin
!= end
)
13699 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
13700 tree index
= (*elts
)[middle
].index
;
13702 if (TREE_CODE (index
) == INTEGER_CST
13703 && tree_int_cst_lt (index
, op1
))
13704 begin
= middle
+ 1;
13705 else if (TREE_CODE (index
) == INTEGER_CST
13706 && tree_int_cst_lt (op1
, index
))
13708 else if (TREE_CODE (index
) == RANGE_EXPR
13709 && tree_int_cst_lt (TREE_OPERAND (index
, 1), op1
))
13710 begin
= middle
+ 1;
13711 else if (TREE_CODE (index
) == RANGE_EXPR
13712 && tree_int_cst_lt (op1
, TREE_OPERAND (index
, 0)))
13715 return (*elts
)[middle
].value
;
13722 /* Return a VECTOR_CST if possible. */
13725 tree type
= TREE_TYPE (t
);
13726 if (TREE_CODE (type
) != VECTOR_TYPE
)
13729 tree
*vec
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
13730 unsigned HOST_WIDE_INT idx
, pos
= 0;
13733 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, value
)
13735 if (!CONSTANT_CLASS_P (value
))
13737 if (TREE_CODE (value
) == VECTOR_CST
)
13739 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (value
); ++i
)
13740 vec
[pos
++] = VECTOR_CST_ELT (value
, i
);
13743 vec
[pos
++] = value
;
13745 for (; pos
< TYPE_VECTOR_SUBPARTS (type
); ++pos
)
13746 vec
[pos
] = build_zero_cst (TREE_TYPE (type
));
13748 return build_vector (type
, vec
);
13752 return fold (DECL_INITIAL (t
));
13756 } /* switch (code) */
13759 #ifdef ENABLE_FOLD_CHECKING
13762 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
13763 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
13764 static void fold_check_failed (const_tree
, const_tree
);
13765 void print_fold_checksum (const_tree
);
13767 /* When --enable-checking=fold, compute a digest of expr before
13768 and after actual fold call to see if fold did not accidentally
13769 change original expr. */
13775 struct md5_ctx ctx
;
13776 unsigned char checksum_before
[16], checksum_after
[16];
13777 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13779 md5_init_ctx (&ctx
);
13780 fold_checksum_tree (expr
, &ctx
, &ht
);
13781 md5_finish_ctx (&ctx
, checksum_before
);
13784 ret
= fold_1 (expr
);
13786 md5_init_ctx (&ctx
);
13787 fold_checksum_tree (expr
, &ctx
, &ht
);
13788 md5_finish_ctx (&ctx
, checksum_after
);
13790 if (memcmp (checksum_before
, checksum_after
, 16))
13791 fold_check_failed (expr
, ret
);
13797 print_fold_checksum (const_tree expr
)
13799 struct md5_ctx ctx
;
13800 unsigned char checksum
[16], cnt
;
13801 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13803 md5_init_ctx (&ctx
);
13804 fold_checksum_tree (expr
, &ctx
, &ht
);
13805 md5_finish_ctx (&ctx
, checksum
);
13806 for (cnt
= 0; cnt
< 16; ++cnt
)
13807 fprintf (stderr
, "%02x", checksum
[cnt
]);
13808 putc ('\n', stderr
);
13812 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
13814 internal_error ("fold check: original tree changed by fold");
13818 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
13819 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
13821 const tree_node
**slot
;
13822 enum tree_code code
;
13823 union tree_node buf
;
13829 slot
= ht
->find_slot (expr
, INSERT
);
13833 code
= TREE_CODE (expr
);
13834 if (TREE_CODE_CLASS (code
) == tcc_declaration
13835 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
13837 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13838 memcpy ((char *) &buf
, expr
, tree_size (expr
));
13839 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
13840 buf
.decl_with_vis
.symtab_node
= NULL
;
13841 expr
= (tree
) &buf
;
13843 else if (TREE_CODE_CLASS (code
) == tcc_type
13844 && (TYPE_POINTER_TO (expr
)
13845 || TYPE_REFERENCE_TO (expr
)
13846 || TYPE_CACHED_VALUES_P (expr
)
13847 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
13848 || TYPE_NEXT_VARIANT (expr
)))
13850 /* Allow these fields to be modified. */
13852 memcpy ((char *) &buf
, expr
, tree_size (expr
));
13853 expr
= tmp
= (tree
) &buf
;
13854 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
13855 TYPE_POINTER_TO (tmp
) = NULL
;
13856 TYPE_REFERENCE_TO (tmp
) = NULL
;
13857 TYPE_NEXT_VARIANT (tmp
) = NULL
;
13858 if (TYPE_CACHED_VALUES_P (tmp
))
13860 TYPE_CACHED_VALUES_P (tmp
) = 0;
13861 TYPE_CACHED_VALUES (tmp
) = NULL
;
13864 md5_process_bytes (expr
, tree_size (expr
), ctx
);
13865 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
13866 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
13867 if (TREE_CODE_CLASS (code
) != tcc_type
13868 && TREE_CODE_CLASS (code
) != tcc_declaration
13869 && code
!= TREE_LIST
13870 && code
!= SSA_NAME
13871 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
13872 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
13873 switch (TREE_CODE_CLASS (code
))
13879 md5_process_bytes (TREE_STRING_POINTER (expr
),
13880 TREE_STRING_LENGTH (expr
), ctx
);
13883 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
13884 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
13887 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
13888 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
13894 case tcc_exceptional
:
13898 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
13899 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
13900 expr
= TREE_CHAIN (expr
);
13901 goto recursive_label
;
13904 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
13905 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
13911 case tcc_expression
:
13912 case tcc_reference
:
13913 case tcc_comparison
:
13916 case tcc_statement
:
13918 len
= TREE_OPERAND_LENGTH (expr
);
13919 for (i
= 0; i
< len
; ++i
)
13920 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
13922 case tcc_declaration
:
13923 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
13924 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
13925 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
13927 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
13928 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
13929 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
13930 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
13931 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
13934 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
13936 if (TREE_CODE (expr
) == FUNCTION_DECL
)
13938 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
13939 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
13941 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
13945 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
13946 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
13947 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
13948 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
13949 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
13950 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
13951 if (INTEGRAL_TYPE_P (expr
)
13952 || SCALAR_FLOAT_TYPE_P (expr
))
13954 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
13955 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
13957 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
13958 if (TREE_CODE (expr
) == RECORD_TYPE
13959 || TREE_CODE (expr
) == UNION_TYPE
13960 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
13961 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
13962 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
13969 /* Helper function for outputting the checksum of a tree T. When
13970 debugging with gdb, you can "define mynext" to be "next" followed
13971 by "call debug_fold_checksum (op0)", then just trace down till the
13974 DEBUG_FUNCTION
void
13975 debug_fold_checksum (const_tree t
)
13978 unsigned char checksum
[16];
13979 struct md5_ctx ctx
;
13980 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
13982 md5_init_ctx (&ctx
);
13983 fold_checksum_tree (t
, &ctx
, &ht
);
13984 md5_finish_ctx (&ctx
, checksum
);
13987 for (i
= 0; i
< 16; i
++)
13988 fprintf (stderr
, "%d ", checksum
[i
]);
13990 fprintf (stderr
, "\n");
13995 /* Fold a unary tree expression with code CODE of type TYPE with an
13996 operand OP0. LOC is the location of the resulting expression.
13997 Return a folded expression if successful. Otherwise, return a tree
13998 expression with code CODE of type TYPE with an operand OP0. */
14001 fold_build1_stat_loc (location_t loc
,
14002 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
14005 #ifdef ENABLE_FOLD_CHECKING
14006 unsigned char checksum_before
[16], checksum_after
[16];
14007 struct md5_ctx ctx
;
14008 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14010 md5_init_ctx (&ctx
);
14011 fold_checksum_tree (op0
, &ctx
, &ht
);
14012 md5_finish_ctx (&ctx
, checksum_before
);
14016 tem
= fold_unary_loc (loc
, code
, type
, op0
);
14018 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
14020 #ifdef ENABLE_FOLD_CHECKING
14021 md5_init_ctx (&ctx
);
14022 fold_checksum_tree (op0
, &ctx
, &ht
);
14023 md5_finish_ctx (&ctx
, checksum_after
);
14025 if (memcmp (checksum_before
, checksum_after
, 16))
14026 fold_check_failed (op0
, tem
);
14031 /* Fold a binary tree expression with code CODE of type TYPE with
14032 operands OP0 and OP1. LOC is the location of the resulting
14033 expression. Return a folded expression if successful. Otherwise,
14034 return a tree expression with code CODE of type TYPE with operands
14038 fold_build2_stat_loc (location_t loc
,
14039 enum tree_code code
, tree type
, tree op0
, tree op1
14043 #ifdef ENABLE_FOLD_CHECKING
14044 unsigned char checksum_before_op0
[16],
14045 checksum_before_op1
[16],
14046 checksum_after_op0
[16],
14047 checksum_after_op1
[16];
14048 struct md5_ctx ctx
;
14049 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14051 md5_init_ctx (&ctx
);
14052 fold_checksum_tree (op0
, &ctx
, &ht
);
14053 md5_finish_ctx (&ctx
, checksum_before_op0
);
14056 md5_init_ctx (&ctx
);
14057 fold_checksum_tree (op1
, &ctx
, &ht
);
14058 md5_finish_ctx (&ctx
, checksum_before_op1
);
14062 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
14064 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
14066 #ifdef ENABLE_FOLD_CHECKING
14067 md5_init_ctx (&ctx
);
14068 fold_checksum_tree (op0
, &ctx
, &ht
);
14069 md5_finish_ctx (&ctx
, checksum_after_op0
);
14072 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14073 fold_check_failed (op0
, tem
);
14075 md5_init_ctx (&ctx
);
14076 fold_checksum_tree (op1
, &ctx
, &ht
);
14077 md5_finish_ctx (&ctx
, checksum_after_op1
);
14079 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14080 fold_check_failed (op1
, tem
);
14085 /* Fold a ternary tree expression with code CODE of type TYPE with
14086 operands OP0, OP1, and OP2. Return a folded expression if
14087 successful. Otherwise, return a tree expression with code CODE of
14088 type TYPE with operands OP0, OP1, and OP2. */
14091 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
14092 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
14095 #ifdef ENABLE_FOLD_CHECKING
14096 unsigned char checksum_before_op0
[16],
14097 checksum_before_op1
[16],
14098 checksum_before_op2
[16],
14099 checksum_after_op0
[16],
14100 checksum_after_op1
[16],
14101 checksum_after_op2
[16];
14102 struct md5_ctx ctx
;
14103 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14105 md5_init_ctx (&ctx
);
14106 fold_checksum_tree (op0
, &ctx
, &ht
);
14107 md5_finish_ctx (&ctx
, checksum_before_op0
);
14110 md5_init_ctx (&ctx
);
14111 fold_checksum_tree (op1
, &ctx
, &ht
);
14112 md5_finish_ctx (&ctx
, checksum_before_op1
);
14115 md5_init_ctx (&ctx
);
14116 fold_checksum_tree (op2
, &ctx
, &ht
);
14117 md5_finish_ctx (&ctx
, checksum_before_op2
);
14121 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
14122 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
14124 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
14126 #ifdef ENABLE_FOLD_CHECKING
14127 md5_init_ctx (&ctx
);
14128 fold_checksum_tree (op0
, &ctx
, &ht
);
14129 md5_finish_ctx (&ctx
, checksum_after_op0
);
14132 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
14133 fold_check_failed (op0
, tem
);
14135 md5_init_ctx (&ctx
);
14136 fold_checksum_tree (op1
, &ctx
, &ht
);
14137 md5_finish_ctx (&ctx
, checksum_after_op1
);
14140 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
14141 fold_check_failed (op1
, tem
);
14143 md5_init_ctx (&ctx
);
14144 fold_checksum_tree (op2
, &ctx
, &ht
);
14145 md5_finish_ctx (&ctx
, checksum_after_op2
);
14147 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
14148 fold_check_failed (op2
, tem
);
14153 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14154 arguments in ARGARRAY, and a null static chain.
14155 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14156 of type TYPE from the given operands as constructed by build_call_array. */
14159 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
14160 int nargs
, tree
*argarray
)
14163 #ifdef ENABLE_FOLD_CHECKING
14164 unsigned char checksum_before_fn
[16],
14165 checksum_before_arglist
[16],
14166 checksum_after_fn
[16],
14167 checksum_after_arglist
[16];
14168 struct md5_ctx ctx
;
14169 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
14172 md5_init_ctx (&ctx
);
14173 fold_checksum_tree (fn
, &ctx
, &ht
);
14174 md5_finish_ctx (&ctx
, checksum_before_fn
);
14177 md5_init_ctx (&ctx
);
14178 for (i
= 0; i
< nargs
; i
++)
14179 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14180 md5_finish_ctx (&ctx
, checksum_before_arglist
);
14184 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
14186 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14188 #ifdef ENABLE_FOLD_CHECKING
14189 md5_init_ctx (&ctx
);
14190 fold_checksum_tree (fn
, &ctx
, &ht
);
14191 md5_finish_ctx (&ctx
, checksum_after_fn
);
14194 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
14195 fold_check_failed (fn
, tem
);
14197 md5_init_ctx (&ctx
);
14198 for (i
= 0; i
< nargs
; i
++)
14199 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
14200 md5_finish_ctx (&ctx
, checksum_after_arglist
);
14202 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
14203 fold_check_failed (NULL_TREE
, tem
);
14208 /* Perform constant folding and related simplification of initializer
14209 expression EXPR. These behave identically to "fold_buildN" but ignore
14210 potential run-time traps and exceptions that fold must preserve. */
14212 #define START_FOLD_INIT \
14213 int saved_signaling_nans = flag_signaling_nans;\
14214 int saved_trapping_math = flag_trapping_math;\
14215 int saved_rounding_math = flag_rounding_math;\
14216 int saved_trapv = flag_trapv;\
14217 int saved_folding_initializer = folding_initializer;\
14218 flag_signaling_nans = 0;\
14219 flag_trapping_math = 0;\
14220 flag_rounding_math = 0;\
14222 folding_initializer = 1;
14224 #define END_FOLD_INIT \
14225 flag_signaling_nans = saved_signaling_nans;\
14226 flag_trapping_math = saved_trapping_math;\
14227 flag_rounding_math = saved_rounding_math;\
14228 flag_trapv = saved_trapv;\
14229 folding_initializer = saved_folding_initializer;
14232 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
14233 tree type
, tree op
)
14238 result
= fold_build1_loc (loc
, code
, type
, op
);
14245 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
14246 tree type
, tree op0
, tree op1
)
14251 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
14258 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
14259 int nargs
, tree
*argarray
)
14264 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
14270 #undef START_FOLD_INIT
14271 #undef END_FOLD_INIT
14273 /* Determine if first argument is a multiple of second argument. Return 0 if
14274 it is not, or we cannot easily determined it to be.
14276 An example of the sort of thing we care about (at this point; this routine
14277 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14278 fold cases do now) is discovering that
14280 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14286 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14288 This code also handles discovering that
14290 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14292 is a multiple of 8 so we don't have to worry about dealing with a
14293 possible remainder.
14295 Note that we *look* inside a SAVE_EXPR only to determine how it was
14296 calculated; it is not safe for fold to do much of anything else with the
14297 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14298 at run time. For example, the latter example above *cannot* be implemented
14299 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14300 evaluation time of the original SAVE_EXPR is not necessarily the same at
14301 the time the new expression is evaluated. The only optimization of this
14302 sort that would be valid is changing
14304 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14308 SAVE_EXPR (I) * SAVE_EXPR (J)
14310 (where the same SAVE_EXPR (J) is used in the original and the
14311 transformed version). */
14314 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
14316 if (operand_equal_p (top
, bottom
, 0))
14319 if (TREE_CODE (type
) != INTEGER_TYPE
)
14322 switch (TREE_CODE (top
))
14325 /* Bitwise and provides a power of two multiple. If the mask is
14326 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14327 if (!integer_pow2p (bottom
))
14332 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
14333 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
14337 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
14338 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
14341 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
14345 op1
= TREE_OPERAND (top
, 1);
14346 /* const_binop may not detect overflow correctly,
14347 so check for it explicitly here. */
14348 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
14349 && 0 != (t1
= fold_convert (type
,
14350 const_binop (LSHIFT_EXPR
,
14353 && !TREE_OVERFLOW (t1
))
14354 return multiple_of_p (type
, t1
, bottom
);
14359 /* Can't handle conversions from non-integral or wider integral type. */
14360 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
14361 || (TYPE_PRECISION (type
)
14362 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
14365 /* .. fall through ... */
14368 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
14371 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
14372 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
14375 if (TREE_CODE (bottom
) != INTEGER_CST
14376 || integer_zerop (bottom
)
14377 || (TYPE_UNSIGNED (type
)
14378 && (tree_int_cst_sgn (top
) < 0
14379 || tree_int_cst_sgn (bottom
) < 0)))
14381 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
14389 /* Return true if CODE or TYPE is known to be non-negative. */
14392 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
14394 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
14395 && truth_value_p (code
))
14396 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14397 have a signed:1 type (where the value is -1 and 0). */
14402 /* Return true if (CODE OP0) is known to be non-negative. If the return
14403 value is based on the assumption that signed overflow is undefined,
14404 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14405 *STRICT_OVERFLOW_P. */
14408 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14409 bool *strict_overflow_p
)
14411 if (TYPE_UNSIGNED (type
))
14417 /* We can't return 1 if flag_wrapv is set because
14418 ABS_EXPR<INT_MIN> = INT_MIN. */
14419 if (!ANY_INTEGRAL_TYPE_P (type
))
14421 if (TYPE_OVERFLOW_UNDEFINED (type
))
14423 *strict_overflow_p
= true;
14428 case NON_LVALUE_EXPR
:
14430 case FIX_TRUNC_EXPR
:
14431 return tree_expr_nonnegative_warnv_p (op0
,
14432 strict_overflow_p
);
14436 tree inner_type
= TREE_TYPE (op0
);
14437 tree outer_type
= type
;
14439 if (TREE_CODE (outer_type
) == REAL_TYPE
)
14441 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14442 return tree_expr_nonnegative_warnv_p (op0
,
14443 strict_overflow_p
);
14444 if (INTEGRAL_TYPE_P (inner_type
))
14446 if (TYPE_UNSIGNED (inner_type
))
14448 return tree_expr_nonnegative_warnv_p (op0
,
14449 strict_overflow_p
);
14452 else if (INTEGRAL_TYPE_P (outer_type
))
14454 if (TREE_CODE (inner_type
) == REAL_TYPE
)
14455 return tree_expr_nonnegative_warnv_p (op0
,
14456 strict_overflow_p
);
14457 if (INTEGRAL_TYPE_P (inner_type
))
14458 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
14459 && TYPE_UNSIGNED (inner_type
);
14465 return tree_simple_nonnegative_warnv_p (code
, type
);
14468 /* We don't know sign of `t', so be conservative and return false. */
14472 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14473 value is based on the assumption that signed overflow is undefined,
14474 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14475 *STRICT_OVERFLOW_P. */
14478 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
14479 tree op1
, bool *strict_overflow_p
)
14481 if (TYPE_UNSIGNED (type
))
14486 case POINTER_PLUS_EXPR
:
14488 if (FLOAT_TYPE_P (type
))
14489 return (tree_expr_nonnegative_warnv_p (op0
,
14491 && tree_expr_nonnegative_warnv_p (op1
,
14492 strict_overflow_p
));
14494 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14495 both unsigned and at least 2 bits shorter than the result. */
14496 if (TREE_CODE (type
) == INTEGER_TYPE
14497 && TREE_CODE (op0
) == NOP_EXPR
14498 && TREE_CODE (op1
) == NOP_EXPR
)
14500 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
14501 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
14502 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
14503 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
14505 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
14506 TYPE_PRECISION (inner2
)) + 1;
14507 return prec
< TYPE_PRECISION (type
);
14513 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
14515 /* x * x is always non-negative for floating point x
14516 or without overflow. */
14517 if (operand_equal_p (op0
, op1
, 0)
14518 || (tree_expr_nonnegative_warnv_p (op0
, strict_overflow_p
)
14519 && tree_expr_nonnegative_warnv_p (op1
, strict_overflow_p
)))
14521 if (ANY_INTEGRAL_TYPE_P (type
)
14522 && TYPE_OVERFLOW_UNDEFINED (type
))
14523 *strict_overflow_p
= true;
14528 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14529 both unsigned and their total bits is shorter than the result. */
14530 if (TREE_CODE (type
) == INTEGER_TYPE
14531 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
14532 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
14534 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
14535 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
14537 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
14538 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
14541 bool unsigned0
= TYPE_UNSIGNED (inner0
);
14542 bool unsigned1
= TYPE_UNSIGNED (inner1
);
14544 if (TREE_CODE (op0
) == INTEGER_CST
)
14545 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
14547 if (TREE_CODE (op1
) == INTEGER_CST
)
14548 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
14550 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
14551 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
14553 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
14554 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
14555 : TYPE_PRECISION (inner0
);
14557 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
14558 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
14559 : TYPE_PRECISION (inner1
);
14561 return precision0
+ precision1
< TYPE_PRECISION (type
);
14568 return (tree_expr_nonnegative_warnv_p (op0
,
14570 || tree_expr_nonnegative_warnv_p (op1
,
14571 strict_overflow_p
));
14577 case TRUNC_DIV_EXPR
:
14578 case CEIL_DIV_EXPR
:
14579 case FLOOR_DIV_EXPR
:
14580 case ROUND_DIV_EXPR
:
14581 return (tree_expr_nonnegative_warnv_p (op0
,
14583 && tree_expr_nonnegative_warnv_p (op1
,
14584 strict_overflow_p
));
14586 case TRUNC_MOD_EXPR
:
14587 case CEIL_MOD_EXPR
:
14588 case FLOOR_MOD_EXPR
:
14589 case ROUND_MOD_EXPR
:
14590 return tree_expr_nonnegative_warnv_p (op0
,
14591 strict_overflow_p
);
14593 return tree_simple_nonnegative_warnv_p (code
, type
);
14596 /* We don't know sign of `t', so be conservative and return false. */
14600 /* Return true if T is known to be non-negative. If the return
14601 value is based on the assumption that signed overflow is undefined,
14602 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14603 *STRICT_OVERFLOW_P. */
14606 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
14608 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14611 switch (TREE_CODE (t
))
14614 return tree_int_cst_sgn (t
) >= 0;
14617 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
14620 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
14623 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
14625 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 2),
14626 strict_overflow_p
));
14628 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
),
14631 /* We don't know sign of `t', so be conservative and return false. */
14635 /* Return true if T is known to be non-negative. If the return
14636 value is based on the assumption that signed overflow is undefined,
14637 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14638 *STRICT_OVERFLOW_P. */
14641 tree_call_nonnegative_warnv_p (tree type
, tree fndecl
,
14642 tree arg0
, tree arg1
, bool *strict_overflow_p
)
14644 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
14645 switch (DECL_FUNCTION_CODE (fndecl
))
14647 CASE_FLT_FN (BUILT_IN_ACOS
):
14648 CASE_FLT_FN (BUILT_IN_ACOSH
):
14649 CASE_FLT_FN (BUILT_IN_CABS
):
14650 CASE_FLT_FN (BUILT_IN_COSH
):
14651 CASE_FLT_FN (BUILT_IN_ERFC
):
14652 CASE_FLT_FN (BUILT_IN_EXP
):
14653 CASE_FLT_FN (BUILT_IN_EXP10
):
14654 CASE_FLT_FN (BUILT_IN_EXP2
):
14655 CASE_FLT_FN (BUILT_IN_FABS
):
14656 CASE_FLT_FN (BUILT_IN_FDIM
):
14657 CASE_FLT_FN (BUILT_IN_HYPOT
):
14658 CASE_FLT_FN (BUILT_IN_POW10
):
14659 CASE_INT_FN (BUILT_IN_FFS
):
14660 CASE_INT_FN (BUILT_IN_PARITY
):
14661 CASE_INT_FN (BUILT_IN_POPCOUNT
):
14662 CASE_INT_FN (BUILT_IN_CLZ
):
14663 CASE_INT_FN (BUILT_IN_CLRSB
):
14664 case BUILT_IN_BSWAP32
:
14665 case BUILT_IN_BSWAP64
:
14669 CASE_FLT_FN (BUILT_IN_SQRT
):
14670 /* sqrt(-0.0) is -0.0. */
14671 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
14673 return tree_expr_nonnegative_warnv_p (arg0
,
14674 strict_overflow_p
);
14676 CASE_FLT_FN (BUILT_IN_ASINH
):
14677 CASE_FLT_FN (BUILT_IN_ATAN
):
14678 CASE_FLT_FN (BUILT_IN_ATANH
):
14679 CASE_FLT_FN (BUILT_IN_CBRT
):
14680 CASE_FLT_FN (BUILT_IN_CEIL
):
14681 CASE_FLT_FN (BUILT_IN_ERF
):
14682 CASE_FLT_FN (BUILT_IN_EXPM1
):
14683 CASE_FLT_FN (BUILT_IN_FLOOR
):
14684 CASE_FLT_FN (BUILT_IN_FMOD
):
14685 CASE_FLT_FN (BUILT_IN_FREXP
):
14686 CASE_FLT_FN (BUILT_IN_ICEIL
):
14687 CASE_FLT_FN (BUILT_IN_IFLOOR
):
14688 CASE_FLT_FN (BUILT_IN_IRINT
):
14689 CASE_FLT_FN (BUILT_IN_IROUND
):
14690 CASE_FLT_FN (BUILT_IN_LCEIL
):
14691 CASE_FLT_FN (BUILT_IN_LDEXP
):
14692 CASE_FLT_FN (BUILT_IN_LFLOOR
):
14693 CASE_FLT_FN (BUILT_IN_LLCEIL
):
14694 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
14695 CASE_FLT_FN (BUILT_IN_LLRINT
):
14696 CASE_FLT_FN (BUILT_IN_LLROUND
):
14697 CASE_FLT_FN (BUILT_IN_LRINT
):
14698 CASE_FLT_FN (BUILT_IN_LROUND
):
14699 CASE_FLT_FN (BUILT_IN_MODF
):
14700 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
14701 CASE_FLT_FN (BUILT_IN_RINT
):
14702 CASE_FLT_FN (BUILT_IN_ROUND
):
14703 CASE_FLT_FN (BUILT_IN_SCALB
):
14704 CASE_FLT_FN (BUILT_IN_SCALBLN
):
14705 CASE_FLT_FN (BUILT_IN_SCALBN
):
14706 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
14707 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
14708 CASE_FLT_FN (BUILT_IN_SINH
):
14709 CASE_FLT_FN (BUILT_IN_TANH
):
14710 CASE_FLT_FN (BUILT_IN_TRUNC
):
14711 /* True if the 1st argument is nonnegative. */
14712 return tree_expr_nonnegative_warnv_p (arg0
,
14713 strict_overflow_p
);
14715 CASE_FLT_FN (BUILT_IN_FMAX
):
14716 /* True if the 1st OR 2nd arguments are nonnegative. */
14717 return (tree_expr_nonnegative_warnv_p (arg0
,
14719 || (tree_expr_nonnegative_warnv_p (arg1
,
14720 strict_overflow_p
)));
14722 CASE_FLT_FN (BUILT_IN_FMIN
):
14723 /* True if the 1st AND 2nd arguments are nonnegative. */
14724 return (tree_expr_nonnegative_warnv_p (arg0
,
14726 && (tree_expr_nonnegative_warnv_p (arg1
,
14727 strict_overflow_p
)));
14729 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
14730 /* True if the 2nd argument is nonnegative. */
14731 return tree_expr_nonnegative_warnv_p (arg1
,
14732 strict_overflow_p
);
14734 CASE_FLT_FN (BUILT_IN_POWI
):
14735 /* True if the 1st argument is nonnegative or the second
14736 argument is an even integer. */
14737 if (TREE_CODE (arg1
) == INTEGER_CST
14738 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
14740 return tree_expr_nonnegative_warnv_p (arg0
,
14741 strict_overflow_p
);
14743 CASE_FLT_FN (BUILT_IN_POW
):
14744 /* True if the 1st argument is nonnegative or the second
14745 argument is an even integer valued real. */
14746 if (TREE_CODE (arg1
) == REAL_CST
)
14751 c
= TREE_REAL_CST (arg1
);
14752 n
= real_to_integer (&c
);
14755 REAL_VALUE_TYPE cint
;
14756 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
14757 if (real_identical (&c
, &cint
))
14761 return tree_expr_nonnegative_warnv_p (arg0
,
14762 strict_overflow_p
);
14767 return tree_simple_nonnegative_warnv_p (CALL_EXPR
,
14771 /* Return true if T is known to be non-negative. If the return
14772 value is based on the assumption that signed overflow is undefined,
14773 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14774 *STRICT_OVERFLOW_P. */
14777 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
14779 enum tree_code code
= TREE_CODE (t
);
14780 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
14787 tree temp
= TARGET_EXPR_SLOT (t
);
14788 t
= TARGET_EXPR_INITIAL (t
);
14790 /* If the initializer is non-void, then it's a normal expression
14791 that will be assigned to the slot. */
14792 if (!VOID_TYPE_P (t
))
14793 return tree_expr_nonnegative_warnv_p (t
, strict_overflow_p
);
14795 /* Otherwise, the initializer sets the slot in some way. One common
14796 way is an assignment statement at the end of the initializer. */
14799 if (TREE_CODE (t
) == BIND_EXPR
)
14800 t
= expr_last (BIND_EXPR_BODY (t
));
14801 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
14802 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
14803 t
= expr_last (TREE_OPERAND (t
, 0));
14804 else if (TREE_CODE (t
) == STATEMENT_LIST
)
14809 if (TREE_CODE (t
) == MODIFY_EXPR
14810 && TREE_OPERAND (t
, 0) == temp
)
14811 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
14812 strict_overflow_p
);
14819 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
14820 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
14822 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
14823 get_callee_fndecl (t
),
14826 strict_overflow_p
);
14828 case COMPOUND_EXPR
:
14830 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 1),
14831 strict_overflow_p
);
14833 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t
, 1)),
14834 strict_overflow_p
);
14836 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t
, 0),
14837 strict_overflow_p
);
14840 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
),
14844 /* We don't know sign of `t', so be conservative and return false. */
14848 /* Return true if T is known to be non-negative. If the return
14849 value is based on the assumption that signed overflow is undefined,
14850 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14851 *STRICT_OVERFLOW_P. */
14854 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
)
14856 enum tree_code code
;
14857 if (t
== error_mark_node
)
14860 code
= TREE_CODE (t
);
14861 switch (TREE_CODE_CLASS (code
))
14864 case tcc_comparison
:
14865 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
14867 TREE_OPERAND (t
, 0),
14868 TREE_OPERAND (t
, 1),
14869 strict_overflow_p
);
14872 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
14874 TREE_OPERAND (t
, 0),
14875 strict_overflow_p
);
14878 case tcc_declaration
:
14879 case tcc_reference
:
14880 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
);
14888 case TRUTH_AND_EXPR
:
14889 case TRUTH_OR_EXPR
:
14890 case TRUTH_XOR_EXPR
:
14891 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
14893 TREE_OPERAND (t
, 0),
14894 TREE_OPERAND (t
, 1),
14895 strict_overflow_p
);
14896 case TRUTH_NOT_EXPR
:
14897 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
14899 TREE_OPERAND (t
, 0),
14900 strict_overflow_p
);
14907 case WITH_SIZE_EXPR
:
14909 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
);
14912 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
);
14916 /* Return true if `t' is known to be non-negative. Handle warnings
14917 about undefined signed overflow. */
14920 tree_expr_nonnegative_p (tree t
)
14922 bool ret
, strict_overflow_p
;
14924 strict_overflow_p
= false;
14925 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
14926 if (strict_overflow_p
)
14927 fold_overflow_warning (("assuming signed overflow does not occur when "
14928 "determining that expression is always "
14930 WARN_STRICT_OVERFLOW_MISC
);
14935 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14936 For floating point we further ensure that T is not denormal.
14937 Similar logic is present in nonzero_address in rtlanal.h.
14939 If the return value is based on the assumption that signed overflow
14940 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14941 change *STRICT_OVERFLOW_P. */
14944 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
14945 bool *strict_overflow_p
)
14950 return tree_expr_nonzero_warnv_p (op0
,
14951 strict_overflow_p
);
14955 tree inner_type
= TREE_TYPE (op0
);
14956 tree outer_type
= type
;
14958 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
14959 && tree_expr_nonzero_warnv_p (op0
,
14960 strict_overflow_p
));
14964 case NON_LVALUE_EXPR
:
14965 return tree_expr_nonzero_warnv_p (op0
,
14966 strict_overflow_p
);
14975 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14976 For floating point we further ensure that T is not denormal.
14977 Similar logic is present in nonzero_address in rtlanal.h.
14979 If the return value is based on the assumption that signed overflow
14980 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14981 change *STRICT_OVERFLOW_P. */
14984 tree_binary_nonzero_warnv_p (enum tree_code code
,
14987 tree op1
, bool *strict_overflow_p
)
14989 bool sub_strict_overflow_p
;
14992 case POINTER_PLUS_EXPR
:
14994 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
14996 /* With the presence of negative values it is hard
14997 to say something. */
14998 sub_strict_overflow_p
= false;
14999 if (!tree_expr_nonnegative_warnv_p (op0
,
15000 &sub_strict_overflow_p
)
15001 || !tree_expr_nonnegative_warnv_p (op1
,
15002 &sub_strict_overflow_p
))
15004 /* One of operands must be positive and the other non-negative. */
15005 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15006 overflows, on a twos-complement machine the sum of two
15007 nonnegative numbers can never be zero. */
15008 return (tree_expr_nonzero_warnv_p (op0
,
15010 || tree_expr_nonzero_warnv_p (op1
,
15011 strict_overflow_p
));
15016 if (TYPE_OVERFLOW_UNDEFINED (type
))
15018 if (tree_expr_nonzero_warnv_p (op0
,
15020 && tree_expr_nonzero_warnv_p (op1
,
15021 strict_overflow_p
))
15023 *strict_overflow_p
= true;
15030 sub_strict_overflow_p
= false;
15031 if (tree_expr_nonzero_warnv_p (op0
,
15032 &sub_strict_overflow_p
)
15033 && tree_expr_nonzero_warnv_p (op1
,
15034 &sub_strict_overflow_p
))
15036 if (sub_strict_overflow_p
)
15037 *strict_overflow_p
= true;
15042 sub_strict_overflow_p
= false;
15043 if (tree_expr_nonzero_warnv_p (op0
,
15044 &sub_strict_overflow_p
))
15046 if (sub_strict_overflow_p
)
15047 *strict_overflow_p
= true;
15049 /* When both operands are nonzero, then MAX must be too. */
15050 if (tree_expr_nonzero_warnv_p (op1
,
15051 strict_overflow_p
))
15054 /* MAX where operand 0 is positive is positive. */
15055 return tree_expr_nonnegative_warnv_p (op0
,
15056 strict_overflow_p
);
15058 /* MAX where operand 1 is positive is positive. */
15059 else if (tree_expr_nonzero_warnv_p (op1
,
15060 &sub_strict_overflow_p
)
15061 && tree_expr_nonnegative_warnv_p (op1
,
15062 &sub_strict_overflow_p
))
15064 if (sub_strict_overflow_p
)
15065 *strict_overflow_p
= true;
15071 return (tree_expr_nonzero_warnv_p (op1
,
15073 || tree_expr_nonzero_warnv_p (op0
,
15074 strict_overflow_p
));
15083 /* Return true when T is an address and is known to be nonzero.
15084 For floating point we further ensure that T is not denormal.
15085 Similar logic is present in nonzero_address in rtlanal.h.
15087 If the return value is based on the assumption that signed overflow
15088 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15089 change *STRICT_OVERFLOW_P. */
15092 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
15094 bool sub_strict_overflow_p
;
15095 switch (TREE_CODE (t
))
15098 return !integer_zerop (t
);
15102 tree base
= TREE_OPERAND (t
, 0);
15104 if (!DECL_P (base
))
15105 base
= get_base_address (base
);
15110 /* For objects in symbol table check if we know they are non-zero.
15111 Don't do anything for variables and functions before symtab is built;
15112 it is quite possible that they will be declared weak later. */
15113 if (DECL_P (base
) && decl_in_symtab_p (base
))
15115 struct symtab_node
*symbol
;
15117 symbol
= symtab_node::get_create (base
);
15119 return symbol
->nonzero_address ();
15124 /* Function local objects are never NULL. */
15126 && (DECL_CONTEXT (base
)
15127 && TREE_CODE (DECL_CONTEXT (base
)) == FUNCTION_DECL
15128 && auto_var_in_fn_p (base
, DECL_CONTEXT (base
))))
15131 /* Constants are never weak. */
15132 if (CONSTANT_CLASS_P (base
))
15139 sub_strict_overflow_p
= false;
15140 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
15141 &sub_strict_overflow_p
)
15142 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
15143 &sub_strict_overflow_p
))
15145 if (sub_strict_overflow_p
)
15146 *strict_overflow_p
= true;
15157 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15158 attempt to fold the expression to a constant without modifying TYPE,
15161 If the expression could be simplified to a constant, then return
15162 the constant. If the expression would not be simplified to a
15163 constant, then return NULL_TREE. */
15166 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
15168 tree tem
= fold_binary (code
, type
, op0
, op1
);
15169 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15172 /* Given the components of a unary expression CODE, TYPE and OP0,
15173 attempt to fold the expression to a constant without modifying
15176 If the expression could be simplified to a constant, then return
15177 the constant. If the expression would not be simplified to a
15178 constant, then return NULL_TREE. */
15181 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
15183 tree tem
= fold_unary (code
, type
, op0
);
15184 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
15187 /* If EXP represents referencing an element in a constant string
15188 (either via pointer arithmetic or array indexing), return the
15189 tree representing the value accessed, otherwise return NULL. */
15192 fold_read_from_constant_string (tree exp
)
15194 if ((TREE_CODE (exp
) == INDIRECT_REF
15195 || TREE_CODE (exp
) == ARRAY_REF
)
15196 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
15198 tree exp1
= TREE_OPERAND (exp
, 0);
15201 location_t loc
= EXPR_LOCATION (exp
);
15203 if (TREE_CODE (exp
) == INDIRECT_REF
)
15204 string
= string_constant (exp1
, &index
);
15207 tree low_bound
= array_ref_low_bound (exp
);
15208 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
15210 /* Optimize the special-case of a zero lower bound.
15212 We convert the low_bound to sizetype to avoid some problems
15213 with constant folding. (E.g. suppose the lower bound is 1,
15214 and its mode is QI. Without the conversion,l (ARRAY
15215 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15216 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15217 if (! integer_zerop (low_bound
))
15218 index
= size_diffop_loc (loc
, index
,
15219 fold_convert_loc (loc
, sizetype
, low_bound
));
15225 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
15226 && TREE_CODE (string
) == STRING_CST
15227 && TREE_CODE (index
) == INTEGER_CST
15228 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
15229 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
15231 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
15232 return build_int_cst_type (TREE_TYPE (exp
),
15233 (TREE_STRING_POINTER (string
)
15234 [TREE_INT_CST_LOW (index
)]));
15239 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15240 an integer constant, real, or fixed-point constant.
15242 TYPE is the type of the result. */
15245 fold_negate_const (tree arg0
, tree type
)
15247 tree t
= NULL_TREE
;
15249 switch (TREE_CODE (arg0
))
15254 wide_int val
= wi::neg (arg0
, &overflow
);
15255 t
= force_fit_type (type
, val
, 1,
15256 (overflow
| TREE_OVERFLOW (arg0
))
15257 && !TYPE_UNSIGNED (type
));
15262 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15267 FIXED_VALUE_TYPE f
;
15268 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
15269 &(TREE_FIXED_CST (arg0
)), NULL
,
15270 TYPE_SATURATING (type
));
15271 t
= build_fixed (type
, f
);
15272 /* Propagate overflow flags. */
15273 if (overflow_p
| TREE_OVERFLOW (arg0
))
15274 TREE_OVERFLOW (t
) = 1;
15279 gcc_unreachable ();
15285 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15286 an integer constant or real constant.
15288 TYPE is the type of the result. */
15291 fold_abs_const (tree arg0
, tree type
)
15293 tree t
= NULL_TREE
;
15295 switch (TREE_CODE (arg0
))
15299 /* If the value is unsigned or non-negative, then the absolute value
15300 is the same as the ordinary value. */
15301 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
15304 /* If the value is negative, then the absolute value is
15309 wide_int val
= wi::neg (arg0
, &overflow
);
15310 t
= force_fit_type (type
, val
, -1,
15311 overflow
| TREE_OVERFLOW (arg0
));
15317 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
15318 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
15324 gcc_unreachable ();
15330 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15331 constant. TYPE is the type of the result. */
15334 fold_not_const (const_tree arg0
, tree type
)
15336 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
15338 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
15341 /* Given CODE, a relational operator, the target type, TYPE and two
15342 constant operands OP0 and OP1, return the result of the
15343 relational operation. If the result is not a compile time
15344 constant, then return NULL_TREE. */
15347 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
15349 int result
, invert
;
15351 /* From here on, the only cases we handle are when the result is
15352 known to be a constant. */
15354 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
15356 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
15357 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
15359 /* Handle the cases where either operand is a NaN. */
15360 if (real_isnan (c0
) || real_isnan (c1
))
15370 case UNORDERED_EXPR
:
15384 if (flag_trapping_math
)
15390 gcc_unreachable ();
15393 return constant_boolean_node (result
, type
);
15396 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
15399 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
15401 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
15402 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
15403 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
15406 /* Handle equality/inequality of complex constants. */
15407 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
15409 tree rcond
= fold_relational_const (code
, type
,
15410 TREE_REALPART (op0
),
15411 TREE_REALPART (op1
));
15412 tree icond
= fold_relational_const (code
, type
,
15413 TREE_IMAGPART (op0
),
15414 TREE_IMAGPART (op1
));
15415 if (code
== EQ_EXPR
)
15416 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
15417 else if (code
== NE_EXPR
)
15418 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
15423 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
15425 unsigned count
= VECTOR_CST_NELTS (op0
);
15426 tree
*elts
= XALLOCAVEC (tree
, count
);
15427 gcc_assert (VECTOR_CST_NELTS (op1
) == count
15428 && TYPE_VECTOR_SUBPARTS (type
) == count
);
15430 for (unsigned i
= 0; i
< count
; i
++)
15432 tree elem_type
= TREE_TYPE (type
);
15433 tree elem0
= VECTOR_CST_ELT (op0
, i
);
15434 tree elem1
= VECTOR_CST_ELT (op1
, i
);
15436 tree tem
= fold_relational_const (code
, elem_type
,
15439 if (tem
== NULL_TREE
)
15442 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
15445 return build_vector (type
, elts
);
15448 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15450 To compute GT, swap the arguments and do LT.
15451 To compute GE, do LT and invert the result.
15452 To compute LE, swap the arguments, do LT and invert the result.
15453 To compute NE, do EQ and invert the result.
15455 Therefore, the code below must handle only EQ and LT. */
15457 if (code
== LE_EXPR
|| code
== GT_EXPR
)
15459 std::swap (op0
, op1
);
15460 code
= swap_tree_comparison (code
);
15463 /* Note that it is safe to invert for real values here because we
15464 have already handled the one case that it matters. */
15467 if (code
== NE_EXPR
|| code
== GE_EXPR
)
15470 code
= invert_tree_comparison (code
, false);
15473 /* Compute a result for LT or EQ if args permit;
15474 Otherwise return T. */
15475 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
15477 if (code
== EQ_EXPR
)
15478 result
= tree_int_cst_equal (op0
, op1
);
15480 result
= tree_int_cst_lt (op0
, op1
);
15487 return constant_boolean_node (result
, type
);
15490 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15491 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15495 fold_build_cleanup_point_expr (tree type
, tree expr
)
15497 /* If the expression does not have side effects then we don't have to wrap
15498 it with a cleanup point expression. */
15499 if (!TREE_SIDE_EFFECTS (expr
))
15502 /* If the expression is a return, check to see if the expression inside the
15503 return has no side effects or the right hand side of the modify expression
15504 inside the return. If either don't have side effects set we don't need to
15505 wrap the expression in a cleanup point expression. Note we don't check the
15506 left hand side of the modify because it should always be a return decl. */
15507 if (TREE_CODE (expr
) == RETURN_EXPR
)
15509 tree op
= TREE_OPERAND (expr
, 0);
15510 if (!op
|| !TREE_SIDE_EFFECTS (op
))
15512 op
= TREE_OPERAND (op
, 1);
15513 if (!TREE_SIDE_EFFECTS (op
))
15517 return build1 (CLEANUP_POINT_EXPR
, type
, expr
);
15520 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15521 of an indirection through OP0, or NULL_TREE if no simplification is
15525 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
15531 subtype
= TREE_TYPE (sub
);
15532 if (!POINTER_TYPE_P (subtype
))
15535 if (TREE_CODE (sub
) == ADDR_EXPR
)
15537 tree op
= TREE_OPERAND (sub
, 0);
15538 tree optype
= TREE_TYPE (op
);
15539 /* *&CONST_DECL -> to the value of the const decl. */
15540 if (TREE_CODE (op
) == CONST_DECL
)
15541 return DECL_INITIAL (op
);
15542 /* *&p => p; make sure to handle *&"str"[cst] here. */
15543 if (type
== optype
)
15545 tree fop
= fold_read_from_constant_string (op
);
15551 /* *(foo *)&fooarray => fooarray[0] */
15552 else if (TREE_CODE (optype
) == ARRAY_TYPE
15553 && type
== TREE_TYPE (optype
)
15554 && (!in_gimple_form
15555 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
15557 tree type_domain
= TYPE_DOMAIN (optype
);
15558 tree min_val
= size_zero_node
;
15559 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15560 min_val
= TYPE_MIN_VALUE (type_domain
);
15562 && TREE_CODE (min_val
) != INTEGER_CST
)
15564 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
15565 NULL_TREE
, NULL_TREE
);
15567 /* *(foo *)&complexfoo => __real__ complexfoo */
15568 else if (TREE_CODE (optype
) == COMPLEX_TYPE
15569 && type
== TREE_TYPE (optype
))
15570 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
15571 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15572 else if (TREE_CODE (optype
) == VECTOR_TYPE
15573 && type
== TREE_TYPE (optype
))
15575 tree part_width
= TYPE_SIZE (type
);
15576 tree index
= bitsize_int (0);
15577 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
15581 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
15582 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
15584 tree op00
= TREE_OPERAND (sub
, 0);
15585 tree op01
= TREE_OPERAND (sub
, 1);
15588 if (TREE_CODE (op00
) == ADDR_EXPR
)
15591 op00
= TREE_OPERAND (op00
, 0);
15592 op00type
= TREE_TYPE (op00
);
15594 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15595 if (TREE_CODE (op00type
) == VECTOR_TYPE
15596 && type
== TREE_TYPE (op00type
))
15598 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
15599 tree part_width
= TYPE_SIZE (type
);
15600 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
15601 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
15602 tree index
= bitsize_int (indexi
);
15604 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
15605 return fold_build3_loc (loc
,
15606 BIT_FIELD_REF
, type
, op00
,
15607 part_width
, index
);
15610 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15611 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
15612 && type
== TREE_TYPE (op00type
))
15614 tree size
= TYPE_SIZE_UNIT (type
);
15615 if (tree_int_cst_equal (size
, op01
))
15616 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
15618 /* ((foo *)&fooarray)[1] => fooarray[1] */
15619 else if (TREE_CODE (op00type
) == ARRAY_TYPE
15620 && type
== TREE_TYPE (op00type
))
15622 tree type_domain
= TYPE_DOMAIN (op00type
);
15623 tree min_val
= size_zero_node
;
15624 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15625 min_val
= TYPE_MIN_VALUE (type_domain
);
15626 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
15627 TYPE_SIZE_UNIT (type
));
15628 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
15629 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
15630 NULL_TREE
, NULL_TREE
);
15635 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15636 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
15637 && type
== TREE_TYPE (TREE_TYPE (subtype
))
15638 && (!in_gimple_form
15639 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
15642 tree min_val
= size_zero_node
;
15643 sub
= build_fold_indirect_ref_loc (loc
, sub
);
15644 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
15645 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
15646 min_val
= TYPE_MIN_VALUE (type_domain
);
15648 && TREE_CODE (min_val
) != INTEGER_CST
)
15650 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
15657 /* Builds an expression for an indirection through T, simplifying some
15661 build_fold_indirect_ref_loc (location_t loc
, tree t
)
15663 tree type
= TREE_TYPE (TREE_TYPE (t
));
15664 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
15669 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
15672 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15675 fold_indirect_ref_loc (location_t loc
, tree t
)
15677 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
15685 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15686 whose result is ignored. The type of the returned tree need not be
15687 the same as the original expression. */
15690 fold_ignored_result (tree t
)
15692 if (!TREE_SIDE_EFFECTS (t
))
15693 return integer_zero_node
;
15696 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
15699 t
= TREE_OPERAND (t
, 0);
15703 case tcc_comparison
:
15704 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
15705 t
= TREE_OPERAND (t
, 0);
15706 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
15707 t
= TREE_OPERAND (t
, 1);
15712 case tcc_expression
:
15713 switch (TREE_CODE (t
))
15715 case COMPOUND_EXPR
:
15716 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
15718 t
= TREE_OPERAND (t
, 0);
15722 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
15723 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
15725 t
= TREE_OPERAND (t
, 0);
15738 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15741 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
15743 tree div
= NULL_TREE
;
15748 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15749 have to do anything. Only do this when we are not given a const,
15750 because in that case, this check is more expensive than just
15752 if (TREE_CODE (value
) != INTEGER_CST
)
15754 div
= build_int_cst (TREE_TYPE (value
), divisor
);
15756 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
15760 /* If divisor is a power of two, simplify this to bit manipulation. */
15761 if (divisor
== (divisor
& -divisor
))
15763 if (TREE_CODE (value
) == INTEGER_CST
)
15765 wide_int val
= value
;
15768 if ((val
& (divisor
- 1)) == 0)
15771 overflow_p
= TREE_OVERFLOW (value
);
15772 val
+= divisor
- 1;
15773 val
&= - (int) divisor
;
15777 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
15783 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
15784 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
15785 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
15786 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
15792 div
= build_int_cst (TREE_TYPE (value
), divisor
);
15793 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
15794 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
15800 /* Likewise, but round down. */
15803 round_down_loc (location_t loc
, tree value
, int divisor
)
15805 tree div
= NULL_TREE
;
15807 gcc_assert (divisor
> 0);
15811 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15812 have to do anything. Only do this when we are not given a const,
15813 because in that case, this check is more expensive than just
15815 if (TREE_CODE (value
) != INTEGER_CST
)
15817 div
= build_int_cst (TREE_TYPE (value
), divisor
);
15819 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
15823 /* If divisor is a power of two, simplify this to bit manipulation. */
15824 if (divisor
== (divisor
& -divisor
))
15828 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
15829 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
15834 div
= build_int_cst (TREE_TYPE (value
), divisor
);
15835 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
15836 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
15842 /* Returns the pointer to the base of the object addressed by EXP and
15843 extracts the information about the offset of the access, storing it
15844 to PBITPOS and POFFSET. */
15847 split_address_to_core_and_offset (tree exp
,
15848 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
15852 int unsignedp
, volatilep
;
15853 HOST_WIDE_INT bitsize
;
15854 location_t loc
= EXPR_LOCATION (exp
);
15856 if (TREE_CODE (exp
) == ADDR_EXPR
)
15858 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
15859 poffset
, &mode
, &unsignedp
, &volatilep
,
15861 core
= build_fold_addr_expr_loc (loc
, core
);
15867 *poffset
= NULL_TREE
;
15873 /* Returns true if addresses of E1 and E2 differ by a constant, false
15874 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15877 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
15880 HOST_WIDE_INT bitpos1
, bitpos2
;
15881 tree toffset1
, toffset2
, tdiff
, type
;
15883 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
15884 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
15886 if (bitpos1
% BITS_PER_UNIT
!= 0
15887 || bitpos2
% BITS_PER_UNIT
!= 0
15888 || !operand_equal_p (core1
, core2
, 0))
15891 if (toffset1
&& toffset2
)
15893 type
= TREE_TYPE (toffset1
);
15894 if (type
!= TREE_TYPE (toffset2
))
15895 toffset2
= fold_convert (type
, toffset2
);
15897 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
15898 if (!cst_and_fits_in_hwi (tdiff
))
15901 *diff
= int_cst_value (tdiff
);
15903 else if (toffset1
|| toffset2
)
15905 /* If only one of the offsets is non-constant, the difference cannot
15912 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
15916 /* Simplify the floating point expression EXP when the sign of the
15917 result is not significant. Return NULL_TREE if no simplification
15921 fold_strip_sign_ops (tree exp
)
15924 location_t loc
= EXPR_LOCATION (exp
);
15926 switch (TREE_CODE (exp
))
15930 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 0));
15931 return arg0
? arg0
: TREE_OPERAND (exp
, 0);
15935 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp
)))
15937 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 0));
15938 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
15939 if (arg0
!= NULL_TREE
|| arg1
!= NULL_TREE
)
15940 return fold_build2_loc (loc
, TREE_CODE (exp
), TREE_TYPE (exp
),
15941 arg0
? arg0
: TREE_OPERAND (exp
, 0),
15942 arg1
? arg1
: TREE_OPERAND (exp
, 1));
15945 case COMPOUND_EXPR
:
15946 arg0
= TREE_OPERAND (exp
, 0);
15947 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
15949 return fold_build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (exp
), arg0
, arg1
);
15953 arg0
= fold_strip_sign_ops (TREE_OPERAND (exp
, 1));
15954 arg1
= fold_strip_sign_ops (TREE_OPERAND (exp
, 2));
15956 return fold_build3_loc (loc
,
15957 COND_EXPR
, TREE_TYPE (exp
), TREE_OPERAND (exp
, 0),
15958 arg0
? arg0
: TREE_OPERAND (exp
, 1),
15959 arg1
? arg1
: TREE_OPERAND (exp
, 2));
15964 const enum built_in_function fcode
= builtin_mathfn_code (exp
);
15967 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
15968 /* Strip copysign function call, return the 1st argument. */
15969 arg0
= CALL_EXPR_ARG (exp
, 0);
15970 arg1
= CALL_EXPR_ARG (exp
, 1);
15971 return omit_one_operand_loc (loc
, TREE_TYPE (exp
), arg0
, arg1
);
15974 /* Strip sign ops from the argument of "odd" math functions. */
15975 if (negate_mathfn_p (fcode
))
15977 arg0
= fold_strip_sign_ops (CALL_EXPR_ARG (exp
, 0));
15979 return build_call_expr_loc (loc
, get_callee_fndecl (exp
), 1, arg0
);
15992 /* Return OFF converted to a pointer offset type suitable as offset for
15993 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
15995 convert_to_ptrofftype_loc (location_t loc
, tree off
)
15997 return fold_convert_loc (loc
, sizetype
, off
);
16000 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16002 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
16004 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16005 ptr
, convert_to_ptrofftype_loc (loc
, off
));
16008 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16010 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
16012 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
16013 ptr
, size_int (off
));